Remove some gross code by using the Value::dump method to do debug dumps
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // This file implements the Value, User, and SymTabValue classes. 
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/ValueHolderImpl.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/SymbolTable.h"
10 #include "llvm/SymTabValue.h"
11 #include "llvm/DerivedTypes.h"
12 #include <algorithm>
13
14 //===----------------------------------------------------------------------===//
15 //                                Value Class
16 //===----------------------------------------------------------------------===//
17
18 static inline const Type *checkType(const Type *Ty) {
19   assert(Ty && "Value defined with a null type: Error!");
20   return Ty;
21 }
22
23 Value::Value(const Type *ty, ValueTy vty, const std::string &name = "")
24   : Name(name), Ty(checkType(ty), this) {
25   VTy = vty;
26 }
27
28 Value::~Value() {
29 #ifndef NDEBUG      // Only in -g mode...
30   // Check to make sure that there are no uses of this value that are still
31   // around when the value is destroyed.  If there are, then we have a dangling
32   // reference and something is wrong.  This code is here to print out what is
33   // still being referenced.  The value in question should be printed as 
34   // a <badref>
35   //
36   if (Uses.begin() != Uses.end()) {
37     std::cerr << "While deleting: ";
38     dump();
39     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I) {
40       std::cerr << "Use still stuck around after Def is destroyed:";
41       (*I)->dump();
42       std::cerr << "\n";
43     }
44   }
45 #endif
46   assert(Uses.begin() == Uses.end());
47 }
48
49 void Value::replaceAllUsesWith(Value *D) {
50   assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
51   assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
52   assert(D->getType() == getType() &&
53          "replaceAllUses of value with new value of different type!");
54   while (!Uses.empty()) {
55     User *Use = Uses.back();
56 #ifndef NDEBUG
57     unsigned NumUses = Uses.size();
58 #endif
59     Use->replaceUsesOfWith(this, D);
60
61 #ifndef NDEBUG      // only in -g mode...
62     if (Uses.size() == NumUses) {
63       std::cerr << "Use: ";
64       Use->dump();
65       std::cerr << "replace with: ";
66       D->dump(); 
67     }
68 #endif
69     assert(Uses.size() != NumUses && "Didn't remove definition!");
70   }
71 }
72
73 // refineAbstractType - This function is implemented because we use
74 // potentially abstract types, and these types may be resolved to more
75 // concrete types after we are constructed.  For the value class, we simply
76 // change Ty to point to the right type.  :)
77 //
78 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
79   assert(Ty.get() == OldTy &&"Can't refine anything but my type!");
80   if (OldTy == NewTy && !OldTy->isAbstract())
81     Ty.removeUserFromConcrete();
82   Ty = NewTy;
83 }
84
85 void Value::killUse(User *i) {
86   if (i == 0) return;
87   use_iterator I = find(Uses.begin(), Uses.end(), i);
88
89   assert(I != Uses.end() && "Use not in uses list!!");
90   Uses.erase(I);
91 }
92
93 User *Value::use_remove(use_iterator &I) {
94   assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
95   User *i = *I;
96   I = Uses.erase(I);
97   return i;
98 }
99
100 //===----------------------------------------------------------------------===//
101 //                                 User Class
102 //===----------------------------------------------------------------------===//
103
104 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
105   : Value(Ty, vty, name) {
106 }
107
108 // replaceUsesOfWith - Replaces all references to the "From" definition with
109 // references to the "To" definition.
110 //
111 void User::replaceUsesOfWith(Value *From, Value *To) {
112   if (From == To) return;   // Duh what?
113
114   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
115     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
116       // The side effects of this setOperand call include linking to
117       // "To", adding "this" to the uses list of To, and
118       // most importantly, removing "this" from the use list of "From".
119       setOperand(i, To); // Fix it now...
120     }
121 }
122
123
124 //===----------------------------------------------------------------------===//
125 //                             SymTabValue Class
126 //===----------------------------------------------------------------------===//
127
128 SymTabValue::SymTabValue(Value *p) : ValueParent(p) { 
129   assert(ValueParent && "SymTavValue without parent!?!");
130   ParentSymTab = SymTab = 0;
131 }
132
133
134 SymTabValue::~SymTabValue() {
135   delete SymTab;
136 }
137
138 void SymTabValue::setParentSymTab(SymbolTable *ST) {
139   ParentSymTab = ST;
140   if (SymTab) 
141     SymTab->setParentSymTab(ST);
142 }
143
144 SymbolTable *SymTabValue::getSymbolTableSure() {
145   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
146   return SymTab;
147 }
148
149 // hasSymbolTable() - Returns true if there is a symbol table allocated to
150 // this object AND if there is at least one name in it!
151 //
152 bool SymTabValue::hasSymbolTable() const {
153   if (!SymTab) return false;
154
155   for (SymbolTable::const_iterator I = SymTab->begin(); 
156        I != SymTab->end(); ++I) {
157     if (I->second.begin() != I->second.end())
158       return true;                                // Found nonempty type plane!
159   }
160   
161   return false;
162 }