LLVM 24.0.0git
Value.cpp
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Value.h"
14#include "LLVMContextImpl.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
18#include "llvm/IR/Constant.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DebugInfo.h"
23#include "llvm/IR/DerivedUser.h"
25#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Operator.h"
31#include "llvm/IR/ValueHandle.h"
35#include <algorithm>
36
37using namespace llvm;
38
39//===----------------------------------------------------------------------===//
40// Value Class
41//===----------------------------------------------------------------------===//
42static inline Type *checkType(Type *Ty) {
43 assert(Ty && "Value defined with a null type: Error!");
44 assert(!isa<TypedPointerType>(Ty->getScalarType()) &&
45 "Cannot have values with typed pointer types");
46 return Ty;
47}
48
49Value::Value(Type *ty, unsigned scid)
50 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
51 SubclassData(0), NumUserOperands(0), IsUsedByMD(false), HasName(false),
52 VTy(checkType(ty)) {
53 static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
54 // FIXME: Why isn't this in the subclass gunk??
55 // Note, we cannot call isa<CallInst> before the CallInst has been
56 // constructed.
57 unsigned OpCode = 0;
58 if (SubclassID >= InstructionVal)
59 OpCode = SubclassID - InstructionVal;
60 if (OpCode == Instruction::Call || OpCode == Instruction::Invoke ||
61 OpCode == Instruction::CallBr)
62 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
63 "invalid CallBase type!");
64 else if (SubclassID != BasicBlockVal &&
65 (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))
66 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
67 "Cannot create non-first-class values except for constants!");
68 static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
69 "Value too big");
70}
71
73 // Notify all ValueHandles (if present) that this value is going away.
74 if (HasValueHandle)
75 ValueHandleBase::ValueIsDeleted(this);
76 if (isUsedByMetadata())
77 ValueAsMetadata::handleDeletion(this);
78
79#ifndef NDEBUG // Only in -g mode...
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
82 // reference and something is wrong. This code is here to print out where
83 // the value is still being referenced.
84 //
85 // Note that use_empty() cannot be called here, as it eventually downcasts
86 // 'this' to GlobalValue (derived class of Value), but GlobalValue has already
87 // been destructed, so accessing it is UB.
88 //
89 if (!materialized_use_empty()) {
90 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
91 for (auto *U : users())
92 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
93
94 llvm_unreachable("Uses remain when a value is destroyed!");
95 }
96#endif
97
98 // If this value is named, destroy the name. This should not be in a symtab
99 // at this point.
100 destroyValueName();
101}
102
103void Value::deleteValue() {
104 switch (getValueID()) {
105#define HANDLE_VALUE(Name) \
106 case Value::Name##Val: \
107 delete static_cast<Name *>(this); \
108 break;
109#define HANDLE_MEMORY_VALUE(Name) \
110 case Value::Name##Val: \
111 static_cast<DerivedUser *>(this)->DeleteValue( \
112 static_cast<DerivedUser *>(this)); \
113 break;
114#define HANDLE_CONSTANT(Name) \
115 case Value::Name##Val: \
116 llvm_unreachable("constants should be destroyed with destroyConstant"); \
117 break;
118#define HANDLE_INSTRUCTION(Name) /* nothing */
119#include "llvm/IR/Value.def"
120
121#define HANDLE_INST(N, OPC, CLASS) \
122 case addEnumValues(Value::InstructionVal, Instruction::OPC): \
123 delete static_cast<CLASS *>(this); \
124 break;
125#define HANDLE_USER_INST(N, OPC, CLASS)
126#include "llvm/IR/Instruction.def"
127
128 default:
129 llvm_unreachable("attempting to delete unknown value kind");
130 }
131}
132
133void Value::destroyValueName() {
134 ValueName *Name = getValueName();
135 if (Name) {
136 MallocAllocator Allocator;
137 Name->Destroy(Allocator);
138 }
139 setValueName(nullptr);
140}
141
142bool Value::hasNUses(unsigned N) const {
143 if (!UseList)
144 return N == 0;
145
146 // TODO: Disallow for ConstantData and remove !UseList check?
147 return hasNItems(use_begin(), use_end(), N);
148}
149
150bool Value::hasNUsesOrMore(unsigned N) const {
151 // TODO: Disallow for ConstantData and remove !UseList check?
152 if (!UseList)
153 return N == 0;
154
155 return hasNItemsOrMore(use_begin(), use_end(), N);
156}
157
158bool Value::hasOneUser() const {
159 if (use_empty())
160 return false;
161 if (hasOneUse())
162 return true;
163 return std::equal(++user_begin(), user_end(), user_begin());
164}
165
166static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
167
168Use *Value::getSingleUndroppableUse() {
169 Use *Result = nullptr;
170 for (Use &U : uses()) {
171 if (!U.getUser()->isDroppable()) {
172 if (Result)
173 return nullptr;
174 Result = &U;
175 }
176 }
177 return Result;
178}
179
180User *Value::getUniqueUndroppableUser() {
181 User *Result = nullptr;
182 for (auto *U : users()) {
183 if (!U->isDroppable()) {
184 if (Result && Result != U)
185 return nullptr;
186 Result = U;
187 }
188 }
189 return Result;
190}
191
192bool Value::hasNUndroppableUses(unsigned int N) const {
193 return hasNItems(user_begin(), user_end(), N, isUnDroppableUser);
194}
195
196bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {
197 return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser);
198}
199
200void Value::dropDroppableUses(
201 llvm::function_ref<bool(const Use *)> ShouldDrop) {
202 SmallVector<Use *, 8> ToBeEdited;
203 for (Use &U : uses())
204 if (U.getUser()->isDroppable() && ShouldDrop(&U))
205 ToBeEdited.push_back(&U);
206 for (Use *U : ToBeEdited)
207 dropDroppableUse(*U);
208}
209
210void Value::dropDroppableUsesIn(User &Usr) {
211 assert(Usr.isDroppable() && "Expected a droppable user!");
212 for (Use &UsrOp : Usr.operands()) {
213 if (UsrOp.get() == this)
214 dropDroppableUse(UsrOp);
215 }
216}
217
218void Value::dropDroppableUse(Use &U) {
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(PoisonValue::get(U.get()->getType()));
225 CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);
226 BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");
227 }
228 return;
229 }
230
231 llvm_unreachable("unknown droppable use");
232}
233
234bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
235 assert(hasUseList() && "ConstantData has no use-list");
236
237 // This can be computed either by scanning the instructions in BB, or by
238 // scanning the use list of this Value. Both lists can be very long, but
239 // usually one is quite short.
240 //
241 // Scan both lists simultaneously until one is exhausted. This limits the
242 // search to the shorter list.
243 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
244 const_user_iterator UI = user_begin(), UE = user_end();
245 for (; BI != BE && UI != UE; ++BI, ++UI) {
246 // Scan basic block: Check if this Value is used by the instruction at BI.
247 if (is_contained(BI->operands(), this))
248 return true;
249 // Scan use list: Check if the use at UI is in BB.
250 const auto *User = dyn_cast<Instruction>(*UI);
251 if (User && User->getParent() == BB)
252 return true;
253 }
254 return false;
255}
256
257unsigned Value::getNumUses() const {
258 // TODO: Disallow for ConstantData and remove !UseList check?
259 if (!UseList)
260 return 0;
261 return (unsigned)std::distance(use_begin(), use_end());
262}
263
264static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
265 ST = nullptr;
266 if (Instruction *I = dyn_cast<Instruction>(V)) {
267 if (BasicBlock *P = I->getParent())
268 if (Function *PP = P->getParent())
269 ST = PP->getValueSymbolTable();
270 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
271 if (Function *P = BB->getParent())
272 ST = P->getValueSymbolTable();
273 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
274 if (Module *P = GV->getParent())
275 ST = &P->getValueSymbolTable();
276 } else if (Argument *A = dyn_cast<Argument>(V)) {
277 if (Function *P = A->getParent())
278 ST = P->getValueSymbolTable();
279 } else {
280 assert(isa<Constant>(V) && "Unknown value type!");
281 return true; // no name is setable for this.
282 }
283 return false;
284}
285
286ValueName *Value::getValueName() const {
287 if (!HasName) return nullptr;
288
289 LLVMContext &Ctx = getContext();
290 auto I = Ctx.pImpl->ValueNames.find(this);
291 assert(I != Ctx.pImpl->ValueNames.end() &&
292 "No name entry found!");
293
294 return I->second;
295}
296
297void Value::setValueName(ValueName *VN) {
298 LLVMContext &Ctx = getContext();
299
300 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
301 "HasName bit out of sync!");
302
303 if (!VN) {
304 if (HasName)
305 Ctx.pImpl->ValueNames.erase(this);
306 HasName = false;
307 return;
308 }
309
310 HasName = true;
311 Ctx.pImpl->ValueNames[this] = VN;
312}
313
314StringRef Value::getName() const {
315 // Make sure the empty string is still a C string. For historical reasons,
316 // some clients want to call .data() on the result and expect it to be null
317 // terminated.
318 if (!hasName())
319 return StringRef("", 0);
320 return getValueName()->getKey();
321}
322
323void Value::setNameImpl(const Twine &NewName) {
324 bool NeedNewName =
325 !getContext().shouldDiscardValueNames() || isa<GlobalValue>(this);
326
327 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
328 // and there is no need to delete the old name.
329 if (!NeedNewName && !hasName())
330 return;
331
332 // Fast path for common IRBuilder case of setName("") when there is no name.
333 if (NewName.isTriviallyEmpty() && !hasName())
334 return;
335
336 SmallString<256> NameData;
337 StringRef NameRef = NeedNewName ? NewName.toStringRef(NameData) : "";
338 assert(!NameRef.contains(0) && "Null bytes are not allowed in names");
339
340 // Name isn't changing?
341 if (getName() == NameRef)
342 return;
343
344 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
345
346 // Get the symbol table to update for this object.
347 ValueSymbolTable *ST;
348 if (getSymTab(this, ST))
349 return; // Cannot set a name on this value (e.g. constant).
350
351 ValueName *NewValueName = nullptr;
352 if (!ST) { // No symbol table to update? Just do the change.
353 if (!NameRef.empty()) {
354 // Create the new name.
355 MallocAllocator Allocator;
356 NewValueName = ValueName::create(NameRef, Allocator);
357 }
358 // NOTE: Could optimize for the case the name is shrinking to not deallocate
359 // then reallocated.
360 destroyValueName();
361
362 if (NewValueName) {
363 assert(NeedNewName);
364 setValueName(NewValueName);
365 getValueName()->setValue(this);
366 }
367 return;
368 }
369
370 if (!NameRef.empty())
371 NewValueName = ST->createValueName(NameRef, this);
372
373 // NOTE: Could optimize for the case the name is shrinking to not deallocate
374 // then reallocated.
375 if (hasName()) {
376 // Remove old name.
377 ST->removeValueName(getValueName());
378 destroyValueName();
379
380 if (NameRef.empty())
381 return;
382 }
383
384 // Name is changing to something new.
385 assert(NeedNewName && NewValueName != nullptr);
386 setValueName(NewValueName);
387}
388
389void Value::setName(const Twine &NewName) {
390 setNameImpl(NewName);
391 if (Function *F = dyn_cast<Function>(this))
392 F->updateAfterNameChange();
393}
394
395void Value::takeName(Value *V) {
396 assert(V != this && "Illegal call to this->takeName(this)!");
397 ValueSymbolTable *ST = nullptr;
398 // If this value has a name, drop it.
399 if (hasName()) {
400 // Get the symtab this is in.
401 if (getSymTab(this, ST)) {
402 // We can't set a name on this value, but we need to clear V's name if
403 // it has one.
404 if (V->hasName()) V->setName("");
405 return; // Cannot set a name on this value (e.g. constant).
406 }
407
408 // Remove old name.
409 if (ST)
410 ST->removeValueName(getValueName());
411 destroyValueName();
412 }
413
414 // Now we know that this has no name.
415
416 // If V has no name either, we're done.
417 if (!V->hasName()) return;
418
419 // Get this's symtab if we didn't before.
420 if (!ST) {
421 if (getSymTab(this, ST)) {
422 // Clear V's name.
423 V->setName("");
424 return; // Cannot set a name on this value (e.g. constant).
425 }
426 }
427
428 // Get V's ST, this should always succeed, because V has a name.
429 ValueSymbolTable *VST;
430 bool Failure = getSymTab(V, VST);
431 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
432
433 // If these values are both in the same symtab, we can do this very fast.
434 // This works even if both values have no symtab yet.
435 if (ST == VST) {
436 // Take the name!
437 setValueName(V->getValueName());
438 V->setValueName(nullptr);
439 getValueName()->setValue(this);
440 return;
441 }
442
443 // Otherwise, things are slightly more complex. Remove V's name from VST and
444 // then reinsert it into ST.
445
446 if (VST)
447 VST->removeValueName(V->getValueName());
448 setValueName(V->getValueName());
449 V->setValueName(nullptr);
450 getValueName()->setValue(this);
451
452 if (ST)
453 ST->reinsertValue(this);
454}
455
456std::string Value::getNameOrAsOperand() const {
457 if (!getName().empty())
458 return std::string(getName());
459
460 std::string BBName;
461 raw_string_ostream OS(BBName);
462 printAsOperand(OS, false);
463 return OS.str();
464}
465
466void Value::assertModuleIsMaterializedImpl() const {
467#ifndef NDEBUG
468 const GlobalValue *GV = dyn_cast<GlobalValue>(this);
469 if (!GV)
470 return;
471 const Module *M = GV->getParent();
472 if (!M)
473 return;
474 assert(M->isMaterialized());
475#endif
476}
477
478#ifndef NDEBUG
479static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
480 Constant *C) {
481 if (!Cache.insert(Expr).second)
482 return false;
483
484 for (auto &O : Expr->operands()) {
485 if (O == C)
486 return true;
487 auto *CE = dyn_cast<ConstantExpr>(O);
488 if (!CE)
489 continue;
490 if (contains(Cache, CE, C))
491 return true;
492 }
493 return false;
494}
495
496static bool contains(Value *Expr, Value *V) {
497 if (Expr == V)
498 return true;
499
500 auto *C = dyn_cast<Constant>(V);
501 if (!C)
502 return false;
503
504 auto *CE = dyn_cast<ConstantExpr>(Expr);
505 if (!CE)
506 return false;
507
508 SmallPtrSet<ConstantExpr *, 4> Cache;
509 return contains(Cache, CE, C);
510}
511#endif // NDEBUG
512
513void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
514 assert(hasUseList() && "Cannot replace constant data");
515 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
516 assert(!contains(New, this) &&
517 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
518 assert(New->getType() == getType() &&
519 "replaceAllUses of value with new value of different type!");
520
521 // Notify all ValueHandles (if present) that this value is going away.
522 if (HasValueHandle)
523 ValueHandleBase::ValueIsRAUWd(this, New);
524 if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())
525 ValueAsMetadata::handleRAUW(this, New);
526
527 while (!materialized_use_empty()) {
528 Use &U = *UseList;
529 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
530 // constant because they are uniqued.
531 if (auto *C = dyn_cast<Constant>(U.getUser())) {
532 if (!isa<GlobalValue>(C)) {
533 C->handleOperandChange(this, New);
534 continue;
535 }
536 }
537
538 U.set(New);
539 }
540
541 if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
542 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
543 if (BB->hasAddressTaken())
544 BlockAddress::lookup(BB)->handleOperandChange(this, New);
545 }
546}
547
548void Value::replaceAllUsesWith(Value *New) {
549 doRAUW(New, ReplaceMetadataUses::Yes);
550}
551
552void Value::replaceNonMetadataUsesWith(Value *New) {
553 doRAUW(New, ReplaceMetadataUses::No);
554}
555
556bool Value::replaceUsesWithIf(Value *New,
557 llvm::function_ref<bool(Use &U)> ShouldReplace) {
558 assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
559 assert(New->getType() == getType() &&
560 "replaceUses of value with new value of different type!");
561
562 SmallVector<TrackingVH<Constant>, 8> Consts;
563 SmallPtrSet<Constant *, 8> Visited;
564
565 bool Changed = false;
566 for (Use &U : llvm::make_early_inc_range(uses())) {
567 if (!ShouldReplace(U))
568 continue;
569 Changed = true;
570
571 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
572 // constant because they are uniqued.
573 if (auto *C = dyn_cast<Constant>(U.getUser())) {
574 if (!isa<GlobalValue>(C)) {
575 if (Visited.insert(C).second)
576 Consts.push_back(TrackingVH<Constant>(C));
577 continue;
578 }
579 }
580 U.set(New);
581 }
582
583 while (!Consts.empty()) {
584 // FIXME: handleOperandChange() updates all the uses in a given Constant,
585 // not just the one passed to ShouldReplace
586 Consts.pop_back_val()->handleOperandChange(this, New);
587 }
588
589 return Changed;
590}
591
592/// Replace debug record uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
593/// with New.
594static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {
595 SmallVector<DbgVariableRecord *> DPUsers;
596 findDbgUsers(V, DPUsers);
597 for (auto *DVR : DPUsers) {
598 DbgMarker *Marker = DVR->getMarker();
599 if (Marker->getParent() != BB)
600 DVR->replaceVariableLocationOp(V, New);
601 }
602}
603
604// Like replaceAllUsesWith except it does not handle constants or basic blocks.
605// This routine leaves uses within BB.
606void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
607 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
608 assert(!contains(New, this) &&
609 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
610 assert(New->getType() == getType() &&
611 "replaceUses of value with new value of different type!");
612 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
613
614 replaceDbgUsesOutsideBlock(this, New, BB);
615 replaceUsesWithIf(New, [BB](Use &U) {
616 auto *I = dyn_cast<Instruction>(U.getUser());
617 // Don't replace if it's an instruction in the BB basic block.
618 return !I || I->getParent() != BB;
619 });
620}
621
622namespace {
623// Various metrics for how much to strip off of pointers.
624enum PointerStripKind {
625 PSK_ZeroIndices,
626 PSK_ZeroIndicesAndAliases,
627 PSK_ZeroIndicesSameRepresentation,
628 PSK_ForAliasAnalysis,
629 PSK_InBoundsConstantIndices,
630 PSK_InBounds
631};
632} // end anonymous namespace
633
634template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}
635
636template <PointerStripKind StripKind>
638 const Value *V,
639 function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {
640 if (!V->getType()->isPointerTy())
641 return V;
642
643 // Even though we don't look through PHI nodes, we could be called on an
644 // instruction in an unreachable block, which may be on a cycle.
645 SmallPtrSet<const Value *, 4> Visited;
646
647 Visited.insert(V);
648 do {
649 Func(V);
650 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
651 switch (StripKind) {
652 case PSK_ZeroIndices:
653 case PSK_ZeroIndicesAndAliases:
654 case PSK_ZeroIndicesSameRepresentation:
655 case PSK_ForAliasAnalysis:
656 if (!GEP->hasAllZeroIndices())
657 return V;
658 break;
659 case PSK_InBoundsConstantIndices:
660 if (!GEP->hasAllConstantIndices())
661 return V;
662 [[fallthrough]];
663 case PSK_InBounds:
664 if (!GEP->isInBounds())
665 return V;
666 break;
667 }
668 V = GEP->getPointerOperand();
669 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
670 Value *NewV = cast<Operator>(V)->getOperand(0);
671 if (!NewV->getType()->isPointerTy())
672 return V;
673 V = NewV;
674 } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
675 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
676 // TODO: If we know an address space cast will not change the
677 // representation we could look through it here as well.
678 V = cast<Operator>(V)->getOperand(0);
679 } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
680 V = cast<GlobalAlias>(V)->getAliasee();
681 } else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&
682 cast<PHINode>(V)->getNumIncomingValues() == 1) {
683 V = cast<PHINode>(V)->getIncomingValue(0);
684 } else {
685 if (const auto *Call = dyn_cast<CallBase>(V)) {
686 if (const Value *RV = Call->getReturnedArgOperand()) {
687 V = RV;
688 continue;
689 }
690 // The result of launder.invariant.group must alias it's argument,
691 // but it can't be marked with returned attribute, that's why it needs
692 // special case.
693 if (StripKind == PSK_ForAliasAnalysis &&
694 Call->getIntrinsicID() == Intrinsic::launder_invariant_group) {
695 V = Call->getArgOperand(0);
696 continue;
697 }
698 }
699 return V;
700 }
701 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
702 } while (Visited.insert(V).second);
703
704 return V;
705}
706
707const Value *Value::stripPointerCasts() const {
709}
710
711const Value *Value::stripPointerCastsAndAliases() const {
713}
714
715const Value *Value::stripPointerCastsSameRepresentation() const {
717}
718
719const Value *Value::stripInBoundsConstantOffsets() const {
721}
722
723const Value *Value::stripPointerCastsForAliasAnalysis() const {
725}
726
727const Value *Value::stripAndAccumulateConstantOffsets(
728 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
729 bool AllowInvariantGroup,
730 function_ref<bool(Value &, APInt &)> ExternalAnalysis,
731 bool LookThroughIntToPtr) const {
732 if (!getType()->isPtrOrPtrVectorTy())
733 return this;
734
735 unsigned BitWidth = Offset.getBitWidth();
736 assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
737 "The offset bit width does not match the DL specification.");
738
739 // Even though we don't look through PHI nodes, we could be called on an
740 // instruction in an unreachable block, which may be on a cycle.
741 SmallPtrSet<const Value *, 4> Visited;
742 Visited.insert(this);
743 const Value *V = this;
744 do {
745 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
746 // If in-bounds was requested, we do not strip non-in-bounds GEPs.
747 if (!AllowNonInbounds && !GEP->isInBounds())
748 return V;
749
750 // If one of the values we have visited is an addrspacecast, then
751 // the pointer type of this GEP may be different from the type
752 // of the Ptr parameter which was passed to this function. This
753 // means when we construct GEPOffset, we need to use the size
754 // of GEP's pointer type rather than the size of the original
755 // pointer type.
756 APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
757 if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
758 return V;
759
760 // Stop traversal if the pointer offset wouldn't fit in the bit-width
761 // provided by the Offset argument. This can happen due to AddrSpaceCast
762 // stripping.
763 if (GEPOffset.getSignificantBits() > BitWidth)
764 return V;
765
766 // External Analysis can return a result higher/lower than the value
767 // represents. We need to detect overflow/underflow.
768 APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
769 if (!ExternalAnalysis) {
770 Offset += GEPOffsetST;
771 } else {
772 bool Overflow = false;
773 APInt OldOffset = Offset;
774 Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
775 if (Overflow) {
776 Offset = std::move(OldOffset);
777 return V;
778 }
779 }
780 V = GEP->getPointerOperand();
781 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
782 const Value *Src = cast<Operator>(V)->getOperand(0);
783 if (!Src->getType()->isPtrOrPtrVectorTy())
784 return V;
785 V = Src;
786 } else if (Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
787 V = cast<Operator>(V)->getOperand(0);
788 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
789 if (!GA->isInterposable())
790 V = GA->getAliasee();
791 } else if (const auto *Call = dyn_cast<CallBase>(V)) {
792 if (const Value *RV = Call->getReturnedArgOperand())
793 V = RV;
794 if (AllowInvariantGroup &&
795 Call->getIntrinsicID() == Intrinsic::launder_invariant_group)
796 V = Call->getArgOperand(0);
797 } else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
798 // Try to accumulate across (inttoptr (add (ptrtoint p), off)).
799 if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||
800 Int2Ptr->getOpcode() != Instruction::IntToPtr ||
801 Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
802 return V;
803
804 auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
805 if (!Add)
806 return V;
807
808 auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
809 auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
810 if (!Ptr2Int || !CI)
811 return V;
812
813 Offset += CI->getValue();
814 V = Ptr2Int->getOperand(0);
815 }
816 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
817 } while (Visited.insert(V).second);
818
819 return V;
820}
821
822const Value *
823Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {
825}
826
827bool Value::canBeFreed() const {
829
830 // Cases that can simply never be deallocated
831 // *) Constants aren't allocated per se, thus not deallocated either.
832 if (isa<Constant>(this))
833 return false;
834
835 // Allocas cannot be freed: They remain dereferenceable after lifetime.end,
836 // in the sense that they can be loaded from without UB. They only become
837 // non-writable, which is not tracked by this API.
838 if (isa<AllocaInst>(this))
839 return false;
840
841 // Handle byval/byref/sret/inalloca/preallocated arguments. The storage
842 // lifetime is guaranteed to be longer than the callee's lifetime.
843 if (auto *A = dyn_cast<Argument>(this)) {
844 if (A->hasPointeeInMemoryValueAttr())
845 return false;
846 // A nofree function can not free (including via synchronization) any
847 // allocations that existed prior to the call, but may free allocations
848 // created inside the function. This logic is limited to argument pointers,
849 // as they definitely exist prior to the call.
850 const Function *F = A->getParent();
851 if (F->doesNotFreeMemory())
852 return false;
853
854 // nofree on the argument ensures that it cannot be freed through that
855 // pointer. noalias additionally ensures that it can't be freed through
856 // another pointer to the same allocation. Readonly implies nofree.
857 if ((A->hasNoFreeAttr() || A->onlyReadsMemory()) && A->hasNoAliasAttr())
858 return false;
859
860 // nofreeobj means that the underlying object cannot be freed, even
861 // through a different pointer.
862 if (A->hasAttribute(Attribute::NoFreeObj))
863 return false;
864 }
865
866 if (auto *ITP = dyn_cast<IntToPtrInst>(this);
867 ITP && ITP->hasMetadata(LLVMContext::MD_nofreeobj))
868 return false;
869
870 if (auto *CB = dyn_cast<CallBase>(this))
871 if (CB->hasRetAttr(Attribute::NoFreeObj))
872 return false;
873
874 const Function *F = nullptr;
875 if (auto *I = dyn_cast<Instruction>(this))
876 F = I->getFunction();
877 if (auto *A = dyn_cast<Argument>(this))
878 F = A->getParent();
879
880 if (!F)
881 return true;
882
883 // With garbage collection, deallocation typically occurs solely at or after
884 // safepoints. If we're compiling for a collector which uses the
885 // gc.statepoint infrastructure, safepoints aren't explicitly present
886 // in the IR until after lowering from abstract to physical machine model.
887 // The collector could chose to mix explicit deallocation and gc'd objects
888 // which is why we need the explicit opt in on a per collector basis.
889 if (!F->hasGC())
890 return true;
891
892 const auto &GCName = F->getGC();
893 if (GCName == "statepoint-example") {
894 auto *PT = cast<PointerType>(this->getType());
895 if (PT->getAddressSpace() != 1)
896 // For the sake of this example GC, we arbitrarily pick addrspace(1) as
897 // our GC managed heap. This must match the same check in
898 // RewriteStatepointsForGC (and probably needs better factored.)
899 return true;
900
901 // It is cheaper to scan for a declaration than to scan for a use in this
902 // function. Note that gc.statepoint is a type overloaded function so the
903 // usual trick of requesting declaration of the intrinsic from the module
904 // doesn't work.
905 for (auto &Fn : *F->getParent())
906 if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)
907 return true;
908 return false;
909 }
910 return true;
911}
912
913uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
914 bool &CanBeNull,
915 bool *CanBeFreed) const {
916 assert(getType()->isPointerTy() && "must be pointer");
917
918 uint64_t DerefBytes = 0;
919 CanBeNull = false;
920 bool CanNotBeFreed = false;
921 if (const Argument *A = dyn_cast<Argument>(this)) {
922 DerefBytes = A->getDereferenceableBytes();
923 if (DerefBytes == 0) {
924 // Handle byval/byref/inalloca/preallocated arguments
925 if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {
926 if (ArgMemTy->isSized()) {
927 // FIXME: Why isn't this the type alloc size?
928 DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinValue();
929 }
930 }
931 }
932
933 if (DerefBytes == 0) {
934 DerefBytes = A->getDereferenceableOrNullBytes();
935 CanBeNull = true;
936 }
937 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
938 DerefBytes = Call->getRetDereferenceableBytes();
939 if (DerefBytes == 0) {
940 DerefBytes = Call->getRetDereferenceableOrNullBytes();
941 CanBeNull = true;
942 }
943 } else if (isa<LoadInst, IntToPtrInst>(this)) {
944 auto *I = cast<Instruction>(this);
945 if (MDNode *MD = I->getMetadata(LLVMContext::MD_dereferenceable)) {
946 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
947 DerefBytes = CI->getLimitedValue();
948 }
949 if (DerefBytes == 0) {
950 if (MDNode *MD =
951 I->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
952 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
953 DerefBytes = CI->getLimitedValue();
954 }
955 CanBeNull = true;
956 }
957 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
958 if (std::optional<TypeSize> Size = AI->getAllocationSize(DL)) {
959 DerefBytes = Size->getKnownMinValue();
960 CanBeNull = false;
961 CanNotBeFreed = true;
962 }
963 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
964 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
965 // TODO: Don't outright reject hasExternalWeakLinkage but set the
966 // CanBeNull flag.
967 DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedValue();
968 CanBeNull = false;
969 CanNotBeFreed = true;
970 }
971 }
972
973 if (CanBeFreed) {
974 // Call canBeFreed() only if there are dereferenceable bytes and it's not
975 // one of the cases that can never be freed.
976 if (!CanNotBeFreed && DerefBytes != 0)
977 *CanBeFreed = canBeFreed();
978 else
979 *CanBeFreed = false;
980 }
981
982 return DerefBytes;
983}
984
985Align Value::getPointerAlignment(const DataLayout &DL) const {
986 assert(getType()->isPointerTy() && "must be pointer");
987 if (const Function *F = dyn_cast<Function>(this)) {
988 Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
989 switch (DL.getFunctionPtrAlignType()) {
990 case DataLayout::FunctionPtrAlignType::Independent:
991 return FunctionPtrAlign;
992 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
993 return std::max(FunctionPtrAlign, F->getAlign().valueOrOne());
994 }
995 llvm_unreachable("Unhandled FunctionPtrAlignType");
996 } else if (auto *GVar = dyn_cast<GlobalVariable>(this)) {
997 const MaybeAlign Alignment(GVar->getAlign());
998 if (!Alignment) {
999 Type *ObjectType = GVar->getValueType();
1000 if (ObjectType->isSized()) {
1001 // If the object is defined in the current Module, we'll be giving
1002 // it the preferred alignment. Otherwise, we have to assume that it
1003 // may only have the minimum ABI alignment.
1004 if (GVar->isStrongDefinitionForLinker())
1005 return DL.getPreferredAlign(GVar);
1006 else
1007 return DL.getABITypeAlign(ObjectType);
1008 }
1009 }
1010 return Alignment.valueOrOne();
1011 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1012 const MaybeAlign Alignment = A->getParamAlign();
1013 if (!Alignment && A->hasStructRetAttr()) {
1014 // An sret parameter has at least the ABI alignment of the return type.
1015 Type *EltTy = A->getParamStructRetType();
1016 if (EltTy->isSized())
1017 return DL.getABITypeAlign(EltTy);
1018 }
1019 return Alignment.valueOrOne();
1020 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
1021 return AI->getAlign();
1022 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
1023 return Call->getRetAlign().valueOrOne();
1024 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
1025 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
1026 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
1027 return Align(CI->getLimitedValue());
1028 }
1029 } else if (auto *CE = dyn_cast<ConstantExpr>(this)) {
1030 // Determine the alignment of inttoptr(C).
1031 if (CE->getOpcode() == Instruction::IntToPtr &&
1032 isa<ConstantInt>(CE->getOperand(0))) {
1033 ConstantInt *IntPtr = cast<ConstantInt>(CE->getOperand(0));
1034 size_t TrailingZeros = IntPtr->getValue().countr_zero();
1035 // While the actual alignment may be large, elsewhere we have
1036 // an arbitrary upper alignmet limit, so let's clamp to it.
1037 return Align(TrailingZeros < Value::MaxAlignmentExponent
1038 ? uint64_t(1) << TrailingZeros
1039 : Value::MaximumAlignment);
1040 }
1041 }
1042 return Align(1);
1043}
1044
1045static std::optional<int64_t>
1046getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
1047 // Skip over the first indices.
1049 for (unsigned i = 1; i != Idx; ++i, ++GTI)
1050 /*skip along*/;
1051
1052 // Compute the offset implied by the rest of the indices.
1053 int64_t Offset = 0;
1054 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
1055 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
1056 if (!OpC)
1057 return std::nullopt;
1058 if (OpC->isZero())
1059 continue; // No offset.
1060
1061 // Handle struct indices, which add their field offset to the pointer.
1062 if (StructType *STy = GTI.getStructTypeOrNull()) {
1063 Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
1064 continue;
1065 }
1066
1067 // Otherwise, we have a sequential type like an array or fixed-length
1068 // vector. Multiply the index by the ElementSize.
1069 TypeSize Size = GTI.getSequentialElementStride(DL);
1070 if (Size.isScalable())
1071 return std::nullopt;
1072 Offset += Size.getFixedValue() * OpC->getSExtValue();
1073 }
1074
1075 return Offset;
1076}
1077
1078std::optional<int64_t> Value::getPointerOffsetFrom(const Value *Other,
1079 const DataLayout &DL) const {
1080 const Value *Ptr1 = Other;
1081 const Value *Ptr2 = this;
1082 APInt Offset1(DL.getIndexTypeSizeInBits(Ptr1->getType()), 0);
1083 APInt Offset2(DL.getIndexTypeSizeInBits(Ptr2->getType()), 0);
1084 Ptr1 = Ptr1->stripAndAccumulateConstantOffsets(DL, Offset1, true);
1085 Ptr2 = Ptr2->stripAndAccumulateConstantOffsets(DL, Offset2, true);
1086
1087 // Handle the trivial case first.
1088 if (Ptr1 == Ptr2)
1089 return Offset2.getSExtValue() - Offset1.getSExtValue();
1090
1091 const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
1092 const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
1093
1094 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
1095 // base. After that base, they may have some number of common (and
1096 // potentially variable) indices. After that they handle some constant
1097 // offset, which determines their offset from each other. At this point, we
1098 // handle no other case.
1099 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
1100 GEP1->getSourceElementType() != GEP2->getSourceElementType())
1101 return std::nullopt;
1102
1103 // Skip any common indices and track the GEP types.
1104 unsigned Idx = 1;
1105 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
1106 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
1107 break;
1108
1109 auto IOffset1 = getOffsetFromIndex(GEP1, Idx, DL);
1110 auto IOffset2 = getOffsetFromIndex(GEP2, Idx, DL);
1111 if (!IOffset1 || !IOffset2)
1112 return std::nullopt;
1113 return *IOffset2 - *IOffset1 + Offset2.getSExtValue() -
1114 Offset1.getSExtValue();
1115}
1116
1117const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
1118 const BasicBlock *PredBB) const {
1119 auto *PN = dyn_cast<PHINode>(this);
1120 if (PN && PN->getParent() == CurBB)
1121 return PN->getIncomingValueForBlock(PredBB);
1122 return this;
1123}
1124
1125void Value::reverseUseList() {
1126 if (!UseList || !UseList->Next)
1127 // No need to reverse 0 or 1 uses.
1128 return;
1129
1130 Use *Head = UseList;
1131 Use *Current = UseList->Next;
1132 Head->Next = nullptr;
1133 while (Current) {
1134 Use *Next = Current->Next;
1135 Current->Next = Head;
1136 Head->Prev = &Current->Next;
1137 Head = Current;
1138 Current = Next;
1139 }
1140 UseList = Head;
1141 Head->Prev = &UseList;
1142}
1143
1144bool Value::isSwiftError() const {
1145 auto *Arg = dyn_cast<Argument>(this);
1146 if (Arg)
1147 return Arg->hasSwiftErrorAttr();
1148 auto *Alloca = dyn_cast<AllocaInst>(this);
1149 if (!Alloca)
1150 return false;
1151 return Alloca->isSwiftError();
1152}
1153
1154//===----------------------------------------------------------------------===//
1155// ValueHandleBase Class
1156//===----------------------------------------------------------------------===//
1157
1158ValueHandleHead::ValueHandleHead(ValueHandleHead &&Other) noexcept
1159 : Head(Other.Head) {
1160 if (ValueHandleBase *Ptr = get())
1161 Ptr->setPrevPtr(&Head);
1162}
1163
1164void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
1165 assert(List && "Handle list is null?");
1166
1167 // Splice ourselves into the list.
1168 Next = ValueHandleHead::exchange(List, this).getPointer();
1169 setPrevPtr(List);
1170 if (Next) {
1171 Next->setPrevPtr(&Next);
1172 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
1173 }
1174}
1175
1176void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
1177 assert(List && "Must insert after existing node");
1178
1179 Next = List->Next;
1180 setPrevPtr(&List->Next);
1181 List->Next = this;
1182 if (Next)
1183 Next->setPrevPtr(&Next);
1184}
1185
1186void ValueHandleBase::AddToUseList() {
1187 assert(getValPtr() && "Null pointer doesn't have a use list!");
1188
1189 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1190 ValueHandleHead &Entry = pImpl->ValueHandles[getValPtr()];
1191 assert(getValPtr()->hasValueHandle() == (Entry.get() != nullptr) &&
1192 "HasValueHandle and ValueHandles out of sync!");
1193 AddToExistingUseList(Entry.getAddress());
1194 getValPtr()->HasValueHandle = true;
1195}
1196
1197void ValueHandleBase::RemoveFromUseList() {
1198 assert(getValPtr() && getValPtr()->HasValueHandle &&
1199 "Pointer doesn't have a use list!");
1200
1201 // Unlink this from its use list.
1202 ValueHandleBase **PrevPtr = getPrevPtr();
1203 auto [OldPtr, IsHead] = ValueHandleHead::exchange(PrevPtr, Next);
1204 assert(OldPtr == this && "List invariant broken");
1205 if (Next) {
1206 assert(Next->getPrevPtr() == &Next && "List invariant broken");
1207 Next->setPrevPtr(PrevPtr);
1208 return;
1209 }
1210
1211 // If the Next pointer was null, then it is possible that this was the last
1212 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
1213 // map.
1214 if (IsHead) {
1215 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1216 pImpl->ValueHandles.erase(getValPtr());
1217 getValPtr()->HasValueHandle = false;
1218 }
1219}
1220
1221void ValueHandleBase::ValueIsDeleted(Value *V) {
1222 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
1223
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].get();
1228 assert(Entry && "Value bit set but no entries exist");
1229
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.");
1243
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;
1257 }
1258 }
1259
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].get()->getKind() == Assert)
1266 llvm_unreachable("An asserting value handle still pointed to this"
1267 " value!");
1268
1269#endif
1270 llvm_unreachable("All references to V were not removed?");
1271 }
1272}
1273
1274void 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!");
1279
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].get();
1284
1285 assert(Entry && "Value bit set but no entries exist");
1286
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.");
1295
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;
1309 }
1310 }
1311
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].get(); 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";
1323 "A weak tracking value handle still pointed to the old value!\n");
1324 default:
1325 break;
1326 }
1327#endif
1328}
1329
1330// Pin the vtable to this file.
1331void CallbackVH::anchor() {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
This file defines the DenseMap class.
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
iv users
Definition IVUsers.cpp:48
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
const uint64_t BitWidth
#define P(N)
if(PassOpts->AAPipeline)
static StringRef getName(Value *V)
Basic Register Allocator
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file contains some templates that are useful if you are working with the STL at all.
static std::optional< int64_t > getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL)
Definition Value.cpp:1046
static void NoopCallback(const Value *)
Definition Value.cpp:634
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
static Type * checkType(Type *Ty)
Definition Value.cpp:42
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition Value.cpp:264
static const Value * stripPointerCastsAndOffsets(const Value *V, function_ref< void(const Value *)> Func=NoopCallback< StripKind >)
Definition Value.cpp:637
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB)
Replace debug record uses of MetadataAsValue(ValueAsMetadata(V)) outside BB with New.
Definition Value.cpp:594
static bool isUnDroppableUser(const User *U)
Definition Value.cpp:166
This file defines the SmallString class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1659
LLVM_ABI void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of basic bl...
iterator end()
Definition BasicBlock.h:453
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:440
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:666
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition Constants.h:269
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition Constants.h:174
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
LLVM_ABI const BasicBlock * getParent() const
bool erase(const KeyT &Val)
Definition DenseMap.h:930
LLVM_ABI Type * getSourceElementType() const
Definition Operator.cpp:86
bool hasExternalWeakLinkage() const
Module * getParent()
Get the module that this global value is contained inside of...
Type * getValueType() const
DenseMap< const Value *, ValueName * > ValueNames
ValueHandlesTy ValueHandles
LLVMContextImpl *const pImpl
Definition LLVMContext.h:78
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void push_back(const T &Elt)
static StringMapEntry * create(StringRef key, AllocatorTy &allocator, InitTy &&...initVals)
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:446
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition Twine.h:398
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition Twine.h:461
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
op_range operands()
Definition User.h:267
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition User.cpp:113
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:49
LLVM_ABI ~Value()
Value's destructor should be virtual by design, but that would require that Value and all of its subc...
Definition Value.cpp:72
TypeSize getSequentialElementStride(const DataLayout &DL) const
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ Entry
Definition COFF.h:862
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition Transport.h:132
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
bool empty() const
Definition BasicBlock.h:101
This is an optimization pass for GlobalISel generic memory operations.
StringMapEntry< Value * > ValueName
Definition Value.h:56
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
bool hasNItemsOrMore(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)> >::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has N or more items.
Definition STLExtras.h:2654
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
bool hasNItems(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)> >::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has exactly N items.
Definition STLExtras.h:2629
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:373
generic_gep_type_iterator<> gep_type_iterator
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
gep_type_iterator gep_type_begin(const User *GEP)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
#define N
StringMapEntry< uint32_t > * Tag
The operand bundle tag, interned by LLVMContextImpl::getOrInsertBundleTag.