[llvm] ef8c34e - [InstSimplify] sle on i1 also encodes implication

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 15 08:27:48 PDT 2022


Author: Craig Topper
Date: 2022-08-15T08:27:23-07:00
New Revision: ef8c34e9543bcf33175f6e5007f4f1150f8bcb96

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

LOG: [InstSimplify] sle on i1 also encodes implication

We already support SGE, so the same logic should hold for SLE with
the LHS and RHS swapped.

I didn't see this in the wild. Just happened to walk past this code
and thought it was odd that it was asymmetric in what condition
codes it handled.

Reviewed By: spatel, reames

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 76ffacd967691..d5f90d6fdc680 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2868,6 +2868,11 @@ static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
     if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
       return getTrue(ITy);
     break;
+  case ICmpInst::ICMP_SLE:
+    /// SLE follows the same logic as SGE with the LHS and RHS swapped.
+    if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
+      return getTrue(ITy);
+    break;
   }
 
   return nullptr;

diff  --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index c9c08bf6d7123..f98ca3d6b1230 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -255,3 +255,15 @@ define i1 @test_sge(i32 %length.i, i32 %i) {
   %res = icmp sge i1 %var30, %var29
   ret i1 %res
 }
+
+; X <=(s) Y == Y ==> X (i1 1 becomes -1 for reasoning)
+define i1 @test_sle(i32 %length.i, i32 %i) {
+; CHECK-LABEL: @test_sle(
+; CHECK-NEXT:    ret i1 true
+;
+  %iplus1 = add nsw nuw i32 %i, 1
+  %var29 = icmp ult i32 %i, %length.i
+  %var30 = icmp ult i32 %iplus1, %length.i
+  %res = icmp sle i1 %var29, %var30
+  ret i1 %res
+}


        


More information about the llvm-commits mailing list