1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 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
28 /* A simple liveness-based dead code elimination pass. */
29
30 void
bi_opt_dead_code_eliminate(bi_context * ctx)31 bi_opt_dead_code_eliminate(bi_context *ctx)
32 {
33 unsigned temp_count = bi_max_temp(ctx);
34
35 bi_compute_liveness(ctx);
36
37 bi_foreach_block_rev(ctx, block) {
38 uint8_t *live = rzalloc_array(block, uint8_t, temp_count);
39
40 bi_foreach_successor(block, succ) {
41 for (unsigned i = 0; i < temp_count; ++i)
42 live[i] |= succ->live_in[i];
43 }
44
45 bi_foreach_instr_in_block_safe_rev(block, ins) {
46 bool all_null = true;
47
48 bi_foreach_dest(ins, d) {
49 unsigned index = bi_get_node(ins->dest[d]);
50
51 /* Destination required */
52 if (ins->op == BI_OPCODE_AXCHG_I32 ||
53 ins->op == BI_OPCODE_ACMPXCHG_I32 ||
54 ins->op == BI_OPCODE_ATOM_RETURN_I32 ||
55 ins->op == BI_OPCODE_ATOM1_RETURN_I32 ||
56 ins->op == BI_OPCODE_BLEND ||
57 ins->op == BI_OPCODE_ATEST ||
58 ins->op == BI_OPCODE_ZS_EMIT)
59 continue;
60
61 if (index < temp_count && !(live[index] & bi_writemask(ins, d)))
62 ins->dest[d] = bi_null();
63
64 all_null &= bi_is_null(ins->dest[d]);
65 }
66
67 if (all_null && !bi_side_effects(ins))
68 bi_remove_instruction(ins);
69 else
70 bi_liveness_ins_update(live, ins, temp_count);
71 }
72
73 ralloc_free(block->live_in);
74 block->live_in = live;
75 }
76 }
77
78 /* Post-RA liveness-based dead code analysis to clean up results of bundling */
79
80 uint64_t MUST_CHECK
bi_postra_liveness_ins(uint64_t live,bi_instr * ins)81 bi_postra_liveness_ins(uint64_t live, bi_instr *ins)
82 {
83 bi_foreach_dest(ins, d) {
84 if (ins->dest[d].type == BI_INDEX_REGISTER) {
85 unsigned nr = bi_count_write_registers(ins, d);
86 unsigned reg = ins->dest[d].value;
87 live &= ~(BITFIELD64_MASK(nr) << reg);
88 }
89 }
90
91 bi_foreach_src(ins, s) {
92 if (ins->src[s].type == BI_INDEX_REGISTER) {
93 unsigned nr = bi_count_read_registers(ins, s);
94 unsigned reg = ins->src[s].value;
95 live |= (BITFIELD64_MASK(nr) << reg);
96 }
97 }
98
99 return live;
100 }
101
102 static bool
bi_postra_liveness_block(bi_block * blk)103 bi_postra_liveness_block(bi_block *blk)
104 {
105 bi_foreach_successor(blk, succ)
106 blk->reg_live_out |= succ->reg_live_in;
107
108 uint64_t live = blk->reg_live_out;
109
110 bi_foreach_instr_in_block_rev(blk, ins)
111 live = bi_postra_liveness_ins(live, ins);
112
113 bool progress = blk->reg_live_in != live;
114 blk->reg_live_in = live;
115 return progress;
116 }
117
118 /* Globally, liveness analysis uses a fixed-point algorithm based on a
119 * worklist. We initialize a work list with the exit block. We iterate the work
120 * list to compute live_in from live_out for each block on the work list,
121 * adding the predecessors of the block to the work list if we made progress.
122 */
123
124 void
bi_postra_liveness(bi_context * ctx)125 bi_postra_liveness(bi_context *ctx)
126 {
127 u_worklist worklist;
128 bi_worklist_init(ctx, &worklist);
129
130 bi_foreach_block(ctx, block) {
131 block->reg_live_out = block->reg_live_in = 0;
132
133 bi_worklist_push_tail(&worklist, block);
134 }
135
136 while (!u_worklist_is_empty(&worklist)) {
137 /* Pop off in reverse order since liveness is backwards */
138 bi_block *blk = bi_worklist_pop_tail(&worklist);
139
140 /* Update liveness information. If we made progress, we need to
141 * reprocess the predecessors
142 */
143 if (bi_postra_liveness_block(blk)) {
144 bi_foreach_predecessor(blk, pred)
145 bi_worklist_push_head(&worklist, *pred);
146 }
147 }
148
149 u_worklist_fini(&worklist);
150 }
151
152 void
bi_opt_dce_post_ra(bi_context * ctx)153 bi_opt_dce_post_ra(bi_context *ctx)
154 {
155 bi_postra_liveness(ctx);
156
157 bi_foreach_block_rev(ctx, block) {
158 uint64_t live = block->reg_live_out;
159
160 bi_foreach_instr_in_block_rev(block, ins) {
161 if (ins->op == BI_OPCODE_DTSEL_IMM)
162 ins->dest[0] = bi_null();
163
164 bi_foreach_dest(ins, d) {
165 if (ins->dest[d].type != BI_INDEX_REGISTER)
166 continue;
167
168 unsigned nr = bi_count_write_registers(ins, d);
169 unsigned reg = ins->dest[d].value;
170 uint64_t mask = (BITFIELD64_MASK(nr) << reg);
171 bool cullable = (ins->op != BI_OPCODE_BLEND);
172 cullable &= !bi_opcode_props[ins->op].sr_write;
173
174 if (!(live & mask) && cullable)
175 ins->dest[d] = bi_null();
176 }
177
178 live = bi_postra_liveness_ins(live, ins);
179 }
180 }
181 }
182