2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/u_math.h"
30 #include "ir3_shader.h"
33 * Dead code elimination:
37 instr_dce(struct ir3_instruction
*instr
, bool falsedep
)
39 /* don't mark falsedep's as used, but otherwise process them normally: */
41 instr
->flags
&= ~IR3_INSTR_UNUSED
;
43 if (ir3_instr_check_mark(instr
))
46 foreach_ssa_src_n (src
, i
, instr
) {
47 instr_dce(src
, __is_false_dep(instr
, i
));
52 remove_unused_by_block(struct ir3_block
*block
)
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
)
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.
64 if (src
&& is_tex_or_prefetch(src
) && (src
->regs
[0]->wrmask
> 1)) {
65 src
->regs
[0]->wrmask
&= ~(1 << instr
->split
.off
);
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
72 struct ir3_instruction
*n
= instr
;
73 while (n
&& n
->cp
.right
)
75 while (n
->flags
& IR3_INSTR_UNUSED
) {
84 /* prune false-deps, etc: */
85 foreach_ssa_use (use
, instr
)
86 foreach_ssa_srcp_n (srcp
, n
, use
)
90 list_delinit(&instr
->node
);
98 find_and_remove_unused(struct ir3
*ir
, struct ir3_shader_variant
*so
)
101 bool progress
= false;
105 /* initially mark everything as unused, we'll clear the flag as we
106 * visit the instructions:
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
113 if (so
->num_sampler_prefetch
&&
114 (instr
->opc
== OPC_META_INPUT
) &&
115 (instr
->input
.sysval
== SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
))
117 instr
->flags
|= IR3_INSTR_UNUSED
;
121 foreach_output (out
, ir
)
122 instr_dce(out
, false);
124 foreach_block (block
, &ir
->block_list
) {
125 for (i
= 0; i
< block
->keeps_count
; i
++)
126 instr_dce(block
->keeps
[i
], false);
128 /* We also need to account for if-condition: */
129 if (block
->condition
)
130 instr_dce(block
->condition
, false);
133 /* remove un-used instructions: */
134 foreach_block (block
, &ir
->block_list
) {
135 progress
|= remove_unused_by_block(block
);
138 /* fixup wrmask of split instructions to account for adjusted tex
141 foreach_block (block
, &ir
->block_list
) {
142 foreach_instr (instr
, &block
->instr_list
) {
143 if (instr
->opc
!= OPC_META_SPLIT
)
146 struct ir3_instruction
*src
= ssa(instr
->regs
[1]);
147 if (!is_tex_or_prefetch(src
))
150 instr
->regs
[1]->wrmask
= src
->regs
[0]->wrmask
;
154 /* note that we can end up with unused indirects, but we should
155 * not end up with unused predicates.
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
;
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
;
169 /* cleanup unused inputs: */
170 foreach_input_n (in
, n
, ir
)
171 if (in
->flags
& IR3_INSTR_UNUSED
)
172 ir
->inputs
[n
] = NULL
;
178 ir3_dce(struct ir3
*ir
, struct ir3_shader_variant
*so
)
180 void *mem_ctx
= ralloc_context(NULL
);
181 bool progress
, made_progress
= false;
183 ir3_find_ssa_uses(ir
, mem_ctx
, true);
186 progress
= find_and_remove_unused(ir
, so
);
187 made_progress
|= progress
;
190 ralloc_free(mem_ctx
);
192 return made_progress
;