xref: /aosp_15_r20/external/mesa3d/src/freedreno/ir3/ir3_dce.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "util/u_math.h"
10 
11 #include "ir3.h"
12 #include "ir3_shader.h"
13 
14 /*
15  * Dead code elimination:
16  */
17 
18 static void
mark_array_use(struct ir3_instruction * instr,struct ir3_register * reg)19 mark_array_use(struct ir3_instruction *instr, struct ir3_register *reg)
20 {
21    if (reg->flags & IR3_REG_ARRAY) {
22       struct ir3_array *arr =
23          ir3_lookup_array(instr->block->shader, reg->array.id);
24       arr->unused = false;
25    }
26 }
27 
28 static void
instr_dce(struct ir3_instruction * instr,bool falsedep)29 instr_dce(struct ir3_instruction *instr, bool falsedep)
30 {
31    /* don't mark falsedep's as used, but otherwise process them normally: */
32    if (!falsedep)
33       instr->flags &= ~IR3_INSTR_UNUSED;
34 
35    if (ir3_instr_check_mark(instr))
36       return;
37 
38    foreach_dst (dst, instr) {
39       if (is_dest_gpr(dst))
40          mark_array_use(instr, dst);
41    }
42 
43    foreach_src (reg, instr)
44       mark_array_use(instr, reg); /* src */
45 
46    foreach_ssa_src_n (src, i, instr) {
47       instr_dce(src, __is_false_dep(instr, i));
48    }
49 }
50 
51 static bool
remove_unused_by_block(struct ir3_block * block)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 ||
57           instr->opc == OPC_CHMASK || instr->opc == OPC_LOCK ||
58           instr->opc == OPC_UNLOCK)
59          continue;
60       if (instr->flags & IR3_INSTR_UNUSED) {
61          if (instr->opc == OPC_META_SPLIT) {
62             struct ir3_instruction *src = ssa(instr->srcs[0]);
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->dsts[0]->wrmask > 1)) {
67                src->dsts[0]->wrmask &= ~(1 << instr->split.off);
68             }
69          }
70 
71          /* prune false-deps, etc: */
72          foreach_ssa_use (use, instr)
73             foreach_ssa_srcp_n (srcp, n, use)
74                if (*srcp == instr)
75                   *srcp = NULL;
76 
77          ir3_instr_remove(instr);
78          progress = true;
79       }
80    }
81    return progress;
82 }
83 
84 static bool
find_and_remove_unused(struct ir3 * ir,struct ir3_shader_variant * so)85 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
86 {
87    unsigned i;
88    bool progress = false;
89 
90    ir3_clear_mark(ir);
91 
92    /* initially mark everything as unused, we'll clear the flag as we
93     * visit the instructions:
94     */
95    foreach_block (block, &ir->block_list) {
96       foreach_instr (instr, &block->instr_list) {
97          if (instr->opc == OPC_META_INPUT) {
98             /* Without GS header geometry shader is never invoked. */
99             if (instr->input.sysval == SYSTEM_VALUE_GS_HEADER_IR3)
100                continue;
101          }
102 
103          instr->flags |= IR3_INSTR_UNUSED;
104       }
105    }
106 
107    foreach_array (arr, &ir->array_list)
108       arr->unused = true;
109 
110    foreach_block (block, &ir->block_list) {
111       for (i = 0; i < block->keeps_count; i++)
112          instr_dce(block->keeps[i], false);
113 
114       /* We also need to account for if-condition: */
115       struct ir3_instruction *terminator = ir3_block_get_terminator(block);
116       if (terminator) {
117          instr_dce(terminator, false);
118       }
119    }
120 
121    /* remove un-used instructions: */
122    foreach_block (block, &ir->block_list) {
123       progress |= remove_unused_by_block(block);
124    }
125 
126    /* remove un-used arrays: */
127    foreach_array_safe (arr, &ir->array_list) {
128       if (arr->unused)
129          list_delinit(&arr->node);
130    }
131 
132    /* fixup wrmask of split instructions to account for adjusted tex
133     * wrmask's:
134     */
135    foreach_block (block, &ir->block_list) {
136       foreach_instr (instr, &block->instr_list) {
137          if (instr->opc != OPC_META_SPLIT)
138             continue;
139 
140          struct ir3_instruction *src = ssa(instr->srcs[0]);
141          if (!is_tex_or_prefetch(src))
142             continue;
143 
144          instr->srcs[0]->wrmask = src->dsts[0]->wrmask;
145       }
146    }
147 
148    for (i = 0; i < ir->a0_users_count; i++) {
149       struct ir3_instruction *instr = ir->a0_users[i];
150       if (instr && (instr->flags & IR3_INSTR_UNUSED))
151          ir->a0_users[i] = NULL;
152    }
153 
154    for (i = 0; i < ir->a1_users_count; i++) {
155       struct ir3_instruction *instr = ir->a1_users[i];
156       if (instr && (instr->flags & IR3_INSTR_UNUSED))
157          ir->a1_users[i] = NULL;
158    }
159 
160    /* cleanup unused inputs: */
161    foreach_input_n (in, n, ir)
162       if (in->flags & IR3_INSTR_UNUSED)
163          ir->inputs[n] = NULL;
164 
165    return progress;
166 }
167 
168 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)169 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
170 {
171    void *mem_ctx = ralloc_context(NULL);
172    bool progress, made_progress = false;
173 
174    ir3_find_ssa_uses(ir, mem_ctx, true);
175 
176    do {
177       progress = find_and_remove_unused(ir, so);
178       made_progress |= progress;
179    } while (progress);
180 
181    ralloc_free(mem_ctx);
182 
183    return made_progress;
184 }
185