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