1c3f76bc4b6a06cfae55438f1319892c4247096b
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // This file implements the Value class. 
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/ConstantPool.h"
12 #include "llvm/ConstPoolVals.h"
13 #include "llvm/Type.h"
14 #ifndef NDEBUG      // Only in -g mode...
15 #include "llvm/Assembly/Writer.h"
16 #endif
17 #include <algorithm>
18
19 //===----------------------------------------------------------------------===//
20 //                                Value Class
21 //===----------------------------------------------------------------------===//
22
23 Value::Value(const Type *ty, ValueTy vty, const string &name = "") : Name(name){
24   Ty = ty;
25   VTy = vty;
26 }
27
28 Value::~Value() {
29 #ifndef NDEBUG      // Only in -g mode...
30   if (Uses.begin() != Uses.end()) {
31     for (use_const_iterator I = Uses.begin(); I != Uses.end(); I++)
32       cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
33   }
34 #endif
35   assert(Uses.begin() == Uses.end());
36 }
37
38 void Value::replaceAllUsesWith(Value *D) {
39   assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
40   while (!Uses.empty()) {
41     User *Use = Uses.front();
42 #ifndef NDEBUG
43     unsigned NumUses = Uses.size();
44 #endif
45     Use->replaceUsesOfWith(this, D);
46
47 #ifndef NDEBUG      // only in -g mode...
48     if (Uses.size() == NumUses)
49       cerr << "Use: " << Use << "replace with: " << D; 
50 #endif
51     assert(Uses.size() != NumUses && "Didn't remove definition!");
52   }
53 }
54
55 void Value::killUse(User *i) {
56   if (i == 0) return;
57   use_iterator I = find(Uses.begin(), Uses.end(), i);
58
59   assert(I != Uses.end() && "Use not in uses list!!");
60   Uses.erase(I);
61 }
62
63 User *Value::use_remove(use_iterator &I) {
64   assert(I != Uses.end() && "Trying to remove the end of the use list!!!");
65   User *i = *I;
66   I = Uses.erase(I);
67   return i;
68 }
69
70
71 //===----------------------------------------------------------------------===//
72 //                                 User Class
73 //===----------------------------------------------------------------------===//
74
75 User::User(const Type *Ty, ValueTy vty, const string &name) 
76   : Value(Ty, vty, name) {
77 }
78
79 // replaceUsesOfWith - Replaces all references to the "From" definition with
80 // references to the "To" definition.
81 //
82 void User::replaceUsesOfWith(Value *From, Value *To) {
83   if (From == To) return;   // Duh what?
84
85   for (unsigned OpNum = 0; Value *D = getOperand(OpNum); OpNum++) {   
86     if (D == From) {  // Okay, this operand is pointing to our fake def.
87       // The side effects of this setOperand call include linking to
88       // "To", adding "this" to the uses list of To, and
89       // most importantly, removing "this" from the use list of "From".
90       setOperand(OpNum, To); // Fix it now...
91     }
92   }
93 }
94
95
96 //===----------------------------------------------------------------------===//
97 //                             SymTabValue Class
98 //===----------------------------------------------------------------------===//
99
100 // Instantiate Templates - This ugliness is the price we have to pay
101 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
102 //
103 template class ValueHolder<ConstPoolVal, SymTabValue>;
104
105 SymTabValue::SymTabValue(const Type *Ty, ValueTy dty, const string &name = "") 
106   : Value(Ty, dty, name), ConstPool(this) { 
107   ParentSymTab = SymTab = 0;
108 }
109
110
111 SymTabValue::~SymTabValue() {
112   ConstPool.dropAllReferences();
113   ConstPool.delete_all();
114   ConstPool.setParent(0);
115
116   delete SymTab;
117 }
118
119 void SymTabValue::setParentSymTab(SymbolTable *ST) {
120   ParentSymTab = ST;
121   if (SymTab) 
122     SymTab->setParentSymTab(ST);
123 }
124
125 SymbolTable *SymTabValue::getSymbolTableSure() {
126   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
127   return SymTab;
128 }
129
130 // hasSymbolTable() - Returns true if there is a symbol table allocated to
131 // this object AND if there is at least one name in it!
132 //
133 bool SymTabValue::hasSymbolTable() const {
134   if (!SymTab) return false;
135
136   for (SymbolTable::const_iterator I = SymTab->begin(); 
137        I != SymTab->end(); I++) {
138     if (I->second.begin() != I->second.end())
139       return true;                                // Found nonempty type plane!
140   }
141   
142   return false;
143 }