Convert some tab stops into spaces.
[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   /// types
55   typedef ValueMap<const BasicBlock *, void *>
56       BasicBlockAddressMapTy;
57   /// data
58   TargetMachine &TM;       // The current target we are compiling to
59   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
60   JITCodeEmitter *JCE;     // JCE object
61   std::vector<JITEventListener*> EventListeners;
62
63   /// AllocateGVsWithCode - Some applications require that global variables and
64   /// code be allocated into the same region of memory, in which case this flag
65   /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
66   bool AllocateGVsWithCode;
67
68   /// True while the JIT is generating code.  Used to assert against recursive
69   /// entry.
70   bool isAlreadyCodeGenerating;
71
72   JITState *jitstate;
73
74   /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their
75   /// actualized version, only filled for basic blocks that have their address
76   /// taken.
77   BasicBlockAddressMapTy BasicBlockAddressMap;
78
79
80   JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
81       JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
82       bool AllocateGVsWithCode);
83 public:
84   ~JIT();
85
86   static void Register() {
87     JITCtor = createJIT;
88   }
89   
90   /// getJITInfo - Return the target JIT information structure.
91   ///
92   TargetJITInfo &getJITInfo() const { return TJI; }
93
94   /// create - Create an return a new JIT compiler if there is one available
95   /// for the current target.  Otherwise, return null.
96   ///
97   static ExecutionEngine *create(Module *M,
98                                  std::string *Err,
99                                  JITMemoryManager *JMM,
100                                  CodeGenOpt::Level OptLevel =
101                                    CodeGenOpt::Default,
102                                  bool GVsWithCode = true,
103                                  CodeModel::Model CMM = CodeModel::Default) {
104     return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
105                                       CMM);
106   }
107
108   virtual void addModule(Module *M);
109   
110   /// removeModule - Remove a Module from the list of modules.  Returns true if
111   /// M is found.
112   virtual bool removeModule(Module *M);
113
114   /// runFunction - Start execution with the specified function and arguments.
115   ///
116   virtual GenericValue runFunction(Function *F,
117                                    const std::vector<GenericValue> &ArgValues);
118
119   /// getPointerToNamedFunction - This method returns the address of the
120   /// specified function by using the dlsym function call.  As such it is only
121   /// useful for resolving library symbols, not code generated symbols.
122   ///
123   /// If AbortOnFailure is false and no function with the given name is
124   /// found, this function silently returns a null pointer. Otherwise,
125   /// it prints a message to stderr and aborts.
126   ///
127   void *getPointerToNamedFunction(const std::string &Name,
128                                   bool AbortOnFailure = true);
129
130   // CompilationCallback - Invoked the first time that a call site is found,
131   // which causes lazy compilation of the target function.
132   //
133   static void CompilationCallback();
134
135   /// getPointerToFunction - This returns the address of the specified function,
136   /// compiling it if necessary.
137   ///
138   void *getPointerToFunction(Function *F);
139
140   /// addPointerToBasicBlock - Adds address of the specific basic block.
141   void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
142
143   /// clearPointerToBasicBlock - Removes address of specific basic block.
144   void clearPointerToBasicBlock(const BasicBlock *BB);
145
146   /// getPointerToBasicBlock - This returns the address of the specified basic
147   /// block, assuming function is compiled.
148   void *getPointerToBasicBlock(BasicBlock *BB);
149   
150   /// getOrEmitGlobalVariable - Return the address of the specified global
151   /// variable, possibly emitting it to memory if needed.  This is used by the
152   /// Emitter.
153   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
154
155   /// getPointerToFunctionOrStub - If the specified function has been
156   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
157   /// a stub to implement lazy compilation if available.
158   ///
159   void *getPointerToFunctionOrStub(Function *F);
160
161   /// recompileAndRelinkFunction - This method is used to force a function
162   /// which has already been compiled, to be compiled again, possibly
163   /// after it has been modified. Then the entry to the old copy is overwritten
164   /// with a branch to the new copy. If there was no old copy, this acts
165   /// just like JIT::getPointerToFunction().
166   ///
167   void *recompileAndRelinkFunction(Function *F);
168
169   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
170   /// Function.
171   ///
172   void freeMachineCodeForFunction(Function *F);
173
174   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
175   /// function was encountered.  Add it to a pending list to be processed after 
176   /// the current function.
177   ///
178   void addPendingFunction(Function *F);
179
180   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
181   ///
182   JITCodeEmitter *getCodeEmitter() const { return JCE; }
183
184   /// selectTarget - Pick a target either via -march or by guessing the native
185   /// arch.  Add any CPU features specified via -mcpu or -mattr.
186   static TargetMachine *selectTarget(Module *M,
187                                      StringRef MArch,
188                                      StringRef MCPU,
189                                      const SmallVectorImpl<std::string>& MAttrs,
190                                      std::string *Err);
191
192   static ExecutionEngine *createJIT(Module *M,
193                                     std::string *ErrorStr,
194                                     JITMemoryManager *JMM,
195                                     CodeGenOpt::Level OptLevel,
196                                     bool GVsWithCode,
197                                     CodeModel::Model CMM,
198                                     StringRef MArch,
199                                     StringRef MCPU,
200                                     const SmallVectorImpl<std::string>& MAttrs);
201
202   // Run the JIT on F and return information about the generated code
203   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
204
205   virtual void RegisterJITEventListener(JITEventListener *L);
206   virtual void UnregisterJITEventListener(JITEventListener *L);
207   /// These functions correspond to the methods on JITEventListener.  They
208   /// iterate over the registered listeners and call the corresponding method on
209   /// each.
210   void NotifyFunctionEmitted(
211       const Function &F, void *Code, size_t Size,
212       const JITEvent_EmittedFunctionDetails &Details);
213   void NotifyFreeingMachineCode(void *OldPtr);
214
215   BasicBlockAddressMapTy &
216   getBasicBlockAddressMap(const MutexGuard &) {
217     return BasicBlockAddressMap;
218   }
219
220
221 private:
222   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
223                                        TargetMachine &tm);
224   void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
225   void updateFunctionStub(Function *F);
226   void jitTheFunction(Function *F, const MutexGuard &locked);
227
228 protected:
229
230   /// getMemoryforGV - Allocate memory for a global variable.
231   virtual char* getMemoryForGV(const GlobalVariable* GV);
232
233 };
234
235 } // End llvm namespace
236
237 #endif