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