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