nir/search: Fold src_is_bool()/alu_instr_is_bool() into src_is_type().
[mesa.git] / src / compiler / nir / nir_search.c
index 3a65ab189285036cb5c4d7450d548390b4757afc..bfa00c24a414b8bb140c3b9eaf321cc56ab79b8e 100644 (file)
@@ -42,32 +42,46 @@ match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
 
 static const uint8_t identity_swizzle[] = { 0, 1, 2, 3 };
 
-static bool alu_instr_is_bool(nir_alu_instr *instr);
-
+/**
+ * Check if a source produces a value of the given type.
+ *
+ * Used for satisfying 'a@type' constraints.
+ */
 static bool
-src_is_bool(nir_src src)
+src_is_type(nir_src src, nir_alu_type type)
 {
+   assert(type != nir_type_invalid);
+
    if (!src.is_ssa)
       return false;
-   if (src.ssa->parent_instr->type != nir_instr_type_alu)
-      return false;
-   return alu_instr_is_bool(nir_instr_as_alu(src.ssa->parent_instr));
-}
 
-static bool
-alu_instr_is_bool(nir_alu_instr *instr)
-{
-   switch (instr->op) {
-   case nir_op_iand:
-   case nir_op_ior:
-   case nir_op_ixor:
-      return src_is_bool(instr->src[0].src) && src_is_bool(instr->src[1].src);
-   case nir_op_inot:
-      return src_is_bool(instr->src[0].src);
-   default:
-      return (nir_alu_type_get_base_type(nir_op_infos[instr->op].output_type)
-             == nir_type_bool);
+   /* Turn nir_type_bool32 into nir_type_bool...they're the same thing. */
+   if (nir_alu_type_get_base_type(type) == nir_type_bool)
+      type = nir_type_bool;
+
+   if (src.ssa->parent_instr->type == nir_instr_type_alu) {
+      nir_alu_instr *src_alu = nir_instr_as_alu(src.ssa->parent_instr);
+      nir_alu_type output_type = nir_op_infos[src_alu->op].output_type;
+
+      if (type == nir_type_bool) {
+         switch (src_alu->op) {
+         case nir_op_iand:
+         case nir_op_ior:
+         case nir_op_ixor:
+            return src_is_type(src_alu->src[0].src, nir_type_bool) &&
+                   src_is_type(src_alu->src[1].src, nir_type_bool);
+         case nir_op_inot:
+            return src_is_type(src_alu->src[0].src, nir_type_bool);
+         default:
+            break;
+         }
+      }
+
+      return nir_alu_type_get_base_type(output_type) == type;
    }
+
+   /* don't know */
+   return false;
 }
 
 static bool
@@ -88,6 +102,11 @@ match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
    for (unsigned i = 0; i < num_components; ++i)
       new_swizzle[i] = instr->src[src].swizzle[swizzle[i]];
 
+   /* If the value has a specific bit size and it doesn't match, bail */
+   if (value->bit_size &&
+       nir_src_bit_size(instr->src[src].src) != value->bit_size)
+      return false;
+
    switch (value->type) {
    case nir_search_value_expression:
       if (!instr->src[src].src.is_ssa)
@@ -122,19 +141,12 @@ match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
              instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)
             return false;
 
-         if (var->type != nir_type_invalid) {
-            if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_alu)
-               return false;
-
-            nir_alu_instr *src_alu =
-               nir_instr_as_alu(instr->src[src].src.ssa->parent_instr);
+         if (var->cond && !var->cond(instr, src, num_components, new_swizzle))
+            return false;
 
-            if (nir_alu_type_get_base_type(nir_op_infos[src_alu->op].output_type) !=
-                var->type &&
-                !(nir_alu_type_get_base_type(var->type) == nir_type_bool &&
-                  alu_instr_is_bool(src_alu)))
-               return false;
-         }
+         if (var->type != nir_type_invalid &&
+             !src_is_type(instr->src[src].src, var->type))
+            return false;
 
          state->variables_seen |= (1 << var->variable);
          state->variables[var->variable].src = instr->src[src].src;
@@ -243,6 +255,10 @@ match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
 
    assert(instr->dest.dest.is_ssa);
 
+   if (expr->value.bit_size &&
+       instr->dest.dest.ssa.bit_size != expr->value.bit_size)
+      return false;
+
    state->inexact_match = expr->inexact || state->inexact_match;
    state->has_exact_alu = instr->exact || state->has_exact_alu;
    if (state->inexact_match && state->has_exact_alu)
@@ -353,6 +369,11 @@ build_bitsize_tree(void *mem_ctx, struct match_state *state,
    }
    }
 
+   if (value->bit_size) {
+      assert(!tree->is_dest_sized || tree->dest_size == value->bit_size);
+      tree->common_size = value->bit_size;
+   }
+
    return tree;
 }
 
@@ -477,7 +498,8 @@ construct_value(const nir_search_value *value,
 
    case nir_search_value_constant: {
       const nir_search_constant *c = nir_search_value_as_constant(value);
-      nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1);
+      nir_load_const_instr *load =
+         nir_load_const_instr_create(mem_ctx, 1, bitsize->dest_size);
 
       switch (c->type) {
       case nir_type_float:
@@ -520,6 +542,7 @@ construct_value(const nir_search_value *value,
          default:
             unreachable("unknown bit size");
          }
+         break;
 
       case nir_type_bool32:
          load->value.u32[0] = c->data.u;
@@ -528,8 +551,6 @@ construct_value(const nir_search_value *value,
          unreachable("Invalid alu source type");
       }
 
-      load->def.bit_size = bitsize->dest_size;
-
       nir_instr_insert_before(instr, &load->instr);
 
       nir_alu_src val;