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