60 return Opts.vplan_print_before_all || Opts.vplan_print_after_all ||
61 !Opts.vplan_print_before.empty() || !Opts.vplan_print_after.empty();
67 bool All = After ? Opts.vplan_print_after_all : Opts.vplan_print_before_all;
69 After ? Opts.vplan_print_after : Opts.vplan_print_before;
74 dbgs() <<
"VPlan for loop in '" << Fn->
getName() <<
"' "
75 << (After ?
"after" :
"before") <<
" " << NumberedPassName <<
'\n';
79 dbgs() << Plan <<
'\n';
97 auto IsConsecutiveAccess = [&](
VPValue *Addr,
Type *AccessTy) {
106 if (!VPBB->getParent())
109 auto EndIter = Term ? Term->getIterator() : VPBB->end();
114 VPValue *VPV = Ingredient.getVPSingleValue();
130 IsConsecutiveAccess(VPI->getOperand(0), VPI->getScalarType());
132 *
Load, Ingredient.getOperand(0),
nullptr , IsConsecutive,
133 Load->getAlign(), *VPI, Ingredient.getDebugLoc());
135 bool IsConsecutive = IsConsecutiveAccess(
136 VPI->getOperand(1), VPI->getOperand(0)->getScalarType());
138 *
Store, Ingredient.getOperand(1), Ingredient.getOperand(0),
139 nullptr , IsConsecutive,
Store->getAlign(), *VPI,
140 Ingredient.getDebugLoc());
143 Ingredient.operands(), *VPI,
144 Ingredient.getDebugLoc(),
GEP);
156 if (VectorID == Intrinsic::experimental_noalias_scope_decl)
161 if (VectorID == Intrinsic::assume ||
162 VectorID == Intrinsic::lifetime_end ||
163 VectorID == Intrinsic::lifetime_start ||
164 VectorID == Intrinsic::sideeffect ||
165 VectorID == Intrinsic::pseudoprobe) {
170 const bool IsSingleScalar = VectorID != Intrinsic::assume &&
171 VectorID != Intrinsic::pseudoprobe;
175 Ingredient.getDebugLoc());
177 assert(!CI->hasOperandBundles() &&
178 "calls with operand bundles must not be widened");
180 *CI, VectorID,
drop_end(Ingredient.operands()), CI->getType(),
181 VPIRFlags(*CI), *VPI, CI->getDebugLoc());
185 CI->getOpcode(), Ingredient.getOperand(0), CI->getType(), CI,
189 *VPI, Ingredient.getDebugLoc());
193 "inductions must be created earlier");
202 "Only recpies with zero or one defined values expected");
203 Ingredient.eraseFromParent();
214 const Loop *L =
nullptr;
219 if (
A->getOpcode() != Instruction::Store ||
220 B->getOpcode() != Instruction::Store)
233 const APInt *Distance;
239 Type *TyA =
A->getOperand(0)->getScalarType();
240 uint64_t SizeA =
DL.getTypeStoreSize(TyA);
241 Type *TyB =
B->getOperand(0)->getScalarType();
242 uint64_t SizeB =
DL.getTypeStoreSize(TyB);
247 uint64_t MaxStoreSize = std::max(SizeA, SizeB);
249 auto VFs =
B->getParent()->getPlan()->vectorFactors();
260 : ExcludeRecipes(ExcludeRecipes.begin(), ExcludeRecipes.end()),
261 GroupLeader(GroupLeader), PSE(&PSE), L(&L) {}
270 return ExcludeRecipes.contains(
Store) ||
271 (
Store && isNoAliasViaDistance(
Store, &GroupLeader));
284 std::optional<SinkStoreInfo> SinkInfo = {}) {
285 bool CheckReads = SinkInfo.has_value();
290 if (SinkInfo && SinkInfo->shouldSkip(R))
294 if (!
R.mayWriteToMemory() && !(CheckReads &&
R.mayReadFromMemory()))
313template <
unsigned Opcode>
318 static_assert(Opcode == Instruction::Load || Opcode == Instruction::Store,
319 "Only Load and Store opcodes supported");
320 constexpr bool IsLoad = (Opcode == Instruction::Load);
323 RecipesByAddressAndType;
327 if (RepR.getOpcode() != Opcode || !FilterFn(&RepR))
331 VPValue *Addr = RepR.getOperand(IsLoad ? 0 : 1);
335 RecipesByAddressAndType[{AddrSCEV, LoadStoreTy}].push_back(&RepR);
340 for (
auto &Group :
Groups) {
355 auto InsertIfValidSinkCandidate = [ScalarVFOnly, &WorkList](
362 if (Candidate->getParent() == SinkTo ||
363 all_of(Candidate->operands(),
364 [](
VPValue *
Op) { return Op->isDefinedOutsideLoopRegions(); }) ||
376 WorkList.
insert({SinkTo, Candidate});
388 for (
auto &Recipe : *VPBB)
390 InsertIfValidSinkCandidate(VPBB,
Op);
394 for (
unsigned I = 0;
I != WorkList.
size(); ++
I) {
397 std::tie(SinkTo, SinkCandidate) = WorkList[
I];
402 auto UsersOutsideSinkTo =
404 return cast<VPRecipeBase>(U)->getParent() != SinkTo;
406 if (
any_of(UsersOutsideSinkTo, [SinkCandidate](
VPUser *U) {
407 return !U->usesFirstLaneOnly(SinkCandidate);
410 bool NeedsDuplicating = !UsersOutsideSinkTo.empty();
412 if (NeedsDuplicating) {
416 if (
auto *SinkCandidateRepR =
421 SinkCandidateRepR->getOpcode(), SinkCandidate->
operands(),
422 nullptr, *SinkCandidateRepR, *SinkCandidateRepR,
427 Clone = SinkCandidate->
clone();
437 InsertIfValidSinkCandidate(SinkTo,
Op);
446 if (EntryBB->getNumSuccessors() != 2)
451 if (!Succ0 || !Succ1)
454 if (Succ0->getNumSuccessors() + Succ1->getNumSuccessors() != 1)
456 if (Succ0->getSingleSuccessor() == Succ1)
458 if (Succ1->getSingleSuccessor() == Succ0)
475 if (!Region1->isReplicator())
477 auto *MiddleBasicBlock =
479 if (!MiddleBasicBlock || !MiddleBasicBlock->empty())
484 if (!Region2 || !Region2->isReplicator())
487 VPValue *Mask1 = Region1->getEntryBranchOnMask()->getOperand(0);
488 VPValue *Mask2 = Region2->getEntryBranchOnMask()->getOperand(0);
489 if (!Mask1 || Mask1 != Mask2)
492 assert(Mask1 && Mask2 &&
"both region must have conditions");
498 if (TransformedRegions.
contains(Region1))
505 if (!Then1 || !Then2)
513 std::optional<VPExecutionFrequency> Freq1 =
516 if (Freq1 && Freq2) {
517 if (Freq2->Freq < Freq1->Freq) {
520 Freq1.emplace(Freq1->Freq, Freq1->IsEstimated || Freq2->IsEstimated);
544 VPValue *Phi1ToMoveV = Phi1ToMove.getVPSingleValue();
550 if (Phi1ToMove.getVPSingleValue()->user_empty()) {
551 Phi1ToMove.eraseFromParent();
554 Phi1ToMove.moveBefore(Merge2->begin());
568 TransformedRegions.
insert(Region1);
571 return !TransformedRegions.
empty();
579 std::string RegionName = (
Twine(
"pred.") + Instr->getOpcodeName()).str();
580 assert(Instr->getParent() &&
"Predicated instruction not in any basic block");
581 auto *BlockInMask = PredRecipe->
getMask();
596 BOMRecipe->setExecutionFrequency(RecipeWithoutMask->getExecutionFrequency(),
598 RecipeWithoutMask->clearExecutionFrequency();
607 Region->setParent(ParentRegion);
615 if (RecipeWithoutMask->getScalarType()->isStructTy())
616 Defs =
to_vector(RecipeWithoutMask->users());
618 assert((Def == RecipeWithoutMask ||
619 Def->getOpcode() == Instruction::ExtractValue) &&
620 "only extracts can use a predicated struct");
621 Def->moveBefore(
Pred->end());
622 if (Def->user_empty())
625 Exiting->appendRecipe(PHIRecipe);
626 Def->replaceAllUsesWith(PHIRecipe);
627 PHIRecipe->setOperand(0, Def);
638 if (RepR.isPredicated())
656 if (ParentRegion && ParentRegion->
getExiting() == CurrentBlock)
668 if (!VPBB->getParent())
680 R.moveBefore(PredVPBB->
end());
682 auto *ParentRegion = VPBB->getParent();
683 if (ParentRegion && ParentRegion->getExiting() == VPBB)
684 ParentRegion->setExiting(PredVPBB);
688 return !WorkList.
empty();
695 bool ShouldSimplify =
true;
696 while (ShouldSimplify) {
713 if (
IV.isTruncated())
728 for (
auto *U : FindMyCast->
users()) {
730 if (UserCast && UserCast->getUnderlyingValue() == IRCast) {
731 FoundUserCast = UserCast;
738 FindMyCast = FoundUserCast;
740 if (FindMyCast != &
IV)
765 PhiR->replaceAllUsesWith(PhiR->getOperand(0));
767 PhiR->eraseFromParent();
833 Def->user_empty() || !Def->getUnderlyingValue() ||
834 (RepR && (RepR->isSingleScalar() || RepR->isPredicated())))
847 Def->getUnderlyingInstr()->getOpcode(), Def->operands(),
849 Def->getScalarType(), Def->getUnderlyingInstr());
850 Clone->insertAfter(Def);
851 Def->replaceAllUsesWith(Clone);
852 Def->eraseFromParent();
867 PtrIV->replaceAllUsesWith(PtrAdd);
874 if (HasOnlyVectorVFs &&
none_of(WideIV->users(), [WideIV](
VPUser *U) {
875 return U->usesScalars(WideIV);
886 Plan, ID.getKind(), ID.getInductionOpcode(),
888 WideIV->getScalarType(), WideIV->getStartValue(),
889 WideIV->getStepValue(), WideIV->getDebugLoc(), Builder, WrapFlags);
892 if (!HasOnlyVectorVFs) {
894 "plans containing a scalar VF cannot also include scalable VFs");
895 WideIV->replaceAllUsesWith(Steps);
898 WideIV->replaceUsesWithIf(Steps, [WideIV, HasScalableVF](
VPUser *U) {
900 return U->usesFirstLaneOnly(WideIV);
901 return U->usesScalars(WideIV);
917 return (IntOrFpIV && IntOrFpIV->isTruncated()) ? nullptr : WideIV;
922 if (!Def || Def->getNumOperands() != 2)
930 auto IsWideIVInc = [&]() {
931 auto &ID = WideIV->getInductionDescriptor();
934 VPValue *IVStep = WideIV->getStepValue();
935 switch (ID.getInductionOpcode()) {
936 case Instruction::Add:
938 case Instruction::FAdd:
940 case Instruction::FSub:
943 case Instruction::Sub: {
963 return IsWideIVInc() ? WideIV :
nullptr;
987 VPValue *FirstActiveLane =
B.createFirstActiveLane(Mask,
DL);
989 B.createScalarZExtOrTrunc(FirstActiveLane, CanonicalIVType,
DL);
990 VPValue *EndValue =
B.createAdd(CanonicalIV, FirstActiveLane,
DL);
995 if (Incoming != WideIV) {
997 EndValue =
B.createAdd(EndValue, One,
DL);
1002 VPValue *Start = WideIV->getStartValue();
1003 VPValue *Step = WideIV->getStepValue();
1004 EndValue =
B.createDerivedIV(
1006 Start, EndValue, Step);
1020 if (WideIntOrFp && WideIntOrFp->isTruncated())
1030 Start, VectorTC, Step);
1062 assert(EndValue &&
"Must have computed the end value up front");
1067 if (Incoming != WideIV)
1080 return B.createPtrAdd(EndValue,
B.createSub(
Zero, Step),
1085 return B.createNaryOp(
1086 ID.getInductionBinOp()->getOpcode() == Instruction::FAdd
1088 : Instruction::FAdd,
1089 {EndValue, Step}, {ID.getInductionBinOp()->getFastMathFlags()});
1106 const SCEV *Start, *Step;
1124 VPValue *ExitCount = Builder.createOverflowingOp(
1127 return Builder.createDerivedIV(Kind,
nullptr, StartVPV, ExitCount,
1136 VPBuilder VectorPHBuilder(VectorPH->getFirstNonPhi());
1143 &WideIV, VectorPHBuilder, ResumeTC))
1144 EndValues[&WideIV] = EndValue;
1154 R.getVPSingleValue()->replaceAllUsesWith(EndValue);
1155 R.eraseFromParent();
1162 for (
auto [Idx, PredVPBB] :
enumerate(ExitVPBB->getPredecessors())) {
1164 if (PredVPBB == MiddleVPBB) {
1169 Plan, ExitIRI.getOperand(Idx), PSE, ResumeTC, L);
1175 ExitIRI.setOperand(Idx, Escape);
1199 return Plan.
getZero(Def->getScalarType());
1216 return Def->getOperand(1);
1227 assert(
X->getScalarType()->isIntegerTy(1) &&
"must have boolean operands");
1243 assert(Weights.
size() == 2 &&
"unexpected branch weights");
1245 LLVMContext::MD_prof,
1282 return Plan.
getZero(Def->getScalarType());
1286 Def->getScalarType() ==
A->getScalarType())
1296 if (Def->getScalarType() ==
A->getScalarType())
1306 A->getScalarType() == Def->getScalarType())
1313 return BuildVector->getLastOperand();
1330 return BuildVector->getOperand(BuildVector->getNumOperands() - 2);
1336 return BuildVector->getOperand(Idx);
1340 if (Def->getNumOperands() == 1) {
1341 return Def->getOperand(0);
1345 return Phi->getOperand(0);
1351 if (Def->getNumOperands() == 1 &&
1357 A->getScalarType() == Def->getScalarType())
1369 return Def->getOperand(1);
1381 return VPR->getOperand(0);
1387 return Steps->getOperand(0);
1402struct VPCombineInserter {
1403 SmallVectorImpl<VPSingleDefRecipe *> &Worklist;
1406 R->insertBefore(It);
1418 VPCombineBuilder &Builder) {
1420 Def->replaceAllUsesWith(
V);
1429 RepR && RepR->isPredicated() && RepR->getOpcode() == Instruction::Store &&
1433 RepR->getUnderlyingInstr(), RepR->operandsWithoutMask(),
1434 RepR->isSingleScalar(),
nullptr, *RepR, *RepR,
1435 RepR->getDebugLoc());
1436 Builder.insert(Unmasked);
1448 bool CanCreateNewRecipe =
1454 if (CanCreateNewRecipe &&
1457 return Builder.createLogicalAnd(
X,
Y);
1460 if (CanCreateNewRecipe &&
1465 (!Def->getOperand(0)->hasMoreThanOneUniqueUser() ||
1466 !Def->getOperand(1)->hasMoreThanOneUniqueUser()))
1467 return Builder.createLogicalAnd(
X, Builder.createOr(
Y, Z));
1470 if (CanCreateNewRecipe &&
1474 return Builder.createLogicalOr(Z,
Y);
1478 if (CanCreateNewRecipe &&
1480 return Builder.createNot(
C);
1484 Def->setOperand(0,
C);
1485 Def->setOperand(1,
Y);
1486 Def->setOperand(2,
X);
1492 if (CanCreateNewRecipe &&
1496 Y->getScalarType()->isIntegerTy(1))
1497 return Builder.createOr(
Y, Builder.createLogicalAnd(
X, Z));
1501 if (CanCreateNewRecipe &&
1507 return Builder.createSelect(Builder.createLogicalAnd(Mask0, Mask1),
X,
Y,
1508 Def->getDebugLoc());
1514 Type *TruncTy = Def->getScalarType();
1515 Type *XTy =
X->getScalarType();
1518 unsigned ExtOpcode =
1522 if (
auto *UnderlyingExt =
Y->getUnderlyingValue()) {
1524 Ext->setUnderlyingValue(UnderlyingExt);
1528 auto *
Trunc = Builder.createWidenCast(Instruction::Trunc,
X, TruncTy);
1537 return Builder.createSub(Plan.
getZero(
X->getScalarType()),
X,
1538 Def->getDebugLoc(),
"", NW);
1541 if (CanCreateNewRecipe &&
1549 return Builder.createSub(
X,
Y, Def->getDebugLoc(),
"", NW);
1556 Def->getDebugLoc());
1563 MulR->hasNoSignedWrap() &&
1565 return Builder.createNaryOp(
1568 Def->getDebugLoc());
1573 return Builder.createNaryOp(
1579 if (CanCreateNewRecipe &&
1582 return Builder.createAnd(
1584 Def->getDebugLoc());
1594 return match(U, m_Not(m_Specific(Cmp))) ||
1595 (match(U, m_Select(m_Specific(Cmp), m_VPValue(),
1597 U->getOperand(1) != Cmp && U->getOperand(2) != Cmp);
1604 R->setOperand(1,
Y);
1605 R->setOperand(2,
X);
1610 R->replaceAllUsesWith(Cmp);
1615 if (!Cmp->getDebugLoc() && Def->getDebugLoc())
1616 Cmp->setDebugLoc(Def->getDebugLoc());
1629 if (
Op->getNumUsers() > 1 ||
1633 }
else if (!UnpairedCmp) {
1634 UnpairedCmp =
Op->getDefiningRecipe();
1638 UnpairedCmp =
nullptr;
1645 if (NewOps.
size() < Def->getNumOperands())
1652 if (CanCreateNewRecipe &&
1657 X->getScalarType() ==
Y->getScalarType())
1662 X->getScalarType() != Def->getScalarType())
1663 return Builder.createWidenCast(Instruction::Trunc,
X, Def->getScalarType());
1670 Def->getScalarType()->isIntegerTy(1)) {
1671 Def->setOperand(1, Plan.
getTrue());
1672 Def->setOperand(0,
Y);
1682 Def->replaceUsesWithIf(Def->getOperand(0), [Def](
VPUser *U) {
1683 return U->usesFirstLaneOnly(Def);
1693 "broadcast operand must be single-scalar");
1694 Def->setOperand(0, Z);
1699 Def->replaceUsesWithIf(
1700 X, [Def](
const VPUser *U) {
return U->usesScalars(Def); });
1712 return Builder.createNaryOp(Instruction::ExtractElement, {
X, LaneToExtract},
1713 Def->getDebugLoc());
1725 IVInc->getNumUsers() == 2) {
1731 if ((Phi->getNumUsers() == 1 || (Phi->getNumUsers() == 2 && Inc)) &&
1733 Def->replaceAllUsesWith(IVInc);
1735 Inc->replaceAllUsesWith(Phi);
1736 Phi->setOperand(0,
Y);
1745 Def->replaceUsesWithIf(StartV, [](
const VPUser *U) {
1747 return PhiR && PhiR->isInLoop();
1764 [[maybe_unused]]
unsigned InitWorklistSize = Worklist.
size();
1766 VPCombineBuilder Builder({Worklist});
1767 while (!Worklist.
empty()) {
1768 assert(Worklist.
size() < InitWorklistSize * 2 &&
1769 "Worklist is growing large, possible cycle?");
1771 Builder.setInsertPoint(Def);
1777 Def->replaceAllUsesWith(New);
1778 Def->eraseFromParent();
1782 Def->eraseFromParent();
1800 R.getVPSingleValue()->replaceAllUsesWith(
X);
1816 while (!Worklist.
empty()) {
1825 R->replaceAllUsesWith(
1826 Builder.createLogicalAnd(HeaderMask, Builder.createLogicalAnd(
X,
Y)));
1830static std::optional<Instruction::BinaryOps>
1833 case Intrinsic::masked_udiv:
1834 return Instruction::UDiv;
1835 case Intrinsic::masked_sdiv:
1836 return Instruction::SDiv;
1837 case Intrinsic::masked_urem:
1838 return Instruction::URem;
1839 case Intrinsic::masked_srem:
1840 return Instruction::SRem;
1857 if (RepR && (RepR->isSingleScalar() || RepR->isPredicated()))
1861 if (RepR && RepR->getOpcode() == Instruction::Store &&
1864 RepOrWidenR->getUnderlyingInstr(), RepOrWidenR->operands(),
1865 true ,
nullptr , *RepR ,
1866 *RepR , RepR->getDebugLoc());
1867 Clone->insertBefore(RepOrWidenR);
1869 VPValue *ExtractOp = Clone->getOperand(0);
1875 Clone->setOperand(0, ExtractOp);
1876 RepR->eraseFromParent();
1888 VPValue *SafeDivisor = Builder.createSelect(
1889 IntrR->getOperand(2), IntrR->getOperand(1),
1891 VPValue *Clone = Builder.createNaryOp(
1892 *
Opc, {IntrR->getOperand(0), SafeDivisor},
1895 IntrR->eraseFromParent();
1901 if (!RepOrWidenR->getUnderlyingValue() ||
1906 auto IntroducesBCastOf = [](
const VPValue *
Op) {
1915 return !U->usesScalars(
Op);
1919 if (
any_of(RepOrWidenR->users(), IntroducesBCastOf(RepOrWidenR)) &&
1922 make_filter_range(Op->users(), not_equal_to(RepOrWidenR)),
1923 IntroducesBCastOf(Op)))
1927 bool LiveInNeedsBroadcast =
1928 isa<VPIRValue>(Op) && !isa<VPConstant>(Op);
1929 auto *OpR = dyn_cast<VPReplicateRecipe>(Op);
1930 return LiveInNeedsBroadcast || (OpR && OpR->isSingleScalar());
1938 RepOrWidenR->getUnderlyingInstr());
1939 Clone->insertBefore(RepOrWidenR);
1940 RepOrWidenR->replaceAllUsesWith(Clone);
1942 RepOrWidenR->eraseFromParent();
1975 if (Blend.isNormalized() || !
match(Blend.getMask(0),
m_False()))
1976 UniqueValues.
insert(Blend.getIncomingValue(0));
1977 for (
unsigned I = 1;
I != Blend.getNumIncomingValues(); ++
I)
1979 UniqueValues.
insert(Blend.getIncomingValue(
I));
1981 if (UniqueValues.
size() == 1) {
1982 Blend.replaceAllUsesWith(*UniqueValues.
begin());
1983 Blend.eraseFromParent();
1987 if (Blend.isNormalized())
1993 unsigned StartIndex = 0;
1994 for (
unsigned I = 0;
I != Blend.getNumIncomingValues(); ++
I) {
2006 OperandsWithMask.
push_back(Blend.getIncomingValue(StartIndex));
2008 for (
unsigned I = 0;
I != Blend.getNumIncomingValues(); ++
I) {
2009 if (
I == StartIndex)
2011 OperandsWithMask.
push_back(Blend.getIncomingValue(
I));
2012 OperandsWithMask.
push_back(Blend.getMask(
I));
2017 OperandsWithMask, Blend, Blend.getDebugLoc());
2018 NewBlend->insertBefore(&Blend);
2020 VPValue *DeadMask = Blend.getMask(StartIndex);
2022 Blend.eraseFromParent();
2027 if (NewBlend->getNumOperands() == 3 &&
2029 VPValue *Inc0 = NewBlend->getOperand(0);
2030 VPValue *Inc1 = NewBlend->getOperand(1);
2031 VPValue *OldMask = NewBlend->getOperand(2);
2032 NewBlend->setOperand(0, Inc1);
2033 NewBlend->setOperand(1, Inc0);
2034 NewBlend->setOperand(2, NewMask);
2061 APInt MaxVal = AlignedTC - 1;
2064 unsigned NewBitWidth =
2070 bool MadeChange =
false;
2098 "canonical IV is not expected to have a truncation");
2103 NewWideIV->insertBefore(WideIV);
2110 Cmp->replaceAllUsesWith(
2111 VPBuilder(Cmp).createICmp(Cmp->getPredicate(), NewWideIV, NewBTC));
2125 return any_of(
Cond->getDefiningRecipe()->operands(), [&Plan, BestVF, BestUF,
2127 return isConditionTrueViaVFAndUF(C, Plan, BestVF, BestUF, PSE);
2141 const SCEV *VectorTripCount =
2146 "Trip count SCEV must be computable");
2161 bool MadeChange =
false;
2176 Builder.setInsertPoint(Extract);
2179 Start = Builder.createAdd(
2184 Extract->eraseFromParent();
2203 return !
Offset ||
Offset->isDefinedOutsideLoopRegions();
2222 auto *Term = &ExitingVPBB->
back();
2233 const SCEV *VectorTripCount =
2239 "Trip count SCEV must be computable");
2253 Term->setOperand(1, Plan.
getTrue());
2258 {}, Term->getDebugLoc());
2260 Term->eraseFromParent();
2268 assert(Plan.
hasVF(BestVF) &&
"BestVF is not available in Plan");
2269 assert(Plan.
hasUF(BestUF) &&
"BestUF is not available in Plan");
2285 RecurKind RK = PhiR.getRecurrenceKind();
2292 RecWithFlags->dropPoisonGeneratingFlags();
2299 return MemR->getAlign();
2304struct VPCSEDenseMapInfo :
public DenseMapInfo<VPSingleDefRecipe *> {
2307 static Type *getGEPSourceElementType(
const VPSingleDefRecipe *R) {
2310 return TypeSwitch<const VPSingleDefRecipe *, Type *>(R)
2311 .Case([](
const VPReplicateRecipe *
I) ->
Type * {
2313 return GEP->getSourceElementType();
2316 .Case<VPVectorPointerRecipe, VPWidenGEPRecipe>(
2317 [](
auto *
I) {
return I->getSourceElementType(); })
2318 .
Default([](
auto *) {
return nullptr; });
2322 static bool canHandle(
const VPSingleDefRecipe *Def) {
2329 if (!
C || (!
C->first &&
C->second == Instruction::Alloca))
2334 if (
Def->mayWriteToMemory())
2336 return !
Def->mayReadFromMemory() ||
2337 (!
C->first &&
C->second == Instruction::Load);
2341 static unsigned getHashValue(
const VPSingleDefRecipe *Def) {
2344 getGEPSourceElementType(Def),
Def->getScalarType(),
2347 if (RFlags->hasPredicate())
2350 return hash_combine(Result, SIVSteps->getInductionOpcode());
2352 return hash_combine(Result, DIV->getInductionKind(), DIV->getFPBinOp());
2363 static bool isEqual(
const VPSingleDefRecipe *L,
const VPSingleDefRecipe *R) {
2364 if (
L->getVPRecipeID() !=
R->getVPRecipeID() ||
2367 getGEPSourceElementType(L) != getGEPSourceElementType(R) ||
2369 !
equal(
L->operands(),
R->operands()))
2373 "must have valid opcode info for both recipes");
2375 if (LFlags->hasPredicate() &&
2376 LFlags->getPredicate() !=
2380 if (LSIV->getInductionOpcode() !=
2385 if (LDIV->getInductionKind() != RDIV->getInductionKind() ||
2386 LDIV->getFPBinOp() != RDIV->getFPBinOp())
2405 const VPRegionBlock *RegionL =
L->getRegion();
2406 const VPRegionBlock *RegionR =
R->getRegion();
2409 L->getParent() !=
R->getParent())
2411 return L->getScalarType() ==
R->getScalarType();
2429 if (R.mayWriteToMemory())
2432 if (!Def || !VPCSEDenseMapInfo::canHandle(Def))
2435 auto [It, Inserted] =
2436 (IsLoad ? LoadCSEMap : CSEMap).try_emplace(Def, Def);
2441 if (!VPDT.
dominates(V->getParent(), VPBB))
2455 if (DefExp && DefExp->expandInIR() &&
2466 Def->replaceAllUsesWith(V);
2480 bool Sinking =
false) {
2515 "Expected vector prehader's successor to be the vector loop region");
2523 return !Op->isDefinedOutsideLoopRegions();
2529 R.moveBefore(Preheader->
end());
2547 assert(!RepR->isPredicated() &&
2548 "Expected prior transformation of predicated replicates to "
2549 "replicate regions");
2554 if (!RepR->isSingleScalar())
2558 if (RepR->getOpcode() == Instruction::Store &&
2559 !RepR->getOperand(1)->isDefinedOutsideLoopRegions())
2567 if (
any_of(Def->users(), [&SinkBB, &LoopRegion](
VPUser *U) {
2568 auto *UserR = cast<VPRecipeBase>(U);
2569 VPBasicBlock *Parent = UserR->getParent();
2571 if (SinkBB && SinkBB != Parent)
2576 return UserR->isPhi() || Parent->getEnclosingLoopRegion() ||
2577 Parent->getSinglePredecessor() != LoopRegion;
2587 assert((!R.mayWriteToMemory() ||
2588 (RepR && RepR->getOpcode() == Instruction::Store &&
2589 RepR->getOperand(1)->isDefinedOutsideLoopRegions())) &&
2590 "The only recipes that may write to memory are expected to be "
2591 "stores with invariant pointer-operand");
2599 "Defining block must dominate sink block");
2626 VPValue *ResultVPV = R.getVPSingleValue();
2628 unsigned NewResSizeInBits = MinBWs.
lookup(UI);
2629 if (!NewResSizeInBits)
2642 (void)OldResSizeInBits;
2650 VPW->dropPoisonGeneratingFlags();
2652 assert((OldResSizeInBits != NewResSizeInBits ||
2654 "Only ICmps should not need extending the result.");
2667 unsigned OpSizeInBits =
Op->getScalarType()->getScalarSizeInBits();
2668 if (OpSizeInBits == NewResSizeInBits)
2670 assert(OpSizeInBits > NewResSizeInBits &&
"nothing to truncate");
2671 auto [ProcessedIter, Inserted] =
2676 Builder.setInsertPoint(PH);
2678 Builder.setInsertPoint(&R);
2679 ProcessedIter->second =
2680 Builder.createWidenCast(Instruction::Trunc,
Op, NewResTy);
2682 Op = ProcessedIter->second;
2686 NWR->insertBefore(&R);
2691 VPValue *Replacement = NWR->getVPSingleValue();
2698 R.eraseFromParent();
2704 std::optional<VPDominatorTree> VPDT;
2712 bool SimplifiedPhi =
false;
2722 assert(VPBB->getNumSuccessors() == 2 &&
2723 "Two successors expected for BranchOnCond");
2724 unsigned RemovedIdx;
2735 "There must be a single edge between VPBB and its successor");
2740 SimplifiedPhi =
true;
2744 if (!PhiR || PhiR->getNumIncoming() != 1)
2746 PhiR->replaceAllUsesWith(PhiR->getOperand(0));
2747 PhiR->eraseFromParent();
2752 VPBB->back().eraseFromParent();
2764 if (Reachable.contains(
B))
2775 for (
VPValue *Def : R.definedValues())
2776 Def->replaceAllUsesWith(&Tmp);
2777 R.eraseFromParent();
2781 return SimplifiedPhi;
2809 auto GetSimplifiedLiveInViaSCEV = [&](
VPValue *VPV) ->
VPValue * {
2818 if (
VPValue *SimplifiedLiveIn = GetSimplifiedLiveInViaSCEV(LiveIn))
2819 LiveIn->replaceAllUsesWith(SimplifiedLiveIn);
2830 "expected to run before loop regions are created");
2832 auto CanUseVersionedStride = [&VPDT, Header = Header, &Plan](
VPUser *U) {
2838 return VPDT.
dominates(Header, R->getParent());
2842 Value *StrideV = Stride->getValue();
2843 const APInt *StrideConst;
2850 CanUseVersionedStride);
2864 CanUseVersionedStride);
2866 RewriteMap[StrideV] = StrideExpr;
2872 const SCEV *ScevExpr = ExpSCEV.getSCEV();
2873 const SCEV *NewSCEV =
2878 ExpSCEV.replaceAllUsesWith(NewExp);
2888 auto CollectPoisonGeneratingInstrsInBackwardSlice([&](
VPRecipeBase *Root) {
2893 while (!Worklist.
empty()) {
2896 if (!Visited.
insert(CurRec).second)
2918 RecWithFlags->isDisjoint()) {
2921 Builder.createAdd(
A,
B, RecWithFlags->getDebugLoc());
2922 New->setUnderlyingValue(RecWithFlags->getUnderlyingValue());
2923 RecWithFlags->replaceAllUsesWith(New);
2924 RecWithFlags->eraseFromParent();
2927 RecWithFlags->dropPoisonGeneratingFlags();
2932 assert((!Instr || !Instr->hasPoisonGeneratingFlags()) &&
2933 "found instruction with poison generating flags not covered by "
2934 "VPRecipeWithIRFlags");
2939 if (
VPRecipeBase *OpDef = Operand->getDefiningRecipe())
2961 VPRecipeBase *AddrDef = WidenRec->getAddr()->getDefiningRecipe();
2962 if (AddrDef && WidenRec->isConsecutive() && WidenRec->getMask() &&
2963 match(WidenRec->getMask(), m_UnlessHdrMask))
2964 CollectPoisonGeneratingInstrsInBackwardSlice(AddrDef);
2966 VPRecipeBase *AddrDef = InterleaveRec->getAddr()->getDefiningRecipe();
2967 if (AddrDef && InterleaveRec->getMask() &&
2968 match(InterleaveRec->getMask(), m_UnlessHdrMask))
2969 CollectPoisonGeneratingInstrsInBackwardSlice(AddrDef);
2979 const bool &EpilogueAllowed) {
2980 if (InterleaveGroups.empty())
2989 IRMemberToRecipe[&MemR->getIngredient()] = MemR;
2995 for (
const auto *IG : InterleaveGroups) {
2998 for (
auto *Member : IG->members())
3000 StartMember = Member;
3008 for (
unsigned I = 0;
I < IG->getFactor(); ++
I) {
3014 StoredValues.
push_back(StoreR->getStoredValue());
3021 bool NeedsMaskForGaps =
3022 (IG->requiresScalarEpilogue() && !EpilogueAllowed) ||
3023 (!StoredValues.
empty() && !IG->isFull());
3026 auto *InsertPos = IRMemberToRecipe.
lookup(IRInsertPos);
3030 "Dead member in non-load group?");
3035 InsertPos->getAsRecipe()))
3036 InsertPos = MemberR;
3037 IRInsertPos = &InsertPos->getIngredient();
3047 VPValue *Addr = Start->getAddr();
3049 if (IG->getIndex(StartMember) != 0 ||
3057 assert(IG->getIndex(IRInsertPos) != 0 &&
3058 "index of insert position shouldn't be zero");
3062 IG->getIndex(IRInsertPos),
3066 Addr =
B.createNoWrapPtrAdd(InsertPos->getAddr(), OffsetVPV, NW);
3072 if (IG->isReverse()) {
3075 -(int64_t)IG->getFactor(), NW, InsertPosR->
getDebugLoc());
3076 ReversePtr->insertBefore(InsertPosR);
3080 IG, Addr, StoredValues, InsertPos->getMask(), NeedsMaskForGaps,
3082 VPIG->insertBefore(InsertPosR);
3085 for (
unsigned i = 0; i < IG->getFactor(); ++i)
3088 if (!Member->getType()->isVoidTy()) {
3110struct CountableConditionMatch {
3112 PredicatedScalarEvolution &PSE;
3115 CountableConditionMatch(VPValue *&Cmp, PredicatedScalarEvolution &PSE,
3119 template <
typename ITy>
bool match(ITy *
V)
const {
3139 return CountableConditionMatch(Cmp, PSE, L);
3158 VPValue *Uncountable =
nullptr;
3175 if (ExitBlocks.
size() != 1)
3182 if (!ExitBlocks.
front()->phis().empty())
3198 LatchVPBB->clearSuccessors();
3208 Term->setOperand(0, Countable);
3272 VPValue *UncountableCondition =
nullptr;
3279 Worklist.
push_back(UncountableCondition);
3280 while (!Worklist.
empty()) {
3284 if (
V->isDefinedOutsideLoopRegions())
3317 if (Recipes.
empty() ||
3321 return UncountableCondition;
3376 for (
auto &Exit : Exits) {
3377 if (Exit.EarlyExitingVPBB == LatchVPBB)
3382 ExitIRI.removeIncomingValueFor(Exit.EarlyExitingVPBB);
3383 Exit.EarlyExitingVPBB->getTerminator()->eraseFromParent();
3397 "loop with side effects",
3398 "EarlyExitSideEffectsCond", ORE, TheLoop);
3413 assert(
Load &&
"Couldn't find exactly one load");
3416 "Uncountable exit condition load is conditional.");
3430 DL.getTypeStoreSize(
Load->getScalarType()).getFixedValue());
3436 "load used by the exit condition that may "
3438 "EarlyExitSideEffectsFaultingLoad", ORE,
3451 "load used by the exit condition with an "
3452 "unsupported memory access pattern",
3453 "EarlyExitSideEffectsBadLoadAccessPattern", ORE,
3461 "load used by the exit condition with an "
3462 "unsupported memory access pattern",
3463 "EarlyExitSideEffectsBadLoadAccessPattern", ORE,
3473 while (InsertIt != HeaderVPBB->
end() &&
3475 erase(ConditionRecipes, &*InsertIt);
3478 for (
auto *Recipe :
reverse(ConditionRecipes))
3479 Recipe->moveBefore(InsertIt);
3490 if (R.mayReadOrWriteMemory() && &R !=
Load) {
3492 if (!VPDT.
dominates(R.getParent(), LatchVPBB)) {
3494 "Early exit loop with side effects contains unsupported "
3495 "conditional memory operations",
3496 "EarlyExitSideEffectsUnsupportedConditionalMemOps", ORE, TheLoop);
3507 "Expected BranchOnCond terminator for MiddleVPBB");
3511 Type *IVScalarTy =
IV->getScalarType();
3527 auto Phis = ScalarPH->
phis();
3532 "Early exit loop with side effects contains "
3533 "unsupported reductions, inductions or recurrences",
3534 "EarlyExitSideEffectsReductions", ORE, TheLoop);
3542 "Continuing from different IV");
3556 if (!R.mayWriteToMemory()) {
3558 R.isSafeToSpeculativelyExecute()) &&
3559 "legality only accepts recipes with side effects that are "
3560 "safe to speculate");
3564 "legality only accepts stores as writes in early-exit loops");
3590 "Auto-vectorization of early exit loops with potentially "
3591 "faulting loads is not supported",
3592 "EarlyExitFaultingLoads", ORE, TheLoop);
3596 VPBuilder LatchBuilder(LatchVPBB->getTerminator());
3598 for (
auto [EarlyExitingVPBB, ExitBlock] :
3605 VPValue *ExitV = ExitIRI.getIncomingValueForBlock(EarlyExitingVPBB);
3608 if (ExitV != PhiR.getBackedgeValue())
3610 RecurKind Kind = PhiR.getRecurrenceKind();
3611 if (PhiR.isOrdered() || PhiR.hasUsesOutsideReductionChain() ||
3615 "Found reduction used on an early exit",
3616 "Cannot vectorize early exit loop with reduction used on an "
3618 "ReductionUsedOnEarlyExit", ORE, TheLoop);
3626 VPValue *CondOfEarlyExitingVPBB;
3627 [[maybe_unused]]
bool Matched =
3628 match(EarlyExitingVPBB->getTerminator(),
3630 assert(Matched &&
"Terminator must be BranchOnCond");
3634 VPBuilder EarlyExitingBuilder(EarlyExitingVPBB->getTerminator());
3635 auto *CondToEarlyExit = EarlyExitingBuilder.
createNaryOp(
3637 TrueSucc == ExitBlock
3638 ? CondOfEarlyExitingVPBB
3639 : EarlyExitingBuilder.
createNot(CondOfEarlyExitingVPBB));
3645 "exit condition must dominate the latch");
3653 assert(!Exits.
empty() &&
"must have at least one early exit");
3660 for (
const auto &[Num, VPB] :
enumerate(RPOT))
3663 return RPOIdx[
A.EarlyExitingVPBB] < RPOIdx[
B.EarlyExitingVPBB];
3669 for (
unsigned I = 0;
I + 1 < Exits.
size(); ++
I)
3670 for (
unsigned J =
I + 1; J < Exits.
size(); ++J)
3672 Exits[
I].EarlyExitingVPBB) &&
3673 "RPO sort must place dominating exits before dominated ones");
3679 VPValue *Combined = Exits[0].CondToExit;
3701 "Unexpected terminator");
3702 VPValue *IsLatchExitTaken = LatchExitingBranch->getOperand(0);
3703 DebugLoc LatchDL = LatchExitingBranch->getDebugLoc();
3704 LatchExitingBranch->eraseFromParent();
3707 {IsAnyExitTaken, IsLatchExitTaken}, LatchDL);
3708 LatchVPBB->clearSuccessors();
3713 LatchVPBB->setSuccessors({MiddleVPBB, MiddleVPBB, HeaderVPBB});
3714 MiddleVPBB->clearPredecessors();
3715 MiddleVPBB->setPredecessors({LatchVPBB, LatchVPBB});
3717 LatchVPBB, MiddleVPBB, ORE,
3718 TheLoop, PSE, DT, AC);
3723 for (
unsigned Idx = 0; Idx != Exits.
size(); ++Idx) {
3727 VectorEarlyExitVPBBs[Idx] = VectorEarlyExitVPBB;
3735 Exits.
size() == 1 ? VectorEarlyExitVPBBs[0]
3738 LatchVPBB->setSuccessors({DispatchVPBB, MiddleVPBB, HeaderVPBB});
3770 for (
auto [Exit, VectorEarlyExitVPBB] :
3771 zip_equal(Exits, VectorEarlyExitVPBBs)) {
3772 auto &[EarlyExitingVPBB, EarlyExitVPBB,
_] = Exit;
3782 VPValue *IncomingVal = ExitIRI.getIncomingValueForBlock(EarlyExitingVPBB);
3783 VPValue *NewIncoming = IncomingVal;
3785 VPBuilder EarlyExitBuilder(VectorEarlyExitVPBB);
3790 ExitIRI.removeIncomingValueFor(EarlyExitingVPBB);
3791 ExitIRI.addIncoming(NewIncoming);
3794 EarlyExitingVPBB->getTerminator()->eraseFromParent();
3829 bool IsLastDispatch = (
I + 2 == Exits.
size());
3831 IsLastDispatch ? VectorEarlyExitVPBBs.
back()
3837 VectorEarlyExitVPBBs[
I]->setPredecessors({CurrentBB});
3840 CurrentBB = FalseBB;
3855 VPValue *VecOp = Red->getVecOp();
3858 if (Red->isPartialReduction())
3862 auto IsExtendedRedValidAndClampRange =
3875 "getExtendedReductionCost only supports integer types");
3876 ExtRedCost = Ctx.TTI.getExtendedReductionCost(
3877 Opcode, ExtOpc == Instruction::CastOps::ZExt, RedTy, SrcVecTy,
3878 Red->getFastMathFlagsOrNone(), CostKind);
3879 return ExtRedCost.
isValid() && ExtRedCost < ExtCost + RedCost;
3887 IsExtendedRedValidAndClampRange(
3907 Type *LHSSrcTy =
LHS->getOperand(0)->getScalarType();
3909 Type *RHSSrcTy =
RHS ?
RHS->getOperand(0)->getScalarType() : LHSSrcTy;
3911 if (
Mul->getScalarType()->getScalarSizeInBits() >=
3914 if (ExtKind == ExtendKind::PR_ZeroExtend)
3915 return Mul->hasNoUnsignedWrap();
3916 return ExtKind == ExtendKind::PR_SignExtend &&
Mul->hasNoSignedWrap();
3932 if (Opcode != Instruction::Add && Opcode != Instruction::Sub &&
3933 Opcode != Instruction::FAdd)
3937 if (Red->isPartialReduction())
3943 auto IsMulAccValidAndClampRange =
3955 (Ext0->getOpcode() != Ext1->getOpcode() ||
3956 Ext0->getOpcode() == Instruction::CastOps::FPExt))
3960 !Ext0 || Ext0->getOpcode() == Instruction::CastOps::ZExt;
3962 MulAccCost = Ctx.TTI.getMulAccReductionCost(IsZExt, Opcode, RedTy,
3963 SrcVecTy, CostKind);
3969 ExtCost += Ext0->computeCost(VF, Ctx);
3971 ExtCost += Ext1->computeCost(VF, Ctx);
3973 ExtCost += OuterExt->computeCost(VF, Ctx);
3975 return MulAccCost.
isValid() &&
3976 MulAccCost < ExtCost + MulCost + RedCost;
3981 VPValue *VecOp = Red->getVecOp();
4019 Builder.createWidenCast(Instruction::CastOps::Trunc, ValB, NarrowTy);
4021 ValB = ExtB = Builder.createWidenCast(ExtOpc,
Trunc, WideTy);
4022 Mul->setOperand(1, ExtB);
4032 ExtendAndReplaceConstantOp(RecipeA, RecipeB,
B,
Mul);
4037 IsMulAccValidAndClampRange(
Mul, RecipeA, RecipeB,
nullptr)) {
4044 if (!
Sub && IsMulAccValidAndClampRange(
Mul,
nullptr,
nullptr,
nullptr))
4061 ExtendAndReplaceConstantOp(Ext0, Ext1,
B,
Mul);
4070 (Ext->getOpcode() == Ext0->getOpcode() ||
4071 (Ext0 == Ext1 && Ext0->getOpcode() == Instruction::SExt)) &&
4072 Ext0->getOpcode() == Ext1->getOpcode() &&
Mul->hasOneUse() &&
4074 IsMulAccValidAndClampRange(
Mul, Ext0, Ext1, Ext)) {
4076 Ext0->getOpcode(), Ext0->getOperand(0), Ext->getScalarType(),
nullptr,
4077 *Ext0, *Ext0, Ext0->getDebugLoc());
4078 NewExt0->insertBefore(Ext0);
4083 Ext->getScalarType(),
nullptr, *Ext1,
4084 *Ext1, Ext1->getDebugLoc());
4087 auto *NewMul =
Mul->cloneWithOperands({NewExt0, NewExt1});
4088 NewMul->insertBefore(
Mul);
4089 Ext->replaceAllUsesWith(NewMul);
4090 Ext->eraseFromParent();
4091 Mul->eraseFromParent();
4105 if (Red->isPartialReduction())
4109 auto IP = std::next(Red->getIterator());
4119 Red->replaceAllUsesWith(AbstractR);
4141 return CommonMetadata;
4144template <
unsigned Opcode>
4149 static_assert(Opcode == Instruction::Load || Opcode == Instruction::Store,
4150 "Only Load and Store opcodes supported");
4157 for (
auto Recipes :
Groups) {
4158 if (Recipes.size() < 2)
4162 "Expected all recipes in group to have the same load-store type");
4169 VPValue *MaskI = RecipeI->getMask();
4175 bool HasComplementaryMask =
false;
4180 VPValue *MaskJ = RecipeJ->getMask();
4189 if (HasComplementaryMask) {
4190 assert(Group.
size() >= 2 &&
"must have at least 2 entries");
4200template <
typename InstType>
4218 for (
auto &Group :
Groups) {
4238 return R->isSingleScalar() == IsSingleScalar;
4240 "all members in group must agree on IsSingleScalar");
4245 LoadWithMinAlign->getUnderlyingInstr(), {EarliestLoad->getOperand(0)},
4246 IsSingleScalar,
nullptr, *EarliestLoad, CommonMetadata);
4248 UnpredicatedLoad->insertBefore(EarliestLoad);
4252 Load->replaceAllUsesWith(UnpredicatedLoad);
4253 Load->eraseFromParent();
4262 if (!StoreLoc || !StoreLoc->AATags.Scope)
4269 SinkStoreInfo SinkInfo(StoresToSink, *StoresToSink[0], PSE, L);
4281 for (
auto &Group :
Groups) {
4293 VPValue *SelectedValue = Group[0]->getOperand(0);
4296 bool IsSingleScalar = Group[0]->isSingleScalar();
4297 for (
unsigned I = 1;
I < Group.size(); ++
I) {
4298 assert(IsSingleScalar == Group[
I]->isSingleScalar() &&
4299 "all members in group must agree on IsSingleScalar");
4300 VPValue *Mask = Group[
I]->getMask();
4302 SelectedValue = Builder.createSelect(
4305 Value->getScalarType()));
4313 StoreWithMinAlign->getUnderlyingInstr(),
4314 {SelectedValue, LastStore->getOperand(1)}, IsSingleScalar,
4315 nullptr, *LastStore, CommonMetadata);
4316 UnpredicatedStore->insertBefore(LastStore);
4320 Store->eraseFromParent();
4327 assert(UF > 1 &&
"Expected plan to have an UF > 1");
4337 VPValue *StoredValue =
nullptr;
4348 assert(
MemOp->isConsecutive() &&
"Expected consecutive load/store");
4355 auto MaskSource =
MemOp->isMasked()
4360 : R.getVPSingleValue()->getScalarType();
4362 std::optional<Instruction::CastOps> CastHint;
4364 : R.getVPSingleValue()->getSingleUser();
4366 CastHint = Cast->getOpcode();
4369 if (!
TTI.hasMultiVectorLoadStore(UF, MaskSource,
4370 VectorAccessType, IsStore, CastHint))
4380 VPValue *WideStoredValue = Builder.createNaryOp(
4382 NewMemOp = Builder.createNaryOp(
4384 {Multiplier, Ptr,
Align, WideStoredValue},
nullptr, {}, *
MemOp,
DL);
4386 VPValue *OldLoad = R.getVPSingleValue();
4387 NewMemOp = Builder.createNaryOp(
4396 R.eraseFromParent();
4398 assert((!MaskPhi || Mask == MaskPhi) &&
"Expected a single mask phi");
4419 "Expected incoming values of Phi to be WideActiveLaneMask");
4434 "Expected entry/loop extract-vector-for-part ops");
4435 EntryMask->replaceAllUsesWith(EntryALM);
4436 LoopMask->replaceAllUsesWith(LoopALM);
4455 if (Member0Op == OpV)
4465 return !
IsScalable && !W->getMask() && W->isConsecutive() &&
4468 return IR->getInterleaveGroup()->isFull() &&
IR->getVPValue(Idx) == OpV;
4487 if (R->getScalarType() != WideMember0->getScalarType())
4489 if (R->hasPredicate() && R->getPredicate() != WideMember0->getPredicate())
4497 if (
V->isDefinedOutsideLoopRegions())
4499 auto [It, Inserted] = FirstMembersOf.try_emplace(
V,
Ops);
4500 if (!Inserted && (It->second.front() ==
V ||
V == WideMember0) &&
4505 for (
unsigned Idx = 0; Idx != WideMember0->getNumOperands(); ++Idx) {
4508 OpsI.
push_back(
Op->getDefiningRecipe()->getOperand(Idx));
4514 const auto &[OpIdx, OpV] =
P;
4526static std::optional<ElementCount>
4530 if (!InterleaveR || InterleaveR->
getMask())
4531 return std::nullopt;
4533 Type *GroupElementTy =
nullptr;
4537 return Op->getScalarType() == GroupElementTy;
4539 return std::nullopt;
4543 return Op->getScalarType() == GroupElementTy;
4545 return std::nullopt;
4549 if (IG->getFactor() != IG->getNumMembers() || IG->isReverse())
4550 return std::nullopt;
4556 assert(
Size.isScalable() == VF.isScalable() &&
4557 "if Size is scalable, VF must be scalable and vice versa");
4558 return Size.getKnownMinValue();
4562 unsigned MinVal = VF.getKnownMinValue();
4564 if (IG->getFactor() == MinVal && GroupSize == GetVectorBitWidthForVF(VF))
4567 return std::nullopt;
4575 return RepR && RepR->isSingleScalar();
4589 if (
V->isDefinedOutsideLoopRegions()) {
4592 return M->isDefinedOutsideLoopRegions() &&
4593 M->getScalarType() ==
V->getScalarType();
4595 "expected distinct loop-invariant values of matching scalar type");
4610 for (
unsigned Idx = 0,
E = WideMember0->getNumOperands(); Idx !=
E; ++Idx) {
4612 for (
VPValue *Member : Members)
4613 OpsI.
push_back(Member->getDefiningRecipe()->getOperand(Idx));
4614 WideMember0->setOperand(
4626 *LI, LoadGroup->getAddr(), LoadGroup->getMask(),
true,
4627 IG->
getAlign(), *LoadGroup, LoadGroup->getDebugLoc());
4633 VPValue *PtrOp = WideLoad->getAddr();
4635 PtrOp = VecPtr->getOperand(0);
4640 nullptr, {}, *WideLoad);
4641 N->insertBefore(WideLoad);
4646std::unique_ptr<VPlan>
4666 "unexpected branch-on-count");
4670 std::optional<ElementCount> VFToOptimize;
4684 if (R.mayWriteToMemory() && !InterleaveR)
4690 return any_of(V->users(), [&](VPUser *U) {
4691 auto *UR = cast<VPRecipeBase>(U);
4692 return UR->getParent()->getParent() != VectorLoop;
4709 std::optional<ElementCount> NarrowedVF =
4711 if (!NarrowedVF || (VFToOptimize && NarrowedVF != VFToOptimize))
4713 VFToOptimize = NarrowedVF;
4716 if (InterleaveR->getStoredValues().empty())
4721 auto *Member0 = InterleaveR->getStoredValues()[0];
4732 if (LoadGroup && LoadGroup->getInterleaveGroup()->isFull() &&
4734 return LoadGroup->getVPValue(Op.index()) == Op.value();
4743 VFToOptimize->isScalable(), FirstMembersOf))
4748 if (StoreGroups.empty())
4752 bool RequiresScalarEpilogue =
4763 std::unique_ptr<VPlan> NewPlan;
4765 NewPlan = std::unique_ptr<VPlan>(Plan.
duplicate());
4766 Plan.
setVF(*VFToOptimize);
4767 NewPlan->removeVF(*VFToOptimize);
4774 for (
auto *StoreGroup : StoreGroups) {
4776 NarrowedOps, Preheader);
4782 StoreGroup->getDebugLoc());
4789 Type *CanIVTy = VectorLoop->getCanonicalIVType();
4795 if (VFToOptimize->isScalable()) {
4798 Step = PHBuilder.createOverflowingOp(Instruction::Mul, {VScale,
UF},
4806 materializeVectorTripCount(Plan, VectorPH,
false,
4807 RequiresScalarEpilogue, Step);
4812 removeDeadRecipes(Plan);
4815 "All VPVectorPointerRecipes should have been removed");
4833 "Cannot handle loops with uncountable early exits");
4840 assert(RecurSplice &&
"expected FirstOrderRecurrenceSplice");
4847 if (
any_of(RecurSplice->users(),
4848 [](
VPUser *U) { return !cast<VPRecipeBase>(U)->getRegion(); }) &&
4929 {},
"vector.recur.extract.for.phi");
4932 ExitPhi->replaceUsesOfWith(ExtractR, PenultimateElement);
4946 VPValue *WidenIVCandidate = BinOp->getOperand(0);
4947 VPValue *InvariantCandidate = BinOp->getOperand(1);
4949 std::swap(WidenIVCandidate, InvariantCandidate);
4963 auto *ClonedOp = BinOp->
clone();
4964 if (ClonedOp->getOperand(0) == WidenIV) {
4965 ClonedOp->setOperand(0, ScalarIV);
4967 assert(ClonedOp->getOperand(1) == WidenIV &&
"one operand must be WideIV");
4968 ClonedOp->setOperand(1, ScalarIV);
4982 return std::nullopt;
4987 return std::nullopt;
4999 auto CheckSentinel = [&SE](
const SCEV *IVSCEV,
5000 bool UseMax) -> std::optional<APSInt> {
5002 for (
bool Signed : {
true,
false}) {
5011 return std::nullopt;
5019 PhiR->getRecurrenceKind()))
5028 VPValue *BackedgeVal = PhiR->getBackedgeValue();
5042 !
match(FindLastSelect,
5051 IVOfExpressionToSink ? IVOfExpressionToSink : FindLastExpression, PSE,
5056 "IVOfExpressionToSink not being an AddRec must imply "
5057 "FindLastExpression not being an AddRec.");
5066 bool UseMax = *StepDirection;
5067 std::optional<APSInt> SentinelVal = CheckSentinel(IVSCEV, UseMax);
5068 bool UseSigned = SentinelVal && SentinelVal->isSigned();
5075 if (IVOfExpressionToSink) {
5076 const SCEV *FindLastExpressionSCEV =
5078 if (std::optional<bool> NewUseMax =
5080 if (
auto NewSentinel =
5081 CheckSentinel(FindLastExpressionSCEV, *NewUseMax)) {
5084 SentinelVal = *NewSentinel;
5085 UseSigned = NewSentinel->isSigned();
5086 UseMax = *NewUseMax;
5087 IVSCEV = FindLastExpressionSCEV;
5088 IVOfExpressionToSink =
nullptr;
5098 if (AR->hasNoSignedWrap())
5100 else if (AR->hasNoUnsignedWrap())
5110 VPValue *NewFindLastSelect = BackedgeVal;
5112 if (!SentinelVal || IVOfExpressionToSink) {
5115 DebugLoc DL = FindLastSelect->getDefiningRecipe()->getDebugLoc();
5116 VPBuilder LoopBuilder(FindLastSelect->getDefiningRecipe());
5117 if (
match(FindLastSelect,
5119 SelectCond = LoopBuilder.
createNot(SelectCond);
5126 if (SelectCond !=
Cond || IVOfExpressionToSink) {
5129 IVOfExpressionToSink ? IVOfExpressionToSink : FindLastExpression,
5138 VPIRFlags Flags(MinMaxKind,
false,
false,
5144 NewFindLastSelect, Flags, ExitDL);
5147 VPValue *VectorRegionExitingVal = ReducedIV;
5148 bool SunkExpression =
false;
5149 if (IVOfExpressionToSink) {
5150 VectorRegionExitingVal =
5152 ReducedIV, IVOfExpressionToSink);
5153 SunkExpression =
true;
5157 VPValue *StartVPV = PhiR->getStartValue();
5164 NewRdxResult = MiddleBuilder.
createSelect(Cmp, VectorRegionExitingVal,
5174 AnyOfPhi->insertAfter(PhiR);
5181 OrVal, VectorRegionExitingVal, StartVPV, ExitDL);
5194 PhiR->hasUsesOutsideReductionChain());
5196 NewPhiR->setExpressionSunk();
5197 NewPhiR->insertBefore(PhiR);
5198 PhiR->replaceAllUsesWith(NewPhiR);
5199 PhiR->eraseFromParent();
5205struct ReductionExtend {
5206 Type *SrcType =
nullptr;
5213struct ExtendedReductionOperand {
5217 ReductionExtend ExtendA, ExtendB;
5225struct PartialReductionDescriptor {
5228 VPWidenRecipe *ReductionBinOp =
nullptr;
5230 ExtendedReductionOperand ExtendedOp;
5237 unsigned AccumulatorOpIdx;
5238 unsigned ScaleFactor;
5241 VPBlendRecipe *Blend =
nullptr;
5246static std::optional<unsigned>
5250 "Expected a non-normalized blend with two incoming values");
5256 return std::nullopt;
5257 return FirstIncomingHasOneUse ? 0 : 1;
5269 if (!
Op->hasOneUse() ||
5275 auto *
Trunc = Builder.createWidenCast(Instruction::CastOps::Trunc,
5276 Op->getOperand(1), NarrowTy);
5278 Op->setOperand(1, Builder.createWidenCast(ExtOpc,
Trunc, WideTy));
5287 auto *
Sub =
Op->getOperand(0)->getDefiningRecipe();
5289 assert(Ext->getOpcode() ==
5291 "Expected both the LHS and RHS extends to be the same");
5292 bool IsSigned = Ext->getOpcode() == Instruction::SExt;
5295 auto *FreezeX = Builder.insert(
new VPWidenRecipe(Instruction::Freeze, {
X}));
5296 auto *FreezeY = Builder.insert(
new VPWidenRecipe(Instruction::Freeze, {
Y}));
5297 auto *
Max = Builder.insert(
5299 {FreezeX, FreezeY}, SrcTy));
5300 auto *Min = Builder.insert(
5302 {FreezeX, FreezeY}, SrcTy));
5303 auto *AbsDiff = Builder.insert(
5306 return Builder.createWidenCast(Instruction::CastOps::ZExt, AbsDiff,
5307 Op->getScalarType());
5319 assert(
Mul->hasOneUse() && Ext->getOpcode() == MulLHS->getOpcode() &&
5320 MulLHS->getOpcode() == MulRHS->getOpcode() &&
5322 "Expected the outer extend to be foldable");
5324 auto *NewLHS = Builder.createWidenCast(
5325 MulLHS->getOpcode(), MulLHS->getOperand(0), Ext->getScalarType());
5326 auto *NewRHS = MulLHS == MulRHS
5328 : Builder.createWidenCast(MulRHS->getOpcode(),
5329 MulRHS->getOperand(0),
5330 Ext->getScalarType());
5331 auto *NewMul =
Mul->cloneWithOperands({NewLHS, NewRHS});
5332 Builder.insert(NewMul);
5333 Op->replaceAllUsesWith(NewMul);
5334 Op->eraseFromParent();
5335 Mul->eraseFromParent();
5344 VPValue *VecOp = Red->getVecOp();
5397static void transformToPartialReduction(
const PartialReductionDescriptor &Link,
5405 "Partial reductions can only be formed from add and sub links");
5409 WidenRecipe->
getOperand(1 - Link.AccumulatorOpIdx));
5412 ExtendedOp = optimizeExtendsForPartialReduction(ExtendedOp);
5428 if ((WidenRecipe->
getOpcode() == Instruction::Sub &&
5430 (WidenRecipe->
getOpcode() == Instruction::FSub &&
5435 if (WidenRecipe->
getOpcode() == Instruction::FSub) {
5447 Builder.insert(NegRecipe);
5448 ExtendedOp = NegRecipe;
5463 std::optional<unsigned> BlendReductionIdx =
5464 getBlendReductionUpdateValueIdx(Link.Blend);
5465 assert(BlendReductionIdx &&
5467 "Expected blend to contain the reduction update");
5481 [[maybe_unused]]
bool IsLastInChain =
5485 assert((!ExitValue || IsLastInChain) &&
5486 "if we found ExitValue, it must match RdxPhi's backedge value");
5497 PartialRed->insertBefore(WidenRecipe);
5507 E->insertBefore(WidenRecipe);
5508 PartialRed->replaceAllUsesWith(
E);
5514 const PartialReductionDescriptor &Link,
5517 const ExtendedReductionOperand &ExtendedOp = Link.ExtendedOp;
5518 std::optional<unsigned> BinOpc = std::nullopt;
5520 if (ExtendedOp.ExtendB.Kind != ExtendKind::PR_None)
5521 BinOpc = ExtendedOp.ExtendsUser->
getOpcode();
5523 std::optional<llvm::FastMathFlags>
Flags;
5527 auto GetLinkOpcode = [&Link]() ->
unsigned {
5530 return Instruction::Add;
5532 return Instruction::FAdd;
5534 return Link.ReductionBinOp->
getOpcode();
5539 GetLinkOpcode(), ExtendedOp.ExtendA.SrcType, ExtendedOp.ExtendB.SrcType,
5540 RdxType, VF, ExtendedOp.ExtendA.Kind, ExtendedOp.ExtendB.Kind, BinOpc,
5557static std::optional<ExtendedReductionOperand>
5560 "Op should be operand of UpdateR");
5568 if (
Op->hasOneUse() &&
5577 Type *RHSInputType =
Y->getScalarType();
5578 if (LHSInputType != RHSInputType ||
5579 LHSExt->getOpcode() != RHSExt->getOpcode())
5580 return std::nullopt;
5583 return ExtendedReductionOperand{
5589 std::optional<TTI::PartialReductionExtendKind> OuterExtKind;
5592 VPValue *CastSource = CastRecipe->getOperand(0);
5603 return ExtendedReductionOperand{
5610 if (!
Op->hasOneUse())
5611 return std::nullopt;
5616 return std::nullopt;
5626 return std::nullopt;
5633 const APInt *RHSConst =
nullptr;
5639 return std::nullopt;
5643 if (Cast && OuterExtKind &&
5645 return std::nullopt;
5647 Type *RHSInputType = LHSInputType;
5656 if (OuterExtKind && MulOp->
getOpcode() == Instruction::Mul &&
5658 return std::nullopt;
5660 return ExtendedReductionOperand{
5661 MulOp, {LHSInputType, LHSExtendKind}, {RHSInputType, RHSExtendKind}};
5668static std::optional<SmallVector<PartialReductionDescriptor>>
5675 return std::nullopt;
5676 VPValue *ExitValue = RdxResult->getOperand(0);
5686 return std::nullopt;
5693 VPValue *CurrentValue = ExitValue;
5694 while (CurrentValue != RedPhiR) {
5696 std::optional<unsigned> BlendReductionIdx;
5700 return std::nullopt;
5702 BlendReductionIdx = getBlendReductionUpdateValueIdx(Blend);
5703 if (!BlendReductionIdx)
5704 return std::nullopt;
5711 return std::nullopt;
5718 std::optional<ExtendedReductionOperand> ExtendedOp =
5719 matchExtendedReductionOperand(UpdateR,
Op);
5721 ExtendedOp = matchExtendedReductionOperand(UpdateR, PrevValue);
5723 return std::nullopt;
5731 return std::nullopt;
5733 Type *ExtSrcType = ExtendedOp->ExtendA.SrcType;
5736 return std::nullopt;
5738 PartialReductionDescriptor Link(
5739 {UpdateR, *ExtendedOp, RK,
5744 CurrentValue = PrevValue;
5749 std::reverse(Chain.
begin(), Chain.
end());
5759 assert(Phi->getVFScaleFactor() == 1 &&
"scale factor must not be set");
5760 Phi->setVFScaleFactor(Factor);
5765 StartInst->setOperand(2, NewScaleFactor);
5772 VPValue *OldStartValue = StartInst->getOperand(0);
5773 StartInst->setOperand(0, StartInst->getOperand(1));
5777 assert(RdxResult &&
"Could not find reduction result");
5780 unsigned SubOpc = RK ==
RecurKind::FSub ? Instruction::BinaryOps::FSub
5781 : Instruction::BinaryOps::Sub;
5784 Phi->getDebugLoc());
5802 [](
VPUser *U) { return !cast<VPRecipeBase>(U)->getRegion(); }))
5804 if (
auto Chain = getScaledReductionChain(&RedPhiR))
5805 PhiToChain.
try_emplace(&RedPhiR, std::move(*Chain));
5806 else if (CostCtx.
Opts.use_partial_reductions_by_default &&
5810 UnorderedReductions.
push_back(&RedPhiR);
5816 for (
auto *Rdx : UnorderedReductions) {
5832 ? std::make_optional(Rdx->getFastMathFlagsOrNone())
5836 Backedge->getOpcode(), ScalarTy,
nullptr,
5838 std::nullopt, CostCtx.
CostKind, FMF);
5839 return PRCost <= CurrentCost;
5845 Rdx->getRecurrenceKind(), Rdx->getFastMathFlagsOrNone(),
5846 Backedge->getUnderlyingInstr(), Rdx, OtherOp,
nullptr,
5849 Partial->insertBefore(Backedge);
5850 Backedge->replaceAllUsesWith(Partial);
5851 Backedge->eraseFromParent();
5854 if (PhiToChain.
empty())
5862 for (
auto &[
_, Chain] : PhiToChain)
5863 for (
const PartialReductionDescriptor &Link : Chain) {
5864 PartialReductionOps.
insert(Link.ExtendedOp.ExtendsUser);
5866 PartialReductionBlends.
insert(Link.Blend);
5867 ScaledReductionMap[Link.ReductionBinOp] = Link.ScaleFactor;
5873 auto ExtendUsersValid = [&](
VPValue *Ext) {
5875 return PartialReductionOps.contains(cast<VPRecipeBase>(U));
5879 auto IsProfitablePartialReductionChainForVF =
5886 for (
const PartialReductionDescriptor &Link : Chain) {
5887 const ExtendedReductionOperand &ExtendedOp = Link.ExtendedOp;
5888 InstructionCost LinkCost = getPartialReductionLinkCost(CostCtx, Link, VF);
5892 PartialCost += LinkCost;
5893 RegularCost += Link.ReductionBinOp->
computeCost(VF, CostCtx);
5895 if (ExtendedOp.ExtendB.Kind != ExtendKind::PR_None)
5896 RegularCost += ExtendedOp.ExtendsUser->
computeCost(VF, CostCtx);
5899 RegularCost += Extend->computeCost(VF, CostCtx);
5901 return PartialCost.
isValid() && PartialCost < RegularCost;
5909 for (
auto &[RedPhiR, Chain] : PhiToChain) {
5910 for (
const PartialReductionDescriptor &Link : Chain) {
5911 if (!
all_of(Link.ExtendedOp.ExtendsUser->
operands(), ExtendUsersValid)) {
5915 auto UseIsValid = [&, RedPhiR = RedPhiR](
VPUser *U) {
5917 return PhiR == RedPhiR;
5921 return Blend == Link.Blend || PartialReductionBlends.
contains(Blend);
5923 return Link.ScaleFactor == ScaledReductionMap.
lookup_or(R, 0) ||
5929 if (!
all_of(Link.ReductionBinOp->
users(), UseIsValid)) {
5938 auto *RepR = dyn_cast<VPReplicateRecipe>(U);
5939 return RepR && RepR->getOpcode() == Instruction::Store;
5950 return IsProfitablePartialReductionChainForVF(Chain, VF);
5956 for (
auto &[Phi, Chain] : PhiToChain) {
5960 for (
const PartialReductionDescriptor &Link : Chain)
5961 transformToPartialReduction(Link, Plan, Phi);
5966 const PartialReductionDescriptor &Link = Chain[0];
5982 if (VPI.getUnderlyingValue() &&
5993 auto ProcessSubset = [&](
VPlan &,
auto ProcessVPInst) {
5996 if (!ProcessVPInst(VPI))
6005 assert(New->getParent() &&
"New recipe must have been inserted");
6006 if (VPI->
getOpcode() == Instruction::Load)
6015 return ReplaceWith(VPI,
VPBuilder(VPI).insert(
6022 "lowerMemoryIdioms", ProcessSubset, Plan, [&](
VPInstruction *VPI) {
6024 VPI, FinalRedStoresBuilder))
6033 return ReplaceWith(VPI,
VPBuilder(VPI).insert(Histogram));
6046 "scalarizeMemOpsWithIrregularTypes", ProcessSubset, Plan,
6050 return Scalarize(VPI);
6057 "makeVPlanMemOpDecision", ProcessSubset, Plan, [&](
VPInstruction *VPI) {
6059 bool IsLoad = VPI->
getOpcode() == Instruction::Load;
6069 const SCEV *PtrSCEV =
6071 bool IsSingleScalarLoad =
6077 I, Ptr, IsSingleScalarLoad,
6086 "widenConsecutiveMemOps", ProcessSubset, Plan, [&](
VPInstruction *VPI) {
6088 bool IsLoad = VPI->
getOpcode() == Instruction::Load;
6092 std::optional<int64_t> Stride =
6094 if (Stride != 1 && Stride != -1)
6126 return ReplaceWith(VPI,
Load);
6135 auto *StoreR = Builder.createWidenStore(
6138 return ReplaceWith(VPI, StoreR);
6145 return ReplaceWith(VPI, Recipe);
6147 return Scalarize(VPI);
6167 if (VPI.mayHaveSideEffects())
6172 if (VPI.getOpcode() == Instruction::ExtractValue)
6176 if (VPI.isMasked() && !VPI.isSafeToSpeculativelyExecute())
6184 VPI.getOpcode(), VPI.operandsWithoutMask(),
nullptr, VPI,
6185 VPI, VPI.getDebugLoc(), VPI.getScalarType(),
I);
6186 Recipe->insertBefore(&VPI);
6187 VPI.replaceAllUsesWith(Recipe);
6188 VPI.eraseFromParent();
6198 switch (Param.ParamKind) {
6199 case VFParamKind::Vector:
6200 case VFParamKind::GlobalPredicate:
6202 case VFParamKind::OMP_Uniform:
6203 return SE->isSCEVable(Args[Param.ParamPos]->getScalarType()) &&
6204 SE->isLoopInvariant(
6205 vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L),
6207 case VFParamKind::OMP_Linear:
6208 return match(vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L),
6209 m_scev_AffineAddRec(
6210 m_SCEV(), m_scev_SpecificSInt(Param.LinearStepOrPos),
6211 m_SpecificLoop(L)));
6228 const auto *It =
find_if(Mappings, [&](
const VFInfo &Info) {
6229 return Info.Shape.VF == VF && (!MaskRequired || Info.isMasked()) &&
6232 if (It == Mappings.end())
6239struct CallWideningDecision {
6240 enum class KindTy { Scalarize,
Intrinsic, VectorVariant };
6241 CallWideningDecision(KindTy Kind,
Function *Variant =
nullptr)
6264 return CallWideningDecision::KindTy::Scalarize;
6274 return CallWideningDecision::KindTy::Scalarize;
6278 false, VF, CostCtx);
6293 return CallWideningDecision::KindTy::Intrinsic;
6297 if (VecFunc && ScalarCost >= VecCallCost)
6298 return {CallWideningDecision::KindTy::VectorVariant, VecFunc};
6300 return CallWideningDecision::KindTy::Scalarize;
6306 bool Widened =
false;
6311 if (!VPI.getUnderlyingValue() || VPI.getOpcode() != Instruction::Call)
6316 VPI.op_begin() + CI->arg_size());
6318 CallWideningDecision Decision =
6326 assert((Decision.Kind == CallWideningDecision::KindTy::Scalarize ||
6327 !CI->hasOperandBundles()) &&
6328 "calls with operand bundles must not be widened");
6330 switch (Decision.Kind) {
6331 case CallWideningDecision::KindTy::Intrinsic: {
6335 VPI, VPI.getDebugLoc());
6339 case CallWideningDecision::KindTy::VectorVariant: {
6344 Ops.push_back(Mask);
6346 Ops.push_back(VPI.getOperand(VPI.getNumOperandsWithoutMask() - 1));
6349 Attrs, VPI.getDebugLoc());
6353 case CallWideningDecision::KindTy::Scalarize:
6359 VPI.replaceAllUsesWith(Replacement);
6360 VPI.eraseFromParent();
6376 if (VPI.getOpcode() != Instruction::Trunc)
6394 if (!IsSub && !
match(
Op, MatchIV) &&
6416 !
TTI.isTruncateFree(
6417 toVectorTy(VPI.getOperand(0)->getScalarType(), VF),
6421 IsNarrowingProfitable,
Range))
6438 VPI.replaceAllUsesWith(NarrowIV);
6439 VPI.eraseFromParent();
6461 if (!MemR || MemR->isConsecutive())
6464 VPValue *Ptr = MemR->getAddr();
6476 VPValue *StoredValue =
nullptr;
6480 StoredValue = StoreR->getStoredValue();
6482 IntrinID = Intrinsic::experimental_vp_strided_store;
6486 IntrinID = Intrinsic::experimental_vp_strided_load;
6489 Align Alignment = MemR->getAlign();
6492 if (!Ctx.TTI.isLegalStridedLoadStore(VectorTy, Alignment))
6497 IntrinID, VectorTy, MemR->isMasked(), Alignment, Ctx);
6498 return StridedLoadStoreCost < CurrentCost;
6509 Ctx.invalidateWideningDecision(&MemR->getIngredient(), VF);
6514 I32VF = Builder.createScalarZExtOrTrunc(
6528 "Stride type from SCEV must match the index type");
6529 VPValue *CanIV = Builder.createScalarZExtOrTrunc(
6532 auto *
Offset = Builder.createOverflowingOp(
6533 Instruction::Mul, {CanIV, StrideInBytes},
6534 {AddRecPtr->hasNoUnsignedWrap(),
false});
6538 VPValue *BasePtr = Builder.createNoWrapPtrAdd(StartVPV,
Offset, NWFlags);
6541 VPValue *NewPtr = Builder.createVectorPointer(
6545 VPValue *Mask = MemR->getMask();
6550 Ops.push_back(StoredValue);
6551 Ops.append({NewPtr, StrideInBytes, Mask, I32VF});
6553 auto *StridedR = Builder.createWidenMemIntrinsic(
6556 *MemR, R.getDebugLoc());
6559 R.eraseFromParent();
6566 if (
Range.Start.isScalar())
6582 Intrinsic::experimental_cttz_elts, IdxTy,
6586 return BeforeFirst->computeCost(VF, Ctx) <
6587 Ctx.TTI.getIntrinsicInstrCost(CttzElts, Ctx.CostKind) +
6588 Ctx.TTI.getIntrinsicInstrCost(ActiveLaneMask, Ctx.CostKind);
6595 VPValue *FirstActive = Builder.createScalarZExtOrTrunc(
6596 Builder.createFirstActiveLane(
X,
DL), Ty,
DL);
6597 VPValue *PartStart = Builder.createOverflowingOp(
6602 BeforeFirst->eraseFromParent();
6615 if (std::optional<CompressedPtrInfo> Info =
6617 CondIndToMemOps[Info->ConditionalInductionPHI].push_back(&VPI);
6622 for (
auto &CondIndPhi :
6627 assert(!BlendR->isNormalized() &&
"Expected to run before simpifyBlends");
6628 for (
unsigned ValIdx :
seq(BlendR->getNumIncomingValues()))
6629 if (
VPValue *IncomingValue = BlendR->getIncomingValue(ValIdx);
6630 IncomingValue != &CondIndPhi) {
6631 Mask = BlendR->getMask(ValIdx);
6634 assert(Mask &&
"Expected mask for conditional induction update");
6638 for (
VPInstruction *VPI : CondIndToMemOps[CondIndPhi.getPHINode()]) {
6641 if (VPI->getMask() != Mask)
6644 Builder.setInsertPoint(VPI);
6645 auto *CompressedMemOp =
6648 VPI->replaceAllUsesWith(CompressedMemOp);
6649 VPI->eraseFromParent();
6655 if (UpdateType->isPointerTy())
6659 const SCEV *StepSCEV = CondIndPhi.getStepSCEV();
6661 Builder.setInsertPoint(BlendR);
6664 {},
DL,
"handled.lanes");
6667 Step = Builder.createFreeze(Step);
6670 Instruction::Mul, {Step, HandledLanes}, {
false,
false},
DL);
6673 ? Builder.createPtrAdd(&CondIndPhi,
Offset,
DL,
"conditional.step")
6674 : Builder.createAdd(&CondIndPhi,
Offset,
DL,
"conditional.step");
6675 CondIndPhi.getBackedgeValue()->replaceAllUsesWith(Update);
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isEqual(AttributeSet CallerAttrs, AttributeSet CalleeAttrs)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
iv Induction Variable Users
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Legalize the Machine IR a function s Machine IR
This file provides utility analysis objects describing memory locations.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
Function const char * Passes
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains the declarations for profiling metadata utility functions.
SmallVector< MachineOperand, 4 > Cond
This is the interface for a metadata-based scoped no-alias analysis.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file implements the TypeSwitch template, which mimics a switch() statement whose cases are type ...
This file implements dominator tree analysis for a single level of a VPlan's H-CFG.
This file contains the declarations of different VPlan-related auxiliary helpers.
This file contains the declarations of the Vectorization Plan base classes:
static const X86InstrFMA3Group Groups[]
static const uint32_t IV[8]
Helper for extra no-alias checks via known-safe recipe and SCEV.
SinkStoreInfo(ArrayRef< VPReplicateRecipe * > ExcludeRecipes, VPReplicateRecipe &GroupLeader, PredicatedScalarEvolution &PSE, const Loop &L)
SinkStoreInfo(VPReplicateRecipe &GroupLeader)
bool shouldSkip(VPRecipeBase &R) const
Return true if R should be skipped during alias checking, either because it's in the exclude set or b...
Class for arbitrary precision integers.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
APInt abs() const
Get the absolute value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
int32_t exactLogBase2() const
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
An arbitrary precision integer that knows its signedness.
static APSInt getMinValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the minimum integer value with the given bit width and signedness.
static APSInt getMaxValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the maximum integer value with the given bit width and signedness.
@ NoAlias
The two locations do not alias at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
Get the last element.
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
const T & front() const
Get the first element.
size_t size() const
Get the array size.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
This class represents a function call, abstracting a target machine's calling convention.
@ ICMP_ULT
unsigned less than
@ ICMP_ULE
unsigned less or equal
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
bool isTrueWhenEqual() const
This is just a convenience.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
This class represents a range of values.
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
A parsed version of the target data layout string in and methods for querying it.
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
static DebugLoc getUnknown()
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static constexpr ElementCount getScalable(ScalarTy MinVal)
constexpr bool isScalar() const
Exactly one element.
Convenience struct for specifying and reasoning about fast-math flags.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedWrap() const
GEPNoWrapFlags withoutNoUnsignedWrap() const
static GEPNoWrapFlags none()
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
A struct for saving information about induction variables.
InductionKind
This enum represents the kinds of inductions that we support.
@ IK_PtrInduction
Pointer induction var. Step = C.
@ IK_IntInduction
Integer induction variable. Step = C.
static InstructionCost getInvalid(CostType Val=0)
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
The group of interleaved loads/stores sharing the same stride and close to each other.
InstTy * getInsertPos() const
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
static bool getDecisionAndClampRange(const std::function< bool(ElementCount)> &Predicate, VFRange &Range)
Test a Predicate on a Range of VF's.
Represents a single loop in the control flow graph.
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
This class implements a map that also provides access to all stored values in a deterministic order.
ValueT lookup(const KeyT &Key) const
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Representation for a specific memory location.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Post-order traversal of a graph.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
static LLVM_ABI unsigned getOpcode(RecurKind Kind)
Returns the opcode corresponding to the RecurrenceKind.
unsigned getOpcode() const
static bool isFindLastRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static bool isFindRecurrenceKind(RecurKind Kind)
LLVM_ABI bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
RegionT * getParent() const
Get the parent of the Region.
This class represents a constant integer value.
ConstantInt * getValue() const
static const SCEV * rewrite(SCEVUse Scev, ScalarEvolution &SE, ValueToSCEVMapTy &Map)
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
const DataLayout & getDataLayout() const
Return the DataLayout associated with the module this SCEV instance is operating on.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEVFlags Flags=SCEV::FlagNone)
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI bool isGuaranteedNotToCauseUB(const SCEV *Op)
Returns true if Op is guaranteed not to cause immediate UB.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEVFlags Flags=SCEV::FlagNone)
Return the SCEV object corresponding to -V.
A simple AA result which uses scoped-noalias metadata to answer queries.
A vector that has set insertion semantics.
size_type size() const
Determine the number of elements in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Provides information about what library functions are available for the current target.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isPointerTy() const
True if this is an instance of PointerType.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static SmallVector< VFInfo, 8 > getMappings(const CallInst &CI)
Retrieve all the VFInfo instances associated to the CallInst CI.
bool isLegalMaskedLoadOrStore(bool IsLoad, Type *ScalarTy, Align Alignment, unsigned AddressSpace) const
Returns true if the target machine supports a masked load (if IsLoad) or masked store of scalar type ...
A recipe for generating the active lane mask for the vector loop that is used to predicate the vector...
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
RecipeListTy::iterator iterator
Instruction iterators...
iterator begin()
Recipe iterator methods.
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
VPBasicBlock * splitAt(iterator SplitAt)
Split current block at SplitAt by inserting a new block between the current block and its successors ...
const VPRecipeBase & front() const
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
const VPRecipeBase & back() const
A recipe for vectorizing a phi-node as a sequence of mask-based select instructions.
VPValue * getIncomingValue(unsigned Idx) const
Return incoming value number Idx.
VPValue * getMask(unsigned Idx) const
Return mask number Idx.
unsigned getNumIncomingValues() const
Return the number of incoming values, taking into account when normalized the first incoming value wi...
void setMask(unsigned Idx, VPValue *V)
Set mask number Idx to V.
bool isNormalized() const
A normalized blend is one that has an odd number of operands, whereby the first operand does not have...
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
void setSuccessors(ArrayRef< VPBlockBase * > NewSuccs)
Set each VPBasicBlock in NewSuccss as successor of this VPBlockBase.
VPRegionBlock * getParent()
const VPBasicBlock * getExitingBasicBlock() const
size_t getNumSuccessors() const
void setPredecessors(ArrayRef< VPBlockBase * > NewPreds)
Set each VPBasicBlock in NewPreds as predecessor of this VPBlockBase.
const VPBlocksTy & getPredecessors() const
VPBlockBase * getSinglePredecessor() const
void clearPredecessors()
Remove all the predecessor of this block.
const VPBasicBlock * getEntryBasicBlock() const
VPBlockBase * getSingleSuccessor() const
const VPBlocksTy & getSuccessors() const
static auto blocksAs(T &&Range)
Return an iterator range over Range with each block cast to BlockTy.
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To, VPBlockBase *BlockPtr)
Inserts BlockPtr on the edge between From and To.
static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop latch, using isHeader().
static VPBasicBlock * getPlainCFGMiddleBlock(const VPlan &Plan)
Returns the middle block of Plan in plain CFG form (before regions are formed).
static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse, VPBlockBase *BlockPtr)
Insert disconnected VPBlockBases IfTrue and IfFalse after BlockPtr.
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To)
Disconnect VPBlockBases From and To bi-directionally.
static auto blocksOnly(T &&Range)
Return an iterator range over Range which only includes BlockTy blocks.
static std::pair< VPBasicBlock *, VPBasicBlock * > getPlainCFGHeaderAndLatch(const VPlan &Plan)
Returns the header and latch of the outermost loop of Plan in plain CFG form (before regions are form...
static void transferSuccessors(VPBlockBase *Old, VPBlockBase *New)
Transfer successors from Old to New. New must have no successors.
static SmallVector< VPBasicBlock * > blocksInSingleSuccessorChainBetween(VPBasicBlock *FirstBB, VPBasicBlock *LastBB)
Returns the blocks between FirstBB and LastBB, where FirstBB to LastBB forms a single-sucessor chain.
A recipe for generating conditional branches on the bits of a mask.
VPlan-based builder utility similar to IRBuilder.
VPInstruction * createFreeze(VPValue *Op, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPWidenIntrinsicRecipe * createWidenIntrinsic(Intrinsic::ID VectorIntrinsicID, ArrayRef< VPValue * > CallArguments, Type *Ty, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown())
static VPBuilderBase getToInsertAfter(VPRecipeBase *R)
VPInstruction * createLogicalAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPInstruction * createAnyOfReduction(VPValue *ChainOp, VPValue *TrueVal, VPValue *FalseVal, DebugLoc DL=DebugLoc::getUnknown())
Create an AnyOf reduction pattern: or-reduce ChainOp, freeze the result, then select between TrueVal ...
VPDerivedIVRecipe * createDerivedIV(InductionDescriptor::InductionKind Kind, FPMathOperator *FPBinOp, VPValue *Start, VPValue *Current, VPValue *Step, const VPIRFlags::WrapFlagsTy &Flags={})
Convert Current to Start + Current * Step.
VPWidenCastRecipe * createWidenCast(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
VPInstruction * createAdd(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", VPRecipeWithIRFlags::WrapFlagsTy WrapFlags={false, false})
VPInstruction * createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", std::optional< VPIRFlags > Flags=std::nullopt)
Create a select of TrueVal and FalseVal based on Cond, using the default flags for the result type,...
VPValue * createScalarZExtOrTrunc(VPValue *Op, Type *ResultTy, DebugLoc DL)
static VPSingleDefRecipe * createSingleScalarOp(unsigned Opcode, ArrayRef< VPValue * > Operands, VPValue *Mask, const VPIRFlags &Flags, const VPIRMetadata &Metadata, DebugLoc DL, Type *ResultTy, Instruction *UV)
VPWidenLoadRecipe * createWidenLoad(LoadInst &Load, VPValue *Addr, VPValue *Mask, bool Consecutive, Align Alignment, const VPIRMetadata &Metadata, DebugLoc DL)
Create a recipe widening Load, loading from Addr with Mask (may be null).
VPInstruction * createLogicalOr(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPInstruction * createScalarCast(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy, DebugLoc DL, std::optional< VPIRFlags > Flags=std::nullopt, const VPIRMetadata &Metadata={})
VPInstruction * createNot(VPValue *Operand, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
void setInsertPoint(const VPInsertPoint &IP)
Set the current insert point.
VPInstruction * createNaryOp(unsigned Opcode, ArrayRef< VPValue * > Operands, Instruction *Inst=nullptr, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", Type *ResultTy=nullptr, const VPIRAttributes &Attrs={})
Create an N-ary operation with Opcode, Operands and set Inst as its underlying Instruction.
VPInstruction * createSub(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", VPRecipeWithIRFlags::WrapFlagsTy WrapFlags={false, false})
VPInstruction * createFirstActiveLane(ArrayRef< VPValue * > Masks, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPWidenStoreRecipe * createWidenStore(StoreInst &Store, VPValue *Addr, VPValue *StoredVal, VPValue *Mask, bool Consecutive, Align Alignment, const VPIRMetadata &Metadata, DebugLoc DL)
Create a recipe widening Store, storing StoredVal to Addr with Mask (may be null).
VPInstruction * createOr(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPInstruction * createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Create a new ICmp VPInstruction with predicate Pred and operands A and B.
unsigned getNumDefinedValues() const
Returns the number of values defined by the VPDef.
VPValue * getVPSingleValue()
Returns the only VPValue defined by the VPDef.
VPValue * getVPValue(unsigned I)
Returns the VPValue with index I defined by the VPDef.
ArrayRef< VPRecipeValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
LLVM_ABI_FOR_TEST bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B) const
Recipe to expand a SCEV expression.
A recipe to combine multiple recipes into a single 'expression' recipe, which should be considered a ...
A recipe representing a sequence of load -> update -> store as part of a histogram operation.
Manage IR attributes for call recipes.
A special type of VPBasicBlock that wraps an existing IR basic block.
Class to record and manage LLVM IR flags.
static LLVM_ABI_FOR_TEST VPIRFlags getDefaultFlags(unsigned Opcode, Type *ResultTy=nullptr)
Returns default flags for Opcode and scalar ResultTy for opcodes that support it, asserts otherwise.
LLVM_ABI_FOR_TEST FastMathFlags getFastMathFlagsOrNone() const
This is a concrete Recipe that models a single VPlan-level instruction.
unsigned getNumOperandsWithoutMask() const
Returns the number of operands, excluding the mask if the VPInstruction is masked.
@ ExtractLane
Extracts a single lane (first operand) from a set of vector operands.
@ ExtractPenultimateElement
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
@ BuildVector
Creates a fixed-width vector containing all operands.
@ CanonicalIVIncrementForPart
@ ComputeReductionResult
Reduce the operands to the final reduction result using the operation specified via the operation's V...
void addMask(VPValue *Mask)
Add mask Mask to an unmasked VPInstruction, if it needs masking.
unsigned getOpcode() const
VPValue * getMask() const
Returns the mask for the VPInstruction.
const InterleaveGroup< Instruction > * getInterleaveGroup() const
VPValue * getMask() const
Return the mask used by this recipe.
ArrayRef< VPValue * > getStoredValues() const
Return the VPValues stored by this interleave group.
VPInterleaveRecipe is a recipe for transforming an interleave group of load or stores into one wide l...
VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when control converges back from ...
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
void moveBefore(RecipeListTy::iterator I)
Unlink this recipe and insert it before I.
VPRegionBlock * getRegion()
RecipeListTy::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
void insertBefore(VPRecipeBase *InsertPos)
Insert an unlinked recipe into a basic block immediately before the specified recipe.
void insertAfter(VPRecipeBase *InsertPos)
Insert an unlinked Recipe into a basic block immediately after the specified Recipe.
Helper class to create VPRecipies from IR instructions.
VPHistogramRecipe * widenIfHistogram(VPInstruction *VPI)
If VPI represents a histogram operation (as determined by LoopVectorizationLegality) make that safe f...
std::optional< CompressedPtrInfo > getCompressedPtrInfo(VPInstruction *VPI)
Returns the CompressedPtrInfo for VPI if it is a compressed load or store (as determined by LoopVecto...
bool prefersVectorizedAddressing() const
Returns true if the target prefers vectorized addressing.
VPRecipeBase * tryToWidenMemory(VPInstruction *VPI, VFRange &Range)
Check if the load or store instruction VPI should widened for Range.Start and potentially masked.
bool replaceWithFinalIfReductionStore(VPInstruction *VPI, VPBuilder &FinalRedStoresBuilder)
If VPI is a store of a reduction into an invariant address, delete it.
VPSingleDefRecipe * handleReplication(VPInstruction *VPI, VFRange &Range)
Build a replicating or single-scalar recipe for VPI.
VPWidenMemIntrinsicRecipe * widenCompressedLoadOrStore(VPInstruction *VPI)
Build a llvm.masked.expandload or llvm.masked.compressstore intrinsic for VPI.
bool isPredicatedInst(Instruction *I) const
Returns true if I needs to be predicated (i.e.
Type * getScalarType() const
Returns the scalar type of this VPRecipeValue.
A recipe for handling reduction phis.
bool isOrdered() const
Returns true, if the phi is part of an ordered reduction.
bool isInLoop() const
Returns true if the phi is part of an in-loop reduction.
RecurKind getRecurrenceKind() const
Returns the recurrence kind of the reduction.
A recipe to represent inloop, ordered or partial reduction operations.
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
const VPBlockBase * getEntry() const
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
void setExiting(VPBlockBase *ExitingBlock)
Set ExitingBlock as the exiting VPBlockBase of this VPRegionBlock.
void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print this VPRegionBlock to O (recursively), prefixing all lines with Indent.
Type * getCanonicalIVType() const
Return the type of the canonical IV for loop regions.
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
const VPBlockBase * getExiting() const
VPRegionValue * getHeaderMask() const
Return the header mask of the region, or null if not set.
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
bool isSingleScalar() const
Returns true if the recipe produces a single scalar value.
static InstructionCost computeCallCost(Function *CalledFn, Type *ResultTy, ArrayRef< const VPValue * > ArgOps, bool IsSingleScalar, ElementCount VF, VPCostContext &Ctx)
Return the cost of scalarizing a call to CalledFn with argument operands ArgOps for a given VF.
operand_range operandsWithoutMask()
Return the recipe's operands, excluding the mask of a predicated recipe.
bool isPredicated() const
VPValue * getMask()
Return the mask of a predicated VPReplicateRecipe.
Lightweight SCEV-to-VPlan expander.
VPValue * expand(const SCEV *S)
Expand S into recipes and live-ins using the builder.
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Instruction * getUnderlyingInstr()
Returns the underlying instruction.
VPSingleDefRecipe * clone() override=0
Clone the current recipe.
A symbolic live-in VPValue, used for values like vector trip count, VF, and VFxUF.
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
void setOperand(unsigned I, VPValue *New)
unsigned getNumOperands() const
VPValue * getOperand(unsigned N) const
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Type * getScalarType() const
Returns the scalar type of this VPValue, dispatching based on the concrete subclass.
Value * getLiveInIRValue() const
Return the underlying IR value for a VPIRValue.
bool isDefinedOutsideLoopRegions() const
Returns true if the VPValue is defined outside any loop.
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
bool hasMoreThanOneUniqueUser() const
Returns true if the value has more than one unique user.
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
void replaceUsesWithIf(VPValue *New, llvm::function_ref< bool(VPUser *U)> ShouldReplace)
Go through the uses list for this VPValue and make each use point to New if the callback ShouldReplac...
VPUser * getSingleUser()
Return the single user of this value, or nullptr if there is not exactly one user.
void replaceAllUsesWith(VPValue *New)
A recipe to compute a pointer to the last element of each part of a widened memory access for widened...
A recipe for widening Call instructions using library calls.
static InstructionCost computeCallCost(Function *Variant, VPCostContext &Ctx)
Return the cost of widening a call using the vector function Variant.
VPWidenCastRecipe is a recipe to create vector cast instructions.
Instruction::CastOps getOpcode() const
A recipe for handling GEP instructions.
Base class for widened induction (VPWidenIntOrFpInductionRecipe and VPWidenPointerInductionRecipe),...
PHINode * getPHINode() const
Returns the underlying PHINode if one exists, or null otherwise.
VPValue * getStepValue()
Returns the step value of the induction.
const InductionDescriptor & getInductionDescriptor() const
Returns the induction descriptor for the recipe.
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
bool isTruncated() const
Returns true if this induction is evaluated in a narrower type than the start value.
A recipe for widening vector intrinsics.
static InstructionCost computeCallCost(Intrinsic::ID ID, ArrayRef< const VPValue * > Operands, const VPRecipeWithIRFlags &R, ElementCount VF, VPCostContext &Ctx)
Compute the cost of a vector intrinsic with ID and Operands.
static InstructionCost computeMemIntrinsicCost(Intrinsic::ID IID, Type *Ty, bool IsMasked, Align Alignment, VPCostContext &Ctx)
Helper function for computing the cost of vector memory intrinsic.
A common mixin class for widening memory operations.
virtual VPRecipeBase * getAsRecipe()=0
Return a VPRecipeBase* to the current object.
A recipe for widened phis.
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenRecipe.
VPWidenRecipe * clone() override
Clone the current recipe.
unsigned getOpcode() const
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
VPIRValue * getLiveIn(Value *V) const
Return the live-in VPIRValue for V, if there is one or nullptr otherwise.
bool hasVF(ElementCount VF) const
const DataLayout & getDataLayout() const
LLVMContext & getContext() const
VPBasicBlock * getEntry()
bool hasScalableVF() const
VPValue * getTripCount() const
The trip count of the original loop.
iterator_range< SmallSetVector< ElementCount, 2 >::iterator > vectorFactors() const
Returns an iterator range over all VFs of the plan.
VPIRValue * getFalse()
Return a VPIRValue wrapping i1 false.
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
VPIRValue * getAllOnesValue(Type *Ty)
Return a VPIRValue wrapping the AllOnes value of type Ty.
VPRegionBlock * createReplicateRegion(VPBlockBase *Entry, VPBlockBase *Exiting, const std::string &Name="")
Create a new replicate region with Entry, Exiting and Name.
auto getLiveIns() const
Return the list of live-in VPValues available in the VPlan.
bool hasUF(unsigned UF) const
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
VPSymbolicValue & getVectorTripCount()
The vector trip count.
VPIRValue * getOrAddLiveIn(Value *V)
Gets the live-in VPIRValue for V or adds a new live-in (if none exists yet) for V.
VPIRValue * getZero(Type *Ty)
Return a VPIRValue wrapping the null value of type Ty.
void setVF(ElementCount VF)
bool isUnrolled() const
Returns true if the VPlan already has been unrolled, i.e.
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
LLVM_ABI_FOR_TEST bool isOuterLoop() const
Returns true if this VPlan is for an outer loop, i.e., its vector loop region contains a nested loop ...
unsigned getConcreteUF() const
Returns the concrete UF of the plan, after unrolling.
void resetTripCount(VPValue *NewTripCount)
Resets the trip count for the VPlan.
VPBasicBlock * getMiddleBlock()
Returns the 'middle' block of the plan, that is the block that selects whether to execute the scalar ...
VPBasicBlock * createVPBasicBlock(const Twine &Name, VPRecipeBase *Recipe=nullptr)
Create a new VPBasicBlock with Name and containing Recipe if present.
VPIRValue * getTrue()
Return a VPIRValue wrapping i1 true.
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
VPSymbolicValue & getUF()
Returns the UF of the vector loop region.
bool hasScalarVFOnly() const
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
bool hasTailFolded() const
Returns true if the vector loop region is tail-folded.
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
LLVM_ABI_FOR_TEST VPlan * duplicate()
Clone the current VPlan, update all VPValues of the new VPlan and cloned recipes to refer to the clon...
VPIRValue * getConstantInt(Type *Ty, uint64_t Val, bool IsSigned=false)
Return a VPIRValue wrapping a ConstantInt with the given type and value.
LLVM Value Representation.
iterator_range< user_iterator > users()
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Base class of all SIMD vector types.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS*X will result in a value whose quantity matches our ...
constexpr ScalarTy getFixedValue() const
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS*X will result in a value whose quantity matches our own.
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM)
Return A unsign-divided by B, rounded by the given rounding mode.
std::variant< std::monostate, Loc::Single, Loc::Multi, Loc::MMI, Loc::EntryValue > Variant
Alias for the std::variant specialization base class of DbgVariable.
void reportVectorizationFailure(const StringRef DebugMsg, const StringRef OREMsg, const StringRef ORETag, OptimizationRemarkEmitter *ORE, const Loop *TheLoop, Instruction *I=nullptr)
Reports a vectorization failure: print DebugMsg for debugging purposes along with the corresponding o...
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
AllOnesConstantMatch m_AllOnes()
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_unless< Pattern > m_Unless(const Pattern &P)
Match if the inner matcher does NOT match.
match_isa< To... > m_Isa()
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
auto m_Cmp()
Matches any compare instruction and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
FNeg_match< OpTy, false > m_FNeg(const OpTy &X)
Match fneg X only.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
LogicalOp_match< LHS, RHS, Instruction::And > m_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R either in the form of L & R or L ?
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
CmpClass_match< LHS, RHS, CmpInst, true > m_c_Cmp(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
SpecificCmpClass_match< LHS, RHS, CmpInst > m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_MaskedStore(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedStore Intrinsic.
auto m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
Matches MaskedLoad Intrinsic.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
bool match(const SCEV *S, const Pattern &P)
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::ExtractLastLane, VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > > m_ExtractLastLaneOfLastPart(const Op0_t &Op0)
AllRecipe_commutative_match< Instruction::And, Op0_t, Op1_t > m_c_BinaryAnd(const Op0_t &Op0, const Op1_t &Op1)
Match a binary AND operation.
AllRecipe_match< Instruction::Or, Op0_t, Op1_t > m_BinaryOr(const Op0_t &Op0, const Op1_t &Op1)
Match a binary OR operation.
VPInstruction_match< VPInstruction::AnyOf > m_AnyOf()
RecipeTy * findSingleUser(VPValue *V, const MatchT &P)
Return the sole user of V matching pattern P, casting it to RecipeTy, or nullptr.
AllRecipe_commutative_match< Instruction::Or, Op0_t, Op1_t > m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::ComputeReductionResult, Op0_t > m_ComputeReductionResult(const Op0_t &Op0)
auto m_WidenAnyExtend(const Op0_t &Op0)
match_bind< VPIRValue > m_VPIRValue(VPIRValue *&V)
Match a VPIRValue.
auto m_VPPhi(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::BranchOnTwoConds > m_BranchOnTwoConds()
VPWidenStoreRecipe_match< Op0_t, Op1_t > m_WidenStore(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_match< Opcode, Op0_t, Op1_t > m_Binary(const Op0_t &Op0, const Op1_t &Op1)
VPWidenLoadRecipe_match< Op0_t > m_WidenLoad(const Op0_t &Op0)
VPInstruction_match< VPInstruction::LastActiveLane, Op0_t > m_LastActiveLane(const Op0_t &Op0)
auto m_WidenIntrinsic(const T &...Ops)
canonical_widen_iv_match m_CanonicalWidenIV()
VPInstruction_match< VPInstruction::ExitingIVValue, Op0_t > m_ExitingIVValue(const Op0_t &Op0)
VPInstruction_match< Instruction::ExtractElement, Op0_t, Op1_t > m_ExtractElement(const Op0_t &Op0, const Op1_t &Op1)
ActiveLaneMaskPHIRecipe_match< Op0_t, Op1_t > m_ActiveLaneMaskPhi(const Op0_t &Op0, const Op1_t &Op1)
VectorPointerRecipe_match< Op0_t, Op1_t > m_VecPtr(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::ExtractLastLane, Op0_t > m_ExtractLastLane(const Op0_t &Op0)
int_pred_ty< is_zero_int, 1 > m_False()
match_bind< VPSingleDefRecipe > m_VPSingleDefRecipe(VPSingleDefRecipe *&V)
Match a VPSingleDefRecipe, capturing if we match.
VPInstruction_match< VPInstruction::BranchOnCount > m_BranchOnCount()
auto m_GetElementPtr(const Op0_t &Op0, const Op1_t &Op1)
auto m_VPValue()
Match an arbitrary VPValue and ignore it.
VPInstruction_match< VPInstruction::Broadcast, Op0_t > m_Broadcast(const Op0_t &Op0)
bool match(Val *V, const Pattern &P)
header_mask_match m_HeaderMask()
VPInstruction_match< VPInstruction::BuildVector > m_BuildVector()
BuildVector is matches only its opcode, w/o matching its operands as the number of operands is not fi...
VPInstruction_match< VPInstruction::ExtractPenultimateElement, Op0_t > m_ExtractPenultimateElement(const Op0_t &Op0)
match_bind< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
VPInstruction_match< VPInstruction::FirstActiveLane, Op0_t > m_FirstActiveLane(const Op0_t &Op0)
int_pred_ty< is_one, 1 > m_True()
VPInstruction_match< VPInstruction::WideActiveLaneMask, Op_t... > m_WideActiveLaneMask(const Op_t &...Op)
VPInstruction_match< VPInstruction::ExtractVectorForPart, Op_t... > m_ExtractVectorForPart(const Op_t &...Op)
auto m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< VPInstruction::BranchOnCond > m_BranchOnCond()
VPInstruction_match< VPInstruction::ExtractLane, Op0_t, Op1_t > m_ExtractLane(const Op0_t &Op0, const Op1_t &Op1)
auto m_AnyNeg(const Op0_t &Op0)
VPInstruction_match< VPInstruction::Reverse, Op0_t > m_Reverse(const Op0_t &Op0)
NodeAddr< DefNode * > Def
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
bool cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking=false)
Return true if we do not know how to (mechanically) hoist or sink R.
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
std::optional< int64_t > getConstantStride(VPValue *Addr, Type *AccessTy, PredicatedScalarEvolution &PSE, const Loop *L)
If the pointer operand Addr of a memory access is an affine AddRec w.r.t.
VPInstruction * findComputeReductionResult(VPReductionPHIRecipe *PhiR)
Find the ComputeReductionResult recipe for PhiR, looking through selects inserted for predicated redu...
VPInstruction * findCanonicalIVIncrement(VPlan &Plan)
Find the canonical IV increment of Plan's vector loop region.
std::optional< MemoryLocation > getMemoryLocation(const VPRecipeBase &R)
Return a MemoryLocation for R with noalias metadata populated from R, if the recipe is supported and ...
bool onlyFirstLaneUsed(const VPValue *Def)
Returns true if only the first lane of Def is used by all users.
VPIRValue * tryToFoldLiveIns(VPSingleDefRecipe &R, ArrayRef< VPValue * > Operands, const DataLayout &DL)
Try to fold R using InstSimplifyFolder.
SmallVector< std::pair< VPBasicBlock *, VPIRBasicBlock * > > getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB)
Returns the (early exiting block, exit block) pairs of Plan, i.e.
Type * getLoadStoreValueType(VPSingleDefRecipe *R)
Get the value type of a VPInstruction or VPReplicateRecipe load or store.
VPScalarIVStepsRecipe * createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind, Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp, Type *ResultTy, VPValue *StartV, VPValue *Step, DebugLoc DL, VPBuilder &Builder, const VPIRFlags::WrapFlagsTy &Flags={})
Create a scalar-iv-steps recipe over Plan's canonical IV for an induction of Kind with InductionOpcod...
void recursivelyDeleteDeadRecipes(VPValue *V)
Recursively delete V and any of its operands that become dead.
bool doesGeneratePerAllLanes(const VPRecipeBase *R)
Returns true if R produces scalar values for all VF lanes.
bool isDeadRecipe(VPRecipeBase &R)
Returns true if R is dead, i.e.
VPRecipeBase * findRecipe(VPValue *Start, PredT Pred)
Search Start's users for a recipe satisfying Pred, looking through recipes with definitions.
LLVM_ABI_FOR_TEST bool isUniformAcrossVFsAndUFs(const VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr, bool ExpandInIR=false)
Get or create a VPValue that corresponds to the expansion of Expr.
bool isUsedByLoadStoreAddress(const VPValue *V)
Returns true if V is used as part of the address of another load or store.
std::optional< std::pair< bool, unsigned > > getOpcodeOrIntrinsicID(const VPValue *V)
Get the instruction opcode or intrinsic ID for the recipe defining V.
VPValue * scalarizeVPWidenPointerInduction(VPWidenPointerInductionRecipe *PtrIV, VPlan &Plan, VPBuilder &Builder)
Scalarize a VPWidenPointerInductionRecipe by replacing it with a PtrAdd (IndStart,...
LLVM_ABI_FOR_TEST const SCEV * getSCEVExprForVPValue(const VPValue *V, PredicatedScalarEvolution &PSE, const Loop *L=nullptr)
Return the SCEV expression for V.
void pullOutPermutations(VPlan &Plan, Match_t Perm, Builder Build)
Removes the permutation pattern Perm from any elementwise operations in the plan, by constructing a n...
SmallVector< VPUser * > collectUsersRecursively(VPValue *V)
Collect all users of V, looking through recipes that define other values.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
SmallVector< VPBasicBlock * > vp_rpo_plain_cfg_loop_body(VPBasicBlock *Header)
Returns the VPBasicBlocks forming the loop body of a plain (pre-region) VPlan in reverse post-order s...
constexpr auto not_equal_to(T &&Arg)
Functor variant of std::not_equal_to that can be used as a UnaryPredicate in functional algorithms li...
void stable_sort(R &&Range)
auto min_element(R &&Range)
Provide wrappers to std::min_element which take ranges instead of having to pass begin/end explicitly...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
unsigned getLoadStoreAddressSpace(const Value *I)
A helper function that returns the address space of the pointer operand of load or store instruction.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
LLVM_ABI Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
ReductionStyle getReductionStyle(bool InLoop, bool Ordered, unsigned ScaleFactor)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
VPBuilderBase<> VPBuilder
constexpr from_range_t from_range
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
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...
auto cast_or_null(const Y &Val)
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
iterator_range< df_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_depth_first_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order.
bool isa_and_nonnull(const Y &Val)
iterator_range< df_iterator< VPBlockDeepTraversalWrapper< VPBlockBase * > > > vp_depth_first_deep(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order while traversing t...
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
auto make_isa_range(RangeT &&Range)
Return a range over Range containing only elements for which isa<T> holds, casting each of them to T.
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
void sort(IteratorTy Start, IteratorTy End)
DenseMap< Value *, const SCEVUnknown * > SymbolicStrideMap
Maps a pointer to its symbolic (non-constant) stride.
bool hasIrregularType(Type *Ty, const DataLayout &DL)
A helper function that returns true if the given type is irregular.
UncountableExitStyle
Different methods of handling early exits.
@ ReadOnly
No side effects to worry about, so we can process any uncountable exits in the loop and branch either...
@ MaskedHandleExitInScalarLoop
All memory operations other than the load(s) required to determine whether an uncountable exit occurr...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
bool canConstantBeExtended(const APInt *C, Type *NarrowType, TTI::PartialReductionExtendKind ExtKind)
Check if a constant CI can be safely treated as having been extended from a narrower type with the gi...
T * find_singleton(R &&Range, Predicate P, bool AllowRepeats=false)
Return the single value in Range that satisfies P(<member of Range> *, AllowRepeats)->T * returning n...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
RecurKind
These are the kinds of recurrences that we support.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FindIV
FindIV reduction with select(icmp(),x,y) where one of (x,y) is a loop induction variable (increasing ...
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ FSub
Subtraction of floats.
@ FAddChainWithSubs
A chain of fadds and fsubs.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Sub
Subtraction of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
LLVM_ABI Value * getRecurrenceIdentity(RecurKind K, Type *Tp, FastMathFlags FMF)
Given information about an recurrence kind, return the identity for the @llvm.vector....
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
DenseMap< const Value *, SCEVUse > ValueToSCEVMapTy
DWARFExpression::Operation Op
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
const VectorizeOptions & getVectorizeOptions(const LLVMContext &C)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Type * toVectorTy(Type *Scalar, ElementCount EC)
A helper function for converting Scalar types to vector types.
LLVM_ABI bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC=nullptr, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
constexpr detail::CastFunc< To > CastTo
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
LLVM_ABI_FOR_TEST bool verifyVPlanIsValid(const VPlan &Plan)
Verify invariants for general VPlans.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
VPBasicBlock * EarlyExitingVPBB
VPIRBasicBlock * EarlyExitVPBB
This struct is a compact representation of a valid (non-zero power of two) alignment.
An information struct used to provide DenseMap with the various necessary components for a given valu...
Matcher to bind the captured value.
This reduction is unordered with the partial result scaled down by some factor.
Holds the VFShape for a specific scalar to vector function mapping.
Encapsulates information needed to describe a parameter.
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
Struct to hold various analysis needed for cost computations.
const VFSelectionContext & Config
static bool isFreeScalarIntrinsic(Intrinsic::ID ID)
Returns true if ID is a pseudo intrinsic that is dropped via scalarization rather than widened.
bool isMaskRequired(Instruction *I) const
Forwards to LoopVectorizationCostModel::isMaskRequired.
PredicatedScalarEvolution & PSE
bool willBeScalarized(Instruction *I, ElementCount VF) const
Returns true if I is known to be scalarized at VF.
TargetTransformInfo::TargetCostKind CostKind
const TargetLibraryInfo & TLI
const TargetTransformInfo & TTI
const VectorizeOptions & Opts
A recipe for handling first-order recurrence phis.
WrapFlagsTy withoutNoSignedWrap()
An overlay for VPIRInstructions wrapping PHI nodes enabling convenient use cast/dyn_cast/isa and exec...
A VPValue representing a live-in from the input IR or a constant.
Type * getType() const
Returns the type of the underlying IR value.
A recipe for widening load operations, using the address to load from and an optional mask.
A recipe for widening store operations, using the stored value, the address to store to and an option...