Temporarily Revert "Nuke the old JIT." as it's not quite ready to
[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/IR/ValueHandle.h"
19 #include "llvm/PassManager.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() {
43     return PM;
44   }
45
46   Module *getModule() const { return M; }
47   std::vector<AssertingVH<Function> > &getPendingFunctions() {
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   JITMemoryManager *JMM;
62   std::vector<JITEventListener*> EventListeners;
63
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
69   /// True while the JIT is generating code.  Used to assert against recursive
70   /// entry.
71   bool isAlreadyCodeGenerating;
72
73   JITState *jitstate;
74
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
81   JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
82       JITMemoryManager *JMM, 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   void addModule(Module *M) override;
95
96   /// removeModule - Remove a Module from the list of modules.  Returns true if
97   /// M is found.
98   bool removeModule(Module *M) override;
99
100   /// runFunction - Start execution with the specified function and arguments.
101   ///
102   GenericValue runFunction(Function *F,
103                            const std::vector<GenericValue> &ArgValues) override;
104
105   /// getPointerToNamedFunction - This method returns the address of the
106   /// specified function by using the MemoryManager. As such it is only
107   /// useful for resolving library symbols, not code generated symbols.
108   ///
109   /// If AbortOnFailure is false and no function with the given name is
110   /// found, this function silently returns a null pointer. Otherwise,
111   /// it prints a message to stderr and aborts.
112   ///
113   void *getPointerToNamedFunction(const std::string &Name,
114                                   bool AbortOnFailure = true) override;
115
116   // CompilationCallback - Invoked the first time that a call site is found,
117   // which causes lazy compilation of the target function.
118   //
119   static void CompilationCallback();
120
121   /// getPointerToFunction - This returns the address of the specified function,
122   /// compiling it if necessary.
123   ///
124   void *getPointerToFunction(Function *F) override;
125
126   /// addPointerToBasicBlock - Adds address of the specific basic block.
127   void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
128
129   /// clearPointerToBasicBlock - Removes address of specific basic block.
130   void clearPointerToBasicBlock(const BasicBlock *BB);
131
132   /// getPointerToBasicBlock - This returns the address of the specified basic
133   /// block, assuming function is compiled.
134   void *getPointerToBasicBlock(BasicBlock *BB) override;
135
136   /// getOrEmitGlobalVariable - Return the address of the specified global
137   /// variable, possibly emitting it to memory if needed.  This is used by the
138   /// Emitter.
139   void *getOrEmitGlobalVariable(const GlobalVariable *GV) override;
140
141   /// getPointerToFunctionOrStub - If the specified function has been
142   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
143   /// a stub to implement lazy compilation if available.
144   ///
145   void *getPointerToFunctionOrStub(Function *F) override;
146
147   /// recompileAndRelinkFunction - This method is used to force a function
148   /// which has already been compiled, to be compiled again, possibly
149   /// after it has been modified. Then the entry to the old copy is overwritten
150   /// with a branch to the new copy. If there was no old copy, this acts
151   /// just like JIT::getPointerToFunction().
152   ///
153   void *recompileAndRelinkFunction(Function *F) override;
154
155   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
156   /// Function.
157   ///
158   void freeMachineCodeForFunction(Function *F) override;
159
160   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
161   /// function was encountered.  Add it to a pending list to be processed after
162   /// the current function.
163   ///
164   void addPendingFunction(Function *F);
165
166   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
167   ///
168   JITCodeEmitter *getCodeEmitter() const { return JCE; }
169
170   static ExecutionEngine *createJIT(Module *M,
171                                     std::string *ErrorStr,
172                                     JITMemoryManager *JMM,
173                                     bool GVsWithCode,
174                                     TargetMachine *TM);
175
176   // Run the JIT on F and return information about the generated code
177   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = nullptr) override;
178
179   void RegisterJITEventListener(JITEventListener *L) override;
180   void UnregisterJITEventListener(JITEventListener *L) override;
181
182   TargetMachine *getTargetMachine() override { return &TM; }
183
184   /// These functions correspond to the methods on JITEventListener.  They
185   /// iterate over the registered listeners and call the corresponding method on
186   /// each.
187   void NotifyFunctionEmitted(
188       const Function &F, void *Code, size_t Size,
189       const JITEvent_EmittedFunctionDetails &Details);
190   void NotifyFreeingMachineCode(void *OldPtr);
191
192   BasicBlockAddressMapTy &
193   getBasicBlockAddressMap() {
194     return BasicBlockAddressMap;
195   }
196
197
198 private:
199   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
200                                        TargetMachine &tm);
201   void runJITOnFunctionUnlocked(Function *F);
202   void updateFunctionStubUnlocked(Function *F);
203   void jitTheFunctionUnlocked(Function *F);
204
205 protected:
206
207   /// getMemoryforGV - Allocate memory for a global variable.
208   char* getMemoryForGV(const GlobalVariable* GV) override;
209
210 };
211
212 } // End llvm namespace
213
214 #endif