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 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
67 return GV->getNumOperands() == 0;
69 if (const Function *F = dyn_cast<Function>(this))
72 const GlobalAlias *GA = cast<GlobalAlias>(this);
73 if (const GlobalValue *AV = GA->getAliasedGlobal())
74 return AV->isDeclaration();
78 //===----------------------------------------------------------------------===//
79 // GlobalVariable Implementation
80 //===----------------------------------------------------------------------===//
82 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
83 Constant *InitVal, const Twine &Name,
84 bool ThreadLocal, unsigned AddressSpace)
85 : GlobalValue(PointerType::get(Ty, AddressSpace),
86 Value::GlobalVariableVal,
87 OperandTraits<GlobalVariable>::op_begin(this),
88 InitVal != 0, Link, Name),
89 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
91 assert(InitVal->getType() == Ty &&
92 "Initializer should be the same type as the GlobalVariable!");
96 LeakDetector::addGarbageObject(this);
99 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
100 LinkageTypes Link, Constant *InitVal,
102 GlobalVariable *Before, bool ThreadLocal,
103 unsigned AddressSpace)
104 : GlobalValue(PointerType::get(Ty, AddressSpace),
105 Value::GlobalVariableVal,
106 OperandTraits<GlobalVariable>::op_begin(this),
107 InitVal != 0, Link, Name),
108 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
110 assert(InitVal->getType() == Ty &&
111 "Initializer should be the same type as the GlobalVariable!");
115 LeakDetector::addGarbageObject(this);
118 Before->getParent()->getGlobalList().insert(Before, this);
120 M.getGlobalList().push_back(this);
123 void GlobalVariable::setParent(Module *parent) {
125 LeakDetector::addGarbageObject(this);
128 LeakDetector::removeGarbageObject(this);
131 void GlobalVariable::removeFromParent() {
132 getParent()->getGlobalList().remove(this);
135 void GlobalVariable::eraseFromParent() {
136 getParent()->getGlobalList().erase(this);
139 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
141 // If you call this, then you better know this GVar has a constant
142 // initializer worth replacing. Enforce that here.
143 assert(getNumOperands() == 1 &&
144 "Attempt to replace uses of Constants on a GVar with no initializer");
146 // And, since you know it has an initializer, the From value better be
147 // the initializer :)
148 assert(getOperand(0) == From &&
149 "Attempt to replace wrong constant initializer in GVar");
151 // And, you better have a constant for the replacement value
152 assert(isa<Constant>(To) &&
153 "Attempt to replace GVar initializer with non-constant");
155 // Okay, preconditions out of the way, replace the constant initializer.
156 this->setOperand(0, cast<Constant>(To));
159 void GlobalVariable::setInitializer(Constant *InitVal) {
161 if (hasInitializer()) {
166 assert(InitVal->getType() == getType()->getElementType() &&
167 "Initializer type must match GlobalVariable type");
168 if (!hasInitializer())
170 Op<0>().set(InitVal);
174 /// copyAttributesFrom - copy all additional attributes (those not needed to
175 /// create a GlobalVariable) from the GlobalVariable Src to this one.
176 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
177 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
178 GlobalValue::copyAttributesFrom(Src);
179 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
180 setThreadLocal(SrcVar->isThreadLocal());
184 //===----------------------------------------------------------------------===//
185 // GlobalAlias Implementation
186 //===----------------------------------------------------------------------===//
188 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
189 const Twine &Name, Constant* aliasee,
190 Module *ParentModule)
191 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
192 LeakDetector::addGarbageObject(this);
195 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
199 ParentModule->getAliasList().push_back(this);
202 void GlobalAlias::setParent(Module *parent) {
204 LeakDetector::addGarbageObject(this);
207 LeakDetector::removeGarbageObject(this);
210 void GlobalAlias::removeFromParent() {
211 getParent()->getAliasList().remove(this);
214 void GlobalAlias::eraseFromParent() {
215 getParent()->getAliasList().erase(this);
218 void GlobalAlias::setAliasee(Constant *Aliasee) {
219 assert((!Aliasee || Aliasee->getType() == getType()) &&
220 "Alias and aliasee types should match!");
222 setOperand(0, Aliasee);
225 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
226 const Constant *C = getAliasee();
227 if (C == 0) return 0;
229 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
232 const ConstantExpr *CE = cast<ConstantExpr>(C);
233 assert((CE->getOpcode() == Instruction::BitCast ||
234 CE->getOpcode() == Instruction::GetElementPtr) &&
235 "Unsupported aliasee");
237 return dyn_cast<GlobalValue>(CE->getOperand(0));
240 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
241 SmallPtrSet<const GlobalValue*, 3> Visited;
243 // Check if we need to stop early.
244 if (stopOnWeak && mayBeOverridden())
247 const GlobalValue *GV = getAliasedGlobal();
250 // Iterate over aliasing chain, stopping on weak alias if necessary.
251 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
252 if (stopOnWeak && GA->mayBeOverridden())
255 GV = GA->getAliasedGlobal();
257 if (!Visited.insert(GV))