Skip to content

Commit 6d6eea9

Browse files
committed
[LV] Use SCEV to simplify wide binop operand to constant.
The legacy cost model uses SCEV to determine if the second operand of a binary op is a constant. Update the VPlan construction logic to mirror the current legacy behavior, to fix a difference in the cost models. Fixes llvm#109528. Fixes llvm#110440.
1 parent 5867362 commit 6d6eea9

File tree

2 files changed

+104
-13
lines changed

2 files changed

+104
-13
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8420,21 +8420,28 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
84208420
case Instruction::Sub:
84218421
case Instruction::Xor:
84228422
case Instruction::Freeze:
8423-
if (I->getOpcode() == Instruction::Mul) {
8424-
// Simplify operands of multiplications using SCEV. This is needed at the
8425-
// moment to match the behavior of the legacy cost-model.
8426-
// TODO: Generalize to any opcode and move to VPlan transformation.
8427-
SmallVector<VPValue *> NewOps(Operands);
8423+
SmallVector<VPValue *> NewOps(Operands);
8424+
if (Instruction::isBinaryOp(I->getOpcode())) {
8425+
// The legacy cost model uses SCEV to check if some of the operands are
8426+
// constants. To match the legacy cost model's behavior, use SCEV to try
8427+
// to replace operands with constants.
84288428
ScalarEvolution &SE = *PSE.getSE();
8429-
for (unsigned I = 0; I < Operands.size(); ++I) {
8430-
Value *V = NewOps[I]->getUnderlyingValue();
8431-
if (!isa<Constant>(V) && SE.isSCEVable(V->getType()))
8432-
if (auto *C = dyn_cast<SCEVConstant>(PSE.getSE()->getSCEV(V)))
8433-
NewOps[I] = Plan.getOrAddLiveIn(C->getValue());
8434-
}
8435-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8429+
auto GetConstantViaSCEV = [this, &SE](VPValue *Op) {
8430+
Value *V = Op->getUnderlyingValue();
8431+
if (isa<Constant>(V) || !SE.isSCEVable(V->getType()))
8432+
return Op;
8433+
auto *C = dyn_cast<SCEVConstant>(SE.getSCEV(V));
8434+
if (!C)
8435+
return Op;
8436+
return Plan.getOrAddLiveIn(C->getValue());
8437+
};
8438+
// For Mul, the legacy cost model checks both operands.
8439+
if (I->getOpcode() == Instruction::Mul)
8440+
NewOps[0] = GetConstantViaSCEV(NewOps[0]);
8441+
// For other binops, the legacy cost model only checks the second operand.
8442+
NewOps[1] = GetConstantViaSCEV(NewOps[1]);
84368443
}
8437-
return new VPWidenRecipe(*I, make_range(Operands.begin(), Operands.end()));
8444+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
84388445
};
84398446
}
84408447

