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 bool GlobalValue::isDeclaration() const {
77 // Globals are definitions if they have an initializer.
78 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
79 return GV->getNumOperands() == 0;
81 // Functions are definitions if they have a body.
82 if (const Function *F = dyn_cast<Function>(this))
85 // Aliases are always definitions.
86 assert(isa<GlobalAlias>(this));
90 //===----------------------------------------------------------------------===//
91 // GlobalVariable Implementation
92 //===----------------------------------------------------------------------===//
94 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
96 const Twine &Name, ThreadLocalMode TLMode,
97 unsigned AddressSpace,
98 bool isExternallyInitialized)
99 : GlobalValue(PointerType::get(Ty, AddressSpace),
100 Value::GlobalVariableVal,
101 OperandTraits<GlobalVariable>::op_begin(this),
102 InitVal != nullptr, Link, Name),
103 isConstantGlobal(constant), threadLocalMode(TLMode),
104 isExternallyInitializedConstant(isExternallyInitialized) {
106 assert(InitVal->getType() == Ty &&
107 "Initializer should be the same type as the GlobalVariable!");
111 LeakDetector::addGarbageObject(this);
114 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
115 LinkageTypes Link, Constant *InitVal,
117 GlobalVariable *Before, ThreadLocalMode TLMode,
118 unsigned AddressSpace,
119 bool isExternallyInitialized)
120 : GlobalValue(PointerType::get(Ty, AddressSpace),
121 Value::GlobalVariableVal,
122 OperandTraits<GlobalVariable>::op_begin(this),
123 InitVal != nullptr, Link, Name),
124 isConstantGlobal(constant), threadLocalMode(TLMode),
125 isExternallyInitializedConstant(isExternallyInitialized) {
127 assert(InitVal->getType() == Ty &&
128 "Initializer should be the same type as the GlobalVariable!");
132 LeakDetector::addGarbageObject(this);
135 Before->getParent()->getGlobalList().insert(Before, this);
137 M.getGlobalList().push_back(this);
140 void GlobalVariable::setParent(Module *parent) {
142 LeakDetector::addGarbageObject(this);
145 LeakDetector::removeGarbageObject(this);
148 void GlobalVariable::removeFromParent() {
149 getParent()->getGlobalList().remove(this);
152 void GlobalVariable::eraseFromParent() {
153 getParent()->getGlobalList().erase(this);
156 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
158 // If you call this, then you better know this GVar has a constant
159 // initializer worth replacing. Enforce that here.
160 assert(getNumOperands() == 1 &&
161 "Attempt to replace uses of Constants on a GVar with no initializer");
163 // And, since you know it has an initializer, the From value better be
164 // the initializer :)
165 assert(getOperand(0) == From &&
166 "Attempt to replace wrong constant initializer in GVar");
168 // And, you better have a constant for the replacement value
169 assert(isa<Constant>(To) &&
170 "Attempt to replace GVar initializer with non-constant");
172 // Okay, preconditions out of the way, replace the constant initializer.
173 this->setOperand(0, cast<Constant>(To));
176 void GlobalVariable::setInitializer(Constant *InitVal) {
178 if (hasInitializer()) {
179 Op<0>().set(nullptr);
183 assert(InitVal->getType() == getType()->getElementType() &&
184 "Initializer type must match GlobalVariable type");
185 if (!hasInitializer())
187 Op<0>().set(InitVal);
191 /// copyAttributesFrom - copy all additional attributes (those not needed to
192 /// create a GlobalVariable) from the GlobalVariable Src to this one.
193 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
194 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
195 GlobalValue::copyAttributesFrom(Src);
196 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
197 setThreadLocalMode(SrcVar->getThreadLocalMode());
201 //===----------------------------------------------------------------------===//
202 // GlobalAlias Implementation
203 //===----------------------------------------------------------------------===//
205 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
206 const Twine &Name, Constant* aliasee,
207 Module *ParentModule)
208 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
209 LeakDetector::addGarbageObject(this);
212 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
216 ParentModule->getAliasList().push_back(this);
219 void GlobalAlias::setParent(Module *parent) {
221 LeakDetector::addGarbageObject(this);
224 LeakDetector::removeGarbageObject(this);
227 void GlobalAlias::removeFromParent() {
228 getParent()->getAliasList().remove(this);
231 void GlobalAlias::eraseFromParent() {
232 getParent()->getAliasList().erase(this);
235 void GlobalAlias::setAliasee(Constant *Aliasee) {
236 assert((!Aliasee || Aliasee->getType() == getType()) &&
237 "Alias and aliasee types should match!");
239 setOperand(0, Aliasee);
242 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
243 Constant *C = GA->getAliasee();
244 assert(C && "Must alias something");
246 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
249 ConstantExpr *CE = cast<ConstantExpr>(C);
250 assert((CE->getOpcode() == Instruction::BitCast ||
251 CE->getOpcode() == Instruction::AddrSpaceCast ||
252 CE->getOpcode() == Instruction::GetElementPtr) &&
253 "Unsupported aliasee");
255 return cast<GlobalValue>(CE->getOperand(0));
258 GlobalValue *GlobalAlias::getAliasedGlobal() {
259 SmallPtrSet<GlobalValue*, 3> Visited;
261 GlobalAlias *GA = this;
264 GlobalValue *GV = getAliaseeGV(GA);
265 if (!Visited.insert(GV))
268 // Iterate over aliasing chain.
269 GA = dyn_cast<GlobalAlias>(GV);