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 void GlobalValue::setAlignment(unsigned Align) {
67 assert((!isa<GlobalAlias>(this)) &&
68 "GlobalAlias should not have an alignment!");
69 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
70 assert(Align <= MaximumAlignment &&
71 "Alignment is greater than MaximumAlignment!");
72 Alignment = Log2_32(Align) + 1;
73 assert(getAlignment() == Align && "Alignment representation error!");
76 void GlobalValue::setSection(StringRef S) {
77 assert(!isa<GlobalAlias>(this) && "GlobalAlias should not have a section!");
81 bool GlobalValue::isDeclaration() const {
82 // Globals are definitions if they have an initializer.
83 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
84 return GV->getNumOperands() == 0;
86 // Functions are definitions if they have a body.
87 if (const Function *F = dyn_cast<Function>(this))
90 // Aliases are always definitions.
91 assert(isa<GlobalAlias>(this));
95 //===----------------------------------------------------------------------===//
96 // GlobalVariable Implementation
97 //===----------------------------------------------------------------------===//
99 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
101 const Twine &Name, ThreadLocalMode TLMode,
102 unsigned AddressSpace,
103 bool isExternallyInitialized)
104 : GlobalValue(PointerType::get(Ty, AddressSpace),
105 Value::GlobalVariableVal,
106 OperandTraits<GlobalVariable>::op_begin(this),
107 InitVal != nullptr, Link, Name),
108 isConstantGlobal(constant), threadLocalMode(TLMode),
109 isExternallyInitializedConstant(isExternallyInitialized) {
111 assert(InitVal->getType() == Ty &&
112 "Initializer should be the same type as the GlobalVariable!");
116 LeakDetector::addGarbageObject(this);
119 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
120 LinkageTypes Link, Constant *InitVal,
122 GlobalVariable *Before, ThreadLocalMode TLMode,
123 unsigned AddressSpace,
124 bool isExternallyInitialized)
125 : GlobalValue(PointerType::get(Ty, AddressSpace),
126 Value::GlobalVariableVal,
127 OperandTraits<GlobalVariable>::op_begin(this),
128 InitVal != nullptr, Link, Name),
129 isConstantGlobal(constant), threadLocalMode(TLMode),
130 isExternallyInitializedConstant(isExternallyInitialized) {
132 assert(InitVal->getType() == Ty &&
133 "Initializer should be the same type as the GlobalVariable!");
137 LeakDetector::addGarbageObject(this);
140 Before->getParent()->getGlobalList().insert(Before, this);
142 M.getGlobalList().push_back(this);
145 void GlobalVariable::setParent(Module *parent) {
147 LeakDetector::addGarbageObject(this);
150 LeakDetector::removeGarbageObject(this);
153 void GlobalVariable::removeFromParent() {
154 getParent()->getGlobalList().remove(this);
157 void GlobalVariable::eraseFromParent() {
158 getParent()->getGlobalList().erase(this);
161 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
163 // If you call this, then you better know this GVar has a constant
164 // initializer worth replacing. Enforce that here.
165 assert(getNumOperands() == 1 &&
166 "Attempt to replace uses of Constants on a GVar with no initializer");
168 // And, since you know it has an initializer, the From value better be
169 // the initializer :)
170 assert(getOperand(0) == From &&
171 "Attempt to replace wrong constant initializer in GVar");
173 // And, you better have a constant for the replacement value
174 assert(isa<Constant>(To) &&
175 "Attempt to replace GVar initializer with non-constant");
177 // Okay, preconditions out of the way, replace the constant initializer.
178 this->setOperand(0, cast<Constant>(To));
181 void GlobalVariable::setInitializer(Constant *InitVal) {
183 if (hasInitializer()) {
184 Op<0>().set(nullptr);
188 assert(InitVal->getType() == getType()->getElementType() &&
189 "Initializer type must match GlobalVariable type");
190 if (!hasInitializer())
192 Op<0>().set(InitVal);
196 /// copyAttributesFrom - copy all additional attributes (those not needed to
197 /// create a GlobalVariable) from the GlobalVariable Src to this one.
198 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
199 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
200 GlobalValue::copyAttributesFrom(Src);
201 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
202 setThreadLocalMode(SrcVar->getThreadLocalMode());
206 //===----------------------------------------------------------------------===//
207 // GlobalAlias Implementation
208 //===----------------------------------------------------------------------===//
210 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
211 const Twine &Name, Constant* aliasee,
212 Module *ParentModule)
213 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
214 LeakDetector::addGarbageObject(this);
217 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
221 ParentModule->getAliasList().push_back(this);
224 void GlobalAlias::setParent(Module *parent) {
226 LeakDetector::addGarbageObject(this);
229 LeakDetector::removeGarbageObject(this);
232 void GlobalAlias::removeFromParent() {
233 getParent()->getAliasList().remove(this);
236 void GlobalAlias::eraseFromParent() {
237 getParent()->getAliasList().erase(this);
240 void GlobalAlias::setAliasee(Constant *Aliasee) {
241 assert((!Aliasee || Aliasee->getType() == getType()) &&
242 "Alias and aliasee types should match!");
244 setOperand(0, Aliasee);
247 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
248 Constant *C = GA->getAliasee();
249 assert(C && "Must alias something");
251 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
254 ConstantExpr *CE = cast<ConstantExpr>(C);
255 assert((CE->getOpcode() == Instruction::BitCast ||
256 CE->getOpcode() == Instruction::AddrSpaceCast ||
257 CE->getOpcode() == Instruction::GetElementPtr) &&
258 "Unsupported aliasee");
260 return cast<GlobalValue>(CE->getOperand(0));
263 GlobalValue *GlobalAlias::getAliasedGlobal() {
264 SmallPtrSet<GlobalValue*, 3> Visited;
266 GlobalAlias *GA = this;
269 GlobalValue *GV = getAliaseeGV(GA);
270 if (!Visited.insert(GV))
273 // Iterate over aliasing chain.
274 GA = dyn_cast<GlobalAlias>(GV);