Make it possible to create multiple JIT instances at the same time, by removing
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.h
1 //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
19 #include "llvm/Support/ValueHandle.h"
20
21 namespace llvm {
22
23 class Function;
24 struct JITEvent_EmittedFunctionDetails;
25 class MachineCodeEmitter;
26 class MachineCodeInfo;
27 class TargetJITInfo;
28 class TargetMachine;
29
30 class JITState {
31 private:
32   FunctionPassManager PM;  // Passes to compile a function
33   Module *M;               // Module used to create the PM
34
35   /// PendingFunctions - Functions which have not been code generated yet, but
36   /// were called from a function being code generated.
37   std::vector<AssertingVH<Function> > PendingFunctions;
38
39 public:
40   explicit JITState(Module *M) : PM(M), M(M) {}
41
42   FunctionPassManager &getPM(const MutexGuard &L) {
43     return PM;
44   }
45   
46   Module *getModule() const { return M; }
47   std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48     return PendingFunctions;
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
56   JITCodeEmitter *JCE;     // JCE object
57   std::vector<JITEventListener*> EventListeners;
58
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
64   /// True while the JIT is generating code.  Used to assert against recursive
65   /// entry.
66   bool isAlreadyCodeGenerating;
67
68   JITState *jitstate;
69
70   JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
71       JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
72       bool AllocateGVsWithCode);
73 public:
74   ~JIT();
75
76   static void Register() {
77     JITCtor = createJIT;
78   }
79   
80   /// getJITInfo - Return the target JIT information structure.
81   ///
82   TargetJITInfo &getJITInfo() const { return TJI; }
83
84   /// create - Create an return a new JIT compiler if there is one available
85   /// for the current target.  Otherwise, return null.
86   ///
87   static ExecutionEngine *create(Module *M,
88                                  std::string *Err,
89                                  JITMemoryManager *JMM,
90                                  CodeGenOpt::Level OptLevel =
91                                    CodeGenOpt::Default,
92                                  bool GVsWithCode = true,
93                                  CodeModel::Model CMM = CodeModel::Default) {
94     return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
95                                       CMM);
96   }
97
98   virtual void addModule(Module *M);
99   
100   /// removeModule - Remove a Module from the list of modules.  Returns true if
101   /// M is found.
102   virtual bool removeModule(Module *M);
103
104   /// runFunction - Start execution with the specified function and arguments.
105   ///
106   virtual GenericValue runFunction(Function *F,
107                                    const std::vector<GenericValue> &ArgValues);
108
109   /// getPointerToNamedFunction - This method returns the address of the
110   /// specified function by using the dlsym function call.  As such it is only
111   /// useful for resolving library symbols, not code generated symbols.
112   ///
113   /// If AbortOnFailure is false and no function with the given name is
114   /// found, this function silently returns a null pointer. Otherwise,
115   /// it prints a message to stderr and aborts.
116   ///
117   void *getPointerToNamedFunction(const std::string &Name,
118                                   bool AbortOnFailure = true);
119
120   // CompilationCallback - Invoked the first time that a call site is found,
121   // which causes lazy compilation of the target function.
122   //
123   static void CompilationCallback();
124
125   /// getPointerToFunction - This returns the address of the specified function,
126   /// compiling it if necessary.
127   ///
128   void *getPointerToFunction(Function *F);
129
130   void *getPointerToBasicBlock(BasicBlock *BB) {
131     assert(0 && "JIT does not support address-of-label yet!");
132     return 0;
133   }
134   
135   /// getOrEmitGlobalVariable - Return the address of the specified global
136   /// variable, possibly emitting it to memory if needed.  This is used by the
137   /// Emitter.
138   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
139
140   /// getPointerToFunctionOrStub - If the specified function has been
141   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
142   /// a stub to implement lazy compilation if available.
143   ///
144   void *getPointerToFunctionOrStub(Function *F);
145
146   /// recompileAndRelinkFunction - This method is used to force a function
147   /// which has already been compiled, to be compiled again, possibly
148   /// after it has been modified. Then the entry to the old copy is overwritten
149   /// with a branch to the new copy. If there was no old copy, this acts
150   /// just like JIT::getPointerToFunction().
151   ///
152   void *recompileAndRelinkFunction(Function *F);
153
154   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
155   /// Function.
156   ///
157   void freeMachineCodeForFunction(Function *F);
158
159   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
160   /// function was encountered.  Add it to a pending list to be processed after 
161   /// the current function.
162   ///
163   void addPendingFunction(Function *F);
164
165   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
166   ///
167   JITCodeEmitter *getCodeEmitter() const { return JCE; }
168
169   /// selectTarget - Pick a target either via -march or by guessing the native
170   /// arch.  Add any CPU features specified via -mcpu or -mattr.
171   static TargetMachine *selectTarget(Module *M,
172                                      StringRef MArch,
173                                      StringRef MCPU,
174                                      const SmallVectorImpl<std::string>& MAttrs,
175                                      std::string *Err);
176
177   static ExecutionEngine *createJIT(Module *M,
178                                     std::string *ErrorStr,
179                                     JITMemoryManager *JMM,
180                                     CodeGenOpt::Level OptLevel,
181                                     bool GVsWithCode,
182                                     CodeModel::Model CMM,
183                                     StringRef MArch,
184                                     StringRef MCPU,
185                                     const SmallVectorImpl<std::string>& MAttrs);
186
187   // Run the JIT on F and return information about the generated code
188   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
189
190   virtual void RegisterJITEventListener(JITEventListener *L);
191   virtual void UnregisterJITEventListener(JITEventListener *L);
192   /// These functions correspond to the methods on JITEventListener.  They
193   /// iterate over the registered listeners and call the corresponding method on
194   /// each.
195   void NotifyFunctionEmitted(
196       const Function &F, void *Code, size_t Size,
197       const JITEvent_EmittedFunctionDetails &Details);
198   void NotifyFreeingMachineCode(void *OldPtr);
199
200 private:
201   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
202                                        TargetMachine &tm);
203   void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
204   void updateFunctionStub(Function *F);
205
206 protected:
207
208   /// getMemoryforGV - Allocate memory for a global variable.
209   virtual char* getMemoryForGV(const GlobalVariable* GV);
210
211 };
212
213 } // End llvm namespace
214
215 #endif