diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2018-04-02 20:37:40 +0000 |
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2018-04-02 20:37:40 +0000 |
| commit | cbb0450540ed03d3c630996cbb8ef719a8f6b6d5 (patch) | |
| tree | 6e2396373f5e266b24d2bd3816f12237ef4d0597 | |
| parent | be0442eeaaefbce1e3ed1f0f1af5b7044544b99f (diff) | |
| download | bcm5719-llvm-cbb0450540ed03d3c630996cbb8ef719a8f6b6d5.tar.gz bcm5719-llvm-cbb0450540ed03d3c630996cbb8ef719a8f6b6d5.zip | |
[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
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 9 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/icmp.ll | 6 |
2 files changed, 9 insertions, 6 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index c0876a27594..cea3a1d705b 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3005,17 +3005,22 @@ Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) { // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow. if (A == Op1 && NoOp0WrapProblem) return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); - // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. if (C == Op0 && NoOp1WrapProblem) 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. if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && // Try not to increase register pressure. BO0->hasOneUse() && BO1->hasOneUse()) return new ICmpInst(Pred, A, C); - // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && // Try not to increase register pressure. diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index 6e6a9135350..34fdf32adf1 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -550,8 +550,7 @@ define i1 @test36(i32 %x, i32 %y) { define i1 @ugt_sub(i32 %xsrc, i32 %y) { ; CHECK-LABEL: @ugt_sub( ; CHECK-NEXT: [[X:%.*]] = udiv i32 [[XSRC:%.*]], 42 -; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[X]], [[Y:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[SUB]], [[X]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], [[Y:%.*]] ; CHECK-NEXT: ret i1 [[CMP]] ; %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) { ; CHECK-LABEL: @ult_sub( ; 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]], [[SUB]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> [[X]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %x = udiv <2 x i8> %xsrc, <i8 42, i8 -42> ; thwart complexity-based canonicalization |

