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