[PATCH] D132989: [InstCombine] fold 1 - X == X to false

Liao Chunyu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 20:33:51 PDT 2022


liaolucy updated this revision to Diff 457491.
liaolucy added a comment.

iupdate to InstSimplify, transform: 
(sub nsw|nuw C, X) == X, C is odd  --> false 
https://alive2.llvm.org/ce/z/dyn2Z2

(sub nsw|nuw C, X) != X, C is odd  --> true
https://alive2.llvm.org/ce/z/5aSkqc

(sub 1, X) == X --> false
(sub 1, X) != X --> true
https://alive2.llvm.org/ce/z/DJqxNZ


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132989/new/

https://reviews.llvm.org/D132989

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstCombine/icmp-sub.ll


Index: llvm/test/Transforms/InstCombine/icmp-sub.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-sub.ll
+++ llvm/test/Transforms/InstCombine/icmp-sub.ll
@@ -561,3 +561,36 @@
 bb_exit:
   ret void
 }
+
+define i1 @sub_false(i32 %x) {
+; CHECK-LABEL: @sub_false(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %sub = sub i32 1, %x
+  %cmp = icmp eq i32 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @sub_nsw_odd(i8 %x, i8 %c) {
+; CHECK-LABEL: @sub_nsw_odd(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i1 true
+;
+entry:
+  %sub = sub nsw i8 3, %x
+  %cmp = icmp ne i8 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @sub_nuw_odd(i8 %x, i8 %c) {
+; CHECK-LABEL: @sub_nuw_odd(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i1 true
+;
+entry:
+  %sub = sub nuw i8 3, %x
+  %cmp = icmp ne i8 %sub, %x
+  ret i1 %cmp
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3292,6 +3292,20 @@
     }
   }
 
+  // (sub nsw|nuw C, X) == X, C is odd  --> false
+  // (sub nsw|nuw C, X) != X, C is odd  --> true
+  // (sub 1, X) == X --> false
+  // (sub 1, X) != X --> true
+  if (LBO && LBO->getOperand(1) == RHS &&
+      match(LHS, m_Sub(m_APInt(C), m_Value()))) {
+    if ((LBO->hasNoSignedWrap() && C->srem(2) != 0) ||
+        (LBO->hasNoUnsignedWrap() && C->urem(2) != 0) || C->isOne()) {
+      if (Pred == CmpInst::ICMP_EQ)
+        return ConstantInt::getFalse(getCompareTy(LHS));
+      if (Pred == CmpInst::ICMP_NE)
+        return ConstantInt::getTrue(getCompareTy(LHS));
+    }
+  }
   // TODO: This is overly constrained. LHS can be any power-of-2.
   // (1 << X)  >u 0x8000 --> false
   // (1 << X) <=u 0x8000 --> true


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132989.457491.patch
Type: text/x-patch
Size: 1857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220902/9d1be240/attachment.bin>


More information about the llvm-commits mailing list