LCOV - code coverage report
Current view: top level - lib/ExecutionEngine - ExecutionEngine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 408 719 56.7 %
Date: 2017-09-14 15:23:50 Functions: 35 46 76.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the common interface used by the various execution engine
      11             : // subclasses.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/ExecutionEngine/ExecutionEngine.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/ADT/SmallString.h"
      18             : #include "llvm/ADT/Statistic.h"
      19             : #include "llvm/ExecutionEngine/GenericValue.h"
      20             : #include "llvm/ExecutionEngine/JITEventListener.h"
      21             : #include "llvm/ExecutionEngine/ObjectCache.h"
      22             : #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
      23             : #include "llvm/IR/Constants.h"
      24             : #include "llvm/IR/DataLayout.h"
      25             : #include "llvm/IR/DerivedTypes.h"
      26             : #include "llvm/IR/Mangler.h"
      27             : #include "llvm/IR/Module.h"
      28             : #include "llvm/IR/Operator.h"
      29             : #include "llvm/IR/ValueHandle.h"
      30             : #include "llvm/Object/Archive.h"
      31             : #include "llvm/Object/ObjectFile.h"
      32             : #include "llvm/Support/Debug.h"
      33             : #include "llvm/Support/DynamicLibrary.h"
      34             : #include "llvm/Support/ErrorHandling.h"
      35             : #include "llvm/Support/Host.h"
      36             : #include "llvm/Support/MutexGuard.h"
      37             : #include "llvm/Support/TargetRegistry.h"
      38             : #include "llvm/Support/raw_ostream.h"
      39             : #include "llvm/Target/TargetMachine.h"
      40             : #include <cmath>
      41             : #include <cstring>
      42             : using namespace llvm;
      43             : 
      44             : #define DEBUG_TYPE "jit"
      45             : 
      46             : STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
      47             : STATISTIC(NumGlobals  , "Number of global vars initialized");
      48             : 
      49             : ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
      50             :     std::unique_ptr<Module> M, std::string *ErrorStr,
      51             :     std::shared_ptr<MCJITMemoryManager> MemMgr,
      52             : 
      53             :     std::shared_ptr<JITSymbolResolver> Resolver,
      54             :     std::unique_ptr<TargetMachine> TM) = nullptr;
      55             : 
      56             : ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
      57             :   std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
      58             :   std::shared_ptr<JITSymbolResolver> Resolver,
      59             :   std::unique_ptr<TargetMachine> TM) = nullptr;
      60             : 
      61             : ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
      62             :                                                 std::string *ErrorStr) =nullptr;
      63             : 
      64           0 : void JITEventListener::anchor() {}
      65             : 
      66           0 : void ObjectCache::anchor() {}
      67             : 
      68         143 : void ExecutionEngine::Init(std::unique_ptr<Module> M) {
      69         143 :   CompilingLazily         = false;
      70         143 :   GVCompilationDisabled   = false;
      71         143 :   SymbolSearchingDisabled = false;
      72             : 
      73             :   // IR module verification is enabled by default in debug builds, and disabled
      74             :   // by default in release builds.
      75             : #ifndef NDEBUG
      76             :   VerifyModules = true;
      77             : #else
      78         143 :   VerifyModules = false;
      79             : #endif
      80             : 
      81             :   assert(M && "Module is null?");
      82         143 :   Modules.push_back(std::move(M));
      83         143 : }
      84             : 
      85          22 : ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
      86         132 :     : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
      87          44 :   Init(std::move(M));
      88          22 : }
      89             : 
      90         121 : ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
      91         605 :     : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
      92         242 :   Init(std::move(M));
      93         121 : }
      94             : 
      95         462 : ExecutionEngine::~ExecutionEngine() {
      96          77 :   clearAllGlobalMappings();
      97          77 : }
      98             : 
      99             : namespace {
     100             : /// \brief Helper class which uses a value handler to automatically deletes the
     101             : /// memory block when the GlobalVariable is destroyed.
     102           2 : class GVMemoryBlock final : public CallbackVH {
     103             :   GVMemoryBlock(const GlobalVariable *GV)
     104          20 :     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
     105             : 
     106             : public:
     107             :   /// \brief Returns the address the GlobalVariable should be written into.  The
     108             :   /// GVMemoryBlock object prefixes that.
     109          10 :   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
     110          10 :     Type *ElTy = GV->getValueType();
     111          10 :     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
     112          10 :     void *RawMemory = ::operator new(
     113          30 :         alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlignment(GV)) + GVSize);
     114          20 :     new(RawMemory) GVMemoryBlock(GV);
     115          10 :     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
     116             :   }
     117             : 
     118           1 :   void deleted() override {
     119             :     // We allocated with operator new and with some extra memory hanging off the
     120             :     // end, so don't just delete this.  I'm not sure if this is actually
     121             :     // required.
     122           1 :     this->~GVMemoryBlock();
     123           1 :     ::operator delete(this);
     124           1 :   }
     125             : };
     126             : }  // anonymous namespace
     127             : 
     128          10 : char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
     129          10 :   return GVMemoryBlock::Create(GV, getDataLayout());
     130             : }
     131             : 
     132           0 : void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
     133           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
     134             : }
     135             : 
     136             : void
     137           0 : ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
     138           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
     139             : }
     140             : 
     141           0 : void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
     142           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
     143             : }
     144             : 
     145           0 : bool ExecutionEngine::removeModule(Module *M) {
     146           0 :   for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
     147           0 :     Module *Found = I->get();
     148           0 :     if (Found == M) {
     149           0 :       I->release();
     150           0 :       Modules.erase(I);
     151           0 :       clearGlobalMappingsFromModule(M);
     152           0 :       return true;
     153             :     }
     154             :   }
     155             :   return false;
     156             : }
     157             : 
     158           0 : Function *ExecutionEngine::FindFunctionNamed(StringRef FnName) {
     159           0 :   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
     160           0 :     Function *F = Modules[i]->getFunction(FnName);
     161           0 :     if (F && !F->isDeclaration())
     162             :       return F;
     163             :   }
     164             :   return nullptr;
     165             : }
     166             : 
     167           0 : GlobalVariable *ExecutionEngine::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
     168           0 :   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
     169           0 :     GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
     170           0 :     if (GV && !GV->isDeclaration())
     171             :       return GV;
     172             :   }
     173             :   return nullptr;
     174             : }
     175             : 
     176           3 : uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
     177           3 :   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
     178             :   uint64_t OldVal;
     179             : 
     180             :   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
     181             :   // GlobalAddressMap.
     182           6 :   if (I == GlobalAddressMap.end())
     183             :     OldVal = 0;
     184             :   else {
     185           9 :     GlobalAddressReverseMap.erase(I->second);
     186           3 :     OldVal = I->second;
     187           3 :     GlobalAddressMap.erase(I);
     188             :   }
     189             : 
     190           3 :   return OldVal;
     191             : }
     192             : 
     193         126 : std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
     194             :   assert(GV->hasName() && "Global must have name.");
     195             : 
     196         378 :   MutexGuard locked(lock);
     197         252 :   SmallString<128> FullName;
     198             : 
     199             :   const DataLayout &DL =
     200         252 :     GV->getParent()->getDataLayout().isDefault()
     201         252 :       ? getDataLayout()
     202         209 :       : GV->getParent()->getDataLayout();
     203             : 
     204         252 :   Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
     205         378 :   return FullName.str();
     206             : }
     207             : 
     208          16 : void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
     209          48 :   MutexGuard locked(lock);
     210          48 :   addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
     211          16 : }
     212             : 
     213          16 : void ExecutionEngine::addGlobalMapping(StringRef Name, uint64_t Addr) {
     214          48 :   MutexGuard locked(lock);
     215             : 
     216             :   assert(!Name.empty() && "Empty GlobalMapping symbol name!");
     217             : 
     218             :   DEBUG(dbgs() << "JIT: Map \'" << Name  << "\' to [" << Addr << "]\n";);
     219          48 :   uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
     220             :   assert((!CurVal || !Addr) && "GlobalMapping already established!");
     221          16 :   CurVal = Addr;
     222             : 
     223             :   // If we are using the reverse mapping, add it too.
     224          32 :   if (!EEState.getGlobalAddressReverseMap().empty()) {
     225           0 :     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
     226             :     assert((!V.empty() || !Name.empty()) &&
     227             :            "GlobalMapping already established!");
     228           0 :     V = Name;
     229             :   }
     230          16 : }
     231             : 
     232          77 : void ExecutionEngine::clearAllGlobalMappings() {
     233         231 :   MutexGuard locked(lock);
     234             : 
     235         154 :   EEState.getGlobalAddressMap().clear();
     236         154 :   EEState.getGlobalAddressReverseMap().clear();
     237          77 : }
     238             : 
     239           1 : void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
     240           3 :   MutexGuard locked(lock);
     241             : 
     242           4 :   for (GlobalObject &GO : M->global_objects())
     243           3 :     EEState.RemoveMapping(getMangledName(&GO));
     244           1 : }
     245             : 
     246          80 : uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
     247             :                                               void *Addr) {
     248         240 :   MutexGuard locked(lock);
     249         320 :   return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
     250             : }
     251             : 
     252          80 : uint64_t ExecutionEngine::updateGlobalMapping(StringRef Name, uint64_t Addr) {
     253         240 :   MutexGuard locked(lock);
     254             : 
     255             :   ExecutionEngineState::GlobalAddressMapTy &Map =
     256         160 :     EEState.getGlobalAddressMap();
     257             : 
     258             :   // Deleting from the mapping?
     259          80 :   if (!Addr)
     260           2 :     return EEState.RemoveMapping(Name);
     261             : 
     262          78 :   uint64_t &CurVal = Map[Name];
     263          78 :   uint64_t OldVal = CurVal;
     264             : 
     265          82 :   if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
     266           2 :     EEState.getGlobalAddressReverseMap().erase(CurVal);
     267          78 :   CurVal = Addr;
     268             : 
     269             :   // If we are using the reverse mapping, add it too.
     270         156 :   if (!EEState.getGlobalAddressReverseMap().empty()) {
     271           2 :     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
     272             :     assert((!V.empty() || !Name.empty()) &&
     273             :            "GlobalMapping already established!");
     274           2 :     V = Name;
     275             :   }
     276             :   return OldVal;
     277             : }
     278             : 
     279         264 : uint64_t ExecutionEngine::getAddressToGlobalIfAvailable(StringRef S) {
     280         792 :   MutexGuard locked(lock);
     281         264 :   uint64_t Address = 0;
     282             :   ExecutionEngineState::GlobalAddressMapTy::iterator I =
     283         528 :     EEState.getGlobalAddressMap().find(S);
     284         792 :   if (I != EEState.getGlobalAddressMap().end())
     285          28 :     Address = I->second;
     286         528 :   return Address;
     287             : }
     288             : 
     289             : 
     290         264 : void *ExecutionEngine::getPointerToGlobalIfAvailable(StringRef S) {
     291         792 :   MutexGuard locked(lock);
     292         264 :   if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
     293             :     return Address;
     294         236 :   return nullptr;
     295             : }
     296             : 
     297          29 : void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
     298          87 :   MutexGuard locked(lock);
     299         116 :   return getPointerToGlobalIfAvailable(getMangledName(GV));
     300             : }
     301             : 
     302          14 : const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
     303          42 :   MutexGuard locked(lock);
     304             : 
     305             :   // If we haven't computed the reverse mapping yet, do so first.
     306          28 :   if (EEState.getGlobalAddressReverseMap().empty()) {
     307             :     for (ExecutionEngineState::GlobalAddressMapTy::iterator
     308          14 :            I = EEState.getGlobalAddressMap().begin(),
     309          27 :            E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
     310          12 :       StringRef Name = I->first();
     311           6 :       uint64_t Addr = I->second;
     312          18 :       EEState.getGlobalAddressReverseMap().insert(std::make_pair(
     313             :                                                           Addr, Name));
     314             :     }
     315             :   }
     316             : 
     317             :   std::map<uint64_t, std::string>::iterator I =
     318          28 :     EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
     319             : 
     320          28 :   if (I != EEState.getGlobalAddressReverseMap().end()) {
     321          20 :     StringRef Name = I->second;
     322          21 :     for (unsigned i = 0, e = Modules.size(); i != e; ++i)
     323          30 :       if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
     324           9 :         return GV;
     325             :   }
     326             :   return nullptr;
     327             : }
     328             : 
     329             : namespace {
     330        1522 : class ArgvArray {
     331             :   std::unique_ptr<char[]> Array;
     332             :   std::vector<std::unique_ptr<char[]>> Values;
     333             : public:
     334             :   /// Turn a vector of strings into a nice argv style array of pointers to null
     335             :   /// terminated strings.
     336             :   void *reset(LLVMContext &C, ExecutionEngine *EE,
     337             :               const std::vector<std::string> &InputArgv);
     338             : };
     339             : }  // anonymous namespace
     340          15 : void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
     341             :                        const std::vector<std::string> &InputArgv) {
     342          15 :   Values.clear();  // Free the old contents.
     343          30 :   Values.reserve(InputArgv.size());
     344          15 :   unsigned PtrSize = EE->getDataLayout().getPointerSize();
     345          75 :   Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
     346             : 
     347             :   DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
     348          15 :   Type *SBytePtr = Type::getInt8PtrTy(C);
     349             : 
     350         124 :   for (unsigned i = 0; i != InputArgv.size(); ++i) {
     351          94 :     unsigned Size = InputArgv[i].size()+1;
     352         141 :     auto Dest = make_unique<char[]>(Size);
     353             :     DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
     354             : 
     355         235 :     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
     356          94 :     Dest[Size-1] = 0;
     357             : 
     358             :     // Endian safe: Array[i] = (PointerTy)Dest;
     359          94 :     EE->StoreValueToMemory(PTOGV(Dest.get()),
     360          94 :                            (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
     361          94 :     Values.push_back(std::move(Dest));
     362             :   }
     363             : 
     364             :   // Null terminate it
     365          15 :   EE->StoreValueToMemory(PTOGV(nullptr),
     366          30 :                          (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
     367             :                          SBytePtr);
     368          30 :   return Array.get();
     369             : }
     370             : 
     371         344 : void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
     372             :                                                        bool isDtors) {
     373         688 :   StringRef Name(isDtors ? "llvm.global_dtors" : "llvm.global_ctors");
     374         344 :   GlobalVariable *GV = module.getNamedGlobal(Name);
     375             : 
     376             :   // If this global has internal linkage, or if it has a use, then it must be
     377             :   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
     378             :   // this is the case, don't execute any of the global ctors, __main will do
     379             :   // it.
     380         348 :   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
     381             : 
     382             :   // Should be an array of '{ i32, void ()* }' structs.  The first value is
     383             :   // the init priority, which we ignore.
     384           8 :   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
     385             :   if (!InitList)
     386             :     return;
     387          12 :   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
     388          12 :     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
     389           0 :     if (!CS) continue;
     390             : 
     391           8 :     Constant *FP = CS->getOperand(1);
     392           4 :     if (FP->isNullValue())
     393           0 :       continue;  // Found a sentinal value, ignore.
     394             : 
     395             :     // Strip off constant expression casts.
     396           0 :     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
     397           0 :       if (CE->isCast())
     398             :         FP = CE->getOperand(0);
     399             : 
     400             :     // Execute the ctor/dtor function!
     401           4 :     if (Function *F = dyn_cast<Function>(FP))
     402           8 :       runFunction(F, None);
     403             : 
     404             :     // FIXME: It is marginally lame that we just do nothing here if we see an
     405             :     // entry we don't recognize. It might not be unreasonable for the verifier
     406             :     // to not even allow this and just assert here.
     407             :   }
     408             : }
     409             : 
     410          34 : void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
     411             :   // Execute global ctors/dtors for each module in the program.
     412         136 :   for (std::unique_ptr<Module> &M : Modules)
     413          68 :     runStaticConstructorsDestructors(*M, isDtors);
     414          34 : }
     415             : 
     416             : #ifndef NDEBUG
     417             : /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
     418             : static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
     419             :   unsigned PtrSize = EE->getDataLayout().getPointerSize();
     420             :   for (unsigned i = 0; i < PtrSize; ++i)
     421             :     if (*(i + (uint8_t*)Loc))
     422             :       return false;
     423             :   return true;
     424             : }
     425             : #endif
     426             : 
     427         153 : int ExecutionEngine::runFunctionAsMain(Function *Fn,
     428             :                                        const std::vector<std::string> &argv,
     429             :                                        const char * const * envp) {
     430         304 :   std::vector<GenericValue> GVArgs;
     431         304 :   GenericValue GVArgc;
     432         765 :   GVArgc.IntVal = APInt(32, argv.size());
     433             : 
     434             :   // Check main() type
     435         306 :   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
     436         153 :   FunctionType *FTy = Fn->getFunctionType();
     437         153 :   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
     438             : 
     439             :   // Check the argument types.
     440         153 :   if (NumArgs > 3)
     441           0 :     report_fatal_error("Invalid number of arguments of main() supplied");
     442         155 :   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
     443           0 :     report_fatal_error("Invalid type for third argument of main() supplied");
     444         166 :   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
     445           0 :     report_fatal_error("Invalid type for second argument of main() supplied");
     446         187 :   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
     447           0 :     report_fatal_error("Invalid type for first argument of main() supplied");
     448         306 :   if (!FTy->getReturnType()->isIntegerTy() &&
     449           0 :       !FTy->getReturnType()->isVoidTy())
     450           0 :     report_fatal_error("Invalid return type of main() supplied");
     451             : 
     452         304 :   ArgvArray CArgv;
     453         304 :   ArgvArray CEnv;
     454         153 :   if (NumArgs) {
     455          17 :     GVArgs.push_back(GVArgc); // Arg #0 = argc.
     456          17 :     if (NumArgs > 1) {
     457             :       // Arg #1 = argv.
     458          39 :       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
     459             :       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
     460             :              "argv[0] was null after CreateArgv");
     461          13 :       if (NumArgs > 2) {
     462           4 :         std::vector<std::string> EnvVars;
     463          34 :         for (unsigned i = 0; envp[i]; ++i)
     464          32 :           EnvVars.emplace_back(envp[i]);
     465             :         // Arg #2 = envp.
     466           6 :         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
     467             :       }
     468             :     }
     469             :   }
     470             : 
     471         608 :   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
     472             : }
     473             : 
     474          42 : EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
     475             : 
     476         253 : EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
     477         253 :     : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
     478             :       OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
     479        2277 :       UseOrcMCJITReplacement(false) {
     480             : // IR module verification is enabled by default in debug builds, and disabled
     481             : // by default in release builds.
     482             : #ifndef NDEBUG
     483             :   VerifyModules = true;
     484             : #else
     485         253 :   VerifyModules = false;
     486             : #endif
     487         253 : }
     488             : 
     489             : EngineBuilder::~EngineBuilder() = default;
     490             : 
     491         205 : EngineBuilder &EngineBuilder::setMCJITMemoryManager(
     492             :                                    std::unique_ptr<RTDyldMemoryManager> mcjmm) {
     493         615 :   auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
     494         410 :   MemMgr = SharedMM;
     495         410 :   Resolver = SharedMM;
     496         410 :   return *this;
     497             : }
     498             : 
     499             : EngineBuilder&
     500           0 : EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
     501           0 :   MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
     502           0 :   return *this;
     503             : }
     504             : 
     505             : EngineBuilder&
     506           0 : EngineBuilder::setSymbolResolver(std::unique_ptr<JITSymbolResolver> SR) {
     507           0 :   Resolver = std::shared_ptr<JITSymbolResolver>(std::move(SR));
     508           0 :   return *this;
     509             : }
     510             : 
     511         232 : ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
     512         464 :   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
     513             : 
     514             :   // Make sure we can resolve symbols in the program as well. The zero arg
     515             :   // to the function tells DynamicLibrary to load the program, not a library.
     516         464 :   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
     517             :     return nullptr;
     518             : 
     519             :   // If the user specified a memory manager but didn't specify which engine to
     520             :   // create, we assume they only want the JIT, and we fail if they only want
     521             :   // the interpreter.
     522         232 :   if (MemMgr) {
     523         205 :     if (WhichEngine & EngineKind::JIT)
     524         205 :       WhichEngine = EngineKind::JIT;
     525             :     else {
     526           0 :       if (ErrorStr)
     527           0 :         *ErrorStr = "Cannot create an interpreter with a memory manager.";
     528             :       return nullptr;
     529             :     }
     530             :   }
     531             : 
     532             :   // Unless the interpreter was explicitly selected or the JIT is not linked,
     533             :   // try making a JIT.
     534         447 :   if ((WhichEngine & EngineKind::JIT) && TheTM) {
     535         840 :     Triple TT(M->getTargetTriple());
     536         210 :     if (!TM->getTarget().hasJIT()) {
     537           0 :       errs() << "WARNING: This target JIT is not designed for the host"
     538           0 :              << " you are running.  If bad things happen, please choose"
     539           0 :              << " a different -march switch.\n";
     540             :     }
     541             : 
     542         210 :     ExecutionEngine *EE = nullptr;
     543         210 :     if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
     544         623 :       EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
     545          89 :                                                     std::move(Resolver),
     546          89 :                                                     std::move(TheTM));
     547         178 :       EE->addModule(std::move(M));
     548         121 :     } else if (ExecutionEngine::MCJITCtor)
     549         968 :       EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
     550         242 :                                       std::move(Resolver), std::move(TheTM));
     551             : 
     552         210 :     if (EE) {
     553         420 :       EE->setVerifyModules(VerifyModules);
     554         420 :       return EE;
     555             :     }
     556             :   }
     557             : 
     558             :   // If we can't make a JIT and we didn't request one specifically, try making
     559             :   // an interpreter instead.
     560          22 :   if (WhichEngine & EngineKind::Interpreter) {
     561          22 :     if (ExecutionEngine::InterpCtor)
     562          44 :       return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
     563           0 :     if (ErrorStr)
     564           0 :       *ErrorStr = "Interpreter has not been linked in.";
     565             :     return nullptr;
     566             :   }
     567             : 
     568           0 :   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
     569           0 :     if (ErrorStr)
     570           0 :       *ErrorStr = "JIT has not been linked in.";
     571             :   }
     572             : 
     573             :   return nullptr;
     574             : }
     575             : 
     576          17 : void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
     577           6 :   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
     578           6 :     return getPointerToFunction(F);
     579             : 
     580          22 :   MutexGuard locked(lock);
     581          11 :   if (void* P = getPointerToGlobalIfAvailable(GV))
     582             :     return P;
     583             : 
     584             :   // Global variable might have been added since interpreter started.
     585             :   if (GlobalVariable *GVar =
     586           1 :           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
     587           1 :     EmitGlobalVariable(GVar);
     588             :   else
     589           0 :     llvm_unreachable("Global hasn't had an address allocated yet!");
     590             : 
     591           1 :   return getPointerToGlobalIfAvailable(GV);
     592             : }
     593             : 
     594             : /// \brief Converts a Constant* into a GenericValue, including handling of
     595             : /// ConstantExpr values.
     596         673 : GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
     597             :   // If its undefined, return the garbage.
     598        1346 :   if (isa<UndefValue>(C)) {
     599          70 :     GenericValue Result;
     600          70 :     switch (C->getType()->getTypeID()) {
     601             :     default:
     602             :       break;
     603           1 :     case Type::IntegerTyID:
     604             :     case Type::X86_FP80TyID:
     605             :     case Type::FP128TyID:
     606             :     case Type::PPC_FP128TyID:
     607             :       // Although the value is undefined, we still have to construct an APInt
     608             :       // with the correct bit width.
     609           4 :       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
     610           1 :       break;
     611           2 :     case Type::StructTyID: {
     612             :       // if the whole struct is 'undef' just reserve memory for the value.
     613           4 :       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
     614           2 :         unsigned int elemNum = STy->getNumElements();
     615           2 :         Result.AggregateVal.resize(elemNum);
     616           6 :         for (unsigned int i = 0; i < elemNum; ++i) {
     617           8 :           Type *ElemTy = STy->getElementType(i);
     618           4 :           if (ElemTy->isIntegerTy())
     619           3 :             Result.AggregateVal[i].IntVal =
     620           4 :               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
     621             :           else if (ElemTy->isAggregateType()) {
     622           1 :               const Constant *ElemUndef = UndefValue::get(ElemTy);
     623           2 :               Result.AggregateVal[i] = getConstantValue(ElemUndef);
     624             :             }
     625             :           }
     626             :         }
     627             :       }
     628             :       break;
     629          30 :     case Type::VectorTyID:
     630             :       // if the whole vector is 'undef' just reserve memory for the value.
     631          60 :       auto* VTy = dyn_cast<VectorType>(C->getType());
     632          30 :       Type *ElemTy = VTy->getElementType();
     633          30 :       unsigned int elemNum = VTy->getNumElements();
     634          30 :       Result.AggregateVal.resize(elemNum);
     635          30 :       if (ElemTy->isIntegerTy())
     636         284 :         for (unsigned int i = 0; i < elemNum; ++i)
     637         396 :           Result.AggregateVal[i].IntVal =
     638         528 :             APInt(ElemTy->getPrimitiveSizeInBits(), 0);
     639             :       break;
     640             :     }
     641          35 :     return Result;
     642             :   }
     643             : 
     644             :   // Otherwise, if the value is a ConstantExpr...
     645         638 :   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
     646           0 :     Constant *Op0 = CE->getOperand(0);
     647           0 :     switch (CE->getOpcode()) {
     648           0 :     case Instruction::GetElementPtr: {
     649             :       // Compute the index
     650           0 :       GenericValue Result = getConstantValue(Op0);
     651           0 :       APInt Offset(DL.getPointerSizeInBits(), 0);
     652           0 :       cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
     653             : 
     654           0 :       char* tmp = (char*) Result.PointerVal;
     655           0 :       Result = PTOGV(tmp + Offset.getSExtValue());
     656           0 :       return Result;
     657             :     }
     658           0 :     case Instruction::Trunc: {
     659           0 :       GenericValue GV = getConstantValue(Op0);
     660           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     661           0 :       GV.IntVal = GV.IntVal.trunc(BitWidth);
     662           0 :       return GV;
     663             :     }
     664           0 :     case Instruction::ZExt: {
     665           0 :       GenericValue GV = getConstantValue(Op0);
     666           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     667           0 :       GV.IntVal = GV.IntVal.zext(BitWidth);
     668           0 :       return GV;
     669             :     }
     670           0 :     case Instruction::SExt: {
     671           0 :       GenericValue GV = getConstantValue(Op0);
     672           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     673           0 :       GV.IntVal = GV.IntVal.sext(BitWidth);
     674           0 :       return GV;
     675             :     }
     676           0 :     case Instruction::FPTrunc: {
     677             :       // FIXME long double
     678           0 :       GenericValue GV = getConstantValue(Op0);
     679           0 :       GV.FloatVal = float(GV.DoubleVal);
     680           0 :       return GV;
     681             :     }
     682           0 :     case Instruction::FPExt:{
     683             :       // FIXME long double
     684           0 :       GenericValue GV = getConstantValue(Op0);
     685           0 :       GV.DoubleVal = double(GV.FloatVal);
     686           0 :       return GV;
     687             :     }
     688           0 :     case Instruction::UIToFP: {
     689           0 :       GenericValue GV = getConstantValue(Op0);
     690           0 :       if (CE->getType()->isFloatTy())
     691           0 :         GV.FloatVal = float(GV.IntVal.roundToDouble());
     692           0 :       else if (CE->getType()->isDoubleTy())
     693           0 :         GV.DoubleVal = GV.IntVal.roundToDouble();
     694           0 :       else if (CE->getType()->isX86_FP80Ty()) {
     695           0 :         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
     696           0 :         (void)apf.convertFromAPInt(GV.IntVal,
     697             :                                    false,
     698             :                                    APFloat::rmNearestTiesToEven);
     699           0 :         GV.IntVal = apf.bitcastToAPInt();
     700             :       }
     701           0 :       return GV;
     702             :     }
     703           0 :     case Instruction::SIToFP: {
     704           0 :       GenericValue GV = getConstantValue(Op0);
     705           0 :       if (CE->getType()->isFloatTy())
     706           0 :         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
     707           0 :       else if (CE->getType()->isDoubleTy())
     708           0 :         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
     709           0 :       else if (CE->getType()->isX86_FP80Ty()) {
     710           0 :         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
     711           0 :         (void)apf.convertFromAPInt(GV.IntVal,
     712             :                                    true,
     713             :                                    APFloat::rmNearestTiesToEven);
     714           0 :         GV.IntVal = apf.bitcastToAPInt();
     715             :       }
     716           0 :       return GV;
     717             :     }
     718           0 :     case Instruction::FPToUI: // double->APInt conversion handles sign
     719             :     case Instruction::FPToSI: {
     720           0 :       GenericValue GV = getConstantValue(Op0);
     721           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     722           0 :       if (Op0->getType()->isFloatTy())
     723           0 :         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
     724           0 :       else if (Op0->getType()->isDoubleTy())
     725           0 :         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
     726           0 :       else if (Op0->getType()->isX86_FP80Ty()) {
     727           0 :         APFloat apf = APFloat(APFloat::x87DoubleExtended(), GV.IntVal);
     728             :         uint64_t v;
     729             :         bool ignored;
     730           0 :         (void)apf.convertToInteger(makeMutableArrayRef(v), BitWidth,
     731           0 :                                    CE->getOpcode()==Instruction::FPToSI,
     732             :                                    APFloat::rmTowardZero, &ignored);
     733           0 :         GV.IntVal = v; // endian?
     734             :       }
     735           0 :       return GV;
     736             :     }
     737           0 :     case Instruction::PtrToInt: {
     738           0 :       GenericValue GV = getConstantValue(Op0);
     739           0 :       uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
     740             :       assert(PtrWidth <= 64 && "Bad pointer width");
     741           0 :       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
     742           0 :       uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
     743           0 :       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
     744           0 :       return GV;
     745             :     }
     746           0 :     case Instruction::IntToPtr: {
     747           0 :       GenericValue GV = getConstantValue(Op0);
     748           0 :       uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
     749           0 :       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
     750             :       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
     751           0 :       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
     752           0 :       return GV;
     753             :     }
     754           0 :     case Instruction::BitCast: {
     755           0 :       GenericValue GV = getConstantValue(Op0);
     756           0 :       Type* DestTy = CE->getType();
     757           0 :       switch (Op0->getType()->getTypeID()) {
     758           0 :         default: llvm_unreachable("Invalid bitcast operand");
     759           0 :         case Type::IntegerTyID:
     760             :           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
     761           0 :           if (DestTy->isFloatTy())
     762           0 :             GV.FloatVal = GV.IntVal.bitsToFloat();
     763           0 :           else if (DestTy->isDoubleTy())
     764           0 :             GV.DoubleVal = GV.IntVal.bitsToDouble();
     765             :           break;
     766           0 :         case Type::FloatTyID:
     767             :           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
     768           0 :           GV.IntVal = APInt::floatToBits(GV.FloatVal);
     769           0 :           break;
     770           0 :         case Type::DoubleTyID:
     771             :           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
     772           0 :           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
     773           0 :           break;
     774             :         case Type::PointerTyID:
     775             :           assert(DestTy->isPointerTy() && "Invalid bitcast");
     776             :           break; // getConstantValue(Op0)  above already converted it
     777             :       }
     778           0 :       return GV;
     779             :     }
     780           0 :     case Instruction::Add:
     781             :     case Instruction::FAdd:
     782             :     case Instruction::Sub:
     783             :     case Instruction::FSub:
     784             :     case Instruction::Mul:
     785             :     case Instruction::FMul:
     786             :     case Instruction::UDiv:
     787             :     case Instruction::SDiv:
     788             :     case Instruction::URem:
     789             :     case Instruction::SRem:
     790             :     case Instruction::And:
     791             :     case Instruction::Or:
     792             :     case Instruction::Xor: {
     793           0 :       GenericValue LHS = getConstantValue(Op0);
     794           0 :       GenericValue RHS = getConstantValue(CE->getOperand(1));
     795           0 :       GenericValue GV;
     796           0 :       switch (CE->getOperand(0)->getType()->getTypeID()) {
     797           0 :       default: llvm_unreachable("Bad add type!");
     798           0 :       case Type::IntegerTyID:
     799           0 :         switch (CE->getOpcode()) {
     800           0 :           default: llvm_unreachable("Invalid integer opcode");
     801           0 :           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
     802           0 :           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
     803           0 :           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
     804           0 :           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
     805           0 :           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
     806           0 :           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
     807           0 :           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
     808           0 :           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
     809           0 :           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
     810           0 :           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
     811             :         }
     812             :         break;
     813           0 :       case Type::FloatTyID:
     814           0 :         switch (CE->getOpcode()) {
     815           0 :           default: llvm_unreachable("Invalid float opcode");
     816           0 :           case Instruction::FAdd:
     817           0 :             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
     818           0 :           case Instruction::FSub:
     819           0 :             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
     820           0 :           case Instruction::FMul:
     821           0 :             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
     822           0 :           case Instruction::FDiv:
     823           0 :             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
     824           0 :           case Instruction::FRem:
     825           0 :             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
     826             :         }
     827             :         break;
     828           0 :       case Type::DoubleTyID:
     829           0 :         switch (CE->getOpcode()) {
     830           0 :           default: llvm_unreachable("Invalid double opcode");
     831           0 :           case Instruction::FAdd:
     832           0 :             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
     833           0 :           case Instruction::FSub:
     834           0 :             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
     835           0 :           case Instruction::FMul:
     836           0 :             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
     837           0 :           case Instruction::FDiv:
     838           0 :             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
     839           0 :           case Instruction::FRem:
     840           0 :             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
     841             :         }
     842             :         break;
     843           0 :       case Type::X86_FP80TyID:
     844             :       case Type::PPC_FP128TyID:
     845             :       case Type::FP128TyID: {
     846           0 :         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
     847           0 :         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
     848           0 :         switch (CE->getOpcode()) {
     849           0 :           default: llvm_unreachable("Invalid long double opcode");
     850           0 :           case Instruction::FAdd:
     851           0 :             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
     852           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     853           0 :             break;
     854           0 :           case Instruction::FSub:
     855           0 :             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
     856             :                             APFloat::rmNearestTiesToEven);
     857           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     858           0 :             break;
     859           0 :           case Instruction::FMul:
     860           0 :             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
     861             :                             APFloat::rmNearestTiesToEven);
     862           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     863           0 :             break;
     864           0 :           case Instruction::FDiv:
     865           0 :             apfLHS.divide(APFloat(Sem, RHS.IntVal),
     866             :                           APFloat::rmNearestTiesToEven);
     867           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     868           0 :             break;
     869           0 :           case Instruction::FRem:
     870           0 :             apfLHS.mod(APFloat(Sem, RHS.IntVal));
     871           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     872           0 :             break;
     873           0 :           }
     874             :         }
     875           0 :         break;
     876             :       }
     877           0 :       return GV;
     878             :     }
     879             :     default:
     880             :       break;
     881             :     }
     882             : 
     883           0 :     SmallString<256> Msg;
     884           0 :     raw_svector_ostream OS(Msg);
     885           0 :     OS << "ConstantExpr not handled: " << *CE;
     886           0 :     report_fatal_error(OS.str());
     887             :   }
     888             : 
     889             :   // Otherwise, we have a simple constant.
     890        1276 :   GenericValue Result;
     891        1276 :   switch (C->getType()->getTypeID()) {
     892          19 :   case Type::FloatTyID:
     893          57 :     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
     894          19 :     break;
     895          26 :   case Type::DoubleTyID:
     896          78 :     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
     897          26 :     break;
     898           0 :   case Type::X86_FP80TyID:
     899             :   case Type::FP128TyID:
     900             :   case Type::PPC_FP128TyID:
     901           0 :     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
     902           0 :     break;
     903         171 :   case Type::IntegerTyID:
     904         513 :     Result.IntVal = cast<ConstantInt>(C)->getValue();
     905         171 :     break;
     906          37 :   case Type::PointerTyID:
     907          74 :     if (isa<ConstantPointerNull>(C))
     908           0 :       Result.PointerVal = nullptr;
     909          64 :     else if (const Function *F = dyn_cast<Function>(C))
     910          54 :       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
     911          20 :     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
     912          20 :       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
     913             :     else
     914           0 :       llvm_unreachable("Unknown constant pointer type!");
     915             :     break;
     916         385 :   case Type::VectorTyID: {
     917             :     unsigned elemNum;
     918             :     Type* ElemTy;
     919         770 :     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
     920         770 :     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
     921         770 :     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
     922             : 
     923         385 :     if (CDV) {
     924         214 :         elemNum = CDV->getNumElements();
     925         214 :         ElemTy = CDV->getElementType();
     926         171 :     } else if (CV || CAZ) {
     927         342 :         VectorType* VTy = dyn_cast<VectorType>(C->getType());
     928         171 :         elemNum = VTy->getNumElements();
     929         171 :         ElemTy = VTy->getElementType();
     930             :     } else {
     931           0 :         llvm_unreachable("Unknown constant vector type!");
     932             :     }
     933             : 
     934         385 :     Result.AggregateVal.resize(elemNum);
     935             :     // Check if vector holds floats.
     936         385 :     if(ElemTy->isFloatTy()) {
     937          38 :       if (CAZ) {
     938          40 :         GenericValue floatZero;
     939          20 :         floatZero.FloatVal = 0.f;
     940          60 :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     941             :                   floatZero);
     942             :         break;
     943             :       }
     944          18 :       if(CV) {
     945           0 :         for (unsigned i = 0; i < elemNum; ++i)
     946           0 :           if (!isa<UndefValue>(CV->getOperand(i)))
     947           0 :             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
     948           0 :               CV->getOperand(i))->getValueAPF().convertToFloat();
     949             :         break;
     950             :       }
     951          18 :       if(CDV)
     952         188 :         for (unsigned i = 0; i < elemNum; ++i)
     953         170 :           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
     954             : 
     955             :       break;
     956             :     }
     957             :     // Check if vector holds doubles.
     958         347 :     if (ElemTy->isDoubleTy()) {
     959          49 :       if (CAZ) {
     960          40 :         GenericValue doubleZero;
     961          20 :         doubleZero.DoubleVal = 0.0;
     962          60 :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     963             :                   doubleZero);
     964             :         break;
     965             :       }
     966          29 :       if(CV) {
     967           0 :         for (unsigned i = 0; i < elemNum; ++i)
     968           0 :           if (!isa<UndefValue>(CV->getOperand(i)))
     969           0 :             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
     970           0 :               CV->getOperand(i))->getValueAPF().convertToDouble();
     971             :         break;
     972             :       }
     973          29 :       if(CDV)
     974         259 :         for (unsigned i = 0; i < elemNum; ++i)
     975         230 :           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
     976             : 
     977             :       break;
     978             :     }
     979             :     // Check if vector holds integers.
     980         298 :     if (ElemTy->isIntegerTy()) {
     981         298 :       if (CAZ) {
     982         162 :         GenericValue intZero;
     983         324 :         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
     984         243 :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     985             :                   intZero);
     986             :         break;
     987             :       }
     988         217 :       if(CV) {
     989         546 :         for (unsigned i = 0; i < elemNum; ++i)
     990         744 :           if (!isa<UndefValue>(CV->getOperand(i)))
     991         744 :             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
     992        1240 :                                             CV->getOperand(i))->getValue();
     993             :           else {
     994           0 :             Result.AggregateVal[i].IntVal =
     995           0 :               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
     996             :           }
     997             :         break;
     998             :       }
     999         167 :       if(CDV)
    1000        1615 :         for (unsigned i = 0; i < elemNum; ++i)
    1001        3620 :           Result.AggregateVal[i].IntVal = APInt(
    1002             :             CDV->getElementType()->getPrimitiveSizeInBits(),
    1003         724 :             CDV->getElementAsInteger(i));
    1004             : 
    1005             :       break;
    1006             :     }
    1007           0 :     llvm_unreachable("Unknown constant pointer type!");
    1008             :   }
    1009             :   break;
    1010             : 
    1011           0 :   default:
    1012           0 :     SmallString<256> Msg;
    1013           0 :     raw_svector_ostream OS(Msg);
    1014           0 :     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
    1015           0 :     report_fatal_error(OS.str());
    1016             :   }
    1017             : 
    1018         638 :   return Result;
    1019             : }
    1020             : 
    1021             : /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
    1022             : /// with the integer held in IntVal.
    1023             : static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
    1024             :                              unsigned StoreBytes) {
    1025             :   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
    1026          26 :   const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
    1027             : 
    1028             :   if (sys::IsLittleEndianHost) {
    1029             :     // Little-endian host - the source is ordered from LSB to MSB.  Order the
    1030             :     // destination from LSB to MSB: Do a straight copy.
    1031          26 :     memcpy(Dst, Src, StoreBytes);
    1032             :   } else {
    1033             :     // Big-endian host - the source is an array of 64 bit words ordered from
    1034             :     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
    1035             :     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
    1036             :     while (StoreBytes > sizeof(uint64_t)) {
    1037             :       StoreBytes -= sizeof(uint64_t);
    1038             :       // May not be aligned so use memcpy.
    1039             :       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
    1040             :       Src += sizeof(uint64_t);
    1041             :     }
    1042             : 
    1043             :     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
    1044             :   }
    1045             : }
    1046             : 
    1047          95 : void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
    1048             :                                          GenericValue *Ptr, Type *Ty) {
    1049         190 :   const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
    1050             : 
    1051          95 :   switch (Ty->getTypeID()) {
    1052           0 :   default:
    1053           0 :     dbgs() << "Cannot store value of type " << *Ty << "!\n";
    1054           0 :     break;
    1055          22 :   case Type::IntegerTyID:
    1056          22 :     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
    1057             :     break;
    1058           4 :   case Type::FloatTyID:
    1059           4 :     *((float*)Ptr) = Val.FloatVal;
    1060           4 :     break;
    1061           4 :   case Type::DoubleTyID:
    1062           4 :     *((double*)Ptr) = Val.DoubleVal;
    1063           4 :     break;
    1064           0 :   case Type::X86_FP80TyID:
    1065           0 :     memcpy(Ptr, Val.IntVal.getRawData(), 10);
    1066           0 :     break;
    1067          62 :   case Type::PointerTyID:
    1068             :     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
    1069          62 :     if (StoreBytes != sizeof(PointerTy))
    1070           0 :       memset(&(Ptr->PointerVal), 0, StoreBytes);
    1071             : 
    1072          62 :     *((PointerTy*)Ptr) = Val.PointerVal;
    1073          62 :     break;
    1074             :   case Type::VectorTyID:
    1075          42 :     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
    1076          24 :       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
    1077           8 :         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
    1078          24 :       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
    1079           8 :         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
    1080          24 :       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
    1081           8 :         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
    1082           4 :         StoreIntToMemory(Val.AggregateVal[i].IntVal,
    1083           4 :           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
    1084             :       }
    1085           3 :     }
    1086             :     break;
    1087             :   }
    1088             : 
    1089          95 :   if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
    1090             :     // Host and target are different endian - reverse the stored bytes.
    1091           0 :     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
    1092          95 : }
    1093             : 
    1094             : /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
    1095             : /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
    1096             : static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
    1097             :   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
    1098             :   uint8_t *Dst = reinterpret_cast<uint8_t *>(
    1099          23 :                    const_cast<uint64_t *>(IntVal.getRawData()));
    1100             : 
    1101             :   if (sys::IsLittleEndianHost)
    1102             :     // Little-endian host - the destination must be ordered from LSB to MSB.
    1103             :     // The source is ordered from LSB to MSB: Do a straight copy.
    1104          23 :     memcpy(Dst, Src, LoadBytes);
    1105             :   else {
    1106             :     // Big-endian - the destination is an array of 64 bit words ordered from
    1107             :     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
    1108             :     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
    1109             :     // a word.
    1110             :     while (LoadBytes > sizeof(uint64_t)) {
    1111             :       LoadBytes -= sizeof(uint64_t);
    1112             :       // May not be aligned so use memcpy.
    1113             :       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
    1114             :       Dst += sizeof(uint64_t);
    1115             :     }
    1116             : 
    1117             :     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
    1118             :   }
    1119             : }
    1120             : 
    1121             : /// FIXME: document
    1122             : ///
    1123          30 : void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
    1124             :                                           GenericValue *Ptr,
    1125             :                                           Type *Ty) {
    1126          60 :   const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
    1127             : 
    1128          30 :   switch (Ty->getTypeID()) {
    1129          19 :   case Type::IntegerTyID:
    1130             :     // An APInt with all words initially zero.
    1131          95 :     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
    1132          19 :     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
    1133             :     break;
    1134           4 :   case Type::FloatTyID:
    1135           4 :     Result.FloatVal = *((float*)Ptr);
    1136           4 :     break;
    1137           4 :   case Type::DoubleTyID:
    1138           4 :     Result.DoubleVal = *((double*)Ptr);
    1139           4 :     break;
    1140           0 :   case Type::PointerTyID:
    1141           0 :     Result.PointerVal = *((PointerTy*)Ptr);
    1142           0 :     break;
    1143           0 :   case Type::X86_FP80TyID: {
    1144             :     // This is endian dependent, but it will only work on x86 anyway.
    1145             :     // FIXME: Will not trap if loading a signaling NaN.
    1146             :     uint64_t y[2];
    1147           0 :     memcpy(y, Ptr, 10);
    1148           0 :     Result.IntVal = APInt(80, y);
    1149             :     break;
    1150             :   }
    1151           3 :   case Type::VectorTyID: {
    1152           3 :     auto *VT = cast<VectorType>(Ty);
    1153           3 :     Type *ElemT = VT->getElementType();
    1154           3 :     const unsigned numElems = VT->getNumElements();
    1155           3 :     if (ElemT->isFloatTy()) {
    1156           1 :       Result.AggregateVal.resize(numElems);
    1157           5 :       for (unsigned i = 0; i < numElems; ++i)
    1158           8 :         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
    1159             :     }
    1160           3 :     if (ElemT->isDoubleTy()) {
    1161           1 :       Result.AggregateVal.resize(numElems);
    1162           5 :       for (unsigned i = 0; i < numElems; ++i)
    1163           8 :         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
    1164             :     }
    1165           3 :     if (ElemT->isIntegerTy()) {
    1166           2 :       GenericValue intZero;
    1167           2 :       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
    1168           3 :       intZero.IntVal = APInt(elemBitWidth, 0);
    1169           1 :       Result.AggregateVal.resize(numElems, intZero);
    1170           5 :       for (unsigned i = 0; i < numElems; ++i)
    1171          16 :         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
    1172           8 :           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
    1173             :     }
    1174             :   break;
    1175             :   }
    1176           0 :   default:
    1177           0 :     SmallString<256> Msg;
    1178           0 :     raw_svector_ostream OS(Msg);
    1179           0 :     OS << "Cannot load value of type " << *Ty << "!";
    1180           0 :     report_fatal_error(OS.str());
    1181             :   }
    1182          30 : }
    1183             : 
    1184          10 : void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
    1185             :   DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
    1186             :   DEBUG(Init->dump());
    1187          20 :   if (isa<UndefValue>(Init))
    1188             :     return;
    1189             : 
    1190          10 :   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
    1191             :     unsigned ElementSize =
    1192           0 :         getDataLayout().getTypeAllocSize(CP->getType()->getElementType());
    1193           0 :     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
    1194           0 :       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
    1195             :     return;
    1196             :   }
    1197             : 
    1198          20 :   if (isa<ConstantAggregateZero>(Init)) {
    1199           1 :     memset(Addr, 0, (size_t)getDataLayout().getTypeAllocSize(Init->getType()));
    1200           1 :     return;
    1201             :   }
    1202             : 
    1203           9 :   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
    1204             :     unsigned ElementSize =
    1205           0 :         getDataLayout().getTypeAllocSize(CPA->getType()->getElementType());
    1206           0 :     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
    1207           0 :       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
    1208             :     return;
    1209             :   }
    1210             : 
    1211           9 :   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
    1212             :     const StructLayout *SL =
    1213           0 :         getDataLayout().getStructLayout(cast<StructType>(CPS->getType()));
    1214           0 :     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
    1215           0 :       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
    1216             :     return;
    1217             :   }
    1218             : 
    1219             :   if (const ConstantDataSequential *CDS =
    1220          17 :                dyn_cast<ConstantDataSequential>(Init)) {
    1221             :     // CDS is already laid out in host memory order.
    1222           8 :     StringRef Data = CDS->getRawDataValues();
    1223          16 :     memcpy(Addr, Data.data(), Data.size());
    1224             :     return;
    1225             :   }
    1226             : 
    1227           2 :   if (Init->getType()->isFirstClassType()) {
    1228           2 :     GenericValue Val = getConstantValue(Init);
    1229           1 :     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
    1230             :     return;
    1231             :   }
    1232             : 
    1233             :   DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
    1234           0 :   llvm_unreachable("Unknown constant type to initialize memory with!");
    1235             : }
    1236             : 
    1237             : /// EmitGlobals - Emit all of the global variables to memory, storing their
    1238             : /// addresses into GlobalAddress.  This must make sure to copy the contents of
    1239             : /// their initializers into the memory.
    1240          22 : void ExecutionEngine::emitGlobals() {
    1241             :   // Loop over all of the global variables in the program, allocating the memory
    1242             :   // to hold them.  If there is more than one module, do a prepass over globals
    1243             :   // to figure out how the different modules should link together.
    1244             :   std::map<std::pair<std::string, Type*>,
    1245          44 :            const GlobalValue*> LinkedGlobalsMap;
    1246             : 
    1247          44 :   if (Modules.size() != 1) {
    1248           0 :     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
    1249           0 :       Module &M = *Modules[m];
    1250           0 :       for (const auto &GV : M.globals()) {
    1251           0 :         if (GV.hasLocalLinkage() || GV.isDeclaration() ||
    1252           0 :             GV.hasAppendingLinkage() || !GV.hasName())
    1253           0 :           continue;// Ignore external globals and globals with internal linkage.
    1254             : 
    1255             :         const GlobalValue *&GVEntry =
    1256           0 :           LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
    1257             : 
    1258             :         // If this is the first time we've seen this global, it is the canonical
    1259             :         // version.
    1260           0 :         if (!GVEntry) {
    1261           0 :           GVEntry = &GV;
    1262           0 :           continue;
    1263             :         }
    1264             : 
    1265             :         // If the existing global is strong, never replace it.
    1266           0 :         if (GVEntry->hasExternalLinkage())
    1267           0 :           continue;
    1268             : 
    1269             :         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
    1270             :         // symbol.  FIXME is this right for common?
    1271           0 :         if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
    1272           0 :           GVEntry = &GV;
    1273             :       }
    1274             :     }
    1275             :   }
    1276             : 
    1277          44 :   std::vector<const GlobalValue*> NonCanonicalGlobals;
    1278          66 :   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
    1279          66 :     Module &M = *Modules[m];
    1280          31 :     for (const auto &GV : M.globals()) {
    1281             :       // In the multi-module case, see what this global maps to.
    1282           9 :       if (!LinkedGlobalsMap.empty()) {
    1283           0 :         if (const GlobalValue *GVEntry =
    1284           0 :               LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
    1285             :           // If something else is the canonical global, ignore this one.
    1286           0 :           if (GVEntry != &GV) {
    1287           0 :             NonCanonicalGlobals.push_back(&GV);
    1288           0 :             continue;
    1289             :           }
    1290             :         }
    1291             :       }
    1292             : 
    1293           9 :       if (!GV.isDeclaration()) {
    1294           9 :         addGlobalMapping(&GV, getMemoryForGV(&GV));
    1295             :       } else {
    1296             :         // External variable reference. Try to use the dynamic loader to
    1297             :         // get a pointer to it.
    1298           0 :         if (void *SymAddr =
    1299           0 :             sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
    1300           0 :           addGlobalMapping(&GV, SymAddr);
    1301             :         else {
    1302           0 :           report_fatal_error("Could not resolve external global address: "
    1303           0 :                             +GV.getName());
    1304             :         }
    1305             :       }
    1306             :     }
    1307             : 
    1308             :     // If there are multiple modules, map the non-canonical globals to their
    1309             :     // canonical location.
    1310          22 :     if (!NonCanonicalGlobals.empty()) {
    1311           0 :       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
    1312           0 :         const GlobalValue *GV = NonCanonicalGlobals[i];
    1313             :         const GlobalValue *CGV =
    1314           0 :           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
    1315           0 :         void *Ptr = getPointerToGlobalIfAvailable(CGV);
    1316             :         assert(Ptr && "Canonical global wasn't codegen'd!");
    1317           0 :         addGlobalMapping(GV, Ptr);
    1318             :       }
    1319             :     }
    1320             : 
    1321             :     // Now that all of the globals are set up in memory, loop through them all
    1322             :     // and initialize their contents.
    1323          31 :     for (const auto &GV : M.globals()) {
    1324           9 :       if (!GV.isDeclaration()) {
    1325           9 :         if (!LinkedGlobalsMap.empty()) {
    1326           0 :           if (const GlobalValue *GVEntry =
    1327           0 :                 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
    1328           0 :             if (GVEntry != &GV)  // Not the canonical variable.
    1329           0 :               continue;
    1330             :         }
    1331           9 :         EmitGlobalVariable(&GV);
    1332             :       }
    1333             :     }
    1334             :   }
    1335          22 : }
    1336             : 
    1337             : // EmitGlobalVariable - This method emits the specified global variable to the
    1338             : // address specified in GlobalAddresses, or allocates new memory if it's not
    1339             : // already in the map.
    1340          10 : void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
    1341          10 :   void *GA = getPointerToGlobalIfAvailable(GV);
    1342             : 
    1343          10 :   if (!GA) {
    1344             :     // If it's not already specified, allocate memory for the global.
    1345           1 :     GA = getMemoryForGV(GV);
    1346             : 
    1347             :     // If we failed to allocate memory for this global, return.
    1348           1 :     if (!GA) return;
    1349             : 
    1350           1 :     addGlobalMapping(GV, GA);
    1351             :   }
    1352             : 
    1353             :   // Don't initialize if it's thread local, let the client do it.
    1354          20 :   if (!GV->isThreadLocal())
    1355          10 :     InitializeMemory(GV->getInitializer(), GA);
    1356             : 
    1357          10 :   Type *ElTy = GV->getValueType();
    1358          10 :   size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
    1359          10 :   NumInitBytes += (unsigned)GVSize;
    1360             :   ++NumGlobals;
    1361             : }

Generated by: LCOV version 1.13