1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "util/u_memory.h"
26 #include "compiler.h"
27 #include "midgard_ops.h"
28
29 /* SIMD-aware dead code elimination. Perform liveness analysis step-by-step,
30 * removing dead components. If an instruction ends up with a zero mask, the
31 * instruction in total is dead and should be removed. */
32
33 static bool
can_cull_mask(compiler_context * ctx,midgard_instruction * ins)34 can_cull_mask(compiler_context *ctx, midgard_instruction *ins)
35 {
36 if (ins->dest >= ctx->temp_count)
37 return false;
38
39 if (ins->dest == ctx->blend_src1)
40 return false;
41
42 if (ins->type == TAG_LOAD_STORE_4)
43 if (load_store_opcode_props[ins->op].props & LDST_SPECIAL_MASK)
44 return false;
45
46 return true;
47 }
48
49 static bool
can_dce(midgard_instruction * ins)50 can_dce(midgard_instruction *ins)
51 {
52 if (ins->mask)
53 return false;
54
55 if (ins->compact_branch)
56 return false;
57
58 if (ins->type == TAG_LOAD_STORE_4)
59 if (load_store_opcode_props[ins->op].props & LDST_SIDE_FX)
60 return false;
61
62 if (ins->type == TAG_TEXTURE_4)
63 if (ins->op == midgard_tex_op_barrier)
64 return false;
65
66 return true;
67 }
68
69 static bool
midgard_opt_dead_code_eliminate_block(compiler_context * ctx,midgard_block * block)70 midgard_opt_dead_code_eliminate_block(compiler_context *ctx,
71 midgard_block *block)
72 {
73 bool progress = false;
74
75 uint16_t *live =
76 mem_dup(block->base.live_out, ctx->temp_count * sizeof(uint16_t));
77
78 mir_foreach_instr_in_block_rev(block, ins) {
79 if (can_cull_mask(ctx, ins)) {
80 unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
81 unsigned round_size = type_size;
82 unsigned oldmask = ins->mask;
83
84 /* Make sure we're packable */
85 if (type_size < 32 && ins->type == TAG_LOAD_STORE_4)
86 round_size = 32;
87
88 unsigned rounded = mir_round_bytemask_up(live[ins->dest], round_size);
89 unsigned cmask = mir_from_bytemask(rounded, type_size);
90
91 ins->mask &= cmask;
92 progress |= (ins->mask != oldmask);
93 }
94
95 mir_liveness_ins_update(live, ins, ctx->temp_count);
96 }
97
98 mir_foreach_instr_in_block_safe(block, ins) {
99 if (can_dce(ins)) {
100 mir_remove_instruction(ins);
101 progress = true;
102 }
103 }
104
105 free(live);
106
107 return progress;
108 }
109
110 bool
midgard_opt_dead_code_eliminate(compiler_context * ctx)111 midgard_opt_dead_code_eliminate(compiler_context *ctx)
112 {
113 /* We track liveness. In fact, it's ok if we assume more things are
114 * live than they actually are, that just reduces the effectiveness of
115 * this iterations lightly. And DCE has the effect of strictly reducing
116 * liveness, so we can run DCE across all blocks while only computing
117 * liveness at the beginning. */
118
119 mir_invalidate_liveness(ctx);
120 mir_compute_liveness(ctx);
121
122 bool progress = false;
123
124 mir_foreach_block(ctx, block) {
125 progress |=
126 midgard_opt_dead_code_eliminate_block(ctx, (midgard_block *)block);
127 }
128
129 return progress;
130 }
131
132 /* Removes dead moves, that is, moves with a destination overwritten before
133 * being read. Normally handled implicitly as part of DCE, but this has to run
134 * after the out-of-SSA pass */
135
136 bool
midgard_opt_dead_move_eliminate(compiler_context * ctx,midgard_block * block)137 midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block)
138 {
139 bool progress = false;
140
141 mir_foreach_instr_in_block_safe(block, ins) {
142 if (ins->type != TAG_ALU_4)
143 continue;
144 if (ins->compact_branch)
145 continue;
146 if (!OP_IS_MOVE(ins->op))
147 continue;
148
149 /* Check if it's overwritten in this block before being read */
150 bool overwritten = false;
151
152 mir_foreach_instr_in_block_from(block, q, mir_next_op(ins)) {
153 /* Check if used */
154 if (mir_has_arg(q, ins->dest))
155 break;
156
157 /* Check if overwritten */
158 if (q->dest == ins->dest) {
159 /* Special case to vec4; component tracking is
160 * harder */
161
162 overwritten = (q->mask == 0xF);
163 break;
164 }
165 }
166
167 if (overwritten) {
168 mir_remove_instruction(ins);
169 progress = true;
170 }
171 }
172
173 return progress;
174 }
175