[llvm] [ValueTracking] Add support for non-splat vecs in cmpExcludesZero (PR #68331)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 5 09:56:18 PDT 2023


================
@@ -573,11 +573,31 @@ static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
 
   // All other predicates - rely on generic ConstantRange handling.
   const APInt *C;
-  if (!match(RHS, m_APInt(C)))
-    return false;
+  if (match(RHS, m_APInt(C))) {
+    ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
+    return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
+  }
 
-  ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
-  return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
+  auto *FVTy = dyn_cast<FixedVectorType>(RHS->getType());
+  if (FVTy == nullptr)
+    return false;
+  Constant *VC = nullptr;
+  if (!match(RHS, m_ImmConstant(VC)))
+    return false;
+  for (unsigned EleIdx = 0, NEle = FVTy->getNumElements(); EleIdx < NEle;
+       ++EleIdx) {
+    Constant *EleC = VC->getAggregateElement(EleIdx);
+    if (EleC == nullptr)
+      return false;
+    ConstantInt *EleCI = dyn_cast<ConstantInt>(EleC);
+    if (EleCI == nullptr)
+      return false;
+    ConstantRange TrueValues =
+        ConstantRange::makeExactICmpRegion(Pred, EleCI->getValue());
+    if (TrueValues.contains(APInt::getZero(EleCI->getBitWidth())))
+      return false;
+  }
+  return true;
----------------
dtcxzyw wrote:

```suggestion
  auto Zero = APInt::getZero(FVTy->getScalarSizeInBits());
  return none_of(VC->operand_values(), [&] (Value *EleC) {
      const APInt *EleCI;
      if (match(EleC, m_APInt(EleCI))
        return ConstantRange::makeExactICmpRegion(Pred, *EleCI).contains(Zero);
      return true;
  });
```


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


More information about the llvm-commits mailing list