diff --git a/src/panfrost/Makefile.sources b/src/panfrost/Makefile.sources index bfe4b803144..4ea3ccffeea 100644 --- a/src/panfrost/Makefile.sources +++ b/src/panfrost/Makefile.sources @@ -9,6 +9,7 @@ bifrost_FILES := \ bifrost/bi_print.c \ bifrost/bi_print.h \ bifrost/bi_ra.c \ + bifrost/bi_opt_copy_prop.c \ bifrost/bi_opt_dce.c \ bifrost/bi_quirks.h \ bifrost/bi_test_pack.c \ diff --git a/src/panfrost/bifrost/bi_opt_copy_prop.c b/src/panfrost/bifrost/bi_opt_copy_prop.c new file mode 100644 index 00000000000..48e2ca2bd9e --- /dev/null +++ b/src/panfrost/bifrost/bi_opt_copy_prop.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 Alyssa Rosenzweig + * Copyright (C) 2019-2020 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "compiler.h" + +/* A simple scalar-only SSA-based copy-propagation pass. O(N^2) due to the lack + * of use tracking. TODO: better data structures for O(N), TODO: vectors */ + +static bool +bi_rewrite_scalar_uses(bi_context *ctx, bi_index old, bi_index new) +{ + bool progress = false; + + bi_foreach_instr_global(ctx, use) { + bi_foreach_src(use, s) { + bi_index src = use->src[s]; + bool scalar = (bi_count_read_registers(use, s) == 1); + + if (bi_is_word_equiv(src, old) && scalar) { + use->src[s] = bi_replace_index(src, new); + progress = true; + } + } + } + + return progress; +} + +bool +bi_opt_copy_prop(bi_context *ctx) +{ + bool progress = false; + + bi_foreach_instr_global_safe(ctx, ins) { + if (ins->op != BI_OPCODE_MOV_I32) continue; + if (!bi_is_ssa(ins->dest[0])) continue; + if (!(bi_is_ssa(ins->src[0]) || ins->src[0].type == BI_INDEX_FAU)) continue; + + progress |= bi_rewrite_scalar_uses(ctx, ins->dest[0], ins->src[0]); + } + + return progress; +} diff --git a/src/panfrost/bifrost/bifrost_compile.c b/src/panfrost/bifrost/bifrost_compile.c index 8a27549868e..9ad489babea 100644 --- a/src/panfrost/bifrost/bifrost_compile.c +++ b/src/panfrost/bifrost/bifrost_compile.c @@ -2517,6 +2517,8 @@ bifrost_compile_shader_nir(void *mem_ctx, nir_shader *nir, do { progress = false; + progress |= bi_opt_copy_prop(ctx); + bi_foreach_block(ctx, _block) { bi_block *block = (bi_block *) _block; progress |= bi_opt_dead_code_eliminate(ctx, block, false); diff --git a/src/panfrost/bifrost/compiler.h b/src/panfrost/bifrost/compiler.h index ec04f529d0d..e7086611cd5 100644 --- a/src/panfrost/bifrost/compiler.h +++ b/src/panfrost/bifrost/compiler.h @@ -746,6 +746,7 @@ void bi_print_shader(bi_context *ctx, FILE *fp); /* BIR passes */ +bool bi_opt_copy_prop(bi_context *ctx); bool bi_opt_dead_code_eliminate(bi_context *ctx, bi_block *block, bool soft); void bi_schedule(bi_context *ctx); void bi_register_allocate(bi_context *ctx); diff --git a/src/panfrost/bifrost/meson.build b/src/panfrost/bifrost/meson.build index e30f2953f44..dd893854e54 100644 --- a/src/panfrost/bifrost/meson.build +++ b/src/panfrost/bifrost/meson.build @@ -23,6 +23,7 @@ libpanfrost_bifrost_files = files( 'bi_layout.c', 'bi_liveness.c', 'bi_print.c', + 'bi_opt_copy_prop.c', 'bi_opt_dce.c', 'bi_pack.c', 'bi_ra.c',