Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Value.cpp - Implement the Value class -----------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +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 | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 14 | #include "llvm/Constant.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/InstrTypes.h" |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 19 | #include "llvm/ValueSymbolTable.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Value Class |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 29 | static inline const Type *checkType(const Type *Ty) { |
| 30 | assert(Ty && "Value defined with a null type: Error!"); |
| 31 | return Ty; |
| 32 | } |
| 33 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 34 | Value::Value(const Type *ty, unsigned scid) |
Chris Lattner | a29c92f | 2005-02-05 01:37:58 +0000 | [diff] [blame] | 35 | : SubclassID(scid), SubclassData(0), Ty(checkType(ty)), |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 36 | UseList(0), Name(0) { |
Devang Patel | ad582fc | 2008-02-21 02:14:01 +0000 | [diff] [blame] | 37 | if (isa<CallInst>(this) || isa<InvokeInst>(this)) |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 38 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
| 39 | isa<OpaqueType>(ty) || Ty->getTypeID() == Type::StructTyID) && |
| 40 | "invalid CallInst type!"); |
| 41 | else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | assert((Ty->isFirstClassType() || Ty == Type::VoidTy || |
Chris Lattner | 9df9afd | 2004-07-07 18:07:46 +0000 | [diff] [blame] | 43 | isa<OpaqueType>(ty)) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 44 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 47 | Value::~Value() { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 49 | // Check to make sure that there are no uses of this value that are still |
| 50 | // around when the value is destroyed. If there are, then we have a dangling |
| 51 | // reference and something is wrong. This code is here to print out what is |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 52 | // still being referenced. The value in question should be printed as |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 53 | // a <badref> |
| 54 | // |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 55 | if (!use_empty()) { |
Nick Lewycky | 7f6e95a | 2008-03-01 17:20:55 +0000 | [diff] [blame^] | 56 | DOUT << "While deleting: " << *Ty << " %" << getNameStr() << "\n"; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 57 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 58 | DOUT << "Use still stuck around after Def is destroyed:" |
| 59 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | } |
| 61 | #endif |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 62 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 63 | |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 64 | // If this value is named, destroy the name. This should not be in a symtab |
| 65 | // at this point. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 66 | if (Name) |
| 67 | Name->Destroy(); |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 69 | // There should be no uses of this object anymore, remove it. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 70 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 73 | /// hasNUses - Return true if this Value has exactly N users. |
| 74 | /// |
| 75 | bool Value::hasNUses(unsigned N) const { |
| 76 | use_const_iterator UI = use_begin(), E = use_end(); |
| 77 | |
| 78 | for (; N; --N, ++UI) |
| 79 | if (UI == E) return false; // Too few. |
| 80 | return UI == E; |
| 81 | } |
| 82 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 83 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 84 | /// logically equivalent to getNumUses() >= N. |
| 85 | /// |
| 86 | bool Value::hasNUsesOrMore(unsigned N) const { |
| 87 | use_const_iterator UI = use_begin(), E = use_end(); |
| 88 | |
| 89 | for (; N; --N, ++UI) |
| 90 | if (UI == E) return false; // Too few. |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 96 | /// getNumUses - This method computes the number of uses of this Value. This |
| 97 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 98 | /// values. |
| 99 | unsigned Value::getNumUses() const { |
| 100 | return (unsigned)std::distance(use_begin(), use_end()); |
| 101 | } |
| 102 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 103 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | 569c8ac | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 104 | ST = 0; |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 105 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 106 | if (BasicBlock *P = I->getParent()) |
| 107 | if (Function *PP = P->getParent()) |
| 108 | ST = &PP->getValueSymbolTable(); |
| 109 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 110 | if (Function *P = BB->getParent()) |
| 111 | ST = &P->getValueSymbolTable(); |
| 112 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 113 | if (Module *P = GV->getParent()) |
| 114 | ST = &P->getValueSymbolTable(); |
| 115 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
| 116 | if (Function *P = A->getParent()) |
| 117 | ST = &P->getValueSymbolTable(); |
| 118 | } else { |
| 119 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 120 | return true; // no name is setable for this. |
| 121 | } |
| 122 | return false; |
| 123 | } |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 125 | /// getNameStart - Return a pointer to a null terminated string for this name. |
| 126 | /// Note that names can have null characters within the string as well as at |
| 127 | /// their end. This always returns a non-null pointer. |
| 128 | const char *Value::getNameStart() const { |
| 129 | if (Name == 0) return ""; |
| 130 | return Name->getKeyData(); |
| 131 | } |
| 132 | |
| 133 | /// getNameLen - Return the length of the string, correctly handling nul |
| 134 | /// characters embedded into them. |
| 135 | unsigned Value::getNameLen() const { |
Chris Lattner | 2be9ec5 | 2007-09-28 20:09:40 +0000 | [diff] [blame] | 136 | return Name ? Name->getKeyLength() : 0; |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
Chris Lattner | fd27ed9 | 2007-02-15 18:53:54 +0000 | [diff] [blame] | 140 | std::string Value::getNameStr() const { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 141 | if (Name == 0) return ""; |
| 142 | return std::string(Name->getKeyData(), |
| 143 | Name->getKeyData()+Name->getKeyLength()); |
| 144 | } |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 146 | void Value::setName(const std::string &name) { |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 147 | setName(&name[0], name.size()); |
| 148 | } |
| 149 | |
Chris Lattner | cb9a626 | 2007-02-13 07:53:34 +0000 | [diff] [blame] | 150 | void Value::setName(const char *Name) { |
| 151 | setName(Name, Name ? strlen(Name) : 0); |
| 152 | } |
| 153 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 154 | void Value::setName(const char *NameStr, unsigned NameLen) { |
| 155 | if (NameLen == 0 && !hasName()) return; |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 156 | assert(getType() != Type::VoidTy && "Cannot assign a name to void values!"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 157 | |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 158 | // Get the symbol table to update for this object. |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 159 | ValueSymbolTable *ST; |
| 160 | if (getSymTab(this, ST)) |
| 161 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 163 | if (!ST) { // No symbol table to update? Just do the change. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 164 | if (NameLen == 0) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 165 | // Free the name for this value. |
| 166 | Name->Destroy(); |
| 167 | Name = 0; |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 168 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 170 | |
| 171 | if (Name) { |
| 172 | // Name isn't changing? |
| 173 | if (NameLen == Name->getKeyLength() && |
| 174 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
| 175 | return; |
| 176 | Name->Destroy(); |
| 177 | } |
| 178 | |
| 179 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 180 | // then reallocated. |
| 181 | |
| 182 | // Create the new name. |
| 183 | Name = ValueName::Create(NameStr, NameStr+NameLen); |
| 184 | Name->setValue(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 185 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 186 | } |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 187 | |
| 188 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 189 | // then reallocated. |
| 190 | if (hasName()) { |
| 191 | // Name isn't changing? |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 192 | if (NameLen == Name->getKeyLength() && |
| 193 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 194 | return; |
| 195 | |
| 196 | // Remove old name. |
| 197 | ST->removeValueName(Name); |
| 198 | Name->Destroy(); |
| 199 | Name = 0; |
| 200 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 201 | if (NameLen == 0) |
| 202 | return; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Name is changing to something new. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 206 | Name = ST->createValueName(NameStr, NameLen, this); |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 209 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 210 | /// takeName - transfer the name from V to this value, setting V's name to |
| 211 | /// empty. It is an error to call V->takeName(V). |
| 212 | void Value::takeName(Value *V) { |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 213 | ValueSymbolTable *ST = 0; |
| 214 | // If this value has a name, drop it. |
| 215 | if (hasName()) { |
| 216 | // Get the symtab this is in. |
| 217 | if (getSymTab(this, ST)) { |
| 218 | // We can't set a name on this value, but we need to clear V's name if |
| 219 | // it has one. |
| 220 | if (V->hasName()) V->setName(0, 0); |
| 221 | return; // Cannot set a name on this value (e.g. constant). |
| 222 | } |
| 223 | |
| 224 | // Remove old name. |
| 225 | if (ST) |
| 226 | ST->removeValueName(Name); |
| 227 | Name->Destroy(); |
| 228 | Name = 0; |
| 229 | } |
| 230 | |
| 231 | // Now we know that this has no name. |
| 232 | |
| 233 | // If V has no name either, we're done. |
| 234 | if (!V->hasName()) return; |
| 235 | |
| 236 | // Get this's symtab if we didn't before. |
| 237 | if (!ST) { |
| 238 | if (getSymTab(this, ST)) { |
| 239 | // Clear V's name. |
| 240 | V->setName(0, 0); |
| 241 | return; // Cannot set a name on this value (e.g. constant). |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Get V's ST, this should always succed, because V has a name. |
| 246 | ValueSymbolTable *VST; |
| 247 | bool Failure = getSymTab(V, VST); |
| 248 | assert(!Failure && "V has a name, so it should have a ST!"); |
| 249 | |
| 250 | // If these values are both in the same symtab, we can do this very fast. |
| 251 | // This works even if both values have no symtab yet. |
| 252 | if (ST == VST) { |
| 253 | // Take the name! |
| 254 | Name = V->Name; |
| 255 | V->Name = 0; |
| 256 | Name->setValue(this); |
Chris Lattner | cba18e3 | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 260 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 261 | // then reinsert it into ST. |
| 262 | |
| 263 | if (VST) |
| 264 | VST->removeValueName(V->Name); |
| 265 | Name = V->Name; |
| 266 | V->Name = 0; |
| 267 | Name->setValue(this); |
| 268 | |
| 269 | if (ST) |
| 270 | ST->reinsertValue(this); |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 274 | // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith, |
| 275 | // except that it doesn't have all of the asserts. The asserts fail because we |
| 276 | // are half-way done resolving types, which causes some types to exist as two |
| 277 | // different Type*'s at the same time. This is a sledgehammer to work around |
| 278 | // this problem. |
| 279 | // |
| 280 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 281 | while (!use_empty()) { |
| 282 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 283 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 284 | // constant because they are uniqued. |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 285 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 286 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 287 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 288 | continue; |
| 289 | } |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 291 | |
| 292 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 296 | void Value::replaceAllUsesWith(Value *New) { |
| 297 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 298 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 299 | assert(New->getType() == getType() && |
| 300 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 302 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | // User Class |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 309 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 310 | // references to the "To" definition. |
| 311 | // |
| 312 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 313 | if (From == To) return; // Duh what? |
| 314 | |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 315 | assert((!isa<Constant>(this) || isa<GlobalValue>(this)) && |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 316 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 317 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 318 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 319 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 320 | // The side effects of this setOperand call include linking to |
| 321 | // "To", adding "this" to the uses list of To, and |
| 322 | // most importantly, removing "this" from the use list of "From". |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 323 | setOperand(i, To); // Fix it now... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 325 | } |
Reid Spencer | bbddbf3 | 2004-07-18 00:01:50 +0000 | [diff] [blame] | 326 | |