blob: f165bd6659e9bcce09f283a5688402b5f5cd79de [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the top-level JIT data structure.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef JIT_H
15#define JIT_H
16
17#include "llvm/ExecutionEngine/ExecutionEngine.h"
18#include "llvm/PassManager.h"
Jeffrey Yasskinf9057462009-10-13 21:32:57 +000019#include "llvm/Support/ValueHandle.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020
21namespace llvm {
22
23class Function;
Daniel Dunbar238dcb72009-07-12 23:50:34 +000024struct JITEvent_EmittedFunctionDetails;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025class MachineCodeEmitter;
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +000026class MachineCodeInfo;
Jeffrey Yasskinf8d55342009-06-25 02:04:04 +000027class TargetJITInfo;
28class TargetMachine;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
30class JITState {
31private:
32 FunctionPassManager PM; // Passes to compile a function
Nate Begeman7b1a8472009-02-18 08:31:02 +000033 ModuleProvider *MP; // ModuleProvider used to create the PM
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034
Nate Begeman7b1a8472009-02-18 08:31:02 +000035 /// PendingFunctions - Functions which have not been code generated yet, but
36 /// were called from a function being code generated.
Jeffrey Yasskinf9057462009-10-13 21:32:57 +000037 std::vector<AssertingVH<Function> > PendingFunctions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
39public:
Nate Begeman7b1a8472009-02-18 08:31:02 +000040 explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
42 FunctionPassManager &getPM(const MutexGuard &L) {
43 return PM;
44 }
Nate Begeman7b1a8472009-02-18 08:31:02 +000045
46 ModuleProvider *getMP() const { return MP; }
Jeffrey Yasskinf9057462009-10-13 21:32:57 +000047 std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
Nate Begeman7b1a8472009-02-18 08:31:02 +000048 return PendingFunctions;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 }
50};
51
52
53class 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
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000056 JITCodeEmitter *JCE; // JCE object
Jeffrey Yasskinf8d55342009-06-25 02:04:04 +000057 std::vector<JITEventListener*> EventListeners;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000059 /// 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;
63
Nate Begemanf7113d92008-05-21 16:34:48 +000064 JITState *jitstate;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000066 JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
67 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68 bool AllocateGVsWithCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069public:
70 ~JIT();
71
72 static void Register() {
73 JITCtor = create;
74 }
75
76 /// getJITInfo - Return the target JIT information structure.
77 ///
78 TargetJITInfo &getJITInfo() const { return TJI; }
79
80 /// create - Create an return a new JIT compiler if there is one available
81 /// for the current target. Otherwise, return null.
82 ///
Reid Klecknerfa3585b2009-07-18 00:42:18 +000083 static ExecutionEngine *create(ModuleProvider *MP,
84 std::string *Err,
85 JITMemoryManager *JMM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000086 CodeGenOpt::Level OptLevel =
Jeffrey Yasskin892956a2009-07-08 21:59:57 +000087 CodeGenOpt::Default,
Eric Christopherb064d5e2009-11-17 21:58:16 +000088 bool GVsWithCode = true,
89 CodeModel::Model CMM = CodeModel::Default) {
90 return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode,
91 CMM);
Chris Lattner4db98aa2007-12-06 01:34:04 +000092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Nate Begemanf7113d92008-05-21 16:34:48 +000094 virtual void addModuleProvider(ModuleProvider *MP);
Nate Begemanb34045e2009-01-23 19:27:28 +000095
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.
Nate Begemanf7113d92008-05-21 16:34:48 +000099 virtual Module *removeModuleProvider(ModuleProvider *MP,
100 std::string *ErrInfo = 0);
101
Nate Begemanb34045e2009-01-23 19:27:28 +0000102 /// 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);
106
Dan Gohmana7440122008-07-03 00:51:05 +0000107 /// runFunction - Start execution with the specified function and arguments.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 ///
109 virtual GenericValue runFunction(Function *F,
110 const std::vector<GenericValue> &ArgValues);
111
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.
115 ///
Dan Gohman0ae39622009-01-05 05:32:42 +0000116 /// 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.
119 ///
120 void *getPointerToNamedFunction(const std::string &Name,
121 bool AbortOnFailure = true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 // CompilationCallback - Invoked the first time that a call site is found,
124 // which causes lazy compilation of the target function.
125 //
126 static void CompilationCallback();
127
128 /// getPointerToFunction - This returns the address of the specified function,
129 /// compiling it if necessary.
130 ///
131 void *getPointerToFunction(Function *F);
132
Chris Lattnerbfa6b852009-10-29 05:26:09 +0000133 void *getPointerToBasicBlock(BasicBlock *BB) {
134 assert(0 && "JIT does not support address-of-label yet!");
Benjamin Kramerdbd28a32009-10-29 12:55:32 +0000135 return 0;
Chris Lattnerbfa6b852009-10-29 05:26:09 +0000136 }
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 /// getOrEmitGlobalVariable - Return the address of the specified global
139 /// variable, possibly emitting it to memory if needed. This is used by the
140 /// Emitter.
141 void *getOrEmitGlobalVariable(const GlobalVariable *GV);
142
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.
146 ///
147 void *getPointerToFunctionOrStub(Function *F);
148
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().
154 ///
155 void *recompileAndRelinkFunction(Function *F);
156
157 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
158 /// Function.
159 ///
160 void freeMachineCodeForFunction(Function *F);
161
Nate Begeman7b1a8472009-02-18 08:31:02 +0000162 /// 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.
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000165 ///
Nate Begeman7b1a8472009-02-18 08:31:02 +0000166 void addPendingFunction(Function *F);
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000167
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000169 ///
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000170 JITCodeEmitter *getCodeEmitter() const { return JCE; }
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000171
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);
175
Jeffrey Yasskin892956a2009-07-08 21:59:57 +0000176 static ExecutionEngine *createJIT(ModuleProvider *MP,
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000177 std::string *ErrorStr,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000178 JITMemoryManager *JMM,
Jeffrey Yasskin892956a2009-07-08 21:59:57 +0000179 CodeGenOpt::Level OptLevel,
Eric Christopherb064d5e2009-11-17 21:58:16 +0000180 bool GVsWithCode,
181 CodeModel::Model CMM);
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +0000182
183 // Run the JIT on F and return information about the generated code
184 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
185
Jeffrey Yasskinf8d55342009-06-25 02:04:04 +0000186 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
190 /// each.
191 void NotifyFunctionEmitted(
192 const Function &F, void *Code, size_t Size,
193 const JITEvent_EmittedFunctionDetails &Details);
Jeffrey Yasskin1291fce62009-10-27 00:03:05 +0000194 void NotifyFreeingMachineCode(void *OldPtr);
Jeffrey Yasskinf8d55342009-06-25 02:04:04 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196private:
Reid Kleckner738b4f22009-09-20 23:52:43 +0000197 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
198 TargetMachine &tm);
Dan Gohman2970af12009-02-06 21:25:08 +0000199 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
Nate Begeman7b1a8472009-02-18 08:31:02 +0000200 void updateFunctionStub(Function *F);
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +0000201
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000202protected:
203
204 /// getMemoryforGV - Allocate memory for a global variable.
205 virtual char* getMemoryForGV(const GlobalVariable* GV);
206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207};
208
209} // End llvm namespace
210
211#endif