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 VMCore
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/LeakDetector.h"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 bool GlobalValue::isMaterializable() const {
30 return getParent() && getParent()->isMaterializable(this);
32 bool GlobalValue::isDematerializable() const {
33 return getParent() && getParent()->isDematerializable(this);
35 bool GlobalValue::Materialize(std::string *ErrInfo) {
36 return getParent()->Materialize(this, ErrInfo);
38 void GlobalValue::Dematerialize() {
39 getParent()->Dematerialize(this);
42 /// Override destroyConstant to make sure it doesn't get called on
43 /// GlobalValue's because they shouldn't be treated like other constants.
44 void GlobalValue::destroyConstant() {
45 llvm_unreachable("You can't GV->destroyConstant()!");
48 /// copyAttributesFrom - copy all additional attributes (those not needed to
49 /// create a GlobalValue) from the GlobalValue Src to this one.
50 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51 setAlignment(Src->getAlignment());
52 setSection(Src->getSection());
53 setVisibility(Src->getVisibility());
54 setUnnamedAddr(Src->hasUnnamedAddr());
57 void GlobalValue::setAlignment(unsigned Align) {
58 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
59 assert(Align <= MaximumAlignment &&
60 "Alignment is greater than MaximumAlignment!");
61 Alignment = Log2_32(Align) + 1;
62 assert(getAlignment() == Align && "Alignment representation error!");
65 bool GlobalValue::isDeclaration() const {
66 // Globals are definitions if they have an initializer.
67 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
68 return GV->getNumOperands() == 0;
70 // Functions are definitions if they have a body.
71 if (const Function *F = dyn_cast<Function>(this))
74 // Aliases are always definitions.
75 assert(isa<GlobalAlias>(this));
79 //===----------------------------------------------------------------------===//
80 // GlobalVariable Implementation
81 //===----------------------------------------------------------------------===//
83 // TODO: Remove once clang is updated.
84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85 Constant *InitVal, const Twine &Name,
86 bool ThreadLocal, unsigned AddressSpace)
87 : GlobalValue(PointerType::get(Ty, AddressSpace),
88 Value::GlobalVariableVal,
89 OperandTraits<GlobalVariable>::op_begin(this),
90 InitVal != 0, Link, Name),
91 isConstantGlobal(constant),
92 threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
94 assert(InitVal->getType() == Ty &&
95 "Initializer should be the same type as the GlobalVariable!");
99 LeakDetector::addGarbageObject(this);
102 // TODO: Remove once clang is updated.
103 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
104 LinkageTypes Link, Constant *InitVal,
106 GlobalVariable *Before, bool ThreadLocal,
107 unsigned AddressSpace)
108 : GlobalValue(PointerType::get(Ty, AddressSpace),
109 Value::GlobalVariableVal,
110 OperandTraits<GlobalVariable>::op_begin(this),
111 InitVal != 0, Link, Name),
112 isConstantGlobal(constant),
113 threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
115 assert(InitVal->getType() == Ty &&
116 "Initializer should be the same type as the GlobalVariable!");
120 LeakDetector::addGarbageObject(this);
123 Before->getParent()->getGlobalList().insert(Before, this);
125 M.getGlobalList().push_back(this);
128 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
129 Constant *InitVal, const Twine &Name,
130 ThreadLocalMode TLMode, unsigned AddressSpace)
131 : GlobalValue(PointerType::get(Ty, AddressSpace),
132 Value::GlobalVariableVal,
133 OperandTraits<GlobalVariable>::op_begin(this),
134 InitVal != 0, Link, Name),
135 isConstantGlobal(constant), threadLocalMode(TLMode) {
137 assert(InitVal->getType() == Ty &&
138 "Initializer should be the same type as the GlobalVariable!");
142 LeakDetector::addGarbageObject(this);
145 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
146 LinkageTypes Link, Constant *InitVal,
148 GlobalVariable *Before, ThreadLocalMode TLMode,
149 unsigned AddressSpace)
150 : GlobalValue(PointerType::get(Ty, AddressSpace),
151 Value::GlobalVariableVal,
152 OperandTraits<GlobalVariable>::op_begin(this),
153 InitVal != 0, Link, Name),
154 isConstantGlobal(constant), threadLocalMode(TLMode) {
156 assert(InitVal->getType() == Ty &&
157 "Initializer should be the same type as the GlobalVariable!");
161 LeakDetector::addGarbageObject(this);
164 Before->getParent()->getGlobalList().insert(Before, this);
166 M.getGlobalList().push_back(this);
169 void GlobalVariable::setParent(Module *parent) {
171 LeakDetector::addGarbageObject(this);
174 LeakDetector::removeGarbageObject(this);
177 void GlobalVariable::removeFromParent() {
178 getParent()->getGlobalList().remove(this);
181 void GlobalVariable::eraseFromParent() {
182 getParent()->getGlobalList().erase(this);
185 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
187 // If you call this, then you better know this GVar has a constant
188 // initializer worth replacing. Enforce that here.
189 assert(getNumOperands() == 1 &&
190 "Attempt to replace uses of Constants on a GVar with no initializer");
192 // And, since you know it has an initializer, the From value better be
193 // the initializer :)
194 assert(getOperand(0) == From &&
195 "Attempt to replace wrong constant initializer in GVar");
197 // And, you better have a constant for the replacement value
198 assert(isa<Constant>(To) &&
199 "Attempt to replace GVar initializer with non-constant");
201 // Okay, preconditions out of the way, replace the constant initializer.
202 this->setOperand(0, cast<Constant>(To));
205 void GlobalVariable::setInitializer(Constant *InitVal) {
207 if (hasInitializer()) {
212 assert(InitVal->getType() == getType()->getElementType() &&
213 "Initializer type must match GlobalVariable type");
214 if (!hasInitializer())
216 Op<0>().set(InitVal);
220 /// copyAttributesFrom - copy all additional attributes (those not needed to
221 /// create a GlobalVariable) from the GlobalVariable Src to this one.
222 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
223 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
224 GlobalValue::copyAttributesFrom(Src);
225 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
226 setThreadLocal(SrcVar->isThreadLocal());
230 //===----------------------------------------------------------------------===//
231 // GlobalAlias Implementation
232 //===----------------------------------------------------------------------===//
234 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
235 const Twine &Name, Constant* aliasee,
236 Module *ParentModule)
237 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
238 LeakDetector::addGarbageObject(this);
241 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
245 ParentModule->getAliasList().push_back(this);
248 void GlobalAlias::setParent(Module *parent) {
250 LeakDetector::addGarbageObject(this);
253 LeakDetector::removeGarbageObject(this);
256 void GlobalAlias::removeFromParent() {
257 getParent()->getAliasList().remove(this);
260 void GlobalAlias::eraseFromParent() {
261 getParent()->getAliasList().erase(this);
264 void GlobalAlias::setAliasee(Constant *Aliasee) {
265 assert((!Aliasee || Aliasee->getType() == getType()) &&
266 "Alias and aliasee types should match!");
268 setOperand(0, Aliasee);
271 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
272 const Constant *C = getAliasee();
273 if (C == 0) return 0;
275 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
278 const ConstantExpr *CE = cast<ConstantExpr>(C);
279 assert((CE->getOpcode() == Instruction::BitCast ||
280 CE->getOpcode() == Instruction::GetElementPtr) &&
281 "Unsupported aliasee");
283 return cast<GlobalValue>(CE->getOperand(0));
286 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
287 SmallPtrSet<const GlobalValue*, 3> Visited;
289 // Check if we need to stop early.
290 if (stopOnWeak && mayBeOverridden())
293 const GlobalValue *GV = getAliasedGlobal();
296 // Iterate over aliasing chain, stopping on weak alias if necessary.
297 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
298 if (stopOnWeak && GA->mayBeOverridden())
301 GV = GA->getAliasedGlobal();
303 if (!Visited.insert(GV))