LLVM 22.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"
17#include "llvm/IR/Constant.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/DebugInfo.h"
22#include "llvm/IR/DerivedUser.h"
24#include "llvm/IR/InstrTypes.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
30#include "llvm/IR/ValueHandle.h"
35#include <algorithm>
36
37using namespace llvm;
38
40 "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),
41 cl::desc("Deref attributes and metadata infer facts at definition only"));
42
43//===----------------------------------------------------------------------===//
44// Value Class
45//===----------------------------------------------------------------------===//
46static 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;
51}
52
53Value::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)) {
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");
74}
75
76Value::~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);
82
83 // Remove associated metadata from context.
84 if (HasMetadata)
85 clearMetadata();
86
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.
92 //
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.
96 //
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";
101
102 llvm_unreachable("Uses remain when a value is destroyed!");
103 }
104#endif
105
106 // If this value is named, destroy the name. This should not be in a symtab
107 // at this point.
108 destroyValueName();
109}
110
111void Value::deleteValue() {
112 switch (getValueID()) {
113#define HANDLE_VALUE(Name) \
114 case Value::Name##Val: \
115 delete static_cast<Name *>(this); \
116 break;
117#define HANDLE_MEMORY_VALUE(Name) \
118 case Value::Name##Val: \
119 static_cast<DerivedUser *>(this)->DeleteValue( \
120 static_cast<DerivedUser *>(this)); \
121 break;
122#define HANDLE_CONSTANT(Name) \
123 case Value::Name##Val: \
124 llvm_unreachable("constants should be destroyed with destroyConstant"); \
125 break;
126#define HANDLE_INSTRUCTION(Name) /* nothing */
127#include "llvm/IR/Value.def"
128
129#define HANDLE_INST(N, OPC, CLASS) \
130 case Value::InstructionVal + Instruction::OPC: \
131 delete static_cast<CLASS *>(this); \
132 break;
133#define HANDLE_USER_INST(N, OPC, CLASS)
134#include "llvm/IR/Instruction.def"
135
136 default:
137 llvm_unreachable("attempting to delete unknown value kind");
138 }
139}
140
141void Value::destroyValueName() {
142 ValueName *Name = getValueName();
143 if (Name) {
145 Name->Destroy(Allocator);
146 }
147 setValueName(nullptr);
148}
149
150bool Value::hasNUses(unsigned N) const {
151 if (!UseList)
152 return N == 0;
153
154 // TODO: Disallow for ConstantData and remove !UseList check?
155 return hasNItems(use_begin(), use_end(), N);
156}
157
158bool Value::hasNUsesOrMore(unsigned N) const {
159 // TODO: Disallow for ConstantData and remove !UseList check?
160 if (!UseList)
161 return N == 0;
162
163 return hasNItemsOrMore(use_begin(), use_end(), N);
164}
165
166bool Value::hasOneUser() const {
167 if (use_empty())
168 return false;
169 if (hasOneUse())
170 return true;
171 return std::equal(++user_begin(), user_end(), user_begin());
172}
173
174static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
175
176Use *Value::getSingleUndroppableUse() {
177 Use *Result = nullptr;
178 for (Use &U : uses()) {
179 if (!U.getUser()->isDroppable()) {
180 if (Result)
181 return nullptr;
182 Result = &U;
183 }
184 }
185 return Result;
186}
187
188User *Value::getUniqueUndroppableUser() {
189 User *Result = nullptr;
190 for (auto *U : users()) {
191 if (!U->isDroppable()) {
192 if (Result && Result != U)
193 return nullptr;
194 Result = U;
195 }
196 }
197 return Result;
198}
199
200bool Value::hasNUndroppableUses(unsigned int N) const {
201 return hasNItems(user_begin(), user_end(), N, isUnDroppableUser);
202}
203
204bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {
205 return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser);
206}
207
208void Value::dropDroppableUses(
209 llvm::function_ref<bool(const Use *)> ShouldDrop) {
210 SmallVector<Use *, 8> ToBeEdited;
211 for (Use &U : uses())
212 if (U.getUser()->isDroppable() && ShouldDrop(&U))
213 ToBeEdited.push_back(&U);
214 for (Use *U : ToBeEdited)
215 dropDroppableUse(*U);
216}
217
218void Value::dropDroppableUsesIn(User &Usr) {
219 assert(Usr.isDroppable() && "Expected a droppable user!");
220 for (Use &UsrOp : Usr.operands()) {
221 if (UsrOp.get() == this)
222 dropDroppableUse(UsrOp);
223 }
224}
225
226void Value::dropDroppableUse(Use &U) {
227 if (auto *Assume = dyn_cast<AssumeInst>(U.getUser())) {
228 unsigned OpNo = U.getOperandNo();
229 if (OpNo == 0)
230 U.set(ConstantInt::getTrue(Assume->getContext()));
231 else {
232 U.set(PoisonValue::get(U.get()->getType()));
233 CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);
234 BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");
235 }
236 return;
237 }
238
239 llvm_unreachable("unknown droppable use");
240}
241
242bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
243 assert(hasUseList() && "ConstantData has no use-list");
244
245 // This can be computed either by scanning the instructions in BB, or by
246 // scanning the use list of this Value. Both lists can be very long, but
247 // usually one is quite short.
248 //
249 // Scan both lists simultaneously until one is exhausted. This limits the
250 // search to the shorter list.
251 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
252 const_user_iterator UI = user_begin(), UE = user_end();
253 for (; BI != BE && UI != UE; ++BI, ++UI) {
254 // Scan basic block: Check if this Value is used by the instruction at BI.
255 if (is_contained(BI->operands(), this))
256 return true;
257 // Scan use list: Check if the use at UI is in BB.
258 const auto *User = dyn_cast<Instruction>(*UI);
259 if (User && User->getParent() == BB)
260 return true;
261 }
262 return false;
263}
264
265unsigned Value::getNumUses() const {
266 // TODO: Disallow for ConstantData and remove !UseList check?
267 if (!UseList)
268 return 0;
269 return (unsigned)std::distance(use_begin(), use_end());
270}
271
272static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
273 ST = nullptr;
274 if (Instruction *I = dyn_cast<Instruction>(V)) {
275 if (BasicBlock *P = I->getParent())
276 if (Function *PP = P->getParent())
277 ST = PP->getValueSymbolTable();
278 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
279 if (Function *P = BB->getParent())
280 ST = P->getValueSymbolTable();
281 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
282 if (Module *P = GV->getParent())
283 ST = &P->getValueSymbolTable();
284 } else if (Argument *A = dyn_cast<Argument>(V)) {
285 if (Function *P = A->getParent())
286 ST = P->getValueSymbolTable();
287 } else {
288 assert(isa<Constant>(V) && "Unknown value type!");
289 return true; // no name is setable for this.
290 }
291 return false;
292}
293
294ValueName *Value::getValueName() const {
295 if (!HasName) return nullptr;
296
297 LLVMContext &Ctx = getContext();
298 auto I = Ctx.pImpl->ValueNames.find(this);
299 assert(I != Ctx.pImpl->ValueNames.end() &&
300 "No name entry found!");
301
302 return I->second;
303}
304
305void Value::setValueName(ValueName *VN) {
306 LLVMContext &Ctx = getContext();
307
308 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
309 "HasName bit out of sync!");
310
311 if (!VN) {
312 if (HasName)
313 Ctx.pImpl->ValueNames.erase(this);
314 HasName = false;
315 return;
316 }
317
318 HasName = true;
319 Ctx.pImpl->ValueNames[this] = VN;
320}
321
322StringRef Value::getName() const {
323 // Make sure the empty string is still a C string. For historical reasons,
324 // some clients want to call .data() on the result and expect it to be null
325 // terminated.
326 if (!hasName())
327 return StringRef("", 0);
328 return getValueName()->getKey();
329}
330
331void Value::setNameImpl(const Twine &NewName) {
332 bool NeedNewName =
333 !getContext().shouldDiscardValueNames() || isa<GlobalValue>(this);
334
335 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
336 // and there is no need to delete the old name.
337 if (!NeedNewName && !hasName())
338 return;
339
340 // Fast path for common IRBuilder case of setName("") when there is no name.
341 if (NewName.isTriviallyEmpty() && !hasName())
342 return;
343
344 SmallString<256> NameData;
345 StringRef NameRef = NeedNewName ? NewName.toStringRef(NameData) : "";
346 assert(!NameRef.contains(0) && "Null bytes are not allowed in names");
347
348 // Name isn't changing?
349 if (getName() == NameRef)
350 return;
351
352 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
353
354 // Get the symbol table to update for this object.
356 if (getSymTab(this, ST))
357 return; // Cannot set a name on this value (e.g. constant).
358
359 if (!ST) { // No symbol table to update? Just do the change.
360 // NOTE: Could optimize for the case the name is shrinking to not deallocate
361 // then reallocated.
362 destroyValueName();
363
364 if (!NameRef.empty()) {
365 // Create the new name.
366 assert(NeedNewName);
368 setValueName(ValueName::create(NameRef, Allocator));
369 getValueName()->setValue(this);
370 }
371 return;
372 }
373
374 // NOTE: Could optimize for the case the name is shrinking to not deallocate
375 // then reallocated.
376 if (hasName()) {
377 // Remove old name.
378 ST->removeValueName(getValueName());
379 destroyValueName();
380
381 if (NameRef.empty())
382 return;
383 }
384
385 // Name is changing to something new.
386 assert(NeedNewName);
387 setValueName(ST->createValueName(NameRef, this));
388}
389
390void Value::setName(const Twine &NewName) {
391 setNameImpl(NewName);
392 if (Function *F = dyn_cast<Function>(this))
393 F->updateAfterNameChange();
394}
395
396void Value::takeName(Value *V) {
397 assert(V != this && "Illegal call to this->takeName(this)!");
398 ValueSymbolTable *ST = nullptr;
399 // If this value has a name, drop it.
400 if (hasName()) {
401 // Get the symtab this is in.
402 if (getSymTab(this, ST)) {
403 // We can't set a name on this value, but we need to clear V's name if
404 // it has one.
405 if (V->hasName()) V->setName("");
406 return; // Cannot set a name on this value (e.g. constant).
407 }
408
409 // Remove old name.
410 if (ST)
411 ST->removeValueName(getValueName());
412 destroyValueName();
413 }
414
415 // Now we know that this has no name.
416
417 // If V has no name either, we're done.
418 if (!V->hasName()) return;
419
420 // Get this's symtab if we didn't before.
421 if (!ST) {
422 if (getSymTab(this, ST)) {
423 // Clear V's name.
424 V->setName("");
425 return; // Cannot set a name on this value (e.g. constant).
426 }
427 }
428
429 // Get V's ST, this should always succeed, because V has a name.
430 ValueSymbolTable *VST;
431 bool Failure = getSymTab(V, VST);
432 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
433
434 // If these values are both in the same symtab, we can do this very fast.
435 // This works even if both values have no symtab yet.
436 if (ST == VST) {
437 // Take the name!
438 setValueName(V->getValueName());
439 V->setValueName(nullptr);
440 getValueName()->setValue(this);
441 return;
442 }
443
444 // Otherwise, things are slightly more complex. Remove V's name from VST and
445 // then reinsert it into ST.
446
447 if (VST)
448 VST->removeValueName(V->getValueName());
449 setValueName(V->getValueName());
450 V->setValueName(nullptr);
451 getValueName()->setValue(this);
452
453 if (ST)
454 ST->reinsertValue(this);
455}
456
457std::string Value::getNameOrAsOperand() const {
458 if (!getName().empty())
459 return std::string(getName());
460
461 std::string BBName;
462 raw_string_ostream OS(BBName);
463 printAsOperand(OS, false);
464 return OS.str();
465}
466
467void Value::assertModuleIsMaterializedImpl() const {
468#ifndef NDEBUG
469 const GlobalValue *GV = dyn_cast<GlobalValue>(this);
470 if (!GV)
471 return;
472 const Module *M = GV->getParent();
473 if (!M)
474 return;
475 assert(M->isMaterialized());
476#endif
477}
478
479#ifndef NDEBUG
481 Constant *C) {
482 if (!Cache.insert(Expr).second)
483 return false;
484
485 for (auto &O : Expr->operands()) {
486 if (O == C)
487 return true;
488 auto *CE = dyn_cast<ConstantExpr>(O);
489 if (!CE)
490 continue;
491 if (contains(Cache, CE, C))
492 return true;
493 }
494 return false;
495}
496
497static bool contains(Value *Expr, Value *V) {
498 if (Expr == V)
499 return true;
500
501 auto *C = dyn_cast<Constant>(V);
502 if (!C)
503 return false;
504
505 auto *CE = dyn_cast<ConstantExpr>(Expr);
506 if (!CE)
507 return false;
508
510 return contains(Cache, CE, C);
511}
512#endif // NDEBUG
513
514void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
515 assert(hasUseList() && "Cannot replace constant data");
516 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
517 assert(!contains(New, this) &&
518 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
519 assert(New->getType() == getType() &&
520 "replaceAllUses of value with new value of different type!");
521
522 // Notify all ValueHandles (if present) that this value is going away.
523 if (HasValueHandle)
524 ValueHandleBase::ValueIsRAUWd(this, New);
525 if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())
526 ValueAsMetadata::handleRAUW(this, New);
527
528 while (!materialized_use_empty()) {
529 Use &U = *UseList;
530 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
531 // constant because they are uniqued.
532 if (auto *C = dyn_cast<Constant>(U.getUser())) {
533 if (!isa<GlobalValue>(C)) {
534 C->handleOperandChange(this, New);
535 continue;
536 }
537 }
538
539 U.set(New);
540 }
541
542 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
543 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
544}
545
546void Value::replaceAllUsesWith(Value *New) {
547 doRAUW(New, ReplaceMetadataUses::Yes);
548}
549
550void Value::replaceNonMetadataUsesWith(Value *New) {
551 doRAUW(New, ReplaceMetadataUses::No);
552}
553
554void Value::replaceUsesWithIf(Value *New,
555 llvm::function_ref<bool(Use &U)> ShouldReplace) {
556 assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
557 assert(New->getType() == getType() &&
558 "replaceUses of value with new value of different type!");
559
562
563 for (Use &U : llvm::make_early_inc_range(uses())) {
564 if (!ShouldReplace(U))
565 continue;
566 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
567 // constant because they are uniqued.
568 if (auto *C = dyn_cast<Constant>(U.getUser())) {
569 if (!isa<GlobalValue>(C)) {
570 if (Visited.insert(C).second)
572 continue;
573 }
574 }
575 U.set(New);
576 }
577
578 while (!Consts.empty()) {
579 // FIXME: handleOperandChange() updates all the uses in a given Constant,
580 // not just the one passed to ShouldReplace
581 Consts.pop_back_val()->handleOperandChange(this, New);
582 }
583}
584
585/// Replace debug record uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
586/// with New.
587static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {
589 findDbgUsers(V, DPUsers);
590 for (auto *DVR : DPUsers) {
591 DbgMarker *Marker = DVR->getMarker();
592 if (Marker->getParent() != BB)
593 DVR->replaceVariableLocationOp(V, New);
594 }
595}
596
597// Like replaceAllUsesWith except it does not handle constants or basic blocks.
598// This routine leaves uses within BB.
599void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
600 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
601 assert(!contains(New, this) &&
602 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
603 assert(New->getType() == getType() &&
604 "replaceUses of value with new value of different type!");
605 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
606
607 replaceDbgUsesOutsideBlock(this, New, BB);
608 replaceUsesWithIf(New, [BB](Use &U) {
609 auto *I = dyn_cast<Instruction>(U.getUser());
610 // Don't replace if it's an instruction in the BB basic block.
611 return !I || I->getParent() != BB;
612 });
613}
614
615namespace {
616// Various metrics for how much to strip off of pointers.
617enum PointerStripKind {
618 PSK_ZeroIndices,
619 PSK_ZeroIndicesAndAliases,
620 PSK_ZeroIndicesSameRepresentation,
621 PSK_ForAliasAnalysis,
622 PSK_InBoundsConstantIndices,
623 PSK_InBounds
624};
625
626template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}
627
628template <PointerStripKind StripKind>
629static const Value *stripPointerCastsAndOffsets(
630 const Value *V,
631 function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {
632 if (!V->getType()->isPointerTy())
633 return V;
634
635 // Even though we don't look through PHI nodes, we could be called on an
636 // instruction in an unreachable block, which may be on a cycle.
638
639 Visited.insert(V);
640 do {
641 Func(V);
642 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
643 switch (StripKind) {
644 case PSK_ZeroIndices:
645 case PSK_ZeroIndicesAndAliases:
646 case PSK_ZeroIndicesSameRepresentation:
647 case PSK_ForAliasAnalysis:
648 if (!GEP->hasAllZeroIndices())
649 return V;
650 break;
651 case PSK_InBoundsConstantIndices:
652 if (!GEP->hasAllConstantIndices())
653 return V;
654 [[fallthrough]];
655 case PSK_InBounds:
656 if (!GEP->isInBounds())
657 return V;
658 break;
659 }
660 V = GEP->getPointerOperand();
661 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
662 Value *NewV = cast<Operator>(V)->getOperand(0);
663 if (!NewV->getType()->isPointerTy())
664 return V;
665 V = NewV;
666 } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
667 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
668 // TODO: If we know an address space cast will not change the
669 // representation we could look through it here as well.
670 V = cast<Operator>(V)->getOperand(0);
671 } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
672 V = cast<GlobalAlias>(V)->getAliasee();
673 } else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&
674 cast<PHINode>(V)->getNumIncomingValues() == 1) {
675 V = cast<PHINode>(V)->getIncomingValue(0);
676 } else {
677 if (const auto *Call = dyn_cast<CallBase>(V)) {
678 if (const Value *RV = Call->getReturnedArgOperand()) {
679 V = RV;
680 continue;
681 }
682 // The result of launder.invariant.group must alias it's argument,
683 // but it can't be marked with returned attribute, that's why it needs
684 // special case.
685 if (StripKind == PSK_ForAliasAnalysis &&
686 (Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
687 Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {
688 V = Call->getArgOperand(0);
689 continue;
690 }
691 }
692 return V;
693 }
694 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
695 } while (Visited.insert(V).second);
696
697 return V;
698}
699} // end anonymous namespace
700
701const Value *Value::stripPointerCasts() const {
702 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
703}
704
705const Value *Value::stripPointerCastsAndAliases() const {
706 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
707}
708
709const Value *Value::stripPointerCastsSameRepresentation() const {
710 return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);
711}
712
713const Value *Value::stripInBoundsConstantOffsets() const {
714 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
715}
716
717const Value *Value::stripPointerCastsForAliasAnalysis() const {
718 return stripPointerCastsAndOffsets<PSK_ForAliasAnalysis>(this);
719}
720
721const Value *Value::stripAndAccumulateConstantOffsets(
722 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
723 bool AllowInvariantGroup,
724 function_ref<bool(Value &, APInt &)> ExternalAnalysis,
725 bool LookThroughIntToPtr) const {
726 if (!getType()->isPtrOrPtrVectorTy())
727 return this;
728
729 unsigned BitWidth = Offset.getBitWidth();
730 assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
731 "The offset bit width does not match the DL specification.");
732
733 // Even though we don't look through PHI nodes, we could be called on an
734 // instruction in an unreachable block, which may be on a cycle.
736 Visited.insert(this);
737 const Value *V = this;
738 do {
739 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
740 // If in-bounds was requested, we do not strip non-in-bounds GEPs.
741 if (!AllowNonInbounds && !GEP->isInBounds())
742 return V;
743
744 // If one of the values we have visited is an addrspacecast, then
745 // the pointer type of this GEP may be different from the type
746 // of the Ptr parameter which was passed to this function. This
747 // means when we construct GEPOffset, we need to use the size
748 // of GEP's pointer type rather than the size of the original
749 // pointer type.
750 APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
751 if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
752 return V;
753
754 // Stop traversal if the pointer offset wouldn't fit in the bit-width
755 // provided by the Offset argument. This can happen due to AddrSpaceCast
756 // stripping.
757 if (GEPOffset.getSignificantBits() > BitWidth)
758 return V;
759
760 // External Analysis can return a result higher/lower than the value
761 // represents. We need to detect overflow/underflow.
762 APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
763 if (!ExternalAnalysis) {
764 Offset += GEPOffsetST;
765 } else {
766 bool Overflow = false;
767 APInt OldOffset = Offset;
768 Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
769 if (Overflow) {
770 Offset = OldOffset;
771 return V;
772 }
773 }
774 V = GEP->getPointerOperand();
775 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
776 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
777 V = cast<Operator>(V)->getOperand(0);
778 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
779 if (!GA->isInterposable())
780 V = GA->getAliasee();
781 } else if (const auto *Call = dyn_cast<CallBase>(V)) {
782 if (const Value *RV = Call->getReturnedArgOperand())
783 V = RV;
784 if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
785 V = Call->getArgOperand(0);
786 } else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {
787 // Try to accumulate across (inttoptr (add (ptrtoint p), off)).
788 if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||
789 Int2Ptr->getOpcode() != Instruction::IntToPtr ||
790 Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)
791 return V;
792
793 auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));
794 if (!Add)
795 return V;
796
797 auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));
798 auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));
799 if (!Ptr2Int || !CI)
800 return V;
801
802 Offset += CI->getValue();
803 V = Ptr2Int->getOperand(0);
804 }
805 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
806 } while (Visited.insert(V).second);
807
808 return V;
809}
810
811const Value *
812Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {
813 return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func);
814}
815
816bool Value::canBeFreed() const {
818
819 // Cases that can simply never be deallocated
820 // *) Constants aren't allocated per se, thus not deallocated either.
821 if (isa<Constant>(this))
822 return false;
823
824 // Handle byval/byref/sret/inalloca/preallocated arguments. The storage
825 // lifetime is guaranteed to be longer than the callee's lifetime.
826 if (auto *A = dyn_cast<Argument>(this)) {
827 if (A->hasPointeeInMemoryValueAttr())
828 return false;
829 // A pointer to an object in a function which neither frees, nor can arrange
830 // for another thread to free on its behalf, can not be freed in the scope
831 // of the function. Note that this logic is restricted to memory
832 // allocations in existance before the call; a nofree function *is* allowed
833 // to free memory it allocated.
834 const Function *F = A->getParent();
835 if (F->doesNotFreeMemory() && F->hasNoSync())
836 return false;
837 }
838
839 if (isa<IntToPtrInst>(this) && getMetadata(LLVMContext::MD_nofree))
840 return false;
841
842 const Function *F = nullptr;
843 if (auto *I = dyn_cast<Instruction>(this))
844 F = I->getFunction();
845 if (auto *A = dyn_cast<Argument>(this))
846 F = A->getParent();
847
848 if (!F)
849 return true;
850
851 // With garbage collection, deallocation typically occurs solely at or after
852 // safepoints. If we're compiling for a collector which uses the
853 // gc.statepoint infrastructure, safepoints aren't explicitly present
854 // in the IR until after lowering from abstract to physical machine model.
855 // The collector could chose to mix explicit deallocation and gc'd objects
856 // which is why we need the explicit opt in on a per collector basis.
857 if (!F->hasGC())
858 return true;
859
860 const auto &GCName = F->getGC();
861 if (GCName == "statepoint-example") {
862 auto *PT = cast<PointerType>(this->getType());
863 if (PT->getAddressSpace() != 1)
864 // For the sake of this example GC, we arbitrarily pick addrspace(1) as
865 // our GC managed heap. This must match the same check in
866 // RewriteStatepointsForGC (and probably needs better factored.)
867 return true;
868
869 // It is cheaper to scan for a declaration than to scan for a use in this
870 // function. Note that gc.statepoint is a type overloaded function so the
871 // usual trick of requesting declaration of the intrinsic from the module
872 // doesn't work.
873 for (auto &Fn : *F->getParent())
874 if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)
875 return true;
876 return false;
877 }
878 return true;
879}
880
881uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
882 bool &CanBeNull,
883 bool &CanBeFreed) const {
884 assert(getType()->isPointerTy() && "must be pointer");
885
886 uint64_t DerefBytes = 0;
887 CanBeNull = false;
888 CanBeFreed = UseDerefAtPointSemantics && canBeFreed();
889 if (const Argument *A = dyn_cast<Argument>(this)) {
890 DerefBytes = A->getDereferenceableBytes();
891 if (DerefBytes == 0) {
892 // Handle byval/byref/inalloca/preallocated arguments
893 if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {
894 if (ArgMemTy->isSized()) {
895 // FIXME: Why isn't this the type alloc size?
896 DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinValue();
897 }
898 }
899 }
900
901 if (DerefBytes == 0) {
902 DerefBytes = A->getDereferenceableOrNullBytes();
903 CanBeNull = true;
904 }
905 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
906 DerefBytes = Call->getRetDereferenceableBytes();
907 if (DerefBytes == 0) {
908 DerefBytes = Call->getRetDereferenceableOrNullBytes();
909 CanBeNull = true;
910 }
911 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
912 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
913 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
914 DerefBytes = CI->getLimitedValue();
915 }
916 if (DerefBytes == 0) {
917 if (MDNode *MD =
918 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
919 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
920 DerefBytes = CI->getLimitedValue();
921 }
922 CanBeNull = true;
923 }
924 } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {
925 if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {
926 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
927 DerefBytes = CI->getLimitedValue();
928 }
929 if (DerefBytes == 0) {
930 if (MDNode *MD =
931 IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
932 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
933 DerefBytes = CI->getLimitedValue();
934 }
935 CanBeNull = true;
936 }
937 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
938 if (!AI->isArrayAllocation()) {
939 DerefBytes =
940 DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinValue();
941 CanBeNull = false;
942 CanBeFreed = false;
943 }
944 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
945 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
946 // TODO: Don't outright reject hasExternalWeakLinkage but set the
947 // CanBeNull flag.
948 DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedValue();
949 CanBeNull = false;
950 CanBeFreed = false;
951 }
952 }
953 return DerefBytes;
954}
955
956Align Value::getPointerAlignment(const DataLayout &DL) const {
957 assert(getType()->isPointerTy() && "must be pointer");
958 if (const Function *F = dyn_cast<Function>(this)) {
959 Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
960 switch (DL.getFunctionPtrAlignType()) {
961 case DataLayout::FunctionPtrAlignType::Independent:
962 return FunctionPtrAlign;
963 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
964 return std::max(FunctionPtrAlign, F->getAlign().valueOrOne());
965 }
966 llvm_unreachable("Unhandled FunctionPtrAlignType");
967 } else if (auto *GVar = dyn_cast<GlobalVariable>(this)) {
968 const MaybeAlign Alignment(GVar->getAlign());
969 if (!Alignment) {
970 Type *ObjectType = GVar->getValueType();
971 if (ObjectType->isSized()) {
972 // If the object is defined in the current Module, we'll be giving
973 // it the preferred alignment. Otherwise, we have to assume that it
974 // may only have the minimum ABI alignment.
975 if (GVar->isStrongDefinitionForLinker())
976 return DL.getPreferredAlign(GVar);
977 else
978 return DL.getABITypeAlign(ObjectType);
979 }
980 }
981 return Alignment.valueOrOne();
982 } else if (const Argument *A = dyn_cast<Argument>(this)) {
983 const MaybeAlign Alignment = A->getParamAlign();
984 if (!Alignment && A->hasStructRetAttr()) {
985 // An sret parameter has at least the ABI alignment of the return type.
986 Type *EltTy = A->getParamStructRetType();
987 if (EltTy->isSized())
988 return DL.getABITypeAlign(EltTy);
989 }
990 return Alignment.valueOrOne();
991 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
992 return AI->getAlign();
993 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
994 MaybeAlign Alignment = Call->getRetAlign();
995 if (!Alignment && Call->getCalledFunction())
996 Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment();
997 return Alignment.valueOrOne();
998 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
999 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
1000 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
1001 return Align(CI->getLimitedValue());
1002 }
1003 } else if (auto *CstPtr = dyn_cast<Constant>(this)) {
1004 // Strip pointer casts to avoid creating unnecessary ptrtoint expression
1005 // if the only "reduction" is combining a bitcast + ptrtoint.
1006 CstPtr = CstPtr->stripPointerCasts();
1007 if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
1008 const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
1009 /*OnlyIfReduced=*/true))) {
1010 size_t TrailingZeros = CstInt->getValue().countr_zero();
1011 // While the actual alignment may be large, elsewhere we have
1012 // an arbitrary upper alignmet limit, so let's clamp to it.
1013 return Align(TrailingZeros < Value::MaxAlignmentExponent
1014 ? uint64_t(1) << TrailingZeros
1015 : Value::MaximumAlignment);
1016 }
1017 }
1018 return Align(1);
1019}
1020
1021static std::optional<int64_t>
1022getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
1023 // Skip over the first indices.
1025 for (unsigned i = 1; i != Idx; ++i, ++GTI)
1026 /*skip along*/;
1027
1028 // Compute the offset implied by the rest of the indices.
1029 int64_t Offset = 0;
1030 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
1031 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
1032 if (!OpC)
1033 return std::nullopt;
1034 if (OpC->isZero())
1035 continue; // No offset.
1036
1037 // Handle struct indices, which add their field offset to the pointer.
1038 if (StructType *STy = GTI.getStructTypeOrNull()) {
1039 Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
1040 continue;
1041 }
1042
1043 // Otherwise, we have a sequential type like an array or fixed-length
1044 // vector. Multiply the index by the ElementSize.
1046 if (Size.isScalable())
1047 return std::nullopt;
1048 Offset += Size.getFixedValue() * OpC->getSExtValue();
1049 }
1050
1051 return Offset;
1052}
1053
1054std::optional<int64_t> Value::getPointerOffsetFrom(const Value *Other,
1055 const DataLayout &DL) const {
1056 const Value *Ptr1 = Other;
1057 const Value *Ptr2 = this;
1058 APInt Offset1(DL.getIndexTypeSizeInBits(Ptr1->getType()), 0);
1059 APInt Offset2(DL.getIndexTypeSizeInBits(Ptr2->getType()), 0);
1060 Ptr1 = Ptr1->stripAndAccumulateConstantOffsets(DL, Offset1, true);
1061 Ptr2 = Ptr2->stripAndAccumulateConstantOffsets(DL, Offset2, true);
1062
1063 // Handle the trivial case first.
1064 if (Ptr1 == Ptr2)
1065 return Offset2.getSExtValue() - Offset1.getSExtValue();
1066
1067 const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
1068 const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
1069
1070 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
1071 // base. After that base, they may have some number of common (and
1072 // potentially variable) indices. After that they handle some constant
1073 // offset, which determines their offset from each other. At this point, we
1074 // handle no other case.
1075 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
1076 GEP1->getSourceElementType() != GEP2->getSourceElementType())
1077 return std::nullopt;
1078
1079 // Skip any common indices and track the GEP types.
1080 unsigned Idx = 1;
1081 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
1082 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
1083 break;
1084
1085 auto IOffset1 = getOffsetFromIndex(GEP1, Idx, DL);
1086 auto IOffset2 = getOffsetFromIndex(GEP2, Idx, DL);
1087 if (!IOffset1 || !IOffset2)
1088 return std::nullopt;
1089 return *IOffset2 - *IOffset1 + Offset2.getSExtValue() -
1090 Offset1.getSExtValue();
1091}
1092
1093const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
1094 const BasicBlock *PredBB) const {
1095 auto *PN = dyn_cast<PHINode>(this);
1096 if (PN && PN->getParent() == CurBB)
1097 return PN->getIncomingValueForBlock(PredBB);
1098 return this;
1099}
1100
1101LLVMContext &Value::getContext() const { return VTy->getContext(); }
1102
1103void Value::reverseUseList() {
1104 if (!UseList || !UseList->Next)
1105 // No need to reverse 0 or 1 uses.
1106 return;
1107
1108 Use *Head = UseList;
1109 Use *Current = UseList->Next;
1110 Head->Next = nullptr;
1111 while (Current) {
1112 Use *Next = Current->Next;
1113 Current->Next = Head;
1114 Head->Prev = &Current->Next;
1115 Head = Current;
1116 Current = Next;
1117 }
1118 UseList = Head;
1119 Head->Prev = &UseList;
1120}
1121
1122bool Value::isSwiftError() const {
1123 auto *Arg = dyn_cast<Argument>(this);
1124 if (Arg)
1125 return Arg->hasSwiftErrorAttr();
1126 auto *Alloca = dyn_cast<AllocaInst>(this);
1127 if (!Alloca)
1128 return false;
1129 return Alloca->isSwiftError();
1130}
1131
1132//===----------------------------------------------------------------------===//
1133// ValueHandleBase Class
1134//===----------------------------------------------------------------------===//
1135
1136void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
1137 assert(List && "Handle list is null?");
1138
1139 // Splice ourselves into the list.
1140 Next = *List;
1141 *List = this;
1142 setPrevPtr(List);
1143 if (Next) {
1144 Next->setPrevPtr(&Next);
1145 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
1146 }
1147}
1148
1149void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
1150 assert(List && "Must insert after existing node");
1151
1152 Next = List->Next;
1153 setPrevPtr(&List->Next);
1154 List->Next = this;
1155 if (Next)
1156 Next->setPrevPtr(&Next);
1157}
1158
1159void ValueHandleBase::AddToUseList() {
1160 assert(getValPtr() && "Null pointer doesn't have a use list!");
1161
1162 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1163
1164 if (getValPtr()->HasValueHandle) {
1165 // If this value already has a ValueHandle, then it must be in the
1166 // ValueHandles map already.
1167 ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
1168 assert(Entry && "Value doesn't have any handles?");
1169 AddToExistingUseList(&Entry);
1170 return;
1171 }
1172
1173 // Ok, it doesn't have any handles yet, so we must insert it into the
1174 // DenseMap. However, doing this insertion could cause the DenseMap to
1175 // reallocate itself, which would invalidate all of the PrevP pointers that
1176 // point into the old table. Handle this by checking for reallocation and
1177 // updating the stale pointers only if needed.
1179 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
1180
1181 ValueHandleBase *&Entry = Handles[getValPtr()];
1182 assert(!Entry && "Value really did already have handles?");
1183 AddToExistingUseList(&Entry);
1184 getValPtr()->HasValueHandle = true;
1185
1186 // If reallocation didn't happen or if this was the first insertion, don't
1187 // walk the table.
1188 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
1189 Handles.size() == 1) {
1190 return;
1191 }
1192
1193 // Okay, reallocation did happen. Fix the Prev Pointers.
1195 E = Handles.end(); I != E; ++I) {
1196 assert(I->second && I->first == I->second->getValPtr() &&
1197 "List invariant broken!");
1198 I->second->setPrevPtr(&I->second);
1199 }
1200}
1201
1202void ValueHandleBase::RemoveFromUseList() {
1203 assert(getValPtr() && getValPtr()->HasValueHandle &&
1204 "Pointer doesn't have a use list!");
1205
1206 // Unlink this from its use list.
1207 ValueHandleBase **PrevPtr = getPrevPtr();
1208 assert(*PrevPtr == this && "List invariant broken");
1209
1210 *PrevPtr = Next;
1211 if (Next) {
1212 assert(Next->getPrevPtr() == &Next && "List invariant broken");
1213 Next->setPrevPtr(PrevPtr);
1214 return;
1215 }
1216
1217 // If the Next pointer was null, then it is possible that this was the last
1218 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
1219 // map.
1220 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1222 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
1223 Handles.erase(getValPtr());
1224 getValPtr()->HasValueHandle = false;
1225 }
1226}
1227
1228void ValueHandleBase::ValueIsDeleted(Value *V) {
1229 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
1230
1231 // Get the linked list base, which is guaranteed to exist since the
1232 // HasValueHandle flag is set.
1233 LLVMContextImpl *pImpl = V->getContext().pImpl;
1235 assert(Entry && "Value bit set but no entries exist");
1236
1237 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
1238 // and remove themselves from the list without breaking our iteration. This
1239 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
1240 // Note that we deliberately do not the support the case when dropping a value
1241 // handle results in a new value handle being permanently added to the list
1242 // (as might occur in theory for CallbackVH's): the new value handle will not
1243 // be processed and the checking code will mete out righteous punishment if
1244 // the handle is still present once we have finished processing all the other
1245 // value handles (it is fine to momentarily add then remove a value handle).
1246 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1247 Iterator.RemoveFromUseList();
1248 Iterator.AddToExistingUseListAfter(Entry);
1249 assert(Entry->Next == &Iterator && "Loop invariant broken.");
1250
1251 switch (Entry->getKind()) {
1252 case Assert:
1253 break;
1254 case Weak:
1255 case WeakTracking:
1256 // WeakTracking and Weak just go to null, which unlinks them
1257 // from the list.
1258 Entry->operator=(nullptr);
1259 break;
1260 case Callback:
1261 // Forward to the subclass's implementation.
1262 static_cast<CallbackVH*>(Entry)->deleted();
1263 break;
1264 }
1265 }
1266
1267 // All callbacks, weak references, and assertingVHs should be dropped by now.
1268 if (V->HasValueHandle) {
1269#ifndef NDEBUG // Only in +Asserts mode...
1270 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
1271 << "\n";
1272 if (pImpl->ValueHandles[V]->getKind() == Assert)
1273 llvm_unreachable("An asserting value handle still pointed to this"
1274 " value!");
1275
1276#endif
1277 llvm_unreachable("All references to V were not removed?");
1278 }
1279}
1280
1281void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
1282 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
1283 assert(Old != New && "Changing value into itself!");
1284 assert(Old->getType() == New->getType() &&
1285 "replaceAllUses of value with new value of different type!");
1286
1287 // Get the linked list base, which is guaranteed to exist since the
1288 // HasValueHandle flag is set.
1289 LLVMContextImpl *pImpl = Old->getContext().pImpl;
1290 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
1291
1292 assert(Entry && "Value bit set but no entries exist");
1293
1294 // We use a local ValueHandleBase as an iterator so that
1295 // ValueHandles can add and remove themselves from the list without
1296 // breaking our iteration. This is not really an AssertingVH; we
1297 // just have to give ValueHandleBase some kind.
1298 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1299 Iterator.RemoveFromUseList();
1300 Iterator.AddToExistingUseListAfter(Entry);
1301 assert(Entry->Next == &Iterator && "Loop invariant broken.");
1302
1303 switch (Entry->getKind()) {
1304 case Assert:
1305 case Weak:
1306 // Asserting and Weak handles do not follow RAUW implicitly.
1307 break;
1308 case WeakTracking:
1309 // Weak goes to the new value, which will unlink it from Old's list.
1310 Entry->operator=(New);
1311 break;
1312 case Callback:
1313 // Forward to the subclass's implementation.
1314 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
1315 break;
1316 }
1317 }
1318
1319#ifndef NDEBUG
1320 // If any new weak value handles were added while processing the
1321 // list, then complain about it now.
1322 if (Old->HasValueHandle)
1323 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
1324 switch (Entry->getKind()) {
1325 case WeakTracking:
1326 dbgs() << "After RAUW from " << *Old->getType() << " %"
1327 << Old->getName() << " to " << *New->getType() << " %"
1328 << New->getName() << "\n";
1330 "A weak tracking value handle still pointed to the old value!\n");
1331 default:
1332 break;
1333 }
1334#endif
1335}
1336
1337// Pin the vtable to this file.
1338void CallbackVH::anchor() {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
This file defines the DenseMap class.
uint64_t Align
std::string Name
uint64_t Size
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
uint64_t Offset
Definition: ELF_riscv.cpp:478
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:55
#define I(x, y, z)
Definition: MD5.cpp:58
const uint64_t BitWidth
#define P(N)
static StringRef getName(Value *V)
const NodeList & List
Definition: RDFGraph.cpp:200
Basic Register Allocator
raw_pwrite_stream & OS
static std::optional< int64_t > getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL)
Definition: Value.cpp:1022
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:480
cl::opt< bool > UseDerefAtPointSemantics("use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false), cl::desc("Deref attributes and metadata infer facts at definition only"))
static Type * checkType(Type *Ty)
Definition: Value.cpp:46
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition: Value.cpp:272
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB)
Replace debug record uses of MetadataAsValue(ValueAsMetadata(V)) outside BB with New.
Definition: Value.cpp:587
static bool isUnDroppableUser(const User *U)
Definition: Value.cpp:174
This file defines the SmallString class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
Class for arbitrary precision integers.
Definition: APInt.h:78
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1041
an instruction to allocate memory on the stack
Definition: Instructions.h:64
This class represents an incoming formal argument to a Function.
Definition: Argument.h:32
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
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...
Definition: BasicBlock.cpp:646
iterator end()
Definition: BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:459
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:171
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:384
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1120
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
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:264
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:214
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:169
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:163
This is an important base class in LLVM.
Definition: Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Per-instruction record of debug-info.
LLVM_ABI const BasicBlock * getParent() const
bool erase(const KeyT &Val)
Definition: DenseMap.h:303
unsigned size() const
Definition: DenseMap.h:108
iterator begin()
Definition: DenseMap.h:78
iterator end()
Definition: DenseMap.h:81
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:333
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:340
LLVM_ABI Type * getSourceElementType() const
Definition: Operator.cpp:70
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:531
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
Type * getValueType() const
Definition: GlobalValue.h:298
DenseMap< const Value *, ValueName * > ValueNames
ValueHandlesTy ValueHandles
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
An instruction for reading from memory.
Definition: Instructions.h:180
Metadata node.
Definition: Metadata.h:1077
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:380
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:82
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:151
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:434
Class to represent struct types.
Definition: DerivedTypes.h:218
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:332
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
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:431
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:494
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:311
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
op_range operands()
Definition: User.h:292
Value * getOperand(unsigned i) const
Definition: User.h:232
unsigned getNumOperands() const
Definition: User.h:254
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:115
This is the common base class of value handles.
Definition: ValueHandle.h:30
This class provides a symbol table of name/value pairs.
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1101
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition: COFF.h:862
@ CE
Windows NT (Windows on ARM)
@ Assume
Do not drop type tests (default).
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:663
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:2575
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:2550
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:288
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:1916
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
Definition: DebugInfo.cpp:129
#define N
NodeList * Next
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Used to keep track of an operand bundle.
Definition: InstrTypes.h:2169
StringMapEntry< uint32_t > * Tag
The operand bundle tag, interned by LLVMContextImpl::getOrInsertBundleTag.
Definition: InstrTypes.h:2172
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141