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