nir: simplify nir_block_cf_tree_{next|prev}
authorDaniel Schürmann <daniel@schuermann.dev>
Tue, 14 Feb 2023 08:49:37 +0000 (09:49 +0100)
committerMarge Bot <emma+marge@anholt.net>
Tue, 21 Feb 2023 10:41:11 +0000 (10:41 +0000)
Removes some case distinction by first checking if this is
the first/last block of a cf_node.

Reviewed-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13962>

src/compiler/nir/nir.c

index 3199af6..c14469d 100644 (file)
@@ -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");