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());
56 void GlobalValue::setAlignment(unsigned Align) {
57 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
58 assert(Align <= MaximumAlignment &&
59 "Alignment is greater than MaximumAlignment!");
60 Alignment = Log2_32(Align) + 1;
61 assert(getAlignment() == Align && "Alignment representation error!");
64 //===----------------------------------------------------------------------===//
65 // GlobalVariable Implementation
66 //===----------------------------------------------------------------------===//
68 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
69 Constant *InitVal, const Twine &Name,
70 bool ThreadLocal, unsigned AddressSpace)
71 : GlobalValue(PointerType::get(Ty, AddressSpace),
72 Value::GlobalVariableVal,
73 OperandTraits<GlobalVariable>::op_begin(this),
74 InitVal != 0, Link, Name),
75 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
77 assert(InitVal->getType() == Ty &&
78 "Initializer should be the same type as the GlobalVariable!");
82 LeakDetector::addGarbageObject(this);
85 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
86 LinkageTypes Link, Constant *InitVal,
88 GlobalVariable *Before, bool ThreadLocal,
89 unsigned AddressSpace)
90 : GlobalValue(PointerType::get(Ty, AddressSpace),
91 Value::GlobalVariableVal,
92 OperandTraits<GlobalVariable>::op_begin(this),
93 InitVal != 0, Link, Name),
94 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
96 assert(InitVal->getType() == Ty &&
97 "Initializer should be the same type as the GlobalVariable!");
101 LeakDetector::addGarbageObject(this);
104 Before->getParent()->getGlobalList().insert(Before, this);
106 M.getGlobalList().push_back(this);
109 void GlobalVariable::setParent(Module *parent) {
111 LeakDetector::addGarbageObject(this);
114 LeakDetector::removeGarbageObject(this);
117 void GlobalVariable::removeFromParent() {
118 getParent()->getGlobalList().remove(this);
121 void GlobalVariable::eraseFromParent() {
122 getParent()->getGlobalList().erase(this);
125 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
127 // If you call this, then you better know this GVar has a constant
128 // initializer worth replacing. Enforce that here.
129 assert(getNumOperands() == 1 &&
130 "Attempt to replace uses of Constants on a GVar with no initializer");
132 // And, since you know it has an initializer, the From value better be
133 // the initializer :)
134 assert(getOperand(0) == From &&
135 "Attempt to replace wrong constant initializer in GVar");
137 // And, you better have a constant for the replacement value
138 assert(isa<Constant>(To) &&
139 "Attempt to replace GVar initializer with non-constant");
141 // Okay, preconditions out of the way, replace the constant initializer.
142 this->setOperand(0, cast<Constant>(To));
145 void GlobalVariable::setInitializer(Constant *InitVal) {
147 if (hasInitializer()) {
152 assert(InitVal->getType() == getType()->getElementType() &&
153 "Initializer type must match GlobalVariable type");
154 if (!hasInitializer())
156 Op<0>().set(InitVal);
160 /// copyAttributesFrom - copy all additional attributes (those not needed to
161 /// create a GlobalVariable) from the GlobalVariable Src to this one.
162 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
163 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
164 GlobalValue::copyAttributesFrom(Src);
165 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
166 setThreadLocal(SrcVar->isThreadLocal());
170 //===----------------------------------------------------------------------===//
171 // GlobalAlias Implementation
172 //===----------------------------------------------------------------------===//
174 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
175 const Twine &Name, Constant* aliasee,
176 Module *ParentModule)
177 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
178 LeakDetector::addGarbageObject(this);
181 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
185 ParentModule->getAliasList().push_back(this);
188 void GlobalAlias::setParent(Module *parent) {
190 LeakDetector::addGarbageObject(this);
193 LeakDetector::removeGarbageObject(this);
196 void GlobalAlias::removeFromParent() {
197 getParent()->getAliasList().remove(this);
200 void GlobalAlias::eraseFromParent() {
201 getParent()->getAliasList().erase(this);
204 bool GlobalAlias::isDeclaration() const {
205 const GlobalValue* AV = getAliasedGlobal();
207 return AV->isDeclaration();
212 void GlobalAlias::setAliasee(Constant *Aliasee)
215 assert(Aliasee->getType() == getType() &&
216 "Alias and aliasee types should match!");
218 setOperand(0, Aliasee);
221 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
222 const Constant *C = getAliasee();
224 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
227 const ConstantExpr *CE = 0;
228 if ((CE = dyn_cast<ConstantExpr>(C)) &&
229 (CE->getOpcode() == Instruction::BitCast ||
230 CE->getOpcode() == Instruction::GetElementPtr))
231 return dyn_cast<GlobalValue>(CE->getOperand(0));
233 llvm_unreachable("Unsupported aliasee");
239 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
240 SmallPtrSet<const GlobalValue*, 3> Visited;
242 // Check if we need to stop early.
243 if (stopOnWeak && mayBeOverridden())
246 const GlobalValue *GV = getAliasedGlobal();
249 // Iterate over aliasing chain, stopping on weak alias if necessary.
250 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
251 if (stopOnWeak && GA->mayBeOverridden())
254 GV = GA->getAliasedGlobal();
256 if (!Visited.insert(GV))