nir/opt_peephole_select: Don't peephole_select expensive math instructions
[mesa.git] / src / broadcom / compiler / vir_opt_dead_code.c
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
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(inst->qpu.flags.apf == V3D_QPU_PF_NONE);
51 assert(inst->qpu.flags.mpf == V3D_QPU_PF_NONE);
52 assert(inst->qpu.flags.auf == V3D_QPU_UF_NONE);
53 assert(inst->qpu.flags.muf == V3D_QPU_UF_NONE);
54 vir_remove_instruction(c, inst);
55 }
56
57 static bool
58 has_nonremovable_reads(struct v3d_compile *c, struct qinst *inst)
59 {
60 for (int i = 0; i < vir_get_nsrc(inst); i++) {
61 if (inst->src[i].file == QFILE_VPM) {
62 /* Instance ID, Vertex ID: Should have been removed at
63 * the NIR level
64 */
65 if (inst->src[i].index == ~0)
66 return true;
67
68 uint32_t attr = inst->src[i].index / 4;
69 uint32_t offset = inst->src[i].index % 4;
70
71 if (c->vattr_sizes[attr] != offset)
72 return true;
73
74 /* Can't get rid of the last VPM read, or the
75 * simulator (at least) throws an error.
76 */
77 uint32_t total_size = 0;
78 for (uint32_t i = 0; i < ARRAY_SIZE(c->vattr_sizes); i++)
79 total_size += c->vattr_sizes[i];
80 if (total_size == 1)
81 return true;
82 }
83 }
84
85 return false;
86 }
87
88 static bool
89 can_write_to_null(struct v3d_compile *c, struct qinst *inst)
90 {
91 /* The SFU instructions must write to a physical register. */
92 if (c->devinfo->ver >= 41 && v3d_qpu_uses_sfu(&inst->qpu))
93 return false;
94
95 return true;
96 }
97
98 bool
99 vir_opt_dead_code(struct v3d_compile *c)
100 {
101 bool progress = false;
102 bool *used = calloc(c->num_temps, sizeof(bool));
103
104 /* Defuse the "are you removing the cursor?" assertion in the core.
105 * You'll need to set up a new cursor for any new instructions after
106 * doing DCE (which we would expect, anyway).
107 */
108 c->cursor.link = NULL;
109
110 vir_for_each_inst_inorder(inst, c) {
111 for (int i = 0; i < vir_get_nsrc(inst); i++) {
112 if (inst->src[i].file == QFILE_TEMP)
113 used[inst->src[i].index] = true;
114 }
115 }
116
117 vir_for_each_block(block, c) {
118 vir_for_each_inst_safe(inst, block) {
119 if (inst->dst.file != QFILE_NULL &&
120 !(inst->dst.file == QFILE_TEMP &&
121 !used[inst->dst.index])) {
122 continue;
123 }
124
125 if (vir_has_side_effects(c, inst))
126 continue;
127
128 if (inst->qpu.flags.apf != V3D_QPU_PF_NONE ||
129 inst->qpu.flags.mpf != V3D_QPU_PF_NONE ||
130 inst->qpu.flags.auf != V3D_QPU_UF_NONE ||
131 inst->qpu.flags.muf != V3D_QPU_UF_NONE ||
132 has_nonremovable_reads(c, inst)) {
133 /* If we can't remove the instruction, but we
134 * don't need its destination value, just
135 * remove the destination. The register
136 * allocator would trivially color it and it
137 * wouldn't cause any register pressure, but
138 * it's nicer to read the VIR code without
139 * unused destination regs.
140 */
141 if (inst->dst.file == QFILE_TEMP &&
142 can_write_to_null(c, inst)) {
143 if (debug) {
144 fprintf(stderr,
145 "Removing dst from: ");
146 vir_dump_inst(c, inst);
147 fprintf(stderr, "\n");
148 }
149 c->defs[inst->dst.index] = NULL;
150 inst->dst.file = QFILE_NULL;
151 progress = true;
152 }
153 continue;
154 }
155
156 for (int i = 0; i < vir_get_nsrc(inst); i++) {
157 if (inst->src[i].file != QFILE_VPM)
158 continue;
159 uint32_t attr = inst->src[i].index / 4;
160 uint32_t offset = (inst->src[i].index % 4);
161
162 if (c->vattr_sizes[attr] == offset) {
163 c->num_inputs--;
164 c->vattr_sizes[attr]--;
165 }
166 }
167
168 dce(c, inst);
169 progress = true;
170 continue;
171 }
172 }
173
174 free(used);
175
176 return progress;
177 }