[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
[llvm-project.git] / llvm / lib / IR / Value.cpp
blobb5a69b9ecdde458748c2d5c7bb2f8657d0336d91
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Value, ValueHandle, and User classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Value.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DerivedUser.h"
23 #include "llvm/IR/GetElementPtrTypeIterator.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/TypedPointerType.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/IR/ValueSymbolTable.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <algorithm>
37 using namespace llvm;
39 cl::opt<bool> UseDerefAtPointSemantics(
40 "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),
41 cl::desc("Deref attributes and metadata infer facts at definition only"));
43 //===----------------------------------------------------------------------===//
44 // Value Class
45 //===----------------------------------------------------------------------===//
46 static inline Type *checkType(Type *Ty) {
47 assert(Ty && "Value defined with a null type: Error!");
48 assert(!isa<TypedPointerType>(Ty->getScalarType()) &&
49 "Cannot have values with typed pointer types");
50 return Ty;
53 Value::Value(Type *ty, unsigned scid)
54 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
55 SubclassData(0), NumUserOperands(0), IsUsedByMD(false), HasName(false),
56 HasMetadata(false), VTy(checkType(ty)), UseList(nullptr) {
57 static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
58 // FIXME: Why isn't this in the subclass gunk??
59 // Note, we cannot call isa<CallInst> before the CallInst has been
60 // constructed.
61 unsigned OpCode = 0;
62 if (SubclassID >= InstructionVal)
63 OpCode = SubclassID - InstructionVal;
64 if (OpCode == Instruction::Call || OpCode == Instruction::Invoke ||
65 OpCode == Instruction::CallBr)
66 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
67 "invalid CallBase type!");
68 else if (SubclassID != BasicBlockVal &&
69 (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))
70 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
71 "Cannot create non-first-class values except for constants!");
72 static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
73 "Value too big");
76 Value::~Value() {
77 // Notify all ValueHandles (if present) that this value is going away.
78 if (HasValueHandle)
79 ValueHandleBase::ValueIsDeleted(this);
80 if (isUsedByMetadata())
81 ValueAsMetadata::handleDeletion(this);
83 // Remove associated metadata from context.
84 if (HasMetadata)
85 clearMetadata();
87 #ifndef NDEBUG // Only in -g mode...
88 // Check to make sure that there are no uses of this value that are still
89 // around when the value is destroyed. If there are, then we have a dangling
90 // reference and something is wrong. This code is here to print out where
91 // the value is still being referenced.
93 // Note that use_empty() cannot be called here, as it eventually downcasts
94 // 'this' to GlobalValue (derived class of Value), but GlobalValue has already
95 // been destructed, so accessing it is UB.
97 if (!materialized_use_empty()) {
98 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
99 for (auto *U : users())
100 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
102 #endif
103 assert(materialized_use_empty() && "Uses remain when a value is destroyed!");
105 // If this value is named, destroy the name. This should not be in a symtab
106 // at this point.
107 destroyValueName();
110 void Value::deleteValue() {
111 switch (getValueID()) {
112 #define HANDLE_VALUE(Name) \
113 case Value::Name##Val: \
114 delete static_cast<Name *>(this); \
115 break;
116 #define HANDLE_MEMORY_VALUE(Name) \
117 case Value::Name##Val: \
118 static_cast<DerivedUser *>(this)->DeleteValue( \
119 static_cast<DerivedUser *>(this)); \
120 break;
121 #define HANDLE_CONSTANT(Name) \
122 case Value::Name##Val: \
123 llvm_unreachable("constants should be destroyed with destroyConstant"); \
124 break;
125 #define HANDLE_INSTRUCTION(Name) /* nothing */
126 #include "llvm/IR/Value.def"
128 #define HANDLE_INST(N, OPC, CLASS) \
129 case Value::InstructionVal + Instruction::OPC: \
130 delete static_cast<CLASS *>(this); \
131 break;
132 #define HANDLE_USER_INST(N, OPC, CLASS)
133 #include "llvm/IR/Instruction.def"
135 default:
136 llvm_unreachable("attempting to delete unknown value kind");
140 void Value::destroyValueName() {
141 ValueName *Name = getValueName();
142 if (Name) {
143 MallocAllocator Allocator;
144 Name->Destroy(Allocator);
146 setValueName(nullptr);
149 bool Value::hasNUses(unsigned N) const {
150 return hasNItems(use_begin(), use_end(), N);
153 bool Value::hasNUsesOrMore(unsigned N) const {
154 return hasNItemsOrMore(use_begin(), use_end(), N);
157 bool Value::hasOneUser() const {
158 if (use_empty())
159 return false;
160 if (hasOneUse())
161 return true;
162 return std::equal(++user_begin(), user_end(), user_begin());
165 static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
167 Use *Value::getSingleUndroppableUse() {
168 Use *Result = nullptr;
169 for (Use &U : uses()) {
170 if (!U.getUser()->isDroppable()) {
171 if (Result)
172 return nullptr;
173 Result = &U;
176 return Result;
179 User *Value::getUniqueUndroppableUser() {
180 User *Result = nullptr;
181 for (auto *U : users()) {
182 if (!U->isDroppable()) {
183 if (Result && Result != U)
184 return nullptr;
185 Result = U;
188 return Result;
191 bool Value::hasNUndroppableUses(unsigned int N) const {
192 return hasNItems(user_begin(), user_end(), N, isUnDroppableUser);
195 bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {
196 return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser);
199 void Value::dropDroppableUses(
200 llvm::function_ref<bool(const Use *)> ShouldDrop) {
201 SmallVector<Use *, 8> ToBeEdited;
202 for (Use &U : uses())
203 if (U.getUser()->isDroppable() && ShouldDrop(&U))
204 ToBeEdited.push_back(&U);
205 for (Use *U : ToBeEdited)
206 dropDroppableUse(*U);
209 void Value::dropDroppableUsesIn(User &Usr) {
210 assert(Usr.isDroppable() && "Expected a droppable user!");
211 for (Use &UsrOp : Usr.operands()) {
212 if (UsrOp.get() == this)
213 dropDroppableUse(UsrOp);
217 void Value::dropDroppableUse(Use &U) {
218 U.removeFromList();
219 if (auto *Assume = dyn_cast<AssumeInst>(U.getUser())) {
220 unsigned OpNo = U.getOperandNo();
221 if (OpNo == 0)
222 U.set(ConstantInt::getTrue(Assume->getContext()));
223 else {
224 U.set(UndefValue::get(U.get()->getType()));
225 CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);
226 BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");
228 return;
231 llvm_unreachable("unkown droppable use");
234 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
235 // This can be computed either by scanning the instructions in BB, or by
236 // scanning the use list of this Value. Both lists can be very long, but
237 // usually one is quite short.
239 // Scan both lists simultaneously until one is exhausted. This limits the
240 // search to the shorter list.
241 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
242 const_user_iterator UI = user_begin(), UE = user_end();
243 for (; BI != BE && UI != UE; ++BI, ++UI) {
244 // Scan basic block: Check if this Value is used by the instruction at BI.
245 if (is_contained(BI->operands(), this))
246 return true;
247 // Scan use list: Check if the use at UI is in BB.
248 const auto *User = dyn_cast<Instruction>(*UI);
249 if (User && User->getParent() == BB)
250 return true;
252 return false;
255 unsigned Value::getNumUses() const {
256 return (unsigned)std::distance(use_begin(), use_end());
259 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
260 ST = nullptr;
261 if (Instruction *I = dyn_cast<Instruction>(V)) {
262 if (BasicBlock *P = I->getParent())
263 if (Function *PP = P->getParent())
264 ST = PP->getValueSymbolTable();
265 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
266 if (Function *P = BB->getParent())
267 ST = P->getValueSymbolTable();
268 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
269 if (Module *P = GV->getParent())
270 ST = &P->getValueSymbolTable();
271 } else if (Argument *A = dyn_cast<Argument>(V)) {
272 if (Function *P = A->getParent())
273 ST = P->getValueSymbolTable();
274 } else {
275 assert(isa<Constant>(V) && "Unknown value type!");
276 return true; // no name is setable for this.
278 return false;
281 ValueName *Value::getValueName() const {
282 if (!HasName) return nullptr;
284 LLVMContext &Ctx = getContext();
285 auto I = Ctx.pImpl->ValueNames.find(this);
286 assert(I != Ctx.pImpl->ValueNames.end() &&
287 "No name entry found!");
289 return I->second;
292 void Value::setValueName(ValueName *VN) {
293 LLVMContext &Ctx = getContext();
295 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
296 "HasName bit out of sync!");
298 if (!VN) {
299 if (HasName)
300 Ctx.pImpl->ValueNames.erase(this);
301 HasName = false;
302 return;
305 HasName = true;
306 Ctx.pImpl->ValueNames[this] = VN;
309 StringRef Value::getName() const {
310 // Make sure the empty string is still a C string. For historical reasons,
311 // some clients want to call .data() on the result and expect it to be null
312 // terminated.
313 if (!hasName())
314 return StringRef("", 0);
315 return getValueName()->getKey();
318 void Value::setNameImpl(const Twine &NewName) {
319 bool NeedNewName =
320 !getContext().shouldDiscardValueNames() || isa<GlobalValue>(this);
322 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
323 // and there is no need to delete the old name.
324 if (!NeedNewName && !hasName())
325 return;
327 // Fast path for common IRBuilder case of setName("") when there is no name.
328 if (NewName.isTriviallyEmpty() && !hasName())
329 return;
331 SmallString<256> NameData;
332 StringRef NameRef = NeedNewName ? NewName.toStringRef(NameData) : "";
333 assert(!NameRef.contains(0) && "Null bytes are not allowed in names");
335 // Name isn't changing?
336 if (getName() == NameRef)
337 return;
339 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
341 // Get the symbol table to update for this object.
342 ValueSymbolTable *ST;
343 if (getSymTab(this, ST))
344 return; // Cannot set a name on this value (e.g. constant).
346 if (!ST) { // No symbol table to update? Just do the change.
347 // NOTE: Could optimize for the case the name is shrinking to not deallocate
348 // then reallocated.
349 destroyValueName();
351 if (!NameRef.empty()) {
352 // Create the new name.
353 assert(NeedNewName);
354 MallocAllocator Allocator;
355 setValueName(ValueName::create(NameRef, Allocator));
356 getValueName()->setValue(this);
358 return;
361 // NOTE: Could optimize for the case the name is shrinking to not deallocate
362 // then reallocated.
363 if (hasName()) {
364 // Remove old name.
365 ST->removeValueName(getValueName());
366 destroyValueName();
368 if (NameRef.empty())
369 return;
372 // Name is changing to something new.
373 assert(NeedNewName);
374 setValueName(ST->createValueName(NameRef, this));
377 void Value::setName(const Twine &NewName) {
378 setNameImpl(NewName);
379 if (Function *F = dyn_cast<Function>(this))
380 F->updateAfterNameChange();
383 void Value::takeName(Value *V) {
384 assert(V != this && "Illegal call to this->takeName(this)!");
385 ValueSymbolTable *ST = nullptr;
386 // If this value has a name, drop it.
387 if (hasName()) {
388 // Get the symtab this is in.
389 if (getSymTab(this, ST)) {
390 // We can't set a name on this value, but we need to clear V's name if
391 // it has one.
392 if (V->hasName()) V->setName("");
393 return; // Cannot set a name on this value (e.g. constant).
396 // Remove old name.
397 if (ST)
398 ST->removeValueName(getValueName());
399 destroyValueName();
402 // Now we know that this has no name.
404 // If V has no name either, we're done.
405 if (!V->hasName()) return;
407 // Get this's symtab if we didn't before.
408 if (!ST) {
409 if (getSymTab(this, ST)) {
410 // Clear V's name.
411 V->setName("");
412 return; // Cannot set a name on this value (e.g. constant).
416 // Get V's ST, this should always succeed, because V has a name.
417 ValueSymbolTable *VST;
418 bool Failure = getSymTab(V, VST);
419 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
421 // If these values are both in the same symtab, we can do this very fast.
422 // This works even if both values have no symtab yet.
423 if (ST == VST) {
424 // Take the name!
425 setValueName(V->getValueName());
426 V->setValueName(nullptr);
427 getValueName()->setValue(this);
428 return;
431 // Otherwise, things are slightly more complex. Remove V's name from VST and
432 // then reinsert it into ST.
434 if (VST)
435 VST->removeValueName(V->getValueName());
436 setValueName(V->getValueName());
437 V->setValueName(nullptr);
438 getValueName()->setValue(this);
440 if (ST)
441 ST->reinsertValue(this);
444 #ifndef NDEBUG
445 std::string Value::getNameOrAsOperand() const {
446 if (!getName().empty())
447 return std::string(getName());
449 std::string BBName;
450 raw_string_ostream OS(BBName);
451 printAsOperand(OS, false);
452 return OS.str();
454 #endif
456 void Value::assertModuleIsMaterializedImpl() const {
457 #ifndef NDEBUG
458 const GlobalValue *GV = dyn_cast<GlobalValue>(this);
459 if (!GV)
460 return;
461 const Module *M = GV->getParent();
462 if (!M)
463 return;
464 assert(M->isMaterialized());
465 #endif
468 #ifndef NDEBUG
469 static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
470 Constant *C) {
471 if (!Cache.insert(Expr).second)
472 return false;
474 for (auto &O : Expr->operands()) {
475 if (O == C)
476 return true;
477 auto *CE = dyn_cast<ConstantExpr>(O);
478 if (!CE)
479 continue;
480 if (contains(Cache, CE, C))
481 return true;
483 return false;
486 static bool contains(Value *Expr, Value *V) {
487 if (Expr == V)
488 return true;
490 auto *C = dyn_cast<Constant>(V);
491 if (!C)
492 return false;
494 auto *CE = dyn_cast<ConstantExpr>(Expr);
495 if (!CE)
496 return false;
498 SmallPtrSet<ConstantExpr *, 4> Cache;
499 return contains(Cache, CE, C);
501 #endif // NDEBUG
503 void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
504 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
505 assert(!contains(New, this) &&
506 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
507 assert(New->getType() == getType() &&
508 "replaceAllUses of value with new value of different type!");
510 // Notify all ValueHandles (if present) that this value is going away.
511 if (HasValueHandle)
512 ValueHandleBase::ValueIsRAUWd(this, New);
513 if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())
514 ValueAsMetadata::handleRAUW(this, New);
516 while (!materialized_use_empty()) {
517 Use &U = *UseList;
518 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
519 // constant because they are uniqued.
520 if (auto *C = dyn_cast<Constant>(U.getUser())) {
521 if (!isa<GlobalValue>(C)) {
522 C->handleOperandChange(this, New);
523 continue;
527 U.set(New);
530 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
531 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
534 void Value::replaceAllUsesWith(Value *New) {
535 doRAUW(New, ReplaceMetadataUses::Yes);
538 void Value::replaceNonMetadataUsesWith(Value *New) {
539 doRAUW(New, ReplaceMetadataUses::No);
542 void Value::replaceUsesWithIf(Value *New,
543 llvm::function_ref<bool(Use &U)> ShouldReplace) {
544 assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
545 assert(New->getType() == getType() &&
546 "replaceUses of value with new value of different type!");
548 SmallVector<TrackingVH<Constant>, 8> Consts;
549 SmallPtrSet<Constant *, 8> Visited;
551 for (Use &U : llvm::make_early_inc_range(uses())) {
552 if (!ShouldReplace(U))
553 continue;
554 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
555 // constant because they are uniqued.
556 if (auto *C = dyn_cast<Constant>(U.getUser())) {
557 if (!isa<GlobalValue>(C)) {
558 if (Visited.insert(C).second)
559 Consts.push_back(TrackingVH<Constant>(C));
560 continue;
563 U.set(New);
566 while (!Consts.empty()) {
567 // FIXME: handleOperandChange() updates all the uses in a given Constant,
568 // not just the one passed to ShouldReplace
569 Consts.pop_back_val()->handleOperandChange(this, New);
573 /// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
574 /// with New.
575 static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {
576 SmallVector<DbgVariableIntrinsic *> DbgUsers;
577 SmallVector<DbgVariableRecord *> DPUsers;
578 findDbgUsers(DbgUsers, V, &DPUsers);
579 for (auto *DVI : DbgUsers) {
580 if (DVI->getParent() != BB)
581 DVI->replaceVariableLocationOp(V, New);
583 for (auto *DVR : DPUsers) {
584 DbgMarker *Marker = DVR->getMarker();
585 if (Marker->getParent() != BB)
586 DVR->replaceVariableLocationOp(V, New);
590 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
591 // This routine leaves uses within BB.
592 void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
593 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
594 assert(!contains(New, this) &&
595 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
596 assert(New->getType() == getType() &&
597 "replaceUses of value with new value of different type!");
598 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
600 replaceDbgUsesOutsideBlock(this, New, BB);
601 replaceUsesWithIf(New, [BB](Use &U) {
602 auto *I = dyn_cast<Instruction>(U.getUser());
603 // Don't replace if it's an instruction in the BB basic block.
604 return !I || I->getParent() != BB;
608 namespace {
609 // Various metrics for how much to strip off of pointers.
610 enum PointerStripKind {
611 PSK_ZeroIndices,
612 PSK_ZeroIndicesAndAliases,
613 PSK_ZeroIndicesSameRepresentation,
614 PSK_ForAliasAnalysis,
615 PSK_InBoundsConstantIndices,
616 PSK_InBounds
619 template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}
621 template <PointerStripKind StripKind>
622 static const Value *stripPointerCastsAndOffsets(
623 const Value *V,
624 function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {
625 if (!V->getType()->isPointerTy())
626 return V;
628 // Even though we don't look through PHI nodes, we could be called on an
629 // instruction in an unreachable block, which may be on a cycle.
630 SmallPtrSet<const Value *, 4> Visited;
632 Visited.insert(V);
633 do {
634 Func(V);
635 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
636 switch (StripKind) {
637 case PSK_ZeroIndices:
638 case PSK_ZeroIndicesAndAliases:
639 case PSK_ZeroIndicesSameRepresentation:
640 case PSK_ForAliasAnalysis:
641 if (!GEP->hasAllZeroIndices())
642 return V;
643 break;
644 case PSK_InBoundsConstantIndices:
645 if (!GEP->hasAllConstantIndices())
646 return V;
647 [[fallthrough]];
648 case PSK_InBounds:
649 if (!GEP->isInBounds())
650 return V;
651 break;
653 V = GEP->getPointerOperand();
654 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
655 Value *NewV = cast<Operator>(V)->getOperand(0);
656 if (!NewV->getType()->isPointerTy())
657 return V;
658 V = NewV;
659 } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
660 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
661 // TODO: If we know an address space cast will not change the
662 // representation we could look through it here as well.
663 V = cast<Operator>(V)->getOperand(0);
664 } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
665 V = cast<GlobalAlias>(V)->getAliasee();
666 } else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&
667 cast<PHINode>(V)->getNumIncomingValues() == 1) {
668 V = cast<PHINode>(V)->getIncomingValue(0);
669 } else {
670 if (const auto *Call = dyn_cast<CallBase>(V)) {
671 if (const Value *RV = Call->getReturnedArgOperand()) {
672 V = RV;
673 continue;
675 // The result of launder.invariant.group must alias it's argument,
676 // but it can't be marked with returned attribute, that's why it needs
677 // special case.
678 if (StripKind == PSK_ForAliasAnalysis &&
679 (Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
680 Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {
681 V = Call->getArgOperand(0);
682 continue;
685 return V;
687 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
688 } while (Visited.insert(V).second);
690 return V;
692 } // end anonymous namespace
694 const Value *Value::stripPointerCasts() const {
695 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
698 const Value *Value::stripPointerCastsAndAliases() const {
699 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
702 const Value *Value::stripPointerCastsSameRepresentation() const {
703 return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);
706 const Value *Value::stripInBoundsConstantOffsets() const {
707 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
710 const Value *Value::stripPointerCastsForAliasAnalysis() const {
711 return stripPointerCastsAndOffsets<PSK_ForAliasAnalysis>(this);
714 const Value *Value::stripAndAccumulateConstantOffsets(
715 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
716 bool AllowInvariantGroup,
717 function_ref<bool(Value &, APInt &)> ExternalAnalysis,
718 bool LookThroughIntToPtr) const {
719 if (!getType()->isPtrOrPtrVectorTy())
720 return this;
722 unsigned BitWidth = Offset.getBitWidth();
723 assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
724 "The offset bit width does not match the DL specification.");
726 // Even though we don't look through PHI nodes, we could be called on an
727 // instruction in an unreachable block, which may be on a cycle.
728 SmallPtrSet<const Value *, 4> Visited;
729 Visited.insert(this);
730 const Value *V = this;
731 do {
732 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
733 // If in-bounds was requested, we do not strip non-in-bounds GEPs.
734 if (!AllowNonInbounds && !GEP->isInBounds())
735 return V;
737 // If one of the values we have visited is an addrspacecast, then
738 // the pointer type of this GEP may be different from the type
739 // of the Ptr parameter which was passed to this function. This
740 // means when we construct GEPOffset, we need to use the size
741 // of GEP's pointer type rather than the size of the original
742 // pointer type.
743 APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
744 if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
745 return V;
747 // Stop traversal if the pointer offset wouldn't fit in the bit-width
748 // provided by the Offset argument. This can happen due to AddrSpaceCast
749 // stripping.
750 if (GEPOffset.getSignificantBits() > BitWidth)
751 return V;
753 // External Analysis can return a result higher/lower than the value
754 // represents. We need to detect overflow/underflow.
755 APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
756 if (!ExternalAnalysis) {
757 Offset += GEPOffsetST;
758 } else {
759 bool Overflow = false;
760 APInt OldOffset = Offset;
761 Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
762 if (Overflow) {
763 Offset = OldOffset;
764 return V;
767 V = GEP->getPointerOperand();
768 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
769 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
770 V = cast<Operator>(V)->getOperand(0);
771 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
772 if (!GA->isInterposable())
773 V = GA->getAliasee();
774 } else if (const auto *Call = dyn_cast<CallBase>(V)) {
775 if (const Value *RV = Call->getReturnedArgOperand())
776 V = RV;
777 if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
778 V = Call->getArgOperand(0);
779 } else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
780 // Try to accumulate across (inttoptr (add (ptrtoint p), off)).
781 if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||
782 Int2Ptr->getOpcode() != Instruction::IntToPtr ||
783 Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
784 return V;
786 auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
787 if (!Add)
788 return V;
790 auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
791 auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
792 if (!Ptr2Int || !CI)
793 return V;
795 Offset += CI->getValue();
796 V = Ptr2Int->getOperand(0);
798 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
799 } while (Visited.insert(V).second);
801 return V;
804 const Value *
805 Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {
806 return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func);
809 bool Value::canBeFreed() const {
810 assert(getType()->isPointerTy());
812 // Cases that can simply never be deallocated
813 // *) Constants aren't allocated per se, thus not deallocated either.
814 if (isa<Constant>(this))
815 return false;
817 // Handle byval/byref/sret/inalloca/preallocated arguments. The storage
818 // lifetime is guaranteed to be longer than the callee's lifetime.
819 if (auto *A = dyn_cast<Argument>(this)) {
820 if (A->hasPointeeInMemoryValueAttr())
821 return false;
822 // A pointer to an object in a function which neither frees, nor can arrange
823 // for another thread to free on its behalf, can not be freed in the scope
824 // of the function. Note that this logic is restricted to memory
825 // allocations in existance before the call; a nofree function *is* allowed
826 // to free memory it allocated.
827 const Function *F = A->getParent();
828 if (F->doesNotFreeMemory() && F->hasNoSync())
829 return false;
832 const Function *F = nullptr;
833 if (auto *I = dyn_cast<Instruction>(this))
834 F = I->getFunction();
835 if (auto *A = dyn_cast<Argument>(this))
836 F = A->getParent();
838 if (!F)
839 return true;
841 // With garbage collection, deallocation typically occurs solely at or after
842 // safepoints. If we're compiling for a collector which uses the
843 // gc.statepoint infrastructure, safepoints aren't explicitly present
844 // in the IR until after lowering from abstract to physical machine model.
845 // The collector could chose to mix explicit deallocation and gc'd objects
846 // which is why we need the explicit opt in on a per collector basis.
847 if (!F->hasGC())
848 return true;
850 const auto &GCName = F->getGC();
851 if (GCName == "statepoint-example") {
852 auto *PT = cast<PointerType>(this->getType());
853 if (PT->getAddressSpace() != 1)
854 // For the sake of this example GC, we arbitrarily pick addrspace(1) as
855 // our GC managed heap. This must match the same check in
856 // RewriteStatepointsForGC (and probably needs better factored.)
857 return true;
859 // It is cheaper to scan for a declaration than to scan for a use in this
860 // function. Note that gc.statepoint is a type overloaded function so the
861 // usual trick of requesting declaration of the intrinsic from the module
862 // doesn't work.
863 for (auto &Fn : *F->getParent())
864 if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)
865 return true;
866 return false;
868 return true;
871 uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
872 bool &CanBeNull,
873 bool &CanBeFreed) const {
874 assert(getType()->isPointerTy() && "must be pointer");
876 uint64_t DerefBytes = 0;
877 CanBeNull = false;
878 CanBeFreed = UseDerefAtPointSemantics && canBeFreed();
879 if (const Argument *A = dyn_cast<Argument>(this)) {
880 DerefBytes = A->getDereferenceableBytes();
881 if (DerefBytes == 0) {
882 // Handle byval/byref/inalloca/preallocated arguments
883 if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {
884 if (ArgMemTy->isSized()) {
885 // FIXME: Why isn't this the type alloc size?
886 DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinValue();
891 if (DerefBytes == 0) {
892 DerefBytes = A->getDereferenceableOrNullBytes();
893 CanBeNull = true;
895 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
896 DerefBytes = Call->getRetDereferenceableBytes();
897 if (DerefBytes == 0) {
898 DerefBytes = Call->getRetDereferenceableOrNullBytes();
899 CanBeNull = true;
901 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
902 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
903 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
904 DerefBytes = CI->getLimitedValue();
906 if (DerefBytes == 0) {
907 if (MDNode *MD =
908 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
909 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
910 DerefBytes = CI->getLimitedValue();
912 CanBeNull = true;
914 } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {
915 if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {
916 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
917 DerefBytes = CI->getLimitedValue();
919 if (DerefBytes == 0) {
920 if (MDNode *MD =
921 IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
922 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
923 DerefBytes = CI->getLimitedValue();
925 CanBeNull = true;
927 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
928 if (!AI->isArrayAllocation()) {
929 DerefBytes =
930 DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinValue();
931 CanBeNull = false;
932 CanBeFreed = false;
934 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
935 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
936 // TODO: Don't outright reject hasExternalWeakLinkage but set the
937 // CanBeNull flag.
938 DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedValue();
939 CanBeNull = false;
940 CanBeFreed = false;
943 return DerefBytes;
946 Align Value::getPointerAlignment(const DataLayout &DL) const {
947 assert(getType()->isPointerTy() && "must be pointer");
948 if (auto *GO = dyn_cast<GlobalObject>(this)) {
949 if (isa<Function>(GO)) {
950 Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
951 switch (DL.getFunctionPtrAlignType()) {
952 case DataLayout::FunctionPtrAlignType::Independent:
953 return FunctionPtrAlign;
954 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
955 return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne());
957 llvm_unreachable("Unhandled FunctionPtrAlignType");
959 const MaybeAlign Alignment(GO->getAlign());
960 if (!Alignment) {
961 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
962 Type *ObjectType = GVar->getValueType();
963 if (ObjectType->isSized()) {
964 // If the object is defined in the current Module, we'll be giving
965 // it the preferred alignment. Otherwise, we have to assume that it
966 // may only have the minimum ABI alignment.
967 if (GVar->isStrongDefinitionForLinker())
968 return DL.getPreferredAlign(GVar);
969 else
970 return DL.getABITypeAlign(ObjectType);
974 return Alignment.valueOrOne();
975 } else if (const Argument *A = dyn_cast<Argument>(this)) {
976 const MaybeAlign Alignment = A->getParamAlign();
977 if (!Alignment && A->hasStructRetAttr()) {
978 // An sret parameter has at least the ABI alignment of the return type.
979 Type *EltTy = A->getParamStructRetType();
980 if (EltTy->isSized())
981 return DL.getABITypeAlign(EltTy);
983 return Alignment.valueOrOne();
984 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
985 return AI->getAlign();
986 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
987 MaybeAlign Alignment = Call->getRetAlign();
988 if (!Alignment && Call->getCalledFunction())
989 Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment();
990 return Alignment.valueOrOne();
991 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
992 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
993 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
994 return Align(CI->getLimitedValue());
996 } else if (auto *CstPtr = dyn_cast<Constant>(this)) {
997 // Strip pointer casts to avoid creating unnecessary ptrtoint expression
998 // if the only "reduction" is combining a bitcast + ptrtoint.
999 CstPtr = CstPtr->stripPointerCasts();
1000 if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
1001 const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
1002 /*OnlyIfReduced=*/true))) {
1003 size_t TrailingZeros = CstInt->getValue().countr_zero();
1004 // While the actual alignment may be large, elsewhere we have
1005 // an arbitrary upper alignmet limit, so let's clamp to it.
1006 return Align(TrailingZeros < Value::MaxAlignmentExponent
1007 ? uint64_t(1) << TrailingZeros
1008 : Value::MaximumAlignment);
1011 return Align(1);
1014 static std::optional<int64_t>
1015 getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
1016 // Skip over the first indices.
1017 gep_type_iterator GTI = gep_type_begin(GEP);
1018 for (unsigned i = 1; i != Idx; ++i, ++GTI)
1019 /*skip along*/;
1021 // Compute the offset implied by the rest of the indices.
1022 int64_t Offset = 0;
1023 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
1024 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
1025 if (!OpC)
1026 return std::nullopt;
1027 if (OpC->isZero())
1028 continue; // No offset.
1030 // Handle struct indices, which add their field offset to the pointer.
1031 if (StructType *STy = GTI.getStructTypeOrNull()) {
1032 Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
1033 continue;
1036 // Otherwise, we have a sequential type like an array or fixed-length
1037 // vector. Multiply the index by the ElementSize.
1038 TypeSize Size = GTI.getSequentialElementStride(DL);
1039 if (Size.isScalable())
1040 return std::nullopt;
1041 Offset += Size.getFixedValue() * OpC->getSExtValue();
1044 return Offset;
1047 std::optional<int64_t> Value::getPointerOffsetFrom(const Value *Other,
1048 const DataLayout &DL) const {
1049 const Value *Ptr1 = Other;
1050 const Value *Ptr2 = this;
1051 APInt Offset1(DL.getIndexTypeSizeInBits(Ptr1->getType()), 0);
1052 APInt Offset2(DL.getIndexTypeSizeInBits(Ptr2->getType()), 0);
1053 Ptr1 = Ptr1->stripAndAccumulateConstantOffsets(DL, Offset1, true);
1054 Ptr2 = Ptr2->stripAndAccumulateConstantOffsets(DL, Offset2, true);
1056 // Handle the trivial case first.
1057 if (Ptr1 == Ptr2)
1058 return Offset2.getSExtValue() - Offset1.getSExtValue();
1060 const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
1061 const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
1063 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
1064 // base. After that base, they may have some number of common (and
1065 // potentially variable) indices. After that they handle some constant
1066 // offset, which determines their offset from each other. At this point, we
1067 // handle no other case.
1068 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
1069 GEP1->getSourceElementType() != GEP2->getSourceElementType())
1070 return std::nullopt;
1072 // Skip any common indices and track the GEP types.
1073 unsigned Idx = 1;
1074 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
1075 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
1076 break;
1078 auto IOffset1 = getOffsetFromIndex(GEP1, Idx, DL);
1079 auto IOffset2 = getOffsetFromIndex(GEP2, Idx, DL);
1080 if (!IOffset1 || !IOffset2)
1081 return std::nullopt;
1082 return *IOffset2 - *IOffset1 + Offset2.getSExtValue() -
1083 Offset1.getSExtValue();
1086 const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
1087 const BasicBlock *PredBB) const {
1088 auto *PN = dyn_cast<PHINode>(this);
1089 if (PN && PN->getParent() == CurBB)
1090 return PN->getIncomingValueForBlock(PredBB);
1091 return this;
1094 LLVMContext &Value::getContext() const { return VTy->getContext(); }
1096 void Value::reverseUseList() {
1097 if (!UseList || !UseList->Next)
1098 // No need to reverse 0 or 1 uses.
1099 return;
1101 Use *Head = UseList;
1102 Use *Current = UseList->Next;
1103 Head->Next = nullptr;
1104 while (Current) {
1105 Use *Next = Current->Next;
1106 Current->Next = Head;
1107 Head->Prev = &Current->Next;
1108 Head = Current;
1109 Current = Next;
1111 UseList = Head;
1112 Head->Prev = &UseList;
1115 bool Value::isSwiftError() const {
1116 auto *Arg = dyn_cast<Argument>(this);
1117 if (Arg)
1118 return Arg->hasSwiftErrorAttr();
1119 auto *Alloca = dyn_cast<AllocaInst>(this);
1120 if (!Alloca)
1121 return false;
1122 return Alloca->isSwiftError();
1125 //===----------------------------------------------------------------------===//
1126 // ValueHandleBase Class
1127 //===----------------------------------------------------------------------===//
1129 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
1130 assert(List && "Handle list is null?");
1132 // Splice ourselves into the list.
1133 Next = *List;
1134 *List = this;
1135 setPrevPtr(List);
1136 if (Next) {
1137 Next->setPrevPtr(&Next);
1138 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
1142 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
1143 assert(List && "Must insert after existing node");
1145 Next = List->Next;
1146 setPrevPtr(&List->Next);
1147 List->Next = this;
1148 if (Next)
1149 Next->setPrevPtr(&Next);
1152 void ValueHandleBase::AddToUseList() {
1153 assert(getValPtr() && "Null pointer doesn't have a use list!");
1155 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1157 if (getValPtr()->HasValueHandle) {
1158 // If this value already has a ValueHandle, then it must be in the
1159 // ValueHandles map already.
1160 ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
1161 assert(Entry && "Value doesn't have any handles?");
1162 AddToExistingUseList(&Entry);
1163 return;
1166 // Ok, it doesn't have any handles yet, so we must insert it into the
1167 // DenseMap. However, doing this insertion could cause the DenseMap to
1168 // reallocate itself, which would invalidate all of the PrevP pointers that
1169 // point into the old table. Handle this by checking for reallocation and
1170 // updating the stale pointers only if needed.
1171 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
1172 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
1174 ValueHandleBase *&Entry = Handles[getValPtr()];
1175 assert(!Entry && "Value really did already have handles?");
1176 AddToExistingUseList(&Entry);
1177 getValPtr()->HasValueHandle = true;
1179 // If reallocation didn't happen or if this was the first insertion, don't
1180 // walk the table.
1181 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
1182 Handles.size() == 1) {
1183 return;
1186 // Okay, reallocation did happen. Fix the Prev Pointers.
1187 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
1188 E = Handles.end(); I != E; ++I) {
1189 assert(I->second && I->first == I->second->getValPtr() &&
1190 "List invariant broken!");
1191 I->second->setPrevPtr(&I->second);
1195 void ValueHandleBase::RemoveFromUseList() {
1196 assert(getValPtr() && getValPtr()->HasValueHandle &&
1197 "Pointer doesn't have a use list!");
1199 // Unlink this from its use list.
1200 ValueHandleBase **PrevPtr = getPrevPtr();
1201 assert(*PrevPtr == this && "List invariant broken");
1203 *PrevPtr = Next;
1204 if (Next) {
1205 assert(Next->getPrevPtr() == &Next && "List invariant broken");
1206 Next->setPrevPtr(PrevPtr);
1207 return;
1210 // If the Next pointer was null, then it is possible that this was the last
1211 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
1212 // map.
1213 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1214 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
1215 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
1216 Handles.erase(getValPtr());
1217 getValPtr()->HasValueHandle = false;
1221 void ValueHandleBase::ValueIsDeleted(Value *V) {
1222 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
1224 // Get the linked list base, which is guaranteed to exist since the
1225 // HasValueHandle flag is set.
1226 LLVMContextImpl *pImpl = V->getContext().pImpl;
1227 ValueHandleBase *Entry = pImpl->ValueHandles[V];
1228 assert(Entry && "Value bit set but no entries exist");
1230 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
1231 // and remove themselves from the list without breaking our iteration. This
1232 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
1233 // Note that we deliberately do not the support the case when dropping a value
1234 // handle results in a new value handle being permanently added to the list
1235 // (as might occur in theory for CallbackVH's): the new value handle will not
1236 // be processed and the checking code will mete out righteous punishment if
1237 // the handle is still present once we have finished processing all the other
1238 // value handles (it is fine to momentarily add then remove a value handle).
1239 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1240 Iterator.RemoveFromUseList();
1241 Iterator.AddToExistingUseListAfter(Entry);
1242 assert(Entry->Next == &Iterator && "Loop invariant broken.");
1244 switch (Entry->getKind()) {
1245 case Assert:
1246 break;
1247 case Weak:
1248 case WeakTracking:
1249 // WeakTracking and Weak just go to null, which unlinks them
1250 // from the list.
1251 Entry->operator=(nullptr);
1252 break;
1253 case Callback:
1254 // Forward to the subclass's implementation.
1255 static_cast<CallbackVH*>(Entry)->deleted();
1256 break;
1260 // All callbacks, weak references, and assertingVHs should be dropped by now.
1261 if (V->HasValueHandle) {
1262 #ifndef NDEBUG // Only in +Asserts mode...
1263 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
1264 << "\n";
1265 if (pImpl->ValueHandles[V]->getKind() == Assert)
1266 llvm_unreachable("An asserting value handle still pointed to this"
1267 " value!");
1269 #endif
1270 llvm_unreachable("All references to V were not removed?");
1274 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
1275 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
1276 assert(Old != New && "Changing value into itself!");
1277 assert(Old->getType() == New->getType() &&
1278 "replaceAllUses of value with new value of different type!");
1280 // Get the linked list base, which is guaranteed to exist since the
1281 // HasValueHandle flag is set.
1282 LLVMContextImpl *pImpl = Old->getContext().pImpl;
1283 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
1285 assert(Entry && "Value bit set but no entries exist");
1287 // We use a local ValueHandleBase as an iterator so that
1288 // ValueHandles can add and remove themselves from the list without
1289 // breaking our iteration. This is not really an AssertingVH; we
1290 // just have to give ValueHandleBase some kind.
1291 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1292 Iterator.RemoveFromUseList();
1293 Iterator.AddToExistingUseListAfter(Entry);
1294 assert(Entry->Next == &Iterator && "Loop invariant broken.");
1296 switch (Entry->getKind()) {
1297 case Assert:
1298 case Weak:
1299 // Asserting and Weak handles do not follow RAUW implicitly.
1300 break;
1301 case WeakTracking:
1302 // Weak goes to the new value, which will unlink it from Old's list.
1303 Entry->operator=(New);
1304 break;
1305 case Callback:
1306 // Forward to the subclass's implementation.
1307 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
1308 break;
1312 #ifndef NDEBUG
1313 // If any new weak value handles were added while processing the
1314 // list, then complain about it now.
1315 if (Old->HasValueHandle)
1316 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
1317 switch (Entry->getKind()) {
1318 case WeakTracking:
1319 dbgs() << "After RAUW from " << *Old->getType() << " %"
1320 << Old->getName() << " to " << *New->getType() << " %"
1321 << New->getName() << "\n";
1322 llvm_unreachable(
1323 "A weak tracking value handle still pointed to the old value!\n");
1324 default:
1325 break;
1327 #endif
1330 // Pin the vtable to this file.
1331 void CallbackVH::anchor() {}