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 | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 10 | #include "llvm/Constant.h" |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 11 | #include "Support/LeakDetector.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Chris Lattner | 3e00994 | 2002-06-30 16:25:25 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | // Value Class |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 18 | static inline const Type *checkType(const Type *Ty) { |
| 19 | assert(Ty && "Value defined with a null type: Error!"); |
| 20 | return Ty; |
| 21 | } |
| 22 | |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 23 | Value::Value(const Type *ty, ValueTy vty, const std::string &name) |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 24 | : Name(name), Ty(checkType(ty), this) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | VTy = vty; |
| 26 | } |
| 27 | |
| 28 | Value::~Value() { |
| 29 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | if (Uses.begin() != Uses.end()) { |
Chris Lattner | e7eaf17 | 2002-04-12 18:19:45 +0000 | [diff] [blame] | 37 | std::cerr << "While deleting: " << Ty << "%" << Name << "\n"; |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 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"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | } |
| 42 | #endif |
Chris Lattner | a89ab2c | 2003-06-24 22:20:19 +0000 | [diff] [blame] | 43 | assert(Uses.begin() == Uses.end() &&"Uses remain when a value is destroyed!"); |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 44 | |
| 45 | // There should be no uses of this object anymore, remove it. |
| 46 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 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() && |
Chris Lattner | 150dcb9 | 2002-03-21 05:38:15 +0000 | [diff] [blame] | 56 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | while (!Uses.empty()) { |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 58 | User *Use = Uses.back(); |
Chris Lattner | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 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)) { |
Chris Lattner | 287d111 | 2003-08-29 05:37:22 +0000 | [diff] [blame^] | 81 | C->replaceUsesOfWithOnConstant(this, New, true); |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 82 | } else { |
| 83 | Use->replaceUsesOfWith(this, New); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
Chris Lattner | 36bd82a | 2001-09-07 16:57:07 +0000 | [diff] [blame] | 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) { |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 95 | assert(Ty.get() == OldTy && "Can't refine anything but my type!"); |
Chris Lattner | 02d429d | 2002-01-25 03:45:27 +0000 | [diff] [blame] | 96 | if (OldTy == NewTy && !OldTy->isAbstract()) |
| 97 | Ty.removeUserFromConcrete(); |
Chris Lattner | 36bd82a | 2001-09-07 16:57:07 +0000 | [diff] [blame] | 98 | Ty = NewTy; |
| 99 | } |
| 100 | |
Chris Lattner | d1c657e | 2002-10-08 23:46:55 +0000 | [diff] [blame] | 101 | void Value::killUse(User *U) { |
| 102 | if (U == 0) return; |
| 103 | unsigned i; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 104 | |
Chris Lattner | d1c657e | 2002-10-08 23:46:55 +0000 | [diff] [blame] | 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!!"); |
Chris Lattner | 0779388 | 2003-02-13 19:46:22 +0000 | [diff] [blame] | 112 | Uses[i] = Uses.back(); |
| 113 | Uses.pop_back(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 116 | //===----------------------------------------------------------------------===// |
| 117 | // User Class |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | User::User(const Type *Ty, ValueTy vty, const std::string &name) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 130 | assert(!isa<Constant>(this) && |
| 131 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 132 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 133 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 134 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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". |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 138 | setOperand(i, To); // Fix it now... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 140 | } |