pan/bi: Add dead branch elimination pass
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Thu, 21 Jan 2021 00:09:34 +0000 (19:09 -0500)
committerMarge Bot <eric+marge@anholt.net>
Fri, 29 Jan 2021 16:55:43 +0000 (16:55 +0000)
Ported from Midgard due to the same quirk of our code generation.
Additional validation, though.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8723>

src/panfrost/bifrost/bifrost_compile.c

index a51b3e4..03c8f76 100644 (file)
@@ -2281,6 +2281,28 @@ bifrost_nir_lower_i8_fragout(nir_shader *shader)
                         NULL);
 }
 
+/* Dead code elimination for branches at the end of a block - only one branch
+ * per block is legal semantically, but unreachable jumps can be generated */
+
+static void
+bi_cull_dead_branch(bi_block *block)
+{
+        bool branched = false;
+        ASSERTED bool was_jump = false;
+
+        bi_foreach_instr_in_block_safe(block, ins) {
+                if (!ins->branch_target) continue;
+
+                if (branched) {
+                        assert(was_jump);
+                        bi_remove_instruction(ins);
+                }
+
+                branched = true;
+                was_jump = ins->op == BI_OPCODE_JUMP;
+        }
+}
+
 panfrost_program *
 bifrost_compile_shader_nir(void *mem_ctx, nir_shader *nir,
                            const struct panfrost_compile_inputs *inputs)
@@ -2356,6 +2378,8 @@ bifrost_compile_shader_nir(void *mem_ctx, nir_shader *nir,
                 /* Name blocks now that we're done emitting so the order is
                  * consistent */
                 block->base.name = block_source_count++;
+
+                bi_cull_dead_branch(block);
         }
 
         bool progress = false;