1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file v3d_opt_dead_code.c
26 *
27 * This is a simple dead code eliminator for SSA values in VIR.
28 *
29 * It walks all the instructions finding what temps are used, then walks again
30 * to remove instructions writing unused temps.
31 *
32 * This is an inefficient implementation if you have long chains of
33 * instructions where the entire chain is dead, but we expect those to have
34 * been eliminated at the NIR level, and here we're just cleaning up small
35 * problems produced by NIR->VIR.
36 */
37
38 #include "v3d_compiler.h"
39
40 static bool debug;
41
42 static void
dce(struct v3d_compile * c,struct qinst * inst)43 dce(struct v3d_compile *c, struct qinst *inst)
44 {
45 if (debug) {
46 fprintf(stderr, "Removing: ");
47 vir_dump_inst(c, inst);
48 fprintf(stderr, "\n");
49 }
50 assert(!v3d_qpu_writes_flags(&inst->qpu));
51 vir_remove_instruction(c, inst);
52 }
53
54 static bool
can_write_to_null(struct v3d_compile * c,struct qinst * inst)55 can_write_to_null(struct v3d_compile *c, struct qinst *inst)
56 {
57 /* The SFU instructions must write to a physical register. */
58 if (v3d_qpu_uses_sfu(&inst->qpu))
59 return false;
60
61 return true;
62 }
63
64 static void
vir_dce_flags(struct v3d_compile * c,struct qinst * inst)65 vir_dce_flags(struct v3d_compile *c, struct qinst *inst)
66 {
67 if (debug) {
68 fprintf(stderr,
69 "Removing flags write from: ");
70 vir_dump_inst(c, inst);
71 fprintf(stderr, "\n");
72 }
73
74 assert(inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU);
75
76 inst->qpu.flags.apf = V3D_QPU_PF_NONE;
77 inst->qpu.flags.mpf = V3D_QPU_PF_NONE;
78 inst->qpu.flags.auf = V3D_QPU_UF_NONE;
79 inst->qpu.flags.muf = V3D_QPU_UF_NONE;
80 }
81
82 static bool
check_last_ldunifa(struct v3d_compile * c,struct qinst * inst,struct qblock * block)83 check_last_ldunifa(struct v3d_compile *c,
84 struct qinst *inst,
85 struct qblock *block)
86 {
87 if (!inst->qpu.sig.ldunifa && !inst->qpu.sig.ldunifarf)
88 return false;
89
90 list_for_each_entry_from(struct qinst, scan_inst, inst->link.next,
91 &block->instructions, link) {
92 /* If we find a new write to unifa, then this was the last
93 * ldunifa in its sequence and is safe to remove.
94 */
95 if (scan_inst->dst.file == QFILE_MAGIC &&
96 scan_inst->dst.index == V3D_QPU_WADDR_UNIFA) {
97 return true;
98 }
99
100 /* If we find another ldunifa in the same sequence then we
101 * can't remove it.
102 */
103 if (scan_inst->qpu.sig.ldunifa || scan_inst->qpu.sig.ldunifarf)
104 return false;
105 }
106
107 return true;
108 }
109
110 static bool
check_first_ldunifa(struct v3d_compile * c,struct qinst * inst,struct qblock * block,struct qinst ** unifa)111 check_first_ldunifa(struct v3d_compile *c,
112 struct qinst *inst,
113 struct qblock *block,
114 struct qinst **unifa)
115 {
116 if (!inst->qpu.sig.ldunifa && !inst->qpu.sig.ldunifarf)
117 return false;
118
119 list_for_each_entry_from_rev(struct qinst, scan_inst, inst->link.prev,
120 &block->instructions, link) {
121 /* If we find a write to unifa, then this was the first
122 * ldunifa in its sequence and is safe to remove.
123 */
124 if (scan_inst->dst.file == QFILE_MAGIC &&
125 scan_inst->dst.index == V3D_QPU_WADDR_UNIFA) {
126 *unifa = scan_inst;
127 return true;
128 }
129
130 /* If we find another ldunifa in the same sequence then we
131 * can't remove it.
132 */
133 if (scan_inst->qpu.sig.ldunifa || scan_inst->qpu.sig.ldunifarf)
134 return false;
135 }
136
137 unreachable("could not find starting unifa for ldunifa sequence");
138 }
139
140 static bool
increment_unifa_address(struct v3d_compile * c,struct qinst * unifa)141 increment_unifa_address(struct v3d_compile *c, struct qinst *unifa)
142 {
143 if (unifa->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
144 unifa->qpu.alu.mul.op == V3D_QPU_M_MOV) {
145 c->cursor = vir_after_inst(unifa);
146 struct qreg unifa_reg = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_UNIFA);
147 vir_ADD_dest(c, unifa_reg, unifa->src[0], vir_uniform_ui(c, 4u));
148 vir_remove_instruction(c, unifa);
149 return true;
150 }
151
152 if (unifa->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
153 unifa->qpu.alu.add.op == V3D_QPU_A_ADD) {
154 c->cursor = vir_after_inst(unifa);
155 struct qreg unifa_reg = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_UNIFA);
156 struct qreg tmp =
157 vir_ADD(c, unifa->src[1], vir_uniform_ui(c, 4u));
158 vir_ADD_dest(c, unifa_reg, unifa->src[0], tmp);
159 vir_remove_instruction(c, unifa);
160 return true;
161 }
162
163 return false;
164 }
165
166 bool
vir_opt_dead_code(struct v3d_compile * c)167 vir_opt_dead_code(struct v3d_compile *c)
168 {
169 bool progress = false;
170 bool *used = calloc(c->num_temps, sizeof(bool));
171
172 /* Defuse the "are you removing the cursor?" assertion in the core.
173 * You'll need to set up a new cursor for any new instructions after
174 * doing DCE (which we would expect, anyway).
175 */
176 c->cursor.link = NULL;
177
178 vir_for_each_inst_inorder(inst, c) {
179 for (int i = 0; i < vir_get_nsrc(inst); i++) {
180 if (inst->src[i].file == QFILE_TEMP)
181 used[inst->src[i].index] = true;
182 }
183 }
184
185 vir_for_each_block(block, c) {
186 struct qinst *last_flags_write = NULL;
187 c->cur_block = block;
188 vir_for_each_inst_safe(inst, block) {
189 /* If this instruction reads the flags, we can't
190 * remove the flags generation for it.
191 */
192 if (v3d_qpu_reads_flags(&inst->qpu))
193 last_flags_write = NULL;
194
195 if (inst->dst.file != QFILE_NULL &&
196 !(inst->dst.file == QFILE_TEMP &&
197 !used[inst->dst.index])) {
198 continue;
199 }
200
201 const bool is_ldunifa = inst->qpu.sig.ldunifa ||
202 inst->qpu.sig.ldunifarf;
203
204 if (vir_has_side_effects(c, inst) && !is_ldunifa)
205 continue;
206
207 bool is_first_ldunifa = false;
208 bool is_last_ldunifa = false;
209 struct qinst *unifa = NULL;
210 if (is_ldunifa) {
211 is_last_ldunifa =
212 check_last_ldunifa(c, inst, block);
213
214 is_first_ldunifa =
215 check_first_ldunifa(c, inst, block, &unifa);
216 }
217
218 if (v3d_qpu_writes_flags(&inst->qpu)) {
219 /* If we obscure a previous flags write,
220 * drop it.
221 */
222 if (last_flags_write &&
223 (inst->qpu.flags.apf != V3D_QPU_PF_NONE ||
224 inst->qpu.flags.mpf != V3D_QPU_PF_NONE)) {
225 vir_dce_flags(c, last_flags_write);
226 progress = true;
227 }
228
229 last_flags_write = inst;
230 }
231
232 if (v3d_qpu_writes_flags(&inst->qpu) ||
233 (is_ldunifa && !is_first_ldunifa && !is_last_ldunifa)) {
234 /* If we can't remove the instruction, but we
235 * don't need its destination value, just
236 * remove the destination. The register
237 * allocator would trivially color it and it
238 * wouldn't cause any register pressure, but
239 * it's nicer to read the VIR code without
240 * unused destination regs.
241 */
242 if (inst->dst.file == QFILE_TEMP &&
243 can_write_to_null(c, inst)) {
244 if (debug) {
245 fprintf(stderr,
246 "Removing dst from: ");
247 vir_dump_inst(c, inst);
248 fprintf(stderr, "\n");
249 }
250 c->defs[inst->dst.index] = NULL;
251 inst->dst.file = QFILE_NULL;
252 progress = true;
253 }
254 continue;
255 }
256
257 /* If we are removing the first ldunifa in a sequence
258 * we need to update the unifa address.
259 */
260 if (is_first_ldunifa) {
261 assert(unifa);
262 if (!increment_unifa_address(c, unifa))
263 continue;
264 }
265
266 assert(inst != last_flags_write);
267 dce(c, inst);
268 progress = true;
269 continue;
270 }
271 }
272
273 free(used);
274
275 return progress;
276 }
277