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_invalidate_liveness(ctx);
36 bi_compute_liveness(ctx);
37
38 bi_foreach_block_rev(ctx, block) {
39 uint8_t *live = rzalloc_array(block, uint8_t, temp_count);
40
41 bi_foreach_successor(block, succ) {
42 for (unsigned i = 0; i < temp_count; ++i)
43 live[i] |= succ->live_in[i];
44 }
45
46 bi_foreach_instr_in_block_safe_rev(block, ins) {
47 bool all_null = true;
48
49 bi_foreach_dest(ins, d) {
50 unsigned index = bi_get_node(ins->dest[d]);
51
52 if (index < temp_count && !(live[index] & bi_writemask(ins, d)))
53 ins->dest[d] = bi_null();
54
55 all_null &= bi_is_null(ins->dest[d]);
56 }
57
58 if (all_null && !bi_side_effects(ins->op))
59 bi_remove_instruction(ins);
60 else
61 bi_liveness_ins_update(live, ins, temp_count);
62 }
63
64 ralloc_free(block->live_in);
65 block->live_in = live;
66 }
67 }
68
69 /* Post-RA liveness-based dead code analysis to clean up results of bundling */
70
71 uint64_t
bi_postra_liveness_ins(uint64_t live,bi_instr * ins)72 bi_postra_liveness_ins(uint64_t live, bi_instr *ins)
73 {
74 bi_foreach_dest(ins, d) {
75 if (ins->dest[d].type == BI_INDEX_REGISTER) {
76 unsigned nr = bi_count_write_registers(ins, d);
77 unsigned reg = ins->dest[d].value;
78 live &= ~(BITFIELD64_MASK(nr) << reg);
79 }
80 }
81
82 bi_foreach_src(ins, s) {
83 if (ins->src[s].type == BI_INDEX_REGISTER) {
84 unsigned nr = bi_count_read_registers(ins, s);
85 unsigned reg = ins->src[s].value;
86 live |= (BITFIELD64_MASK(nr) << reg);
87 }
88 }
89
90 return live;
91 }
92
93 static bool
bi_postra_liveness_block(bi_block * blk)94 bi_postra_liveness_block(bi_block *blk)
95 {
96 bi_foreach_successor(blk, succ)
97 blk->reg_live_out |= succ->reg_live_in;
98
99 uint64_t live = blk->reg_live_out;
100
101 bi_foreach_instr_in_block_rev(blk, ins)
102 live = bi_postra_liveness_ins(live, ins);
103
104 bool progress = blk->reg_live_in != live;
105 blk->reg_live_in = live;
106 return progress;
107 }
108
109 /* Globally, liveness analysis uses a fixed-point algorithm based on a
110 * worklist. We initialize a work list with the exit block. We iterate the work
111 * list to compute live_in from live_out for each block on the work list,
112 * adding the predecessors of the block to the work list if we made progress.
113 */
114
115 void
bi_postra_liveness(bi_context * ctx)116 bi_postra_liveness(bi_context *ctx)
117 {
118 struct set *work_list = _mesa_set_create(NULL,
119 _mesa_hash_pointer,
120 _mesa_key_pointer_equal);
121
122 struct set *visited = _mesa_set_create(NULL,
123 _mesa_hash_pointer,
124 _mesa_key_pointer_equal);
125
126 struct set_entry *cur;
127 cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks));
128
129 bi_foreach_block(ctx, block) {
130 block->reg_live_out = block->reg_live_in = 0;
131 }
132
133 do {
134 bi_block *blk = (struct bi_block *) cur->key;
135 _mesa_set_remove(work_list, cur);
136
137 /* Update its liveness information */
138 bool progress = bi_postra_liveness_block(blk);
139
140 /* If we made progress, we need to process the predecessors */
141
142 if (progress || !_mesa_set_search(visited, blk)) {
143 bi_foreach_predecessor((blk), pred)
144 _mesa_set_add(work_list, pred);
145 }
146
147 _mesa_set_add(visited, blk);
148 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
149
150 _mesa_set_destroy(visited, NULL);
151 _mesa_set_destroy(work_list, NULL);
152 }
153
154 void
bi_opt_dce_post_ra(bi_context * ctx)155 bi_opt_dce_post_ra(bi_context *ctx)
156 {
157 bi_postra_liveness(ctx);
158
159 bi_foreach_block_rev(ctx, block) {
160 uint64_t live = block->reg_live_out;
161
162 bi_foreach_instr_in_block_rev(block, ins) {
163 bi_foreach_dest(ins, d) {
164 if (ins->dest[d].type != BI_INDEX_REGISTER)
165 continue;
166
167 unsigned nr = bi_count_write_registers(ins, d);
168 unsigned reg = ins->dest[d].value;
169 uint64_t mask = (BITFIELD64_MASK(nr) << reg);
170 bool cullable = (ins->op != BI_OPCODE_BLEND);
171
172 if (!(live & mask) && cullable)
173 ins->dest[d] = bi_null();
174 }
175
176 live = bi_postra_liveness_ins(live, ins);
177 }
178 }
179 }
180