JIT.h revision 226633
1139823Simp//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
2139365Srik//
3139365Srik//                     The LLVM Compiler Infrastructure
4139365Srik//
5139365Srik// This file is distributed under the University of Illinois Open Source
6139365Srik// License. See LICENSE.TXT for details.
7139365Srik//
8139365Srik//===----------------------------------------------------------------------===//
9139365Srik//
10139365Srik// This file defines the top-level JIT data structure.
11139365Srik//
12139365Srik//===----------------------------------------------------------------------===//
13139365Srik
14139365Srik#ifndef JIT_H
15139365Srik#define JIT_H
16139365Srik
17139365Srik#include "llvm/ExecutionEngine/ExecutionEngine.h"
18139365Srik#include "llvm/PassManager.h"
19139365Srik#include "llvm/Support/ValueHandle.h"
20139365Srik
21139365Sriknamespace llvm {
22139365Srik
23139365Srikclass Function;
24139365Srikstruct JITEvent_EmittedFunctionDetails;
25139365Srikclass MachineCodeEmitter;
26139365Srikclass MachineCodeInfo;
27139365Srikclass TargetJITInfo;
28227503Srmhclass TargetMachine;
29139365Srik
30139365Srikclass JITState {
31139365Srikprivate:
32139365Srik  FunctionPassManager PM;  // Passes to compile a function
33139365Srik  Module *M;               // Module used to create the PM
34139365Srik
35139365Srik  /// PendingFunctions - Functions which have not been code generated yet, but
36139365Srik  /// were called from a function being code generated.
37139365Srik  std::vector<AssertingVH<Function> > PendingFunctions;
38139365Srik
39139365Srikpublic:
40139365Srik  explicit JITState(Module *M) : PM(M), M(M) {}
41139365Srik
42139365Srik  FunctionPassManager &getPM(const MutexGuard &L) {
43139365Srik    return PM;
44139365Srik  }
45139365Srik
46139365Srik  Module *getModule() const { return M; }
47139365Srik  std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48227503Srmh    return PendingFunctions;
49139365Srik  }
50139365Srik};
51139365Srik
52139365Srik
53139365Srikclass JIT : public ExecutionEngine {
54139365Srik  /// types
55139365Srik  typedef ValueMap<const BasicBlock *, void *>
56139365Srik      BasicBlockAddressMapTy;
57139365Srik  /// data
58139365Srik  TargetMachine &TM;       // The current target we are compiling to
59139365Srik  TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
60139365Srik  JITCodeEmitter *JCE;     // JCE object
61139365Srik  std::vector<JITEventListener*> EventListeners;
62139365Srik
63139365Srik  /// AllocateGVsWithCode - Some applications require that global variables and
64139365Srik  /// code be allocated into the same region of memory, in which case this flag
65139365Srik  /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
66139365Srik  bool AllocateGVsWithCode;
67139365Srik
68139365Srik  /// True while the JIT is generating code.  Used to assert against recursive
69139365Srik  /// entry.
70139365Srik  bool isAlreadyCodeGenerating;
71139365Srik
72139365Srik  JITState *jitstate;
73139365Srik
74139365Srik  /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their
75139365Srik  /// actualized version, only filled for basic blocks that have their address
76139365Srik  /// taken.
77139365Srik  BasicBlockAddressMapTy BasicBlockAddressMap;
78139365Srik
79139365Srik
80139365Srik  JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
81139365Srik      JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
82139365Srik      bool AllocateGVsWithCode);
83139365Srikpublic:
84139365Srik  ~JIT();
85139365Srik
86139365Srik  static void Register() {
87139365Srik    JITCtor = createJIT;
88139365Srik  }
89139365Srik
90139365Srik  /// getJITInfo - Return the target JIT information structure.
91139365Srik  ///
92139365Srik  TargetJITInfo &getJITInfo() const { return TJI; }
93139365Srik
94139365Srik  /// create - Create an return a new JIT compiler if there is one available
95139365Srik  /// for the current target.  Otherwise, return null.
96139365Srik  ///
97139365Srik  static ExecutionEngine *create(Module *M,
98139365Srik                                 std::string *Err,
99139365Srik                                 JITMemoryManager *JMM,
100139365Srik                                 CodeGenOpt::Level OptLevel =
101139365Srik                                   CodeGenOpt::Default,
102139365Srik                                 bool GVsWithCode = true,
103139365Srik                                 Reloc::Model RM = Reloc::Default,
104139365Srik                                 CodeModel::Model CMM = CodeModel::JITDefault) {
105139365Srik    return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode,
106139365Srik                                      RM, CMM);
107139365Srik  }
108139365Srik
109139365Srik  virtual void addModule(Module *M);
110139365Srik
111139365Srik  /// removeModule - Remove a Module from the list of modules.  Returns true if
112139365Srik  /// M is found.
113139365Srik  virtual bool removeModule(Module *M);
114139365Srik
115139365Srik  /// runFunction - Start execution with the specified function and arguments.
116139365Srik  ///
117139365Srik  virtual GenericValue runFunction(Function *F,
118139365Srik                                   const std::vector<GenericValue> &ArgValues);
119139365Srik
120139365Srik  /// getPointerToNamedFunction - This method returns the address of the
121139365Srik  /// specified function by using the dlsym function call.  As such it is only
122139365Srik  /// useful for resolving library symbols, not code generated symbols.
123139365Srik  ///
124139365Srik  /// If AbortOnFailure is false and no function with the given name is
125139365Srik  /// found, this function silently returns a null pointer. Otherwise,
126139365Srik  /// it prints a message to stderr and aborts.
127139365Srik  ///
128139365Srik  void *getPointerToNamedFunction(const std::string &Name,
129139365Srik                                  bool AbortOnFailure = true);
130139365Srik
131139365Srik  // CompilationCallback - Invoked the first time that a call site is found,
132139365Srik  // which causes lazy compilation of the target function.
133139365Srik  //
134139365Srik  static void CompilationCallback();
135139365Srik
136139365Srik  /// getPointerToFunction - This returns the address of the specified function,
137139365Srik  /// compiling it if necessary.
138139365Srik  ///
139139365Srik  void *getPointerToFunction(Function *F);
140139365Srik
141139365Srik  /// addPointerToBasicBlock - Adds address of the specific basic block.
142139365Srik  void addPointerToBasicBlock(const BasicBlock *BB, void *Addr);
143139365Srik
144139365Srik  /// clearPointerToBasicBlock - Removes address of specific basic block.
145139365Srik  void clearPointerToBasicBlock(const BasicBlock *BB);
146139365Srik
147139365Srik  /// getPointerToBasicBlock - This returns the address of the specified basic
148139365Srik  /// block, assuming function is compiled.
149139365Srik  void *getPointerToBasicBlock(BasicBlock *BB);
150139365Srik
151139365Srik  /// getOrEmitGlobalVariable - Return the address of the specified global
152227503Srmh  /// variable, possibly emitting it to memory if needed.  This is used by the
153139365Srik  /// Emitter.
154139365Srik  void *getOrEmitGlobalVariable(const GlobalVariable *GV);
155139365Srik
156139365Srik  /// getPointerToFunctionOrStub - If the specified function has been
157139365Srik  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
158139365Srik  /// a stub to implement lazy compilation if available.
159139365Srik  ///
160139365Srik  void *getPointerToFunctionOrStub(Function *F);
161139365Srik
162147256Sbrooks  /// recompileAndRelinkFunction - This method is used to force a function
163139365Srik  /// which has already been compiled, to be compiled again, possibly
164139365Srik  /// after it has been modified. Then the entry to the old copy is overwritten
165139365Srik  /// with a branch to the new copy. If there was no old copy, this acts
166139365Srik  /// just like JIT::getPointerToFunction().
167139365Srik  ///
168139365Srik  void *recompileAndRelinkFunction(Function *F);
169139365Srik
170139365Srik  /// freeMachineCodeForFunction - deallocate memory used to code-generate this
171139365Srik  /// Function.
172139365Srik  ///
173139365Srik  void freeMachineCodeForFunction(Function *F);
174139365Srik
175139365Srik  /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
176139365Srik  /// function was encountered.  Add it to a pending list to be processed after
177139365Srik  /// the current function.
178139365Srik  ///
179139365Srik  void addPendingFunction(Function *F);
180139365Srik
181139365Srik  /// getCodeEmitter - Return the code emitter this JIT is emitting into.
182139365Srik  ///
183139365Srik  JITCodeEmitter *getCodeEmitter() const { return JCE; }
184139365Srik
185139365Srik  static ExecutionEngine *createJIT(Module *M,
186139365Srik                                    std::string *ErrorStr,
187139365Srik                                    JITMemoryManager *JMM,
188139365Srik                                    CodeGenOpt::Level OptLevel,
189139365Srik                                    bool GVsWithCode,
190139365Srik                                    TargetMachine *TM);
191139365Srik
192139365Srik  // Run the JIT on F and return information about the generated code
193139365Srik  void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
194139365Srik
195139365Srik  virtual void RegisterJITEventListener(JITEventListener *L);
196139365Srik  virtual void UnregisterJITEventListener(JITEventListener *L);
197139365Srik  /// These functions correspond to the methods on JITEventListener.  They
198139365Srik  /// iterate over the registered listeners and call the corresponding method on
199139365Srik  /// each.
200139365Srik  void NotifyFunctionEmitted(
201139365Srik      const Function &F, void *Code, size_t Size,
202139365Srik      const JITEvent_EmittedFunctionDetails &Details);
203139365Srik  void NotifyFreeingMachineCode(void *OldPtr);
204139365Srik
205139365Srik  BasicBlockAddressMapTy &
206139365Srik  getBasicBlockAddressMap(const MutexGuard &) {
207139365Srik    return BasicBlockAddressMap;
208139365Srik  }
209139365Srik
210139365Srik
211139365Srikprivate:
212139365Srik  static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
213139365Srik                                       TargetMachine &tm);
214139365Srik  void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
215139365Srik  void updateFunctionStub(Function *F);
216139365Srik  void jitTheFunction(Function *F, const MutexGuard &locked);
217139365Srik
218139365Srikprotected:
219139365Srik
220139365Srik  /// getMemoryforGV - Allocate memory for a global variable.
221139365Srik  virtual char* getMemoryForGV(const GlobalVariable* GV);
222139365Srik
223139365Srik};
224139365Srik
225139365Srik} // End llvm namespace
226139365Srik
227139365Srik#endif
228139365Srik