[PATCH] D131805: [InstSimplify] sle on i1 also encodes implication

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 12 13:35:12 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: spatel, reames.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131805

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


Index: llvm/test/Transforms/InstSimplify/implies.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/implies.ll
+++ llvm/test/Transforms/InstSimplify/implies.ll
@@ -255,3 +255,15 @@
   %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
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2868,6 +2868,11 @@
     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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131805.452293.patch
Type: text/x-patch
Size: 1192 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220812/a71e3b03/attachment-0001.bin>


More information about the llvm-commits mailing list