blob: cbd6450a20c9b138c9c222a0778497606bb4e5ff [file] [log] [blame]
Chris Lattnercc041ba2006-01-24 04:13:11 +00001//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Reid Spencere253cf62004-07-18 00:06:26 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Reid Spencere253cf62004-07-18 00:06:26 +00008//===----------------------------------------------------------------------===//
9//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the GlobalValue & GlobalVariable classes for the IR
Reid Spencere253cf62004-07-18 00:06:26 +000011// library.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthe3e43d92017-06-06 11:49:48 +000015#include "LLVMContextImpl.h"
Anton Korobeynikove846dd82008-03-11 22:28:56 +000016#include "llvm/ADT/SmallPtrSet.h"
James Y Knightb707e2a2016-01-15 16:33:06 +000017#include "llvm/ADT/Triple.h"
Peter Collingbourneb29976c2016-12-08 19:01:00 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000019#include "llvm/IR/Constants.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/GlobalAlias.h"
James Y Knightb707e2a2016-01-15 16:33:06 +000022#include "llvm/IR/GlobalValue.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Module.h"
Rafael Espindola2d21b252014-06-03 02:41:57 +000025#include "llvm/IR/Operator.h"
Peter Collingbourne76c218e2016-11-09 17:49:19 +000026#include "llvm/Support/Error.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000027#include "llvm/Support/ErrorHandling.h"
Reid Spencere253cf62004-07-18 00:06:26 +000028using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// GlobalValue Class
32//===----------------------------------------------------------------------===//
33
Reid Kleckner93378182016-12-29 00:55:51 +000034// GlobalValue should be a Constant, plus a type, a module, some flags, and an
35// intrinsic ID. Add an assert to prevent people from accidentally growing
36// GlobalValue while adding flags.
37static_assert(sizeof(GlobalValue) ==
38 sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
39 "unexpected GlobalValue size growth");
40
Reid Kleckner7b685032017-01-10 23:23:58 +000041// GlobalObject adds a comdat.
42static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
43 "unexpected GlobalObject size growth");
44
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000045bool GlobalValue::isMaterializable() const {
Rafael Espindola68b02dc2014-10-24 18:13:04 +000046 if (const Function *F = dyn_cast<Function>(this))
47 return F->isMaterializable();
48 return false;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000049}
Peter Collingbourne76c218e2016-11-09 17:49:19 +000050Error GlobalValue::materialize() {
Rafael Espindolac4982842014-10-24 22:50:48 +000051 return getParent()->materialize(this);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000052}
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000053
Pete Cooperd6218122015-06-23 21:55:11 +000054/// Override destroyConstantImpl to make sure it doesn't get called on
Reid Spencere253cf62004-07-18 00:06:26 +000055/// GlobalValue's because they shouldn't be treated like other constants.
Pete Cooperd6218122015-06-23 21:55:11 +000056void GlobalValue::destroyConstantImpl() {
57 llvm_unreachable("You can't GV->destroyConstantImpl()!");
Reid Spencere253cf62004-07-18 00:06:26 +000058}
Duncan Sands28c3cff2008-05-26 19:58:59 +000059
Mehdi Amini4c696962016-02-10 22:47:15 +000060Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
Pete Cooper234c5892015-06-24 18:55:24 +000061 llvm_unreachable("Unsupported class for handleOperandChange()!");
Pete Coopereab3c652015-06-24 00:05:07 +000062}
63
Duncan Sands28c3cff2008-05-26 19:58:59 +000064/// copyAttributesFrom - copy all additional attributes (those not needed to
65/// create a GlobalValue) from the GlobalValue Src to this one.
66void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
Duncan Sands28c3cff2008-05-26 19:58:59 +000067 setVisibility(Src->getVisibility());
Peter Collingbourne63b34cd2016-06-14 21:01:22 +000068 setUnnamedAddr(Src->getUnnamedAddr());
Rafael Espindola17c91692014-02-13 05:11:35 +000069 setDLLStorageClass(Src->getDLLStorageClass());
Sean Fertile509132b2017-10-26 15:00:26 +000070 setDSOLocal(Src->isDSOLocal());
Duncan Sands28c3cff2008-05-26 19:58:59 +000071}
72
Reid Klecknerc130a202017-05-11 21:14:29 +000073void GlobalValue::removeFromParent() {
74 switch (getValueID()) {
75#define HANDLE_GLOBAL_VALUE(NAME) \
76 case Value::NAME##Val: \
77 return static_cast<NAME *>(this)->removeFromParent();
78#include "llvm/IR/Value.def"
79 default:
80 break;
81 }
82 llvm_unreachable("not a global");
83}
84
85void GlobalValue::eraseFromParent() {
86 switch (getValueID()) {
87#define HANDLE_GLOBAL_VALUE(NAME) \
88 case Value::NAME##Val: \
89 return static_cast<NAME *>(this)->eraseFromParent();
90#include "llvm/IR/Value.def"
91 default:
92 break;
93 }
94 llvm_unreachable("not a global");
95}
96
Rafael Espindola2d21b252014-06-03 02:41:57 +000097unsigned GlobalValue::getAlignment() const {
98 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
99 // In general we cannot compute this at the IR level, but we try.
David Majnemerc8a11692014-06-27 18:19:56 +0000100 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola2d21b252014-06-03 02:41:57 +0000101 return GO->getAlignment();
102
103 // FIXME: we should also be able to handle:
104 // Alias = Global + Offset
105 // Alias = Absolute
106 return 0;
107 }
Rafael Espindola834384b2014-05-13 18:45:48 +0000108 return cast<GlobalObject>(this)->getAlignment();
Rafael Espindolab8894482014-05-06 16:48:58 +0000109}
110
Alexander Richardson47ff67b2018-08-23 09:25:17 +0000111unsigned GlobalValue::getAddressSpace() const {
112 PointerType *PtrTy = getType();
113 return PtrTy->getAddressSpace();
114}
115
Rafael Espindola834384b2014-05-13 18:45:48 +0000116void GlobalObject::setAlignment(unsigned Align) {
Dan Gohman6bbe6712010-07-28 20:56:48 +0000117 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
118 assert(Align <= MaximumAlignment &&
119 "Alignment is greater than MaximumAlignment!");
Rafael Espindola68b02dc2014-10-24 18:13:04 +0000120 unsigned AlignmentData = Log2_32(Align) + 1;
121 unsigned OldData = getGlobalValueSubClassData();
122 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
Dan Gohman6bbe6712010-07-28 20:56:48 +0000123 assert(getAlignment() == Align && "Alignment representation error!");
124}
Chris Lattner6c482442011-07-14 18:10:41 +0000125
Reid Klecknerc130a202017-05-11 21:14:29 +0000126void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
Rafael Espindola88559242015-12-02 20:03:17 +0000127 GlobalValue::copyAttributesFrom(Src);
Reid Klecknerc130a202017-05-11 21:14:29 +0000128 setAlignment(Src->getAlignment());
129 setSection(Src->getSection());
Rafael Espindola834384b2014-05-13 18:45:48 +0000130}
131
Teresa Johnson54d058e2016-03-15 02:13:19 +0000132std::string GlobalValue::getGlobalIdentifier(StringRef Name,
133 GlobalValue::LinkageTypes Linkage,
134 StringRef FileName) {
135
136 // Value names may be prefixed with a binary '1' to indicate
137 // that the backend should not modify the symbols due to any platform
138 // naming convention. Do not include that '1' in the PGO profile name.
139 if (Name[0] == '\1')
140 Name = Name.substr(1);
141
142 std::string NewName = Name;
143 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
144 // For local symbols, prepend the main file name to distinguish them.
145 // Do not include the full path in the file name since there's no guarantee
146 // that it will stay the same, e.g., if the files are checked out from
147 // version control in different locations.
148 if (FileName.empty())
149 NewName = NewName.insert(0, "<unknown>:");
150 else
151 NewName = NewName.insert(0, FileName.str() + ":");
152 }
153 return NewName;
154}
155
Mehdi Amini39834162016-04-02 05:25:27 +0000156std::string GlobalValue::getGlobalIdentifier() const {
Mehdi Aminifacd1242016-03-25 05:57:41 +0000157 return getGlobalIdentifier(getName(), getLinkage(),
158 getParent()->getSourceFileName());
159}
160
Rafael Espindolae973d3f2016-05-11 18:21:59 +0000161StringRef GlobalValue::getSection() const {
Rafael Espindola2d21b252014-06-03 02:41:57 +0000162 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
163 // In general we cannot compute this at the IR level, but we try.
David Majnemerc8a11692014-06-27 18:19:56 +0000164 if (const GlobalObject *GO = GA->getBaseObject())
Rafael Espindola2d21b252014-06-03 02:41:57 +0000165 return GO->getSection();
166 return "";
167 }
Rafael Espindola834384b2014-05-13 18:45:48 +0000168 return cast<GlobalObject>(this)->getSection();
Rafael Espindola26668d02014-05-06 22:44:30 +0000169}
170
Craig Topper306a7df2017-03-27 05:47:03 +0000171const Comdat *GlobalValue::getComdat() const {
David Majnemerc8a11692014-06-27 18:19:56 +0000172 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
173 // In general we cannot compute this at the IR level, but we try.
174 if (const GlobalObject *GO = GA->getBaseObject())
175 return const_cast<GlobalObject *>(GO)->getComdat();
176 return nullptr;
177 }
Dmitry Polukhinba492232016-04-07 12:32:19 +0000178 // ifunc and its resolver are separate things so don't use resolver comdat.
179 if (isa<GlobalIFunc>(this))
180 return nullptr;
David Majnemerc8a11692014-06-27 18:19:56 +0000181 return cast<GlobalObject>(this)->getComdat();
182}
183
Reid Kleckner7b685032017-01-10 23:23:58 +0000184StringRef GlobalObject::getSectionImpl() const {
185 assert(hasSection());
186 return getContext().pImpl->GlobalObjectSections[this];
187}
Rafael Espindolae973d3f2016-05-11 18:21:59 +0000188
Reid Kleckner7b685032017-01-10 23:23:58 +0000189void GlobalObject::setSection(StringRef S) {
190 // Do nothing if we're clearing the section and it is already empty.
191 if (!hasSection() && S.empty())
192 return;
193
194 // Get or create a stable section name string and put it in the table in the
195 // context.
Keno Fischerb863b6b2017-02-15 21:42:42 +0000196 if (!S.empty()) {
197 S = getContext().pImpl->SectionStrings.insert(S).first->first();
198 }
Reid Kleckner7b685032017-01-10 23:23:58 +0000199 getContext().pImpl->GlobalObjectSections[this] = S;
200
201 // Update the HasSectionHashEntryBit. Setting the section to the empty string
202 // means this global no longer has a section.
203 setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
Rafael Espindolae973d3f2016-05-11 18:21:59 +0000204}
Rafael Espindola4f172352014-05-06 14:59:14 +0000205
Chris Lattner6c482442011-07-14 18:10:41 +0000206bool GlobalValue::isDeclaration() const {
Chris Lattner97d97302011-07-14 18:12:44 +0000207 // Globals are definitions if they have an initializer.
Chris Lattner6c482442011-07-14 18:10:41 +0000208 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
209 return GV->getNumOperands() == 0;
210
Chris Lattner97d97302011-07-14 18:12:44 +0000211 // Functions are definitions if they have a body.
Chris Lattner6c482442011-07-14 18:10:41 +0000212 if (const Function *F = dyn_cast<Function>(this))
Rafael Espindola68b02dc2014-10-24 18:13:04 +0000213 return F->empty() && !F->isMaterializable();
Chris Lattner97d97302011-07-14 18:12:44 +0000214
Dmitry Polukhin51a06a22016-04-05 08:47:51 +0000215 // Aliases and ifuncs are always definitions.
216 assert(isa<GlobalIndirectSymbol>(this));
Chris Lattner6c482442011-07-14 18:10:41 +0000217 return false;
218}
Rafael Espindolaf9929302014-05-09 14:31:07 +0000219
James Y Knightb707e2a2016-01-15 16:33:06 +0000220bool GlobalValue::canIncreaseAlignment() const {
221 // Firstly, can only increase the alignment of a global if it
222 // is a strong definition.
223 if (!isStrongDefinitionForLinker())
224 return false;
225
226 // It also has to either not have a section defined, or, not have
227 // alignment specified. (If it is assigned a section, the global
228 // could be densely packed with other objects in the section, and
229 // increasing the alignment could cause padding issues.)
230 if (hasSection() && getAlignment() > 0)
231 return false;
232
233 // On ELF platforms, we're further restricted in that we can't
234 // increase the alignment of any variable which might be emitted
235 // into a shared library, and which is exported. If the main
236 // executable accesses a variable found in a shared-lib, the main
237 // exe actually allocates memory for and exports the symbol ITSELF,
238 // overriding the symbol found in the library. That is, at link
239 // time, the observed alignment of the variable is copied into the
240 // executable binary. (A COPY relocation is also generated, to copy
241 // the initial data from the shadowed variable in the shared-lib
242 // into the location in the main binary, before running code.)
243 //
244 // And thus, even though you might think you are defining the
245 // global, and allocating the memory for the global in your object
246 // file, and thus should be able to set the alignment arbitrarily,
247 // that's not actually true. Doing so can cause an ABI breakage; an
248 // executable might have already been built with the previous
249 // alignment of the variable, and then assuming an increased
250 // alignment will be incorrect.
251
252 // Conservatively assume ELF if there's no parent pointer.
253 bool isELF =
254 (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF());
Eli Friedman58970cd2018-10-31 23:03:58 +0000255 if (isELF && !isDSOLocal())
James Y Knightb707e2a2016-01-15 16:33:06 +0000256 return false;
257
258 return true;
259}
260
Craig Topper306a7df2017-03-27 05:47:03 +0000261const GlobalObject *GlobalValue::getBaseObject() const {
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000262 if (auto *GO = dyn_cast<GlobalObject>(this))
263 return GO;
Teresa Johnson34e726e2017-05-15 18:28:29 +0000264 if (auto *GA = dyn_cast<GlobalIndirectSymbol>(this))
Peter Collingbourne80e2a2f2016-10-24 19:23:39 +0000265 return GA->getBaseObject();
266 return nullptr;
267}
268
Peter Collingbourneb29976c2016-12-08 19:01:00 +0000269bool GlobalValue::isAbsoluteSymbolRef() const {
270 auto *GO = dyn_cast<GlobalObject>(this);
271 if (!GO)
272 return false;
273
274 return GO->getMetadata(LLVMContext::MD_absolute_symbol);
275}
276
277Optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const {
278 auto *GO = dyn_cast<GlobalObject>(this);
279 if (!GO)
280 return None;
281
282 MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
283 if (!MD)
284 return None;
285
286 return getConstantRangeFromMetadata(*MD);
287}
288
David Blaikied7ca9162018-03-21 19:23:45 +0000289bool GlobalValue::canBeOmittedFromSymbolTable() const {
290 if (!hasLinkOnceODRLinkage())
291 return false;
292
293 // We assume that anyone who sets global unnamed_addr on a non-constant
294 // knows what they're doing.
295 if (hasGlobalUnnamedAddr())
296 return true;
297
298 // If it is a non constant variable, it needs to be uniqued across shared
299 // objects.
300 if (auto *Var = dyn_cast<GlobalVariable>(this))
301 if (!Var->isConstant())
302 return false;
303
304 return hasAtLeastLocalUnnamedAddr();
305}
306
Reid Spencere253cf62004-07-18 00:06:26 +0000307//===----------------------------------------------------------------------===//
308// GlobalVariable Implementation
309//===----------------------------------------------------------------------===//
310
Hans Wennborgce718ff2012-06-23 11:37:03 +0000311GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
Rafael Espindolaf35cc812014-05-09 15:49:02 +0000312 Constant *InitVal, const Twine &Name,
313 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesmanaf2f4942013-02-03 21:54:38 +0000314 bool isExternallyInitialized)
David Blaikie7b1f1042015-08-21 21:35:28 +0000315 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola834384b2014-05-13 18:45:48 +0000316 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikie7b1f1042015-08-21 21:35:28 +0000317 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola665d42a2014-05-28 18:15:43 +0000318 isConstantGlobal(constant),
Rafael Espindolaf35cc812014-05-09 15:49:02 +0000319 isExternallyInitializedConstant(isExternallyInitialized) {
Peter Collingbournef38a0162017-06-04 22:12:03 +0000320 assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
321 "invalid type for global variable");
Rafael Espindola665d42a2014-05-28 18:15:43 +0000322 setThreadLocalMode(TLMode);
Chris Lattner96d83f62005-01-29 00:35:33 +0000323 if (InitVal) {
324 assert(InitVal->getType() == Ty &&
Alkis Evlogimenos82439762004-08-05 11:28:34 +0000325 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000326 Op<0>() = InitVal;
Alkis Evlogimenos82439762004-08-05 11:28:34 +0000327 }
Reid Spencere253cf62004-07-18 00:06:26 +0000328}
329
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000330GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersone9b11b42009-07-08 19:03:57 +0000331 LinkageTypes Link, Constant *InitVal,
Rafael Espindolaf35cc812014-05-09 15:49:02 +0000332 const Twine &Name, GlobalVariable *Before,
333 ThreadLocalMode TLMode, unsigned AddressSpace,
Michael Gottesmanaf2f4942013-02-03 21:54:38 +0000334 bool isExternallyInitialized)
David Blaikie7b1f1042015-08-21 21:35:28 +0000335 : GlobalObject(Ty, Value::GlobalVariableVal,
Rafael Espindola834384b2014-05-13 18:45:48 +0000336 OperandTraits<GlobalVariable>::op_begin(this),
David Blaikie7b1f1042015-08-21 21:35:28 +0000337 InitVal != nullptr, Link, Name, AddressSpace),
Rafael Espindola665d42a2014-05-28 18:15:43 +0000338 isConstantGlobal(constant),
Rafael Espindolaf35cc812014-05-09 15:49:02 +0000339 isExternallyInitializedConstant(isExternallyInitialized) {
Peter Collingbournef38a0162017-06-04 22:12:03 +0000340 assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
341 "invalid type for global variable");
Rafael Espindola665d42a2014-05-28 18:15:43 +0000342 setThreadLocalMode(TLMode);
Chris Lattneradc95462006-09-30 21:31:26 +0000343 if (InitVal) {
344 assert(InitVal->getType() == Ty &&
345 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000346 Op<0>() = InitVal;
Chris Lattneradc95462006-09-30 21:31:26 +0000347 }
Rafael Espindolaf9929302014-05-09 14:31:07 +0000348
Chris Lattneradc95462006-09-30 21:31:26 +0000349 if (Before)
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000350 Before->getParent()->getGlobalList().insert(Before->getIterator(), this);
Owen Andersone9b11b42009-07-08 19:03:57 +0000351 else
352 M.getGlobalList().push_back(this);
Chris Lattneradc95462006-09-30 21:31:26 +0000353}
354
Chris Lattner4b833802004-10-11 22:21:39 +0000355void GlobalVariable::removeFromParent() {
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000356 getParent()->getGlobalList().remove(getIterator());
Chris Lattner4b833802004-10-11 22:21:39 +0000357}
358
359void GlobalVariable::eraseFromParent() {
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000360 getParent()->getGlobalList().erase(getIterator());
Chris Lattner4b833802004-10-11 22:21:39 +0000361}
362
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000363void GlobalVariable::setInitializer(Constant *InitVal) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000364 if (!InitVal) {
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000365 if (hasInitializer()) {
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000366 // Note, the num operands is used to compute the offset of the operand, so
367 // the order here matters. Clearing the operand then clearing the num
368 // operands ensures we have the correct offset to the operand.
Craig Topperec0f0bc2014-04-09 06:08:46 +0000369 Op<0>().set(nullptr);
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000370 setGlobalVariableNumOperands(0);
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000371 }
372 } else {
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000373 assert(InitVal->getType() == getValueType() &&
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000374 "Initializer type must match GlobalVariable type");
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000375 // Note, the num operands is used to compute the offset of the operand, so
376 // the order here matters. We need to set num operands to 1 first so that
377 // we get the correct offset to the first operand when we set it.
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000378 if (!hasInitializer())
Pete Cooperaaa3fa62015-06-12 17:48:10 +0000379 setGlobalVariableNumOperands(1);
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000380 Op<0>().set(InitVal);
381 }
382}
383
Rafael Espindola88559242015-12-02 20:03:17 +0000384/// Copy all additional attributes (those not needed to create a GlobalVariable)
385/// from the GlobalVariable Src to this one.
Reid Klecknerc130a202017-05-11 21:14:29 +0000386void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
Rafael Espindola834384b2014-05-13 18:45:48 +0000387 GlobalObject::copyAttributesFrom(Src);
Reid Klecknerc130a202017-05-11 21:14:29 +0000388 setThreadLocalMode(Src->getThreadLocalMode());
389 setExternallyInitialized(Src->isExternallyInitialized());
390 setAttributes(Src->getAttributes());
Duncan Sands28c3cff2008-05-26 19:58:59 +0000391}
392
Peter Collingbourne6aef9f92016-05-31 23:01:54 +0000393void GlobalVariable::dropAllReferences() {
394 User::dropAllReferences();
395 clearMetadata();
396}
Duncan Sands28c3cff2008-05-26 19:58:59 +0000397
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000398//===----------------------------------------------------------------------===//
Dmitry Polukhindc022ba2016-03-31 14:16:21 +0000399// GlobalIndirectSymbol Implementation
400//===----------------------------------------------------------------------===//
401
402GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy,
403 unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name,
404 Constant *Symbol)
405 : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) {
406 Op<0>() = Symbol;
407}
408
409
410//===----------------------------------------------------------------------===//
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000411// GlobalAlias Implementation
412//===----------------------------------------------------------------------===//
413
David Blaikie2d353482015-09-14 18:01:59 +0000414GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
415 const Twine &Name, Constant *Aliasee,
416 Module *ParentModule)
Dmitry Polukhindc022ba2016-03-31 14:16:21 +0000417 : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name,
418 Aliasee) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000419 if (ParentModule)
420 ParentModule->getAliasList().push_back(this);
421}
422
David Blaikie2d353482015-09-14 18:01:59 +0000423GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
424 LinkageTypes Link, const Twine &Name,
425 Constant *Aliasee, Module *ParentModule) {
426 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
Rafael Espindolac86235f2014-05-17 21:29:57 +0000427}
Rafael Espindolafbd8cc02014-05-17 19:57:46 +0000428
David Blaikie2d353482015-09-14 18:01:59 +0000429GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
430 LinkageTypes Linkage, const Twine &Name,
431 Module *Parent) {
432 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
Rafael Espindolac86235f2014-05-17 21:29:57 +0000433}
Rafael Espindolafbd8cc02014-05-17 19:57:46 +0000434
David Blaikie2d353482015-09-14 18:01:59 +0000435GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
436 LinkageTypes Linkage, const Twine &Name,
437 GlobalValue *Aliasee) {
438 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
Rafael Espindolac86235f2014-05-17 21:29:57 +0000439}
Rafael Espindolafbd8cc02014-05-17 19:57:46 +0000440
Rafael Espindolac86235f2014-05-17 21:29:57 +0000441GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
Rafael Espindola2d21b252014-06-03 02:41:57 +0000442 GlobalValue *Aliasee) {
Rafael Espindolac86235f2014-05-17 21:29:57 +0000443 PointerType *PTy = Aliasee->getType();
David Blaikie2d353482015-09-14 18:01:59 +0000444 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
445 Aliasee);
Rafael Espindolac86235f2014-05-17 21:29:57 +0000446}
447
Rafael Espindola2d21b252014-06-03 02:41:57 +0000448GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
Rafael Espindolac86235f2014-05-17 21:29:57 +0000449 return create(Aliasee->getLinkage(), Name, Aliasee);
450}
Rafael Espindolafbd8cc02014-05-17 19:57:46 +0000451
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000452void GlobalAlias::removeFromParent() {
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000453 getParent()->getAliasList().remove(getIterator());
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000454}
455
456void GlobalAlias::eraseFromParent() {
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000457 getParent()->getAliasList().erase(getIterator());
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000458}
459
Rafael Espindola2d21b252014-06-03 02:41:57 +0000460void GlobalAlias::setAliasee(Constant *Aliasee) {
Patrik Hagglunde0e4fca2014-06-04 11:21:11 +0000461 assert((!Aliasee || Aliasee->getType() == getType()) &&
Rafael Espindola2d21b252014-06-03 02:41:57 +0000462 "Alias and aliasee types should match!");
Dmitry Polukhindc022ba2016-03-31 14:16:21 +0000463 setIndirectSymbol(Aliasee);
Rafael Espindola2d21b252014-06-03 02:41:57 +0000464}
Dmitry Polukhinba492232016-04-07 12:32:19 +0000465
466//===----------------------------------------------------------------------===//
467// GlobalIFunc Implementation
468//===----------------------------------------------------------------------===//
469
470GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
471 const Twine &Name, Constant *Resolver,
472 Module *ParentModule)
473 : GlobalIndirectSymbol(Ty, Value::GlobalIFuncVal, AddressSpace, Link, Name,
474 Resolver) {
475 if (ParentModule)
476 ParentModule->getIFuncList().push_back(this);
477}
478
479GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace,
480 LinkageTypes Link, const Twine &Name,
481 Constant *Resolver, Module *ParentModule) {
482 return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule);
483}
484
Dmitry Polukhinba492232016-04-07 12:32:19 +0000485void GlobalIFunc::removeFromParent() {
486 getParent()->getIFuncList().remove(getIterator());
487}
488
489void GlobalIFunc::eraseFromParent() {
490 getParent()->getIFuncList().erase(getIterator());
491}