From 93a47bab04287cac4d2381f238a145f6730a715e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Sch=C3=BCrmann?= Date: Tue, 14 Feb 2023 09:49:37 +0100 Subject: [PATCH] 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 Part-of: --- 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 3199af6..c14469d 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"); -- 2.7.4