68b4af094950594e188f21ead672d61af8907d8e
[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)) {
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(), E = Uses.end(); I != E; ++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 // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith,
51 // except that it doesn't have all of the asserts.  The asserts fail because we
52 // are half-way done resolving types, which causes some types to exist as two
53 // different Type*'s at the same time.  This is a sledgehammer to work around
54 // this problem.
55 //
56 void Value::uncheckedReplaceAllUsesWith(Value *New) {
57   while (!Uses.empty()) {
58     Use &U = Uses.back();
59     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
60     // constant!
61     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
62       C->replaceUsesOfWithOnConstant(this, New);
63     } else {
64       U.set(New);
65     }
66   }
67 }
68
69 void Value::replaceAllUsesWith(Value *New) {
70   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
71   assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
72   assert(New->getType() == getType() &&
73          "replaceAllUses of value with new value of different type!");
74
75   uncheckedReplaceAllUsesWith(New);
76 }
77
78 //===----------------------------------------------------------------------===//
79 //                                 User Class
80 //===----------------------------------------------------------------------===//
81
82 User::User(const Type *Ty, ValueTy vty, const std::string &name) 
83   : Value(Ty, vty, name) {
84 }
85
86 // replaceUsesOfWith - Replaces all references to the "From" definition with
87 // references to the "To" definition.
88 //
89 void User::replaceUsesOfWith(Value *From, Value *To) {
90   if (From == To) return;   // Duh what?
91
92   assert(!isa<Constant>(this) &&
93          "Cannot call User::replaceUsesofWith on a constant!");
94
95   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
96     if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
97       // The side effects of this setOperand call include linking to
98       // "To", adding "this" to the uses list of To, and
99       // most importantly, removing "this" from the use list of "From".
100       setOperand(i, To); // Fix it now...
101     }
102 }