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