1 /*
2 * Copyright © 2014 Rob Clark <robclark@freedesktop.org>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <robclark@freedesktop.org>
7 */
8
9 #include "util/u_math.h"
10
11 #include "ir3.h"
12 #include "ir3_shader.h"
13
14 /*
15 * Dead code elimination:
16 */
17
18 static void
mark_array_use(struct ir3_instruction * instr,struct ir3_register * reg)19 mark_array_use(struct ir3_instruction *instr, struct ir3_register *reg)
20 {
21 if (reg->flags & IR3_REG_ARRAY) {
22 struct ir3_array *arr =
23 ir3_lookup_array(instr->block->shader, reg->array.id);
24 arr->unused = false;
25 }
26 }
27
28 static void
instr_dce(struct ir3_instruction * instr,bool falsedep)29 instr_dce(struct ir3_instruction *instr, bool falsedep)
30 {
31 /* don't mark falsedep's as used, but otherwise process them normally: */
32 if (!falsedep)
33 instr->flags &= ~IR3_INSTR_UNUSED;
34
35 if (ir3_instr_check_mark(instr))
36 return;
37
38 foreach_dst (dst, instr) {
39 if (is_dest_gpr(dst))
40 mark_array_use(instr, dst);
41 }
42
43 foreach_src (reg, instr)
44 mark_array_use(instr, reg); /* src */
45
46 foreach_ssa_src_n (src, i, instr) {
47 if (!__is_false_dep(instr, i)) {
48 if (instr->opc == OPC_META_COLLECT &&
49 !(instr->dsts[0]->wrmask & (1 << i))) {
50 /* Ignore sources of collects for which the corresponding dst is not
51 * written since they are unused.
52 */
53 continue;
54 }
55
56 /* Propagate the wrmask of sources to their defs. */
57 struct ir3_register *src_reg = instr->srcs[i];
58 src_reg->def->wrmask |= src_reg->wrmask;
59
60 if (!src_reg->wrmask) {
61 /* If no components are read, the def is unused. */
62 continue;
63 }
64 }
65
66 instr_dce(src, __is_false_dep(instr, i));
67 }
68 }
69
70 static bool
remove_unused_by_block(struct ir3_block * block)71 remove_unused_by_block(struct ir3_block *block)
72 {
73 bool progress = false;
74 foreach_instr_safe (instr, &block->instr_list) {
75 if (instr->opc == OPC_END || instr->opc == OPC_CHSH ||
76 instr->opc == OPC_CHMASK || instr->opc == OPC_LOCK ||
77 instr->opc == OPC_UNLOCK)
78 continue;
79 if (instr->flags & IR3_INSTR_UNUSED) {
80 if (instr->opc == OPC_META_SPLIT) {
81 struct ir3_instruction *src = ssa(instr->srcs[0]);
82 /* tex (cat5) instructions have a writemask, so we can
83 * mask off unused components. Other instructions do not.
84 */
85 if (src && is_tex_or_prefetch(src) && (src->dsts[0]->wrmask > 1)) {
86 src->dsts[0]->wrmask &= ~(1 << instr->split.off);
87 }
88 }
89
90 /* prune false-deps, etc: */
91 foreach_ssa_use (use, instr)
92 foreach_ssa_srcp_n (srcp, n, use)
93 if (*srcp == instr)
94 *srcp = NULL;
95
96 ir3_instr_remove(instr);
97 progress = true;
98 } else if (instr->opc == OPC_META_COLLECT) {
99 struct ir3_register *dst = instr->dsts[0];
100
101 /* Trim unused trailing components. While it's tempting to just remove
102 * all unused components, this doesn't work for a few reasons. Note
103 * that currently, collects with unused components are only created
104 * when certain FS output components are aliased using alias.rt. The
105 * important part here is that the collect will be used for an output.
106 * Even if only certain components of an output are written to GPRs, we
107 * still need to allocate the correct consecutive registers. For
108 * example, if we only write out.xz, we have to make sure there is
109 * still a register in between the registers allocated for the x and z
110 * components. In other words, we have to be able to allocate a base
111 * register for the output such that all components written to GPRs
112 * have the correct offset from the base register. So we cannot remove
113 * any unused holes in the collect. We also cannot remove the leading
114 * unused components because then RA might decide put the first used
115 * component in, say, r0.x, leaving no space to allocate a base
116 * register. Therefore, we only trim trailing components.
117 *
118 * TODO: we could probably trim leading components by having a way to
119 * request a minimum register number from RA.
120 */
121 instr->srcs_count = util_last_bit(dst->wrmask);
122
123 /* Mark sources for which the corresponding dst is not written as
124 * undef.
125 */
126 foreach_src_n (src, src_n, instr) {
127 if (!(dst->wrmask & (1 << src_n))) {
128 src->def = NULL;
129 src->num = INVALID_REG;
130 src->flags &= ~(IR3_REG_CONST | IR3_REG_IMMED);
131 }
132 }
133 }
134 }
135 return progress;
136 }
137
138 static bool
find_and_remove_unused(struct ir3 * ir,struct ir3_shader_variant * so)139 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
140 {
141 unsigned i;
142 bool progress = false;
143
144 ir3_clear_mark(ir);
145
146 /* initially mark everything as unused, we'll clear the flag as we
147 * visit the instructions:
148 */
149 foreach_block (block, &ir->block_list) {
150 foreach_instr (instr, &block->instr_list) {
151 if (instr->opc == OPC_META_INPUT) {
152 /* Without GS header geometry shader is never invoked. */
153 if (instr->input.sysval == SYSTEM_VALUE_GS_HEADER_IR3)
154 continue;
155 if (instr->input.sysval == SYSTEM_VALUE_SAMPLE_MASK_IN &&
156 so->reads_shading_rate &&
157 ir->compiler->reading_shading_rate_requires_smask_quirk)
158 continue;
159 }
160
161 instr->flags |= IR3_INSTR_UNUSED;
162
163 /* To eliminate unused components in collect, we zero the wrmask and
164 * update it using the wrmask of its users.
165 */
166 if (instr->opc == OPC_META_COLLECT) {
167 instr->dsts[0]->wrmask = 0;
168 }
169 }
170 }
171
172 foreach_array (arr, &ir->array_list)
173 arr->unused = true;
174
175 foreach_block (block, &ir->block_list) {
176 for (i = 0; i < block->keeps_count; i++)
177 instr_dce(block->keeps[i], false);
178
179 /* We also need to account for if-condition: */
180 struct ir3_instruction *terminator = ir3_block_get_terminator(block);
181 if (terminator) {
182 instr_dce(terminator, false);
183 }
184 }
185
186 /* remove un-used instructions: */
187 foreach_block (block, &ir->block_list) {
188 progress |= remove_unused_by_block(block);
189 }
190
191 /* remove un-used arrays: */
192 foreach_array_safe (arr, &ir->array_list) {
193 if (arr->unused)
194 list_delinit(&arr->node);
195 }
196
197 /* fixup wrmask of split instructions to account for adjusted tex
198 * wrmask's:
199 */
200 foreach_block (block, &ir->block_list) {
201 foreach_instr (instr, &block->instr_list) {
202 if (instr->opc != OPC_META_SPLIT)
203 continue;
204
205 struct ir3_instruction *src = ssa(instr->srcs[0]);
206 if (!is_tex_or_prefetch(src))
207 continue;
208
209 instr->srcs[0]->wrmask = src->dsts[0]->wrmask;
210 }
211 }
212
213 for (i = 0; i < ir->a0_users_count; i++) {
214 struct ir3_instruction *instr = ir->a0_users[i];
215 if (instr && (instr->flags & IR3_INSTR_UNUSED))
216 ir->a0_users[i] = NULL;
217 }
218
219 for (i = 0; i < ir->a1_users_count; i++) {
220 struct ir3_instruction *instr = ir->a1_users[i];
221 if (instr && (instr->flags & IR3_INSTR_UNUSED))
222 ir->a1_users[i] = NULL;
223 }
224
225 /* cleanup unused inputs: */
226 foreach_input_n (in, n, ir)
227 if (in->flags & IR3_INSTR_UNUSED)
228 ir->inputs[n] = NULL;
229
230 return progress;
231 }
232
233 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)234 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
235 {
236 void *mem_ctx = ralloc_context(NULL);
237 bool progress, made_progress = false;
238
239 ir3_find_ssa_uses(ir, mem_ctx, true);
240
241 do {
242 progress = find_and_remove_unused(ir, so);
243 made_progress |= progress;
244 } while (progress);
245
246 ralloc_free(mem_ctx);
247
248 return made_progress;
249 }
250