LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lib/ExecutionEngine/JIT/JIT.h
Go to the documentation of this file.
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 
18 #include "llvm/PassManager.h"
20 
21 namespace llvm {
22 
23 class Function;
24 struct JITEvent_EmittedFunctionDetails;
25 class MachineCodeEmitter;
26 class MachineCodeInfo;
27 class TargetJITInfo;
28 class TargetMachine;
29 
30 class JITState {
31 private:
32  FunctionPassManager PM; // Passes to compile a function
33  Module *M; // Module 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 
39 public:
40  explicit JITState(Module *M) : PM(M), M(M) {}
41 
43  return PM;
44  }
45 
46  Module *getModule() const { return M; }
47  std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48  return PendingFunctions;
49  }
50 };
51 
52 
53 class JIT : public ExecutionEngine {
54  /// types
57  /// data
58  TargetMachine &TM; // The current target we are compiling to
59  TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
60  JITCodeEmitter *JCE; // JCE object
61  JITMemoryManager *JMM;
62  std::vector<JITEventListener*> EventListeners;
63 
64  /// AllocateGVsWithCode - Some applications require that global variables and
65  /// code be allocated into the same region of memory, in which case this flag
66  /// should be set to true. Doing so breaks freeMachineCodeForFunction.
67  bool AllocateGVsWithCode;
68 
69  /// True while the JIT is generating code. Used to assert against recursive
70  /// entry.
71  bool isAlreadyCodeGenerating;
72 
73  JITState *jitstate;
74 
75  /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their
76  /// actualized version, only filled for basic blocks that have their address
77  /// taken.
78  BasicBlockAddressMapTy BasicBlockAddressMap;
79 
80 
81  JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
82  JITMemoryManager *JMM, bool AllocateGVsWithCode);
83 public:
84  ~JIT();
85 
86  static void Register() {
88  }
89 
90  /// getJITInfo - Return the target JIT information structure.
91  ///
92  TargetJITInfo &getJITInfo() const { return TJI; }
93 
94  /// create - Create an return a new JIT compiler if there is one available
95  /// for the current target. Otherwise, return null.
96  ///
98  std::string *Err,
99  JITMemoryManager *JMM,
100  CodeGenOpt::Level OptLevel =
102  bool GVsWithCode = true,
105  return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
106  RM, CMM);
107  }
108 
109  virtual void addModule(Module *M);
110 
111  /// removeModule - Remove a Module from the list of modules. Returns true if
112  /// M is found.
113  virtual bool removeModule(Module *M);
114 
115  /// runFunction - Start execution with the specified function and arguments.
116  ///
118  const std::vector<GenericValue> &ArgValues);
119 
120  /// getPointerToNamedFunction - This method returns the address of the
121  /// specified function by using the MemoryManager. As such it is only
122  /// useful for resolving library symbols, not code generated symbols.
123  ///
124  /// If AbortOnFailure is false and no function with the given name is
125  /// found, this function silently returns a null pointer. Otherwise,
126  /// it prints a message to stderr and aborts.
127  ///
128  virtual void *getPointerToNamedFunction(const std::string &Name,
129  bool AbortOnFailure = true);
130 
131  // CompilationCallback - Invoked the first time that a call site is found,
132  // which causes lazy compilation of the target function.
133  //
134  static void CompilationCallback();
135 
136  /// getPointerToFunction - This returns the address of the specified function,
137  /// compiling it if necessary.
138  ///
140 
141  /// addPointerToBasicBlock - Adds address of the specific basic block.
142  void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
143 
144  /// clearPointerToBasicBlock - Removes address of specific basic block.
145  void clearPointerToBasicBlock(const BasicBlock *BB);
146 
147  /// getPointerToBasicBlock - This returns the address of the specified basic
148  /// block, assuming function is compiled.
150 
151  /// getOrEmitGlobalVariable - Return the address of the specified global
152  /// variable, possibly emitting it to memory if needed. This is used by the
153  /// Emitter.
154  void *getOrEmitGlobalVariable(const GlobalVariable *GV);
155 
156  /// getPointerToFunctionOrStub - If the specified function has been
157  /// code-gen'd, return a pointer to the function. If not, compile it, or use
158  /// a stub to implement lazy compilation if available.
159  ///
161 
162  /// recompileAndRelinkFunction - This method is used to force a function
163  /// which has already been compiled, to be compiled again, possibly
164  /// after it has been modified. Then the entry to the old copy is overwritten
165  /// with a branch to the new copy. If there was no old copy, this acts
166  /// just like JIT::getPointerToFunction().
167  ///
169 
170  /// freeMachineCodeForFunction - deallocate memory used to code-generate this
171  /// Function.
172  ///
174 
175  /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
176  /// function was encountered. Add it to a pending list to be processed after
177  /// the current function.
178  ///
180 
181  /// getCodeEmitter - Return the code emitter this JIT is emitting into.
182  ///
183  JITCodeEmitter *getCodeEmitter() const { return JCE; }
184 
185  static ExecutionEngine *createJIT(Module *M,
186  std::string *ErrorStr,
187  JITMemoryManager *JMM,
188  bool GVsWithCode,
189  TargetMachine *TM);
190 
191  // Run the JIT on F and return information about the generated code
192  void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
193 
196  /// These functions correspond to the methods on JITEventListener. They
197  /// iterate over the registered listeners and call the corresponding method on
198  /// each.
200  const Function &F, void *Code, size_t Size,
201  const JITEvent_EmittedFunctionDetails &Details);
202  void NotifyFreeingMachineCode(void *OldPtr);
203 
204  BasicBlockAddressMapTy &
206  return BasicBlockAddressMap;
207  }
208 
209 
210 private:
211  static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
212  TargetMachine &tm);
213  void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
214  void updateFunctionStub(Function *F);
215  void jitTheFunction(Function *F, const MutexGuard &locked);
216 
217 protected:
218 
219  /// getMemoryforGV - Allocate memory for a global variable.
220  virtual char* getMemoryForGV(const GlobalVariable* GV);
221 
222 };
223 
224 } // End llvm namespace
225 
226 #endif
void NotifyFunctionEmitted(const Function &F, void *Code, size_t Size, const JITEvent_EmittedFunctionDetails &Details)
Definition: JIT.cpp:421
virtual void UnregisterJITEventListener(JITEventListener *L)
Definition: JIT.cpp:410
void * getPointerToFunction(Function *F)
Definition: JIT.cpp:500
BasicBlockAddressMapTy & getBasicBlockAddressMap(const MutexGuard &)
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
void addPointerToBasicBlock(const BasicBlock *BB, void *Addr)
addPointerToBasicBlock - Adds address of the specific basic block.
Definition: JIT.cpp:533
static ExecutionEngine * createJIT(Module *M, std::string *ErrorStr, JITMemoryManager *JMM, bool GVsWithCode, TargetMachine *TM)
Definition: JIT.cpp:73
F(f)
FunctionPassManager & getPM(const MutexGuard &L)
static void CompilationCallback()
void * getPointerToFunctionOrStub(Function *F)
static ExecutionEngine * create(Module *M, std::string *Err, JITMemoryManager *JMM, CodeGenOpt::Level OptLevel=CodeGenOpt::Default, bool GVsWithCode=true, Reloc::Model RM=Reloc::Default, CodeModel::Model CMM=CodeModel::JITDefault)
void runJITOnFunction(Function *F, MachineCodeInfo *MCI=0)
Definition: JIT.cpp:442
static void Register()
Guard a section of code with a Mutex.
Definition: MutexGuard.h:27
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
std::vector< AssertingVH< Function > > & getPendingFunctions(const MutexGuard &L)
static ExecutionEngine * createJIT(Module *M, std::string *ErrorStr=0, JITMemoryManager *JMM=0, CodeGenOpt::Level OptLevel=CodeGenOpt::Default, bool GVsWithCode=true, Reloc::Model RM=Reloc::Default, CodeModel::Model CMM=CodeModel::JITDefault)
void * getOrEmitGlobalVariable(const GlobalVariable *GV)
Definition: JIT.cpp:591
~JIT()
Definition: JIT.cpp:166
void freeMachineCodeForFunction(Function *F)
void addPendingFunction(Function *F)
Definition: JIT.cpp:686
void clearPointerToBasicBlock(const BasicBlock *BB)
clearPointerToBasicBlock - Removes address of specific basic block.
Definition: JIT.cpp:545
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void * getPointerToBasicBlock(BasicBlock *BB)
Definition: JIT.cpp:550
void * recompileAndRelinkFunction(Function *F)
Definition: JIT.cpp:625
Module * getModule() const
void NotifyFreeingMachineCode(void *OldPtr)
Definition: JIT.cpp:431
virtual void addModule(Module *M)
Definition: JIT.cpp:177
JITCodeEmitter * getCodeEmitter() const
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
Definition: JIT.cpp:567
virtual char * getMemoryForGV(const GlobalVariable *GV)
getMemoryforGV - Allocate memory for a global variable.
Definition: JIT.cpp:648
TargetJITInfo & getJITInfo() const
virtual bool removeModule(Module *M)
Definition: JIT.cpp:203
static ExecutionEngine *(* JITCtor)(Module *M, std::string *ErrorStr, JITMemoryManager *JMM, bool GVsWithCode, TargetMachine *TM)
virtual void RegisterJITEventListener(JITEventListener *L)
Definition: JIT.cpp:404
virtual GenericValue runFunction(Function *F, const std::vector< GenericValue > &ArgValues)
Definition: JIT.cpp:233