blob: a8de4205dad9a5974eb95d81e30751b63cb6b61e [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +000016#include "llvm/ExecutionEngine/ExecutionEngine.h"
Filip Pizlo13a3cf12013-05-14 19:29:00 +000017#include "llvm/ExecutionEngine/JITMemoryManager.h"
Daniel Dunbar48dd8752010-11-13 02:48:57 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner9f3ff922009-08-23 22:49:13 +000019#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth0b8c9a82013-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"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/DynamicLibrary.h"
Torok Edwin31e24662009-07-07 17:32:34 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/Host.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000030#include "llvm/Support/MutexGuard.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/TargetRegistry.h"
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +000032#include "llvm/Support/ValueHandle.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000033#include "llvm/Support/raw_ostream.h"
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000034#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000035#include <cmath>
36#include <cstring>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000037using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000038
Chris Lattner36343732006-12-19 22:43:32 +000039STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
40STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000041
Jeffrey Yasskin46882612010-02-05 16:19:36 +000042ExecutionEngine *(*ExecutionEngine::JITCtor)(
43 Module *M,
44 std::string *ErrorStr,
45 JITMemoryManager *JMM,
Jeffrey Yasskin46882612010-02-05 16:19:36 +000046 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000047 TargetMachine *TM) = 0;
Daniel Dunbar6d135972010-11-17 16:06:37 +000048ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
49 Module *M,
50 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +000051 RTDyldMemoryManager *MCJMM,
Daniel Dunbar6d135972010-11-17 16:06:37 +000052 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000053 TargetMachine *TM) = 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000054ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Reid Kleckner4b1511b2009-07-18 00:42:18 +000055 std::string *ErrorStr) = 0;
Chris Lattner2fe4bb02006-03-22 06:07:50 +000056
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000057ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +000058 : EEState(*this),
Rafael Espindolad0acc842013-10-07 13:54:50 +000059 LazyFunctionCreator(0) {
Jeffrey Yasskindc857242009-10-27 20:30:28 +000060 CompilingLazily = false;
Evan Cheng446531e2008-09-24 16:25:55 +000061 GVCompilationDisabled = false;
Evan Cheng1b088f32008-06-17 16:49:02 +000062 SymbolSearchingDisabled = false;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000063 Modules.push_back(M);
64 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000065}
66
Brian Gaeke8e539482003-09-04 22:57:27 +000067ExecutionEngine::~ExecutionEngine() {
Reid Spencerd4c0e622007-03-03 18:19:18 +000068 clearAllGlobalMappings();
Chris Lattnerfe854032006-08-16 01:24:12 +000069 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
70 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000071}
72
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000073namespace {
Daniel Dunbar48dd8752010-11-13 02:48:57 +000074/// \brief Helper class which uses a value handler to automatically deletes the
75/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000076class GVMemoryBlock : public CallbackVH {
77 GVMemoryBlock(const GlobalVariable *GV)
78 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
79
80public:
Daniel Dunbar48dd8752010-11-13 02:48:57 +000081 /// \brief Returns the address the GlobalVariable should be written into. The
82 /// GVMemoryBlock object prefixes that.
Micah Villmow3574eca2012-10-08 16:38:25 +000083 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000084 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000085 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
86 void *RawMemory = ::operator new(
Micah Villmow3574eca2012-10-08 16:38:25 +000087 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000088 TD.getPreferredAlignment(GV))
89 + GVSize);
90 new(RawMemory) GVMemoryBlock(GV);
91 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
92 }
93
94 virtual void deleted() {
95 // We allocated with operator new and with some extra memory hanging off the
96 // end, so don't just delete this. I'm not sure if this is actually
97 // required.
98 this->~GVMemoryBlock();
99 ::operator delete(this);
100 }
101};
102} // anonymous namespace
103
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000104char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000105 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000106}
107
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000108bool ExecutionEngine::removeModule(Module *M) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000109 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel73d0e212007-10-15 19:56:32 +0000110 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000111 Module *Found = *I;
112 if (Found == M) {
Devang Patel73d0e212007-10-15 19:56:32 +0000113 Modules.erase(I);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000114 clearGlobalMappingsFromModule(M);
115 return true;
Devang Patel73d0e212007-10-15 19:56:32 +0000116 }
117 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000118 return false;
Nate Begeman60789e42009-01-23 19:27:28 +0000119}
120
Chris Lattnerfe854032006-08-16 01:24:12 +0000121Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
122 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000123 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattnerfe854032006-08-16 01:24:12 +0000124 return F;
125 }
126 return 0;
127}
128
129
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000130void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
131 const GlobalValue *ToUnmap) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000132 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000133 void *OldVal;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000134
135 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
136 // GlobalAddressMap.
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000137 if (I == GlobalAddressMap.end())
138 OldVal = 0;
139 else {
140 OldVal = I->second;
141 GlobalAddressMap.erase(I);
142 }
143
144 GlobalAddressReverseMap.erase(OldVal);
145 return OldVal;
146}
147
Chris Lattnere7fd5532006-05-08 22:00:52 +0000148void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
149 MutexGuard locked(lock);
Evan Chengbc4707a2008-09-18 07:54:21 +0000150
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000151 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar93b67e42009-07-26 07:49:05 +0000152 << "\' to [" << Addr << "]\n";);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000153 void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000154 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
155 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000156
157 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000158 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000159 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000160 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000161 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
162 V = GV;
163 }
164}
165
Chris Lattnere7fd5532006-05-08 22:00:52 +0000166void ExecutionEngine::clearAllGlobalMappings() {
167 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000168
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000169 EEState.getGlobalAddressMap(locked).clear();
170 EEState.getGlobalAddressReverseMap(locked).clear();
Chris Lattnere7fd5532006-05-08 22:00:52 +0000171}
172
Nate Begemanf049e072008-05-21 16:34:48 +0000173void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
174 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000175
176 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000177 EEState.RemoveMapping(locked, FI);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000178 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
179 GI != GE; ++GI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000180 EEState.RemoveMapping(locked, GI);
Nate Begemanf049e072008-05-21 16:34:48 +0000181}
182
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000183void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000184 MutexGuard locked(lock);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000185
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000186 ExecutionEngineState::GlobalAddressMapTy &Map =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000187 EEState.getGlobalAddressMap(locked);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000188
Chris Lattnere7fd5532006-05-08 22:00:52 +0000189 // Deleting from the mapping?
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000190 if (Addr == 0)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000191 return EEState.RemoveMapping(locked, GV);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000192
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000193 void *&CurVal = Map[GV];
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000194 void *OldVal = CurVal;
195
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000196 if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
197 EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
Chris Lattnere7fd5532006-05-08 22:00:52 +0000198 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000199
200 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000201 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000202 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000203 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000204 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
205 V = GV;
206 }
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000207 return OldVal;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000208}
209
Chris Lattnere7fd5532006-05-08 22:00:52 +0000210void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
211 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000212
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000213 ExecutionEngineState::GlobalAddressMapTy::iterator I =
214 EEState.getGlobalAddressMap(locked).find(GV);
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000215 return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000216}
217
Chris Lattner55d86482003-12-31 20:21:04 +0000218const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000219 MutexGuard locked(lock);
220
Chris Lattner55d86482003-12-31 20:21:04 +0000221 // If we haven't computed the reverse mapping yet, do so first.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000222 if (EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000223 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000224 I = EEState.getGlobalAddressMap(locked).begin(),
225 E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
Daniel Dunbarab19da42010-11-13 00:55:42 +0000226 EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
227 I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000228 }
229
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000230 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000231 EEState.getGlobalAddressReverseMap(locked).find(Addr);
232 return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000233}
Chris Lattner87f03102003-12-26 06:50:30 +0000234
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000235namespace {
236class ArgvArray {
237 char *Array;
238 std::vector<char*> Values;
239public:
240 ArgvArray() : Array(NULL) {}
241 ~ArgvArray() { clear(); }
242 void clear() {
243 delete[] Array;
244 Array = NULL;
245 for (size_t I = 0, E = Values.size(); I != E; ++I) {
246 delete[] Values[I];
247 }
248 Values.clear();
249 }
250 /// Turn a vector of strings into a nice argv style array of pointers to null
251 /// terminated strings.
252 void *reset(LLVMContext &C, ExecutionEngine *EE,
253 const std::vector<std::string> &InputArgv);
254};
255} // anonymous namespace
256void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
257 const std::vector<std::string> &InputArgv) {
258 clear(); // Free the old contents.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000259 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000260 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner87f03102003-12-26 06:50:30 +0000261
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000262 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000263 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner87f03102003-12-26 06:50:30 +0000264
265 for (unsigned i = 0; i != InputArgv.size(); ++i) {
266 unsigned Size = InputArgv[i].size()+1;
267 char *Dest = new char[Size];
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000268 Values.push_back(Dest);
David Greeneae7c59a2010-01-05 01:27:39 +0000269 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000270
Chris Lattner87f03102003-12-26 06:50:30 +0000271 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
272 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000273
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000274 // Endian safe: Array[i] = (PointerTy)Dest;
275 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000276 SBytePtr);
277 }
278
279 // Null terminate it
280 EE->StoreValueToMemory(PTOGV(0),
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000281 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000282 SBytePtr);
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000283 return Array;
Chris Lattner87f03102003-12-26 06:50:30 +0000284}
285
Chris Lattnerfbd39762009-09-23 01:46:04 +0000286void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
287 bool isDtors) {
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000288 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000289 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng18314dc2008-09-30 15:51:21 +0000290
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000291 // If this global has internal linkage, or if it has a use, then it must be
292 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
293 // this is the case, don't execute any of the global ctors, __main will do
294 // it.
295 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
296
Nick Lewyckya040d472011-04-06 20:38:44 +0000297 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000298 // the init priority, which we ignore.
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000299 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
300 if (InitList == 0)
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000301 return;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000302 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000303 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
304 if (CS == 0) continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000305
306 Constant *FP = CS->getOperand(1);
307 if (FP->isNullValue())
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000308 continue; // Found a sentinal value, ignore.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000309
310 // Strip off constant expression casts.
311 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
312 if (CE->isCast())
313 FP = CE->getOperand(0);
314
315 // Execute the ctor/dtor function!
316 if (Function *F = dyn_cast<Function>(FP))
317 runFunction(F, std::vector<GenericValue>());
318
319 // FIXME: It is marginally lame that we just do nothing here if we see an
320 // entry we don't recognize. It might not be unreasonable for the verifier
321 // to not even allow this and just assert here.
322 }
Evan Cheng18314dc2008-09-30 15:51:21 +0000323}
324
Evan Cheng18314dc2008-09-30 15:51:21 +0000325void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
326 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000327 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
328 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000329}
330
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000331#ifndef NDEBUG
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000332/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
333static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000334 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000335 for (unsigned i = 0; i < PtrSize; ++i)
336 if (*(i + (uint8_t*)Loc))
337 return false;
338 return true;
339}
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000340#endif
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000341
Chris Lattner87f03102003-12-26 06:50:30 +0000342int ExecutionEngine::runFunctionAsMain(Function *Fn,
343 const std::vector<std::string> &argv,
344 const char * const * envp) {
345 std::vector<GenericValue> GVArgs;
346 GenericValue GVArgc;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000347 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov499d8f02007-06-03 19:17:35 +0000348
349 // Check main() type
Chris Lattnerf24d0992004-08-16 01:05:35 +0000350 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000351 FunctionType *FTy = Fn->getFunctionType();
352 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000353
354 // Check the argument types.
355 if (NumArgs > 3)
356 report_fatal_error("Invalid number of arguments of main() supplied");
357 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
358 report_fatal_error("Invalid type for third argument of main() supplied");
359 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
360 report_fatal_error("Invalid type for second argument of main() supplied");
361 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
362 report_fatal_error("Invalid type for first argument of main() supplied");
363 if (!FTy->getReturnType()->isIntegerTy() &&
364 !FTy->getReturnType()->isVoidTy())
365 report_fatal_error("Invalid return type of main() supplied");
366
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000367 ArgvArray CArgv;
368 ArgvArray CEnv;
Chris Lattnerf24d0992004-08-16 01:05:35 +0000369 if (NumArgs) {
370 GVArgs.push_back(GVArgc); // Arg #0 = argc.
371 if (NumArgs > 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000372 // Arg #1 = argv.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000373 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000374 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerf24d0992004-08-16 01:05:35 +0000375 "argv[0] was null after CreateArgv");
376 if (NumArgs > 2) {
377 std::vector<std::string> EnvVars;
378 for (unsigned i = 0; envp[i]; ++i)
379 EnvVars.push_back(envp[i]);
Owen Anderson1d0be152009-08-13 21:58:54 +0000380 // Arg #2 = envp.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000381 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerf24d0992004-08-16 01:05:35 +0000382 }
383 }
384 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000385
Reid Spencer8fb0f192007-03-06 03:04:04 +0000386 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner87f03102003-12-26 06:50:30 +0000387}
388
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000389ExecutionEngine *ExecutionEngine::create(Module *M,
Reid Spencerd4c0e622007-03-03 18:19:18 +0000390 bool ForceInterpreter,
Evan Cheng502f20b2008-08-08 08:11:34 +0000391 std::string *ErrorStr,
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000392 CodeGenOpt::Level OptLevel,
393 bool GVsWithCode) {
Owen Anderson8e1fc562012-03-23 17:40:56 +0000394 EngineBuilder EB = EngineBuilder(M)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000395 .setEngineKind(ForceInterpreter
396 ? EngineKind::Interpreter
397 : EngineKind::JIT)
398 .setErrorStr(ErrorStr)
399 .setOptLevel(OptLevel)
Owen Anderson8e1fc562012-03-23 17:40:56 +0000400 .setAllocateGVsWithCode(GVsWithCode);
401
402 return EB.create();
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000403}
Brian Gaeke82d82772003-09-03 20:34:19 +0000404
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000405/// createJIT - This is the factory method for creating a JIT for the current
406/// machine, it does not fall back to the interpreter. This takes ownership
407/// of the module.
408ExecutionEngine *ExecutionEngine::createJIT(Module *M,
409 std::string *ErrorStr,
410 JITMemoryManager *JMM,
Dylan Noblesmithd95e67d2011-12-01 21:49:21 +0000411 CodeGenOpt::Level OL,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000412 bool GVsWithCode,
Evan Cheng43966132011-07-19 06:37:02 +0000413 Reloc::Model RM,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000414 CodeModel::Model CMM) {
415 if (ExecutionEngine::JITCtor == 0) {
416 if (ErrorStr)
417 *ErrorStr = "JIT has not been linked in.";
418 return 0;
419 }
420
421 // Use the defaults for extra parameters. Users can use EngineBuilder to
422 // set them.
Owen Anderson8e1fc562012-03-23 17:40:56 +0000423 EngineBuilder EB(M);
424 EB.setEngineKind(EngineKind::JIT);
425 EB.setErrorStr(ErrorStr);
426 EB.setRelocationModel(RM);
427 EB.setCodeModel(CMM);
428 EB.setAllocateGVsWithCode(GVsWithCode);
429 EB.setOptLevel(OL);
430 EB.setJITMemoryManager(JMM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000431
Peter Collingbourned40e1032011-12-07 23:58:57 +0000432 // TODO: permit custom TargetOptions here
Owen Anderson8e1fc562012-03-23 17:40:56 +0000433 TargetMachine *TM = EB.selectTarget();
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000434 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000435
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000436 return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000437}
438
Owen Anderson8e1fc562012-03-23 17:40:56 +0000439ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Benjamin Kramer0f554492012-04-08 14:53:14 +0000440 OwningPtr<TargetMachine> TheTM(TM); // Take ownership.
441
Nick Lewycky6456d862008-03-08 02:49:45 +0000442 // Make sure we can resolve symbols in the program as well. The zero arg
443 // to the function tells DynamicLibrary to load the program, not a library.
444 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
445 return 0;
446
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000447 assert(!(JMM && MCJMM));
448
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000449 // If the user specified a memory manager but didn't specify which engine to
450 // create, we assume they only want the JIT, and we fail if they only want
451 // the interpreter.
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000452 if (JMM || MCJMM) {
Chris Lattnerfbd39762009-09-23 01:46:04 +0000453 if (WhichEngine & EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000454 WhichEngine = EngineKind::JIT;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000455 else {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000456 if (ErrorStr)
457 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000458 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000459 }
460 }
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000461
462 if (MCJMM && ! UseMCJIT) {
463 if (ErrorStr)
464 *ErrorStr =
465 "Cannot create a legacy JIT with a runtime dyld memory "
466 "manager.";
467 return 0;
468 }
Brian Gaeke82d82772003-09-03 20:34:19 +0000469
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000470 // Unless the interpreter was explicitly selected or the JIT is not linked,
471 // try making a JIT.
Benjamin Kramer0f554492012-04-08 14:53:14 +0000472 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000473 Triple TT(M->getTargetTriple());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000474 if (!TM->getTarget().hasJIT()) {
475 errs() << "WARNING: This target JIT is not designed for the host"
476 << " you are running. If bad things happen, please choose"
477 << " a different -march switch.\n";
478 }
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000479
Owen Anderson8e1fc562012-03-23 17:40:56 +0000480 if (UseMCJIT && ExecutionEngine::MCJITCtor) {
481 ExecutionEngine *EE =
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000482 ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
Benjamin Kramer0f554492012-04-08 14:53:14 +0000483 AllocateGVsWithCode, TheTM.take());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000484 if (EE) return EE;
485 } else if (ExecutionEngine::JITCtor) {
486 ExecutionEngine *EE =
487 ExecutionEngine::JITCtor(M, ErrorStr, JMM,
Benjamin Kramer0f554492012-04-08 14:53:14 +0000488 AllocateGVsWithCode, TheTM.take());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000489 if (EE) return EE;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000490 }
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000491 }
492
493 // If we can't make a JIT and we didn't request one specifically, try making
494 // an interpreter instead.
Chris Lattnerfbd39762009-09-23 01:46:04 +0000495 if (WhichEngine & EngineKind::Interpreter) {
496 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000497 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000498 if (ErrorStr)
499 *ErrorStr = "Interpreter has not been linked in.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000500 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000501 }
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000502
Jim Grosbach8005bcd2012-08-21 15:42:49 +0000503 if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 &&
504 ExecutionEngine::MCJITCtor == 0) {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000505 if (ErrorStr)
506 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000507 }
508
Chris Lattnerfbd39762009-09-23 01:46:04 +0000509 return 0;
Brian Gaeke82d82772003-09-03 20:34:19 +0000510}
511
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000512void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000513 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000514 return getPointerToFunction(F);
515
Reid Spenceree448632005-07-12 15:51:55 +0000516 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000517 if (void *P = EEState.getGlobalAddressMap(locked)[GV])
518 return P;
Jeff Cohen68835dd2006-02-07 05:11:57 +0000519
520 // Global variable might have been added since interpreter started.
521 if (GlobalVariable *GVar =
522 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
523 EmitGlobalVariable(GVar);
524 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000525 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000526
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000527 return EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000528}
529
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000530/// \brief Converts a Constant* into a GenericValue, including handling of
531/// ConstantExpr values.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000532GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000533 // If its undefined, return the garbage.
Jay Foad5b370122010-01-15 08:32:58 +0000534 if (isa<UndefValue>(C)) {
535 GenericValue Result;
536 switch (C->getType()->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +0000537 default:
538 break;
Jay Foad5b370122010-01-15 08:32:58 +0000539 case Type::IntegerTyID:
540 case Type::X86_FP80TyID:
541 case Type::FP128TyID:
542 case Type::PPC_FP128TyID:
543 // Although the value is undefined, we still have to construct an APInt
544 // with the correct bit width.
545 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
546 break;
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000547 case Type::StructTyID: {
548 // if the whole struct is 'undef' just reserve memory for the value.
549 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
550 unsigned int elemNum = STy->getNumElements();
551 Result.AggregateVal.resize(elemNum);
552 for (unsigned int i = 0; i < elemNum; ++i) {
553 Type *ElemTy = STy->getElementType(i);
554 if (ElemTy->isIntegerTy())
555 Result.AggregateVal[i].IntVal =
556 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
557 else if (ElemTy->isAggregateType()) {
558 const Constant *ElemUndef = UndefValue::get(ElemTy);
559 Result.AggregateVal[i] = getConstantValue(ElemUndef);
560 }
561 }
562 }
563 }
564 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000565 case Type::VectorTyID:
566 // if the whole vector is 'undef' just reserve memory for the value.
567 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
568 const Type *ElemTy = VTy->getElementType();
569 unsigned int elemNum = VTy->getNumElements();
570 Result.AggregateVal.resize(elemNum);
571 if (ElemTy->isIntegerTy())
572 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000573 Result.AggregateVal[i].IntVal =
Nadav Rotem953783e2013-04-01 15:53:30 +0000574 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foad5b370122010-01-15 08:32:58 +0000575 break;
576 }
577 return Result;
578 }
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000579
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000580 // Otherwise, if the value is a ConstantExpr...
Reid Spencer3da59db2006-11-27 01:05:10 +0000581 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencerbce30f12007-03-06 22:23:15 +0000582 Constant *Op0 = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000583 switch (CE->getOpcode()) {
584 case Instruction::GetElementPtr: {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000585 // Compute the index
Reid Spencerbce30f12007-03-06 22:23:15 +0000586 GenericValue Result = getConstantValue(Op0);
Nuno Lopes98281a22012-12-30 16:25:48 +0000587 APInt Offset(TD->getPointerSizeInBits(), 0);
588 cast<GEPOperator>(CE)->accumulateConstantOffset(*TD, Offset);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000589
Reid Spencer8fb0f192007-03-06 03:04:04 +0000590 char* tmp = (char*) Result.PointerVal;
Nuno Lopes98281a22012-12-30 16:25:48 +0000591 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000592 return Result;
593 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000594 case Instruction::Trunc: {
595 GenericValue GV = getConstantValue(Op0);
596 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
597 GV.IntVal = GV.IntVal.trunc(BitWidth);
598 return GV;
599 }
600 case Instruction::ZExt: {
601 GenericValue GV = getConstantValue(Op0);
602 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
603 GV.IntVal = GV.IntVal.zext(BitWidth);
604 return GV;
605 }
606 case Instruction::SExt: {
607 GenericValue GV = getConstantValue(Op0);
608 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
609 GV.IntVal = GV.IntVal.sext(BitWidth);
610 return GV;
611 }
612 case Instruction::FPTrunc: {
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000613 // FIXME long double
Reid Spencerbce30f12007-03-06 22:23:15 +0000614 GenericValue GV = getConstantValue(Op0);
615 GV.FloatVal = float(GV.DoubleVal);
616 return GV;
617 }
618 case Instruction::FPExt:{
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000619 // FIXME long double
Reid Spencerbce30f12007-03-06 22:23:15 +0000620 GenericValue GV = getConstantValue(Op0);
621 GV.DoubleVal = double(GV.FloatVal);
622 return GV;
623 }
624 case Instruction::UIToFP: {
625 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000626 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000627 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000628 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000629 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000630 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000631 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000632 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000633 false,
634 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000635 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000636 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000637 return GV;
638 }
639 case Instruction::SIToFP: {
640 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000641 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000642 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000643 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000644 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000645 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000646 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000647 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000648 true,
649 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000650 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000651 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000652 return GV;
653 }
654 case Instruction::FPToUI: // double->APInt conversion handles sign
655 case Instruction::FPToSI: {
656 GenericValue GV = getConstantValue(Op0);
657 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000658 if (Op0->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000659 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000660 else if (Op0->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000661 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000662 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover0a29cb02013-01-22 09:46:31 +0000663 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000664 uint64_t v;
Dale Johannesen23a98552008-10-09 23:00:39 +0000665 bool ignored;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000666 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000667 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen23a98552008-10-09 23:00:39 +0000668 APFloat::rmTowardZero, &ignored);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000669 GV.IntVal = v; // endian?
670 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000671 return GV;
672 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000673 case Instruction::PtrToInt: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000674 GenericValue GV = getConstantValue(Op0);
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000675 uint32_t PtrWidth = TD->getTypeSizeInBits(Op0->getType());
676 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencerbce30f12007-03-06 22:23:15 +0000677 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000678 uint32_t IntWidth = TD->getTypeSizeInBits(CE->getType());
679 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000680 return GV;
681 }
682 case Instruction::IntToPtr: {
683 GenericValue GV = getConstantValue(Op0);
Micah Villmowb52fb872012-10-24 18:36:13 +0000684 uint32_t PtrWidth = TD->getTypeSizeInBits(CE->getType());
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000685 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000686 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
687 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer3da59db2006-11-27 01:05:10 +0000688 return GV;
689 }
690 case Instruction::BitCast: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000691 GenericValue GV = getConstantValue(Op0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000692 Type* DestTy = CE->getType();
Reid Spencerbce30f12007-03-06 22:23:15 +0000693 switch (Op0->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000694 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencerbce30f12007-03-06 22:23:15 +0000695 case Type::IntegerTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000696 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000697 if (DestTy->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000698 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000699 else if (DestTy->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000700 GV.DoubleVal = GV.IntVal.bitsToDouble();
701 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000702 case Type::FloatTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000703 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000704 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000705 break;
706 case Type::DoubleTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000707 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000708 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000709 break;
710 case Type::PointerTyID:
Duncan Sands1df98592010-02-16 11:11:14 +0000711 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencerbce30f12007-03-06 22:23:15 +0000712 break; // getConstantValue(Op0) above already converted it
713 }
714 return GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000715 }
Chris Lattner9a231222003-05-14 17:51:49 +0000716 case Instruction::Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000717 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000718 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000719 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000720 case Instruction::Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000721 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000722 case Instruction::UDiv:
723 case Instruction::SDiv:
724 case Instruction::URem:
725 case Instruction::SRem:
726 case Instruction::And:
727 case Instruction::Or:
728 case Instruction::Xor: {
729 GenericValue LHS = getConstantValue(Op0);
730 GenericValue RHS = getConstantValue(CE->getOperand(1));
731 GenericValue GV;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000732 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000733 default: llvm_unreachable("Bad add type!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000734 case Type::IntegerTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000735 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000736 default: llvm_unreachable("Invalid integer opcode");
Reid Spencerbce30f12007-03-06 22:23:15 +0000737 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
738 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
739 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
740 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
741 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
742 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
743 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
744 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
745 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
746 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
747 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000748 break;
749 case Type::FloatTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000750 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000751 default: llvm_unreachable("Invalid float opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000752 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000753 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000754 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000755 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000756 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000757 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000758 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000759 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000760 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000761 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000762 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000763 break;
764 case Type::DoubleTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000765 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000766 default: llvm_unreachable("Invalid double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000767 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000768 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000769 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000770 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000771 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000772 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000773 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000774 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000775 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000776 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000777 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000778 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000779 case Type::X86_FP80TyID:
780 case Type::PPC_FP128TyID:
781 case Type::FP128TyID: {
Tim Northover0a29cb02013-01-22 09:46:31 +0000782 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
783 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000784 switch (CE->getOpcode()) {
Daniel Dunbarab19da42010-11-13 00:55:42 +0000785 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000786 case Instruction::FAdd:
Tim Northover0a29cb02013-01-22 09:46:31 +0000787 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000788 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000789 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000790 case Instruction::FSub:
Tim Northover0a29cb02013-01-22 09:46:31 +0000791 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
792 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000793 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000794 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000795 case Instruction::FMul:
Tim Northover0a29cb02013-01-22 09:46:31 +0000796 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
797 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000798 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000799 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000800 case Instruction::FDiv:
Tim Northover0a29cb02013-01-22 09:46:31 +0000801 apfLHS.divide(APFloat(Sem, RHS.IntVal),
802 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000803 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000804 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000805 case Instruction::FRem:
Tim Northover0a29cb02013-01-22 09:46:31 +0000806 apfLHS.mod(APFloat(Sem, RHS.IntVal),
807 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000808 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000809 break;
810 }
811 }
812 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000813 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000814 return GV;
815 }
Chris Lattner9a231222003-05-14 17:51:49 +0000816 default:
817 break;
818 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000819
820 SmallString<256> Msg;
821 raw_svector_ostream OS(Msg);
822 OS << "ConstantExpr not handled: " << *CE;
823 report_fatal_error(OS.str());
Chris Lattner9a231222003-05-14 17:51:49 +0000824 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000825
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000826 // Otherwise, we have a simple constant.
Reid Spencerbce30f12007-03-06 22:23:15 +0000827 GenericValue Result;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000828 switch (C->getType()->getTypeID()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000829 case Type::FloatTyID:
830 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000831 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000832 case Type::DoubleTyID:
Dale Johannesen43421b32007-09-06 18:13:44 +0000833 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer8fb0f192007-03-06 03:04:04 +0000834 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000835 case Type::X86_FP80TyID:
836 case Type::FP128TyID:
837 case Type::PPC_FP128TyID:
Dale Johannesen7111b022008-10-09 18:53:47 +0000838 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000839 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000840 case Type::IntegerTyID:
841 Result.IntVal = cast<ConstantInt>(C)->getValue();
842 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000843 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000844 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000845 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000846 else if (const Function *F = dyn_cast<Function>(C))
847 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000848 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer40cf2f92004-07-18 00:41:27 +0000849 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000850 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
851 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
852 BA->getBasicBlock())));
Reid Spencer40cf2f92004-07-18 00:41:27 +0000853 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000854 llvm_unreachable("Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000855 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000856 case Type::VectorTyID: {
857 unsigned elemNum;
858 Type* ElemTy;
859 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
860 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
861 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
862
863 if (CDV) {
864 elemNum = CDV->getNumElements();
865 ElemTy = CDV->getElementType();
866 } else if (CV || CAZ) {
867 VectorType* VTy = dyn_cast<VectorType>(C->getType());
868 elemNum = VTy->getNumElements();
869 ElemTy = VTy->getElementType();
870 } else {
871 llvm_unreachable("Unknown constant vector type!");
872 }
873
874 Result.AggregateVal.resize(elemNum);
875 // Check if vector holds floats.
876 if(ElemTy->isFloatTy()) {
877 if (CAZ) {
878 GenericValue floatZero;
879 floatZero.FloatVal = 0.f;
880 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
881 floatZero);
882 break;
883 }
884 if(CV) {
885 for (unsigned i = 0; i < elemNum; ++i)
886 if (!isa<UndefValue>(CV->getOperand(i)))
887 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
888 CV->getOperand(i))->getValueAPF().convertToFloat();
889 break;
890 }
891 if(CDV)
892 for (unsigned i = 0; i < elemNum; ++i)
893 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
894
895 break;
896 }
897 // Check if vector holds doubles.
898 if (ElemTy->isDoubleTy()) {
899 if (CAZ) {
900 GenericValue doubleZero;
901 doubleZero.DoubleVal = 0.0;
902 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
903 doubleZero);
904 break;
905 }
906 if(CV) {
907 for (unsigned i = 0; i < elemNum; ++i)
908 if (!isa<UndefValue>(CV->getOperand(i)))
909 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
910 CV->getOperand(i))->getValueAPF().convertToDouble();
911 break;
912 }
913 if(CDV)
914 for (unsigned i = 0; i < elemNum; ++i)
915 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
916
917 break;
918 }
919 // Check if vector holds integers.
920 if (ElemTy->isIntegerTy()) {
921 if (CAZ) {
922 GenericValue intZero;
923 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
924 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
925 intZero);
926 break;
927 }
928 if(CV) {
929 for (unsigned i = 0; i < elemNum; ++i)
930 if (!isa<UndefValue>(CV->getOperand(i)))
931 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
932 CV->getOperand(i))->getValue();
933 else {
934 Result.AggregateVal[i].IntVal =
935 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
936 }
937 break;
938 }
939 if(CDV)
940 for (unsigned i = 0; i < elemNum; ++i)
941 Result.AggregateVal[i].IntVal = APInt(
942 CDV->getElementType()->getPrimitiveSizeInBits(),
943 CDV->getElementAsInteger(i));
944
945 break;
946 }
947 llvm_unreachable("Unknown constant pointer type!");
948 }
949 break;
950
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000951 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000952 SmallString<256> Msg;
953 raw_svector_ostream OS(Msg);
954 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
955 report_fatal_error(OS.str());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000956 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000957
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000958 return Result;
959}
960
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000961/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
962/// with the integer held in IntVal.
963static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
964 unsigned StoreBytes) {
965 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divacky59324292012-09-05 22:26:57 +0000966 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000967
Rafael Espindola21a01d12013-04-15 14:44:24 +0000968 if (sys::IsLittleEndianHost) {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000969 // Little-endian host - the source is ordered from LSB to MSB. Order the
970 // destination from LSB to MSB: Do a straight copy.
971 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000972 } else {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000973 // Big-endian host - the source is an array of 64 bit words ordered from
974 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
975 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
976 while (StoreBytes > sizeof(uint64_t)) {
977 StoreBytes -= sizeof(uint64_t);
978 // May not be aligned so use memcpy.
979 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
980 Src += sizeof(uint64_t);
981 }
982
983 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
984 }
985}
986
Evan Cheng89687e32008-11-04 06:10:31 +0000987void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000988 GenericValue *Ptr, Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000989 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000990
Reid Spencer8fb0f192007-03-06 03:04:04 +0000991 switch (Ty->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +0000992 default:
993 dbgs() << "Cannot store value of type " << *Ty << "!\n";
994 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000995 case Type::IntegerTyID:
996 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer8fb0f192007-03-06 03:04:04 +0000997 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000998 case Type::FloatTyID:
999 *((float*)Ptr) = Val.FloatVal;
1000 break;
1001 case Type::DoubleTyID:
1002 *((double*)Ptr) = Val.DoubleVal;
1003 break;
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001004 case Type::X86_FP80TyID:
1005 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1006 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001007 case Type::PointerTyID:
1008 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1009 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth80d9e072011-04-28 08:37:18 +00001010 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001011
Reid Spencer8fb0f192007-03-06 03:04:04 +00001012 *((PointerTy*)Ptr) = Val.PointerVal;
1013 break;
Nadav Rotem953783e2013-04-01 15:53:30 +00001014 case Type::VectorTyID:
1015 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1016 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1017 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1018 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1019 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1020 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1021 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1022 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1023 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1024 }
1025 }
1026 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001027 }
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001028
Rafael Espindola21a01d12013-04-15 14:44:24 +00001029 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001030 // Host and target are different endian - reverse the stored bytes.
1031 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1032}
1033
1034/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1035/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1036static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1037 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greenec2680be2013-01-14 21:04:45 +00001038 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1039 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001040
Rafael Espindola21a01d12013-04-15 14:44:24 +00001041 if (sys::IsLittleEndianHost)
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001042 // Little-endian host - the destination must be ordered from LSB to MSB.
1043 // The source is ordered from LSB to MSB: Do a straight copy.
1044 memcpy(Dst, Src, LoadBytes);
1045 else {
1046 // Big-endian - the destination is an array of 64 bit words ordered from
1047 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1048 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1049 // a word.
1050 while (LoadBytes > sizeof(uint64_t)) {
1051 LoadBytes -= sizeof(uint64_t);
1052 // May not be aligned so use memcpy.
1053 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1054 Dst += sizeof(uint64_t);
1055 }
1056
1057 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1058 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001059}
1060
Misha Brukman4afac182003-10-10 17:45:12 +00001061/// FIXME: document
1062///
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001063void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sands08bfe262008-03-10 16:38:37 +00001064 GenericValue *Ptr,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001065 Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001066 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1eff7042007-12-10 17:43:13 +00001067
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001068 switch (Ty->getTypeID()) {
1069 case Type::IntegerTyID:
1070 // An APInt with all words initially zero.
1071 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1072 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1073 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +00001074 case Type::FloatTyID:
1075 Result.FloatVal = *((float*)Ptr);
1076 break;
1077 case Type::DoubleTyID:
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001078 Result.DoubleVal = *((double*)Ptr);
Reid Spencer8fb0f192007-03-06 03:04:04 +00001079 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001080 case Type::PointerTyID:
Reid Spencer8fb0f192007-03-06 03:04:04 +00001081 Result.PointerVal = *((PointerTy*)Ptr);
1082 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001083 case Type::X86_FP80TyID: {
1084 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands9e4635a2007-12-15 17:37:40 +00001085 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001086 uint64_t y[2];
1087 memcpy(y, Ptr, 10);
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001088 Result.IntVal = APInt(80, y);
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001089 break;
1090 }
Nadav Rotem953783e2013-04-01 15:53:30 +00001091 case Type::VectorTyID: {
1092 const VectorType *VT = cast<VectorType>(Ty);
1093 const Type *ElemT = VT->getElementType();
1094 const unsigned numElems = VT->getNumElements();
1095 if (ElemT->isFloatTy()) {
1096 Result.AggregateVal.resize(numElems);
1097 for (unsigned i = 0; i < numElems; ++i)
1098 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1099 }
1100 if (ElemT->isDoubleTy()) {
1101 Result.AggregateVal.resize(numElems);
1102 for (unsigned i = 0; i < numElems; ++i)
1103 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1104 }
1105 if (ElemT->isIntegerTy()) {
1106 GenericValue intZero;
1107 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1108 intZero.IntVal = APInt(elemBitWidth, 0);
1109 Result.AggregateVal.resize(numElems, intZero);
1110 for (unsigned i = 0; i < numElems; ++i)
1111 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1112 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1113 }
1114 break;
1115 }
Reid Spencer8fb0f192007-03-06 03:04:04 +00001116 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001117 SmallString<256> Msg;
1118 raw_svector_ostream OS(Msg);
1119 OS << "Cannot load value of type " << *Ty << "!";
1120 report_fatal_error(OS.str());
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001121 }
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001122}
1123
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001124void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greeneae7c59a2010-01-05 01:27:39 +00001125 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesendd947ea2008-08-07 01:30:15 +00001126 DEBUG(Init->dump());
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001127 if (isa<UndefValue>(Init))
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001128 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001129
1130 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001131 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001132 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001133 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1134 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1135 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001136 }
1137
1138 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001139 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattnerb6e1dd72008-02-15 00:57:28 +00001140 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001141 }
1142
1143 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001144 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001145 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman638e3782008-05-20 03:20:09 +00001146 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1147 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1148 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001149 }
1150
1151 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001152 const StructLayout *SL =
Micah Villmow3574eca2012-10-08 16:38:25 +00001153 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman638e3782008-05-20 03:20:09 +00001154 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1155 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1156 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001157 }
1158
1159 if (const ConstantDataSequential *CDS =
1160 dyn_cast<ConstantDataSequential>(Init)) {
1161 // CDS is already laid out in host memory order.
1162 StringRef Data = CDS->getRawDataValues();
1163 memcpy(Addr, Data.data(), Data.size());
1164 return;
1165 }
1166
1167 if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001168 GenericValue Val = getConstantValue(Init);
1169 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1170 return;
1171 }
1172
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001173 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinc23197a2009-07-14 16:55:14 +00001174 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001175}
1176
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001177/// EmitGlobals - Emit all of the global variables to memory, storing their
1178/// addresses into GlobalAddress. This must make sure to copy the contents of
1179/// their initializers into the memory.
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001180void ExecutionEngine::emitGlobals() {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001181 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +00001182 // to hold them. If there is more than one module, do a prepass over globals
1183 // to figure out how the different modules should link together.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001184 std::map<std::pair<std::string, Type*>,
Chris Lattnerfe854032006-08-16 01:24:12 +00001185 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001186
Chris Lattnerfe854032006-08-16 01:24:12 +00001187 if (Modules.size() != 1) {
1188 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001189 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001190 for (Module::const_global_iterator I = M.global_begin(),
1191 E = M.global_end(); I != E; ++I) {
1192 const GlobalValue *GV = I;
Rafael Espindolabb46f522009-01-15 20:18:42 +00001193 if (GV->hasLocalLinkage() || GV->isDeclaration() ||
Chris Lattnerfe854032006-08-16 01:24:12 +00001194 GV->hasAppendingLinkage() || !GV->hasName())
1195 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001196
1197 const GlobalValue *&GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001198 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1199
1200 // If this is the first time we've seen this global, it is the canonical
1201 // version.
1202 if (!GVEntry) {
1203 GVEntry = GV;
1204 continue;
1205 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001206
Chris Lattnerfe854032006-08-16 01:24:12 +00001207 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001208 if (GVEntry->hasExternalLinkage() ||
1209 GVEntry->hasDLLImportLinkage() ||
1210 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001211 continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001212
Chris Lattnerfe854032006-08-16 01:24:12 +00001213 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesenaafce772008-05-14 20:12:51 +00001214 // symbol. FIXME is this right for common?
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001215 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001216 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +00001217 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001218 }
Chris Lattnerfe854032006-08-16 01:24:12 +00001219 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001220
Chris Lattnerfe854032006-08-16 01:24:12 +00001221 std::vector<const GlobalValue*> NonCanonicalGlobals;
1222 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001223 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001224 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1225 I != E; ++I) {
1226 // In the multi-module case, see what this global maps to.
1227 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001228 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001229 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1230 // If something else is the canonical global, ignore this one.
1231 if (GVEntry != &*I) {
1232 NonCanonicalGlobals.push_back(I);
1233 continue;
1234 }
1235 }
1236 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001237
Reid Spencer5cbf9852007-01-30 20:08:39 +00001238 if (!I->isDeclaration()) {
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001239 addGlobalMapping(I, getMemoryForGV(I));
Chris Lattnerfe854032006-08-16 01:24:12 +00001240 } else {
1241 // External variable reference. Try to use the dynamic loader to
1242 // get a pointer to it.
1243 if (void *SymAddr =
Daniel Dunbarfbee5792009-07-21 08:54:24 +00001244 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
Chris Lattnerfe854032006-08-16 01:24:12 +00001245 addGlobalMapping(I, SymAddr);
1246 else {
Chris Lattner75361b62010-04-07 22:58:41 +00001247 report_fatal_error("Could not resolve external global address: "
Torok Edwin31e24662009-07-07 17:32:34 +00001248 +I->getName());
Chris Lattnerfe854032006-08-16 01:24:12 +00001249 }
1250 }
1251 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001252
Chris Lattnerfe854032006-08-16 01:24:12 +00001253 // If there are multiple modules, map the non-canonical globals to their
1254 // canonical location.
1255 if (!NonCanonicalGlobals.empty()) {
1256 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1257 const GlobalValue *GV = NonCanonicalGlobals[i];
1258 const GlobalValue *CGV =
1259 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1260 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1261 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesc8ed9022008-10-14 10:04:52 +00001262 addGlobalMapping(GV, Ptr);
Chris Lattnerfe854032006-08-16 01:24:12 +00001263 }
1264 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001265
1266 // Now that all of the globals are set up in memory, loop through them all
Reid Spencera54b7cb2007-01-12 07:05:14 +00001267 // and initialize their contents.
Chris Lattnerfe854032006-08-16 01:24:12 +00001268 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1269 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001270 if (!I->isDeclaration()) {
Chris Lattnerfe854032006-08-16 01:24:12 +00001271 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001272 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001273 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1274 if (GVEntry != &*I) // Not the canonical variable.
1275 continue;
1276 }
1277 EmitGlobalVariable(I);
1278 }
1279 }
1280 }
Chris Lattner24b0a182003-12-20 02:45:37 +00001281}
1282
1283// EmitGlobalVariable - This method emits the specified global variable to the
1284// address specified in GlobalAddresses, or allocates new memory if it's not
1285// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +00001286void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +00001287 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +00001288
Chris Lattner24b0a182003-12-20 02:45:37 +00001289 if (GA == 0) {
1290 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001291 GA = getMemoryForGV(GV);
Chris Lattner55d86482003-12-31 20:21:04 +00001292 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +00001293 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001294
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001295 // Don't initialize if it's thread local, let the client do it.
1296 if (!GV->isThreadLocal())
1297 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001298
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001299 Type *ElTy = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00001300 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattner813c8152005-01-08 20:13:19 +00001301 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +00001302 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001303}
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001304
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001305ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1306 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001307}
1308
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001309sys::Mutex *
1310ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001311 return &EES->EE.lock;
1312}
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001313
1314void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1315 const GlobalValue *Old) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001316 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1317 EES->GlobalAddressReverseMap.erase(OldVal);
1318}
1319
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001320void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1321 const GlobalValue *,
1322 const GlobalValue *) {
Craig Topper85814382012-02-07 05:05:23 +00001323 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1324 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001325}