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 //===----------------------------------------------------------------------===//
66 // GlobalVariable Implementation
67 //===----------------------------------------------------------------------===//
69 GlobalVariable::GlobalVariable(const Type
*Ty
, bool constant
, LinkageTypes Link
,
70 Constant
*InitVal
, const Twine
&Name
,
71 bool ThreadLocal
, unsigned AddressSpace
)
72 : GlobalValue(PointerType::get(Ty
, AddressSpace
),
73 Value::GlobalVariableVal
,
74 OperandTraits
<GlobalVariable
>::op_begin(this),
75 InitVal
!= 0, Link
, Name
),
76 isConstantGlobal(constant
), isThreadLocalSymbol(ThreadLocal
) {
78 assert(InitVal
->getType() == Ty
&&
79 "Initializer should be the same type as the GlobalVariable!");
83 LeakDetector::addGarbageObject(this);
86 GlobalVariable::GlobalVariable(Module
&M
, const Type
*Ty
, bool constant
,
87 LinkageTypes Link
, Constant
*InitVal
,
89 GlobalVariable
*Before
, bool ThreadLocal
,
90 unsigned AddressSpace
)
91 : GlobalValue(PointerType::get(Ty
, AddressSpace
),
92 Value::GlobalVariableVal
,
93 OperandTraits
<GlobalVariable
>::op_begin(this),
94 InitVal
!= 0, Link
, Name
),
95 isConstantGlobal(constant
), isThreadLocalSymbol(ThreadLocal
) {
97 assert(InitVal
->getType() == Ty
&&
98 "Initializer should be the same type as the GlobalVariable!");
102 LeakDetector::addGarbageObject(this);
105 Before
->getParent()->getGlobalList().insert(Before
, this);
107 M
.getGlobalList().push_back(this);
110 void GlobalVariable::setParent(Module
*parent
) {
112 LeakDetector::addGarbageObject(this);
115 LeakDetector::removeGarbageObject(this);
118 void GlobalVariable::removeFromParent() {
119 getParent()->getGlobalList().remove(this);
122 void GlobalVariable::eraseFromParent() {
123 getParent()->getGlobalList().erase(this);
126 void GlobalVariable::replaceUsesOfWithOnConstant(Value
*From
, Value
*To
,
128 // If you call this, then you better know this GVar has a constant
129 // initializer worth replacing. Enforce that here.
130 assert(getNumOperands() == 1 &&
131 "Attempt to replace uses of Constants on a GVar with no initializer");
133 // And, since you know it has an initializer, the From value better be
134 // the initializer :)
135 assert(getOperand(0) == From
&&
136 "Attempt to replace wrong constant initializer in GVar");
138 // And, you better have a constant for the replacement value
139 assert(isa
<Constant
>(To
) &&
140 "Attempt to replace GVar initializer with non-constant");
142 // Okay, preconditions out of the way, replace the constant initializer.
143 this->setOperand(0, cast
<Constant
>(To
));
146 void GlobalVariable::setInitializer(Constant
*InitVal
) {
148 if (hasInitializer()) {
153 assert(InitVal
->getType() == getType()->getElementType() &&
154 "Initializer type must match GlobalVariable type");
155 if (!hasInitializer())
157 Op
<0>().set(InitVal
);
161 /// copyAttributesFrom - copy all additional attributes (those not needed to
162 /// create a GlobalVariable) from the GlobalVariable Src to this one.
163 void GlobalVariable::copyAttributesFrom(const GlobalValue
*Src
) {
164 assert(isa
<GlobalVariable
>(Src
) && "Expected a GlobalVariable!");
165 GlobalValue::copyAttributesFrom(Src
);
166 const GlobalVariable
*SrcVar
= cast
<GlobalVariable
>(Src
);
167 setThreadLocal(SrcVar
->isThreadLocal());
171 //===----------------------------------------------------------------------===//
172 // GlobalAlias Implementation
173 //===----------------------------------------------------------------------===//
175 GlobalAlias::GlobalAlias(const Type
*Ty
, LinkageTypes Link
,
176 const Twine
&Name
, Constant
* aliasee
,
177 Module
*ParentModule
)
178 : GlobalValue(Ty
, Value::GlobalAliasVal
, &Op
<0>(), 1, Link
, Name
) {
179 LeakDetector::addGarbageObject(this);
182 assert(aliasee
->getType() == Ty
&& "Alias and aliasee types should match!");
186 ParentModule
->getAliasList().push_back(this);
189 void GlobalAlias::setParent(Module
*parent
) {
191 LeakDetector::addGarbageObject(this);
194 LeakDetector::removeGarbageObject(this);
197 void GlobalAlias::removeFromParent() {
198 getParent()->getAliasList().remove(this);
201 void GlobalAlias::eraseFromParent() {
202 getParent()->getAliasList().erase(this);
205 bool GlobalAlias::isDeclaration() const {
206 const GlobalValue
* AV
= getAliasedGlobal();
208 return AV
->isDeclaration();
213 void GlobalAlias::setAliasee(Constant
*Aliasee
)
216 assert(Aliasee
->getType() == getType() &&
217 "Alias and aliasee types should match!");
219 setOperand(0, Aliasee
);
222 const GlobalValue
*GlobalAlias::getAliasedGlobal() const {
223 const Constant
*C
= getAliasee();
225 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
228 const ConstantExpr
*CE
= 0;
229 if ((CE
= dyn_cast
<ConstantExpr
>(C
)) &&
230 (CE
->getOpcode() == Instruction::BitCast
||
231 CE
->getOpcode() == Instruction::GetElementPtr
))
232 return dyn_cast
<GlobalValue
>(CE
->getOperand(0));
234 llvm_unreachable("Unsupported aliasee");
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
))