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 | // |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 10 | // This file implements the Value, ValueHandle, 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" |
Anton Korobeynikov | 82c02b2 | 2008-05-06 22:52:30 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/InstrTypes.h" |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 20 | #include "llvm/ValueSymbolTable.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/ValueHandle.h" |
| 25 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Value Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 33 | static inline const Type *checkType(const Type *Ty) { |
| 34 | assert(Ty && "Value defined with a null type: Error!"); |
| 35 | return Ty; |
| 36 | } |
| 37 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 38 | Value::Value(const Type *ty, unsigned scid) |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 39 | : SubclassID(scid), HasValueHandle(0), SubclassData(0), VTy(checkType(ty)), |
Gabor Greif | 715b9d2 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 40 | UseList(0), Name(0) { |
Devang Patel | ad582fc | 2008-02-21 02:14:01 +0000 | [diff] [blame] | 41 | if (isa<CallInst>(this) || isa<InvokeInst>(this)) |
Evan Cheng | a05c07e | 2008-07-24 00:08:56 +0000 | [diff] [blame] | 42 | assert((VTy->isFirstClassType() || VTy == Type::VoidTy || |
| 43 | isa<OpaqueType>(ty) || VTy->getTypeID() == Type::StructTyID) && |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 44 | "invalid CallInst type!"); |
| 45 | else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Evan Cheng | a05c07e | 2008-07-24 00:08:56 +0000 | [diff] [blame] | 46 | assert((VTy->isFirstClassType() || VTy == Type::VoidTy || |
Chris Lattner | 9df9afd | 2004-07-07 18:07:46 +0000 | [diff] [blame] | 47 | isa<OpaqueType>(ty)) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 48 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 51 | Value::~Value() { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 52 | // Notify all ValueHandles (if present) that this value is going away. |
| 53 | if (HasValueHandle) |
| 54 | ValueHandleBase::ValueIsDeleted(this); |
| 55 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 57 | // Check to make sure that there are no uses of this value that are still |
| 58 | // around when the value is destroyed. If there are, then we have a dangling |
| 59 | // 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] | 60 | // still being referenced. The value in question should be printed as |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 61 | // a <badref> |
| 62 | // |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 63 | if (!use_empty()) { |
Chris Lattner | 695bc77 | 2008-12-13 18:37:58 +0000 | [diff] [blame] | 64 | cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n"; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 65 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
Chris Lattner | 695bc77 | 2008-12-13 18:37:58 +0000 | [diff] [blame] | 66 | cerr << "Use still stuck around after Def is destroyed:" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 67 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | } |
| 69 | #endif |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 70 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 71 | |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 72 | // If this value is named, destroy the name. This should not be in a symtab |
| 73 | // at this point. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 74 | if (Name) |
| 75 | Name->Destroy(); |
Chris Lattner | af867a3 | 2007-03-20 00:18:10 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 77 | // There should be no uses of this object anymore, remove it. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 78 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 81 | /// hasNUses - Return true if this Value has exactly N users. |
| 82 | /// |
| 83 | bool Value::hasNUses(unsigned N) const { |
| 84 | use_const_iterator UI = use_begin(), E = use_end(); |
| 85 | |
| 86 | for (; N; --N, ++UI) |
| 87 | if (UI == E) return false; // Too few. |
| 88 | return UI == E; |
| 89 | } |
| 90 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 91 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 92 | /// logically equivalent to getNumUses() >= N. |
| 93 | /// |
| 94 | bool Value::hasNUsesOrMore(unsigned N) const { |
| 95 | use_const_iterator UI = use_begin(), E = use_end(); |
| 96 | |
| 97 | for (; N; --N, ++UI) |
| 98 | if (UI == E) return false; // Too few. |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
Evan Cheng | 89553cc | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 103 | /// isUsedInBasicBlock - Return true if this value is used in the specified |
| 104 | /// basic block. |
Bill Wendling | 0c37421 | 2008-09-25 22:42:01 +0000 | [diff] [blame] | 105 | bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { |
Evan Cheng | 89553cc | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 106 | for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
| 107 | const Instruction *User = dyn_cast<Instruction>(*I); |
| 108 | if (User && User->getParent() == BB) |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 115 | /// getNumUses - This method computes the number of uses of this Value. This |
| 116 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 117 | /// values. |
| 118 | unsigned Value::getNumUses() const { |
| 119 | return (unsigned)std::distance(use_begin(), use_end()); |
| 120 | } |
| 121 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 122 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | 569c8ac | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 123 | ST = 0; |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 124 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 125 | if (BasicBlock *P = I->getParent()) |
| 126 | if (Function *PP = P->getParent()) |
| 127 | ST = &PP->getValueSymbolTable(); |
| 128 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 129 | if (Function *P = BB->getParent()) |
| 130 | ST = &P->getValueSymbolTable(); |
| 131 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 132 | if (Module *P = GV->getParent()) |
| 133 | ST = &P->getValueSymbolTable(); |
| 134 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
| 135 | if (Function *P = A->getParent()) |
| 136 | ST = &P->getValueSymbolTable(); |
| 137 | } else { |
| 138 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 139 | return true; // no name is setable for this. |
| 140 | } |
| 141 | return false; |
| 142 | } |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 144 | /// getNameStart - Return a pointer to a null terminated string for this name. |
| 145 | /// Note that names can have null characters within the string as well as at |
| 146 | /// their end. This always returns a non-null pointer. |
| 147 | const char *Value::getNameStart() const { |
| 148 | if (Name == 0) return ""; |
| 149 | return Name->getKeyData(); |
| 150 | } |
| 151 | |
| 152 | /// getNameLen - Return the length of the string, correctly handling nul |
| 153 | /// characters embedded into them. |
| 154 | unsigned Value::getNameLen() const { |
Chris Lattner | 2be9ec5 | 2007-09-28 20:09:40 +0000 | [diff] [blame] | 155 | return Name ? Name->getKeyLength() : 0; |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chris Lattner | a1d850e | 2008-04-30 03:55:40 +0000 | [diff] [blame] | 158 | /// isName - Return true if this value has the name specified by the provided |
| 159 | /// nul terminated string. |
| 160 | bool Value::isName(const char *N) const { |
| 161 | unsigned InLen = strlen(N); |
Chris Lattner | 78769f0 | 2008-04-30 15:27:09 +0000 | [diff] [blame] | 162 | return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0; |
Chris Lattner | a1d850e | 2008-04-30 03:55:40 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 165 | |
Chris Lattner | fd27ed9 | 2007-02-15 18:53:54 +0000 | [diff] [blame] | 166 | std::string Value::getNameStr() const { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 167 | if (Name == 0) return ""; |
| 168 | return std::string(Name->getKeyData(), |
| 169 | Name->getKeyData()+Name->getKeyLength()); |
| 170 | } |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 172 | void Value::setName(const std::string &name) { |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 173 | setName(&name[0], name.size()); |
| 174 | } |
| 175 | |
Chris Lattner | cb9a626 | 2007-02-13 07:53:34 +0000 | [diff] [blame] | 176 | void Value::setName(const char *Name) { |
| 177 | setName(Name, Name ? strlen(Name) : 0); |
| 178 | } |
| 179 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 180 | void Value::setName(const char *NameStr, unsigned NameLen) { |
| 181 | if (NameLen == 0 && !hasName()) return; |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 182 | assert(getType() != Type::VoidTy && "Cannot assign a name to void values!"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 183 | |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 184 | // Get the symbol table to update for this object. |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 185 | ValueSymbolTable *ST; |
| 186 | if (getSymTab(this, ST)) |
| 187 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 189 | if (!ST) { // No symbol table to update? Just do the change. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 190 | if (NameLen == 0) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 191 | // Free the name for this value. |
| 192 | Name->Destroy(); |
| 193 | Name = 0; |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 194 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 196 | |
| 197 | if (Name) { |
| 198 | // Name isn't changing? |
| 199 | if (NameLen == Name->getKeyLength() && |
| 200 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
| 201 | return; |
| 202 | Name->Destroy(); |
| 203 | } |
| 204 | |
| 205 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 206 | // then reallocated. |
| 207 | |
| 208 | // Create the new name. |
| 209 | Name = ValueName::Create(NameStr, NameStr+NameLen); |
| 210 | Name->setValue(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 211 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 213 | |
| 214 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 215 | // then reallocated. |
| 216 | if (hasName()) { |
| 217 | // Name isn't changing? |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 218 | if (NameLen == Name->getKeyLength() && |
| 219 | !memcmp(Name->getKeyData(), NameStr, NameLen)) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 220 | return; |
| 221 | |
| 222 | // Remove old name. |
| 223 | ST->removeValueName(Name); |
| 224 | Name->Destroy(); |
| 225 | Name = 0; |
| 226 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 227 | if (NameLen == 0) |
| 228 | return; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Name is changing to something new. |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 232 | Name = ST->createValueName(NameStr, NameLen, this); |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 235 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 236 | /// takeName - transfer the name from V to this value, setting V's name to |
| 237 | /// empty. It is an error to call V->takeName(V). |
| 238 | void Value::takeName(Value *V) { |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 239 | ValueSymbolTable *ST = 0; |
| 240 | // If this value has a name, drop it. |
| 241 | if (hasName()) { |
| 242 | // Get the symtab this is in. |
| 243 | if (getSymTab(this, ST)) { |
| 244 | // We can't set a name on this value, but we need to clear V's name if |
| 245 | // it has one. |
| 246 | if (V->hasName()) V->setName(0, 0); |
| 247 | return; // Cannot set a name on this value (e.g. constant). |
| 248 | } |
| 249 | |
| 250 | // Remove old name. |
| 251 | if (ST) |
| 252 | ST->removeValueName(Name); |
| 253 | Name->Destroy(); |
| 254 | Name = 0; |
| 255 | } |
| 256 | |
| 257 | // Now we know that this has no name. |
| 258 | |
| 259 | // If V has no name either, we're done. |
| 260 | if (!V->hasName()) return; |
| 261 | |
| 262 | // Get this's symtab if we didn't before. |
| 263 | if (!ST) { |
| 264 | if (getSymTab(this, ST)) { |
| 265 | // Clear V's name. |
| 266 | V->setName(0, 0); |
| 267 | return; // Cannot set a name on this value (e.g. constant). |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Get V's ST, this should always succed, because V has a name. |
| 272 | ValueSymbolTable *VST; |
| 273 | bool Failure = getSymTab(V, VST); |
Chris Lattner | 106b046 | 2008-06-21 19:47:03 +0000 | [diff] [blame] | 274 | assert(!Failure && "V has a name, so it should have a ST!"); Failure=Failure; |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 275 | |
| 276 | // If these values are both in the same symtab, we can do this very fast. |
| 277 | // This works even if both values have no symtab yet. |
| 278 | if (ST == VST) { |
| 279 | // Take the name! |
| 280 | Name = V->Name; |
| 281 | V->Name = 0; |
| 282 | Name->setValue(this); |
Chris Lattner | cba18e3 | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 286 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 287 | // then reinsert it into ST. |
| 288 | |
| 289 | if (VST) |
| 290 | VST->removeValueName(V->Name); |
| 291 | Name = V->Name; |
| 292 | V->Name = 0; |
| 293 | Name->setValue(this); |
| 294 | |
| 295 | if (ST) |
| 296 | ST->reinsertValue(this); |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 300 | // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith, |
| 301 | // except that it doesn't have all of the asserts. The asserts fail because we |
| 302 | // are half-way done resolving types, which causes some types to exist as two |
| 303 | // different Type*'s at the same time. This is a sledgehammer to work around |
| 304 | // this problem. |
| 305 | // |
| 306 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 307 | // Notify all ValueHandles (if present) that this value is going away. |
| 308 | if (HasValueHandle) |
| 309 | ValueHandleBase::ValueIsRAUWd(this, New); |
| 310 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 311 | while (!use_empty()) { |
Gabor Greif | 715b9d2 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 312 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 313 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 314 | // constant because they are uniqued. |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 315 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 316 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 317 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 318 | continue; |
| 319 | } |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 321 | |
| 322 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 326 | void Value::replaceAllUsesWith(Value *New) { |
| 327 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 328 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 329 | assert(New->getType() == getType() && |
| 330 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 332 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 335 | Value *Value::stripPointerCasts() { |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 336 | if (!isa<PointerType>(getType())) |
| 337 | return this; |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 338 | Value *V = this; |
| 339 | do { |
| 340 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 341 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 342 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 343 | if (!CE->getOperand(i)->isNullValue()) |
| 344 | return V; |
| 345 | V = CE->getOperand(0); |
| 346 | } else if (CE->getOpcode() == Instruction::BitCast) { |
| 347 | V = CE->getOperand(0); |
| 348 | } else { |
| 349 | return V; |
| 350 | } |
| 351 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) { |
| 352 | if (!GEP->hasAllZeroIndices()) |
| 353 | return V; |
| 354 | V = GEP->getOperand(0); |
| 355 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) { |
| 356 | V = CI->getOperand(0); |
| 357 | } else { |
| 358 | return V; |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 359 | } |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 360 | assert(isa<PointerType>(V->getType()) && "Unexpected operand type!"); |
| 361 | } while (1); |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 364 | Value *Value::getUnderlyingObject() { |
| 365 | if (!isa<PointerType>(getType())) |
| 366 | return this; |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 367 | Value *V = this; |
Nick Lewycky | 4a7bcf6 | 2009-04-15 06:23:41 +0000 | [diff] [blame] | 368 | unsigned MaxLookup = 6; |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 369 | do { |
| 370 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 371 | if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I)) |
| 372 | return V; |
| 373 | V = I->getOperand(0); |
| 374 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 375 | if (CE->getOpcode() != Instruction::BitCast && |
| 376 | CE->getOpcode() != Instruction::GetElementPtr) |
| 377 | return V; |
| 378 | V = CE->getOperand(0); |
| 379 | } else { |
| 380 | return V; |
| 381 | } |
| 382 | assert(isa<PointerType>(V->getType()) && "Unexpected operand type!"); |
Nick Lewycky | 4a7bcf6 | 2009-04-15 06:23:41 +0000 | [diff] [blame] | 383 | } while (--MaxLookup); |
| 384 | return V; |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 027d726 | 2008-12-02 18:33:11 +0000 | [diff] [blame] | 387 | /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, |
Chris Lattner | 9c1b502 | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 388 | /// return the value in the PHI node corresponding to PredBB. If not, return |
| 389 | /// ourself. This is useful if you want to know the value something has in a |
| 390 | /// predecessor block. |
| 391 | Value *Value::DoPHITranslation(const BasicBlock *CurBB, |
| 392 | const BasicBlock *PredBB) { |
| 393 | PHINode *PN = dyn_cast<PHINode>(this); |
| 394 | if (PN && PN->getParent() == CurBB) |
| 395 | return PN->getIncomingValueForBlock(PredBB); |
| 396 | return this; |
| 397 | } |
| 398 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // ValueHandleBase Class |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
| 403 | /// ValueHandles - This map keeps track of all of the value handles that are |
| 404 | /// watching a Value*. The Value::HasValueHandle bit is used to know whether or |
| 405 | /// not a value has an entry in this map. |
| 406 | typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; |
| 407 | static ManagedStatic<ValueHandlesTy> ValueHandles; |
| 408 | |
Dan Gohman | 3f02595 | 2009-05-04 17:25:21 +0000 | [diff] [blame] | 409 | /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where |
| 410 | /// List is known to point into the existing use list. |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 411 | void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { |
| 412 | assert(List && "Handle list is null?"); |
| 413 | |
| 414 | // Splice ourselves into the list. |
| 415 | Next = *List; |
| 416 | *List = this; |
| 417 | setPrevPtr(List); |
| 418 | if (Next) { |
| 419 | Next->setPrevPtr(&Next); |
| 420 | assert(VP == Next->VP && "Added to wrong list?"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /// AddToUseList - Add this ValueHandle to the use list for VP. |
| 425 | void ValueHandleBase::AddToUseList() { |
| 426 | assert(VP && "Null pointer doesn't have a use list!"); |
| 427 | if (VP->HasValueHandle) { |
| 428 | // If this value already has a ValueHandle, then it must be in the |
| 429 | // ValueHandles map already. |
| 430 | ValueHandleBase *&Entry = (*ValueHandles)[VP]; |
| 431 | assert(Entry != 0 && "Value doesn't have any handles?"); |
| 432 | return AddToExistingUseList(&Entry); |
| 433 | } |
| 434 | |
| 435 | // Ok, it doesn't have any handles yet, so we must insert it into the |
| 436 | // DenseMap. However, doing this insertion could cause the DenseMap to |
| 437 | // reallocate itself, which would invalidate all of the PrevP pointers that |
| 438 | // point into the old table. Handle this by checking for reallocation and |
| 439 | // updating the stale pointers only if needed. |
| 440 | ValueHandlesTy &Handles = *ValueHandles; |
| 441 | const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); |
| 442 | |
| 443 | ValueHandleBase *&Entry = Handles[VP]; |
| 444 | assert(Entry == 0 && "Value really did already have handles?"); |
| 445 | AddToExistingUseList(&Entry); |
Dan Gohman | 3f02595 | 2009-05-04 17:25:21 +0000 | [diff] [blame] | 446 | VP->HasValueHandle = true; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 447 | |
| 448 | // If reallocation didn't happen or if this was the first insertion, don't |
| 449 | // walk the table. |
| 450 | if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || |
| 451 | Handles.size() == 1) |
| 452 | return; |
| 453 | |
| 454 | // Okay, reallocation did happen. Fix the Prev Pointers. |
| 455 | for (ValueHandlesTy::iterator I = Handles.begin(), E = Handles.end(); |
| 456 | I != E; ++I) { |
| 457 | assert(I->second && I->first == I->second->VP && "List invariant broken!"); |
| 458 | I->second->setPrevPtr(&I->second); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /// RemoveFromUseList - Remove this ValueHandle from its current use list. |
| 463 | void ValueHandleBase::RemoveFromUseList() { |
| 464 | assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!"); |
| 465 | |
| 466 | // Unlink this from its use list. |
| 467 | ValueHandleBase **PrevPtr = getPrevPtr(); |
| 468 | assert(*PrevPtr == this && "List invariant broken"); |
| 469 | |
| 470 | *PrevPtr = Next; |
| 471 | if (Next) { |
| 472 | assert(Next->getPrevPtr() == &Next && "List invariant broken"); |
| 473 | Next->setPrevPtr(PrevPtr); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | // If the Next pointer was null, then it is possible that this was the last |
| 478 | // ValueHandle watching VP. If so, delete its entry from the ValueHandles |
| 479 | // map. |
| 480 | ValueHandlesTy &Handles = *ValueHandles; |
| 481 | if (Handles.isPointerIntoBucketsArray(PrevPtr)) { |
| 482 | Handles.erase(VP); |
| 483 | VP->HasValueHandle = false; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
| 488 | void ValueHandleBase::ValueIsDeleted(Value *V) { |
| 489 | assert(V->HasValueHandle && "Should only be called if ValueHandles present"); |
| 490 | |
| 491 | // Get the linked list base, which is guaranteed to exist since the |
| 492 | // HasValueHandle flag is set. |
| 493 | ValueHandleBase *Entry = (*ValueHandles)[V]; |
| 494 | assert(Entry && "Value bit set but no entries exist"); |
| 495 | |
| 496 | while (Entry) { |
| 497 | // Advance pointer to avoid invalidation. |
| 498 | ValueHandleBase *ThisNode = Entry; |
| 499 | Entry = Entry->Next; |
| 500 | |
| 501 | switch (ThisNode->getKind()) { |
| 502 | case Assert: |
| 503 | #ifndef NDEBUG // Only in -g mode... |
| 504 | cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr() |
| 505 | << "\n"; |
| 506 | #endif |
| 507 | cerr << "An asserting value handle still pointed to this value!\n"; |
| 508 | abort(); |
| 509 | case Weak: |
| 510 | // Weak just goes to null, which will unlink it from the list. |
| 511 | ThisNode->operator=(0); |
| 512 | break; |
| 513 | case Callback: |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 514 | // Forward to the subclass's implementation. |
| 515 | static_cast<CallbackVH*>(ThisNode)->deleted(); |
| 516 | break; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | // All callbacks and weak references should be dropped by now. |
| 521 | assert(!V->HasValueHandle && "All references to V were not removed?"); |
| 522 | } |
| 523 | |
| 524 | |
| 525 | void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { |
| 526 | assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); |
| 527 | assert(Old != New && "Changing value into itself!"); |
| 528 | |
| 529 | // Get the linked list base, which is guaranteed to exist since the |
| 530 | // HasValueHandle flag is set. |
| 531 | ValueHandleBase *Entry = (*ValueHandles)[Old]; |
| 532 | assert(Entry && "Value bit set but no entries exist"); |
| 533 | |
| 534 | while (Entry) { |
| 535 | // Advance pointer to avoid invalidation. |
| 536 | ValueHandleBase *ThisNode = Entry; |
| 537 | Entry = Entry->Next; |
| 538 | |
| 539 | switch (ThisNode->getKind()) { |
| 540 | case Assert: |
| 541 | // Asserting handle does not follow RAUW implicitly. |
| 542 | break; |
| 543 | case Weak: |
| 544 | // Weak goes to the new value, which will unlink it from Old's list. |
| 545 | ThisNode->operator=(New); |
| 546 | break; |
| 547 | case Callback: |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 548 | // Forward to the subclass's implementation. |
| 549 | static_cast<CallbackVH*>(ThisNode)->allUsesReplacedWith(New); |
| 550 | break; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 555 | /// ~CallbackVH. Empty, but defined here to avoid emitting the vtable |
| 556 | /// more than once. |
| 557 | CallbackVH::~CallbackVH() {} |
| 558 | |
Chris Lattner | 9c1b502 | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 560 | //===----------------------------------------------------------------------===// |
| 561 | // User Class |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 564 | // replaceUsesOfWith - Replaces all references to the "From" definition with |
| 565 | // references to the "To" definition. |
| 566 | // |
| 567 | void User::replaceUsesOfWith(Value *From, Value *To) { |
| 568 | if (From == To) return; // Duh what? |
| 569 | |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 570 | assert((!isa<Constant>(this) || isa<GlobalValue>(this)) && |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 571 | "Cannot call User::replaceUsesofWith on a constant!"); |
| 572 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 573 | for (unsigned i = 0, E = getNumOperands(); i != E; ++i) |
| 574 | if (getOperand(i) == From) { // Is This operand is pointing to oldval? |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 575 | // The side effects of this setOperand call include linking to |
| 576 | // "To", adding "this" to the uses list of To, and |
| 577 | // most importantly, removing "this" from the use list of "From". |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 578 | setOperand(i, To); // Fix it now... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 579 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 580 | } |
Nate Begeman | 60f9320 | 2008-05-15 01:23:11 +0000 | [diff] [blame] | 581 | |