1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the common interface used by the various execution engine
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ExecutionEngine/GenericValue.h"
19 #include "llvm/ExecutionEngine/JITMemoryManager.h"
20 #include "llvm/ExecutionEngine/ObjectCache.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Operator.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include "llvm/Object/Archive.h"
28 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/DynamicLibrary.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Host.h"
33 #include "llvm/Support/MutexGuard.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetMachine.h"
41 #define DEBUG_TYPE "jit"
43 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
44 STATISTIC(NumGlobals , "Number of global vars initialized");
46 // Pin the vtable to this file.
47 void ObjectCache::anchor() {}
48 void ObjectBuffer::anchor() {}
49 void ObjectBufferStream::anchor() {}
51 ExecutionEngine *(*ExecutionEngine::JITCtor)(
52 std::unique_ptr<Module> M,
53 std::string *ErrorStr,
54 JITMemoryManager *JMM,
56 TargetMachine *TM) = nullptr;
57 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
58 std::unique_ptr<Module >M,
59 std::string *ErrorStr,
60 RTDyldMemoryManager *MCJMM,
61 TargetMachine *TM) = nullptr;
62 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
63 std::string *ErrorStr) =nullptr;
65 ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
67 LazyFunctionCreator(nullptr) {
68 CompilingLazily = false;
69 GVCompilationDisabled = false;
70 SymbolSearchingDisabled = false;
72 // IR module verification is enabled by default in debug builds, and disabled
73 // by default in release builds.
77 VerifyModules = false;
80 assert(M && "Module is null?");
81 Modules.push_back(std::move(M));
84 ExecutionEngine::~ExecutionEngine() {
85 clearAllGlobalMappings();
89 /// \brief Helper class which uses a value handler to automatically deletes the
90 /// memory block when the GlobalVariable is destroyed.
91 class GVMemoryBlock : public CallbackVH {
92 GVMemoryBlock(const GlobalVariable *GV)
93 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
96 /// \brief Returns the address the GlobalVariable should be written into. The
97 /// GVMemoryBlock object prefixes that.
98 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
99 Type *ElTy = GV->getType()->getElementType();
100 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
101 void *RawMemory = ::operator new(
102 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
103 TD.getPreferredAlignment(GV))
105 new(RawMemory) GVMemoryBlock(GV);
106 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
109 void deleted() override {
110 // We allocated with operator new and with some extra memory hanging off the
111 // end, so don't just delete this. I'm not sure if this is actually
113 this->~GVMemoryBlock();
114 ::operator delete(this);
117 } // anonymous namespace
119 char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
120 return GVMemoryBlock::Create(GV, *getDataLayout());
123 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
124 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
128 ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
129 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
132 void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
133 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
136 bool ExecutionEngine::removeModule(Module *M) {
137 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
138 Module *Found = I->get();
142 clearGlobalMappingsFromModule(M);
149 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
150 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
151 if (Function *F = Modules[i]->getFunction(FnName))
158 void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
159 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
162 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
164 if (I == GlobalAddressMap.end())
168 GlobalAddressMap.erase(I);
171 GlobalAddressReverseMap.erase(OldVal);
175 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
176 MutexGuard locked(lock);
178 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
179 << "\' to [" << Addr << "]\n";);
180 void *&CurVal = EEState.getGlobalAddressMap()[GV];
181 assert((!CurVal || !Addr) && "GlobalMapping already established!");
184 // If we are using the reverse mapping, add it too.
185 if (!EEState.getGlobalAddressReverseMap().empty()) {
186 AssertingVH<const GlobalValue> &V =
187 EEState.getGlobalAddressReverseMap()[Addr];
188 assert((!V || !GV) && "GlobalMapping already established!");
193 void ExecutionEngine::clearAllGlobalMappings() {
194 MutexGuard locked(lock);
196 EEState.getGlobalAddressMap().clear();
197 EEState.getGlobalAddressReverseMap().clear();
200 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
201 MutexGuard locked(lock);
203 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
204 EEState.RemoveMapping(FI);
205 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
207 EEState.RemoveMapping(GI);
210 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
211 MutexGuard locked(lock);
213 ExecutionEngineState::GlobalAddressMapTy &Map =
214 EEState.getGlobalAddressMap();
216 // Deleting from the mapping?
218 return EEState.RemoveMapping(GV);
220 void *&CurVal = Map[GV];
221 void *OldVal = CurVal;
223 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
224 EEState.getGlobalAddressReverseMap().erase(CurVal);
227 // If we are using the reverse mapping, add it too.
228 if (!EEState.getGlobalAddressReverseMap().empty()) {
229 AssertingVH<const GlobalValue> &V =
230 EEState.getGlobalAddressReverseMap()[Addr];
231 assert((!V || !GV) && "GlobalMapping already established!");
237 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
238 MutexGuard locked(lock);
240 ExecutionEngineState::GlobalAddressMapTy::iterator I =
241 EEState.getGlobalAddressMap().find(GV);
242 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
245 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
246 MutexGuard locked(lock);
248 // If we haven't computed the reverse mapping yet, do so first.
249 if (EEState.getGlobalAddressReverseMap().empty()) {
250 for (ExecutionEngineState::GlobalAddressMapTy::iterator
251 I = EEState.getGlobalAddressMap().begin(),
252 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
253 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
254 I->second, I->first));
257 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
258 EEState.getGlobalAddressReverseMap().find(Addr);
259 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
264 std::unique_ptr<char[]> Array;
265 std::vector<std::unique_ptr<char[]>> Values;
267 /// Turn a vector of strings into a nice argv style array of pointers to null
268 /// terminated strings.
269 void *reset(LLVMContext &C, ExecutionEngine *EE,
270 const std::vector<std::string> &InputArgv);
272 } // anonymous namespace
273 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
274 const std::vector<std::string> &InputArgv) {
275 Values.clear(); // Free the old contents.
276 Values.reserve(InputArgv.size());
277 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
278 Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
280 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
281 Type *SBytePtr = Type::getInt8PtrTy(C);
283 for (unsigned i = 0; i != InputArgv.size(); ++i) {
284 unsigned Size = InputArgv[i].size()+1;
285 auto Dest = make_unique<char[]>(Size);
286 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
288 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
291 // Endian safe: Array[i] = (PointerTy)Dest;
292 EE->StoreValueToMemory(PTOGV(Dest.get()),
293 (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
294 Values.push_back(std::move(Dest));
298 EE->StoreValueToMemory(PTOGV(nullptr),
299 (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
304 void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
306 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
307 GlobalVariable *GV = module.getNamedGlobal(Name);
309 // If this global has internal linkage, or if it has a use, then it must be
310 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
311 // this is the case, don't execute any of the global ctors, __main will do
313 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
315 // Should be an array of '{ i32, void ()* }' structs. The first value is
316 // the init priority, which we ignore.
317 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
320 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
321 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
324 Constant *FP = CS->getOperand(1);
325 if (FP->isNullValue())
326 continue; // Found a sentinal value, ignore.
328 // Strip off constant expression casts.
329 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
331 FP = CE->getOperand(0);
333 // Execute the ctor/dtor function!
334 if (Function *F = dyn_cast<Function>(FP))
335 runFunction(F, std::vector<GenericValue>());
337 // FIXME: It is marginally lame that we just do nothing here if we see an
338 // entry we don't recognize. It might not be unreasonable for the verifier
339 // to not even allow this and just assert here.
343 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
344 // Execute global ctors/dtors for each module in the program.
345 for (std::unique_ptr<Module> &M : Modules)
346 runStaticConstructorsDestructors(*M, isDtors);
350 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
351 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
352 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
353 for (unsigned i = 0; i < PtrSize; ++i)
354 if (*(i + (uint8_t*)Loc))
360 int ExecutionEngine::runFunctionAsMain(Function *Fn,
361 const std::vector<std::string> &argv,
362 const char * const * envp) {
363 std::vector<GenericValue> GVArgs;
365 GVArgc.IntVal = APInt(32, argv.size());
368 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
369 FunctionType *FTy = Fn->getFunctionType();
370 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
372 // Check the argument types.
374 report_fatal_error("Invalid number of arguments of main() supplied");
375 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
376 report_fatal_error("Invalid type for third argument of main() supplied");
377 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
378 report_fatal_error("Invalid type for second argument of main() supplied");
379 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
380 report_fatal_error("Invalid type for first argument of main() supplied");
381 if (!FTy->getReturnType()->isIntegerTy() &&
382 !FTy->getReturnType()->isVoidTy())
383 report_fatal_error("Invalid return type of main() supplied");
388 GVArgs.push_back(GVArgc); // Arg #0 = argc.
391 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
392 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
393 "argv[0] was null after CreateArgv");
395 std::vector<std::string> EnvVars;
396 for (unsigned i = 0; envp[i]; ++i)
397 EnvVars.push_back(envp[i]);
399 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
404 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
407 void EngineBuilder::InitEngine() {
408 WhichEngine = EngineKind::Either;
410 OptLevel = CodeGenOpt::Default;
413 Options = TargetOptions();
414 AllocateGVsWithCode = false;
415 RelocModel = Reloc::Default;
416 CMModel = CodeModel::JITDefault;
419 // IR module verification is enabled by default in debug builds, and disabled
420 // by default in release builds.
422 VerifyModules = true;
424 VerifyModules = false;
428 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
429 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
431 // Make sure we can resolve symbols in the program as well. The zero arg
432 // to the function tells DynamicLibrary to load the program, not a library.
433 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
436 assert(!(JMM && MCJMM));
438 // If the user specified a memory manager but didn't specify which engine to
439 // create, we assume they only want the JIT, and we fail if they only want
442 if (WhichEngine & EngineKind::JIT)
443 WhichEngine = EngineKind::JIT;
446 *ErrorStr = "Cannot create an interpreter with a memory manager.";
451 if (MCJMM && ! UseMCJIT) {
454 "Cannot create a legacy JIT with a runtime dyld memory "
459 // Unless the interpreter was explicitly selected or the JIT is not linked,
461 if ((WhichEngine & EngineKind::JIT) && TheTM) {
462 Triple TT(M->getTargetTriple());
463 if (!TM->getTarget().hasJIT()) {
464 errs() << "WARNING: This target JIT is not designed for the host"
465 << " you are running. If bad things happen, please choose"
466 << " a different -march switch.\n";
469 ExecutionEngine *EE = nullptr;
470 if (UseMCJIT && ExecutionEngine::MCJITCtor)
471 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
472 MCJMM ? MCJMM : JMM, TheTM.release());
473 else if (ExecutionEngine::JITCtor)
474 EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
475 AllocateGVsWithCode, TheTM.release());
478 EE->setVerifyModules(VerifyModules);
483 // If we can't make a JIT and we didn't request one specifically, try making
484 // an interpreter instead.
485 if (WhichEngine & EngineKind::Interpreter) {
486 if (ExecutionEngine::InterpCtor)
487 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
489 *ErrorStr = "Interpreter has not been linked in.";
493 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
494 !ExecutionEngine::MCJITCtor) {
496 *ErrorStr = "JIT has not been linked in.";
502 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
503 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
504 return getPointerToFunction(F);
506 MutexGuard locked(lock);
507 if (void *P = EEState.getGlobalAddressMap()[GV])
510 // Global variable might have been added since interpreter started.
511 if (GlobalVariable *GVar =
512 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
513 EmitGlobalVariable(GVar);
515 llvm_unreachable("Global hasn't had an address allocated yet!");
517 return EEState.getGlobalAddressMap()[GV];
520 /// \brief Converts a Constant* into a GenericValue, including handling of
521 /// ConstantExpr values.
522 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
523 // If its undefined, return the garbage.
524 if (isa<UndefValue>(C)) {
526 switch (C->getType()->getTypeID()) {
529 case Type::IntegerTyID:
530 case Type::X86_FP80TyID:
531 case Type::FP128TyID:
532 case Type::PPC_FP128TyID:
533 // Although the value is undefined, we still have to construct an APInt
534 // with the correct bit width.
535 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
537 case Type::StructTyID: {
538 // if the whole struct is 'undef' just reserve memory for the value.
539 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
540 unsigned int elemNum = STy->getNumElements();
541 Result.AggregateVal.resize(elemNum);
542 for (unsigned int i = 0; i < elemNum; ++i) {
543 Type *ElemTy = STy->getElementType(i);
544 if (ElemTy->isIntegerTy())
545 Result.AggregateVal[i].IntVal =
546 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
547 else if (ElemTy->isAggregateType()) {
548 const Constant *ElemUndef = UndefValue::get(ElemTy);
549 Result.AggregateVal[i] = getConstantValue(ElemUndef);
555 case Type::VectorTyID:
556 // if the whole vector is 'undef' just reserve memory for the value.
557 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
558 const Type *ElemTy = VTy->getElementType();
559 unsigned int elemNum = VTy->getNumElements();
560 Result.AggregateVal.resize(elemNum);
561 if (ElemTy->isIntegerTy())
562 for (unsigned int i = 0; i < elemNum; ++i)
563 Result.AggregateVal[i].IntVal =
564 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
570 // Otherwise, if the value is a ConstantExpr...
571 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
572 Constant *Op0 = CE->getOperand(0);
573 switch (CE->getOpcode()) {
574 case Instruction::GetElementPtr: {
576 GenericValue Result = getConstantValue(Op0);
577 APInt Offset(DL->getPointerSizeInBits(), 0);
578 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
580 char* tmp = (char*) Result.PointerVal;
581 Result = PTOGV(tmp + Offset.getSExtValue());
584 case Instruction::Trunc: {
585 GenericValue GV = getConstantValue(Op0);
586 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
587 GV.IntVal = GV.IntVal.trunc(BitWidth);
590 case Instruction::ZExt: {
591 GenericValue GV = getConstantValue(Op0);
592 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
593 GV.IntVal = GV.IntVal.zext(BitWidth);
596 case Instruction::SExt: {
597 GenericValue GV = getConstantValue(Op0);
598 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
599 GV.IntVal = GV.IntVal.sext(BitWidth);
602 case Instruction::FPTrunc: {
604 GenericValue GV = getConstantValue(Op0);
605 GV.FloatVal = float(GV.DoubleVal);
608 case Instruction::FPExt:{
610 GenericValue GV = getConstantValue(Op0);
611 GV.DoubleVal = double(GV.FloatVal);
614 case Instruction::UIToFP: {
615 GenericValue GV = getConstantValue(Op0);
616 if (CE->getType()->isFloatTy())
617 GV.FloatVal = float(GV.IntVal.roundToDouble());
618 else if (CE->getType()->isDoubleTy())
619 GV.DoubleVal = GV.IntVal.roundToDouble();
620 else if (CE->getType()->isX86_FP80Ty()) {
621 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
622 (void)apf.convertFromAPInt(GV.IntVal,
624 APFloat::rmNearestTiesToEven);
625 GV.IntVal = apf.bitcastToAPInt();
629 case Instruction::SIToFP: {
630 GenericValue GV = getConstantValue(Op0);
631 if (CE->getType()->isFloatTy())
632 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
633 else if (CE->getType()->isDoubleTy())
634 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
635 else if (CE->getType()->isX86_FP80Ty()) {
636 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
637 (void)apf.convertFromAPInt(GV.IntVal,
639 APFloat::rmNearestTiesToEven);
640 GV.IntVal = apf.bitcastToAPInt();
644 case Instruction::FPToUI: // double->APInt conversion handles sign
645 case Instruction::FPToSI: {
646 GenericValue GV = getConstantValue(Op0);
647 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
648 if (Op0->getType()->isFloatTy())
649 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
650 else if (Op0->getType()->isDoubleTy())
651 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
652 else if (Op0->getType()->isX86_FP80Ty()) {
653 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
656 (void)apf.convertToInteger(&v, BitWidth,
657 CE->getOpcode()==Instruction::FPToSI,
658 APFloat::rmTowardZero, &ignored);
659 GV.IntVal = v; // endian?
663 case Instruction::PtrToInt: {
664 GenericValue GV = getConstantValue(Op0);
665 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
666 assert(PtrWidth <= 64 && "Bad pointer width");
667 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
668 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
669 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
672 case Instruction::IntToPtr: {
673 GenericValue GV = getConstantValue(Op0);
674 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
675 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
676 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
677 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
680 case Instruction::BitCast: {
681 GenericValue GV = getConstantValue(Op0);
682 Type* DestTy = CE->getType();
683 switch (Op0->getType()->getTypeID()) {
684 default: llvm_unreachable("Invalid bitcast operand");
685 case Type::IntegerTyID:
686 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
687 if (DestTy->isFloatTy())
688 GV.FloatVal = GV.IntVal.bitsToFloat();
689 else if (DestTy->isDoubleTy())
690 GV.DoubleVal = GV.IntVal.bitsToDouble();
692 case Type::FloatTyID:
693 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
694 GV.IntVal = APInt::floatToBits(GV.FloatVal);
696 case Type::DoubleTyID:
697 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
698 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
700 case Type::PointerTyID:
701 assert(DestTy->isPointerTy() && "Invalid bitcast");
702 break; // getConstantValue(Op0) above already converted it
706 case Instruction::Add:
707 case Instruction::FAdd:
708 case Instruction::Sub:
709 case Instruction::FSub:
710 case Instruction::Mul:
711 case Instruction::FMul:
712 case Instruction::UDiv:
713 case Instruction::SDiv:
714 case Instruction::URem:
715 case Instruction::SRem:
716 case Instruction::And:
717 case Instruction::Or:
718 case Instruction::Xor: {
719 GenericValue LHS = getConstantValue(Op0);
720 GenericValue RHS = getConstantValue(CE->getOperand(1));
722 switch (CE->getOperand(0)->getType()->getTypeID()) {
723 default: llvm_unreachable("Bad add type!");
724 case Type::IntegerTyID:
725 switch (CE->getOpcode()) {
726 default: llvm_unreachable("Invalid integer opcode");
727 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
728 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
729 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
730 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
731 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
732 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
733 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
734 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
735 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
736 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
739 case Type::FloatTyID:
740 switch (CE->getOpcode()) {
741 default: llvm_unreachable("Invalid float opcode");
742 case Instruction::FAdd:
743 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
744 case Instruction::FSub:
745 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
746 case Instruction::FMul:
747 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
748 case Instruction::FDiv:
749 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
750 case Instruction::FRem:
751 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
754 case Type::DoubleTyID:
755 switch (CE->getOpcode()) {
756 default: llvm_unreachable("Invalid double opcode");
757 case Instruction::FAdd:
758 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
759 case Instruction::FSub:
760 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
761 case Instruction::FMul:
762 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
763 case Instruction::FDiv:
764 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
765 case Instruction::FRem:
766 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
769 case Type::X86_FP80TyID:
770 case Type::PPC_FP128TyID:
771 case Type::FP128TyID: {
772 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
773 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
774 switch (CE->getOpcode()) {
775 default: llvm_unreachable("Invalid long double opcode");
776 case Instruction::FAdd:
777 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
778 GV.IntVal = apfLHS.bitcastToAPInt();
780 case Instruction::FSub:
781 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
782 APFloat::rmNearestTiesToEven);
783 GV.IntVal = apfLHS.bitcastToAPInt();
785 case Instruction::FMul:
786 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
787 APFloat::rmNearestTiesToEven);
788 GV.IntVal = apfLHS.bitcastToAPInt();
790 case Instruction::FDiv:
791 apfLHS.divide(APFloat(Sem, RHS.IntVal),
792 APFloat::rmNearestTiesToEven);
793 GV.IntVal = apfLHS.bitcastToAPInt();
795 case Instruction::FRem:
796 apfLHS.mod(APFloat(Sem, RHS.IntVal),
797 APFloat::rmNearestTiesToEven);
798 GV.IntVal = apfLHS.bitcastToAPInt();
810 SmallString<256> Msg;
811 raw_svector_ostream OS(Msg);
812 OS << "ConstantExpr not handled: " << *CE;
813 report_fatal_error(OS.str());
816 // Otherwise, we have a simple constant.
818 switch (C->getType()->getTypeID()) {
819 case Type::FloatTyID:
820 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
822 case Type::DoubleTyID:
823 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
825 case Type::X86_FP80TyID:
826 case Type::FP128TyID:
827 case Type::PPC_FP128TyID:
828 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
830 case Type::IntegerTyID:
831 Result.IntVal = cast<ConstantInt>(C)->getValue();
833 case Type::PointerTyID:
834 if (isa<ConstantPointerNull>(C))
835 Result.PointerVal = nullptr;
836 else if (const Function *F = dyn_cast<Function>(C))
837 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
838 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
839 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
840 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
841 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
842 BA->getBasicBlock())));
844 llvm_unreachable("Unknown constant pointer type!");
846 case Type::VectorTyID: {
849 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
850 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
851 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
854 elemNum = CDV->getNumElements();
855 ElemTy = CDV->getElementType();
856 } else if (CV || CAZ) {
857 VectorType* VTy = dyn_cast<VectorType>(C->getType());
858 elemNum = VTy->getNumElements();
859 ElemTy = VTy->getElementType();
861 llvm_unreachable("Unknown constant vector type!");
864 Result.AggregateVal.resize(elemNum);
865 // Check if vector holds floats.
866 if(ElemTy->isFloatTy()) {
868 GenericValue floatZero;
869 floatZero.FloatVal = 0.f;
870 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
875 for (unsigned i = 0; i < elemNum; ++i)
876 if (!isa<UndefValue>(CV->getOperand(i)))
877 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
878 CV->getOperand(i))->getValueAPF().convertToFloat();
882 for (unsigned i = 0; i < elemNum; ++i)
883 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
887 // Check if vector holds doubles.
888 if (ElemTy->isDoubleTy()) {
890 GenericValue doubleZero;
891 doubleZero.DoubleVal = 0.0;
892 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
897 for (unsigned i = 0; i < elemNum; ++i)
898 if (!isa<UndefValue>(CV->getOperand(i)))
899 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
900 CV->getOperand(i))->getValueAPF().convertToDouble();
904 for (unsigned i = 0; i < elemNum; ++i)
905 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
909 // Check if vector holds integers.
910 if (ElemTy->isIntegerTy()) {
912 GenericValue intZero;
913 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
914 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
919 for (unsigned i = 0; i < elemNum; ++i)
920 if (!isa<UndefValue>(CV->getOperand(i)))
921 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
922 CV->getOperand(i))->getValue();
924 Result.AggregateVal[i].IntVal =
925 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
930 for (unsigned i = 0; i < elemNum; ++i)
931 Result.AggregateVal[i].IntVal = APInt(
932 CDV->getElementType()->getPrimitiveSizeInBits(),
933 CDV->getElementAsInteger(i));
937 llvm_unreachable("Unknown constant pointer type!");
942 SmallString<256> Msg;
943 raw_svector_ostream OS(Msg);
944 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
945 report_fatal_error(OS.str());
951 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
952 /// with the integer held in IntVal.
953 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
954 unsigned StoreBytes) {
955 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
956 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
958 if (sys::IsLittleEndianHost) {
959 // Little-endian host - the source is ordered from LSB to MSB. Order the
960 // destination from LSB to MSB: Do a straight copy.
961 memcpy(Dst, Src, StoreBytes);
963 // Big-endian host - the source is an array of 64 bit words ordered from
964 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
965 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
966 while (StoreBytes > sizeof(uint64_t)) {
967 StoreBytes -= sizeof(uint64_t);
968 // May not be aligned so use memcpy.
969 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
970 Src += sizeof(uint64_t);
973 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
977 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
978 GenericValue *Ptr, Type *Ty) {
979 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
981 switch (Ty->getTypeID()) {
983 dbgs() << "Cannot store value of type " << *Ty << "!\n";
985 case Type::IntegerTyID:
986 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
988 case Type::FloatTyID:
989 *((float*)Ptr) = Val.FloatVal;
991 case Type::DoubleTyID:
992 *((double*)Ptr) = Val.DoubleVal;
994 case Type::X86_FP80TyID:
995 memcpy(Ptr, Val.IntVal.getRawData(), 10);
997 case Type::PointerTyID:
998 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
999 if (StoreBytes != sizeof(PointerTy))
1000 memset(&(Ptr->PointerVal), 0, StoreBytes);
1002 *((PointerTy*)Ptr) = Val.PointerVal;
1004 case Type::VectorTyID:
1005 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1006 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1007 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1008 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1009 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1010 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1011 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1012 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1013 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1019 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
1020 // Host and target are different endian - reverse the stored bytes.
1021 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1024 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1025 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1026 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1027 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1028 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1029 const_cast<uint64_t *>(IntVal.getRawData()));
1031 if (sys::IsLittleEndianHost)
1032 // Little-endian host - the destination must be ordered from LSB to MSB.
1033 // The source is ordered from LSB to MSB: Do a straight copy.
1034 memcpy(Dst, Src, LoadBytes);
1036 // Big-endian - the destination is an array of 64 bit words ordered from
1037 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1038 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1040 while (LoadBytes > sizeof(uint64_t)) {
1041 LoadBytes -= sizeof(uint64_t);
1042 // May not be aligned so use memcpy.
1043 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1044 Dst += sizeof(uint64_t);
1047 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1053 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
1056 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
1058 switch (Ty->getTypeID()) {
1059 case Type::IntegerTyID:
1060 // An APInt with all words initially zero.
1061 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1062 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1064 case Type::FloatTyID:
1065 Result.FloatVal = *((float*)Ptr);
1067 case Type::DoubleTyID:
1068 Result.DoubleVal = *((double*)Ptr);
1070 case Type::PointerTyID:
1071 Result.PointerVal = *((PointerTy*)Ptr);
1073 case Type::X86_FP80TyID: {
1074 // This is endian dependent, but it will only work on x86 anyway.
1075 // FIXME: Will not trap if loading a signaling NaN.
1078 Result.IntVal = APInt(80, y);
1081 case Type::VectorTyID: {
1082 const VectorType *VT = cast<VectorType>(Ty);
1083 const Type *ElemT = VT->getElementType();
1084 const unsigned numElems = VT->getNumElements();
1085 if (ElemT->isFloatTy()) {
1086 Result.AggregateVal.resize(numElems);
1087 for (unsigned i = 0; i < numElems; ++i)
1088 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1090 if (ElemT->isDoubleTy()) {
1091 Result.AggregateVal.resize(numElems);
1092 for (unsigned i = 0; i < numElems; ++i)
1093 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1095 if (ElemT->isIntegerTy()) {
1096 GenericValue intZero;
1097 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1098 intZero.IntVal = APInt(elemBitWidth, 0);
1099 Result.AggregateVal.resize(numElems, intZero);
1100 for (unsigned i = 0; i < numElems; ++i)
1101 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1102 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1107 SmallString<256> Msg;
1108 raw_svector_ostream OS(Msg);
1109 OS << "Cannot load value of type " << *Ty << "!";
1110 report_fatal_error(OS.str());
1114 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
1115 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1116 DEBUG(Init->dump());
1117 if (isa<UndefValue>(Init))
1120 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1121 unsigned ElementSize =
1122 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
1123 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1124 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1128 if (isa<ConstantAggregateZero>(Init)) {
1129 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
1133 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1134 unsigned ElementSize =
1135 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
1136 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1137 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1141 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1142 const StructLayout *SL =
1143 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
1144 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1145 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1149 if (const ConstantDataSequential *CDS =
1150 dyn_cast<ConstantDataSequential>(Init)) {
1151 // CDS is already laid out in host memory order.
1152 StringRef Data = CDS->getRawDataValues();
1153 memcpy(Addr, Data.data(), Data.size());
1157 if (Init->getType()->isFirstClassType()) {
1158 GenericValue Val = getConstantValue(Init);
1159 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1163 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1164 llvm_unreachable("Unknown constant type to initialize memory with!");
1167 /// EmitGlobals - Emit all of the global variables to memory, storing their
1168 /// addresses into GlobalAddress. This must make sure to copy the contents of
1169 /// their initializers into the memory.
1170 void ExecutionEngine::emitGlobals() {
1171 // Loop over all of the global variables in the program, allocating the memory
1172 // to hold them. If there is more than one module, do a prepass over globals
1173 // to figure out how the different modules should link together.
1174 std::map<std::pair<std::string, Type*>,
1175 const GlobalValue*> LinkedGlobalsMap;
1177 if (Modules.size() != 1) {
1178 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1179 Module &M = *Modules[m];
1180 for (const auto &GV : M.globals()) {
1181 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1182 GV.hasAppendingLinkage() || !GV.hasName())
1183 continue;// Ignore external globals and globals with internal linkage.
1185 const GlobalValue *&GVEntry =
1186 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
1188 // If this is the first time we've seen this global, it is the canonical
1195 // If the existing global is strong, never replace it.
1196 if (GVEntry->hasExternalLinkage())
1199 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1200 // symbol. FIXME is this right for common?
1201 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1207 std::vector<const GlobalValue*> NonCanonicalGlobals;
1208 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1209 Module &M = *Modules[m];
1210 for (const auto &GV : M.globals()) {
1211 // In the multi-module case, see what this global maps to.
1212 if (!LinkedGlobalsMap.empty()) {
1213 if (const GlobalValue *GVEntry =
1214 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
1215 // If something else is the canonical global, ignore this one.
1216 if (GVEntry != &GV) {
1217 NonCanonicalGlobals.push_back(&GV);
1223 if (!GV.isDeclaration()) {
1224 addGlobalMapping(&GV, getMemoryForGV(&GV));
1226 // External variable reference. Try to use the dynamic loader to
1227 // get a pointer to it.
1229 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1230 addGlobalMapping(&GV, SymAddr);
1232 report_fatal_error("Could not resolve external global address: "
1238 // If there are multiple modules, map the non-canonical globals to their
1239 // canonical location.
1240 if (!NonCanonicalGlobals.empty()) {
1241 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1242 const GlobalValue *GV = NonCanonicalGlobals[i];
1243 const GlobalValue *CGV =
1244 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1245 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1246 assert(Ptr && "Canonical global wasn't codegen'd!");
1247 addGlobalMapping(GV, Ptr);
1251 // Now that all of the globals are set up in memory, loop through them all
1252 // and initialize their contents.
1253 for (const auto &GV : M.globals()) {
1254 if (!GV.isDeclaration()) {
1255 if (!LinkedGlobalsMap.empty()) {
1256 if (const GlobalValue *GVEntry =
1257 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1258 if (GVEntry != &GV) // Not the canonical variable.
1261 EmitGlobalVariable(&GV);
1267 // EmitGlobalVariable - This method emits the specified global variable to the
1268 // address specified in GlobalAddresses, or allocates new memory if it's not
1269 // already in the map.
1270 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1271 void *GA = getPointerToGlobalIfAvailable(GV);
1274 // If it's not already specified, allocate memory for the global.
1275 GA = getMemoryForGV(GV);
1277 // If we failed to allocate memory for this global, return.
1280 addGlobalMapping(GV, GA);
1283 // Don't initialize if it's thread local, let the client do it.
1284 if (!GV->isThreadLocal())
1285 InitializeMemory(GV->getInitializer(), GA);
1287 Type *ElTy = GV->getType()->getElementType();
1288 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
1289 NumInitBytes += (unsigned)GVSize;
1293 ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1294 : EE(EE), GlobalAddressMap(this) {
1298 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
1299 return &EES->EE.lock;
1302 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1303 const GlobalValue *Old) {
1304 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1305 EES->GlobalAddressReverseMap.erase(OldVal);
1308 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1309 const GlobalValue *,
1310 const GlobalValue *) {
1311 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1312 " RAUW on a value it has a global mapping for.");