xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/ExecutionEngine.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the common interface used by the various execution engine
100b57cec5SDimitry Andric // subclasses.
110b57cec5SDimitry Andric //
12*5ffd83dbSDimitry Andric // FIXME: This file needs to be updated to support scalable vectors
13*5ffd83dbSDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/ExecutionEngine/ExecutionEngine.h"
170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
200b57cec5SDimitry Andric #include "llvm/ExecutionEngine/GenericValue.h"
210b57cec5SDimitry Andric #include "llvm/ExecutionEngine/JITEventListener.h"
220b57cec5SDimitry Andric #include "llvm/ExecutionEngine/ObjectCache.h"
230b57cec5SDimitry Andric #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
240b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
250b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
260b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
270b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
280b57cec5SDimitry Andric #include "llvm/IR/Module.h"
290b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
300b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h"
310b57cec5SDimitry Andric #include "llvm/Object/Archive.h"
320b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
330b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
340b57cec5SDimitry Andric #include "llvm/Support/DynamicLibrary.h"
350b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
360b57cec5SDimitry Andric #include "llvm/Support/Host.h"
370b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
390b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
400b57cec5SDimitry Andric #include <cmath>
410b57cec5SDimitry Andric #include <cstring>
428bcb0991SDimitry Andric #include <mutex>
430b57cec5SDimitry Andric using namespace llvm;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric #define DEBUG_TYPE "jit"
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
480b57cec5SDimitry Andric STATISTIC(NumGlobals  , "Number of global vars initialized");
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
510b57cec5SDimitry Andric     std::unique_ptr<Module> M, std::string *ErrorStr,
520b57cec5SDimitry Andric     std::shared_ptr<MCJITMemoryManager> MemMgr,
530b57cec5SDimitry Andric     std::shared_ptr<LegacyJITSymbolResolver> Resolver,
540b57cec5SDimitry Andric     std::unique_ptr<TargetMachine> TM) = nullptr;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
570b57cec5SDimitry Andric     std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
580b57cec5SDimitry Andric     std::shared_ptr<LegacyJITSymbolResolver> Resolver,
590b57cec5SDimitry Andric     std::unique_ptr<TargetMachine> TM) = nullptr;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
620b57cec5SDimitry Andric                                                 std::string *ErrorStr) =nullptr;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric void JITEventListener::anchor() {}
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric void ObjectCache::anchor() {}
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric void ExecutionEngine::Init(std::unique_ptr<Module> M) {
690b57cec5SDimitry Andric   CompilingLazily         = false;
700b57cec5SDimitry Andric   GVCompilationDisabled   = false;
710b57cec5SDimitry Andric   SymbolSearchingDisabled = false;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // IR module verification is enabled by default in debug builds, and disabled
740b57cec5SDimitry Andric   // by default in release builds.
750b57cec5SDimitry Andric #ifndef NDEBUG
760b57cec5SDimitry Andric   VerifyModules = true;
770b57cec5SDimitry Andric #else
780b57cec5SDimitry Andric   VerifyModules = false;
790b57cec5SDimitry Andric #endif
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   assert(M && "Module is null?");
820b57cec5SDimitry Andric   Modules.push_back(std::move(M));
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
860b57cec5SDimitry Andric     : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
870b57cec5SDimitry Andric   Init(std::move(M));
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
910b57cec5SDimitry Andric     : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
920b57cec5SDimitry Andric   Init(std::move(M));
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric ExecutionEngine::~ExecutionEngine() {
960b57cec5SDimitry Andric   clearAllGlobalMappings();
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric namespace {
1000b57cec5SDimitry Andric /// Helper class which uses a value handler to automatically deletes the
1010b57cec5SDimitry Andric /// memory block when the GlobalVariable is destroyed.
1020b57cec5SDimitry Andric class GVMemoryBlock final : public CallbackVH {
1030b57cec5SDimitry Andric   GVMemoryBlock(const GlobalVariable *GV)
1040b57cec5SDimitry Andric     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric public:
1070b57cec5SDimitry Andric   /// Returns the address the GlobalVariable should be written into.  The
1080b57cec5SDimitry Andric   /// GVMemoryBlock object prefixes that.
1090b57cec5SDimitry Andric   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
1100b57cec5SDimitry Andric     Type *ElTy = GV->getValueType();
1110b57cec5SDimitry Andric     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
1120b57cec5SDimitry Andric     void *RawMemory = ::operator new(
113*5ffd83dbSDimitry Andric         alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlign(GV)) + GVSize);
1140b57cec5SDimitry Andric     new(RawMemory) GVMemoryBlock(GV);
1150b57cec5SDimitry Andric     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
1160b57cec5SDimitry Andric   }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   void deleted() override {
1190b57cec5SDimitry Andric     // We allocated with operator new and with some extra memory hanging off the
1200b57cec5SDimitry Andric     // end, so don't just delete this.  I'm not sure if this is actually
1210b57cec5SDimitry Andric     // required.
1220b57cec5SDimitry Andric     this->~GVMemoryBlock();
1230b57cec5SDimitry Andric     ::operator delete(this);
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric };
1260b57cec5SDimitry Andric }  // anonymous namespace
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
1290b57cec5SDimitry Andric   return GVMemoryBlock::Create(GV, getDataLayout());
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
1330b57cec5SDimitry Andric   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric void
1370b57cec5SDimitry Andric ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
1380b57cec5SDimitry Andric   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
1420b57cec5SDimitry Andric   llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric bool ExecutionEngine::removeModule(Module *M) {
1460b57cec5SDimitry Andric   for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
1470b57cec5SDimitry Andric     Module *Found = I->get();
1480b57cec5SDimitry Andric     if (Found == M) {
1490b57cec5SDimitry Andric       I->release();
1500b57cec5SDimitry Andric       Modules.erase(I);
1510b57cec5SDimitry Andric       clearGlobalMappingsFromModule(M);
1520b57cec5SDimitry Andric       return true;
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric   return false;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric Function *ExecutionEngine::FindFunctionNamed(StringRef FnName) {
1590b57cec5SDimitry Andric   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
1600b57cec5SDimitry Andric     Function *F = Modules[i]->getFunction(FnName);
1610b57cec5SDimitry Andric     if (F && !F->isDeclaration())
1620b57cec5SDimitry Andric       return F;
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric   return nullptr;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric GlobalVariable *ExecutionEngine::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
1680b57cec5SDimitry Andric   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
1690b57cec5SDimitry Andric     GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
1700b57cec5SDimitry Andric     if (GV && !GV->isDeclaration())
1710b57cec5SDimitry Andric       return GV;
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric   return nullptr;
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
1770b57cec5SDimitry Andric   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
1780b57cec5SDimitry Andric   uint64_t OldVal;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
1810b57cec5SDimitry Andric   // GlobalAddressMap.
1820b57cec5SDimitry Andric   if (I == GlobalAddressMap.end())
1830b57cec5SDimitry Andric     OldVal = 0;
1840b57cec5SDimitry Andric   else {
1850b57cec5SDimitry Andric     GlobalAddressReverseMap.erase(I->second);
1860b57cec5SDimitry Andric     OldVal = I->second;
1870b57cec5SDimitry Andric     GlobalAddressMap.erase(I);
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   return OldVal;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
1940b57cec5SDimitry Andric   assert(GV->hasName() && "Global must have name.");
1950b57cec5SDimitry Andric 
1968bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
1970b57cec5SDimitry Andric   SmallString<128> FullName;
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   const DataLayout &DL =
2000b57cec5SDimitry Andric     GV->getParent()->getDataLayout().isDefault()
2010b57cec5SDimitry Andric       ? getDataLayout()
2020b57cec5SDimitry Andric       : GV->getParent()->getDataLayout();
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
205*5ffd83dbSDimitry Andric   return std::string(FullName.str());
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
2098bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2100b57cec5SDimitry Andric   addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric void ExecutionEngine::addGlobalMapping(StringRef Name, uint64_t Addr) {
2148bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   assert(!Name.empty() && "Empty GlobalMapping symbol name!");
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "JIT: Map \'" << Name << "\' to [" << Addr << "]\n";);
2190b57cec5SDimitry Andric   uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
2200b57cec5SDimitry Andric   assert((!CurVal || !Addr) && "GlobalMapping already established!");
2210b57cec5SDimitry Andric   CurVal = Addr;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   // If we are using the reverse mapping, add it too.
2240b57cec5SDimitry Andric   if (!EEState.getGlobalAddressReverseMap().empty()) {
2250b57cec5SDimitry Andric     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
2260b57cec5SDimitry Andric     assert((!V.empty() || !Name.empty()) &&
2270b57cec5SDimitry Andric            "GlobalMapping already established!");
228*5ffd83dbSDimitry Andric     V = std::string(Name);
2290b57cec5SDimitry Andric   }
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric void ExecutionEngine::clearAllGlobalMappings() {
2338bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   EEState.getGlobalAddressMap().clear();
2360b57cec5SDimitry Andric   EEState.getGlobalAddressReverseMap().clear();
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
2408bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   for (GlobalObject &GO : M->global_objects())
2430b57cec5SDimitry Andric     EEState.RemoveMapping(getMangledName(&GO));
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
2470b57cec5SDimitry Andric                                               void *Addr) {
2488bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2490b57cec5SDimitry Andric   return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric uint64_t ExecutionEngine::updateGlobalMapping(StringRef Name, uint64_t Addr) {
2538bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   ExecutionEngineState::GlobalAddressMapTy &Map =
2560b57cec5SDimitry Andric     EEState.getGlobalAddressMap();
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   // Deleting from the mapping?
2590b57cec5SDimitry Andric   if (!Addr)
2600b57cec5SDimitry Andric     return EEState.RemoveMapping(Name);
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   uint64_t &CurVal = Map[Name];
2630b57cec5SDimitry Andric   uint64_t OldVal = CurVal;
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
2660b57cec5SDimitry Andric     EEState.getGlobalAddressReverseMap().erase(CurVal);
2670b57cec5SDimitry Andric   CurVal = Addr;
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   // If we are using the reverse mapping, add it too.
2700b57cec5SDimitry Andric   if (!EEState.getGlobalAddressReverseMap().empty()) {
2710b57cec5SDimitry Andric     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
2720b57cec5SDimitry Andric     assert((!V.empty() || !Name.empty()) &&
2730b57cec5SDimitry Andric            "GlobalMapping already established!");
274*5ffd83dbSDimitry Andric     V = std::string(Name);
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric   return OldVal;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric uint64_t ExecutionEngine::getAddressToGlobalIfAvailable(StringRef S) {
2808bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2810b57cec5SDimitry Andric   uint64_t Address = 0;
2820b57cec5SDimitry Andric   ExecutionEngineState::GlobalAddressMapTy::iterator I =
2830b57cec5SDimitry Andric     EEState.getGlobalAddressMap().find(S);
2840b57cec5SDimitry Andric   if (I != EEState.getGlobalAddressMap().end())
2850b57cec5SDimitry Andric     Address = I->second;
2860b57cec5SDimitry Andric   return Address;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric void *ExecutionEngine::getPointerToGlobalIfAvailable(StringRef S) {
2918bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2920b57cec5SDimitry Andric   if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
2930b57cec5SDimitry Andric     return Address;
2940b57cec5SDimitry Andric   return nullptr;
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
2988bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
2990b57cec5SDimitry Andric   return getPointerToGlobalIfAvailable(getMangledName(GV));
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
3038bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   // If we haven't computed the reverse mapping yet, do so first.
3060b57cec5SDimitry Andric   if (EEState.getGlobalAddressReverseMap().empty()) {
3070b57cec5SDimitry Andric     for (ExecutionEngineState::GlobalAddressMapTy::iterator
3080b57cec5SDimitry Andric            I = EEState.getGlobalAddressMap().begin(),
3090b57cec5SDimitry Andric            E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
3100b57cec5SDimitry Andric       StringRef Name = I->first();
3110b57cec5SDimitry Andric       uint64_t Addr = I->second;
312*5ffd83dbSDimitry Andric       EEState.getGlobalAddressReverseMap().insert(
313*5ffd83dbSDimitry Andric           std::make_pair(Addr, std::string(Name)));
3140b57cec5SDimitry Andric     }
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   std::map<uint64_t, std::string>::iterator I =
3180b57cec5SDimitry Andric     EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   if (I != EEState.getGlobalAddressReverseMap().end()) {
3210b57cec5SDimitry Andric     StringRef Name = I->second;
3220b57cec5SDimitry Andric     for (unsigned i = 0, e = Modules.size(); i != e; ++i)
3230b57cec5SDimitry Andric       if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
3240b57cec5SDimitry Andric         return GV;
3250b57cec5SDimitry Andric   }
3260b57cec5SDimitry Andric   return nullptr;
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric namespace {
3300b57cec5SDimitry Andric class ArgvArray {
3310b57cec5SDimitry Andric   std::unique_ptr<char[]> Array;
3320b57cec5SDimitry Andric   std::vector<std::unique_ptr<char[]>> Values;
3330b57cec5SDimitry Andric public:
3340b57cec5SDimitry Andric   /// Turn a vector of strings into a nice argv style array of pointers to null
3350b57cec5SDimitry Andric   /// terminated strings.
3360b57cec5SDimitry Andric   void *reset(LLVMContext &C, ExecutionEngine *EE,
3370b57cec5SDimitry Andric               const std::vector<std::string> &InputArgv);
3380b57cec5SDimitry Andric };
3390b57cec5SDimitry Andric }  // anonymous namespace
3400b57cec5SDimitry Andric void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
3410b57cec5SDimitry Andric                        const std::vector<std::string> &InputArgv) {
3420b57cec5SDimitry Andric   Values.clear();  // Free the old contents.
3430b57cec5SDimitry Andric   Values.reserve(InputArgv.size());
3440b57cec5SDimitry Andric   unsigned PtrSize = EE->getDataLayout().getPointerSize();
3458bcb0991SDimitry Andric   Array = std::make_unique<char[]>((InputArgv.size()+1)*PtrSize);
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "JIT: ARGV = " << (void *)Array.get() << "\n");
3480b57cec5SDimitry Andric   Type *SBytePtr = Type::getInt8PtrTy(C);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   for (unsigned i = 0; i != InputArgv.size(); ++i) {
3510b57cec5SDimitry Andric     unsigned Size = InputArgv[i].size()+1;
3528bcb0991SDimitry Andric     auto Dest = std::make_unique<char[]>(Size);
3530b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void *)Dest.get()
3540b57cec5SDimitry Andric                       << "\n");
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
3570b57cec5SDimitry Andric     Dest[Size-1] = 0;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     // Endian safe: Array[i] = (PointerTy)Dest;
3600b57cec5SDimitry Andric     EE->StoreValueToMemory(PTOGV(Dest.get()),
3610b57cec5SDimitry Andric                            (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
3620b57cec5SDimitry Andric     Values.push_back(std::move(Dest));
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // Null terminate it
3660b57cec5SDimitry Andric   EE->StoreValueToMemory(PTOGV(nullptr),
3670b57cec5SDimitry Andric                          (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
3680b57cec5SDimitry Andric                          SBytePtr);
3690b57cec5SDimitry Andric   return Array.get();
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
3730b57cec5SDimitry Andric                                                        bool isDtors) {
3740b57cec5SDimitry Andric   StringRef Name(isDtors ? "llvm.global_dtors" : "llvm.global_ctors");
3750b57cec5SDimitry Andric   GlobalVariable *GV = module.getNamedGlobal(Name);
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   // If this global has internal linkage, or if it has a use, then it must be
3780b57cec5SDimitry Andric   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
3790b57cec5SDimitry Andric   // this is the case, don't execute any of the global ctors, __main will do
3800b57cec5SDimitry Andric   // it.
3810b57cec5SDimitry Andric   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   // Should be an array of '{ i32, void ()* }' structs.  The first value is
3840b57cec5SDimitry Andric   // the init priority, which we ignore.
3850b57cec5SDimitry Andric   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
3860b57cec5SDimitry Andric   if (!InitList)
3870b57cec5SDimitry Andric     return;
3880b57cec5SDimitry Andric   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
3890b57cec5SDimitry Andric     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
3900b57cec5SDimitry Andric     if (!CS) continue;
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric     Constant *FP = CS->getOperand(1);
3930b57cec5SDimitry Andric     if (FP->isNullValue())
3940b57cec5SDimitry Andric       continue;  // Found a sentinal value, ignore.
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric     // Strip off constant expression casts.
3970b57cec5SDimitry Andric     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
3980b57cec5SDimitry Andric       if (CE->isCast())
3990b57cec5SDimitry Andric         FP = CE->getOperand(0);
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric     // Execute the ctor/dtor function!
4020b57cec5SDimitry Andric     if (Function *F = dyn_cast<Function>(FP))
4030b57cec5SDimitry Andric       runFunction(F, None);
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric     // FIXME: It is marginally lame that we just do nothing here if we see an
4060b57cec5SDimitry Andric     // entry we don't recognize. It might not be unreasonable for the verifier
4070b57cec5SDimitry Andric     // to not even allow this and just assert here.
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
4120b57cec5SDimitry Andric   // Execute global ctors/dtors for each module in the program.
4130b57cec5SDimitry Andric   for (std::unique_ptr<Module> &M : Modules)
4140b57cec5SDimitry Andric     runStaticConstructorsDestructors(*M, isDtors);
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric #ifndef NDEBUG
4180b57cec5SDimitry Andric /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
4190b57cec5SDimitry Andric static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
4200b57cec5SDimitry Andric   unsigned PtrSize = EE->getDataLayout().getPointerSize();
4210b57cec5SDimitry Andric   for (unsigned i = 0; i < PtrSize; ++i)
4220b57cec5SDimitry Andric     if (*(i + (uint8_t*)Loc))
4230b57cec5SDimitry Andric       return false;
4240b57cec5SDimitry Andric   return true;
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric #endif
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric int ExecutionEngine::runFunctionAsMain(Function *Fn,
4290b57cec5SDimitry Andric                                        const std::vector<std::string> &argv,
4300b57cec5SDimitry Andric                                        const char * const * envp) {
4310b57cec5SDimitry Andric   std::vector<GenericValue> GVArgs;
4320b57cec5SDimitry Andric   GenericValue GVArgc;
4330b57cec5SDimitry Andric   GVArgc.IntVal = APInt(32, argv.size());
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // Check main() type
4360b57cec5SDimitry Andric   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
4370b57cec5SDimitry Andric   FunctionType *FTy = Fn->getFunctionType();
4380b57cec5SDimitry Andric   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   // Check the argument types.
4410b57cec5SDimitry Andric   if (NumArgs > 3)
4420b57cec5SDimitry Andric     report_fatal_error("Invalid number of arguments of main() supplied");
4430b57cec5SDimitry Andric   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
4440b57cec5SDimitry Andric     report_fatal_error("Invalid type for third argument of main() supplied");
4450b57cec5SDimitry Andric   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
4460b57cec5SDimitry Andric     report_fatal_error("Invalid type for second argument of main() supplied");
4470b57cec5SDimitry Andric   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
4480b57cec5SDimitry Andric     report_fatal_error("Invalid type for first argument of main() supplied");
4490b57cec5SDimitry Andric   if (!FTy->getReturnType()->isIntegerTy() &&
4500b57cec5SDimitry Andric       !FTy->getReturnType()->isVoidTy())
4510b57cec5SDimitry Andric     report_fatal_error("Invalid return type of main() supplied");
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   ArgvArray CArgv;
4540b57cec5SDimitry Andric   ArgvArray CEnv;
4550b57cec5SDimitry Andric   if (NumArgs) {
4560b57cec5SDimitry Andric     GVArgs.push_back(GVArgc); // Arg #0 = argc.
4570b57cec5SDimitry Andric     if (NumArgs > 1) {
4580b57cec5SDimitry Andric       // Arg #1 = argv.
4590b57cec5SDimitry Andric       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
4600b57cec5SDimitry Andric       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
4610b57cec5SDimitry Andric              "argv[0] was null after CreateArgv");
4620b57cec5SDimitry Andric       if (NumArgs > 2) {
4630b57cec5SDimitry Andric         std::vector<std::string> EnvVars;
4640b57cec5SDimitry Andric         for (unsigned i = 0; envp[i]; ++i)
4650b57cec5SDimitry Andric           EnvVars.emplace_back(envp[i]);
4660b57cec5SDimitry Andric         // Arg #2 = envp.
4670b57cec5SDimitry Andric         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
4680b57cec5SDimitry Andric       }
4690b57cec5SDimitry Andric     }
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
4780b57cec5SDimitry Andric     : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
4790b57cec5SDimitry Andric       OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
4800b57cec5SDimitry Andric       UseOrcMCJITReplacement(false) {
4810b57cec5SDimitry Andric // IR module verification is enabled by default in debug builds, and disabled
4820b57cec5SDimitry Andric // by default in release builds.
4830b57cec5SDimitry Andric #ifndef NDEBUG
4840b57cec5SDimitry Andric   VerifyModules = true;
4850b57cec5SDimitry Andric #else
4860b57cec5SDimitry Andric   VerifyModules = false;
4870b57cec5SDimitry Andric #endif
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric EngineBuilder::~EngineBuilder() = default;
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric EngineBuilder &EngineBuilder::setMCJITMemoryManager(
4930b57cec5SDimitry Andric                                    std::unique_ptr<RTDyldMemoryManager> mcjmm) {
4940b57cec5SDimitry Andric   auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
4950b57cec5SDimitry Andric   MemMgr = SharedMM;
4960b57cec5SDimitry Andric   Resolver = SharedMM;
4970b57cec5SDimitry Andric   return *this;
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric EngineBuilder&
5010b57cec5SDimitry Andric EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
5020b57cec5SDimitry Andric   MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
5030b57cec5SDimitry Andric   return *this;
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric EngineBuilder &
5070b57cec5SDimitry Andric EngineBuilder::setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR) {
5080b57cec5SDimitry Andric   Resolver = std::shared_ptr<LegacyJITSymbolResolver>(std::move(SR));
5090b57cec5SDimitry Andric   return *this;
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
5130b57cec5SDimitry Andric   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   // Make sure we can resolve symbols in the program as well. The zero arg
5160b57cec5SDimitry Andric   // to the function tells DynamicLibrary to load the program, not a library.
5170b57cec5SDimitry Andric   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
5180b57cec5SDimitry Andric     return nullptr;
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // If the user specified a memory manager but didn't specify which engine to
5210b57cec5SDimitry Andric   // create, we assume they only want the JIT, and we fail if they only want
5220b57cec5SDimitry Andric   // the interpreter.
5230b57cec5SDimitry Andric   if (MemMgr) {
5240b57cec5SDimitry Andric     if (WhichEngine & EngineKind::JIT)
5250b57cec5SDimitry Andric       WhichEngine = EngineKind::JIT;
5260b57cec5SDimitry Andric     else {
5270b57cec5SDimitry Andric       if (ErrorStr)
5280b57cec5SDimitry Andric         *ErrorStr = "Cannot create an interpreter with a memory manager.";
5290b57cec5SDimitry Andric       return nullptr;
5300b57cec5SDimitry Andric     }
5310b57cec5SDimitry Andric   }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   // Unless the interpreter was explicitly selected or the JIT is not linked,
5340b57cec5SDimitry Andric   // try making a JIT.
5350b57cec5SDimitry Andric   if ((WhichEngine & EngineKind::JIT) && TheTM) {
5360b57cec5SDimitry Andric     if (!TM->getTarget().hasJIT()) {
5370b57cec5SDimitry Andric       errs() << "WARNING: This target JIT is not designed for the host"
5380b57cec5SDimitry Andric              << " you are running.  If bad things happen, please choose"
5390b57cec5SDimitry Andric              << " a different -march switch.\n";
5400b57cec5SDimitry Andric     }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric     ExecutionEngine *EE = nullptr;
5430b57cec5SDimitry Andric     if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
5440b57cec5SDimitry Andric       EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
5450b57cec5SDimitry Andric                                                     std::move(Resolver),
5460b57cec5SDimitry Andric                                                     std::move(TheTM));
5470b57cec5SDimitry Andric       EE->addModule(std::move(M));
5480b57cec5SDimitry Andric     } else if (ExecutionEngine::MCJITCtor)
5490b57cec5SDimitry Andric       EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
5500b57cec5SDimitry Andric                                       std::move(Resolver), std::move(TheTM));
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric     if (EE) {
5530b57cec5SDimitry Andric       EE->setVerifyModules(VerifyModules);
5540b57cec5SDimitry Andric       return EE;
5550b57cec5SDimitry Andric     }
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   // If we can't make a JIT and we didn't request one specifically, try making
5590b57cec5SDimitry Andric   // an interpreter instead.
5600b57cec5SDimitry Andric   if (WhichEngine & EngineKind::Interpreter) {
5610b57cec5SDimitry Andric     if (ExecutionEngine::InterpCtor)
5620b57cec5SDimitry Andric       return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
5630b57cec5SDimitry Andric     if (ErrorStr)
5640b57cec5SDimitry Andric       *ErrorStr = "Interpreter has not been linked in.";
5650b57cec5SDimitry Andric     return nullptr;
5660b57cec5SDimitry Andric   }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
5690b57cec5SDimitry Andric     if (ErrorStr)
5700b57cec5SDimitry Andric       *ErrorStr = "JIT has not been linked in.";
5710b57cec5SDimitry Andric   }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   return nullptr;
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
5770b57cec5SDimitry Andric   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
5780b57cec5SDimitry Andric     return getPointerToFunction(F);
5790b57cec5SDimitry Andric 
5808bcb0991SDimitry Andric   std::lock_guard<sys::Mutex> locked(lock);
5810b57cec5SDimitry Andric   if (void* P = getPointerToGlobalIfAvailable(GV))
5820b57cec5SDimitry Andric     return P;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   // Global variable might have been added since interpreter started.
5850b57cec5SDimitry Andric   if (GlobalVariable *GVar =
5860b57cec5SDimitry Andric           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
587*5ffd83dbSDimitry Andric     emitGlobalVariable(GVar);
5880b57cec5SDimitry Andric   else
5890b57cec5SDimitry Andric     llvm_unreachable("Global hasn't had an address allocated yet!");
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   return getPointerToGlobalIfAvailable(GV);
5920b57cec5SDimitry Andric }
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric /// Converts a Constant* into a GenericValue, including handling of
5950b57cec5SDimitry Andric /// ConstantExpr values.
5960b57cec5SDimitry Andric GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
5970b57cec5SDimitry Andric   // If its undefined, return the garbage.
5980b57cec5SDimitry Andric   if (isa<UndefValue>(C)) {
5990b57cec5SDimitry Andric     GenericValue Result;
6000b57cec5SDimitry Andric     switch (C->getType()->getTypeID()) {
6010b57cec5SDimitry Andric     default:
6020b57cec5SDimitry Andric       break;
6030b57cec5SDimitry Andric     case Type::IntegerTyID:
6040b57cec5SDimitry Andric     case Type::X86_FP80TyID:
6050b57cec5SDimitry Andric     case Type::FP128TyID:
6060b57cec5SDimitry Andric     case Type::PPC_FP128TyID:
6070b57cec5SDimitry Andric       // Although the value is undefined, we still have to construct an APInt
6080b57cec5SDimitry Andric       // with the correct bit width.
6090b57cec5SDimitry Andric       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
6100b57cec5SDimitry Andric       break;
6110b57cec5SDimitry Andric     case Type::StructTyID: {
6120b57cec5SDimitry Andric       // if the whole struct is 'undef' just reserve memory for the value.
6130b57cec5SDimitry Andric       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
6140b57cec5SDimitry Andric         unsigned int elemNum = STy->getNumElements();
6150b57cec5SDimitry Andric         Result.AggregateVal.resize(elemNum);
6160b57cec5SDimitry Andric         for (unsigned int i = 0; i < elemNum; ++i) {
6170b57cec5SDimitry Andric           Type *ElemTy = STy->getElementType(i);
6180b57cec5SDimitry Andric           if (ElemTy->isIntegerTy())
6190b57cec5SDimitry Andric             Result.AggregateVal[i].IntVal =
6200b57cec5SDimitry Andric               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
6210b57cec5SDimitry Andric           else if (ElemTy->isAggregateType()) {
6220b57cec5SDimitry Andric               const Constant *ElemUndef = UndefValue::get(ElemTy);
6230b57cec5SDimitry Andric               Result.AggregateVal[i] = getConstantValue(ElemUndef);
6240b57cec5SDimitry Andric             }
6250b57cec5SDimitry Andric           }
6260b57cec5SDimitry Andric         }
6270b57cec5SDimitry Andric       }
6280b57cec5SDimitry Andric       break;
629*5ffd83dbSDimitry Andric       case Type::ScalableVectorTyID:
630*5ffd83dbSDimitry Andric         report_fatal_error(
631*5ffd83dbSDimitry Andric             "Scalable vector support not yet implemented in ExecutionEngine");
632*5ffd83dbSDimitry Andric       case Type::FixedVectorTyID:
6330b57cec5SDimitry Andric         // if the whole vector is 'undef' just reserve memory for the value.
634*5ffd83dbSDimitry Andric         auto *VTy = cast<FixedVectorType>(C->getType());
6350b57cec5SDimitry Andric         Type *ElemTy = VTy->getElementType();
6360b57cec5SDimitry Andric         unsigned int elemNum = VTy->getNumElements();
6370b57cec5SDimitry Andric         Result.AggregateVal.resize(elemNum);
6380b57cec5SDimitry Andric         if (ElemTy->isIntegerTy())
6390b57cec5SDimitry Andric           for (unsigned int i = 0; i < elemNum; ++i)
6400b57cec5SDimitry Andric             Result.AggregateVal[i].IntVal =
6410b57cec5SDimitry Andric                 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
6420b57cec5SDimitry Andric         break;
6430b57cec5SDimitry Andric     }
6440b57cec5SDimitry Andric     return Result;
6450b57cec5SDimitry Andric   }
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric   // Otherwise, if the value is a ConstantExpr...
6480b57cec5SDimitry Andric   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
6490b57cec5SDimitry Andric     Constant *Op0 = CE->getOperand(0);
6500b57cec5SDimitry Andric     switch (CE->getOpcode()) {
6510b57cec5SDimitry Andric     case Instruction::GetElementPtr: {
6520b57cec5SDimitry Andric       // Compute the index
6530b57cec5SDimitry Andric       GenericValue Result = getConstantValue(Op0);
6540b57cec5SDimitry Andric       APInt Offset(DL.getPointerSizeInBits(), 0);
6550b57cec5SDimitry Andric       cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric       char* tmp = (char*) Result.PointerVal;
6580b57cec5SDimitry Andric       Result = PTOGV(tmp + Offset.getSExtValue());
6590b57cec5SDimitry Andric       return Result;
6600b57cec5SDimitry Andric     }
6610b57cec5SDimitry Andric     case Instruction::Trunc: {
6620b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6630b57cec5SDimitry Andric       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
6640b57cec5SDimitry Andric       GV.IntVal = GV.IntVal.trunc(BitWidth);
6650b57cec5SDimitry Andric       return GV;
6660b57cec5SDimitry Andric     }
6670b57cec5SDimitry Andric     case Instruction::ZExt: {
6680b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6690b57cec5SDimitry Andric       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
6700b57cec5SDimitry Andric       GV.IntVal = GV.IntVal.zext(BitWidth);
6710b57cec5SDimitry Andric       return GV;
6720b57cec5SDimitry Andric     }
6730b57cec5SDimitry Andric     case Instruction::SExt: {
6740b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6750b57cec5SDimitry Andric       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
6760b57cec5SDimitry Andric       GV.IntVal = GV.IntVal.sext(BitWidth);
6770b57cec5SDimitry Andric       return GV;
6780b57cec5SDimitry Andric     }
6790b57cec5SDimitry Andric     case Instruction::FPTrunc: {
6800b57cec5SDimitry Andric       // FIXME long double
6810b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6820b57cec5SDimitry Andric       GV.FloatVal = float(GV.DoubleVal);
6830b57cec5SDimitry Andric       return GV;
6840b57cec5SDimitry Andric     }
6850b57cec5SDimitry Andric     case Instruction::FPExt:{
6860b57cec5SDimitry Andric       // FIXME long double
6870b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6880b57cec5SDimitry Andric       GV.DoubleVal = double(GV.FloatVal);
6890b57cec5SDimitry Andric       return GV;
6900b57cec5SDimitry Andric     }
6910b57cec5SDimitry Andric     case Instruction::UIToFP: {
6920b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
6930b57cec5SDimitry Andric       if (CE->getType()->isFloatTy())
6940b57cec5SDimitry Andric         GV.FloatVal = float(GV.IntVal.roundToDouble());
6950b57cec5SDimitry Andric       else if (CE->getType()->isDoubleTy())
6960b57cec5SDimitry Andric         GV.DoubleVal = GV.IntVal.roundToDouble();
6970b57cec5SDimitry Andric       else if (CE->getType()->isX86_FP80Ty()) {
6980b57cec5SDimitry Andric         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
6990b57cec5SDimitry Andric         (void)apf.convertFromAPInt(GV.IntVal,
7000b57cec5SDimitry Andric                                    false,
7010b57cec5SDimitry Andric                                    APFloat::rmNearestTiesToEven);
7020b57cec5SDimitry Andric         GV.IntVal = apf.bitcastToAPInt();
7030b57cec5SDimitry Andric       }
7040b57cec5SDimitry Andric       return GV;
7050b57cec5SDimitry Andric     }
7060b57cec5SDimitry Andric     case Instruction::SIToFP: {
7070b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
7080b57cec5SDimitry Andric       if (CE->getType()->isFloatTy())
7090b57cec5SDimitry Andric         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
7100b57cec5SDimitry Andric       else if (CE->getType()->isDoubleTy())
7110b57cec5SDimitry Andric         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
7120b57cec5SDimitry Andric       else if (CE->getType()->isX86_FP80Ty()) {
7130b57cec5SDimitry Andric         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
7140b57cec5SDimitry Andric         (void)apf.convertFromAPInt(GV.IntVal,
7150b57cec5SDimitry Andric                                    true,
7160b57cec5SDimitry Andric                                    APFloat::rmNearestTiesToEven);
7170b57cec5SDimitry Andric         GV.IntVal = apf.bitcastToAPInt();
7180b57cec5SDimitry Andric       }
7190b57cec5SDimitry Andric       return GV;
7200b57cec5SDimitry Andric     }
7210b57cec5SDimitry Andric     case Instruction::FPToUI: // double->APInt conversion handles sign
7220b57cec5SDimitry Andric     case Instruction::FPToSI: {
7230b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
7240b57cec5SDimitry Andric       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
7250b57cec5SDimitry Andric       if (Op0->getType()->isFloatTy())
7260b57cec5SDimitry Andric         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
7270b57cec5SDimitry Andric       else if (Op0->getType()->isDoubleTy())
7280b57cec5SDimitry Andric         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
7290b57cec5SDimitry Andric       else if (Op0->getType()->isX86_FP80Ty()) {
7300b57cec5SDimitry Andric         APFloat apf = APFloat(APFloat::x87DoubleExtended(), GV.IntVal);
7310b57cec5SDimitry Andric         uint64_t v;
7320b57cec5SDimitry Andric         bool ignored;
7330b57cec5SDimitry Andric         (void)apf.convertToInteger(makeMutableArrayRef(v), BitWidth,
7340b57cec5SDimitry Andric                                    CE->getOpcode()==Instruction::FPToSI,
7350b57cec5SDimitry Andric                                    APFloat::rmTowardZero, &ignored);
7360b57cec5SDimitry Andric         GV.IntVal = v; // endian?
7370b57cec5SDimitry Andric       }
7380b57cec5SDimitry Andric       return GV;
7390b57cec5SDimitry Andric     }
7400b57cec5SDimitry Andric     case Instruction::PtrToInt: {
7410b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
7420b57cec5SDimitry Andric       uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
7430b57cec5SDimitry Andric       assert(PtrWidth <= 64 && "Bad pointer width");
7440b57cec5SDimitry Andric       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
7450b57cec5SDimitry Andric       uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
7460b57cec5SDimitry Andric       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
7470b57cec5SDimitry Andric       return GV;
7480b57cec5SDimitry Andric     }
7490b57cec5SDimitry Andric     case Instruction::IntToPtr: {
7500b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
7510b57cec5SDimitry Andric       uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
7520b57cec5SDimitry Andric       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
7530b57cec5SDimitry Andric       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
7540b57cec5SDimitry Andric       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
7550b57cec5SDimitry Andric       return GV;
7560b57cec5SDimitry Andric     }
7570b57cec5SDimitry Andric     case Instruction::BitCast: {
7580b57cec5SDimitry Andric       GenericValue GV = getConstantValue(Op0);
7590b57cec5SDimitry Andric       Type* DestTy = CE->getType();
7600b57cec5SDimitry Andric       switch (Op0->getType()->getTypeID()) {
7610b57cec5SDimitry Andric         default: llvm_unreachable("Invalid bitcast operand");
7620b57cec5SDimitry Andric         case Type::IntegerTyID:
7630b57cec5SDimitry Andric           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
7640b57cec5SDimitry Andric           if (DestTy->isFloatTy())
7650b57cec5SDimitry Andric             GV.FloatVal = GV.IntVal.bitsToFloat();
7660b57cec5SDimitry Andric           else if (DestTy->isDoubleTy())
7670b57cec5SDimitry Andric             GV.DoubleVal = GV.IntVal.bitsToDouble();
7680b57cec5SDimitry Andric           break;
7690b57cec5SDimitry Andric         case Type::FloatTyID:
7700b57cec5SDimitry Andric           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
7710b57cec5SDimitry Andric           GV.IntVal = APInt::floatToBits(GV.FloatVal);
7720b57cec5SDimitry Andric           break;
7730b57cec5SDimitry Andric         case Type::DoubleTyID:
7740b57cec5SDimitry Andric           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
7750b57cec5SDimitry Andric           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
7760b57cec5SDimitry Andric           break;
7770b57cec5SDimitry Andric         case Type::PointerTyID:
7780b57cec5SDimitry Andric           assert(DestTy->isPointerTy() && "Invalid bitcast");
7790b57cec5SDimitry Andric           break; // getConstantValue(Op0)  above already converted it
7800b57cec5SDimitry Andric       }
7810b57cec5SDimitry Andric       return GV;
7820b57cec5SDimitry Andric     }
7830b57cec5SDimitry Andric     case Instruction::Add:
7840b57cec5SDimitry Andric     case Instruction::FAdd:
7850b57cec5SDimitry Andric     case Instruction::Sub:
7860b57cec5SDimitry Andric     case Instruction::FSub:
7870b57cec5SDimitry Andric     case Instruction::Mul:
7880b57cec5SDimitry Andric     case Instruction::FMul:
7890b57cec5SDimitry Andric     case Instruction::UDiv:
7900b57cec5SDimitry Andric     case Instruction::SDiv:
7910b57cec5SDimitry Andric     case Instruction::URem:
7920b57cec5SDimitry Andric     case Instruction::SRem:
7930b57cec5SDimitry Andric     case Instruction::And:
7940b57cec5SDimitry Andric     case Instruction::Or:
7950b57cec5SDimitry Andric     case Instruction::Xor: {
7960b57cec5SDimitry Andric       GenericValue LHS = getConstantValue(Op0);
7970b57cec5SDimitry Andric       GenericValue RHS = getConstantValue(CE->getOperand(1));
7980b57cec5SDimitry Andric       GenericValue GV;
7990b57cec5SDimitry Andric       switch (CE->getOperand(0)->getType()->getTypeID()) {
8000b57cec5SDimitry Andric       default: llvm_unreachable("Bad add type!");
8010b57cec5SDimitry Andric       case Type::IntegerTyID:
8020b57cec5SDimitry Andric         switch (CE->getOpcode()) {
8030b57cec5SDimitry Andric           default: llvm_unreachable("Invalid integer opcode");
8040b57cec5SDimitry Andric           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
8050b57cec5SDimitry Andric           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
8060b57cec5SDimitry Andric           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
8070b57cec5SDimitry Andric           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
8080b57cec5SDimitry Andric           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
8090b57cec5SDimitry Andric           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
8100b57cec5SDimitry Andric           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
8110b57cec5SDimitry Andric           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
8120b57cec5SDimitry Andric           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
8130b57cec5SDimitry Andric           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
8140b57cec5SDimitry Andric         }
8150b57cec5SDimitry Andric         break;
8160b57cec5SDimitry Andric       case Type::FloatTyID:
8170b57cec5SDimitry Andric         switch (CE->getOpcode()) {
8180b57cec5SDimitry Andric           default: llvm_unreachable("Invalid float opcode");
8190b57cec5SDimitry Andric           case Instruction::FAdd:
8200b57cec5SDimitry Andric             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
8210b57cec5SDimitry Andric           case Instruction::FSub:
8220b57cec5SDimitry Andric             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
8230b57cec5SDimitry Andric           case Instruction::FMul:
8240b57cec5SDimitry Andric             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
8250b57cec5SDimitry Andric           case Instruction::FDiv:
8260b57cec5SDimitry Andric             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
8270b57cec5SDimitry Andric           case Instruction::FRem:
8280b57cec5SDimitry Andric             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
8290b57cec5SDimitry Andric         }
8300b57cec5SDimitry Andric         break;
8310b57cec5SDimitry Andric       case Type::DoubleTyID:
8320b57cec5SDimitry Andric         switch (CE->getOpcode()) {
8330b57cec5SDimitry Andric           default: llvm_unreachable("Invalid double opcode");
8340b57cec5SDimitry Andric           case Instruction::FAdd:
8350b57cec5SDimitry Andric             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
8360b57cec5SDimitry Andric           case Instruction::FSub:
8370b57cec5SDimitry Andric             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
8380b57cec5SDimitry Andric           case Instruction::FMul:
8390b57cec5SDimitry Andric             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
8400b57cec5SDimitry Andric           case Instruction::FDiv:
8410b57cec5SDimitry Andric             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
8420b57cec5SDimitry Andric           case Instruction::FRem:
8430b57cec5SDimitry Andric             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
8440b57cec5SDimitry Andric         }
8450b57cec5SDimitry Andric         break;
8460b57cec5SDimitry Andric       case Type::X86_FP80TyID:
8470b57cec5SDimitry Andric       case Type::PPC_FP128TyID:
8480b57cec5SDimitry Andric       case Type::FP128TyID: {
8490b57cec5SDimitry Andric         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
8500b57cec5SDimitry Andric         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
8510b57cec5SDimitry Andric         switch (CE->getOpcode()) {
8520b57cec5SDimitry Andric           default: llvm_unreachable("Invalid long double opcode");
8530b57cec5SDimitry Andric           case Instruction::FAdd:
8540b57cec5SDimitry Andric             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
8550b57cec5SDimitry Andric             GV.IntVal = apfLHS.bitcastToAPInt();
8560b57cec5SDimitry Andric             break;
8570b57cec5SDimitry Andric           case Instruction::FSub:
8580b57cec5SDimitry Andric             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
8590b57cec5SDimitry Andric                             APFloat::rmNearestTiesToEven);
8600b57cec5SDimitry Andric             GV.IntVal = apfLHS.bitcastToAPInt();
8610b57cec5SDimitry Andric             break;
8620b57cec5SDimitry Andric           case Instruction::FMul:
8630b57cec5SDimitry Andric             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
8640b57cec5SDimitry Andric                             APFloat::rmNearestTiesToEven);
8650b57cec5SDimitry Andric             GV.IntVal = apfLHS.bitcastToAPInt();
8660b57cec5SDimitry Andric             break;
8670b57cec5SDimitry Andric           case Instruction::FDiv:
8680b57cec5SDimitry Andric             apfLHS.divide(APFloat(Sem, RHS.IntVal),
8690b57cec5SDimitry Andric                           APFloat::rmNearestTiesToEven);
8700b57cec5SDimitry Andric             GV.IntVal = apfLHS.bitcastToAPInt();
8710b57cec5SDimitry Andric             break;
8720b57cec5SDimitry Andric           case Instruction::FRem:
8730b57cec5SDimitry Andric             apfLHS.mod(APFloat(Sem, RHS.IntVal));
8740b57cec5SDimitry Andric             GV.IntVal = apfLHS.bitcastToAPInt();
8750b57cec5SDimitry Andric             break;
8760b57cec5SDimitry Andric           }
8770b57cec5SDimitry Andric         }
8780b57cec5SDimitry Andric         break;
8790b57cec5SDimitry Andric       }
8800b57cec5SDimitry Andric       return GV;
8810b57cec5SDimitry Andric     }
8820b57cec5SDimitry Andric     default:
8830b57cec5SDimitry Andric       break;
8840b57cec5SDimitry Andric     }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric     SmallString<256> Msg;
8870b57cec5SDimitry Andric     raw_svector_ostream OS(Msg);
8880b57cec5SDimitry Andric     OS << "ConstantExpr not handled: " << *CE;
8890b57cec5SDimitry Andric     report_fatal_error(OS.str());
8900b57cec5SDimitry Andric   }
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric   // Otherwise, we have a simple constant.
8930b57cec5SDimitry Andric   GenericValue Result;
8940b57cec5SDimitry Andric   switch (C->getType()->getTypeID()) {
8950b57cec5SDimitry Andric   case Type::FloatTyID:
8960b57cec5SDimitry Andric     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
8970b57cec5SDimitry Andric     break;
8980b57cec5SDimitry Andric   case Type::DoubleTyID:
8990b57cec5SDimitry Andric     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
9000b57cec5SDimitry Andric     break;
9010b57cec5SDimitry Andric   case Type::X86_FP80TyID:
9020b57cec5SDimitry Andric   case Type::FP128TyID:
9030b57cec5SDimitry Andric   case Type::PPC_FP128TyID:
9040b57cec5SDimitry Andric     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
9050b57cec5SDimitry Andric     break;
9060b57cec5SDimitry Andric   case Type::IntegerTyID:
9070b57cec5SDimitry Andric     Result.IntVal = cast<ConstantInt>(C)->getValue();
9080b57cec5SDimitry Andric     break;
9090b57cec5SDimitry Andric   case Type::PointerTyID:
9100b57cec5SDimitry Andric     while (auto *A = dyn_cast<GlobalAlias>(C)) {
9110b57cec5SDimitry Andric       C = A->getAliasee();
9120b57cec5SDimitry Andric     }
9130b57cec5SDimitry Andric     if (isa<ConstantPointerNull>(C))
9140b57cec5SDimitry Andric       Result.PointerVal = nullptr;
9150b57cec5SDimitry Andric     else if (const Function *F = dyn_cast<Function>(C))
9160b57cec5SDimitry Andric       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
9170b57cec5SDimitry Andric     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
9180b57cec5SDimitry Andric       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
9190b57cec5SDimitry Andric     else
9200b57cec5SDimitry Andric       llvm_unreachable("Unknown constant pointer type!");
9210b57cec5SDimitry Andric     break;
922*5ffd83dbSDimitry Andric   case Type::ScalableVectorTyID:
923*5ffd83dbSDimitry Andric     report_fatal_error(
924*5ffd83dbSDimitry Andric         "Scalable vector support not yet implemented in ExecutionEngine");
925*5ffd83dbSDimitry Andric   case Type::FixedVectorTyID: {
9260b57cec5SDimitry Andric     unsigned elemNum;
9270b57cec5SDimitry Andric     Type* ElemTy;
9280b57cec5SDimitry Andric     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
9290b57cec5SDimitry Andric     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
9300b57cec5SDimitry Andric     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric     if (CDV) {
9330b57cec5SDimitry Andric         elemNum = CDV->getNumElements();
9340b57cec5SDimitry Andric         ElemTy = CDV->getElementType();
9350b57cec5SDimitry Andric     } else if (CV || CAZ) {
936*5ffd83dbSDimitry Andric       auto *VTy = cast<FixedVectorType>(C->getType());
9370b57cec5SDimitry Andric       elemNum = VTy->getNumElements();
9380b57cec5SDimitry Andric       ElemTy = VTy->getElementType();
9390b57cec5SDimitry Andric     } else {
9400b57cec5SDimitry Andric         llvm_unreachable("Unknown constant vector type!");
9410b57cec5SDimitry Andric     }
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric     Result.AggregateVal.resize(elemNum);
9440b57cec5SDimitry Andric     // Check if vector holds floats.
9450b57cec5SDimitry Andric     if(ElemTy->isFloatTy()) {
9460b57cec5SDimitry Andric       if (CAZ) {
9470b57cec5SDimitry Andric         GenericValue floatZero;
9480b57cec5SDimitry Andric         floatZero.FloatVal = 0.f;
9490b57cec5SDimitry Andric         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
9500b57cec5SDimitry Andric                   floatZero);
9510b57cec5SDimitry Andric         break;
9520b57cec5SDimitry Andric       }
9530b57cec5SDimitry Andric       if(CV) {
9540b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
9550b57cec5SDimitry Andric           if (!isa<UndefValue>(CV->getOperand(i)))
9560b57cec5SDimitry Andric             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
9570b57cec5SDimitry Andric               CV->getOperand(i))->getValueAPF().convertToFloat();
9580b57cec5SDimitry Andric         break;
9590b57cec5SDimitry Andric       }
9600b57cec5SDimitry Andric       if(CDV)
9610b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
9620b57cec5SDimitry Andric           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric       break;
9650b57cec5SDimitry Andric     }
9660b57cec5SDimitry Andric     // Check if vector holds doubles.
9670b57cec5SDimitry Andric     if (ElemTy->isDoubleTy()) {
9680b57cec5SDimitry Andric       if (CAZ) {
9690b57cec5SDimitry Andric         GenericValue doubleZero;
9700b57cec5SDimitry Andric         doubleZero.DoubleVal = 0.0;
9710b57cec5SDimitry Andric         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
9720b57cec5SDimitry Andric                   doubleZero);
9730b57cec5SDimitry Andric         break;
9740b57cec5SDimitry Andric       }
9750b57cec5SDimitry Andric       if(CV) {
9760b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
9770b57cec5SDimitry Andric           if (!isa<UndefValue>(CV->getOperand(i)))
9780b57cec5SDimitry Andric             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
9790b57cec5SDimitry Andric               CV->getOperand(i))->getValueAPF().convertToDouble();
9800b57cec5SDimitry Andric         break;
9810b57cec5SDimitry Andric       }
9820b57cec5SDimitry Andric       if(CDV)
9830b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
9840b57cec5SDimitry Andric           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric       break;
9870b57cec5SDimitry Andric     }
9880b57cec5SDimitry Andric     // Check if vector holds integers.
9890b57cec5SDimitry Andric     if (ElemTy->isIntegerTy()) {
9900b57cec5SDimitry Andric       if (CAZ) {
9910b57cec5SDimitry Andric         GenericValue intZero;
9920b57cec5SDimitry Andric         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
9930b57cec5SDimitry Andric         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
9940b57cec5SDimitry Andric                   intZero);
9950b57cec5SDimitry Andric         break;
9960b57cec5SDimitry Andric       }
9970b57cec5SDimitry Andric       if(CV) {
9980b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
9990b57cec5SDimitry Andric           if (!isa<UndefValue>(CV->getOperand(i)))
10000b57cec5SDimitry Andric             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
10010b57cec5SDimitry Andric                                             CV->getOperand(i))->getValue();
10020b57cec5SDimitry Andric           else {
10030b57cec5SDimitry Andric             Result.AggregateVal[i].IntVal =
10040b57cec5SDimitry Andric               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
10050b57cec5SDimitry Andric           }
10060b57cec5SDimitry Andric         break;
10070b57cec5SDimitry Andric       }
10080b57cec5SDimitry Andric       if(CDV)
10090b57cec5SDimitry Andric         for (unsigned i = 0; i < elemNum; ++i)
10100b57cec5SDimitry Andric           Result.AggregateVal[i].IntVal = APInt(
10110b57cec5SDimitry Andric             CDV->getElementType()->getPrimitiveSizeInBits(),
10120b57cec5SDimitry Andric             CDV->getElementAsInteger(i));
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric       break;
10150b57cec5SDimitry Andric     }
10160b57cec5SDimitry Andric     llvm_unreachable("Unknown constant pointer type!");
1017*5ffd83dbSDimitry Andric   } break;
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric   default:
10200b57cec5SDimitry Andric     SmallString<256> Msg;
10210b57cec5SDimitry Andric     raw_svector_ostream OS(Msg);
10220b57cec5SDimitry Andric     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
10230b57cec5SDimitry Andric     report_fatal_error(OS.str());
10240b57cec5SDimitry Andric   }
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric   return Result;
10270b57cec5SDimitry Andric }
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
10300b57cec5SDimitry Andric                                          GenericValue *Ptr, Type *Ty) {
10310b57cec5SDimitry Andric   const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric   switch (Ty->getTypeID()) {
10340b57cec5SDimitry Andric   default:
10350b57cec5SDimitry Andric     dbgs() << "Cannot store value of type " << *Ty << "!\n";
10360b57cec5SDimitry Andric     break;
10370b57cec5SDimitry Andric   case Type::IntegerTyID:
10380b57cec5SDimitry Andric     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
10390b57cec5SDimitry Andric     break;
10400b57cec5SDimitry Andric   case Type::FloatTyID:
10410b57cec5SDimitry Andric     *((float*)Ptr) = Val.FloatVal;
10420b57cec5SDimitry Andric     break;
10430b57cec5SDimitry Andric   case Type::DoubleTyID:
10440b57cec5SDimitry Andric     *((double*)Ptr) = Val.DoubleVal;
10450b57cec5SDimitry Andric     break;
10460b57cec5SDimitry Andric   case Type::X86_FP80TyID:
10470b57cec5SDimitry Andric     memcpy(Ptr, Val.IntVal.getRawData(), 10);
10480b57cec5SDimitry Andric     break;
10490b57cec5SDimitry Andric   case Type::PointerTyID:
10500b57cec5SDimitry Andric     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
10510b57cec5SDimitry Andric     if (StoreBytes != sizeof(PointerTy))
10520b57cec5SDimitry Andric       memset(&(Ptr->PointerVal), 0, StoreBytes);
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric     *((PointerTy*)Ptr) = Val.PointerVal;
10550b57cec5SDimitry Andric     break;
1056*5ffd83dbSDimitry Andric   case Type::FixedVectorTyID:
1057*5ffd83dbSDimitry Andric   case Type::ScalableVectorTyID:
10580b57cec5SDimitry Andric     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
10590b57cec5SDimitry Andric       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
10600b57cec5SDimitry Andric         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
10610b57cec5SDimitry Andric       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
10620b57cec5SDimitry Andric         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
10630b57cec5SDimitry Andric       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
10640b57cec5SDimitry Andric         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
10650b57cec5SDimitry Andric         StoreIntToMemory(Val.AggregateVal[i].IntVal,
10660b57cec5SDimitry Andric           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
10670b57cec5SDimitry Andric       }
10680b57cec5SDimitry Andric     }
10690b57cec5SDimitry Andric     break;
10700b57cec5SDimitry Andric   }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
10730b57cec5SDimitry Andric     // Host and target are different endian - reverse the stored bytes.
10740b57cec5SDimitry Andric     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
10750b57cec5SDimitry Andric }
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric /// FIXME: document
10780b57cec5SDimitry Andric ///
10790b57cec5SDimitry Andric void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
10800b57cec5SDimitry Andric                                           GenericValue *Ptr,
10810b57cec5SDimitry Andric                                           Type *Ty) {
10820b57cec5SDimitry Andric   const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
10830b57cec5SDimitry Andric 
10840b57cec5SDimitry Andric   switch (Ty->getTypeID()) {
10850b57cec5SDimitry Andric   case Type::IntegerTyID:
10860b57cec5SDimitry Andric     // An APInt with all words initially zero.
10870b57cec5SDimitry Andric     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
10880b57cec5SDimitry Andric     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
10890b57cec5SDimitry Andric     break;
10900b57cec5SDimitry Andric   case Type::FloatTyID:
10910b57cec5SDimitry Andric     Result.FloatVal = *((float*)Ptr);
10920b57cec5SDimitry Andric     break;
10930b57cec5SDimitry Andric   case Type::DoubleTyID:
10940b57cec5SDimitry Andric     Result.DoubleVal = *((double*)Ptr);
10950b57cec5SDimitry Andric     break;
10960b57cec5SDimitry Andric   case Type::PointerTyID:
10970b57cec5SDimitry Andric     Result.PointerVal = *((PointerTy*)Ptr);
10980b57cec5SDimitry Andric     break;
10990b57cec5SDimitry Andric   case Type::X86_FP80TyID: {
11000b57cec5SDimitry Andric     // This is endian dependent, but it will only work on x86 anyway.
11010b57cec5SDimitry Andric     // FIXME: Will not trap if loading a signaling NaN.
11020b57cec5SDimitry Andric     uint64_t y[2];
11030b57cec5SDimitry Andric     memcpy(y, Ptr, 10);
11040b57cec5SDimitry Andric     Result.IntVal = APInt(80, y);
11050b57cec5SDimitry Andric     break;
11060b57cec5SDimitry Andric   }
1107*5ffd83dbSDimitry Andric   case Type::ScalableVectorTyID:
1108*5ffd83dbSDimitry Andric     report_fatal_error(
1109*5ffd83dbSDimitry Andric         "Scalable vector support not yet implemented in ExecutionEngine");
1110*5ffd83dbSDimitry Andric   case Type::FixedVectorTyID: {
1111*5ffd83dbSDimitry Andric     auto *VT = cast<FixedVectorType>(Ty);
11120b57cec5SDimitry Andric     Type *ElemT = VT->getElementType();
11130b57cec5SDimitry Andric     const unsigned numElems = VT->getNumElements();
11140b57cec5SDimitry Andric     if (ElemT->isFloatTy()) {
11150b57cec5SDimitry Andric       Result.AggregateVal.resize(numElems);
11160b57cec5SDimitry Andric       for (unsigned i = 0; i < numElems; ++i)
11170b57cec5SDimitry Andric         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
11180b57cec5SDimitry Andric     }
11190b57cec5SDimitry Andric     if (ElemT->isDoubleTy()) {
11200b57cec5SDimitry Andric       Result.AggregateVal.resize(numElems);
11210b57cec5SDimitry Andric       for (unsigned i = 0; i < numElems; ++i)
11220b57cec5SDimitry Andric         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
11230b57cec5SDimitry Andric     }
11240b57cec5SDimitry Andric     if (ElemT->isIntegerTy()) {
11250b57cec5SDimitry Andric       GenericValue intZero;
11260b57cec5SDimitry Andric       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
11270b57cec5SDimitry Andric       intZero.IntVal = APInt(elemBitWidth, 0);
11280b57cec5SDimitry Andric       Result.AggregateVal.resize(numElems, intZero);
11290b57cec5SDimitry Andric       for (unsigned i = 0; i < numElems; ++i)
11300b57cec5SDimitry Andric         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
11310b57cec5SDimitry Andric           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
11320b57cec5SDimitry Andric     }
11330b57cec5SDimitry Andric   break;
11340b57cec5SDimitry Andric   }
11350b57cec5SDimitry Andric   default:
11360b57cec5SDimitry Andric     SmallString<256> Msg;
11370b57cec5SDimitry Andric     raw_svector_ostream OS(Msg);
11380b57cec5SDimitry Andric     OS << "Cannot load value of type " << *Ty << "!";
11390b57cec5SDimitry Andric     report_fatal_error(OS.str());
11400b57cec5SDimitry Andric   }
11410b57cec5SDimitry Andric }
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
11440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
11450b57cec5SDimitry Andric   LLVM_DEBUG(Init->dump());
11460b57cec5SDimitry Andric   if (isa<UndefValue>(Init))
11470b57cec5SDimitry Andric     return;
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
11500b57cec5SDimitry Andric     unsigned ElementSize =
11510b57cec5SDimitry Andric         getDataLayout().getTypeAllocSize(CP->getType()->getElementType());
11520b57cec5SDimitry Andric     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
11530b57cec5SDimitry Andric       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
11540b57cec5SDimitry Andric     return;
11550b57cec5SDimitry Andric   }
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric   if (isa<ConstantAggregateZero>(Init)) {
11580b57cec5SDimitry Andric     memset(Addr, 0, (size_t)getDataLayout().getTypeAllocSize(Init->getType()));
11590b57cec5SDimitry Andric     return;
11600b57cec5SDimitry Andric   }
11610b57cec5SDimitry Andric 
11620b57cec5SDimitry Andric   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
11630b57cec5SDimitry Andric     unsigned ElementSize =
11640b57cec5SDimitry Andric         getDataLayout().getTypeAllocSize(CPA->getType()->getElementType());
11650b57cec5SDimitry Andric     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
11660b57cec5SDimitry Andric       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
11670b57cec5SDimitry Andric     return;
11680b57cec5SDimitry Andric   }
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
11710b57cec5SDimitry Andric     const StructLayout *SL =
11720b57cec5SDimitry Andric         getDataLayout().getStructLayout(cast<StructType>(CPS->getType()));
11730b57cec5SDimitry Andric     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
11740b57cec5SDimitry Andric       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
11750b57cec5SDimitry Andric     return;
11760b57cec5SDimitry Andric   }
11770b57cec5SDimitry Andric 
11780b57cec5SDimitry Andric   if (const ConstantDataSequential *CDS =
11790b57cec5SDimitry Andric                dyn_cast<ConstantDataSequential>(Init)) {
11800b57cec5SDimitry Andric     // CDS is already laid out in host memory order.
11810b57cec5SDimitry Andric     StringRef Data = CDS->getRawDataValues();
11820b57cec5SDimitry Andric     memcpy(Addr, Data.data(), Data.size());
11830b57cec5SDimitry Andric     return;
11840b57cec5SDimitry Andric   }
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric   if (Init->getType()->isFirstClassType()) {
11870b57cec5SDimitry Andric     GenericValue Val = getConstantValue(Init);
11880b57cec5SDimitry Andric     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
11890b57cec5SDimitry Andric     return;
11900b57cec5SDimitry Andric   }
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
11930b57cec5SDimitry Andric   llvm_unreachable("Unknown constant type to initialize memory with!");
11940b57cec5SDimitry Andric }
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric /// EmitGlobals - Emit all of the global variables to memory, storing their
11970b57cec5SDimitry Andric /// addresses into GlobalAddress.  This must make sure to copy the contents of
11980b57cec5SDimitry Andric /// their initializers into the memory.
11990b57cec5SDimitry Andric void ExecutionEngine::emitGlobals() {
12000b57cec5SDimitry Andric   // Loop over all of the global variables in the program, allocating the memory
12010b57cec5SDimitry Andric   // to hold them.  If there is more than one module, do a prepass over globals
12020b57cec5SDimitry Andric   // to figure out how the different modules should link together.
12030b57cec5SDimitry Andric   std::map<std::pair<std::string, Type*>,
12040b57cec5SDimitry Andric            const GlobalValue*> LinkedGlobalsMap;
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric   if (Modules.size() != 1) {
12070b57cec5SDimitry Andric     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
12080b57cec5SDimitry Andric       Module &M = *Modules[m];
12090b57cec5SDimitry Andric       for (const auto &GV : M.globals()) {
12100b57cec5SDimitry Andric         if (GV.hasLocalLinkage() || GV.isDeclaration() ||
12110b57cec5SDimitry Andric             GV.hasAppendingLinkage() || !GV.hasName())
12120b57cec5SDimitry Andric           continue;// Ignore external globals and globals with internal linkage.
12130b57cec5SDimitry Andric 
1214*5ffd83dbSDimitry Andric         const GlobalValue *&GVEntry = LinkedGlobalsMap[std::make_pair(
1215*5ffd83dbSDimitry Andric             std::string(GV.getName()), GV.getType())];
12160b57cec5SDimitry Andric 
12170b57cec5SDimitry Andric         // If this is the first time we've seen this global, it is the canonical
12180b57cec5SDimitry Andric         // version.
12190b57cec5SDimitry Andric         if (!GVEntry) {
12200b57cec5SDimitry Andric           GVEntry = &GV;
12210b57cec5SDimitry Andric           continue;
12220b57cec5SDimitry Andric         }
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric         // If the existing global is strong, never replace it.
12250b57cec5SDimitry Andric         if (GVEntry->hasExternalLinkage())
12260b57cec5SDimitry Andric           continue;
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
12290b57cec5SDimitry Andric         // symbol.  FIXME is this right for common?
12300b57cec5SDimitry Andric         if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
12310b57cec5SDimitry Andric           GVEntry = &GV;
12320b57cec5SDimitry Andric       }
12330b57cec5SDimitry Andric     }
12340b57cec5SDimitry Andric   }
12350b57cec5SDimitry Andric 
12360b57cec5SDimitry Andric   std::vector<const GlobalValue*> NonCanonicalGlobals;
12370b57cec5SDimitry Andric   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
12380b57cec5SDimitry Andric     Module &M = *Modules[m];
12390b57cec5SDimitry Andric     for (const auto &GV : M.globals()) {
12400b57cec5SDimitry Andric       // In the multi-module case, see what this global maps to.
12410b57cec5SDimitry Andric       if (!LinkedGlobalsMap.empty()) {
1242*5ffd83dbSDimitry Andric         if (const GlobalValue *GVEntry = LinkedGlobalsMap[std::make_pair(
1243*5ffd83dbSDimitry Andric                 std::string(GV.getName()), GV.getType())]) {
12440b57cec5SDimitry Andric           // If something else is the canonical global, ignore this one.
12450b57cec5SDimitry Andric           if (GVEntry != &GV) {
12460b57cec5SDimitry Andric             NonCanonicalGlobals.push_back(&GV);
12470b57cec5SDimitry Andric             continue;
12480b57cec5SDimitry Andric           }
12490b57cec5SDimitry Andric         }
12500b57cec5SDimitry Andric       }
12510b57cec5SDimitry Andric 
12520b57cec5SDimitry Andric       if (!GV.isDeclaration()) {
12530b57cec5SDimitry Andric         addGlobalMapping(&GV, getMemoryForGV(&GV));
12540b57cec5SDimitry Andric       } else {
12550b57cec5SDimitry Andric         // External variable reference. Try to use the dynamic loader to
12560b57cec5SDimitry Andric         // get a pointer to it.
1257*5ffd83dbSDimitry Andric         if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(
1258*5ffd83dbSDimitry Andric                 std::string(GV.getName())))
12590b57cec5SDimitry Andric           addGlobalMapping(&GV, SymAddr);
12600b57cec5SDimitry Andric         else {
12610b57cec5SDimitry Andric           report_fatal_error("Could not resolve external global address: "
12620b57cec5SDimitry Andric                             +GV.getName());
12630b57cec5SDimitry Andric         }
12640b57cec5SDimitry Andric       }
12650b57cec5SDimitry Andric     }
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric     // If there are multiple modules, map the non-canonical globals to their
12680b57cec5SDimitry Andric     // canonical location.
12690b57cec5SDimitry Andric     if (!NonCanonicalGlobals.empty()) {
12700b57cec5SDimitry Andric       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
12710b57cec5SDimitry Andric         const GlobalValue *GV = NonCanonicalGlobals[i];
1272*5ffd83dbSDimitry Andric         const GlobalValue *CGV = LinkedGlobalsMap[std::make_pair(
1273*5ffd83dbSDimitry Andric             std::string(GV->getName()), GV->getType())];
12740b57cec5SDimitry Andric         void *Ptr = getPointerToGlobalIfAvailable(CGV);
12750b57cec5SDimitry Andric         assert(Ptr && "Canonical global wasn't codegen'd!");
12760b57cec5SDimitry Andric         addGlobalMapping(GV, Ptr);
12770b57cec5SDimitry Andric       }
12780b57cec5SDimitry Andric     }
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric     // Now that all of the globals are set up in memory, loop through them all
12810b57cec5SDimitry Andric     // and initialize their contents.
12820b57cec5SDimitry Andric     for (const auto &GV : M.globals()) {
12830b57cec5SDimitry Andric       if (!GV.isDeclaration()) {
12840b57cec5SDimitry Andric         if (!LinkedGlobalsMap.empty()) {
1285*5ffd83dbSDimitry Andric           if (const GlobalValue *GVEntry = LinkedGlobalsMap[std::make_pair(
1286*5ffd83dbSDimitry Andric                   std::string(GV.getName()), GV.getType())])
12870b57cec5SDimitry Andric             if (GVEntry != &GV)  // Not the canonical variable.
12880b57cec5SDimitry Andric               continue;
12890b57cec5SDimitry Andric         }
1290*5ffd83dbSDimitry Andric         emitGlobalVariable(&GV);
12910b57cec5SDimitry Andric       }
12920b57cec5SDimitry Andric     }
12930b57cec5SDimitry Andric   }
12940b57cec5SDimitry Andric }
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric // EmitGlobalVariable - This method emits the specified global variable to the
12970b57cec5SDimitry Andric // address specified in GlobalAddresses, or allocates new memory if it's not
12980b57cec5SDimitry Andric // already in the map.
1299*5ffd83dbSDimitry Andric void ExecutionEngine::emitGlobalVariable(const GlobalVariable *GV) {
13000b57cec5SDimitry Andric   void *GA = getPointerToGlobalIfAvailable(GV);
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   if (!GA) {
13030b57cec5SDimitry Andric     // If it's not already specified, allocate memory for the global.
13040b57cec5SDimitry Andric     GA = getMemoryForGV(GV);
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric     // If we failed to allocate memory for this global, return.
13070b57cec5SDimitry Andric     if (!GA) return;
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric     addGlobalMapping(GV, GA);
13100b57cec5SDimitry Andric   }
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric   // Don't initialize if it's thread local, let the client do it.
13130b57cec5SDimitry Andric   if (!GV->isThreadLocal())
13140b57cec5SDimitry Andric     InitializeMemory(GV->getInitializer(), GA);
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric   Type *ElTy = GV->getValueType();
13170b57cec5SDimitry Andric   size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
13180b57cec5SDimitry Andric   NumInitBytes += (unsigned)GVSize;
13190b57cec5SDimitry Andric   ++NumGlobals;
13200b57cec5SDimitry Andric }
1321