Skip to content

Commit eb0c7e7

Browse files
authored
[VPlan] Replace BranchOnCount with Compare + BranchOnCond (NFC). (#172181)
Expand BranchOnCount to BranchOnCond + ICmp in convertToConcreteRecipes to simplify codegen. PR: #172181
1 parent cea15b6 commit eb0c7e7

File tree

7 files changed

+47
-46
lines changed

7 files changed

+47
-46
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
10541054
CalculateTripCountMinusVF,
10551055
// Increment the canonical IV separately for each unrolled part.
10561056
CanonicalIVIncrementForPart,
1057+
// Abstract instruction that compares two values and branches. This is
1058+
// lowered to ICmp + BranchOnCond during VPlan to VPlan transformation.
10571059
BranchOnCount,
10581060
BranchOnCond,
10591061
Broadcast,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -510,26 +510,6 @@ bool VPInstruction::canGenerateScalarForFirstLane() const {
510510
}
511511
}
512512

513-
/// Create a conditional branch using \p Cond branching to the successors of \p
514-
/// VPBB. Note that the first successor is always forward (i.e. not created yet)
515-
/// while the second successor may already have been created (if it is a header
516-
/// block and VPBB is a latch).
517-
static BranchInst *createCondBranch(Value *Cond, VPBasicBlock *VPBB,
518-
VPTransformState &State) {
519-
// Replace the temporary unreachable terminator with a new conditional
520-
// branch, hooking it up to backward destination (header) for latch blocks
521-
// now, and to forward destination(s) later when they are created.
522-
// Second successor may be backwards - iff it is already in VPBB2IRBB.
523-
VPBasicBlock *SecondVPSucc = cast<VPBasicBlock>(VPBB->getSuccessors()[1]);
524-
BasicBlock *SecondIRSucc = State.CFG.VPBB2IRBB.lookup(SecondVPSucc);
525-
BasicBlock *IRBB = State.CFG.VPBB2IRBB[VPBB];
526-
BranchInst *CondBr = State.Builder.CreateCondBr(Cond, IRBB, SecondIRSucc);
527-
// First successor is always forward, reset it to nullptr
528-
CondBr->setSuccessor(0, nullptr);
529-
IRBB->getTerminator()->eraseFromParent();
530-
return CondBr;
531-
}
532-
533513
Value *VPInstruction::generate(VPTransformState &State) {
534514
IRBuilderBase &Builder = State.Builder;
535515

@@ -659,17 +639,21 @@ Value *VPInstruction::generate(VPTransformState &State) {
659639
}
660640
case VPInstruction::BranchOnCond: {
661641
Value *Cond = State.get(getOperand(0), VPLane(0));
662-
auto *Br = createCondBranch(Cond, getParent(), State);
642+
// Replace the temporary unreachable terminator with a new conditional
643+
// branch, hooking it up to backward destination for latch blocks now, and
644+
// to forward destination(s) later when they are created.
645+
// Second successor may be backwards - iff it is already in VPBB2IRBB.
646+
VPBasicBlock *SecondVPSucc =
647+
cast<VPBasicBlock>(getParent()->getSuccessors()[1]);
648+
BasicBlock *SecondIRSucc = State.CFG.VPBB2IRBB.lookup(SecondVPSucc);
649+
BasicBlock *IRBB = State.CFG.VPBB2IRBB[getParent()];
650+
auto *Br = Builder.CreateCondBr(Cond, IRBB, SecondIRSucc);
651+
// First successor is always forward, reset it to nullptr.
652+
Br->setSuccessor(0, nullptr);
653+
IRBB->getTerminator()->eraseFromParent();
663654
applyMetadata(*Br);
664655
return Br;
665656
}
666-
case VPInstruction::BranchOnCount: {
667-
// First create the compare.
668-
Value *IV = State.get(getOperand(0), /*IsScalar*/ true);
669-
Value *TC = State.get(getOperand(1), /*IsScalar*/ true);
670-
Value *Cond = Builder.CreateICmpEQ(IV, TC);
671-
return createCondBranch(Cond, getParent(), State);
672-
}
673657
case VPInstruction::Broadcast: {
674658
return Builder.CreateVectorSplat(
675659
State.VF, State.get(getOperand(0), /*IsScalar*/ true), "broadcast");

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,27 +3230,25 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
32303230
CanonicalIV->eraseFromParent();
32313231

32323232
// Replace the use of VectorTripCount in the latch-exiting block.
3233-
// Before: (branch-on-count EVLIVInc, VectorTripCount)
3234-
// After: (branch-on-cond eq AVLNext, 0)
3235-
3233+
// Before: (branch-on-cond (icmp eq EVLIVInc, VectorTripCount))
3234+
// After: (branch-on-cond icmp eq AVLNext, 0)
32363235
VPBasicBlock *LatchExiting =
32373236
HeaderVPBB->getPredecessors()[1]->getEntryBasicBlock();
32383237
auto *LatchExitingBr = cast<VPInstruction>(LatchExiting->getTerminator());
3239-
// Skip single-iteration loop region
32403238
if (match(LatchExitingBr, m_BranchOnCond(m_True())))
32413239
return;
3242-
assert(LatchExitingBr &&
3243-
match(LatchExitingBr,
3244-
m_BranchOnCount(m_VPValue(EVLIncrement),
3245-
m_Specific(&Plan.getVectorTripCount()))) &&
3246-
"Unexpected terminator in EVL loop");
3240+
3241+
assert(match(LatchExitingBr, m_BranchOnCond(m_SpecificCmp(
3242+
CmpInst::ICMP_EQ, m_VPValue(EVLIncrement),
3243+
m_Specific(&Plan.getVectorTripCount())))) &&
3244+
"Expected BranchOnCond with ICmp comparing EVL increment with vector "
3245+
"trip count");
32473246

32483247
Type *AVLTy = VPTypeAnalysis(Plan).inferScalarType(AVLNext);
32493248
VPBuilder Builder(LatchExitingBr);
3250-
VPValue *Cmp = Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
3251-
Plan.getConstantInt(AVLTy, 0));
3252-
Builder.createNaryOp(VPInstruction::BranchOnCond, Cmp);
3253-
LatchExitingBr->eraseFromParent();
3249+
LatchExitingBr->setOperand(0,
3250+
Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
3251+
Plan.getConstantInt(AVLTy, 0)));
32543252
}
32553253

