Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit f097e13

Browse files
committed
[InstCombine] replace divide-by-constant checks with asserts; NFC
These folds already have tests for scalar and vector types, except for the vector div-by-0 case, so I'm adding tests for that. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280115 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bb0403b commit f097e13

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,15 +1971,6 @@ Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
19711971
Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp,
19721972
BinaryOperator *Div,
19731973
const APInt *C) {
1974-
// FIXME: These checks restrict all folds under here to scalar types.
1975-
ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
1976-
if (!RHS)
1977-
return nullptr;
1978-
1979-
ConstantInt *DivRHS = dyn_cast<ConstantInt>(Div->getOperand(1));
1980-
if (!DivRHS)
1981-
return nullptr;
1982-
19831974
// Fold: icmp pred ([us]div X, C2), C -> range test
19841975
// Fold this div into the comparison, producing a range check.
19851976
// Determine, based on the divide type, what the range is being
@@ -2002,16 +1993,22 @@ Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp,
20021993
if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
20031994
return nullptr;
20041995

2005-
// FIXME: These 3 checks can be asserts.
2006-
if (*C2 == 0)
2007-
return nullptr; // The ProdOV computation fails on divide by zero.
2008-
if (DivIsSigned && C2->isAllOnesValue())
2009-
return nullptr; // The overflow computation also screws up here
2010-
if (*C2 == 1) {
2011-
// This eliminates some funny cases with INT_MIN.
2012-
Cmp.setOperand(0, Div->getOperand(0)); // X/1 == X.
2013-
return &Cmp;
2014-
}
1996+
// These constant divides should already be folded in InstSimplify.
1997+
assert(*C2 != 0 && "The ProdOV computation fails on divide by zero.");
1998+
assert(*C2 != 1 && "Funny cases with INT_MIN will fail.");
1999+
2000+
// This constant divide should already be folded in InstCombine.
2001+
assert(!(DivIsSigned && C2->isAllOnesValue()) &&
2002+
"The overflow computation will fail.");
2003+
2004+
// FIXME: These checks restrict all folds under here to scalar types.
2005+
ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
2006+
if (!RHS)
2007+
return nullptr;
2008+
2009+
ConstantInt *DivRHS = dyn_cast<ConstantInt>(Div->getOperand(1));
2010+
if (!DivRHS)
2011+
return nullptr;
20152012

20162013
// Compute Prod = CI * DivRHS. We are essentially solving an equation
20172014
// of form X/C2=C. We solve for X by multiplying C2 (DivRHS) and

test/Transforms/InstSimplify/undef.ll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
; NOTE: Assertions have been autogenerated by update_test_checks.py
21
; RUN: opt -instsimplify -S < %s | FileCheck %s
32

43
define i64 @test0() {
@@ -194,6 +193,14 @@ define i32 @test20(i32 %a) {
194193
ret i32 %b
195194
}
196195

196+
define <2 x i32> @test20vec(<2 x i32> %a) {
197+
; CHECK-LABEL: @test20vec(
198+
; CHECK-NEXT: ret <2 x i32> undef
199+
;
200+
%b = udiv <2 x i32> %a, zeroinitializer
201+
ret <2 x i32> %b
202+
}
203+
197204
define i32 @test21(i32 %a) {
198205
; CHECK-LABEL: @test21(
199206
; CHECK: ret i32 undef
@@ -202,6 +209,14 @@ define i32 @test21(i32 %a) {
202209
ret i32 %b
203210
}
204211

212+
define <2 x i32> @test21vec(<2 x i32> %a) {
213+
; CHECK-LABEL: @test21vec(
214+
; CHECK-NEXT: ret <2 x i32> undef
215+
;
216+
%b = sdiv <2 x i32> %a, zeroinitializer
217+
ret <2 x i32> %b
218+
}
219+
205220
define i32 @test22(i32 %a) {
206221
; CHECK-LABEL: @test22(
207222
; CHECK: ret i32 undef

0 commit comments

Comments
 (0)