broadcom: Add VC5 NIR compiler.
[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 vir_remove_instruction(c, inst);
53 }
54
55 static bool
56 has_nonremovable_reads(struct v3d_compile *c, struct qinst *inst)
57 {
58 for (int i = 0; i < vir_get_nsrc(inst); i++) {
59 if (inst->src[i].file == QFILE_VPM) {
60 /* Instance ID, Vertex ID: Should have been removed at
61 * the NIR level
62 */
63 if (inst->src[i].index == ~0)
64 return true;
65
66 uint32_t attr = inst->src[i].index / 4;
67 uint32_t offset = inst->src[i].index % 4;
68
69 if (c->vattr_sizes[attr] != offset)
70 return true;
71
72 /* Can't get rid of the last VPM read, or the
73 * simulator (at least) throws an error.
74 */
75 uint32_t total_size = 0;
76 for (uint32_t i = 0; i < ARRAY_SIZE(c->vattr_sizes); i++)
77 total_size += c->vattr_sizes[i];
78 if (total_size == 1)
79 return true;
80 }
81
82 /* Dead code removal of varyings is tricky, so just assert
83 * that it all happened at the NIR level.
84 */
85 if (inst->src[i].file == QFILE_VARY)
86 return true;
87 }
88
89 return false;
90 }
91
92 bool
93 vir_opt_dead_code(struct v3d_compile *c)
94 {
95 bool progress = false;
96 bool *used = calloc(c->num_temps, sizeof(bool));
97
98 vir_for_each_inst_inorder(inst, c) {
99 for (int i = 0; i < vir_get_nsrc(inst); i++) {
100 if (inst->src[i].file == QFILE_TEMP)
101 used[inst->src[i].index] = true;
102 }
103 }
104
105 vir_for_each_block(block, c) {
106 vir_for_each_inst_safe(inst, block) {
107 if (inst->dst.file != QFILE_NULL &&
108 !(inst->dst.file == QFILE_TEMP &&
109 !used[inst->dst.index])) {
110 continue;
111 }
112
113 if (vir_has_side_effects(c, inst))
114 continue;
115
116 if (inst->qpu.flags.apf != V3D_QPU_PF_NONE ||
117 inst->qpu.flags.mpf != V3D_QPU_PF_NONE||
118 has_nonremovable_reads(c, inst)) {
119 /* If we can't remove the instruction, but we
120 * don't need its destination value, just
121 * remove the destination. The register
122 * allocator would trivially color it and it
123 * wouldn't cause any register pressure, but
124 * it's nicer to read the VIR code without
125 * unused destination regs.
126 */
127 if (inst->dst.file == QFILE_TEMP) {
128 if (debug) {
129 fprintf(stderr,
130 "Removing dst from: ");
131 vir_dump_inst(c, inst);
132 fprintf(stderr, "\n");
133 }
134 c->defs[inst->dst.index] = NULL;
135 inst->dst.file = QFILE_NULL;
136 progress = true;
137 }
138 continue;
139 }
140
141 for (int i = 0; i < vir_get_nsrc(inst); i++) {
142 if (inst->src[i].file != QFILE_VPM)
143 continue;
144 uint32_t attr = inst->src[i].index / 4;
145 uint32_t offset = (inst->src[i].index % 4);
146
147 if (c->vattr_sizes[attr] == offset) {
148 c->num_inputs--;
149 c->vattr_sizes[attr]--;
150 }
151 }
152
153 dce(c, inst);
154 progress = true;
155 continue;
156 }
157 }
158
159 free(used);
160
161 return progress;
162 }