llvm/test/Transforms/LoopVectorize/X86/cost-constant-known-via-scev.ll

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,93 @@ exit:
5858
%ret = phi i64 [ %mul, %loop ]
5959
ret i64 %ret
6060
}
61+
62+
; Test case for https://github.com/llvm/llvm-project/issues/109528.
63+
define i64 @second_lshr_operand_zero_via_scev() {
64+
; CHECK-LABEL: define i64 @second_lshr_operand_zero_via_scev() {
65+
; CHECK-NEXT: [[ENTRY:.*]]:
66+
; CHECK-NEXT: [[EXT_0:%.*]] = sext i8 0 to i32
67+
; CHECK-NEXT: br i1 true, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
68+
; CHECK: [[VECTOR_PH]]:
69+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
70+
; CHECK: [[VECTOR_BODY]]:
71+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
72+
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <2 x i64> [ <i64 0, i64 1>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
73+
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i64> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP10:%.*]], %[[VECTOR_BODY]] ]
74+
; CHECK-NEXT: [[VEC_PHI1:%.*]] = phi <2 x i64> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP11:%.*]], %[[VECTOR_BODY]] ]
75+
; CHECK-NEXT: [[VEC_IND2:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT3:%.*]], %[[VECTOR_BODY]] ]
76+
; CHECK-NEXT: [[STEP_ADD:%.*]] = add <2 x i64> [[VEC_IND]], <i64 2, i64 2>
77+
; CHECK-NEXT: [[STEP_ADD4:%.*]] = add <2 x i32> [[VEC_IND2]], <i32 2, i32 2>
78+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq <2 x i64> [[VEC_IND]], zeroinitializer
79+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i64> [[STEP_ADD]], zeroinitializer
80+
; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i64> [[VEC_IND]], zeroinitializer
81+
; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i64> [[STEP_ADD]], zeroinitializer
82+
; CHECK-NEXT: [[TMP4:%.*]] = lshr <2 x i32> [[VEC_IND2]], zeroinitializer
83+
; CHECK-NEXT: [[TMP5:%.*]] = lshr <2 x i32> [[STEP_ADD4]], zeroinitializer
84+
; CHECK-NEXT: [[TMP6:%.*]] = zext <2 x i32> [[TMP4]] to <2 x i64>
85+
; CHECK-NEXT: [[TMP7:%.*]] = zext <2 x i32> [[TMP5]] to <2 x i64>
86+
; CHECK-NEXT: [[TMP8:%.*]] = select <2 x i1> [[TMP0]], <2 x i64> [[TMP2]], <2 x i64> [[TMP6]]
87+
; CHECK-NEXT: [[TMP9:%.*]] = select <2 x i1> [[TMP1]], <2 x i64> [[TMP3]], <2 x i64> [[TMP7]]
88+
; CHECK-NEXT: [[TMP10]] = or <2 x i64> [[TMP8]], [[VEC_PHI]]
89+
; CHECK-NEXT: [[TMP11]] = or <2 x i64> [[TMP9]], [[VEC_PHI1]]
90+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
91+
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <2 x i64> [[STEP_ADD]], <i64 2, i64 2>
92+
; CHECK-NEXT: [[VEC_IND_NEXT3]] = add <2 x i32> [[STEP_ADD4]], <i32 2, i32 2>
93+
; CHECK-NEXT: [[TMP12:%.*]] = icmp eq i64 [[INDEX_NEXT]], 0
94+
; CHECK-NEXT: br i1 [[TMP12]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
95+
; CHECK: [[MIDDLE_BLOCK]]:
96+
; CHECK-NEXT: [[BIN_RDX:%.*]] = or <2 x i64> [[TMP11]], [[TMP10]]
97+
; CHECK-NEXT: [[TMP13:%.*]] = call i64 @llvm.vector.reduce.or.v2i64(<2 x i64> [[BIN_RDX]])
98+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
99+
; CHECK: [[SCALAR_PH]]:
100+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ 0, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
101+
; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i64 [ [[TMP13]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
102+
; CHECK-NEXT: br label %[[LOOPS:.*]]
103+
; CHECK: [[LOOPS]]:
104+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOPS]] ]
105+
; CHECK-NEXT: [[RED:%.*]] = phi i64 [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ], [ [[RED_NEXT:%.*]], %[[LOOPS]] ]
106+
; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[IV]], 0
107+
; CHECK-NEXT: [[AND:%.*]] = and i64 [[IV]], 0
108+
; CHECK-NEXT: [[TMP14:%.*]] = trunc i64 [[IV]] to i32
109+
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[TMP14]], [[EXT_0]]
110+
; CHECK-NEXT: [[CONV_1:%.*]] = zext i32 [[SHR]] to i64
111+
; CHECK-NEXT: [[RED_NEXT_V:%.*]] = select i1 [[C]], i64 [[AND]], i64 [[CONV_1]]
112+
; CHECK-NEXT: [[RED_NEXT]] = or i64 [[RED_NEXT_V]], [[RED]]
113+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
114+
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[IV_NEXT]], 0
115+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOPS]], !llvm.loop [[LOOP5:![0-9]+]]
116+
; CHECK: [[EXIT]]:
117+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[RED_NEXT]], %[[LOOPS]] ], [ [[TMP13]], %[[MIDDLE_BLOCK]] ]
118+
; CHECK-NEXT: ret i64 [[RES]]
119+
;
120+
entry:
121+
%ext.0 = sext i8 0 to i32
122+
br label %loops
123+
124+
loops:
125+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loops ]
126+
%red = phi i64 [ 0, %entry ], [ %red.next, %loops ]
127+
%c = icmp eq i64 %iv, 0
128+
%and = and i64 %iv, 0
129+
%0 = trunc i64 %iv to i32
130+
%shr = lshr i32 %0, %ext.0
131+
%conv.1 = zext i32 %shr to i64
132+
%red.next.v = select i1 %c, i64 %and, i64 %conv.1
133+
%red.next = or i64 %red.next.v, %red
134+
%iv.next = add i64 %iv, 1
135+
%ec = icmp eq i64 %iv.next, 0
136+
br i1 %ec, label %exit, label %loops
137+
138+
exit:
139+
%res = phi i64 [ %red.next, %loops ]
140+
ret i64 %res
141+
}
142+
61143
;.
62144
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
63145
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
64146
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
65147
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
148+
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
149+
; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
66150
;.

0 commit comments

Comments
 (0)