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 | |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 14 | #include "LLVMContextImpl.h" |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 15 | #include "llvm/Constant.h" |
Anton Korobeynikov | 82c02b2 | 2008-05-06 22:52:30 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/InstrTypes.h" |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Dan Gohman | 1d548d8 | 2009-07-17 22:25:10 +0000 | [diff] [blame] | 20 | #include "llvm/Operator.h" |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 22 | #include "llvm/ValueSymbolTable.h" |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Dan Gohman | a826a88 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 25 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/ValueHandle.h" |
| 30 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | // Value Class |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Chris Lattner | 25450e3 | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 38 | static inline const Type *checkType(const Type *Ty) { |
| 39 | assert(Ty && "Value defined with a null type: Error!"); |
| 40 | return Ty; |
| 41 | } |
| 42 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 43 | Value::Value(const Type *ty, unsigned scid) |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 44 | : SubclassID(scid), HasValueHandle(0), |
Benjamin Kramer | 5ff90ed | 2009-09-17 14:51:57 +0000 | [diff] [blame] | 45 | SubclassOptionalData(0), SubclassData(0), VTy(checkType(ty)), |
Gabor Greif | 715b9d2 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 46 | UseList(0), Name(0) { |
Devang Patel | ad582fc | 2008-02-21 02:14:01 +0000 | [diff] [blame] | 47 | if (isa<CallInst>(this) || isa<InvokeInst>(this)) |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 48 | assert((VTy->isFirstClassType() || VTy->isVoidTy() || |
Duncan Sands | cbd43f8 | 2010-02-16 14:50:09 +0000 | [diff] [blame] | 49 | ty->isOpaqueTy() || VTy->isStructTy()) && |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 50 | "invalid CallInst type!"); |
| 51 | else if (!isa<Constant>(this) && !isa<BasicBlock>(this)) |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 52 | assert((VTy->isFirstClassType() || VTy->isVoidTy() || |
Duncan Sands | cbd43f8 | 2010-02-16 14:50:09 +0000 | [diff] [blame] | 53 | ty->isOpaqueTy()) && |
Chris Lattner | bea7247 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 54 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 57 | Value::~Value() { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 58 | // Notify all ValueHandles (if present) that this value is going away. |
| 59 | if (HasValueHandle) |
| 60 | ValueHandleBase::ValueIsDeleted(this); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 63 | // Check to make sure that there are no uses of this value that are still |
| 64 | // around when the value is destroyed. If there are, then we have a dangling |
| 65 | // 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] | 66 | // still being referenced. The value in question should be printed as |
Chris Lattner | 874ddad | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 67 | // a <badref> |
| 68 | // |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 69 | if (!use_empty()) { |
David Greene | 3f907a9 | 2010-01-05 01:30:09 +0000 | [diff] [blame] | 70 | dbgs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n"; |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 71 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
David Greene | 3f907a9 | 2010-01-05 01:30:09 +0000 | [diff] [blame] | 72 | dbgs() << "Use still stuck around after Def is destroyed:" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 73 | << **I << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | } |
| 75 | #endif |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 76 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 694285c | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 77 | |
| 78 | // If this value is named, destroy the name. This should not be in a symtab |
| 79 | // at this point. |
| 80 | if (Name) |
| 81 | Name->Destroy(); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 694285c | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 83 | // There should be no uses of this object anymore, remove it. |
| 84 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 87 | /// hasNUses - Return true if this Value has exactly N users. |
| 88 | /// |
| 89 | bool Value::hasNUses(unsigned N) const { |
Gabor Greif | c78d720 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 90 | const_use_iterator UI = use_begin(), E = use_end(); |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 91 | |
| 92 | for (; N; --N, ++UI) |
| 93 | if (UI == E) return false; // Too few. |
| 94 | return UI == E; |
| 95 | } |
| 96 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 97 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 98 | /// logically equivalent to getNumUses() >= N. |
| 99 | /// |
| 100 | bool Value::hasNUsesOrMore(unsigned N) const { |
Gabor Greif | c78d720 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 101 | const_use_iterator UI = use_begin(), E = use_end(); |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 102 | |
| 103 | for (; N; --N, ++UI) |
| 104 | if (UI == E) return false; // Too few. |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
Evan Cheng | 89553cc | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 109 | /// isUsedInBasicBlock - Return true if this value is used in the specified |
| 110 | /// basic block. |
Bill Wendling | 0c37421 | 2008-09-25 22:42:01 +0000 | [diff] [blame] | 111 | bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { |
Gabor Greif | c78d720 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 112 | for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
Evan Cheng | 89553cc | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 113 | const Instruction *User = dyn_cast<Instruction>(*I); |
| 114 | if (User && User->getParent() == BB) |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
Chris Lattner | d36552f | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 121 | /// getNumUses - This method computes the number of uses of this Value. This |
| 122 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 123 | /// values. |
| 124 | unsigned Value::getNumUses() const { |
| 125 | return (unsigned)std::distance(use_begin(), use_end()); |
| 126 | } |
| 127 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 128 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | 569c8ac | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 129 | ST = 0; |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 130 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 131 | if (BasicBlock *P = I->getParent()) |
| 132 | if (Function *PP = P->getParent()) |
| 133 | ST = &PP->getValueSymbolTable(); |
| 134 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 135 | if (Function *P = BB->getParent()) |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 136 | ST = &P->getValueSymbolTable(); |
| 137 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 138 | if (Module *P = GV->getParent()) |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 139 | ST = &P->getValueSymbolTable(); |
| 140 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 141 | if (Function *P = A->getParent()) |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 142 | ST = &P->getValueSymbolTable(); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 143 | } else if (isa<MDString>(V)) |
| 144 | return true; |
| 145 | else { |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 146 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 147 | return true; // no name is setable for this. |
| 148 | } |
| 149 | return false; |
| 150 | } |
Chris Lattner | 2c6abea | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 3228594 | 2009-07-26 00:51:56 +0000 | [diff] [blame] | 152 | StringRef Value::getName() const { |
Daniel Dunbar | 7cc8f7e | 2009-07-26 09:22:02 +0000 | [diff] [blame] | 153 | // Make sure the empty string is still a C string. For historical reasons, |
| 154 | // some clients want to call .data() on the result and expect it to be null |
| 155 | // terminated. |
| 156 | if (!Name) return StringRef("", 0); |
Daniel Dunbar | 3228594 | 2009-07-26 00:51:56 +0000 | [diff] [blame] | 157 | return Name->getKey(); |
Chris Lattner | 5109a88 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | fd27ed9 | 2007-02-15 18:53:54 +0000 | [diff] [blame] | 160 | std::string Value::getNameStr() const { |
Daniel Dunbar | 987ec56 | 2009-07-26 00:17:14 +0000 | [diff] [blame] | 161 | return getName().str(); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 164 | void Value::setName(const Twine &NewName) { |
Daniel Dunbar | cb13b48 | 2009-08-19 23:37:23 +0000 | [diff] [blame] | 165 | // Fast path for common IRBuilder case of setName("") when there is no name. |
| 166 | if (NewName.isTriviallyEmpty() && !hasName()) |
| 167 | return; |
| 168 | |
Daniel Dunbar | acf0b25 | 2009-08-19 05:08:06 +0000 | [diff] [blame] | 169 | SmallString<256> NameData; |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 170 | StringRef NameRef = NewName.toStringRef(NameData); |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | a32c999 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 172 | // Name isn't changing? |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 173 | if (getName() == NameRef) |
Daniel Dunbar | a32c999 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 174 | return; |
| 175 | |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 176 | assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 177 | |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 178 | // Get the symbol table to update for this object. |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 179 | ValueSymbolTable *ST; |
| 180 | if (getSymTab(this, ST)) |
| 181 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 183 | if (!ST) { // No symbol table to update? Just do the change. |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 184 | if (NameRef.empty()) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 185 | // Free the name for this value. |
| 186 | Name->Destroy(); |
| 187 | Name = 0; |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 188 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 189 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | a32c999 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 191 | if (Name) |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 192 | Name->Destroy(); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 194 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 195 | // then reallocated. |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 197 | // Create the new name. |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 198 | Name = ValueName::Create(NameRef.begin(), NameRef.end()); |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 199 | Name->setValue(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 200 | return; |
Chris Lattner | ffb3778 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 201 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 203 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 204 | // then reallocated. |
| 205 | if (hasName()) { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 206 | // Remove old name. |
| 207 | ST->removeValueName(Name); |
| 208 | Name->Destroy(); |
| 209 | Name = 0; |
| 210 | |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 211 | if (NameRef.empty()) |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 212 | return; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Name is changing to something new. |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 216 | Name = ST->createValueName(NameRef, this); |
Chris Lattner | cdb9bfc | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 1a5de58 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 219 | |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 220 | /// takeName - transfer the name from V to this value, setting V's name to |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 221 | /// empty. It is an error to call V->takeName(V). |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 222 | void Value::takeName(Value *V) { |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 223 | ValueSymbolTable *ST = 0; |
| 224 | // If this value has a name, drop it. |
| 225 | if (hasName()) { |
| 226 | // Get the symtab this is in. |
| 227 | if (getSymTab(this, ST)) { |
| 228 | // We can't set a name on this value, but we need to clear V's name if |
| 229 | // it has one. |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 230 | if (V->hasName()) V->setName(""); |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 231 | return; // Cannot set a name on this value (e.g. constant). |
| 232 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 233 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 234 | // Remove old name. |
| 235 | if (ST) |
| 236 | ST->removeValueName(Name); |
| 237 | Name->Destroy(); |
| 238 | Name = 0; |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 241 | // Now we know that this has no name. |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 242 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 243 | // If V has no name either, we're done. |
| 244 | if (!V->hasName()) return; |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 245 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 246 | // Get this's symtab if we didn't before. |
| 247 | if (!ST) { |
| 248 | if (getSymTab(this, ST)) { |
| 249 | // Clear V's name. |
Daniel Dunbar | d786b51 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 250 | V->setName(""); |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 251 | return; // Cannot set a name on this value (e.g. constant). |
| 252 | } |
| 253 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 254 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 255 | // Get V's ST, this should always succed, because V has a name. |
| 256 | ValueSymbolTable *VST; |
| 257 | bool Failure = getSymTab(V, VST); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 258 | assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 259 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 260 | // If these values are both in the same symtab, we can do this very fast. |
| 261 | // This works even if both values have no symtab yet. |
| 262 | if (ST == VST) { |
| 263 | // Take the name! |
| 264 | Name = V->Name; |
| 265 | V->Name = 0; |
| 266 | Name->setValue(this); |
Chris Lattner | cba18e3 | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 267 | return; |
| 268 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 269 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 270 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 271 | // then reinsert it into ST. |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 272 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 273 | if (VST) |
| 274 | VST->removeValueName(V->Name); |
| 275 | Name = V->Name; |
| 276 | V->Name = 0; |
| 277 | Name->setValue(this); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 278 | |
Chris Lattner | cf835ff | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 279 | if (ST) |
| 280 | ST->reinsertValue(this); |
Chris Lattner | b625082 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 284 | // uncheckedReplaceAllUsesWith - This is exactly the same as replaceAllUsesWith, |
| 285 | // except that it doesn't have all of the asserts. The asserts fail because we |
| 286 | // are half-way done resolving types, which causes some types to exist as two |
| 287 | // different Type*'s at the same time. This is a sledgehammer to work around |
| 288 | // this problem. |
| 289 | // |
| 290 | void Value::uncheckedReplaceAllUsesWith(Value *New) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 291 | // Notify all ValueHandles (if present) that this value is going away. |
| 292 | if (HasValueHandle) |
| 293 | ValueHandleBase::ValueIsRAUWd(this, New); |
Chris Lattner | 2f2aa2b | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 4947e67 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 295 | while (!use_empty()) { |
Gabor Greif | 715b9d2 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 296 | Use &U = *UseList; |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 297 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 298 | // constant because they are uniqued. |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 299 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 300 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 301 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 302 | continue; |
| 303 | } |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 304 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 8e5b2c2 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 306 | U.set(New); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 310 | void Value::replaceAllUsesWith(Value *New) { |
| 311 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 312 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 313 | assert(New->getType() == getType() && |
| 314 | "replaceAllUses of value with new value of different type!"); |
Chris Lattner | 9f15812 | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 079edeb | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 316 | uncheckedReplaceAllUsesWith(New); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 319 | Value *Value::stripPointerCasts() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 320 | if (!getType()->isPointerTy()) |
Duncan Sands | d65a4da | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 321 | return this; |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 322 | |
| 323 | // Even though we don't look through PHI nodes, we could be called on an |
| 324 | // instruction in an unreachable block, which may be on a cycle. |
| 325 | SmallPtrSet<Value *, 4> Visited; |
| 326 | |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 327 | Value *V = this; |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 328 | Visited.insert(V); |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 329 | do { |
Dan Gohman | e1019db | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 330 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 331 | if (!GEP->hasAllZeroIndices()) |
| 332 | return V; |
Dan Gohman | e1019db | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 333 | V = GEP->getPointerOperand(); |
| 334 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
| 335 | V = cast<Operator>(V)->getOperand(0); |
Dan Gohman | 4ce5ade | 2009-08-27 17:55:13 +0000 | [diff] [blame] | 336 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 337 | if (GA->mayBeOverridden()) |
| 338 | return V; |
| 339 | V = GA->getAliasee(); |
Duncan Sands | e59fa78 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 340 | } else { |
| 341 | return V; |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 342 | } |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 343 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
Dan Gohman | 7c34ece | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 344 | } while (Visited.insert(V)); |
| 345 | |
| 346 | return V; |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Dan Gohman | a826a88 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 349 | /// isDereferenceablePointer - Test if this value is always a pointer to |
Nick Lewycky | add50b0 | 2010-11-11 21:51:44 +0000 | [diff] [blame] | 350 | /// allocated and suitably aligned memory for a simple load or store. |
Dan Gohman | a826a88 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 351 | bool Value::isDereferenceablePointer() const { |
| 352 | // Note that it is not safe to speculate into a malloc'd region because |
| 353 | // malloc may return null. |
| 354 | // It's also not always safe to follow a bitcast, for example: |
| 355 | // bitcast i8* (alloca i8) to i32* |
| 356 | // would result in a 4-byte load from a 1-byte alloca. Some cases could |
| 357 | // be handled using TargetData to check sizes and alignments though. |
| 358 | |
| 359 | // These are obviously ok. |
| 360 | if (isa<AllocaInst>(this)) return true; |
| 361 | |
| 362 | // Global variables which can't collapse to null are ok. |
| 363 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) |
| 364 | return !GV->hasExternalWeakLinkage(); |
| 365 | |
Chris Lattner | 9879965 | 2011-01-23 21:15:29 +0000 | [diff] [blame] | 366 | // byval arguments are ok. |
| 367 | if (const Argument *A = dyn_cast<Argument>(this)) |
| 368 | return A->hasByValAttr(); |
| 369 | |
Dan Gohman | a826a88 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 370 | // For GEPs, determine if the indexing lands within the allocated object. |
| 371 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(this)) { |
| 372 | // Conservatively require that the base pointer be fully dereferenceable. |
| 373 | if (!GEP->getOperand(0)->isDereferenceablePointer()) |
| 374 | return false; |
| 375 | // Check the indices. |
| 376 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 377 | for (User::const_op_iterator I = GEP->op_begin()+1, |
| 378 | E = GEP->op_end(); I != E; ++I) { |
| 379 | Value *Index = *I; |
| 380 | const Type *Ty = *GTI++; |
| 381 | // Struct indices can't be out of bounds. |
| 382 | if (isa<StructType>(Ty)) |
| 383 | continue; |
| 384 | ConstantInt *CI = dyn_cast<ConstantInt>(Index); |
| 385 | if (!CI) |
| 386 | return false; |
| 387 | // Zero is always ok. |
| 388 | if (CI->isZero()) |
| 389 | continue; |
| 390 | // Check to see that it's within the bounds of an array. |
| 391 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 392 | if (!ATy) |
| 393 | return false; |
| 394 | if (CI->getValue().getActiveBits() > 64) |
| 395 | return false; |
| 396 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 397 | return false; |
| 398 | } |
| 399 | // Indices check out; this is dereferenceable. |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | // If we don't know, assume the worst. |
| 404 | return false; |
| 405 | } |
| 406 | |
Chris Lattner | 027d726 | 2008-12-02 18:33:11 +0000 | [diff] [blame] | 407 | /// 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] | 408 | /// return the value in the PHI node corresponding to PredBB. If not, return |
| 409 | /// ourself. This is useful if you want to know the value something has in a |
| 410 | /// predecessor block. |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 411 | Value *Value::DoPHITranslation(const BasicBlock *CurBB, |
Chris Lattner | 9c1b502 | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 412 | const BasicBlock *PredBB) { |
| 413 | PHINode *PN = dyn_cast<PHINode>(this); |
| 414 | if (PN && PN->getParent() == CurBB) |
| 415 | return PN->getIncomingValueForBlock(PredBB); |
| 416 | return this; |
| 417 | } |
| 418 | |
Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 419 | LLVMContext &Value::getContext() const { return VTy->getContext(); } |
| 420 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 421 | //===----------------------------------------------------------------------===// |
| 422 | // ValueHandleBase Class |
| 423 | //===----------------------------------------------------------------------===// |
| 424 | |
Dan Gohman | 3f02595 | 2009-05-04 17:25:21 +0000 | [diff] [blame] | 425 | /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where |
| 426 | /// List is known to point into the existing use list. |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 427 | void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { |
| 428 | assert(List && "Handle list is null?"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 430 | // Splice ourselves into the list. |
| 431 | Next = *List; |
| 432 | *List = this; |
| 433 | setPrevPtr(List); |
| 434 | if (Next) { |
| 435 | Next->setPrevPtr(&Next); |
| 436 | assert(VP == Next->VP && "Added to wrong list?"); |
| 437 | } |
| 438 | } |
| 439 | |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 440 | void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { |
| 441 | assert(List && "Must insert after existing node"); |
| 442 | |
| 443 | Next = List->Next; |
| 444 | setPrevPtr(&List->Next); |
| 445 | List->Next = this; |
| 446 | if (Next) |
| 447 | Next->setPrevPtr(&Next); |
| 448 | } |
| 449 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 450 | /// AddToUseList - Add this ValueHandle to the use list for VP. |
| 451 | void ValueHandleBase::AddToUseList() { |
| 452 | assert(VP && "Null pointer doesn't have a use list!"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 453 | |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 454 | LLVMContextImpl *pImpl = VP->getContext().pImpl; |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 456 | if (VP->HasValueHandle) { |
| 457 | // If this value already has a ValueHandle, then it must be in the |
| 458 | // ValueHandles map already. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 459 | ValueHandleBase *&Entry = pImpl->ValueHandles[VP]; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 460 | assert(Entry != 0 && "Value doesn't have any handles?"); |
Owen Anderson | 4b660a8 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 461 | AddToExistingUseList(&Entry); |
Owen Anderson | 4b660a8 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 462 | return; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 463 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 465 | // Ok, it doesn't have any handles yet, so we must insert it into the |
| 466 | // DenseMap. However, doing this insertion could cause the DenseMap to |
| 467 | // reallocate itself, which would invalidate all of the PrevP pointers that |
| 468 | // point into the old table. Handle this by checking for reallocation and |
| 469 | // updating the stale pointers only if needed. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 470 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 471 | const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 473 | ValueHandleBase *&Entry = Handles[VP]; |
| 474 | assert(Entry == 0 && "Value really did already have handles?"); |
| 475 | AddToExistingUseList(&Entry); |
Dan Gohman | 3f02595 | 2009-05-04 17:25:21 +0000 | [diff] [blame] | 476 | VP->HasValueHandle = true; |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 478 | // If reallocation didn't happen or if this was the first insertion, don't |
| 479 | // walk the table. |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 480 | if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || |
Owen Anderson | 4b660a8 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 481 | Handles.size() == 1) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 482 | return; |
Owen Anderson | 4b660a8 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 483 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 485 | // Okay, reallocation did happen. Fix the Prev Pointers. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 486 | for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), |
| 487 | E = Handles.end(); I != E; ++I) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 488 | assert(I->second && I->first == I->second->VP && "List invariant broken!"); |
| 489 | I->second->setPrevPtr(&I->second); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /// RemoveFromUseList - Remove this ValueHandle from its current use list. |
| 494 | void ValueHandleBase::RemoveFromUseList() { |
| 495 | assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!"); |
| 496 | |
| 497 | // Unlink this from its use list. |
| 498 | ValueHandleBase **PrevPtr = getPrevPtr(); |
| 499 | assert(*PrevPtr == this && "List invariant broken"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 500 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 501 | *PrevPtr = Next; |
| 502 | if (Next) { |
| 503 | assert(Next->getPrevPtr() == &Next && "List invariant broken"); |
| 504 | Next->setPrevPtr(PrevPtr); |
| 505 | return; |
| 506 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 508 | // If the Next pointer was null, then it is possible that this was the last |
| 509 | // ValueHandle watching VP. If so, delete its entry from the ValueHandles |
| 510 | // map. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 511 | LLVMContextImpl *pImpl = VP->getContext().pImpl; |
| 512 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 513 | if (Handles.isPointerIntoBucketsArray(PrevPtr)) { |
| 514 | Handles.erase(VP); |
| 515 | VP->HasValueHandle = false; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | void ValueHandleBase::ValueIsDeleted(Value *V) { |
| 521 | assert(V->HasValueHandle && "Should only be called if ValueHandles present"); |
| 522 | |
| 523 | // Get the linked list base, which is guaranteed to exist since the |
| 524 | // HasValueHandle flag is set. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 525 | LLVMContextImpl *pImpl = V->getContext().pImpl; |
| 526 | ValueHandleBase *Entry = pImpl->ValueHandles[V]; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 527 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 528 | |
Duncan Sands | 3bc93c2 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 529 | // We use a local ValueHandleBase as an iterator so that ValueHandles can add |
| 530 | // and remove themselves from the list without breaking our iteration. This |
| 531 | // is not really an AssertingVH; we just have to give ValueHandleBase a kind. |
| 532 | // Note that we deliberately do not the support the case when dropping a value |
| 533 | // handle results in a new value handle being permanently added to the list |
| 534 | // (as might occur in theory for CallbackVH's): the new value handle will not |
Duncan Sands | fd5c832 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 535 | // be processed and the checking code will mete out righteous punishment if |
Duncan Sands | 3bc93c2 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 536 | // the handle is still present once we have finished processing all the other |
| 537 | // value handles (it is fine to momentarily add then remove a value handle). |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 538 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 539 | Iterator.RemoveFromUseList(); |
| 540 | Iterator.AddToExistingUseListAfter(Entry); |
| 541 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 542 | |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 543 | switch (Entry->getKind()) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 544 | case Assert: |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 545 | break; |
Daniel Dunbar | 70d4fb0 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 546 | case Tracking: |
| 547 | // Mark that this value has been deleted by setting it to an invalid Value |
| 548 | // pointer. |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 549 | Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); |
Daniel Dunbar | 70d4fb0 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 550 | break; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 551 | case Weak: |
| 552 | // Weak just goes to null, which will unlink it from the list. |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 553 | Entry->operator=(0); |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 554 | break; |
| 555 | case Callback: |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 556 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 557 | static_cast<CallbackVH*>(Entry)->deleted(); |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 558 | break; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 561 | |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 562 | // All callbacks, weak references, and assertingVHs should be dropped by now. |
| 563 | if (V->HasValueHandle) { |
| 564 | #ifndef NDEBUG // Only in +Asserts mode... |
David Greene | 3f907a9 | 2010-01-05 01:30:09 +0000 | [diff] [blame] | 565 | dbgs() << "While deleting: " << *V->getType() << " %" << V->getNameStr() |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 566 | << "\n"; |
| 567 | if (pImpl->ValueHandles[V]->getKind() == Assert) |
| 568 | llvm_unreachable("An asserting value handle still pointed to this" |
| 569 | " value!"); |
| 570 | |
| 571 | #endif |
| 572 | llvm_unreachable("All references to V were not removed?"); |
| 573 | } |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { |
| 578 | assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); |
| 579 | assert(Old != New && "Changing value into itself!"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 581 | // Get the linked list base, which is guaranteed to exist since the |
| 582 | // HasValueHandle flag is set. |
Owen Anderson | e8f2185 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 583 | LLVMContextImpl *pImpl = Old->getContext().pImpl; |
| 584 | ValueHandleBase *Entry = pImpl->ValueHandles[Old]; |
| 585 | |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 586 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 587 | |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 588 | // We use a local ValueHandleBase as an iterator so that |
| 589 | // ValueHandles can add and remove themselves from the list without |
| 590 | // breaking our iteration. This is not really an AssertingVH; we |
| 591 | // just have to give ValueHandleBase some kind. |
| 592 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 593 | Iterator.RemoveFromUseList(); |
| 594 | Iterator.AddToExistingUseListAfter(Entry); |
| 595 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | 6058b51 | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 596 | |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 597 | switch (Entry->getKind()) { |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 598 | case Assert: |
| 599 | // Asserting handle does not follow RAUW implicitly. |
| 600 | break; |
Daniel Dunbar | 70d4fb0 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 601 | case Tracking: |
| 602 | // Tracking goes to new value like a WeakVH. Note that this may make it |
| 603 | // something incompatible with its templated type. We don't want to have a |
| 604 | // virtual (or inline) interface to handle this though, so instead we make |
Daniel Dunbar | 86707c9 | 2009-09-22 10:30:34 +0000 | [diff] [blame] | 605 | // the TrackingVH accessors guarantee that a client never sees this value. |
Daniel Dunbar | 70d4fb0 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 606 | |
| 607 | // FALLTHROUGH |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 608 | case Weak: |
| 609 | // Weak goes to the new value, which will unlink it from Old's list. |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 610 | Entry->operator=(New); |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 611 | break; |
| 612 | case Callback: |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 613 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 406ac81 | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 614 | static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 615 | break; |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
Duncan Sands | fd5c832 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 618 | |
| 619 | #ifndef NDEBUG |
| 620 | // If any new tracking or weak value handles were added while processing the |
| 621 | // list, then complain about it now. |
| 622 | if (Old->HasValueHandle) |
| 623 | for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) |
| 624 | switch (Entry->getKind()) { |
| 625 | case Tracking: |
| 626 | case Weak: |
| 627 | dbgs() << "After RAUW from " << *Old->getType() << " %" |
| 628 | << Old->getNameStr() << " to " << *New->getType() << " %" |
| 629 | << New->getNameStr() << "\n"; |
| 630 | llvm_unreachable("A tracking or weak value handle still pointed to the" |
| 631 | " old value!\n"); |
| 632 | default: |
| 633 | break; |
| 634 | } |
| 635 | #endif |
Chris Lattner | 90234f3 | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Dan Gohman | 745ad44 | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 638 | /// ~CallbackVH. Empty, but defined here to avoid emitting the vtable |
| 639 | /// more than once. |
| 640 | CallbackVH::~CallbackVH() {} |