Added debugging support.
[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/ConstPoolVals.h"
12 #include "llvm/Type.h"
13 #ifndef NDEBUG      // Only in -g mode...
14 #include "llvm/Assembly/Writer.h"
15 #endif
16 #include <algorithm>
17
18 //===----------------------------------------------------------------------===//
19 //                                Value Class
20 //===----------------------------------------------------------------------===//
21
22 Value::Value(const Type *ty, ValueTy vty, const string &name = "")
23   : Name(name), Ty(ty, this) {
24   VTy = vty;
25 }
26
27 Value::~Value() {
28 #ifndef NDEBUG      // Only in -g mode...
29   // Check to make sure that there are no uses of this value that are still
30   // around when the value is destroyed.  If there are, then we have a dangling
31   // reference and something is wrong.  This code is here to print out what is
32   // still being referenced.  The value in question should be printed as 
33   // a <badref>
34   //
35   if (Uses.begin() != Uses.end()) {
36     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
37       cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
38   }
39 #endif
40   assert(Uses.begin() == Uses.end());
41 }
42
43 void Value::replaceAllUsesWith(Value *D) {
44   assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
45   assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
46   while (!Uses.empty()) {
47     User *Use = Uses.back();
48 #ifndef NDEBUG
49     unsigned NumUses = Uses.size();
50 #endif
51     Use->replaceUsesOfWith(this, D);
52
53 #ifndef NDEBUG      // only in -g mode...
54     if (Uses.size() == NumUses)
55       cerr << "Use: " << Use << "replace with: " << D; 
56 #endif
57     assert(Uses.size() != NumUses && "Didn't remove definition!");
58   }
59 }
60
61 // refineAbstractType - This function is implemented because we use
62 // potentially abstract types, and these types may be resolved to more
63 // concrete types after we are constructed.  For the value class, we simply
64 // change Ty to point to the right type.  :)
65 //
66 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
67   assert(Ty.get() == (const Type*)OldTy &&"Can't refine anything but my type!");
68   Ty = NewTy;
69 }
70
71 void Value::killUse(User *i) {
72   if (i == 0) return;
73   use_iterator I = find(Uses.begin(), Uses.end(), i);
74
75   assert(I != Uses.end() && "Use not in uses list!!");
76   Uses.erase(I);
77 }
78
79 User *Value::use_remove(use_iterator &I) {
80   assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
81   User *i = *I;
82   I = Uses.erase(I);
83   return i;
84 }
85
86 void
87 Value::dump() const
88 {
89   DebugValue(*this);
90 }
91
92 ostream&
93 operator<<(ostream &o, const Value& I)
94 {
95   switch (I.getValueType()) {
96   case Value::TypeVal:       return o << I.castTypeAsserting();
97   case Value::ConstantVal:   WriteToAssembly((const ConstPoolVal*)&I,o);break;
98   case Value::MethodArgumentVal: return o << I.getType() << " "<< I.getName();
99   case Value::InstructionVal:WriteToAssembly((const Instruction *)&I, o);break;
100   case Value::BasicBlockVal: WriteToAssembly((const BasicBlock  *)&I, o);break;
101   case Value::MethodVal:     WriteToAssembly((const Method      *)&I, o);break;
102   case Value::GlobalVal:     WriteToAssembly((const GlobalVariable*)&I,o);break;
103   case Value::ModuleVal:     WriteToAssembly((const Module      *)&I,o); break;
104   default: return o << "<unknown value type: " << I.getValueType() << ">";
105   }
106   return o;
107 }
108
109 void
110 DebugValue(const Value* V)
111 {
112   if (V)
113     cerr << *V << endl;
114   else
115     cerr << "<NULL value>" << endl;
116 }
117
118 void
119 DebugValue(const Value& V)
120 {
121   cerr << V << endl;
122 }
123
124
125 //===----------------------------------------------------------------------===//
126 //                                 User Class
127 //===----------------------------------------------------------------------===//
128
129 User::User(const Type *Ty, ValueTy vty, const string &name) 
130   : Value(Ty, vty, name) {
131 }
132
133 // replaceUsesOfWith - Replaces all references to the "From" definition with
134 // references to the "To" definition.
135 //
136 void User::replaceUsesOfWith(Value *From, Value *To) {
137   if (From == To) return;   // Duh what?
138
139   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
140     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
141       // The side effects of this setOperand call include linking to
142       // "To", adding "this" to the uses list of To, and
143       // most importantly, removing "this" from the use list of "From".
144       setOperand(i, To); // Fix it now...
145     }
146 }
147
148
149 //===----------------------------------------------------------------------===//
150 //                             SymTabValue Class
151 //===----------------------------------------------------------------------===//
152
153 SymTabValue::SymTabValue(Value *p) : ValueParent(p) { 
154   assert(ValueParent && "SymTavValue without parent!?!");
155   ParentSymTab = SymTab = 0;
156 }
157
158
159 SymTabValue::~SymTabValue() {
160   delete SymTab;
161 }
162
163 void SymTabValue::setParentSymTab(SymbolTable *ST) {
164   ParentSymTab = ST;
165   if (SymTab) 
166     SymTab->setParentSymTab(ST);
167 }
168
169 SymbolTable *SymTabValue::getSymbolTableSure() {
170   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
171   return SymTab;
172 }
173
174 // hasSymbolTable() - Returns true if there is a symbol table allocated to
175 // this object AND if there is at least one name in it!
176 //
177 bool SymTabValue::hasSymbolTable() const {
178   if (!SymTab) return false;
179
180   for (SymbolTable::const_iterator I = SymTab->begin(); 
181        I != SymTab->end(); ++I) {
182     if (I->second.begin() != I->second.end())
183       return true;                                // Found nonempty type plane!
184   }
185   
186   return false;
187 }