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