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