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
36 static void
mark_array_use(struct ir3_instruction * instr,struct ir3_register * reg)37 mark_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
46 static void
instr_dce(struct ir3_instruction * instr,bool falsedep)47 instr_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 foreach_dst (dst, instr) {
57 if (is_dest_gpr(dst))
58 mark_array_use(instr, dst);
59 }
60
61 foreach_src (reg, instr)
62 mark_array_use(instr, reg); /* src */
63
64 foreach_ssa_src_n (src, i, instr) {
65 instr_dce(src, __is_false_dep(instr, i));
66 }
67 }
68
69 static bool
remove_unused_by_block(struct ir3_block * block)70 remove_unused_by_block(struct ir3_block *block)
71 {
72 bool progress = false;
73 foreach_instr_safe (instr, &block->instr_list) {
74 if (instr->opc == OPC_END || instr->opc == OPC_CHSH ||
75 instr->opc == OPC_CHMASK)
76 continue;
77 if (instr->flags & IR3_INSTR_UNUSED) {
78 if (instr->opc == OPC_META_SPLIT) {
79 struct ir3_instruction *src = ssa(instr->srcs[0]);
80 /* tex (cat5) instructions have a writemask, so we can
81 * mask off unused components. Other instructions do not.
82 */
83 if (src && is_tex_or_prefetch(src) && (src->dsts[0]->wrmask > 1)) {
84 src->dsts[0]->wrmask &= ~(1 << instr->split.off);
85 }
86 }
87
88 /* prune false-deps, etc: */
89 foreach_ssa_use (use, instr)
90 foreach_ssa_srcp_n (srcp, n, use)
91 if (*srcp == instr)
92 *srcp = NULL;
93
94 list_delinit(&instr->node);
95 progress = true;
96 }
97 }
98 return progress;
99 }
100
101 static bool
find_and_remove_unused(struct ir3 * ir,struct ir3_shader_variant * so)102 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
103 {
104 unsigned i;
105 bool progress = false;
106
107 ir3_clear_mark(ir);
108
109 /* initially mark everything as unused, we'll clear the flag as we
110 * visit the instructions:
111 */
112 foreach_block (block, &ir->block_list) {
113 foreach_instr (instr, &block->instr_list) {
114 if (instr->opc == OPC_META_INPUT) {
115 /* special case, if pre-fs texture fetch used, we cannot
116 * eliminate the barycentric i/j input
117 */
118 if (so->num_sampler_prefetch &&
119 instr->input.sysval == SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL)
120 continue;
121
122 /* Without GS header geometry shader is never invoked. */
123 if (instr->input.sysval == SYSTEM_VALUE_GS_HEADER_IR3)
124 continue;
125 }
126
127 instr->flags |= IR3_INSTR_UNUSED;
128 }
129 }
130
131 foreach_array (arr, &ir->array_list)
132 arr->unused = true;
133
134 foreach_block (block, &ir->block_list) {
135 for (i = 0; i < block->keeps_count; i++)
136 instr_dce(block->keeps[i], false);
137
138 /* We also need to account for if-condition: */
139 if (block->condition)
140 instr_dce(block->condition, false);
141 }
142
143 /* remove un-used instructions: */
144 foreach_block (block, &ir->block_list) {
145 progress |= remove_unused_by_block(block);
146 }
147
148 /* remove un-used arrays: */
149 foreach_array_safe (arr, &ir->array_list) {
150 if (arr->unused)
151 list_delinit(&arr->node);
152 }
153
154 /* fixup wrmask of split instructions to account for adjusted tex
155 * wrmask's:
156 */
157 foreach_block (block, &ir->block_list) {
158 foreach_instr (instr, &block->instr_list) {
159 if (instr->opc != OPC_META_SPLIT)
160 continue;
161
162 struct ir3_instruction *src = ssa(instr->srcs[0]);
163 if (!is_tex_or_prefetch(src))
164 continue;
165
166 instr->srcs[0]->wrmask = src->dsts[0]->wrmask;
167 }
168 }
169
170 for (i = 0; i < ir->a0_users_count; i++) {
171 struct ir3_instruction *instr = ir->a0_users[i];
172 if (instr && (instr->flags & IR3_INSTR_UNUSED))
173 ir->a0_users[i] = NULL;
174 }
175
176 for (i = 0; i < ir->a1_users_count; i++) {
177 struct ir3_instruction *instr = ir->a1_users[i];
178 if (instr && (instr->flags & IR3_INSTR_UNUSED))
179 ir->a1_users[i] = NULL;
180 }
181
182 for (i = 0; i < ir->predicates_count; i++) {
183 struct ir3_instruction *instr = ir->predicates[i];
184 if (instr && (instr->flags & IR3_INSTR_UNUSED))
185 ir->predicates[i] = NULL;
186 }
187
188 /* cleanup unused inputs: */
189 foreach_input_n (in, n, ir)
190 if (in->flags & IR3_INSTR_UNUSED)
191 ir->inputs[n] = NULL;
192
193 return progress;
194 }
195
196 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)197 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
198 {
199 void *mem_ctx = ralloc_context(NULL);
200 bool progress, made_progress = false;
201
202 ir3_find_ssa_uses(ir, mem_ctx, true);
203
204 do {
205 progress = find_and_remove_unused(ir, so);
206 made_progress |= progress;
207 } while (progress);
208
209 ralloc_free(mem_ctx);
210
211 return made_progress;
212 }
213