1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org> 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci * Authors: 24bf215546Sopenharmony_ci * Rob Clark <robclark@freedesktop.org> 25bf215546Sopenharmony_ci */ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "util/u_math.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "ir3.h" 30bf215546Sopenharmony_ci#include "ir3_shader.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci/* 33bf215546Sopenharmony_ci * Dead code elimination: 34bf215546Sopenharmony_ci */ 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_cistatic void 37bf215546Sopenharmony_cimark_array_use(struct ir3_instruction *instr, struct ir3_register *reg) 38bf215546Sopenharmony_ci{ 39bf215546Sopenharmony_ci if (reg->flags & IR3_REG_ARRAY) { 40bf215546Sopenharmony_ci struct ir3_array *arr = 41bf215546Sopenharmony_ci ir3_lookup_array(instr->block->shader, reg->array.id); 42bf215546Sopenharmony_ci arr->unused = false; 43bf215546Sopenharmony_ci } 44bf215546Sopenharmony_ci} 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_cistatic void 47bf215546Sopenharmony_ciinstr_dce(struct ir3_instruction *instr, bool falsedep) 48bf215546Sopenharmony_ci{ 49bf215546Sopenharmony_ci /* don't mark falsedep's as used, but otherwise process them normally: */ 50bf215546Sopenharmony_ci if (!falsedep) 51bf215546Sopenharmony_ci instr->flags &= ~IR3_INSTR_UNUSED; 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci if (ir3_instr_check_mark(instr)) 54bf215546Sopenharmony_ci return; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci foreach_dst (dst, instr) { 57bf215546Sopenharmony_ci if (is_dest_gpr(dst)) 58bf215546Sopenharmony_ci mark_array_use(instr, dst); 59bf215546Sopenharmony_ci } 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci foreach_src (reg, instr) 62bf215546Sopenharmony_ci mark_array_use(instr, reg); /* src */ 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci foreach_ssa_src_n (src, i, instr) { 65bf215546Sopenharmony_ci instr_dce(src, __is_false_dep(instr, i)); 66bf215546Sopenharmony_ci } 67bf215546Sopenharmony_ci} 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_cistatic bool 70bf215546Sopenharmony_ciremove_unused_by_block(struct ir3_block *block) 71bf215546Sopenharmony_ci{ 72bf215546Sopenharmony_ci bool progress = false; 73bf215546Sopenharmony_ci foreach_instr_safe (instr, &block->instr_list) { 74bf215546Sopenharmony_ci if (instr->opc == OPC_END || instr->opc == OPC_CHSH || 75bf215546Sopenharmony_ci instr->opc == OPC_CHMASK) 76bf215546Sopenharmony_ci continue; 77bf215546Sopenharmony_ci if (instr->flags & IR3_INSTR_UNUSED) { 78bf215546Sopenharmony_ci if (instr->opc == OPC_META_SPLIT) { 79bf215546Sopenharmony_ci struct ir3_instruction *src = ssa(instr->srcs[0]); 80bf215546Sopenharmony_ci /* tex (cat5) instructions have a writemask, so we can 81bf215546Sopenharmony_ci * mask off unused components. Other instructions do not. 82bf215546Sopenharmony_ci */ 83bf215546Sopenharmony_ci if (src && is_tex_or_prefetch(src) && (src->dsts[0]->wrmask > 1)) { 84bf215546Sopenharmony_ci src->dsts[0]->wrmask &= ~(1 << instr->split.off); 85bf215546Sopenharmony_ci } 86bf215546Sopenharmony_ci } 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci /* prune false-deps, etc: */ 89bf215546Sopenharmony_ci foreach_ssa_use (use, instr) 90bf215546Sopenharmony_ci foreach_ssa_srcp_n (srcp, n, use) 91bf215546Sopenharmony_ci if (*srcp == instr) 92bf215546Sopenharmony_ci *srcp = NULL; 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci list_delinit(&instr->node); 95bf215546Sopenharmony_ci progress = true; 96bf215546Sopenharmony_ci } 97bf215546Sopenharmony_ci } 98bf215546Sopenharmony_ci return progress; 99bf215546Sopenharmony_ci} 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_cistatic bool 102bf215546Sopenharmony_cifind_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so) 103bf215546Sopenharmony_ci{ 104bf215546Sopenharmony_ci unsigned i; 105bf215546Sopenharmony_ci bool progress = false; 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci ir3_clear_mark(ir); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci /* initially mark everything as unused, we'll clear the flag as we 110bf215546Sopenharmony_ci * visit the instructions: 111bf215546Sopenharmony_ci */ 112bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 113bf215546Sopenharmony_ci foreach_instr (instr, &block->instr_list) { 114bf215546Sopenharmony_ci if (instr->opc == OPC_META_INPUT) { 115bf215546Sopenharmony_ci /* special case, if pre-fs texture fetch used, we cannot 116bf215546Sopenharmony_ci * eliminate the barycentric i/j input 117bf215546Sopenharmony_ci */ 118bf215546Sopenharmony_ci if (so->num_sampler_prefetch && 119bf215546Sopenharmony_ci instr->input.sysval == SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL) 120bf215546Sopenharmony_ci continue; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci /* Without GS header geometry shader is never invoked. */ 123bf215546Sopenharmony_ci if (instr->input.sysval == SYSTEM_VALUE_GS_HEADER_IR3) 124bf215546Sopenharmony_ci continue; 125bf215546Sopenharmony_ci } 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci instr->flags |= IR3_INSTR_UNUSED; 128bf215546Sopenharmony_ci } 129bf215546Sopenharmony_ci } 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci foreach_array (arr, &ir->array_list) 132bf215546Sopenharmony_ci arr->unused = true; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 135bf215546Sopenharmony_ci for (i = 0; i < block->keeps_count; i++) 136bf215546Sopenharmony_ci instr_dce(block->keeps[i], false); 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci /* We also need to account for if-condition: */ 139bf215546Sopenharmony_ci if (block->condition) 140bf215546Sopenharmony_ci instr_dce(block->condition, false); 141bf215546Sopenharmony_ci } 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci /* remove un-used instructions: */ 144bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 145bf215546Sopenharmony_ci progress |= remove_unused_by_block(block); 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci /* remove un-used arrays: */ 149bf215546Sopenharmony_ci foreach_array_safe (arr, &ir->array_list) { 150bf215546Sopenharmony_ci if (arr->unused) 151bf215546Sopenharmony_ci list_delinit(&arr->node); 152bf215546Sopenharmony_ci } 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci /* fixup wrmask of split instructions to account for adjusted tex 155bf215546Sopenharmony_ci * wrmask's: 156bf215546Sopenharmony_ci */ 157bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 158bf215546Sopenharmony_ci foreach_instr (instr, &block->instr_list) { 159bf215546Sopenharmony_ci if (instr->opc != OPC_META_SPLIT) 160bf215546Sopenharmony_ci continue; 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci struct ir3_instruction *src = ssa(instr->srcs[0]); 163bf215546Sopenharmony_ci if (!is_tex_or_prefetch(src)) 164bf215546Sopenharmony_ci continue; 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci instr->srcs[0]->wrmask = src->dsts[0]->wrmask; 167bf215546Sopenharmony_ci } 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci for (i = 0; i < ir->a0_users_count; i++) { 171bf215546Sopenharmony_ci struct ir3_instruction *instr = ir->a0_users[i]; 172bf215546Sopenharmony_ci if (instr && (instr->flags & IR3_INSTR_UNUSED)) 173bf215546Sopenharmony_ci ir->a0_users[i] = NULL; 174bf215546Sopenharmony_ci } 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci for (i = 0; i < ir->a1_users_count; i++) { 177bf215546Sopenharmony_ci struct ir3_instruction *instr = ir->a1_users[i]; 178bf215546Sopenharmony_ci if (instr && (instr->flags & IR3_INSTR_UNUSED)) 179bf215546Sopenharmony_ci ir->a1_users[i] = NULL; 180bf215546Sopenharmony_ci } 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci for (i = 0; i < ir->predicates_count; i++) { 183bf215546Sopenharmony_ci struct ir3_instruction *instr = ir->predicates[i]; 184bf215546Sopenharmony_ci if (instr && (instr->flags & IR3_INSTR_UNUSED)) 185bf215546Sopenharmony_ci ir->predicates[i] = NULL; 186bf215546Sopenharmony_ci } 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci /* cleanup unused inputs: */ 189bf215546Sopenharmony_ci foreach_input_n (in, n, ir) 190bf215546Sopenharmony_ci if (in->flags & IR3_INSTR_UNUSED) 191bf215546Sopenharmony_ci ir->inputs[n] = NULL; 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci return progress; 194bf215546Sopenharmony_ci} 195bf215546Sopenharmony_ci 196bf215546Sopenharmony_cibool 197bf215546Sopenharmony_ciir3_dce(struct ir3 *ir, struct ir3_shader_variant *so) 198bf215546Sopenharmony_ci{ 199bf215546Sopenharmony_ci void *mem_ctx = ralloc_context(NULL); 200bf215546Sopenharmony_ci bool progress, made_progress = false; 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci ir3_find_ssa_uses(ir, mem_ctx, true); 203bf215546Sopenharmony_ci 204bf215546Sopenharmony_ci do { 205bf215546Sopenharmony_ci progress = find_and_remove_unused(ir, so); 206bf215546Sopenharmony_ci made_progress |= progress; 207bf215546Sopenharmony_ci } while (progress); 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci ralloc_free(mem_ctx); 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci return made_progress; 212bf215546Sopenharmony_ci} 213