LLVM API Documentation

JIT.cpp
Go to the documentation of this file.
00001 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
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 tool implements a just-in-time compiler for LLVM, allowing direct
00011 // execution of LLVM bitcode in an efficient manner.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "JIT.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/CodeGen/JITCodeEmitter.h"
00018 #include "llvm/CodeGen/MachineCodeInfo.h"
00019 #include "llvm/Config/config.h"
00020 #include "llvm/ExecutionEngine/GenericValue.h"
00021 #include "llvm/ExecutionEngine/JITEventListener.h"
00022 #include "llvm/ExecutionEngine/JITMemoryManager.h"
00023 #include "llvm/IR/Constants.h"
00024 #include "llvm/IR/DataLayout.h"
00025 #include "llvm/IR/DerivedTypes.h"
00026 #include "llvm/IR/Function.h"
00027 #include "llvm/IR/GlobalVariable.h"
00028 #include "llvm/IR/Instructions.h"
00029 #include "llvm/IR/Module.h"
00030 #include "llvm/Support/Dwarf.h"
00031 #include "llvm/Support/DynamicLibrary.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/ManagedStatic.h"
00034 #include "llvm/Support/MutexGuard.h"
00035 #include "llvm/Target/TargetJITInfo.h"
00036 #include "llvm/Target/TargetMachine.h"
00037 #include "llvm/Target/TargetSubtargetInfo.h"
00038 
00039 using namespace llvm;
00040 
00041 #ifdef __APPLE__
00042 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
00043 // of atexit). It passes the address of linker generated symbol __dso_handle
00044 // to the function.
00045 // This configuration change happened at version 5330.
00046 # include <AvailabilityMacros.h>
00047 # if defined(MAC_OS_X_VERSION_10_4) && \
00048      ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
00049       (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
00050        __APPLE_CC__ >= 5330))
00051 #  ifndef HAVE___DSO_HANDLE
00052 #   define HAVE___DSO_HANDLE 1
00053 #  endif
00054 # endif
00055 #endif
00056 
00057 #if HAVE___DSO_HANDLE
00058 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
00059 #endif
00060 
00061 namespace {
00062 
00063 static struct RegisterJIT {
00064   RegisterJIT() { JIT::Register(); }
00065 } JITRegistrator;
00066 
00067 }
00068 
00069 extern "C" void LLVMLinkInJIT() {
00070 }
00071 
00072 /// This is the factory method for creating a JIT for the current machine, it
00073 /// does not fall back to the interpreter.
00074 ExecutionEngine *JIT::createJIT(std::unique_ptr<Module> M,
00075                                 std::string *ErrorStr,
00076                                 JITMemoryManager *JMM,
00077                                 bool GVsWithCode,
00078                                 TargetMachine *TM) {
00079   // Try to register the program as a source of symbols to resolve against.
00080   //
00081   // FIXME: Don't do this here.
00082   sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
00083 
00084   // If the target supports JIT code generation, create the JIT.
00085   if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
00086     return new JIT(std::move(M), *TM, *TJ, JMM, GVsWithCode);
00087   } else {
00088     if (ErrorStr)
00089       *ErrorStr = "target does not support JIT code generation";
00090     return nullptr;
00091   }
00092 }
00093 
00094 namespace {
00095 /// This class supports the global getPointerToNamedFunction(), which allows
00096 /// bugpoint or gdb users to search for a function by name without any context.
00097 class JitPool {
00098   SmallPtrSet<JIT*, 1> JITs;  // Optimize for process containing just 1 JIT.
00099   mutable sys::Mutex Lock;
00100 public:
00101   void Add(JIT *jit) {
00102     MutexGuard guard(Lock);
00103     JITs.insert(jit);
00104   }
00105   void Remove(JIT *jit) {
00106     MutexGuard guard(Lock);
00107     JITs.erase(jit);
00108   }
00109   void *getPointerToNamedFunction(const char *Name) const {
00110     MutexGuard guard(Lock);
00111     assert(JITs.size() != 0 && "No Jit registered");
00112     //search function in every instance of JIT
00113     for (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
00114            end = JITs.end();
00115          Jit != end; ++Jit) {
00116       if (Function *F = (*Jit)->FindFunctionNamed(Name))
00117         return (*Jit)->getPointerToFunction(F);
00118     }
00119     // The function is not available : fallback on the first created (will
00120     // search in symbol of the current program/library)
00121     return (*JITs.begin())->getPointerToNamedFunction(Name);
00122   }
00123 };
00124 ManagedStatic<JitPool> AllJits;
00125 }
00126 extern "C" {
00127   // getPointerToNamedFunction - This function is used as a global wrapper to
00128   // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
00129   // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
00130   // need to resolve function(s) that are being mis-codegenerated, so we need to
00131   // resolve their addresses at runtime, and this is the way to do it.
00132   void *getPointerToNamedFunction(const char *Name) {
00133     return AllJits->getPointerToNamedFunction(Name);
00134   }
00135 }
00136 
00137 JIT::JIT(std::unique_ptr<Module> M, TargetMachine &tm, TargetJITInfo &tji,
00138          JITMemoryManager *jmm, bool GVsWithCode)
00139   : ExecutionEngine(std::move(M)), TM(tm), TJI(tji),
00140     JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
00141     AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
00142   setDataLayout(TM.getSubtargetImpl()->getDataLayout());
00143 
00144   Module *Mod = Modules.back().get();
00145   jitstate = new JITState(Mod);
00146 
00147   // Initialize JCE
00148   JCE = createEmitter(*this, JMM, TM);
00149 
00150   // Register in global list of all JITs.
00151   AllJits->Add(this);
00152 
00153   // Add target data
00154   MutexGuard locked(lock);
00155   FunctionPassManager &PM = jitstate->getPM();
00156   Mod->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
00157   PM.add(new DataLayoutPass(Mod));
00158 
00159   // Turn the machine code intermediate representation into bytes in memory that
00160   // may be executed.
00161   if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
00162     report_fatal_error("Target does not support machine code emission!");
00163   }
00164 
00165   // Initialize passes.
00166   PM.doInitialization();
00167 }
00168 
00169 JIT::~JIT() {
00170   // Cleanup.
00171   AllJits->Remove(this);
00172   delete jitstate;
00173   delete JCE;
00174   // JMM is a ownership of JCE, so we no need delete JMM here.
00175   delete &TM;
00176 }
00177 
00178 /// Add a new Module to the JIT. If we previously removed the last Module, we
00179 /// need re-initialize jitstate with a valid Module.
00180 void JIT::addModule(std::unique_ptr<Module> M) {
00181   MutexGuard locked(lock);
00182 
00183   if (Modules.empty()) {
00184     assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
00185 
00186     jitstate = new JITState(M.get());
00187 
00188     FunctionPassManager &PM = jitstate->getPM();
00189     M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
00190     PM.add(new DataLayoutPass(M.get()));
00191 
00192     // Turn the machine code intermediate representation into bytes in memory
00193     // that may be executed.
00194     if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
00195       report_fatal_error("Target does not support machine code emission!");
00196     }
00197 
00198     // Initialize passes.
00199     PM.doInitialization();
00200   }
00201 
00202   ExecutionEngine::addModule(std::move(M));
00203 }
00204 
00205 ///  If we are removing the last Module, invalidate the jitstate since the
00206 ///  PassManager it contains references a released Module.
00207 bool JIT::removeModule(Module *M) {
00208   bool result = ExecutionEngine::removeModule(M);
00209 
00210   MutexGuard locked(lock);
00211 
00212   if (jitstate && jitstate->getModule() == M) {
00213     delete jitstate;
00214     jitstate = nullptr;
00215   }
00216 
00217   if (!jitstate && !Modules.empty()) {
00218     jitstate = new JITState(Modules[0].get());
00219 
00220     FunctionPassManager &PM = jitstate->getPM();
00221     M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
00222     PM.add(new DataLayoutPass(M));
00223 
00224     // Turn the machine code intermediate representation into bytes in memory
00225     // that may be executed.
00226     if (TM.addPassesToEmitMachineCode(PM, *JCE, !getVerifyModules())) {
00227       report_fatal_error("Target does not support machine code emission!");
00228     }
00229 
00230     // Initialize passes.
00231     PM.doInitialization();
00232   }
00233   return result;
00234 }
00235 
00236 /// run - Start execution with the specified function and arguments.
00237 ///
00238 GenericValue JIT::runFunction(Function *F,
00239                               const std::vector<GenericValue> &ArgValues) {
00240   assert(F && "Function *F was null at entry to run()");
00241 
00242   void *FPtr = getPointerToFunction(F);
00243   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
00244   FunctionType *FTy = F->getFunctionType();
00245   Type *RetTy = FTy->getReturnType();
00246 
00247   assert((FTy->getNumParams() == ArgValues.size() ||
00248           (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
00249          "Wrong number of arguments passed into function!");
00250   assert(FTy->getNumParams() == ArgValues.size() &&
00251          "This doesn't support passing arguments through varargs (yet)!");
00252 
00253   // Handle some common cases first.  These cases correspond to common `main'
00254   // prototypes.
00255   if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
00256     switch (ArgValues.size()) {
00257     case 3:
00258       if (FTy->getParamType(0)->isIntegerTy(32) &&
00259           FTy->getParamType(1)->isPointerTy() &&
00260           FTy->getParamType(2)->isPointerTy()) {
00261         int (*PF)(int, char **, const char **) =
00262           (int(*)(int, char **, const char **))(intptr_t)FPtr;
00263 
00264         // Call the function.
00265         GenericValue rv;
00266         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
00267                                  (char **)GVTOP(ArgValues[1]),
00268                                  (const char **)GVTOP(ArgValues[2])));
00269         return rv;
00270       }
00271       break;
00272     case 2:
00273       if (FTy->getParamType(0)->isIntegerTy(32) &&
00274           FTy->getParamType(1)->isPointerTy()) {
00275         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
00276 
00277         // Call the function.
00278         GenericValue rv;
00279         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
00280                                  (char **)GVTOP(ArgValues[1])));
00281         return rv;
00282       }
00283       break;
00284     case 1:
00285       if (FTy->getParamType(0)->isIntegerTy(32)) {
00286         GenericValue rv;
00287         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
00288         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
00289         return rv;
00290       }
00291       if (FTy->getParamType(0)->isPointerTy()) {
00292         GenericValue rv;
00293         int (*PF)(char *) = (int(*)(char *))(intptr_t)FPtr;
00294         rv.IntVal = APInt(32, PF((char*)GVTOP(ArgValues[0])));
00295         return rv;
00296       }
00297       break;
00298     }
00299   }
00300 
00301   // Handle cases where no arguments are passed first.
00302   if (ArgValues.empty()) {
00303     GenericValue rv;
00304     switch (RetTy->getTypeID()) {
00305     default: llvm_unreachable("Unknown return type for function call!");
00306     case Type::IntegerTyID: {
00307       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
00308       if (BitWidth == 1)
00309         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
00310       else if (BitWidth <= 8)
00311         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
00312       else if (BitWidth <= 16)
00313         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
00314       else if (BitWidth <= 32)
00315         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
00316       else if (BitWidth <= 64)
00317         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
00318       else
00319         llvm_unreachable("Integer types > 64 bits not supported");
00320       return rv;
00321     }
00322     case Type::VoidTyID:
00323       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
00324       return rv;
00325     case Type::FloatTyID:
00326       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
00327       return rv;
00328     case Type::DoubleTyID:
00329       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
00330       return rv;
00331     case Type::X86_FP80TyID:
00332     case Type::FP128TyID:
00333     case Type::PPC_FP128TyID:
00334       llvm_unreachable("long double not supported yet");
00335     case Type::PointerTyID:
00336       return PTOGV(((void*(*)())(intptr_t)FPtr)());
00337     }
00338   }
00339 
00340   // Okay, this is not one of our quick and easy cases.  Because we don't have a
00341   // full FFI, we have to codegen a nullary stub function that just calls the
00342   // function we are interested in, passing in constants for all of the
00343   // arguments.  Make this function and return.
00344 
00345   // First, create the function.
00346   FunctionType *STy=FunctionType::get(RetTy, false);
00347   Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
00348                                     F->getParent());
00349 
00350   // Insert a basic block.
00351   BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
00352 
00353   // Convert all of the GenericValue arguments over to constants.  Note that we
00354   // currently don't support varargs.
00355   SmallVector<Value*, 8> Args;
00356   for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
00357     Constant *C = nullptr;
00358     Type *ArgTy = FTy->getParamType(i);
00359     const GenericValue &AV = ArgValues[i];
00360     switch (ArgTy->getTypeID()) {
00361     default: llvm_unreachable("Unknown argument type for function call!");
00362     case Type::IntegerTyID:
00363         C = ConstantInt::get(F->getContext(), AV.IntVal);
00364         break;
00365     case Type::FloatTyID:
00366         C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
00367         break;
00368     case Type::DoubleTyID:
00369         C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
00370         break;
00371     case Type::PPC_FP128TyID:
00372     case Type::X86_FP80TyID:
00373     case Type::FP128TyID:
00374         C = ConstantFP::get(F->getContext(), APFloat(ArgTy->getFltSemantics(),
00375                                                      AV.IntVal));
00376         break;
00377     case Type::PointerTyID:
00378       void *ArgPtr = GVTOP(AV);
00379       if (sizeof(void*) == 4)
00380         C = ConstantInt::get(Type::getInt32Ty(F->getContext()),
00381                              (int)(intptr_t)ArgPtr);
00382       else
00383         C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
00384                              (intptr_t)ArgPtr);
00385       // Cast the integer to pointer
00386       C = ConstantExpr::getIntToPtr(C, ArgTy);
00387       break;
00388     }
00389     Args.push_back(C);
00390   }
00391 
00392   CallInst *TheCall = CallInst::Create(F, Args, "", StubBB);
00393   TheCall->setCallingConv(F->getCallingConv());
00394   TheCall->setTailCall();
00395   if (!TheCall->getType()->isVoidTy())
00396     // Return result of the call.
00397     ReturnInst::Create(F->getContext(), TheCall, StubBB);
00398   else
00399     ReturnInst::Create(F->getContext(), StubBB);           // Just return void.
00400 
00401   // Finally, call our nullary stub function.
00402   GenericValue Result = runFunction(Stub, std::vector<GenericValue>());
00403   // Erase it, since no other function can have a reference to it.
00404   Stub->eraseFromParent();
00405   // And return the result.
00406   return Result;
00407 }
00408 
00409 void JIT::RegisterJITEventListener(JITEventListener *L) {
00410   if (!L)
00411     return;
00412   MutexGuard locked(lock);
00413   EventListeners.push_back(L);
00414 }
00415 void JIT::UnregisterJITEventListener(JITEventListener *L) {
00416   if (!L)
00417     return;
00418   MutexGuard locked(lock);
00419   std::vector<JITEventListener*>::reverse_iterator I=
00420       std::find(EventListeners.rbegin(), EventListeners.rend(), L);
00421   if (I != EventListeners.rend()) {
00422     std::swap(*I, EventListeners.back());
00423     EventListeners.pop_back();
00424   }
00425 }
00426 void JIT::NotifyFunctionEmitted(
00427     const Function &F,
00428     void *Code, size_t Size,
00429     const JITEvent_EmittedFunctionDetails &Details) {
00430   MutexGuard locked(lock);
00431   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
00432     EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
00433   }
00434 }
00435 
00436 void JIT::NotifyFreeingMachineCode(void *OldPtr) {
00437   MutexGuard locked(lock);
00438   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
00439     EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
00440   }
00441 }
00442 
00443 /// runJITOnFunction - Run the FunctionPassManager full of
00444 /// just-in-time compilation passes on F, hopefully filling in
00445 /// GlobalAddress[F] with the address of F's machine code.
00446 ///
00447 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
00448   MutexGuard locked(lock);
00449 
00450   class MCIListener : public JITEventListener {
00451     MachineCodeInfo *const MCI;
00452    public:
00453     MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
00454     void NotifyFunctionEmitted(const Function &, void *Code, size_t Size,
00455                                const EmittedFunctionDetails &) override {
00456       MCI->setAddress(Code);
00457       MCI->setSize(Size);
00458     }
00459   };
00460   MCIListener MCIL(MCI);
00461   if (MCI)
00462     RegisterJITEventListener(&MCIL);
00463 
00464   runJITOnFunctionUnlocked(F);
00465 
00466   if (MCI)
00467     UnregisterJITEventListener(&MCIL);
00468 }
00469 
00470 void JIT::runJITOnFunctionUnlocked(Function *F) {
00471   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
00472 
00473   jitTheFunctionUnlocked(F);
00474 
00475   // If the function referred to another function that had not yet been
00476   // read from bitcode, and we are jitting non-lazily, emit it now.
00477   while (!jitstate->getPendingFunctions().empty()) {
00478     Function *PF = jitstate->getPendingFunctions().back();
00479     jitstate->getPendingFunctions().pop_back();
00480 
00481     assert(!PF->hasAvailableExternallyLinkage() &&
00482            "Externally-defined function should not be in pending list.");
00483 
00484     jitTheFunctionUnlocked(PF);
00485 
00486     // Now that the function has been jitted, ask the JITEmitter to rewrite
00487     // the stub with real address of the function.
00488     updateFunctionStubUnlocked(PF);
00489   }
00490 }
00491 
00492 void JIT::jitTheFunctionUnlocked(Function *F) {
00493   isAlreadyCodeGenerating = true;
00494   jitstate->getPM().run(*F);
00495   isAlreadyCodeGenerating = false;
00496 
00497   // clear basic block addresses after this function is done
00498   getBasicBlockAddressMap().clear();
00499 }
00500 
00501 /// getPointerToFunction - This method is used to get the address of the
00502 /// specified function, compiling it if necessary.
00503 ///
00504 void *JIT::getPointerToFunction(Function *F) {
00505 
00506   if (void *Addr = getPointerToGlobalIfAvailable(F))
00507     return Addr;   // Check if function already code gen'd
00508 
00509   MutexGuard locked(lock);
00510 
00511   // Now that this thread owns the lock, make sure we read in the function if it
00512   // exists in this Module.
00513   std::string ErrorMsg;
00514   if (F->Materialize(&ErrorMsg)) {
00515     report_fatal_error("Error reading function '" + F->getName()+
00516                       "' from bitcode file: " + ErrorMsg);
00517   }
00518 
00519   // ... and check if another thread has already code gen'd the function.
00520   if (void *Addr = getPointerToGlobalIfAvailable(F))
00521     return Addr;
00522 
00523   if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
00524     bool AbortOnFailure = !F->hasExternalWeakLinkage();
00525     void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
00526     addGlobalMapping(F, Addr);
00527     return Addr;
00528   }
00529 
00530   runJITOnFunctionUnlocked(F);
00531 
00532   void *Addr = getPointerToGlobalIfAvailable(F);
00533   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00534   return Addr;
00535 }
00536 
00537 void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
00538   MutexGuard locked(lock);
00539 
00540   BasicBlockAddressMapTy::iterator I =
00541     getBasicBlockAddressMap().find(BB);
00542   if (I == getBasicBlockAddressMap().end()) {
00543     getBasicBlockAddressMap()[BB] = Addr;
00544   } else {
00545     // ignore repeats: some BBs can be split into few MBBs?
00546   }
00547 }
00548 
00549 void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
00550   MutexGuard locked(lock);
00551   getBasicBlockAddressMap().erase(BB);
00552 }
00553 
00554 void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
00555   // make sure it's function is compiled by JIT
00556   (void)getPointerToFunction(BB->getParent());
00557 
00558   // resolve basic block address
00559   MutexGuard locked(lock);
00560 
00561   BasicBlockAddressMapTy::iterator I =
00562     getBasicBlockAddressMap().find(BB);
00563   if (I != getBasicBlockAddressMap().end()) {
00564     return I->second;
00565   } else {
00566     llvm_unreachable("JIT does not have BB address for address-of-label, was"
00567                      " it eliminated by optimizer?");
00568   }
00569 }
00570 
00571 void *JIT::getPointerToNamedFunction(const std::string &Name,
00572                                      bool AbortOnFailure){
00573   if (!isSymbolSearchingDisabled()) {
00574     void *ptr = JMM->getPointerToNamedFunction(Name, false);
00575     if (ptr)
00576       return ptr;
00577   }
00578 
00579   /// If a LazyFunctionCreator is installed, use it to get/create the function.
00580   if (LazyFunctionCreator)
00581     if (void *RP = LazyFunctionCreator(Name))
00582       return RP;
00583 
00584   if (AbortOnFailure) {
00585     report_fatal_error("Program used external function '"+Name+
00586                       "' which could not be resolved!");
00587   }
00588   return nullptr;
00589 }
00590 
00591 
00592 /// getOrEmitGlobalVariable - Return the address of the specified global
00593 /// variable, possibly emitting it to memory if needed.  This is used by the
00594 /// Emitter.
00595 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
00596   MutexGuard locked(lock);
00597 
00598   void *Ptr = getPointerToGlobalIfAvailable(GV);
00599   if (Ptr) return Ptr;
00600 
00601   // If the global is external, just remember the address.
00602   if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) {
00603 #if HAVE___DSO_HANDLE
00604     if (GV->getName() == "__dso_handle")
00605       return (void*)&__dso_handle;
00606 #endif
00607     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
00608     if (!Ptr) {
00609       report_fatal_error("Could not resolve external global address: "
00610                         +GV->getName());
00611     }
00612     addGlobalMapping(GV, Ptr);
00613   } else {
00614     // If the global hasn't been emitted to memory yet, allocate space and
00615     // emit it into memory.
00616     Ptr = getMemoryForGV(GV);
00617     addGlobalMapping(GV, Ptr);
00618     EmitGlobalVariable(GV);  // Initialize the variable.
00619   }
00620   return Ptr;
00621 }
00622 
00623 /// recompileAndRelinkFunction - This method is used to force a function
00624 /// which has already been compiled, to be compiled again, possibly
00625 /// after it has been modified. Then the entry to the old copy is overwritten
00626 /// with a branch to the new copy. If there was no old copy, this acts
00627 /// just like JIT::getPointerToFunction().
00628 ///
00629 void *JIT::recompileAndRelinkFunction(Function *F) {
00630   void *OldAddr = getPointerToGlobalIfAvailable(F);
00631 
00632   // If it's not already compiled there is no reason to patch it up.
00633   if (!OldAddr) return getPointerToFunction(F);
00634 
00635   // Delete the old function mapping.
00636   addGlobalMapping(F, nullptr);
00637 
00638   // Recodegen the function
00639   runJITOnFunction(F);
00640 
00641   // Update state, forward the old function to the new function.
00642   void *Addr = getPointerToGlobalIfAvailable(F);
00643   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00644   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
00645   return Addr;
00646 }
00647 
00648 /// getMemoryForGV - This method abstracts memory allocation of global
00649 /// variable so that the JIT can allocate thread local variables depending
00650 /// on the target.
00651 ///
00652 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
00653   char *Ptr;
00654 
00655   // GlobalVariable's which are not "constant" will cause trouble in a server
00656   // situation. It's returned in the same block of memory as code which may
00657   // not be writable.
00658   if (isGVCompilationDisabled() && !GV->isConstant()) {
00659     report_fatal_error("Compilation of non-internal GlobalValue is disabled!");
00660   }
00661 
00662   // Some applications require globals and code to live together, so they may
00663   // be allocated into the same buffer, but in general globals are allocated
00664   // through the memory manager which puts them near the code but not in the
00665   // same buffer.
00666   Type *GlobalType = GV->getType()->getElementType();
00667   size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
00668   size_t A = getDataLayout()->getPreferredAlignment(GV);
00669   if (GV->isThreadLocal()) {
00670     MutexGuard locked(lock);
00671     Ptr = TJI.allocateThreadLocalMemory(S);
00672   } else if (TJI.allocateSeparateGVMemory()) {
00673     if (A <= 8) {
00674       Ptr = (char*)malloc(S);
00675     } else {
00676       // Allocate S+A bytes of memory, then use an aligned pointer within that
00677       // space.
00678       Ptr = (char*)malloc(S+A);
00679       unsigned MisAligned = ((intptr_t)Ptr & (A-1));
00680       Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
00681     }
00682   } else if (AllocateGVsWithCode) {
00683     Ptr = (char*)JCE->allocateSpace(S, A);
00684   } else {
00685     Ptr = (char*)JCE->allocateGlobal(S, A);
00686   }
00687   return Ptr;
00688 }
00689 
00690 void JIT::addPendingFunction(Function *F) {
00691   MutexGuard locked(lock);
00692   jitstate->getPendingFunctions().push_back(F);
00693 }
00694 
00695 
00696 JITEventListener::~JITEventListener() {}