Skip to content

Commit e43b3b9

Browse files
committed
[InstSimplify][InstCombine][ConstantFold] Move vector div/rem by zero fold to InstCombine
1 parent ed7dfec commit e43b3b9

File tree

8 files changed

+42
-34
lines changed

8 files changed

+42
-34
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,19 +1095,6 @@ static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0,
10951095
if (match(Op1, m_Zero()))
10961096
return PoisonValue::get(Ty);
10971097

1098-
// If any element of a constant divisor fixed width vector is zero or undef
1099-
// the behavior is undefined and we can fold the whole op to poison.
1100-
auto *Op1C = dyn_cast<Constant>(Op1);
1101-
auto *VTy = dyn_cast<FixedVectorType>(Ty);
1102-
if (Op1C && VTy) {
1103-
unsigned NumElts = VTy->getNumElements();
1104-
for (unsigned i = 0; i != NumElts; ++i) {
1105-
Constant *Elt = Op1C->getAggregateElement(i);
1106-
if (Elt && (Elt->isNullValue() || Q.isUndefValue(Elt)))
1107-
return PoisonValue::get(Ty);
1108-
}
1109-
}
1110-
11111098
// poison / X -> poison
11121099
// poison % X -> poison
11131100
if (isa<PoisonValue>(Op0))

