Path: blob/21.2-virgl/src/freedreno/ir3/ir3_dce.c
4565 views
/*1* Copyright (C) 2014 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "util/u_math.h"2728#include "ir3.h"29#include "ir3_shader.h"3031/*32* Dead code elimination:33*/3435static void36mark_array_use(struct ir3_instruction *instr, struct ir3_register *reg)37{38if (reg->flags & IR3_REG_ARRAY) {39struct ir3_array *arr =40ir3_lookup_array(instr->block->shader, reg->array.id);41arr->unused = false;42}43}4445static void46instr_dce(struct ir3_instruction *instr, bool falsedep)47{48/* don't mark falsedep's as used, but otherwise process them normally: */49if (!falsedep)50instr->flags &= ~IR3_INSTR_UNUSED;5152if (ir3_instr_check_mark(instr))53return;5455if (writes_gpr(instr))56mark_array_use(instr, instr->dsts[0]); /* dst */5758foreach_src (reg, instr)59mark_array_use(instr, reg); /* src */6061foreach_ssa_src_n (src, i, instr) {62instr_dce(src, __is_false_dep(instr, i));63}64}6566static bool67remove_unused_by_block(struct ir3_block *block)68{69bool progress = false;70foreach_instr_safe (instr, &block->instr_list) {71if (instr->opc == OPC_END || instr->opc == OPC_CHSH ||72instr->opc == OPC_CHMASK)73continue;74if (instr->flags & IR3_INSTR_UNUSED) {75if (instr->opc == OPC_META_SPLIT) {76struct ir3_instruction *src = ssa(instr->srcs[0]);77/* tex (cat5) instructions have a writemask, so we can78* mask off unused components. Other instructions do not.79*/80if (src && is_tex_or_prefetch(src) && (src->dsts[0]->wrmask > 1)) {81src->dsts[0]->wrmask &= ~(1 << instr->split.off);82}83}8485/* prune false-deps, etc: */86foreach_ssa_use (use, instr)87foreach_ssa_srcp_n (srcp, n, use)88if (*srcp == instr)89*srcp = NULL;9091list_delinit(&instr->node);92progress = true;93}94}95return progress;96}9798static bool99find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)100{101unsigned i;102bool progress = false;103104ir3_clear_mark(ir);105106/* initially mark everything as unused, we'll clear the flag as we107* visit the instructions:108*/109foreach_block (block, &ir->block_list) {110foreach_instr (instr, &block->instr_list) {111/* special case, if pre-fs texture fetch used, we cannot112* eliminate the barycentric i/j input113*/114if (so->num_sampler_prefetch && (instr->opc == OPC_META_INPUT) &&115(instr->input.sysval == SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL))116continue;117instr->flags |= IR3_INSTR_UNUSED;118}119}120121foreach_array (arr, &ir->array_list)122arr->unused = true;123124foreach_block (block, &ir->block_list) {125for (i = 0; i < block->keeps_count; i++)126instr_dce(block->keeps[i], false);127128/* We also need to account for if-condition: */129if (block->condition)130instr_dce(block->condition, false);131}132133/* remove un-used instructions: */134foreach_block (block, &ir->block_list) {135progress |= remove_unused_by_block(block);136}137138/* remove un-used arrays: */139foreach_array_safe (arr, &ir->array_list) {140if (arr->unused)141list_delinit(&arr->node);142}143144/* fixup wrmask of split instructions to account for adjusted tex145* wrmask's:146*/147foreach_block (block, &ir->block_list) {148foreach_instr (instr, &block->instr_list) {149if (instr->opc != OPC_META_SPLIT)150continue;151152struct ir3_instruction *src = ssa(instr->srcs[0]);153if (!is_tex_or_prefetch(src))154continue;155156instr->srcs[0]->wrmask = src->dsts[0]->wrmask;157}158}159160for (i = 0; i < ir->a0_users_count; i++) {161struct ir3_instruction *instr = ir->a0_users[i];162if (instr && (instr->flags & IR3_INSTR_UNUSED))163ir->a0_users[i] = NULL;164}165166for (i = 0; i < ir->a1_users_count; i++) {167struct ir3_instruction *instr = ir->a1_users[i];168if (instr && (instr->flags & IR3_INSTR_UNUSED))169ir->a1_users[i] = NULL;170}171172for (i = 0; i < ir->predicates_count; i++) {173struct ir3_instruction *instr = ir->predicates[i];174if (instr && (instr->flags & IR3_INSTR_UNUSED))175ir->predicates[i] = NULL;176}177178/* cleanup unused inputs: */179foreach_input_n (in, n, ir)180if (in->flags & IR3_INSTR_UNUSED)181ir->inputs[n] = NULL;182183return progress;184}185186bool187ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)188{189void *mem_ctx = ralloc_context(NULL);190bool progress, made_progress = false;191192ir3_find_ssa_uses(ir, mem_ctx, true);193194do {195progress = find_and_remove_unused(ir, so);196made_progress |= progress;197} while (progress);198199ralloc_free(mem_ctx);200201return made_progress;202}203204205