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 e925a2ba0240c7e75bdf46318716352acd353212..631c65a8642259dcea07acc3dba268e1f8ac0048 100644 (file)
 #define _NIR_SEARCH_HELPERS_
 
 #include "nir.h"
-
-static inline bool
-__is_power_of_two(unsigned int x)
-{
-   return ((x != 0) && !(x & (x - 1)));
-}
+#include "util/bitscan.h"
+#include <math.h>
 
 static inline bool
 is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
                     const uint8_t *swizzle)
 {
-   nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
-
-   /* only constant src's: */
-   if (!val)
+   /* 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_int:
-         if (val->i32[swizzle[i]] < 0)
-            return false;
-         if (!__is_power_of_two(val->i32[swizzle[i]]))
+      case nir_type_int: {
+         int64_t val = nir_src_comp_as_int(instr->src[src].src, swizzle[i]);
+         if (val <= 0 || !util_is_power_of_two_or_zero64(val))
             return false;
          break;
-      case nir_type_uint:
-         if (!__is_power_of_two(val->u32[swizzle[i]]))
+      }
+      case nir_type_uint: {
+         uint64_t val = nir_src_comp_as_uint(instr->src[src].src, swizzle[i]);
+         if (val == 0 || !util_is_power_of_two_or_zero64(val))
             return false;
          break;
+      }
       default:
          return false;
       }
@@ -69,20 +65,18 @@ static inline bool
 is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
                     const uint8_t *swizzle)
 {
-   nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
-
-   /* only constant src's: */
-   if (!val)
+   /* 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_int:
-         if (val->i32[swizzle[i]] > 0)
-            return false;
-         if (!__is_power_of_two(abs(val->i32[swizzle[i]])))
+      case nir_type_int: {
+         int64_t val = nir_src_comp_as_int(instr->src[src].src, swizzle[i]);
+         if (val >= 0 || !util_is_power_of_two_or_zero64(-val))
             return false;
          break;
+      }
       default:
          return false;
       }
@@ -95,15 +89,73 @@ static inline bool
 is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
                const uint8_t *swizzle)
 {
-   nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
+   /* 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;
+      }
+   }
 
-   if (!val)
+   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)
+{
+   if (nir_src_as_const_value(instr->src[src].src) == NULL)
+      return true;
+
    for (unsigned i = 0; i < num_components; i++) {
       switch (nir_op_infos[instr->op].input_types[src]) {
       case nir_type_float:
-         if (val->f32[swizzle[i]] < 0.0f || val->f32[swizzle[i]] > 1.0f)
+         if (nir_src_comp_as_float(instr->src[src].src, swizzle[i]) == 0.0)
+            return false;
+         break;
+      case nir_type_bool:
+      case nir_type_int:
+      case nir_type_uint:
+         if (nir_src_comp_as_uint(instr->src[src].src, swizzle[i]) == 0)
             return false;
          break;
       default:
@@ -115,25 +167,79 @@ is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
 }
 
 static inline bool
-is_used_more_than_once(nir_alu_instr *instr)
+is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
+             UNUSED const uint8_t *swizzle)
+{
+   return !nir_src_is_const(instr->src[src].src);
+}
+
+static inline bool
+is_not_fmul(nir_alu_instr *instr, unsigned src,
+            UNUSED unsigned num_components, UNUSED const uint8_t *swizzle)
+{
+   nir_alu_instr *src_alu =
+      nir_src_as_alu_instr(instr->src[src].src);
+
+   if (src_alu == NULL)
+      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
+is_used_once(nir_alu_instr *instr)
 {
    bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
    bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
 
-   if (zero_use && zero_if_use)
+   if (zero_if_use && zero_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))
+
+   if (!zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
+     return false;
+
+   if (!zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
+     return false;
+
+   if (!list_is_singular(&instr->dest.dest.ssa.if_uses) &&
+       !list_is_singular(&instr->dest.dest.ssa.uses))
       return false;
 
    return true;
 }
 
+static inline bool
+is_used_by_if(nir_alu_instr *instr)
+{
+   return !list_empty(&instr->dest.dest.ssa.if_uses);
+}
+
 static inline bool
 is_not_used_by_if(nir_alu_instr *instr)
 {
    return list_empty(&instr->dest.dest.ssa.if_uses);
 }
 
+static inline bool
+is_used_by_non_fsat(nir_alu_instr *instr)
+{
+   nir_foreach_use(src, &instr->dest.dest.ssa) {
+      const nir_instr *const user_instr = src->parent_instr;
+
+      if (user_instr->type != nir_instr_type_alu)
+         return true;
+
+      const nir_alu_instr *const user_alu = nir_instr_as_alu(user_instr);
+
+      assert(instr != user_alu);
+      if (user_alu->op != nir_op_fsat)
+         return true;
+   }
+
+   return false;
+}
+
 #endif /* _NIR_SEARCH_ */