1193323Sed//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the top-level JIT data structure. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#ifndef JIT_H 15193323Sed#define JIT_H 16193323Sed 17193323Sed#include "llvm/ExecutionEngine/ExecutionEngine.h" 18193323Sed#include "llvm/PassManager.h" 19198090Srdivacky#include "llvm/Support/ValueHandle.h" 20193323Sed 21193323Sednamespace llvm { 22193323Sed 23193323Sedclass Function; 24198090Srdivackystruct JITEvent_EmittedFunctionDetails; 25193323Sedclass MachineCodeEmitter; 26193323Sedclass MachineCodeInfo; 27195098Sedclass TargetJITInfo; 28195098Sedclass TargetMachine; 29193323Sed 30193323Sedclass JITState { 31193323Sedprivate: 32193323Sed FunctionPassManager PM; // Passes to compile a function 33203954Srdivacky Module *M; // Module used to create the PM 34193323Sed 35193323Sed /// PendingFunctions - Functions which have not been code generated yet, but 36193323Sed /// were called from a function being code generated. 37198090Srdivacky std::vector<AssertingVH<Function> > PendingFunctions; 38193323Sed 39193323Sedpublic: 40203954Srdivacky explicit JITState(Module *M) : PM(M), M(M) {} 41193323Sed 42193323Sed FunctionPassManager &getPM(const MutexGuard &L) { 43193323Sed return PM; 44193323Sed } 45221345Sdim 46203954Srdivacky Module *getModule() const { return M; } 47198090Srdivacky std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){ 48193323Sed return PendingFunctions; 49193323Sed } 50193323Sed}; 51193323Sed 52193323Sed 53193323Sedclass JIT : public ExecutionEngine { 54210299Sed /// types 55210299Sed typedef ValueMap<const BasicBlock *, void *> 56210299Sed BasicBlockAddressMapTy; 57210299Sed /// data 58193323Sed TargetMachine &TM; // The current target we are compiling to 59193323Sed TargetJITInfo &TJI; // The JITInfo for the target we are compiling to 60193323Sed JITCodeEmitter *JCE; // JCE object 61235633Sdim JITMemoryManager *JMM; 62195098Sed std::vector<JITEventListener*> EventListeners; 63193323Sed 64198090Srdivacky /// AllocateGVsWithCode - Some applications require that global variables and 65198090Srdivacky /// code be allocated into the same region of memory, in which case this flag 66198090Srdivacky /// should be set to true. Doing so breaks freeMachineCodeForFunction. 67198090Srdivacky bool AllocateGVsWithCode; 68198090Srdivacky 69203954Srdivacky /// True while the JIT is generating code. Used to assert against recursive 70203954Srdivacky /// entry. 71203954Srdivacky bool isAlreadyCodeGenerating; 72203954Srdivacky 73193323Sed JITState *jitstate; 74193323Sed 75210299Sed /// BasicBlockAddressMap - A mapping between LLVM basic blocks and their 76210299Sed /// actualized version, only filled for basic blocks that have their address 77210299Sed /// taken. 78210299Sed BasicBlockAddressMapTy BasicBlockAddressMap; 79210299Sed 80210299Sed 81203954Srdivacky JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, 82235633Sdim JITMemoryManager *JMM, bool AllocateGVsWithCode); 83193323Sedpublic: 84193323Sed ~JIT(); 85193323Sed 86193323Sed static void Register() { 87203954Srdivacky JITCtor = createJIT; 88193323Sed } 89221345Sdim 90193323Sed /// getJITInfo - Return the target JIT information structure. 91193323Sed /// 92193323Sed TargetJITInfo &getJITInfo() const { return TJI; } 93193323Sed 94193323Sed /// create - Create an return a new JIT compiler if there is one available 95193323Sed /// for the current target. Otherwise, return null. 96193323Sed /// 97203954Srdivacky static ExecutionEngine *create(Module *M, 98198090Srdivacky std::string *Err, 99198090Srdivacky JITMemoryManager *JMM, 100193323Sed CodeGenOpt::Level OptLevel = 101198090Srdivacky CodeGenOpt::Default, 102199481Srdivacky bool GVsWithCode = true, 103226890Sdim Reloc::Model RM = Reloc::Default, 104226890Sdim CodeModel::Model CMM = CodeModel::JITDefault) { 105203954Srdivacky return ExecutionEngine::createJIT(M, Err, JMM, OptLevel, GVsWithCode, 106226890Sdim RM, CMM); 107193323Sed } 108193323Sed 109203954Srdivacky virtual void addModule(Module *M); 110221345Sdim 111203954Srdivacky /// removeModule - Remove a Module from the list of modules. Returns true if 112203954Srdivacky /// M is found. 113203954Srdivacky virtual bool removeModule(Module *M); 114193323Sed 115193323Sed /// runFunction - Start execution with the specified function and arguments. 116193323Sed /// 117193323Sed virtual GenericValue runFunction(Function *F, 118193323Sed const std::vector<GenericValue> &ArgValues); 119193323Sed 120193323Sed /// getPointerToNamedFunction - This method returns the address of the 121235633Sdim /// specified function by using the MemoryManager. As such it is only 122193323Sed /// useful for resolving library symbols, not code generated symbols. 123193323Sed /// 124193323Sed /// If AbortOnFailure is false and no function with the given name is 125193323Sed /// found, this function silently returns a null pointer. Otherwise, 126193323Sed /// it prints a message to stderr and aborts. 127193323Sed /// 128235633Sdim virtual void *getPointerToNamedFunction(const std::string &Name, 129235633Sdim bool AbortOnFailure = true); 130193323Sed 131193323Sed // CompilationCallback - Invoked the first time that a call site is found, 132193323Sed // which causes lazy compilation of the target function. 133193323Sed // 134193323Sed static void CompilationCallback(); 135193323Sed 136193323Sed /// getPointerToFunction - This returns the address of the specified function, 137193323Sed /// compiling it if necessary. 138193323Sed /// 139193323Sed void *getPointerToFunction(Function *F); 140193323Sed 141210299Sed /// addPointerToBasicBlock - Adds address of the specific basic block. 142210299Sed void addPointerToBasicBlock(const BasicBlock *BB, void *Addr); 143210299Sed 144210299Sed /// clearPointerToBasicBlock - Removes address of specific basic block. 145210299Sed void clearPointerToBasicBlock(const BasicBlock *BB); 146210299Sed 147210299Sed /// getPointerToBasicBlock - This returns the address of the specified basic 148210299Sed /// block, assuming function is compiled. 149210299Sed void *getPointerToBasicBlock(BasicBlock *BB); 150221345Sdim 151193323Sed /// getOrEmitGlobalVariable - Return the address of the specified global 152193323Sed /// variable, possibly emitting it to memory if needed. This is used by the 153193323Sed /// Emitter. 154193323Sed void *getOrEmitGlobalVariable(const GlobalVariable *GV); 155193323Sed 156193323Sed /// getPointerToFunctionOrStub - If the specified function has been 157193323Sed /// code-gen'd, return a pointer to the function. If not, compile it, or use 158193323Sed /// a stub to implement lazy compilation if available. 159193323Sed /// 160193323Sed void *getPointerToFunctionOrStub(Function *F); 161193323Sed 162193323Sed /// recompileAndRelinkFunction - This method is used to force a function 163193323Sed /// which has already been compiled, to be compiled again, possibly 164193323Sed /// after it has been modified. Then the entry to the old copy is overwritten 165193323Sed /// with a branch to the new copy. If there was no old copy, this acts 166193323Sed /// just like JIT::getPointerToFunction(). 167193323Sed /// 168193323Sed void *recompileAndRelinkFunction(Function *F); 169193323Sed 170193323Sed /// freeMachineCodeForFunction - deallocate memory used to code-generate this 171193323Sed /// Function. 172193323Sed /// 173193323Sed void freeMachineCodeForFunction(Function *F); 174193323Sed 175193323Sed /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd 176221345Sdim /// function was encountered. Add it to a pending list to be processed after 177193323Sed /// the current function. 178198090Srdivacky /// 179193323Sed void addPendingFunction(Function *F); 180198090Srdivacky 181193323Sed /// getCodeEmitter - Return the code emitter this JIT is emitting into. 182198090Srdivacky /// 183193323Sed JITCodeEmitter *getCodeEmitter() const { return JCE; } 184198090Srdivacky 185203954Srdivacky static ExecutionEngine *createJIT(Module *M, 186198090Srdivacky std::string *ErrorStr, 187193323Sed JITMemoryManager *JMM, 188199481Srdivacky bool GVsWithCode, 189223017Sdim TargetMachine *TM); 190193323Sed 191193323Sed // Run the JIT on F and return information about the generated code 192193323Sed void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0); 193193323Sed 194195098Sed virtual void RegisterJITEventListener(JITEventListener *L); 195195098Sed virtual void UnregisterJITEventListener(JITEventListener *L); 196195098Sed /// These functions correspond to the methods on JITEventListener. They 197195098Sed /// iterate over the registered listeners and call the corresponding method on 198195098Sed /// each. 199195098Sed void NotifyFunctionEmitted( 200195098Sed const Function &F, void *Code, size_t Size, 201195098Sed const JITEvent_EmittedFunctionDetails &Details); 202198892Srdivacky void NotifyFreeingMachineCode(void *OldPtr); 203195098Sed 204210299Sed BasicBlockAddressMapTy & 205210299Sed getBasicBlockAddressMap(const MutexGuard &) { 206210299Sed return BasicBlockAddressMap; 207210299Sed } 208210299Sed 209210299Sed 210193323Sedprivate: 211198090Srdivacky static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM, 212198090Srdivacky TargetMachine &tm); 213193323Sed void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked); 214193323Sed void updateFunctionStub(Function *F); 215210299Sed void jitTheFunction(Function *F, const MutexGuard &locked); 216193323Sed 217193323Sedprotected: 218193323Sed 219193323Sed /// getMemoryforGV - Allocate memory for a global variable. 220193323Sed virtual char* getMemoryForGV(const GlobalVariable* GV); 221193323Sed 222193323Sed}; 223193323Sed 224193323Sed} // End llvm namespace 225193323Sed 226193323Sed#endif 227