[llvm] 3b67102 - [InstSimplify] Simplify comparison between zext(x) and sext(x)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 4 02:03:14 PDT 2020


Author: Nikita Popov
Date: 2020-07-04T11:03:00+02:00
New Revision: 3b671022e4518166fc5a72e88786db037fb7708c

URL: https://github.com/llvm/llvm-project/commit/3b671022e4518166fc5a72e88786db037fb7708c
DIFF: https://github.com/llvm/llvm-project/commit/3b671022e4518166fc5a72e88786db037fb7708c.diff

LOG: [InstSimplify] Simplify comparison between zext(x) and sext(x)

This is picking up a loose thread from D69006: We can simplify
(zext x) ule (sext x) and (zext x) sge (sext x) to true, with
various permutations. Oddly, SCEV knows about this identity,
but nothing on the IR level does.

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

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/cmp_ext.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 817d23ac7beb..df4abe09797c 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3404,6 +3404,15 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
                                           MaxRecurse-1))
             return V;
       }
+      // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
+      else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
+        if (SrcOp == RI->getOperand(0)) {
+          if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
+            return ConstantInt::getTrue(ITy);
+          if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
+            return ConstantInt::getFalse(ITy);
+        }
+      }
       // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
       // too.  If not, then try to deduce the result of the comparison.
       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
@@ -3463,6 +3472,15 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
                                           Q, MaxRecurse-1))
             return V;
       }
+      // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
+      else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
+        if (SrcOp == RI->getOperand(0)) {
+          if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
+            return ConstantInt::getTrue(ITy);
+          if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
+            return ConstantInt::getFalse(ITy);
+        }
+      }
       // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
       // too.  If not, then try to deduce the result of the comparison.
       else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {

diff  --git a/llvm/test/Transforms/InstSimplify/cmp_ext.ll b/llvm/test/Transforms/InstSimplify/cmp_ext.ll
index 4bbe23bb870a..63d3652fefe5 100644
--- a/llvm/test/Transforms/InstSimplify/cmp_ext.ll
+++ b/llvm/test/Transforms/InstSimplify/cmp_ext.ll
@@ -16,10 +16,7 @@ define i1 @zext_uge_sext(i32 %x) {
 
 define i1 @zext_ugt_sext(i32 %x) {
 ; CHECK-LABEL: @zext_ugt_sext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i64 [[ZEXT]], [[SEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -42,10 +39,7 @@ define i1 @zext_ult_sext(i32 %x) {
 
 define i1 @zext_ule_sext(i32 %x) {
 ; CHECK-LABEL: @zext_ule_sext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ule i64 [[ZEXT]], [[SEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -55,10 +49,7 @@ define i1 @zext_ule_sext(i32 %x) {
 
 define i1 @zext_sge_sext(i32 %x) {
 ; CHECK-LABEL: @zext_sge_sext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sge i64 [[ZEXT]], [[SEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -81,10 +72,7 @@ define i1 @zext_sgt_sext(i32 %x) {
 
 define i1 @zext_slt_sext(i32 %x) {
 ; CHECK-LABEL: @zext_slt_sext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i64 [[ZEXT]], [[SEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -107,10 +95,7 @@ define i1 @zext_sle_sext(i32 %x) {
 
 define i1 @sext_uge_zext(i32 %x) {
 ; CHECK-LABEL: @sext_uge_zext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp uge i64 [[SEXT]], [[ZEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -133,10 +118,7 @@ define i1 @sext_ugt_zext(i32 %x) {
 
 define i1 @sext_ult_zext(i32 %x) {
 ; CHECK-LABEL: @sext_ult_zext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i64 [[SEXT]], [[ZEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -172,10 +154,7 @@ define i1 @sext_sge_zext(i32 %x) {
 
 define i1 @sext_sgt_zext(i32 %x) {
 ; CHECK-LABEL: @sext_sgt_zext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEXT]], [[ZEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -198,10 +177,7 @@ define i1 @sext_slt_zext(i32 %x) {
 
 define i1 @sext_sle_zext(i32 %x) {
 ; CHECK-LABEL: @sext_sle_zext(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext i32 [[X:%.*]] to i64
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i32 [[X]] to i64
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i64 [[SEXT]], [[ZEXT]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %sext = sext i32 %x to i64
   %zext = zext i32 %x to i64
@@ -211,10 +187,7 @@ define i1 @sext_sle_zext(i32 %x) {
 
 define <4 x i1> @zext_ugt_sext_vec(<4 x i32> %x) {
 ; CHECK-LABEL: @zext_ugt_sext_vec(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext <4 x i32> [[X:%.*]] to <4 x i64>
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext <4 x i32> [[X]] to <4 x i64>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt <4 x i64> [[ZEXT]], [[SEXT]]
-; CHECK-NEXT:    ret <4 x i1> [[CMP]]
+; CHECK-NEXT:    ret <4 x i1> zeroinitializer
 ;
   %sext = sext <4 x i32> %x to <4 x i64>
   %zext = zext <4 x i32> %x to <4 x i64>
@@ -224,10 +197,7 @@ define <4 x i1> @zext_ugt_sext_vec(<4 x i32> %x) {
 
 define <4 x i1> @sext_ult_zext_vec(<4 x i32> %x) {
 ; CHECK-LABEL: @sext_ult_zext_vec(
-; CHECK-NEXT:    [[SEXT:%.*]] = sext <4 x i32> [[X:%.*]] to <4 x i64>
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext <4 x i32> [[X]] to <4 x i64>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult <4 x i64> [[SEXT]], [[ZEXT]]
-; CHECK-NEXT:    ret <4 x i1> [[CMP]]
+; CHECK-NEXT:    ret <4 x i1> zeroinitializer
 ;
   %sext = sext <4 x i32> %x to <4 x i64>
   %zext = zext <4 x i32> %x to <4 x i64>


        


More information about the llvm-commits mailing list