Fix bug: Linker/2003-08-28-TypeResolvesGlobal3.ll
[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 "llvm/Constant.h"
11 #include "Support/LeakDetector.h"
12 #include <algorithm>
13
14 //===----------------------------------------------------------------------===//
15 //                                Value Class
16 //===----------------------------------------------------------------------===//
17
18 static inline const Type *checkType(const Type *Ty) {
19   assert(Ty && "Value defined with a null type: Error!");
20   return Ty;
21 }
22
23 Value::Value(const Type *ty, ValueTy vty, const std::string &name)
24   : Name(name), Ty(checkType(ty), this) {
25   VTy = vty;
26 }
27
28 Value::~Value() {
29 #ifndef NDEBUG      // Only in -g mode...
30   // Check to make sure that there are no uses of this value that are still
31   // around when the value is destroyed.  If there are, then we have a dangling
32   // reference and something is wrong.  This code is here to print out what is
33   // still being referenced.  The value in question should be printed as 
34   // a <badref>
35   //
36   if (Uses.begin() != Uses.end()) {
37     std::cerr << "While deleting: " << Ty << "%" << Name << "\n";
38     for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
39       std::cerr << "Use still stuck around after Def is destroyed:"
40                 << **I << "\n";
41   }
42 #endif
43   assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!");
44
45   // There should be no uses of this object anymore, remove it.
46   LeakDetector::removeGarbageObject(this);
47 }
48
49
50
51
52 void Value::replaceAllUsesWith(Value *New) {
53   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
54   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
55   assert(New->getType() == getType() &&
56          "replaceAllUses of value with new value of different type!");
57   while (!Uses.empty()) {
58     User *Use = Uses.back();
59     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
60     // constant!
61     if (Constant *C = dyn_cast<Constant>(Use)) {
62       C->replaceUsesOfWithOnConstant(this, New);
63     } else {
64       Use->replaceUsesOfWith(this, New);
65     }
66   }
67 }
68
69 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
70 // except that it doesn't have all of the asserts.  The asserts fail because we
71 // are half-way done resolving types, which causes some types to exist as two
72 // different Type*'s at the same time.  This is a sledgehammer to work around
73 // this problem.
74 //
75 void Value::uncheckedReplaceAllUsesWith(Value *New) {
76   while (!Uses.empty()) {
77     User *Use = Uses.back();
78     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
79     // constant!
80     if (Constant *C = dyn_cast<Constant>(Use)) {
81       C->replaceUsesOfWithOnConstant(this, New, true);
82     } else {
83       Use->replaceUsesOfWith(this, New);
84     }
85   }
86 }
87
88
89 // refineAbstractType - This function is implemented because we use
90 // potentially abstract types, and these types may be resolved to more
91 // concrete types after we are constructed.  For the value class, we simply
92 // change Ty to point to the right type.  :)
93 //
94 void Value::refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
95   assert(Ty.get() == OldTy && "Can't refine anything but my type!");
96   if (OldTy == NewTy && !OldTy->isAbstract())
97     Ty.removeUserFromConcrete();
98   Ty = NewTy;
99 }
100
101 void Value::killUse(User *U) {
102   if (U == 0) return;
103   unsigned i;
104
105   // Scan backwards through the uses list looking for the user.  We do this
106   // because vectors like to be accessed on the end.  This is incredibly
107   // important from a performance perspective.
108   for (i = Uses.size()-1; Uses[i] != U; --i)
109     /* empty */;
110
111   assert(i < Uses.size() && "Use not in uses list!!");
112   Uses[i] = Uses.back();
113   Uses.pop_back();
114 }
115
116 //===----------------------------------------------------------------------===//
117 //                                 User Class
118 //===----------------------------------------------------------------------===//
119
120 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
121   : Value(Ty, vty, name) {
122 }
123
124 // replaceUsesOfWith - Replaces all references to the "From" definition with
125 // references to the "To" definition.
126 //
127 void User::replaceUsesOfWith(Value *From, Value *To) {
128   if (From == To) return;   // Duh what?
129
130   assert(!isa<Constant>(this) &&
131          "Cannot call User::replaceUsesofWith on a constant!");
132
133   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
134     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
135       // The side effects of this setOperand call include linking to
136       // "To", adding "this" to the uses list of To, and
137       // most importantly, removing "this" from the use list of "From".
138       setOperand(i, To); // Fix it now...
139     }
140 }