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