1 //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the top-level JIT data structure.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/IR/ValueHandle.h"
19 #include "llvm/PassManager.h"
24 struct JITEvent_EmittedFunctionDetails;
25 class MachineCodeEmitter;
26 class MachineCodeInfo;
32 FunctionPassManager PM; // Passes to compile a function
33 Module *M; // Module used to create the PM
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;
40 explicit JITState(Module *M) : PM(M), M(M) {}
42 FunctionPassManager &getPM(const MutexGuard &L) {
46 Module *getModule() const { return M; }
47 std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48 return PendingFunctions;
53 class JIT : public ExecutionEngine {
55 typedef ValueMap<const BasicBlock *, void *>
56 BasicBlockAddressMapTy;
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;
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;
69 /// True while the JIT is generating code. Used to assert against recursive
71 bool isAlreadyCodeGenerating;
75 /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their
76 /// actualized version, only filled for basic blocks that have their address
78 BasicBlockAddressMapTy BasicBlockAddressMap;
81 JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
82 JITMemoryManager *JMM, bool AllocateGVsWithCode);
86 static void Register() {
90 /// getJITInfo - Return the target JIT information structure.
92 TargetJITInfo &getJITInfo() const { return TJI; }
94 /// create - Create an return a new JIT compiler if there is one available
95 /// for the current target. Otherwise, return null.
97 static ExecutionEngine *create(Module *M,
99 JITMemoryManager *JMM,
100 CodeGenOpt::Level OptLevel =
102 bool GVsWithCode = true,
103 Reloc::Model RM = Reloc::Default,
104 CodeModel::Model CMM = CodeModel::JITDefault) {
105 return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
109 void addModule(Module *M) override;
111 /// removeModule - Remove a Module from the list of modules. Returns true if
113 bool removeModule(Module *M) override;
115 /// runFunction - Start execution with the specified function and arguments.
117 GenericValue runFunction(Function *F,
118 const std::vector<GenericValue> &ArgValues) override;
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.
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.
128 void *getPointerToNamedFunction(const std::string &Name,
129 bool AbortOnFailure = true) override;
131 // CompilationCallback - Invoked the first time that a call site is found,
132 // which causes lazy compilation of the target function.
134 static void CompilationCallback();
136 /// getPointerToFunction - This returns the address of the specified function,
137 /// compiling it if necessary.
139 void *getPointerToFunction(Function *F) override;
141 /// addPointerToBasicBlock - Adds address of the specific basic block.
142 void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
144 /// clearPointerToBasicBlock - Removes address of specific basic block.
145 void clearPointerToBasicBlock(const BasicBlock *BB);
147 /// getPointerToBasicBlock - This returns the address of the specified basic
148 /// block, assuming function is compiled.
149 void *getPointerToBasicBlock(BasicBlock *BB) override;
151 /// getOrEmitGlobalVariable - Return the address of the specified global
152 /// variable, possibly emitting it to memory if needed. This is used by the
154 void *getOrEmitGlobalVariable(const GlobalVariable *GV) override;
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.
160 void *getPointerToFunctionOrStub(Function *F) override;
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().
168 void *recompileAndRelinkFunction(Function *F) override;
170 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
173 void freeMachineCodeForFunction(Function *F) override;
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.
179 void addPendingFunction(Function *F);
181 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
183 JITCodeEmitter *getCodeEmitter() const { return JCE; }
185 static ExecutionEngine *createJIT(Module *M,
186 std::string *ErrorStr,
187 JITMemoryManager *JMM,
191 // Run the JIT on F and return information about the generated code
192 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0) override;
194 void RegisterJITEventListener(JITEventListener *L) override;
195 void UnregisterJITEventListener(JITEventListener *L) override;
197 TargetMachine *getTargetMachine() override { return &TM; }
199 /// These functions correspond to the methods on JITEventListener. They
200 /// iterate over the registered listeners and call the corresponding method on
202 void NotifyFunctionEmitted(
203 const Function &F, void *Code, size_t Size,
204 const JITEvent_EmittedFunctionDetails &Details);
205 void NotifyFreeingMachineCode(void *OldPtr);
207 BasicBlockAddressMapTy &
208 getBasicBlockAddressMap(const MutexGuard &) {
209 return BasicBlockAddressMap;
214 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
216 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
217 void updateFunctionStub(Function *F);
218 void jitTheFunction(Function *F, const MutexGuard &locked);
222 /// getMemoryforGV - Allocate memory for a global variable.
223 char* getMemoryForGV(const GlobalVariable* GV) override;
227 } // End llvm namespace