Module: Mesa Branch: main Commit: 93a47bab04287cac4d2381f238a145f6730a715e URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=93a47bab04287cac4d2381f238a145f6730a715e
Author: Daniel Schürmann <[email protected]> Date: Tue Feb 14 09:49:37 2023 +0100 nir: simplify nir_block_cf_tree_{next|prev} Removes some case distinction by first checking if this is the first/last block of a cf_node. Reviewed-by: Faith Ekstrand <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13962> --- src/compiler/nir/nir.c | 54 ++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index 3199af67a4f..c14469d83f8 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -1917,30 +1917,29 @@ nir_block_cf_tree_next(nir_block *block) return nir_cf_node_cf_tree_first(cf_next); nir_cf_node *parent = block->cf_node.parent; + if (parent->type == nir_cf_node_function) + return NULL; + + /* Is this the last block of a cf_node? Return the following block */ + if (block == nir_cf_node_cf_tree_last(parent)) + return nir_cf_node_as_block(nir_cf_node_next(parent)); switch (parent->type) { case nir_cf_node_if: { - /* Are we at the end of the if? Go to the beginning of the else */ + /* We are at the end of the if. Go to the beginning of the else */ nir_if *if_stmt = nir_cf_node_as_if(parent); - if (block == nir_if_last_then_block(if_stmt)) - return nir_if_first_else_block(if_stmt); - - assert(block == nir_if_last_else_block(if_stmt)); - return nir_cf_node_as_block(nir_cf_node_next(parent)); + assert(block == nir_if_last_then_block(if_stmt)); + return nir_if_first_else_block(if_stmt); } case nir_cf_node_loop: { + /* We are at the end of the body and there is a continue construct */ nir_loop *loop = nir_cf_node_as_loop(parent); - if (block == nir_loop_last_block(loop) && - nir_loop_has_continue_construct(loop)) - return nir_loop_first_continue_block(loop); - - return nir_cf_node_as_block(nir_cf_node_next(parent)); + assert(block == nir_loop_last_block(loop) && + nir_loop_has_continue_construct(loop)); + return nir_loop_first_continue_block(loop); } - case nir_cf_node_function: - return NULL; - default: unreachable("unknown cf node type"); } @@ -1961,28 +1960,27 @@ nir_block_cf_tree_prev(nir_block *block) return nir_cf_node_cf_tree_last(cf_prev); nir_cf_node *parent = block->cf_node.parent; + if (parent->type == nir_cf_node_function) + return NULL; + + /* Is this the first block of a cf_node? Return the previous block */ + if (block == nir_cf_node_cf_tree_first(parent)) + return nir_cf_node_as_block(nir_cf_node_prev(parent)); switch (parent->type) { case nir_cf_node_if: { - /* Are we at the beginning of the else? Go to the end of the if */ + /* We are at the beginning of the else. Go to the end of the if */ nir_if *if_stmt = nir_cf_node_as_if(parent); - if (block == nir_if_first_else_block(if_stmt)) - return nir_if_last_then_block(if_stmt); - - assert(block == nir_if_first_then_block(if_stmt)); - return nir_cf_node_as_block(nir_cf_node_prev(parent)); + assert(block == nir_if_first_else_block(if_stmt)); + return nir_if_last_then_block(if_stmt); } case nir_cf_node_loop: { + /* We are at the beginning of the continue construct. */ nir_loop *loop = nir_cf_node_as_loop(parent); - if (nir_loop_has_continue_construct(loop) && - block == nir_loop_first_continue_block(loop)) - return nir_loop_last_block(loop); - - assert(block == nir_loop_first_block(loop)); - return nir_cf_node_as_block(nir_cf_node_prev(parent)); + assert(nir_loop_has_continue_construct(loop) && + block == nir_loop_first_continue_block(loop)); + return nir_loop_last_block(loop); } - case nir_cf_node_function: - return NULL; default: unreachable("unknown cf node type");
