aboutsummaryrefslogtreecommitdiffstats
path: root/src/panfrost/midgard/midgard_opt_dce.c
blob: be9307e712a746c1be7c05d2db4c15ecbb997076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2018 Alyssa Rosenzweig
 * Copyright (C) 2019 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "compiler.h"
#include "util/u_memory.h"
#include "midgard_ops.h"

/* SIMD-aware dead code elimination. Perform liveness analysis step-by-step,
 * removing dead components. If an instruction ends up with a zero mask, the
 * instruction in total is dead and should be removed. */

static bool
can_cull_mask(compiler_context *ctx, midgard_instruction *ins)
{
        if (ins->dest >= ctx->temp_count)
                return false;

        if (ins->type == TAG_LOAD_STORE_4)
                if (load_store_opcode_props[ins->load_store.op].props & LDST_SPECIAL_MASK)
                        return false;

        return true;
}

static bool
can_dce(midgard_instruction *ins)
{
        if (ins->mask)
                return false;

        if (ins->compact_branch)
                return false;

        if (ins->type == TAG_LOAD_STORE_4)
                if (load_store_opcode_props[ins->load_store.op].props & LDST_SIDE_FX)
                        return false;

        if (ins->type == TAG_TEXTURE_4)
                if (ins->texture.op == TEXTURE_OP_BARRIER)
                        return false;

        return true;
}

static bool
midgard_opt_dead_code_eliminate_block(compiler_context *ctx, midgard_block *block)
{
        bool progress = false;

        uint16_t *live = mem_dup(block->base.live_out, ctx->temp_count * sizeof(uint16_t));

        mir_foreach_instr_in_block_rev(block, ins) {
                if (can_cull_mask(ctx, ins)) {
                        unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
                        unsigned oldmask = ins->mask;

                        unsigned rounded = mir_round_bytemask_up(live[ins->dest], type_size);
                        unsigned cmask = mir_from_bytemask(rounded, type_size);

                        ins->mask &= cmask;
                        progress |= (ins->mask != oldmask);
                }

                mir_liveness_ins_update(live, ins, ctx->temp_count);
        }

        mir_foreach_instr_in_block_safe(block, ins) {
                if (can_dce(ins)) {
                        mir_remove_instruction(ins);
                        progress = true;
                }
        }

        free(live);

        return progress;
}

bool
midgard_opt_dead_code_eliminate(compiler_context *ctx)
{
        /* We track liveness. In fact, it's ok if we assume more things are
         * live than they actually are, that just reduces the effectiveness of
         * this iterations lightly. And DCE has the effect of strictly reducing
         * liveness, so we can run DCE across all blocks while only computing
         * liveness at the beginning. */

        mir_invalidate_liveness(ctx);
        mir_compute_liveness(ctx);

        bool progress = false;

        mir_foreach_block(ctx, block) {
                progress |= midgard_opt_dead_code_eliminate_block(ctx, (midgard_block *) block);
        }

        return progress;
}

/* Removes dead moves, that is, moves with a destination overwritten before
 * being read. Normally handled implicitly as part of DCE, but this has to run
 * after the out-of-SSA pass */

bool
midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block)
{
        bool progress = false;

        mir_foreach_instr_in_block_safe(block, ins) {
                if (ins->type != TAG_ALU_4) continue;
                if (ins->compact_branch) continue;
                if (!OP_IS_MOVE(ins->alu.op)) continue;

                /* Check if it's overwritten in this block before being read */
                bool overwritten = false;

                mir_foreach_instr_in_block_from(block, q, mir_next_op(ins)) {
                        /* Check if used */
                        if (mir_has_arg(q, ins->dest))
                                break;

                        /* Check if overwritten */
                        if (q->dest == ins->dest) {
                                /* Special case to vec4; component tracking is
                                 * harder */

                                overwritten = (q->mask == 0xF);
                                break;
                        }
                }

                if (overwritten) {
                        mir_remove_instruction(ins);
                        progress = true;
                }
        }

        return progress;
}