- Remove Value::use_remove
[oota-llvm.git] / lib / VMCore / Value.cpp
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // This file implements the Value and User classes. 
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/InstrTypes.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/DerivedTypes.h"
10 #include "Support/LeakDetector.h"
11 #include <algorithm>
12
13 //===----------------------------------------------------------------------===//
14 //                                Value Class
15 //===----------------------------------------------------------------------===//
16
17 static inline const Type *checkType(const Type *Ty) {
18   assert(Ty && "Value defined with a null type: Error!");
19   return Ty;
20 }
21
22 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
23   : Name(name), Ty(checkType(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     std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
37     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
38       std::cerr << "Use still stuck around after Def is destroyed:"
39                 << **I << "\n";
40   }
41 #endif
42   assert(Uses.begin() == Uses.end());
43
44   // There should be no uses of this object anymore, remove it.
45   LeakDetector::removeGarbageObject(this);
46 }
47
48 void Value::replaceAllUsesWith(Value *D) {
49   assert(D && "Value::replaceAllUsesWith(<null>) is invalid!");
50   assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!");
51   assert(D->getType() == getType() &&
52          "replaceAllUses of value with new value of different type!");
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       std::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() == OldTy && "Can't refine anything but my type!");
75   if (OldTy == NewTy && !OldTy->isAbstract())
76     Ty.removeUserFromConcrete();
77   Ty = NewTy;
78 }
79
80 void Value::killUse(User *U) {
81   if (U == 0) return;
82   unsigned i;
83
84   // Scan backwards through the uses list looking for the user.  We do this
85   // because vectors like to be accessed on the end.  This is incredibly
86   // important from a performance perspective.
87   for (i = Uses.size()-1; Uses[i] != U; --i)
88     /* empty */;
89
90   assert(i < Uses.size() && "Use not in uses list!!");
91   Uses.erase(Uses.begin()+i);
92 }
93
94 //===----------------------------------------------------------------------===//
95 //                                 User Class
96 //===----------------------------------------------------------------------===//
97
98 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
99   : Value(Ty, vty, name) {
100 }
101
102 // replaceUsesOfWith - Replaces all references to the "From" definition with
103 // references to the "To" definition.
104 //
105 void User::replaceUsesOfWith(Value *From, Value *To) {
106   if (From == To) return;   // Duh what?
107
108   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
109     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
110       // The side effects of this setOperand call include linking to
111       // "To", adding "this" to the uses list of To, and
112       // most importantly, removing "this" from the use list of "From".
113       setOperand(i, To); // Fix it now...
114     }
115 }
116
117