Skip to content

Commit eb7f6a5

Browse files
authored
[VPlan] Simplify (x && y) || (x && z) -> x && (y || z) (#156308)
Split off from #155383, since it turns out this has a diff on its own.
1 parent fb7c0d7 commit eb7f6a5

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10721072
// TODO: Split up into simpler, modular combines: (X && Y) || (X && Z) into X
10731073
// && (Y || Z) and (X || !X) into true. This requires queuing newly created
10741074
// recipes to be visited during simplification.
1075-
VPValue *X, *Y;
1075+
VPValue *X, *Y, *Z;
10761076
if (match(Def,
10771077
m_c_BinaryOr(m_LogicalAnd(m_VPValue(X), m_VPValue(Y)),
10781078
m_LogicalAnd(m_Deferred(X), m_Not(m_Deferred(Y)))))) {
@@ -1096,6 +1096,17 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10961096
return;
10971097
}
10981098

1099+
// (x && y) || (x && z) -> x && (y || z)
1100+
VPBuilder Builder(Def);
1101+
if (match(Def, m_c_BinaryOr(m_LogicalAnd(m_VPValue(X), m_VPValue(Y)),
1102+
m_LogicalAnd(m_Deferred(X), m_VPValue(Z)))) &&
1103+
// Simplify only if one of the operands has one use to avoid creating an
1104+
// extra recipe.
1105+
(!Def->getOperand(0)->hasMoreThanOneUniqueUser() ||
1106+
!Def->getOperand(1)->hasMoreThanOneUniqueUser()))
1107+
return Def->replaceAllUsesWith(
1108+
Builder.createLogicalAnd(X, Builder.createOr(Y, Z)));
1109+
10991110
if (match(Def, m_Select(m_VPValue(), m_VPValue(X), m_Deferred(X))))
11001111
return Def->replaceAllUsesWith(X);
11011112

@@ -1162,7 +1173,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
11621173
m_VPValue(X), m_SpecificInt(1)))) {
11631174
Type *WideStepTy = TypeInfo.inferScalarType(Def);
11641175
if (TypeInfo.inferScalarType(X) != WideStepTy)
1165-
X = VPBuilder(Def).createWidenCast(Instruction::Trunc, X, WideStepTy);
1176+
X = Builder.createWidenCast(Instruction::Trunc, X, WideStepTy);
11661177
Def->replaceAllUsesWith(X);
11671178
return;
11681179
}

llvm/test/Transforms/LoopVectorize/RISCV/dead-ops-cost.ll

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,19 +315,12 @@ define void @test_phi_in_latch_redundant(ptr %dst, i32 %a) {
315315
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[INDUCTION]], %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
316316
; CHECK-NEXT: [[AVL:%.*]] = phi i64 [ 37, %[[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
317317
; CHECK-NEXT: [[TMP8:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
318-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP8]], i64 0
319-
; CHECK-NEXT: [[BROADCAST_SPLAT4:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT3]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
320318
; CHECK-NEXT: [[TMP5:%.*]] = zext i32 [[TMP8]] to i64
321319
; CHECK-NEXT: [[TMP9:%.*]] = mul i64 9, [[TMP5]]
322320
; CHECK-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP9]], i64 0
323321
; CHECK-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
324-
; CHECK-NEXT: [[TMP11:%.*]] = call <vscale x 4 x i32> @llvm.stepvector.nxv4i32()
325-
; CHECK-NEXT: [[TMP12:%.*]] = icmp ult <vscale x 4 x i32> [[TMP11]], [[BROADCAST_SPLAT4]]
326-
; CHECK-NEXT: [[TMP13:%.*]] = select <vscale x 4 x i1> [[TMP12]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i1> zeroinitializer
327-
; CHECK-NEXT: [[TMP14:%.*]] = select <vscale x 4 x i1> [[TMP12]], <vscale x 4 x i1> zeroinitializer, <vscale x 4 x i1> zeroinitializer
328-
; CHECK-NEXT: [[TMP15:%.*]] = or <vscale x 4 x i1> [[TMP13]], [[TMP14]]
329322
; CHECK-NEXT: [[TMP16:%.*]] = getelementptr i32, ptr [[DST]], <vscale x 4 x i64> [[VEC_IND]]
330-
; CHECK-NEXT: call void @llvm.vp.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP19]], <vscale x 4 x ptr> align 4 [[TMP16]], <vscale x 4 x i1> [[TMP15]], i32 [[TMP8]])
323+
; CHECK-NEXT: call void @llvm.vp.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP19]], <vscale x 4 x ptr> align 4 [[TMP16]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP8]])
331324
; CHECK-NEXT: [[TMP17:%.*]] = zext i32 [[TMP8]] to i64
332325
; CHECK-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP17]]
333326
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT2]]

0 commit comments

Comments
 (0)