1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Support/LeakDetector.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
28 /// it. This involves recursively eliminating any dead users of the
30 static bool removeDeadUsersOfConstant(Constant
*C
) {
31 if (isa
<GlobalValue
>(C
)) return false; // Cannot remove this
33 while (!C
->use_empty()) {
34 Constant
*User
= dyn_cast
<Constant
>(C
->use_back());
35 if (!User
) return false; // Non-constant usage;
36 if (!removeDeadUsersOfConstant(User
))
37 return false; // Constant wasn't dead
44 /// removeDeadConstantUsers - If there are any dead constant users dangling
45 /// off of this global value, remove them. This method is useful for clients
46 /// that want to check to see if a global is unused, but don't want to deal
47 /// with potentially dead constants hanging off of the globals.
48 void GlobalValue::removeDeadConstantUsers() {
49 Value::use_iterator I
= use_begin(), E
= use_end();
50 Value::use_iterator LastNonDeadUser
= E
;
52 if (Constant
*User
= dyn_cast
<Constant
>(*I
)) {
53 if (!removeDeadUsersOfConstant(User
)) {
54 // If the constant wasn't dead, remember that this was the last live use
55 // and move on to the next constant.
59 // If the constant was dead, then the iterator is invalidated.
60 if (LastNonDeadUser
== E
) {
75 /// Override destroyConstant to make sure it doesn't get called on
76 /// GlobalValue's because they shouldn't be treated like other constants.
77 void GlobalValue::destroyConstant() {
78 assert(0 && "You can't GV->destroyConstant()!");
82 //===----------------------------------------------------------------------===//
83 // GlobalVariable Implementation
84 //===----------------------------------------------------------------------===//
86 GlobalVariable::GlobalVariable(const Type
*Ty
, bool constant
, LinkageTypes Link
,
87 Constant
*InitVal
, const std::string
&Name
,
88 Module
*ParentModule
, bool ThreadLocal
)
89 : GlobalValue(PointerType::get(Ty
), Value::GlobalVariableVal
,
90 &Initializer
, InitVal
!= 0, Link
, Name
),
91 isConstantGlobal(constant
), isThreadLocalSymbol(ThreadLocal
) {
93 assert(InitVal
->getType() == Ty
&&
94 "Initializer should be the same type as the GlobalVariable!");
95 Initializer
.init(InitVal
, this);
97 Initializer
.init(0, this);
100 LeakDetector::addGarbageObject(this);
103 ParentModule
->getGlobalList().push_back(this);
106 GlobalVariable::GlobalVariable(const Type
*Ty
, bool constant
, LinkageTypes Link
,
107 Constant
*InitVal
, const std::string
&Name
,
108 GlobalVariable
*Before
, bool ThreadLocal
)
109 : GlobalValue(PointerType::get(Ty
), Value::GlobalVariableVal
,
110 &Initializer
, InitVal
!= 0, Link
, Name
),
111 isConstantGlobal(constant
), isThreadLocalSymbol(ThreadLocal
) {
113 assert(InitVal
->getType() == Ty
&&
114 "Initializer should be the same type as the GlobalVariable!");
115 Initializer
.init(InitVal
, this);
117 Initializer
.init(0, this);
120 LeakDetector::addGarbageObject(this);
123 Before
->getParent()->getGlobalList().insert(Before
, this);
126 void GlobalVariable::setParent(Module
*parent
) {
128 LeakDetector::addGarbageObject(this);
131 LeakDetector::removeGarbageObject(this);
134 void GlobalVariable::removeFromParent() {
135 getParent()->getGlobalList().remove(this);
138 void GlobalVariable::eraseFromParent() {
139 getParent()->getGlobalList().erase(this);
142 void GlobalVariable::replaceUsesOfWithOnConstant(Value
*From
, Value
*To
,
144 // If you call this, then you better know this GVar has a constant
145 // initializer worth replacing. Enforce that here.
146 assert(getNumOperands() == 1 &&
147 "Attempt to replace uses of Constants on a GVar with no initializer");
149 // And, since you know it has an initializer, the From value better be
150 // the initializer :)
151 assert(getOperand(0) == From
&&
152 "Attempt to replace wrong constant initializer in GVar");
154 // And, you better have a constant for the replacement value
155 assert(isa
<Constant
>(To
) &&
156 "Attempt to replace GVar initializer with non-constant");
158 // Okay, preconditions out of the way, replace the constant initializer.
159 this->setOperand(0, cast
<Constant
>(To
));
162 //===----------------------------------------------------------------------===//
163 // GlobalAlias Implementation
164 //===----------------------------------------------------------------------===//
166 GlobalAlias::GlobalAlias(const Type
*Ty
, LinkageTypes Link
,
167 const std::string
&Name
, Constant
* aliasee
,
168 Module
*ParentModule
)
169 : GlobalValue(Ty
, Value::GlobalAliasVal
, &Aliasee
, 1, Link
, Name
) {
170 LeakDetector::addGarbageObject(this);
173 assert(aliasee
->getType() == Ty
&& "Alias and aliasee types should match!");
174 Aliasee
.init(aliasee
, this);
177 ParentModule
->getAliasList().push_back(this);
180 void GlobalAlias::setParent(Module
*parent
) {
182 LeakDetector::addGarbageObject(this);
185 LeakDetector::removeGarbageObject(this);
188 void GlobalAlias::removeFromParent() {
189 getParent()->getAliasList().remove(this);
192 void GlobalAlias::eraseFromParent() {
193 getParent()->getAliasList().erase(this);
196 bool GlobalAlias::isDeclaration() const {
197 const GlobalValue
* AV
= getAliasedGlobal();
199 return AV
->isDeclaration();
204 void GlobalAlias::setAliasee(Constant
*Aliasee
)
207 assert(Aliasee
->getType() == getType() &&
208 "Alias and aliasee types should match!");
210 setOperand(0, Aliasee
);
213 const GlobalValue
*GlobalAlias::getAliasedGlobal() const {
214 const Constant
*C
= getAliasee();
216 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
219 const ConstantExpr
*CE
= 0;
220 if ((CE
= dyn_cast
<ConstantExpr
>(C
)) &&
221 (CE
->getOpcode() == Instruction::BitCast
||
222 CE
->getOpcode() == Instruction::GetElementPtr
))
223 return dyn_cast
<GlobalValue
>(CE
->getOperand(0));
225 assert(0 && "Unsupported aliasee");