b23ca88baf1a7f0188f962a39bcf9e2c2cfc823a
[oota-llvm.git] / lib / ExecutionEngine / IntelJITEvents / IntelJITEventListener.cpp
1 //===-- IntelJITEventListener.cpp - Tell Intel profiler about JITed code --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a JITEventListener object to tell Intel(R) VTune(TM)
11 // Amplifier XE 2011 about JITted functions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/JITEventListener.h"
17
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Metadata.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/DebugInfo/DIContext.h"
24 #include "llvm/ExecutionEngine/ObjectImage.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/Errno.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "EventListenerCommon.h"
31 #include "IntelJITEventsWrapper.h"
32
33 using namespace llvm;
34 using namespace llvm::jitprofiling;
35
36 #define DEBUG_TYPE "amplifier-jit-event-listener"
37
38 namespace {
39
40 class IntelJITEventListener : public JITEventListener {
41   typedef DenseMap<void*, unsigned int> MethodIDMap;
42
43   std::unique_ptr<IntelJITEventsWrapper> Wrapper;
44   MethodIDMap MethodIDs;
45   FilenameCache Filenames;
46
47   typedef SmallVector<const void *, 64> MethodAddressVector;
48   typedef DenseMap<const void *, MethodAddressVector>  ObjectMap;
49
50   ObjectMap  LoadedObjectMap;
51
52 public:
53   IntelJITEventListener(IntelJITEventsWrapper* libraryWrapper) {
54       Wrapper.reset(libraryWrapper);
55   }
56
57   ~IntelJITEventListener() {
58   }
59
60   virtual void NotifyObjectEmitted(const ObjectImage &Obj);
61
62   virtual void NotifyFreeingObject(const ObjectImage &Obj);
63 };
64
65 static LineNumberInfo DILineInfoToIntelJITFormat(uintptr_t StartAddress,
66                                                  uintptr_t Address,
67                                                  DILineInfo Line) {
68   LineNumberInfo Result;
69
70   Result.Offset = Address - StartAddress;
71   Result.LineNumber = Line.Line;
72
73   return Result;
74 }
75
76 static iJIT_Method_Load FunctionDescToIntelJITFormat(
77     IntelJITEventsWrapper& Wrapper,
78     const char* FnName,
79     uintptr_t FnStart,
80     size_t FnSize) {
81   iJIT_Method_Load Result;
82   memset(&Result, 0, sizeof(iJIT_Method_Load));
83
84   Result.method_id = Wrapper.iJIT_GetNewMethodID();
85   Result.method_name = const_cast<char*>(FnName);
86   Result.method_load_address = reinterpret_cast<void*>(FnStart);
87   Result.method_size = FnSize;
88
89   Result.class_id = 0;
90   Result.class_file_name = NULL;
91   Result.user_data = NULL;
92   Result.user_data_size = 0;
93   Result.env = iJDE_JittingAPI;
94
95   return Result;
96 }
97
98 void IntelJITEventListener::NotifyObjectEmitted(const ObjectImage &Obj) {
99   // Get the address of the object image for use as a unique identifier
100   const void* ObjData = Obj.getData().data();
101   DIContext* Context = DIContext::getDWARFContext(*Obj.getObjectFile());
102   MethodAddressVector Functions;
103
104   // Use symbol info to iterate functions in the object.
105   for (object::symbol_iterator I = Obj.begin_symbols(),
106                                E = Obj.end_symbols();
107                         I != E;
108                         ++I) {
109     std::vector<LineNumberInfo> LineInfo;
110     std::string SourceFileName;
111
112     object::SymbolRef::Type SymType;
113     if (I->getType(SymType)) continue;
114     if (SymType == object::SymbolRef::ST_Function) {
115       StringRef  Name;
116       uint64_t   Addr;
117       uint64_t   Size;
118       if (I->getName(Name)) continue;
119       if (I->getAddress(Addr)) continue;
120       if (I->getSize(Size)) continue;
121
122       // Record this address in a local vector
123       Functions.push_back((void*)Addr);
124
125       // Build the function loaded notification message
126       iJIT_Method_Load FunctionMessage = FunctionDescToIntelJITFormat(*Wrapper,
127                                            Name.data(),
128                                            Addr,
129                                            Size);
130       if (Context) {
131         DILineInfoTable  Lines = Context->getLineInfoForAddressRange(Addr, Size);
132         DILineInfoTable::iterator  Begin = Lines.begin();
133         DILineInfoTable::iterator  End = Lines.end();
134         for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
135           LineInfo.push_back(DILineInfoToIntelJITFormat((uintptr_t)Addr,
136                                                         It->first,
137                                                         It->second));
138         }
139         if (LineInfo.size() == 0) {
140           FunctionMessage.source_file_name = 0;
141           FunctionMessage.line_number_size = 0;
142           FunctionMessage.line_number_table = 0;
143         } else {
144           SourceFileName = Lines.front().second.FileName;
145           FunctionMessage.source_file_name = const_cast<char *>(SourceFileName.c_str());
146           FunctionMessage.line_number_size = LineInfo.size();
147           FunctionMessage.line_number_table = &*LineInfo.begin();
148         }
149       } else {
150         FunctionMessage.source_file_name = 0;
151         FunctionMessage.line_number_size = 0;
152         FunctionMessage.line_number_table = 0;
153       }
154
155       Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED,
156                                 &FunctionMessage);
157       MethodIDs[(void*)Addr] = FunctionMessage.method_id;
158     }
159   }
160
161   // To support object unload notification, we need to keep a list of
162   // registered function addresses for each loaded object.  We will
163   // use the MethodIDs map to get the registered ID for each function.
164   LoadedObjectMap[ObjData] = Functions;
165 }
166
167 void IntelJITEventListener::NotifyFreeingObject(const ObjectImage &Obj) {
168   // Get the address of the object image for use as a unique identifier
169   const void* ObjData = Obj.getData().data();
170
171   // Get the object's function list from LoadedObjectMap
172   ObjectMap::iterator OI = LoadedObjectMap.find(ObjData);
173   if (OI == LoadedObjectMap.end())
174     return;
175   MethodAddressVector& Functions = OI->second;
176
177   // Walk the function list, unregistering each function
178   for (MethodAddressVector::iterator FI = Functions.begin(),
179                                      FE = Functions.end();
180        FI != FE;
181        ++FI) {
182     void* FnStart = const_cast<void*>(*FI);
183     MethodIDMap::iterator MI = MethodIDs.find(FnStart);
184     if (MI != MethodIDs.end()) {
185       Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_UNLOAD_START,
186                                 &MI->second);
187       MethodIDs.erase(MI);
188     }
189   }
190
191   // Erase the object from LoadedObjectMap
192   LoadedObjectMap.erase(OI);
193 }
194
195 }  // anonymous namespace.
196
197 namespace llvm {
198 JITEventListener *JITEventListener::createIntelJITEventListener() {
199   return new IntelJITEventListener(new IntelJITEventsWrapper);
200 }
201
202 // for testing
203 JITEventListener *JITEventListener::createIntelJITEventListener(
204                                       IntelJITEventsWrapper* TestImpl) {
205   return new IntelJITEventListener(TestImpl);
206 }
207
208 } // namespace llvm
209