Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Value.cpp - Implement the Value class -----------------------------===// |
| 2 | // |
Chris Lattner | 67e08db | 2002-04-28 04:52:28 +0000 | [diff] [blame] | 3 | // This file implements the Value and User classes. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 7 | #include "llvm/InstrTypes.h" |
| 8 | #include "llvm/SymbolTable.h" |
Chris Lattner | 02d429d | 2002-01-25 03:45:27 +0000 | [diff] [blame] | 9 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame^] | 10 | #include "Support/LeakDetector.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | #include <algorithm> |
Chris Lattner | 3e00994 | 2002-06-30 16:25:25 +0000 | [diff] [blame] | 12 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | // Value Class |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 17 | static inline const Type *checkType(const Type *Ty) { |
| 18 | assert(Ty && "Value defined with a null type: Error!"); |
| 19 | return Ty; |
| 20 | } |
| 21 | |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 22 | Value::Value(const Type *ty, ValueTy vty, const std::string &name) |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 23 | : Name(name), Ty(checkType(ty), this) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | VTy = vty; |
| 25 | } |
| 26 | |
| 27 | Value::~Value() { |
| 28 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | if (Uses.begin() != Uses.end()) { |
Chris Lattner | e7eaf17 | 2002-04-12 18:19:45 +0000 | [diff] [blame] | 36 | std::cerr << "While deleting: " << Ty << "%" << Name << "\n"; |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 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"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | } |
| 41 | #endif |
| 42 | assert(Uses.begin() == Uses.end()); |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame^] | 43 | |
| 44 | // There should be no uses of this object anymore, remove it. |
| 45 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void Value::replaceAllUsesWith(Value *D) { |
| 49 | assert(D && "Value::replaceAllUsesWith(<null>) is invalid!"); |
Chris Lattner | 7b8ec2d | 2001-06-29 05:25:51 +0000 | [diff] [blame] | 50 | assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!"); |
Chris Lattner | 150dcb9 | 2002-03-21 05:38:15 +0000 | [diff] [blame] | 51 | assert(D->getType() == getType() && |
| 52 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | while (!Uses.empty()) { |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 54 | User *Use = Uses.back(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | #ifndef NDEBUG |
| 56 | unsigned NumUses = Uses.size(); |
| 57 | #endif |
| 58 | Use->replaceUsesOfWith(this, D); |
| 59 | |
| 60 | #ifndef NDEBUG // only in -g mode... |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 61 | if (Uses.size() == NumUses) |
| 62 | std::cerr << "Use: " << *Use << "replace with: " << *D; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | #endif |
| 64 | assert(Uses.size() != NumUses && "Didn't remove definition!"); |
| 65 | } |
| 66 | } |
| 67 | |
Chris Lattner | 36bd82a | 2001-09-07 16:57:07 +0000 | [diff] [blame] | 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) { |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 74 | assert(Ty.get() == OldTy && "Can't refine anything but my type!"); |
Chris Lattner | 02d429d | 2002-01-25 03:45:27 +0000 | [diff] [blame] | 75 | if (OldTy == NewTy && !OldTy->isAbstract()) |
| 76 | Ty.removeUserFromConcrete(); |
Chris Lattner | 36bd82a | 2001-09-07 16:57:07 +0000 | [diff] [blame] | 77 | Ty = NewTy; |
| 78 | } |
| 79 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | void Value::killUse(User *i) { |
| 81 | if (i == 0) return; |
| 82 | use_iterator I = find(Uses.begin(), Uses.end(), i); |
| 83 | |
| 84 | assert(I != Uses.end() && "Use not in uses list!!"); |
| 85 | Uses.erase(I); |
| 86 | } |
| 87 | |
| 88 | User *Value::use_remove(use_iterator &I) { |
| 89 | assert(I != Uses.end() && "Trying to remove the end of the use list!!!"); |
| 90 | User *i = *I; |
| 91 | I = Uses.erase(I); |
| 92 | return i; |
| 93 | } |
| 94 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // User Class |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 99 | User::User(const Type *Ty, ValueTy vty, const std::string &name) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 100 | : Value(Ty, vty, name) { |
| 101 | } |
| 102 | |
| 103 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 104 | // references to the "To" definition. |
| 105 | // |
| 106 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 107 | if (From == To) return; // Duh what? |
| 108 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 109 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 110 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | // The side effects of this setOperand call include linking to |
| 112 | // "To", adding "this" to the uses list of To, and |
| 113 | // most importantly, removing "this" from the use list of "From". |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 114 | setOperand(i, To); // Fix it now... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |