blob: c586ba725c54d7c716352776fac2dc0d4920c28b [file] [log] [blame]
Misha Brukman857c21b2003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00009//
Chris Lattner996fe012002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Daniel Dunbar868e3f02010-11-13 02:48:57 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner390d78b2009-08-23 22:49:13 +000018#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ExecutionEngine/GenericValue.h"
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000020#include "llvm/ExecutionEngine/JITEventListener.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#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"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000026#include "llvm/IR/ValueHandle.h"
Rafael Espindoladd396572014-08-01 19:28:15 +000027#include "llvm/Object/Archive.h"
David Blaikie35907d82014-04-29 22:04:55 +000028#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/DynamicLibrary.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Host.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000033#include "llvm/Support/MutexGuard.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/TargetRegistry.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000035#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +000036#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000037#include <cmath>
38#include <cstring>
Chris Lattner29681de2003-11-19 21:08:57 +000039using namespace llvm;
Chris Lattner996fe012002-12-24 00:01:05 +000040
Chandler Carruthf58e3762014-04-22 03:04:17 +000041#define DEBUG_TYPE "jit"
42
Chris Lattnerc346ecd2006-12-19 22:43:32 +000043STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
44STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattner996fe012002-12-24 00:01:05 +000045
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000046ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
David Blaikie196e3232014-09-02 22:41:07 +000047 std::unique_ptr<Module> M, std::string *ErrorStr,
Lang Hames4a5697e2014-12-03 00:51:19 +000048 std::unique_ptr<RTDyldMemoryManager> MCJMM,
49 std::unique_ptr<TargetMachine> TM) = nullptr;
Lang Hames93de2a12015-01-23 21:25:00 +000050
51ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
52 std::string *ErrorStr, std::unique_ptr<RTDyldMemoryManager> OrcJMM,
53 std::unique_ptr<TargetMachine> TM) = nullptr;
54
Rafael Espindola2a8a2792014-08-19 04:04:25 +000055ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
Craig Topper2617dcc2014-04-15 06:32:26 +000056 std::string *ErrorStr) =nullptr;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000057
Lang Hames7ea98e12014-11-27 01:41:16 +000058void JITEventListener::anchor() {}
59
Rafael Espindola2a8a2792014-08-19 04:04:25 +000060ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +000061 : EEState(*this),
Craig Topper2617dcc2014-04-15 06:32:26 +000062 LazyFunctionCreator(nullptr) {
Jeffrey Yasskin4567db42009-10-27 20:30:28 +000063 CompilingLazily = false;
Evan Chengcdc00602008-09-24 16:25:55 +000064 GVCompilationDisabled = false;
Evan Cheng84a90552008-06-17 16:49:02 +000065 SymbolSearchingDisabled = false;
Lang Hamesbc876012014-04-18 06:48:23 +000066
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
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000075 assert(M && "Module is null?");
Rafael Espindola2a8a2792014-08-19 04:04:25 +000076 Modules.push_back(std::move(M));
Misha Brukman260b0c82003-10-16 21:18:05 +000077}
78
Brian Gaeke92f8b302003-09-04 22:57:27 +000079ExecutionEngine::~ExecutionEngine() {
Reid Spencer603682a2007-03-03 18:19:18 +000080 clearAllGlobalMappings();
Brian Gaeke92f8b302003-09-04 22:57:27 +000081}
82
Jeffrey Yasskina4044332010-03-27 04:53:56 +000083namespace {
Daniel Dunbar868e3f02010-11-13 02:48:57 +000084/// \brief Helper class which uses a value handler to automatically deletes the
85/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskina4044332010-03-27 04:53:56 +000086class GVMemoryBlock : public CallbackVH {
87 GVMemoryBlock(const GlobalVariable *GV)
88 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
89
90public:
Daniel Dunbar868e3f02010-11-13 02:48:57 +000091 /// \brief Returns the address the GlobalVariable should be written into. The
92 /// GVMemoryBlock object prefixes that.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000093 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattner229907c2011-07-18 04:54:35 +000094 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskina4044332010-03-27 04:53:56 +000095 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
96 void *RawMemory = ::operator new(
David Majnemerf3cadce2014-10-20 06:13:33 +000097 RoundUpToAlignment(sizeof(GVMemoryBlock),
98 TD.getPreferredAlignment(GV))
Jeffrey Yasskina4044332010-03-27 04:53:56 +000099 + GVSize);
100 new(RawMemory) GVMemoryBlock(GV);
101 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
102 }
103
Craig Topperb51ff602014-03-08 07:51:20 +0000104 void deleted() override {
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000105 // We allocated with operator new and with some extra memory hanging off the
106 // end, so don't just delete this. I'm not sure if this is actually
107 // required.
108 this->~GVMemoryBlock();
109 ::operator delete(this);
110 }
111};
112} // anonymous namespace
113
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000114char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000115 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000116}
117
David Blaikie35907d82014-04-29 22:04:55 +0000118void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
119 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
120}
121
Rafael Espindola7271c192014-08-26 21:04:04 +0000122void
123ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
124 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
125}
126
Rafael Espindola48af1c22014-08-19 18:44:46 +0000127void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
Rafael Espindolaacfd6282014-08-01 18:49:24 +0000128 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
129}
130
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000131bool ExecutionEngine::removeModule(Module *M) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000132 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
133 Module *Found = I->get();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000134 if (Found == M) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000135 I->release();
Devang Patel324fe892007-10-15 19:56:32 +0000136 Modules.erase(I);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000137 clearGlobalMappingsFromModule(M);
138 return true;
Devang Patel324fe892007-10-15 19:56:32 +0000139 }
140 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000141 return false;
Nate Begeman617001d2009-01-23 19:27:28 +0000142}
143
Chris Lattner0621cae2006-08-16 01:24:12 +0000144Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
145 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Keno Fischer5f92a082015-01-27 19:29:00 +0000146 Function *F = Modules[i]->getFunction(FnName);
147 if (F && !F->isDeclaration())
Chris Lattner0621cae2006-08-16 01:24:12 +0000148 return F;
149 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000150 return nullptr;
Chris Lattner0621cae2006-08-16 01:24:12 +0000151}
152
153
Zachary Turner2f825df2014-06-16 20:54:28 +0000154void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000155 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000156 void *OldVal;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000157
158 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
159 // GlobalAddressMap.
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000160 if (I == GlobalAddressMap.end())
Craig Topper2617dcc2014-04-15 06:32:26 +0000161 OldVal = nullptr;
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000162 else {
163 OldVal = I->second;
164 GlobalAddressMap.erase(I);
165 }
166
167 GlobalAddressReverseMap.erase(OldVal);
168 return OldVal;
169}
170
Chris Lattner6d8dd182006-05-08 22:00:52 +0000171void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000172 MutexGuard locked(lock);
Evan Cheng5cc53c32008-09-18 07:54:21 +0000173
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000174 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000175 << "\' to [" << Addr << "]\n";);
Zachary Turner2f825df2014-06-16 20:54:28 +0000176 void *&CurVal = EEState.getGlobalAddressMap()[GV];
Craig Topper2617dcc2014-04-15 06:32:26 +0000177 assert((!CurVal || !Addr) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000178 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000179
180 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000181 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000182 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000183 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000184 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000185 V = GV;
186 }
187}
188
Chris Lattner6d8dd182006-05-08 22:00:52 +0000189void ExecutionEngine::clearAllGlobalMappings() {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000190 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000191
Zachary Turner2f825df2014-06-16 20:54:28 +0000192 EEState.getGlobalAddressMap().clear();
193 EEState.getGlobalAddressReverseMap().clear();
Chris Lattner6d8dd182006-05-08 22:00:52 +0000194}
195
Nate Begeman8f83fc42008-05-21 16:34:48 +0000196void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000197 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000198
199 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000200 EEState.RemoveMapping(FI);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000201 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
202 GI != GE; ++GI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000203 EEState.RemoveMapping(GI);
Nate Begeman8f83fc42008-05-21 16:34:48 +0000204}
205
Chris Lattneree181732008-04-04 04:47:41 +0000206void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000207 MutexGuard locked(lock);
Chris Lattneree181732008-04-04 04:47:41 +0000208
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000209 ExecutionEngineState::GlobalAddressMapTy &Map =
Zachary Turner2f825df2014-06-16 20:54:28 +0000210 EEState.getGlobalAddressMap();
Chris Lattneree181732008-04-04 04:47:41 +0000211
Chris Lattner6d8dd182006-05-08 22:00:52 +0000212 // Deleting from the mapping?
Craig Topper2617dcc2014-04-15 06:32:26 +0000213 if (!Addr)
Zachary Turner2f825df2014-06-16 20:54:28 +0000214 return EEState.RemoveMapping(GV);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000215
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000216 void *&CurVal = Map[GV];
Chris Lattneree181732008-04-04 04:47:41 +0000217 void *OldVal = CurVal;
218
Zachary Turner2f825df2014-06-16 20:54:28 +0000219 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
220 EEState.getGlobalAddressReverseMap().erase(CurVal);
Chris Lattner6d8dd182006-05-08 22:00:52 +0000221 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000222
223 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000224 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000225 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000226 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000227 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000228 V = GV;
229 }
Chris Lattneree181732008-04-04 04:47:41 +0000230 return OldVal;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000231}
232
Chris Lattner6d8dd182006-05-08 22:00:52 +0000233void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000234 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000235
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000236 ExecutionEngineState::GlobalAddressMapTy::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000237 EEState.getGlobalAddressMap().find(GV);
238 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000239}
240
Chris Lattner748e8572003-12-31 20:21:04 +0000241const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000242 MutexGuard locked(lock);
Reid Spencer79876f52005-07-12 15:51:55 +0000243
Chris Lattner748e8572003-12-31 20:21:04 +0000244 // If we haven't computed the reverse mapping yet, do so first.
Zachary Turner2f825df2014-06-16 20:54:28 +0000245 if (EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000246 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Zachary Turner2f825df2014-06-16 20:54:28 +0000247 I = EEState.getGlobalAddressMap().begin(),
248 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
249 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
Daniel Dunbare4f47432010-11-13 00:55:42 +0000250 I->second, I->first));
Chris Lattner748e8572003-12-31 20:21:04 +0000251 }
252
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000253 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000254 EEState.getGlobalAddressReverseMap().find(Addr);
255 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
Chris Lattner748e8572003-12-31 20:21:04 +0000256}
Chris Lattner5a0d4822003-12-26 06:50:30 +0000257
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000258namespace {
259class ArgvArray {
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000260 std::unique_ptr<char[]> Array;
261 std::vector<std::unique_ptr<char[]>> Values;
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000262public:
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000263 /// Turn a vector of strings into a nice argv style array of pointers to null
264 /// terminated strings.
265 void *reset(LLVMContext &C, ExecutionEngine *EE,
266 const std::vector<std::string> &InputArgv);
267};
268} // anonymous namespace
269void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
270 const std::vector<std::string> &InputArgv) {
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000271 Values.clear(); // Free the old contents.
272 Values.reserve(InputArgv.size());
Chandler Carruth5da3f052012-11-01 09:14:31 +0000273 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000274 Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000275
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000276 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
Chris Lattner229907c2011-07-18 04:54:35 +0000277 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000278
279 for (unsigned i = 0; i != InputArgv.size(); ++i) {
280 unsigned Size = InputArgv[i].size()+1;
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000281 auto Dest = make_unique<char[]>(Size);
282 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
Misha Brukman835702a2005-04-21 22:36:52 +0000283
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000284 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
Chris Lattner5a0d4822003-12-26 06:50:30 +0000285 Dest[Size-1] = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000286
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000287 // Endian safe: Array[i] = (PointerTy)Dest;
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000288 EE->StoreValueToMemory(PTOGV(Dest.get()),
289 (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
290 Values.push_back(std::move(Dest));
Chris Lattner5a0d4822003-12-26 06:50:30 +0000291 }
292
293 // Null terminate it
Craig Topper2617dcc2014-04-15 06:32:26 +0000294 EE->StoreValueToMemory(PTOGV(nullptr),
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000295 (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000296 SBytePtr);
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000297 return Array.get();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000298}
299
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000300void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000301 bool isDtors) {
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000302 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000303 GlobalVariable *GV = module.getNamedGlobal(Name);
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000304
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000305 // If this global has internal linkage, or if it has a use, then it must be
306 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
307 // this is the case, don't execute any of the global ctors, __main will do
308 // it.
309 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
310
Nick Lewycky0cbfcb22011-04-06 20:38:44 +0000311 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000312 // the init priority, which we ignore.
Chris Lattner00245f42012-01-24 13:41:11 +0000313 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Craig Topper2617dcc2014-04-15 06:32:26 +0000314 if (!InitList)
Nick Lewycky0f857892011-04-11 22:11:20 +0000315 return;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000316 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner00245f42012-01-24 13:41:11 +0000317 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
Craig Topper2617dcc2014-04-15 06:32:26 +0000318 if (!CS) continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000319
320 Constant *FP = CS->getOperand(1);
321 if (FP->isNullValue())
Nick Lewycky0f857892011-04-11 22:11:20 +0000322 continue; // Found a sentinal value, ignore.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000323
324 // Strip off constant expression casts.
325 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
326 if (CE->isCast())
327 FP = CE->getOperand(0);
328
329 // Execute the ctor/dtor function!
330 if (Function *F = dyn_cast<Function>(FP))
331 runFunction(F, std::vector<GenericValue>());
332
333 // FIXME: It is marginally lame that we just do nothing here if we see an
334 // entry we don't recognize. It might not be unreasonable for the verifier
335 // to not even allow this and just assert here.
336 }
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000337}
338
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000339void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
340 // Execute global ctors/dtors for each module in the program.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000341 for (std::unique_ptr<Module> &M : Modules)
342 runStaticConstructorsDestructors(*M, isDtors);
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000343}
344
Dan Gohmancf3e3012008-08-26 01:38:29 +0000345#ifndef NDEBUG
Duncan Sands1202d1b2007-12-14 19:38:31 +0000346/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
347static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000348 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000349 for (unsigned i = 0; i < PtrSize; ++i)
350 if (*(i + (uint8_t*)Loc))
351 return false;
352 return true;
353}
Dan Gohmancf3e3012008-08-26 01:38:29 +0000354#endif
Duncan Sands1202d1b2007-12-14 19:38:31 +0000355
Chris Lattner5a0d4822003-12-26 06:50:30 +0000356int ExecutionEngine::runFunctionAsMain(Function *Fn,
357 const std::vector<std::string> &argv,
358 const char * const * envp) {
359 std::vector<GenericValue> GVArgs;
360 GenericValue GVArgc;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000361 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov8c32c112007-06-03 19:17:35 +0000362
363 // Check main() type
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000364 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattner229907c2011-07-18 04:54:35 +0000365 FunctionType *FTy = Fn->getFunctionType();
366 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000367
368 // Check the argument types.
369 if (NumArgs > 3)
370 report_fatal_error("Invalid number of arguments of main() supplied");
371 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
372 report_fatal_error("Invalid type for third argument of main() supplied");
373 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
374 report_fatal_error("Invalid type for second argument of main() supplied");
375 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
376 report_fatal_error("Invalid type for first argument of main() supplied");
377 if (!FTy->getReturnType()->isIntegerTy() &&
378 !FTy->getReturnType()->isVoidTy())
379 report_fatal_error("Invalid return type of main() supplied");
380
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000381 ArgvArray CArgv;
382 ArgvArray CEnv;
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000383 if (NumArgs) {
384 GVArgs.push_back(GVArgc); // Arg #0 = argc.
385 if (NumArgs > 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000386 // Arg #1 = argv.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000387 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands1202d1b2007-12-14 19:38:31 +0000388 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000389 "argv[0] was null after CreateArgv");
390 if (NumArgs > 2) {
391 std::vector<std::string> EnvVars;
392 for (unsigned i = 0; envp[i]; ++i)
393 EnvVars.push_back(envp[i]);
Owen Anderson55f1c092009-08-13 21:58:54 +0000394 // Arg #2 = envp.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000395 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000396 }
397 }
398 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000399
Reid Spencer87aa65f2007-03-06 03:04:04 +0000400 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000401}
402
Benjamin Kramer298a3a02015-03-06 16:21:15 +0000403EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
Lang Hames93de2a12015-01-23 21:25:00 +0000404
Lang Hames4a5697e2014-12-03 00:51:19 +0000405EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
Benjamin Kramer298a3a02015-03-06 16:21:15 +0000406 : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
407 OptLevel(CodeGenOpt::Default), MCJMM(nullptr), RelocModel(Reloc::Default),
408 CMModel(CodeModel::JITDefault), UseOrcMCJITReplacement(false) {
Alp Toker322db9e2014-05-31 21:26:17 +0000409// IR module verification is enabled by default in debug builds, and disabled
410// by default in release builds.
411#ifndef NDEBUG
412 VerifyModules = true;
413#else
414 VerifyModules = false;
415#endif
416}
417
Benjamin Kramer298a3a02015-03-06 16:21:15 +0000418EngineBuilder::~EngineBuilder() = default;
419
420EngineBuilder &EngineBuilder::setMCJITMemoryManager(
421 std::unique_ptr<RTDyldMemoryManager> mcjmm) {
422 MCJMM = std::move(mcjmm);
423 return *this;
424}
425
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000426ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000427 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000428
Nick Lewyckya53414f2008-03-08 02:49:45 +0000429 // Make sure we can resolve symbols in the program as well. The zero arg
430 // to the function tells DynamicLibrary to load the program, not a library.
Craig Topper2617dcc2014-04-15 06:32:26 +0000431 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
432 return nullptr;
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000433
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000434 // If the user specified a memory manager but didn't specify which engine to
435 // create, we assume they only want the JIT, and we fail if they only want
436 // the interpreter.
Lang Hames0f154902014-09-23 16:56:02 +0000437 if (MCJMM) {
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000438 if (WhichEngine & EngineKind::JIT)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000439 WhichEngine = EngineKind::JIT;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000440 else {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000441 if (ErrorStr)
442 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000443 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000444 }
445 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000446
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000447 // Unless the interpreter was explicitly selected or the JIT is not linked,
448 // try making a JIT.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000449 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000450 Triple TT(M->getTargetTriple());
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000451 if (!TM->getTarget().hasJIT()) {
452 errs() << "WARNING: This target JIT is not designed for the host"
453 << " you are running. If bad things happen, please choose"
454 << " a different -march switch.\n";
455 }
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000456
Lang Hamesbc876012014-04-18 06:48:23 +0000457 ExecutionEngine *EE = nullptr;
Lang Hames93de2a12015-01-23 21:25:00 +0000458 if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
459 EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MCJMM),
460 std::move(TheTM));
461 EE->addModule(std::move(M));
462 } else if (ExecutionEngine::MCJITCtor)
Lang Hames4a5697e2014-12-03 00:51:19 +0000463 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MCJMM),
Lang Hames0f154902014-09-23 16:56:02 +0000464 std::move(TheTM));
Lang Hames93de2a12015-01-23 21:25:00 +0000465
Lang Hamesbc876012014-04-18 06:48:23 +0000466 if (EE) {
467 EE->setVerifyModules(VerifyModules);
468 return EE;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000469 }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000470 }
471
472 // If we can't make a JIT and we didn't request one specifically, try making
473 // an interpreter instead.
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000474 if (WhichEngine & EngineKind::Interpreter) {
475 if (ExecutionEngine::InterpCtor)
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000476 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
Chris Lattner8bcc6442009-09-23 02:03:49 +0000477 if (ErrorStr)
478 *ErrorStr = "Interpreter has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000479 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000480 }
Chris Lattner8bcc6442009-09-23 02:03:49 +0000481
Eric Christopher79cc1e32014-09-02 22:28:02 +0000482 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000483 if (ErrorStr)
484 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000485 }
486
Craig Topper2617dcc2014-04-15 06:32:26 +0000487 return nullptr;
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000488}
489
Chris Lattner996fe012002-12-24 00:01:05 +0000490void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke1678e852003-08-13 18:16:14 +0000491 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattner996fe012002-12-24 00:01:05 +0000492 return getPointerToFunction(F);
493
Zachary Turnerc04b8922014-06-20 21:07:14 +0000494 MutexGuard locked(lock);
Zachary Turner2f825df2014-06-16 20:54:28 +0000495 if (void *P = EEState.getGlobalAddressMap()[GV])
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000496 return P;
Jeff Cohen69e849012006-02-07 05:11:57 +0000497
498 // Global variable might have been added since interpreter started.
499 if (GlobalVariable *GVar =
500 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
501 EmitGlobalVariable(GVar);
502 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000503 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000504
Zachary Turner2f825df2014-06-16 20:54:28 +0000505 return EEState.getGlobalAddressMap()[GV];
Chris Lattner996fe012002-12-24 00:01:05 +0000506}
507
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000508/// \brief Converts a Constant* into a GenericValue, including handling of
509/// ConstantExpr values.
Chris Lattner996fe012002-12-24 00:01:05 +0000510GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000511 // If its undefined, return the garbage.
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000512 if (isa<UndefValue>(C)) {
513 GenericValue Result;
514 switch (C->getType()->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000515 default:
516 break;
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000517 case Type::IntegerTyID:
518 case Type::X86_FP80TyID:
519 case Type::FP128TyID:
520 case Type::PPC_FP128TyID:
521 // Although the value is undefined, we still have to construct an APInt
522 // with the correct bit width.
523 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
524 break;
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000525 case Type::StructTyID: {
526 // if the whole struct is 'undef' just reserve memory for the value.
527 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
528 unsigned int elemNum = STy->getNumElements();
529 Result.AggregateVal.resize(elemNum);
530 for (unsigned int i = 0; i < elemNum; ++i) {
531 Type *ElemTy = STy->getElementType(i);
532 if (ElemTy->isIntegerTy())
533 Result.AggregateVal[i].IntVal =
534 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
535 else if (ElemTy->isAggregateType()) {
536 const Constant *ElemUndef = UndefValue::get(ElemTy);
537 Result.AggregateVal[i] = getConstantValue(ElemUndef);
538 }
539 }
540 }
541 }
542 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000543 case Type::VectorTyID:
544 // if the whole vector is 'undef' just reserve memory for the value.
545 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
546 const Type *ElemTy = VTy->getElementType();
547 unsigned int elemNum = VTy->getNumElements();
548 Result.AggregateVal.resize(elemNum);
549 if (ElemTy->isIntegerTy())
550 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000551 Result.AggregateVal[i].IntVal =
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000552 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000553 break;
554 }
555 return Result;
556 }
Chris Lattner9de0d142003-04-23 19:01:49 +0000557
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000558 // Otherwise, if the value is a ConstantExpr...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000559 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000560 Constant *Op0 = CE->getOperand(0);
Chris Lattner9de0d142003-04-23 19:01:49 +0000561 switch (CE->getOpcode()) {
562 case Instruction::GetElementPtr: {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000563 // Compute the index
Reid Spencer4fd528f2007-03-06 22:23:15 +0000564 GenericValue Result = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000565 APInt Offset(DL->getPointerSizeInBits(), 0);
566 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukman835702a2005-04-21 22:36:52 +0000567
Reid Spencer87aa65f2007-03-06 03:04:04 +0000568 char* tmp = (char*) Result.PointerVal;
Nuno Lopesb6ad9822012-12-30 16:25:48 +0000569 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattner9de0d142003-04-23 19:01:49 +0000570 return Result;
571 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000572 case Instruction::Trunc: {
573 GenericValue GV = getConstantValue(Op0);
574 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
575 GV.IntVal = GV.IntVal.trunc(BitWidth);
576 return GV;
577 }
578 case Instruction::ZExt: {
579 GenericValue GV = getConstantValue(Op0);
580 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
581 GV.IntVal = GV.IntVal.zext(BitWidth);
582 return GV;
583 }
584 case Instruction::SExt: {
585 GenericValue GV = getConstantValue(Op0);
586 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
587 GV.IntVal = GV.IntVal.sext(BitWidth);
588 return GV;
589 }
590 case Instruction::FPTrunc: {
Dale Johannesena1336cf2007-09-17 18:44:13 +0000591 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000592 GenericValue GV = getConstantValue(Op0);
593 GV.FloatVal = float(GV.DoubleVal);
594 return GV;
595 }
596 case Instruction::FPExt:{
Dale Johannesena1336cf2007-09-17 18:44:13 +0000597 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000598 GenericValue GV = getConstantValue(Op0);
599 GV.DoubleVal = double(GV.FloatVal);
600 return GV;
601 }
602 case Instruction::UIToFP: {
603 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000604 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000605 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000606 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000607 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000608 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000609 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000610 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000611 false,
612 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000613 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000614 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000615 return GV;
616 }
617 case Instruction::SIToFP: {
618 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000619 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000620 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000621 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000622 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000623 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000624 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000625 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000626 true,
627 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000628 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000629 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000630 return GV;
631 }
632 case Instruction::FPToUI: // double->APInt conversion handles sign
633 case Instruction::FPToSI: {
634 GenericValue GV = getConstantValue(Op0);
635 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000636 if (Op0->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000637 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000638 else if (Op0->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000639 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000640 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover29178a32013-01-22 09:46:31 +0000641 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000642 uint64_t v;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000643 bool ignored;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000644 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000645 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000646 APFloat::rmTowardZero, &ignored);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000647 GV.IntVal = v; // endian?
648 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000649 return GV;
650 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000651 case Instruction::PtrToInt: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000652 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000653 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000654 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000655 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000656 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000657 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000658 return GV;
659 }
660 case Instruction::IntToPtr: {
661 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000662 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000663 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000664 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
665 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000666 return GV;
667 }
668 case Instruction::BitCast: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000669 GenericValue GV = getConstantValue(Op0);
Chris Lattner229907c2011-07-18 04:54:35 +0000670 Type* DestTy = CE->getType();
Reid Spencer4fd528f2007-03-06 22:23:15 +0000671 switch (Op0->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000672 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000673 case Type::IntegerTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000674 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnerfdd87902009-10-05 05:54:46 +0000675 if (DestTy->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000676 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000677 else if (DestTy->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000678 GV.DoubleVal = GV.IntVal.bitsToDouble();
679 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000680 case Type::FloatTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000681 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000682 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000683 break;
684 case Type::DoubleTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000685 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000686 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000687 break;
688 case Type::PointerTyID:
Duncan Sands19d0b472010-02-16 11:11:14 +0000689 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000690 break; // getConstantValue(Op0) above already converted it
691 }
692 return GV;
Chris Lattner9de0d142003-04-23 19:01:49 +0000693 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000694 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000695 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000696 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000697 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000698 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000699 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000700 case Instruction::UDiv:
701 case Instruction::SDiv:
702 case Instruction::URem:
703 case Instruction::SRem:
704 case Instruction::And:
705 case Instruction::Or:
706 case Instruction::Xor: {
707 GenericValue LHS = getConstantValue(Op0);
708 GenericValue RHS = getConstantValue(CE->getOperand(1));
709 GenericValue GV;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000710 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000711 default: llvm_unreachable("Bad add type!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000712 case Type::IntegerTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000713 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000714 default: llvm_unreachable("Invalid integer opcode");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000715 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
716 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
717 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
718 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
719 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
720 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
721 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
722 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
723 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
724 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
725 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000726 break;
727 case Type::FloatTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000728 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000729 default: llvm_unreachable("Invalid float opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000730 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000731 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000732 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000733 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000734 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000735 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000736 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000737 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000738 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000739 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000740 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000741 break;
742 case Type::DoubleTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000743 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000744 default: llvm_unreachable("Invalid double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000745 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000746 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000747 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000748 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000749 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000750 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000751 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000752 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000753 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000754 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000755 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000756 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000757 case Type::X86_FP80TyID:
758 case Type::PPC_FP128TyID:
759 case Type::FP128TyID: {
Tim Northover29178a32013-01-22 09:46:31 +0000760 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
761 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000762 switch (CE->getOpcode()) {
Daniel Dunbare4f47432010-11-13 00:55:42 +0000763 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000764 case Instruction::FAdd:
Tim Northover29178a32013-01-22 09:46:31 +0000765 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000766 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000767 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000768 case Instruction::FSub:
Tim Northover29178a32013-01-22 09:46:31 +0000769 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
770 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000771 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000772 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000773 case Instruction::FMul:
Tim Northover29178a32013-01-22 09:46:31 +0000774 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
775 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000776 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000777 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000778 case Instruction::FDiv:
Tim Northover29178a32013-01-22 09:46:31 +0000779 apfLHS.divide(APFloat(Sem, RHS.IntVal),
780 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000781 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000782 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000783 case Instruction::FRem:
Tim Northover29178a32013-01-22 09:46:31 +0000784 apfLHS.mod(APFloat(Sem, RHS.IntVal),
785 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000786 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000787 break;
788 }
789 }
790 break;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000791 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000792 return GV;
793 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000794 default:
795 break;
796 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000797
798 SmallString<256> Msg;
799 raw_svector_ostream OS(Msg);
800 OS << "ConstantExpr not handled: " << *CE;
801 report_fatal_error(OS.str());
Chris Lattner68cbcc32003-05-14 17:51:49 +0000802 }
Misha Brukman835702a2005-04-21 22:36:52 +0000803
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000804 // Otherwise, we have a simple constant.
Reid Spencer4fd528f2007-03-06 22:23:15 +0000805 GenericValue Result;
Chris Lattner6b727592004-06-17 18:19:28 +0000806 switch (C->getType()->getTypeID()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000807 case Type::FloatTyID:
808 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000809 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000810 case Type::DoubleTyID:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000811 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer87aa65f2007-03-06 03:04:04 +0000812 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000813 case Type::X86_FP80TyID:
814 case Type::FP128TyID:
815 case Type::PPC_FP128TyID:
Dale Johannesen54306fe2008-10-09 18:53:47 +0000816 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000817 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000818 case Type::IntegerTyID:
819 Result.IntVal = cast<ConstantInt>(C)->getValue();
820 break;
Chris Lattner996fe012002-12-24 00:01:05 +0000821 case Type::PointerTyID:
Reid Spencer6a0fd732004-07-18 00:41:27 +0000822 if (isa<ConstantPointerNull>(C))
Craig Topper2617dcc2014-04-15 06:32:26 +0000823 Result.PointerVal = nullptr;
Reid Spencer6a0fd732004-07-18 00:41:27 +0000824 else if (const Function *F = dyn_cast<Function>(C))
825 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000826 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer6a0fd732004-07-18 00:41:27 +0000827 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
828 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000829 llvm_unreachable("Unknown constant pointer type!");
Chris Lattner996fe012002-12-24 00:01:05 +0000830 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000831 case Type::VectorTyID: {
832 unsigned elemNum;
833 Type* ElemTy;
834 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
835 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
836 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
837
838 if (CDV) {
839 elemNum = CDV->getNumElements();
840 ElemTy = CDV->getElementType();
841 } else if (CV || CAZ) {
842 VectorType* VTy = dyn_cast<VectorType>(C->getType());
843 elemNum = VTy->getNumElements();
844 ElemTy = VTy->getElementType();
845 } else {
846 llvm_unreachable("Unknown constant vector type!");
847 }
848
849 Result.AggregateVal.resize(elemNum);
850 // Check if vector holds floats.
851 if(ElemTy->isFloatTy()) {
852 if (CAZ) {
853 GenericValue floatZero;
854 floatZero.FloatVal = 0.f;
855 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
856 floatZero);
857 break;
858 }
859 if(CV) {
860 for (unsigned i = 0; i < elemNum; ++i)
861 if (!isa<UndefValue>(CV->getOperand(i)))
862 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
863 CV->getOperand(i))->getValueAPF().convertToFloat();
864 break;
865 }
866 if(CDV)
867 for (unsigned i = 0; i < elemNum; ++i)
868 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
869
870 break;
871 }
872 // Check if vector holds doubles.
873 if (ElemTy->isDoubleTy()) {
874 if (CAZ) {
875 GenericValue doubleZero;
876 doubleZero.DoubleVal = 0.0;
877 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
878 doubleZero);
879 break;
880 }
881 if(CV) {
882 for (unsigned i = 0; i < elemNum; ++i)
883 if (!isa<UndefValue>(CV->getOperand(i)))
884 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
885 CV->getOperand(i))->getValueAPF().convertToDouble();
886 break;
887 }
888 if(CDV)
889 for (unsigned i = 0; i < elemNum; ++i)
890 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
891
892 break;
893 }
894 // Check if vector holds integers.
895 if (ElemTy->isIntegerTy()) {
896 if (CAZ) {
897 GenericValue intZero;
898 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
899 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
900 intZero);
901 break;
902 }
903 if(CV) {
904 for (unsigned i = 0; i < elemNum; ++i)
905 if (!isa<UndefValue>(CV->getOperand(i)))
906 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
907 CV->getOperand(i))->getValue();
908 else {
909 Result.AggregateVal[i].IntVal =
910 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
911 }
912 break;
913 }
914 if(CDV)
915 for (unsigned i = 0; i < elemNum; ++i)
916 Result.AggregateVal[i].IntVal = APInt(
917 CDV->getElementType()->getPrimitiveSizeInBits(),
918 CDV->getElementAsInteger(i));
919
920 break;
921 }
922 llvm_unreachable("Unknown constant pointer type!");
923 }
924 break;
925
Chris Lattner996fe012002-12-24 00:01:05 +0000926 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000927 SmallString<256> Msg;
928 raw_svector_ostream OS(Msg);
929 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
930 report_fatal_error(OS.str());
Chris Lattner996fe012002-12-24 00:01:05 +0000931 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000932
Chris Lattner996fe012002-12-24 00:01:05 +0000933 return Result;
934}
935
Duncan Sands1202d1b2007-12-14 19:38:31 +0000936/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
937/// with the integer held in IntVal.
938static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
939 unsigned StoreBytes) {
940 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000941 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000942
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000943 if (sys::IsLittleEndianHost) {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000944 // Little-endian host - the source is ordered from LSB to MSB. Order the
945 // destination from LSB to MSB: Do a straight copy.
946 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000947 } else {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000948 // Big-endian host - the source is an array of 64 bit words ordered from
949 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
950 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
951 while (StoreBytes > sizeof(uint64_t)) {
952 StoreBytes -= sizeof(uint64_t);
953 // May not be aligned so use memcpy.
954 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
955 Src += sizeof(uint64_t);
956 }
957
958 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
959 }
960}
961
Evan Cheng09053e62008-11-04 06:10:31 +0000962void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattner229907c2011-07-18 04:54:35 +0000963 GenericValue *Ptr, Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000964 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1202d1b2007-12-14 19:38:31 +0000965
Reid Spencer87aa65f2007-03-06 03:04:04 +0000966 switch (Ty->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000967 default:
968 dbgs() << "Cannot store value of type " << *Ty << "!\n";
969 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000970 case Type::IntegerTyID:
971 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer87aa65f2007-03-06 03:04:04 +0000972 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000973 case Type::FloatTyID:
974 *((float*)Ptr) = Val.FloatVal;
975 break;
976 case Type::DoubleTyID:
977 *((double*)Ptr) = Val.DoubleVal;
978 break;
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +0000979 case Type::X86_FP80TyID:
980 memcpy(Ptr, Val.IntVal.getRawData(), 10);
981 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000982 case Type::PointerTyID:
983 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
984 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth93da3c82011-04-28 08:37:18 +0000985 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands1202d1b2007-12-14 19:38:31 +0000986
Reid Spencer87aa65f2007-03-06 03:04:04 +0000987 *((PointerTy*)Ptr) = Val.PointerVal;
988 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000989 case Type::VectorTyID:
990 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
991 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
992 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
993 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
994 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
995 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
996 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
997 StoreIntToMemory(Val.AggregateVal[i].IntVal,
998 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
999 }
1000 }
1001 break;
Chris Lattner996fe012002-12-24 00:01:05 +00001002 }
Duncan Sands1202d1b2007-12-14 19:38:31 +00001003
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001004 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands1202d1b2007-12-14 19:38:31 +00001005 // Host and target are different endian - reverse the stored bytes.
1006 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1007}
1008
1009/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1010/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1011static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1012 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greene82b63572013-01-14 21:04:45 +00001013 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1014 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands1202d1b2007-12-14 19:38:31 +00001015
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001016 if (sys::IsLittleEndianHost)
Duncan Sands1202d1b2007-12-14 19:38:31 +00001017 // Little-endian host - the destination must be ordered from LSB to MSB.
1018 // The source is ordered from LSB to MSB: Do a straight copy.
1019 memcpy(Dst, Src, LoadBytes);
1020 else {
1021 // Big-endian - the destination is an array of 64 bit words ordered from
1022 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1023 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1024 // a word.
1025 while (LoadBytes > sizeof(uint64_t)) {
1026 LoadBytes -= sizeof(uint64_t);
1027 // May not be aligned so use memcpy.
1028 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1029 Dst += sizeof(uint64_t);
1030 }
1031
1032 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1033 }
Chris Lattner996fe012002-12-24 00:01:05 +00001034}
1035
Misha Brukman857c21b2003-10-10 17:45:12 +00001036/// FIXME: document
1037///
Duncan Sands1202d1b2007-12-14 19:38:31 +00001038void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sandscd4a6be2008-03-10 16:38:37 +00001039 GenericValue *Ptr,
Chris Lattner229907c2011-07-18 04:54:35 +00001040 Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001041 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands5c65cb42007-12-10 17:43:13 +00001042
Duncan Sands1202d1b2007-12-14 19:38:31 +00001043 switch (Ty->getTypeID()) {
1044 case Type::IntegerTyID:
1045 // An APInt with all words initially zero.
1046 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1047 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1048 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001049 case Type::FloatTyID:
1050 Result.FloatVal = *((float*)Ptr);
1051 break;
1052 case Type::DoubleTyID:
Duncan Sands1202d1b2007-12-14 19:38:31 +00001053 Result.DoubleVal = *((double*)Ptr);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001054 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001055 case Type::PointerTyID:
Reid Spencer87aa65f2007-03-06 03:04:04 +00001056 Result.PointerVal = *((PointerTy*)Ptr);
1057 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +00001058 case Type::X86_FP80TyID: {
1059 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands26d65392007-12-15 17:37:40 +00001060 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001061 uint64_t y[2];
1062 memcpy(y, Ptr, 10);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001063 Result.IntVal = APInt(80, y);
Dale Johannesena1336cf2007-09-17 18:44:13 +00001064 break;
1065 }
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001066 case Type::VectorTyID: {
1067 const VectorType *VT = cast<VectorType>(Ty);
1068 const Type *ElemT = VT->getElementType();
1069 const unsigned numElems = VT->getNumElements();
1070 if (ElemT->isFloatTy()) {
1071 Result.AggregateVal.resize(numElems);
1072 for (unsigned i = 0; i < numElems; ++i)
1073 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1074 }
1075 if (ElemT->isDoubleTy()) {
1076 Result.AggregateVal.resize(numElems);
1077 for (unsigned i = 0; i < numElems; ++i)
1078 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1079 }
1080 if (ElemT->isIntegerTy()) {
1081 GenericValue intZero;
1082 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1083 intZero.IntVal = APInt(elemBitWidth, 0);
1084 Result.AggregateVal.resize(numElems, intZero);
1085 for (unsigned i = 0; i < numElems; ++i)
1086 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1087 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1088 }
1089 break;
1090 }
Reid Spencer87aa65f2007-03-06 03:04:04 +00001091 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001092 SmallString<256> Msg;
1093 raw_svector_ostream OS(Msg);
1094 OS << "Cannot load value of type " << *Ty << "!";
1095 report_fatal_error(OS.str());
Chris Lattner7f389e82003-05-08 16:52:16 +00001096 }
Chris Lattner7f389e82003-05-08 16:52:16 +00001097}
1098
Chris Lattner996fe012002-12-24 00:01:05 +00001099void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greene0967d2d2010-01-05 01:27:39 +00001100 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesenb086d382008-08-07 01:30:15 +00001101 DEBUG(Init->dump());
Chris Lattner00245f42012-01-24 13:41:11 +00001102 if (isa<UndefValue>(Init))
Chris Lattner61753bf2004-10-16 18:19:26 +00001103 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001104
1105 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino69d62132006-01-20 18:18:40 +00001106 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001107 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino69d62132006-01-20 18:18:40 +00001108 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1109 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1110 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001111 }
1112
1113 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001114 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattner1dd86b12008-02-15 00:57:28 +00001115 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001116 }
1117
1118 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001119 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001120 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001121 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1122 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1123 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001124 }
1125
1126 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001127 const StructLayout *SL =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001128 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001129 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1130 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1131 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001132 }
1133
1134 if (const ConstantDataSequential *CDS =
1135 dyn_cast<ConstantDataSequential>(Init)) {
1136 // CDS is already laid out in host memory order.
1137 StringRef Data = CDS->getRawDataValues();
1138 memcpy(Addr, Data.data(), Data.size());
1139 return;
1140 }
1141
1142 if (Init->getType()->isFirstClassType()) {
Chris Lattner996fe012002-12-24 00:01:05 +00001143 GenericValue Val = getConstantValue(Init);
1144 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1145 return;
1146 }
1147
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001148 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinfbcc6632009-07-14 16:55:14 +00001149 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattner996fe012002-12-24 00:01:05 +00001150}
1151
Chris Lattner996fe012002-12-24 00:01:05 +00001152/// EmitGlobals - Emit all of the global variables to memory, storing their
1153/// addresses into GlobalAddress. This must make sure to copy the contents of
1154/// their initializers into the memory.
Chris Lattner996fe012002-12-24 00:01:05 +00001155void ExecutionEngine::emitGlobals() {
Chris Lattner996fe012002-12-24 00:01:05 +00001156 // Loop over all of the global variables in the program, allocating the memory
Chris Lattner0621cae2006-08-16 01:24:12 +00001157 // to hold them. If there is more than one module, do a prepass over globals
1158 // to figure out how the different modules should link together.
Chris Lattner229907c2011-07-18 04:54:35 +00001159 std::map<std::pair<std::string, Type*>,
Chris Lattner0621cae2006-08-16 01:24:12 +00001160 const GlobalValue*> LinkedGlobalsMap;
Misha Brukman835702a2005-04-21 22:36:52 +00001161
Chris Lattner0621cae2006-08-16 01:24:12 +00001162 if (Modules.size() != 1) {
1163 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001164 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001165 for (const auto &GV : M.globals()) {
1166 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1167 GV.hasAppendingLinkage() || !GV.hasName())
Chris Lattner0621cae2006-08-16 01:24:12 +00001168 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001169
1170 const GlobalValue *&GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001171 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
Chris Lattner0621cae2006-08-16 01:24:12 +00001172
1173 // If this is the first time we've seen this global, it is the canonical
1174 // version.
1175 if (!GVEntry) {
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001176 GVEntry = &GV;
Chris Lattner0621cae2006-08-16 01:24:12 +00001177 continue;
1178 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001179
Chris Lattner0621cae2006-08-16 01:24:12 +00001180 // If the existing global is strong, never replace it.
Nico Rieck7157bb72014-01-14 15:22:47 +00001181 if (GVEntry->hasExternalLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001182 continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001183
Chris Lattner0621cae2006-08-16 01:24:12 +00001184 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesence4396b2008-05-14 20:12:51 +00001185 // symbol. FIXME is this right for common?
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001186 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1187 GVEntry = &GV;
Chris Lattner9de0d142003-04-23 19:01:49 +00001188 }
Chris Lattner996fe012002-12-24 00:01:05 +00001189 }
Chris Lattner0621cae2006-08-16 01:24:12 +00001190 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001191
Chris Lattner0621cae2006-08-16 01:24:12 +00001192 std::vector<const GlobalValue*> NonCanonicalGlobals;
1193 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001194 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001195 for (const auto &GV : M.globals()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001196 // In the multi-module case, see what this global maps to.
1197 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001198 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001199 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001200 // If something else is the canonical global, ignore this one.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001201 if (GVEntry != &GV) {
1202 NonCanonicalGlobals.push_back(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001203 continue;
1204 }
1205 }
1206 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001207
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001208 if (!GV.isDeclaration()) {
1209 addGlobalMapping(&GV, getMemoryForGV(&GV));
Chris Lattner0621cae2006-08-16 01:24:12 +00001210 } else {
1211 // External variable reference. Try to use the dynamic loader to
1212 // get a pointer to it.
1213 if (void *SymAddr =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001214 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1215 addGlobalMapping(&GV, SymAddr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001216 else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001217 report_fatal_error("Could not resolve external global address: "
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001218 +GV.getName());
Chris Lattner0621cae2006-08-16 01:24:12 +00001219 }
1220 }
1221 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001222
Chris Lattner0621cae2006-08-16 01:24:12 +00001223 // If there are multiple modules, map the non-canonical globals to their
1224 // canonical location.
1225 if (!NonCanonicalGlobals.empty()) {
1226 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1227 const GlobalValue *GV = NonCanonicalGlobals[i];
1228 const GlobalValue *CGV =
1229 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1230 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1231 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesa67f06b2008-10-14 10:04:52 +00001232 addGlobalMapping(GV, Ptr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001233 }
1234 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001235
1236 // Now that all of the globals are set up in memory, loop through them all
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001237 // and initialize their contents.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001238 for (const auto &GV : M.globals()) {
1239 if (!GV.isDeclaration()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001240 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001241 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001242 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1243 if (GVEntry != &GV) // Not the canonical variable.
Chris Lattner0621cae2006-08-16 01:24:12 +00001244 continue;
1245 }
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001246 EmitGlobalVariable(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001247 }
1248 }
1249 }
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001250}
1251
1252// EmitGlobalVariable - This method emits the specified global variable to the
1253// address specified in GlobalAddresses, or allocates new memory if it's not
1254// already in the map.
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +00001255void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner748e8572003-12-31 20:21:04 +00001256 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerdc631732004-02-08 19:33:23 +00001257
Craig Topper2617dcc2014-04-15 06:32:26 +00001258 if (!GA) {
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001259 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001260 GA = getMemoryForGV(GV);
Andrew Kaylor3b442372013-11-15 17:52:54 +00001261
1262 // If we failed to allocate memory for this global, return.
Craig Topper2617dcc2014-04-15 06:32:26 +00001263 if (!GA) return;
Andrew Kaylor3b442372013-11-15 17:52:54 +00001264
Chris Lattner748e8572003-12-31 20:21:04 +00001265 addGlobalMapping(GV, GA);
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001266 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001267
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001268 // Don't initialize if it's thread local, let the client do it.
1269 if (!GV->isThreadLocal())
1270 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001271
Chris Lattner229907c2011-07-18 04:54:35 +00001272 Type *ElTy = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001273 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattnerdf1f1522005-01-08 20:13:19 +00001274 NumInitBytes += (unsigned)GVSize;
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001275 ++NumGlobals;
Chris Lattner996fe012002-12-24 00:01:05 +00001276}
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001277
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001278ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1279 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001280}
1281
Zachary Turnerc04b8922014-06-20 21:07:14 +00001282sys::Mutex *
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001283ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001284 return &EES->EE.lock;
1285}
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001286
1287void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1288 const GlobalValue *Old) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001289 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1290 EES->GlobalAddressReverseMap.erase(OldVal);
1291}
1292
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001293void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1294 const GlobalValue *,
1295 const GlobalValue *) {
Craig Toppera2886c22012-02-07 05:05:23 +00001296 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1297 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001298}