[llvm] [CVP] #114820 add 'samesign' for 'icmp' (PR #115642)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 10 01:03:14 PST 2024


================
@@ -288,24 +289,37 @@ static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
   if (!Cmp->getOperand(0)->getType()->isIntOrIntVectorTy())
     return false;
 
-  if (!Cmp->isSigned())
+  if (!(Cmp->isSigned() || (Cmp->isUnsigned() && !Cmp->hasSameSign())))
     return false;
 
-  ICmpInst::Predicate UnsignedPred =
-      ConstantRange::getEquivalentPredWithFlippedSignedness(
-          Cmp->getPredicate(),
-          LVI->getConstantRangeAtUse(Cmp->getOperandUse(0),
-                                     /*UndefAllowed*/ true),
-          LVI->getConstantRangeAtUse(Cmp->getOperandUse(1),
-                                     /*UndefAllowed*/ true));
+  bool Changed = false;
 
-  if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
-    return false;
+  ConstantRange CR1 = LVI->getConstantRangeAtUse(Cmp->getOperandUse(0),
+                                                 /*UndefAllowed*/ true),
+                CR2 = LVI->getConstantRangeAtUse(Cmp->getOperandUse(1),
+                                                 /*UndefAllowed*/ true);
 
-  ++NumSICmps;
-  Cmp->setPredicate(UnsignedPred);
+  if (Cmp->isSigned()) {
+    ICmpInst::Predicate UnsignedPred =
+        ConstantRange::getEquivalentPredWithFlippedSignedness(
+            Cmp->getPredicate(), CR1, CR2);
 
-  return true;
+    if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
+      return false;
+
+    ++NumSICmps;
+    Cmp->setPredicate(UnsignedPred);
+    Changed = true;
+  }
+
+  auto Op0Known = CR1.toKnownBits(), Op1Known = CR2.toKnownBits();
+  if ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) ||
+      (Op0Known.One.isNegative() && Op1Known.One.isNegative())) {
----------------
dtcxzyw wrote:

```suggestion
  if ((CR1.isAllNonNegative() && CR2.isAllNonNegative()) ||
      (CR1.isAllNegative() && CR2.isAllNegative())) {
```

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


More information about the llvm-commits mailing list