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 void GlobalValue::setSection(StringRef S) {
84 assert(!isa<GlobalAlias>(this) && "GlobalAlias should not have a section!");
88 bool GlobalValue::isDeclaration() const {
89 // Globals are definitions if they have an initializer.
90 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
91 return GV->getNumOperands() == 0;
93 // Functions are definitions if they have a body.
94 if (const Function *F = dyn_cast<Function>(this))
97 // Aliases are always definitions.
98 assert(isa<GlobalAlias>(this));
102 //===----------------------------------------------------------------------===//
103 // GlobalVariable Implementation
104 //===----------------------------------------------------------------------===//
106 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
108 const Twine &Name, ThreadLocalMode TLMode,
109 unsigned AddressSpace,
110 bool isExternallyInitialized)
111 : GlobalValue(PointerType::get(Ty, AddressSpace),
112 Value::GlobalVariableVal,
113 OperandTraits<GlobalVariable>::op_begin(this),
114 InitVal != nullptr, Link, Name),
115 isConstantGlobal(constant), threadLocalMode(TLMode),
116 isExternallyInitializedConstant(isExternallyInitialized) {
118 assert(InitVal->getType() == Ty &&
119 "Initializer should be the same type as the GlobalVariable!");
123 LeakDetector::addGarbageObject(this);
126 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
127 LinkageTypes Link, Constant *InitVal,
129 GlobalVariable *Before, ThreadLocalMode TLMode,
130 unsigned AddressSpace,
131 bool isExternallyInitialized)
132 : GlobalValue(PointerType::get(Ty, AddressSpace),
133 Value::GlobalVariableVal,
134 OperandTraits<GlobalVariable>::op_begin(this),
135 InitVal != nullptr, Link, Name),
136 isConstantGlobal(constant), threadLocalMode(TLMode),
137 isExternallyInitializedConstant(isExternallyInitialized) {
139 assert(InitVal->getType() == Ty &&
140 "Initializer should be the same type as the GlobalVariable!");
144 LeakDetector::addGarbageObject(this);
147 Before->getParent()->getGlobalList().insert(Before, this);
149 M.getGlobalList().push_back(this);
152 void GlobalVariable::setParent(Module *parent) {
154 LeakDetector::addGarbageObject(this);
157 LeakDetector::removeGarbageObject(this);
160 void GlobalVariable::removeFromParent() {
161 getParent()->getGlobalList().remove(this);
164 void GlobalVariable::eraseFromParent() {
165 getParent()->getGlobalList().erase(this);
168 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
170 // If you call this, then you better know this GVar has a constant
171 // initializer worth replacing. Enforce that here.
172 assert(getNumOperands() == 1 &&
173 "Attempt to replace uses of Constants on a GVar with no initializer");
175 // And, since you know it has an initializer, the From value better be
176 // the initializer :)
177 assert(getOperand(0) == From &&
178 "Attempt to replace wrong constant initializer in GVar");
180 // And, you better have a constant for the replacement value
181 assert(isa<Constant>(To) &&
182 "Attempt to replace GVar initializer with non-constant");
184 // Okay, preconditions out of the way, replace the constant initializer.
185 this->setOperand(0, cast<Constant>(To));
188 void GlobalVariable::setInitializer(Constant *InitVal) {
190 if (hasInitializer()) {
191 Op<0>().set(nullptr);
195 assert(InitVal->getType() == getType()->getElementType() &&
196 "Initializer type must match GlobalVariable type");
197 if (!hasInitializer())
199 Op<0>().set(InitVal);
203 /// copyAttributesFrom - copy all additional attributes (those not needed to
204 /// create a GlobalVariable) from the GlobalVariable Src to this one.
205 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
206 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
207 GlobalValue::copyAttributesFrom(Src);
208 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
209 setThreadLocalMode(SrcVar->getThreadLocalMode());
213 //===----------------------------------------------------------------------===//
214 // GlobalAlias Implementation
215 //===----------------------------------------------------------------------===//
217 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
218 const Twine &Name, Constant* aliasee,
219 Module *ParentModule)
220 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
221 LeakDetector::addGarbageObject(this);
224 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
228 ParentModule->getAliasList().push_back(this);
231 void GlobalAlias::setParent(Module *parent) {
233 LeakDetector::addGarbageObject(this);
236 LeakDetector::removeGarbageObject(this);
239 void GlobalAlias::removeFromParent() {
240 getParent()->getAliasList().remove(this);
243 void GlobalAlias::eraseFromParent() {
244 getParent()->getAliasList().erase(this);
247 void GlobalAlias::setAliasee(Constant *Aliasee) {
248 assert((!Aliasee || Aliasee->getType() == getType()) &&
249 "Alias and aliasee types should match!");
251 setOperand(0, Aliasee);
254 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
255 Constant *C = GA->getAliasee();
256 assert(C && "Must alias something");
258 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
261 ConstantExpr *CE = cast<ConstantExpr>(C);
262 assert((CE->getOpcode() == Instruction::BitCast ||
263 CE->getOpcode() == Instruction::AddrSpaceCast ||
264 CE->getOpcode() == Instruction::GetElementPtr) &&
265 "Unsupported aliasee");
267 return cast<GlobalValue>(CE->getOperand(0));
270 GlobalValue *GlobalAlias::getAliasedGlobal() {
271 SmallPtrSet<GlobalValue*, 3> Visited;
273 GlobalAlias *GA = this;
276 GlobalValue *GV = getAliaseeGV(GA);
277 if (!Visited.insert(GV))
280 // Iterate over aliasing chain.
281 GA = dyn_cast<GlobalAlias>(GV);