• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
mark_array_use(struct ir3_instruction * instr,struct ir3_register * reg)37 mark_array_use(struct ir3_instruction *instr, struct ir3_register *reg)
38 {
39    if (reg->flags & IR3_REG_ARRAY) {
40       struct ir3_array *arr =
41          ir3_lookup_array(instr->block->shader, reg->array.id);
42       arr->unused = false;
43    }
44 }
45 
46 static void
instr_dce(struct ir3_instruction * instr,bool falsedep)47 instr_dce(struct ir3_instruction *instr, bool falsedep)
48 {
49    /* don't mark falsedep's as used, but otherwise process them normally: */
50    if (!falsedep)
51       instr->flags &= ~IR3_INSTR_UNUSED;
52 
53    if (ir3_instr_check_mark(instr))
54       return;
55 
56    foreach_dst (dst, instr) {
57       if (is_dest_gpr(dst))
58          mark_array_use(instr, dst);
59    }
60 
61    foreach_src (reg, instr)
62       mark_array_use(instr, reg); /* src */
63 
64    foreach_ssa_src_n (src, i, instr) {
65       instr_dce(src, __is_false_dep(instr, i));
66    }
67 }
68 
69 static bool
remove_unused_by_block(struct ir3_block * block)70 remove_unused_by_block(struct ir3_block *block)
71 {
72    bool progress = false;
73    foreach_instr_safe (instr, &block->instr_list) {
74       if (instr->opc == OPC_END || instr->opc == OPC_CHSH ||
75           instr->opc == OPC_CHMASK || instr->opc == OPC_LOCK ||
76           instr->opc == OPC_UNLOCK)
77          continue;
78       if (instr->flags & IR3_INSTR_UNUSED) {
79          if (instr->opc == OPC_META_SPLIT) {
80             struct ir3_instruction *src = ssa(instr->srcs[0]);
81             /* tex (cat5) instructions have a writemask, so we can
82              * mask off unused components.  Other instructions do not.
83              */
84             if (src && is_tex_or_prefetch(src) && (src->dsts[0]->wrmask > 1)) {
85                src->dsts[0]->wrmask &= ~(1 << instr->split.off);
86             }
87          }
88 
89          /* prune false-deps, etc: */
90          foreach_ssa_use (use, instr)
91             foreach_ssa_srcp_n (srcp, n, use)
92                if (*srcp == instr)
93                   *srcp = NULL;
94 
95          list_delinit(&instr->node);
96          progress = true;
97       }
98    }
99    return progress;
100 }
101 
102 static bool
find_and_remove_unused(struct ir3 * ir,struct ir3_shader_variant * so)103 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
104 {
105    unsigned i;
106    bool progress = false;
107 
108    ir3_clear_mark(ir);
109 
110    /* initially mark everything as unused, we'll clear the flag as we
111     * visit the instructions:
112     */
113    foreach_block (block, &ir->block_list) {
114       foreach_instr (instr, &block->instr_list) {
115          if (instr->opc == OPC_META_INPUT) {
116             /* Without GS header geometry shader is never invoked. */
117             if (instr->input.sysval == SYSTEM_VALUE_GS_HEADER_IR3)
118                continue;
119          }
120 
121          instr->flags |= IR3_INSTR_UNUSED;
122       }
123    }
124 
125    foreach_array (arr, &ir->array_list)
126       arr->unused = true;
127 
128    foreach_block (block, &ir->block_list) {
129       for (i = 0; i < block->keeps_count; i++)
130          instr_dce(block->keeps[i], false);
131 
132       /* We also need to account for if-condition: */
133       if (block->condition)
134          instr_dce(block->condition, false);
135    }
136 
137    /* remove un-used instructions: */
138    foreach_block (block, &ir->block_list) {
139       progress |= remove_unused_by_block(block);
140    }
141 
142    /* remove un-used arrays: */
143    foreach_array_safe (arr, &ir->array_list) {
144       if (arr->unused)
145          list_delinit(&arr->node);
146    }
147 
148    /* fixup wrmask of split instructions to account for adjusted tex
149     * wrmask's:
150     */
151    foreach_block (block, &ir->block_list) {
152       foreach_instr (instr, &block->instr_list) {
153          if (instr->opc != OPC_META_SPLIT)
154             continue;
155 
156          struct ir3_instruction *src = ssa(instr->srcs[0]);
157          if (!is_tex_or_prefetch(src))
158             continue;
159 
160          instr->srcs[0]->wrmask = src->dsts[0]->wrmask;
161       }
162    }
163 
164    for (i = 0; i < ir->a0_users_count; i++) {
165       struct ir3_instruction *instr = ir->a0_users[i];
166       if (instr && (instr->flags & IR3_INSTR_UNUSED))
167          ir->a0_users[i] = NULL;
168    }
169 
170    for (i = 0; i < ir->a1_users_count; i++) {
171       struct ir3_instruction *instr = ir->a1_users[i];
172       if (instr && (instr->flags & IR3_INSTR_UNUSED))
173          ir->a1_users[i] = NULL;
174    }
175 
176    for (i = 0; i < ir->predicates_count; i++) {
177       struct ir3_instruction *instr = ir->predicates[i];
178       if (instr && (instr->flags & IR3_INSTR_UNUSED))
179          ir->predicates[i] = NULL;
180    }
181 
182    /* cleanup unused inputs: */
183    foreach_input_n (in, n, ir)
184       if (in->flags & IR3_INSTR_UNUSED)
185          ir->inputs[n] = NULL;
186 
187    return progress;
188 }
189 
190 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)191 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
192 {
193    void *mem_ctx = ralloc_context(NULL);
194    bool progress, made_progress = false;
195 
196    ir3_find_ssa_uses(ir, mem_ctx, true);
197 
198    do {
199       progress = find_and_remove_unused(ir, so);
200       made_progress |= progress;
201    } while (progress);
202 
203    ralloc_free(mem_ctx);
204 
205    return made_progress;
206 }
207