[PATCH] D132989: [InstSimplify] Odd - X ==/!= X -> false/true

Liao Chunyu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 4 20:29:58 PDT 2022


liaolucy updated this revision to Diff 457886.
liaolucy marked 5 inline comments as done.
liaolucy added a comment.

1. Move testcases to  tests/Transforms/InstSimplify
2. Use m_Specific


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/InstSimplify/icmp.ll


Index: llvm/test/Transforms/InstSimplify/icmp.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/icmp.ll
+++ llvm/test/Transforms/InstSimplify/icmp.ll
@@ -211,3 +211,52 @@
   %sub2 = sub i32 42, %p2
   br label %loop
 }
+
+define i1 @sub_false(i32 %x) {
+; CHECK-LABEL: @sub_false(
+; CHECK-NEXT:    ret i1 false
+;
+  %sub = sub i32 1, %x
+  %cmp = icmp eq i32 %sub, %x
+  ret i1 %cmp
+}
+
+define i1 @sub_swap(i8 %x) {
+; CHECK-LABEL: @sub_swap(
+; CHECK-NEXT:    ret i1 true
+;
+  %sub = sub i8 3, %x
+  %cmp = icmp ne i8 %x, %sub
+  ret i1 %cmp
+}
+
+define <2 x i1> @sub_odd(<2 x i8> %x) {
+; CHECK-LABEL: @sub_odd(
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
+;
+  %sub = sub <2 x i8> <i8 3, i8 3>, %x
+  %cmp = icmp ne <2 x i8> %sub, %x
+  ret <2 x i1> %cmp
+}
+
+define <2 x i1> @sub_odd_poison(<2 x i8> %x) {
+; CHECK-LABEL: @sub_odd_poison(
+; CHECK-NEXT:    [[SUB:%.*]] = sub <2 x i8> <i8 poison, i8 1>, [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i8> [[SUB]], [[X]]
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %sub = sub <2 x i8> <i8 poison, i8 1>, %x
+  %cmp = icmp ne <2 x i8> %sub, %x
+  ret <2 x i1> %cmp
+}
+
+define i1 @sub_even(i8 %x) {
+; CHECK-LABEL: @sub_even(
+; CHECK-NEXT:    [[SUB:%.*]] = sub i8 2, [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[SUB]], [[X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %sub = sub i8 2, %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,19 @@
     }
   }
 
+  // (sub C, X) == X, C is odd  --> false
+  // (sub C, X) != X, C is odd  --> true
+  if ((LBO && LBO->getOperand(1) == RHS &&
+      match(LHS, m_Sub(m_APInt(C), m_Specific(RHS)))) || 
+      (RBO && RBO->getOperand(1) == LHS &&
+      match(RHS, m_Sub(m_APInt(C), m_Specific(LHS))))) {
+    if ((*C & 1) == 1) {
+      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.457886.patch
Type: text/x-patch
Size: 2377 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220905/1980f289/attachment.bin>


More information about the llvm-commits mailing list