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/PassManager.h"
19 #include "llvm/Support/ValueHandle.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 {
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;
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;
64 /// True while the JIT is generating code. Used to assert against recursive
66 bool isAlreadyCodeGenerating;
70 JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
71 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
72 bool AllocateGVsWithCode);
76 static void Register() {
80 /// getJITInfo - Return the target JIT information structure.
82 TargetJITInfo &getJITInfo() const { return TJI; }
84 /// create - Create an return a new JIT compiler if there is one available
85 /// for the current target. Otherwise, return null.
87 static ExecutionEngine *create(Module *M,
89 JITMemoryManager *JMM,
90 CodeGenOpt::Level OptLevel =
92 bool GVsWithCode = true,
93 CodeModel::Model CMM = CodeModel::Default) {
94 return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
98 virtual void addModule(Module *M);
100 /// removeModule - Remove a Module from the list of modules. Returns true if
102 virtual bool removeModule(Module *M);
104 /// runFunction - Start execution with the specified function and arguments.
106 virtual GenericValue runFunction(Function *F,
107 const std::vector<GenericValue> &ArgValues);
109 /// getPointerToNamedFunction - This method returns the address of the
110 /// specified function by using the dlsym function call. As such it is only
111 /// useful for resolving library symbols, not code generated symbols.
113 /// If AbortOnFailure is false and no function with the given name is
114 /// found, this function silently returns a null pointer. Otherwise,
115 /// it prints a message to stderr and aborts.
117 void *getPointerToNamedFunction(const std::string &Name,
118 bool AbortOnFailure = true);
120 // CompilationCallback - Invoked the first time that a call site is found,
121 // which causes lazy compilation of the target function.
123 static void CompilationCallback();
125 /// getPointerToFunction - This returns the address of the specified function,
126 /// compiling it if necessary.
128 void *getPointerToFunction(Function *F);
130 void *getPointerToBasicBlock(BasicBlock *BB) {
131 assert(0 && "JIT does not support address-of-label yet!");
135 /// getOrEmitGlobalVariable - Return the address of the specified global
136 /// variable, possibly emitting it to memory if needed. This is used by the
138 void *getOrEmitGlobalVariable(const GlobalVariable *GV);
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.
144 void *getPointerToFunctionOrStub(Function *F);
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().
152 void *recompileAndRelinkFunction(Function *F);
154 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
157 void freeMachineCodeForFunction(Function *F);
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.
163 void addPendingFunction(Function *F);
165 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
167 JITCodeEmitter *getCodeEmitter() const { return JCE; }
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(Module *M,
174 const SmallVectorImpl<std::string>& MAttrs,
177 static ExecutionEngine *createJIT(Module *M,
178 std::string *ErrorStr,
179 JITMemoryManager *JMM,
180 CodeGenOpt::Level OptLevel,
182 CodeModel::Model CMM,
185 const SmallVectorImpl<std::string>& MAttrs);
187 // Run the JIT on F and return information about the generated code
188 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
190 virtual void RegisterJITEventListener(JITEventListener *L);
191 virtual void UnregisterJITEventListener(JITEventListener *L);
192 /// These functions correspond to the methods on JITEventListener. They
193 /// iterate over the registered listeners and call the corresponding method on
195 void NotifyFunctionEmitted(
196 const Function &F, void *Code, size_t Size,
197 const JITEvent_EmittedFunctionDetails &Details);
198 void NotifyFreeingMachineCode(void *OldPtr);
201 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
203 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
204 void updateFunctionStub(Function *F);
208 /// getMemoryforGV - Allocate memory for a global variable.
209 virtual char* getMemoryForGV(const GlobalVariable* GV);
213 } // End llvm namespace