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/Module.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LeakDetector.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 /// Override destroyConstant to make sure it doesn't get called on
44 /// GlobalValue's because they shouldn't be treated like other constants.
45 void GlobalValue::destroyConstant() {
46 llvm_unreachable("You can't GV->destroyConstant()!");
49 /// copyAttributesFrom - copy all additional attributes (those not needed to
50 /// create a GlobalValue) from the GlobalValue Src to this one.
51 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setAlignment(Src->getAlignment());
53 setSection(Src->getSection());
54 setVisibility(Src->getVisibility());
55 setUnnamedAddr(Src->hasUnnamedAddr());
56 setDLLStorageClass(Src->getDLLStorageClass());
59 void GlobalValue::setAlignment(unsigned Align) {
60 assert((!isa<GlobalAlias>(this) || !Align) &&
61 "GlobalAlias should not have an alignment!");
62 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
63 assert(Align <= MaximumAlignment &&
64 "Alignment is greater than MaximumAlignment!");
65 Alignment = Log2_32(Align) + 1;
66 assert(getAlignment() == Align && "Alignment representation error!");
69 bool GlobalValue::isDeclaration() const {
70 // Globals are definitions if they have an initializer.
71 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
72 return GV->getNumOperands() == 0;
74 // Functions are definitions if they have a body.
75 if (const Function *F = dyn_cast<Function>(this))
78 // Aliases are always definitions.
79 assert(isa<GlobalAlias>(this));
83 //===----------------------------------------------------------------------===//
84 // GlobalVariable Implementation
85 //===----------------------------------------------------------------------===//
87 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
89 const Twine &Name, ThreadLocalMode TLMode,
90 unsigned AddressSpace,
91 bool isExternallyInitialized)
92 : GlobalValue(PointerType::get(Ty, AddressSpace),
93 Value::GlobalVariableVal,
94 OperandTraits<GlobalVariable>::op_begin(this),
95 InitVal != 0, Link, Name),
96 isConstantGlobal(constant), threadLocalMode(TLMode),
97 isExternallyInitializedConstant(isExternallyInitialized) {
99 assert(InitVal->getType() == Ty &&
100 "Initializer should be the same type as the GlobalVariable!");
104 LeakDetector::addGarbageObject(this);
107 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
108 LinkageTypes Link, Constant *InitVal,
110 GlobalVariable *Before, ThreadLocalMode TLMode,
111 unsigned AddressSpace,
112 bool isExternallyInitialized)
113 : GlobalValue(PointerType::get(Ty, AddressSpace),
114 Value::GlobalVariableVal,
115 OperandTraits<GlobalVariable>::op_begin(this),
116 InitVal != 0, Link, Name),
117 isConstantGlobal(constant), threadLocalMode(TLMode),
118 isExternallyInitializedConstant(isExternallyInitialized) {
120 assert(InitVal->getType() == Ty &&
121 "Initializer should be the same type as the GlobalVariable!");
125 LeakDetector::addGarbageObject(this);
128 Before->getParent()->getGlobalList().insert(Before, this);
130 M.getGlobalList().push_back(this);
133 void GlobalVariable::setParent(Module *parent) {
135 LeakDetector::addGarbageObject(this);
138 LeakDetector::removeGarbageObject(this);
141 void GlobalVariable::removeFromParent() {
142 getParent()->getGlobalList().remove(this);
145 void GlobalVariable::eraseFromParent() {
146 getParent()->getGlobalList().erase(this);
149 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
151 // If you call this, then you better know this GVar has a constant
152 // initializer worth replacing. Enforce that here.
153 assert(getNumOperands() == 1 &&
154 "Attempt to replace uses of Constants on a GVar with no initializer");
156 // And, since you know it has an initializer, the From value better be
157 // the initializer :)
158 assert(getOperand(0) == From &&
159 "Attempt to replace wrong constant initializer in GVar");
161 // And, you better have a constant for the replacement value
162 assert(isa<Constant>(To) &&
163 "Attempt to replace GVar initializer with non-constant");
165 // Okay, preconditions out of the way, replace the constant initializer.
166 this->setOperand(0, cast<Constant>(To));
169 void GlobalVariable::setInitializer(Constant *InitVal) {
171 if (hasInitializer()) {
176 assert(InitVal->getType() == getType()->getElementType() &&
177 "Initializer type must match GlobalVariable type");
178 if (!hasInitializer())
180 Op<0>().set(InitVal);
184 /// copyAttributesFrom - copy all additional attributes (those not needed to
185 /// create a GlobalVariable) from the GlobalVariable Src to this one.
186 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
187 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
188 GlobalValue::copyAttributesFrom(Src);
189 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
190 setThreadLocalMode(SrcVar->getThreadLocalMode());
194 //===----------------------------------------------------------------------===//
195 // GlobalAlias Implementation
196 //===----------------------------------------------------------------------===//
198 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
199 const Twine &Name, Constant* aliasee,
200 Module *ParentModule)
201 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
202 LeakDetector::addGarbageObject(this);
205 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
209 ParentModule->getAliasList().push_back(this);
212 void GlobalAlias::setParent(Module *parent) {
214 LeakDetector::addGarbageObject(this);
217 LeakDetector::removeGarbageObject(this);
220 void GlobalAlias::removeFromParent() {
221 getParent()->getAliasList().remove(this);
224 void GlobalAlias::eraseFromParent() {
225 getParent()->getAliasList().erase(this);
228 void GlobalAlias::setAliasee(Constant *Aliasee) {
229 assert((!Aliasee || Aliasee->getType() == getType()) &&
230 "Alias and aliasee types should match!");
232 setOperand(0, Aliasee);
235 GlobalValue *GlobalAlias::getAliasedGlobal() {
236 Constant *C = getAliasee();
237 if (C == 0) return 0;
239 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
242 ConstantExpr *CE = cast<ConstantExpr>(C);
243 assert((CE->getOpcode() == Instruction::BitCast ||
244 CE->getOpcode() == Instruction::AddrSpaceCast ||
245 CE->getOpcode() == Instruction::GetElementPtr) &&
246 "Unsupported aliasee");
248 return cast<GlobalValue>(CE->getOperand(0));
251 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
252 SmallPtrSet<GlobalValue*, 3> Visited;
254 // Check if we need to stop early.
255 if (stopOnWeak && mayBeOverridden())
258 GlobalValue *GV = getAliasedGlobal();
261 // Iterate over aliasing chain, stopping on weak alias if necessary.
262 while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
263 if (stopOnWeak && GA->mayBeOverridden())
266 GV = GA->getAliasedGlobal();
268 if (!Visited.insert(GV))