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.");
127 void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
128 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
131 bool ExecutionEngine::removeModule(Module *M) {
132 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
133 Module *Found = I->get();
137 clearGlobalMappingsFromModule(M);
144 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
145 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
146 if (Function *F = Modules[i]->getFunction(FnName))
153 void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
154 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
157 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
159 if (I == GlobalAddressMap.end())
163 GlobalAddressMap.erase(I);
166 GlobalAddressReverseMap.erase(OldVal);
170 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
171 MutexGuard locked(lock);
173 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
174 << "\' to [" << Addr << "]\n";);
175 void *&CurVal = EEState.getGlobalAddressMap()[GV];
176 assert((!CurVal || !Addr) && "GlobalMapping already established!");
179 // If we are using the reverse mapping, add it too.
180 if (!EEState.getGlobalAddressReverseMap().empty()) {
181 AssertingVH<const GlobalValue> &V =
182 EEState.getGlobalAddressReverseMap()[Addr];
183 assert((!V || !GV) && "GlobalMapping already established!");
188 void ExecutionEngine::clearAllGlobalMappings() {
189 MutexGuard locked(lock);
191 EEState.getGlobalAddressMap().clear();
192 EEState.getGlobalAddressReverseMap().clear();
195 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
196 MutexGuard locked(lock);
198 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
199 EEState.RemoveMapping(FI);
200 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
202 EEState.RemoveMapping(GI);
205 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
206 MutexGuard locked(lock);
208 ExecutionEngineState::GlobalAddressMapTy &Map =
209 EEState.getGlobalAddressMap();
211 // Deleting from the mapping?
213 return EEState.RemoveMapping(GV);
215 void *&CurVal = Map[GV];
216 void *OldVal = CurVal;
218 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
219 EEState.getGlobalAddressReverseMap().erase(CurVal);
222 // If we are using the reverse mapping, add it too.
223 if (!EEState.getGlobalAddressReverseMap().empty()) {
224 AssertingVH<const GlobalValue> &V =
225 EEState.getGlobalAddressReverseMap()[Addr];
226 assert((!V || !GV) && "GlobalMapping already established!");
232 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
233 MutexGuard locked(lock);
235 ExecutionEngineState::GlobalAddressMapTy::iterator I =
236 EEState.getGlobalAddressMap().find(GV);
237 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
240 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
241 MutexGuard locked(lock);
243 // If we haven't computed the reverse mapping yet, do so first.
244 if (EEState.getGlobalAddressReverseMap().empty()) {
245 for (ExecutionEngineState::GlobalAddressMapTy::iterator
246 I = EEState.getGlobalAddressMap().begin(),
247 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
248 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
249 I->second, I->first));
252 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
253 EEState.getGlobalAddressReverseMap().find(Addr);
254 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
259 std::unique_ptr<char[]> Array;
260 std::vector<std::unique_ptr<char[]>> Values;
262 /// Turn a vector of strings into a nice argv style array of pointers to null
263 /// terminated strings.
264 void *reset(LLVMContext &C, ExecutionEngine *EE,
265 const std::vector<std::string> &InputArgv);
267 } // anonymous namespace
268 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
269 const std::vector<std::string> &InputArgv) {
270 Values.clear(); // Free the old contents.
271 Values.reserve(InputArgv.size());
272 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
273 Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
275 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
276 Type *SBytePtr = Type::getInt8PtrTy(C);
278 for (unsigned i = 0; i != InputArgv.size(); ++i) {
279 unsigned Size = InputArgv[i].size()+1;
280 auto Dest = make_unique<char[]>(Size);
281 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
283 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
286 // Endian safe: Array[i] = (PointerTy)Dest;
287 EE->StoreValueToMemory(PTOGV(Dest.get()),
288 (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
289 Values.push_back(std::move(Dest));
293 EE->StoreValueToMemory(PTOGV(nullptr),
294 (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
299 void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
301 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
302 GlobalVariable *GV = module.getNamedGlobal(Name);
304 // If this global has internal linkage, or if it has a use, then it must be
305 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
306 // this is the case, don't execute any of the global ctors, __main will do
308 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
310 // Should be an array of '{ i32, void ()* }' structs. The first value is
311 // the init priority, which we ignore.
312 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
315 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
316 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
319 Constant *FP = CS->getOperand(1);
320 if (FP->isNullValue())
321 continue; // Found a sentinal value, ignore.
323 // Strip off constant expression casts.
324 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
326 FP = CE->getOperand(0);
328 // Execute the ctor/dtor function!
329 if (Function *F = dyn_cast<Function>(FP))
330 runFunction(F, std::vector<GenericValue>());
332 // FIXME: It is marginally lame that we just do nothing here if we see an
333 // entry we don't recognize. It might not be unreasonable for the verifier
334 // to not even allow this and just assert here.
338 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
339 // Execute global ctors/dtors for each module in the program.
340 for (std::unique_ptr<Module> &M : Modules)
341 runStaticConstructorsDestructors(*M, isDtors);
345 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
346 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
347 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
348 for (unsigned i = 0; i < PtrSize; ++i)
349 if (*(i + (uint8_t*)Loc))
355 int ExecutionEngine::runFunctionAsMain(Function *Fn,
356 const std::vector<std::string> &argv,
357 const char * const * envp) {
358 std::vector<GenericValue> GVArgs;
360 GVArgc.IntVal = APInt(32, argv.size());
363 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
364 FunctionType *FTy = Fn->getFunctionType();
365 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
367 // Check the argument types.
369 report_fatal_error("Invalid number of arguments of main() supplied");
370 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
371 report_fatal_error("Invalid type for third argument of main() supplied");
372 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
373 report_fatal_error("Invalid type for second argument of main() supplied");
374 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
375 report_fatal_error("Invalid type for first argument of main() supplied");
376 if (!FTy->getReturnType()->isIntegerTy() &&
377 !FTy->getReturnType()->isVoidTy())
378 report_fatal_error("Invalid return type of main() supplied");
383 GVArgs.push_back(GVArgc); // Arg #0 = argc.
386 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
387 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
388 "argv[0] was null after CreateArgv");
390 std::vector<std::string> EnvVars;
391 for (unsigned i = 0; envp[i]; ++i)
392 EnvVars.push_back(envp[i]);
394 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
399 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
402 void EngineBuilder::InitEngine() {
403 WhichEngine = EngineKind::Either;
405 OptLevel = CodeGenOpt::Default;
408 Options = TargetOptions();
409 AllocateGVsWithCode = false;
410 RelocModel = Reloc::Default;
411 CMModel = CodeModel::JITDefault;
414 // IR module verification is enabled by default in debug builds, and disabled
415 // by default in release builds.
417 VerifyModules = true;
419 VerifyModules = false;
423 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
424 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
426 // Make sure we can resolve symbols in the program as well. The zero arg
427 // to the function tells DynamicLibrary to load the program, not a library.
428 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
431 assert(!(JMM && MCJMM));
433 // If the user specified a memory manager but didn't specify which engine to
434 // create, we assume they only want the JIT, and we fail if they only want
437 if (WhichEngine & EngineKind::JIT)
438 WhichEngine = EngineKind::JIT;
441 *ErrorStr = "Cannot create an interpreter with a memory manager.";
446 if (MCJMM && ! UseMCJIT) {
449 "Cannot create a legacy JIT with a runtime dyld memory "
454 // Unless the interpreter was explicitly selected or the JIT is not linked,
456 if ((WhichEngine & EngineKind::JIT) && TheTM) {
457 Triple TT(M->getTargetTriple());
458 if (!TM->getTarget().hasJIT()) {
459 errs() << "WARNING: This target JIT is not designed for the host"
460 << " you are running. If bad things happen, please choose"
461 << " a different -march switch.\n";
464 ExecutionEngine *EE = nullptr;
465 if (UseMCJIT && ExecutionEngine::MCJITCtor)
466 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
467 MCJMM ? MCJMM : JMM, TheTM.release());
468 else if (ExecutionEngine::JITCtor)
469 EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
470 AllocateGVsWithCode, TheTM.release());
473 EE->setVerifyModules(VerifyModules);
478 // If we can't make a JIT and we didn't request one specifically, try making
479 // an interpreter instead.
480 if (WhichEngine & EngineKind::Interpreter) {
481 if (ExecutionEngine::InterpCtor)
482 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
484 *ErrorStr = "Interpreter has not been linked in.";
488 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
489 !ExecutionEngine::MCJITCtor) {
491 *ErrorStr = "JIT has not been linked in.";
497 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
498 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
499 return getPointerToFunction(F);
501 MutexGuard locked(lock);
502 if (void *P = EEState.getGlobalAddressMap()[GV])
505 // Global variable might have been added since interpreter started.
506 if (GlobalVariable *GVar =
507 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
508 EmitGlobalVariable(GVar);
510 llvm_unreachable("Global hasn't had an address allocated yet!");
512 return EEState.getGlobalAddressMap()[GV];
515 /// \brief Converts a Constant* into a GenericValue, including handling of
516 /// ConstantExpr values.
517 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
518 // If its undefined, return the garbage.
519 if (isa<UndefValue>(C)) {
521 switch (C->getType()->getTypeID()) {
524 case Type::IntegerTyID:
525 case Type::X86_FP80TyID:
526 case Type::FP128TyID:
527 case Type::PPC_FP128TyID:
528 // Although the value is undefined, we still have to construct an APInt
529 // with the correct bit width.
530 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
532 case Type::StructTyID: {
533 // if the whole struct is 'undef' just reserve memory for the value.
534 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
535 unsigned int elemNum = STy->getNumElements();
536 Result.AggregateVal.resize(elemNum);
537 for (unsigned int i = 0; i < elemNum; ++i) {
538 Type *ElemTy = STy->getElementType(i);
539 if (ElemTy->isIntegerTy())
540 Result.AggregateVal[i].IntVal =
541 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
542 else if (ElemTy->isAggregateType()) {
543 const Constant *ElemUndef = UndefValue::get(ElemTy);
544 Result.AggregateVal[i] = getConstantValue(ElemUndef);
550 case Type::VectorTyID:
551 // if the whole vector is 'undef' just reserve memory for the value.
552 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
553 const Type *ElemTy = VTy->getElementType();
554 unsigned int elemNum = VTy->getNumElements();
555 Result.AggregateVal.resize(elemNum);
556 if (ElemTy->isIntegerTy())
557 for (unsigned int i = 0; i < elemNum; ++i)
558 Result.AggregateVal[i].IntVal =
559 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
565 // Otherwise, if the value is a ConstantExpr...
566 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
567 Constant *Op0 = CE->getOperand(0);
568 switch (CE->getOpcode()) {
569 case Instruction::GetElementPtr: {
571 GenericValue Result = getConstantValue(Op0);
572 APInt Offset(DL->getPointerSizeInBits(), 0);
573 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
575 char* tmp = (char*) Result.PointerVal;
576 Result = PTOGV(tmp + Offset.getSExtValue());
579 case Instruction::Trunc: {
580 GenericValue GV = getConstantValue(Op0);
581 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
582 GV.IntVal = GV.IntVal.trunc(BitWidth);
585 case Instruction::ZExt: {
586 GenericValue GV = getConstantValue(Op0);
587 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
588 GV.IntVal = GV.IntVal.zext(BitWidth);
591 case Instruction::SExt: {
592 GenericValue GV = getConstantValue(Op0);
593 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
594 GV.IntVal = GV.IntVal.sext(BitWidth);
597 case Instruction::FPTrunc: {
599 GenericValue GV = getConstantValue(Op0);
600 GV.FloatVal = float(GV.DoubleVal);
603 case Instruction::FPExt:{
605 GenericValue GV = getConstantValue(Op0);
606 GV.DoubleVal = double(GV.FloatVal);
609 case Instruction::UIToFP: {
610 GenericValue GV = getConstantValue(Op0);
611 if (CE->getType()->isFloatTy())
612 GV.FloatVal = float(GV.IntVal.roundToDouble());
613 else if (CE->getType()->isDoubleTy())
614 GV.DoubleVal = GV.IntVal.roundToDouble();
615 else if (CE->getType()->isX86_FP80Ty()) {
616 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
617 (void)apf.convertFromAPInt(GV.IntVal,
619 APFloat::rmNearestTiesToEven);
620 GV.IntVal = apf.bitcastToAPInt();
624 case Instruction::SIToFP: {
625 GenericValue GV = getConstantValue(Op0);
626 if (CE->getType()->isFloatTy())
627 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
628 else if (CE->getType()->isDoubleTy())
629 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
630 else if (CE->getType()->isX86_FP80Ty()) {
631 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
632 (void)apf.convertFromAPInt(GV.IntVal,
634 APFloat::rmNearestTiesToEven);
635 GV.IntVal = apf.bitcastToAPInt();
639 case Instruction::FPToUI: // double->APInt conversion handles sign
640 case Instruction::FPToSI: {
641 GenericValue GV = getConstantValue(Op0);
642 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
643 if (Op0->getType()->isFloatTy())
644 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
645 else if (Op0->getType()->isDoubleTy())
646 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
647 else if (Op0->getType()->isX86_FP80Ty()) {
648 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
651 (void)apf.convertToInteger(&v, BitWidth,
652 CE->getOpcode()==Instruction::FPToSI,
653 APFloat::rmTowardZero, &ignored);
654 GV.IntVal = v; // endian?
658 case Instruction::PtrToInt: {
659 GenericValue GV = getConstantValue(Op0);
660 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
661 assert(PtrWidth <= 64 && "Bad pointer width");
662 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
663 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
664 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
667 case Instruction::IntToPtr: {
668 GenericValue GV = getConstantValue(Op0);
669 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
670 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
671 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
672 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
675 case Instruction::BitCast: {
676 GenericValue GV = getConstantValue(Op0);
677 Type* DestTy = CE->getType();
678 switch (Op0->getType()->getTypeID()) {
679 default: llvm_unreachable("Invalid bitcast operand");
680 case Type::IntegerTyID:
681 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
682 if (DestTy->isFloatTy())
683 GV.FloatVal = GV.IntVal.bitsToFloat();
684 else if (DestTy->isDoubleTy())
685 GV.DoubleVal = GV.IntVal.bitsToDouble();
687 case Type::FloatTyID:
688 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
689 GV.IntVal = APInt::floatToBits(GV.FloatVal);
691 case Type::DoubleTyID:
692 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
693 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
695 case Type::PointerTyID:
696 assert(DestTy->isPointerTy() && "Invalid bitcast");
697 break; // getConstantValue(Op0) above already converted it
701 case Instruction::Add:
702 case Instruction::FAdd:
703 case Instruction::Sub:
704 case Instruction::FSub:
705 case Instruction::Mul:
706 case Instruction::FMul:
707 case Instruction::UDiv:
708 case Instruction::SDiv:
709 case Instruction::URem:
710 case Instruction::SRem:
711 case Instruction::And:
712 case Instruction::Or:
713 case Instruction::Xor: {
714 GenericValue LHS = getConstantValue(Op0);
715 GenericValue RHS = getConstantValue(CE->getOperand(1));
717 switch (CE->getOperand(0)->getType()->getTypeID()) {
718 default: llvm_unreachable("Bad add type!");
719 case Type::IntegerTyID:
720 switch (CE->getOpcode()) {
721 default: llvm_unreachable("Invalid integer opcode");
722 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
723 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
724 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
725 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
726 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
727 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
728 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
729 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
730 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
731 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
734 case Type::FloatTyID:
735 switch (CE->getOpcode()) {
736 default: llvm_unreachable("Invalid float opcode");
737 case Instruction::FAdd:
738 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
739 case Instruction::FSub:
740 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
741 case Instruction::FMul:
742 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
743 case Instruction::FDiv:
744 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
745 case Instruction::FRem:
746 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
749 case Type::DoubleTyID:
750 switch (CE->getOpcode()) {
751 default: llvm_unreachable("Invalid double opcode");
752 case Instruction::FAdd:
753 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
754 case Instruction::FSub:
755 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
756 case Instruction::FMul:
757 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
758 case Instruction::FDiv:
759 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
760 case Instruction::FRem:
761 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
764 case Type::X86_FP80TyID:
765 case Type::PPC_FP128TyID:
766 case Type::FP128TyID: {
767 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
768 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
769 switch (CE->getOpcode()) {
770 default: llvm_unreachable("Invalid long double opcode");
771 case Instruction::FAdd:
772 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
773 GV.IntVal = apfLHS.bitcastToAPInt();
775 case Instruction::FSub:
776 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
777 APFloat::rmNearestTiesToEven);
778 GV.IntVal = apfLHS.bitcastToAPInt();
780 case Instruction::FMul:
781 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
782 APFloat::rmNearestTiesToEven);
783 GV.IntVal = apfLHS.bitcastToAPInt();
785 case Instruction::FDiv:
786 apfLHS.divide(APFloat(Sem, RHS.IntVal),
787 APFloat::rmNearestTiesToEven);
788 GV.IntVal = apfLHS.bitcastToAPInt();
790 case Instruction::FRem:
791 apfLHS.mod(APFloat(Sem, RHS.IntVal),
792 APFloat::rmNearestTiesToEven);
793 GV.IntVal = apfLHS.bitcastToAPInt();
805 SmallString<256> Msg;
806 raw_svector_ostream OS(Msg);
807 OS << "ConstantExpr not handled: " << *CE;
808 report_fatal_error(OS.str());
811 // Otherwise, we have a simple constant.
813 switch (C->getType()->getTypeID()) {
814 case Type::FloatTyID:
815 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
817 case Type::DoubleTyID:
818 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
820 case Type::X86_FP80TyID:
821 case Type::FP128TyID:
822 case Type::PPC_FP128TyID:
823 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
825 case Type::IntegerTyID:
826 Result.IntVal = cast<ConstantInt>(C)->getValue();
828 case Type::PointerTyID:
829 if (isa<ConstantPointerNull>(C))
830 Result.PointerVal = nullptr;
831 else if (const Function *F = dyn_cast<Function>(C))
832 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
833 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
834 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
835 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
836 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
837 BA->getBasicBlock())));
839 llvm_unreachable("Unknown constant pointer type!");
841 case Type::VectorTyID: {
844 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
845 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
846 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
849 elemNum = CDV->getNumElements();
850 ElemTy = CDV->getElementType();
851 } else if (CV || CAZ) {
852 VectorType* VTy = dyn_cast<VectorType>(C->getType());
853 elemNum = VTy->getNumElements();
854 ElemTy = VTy->getElementType();
856 llvm_unreachable("Unknown constant vector type!");
859 Result.AggregateVal.resize(elemNum);
860 // Check if vector holds floats.
861 if(ElemTy->isFloatTy()) {
863 GenericValue floatZero;
864 floatZero.FloatVal = 0.f;
865 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
870 for (unsigned i = 0; i < elemNum; ++i)
871 if (!isa<UndefValue>(CV->getOperand(i)))
872 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
873 CV->getOperand(i))->getValueAPF().convertToFloat();
877 for (unsigned i = 0; i < elemNum; ++i)
878 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
882 // Check if vector holds doubles.
883 if (ElemTy->isDoubleTy()) {
885 GenericValue doubleZero;
886 doubleZero.DoubleVal = 0.0;
887 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
892 for (unsigned i = 0; i < elemNum; ++i)
893 if (!isa<UndefValue>(CV->getOperand(i)))
894 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
895 CV->getOperand(i))->getValueAPF().convertToDouble();
899 for (unsigned i = 0; i < elemNum; ++i)
900 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
904 // Check if vector holds integers.
905 if (ElemTy->isIntegerTy()) {
907 GenericValue intZero;
908 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
909 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
914 for (unsigned i = 0; i < elemNum; ++i)
915 if (!isa<UndefValue>(CV->getOperand(i)))
916 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
917 CV->getOperand(i))->getValue();
919 Result.AggregateVal[i].IntVal =
920 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
925 for (unsigned i = 0; i < elemNum; ++i)
926 Result.AggregateVal[i].IntVal = APInt(
927 CDV->getElementType()->getPrimitiveSizeInBits(),
928 CDV->getElementAsInteger(i));
932 llvm_unreachable("Unknown constant pointer type!");
937 SmallString<256> Msg;
938 raw_svector_ostream OS(Msg);
939 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
940 report_fatal_error(OS.str());
946 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
947 /// with the integer held in IntVal.
948 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
949 unsigned StoreBytes) {
950 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
951 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
953 if (sys::IsLittleEndianHost) {
954 // Little-endian host - the source is ordered from LSB to MSB. Order the
955 // destination from LSB to MSB: Do a straight copy.
956 memcpy(Dst, Src, StoreBytes);
958 // Big-endian host - the source is an array of 64 bit words ordered from
959 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
960 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
961 while (StoreBytes > sizeof(uint64_t)) {
962 StoreBytes -= sizeof(uint64_t);
963 // May not be aligned so use memcpy.
964 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
965 Src += sizeof(uint64_t);
968 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
972 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
973 GenericValue *Ptr, Type *Ty) {
974 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
976 switch (Ty->getTypeID()) {
978 dbgs() << "Cannot store value of type " << *Ty << "!\n";
980 case Type::IntegerTyID:
981 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
983 case Type::FloatTyID:
984 *((float*)Ptr) = Val.FloatVal;
986 case Type::DoubleTyID:
987 *((double*)Ptr) = Val.DoubleVal;
989 case Type::X86_FP80TyID:
990 memcpy(Ptr, Val.IntVal.getRawData(), 10);
992 case Type::PointerTyID:
993 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
994 if (StoreBytes != sizeof(PointerTy))
995 memset(&(Ptr->PointerVal), 0, StoreBytes);
997 *((PointerTy*)Ptr) = Val.PointerVal;
999 case Type::VectorTyID:
1000 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1001 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1002 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1003 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1004 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1005 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1006 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1007 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1008 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1014 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
1015 // Host and target are different endian - reverse the stored bytes.
1016 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1019 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1020 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1021 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1022 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1023 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1024 const_cast<uint64_t *>(IntVal.getRawData()));
1026 if (sys::IsLittleEndianHost)
1027 // Little-endian host - the destination must be ordered from LSB to MSB.
1028 // The source is ordered from LSB to MSB: Do a straight copy.
1029 memcpy(Dst, Src, LoadBytes);
1031 // Big-endian - the destination is an array of 64 bit words ordered from
1032 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1033 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1035 while (LoadBytes > sizeof(uint64_t)) {
1036 LoadBytes -= sizeof(uint64_t);
1037 // May not be aligned so use memcpy.
1038 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1039 Dst += sizeof(uint64_t);
1042 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1048 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
1051 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
1053 switch (Ty->getTypeID()) {
1054 case Type::IntegerTyID:
1055 // An APInt with all words initially zero.
1056 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1057 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1059 case Type::FloatTyID:
1060 Result.FloatVal = *((float*)Ptr);
1062 case Type::DoubleTyID:
1063 Result.DoubleVal = *((double*)Ptr);
1065 case Type::PointerTyID:
1066 Result.PointerVal = *((PointerTy*)Ptr);
1068 case Type::X86_FP80TyID: {
1069 // This is endian dependent, but it will only work on x86 anyway.
1070 // FIXME: Will not trap if loading a signaling NaN.
1073 Result.IntVal = APInt(80, y);
1076 case Type::VectorTyID: {
1077 const VectorType *VT = cast<VectorType>(Ty);
1078 const Type *ElemT = VT->getElementType();
1079 const unsigned numElems = VT->getNumElements();
1080 if (ElemT->isFloatTy()) {
1081 Result.AggregateVal.resize(numElems);
1082 for (unsigned i = 0; i < numElems; ++i)
1083 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1085 if (ElemT->isDoubleTy()) {
1086 Result.AggregateVal.resize(numElems);
1087 for (unsigned i = 0; i < numElems; ++i)
1088 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1090 if (ElemT->isIntegerTy()) {
1091 GenericValue intZero;
1092 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1093 intZero.IntVal = APInt(elemBitWidth, 0);
1094 Result.AggregateVal.resize(numElems, intZero);
1095 for (unsigned i = 0; i < numElems; ++i)
1096 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1097 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1102 SmallString<256> Msg;
1103 raw_svector_ostream OS(Msg);
1104 OS << "Cannot load value of type " << *Ty << "!";
1105 report_fatal_error(OS.str());
1109 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
1110 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1111 DEBUG(Init->dump());
1112 if (isa<UndefValue>(Init))
1115 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1116 unsigned ElementSize =
1117 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
1118 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1119 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1123 if (isa<ConstantAggregateZero>(Init)) {
1124 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
1128 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1129 unsigned ElementSize =
1130 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
1131 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1132 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1136 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1137 const StructLayout *SL =
1138 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
1139 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1140 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1144 if (const ConstantDataSequential *CDS =
1145 dyn_cast<ConstantDataSequential>(Init)) {
1146 // CDS is already laid out in host memory order.
1147 StringRef Data = CDS->getRawDataValues();
1148 memcpy(Addr, Data.data(), Data.size());
1152 if (Init->getType()->isFirstClassType()) {
1153 GenericValue Val = getConstantValue(Init);
1154 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1158 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1159 llvm_unreachable("Unknown constant type to initialize memory with!");
1162 /// EmitGlobals - Emit all of the global variables to memory, storing their
1163 /// addresses into GlobalAddress. This must make sure to copy the contents of
1164 /// their initializers into the memory.
1165 void ExecutionEngine::emitGlobals() {
1166 // Loop over all of the global variables in the program, allocating the memory
1167 // to hold them. If there is more than one module, do a prepass over globals
1168 // to figure out how the different modules should link together.
1169 std::map<std::pair<std::string, Type*>,
1170 const GlobalValue*> LinkedGlobalsMap;
1172 if (Modules.size() != 1) {
1173 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1174 Module &M = *Modules[m];
1175 for (const auto &GV : M.globals()) {
1176 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1177 GV.hasAppendingLinkage() || !GV.hasName())
1178 continue;// Ignore external globals and globals with internal linkage.
1180 const GlobalValue *&GVEntry =
1181 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
1183 // If this is the first time we've seen this global, it is the canonical
1190 // If the existing global is strong, never replace it.
1191 if (GVEntry->hasExternalLinkage())
1194 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1195 // symbol. FIXME is this right for common?
1196 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1202 std::vector<const GlobalValue*> NonCanonicalGlobals;
1203 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1204 Module &M = *Modules[m];
1205 for (const auto &GV : M.globals()) {
1206 // In the multi-module case, see what this global maps to.
1207 if (!LinkedGlobalsMap.empty()) {
1208 if (const GlobalValue *GVEntry =
1209 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
1210 // If something else is the canonical global, ignore this one.
1211 if (GVEntry != &GV) {
1212 NonCanonicalGlobals.push_back(&GV);
1218 if (!GV.isDeclaration()) {
1219 addGlobalMapping(&GV, getMemoryForGV(&GV));
1221 // External variable reference. Try to use the dynamic loader to
1222 // get a pointer to it.
1224 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1225 addGlobalMapping(&GV, SymAddr);
1227 report_fatal_error("Could not resolve external global address: "
1233 // If there are multiple modules, map the non-canonical globals to their
1234 // canonical location.
1235 if (!NonCanonicalGlobals.empty()) {
1236 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1237 const GlobalValue *GV = NonCanonicalGlobals[i];
1238 const GlobalValue *CGV =
1239 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1240 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1241 assert(Ptr && "Canonical global wasn't codegen'd!");
1242 addGlobalMapping(GV, Ptr);
1246 // Now that all of the globals are set up in memory, loop through them all
1247 // and initialize their contents.
1248 for (const auto &GV : M.globals()) {
1249 if (!GV.isDeclaration()) {
1250 if (!LinkedGlobalsMap.empty()) {
1251 if (const GlobalValue *GVEntry =
1252 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1253 if (GVEntry != &GV) // Not the canonical variable.
1256 EmitGlobalVariable(&GV);
1262 // EmitGlobalVariable - This method emits the specified global variable to the
1263 // address specified in GlobalAddresses, or allocates new memory if it's not
1264 // already in the map.
1265 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1266 void *GA = getPointerToGlobalIfAvailable(GV);
1269 // If it's not already specified, allocate memory for the global.
1270 GA = getMemoryForGV(GV);
1272 // If we failed to allocate memory for this global, return.
1275 addGlobalMapping(GV, GA);
1278 // Don't initialize if it's thread local, let the client do it.
1279 if (!GV->isThreadLocal())
1280 InitializeMemory(GV->getInitializer(), GA);
1282 Type *ElTy = GV->getType()->getElementType();
1283 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
1284 NumInitBytes += (unsigned)GVSize;
1288 ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1289 : EE(EE), GlobalAddressMap(this) {
1293 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
1294 return &EES->EE.lock;
1297 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1298 const GlobalValue *Old) {
1299 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1300 EES->GlobalAddressReverseMap.erase(OldVal);
1303 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1304 const GlobalValue *,
1305 const GlobalValue *) {
1306 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1307 " RAUW on a value it has a global mapping for.");