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 static const GlobalObject *getBaseObject(const Constant &C) {
63 // FIXME: We should probably return a base + offset pair for non-zero GEPs.
64 return dyn_cast<GlobalObject>(C.stripPointerCasts());
67 unsigned GlobalValue::getAlignment() const {
68 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
69 // In general we cannot compute this at the IR level, but we try.
70 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee()))
71 return GO->getAlignment();
73 // FIXME: we should also be able to handle:
74 // Alias = Global + Offset
78 return cast<GlobalObject>(this)->getAlignment();
81 void GlobalObject::setAlignment(unsigned Align) {
82 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
83 assert(Align <= MaximumAlignment &&
84 "Alignment is greater than MaximumAlignment!");
85 setGlobalValueSubClassData(Log2_32(Align) + 1);
86 assert(getAlignment() == Align && "Alignment representation error!");
89 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
90 const auto *GV = cast<GlobalObject>(Src);
91 GlobalValue::copyAttributesFrom(GV);
92 setAlignment(GV->getAlignment());
93 setSection(GV->getSection());
96 const char *GlobalValue::getSection() const {
97 if (auto *GA = dyn_cast<GlobalAlias>(this)) {
98 // In general we cannot compute this at the IR level, but we try.
99 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee()))
100 return GO->getSection();
103 return cast<GlobalObject>(this)->getSection();
106 void GlobalObject::setSection(StringRef S) { Section = S; }
108 bool GlobalValue::isDeclaration() const {
109 // Globals are definitions if they have an initializer.
110 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
111 return GV->getNumOperands() == 0;
113 // Functions are definitions if they have a body.
114 if (const Function *F = dyn_cast<Function>(this))
117 // Aliases are always definitions.
118 assert(isa<GlobalAlias>(this));
122 //===----------------------------------------------------------------------===//
123 // GlobalVariable Implementation
124 //===----------------------------------------------------------------------===//
126 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
127 Constant *InitVal, const Twine &Name,
128 ThreadLocalMode TLMode, unsigned AddressSpace,
129 bool isExternallyInitialized)
130 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
131 OperandTraits<GlobalVariable>::op_begin(this),
132 InitVal != nullptr, Link, Name),
133 isConstantGlobal(constant),
134 isExternallyInitializedConstant(isExternallyInitialized) {
135 setThreadLocalMode(TLMode);
137 assert(InitVal->getType() == Ty &&
138 "Initializer should be the same type as the GlobalVariable!");
142 LeakDetector::addGarbageObject(this);
145 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
146 LinkageTypes Link, Constant *InitVal,
147 const Twine &Name, GlobalVariable *Before,
148 ThreadLocalMode TLMode, unsigned AddressSpace,
149 bool isExternallyInitialized)
150 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
151 OperandTraits<GlobalVariable>::op_begin(this),
152 InitVal != nullptr, Link, Name),
153 isConstantGlobal(constant),
154 isExternallyInitializedConstant(isExternallyInitialized) {
155 setThreadLocalMode(TLMode);
157 assert(InitVal->getType() == Ty &&
158 "Initializer should be the same type as the GlobalVariable!");
162 LeakDetector::addGarbageObject(this);
165 Before->getParent()->getGlobalList().insert(Before, this);
167 M.getGlobalList().push_back(this);
170 void GlobalVariable::setParent(Module *parent) {
172 LeakDetector::addGarbageObject(this);
175 LeakDetector::removeGarbageObject(this);
178 void GlobalVariable::removeFromParent() {
179 getParent()->getGlobalList().remove(this);
182 void GlobalVariable::eraseFromParent() {
183 getParent()->getGlobalList().erase(this);
186 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
188 // If you call this, then you better know this GVar has a constant
189 // initializer worth replacing. Enforce that here.
190 assert(getNumOperands() == 1 &&
191 "Attempt to replace uses of Constants on a GVar with no initializer");
193 // And, since you know it has an initializer, the From value better be
194 // the initializer :)
195 assert(getOperand(0) == From &&
196 "Attempt to replace wrong constant initializer in GVar");
198 // And, you better have a constant for the replacement value
199 assert(isa<Constant>(To) &&
200 "Attempt to replace GVar initializer with non-constant");
202 // Okay, preconditions out of the way, replace the constant initializer.
203 this->setOperand(0, cast<Constant>(To));
206 void GlobalVariable::setInitializer(Constant *InitVal) {
208 if (hasInitializer()) {
209 Op<0>().set(nullptr);
213 assert(InitVal->getType() == getType()->getElementType() &&
214 "Initializer type must match GlobalVariable type");
215 if (!hasInitializer())
217 Op<0>().set(InitVal);
221 /// copyAttributesFrom - copy all additional attributes (those not needed to
222 /// create a GlobalVariable) from the GlobalVariable Src to this one.
223 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
224 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
225 GlobalObject::copyAttributesFrom(Src);
226 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
227 setThreadLocalMode(SrcVar->getThreadLocalMode());
231 //===----------------------------------------------------------------------===//
232 // GlobalAlias Implementation
233 //===----------------------------------------------------------------------===//
235 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
236 const Twine &Name, Constant *Aliasee,
237 Module *ParentModule)
238 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal,
239 &Op<0>(), 1, Link, Name) {
240 LeakDetector::addGarbageObject(this);
244 ParentModule->getAliasList().push_back(this);
247 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
248 LinkageTypes Link, const Twine &Name,
249 Constant *Aliasee, Module *ParentModule) {
250 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
253 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
254 LinkageTypes Linkage, const Twine &Name,
256 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
259 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
260 LinkageTypes Linkage, const Twine &Name,
261 GlobalValue *Aliasee) {
262 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
265 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
266 GlobalValue *Aliasee) {
267 PointerType *PTy = Aliasee->getType();
268 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name,
272 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
273 return create(Aliasee->getLinkage(), Name, Aliasee);
276 void GlobalAlias::setParent(Module *parent) {
278 LeakDetector::addGarbageObject(this);
281 LeakDetector::removeGarbageObject(this);
284 void GlobalAlias::removeFromParent() {
285 getParent()->getAliasList().remove(this);
288 void GlobalAlias::eraseFromParent() {
289 getParent()->getAliasList().erase(this);
292 void GlobalAlias::setAliasee(Constant *Aliasee) {
293 assert((!Aliasee || Aliasee->getType() == getType()) &&
294 "Alias and aliasee types should match!");
295 setOperand(0, Aliasee);