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/LeakDetector.h"
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
29 /// it. This involves recursively eliminating any dead users of the
31 static bool removeDeadUsersOfConstant(Constant *C) {
32 if (isa<GlobalValue>(C)) return false; // Cannot remove this
34 while (!C->use_empty()) {
35 Constant *User = dyn_cast<Constant>(C->use_back());
36 if (!User) return false; // Non-constant usage;
37 if (!removeDeadUsersOfConstant(User))
38 return false; // Constant wasn't dead
45 /// removeDeadConstantUsers - If there are any dead constant users dangling
46 /// off of this global value, remove them. This method is useful for clients
47 /// that want to check to see if a global is unused, but don't want to deal
48 /// with potentially dead constants hanging off of the globals.
49 void GlobalValue::removeDeadConstantUsers() {
50 Value::use_iterator I = use_begin(), E = use_end();
51 Value::use_iterator LastNonDeadUser = E;
53 if (Constant *User = dyn_cast<Constant>(*I)) {
54 if (!removeDeadUsersOfConstant(User)) {
55 // If the constant wasn't dead, remember that this was the last live use
56 // and move on to the next constant.
60 // If the constant was dead, then the iterator is invalidated.
61 if (LastNonDeadUser == E) {
76 /// Override destroyConstant to make sure it doesn't get called on
77 /// GlobalValue's because they shouldn't be treated like other constants.
78 void GlobalValue::destroyConstant() {
79 assert(0 && "You can't GV->destroyConstant()!");
83 //===----------------------------------------------------------------------===//
84 // GlobalVariable Implementation
85 //===----------------------------------------------------------------------===//
87 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
88 Constant *InitVal, const std::string &Name,
89 Module *ParentModule, bool ThreadLocal,
90 unsigned AddressSpace)
91 : GlobalValue(PointerType::get(Ty, AddressSpace), 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!");
98 Op<0>().init(InitVal, this);
101 LeakDetector::addGarbageObject(this);
104 ParentModule->getGlobalList().push_back(this);
107 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
108 Constant *InitVal, const std::string &Name,
109 GlobalVariable *Before, bool ThreadLocal,
110 unsigned AddressSpace)
111 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
112 OperandTraits<GlobalVariable>::op_begin(this),
113 InitVal != 0, Link, Name),
114 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
116 assert(InitVal->getType() == Ty &&
117 "Initializer should be the same type as the GlobalVariable!");
118 Op<0>().init(InitVal, this);
121 LeakDetector::addGarbageObject(this);
124 Before->getParent()->getGlobalList().insert(Before, this);
127 void GlobalVariable::setParent(Module *parent) {
129 LeakDetector::addGarbageObject(this);
132 LeakDetector::removeGarbageObject(this);
135 void GlobalVariable::removeFromParent() {
136 getParent()->getGlobalList().remove(this);
139 void GlobalVariable::eraseFromParent() {
140 getParent()->getGlobalList().erase(this);
143 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
145 // If you call this, then you better know this GVar has a constant
146 // initializer worth replacing. Enforce that here.
147 assert(getNumOperands() == 1 &&
148 "Attempt to replace uses of Constants on a GVar with no initializer");
150 // And, since you know it has an initializer, the From value better be
151 // the initializer :)
152 assert(getOperand(0) == From &&
153 "Attempt to replace wrong constant initializer in GVar");
155 // And, you better have a constant for the replacement value
156 assert(isa<Constant>(To) &&
157 "Attempt to replace GVar initializer with non-constant");
159 // Okay, preconditions out of the way, replace the constant initializer.
160 this->setOperand(0, cast<Constant>(To));
163 //===----------------------------------------------------------------------===//
164 // GlobalAlias Implementation
165 //===----------------------------------------------------------------------===//
167 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
168 const std::string &Name, Constant* aliasee,
169 Module *ParentModule)
170 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
171 LeakDetector::addGarbageObject(this);
174 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
175 Op<0>().init(aliasee, this);
178 ParentModule->getAliasList().push_back(this);
181 void GlobalAlias::setParent(Module *parent) {
183 LeakDetector::addGarbageObject(this);
186 LeakDetector::removeGarbageObject(this);
189 void GlobalAlias::removeFromParent() {
190 getParent()->getAliasList().remove(this);
193 void GlobalAlias::eraseFromParent() {
194 getParent()->getAliasList().erase(this);
197 bool GlobalAlias::isDeclaration() const {
198 const GlobalValue* AV = getAliasedGlobal();
200 return AV->isDeclaration();
205 void GlobalAlias::setAliasee(Constant *Aliasee)
208 assert(Aliasee->getType() == getType() &&
209 "Alias and aliasee types should match!");
211 setOperand(0, Aliasee);
214 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
215 const Constant *C = getAliasee();
217 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
220 const ConstantExpr *CE = 0;
221 if ((CE = dyn_cast<ConstantExpr>(C)) &&
222 (CE->getOpcode() == Instruction::BitCast ||
223 CE->getOpcode() == Instruction::GetElementPtr))
224 return dyn_cast<GlobalValue>(CE->getOperand(0));
226 assert(0 && "Unsupported aliasee");
232 const GlobalValue *GlobalAlias::resolveAliasedGlobal() const {
233 SmallPtrSet<const GlobalValue*, 3> Visited;
235 const GlobalValue *GV = getAliasedGlobal();
238 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
239 GV = GA->getAliasedGlobal();
241 if (!Visited.insert(GV))