Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Value.cpp - Implement the Value class -----------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 10 | // This file implements the Value, ValueHandle, and User classes. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Value.h" |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constant.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/InstrTypes.h" |
| 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Operator.h" |
| 25 | #include "llvm/IR/ValueSymbolTable.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ManagedStatic.h" |
| 31 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // Value Class |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 39 | static inline Type *checkType(Type *Ty) { |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 40 | assert(Ty && "Value defined with a null type: Error!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 41 | return const_cast<Type*>(Ty); |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 44 | Value::Value(Type *ty, unsigned scid) |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 45 | : SubclassID(scid), HasValueHandle(0), |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 46 | SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)), |
Gabor Greif | 6f42665 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 47 | UseList(0), Name(0) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 48 | // FIXME: Why isn't this in the subclass gunk?? |
Richard Smith | 488fdce | 2012-12-20 04:11:02 +0000 | [diff] [blame] | 49 | // Note, we cannot call isa<CallInst> before the CallInst has been |
| 50 | // constructed. |
| 51 | if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 52 | assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && |
| 53 | "invalid CallInst type!"); |
Richard Smith | 488fdce | 2012-12-20 04:11:02 +0000 | [diff] [blame] | 54 | else if (SubclassID != BasicBlockVal && |
| 55 | (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal)) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 56 | assert((VTy->isFirstClassType() || VTy->isVoidTy()) && |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 57 | "Cannot create non-first-class values except for constants!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 60 | Value::~Value() { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 61 | // Notify all ValueHandles (if present) that this value is going away. |
| 62 | if (HasValueHandle) |
| 63 | ValueHandleBase::ValueIsDeleted(this); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 66 | // Check to make sure that there are no uses of this value that are still |
| 67 | // around when the value is destroyed. If there are, then we have a dangling |
| 68 | // reference and something is wrong. This code is here to print out what is |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 69 | // still being referenced. The value in question should be printed as |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 70 | // a <badref> |
| 71 | // |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 72 | if (!use_empty()) { |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 73 | dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 74 | for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
David Greene | 6892c7a | 2010-01-05 01:30:09 +0000 | [diff] [blame] | 75 | dbgs() << "Use still stuck around after Def is destroyed:" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 76 | << **I << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | } |
| 78 | #endif |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 79 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 4f95d2b | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 80 | |
| 81 | // If this value is named, destroy the name. This should not be in a symtab |
| 82 | // at this point. |
Bill Wendling | 3ecb447 | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 83 | if (Name && SubclassID != MDStringVal) |
Chris Lattner | 4f95d2b | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 84 | Name->Destroy(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 4f95d2b | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 86 | // There should be no uses of this object anymore, remove it. |
| 87 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 90 | /// hasNUses - Return true if this Value has exactly N users. |
| 91 | /// |
| 92 | bool Value::hasNUses(unsigned N) const { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 93 | const_use_iterator UI = use_begin(), E = use_end(); |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 94 | |
| 95 | for (; N; --N, ++UI) |
| 96 | if (UI == E) return false; // Too few. |
| 97 | return UI == E; |
| 98 | } |
| 99 | |
Chris Lattner | 8daf056 | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 100 | /// hasNUsesOrMore - Return true if this value has N users or more. This is |
| 101 | /// logically equivalent to getNumUses() >= N. |
| 102 | /// |
| 103 | bool Value::hasNUsesOrMore(unsigned N) const { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 104 | const_use_iterator UI = use_begin(), E = use_end(); |
Chris Lattner | 8daf056 | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 105 | |
| 106 | for (; N; --N, ++UI) |
| 107 | if (UI == E) return false; // Too few. |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Evan Cheng | 502a4f5 | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 112 | /// isUsedInBasicBlock - Return true if this value is used in the specified |
| 113 | /// basic block. |
Bill Wendling | c4f72dd | 2008-09-25 22:42:01 +0000 | [diff] [blame] | 114 | bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { |
Benjamin Kramer | 4da7f90 | 2011-12-05 17:23:27 +0000 | [diff] [blame] | 115 | // Start by scanning over the instructions looking for a use before we start |
| 116 | // the expensive use iteration. |
| 117 | unsigned MaxBlockSize = 3; |
| 118 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 119 | if (std::find(I->op_begin(), I->op_end(), this) != I->op_end()) |
| 120 | return true; |
| 121 | if (MaxBlockSize-- == 0) // If the block is larger fall back to use_iterator |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (MaxBlockSize != 0) // We scanned the entire block and found no use. |
| 126 | return false; |
| 127 | |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 128 | for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
Evan Cheng | 502a4f5 | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 129 | const Instruction *User = dyn_cast<Instruction>(*I); |
| 130 | if (User && User->getParent() == BB) |
| 131 | return true; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
Chris Lattner | 8daf056 | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 137 | /// getNumUses - This method computes the number of uses of this Value. This |
| 138 | /// is a linear time operation. Use hasOneUse or hasNUses to check for specific |
| 139 | /// values. |
| 140 | unsigned Value::getNumUses() const { |
| 141 | return (unsigned)std::distance(use_begin(), use_end()); |
| 142 | } |
| 143 | |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 144 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Chris Lattner | ea7acb8 | 2007-02-11 19:12:18 +0000 | [diff] [blame] | 145 | ST = 0; |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 146 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 147 | if (BasicBlock *P = I->getParent()) |
| 148 | if (Function *PP = P->getParent()) |
| 149 | ST = &PP->getValueSymbolTable(); |
| 150 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 151 | if (Function *P = BB->getParent()) |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 152 | ST = &P->getValueSymbolTable(); |
| 153 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 154 | if (Module *P = GV->getParent()) |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 155 | ST = &P->getValueSymbolTable(); |
| 156 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 157 | if (Function *P = A->getParent()) |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 158 | ST = &P->getValueSymbolTable(); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 159 | } else if (isa<MDString>(V)) |
| 160 | return true; |
| 161 | else { |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 162 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 163 | return true; // no name is setable for this. |
| 164 | } |
| 165 | return false; |
| 166 | } |
Chris Lattner | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 167 | |
Daniel Dunbar | 499027f | 2009-07-26 00:51:56 +0000 | [diff] [blame] | 168 | StringRef Value::getName() const { |
Daniel Dunbar | 4fa4990 | 2009-07-26 09:22:02 +0000 | [diff] [blame] | 169 | // Make sure the empty string is still a C string. For historical reasons, |
| 170 | // some clients want to call .data() on the result and expect it to be null |
| 171 | // terminated. |
| 172 | if (!Name) return StringRef("", 0); |
Daniel Dunbar | 499027f | 2009-07-26 00:51:56 +0000 | [diff] [blame] | 173 | return Name->getKey(); |
Chris Lattner | 71996e7 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 176 | void Value::setName(const Twine &NewName) { |
Bill Wendling | 3ecb447 | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 177 | assert(SubclassID != MDStringVal && |
| 178 | "Cannot set the name of MDString with this method!"); |
| 179 | |
Daniel Dunbar | 5149932 | 2009-08-19 23:37:23 +0000 | [diff] [blame] | 180 | // Fast path for common IRBuilder case of setName("") when there is no name. |
| 181 | if (NewName.isTriviallyEmpty() && !hasName()) |
| 182 | return; |
| 183 | |
Daniel Dunbar | e476004 | 2009-08-19 05:08:06 +0000 | [diff] [blame] | 184 | SmallString<256> NameData; |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 185 | StringRef NameRef = NewName.toStringRef(NameData); |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 07f6903 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 187 | // Name isn't changing? |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 188 | if (getName() == NameRef) |
Daniel Dunbar | 07f6903 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 189 | return; |
| 190 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 191 | assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 193 | // Get the symbol table to update for this object. |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 194 | ValueSymbolTable *ST; |
| 195 | if (getSymTab(this, ST)) |
| 196 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | 0d1e407 | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 197 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 198 | if (!ST) { // No symbol table to update? Just do the change. |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 199 | if (NameRef.empty()) { |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 200 | // Free the name for this value. |
| 201 | Name->Destroy(); |
| 202 | Name = 0; |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 203 | return; |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 204 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 205 | |
Daniel Dunbar | 07f6903 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 206 | if (Name) |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 207 | Name->Destroy(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 209 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 210 | // then reallocated. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 212 | // Create the new name. |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 213 | Name = ValueName::Create(NameRef.begin(), NameRef.end()); |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 214 | Name->setValue(this); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 215 | return; |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 216 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 217 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 218 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 219 | // then reallocated. |
| 220 | if (hasName()) { |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 221 | // Remove old name. |
| 222 | ST->removeValueName(Name); |
| 223 | Name->Destroy(); |
| 224 | Name = 0; |
| 225 | |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 226 | if (NameRef.empty()) |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 227 | return; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Name is changing to something new. |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 231 | Name = ST->createValueName(NameRef, this); |
Chris Lattner | 0d1e407 | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 235 | /// takeName - transfer the name from V to this value, setting V's name to |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 236 | /// empty. It is an error to call V->takeName(V). |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 237 | void Value::takeName(Value *V) { |
Bill Wendling | 3ecb447 | 2012-04-10 20:12:16 +0000 | [diff] [blame] | 238 | assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); |
| 239 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 240 | ValueSymbolTable *ST = 0; |
| 241 | // If this value has a name, drop it. |
| 242 | if (hasName()) { |
| 243 | // Get the symtab this is in. |
| 244 | if (getSymTab(this, ST)) { |
| 245 | // We can't set a name on this value, but we need to clear V's name if |
| 246 | // it has one. |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 247 | if (V->hasName()) V->setName(""); |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 248 | return; // Cannot set a name on this value (e.g. constant). |
| 249 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 251 | // Remove old name. |
| 252 | if (ST) |
| 253 | ST->removeValueName(Name); |
| 254 | Name->Destroy(); |
| 255 | Name = 0; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 258 | // Now we know that this has no name. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 260 | // If V has no name either, we're done. |
| 261 | if (!V->hasName()) return; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 263 | // Get this's symtab if we didn't before. |
| 264 | if (!ST) { |
| 265 | if (getSymTab(this, ST)) { |
| 266 | // Clear V's name. |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 267 | V->setName(""); |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 268 | return; // Cannot set a name on this value (e.g. constant). |
| 269 | } |
| 270 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 272 | // Get V's ST, this should always succed, because V has a name. |
| 273 | ValueSymbolTable *VST; |
| 274 | bool Failure = getSymTab(V, VST); |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 275 | assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 277 | // If these values are both in the same symtab, we can do this very fast. |
| 278 | // This works even if both values have no symtab yet. |
| 279 | if (ST == VST) { |
| 280 | // Take the name! |
| 281 | Name = V->Name; |
| 282 | V->Name = 0; |
| 283 | Name->setValue(this); |
Chris Lattner | f41916e | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 284 | return; |
| 285 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 287 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 288 | // then reinsert it into ST. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 290 | if (VST) |
| 291 | VST->removeValueName(V->Name); |
| 292 | Name = V->Name; |
| 293 | V->Name = 0; |
| 294 | Name->setValue(this); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 296 | if (ST) |
| 297 | ST->reinsertValue(this); |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 301 | void Value::replaceAllUsesWith(Value *New) { |
| 302 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
| 303 | assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); |
| 304 | assert(New->getType() == getType() && |
| 305 | "replaceAllUses of value with new value of different type!"); |
| 306 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 307 | // Notify all ValueHandles (if present) that this value is going away. |
| 308 | if (HasValueHandle) |
| 309 | ValueHandleBase::ValueIsRAUWd(this, New); |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 311 | while (!use_empty()) { |
Gabor Greif | 6f42665 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 312 | Use &U = *UseList; |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 313 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 314 | // constant because they are uniqued. |
Chris Lattner | c3cc71a | 2003-10-16 16:53:07 +0000 | [diff] [blame] | 315 | if (Constant *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 316 | if (!isa<GlobalValue>(C)) { |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 317 | C->replaceUsesOfWithOnConstant(this, New, &U); |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 318 | continue; |
| 319 | } |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 322 | U.set(New); |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 323 | } |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 324 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 325 | if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) |
| 326 | BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 329 | namespace { |
| 330 | // Various metrics for how much to strip off of pointers. |
| 331 | enum PointerStripKind { |
| 332 | PSK_ZeroIndices, |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 333 | PSK_InBoundsConstantIndices, |
| 334 | PSK_InBounds |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | template <PointerStripKind StripKind> |
| 338 | static Value *stripPointerCastsAndOffsets(Value *V) { |
| 339 | if (!V->getType()->isPointerTy()) |
| 340 | return V; |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 341 | |
| 342 | // Even though we don't look through PHI nodes, we could be called on an |
| 343 | // instruction in an unreachable block, which may be on a cycle. |
| 344 | SmallPtrSet<Value *, 4> Visited; |
| 345 | |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 346 | Visited.insert(V); |
Duncan Sands | f08bf11 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 347 | do { |
Dan Gohman | 016de81 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 348 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 349 | switch (StripKind) { |
| 350 | case PSK_ZeroIndices: |
| 351 | if (!GEP->hasAllZeroIndices()) |
| 352 | return V; |
| 353 | break; |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 354 | case PSK_InBoundsConstantIndices: |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 355 | if (!GEP->hasAllConstantIndices()) |
| 356 | return V; |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 357 | // fallthrough |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 358 | case PSK_InBounds: |
| 359 | if (!GEP->isInBounds()) |
| 360 | return V; |
| 361 | break; |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 362 | } |
Dan Gohman | 016de81 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 363 | V = GEP->getPointerOperand(); |
| 364 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
| 365 | V = cast<Operator>(V)->getOperand(0); |
Dan Gohman | aac1bfb | 2009-08-27 17:55:13 +0000 | [diff] [blame] | 366 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 367 | if (GA->mayBeOverridden()) |
| 368 | return V; |
| 369 | V = GA->getAliasee(); |
Duncan Sands | f08bf11 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 370 | } else { |
| 371 | return V; |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 372 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 373 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 374 | } while (Visited.insert(V)); |
| 375 | |
| 376 | return V; |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 377 | } |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 378 | } // namespace |
| 379 | |
| 380 | Value *Value::stripPointerCasts() { |
| 381 | return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); |
| 382 | } |
| 383 | |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 384 | Value *Value::stripInBoundsConstantOffsets() { |
| 385 | return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | Value *Value::stripInBoundsOffsets() { |
| 389 | return stripPointerCastsAndOffsets<PSK_InBounds>(this); |
| 390 | } |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 391 | |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 392 | /// isDereferenceablePointer - Test if this value is always a pointer to |
Nick Lewycky | 67b6464 | 2010-11-11 21:51:44 +0000 | [diff] [blame] | 393 | /// allocated and suitably aligned memory for a simple load or store. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 394 | static bool isDereferenceablePointer(const Value *V, |
| 395 | SmallPtrSet<const Value *, 32> &Visited) { |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 396 | // Note that it is not safe to speculate into a malloc'd region because |
| 397 | // malloc may return null. |
| 398 | // It's also not always safe to follow a bitcast, for example: |
| 399 | // bitcast i8* (alloca i8) to i32* |
| 400 | // would result in a 4-byte load from a 1-byte alloca. Some cases could |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 401 | // be handled using DataLayout to check sizes and alignments though. |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 402 | |
| 403 | // These are obviously ok. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 404 | if (isa<AllocaInst>(V)) return true; |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 405 | |
| 406 | // Global variables which can't collapse to null are ok. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 407 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 408 | return !GV->hasExternalWeakLinkage(); |
| 409 | |
Chris Lattner | 3928af6 | 2011-01-23 21:15:29 +0000 | [diff] [blame] | 410 | // byval arguments are ok. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 411 | if (const Argument *A = dyn_cast<Argument>(V)) |
Chris Lattner | 3928af6 | 2011-01-23 21:15:29 +0000 | [diff] [blame] | 412 | return A->hasByValAttr(); |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 413 | |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 414 | // For GEPs, determine if the indexing lands within the allocated object. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 415 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 416 | // Conservatively require that the base pointer be fully dereferenceable. |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 417 | if (!Visited.insert(GEP->getOperand(0))) |
| 418 | return false; |
| 419 | if (!isDereferenceablePointer(GEP->getOperand(0), Visited)) |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 420 | return false; |
| 421 | // Check the indices. |
| 422 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 423 | for (User::const_op_iterator I = GEP->op_begin()+1, |
| 424 | E = GEP->op_end(); I != E; ++I) { |
| 425 | Value *Index = *I; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 426 | Type *Ty = *GTI++; |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 427 | // Struct indices can't be out of bounds. |
| 428 | if (isa<StructType>(Ty)) |
| 429 | continue; |
| 430 | ConstantInt *CI = dyn_cast<ConstantInt>(Index); |
| 431 | if (!CI) |
| 432 | return false; |
| 433 | // Zero is always ok. |
| 434 | if (CI->isZero()) |
| 435 | continue; |
| 436 | // Check to see that it's within the bounds of an array. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 437 | ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
Dan Gohman | 4d70a29 | 2010-11-11 21:23:25 +0000 | [diff] [blame] | 438 | if (!ATy) |
| 439 | return false; |
| 440 | if (CI->getValue().getActiveBits() > 64) |
| 441 | return false; |
| 442 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 443 | return false; |
| 444 | } |
| 445 | // Indices check out; this is dereferenceable. |
| 446 | return true; |
| 447 | } |
| 448 | |
| 449 | // If we don't know, assume the worst. |
| 450 | return false; |
| 451 | } |
| 452 | |
Nick Lewycky | 37abc48 | 2012-01-23 00:05:17 +0000 | [diff] [blame] | 453 | /// isDereferenceablePointer - Test if this value is always a pointer to |
| 454 | /// allocated and suitably aligned memory for a simple load or store. |
| 455 | bool Value::isDereferenceablePointer() const { |
| 456 | SmallPtrSet<const Value *, 32> Visited; |
| 457 | return ::isDereferenceablePointer(this, Visited); |
| 458 | } |
| 459 | |
Chris Lattner | 01c8e02 | 2008-12-02 18:33:11 +0000 | [diff] [blame] | 460 | /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, |
Chris Lattner | c7f7c1d | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 461 | /// return the value in the PHI node corresponding to PredBB. If not, return |
| 462 | /// ourself. This is useful if you want to know the value something has in a |
| 463 | /// predecessor block. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 464 | Value *Value::DoPHITranslation(const BasicBlock *CurBB, |
Chris Lattner | c7f7c1d | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 465 | const BasicBlock *PredBB) { |
| 466 | PHINode *PN = dyn_cast<PHINode>(this); |
| 467 | if (PN && PN->getParent() == CurBB) |
| 468 | return PN->getIncomingValueForBlock(PredBB); |
| 469 | return this; |
| 470 | } |
| 471 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 472 | LLVMContext &Value::getContext() const { return VTy->getContext(); } |
| 473 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 474 | //===----------------------------------------------------------------------===// |
| 475 | // ValueHandleBase Class |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 478 | /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where |
| 479 | /// List is known to point into the existing use list. |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 480 | void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { |
| 481 | assert(List && "Handle list is null?"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 483 | // Splice ourselves into the list. |
| 484 | Next = *List; |
| 485 | *List = this; |
| 486 | setPrevPtr(List); |
| 487 | if (Next) { |
| 488 | Next->setPrevPtr(&Next); |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 489 | assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 493 | void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { |
| 494 | assert(List && "Must insert after existing node"); |
| 495 | |
| 496 | Next = List->Next; |
| 497 | setPrevPtr(&List->Next); |
| 498 | List->Next = this; |
| 499 | if (Next) |
| 500 | Next->setPrevPtr(&Next); |
| 501 | } |
| 502 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 503 | /// AddToUseList - Add this ValueHandle to the use list for VP. |
| 504 | void ValueHandleBase::AddToUseList() { |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 505 | assert(VP.getPointer() && "Null pointer doesn't have a use list!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 506 | |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 507 | LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 508 | |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 509 | if (VP.getPointer()->HasValueHandle) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 510 | // If this value already has a ValueHandle, then it must be in the |
| 511 | // ValueHandles map already. |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 512 | ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 513 | assert(Entry != 0 && "Value doesn't have any handles?"); |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 514 | AddToExistingUseList(&Entry); |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 515 | return; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 516 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 518 | // Ok, it doesn't have any handles yet, so we must insert it into the |
| 519 | // DenseMap. However, doing this insertion could cause the DenseMap to |
| 520 | // reallocate itself, which would invalidate all of the PrevP pointers that |
| 521 | // point into the old table. Handle this by checking for reallocation and |
| 522 | // updating the stale pointers only if needed. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 523 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 524 | const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 525 | |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 526 | ValueHandleBase *&Entry = Handles[VP.getPointer()]; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 527 | assert(Entry == 0 && "Value really did already have handles?"); |
| 528 | AddToExistingUseList(&Entry); |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 529 | VP.getPointer()->HasValueHandle = true; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 531 | // If reallocation didn't happen or if this was the first insertion, don't |
| 532 | // walk the table. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 533 | if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 534 | Handles.size() == 1) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 535 | return; |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 536 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 538 | // Okay, reallocation did happen. Fix the Prev Pointers. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 539 | for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), |
| 540 | E = Handles.end(); I != E; ++I) { |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 541 | assert(I->second && I->first == I->second->VP.getPointer() && |
| 542 | "List invariant broken!"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 543 | I->second->setPrevPtr(&I->second); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /// RemoveFromUseList - Remove this ValueHandle from its current use list. |
| 548 | void ValueHandleBase::RemoveFromUseList() { |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 549 | assert(VP.getPointer() && VP.getPointer()->HasValueHandle && |
| 550 | "Pointer doesn't have a use list!"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 551 | |
| 552 | // Unlink this from its use list. |
| 553 | ValueHandleBase **PrevPtr = getPrevPtr(); |
| 554 | assert(*PrevPtr == this && "List invariant broken"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 556 | *PrevPtr = Next; |
| 557 | if (Next) { |
| 558 | assert(Next->getPrevPtr() == &Next && "List invariant broken"); |
| 559 | Next->setPrevPtr(PrevPtr); |
| 560 | return; |
| 561 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 563 | // If the Next pointer was null, then it is possible that this was the last |
| 564 | // ValueHandle watching VP. If so, delete its entry from the ValueHandles |
| 565 | // map. |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 566 | LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 567 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 568 | if (Handles.isPointerIntoBucketsArray(PrevPtr)) { |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 569 | Handles.erase(VP.getPointer()); |
| 570 | VP.getPointer()->HasValueHandle = false; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | |
| 575 | void ValueHandleBase::ValueIsDeleted(Value *V) { |
| 576 | assert(V->HasValueHandle && "Should only be called if ValueHandles present"); |
| 577 | |
| 578 | // Get the linked list base, which is guaranteed to exist since the |
| 579 | // HasValueHandle flag is set. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 580 | LLVMContextImpl *pImpl = V->getContext().pImpl; |
| 581 | ValueHandleBase *Entry = pImpl->ValueHandles[V]; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 582 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 583 | |
Duncan Sands | ffe8c88 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 584 | // We use a local ValueHandleBase as an iterator so that ValueHandles can add |
| 585 | // and remove themselves from the list without breaking our iteration. This |
| 586 | // is not really an AssertingVH; we just have to give ValueHandleBase a kind. |
| 587 | // Note that we deliberately do not the support the case when dropping a value |
| 588 | // handle results in a new value handle being permanently added to the list |
| 589 | // (as might occur in theory for CallbackVH's): the new value handle will not |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 590 | // be processed and the checking code will mete out righteous punishment if |
Duncan Sands | ffe8c88 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 591 | // the handle is still present once we have finished processing all the other |
| 592 | // value handles (it is fine to momentarily add then remove a value handle). |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 593 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 594 | Iterator.RemoveFromUseList(); |
| 595 | Iterator.AddToExistingUseListAfter(Entry); |
| 596 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 597 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 598 | switch (Entry->getKind()) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 599 | case Assert: |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 600 | break; |
Daniel Dunbar | e5b1836 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 601 | case Tracking: |
| 602 | // Mark that this value has been deleted by setting it to an invalid Value |
| 603 | // pointer. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 604 | Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); |
Daniel Dunbar | e5b1836 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 605 | break; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 606 | case Weak: |
| 607 | // Weak just goes to null, which will unlink it from the list. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 608 | Entry->operator=(0); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 609 | break; |
| 610 | case Callback: |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 611 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 612 | static_cast<CallbackVH*>(Entry)->deleted(); |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 613 | break; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 616 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 617 | // All callbacks, weak references, and assertingVHs should be dropped by now. |
| 618 | if (V->HasValueHandle) { |
| 619 | #ifndef NDEBUG // Only in +Asserts mode... |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 620 | dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 621 | << "\n"; |
| 622 | if (pImpl->ValueHandles[V]->getKind() == Assert) |
| 623 | llvm_unreachable("An asserting value handle still pointed to this" |
| 624 | " value!"); |
| 625 | |
| 626 | #endif |
| 627 | llvm_unreachable("All references to V were not removed?"); |
| 628 | } |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
| 632 | void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { |
| 633 | assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); |
| 634 | assert(Old != New && "Changing value into itself!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 636 | // Get the linked list base, which is guaranteed to exist since the |
| 637 | // HasValueHandle flag is set. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 638 | LLVMContextImpl *pImpl = Old->getContext().pImpl; |
| 639 | ValueHandleBase *Entry = pImpl->ValueHandles[Old]; |
| 640 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 641 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 642 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 643 | // We use a local ValueHandleBase as an iterator so that |
| 644 | // ValueHandles can add and remove themselves from the list without |
| 645 | // breaking our iteration. This is not really an AssertingVH; we |
| 646 | // just have to give ValueHandleBase some kind. |
| 647 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 648 | Iterator.RemoveFromUseList(); |
| 649 | Iterator.AddToExistingUseListAfter(Entry); |
| 650 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 651 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 652 | switch (Entry->getKind()) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 653 | case Assert: |
| 654 | // Asserting handle does not follow RAUW implicitly. |
| 655 | break; |
Daniel Dunbar | e5b1836 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 656 | case Tracking: |
| 657 | // Tracking goes to new value like a WeakVH. Note that this may make it |
| 658 | // something incompatible with its templated type. We don't want to have a |
| 659 | // virtual (or inline) interface to handle this though, so instead we make |
Daniel Dunbar | 460a786 | 2009-09-22 10:30:34 +0000 | [diff] [blame] | 660 | // the TrackingVH accessors guarantee that a client never sees this value. |
Daniel Dunbar | e5b1836 | 2009-09-22 02:02:33 +0000 | [diff] [blame] | 661 | |
| 662 | // FALLTHROUGH |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 663 | case Weak: |
| 664 | // Weak goes to the new value, which will unlink it from Old's list. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 665 | Entry->operator=(New); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 666 | break; |
| 667 | case Callback: |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 668 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 669 | static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 670 | break; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 673 | |
| 674 | #ifndef NDEBUG |
| 675 | // If any new tracking or weak value handles were added while processing the |
| 676 | // list, then complain about it now. |
| 677 | if (Old->HasValueHandle) |
| 678 | for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) |
| 679 | switch (Entry->getKind()) { |
| 680 | case Tracking: |
| 681 | case Weak: |
| 682 | dbgs() << "After RAUW from " << *Old->getType() << " %" |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 683 | << Old->getName() << " to " << *New->getType() << " %" |
| 684 | << New->getName() << "\n"; |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 685 | llvm_unreachable("A tracking or weak value handle still pointed to the" |
| 686 | " old value!\n"); |
| 687 | default: |
| 688 | break; |
| 689 | } |
| 690 | #endif |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Benjamin Kramer | 4e4c340 | 2012-05-19 19:15:25 +0000 | [diff] [blame] | 693 | // Default implementation for CallbackVH. |
| 694 | void CallbackVH::allUsesReplacedWith(Value *) {} |
| 695 | |
| 696 | void CallbackVH::deleted() { |
| 697 | setValPtr(NULL); |
| 698 | } |