blob: d2bd5089cb29396188c692af60f7e9c285df2607 [file] [log] [blame]
Chris Lattner9bcae072003-12-20 01:46:27 +00001//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattnere6761db2002-12-24 00:01:22 +00009//
Chris Lattner9bcae072003-12-20 01:46:27 +000010// This file defines the top-level JIT data structure.
Chris Lattnere6761db2002-12-24 00:01:22 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner9bcae072003-12-20 01:46:27 +000014#ifndef JIT_H
15#define JIT_H
Chris Lattnere6761db2002-12-24 00:01:22 +000016
Brian Gaekef3a300d2003-09-05 19:39:22 +000017#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000018#include "llvm/IR/ValueHandle.h"
Chris Lattnere6761db2002-12-24 00:01:22 +000019#include "llvm/PassManager.h"
Chris Lattnere6761db2002-12-24 00:01:22 +000020
Brian Gaeke960707c2003-11-11 22:41:34 +000021namespace llvm {
22
Chris Lattnere6761db2002-12-24 00:01:22 +000023class Function;
Daniel Dunbar65553d42009-07-12 23:50:34 +000024struct JITEvent_EmittedFunctionDetails;
Chris Lattnere6761db2002-12-24 00:01:22 +000025class MachineCodeEmitter;
Argyrios Kyrtzidisc65c5252009-05-18 21:06:40 +000026class MachineCodeInfo;
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +000027class TargetJITInfo;
28class TargetMachine;
Chris Lattnere6761db2002-12-24 00:01:22 +000029
Reid Spencer79876f52005-07-12 15:51:55 +000030class JITState {
31private:
Brian Gaeke2b804a22003-08-13 18:16:50 +000032 FunctionPassManager PM; // Passes to compile a function
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000033 Module *M; // Module used to create the PM
Chris Lattnere6761db2002-12-24 00:01:22 +000034
Nate Begeman18d85e72009-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 Yasskind162dba2009-10-13 21:32:57 +000037 std::vector<AssertingVH<Function> > PendingFunctions;
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +000038
Reid Spencer79876f52005-07-12 15:51:55 +000039public:
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000040 explicit JITState(Module *M) : PM(M), M(M) {}
Reid Spencer79876f52005-07-12 15:51:55 +000041
Chris Lattner1e999c42007-04-20 22:40:40 +000042 FunctionPassManager &getPM(const MutexGuard &L) {
Reid Spencer79876f52005-07-12 15:51:55 +000043 return PM;
44 }
Jim Grosbachc91fa6d2011-03-16 01:21:55 +000045
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000046 Module *getModule() const { return M; }
Jeffrey Yasskind162dba2009-10-13 21:32:57 +000047 std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
Nate Begeman18d85e72009-02-18 08:31:02 +000048 return PendingFunctions;
Reid Spencer79876f52005-07-12 15:51:55 +000049 }
50};
51
52
53class JIT : public ExecutionEngine {
Chris Lattnerb6df00c2010-07-11 23:07:28 +000054 /// types
55 typedef ValueMap<const BasicBlock *, void *>
56 BasicBlockAddressMapTy;
57 /// data
Reid Spencer79876f52005-07-12 15:51:55 +000058 TargetMachine &TM; // The current target we are compiling to
59 TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000060 JITCodeEmitter *JCE; // JCE object
Danil Malyshevbfee5422012-03-28 21:46:36 +000061 JITMemoryManager *JMM;
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +000062 std::vector<JITEventListener*> EventListeners;
Reid Spencer79876f52005-07-12 15:51:55 +000063
Jeffrey Yasskin70415d92009-07-08 21:59:57 +000064 /// 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 Yasskine0913882010-02-11 01:07:39 +000069 /// True while the JIT is generating code. Used to assert against recursive
70 /// entry.
71 bool isAlreadyCodeGenerating;
72
Nate Begeman8f83fc42008-05-21 16:34:48 +000073 JITState *jitstate;
Reid Spencer79876f52005-07-12 15:51:55 +000074
Chris Lattnerb6df00c2010-07-11 23:07:28 +000075 /// 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 Yasskin091217b2010-01-27 20:34:15 +000081 JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
Dylan Noblesmith7f262462011-12-12 04:20:36 +000082 JITMemoryManager *JMM, bool AllocateGVsWithCode);
Chris Lattnere6761db2002-12-24 00:01:22 +000083public:
Chris Lattner9bcae072003-12-20 01:46:27 +000084 ~JIT();
Chris Lattnere6761db2002-12-24 00:01:22 +000085
Chris Lattner2d52c1b2006-03-22 06:07:50 +000086 static void Register() {
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000087 JITCtor = createJIT;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000088 }
Jim Grosbachc91fa6d2011-03-16 01:21:55 +000089
Chris Lattnerb7b78502004-11-20 03:11:07 +000090 /// getJITInfo - Return the target JIT information structure.
91 ///
92 TargetJITInfo &getJITInfo() const { return TJI; }
93
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000094 /// create - Create an return a new JIT compiler if there is one available
Chris Lattner0b2de9f2006-03-23 05:22:51 +000095 /// for the current target. Otherwise, return null.
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000096 ///
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000097 static ExecutionEngine *create(Module *M,
Reid Klecknerfc8a2d52009-07-18 00:42:18 +000098 std::string *Err,
99 JITMemoryManager *JMM,
Bill Wendling026e5d72009-04-29 23:29:43 +0000100 CodeGenOpt::Level OptLevel =
Jeffrey Yasskin70415d92009-07-08 21:59:57 +0000101 CodeGenOpt::Default,
Eric Christopher700d08e2009-11-17 21:58:16 +0000102 bool GVsWithCode = true,
Evan Cheng2129f592011-07-19 06:37:02 +0000103 Reloc::Model RM = Reloc::Default,
Evan Chengefd9b422011-07-20 07:51:56 +0000104 CodeModel::Model CMM = CodeModel::JITDefault) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000105 return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
Evan Cheng2129f592011-07-19 06:37:02 +0000106 RM, CMM);
Chris Lattner7f3587e2007-12-06 01:34:04 +0000107 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000108
Craig Topperb51ff602014-03-08 07:51:20 +0000109 void addModule(Module *M) override;
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000110
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000111 /// removeModule - Remove a Module from the list of modules. Returns true if
112 /// M is found.
Craig Topperb51ff602014-03-08 07:51:20 +0000113 bool removeModule(Module *M) override;
Jeffrey Yasskin2b73a4e2009-12-17 21:35:29 +0000114
Dan Gohman4c6db532008-07-03 00:51:05 +0000115 /// runFunction - Start execution with the specified function and arguments.
Chris Lattnere6761db2002-12-24 00:01:22 +0000116 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000117 GenericValue runFunction(Function *F,
118 const std::vector<GenericValue> &ArgValues) override;
Chris Lattnere6761db2002-12-24 00:01:22 +0000119
Chris Lattner4bc4b672003-01-13 01:00:48 +0000120 /// getPointerToNamedFunction - This method returns the address of the
Danil Malyshevbfee5422012-03-28 21:46:36 +0000121 /// specified function by using the MemoryManager. As such it is only
Chris Lattner4bc4b672003-01-13 01:00:48 +0000122 /// useful for resolving library symbols, not code generated symbols.
123 ///
Dan Gohmand32ec012009-01-05 05:32:42 +0000124 /// 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 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000128 void *getPointerToNamedFunction(const std::string &Name,
129 bool AbortOnFailure = true) override;
Chris Lattner4bc4b672003-01-13 01:00:48 +0000130
Chris Lattnerd340dbd2003-05-08 21:34:11 +0000131 // CompilationCallback - Invoked the first time that a call site is found,
132 // which causes lazy compilation of the target function.
Misha Brukman10468d82005-04-21 22:55:34 +0000133 //
Chris Lattnerd340dbd2003-05-08 21:34:11 +0000134 static void CompilationCallback();
Chris Lattner2537ca32003-05-14 13:53:40 +0000135
Chris Lattner6b689e32003-06-01 23:24:36 +0000136 /// getPointerToFunction - This returns the address of the specified function,
137 /// compiling it if necessary.
Brian Gaeked2bc5ac2003-10-17 18:27:12 +0000138 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000139 void *getPointerToFunction(Function *F) override;
Chris Lattner6b689e32003-06-01 23:24:36 +0000140
Chris Lattnerb6df00c2010-07-11 23:07:28 +0000141 /// 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.
Craig Topperb51ff602014-03-08 07:51:20 +0000149 void *getPointerToBasicBlock(BasicBlock *BB) override;
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000150
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +0000151 /// getOrEmitGlobalVariable - Return the address of the specified global
152 /// variable, possibly emitting it to memory if needed. This is used by the
153 /// Emitter.
Craig Topperb51ff602014-03-08 07:51:20 +0000154 void *getOrEmitGlobalVariable(const GlobalVariable *GV) override;
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +0000155
Chris Lattner89af2d52003-12-12 07:12:02 +0000156 /// 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 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000160 void *getPointerToFunctionOrStub(Function *F) override;
Chris Lattner89af2d52003-12-12 07:12:02 +0000161
Brian Gaeked2bc5ac2003-10-17 18:27:12 +0000162 /// 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 Lattner9bcae072003-12-20 01:46:27 +0000166 /// just like JIT::getPointerToFunction().
Brian Gaeked2bc5ac2003-10-17 18:27:12 +0000167 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000168 void *recompileAndRelinkFunction(Function *F) override;
Brian Gaeked2bc5ac2003-10-17 18:27:12 +0000169
Misha Brukman624685d2004-11-07 23:58:46 +0000170 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
171 /// Function.
172 ///
Craig Topperb51ff602014-03-08 07:51:20 +0000173 void freeMachineCodeForFunction(Function *F) override;
Misha Brukman624685d2004-11-07 23:58:46 +0000174
Nate Begeman18d85e72009-02-18 08:31:02 +0000175 /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
Jim Grosbachc91fa6d2011-03-16 01:21:55 +0000176 /// function was encountered. Add it to a pending list to be processed after
Nate Begeman18d85e72009-02-18 08:31:02 +0000177 /// the current function.
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000178 ///
Nate Begeman18d85e72009-02-18 08:31:02 +0000179 void addPendingFunction(Function *F);
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000180
Chris Lattner05858a92007-02-24 02:57:03 +0000181 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000182 ///
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000183 JITCodeEmitter *getCodeEmitter() const { return JCE; }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000184
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000185 static ExecutionEngine *createJIT(Module *M,
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000186 std::string *ErrorStr,
Bill Wendling026e5d72009-04-29 23:29:43 +0000187 JITMemoryManager *JMM,
Eric Christopher700d08e2009-11-17 21:58:16 +0000188 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000189 TargetMachine *TM);
Argyrios Kyrtzidisc65c5252009-05-18 21:06:40 +0000190
191 // Run the JIT on F and return information about the generated code
Craig Toppere73658d2014-04-28 04:05:08 +0000192 void runJITOnFunction(Function *F, MachineCodeInfo *MCI = nullptr) override;
Argyrios Kyrtzidisc65c5252009-05-18 21:06:40 +0000193
Craig Topperb51ff602014-03-08 07:51:20 +0000194 void RegisterJITEventListener(JITEventListener *L) override;
195 void UnregisterJITEventListener(JITEventListener *L) override;
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000196
Craig Topperb51ff602014-03-08 07:51:20 +0000197 TargetMachine *getTargetMachine() override { return &TM; }
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000198
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +0000199 /// These functions correspond to the methods on JITEventListener. They
200 /// iterate over the registered listeners and call the corresponding method on
201 /// each.
202 void NotifyFunctionEmitted(
203 const Function &F, void *Code, size_t Size,
204 const JITEvent_EmittedFunctionDetails &Details);
Jeffrey Yasskinbf43f652009-10-27 00:03:05 +0000205 void NotifyFreeingMachineCode(void *OldPtr);
Jeffrey Yasskin0b08f3d2009-06-25 02:04:04 +0000206
Chris Lattnerb6df00c2010-07-11 23:07:28 +0000207 BasicBlockAddressMapTy &
208 getBasicBlockAddressMap(const MutexGuard &) {
209 return BasicBlockAddressMap;
210 }
211
212
Chris Lattnere6761db2002-12-24 00:01:22 +0000213private:
Reid Kleckner9a10db82009-09-20 23:52:43 +0000214 static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
215 TargetMachine &tm);
Dan Gohman21cb4112009-02-06 21:25:08 +0000216 void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
Nate Begeman18d85e72009-02-18 08:31:02 +0000217 void updateFunctionStub(Function *F);
Chris Lattnerb6df00c2010-07-11 23:07:28 +0000218 void jitTheFunction(Function *F, const MutexGuard &locked);
Argyrios Kyrtzidisc65c5252009-05-18 21:06:40 +0000219
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000220protected:
221
222 /// getMemoryforGV - Allocate memory for a global variable.
Craig Topperb51ff602014-03-08 07:51:20 +0000223 char* getMemoryForGV(const GlobalVariable* GV) override;
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000224
Chris Lattnere6761db2002-12-24 00:01:22 +0000225};
226
Brian Gaeke960707c2003-11-11 22:41:34 +0000227} // End llvm namespace
228
Chris Lattnere6761db2002-12-24 00:01:22 +0000229#endif