Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Value.cpp - Implement the Value class -----------------------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 30ddf7f | 2002-04-28 04:52:28 +0000 | [diff] [blame] | 10 | // This file implements the Value and User classes. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/InstrTypes.h" |
| 15 | #include "llvm/SymbolTable.h" |
Chris Lattner | 67e5c29 | 2002-01-25 03:45:27 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Reid Spencer | 8baf8e2 | 2004-07-04 11:55:37 +0000 | [diff] [blame] | 21 | #include <iostream> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Value Class |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 28 | static inline const Type *checkType(const Type *Ty) { |
| 29 | assert(Ty && "Value defined with a null type: Error!"); |
| 30 | return Ty; |
| 31 | } |
| 32 | |
Chris Lattner | d0b0b45 | 2004-06-26 20:33:39 +0000 | [diff] [blame] | 33 | Value::Value(const Type *ty, unsigned scid, const std::string &name) |
Chris Lattner | a29c92f | 2005-02-05 01:37:58 +0000 | [diff] [blame^] | 34 | : SubclassID(scid), SubclassData(0), Ty(checkType(ty)), |
| 35 | UseList(0), Name(name) { |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 36 | if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Chris Lattner | 9df9afd | 2004-07-07 18:07:46 +0000 | [diff] [blame] | 37 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
| 38 | isa<OpaqueType>(ty)) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 39 | "Cannot create non-first-class values except for constants!"); |
Alkis Evlogimenos | 2f13028 | 2004-07-25 06:07:15 +0000 | [diff] [blame] | 40 | if (ty == Type::VoidTy) |
Alkis Evlogimenos | 74614b0 | 2004-07-25 06:16:52 +0000 | [diff] [blame] | 41 | assert(name.empty() && "Cannot have named void values!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Value::~Value() { |
| 45 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 46 | // Check to make sure that there are no uses of this value that are still |
| 47 | // around when the value is destroyed. If there are, then we have a dangling |
| 48 | // reference and something is wrong. This code is here to print out what is |
| 49 | // still being referenced. The value in question should be printed as |
| 50 | // a <badref> |
| 51 | // |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 52 | if (use_begin() != use_end()) { |
Misha Brukman | 65c23ee | 2004-10-15 23:08:50 +0000 | [diff] [blame] | 53 | std::cerr << "While deleting: " << *Ty << " %" << Name << "\n"; |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 54 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 55 | std::cerr << "Use still stuck around after Def is destroyed:" |
| 56 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | } |
| 58 | #endif |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 59 | assert(use_begin() == use_end() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 60 | |
| 61 | // There should be no uses of this object anymore, remove it. |
| 62 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 65 | /// hasNUses - Return true if this Value has exactly N users. |
| 66 | /// |
| 67 | bool Value::hasNUses(unsigned N) const { |
| 68 | use_const_iterator UI = use_begin(), E = use_end(); |
| 69 | |
| 70 | for (; N; --N, ++UI) |
| 71 | if (UI == E) return false; // Too few. |
| 72 | return UI == E; |
| 73 | } |
| 74 | |
| 75 | /// getNumUses - This method computes the number of uses of this Value. This |
| 76 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 77 | /// values. |
| 78 | unsigned Value::getNumUses() const { |
| 79 | return (unsigned)std::distance(use_begin(), use_end()); |
| 80 | } |
| 81 | |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 83 | // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith, |
| 84 | // except that it doesn't have all of the asserts. The asserts fail because we |
| 85 | // are half-way done resolving types, which causes some types to exist as two |
| 86 | // different Type*'s at the same time. This is a sledgehammer to work around |
| 87 | // this problem. |
| 88 | // |
| 89 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 90 | while (!use_empty()) { |
| 91 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 92 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
| 93 | // constant! |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 94 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 95 | if (!isa<GlobalValue>(C)) |
| 96 | C->replaceUsesOfWithOnConstant(this, New, true); |
| 97 | else |
| 98 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 99 | } else { |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 100 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 105 | void Value::replaceAllUsesWith(Value *New) { |
| 106 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 107 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 108 | assert(New->getType() == getType() && |
| 109 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 111 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | // User Class |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 119 | // references to the "To" definition. |
| 120 | // |
| 121 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 122 | if (From == To) return; // Duh what? |
| 123 | |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 124 | assert(!isa<Constant>(this) || isa<GlobalValue>(this) && |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 125 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 126 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 127 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 128 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | // The side effects of this setOperand call include linking to |
| 130 | // "To", adding "this" to the uses list of To, and |
| 131 | // most importantly, removing "this" from the use list of "From". |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 132 | setOperand(i, To); // Fix it now... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 134 | } |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 135 | |