util: Add a mapping from VkFormat to PIPE_FORMAT.
[mesa.git] / src / panfrost / midgard / midgard_opt_dce.c
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 "compiler.h"
26 #include "util/u_memory.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
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->type == TAG_LOAD_STORE_4)
40 if (load_store_opcode_props[ins->load_store.op].props & LDST_SPECIAL_MASK)
41 return false;
42
43 return true;
44 }
45
46 static bool
47 can_dce(midgard_instruction *ins)
48 {
49 if (ins->mask)
50 return false;
51
52 if (ins->compact_branch)
53 return false;
54
55 if (ins->type == TAG_LOAD_STORE_4)
56 if (load_store_opcode_props[ins->load_store.op].props & LDST_SIDE_FX)
57 return false;
58
59 return true;
60 }
61
62 bool
63 midgard_opt_dead_code_eliminate(compiler_context *ctx, midgard_block *block)
64 {
65 bool progress = false;
66
67 mir_invalidate_liveness(ctx);
68 mir_compute_liveness(ctx);
69
70 uint16_t *live = mem_dup(block->live_out, ctx->temp_count * sizeof(uint16_t));
71
72 mir_foreach_instr_in_block_rev(block, ins) {
73 if (can_cull_mask(ctx, ins)) {
74 midgard_reg_mode mode = mir_typesize(ins);
75 unsigned oldmask = ins->mask;
76
77 unsigned rounded = mir_round_bytemask_down(live[ins->dest], mode);
78 unsigned cmask = mir_from_bytemask(rounded, mode);
79
80 ins->mask &= cmask;
81 progress |= (ins->mask != oldmask);
82 }
83
84 mir_liveness_ins_update(live, ins, ctx->temp_count);
85 }
86
87 mir_foreach_instr_in_block_safe(block, ins) {
88 if (can_dce(ins)) {
89 mir_remove_instruction(ins);
90 progress = true;
91 }
92 }
93
94 free(live);
95
96 return progress;
97 }
98
99 /* Removes dead moves, that is, moves with a destination overwritten before
100 * being read. Normally handled implicitly as part of DCE, but this has to run
101 * after the out-of-SSA pass */
102
103 bool
104 midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block)
105 {
106 bool progress = false;
107
108 mir_foreach_instr_in_block_safe(block, ins) {
109 if (ins->type != TAG_ALU_4) continue;
110 if (ins->compact_branch) continue;
111 if (!OP_IS_MOVE(ins->alu.op)) continue;
112
113 /* Check if it's overwritten in this block before being read */
114 bool overwritten = false;
115
116 mir_foreach_instr_in_block_from(block, q, mir_next_op(ins)) {
117 /* Check if used */
118 if (mir_has_arg(q, ins->dest))
119 break;
120
121 /* Check if overwritten */
122 if (q->dest == ins->dest) {
123 /* Special case to vec4; component tracking is
124 * harder */
125
126 overwritten = (q->mask == 0xF);
127 break;
128 }
129 }
130
131 if (overwritten) {
132 mir_remove_instruction(ins);
133 progress = true;
134 }
135 }
136
137 return progress;
138 }