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