OSDN Git Service

[InstSimplify] Fix missed optimization in simplifyUnsignedRangeCheck()
authorSanjay Patel <spatel@rotateright.com>
Wed, 20 Jun 2018 14:22:49 +0000 (14:22 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 20 Jun 2018 14:22:49 +0000 (14:22 +0000)
For both operands are unsigned, the following optimizations are valid, and missing:

   1. X > Y && X != 0 --> X > Y
   2. X > Y || X != 0 --> X != 0
   3. X <= Y || X != 0 --> true
   4. X <= Y || X == 0 --> X <= Y
   5. X > Y && X == 0 --> false

unsigned foo(unsigned x, unsigned y) { return x > y && x != 0; }
should fold to x > y, but I found we haven't done it right now.
besides, unsigned foo(unsigned x, unsigned y) { return x < y && y != 0; }
Has been folded to x < y, so there may be a bug.

Patch by: Li Jia He!

Differential Revision: https://reviews.llvm.org/D47922

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@335129 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/AndOrXor.ll

index ffcffc7..f1bfa33 100644 (file)
@@ -1313,7 +1313,7 @@ static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
       ICmpInst::isUnsigned(UnsignedPred))
     ;
   else if (match(UnsignedICmp,
-                 m_ICmp(UnsignedPred, m_Value(Y), m_Specific(X))) &&
+                 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
            ICmpInst::isUnsigned(UnsignedPred))
     UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
   else
index 9447f06..09181bf 100644 (file)
@@ -375,9 +375,7 @@ define i1 @and_icmp1(i32 %x, i32 %y) {
 define i1 @and_icmp2(i32 %x, i32 %y) {
 ; CHECK-LABEL: @and_icmp2(
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-NEXT:    ret i1 [[TMP1]]
 ;
   %1 = icmp ugt i32 %x, %y
   %2 = icmp ne i32 %x, 0
@@ -397,10 +395,7 @@ define i1 @and_icmp3(i32 %x, i32 %y) {
 
 define i1 @and_icmp4(i32 %x, i32 %y) {
 ; CHECK-LABEL: @and_icmp4(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[X]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-NEXT:    ret i1 false
 ;
   %1 = icmp ugt i32 %x, %y
   %2 = icmp eq i32 %x, 0
@@ -421,10 +416,8 @@ define i1 @or_icmp1(i32 %x, i32 %y) {
 
 define i1 @or_icmp2(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_icmp2(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[TMP1]]
 ;
   %1 = icmp ugt i32 %x, %y
   %2 = icmp ne i32 %x, 0
@@ -444,10 +437,7 @@ define i1 @or_icmp3(i32 %x, i32 %y) {
 
 define i1 @or_icmp4(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_icmp4(
-; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-NEXT:    ret i1 true
 ;
   %1 = icmp ule i32 %x, %y
   %2 = icmp ne i32 %x, 0
@@ -469,9 +459,7 @@ define i1 @or_icmp5(i32 %x, i32 %y) {
 define i1 @or_icmp6(i32 %x, i32 %y) {
 ; CHECK-LABEL: @or_icmp6(
 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[X]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
-; CHECK-NEXT:    ret i1 [[TMP3]]
+; CHECK-NEXT:    ret i1 [[TMP1]]
 ;
   %1 = icmp ule i32 %x, %y
   %2 = icmp eq i32 %x, 0