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/GlobalVariable.h"
16 #include "llvm/GlobalAlias.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/LeakDetector.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
27 /// it. This involves recursively eliminating any dead users of the
29 static bool removeDeadUsersOfConstant(Constant *C) {
30 if (isa<GlobalValue>(C)) return false; // Cannot remove this
32 while (!C->use_empty()) {
33 Constant *User = dyn_cast<Constant>(C->use_back());
34 if (!User) return false; // Non-constant usage;
35 if (!removeDeadUsersOfConstant(User))
36 return false; // Constant wasn't dead
43 /// removeDeadConstantUsers - If there are any dead constant users dangling
44 /// off of this global value, remove them. This method is useful for clients
45 /// that want to check to see if a global is unused, but don't want to deal
46 /// with potentially dead constants hanging off of the globals.
47 void GlobalValue::removeDeadConstantUsers() {
48 Value::use_iterator I = use_begin(), E = use_end();
49 Value::use_iterator LastNonDeadUser = E;
51 if (Constant *User = dyn_cast<Constant>(*I)) {
52 if (!removeDeadUsersOfConstant(User)) {
53 // If the constant wasn't dead, remember that this was the last live use
54 // and move on to the next constant.
58 // If the constant was dead, then the iterator is invalidated.
59 if (LastNonDeadUser == E) {
74 /// Override destroyConstant to make sure it doesn't get called on
75 /// GlobalValue's because they shouldn't be treated like other constants.
76 void GlobalValue::destroyConstant() {
77 assert(0 && "You can't GV->destroyConstant()!");
81 //===----------------------------------------------------------------------===//
82 // GlobalVariable Implementation
83 //===----------------------------------------------------------------------===//
85 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
86 Constant *InitVal, const std::string &Name,
87 Module *ParentModule, bool ThreadLocal)
88 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
89 &Initializer, InitVal != 0, Link, Name),
90 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
92 assert(InitVal->getType() == Ty &&
93 "Initializer should be the same type as the GlobalVariable!");
94 Initializer.init(InitVal, this);
96 Initializer.init(0, this);
99 LeakDetector::addGarbageObject(this);
102 ParentModule->getGlobalList().push_back(this);
105 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
106 Constant *InitVal, const std::string &Name,
107 GlobalVariable *Before, bool ThreadLocal)
108 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
109 &Initializer, InitVal != 0, Link, Name),
110 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
112 assert(InitVal->getType() == Ty &&
113 "Initializer should be the same type as the GlobalVariable!");
114 Initializer.init(InitVal, this);
116 Initializer.init(0, this);
119 LeakDetector::addGarbageObject(this);
122 Before->getParent()->getGlobalList().insert(Before, this);
125 void GlobalVariable::setParent(Module *parent) {
127 LeakDetector::addGarbageObject(this);
130 LeakDetector::removeGarbageObject(this);
133 void GlobalVariable::removeFromParent() {
134 getParent()->getGlobalList().remove(this);
137 void GlobalVariable::eraseFromParent() {
138 getParent()->getGlobalList().erase(this);
141 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
143 // If you call this, then you better know this GVar has a constant
144 // initializer worth replacing. Enforce that here.
145 assert(getNumOperands() == 1 &&
146 "Attempt to replace uses of Constants on a GVar with no initializer");
148 // And, since you know it has an initializer, the From value better be
149 // the initializer :)
150 assert(getOperand(0) == From &&
151 "Attempt to replace wrong constant initializer in GVar");
153 // And, you better have a constant for the replacement value
154 assert(isa<Constant>(To) &&
155 "Attempt to replace GVar initializer with non-constant");
157 // Okay, preconditions out of the way, replace the constant initializer.
158 this->setOperand(0, cast<Constant>(To));
161 //===----------------------------------------------------------------------===//
162 // GlobalAlias Implementation
163 //===----------------------------------------------------------------------===//
165 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
166 const std::string &Name, const GlobalValue* aliasee,
167 Module *ParentModule)
168 : GlobalValue(Ty, Value::GlobalAliasVal, 0, 0,
169 Link, Name), Aliasee(aliasee) {
170 LeakDetector::addGarbageObject(this);
173 ParentModule->getAliasList().push_back(this);
176 void GlobalAlias::setParent(Module *parent) {
178 LeakDetector::addGarbageObject(this);
181 LeakDetector::removeGarbageObject(this);
184 void GlobalAlias::removeFromParent() {
185 getParent()->getAliasList().remove(this);
188 void GlobalAlias::eraseFromParent() {
189 getParent()->getAliasList().erase(this);
192 bool GlobalAlias::isDeclaration() const {
193 return (Aliasee && Aliasee->isDeclaration());
196 void GlobalAlias::setAliasee(const GlobalValue *GV)
198 // FIXME: Some checks?