nir: Don't require a function in ssa_def_init
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 20 Nov 2014 00:06:32 +0000 (16:06 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 15 Jan 2015 15:19:02 +0000 (07:19 -0800)
Instead, we give SSA definitions a temporary index of 0xFFFFFFFF if the
instruction does not have a block and a proper index when it actually gets
added to the list.

Reviewed-by: Connor Abbott <cwabbott0@gmail.com>
src/glsl/nir/nir.c
src/glsl/nir/nir.h
src/glsl/nir/nir_from_ssa.c
src/glsl/nir/nir_opt_peephole_ffma.c
src/glsl/nir/nir_opt_peephole_select.c
src/glsl/nir/nir_to_ssa.c

index 3f04de2fee69f8563d1e6d7c840f530e5a82f624..281683a542da350565cc8c0c2931946d7ffa9600 100644 (file)
@@ -1188,17 +1188,27 @@ add_use_cb(nir_src *src, void *state)
    return true;
 }
 
+static void
+add_ssa_def(nir_instr *instr, nir_ssa_def *def)
+{
+   if (instr->block && def->index == UINT_MAX) {
+      nir_function_impl *impl =
+         nir_cf_node_get_function(&instr->block->cf_node);
+
+      def->index = impl->ssa_alloc++;
+   }
+}
+
 static bool
 add_def_cb(nir_dest *dest, void *state)
 {
    nir_instr *instr = (nir_instr *) state;
 
-   if (dest->is_ssa)
-      return true;
-
-   nir_register *reg = dest->reg.reg;
-
-   _mesa_set_add(reg->defs, _mesa_hash_pointer(instr), instr);
+   if (dest->is_ssa) {
+      add_ssa_def(instr, &dest->ssa);
+   } else {
+      _mesa_set_add(dest->reg.reg->defs, _mesa_hash_pointer(instr), instr);
+   }
 
    return true;
 }
@@ -1206,8 +1216,12 @@ add_def_cb(nir_dest *dest, void *state)
 static void
 add_defs_uses(nir_instr *instr)
 {
-   nir_foreach_src(instr, add_use_cb, instr);
-   nir_foreach_dest(instr, add_def_cb, instr);
+   if (instr->type == nir_instr_type_ssa_undef) {
+      add_ssa_def(instr, &nir_instr_as_ssa_undef(instr)->def);
+   } else {
+      nir_foreach_src(instr, add_use_cb, instr);
+      nir_foreach_dest(instr, add_def_cb, instr);
+   }
 }
 
 void
@@ -1748,17 +1762,25 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
 }
 
 void
-nir_ssa_def_init(nir_function_impl *impl, nir_instr *instr, nir_ssa_def *def,
+nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
                  unsigned num_components, const char *name)
 {
    void *mem_ctx = ralloc_parent(instr);
 
    def->name = name;
-   def->index = impl->ssa_alloc++;
    def->parent_instr = instr;
    def->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
    def->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
    def->num_components = num_components;
+
+   if (instr->block) {
+      nir_function_impl *impl =
+         nir_cf_node_get_function(&instr->block->cf_node);
+
+      def->index = impl->ssa_alloc++;
+   } else {
+      def->index = UINT_MAX;
+   }
 }
 
 struct ssa_def_rewrite_state {
index e977159584f7430d4869ca1273915dda1dc637f5..ac2329fbbc8f8f1b82dc57f7a1218dbcc52dda37 100644 (file)
@@ -1318,9 +1318,8 @@ bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
 
-void nir_ssa_def_init(nir_function_impl *impl, nir_instr *instr,
-                      nir_ssa_def *def, unsigned num_components,
-                      const char *name);
+void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
+                      unsigned num_components, const char *name);
 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx);
 
 /* visits basic blocks in source-code order */
index 3186c7d4aa88487f67efb2bc70b9b47c35bcbce6..9b7c931c072a86b7a5b32efa19f8ac234c49382d 100644 (file)
@@ -314,7 +314,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
                        _mesa_hash_pointer(&pcopy->instr), &pcopy->instr);
 
          copy->dest.is_ssa = true;
