blob: b6f74ffad03192fd2389fcb38f448a76e67b32d5 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner836f6752002-12-24 00:01:22 +00009//
Chris Lattner4d326fa2003-12-20 01:46:27 +000010// This file defines the top-level JIT data structure.
Chris Lattner836f6752002-12-24 00:01:22 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner4d326fa2003-12-20 01:46:27 +000014#ifndef JIT_H
15#define JIT_H
Chris Lattner836f6752002-12-24 00:01:22 +000016
Brian Gaeke97222942003-09-05 19:39:22 +000017#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattner836f6752002-12-24 00:01:22 +000018#include "llvm/PassManager.h"
Jeffrey Yasskine5f87982009-10-13 21:32:57 +000019#include "llvm/Support/ValueHandle.h"
Chris Lattner836f6752002-12-24 00:01:22 +000020
Brian Gaeked0fde302003-11-11 22:41:34 +000021namespace llvm {
22
Chris Lattner836f6752002-12-24 00:01:22 +000023class Function;
Daniel Dunbardeb052a2009-07-12 23:50:34 +000024struct JITEvent_EmittedFunctionDetails;
Chris Lattner836f6752002-12-24 00:01:22 +000025class MachineCodeEmitter;
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +000026class MachineCodeInfo;
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +000027class TargetJITInfo;
28class TargetMachine;
Chris Lattner836f6752002-12-24 00:01:22 +000029
Reid Spenceree448632005-07-12 15:51:55 +000030class JITState {
31private:
Brian Gaekec227c1f2003-08-13 18:16:50 +000032 FunctionPassManager PM; // Passes to compile a function
Nate Begemand6b7a242009-02-18 08:31:02 +000033 ModuleProvider *MP; // ModuleProvider used to create the PM
Chris Lattner836f6752002-12-24 00:01:22 +000034
Nate Begemand6b7a242009-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 Yasskine5f87982009-10-13 21:32:57 +000037 std::vector<AssertingVH<Function> > PendingFunctions;
Chris Lattnerc07ed132003-12-20 03:36:47 +000038
Reid Spenceree448632005-07-12 15:51:55 +000039public:
Nate Begemand6b7a242009-02-18 08:31:02 +000040 explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
Reid Spenceree448632005-07-12 15:51:55 +000041
Chris Lattnerf7e968a2007-04-20 22:40:40 +000042 FunctionPassManager &getPM(const MutexGuard &L) {
Reid Spenceree448632005-07-12 15:51:55 +000043 return PM;
44 }
Nate Begemand6b7a242009-02-18 08:31:02 +000045
46 ModuleProvider *getMP() const { return MP; }
Jeffrey Yasskine5f87982009-10-13 21:32:57 +000047 std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
Nate Begemand6b7a242009-02-18 08:31:02 +000048 return PendingFunctions;
Reid Spenceree448632005-07-12 15:51:55 +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 Lopes434dd4f2009-06-01 19:57:37 +000056 JITCodeEmitter *JCE; // JCE object
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +000057 std::vector<JITEventListener*> EventListeners;
Reid Spenceree448632005-07-12 15:51:55 +000058
Jeffrey Yasskin489393d2009-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 Begemanf049e072008-05-21 16:34:48 +000064 JITState *jitstate;
Reid Spenceree448632005-07-12 15:51:55 +000065
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000066 JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
67 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68 bool AllocateGVsWithCode);
Chris Lattner836f6752002-12-24 00:01:22 +000069public:
Chris Lattner4d326fa2003-12-20 01:46:27 +000070 ~JIT();
Chris Lattner836f6752002-12-24 00:01:22 +000071
Chris Lattner2fe4bb02006-03-22 06:07:50 +000072 static void Register() {
73 JITCtor = create;
74 }
75
Chris Lattner890b4bd2004-11-20 03:11:07 +000076 /// getJITInfo - Return the target JIT information structure.
77 ///
78 TargetJITInfo &getJITInfo() const { return TJI; }
79
Brian Gaeke82d82772003-09-03 20:34:19 +000080 /// create - Create an return a new JIT compiler if there is one available
Chris Lattner726c1ef2006-03-23 05:22:51 +000081 /// for the current target. Otherwise, return null.
Brian Gaeke82d82772003-09-03 20:34:19 +000082 ///
Reid Kleckner4b1511b2009-07-18 00:42:18 +000083 static ExecutionEngine *create(ModuleProvider *MP,
84 std::string *Err,
85 JITMemoryManager *JMM,
Bill Wendling98a366d2009-04-29 23:29:43 +000086 CodeGenOpt::Level OptLevel =
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000087 CodeGenOpt::Default,
Eric Christopher88b5aca2009-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 Lattner34c94332007-12-06 01:34:04 +000092 }
Brian Gaeke82d82772003-09-03 20:34:19 +000093
Nate Begemanf049e072008-05-21 16:34:48 +000094 virtual void addModuleProvider(ModuleProvider *MP);
Nate Begeman60789e42009-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 Begemanf049e072008-05-21 16:34:48 +000099 virtual Module *removeModuleProvider(ModuleProvider *MP,
100 std::string *ErrInfo = 0);
101
Nate Begeman60789e42009-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
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000107 /// materializeFunction - make sure the given function is fully read. If the
108 /// module is corrupt, this returns true and fills in the optional string with
109 /// information about the problem. If successful, this returns false.
110 ///
111 bool materializeFunction(Function *F, std::string *ErrInfo = 0);
112
Dan Gohmance3c4132008-07-03 00:51:05 +0000113 /// runFunction - Start execution with the specified function and arguments.
Chris Lattner836f6752002-12-24 00:01:22 +0000114 ///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000115 virtual GenericValue runFunction(Function *F,
116 const std::vector<GenericValue> &ArgValues);
Chris Lattner836f6752002-12-24 00:01:22 +0000117
Chris Lattner0d448c02003-01-13 01:00:48 +0000118 /// getPointerToNamedFunction - This method returns the address of the
119 /// specified function by using the dlsym function call. As such it is only
120 /// useful for resolving library symbols, not code generated symbols.
121 ///
Dan Gohman69f93782009-01-05 05:32:42 +0000122 /// If AbortOnFailure is false and no function with the given name is
123 /// found, this function silently returns a null pointer. Otherwise,
124 /// it prints a message to stderr and aborts.
125 ///
126 void *getPointerToNamedFunction(const std::string &Name,
127 bool AbortOnFailure = true);
Chris Lattner0d448c02003-01-13 01:00:48 +0000128
Chris Lattnerc309a762003-05-08 21:34:11 +0000129 // CompilationCallback - Invoked the first time that a call site is found,
130 // which causes lazy compilation of the target function.
Misha Brukmanf976c852005-04-21 22:55:34 +0000131 //
Chris Lattnerc309a762003-05-08 21:34:11 +0000132 static void CompilationCallback();
Chris Lattner22080f92003-05-14 13:53:40 +0000133
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000134 /// getPointerToFunction - This returns the address of the specified function,
135 /// compiling it if necessary.
Brian Gaeke55c0f022003-10-17 18:27:12 +0000136 ///
Brian Gaekec227c1f2003-08-13 18:16:50 +0000137 void *getPointerToFunction(Function *F);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000138
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000139 void *getPointerToBasicBlock(BasicBlock *BB) {
140 assert(0 && "JIT does not support address-of-label yet!");
Benjamin Kramer0507d832009-10-29 12:55:32 +0000141 return 0;
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000142 }
143
Chris Lattnerc07ed132003-12-20 03:36:47 +0000144 /// getOrEmitGlobalVariable - Return the address of the specified global
145 /// variable, possibly emitting it to memory if needed. This is used by the
146 /// Emitter.
147 void *getOrEmitGlobalVariable(const GlobalVariable *GV);
148
Chris Lattner993bdce2003-12-12 07:12:02 +0000149 /// getPointerToFunctionOrStub - If the specified function has been
150 /// code-gen'd, return a pointer to the function. If not, compile it, or use
151 /// a stub to implement lazy compilation if available.
152 ///
153 void *getPointerToFunctionOrStub(Function *F);
154
Brian Gaeke55c0f022003-10-17 18:27:12 +0000155 /// recompileAndRelinkFunction - This method is used to force a function
156 /// which has already been compiled, to be compiled again, possibly
157 /// after it has been modified. Then the entry to the old copy is overwritten
158 /// with a branch to the new copy. If there was no old copy, this acts
Chris Lattner4d326fa2003-12-20 01:46:27 +0000159 /// just like JIT::getPointerToFunction().
Brian Gaeke55c0f022003-10-17 18:27:12 +0000160 ///
161 void *recompileAndRelinkFunction(Function *F);
162
Misha Brukman895eddf2004-11-07 23:58:46 +0000163 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
164 /// Function.
165 ///
166 void freeMachineCodeForFunction(Function *F);
167
Nate Begemand6b7a242009-02-18 08:31:02 +0000168 /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
169 /// function was encountered. Add it to a pending list to be processed after
170 /// the current function.
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000171 ///
Nate Begemand6b7a242009-02-18 08:31:02 +0000172 void addPendingFunction(Function *F);
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000173
Chris Lattnere7484012007-02-24 02:57:03 +0000174 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000175 ///
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000176 JITCodeEmitter *getCodeEmitter() const { return JCE; }
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000177
178 /// selectTarget - Pick a target either via -march or by guessing the native
179 /// arch. Add any CPU features specified via -mcpu or -mattr.
180 static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
181
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000182 static ExecutionEngine *createJIT(ModuleProvider *MP,
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000183 std::string *ErrorStr,
Bill Wendling98a366d2009-04-29 23:29:43 +0000184 JITMemoryManager *JMM,
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000185 CodeGenOpt::Level OptLevel,
Eric Christopher88b5aca2009-11-17 21:58:16 +0000186 bool GVsWithCode,
187 CodeModel::Model CMM);
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000188
189 // Run the JIT on F and return information about the generated code
190 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
191
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000192 virtual void RegisterJITEventListener(JITEventListener *L);
193 virtual void UnregisterJITEventListener(JITEventListener *L);
194 /// These functions correspond to the methods on JITEventListener. They
195 /// iterate over the registered listeners and call the corresponding method on
196 /// each.
197 void NotifyFunctionEmitted(
198 const Function &F, void *Code, size_t Size,
199 const JITEvent_EmittedFunctionDetails &Details);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000200 void NotifyFreeingMachineCode(void *OldPtr);
Jeffrey Yasskindf5a7da2009-06-25 02:04:04 +0000201
Chris Lattner836f6752002-12-24 00:01:22 +0000202private:
Reid Kleckner27632172009-09-20 23:52:43 +0000203 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
204 TargetMachine &tm);
Dan Gohman21afcda2009-02-06 21:25:08 +0000205 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
Nate Begemand6b7a242009-02-18 08:31:02 +0000206 void updateFunctionStub(Function *F);
Argyrios Kyrtzidisb3a847d2009-05-18 21:06:40 +0000207
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000208protected:
209
210 /// getMemoryforGV - Allocate memory for a global variable.
211 virtual char* getMemoryForGV(const GlobalVariable* GV);
212
Chris Lattner836f6752002-12-24 00:01:22 +0000213};
214
Brian Gaeked0fde302003-11-11 22:41:34 +0000215} // End llvm namespace
216
Chris Lattner836f6752002-12-24 00:01:22 +0000217#endif