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 setAlignment(Src->getAlignment());
57 setSection(Src->getSection());
58 setVisibility(Src->getVisibility());
59 setUnnamedAddr(Src->hasUnnamedAddr());
60 setDLLStorageClass(Src->getDLLStorageClass());
63 void GlobalValue::setAlignment(unsigned Align) {
64 assert((!isa<GlobalAlias>(this) || !Align) &&
65 "GlobalAlias should not have an alignment!");
66 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
67 assert(Align <= MaximumAlignment &&
68 "Alignment is greater than MaximumAlignment!");
69 Alignment = Log2_32(Align) + 1;
70 assert(getAlignment() == Align && "Alignment representation error!");
73 bool GlobalValue::isDeclaration() const {
74 // Globals are definitions if they have an initializer.
75 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
76 return GV->getNumOperands() == 0;
78 // Functions are definitions if they have a body.
79 if (const Function *F = dyn_cast<Function>(this))
82 // Aliases are always definitions.
83 assert(isa<GlobalAlias>(this));
87 //===----------------------------------------------------------------------===//
88 // GlobalVariable Implementation
89 //===----------------------------------------------------------------------===//
91 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
93 const Twine &Name, ThreadLocalMode TLMode,
94 unsigned AddressSpace,
95 bool isExternallyInitialized)
96 : GlobalValue(PointerType::get(Ty, AddressSpace),
97 Value::GlobalVariableVal,
98 OperandTraits<GlobalVariable>::op_begin(this),
99 InitVal != nullptr, Link, Name),
100 isConstantGlobal(constant), threadLocalMode(TLMode),
101 isExternallyInitializedConstant(isExternallyInitialized) {
103 assert(InitVal->getType() == Ty &&
104 "Initializer should be the same type as the GlobalVariable!");
108 LeakDetector::addGarbageObject(this);
111 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
112 LinkageTypes Link, Constant *InitVal,
114 GlobalVariable *Before, 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 Before->getParent()->getGlobalList().insert(Before, this);
134 M.getGlobalList().push_back(this);
137 void GlobalVariable::setParent(Module *parent) {
139 LeakDetector::addGarbageObject(this);
142 LeakDetector::removeGarbageObject(this);
145 void GlobalVariable::removeFromParent() {
146 getParent()->getGlobalList().remove(this);
149 void GlobalVariable::eraseFromParent() {
150 getParent()->getGlobalList().erase(this);
153 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
155 // If you call this, then you better know this GVar has a constant
156 // initializer worth replacing. Enforce that here.
157 assert(getNumOperands() == 1 &&
158 "Attempt to replace uses of Constants on a GVar with no initializer");
160 // And, since you know it has an initializer, the From value better be
161 // the initializer :)
162 assert(getOperand(0) == From &&
163 "Attempt to replace wrong constant initializer in GVar");
165 // And, you better have a constant for the replacement value
166 assert(isa<Constant>(To) &&
167 "Attempt to replace GVar initializer with non-constant");
169 // Okay, preconditions out of the way, replace the constant initializer.
170 this->setOperand(0, cast<Constant>(To));
173 void GlobalVariable::setInitializer(Constant *InitVal) {
175 if (hasInitializer()) {
176 Op<0>().set(nullptr);
180 assert(InitVal->getType() == getType()->getElementType() &&
181 "Initializer type must match GlobalVariable type");
182 if (!hasInitializer())
184 Op<0>().set(InitVal);
188 /// copyAttributesFrom - copy all additional attributes (those not needed to
189 /// create a GlobalVariable) from the GlobalVariable Src to this one.
190 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
191 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
192 GlobalValue::copyAttributesFrom(Src);
193 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
194 setThreadLocalMode(SrcVar->getThreadLocalMode());
198 //===----------------------------------------------------------------------===//
199 // GlobalAlias Implementation
200 //===----------------------------------------------------------------------===//
202 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
203 const Twine &Name, Constant* aliasee,
204 Module *ParentModule)
205 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
206 LeakDetector::addGarbageObject(this);
209 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
213 ParentModule->getAliasList().push_back(this);
216 void GlobalAlias::setParent(Module *parent) {
218 LeakDetector::addGarbageObject(this);
221 LeakDetector::removeGarbageObject(this);
224 void GlobalAlias::removeFromParent() {
225 getParent()->getAliasList().remove(this);
228 void GlobalAlias::eraseFromParent() {
229 getParent()->getAliasList().erase(this);
232 void GlobalAlias::setAliasee(Constant *Aliasee) {
233 assert((!Aliasee || Aliasee->getType() == getType()) &&
234 "Alias and aliasee types should match!");
236 setOperand(0, Aliasee);
239 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
240 Constant *C = GA->getAliasee();
241 assert(C && "Must alias something");
243 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
246 ConstantExpr *CE = cast<ConstantExpr>(C);
247 assert((CE->getOpcode() == Instruction::BitCast ||
248 CE->getOpcode() == Instruction::AddrSpaceCast ||
249 CE->getOpcode() == Instruction::GetElementPtr) &&
250 "Unsupported aliasee");
252 return cast<GlobalValue>(CE->getOperand(0));
255 GlobalValue *GlobalAlias::getAliasedGlobal() {
256 SmallPtrSet<GlobalValue*, 3> Visited;
258 GlobalAlias *GA = this;
261 GlobalValue *GV = getAliaseeGV(GA);
262 if (!Visited.insert(GV))
265 // Iterate over aliasing chain.
266 GA = dyn_cast<GlobalAlias>(GV);