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 struct ir3_instruction
*src
;
41 /* don't mark falsedep's as used, but otherwise process them normally: */
43 instr
->flags
&= ~IR3_INSTR_UNUSED
;
45 if (ir3_instr_check_mark(instr
))
48 foreach_ssa_src_n (src
, i
, instr
) {
49 instr_dce(src
, __is_false_dep(instr
, i
));
54 remove_unused_by_block(struct ir3_block
*block
)
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
)
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.
66 if (src
&& is_tex_or_prefetch(src
) && (src
->regs
[0]->wrmask
> 1)) {
67 src
->regs
[0]->wrmask
&= ~(1 << instr
->split
.off
);
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
74 struct ir3_instruction
*n
= instr
;
75 while (n
&& n
->cp
.right
)
77 while (n
->flags
& IR3_INSTR_UNUSED
) {
86 /* prune false-deps, etc: */
87 foreach_ssa_use (use
, instr
)
88 foreach_ssa_srcp_n (srcp
, n
, use
)
92 list_delinit(&instr
->node
);
100 find_and_remove_unused(struct ir3
*ir
, struct ir3_shader_variant
*so
)
103 bool progress
= false;
107 /* initially mark everything as unused, we'll clear the flag as we
108 * visit the instructions:
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
115 if (so
->num_sampler_prefetch
&&
116 (instr
->opc
== OPC_META_INPUT
) &&
117 (instr
->input
.sysval
== SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
))
119 instr
->flags
|= IR3_INSTR_UNUSED
;
123 struct ir3_instruction
*out
;
124 foreach_output (out
, ir
)
125 instr_dce(out
, false);
127 foreach_block (block
, &ir
->block_list
) {
128 for (i
= 0; i
< block
->keeps_count
; i
++)
129 instr_dce(block
->keeps
[i
], false);
131 /* We also need to account for if-condition: */
132 if (block
->condition
)
133 instr_dce(block
->condition
, false);
136 /* remove un-used instructions: */
137 foreach_block (block
, &ir
->block_list
) {
138 progress
|= remove_unused_by_block(block
);
141 /* fixup wrmask of split instructions to account for adjusted tex
144 foreach_block (block
, &ir
->block_list
) {
145 foreach_instr (instr
, &block
->instr_list
) {
146 if (instr
->opc
!= OPC_META_SPLIT
)
149 struct ir3_instruction
*src
= ssa(instr
->regs
[1]);
150 if (!is_tex_or_prefetch(src
))
153 instr
->regs
[1]->wrmask
= src
->regs
[0]->wrmask
;
157 /* note that we can end up with unused indirects, but we should
158 * not end up with unused predicates.
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
;
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
;
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
;
182 ir3_dce(struct ir3
*ir
, struct ir3_shader_variant
*so
)
184 void *mem_ctx
= ralloc_context(NULL
);
187 ir3_find_ssa_uses(ir
, mem_ctx
, true);
190 progress
= find_and_remove_unused(ir
, so
);
193 ralloc_free(mem_ctx
);