pan/midgard: Replace mir_is_live_after with new pass
[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 #include "compiler.h"
25 #include "util/u_memory.h"
26
27 /* Routines for liveness analysis */
28
29 static void
30 liveness_gen(uint8_t *live, unsigned node, unsigned max, unsigned mask)
31 {
32 if (node >= max)
33 return;
34
35 live[node] |= mask;
36 }
37
38 static void
39 liveness_kill(uint8_t *live, unsigned node, unsigned max, unsigned mask)
40 {
41 if (node >= max)
42 return;
43
44 live[node] &= ~mask;
45 }
46
47 static bool
48 liveness_get(uint8_t *live, unsigned node, unsigned max) {
49 if (node >= max)
50 return false;
51
52 return live[node];
53 }
54
55 /* Updates live_in for a single instruction */
56
57 void
58 mir_liveness_ins_update(uint8_t *live, midgard_instruction *ins, unsigned max)
59 {
60 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
61
62 liveness_kill(live, ins->dest, max, ins->mask);
63
64 mir_foreach_src(ins, src) {
65 unsigned node = ins->src[src];
66 unsigned mask = mir_mask_of_read_components(ins, node);
67
68 liveness_gen(live, node, max, mask);
69 }
70 }
71
72 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
73
74 static void
75 liveness_block_live_out(compiler_context *ctx, midgard_block *blk)
76 {
77 mir_foreach_successor(blk, succ) {
78 for (unsigned i = 0; i < ctx->temp_count; ++i)
79 blk->live_out[i] |= succ->live_in[i];
80 }
81 }
82
83 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
84 * we compute live_out from live_in. The intrablock pass is linear-time. It
85 * returns whether progress was made. */
86
87 static bool
88 liveness_block_update(compiler_context *ctx, midgard_block *blk)
89 {
90 bool progress = false;
91
92 liveness_block_live_out(ctx, blk);
93
94 uint8_t *live = mem_dup(blk->live_out, ctx->temp_count);
95
96 mir_foreach_instr_in_block_rev(blk, ins)
97 mir_liveness_ins_update(live, ins, ctx->temp_count);
98
99 /* To figure out progress, diff live_in */
100
101 for (unsigned i = 0; (i < ctx->temp_count) && !progress; ++i)
102 progress |= (blk->live_in[i] != live[i]);
103
104 free(blk->live_in);
105 blk->live_in = live;
106
107 return progress;
108 }
109
110 /* Globally, liveness analysis uses a fixed-point algorithm based on a
111 * worklist. We initialize a work list with the exit block. We iterate the work
112 * list to compute live_in from live_out for each block on the work list,
113 * adding the predecessors of the block to the work list if we made progress.
114 */
115
116 void
117 mir_compute_liveness(compiler_context *ctx)
118 {
119 /* If we already have fresh liveness, nothing to do */
120 if (ctx->metadata & MIDGARD_METADATA_LIVENESS)
121 return;
122
123 mir_compute_temp_count(ctx);
124
125 /* List of midgard_block */
126 struct set *work_list = _mesa_set_create(ctx,
127 _mesa_hash_pointer,
128 _mesa_key_pointer_equal);
129
130 /* Allocate */
131
132 mir_foreach_block(ctx, block) {
133 block->live_in = calloc(ctx->temp_count, 1);
134 block->live_out = calloc(ctx->temp_count, 1);
135 }
136
137 /* Initialize the work list with the exit block */
138 struct set_entry *cur;
139
140 midgard_block *exit = mir_exit_block(ctx);
141 cur = _mesa_set_add(work_list, exit);
142
143 /* Iterate the work list */
144
145 do {
146 /* Pop off a block */
147 midgard_block *blk = (struct midgard_block *) cur->key;
148 _mesa_set_remove(work_list, cur);
149
150 /* Update its liveness information */
151 bool progress = liveness_block_update(ctx, blk);
152
153 /* If we made progress, we need to process the predecessors */
154
155 if (progress || (blk == exit)) {
156 mir_foreach_predecessor(blk, pred)
157 _mesa_set_add(work_list, pred);
158 }
159 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
160
161 /* Liveness is now valid */
162 ctx->metadata |= MIDGARD_METADATA_LIVENESS;
163 }
164
165 /* Once liveness data is no longer valid, call this */
166
167 void
168 mir_invalidate_liveness(compiler_context *ctx)
169 {
170 /* If we didn't already compute liveness, there's nothing to do */
171 if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
172 return;
173
174 /* It's now invalid regardless */
175 ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
176
177 mir_foreach_block(ctx, block) {
178 if (block->live_in)
179 free(block->live_in);
180
181 if (block->live_out)
182 free(block->live_out);
183
184 block->live_in = NULL;
185 block->live_out = NULL;
186 }
187 }
188
189 bool
190 mir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src)
191 {
192 mir_compute_liveness(ctx);
193
194 /* Check whether we're live in the successors */
195
196 if (liveness_get(block->live_out, src, ctx->temp_count))
197 return true;
198
199 /* Check the rest of the block for liveness */
200
201 mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
202 if (mir_has_arg(ins, src))
203 return true;
204 }
205
206 return false;
207 }