llvm/lib/IR/ConstantFold.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
902902
Constant *ExtractIdx = ConstantInt::get(Ty, i);
903903
Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
904904
Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
905-
906-
// If any element of a divisor vector is zero, the whole op is poison.
907-
if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue())
908-
return PoisonValue::get(VTy);
909-
910905
Constant *Res = ConstantExpr::isDesirableBinOp(Opcode)
911906
? ConstantExpr::get(Opcode, LHS, RHS)
912907
: ConstantFoldBinaryInstruction(Opcode, LHS, RHS);

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,20 @@ Instruction *InstCombinerImpl::commonIDivRemTransforms(BinaryOperator &I) {
11631163
assert(I.isIntDivRem() && "Unexpected instruction");
11641164
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
11651165

1166+
// If any element of a constant divisor fixed width vector is zero or undef
1167+
// the behavior is undefined and we can fold the whole op to poison.
1168+
auto *Op1C = dyn_cast<Constant>(Op1);
1169+
Type *Ty = I.getType();
1170+
auto *VTy = dyn_cast<FixedVectorType>(Ty);
1171+
if (Op1C && VTy) {
1172+
unsigned NumElts = VTy->getNumElements();
1173+
for (unsigned i = 0; i != NumElts; ++i) {
1174+
Constant *Elt = Op1C->getAggregateElement(i);
1175+
if (Elt && (Elt->isNullValue() || isa<UndefValue>(Elt)))
1176+
return replaceInstUsesWith(I, PoisonValue::get(Ty));
1177+
}
1178+
}
1179+
11661180
if (Instruction *Phi = foldBinopWithPhiOperands(I))
11671181
return Phi;
11681182

llvm/test/Transforms/InstCombine/div.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,8 @@ define <2 x i8> @sdiv_constant_dividend_select_of_constants_divisor_vec(i1 %b) {
11631163

11641164
define <2 x i8> @sdiv_constant_dividend_select_of_constants_divisor_vec_ub1(i1 %b) {
11651165
; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_vec_ub1(
1166-
; CHECK-NEXT: ret <2 x i8> <i8 -10, i8 -10>
1166+
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 poison, i8 8>, <2 x i8> <i8 -10, i8 -10>
1167+
; CHECK-NEXT: ret <2 x i8> [[R]]
11671168
;
11681169
%s = select i1 %b, <2 x i8> <i8 0, i8 -5>, <2 x i8> <i8 -4, i8 4>
11691170
%r = sdiv <2 x i8> <i8 42, i8 -42>, %s
@@ -1269,7 +1270,8 @@ define <2 x i8> @udiv_constant_dividend_select_of_constants_divisor_vec(i1 %b) {
12691270

12701271
define <2 x i8> @udiv_constant_dividend_select_of_constants_divisor_vec_ub1(i1 %b) {
12711272
; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_vec_ub1(
1272-
; CHECK-NEXT: ret <2 x i8> <i8 0, i8 53>
1273+
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 poison, i8 0>, <2 x i8> <i8 0, i8 53>
1274+
; CHECK-NEXT: ret <2 x i8> [[R]]
12731275
;
12741276
%s = select i1 %b, <2 x i8> <i8 0, i8 -5>, <2 x i8> <i8 -4, i8 4>
12751277
%r = udiv <2 x i8> <i8 42, i8 -42>, %s

llvm/test/Transforms/InstCombine/rem.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,8 @@ define <2 x i8> @urem_constant_dividend_select_of_constants_divisor_vec(i1 %b) {
997997

998998
define <2 x i8> @urem_constant_dividend_select_of_constants_divisor_vec_ub1(i1 %b) {
999999
; CHECK-LABEL: @urem_constant_dividend_select_of_constants_divisor_vec_ub1(
1000-
; CHECK-NEXT: ret <2 x i8> <i8 42, i8 2>
1000+
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 poison, i8 -42>, <2 x i8> <i8 42, i8 2>
1001+
; CHECK-NEXT: ret <2 x i8> [[R]]
10011002
;
10021003
%s = select i1 %b, <2 x i8> <i8 0, i8 -5>, <2 x i8> <i8 -4, i8 4>
10031004
%r = urem <2 x i8> <i8 42, i8 -42>, %s

llvm/test/Transforms/InstCombine/vector-udiv.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ define <4 x i32> @test_v4i32_zext_shl_const_pow2(<4 x i32> %a0, <4 x i16> %a1) {
103103

104104
define <2 x i32> @vec_select_udiv_poison(<2 x i1> %x) {
105105
; CHECK-LABEL: @vec_select_udiv_poison(
106-
; CHECK-NEXT: ret <2 x i32> zeroinitializer
106+
; CHECK-NEXT: [[DIV:%.*]] = select <2 x i1> [[X:%.*]], <2 x i32> zeroinitializer, <2 x i32> <i32 poison, i32 -7>
107+
; CHECK-NEXT: ret <2 x i32> [[DIV]]
107108
;
108109
%sel = select <2 x i1> %x, <2 x i32> <i32 -1, i32 -1>, <2 x i32> <i32 0, i32 1>
109110
%div = udiv <2 x i32> <i32 42, i32 -7>, %sel

llvm/test/Transforms/InstSimplify/div.ll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,51 @@ define <2 x i32> @zero_dividend_vector_poison_elt(<2 x i32> %A) {
2929

3030
define <2 x i8> @sdiv_zero_elt_vec_constfold(<2 x i8> %x) {
3131
; CHECK-LABEL: @sdiv_zero_elt_vec_constfold(
32-
; CHECK-NEXT: ret <2 x i8> poison
32+
; CHECK-NEXT: ret <2 x i8> <i8 poison, i8 0>
3333
;
3434
%div = sdiv <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
3535
ret <2 x i8> %div
3636
}
3737

3838
define <2 x i8> @udiv_zero_elt_vec_constfold(<2 x i8> %x) {
3939
; CHECK-LABEL: @udiv_zero_elt_vec_constfold(
40-
; CHECK-NEXT: ret <2 x i8> poison
40+
; CHECK-NEXT: ret <2 x i8> <i8 0, i8 poison>
4141
;
4242
%div = udiv <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
4343
ret <2 x i8> %div
4444
}
4545

4646
define <2 x i8> @sdiv_zero_elt_vec(<2 x i8> %x) {
4747
; CHECK-LABEL: @sdiv_zero_elt_vec(
48-
; CHECK-NEXT: ret <2 x i8> poison
48+
; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -42, i8 0>
49+
; CHECK-NEXT: ret <2 x i8> [[DIV]]
4950
;
5051
%div = sdiv <2 x i8> %x, <i8 -42, i8 0>
5152
ret <2 x i8> %div
5253
}
5354

5455
define <2 x i8> @udiv_zero_elt_vec(<2 x i8> %x) {
5556
; CHECK-LABEL: @udiv_zero_elt_vec(
56-
; CHECK-NEXT: ret <2 x i8> poison
57+
; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i8> [[X:%.*]], <i8 0, i8 42>
58+
; CHECK-NEXT: ret <2 x i8> [[DIV]]
5759
;
5860
%div = udiv <2 x i8> %x, <i8 0, i8 42>
5961
ret <2 x i8> %div
6062
}
6163

6264
define <2 x i8> @sdiv_poison_elt_vec(<2 x i8> %x) {
6365
; CHECK-LABEL: @sdiv_poison_elt_vec(
64-
; CHECK-NEXT: ret <2 x i8> poison
66+
; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -42, i8 poison>
67+
; CHECK-NEXT: ret <2 x i8> [[DIV]]
6568
;
6669
%div = sdiv <2 x i8> %x, <i8 -42, i8 poison>
6770
ret <2 x i8> %div
6871
}
6972

7073
define <2 x i8> @udiv_poison_elt_vec(<2 x i8> %x) {
7174
; CHECK-LABEL: @udiv_poison_elt_vec(
72-
; CHECK-NEXT: ret <2 x i8> poison
75+
; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i8> [[X:%.*]], <i8 poison, i8 42>
76+
; CHECK-NEXT: ret <2 x i8> [[DIV]]
7377
;
7478
%div = udiv <2 x i8> %x, <i8 poison, i8 42>
7579
ret <2 x i8> %div

llvm/test/Transforms/InstSimplify/rem.ll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,51 @@ define <2 x i32> @zero_dividend_vector_poison_elt(<2 x i32> %A) {
2929

3030
define <2 x i8> @srem_zero_elt_vec_constfold(<2 x i8> %x) {
3131
; CHECK-LABEL: @srem_zero_elt_vec_constfold(
32-
; CHECK-NEXT: ret <2 x i8> poison
32+
; CHECK-NEXT: ret <2 x i8> <i8 poison, i8 2>
3333
;
3434
%rem = srem <2 x i8> <i8 1, i8 2>, <i8 0, i8 -42>
3535
ret <2 x i8> %rem
3636
}
3737

3838
define <2 x i8> @urem_zero_elt_vec_constfold(<2 x i8> %x) {
3939
; CHECK-LABEL: @urem_zero_elt_vec_constfold(
40-
; CHECK-NEXT: ret <2 x i8> poison
40+
; CHECK-NEXT: ret <2 x i8> <i8 1, i8 poison>
4141
;
4242
%rem = urem <2 x i8> <i8 1, i8 2>, <i8 42, i8 0>
4343
ret <2 x i8> %rem
4444
}
4545

4646
define <2 x i8> @srem_zero_elt_vec(<2 x i8> %x) {
4747
; CHECK-LABEL: @srem_zero_elt_vec(
48-
; CHECK-NEXT: ret <2 x i8> poison
48+
; CHECK-NEXT: [[REM:%.*]] = srem <2 x i8> [[X:%.*]], <i8 -42, i8 0>
49+
; CHECK-NEXT: ret <2 x i8> [[REM]]
4950
;
5051
%rem = srem <2 x i8> %x, <i8 -42, i8 0>
5152
ret <2 x i8> %rem
5253
}
5354

5455
define <2 x i8> @urem_zero_elt_vec(<2 x i8> %x) {
5556
; CHECK-LABEL: @urem_zero_elt_vec(
56-
; CHECK-NEXT: ret <2 x i8> poison
57+
; CHECK-NEXT: [[REM:%.*]] = urem <2 x i8> [[X:%.*]], <i8 0, i8 42>
58+
; CHECK-NEXT: ret <2 x i8> [[REM]]
5759
;
5860
%rem = urem <2 x i8> %x, <i8 0, i8 42>
5961
ret <2 x i8> %rem
6062
}
6163

6264
define <2 x i8> @srem_undef_elt_vec(<2 x i8> %x) {
6365
; CHECK-LABEL: @srem_undef_elt_vec(
64-
; CHECK-NEXT: ret <2 x i8> poison
66+
; CHECK-NEXT: [[REM:%.*]] = srem <2 x i8> [[X:%.*]], <i8 -42, i8 undef>
67+
; CHECK-NEXT: ret <2 x i8> [[REM]]
6568
;
6669
%rem = srem <2 x i8> %x, <i8 -42, i8 undef>
6770
ret <2 x i8> %rem
6871
}
6972

7073
define <2 x i8> @urem_undef_elt_vec(<2 x i8> %x) {
7174
; CHECK-LABEL: @urem_undef_elt_vec(
72-
; CHECK-NEXT: ret <2 x i8> poison
75+
; CHECK-NEXT: [[REM:%.*]] = urem <2 x i8> [[X:%.*]], <i8 undef, i8 42>
76+
; CHECK-NEXT: ret <2 x i8> [[REM]]
7377
;
7478
%rem = urem <2 x i8> %x, <i8 undef, i8 42>
7579
ret <2 x i8> %rem

0 commit comments

Comments
 (0)