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