[llvm] [InstCombine] Add transforms `(icmp spred (and X, Y), X)` if `X` or `Y` are known signed/unsigned (PR #94417)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 5 09:54:50 PDT 2024


================
@@ -4745,6 +4745,40 @@ static Instruction *foldICmpAndXX(ICmpInst &I, const SimplifyQuery &Q,
                           Constant::getNullValue(Op1->getType()));
   }
 
+  if (!ICmpInst::isSigned(Pred))
+    return nullptr;
+
+  KnownBits KnownY = IC.computeKnownBits(A, /*Depth=*/0, &I);
+  // (X & NegY) spred X --> (X & NegY) upred X
+  if (KnownY.isNegative())
+    return new ICmpInst(ICmpInst::getUnsignedPredicate(Pred), Op0, Op1);
+
+  if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGT)
+    return nullptr;
+
+  if (KnownY.isNonNegative()) {
+    // (X & PosY) s<= X --> X s>= 0
+    if (Pred == ICmpInst::ICMP_SLE)
+      return new ICmpInst(ICmpInst::ICMP_SGE, Op1,
+                          Constant::getNullValue(Op1->getType()));
+    // (X & PosY) s> X --> X s< 0
+    if (Pred == ICmpInst::ICMP_SGT)
+      return new ICmpInst(ICmpInst::ICMP_SLT, Op1,
+                          Constant::getNullValue(Op1->getType()));
----------------
dtcxzyw wrote:

```suggestion
    // (X & PosY) s> X --> X s< 0
      return new ICmpInst(Pred == ICmpInst::ICMP_SLE ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SLT, Op1,
                          Constant::getNullValue(Op1->getType()));
```
or 
```suggestion
    // (X & PosY) s> X --> X s< 0
      return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
                          Constant::getNullValue(Op1->getType()));
```

https://github.com/llvm/llvm-project/pull/94417


More information about the llvm-commits mailing list