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" |
Dmitry Mikulin | 6d3e69e | 2017-11-17 00:30:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constant.h" |
| 20 | #include "llvm/IR/Constants.h" |
Hal Finkel | a739834 | 2014-07-10 05:27:53 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DerivedTypes.h" |
Reid Kleckner | 816047d | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedUser.h" |
Chandler Carruth | bd7cba0 | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/InstrTypes.h" |
| 26 | #include "llvm/IR/Instructions.h" |
Ramkumar Ramachandra | ba59efd | 2015-02-09 21:08:03 +0000 | [diff] [blame] | 27 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
| 29 | #include "llvm/IR/Operator.h" |
Ramkumar Ramachandra | ba59efd | 2015-02-09 21:08:03 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Statepoint.h" |
Chandler Carruth | eb3d76d | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 31 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/ValueSymbolTable.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ManagedStatic.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | |
Serge Guelton | 4c23def | 2018-01-05 19:41:19 +0000 | [diff] [blame] | 41 | static cl::opt<unsigned> NonGlobalValueMaxNameSize( |
| 42 | "non-global-value-max-name-size", cl::Hidden, cl::init(1024), |
| 43 | cl::desc("Maximum size for the name of non-global values.")); |
| 44 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // Value Class |
| 47 | //===----------------------------------------------------------------------===// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 48 | static inline Type *checkType(Type *Ty) { |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 49 | assert(Ty && "Value defined with a null type: Error!"); |
Reid Kleckner | 9de6eef | 2014-06-09 23:32:20 +0000 | [diff] [blame] | 50 | return Ty; |
Chris Lattner | 82d18aa | 2001-12-13 00:41:27 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 53 | Value::Value(Type *ty, unsigned scid) |
Owen Anderson | bd84bdb | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 54 | : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), |
| 55 | HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), |
Pete Cooper | aaa3fa6 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 56 | NumUserOperands(0), IsUsedByMD(false), HasName(false) { |
Serge Guelton | f3d06a2 | 2017-11-13 21:55:01 +0000 | [diff] [blame] | 57 | static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 58 | // FIXME: Why isn't this in the subclass gunk?? |
Richard Smith | 488fdce | 2012-12-20 04:11:02 +0000 | [diff] [blame] | 59 | // Note, we cannot call isa<CallInst> before the CallInst has been |
| 60 | // constructed. |
| 61 | if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 62 | assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && |
| 63 | "invalid CallInst type!"); |
Richard Smith | 488fdce | 2012-12-20 04:11:02 +0000 | [diff] [blame] | 64 | else if (SubclassID != BasicBlockVal && |
Serge Guelton | f3d06a2 | 2017-11-13 21:55:01 +0000 | [diff] [blame] | 65 | (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal)) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 66 | assert((VTy->isFirstClassType() || VTy->isVoidTy()) && |
Chris Lattner | a9e7781 | 2004-07-06 17:44:17 +0000 | [diff] [blame] | 67 | "Cannot create non-first-class values except for constants!"); |
Reid Kleckner | 816047d | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 68 | static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned), |
Reid Kleckner | 4f8c4be | 2016-02-26 18:08:59 +0000 | [diff] [blame] | 69 | "Value too big"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 72 | Value::~Value() { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 73 | // Notify all ValueHandles (if present) that this value is going away. |
| 74 | if (HasValueHandle) |
| 75 | ValueHandleBase::ValueIsDeleted(this); |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 76 | if (isUsedByMetadata()) |
| 77 | ValueAsMetadata::handleDeletion(this); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 80 | // Check to make sure that there are no uses of this value that are still |
| 81 | // around when the value is destroyed. If there are, then we have a dangling |
Andrew Kaylor | dd3224b | 2015-03-10 23:55:38 +0000 | [diff] [blame] | 82 | // reference and something is wrong. This code is here to print out where |
| 83 | // the value is still being referenced. |
Chris Lattner | ee976f3 | 2001-06-11 15:04:40 +0000 | [diff] [blame] | 84 | // |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 85 | if (!use_empty()) { |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 86 | dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; |
Andrew Kaylor | dd3224b | 2015-03-10 23:55:38 +0000 | [diff] [blame] | 87 | for (auto *U : users()) |
| 88 | dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | } |
| 90 | #endif |
Gordon Henriksen | afba8fe66 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 91 | assert(use_empty() && "Uses remain when a value is destroyed!"); |
Chris Lattner | 4f95d2b | 2009-08-04 23:07:12 +0000 | [diff] [blame] | 92 | |
| 93 | // If this value is named, destroy the name. This should not be in a symtab |
| 94 | // at this point. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 95 | destroyValueName(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Reid Kleckner | 816047d | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 98 | void Value::deleteValue() { |
| 99 | switch (getValueID()) { |
| 100 | #define HANDLE_VALUE(Name) \ |
| 101 | case Value::Name##Val: \ |
| 102 | delete static_cast<Name *>(this); \ |
| 103 | break; |
| 104 | #define HANDLE_MEMORY_VALUE(Name) \ |
| 105 | case Value::Name##Val: \ |
| 106 | static_cast<DerivedUser *>(this)->DeleteValue( \ |
| 107 | static_cast<DerivedUser *>(this)); \ |
| 108 | break; |
| 109 | #define HANDLE_INSTRUCTION(Name) /* nothing */ |
| 110 | #include "llvm/IR/Value.def" |
| 111 | |
| 112 | #define HANDLE_INST(N, OPC, CLASS) \ |
| 113 | case Value::InstructionVal + Instruction::OPC: \ |
| 114 | delete static_cast<CLASS *>(this); \ |
| 115 | break; |
| 116 | #define HANDLE_USER_INST(N, OPC, CLASS) |
| 117 | #include "llvm/IR/Instruction.def" |
| 118 | |
| 119 | default: |
| 120 | llvm_unreachable("attempting to delete unknown value kind"); |
| 121 | } |
| 122 | } |
| 123 | |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 124 | void Value::destroyValueName() { |
| 125 | ValueName *Name = getValueName(); |
| 126 | if (Name) |
| 127 | Name->Destroy(); |
| 128 | setValueName(nullptr); |
| 129 | } |
| 130 | |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 131 | bool Value::hasNUses(unsigned N) const { |
Vedant Kumar | 94a229e | 2018-11-19 19:54:27 +0000 | [diff] [blame] | 132 | return hasNItems(use_begin(), use_end(), N); |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 8daf056 | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 135 | bool Value::hasNUsesOrMore(unsigned N) const { |
Vedant Kumar | 94a229e | 2018-11-19 19:54:27 +0000 | [diff] [blame] | 136 | return hasNItemsOrMore(use_begin(), use_end(), N); |
Chris Lattner | 8daf056 | 2005-02-23 16:51:11 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Bill Wendling | c4f72dd | 2008-09-25 22:42:01 +0000 | [diff] [blame] | 139 | bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { |
Jakob Stoklund Olesen | a88d974 | 2013-05-14 23:45:56 +0000 | [diff] [blame] | 140 | // This can be computed either by scanning the instructions in BB, or by |
| 141 | // scanning the use list of this Value. Both lists can be very long, but |
| 142 | // usually one is quite short. |
| 143 | // |
| 144 | // Scan both lists simultaneously until one is exhausted. This limits the |
| 145 | // search to the shorter list. |
| 146 | BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 147 | const_user_iterator UI = user_begin(), UE = user_end(); |
Jakob Stoklund Olesen | a88d974 | 2013-05-14 23:45:56 +0000 | [diff] [blame] | 148 | for (; BI != BE && UI != UE; ++BI, ++UI) { |
| 149 | // Scan basic block: Check if this Value is used by the instruction at BI. |
David Majnemer | 2d62ce6 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 150 | if (is_contained(BI->operands(), this)) |
Benjamin Kramer | 4da7f90 | 2011-12-05 17:23:27 +0000 | [diff] [blame] | 151 | return true; |
Jakob Stoklund Olesen | a88d974 | 2013-05-14 23:45:56 +0000 | [diff] [blame] | 152 | // Scan use list: Check if the use at UI is in BB. |
David Majnemer | 2d62ce6 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 153 | const auto *User = dyn_cast<Instruction>(*UI); |
Evan Cheng | 502a4f5 | 2008-06-12 21:15:59 +0000 | [diff] [blame] | 154 | if (User && User->getParent() == BB) |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
Vedant Kumar | ca6a4d9 | 2018-05-16 23:20:42 +0000 | [diff] [blame] | 160 | unsigned Value::getNumUses() const { |
| 161 | return (unsigned)std::distance(use_begin(), use_end()); |
| 162 | } |
Chris Lattner | 29d1ca6 | 2005-02-01 01:24:21 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 164 | static bool getSymTab(Value *V, ValueSymbolTable *&ST) { |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 165 | ST = nullptr; |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 166 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 167 | if (BasicBlock *P = I->getParent()) |
| 168 | if (Function *PP = P->getParent()) |
Mehdi Amini | d1e3c5a | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 169 | ST = PP->getValueSymbolTable(); |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 170 | } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 171 | if (Function *P = BB->getParent()) |
Mehdi Amini | d1e3c5a | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 172 | ST = P->getValueSymbolTable(); |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 173 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 174 | if (Module *P = GV->getParent()) |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 175 | ST = &P->getValueSymbolTable(); |
| 176 | } else if (Argument *A = dyn_cast<Argument>(V)) { |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 177 | if (Function *P = A->getParent()) |
Mehdi Amini | d1e3c5a | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 178 | ST = P->getValueSymbolTable(); |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 179 | } else { |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 180 | assert(isa<Constant>(V) && "Unknown value type!"); |
| 181 | return true; // no name is setable for this. |
| 182 | } |
| 183 | return false; |
| 184 | } |
Chris Lattner | 28eca8b | 2002-10-09 23:12:59 +0000 | [diff] [blame] | 185 | |
Owen Anderson | bd84bdb | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 186 | ValueName *Value::getValueName() const { |
| 187 | if (!HasName) return nullptr; |
| 188 | |
| 189 | LLVMContext &Ctx = getContext(); |
| 190 | auto I = Ctx.pImpl->ValueNames.find(this); |
| 191 | assert(I != Ctx.pImpl->ValueNames.end() && |
| 192 | "No name entry found!"); |
| 193 | |
| 194 | return I->second; |
| 195 | } |
| 196 | |
| 197 | void Value::setValueName(ValueName *VN) { |
| 198 | LLVMContext &Ctx = getContext(); |
| 199 | |
| 200 | assert(HasName == Ctx.pImpl->ValueNames.count(this) && |
| 201 | "HasName bit out of sync!"); |
| 202 | |
| 203 | if (!VN) { |
| 204 | if (HasName) |
| 205 | Ctx.pImpl->ValueNames.erase(this); |
| 206 | HasName = false; |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | HasName = true; |
| 211 | Ctx.pImpl->ValueNames[this] = VN; |
| 212 | } |
| 213 | |
Daniel Dunbar | 499027f | 2009-07-26 00:51:56 +0000 | [diff] [blame] | 214 | StringRef Value::getName() const { |
Daniel Dunbar | 4fa4990 | 2009-07-26 09:22:02 +0000 | [diff] [blame] | 215 | // Make sure the empty string is still a C string. For historical reasons, |
| 216 | // some clients want to call .data() on the result and expect it to be null |
| 217 | // terminated. |
Owen Anderson | bd84bdb | 2015-06-01 22:24:01 +0000 | [diff] [blame] | 218 | if (!hasName()) |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 219 | return StringRef("", 0); |
| 220 | return getValueName()->getKey(); |
Chris Lattner | 71996e7 | 2007-08-10 15:34:35 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Pete Cooper | a641a4a | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 223 | void Value::setNameImpl(const Twine &NewName) { |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 224 | // Fast-path: LLVMContext can be set to strip out non-GlobalValue names |
Mehdi Amini | 64a77d0 | 2016-04-02 03:46:17 +0000 | [diff] [blame] | 225 | if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) |
Mehdi Amini | 2de9927 | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 226 | return; |
| 227 | |
Daniel Dunbar | 5149932 | 2009-08-19 23:37:23 +0000 | [diff] [blame] | 228 | // Fast path for common IRBuilder case of setName("") when there is no name. |
| 229 | if (NewName.isTriviallyEmpty() && !hasName()) |
| 230 | return; |
| 231 | |
Daniel Dunbar | e476004 | 2009-08-19 05:08:06 +0000 | [diff] [blame] | 232 | SmallString<256> NameData; |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 233 | StringRef NameRef = NewName.toStringRef(NameData); |
Rafael Espindola | edeaa64 | 2013-11-19 21:12:39 +0000 | [diff] [blame] | 234 | assert(NameRef.find_first_of(0) == StringRef::npos && |
| 235 | "Null bytes are not allowed in names"); |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 236 | |
Daniel Dunbar | 07f6903 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 237 | // Name isn't changing? |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 238 | if (getName() == NameRef) |
Daniel Dunbar | 07f6903 | 2009-07-26 00:42:33 +0000 | [diff] [blame] | 239 | return; |
| 240 | |
Serge Guelton | 4c23def | 2018-01-05 19:41:19 +0000 | [diff] [blame] | 241 | // Cap the size of non-GlobalValue names. |
| 242 | if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this)) |
| 243 | NameRef = |
| 244 | NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); |
| 245 | |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 246 | assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 248 | // Get the symbol table to update for this object. |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 249 | ValueSymbolTable *ST; |
| 250 | if (getSymTab(this, ST)) |
| 251 | return; // Cannot set a name on this value (e.g. constant). |
Chris Lattner | 0d1e407 | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 252 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 253 | if (!ST) { // No symbol table to update? Just do the change. |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 254 | if (NameRef.empty()) { |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 255 | // Free the name for this value. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 256 | destroyValueName(); |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 257 | return; |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 258 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 260 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 261 | // then reallocated. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 262 | destroyValueName(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 264 | // Create the new name. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 265 | setValueName(ValueName::Create(NameRef)); |
| 266 | getValueName()->setValue(this); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 267 | return; |
Chris Lattner | 04cb800 | 2005-03-06 02:14:28 +0000 | [diff] [blame] | 268 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 269 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 270 | // NOTE: Could optimize for the case the name is shrinking to not deallocate |
| 271 | // then reallocated. |
| 272 | if (hasName()) { |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 273 | // Remove old name. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 274 | ST->removeValueName(getValueName()); |
| 275 | destroyValueName(); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 276 | |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 277 | if (NameRef.empty()) |
Chris Lattner | 042ad36 | 2007-02-12 18:52:59 +0000 | [diff] [blame] | 278 | return; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Name is changing to something new. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 282 | setValueName(ST->createValueName(NameRef, this)); |
Chris Lattner | 0d1e407 | 2005-03-05 19:51:50 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Pete Cooper | a641a4a | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 285 | void Value::setName(const Twine &NewName) { |
| 286 | setNameImpl(NewName); |
| 287 | if (Function *F = dyn_cast<Function>(this)) |
| 288 | F->recalculateIntrinsicID(); |
| 289 | } |
| 290 | |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 291 | void Value::takeName(Value *V) { |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 292 | ValueSymbolTable *ST = nullptr; |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 293 | // If this value has a name, drop it. |
| 294 | if (hasName()) { |
| 295 | // Get the symtab this is in. |
| 296 | if (getSymTab(this, ST)) { |
| 297 | // We can't set a name on this value, but we need to clear V's name if |
| 298 | // it has one. |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 299 | if (V->hasName()) V->setName(""); |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 300 | return; // Cannot set a name on this value (e.g. constant). |
| 301 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 303 | // Remove old name. |
| 304 | if (ST) |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 305 | ST->removeValueName(getValueName()); |
| 306 | destroyValueName(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 309 | // Now we know that this has no name. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 311 | // If V has no name either, we're done. |
| 312 | if (!V->hasName()) return; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 314 | // Get this's symtab if we didn't before. |
| 315 | if (!ST) { |
| 316 | if (getSymTab(this, ST)) { |
| 317 | // Clear V's name. |
Daniel Dunbar | 3f53fa9 | 2009-07-26 00:34:27 +0000 | [diff] [blame] | 318 | V->setName(""); |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 319 | return; // Cannot set a name on this value (e.g. constant). |
| 320 | } |
| 321 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 323 | // Get V's ST, this should always succed, because V has a name. |
| 324 | ValueSymbolTable *VST; |
| 325 | bool Failure = getSymTab(V, VST); |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 326 | 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] | 327 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 328 | // If these values are both in the same symtab, we can do this very fast. |
| 329 | // This works even if both values have no symtab yet. |
| 330 | if (ST == VST) { |
| 331 | // Take the name! |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 332 | setValueName(V->getValueName()); |
| 333 | V->setValueName(nullptr); |
| 334 | getValueName()->setValue(this); |
Chris Lattner | f41916e | 2007-02-11 01:04:09 +0000 | [diff] [blame] | 335 | return; |
| 336 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 338 | // Otherwise, things are slightly more complex. Remove V's name from VST and |
| 339 | // then reinsert it into ST. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 341 | if (VST) |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 342 | VST->removeValueName(V->getValueName()); |
| 343 | setValueName(V->getValueName()); |
| 344 | V->setValueName(nullptr); |
| 345 | getValueName()->setValue(this); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 0878c31 | 2007-02-15 20:01:43 +0000 | [diff] [blame] | 347 | if (ST) |
| 348 | ST->reinsertValue(this); |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Craig Topper | 6ffcfe0 | 2017-01-13 06:26:18 +0000 | [diff] [blame] | 351 | void Value::assertModuleIsMaterializedImpl() const { |
Todd Fiala | 888a66d | 2016-02-03 21:13:23 +0000 | [diff] [blame] | 352 | #ifndef NDEBUG |
Rafael Espindola | 17920c2 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 353 | const GlobalValue *GV = dyn_cast<GlobalValue>(this); |
| 354 | if (!GV) |
| 355 | return; |
| 356 | const Module *M = GV->getParent(); |
| 357 | if (!M) |
| 358 | return; |
| 359 | assert(M->isMaterialized()); |
Todd Fiala | 888a66d | 2016-02-03 21:13:23 +0000 | [diff] [blame] | 360 | #endif |
Rafael Espindola | 17920c2 | 2016-01-15 19:00:20 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Todd Fiala | 888a66d | 2016-02-03 21:13:23 +0000 | [diff] [blame] | 363 | #ifndef NDEBUG |
Craig Topper | 431bdfc | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 364 | static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 365 | Constant *C) { |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 366 | if (!Cache.insert(Expr).second) |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 367 | return false; |
| 368 | |
| 369 | for (auto &O : Expr->operands()) { |
| 370 | if (O == C) |
| 371 | return true; |
| 372 | auto *CE = dyn_cast<ConstantExpr>(O); |
| 373 | if (!CE) |
| 374 | continue; |
| 375 | if (contains(Cache, CE, C)) |
| 376 | return true; |
| 377 | } |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | static bool contains(Value *Expr, Value *V) { |
| 382 | if (Expr == V) |
| 383 | return true; |
| 384 | |
| 385 | auto *C = dyn_cast<Constant>(V); |
| 386 | if (!C) |
| 387 | return false; |
| 388 | |
| 389 | auto *CE = dyn_cast<ConstantExpr>(Expr); |
| 390 | if (!CE) |
| 391 | return false; |
| 392 | |
| 393 | SmallPtrSet<ConstantExpr *, 4> Cache; |
| 394 | return contains(Cache, CE, C); |
| 395 | } |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 396 | #endif // NDEBUG |
Chris Lattner | 7216811 | 2007-02-11 00:37:27 +0000 | [diff] [blame] | 397 | |
JF Bastien | 4afd04a | 2018-08-09 18:28:54 +0000 | [diff] [blame] | 398 | void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 399 | assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 400 | assert(!contains(New, this) && |
| 401 | "this->replaceAllUsesWith(expr(this)) is NOT valid!"); |
Chris Lattner | 678f9e0 | 2011-07-15 06:18:52 +0000 | [diff] [blame] | 402 | assert(New->getType() == getType() && |
| 403 | "replaceAllUses of value with new value of different type!"); |
| 404 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 405 | // Notify all ValueHandles (if present) that this value is going away. |
| 406 | if (HasValueHandle) |
| 407 | ValueHandleBase::ValueIsRAUWd(this, New); |
JF Bastien | 4afd04a | 2018-08-09 18:28:54 +0000 | [diff] [blame] | 408 | if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata()) |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 409 | ValueAsMetadata::handleRAUW(this, New); |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 410 | |
Teresa Johnson | 2140d92 | 2017-12-16 00:18:12 +0000 | [diff] [blame] | 411 | while (!materialized_use_empty()) { |
Gabor Greif | 6f42665 | 2008-09-19 15:13:20 +0000 | [diff] [blame] | 412 | Use &U = *UseList; |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 413 | // Must handle Constants specially, we cannot call replaceUsesOfWith on a |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 414 | // constant because they are uniqued. |
Rafael Espindola | 27c076a | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 415 | if (auto *C = dyn_cast<Constant>(U.getUser())) { |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 416 | if (!isa<GlobalValue>(C)) { |
Mehdi Amini | 4c69696 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 417 | C->handleOperandChange(this, New); |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 418 | continue; |
| 419 | } |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 420 | } |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 2d69133 | 2007-08-21 00:21:07 +0000 | [diff] [blame] | 422 | U.set(New); |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 423 | } |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 424 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 425 | if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) |
| 426 | BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); |
Chris Lattner | 2bc065b | 2003-08-29 05:09:37 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Davide Italiano | 4e51983 | 2016-12-07 21:47:32 +0000 | [diff] [blame] | 429 | void Value::replaceAllUsesWith(Value *New) { |
JF Bastien | 4afd04a | 2018-08-09 18:28:54 +0000 | [diff] [blame] | 430 | doRAUW(New, ReplaceMetadataUses::Yes); |
Davide Italiano | 4e51983 | 2016-12-07 21:47:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void Value::replaceNonMetadataUsesWith(Value *New) { |
JF Bastien | 4afd04a | 2018-08-09 18:28:54 +0000 | [diff] [blame] | 434 | doRAUW(New, ReplaceMetadataUses::No); |
Davide Italiano | 4e51983 | 2016-12-07 21:47:32 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Gerolf Hoflehner | 5182ad5 | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 437 | // Like replaceAllUsesWith except it does not handle constants or basic blocks. |
| 438 | // This routine leaves uses within BB. |
| 439 | void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { |
| 440 | assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); |
| 441 | assert(!contains(New, this) && |
| 442 | "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); |
| 443 | assert(New->getType() == getType() && |
| 444 | "replaceUses of value with new value of different type!"); |
| 445 | assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); |
| 446 | |
| 447 | use_iterator UI = use_begin(), E = use_end(); |
| 448 | for (; UI != E;) { |
| 449 | Use &U = *UI; |
| 450 | ++UI; |
| 451 | auto *Usr = dyn_cast<Instruction>(U.getUser()); |
| 452 | if (Usr && Usr->getParent() == BB) |
| 453 | continue; |
| 454 | U.set(New); |
| 455 | } |
Gerolf Hoflehner | 5182ad5 | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 458 | namespace { |
| 459 | // Various metrics for how much to strip off of pointers. |
| 460 | enum PointerStripKind { |
| 461 | PSK_ZeroIndices, |
Rafael Espindola | eaf1478 | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 462 | PSK_ZeroIndicesAndAliases, |
Piotr Padlewski | 9648b46 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 463 | PSK_ZeroIndicesAndAliasesAndInvariantGroups, |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 464 | PSK_InBoundsConstantIndices, |
| 465 | PSK_InBounds |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | template <PointerStripKind StripKind> |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 469 | static const Value *stripPointerCastsAndOffsets(const Value *V) { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 470 | if (!V->getType()->isPointerTy()) |
| 471 | return V; |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 472 | |
| 473 | // Even though we don't look through PHI nodes, we could be called on an |
| 474 | // instruction in an unreachable block, which may be on a cycle. |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 475 | SmallPtrSet<const Value *, 4> Visited; |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 476 | |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 477 | Visited.insert(V); |
Duncan Sands | f08bf11 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 478 | do { |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 479 | if (auto *GEP = dyn_cast<GEPOperator>(V)) { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 480 | switch (StripKind) { |
Rafael Espindola | eaf1478 | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 481 | case PSK_ZeroIndicesAndAliases: |
Piotr Padlewski | 9648b46 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 482 | case PSK_ZeroIndicesAndAliasesAndInvariantGroups: |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 483 | case PSK_ZeroIndices: |
| 484 | if (!GEP->hasAllZeroIndices()) |
| 485 | return V; |
| 486 | break; |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 487 | case PSK_InBoundsConstantIndices: |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 488 | if (!GEP->hasAllConstantIndices()) |
| 489 | return V; |
Justin Bogner | 6673ea8 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 490 | LLVM_FALLTHROUGH; |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 491 | case PSK_InBounds: |
| 492 | if (!GEP->isInBounds()) |
| 493 | return V; |
| 494 | break; |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 495 | } |
Dan Gohman | 016de81 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 496 | V = GEP->getPointerOperand(); |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 497 | } else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 498 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
Dan Gohman | 016de81 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 499 | V = cast<Operator>(V)->getOperand(0); |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 500 | } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | c9e3e3c | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 501 | if (StripKind == PSK_ZeroIndices || GA->isInterposable()) |
Dan Gohman | aac1bfb | 2009-08-27 17:55:13 +0000 | [diff] [blame] | 502 | return V; |
| 503 | V = GA->getAliasee(); |
Duncan Sands | f08bf11 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 504 | } else { |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 505 | if (const auto *Call = dyn_cast<CallBase>(V)) { |
| 506 | if (const Value *RV = Call->getReturnedArgOperand()) { |
Hal Finkel | 4cb3366 | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 507 | V = RV; |
| 508 | continue; |
| 509 | } |
Piotr Padlewski | 9648b46 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 510 | // The result of launder.invariant.group must alias it's argument, |
Piotr Padlewski | 6986670 | 2017-04-24 19:37:17 +0000 | [diff] [blame] | 511 | // but it can't be marked with returned attribute, that's why it needs |
| 512 | // special case. |
Piotr Padlewski | 9648b46 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 513 | if (StripKind == PSK_ZeroIndicesAndAliasesAndInvariantGroups && |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 514 | (Call->getIntrinsicID() == Intrinsic::launder_invariant_group || |
| 515 | Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) { |
| 516 | V = Call->getArgOperand(0); |
Piotr Padlewski | 6986670 | 2017-04-24 19:37:17 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | } |
Duncan Sands | f08bf11 | 2008-12-29 21:06:19 +0000 | [diff] [blame] | 520 | return V; |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 521 | } |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 522 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 523 | } while (Visited.insert(V).second); |
Dan Gohman | 50f424c | 2010-06-28 21:16:52 +0000 | [diff] [blame] | 524 | |
| 525 | return V; |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 526 | } |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 527 | } // end anonymous namespace |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 528 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 529 | const Value *Value::stripPointerCasts() const { |
Rafael Espindola | eaf1478 | 2013-05-06 01:48:55 +0000 | [diff] [blame] | 530 | return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); |
| 531 | } |
| 532 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 533 | const Value *Value::stripPointerCastsNoFollowAliases() const { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 534 | return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); |
| 535 | } |
| 536 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 537 | const Value *Value::stripInBoundsConstantOffsets() const { |
Chandler Carruth | 274d377 | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 538 | return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Piotr Padlewski | 9648b46 | 2018-05-03 11:03:01 +0000 | [diff] [blame] | 541 | const Value *Value::stripPointerCastsAndInvariantGroups() const { |
| 542 | return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndInvariantGroups>( |
Piotr Padlewski | 6986670 | 2017-04-24 19:37:17 +0000 | [diff] [blame] | 543 | this); |
| 544 | } |
| 545 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 546 | const Value * |
| 547 | Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 548 | APInt &Offset) const { |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 549 | if (!getType()->isPointerTy()) |
| 550 | return this; |
| 551 | |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 552 | assert(Offset.getBitWidth() == DL.getIndexSizeInBits(cast<PointerType>( |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 553 | getType())->getAddressSpace()) && |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 554 | "The offset bit width does not match the DL specification."); |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 555 | |
| 556 | // Even though we don't look through PHI nodes, we could be called on an |
| 557 | // instruction in an unreachable block, which may be on a cycle. |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 558 | SmallPtrSet<const Value *, 4> Visited; |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 559 | Visited.insert(this); |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 560 | const Value *V = this; |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 561 | do { |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 562 | if (auto *GEP = dyn_cast<GEPOperator>(V)) { |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 563 | if (!GEP->isInBounds()) |
| 564 | return V; |
Chandler Carruth | c99a0d8 | 2013-08-25 10:46:39 +0000 | [diff] [blame] | 565 | APInt GEPOffset(Offset); |
| 566 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 567 | return V; |
Chandler Carruth | c99a0d8 | 2013-08-25 10:46:39 +0000 | [diff] [blame] | 568 | Offset = GEPOffset; |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 569 | V = GEP->getPointerOperand(); |
Philip Reames | f29442d | 2015-09-23 19:48:43 +0000 | [diff] [blame] | 570 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 571 | V = cast<Operator>(V)->getOperand(0); |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 572 | } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 573 | V = GA->getAliasee(); |
| 574 | } else { |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 575 | if (const auto *Call = dyn_cast<CallBase>(V)) |
| 576 | if (const Value *RV = Call->getReturnedArgOperand()) { |
Hal Finkel | 4cb3366 | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 577 | V = RV; |
| 578 | continue; |
| 579 | } |
| 580 | |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 581 | return V; |
| 582 | } |
| 583 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 584 | } while (Visited.insert(V).second); |
Chandler Carruth | f73826b | 2013-08-22 11:25:11 +0000 | [diff] [blame] | 585 | |
| 586 | return V; |
| 587 | } |
| 588 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 589 | const Value *Value::stripInBoundsOffsets() const { |
Chandler Carruth | 84dfc32 | 2012-03-10 08:39:09 +0000 | [diff] [blame] | 590 | return stripPointerCastsAndOffsets<PSK_InBounds>(this); |
| 591 | } |
Anton Korobeynikov | 0b12ecf | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 592 | |
Bjorn Steinbrink | 53f8289 | 2017-12-17 21:20:16 +0000 | [diff] [blame] | 593 | uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL, |
Sanjoy Das | ca4fdcc | 2016-06-02 00:52:48 +0000 | [diff] [blame] | 594 | bool &CanBeNull) const { |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 595 | assert(getType()->isPointerTy() && "must be pointer"); |
| 596 | |
Bjorn Steinbrink | 53f8289 | 2017-12-17 21:20:16 +0000 | [diff] [blame] | 597 | uint64_t DerefBytes = 0; |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 598 | CanBeNull = false; |
| 599 | if (const Argument *A = dyn_cast<Argument>(this)) { |
| 600 | DerefBytes = A->getDereferenceableBytes(); |
Bjorn Steinbrink | 9cf2067 | 2017-12-19 08:46:46 +0000 | [diff] [blame] | 601 | if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) { |
Bjorn Steinbrink | b2ce483 | 2017-12-17 02:37:42 +0000 | [diff] [blame] | 602 | Type *PT = cast<PointerType>(A->getType())->getElementType(); |
Bjorn Steinbrink | 9cf2067 | 2017-12-19 08:46:46 +0000 | [diff] [blame] | 603 | if (PT->isSized()) |
| 604 | DerefBytes = DL.getTypeStoreSize(PT); |
Sanjoy Das | ca4fdcc | 2016-06-02 00:52:48 +0000 | [diff] [blame] | 605 | } |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 606 | if (DerefBytes == 0) { |
| 607 | DerefBytes = A->getDereferenceableOrNullBytes(); |
| 608 | CanBeNull = true; |
| 609 | } |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 610 | } else if (const auto *Call = dyn_cast<CallBase>(this)) { |
| 611 | DerefBytes = Call->getDereferenceableBytes(AttributeList::ReturnIndex); |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 612 | if (DerefBytes == 0) { |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 613 | DerefBytes = |
| 614 | Call->getDereferenceableOrNullBytes(AttributeList::ReturnIndex); |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 615 | CanBeNull = true; |
| 616 | } |
| 617 | } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { |
| 618 | if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { |
| 619 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 620 | DerefBytes = CI->getLimitedValue(); |
| 621 | } |
| 622 | if (DerefBytes == 0) { |
| 623 | if (MDNode *MD = |
| 624 | LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { |
| 625 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 626 | DerefBytes = CI->getLimitedValue(); |
| 627 | } |
| 628 | CanBeNull = true; |
| 629 | } |
Sanjoy Das | ca4fdcc | 2016-06-02 00:52:48 +0000 | [diff] [blame] | 630 | } else if (auto *AI = dyn_cast<AllocaInst>(this)) { |
Bjorn Steinbrink | ea96fe1 | 2017-12-20 10:01:30 +0000 | [diff] [blame] | 631 | if (!AI->isArrayAllocation()) { |
| 632 | DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType()); |
Sanjoy Das | ca4fdcc | 2016-06-02 00:52:48 +0000 | [diff] [blame] | 633 | CanBeNull = false; |
| 634 | } |
| 635 | } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { |
| 636 | if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { |
| 637 | // TODO: Don't outright reject hasExternalWeakLinkage but set the |
| 638 | // CanBeNull flag. |
| 639 | DerefBytes = DL.getTypeStoreSize(GV->getValueType()); |
| 640 | CanBeNull = false; |
| 641 | } |
Artur Pilipenko | 25f648c | 2016-04-27 12:51:01 +0000 | [diff] [blame] | 642 | } |
| 643 | return DerefBytes; |
| 644 | } |
| 645 | |
Artur Pilipenko | f5b2749 | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 646 | unsigned Value::getPointerAlignment(const DataLayout &DL) const { |
| 647 | assert(getType()->isPointerTy() && "must be pointer"); |
| 648 | |
| 649 | unsigned Align = 0; |
| 650 | if (auto *GO = dyn_cast<GlobalObject>(this)) { |
Mikhail Maltsev | 90b5ec2 | 2018-04-27 09:12:12 +0000 | [diff] [blame] | 651 | // Don't make any assumptions about function pointer alignment. Some |
| 652 | // targets use the LSBs to store additional information. |
| 653 | if (isa<Function>(GO)) |
| 654 | return 0; |
Artur Pilipenko | f5b2749 | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 655 | Align = GO->getAlignment(); |
| 656 | if (Align == 0) { |
| 657 | if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { |
| 658 | Type *ObjectType = GVar->getValueType(); |
| 659 | if (ObjectType->isSized()) { |
| 660 | // If the object is defined in the current Module, we'll be giving |
| 661 | // it the preferred alignment. Otherwise, we have to assume that it |
| 662 | // may only have the minimum ABI alignment. |
| 663 | if (GVar->isStrongDefinitionForLinker()) |
| 664 | Align = DL.getPreferredAlignment(GVar); |
| 665 | else |
| 666 | Align = DL.getABITypeAlignment(ObjectType); |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } else if (const Argument *A = dyn_cast<Argument>(this)) { |
| 671 | Align = A->getParamAlignment(); |
| 672 | |
| 673 | if (!Align && A->hasStructRetAttr()) { |
| 674 | // An sret parameter has at least the ABI alignment of the return type. |
| 675 | Type *EltTy = cast<PointerType>(A->getType())->getElementType(); |
| 676 | if (EltTy->isSized()) |
| 677 | Align = DL.getABITypeAlignment(EltTy); |
| 678 | } |
Artur Pilipenko | 5f2b68d | 2016-04-27 10:42:29 +0000 | [diff] [blame] | 679 | } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { |
Artur Pilipenko | f5b2749 | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 680 | Align = AI->getAlignment(); |
Artur Pilipenko | 5f2b68d | 2016-04-27 10:42:29 +0000 | [diff] [blame] | 681 | if (Align == 0) { |
| 682 | Type *AllocatedType = AI->getAllocatedType(); |
| 683 | if (AllocatedType->isSized()) |
| 684 | Align = DL.getPrefTypeAlignment(AllocatedType); |
| 685 | } |
Chandler Carruth | 9f32dc9 | 2019-01-07 07:31:49 +0000 | [diff] [blame] | 686 | } else if (const auto *Call = dyn_cast<CallBase>(this)) |
| 687 | Align = Call->getAttributes().getRetAlignment(); |
Artur Pilipenko | f5b2749 | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 688 | else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) |
| 689 | if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { |
| 690 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 691 | Align = CI->getLimitedValue(); |
| 692 | } |
| 693 | |
| 694 | return Align; |
| 695 | } |
| 696 | |
Craig Topper | 306a7df | 2017-03-27 05:47:03 +0000 | [diff] [blame] | 697 | const Value *Value::DoPHITranslation(const BasicBlock *CurBB, |
| 698 | const BasicBlock *PredBB) const { |
| 699 | auto *PN = dyn_cast<PHINode>(this); |
Chris Lattner | c7f7c1d | 2008-12-02 07:16:45 +0000 | [diff] [blame] | 700 | if (PN && PN->getParent() == CurBB) |
| 701 | return PN->getIncomingValueForBlock(PredBB); |
| 702 | return this; |
| 703 | } |
| 704 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 705 | LLVMContext &Value::getContext() const { return VTy->getContext(); } |
| 706 | |
Duncan P. N. Exon Smith | 99a43c2 | 2014-08-01 23:28:49 +0000 | [diff] [blame] | 707 | void Value::reverseUseList() { |
| 708 | if (!UseList || !UseList->Next) |
| 709 | // No need to reverse 0 or 1 uses. |
| 710 | return; |
| 711 | |
| 712 | Use *Head = UseList; |
| 713 | Use *Current = UseList->Next; |
| 714 | Head->Next = nullptr; |
| 715 | while (Current) { |
| 716 | Use *Next = Current->Next; |
| 717 | Current->Next = Head; |
| 718 | Head->setPrev(&Current->Next); |
| 719 | Head = Current; |
| 720 | Current = Next; |
| 721 | } |
| 722 | UseList = Head; |
| 723 | Head->setPrev(&UseList); |
| 724 | } |
| 725 | |
Arnold Schwaighofer | df708a5 | 2016-09-10 18:14:54 +0000 | [diff] [blame] | 726 | bool Value::isSwiftError() const { |
| 727 | auto *Arg = dyn_cast<Argument>(this); |
| 728 | if (Arg) |
| 729 | return Arg->hasSwiftErrorAttr(); |
| 730 | auto *Alloca = dyn_cast<AllocaInst>(this); |
| 731 | if (!Alloca) |
| 732 | return false; |
| 733 | return Alloca->isSwiftError(); |
| 734 | } |
| 735 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 736 | //===----------------------------------------------------------------------===// |
| 737 | // ValueHandleBase Class |
| 738 | //===----------------------------------------------------------------------===// |
| 739 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 740 | void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { |
| 741 | assert(List && "Handle list is null?"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 743 | // Splice ourselves into the list. |
| 744 | Next = *List; |
| 745 | *List = this; |
| 746 | setPrevPtr(List); |
| 747 | if (Next) { |
| 748 | Next->setPrevPtr(&Next); |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 749 | assert(getValPtr() == Next->getValPtr() && "Added to wrong list?"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 753 | void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { |
| 754 | assert(List && "Must insert after existing node"); |
| 755 | |
| 756 | Next = List->Next; |
| 757 | setPrevPtr(&List->Next); |
| 758 | List->Next = this; |
| 759 | if (Next) |
| 760 | Next->setPrevPtr(&Next); |
| 761 | } |
| 762 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 763 | void ValueHandleBase::AddToUseList() { |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 764 | assert(getValPtr() && "Null pointer doesn't have a use list!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 765 | |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 766 | LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 767 | |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 768 | if (getValPtr()->HasValueHandle) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 769 | // If this value already has a ValueHandle, then it must be in the |
| 770 | // ValueHandles map already. |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 771 | ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()]; |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 772 | assert(Entry && "Value doesn't have any handles?"); |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 773 | AddToExistingUseList(&Entry); |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 774 | return; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 775 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 777 | // Ok, it doesn't have any handles yet, so we must insert it into the |
| 778 | // DenseMap. However, doing this insertion could cause the DenseMap to |
| 779 | // reallocate itself, which would invalidate all of the PrevP pointers that |
| 780 | // point into the old table. Handle this by checking for reallocation and |
| 781 | // updating the stale pointers only if needed. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 782 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 783 | const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 784 | |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 785 | ValueHandleBase *&Entry = Handles[getValPtr()]; |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 786 | assert(!Entry && "Value really did already have handles?"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 787 | AddToExistingUseList(&Entry); |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 788 | getValPtr()->HasValueHandle = true; |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 790 | // If reallocation didn't happen or if this was the first insertion, don't |
| 791 | // walk the table. |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 792 | if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 793 | Handles.size() == 1) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 794 | return; |
Owen Anderson | f2aac28 | 2009-06-17 17:36:57 +0000 | [diff] [blame] | 795 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 797 | // Okay, reallocation did happen. Fix the Prev Pointers. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 798 | for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), |
| 799 | E = Handles.end(); I != E; ++I) { |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 800 | assert(I->second && I->first == I->second->getValPtr() && |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 801 | "List invariant broken!"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 802 | I->second->setPrevPtr(&I->second); |
| 803 | } |
| 804 | } |
| 805 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 806 | void ValueHandleBase::RemoveFromUseList() { |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 807 | assert(getValPtr() && getValPtr()->HasValueHandle && |
Bill Wendling | 5252c43 | 2012-04-08 10:16:43 +0000 | [diff] [blame] | 808 | "Pointer doesn't have a use list!"); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 809 | |
| 810 | // Unlink this from its use list. |
| 811 | ValueHandleBase **PrevPtr = getPrevPtr(); |
| 812 | assert(*PrevPtr == this && "List invariant broken"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 814 | *PrevPtr = Next; |
| 815 | if (Next) { |
| 816 | assert(Next->getPrevPtr() == &Next && "List invariant broken"); |
| 817 | Next->setPrevPtr(PrevPtr); |
| 818 | return; |
| 819 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 821 | // If the Next pointer was null, then it is possible that this was the last |
| 822 | // ValueHandle watching VP. If so, delete its entry from the ValueHandles |
| 823 | // map. |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 824 | LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 825 | DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 826 | if (Handles.isPointerIntoBucketsArray(PrevPtr)) { |
Sanjoy Das | 9f5bf2cd | 2017-04-27 06:02:18 +0000 | [diff] [blame] | 827 | Handles.erase(getValPtr()); |
| 828 | getValPtr()->HasValueHandle = false; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 832 | void ValueHandleBase::ValueIsDeleted(Value *V) { |
| 833 | assert(V->HasValueHandle && "Should only be called if ValueHandles present"); |
| 834 | |
| 835 | // Get the linked list base, which is guaranteed to exist since the |
| 836 | // HasValueHandle flag is set. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 837 | LLVMContextImpl *pImpl = V->getContext().pImpl; |
| 838 | ValueHandleBase *Entry = pImpl->ValueHandles[V]; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 839 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 840 | |
Duncan Sands | ffe8c88 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 841 | // We use a local ValueHandleBase as an iterator so that ValueHandles can add |
| 842 | // and remove themselves from the list without breaking our iteration. This |
| 843 | // is not really an AssertingVH; we just have to give ValueHandleBase a kind. |
| 844 | // Note that we deliberately do not the support the case when dropping a value |
| 845 | // handle results in a new value handle being permanently added to the list |
| 846 | // (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] | 847 | // be processed and the checking code will mete out righteous punishment if |
Duncan Sands | ffe8c88 | 2010-07-24 12:09:22 +0000 | [diff] [blame] | 848 | // the handle is still present once we have finished processing all the other |
| 849 | // 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] | 850 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 851 | Iterator.RemoveFromUseList(); |
| 852 | Iterator.AddToExistingUseListAfter(Entry); |
| 853 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 854 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 855 | switch (Entry->getKind()) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 856 | case Assert: |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 857 | break; |
Sanjoy Das | 41673c6 | 2017-05-01 17:07:54 +0000 | [diff] [blame] | 858 | case Weak: |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 859 | case WeakTracking: |
Sanjoy Das | 41673c6 | 2017-05-01 17:07:54 +0000 | [diff] [blame] | 860 | // WeakTracking and Weak just go to null, which unlinks them |
| 861 | // from the list. |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 862 | Entry->operator=(nullptr); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 863 | break; |
| 864 | case Callback: |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 865 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 866 | static_cast<CallbackVH*>(Entry)->deleted(); |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 867 | break; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 870 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 871 | // All callbacks, weak references, and assertingVHs should be dropped by now. |
| 872 | if (V->HasValueHandle) { |
| 873 | #ifndef NDEBUG // Only in +Asserts mode... |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 874 | dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 875 | << "\n"; |
| 876 | if (pImpl->ValueHandles[V]->getKind() == Assert) |
| 877 | llvm_unreachable("An asserting value handle still pointed to this" |
| 878 | " value!"); |
| 879 | |
| 880 | #endif |
| 881 | llvm_unreachable("All references to V were not removed?"); |
| 882 | } |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 885 | void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { |
| 886 | assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); |
| 887 | assert(Old != New && "Changing value into itself!"); |
Frederic Riss | 648728d | 2014-10-23 04:08:42 +0000 | [diff] [blame] | 888 | assert(Old->getType() == New->getType() && |
| 889 | "replaceAllUses of value with new value of different type!"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 891 | // Get the linked list base, which is guaranteed to exist since the |
| 892 | // HasValueHandle flag is set. |
Owen Anderson | 4d91943 | 2009-08-18 18:28:58 +0000 | [diff] [blame] | 893 | LLVMContextImpl *pImpl = Old->getContext().pImpl; |
| 894 | ValueHandleBase *Entry = pImpl->ValueHandles[Old]; |
| 895 | |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 896 | assert(Entry && "Value bit set but no entries exist"); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 897 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 898 | // We use a local ValueHandleBase as an iterator so that |
| 899 | // ValueHandles can add and remove themselves from the list without |
| 900 | // breaking our iteration. This is not really an AssertingVH; we |
| 901 | // just have to give ValueHandleBase some kind. |
| 902 | for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { |
| 903 | Iterator.RemoveFromUseList(); |
| 904 | Iterator.AddToExistingUseListAfter(Entry); |
| 905 | assert(Entry->Next == &Iterator && "Loop invariant broken."); |
Daniel Dunbar | ce99a6e | 2009-09-20 04:03:34 +0000 | [diff] [blame] | 906 | |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 907 | switch (Entry->getKind()) { |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 908 | case Assert: |
Sanjoy Das | 41673c6 | 2017-05-01 17:07:54 +0000 | [diff] [blame] | 909 | case Weak: |
| 910 | // Asserting and Weak handles do not follow RAUW implicitly. |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 911 | break; |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 912 | case WeakTracking: |
Sanjoy Das | 263da12 | 2017-04-26 16:37:05 +0000 | [diff] [blame] | 913 | // 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] | 914 | Entry->operator=(New); |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 915 | break; |
| 916 | case Callback: |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 917 | // Forward to the subclass's implementation. |
Jeffrey Yasskin | 6a9291a | 2009-10-12 17:43:32 +0000 | [diff] [blame] | 918 | static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); |
Dan Gohman | c09b12c | 2009-05-02 21:10:48 +0000 | [diff] [blame] | 919 | break; |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 922 | |
| 923 | #ifndef NDEBUG |
Sanjoy Das | 3284bd7 | 2017-05-01 16:28:58 +0000 | [diff] [blame] | 924 | // If any new weak value handles were added while processing the |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 925 | // list, then complain about it now. |
| 926 | if (Old->HasValueHandle) |
| 927 | for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) |
| 928 | switch (Entry->getKind()) { |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 929 | case WeakTracking: |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 930 | dbgs() << "After RAUW from " << *Old->getType() << " %" |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 931 | << Old->getName() << " to " << *New->getType() << " %" |
| 932 | << New->getName() << "\n"; |
Sanjoy Das | 3284bd7 | 2017-05-01 16:28:58 +0000 | [diff] [blame] | 933 | llvm_unreachable( |
Sanjoy Das | 399b4d0 | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 934 | "A weak tracking value handle still pointed to the old value!\n"); |
Duncan Sands | 2c11046 | 2010-07-27 06:53:14 +0000 | [diff] [blame] | 935 | default: |
| 936 | break; |
| 937 | } |
| 938 | #endif |
Chris Lattner | 722272d | 2009-03-31 22:11:05 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Juergen Ributzka | 3543625 | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 941 | // Pin the vtable to this file. |
| 942 | void CallbackVH::anchor() {} |