Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1 | //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | 856ba76 | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 10 | // This file defines the top-level JIT data structure. |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 14 | #ifndef JIT_H |
| 15 | #define JIT_H |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 16 | |
Brian Gaeke | 9722294 | 2003-09-05 19:39:22 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 23 | class Function; |
Daniel Dunbar | deb052a | 2009-07-12 23:50:34 +0000 | [diff] [blame] | 24 | struct JITEvent_EmittedFunctionDetails; |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 25 | class MachineCodeEmitter; |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 26 | class MachineCodeInfo; |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 27 | class TargetJITInfo; |
| 28 | class TargetMachine; |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 29 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 30 | class JITState { |
| 31 | private: |
Brian Gaeke | c227c1f | 2003-08-13 18:16:50 +0000 | [diff] [blame] | 32 | FunctionPassManager PM; // Passes to compile a function |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 33 | Module *M; // Module used to create the PM |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 34 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 35 | /// PendingFunctions - Functions which have not been code generated yet, but |
| 36 | /// were called from a function being code generated. |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 37 | std::vector<AssertingVH<Function> > PendingFunctions; |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 38 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 39 | public: |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 40 | explicit JITState(Module *M) : PM(M), M(M) {} |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f7e968a | 2007-04-20 22:40:40 +0000 | [diff] [blame] | 42 | FunctionPassManager &getPM(const MutexGuard &L) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 43 | return PM; |
| 44 | } |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 45 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 46 | Module *getModule() const { return M; } |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 47 | std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){ |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 48 | return PendingFunctions; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
| 52 | |
| 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 |
Bruno Cardoso Lopes | 434dd4f | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 56 | JITCodeEmitter *JCE; // JCE object |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 57 | std::vector<JITEventListener*> EventListeners; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 58 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 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; |
| 63 | |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 64 | JITState *jitstate; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 65 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 66 | JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 67 | JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, |
| 68 | bool AllocateGVsWithCode); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 69 | public: |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 70 | ~JIT(); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 72 | static void Register() { |
| 73 | JITCtor = create; |
| 74 | } |
| 75 | |
Chris Lattner | 890b4bd | 2004-11-20 03:11:07 +0000 | [diff] [blame] | 76 | /// getJITInfo - Return the target JIT information structure. |
| 77 | /// |
| 78 | TargetJITInfo &getJITInfo() const { return TJI; } |
| 79 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 80 | /// create - Create an return a new JIT compiler if there is one available |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 81 | /// for the current target. Otherwise, return null. |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 82 | /// |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 83 | static ExecutionEngine *create(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 84 | std::string *Err, |
| 85 | JITMemoryManager *JMM, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 86 | CodeGenOpt::Level OptLevel = |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 87 | CodeGenOpt::Default, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 88 | bool GVsWithCode = true, |
| 89 | CodeModel::Model CMM = CodeModel::Default) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 90 | return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 91 | CMM); |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 92 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 93 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 94 | virtual void addModule(Module *M); |
Nate Begeman | 60789e4 | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 95 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 96 | /// removeModule - Remove a Module from the list of modules. Returns true if |
| 97 | /// M is found. |
| 98 | virtual bool removeModule(Module *M); |
Jeffrey Yasskin | aad0d52 | 2009-12-17 21:35:29 +0000 | [diff] [blame] | 99 | |
Dan Gohman | ce3c413 | 2008-07-03 00:51:05 +0000 | [diff] [blame] | 100 | /// runFunction - Start execution with the specified function and arguments. |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 101 | /// |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 102 | virtual GenericValue runFunction(Function *F, |
| 103 | const std::vector<GenericValue> &ArgValues); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 105 | /// getPointerToNamedFunction - This method returns the address of the |
| 106 | /// specified function by using the dlsym function call. As such it is only |
| 107 | /// useful for resolving library symbols, not code generated symbols. |
| 108 | /// |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 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. |
| 112 | /// |
| 113 | void *getPointerToNamedFunction(const std::string &Name, |
| 114 | bool AbortOnFailure = true); |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 115 | |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 116 | // CompilationCallback - Invoked the first time that a call site is found, |
| 117 | // which causes lazy compilation of the target function. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 118 | // |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 119 | static void CompilationCallback(); |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 120 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 121 | /// getPointerToFunction - This returns the address of the specified function, |
| 122 | /// compiling it if necessary. |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 123 | /// |
Brian Gaeke | c227c1f | 2003-08-13 18:16:50 +0000 | [diff] [blame] | 124 | void *getPointerToFunction(Function *F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 125 | |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 126 | void *getPointerToBasicBlock(BasicBlock *BB) { |
| 127 | assert(0 && "JIT does not support address-of-label yet!"); |
Benjamin Kramer | 0507d83 | 2009-10-29 12:55:32 +0000 | [diff] [blame] | 128 | return 0; |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 131 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 132 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 133 | /// Emitter. |
| 134 | void *getOrEmitGlobalVariable(const GlobalVariable *GV); |
| 135 | |
Chris Lattner | 993bdce | 2003-12-12 07:12:02 +0000 | [diff] [blame] | 136 | /// getPointerToFunctionOrStub - If the specified function has been |
| 137 | /// code-gen'd, return a pointer to the function. If not, compile it, or use |
| 138 | /// a stub to implement lazy compilation if available. |
| 139 | /// |
| 140 | void *getPointerToFunctionOrStub(Function *F); |
| 141 | |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 142 | /// recompileAndRelinkFunction - This method is used to force a function |
| 143 | /// which has already been compiled, to be compiled again, possibly |
| 144 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 145 | /// with a branch to the new copy. If there was no old copy, this acts |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 146 | /// just like JIT::getPointerToFunction(). |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 147 | /// |
| 148 | void *recompileAndRelinkFunction(Function *F); |
| 149 | |
Misha Brukman | 895eddf | 2004-11-07 23:58:46 +0000 | [diff] [blame] | 150 | /// freeMachineCodeForFunction - deallocate memory used to code-generate this |
| 151 | /// Function. |
| 152 | /// |
| 153 | void freeMachineCodeForFunction(Function *F); |
| 154 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 155 | /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd |
| 156 | /// function was encountered. Add it to a pending list to be processed after |
| 157 | /// the current function. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 158 | /// |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 159 | void addPendingFunction(Function *F); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 160 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 161 | /// getCodeEmitter - Return the code emitter this JIT is emitting into. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 162 | /// |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 163 | JITCodeEmitter *getCodeEmitter() const { return JCE; } |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 164 | |
| 165 | /// selectTarget - Pick a target either via -march or by guessing the native |
| 166 | /// arch. Add any CPU features specified via -mcpu or -mattr. |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 167 | static TargetMachine *selectTarget(Module *M, std::string *Err); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 168 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 169 | static ExecutionEngine *createJIT(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 170 | std::string *ErrorStr, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 171 | JITMemoryManager *JMM, |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 172 | CodeGenOpt::Level OptLevel, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 173 | bool GVsWithCode, |
| 174 | CodeModel::Model CMM); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 175 | |
| 176 | // Run the JIT on F and return information about the generated code |
| 177 | void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0); |
| 178 | |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 179 | virtual void RegisterJITEventListener(JITEventListener *L); |
| 180 | virtual void UnregisterJITEventListener(JITEventListener *L); |
| 181 | /// These functions correspond to the methods on JITEventListener. They |
| 182 | /// iterate over the registered listeners and call the corresponding method on |
| 183 | /// each. |
| 184 | void NotifyFunctionEmitted( |
| 185 | const Function &F, void *Code, size_t Size, |
| 186 | const JITEvent_EmittedFunctionDetails &Details); |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 187 | void NotifyFreeingMachineCode(void *OldPtr); |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 189 | private: |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 190 | static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM, |
| 191 | TargetMachine &tm); |
Dan Gohman | 21afcda | 2009-02-06 21:25:08 +0000 | [diff] [blame] | 192 | void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 193 | void updateFunctionStub(Function *F); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 194 | |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 195 | protected: |
| 196 | |
| 197 | /// getMemoryforGV - Allocate memory for a global variable. |
| 198 | virtual char* getMemoryForGV(const GlobalVariable* GV); |
| 199 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 202 | } // End llvm namespace |
| 203 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 204 | #endif |