JIT.h revision f32a6a3091e0b01d17926f4b1cf78972854b8cb5
1//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
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 the top-level JIT data structure.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef JIT_H
15#define JIT_H
16
17#include "llvm/ExecutionEngine/ExecutionEngine.h"
18#include "llvm/PassManager.h"
19#include "llvm/Support/ValueHandle.h"
20
21namespace llvm {
22
23class Function;
24struct JITEvent_EmittedFunctionDetails;
25class MachineCodeEmitter;
26class MachineCodeInfo;
27class TargetJITInfo;
28class TargetMachine;
29
30class JITState {
31private:
32  FunctionPassManager PM;  // Passes to compile a function
33  ModuleProvider *MP;      // ModuleProvider used to create the PM
34
35  /// PendingFunctions - Functions which have not been code generated yet, but
36  /// were called from a function being code generated.
37  std::vector<AssertingVH<Function> > PendingFunctions;
38
39public:
40  explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
41
42  FunctionPassManager &getPM(const MutexGuard &L) {
43    return PM;
44  }
45
46  ModuleProvider *getMP() const { return MP; }
47  std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48    return PendingFunctions;
49  }
50};
51
52
53class JIT : public ExecutionEngine {
54  TargetMachine &TM;       // The current target we are compiling to
55  TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
56  JITCodeEmitter *JCE;     // JCE object
57  std::vector<JITEventListener*> EventListeners;
58
59  /// AllocateGVsWithCode - Some applications require that global variables and
60  /// code be allocated into the same region of memory, in which case this flag
61  /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
62  bool AllocateGVsWithCode;
63
64  JITState *jitstate;
65
66  JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
67      JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68      bool AllocateGVsWithCode);
69public:
70  ~JIT();
71
72  static void Register() {
73    JITCtor = create;
74  }
75
76  /// getJITInfo - Return the target JIT information structure.
77  ///
78  TargetJITInfo &getJITInfo() const { return TJI; }
79
80  /// create - Create an return a new JIT compiler if there is one available
81  /// for the current target.  Otherwise, return null.
82  ///
83  static ExecutionEngine *create(ModuleProvider *MP,
84                                 std::string *Err,
85                                 JITMemoryManager *JMM,
86                                 CodeGenOpt::Level OptLevel =
87                                   CodeGenOpt::Default,
88                                 bool GVsWithCode = true) {
89    return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode);
90  }
91
92  virtual void addModuleProvider(ModuleProvider *MP);
93
94  /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
95  /// Relases the Module from the ModuleProvider, materializing it in the
96  /// process, and returns the materialized Module.
97  virtual Module *removeModuleProvider(ModuleProvider *MP,
98                                       std::string *ErrInfo = 0);
99
100  /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
101  /// and deletes the ModuleProvider and owned Module.  Avoids materializing
102  /// the underlying module.
103  virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
104
105  /// runFunction - Start execution with the specified function and arguments.
106  ///
107  virtual GenericValue runFunction(Function *F,
108                                   const std::vector<GenericValue> &ArgValues);
109
110  /// getPointerToNamedFunction - This method returns the address of the
111  /// specified function by using the dlsym function call.  As such it is only
112  /// useful for resolving library symbols, not code generated symbols.
113  ///
114  /// If AbortOnFailure is false and no function with the given name is
115  /// found, this function silently returns a null pointer. Otherwise,
116  /// it prints a message to stderr and aborts.
117  ///
118  void *getPointerToNamedFunction(const std::string &Name,
119                                  bool AbortOnFailure = true);
120
121  // CompilationCallback - Invoked the first time that a call site is found,
122  // which causes lazy compilation of the target function.
123  //
124  static void CompilationCallback();
125
126  /// getPointerToFunction - This returns the address of the specified function,
127  /// compiling it if necessary.
128  ///
129  void *getPointerToFunction(Function *F);
130
131  void *getPointerToBasicBlock(BasicBlock *BB) {
132    assert(0 && "JIT does not support address-of-label yet!");
133  }
134
135  /// getOrEmitGlobalVariable - Return the address of the specified global
136  /// variable, possibly emitting it to memory if needed.  This is used by the
137  /// Emitter.
138  void *getOrEmitGlobalVariable(const GlobalVariable *GV);
139
140  /// getPointerToFunctionOrStub - If the specified function has been
141  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
142  /// a stub to implement lazy compilation if available.
143  ///
144  void *getPointerToFunctionOrStub(Function *F);
145
146  /// recompileAndRelinkFunction - This method is used to force a function
147  /// which has already been compiled, to be compiled again, possibly
148  /// after it has been modified. Then the entry to the old copy is overwritten
149  /// with a branch to the new copy. If there was no old copy, this acts
150  /// just like JIT::getPointerToFunction().
151  ///
152  void *recompileAndRelinkFunction(Function *F);
153
154  /// freeMachineCodeForFunction - deallocate memory used to code-generate this
155  /// Function.
156  ///
157  void freeMachineCodeForFunction(Function *F);
158
159  /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
160  /// function was encountered.  Add it to a pending list to be processed after
161  /// the current function.
162  ///
163  void addPendingFunction(Function *F);
164
165  /// getCodeEmitter - Return the code emitter this JIT is emitting into.
166  ///
167  JITCodeEmitter *getCodeEmitter() const { return JCE; }
168
169  /// selectTarget - Pick a target either via -march or by guessing the native
170  /// arch.  Add any CPU features specified via -mcpu or -mattr.
171  static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
172
173  static ExecutionEngine *createJIT(ModuleProvider *MP,
174                                    std::string *ErrorStr,
175                                    JITMemoryManager *JMM,
176                                    CodeGenOpt::Level OptLevel,
177                                    bool GVsWithCode);
178
179  // Run the JIT on F and return information about the generated code
180  void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
181
182  virtual void RegisterJITEventListener(JITEventListener *L);
183  virtual void UnregisterJITEventListener(JITEventListener *L);
184  /// These functions correspond to the methods on JITEventListener.  They
185  /// iterate over the registered listeners and call the corresponding method on
186  /// each.
187  void NotifyFunctionEmitted(
188      const Function &F, void *Code, size_t Size,
189      const JITEvent_EmittedFunctionDetails &Details);
190  void NotifyFreeingMachineCode(void *OldPtr);
191
192private:
193  static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
194                                       TargetMachine &tm);
195  void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
196  void updateFunctionStub(Function *F);
197  void updateDlsymStubTable();
198
199protected:
200
201  /// getMemoryforGV - Allocate memory for a global variable.
202  virtual char* getMemoryForGV(const GlobalVariable* GV);
203
204};
205
206} // End llvm namespace
207
208#endif
209