Skip to content

Commit 876f977

Browse files
committed
[VPlan] Replace ExtractFromEnd with Extract(Last|Penultimate)Lane (NFC).
ExtractFromEnd only has 2 uses, extracting the last and penultimate lanes. Replace it with 2 separate opcodes, removing the need to materialize and handle a constant argument.
1 parent 0f5965f commit 876f977

13 files changed

+54
-53
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9326,8 +9326,6 @@ static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
93269326
cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
93279327
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
93289328
VPBuilder ScalarPHBuilder(ScalarPH);
9329-
VPValue *OneVPV = Plan.getOrAddLiveIn(
9330-
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 1));
93319329
for (VPRecipeBase &ScalarPhiR : Plan.getScalarHeader()->phis()) {
93329330
auto *ScalarPhiIRI = cast<VPIRPhi>(&ScalarPhiR);
93339331

@@ -9362,7 +9360,7 @@ static void addScalarResumePhis(VPRecipeBuilder &Builder, VPlan &Plan,
93629360
"Cannot handle loops with uncountable early exits");
93639361
if (IsFOR)
93649362
ResumeFromVectorLoop = MiddleBuilder.createNaryOp(
9365-
VPInstruction::ExtractFromEnd, {ResumeFromVectorLoop, OneVPV}, {},
9363+
VPInstruction::ExtractLastLane, {ResumeFromVectorLoop}, {},
93669364
"vector.recur.extract");
93679365
StringRef Name = IsFOR ? "scalar.recur.init" : "bc.merge.rdx";
93689366
auto *ResumePhiR = ScalarPHBuilder.createNaryOp(
@@ -9440,8 +9438,6 @@ static void addExitUsersForFirstOrderRecurrences(
94409438
auto *MiddleVPBB = Plan.getMiddleBlock();
94419439
VPBuilder ScalarPHBuilder(ScalarPHVPBB);
94429440
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
9443-
VPValue *TwoVPV = Plan.getOrAddLiveIn(
9444-
ConstantInt::get(Plan.getCanonicalIV()->getScalarType(), 2));
94459441

94469442
for (auto &HeaderPhi : VectorRegion->getEntryBasicBlock()->phis()) {
94479443
auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&HeaderPhi);
@@ -9525,7 +9521,7 @@ static void addExitUsersForFirstOrderRecurrences(
95259521
if (ExitIRI->getOperand(0) != FOR)
95269522
continue;
95279523
VPValue *PenultimateElement = MiddleBuilder.createNaryOp(
9528-
VPInstruction::ExtractFromEnd, {FOR->getBackedgeValue(), TwoVPV}, {},
9524+
VPInstruction::ExtractPenultimateLane, {FOR->getBackedgeValue()}, {},
95299525
"vector.recur.extract.for.phi");
95309526
ExitIRI->setOperand(0, PenultimateElement);
95319527
ExitUsersToFix.remove(ExitIRI);

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,10 @@ class VPInstruction : public VPRecipeWithIRFlags,
878878
Broadcast,
879879
ComputeFindLastIVResult,
880880
ComputeReductionResult,
881-
// Takes the VPValue to extract from as first operand and the lane or part
882-
// to extract as second operand, counting from the end starting with 1 for
883-
// last. The second operand must be a positive constant and <= VF.
884-
ExtractFromEnd,
881+
// Extracts the last lane from its operand.
882+
ExtractLastLane,
883+
// Extracts the second-to-last lane from its operand.
884+
ExtractPenultimateLane,
885885
LogicalAnd, // Non-poison propagating logical And.
886886
// Add an offset in bytes (second operand) to a base pointer (first
887887
// operand). Only generates scalar values (either for the first lane only or

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
8888
return SetResultTyFromOp();
8989
case VPInstruction::FirstActiveLane:
9090
return Type::getIntNTy(Ctx, 64);
91-
case VPInstruction::ExtractFromEnd: {
91+
case VPInstruction::ExtractLastLane:
92+
case VPInstruction::ExtractPenultimateLane: {
9293
Type *BaseTy = inferScalarType(R->getOperand(0));
9394
if (auto *VecTy = dyn_cast<VectorType>(BaseTy))
9495
return VecTy->getElementType();

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,9 @@ Value *VPInstruction::generate(VPTransformState &State) {
725725

726726
return ReducedPartRdx;
727727
}
728-
case VPInstruction::ExtractFromEnd: {
729-
auto *CI = cast<ConstantInt>(getOperand(1)->getLiveInIRValue());
730-
unsigned Offset = CI->getZExtValue();
731-
assert(Offset > 0 && "Offset from end must be positive");
728+
case VPInstruction::ExtractLastLane:
729+
case VPInstruction::ExtractPenultimateLane: {
730+
unsigned Offset = getOpcode() == VPInstruction::ExtractLastLane ? 1 : 2;
732731
Value *Res;
733732
if (State.VF.isVector()) {
734733
assert(Offset <= State.VF.getKnownMinValue() &&
@@ -854,7 +853,8 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
854853
}
855854

856855
bool VPInstruction::isVectorToScalar() const {
857-
return getOpcode() == VPInstruction::ExtractFromEnd ||
856+
return getOpcode() == VPInstruction::ExtractLastLane ||
857+
getOpcode() == VPInstruction::ExtractPenultimateLane ||
858858
getOpcode() == Instruction::ExtractElement ||
859859
getOpcode() == VPInstruction::FirstActiveLane ||
860860
getOpcode() == VPInstruction::ComputeFindLastIVResult ||
@@ -924,7 +924,8 @@ bool VPInstruction::opcodeMayReadOrWriteFromMemory() const {
924924
case VPInstruction::AnyOf:
925925
case VPInstruction::CalculateTripCountMinusVF:
926926
case VPInstruction::CanonicalIVIncrementForPart:
927-
case VPInstruction::ExtractFromEnd:
927+
case VPInstruction::ExtractLastLane:
928+
case VPInstruction::ExtractPenultimateLane:
928929
case VPInstruction::FirstActiveLane:
929930
case VPInstruction::FirstOrderRecurrenceSplice:
930931
case VPInstruction::LogicalAnd:
@@ -1042,8 +1043,11 @@ void VPInstruction::print(raw_ostream &O, const Twine &Indent,
10421043
case VPInstruction::Broadcast:
10431044
O << "broadcast";
10441045
break;
1045-
case VPInstruction::ExtractFromEnd:
1046-
O << "extract-from-end";
1046+
case VPInstruction::ExtractLastLane:
1047+
O << "extract-last-lane";
1048+
break;
1049+
case VPInstruction::ExtractPenultimateLane:
1050+
O << "extract-penultimate-lane";
10471051
break;
10481052
case VPInstruction::ComputeFindLastIVResult:
10491053
O << "compute-find-last-iv-result";
@@ -1143,12 +1147,7 @@ void VPIRInstruction::extractLastLaneOfOperand(VPBuilder &Builder) {
11431147
assert(getNumOperands() == 1 && "must have a single operand");
11441148
VPValue *Exiting = getOperand(0);
11451149
if (!Exiting->isLiveIn()) {
1146-
LLVMContext &Ctx = getInstruction().getContext();
1147-
auto &Plan = *getParent()->getPlan();
1148-
Exiting = Builder.createNaryOp(
1149-
VPInstruction::ExtractFromEnd,
1150-
{Exiting,
1151-
Plan.getOrAddLiveIn(ConstantInt::get(IntegerType::get(Ctx, 32), 1))});
1150+
Exiting = Builder.createNaryOp(VPInstruction::ExtractLastLane, {Exiting});
11521151
}
11531152
setOperand(0, Exiting);
11541153
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,8 @@ optimizeLatchExitInductionUser(VPlan &Plan, VPTypeAnalysis &TypeInfo,
827827
using namespace VPlanPatternMatch;
828828

829829
VPValue *Incoming;
830-
if (!match(Op, m_VPInstruction<VPInstruction::ExtractFromEnd>(
831-
m_VPValue(Incoming), m_SpecificInt(1))))
830+
if (!match(Op, m_VPInstruction<VPInstruction::ExtractLastLane>(
831+
m_VPValue(Incoming))))
832832
return nullptr;
833833

834834
auto *WideIV = getOptimizableIVOf(Incoming);

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,19 @@ void UnrollState::unrollBlock(VPBlockBase *VPB) {
337337
continue;
338338
}
339339
VPValue *Op0;
340-
if (match(&R, m_VPInstruction<VPInstruction::ExtractFromEnd>(
341-
m_VPValue(Op0), m_VPValue(Op1)))) {
340+
if (match(&R, m_VPInstruction<VPInstruction::ExtractLastLane>(
341+
m_VPValue(Op0))) ||
342+
match(&R, m_VPInstruction<VPInstruction::ExtractPenultimateLane>(
343+
m_VPValue(Op0)))) {
342344
addUniformForAllParts(cast<VPSingleDefRecipe>(&R));
343345
if (Plan.hasScalarVFOnly()) {
344346
// Extracting from end with VF = 1 implies retrieving the scalar part UF
345347
// - Op1.
346348
unsigned Offset =
347-
cast<ConstantInt>(Op1->getLiveInIRValue())->getZExtValue();
349+
match(&R,
350+
m_VPInstruction<VPInstruction::ExtractLastLane>(m_VPValue()))
351+
? 1
352+
: 2;
348353
R.getVPSingleValue()->replaceAllUsesWith(
349354
getValueForPart(Op0, UF - Offset));
350355
R.eraseFromParent();

llvm/test/Transforms/LoopVectorize/AArch64/vplan-printing.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ define i32 @print_partial_reduction(ptr %a, ptr %b) {
4242
; CHECK-EMPTY:
4343
; CHECK-NEXT: middle.block:
4444
; CHECK-NEXT: EMIT vp<[[RED_RESULT:%.+]]> = compute-reduction-result ir<[[ACC]]>, ir<[[REDUCE]]>
45-
; CHECK-NEXT: EMIT vp<[[EXTRACT:%.+]]> = extract-from-end vp<[[RED_RESULT]]>, ir<1>
45+
; CHECK-NEXT: EMIT vp<[[EXTRACT:%.+]]> = extract-last-lane vp<[[RED_RESULT]]>
4646
; CHECK-NEXT: EMIT vp<[[CMP:%.+]]> = icmp eq ir<1024>, vp<[[VEC_TC]]>
4747
; CHECK-NEXT: EMIT branch-on-cond vp<[[CMP]]>
4848
; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph
@@ -107,7 +107,7 @@ define i32 @print_partial_reduction(ptr %a, ptr %b) {
107107
; CHECK-EMPTY:
108108
; CHECK-NEXT: middle.block:
109109
; CHECK-NEXT: EMIT vp<[[RED_RESULT:%.+]]> = compute-reduction-result ir<%accum>, ir<%add>
110-
; CHECK-NEXT: EMIT vp<[[EXTRACT:%.+]]> = extract-from-end vp<[[RED_RESULT]]>, ir<1>
110+
; CHECK-NEXT: EMIT vp<[[EXTRACT:%.+]]> = extract-last-lane vp<[[RED_RESULT]]>
111111
; CHECK-NEXT: EMIT vp<[[CMP:%.+]]> = icmp eq ir<1024>, ir<1024>
112112
; CHECK-NEXT: EMIT branch-on-cond vp<[[CMP]]>
113113
; CHECK-NEXT: Successor(s): ir-bb<exit>, ir-bb<scalar.ph>

llvm/test/Transforms/LoopVectorize/RISCV/vplan-vp-intrinsics-reduction.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ define i32 @reduction(ptr %a, i64 %n, i32 %start) {
6262
; IF-EVL-OUTLOOP-EMPTY:
6363
; IF-EVL-OUTLOOP-NEXT: middle.block:
6464
; IF-EVL-OUTLOOP-NEXT: EMIT vp<[[RDX:%.+]]> = compute-reduction-result ir<[[RDX_PHI]]>, vp<[[RDX_SELECT]]>
65-
; IF-EVL-OUTLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-from-end vp<[[RDX]]>, ir<1>
65+
; IF-EVL-OUTLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-last-lane vp<[[RDX]]>
6666
; IF-EVL-OUTLOOP-NEXT: Successor(s): ir-bb<for.end>
6767
; IF-EVL-OUTLOOP-EMPTY:
6868
; IF-EVL-OUTLOOP-NEXT: ir-bb<for.end>:
@@ -102,7 +102,7 @@ define i32 @reduction(ptr %a, i64 %n, i32 %start) {
102102
; IF-EVL-INLOOP-EMPTY:
103103
; IF-EVL-INLOOP-NEXT: middle.block:
104104
; IF-EVL-INLOOP-NEXT: EMIT vp<[[RDX:%.+]]> = compute-reduction-result ir<[[RDX_PHI]]>, ir<[[ADD]]>
105-
; IF-EVL-INLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-from-end vp<[[RDX]]>, ir<1>
105+
; IF-EVL-INLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-last-lane vp<[[RDX]]>
106106
; IF-EVL-INLOOP-NEXT: Successor(s): ir-bb<for.end>
107107
; IF-EVL-INLOOP-EMPTY:
108108
; IF-EVL-INLOOP-NEXT: ir-bb<for.end>:
@@ -137,7 +137,7 @@ define i32 @reduction(ptr %a, i64 %n, i32 %start) {
137137
; NO-VP-OUTLOOP-EMPTY:
138138
; NO-VP-OUTLOOP-NEXT: middle.block:
139139
; NO-VP-OUTLOOP-NEXT: EMIT vp<[[RDX:%.+]]> = compute-reduction-result ir<[[RDX_PHI]]>, ir<[[ADD]]>
140-
; NO-VP-OUTLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-from-end vp<[[RDX]]>, ir<1>
140+
; NO-VP-OUTLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-last-lane vp<[[RDX]]>
141141
; NO-VP-OUTLOOP-NEXT: EMIT vp<[[BOC:%.+]]> = icmp eq ir<%n>, vp<[[VTC]]>
142142
; NO-VP-OUTLOOP-NEXT: EMIT branch-on-cond vp<[[BOC]]>
143143
; NO-VP-OUTLOOP-NEXT: Successor(s): ir-bb<for.end>, scalar.ph
@@ -185,7 +185,7 @@ define i32 @reduction(ptr %a, i64 %n, i32 %start) {
185185
; NO-VP-INLOOP-EMPTY:
186186
; NO-VP-INLOOP-NEXT: middle.block:
187187
; NO-VP-INLOOP-NEXT: EMIT vp<[[RDX:%.+]]> = compute-reduction-result ir<[[RDX_PHI]]>, ir<[[ADD]]>
188-
; NO-VP-INLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-from-end vp<[[RDX]]>, ir<1>
188+
; NO-VP-INLOOP-NEXT: EMIT vp<[[RDX_EX:%.+]]> = extract-last-lane vp<[[RDX]]>
189189
; NO-VP-INLOOP-NEXT: EMIT vp<[[BOC:%.+]]> = icmp eq ir<%n>, vp<[[VTC]]>
190190
; NO-VP-INLOOP-NEXT: EMIT branch-on-cond vp<[[BOC]]>
191191
; NO-VP-INLOOP-NEXT: Successor(s): ir-bb<for.end>, scalar.ph

llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains-vplan.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ define void @test_chained_first_order_recurrences_1(ptr %ptr) {
3737
; CHECK-NEXT: Successor(s): middle.block
3838
; CHECK-EMPTY:
3939
; CHECK-NEXT: middle.block:
40-
; CHECK-NEXT: EMIT vp<[[RESUME_1:%.+]]> = extract-from-end ir<%for.1.next>, ir<1>
41-
; CHECK-NEXT: EMIT vp<[[RESUME_2:%.+]]>.1 = extract-from-end vp<[[FOR1_SPLICE]]>, ir<1>
40+
; CHECK-NEXT: EMIT vp<[[RESUME_1:%.+]]> = extract-last-lane ir<%for.1.next>
41+
; CHECK-NEXT: EMIT vp<[[RESUME_2:%.+]]>.1 = extract-last-lane vp<[[FOR1_SPLICE]]>
4242
; CHECK-NEXT: EMIT vp<[[CMP:%.+]]> = icmp eq ir<1000>, vp<[[VTC]]>
4343
; CHECK-NEXT: EMIT branch-on-cond vp<[[CMP]]>
4444
; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph
@@ -117,9 +117,9 @@ define void @test_chained_first_order_recurrences_3(ptr %ptr) {
117117
; CHECK-NEXT: Successor(s): middle.block
118118
; CHECK-EMPTY:
119119
; CHECK-NEXT: middle.block:
120-
; CHECK-NEXT: EMIT vp<[[RESUME_1:%.+]]> = extract-from-end ir<%for.1.next>, ir<1>
121-
; CHECK-NEXT: EMIT vp<[[RESUME_2:%.+]]>.1 = extract-from-end vp<[[FOR1_SPLICE]]>, ir<1>
122-
; CHECK-NEXT: EMIT vp<[[RESUME_3:%.+]]>.2 = extract-from-end vp<[[FOR2_SPLICE]]>, ir<1>
120+
; CHECK-NEXT: EMIT vp<[[RESUME_1:%.+]]> = extract-last-lane ir<%for.1.next>
121+
; CHECK-NEXT: EMIT vp<[[RESUME_2:%.+]]>.1 = extract-last-lane vp<[[FOR1_SPLICE]]>
122+
; CHECK-NEXT: EMIT vp<[[RESUME_3:%.+]]>.2 = extract-last-lane vp<[[FOR2_SPLICE]]>
123123
; CHECK-NEXT: EMIT vp<[[CMP:%.+]]> = icmp eq ir<1000>, vp<[[VTC]]>
124124
; CHECK-NEXT: EMIT branch-on-cond vp<[[CMP]]>
125125
; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph
@@ -203,8 +203,8 @@ define i32 @test_chained_first_order_recurrences_4(ptr %base, i64 %x) {
203203
; CHECK-NEXT: Successor(s): middle.block
204204
; CHECK-EMPTY:
205205
; CHECK-NEXT: middle.block:
206-
; CHECK-NEXT: EMIT vp<[[EXT_X:%.+]]> = extract-from-end ir<%for.x.next>, ir<1>
207-
; CHECK-NEXT: EMIT vp<[[EXT_Y:%.+]]>.1 = extract-from-end ir<%for.x.prev>, ir<1>
206+
; CHECK-NEXT: EMIT vp<[[EXT_X:%.+]]> = extract-last-lane ir<%for.x.next>
207+
; CHECK-NEXT: EMIT vp<[[EXT_Y:%.+]]>.1 = extract-last-lane ir<%for.x.prev>
208208
; CHECK-NEXT: EMIT vp<[[MIDDLE_C:%.+]]> = icmp eq ir<4098>, vp<[[VTC]]>
209209
; CHECK-NEXT: EMIT branch-on-cond vp<[[MIDDLE_C]]>
210210
; CHECK-NEXT: Successor(s): ir-bb<ret>, scalar.ph
@@ -282,8 +282,8 @@ define i32 @test_chained_first_order_recurrences_5_hoist_to_load(ptr %base) {
282282
; CHECK-NEXT: Successor(s): middle.block
283283
; CHECK-EMPTY:
284284
; CHECK-NEXT: middle.block:
285-
; CHECK-NEXT: EMIT vp<[[EXT_X:%.+]]> = extract-from-end ir<%for.x.next>, ir<1>
286-
; CHECK-NEXT: EMIT vp<[[EXT_Y:%.+]]>.1 = extract-from-end ir<%for.x.prev>, ir<1>
285+
; CHECK-NEXT: EMIT vp<[[EXT_X:%.+]]> = extract-last-lane ir<%for.x.next>
286+
; CHECK-NEXT: EMIT vp<[[EXT_Y:%.+]]>.1 = extract-last-lane ir<%for.x.prev>
287287
; CHECK-NEXT: EMIT vp<[[MIDDLE_C:%.+]]> = icmp eq ir<4098>, vp<[[VTC]]>
288288
; CHECK-NEXT: EMIT branch-on-cond vp<[[MIDDLE_C]]>
289289
; CHECK-NEXT: Successor(s): ir-bb<ret>, scalar.ph

llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ define i32 @sink_replicate_region_3_reduction(i32 %x, i8 %y, ptr %ptr) optsize {
214214
; CHECK-EMPTY:
215215
; CHECK-NEXT: middle.block:
216216
; CHECK-NEXT: EMIT vp<[[RED_RES:%.+]]> = compute-reduction-result ir<%and.red>, vp<[[SEL]]>
217-
; CHECK-NEXT: EMIT vp<[[RED_EX:%.+]]> = extract-from-end vp<[[RED_RES]]>, ir<1>
217+
; CHECK-NEXT: EMIT vp<[[RED_EX:%.+]]> = extract-last-lane vp<[[RED_RES]]>
218218
; CHECK-NEXT: Successor(s): ir-bb<exit>
219219
; CHECK-EMPTY:
220220
; CHECK-NEXT: ir-bb<exit>

0 commit comments

Comments
 (0)