Skip to content

Commit a43fe4f

Browse files
committed
[LV] Fix tryToWiden logic for binop simplification
Fix the logic in the GetConstantViaSCEV routine in tryToWiden to match intent: we don't need to check if the VPValue has a defining recipe, and can simply check the underlying value. This allows us to better vectorize certain cases of binary ops.
1 parent 3b9f964 commit a43fe4f

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8621,17 +8621,14 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
86218621
// The legacy cost model uses SCEV to check if some of the operands are
86228622
// constants. To match the legacy cost model's behavior, use SCEV to try
86238623
// to replace operands with constants.
8624-
ScalarEvolution &SE = *PSE.getSE();
8625-
auto GetConstantViaSCEV = [this, &SE](VPValue *Op) {
8626-
if (!Op->isLiveIn())
8624+
auto GetConstantViaSCEV = [this](VPValue *Op) {
8625+
ScalarEvolution &SE = *PSE.getSE();
8626+
Value *V = dyn_cast_if_present<Value>(Op->getUnderlyingValue());
8627+
if (!V || !SE.isSCEVable(V->getType()))
86278628
return Op;
8628-
Value *V = Op->getUnderlyingValue();
8629-
if (isa<Constant>(V) || !SE.isSCEVable(V->getType()))
8630-
return Op;
8631-
auto *C = dyn_cast<SCEVConstant>(SE.getSCEV(V));
8632-
if (!C)
8633-
return Op;
8634-
return Plan.getOrAddLiveIn(C->getValue());
8629+
if (auto *C = dyn_cast<SCEVConstant>(SE.getSCEV(V)))
8630+
return Plan.getOrAddLiveIn(C->getValue());
8631+
return Op;
86358632
};
86368633
// For Mul, the legacy cost model checks both operands.
86378634
if (I->getOpcode() == Instruction::Mul)

llvm/test/Transforms/LoopVectorize/AArch64/mul-simplification.ll

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,34 @@ target triple = "arm64-apple-macosx"
77
define i64 @mul_select_operand_known_1_via_scev() {
88
; CHECK-LABEL: define i64 @mul_select_operand_known_1_via_scev() {
99
; CHECK-NEXT: [[ENTRY:.*]]:
10+
; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
11+
; CHECK: [[VECTOR_PH]]:
12+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
13+
; CHECK: [[VECTOR_BODY]]:
14+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
15+
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i64> [ <i64 12, i64 1>, %[[VECTOR_PH]] ], [ [[VEC_PHI]], %[[VECTOR_BODY]] ]
16+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 2
17+
; CHECK-NEXT: br i1 true, label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
18+
; CHECK: [[MIDDLE_BLOCK]]:
19+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vector.reduce.mul.v2i64(<2 x i64> [[VEC_PHI]])
20+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
21+
; CHECK: [[SCALAR_PH]]:
22+
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i64 [ [[TMP0]], %[[MIDDLE_BLOCK]] ], [ 12, %[[ENTRY]] ]
23+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 2, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
1024
; CHECK-NEXT: br label %[[LOOP:.*]]
1125
; CHECK: [[LOOP]]:
12-
; CHECK-NEXT: [[RED:%.*]] = phi i64 [ 12, %[[ENTRY]] ], [ [[RED_NEXT:%.*]], %[[LOOP]] ]
13-
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
26+
; CHECK-NEXT: [[RED:%.*]] = phi i64 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_NEXT:%.*]], %[[LOOP]] ]
27+
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
1428
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IV]], 1
1529
; CHECK-NEXT: [[CMP1_I:%.*]] = icmp eq i32 [[TMP1]], 0
1630
; CHECK-NEXT: [[NARROW_I:%.*]] = select i1 [[CMP1_I]], i32 1, i32 [[IV]]
1731
; CHECK-NEXT: [[MUL:%.*]] = zext nneg i32 [[NARROW_I]] to i64
1832
; CHECK-NEXT: [[RED_NEXT]] = mul nsw i64 [[RED]], [[MUL]]
1933
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i32 [[IV]], 1
2034
; CHECK-NEXT: [[EC:%.*]] = icmp eq i32 [[IV]], 1
21-
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
35+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP3:![0-9]+]]
2236
; CHECK: [[EXIT]]:
23-
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[RED_NEXT]], %[[LOOP]] ]
37+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[RED_NEXT]], %[[LOOP]] ], [ [[TMP0]], %[[MIDDLE_BLOCK]] ]
2438
; CHECK-NEXT: ret i64 [[RES]]
2539
;
2640
entry:
@@ -51,30 +65,27 @@ define i32 @add_reduction_select_operand_constant_but_non_uniform() {
5165
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
5266
; CHECK: [[VECTOR_BODY]]:
5367
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
54-
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i32> [ <i32 42, i32 0, i32 0, i32 0>, %[[VECTOR_PH]] ], [ [[TMP2:%.*]], %[[VECTOR_BODY]] ]
55-
; CHECK-NEXT: [[VEC_PHI1:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP1:%.*]], %[[VECTOR_BODY]] ]
56-
; CHECK-NEXT: [[TMP2]] = add <4 x i32> zeroinitializer, [[VEC_PHI]]
57-
; CHECK-NEXT: [[TMP1]] = add <4 x i32> zeroinitializer, [[VEC_PHI1]]
68+
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i32> [ <i32 42, i32 0, i32 0, i32 0>, %[[VECTOR_PH]] ], [ splat (i32 42), %[[VECTOR_BODY]] ]
69+
; CHECK-NEXT: [[VEC_PHI1:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_PH]] ], [ splat (i32 42), %[[VECTOR_BODY]] ]
5870
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 8
5971
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[INDEX_NEXT]], 64
60-
; CHECK-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
72+
; CHECK-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
6173
; CHECK: [[MIDDLE_BLOCK]]:
62-
; CHECK-NEXT: [[BIN_RDX:%.*]] = add <4 x i32> [[TMP1]], [[TMP2]]
63-
; CHECK-NEXT: [[TMP3:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[BIN_RDX]])
74+
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> splat (i32 84))
6475
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
6576
; CHECK: [[SCALAR_PH]]:
6677
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 64, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
67-
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 42, %[[ENTRY]] ]
78+
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i32 [ [[TMP1]], %[[MIDDLE_BLOCK]] ], [ 42, %[[ENTRY]] ]
6879
; CHECK-NEXT: br label %[[LOOP:.*]]
6980
; CHECK: [[LOOP]]:
7081
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[ADD2_REASS:%.*]], %[[LOOP]] ]
7182
; CHECK-NEXT: [[RDX:%.*]] = phi i32 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RDX_NEXT:%.*]], %[[LOOP]] ]
7283
; CHECK-NEXT: [[ADD2_REASS]] = add i32 [[IV]], 1
7384
; CHECK-NEXT: [[RDX_NEXT]] = add i32 0, [[RDX]]
7485
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD2_REASS]], 64
75-
; CHECK-NEXT: br i1 [[CMP]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
86+
; CHECK-NEXT: br i1 [[CMP]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP5:![0-9]+]]
7687
; CHECK: [[EXIT]]:
77-
; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[RDX_NEXT]], %[[LOOP]] ], [ [[TMP3]], %[[MIDDLE_BLOCK]] ]
88+
; CHECK-NEXT: [[ADD_LCSSA:%.*]] = phi i32 [ [[RDX_NEXT]], %[[LOOP]] ], [ [[TMP1]], %[[MIDDLE_BLOCK]] ]
7889
; CHECK-NEXT: ret i32 [[ADD_LCSSA]]
7990
;
8091
entry:
@@ -98,4 +109,6 @@ exit:
98109
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
99110
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
100111
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
112+
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
113+
; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
101114
;.

0 commit comments

Comments
 (0)