Skip to content

Commit 2a3d849

Browse files
committed
[VPlan] Simplify select !c, x, y -> select c, y, x
This is split off from llvm#133993 On its own this simplification isn't that useful, but it allows us to make the equivalent VPBlendRecipe optimisation more generic by operating on VPInstructions. In order to actually test this without llvm#133993, I've had to also extend the m_Not pattern matcher to also catch VPWidenRecipes, since I couldn't really think of a straightforward way to create a VPInstruction::Select with a negated condition.
1 parent 4ad7b1c commit 2a3d849

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,6 @@ m_Freeze(const Op0_t &Op0) {
366366
return m_VPInstruction<Instruction::Freeze>(Op0);
367367
}
368368

369-
template <typename Op0_t>
370-
inline UnaryVPInstruction_match<Op0_t, VPInstruction::Not>
371-
m_Not(const Op0_t &Op0) {
372-
return m_VPInstruction<VPInstruction::Not>(Op0);
373-
}
374-
375369
template <typename Op0_t>
376370
inline UnaryVPInstruction_match<Op0_t, VPInstruction::BranchOnCond>
377371
m_BranchOnCond(const Op0_t &Op0) {
@@ -491,6 +485,15 @@ m_Select(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
491485
{Op0, Op1, Op2});
492486
}
493487

488+
template <typename Op0_t>
489+
inline match_combine_or<UnaryVPInstruction_match<Op0_t, VPInstruction::Not>,
490+
AllBinaryRecipe_match<int_pred_ty<is_all_ones>, Op0_t,
491+
Instruction::Xor, true>>
492+
m_Not(const Op0_t &Op0) {
493+
return m_CombineOr(m_VPInstruction<VPInstruction::Not>(Op0),
494+
m_c_Binary<Instruction::Xor>(m_AllOnes(), Op0));
495+
}
496+
494497
template <typename Op0_t, typename Op1_t>
495498
inline match_combine_or<
496499
BinaryVPInstruction_match<Op0_t, Op1_t, VPInstruction::LogicalAnd>,

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,15 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10821082
if (match(Def, m_Select(m_VPValue(), m_VPValue(X), m_Deferred(X))))
10831083
return Def->replaceAllUsesWith(X);
10841084

1085+
// select !c, x, y -> select c, y, x
1086+
VPValue *C;
1087+
if (match(Def, m_Select(m_Not(m_VPValue(C)), m_VPValue(X), m_VPValue(Y)))) {
1088+
Def->setOperand(0, C);
1089+
Def->setOperand(1, Y);
1090+
Def->setOperand(2, X);
1091+
return;
1092+
}
1093+
10851094
if (match(Def, m_c_Mul(m_VPValue(A), m_SpecificInt(1))))
10861095
return Def->replaceAllUsesWith(A);
10871096

llvm/test/Transforms/LoopVectorize/select-neg-cond.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ define void @neg_cond(ptr noalias %p, ptr noalias %q) {
1313
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[P]], i32 [[INDEX]]
1414
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[TMP0]], i32 0
1515
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP1]], align 4
16-
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <4 x i32> [[WIDE_LOAD]], splat (i32 42)
17-
; CHECK-NEXT: [[TMP3:%.*]] = xor <4 x i1> [[TMP2]], splat (i1 true)
16+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <4 x i32> [[WIDE_LOAD]], splat (i32 42)
1817
; CHECK-NEXT: [[TMP4:%.*]] = select <4 x i1> [[TMP3]], <4 x i32> splat (i32 42), <4 x i32> splat (i32 43)
1918
; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[TMP1]], align 4
2019
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4

0 commit comments

Comments
 (0)