Rework GlobalValue::removeDeadConstantUsers to always remove dead constant
[oota-llvm.git] / lib / VMCore / Globals.cpp
1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the GlobalValue & GlobalVariable classes for the VMCore
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/LeakDetector.h"
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 //                            GlobalValue Class
23 //===----------------------------------------------------------------------===//
24
25 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
26 /// it.  This involves recursively eliminating any dead users of the
27 /// constantexpr.
28 static bool removeDeadUsersOfConstant(Constant *C) {
29   if (isa<GlobalValue>(C)) return false; // Cannot remove this
30
31   while (!C->use_empty()) {
32     Constant *User = dyn_cast<Constant>(C->use_back());
33     if (!User) return false; // Non-constant usage;
34     if (!removeDeadUsersOfConstant(User))
35       return false; // Constant wasn't dead
36   }
37
38   C->destroyConstant();
39   return true;
40 }
41
42 /// removeDeadConstantUsers - If there are any dead constant users dangling
43 /// off of this global value, remove them.  This method is useful for clients
44 /// that want to check to see if a global is unused, but don't want to deal
45 /// with potentially dead constants hanging off of the globals.
46 void GlobalValue::removeDeadConstantUsers() {
47   
48   Value::use_iterator I = use_begin(), E = use_end();
49   Value::use_iterator LastNonDeadUser = E;
50   for (; I != E; ++I) {
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.
55         LastNonDeadUser = I;
56       } else {
57         // If the constant was dead, then the iterator is invalidated.
58         if (LastNonDeadUser == E) {
59           I = use_begin();
60           if (I == E) break;
61         } else {
62           I = LastNonDeadUser;
63         }
64       }
65     } else {
66       LastNonDeadUser = I;
67     }
68   }
69 }
70
71 /// Override destroyConstant to make sure it doesn't get called on
72 /// GlobalValue's because they shouldn't be treated like other constants.
73 void GlobalValue::destroyConstant() {
74   assert(0 && "You can't GV->destroyConstant()!");
75   abort();
76 }
77 //===----------------------------------------------------------------------===//
78 // GlobalVariable Implementation
79 //===----------------------------------------------------------------------===//
80
81 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
82                                Constant *InitVal,
83                                const std::string &Name, Module *ParentModule)
84   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
85                 &Initializer, InitVal != 0, Link, Name),
86     isConstantGlobal(constant) {
87   if (InitVal) {
88     assert(InitVal->getType() == Ty &&
89            "Initializer should be the same type as the GlobalVariable!");
90     Initializer.init(InitVal, this);
91   } else {
92     Initializer.init(0, this);
93   }
94
95   LeakDetector::addGarbageObject(this);
96
97   if (ParentModule)
98     ParentModule->getGlobalList().push_back(this);
99 }
100
101 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
102                                Constant *InitVal,
103                                const std::string &Name, GlobalVariable *Before)
104   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
105                 &Initializer, InitVal != 0, Link, Name), 
106     isConstantGlobal(constant) {
107   if (InitVal) {
108     assert(InitVal->getType() == Ty &&
109            "Initializer should be the same type as the GlobalVariable!");
110     Initializer.init(InitVal, this);
111   } else {
112     Initializer.init(0, this);
113   }
114   
115   LeakDetector::addGarbageObject(this);
116   
117   if (Before)
118     Before->getParent()->getGlobalList().insert(Before, this);
119 }
120
121
122 void GlobalVariable::setParent(Module *parent) {
123   if (getParent())
124     LeakDetector::addGarbageObject(this);
125   Parent = parent;
126   if (getParent())
127     LeakDetector::removeGarbageObject(this);
128 }
129
130 void GlobalVariable::removeFromParent() {
131   getParent()->getGlobalList().remove(this);
132 }
133
134 void GlobalVariable::eraseFromParent() {
135   getParent()->getGlobalList().erase(this);
136 }
137
138 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
139                                                  Use *U) {
140   // If you call this, then you better know this GVar has a constant
141   // initializer worth replacing. Enforce that here.
142   assert(getNumOperands() == 1 &&
143          "Attempt to replace uses of Constants on a GVar with no initializer");
144
145   // And, since you know it has an initializer, the From value better be
146   // the initializer :)
147   assert(getOperand(0) == From &&
148          "Attempt to replace wrong constant initializer in GVar");
149
150   // And, you better have a constant for the replacement value
151   assert(isa<Constant>(To) &&
152          "Attempt to replace GVar initializer with non-constant");
153
154   // Okay, preconditions out of the way, replace the constant initializer.
155   this->setOperand(0, cast<Constant>(To));
156 }