pan/midgard: Optimize branches with inverted arguments
authorAfonso Bordado <afonsobordado@az8.co>
Fri, 27 Dec 2019 17:09:51 +0000 (17:09 +0000)
committerAfonso Bordado <afonsobordado@az8.co>
Tue, 31 Dec 2019 20:01:16 +0000 (20:01 +0000)
Remove the invert on arguments to branches, and invert the branch
condition instead. This saves one instruction per inverted argument.

Closes #2088

Signed-off-by: Afonso Bordado <afonsobordado@az8.co>
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
src/panfrost/midgard/compiler.h
src/panfrost/midgard/midgard_compile.c
src/panfrost/midgard/midgard_opt_invert.c

index 85b7a87405e453ddb025630ac0bdcc20c3987c04..024aaf93f64d800ecad9b8c8938fe52ccc059ec1 100644 (file)
@@ -705,5 +705,6 @@ bool midgard_opt_fuse_dest_invert(compiler_context *ctx, midgard_block *block);
 bool midgard_opt_csel_invert(compiler_context *ctx, midgard_block *block);
 bool midgard_opt_promote_fmov(compiler_context *ctx, midgard_block *block);
 bool midgard_opt_drop_cmp_invert(compiler_context *ctx, midgard_block *block);
+bool midgard_opt_invert_branch(compiler_context *ctx, midgard_block *block);
 
 #endif
index b57291dad05a687cf249c11c27016dd553722dc8..306d63374b154cdc3385dade79334fc66b2ec842 100644 (file)
@@ -2679,6 +2679,7 @@ midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_bl
                         progress |= midgard_opt_fuse_dest_invert(ctx, block);
                         progress |= midgard_opt_csel_invert(ctx, block);
                         progress |= midgard_opt_drop_cmp_invert(ctx, block);
+                        progress |= midgard_opt_invert_branch(ctx, block);
                 }
         } while (progress);
 
index 4527102553b02d95a260027cfbbac540b9e0f3de..e11e4c0fdec2c74c1a10f29dc910630fb57843c0 100644 (file)
@@ -375,3 +375,27 @@ midgard_opt_drop_cmp_invert(compiler_context *ctx, midgard_block *block)
 
         return progress;
 }
+
+/* Optimizes branches with inverted arguments by inverting the
+ * branch condition instead of the argument condition.
+ */
+bool
+midgard_opt_invert_branch(compiler_context *ctx, midgard_block *block)
+{
+        bool progress = false;
+
+        mir_foreach_instr_in_block_safe(block, ins) {
+                if (ins->type != TAG_ALU_4) continue;
+                if (!midgard_is_branch_unit(ins->unit)) continue;
+                if (!ins->branch.conditional) continue;
+                if (ins->src[0] & IS_REG) continue;
+
+                if (mir_strip_inverted(ctx, ins->src[0])) {
+                        ins->branch.invert_conditional = !ins->branch.invert_conditional;
+
+                        progress |= true;
+               }
+        }
+
+        return progress;
+}