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