[InstCombine] add folds for icmp + sub (PR36969)

(A - B) >u A --> A <u B
C <u (C - D) --> C <u D

https://rise4fun.com/Alive/e7j

Name: ugt
  %sub = sub i8 %x, %y
  %cmp = icmp ugt i8 %sub, %x
=>
  %cmp = icmp ult i8 %x, %y
  
Name: ult
  %sub = sub i8 %x, %y
  %cmp = icmp ult i8 %x, %sub
=>
  %cmp = icmp ult i8 %x, %y

This should fix:
https://bugs.llvm.org/show_bug.cgi?id=36969

llvm-svn: 329011
This commit is contained in:
Sanjay Patel 2018-04-02 20:37:40 +00:00
parent be0442eeaa
commit cbb0450540
2 changed files with 9 additions and 6 deletions

View File

@ -3005,17 +3005,22 @@ Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) {
// icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow. // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
if (A == Op1 && NoOp0WrapProblem) if (A == Op1 && NoOp0WrapProblem)
return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
// icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
if (C == Op0 && NoOp1WrapProblem) if (C == Op0 && NoOp1WrapProblem)
return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
// (A - B) >u A --> A <u B
if (A == Op1 && Pred == ICmpInst::ICMP_UGT)
return new ICmpInst(ICmpInst::ICMP_ULT, A, B);
// C <u (C - D) --> C <u D
if (C == Op0 && Pred == ICmpInst::ICMP_ULT)
return new ICmpInst(ICmpInst::ICMP_ULT, C, D);
// icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow. // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
// Try not to increase register pressure. // Try not to increase register pressure.
BO0->hasOneUse() && BO1->hasOneUse()) BO0->hasOneUse() && BO1->hasOneUse())
return new ICmpInst(Pred, A, C); return new ICmpInst(Pred, A, C);
// icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
// Try not to increase register pressure. // Try not to increase register pressure.

View File

@ -550,8 +550,7 @@ define i1 @test36(i32 %x, i32 %y) {
define i1 @ugt_sub(i32 %xsrc, i32 %y) { define i1 @ugt_sub(i32 %xsrc, i32 %y) {
; CHECK-LABEL: @ugt_sub( ; CHECK-LABEL: @ugt_sub(
; CHECK-NEXT: [[X:%.*]] = udiv i32 [[XSRC:%.*]], 42 ; CHECK-NEXT: [[X:%.*]] = udiv i32 [[XSRC:%.*]], 42
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[X]], [[Y:%.*]] ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[SUB]], [[X]]
; CHECK-NEXT: ret i1 [[CMP]] ; CHECK-NEXT: ret i1 [[CMP]]
; ;
%x = udiv i32 %xsrc, 42 ; thwart complexity-based canonicalization %x = udiv i32 %xsrc, 42 ; thwart complexity-based canonicalization
@ -565,8 +564,7 @@ define i1 @ugt_sub(i32 %xsrc, i32 %y) {
define <2 x i1> @ult_sub(<2 x i8> %xsrc, <2 x i8> %y) { define <2 x i1> @ult_sub(<2 x i8> %xsrc, <2 x i8> %y) {
; CHECK-LABEL: @ult_sub( ; CHECK-LABEL: @ult_sub(
; CHECK-NEXT: [[X:%.*]] = udiv <2 x i8> [[XSRC:%.*]], <i8 42, i8 -42> ; CHECK-NEXT: [[X:%.*]] = udiv <2 x i8> [[XSRC:%.*]], <i8 42, i8 -42>
; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i8> [[X]], [[Y:%.*]] ; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> [[X]], [[Y:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> [[X]], [[SUB]]
; CHECK-NEXT: ret <2 x i1> [[CMP]] ; CHECK-NEXT: ret <2 x i1> [[CMP]]
; ;
%x = udiv <2 x i8> %xsrc, <i8 42, i8 -42> ; thwart complexity-based canonicalization %x = udiv <2 x i8> %xsrc, <i8 42, i8 -42> ; thwart complexity-based canonicalization