freedreno/ir3: build binning variant at same time as draw variant
[mesa.git] / src / freedreno / ir3 / ir3_dce.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_math.h"
28
29 #include "ir3.h"
30 #include "ir3_shader.h"
31
32 /*
33 * Dead code elimination:
34 */
35
36 static void
37 instr_dce(struct ir3_instruction *instr, bool falsedep)
38 {
39 /* don't mark falsedep's as used, but otherwise process them normally: */
40 if (!falsedep)
41 instr->flags &= ~IR3_INSTR_UNUSED;
42
43 if (ir3_instr_check_mark(instr))
44 return;
45
46 foreach_ssa_src_n (src, i, instr) {
47 instr_dce(src, __is_false_dep(instr, i));
48 }
49 }
50
51 static bool
52 remove_unused_by_block(struct ir3_block *block)
53 {
54 bool progress = false;
55 foreach_instr_safe (instr, &block->instr_list) {
56 if (instr->opc == OPC_END || instr->opc == OPC_CHSH || instr->opc == OPC_CHMASK)
57 continue;
58 if (instr->flags & IR3_INSTR_UNUSED) {
59 if (instr->opc == OPC_META_SPLIT) {
60 struct ir3_instruction *src = ssa(instr->regs[1]);
61 /* tex (cat5) instructions have a writemask, so we can
62 * mask off unused components. Other instructions do not.
63 */
64 if (src && is_tex_or_prefetch(src) && (src->regs[0]->wrmask > 1)) {
65 src->regs[0]->wrmask &= ~(1 << instr->split.off);
66
67 /* prune no-longer needed right-neighbors. We could
68 * probably do the same for left-neighbors (ie. tex
69 * fetch that only need .yw components), but that
70 * makes RA a bit more confusing than it already is
71 */
72 struct ir3_instruction *n = instr;
73 while (n && n->cp.right)
74 n = n->cp.right;
75 while (n->flags & IR3_INSTR_UNUSED) {
76 n = n->cp.left;
77 if (!n)
78 break;
79 n->cp.right = NULL;
80 }
81 }
82 }
83
84 /* prune false-deps, etc: */
85 foreach_ssa_use (use, instr)
86 foreach_ssa_srcp_n (srcp, n, use)
87 if (*srcp == instr)
88 *srcp = NULL;
89
90 list_delinit(&instr->node);
91 progress = true;
92 }
93 }
94 return progress;
95 }
96
97 static bool
98 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
99 {
100 unsigned i;
101 bool progress = false;
102
103 ir3_clear_mark(ir);
104
105 /* initially mark everything as unused, we'll clear the flag as we
106 * visit the instructions:
107 */
108 foreach_block (block, &ir->block_list) {
109 foreach_instr (instr, &block->instr_list) {
110 /* special case, if pre-fs texture fetch used, we cannot
111 * eliminate the barycentric i/j input
112 */
113 if (so->num_sampler_prefetch &&
114 (instr->opc == OPC_META_INPUT) &&
115 (instr->input.sysval == SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL))
116 continue;
117 instr->flags |= IR3_INSTR_UNUSED;
118 }
119 }
120
121 foreach_output (out, ir)
122 instr_dce(out, false);
123
124 foreach_block (block, &ir->block_list) {
125 for (i = 0; i < block->keeps_count; i++)
126 instr_dce(block->keeps[i], false);
127
128 /* We also need to account for if-condition: */
129 if (block->condition)
130 instr_dce(block->condition, false);
131 }
132
133 /* remove un-used instructions: */
134 foreach_block (block, &ir->block_list) {
135 progress |= remove_unused_by_block(block);
136 }
137
138 /* fixup wrmask of split instructions to account for adjusted tex
139 * wrmask's:
140 */
141 foreach_block (block, &ir->block_list) {
142 foreach_instr (instr, &block->instr_list) {
143 if (instr->opc != OPC_META_SPLIT)
144 continue;
145
146 struct ir3_instruction *src = ssa(instr->regs[1]);
147 if (!is_tex_or_prefetch(src))
148 continue;
149
150 instr->regs[1]->wrmask = src->regs[0]->wrmask;
151 }
152 }
153
154 /* note that we can end up with unused indirects, but we should
155 * not end up with unused predicates.
156 */
157 for (i = 0; i < ir->a0_users_count; i++) {
158 struct ir3_instruction *instr = ir->a0_users[i];
159 if (instr && (instr->flags & IR3_INSTR_UNUSED))
160 ir->a0_users[i] = NULL;
161 }
162
163 for (i = 0; i < ir->a1_users_count; i++) {
164 struct ir3_instruction *instr = ir->a1_users[i];
165 if (instr && (instr->flags & IR3_INSTR_UNUSED))
166 ir->a1_users[i] = NULL;
167 }
168
169 /* cleanup unused inputs: */
170 foreach_input_n (in, n, ir)
171 if (in->flags & IR3_INSTR_UNUSED)
172 ir->inputs[n] = NULL;
173
174 return progress;
175 }
176
177 bool
178 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
179 {
180 void *mem_ctx = ralloc_context(NULL);
181 bool progress, made_progress = false;
182
183 ir3_find_ssa_uses(ir, mem_ctx, true);
184
185 do {
186 progress = find_and_remove_unused(ir, so);
187 made_progress |= progress;
188 } while (progress);
189
190 ralloc_free(mem_ctx);
191
192 return made_progress;
193 }