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 | } |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +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 { |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 54 | /// types |
| 55 | typedef ValueMap<const BasicBlock *, void *> |
| 56 | BasicBlockAddressMapTy; |
| 57 | /// data |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 58 | TargetMachine &TM; // The current target we are compiling to |
| 59 | TargetJITInfo &TJI; // The JITInfo for the target we are compiling to |
Bruno Cardoso Lopes | 434dd4f | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 60 | JITCodeEmitter *JCE; // JCE object |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 61 | JITMemoryManager *JMM; |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 62 | std::vector<JITEventListener*> EventListeners; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 63 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 64 | /// AllocateGVsWithCode - Some applications require that global variables and |
| 65 | /// code be allocated into the same region of memory, in which case this flag |
| 66 | /// should be set to true. Doing so breaks freeMachineCodeForFunction. |
| 67 | bool AllocateGVsWithCode; |
| 68 | |
Jeffrey Yasskin | 40966a7 | 2010-02-11 01:07:39 +0000 | [diff] [blame] | 69 | /// True while the JIT is generating code. Used to assert against recursive |
| 70 | /// entry. |
| 71 | bool isAlreadyCodeGenerating; |
| 72 | |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 73 | JITState *jitstate; |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 75 | /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their |
| 76 | /// actualized version, only filled for basic blocks that have their address |
| 77 | /// taken. |
| 78 | BasicBlockAddressMapTy BasicBlockAddressMap; |
| 79 | |
| 80 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 81 | JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, |
Dylan Noblesmith | 9ea4717 | 2011-12-12 04:20:36 +0000 | [diff] [blame] | 82 | JITMemoryManager *JMM, bool AllocateGVsWithCode); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 83 | public: |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 84 | ~JIT(); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 86 | static void Register() { |
Jeffrey Yasskin | 4688261 | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 87 | JITCtor = createJIT; |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 88 | } |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 890b4bd | 2004-11-20 03:11:07 +0000 | [diff] [blame] | 90 | /// getJITInfo - Return the target JIT information structure. |
| 91 | /// |
| 92 | TargetJITInfo &getJITInfo() const { return TJI; } |
| 93 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 94 | /// 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] | 95 | /// for the current target. Otherwise, return null. |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 96 | /// |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 97 | static ExecutionEngine *create(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 98 | std::string *Err, |
| 99 | JITMemoryManager *JMM, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 100 | CodeGenOpt::Level OptLevel = |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 101 | CodeGenOpt::Default, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 102 | bool GVsWithCode = true, |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 103 | Reloc::Model RM = Reloc::Default, |
Evan Cheng | 34ad6db | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 104 | CodeModel::Model CMM = CodeModel::JITDefault) { |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 105 | return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode, |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 106 | RM, CMM); |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 107 | } |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 108 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 109 | virtual void addModule(Module *M); |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 110 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 111 | /// removeModule - Remove a Module from the list of modules. Returns true if |
| 112 | /// M is found. |
| 113 | virtual bool removeModule(Module *M); |
Jeffrey Yasskin | aad0d52 | 2009-12-17 21:35:29 +0000 | [diff] [blame] | 114 | |
Dan Gohman | ce3c413 | 2008-07-03 00:51:05 +0000 | [diff] [blame] | 115 | /// runFunction - Start execution with the specified function and arguments. |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 116 | /// |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 117 | virtual GenericValue runFunction(Function *F, |
| 118 | const std::vector<GenericValue> &ArgValues); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 120 | /// getPointerToNamedFunction - This method returns the address of the |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 121 | /// specified function by using the MemoryManager. As such it is only |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 122 | /// useful for resolving library symbols, not code generated symbols. |
| 123 | /// |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 124 | /// If AbortOnFailure is false and no function with the given name is |
| 125 | /// found, this function silently returns a null pointer. Otherwise, |
| 126 | /// it prints a message to stderr and aborts. |
| 127 | /// |
Danil Malyshev | 45a93d6 | 2012-01-05 21:16:14 +0000 | [diff] [blame] | 128 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 129 | bool AbortOnFailure = true); |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 130 | |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 131 | // CompilationCallback - Invoked the first time that a call site is found, |
| 132 | // which causes lazy compilation of the target function. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 133 | // |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 134 | static void CompilationCallback(); |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 135 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 136 | /// getPointerToFunction - This returns the address of the specified function, |
| 137 | /// compiling it if necessary. |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 138 | /// |
Brian Gaeke | c227c1f | 2003-08-13 18:16:50 +0000 | [diff] [blame] | 139 | void *getPointerToFunction(Function *F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 141 | /// addPointerToBasicBlock - Adds address of the specific basic block. |
| 142 | void addPointerToBasicBlock(const BasicBlock *BB, void *Addr); |
| 143 | |
| 144 | /// clearPointerToBasicBlock - Removes address of specific basic block. |
| 145 | void clearPointerToBasicBlock(const BasicBlock *BB); |
| 146 | |
| 147 | /// getPointerToBasicBlock - This returns the address of the specified basic |
| 148 | /// block, assuming function is compiled. |
| 149 | void *getPointerToBasicBlock(BasicBlock *BB); |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 150 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 151 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 152 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 153 | /// Emitter. |
| 154 | void *getOrEmitGlobalVariable(const GlobalVariable *GV); |
| 155 | |
Chris Lattner | 993bdce | 2003-12-12 07:12:02 +0000 | [diff] [blame] | 156 | /// getPointerToFunctionOrStub - If the specified function has been |
| 157 | /// code-gen'd, return a pointer to the function. If not, compile it, or use |
| 158 | /// a stub to implement lazy compilation if available. |
| 159 | /// |
| 160 | void *getPointerToFunctionOrStub(Function *F); |
| 161 | |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 162 | /// recompileAndRelinkFunction - This method is used to force a function |
| 163 | /// which has already been compiled, to be compiled again, possibly |
| 164 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 165 | /// 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] | 166 | /// just like JIT::getPointerToFunction(). |
Brian Gaeke | 55c0f02 | 2003-10-17 18:27:12 +0000 | [diff] [blame] | 167 | /// |
| 168 | void *recompileAndRelinkFunction(Function *F); |
| 169 | |
Misha Brukman | 895eddf | 2004-11-07 23:58:46 +0000 | [diff] [blame] | 170 | /// freeMachineCodeForFunction - deallocate memory used to code-generate this |
| 171 | /// Function. |
| 172 | /// |
| 173 | void freeMachineCodeForFunction(Function *F); |
| 174 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 175 | /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd |
Jim Grosbach | 124d033 | 2011-03-16 01:21:55 +0000 | [diff] [blame] | 176 | /// function was encountered. Add it to a pending list to be processed after |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 177 | /// the current function. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 178 | /// |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 179 | void addPendingFunction(Function *F); |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 180 | |
Chris Lattner | e748401 | 2007-02-24 02:57:03 +0000 | [diff] [blame] | 181 | /// getCodeEmitter - Return the code emitter this JIT is emitting into. |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 182 | /// |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 183 | JITCodeEmitter *getCodeEmitter() const { return JCE; } |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 184 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 185 | static ExecutionEngine *createJIT(Module *M, |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 186 | std::string *ErrorStr, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 187 | JITMemoryManager *JMM, |
Eric Christopher | 88b5aca | 2009-11-17 21:58:16 +0000 | [diff] [blame] | 188 | bool GVsWithCode, |
Dylan Noblesmith | c5b2858 | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 189 | TargetMachine *TM); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 190 | |
| 191 | // Run the JIT on F and return information about the generated code |
| 192 | void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0); |
| 193 | |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 194 | virtual void RegisterJITEventListener(JITEventListener *L); |
| 195 | virtual void UnregisterJITEventListener(JITEventListener *L); |
| 196 | /// These functions correspond to the methods on JITEventListener. They |
| 197 | /// iterate over the registered listeners and call the corresponding method on |
| 198 | /// each. |
| 199 | void NotifyFunctionEmitted( |
| 200 | const Function &F, void *Code, size_t Size, |
| 201 | const JITEvent_EmittedFunctionDetails &Details); |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 202 | void NotifyFreeingMachineCode(void *OldPtr); |
Jeffrey Yasskin | df5a7da | 2009-06-25 02:04:04 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 204 | BasicBlockAddressMapTy & |
| 205 | getBasicBlockAddressMap(const MutexGuard &) { |
| 206 | return BasicBlockAddressMap; |
| 207 | } |
| 208 | |
| 209 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 210 | private: |
Reid Kleckner | 2763217 | 2009-09-20 23:52:43 +0000 | [diff] [blame] | 211 | static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM, |
| 212 | TargetMachine &tm); |
Dan Gohman | 21afcda | 2009-02-06 21:25:08 +0000 | [diff] [blame] | 213 | void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 214 | void updateFunctionStub(Function *F); |
Chris Lattner | 68feb22 | 2010-07-11 23:07:28 +0000 | [diff] [blame] | 215 | void jitTheFunction(Function *F, const MutexGuard &locked); |
Argyrios Kyrtzidis | b3a847d | 2009-05-18 21:06:40 +0000 | [diff] [blame] | 216 | |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 217 | protected: |
| 218 | |
| 219 | /// getMemoryforGV - Allocate memory for a global variable. |
| 220 | virtual char* getMemoryForGV(const GlobalVariable* GV); |
| 221 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 224 | } // End llvm namespace |
| 225 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 226 | #endif |