LLVM 23.0.0git
VPlanUnroll.cpp
Go to the documentation of this file.
1//===-- VPlanUnroll.cpp - VPlan unroller ----------------------------------===//
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 explicit unrolling for VPlans.
11///
12//===----------------------------------------------------------------------===//
13
14#include "VPRecipeBuilder.h"
15#include "VPlan.h"
16#include "VPlanAnalysis.h"
17#include "VPlanCFG.h"
18#include "VPlanHelpers.h"
19#include "VPlanPatternMatch.h"
20#include "VPlanTransforms.h"
21#include "VPlanUtils.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/ScopeExit.h"
26#include "llvm/IR/Intrinsics.h"
27
28using namespace llvm;
29using namespace llvm::VPlanPatternMatch;
30
31namespace {
32
33/// Helper to hold state needed for unrolling. It holds the Plan to unroll by
34/// UF. It also holds copies of VPValues across UF-1 unroll parts to facilitate
35/// the unrolling transformation, where the original VPValues are retained for
36/// part zero.
37class UnrollState {
38 /// Plan to unroll.
39 VPlan &Plan;
40 /// Unroll factor to unroll by.
41 const unsigned UF;
42 /// Analysis for types.
43 VPTypeAnalysis TypeInfo;
44
45 /// Unrolling may create recipes that should not be unrolled themselves.
46 /// Those are tracked in ToSkip.
47 SmallPtrSet<VPRecipeBase *, 8> ToSkip;
48
49 // Associate with each VPValue of part 0 its unrolled instances of parts 1,
50 // ..., UF-1.
51 DenseMap<VPValue *, SmallVector<VPValue *>> VPV2Parts;
52
53 /// Unroll replicate region \p VPR by cloning the region UF - 1 times.
54 void unrollReplicateRegionByUF(VPRegionBlock *VPR);
55
56 /// Unroll recipe \p R by cloning it UF - 1 times, unless it is uniform across
57 /// all parts.
58 void unrollRecipeByUF(VPRecipeBase &R);
59
60 /// Unroll header phi recipe \p R. How exactly the recipe gets unrolled
61 /// depends on the concrete header phi. Inserts newly created recipes at \p
62 /// InsertPtForPhi.
63 void unrollHeaderPHIByUF(VPHeaderPHIRecipe *R,
64 VPBasicBlock::iterator InsertPtForPhi);
65
66 /// Unroll a widen induction recipe \p IV. This introduces recipes to compute
67 /// the induction steps for each part.
68 void unrollWidenInductionByUF(VPWidenInductionRecipe *IV,
69 VPBasicBlock::iterator InsertPtForPhi);
70
71 VPValue *getConstantInt(unsigned Part) {
72 Type *CanIVIntTy = Plan.getVectorLoopRegion()->getCanonicalIVType();
73 return Plan.getConstantInt(CanIVIntTy, Part);
74 }
75
76public:
77 UnrollState(VPlan &Plan, unsigned UF) : Plan(Plan), UF(UF), TypeInfo(Plan) {}
78
79 void unrollBlock(VPBlockBase *VPB);
80
81 VPValue *getValueForPart(VPValue *V, unsigned Part) {
82 if (Part == 0 || isa<VPIRValue, VPSymbolicValue>(V))
83 return V;
84 assert((VPV2Parts.contains(V) && VPV2Parts[V].size() >= Part) &&
85 "accessed value does not exist");
86 return VPV2Parts[V][Part - 1];
87 }
88
89 /// Given a single original recipe \p OrigR (of part zero), and its copy \p
90 /// CopyR for part \p Part, map every VPValue defined by \p OrigR to its
91 /// corresponding VPValue defined by \p CopyR.
92 void addRecipeForPart(VPRecipeBase *OrigR, VPRecipeBase *CopyR,
93 unsigned Part) {
94 for (const auto &[Idx, VPV] : enumerate(OrigR->definedValues())) {
95 const auto &[V, _] = VPV2Parts.try_emplace(VPV);
96 assert(V->second.size() == Part - 1 && "earlier parts not set");
97 V->second.push_back(CopyR->getVPValue(Idx));
98 }
99 }
100
101 /// Given a uniform recipe \p R, add it for all parts.
102 void addUniformForAllParts(VPSingleDefRecipe *R) {
103 const auto &[V, Inserted] = VPV2Parts.try_emplace(R);
104 assert(Inserted && "uniform value already added");
105 for (unsigned Part = 0; Part != UF; ++Part)
106 V->second.push_back(R);
107 }
108
109 bool contains(VPValue *VPV) const { return VPV2Parts.contains(VPV); }
110
111 /// Update \p R's operand at \p OpIdx with its corresponding VPValue for part
112 /// \p P.
113 void remapOperand(VPRecipeBase *R, unsigned OpIdx, unsigned Part) {
114 auto *Op = R->getOperand(OpIdx);
115 R->setOperand(OpIdx, getValueForPart(Op, Part));
116 }
117
118 /// Update \p R's operands with their corresponding VPValues for part \p P.
119 void remapOperands(VPRecipeBase *R, unsigned Part) {
120 for (const auto &[OpIdx, Op] : enumerate(R->operands()))
121 R->setOperand(OpIdx, getValueForPart(Op, Part));
122 }
123};
124} // namespace
125
127 unsigned Part, VPlan &Plan,
128 VPTypeAnalysis &TypeInfo) {
129 if (Part == 0)
130 return;
131
132 VPBuilder Builder(Steps);
133 Type *BaseIVTy = TypeInfo.inferScalarType(Steps->getOperand(0));
134 Type *IntStepTy =
135 IntegerType::get(BaseIVTy->getContext(), BaseIVTy->getScalarSizeInBits());
136 VPValue *StartIndex = Steps->getVFValue();
137 if (Part > 1) {
138 StartIndex = Builder.createOverflowingOp(
139 Instruction::Mul,
140 {StartIndex,
141 Plan.getConstantInt(TypeInfo.inferScalarType(StartIndex), Part)});
142 }
143 StartIndex = Builder.createScalarSExtOrTrunc(
144 StartIndex, IntStepTy, TypeInfo.inferScalarType(StartIndex),
145 Steps->getDebugLoc());
146
147 if (BaseIVTy->isFloatingPointTy())
148 StartIndex = Builder.createScalarCast(Instruction::SIToFP, StartIndex,
149 BaseIVTy, Steps->getDebugLoc());
150
151 Steps->setStartIndex(StartIndex);
152}
153
154void UnrollState::unrollReplicateRegionByUF(VPRegionBlock *VPR) {
155 VPBlockBase *InsertPt = VPR->getSingleSuccessor();
156 for (unsigned Part = 1; Part != UF; ++Part) {
157 auto *Copy = VPR->clone();
158 VPBlockUtils::insertBlockBefore(Copy, InsertPt);
159
160 auto PartI = vp_depth_first_shallow(Copy->getEntry());
161 auto Part0 = vp_depth_first_shallow(VPR->getEntry());
162 for (const auto &[PartIVPBB, Part0VPBB] :
165 for (const auto &[PartIR, Part0R] : zip(*PartIVPBB, *Part0VPBB)) {
166 remapOperands(&PartIR, Part);
167 if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(&PartIR))
168 addStartIndexForScalarSteps(Steps, Part, Plan, TypeInfo);
169
170 addRecipeForPart(&Part0R, &PartIR, Part);
171 }
172 }
173 }
174}
175
176void UnrollState::unrollWidenInductionByUF(
177 VPWidenInductionRecipe *IV, VPBasicBlock::iterator InsertPtForPhi) {
178 VPBasicBlock *PH = cast<VPBasicBlock>(
179 IV->getParent()->getEnclosingLoopRegion()->getSinglePredecessor());
180 Type *IVTy = TypeInfo.inferScalarType(IV);
181 auto &ID = IV->getInductionDescriptor();
182 VPIRFlags Flags;
183 if (isa_and_present<FPMathOperator>(ID.getInductionBinOp()))
184 Flags = ID.getInductionBinOp()->getFastMathFlags();
185
186 VPValue *ScalarStep = IV->getStepValue();
187 VPBuilder Builder(PH);
188 Type *VectorStepTy =
189 IVTy->isPointerTy() ? TypeInfo.inferScalarType(ScalarStep) : IVTy;
190 VPInstruction *VectorStep = Builder.createNaryOp(
191 VPInstruction::WideIVStep, {&Plan.getVF(), ScalarStep}, VectorStepTy,
192 Flags, IV->getDebugLoc());
193
194 ToSkip.insert(VectorStep);
195
196 // Now create recipes to compute the induction steps for part 1 .. UF. Part 0
197 // remains the header phi. Parts > 0 are computed by adding Step to the
198 // previous part. The header phi recipe will get 2 new operands: the step
199 // value for a single part and the last part, used to compute the backedge
200 // value during VPWidenInductionRecipe::execute.
201 // %Part.0 = VPWidenInductionRecipe %Start, %ScalarStep, %VectorStep, %Part.3
202 // %Part.1 = %Part.0 + %VectorStep
203 // %Part.2 = %Part.1 + %VectorStep
204 // %Part.3 = %Part.2 + %VectorStep
205 //
206 // The newly added recipes are added to ToSkip to avoid interleaving them
207 // again.
208 VPValue *Prev = IV;
209 Builder.setInsertPoint(IV->getParent(), InsertPtForPhi);
210 unsigned AddOpc;
211 VPIRFlags AddFlags;
212 if (IVTy->isPointerTy()) {
214 AddFlags = GEPNoWrapFlags::none();
215 } else if (IVTy->isFloatingPointTy()) {
216 AddOpc = ID.getInductionOpcode();
217 AddFlags = Flags; // FMF flags
218 } else {
219 AddOpc = Instruction::Add;
220 AddFlags = VPIRFlags::getDefaultFlags(AddOpc);
222 AddFlags = VPIRFlags::WrapFlagsTy(/*NUW=*/true, /*NSW=*/false);
223 }
224 for (unsigned Part = 1; Part != UF; ++Part) {
225 std::string Name =
226 Part > 1 ? "step.add." + std::to_string(Part) : "step.add";
227
228 VPInstruction *Add =
229 Builder.createNaryOp(AddOpc,
230 {
231 Prev,
232 VectorStep,
233 },
234 AddFlags, IV->getDebugLoc(), Name);
235 ToSkip.insert(Add);
236 addRecipeForPart(IV, Add, Part);
237 Prev = Add;
238 }
239 IV->addOperand(VectorStep);
240 IV->addOperand(Prev);
241}
242
243void UnrollState::unrollHeaderPHIByUF(VPHeaderPHIRecipe *R,
244 VPBasicBlock::iterator InsertPtForPhi) {
245 // First-order recurrences pass a single vector or scalar through their header
246 // phis, irrespective of interleaving.
248 return;
249
250 // Generate step vectors for each unrolled part.
251 if (auto *IV = dyn_cast<VPWidenInductionRecipe>(R)) {
252 unrollWidenInductionByUF(IV, InsertPtForPhi);
253 return;
254 }
255
256 auto *RdxPhi = dyn_cast<VPReductionPHIRecipe>(R);
257 if (RdxPhi && RdxPhi->isOrdered())
258 return;
259
260 auto InsertPt = std::next(R->getIterator());
261 for (unsigned Part = 1; Part != UF; ++Part) {
262 VPRecipeBase *Copy = R->clone();
263 Copy->insertBefore(*R->getParent(), InsertPt);
264 addRecipeForPart(R, Copy, Part);
265 if (RdxPhi) {
266 // If the start value is a ReductionStartVector, use the identity value
267 // (second operand) for unrolled parts. If the scaling factor is > 1,
268 // create a new ReductionStartVector with the scale factor and both
269 // operands set to the identity value.
270 if (auto *VPI = dyn_cast<VPInstruction>(RdxPhi->getStartValue())) {
271 assert(VPI->getOpcode() == VPInstruction::ReductionStartVector &&
272 "unexpected start VPInstruction");
273 if (Part != 1)
274 continue;
275 VPValue *StartV;
276 if (match(VPI->getOperand(2), m_One())) {
277 StartV = VPI->getOperand(1);
278 } else {
279 auto *C = VPI->clone();
280 C->setOperand(0, C->getOperand(1));
281 C->insertAfter(VPI);
282 StartV = C;
283 }
284 for (unsigned Part = 1; Part != UF; ++Part)
285 VPV2Parts[VPI][Part - 1] = StartV;
286 }
287 } else {
289 "unexpected header phi recipe not needing unrolled part");
290 }
291 }
292}
293
294/// Handle non-header-phi recipes.
295void UnrollState::unrollRecipeByUF(VPRecipeBase &R) {
297 return;
298
299 if (auto *VPI = dyn_cast<VPInstruction>(&R)) {
301 addUniformForAllParts(VPI);
302 return;
303 }
304 }
305 if (auto *RepR = dyn_cast<VPReplicateRecipe>(&R)) {
306 if (isa<StoreInst>(RepR->getUnderlyingValue()) &&
307 RepR->getOperand(1)->isDefinedOutsideLoopRegions()) {
308 // Stores to an invariant address only need to store the last part.
309 remapOperands(&R, UF - 1);
310 return;
311 }
312 if (match(RepR,
314 addUniformForAllParts(RepR);
315 return;
316 }
317 }
318
319 // Unroll non-uniform recipes.
320 auto InsertPt = std::next(R.getIterator());
321 VPBasicBlock &VPBB = *R.getParent();
322 for (unsigned Part = 1; Part != UF; ++Part) {
323 VPRecipeBase *Copy = R.clone();
324 Copy->insertBefore(VPBB, InsertPt);
325 addRecipeForPart(&R, Copy, Part);
326
327 // Phi operands are updated once all other recipes have been unrolled.
328 if (isa<VPWidenPHIRecipe>(Copy))
329 continue;
330
331 VPValue *Op;
333 m_VPValue(), m_VPValue(Op)))) {
334 Copy->setOperand(0, getValueForPart(Op, Part - 1));
335 Copy->setOperand(1, getValueForPart(Op, Part));
336 continue;
337 }
338 if (auto *VPR = dyn_cast<VPVectorPointerRecipe>(&R)) {
339 VPBuilder Builder(VPR);
340 const DataLayout &DL =
342 Type *IndexTy = DL.getIndexType(TypeInfo.inferScalarType(VPR));
343 Type *VFTy = TypeInfo.inferScalarType(&Plan.getVF());
344 VPValue *VF = Builder.createScalarZExtOrTrunc(
345 &Plan.getVF(), IndexTy, VFTy, DebugLoc::getUnknown());
346 // VFxUF does not wrap, so VF * Part also cannot wrap.
347 VPValue *VFxPart = Builder.createOverflowingOp(
348 Instruction::Mul, {VF, Plan.getConstantInt(IndexTy, Part)},
349 {true, true});
350 Copy->setOperand(0, VPR->getOperand(0));
351 Copy->addOperand(VFxPart);
352 continue;
353 }
354 if (auto *Red = dyn_cast<VPReductionRecipe>(&R)) {
355 auto *Phi = dyn_cast<VPReductionPHIRecipe>(R.getOperand(0));
356 if (Phi && Phi->isOrdered()) {
357 auto &Parts = VPV2Parts[Phi];
358 if (Part == 1) {
359 Parts.clear();
360 Parts.push_back(Red);
361 }
362 Parts.push_back(Copy->getVPSingleValue());
363 Phi->setOperand(1, Copy->getVPSingleValue());
364 }
365 }
366 if (auto *VEPR = dyn_cast<VPVectorEndPointerRecipe>(Copy)) {
367 // Materialize PartN offset for VectorEndPointer.
368 VEPR->setOperand(0, R.getOperand(0));
369 VEPR->setOperand(1, R.getOperand(1));
370 VEPR->materializeOffset(Part);
371 continue;
372 }
373
374 remapOperands(Copy, Part);
375
376 if (auto *ScalarIVSteps = dyn_cast<VPScalarIVStepsRecipe>(Copy))
377 addStartIndexForScalarSteps(ScalarIVSteps, Part, Plan, TypeInfo);
378
379 // Add operand indicating the part to generate code for, to recipes still
380 // requiring it.
382 Copy->addOperand(getConstantInt(Part));
383
384 if (match(Copy,
386 VPBuilder Builder(Copy);
387 VPValue *ScaledByPart = Builder.createOverflowingOp(
388 Instruction::Mul, {Copy->getOperand(1), getConstantInt(Part)});
389 Copy->setOperand(1, ScaledByPart);
390 }
391 }
392 if (auto *VEPR = dyn_cast<VPVectorEndPointerRecipe>(&R)) {
393 // Materialize Part0 offset for VectorEndPointer.
394 VEPR->materializeOffset();
395 }
396}
397
398void UnrollState::unrollBlock(VPBlockBase *VPB) {
399 auto *VPR = dyn_cast<VPRegionBlock>(VPB);
400 if (VPR) {
401 if (VPR->isReplicator())
402 return unrollReplicateRegionByUF(VPR);
403
404 // Traverse blocks in region in RPO to ensure defs are visited before uses
405 // across blocks.
406 ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
407 RPOT(VPR->getEntry());
408 for (VPBlockBase *VPB : RPOT)
409 unrollBlock(VPB);
410 return;
411 }
412
413 // VPB is a VPBasicBlock; unroll it, i.e., unroll its recipes.
414 auto *VPBB = cast<VPBasicBlock>(VPB);
415 auto InsertPtForPhi = VPBB->getFirstNonPhi();
416 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
417 if (ToSkip.contains(&R) || isa<VPIRInstruction>(&R))
418 continue;
419
420 // Add all VPValues for all parts to AnyOf, FirstActiveLaneMask and
421 // Compute*Result which combine all parts to compute the final value.
422 VPValue *Op1;
424 match(&R, m_FirstActiveLane(m_VPValue(Op1))) ||
425 match(&R, m_LastActiveLane(m_VPValue(Op1))) ||
426 match(&R,
429 addUniformForAllParts(cast<VPInstruction>(&R));
430 for (unsigned Part = 1; Part != UF; ++Part)
431 R.addOperand(getValueForPart(Op1, Part));
432 continue;
433 }
434 VPValue *Op0;
435 if (match(&R, m_ExtractLane(m_VPValue(Op0), m_VPValue(Op1)))) {
436 addUniformForAllParts(cast<VPInstruction>(&R));
437 for (unsigned Part = 1; Part != UF; ++Part)
438 R.addOperand(getValueForPart(Op1, Part));
439 continue;
440 }
441
442 VPValue *Op2;
444 m_VPValue(Op2)))) {
445 addUniformForAllParts(cast<VPInstruction>(&R));
446 for (unsigned Part = 1; Part != UF; ++Part) {
447 R.addOperand(getValueForPart(Op1, Part));
448 R.addOperand(getValueForPart(Op2, Part));
449 }
450 continue;
451 }
452
453 if (Plan.hasScalarVFOnly()) {
454 if (match(&R, m_ExtractLastPart(m_VPValue(Op0))) ||
456 auto *I = cast<VPInstruction>(&R);
457 bool IsPenultimatePart =
459 unsigned PartIdx = IsPenultimatePart ? UF - 2 : UF - 1;
460 // For scalar VF, directly use the scalar part value.
461 I->replaceAllUsesWith(getValueForPart(Op0, PartIdx));
462 continue;
463 }
464 }
465 // For vector VF, the penultimate element is always extracted from the last part.
468 addUniformForAllParts(cast<VPSingleDefRecipe>(&R));
469 R.setOperand(0, getValueForPart(Op0, UF - 1));
470 continue;
471 }
472
473 auto *SingleDef = dyn_cast<VPSingleDefRecipe>(&R);
474 if (SingleDef && vputils::isUniformAcrossVFsAndUFs(SingleDef)) {
475 addUniformForAllParts(SingleDef);
476 continue;
477 }
478
479 if (auto *H = dyn_cast<VPHeaderPHIRecipe>(&R)) {
480 unrollHeaderPHIByUF(H, InsertPtForPhi);
481 continue;
482 }
483
484 unrollRecipeByUF(R);
485 }
486}
487
488void VPlanTransforms::unrollByUF(VPlan &Plan, unsigned UF) {
489 assert(UF > 0 && "Unroll factor must be positive");
490 Plan.setUF(UF);
491 llvm::scope_exit Cleanup([&Plan, UF]() {
492 auto Iter = vp_depth_first_deep(Plan.getEntry());
493 // Remove recipes that are redundant after unrolling.
495 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
496 auto *VPI = dyn_cast<VPInstruction>(&R);
497 if (VPI &&
498 VPI->getOpcode() == VPInstruction::CanonicalIVIncrementForPart &&
499 VPI->getOperand(1) == &Plan.getVF()) {
500 VPI->replaceAllUsesWith(VPI->getOperand(0));
501 VPI->eraseFromParent();
502 }
503 }
504 }
505
506 Type *TCTy = VPTypeAnalysis(Plan).inferScalarType(Plan.getTripCount());
507 Plan.getUF().replaceAllUsesWith(Plan.getConstantInt(TCTy, UF));
508 });
509 if (UF == 1) {
510 return;
511 }
512
513 UnrollState Unroller(Plan, UF);
514
515 // Iterate over all blocks in the plan starting from Entry, and unroll
516 // recipes inside them. This includes the vector preheader and middle blocks,
517 // which may set up or post-process per-part values.
519 Plan.getEntry());
520 for (VPBlockBase *VPB : RPOT)
521 Unroller.unrollBlock(VPB);
522
523 unsigned Part = 1;
524 // Remap operands of cloned header phis to update backedge values. The header
525 // phis cloned during unrolling are just after the header phi for part 0.
526 // Reset Part to 1 when reaching the first (part 0) recipe of a block.
527 for (VPRecipeBase &H :
529 // The second operand of Fixed Order Recurrence phi's, feeding the spliced
530 // value across the backedge, needs to remap to the last part of the spliced
531 // value.
533 Unroller.remapOperand(&H, 1, UF - 1);
534 continue;
535 }
536 if (Unroller.contains(H.getVPSingleValue())) {
537 Part = 1;
538 continue;
539 }
540 Unroller.remapOperands(&H, Part);
541 Part++;
542 }
543
545}
546
547/// Create a single-scalar clone of \p DefR (must be a VPReplicateRecipe,
548/// VPInstruction or VPScalarIVStepsRecipe) for lane \p Lane. Use \p
549/// Def2LaneDefs to look up scalar definitions for operands of \DefR.
550static VPValue *
551cloneForLane(VPlan &Plan, VPBuilder &Builder, Type *IdxTy,
552 VPSingleDefRecipe *DefR, VPLane Lane,
553 const DenseMap<VPValue *, SmallVector<VPValue *>> &Def2LaneDefs) {
555 "DefR must be a VPReplicateRecipe, VPInstruction or "
556 "VPScalarIVStepsRecipe");
557 VPValue *Op;
559 auto LaneDefs = Def2LaneDefs.find(Op);
560 if (LaneDefs != Def2LaneDefs.end())
561 return LaneDefs->second[Lane.getKnownLane()];
562
563 VPValue *Idx = Plan.getConstantInt(IdxTy, Lane.getKnownLane());
564 return Builder.createNaryOp(Instruction::ExtractElement, {Op, Idx});
565 }
566
567 // Collect the operands at Lane, creating extracts as needed.
569 for (VPValue *Op : DefR->operands()) {
570 // If Op is a definition that has been unrolled, directly use the clone for
571 // the corresponding lane.
572 auto LaneDefs = Def2LaneDefs.find(Op);
573 if (LaneDefs != Def2LaneDefs.end()) {
574 NewOps.push_back(LaneDefs->second[Lane.getKnownLane()]);
575 continue;
576 }
577 if (Lane.getKind() == VPLane::Kind::ScalableLast) {
578 // Look through mandatory Unpack.
579 [[maybe_unused]] bool Matched =
581 assert(Matched && "original op must have been Unpack");
582 auto *ExtractPart =
583 Builder.createNaryOp(VPInstruction::ExtractLastPart, {Op});
584 NewOps.push_back(
585 Builder.createNaryOp(VPInstruction::ExtractLastLane, {ExtractPart}));
586 continue;
587 }
589 NewOps.push_back(Op);
590 continue;
591 }
592
593 // Look through buildvector to avoid unnecessary extracts.
594 if (match(Op, m_BuildVector())) {
595 NewOps.push_back(
596 cast<VPInstruction>(Op)->getOperand(Lane.getKnownLane()));
597 continue;
598 }
599 VPValue *Idx = Plan.getConstantInt(IdxTy, Lane.getKnownLane());
600 VPValue *Ext = Builder.createNaryOp(Instruction::ExtractElement, {Op, Idx});
601 NewOps.push_back(Ext);
602 }
603
605 if (auto *RepR = dyn_cast<VPReplicateRecipe>(DefR)) {
606 // TODO: have cloning of replicate recipes also provide the desired result
607 // coupled with setting its operands to NewOps (deriving IsSingleScalar and
608 // Mask from the operands?)
609 New = new VPReplicateRecipe(RepR->getUnderlyingInstr(), NewOps,
610 /*IsSingleScalar=*/true, /*Mask=*/nullptr,
611 *RepR, *RepR, RepR->getDebugLoc());
612 } else {
613 New = DefR->clone();
614 for (const auto &[Idx, Op] : enumerate(NewOps)) {
615 New->setOperand(Idx, Op);
616 }
617 if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(New)) {
618 // Skip lane 0: an absent start index is implicitly zero.
619 unsigned KnownLane = Lane.getKnownLane();
620 if (KnownLane != 0) {
621 VPTypeAnalysis TypeInfo(Plan);
622 Type *BaseIVTy = TypeInfo.inferScalarType(DefR->getOperand(0));
623
624 VPValue *StartIndex = Steps->getStartIndex();
625 VPValue *LaneOffset;
626 unsigned AddOp;
627 VPIRFlags Flags;
628 if (BaseIVTy->isFloatingPointTy()) {
629 int SignedLane = static_cast<int>(KnownLane);
630 if (!StartIndex && Steps->getInductionOpcode() == Instruction::FSub)
631 SignedLane = -SignedLane;
632 LaneOffset =
633 Plan.getOrAddLiveIn(ConstantFP::get(BaseIVTy, SignedLane));
634 AddOp = Steps->getInductionOpcode();
635 Flags = VPIRFlags(FastMathFlags());
636 } else {
637 unsigned BaseIVBits = BaseIVTy->getScalarSizeInBits();
638 LaneOffset = Plan.getConstantInt(APInt(BaseIVBits, KnownLane,
639 /*isSigned*/ false,
640 /*implicitTrunc*/ true));
641 AddOp = Instruction::Add;
642 Flags = VPIRFlags(VPIRFlags::WrapFlagsTy(false, false));
643 }
644
645 if (StartIndex) {
646 VPBuilder LaneBuilder(DefR);
647 LaneOffset =
648 LaneBuilder.createNaryOp(AddOp, {StartIndex, LaneOffset}, Flags);
649 }
650 Steps->setStartIndex(LaneOffset);
651 }
652 }
653 }
654 New->insertBefore(DefR);
655 return New;
656}
657
659 if (Plan.hasScalarVFOnly())
660 return;
661
662 Type *IdxTy = IntegerType::get(
664
665 // Visit all VPBBs outside the loop region and directly inside the top-level
666 // loop region.
667 auto VPBBsOutsideLoopRegion = VPBlockUtils::blocksOnly<VPBasicBlock>(
669 auto VPBBsInsideLoopRegion = VPBlockUtils::blocksOnly<VPBasicBlock>(
671 auto VPBBsToUnroll =
672 concat<VPBasicBlock *>(VPBBsOutsideLoopRegion, VPBBsInsideLoopRegion);
673 // A mapping of current VPValue definitions to collections of new VPValues
674 // defined per lane. Serves to hook-up potential users of current VPValue
675 // definition that are replicated-per-VF later.
677 // The removal of current recipes being replaced by new ones needs to be
678 // delayed after Def2LaneDefs is no longer in use.
680 for (VPBasicBlock *VPBB : VPBBsToUnroll) {
681 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
684 cast<VPReplicateRecipe>(&R)->isSingleScalar()) ||
685 (isa<VPInstruction>(&R) &&
686 !cast<VPInstruction>(&R)->doesGeneratePerAllLanes() &&
688 continue;
689
690 auto *DefR = cast<VPSingleDefRecipe>(&R);
691 VPBuilder Builder(DefR);
692 if (DefR->getNumUsers() == 0) {
693 // Create single-scalar version of DefR for all lanes.
694 for (unsigned I = 0; I != VF.getKnownMinValue(); ++I)
695 cloneForLane(Plan, Builder, IdxTy, DefR, VPLane(I), Def2LaneDefs);
696 DefR->eraseFromParent();
697 continue;
698 }
699 /// Create single-scalar version of DefR for all lanes.
700 SmallVector<VPValue *> LaneDefs;
701 for (unsigned I = 0; I != VF.getKnownMinValue(); ++I)
702 LaneDefs.push_back(
703 cloneForLane(Plan, Builder, IdxTy, DefR, VPLane(I), Def2LaneDefs));
704
705 Def2LaneDefs[DefR] = LaneDefs;
706 /// Users that only demand the first lane can use the definition for lane
707 /// 0.
708 DefR->replaceUsesWithIf(LaneDefs[0], [DefR](VPUser &U, unsigned) {
709 return U.usesFirstLaneOnly(DefR);
710 });
711
712 // Update each build vector user that currently has DefR as its only
713 // operand, to have all LaneDefs as its operands.
714 for (VPUser *U : to_vector(DefR->users())) {
715 auto *VPI = dyn_cast<VPInstruction>(U);
716 if (!VPI || (VPI->getOpcode() != VPInstruction::BuildVector &&
717 VPI->getOpcode() != VPInstruction::BuildStructVector))
718 continue;
719 assert(VPI->getNumOperands() == 1 &&
720 "Build(Struct)Vector must have a single operand before "
721 "replicating by VF");
722 VPI->setOperand(0, LaneDefs[0]);
723 for (VPValue *LaneDef : drop_begin(LaneDefs))
724 VPI->addOperand(LaneDef);
725 }
726 ToRemove.push_back(DefR);
727 }
728 }
729 for (auto *R : reverse(ToRemove))
730 R->eraseFromParent();
731}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefInfo InstSet & ToRemove
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isCanonical(const MDString *S)
static const HTTPClientCleanup Cleanup
#define _
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
MachineInstr unsigned OpIdx
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:487
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
static ConstantInt * getConstantInt(Value *V, const DataLayout &DL)
Extract ConstantInt from value, looking through IntToPtr and PointerNullValue.
This file contains the declarations of different VPlan-related auxiliary helpers.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
This file provides utility VPlan to VPlan transformations.
static VPValue * cloneForLane(VPlan &Plan, VPBuilder &Builder, Type *IdxTy, VPSingleDefRecipe *DefR, VPLane Lane, const DenseMap< VPValue *, SmallVector< VPValue * > > &Def2LaneDefs)
Create a single-scalar clone of DefR (must be a VPReplicateRecipe, VPInstruction or VPScalarIVStepsRe...
static void addStartIndexForScalarSteps(VPScalarIVStepsRecipe *Steps, unsigned Part, VPlan &Plan, VPTypeAnalysis &TypeInfo)
static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry, DenseMap< VPValue *, VPValue * > &Old2NewVPValues)
Definition VPlan.cpp:1148
This file contains the declarations of the Vectorization Plan base classes:
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
static DebugLoc getUnknown()
Definition DebugLoc.h:161
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
static GEPNoWrapFlags none()
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:318
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:184
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4236
RecipeListTy::iterator iterator
Instruction iterators...
Definition VPlan.h:4263
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4324
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:232
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:82
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:182
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:210
static auto blocksOnly(const T &Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:269
static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr)
Insert disconnected block NewBlock before Blockptr.
Definition VPlanUtils.h:183
VPlan-based builder utility analogous to IRBuilder.
VPInstruction * createNaryOp(unsigned Opcode, ArrayRef< VPValue * > Operands, Instruction *Inst=nullptr, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Create an N-ary operation with Opcode, Operands and set Inst as its underlying Instruction.
VPValue * getVPValue(unsigned I)
Returns the VPValue with index I defined by the VPDef.
Definition VPlanValue.h:412
ArrayRef< VPRecipeValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Definition VPlanValue.h:422
BasicBlock * getIRBasicBlock() const
Definition VPlan.h:4413
Class to record and manage LLVM IR flags.
Definition VPlan.h:672
static VPIRFlags getDefaultFlags(unsigned Opcode)
Returns default flags for Opcode for opcodes that support it, asserts otherwise.
@ WideIVStep
Scale the first operand (vector step) by the second operand (scalar-step).
Definition VPlan.h:1285
@ Unpack
Extracts all lanes from its (non-scalable) vector operand.
Definition VPlan.h:1237
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1289
@ BuildVector
Creates a fixed-width vector containing all operands.
Definition VPlan.h:1232
@ BuildStructVector
Given operands of (the same) struct type, creates a struct of fixed- width vectors each containing a ...
Definition VPlan.h:1229
@ CanonicalIVIncrementForPart
Definition VPlan.h:1213
In what follows, the term "input IR" refers to code that is fed into the vectorizer whereas the term ...
Kind getKind() const
Returns the Kind of lane offset.
unsigned getKnownLane() const
Returns a compile-time known value for the lane index and asserts if the lane can only be calculated ...
@ ScalableLast
For ScalableLast, Lane is the offset from the start of the last N-element subvector in a scalable vec...
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:388
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:537
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4424
VPRegionBlock * clone() override
Clone all blocks in the single-entry single-exit region of the block and their recipes without updati...
Definition VPlan.cpp:749
const VPBlockBase * getEntry() const
Definition VPlan.h:4460
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4492
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3184
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
Definition VPlan.h:4040
void setStartIndex(VPValue *StartIndex)
Set or add the StartIndex operand.
Definition VPlan.h:4097
VPValue * getVFValue() const
Return the number of scalars to produce per unroll part, used to compute StartIndex during unrolling.
Definition VPlan.h:4088
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:589
VPSingleDefRecipe * clone() override=0
Clone the current recipe.
An analysis for type-inference for VPValues.
Type * inferScalarType(const VPValue *V)
Infer the type of V. Returns the scalar type of V.
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:258
operand_range operands()
Definition VPlanValue.h:326
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:297
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:46
void replaceAllUsesWith(VPValue *New)
Definition VPlan.cpp:1408
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4554
VPBasicBlock * getEntry()
Definition VPlan.h:4646
VPValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:4736
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4704
VPValue & getUF()
Returns the UF of the vector loop region.
Definition VPlan.h:4740
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:4806
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1038
bool hasScalarVFOnly() const
Definition VPlan.h:4774
VPIRBasicBlock * getScalarHeader() const
Return the VPIRBasicBlock wrapping the header of the scalar loop.
Definition VPlan.h:4690
void setUF(unsigned UF)
Definition VPlan.h:4789
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:4840
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool match(Val *V, const Pattern &P)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
VPInstruction_match< VPInstruction::ExtractLastLane, VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > > m_ExtractLastLaneOfLastPart(const Op0_t &Op0)
VPInstruction_match< VPInstruction::ComputeReductionResult, Op0_t > m_ComputeReductionResult(const Op0_t &Op0)
VPInstruction_match< VPInstruction::LastActiveLane, Op0_t > m_LastActiveLane(const Op0_t &Op0)
VPInstruction_match< VPInstruction::ExtractLastActive, Op0_t, Op1_t, Op2_t > m_ExtractLastActive(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< VPInstruction::BranchOnCount > m_BranchOnCount()
VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > m_ExtractLastPart(const Op0_t &Op0)
class_match< VPValue > m_VPValue()
Match an arbitrary VPValue and ignore it.
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)
VPInstruction_match< VPInstruction::FirstActiveLane, Op0_t > m_FirstActiveLane(const Op0_t &Op0)
bind_ty< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
VPInstruction_match< VPInstruction::ComputeAnyOfResult, Op0_t, Op1_t, Op2_t > m_ComputeAnyOfResult(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)
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
bool isUniformAcrossVFsAndUFs(VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
bool onlyFirstPartUsed(const VPValue *Def)
Returns true if only the first part of Def is used.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:831
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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:634
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:253
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:280
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1152
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
bool isa_and_present(const Y &Val)
isa_and_present<X> - Functionally identical to isa, except that a null value is accepted.
Definition Casting.h:669
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...
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
@ Add
Sum of integers.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static void unrollByUF(VPlan &Plan, unsigned UF)
Explicitly unroll Plan by UF.
static void removeDeadRecipes(VPlan &Plan)
Remove dead recipes from Plan.
static void replicateByVF(VPlan &Plan, ElementCount VF)
Replace each replicating VPReplicateRecipe and VPInstruction outside of any replicate region in Plan ...