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/Support/ErrorHandling.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 bool GlobalValue::isMaterializable() const {
31 return getParent() && getParent()->isMaterializable(this);
33 bool GlobalValue::isDematerializable() const {
34 return getParent() && getParent()->isDematerializable(this);
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
39 void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
43 const DataLayout *GlobalValue::getDataLayout() const {
44 return getParent()->getDataLayout();
47 /// Override destroyConstant to make sure it doesn't get called on
48 /// GlobalValue's because they shouldn't be treated like other constants.
49 void GlobalValue::destroyConstant() {
50 llvm_unreachable("You can't GV->destroyConstant()!");
53 /// copyAttributesFrom - copy all additional attributes (those not needed to
54 /// create a GlobalValue) from the GlobalValue Src to this one.
55 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
56 if (!isa<GlobalAlias>(this)) {
57 setAlignment(Src->getAlignment());
58 setSection(Src->getSection());
61 setVisibility(Src->getVisibility());
62 setUnnamedAddr(Src->hasUnnamedAddr());
63 setDLLStorageClass(Src->getDLLStorageClass());
66 unsigned GlobalValue::getAlignment() const {
67 if (auto *GA = dyn_cast<GlobalAlias>(this))
68 return GA->getAliasedGlobal()->getAlignment();
70 return (1u << Alignment) >> 1;
73 void GlobalValue::setAlignment(unsigned Align) {
74 assert((!isa<GlobalAlias>(this)) &&
75 "GlobalAlias should not have an alignment!");
76 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
77 assert(Align <= MaximumAlignment &&
78 "Alignment is greater than MaximumAlignment!");
79 Alignment = Log2_32(Align) + 1;
80 assert(getAlignment() == Align && "Alignment representation error!");
83 const std::string &GlobalValue::getSection() const {
84 if (auto *GA = dyn_cast<GlobalAlias>(this))
85 return GA->getAliasedGlobal()->getSection();
89 void GlobalValue::setSection(StringRef S) {
90 assert(!isa<GlobalAlias>(this) && "GlobalAlias should not have a section!");
94 bool GlobalValue::isDeclaration() const {
95 // Globals are definitions if they have an initializer.
96 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
97 return GV->getNumOperands() == 0;
99 // Functions are definitions if they have a body.
100 if (const Function *F = dyn_cast<Function>(this))
103 // Aliases are always definitions.
104 assert(isa<GlobalAlias>(this));
108 //===----------------------------------------------------------------------===//
109 // GlobalVariable Implementation
110 //===----------------------------------------------------------------------===//
112 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
114 const Twine &Name, ThreadLocalMode TLMode,
115 unsigned AddressSpace,
116 bool isExternallyInitialized)
117 : GlobalValue(PointerType::get(Ty, AddressSpace),
118 Value::GlobalVariableVal,
119 OperandTraits<GlobalVariable>::op_begin(this),
120 InitVal != nullptr, Link, Name),
121 isConstantGlobal(constant), threadLocalMode(TLMode),
122 isExternallyInitializedConstant(isExternallyInitialized) {
124 assert(InitVal->getType() == Ty &&
125 "Initializer should be the same type as the GlobalVariable!");
129 LeakDetector::addGarbageObject(this);
132 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
133 LinkageTypes Link, Constant *InitVal,
135 GlobalVariable *Before, ThreadLocalMode TLMode,
136 unsigned AddressSpace,
137 bool isExternallyInitialized)
138 : GlobalValue(PointerType::get(Ty, AddressSpace),
139 Value::GlobalVariableVal,
140 OperandTraits<GlobalVariable>::op_begin(this),
141 InitVal != nullptr, Link, Name),
142 isConstantGlobal(constant), threadLocalMode(TLMode),
143 isExternallyInitializedConstant(isExternallyInitialized) {
145 assert(InitVal->getType() == Ty &&
146 "Initializer should be the same type as the GlobalVariable!");
150 LeakDetector::addGarbageObject(this);
153 Before->getParent()->getGlobalList().insert(Before, this);
155 M.getGlobalList().push_back(this);
158 void GlobalVariable::setParent(Module *parent) {
160 LeakDetector::addGarbageObject(this);
163 LeakDetector::removeGarbageObject(this);
166 void GlobalVariable::removeFromParent() {
167 getParent()->getGlobalList().remove(this);
170 void GlobalVariable::eraseFromParent() {
171 getParent()->getGlobalList().erase(this);
174 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
176 // If you call this, then you better know this GVar has a constant
177 // initializer worth replacing. Enforce that here.
178 assert(getNumOperands() == 1 &&
179 "Attempt to replace uses of Constants on a GVar with no initializer");
181 // And, since you know it has an initializer, the From value better be
182 // the initializer :)
183 assert(getOperand(0) == From &&
184 "Attempt to replace wrong constant initializer in GVar");
186 // And, you better have a constant for the replacement value
187 assert(isa<Constant>(To) &&
188 "Attempt to replace GVar initializer with non-constant");
190 // Okay, preconditions out of the way, replace the constant initializer.
191 this->setOperand(0, cast<Constant>(To));
194 void GlobalVariable::setInitializer(Constant *InitVal) {
196 if (hasInitializer()) {
197 Op<0>().set(nullptr);
201 assert(InitVal->getType() == getType()->getElementType() &&
202 "Initializer type must match GlobalVariable type");
203 if (!hasInitializer())
205 Op<0>().set(InitVal);
209 /// copyAttributesFrom - copy all additional attributes (those not needed to
210 /// create a GlobalVariable) from the GlobalVariable Src to this one.
211 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
212 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
213 GlobalValue::copyAttributesFrom(Src);
214 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
215 setThreadLocalMode(SrcVar->getThreadLocalMode());
219 //===----------------------------------------------------------------------===//
220 // GlobalAlias Implementation
221 //===----------------------------------------------------------------------===//
223 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
224 const Twine &Name, Constant* aliasee,
225 Module *ParentModule)
226 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
227 LeakDetector::addGarbageObject(this);
230 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
234 ParentModule->getAliasList().push_back(this);
237 void GlobalAlias::setParent(Module *parent) {
239 LeakDetector::addGarbageObject(this);
242 LeakDetector::removeGarbageObject(this);
245 void GlobalAlias::removeFromParent() {
246 getParent()->getAliasList().remove(this);
249 void GlobalAlias::eraseFromParent() {
250 getParent()->getAliasList().erase(this);
253 void GlobalAlias::setAliasee(Constant *Aliasee) {
254 assert((!Aliasee || Aliasee->getType() == getType()) &&
255 "Alias and aliasee types should match!");
257 setOperand(0, Aliasee);
260 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
261 Constant *C = GA->getAliasee();
262 assert(C && "Must alias something");
264 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
267 ConstantExpr *CE = cast<ConstantExpr>(C);
268 assert((CE->getOpcode() == Instruction::BitCast ||
269 CE->getOpcode() == Instruction::AddrSpaceCast ||
270 CE->getOpcode() == Instruction::GetElementPtr) &&
271 "Unsupported aliasee");
273 return cast<GlobalValue>(CE->getOperand(0));
276 GlobalValue *GlobalAlias::getAliasedGlobal() {
277 SmallPtrSet<GlobalValue*, 3> Visited;
279 GlobalAlias *GA = this;
282 GlobalValue *GV = getAliaseeGV(GA);
283 if (!Visited.insert(GV))
286 // Iterate over aliasing chain.
287 GA = dyn_cast<GlobalAlias>(GV);