-         nir_ssa_def_init(state->impl, &pcopy->instr, &copy->dest.ssa,
+         nir_ssa_def_init(&pcopy->instr, &copy->dest.ssa,
                           phi->dest.ssa.num_components, src->src.ssa->name);
 
          struct set_entry *entry = _mesa_set_search(src->src.ssa->uses,
@@ -339,7 +339,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
       exec_list_push_tail(&block_pcopy->copies, &copy->node);
 
       copy->dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &block_pcopy->instr, &copy->dest.ssa,
+      nir_ssa_def_init(&block_pcopy->instr, &copy->dest.ssa,
                        phi->dest.ssa.num_components, phi->dest.ssa.name);
 
       nir_src copy_dest_src = {
index 2c9b8e570d4e4e7e16c2bc0dff09133074d2b392..1165d7aa60559465fc02e120d357b9c580d3054c 100644 (file)
@@ -34,7 +34,6 @@
 
 struct peephole_ffma_state {
    void *mem_ctx;
-   nir_function_impl *impl;
    bool progress;
 };
 
@@ -135,7 +134,7 @@ nir_opt_peephole_ffma_block(nir_block *block, void *void_state)
 
       if (add->dest.dest.is_ssa) {
          ffma->dest.dest.is_ssa = true;
-         nir_ssa_def_init(state->impl, &ffma->instr, &ffma->dest.dest.ssa,
+         nir_ssa_def_init(&ffma->instr, &ffma->dest.dest.ssa,
                           add->dest.dest.ssa.num_components,
                           add->dest.dest.ssa.name);
 
@@ -165,7 +164,6 @@ nir_opt_peephole_ffma_impl(nir_function_impl *impl)
    struct peephole_ffma_state state;
 
    state.mem_ctx = ralloc_parent(impl);
-   state.impl = impl;
    state.progress = false;
 
    nir_foreach_block(impl, nir_opt_peephole_ffma_block, &state);
index b3b54c1a1cbe1c50552210dd0e0a5cc445d546b3..4fc7726a85a34ab4de9b0cbcce98911761b8e909 100644 (file)
@@ -48,7 +48,6 @@
 
 struct peephole_select_state {
    void *mem_ctx;
-   nir_function_impl *impl;
    bool progress;
 };
 
@@ -163,7 +162,7 @@ nir_opt_peephole_select_block(nir_block *block, void *void_state)
       }
 
       sel->dest.dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &sel->instr, &sel->dest.dest.ssa,
+      nir_ssa_def_init(&sel->instr, &sel->dest.dest.ssa,
                        phi->dest.ssa.num_components, phi->dest.ssa.name);
       sel->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
 
@@ -190,7 +189,6 @@ nir_opt_peephole_select_impl(nir_function_impl *impl)
 
    state.mem_ctx = ralloc_parent(impl);
    state.progress = false;
-   state.impl = impl;
 
    nir_foreach_block(impl, nir_opt_peephole_select_block, &state);
 
index fb36ff4f6499645353d5b38739804d43cd5287e2..a76b7acc132b5dd6c058fbe9b8709a9b7cd73411 100644 (file)
@@ -163,7 +163,7 @@ static nir_ssa_def *get_ssa_src(nir_register *reg, rewrite_state *state)
        * to preserve the information that this source is undefined
        */
       nir_ssa_undef_instr *instr = nir_ssa_undef_instr_create(state->mem_ctx);
-      nir_ssa_def_init(state->impl, &instr->instr, &instr->def,
+      nir_ssa_def_init(&instr->instr, &instr->def,
                        reg->num_components, NULL);
 
       /*
@@ -246,7 +246,7 @@ rewrite_def_forwards(nir_dest *dest, void *_state)
       name = ralloc_asprintf(state->mem_ctx, "%s_%u", dest->reg.reg->name,
                              state->states[index].num_defs);
 
-   nir_ssa_def_init(state->impl, state->parent_instr, &dest->ssa,
+   nir_ssa_def_init(state->parent_instr, &dest->ssa,
                     reg->num_components, name);
 
    /* push our SSA destination on the stack */
@@ -312,7 +312,7 @@ rewrite_alu_instr_forward(nir_alu_instr *instr, rewrite_state *state)
 
       instr->dest.write_mask = (1 << num_components) - 1;
       instr->dest.dest.is_ssa = true;
-      nir_ssa_def_init(state->impl, &instr->instr, &instr->dest.dest.ssa,
+      nir_ssa_def_init(&instr->instr, &instr->dest.dest.ssa,
                        num_components, name);
 
       if (nir_op_infos[instr->op].output_size == 0) {