pan/midgard: Calculate temp_count for liveness
[mesa.git] / src / panfrost / midgard / midgard_liveness.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 /* mir_is_live_after performs liveness analysis on the MIR, used primarily
25 * as part of register allocation. TODO: Algorithmic improvements for
26 * compiler performance (this is the worst algorithm possible -- see
27 * backlog with Connor on IRC) */
28
29 #include "compiler.h"
30 #include "util/u_memory.h"
31
32 /* Routines for liveness analysis */
33
34 static void
35 liveness_gen(uint8_t *live, unsigned node, unsigned max, unsigned mask)
36 {
37 if (node >= max)
38 return;
39
40 live[node] |= mask;
41 }
42
43 static void
44 liveness_kill(uint8_t *live, unsigned node, unsigned max, unsigned mask)
45 {
46 if (node >= max)
47 return;
48
49 live[node] &= ~mask;
50 }
51
52 /* Updates live_in for a single instruction */
53
54 void
55 mir_liveness_ins_update(uint8_t *live, midgard_instruction *ins, unsigned max)
56 {
57 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
58
59 liveness_kill(live, ins->dest, max, ins->mask);
60
61 mir_foreach_src(ins, src) {
62 unsigned node = ins->src[src];
63 unsigned mask = mir_mask_of_read_components(ins, node);
64
65 liveness_gen(live, node, max, mask);
66 }
67 }
68
69 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
70
71 static void
72 liveness_block_live_out(compiler_context *ctx, midgard_block *blk)
73 {
74 mir_foreach_successor(blk, succ) {
75 for (unsigned i = 0; i < ctx->temp_count; ++i)
76 blk->live_out[i] |= succ->live_in[i];
77 }
78 }
79
80 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
81 * we compute live_out from live_in. The intrablock pass is linear-time. It
82 * returns whether progress was made. */
83
84 static bool
85 liveness_block_update(compiler_context *ctx, midgard_block *blk)
86 {
87 bool progress = false;
88
89 liveness_block_live_out(ctx, blk);
90
91 uint8_t *live = mem_dup(blk->live_out, ctx->temp_count);
92
93 mir_foreach_instr_in_block_rev(blk, ins)
94 mir_liveness_ins_update(live, ins, ctx->temp_count);
95
96 /* To figure out progress, diff live_in */
97
98 for (unsigned i = 0; (i < ctx->temp_count) && !progress; ++i)
99 progress |= (blk->live_in[i] != live[i]);
100
101 free(blk->live_in);
102 blk->live_in = live;
103
104 return progress;
105 }
106
107 /* Globally, liveness analysis uses a fixed-point algorithm based on a
108 * worklist. We initialize a work list with the exit block. We iterate the work
109 * list to compute live_in from live_out for each block on the work list,
110 * adding the predecessors of the block to the work list if we made progress.
111 */
112
113 void
114 mir_compute_liveness(compiler_context *ctx)
115 {
116 /* If we already have fresh liveness, nothing to do */
117 if (ctx->metadata & MIDGARD_METADATA_LIVENESS)
118 return;
119
120 mir_compute_temp_count(ctx);
121
122 /* List of midgard_block */
123 struct set *work_list = _mesa_set_create(ctx,
124 _mesa_hash_pointer,
125 _mesa_key_pointer_equal);
126
127 /* Allocate */
128
129 mir_foreach_block(ctx, block) {
130 block->live_in = calloc(ctx->temp_count, 1);
131 block->live_out = calloc(ctx->temp_count, 1);
132 }
133
134 /* Initialize the work list with the exit block */
135 struct set_entry *cur;
136
137 midgard_block *exit = mir_exit_block(ctx);
138 cur = _mesa_set_add(work_list, exit);
139
140 /* Iterate the work list */
141
142 do {
143 /* Pop off a block */
144 midgard_block *blk = (struct midgard_block *) cur->key;
145 _mesa_set_remove(work_list, cur);
146
147 /* Update its liveness information */
148 bool progress = liveness_block_update(ctx, blk);
149
150 /* If we made progress, we need to process the predecessors */
151
152 if (progress || (blk == exit)) {
153 mir_foreach_predecessor(blk, pred)
154 _mesa_set_add(work_list, pred);
155 }
156 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
157
158 /* Liveness is now valid */
159 ctx->metadata |= MIDGARD_METADATA_LIVENESS;
160 }
161
162 /* Once liveness data is no longer valid, call this */
163
164 void
165 mir_invalidate_liveness(compiler_context *ctx)
166 {
167 /* If we didn't already compute liveness, there's nothing to do */
168 if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
169 return;
170
171 /* It's now invalid regardless */
172 ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
173
174 mir_foreach_block(ctx, block) {
175 if (block->live_in)
176 free(block->live_in);
177
178 if (block->live_out)
179 free(block->live_out);
180
181 block->live_in = NULL;
182 block->live_out = NULL;
183 }
184 }
185
186 /* Determine if a variable is live in the successors of a block */
187 static bool
188 is_live_after_successors(compiler_context *ctx, midgard_block *bl, int src)
189 {
190 for (unsigned i = 0; i < bl->nr_successors; ++i) {
191 midgard_block *succ = bl->successors[i];
192
193 /* If we already visited, the value we're seeking
194 * isn't down this path (or we would have short
195 * circuited */
196
197 if (succ->visited) continue;
198
199 /* Otherwise (it's visited *now*), check the block */
200
201 succ->visited = true;
202
203 /* Within this block, check if it's overwritten first */
204 unsigned overwritten_mask = 0;
205
206 mir_foreach_instr_in_block(succ, ins) {
207 /* Did we read any components that we haven't overwritten yet? */
208 if (mir_mask_of_read_components(ins, src) & ~overwritten_mask)
209 return true;
210
211 /* If written-before-use, we're gone */
212
213 if (ins->dest == src)
214 overwritten_mask |= ins->mask;
215 }
216
217 /* ...and also, check *its* successors */
218 if (is_live_after_successors(ctx, succ, src))
219 return true;
220
221 }
222
223 /* Welp. We're really not live. */
224
225 return false;
226 }
227
228 bool
229 mir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src)
230 {
231 assert(ctx->metadata & MIDGARD_METADATA_LIVENESS);
232
233 /* Check the rest of the block for liveness */
234
235 mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
236 if (mir_has_arg(ins, src))
237 return true;
238 }
239
240 /* Check the rest of the blocks for liveness recursively */
241
242 bool succ = is_live_after_successors(ctx, block, src);
243
244 mir_foreach_block(ctx, block) {
245 block->visited = false;
246 }
247
248 return succ;
249 }