• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Rob Clark <robclark@freedesktop.org>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <robclark@freedesktop.org>
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             if (instr->input.sysval == SYSTEM_VALUE_SAMPLE_MASK_IN &&
102                 so->reads_shading_rate &&
103                 ir->compiler->reading_shading_rate_requires_smask_quirk)
104                continue;
105          }
106 
107          instr->flags |= IR3_INSTR_UNUSED;
108       }
109    }
110 
111    foreach_array (arr, &ir->array_list)
112       arr->unused = true;
113 
114    foreach_block (block, &ir->block_list) {
115       for (i = 0; i < block->keeps_count; i++)
116          instr_dce(block->keeps[i], false);
117 
118       /* We also need to account for if-condition: */
119       struct ir3_instruction *terminator = ir3_block_get_terminator(block);
120       if (terminator) {
121          instr_dce(terminator, false);
122       }
123    }
124 
125    /* remove un-used instructions: */
126    foreach_block (block, &ir->block_list) {
127       progress |= remove_unused_by_block(block);
128    }
129 
130    /* remove un-used arrays: */
131    foreach_array_safe (arr, &ir->array_list) {
132       if (arr->unused)
133          list_delinit(&arr->node);
134    }
135 
136    /* fixup wrmask of split instructions to account for adjusted tex
137     * wrmask's:
138     */
139    foreach_block (block, &ir->block_list) {
140       foreach_instr (instr, &block->instr_list) {
141          if (instr->opc != OPC_META_SPLIT)
142             continue;
143 
144          struct ir3_instruction *src = ssa(instr->srcs[0]);
145          if (!is_tex_or_prefetch(src))
146             continue;
147 
148          instr->srcs[0]->wrmask = src->dsts[0]->wrmask;
149       }
150    }
151 
152    for (i = 0; i < ir->a0_users_count; i++) {
153       struct ir3_instruction *instr = ir->a0_users[i];
154       if (instr && (instr->flags & IR3_INSTR_UNUSED))
155          ir->a0_users[i] = NULL;
156    }
157 
158    for (i = 0; i < ir->a1_users_count; i++) {
159       struct ir3_instruction *instr = ir->a1_users[i];
160       if (instr && (instr->flags & IR3_INSTR_UNUSED))
161          ir->a1_users[i] = NULL;
162    }
163 
164    /* cleanup unused inputs: */
165    foreach_input_n (in, n, ir)
166       if (in->flags & IR3_INSTR_UNUSED)
167          ir->inputs[n] = NULL;
168 
169    return progress;
170 }
171 
172 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)173 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
174 {
175    void *mem_ctx = ralloc_context(NULL);
176    bool progress, made_progress = false;
177 
178    ir3_find_ssa_uses(ir, mem_ctx, true);
179 
180    do {
181       progress = find_and_remove_unused(ir, so);
182       made_progress |= progress;
183    } while (progress);
184 
185    ralloc_free(mem_ctx);
186 
187    return made_progress;
188 }
189