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 &Initializer, InitVal != 0, Link, Name),
93 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
95 assert(InitVal->getType() == Ty &&
96 "Initializer should be the same type as the GlobalVariable!");
97 Initializer.init(InitVal, this);
99 Initializer.init(0, this);
102 LeakDetector::addGarbageObject(this);
105 ParentModule->getGlobalList().push_back(this);
108 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
109 Constant *InitVal, const std::string &Name,
110 GlobalVariable *Before, bool ThreadLocal,
111 unsigned AddressSpace)
112 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
113 &Initializer, 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 Initializer.init(InitVal, this);
120 Initializer.init(0, this);
123 LeakDetector::addGarbageObject(this);
126 Before->getParent()->getGlobalList().insert(Before, this);
129 void GlobalVariable::setParent(Module *parent) {
131 LeakDetector::addGarbageObject(this);
134 LeakDetector::removeGarbageObject(this);
137 void GlobalVariable::removeFromParent() {
138 getParent()->getGlobalList().remove(this);
141 void GlobalVariable::eraseFromParent() {
142 getParent()->getGlobalList().erase(this);
145 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
147 // If you call this, then you better know this GVar has a constant
148 // initializer worth replacing. Enforce that here.
149 assert(getNumOperands() == 1 &&
150 "Attempt to replace uses of Constants on a GVar with no initializer");
152 // And, since you know it has an initializer, the From value better be
153 // the initializer :)
154 assert(getOperand(0) == From &&
155 "Attempt to replace wrong constant initializer in GVar");
157 // And, you better have a constant for the replacement value
158 assert(isa<Constant>(To) &&
159 "Attempt to replace GVar initializer with non-constant");
161 // Okay, preconditions out of the way, replace the constant initializer.
162 this->setOperand(0, cast<Constant>(To));
165 //===----------------------------------------------------------------------===//
166 // GlobalAlias Implementation
167 //===----------------------------------------------------------------------===//
169 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
170 const std::string &Name, Constant* aliasee,
171 Module *ParentModule)
172 : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) {
173 LeakDetector::addGarbageObject(this);
176 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
177 Aliasee.init(aliasee, this);
180 ParentModule->getAliasList().push_back(this);
183 void GlobalAlias::setParent(Module *parent) {
185 LeakDetector::addGarbageObject(this);
188 LeakDetector::removeGarbageObject(this);
191 void GlobalAlias::removeFromParent() {
192 getParent()->getAliasList().remove(this);
195 void GlobalAlias::eraseFromParent() {
196 getParent()->getAliasList().erase(this);
199 bool GlobalAlias::isDeclaration() const {
200 const GlobalValue* AV = getAliasedGlobal();
202 return AV->isDeclaration();
207 void GlobalAlias::setAliasee(Constant *Aliasee)
210 assert(Aliasee->getType() == getType() &&
211 "Alias and aliasee types should match!");
213 setOperand(0, Aliasee);
216 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
217 const Constant *C = getAliasee();
219 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
222 const ConstantExpr *CE = 0;
223 if ((CE = dyn_cast<ConstantExpr>(C)) &&
224 (CE->getOpcode() == Instruction::BitCast ||
225 CE->getOpcode() == Instruction::GetElementPtr))
226 return dyn_cast<GlobalValue>(CE->getOperand(0));
228 assert(0 && "Unsupported aliasee");
234 const GlobalValue *GlobalAlias::resolveAliasedGlobal() const {
235 SmallPtrSet<const GlobalValue*, 3> Visited;
237 const GlobalValue *GV = getAliasedGlobal();
240 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
241 GV = GA->getAliasedGlobal();
243 if (!Visited.insert(GV))