nir/algebraic: Commute 1-fsat(a) to fsat(1-a) for all non-fmul instructions
[mesa.git] / src / compiler / nir / nir_search_helpers.h
index 91d638a488f17f3b49144c10e41966a70a09ee8c..631c65a8642259dcea07acc3dba268e1f8ac0048 100644 (file)
@@ -109,6 +109,36 @@ is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
    return true;
 }
 
+/**
+ * Exclusive compare with (0, 1).
+ *
+ * This differs from \c is_zero_to_one because that function tests 0 <= src <=
+ * 1 while this function tests 0 < src < 1.
+ */
+static inline bool
+is_gt_0_and_lt_1(nir_alu_instr *instr, unsigned src, unsigned num_components,
+                 const uint8_t *swizzle)
+{
+   /* only constant srcs: */
+   if (!nir_src_is_const(instr->src[src].src))
+      return false;
+
+   for (unsigned i = 0; i < num_components; i++) {
+      switch (nir_op_infos[instr->op].input_types[src]) {
+      case nir_type_float: {
+         double val = nir_src_comp_as_float(instr->src[src].src, swizzle[i]);
+         if (isnan(val) || val <= 0.0f || val >= 1.0f)
+            return false;
+         break;
+      }
+      default:
+         return false;
+      }
+   }
+
+   return true;
+}
+
 static inline bool
 is_not_const_zero(nir_alu_instr *instr, unsigned src, unsigned num_components,
                   const uint8_t *swizzle)
@@ -144,19 +174,19 @@ is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
 }
 
 static inline bool
-is_used_more_than_once(nir_alu_instr *instr)
+is_not_fmul(nir_alu_instr *instr, unsigned src,
+            UNUSED unsigned num_components, UNUSED const uint8_t *swizzle)
 {
-   bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
-   bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
+   nir_alu_instr *src_alu =
+      nir_src_as_alu_instr(instr->src[src].src);
 
-   if (zero_use && zero_if_use)
-      return false;
-   else if (zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
-      return false;
-   else if (zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
-      return false;
+   if (src_alu == NULL)
+      return true;
 
-   return true;
+   if (src_alu->op == nir_op_fneg)
+      return is_not_fmul(src_alu, 0, 0, NULL);
+
+   return src_alu->op != nir_op_fmul;
 }
 
 static inline bool