2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
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:
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
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
26 #include "util/u_memory.h"
27 #include "midgard_ops.h"
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. */
34 can_cull_mask(compiler_context
*ctx
, midgard_instruction
*ins
)
36 if (ins
->dest
>= ctx
->temp_count
)
39 if (ins
->type
== TAG_LOAD_STORE_4
)
40 if (load_store_opcode_props
[ins
->load_store
.op
].props
& LDST_SPECIAL_MASK
)
47 can_dce(midgard_instruction
*ins
)
52 if (ins
->compact_branch
)
55 if (ins
->type
== TAG_LOAD_STORE_4
)
56 if (load_store_opcode_props
[ins
->load_store
.op
].props
& LDST_SIDE_FX
)
59 if (ins
->type
== TAG_TEXTURE_4
)
60 if (ins
->texture
.op
== TEXTURE_OP_BARRIER
)
67 midgard_opt_dead_code_eliminate_block(compiler_context
*ctx
, midgard_block
*block
)
69 bool progress
= false;
71 uint16_t *live
= mem_dup(block
->base
.live_out
, ctx
->temp_count
* sizeof(uint16_t));
73 mir_foreach_instr_in_block_rev(block
, ins
) {
74 if (can_cull_mask(ctx
, ins
)) {
75 unsigned type_size
= nir_alu_type_get_type_size(ins
->dest_type
);
76 unsigned oldmask
= ins
->mask
;
78 unsigned rounded
= mir_round_bytemask_up(live
[ins
->dest
], type_size
);
79 unsigned cmask
= mir_from_bytemask(rounded
, type_size
);
82 progress
|= (ins
->mask
!= oldmask
);
85 mir_liveness_ins_update(live
, ins
, ctx
->temp_count
);
88 mir_foreach_instr_in_block_safe(block
, ins
) {
90 mir_remove_instruction(ins
);
101 midgard_opt_dead_code_eliminate(compiler_context
*ctx
)
103 /* We track liveness. In fact, it's ok if we assume more things are
104 * live than they actually are, that just reduces the effectiveness of
105 * this iterations lightly. And DCE has the effect of strictly reducing
106 * liveness, so we can run DCE across all blocks while only computing
107 * liveness at the beginning. */
109 mir_invalidate_liveness(ctx
);
110 mir_compute_liveness(ctx
);
112 bool progress
= false;
114 mir_foreach_block(ctx
, block
) {
115 progress
|= midgard_opt_dead_code_eliminate_block(ctx
, (midgard_block
*) block
);
121 /* Removes dead moves, that is, moves with a destination overwritten before
122 * being read. Normally handled implicitly as part of DCE, but this has to run
123 * after the out-of-SSA pass */
126 midgard_opt_dead_move_eliminate(compiler_context
*ctx
, midgard_block
*block
)
128 bool progress
= false;
130 mir_foreach_instr_in_block_safe(block
, ins
) {
131 if (ins
->type
!= TAG_ALU_4
) continue;
132 if (ins
->compact_branch
) continue;
133 if (!OP_IS_MOVE(ins
->alu
.op
)) continue;
135 /* Check if it's overwritten in this block before being read */
136 bool overwritten
= false;
138 mir_foreach_instr_in_block_from(block
, q
, mir_next_op(ins
)) {
140 if (mir_has_arg(q
, ins
->dest
))
143 /* Check if overwritten */
144 if (q
->dest
== ins
->dest
) {
145 /* Special case to vec4; component tracking is
148 overwritten
= (q
->mask
== 0xF);
154 mir_remove_instruction(ins
);