32563254
void VPlanTransforms::replaceSymbolicStrides(
@@ -3769,6 +3767,17 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
37693767
continue;
37703768
}
37713769

3770+
// Lower BranchOnCount to ICmp + BranchOnCond.
3771+
VPValue *IV, *TC;
3772+
if (match(&R, m_BranchOnCount(m_VPValue(IV), m_VPValue(TC)))) {
3773+
auto *BranchOnCountInst = cast<VPInstruction>(&R);
3774+
DebugLoc DL = BranchOnCountInst->getDebugLoc();
3775+
VPValue *Cond = Builder.createICmp(CmpInst::ICMP_EQ, IV, TC, DL);
3776+
Builder.createNaryOp(VPInstruction::BranchOnCond, Cond, DL);
3777+
ToRemove.push_back(BranchOnCountInst);
3778+
continue;
3779+
}
3780+
37723781
VPValue *VectorStep;
37733782
VPValue *ScalarStep;
37743783
if (!match(&R, m_VPInstruction<VPInstruction::WideIVStep>(
@@ -3882,6 +3891,7 @@ void VPlanTransforms::handleUncountableEarlyExit(VPBasicBlock *EarlyExitingVPBB,
38823891
// with one exiting if either the original condition of the vector latch is
38833892
// true or the early exit has been taken.
38843893
auto *LatchExitingBranch = cast<VPInstruction>(LatchVPBB->getTerminator());
3894+
// Skip single-iteration loop region
38853895
assert(LatchExitingBranch->getOpcode() == VPInstruction::BranchOnCount &&
38863896
"Unexpected terminator");
38873897
auto *IsLatchExitTaken =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ define i32 @print_partial_reduction(ptr %a, ptr %b) "target-features"="+neon,+do
9191
; CHECK-NEXT: WIDEN ir<%mul> = mul ir<%ext.b>, ir<%ext.a>
9292
; CHECK-NEXT: PARTIAL-REDUCE ir<[[RDX_NEXT]]> = ir<[[RDX]]> + reduce.add (ir<%mul>)
9393
; CHECK-NEXT: EMIT vp<[[EP_IV_NEXT:%.+]]> = add nuw vp<[[EP_IV]]>, ir<16>
94-
; CHECK-NEXT: EMIT branch-on-count vp<[[EP_IV_NEXT]]>, ir<1024>
94+
; CHECK-NEXT: EMIT vp<{{%.+}}> = icmp eq vp<%index.next>, ir<1024>
95+
; CHECK-NEXT: EMIT branch-on-cond vp<{{%.+}}>
9596
; CHECK-NEXT: Successor(s): middle.block, vector.body
9697
; CHECK-EMPTY:
9798
; CHECK-NEXT: middle.block:

llvm/test/Transforms/LoopVectorize/vplan-iv-transforms.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ define void @iv_expand(ptr %p, i64 %n) {
115115
; CHECK-NEXT: WIDEN store ir<%q>, ir<%y>
116116
; CHECK-NEXT: EMIT vp<%index.next> = add nuw vp<[[SCALAR_PHI]]>, ir<8>
117117
; CHECK-NEXT: EMIT vp<%vec.ind.next> = add ir<%iv>, vp<[[BROADCAST_INC]]>
118-
; CHECK-NEXT: EMIT branch-on-count vp<%index.next>, vp<%n.vec>
118+
; CHECK-NEXT: EMIT vp<{{%.+}}> = icmp eq vp<%index.next>, vp<%n.vec>
119+
; CHECK-NEXT: EMIT branch-on-cond vp<{{%.+}}>
119120
; CHECK-NEXT: Successor(s): middle.block, vector.body
120121
entry:
121122
br label %loop

llvm/test/Transforms/LoopVectorize/vplan-predicate-switch.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ define void @switch4_default_common_dest_with_case(ptr %start, ptr %end) {
8484
; CHECK-EMPTY:
8585
; CHECK-NEXT: default.2:
8686
; CHECK-NEXT: EMIT vp<[[CAN_IV_NEXT]]> = add nuw vp<[[CAN_IV]]>, ir<2>
87-
; CHECK-NEXT: EMIT branch-on-count vp<[[CAN_IV_NEXT]]>, vp<[[VTC]]>
87+
; CHECK-NEXT: EMIT vp<{{%.+}}> = icmp eq vp<[[CAN_IV_NEXT]]>, vp<[[VTC]]>
88+
; CHECK-NEXT: EMIT branch-on-cond vp<{{%.+}}>
8889
; CHECK-NEXT: Successor(s): middle.block, vector.body
8990
; CHECK-EMPTY:
9091
; CHECK-NEXT: middle.block:

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ define i32 @print_mulacc_sub(ptr %a, ptr %b) {
547547
; CHECK-NEXT: WIDEN ir<%mul> = mul ir<%ext.b>, ir<%ext.a>
548548
; CHECK-NEXT: REDUCE ir<%add> = ir<%accum> + reduce.sub (ir<%mul>)
549549
; CHECK-NEXT: EMIT vp<%index.next> = add nuw vp<%index>, ir<4>
550-
; CHECK-NEXT: EMIT branch-on-count vp<%index.next>, ir<1024>
550+
; CHECK-NEXT: EMIT vp<{{%.+}}> = icmp eq vp<%index.next>, ir<1024>
551+
; CHECK-NEXT: EMIT branch-on-cond vp<{{%.+}}>
551552
; CHECK-NEXT: Successor(s): middle.block, vector.body
552553
; CHECK-EMPTY:
553554
; CHECK-NEXT: middle.block:
@@ -667,7 +668,8 @@ define i32 @print_mulacc_negated(ptr %a, ptr %b) {
667668
; CHECK-NEXT: WIDEN ir<%sub> = sub ir<0>, ir<%mul>
668669
; CHECK-NEXT: REDUCE ir<%add> = ir<%accum> + reduce.add (ir<%sub>)
669670
; CHECK-NEXT: EMIT vp<%index.next> = add nuw vp<%index>, ir<4>
670-
; CHECK-NEXT: EMIT branch-on-count vp<%index.next>, ir<1024>
671+
; CHECK-NEXT: EMIT vp<{{%.+}}> = icmp eq vp<%index.next>, ir<1024>
672+
; CHECK-NEXT: EMIT branch-on-cond vp<{{%.+}}>
671673
; CHECK-NEXT: Successor(s): middle.block, vector.body
672674
; CHECK-EMPTY:
673675
; CHECK-NEXT: middle.block:

0 commit comments

Comments
 (0)