ValueTracking: Handle non-splat vectors in computeKnownFPClass
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Mon, 10 Apr 2023 19:41:46 +0000 (15:41 -0400)
committerMatt Arsenault <arsenm2@gmail.com>
Fri, 14 Apr 2023 21:41:26 +0000 (17:41 -0400)
Avoids some regressions when the implementation of isKnownNeverNaN is
replaced with computeKnownFPClass.

llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/Attributor/nofpclass.ll

index b4ff594..51f8894 100644 (file)
@@ -4362,12 +4362,38 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
 
   assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
 
-  const APFloat *C;
-  if (match(V, m_APFloat(C))) {
-    // We know all of the classes for a scalar constant or a splat vector
-    // constant!
-    Known.KnownFPClasses = C->classify();
-    Known.SignBit = C->isNegative();
+  if (auto *CFP = dyn_cast_or_null<ConstantFP>(V)) {
+    Known.KnownFPClasses = CFP->getValueAPF().classify();
+    Known.SignBit = CFP->isNegative();
+    return;
+  }
+
+  // Try to handle fixed width vector constants
+  auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
+  const Constant *CV = dyn_cast<Constant>(V);
+  if (VFVTy && CV) {
+    Known.KnownFPClasses = fcNone;
+
+    // For vectors, verify that each element is not NaN.
+    unsigned NumElts = VFVTy->getNumElements();
+    for (unsigned i = 0; i != NumElts; ++i) {
+      Constant *Elt = CV->getAggregateElement(i);
+      if (!Elt) {
+        Known = KnownFPClass();
+        return;
+      }
+      if (isa<UndefValue>(Elt))
+        continue;
+      auto *CElt = dyn_cast<ConstantFP>(Elt);
+      if (!CElt) {
+        Known = KnownFPClass();
+        return;
+      }
+
+      KnownFPClass KnownElt{CElt->getValueAPF().classify(), CElt->isNegative()};
+      Known |= KnownElt;
+    }
+
     return;
   }
 
index a363933..52c9c2e 100644 (file)
@@ -97,7 +97,7 @@ define <2 x double> @returned_strange_constant_vector_elt() {
 
 ; Test a vector element that's an undef/poison
 define <3 x double> @returned_undef_constant_vector_elt() {
-; CHECK-LABEL: define <3 x double> @returned_undef_constant_vector_elt() {
+; CHECK-LABEL: define nofpclass(nan inf pzero sub norm) <3 x double> @returned_undef_constant_vector_elt() {
 ; CHECK-NEXT:    call void @unknown()
 ; CHECK-NEXT:    ret <3 x double> <double -0.000000e+00, double poison, double undef>
 ;
@@ -106,7 +106,7 @@ define <3 x double> @returned_undef_constant_vector_elt() {
 }
 
 define <2 x double> @returned_qnan_zero_vector() {
-; CHECK-LABEL: define noundef <2 x double> @returned_qnan_zero_vector() {
+; CHECK-LABEL: define noundef nofpclass(snan inf nzero sub norm) <2 x double> @returned_qnan_zero_vector() {
 ; CHECK-NEXT:    call void @unknown()
 ; CHECK-NEXT:    ret <2 x double> <double 0x7FF8000000000000, double 0.000000e+00>
 ;