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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_JIT_JIT_H
15 #define LLVM_LIB_EXECUTIONENGINE_JIT_JIT_H
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() {
46 Module *getModule() const { return M; }
47 std::vector<AssertingVH<Function> > &getPendingFunctions() {
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 void addModule(Module *M) override;
96 /// removeModule - Remove a Module from the list of modules. Returns true if
98 bool removeModule(Module *M) override;
100 /// runFunction - Start execution with the specified function and arguments.
102 GenericValue runFunction(Function *F,
103 const std::vector<GenericValue> &ArgValues) override;
105 /// getPointerToNamedFunction - This method returns the address of the
106 /// specified function by using the MemoryManager. As such it is only
107 /// useful for resolving library symbols, not code generated symbols.
109 /// If AbortOnFailure is false and no function with the given name is
110 /// found, this function silently returns a null pointer. Otherwise,
111 /// it prints a message to stderr and aborts.
113 void *getPointerToNamedFunction(const std::string &Name,
114 bool AbortOnFailure = true) override;
116 // CompilationCallback - Invoked the first time that a call site is found,
117 // which causes lazy compilation of the target function.
119 static void CompilationCallback();
121 /// getPointerToFunction - This returns the address of the specified function,
122 /// compiling it if necessary.
124 void *getPointerToFunction(Function *F) override;
126 /// addPointerToBasicBlock - Adds address of the specific basic block.
127 void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
129 /// clearPointerToBasicBlock - Removes address of specific basic block.
130 void clearPointerToBasicBlock(const BasicBlock *BB);
132 /// getPointerToBasicBlock - This returns the address of the specified basic
133 /// block, assuming function is compiled.
134 void *getPointerToBasicBlock(BasicBlock *BB) override;
136 /// getOrEmitGlobalVariable - Return the address of the specified global
137 /// variable, possibly emitting it to memory if needed. This is used by the
139 void *getOrEmitGlobalVariable(const GlobalVariable *GV) override;
141 /// getPointerToFunctionOrStub - If the specified function has been
142 /// code-gen'd, return a pointer to the function. If not, compile it, or use
143 /// a stub to implement lazy compilation if available.
145 void *getPointerToFunctionOrStub(Function *F) override;
147 /// recompileAndRelinkFunction - This method is used to force a function
148 /// which has already been compiled, to be compiled again, possibly
149 /// after it has been modified. Then the entry to the old copy is overwritten
150 /// with a branch to the new copy. If there was no old copy, this acts
151 /// just like JIT::getPointerToFunction().
153 void *recompileAndRelinkFunction(Function *F) override;
155 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
158 void freeMachineCodeForFunction(Function *F) override;
160 /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
161 /// function was encountered. Add it to a pending list to be processed after
162 /// the current function.
164 void addPendingFunction(Function *F);
166 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
168 JITCodeEmitter *getCodeEmitter() const { return JCE; }
170 static ExecutionEngine *createJIT(Module *M,
171 std::string *ErrorStr,
172 JITMemoryManager *JMM,
176 // Run the JIT on F and return information about the generated code
177 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = nullptr) override;
179 void RegisterJITEventListener(JITEventListener *L) override;
180 void UnregisterJITEventListener(JITEventListener *L) override;
182 TargetMachine *getTargetMachine() override { return &TM; }
184 /// These functions correspond to the methods on JITEventListener. They
185 /// iterate over the registered listeners and call the corresponding method on
187 void NotifyFunctionEmitted(
188 const Function &F, void *Code, size_t Size,
189 const JITEvent_EmittedFunctionDetails &Details);
190 void NotifyFreeingMachineCode(void *OldPtr);
192 BasicBlockAddressMapTy &
193 getBasicBlockAddressMap() {
194 return BasicBlockAddressMap;
199 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
201 void runJITOnFunctionUnlocked(Function *F);
202 void updateFunctionStubUnlocked(Function *F);
203 void jitTheFunctionUnlocked(Function *F);
207 /// getMemoryforGV - Allocate memory for a global variable.
208 char* getMemoryForGV(const GlobalVariable* GV) override;
212 } // End llvm namespace