[Mesa-dev] [PATCH 049/133] nir: Add a function to detect if a block is immediately followed by an if
Connor Abbott
cwabbott0 at gmail.com
Wed Dec 17 13:16:47 PST 2014
On Tue, Dec 16, 2014 at 1:04 AM, Jason Ekstrand <jason at jlekstrand.net> wrote:
> Since we don't actually have an "if" instruction, this is a very common
> pattern when iterating over instructions. This adds a helper function for
> it to make things a little less painful.
> ---
> src/glsl/nir/nir.c | 17 +++++++++++++++++
> src/glsl/nir/nir.h | 5 +++++
> src/glsl/nir/nir_from_ssa.c | 9 +++------
> src/glsl/nir/nir_opt_dce.c | 12 +++++-------
> 4 files changed, 30 insertions(+), 13 deletions(-)
>
> diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
> index 5dbcc77..e90eeda 100644
> --- a/src/glsl/nir/nir.c
> +++ b/src/glsl/nir/nir.c
> @@ -1686,6 +1686,23 @@ nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
> return true;
> }
>
> +nir_if *
> +nir_block_following_if(nir_block *block)
Could you rename this to "nir_block_get_following_if"? I read this as
"block following if" (i.e. finding the block after an if statement)
and was confused at first. Also, it's generally a good idea to name
functions with verbs.
> +{
> + if (exec_node_is_tail_sentinel(&block->cf_node.node))
> + return NULL;
> +
> + if (nir_cf_node_is_last(&block->cf_node))
> + return NULL;
> +
> + nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
> +
> + if (next_node->type != nir_cf_node_if)
> + return NULL;
> +
> + return nir_cf_node_as_if(next_node);
> +}
> +
> static bool
> index_block(nir_block *block, void *state)
> {
> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
> index 8d5f6b8..f405694 100644
> --- a/src/glsl/nir/nir.h
> +++ b/src/glsl/nir/nir.h
> @@ -1275,6 +1275,11 @@ typedef bool (*nir_foreach_block_cb)(nir_block *block, void *state);
> bool nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb,
> void *state);
>
> +/* If the following CF node is an if, this function returns that if.
> + * Otherwise, it returns NULL.
> + */
> +nir_if *nir_block_following_if(nir_block *block);
> +
> void nir_index_local_regs(nir_function_impl *impl);
> void nir_index_global_regs(nir_shader *shader);
> void nir_index_ssa_defs(nir_function_impl *impl);
> diff --git a/src/glsl/nir/nir_from_ssa.c b/src/glsl/nir/nir_from_ssa.c
> index 04c8103..a26f0c4 100644
> --- a/src/glsl/nir/nir_from_ssa.c
> +++ b/src/glsl/nir/nir_from_ssa.c
> @@ -109,12 +109,9 @@ convert_from_ssa_block(nir_block *block, void *void_state)
> }
> }
>
> - if (block->cf_node.node.next != NULL && /* check that we aren't the end node */
> - !nir_cf_node_is_last(&block->cf_node) &&
> - nir_cf_node_next(&block->cf_node)->type == nir_cf_node_if) {
> - nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_next(&block->cf_node));
> - rewrite_ssa_src(&if_stmt->condition, state);
> - }
> + nir_if *following_if = nir_block_following_if(block);
> + if (following_if)
> + rewrite_ssa_src(&following_if->condition, state);
>
> return true;
> }
> diff --git a/src/glsl/nir/nir_opt_dce.c b/src/glsl/nir/nir_opt_dce.c
> index c18ba32..c3bbcb4 100644
> --- a/src/glsl/nir/nir_opt_dce.c
> +++ b/src/glsl/nir/nir_opt_dce.c
> @@ -123,13 +123,11 @@ init_block_cb(nir_block *block, void *_state)
> nir_foreach_instr(block, instr)
> init_instr(instr, worklist);
>
> - if (block->cf_node.node.next != NULL && /* check that we aren't the end node */
> - !nir_cf_node_is_last(&block->cf_node) &&
> - nir_cf_node_next(&block->cf_node)->type == nir_cf_node_if) {
> - nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_next(&block->cf_node));
> - if (if_stmt->condition.is_ssa &&
> - !if_stmt->condition.ssa->parent_instr->live)
> - worklist_push(worklist, if_stmt->condition.ssa->parent_instr);
> + nir_if *following_if = nir_block_following_if(block);
> + if (following_if) {
> + if (following_if->condition.is_ssa &&
> + !following_if->condition.ssa->parent_instr->live)
> + worklist_push(worklist, following_if->condition.ssa->parent_instr);
> }
>
> return true;
> --
> 2.2.0
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/mesa-dev
More information about the mesa-dev
mailing list