1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the IR
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/GlobalValue.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/GlobalAlias.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/LeakDetector.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Operator.h"
24 #include "llvm/Support/ErrorHandling.h"
27 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
31 bool GlobalValue::isMaterializable() const {
32 return getParent() && getParent()->isMaterializable(this);
34 bool GlobalValue::isDematerializable() const {
35 return getParent() && getParent()->isDematerializable(this);
37 bool GlobalValue::Materialize(std::string *ErrInfo) {
38 return getParent()->Materialize(this, ErrInfo);
40 void GlobalValue::Dematerialize() {
41 getParent()->Dematerialize(this);
44 const DataLayout *GlobalValue::getDataLayout() const {
45 return getParent()->getDataLayout();
48 /// Override destroyConstant to make sure it doesn't get called on
49 /// GlobalValue's because they shouldn't be treated like other constants.
50 void GlobalValue::destroyConstant() {
51 llvm_unreachable("You can't GV->destroyConstant()!");
54 /// copyAttributesFrom - copy all additional attributes (those not needed to
55 /// create a GlobalValue) from the GlobalValue Src to this one.
56 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
57 setVisibility(Src->getVisibility());
58 setUnnamedAddr(Src->hasUnnamedAddr());
59 setDLLStorageClass(Src->getDLLStorageClass());
62 unsigned GlobalValue::getAlignment() const {
63 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
64 // In general we cannot compute this at the IR level, but we try.
65 if (const GlobalObject *GO = GA->getBaseObject())
66 return GO->getAlignment();
68 // FIXME: we should also be able to handle:
69 // Alias = Global + Offset
73 return cast<GlobalObject>(this)->getAlignment();
76 void GlobalObject::setAlignment(unsigned Align) {
77 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
78 assert(Align <= MaximumAlignment &&
79 "Alignment is greater than MaximumAlignment!");
80 setGlobalValueSubClassData(Log2_32(Align) + 1);
81 assert(getAlignment() == Align && "Alignment representation error!");
84 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
85 const auto *GV = cast<GlobalObject>(Src);
86 GlobalValue::copyAttributesFrom(GV);
87 setAlignment(GV->getAlignment());
88 setSection(GV->getSection());
91 const char *GlobalValue::getSection() const {
92 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
93 // In general we cannot compute this at the IR level, but we try.
94 if (const GlobalObject *GO = GA->getBaseObject())
95 return GO->getSection();
98 return cast<GlobalObject>(this)->getSection();
101 Comdat *GlobalValue::getComdat() {
102 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
103 // In general we cannot compute this at the IR level, but we try.
104 if (const GlobalObject *GO = GA->getBaseObject())
105 return const_cast<GlobalObject *>(GO)->getComdat();
108 return cast<GlobalObject>(this)->getComdat();
111 void GlobalObject::setSection(StringRef S) { Section = S; }
113 bool GlobalValue::isDeclaration() const {
114 // Globals are definitions if they have an initializer.
115 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
116 return GV->getNumOperands() == 0;
118 // Functions are definitions if they have a body.
119 if (const Function *F = dyn_cast<Function>(this))
122 // Aliases are always definitions.
123 assert(isa<GlobalAlias>(this));
127 //===----------------------------------------------------------------------===//
128 // GlobalVariable Implementation
129 //===----------------------------------------------------------------------===//
131 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
132 Constant *InitVal, const Twine &Name,
133 ThreadLocalMode TLMode, unsigned AddressSpace,
134 bool isExternallyInitialized)
135 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
136 OperandTraits<GlobalVariable>::op_begin(this),
137 InitVal != nullptr, Link, Name),
138 isConstantGlobal(constant),
139 isExternallyInitializedConstant(isExternallyInitialized) {
140 setThreadLocalMode(TLMode);
142 assert(InitVal->getType() == Ty &&
143 "Initializer should be the same type as the GlobalVariable!");
147 LeakDetector::addGarbageObject(this);
150 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
151 LinkageTypes Link, Constant *InitVal,
152 const Twine &Name, GlobalVariable *Before,
153 ThreadLocalMode TLMode, unsigned AddressSpace,
154 bool isExternallyInitialized)
155 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
156 OperandTraits<GlobalVariable>::op_begin(this),
157 InitVal != nullptr, Link, Name),
158 isConstantGlobal(constant),
159 isExternallyInitializedConstant(isExternallyInitialized) {
160 setThreadLocalMode(TLMode);
162 assert(InitVal->getType() == Ty &&
163 "Initializer should be the same type as the GlobalVariable!");
167 LeakDetector::addGarbageObject(this);
170 Before->getParent()->getGlobalList().insert(Before, this);
172 M.getGlobalList().push_back(this);
175 void GlobalVariable::setParent(Module *parent) {
177 LeakDetector::addGarbageObject(this);
180 LeakDetector::removeGarbageObject(this);
183 void GlobalVariable::removeFromParent() {
184 getParent()->getGlobalList().remove(this);
187 void GlobalVariable::eraseFromParent() {
188 getParent()->getGlobalList().erase(this);
191 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
193 // If you call this, then you better know this GVar has a constant
194 // initializer worth replacing. Enforce that here.
195 assert(getNumOperands() == 1 &&
196 "Attempt to replace uses of Constants on a GVar with no initializer");
198 // And, since you know it has an initializer, the From value better be
199 // the initializer :)
200 assert(getOperand(0) == From &&
201 "Attempt to replace wrong constant initializer in GVar");
203 // And, you better have a constant for the replacement value
204 assert(isa<Constant>(To) &&
205 "Attempt to replace GVar initializer with non-constant");
207 // Okay, preconditions out of the way, replace the constant initializer.
208 this->setOperand(0, cast<Constant>(To));
211 void GlobalVariable::setInitializer(Constant *InitVal) {
213 if (hasInitializer()) {
214 Op<0>().set(nullptr);
218 assert(InitVal->getType() == getType()->getElementType() &&
219 "Initializer type must match GlobalVariable type");
220 if (!hasInitializer())
222 Op<0>().set(InitVal);
226 /// copyAttributesFrom - copy all additional attributes (those not needed to
227 /// create a GlobalVariable) from the GlobalVariable Src to this one.
228 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
229 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
230 GlobalObject::copyAttributesFrom(Src);
231 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
232 setThreadLocalMode(SrcVar->getThreadLocalMode());
236 //===----------------------------------------------------------------------===//
237 // GlobalAlias Implementation
238 //===----------------------------------------------------------------------===//
240 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
241 const Twine &Name, Constant *Aliasee,
242 Module *ParentModule)
243 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
244 &Op<0>(), 1, Link, Name) {
245 LeakDetector::addGarbageObject(this);
249 ParentModule->getAliasList().push_back(this);
252 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
253 LinkageTypes Link, const Twine &Name,
254 Constant *Aliasee, Module *ParentModule) {
255 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
258 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
259 LinkageTypes Linkage, const Twine &Name,
261 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
264 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
265 LinkageTypes Linkage, const Twine &Name,
266 GlobalValue *Aliasee) {
267 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
270 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
271 GlobalValue *Aliasee) {
272 PointerType *PTy = Aliasee->getType();
273 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
277 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
278 return create(Aliasee->getLinkage(), Name, Aliasee);
281 void GlobalAlias::setParent(Module *parent) {
283 LeakDetector::addGarbageObject(this);
286 LeakDetector::removeGarbageObject(this);
289 void GlobalAlias::removeFromParent() {
290 getParent()->getAliasList().remove(this);
293 void GlobalAlias::eraseFromParent() {
294 getParent()->getAliasList().erase(this);
297 void GlobalAlias::setAliasee(Constant *Aliasee) {
298 assert((!Aliasee || Aliasee->getType() == getType()) &&
299 "Alias and aliasee types should match!");
300 setOperand(0, Aliasee);