Add EngineBuilder to ExecutionEngine in favor of the five optional argument EE::create().
[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
20 namespace llvm {
21
22 class Function;
23 struct JITEvent_EmittedFunctionDetails;
24 class MachineCodeEmitter;
25 class MachineCodeInfo;
26 class TargetJITInfo;
27 class TargetMachine;
28
29 class JITState {
30 private:
31   FunctionPassManager PM;  // Passes to compile a function
32   ModuleProvider *MP;      // ModuleProvider used to create the PM
33
34   /// PendingFunctions - Functions which have not been code generated yet, but
35   /// were called from a function being code generated.
36   std::vector<Function*> PendingFunctions;
37
38 public:
39   explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
40
41   FunctionPassManager &getPM(const MutexGuard &L) {
42     return PM;
43   }
44   
45   ModuleProvider *getMP() const { return MP; }
46   std::vector<Function*> &getPendingFunctions(const MutexGuard &L) {
47     return PendingFunctions;
48   }
49 };
50
51
52 class JIT : public ExecutionEngine {
53   TargetMachine &TM;       // The current target we are compiling to
54   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
55   JITCodeEmitter *JCE;     // JCE object
56   std::vector<JITEventListener*> EventListeners;
57
58   /// AllocateGVsWithCode - Some applications require that global variables and
59   /// code be allocated into the same region of memory, in which case this flag
60   /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
61   bool AllocateGVsWithCode;
62
63   JITState *jitstate;
64
65   JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
66       JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
67       bool AllocateGVsWithCode);
68 public:
69   ~JIT();
70
71   static void Register() {
72     JITCtor = create;
73   }
74   
75   /// getJITInfo - Return the target JIT information structure.
76   ///
77   TargetJITInfo &getJITInfo() const { return TJI; }
78
79   /// create - Create an return a new JIT compiler if there is one available
80   /// for the current target.  Otherwise, return null.
81   ///
82   static ExecutionEngine *create(ModuleProvider *MP,
83                                  std::string *Err,
84                                  JITMemoryManager *JMM,
85                                  CodeGenOpt::Level OptLevel =
86                                    CodeGenOpt::Default,
87                                  bool GVsWithCode = true) {
88     return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode);
89   }
90
91   virtual void addModuleProvider(ModuleProvider *MP);
92   
93   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
94   /// Relases the Module from the ModuleProvider, materializing it in the
95   /// process, and returns the materialized Module.
96   virtual Module *removeModuleProvider(ModuleProvider *MP,
97                                        std::string *ErrInfo = 0);
98
99   /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
100   /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
101   /// the underlying module.
102   virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
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   /// getOrEmitGlobalVariable - Return the address of the specified global
131   /// variable, possibly emitting it to memory if needed.  This is used by the
132   /// Emitter.
133   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
134
135   /// getPointerToFunctionOrStub - If the specified function has been
136   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
137   /// a stub to implement lazy compilation if available.
138   ///
139   void *getPointerToFunctionOrStub(Function *F);
140
141   /// recompileAndRelinkFunction - This method is used to force a function
142   /// which has already been compiled, to be compiled again, possibly
143   /// after it has been modified. Then the entry to the old copy is overwritten
144   /// with a branch to the new copy. If there was no old copy, this acts
145   /// just like JIT::getPointerToFunction().
146   ///
147   void *recompileAndRelinkFunction(Function *F);
148
149   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
150   /// Function.
151   ///
152   void freeMachineCodeForFunction(Function *F);
153
154   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
155   /// function was encountered.  Add it to a pending list to be processed after 
156   /// the current function.
157   ///
158   void addPendingFunction(Function *F);
159
160   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
161   ///
162   JITCodeEmitter *getCodeEmitter() const { return JCE; }
163
164   /// selectTarget - Pick a target either via -march or by guessing the native
165   /// arch.  Add any CPU features specified via -mcpu or -mattr.
166   static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
167
168   static ExecutionEngine *createJIT(ModuleProvider *MP,
169                                     std::string *ErrorStr,
170                                     JITMemoryManager *JMM,
171                                     CodeGenOpt::Level OptLevel,
172                                     bool GVsWithCode);
173
174   // Run the JIT on F and return information about the generated code
175   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
176
177   virtual void RegisterJITEventListener(JITEventListener *L);
178   virtual void UnregisterJITEventListener(JITEventListener *L);
179   /// These functions correspond to the methods on JITEventListener.  They
180   /// iterate over the registered listeners and call the corresponding method on
181   /// each.
182   void NotifyFunctionEmitted(
183       const Function &F, void *Code, size_t Size,
184       const JITEvent_EmittedFunctionDetails &Details);
185   void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
186
187 private:
188   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
189   void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
190   void updateFunctionStub(Function *F);
191   void updateDlsymStubTable();
192
193 protected:
194
195   /// getMemoryforGV - Allocate memory for a global variable.
196   virtual char* getMemoryForGV(const GlobalVariable* GV);
197
198 };
199
200 } // End llvm namespace
201
202 #endif