[llvm] 8878b79 - [InstCombine] (-NSW x) ==/!= x --> x ==/!= 0 (PR39480)
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 01:51:43 PDT 2020
Author: Roman Lebedev
Date: 2020-08-06T11:50:34+03:00
New Revision: 8878b79cfe99e1ef36a265539329cd323e05a40d
URL: https://github.com/llvm/llvm-project/commit/8878b79cfe99e1ef36a265539329cd323e05a40d
DIFF: https://github.com/llvm/llvm-project/commit/8878b79cfe99e1ef36a265539329cd323e05a40d.diff
LOG: [InstCombine] (-NSW x) ==/!= x --> x ==/!= 0 (PR39480)
Name: (-x) == x --> x == 0
%neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN
%r = icmp eq i8 %neg_x, %x
=>
%r = icmp eq i8 %x, 0
Name: (-x) != x --> x != 0
%neg_x = sub nsw i8 0, %x ; %x must not be INT_MIN
%r = icmp ne i8 %neg_x, %x
=>
%r = icmp ne i8 %x, 0
https://rise4fun.com/Alive/4slH
https://bugs.llvm.org/show_bug.cgi?id=39480
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0032b65c3d2a..ce75a360de9d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3733,6 +3733,12 @@ Instruction *foldICmpXNegX(ICmpInst &I) {
NewRHS = Constant::getNullValue(X->getType());
break;
+ case ICmpInst::ICMP_EQ:
+ case ICmpInst::ICMP_NE:
+ NewPred = Pred;
+ NewRHS = Constant::getNullValue(X->getType());
+ break;
+
default:
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll b/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
index 485e32913601..a2e39f1637d8 100644
--- a/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
+++ b/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
@@ -118,8 +118,7 @@ define i1 @t7(i8 %x) {
define i1 @t8(i8 %x) {
; CHECK-LABEL: @t8(
-; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[NEG_X]], [[X]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%neg_x = sub nsw i8 0, %x
@@ -129,8 +128,7 @@ define i1 @t8(i8 %x) {
define i1 @t9(i8 %x) {
; CHECK-LABEL: @t9(
-; CHECK-NEXT: [[NEG_X:%.*]] = sub nsw i8 0, [[X:%.*]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[NEG_X]], [[X]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%neg_x = sub nsw i8 0, %x
More information about the llvm-commits
mailing list