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