blob: b2f5640026f9f5a831802386698685b2896cb9b9 [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00006//
Reid Spencer3d169b12004-07-18 00:06:26 +00007//===----------------------------------------------------------------------===//
8//
Chandler Carruthef860a22013-01-02 09:10:48 +00009// This file implements the GlobalValue & GlobalVariable classes for the IR
Reid Spencer3d169b12004-07-18 00:06:26 +000010// library.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "LLVMContextImpl.h"
Anton Korobeynikovda7db7d2008-03-11 22:28:56 +000015#include "llvm/ADT/SmallPtrSet.h"
James Y Knightac03dca2016-01-15 16:33:06 +000016#include "llvm/ADT/Triple.h"
Peter Collingbourne235c2752016-12-08 19:01:00 +000017#include "llvm/IR/ConstantRange.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalAlias.h"
James Y Knightac03dca2016-01-15 16:33:06 +000021#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/GlobalVariable.h"
23#include "llvm/IR/Module.h"
Rafael Espindola64c1e182014-06-03 02:41:57 +000024#include "llvm/IR/Operator.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000025#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000026#include "llvm/Support/ErrorHandling.h"
Reid Spencer3d169b12004-07-18 00:06:26 +000027using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30// GlobalValue Class
31//===----------------------------------------------------------------------===//
32
Reid Klecknere9c8d7f2016-12-29 00:55:51 +000033// 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.
36static_assert(sizeof(GlobalValue) ==
37 sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
38 "unexpected GlobalValue size growth");
39
Reid Kleckner443423e2017-01-10 23:23:58 +000040// GlobalObject adds a comdat.
41static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
42 "unexpected GlobalObject size growth");
43
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000044bool GlobalValue::isMaterializable() const {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +000045 if (const Function *F = dyn_cast<Function>(this))
46 return F->isMaterializable();
47 return false;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000048}
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000049Error GlobalValue::materialize() {
Rafael Espindola5a52e6d2014-10-24 22:50:48 +000050 return getParent()->materialize(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000051}
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000052
Pete Cooper86dd4cf2015-06-23 21:55:11 +000053/// Override destroyConstantImpl to make sure it doesn't get called on
Reid Spencer3d169b12004-07-18 00:06:26 +000054/// GlobalValue's because they shouldn't be treated like other constants.
Pete Cooper86dd4cf2015-06-23 21:55:11 +000055void GlobalValue::destroyConstantImpl() {
56 llvm_unreachable("You can't GV->destroyConstantImpl()!");
Reid Spencer3d169b12004-07-18 00:06:26 +000057}
Duncan Sandsdd7daee2008-05-26 19:58:59 +000058
Mehdi Amini8914d292016-02-10 22:47:15 +000059Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +000060 llvm_unreachable("Unsupported class for handleOperandChange()!");
Pete Coopera272f7f2015-06-24 00:05:07 +000061}
62
Duncan Sandsdd7daee2008-05-26 19:58:59 +000063/// copyAttributesFrom - copy all additional attributes (those not needed to
64/// create a GlobalValue) from the GlobalValue Src to this one.
65void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
Duncan Sandsdd7daee2008-05-26 19:58:59 +000066 setVisibility(Src->getVisibility());
Peter Collingbourne96efdd62016-06-14 21:01:22 +000067 setUnnamedAddr(Src->getUnnamedAddr());
Rafael Espindola75ec01f2014-02-13 05:11:35 +000068 setDLLStorageClass(Src->getDLLStorageClass());
Sean Fertilec70d28b2017-10-26 15:00:26 +000069 setDSOLocal(Src->isDSOLocal());
Duncan Sandsdd7daee2008-05-26 19:58:59 +000070}
71
Reid Klecknere7c78542017-05-11 21:14:29 +000072void 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
84void 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 Espindola64c1e182014-06-03 02:41:57 +000096unsigned 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 Majnemerdad0a642014-06-27 18:19:56 +000099 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +0000100 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 Espindola99e05cf2014-05-13 18:45:48 +0000107 return cast<GlobalObject>(this)->getAlignment();
Rafael Espindola52dc5d82014-05-06 16:48:58 +0000108}
109
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +0000110unsigned GlobalValue::getAddressSpace() const {
111 PointerType *PtrTy = getType();
112 return PtrTy->getAddressSpace();
113}
114
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000115void GlobalObject::setAlignment(unsigned Align) {
Dan Gohman390914c2010-07-28 20:56:48 +0000116 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
117 assert(Align <= MaximumAlignment &&
118 "Alignment is greater than MaximumAlignment!");
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000119 unsigned AlignmentData = Log2_32(Align) + 1;
120 unsigned OldData = getGlobalValueSubClassData();
121 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
Dan Gohman390914c2010-07-28 20:56:48 +0000122 assert(getAlignment() == Align && "Alignment representation error!");
123}
Chris Lattner923053a2011-07-14 18:10:41 +0000124
Reid Klecknere7c78542017-05-11 21:14:29 +0000125void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
Rafael Espindola769efe62015-12-02 20:03:17 +0000126 GlobalValue::copyAttributesFrom(Src);
Reid Klecknere7c78542017-05-11 21:14:29 +0000127 setAlignment(Src->getAlignment());
128 setSection(Src->getSection());
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000129}
130
Teresa Johnsonb43027d2016-03-15 02:13:19 +0000131std::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 Aminib0494312016-04-02 05:25:27 +0000155std::string GlobalValue::getGlobalIdentifier() const {
Mehdi Amini4f2bb502016-03-25 05:57:41 +0000156 return getGlobalIdentifier(getName(), getLinkage(),
157 getParent()->getSourceFileName());
158}
159
Rafael Espindola83658d62016-05-11 18:21:59 +0000160StringRef GlobalValue::getSection() const {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000161 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
162 // In general we cannot compute this at the IR level, but we try.
David Majnemerdad0a642014-06-27 18:19:56 +0000163 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola64c1e182014-06-03 02:41:57 +0000164 return GO->getSection();
165 return "";
166 }
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000167 return cast<GlobalObject>(this)->getSection();
Rafael Espindola8d8f1002014-05-06 22:44:30 +0000168}
169
Craig Topper1dd20e62017-03-27 05:47:03 +0000170const Comdat *GlobalValue::getComdat() const {
David Majnemerdad0a642014-06-27 18:19:56 +0000171 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 Polukhina1feff72016-04-07 12:32:19 +0000177 // ifunc and its resolver are separate things so don't use resolver comdat.
178 if (isa<GlobalIFunc>(this))
179 return nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +0000180 return cast<GlobalObject>(this)->getComdat();
181}
182
Reid Kleckner443423e2017-01-10 23:23:58 +0000183StringRef GlobalObject::getSectionImpl() const {
184 assert(hasSection());
185 return getContext().pImpl->GlobalObjectSections[this];
186}
Rafael Espindola83658d62016-05-11 18:21:59 +0000187
Reid Kleckner443423e2017-01-10 23:23:58 +0000188void 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 Fischer5e1e5912017-02-15 21:42:42 +0000195 if (!S.empty()) {
196 S = getContext().pImpl->SectionStrings.insert(S).first->first();
197 }
Reid Kleckner443423e2017-01-10 23:23:58 +0000198 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 Espindola83658d62016-05-11 18:21:59 +0000203}
Rafael Espindola8fbbfbb2014-05-06 14:59:14 +0000204
Chris Lattner923053a2011-07-14 18:10:41 +0000205bool GlobalValue::isDeclaration() const {
Chris Lattner85617212011-07-14 18:12:44 +0000206 // Globals are definitions if they have an initializer.
Chris Lattner923053a2011-07-14 18:10:41 +0000207 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
208 return GV->getNumOperands() == 0;
209
Chris Lattner85617212011-07-14 18:12:44 +0000210 // Functions are definitions if they have a body.
Chris Lattner923053a2011-07-14 18:10:41 +0000211 if (const Function *F = dyn_cast<Function>(this))
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000212 return F->empty() && !F->isMaterializable();
Chris Lattner85617212011-07-14 18:12:44 +0000213
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000214 // Aliases and ifuncs are always definitions.
215 assert(isa<GlobalIndirectSymbol>(this));
Chris Lattner923053a2011-07-14 18:10:41 +0000216 return false;
217}
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000218
James Y Knightac03dca2016-01-15 16:33:06 +0000219bool 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 Friedmand548f0a2018-10-31 23:03:58 +0000254 if (isELF && !isDSOLocal())
James Y Knightac03dca2016-01-15 16:33:06 +0000255 return false;
256
257 return true;
258}
259
Craig Topper1dd20e62017-03-27 05:47:03 +0000260const GlobalObject *GlobalValue::getBaseObject() const {
Peter Collingbourne67335642016-10-24 19:23:39 +0000261 if (auto *GO = dyn_cast<GlobalObject>(this))
262 return GO;
Teresa Johnson41db92f2017-05-15 18:28:29 +0000263 if (auto *GA = dyn_cast<GlobalIndirectSymbol>(this))
Peter Collingbourne67335642016-10-24 19:23:39 +0000264 return GA->getBaseObject();
265 return nullptr;
266}
267
Peter Collingbourne235c2752016-12-08 19:01:00 +0000268bool 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
276Optional<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 Blaikie88209292018-03-21 19:23:45 +0000288bool 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 Spencer3d169b12004-07-18 00:06:26 +0000306//===----------------------------------------------------------------------===//
307// GlobalVariable Implementation
308//===----------------------------------------------------------------------===//
309
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000310GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000311 Constant *InitVal, const Twine &Name,
312 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000313 bool isExternallyInitialized)
David Blaikied583b192015-08-21 21:35:28 +0000314 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000315 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikied583b192015-08-21 21:35:28 +0000316 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000317 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000318 isExternallyInitializedConstant(isExternallyInitialized) {
Peter Collingbourne2b9e9e42017-06-04 22:12:03 +0000319 assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
320 "invalid type for global variable");
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000321 setThreadLocalMode(TLMode);
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000322 if (InitVal) {
323 assert(InitVal->getType() == Ty &&
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000324 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000325 Op<0>() = InitVal;
Alkis Evlogimenosf45cc7a2004-08-05 11:28:34 +0000326 }
Reid Spencer3d169b12004-07-18 00:06:26 +0000327}
328
Chris Lattner229907c2011-07-18 04:54:35 +0000329GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersonb17f3292009-07-08 19:03:57 +0000330 LinkageTypes Link, Constant *InitVal,
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000331 const Twine &Name, GlobalVariable *Before,
332 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesman4c4ffd72013-02-03 21:54:38 +0000333 bool isExternallyInitialized)
David Blaikied583b192015-08-21 21:35:28 +0000334 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000335 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikied583b192015-08-21 21:35:28 +0000336 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000337 isConstantGlobal(constant),
Rafael Espindolab2ea3392014-05-09 15:49:02 +0000338 isExternallyInitializedConstant(isExternallyInitialized) {
Peter Collingbourne2b9e9e42017-06-04 22:12:03 +0000339 assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
340 "invalid type for global variable");
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000341 setThreadLocalMode(TLMode);
Chris Lattner87732cf2006-09-30 21:31:26 +0000342 if (InitVal) {
343 assert(InitVal->getType() == Ty &&
344 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000345 Op<0>() = InitVal;
Chris Lattner87732cf2006-09-30 21:31:26 +0000346 }
Rafael Espindolaa03c5992014-05-09 14:31:07 +0000347
Chris Lattner87732cf2006-09-30 21:31:26 +0000348 if (Before)
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000349 Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
Owen Andersonb17f3292009-07-08 19:03:57 +0000350 else
351 M.getGlobalList().push_back(this);
Chris Lattner87732cf2006-09-30 21:31:26 +0000352}
353
Chris Lattner02a71e72004-10-11 22:21:39 +0000354void GlobalVariable::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000355 getParent()->getGlobalList().remove(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +0000356}
357
358void GlobalVariable::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000359 getParent()->getGlobalList().erase(getIterator());
Chris Lattner02a71e72004-10-11 22:21:39 +0000360}
361
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000362void GlobalVariable::setInitializer(Constant *InitVal) {
Craig Topperc6207612014-04-09 06:08:46 +0000363 if (!InitVal) {
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000364 if (hasInitializer()) {
Pete Cooperb4eede22015-06-12 17:48:10 +0000365 // 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 Topperc6207612014-04-09 06:08:46 +0000368 Op<0>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000369 setGlobalVariableNumOperands(0);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000370 }
371 } else {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000372 assert(InitVal->getType() == getValueType() &&
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000373 "Initializer type must match GlobalVariable type");
Pete Cooperb4eede22015-06-12 17:48:10 +0000374 // 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 Yasskin7c57c412009-11-17 00:43:13 +0000377 if (!hasInitializer())
Pete Cooperb4eede22015-06-12 17:48:10 +0000378 setGlobalVariableNumOperands(1);
Jeffrey Yasskin7c57c412009-11-17 00:43:13 +0000379 Op<0>().set(InitVal);
380 }
381}
382
Rafael Espindola769efe62015-12-02 20:03:17 +0000383/// Copy all additional attributes (those not needed to create a GlobalVariable)
384/// from the GlobalVariable Src to this one.
Reid Klecknere7c78542017-05-11 21:14:29 +0000385void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000386 GlobalObject::copyAttributesFrom(Src);
Reid Klecknere7c78542017-05-11 21:14:29 +0000387 setThreadLocalMode(Src->getThreadLocalMode());
388 setExternallyInitialized(Src->isExternallyInitialized());
389 setAttributes(Src->getAttributes());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000390}
391
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000392void GlobalVariable::dropAllReferences() {
393 User::dropAllReferences();
394 clearMetadata();
395}
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000396
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000397//===----------------------------------------------------------------------===//
Dmitry Polukhincd835ad2016-03-31 14:16:21 +0000398// GlobalIndirectSymbol Implementation
399//===----------------------------------------------------------------------===//
400
401GlobalIndirectSymbol::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 Korobeynikova97b6942007-04-25 14:27:10 +0000410// GlobalAlias Implementation
411//===----------------------------------------------------------------------===//
412
David Blaikie16a2f3e2015-09-14 18:01:59 +0000413GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
414 const Twine &Name, Constant *Aliasee,
415 Module *ParentModule)
Dmitry Polukhincd835ad2016-03-31 14:16:21 +0000416 : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name,
417 Aliasee) {
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000418 if (ParentModule)
419 ParentModule->getAliasList().push_back(this);
420}
421
David Blaikie16a2f3e2015-09-14 18:01:59 +0000422GlobalAlias *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 Espindolaf1bedd3742014-05-17 21:29:57 +0000426}
Rafael Espindola83705652014-05-17 19:57:46 +0000427
David Blaikie16a2f3e2015-09-14 18:01:59 +0000428GlobalAlias *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 Espindolaf1bedd3742014-05-17 21:29:57 +0000432}
Rafael Espindola83705652014-05-17 19:57:46 +0000433
David Blaikie16a2f3e2015-09-14 18:01:59 +0000434GlobalAlias *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 Espindolaf1bedd3742014-05-17 21:29:57 +0000438}
Rafael Espindola83705652014-05-17 19:57:46 +0000439
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000440GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
Rafael Espindola64c1e182014-06-03 02:41:57 +0000441 GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000442 PointerType *PTy = Aliasee->getType();
David Blaikie16a2f3e2015-09-14 18:01:59 +0000443 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
444 Aliasee);
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000445}
446
Rafael Espindola64c1e182014-06-03 02:41:57 +0000447GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000448 return create(Aliasee->getLinkage(), Name, Aliasee);
449}
Rafael Espindola83705652014-05-17 19:57:46 +0000450
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000451void GlobalAlias::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000452 getParent()->getAliasList().remove(getIterator());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000453}
454
455void GlobalAlias::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000456 getParent()->getAliasList().erase(getIterator());
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000457}
458
Rafael Espindola64c1e182014-06-03 02:41:57 +0000459void GlobalAlias::setAliasee(Constant *Aliasee) {
Patrik Hagglund31541712014-06-04 11:21:11 +0000460 assert((!Aliasee || Aliasee->getType() == getType()) &&
Rafael Espindola64c1e182014-06-03 02:41:57 +0000461 "Alias and aliasee types should match!");
Dmitry Polukhincd835ad2016-03-31 14:16:21 +0000462 setIndirectSymbol(Aliasee);
Rafael Espindola64c1e182014-06-03 02:41:57 +0000463}
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000464
465//===----------------------------------------------------------------------===//
466// GlobalIFunc Implementation
467//===----------------------------------------------------------------------===//
468
469GlobalIFunc::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
478GlobalIFunc *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 Polukhina1feff72016-04-07 12:32:19 +0000484void GlobalIFunc::removeFromParent() {
485 getParent()->getIFuncList().remove(getIterator());
486}
487
488void GlobalIFunc::eraseFromParent() {
489 getParent()->getIFuncList().erase(getIterator());
490}