[InstSimplify] allow poison/undef in constant match for "C - X ==/!= X -> false/true"
authorSanjay Patel <spatel@rotateright.com>
Tue, 6 Sep 2022 12:07:31 +0000 (08:07 -0400)
committerSanjay Patel <spatel@rotateright.com>
Tue, 6 Sep 2022 12:19:30 +0000 (08:19 -0400)
This fold was added with 5e9522c311dd, but over-specified.
We can assume that an undef element is an odd number:
https://alive2.llvm.org/ce/z/djQmWU

llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/icmp.ll

index 27ec7a0..eebb5f1 100644 (file)
@@ -3128,14 +3128,9 @@ static Value *simplifyICmpWithBinOpOnLHS(CmpInst::Predicate Pred,
 
   // (sub C, X) == X, C is odd  --> false
   // (sub C, X) != X, C is odd  --> true
-  if (match(LBO, m_Sub(m_APInt(C), m_Specific(RHS)))) {
-    if ((*C & 1) == 1) {
-      if (Pred == CmpInst::ICMP_EQ)
-        return getFalse(ITy);
-      if (Pred == CmpInst::ICMP_NE)
-        return getTrue(ITy);
-    }
-  }
+  if (match(LBO, m_Sub(m_APIntAllowUndef(C), m_Specific(RHS))) &&
+      (*C & 1) == 1 && ICmpInst::isEquality(Pred))
+    return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
 
   return nullptr;
 }
index c6d2f3c..6f0a3fa 100644 (file)
@@ -241,9 +241,7 @@ define <2 x i1> @sub_odd(<2 x i8> %x) {
 
 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]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %sub = sub <2 x i8> <i8 poison, i8 1>, %x
   %cmp = icmp ne <2 x i8> %sub, %x