| Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1 | //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 9 | // This file implements the GlobalValue & GlobalVariable classes for the IR |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 10 | // library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "LLVMContextImpl.h" |
| Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| James Y Knight | ac03dca | 2016-01-15 16:33:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
| Peter Collingbourne | 235c275 | 2016-12-08 19:01:00 +0000 | [diff] [blame] | 17 | #include "llvm/IR/ConstantRange.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/GlobalAlias.h" |
| James Y Knight | ac03dca | 2016-01-15 16:33:06 +0000 | [diff] [blame] | 21 | #include "llvm/IR/GlobalValue.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GlobalVariable.h" |
| 23 | #include "llvm/IR/Module.h" |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Operator.h" |
| Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Error.h" |
| Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // GlobalValue Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| Reid Kleckner | e9c8d7f | 2016-12-29 00:55:51 +0000 | [diff] [blame] | 33 | // GlobalValue should be a Constant, plus a type, a module, some flags, and an |
| 34 | // intrinsic ID. Add an assert to prevent people from accidentally growing |
| 35 | // GlobalValue while adding flags. |
| 36 | static_assert(sizeof(GlobalValue) == |
| 37 | sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned), |
| 38 | "unexpected GlobalValue size growth"); |
| 39 | |
| Reid Kleckner | 443423e | 2017-01-10 23:23:58 +0000 | [diff] [blame] | 40 | // GlobalObject adds a comdat. |
| 41 | static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *), |
| 42 | "unexpected GlobalObject size growth"); |
| 43 | |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 44 | bool GlobalValue::isMaterializable() const { |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 45 | if (const Function *F = dyn_cast<Function>(this)) |
| 46 | return F->isMaterializable(); |
| 47 | return false; |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 48 | } |
| Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 49 | Error GlobalValue::materialize() { |
| Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 50 | return getParent()->materialize(this); |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 51 | } |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 52 | |
| Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 53 | /// Override destroyConstantImpl to make sure it doesn't get called on |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 54 | /// GlobalValue's because they shouldn't be treated like other constants. |
| Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 55 | void GlobalValue::destroyConstantImpl() { |
| 56 | llvm_unreachable("You can't GV->destroyConstantImpl()!"); |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 57 | } |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 58 | |
| Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 59 | Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) { |
| Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 60 | llvm_unreachable("Unsupported class for handleOperandChange()!"); |
| Pete Cooper | a272f7f | 2015-06-24 00:05:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 63 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 64 | /// create a GlobalValue) from the GlobalValue Src to this one. |
| 65 | void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 66 | setVisibility(Src->getVisibility()); |
| Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 67 | setUnnamedAddr(Src->getUnnamedAddr()); |
| Rafael Espindola | 75ec01f | 2014-02-13 05:11:35 +0000 | [diff] [blame] | 68 | setDLLStorageClass(Src->getDLLStorageClass()); |
| Sean Fertile | c70d28b | 2017-10-26 15:00:26 +0000 | [diff] [blame] | 69 | setDSOLocal(Src->isDSOLocal()); |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 72 | void GlobalValue::removeFromParent() { |
| 73 | switch (getValueID()) { |
| 74 | #define HANDLE_GLOBAL_VALUE(NAME) \ |
| 75 | case Value::NAME##Val: \ |
| 76 | return static_cast<NAME *>(this)->removeFromParent(); |
| 77 | #include "llvm/IR/Value.def" |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | llvm_unreachable("not a global"); |
| 82 | } |
| 83 | |
| 84 | void GlobalValue::eraseFromParent() { |
| 85 | switch (getValueID()) { |
| 86 | #define HANDLE_GLOBAL_VALUE(NAME) \ |
| 87 | case Value::NAME##Val: \ |
| 88 | return static_cast<NAME *>(this)->eraseFromParent(); |
| 89 | #include "llvm/IR/Value.def" |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | llvm_unreachable("not a global"); |
| 94 | } |
| 95 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 96 | unsigned GlobalValue::getAlignment() const { |
| 97 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 98 | // In general we cannot compute this at the IR level, but we try. |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 99 | if (const GlobalObject *GO = GA->getBaseObject()) |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 100 | return GO->getAlignment(); |
| 101 | |
| 102 | // FIXME: we should also be able to handle: |
| 103 | // Alias = Global + Offset |
| 104 | // Alias = Absolute |
| 105 | return 0; |
| 106 | } |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 107 | return cast<GlobalObject>(this)->getAlignment(); |
| Rafael Espindola | 52dc5d8 | 2014-05-06 16:48:58 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| Alexander Richardson | 6bcf2ba | 2018-08-23 09:25:17 +0000 | [diff] [blame] | 110 | unsigned GlobalValue::getAddressSpace() const { |
| 111 | PointerType *PtrTy = getType(); |
| 112 | return PtrTy->getAddressSpace(); |
| 113 | } |
| 114 | |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 115 | void GlobalObject::setAlignment(unsigned Align) { |
| Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 116 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 117 | assert(Align <= MaximumAlignment && |
| 118 | "Alignment is greater than MaximumAlignment!"); |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 119 | unsigned AlignmentData = Log2_32(Align) + 1; |
| 120 | unsigned OldData = getGlobalValueSubClassData(); |
| 121 | setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); |
| Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 122 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 123 | } |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 124 | |
| Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 125 | void GlobalObject::copyAttributesFrom(const GlobalObject *Src) { |
| Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 126 | GlobalValue::copyAttributesFrom(Src); |
| Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 127 | setAlignment(Src->getAlignment()); |
| 128 | setSection(Src->getSection()); |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| Teresa Johnson | b43027d | 2016-03-15 02:13:19 +0000 | [diff] [blame] | 131 | std::string GlobalValue::getGlobalIdentifier(StringRef Name, |
| 132 | GlobalValue::LinkageTypes Linkage, |
| 133 | StringRef FileName) { |
| 134 | |
| 135 | // Value names may be prefixed with a binary '1' to indicate |
| 136 | // that the backend should not modify the symbols due to any platform |
| 137 | // naming convention. Do not include that '1' in the PGO profile name. |
| 138 | if (Name[0] == '\1') |
| 139 | Name = Name.substr(1); |
| 140 | |
| 141 | std::string NewName = Name; |
| 142 | if (llvm::GlobalValue::isLocalLinkage(Linkage)) { |
| 143 | // For local symbols, prepend the main file name to distinguish them. |
| 144 | // Do not include the full path in the file name since there's no guarantee |
| 145 | // that it will stay the same, e.g., if the files are checked out from |
| 146 | // version control in different locations. |
| 147 | if (FileName.empty()) |
| 148 | NewName = NewName.insert(0, "<unknown>:"); |
| 149 | else |
| 150 | NewName = NewName.insert(0, FileName.str() + ":"); |
| 151 | } |
| 152 | return NewName; |
| 153 | } |
| 154 | |
| Mehdi Amini | b049431 | 2016-04-02 05:25:27 +0000 | [diff] [blame] | 155 | std::string GlobalValue::getGlobalIdentifier() const { |
| Mehdi Amini | 4f2bb50 | 2016-03-25 05:57:41 +0000 | [diff] [blame] | 156 | return getGlobalIdentifier(getName(), getLinkage(), |
| 157 | getParent()->getSourceFileName()); |
| 158 | } |
| 159 | |
| Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 160 | StringRef GlobalValue::getSection() const { |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 161 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 162 | // In general we cannot compute this at the IR level, but we try. |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 163 | if (const GlobalObject *GO = GA->getBaseObject()) |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 164 | return GO->getSection(); |
| 165 | return ""; |
| 166 | } |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 167 | return cast<GlobalObject>(this)->getSection(); |
| Rafael Espindola | 8d8f100 | 2014-05-06 22:44:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| Craig Topper | 1dd20e6 | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 170 | const Comdat *GlobalValue::getComdat() const { |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 171 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 172 | // In general we cannot compute this at the IR level, but we try. |
| 173 | if (const GlobalObject *GO = GA->getBaseObject()) |
| 174 | return const_cast<GlobalObject *>(GO)->getComdat(); |
| 175 | return nullptr; |
| 176 | } |
| Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 177 | // ifunc and its resolver are separate things so don't use resolver comdat. |
| 178 | if (isa<GlobalIFunc>(this)) |
| 179 | return nullptr; |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 180 | return cast<GlobalObject>(this)->getComdat(); |
| 181 | } |
| 182 | |
| Reid Kleckner | 443423e | 2017-01-10 23:23:58 +0000 | [diff] [blame] | 183 | StringRef GlobalObject::getSectionImpl() const { |
| 184 | assert(hasSection()); |
| 185 | return getContext().pImpl->GlobalObjectSections[this]; |
| 186 | } |
| Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 187 | |
| Reid Kleckner | 443423e | 2017-01-10 23:23:58 +0000 | [diff] [blame] | 188 | void GlobalObject::setSection(StringRef S) { |
| 189 | // Do nothing if we're clearing the section and it is already empty. |
| 190 | if (!hasSection() && S.empty()) |
| 191 | return; |
| 192 | |
| 193 | // Get or create a stable section name string and put it in the table in the |
| 194 | // context. |
| Keno Fischer | 5e1e591 | 2017-02-15 21:42:42 +0000 | [diff] [blame] | 195 | if (!S.empty()) { |
| 196 | S = getContext().pImpl->SectionStrings.insert(S).first->first(); |
| 197 | } |
| Reid Kleckner | 443423e | 2017-01-10 23:23:58 +0000 | [diff] [blame] | 198 | getContext().pImpl->GlobalObjectSections[this] = S; |
| 199 | |
| 200 | // Update the HasSectionHashEntryBit. Setting the section to the empty string |
| 201 | // means this global no longer has a section. |
| 202 | setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty()); |
| Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 203 | } |
| Rafael Espindola | 8fbbfbb | 2014-05-06 14:59:14 +0000 | [diff] [blame] | 204 | |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 205 | bool GlobalValue::isDeclaration() const { |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 206 | // Globals are definitions if they have an initializer. |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 207 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) |
| 208 | return GV->getNumOperands() == 0; |
| 209 | |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 210 | // Functions are definitions if they have a body. |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 211 | if (const Function *F = dyn_cast<Function>(this)) |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 212 | return F->empty() && !F->isMaterializable(); |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 213 | |
| Dmitry Polukhin | a3d5b0b | 2016-04-05 08:47:51 +0000 | [diff] [blame] | 214 | // Aliases and ifuncs are always definitions. |
| 215 | assert(isa<GlobalIndirectSymbol>(this)); |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 216 | return false; |
| 217 | } |
| Rafael Espindola | a03c599 | 2014-05-09 14:31:07 +0000 | [diff] [blame] | 218 | |
| James Y Knight | ac03dca | 2016-01-15 16:33:06 +0000 | [diff] [blame] | 219 | bool GlobalValue::canIncreaseAlignment() const { |
| 220 | // Firstly, can only increase the alignment of a global if it |
| 221 | // is a strong definition. |
| 222 | if (!isStrongDefinitionForLinker()) |
| 223 | return false; |
| 224 | |
| 225 | // It also has to either not have a section defined, or, not have |
| 226 | // alignment specified. (If it is assigned a section, the global |
| 227 | // could be densely packed with other objects in the section, and |
| 228 | // increasing the alignment could cause padding issues.) |
| 229 | if (hasSection() && getAlignment() > 0) |
| 230 | return false; |
| 231 | |
| 232 | // On ELF platforms, we're further restricted in that we can't |
| 233 | // increase the alignment of any variable which might be emitted |
| 234 | // into a shared library, and which is exported. If the main |
| 235 | // executable accesses a variable found in a shared-lib, the main |
| 236 | // exe actually allocates memory for and exports the symbol ITSELF, |
| 237 | // overriding the symbol found in the library. That is, at link |
| 238 | // time, the observed alignment of the variable is copied into the |
| 239 | // executable binary. (A COPY relocation is also generated, to copy |
| 240 | // the initial data from the shadowed variable in the shared-lib |
| 241 | // into the location in the main binary, before running code.) |
| 242 | // |
| 243 | // And thus, even though you might think you are defining the |
| 244 | // global, and allocating the memory for the global in your object |
| 245 | // file, and thus should be able to set the alignment arbitrarily, |
| 246 | // that's not actually true. Doing so can cause an ABI breakage; an |
| 247 | // executable might have already been built with the previous |
| 248 | // alignment of the variable, and then assuming an increased |
| 249 | // alignment will be incorrect. |
| 250 | |
| 251 | // Conservatively assume ELF if there's no parent pointer. |
| 252 | bool isELF = |
| 253 | (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF()); |
| Eli Friedman | d548f0a | 2018-10-31 23:03:58 +0000 | [diff] [blame] | 254 | if (isELF && !isDSOLocal()) |
| James Y Knight | ac03dca | 2016-01-15 16:33:06 +0000 | [diff] [blame] | 255 | return false; |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| Craig Topper | 1dd20e6 | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 260 | const GlobalObject *GlobalValue::getBaseObject() const { |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 261 | if (auto *GO = dyn_cast<GlobalObject>(this)) |
| 262 | return GO; |
| Teresa Johnson | 41db92f | 2017-05-15 18:28:29 +0000 | [diff] [blame] | 263 | if (auto *GA = dyn_cast<GlobalIndirectSymbol>(this)) |
| Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 264 | return GA->getBaseObject(); |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
| Peter Collingbourne | 235c275 | 2016-12-08 19:01:00 +0000 | [diff] [blame] | 268 | bool GlobalValue::isAbsoluteSymbolRef() const { |
| 269 | auto *GO = dyn_cast<GlobalObject>(this); |
| 270 | if (!GO) |
| 271 | return false; |
| 272 | |
| 273 | return GO->getMetadata(LLVMContext::MD_absolute_symbol); |
| 274 | } |
| 275 | |
| 276 | Optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const { |
| 277 | auto *GO = dyn_cast<GlobalObject>(this); |
| 278 | if (!GO) |
| 279 | return None; |
| 280 | |
| 281 | MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol); |
| 282 | if (!MD) |
| 283 | return None; |
| 284 | |
| 285 | return getConstantRangeFromMetadata(*MD); |
| 286 | } |
| 287 | |
| David Blaikie | 8820929 | 2018-03-21 19:23:45 +0000 | [diff] [blame] | 288 | bool GlobalValue::canBeOmittedFromSymbolTable() const { |
| 289 | if (!hasLinkOnceODRLinkage()) |
| 290 | return false; |
| 291 | |
| 292 | // We assume that anyone who sets global unnamed_addr on a non-constant |
| 293 | // knows what they're doing. |
| 294 | if (hasGlobalUnnamedAddr()) |
| 295 | return true; |
| 296 | |
| 297 | // If it is a non constant variable, it needs to be uniqued across shared |
| 298 | // objects. |
| 299 | if (auto *Var = dyn_cast<GlobalVariable>(this)) |
| 300 | if (!Var->isConstant()) |
| 301 | return false; |
| 302 | |
| 303 | return hasAtLeastLocalUnnamedAddr(); |
| 304 | } |
| 305 | |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 306 | //===----------------------------------------------------------------------===// |
| 307 | // GlobalVariable Implementation |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 310 | GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 311 | Constant *InitVal, const Twine &Name, |
| 312 | ThreadLocalMode TLMode, unsigned AddressSpace, |
| Michael Gottesman | 4c4ffd7 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 313 | bool isExternallyInitialized) |
| David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 314 | : GlobalObject(Ty, Value::GlobalVariableVal, |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 315 | OperandTraits<GlobalVariable>::op_begin(this), |
| David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 316 | InitVal != nullptr, Link, Name, AddressSpace), |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 317 | isConstantGlobal(constant), |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 318 | isExternallyInitializedConstant(isExternallyInitialized) { |
| Peter Collingbourne | 2b9e9e4 | 2017-06-04 22:12:03 +0000 | [diff] [blame] | 319 | assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && |
| 320 | "invalid type for global variable"); |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 321 | setThreadLocalMode(TLMode); |
| Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 322 | if (InitVal) { |
| 323 | assert(InitVal->getType() == Ty && |
| Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 324 | "Initializer should be the same type as the GlobalVariable!"); |
| Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 325 | Op<0>() = InitVal; |
| Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 326 | } |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 329 | GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, |
| Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 330 | LinkageTypes Link, Constant *InitVal, |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 331 | const Twine &Name, GlobalVariable *Before, |
| 332 | ThreadLocalMode TLMode, unsigned AddressSpace, |
| Michael Gottesman | 4c4ffd7 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 333 | bool isExternallyInitialized) |
| David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 334 | : GlobalObject(Ty, Value::GlobalVariableVal, |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 335 | OperandTraits<GlobalVariable>::op_begin(this), |
| David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 336 | InitVal != nullptr, Link, Name, AddressSpace), |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 337 | isConstantGlobal(constant), |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 338 | isExternallyInitializedConstant(isExternallyInitialized) { |
| Peter Collingbourne | 2b9e9e4 | 2017-06-04 22:12:03 +0000 | [diff] [blame] | 339 | assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && |
| 340 | "invalid type for global variable"); |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 341 | setThreadLocalMode(TLMode); |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 342 | if (InitVal) { |
| 343 | assert(InitVal->getType() == Ty && |
| 344 | "Initializer should be the same type as the GlobalVariable!"); |
| Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 345 | Op<0>() = InitVal; |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 346 | } |
| Rafael Espindola | a03c599 | 2014-05-09 14:31:07 +0000 | [diff] [blame] | 347 | |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 348 | if (Before) |
| Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 349 | Before->getParent()->getGlobalList().insert(Before->getIterator(), this); |
| Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 350 | else |
| 351 | M.getGlobalList().push_back(this); |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 354 | void GlobalVariable::removeFromParent() { |
| Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 355 | getParent()->getGlobalList().remove(getIterator()); |
| Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void GlobalVariable::eraseFromParent() { |
| Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 359 | getParent()->getGlobalList().erase(getIterator()); |
| Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 362 | void GlobalVariable::setInitializer(Constant *InitVal) { |
| Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 363 | if (!InitVal) { |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 364 | if (hasInitializer()) { |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 365 | // Note, the num operands is used to compute the offset of the operand, so |
| 366 | // the order here matters. Clearing the operand then clearing the num |
| 367 | // operands ensures we have the correct offset to the operand. |
| Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 368 | Op<0>().set(nullptr); |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 369 | setGlobalVariableNumOperands(0); |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 370 | } |
| 371 | } else { |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 372 | assert(InitVal->getType() == getValueType() && |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 373 | "Initializer type must match GlobalVariable type"); |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 374 | // Note, the num operands is used to compute the offset of the operand, so |
| 375 | // the order here matters. We need to set num operands to 1 first so that |
| 376 | // we get the correct offset to the first operand when we set it. |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 377 | if (!hasInitializer()) |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 378 | setGlobalVariableNumOperands(1); |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 379 | Op<0>().set(InitVal); |
| 380 | } |
| 381 | } |
| 382 | |
| Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 383 | /// Copy all additional attributes (those not needed to create a GlobalVariable) |
| 384 | /// from the GlobalVariable Src to this one. |
| Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 385 | void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) { |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 386 | GlobalObject::copyAttributesFrom(Src); |
| Reid Kleckner | e7c7854 | 2017-05-11 21:14:29 +0000 | [diff] [blame] | 387 | setThreadLocalMode(Src->getThreadLocalMode()); |
| 388 | setExternallyInitialized(Src->isExternallyInitialized()); |
| 389 | setAttributes(Src->getAttributes()); |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 392 | void GlobalVariable::dropAllReferences() { |
| 393 | User::dropAllReferences(); |
| 394 | clearMetadata(); |
| 395 | } |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 396 | |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 397 | //===----------------------------------------------------------------------===// |
| Dmitry Polukhin | cd835ad | 2016-03-31 14:16:21 +0000 | [diff] [blame] | 398 | // GlobalIndirectSymbol Implementation |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
| 401 | GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy, |
| 402 | unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, |
| 403 | Constant *Symbol) |
| 404 | : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) { |
| 405 | Op<0>() = Symbol; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | //===----------------------------------------------------------------------===// |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 410 | // GlobalAlias Implementation |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | |
| David Blaikie | 16a2f3e | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 413 | GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, |
| 414 | const Twine &Name, Constant *Aliasee, |
| 415 | Module *ParentModule) |
| Dmitry Polukhin | cd835ad | 2016-03-31 14:16:21 +0000 | [diff] [blame] | 416 | : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name, |
| 417 | Aliasee) { |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 418 | if (ParentModule) |
| 419 | ParentModule->getAliasList().push_back(this); |
| 420 | } |
| 421 | |
| David Blaikie | 16a2f3e | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 422 | GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, |
| 423 | LinkageTypes Link, const Twine &Name, |
| 424 | Constant *Aliasee, Module *ParentModule) { |
| 425 | return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 426 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 427 | |
| David Blaikie | 16a2f3e | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 428 | GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, |
| 429 | LinkageTypes Linkage, const Twine &Name, |
| 430 | Module *Parent) { |
| 431 | return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 432 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 433 | |
| David Blaikie | 16a2f3e | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 434 | GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, |
| 435 | LinkageTypes Linkage, const Twine &Name, |
| 436 | GlobalValue *Aliasee) { |
| 437 | return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 438 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 439 | |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 440 | GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 441 | GlobalValue *Aliasee) { |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 442 | PointerType *PTy = Aliasee->getType(); |
| David Blaikie | 16a2f3e | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 443 | return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, |
| 444 | Aliasee); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 447 | GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 448 | return create(Aliasee->getLinkage(), Name, Aliasee); |
| 449 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 450 | |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 451 | void GlobalAlias::removeFromParent() { |
| Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 452 | getParent()->getAliasList().remove(getIterator()); |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void GlobalAlias::eraseFromParent() { |
| Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 456 | getParent()->getAliasList().erase(getIterator()); |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 459 | void GlobalAlias::setAliasee(Constant *Aliasee) { |
| Patrik Hagglund | 3154171 | 2014-06-04 11:21:11 +0000 | [diff] [blame] | 460 | assert((!Aliasee || Aliasee->getType() == getType()) && |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 461 | "Alias and aliasee types should match!"); |
| Dmitry Polukhin | cd835ad | 2016-03-31 14:16:21 +0000 | [diff] [blame] | 462 | setIndirectSymbol(Aliasee); |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 463 | } |
| Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 464 | |
| 465 | //===----------------------------------------------------------------------===// |
| 466 | // GlobalIFunc Implementation |
| 467 | //===----------------------------------------------------------------------===// |
| 468 | |
| 469 | GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link, |
| 470 | const Twine &Name, Constant *Resolver, |
| 471 | Module *ParentModule) |
| 472 | : GlobalIndirectSymbol(Ty, Value::GlobalIFuncVal, AddressSpace, Link, Name, |
| 473 | Resolver) { |
| 474 | if (ParentModule) |
| 475 | ParentModule->getIFuncList().push_back(this); |
| 476 | } |
| 477 | |
| 478 | GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace, |
| 479 | LinkageTypes Link, const Twine &Name, |
| 480 | Constant *Resolver, Module *ParentModule) { |
| 481 | return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule); |
| 482 | } |
| 483 | |
| Dmitry Polukhin | a1feff7 | 2016-04-07 12:32:19 +0000 | [diff] [blame] | 484 | void GlobalIFunc::removeFromParent() { |
| 485 | getParent()->getIFuncList().remove(getIterator()); |
| 486 | } |
| 487 | |
| 488 | void GlobalIFunc::eraseFromParent() { |
| 489 | getParent()->getIFuncList().erase(getIterator()); |
| 490 | } |