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