From c20063aa4af5d31ff706bcd4026a5c9fa575dd8b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sat, 28 Sep 2019 12:39:15 -0400 Subject: [PATCH] pan/midgard: Add csel invert optimization Signed-off-by: Alyssa Rosenzweig --- src/panfrost/midgard/compiler.h | 1 + src/panfrost/midgard/midgard_compile.c | 1 + src/panfrost/midgard/midgard_opt_invert.c | 25 +++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/panfrost/midgard/compiler.h b/src/panfrost/midgard/compiler.h index 8bbf67aa4e7..b0b5ba07143 100644 --- a/src/panfrost/midgard/compiler.h +++ b/src/panfrost/midgard/compiler.h @@ -654,6 +654,7 @@ void midgard_lower_invert(compiler_context *ctx, midgard_block *block); bool midgard_opt_not_propagate(compiler_context *ctx, midgard_block *block); bool midgard_opt_fuse_src_invert(compiler_context *ctx, midgard_block *block); 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); #endif diff --git a/src/panfrost/midgard/midgard_compile.c b/src/panfrost/midgard/midgard_compile.c index f1f8b7ecb9a..857e6c70112 100644 --- a/src/panfrost/midgard/midgard_compile.c +++ b/src/panfrost/midgard/midgard_compile.c @@ -2512,6 +2512,7 @@ midgard_compile_shader_nir(struct midgard_screen *screen, nir_shader *nir, midga progress |= midgard_opt_not_propagate(ctx, block); progress |= midgard_opt_fuse_src_invert(ctx, block); progress |= midgard_opt_fuse_dest_invert(ctx, block); + progress |= midgard_opt_csel_invert(ctx, block); } } while (progress); diff --git a/src/panfrost/midgard/midgard_opt_invert.c b/src/panfrost/midgard/midgard_opt_invert.c index f846bca5bf9..3a4c455877c 100644 --- a/src/panfrost/midgard/midgard_opt_invert.c +++ b/src/panfrost/midgard/midgard_opt_invert.c @@ -275,3 +275,28 @@ midgard_opt_fuse_src_invert(compiler_context *ctx, midgard_block *block) return progress; } + +/* Optimizes a .not away when used as the source of a conditional select: + * + * csel(a, b, c) = { b if a, c if !a } + * csel(!a, b, c) = { b if !a, c if !(!a) } = { c if a, b if !a } = csel(a, c, b) + * csel(!a, b, c) = csel(a, c, b) + */ + +bool +midgard_opt_csel_invert(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 (!OP_IS_CSEL(ins->alu.op)) continue; + if (!mir_single_use(ctx, ins->src[2])) continue; + if (!mir_strip_inverted(ctx, ins->src[2])) continue; + + mir_flip(ins); + progress |= true; + } + + return progress; +} -- 2.30.2