LLVM 24.0.0git
VPlanTransforms.cpp
Go to the documentation of this file.
1//===-- VPlanTransforms.cpp - Utility VPlan to VPlan transforms -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements a set of utility VPlan to VPlan transformations.
11///
12//===----------------------------------------------------------------------===//
13
14#include "VPlanTransforms.h"
15#include "VPRecipeBuilder.h"
16#include "VPlan.h"
17#include "VPlanAnalysis.h"
18#include "VPlanCFG.h"
19#include "VPlanDominatorTree.h"
20#include "VPlanHelpers.h"
21#include "VPlanPatternMatch.h"
22#include "VPlanUtils.h"
23#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/SetVector.h"
28#include "llvm/ADT/TypeSwitch.h"
30#include "llvm/Analysis/Loads.h"
36#include "llvm/IR/Intrinsics.h"
37#include "llvm/IR/Metadata.h"
41
42using namespace llvm;
43using namespace VPlanPatternMatch;
44using namespace SCEVPatternMatch;
45
46/// If the pointer operand \p Addr of a memory access is an affine AddRec
47/// w.r.t. \p L with a constant stride, return the stride in units of
48/// \p AccessTy. Otherwise return std::nullopt.
49static std::optional<int64_t> getConstantStride(VPValue *Addr, Type *AccessTy,
51 const Loop *L) {
52 assert(!hasIrregularType(AccessTy, L->getHeader()->getDataLayout()) &&
53 "should not try to widen irregular types");
54 const SCEV *AddrSCEV = vputils::getSCEVExprForVPValue(Addr, PSE, L);
55 auto *AddRec = dyn_cast<SCEVAddRecExpr>(AddrSCEV);
56 if (!AddRec)
57 return {};
58
59 return getStrideFromAddRec(AddRec, L, AccessTy, /*Ptr=*/nullptr, PSE);
60}
61
64 Loop *OuterLoop) {
65
66 // Returns true if the access of \p AccessTy at \p Addr can be widened to a
67 // consecutive vector access.
68 auto IsConsecutiveAccess = [&](VPValue *Addr, Type *AccessTy) {
69 return !hasIrregularType(AccessTy, Plan.getDataLayout()) &&
70 getConstantStride(Addr, AccessTy, PSE, OuterLoop) == 1;
71 };
72
74 Plan.getVectorLoopRegion());
76 // Skip blocks outside region
77 if (!VPBB->getParent())
78 break;
79 VPRecipeBase *Term = VPBB->getTerminator();
80 auto EndIter = Term ? Term->getIterator() : VPBB->end();
81 // Introduce each ingredient into VPlan.
82 for (VPRecipeBase &Ingredient :
83 make_early_inc_range(make_range(VPBB->begin(), EndIter))) {
84
85 VPValue *VPV = Ingredient.getVPSingleValue();
86 if (!VPV->getUnderlyingValue())
87 continue;
88
90
91 // Atomic accesses and fences have ordering/atomicity semantics that
92 // cannot be preserved by lane-wise widening.
94 return false;
95
96 VPRecipeBase *NewRecipe = nullptr;
97 if (auto *PhiR = dyn_cast<VPPhi>(&Ingredient)) {
98 auto *Phi = cast<PHINode>(PhiR->getUnderlyingValue());
99 NewRecipe = new VPWidenPHIRecipe(PhiR->operands(), PhiR->getDebugLoc(),
100 Phi->getName());
101 } else if (auto *VPI = dyn_cast<VPInstruction>(&Ingredient)) {
102 assert(!isa<PHINode>(Inst) && "phis should be handled above");
103 // Create VPWidenMemoryRecipe for loads and stores.
104 if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
105 bool IsConsecutive =
106 IsConsecutiveAccess(VPI->getOperand(0), VPI->getScalarType());
107 NewRecipe = new VPWidenLoadRecipe(*Load, Ingredient.getOperand(0),
108 nullptr /*Mask*/, IsConsecutive,
109 *VPI, Ingredient.getDebugLoc());
110 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
111 bool IsConsecutive = IsConsecutiveAccess(
112 VPI->getOperand(1), VPI->getOperand(0)->getScalarType());
113 NewRecipe = new VPWidenStoreRecipe(
114 *Store, Ingredient.getOperand(1), Ingredient.getOperand(0),
115 nullptr /*Mask*/, IsConsecutive, *VPI, Ingredient.getDebugLoc());
117 NewRecipe = new VPWidenGEPRecipe(GEP->getSourceElementType(),
118 Ingredient.operands(), *VPI,
119 Ingredient.getDebugLoc(), GEP);
120 } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) {
121 Intrinsic::ID VectorID = getVectorIntrinsicIDForCall(CI, &TLI);
122 if (VectorID == Intrinsic::not_intrinsic)
123 return false;
124
125 // The noalias.scope.decl intrinsic declares a noalias scope that
126 // is valid for a single iteration. Emitting it as a single-scalar
127 // replicate would incorrectly extend the scope across multiple
128 // original iterations packed into one vector iteration.
129 // FIXME: If we want to vectorize this loop, then we have to drop
130 // all the associated !alias.scope and !noalias.
131 if (VectorID == Intrinsic::experimental_noalias_scope_decl)
132 return false;
133
134 // These intrinsics are recognized by getVectorIntrinsicIDForCall
135 // but are not widenable. Emit them as replicate instead of widening.
136 if (VectorID == Intrinsic::assume ||
137 VectorID == Intrinsic::lifetime_end ||
138 VectorID == Intrinsic::lifetime_start ||
139 VectorID == Intrinsic::sideeffect ||
140 VectorID == Intrinsic::pseudoprobe) {
141 // If the operand of llvm.assume holds before vectorization, it will
142 // also hold per lane.
143 // llvm.pseudoprobe requires to be duplicated per lane for accurate
144 // sample count.
145 const bool IsSingleScalar = VectorID != Intrinsic::assume &&
146 VectorID != Intrinsic::pseudoprobe;
147 NewRecipe = new VPReplicateRecipe(CI, Ingredient.operands(),
148 /*IsSingleScalar=*/IsSingleScalar,
149 /*Mask=*/nullptr, *VPI, *VPI,
150 Ingredient.getDebugLoc());
151 } else {
152 NewRecipe = new VPWidenIntrinsicRecipe(
153 *CI, VectorID, drop_end(Ingredient.operands()), CI->getType(),
154 VPIRFlags(*CI), *VPI, CI->getDebugLoc());
155 }
156 } else if (auto *CI = dyn_cast<CastInst>(Inst)) {
157 NewRecipe = new VPWidenCastRecipe(
158 CI->getOpcode(), Ingredient.getOperand(0), CI->getType(), CI,
159 VPIRFlags(*CI), VPIRMetadata(*CI));
160 } else {
161 NewRecipe = new VPWidenRecipe(*Inst, Ingredient.operands(), *VPI,
162 *VPI, Ingredient.getDebugLoc());
163 }
164 } else {
166 "inductions must be created earlier");
167 continue;
168 }
169
170 NewRecipe->insertBefore(&Ingredient);
171 if (NewRecipe->getNumDefinedValues() == 1)
172 VPV->replaceAllUsesWith(NewRecipe->getVPSingleValue());
173 else
174 assert(NewRecipe->getNumDefinedValues() == 0 &&
175 "Only recpies with zero or one defined values expected");
176 Ingredient.eraseFromParent();
177 }
178 }
179 return true;
180}
181
182/// Helper for extra no-alias checks via known-safe recipe and SCEV.
185 VPReplicateRecipe &GroupLeader;
186 PredicatedScalarEvolution *PSE = nullptr;
187 const Loop *L = nullptr;
188
189 // Return true if \p A and \p B are known to not alias for all VFs in the
190 // plan, checked via the distance between the accesses
191 bool isNoAliasViaDistance(VPReplicateRecipe *A, VPReplicateRecipe *B) const {
192 if (A->getOpcode() != Instruction::Store ||
193 B->getOpcode() != Instruction::Store)
194 return false;
195
196 if (!PSE || !L)
197 return A == B;
198
199 VPValue *AddrA = A->getOperand(1);
200 const SCEV *SCEVA = vputils::getSCEVExprForVPValue(AddrA, *PSE, L);
201 VPValue *AddrB = B->getOperand(1);
202 const SCEV *SCEVB = vputils::getSCEVExprForVPValue(AddrB, *PSE, L);
204 return false;
205
206 const APInt *Distance;
207 ScalarEvolution &SE = *PSE->getSE();
208 if (!match(SE.getMinusSCEV(SCEVA, SCEVB), m_scev_APInt(Distance)))
209 return false;
210
211 const DataLayout &DL = SE.getDataLayout();
212 Type *TyA = A->getOperand(0)->getScalarType();
213 uint64_t SizeA = DL.getTypeStoreSize(TyA);
214 Type *TyB = B->getOperand(0)->getScalarType();
215 uint64_t SizeB = DL.getTypeStoreSize(TyB);
216
217 // Use the maximum store size to ensure no overlap from either direction.
218 // Currently only handles fixed sizes, as it is only used for
219 // replicating VPReplicateRecipes.
220 uint64_t MaxStoreSize = std::max(SizeA, SizeB);
221
222 auto VFs = B->getParent()->getPlan()->vectorFactors();
224 if (MaxVF.isScalable())
225 return false;
226 return Distance->abs().uge(
227 MaxVF.multiplyCoefficientBy(MaxStoreSize).getFixedValue());
228 }
229
230public:
233 const Loop &L)
234 : ExcludeRecipes(ExcludeRecipes.begin(), ExcludeRecipes.end()),
235 GroupLeader(GroupLeader), PSE(&PSE), L(&L) {}
236
237 SinkStoreInfo(VPReplicateRecipe &GroupLeader) : GroupLeader(GroupLeader) {}
238
239 /// Return true if \p R should be skipped during alias checking, either
240 /// because it's in the exclude set or because no-alias can be proven via
241 /// SCEV.
242 bool shouldSkip(VPRecipeBase &R) const {
244 return ExcludeRecipes.contains(Store) ||
245 (Store && isNoAliasViaDistance(Store, &GroupLeader));
246 }
247};
248
249/// Check if a memory operation doesn't alias with memory operations using
250/// scoped noalias metadata, in blocks in the single-successor chain between \p
251/// FirstBB and \p LastBB. If \p SinkInfo is std::nullopt, only recipes that may
252/// write to memory are checked (for load hoisting). Otherwise recipes that both
253/// read and write memory are checked, and SCEV is used to prove no-alias
254/// between the group leader and other replicate recipes (for store sinking).
255static bool
257 VPBasicBlock *FirstBB, VPBasicBlock *LastBB,
258 std::optional<SinkStoreInfo> SinkInfo = {}) {
259 bool CheckReads = SinkInfo.has_value();
260 for (VPBasicBlock *VPBB :
262 for (VPRecipeBase &R : *VPBB) {
263 if (SinkInfo && SinkInfo->shouldSkip(R))
264 continue;
265
266 // Skip recipes that don't need checking.
267 if (!R.mayWriteToMemory() && !(CheckReads && R.mayReadFromMemory()))
268 continue;
269
271 if (!Loc)
272 // Conservatively assume aliasing for memory operations without
273 // location.
274 return false;
275
277 return false;
278 }
279 }
280 return true;
281}
282
283/// Get the value type of the replicate load or store. \p IsLoad indicates
284/// whether it is a load.
286 return (IsLoad ? R : R->getOperand(0))->getScalarType();
287}
288
289/// Collect either replicated Loads or Stores grouped by their address SCEV and
290/// their load-store type, in a deep-traversal of the vector loop region in \p
291/// Plan.
292template <unsigned Opcode>
295 VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L,
296 function_ref<bool(VPReplicateRecipe *)> FilterFn) {
297 static_assert(Opcode == Instruction::Load || Opcode == Instruction::Store,
298 "Only Load and Store opcodes supported");
299 constexpr bool IsLoad = (Opcode == Instruction::Load);
302 RecipesByAddressAndType;
305 for (VPRecipeBase &R : *VPBB) {
306 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
307 if (!RepR || RepR->getOpcode() != Opcode || !FilterFn(RepR))
308 continue;
309
310 // For loads, operand 0 is address; for stores, operand 1 is address.
311 VPValue *Addr = RepR->getOperand(IsLoad ? 0 : 1);
312 const Type *LoadStoreTy = getLoadStoreValueType(RepR, IsLoad);
313 const SCEV *AddrSCEV = vputils::getSCEVExprForVPValue(Addr, PSE, L);
314 if (!isa<SCEVCouldNotCompute>(AddrSCEV))
315 RecipesByAddressAndType[{AddrSCEV, LoadStoreTy}].push_back(RepR);
316 }
317 }
318 auto Groups = to_vector(RecipesByAddressAndType.values());
319 VPDominatorTree VPDT(Plan);
320 for (auto &Group : Groups) {
321 // Sort mem ops by dominance order, with earliest (most dominating) first.
323 return VPDT.properlyDominates(A, B);
324 });
325 }
326 return Groups;
327}
328
329static bool sinkScalarOperands(VPlan &Plan) {
330 auto Iter = vp_depth_first_deep(Plan.getEntry());
331 bool ScalarVFOnly = Plan.hasScalarVFOnly();
332 bool Changed = false;
333
335 auto InsertIfValidSinkCandidate = [ScalarVFOnly, &WorkList](
336 VPBasicBlock *SinkTo, VPValue *Op) {
337 auto *Candidate = dyn_cast<VPSingleDefRecipe>(Op);
339 VPInstruction>(Candidate))
340 return;
341
342 if (Candidate->getParent() == SinkTo ||
343 all_of(Candidate->operands(),
344 [](VPValue *Op) { return Op->isDefinedOutsideLoopRegions(); }) ||
345 vputils::cannotHoistOrSinkRecipe(*Candidate, /*Sinking=*/true))
346 return;
347
348 if (!ScalarVFOnly && !vputils::doesGeneratePerAllLanes(Candidate))
349 return;
350
351 // Only single-scalar VPInstructions can be sunk.
352 if (auto *VPI = dyn_cast<VPInstruction>(Candidate))
353 if (!vputils::isSingleScalar(VPI))
354 return;
355
356 WorkList.insert({SinkTo, Candidate});
357 };
358
359 // First, collect the operands of all recipes in replicate blocks as seeds for
360 // sinking.
362 VPBasicBlock *EntryVPBB = VPR->getEntryBasicBlock();
363 if (!VPR->isReplicator() || EntryVPBB->getSuccessors().size() != 2)
364 continue;
365 VPBasicBlock *VPBB = cast<VPBasicBlock>(EntryVPBB->getSuccessors().front());
366 if (VPBB->getSingleSuccessor() != VPR->getExitingBasicBlock())
367 continue;
368 for (auto &Recipe : *VPBB)
369 for (VPValue *Op : Recipe.operands())
370 InsertIfValidSinkCandidate(VPBB, Op);
371 }
372
373 // Try to sink each replicate or scalar IV steps recipe in the worklist.
374 for (unsigned I = 0; I != WorkList.size(); ++I) {
375 VPBasicBlock *SinkTo;
376 VPSingleDefRecipe *SinkCandidate;
377 std::tie(SinkTo, SinkCandidate) = WorkList[I];
378
379 // All recipe users of SinkCandidate must be in the same block SinkTo or all
380 // users outside of SinkTo must only use the first lane of SinkCandidate. In
381 // the latter case, we need to duplicate SinkCandidate.
382 auto UsersOutsideSinkTo =
383 make_filter_range(SinkCandidate->users(), [SinkTo](VPUser *U) {
384 return cast<VPRecipeBase>(U)->getParent() != SinkTo;
385 });
386 if (any_of(UsersOutsideSinkTo, [SinkCandidate](VPUser *U) {
387 return !U->usesFirstLaneOnly(SinkCandidate);
388 }))
389 continue;
390 bool NeedsDuplicating = !UsersOutsideSinkTo.empty();
391
392 if (NeedsDuplicating) {
393 if (ScalarVFOnly)
394 continue;
395 VPSingleDefRecipe *Clone;
396 if (auto *SinkCandidateRepR =
397 dyn_cast<VPReplicateRecipe>(SinkCandidate)) {
398 // TODO: Handle converting to uniform recipes as separate transform,
399 // then cloning should be sufficient here.
401 SinkCandidateRepR->getOpcode(), SinkCandidate->operands(),
402 /*Mask=*/nullptr, *SinkCandidateRepR, *SinkCandidateRepR,
403 SinkCandidate->getDebugLoc(), SinkCandidate->getUnderlyingInstr());
404 // TODO: add ".cloned" suffix to name of Clone's VPValue.
405 } else {
406 Clone = SinkCandidate->clone();
407 }
408
409 Clone->insertBefore(SinkCandidate);
410 SinkCandidate->replaceUsesWithIf(Clone, [SinkTo](VPUser &U, unsigned) {
411 return cast<VPRecipeBase>(&U)->getParent() != SinkTo;
412 });
413 }
414 SinkCandidate->moveBefore(*SinkTo, SinkTo->getFirstNonPhi());
415 for (VPValue *Op : SinkCandidate->operands())
416 InsertIfValidSinkCandidate(SinkTo, Op);
417 Changed = true;
418 }
419 return Changed;
420}
421
422/// If \p R is a triangle region, return the 'then' block of the triangle.
424 auto *EntryBB = cast<VPBasicBlock>(R->getEntry());
425 if (EntryBB->getNumSuccessors() != 2)
426 return nullptr;
427
428 auto *Succ0 = dyn_cast<VPBasicBlock>(EntryBB->getSuccessors()[0]);
429 auto *Succ1 = dyn_cast<VPBasicBlock>(EntryBB->getSuccessors()[1]);
430 if (!Succ0 || !Succ1)
431 return nullptr;
432
433 if (Succ0->getNumSuccessors() + Succ1->getNumSuccessors() != 1)
434 return nullptr;
435 if (Succ0->getSingleSuccessor() == Succ1)
436 return Succ0;
437 if (Succ1->getSingleSuccessor() == Succ0)
438 return Succ1;
439 return nullptr;
440}
441
442// Merge replicate regions in their successor region, if a replicate region
443// is connected to a successor replicate region with the same predicate by a
444// single, empty VPBasicBlock.
446 SmallPtrSet<VPRegionBlock *, 4> TransformedRegions;
447
448 // Collect replicate regions followed by an empty block, followed by another
449 // replicate region with matching masks to process front. This is to avoid
450 // iterator invalidation issues while merging regions.
453 vp_depth_first_deep(Plan.getEntry()))) {
454 if (!Region1->isReplicator())
455 continue;
456 auto *MiddleBasicBlock =
457 dyn_cast_or_null<VPBasicBlock>(Region1->getSingleSuccessor());
458 if (!MiddleBasicBlock || !MiddleBasicBlock->empty())
459 continue;
460
461 auto *Region2 =
462 dyn_cast_or_null<VPRegionBlock>(MiddleBasicBlock->getSingleSuccessor());
463 if (!Region2 || !Region2->isReplicator())
464 continue;
465
466 VPValue *Mask1 = Region1->getEntryBranchOnMask()->getOperand(0);
467 VPValue *Mask2 = Region2->getEntryBranchOnMask()->getOperand(0);
468 if (!Mask1 || Mask1 != Mask2)
469 continue;
470
471 assert(Mask1 && Mask2 && "both region must have conditions");
472 WorkList.push_back(Region1);
473 }
474
475 // Move recipes from Region1 to its successor region, if both are triangles.
476 for (VPRegionBlock *Region1 : WorkList) {
477 if (TransformedRegions.contains(Region1))
478 continue;
479 auto *MiddleBasicBlock = cast<VPBasicBlock>(Region1->getSingleSuccessor());
480 auto *Region2 = cast<VPRegionBlock>(MiddleBasicBlock->getSingleSuccessor());
481
482 VPBasicBlock *Then1 = getPredicatedThenBlock(Region1);
483 VPBasicBlock *Then2 = getPredicatedThenBlock(Region2);
484 if (!Then1 || !Then2)
485 continue;
486
487 // Note: No fusion-preventing memory dependencies are expected in either
488 // region. Such dependencies should be rejected during earlier dependence
489 // checks, which guarantee accesses can be re-ordered for vectorization.
490 //
491 // Move recipes to the successor region.
492 for (VPRecipeBase &ToMove : make_early_inc_range(reverse(*Then1)))
493 ToMove.moveBefore(*Then2, Then2->getFirstNonPhi());
494
495 auto *Merge1 = cast<VPBasicBlock>(Then1->getSingleSuccessor());
496 auto *Merge2 = cast<VPBasicBlock>(Then2->getSingleSuccessor());
497
498 // Move VPPredInstPHIRecipes from the merge block to the successor region's
499 // merge block. Update all users inside the successor region to use the
500 // original values.
501 for (VPRecipeBase &Phi1ToMove : make_early_inc_range(reverse(*Merge1))) {
502 VPValue *PredInst1 =
503 cast<VPPredInstPHIRecipe>(&Phi1ToMove)->getOperand(0);
504 VPValue *Phi1ToMoveV = Phi1ToMove.getVPSingleValue();
505 Phi1ToMoveV->replaceUsesWithIf(PredInst1, [Then2](VPUser &U, unsigned) {
506 return cast<VPRecipeBase>(&U)->getParent() == Then2;
507 });
508
509 // Remove phi recipes that are unused after merging the regions.
510 if (Phi1ToMove.getVPSingleValue()->user_empty()) {
511 Phi1ToMove.eraseFromParent();
512 continue;
513 }
514 Phi1ToMove.moveBefore(*Merge2, Merge2->begin());
515 }
516
517 // Remove the dead recipes in Region1's entry block.
518 for (VPRecipeBase &R :
519 make_early_inc_range(reverse(*Region1->getEntryBasicBlock())))
520 R.eraseFromParent();
521
522 // Finally, remove the first region.
523 for (VPBlockBase *Pred : make_early_inc_range(Region1->getPredecessors())) {
524 VPBlockUtils::disconnectBlocks(Pred, Region1);
525 VPBlockUtils::connectBlocks(Pred, MiddleBasicBlock);
526 }
527 VPBlockUtils::disconnectBlocks(Region1, MiddleBasicBlock);
528 TransformedRegions.insert(Region1);
529 }
530
531 return !TransformedRegions.empty();
532}
533
535 VPRegionBlock *ParentRegion,
536 VPlan &Plan) {
537 Instruction *Instr = PredRecipe->getUnderlyingInstr();
538 // Build the triangular if-then region.
539 std::string RegionName = (Twine("pred.") + Instr->getOpcodeName()).str();
540 assert(Instr->getParent() && "Predicated instruction not in any basic block");
541 auto *BlockInMask = PredRecipe->getMask();
542 auto *MaskDef = BlockInMask->getDefiningRecipe();
543 auto *BOMRecipe = new VPBranchOnMaskRecipe(
544 BlockInMask, MaskDef ? MaskDef->getDebugLoc() : DebugLoc::getUnknown());
545 auto *Entry =
546 Plan.createVPBasicBlock(Twine(RegionName) + ".entry", BOMRecipe);
547
548 // Replace predicated replicate recipe with a replicate recipe without a
549 // mask but in the replicate region.
550 auto *RecipeWithoutMask = new VPReplicateRecipe(
551 PredRecipe->getUnderlyingInstr(), PredRecipe->operandsWithoutMask(),
552 PredRecipe->isSingleScalar(), nullptr /*Mask*/, *PredRecipe, *PredRecipe,
553 PredRecipe->getDebugLoc());
554 auto *Pred =
555 Plan.createVPBasicBlock(Twine(RegionName) + ".if", RecipeWithoutMask);
556 auto *Exiting = Plan.createVPBasicBlock(Twine(RegionName) + ".continue");
558 Plan.createReplicateRegion(Entry, Exiting, RegionName);
559
560 // Note: first set Entry as region entry and then connect successors starting
561 // from it in order, to propagate the "parent" of each VPBasicBlock.
562 Region->setParent(ParentRegion);
563 VPBlockUtils::insertTwoBlocksAfter(Pred, Exiting, Entry);
564 VPBlockUtils::connectBlocks(Pred, Exiting);
565
566 if (!PredRecipe->user_empty()) {
567 auto *PHIRecipe = new VPPredInstPHIRecipe(RecipeWithoutMask,
568 RecipeWithoutMask->getDebugLoc());
569 Exiting->appendRecipe(PHIRecipe);
570 PredRecipe->replaceAllUsesWith(PHIRecipe);
571 }
572 PredRecipe->eraseFromParent();
573 return Region;
574}
575
576static void addReplicateRegions(VPlan &Plan) {
579 vp_depth_first_deep(Plan.getEntry()))) {
580 for (VPRecipeBase &R : *VPBB)
581 if (auto *RepR = dyn_cast<VPReplicateRecipe>(&R)) {
582 if (RepR->isPredicated())
583 WorkList.push_back(RepR);
584 }
585 }
586
587 unsigned BBNum = 0;
588 for (VPReplicateRecipe *RepR : WorkList) {
589 VPBasicBlock *CurrentBlock = RepR->getParent();
590 VPBasicBlock *SplitBlock = CurrentBlock->splitAt(RepR->getIterator());
591
592 BasicBlock *OrigBB = RepR->getUnderlyingInstr()->getParent();
593 SplitBlock->setName(
594 OrigBB->hasName() ? OrigBB->getName() + "." + Twine(BBNum++) : "");
595 // Record predicated instructions for above packing optimizations.
597 createReplicateRegion(RepR, CurrentBlock->getParent(), Plan);
599
600 VPRegionBlock *ParentRegion = Region->getParent();
601 if (ParentRegion && ParentRegion->getExiting() == CurrentBlock)
602 ParentRegion->setExiting(SplitBlock);
603 }
604}
605
609 vp_depth_first_deep(Plan.getEntry()))) {
610 // Don't fold the blocks in the skeleton of the Plan into their single
611 // predecessors for now.
612 // TODO: Remove restriction once more of the skeleton is modeled in VPlan.
613 if (!VPBB->getParent())
614 continue;
615 auto *PredVPBB =
616 dyn_cast_or_null<VPBasicBlock>(VPBB->getSinglePredecessor());
617 if (!PredVPBB || PredVPBB->getNumSuccessors() != 1 ||
618 isa<VPIRBasicBlock>(PredVPBB))
619 continue;
620 WorkList.push_back(VPBB);
621 }
622
623 for (VPBasicBlock *VPBB : WorkList) {
624 VPBasicBlock *PredVPBB = cast<VPBasicBlock>(VPBB->getSinglePredecessor());
625 for (VPRecipeBase &R : make_early_inc_range(*VPBB))
626 R.moveBefore(*PredVPBB, PredVPBB->end());
627 VPBlockUtils::disconnectBlocks(PredVPBB, VPBB);
628 auto *ParentRegion = VPBB->getParent();
629 if (ParentRegion && ParentRegion->getExiting() == VPBB)
630 ParentRegion->setExiting(PredVPBB);
631 VPBlockUtils::transferSuccessors(VPBB, PredVPBB);
632 // VPBB is now dead and will be cleaned up when the plan gets destroyed.
633 }
634 return !WorkList.empty();
635}
636
638 // Convert masked VPReplicateRecipes to if-then region blocks.
640
641 bool ShouldSimplify = true;
642 while (ShouldSimplify) {
643 ShouldSimplify = sinkScalarOperands(Plan);
644 ShouldSimplify |= mergeReplicateRegionsIntoSuccessors(Plan);
645 ShouldSimplify |= mergeBlocksIntoPredecessors(Plan);
646 }
647}
648
649/// Remove redundant casts of inductions.
650///
651/// Such redundant casts are casts of induction variables that can be ignored,
652/// because we already proved that the casted phi is equal to the uncasted phi
653/// in the vectorized loop. There is no need to vectorize the cast - the same
654/// value can be used for both the phi and casts in the vector loop.
656 for (auto &Phi : Plan.getVectorLoopRegion()->getEntryBasicBlock()->phis()) {
658 if (!IV || IV->getTruncInst())
659 continue;
660
661 // A sequence of IR Casts has potentially been recorded for IV, which
662 // *must be bypassed* when the IV is vectorized, because the vectorized IV
663 // will produce the desired casted value. This sequence forms a def-use
664 // chain and is provided in reverse order, ending with the cast that uses
665 // the IV phi. Search for the recipe of the last cast in the chain and
666 // replace it with the original IV. Note that only the final cast is
667 // expected to have users outside the cast-chain and the dead casts left
668 // over will be cleaned up later.
669 ArrayRef<Instruction *> Casts = IV->getInductionDescriptor().getCastInsts();
670 VPValue *FindMyCast = IV;
671 for (Instruction *IRCast : reverse(Casts)) {
672 VPSingleDefRecipe *FoundUserCast = nullptr;
673 for (auto *U : FindMyCast->users()) {
674 auto *UserCast = dyn_cast<VPSingleDefRecipe>(U);
675 if (UserCast && UserCast->getUnderlyingValue() == IRCast) {
676 FoundUserCast = UserCast;
677 break;
678 }
679 }
680 // A cast recipe in the chain may have been removed by earlier DCE.
681 if (!FoundUserCast)
682 break;
683 FindMyCast = FoundUserCast;
684 }
685 if (FindMyCast != IV)
686 FindMyCast->replaceAllUsesWith(IV);
687 }
688}
689
690/// If R is a phi-like recipe starting a dead cycle of recipes, erase all
691/// reachable recipes of the dead cycle.
693 auto *PhiR = dyn_cast<VPSingleDefRecipe>(R);
694 if (!PhiR || !isa<VPPhi, VPReductionPHIRecipe>(R))
695 return;
696
697 // The transitive users of PhiR are closed under users, so the cycle is dead
698 // if every one of them can be erased.
700 auto *R = cast<VPRecipeBase>(U);
701 // Bail out if a user must be retained, or if it is a phi-like recipe other
702 // than PhiR;
703 if (R->mayHaveSideEffects() || (R != PhiR && isa<VPPhiAccessors>(R)))
704 return;
705 }
706
707 // Break the cycle by replacing PhiR with its first incoming value, which is
708 // defined outside the cycle. That leaves the rest of the cycle dead.
709 PhiR->replaceAllUsesWith(PhiR->getOperand(0));
710 SmallVector<VPValue *> Incoming(PhiR->operands());
711 PhiR->eraseFromParent();
712 for (VPValue *Op : Incoming)
714}
715
718 Plan.getEntry());
720 // The recipes in the block are processed in reverse order, to catch chains
721 // of dead recipes.
722 for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
723 if (vputils::isDeadRecipe(R)) {
724 R.eraseFromParent();
725 continue;
726 }
727
728 // If R is a phi-like recipe starting a dead cycle of recipes, erase the
729 // whole cycle.
731 }
732 }
733}
734
735/// Legalize VPWidenPointerInductionRecipe, by replacing it with a PtrAdd
736/// (IndStart, ScalarIVSteps (0, Step)) if only its scalar values are used, as
737/// VPWidenPointerInductionRecipe will generate vectors only. If some users
738/// require vectors while other require scalars, the scalar uses need to extract
739/// the scalars from the generated vectors (Note that this is different to how
740/// int/fp inductions are handled). Legalize extract-from-ends using uniform
741/// VPReplicateRecipe of wide inductions to use regular VPReplicateRecipe, so
742/// the correct end value is available. Also optimize
743/// VPWidenIntOrFpInductionRecipe, if any of its users needs scalar values, by
744/// providing them scalar steps built on the canonical scalar IV and update the
745/// original IV's users. This is an optional optimization to reduce the needs of
746/// vector extracts.
749 bool HasOnlyVectorVFs = !Plan.hasScalarVFOnly();
750
752 for (VPRecipeBase &Phi : HeaderVPBB->phis())
753 if (auto *PhiR = dyn_cast<VPWidenInductionRecipe>(&Phi))
754 WideIVs.push_back(PhiR);
755
756 // Try to narrow wide and replicating recipes to uniform recipes, based on
757 // VPlan analysis.
758 // TODO: Apply to all recipes in the future, to replace legacy uniformity
759 // analysis.
760 for (VPWidenInductionRecipe *PhiR : WideIVs) {
762 for (VPUser *U : reverse(Users)) {
763 auto *Def = dyn_cast<VPRecipeWithIRFlags>(U);
764 auto *RepR = dyn_cast<VPReplicateRecipe>(U);
765 // Skip recipes that shouldn't be narrowed.
766 if (!Def || !isa<VPReplicateRecipe, VPWidenRecipe>(Def) ||
767 Def->user_empty() || !Def->getUnderlyingValue() ||
768 (RepR && (RepR->isSingleScalar() || RepR->isPredicated())))
769 continue;
770
771 // Skip recipes that may have other lanes than their first used.
773 continue;
774
775 // TODO: Support scalarizing ExtractValue.
776 if (match(Def,
778 continue;
779
781 Def->getUnderlyingInstr()->getOpcode(), Def->operands(),
782 /*Mask=*/nullptr, *Def, {}, DebugLoc::getUnknown(),
783 Def->getUnderlyingInstr());
784 Clone->insertAfter(Def);
785 Def->replaceAllUsesWith(Clone);
786 Def->eraseFromParent();
787 }
788 }
789
790 VPBuilder Builder(HeaderVPBB, HeaderVPBB->getFirstNonPhi());
791 for (VPWidenInductionRecipe *PhiR : WideIVs) {
792 // Replace wide pointer inductions which have only their scalars used by
793 // PtrAdd(IndStart, ScalarIVSteps (0, Step)).
794 if (auto *PtrIV = dyn_cast<VPWidenPointerInductionRecipe>(PhiR)) {
795 if (!Plan.hasScalarVFOnly() &&
796 !PtrIV->onlyScalarsGenerated(Plan.hasScalableVF()))
797 continue;
798
799 VPValue *PtrAdd =
800 vputils::scalarizeVPWidenPointerInduction(PtrIV, Plan, Builder);
801 PtrIV->replaceAllUsesWith(PtrAdd);
802 continue;
803 }
804
805 // Replace widened induction with scalar steps for users that only use
806 // scalars.
807 auto *WideIV = cast<VPWidenIntOrFpInductionRecipe>(PhiR);
808 if (HasOnlyVectorVFs && none_of(WideIV->users(), [WideIV](VPUser *U) {
809 return U->usesScalars(WideIV);
810 }))
811 continue;
812
813 const InductionDescriptor &ID = WideIV->getInductionDescriptor();
814 VPIRFlags::WrapFlagsTy WrapFlags;
815 // We can preserve nuw when the step is non-negative.
816 const APInt *Step;
817 if (match(WideIV->getStepValue(), m_APInt(Step)) && Step->isNonNegative())
818 WrapFlags = {static_cast<bool>(WideIV->getNoWrapFlagsOrNone().HasNUW),
819 false};
821 Plan, ID.getKind(), ID.getInductionOpcode(),
822 dyn_cast_or_null<FPMathOperator>(ID.getInductionBinOp()),
823 WideIV->getTruncInst(), WideIV->getStartValue(), WideIV->getStepValue(),
824 WideIV->getDebugLoc(), Builder, WrapFlags);
825
826 // Update scalar users of IV to use Step instead.
827 if (!HasOnlyVectorVFs) {
828 assert(!Plan.hasScalableVF() &&
829 "plans containing a scalar VF cannot also include scalable VFs");
830 WideIV->replaceAllUsesWith(Steps);
831 } else {
832 bool HasScalableVF = Plan.hasScalableVF();
833 WideIV->replaceUsesWithIf(Steps,
834 [WideIV, HasScalableVF](VPUser &U, unsigned) {
835 if (HasScalableVF)
836 return U.usesFirstLaneOnly(WideIV);
837 return U.usesScalars(WideIV);
838 });
839 }
840 }
841}
842
843/// Check if \p VPV is an untruncated wide induction, either before or after the
844/// increment. If so return the header IV (before the increment), otherwise
845/// return null.
848 auto *WideIV = dyn_cast<VPWidenInductionRecipe>(VPV);
849 if (WideIV) {
850 // VPV itself is a wide induction, separately compute the end value for exit
851 // users if it is not a truncated IV.
852 auto *IntOrFpIV = dyn_cast<VPWidenIntOrFpInductionRecipe>(WideIV);
853 return (IntOrFpIV && IntOrFpIV->getTruncInst()) ? nullptr : WideIV;
854 }
855
856 // Check if VPV is an optimizable induction increment.
857 VPRecipeBase *Def = VPV->getDefiningRecipe();
858 if (!Def || Def->getNumOperands() != 2)
859 return nullptr;
860 WideIV = dyn_cast<VPWidenInductionRecipe>(Def->getOperand(0));
861 if (!WideIV)
862 WideIV = dyn_cast<VPWidenInductionRecipe>(Def->getOperand(1));
863 if (!WideIV)
864 return nullptr;
865
866 auto IsWideIVInc = [&]() {
867 auto &ID = WideIV->getInductionDescriptor();
868
869 // Check if VPV increments the induction by the induction step.
870 VPValue *IVStep = WideIV->getStepValue();
871 switch (ID.getInductionOpcode()) {
872 case Instruction::Add:
873 return match(VPV, m_c_Add(m_Specific(WideIV), m_Specific(IVStep)));
874 case Instruction::FAdd:
875 return match(VPV, m_c_FAdd(m_Specific(WideIV), m_Specific(IVStep)));
876 case Instruction::FSub:
877 return match(VPV, m_Binary<Instruction::FSub>(m_Specific(WideIV),
878 m_Specific(IVStep)));
879 case Instruction::Sub: {
880 // IVStep will be the negated step of the subtraction. Check if Step == -1
881 // * IVStep.
882 VPValue *Step;
883 if (!match(VPV, m_Sub(m_VPValue(), m_VPValue(Step))))
884 return false;
885 const SCEV *IVStepSCEV = vputils::getSCEVExprForVPValue(IVStep, PSE);
886 const SCEV *StepSCEV = vputils::getSCEVExprForVPValue(Step, PSE);
887 ScalarEvolution &SE = *PSE.getSE();
888 return !isa<SCEVCouldNotCompute>(IVStepSCEV) &&
889 !isa<SCEVCouldNotCompute>(StepSCEV) &&
890 IVStepSCEV == SE.getNegativeSCEV(StepSCEV);
891 }
892 default:
893 return ID.getKind() == InductionDescriptor::IK_PtrInduction &&
894 match(VPV, m_GetElementPtr(m_Specific(WideIV),
895 m_Specific(WideIV->getStepValue())));
896 }
897 llvm_unreachable("should have been covered by switch above");
898 };
899 return IsWideIVInc() ? WideIV : nullptr;
900}
901
902/// Attempts to optimize the induction variable exit values for users in the
903/// early exit block.
906 VPValue *Incoming, *Mask;
908 m_VPValue(Incoming))))
909 return nullptr;
910
911 auto *WideIV = getOptimizableIVOf(Incoming, PSE);
912 if (!WideIV)
913 return nullptr;
914
915 // Calculate the final index.
916 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
917 auto *CanonicalIV = LoopRegion->getCanonicalIV();
918 Type *CanonicalIVType = LoopRegion->getCanonicalIVType();
919 auto *ExtractR = cast<VPInstruction>(Op);
920 VPBuilder B(ExtractR);
921
922 DebugLoc DL = ExtractR->getDebugLoc();
923 VPValue *FirstActiveLane = B.createFirstActiveLane(Mask, DL);
924 FirstActiveLane =
925 B.createScalarZExtOrTrunc(FirstActiveLane, CanonicalIVType, DL);
926 VPValue *EndValue = B.createAdd(CanonicalIV, FirstActiveLane, DL);
927
928 // `getOptimizableIVOf()` always returns the pre-incremented IV, so if it
929 // changed it means the exit is using the incremented value, so we need to
930 // add the step.
931 if (Incoming != WideIV) {
932 VPValue *One = Plan.getConstantInt(CanonicalIVType, 1);
933 EndValue = B.createAdd(EndValue, One, DL);
934 }
935
936 if (!match(WideIV, m_CanonicalWidenIV())) {
937 const InductionDescriptor &ID = WideIV->getInductionDescriptor();
938 VPIRValue *Start = WideIV->getStartValue();
939 VPValue *Step = WideIV->getStepValue();
940 EndValue = B.createDerivedIV(
941 ID.getKind(), dyn_cast_or_null<FPMathOperator>(ID.getInductionBinOp()),
942 Start, EndValue, Step);
943 }
944
945 return EndValue;
946}
947
948/// Compute the end value for \p WideIV, unless it is truncated. Creates a
949/// VPDerivedIVRecipe for non-canonical inductions.
951 VPBuilder &VectorPHBuilder,
952 VPValue *VectorTC) {
953 auto *WideIntOrFp = dyn_cast<VPWidenIntOrFpInductionRecipe>(WideIV);
954 // Truncated wide inductions resume from the last lane of their vector value
955 // in the last vector iteration which is handled elsewhere.
956 if (WideIntOrFp && WideIntOrFp->getTruncInst())
957 return nullptr;
958
959 VPIRValue *Start = WideIV->getStartValue();
960 VPValue *Step = WideIV->getStepValue();
961 const InductionDescriptor &ID = WideIV->getInductionDescriptor();
962 VPValue *EndValue = VectorTC;
963 if (!match(WideIV, m_CanonicalWidenIV())) {
964 EndValue = VectorPHBuilder.createDerivedIV(
965 ID.getKind(), dyn_cast_or_null<FPMathOperator>(ID.getInductionBinOp()),
966 Start, VectorTC, Step);
967 }
968
969 // EndValue is derived from the vector trip count (which has the same type as
970 // the widest induction) and thus may be wider than the induction here.
971 Type *ScalarTypeOfWideIV = WideIV->getScalarType();
972 if (ScalarTypeOfWideIV != EndValue->getScalarType()) {
973 EndValue = VectorPHBuilder.createScalarCast(Instruction::Trunc, EndValue,
974 ScalarTypeOfWideIV,
975 WideIV->getDebugLoc());
976 }
977
978 return EndValue;
979}
980
981/// Attempts to optimize the induction variable exit values for users in the
982/// exit block coming from the latch in the original scalar loop.
983static VPValue *
987 VPValue *Incoming;
990 m_VPValue(Incoming)))))
991 return nullptr;
992
993 VPWidenInductionRecipe *WideIV = getOptimizableIVOf(Incoming, PSE);
994 if (!WideIV)
995 return nullptr;
996
997 VPValue *EndValue = EndValues.lookup(WideIV);
998 assert(EndValue && "Must have computed the end value up front");
999
1000 // `getOptimizableIVOf()` always returns the pre-incremented IV, so if it
1001 // changed it means the exit is using the incremented value, so we don't
1002 // need to subtract the step.
1003 if (Incoming != WideIV)
1004 return EndValue;
1005
1006 // Otherwise, subtract the step from the EndValue.
1007 auto *ExtractR = cast<VPInstruction>(Op);
1008 VPBuilder B(ExtractR);
1009 VPValue *Step = WideIV->getStepValue();
1010 Type *ScalarTy = WideIV->getScalarType();
1011 if (ScalarTy->isIntegerTy())
1012 return B.createSub(EndValue, Step, DebugLoc::getUnknown(), "ind.escape");
1013 if (ScalarTy->isPointerTy()) {
1014 Type *StepTy = Step->getScalarType();
1015 auto *Zero = Plan.getZero(StepTy);
1016 return B.createPtrAdd(EndValue, B.createSub(Zero, Step),
1017 DebugLoc::getUnknown(), "ind.escape");
1018 }
1019 if (ScalarTy->isFloatingPointTy()) {
1020 const auto &ID = WideIV->getInductionDescriptor();
1021 return B.createNaryOp(
1022 ID.getInductionBinOp()->getOpcode() == Instruction::FAdd
1023 ? Instruction::FSub
1024 : Instruction::FAdd,
1025 {EndValue, Step}, {ID.getInductionBinOp()->getFastMathFlags()});
1026 }
1027 llvm_unreachable("all possible induction types must be handled");
1028 return nullptr;
1029}
1030
1033 VPValue *ResumeTC,
1034 const Loop *L) {
1035 VPValue *Incoming;
1037 return nullptr;
1038
1039 const SCEV *IncomingSCEV = vputils::getSCEVExprForVPValue(Incoming, PSE, L);
1040 const SCEV *Start, *Step;
1041 if (!match(IncomingSCEV, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step),
1042 m_SpecificLoop(L))))
1043 return nullptr;
1044
1045 auto *ExtractR = cast<VPInstruction>(Op);
1046 DebugLoc DL = ExtractR->getDebugLoc();
1047 VPBuilder Builder(ExtractR);
1048 VPSCEVExpander Expander(Builder, *PSE.getSE(), DL);
1049 VPValue *StartVPV = Expander.expand(Start);
1050 VPValue *StepVPV = Expander.expand(Step);
1051
1052 Type *StartTy = StartVPV->getScalarType();
1053 assert(StartTy->isIntOrPtrTy() && "The type must be SCEVable");
1057 Type *TCTy = ResumeTC->getScalarType();
1058 VPValue *ExitCount = Builder.createOverflowingOp(
1059 Instruction::Sub, {ResumeTC, Plan.getConstantInt(TCTy, 1)},
1060 {/*HasNUW=*/true, /*HasNSW=*/false}, DebugLoc::getUnknown());
1061 return Builder.createDerivedIV(Kind, /*FPBinOp=*/nullptr, StartVPV, ExitCount,
1062 StepVPV);
1063}
1064
1066 VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L) {
1067 // Compute end values for all inductions.
1068 VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
1069 auto *VectorPH = cast<VPBasicBlock>(VectorRegion->getSinglePredecessor());
1070 VPBuilder VectorPHBuilder(VectorPH, VectorPH->begin());
1072 VPValue *ResumeTC =
1073 Plan.hasTailFolded() ? Plan.getTripCount() : &Plan.getVectorTripCount();
1074 for (auto &Phi : VectorRegion->getEntryBasicBlock()->phis()) {
1075 auto *WideIV = dyn_cast<VPWidenInductionRecipe>(&Phi);
1076 if (!WideIV)
1077 continue;
1078 if (VPValue *EndValue =
1079 tryToComputeEndValueForInduction(WideIV, VectorPHBuilder, ResumeTC))
1080 EndValues[WideIV] = EndValue;
1081 }
1082
1083 VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
1084 for (VPRecipeBase &R : make_early_inc_range(*MiddleVPBB)) {
1085 VPValue *Op;
1086 if (!match(&R, m_ExitingIVValue(m_VPValue(Op))))
1087 continue;
1088 auto *WideIV = cast<VPWidenInductionRecipe>(Op);
1089 if (VPValue *EndValue = EndValues.lookup(WideIV)) {
1090 R.getVPSingleValue()->replaceAllUsesWith(EndValue);
1091 R.eraseFromParent();
1092 }
1093 }
1094
1095 // Then, optimize exit block users.
1096 for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks()) {
1097 for (VPRecipeBase &R : ExitVPBB->phis()) {
1098 auto *ExitIRI = cast<VPIRPhi>(&R);
1099
1100 for (auto [Idx, PredVPBB] : enumerate(ExitVPBB->getPredecessors())) {
1101 VPValue *Escape = nullptr;
1102 if (PredVPBB == MiddleVPBB) {
1104 Plan, ExitIRI->getOperand(Idx), EndValues, PSE);
1105 if (!Escape)
1107 Plan, ExitIRI->getOperand(Idx), PSE, ResumeTC, L);
1108 } else {
1110 Plan, ExitIRI->getOperand(Idx), PSE);
1111 }
1112 if (Escape)
1113 ExitIRI->setOperand(Idx, Escape);
1114 }
1115 }
1116 }
1117}
1118
1119/// Remove redundant ExpandSCEVRecipes in \p Plan's entry block by replacing
1120/// them with already existing recipes expanding the same SCEV expression.
1123
1124 for (VPRecipeBase &R :
1126 auto *ExpR = dyn_cast<VPExpandSCEVRecipe>(&R);
1127 if (!ExpR)
1128 continue;
1129
1130 const auto &[V, Inserted] = SCEV2VPV.try_emplace(ExpR->getSCEV(), ExpR);
1131 if (Inserted)
1132 continue;
1133
1134 ExpR->replaceAllUsesWith(V->second);
1135 if (ExpR == Plan.getTripCount())
1136 Plan.resetTripCount(V->second);
1137
1138 ExpR->eraseFromParent();
1139 }
1140}
1141
1142/// Try to simplify logical and bitwise recipes in \p Def.
1144 VPBuilder &Builder,
1145 bool CanCreateNewRecipe) {
1146 VPlan *Plan = Def->getParent()->getPlan();
1147
1148 // Simplify (X && Y) | (X && !Y) -> X.
1149 // TODO: Split up into simpler, modular combines: (X && Y) | (X && Z) into X
1150 // && (Y | Z) and (X | !X) into true. This requires queuing newly created
1151 // recipes to be visited during simplification.
1152 VPValue *X, *Y, *Z;
1153 if (match(Def,
1156 return X;
1157
1158 // x | AllOnes -> AllOnes
1159 if (match(Def, m_c_BinaryOr(m_VPValue(X), m_AllOnes())))
1160 return Plan->getAllOnesValue(Def->getScalarType());
1161
1162 // x | 0 -> x
1163 if (match(Def, m_c_BinaryOr(m_VPValue(X), m_ZeroInt())))
1164 return X;
1165
1166 // x | !x -> AllOnes
1168 return Plan->getAllOnesValue(Def->getScalarType());
1169
1170 // x & 0 -> 0
1171 if (match(Def, m_c_BinaryAnd(m_VPValue(X), m_ZeroInt())))
1172 return Plan->getZero(Def->getScalarType());
1173
1174 // x & AllOnes -> x
1175 if (match(Def, m_c_BinaryAnd(m_VPValue(X), m_AllOnes())))
1176 return X;
1177
1178 // x && false -> false
1179 if (match(Def, m_c_LogicalAnd(m_VPValue(X), m_False())))
1180 return Plan->getFalse();
1181
1182 // x && true -> x
1183 if (match(Def, m_c_LogicalAnd(m_VPValue(X), m_True())))
1184 return X;
1185
1186 // (x && y) | (x && z) -> x && (y | z)
1187 if (CanCreateNewRecipe &&
1190 // Simplify only if one of the operands has one use to avoid creating an
1191 // extra recipe.
1192 (!Def->getOperand(0)->hasMoreThanOneUniqueUser() ||
1193 !Def->getOperand(1)->hasMoreThanOneUniqueUser()))
1194 return Builder.createLogicalAnd(X, Builder.createOr(Y, Z));
1195
1196 // x && (x && y) -> x && y
1197 if (match(Def, m_LogicalAnd(m_VPValue(X),
1199 return Def->getOperand(1);
1200
1201 // x && (y && x) -> x && y
1202 if (match(Def, m_LogicalAnd(m_VPValue(X),
1204 return Builder.createLogicalAnd(X, Y);
1205
1206 // x && !x -> 0
1208 return Plan->getFalse();
1209
1210 if (match(Def, m_Select(m_VPValue(), m_VPValue(X), m_Deferred(X))))
1211 return X;
1212
1213 // select c, false, true -> not c
1214 VPValue *C;
1215 if (CanCreateNewRecipe &&
1216 match(Def, m_Select(m_VPValue(C), m_False(), m_True())))
1217 return Builder.createNot(C);
1218
1219 // select !c, x, y -> select c, y, x
1220 if (match(Def, m_Select(m_Not(m_VPValue(C)), m_VPValue(X), m_VPValue(Y)))) {
1221 Def->setOperand(0, C);
1222 Def->setOperand(1, Y);
1223 Def->setOperand(2, X);
1224 return Def;
1225 }
1226
1227 // select x, (i1 y | z), y -> y | (x && z)
1228 if (CanCreateNewRecipe &&
1229 match(Def, m_Select(m_VPValue(X),
1231 m_Deferred(Y))) &&
1232 Y->getScalarType()->isIntegerTy(1))
1233 return Builder.createOr(Y, Builder.createLogicalAnd(X, Z));
1234
1235 // select %M0, (select %M1, %X, %Y), %Y -> select (%M0 && %M1), %X, %Y
1236 VPValue *Mask0, *Mask1;
1237 if (CanCreateNewRecipe &&
1238 match(Def,
1239 m_SelectLike(m_VPValue(Mask0),
1241 m_VPValue(Y))),
1242 m_Deferred(Y))))
1243 return Builder.createSelect(Builder.createLogicalAnd(Mask0, Mask1), X, Y,
1244 Def->getDebugLoc());
1245
1246 return nullptr;
1247}
1248
1249/// Try to simplify VPSingleDefRecipe \p Def. Returns a new recipe if it should
1250/// be replaced, or the existing recipe if it was modified. Returns nullptr if
1251/// nothing was simplified.
1253 VPlan *Plan = Def->getParent()->getPlan();
1254
1255 // Simplification of live-in IR values for SingleDef recipes using
1256 // InstSimplifyFolder.
1257 const DataLayout &DL = Plan->getDataLayout();
1258 if (VPValue *V = vputils::tryToFoldLiveIns(*Def, Def->operands(), DL))
1259 return V;
1260
1261 // Fold PredPHI LiveIn -> LiveIn.
1262 if (auto *PredPHI = dyn_cast<VPPredInstPHIRecipe>(Def)) {
1263 VPValue *Op = PredPHI->getOperand(0);
1264 if (isa<VPIRValue>(Op))
1265 return Op;
1266 }
1267
1268 // Drop the mask of a predicated store masked by the header mask (which is
1269 // guaranteed to be true at least for the first lane) and both the stored
1270 // value and the address are uniform across VF and UF. The header mask is
1271 // still the abstract region value here.
1272 if (auto *RepR = dyn_cast<VPReplicateRecipe>(Def);
1273 RepR && RepR->isPredicated() && RepR->getOpcode() == Instruction::Store &&
1274 all_of(RepR->operandsWithoutMask(), vputils::isUniformAcrossVFsAndUFs) &&
1275 match(RepR->getMask(), m_HeaderMask())) {
1276 auto *Unmasked = new VPReplicateRecipe(
1277 RepR->getUnderlyingInstr(), RepR->operandsWithoutMask(),
1278 RepR->isSingleScalar(), /*Mask=*/nullptr, *RepR, *RepR,
1279 RepR->getDebugLoc());
1280 Unmasked->insertBefore(RepR);
1281 return Unmasked;
1282 }
1283
1284 VPBuilder Builder(Def);
1285
1286 // Avoid replacing VPInstructions with underlying values with new
1287 // VPInstructions, as we would fail to create widen/replicate recpes from the
1288 // new VPInstructions without an underlying value, and miss out on some
1289 // transformations that only apply to widened/replicated recipes later, by
1290 // doing so.
1291 // TODO: We should also not replace non-VPInstructions like VPWidenRecipe with
1292 // VPInstructions without underlying values, as those will get skipped during
1293 // cost computation.
1294 bool CanCreateNewRecipe =
1295 !isa<VPInstruction>(Def) || !Def->getUnderlyingValue();
1296
1297 VPValue *A, *Z;
1298
1299 // A bitcast to the same type is a no-op.
1300 if (match(Def, m_BitCast(m_VPValue(A))) &&
1301 Def->getScalarType() == A->getScalarType())
1302 return A;
1303
1304 if (match(Def, m_Trunc(m_VPValue(Z, m_ZExtOrSExt(m_VPValue(A)))))) {
1305 Type *TruncTy = Def->getScalarType();
1306 Type *ATy = A->getScalarType();
1307 if (TruncTy == ATy) {
1308 return A;
1309 } else {
1310 // Don't replace a non-widened cast recipe with a widened cast.
1311 if (!isa<VPWidenCastRecipe>(Def))
1312 return nullptr;
1313 if (ATy->getScalarSizeInBits() < TruncTy->getScalarSizeInBits()) {
1314
1315 unsigned ExtOpcode = match(Z, m_SExt(m_VPValue())) ? Instruction::SExt
1316 : Instruction::ZExt;
1317 auto *Ext = Builder.createWidenCast(Instruction::CastOps(ExtOpcode), A,
1318 TruncTy);
1319 if (auto *UnderlyingExt = Z->getUnderlyingValue()) {
1320 // UnderlyingExt has distinct return type, used to retain legacy cost.
1321 Ext->setUnderlyingValue(UnderlyingExt);
1322 }
1323 return Ext;
1324 } else if (ATy->getScalarSizeInBits() > TruncTy->getScalarSizeInBits()) {
1325 auto *Trunc = Builder.createWidenCast(Instruction::Trunc, A, TruncTy);
1326 return Trunc;
1327 }
1328 }
1329 }
1330
1331 if (VPValue *V = simplifyLogicalRecipe(Def, Builder, CanCreateNewRecipe))
1332 return V;
1333
1334 VPValue *X, *Y;
1335 if (match(Def, m_c_Add(m_VPValue(A), m_ZeroInt())))
1336 return A;
1337
1338 if (match(Def, m_c_Mul(m_VPValue(A), m_One())))
1339 return A;
1340
1341 if (match(Def, m_c_Mul(m_VPValue(A), m_ZeroInt())))
1342 return Plan->getZero(Def->getScalarType());
1343
1344 if (CanCreateNewRecipe && match(Def, m_c_Mul(m_VPValue(A), m_AllOnes()))) {
1345 // Preserve nsw from the Mul on the new Sub.
1347 false, cast<VPRecipeWithIRFlags>(Def)->hasNoSignedWrap()};
1348 return Builder.createSub(Plan->getZero(A->getScalarType()), A,
1349 Def->getDebugLoc(), "", NW);
1350 }
1351
1352 if (CanCreateNewRecipe &&
1353 match(Def, m_c_Add(m_VPValue(X),
1354 m_VPValue(Z, m_Sub(m_ZeroInt(), m_VPValue(Y)))))) {
1355 // Preserve nsw from the Add and the Sub, if it's present on both, on the
1356 // new Sub.
1358 false, cast<VPRecipeWithIRFlags>(Def)->hasNoSignedWrap() &&
1359 cast<VPRecipeWithIRFlags>(Z)->hasNoSignedWrap()};
1360 return Builder.createSub(X, Y, Def->getDebugLoc(), "", NW);
1361 }
1362
1363 const APInt *APC;
1364 if (CanCreateNewRecipe && match(Def, m_URem(m_VPValue(X), m_APInt(APC))) &&
1365 APC->isPowerOf2())
1366 return Builder.createAnd(X, Plan->getConstantInt(*APC - 1),
1367 Def->getDebugLoc());
1368
1369 if (CanCreateNewRecipe && match(Def, m_c_Mul(m_VPValue(A), m_APInt(APC))) &&
1370 APC->isPowerOf2()) {
1371 auto *MulR = cast<VPRecipeWithIRFlags>(Def);
1372 unsigned ShiftAmt = APC->exactLogBase2();
1373 VPIRFlags::WrapFlagsTy NW(MulR->hasNoUnsignedWrap(),
1374 MulR->hasNoSignedWrap() &&
1375 ShiftAmt != APC->getBitWidth() - 1);
1376 return Builder.createNaryOp(
1377 Instruction::Shl,
1378 {A, Plan->getConstantInt(APC->getBitWidth(), ShiftAmt)}, NW,
1379 Def->getDebugLoc());
1380 }
1381
1382 if (CanCreateNewRecipe && match(Def, m_UDiv(m_VPValue(A), m_APInt(APC))) &&
1383 APC->isPowerOf2())
1384 return Builder.createNaryOp(
1385 Instruction::LShr,
1386 {A, Plan->getConstantInt(APC->getBitWidth(), APC->exactLogBase2())},
1387 *cast<VPRecipeWithIRFlags>(Def), Def->getDebugLoc());
1388
1389 if (match(Def, m_Not(m_VPValue(A)))) {
1390 if (match(A, m_Not(m_VPValue(A))))
1391 return A;
1392
1393 // Try to fold Not into compares by adjusting the predicate in-place.
1394 CmpPredicate Pred;
1395 if (match(A, m_Cmp(Pred, m_VPValue(), m_VPValue()))) {
1396 auto *Cmp = cast<VPRecipeWithIRFlags>(A);
1397 // Only fold if every user is a Not of the cmp, or a select using the cmp
1398 // solely as its condition.
1399 if (all_of(Cmp->users(), [Cmp](VPUser *U) {
1400 return match(U, m_Not(m_Specific(Cmp))) ||
1401 (match(U, m_Select(m_Specific(Cmp), m_VPValue(),
1402 m_VPValue())) &&
1403 U->getOperand(1) != Cmp && U->getOperand(2) != Cmp);
1404 })) {
1405 Cmp->setPredicate(CmpInst::getInversePredicate(Pred));
1406 for (VPUser *U : to_vector(Cmp->users())) {
1407 auto *R = cast<VPSingleDefRecipe>(U);
1408 if (match(R, m_Select(m_Specific(Cmp), m_VPValue(X), m_VPValue(Y)))) {
1409 // select (cmp pred), x, y -> select (cmp inv_pred), y, x
1410 R->setOperand(1, Y);
1411 R->setOperand(2, X);
1412 } else {
1413 // not (cmp pred) -> cmp inv_pred
1414 assert(match(R, m_Not(m_Specific(Cmp))) && "Unexpected user");
1415 R->replaceAllUsesWith(Cmp);
1416 }
1417 }
1418 // If Cmp doesn't have a debug location, use the one from the negation,
1419 // to preserve the location.
1420 if (!Cmp->getDebugLoc() && Def->getDebugLoc())
1421 Cmp->setDebugLoc(Def->getDebugLoc());
1422 return Def;
1423 }
1424 }
1425 }
1426
1427 // Fold any-of (fcmp uno %A, %A), (fcmp uno %B, %B), ... ->
1428 // any-of (fcmp uno %A, %B), ...
1429 if (match(Def, m_AnyOf())) {
1431 VPRecipeBase *UnpairedCmp = nullptr;
1432 for (VPValue *Op : Def->operands()) {
1433 VPValue *X;
1434 if (Op->getNumUsers() > 1 ||
1436 m_Deferred(X)))) {
1437 NewOps.push_back(Op);
1438 } else if (!UnpairedCmp) {
1439 UnpairedCmp = Op->getDefiningRecipe();
1440 } else {
1441 NewOps.push_back(Builder.createFCmp(CmpInst::FCMP_UNO,
1442 UnpairedCmp->getOperand(0), X));
1443 UnpairedCmp = nullptr;
1444 }
1445 }
1446
1447 if (UnpairedCmp)
1448 NewOps.push_back(UnpairedCmp->getVPSingleValue());
1449
1450 if (NewOps.size() < Def->getNumOperands()) {
1451 VPValue *NewAnyOf = Builder.createNaryOp(VPInstruction::AnyOf, NewOps);
1452 return NewAnyOf;
1453 }
1454 }
1455
1456 // Fold (fcmp uno %X, %X) or (fcmp uno %Y, %Y) -> fcmp uno %X, %Y
1457 // This is useful for fmax/fmin without fast-math flags, where we need to
1458 // check if any operand is NaN.
1459 if (CanCreateNewRecipe &&
1460 match(Def,
1461 m_BinaryOr(
1464 return Builder.createFCmp(CmpInst::FCMP_UNO, X, Y);
1465
1466 // Remove redundant DerviedIVs, that is 0 + A * 1 -> A and 0 + 0 * x -> 0.
1467 if ((match(Def, m_DerivedIV(m_ZeroInt(), m_VPValue(A), m_One())) ||
1469 m_VPValue()))) &&
1470 A->getScalarType() == Def->getScalarType())
1471 return A;
1472
1474 m_One()))) {
1475 Type *WideStepTy = Def->getScalarType();
1476 if (X->getScalarType() != WideStepTy)
1477 X = Builder.createWidenCast(Instruction::Trunc, X, WideStepTy);
1478 return X;
1479 }
1480
1481 // For i1 vp.merges produced by AnyOf reductions:
1482 // vp.merge true, (or x, y), x, evl -> vp.merge y, true, x, evl
1484 m_VPValue(X), m_VPValue())) &&
1486 Def->getScalarType()->isIntegerTy(1)) {
1487 Def->setOperand(1, Plan->getTrue());
1488 Def->setOperand(0, Y);
1489 return Def;
1490 }
1491
1492 // Simplify MaskedCond with no block mask to its single operand.
1494 !cast<VPInstruction>(Def)->isMasked())
1495 return Def->getOperand(0);
1496
1497 // Look through ExtractLastLane.
1498 if (match(Def, m_ExtractLastLane(m_VPValue(A)))) {
1499 if (match(A, m_BuildVector())) {
1500 auto *BuildVector = cast<VPInstruction>(A);
1501 return BuildVector->getOperand(BuildVector->getNumOperands() - 1);
1502 }
1503
1504 if (match(A, m_Broadcast(m_VPValue(X))))
1505 return X;
1506
1508 return A;
1509
1510 if (Plan->hasScalarVFOnly())
1511 return A;
1512 }
1513
1514 // Look through ExtractPenultimateElement (BuildVector ....).
1516 auto *BuildVector = cast<VPInstruction>(Def->getOperand(0));
1517 return BuildVector->getOperand(BuildVector->getNumOperands() - 2);
1518 }
1519
1520 uint64_t Idx;
1522 auto *BuildVector = cast<VPInstruction>(Def->getOperand(0));
1523 return BuildVector->getOperand(Idx);
1524 }
1525
1526 if (match(Def, m_BuildVector()) && all_equal(Def->operands()))
1527 return Builder.createNaryOp(VPInstruction::Broadcast, Def->getOperand(0));
1528
1529 // Replace uses of a BuildVector by users that only use its first lane with
1530 // its first operand directly.
1531 if (match(Def, m_BuildVector())) {
1532 Def->replaceUsesWithIf(Def->getOperand(0), [Def](VPUser &U, unsigned) {
1533 return U.usesFirstLaneOnly(Def);
1534 });
1535 return Def;
1536 }
1537
1538 // Look through broadcast of single-scalar when used as select conditions; in
1539 // that case the scalar condition can be used directly.
1540 if (match(Def,
1543 "broadcast operand must be single-scalar");
1544 Def->setOperand(0, Z);
1545 return Def;
1546 }
1547
1548 if (match(Def, m_Broadcast(m_VPValue(X)))) {
1549 Def->replaceUsesWithIf(
1550 X, [Def](const VPUser &U, unsigned) { return U.usesScalars(Def); });
1551 return Def;
1552 }
1553
1555 if (Def->getNumOperands() == 1) {
1556 return Def->getOperand(0);
1557 }
1558 if (auto *Phi = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(Def)) {
1559 if (all_equal(Phi->incoming_values()))
1560 return Phi->getOperand(0);
1561 }
1562 return nullptr;
1563 }
1564
1565 VPIRValue *IRV;
1566 if (Def->getNumOperands() == 1 &&
1568 return IRV;
1569
1570 // Some simplifications can only be applied after unrolling. Perform them
1571 // below.
1572 if (!Plan->isUnrolled())
1573 return nullptr;
1574
1575 // After unrolling, extract-lane may be used to extract values from multiple
1576 // scalar sources. Only simplify when extracting from a single scalar source.
1577 VPValue *LaneToExtract;
1578 if (match(Def, m_ExtractLane(m_VPValue(LaneToExtract), m_VPValue(A)))) {
1579 // Simplify extract-lane(%lane_num, %scalar_val) -> %scalar_val.
1581 return A;
1582
1583 // Replace extract-lane(0, canonical-WIDEN-INDUCTION) with the region's
1584 // scalar canonical IV.
1586 if (match(LaneToExtract, m_ZeroInt()) &&
1587 match(A, m_CanonicalWidenIV(WidenIV)))
1588 return WidenIV->getRegion()->getCanonicalIV();
1589
1590 // Simplify extract-lane with single source to extract-element.
1591 return Builder.createNaryOp(Instruction::ExtractElement, {A, LaneToExtract},
1592 Def->getDebugLoc());
1593 }
1594
1595 // Look for cycles where Def is of the form:
1596 // X = phi(0, IVInc) ; used only by IVInc, or by IVInc and Inc = X + Y
1597 // IVInc = X + Step ; used by X and Def
1598 // Def = IVInc + Y
1599 // Fold the increment Y into the phi's start value, replace Def with IVInc,
1600 // and if Inc exists, replace it with X.
1601 VPValue *IVInc;
1602 if (match(Def, m_Add(m_VPValue(IVInc, m_Add(m_VPValue(X), m_VPValue())),
1603 m_VPValue(Y))) &&
1604 isa<VPIRValue>(Y) && match(X, m_VPPhi(m_ZeroInt(), m_Specific(IVInc)))) {
1605 auto *Phi = cast<VPPhi>(X);
1606 if (IVInc->getNumUsers() == 2) {
1607 // If Phi has a second user (besides IVInc's defining recipe), it must
1608 // be Inc = Phi + Y for the fold to apply.
1610 findUserOf(Phi, m_Add(m_Specific(Phi), m_Specific(Y))));
1611 if (Phi->getNumUsers() == 1 || (Phi->getNumUsers() == 2 && Inc)) {
1612 Def->replaceAllUsesWith(IVInc);
1613 if (Inc)
1614 Inc->replaceAllUsesWith(Phi);
1615 Phi->setOperand(0, Y);
1616 return Def;
1617 }
1618 }
1619 }
1620
1621 // Simplify unrolled VectorPointer without offset, or with zero offset, to
1622 // just the pointer operand.
1623 if (auto *VPR = dyn_cast<VPVectorPointerRecipe>(Def))
1624 if (!VPR->getVFxPart() || match(VPR->getVFxPart(), m_ZeroInt()))
1625 return VPR->getOperand(0);
1626
1627 // VPScalarIVSteps after unrolling can be replaced by their start value, if
1628 // the start index is zero and only the first lane 0 is demanded.
1629 if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(Def))
1630 if (!Steps->getStartIndex() && vputils::onlyFirstLaneUsed(Steps))
1631 return Steps->getOperand(0);
1632
1633 // Simplify redundant ReductionStartVector recipes after unrolling.
1634 VPValue *StartV;
1636 m_VPValue(StartV), m_VPValue(), m_VPValue()))) {
1637 Def->replaceUsesWithIf(StartV, [](const VPUser &U, unsigned Idx) {
1638 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&U);
1639 return PhiR && PhiR->isInLoop();
1640 });
1641 return Def;
1642 }
1643
1644 if (Plan->getConcreteUF() == 1 && match(Def, m_ExtractLastPart(m_VPValue(A))))
1645 return A;
1646
1647 return nullptr;
1648}
1649
1652 Plan.getEntry());
1654 for (VPRecipeBase &R : make_early_inc_range(*VPBB))
1655 if (auto *Def = dyn_cast<VPSingleDefRecipe>(&R))
1656 if (VPValue *New = simplifyRecipe(Def)) {
1657 if (New != Def) {
1658 // Replace the recipe with a new one.
1659 Def->replaceAllUsesWith(New);
1660 Def->eraseFromParent();
1661 } else if (vputils::isDeadRecipe(R)) {
1662 // Recipe was modified - it may be dead now.
1663 Def->eraseFromParent();
1664 }
1665 }
1666 }
1667}
1668
1670 // Pull out reverses from any elementwise op.
1671 // binop(reverse(x), reverse(y)) -> reverse(binop(x,y))
1673 Plan, [](VPValue *&X) { return m_Reverse(m_VPValue(X)); },
1674 [](auto *X) { return new VPInstruction(VPInstruction::Reverse, X); });
1675
1676 // reverse(reverse(x)) -> x
1677 VPValue *X;
1680 for (VPRecipeBase &R : make_early_inc_range(*VPBB))
1681 if (match(&R, m_Reverse(m_Reverse(m_VPValue(X)))))
1682 R.getVPSingleValue()->replaceAllUsesWith(X);
1683}
1684
1685/// Reassociate (headermask && x) && y -> headermask && (x && y) to allow the
1686/// header mask to be simplified further when tail folding, e.g. in
1687/// optimizeEVLMasks.
1688static void reassociateHeaderMask(VPlan &Plan) {
1689 VPValue *HeaderMask = Plan.getVectorLoopRegion()->getHeaderMask();
1690 if (!HeaderMask)
1691 return;
1692
1693 SmallVector<VPUser *> Worklist;
1694 for (VPUser *U : HeaderMask->users())
1695 if (match(U, m_LogicalAnd(m_Specific(HeaderMask), m_VPValue())))
1697
1698 while (!Worklist.empty()) {
1699 auto *R = dyn_cast<VPSingleDefRecipe>(Worklist.pop_back_val());
1700 VPValue *X, *Y;
1701 if (!R || !match(R, m_LogicalAnd(
1702 m_LogicalAnd(m_Specific(HeaderMask), m_VPValue(X)),
1703 m_VPValue(Y))))
1704 continue;
1705 append_range(Worklist, R->users());
1706 VPBuilder Builder(R);
1707 R->replaceAllUsesWith(
1708 Builder.createLogicalAnd(HeaderMask, Builder.createLogicalAnd(X, Y)));
1709 }
1710}
1711
1712static std::optional<Instruction::BinaryOps>
1714 switch (ID) {
1715 case Intrinsic::masked_udiv:
1716 return Instruction::UDiv;
1717 case Intrinsic::masked_sdiv:
1718 return Instruction::SDiv;
1719 case Intrinsic::masked_urem:
1720 return Instruction::URem;
1721 case Intrinsic::masked_srem:
1722 return Instruction::SRem;
1723 default:
1724 return {};
1725 }
1726}
1727
1729 if (Plan.hasScalarVFOnly())
1730 return;
1731
1733 vp_depth_first_deep(Plan.getEntry()))) {
1734 for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
1737 continue;
1738 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
1739 if (RepR && (RepR->isSingleScalar() || RepR->isPredicated()))
1740 continue;
1741
1742 auto *RepOrWidenR = cast<VPRecipeWithIRFlags>(&R);
1743 if (RepR && RepR->getOpcode() == Instruction::Store &&
1744 vputils::isSingleScalar(RepR->getOperand(1))) {
1745 auto *Clone = new VPReplicateRecipe(
1746 RepOrWidenR->getUnderlyingInstr(), RepOrWidenR->operands(),
1747 true /*IsSingleScalar*/, nullptr /*Mask*/, *RepR /*Flags*/,
1748 *RepR /*Metadata*/, RepR->getDebugLoc());
1749 Clone->insertBefore(RepOrWidenR);
1750 VPBuilder Builder(Clone);
1751 VPValue *ExtractOp = Clone->getOperand(0);
1752 if (vputils::isUniformAcrossVFsAndUFs(RepR->getOperand(1)))
1753 ExtractOp =
1754 Builder.createNaryOp(VPInstruction::ExtractLastPart, ExtractOp);
1755 ExtractOp =
1756 Builder.createNaryOp(VPInstruction::ExtractLastLane, ExtractOp);
1757 Clone->setOperand(0, ExtractOp);
1758 RepR->eraseFromParent();
1759 continue;
1760 }
1761
1762 // Narrow llvm.masked.{u,s}{div,rem} intrinsics with a safe divisor.
1763 if (auto *IntrR = dyn_cast<VPWidenIntrinsicRecipe>(RepOrWidenR)) {
1764 if (!vputils::onlyFirstLaneUsed(IntrR))
1765 continue;
1766 auto Opc = getUnmaskedDivRemOpcode(IntrR->getVectorIntrinsicID());
1767 if (!Opc)
1768 continue;
1769 VPBuilder Builder(IntrR);
1770 VPValue *SafeDivisor = Builder.createSelect(
1771 IntrR->getOperand(2), IntrR->getOperand(1),
1772 Plan.getConstantInt(IntrR->getScalarType(), 1));
1773 VPValue *Clone = Builder.createNaryOp(
1774 *Opc, {IntrR->getOperand(0), SafeDivisor},
1775 VPIRFlags::getDefaultFlags(*Opc), IntrR->getDebugLoc());
1776 IntrR->replaceAllUsesWith(Clone);
1777 IntrR->eraseFromParent();
1778 continue;
1779 }
1780
1781 // Skip recipes that aren't single scalars.
1782 if (!vputils::isSingleScalar(RepOrWidenR))
1783 continue;
1784
1785 // Predicate to check if a user of Op introduces extra broadcasts.
1786 auto IntroducesBCastOf = [](const VPValue *Op) {
1787 return [Op](const VPUser *U) {
1788 if (auto *VPI = dyn_cast<VPInstruction>(U)) {
1792 VPI->getOpcode()))
1793 return false;
1794 }
1795 return !U->usesScalars(Op);
1796 };
1797 };
1798
1799 if (any_of(RepOrWidenR->users(), IntroducesBCastOf(RepOrWidenR)) &&
1800 none_of(RepOrWidenR->operands(), [&](VPValue *Op) {
1801 if (any_of(
1802 make_filter_range(Op->users(), not_equal_to(RepOrWidenR)),
1803 IntroducesBCastOf(Op)))
1804 return false;
1805 // Non-constant live-ins require broadcasts, while constants do not
1806 // need explicit broadcasts.
1807 bool LiveInNeedsBroadcast =
1808 isa<VPIRValue>(Op) && !isa<VPConstant>(Op);
1809 auto *OpR = dyn_cast<VPReplicateRecipe>(Op);
1810 return LiveInNeedsBroadcast || (OpR && OpR->isSingleScalar());
1811 }))
1812 continue;
1813
1814 auto *Clone = VPBuilder::createSingleScalarOp(
1815 vputils::getOpcode(RepOrWidenR), RepOrWidenR->operands(),
1816 /*Mask=*/nullptr, *RepOrWidenR, {}, DebugLoc::getUnknown(),
1817 RepOrWidenR->getUnderlyingInstr());
1818 Clone->insertBefore(RepOrWidenR);
1819 RepOrWidenR->replaceAllUsesWith(Clone);
1820 if (vputils::isDeadRecipe(*RepOrWidenR))
1821 RepOrWidenR->eraseFromParent();
1822 }
1823 }
1824}
1825
1826/// Try to see if all of \p Blend's masks share a common value logically and'ed
1827/// and remove it from the masks.
1829 if (Blend->isNormalized())
1830 return;
1831 VPValue *CommonEdgeMask;
1832 if (!match(Blend->getMask(0),
1833 m_LogicalAnd(m_VPValue(CommonEdgeMask), m_VPValue())))
1834 return;
1835 for (unsigned I = 0; I < Blend->getNumIncomingValues(); I++)
1836 if (!match(Blend->getMask(I),
1837 m_LogicalAnd(m_Specific(CommonEdgeMask), m_VPValue())))
1838 return;
1839 for (unsigned I = 0; I < Blend->getNumIncomingValues(); I++)
1840 Blend->setMask(I, Blend->getMask(I)->getDefiningRecipe()->getOperand(1));
1841}
1842
1843/// Normalize and simplify VPBlendRecipes. Should be run after simplifyRecipes
1844/// to make sure the masks are simplified.
1845static void simplifyBlends(VPlan &Plan) {
1848 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
1849 auto *Blend = dyn_cast<VPBlendRecipe>(&R);
1850 if (!Blend)
1851 continue;
1852
1853 removeCommonBlendMask(Blend);
1854
1855 // Try to remove redundant blend recipes.
1856 SmallPtrSet<VPValue *, 4> UniqueValues;
1857 if (Blend->isNormalized() || !match(Blend->getMask(0), m_False()))
1858 UniqueValues.insert(Blend->getIncomingValue(0));
1859 for (unsigned I = 1; I != Blend->getNumIncomingValues(); ++I)
1860 if (!match(Blend->getMask(I), m_False()))
1861 UniqueValues.insert(Blend->getIncomingValue(I));
1862
1863 if (UniqueValues.size() == 1) {
1864 Blend->replaceAllUsesWith(*UniqueValues.begin());
1865 Blend->eraseFromParent();
1866 continue;
1867 }
1868
1869 if (Blend->isNormalized())
1870 continue;
1871
1872 // Normalize the blend so its first incoming value is used as the initial
1873 // value with the others blended into it.
1874
1875 unsigned StartIndex = 0;
1876 for (unsigned I = 0; I != Blend->getNumIncomingValues(); ++I) {
1877 // If a value's mask is used only by the blend then is can be deadcoded.
1878 // TODO: Find the most expensive mask that can be deadcoded, or a mask
1879 // that's used by multiple blends where it can be removed from them all.
1880 VPValue *Mask = Blend->getMask(I);
1881 if (Mask->hasOneUse() && !match(Mask, m_False())) {
1882 StartIndex = I;
1883 break;
1884 }
1885 }
1886
1887 SmallVector<VPValue *, 4> OperandsWithMask;
1888 OperandsWithMask.push_back(Blend->getIncomingValue(StartIndex));
1889
1890 for (unsigned I = 0; I != Blend->getNumIncomingValues(); ++I) {
1891 if (I == StartIndex)
1892 continue;
1893 OperandsWithMask.push_back(Blend->getIncomingValue(I));
1894 OperandsWithMask.push_back(Blend->getMask(I));
1895 }
1896
1897 auto *NewBlend =
1898 new VPBlendRecipe(cast_or_null<PHINode>(Blend->getUnderlyingValue()),
1899 OperandsWithMask, *Blend, Blend->getDebugLoc());
1900 NewBlend->insertBefore(&R);
1901
1902 VPValue *DeadMask = Blend->getMask(StartIndex);
1903 Blend->replaceAllUsesWith(NewBlend);
1904 Blend->eraseFromParent();
1906
1907 /// Simplify BLEND %a, %b, Not(%mask) -> BLEND %b, %a, %mask.
1908 VPValue *NewMask;
1909 if (NewBlend->getNumOperands() == 3 &&
1910 match(NewBlend->getMask(1), m_Not(m_VPValue(NewMask)))) {
1911 VPValue *Inc0 = NewBlend->getOperand(0);
1912 VPValue *Inc1 = NewBlend->getOperand(1);
1913 VPValue *OldMask = NewBlend->getOperand(2);
1914 NewBlend->setOperand(0, Inc1);
1915 NewBlend->setOperand(1, Inc0);
1916 NewBlend->setOperand(2, NewMask);
1917 if (OldMask->user_empty())
1918 cast<VPInstruction>(OldMask)->eraseFromParent();
1919 }
1920 }
1921 }
1922}
1923
1924/// Optimize the width of vector induction variables in \p Plan based on a known
1925/// constant Trip Count, \p BestVF and \p BestUF.
1927 ElementCount BestVF,
1928 unsigned BestUF) {
1929 // Only proceed if we have not completely removed the vector region.
1930 if (!Plan.getVectorLoopRegion())
1931 return false;
1932
1933 const APInt *TC;
1934 if (!BestVF.isFixed() || !match(Plan.getTripCount(), m_APInt(TC)))
1935 return false;
1936
1937 // Calculate the minimum power-of-2 bit width that can fit the known TC, VF
1938 // and UF. Returns at least 8.
1939 auto ComputeBitWidth = [](APInt TC, uint64_t Align) {
1940 APInt AlignedTC =
1943 APInt MaxVal = AlignedTC - 1;
1944 return std::max<unsigned>(PowerOf2Ceil(MaxVal.getActiveBits()), 8);
1945 };
1946 unsigned NewBitWidth =
1947 ComputeBitWidth(*TC, BestVF.getKnownMinValue() * BestUF);
1948
1949 LLVMContext &Ctx = Plan.getContext();
1950 auto *NewIVTy = IntegerType::get(Ctx, NewBitWidth);
1951
1952 bool MadeChange = false;
1953
1954 VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
1955 for (VPRecipeBase &Phi : HeaderVPBB->phis()) {
1956 // Currently only handle canonical IVs as it is trivial to replace the start
1957 // and stop values, and we currently only perform the optimization when the
1958 // IV has a single use.
1960 if (!match(&Phi, m_CanonicalWidenIV(WideIV)))
1961 continue;
1962 if (WideIV->hasMoreThanOneUniqueUser() ||
1963 NewIVTy == WideIV->getScalarType())
1964 continue;
1965
1966 // Currently only handle cases where the single user is a header-mask
1967 // comparison with the backedge-taken-count.
1968 VPUser *SingleUser = WideIV->getSingleUser();
1969 if (!SingleUser ||
1970 !match(SingleUser,
1971 m_ICmp(m_Specific(WideIV),
1973 continue;
1974
1975 // Update IV operands and comparison bound to use new narrower type.
1976 assert(!WideIV->getTruncInst() &&
1977 "canonical IV is not expected to have a truncation");
1978 auto *NewWideIV = new VPWidenIntOrFpInductionRecipe(
1979 WideIV->getPHINode(), Plan.getZero(NewIVTy),
1980 Plan.getConstantInt(NewIVTy, 1), WideIV->getVFValue(),
1981 WideIV->getInductionDescriptor(), *WideIV, WideIV->getDebugLoc());
1982 NewWideIV->insertBefore(WideIV);
1983
1984 auto *NewBTC = new VPWidenCastRecipe(
1985 Instruction::Trunc, Plan.getOrCreateBackedgeTakenCount(), NewIVTy,
1986 nullptr, VPIRFlags::getDefaultFlags(Instruction::Trunc));
1987 Plan.getVectorPreheader()->appendRecipe(NewBTC);
1988 auto *Cmp = cast<VPInstruction>(WideIV->getSingleUser());
1989 Cmp->replaceAllUsesWith(
1990 VPBuilder(Cmp).createICmp(Cmp->getPredicate(), NewWideIV, NewBTC));
1991
1992 MadeChange = true;
1993 }
1994
1995 return MadeChange;
1996}
1997
1998/// Return true if \p Cond is known to be true for given \p BestVF and \p
1999/// BestUF.
2001 ElementCount BestVF, unsigned BestUF,
2004 return any_of(Cond->getDefiningRecipe()->operands(), [&Plan, BestVF, BestUF,
2005 &PSE](VPValue *C) {
2006 return isConditionTrueViaVFAndUF(C, Plan, BestVF, BestUF, PSE);
2007 });
2008
2009 auto *CanIV = Plan.getVectorLoopRegion()->getCanonicalIV();
2012 m_c_Add(m_Specific(CanIV), m_Specific(&Plan.getVFxUF())),
2013 m_Specific(&Plan.getVectorTripCount()))))
2014 return false;
2015
2016 // The compare checks CanIV + VFxUF == vector trip count. The vector trip
2017 // count is not conveniently available as SCEV so far, so we compare directly
2018 // against the original trip count. This is stricter than necessary, as we
2019 // will only return true if the trip count == vector trip count.
2020 const SCEV *VectorTripCount =
2022 if (isa<SCEVCouldNotCompute>(VectorTripCount))
2023 VectorTripCount = vputils::getSCEVExprForVPValue(Plan.getTripCount(), PSE);
2024 assert(!isa<SCEVCouldNotCompute>(VectorTripCount) &&
2025 "Trip count SCEV must be computable");
2026 ScalarEvolution &SE = *PSE.getSE();
2027 ElementCount NumElements = BestVF.multiplyCoefficientBy(BestUF);
2028 const SCEV *C = SE.getElementCount(VectorTripCount->getType(), NumElements);
2029 return SE.isKnownPredicate(CmpInst::ICMP_EQ, VectorTripCount, C);
2030}
2031
2032// Replaces ExtractVectorForPart instructions with ICMP when the VF is scalar
2033// and the source is a WideActiveLaneMask. The unused mask is removed later
2034// when removing dead recipes.
2036 ElementCount BestVF) {
2037 if (!BestVF.isScalar())
2038 return false;
2039
2040 bool MadeChange = false;
2041 VPBuilder Builder;
2042 VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
2043 VPBasicBlock *PreheaderVPBB = Plan.getVectorPreheader();
2044 VPBasicBlock *ExitingVPBB = VectorRegion->getExitingBasicBlock();
2045
2046 VPValue *Start, *TC;
2047 uint64_t Idx;
2048 for (VPBasicBlock *VPBB : {PreheaderVPBB, ExitingVPBB}) {
2049 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
2052 m_VPValue()),
2053 m_ConstantInt(Idx))))
2054 continue;
2055
2056 auto *Extract = cast<VPInstruction>(&R);
2057 Builder.setInsertPoint(Extract);
2058
2059 if (Idx > 0)
2060 Start = Builder.createAdd(
2061 Start, Plan.getConstantInt(Start->getScalarType(), Idx));
2062
2063 VPValue *ICmp = Builder.createICmp(CmpInst::ICMP_ULT, Start, TC);
2064 Extract->replaceAllUsesWith(ICmp);
2065 Extract->eraseFromParent();
2066 MadeChange = true;
2067 }
2068 }
2069
2070 return MadeChange;
2071}
2072
2073/// Try to simplify the branch condition of \p Plan. This may restrict the
2074/// resulting plan to \p BestVF and \p BestUF.
2076 unsigned BestUF,
2078 VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
2079 VPBasicBlock *ExitingVPBB = VectorRegion->getExitingBasicBlock();
2080 auto *Term = &ExitingVPBB->back();
2081 VPValue *Cond;
2082 auto m_CanIVInc = m_Add(m_VPValue(), m_Specific(&Plan.getVFxUF()));
2083 // Check if the branch condition compares the canonical IV increment (for main
2084 // loop), or the canonical IV increment plus an offset (for epilog loop).
2085 if (match(Term, m_BranchOnCount(
2086 m_CombineOr(m_CanIVInc, m_c_Add(m_CanIVInc, m_LiveIn())),
2087 m_VPValue())) ||
2088 match(Term,
2091 m_ZeroInt()))))) {
2092 // Try to simplify the branch condition if VectorTC <= VF * UF when the
2093 // latch terminator is BranchOnCount or
2094 // BranchOnCond(Not(ExtractVectorForPart(WideActiveLaneMask), 0))
2095 const SCEV *VectorTripCount =
2097 if (isa<SCEVCouldNotCompute>(VectorTripCount))
2098 VectorTripCount =
2100 assert(!isa<SCEVCouldNotCompute>(VectorTripCount) &&
2101 "Trip count SCEV must be computable");
2102 ScalarEvolution &SE = *PSE.getSE();
2103 ElementCount NumElements = BestVF.multiplyCoefficientBy(BestUF);
2104 const SCEV *C = SE.getElementCount(VectorTripCount->getType(), NumElements);
2105 if (!SE.isKnownPredicate(CmpInst::ICMP_ULE, VectorTripCount, C))
2106 return false;
2107 } else if (match(Term, m_BranchOnCond(m_VPValue(Cond))) ||
2109 // For BranchOnCond, check if we can prove the condition to be true using VF
2110 // and UF.
2111 if (!isConditionTrueViaVFAndUF(Cond, Plan, BestVF, BestUF, PSE))
2112 return false;
2113 } else {
2114 return false;
2115 }
2116
2117 // The vector loop region only executes once. Convert terminator of the
2118 // exiting block to exit in the first iteration.
2119 if (match(Term, m_BranchOnTwoConds())) {
2120 Term->setOperand(1, Plan.getTrue());
2121 return true;
2122 }
2123
2124 auto *BOC = new VPInstruction(VPInstruction::BranchOnCond, Plan.getTrue(), {},
2125 {}, Term->getDebugLoc());
2126 ExitingVPBB->appendRecipe(BOC);
2127 Term->eraseFromParent();
2128
2129 return true;
2130}
2131
2133 unsigned BestUF,
2135 assert(Plan.hasVF(BestVF) && "BestVF is not available in Plan");
2136 assert(Plan.hasUF(BestUF) && "BestUF is not available in Plan");
2137
2138 bool MadeChange =
2139 simplifyBranchConditionForVFAndUF(Plan, BestVF, BestUF, PSE);
2140 MadeChange |= replaceMaskWithCompareForScalarPlan(Plan, BestVF);
2141 MadeChange |= optimizeVectorInductionWidthForTCAndVFUF(Plan, BestVF, BestUF);
2142
2143 if (MadeChange) {
2144 Plan.setVF(BestVF);
2145 assert(Plan.getConcreteUF() == BestUF && "BestUF must match the Plan's UF");
2146 }
2147}
2148
2150 for (VPRecipeBase &R :
2152 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
2153 if (!PhiR)
2154 continue;
2155 RecurKind RK = PhiR->getRecurrenceKind();
2156 if (RK != RecurKind::Add && RK != RecurKind::Mul && RK != RecurKind::Sub &&
2158 continue;
2159
2161 if (auto *RecWithFlags = dyn_cast<VPRecipeWithIRFlags>(U)) {
2162 RecWithFlags->dropPoisonGeneratingFlags();
2163 }
2164 }
2165}
2166
2167namespace {
2168struct VPCSEDenseMapInfo : public DenseMapInfo<VPSingleDefRecipe *> {
2169 /// If recipe \p R will lower to a GEP with a non-i8 source element type,
2170 /// return that source element type.
2171 static Type *getGEPSourceElementType(const VPSingleDefRecipe *R) {
2172 // All VPInstructions that lower to GEPs must have the i8 source element
2173 // type (as they are PtrAdds), so we omit it.
2175 .Case([](const VPReplicateRecipe *I) -> Type * {
2176 if (auto *GEP = dyn_cast<GetElementPtrInst>(I->getUnderlyingValue()))
2177 return GEP->getSourceElementType();
2178 return nullptr;
2179 })
2180 .Case<VPVectorPointerRecipe, VPWidenGEPRecipe>(
2181 [](auto *I) { return I->getSourceElementType(); })
2182 .Default([](auto *) { return nullptr; });
2183 }
2184
2185 /// Returns true if recipe \p Def can be safely handed for CSE.
2186 static bool canHandle(const VPSingleDefRecipe *Def) {
2187 // We can extend the list of handled recipes in the future,
2188 // provided we account for the data embedded in them while checking for
2189 // equality or hashing.
2191
2192 // The issue with (Insert|Extract)Value is that the index of the
2193 // insert/extract is not a proper operand in LLVM IR, and hence also not in
2194 // VPlan.
2195 if (!C || (!C->first && (C->second == Instruction::InsertValue ||
2196 C->second == Instruction::ExtractValue)))
2197 return false;
2198
2199 // During CSE, we can only handle non-memory recipes, as memory can alias.
2200 return !Def->mayReadOrWriteMemory();
2201 }
2202
2203 /// Hash the underlying data of \p Def.
2204 static unsigned getHashValue(const VPSingleDefRecipe *Def) {
2205 hash_code Result = hash_combine(
2206 Def->getVPRecipeID(), vputils::getOpcodeOrIntrinsicID(Def),
2207 getGEPSourceElementType(Def), Def->getScalarType(),
2209 if (auto *RFlags = dyn_cast<VPRecipeWithIRFlags>(Def))
2210 if (RFlags->hasPredicate())
2211 return hash_combine(Result, RFlags->getPredicate());
2212 if (auto *SIVSteps = dyn_cast<VPScalarIVStepsRecipe>(Def))
2213 return hash_combine(Result, SIVSteps->getInductionOpcode());
2214 return Result;
2215 }
2216
2217 /// Check equality of underlying data of \p L and \p R.
2218 static bool isEqual(const VPSingleDefRecipe *L, const VPSingleDefRecipe *R) {
2219 if (L->getVPRecipeID() != R->getVPRecipeID() ||
2222 getGEPSourceElementType(L) != getGEPSourceElementType(R) ||
2224 !equal(L->operands(), R->operands()))
2225 return false;
2228 "must have valid opcode info for both recipes");
2229 if (auto *LFlags = dyn_cast<VPRecipeWithIRFlags>(L))
2230 if (LFlags->hasPredicate() &&
2231 LFlags->getPredicate() !=
2232 cast<VPRecipeWithIRFlags>(R)->getPredicate())
2233 return false;
2234 if (auto *LSIV = dyn_cast<VPScalarIVStepsRecipe>(L))
2235 if (LSIV->getInductionOpcode() !=
2236 cast<VPScalarIVStepsRecipe>(R)->getInductionOpcode())
2237 return false;
2238 // Phi recipes can only be equal if they are in the same VPBB, as they
2239 // implicitly depend on their predecessors.
2240 if (isa<VPWidenPHIRecipe>(L) && L->getParent() != R->getParent())
2241 return false;
2242 // Recipes in replicate regions implicitly depend on predicate. If either
2243 // recipe is in a replicate region, only consider them equal if both have
2244 // the same parent.
2245 const VPRegionBlock *RegionL = L->getRegion();
2246 const VPRegionBlock *RegionR = R->getRegion();
2247 if (((RegionL && RegionL->isReplicator()) ||
2248 (RegionR && RegionR->isReplicator())) &&
2249 L->getParent() != R->getParent())
2250 return false;
2251 return L->getScalarType() == R->getScalarType();
2252 }
2253};
2254} // end anonymous namespace
2255
2256/// Perform a common-subexpression-elimination of VPSingleDefRecipes on the \p
2257/// Plan.
2259 VPDominatorTree VPDT(Plan);
2261
2263 Plan.getEntry());
2265 for (VPRecipeBase &R : *VPBB) {
2266 auto *Def = dyn_cast<VPSingleDefRecipe>(&R);
2267 if (!Def || !VPCSEDenseMapInfo::canHandle(Def))
2268 continue;
2269 if (VPSingleDefRecipe *V = CSEMap.lookup(Def)) {
2270 // V must dominate Def for a valid replacement.
2271 if (!VPDT.dominates(V->getParent(), VPBB))
2272 continue;
2273 // Only keep flags present on both V and Def.
2274 if (auto *RFlags = dyn_cast<VPRecipeWithIRFlags>(V))
2275 RFlags->intersectFlags(*cast<VPRecipeWithIRFlags>(Def));
2276 Def->replaceAllUsesWith(V);
2277 continue;
2278 }
2279 CSEMap[Def] = Def;
2280 }
2281 }
2282}
2283
2284/// Return true if we do not know how to (mechanically) hoist or sink a
2285/// non-memory or memory recipe \p R out of a loop region. When sinking, passing
2286/// \p Sinking = true ensures that assumes aren't sunk.
2288 VPBasicBlock *LastBB,
2289 bool Sinking = false) {
2290 if (!isa<VPReplicateRecipe>(R) || !R.mayReadOrWriteMemory() ||
2292 return vputils::cannotHoistOrSinkRecipe(R, Sinking);
2293
2294 // Check that the memory operation doesn't alias between FirstBB and LastBB.
2295 auto MemLoc = vputils::getMemoryLocation(R);
2296
2297 // TODO: Could make use of SinkStoreInfo::isNoAliasViaDistance by collecting
2298 // stores upfront, and constructing a full SinkStoreInfo.
2299 auto SinkInfo =
2300 Sinking ? std::make_optional(SinkStoreInfo(cast<VPReplicateRecipe>(R)))
2301 : std::nullopt;
2302
2303 return !MemLoc ||
2304 !canHoistOrSinkWithNoAliasCheck(*MemLoc, FirstBB, LastBB, SinkInfo);
2305}
2306
2307/// Move loop-invariant recipes out of the vector loop region in \p Plan.
2308static void licm(VPlan &Plan) {
2309 VPBasicBlock *Preheader = Plan.getVectorPreheader();
2310
2311 // Hoist any loop invariant recipes from the vector loop region to the
2312 // preheader. Preform a shallow traversal of the vector loop region, to
2313 // exclude recipes in replicate regions. Since the top-level blocks in the
2314 // vector loop region are guaranteed to execute if the vector pre-header is,
2315 // we don't need to check speculation safety.
2316 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
2317 assert(Preheader->getSingleSuccessor() == LoopRegion &&
2318 "Expected vector prehader's successor to be the vector loop region");
2320 vp_depth_first_shallow(LoopRegion->getEntry()))) {
2321 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
2322 if (cannotHoistOrSinkRecipe(R, LoopRegion->getEntryBasicBlock(),
2323 LoopRegion->getExitingBasicBlock()))
2324 continue;
2325 if (any_of(R.operands(), [](VPValue *Op) {
2326 return !Op->isDefinedOutsideLoopRegions();
2327 }))
2328 continue;
2329 R.moveBefore(*Preheader, Preheader->end());
2330 }
2331 }
2332
2333#ifndef NDEBUG
2334 VPDominatorTree VPDT(Plan);
2335#endif
2336 // Sink recipes with no users inside the vector loop region if all users are
2337 // in the same exit block of the region.
2338 // TODO: Extend to sink recipes from inner loops.
2340 LoopRegion->getEntry());
2342 for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
2343 if (cannotHoistOrSinkRecipe(R, LoopRegion->getEntryBasicBlock(),
2344 LoopRegion->getExitingBasicBlock(),
2345 /*Sinking=*/true))
2346 continue;
2347
2348 if (auto *RepR = dyn_cast<VPReplicateRecipe>(&R)) {
2349 assert(!RepR->isPredicated() &&
2350 "Expected prior transformation of predicated replicates to "
2351 "replicate regions");
2352 // narrowToSingleScalarRecipes should have already maximally narrowed
2353 // replicates to single-scalar replicates.
2354 // TODO: When unrolling, replicateByVF doesn't handle sunk
2355 // non-single-scalar replicates correctly.
2356 if (!RepR->isSingleScalar())
2357 continue;
2358
2359 // The pointer operand of stores must be loop-invariant.
2360 if (RepR->getOpcode() == Instruction::Store &&
2361 !RepR->getOperand(1)->isDefinedOutsideLoopRegions())
2362 continue;
2363 }
2364
2365 [[maybe_unused]] auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
2366 assert((!R.mayWriteToMemory() ||
2367 (RepR && RepR->getOpcode() == Instruction::Store &&
2368 RepR->getOperand(1)->isDefinedOutsideLoopRegions())) &&
2369 "The only recipes that may write to memory are expected to be "
2370 "stores with invariant pointer-operand");
2371
2372 // TODO: Use R.definedValues() instead of casting to VPSingleDefRecipe to
2373 // support recipes with multiple defined values (e.g., interleaved loads).
2374 auto *Def = cast<VPSingleDefRecipe>(&R);
2375
2376 // Cannot sink the recipe if the user is defined in a loop region or a
2377 // non-successor of the vector loop region. Cannot sink if user is a phi
2378 // either.
2379 VPBasicBlock *SinkBB = nullptr;
2380 if (any_of(Def->users(), [&SinkBB, &LoopRegion](VPUser *U) {
2381 auto *UserR = cast<VPRecipeBase>(U);
2382 VPBasicBlock *Parent = UserR->getParent();
2383 // TODO: Support sinking when users are in multiple blocks.
2384 if (SinkBB && SinkBB != Parent)
2385 return true;
2386 SinkBB = Parent;
2387 // TODO: If the user is a PHI node, we should check the block of
2388 // incoming value. Support PHI node users if needed.
2389 return UserR->isPhi() || Parent->getEnclosingLoopRegion() ||
2390 Parent->getSinglePredecessor() != LoopRegion;
2391 }))
2392 continue;
2393
2394 if (!SinkBB)
2395 SinkBB = cast<VPBasicBlock>(LoopRegion->getSingleSuccessor());
2396
2397 // TODO: This will need to be a check instead of a assert after
2398 // conditional branches in vectorized loops are supported.
2399 assert(VPDT.properlyDominates(VPBB, SinkBB) &&
2400 "Defining block must dominate sink block");
2401 // TODO: Clone the recipe if users are on multiple exit paths, instead of
2402 // just moving.
2403 Def->moveBefore(*SinkBB, SinkBB->getFirstNonPhi());
2404 }
2405 }
2406}
2407
2409 VPlan &Plan, const MapVector<Instruction *, uint64_t> &MinBWs) {
2410 if (Plan.hasScalarVFOnly())
2411 return;
2412 // Keep track of created truncates, so they can be re-used. Note that we
2413 // cannot use RAUW after creating a new truncate, as this would could make
2414 // other uses have different types for their operands, making them invalidly
2415 // typed.
2417 VPBasicBlock *PH = Plan.getVectorPreheader();
2420 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
2423 continue;
2424
2425 VPValue *ResultVPV = R.getVPSingleValue();
2426 auto *UI = cast_or_null<Instruction>(ResultVPV->getUnderlyingValue());
2427 unsigned NewResSizeInBits = MinBWs.lookup(UI);
2428 if (!NewResSizeInBits)
2429 continue;
2430
2431 // If the value wasn't vectorized, we must maintain the original scalar
2432 // type. Skip those here, after incrementing NumProcessedRecipes. Also
2433 // skip casts which do not need to be handled explicitly here, as
2434 // redundant casts will be removed during recipe simplification.
2436 continue;
2437
2438 Type *OldResTy = ResultVPV->getScalarType();
2439 unsigned OldResSizeInBits = OldResTy->getScalarSizeInBits();
2440 assert(OldResTy->isIntegerTy() && "only integer types supported");
2441 (void)OldResSizeInBits;
2442
2443 auto *NewResTy = IntegerType::get(Plan.getContext(), NewResSizeInBits);
2444
2445 // Any wrapping introduced by shrinking this operation shouldn't be
2446 // considered undefined behavior. So, we can't unconditionally copy
2447 // arithmetic wrapping flags to VPW.
2448 if (auto *VPW = dyn_cast<VPRecipeWithIRFlags>(&R))
2449 VPW->dropPoisonGeneratingFlags();
2450
2451 assert((OldResSizeInBits != NewResSizeInBits ||
2452 match(&R, m_ICmp(m_VPValue(), m_VPValue()))) &&
2453 "Only ICmps should not need extending the result.");
2454 assert(!isa<VPWidenStoreRecipe>(&R) && "stores cannot be narrowed");
2455
2456 // For loads/intrinsics we don't recreate the recipe; just wrap the
2457 // original wide result in a ZExt to OldResTy.
2459 if (OldResSizeInBits != NewResSizeInBits) {
2461 Instruction::ZExt, ResultVPV, OldResTy);
2462 ResultVPV->replaceAllUsesWith(Ext);
2463 Ext->setOperand(0, ResultVPV);
2464 }
2465 continue;
2466 }
2467
2468 // Shrink operands by introducing truncates as needed.
2469 unsigned StartIdx =
2470 match(&R, m_Select(m_VPValue(), m_VPValue(), m_VPValue())) ? 1 : 0;
2471 SmallVector<VPValue *> NewOperands(R.operands());
2472 for (VPValue *&Op : drop_begin(NewOperands, StartIdx)) {
2473 unsigned OpSizeInBits = Op->getScalarType()->getScalarSizeInBits();
2474 if (OpSizeInBits == NewResSizeInBits)
2475 continue;
2476 assert(OpSizeInBits > NewResSizeInBits && "nothing to truncate");
2477 auto [ProcessedIter, Inserted] = ProcessedTruncs.try_emplace(Op);
2478 if (Inserted) {
2479 VPBuilder Builder;
2480 if (isa<VPIRValue>(Op))
2481 Builder.setInsertPoint(PH);
2482 else
2483 Builder.setInsertPoint(&R);
2484 ProcessedIter->second =
2485 Builder.createWidenCast(Instruction::Trunc, Op, NewResTy);
2486 }
2487 Op = ProcessedIter->second;
2488 }
2489
2490 auto *NWR = cast<VPWidenRecipe>(&R)->cloneWithOperands(NewOperands);
2491 NWR->insertBefore(&R);
2492
2493 // Wrap NWR in a ZExt to preserve the original wide type for downstream
2494 // users (unless this is an ICmp, which produces i1 regardless).
2495 VPValue *Replacement = NWR->getVPSingleValue();
2496 if (OldResSizeInBits != NewResSizeInBits)
2497 Replacement =
2499 .createWidenCast(Instruction::ZExt, Replacement, OldResTy)
2500 ->getVPSingleValue();
2501 ResultVPV->replaceAllUsesWith(Replacement);
2502 R.eraseFromParent();
2503 }
2504 }
2505}
2506
2507bool VPlanTransforms::removeBranchOnConst(VPlan &Plan, bool OnlyLatches) {
2508 std::optional<VPDominatorTree> VPDT;
2509 if (OnlyLatches)
2510 VPDT.emplace(Plan);
2511
2512 // Collect all blocks before modifying the CFG so we can identify unreachable
2513 // ones after constant branch removal.
2515
2516 bool SimplifiedPhi = false;
2517 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(AllBlocks)) {
2518 VPValue *Cond;
2519 // Skip blocks that are not terminated by BranchOnCond.
2520 if (VPBB->empty() || !match(&VPBB->back(), m_BranchOnCond(m_VPValue(Cond))))
2521 continue;
2522
2523 if (OnlyLatches && !VPBlockUtils::isLatch(VPBB, *VPDT))
2524 continue;
2525
2526 assert(VPBB->getNumSuccessors() == 2 &&
2527 "Two successors expected for BranchOnCond");
2528 unsigned RemovedIdx;
2529 if (match(Cond, m_True()))
2530 RemovedIdx = 1;
2531 else if (match(Cond, m_False()))
2532 RemovedIdx = 0;
2533 else
2534 continue;
2535
2536 VPBasicBlock *RemovedSucc =
2537 cast<VPBasicBlock>(VPBB->getSuccessors()[RemovedIdx]);
2538 assert(count(RemovedSucc->getPredecessors(), VPBB) == 1 &&
2539 "There must be a single edge between VPBB and its successor");
2540 // Values coming from VPBB into phi recipes of RemovedSucc are removed from
2541 // these recipes and single-entry header phis are removed.
2542 for (VPRecipeBase &R : make_early_inc_range(RemovedSucc->phis())) {
2543 cast<VPPhiAccessors>(&R)->removeIncomingValueFor(VPBB);
2544 SimplifiedPhi = true;
2545 // Remove now invalid header phis that are left single-entry after
2546 // removing their backedges.
2547 auto *PhiR = dyn_cast<VPHeaderPHIRecipe>(&R);
2548 if (!PhiR || PhiR->getNumIncoming() != 1)
2549 continue;
2550 PhiR->replaceAllUsesWith(PhiR->getOperand(0));
2551 PhiR->eraseFromParent();
2552 }
2553
2554 // Disconnect blocks and remove the terminator.
2555 VPBlockUtils::disconnectBlocks(VPBB, RemovedSucc);
2556 VPBB->back().eraseFromParent();
2557 }
2558
2559 // Compute which blocks are still reachable from the entry after constant
2560 // branch removal.
2563
2564 // Detach all unreachable blocks from their successors, removing their recipes
2565 // and incoming values from phi recipes.
2566 VPSymbolicValue Tmp(nullptr);
2567 for (VPBlockBase *B : AllBlocks) {
2568 if (Reachable.contains(B))
2569 continue;
2570 for (VPBlockBase *Succ : to_vector(B->successors())) {
2571 if (auto *SuccBB = dyn_cast<VPBasicBlock>(Succ))
2572 for (VPRecipeBase &R : SuccBB->phis())
2573 cast<VPPhiAccessors>(&R)->removeIncomingValueFor(B);
2575 }
2576 for (VPBasicBlock *DeadBB :
2578 for (VPRecipeBase &R : make_early_inc_range(*DeadBB)) {
2579 for (VPValue *Def : R.definedValues())
2580 Def->replaceAllUsesWith(&Tmp);
2581 R.eraseFromParent();
2582 }
2583 }
2584 }
2585 return SimplifiedPhi;
2586}
2587
2608
2611 auto GetSimplifiedLiveInViaSCEV = [&](VPValue *VPV) -> VPValue * {
2612 const SCEV *Expr = vputils::getSCEVExprForVPValue(VPV, PSE);
2613 const APInt *C;
2614 if (match(Expr, m_scev_APInt(C)))
2615 return Plan.getConstantInt(*C);
2616 return nullptr;
2617 };
2618
2619 for (VPValue *LiveIn : to_vector(Plan.getLiveIns())) {
2620 if (VPValue *SimplifiedLiveIn = GetSimplifiedLiveInViaSCEV(LiveIn))
2621 LiveIn->replaceAllUsesWith(SimplifiedLiveIn);
2622 }
2623}
2624
2626 VPlan &Plan, PredicatedScalarEvolution &PSE,
2627 const SymbolicStrideMap &StridesMap, const VPDominatorTree &VPDT) {
2628 // Replace VPValues for known constant strides guaranteed by predicated scalar
2629 // evolution that are guaranteed to be guarded by the runtime checks; that is,
2630 // blocks dominated by the vector header.
2631 assert(!Plan.getVectorLoopRegion() &&
2632 "expected to run before loop regions are created");
2633 const auto &[Header, _] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
2634 auto CanUseVersionedStride = [&VPDT, Header = Header, &Plan](VPUser &U,
2635 unsigned Idx) {
2636 auto *R = cast<VPRecipeBase>(&U);
2637 // Skip phis if the loop if loop is not yet guarded.
2638 if (isa<VPPhiAccessors>(R) &&
2639 Header == Plan.getEntry()->getSingleSuccessor())
2640 return false;
2641 return VPDT.dominates(Header, R->getParent());
2642 };
2643 ValueToSCEVMapTy RewriteMap;
2644 for (const SCEVUnknown *Stride : StridesMap.values()) {
2645 Value *StrideV = Stride->getValue();
2646 const APInt *StrideConst;
2647 const SCEV *StrideExpr = PSE.getSCEV(StrideV);
2648 if (!match(StrideExpr, m_scev_APInt(StrideConst)))
2649 // Only handle constant strides for now.
2650 continue;
2651 if (VPValue *StrideVPV = Plan.getLiveIn(StrideV))
2652 StrideVPV->replaceUsesWithIf(Plan.getConstantInt(*StrideConst),
2653 CanUseVersionedStride);
2654
2655 // The versioned value may not be used in the loop directly but through an
2656 // integral cast (sext/zext/trunc). Add new live-ins in those cases.
2657 for (Value *U : StrideV->users()) {
2659 continue;
2660 VPValue *StrideVPV = Plan.getLiveIn(U);
2661 if (!StrideVPV)
2662 continue;
2663 unsigned BW = U->getType()->getScalarSizeInBits();
2664 APInt C = isa<SExtInst>(U) ? StrideConst->sext(BW)
2665 : StrideConst->zextOrTrunc(BW);
2666 StrideVPV->replaceUsesWithIf(Plan.getConstantInt(C),
2667 CanUseVersionedStride);
2668 }
2669 RewriteMap[StrideV] = StrideExpr;
2670 }
2671
2672 for (VPRecipeBase &R : *Plan.getEntry()) {
2673 auto *ExpSCEV = dyn_cast<VPExpandSCEVRecipe>(&R);
2674 if (!ExpSCEV)
2675 continue;
2676 const SCEV *ScevExpr = ExpSCEV->getSCEV();
2677 auto *NewSCEV =
2678 SCEVParameterRewriter::rewrite(ScevExpr, *PSE.getSE(), RewriteMap);
2679 if (NewSCEV != ScevExpr) {
2680 VPValue *NewExp = vputils::getOrCreateVPValueForSCEVExpr(Plan, NewSCEV);
2681 ExpSCEV->replaceAllUsesWith(NewExp);
2682 if (Plan.getTripCount() == ExpSCEV)
2683 Plan.resetTripCount(NewExp);
2684 }
2685 }
2686}
2687
2689 // Collect recipes in the backward slice of `Root` that may generate a poison
2690 // value that is used after vectorization.
2692 auto CollectPoisonGeneratingInstrsInBackwardSlice([&](VPRecipeBase *Root) {
2694 Worklist.push_back(Root);
2695
2696 // Traverse the backward slice of Root through its use-def chain.
2697 while (!Worklist.empty()) {
2698 VPRecipeBase *CurRec = Worklist.pop_back_val();
2699
2700 if (!Visited.insert(CurRec).second)
2701 continue;
2702
2703 // Prune search if we find another recipe generating a widen memory
2704 // instruction. Widen memory instructions involved in address computation
2705 // will lead to gather/scatter instructions, which don't need to be
2706 // handled.
2708 VPHeaderPHIRecipe>(CurRec))
2709 continue;
2710
2711 // This recipe contributes to the address computation of a widen
2712 // load/store. If the underlying instruction has poison-generating flags,
2713 // drop them directly.
2714 if (auto *RecWithFlags = dyn_cast<VPRecipeWithIRFlags>(CurRec)) {
2715 VPValue *A, *B;
2716 // Dropping disjoint from an OR may yield incorrect results, as some
2717 // analysis may have converted it to an Add implicitly (e.g. SCEV used
2718 // for dependence analysis). Instead, replace it with an equivalent Add.
2719 // This is possible as all users of the disjoint OR only access lanes
2720 // where the operands are disjoint or poison otherwise.
2721 if (match(RecWithFlags, m_BinaryOr(m_VPValue(A), m_VPValue(B))) &&
2722 RecWithFlags->isDisjoint()) {
2723 VPBuilder Builder(RecWithFlags);
2724 VPInstruction *New =
2725 Builder.createAdd(A, B, RecWithFlags->getDebugLoc());
2726 New->setUnderlyingValue(RecWithFlags->getUnderlyingValue());
2727 RecWithFlags->replaceAllUsesWith(New);
2728 RecWithFlags->eraseFromParent();
2729 CurRec = New;
2730 } else
2731 RecWithFlags->dropPoisonGeneratingFlags();
2732 } else {
2735 (void)Instr;
2736 assert((!Instr || !Instr->hasPoisonGeneratingFlags()) &&
2737 "found instruction with poison generating flags not covered by "
2738 "VPRecipeWithIRFlags");
2739 }
2740
2741 // Add new definitions to the worklist.
2742 for (VPValue *Operand : CurRec->operands())
2743 if (VPRecipeBase *OpDef = Operand->getDefiningRecipe())
2744 Worklist.push_back(OpDef);
2745 }
2746 });
2747
2748 // We want to exclude the tail folding case, as we don't need to drop flags
2749 // for operations computing the first lane in this case: the first lane of the
2750 // header mask must always be true. For reverse memory accesses, the mask is
2751 // wrapped in a Reverse, which is just a permutation of the header mask, so
2752 // peel it off before checking. The header mask is still the abstract region
2753 // value at this point (materialization happens later).
2754 auto m_UnlessHdrMask = m_Unless( // NOLINT
2756
2757 // Traverse all the recipes in the VPlan and collect the poison-generating
2758 // recipes in the backward slice starting at the address of a VPWidenRecipe or
2759 // VPInterleaveRecipe.
2760 auto Iter =
2763 for (VPRecipeBase &Recipe : *VPBB) {
2764 if (auto *WidenRec = dyn_cast<VPWidenMemoryRecipe>(&Recipe)) {
2765 VPRecipeBase *AddrDef = WidenRec->getAddr()->getDefiningRecipe();
2766 if (AddrDef && WidenRec->isConsecutive() && WidenRec->getMask() &&
2767 match(WidenRec->getMask(), m_UnlessHdrMask))
2768 CollectPoisonGeneratingInstrsInBackwardSlice(AddrDef);
2769 } else if (auto *InterleaveRec = dyn_cast<VPInterleaveRecipe>(&Recipe)) {
2770 VPRecipeBase *AddrDef = InterleaveRec->getAddr()->getDefiningRecipe();
2771 if (AddrDef && InterleaveRec->getMask() &&
2772 match(InterleaveRec->getMask(), m_UnlessHdrMask))
2773 CollectPoisonGeneratingInstrsInBackwardSlice(AddrDef);
2774 }
2775 }
2776 }
2777}
2778
2780 VPlan &Plan,
2782 &InterleaveGroups,
2783 const bool &EpilogueAllowed) {
2784 if (InterleaveGroups.empty())
2785 return;
2786
2788 for (VPBasicBlock *VPBB :
2791 for (VPRecipeBase &R : make_filter_range(*VPBB, [](VPRecipeBase &R) {
2792 return isa<VPWidenMemoryRecipe>(&R);
2793 })) {
2794 auto *MemR = cast<VPWidenMemoryRecipe>(&R);
2795 IRMemberToRecipe[&MemR->getIngredient()] = MemR;
2796 }
2797
2798 // Interleave memory: for each Interleave Group we marked earlier as relevant
2799 // for this VPlan, replace the Recipes widening its memory instructions with a
2800 // single VPInterleaveRecipe at its insertion point.
2801 VPDominatorTree VPDT(Plan);
2802 for (const auto *IG : InterleaveGroups) {
2803 VPWidenMemoryRecipe *Start = nullptr;
2804 Instruction *StartMember = nullptr;
2805 for (auto *Member : IG->members())
2806 if (VPWidenMemoryRecipe *R = IRMemberToRecipe.lookup(Member)) {
2807 StartMember = Member;
2808 Start = R;
2809 break;
2810 }
2811 if (!StartMember) // All member recipes are dead, so the group is dead.
2812 continue;
2813 VPIRMetadata InterleaveMD(*Start);
2814 SmallVector<VPValue *, 4> StoredValues;
2815 for (unsigned I = 0; I < IG->getFactor(); ++I) {
2816 Instruction *MemberI = IG->getMember(I);
2817 if (!MemberI)
2818 continue;
2819 if (VPWidenMemoryRecipe *MemoryR = IRMemberToRecipe.lookup(MemberI)) {
2820 if (auto *StoreR = dyn_cast<VPWidenStoreRecipe>(MemoryR->getAsRecipe()))
2821 StoredValues.push_back(StoreR->getStoredValue());
2822 InterleaveMD.intersect(*MemoryR);
2823 } else {
2824 InterleaveMD.intersect(VPIRMetadata(*MemberI));
2825 }
2826 }
2827
2828 bool NeedsMaskForGaps =
2829 (IG->requiresScalarEpilogue() && !EpilogueAllowed) ||
2830 (!StoredValues.empty() && !IG->isFull());
2831
2832 Instruction *IRInsertPos = IG->getInsertPos();
2833 auto *InsertPos = IRMemberToRecipe.lookup(IRInsertPos);
2834 if (!InsertPos) {
2835 // InsertPos member is dead: find a new member that is alive.
2836 assert(isa<VPWidenLoadRecipe>(Start->getAsRecipe()) &&
2837 "Dead member in non-load group?");
2838 InsertPos = Start;
2839 for (Instruction *Member : IG->members())
2840 if (VPWidenMemoryRecipe *MemberR = IRMemberToRecipe.lookup(Member))
2841 if (VPDT.properlyDominates(MemberR->getAsRecipe(),
2842 InsertPos->getAsRecipe()))
2843 InsertPos = MemberR;
2844 IRInsertPos = &InsertPos->getIngredient();
2845 }
2846 VPRecipeBase *InsertPosR = InsertPos->getAsRecipe();
2847
2849 if (auto *Gep = dyn_cast<GetElementPtrInst>(
2850 getLoadStorePointerOperand(IRInsertPos)->stripPointerCasts()))
2851 NW = Gep->getNoWrapFlags().withoutNoUnsignedWrap();
2852
2853 // Get or create the start address for the interleave group.
2854 VPValue *Addr = Start->getAddr();
2855 VPRecipeBase *AddrDef = Addr->getDefiningRecipe();
2856 if (IG->getIndex(StartMember) != 0 ||
2857 (AddrDef && !VPDT.properlyDominates(AddrDef, InsertPosR))) {
2858 // Either member zero's recipe is dead, or we cannot re-use the address of
2859 // member zero because it does not dominate the insert position. Instead,
2860 // use the address of the insert position and create a PtrAdd adjusting it
2861 // to the address of member zero.
2862 // TODO: Hoist Addr's defining recipe (and any operands as needed) to
2863 // InsertPos or sink loads above zero members to join it.
2864 assert(IG->getIndex(IRInsertPos) != 0 &&
2865 "index of insert position shouldn't be zero");
2866 auto &DL = IRInsertPos->getDataLayout();
2867 APInt Offset(32,
2868 DL.getTypeAllocSize(getLoadStoreType(IRInsertPos)) *
2869 IG->getIndex(IRInsertPos),
2870 /*IsSigned=*/true);
2871 VPValue *OffsetVPV = Plan.getConstantInt(-Offset);
2872 VPBuilder B(InsertPosR);
2873 Addr = B.createNoWrapPtrAdd(InsertPos->getAddr(), OffsetVPV, NW);
2874 }
2875 // If the group is reverse, adjust the index to refer to the last vector
2876 // lane instead of the first. We adjust the index from the first vector
2877 // lane, rather than directly getting the pointer for lane VF - 1, because
2878 // the pointer operand of the interleaved access is supposed to be uniform.
2879 if (IG->isReverse()) {
2880 auto *ReversePtr = new VPVectorEndPointerRecipe(
2881 Addr, &Plan.getVF(), getLoadStoreType(IRInsertPos),
2882 -(int64_t)IG->getFactor(), NW, InsertPosR->getDebugLoc());
2883 ReversePtr->insertBefore(InsertPosR);
2884 Addr = ReversePtr;
2885 }
2886 auto *VPIG = new VPInterleaveRecipe(
2887 IG, Addr, StoredValues, InsertPos->getMask(), NeedsMaskForGaps,
2888 InterleaveMD, InsertPosR->getDebugLoc());
2889 VPIG->insertBefore(InsertPosR);
2890
2891 unsigned J = 0;
2892 for (unsigned i = 0; i < IG->getFactor(); ++i)
2893 if (Instruction *Member = IG->getMember(i)) {
2894 VPWidenMemoryRecipe *MemberR = IRMemberToRecipe.lookup(Member);
2895 if (!Member->getType()->isVoidTy()) {
2896 if (MemberR) {
2897 VPValue *OriginalV = MemberR->getAsRecipe()->getVPSingleValue();
2898 OriginalV->replaceAllUsesWith(VPIG->getVPValue(J));
2899 }
2900 J++;
2901 }
2902 if (MemberR)
2903 MemberR->getAsRecipe()->eraseFromParent();
2904 }
2905 }
2906}
2907
2908/// Returns the VPValue representing the uncountable exit comparison used by
2909/// AnyOf if the recipes it depends on can be traced back to live-ins and
2910/// the addresses (in GEP/PtrAdd form) of any (non-masked) load used in
2911/// generating the values for the comparison. The recipes are stored in
2912/// \p Recipes.
2913static std::optional<VPValue *>
2915 VPBasicBlock *LatchVPBB) {
2916 // Given a plain CFG VPlan loop with countable latch exiting block
2917 // \p LatchVPBB, we're looking to match the recipes contributing to the
2918 // uncountable exit condition comparison (here, vp<%4>) back to either
2919 // live-ins or the address nodes for the load used as part of the uncountable
2920 // exit comparison so that we can either move them within the loop, or copy
2921 // them to the preheader depending on the chosen method for dealing with
2922 // stores in uncountable exit loops.
2923 //
2924 // Currently, the address of the load is restricted to a GEP with 2 operands
2925 // and a live-in base address. This constraint may be relaxed later.
2926 //
2927 // VPlan ' for UF>=1' {
2928 // Live-in vp<%0> = VF * UF
2929 // Live-in vp<%1> = vector-trip-count
2930 // Live-in ir<20> = original trip-count
2931 //
2932 // ir-bb<entry>:
2933 // Successor(s): scalar.ph, vector.ph
2934 //
2935 // vector.ph:
2936 // Successor(s): for.body
2937 //
2938 // for.body:
2939 // EMIT vp<%2> = phi ir<0>, vp<%index.next>
2940 // EMIT-SCALAR ir<%iv> = phi [ ir<0>, vector.ph ], [ ir<%iv.next>, for.inc ]
2941 // EMIT ir<%uncountable.addr> = getelementptr inbounds nuw ir<%pred>,ir<%iv>
2942 // EMIT ir<%uncountable.val> = load ir<%uncountable.addr>
2943 // EMIT ir<%uncountable.cond> = icmp sgt ir<%uncountable.val>, ir<500>
2944 // EMIT vp<%3> = masked-cond ir<%uncountable.cond>
2945 // Successor(s): for.inc
2946 //
2947 // for.inc:
2948 // EMIT ir<%iv.next> = add nuw nsw ir<%iv>, ir<1>
2949 // EMIT ir<%countable.cond> = icmp eq ir<%iv.next>, ir<20>
2950 // EMIT vp<%index.next> = add nuw vp<%2>, vp<%0>
2951 // EMIT vp<%4> = any-of ir<%3>
2952 // EMIT vp<%5> = icmp eq vp<%index.next>, vp<%1>
2953 // EMIT branch-on-two-conds vp<%4>, vp<%5>
2954 // Successor(s): middle.block, middle.block, for.body
2955 //
2956 // middle.block:
2957 // Successor(s): ir-bb<exit>, scalar.ph
2958 //
2959 // ir-bb<exit>:
2960 // No successors
2961 //
2962 // scalar.ph:
2963 // }
2964
2965 // Find the uncountable loop exit condition.
2966 VPValue *UncountableCondition = nullptr;
2967 if (!match(LatchVPBB->getTerminator(),
2968 m_BranchOnTwoConds(m_AnyOf(m_VPValue(UncountableCondition)),
2969 m_VPValue())))
2970 return std::nullopt;
2971
2973 Worklist.push_back(UncountableCondition);
2974 while (!Worklist.empty()) {
2975 VPValue *V = Worklist.pop_back_val();
2976
2977 // Any value defined outside the loop does not need to be copied.
2978 if (V->isDefinedOutsideLoopRegions())
2979 continue;
2980
2981 // FIXME: Remove the single user restriction; it's here because we're
2982 // starting with the simplest set of loops we can, and multiple
2983 // users means needing to add PHI nodes in the transform.
2984 if (V->getNumUsers() > 1)
2985 return std::nullopt;
2986
2987 VPValue *Op1, *Op2;
2988 // Walk back through recipes until we find at least one load from memory.
2989 if (match(V, m_ICmp(m_VPValue(Op1), m_VPValue(Op2)))) {
2990 Worklist.push_back(Op1);
2991 Worklist.push_back(Op2);
2992 Recipes.push_back(cast<VPInstruction>(V->getDefiningRecipe()));
2993 } else if (match(V, m_VPInstruction<Instruction::Load>(m_VPValue(Op1)))) {
2994 VPRecipeBase *GepR = Op1->getDefiningRecipe();
2995 // Only matching base + single offset term for now.
2996 if (GepR->getNumOperands() != 2)
2997 return std::nullopt;
2998 // Matching a GEP with a loop-invariant base ptr.
3000 m_LiveIn(), m_VPValue())))
3001 return std::nullopt;
3002 Recipes.push_back(cast<VPInstruction>(V->getDefiningRecipe()));
3003 Recipes.push_back(cast<VPInstruction>(GepR));
3005 m_VPValue(Op1)))) {
3006 Worklist.push_back(Op1);
3007 Recipes.push_back(cast<VPInstruction>(V->getDefiningRecipe()));
3008 } else
3009 return std::nullopt;
3010 }
3011
3012 // If we couldn't match anything, don't return the condition. It may be
3013 // defined outside the loop.
3014 if (Recipes.empty() ||
3016 return std::nullopt;
3017
3018 return UncountableCondition;
3019}
3020
3026
3027/// Update \p Plan to mask memory operations in the loop based on whether the
3028/// early exit is taken or not.
3029///
3030/// We're currently expecting to find a loop with properties similar to the
3031/// following:
3032///
3033/// for.body:
3034/// ir<%indvars.iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<%0>
3035/// EMIT ir<%arrayidx> = getelementptr inbounds nuw ir<@c>, ir<%indvars.iv>
3036/// EMIT-SCALAR ir<%0> = load ir<%arrayidx>
3037/// EMIT ir<%cmp1> = icmp sgt ir<%0>, ir<5>
3038/// EMIT vp<%1> = masked-cond ir<%cmp1>
3039/// Successor(s): if.end
3040///
3041/// if.end:
3042/// EMIT ir<%arrayidx3> = getelementptr inbounds nuw ir<@src>, ir<%indvars.iv>
3043/// EMIT-SCALAR ir<%2> = load ir<%arrayidx3>
3044/// EMIT ir<%add> = add nsw ir<%2>, ir<42>
3045/// EMIT ir<%arrayidx5> = getelementptr inbounds nuw ir<@dst>, ir<%indvars.iv>
3046/// EMIT store ir<%add>, ir<%arrayidx5>
3047/// EMIT ir<%indvars.iv.next> = add nuw nsw ir<%indvars.iv>, ir<1>
3048/// EMIT vp<%3> = any-of ir<%1>
3049/// EMIT ir<%exitcond.not> = icmp eq ir<%indvars.iv.next>, ir<10000>
3050/// EMIT branch-on-two-conds vp<%3>, ir<%exitcond.not>
3051/// Successor(s): middle.block, middle.block, for.body
3052///
3053/// We currently expect LoopVectorizationLegality to ensure that:
3054/// * There must also be a counted exit. We will need to support speculative
3055/// or first-faulting loads before we can remove this restriction.
3056/// * Any stores within the loop must not alias with the load used for the
3057/// uncountable exit. We can relax this a bit with runtime aliasing checks.
3058/// * Other memory operations in the loop can take place before or after the
3059/// uncountable exit, but must also be unconditional. We need to support
3060/// combining the conditions in VPlanPredicator.
3061/// * The loop must have a single unconditional load contributing to the
3062/// uncountable exit comparison, and the other term must be loop-invariant.
3063/// Improving upon this requires work in getRecipesForUncountableExit to
3064/// handle more complex recipe graphs.
3067 VPBasicBlock *HeaderVPBB, VPBasicBlock *LatchVPBB, VPBasicBlock *MiddleVPBB,
3068 Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT,
3069 AssumptionCache *AC) {
3070
3071 // Disconnect early exiting blocks from successors, remove branches. We
3072 // currently don't support multiple uses for recipes involved in creating
3073 // the uncountable exit condition.
3074 for (auto &Exit : Exits) {
3075 if (Exit.EarlyExitingVPBB == LatchVPBB)
3076 continue;
3077
3078 for (VPRecipeBase &R : Exit.EarlyExitVPBB->phis())
3079 cast<VPIRPhi>(&R)->removeIncomingValueFor(Exit.EarlyExitingVPBB);
3080 Exit.EarlyExitingVPBB->getTerminator()->eraseFromParent();
3081 VPBlockUtils::disconnectBlocks(Exit.EarlyExitingVPBB, Exit.EarlyExitVPBB);
3082 }
3083
3084 VPDominatorTree VPDT(Plan);
3085
3086 // We can abandon a VPlan entirely if we return false here, so we shouldn't
3087 // crash if some earlier assumptions on scalar IR don't hold for the vplan
3088 // version of the loop.
3089 SmallVector<VPInstruction *, 8> ConditionRecipes;
3090
3091 std::optional<VPValue *> Cond =
3092 getRecipesForUncountableExit(ConditionRecipes, LatchVPBB);
3093 if (!Cond)
3094 return false;
3095
3096 // Find load contributing to condition.
3097 // At the moment LoopVectorizationLegality only supports a single
3098 // early-exit expression with a compare and a single load that must
3099 // be unconditional.
3100 // TODO: Support more than one load.
3101 auto *Load =
3102 find_singleton<VPInstruction>(ConditionRecipes, [](auto *I, bool _) {
3104 ? I
3105 : nullptr;
3106 });
3107 assert(Load && "Couldn't find exactly one load");
3108 // TODO: Support conditional loads for uncountable exits.
3109 assert(VPDT.dominates(Load->getParent(), LatchVPBB) &&
3110 "Uncountable exit condition load is conditional.");
3111 VPInstruction *Ptr = cast<VPInstruction>(Load->getOperand(0));
3112
3113 // Ensure that we are guaranteed to be able to dereference the memory used
3114 // for determining the uncountable exit for the maximum possible number of
3115 // scalar iterations of the loop.
3116 //
3117 // TODO: Support first-faulting loads in cases where we don't know whether
3118 // all possible addresses are dereferenceable.
3119 {
3121 const SCEV *PtrSCEV = vputils::getSCEVExprForVPValue(Ptr, PSE, TheLoop);
3122 const DataLayout &DL = Plan.getDataLayout();
3123 APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getScalarType()),
3124 DL.getTypeStoreSize(Load->getScalarType()).getFixedValue());
3126 PtrSCEV, cast<LoadInst>(Load->getUnderlyingInstr())->getAlign(),
3127 PSE.getSE()->getConstant(EltSize), TheLoop, *PSE.getSE(), DT, AC,
3128 &Predicates))
3129 return false;
3130 }
3131
3132 // Check for a single GEP for the condition load to see if we can link it to
3133 // a widen IV recipe with a step of 1; we're only interested in contiguous
3134 // accesses for the condition load right now.
3135 auto *IV = cast<VPWidenInductionRecipe>(&HeaderVPBB->front());
3136 if (!match(IV->getStartValue(), m_SpecificInt(0)) ||
3137 !match(IV->getStepValue(), m_SpecificInt(1)))
3138 return false;
3140 m_Specific(IV))))
3141 return false;
3142
3143 // We want to guarantee that the uncountable exit condition (and the mask
3144 // we will generate from it) are available for all operations in the loop
3145 // that need to be masked. If the condition recipes are not already the first
3146 // recipes in the header after the last phi, move them there.
3147 auto InsertIt = HeaderVPBB->getFirstNonPhi();
3148 while (InsertIt != HeaderVPBB->end() &&
3149 is_contained(ConditionRecipes, &*InsertIt)) {
3150 erase(ConditionRecipes, &*InsertIt);
3151 InsertIt++;
3152 }
3153 for (auto *Recipe : reverse(ConditionRecipes))
3154 Recipe->moveBefore(*HeaderVPBB, InsertIt);
3155
3156 // Create a mask to represent all lanes that fully execute in the vector loop,
3157 // stopping short of any early exit.
3158 VPBuilder MaskBuilder(HeaderVPBB, InsertIt);
3159 VPValue *FirstActive = MaskBuilder.createFirstActiveLane(*Cond);
3160 Type *IVScalarTy = IV->getScalarType();
3161 VPValue *Zero = Plan.getZero(IVScalarTy);
3162 FirstActive =
3163 MaskBuilder.createScalarZExtOrTrunc(FirstActive, IVScalarTy, DebugLoc());
3165 {Zero, FirstActive}, DebugLoc(),
3166 "uncountable.exit.mask");
3167
3168 // Convert all other memory operations to use the mask.
3169 for (VPBasicBlock *VPBB : vp_rpo_plain_cfg_loop_body(HeaderVPBB))
3170 for (VPRecipeBase &R : *VPBB)
3171 if (R.mayReadOrWriteMemory() && &R != Load) {
3172 // TODO: Handle conditional memory operations in the loop.
3173 if (!VPDT.dominates(R.getParent(), LatchVPBB))
3174 return false;
3175 cast<VPInstruction>(&R)->addMask(Mask);
3176 }
3177
3178 // Update middle block branch to compare (IV + however many lanes were active)
3179 // against the full trip count, since we may be exiting the vector loop early.
3180 // If we didn't take an early exit, we should get the equivalent of VF from
3181 // the FirstActiveLane.
3182 assert(match(MiddleVPBB->getTerminator(), m_BranchOnCond()) &&
3183 "Expected BranchOnCond terminator for MiddleVPBB");
3184 VPBuilder MiddleBuilder(MiddleVPBB->getTerminator());
3185 VPValue *ScalarIV = MiddleBuilder.createNaryOp(VPInstruction::ExtractLane,
3186 {Zero, IV}, DebugLoc());
3187 VPValue *ExitIV = MiddleBuilder.createAdd(ScalarIV, FirstActive);
3188 VPValue *FullTC =
3189 MiddleBuilder.createICmp(CmpInst::ICMP_EQ, ExitIV, Plan.getTripCount());
3190 MiddleVPBB->getTerminator()->setOperand(0, FullTC);
3191
3192 // Update resume phi in scalar.ph.
3193 VPBasicBlock *ScalarPH = Plan.getScalarPreheader();
3194 auto Phis = ScalarPH->phis();
3195 // TODO: Handle more than one Phi; re-derive from IV.
3196 // TODO: Handle reductions.
3197 if (range_size(Phis) != 1)
3198 return false;
3199 VPPhi *ContinueIV = cast<VPPhi>(Phis.begin());
3200 // Make sure we're referring to the same IV.
3201 assert(
3202 match(ContinueIV->getOperand(0),
3204 "Continuing from different IV");
3205 ContinueIV->setOperand(0, ExitIV);
3206 return true;
3207}
3208
3210 VPlan &Plan, Loop *TheLoop, PredicatedScalarEvolution &PSE,
3212#ifndef NDEBUG
3213 VPDominatorTree VPDT(Plan);
3214#endif
3215
3216 auto *MiddleVPBB = VPBlockUtils::getPlainCFGMiddleBlock(Plan);
3217 auto [HeaderVPBB, LatchVPBB] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
3218
3219 // Dereferenceability is checked separately for uncountable exit loops with
3220 // stores, as only the loads contributing to the exit condition need to
3221 // be checked.
3222 if (Style == UncountableExitStyle::ReadOnly &&
3223 !areAllLoadsDereferenceable(HeaderVPBB, TheLoop, PSE, DT, AC))
3224 return false;
3225
3226 VPBuilder LatchBuilder(LatchVPBB->getTerminator());
3228 for (auto [EarlyExitingVPBB, ExitBlock] :
3229 vputils::getEarlyExits(Plan, MiddleVPBB)) {
3230 // Collect condition for this early exit.
3231 VPBlockBase *TrueSucc = EarlyExitingVPBB->getSuccessors()[0];
3232 VPValue *CondOfEarlyExitingVPBB;
3233 [[maybe_unused]] bool Matched =
3234 match(EarlyExitingVPBB->getTerminator(),
3235 m_BranchOnCond(m_VPValue(CondOfEarlyExitingVPBB)));
3236 assert(Matched && "Terminator must be BranchOnCond");
3237
3238 // Insert the MaskedCond in the EarlyExitingVPBB so the predicator adds
3239 // the correct block mask.
3240 VPBuilder EarlyExitingBuilder(EarlyExitingVPBB->getTerminator());
3241 auto *CondToEarlyExit = EarlyExitingBuilder.createNaryOp(
3243 TrueSucc == ExitBlock
3244 ? CondOfEarlyExitingVPBB
3245 : EarlyExitingBuilder.createNot(CondOfEarlyExitingVPBB));
3246 assert((isa<VPIRValue>(CondOfEarlyExitingVPBB) ||
3247 !VPDT.properlyDominates(EarlyExitingVPBB, LatchVPBB) ||
3248 VPDT.properlyDominates(
3249 CondOfEarlyExitingVPBB->getDefiningRecipe()->getParent(),
3250 LatchVPBB)) &&
3251 "exit condition must dominate the latch");
3252 Exits.push_back({
3253 EarlyExitingVPBB,
3254 ExitBlock,
3255 CondToEarlyExit,
3256 });
3257 }
3258
3259 assert(!Exits.empty() && "must have at least one early exit");
3260 // Sort exits by RPO order to get correct program order. RPO gives a
3261 // topological ordering of the CFG, ensuring upstream exits are checked
3262 // before downstream exits in the dispatch chain.
3264 HeaderVPBB);
3266 for (const auto &[Num, VPB] : enumerate(RPOT))
3267 RPOIdx[VPB] = Num;
3268 llvm::sort(Exits, [&RPOIdx](const EarlyExitInfo &A, const EarlyExitInfo &B) {
3269 return RPOIdx[A.EarlyExitingVPBB] < RPOIdx[B.EarlyExitingVPBB];
3270 });
3271#ifndef NDEBUG
3272 // After RPO sorting, verify that for any pair where one exit dominates
3273 // another, the dominating exit comes first. This is guaranteed by RPO
3274 // (topological order) and is required for the dispatch chain correctness.
3275 for (unsigned I = 0; I + 1 < Exits.size(); ++I)
3276 for (unsigned J = I + 1; J < Exits.size(); ++J)
3277 assert(!VPDT.properlyDominates(Exits[J].EarlyExitingVPBB,
3278 Exits[I].EarlyExitingVPBB) &&
3279 "RPO sort must place dominating exits before dominated ones");
3280#endif
3281
3282 // Build the AnyOf condition for the latch terminator using logical OR
3283 // to avoid poison propagation from later exit conditions when an earlier
3284 // exit is taken.
3285 VPValue *Combined = Exits[0].CondToExit;
3286 for (const EarlyExitInfo &Info : drop_begin(Exits))
3287 Combined = LatchBuilder.createLogicalOr(Combined, Info.CondToExit);
3288
3289 VPValue *IsAnyExitTaken =
3290 LatchBuilder.createNaryOp(VPInstruction::AnyOf, {Combined});
3291
3292 // Create a comparison for the latch exit condition and replace the
3293 // BranchOnCond with a BranchOnTwoConds. The original BranchOnCond's condition
3294 // is used as the latch-exit condition; canonical IV recipes have not been
3295 // introduced yet, so there is no BranchOnCount to derive the condition from.
3296 auto *LatchExitingBranch = cast<VPInstruction>(LatchVPBB->getTerminator());
3297 assert(LatchExitingBranch->getOpcode() == VPInstruction::BranchOnCond &&
3298 "Unexpected terminator");
3299 VPValue *IsLatchExitTaken = LatchExitingBranch->getOperand(0);
3300 DebugLoc LatchDL = LatchExitingBranch->getDebugLoc();
3301 LatchExitingBranch->eraseFromParent();
3302 LatchBuilder.setInsertPoint(LatchVPBB);
3304 {IsAnyExitTaken, IsLatchExitTaken}, LatchDL);
3305 LatchVPBB->clearSuccessors();
3306
3308 // If handling the exiting lane in the scalar loop, combine the exit
3309 // conditions into a single BranchOnCond.
3310 LatchVPBB->setSuccessors({MiddleVPBB, MiddleVPBB, HeaderVPBB});
3311 MiddleVPBB->clearPredecessors();
3312 MiddleVPBB->setPredecessors({LatchVPBB, LatchVPBB});
3314 Plan, Exits, HeaderVPBB, LatchVPBB, MiddleVPBB, TheLoop, PSE, DT, AC);
3315 }
3316
3317 // Create the vector.early.exit blocks.
3318 SmallVector<VPBasicBlock *> VectorEarlyExitVPBBs(Exits.size());
3319 for (unsigned Idx = 0; Idx != Exits.size(); ++Idx) {
3320 Twine BlockSuffix = Exits.size() == 1 ? "" : Twine(".") + Twine(Idx);
3321 VPBasicBlock *VectorEarlyExitVPBB =
3322 Plan.createVPBasicBlock("vector.early.exit" + BlockSuffix);
3323 VectorEarlyExitVPBBs[Idx] = VectorEarlyExitVPBB;
3324 }
3325
3326 // Create the dispatch block (or reuse the single exit block if only one
3327 // exit). The dispatch block computes the first active lane of the combined
3328 // condition and, for multiple exits, chains through conditions to determine
3329 // which exit to take.
3330 VPBasicBlock *DispatchVPBB =
3331 Exits.size() == 1 ? VectorEarlyExitVPBBs[0]
3332 : Plan.createVPBasicBlock("vector.early.exit.check");
3333 DispatchVPBB->setPredecessors({LatchVPBB});
3334 LatchVPBB->setSuccessors({DispatchVPBB, MiddleVPBB, HeaderVPBB});
3335 VPBuilder DispatchBuilder(DispatchVPBB, DispatchVPBB->begin());
3336 VPValue *FirstActiveLane = DispatchBuilder.createFirstActiveLane(
3337 {Combined}, DebugLoc::getUnknown(), "first.active.lane");
3338
3339 // For each early exit, disconnect the original exiting block
3340 // (early.exiting.I) from the exit block (ir-bb<exit.I>) and route through a
3341 // new vector.early.exit block. Update ir-bb<exit.I>'s phis to extract their
3342 // values at the first active lane:
3343 //
3344 // Input:
3345 // early.exiting.I:
3346 // ...
3347 // EMIT branch-on-cond vp<%cond.I>
3348 // Successor(s): in.loop.succ, ir-bb<exit.I>
3349 //
3350 // ir-bb<exit.I>:
3351 // IR %phi = phi [ vp<%incoming.I>, early.exiting.I ], ...
3352 //
3353 // Output:
3354 // early.exiting.I:
3355 // ...
3356 // Successor(s): in.loop.succ
3357 //
3358 // vector.early.exit.I:
3359 // EMIT vp<%exit.val> = extract-lane vp<%first.lane>, vp<%incoming.I>
3360 // Successor(s): ir-bb<exit.I>
3361 //
3362 // ir-bb<exit.I>:
3363 // IR %phi = phi ... (extra operand: vp<%exit.val> from
3364 // vector.early.exit.I)
3365 //
3366 for (auto [Exit, VectorEarlyExitVPBB] :
3367 zip_equal(Exits, VectorEarlyExitVPBBs)) {
3368 auto &[EarlyExitingVPBB, EarlyExitVPBB, _] = Exit;
3369 // Adjust the phi nodes in EarlyExitVPBB.
3370 // 1. remove incoming values from EarlyExitingVPBB,
3371 // 2. extract the incoming value at FirstActiveLane
3372 // 3. add back the extracts as last operands for the phis
3373 // Then adjust the CFG, removing the edge between EarlyExitingVPBB and
3374 // EarlyExitVPBB and adding a new edge between VectorEarlyExitVPBB and
3375 // EarlyExitVPBB. The extracts at FirstActiveLane are now the incoming
3376 // values from VectorEarlyExitVPBB.
3377 for (VPRecipeBase &R : EarlyExitVPBB->phis()) {
3378 auto *ExitIRI = cast<VPIRPhi>(&R);
3379 VPValue *IncomingVal =
3380 ExitIRI->getIncomingValueForBlock(EarlyExitingVPBB);
3381 VPValue *NewIncoming = IncomingVal;
3382 if (!isa<VPIRValue>(IncomingVal)) {
3383 VPBuilder EarlyExitBuilder(VectorEarlyExitVPBB);
3384 NewIncoming = EarlyExitBuilder.createNaryOp(
3385 VPInstruction::ExtractLane, {FirstActiveLane, IncomingVal},
3386 DebugLoc::getUnknown(), "early.exit.value");
3387 }
3388 ExitIRI->removeIncomingValueFor(EarlyExitingVPBB);
3389 ExitIRI->addIncoming(NewIncoming);
3390 }
3391
3392 EarlyExitingVPBB->getTerminator()->eraseFromParent();
3393 VPBlockUtils::disconnectBlocks(EarlyExitingVPBB, EarlyExitVPBB);
3394 VPBlockUtils::connectBlocks(VectorEarlyExitVPBB, EarlyExitVPBB);
3395 }
3396
3397 // Chain through exits: for each exit, check if its condition is true at
3398 // the first active lane. If so, take that exit; otherwise, try the next.
3399 // The last exit needs no check since it must be taken if all others fail.
3400 //
3401 // For 3 exits (cond.0, cond.1, cond.2), this creates:
3402 //
3403 // latch:
3404 // ...
3405 // EMIT vp<%combined> = logical-or vp<%cond.0>, vp<%cond.1>, vp<%cond.2>
3406 // ...
3407 //
3408 // vector.early.exit.check:
3409 // EMIT vp<%first.lane> = first-active-lane vp<%combined>
3410 // EMIT vp<%at.cond.0> = extract-lane vp<%first.lane>, vp<%cond.0>
3411 // EMIT branch-on-cond vp<%at.cond.0>
3412 // Successor(s): vector.early.exit.0, vector.early.exit.check.0
3413 //
3414 // vector.early.exit.check.0:
3415 // EMIT vp<%at.cond.1> = extract-lane vp<%first.lane>, vp<%cond.1>
3416 // EMIT branch-on-cond vp<%at.cond.1>
3417 // Successor(s): vector.early.exit.1, vector.early.exit.2
3418 VPBasicBlock *CurrentBB = DispatchVPBB;
3419 for (auto [I, Exit] : enumerate(ArrayRef(Exits).drop_back())) {
3420 VPValue *LaneVal = DispatchBuilder.createNaryOp(
3421 VPInstruction::ExtractLane, {FirstActiveLane, Exit.CondToExit},
3422 DebugLoc::getUnknown(), "exit.cond.at.lane");
3423
3424 // For the last dispatch, branch directly to the last exit on false;
3425 // otherwise, create a new check block.
3426 bool IsLastDispatch = (I + 2 == Exits.size());
3427 VPBasicBlock *FalseBB =
3428 IsLastDispatch ? VectorEarlyExitVPBBs.back()
3429 : Plan.createVPBasicBlock(
3430 Twine("vector.early.exit.check.") + Twine(I));
3431
3432 DispatchBuilder.createNaryOp(VPInstruction::BranchOnCond, {LaneVal});
3433 CurrentBB->setSuccessors({VectorEarlyExitVPBBs[I], FalseBB});
3434 VectorEarlyExitVPBBs[I]->setPredecessors({CurrentBB});
3435 FalseBB->setPredecessors({CurrentBB});
3436
3437 CurrentBB = FalseBB;
3438 DispatchBuilder.setInsertPoint(CurrentBB);
3439 }
3440
3441 return true;
3442}
3443
3444/// This function tries convert extended in-loop reductions to
3445/// VPExpressionRecipe and clamp the \p Range if it is beneficial and
3446/// valid. The created recipe must be decomposed to its constituent
3447/// recipes before execution.
3448static VPExpressionRecipe *
3450 VFRange &Range) {
3451 Type *RedTy = Red->getScalarType();
3452 VPValue *VecOp = Red->getVecOp();
3453
3454 assert(!Red->isPartialReduction() &&
3455 "This path does not support partial reductions");
3456
3457 // Clamp the range if using extended-reduction is profitable.
3458 auto IsExtendedRedValidAndClampRange =
3459 [&](unsigned Opcode, Instruction::CastOps ExtOpc, Type *SrcTy) -> bool {
3461 [&](ElementCount VF) {
3462 auto *SrcVecTy = cast<VectorType>(toVectorTy(SrcTy, VF));
3464
3466 InstructionCost ExtCost =
3467 cast<VPWidenCastRecipe>(VecOp)->computeCost(VF, Ctx);
3468 InstructionCost RedCost = Red->computeCost(VF, Ctx);
3469
3470 assert(!RedTy->isFloatingPointTy() &&
3471 "getExtendedReductionCost only supports integer types");
3472 ExtRedCost = Ctx.TTI.getExtendedReductionCost(
3473 Opcode, ExtOpc == Instruction::CastOps::ZExt, RedTy, SrcVecTy,
3474 Red->getFastMathFlagsOrNone(), CostKind);
3475 return ExtRedCost.isValid() && ExtRedCost < ExtCost + RedCost;
3476 },
3477 Range);
3478 };
3479
3480 VPValue *A;
3481 // Match reduce(ext)).
3483 IsExtendedRedValidAndClampRange(
3484 RecurrenceDescriptor::getOpcode(Red->getRecurrenceKind()),
3485 cast<VPWidenCastRecipe>(VecOp)->getOpcode(), A->getScalarType()))
3486 return new VPExpressionRecipe(cast<VPWidenCastRecipe>(VecOp), Red);
3487
3488 return nullptr;
3489}
3490
3491/// This function tries convert extended in-loop reductions to
3492/// VPExpressionRecipe and clamp the \p Range if it is beneficial
3493/// and valid. The created VPExpressionRecipe must be decomposed to its
3494/// constituent recipes before execution. Patterns of the
3495/// VPExpressionRecipe:
3496/// reduce.add(mul(...)),
3497/// reduce.add(mul(ext(A), ext(B))),
3498/// reduce.add(ext(mul(ext(A), ext(B)))).
3499/// reduce.fadd(fmul(ext(A), ext(B)))
3500static VPExpressionRecipe *
3502 VPCostContext &Ctx, VFRange &Range) {
3503 unsigned Opcode = RecurrenceDescriptor::getOpcode(Red->getRecurrenceKind());
3504 if (Opcode != Instruction::Add && Opcode != Instruction::Sub &&
3505 Opcode != Instruction::FAdd)
3506 return nullptr;
3507
3508 assert(!Red->isPartialReduction() &&
3509 "This path does not support partial reductions");
3510 Type *RedTy = Red->getScalarType();
3511
3512 // Clamp the range if using multiply-accumulate-reduction is profitable.
3513 auto IsMulAccValidAndClampRange =
3515 VPWidenCastRecipe *OuterExt) -> bool {
3517 [&](ElementCount VF) {
3519 Type *SrcTy = Ext0 ? Ext0->getOperand(0)->getScalarType() : RedTy;
3520 InstructionCost MulAccCost;
3521
3522 // getMulAccReductionCost for in-loop reductions does not support
3523 // mixed or floating-point extends.
3524 if (Ext0 && Ext1 &&
3525 (Ext0->getOpcode() != Ext1->getOpcode() ||
3526 Ext0->getOpcode() == Instruction::CastOps::FPExt))
3527 return false;
3528
3529 bool IsZExt =
3530 !Ext0 || Ext0->getOpcode() == Instruction::CastOps::ZExt;
3531 auto *SrcVecTy = cast<VectorType>(toVectorTy(SrcTy, VF));
3532 MulAccCost = Ctx.TTI.getMulAccReductionCost(IsZExt, Opcode, RedTy,
3533 SrcVecTy, CostKind);
3534
3535 InstructionCost MulCost = Mul->computeCost(VF, Ctx);
3536 InstructionCost RedCost = Red->computeCost(VF, Ctx);
3537 InstructionCost ExtCost = 0;
3538 if (Ext0)
3539 ExtCost += Ext0->computeCost(VF, Ctx);
3540 if (Ext1)
3541 ExtCost += Ext1->computeCost(VF, Ctx);
3542 if (OuterExt)
3543 ExtCost += OuterExt->computeCost(VF, Ctx);
3544
3545 return MulAccCost.isValid() &&
3546 MulAccCost < ExtCost + MulCost + RedCost;
3547 },
3548 Range);
3549 };
3550
3551 VPValue *VecOp = Red->getVecOp();
3552 VPRecipeBase *Sub = nullptr;
3553 VPValue *A, *B;
3554 VPValue *Tmp = nullptr;
3555
3556 if (RedTy->isFloatingPointTy())
3557 return nullptr;
3558
3559 // Sub reductions could have a sub between the add reduction and vec op.
3560 if (match(VecOp, m_Sub(m_ZeroInt(), m_VPValue(Tmp)))) {
3561 Sub = VecOp->getDefiningRecipe();
3562 VecOp = Tmp;
3563 }
3564
3565 // If ValB is a constant and can be safely extended, truncate it to the same
3566 // type as ExtA's operand, then extend it to the same type as ExtA. This
3567 // creates two uniform extends that can more easily be matched by the rest of
3568 // the bundling code. The ExtB reference, ValB and operand 1 of Mul are all
3569 // replaced with the new extend of the constant.
3570 auto ExtendAndReplaceConstantOp = [](VPWidenCastRecipe *ExtA,
3571 VPWidenCastRecipe *&ExtB, VPValue *&ValB,
3572 VPWidenRecipe *Mul) {
3573 if (!ExtA || ExtB || !isa<VPIRValue>(ValB))
3574 return;
3575 Type *NarrowTy = ExtA->getOperand(0)->getScalarType();
3576 Instruction::CastOps ExtOpc = ExtA->getOpcode();
3577 const APInt *Const;
3578 if (!match(ValB, m_APInt(Const)) ||
3580 Const, NarrowTy, TTI::getPartialReductionExtendKind(ExtOpc)))
3581 return;
3582 // The truncate ensures that the type of each extended operand is the
3583 // same, and it's been proven that the constant can be extended from
3584 // NarrowTy safely. Necessary since ExtA's extended operand would be
3585 // e.g. an i8, while the const will likely be an i32. This will be
3586 // elided by later optimisations.
3587 VPBuilder Builder(Mul);
3588 auto *Trunc =
3589 Builder.createWidenCast(Instruction::CastOps::Trunc, ValB, NarrowTy);
3590 Type *WideTy = ExtA->getScalarType();
3591 ValB = ExtB = Builder.createWidenCast(ExtOpc, Trunc, WideTy);
3592 Mul->setOperand(1, ExtB);
3593 };
3594
3595 // Try to match reduce.add(mul(...)).
3596 if (match(VecOp, m_Mul(m_VPValue(A), m_VPValue(B)))) {
3597 auto *RecipeA = dyn_cast<VPWidenCastRecipe>(A);
3598 auto *RecipeB = dyn_cast<VPWidenCastRecipe>(B);
3599 auto *Mul = cast<VPWidenRecipe>(VecOp);
3600
3601 // Convert reduce.add(mul(ext, const)) to reduce.add(mul(ext, ext(const)))
3602 ExtendAndReplaceConstantOp(RecipeA, RecipeB, B, Mul);
3603
3604 // Match reduce.add/sub(mul(ext, ext)).
3605 if (RecipeA && RecipeB && match(RecipeA, m_ZExtOrSExt(m_VPValue())) &&
3606 match(RecipeB, m_ZExtOrSExt(m_VPValue())) &&
3607 IsMulAccValidAndClampRange(Mul, RecipeA, RecipeB, nullptr)) {
3608 if (Sub)
3609 return new VPExpressionRecipe(RecipeA, RecipeB, Mul,
3610 cast<VPWidenRecipe>(Sub), Red);
3611 return new VPExpressionRecipe(RecipeA, RecipeB, Mul, Red);
3612 }
3613 // TODO: Add an expression type for this variant with a negated mul
3614 if (!Sub && IsMulAccValidAndClampRange(Mul, nullptr, nullptr, nullptr))
3615 return new VPExpressionRecipe(Mul, Red);
3616 }
3617 // TODO: Add an expression type for negated versions of other expression
3618 // variants.
3619 if (Sub)
3620 return nullptr;
3621
3622 // Match reduce.add(ext(mul(A, B))).
3623 if (match(VecOp, m_ZExtOrSExt(m_Mul(m_VPValue(A), m_VPValue(B))))) {
3624 auto *Ext = cast<VPWidenCastRecipe>(VecOp);
3625 auto *Mul = cast<VPWidenRecipe>(Ext->getOperand(0));
3626 auto *Ext0 = dyn_cast<VPWidenCastRecipe>(A);
3627 auto *Ext1 = dyn_cast<VPWidenCastRecipe>(B);
3628
3629 // reduce.add(ext(mul(ext, const)))
3630 // -> reduce.add(ext(mul(ext, ext(const))))
3631 ExtendAndReplaceConstantOp(Ext0, Ext1, B, Mul);
3632
3633 // reduce.add(ext(mul(ext(A), ext(B))))
3634 // -> reduce.add(mul(wider_ext(A), wider_ext(B)))
3635 // The inner extends must either have the same opcode as the outer extend or
3636 // be the same, in which case the multiply can never result in a negative
3637 // value and the outer extend can be folded away by doing wider
3638 // extends for the operands of the mul.
3639 if (Ext0 && Ext1 &&
3640 (Ext->getOpcode() == Ext0->getOpcode() || Ext0 == Ext1) &&
3641 Ext0->getOpcode() == Ext1->getOpcode() &&
3642 IsMulAccValidAndClampRange(Mul, Ext0, Ext1, Ext) && Mul->hasOneUse()) {
3643 auto *NewExt0 = new VPWidenCastRecipe(
3644 Ext0->getOpcode(), Ext0->getOperand(0), Ext->getScalarType(), nullptr,
3645 *Ext0, *Ext0, Ext0->getDebugLoc());
3646 NewExt0->insertBefore(Ext0);
3647
3648 VPWidenCastRecipe *NewExt1 = NewExt0;
3649 if (Ext0 != Ext1) {
3650 NewExt1 = new VPWidenCastRecipe(Ext1->getOpcode(), Ext1->getOperand(0),
3651 Ext->getScalarType(), nullptr, *Ext1,
3652 *Ext1, Ext1->getDebugLoc());
3653 NewExt1->insertBefore(Ext1);
3654 }
3655 auto *NewMul = Mul->cloneWithOperands({NewExt0, NewExt1});
3656 NewMul->insertBefore(Mul);
3657 Ext->replaceAllUsesWith(NewMul);
3658 Ext->eraseFromParent();
3659 Mul->eraseFromParent();
3660 return new VPExpressionRecipe(NewExt0, NewExt1, NewMul, Red);
3661 }
3662 }
3663 return nullptr;
3664}
3665
3666/// This function tries to create abstract recipes from the reduction recipe for
3667/// following optimizations and cost estimation.
3669 VPCostContext &Ctx,
3670 VFRange &Range) {
3671 // Creation of VPExpressions for partial reductions is entirely handled in
3672 // transformToPartialReduction.
3673 assert(!Red->isPartialReduction() &&
3674 "This path does not support partial reductions");
3675
3676 VPExpressionRecipe *AbstractR = nullptr;
3677 auto IP = std::next(Red->getIterator());
3678 auto *VPBB = Red->getParent();
3679 if (auto *MulAcc = tryToMatchAndCreateMulAccumulateReduction(Red, Ctx, Range))
3680 AbstractR = MulAcc;
3681 else if (auto *ExtRed = tryToMatchAndCreateExtendedReduction(Red, Ctx, Range))
3682 AbstractR = ExtRed;
3683 // Cannot create abstract inloop reduction recipes.
3684 if (!AbstractR)
3685 return;
3686
3687 AbstractR->insertBefore(*VPBB, IP);
3688 Red->replaceAllUsesWith(AbstractR);
3689}
3690
3701
3702// Collect common metadata from a group of replicate recipes by intersecting
3703// metadata from all recipes in the group.
3705 VPIRMetadata CommonMetadata = *Recipes.front();
3706 for (VPReplicateRecipe *Recipe : drop_begin(Recipes))
3707 CommonMetadata.intersect(*Recipe);
3708 return CommonMetadata;
3709}
3710
3711template <unsigned Opcode>
3715 const Loop *L) {
3716 static_assert(Opcode == Instruction::Load || Opcode == Instruction::Store,
3717 "Only Load and Store opcodes supported");
3718 [[maybe_unused]] constexpr bool IsLoad = (Opcode == Instruction::Load);
3719
3720 // For each address, collect operations with the same or complementary masks.
3723 Plan, PSE, L,
3724 [](VPReplicateRecipe *RepR) { return RepR->isPredicated(); });
3725 for (auto Recipes : Groups) {
3726 if (Recipes.size() < 2)
3727 continue;
3728
3730 map_range(Recipes, bind_back<getLoadStoreValueType>(IsLoad))) &&
3731 "Expected all recipes in group to have the same load-store type");
3732
3733 // Collect groups with the same or complementary masks.
3734 for (VPReplicateRecipe *&RecipeI : Recipes) {
3735 if (!RecipeI)
3736 continue;
3737
3738 VPValue *MaskI = RecipeI->getMask();
3740 Group.push_back(RecipeI);
3741 RecipeI = nullptr;
3742
3743 // Find all operations with the same or complementary masks.
3744 bool HasComplementaryMask = false;
3745 for (VPReplicateRecipe *&RecipeJ : Recipes) {
3746 if (!RecipeJ)
3747 continue;
3748
3749 VPValue *MaskJ = RecipeJ->getMask();
3750 // Check if any operation in the group has a complementary mask with
3751 // another, that is M1 == NOT(M2) or M2 == NOT(M1).
3752 HasComplementaryMask |= match(MaskI, m_Not(m_Specific(MaskJ))) ||
3753 match(MaskJ, m_Not(m_Specific(MaskI)));
3754 Group.push_back(RecipeJ);
3755 RecipeJ = nullptr;
3756 }
3757
3758 if (HasComplementaryMask) {
3759 assert(Group.size() >= 2 && "must have at least 2 entries");
3760 AllGroups.push_back(std::move(Group));
3761 }
3762 }
3763 }
3764
3765 return AllGroups;
3766}
3767
3768// Find the recipe with minimum alignment in the group.
3769template <typename InstType>
3770static VPReplicateRecipe *
3772 return *min_element(Group, [](VPReplicateRecipe *A, VPReplicateRecipe *B) {
3773 return cast<InstType>(A->getUnderlyingInstr())->getAlign() <
3774 cast<InstType>(B->getUnderlyingInstr())->getAlign();
3775 });
3776}
3777
3780 const Loop *L) {
3781 auto Groups =
3783 if (Groups.empty())
3784 return;
3785
3786 // Process each group of loads.
3787 for (auto &Group : Groups) {
3788 // Try to use the earliest (most dominating) load to replace all others.
3789 VPReplicateRecipe *EarliestLoad = Group[0];
3790 VPBasicBlock *FirstBB = EarliestLoad->getParent();
3791 VPBasicBlock *LastBB = Group.back()->getParent();
3792
3793 // Check that the load doesn't alias with stores between first and last.
3794 auto LoadLoc = vputils::getMemoryLocation(*EarliestLoad);
3795 if (!LoadLoc || !canHoistOrSinkWithNoAliasCheck(*LoadLoc, FirstBB, LastBB))
3796 continue;
3797
3798 // Collect common metadata from all loads in the group.
3799 VPIRMetadata CommonMetadata = getCommonMetadata(Group);
3800
3801 // Find the load with minimum alignment to use.
3802 auto *LoadWithMinAlign = findRecipeWithMinAlign<LoadInst>(Group);
3803
3804 bool IsSingleScalar = EarliestLoad->isSingleScalar();
3805 assert(all_of(Group,
3806 [IsSingleScalar](VPReplicateRecipe *R) {
3807 return R->isSingleScalar() == IsSingleScalar;
3808 }) &&
3809 "all members in group must agree on IsSingleScalar");
3810
3811 // Create an unpredicated version of the earliest load with common
3812 // metadata.
3813 auto *UnpredicatedLoad = new VPReplicateRecipe(
3814 LoadWithMinAlign->getUnderlyingInstr(), {EarliestLoad->getOperand(0)},
3815 IsSingleScalar, /*Mask=*/nullptr, *EarliestLoad, CommonMetadata);
3816
3817 UnpredicatedLoad->insertBefore(EarliestLoad);
3818
3819 // Replace all loads in the group with the unpredicated load.
3820 for (VPReplicateRecipe *Load : Group) {
3821 Load->replaceAllUsesWith(UnpredicatedLoad);
3822 Load->eraseFromParent();
3823 }
3824 }
3825}
3826
3827static bool
3829 PredicatedScalarEvolution &PSE, const Loop &L) {
3830 auto StoreLoc = vputils::getMemoryLocation(*StoresToSink.front());
3831 if (!StoreLoc || !StoreLoc->AATags.Scope)
3832 return false;
3833
3834 // When sinking a group of stores, all members of the group alias each other.
3835 // Skip them during the alias checks.
3836 VPBasicBlock *FirstBB = StoresToSink.front()->getParent();
3837 VPBasicBlock *LastBB = StoresToSink.back()->getParent();
3838 SinkStoreInfo SinkInfo(StoresToSink, *StoresToSink[0], PSE, L);
3839 return canHoistOrSinkWithNoAliasCheck(*StoreLoc, FirstBB, LastBB, SinkInfo);
3840}
3841
3844 const Loop *L) {
3845 auto Groups =
3847 if (Groups.empty())
3848 return;
3849
3850 for (auto &Group : Groups) {
3851 if (!canSinkStoreWithNoAliasCheck(Group, PSE, *L))
3852 continue;
3853
3854 // Use the last (most dominated) store's location for the unconditional
3855 // store.
3856 VPReplicateRecipe *LastStore = Group.back();
3857 VPBasicBlock *InsertBB = LastStore->getParent();
3858
3859 // Collect common alias metadata from all stores in the group.
3860 VPIRMetadata CommonMetadata = getCommonMetadata(Group);
3861
3862 // Build select chain for stored values.
3863 VPValue *SelectedValue = Group[0]->getOperand(0);
3864 VPBuilder Builder(InsertBB, LastStore->getIterator());
3865
3866 bool IsSingleScalar = Group[0]->isSingleScalar();
3867 for (unsigned I = 1; I < Group.size(); ++I) {
3868 assert(IsSingleScalar == Group[I]->isSingleScalar() &&
3869 "all members in group must agree on IsSingleScalar");
3870 VPValue *Mask = Group[I]->getMask();
3871 VPValue *Value = Group[I]->getOperand(0);
3872 SelectedValue = Builder.createSelect(
3873 Mask, Value, SelectedValue, Group[I]->getDebugLoc(), "",
3874 VPIRFlags::getDefaultFlags(Instruction::Select,
3875 Value->getScalarType()));
3876 }
3877
3878 // Find the store with minimum alignment to use.
3879 auto *StoreWithMinAlign = findRecipeWithMinAlign<StoreInst>(Group);
3880
3881 // Create unconditional store with selected value and common metadata.
3882 auto *UnpredicatedStore = new VPReplicateRecipe(
3883 StoreWithMinAlign->getUnderlyingInstr(),
3884 {SelectedValue, LastStore->getOperand(1)}, IsSingleScalar,
3885 /*Mask=*/nullptr, *LastStore, CommonMetadata);
3886 UnpredicatedStore->insertBefore(*InsertBB, LastStore->getIterator());
3887
3888 // Remove all predicated stores from the group.
3889 for (VPReplicateRecipe *Store : Group)
3890 Store->eraseFromParent();
3891 }
3892}
3893
3894/// Returns true if \p V is VPWidenLoadRecipe or VPInterleaveRecipe that can be
3895/// converted to a narrower recipe. \p V is used by a wide recipe that feeds a
3896/// store interleave group at index \p Idx, \p WideMember0 is the recipe feeding
3897/// the same interleave group at index 0. A VPWidenLoadRecipe can be narrowed to
3898/// an index-independent load if it feeds all wide ops at all indices (\p OpV
3899/// must be the operand at index \p OpIdx for both the recipe at lane 0, \p
3900/// WideMember0). A VPInterleaveRecipe can be narrowed to a wide load, if \p V
3901/// is defined at \p Idx of a load interleave group.
3902/// A live-in or recipe defined outside the loop region can be converted, if it
3903/// is the same across all lanes, or we can create a BuildVector for it.
3904static bool canNarrowLoad(VPSingleDefRecipe *WideMember0, unsigned OpIdx,
3905 VPValue *OpV, unsigned Idx, bool IsScalable) {
3906 VPValue *Member0Op = WideMember0->getOperand(OpIdx);
3907 if (Member0Op->isDefinedOutsideLoopRegions()) {
3908 // Operand matches Member0, broadcast across all fields for both live-ins
3909 // and recipes.
3910 if (Member0Op == OpV)
3911 return true;
3912 // Otherwise distinct per-field VPValues are assembled into a BuildVector.
3913 return !IsScalable && OpV->isDefinedOutsideLoopRegions() &&
3914 OpV->getScalarType() == Member0Op->getScalarType();
3915 }
3916 VPRecipeBase *Member0OpR = Member0Op->getDefiningRecipe();
3917 if (auto *W = dyn_cast<VPWidenLoadRecipe>(Member0OpR))
3918 // For scalable VFs, the narrowed plan processes vscale iterations at once,
3919 // so a shared wide load cannot be narrowed to a uniform scalar; bail out.
3920 return !IsScalable && !W->getMask() && W->isConsecutive() &&
3921 Member0Op == OpV;
3922 if (auto *IR = dyn_cast<VPInterleaveRecipe>(Member0OpR))
3923 return IR->getInterleaveGroup()->isFull() && IR->getVPValue(Idx) == OpV;
3924 return false;
3925}
3926
3927static bool canNarrowOps(ArrayRef<VPValue *> Ops, bool IsScalable) {
3929 auto *WideMember0 = dyn_cast<VPRecipeWithIRFlags>(Ops[0]);
3930 if (!WideMember0)
3931 return false;
3932 for (VPValue *V : Ops) {
3934 return false;
3935 auto *R = cast<VPRecipeWithIRFlags>(V);
3936 if (vputils::getOpcode(R) != vputils::getOpcode(WideMember0))
3937 return false;
3938 if (R->getScalarType() != WideMember0->getScalarType())
3939 return false;
3940 if (R->hasPredicate() && R->getPredicate() != WideMember0->getPredicate())
3941 return false;
3942 }
3943
3944 for (unsigned Idx = 0; Idx != WideMember0->getNumOperands(); ++Idx) {
3946 for (VPValue *Op : Ops)
3947 OpsI.push_back(Op->getDefiningRecipe()->getOperand(Idx));
3948
3949 if (canNarrowOps(OpsI, IsScalable))
3950 continue;
3951
3952 if (any_of(enumerate(OpsI), [WideMember0, Idx, IsScalable](const auto &P) {
3953 const auto &[OpIdx, OpV] = P;
3954 return !canNarrowLoad(WideMember0, Idx, OpV, OpIdx, IsScalable);
3955 }))
3956 return false;
3957 }
3958
3959 return true;
3960}
3961
3962/// Returns VF from \p VFs if \p IR is a full interleave group with factor and
3963/// number of members both equal to VF. The interleave group must also access
3964/// the full vector width.
3965static std::optional<ElementCount>
3968 const TargetTransformInfo &TTI) {
3969 if (!InterleaveR || InterleaveR->getMask())
3970 return std::nullopt;
3971
3972 Type *GroupElementTy = nullptr;
3973 if (InterleaveR->getStoredValues().empty()) {
3974 GroupElementTy = InterleaveR->getVPValue(0)->getScalarType();
3975 if (!all_of(InterleaveR->definedValues(), [GroupElementTy](VPValue *Op) {
3976 return Op->getScalarType() == GroupElementTy;
3977 }))
3978 return std::nullopt;
3979 } else {
3980 GroupElementTy = InterleaveR->getStoredValues()[0]->getScalarType();
3981 if (!all_of(InterleaveR->getStoredValues(), [GroupElementTy](VPValue *Op) {
3982 return Op->getScalarType() == GroupElementTy;
3983 }))
3984 return std::nullopt;
3985 }
3986
3987 auto IG = InterleaveR->getInterleaveGroup();
3988 if (IG->getFactor() != IG->getNumMembers())
3989 return std::nullopt;
3990
3991 auto GetVectorBitWidthForVF = [&TTI](ElementCount VF) {
3992 TypeSize Size = TTI.getRegisterBitWidth(
3995 assert(Size.isScalable() == VF.isScalable() &&
3996 "if Size is scalable, VF must be scalable and vice versa");
3997 return Size.getKnownMinValue();
3998 };
3999
4000 for (ElementCount VF : VFs) {
4001 unsigned MinVal = VF.getKnownMinValue();
4002 unsigned GroupSize = GroupElementTy->getScalarSizeInBits() * MinVal;
4003 if (IG->getFactor() == MinVal && GroupSize == GetVectorBitWidthForVF(VF))
4004 return {VF};
4005 }
4006 return std::nullopt;
4007}
4008
4009/// Returns true if \p VPValue is a narrow VPValue.
4010static bool isAlreadyNarrow(VPValue *VPV) {
4011 if (isa<VPIRValue>(VPV))
4012 return true;
4013 auto *RepR = dyn_cast<VPReplicateRecipe>(VPV);
4014 return RepR && RepR->isSingleScalar();
4015}
4016
4017// Convert the wide recipes defining the VPValues in \p Members feeding an
4018// interleave group to a single narrow variant. The first member is reused as
4019// the narrowed recipe. BuildVectors for live-in operands are inserted into \p
4020// Preheader.
4022 SmallPtrSetImpl<VPValue *> &NarrowedOps,
4023 VPBasicBlock *Preheader) {
4024 VPValue *V = Members.front();
4025 if (NarrowedOps.contains(V))
4026 return V;
4027
4028 if (V->isDefinedOutsideLoopRegions()) {
4029 assert(all_of(Members,
4030 [V](VPValue *M) {
4031 return M->isDefinedOutsideLoopRegions() &&
4032 M->getScalarType() == V->getScalarType();
4033 }) &&
4034 "expected distinct loop-invariant values of matching scalar type");
4035 auto *BV = new VPInstruction(VPInstruction::BuildVector, Members);
4036 Preheader->appendRecipe(BV);
4037 NarrowedOps.insert(BV);
4038 return BV;
4039 }
4040
4041 if (isAlreadyNarrow(V))
4042 return V;
4043
4044 VPRecipeBase *R = V->getDefiningRecipe();
4046 auto *WideMember0 = cast<VPRecipeWithIRFlags>(R);
4047 for (VPValue *Member : Members.drop_front())
4048 WideMember0->intersectFlags(*cast<VPRecipeWithIRFlags>(Member));
4049 for (unsigned Idx = 0, E = WideMember0->getNumOperands(); Idx != E; ++Idx) {
4051 for (VPValue *Member : Members)
4052 OpsI.push_back(Member->getDefiningRecipe()->getOperand(Idx));
4053 WideMember0->setOperand(
4054 Idx, narrowInterleaveGroupOp(OpsI, NarrowedOps, Preheader));
4055 }
4056 return V;
4057 }
4058
4059 if (auto *LoadGroup = dyn_cast<VPInterleaveRecipe>(R)) {
4060 // Narrow interleave group to wide load, as transformed VPlan will only
4061 // process one original iteration.
4062 auto *LI = cast<LoadInst>(LoadGroup->getInterleaveGroup()->getInsertPos());
4063 auto *L = VPBuilder(LoadGroup).createWidenLoad(
4064 *LI, LoadGroup->getAddr(), LoadGroup->getMask(), /*Consecutive=*/true,
4065 *LoadGroup, LoadGroup->getDebugLoc());
4066 NarrowedOps.insert(L);
4067 return L;
4068 }
4069
4070 if (auto *RepR = dyn_cast<VPReplicateRecipe>(R)) {
4071 assert(RepR->isSingleScalar() && RepR->getOpcode() == Instruction::Load &&
4072 "must be a single scalar load");
4073 NarrowedOps.insert(RepR);
4074 return RepR;
4075 }
4076
4077 auto *WideLoad = cast<VPWidenLoadRecipe>(R);
4078 VPValue *PtrOp = WideLoad->getAddr();
4079 if (auto *VecPtr = dyn_cast<VPVectorPointerRecipe>(PtrOp))
4080 PtrOp = VecPtr->getOperand(0);
4081 // Narrow wide load to uniform scalar load, as transformed VPlan will only
4082 // process one original iteration.
4083 auto *N = new VPReplicateRecipe(&WideLoad->getIngredient(), {PtrOp},
4084 /*IsUniform*/ true,
4085 /*Mask*/ nullptr, {}, *WideLoad);
4086 N->insertBefore(WideLoad);
4087 NarrowedOps.insert(N);
4088 return N;
4089}
4090
4091std::unique_ptr<VPlan>
4093 const TargetTransformInfo &TTI) {
4094 VPRegionBlock *VectorLoop = Plan.getVectorLoopRegion();
4095
4096 if (!VectorLoop)
4097 return nullptr;
4098
4099 // Only handle single-block loops for now.
4100 if (VectorLoop->getEntryBasicBlock() != VectorLoop->getExitingBasicBlock())
4101 return nullptr;
4102
4103 // Skip plans when we may not be able to properly narrow.
4104 VPBasicBlock *Exiting = VectorLoop->getExitingBasicBlock();
4105 if (!match(&Exiting->back(), m_BranchOnCount()))
4106 return nullptr;
4107
4108 assert(match(&Exiting->back(),
4110 m_Specific(&Plan.getVectorTripCount()))) &&
4111 "unexpected branch-on-count");
4112
4114 std::optional<ElementCount> VFToOptimize;
4115 for (auto &R : *VectorLoop->getEntryBasicBlock()) {
4118 continue;
4119
4120 // Bail out on recipes not supported at the moment:
4121 // * phi recipes other than the canonical induction
4122 // * recipes writing to memory except interleave groups
4123 // Only support plans with a canonical induction phi.
4124 if (R.isPhi())
4125 return nullptr;
4126
4127 auto *InterleaveR = dyn_cast<VPInterleaveRecipe>(&R);
4128 if (R.mayWriteToMemory() && !InterleaveR)
4129 return nullptr;
4130
4131 // Bail out if any recipe defines a vector value used outside the
4132 // vector loop region.
4133 if (any_of(R.definedValues(), [&](VPValue *V) {
4134 return any_of(V->users(), [&](VPUser *U) {
4135 auto *UR = cast<VPRecipeBase>(U);
4136 return UR->getParent()->getParent() != VectorLoop;
4137 });
4138 }))
4139 return nullptr;
4140
4141 // All other ops are allowed, but we reject uses that cannot be converted
4142 // when checking all allowed consumers (store interleave groups) below.
4143 if (!InterleaveR)
4144 continue;
4145
4146 // Try to find a single VF, where all interleave groups are consecutive and
4147 // saturate the full vector width. If we already have a candidate VF, check
4148 // if it is applicable for the current InterleaveR, otherwise look for a
4149 // suitable VF across the Plan's VFs.
4151 VFToOptimize ? SmallVector<ElementCount>({*VFToOptimize})
4152 : to_vector(Plan.vectorFactors());
4153 std::optional<ElementCount> NarrowedVF =
4154 isConsecutiveInterleaveGroup(InterleaveR, VFs, TTI);
4155 if (!NarrowedVF || (VFToOptimize && NarrowedVF != VFToOptimize))
4156 return nullptr;
4157 VFToOptimize = NarrowedVF;
4158
4159 // Skip read interleave groups.
4160 if (InterleaveR->getStoredValues().empty())
4161 continue;
4162
4163 // Narrow interleave groups, if all operands are already matching narrow
4164 // ops.
4165 auto *Member0 = InterleaveR->getStoredValues()[0];
4166 if (isAlreadyNarrow(Member0) &&
4167 all_of(InterleaveR->getStoredValues(), equal_to(Member0))) {
4168 StoreGroups.push_back(InterleaveR);
4169 continue;
4170 }
4171
4172 // For now, we only support full interleave groups storing load interleave
4173 // groups.
4174 if (all_of(enumerate(InterleaveR->getStoredValues()), [](auto Op) {
4175 VPRecipeBase *DefR = Op.value()->getDefiningRecipe();
4176 if (!DefR)
4177 return false;
4178 auto *IR = dyn_cast<VPInterleaveRecipe>(DefR);
4179 return IR && IR->getInterleaveGroup()->isFull() &&
4180 IR->getVPValue(Op.index()) == Op.value();
4181 })) {
4182 StoreGroups.push_back(InterleaveR);
4183 continue;
4184 }
4185
4186 // Check if all values feeding InterleaveR are matching wide recipes, which
4187 // operands that can be narrowed.
4188 if (!canNarrowOps(InterleaveR->getStoredValues(),
4189 VFToOptimize->isScalable()))
4190 return nullptr;
4191 StoreGroups.push_back(InterleaveR);
4192 }
4193
4194 if (StoreGroups.empty())
4195 return nullptr;
4196
4197 VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
4198 bool RequiresScalarEpilogue =
4199 MiddleVPBB->getNumSuccessors() == 1 &&
4200 MiddleVPBB->getSingleSuccessor() == Plan.getScalarPreheader();
4201 // Bail out for tail-folding (middle block with a single successor to exit).
4202 if (MiddleVPBB->getNumSuccessors() != 2 && !RequiresScalarEpilogue)
4203 return nullptr;
4204
4205 // All interleave groups in Plan can be narrowed for VFToOptimize. Split the
4206 // original Plan into 2: a) a new clone which contains all VFs of Plan, except
4207 // VFToOptimize, and b) the original Plan with VFToOptimize as single VF.
4208 // TODO: Handle cases where only some interleave groups can be narrowed.
4209 std::unique_ptr<VPlan> NewPlan;
4210 if (size(Plan.vectorFactors()) != 1) {
4211 NewPlan = std::unique_ptr<VPlan>(Plan.duplicate());
4212 Plan.setVF(*VFToOptimize);
4213 NewPlan->removeVF(*VFToOptimize);
4214 }
4215
4216 // Convert InterleaveGroup \p R to a single VPWidenLoadRecipe.
4217 SmallPtrSet<VPValue *, 4> NarrowedOps;
4218 VPBasicBlock *Preheader = Plan.getVectorPreheader();
4219 // Narrow operation tree rooted at store groups.
4220 for (auto *StoreGroup : StoreGroups) {
4221 VPValue *Res = narrowInterleaveGroupOp(StoreGroup->getStoredValues(),
4222 NarrowedOps, Preheader);
4223 auto *SI =
4224 cast<StoreInst>(StoreGroup->getInterleaveGroup()->getInsertPos());
4225 VPBuilder(StoreGroup)
4226 .createWidenStore(*SI, StoreGroup->getAddr(), Res, nullptr,
4227 /*Consecutive=*/true, *StoreGroup,
4228 StoreGroup->getDebugLoc());
4229 StoreGroup->eraseFromParent();
4230 }
4231
4232 // Adjust induction to reflect that the transformed plan only processes one
4233 // original iteration.
4235 Type *CanIVTy = VectorLoop->getCanonicalIVType();
4236 VPBasicBlock *VectorPH = Plan.getVectorPreheader();
4237 VPBuilder PHBuilder(VectorPH, VectorPH->begin());
4238
4239 VPValue *UF = &Plan.getUF();
4240 VPValue *Step;
4241 if (VFToOptimize->isScalable()) {
4242 VPValue *VScale =
4243 PHBuilder.createElementCount(CanIVTy, ElementCount::getScalable(1));
4244 Step = PHBuilder.createOverflowingOp(Instruction::Mul, {VScale, UF},
4245 {true, false});
4246 Plan.getVF().replaceAllUsesWith(VScale);
4247 } else {
4248 Step = UF;
4249 Plan.getVF().replaceAllUsesWith(Plan.getConstantInt(CanIVTy, 1));
4250 }
4251 // Materialize vector trip count with the narrowed step.
4252 materializeVectorTripCount(Plan, VectorPH, /*TailByMasking=*/false,
4253 RequiresScalarEpilogue, Step);
4254
4255 CanIVInc->setOperand(1, Step);
4256 Plan.getVFxUF().replaceAllUsesWith(Step);
4257
4258 removeDeadRecipes(Plan);
4259 assert(none_of(*VectorLoop->getEntryBasicBlock(),
4261 "All VPVectorPointerRecipes should have been removed");
4262 return NewPlan;
4263}
4264
4266 VFRange &Range) {
4267 VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
4268 auto *MiddleVPBB = Plan.getMiddleBlock();
4269 VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
4270
4271 auto IsScalableOne = [](ElementCount VF) -> bool {
4272 return VF == ElementCount::getScalable(1);
4273 };
4274
4275 for (auto &HeaderPhi : VectorRegion->getEntryBasicBlock()->phis()) {
4276 auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&HeaderPhi);
4277 if (!FOR)
4278 continue;
4279
4280 assert(VectorRegion->getSingleSuccessor() == Plan.getMiddleBlock() &&
4281 "Cannot handle loops with uncountable early exits");
4282
4283 // Find the existing splice for this FOR, created in
4284 // createHeaderPhiRecipes. All uses of FOR have already been replaced with
4285 // RecurSplice there; only RecurSplice itself still references FOR.
4286 auto *RecurSplice =
4288 assert(RecurSplice && "expected FirstOrderRecurrenceSplice");
4289
4290 // For VF vscale x 1, if vscale = 1, we are unable to extract the
4291 // penultimate value of the recurrence. Instead we rely on the existing
4292 // extract of the last element from the result of
4293 // VPInstruction::FirstOrderRecurrenceSplice.
4294 // TODO: Consider vscale_range info and UF.
4295 if (any_of(RecurSplice->users(),
4296 [](VPUser *U) { return !cast<VPRecipeBase>(U)->getRegion(); }) &&
4298 Range))
4299 return;
4300
4301 // This is the second phase of vectorizing first-order recurrences, creating
4302 // extracts for users outside the loop. An overview of the transformation is
4303 // described below. Suppose we have the following loop with some use after
4304 // the loop of the last a[i-1],
4305 //
4306 // for (int i = 0; i < n; ++i) {
4307 // t = a[i - 1];
4308 // b[i] = a[i] - t;
4309 // }
4310 // use t;
4311 //
4312 // There is a first-order recurrence on "a". For this loop, the shorthand
4313 // scalar IR looks like:
4314 //
4315 // scalar.ph:
4316 // s.init = a[-1]
4317 // br scalar.body
4318 //
4319 // scalar.body:
4320 // i = phi [0, scalar.ph], [i+1, scalar.body]
4321 // s1 = phi [s.init, scalar.ph], [s2, scalar.body]
4322 // s2 = a[i]
4323 // b[i] = s2 - s1
4324 // br cond, scalar.body, exit.block
4325 //
4326 // exit.block:
4327 // use = lcssa.phi [s1, scalar.body]
4328 //
4329 // In this example, s1 is a recurrence because it's value depends on the
4330 // previous iteration. In the first phase of vectorization, we created a
4331 // VPFirstOrderRecurrencePHIRecipe v1 for s1. Now we create the extracts
4332 // for users in the scalar preheader and exit block.
4333 //
4334 // vector.ph:
4335 // v_init = vector(..., ..., ..., a[-1])
4336 // br vector.body
4337 //
4338 // vector.body
4339 // i = phi [0, vector.ph], [i+4, vector.body]
4340 // v1 = phi [v_init, vector.ph], [v2, vector.body]
4341 // v2 = a[i, i+1, i+2, i+3]
4342 // v1' = splice(v1(3), v2(0, 1, 2))
4343 // b[i, i+1, i+2, i+3] = v2 - v1'
4344 // br cond, vector.body, middle.block
4345 //
4346 // middle.block:
4347 // vector.recur.extract.for.phi = v2(2)
4348 // vector.recur.extract = v2(3)
4349 // br cond, scalar.ph, exit.block
4350 //
4351 // scalar.ph:
4352 // scalar.recur.init = phi [vector.recur.extract, middle.block],
4353 // [s.init, otherwise]
4354 // br scalar.body
4355 //
4356 // scalar.body:
4357 // i = phi [0, scalar.ph], [i+1, scalar.body]
4358 // s1 = phi [scalar.recur.init, scalar.ph], [s2, scalar.body]
4359 // s2 = a[i]
4360 // b[i] = s2 - s1
4361 // br cond, scalar.body, exit.block
4362 //
4363 // exit.block:
4364 // lo = lcssa.phi [s1, scalar.body],
4365 // [vector.recur.extract.for.phi, middle.block]
4366 //
4367 // Update extracts of the splice in the middle block: they extract the
4368 // penultimate element of the recurrence.
4370 make_range(MiddleVPBB->getFirstNonPhi(), MiddleVPBB->end()))) {
4371 if (!match(&R, m_ExtractLastLaneOfLastPart(m_Specific(RecurSplice))))
4372 continue;
4373
4374 auto *ExtractR = cast<VPInstruction>(&R);
4375 VPValue *PenultimateElement = MiddleBuilder.createNaryOp(
4376 VPInstruction::ExtractPenultimateElement, RecurSplice->getOperand(1),
4377 {}, "vector.recur.extract.for.phi");
4378 for (VPUser *ExitU : to_vector(ExtractR->users())) {
4379 if (auto *ExitPhi = dyn_cast<VPIRPhi>(ExitU))
4380 ExitPhi->replaceUsesOfWith(ExtractR, PenultimateElement);
4381 }
4382 }
4383 }
4384}
4385
4386/// Check if \p V is a binary expression of a widened IV and a loop-invariant
4387/// value. Returns the widened IV if found, nullptr otherwise.
4389 auto *BinOp = dyn_cast<VPWidenRecipe>(V);
4390 if (!BinOp || !Instruction::isBinaryOp(BinOp->getOpcode()) ||
4391 Instruction::isIntDivRem(BinOp->getOpcode()))
4392 return nullptr;
4393
4394 VPValue *WidenIVCandidate = BinOp->getOperand(0);
4395 VPValue *InvariantCandidate = BinOp->getOperand(1);
4396 if (!isa<VPWidenIntOrFpInductionRecipe>(WidenIVCandidate))
4397 std::swap(WidenIVCandidate, InvariantCandidate);
4398
4399 if (!InvariantCandidate->isDefinedOutsideLoopRegions())
4400 return nullptr;
4401
4402 return dyn_cast<VPWidenIntOrFpInductionRecipe>(WidenIVCandidate);
4403}
4404
4405/// Create a scalar version of \p BinOp, with its \p WidenIV operand replaced
4406/// by \p ScalarIV, and place it after \p ScalarIV's defining recipe.
4410 BinOp->getNumOperands() == 2 && "BinOp must have 2 operands");
4411 auto *ClonedOp = BinOp->clone();
4412 if (ClonedOp->getOperand(0) == WidenIV) {
4413 ClonedOp->setOperand(0, ScalarIV);
4414 } else {
4415 assert(ClonedOp->getOperand(1) == WidenIV && "one operand must be WideIV");
4416 ClonedOp->setOperand(1, ScalarIV);
4417 }
4418 ClonedOp->insertAfter(ScalarIV->getDefiningRecipe());
4419 return ClonedOp;
4420}
4421
4422/// If \p S is an affine AddRec, returns true if its step is known to be
4423/// positive and false if it is known to be negative. Returns std::nullopt if
4424/// \p S is not an affine AddRec, or if the sign of its step cannot be
4425/// determined.
4426static std::optional<bool> getStepDirection(const SCEV *S,
4427 ScalarEvolution &SE) {
4428 const SCEV *Step;
4429 if (!match(S, m_scev_AffineAddRec(m_SCEV(), m_SCEV(Step))))
4430 return std::nullopt;
4431 if (SE.isKnownPositive(Step))
4432 return true;
4433 if (SE.isKnownNegative(Step))
4434 return false;
4435 return std::nullopt;
4436}
4437
4440 Loop &L) {
4441 ScalarEvolution &SE = *PSE.getSE();
4442 VPRegionBlock *VectorLoopRegion = Plan.getVectorLoopRegion();
4443
4444 // Helper lambda to check if the IV range excludes the sentinel value. Try
4445 // signed first, then unsigned. Return an excluded sentinel if found,
4446 // otherwise return std::nullopt.
4447 auto CheckSentinel = [&SE](const SCEV *IVSCEV,
4448 bool UseMax) -> std::optional<APSInt> {
4449 unsigned BW = IVSCEV->getType()->getScalarSizeInBits();
4450 for (bool Signed : {true, false}) {
4451 APSInt Sentinel = UseMax ? APSInt::getMinValue(BW, /*Unsigned=*/!Signed)
4452 : APSInt::getMaxValue(BW, /*Unsigned=*/!Signed);
4453
4454 ConstantRange IVRange =
4455 Signed ? SE.getSignedRange(IVSCEV) : SE.getUnsignedRange(IVSCEV);
4456 if (!IVRange.contains(Sentinel))
4457 return Sentinel;
4458 }
4459 return std::nullopt;
4460 };
4461
4462 VPValue *HeaderMask = VectorLoopRegion->getHeaderMask();
4463 for (VPRecipeBase &Phi :
4464 make_early_inc_range(VectorLoopRegion->getEntryBasicBlock()->phis())) {
4465 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&Phi);
4467 PhiR->getRecurrenceKind()))
4468 continue;
4469
4470 Type *PhiTy = PhiR->getScalarType();
4471 if (PhiTy->isPointerTy() || PhiTy->isFloatingPointTy())
4472 continue;
4473
4474 // If there's a header mask, the backedge select will not be the find-last
4475 // select.
4476 VPValue *BackedgeVal = PhiR->getBackedgeValue();
4477 auto *FindLastSelect = cast<VPSingleDefRecipe>(BackedgeVal);
4478 if (HeaderMask &&
4479 !match(BackedgeVal,
4480 m_Select(m_Specific(HeaderMask),
4481 m_VPSingleDefRecipe(FindLastSelect), m_Specific(PhiR))))
4482 continue;
4483
4484 // Get the find-last expression from the find-last select of the reduction
4485 // phi. The find-last select should be a select between the phi and the
4486 // find-last expression.
4487 VPValue *Cond, *FindLastExpression;
4488 if (!match(FindLastSelect, m_SelectLike(m_VPValue(Cond), m_Specific(PhiR),
4489 m_VPValue(FindLastExpression))) &&
4490 !match(FindLastSelect,
4491 m_SelectLike(m_VPValue(Cond), m_VPValue(FindLastExpression),
4492 m_Specific(PhiR))))
4493 continue;
4494
4495 // Check if FindLastExpression is a simple expression of a widened IV. If
4496 // so, we can track the underlying IV instead and sink the expression.
4497 auto *IVOfExpressionToSink = getExpressionIV(FindLastExpression);
4498 const SCEV *IVSCEV = vputils::getSCEVExprForVPValue(
4499 IVOfExpressionToSink ? IVOfExpressionToSink : FindLastExpression, PSE,
4500 &L);
4501 if (!match(IVSCEV, m_scev_AffineAddRec(m_SCEV(), m_SCEV()))) {
4502 assert(!match(vputils::getSCEVExprForVPValue(FindLastExpression, PSE, &L),
4504 "IVOfExpressionToSink not being an AddRec must imply "
4505 "FindLastExpression not being an AddRec.");
4506 continue;
4507 }
4508
4509 // Determine direction from the step of IVSCEV, if possible.
4510 std::optional<bool> StepDirection = getStepDirection(IVSCEV, SE);
4511 if (!StepDirection)
4512 continue;
4513
4514 bool UseMax = *StepDirection;
4515 std::optional<APSInt> SentinelVal = CheckSentinel(IVSCEV, UseMax);
4516 bool UseSigned = SentinelVal && SentinelVal->isSigned();
4517
4518 // Sinking an expression will disable epilogue vectorization. Only use it,
4519 // if FindLastExpression cannot be vectorized via a sentinel. Sinking may
4520 // also prevent vectorizing using a sentinel (e.g., if the expression is a
4521 // multiply or divide by large constant, respectively), which also makes
4522 // sinking undesirable.
4523 if (IVOfExpressionToSink) {
4524 const SCEV *FindLastExpressionSCEV =
4525 vputils::getSCEVExprForVPValue(FindLastExpression, PSE, &L);
4526 if (std::optional<bool> NewUseMax =
4527 getStepDirection(FindLastExpressionSCEV, SE)) {
4528 if (auto NewSentinel =
4529 CheckSentinel(FindLastExpressionSCEV, *NewUseMax)) {
4530 // The original expression already has a sentinel, so prefer not
4531 // sinking to keep epilogue vectorization possible.
4532 SentinelVal = *NewSentinel;
4533 UseSigned = NewSentinel->isSigned();
4534 UseMax = *NewUseMax;
4535 IVSCEV = FindLastExpressionSCEV;
4536 IVOfExpressionToSink = nullptr;
4537 }
4538 }
4539 }
4540
4541 // If no sentinel was found, fall back to a boolean AnyOf reduction to track
4542 // if the condition was ever true. Requires the IV to not wrap, otherwise we
4543 // cannot use min/max.
4544 if (!SentinelVal) {
4545 auto *AR = cast<SCEVAddRecExpr>(IVSCEV);
4546 if (AR->hasNoSignedWrap())
4547 UseSigned = true;
4548 else if (AR->hasNoUnsignedWrap())
4549 UseSigned = false;
4550 else
4551 continue;
4552 }
4553
4555 BackedgeVal,
4557
4558 VPValue *NewFindLastSelect = BackedgeVal;
4559 VPValue *SelectCond = Cond;
4560 if (!SentinelVal || IVOfExpressionToSink) {
4561 // When we need to create a new select, normalize the condition so that
4562 // PhiR is the last operand and include the header mask if needed.
4563 DebugLoc DL = FindLastSelect->getDefiningRecipe()->getDebugLoc();
4564 VPBuilder LoopBuilder(FindLastSelect->getDefiningRecipe());
4565 if (match(FindLastSelect,
4567 SelectCond = LoopBuilder.createNot(SelectCond);
4568
4569 // When tail folding, mask the condition with the header mask to prevent
4570 // propagating poison from inactive lanes in the last vector iteration.
4571 if (HeaderMask)
4572 SelectCond = LoopBuilder.createLogicalAnd(HeaderMask, SelectCond);
4573
4574 if (SelectCond != Cond || IVOfExpressionToSink) {
4575 NewFindLastSelect = LoopBuilder.createSelect(
4576 SelectCond,
4577 IVOfExpressionToSink ? IVOfExpressionToSink : FindLastExpression,
4578 PhiR, DL);
4579 }
4580 }
4581
4582 // Create the reduction result in the middle block using sentinel directly.
4583 RecurKind MinMaxKind =
4584 UseMax ? (UseSigned ? RecurKind::SMax : RecurKind::UMax)
4585 : (UseSigned ? RecurKind::SMin : RecurKind::UMin);
4586 VPIRFlags Flags(MinMaxKind, /*IsOrdered=*/false, /*IsInLoop=*/false,
4587 FastMathFlags());
4588 DebugLoc ExitDL = RdxResult->getDebugLoc();
4589 VPBuilder MiddleBuilder(RdxResult);
4590 VPValue *ReducedIV =
4592 NewFindLastSelect, Flags, ExitDL);
4593
4594 // If IVOfExpressionToSink is an expression to sink, sink it now.
4595 VPValue *VectorRegionExitingVal = ReducedIV;
4596 if (IVOfExpressionToSink)
4597 VectorRegionExitingVal =
4598 cloneBinOpForScalarIV(cast<VPWidenRecipe>(FindLastExpression),
4599 ReducedIV, IVOfExpressionToSink);
4600
4601 VPValue *NewRdxResult;
4602 VPValue *StartVPV = PhiR->getStartValue();
4603 if (SentinelVal) {
4604 // Sentinel-based approach: reduce IVs with min/max, compare against
4605 // sentinel to detect if condition was ever true, select accordingly.
4606 VPValue *Sentinel = Plan.getConstantInt(*SentinelVal);
4607 auto *Cmp = MiddleBuilder.createICmp(CmpInst::ICMP_NE, ReducedIV,
4608 Sentinel, ExitDL);
4609 NewRdxResult = MiddleBuilder.createSelect(Cmp, VectorRegionExitingVal,
4610 StartVPV, ExitDL);
4611 StartVPV = Sentinel;
4612 } else {
4613 // Introduce a boolean AnyOf reduction to track if the condition was ever
4614 // true in the loop. Use it to select the initial start value, if it was
4615 // never true.
4616 auto *AnyOfPhi = new VPReductionPHIRecipe(
4617 /*Phi=*/nullptr, RecurKind::Or, *Plan.getFalse(), *Plan.getFalse(),
4618 RdxUnordered{1}, {}, /*HasUsesOutsideReductionChain=*/false);
4619 AnyOfPhi->insertAfter(PhiR);
4620
4621 VPBuilder LoopBuilder(BackedgeVal->getDefiningRecipe());
4622 VPValue *OrVal = LoopBuilder.createOr(AnyOfPhi, SelectCond);
4623 AnyOfPhi->setOperand(1, OrVal);
4624
4625 NewRdxResult = MiddleBuilder.createAnyOfReduction(
4626 OrVal, VectorRegionExitingVal, StartVPV, ExitDL);
4627
4628 // Initialize the IV reduction phi with the neutral element, not the
4629 // original start value, to ensure correct min/max reduction results.
4630 StartVPV = Plan.getOrAddLiveIn(
4631 getRecurrenceIdentity(MinMaxKind, IVSCEV->getType(), {}));
4632 }
4633 RdxResult->replaceAllUsesWith(NewRdxResult);
4634 RdxResult->eraseFromParent();
4635
4636 auto *NewPhiR = new VPReductionPHIRecipe(
4637 cast<PHINode>(PhiR->getUnderlyingInstr()), RecurKind::FindIV, *StartVPV,
4638 *NewFindLastSelect, RdxUnordered{1}, {},
4639 PhiR->hasUsesOutsideReductionChain());
4640 NewPhiR->insertBefore(PhiR);
4641 PhiR->replaceAllUsesWith(NewPhiR);
4642 PhiR->eraseFromParent();
4643 }
4644}
4645
4646namespace {
4647
4648using ExtendKind = TTI::PartialReductionExtendKind;
4649struct ReductionExtend {
4650 Type *SrcType = nullptr;
4651 ExtendKind Kind = ExtendKind::PR_None;
4652};
4653
4654/// Describes the extends used to compute the extended reduction operand.
4655/// ExtendB is optional. If ExtendB is present, ExtendsUser is a binary
4656/// operation.
4657struct ExtendedReductionOperand {
4658 /// The recipe that consumes the extends.
4659 VPWidenRecipe *ExtendsUser = nullptr;
4660 /// Extend descriptions (inputs to getPartialReductionCost).
4661 ReductionExtend ExtendA, ExtendB;
4662};
4663
4664/// A chain of recipes that form a partial reduction. Matches either
4665/// reduction_bin_op (extended op, accumulator), or
4666/// reduction_bin_op (accumulator, extended op).
4667/// The possible forms of the "extended op" are listed in
4668/// matchExtendedReductionOperand.
4669struct VPPartialReductionChain {
4670 /// The top-level binary operation that forms the reduction to a scalar
4671 /// after the loop body.
4672 VPWidenRecipe *ReductionBinOp = nullptr;
4673 /// The user of the extends that is then reduced.
4674 ExtendedReductionOperand ExtendedOp;
4675 /// The recurrence kind for the entire partial reduction chain.
4676 /// This allows distinguishing between Sub and AddWithSub recurrences,
4677 /// when the ReductionBinOp is a Instruction::Sub.
4678 RecurKind RK;
4679 /// The index of the accumulator operand of ReductionBinOp. The extended op
4680 /// is `1 - AccumulatorOpIdx`.
4681 unsigned AccumulatorOpIdx;
4682 unsigned ScaleFactor;
4683 /// Optional blend to represent predication for the block that updates the
4684 /// reduction.
4685 VPBlendRecipe *Blend = nullptr;
4686};
4687
4688// Return the incoming index of the single-use value in the blend, which is
4689// expected to be the predicated reduction update.
4690static std::optional<unsigned>
4691getBlendReductionUpdateValueIdx(VPBlendRecipe *Blend) {
4692 assert(Blend && !Blend->isNormalized() &&
4693 Blend->getNumIncomingValues() == 2 &&
4694 "Expected a non-normalized blend with two incoming values");
4695 bool FirstIncomingHasOneUse = Blend->getIncomingValue(0)->hasOneUse();
4696
4697 // Only the update value should have one use (the blend). The previous
4698 // value should always have at least two uses, the blend and the reduction.
4699 if (FirstIncomingHasOneUse == Blend->getIncomingValue(1)->hasOneUse())
4700 return std::nullopt;
4701 return FirstIncomingHasOneUse ? 0 : 1;
4702}
4703
4704static VPSingleDefRecipe *
4705optimizeExtendsForPartialReduction(VPSingleDefRecipe *Op) {
4706 // reduce.add(mul(ext(A), C))
4707 // -> reduce.add(mul(ext(A), ext(trunc(C))))
4708 const APInt *Const;
4709 if (match(Op, m_Mul(m_ZExtOrSExt(m_VPValue()), m_APInt(Const)))) {
4710 auto *ExtA = cast<VPWidenCastRecipe>(Op->getOperand(0));
4711 Instruction::CastOps ExtOpc = ExtA->getOpcode();
4712 Type *NarrowTy = ExtA->getOperand(0)->getScalarType();
4713 if (!Op->hasOneUse() ||
4715 Const, NarrowTy, TTI::getPartialReductionExtendKind(ExtOpc)))
4716 return Op;
4717
4718 VPBuilder Builder(Op);
4719 auto *Trunc = Builder.createWidenCast(Instruction::CastOps::Trunc,
4720 Op->getOperand(1), NarrowTy);
4721 Type *WideTy = ExtA->getScalarType();
4722 Op->setOperand(1, Builder.createWidenCast(ExtOpc, Trunc, WideTy));
4723 return Op;
4724 }
4725
4726 // reduce.add(abs(sub(ext(A), ext(B))))
4727 // -> reduce.add(ext(absolute-difference(A, B)))
4728 VPValue *X, *Y;
4731 auto *Sub = Op->getOperand(0)->getDefiningRecipe();
4732 auto *Ext = cast<VPWidenCastRecipe>(Sub->getOperand(0));
4733 assert(Ext->getOpcode() ==
4734 cast<VPWidenCastRecipe>(Sub->getOperand(1))->getOpcode() &&
4735 "Expected both the LHS and RHS extends to be the same");
4736 bool IsSigned = Ext->getOpcode() == Instruction::SExt;
4737 VPBuilder Builder(Op);
4738 Type *SrcTy = X->getScalarType();
4739 auto *FreezeX = Builder.insert(new VPWidenRecipe(Instruction::Freeze, {X}));
4740 auto *FreezeY = Builder.insert(new VPWidenRecipe(Instruction::Freeze, {Y}));
4741 auto *Max = Builder.insert(
4742 new VPWidenIntrinsicRecipe(IsSigned ? Intrinsic::smax : Intrinsic::umax,
4743 {FreezeX, FreezeY}, SrcTy));
4744 auto *Min = Builder.insert(
4745 new VPWidenIntrinsicRecipe(IsSigned ? Intrinsic::smin : Intrinsic::umin,
4746 {FreezeX, FreezeY}, SrcTy));
4747 auto *AbsDiff = Builder.insert(
4748 new VPWidenRecipe(Instruction::Sub, {Max, Min},
4749 VPIRFlags::getDefaultFlags(Instruction::Sub)));
4750 return Builder.createWidenCast(Instruction::CastOps::ZExt, AbsDiff,
4751 Op->getScalarType());
4752 }
4753
4754 // reduce.add(ext(mul(ext(A), ext(B))))
4755 // -> reduce.add(mul(wider_ext(A), wider_ext(B)))
4756 // TODO: Support this optimization for float types.
4758 m_ZExtOrSExt(m_VPValue()))))) {
4759 auto *Ext = cast<VPWidenCastRecipe>(Op);
4760 auto *Mul = cast<VPWidenRecipe>(Ext->getOperand(0));
4761 auto *MulLHS = cast<VPWidenCastRecipe>(Mul->getOperand(0));
4762 auto *MulRHS = cast<VPWidenCastRecipe>(Mul->getOperand(1));
4763 if (!Mul->hasOneUse() ||
4764 (Ext->getOpcode() != MulLHS->getOpcode() && MulLHS != MulRHS) ||
4765 MulLHS->getOpcode() != MulRHS->getOpcode())
4766 return Op;
4767 VPBuilder Builder(Mul);
4768 auto *NewLHS = Builder.createWidenCast(
4769 MulLHS->getOpcode(), MulLHS->getOperand(0), Ext->getScalarType());
4770 auto *NewRHS = MulLHS == MulRHS
4771 ? NewLHS
4772 : Builder.createWidenCast(MulRHS->getOpcode(),
4773 MulRHS->getOperand(0),
4774 Ext->getScalarType());
4775 auto *NewMul = Mul->cloneWithOperands({NewLHS, NewRHS});
4776 Builder.insert(NewMul);
4777 Op->replaceAllUsesWith(NewMul);
4778 Op->eraseFromParent();
4779 Mul->eraseFromParent();
4780 return NewMul;
4781 }
4782
4783 return Op;
4784}
4785
4786static VPExpressionRecipe *
4787createPartialReductionExpression(VPReductionRecipe *Red) {
4788 VPValue *VecOp = Red->getVecOp();
4789
4790 // reduce.[f]add(ext(op))
4791 // -> VPExpressionRecipe(op, red)
4792 if (match(VecOp, m_WidenAnyExtend(m_VPValue())))
4793 return new VPExpressionRecipe(cast<VPWidenCastRecipe>(VecOp), Red);
4794
4795 // reduce.[f]add(neg(ext(op)))
4796 // -> VPExpressionRecipe(op, sub/neg, red)
4797 if (match(VecOp, m_AnyNeg(m_WidenAnyExtend(m_VPValue())))) {
4798 auto *Neg = cast<VPWidenRecipe>(VecOp);
4799 auto *Ext =
4800 cast<VPWidenCastRecipe>(Neg->getOperand(Neg->getNumOperands() - 1));
4801 return new VPExpressionRecipe(Ext, Neg, Red);
4802 }
4803
4804 // reduce.[f]add([f]mul(ext(a), ext(b)))
4805 // -> VPExpressionRecipe(a, b, mul, red)
4806 if (match(VecOp, m_FMul(m_FPExt(m_VPValue()), m_FPExt(m_VPValue()))) ||
4807 match(VecOp,
4809 auto *Mul = cast<VPWidenRecipe>(VecOp);
4810 auto *ExtA = cast<VPWidenCastRecipe>(Mul->getOperand(0));
4811 auto *ExtB = cast<VPWidenCastRecipe>(Mul->getOperand(1));
4812 return new VPExpressionRecipe(ExtA, ExtB, Mul, Red);
4813 }
4814
4815 // reduce.fadd(fneg(fmul(fpext(a), fpext(b))))
4816 // -> VPExpressionRecipe(a, b, fmul, fsub, red)
4817 if (match(VecOp,
4819 auto *FNeg = cast<VPWidenRecipe>(VecOp);
4820 auto *FMul = cast<VPWidenRecipe>(FNeg->getOperand(0));
4821 auto *ExtA = cast<VPWidenCastRecipe>(FMul->getOperand(0));
4822 auto *ExtB = cast<VPWidenCastRecipe>(FMul->getOperand(1));
4823 return new VPExpressionRecipe(ExtA, ExtB, FMul, FNeg, Red);
4824 }
4825
4826 // reduce.add(neg(mul(ext(a), ext(b))))
4827 // -> VPExpressionRecipe(a, b, mul, sub, red)
4829 m_ZExtOrSExt(m_VPValue()))))) {
4830 auto *Sub = cast<VPWidenRecipe>(VecOp);
4831 auto *Mul = cast<VPWidenRecipe>(Sub->getOperand(1));
4832 auto *ExtA = cast<VPWidenCastRecipe>(Mul->getOperand(0));
4833 auto *ExtB = cast<VPWidenCastRecipe>(Mul->getOperand(1));
4834 return new VPExpressionRecipe(ExtA, ExtB, Mul, Sub, Red);
4835 }
4836
4837 llvm_unreachable("Unsupported expression");
4838}
4839
4840// Helper to transform a partial reduction chain into a partial reduction
4841// recipe. Assumes profitability has been checked.
4842static void transformToPartialReduction(const VPPartialReductionChain &Chain,
4843 VPlan &Plan,
4844 VPReductionPHIRecipe *RdxPhi) {
4845 VPWidenRecipe *WidenRecipe = Chain.ReductionBinOp;
4846 assert(WidenRecipe->getNumOperands() == 2 && "Expected binary operation");
4847
4848 VPValue *Accumulator = WidenRecipe->getOperand(Chain.AccumulatorOpIdx);
4849 auto *ExtendedOp = cast<VPSingleDefRecipe>(
4850 WidenRecipe->getOperand(1 - Chain.AccumulatorOpIdx));
4851
4852 // FIXME: Do these transforms before invoking the cost-model.
4853 ExtendedOp = optimizeExtendsForPartialReduction(ExtendedOp);
4854
4855 // Sub-reductions can be implemented in two ways:
4856 // (1) negate the operand in the vector loop (the default way).
4857 // (2) subtract the reduced value from the init value in the middle block.
4858 // Both ways keep the reduction itself as an 'add' reduction.
4859 //
4860 // The ISD nodes for partial reductions don't support folding the
4861 // sub/negation into its operands because the following is not a valid
4862 // transformation:
4863 // sub(0, mul(ext(a), ext(b)))
4864 // -> mul(ext(a), ext(sub(0, b)))
4865 //
4866 // It's therefore better to choose option (2) such that the partial
4867 // reduction is always positive (starting at '0') and to do a final
4868 // subtract in the middle block.
4869 if ((WidenRecipe->getOpcode() == Instruction::Sub &&
4870 Chain.RK != RecurKind::Sub) ||
4871 (WidenRecipe->getOpcode() == Instruction::FSub &&
4872 Chain.RK != RecurKind::FSub)) {
4873 VPBuilder Builder(WidenRecipe);
4874 Type *ElemTy = ExtendedOp->getScalarType();
4875 VPWidenRecipe *NegRecipe;
4876 if (WidenRecipe->getOpcode() == Instruction::FSub) {
4877 NegRecipe =
4878 new VPWidenRecipe(Instruction::FNeg, {ExtendedOp},
4879 VPIRFlags::getDefaultFlags(Instruction::FNeg),
4881 } else {
4882 auto *Zero = Plan.getZero(ElemTy);
4883 NegRecipe =
4884 new VPWidenRecipe(Instruction::Sub, {Zero, ExtendedOp},
4885 VPIRFlags::getDefaultFlags(Instruction::Sub),
4887 }
4888 Builder.insert(NegRecipe);
4889 ExtendedOp = NegRecipe;
4890 }
4891
4892 // Check if WidenRecipe is the final result of the reduction. If so, look
4893 // through the Select recipe introduced by tail-folding, otherwise look
4894 // through any Blend recipe introduced by predication for the block.
4895 VPValue *ExitSearch =
4896 Chain.Blend ? cast<VPValue>(Chain.Blend) : cast<VPValue>(WidenRecipe);
4897
4898 VPValue *Cond = nullptr;
4900 findUserOf(ExitSearch, m_Select(m_VPValue(Cond), m_Specific(ExitSearch),
4901 m_Specific(RdxPhi))));
4902
4903 if (Chain.Blend) {
4904 std::optional<unsigned> BlendReductionIdx =
4905 getBlendReductionUpdateValueIdx(Chain.Blend);
4906 assert(BlendReductionIdx &&
4907 Chain.Blend->getIncomingValue(*BlendReductionIdx) == WidenRecipe &&
4908 "Expected blend to contain the reduction update");
4909 VPValue *BlendCond = Chain.Blend->getMask(*BlendReductionIdx);
4910 Cond = ExitValue ? VPBuilder(WidenRecipe)
4911 .createLogicalAnd(Cond, BlendCond,
4912 WidenRecipe->getDebugLoc())
4913 : BlendCond;
4914 }
4915
4916 // When folding the tail, the inactive lanes of the reduction update are
4917 // computed from values that do not correspond to any scalar iteration
4918 // and must not be accumulated.
4919 if (!Cond)
4921
4922 bool IsLastInChain = RdxPhi->getBackedgeValue() == WidenRecipe ||
4923 RdxPhi->getBackedgeValue() == ExitValue ||
4924 RdxPhi->getBackedgeValue() == Chain.Blend;
4925 assert((!ExitValue || IsLastInChain) &&
4926 "if we found ExitValue, it must match RdxPhi's backedge value");
4927
4928 Type *PhiType = RdxPhi->getScalarType();
4929 RecurKind RdxKind =
4931 auto *PartialRed = new VPReductionRecipe(
4932 RdxKind,
4933 RdxKind == RecurKind::FAdd ? WidenRecipe->getFastMathFlagsOrNone()
4934 : FastMathFlags(),
4935 WidenRecipe->getUnderlyingInstr(), Accumulator, ExtendedOp, Cond,
4936 RdxUnordered{/*VFScaleFactor=*/Chain.ScaleFactor});
4937 PartialRed->insertBefore(WidenRecipe);
4938
4939 if (ExitValue)
4940 ExitValue->replaceAllUsesWith(PartialRed);
4941 if (Chain.Blend)
4942 Chain.Blend->replaceAllUsesWith(PartialRed);
4943 WidenRecipe->replaceAllUsesWith(PartialRed);
4944
4945 // For cost-model purposes, fold this into a VPExpression.
4946 VPExpressionRecipe *E = createPartialReductionExpression(PartialRed);
4947 E->insertBefore(WidenRecipe);
4948 PartialRed->replaceAllUsesWith(E);
4949
4950 // We only need to update the PHI node once, which is when we find the
4951 // last reduction in the chain.
4952 if (!IsLastInChain)
4953 return;
4954
4955 // Scale the PHI and ReductionStartVector by the VFScaleFactor
4956 assert(RdxPhi->getVFScaleFactor() == 1 && "scale factor must not be set");
4957 RdxPhi->setVFScaleFactor(Chain.ScaleFactor);
4958
4959 auto *StartInst = cast<VPInstruction>(RdxPhi->getStartValue());
4960 assert(StartInst->getOpcode() == VPInstruction::ReductionStartVector);
4961 auto *NewScaleFactor = Plan.getConstantInt(32, Chain.ScaleFactor);
4962 StartInst->setOperand(2, NewScaleFactor);
4963
4964 // If this is the last value in a sub-reduction chain, then update the PHI
4965 // node to start at `0` and update the reduction-result to subtract from
4966 // the PHI's start value.
4967 if (Chain.RK != RecurKind::Sub && Chain.RK != RecurKind::FSub)
4968 return;
4969
4970 VPValue *OldStartValue = StartInst->getOperand(0);
4971 StartInst->setOperand(0, StartInst->getOperand(1));
4972
4973 // Replace reduction_result by 'sub (startval, reductionresult)'.
4975 assert(RdxResult && "Could not find reduction result");
4976
4977 VPBuilder Builder = VPBuilder::getToInsertAfter(RdxResult);
4978 unsigned SubOpc = Chain.RK == RecurKind::FSub ? Instruction::BinaryOps::FSub
4979 : Instruction::BinaryOps::Sub;
4980 VPInstruction *NewResult = Builder.createNaryOp(
4981 SubOpc, {OldStartValue, RdxResult}, VPIRFlags::getDefaultFlags(SubOpc),
4982 RdxPhi->getDebugLoc());
4983 RdxResult->replaceUsesWithIf(
4984 NewResult,
4985 [&NewResult](VPUser &U, unsigned Idx) { return &U != NewResult; });
4986}
4987
4988/// Returns the cost of a link in a partial-reduction chain for a given VF.
4989static InstructionCost
4990getPartialReductionLinkCost(VPCostContext &CostCtx,
4991 const VPPartialReductionChain &Link,
4992 ElementCount VF) {
4993 Type *RdxType = Link.ReductionBinOp->getScalarType();
4994 const ExtendedReductionOperand &ExtendedOp = Link.ExtendedOp;
4995 std::optional<unsigned> BinOpc = std::nullopt;
4996 // If ExtendB is not none, then the "ExtendsUser" is the binary operation.
4997 if (ExtendedOp.ExtendB.Kind != ExtendKind::PR_None)
4998 BinOpc = ExtendedOp.ExtendsUser->getOpcode();
4999
5000 std::optional<llvm::FastMathFlags> Flags;
5001 if (RdxType->isFloatingPointTy())
5002 Flags = Link.ReductionBinOp->getFastMathFlagsOrNone();
5003
5004 auto GetLinkOpcode = [&Link]() -> unsigned {
5005 switch (Link.RK) {
5006 case RecurKind::Sub:
5007 return Instruction::Add;
5008 case RecurKind::FSub:
5009 return Instruction::FAdd;
5010 default:
5011 return Link.ReductionBinOp->getOpcode();
5012 }
5013 };
5014
5015 return CostCtx.TTI.getPartialReductionCost(
5016 GetLinkOpcode(), ExtendedOp.ExtendA.SrcType, ExtendedOp.ExtendB.SrcType,
5017 RdxType, VF, ExtendedOp.ExtendA.Kind, ExtendedOp.ExtendB.Kind, BinOpc,
5018 CostCtx.CostKind, Flags);
5019}
5020
5021static ExtendKind getPartialReductionExtendKind(VPWidenCastRecipe *Cast) {
5023}
5024
5025/// Checks if \p Op (which is an operand of \p UpdateR) is an extended reduction
5026/// operand. This is an operand where the source of the value (e.g. a load) has
5027/// been extended (sext, zext, or fpext) before it is used in the reduction.
5028///
5029/// Possible forms matched by this function:
5030/// - UpdateR(PrevValue, ext(...))
5031/// - UpdateR(PrevValue, mul(ext(...), ext(...)))
5032/// - UpdateR(PrevValue, mul(ext(...), Constant))
5033/// - UpdateR(PrevValue, ext(mul(ext(...), ext(...))))
5034/// - UpdateR(PrevValue, ext(mul(ext(...), Constant)))
5035/// - UpdateR(PrevValue, abs(sub(ext(...), ext(...)))
5036///
5037/// Note: The second operand of UpdateR corresponds to \p Op in the examples.
5038static std::optional<ExtendedReductionOperand>
5039matchExtendedReductionOperand(VPWidenRecipe *UpdateR, VPValue *Op) {
5040 assert(is_contained(UpdateR->operands(), Op) &&
5041 "Op should be operand of UpdateR");
5042
5043 // Try matching an absolute difference operand of the form
5044 // `abs(sub(ext(A), ext(B)))`. This will be later transformed into
5045 // `ext(absolute-difference(A, B))`. This allows us to perform the absolute
5046 // difference on a wider type and get the extend for "free" from the partial
5047 // reduction.
5048 VPValue *X, *Y;
5049 if (Op->hasOneUse() &&
5053 auto *Abs = cast<VPWidenIntrinsicRecipe>(Op);
5054 auto *Sub = cast<VPWidenRecipe>(Abs->getOperand(0));
5055 auto *LHSExt = cast<VPWidenCastRecipe>(Sub->getOperand(0));
5056 auto *RHSExt = cast<VPWidenCastRecipe>(Sub->getOperand(1));
5057 Type *LHSInputType = X->getScalarType();
5058 Type *RHSInputType = Y->getScalarType();
5059 if (LHSInputType != RHSInputType ||
5060 LHSExt->getOpcode() != RHSExt->getOpcode())
5061 return std::nullopt;
5062 // Note: This is essentially the same as matching ext(...) as we will
5063 // rewrite this operand to ext(absolute-difference(A, B)).
5064 return ExtendedReductionOperand{
5065 Sub,
5066 /*ExtendA=*/{LHSInputType, getPartialReductionExtendKind(LHSExt)},
5067 /*ExtendB=*/{}};
5068 }
5069
5070 std::optional<TTI::PartialReductionExtendKind> OuterExtKind;
5072 auto *CastRecipe = cast<VPWidenCastRecipe>(Op);
5073 VPValue *CastSource = CastRecipe->getOperand(0);
5074 OuterExtKind = getPartialReductionExtendKind(CastRecipe);
5075 if (match(CastSource, m_Mul(m_VPValue(), m_VPValue())) ||
5076 match(CastSource, m_FMul(m_VPValue(), m_VPValue()))) {
5077 // Match: ext(mul(...))
5078 // Record the outer extend kind and set `Op` to the mul. We can then match
5079 // this as a binary operation. Note: We can optimize out the outer extend
5080 // by widening the inner extends to match it. See
5081 // optimizeExtendsForPartialReduction.
5082 Op = CastSource;
5083 } else {
5084 return ExtendedReductionOperand{
5085 UpdateR,
5086 /*ExtendA=*/{CastSource->getScalarType(), *OuterExtKind},
5087 /*ExtendB=*/{}};
5088 }
5089 }
5090
5091 if (!Op->hasOneUse())
5092 return std::nullopt;
5093
5095 if (!MulOp ||
5096 !is_contained({Instruction::Mul, Instruction::FMul}, MulOp->getOpcode()))
5097 return std::nullopt;
5098
5099 // The rest of the matching assumes `Op` is a (possibly extended) mul
5100 // operation.
5101
5102 VPValue *LHS = MulOp->getOperand(0);
5103 VPValue *RHS = MulOp->getOperand(1);
5104
5105 // The LHS of the operation must always be an extend.
5107 return std::nullopt;
5108
5109 auto *LHSCast = cast<VPWidenCastRecipe>(LHS);
5110 Type *LHSInputType = LHSCast->getOperand(0)->getScalarType();
5111 ExtendKind LHSExtendKind = getPartialReductionExtendKind(LHSCast);
5112
5113 // The RHS of the operation can be an extend or a constant integer.
5114 const APInt *RHSConst = nullptr;
5115 VPWidenCastRecipe *RHSCast = nullptr;
5117 RHSCast = cast<VPWidenCastRecipe>(RHS);
5118 else if (!match(RHS, m_APInt(RHSConst)) ||
5119 !canConstantBeExtended(RHSConst, LHSInputType, LHSExtendKind))
5120 return std::nullopt;
5121
5122 // The outer extend kind must match the inner extends for folding.
5123 for (VPWidenCastRecipe *Cast : {LHSCast, RHSCast})
5124 if (Cast && OuterExtKind &&
5125 getPartialReductionExtendKind(Cast) != OuterExtKind)
5126 return std::nullopt;
5127
5128 Type *RHSInputType = LHSInputType;
5129 ExtendKind RHSExtendKind = LHSExtendKind;
5130 if (RHSCast) {
5131 RHSInputType = RHSCast->getOperand(0)->getScalarType();
5132 RHSExtendKind = getPartialReductionExtendKind(RHSCast);
5133 }
5134
5135 return ExtendedReductionOperand{
5136 MulOp, {LHSInputType, LHSExtendKind}, {RHSInputType, RHSExtendKind}};
5137}
5138
5139/// Examines each operation in the reduction chain corresponding to \p RedPhiR,
5140/// and determines if the target can use a cheaper operation with a wider
5141/// per-iteration input VF and narrower PHI VF. If successful, returns the chain
5142/// of operations in the reduction.
5143static std::optional<SmallVector<VPPartialReductionChain>>
5144getScaledReductions(VPReductionPHIRecipe *RedPhiR) {
5145 // Get the backedge value from the reduction PHI and find the
5146 // ComputeReductionResult that uses it (directly or through a select for
5147 // predicated reductions).
5148 auto *RdxResult = vputils::findComputeReductionResult(RedPhiR);
5149 if (!RdxResult)
5150 return std::nullopt;
5151 VPValue *ExitValue = RdxResult->getOperand(0);
5152 match(ExitValue, m_Select(m_VPValue(), m_VPValue(ExitValue), m_VPValue()));
5153
5155 RecurKind RK = RedPhiR->getRecurrenceKind();
5156 Type *PhiType = RedPhiR->getScalarType();
5157 TypeSize PHISize = PhiType->getPrimitiveSizeInBits();
5158
5159 // Work backwards from the ExitValue examining each reduction operation.
5160 VPValue *CurrentValue = ExitValue;
5161 while (CurrentValue != RedPhiR) {
5162 VPBlendRecipe *Blend = dyn_cast<VPBlendRecipe>(CurrentValue);
5163 std::optional<unsigned> BlendReductionIdx;
5164 if (Blend) {
5165 assert(!Blend->isNormalized() && "Expect Blend not to be normalized.");
5166 if (Blend->getNumIncomingValues() != 2)
5167 return std::nullopt;
5168
5169 BlendReductionIdx = getBlendReductionUpdateValueIdx(Blend);
5170 if (!BlendReductionIdx)
5171 return std::nullopt;
5172
5173 CurrentValue = Blend->getIncomingValue(*BlendReductionIdx);
5174 }
5175
5176 auto *UpdateR = dyn_cast<VPWidenRecipe>(CurrentValue);
5177 if (!UpdateR || !Instruction::isBinaryOp(UpdateR->getOpcode()))
5178 return std::nullopt;
5179
5180 VPValue *Op = UpdateR->getOperand(1);
5181 VPValue *PrevValue = UpdateR->getOperand(0);
5182
5183 // Find the extended operand. The other operand (PrevValue) is the next link
5184 // in the reduction chain.
5185 std::optional<ExtendedReductionOperand> ExtendedOp =
5186 matchExtendedReductionOperand(UpdateR, Op);
5187 if (!ExtendedOp) {
5188 ExtendedOp = matchExtendedReductionOperand(UpdateR, PrevValue);
5189 if (!ExtendedOp)
5190 return std::nullopt;
5191 std::swap(Op, PrevValue);
5192 }
5193
5194 // Look for VPBlend(reduce(PrevValue, Op), PrevValue), where
5195 // reduce is equal to CurrentValue. This can be lowered as
5196 // a conditional reduction by hoisting the select to the inputs.
5197 if (Blend && Blend->getIncomingValue(1 - *BlendReductionIdx) != PrevValue)
5198 return std::nullopt;
5199
5200 Type *ExtSrcType = ExtendedOp->ExtendA.SrcType;
5201 TypeSize ExtSrcSize = ExtSrcType->getPrimitiveSizeInBits();
5202 if (!PHISize.hasKnownScalarFactor(ExtSrcSize))
5203 return std::nullopt;
5204
5205 VPPartialReductionChain Link(
5206 {UpdateR, *ExtendedOp, RK,
5207 PrevValue == UpdateR->getOperand(0) ? 0U : 1U,
5208 static_cast<unsigned>(PHISize.getKnownScalarFactor(ExtSrcSize)),
5209 Blend});
5210 Chain.push_back(Link);
5211 CurrentValue = PrevValue;
5212 }
5213
5214 // The chain links were collected by traversing backwards from the exit value.
5215 // Reverse the chains so they are in program order.
5216 std::reverse(Chain.begin(), Chain.end());
5217 return Chain;
5218}
5219} // namespace
5220
5222 VPCostContext &CostCtx,
5223 VFRange &Range) {
5224 // Find all possible valid partial reductions, grouping chains by their PHI.
5225 // This grouping allows invalidating the whole chain, if any link is not a
5226 // valid partial reduction.
5228 ChainsByPhi;
5229 VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
5230 for (VPRecipeBase &R : HeaderVPBB->phis()) {
5231 auto *RedPhiR = dyn_cast<VPReductionPHIRecipe>(&R);
5232 if (!RedPhiR)
5233 continue;
5234
5235 if (auto Chains = getScaledReductions(RedPhiR))
5236 ChainsByPhi.try_emplace(RedPhiR, std::move(*Chains));
5237 }
5238
5239 if (ChainsByPhi.empty())
5240 return;
5241
5242 // Build set of partial reduction operations and blends for user validation
5243 // and a map of reduction bin ops to their scale factors for scale validation.
5244 SmallPtrSet<VPRecipeBase *, 4> PartialReductionOps;
5245 SmallPtrSet<VPBlendRecipe *, 4> PartialReductionBlends;
5246 DenseMap<VPSingleDefRecipe *, unsigned> ScaledReductionMap;
5247 for (const auto &[_, Chains] : ChainsByPhi)
5248 for (const VPPartialReductionChain &Chain : Chains) {
5249 PartialReductionOps.insert(Chain.ExtendedOp.ExtendsUser);
5250 if (Chain.Blend)
5251 PartialReductionBlends.insert(Chain.Blend);
5252 ScaledReductionMap[Chain.ReductionBinOp] = Chain.ScaleFactor;
5253 }
5254
5255 // A partial reduction is invalid if any of its extends are used by
5256 // something that isn't another partial reduction. This is because the
5257 // extends are intended to be lowered along with the reduction itself.
5258 auto ExtendUsersValid = [&](VPValue *Ext) {
5259 return !isa<VPWidenCastRecipe>(Ext) || all_of(Ext->users(), [&](VPUser *U) {
5260 return PartialReductionOps.contains(cast<VPRecipeBase>(U));
5261 });
5262 };
5263
5264 auto IsProfitablePartialReductionChainForVF =
5265 [&](ArrayRef<VPPartialReductionChain> Chain, ElementCount VF) -> bool {
5266 InstructionCost PartialCost = 0, RegularCost = 0;
5267
5268 // The chain is a profitable partial reduction chain if the cost of handling
5269 // the entire chain is cheaper when using partial reductions than when
5270 // handling the entire chain using regular reductions.
5271 for (const VPPartialReductionChain &Link : Chain) {
5272 const ExtendedReductionOperand &ExtendedOp = Link.ExtendedOp;
5273 InstructionCost LinkCost = getPartialReductionLinkCost(CostCtx, Link, VF);
5274 if (!LinkCost.isValid())
5275 return false;
5276
5277 PartialCost += LinkCost;
5278 RegularCost += Link.ReductionBinOp->computeCost(VF, CostCtx);
5279 // If ExtendB is not none, then the "ExtendsUser" is the binary operation.
5280 if (ExtendedOp.ExtendB.Kind != ExtendKind::PR_None)
5281 RegularCost += ExtendedOp.ExtendsUser->computeCost(VF, CostCtx);
5282 for (VPValue *Op : ExtendedOp.ExtendsUser->operands())
5283 if (auto *Extend = dyn_cast<VPWidenCastRecipe>(Op))
5284 RegularCost += Extend->computeCost(VF, CostCtx);
5285 }
5286 return PartialCost.isValid() && PartialCost < RegularCost;
5287 };
5288
5289 // Validate chains: check that extends are only used by partial reductions,
5290 // and that reduction bin ops are only used by other partial reductions with
5291 // matching scale factors, are outside the loop region or the select
5292 // introduced by tail-folding. Otherwise we would create users of scaled
5293 // reductions where the types of the other operands don't match.
5294 for (auto &[RedPhiR, Chains] : ChainsByPhi) {
5295 for (const VPPartialReductionChain &Chain : Chains) {
5296 if (!all_of(Chain.ExtendedOp.ExtendsUser->operands(), ExtendUsersValid)) {
5297 Chains.clear();
5298 break;
5299 }
5300 auto UseIsValid = [&, RedPhiR = RedPhiR](VPUser *U) {
5301 if (auto *PhiR = dyn_cast<VPReductionPHIRecipe>(U))
5302 return PhiR == RedPhiR;
5303 auto *R = cast<VPSingleDefRecipe>(U);
5304
5305 if (auto *Blend = dyn_cast<VPBlendRecipe>(R))
5306 return Blend == Chain.Blend || PartialReductionBlends.contains(Blend);
5307
5308 return Chain.ScaleFactor == ScaledReductionMap.lookup_or(R, 0) ||
5310 m_Specific(Chain.ReductionBinOp))) ||
5311 match(R, m_Select(m_VPValue(), m_Specific(Chain.ReductionBinOp),
5312 m_Specific(RedPhiR)));
5313 };
5314 if (!all_of(Chain.ReductionBinOp->users(), UseIsValid)) {
5315 Chains.clear();
5316 break;
5317 }
5318
5319 // Check if the compute-reduction-result is used by a sunk store.
5320 // TODO: Also form partial reductions in those cases.
5321 if (auto *RdxResult = vputils::findComputeReductionResult(RedPhiR)) {
5322 if (any_of(RdxResult->users(), [](VPUser *U) {
5323 auto *RepR = dyn_cast<VPReplicateRecipe>(U);
5324 return RepR && RepR->getOpcode() == Instruction::Store;
5325 })) {
5326 Chains.clear();
5327 break;
5328 }
5329 }
5330 }
5331
5332 // Clear the chain if it is not profitable.
5334 [&, &Chains = Chains](ElementCount VF) {
5335 return IsProfitablePartialReductionChainForVF(Chains, VF);
5336 },
5337 Range))
5338 Chains.clear();
5339 }
5340
5341 for (auto &[Phi, Chains] : ChainsByPhi)
5342 for (const VPPartialReductionChain &Chain : Chains)
5343 transformToPartialReduction(Chain, Plan, Phi);
5344}
5345
5347 VPRecipeBuilder &RecipeBuilder,
5348 VPCostContext &CostCtx) {
5349 // Collect all loads/stores first. We will start with ones having simpler
5350 // decisions followed by more complex ones that are potentially
5351 // guided/dependent on the simpler ones.
5353 for (VPBasicBlock *VPBB :
5356 for (VPRecipeBase &R : *VPBB) {
5357 auto *VPI = dyn_cast<VPInstruction>(&R);
5358 if (VPI && VPI->getUnderlyingValue() &&
5359 is_contained({Instruction::Load, Instruction::Store},
5360 VPI->getOpcode()))
5361 MemOps.push_back(VPI);
5362 }
5363 }
5364
5365 // Few helpers to process different kinds of memory operations.
5366
5367 // To be used as argument to `VPlanTransforms::runPass` which explicitly
5368 // specified pass name, hence `VPlan &` parameter.
5369 auto ProcessSubset = [&](VPlan &, auto ProcessVPInst) {
5370 SmallVector<VPInstruction *> RemainingMemOps;
5371 for (VPInstruction *VPI : MemOps) {
5372 if (!ProcessVPInst(VPI))
5373 RemainingMemOps.push_back(VPI);
5374 }
5375
5376 MemOps.clear();
5377 std::swap(MemOps, RemainingMemOps);
5378 };
5379
5380 auto ReplaceWith = [&](VPInstruction *VPI, VPRecipeBase *New) {
5381 assert(New->getParent() && "New recipe must have been inserted");
5382 if (VPI->getOpcode() == Instruction::Load)
5383 VPI->replaceAllUsesWith(New->getVPSingleValue());
5384 VPI->eraseFromParent();
5385
5386 // VPI has been processed.
5387 return true;
5388 };
5389
5390 auto Scalarize = [&](VPInstruction *VPI) {
5391 return ReplaceWith(VPI, VPBuilder(VPI).insert(
5392 RecipeBuilder.handleReplication(VPI, Range)));
5393 };
5394
5395 VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
5396 VPBuilder FinalRedStoresBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
5398 "lowerMemoryIdioms", ProcessSubset, Plan, [&](VPInstruction *VPI) {
5399 if (RecipeBuilder.replaceWithFinalIfReductionStore(
5400 VPI, FinalRedStoresBuilder))
5401 return true;
5402
5403 // Filter out scalar VPlan for the remaining idioms.
5405 [](ElementCount VF) { return VF.isScalar(); }, Range))
5406 return false;
5407
5408 if (VPHistogramRecipe *Histogram = RecipeBuilder.widenIfHistogram(VPI))
5409 return ReplaceWith(VPI, VPBuilder(VPI).insert(Histogram));
5410
5411 return false;
5412 });
5413
5414 // Filter out scalar VPlan for the remaining memory operations.
5416 [](ElementCount VF) { return VF.isScalar(); }, Range))
5417 return;
5418
5419 // If the instruction's allocated size doesn't equal it's type size, it
5420 // requires padding and will be scalarized.
5422 "scalarizeMemOpsWithIrregularTypes", ProcessSubset, Plan,
5423 [&](VPInstruction *VPI) {
5425 if (hasIrregularType(getLoadStoreType(I), I->getDataLayout()))
5426 return Scalarize(VPI);
5427
5428 return false;
5429 });
5430
5431 if (!RecipeBuilder.prefersVectorizedAddressing()) {
5433 "makeVPlanMemOpDecision", ProcessSubset, Plan, [&](VPInstruction *VPI) {
5435 bool IsLoad = VPI->getOpcode() == Instruction::Load;
5436 if (RecipeBuilder.isPredicatedInst(I) || !IsLoad ||
5438 return false;
5439
5440 // Scalarize loads used as addresses, matching the legacy CM. The load
5441 // is single-scalar if the pointer is loop-invariant, otherwise it is
5442 // replicated per-lane. No mask is needed as the load is not
5443 // predicated.
5444 VPValue *Ptr = VPI->getOperand(0);
5445 const SCEV *PtrSCEV =
5446 vputils::getSCEVExprForVPValue(Ptr, CostCtx.PSE, CostCtx.L);
5447 bool IsSingleScalarLoad =
5448 !isa<SCEVCouldNotCompute>(PtrSCEV) &&
5449 CostCtx.PSE.getSE()->isLoopInvariant(PtrSCEV, CostCtx.L);
5450
5451 ReplaceWith(VPI,
5452 VPBuilder(VPI).insert(new VPReplicateRecipe(
5453 I, Ptr, /*IsSingleScalar=*/IsSingleScalarLoad,
5454 /*Mask=*/nullptr, *VPI, *VPI, VPI->getDebugLoc())));
5455 return true;
5456 });
5457 }
5458
5459 // Widen unit-stride consecutive accesses, matching the legacy CM. Both
5460 // forward (stride +1) and reverse (stride -1) accesses are handled.
5462 "widenConsecutiveMemOps", ProcessSubset, Plan, [&](VPInstruction *VPI) {
5464 bool IsLoad = VPI->getOpcode() == Instruction::Load;
5465 VPValue *Ptr = VPI->getOperand(!IsLoad);
5466 Type *ScalarTy =
5467 IsLoad ? VPI->getScalarType() : VPI->getOperand(0)->getScalarType();
5468 std::optional<int64_t> Stride =
5469 getConstantStride(Ptr, ScalarTy, CostCtx.PSE, CostCtx.L);
5470 if (Stride != 1 && Stride != -1)
5471 return false;
5472 bool Reverse = Stride == -1;
5473
5474 // A predicated access can only be widened (rather than scalarized) if
5475 // the target supports a masked load/store for it.
5476 // TODO: Determine if a load/store needs predication directly in VPlan.
5477 bool IsPredicated = RecipeBuilder.isPredicatedInst(I);
5478 if (IsPredicated && !CostCtx.Config.isLegalMaskedLoadOrStore(
5479 IsLoad, ScalarTy, getLoadStoreAlignment(I),
5481 return false;
5482
5483 VPBuilder Builder(VPI);
5484 VPSingleDefRecipe *VectorPtr = Builder.createConsecutiveVectorPointer(
5485 Ptr, ScalarTy, Reverse, VPI->getDebugLoc());
5486
5487 VPValue *Mask = IsPredicated ? VPI->getMask() : nullptr;
5488 // Reverse the mask so it matches the reversed access order.
5489 if (Reverse && Mask)
5490 Mask = Builder.createNaryOp(VPInstruction::Reverse, Mask,
5491 VPI->getDebugLoc());
5492
5493 if (IsLoad) {
5494 VPSingleDefRecipe *Load = Builder.createWidenLoad(
5495 *cast<LoadInst>(I), VectorPtr, Mask,
5496 /*Consecutive=*/true, *VPI, VPI->getDebugLoc());
5497 // Reverse the loaded values back into program order.
5498 if (Reverse)
5499 Load = Builder.createNaryOp(VPInstruction::Reverse, Load,
5500 VPI->getDebugLoc());
5501 return ReplaceWith(VPI, Load);
5502 }
5503
5504 VPValue *StoredVal = VPI->getOperand(0);
5505 if (Reverse)
5506 // Reverse the stored values so they are written in descending order.
5507 StoredVal = Builder.createNaryOp(VPInstruction::Reverse, StoredVal,
5508 VPI->getDebugLoc());
5509
5510 auto *StoreR = Builder.createWidenStore(
5511 *cast<StoreInst>(I), VectorPtr, StoredVal, Mask,
5512 /*Consecutive=*/true, *VPI, VPI->getDebugLoc());
5513 return ReplaceWith(VPI, StoreR);
5514 });
5515
5516 VPlanTransforms::runPass("delegateMemOpWideningToLegacyCM", ProcessSubset,
5517 Plan, [&](VPInstruction *VPI) {
5518 if (VPRecipeBase *Recipe =
5519 RecipeBuilder.tryToWidenMemory(VPI, Range))
5520 return ReplaceWith(VPI, Recipe);
5521
5522 return Scalarize(VPI);
5523 });
5524}
5525
5528 [&](ElementCount VF) { return VF.isScalar(); }, Range))
5529 return;
5530
5532 Plan.getEntry());
5534 for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
5535 auto *VPI = dyn_cast<VPInstruction>(&R);
5536 if (!VPI)
5537 continue;
5538
5539 auto *I = cast_or_null<Instruction>(VPI->getUnderlyingValue());
5540 // Wouldn't be able to create a `VPReplicateRecipe` anyway.
5541 if (!I)
5542 continue;
5543
5544 // If executing other lanes produces side-effects we can't avoid them.
5545 if (VPI->mayHaveSideEffects())
5546 continue;
5547
5548 // We want to drop the mask operand, verify we can safely do that.
5549 if (VPI->isMasked() && !VPI->isSafeToSpeculativelyExecute())
5550 continue;
5551
5552 // Avoid rewriting IV increment as that interferes with
5553 // `removeRedundantCanonicalIVs`.
5554 if (VPI->getOpcode() == Instruction::Add &&
5556 continue;
5557
5558 // Other lanes are needed - can't drop them.
5560 continue;
5561
5562 auto *Recipe = VPBuilder::createSingleScalarOp(
5563 VPI->getOpcode(), VPI->operandsWithoutMask(), /*Mask=*/nullptr, *VPI,
5564 *VPI, VPI->getDebugLoc(), I);
5565 Recipe->insertBefore(VPI);
5566 VPI->replaceAllUsesWith(Recipe);
5567 VPI->eraseFromParent();
5568 }
5569 }
5570}
5571
5572/// Returns true if \p Info's parameter kinds are compatible with \p Args.
5573static bool areVFParamsOk(const VFInfo &Info, ArrayRef<VPValue *> Args,
5574 PredicatedScalarEvolution &PSE, const Loop *L) {
5575 ScalarEvolution *SE = PSE.getSE();
5576 return all_of(Info.Shape.Parameters, [&](VFParameter Param) {
5577 switch (Param.ParamKind) {
5578 case VFParamKind::Vector:
5579 case VFParamKind::GlobalPredicate:
5580 return true;
5581 case VFParamKind::OMP_Uniform:
5582 return SE->isSCEVable(Args[Param.ParamPos]->getScalarType()) &&
5583 SE->isLoopInvariant(
5584 vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L),
5585 L);
5586 case VFParamKind::OMP_Linear:
5587 return match(vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L),
5588 m_scev_AffineAddRec(
5589 m_SCEV(), m_scev_SpecificSInt(Param.LinearStepOrPos),
5590 m_SpecificLoop(L)));
5591 default:
5592 return false;
5593 }
5594 });
5595}
5596
5597/// Find a vector variant of \p CI for \p VF, respecting \p MaskRequired.
5598/// Returns the variant function, or nullptr. Masked variants are assumed to
5599/// take the mask as a trailing parameter.
5601 ElementCount VF, bool MaskRequired,
5603 const Loop *L) {
5604 if (CI->isNoBuiltin())
5605 return nullptr;
5606 auto Mappings = VFDatabase::getMappings(*CI);
5607 const auto *It = find_if(Mappings, [&](const VFInfo &Info) {
5608 return Info.Shape.VF == VF && (!MaskRequired || Info.isMasked()) &&
5609 areVFParamsOk(Info, Args, PSE, L);
5610 });
5611 if (It == Mappings.end())
5612 return nullptr;
5613 return CI->getModule()->getFunction(It->VectorName);
5614}
5615
5616namespace {
5617/// The outcome of choosing how to widen a call at a given VF.
5618struct CallWideningDecision {
5619 enum class KindTy { Scalarize, Intrinsic, VectorVariant };
5620 CallWideningDecision(KindTy Kind, Function *Variant = nullptr)
5621 : Kind(Kind), Variant(Variant) {}
5622 KindTy Kind;
5623
5624 /// Set when Kind == VectorVariant.
5626
5627 bool operator==(const CallWideningDecision &Other) const {
5628 return Kind == Other.Kind && Variant == Other.Variant;
5629 }
5630};
5631} // namespace
5632
5633/// Pick the cheapest widening for the call \p VPI at \p VF among scalarization,
5634/// vector intrinsic, and vector library variant.
5635static CallWideningDecision decideCallWidening(VPInstruction &VPI,
5637 ElementCount VF,
5638 VPCostContext &CostCtx) {
5639 auto *CI = cast<CallInst>(VPI.getUnderlyingInstr());
5640
5641 // Scalar VFs and calls forced or known to scalarize always replicate.
5642 if (VF.isScalar() || CostCtx.willBeScalarized(CI, VF))
5643 return CallWideningDecision::KindTy::Scalarize;
5644
5645 auto *CalledFn = cast<Function>(
5647 Type *ResultTy = VPI.getScalarType();
5649 bool MaskRequired = CostCtx.isMaskRequired(CI);
5650
5651 // Pseudo intrinsics (assume, lifetime, ...) are always scalarized.
5653 return CallWideningDecision::KindTy::Scalarize;
5654
5655 InstructionCost ScalarCost =
5656 VPReplicateRecipe::computeCallCost(CalledFn, ResultTy, Ops,
5657 /*IsSingleScalar=*/false, VF, CostCtx);
5658
5659 Function *VecFunc =
5660 findVectorVariant(CI, Ops, VF, MaskRequired, CostCtx.PSE, CostCtx.L);
5662 if (VecFunc)
5663 VecCallCost = VPWidenCallRecipe::computeCallCost(VecFunc, CostCtx);
5664
5665 // Prefer the intrinsic if it is at least as cheap as scalarizing and any
5666 // available vector variant.
5667 if (ID) {
5669 VPWidenIntrinsicRecipe::computeCallCost(ID, Ops, VPI, VF, CostCtx);
5670 if (IntrinsicCost.isValid() && ScalarCost >= IntrinsicCost &&
5671 (!VecFunc || VecCallCost >= IntrinsicCost))
5672 return CallWideningDecision::KindTy::Intrinsic;
5673 }
5674
5675 // Otherwise, use a vector library variant when it beats scalarizing.
5676 if (VecFunc && ScalarCost >= VecCallCost)
5677 return {CallWideningDecision::KindTy::VectorVariant, VecFunc};
5678
5679 return CallWideningDecision::KindTy::Scalarize;
5680}
5681
5683 VPRecipeBuilder &RecipeBuilder,
5684 VPCostContext &CostCtx) {
5687 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
5688 auto *VPI = dyn_cast<VPInstruction>(&R);
5689 if (!VPI || !VPI->getUnderlyingValue() ||
5690 VPI->getOpcode() != Instruction::Call)
5691 continue;
5692
5693 auto *CI = cast<CallInst>(VPI->getUnderlyingInstr());
5694 SmallVector<VPValue *, 4> Ops(VPI->op_begin(),
5695 VPI->op_begin() + CI->arg_size());
5696
5697 CallWideningDecision Decision =
5698 decideCallWidening(*VPI, Ops, Range.Start, CostCtx);
5700 [&](ElementCount VF) {
5701 return Decision == decideCallWidening(*VPI, Ops, VF, CostCtx);
5702 },
5703 Range);
5704
5705 VPSingleDefRecipe *Replacement = nullptr;
5706 switch (Decision.Kind) {
5707 case CallWideningDecision::KindTy::Intrinsic: {
5709 Type *ResultTy = VPI->getScalarType();
5710 Replacement = new VPWidenIntrinsicRecipe(*CI, ID, Ops, ResultTy, *VPI,
5711 *VPI, VPI->getDebugLoc());
5712 break;
5713 }
5714 case CallWideningDecision::KindTy::VectorVariant: {
5715 // Masked variants take the mask as a trailing parameter, so they have
5716 // one more parameter than the original call's arguments.
5717 if (Decision.Variant->arg_size() > Ops.size()) {
5718 VPValue *Mask = VPI->isMasked() ? VPI->getMask() : Plan.getTrue();
5719 Ops.push_back(Mask);
5720 }
5721 Ops.push_back(VPI->getOperand(VPI->getNumOperandsWithoutMask() - 1));
5722 Replacement = new VPWidenCallRecipe(CI, Decision.Variant, Ops, *VPI,
5723 *VPI, VPI->getDebugLoc());
5724 break;
5725 }
5726 case CallWideningDecision::KindTy::Scalarize:
5727 Replacement = RecipeBuilder.handleReplication(VPI, Range);
5728 break;
5729 }
5730
5731 Replacement->insertBefore(VPI);
5732 VPI->replaceAllUsesWith(Replacement);
5733 VPI->eraseFromParent();
5734 }
5735 }
5736}
5737
5740 Loop &L, VPCostContext &Ctx,
5741 VFRange &Range) {
5742 if (Plan.hasScalarVFOnly())
5743 return;
5744
5745 VPRegionBlock *VectorLoop = Plan.getVectorLoopRegion();
5746 VPValue *I32VF = nullptr;
5748 vp_depth_first_shallow(VectorLoop->getEntry()))) {
5749 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
5750 auto *MemR = dyn_cast<VPWidenMemoryRecipe>(&R);
5751 // TODO: Transform reverse access into strided access with -1 stride.
5752 // TODO: Transform gather/scatter with uniform address into strided access
5753 // with 0 stride.
5754 // TODO: Transform interleave access into multiple strided accesses.
5755 if (!MemR || MemR->isConsecutive())
5756 continue;
5757
5758 VPValue *Ptr = MemR->getAddr();
5759 // Check if this is a strided access by analyzing the address SCEV for an
5760 // affine addRec.
5761 const SCEV *PtrSCEV = vputils::getSCEVExprForVPValue(Ptr, PSE, &L);
5762 const SCEV *Start;
5763 const SCEVConstant *Step;
5764 // TODO: Support non-constant loop invariant stride.
5765 if (!match(PtrSCEV,
5767 m_SpecificLoop(&L))))
5768 continue;
5769
5770 VPValue *StoredValue = nullptr;
5771 Type *DataTy;
5772 Intrinsic::ID IntrinID;
5773 if (auto *StoreR = dyn_cast<VPWidenStoreRecipe>(&R)) {
5774 StoredValue = StoreR->getStoredValue();
5775 DataTy = StoredValue->getScalarType();
5776 IntrinID = Intrinsic::experimental_vp_strided_store;
5777 } else {
5778 auto *LoadR = cast<VPWidenLoadRecipe>(&R);
5779 DataTy = LoadR->getScalarType();
5780 IntrinID = Intrinsic::experimental_vp_strided_load;
5781 }
5782
5783 Align Alignment = MemR->getAlign();
5784 auto IsProfitable = [&](ElementCount VF) {
5785 Type *VectorTy = toVectorTy(DataTy, VF);
5786 if (!Ctx.TTI.isLegalStridedLoadStore(VectorTy, Alignment))
5787 return false;
5788 const InstructionCost CurrentCost = MemR->computeCost(VF, Ctx);
5789 const InstructionCost StridedLoadStoreCost =
5791 IntrinID, VectorTy, MemR->isMasked(), Alignment, Ctx);
5792 return StridedLoadStoreCost < CurrentCost;
5793 };
5794
5796 Range))
5797 continue;
5798
5799 // Invalidate the legacy widening decision so the cost of replaced load is
5800 // not counted during precomputeCosts.
5801 // TODO: Remove once the legacy exit cost computation is retired.
5802 for (ElementCount VF : Range)
5803 Ctx.invalidateWideningDecision(&MemR->getIngredient(), VF);
5804
5805 // Get VF as i32 for the vector length operand.
5806 if (!I32VF) {
5807 VPBuilder Builder(Plan.getVectorPreheader());
5808 I32VF = Builder.createScalarZExtOrTrunc(
5809 &Plan.getVF(), Type::getInt32Ty(Plan.getContext()),
5811 }
5812
5813 VPBuilder Builder(&R);
5814 // Create the base pointer of strided access.
5815 // TODO: reuse VPDerivedIVRecipe for base pointer computation when it
5816 // supports a general VPValue as the start value.
5817 VPValue *StartVPV =
5818 VPSCEVExpander(Builder, *PSE.getSE(), R.getDebugLoc()).expand(Start);
5819 VPValue *StrideInBytes = Plan.getOrAddLiveIn(Step->getValue());
5820 Type *IndexTy = Plan.getDataLayout().getIndexType(Ptr->getScalarType());
5821 assert(IndexTy == StrideInBytes->getScalarType() &&
5822 "Stride type from SCEV must match the index type");
5823 VPValue *CanIV = Builder.createScalarZExtOrTrunc(
5824 VectorLoop->getCanonicalIV(), IndexTy, DebugLoc::getUnknown());
5825 auto *AddRecPtr = cast<SCEVAddRecExpr>(PtrSCEV);
5826 auto *Offset = Builder.createOverflowingOp(
5827 Instruction::Mul, {CanIV, StrideInBytes},
5828 {AddRecPtr->hasNoUnsignedWrap(), /*HasNSW=*/false});
5829 GEPNoWrapFlags NWFlags = AddRecPtr->hasNoUnsignedWrap()
5832 VPValue *BasePtr = Builder.createNoWrapPtrAdd(StartVPV, Offset, NWFlags);
5833
5834 // Create a new vector pointer for strided access.
5835 VPValue *NewPtr = Builder.createVectorPointer(
5836 BasePtr, Type::getInt8Ty(Plan.getContext()), StrideInBytes, NWFlags,
5837 R.getDebugLoc());
5838
5839 VPValue *Mask = MemR->getMask();
5840 if (!Mask)
5841 Mask = Plan.getTrue();
5843 if (StoredValue)
5844 Ops.push_back(StoredValue);
5845 Ops.append({NewPtr, StrideInBytes, Mask, I32VF});
5846
5847 auto *StridedR = Builder.createWidenMemIntrinsic(
5848 IntrinID, Ops,
5849 StoredValue ? Type::getVoidTy(Plan.getContext()) : DataTy, Alignment,
5850 *MemR, R.getDebugLoc());
5851 if (!StoredValue)
5852 cast<VPWidenLoadRecipe>(&R)->replaceAllUsesWith(StridedR);
5853 R.eraseFromParent();
5854 }
5855 }
5856}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isEqual(const Function &Caller, const Function &Callee)
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
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")
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
static cl::opt< IntrinsicCostStrategy > IntrinsicCost("intrinsic-cost-strategy", cl::desc("Costing strategy for intrinsic instructions"), cl::init(IntrinsicCostStrategy::InstructionCost), cl::values(clEnumValN(IntrinsicCostStrategy::InstructionCost, "instruction-cost", "Use TargetTransformInfo::getInstructionCost"), clEnumValN(IntrinsicCostStrategy::IntrinsicCost, "intrinsic-cost", "Use TargetTransformInfo::getIntrinsicInstrCost"), clEnumValN(IntrinsicCostStrategy::TypeBasedIntrinsicCost, "type-based-intrinsic-cost", "Calculate the intrinsic cost based only on argument types")))
@ Default
Hexagon Common GEP
#define _
iv Induction Variable Users
Definition IVUsers.cpp:48
iv users
Definition IVUsers.cpp:48
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
licm
Definition LICM.cpp:391
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > & Cond
Func MI getDebugLoc()))
This file contains some templates that are useful if you are working with the STL at all.
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.
static SmallVector< SmallVector< VPReplicateRecipe *, 4 > > collectComplementaryPredicatedMemOps(VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L)
static void removeCommonBlendMask(VPBlendRecipe *Blend)
Try to see if all of Blend's masks share a common value logically and'ed and remove it from the masks...
static void tryToCreateAbstractReductionRecipe(VPReductionRecipe *Red, VPCostContext &Ctx, VFRange &Range)
This function tries to create abstract recipes from the reduction recipe for following optimizations ...
static VPReplicateRecipe * findRecipeWithMinAlign(ArrayRef< VPReplicateRecipe * > Group)
static bool handleUncountableExitsWithSideEffects(VPlan &Plan, SmallVectorImpl< EarlyExitInfo > &Exits, VPBasicBlock *HeaderVPBB, VPBasicBlock *LatchVPBB, VPBasicBlock *MiddleVPBB, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC)
Update Plan to mask memory operations in the loop based on whether the early exit is taken or not.
static CallWideningDecision decideCallWidening(VPInstruction &VPI, ArrayRef< VPValue * > Ops, ElementCount VF, VPCostContext &CostCtx)
Pick the cheapest widening for the call VPI at VF among scalarization, vector intrinsic,...
static bool areVFParamsOk(const VFInfo &Info, ArrayRef< VPValue * > Args, PredicatedScalarEvolution &PSE, const Loop *L)
Returns true if Info's parameter kinds are compatible with Args.
static std::optional< VPValue * > getRecipesForUncountableExit(SmallVectorImpl< VPInstruction * > &Recipes, VPBasicBlock *LatchVPBB)
Returns the VPValue representing the uncountable exit comparison used by AnyOf if the recipes it depe...
static bool sinkScalarOperands(VPlan &Plan)
static void tryToRemoveDeadCycle(VPRecipeBase *R)
If R is a phi-like recipe starting a dead cycle of recipes, erase all reachable recipes of the dead c...
static 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.
static bool simplifyBranchConditionForVFAndUF(VPlan &Plan, ElementCount BestVF, unsigned BestUF, PredicatedScalarEvolution &PSE)
Try to simplify the branch condition of Plan.
static VPValue * cloneBinOpForScalarIV(VPWidenRecipe *BinOp, VPValue *ScalarIV, VPWidenIntOrFpInductionRecipe *WidenIV)
Create a scalar version of BinOp, with its WidenIV operand replaced by ScalarIV, and place it after S...
static VPWidenIntOrFpInductionRecipe * getExpressionIV(VPValue *V)
Check if V is a binary expression of a widened IV and a loop-invariant value.
static void removeRedundantInductionCasts(VPlan &Plan)
Remove redundant casts of inductions.
static bool isConditionTrueViaVFAndUF(VPValue *Cond, VPlan &Plan, ElementCount BestVF, unsigned BestUF, PredicatedScalarEvolution &PSE)
Return true if Cond is known to be true for given BestVF and BestUF.
static VPExpressionRecipe * tryToMatchAndCreateExtendedReduction(VPReductionRecipe *Red, VPCostContext &Ctx, VFRange &Range)
This function tries convert extended in-loop reductions to VPExpressionRecipe and clamp the Range if ...
static std::optional< ElementCount > isConsecutiveInterleaveGroup(VPInterleaveRecipe *InterleaveR, ArrayRef< ElementCount > VFs, const TargetTransformInfo &TTI)
Returns VF from VFs if IR is a full interleave group with factor and number of members both equal to ...
static Type * getLoadStoreValueType(VPReplicateRecipe *R, bool IsLoad)
Get the value type of the replicate load or store.
static VPIRMetadata getCommonMetadata(ArrayRef< VPReplicateRecipe * > Recipes)
static VPValue * simplifyLogicalRecipe(VPSingleDefRecipe *Def, VPBuilder &Builder, bool CanCreateNewRecipe)
Try to simplify logical and bitwise recipes in Def.
static bool mergeReplicateRegionsIntoSuccessors(VPlan &Plan)
static Function * findVectorVariant(CallInst *CI, ArrayRef< VPValue * > Args, ElementCount VF, bool MaskRequired, PredicatedScalarEvolution &PSE, const Loop *L)
Find a vector variant of CI for VF, respecting MaskRequired.
static VPWidenInductionRecipe * getOptimizableIVOf(VPValue *VPV, PredicatedScalarEvolution &PSE)
Check if VPV is an untruncated wide induction, either before or after the increment.
static bool canNarrowLoad(VPSingleDefRecipe *WideMember0, unsigned OpIdx, VPValue *OpV, unsigned Idx, bool IsScalable)
Returns true if V is VPWidenLoadRecipe or VPInterleaveRecipe that can be converted to a narrower reci...
static void legalizeAndOptimizeInductions(VPlan &Plan)
Legalize VPWidenPointerInductionRecipe, by replacing it with a PtrAdd (IndStart, ScalarIVSteps (0,...
static void addReplicateRegions(VPlan &Plan)
static VPValue * optimizeLatchExitIVUserViaSCEV(VPlan &Plan, VPValue *Op, PredicatedScalarEvolution &PSE, VPValue *ResumeTC, const Loop *L)
static SmallVector< SmallVector< VPReplicateRecipe *, 4 > > collectGroupedReplicateMemOps(VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L, function_ref< bool(VPReplicateRecipe *)> FilterFn)
Collect either replicated Loads or Stores grouped by their address SCEV and their load-store type,...
static VPValue * tryToComputeEndValueForInduction(VPWidenInductionRecipe *WideIV, VPBuilder &VectorPHBuilder, VPValue *VectorTC)
Compute the end value for WideIV, unless it is truncated.
static bool replaceMaskWithCompareForScalarPlan(VPlan &Plan, ElementCount BestVF)
static void removeRedundantExpandSCEVRecipes(VPlan &Plan)
Remove redundant ExpandSCEVRecipes in Plan's entry block by replacing them with already existing reci...
static VPValue * optimizeEarlyExitInductionUser(VPlan &Plan, VPValue *Op, PredicatedScalarEvolution &PSE)
Attempts to optimize the induction variable exit values for users in the early exit block.
static VPValue * narrowInterleaveGroupOp(ArrayRef< VPValue * > Members, SmallPtrSetImpl< VPValue * > &NarrowedOps, VPBasicBlock *Preheader)
static VPValue * simplifyRecipe(VPSingleDefRecipe *Def)
Try to simplify VPSingleDefRecipe Def.
static VPValue * optimizeLatchExitInductionUser(VPlan &Plan, VPValue *Op, DenseMap< VPValue *, VPValue * > &EndValues, PredicatedScalarEvolution &PSE)
Attempts to optimize the induction variable exit values for users in the exit block coming from the l...
static void reassociateHeaderMask(VPlan &Plan)
Reassociate (headermask && x) && y -> headermask && (x && y) to allow the header mask to be simplifie...
static VPBasicBlock * getPredicatedThenBlock(VPRegionBlock *R)
If R is a triangle region, return the 'then' block of the triangle.
static bool canHoistOrSinkWithNoAliasCheck(const MemoryLocation &MemLoc, VPBasicBlock *FirstBB, VPBasicBlock *LastBB, std::optional< SinkStoreInfo > SinkInfo={})
Check if a memory operation doesn't alias with memory operations using scoped noalias metadata,...
static VPRegionBlock * createReplicateRegion(VPReplicateRecipe *PredRecipe, VPRegionBlock *ParentRegion, VPlan &Plan)
static void simplifyBlends(VPlan &Plan)
Normalize and simplify VPBlendRecipes.
static bool cannotHoistOrSinkRecipe(VPRecipeBase &R, VPBasicBlock *FirstBB, VPBasicBlock *LastBB, bool Sinking=false)
Return true if we do not know how to (mechanically) hoist or sink a non-memory or memory recipe R out...
static std::optional< Instruction::BinaryOps > getUnmaskedDivRemOpcode(Intrinsic::ID ID)
static bool isAlreadyNarrow(VPValue *VPV)
Returns true if VPValue is a narrow VPValue.
static bool canNarrowOps(ArrayRef< VPValue * > Ops, bool IsScalable)
static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan, ElementCount BestVF, unsigned BestUF)
Optimize the width of vector induction variables in Plan based on a known constant Trip Count,...
static VPExpressionRecipe * tryToMatchAndCreateMulAccumulateReduction(VPReductionRecipe *Red, VPCostContext &Ctx, VFRange &Range)
This function tries convert extended in-loop reductions to VPExpressionRecipe and clamp the Range if ...
static bool canSinkStoreWithNoAliasCheck(ArrayRef< VPReplicateRecipe * > StoresToSink, PredicatedScalarEvolution &PSE, const Loop &L)
static std::optional< bool > getStepDirection(const SCEV *S, ScalarEvolution &SE)
If S is an affine AddRec, returns true if its step is known to be positive and false if it is known t...
static void narrowToSingleScalarRecipes(VPlan &Plan)
This file provides utility VPlan to VPlan transformations.
#define RUN_VPLAN_PASS(PASS,...)
This file contains the declarations of the Vectorization Plan base classes:
static const X86InstrFMA3Group Groups[]
Value * RHS
Value * LHS
BinaryOperator * Mul
static const uint32_t IV[8]
Definition blake3_impl.h:83
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.
Definition APInt.h:78
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1077
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1533
APInt abs() const
Get the absolute value.
Definition APInt.h:1816
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1509
int32_t exactLogBase2() const
Definition APInt.h:1804
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:331
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1029
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:437
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1226
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
static APSInt getMinValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the minimum integer value with the given bit width and signedness.
Definition APSInt.h:310
static APSInt getMaxValue(uint32_t numBits, bool Unsigned)
Return the APSInt representing the maximum integer value with the given bit width and signedness.
Definition APSInt.h:302
@ NoAlias
The two locations do not alias at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & back() const
Get the last element.
Definition ArrayRef.h:150
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
const T & front() const
Get the first element.
Definition ArrayRef.h:144
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
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
Definition InstrTypes.h:765
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
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.
Definition DataLayout.h:64
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
A debug info location.
Definition DebugLoc.h:126
static DebugLoc getUnknown()
Definition DebugLoc.h:153
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.
Definition DenseMap.h:250
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
Definition DenseMap.h:260
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.
Definition Dominators.h:122
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition TypeSize.h:312
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
size_t arg_size() const
Definition Function.h:885
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...
bool isBinaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
bool isIntDivRem() const
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:348
The group of interleaved loads/stores sharing the same stride and close to each other.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
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.
Definition VPlan.cpp:1681
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
ValueT lookup(const KeyT &Key) const
Definition MapVector.h:110
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:118
bool empty() const
Definition MapVector.h:79
Representation for a specific memory location.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
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.
static bool isFindLastRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
RegionT * getParent() const
Get the parent of the Region.
Definition RegionInfo.h:362
This class represents a constant integer value.
ConstantInt * getValue() const
static const SCEV * rewrite(const SCEV *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 * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
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.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
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,...
static LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
size_type size() const
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.
iterator begin() const
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)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static LLVM_ABI PartialReductionExtendKind getPartialReductionExtendKind(Instruction *I)
Get the kind of extension that an instruction represents.
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
LLVM_ABI InstructionCost getPartialReductionCost(unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType, ElementCount VF, PartialReductionExtendKind OpAExtend, PartialReductionExtendKind OpBExtend, std::optional< unsigned > BinOp, TTI::TargetCostKind CostKind, std::optional< FastMathFlags > FMF) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements a switch-like dispatch statement for a value of 'T' using dyn_cast functionalit...
Definition TypeSwitch.h:89
TypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
Definition TypeSwitch.h:98
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
op_range operands()
Definition User.h:267
static SmallVector< VFInfo, 8 > getMappings(const CallInst &CI)
Retrieve all the VFInfo instances associated to the CallInst CI.
Definition VectorUtils.h:76
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 ...
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4400
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition VPlan.h:4475
iterator end()
Definition VPlan.h:4437
iterator begin()
Recipe iterator methods.
Definition VPlan.h:4435
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4488
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:266
VPBasicBlock * splitAt(iterator SplitAt)
Split current block at SplitAt by inserting a new block between the current block and its successors ...
Definition VPlan.cpp:584
const VPRecipeBase & front() const
Definition VPlan.h:4447
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
Definition VPlan.cpp:663
const VPRecipeBase & back() const
Definition VPlan.h:4449
A recipe for vectorizing a phi-node as a sequence of mask-based select instructions.
Definition VPlan.h:2963
VPValue * getIncomingValue(unsigned Idx) const
Return incoming value number Idx.
Definition VPlan.h:3010
VPValue * getMask(unsigned Idx) const
Return mask number Idx.
Definition VPlan.h:3015
unsigned getNumIncomingValues() const
Return the number of incoming values, taking into account when normalized the first incoming value wi...
Definition VPlan.h:3005
void setMask(unsigned Idx, VPValue *V)
Set mask number Idx to V.
Definition VPlan.h:3021
bool isNormalized() const
A normalized blend is one that has an odd number of operands, whereby the first operand does not have...
Definition VPlan.h:3001
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:93
void setSuccessors(ArrayRef< VPBlockBase * > NewSuccs)
Set each VPBasicBlock in NewSuccss as successor of this VPBlockBase.
Definition VPlan.h:314
VPRegionBlock * getParent()
Definition VPlan.h:191
const VPBasicBlock * getExitingBasicBlock() const
Definition VPlan.cpp:236
size_t getNumSuccessors() const
Definition VPlan.h:242
void setPredecessors(ArrayRef< VPBlockBase * > NewPreds)
Set each VPBasicBlock in NewPreds as predecessor of this VPBlockBase.
Definition VPlan.h:305
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:227
VPBlockBase * getSinglePredecessor() const
Definition VPlan.h:238
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:216
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:232
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:216
static auto blocksAs(T &&Range)
Return an iterator range over Range with each block cast to BlockTy.
Definition VPlanUtils.h:405
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To, VPBlockBase *BlockPtr)
Inserts BlockPtr on the edge between From and To.
Definition VPlanUtils.h:424
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.
Definition VPlanUtils.h:315
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:333
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To)
Disconnect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:351
static auto blocksOnly(T &&Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:387
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.
Definition VPlanUtils.h:371
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.
Definition VPlan.h:3513
VPlan-based builder utility analogous to IRBuilder.
VPInstruction * createFirstActiveLane(ArrayRef< VPValue * > Masks, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPWidenStoreRecipe * createWidenStore(StoreInst &Store, VPValue *Addr, VPValue *StoredVal, VPValue *Mask, bool Consecutive, const VPIRMetadata &Metadata, DebugLoc DL)
Create a recipe widening Store, storing StoredVal to Addr with Mask (may be null).
VPInstruction * createAdd(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", VPRecipeWithIRFlags::WrapFlagsTy WrapFlags={false, false})
VPInstruction * createOr(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPInstruction * createLogicalOr(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPWidenLoadRecipe * createWidenLoad(LoadInst &Load, VPValue *Addr, VPValue *Mask, bool Consecutive, const VPIRMetadata &Metadata, DebugLoc DL)
Create a recipe widening Load, loading from Addr with Mask (may be null).
VPInstruction * createNot(VPValue *Operand, 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 ...
Definition VPlan.cpp:1668
void setInsertPoint(const VPInsertPoint &IP)
Set the current insert point.
VPInstruction * createLogicalAnd(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={})
VPValue * createScalarZExtOrTrunc(VPValue *Op, Type *ResultTy, DebugLoc DL)
static VPBuilder getToInsertAfter(VPRecipeBase *R)
Create a VPBuilder to insert after R.
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 * 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.
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,...
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)
Create an N-ary operation with Opcode, Operands and set Inst as its underlying Instruction.
static VPSingleDefRecipe * createSingleScalarOp(unsigned Opcode, ArrayRef< VPValue * > Operands, VPValue *Mask, const VPIRFlags &Flags, const VPIRMetadata &Metadata, DebugLoc DL, Instruction *UV)
Create a single-scalar recipe with Opcode and Operands without inserting it.
unsigned getNumDefinedValues() const
Returns the number of values defined by the VPDef.
Definition VPlanValue.h:578
VPValue * getVPSingleValue()
Returns the only VPValue defined by the VPDef.
Definition VPlanValue.h:551
VPValue * getVPValue(unsigned I)
Returns the VPValue with index I defined by the VPDef.
Definition VPlanValue.h:563
ArrayRef< VPRecipeValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Definition VPlanValue.h:573
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B) const
A recipe to combine multiple recipes into a single 'expression' recipe, which should be considered a ...
Definition VPlan.h:3558
A pure virtual base class for all recipes modeling header phis, including phis for first order recurr...
Definition VPlan.h:2451
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2498
VPValue * getStartValue()
Returns the start value of the phi, if one is set.
Definition VPlan.h:2487
A recipe representing a sequence of load -> update -> store as part of a histogram operation.
Definition VPlan.h:2178
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:4553
Class to record and manage LLVM IR flags.
Definition VPlan.h:703
static 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
Helper to manage IR metadata for recipes.
Definition VPlan.h:1180
void intersect(const VPIRMetadata &MD)
Intersect this VPIRMetadata object with MD, keeping only metadata nodes that are common to both.
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1235
unsigned getNumOperandsWithoutMask() const
Returns the number of operands, excluding the mask if the VPInstruction is masked.
Definition VPlan.h:1485
@ ExtractLane
Extracts a single lane (first operand) from a set of vector operands.
Definition VPlan.h:1336
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1332
@ BuildVector
Creates a fixed-width vector containing all operands.
Definition VPlan.h:1281
@ ComputeReductionResult
Reduce the operands to the final reduction result using the operation specified via the operation's V...
Definition VPlan.h:1289
unsigned getOpcode() const
Definition VPlan.h:1429
VPValue * getMask() const
Returns the mask for the VPInstruction.
Definition VPlan.h:1501
const InterleaveGroup< Instruction > * getInterleaveGroup() const
Definition VPlan.h:3116
VPValue * getMask() const
Return the mask used by this recipe.
Definition VPlan.h:3108
ArrayRef< VPValue * > getStoredValues() const
Return the VPValues stored by this interleave group.
Definition VPlan.h:3137
VPInterleaveRecipe is a recipe for transforming an interleave group of load or stores into one wide l...
Definition VPlan.h:3147
VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when control converges back from ...
Definition VPlan.h:3719
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:410
VPRegionBlock * getRegion()
Definition VPlan.h:4799
VPBasicBlock * getParent()
Definition VPlan.h:482
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:560
void moveBefore(VPBasicBlock &BB, iplist< VPRecipeBase >::iterator I)
Unlink this recipe and insert into BB before I.
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.
iplist< VPRecipeBase >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
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...
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.
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.
Definition VPlanValue.h:354
A recipe for handling reduction phis.
Definition VPlan.h:2870
void setVFScaleFactor(unsigned ScaleFactor)
Set the VFScaleFactor for this reduction phi.
Definition VPlan.h:2921
unsigned getVFScaleFactor() const
Get the factor that the VF of this recipe's output should be scaled by, or 1 if it isn't scaled.
Definition VPlan.h:2914
RecurKind getRecurrenceKind() const
Returns the recurrence kind of the reduction.
Definition VPlan.h:2927
A recipe to represent inloop, ordered or partial reduction operations.
Definition VPlan.h:3240
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4625
const VPBlockBase * getEntry() const
Definition VPlan.h:4669
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4701
void setExiting(VPBlockBase *ExitingBlock)
Set ExitingBlock as the exiting VPBlockBase of this VPRegionBlock.
Definition VPlan.h:4686
Type * getCanonicalIVType() const
Return the type of the canonical IV for loop regions.
Definition VPlan.h:4753
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
Definition VPlan.h:4745
const VPBlockBase * getExiting() const
Definition VPlan.h:4681
VPRegionValue * getHeaderMask() const
Return the header mask of the region, or null if not set.
Definition VPlan.h:4758
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3405
bool isSingleScalar() const
Returns true if the recipe produces a single scalar value.
Definition VPlan.h:3464
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.
Definition VPlan.h:3492
bool isPredicated() const
Definition VPlan.h:3469
VPValue * getMask()
Return the mask of a predicated VPReplicateRecipe.
Definition VPlan.h:3486
Lightweight SCEV-to-VPlan expander.
Definition VPlanUtils.h:250
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...
Definition VPlan.h:4255
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Definition VPlan.h:618
Instruction * getUnderlyingInstr()
Returns the underlying instruction.
Definition VPlan.h:688
VPSingleDefRecipe * clone() override=0
Clone the current recipe.
A symbolic live-in VPValue, used for values like vector trip count, VF, and VFxUF.
Definition VPlanValue.h:217
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:401
operand_range operands()
Definition VPlanValue.h:474
void setOperand(unsigned I, VPValue *New)
Definition VPlanValue.h:447
unsigned getNumOperands() const
Definition VPlanValue.h:441
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:442
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:50
Type * getScalarType() const
Returns the scalar type of this VPValue, dispatching based on the concrete subclass.
Definition VPlan.cpp:149
Value * getLiveInIRValue() const
Return the underlying IR value for a VPIRValue.
Definition VPlan.cpp:143
bool isDefinedOutsideLoopRegions() const
Returns true if the VPValue is defined outside any loop.
Definition VPlan.cpp:1492
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:130
bool hasMoreThanOneUniqueUser() const
Returns true if the value has more than one unique user.
Definition VPlanValue.h:164
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
Definition VPlanValue.h:75
bool user_empty() const
Definition VPlanValue.h:161
bool hasOneUse() const
Definition VPlanValue.h:175
VPUser * getSingleUser()
Return the single user of this value, or nullptr if there is not exactly one user.
Definition VPlanValue.h:179
void replaceAllUsesWith(VPValue *New)
Definition VPlan.cpp:1495
void replaceUsesWithIf(VPValue *New, llvm::function_ref< bool(VPUser &U, unsigned Idx)> ShouldReplace)
Go through the uses list for this VPValue and make each use point to New if the callback ShouldReplac...
Definition VPlan.cpp:1501
user_range users()
Definition VPlanValue.h:157
A recipe to compute a pointer to the last element of each part of a widened memory access for widened...
Definition VPlan.h:2281
A recipe for widening Call instructions using library calls.
Definition VPlan.h:2112
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.
Definition VPlan.h:1894
Instruction::CastOps getOpcode() const
Definition VPlan.h:1930
A recipe for handling GEP instructions.
Definition VPlan.h:2221
Base class for widened induction (VPWidenIntOrFpInductionRecipe and VPWidenPointerInductionRecipe),...
Definition VPlan.h:2525
VPIRValue * getStartValue() const
Returns the start value of the induction.
Definition VPlan.h:2573
PHINode * getPHINode() const
Returns the underlying PHINode if one exists, or null otherwise.
Definition VPlan.h:2591
VPValue * getStepValue()
Returns the step value of the induction.
Definition VPlan.h:2576
const InductionDescriptor & getInductionDescriptor() const
Returns the induction descriptor for the recipe.
Definition VPlan.h:2596
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2625
TruncInst * getTruncInst()
Returns the first defined value as TruncInst, if it is one or nullptr otherwise.
Definition VPlan.h:2684
A recipe for widening vector intrinsics.
Definition VPlan.h:1941
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.
Definition VPlan.h:3755
virtual VPRecipeBase * getAsRecipe()=0
Return a VPRecipeBase* to the current object.
A recipe for widened phis.
Definition VPlan.h:2757
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1828
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenRecipe.
VPWidenRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1854
unsigned getOpcode() const
Definition VPlan.h:1873
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4812
VPIRValue * getLiveIn(Value *V) const
Return the live-in VPIRValue for V, if there is one or nullptr otherwise.
Definition VPlan.h:5151
bool hasVF(ElementCount VF) const
Definition VPlan.h:5044
const DataLayout & getDataLayout() const
Definition VPlan.h:5026
LLVMContext & getContext() const
Definition VPlan.h:5022
VPBasicBlock * getEntry()
Definition VPlan.h:4908
bool hasScalableVF() const
Definition VPlan.h:5045
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4980
VPValue * getOrCreateBackedgeTakenCount()
The backedge taken count of the original loop.
Definition VPlan.h:5001
iterator_range< SmallSetVector< ElementCount, 2 >::iterator > vectorFactors() const
Returns an iterator range over all VFs of the plan.
Definition VPlan.h:5051
VPIRValue * getFalse()
Return a VPIRValue wrapping i1 false.
Definition VPlan.h:5117
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:5020
VPIRValue * getAllOnesValue(Type *Ty)
Return a VPIRValue wrapping the AllOnes value of type Ty.
Definition VPlan.h:5123
VPRegionBlock * createReplicateRegion(VPBlockBase *Entry, VPBlockBase *Exiting, const std::string &Name="")
Create a new replicate region with Entry, Exiting and Name.
Definition VPlan.h:5202
auto getLiveIns() const
Return the list of live-in VPValues available in the VPlan.
Definition VPlan.h:5154
bool hasUF(unsigned UF) const
Definition VPlan.h:5069
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:4974
VPSymbolicValue & getVectorTripCount()
The vector trip count.
Definition VPlan.h:5010
VPValue * getBackedgeTakenCount() const
Definition VPlan.h:5007
VPIRValue * getOrAddLiveIn(Value *V)
Gets the live-in VPIRValue for V or adds a new live-in (if none exists yet) for V.
Definition VPlan.h:5094
VPIRValue * getZero(Type *Ty)
Return a VPIRValue wrapping the null value of type Ty.
Definition VPlan.h:5120
void setVF(ElementCount VF)
Definition VPlan.h:5032
bool isUnrolled() const
Returns true if the VPlan already has been unrolled, i.e.
Definition VPlan.h:5085
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1080
unsigned getConcreteUF() const
Returns the concrete UF of the plan, after unrolling.
Definition VPlan.h:5072
void resetTripCount(VPValue *NewTripCount)
Resets the trip count for the VPlan.
Definition VPlan.h:4994
VPBasicBlock * getMiddleBlock()
Returns the 'middle' block of the plan, that is the block that selects whether to execute the scalar ...
Definition VPlan.h:4950
VPBasicBlock * createVPBasicBlock(const Twine &Name, VPRecipeBase *Recipe=nullptr)
Create a new VPBasicBlock with Name and containing Recipe if present.
Definition VPlan.h:5177
VPIRValue * getTrue()
Return a VPIRValue wrapping i1 true.
Definition VPlan.h:5114
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4913
VPSymbolicValue & getUF()
Returns the UF of the vector loop region.
Definition VPlan.h:5017
bool hasScalarVFOnly() const
Definition VPlan.h:5062
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:4964
bool hasTailFolded() const
Returns true if the vector loop region is tail-folded.
Definition VPlan.h:4929
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:5013
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...
Definition VPlan.cpp:1240
VPIRValue * getConstantInt(Type *Ty, uint64_t Val, bool IsSigned=false)
Return a VPIRValue wrapping a ConstantInt with the given type and value.
Definition VPlan.h:5128
LLVM Value Representation.
Definition Value.h:75
iterator_range< user_iterator > users()
Definition Value.h:426
bool hasName() const
Definition Value.h:261
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS.multiplyCoefficientBy(X) will result in a value whose quantity matches ou...
Definition TypeSize.h:277
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:216
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr LeafTy multiplyCoefficientBy(ScalarTy RHS) const
Definition TypeSize.h:256
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
Definition TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#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.
Definition APInt.cpp:2799
std::variant< std::monostate, Loc::Single, Loc::Multi, Loc::MMI, Loc::EntryValue > Variant
Alias for the std::variant specialization base class of DbgVariable.
Definition DwarfDebug.h:190
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_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::URem > m_URem(const LHS &L, const RHS &R)
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.
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)
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))
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)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
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.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
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.
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()
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.
VPInstruction_match< VPInstruction::WideActiveLaneMask, Op0_t, Op1_t, Op2_t > m_WideActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
auto m_VPPhi(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::BranchOnTwoConds > m_BranchOnTwoConds()
AllRecipe_match< Opcode, Op0_t, Op1_t > m_Binary(const Op0_t &Op0, const Op1_t &Op1)
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)
specific_intval< 1 > m_False()
VPInstruction_match< VPInstruction::ExtractLastLane, Op0_t > m_ExtractLastLane(const Op0_t &Op0)
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)
specific_intval< 1 > m_True()
auto m_VPValue()
Match an arbitrary VPValue and ignore it.
VPInstruction_match< VPInstruction::ExtractVectorForPart, Op0_t, Op1_t > m_ExtractVectorForPart(const Op0_t &Op0, const Op1_t &Op1)
VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > m_ExtractLastPart(const Op0_t &Op0)
VPRecipeBase * findUserOf(VPValue *V, const MatchT &P)
If V is used by a recipe matching pattern P, return it.
VPInstruction_match< VPInstruction::Broadcast, Op0_t > m_Broadcast(const Op0_t &Op0)
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)
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
Definition RDFGraph.h:384
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...
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr)
Get or create a VPValue that corresponds to the expansion of Expr.
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...
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.
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.
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.
Definition VPlanUtils.h:149
bool isUniformAcrossVFsAndUFs(const VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
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,...
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...
Definition VPlanUtils.h:236
SmallVector< VPUser * > collectUsersRecursively(VPValue *V)
Collect all users of V, looking through recipes that define other values.
VPScalarIVStepsRecipe * createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind, Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp, Instruction *TruncI, VPIRValue *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...
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.
Definition STLExtras.h:315
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...
Definition VPlanCFG.h:262
@ Offset
Definition DWP.cpp:577
void stable_sort(R &&Range)
Definition STLExtras.h:2116
auto min_element(R &&Range)
Provide wrappers to std::min_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2078
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
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.
Definition STLExtras.h:1669
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.
Definition STLExtras.h:840
DenseMap< const Value *, const SCEV * > ValueToSCEVMapTy
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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).
constexpr from_range_t from_range
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.
Definition STLExtras.h:2208
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
auto cast_or_null(const Y &Val)
Definition Casting.h:714
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.
Definition VPlanCFG.h:250
constexpr auto bind_back(FnT &&Fn, BindArgsT &&...BindArgs)
C++23 bind_back.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
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...
Definition VPlanCFG.h:285
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2173
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.
Definition STLExtras.h:365
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:380
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2200
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition STLExtras.h:1694
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
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.
Definition VPlan.h:79
@ ReadOnly
No side effects to worry about, so we can process any uncountable exits in the loop and branch either...
Definition VPlan.h:83
@ MaskedHandleExitInScalarLoop
All memory operations other than the load(s) required to determine whether an uncountable exit occurr...
Definition VPlan.h:88
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
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...
Definition STLExtras.h:551
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...
Definition VPlan.cpp:1884
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...
Definition STLExtras.h:1837
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...
Definition Casting.h:547
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
Definition STLExtras.h:322
@ Other
Any other memory.
Definition ModRef.h:68
TargetTransformInfo TTI
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.
@ FMul
Product of floats.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ FAdd
Sum of floats.
@ 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...
Definition STLExtras.h:2012
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...
Definition STLExtras.h:2088
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
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.
Definition STLExtras.h:2166
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:305
LLVM_ABI std::optional< int64_t > getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy, Value *Ptr, PredicatedScalarEvolution &PSE)
If AR is an affine AddRec for Lp with a constant step, return the step in units of AccessTy's allocat...
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition STLExtras.h:2146
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...
Definition Loads.cpp:304
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:285
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
VPBasicBlock * EarlyExitingVPBB
VPIRBasicBlock * EarlyExitVPBB
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
An information struct used to provide DenseMap with the various necessary components for a given valu...
This reduction is unordered with the partial result scaled down by some factor.
Definition VPlan.h:2852
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.
Definition VPlan.cpp:1990
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
A VPValue representing a live-in from the input IR or a constant.
Definition VPlanValue.h:279
Type * getType() const
Returns the type of the underlying IR value.
Definition VPlan.cpp:147
A recipe for widening load operations, using the address to load from and an optional mask.
Definition VPlan.h:3819
A recipe for widening store operations, using the stored value, the address to store to and an option...
Definition VPlan.h:3918
static void simplifyLiveInsWithSCEV(VPlan &Plan, PredicatedScalarEvolution &PSE)
Check Plan's live-ins and replace them with constants, if they can be simplified via SCEV.
static decltype(auto) runPass(StringRef PassName, PassTy &&Pass, VPlan &Plan, ArgsTy &&...Args)
Helper to run a VPlan pass Pass on VPlan, forwarding extra arguments to the pass.
static void createInterleaveGroups(VPlan &Plan, const SmallPtrSetImpl< const InterleaveGroup< Instruction > * > &InterleaveGroups, const bool &EpilogueAllowed)
static LLVM_ABI_FOR_TEST bool tryToConvertVPInstructionsToVPRecipes(VPlan &Plan, const TargetLibraryInfo &TLI, PredicatedScalarEvolution &PSE, Loop *OuterLoop)
Replaces the VPInstructions in Plan with corresponding widen recipes.
static void createAndOptimizeReplicateRegions(VPlan &Plan)
Wrap predicated VPReplicateRecipes with a mask operand in an if-then region block and remove the mask...
static std::unique_ptr< VPlan > narrowInterleaveGroups(VPlan &Plan, const TargetTransformInfo &TTI)
Try to find a single VF among Plan's VFs for which all interleave groups (with known minimum VF eleme...
static void makeMemOpWideningDecisions(VPlan &Plan, VFRange &Range, VPRecipeBuilder &RecipeBuilder, VPCostContext &CostCtx)
Convert load/store VPInstructions in Plan into widened or replicate recipes.
static LLVM_ABI_FOR_TEST bool handleUncountableEarlyExits(VPlan &Plan, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC, UncountableExitStyle Style)
Update Plan to account for uncountable early exits by introducing appropriate branching logic in the ...
static void hoistPredicatedLoads(VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L)
Hoist predicated loads from the same address to the loop entry block, if they are guaranteed to execu...
static bool mergeBlocksIntoPredecessors(VPlan &Plan)
Remove redundant VPBasicBlocks by merging them into their single predecessor if the latter has a sing...
static void optimizeFindIVReductions(VPlan &Plan, PredicatedScalarEvolution &PSE, Loop &L)
Optimize FindLast reductions selecting IVs (or expressions of IVs) by converting them to FindIV reduc...
static void convertToAbstractRecipes(VPlan &Plan, VPCostContext &Ctx, VFRange &Range)
This function converts initial recipes to the abstract recipes and clamps Range based on cost model f...
static void makeScalarizationDecisions(VPlan &Plan, VFRange &Range)
Make VPlan-based scalarization decision prior to delegating to the ones made by the legacy CM.
static bool areAllLoadsDereferenceable(VPBasicBlock *HeaderVPBB, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC)
Check if all loads in the loop are dereferenceable.
static void optimizeInductionLiveOutUsers(VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L)
If there's a single exit block, optimize its phi recipes that use exiting IV values by feeding them p...
static void simplifyReverses(VPlan &Plan)
Cancel out redundant reverses in Plan, e.g. reverse(reverse(x)) -> x.
static void makeCallWideningDecisions(VPlan &Plan, VFRange &Range, VPRecipeBuilder &RecipeBuilder, VPCostContext &CostCtx)
Convert call VPInstructions in Plan into widened call, vector intrinsic or replicate recipes based on...
static void adjustFirstOrderRecurrenceMiddleUsers(VPlan &Plan, VFRange &Range)
Adjust first-order recurrence users in the middle block: create penultimate element extracts for LCSS...
static void removeDeadRecipes(VPlan &Plan)
Remove dead recipes from Plan.
static void simplifyRecipes(VPlan &Plan)
Perform instcombine-like simplifications on recipes in Plan.
static void sinkPredicatedStores(VPlan &Plan, PredicatedScalarEvolution &PSE, const Loop *L)
Sink predicated stores to the same address with complementary predicates (P and NOT P) to an uncondit...
static bool removeBranchOnConst(VPlan &Plan, bool OnlyLatches=false)
Remove BranchOnCond recipes with true or false conditions together with removing dead edges to their ...
static void convertToStridedAccesses(VPlan &Plan, PredicatedScalarEvolution &PSE, Loop &L, VPCostContext &Ctx, VFRange &Range)
Transform widen memory recipes into strided access recipes when legal and profitable.
static void clearReductionWrapFlags(VPlan &Plan)
Clear NSW/NUW flags from reduction instructions if necessary.
static void createPartialReductions(VPlan &Plan, VPCostContext &CostCtx, VFRange &Range)
Detect and create partial reduction recipes for scaled reductions in Plan.
static void cse(VPlan &Plan)
Perform common-subexpression-elimination on Plan.
static void replaceSymbolicStrides(VPlan &Plan, PredicatedScalarEvolution &PSE, const SymbolicStrideMap &StridesMap, const VPDominatorTree &VPDT)
Replace symbolic strides from StridesMap in Plan with constants when possible.
static LLVM_ABI_FOR_TEST void optimize(VPlan &Plan)
Apply VPlan-to-VPlan optimizations to Plan, including induction recipe optimizations,...
static void truncateToMinimalBitwidths(VPlan &Plan, const MapVector< Instruction *, uint64_t > &MinBWs)
Insert truncates and extends for any truncated recipe.
static void dropPoisonGeneratingRecipes(VPlan &Plan)
Drop poison flags from recipes that may generate a poison value that is used after vectorization,...
static void optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF, unsigned BestUF, PredicatedScalarEvolution &PSE)
Optimize Plan based on BestVF and BestUF.