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