2
* Copyright © 2014 Broadcom
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:
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
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
25
* @file v3d_opt_dead_code.c
27
* This is a simple dead code eliminator for SSA values in VIR.
29
* It walks all the instructions finding what temps are used, then walks again
30
* to remove instructions writing unused temps.
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.
38
#include "v3d_compiler.h"
43
dce(struct v3d_compile *c, struct qinst *inst)
46
fprintf(stderr, "Removing: ");
47
vir_dump_inst(c, inst);
48
fprintf(stderr, "\n");
50
assert(!v3d_qpu_writes_flags(&inst->qpu));
51
vir_remove_instruction(c, inst);
55
has_nonremovable_reads(struct v3d_compile *c, struct qinst *inst)
57
for (int i = 0; i < vir_get_nsrc(inst); i++) {
58
if (inst->src[i].file == QFILE_VPM)
66
can_write_to_null(struct v3d_compile *c, struct qinst *inst)
68
/* The SFU instructions must write to a physical register. */
69
if (c->devinfo->ver >= 41 && v3d_qpu_uses_sfu(&inst->qpu))
76
vir_dce_flags(struct v3d_compile *c, struct qinst *inst)
80
"Removing flags write from: ");
81
vir_dump_inst(c, inst);
82
fprintf(stderr, "\n");
85
assert(inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU);
87
inst->qpu.flags.apf = V3D_QPU_PF_NONE;
88
inst->qpu.flags.mpf = V3D_QPU_PF_NONE;
89
inst->qpu.flags.auf = V3D_QPU_UF_NONE;
90
inst->qpu.flags.muf = V3D_QPU_UF_NONE;
94
check_last_ldunifa(struct v3d_compile *c,
98
if (!inst->qpu.sig.ldunifa && !inst->qpu.sig.ldunifarf)
101
list_for_each_entry_from(struct qinst, scan_inst, inst->link.next,
102
&block->instructions, link) {
103
/* If we find a new write to unifa, then this was the last
104
* ldunifa in its sequence and is safe to remove.
106
if (scan_inst->dst.file == QFILE_MAGIC &&
107
scan_inst->dst.index == V3D_QPU_WADDR_UNIFA) {
111
/* If we find another ldunifa in the same sequence then we
114
if (scan_inst->qpu.sig.ldunifa || scan_inst->qpu.sig.ldunifarf)
122
check_first_ldunifa(struct v3d_compile *c,
124
struct qblock *block,
125
struct qinst **unifa)
127
if (!inst->qpu.sig.ldunifa && !inst->qpu.sig.ldunifarf)
130
list_for_each_entry_from_rev(struct qinst, scan_inst, inst->link.prev,
131
&block->instructions, link) {
132
/* If we find a write to unifa, then this was the first
133
* ldunifa in its sequence and is safe to remove.
135
if (scan_inst->dst.file == QFILE_MAGIC &&
136
scan_inst->dst.index == V3D_QPU_WADDR_UNIFA) {
141
/* If we find another ldunifa in the same sequence then we
144
if (scan_inst->qpu.sig.ldunifa || scan_inst->qpu.sig.ldunifarf)
148
unreachable("could not find starting unifa for ldunifa sequence");
152
increment_unifa_address(struct v3d_compile *c, struct qinst *unifa)
154
if (unifa->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
155
unifa->qpu.alu.mul.op == V3D_QPU_M_MOV) {
156
c->cursor = vir_after_inst(unifa);
157
struct qreg unifa_reg = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_UNIFA);
158
vir_ADD_dest(c, unifa_reg, unifa->src[0], vir_uniform_ui(c, 4u));
159
vir_remove_instruction(c, unifa);
163
if (unifa->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
164
unifa->qpu.alu.add.op == V3D_QPU_A_ADD) {
165
c->cursor = vir_after_inst(unifa);
166
struct qreg unifa_reg = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_UNIFA);
168
vir_ADD(c, unifa->src[1], vir_uniform_ui(c, 4u));
169
vir_ADD_dest(c, unifa_reg, unifa->src[0], tmp);
170
vir_remove_instruction(c, unifa);
178
vir_opt_dead_code(struct v3d_compile *c)
180
bool progress = false;
181
bool *used = calloc(c->num_temps, sizeof(bool));
183
/* Defuse the "are you removing the cursor?" assertion in the core.
184
* You'll need to set up a new cursor for any new instructions after
185
* doing DCE (which we would expect, anyway).
187
c->cursor.link = NULL;
189
vir_for_each_inst_inorder(inst, c) {
190
for (int i = 0; i < vir_get_nsrc(inst); i++) {
191
if (inst->src[i].file == QFILE_TEMP)
192
used[inst->src[i].index] = true;
196
vir_for_each_block(block, c) {
197
struct qinst *last_flags_write = NULL;
198
c->cur_block = block;
199
vir_for_each_inst_safe(inst, block) {
200
/* If this instruction reads the flags, we can't
201
* remove the flags generation for it.
203
if (v3d_qpu_reads_flags(&inst->qpu))
204
last_flags_write = NULL;
206
if (inst->dst.file != QFILE_NULL &&
207
!(inst->dst.file == QFILE_TEMP &&
208
!used[inst->dst.index])) {
212
const bool is_ldunifa = inst->qpu.sig.ldunifa ||
213
inst->qpu.sig.ldunifarf;
215
if (vir_has_side_effects(c, inst) && !is_ldunifa)
218
bool is_first_ldunifa = false;
219
bool is_last_ldunifa = false;
220
struct qinst *unifa = NULL;
223
check_last_ldunifa(c, inst, block);
226
check_first_ldunifa(c, inst, block, &unifa);
229
if (v3d_qpu_writes_flags(&inst->qpu)) {
230
/* If we obscure a previous flags write,
233
if (last_flags_write &&
234
(inst->qpu.flags.apf != V3D_QPU_PF_NONE ||
235
inst->qpu.flags.mpf != V3D_QPU_PF_NONE)) {
236
vir_dce_flags(c, last_flags_write);
240
last_flags_write = inst;
243
if (v3d_qpu_writes_flags(&inst->qpu) ||
244
has_nonremovable_reads(c, inst) ||
245
(is_ldunifa && !is_first_ldunifa && !is_last_ldunifa)) {
246
/* If we can't remove the instruction, but we
247
* don't need its destination value, just
248
* remove the destination. The register
249
* allocator would trivially color it and it
250
* wouldn't cause any register pressure, but
251
* it's nicer to read the VIR code without
252
* unused destination regs.
254
if (inst->dst.file == QFILE_TEMP &&
255
can_write_to_null(c, inst)) {
258
"Removing dst from: ");
259
vir_dump_inst(c, inst);
260
fprintf(stderr, "\n");
262
c->defs[inst->dst.index] = NULL;
263
inst->dst.file = QFILE_NULL;
269
/* If we are removing the first ldunifa in a sequence
270
* we need to update the unifa address.
272
if (is_first_ldunifa) {
274
if (!increment_unifa_address(c, unifa))
278
assert(inst != last_flags_write);