LLVM API Documentation

lib/ExecutionEngine/JIT/JIT.h

Go to the documentation of this file.
00001 //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the top-level JIT data structure.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef JIT_H
00015 #define JIT_H
00016 
00017 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00018 #include "llvm/PassManager.h"
00019 
00020 namespace llvm {
00021 
00022 class Function;
00023 struct JITEvent_EmittedFunctionDetails;
00024 class MachineCodeEmitter;
00025 class MachineCodeInfo;
00026 class TargetJITInfo;
00027 class TargetMachine;
00028 
00029 class JITState {
00030 private:
00031   FunctionPassManager PM;  // Passes to compile a function
00032   ModuleProvider *MP;      // ModuleProvider used to create the PM
00033 
00034   /// PendingFunctions - Functions which have not been code generated yet, but
00035   /// were called from a function being code generated.
00036   std::vector<Function*> PendingFunctions;
00037 
00038 public:
00039   explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
00040 
00041   FunctionPassManager &getPM(const MutexGuard &L) {
00042     return PM;
00043   }
00044   
00045   ModuleProvider *getMP() const { return MP; }
00046   std::vector<Function*> &getPendingFunctions(const MutexGuard &L) {
00047     return PendingFunctions;
00048   }
00049 };
00050 
00051 
00052 class JIT : public ExecutionEngine {
00053   TargetMachine &TM;       // The current target we are compiling to
00054   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
00055   JITCodeEmitter *JCE;     // JCE object
00056   std::vector<JITEventListener*> EventListeners;
00057 
00058   /// AllocateGVsWithCode - Some applications require that global variables and
00059   /// code be allocated into the same region of memory, in which case this flag
00060   /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
00061   bool AllocateGVsWithCode;
00062 
00063   JITState *jitstate;
00064 
00065   JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
00066       JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
00067       bool AllocateGVsWithCode);
00068 public:
00069   ~JIT();
00070 
00071   static void Register() {
00072     JITCtor = create;
00073   }
00074   
00075   /// getJITInfo - Return the target JIT information structure.
00076   ///
00077   TargetJITInfo &getJITInfo() const { return TJI; }
00078 
00079   /// create - Create an return a new JIT compiler if there is one available
00080   /// for the current target.  Otherwise, return null.
00081   ///
00082   static ExecutionEngine *create(ModuleProvider *MP,
00083                                  std::string *Err,
00084                                  JITMemoryManager *JMM,
00085                                  CodeGenOpt::Level OptLevel =
00086                                    CodeGenOpt::Default,
00087                                  bool GVsWithCode = true) {
00088     return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode);
00089   }
00090 
00091   virtual void addModuleProvider(ModuleProvider *MP);
00092   
00093   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
00094   /// Relases the Module from the ModuleProvider, materializing it in the
00095   /// process, and returns the materialized Module.
00096   virtual Module *removeModuleProvider(ModuleProvider *MP,
00097                                        std::string *ErrInfo = 0);
00098 
00099   /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
00100   /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
00101   /// the underlying module.
00102   virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
00103 
00104   /// runFunction - Start execution with the specified function and arguments.
00105   ///
00106   virtual GenericValue runFunction(Function *F,
00107                                    const std::vector<GenericValue> &ArgValues);
00108 
00109   /// getPointerToNamedFunction - This method returns the address of the
00110   /// specified function by using the dlsym function call.  As such it is only
00111   /// useful for resolving library symbols, not code generated symbols.
00112   ///
00113   /// If AbortOnFailure is false and no function with the given name is
00114   /// found, this function silently returns a null pointer. Otherwise,
00115   /// it prints a message to stderr and aborts.
00116   ///
00117   void *getPointerToNamedFunction(const std::string &Name,
00118                                   bool AbortOnFailure = true);
00119 
00120   // CompilationCallback - Invoked the first time that a call site is found,
00121   // which causes lazy compilation of the target function.
00122   //
00123   static void CompilationCallback();
00124 
00125   /// getPointerToFunction - This returns the address of the specified function,
00126   /// compiling it if necessary.
00127   ///
00128   void *getPointerToFunction(Function *F);
00129 
00130   /// getOrEmitGlobalVariable - Return the address of the specified global
00131   /// variable, possibly emitting it to memory if needed.  This is used by the
00132   /// Emitter.
00133   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
00134 
00135   /// getPointerToFunctionOrStub - If the specified function has been
00136   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
00137   /// a stub to implement lazy compilation if available.
00138   ///
00139   void *getPointerToFunctionOrStub(Function *F);
00140 
00141   /// recompileAndRelinkFunction - This method is used to force a function
00142   /// which has already been compiled, to be compiled again, possibly
00143   /// after it has been modified. Then the entry to the old copy is overwritten
00144   /// with a branch to the new copy. If there was no old copy, this acts
00145   /// just like JIT::getPointerToFunction().
00146   ///
00147   void *recompileAndRelinkFunction(Function *F);
00148 
00149   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
00150   /// Function.
00151   ///
00152   void freeMachineCodeForFunction(Function *F);
00153 
00154   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
00155   /// function was encountered.  Add it to a pending list to be processed after 
00156   /// the current function.
00157   ///
00158   void addPendingFunction(Function *F);
00159 
00160   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
00161   ///
00162   JITCodeEmitter *getCodeEmitter() const { return JCE; }
00163 
00164   /// selectTarget - Pick a target either via -march or by guessing the native
00165   /// arch.  Add any CPU features specified via -mcpu or -mattr.
00166   static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
00167 
00168   static ExecutionEngine *createJIT(ModuleProvider *MP,
00169                                     std::string *ErrorStr,
00170                                     JITMemoryManager *JMM,
00171                                     CodeGenOpt::Level OptLevel,
00172                                     bool GVsWithCode);
00173 
00174   // Run the JIT on F and return information about the generated code
00175   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
00176 
00177   virtual void RegisterJITEventListener(JITEventListener *L);
00178   virtual void UnregisterJITEventListener(JITEventListener *L);
00179   /// These functions correspond to the methods on JITEventListener.  They
00180   /// iterate over the registered listeners and call the corresponding method on
00181   /// each.
00182   void NotifyFunctionEmitted(
00183       const Function &F, void *Code, size_t Size,
00184       const JITEvent_EmittedFunctionDetails &Details);
00185   void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
00186 
00187 private:
00188   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
00189   void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
00190   void updateFunctionStub(Function *F);
00191   void updateDlsymStubTable();
00192 
00193 protected:
00194 
00195   /// getMemoryforGV - Allocate memory for a global variable.
00196   virtual char* getMemoryForGV(const GlobalVariable* GV);
00197 
00198 };
00199 
00200 } // End llvm namespace
00201 
00202 #endif