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 ModuleProvider *MP; // ModuleProvider 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(ModuleProvider *MP) : PM(MP), MP(MP) {}
42 FunctionPassManager &getPM(const MutexGuard &L) {
46 ModuleProvider *getMP() const { return MP; }
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;
66 JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
67 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68 bool AllocateGVsWithCode);
72 static void Register() {
76 /// getJITInfo - Return the target JIT information structure.
78 TargetJITInfo &getJITInfo() const { return TJI; }
80 /// create - Create an return a new JIT compiler if there is one available
81 /// for the current target. Otherwise, return null.
83 static ExecutionEngine *create(ModuleProvider *MP,
85 JITMemoryManager *JMM,
86 CodeGenOpt::Level OptLevel =
88 bool GVsWithCode = true,
89 CodeModel::Model CMM = CodeModel::Default) {
90 return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode,
94 virtual void addModuleProvider(ModuleProvider *MP);
96 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
97 /// Relases the Module from the ModuleProvider, materializing it in the
98 /// process, and returns the materialized Module.
99 virtual Module *removeModuleProvider(ModuleProvider *MP,
100 std::string *ErrInfo = 0);
102 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
103 /// and deletes the ModuleProvider and owned Module. Avoids materializing
104 /// the underlying module.
105 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
107 /// runFunction - Start execution with the specified function and arguments.
109 virtual GenericValue runFunction(Function *F,
110 const std::vector<GenericValue> &ArgValues);
112 /// getPointerToNamedFunction - This method returns the address of the
113 /// specified function by using the dlsym function call. As such it is only
114 /// useful for resolving library symbols, not code generated symbols.
116 /// If AbortOnFailure is false and no function with the given name is
117 /// found, this function silently returns a null pointer. Otherwise,
118 /// it prints a message to stderr and aborts.
120 void *getPointerToNamedFunction(const std::string &Name,
121 bool AbortOnFailure = true);
123 // CompilationCallback - Invoked the first time that a call site is found,
124 // which causes lazy compilation of the target function.
126 static void CompilationCallback();
128 /// getPointerToFunction - This returns the address of the specified function,
129 /// compiling it if necessary.
131 void *getPointerToFunction(Function *F);
133 void *getPointerToBasicBlock(BasicBlock *BB) {
134 assert(0 && "JIT does not support address-of-label yet!");
138 /// getOrEmitGlobalVariable - Return the address of the specified global
139 /// variable, possibly emitting it to memory if needed. This is used by the
141 void *getOrEmitGlobalVariable(const GlobalVariable *GV);
143 /// getPointerToFunctionOrStub - If the specified function has been
144 /// code-gen'd, return a pointer to the function. If not, compile it, or use
145 /// a stub to implement lazy compilation if available.
147 void *getPointerToFunctionOrStub(Function *F);
149 /// recompileAndRelinkFunction - This method is used to force a function
150 /// which has already been compiled, to be compiled again, possibly
151 /// after it has been modified. Then the entry to the old copy is overwritten
152 /// with a branch to the new copy. If there was no old copy, this acts
153 /// just like JIT::getPointerToFunction().
155 void *recompileAndRelinkFunction(Function *F);
157 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
160 void freeMachineCodeForFunction(Function *F);
162 /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
163 /// function was encountered. Add it to a pending list to be processed after
164 /// the current function.
166 void addPendingFunction(Function *F);
168 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
170 JITCodeEmitter *getCodeEmitter() const { return JCE; }
172 /// selectTarget - Pick a target either via -march or by guessing the native
173 /// arch. Add any CPU features specified via -mcpu or -mattr.
174 static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
176 static ExecutionEngine *createJIT(ModuleProvider *MP,
177 std::string *ErrorStr,
178 JITMemoryManager *JMM,
179 CodeGenOpt::Level OptLevel,
181 CodeModel::Model CMM);
183 // Run the JIT on F and return information about the generated code
184 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
186 virtual void RegisterJITEventListener(JITEventListener *L);
187 virtual void UnregisterJITEventListener(JITEventListener *L);
188 /// These functions correspond to the methods on JITEventListener. They
189 /// iterate over the registered listeners and call the corresponding method on
191 void NotifyFunctionEmitted(
192 const Function &F, void *Code, size_t Size,
193 const JITEvent_EmittedFunctionDetails &Details);
194 void NotifyFreeingMachineCode(void *OldPtr);
197 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
199 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
200 void updateFunctionStub(Function *F);
204 /// getMemoryforGV - Allocate memory for a global variable.
205 virtual char* getMemoryForGV(const GlobalVariable* GV);
209 } // End llvm namespace