1 /*
2 * Copyright (C) 2021 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 "agx_compiler.h"
26 #include "util/u_memory.h"
27 #include "util/list.h"
28 #include "util/set.h"
29
30 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
31 * we compute live_out from live_in. The intrablock pass is linear-time. It
32 * returns whether progress was made. */
33
34 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
35
36 void
agx_liveness_ins_update(BITSET_WORD * live,agx_instr * I)37 agx_liveness_ins_update(BITSET_WORD *live, agx_instr *I)
38 {
39 agx_foreach_dest(I, d) {
40 if (I->dest[d].type == AGX_INDEX_NORMAL)
41 BITSET_CLEAR(live, I->dest[d].value);
42 }
43
44 agx_foreach_src(I, s) {
45 if (I->src[s].type == AGX_INDEX_NORMAL) {
46 /* If the source is not live after this instruction, but becomes live
47 * at this instruction, this is the use that kills the source */
48 I->src[s].kill = !BITSET_TEST(live, I->src[s].value);
49 BITSET_SET(live, I->src[s].value);
50 }
51 }
52 }
53
54 static bool
liveness_block_update(agx_block * blk,unsigned words)55 liveness_block_update(agx_block *blk, unsigned words)
56 {
57 bool progress = false;
58
59 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
60 agx_foreach_successor(blk, succ) {
61 for (unsigned i = 0; i < words; ++i)
62 blk->live_out[i] |= succ->live_in[i];
63 }
64
65 /* live_in is live_out after iteration */
66 BITSET_WORD *live = ralloc_array(blk, BITSET_WORD, words);
67 memcpy(live, blk->live_out, words * sizeof(BITSET_WORD));
68
69 agx_foreach_instr_in_block_rev(blk, I)
70 agx_liveness_ins_update(live, I);
71
72 /* To figure out progress, diff live_in */
73 for (unsigned i = 0; i < words; ++i)
74 progress |= (blk->live_in[i] != live[i]);
75
76 ralloc_free(blk->live_in);
77 blk->live_in = live;
78
79 return progress;
80 }
81
82 /* Globally, liveness analysis uses a fixed-point algorithm based on a
83 * worklist. We initialize a work list with the exit block. We iterate the work
84 * list to compute live_in from live_out for each block on the work list,
85 * adding the predecessors of the block to the work list if we made progress.
86 */
87
88 void
agx_compute_liveness(agx_context * ctx)89 agx_compute_liveness(agx_context *ctx)
90 {
91 if (ctx->has_liveness)
92 return;
93
94 /* Set of agx_block */
95 struct set *work_list = _mesa_set_create(NULL, _mesa_hash_pointer,
96 _mesa_key_pointer_equal);
97
98 /* Free any previous liveness, and allocate */
99 unsigned words = BITSET_WORDS(ctx->alloc);
100
101 agx_foreach_block(ctx, block) {
102 if (block->live_in)
103 ralloc_free(block->live_in);
104
105 if (block->live_out)
106 ralloc_free(block->live_out);
107
108 block->pass_flags = false;
109 block->live_in = rzalloc_array(block, BITSET_WORD, words);
110 block->live_out = rzalloc_array(block, BITSET_WORD, words);
111 }
112
113 /* Initialize the work list with the exit block */
114 struct set_entry *cur = _mesa_set_add(work_list, agx_exit_block(ctx));
115
116 /* Iterate the work list */
117 do {
118 /* Pop off a block */
119 agx_block *blk = (struct agx_block *) cur->key;
120 _mesa_set_remove(work_list, cur);
121
122 /* Update its liveness information */
123 bool progress = liveness_block_update(blk, words);
124
125 /* If we made progress, we need to process the predecessors */
126
127 if (progress || !blk->pass_flags) {
128 agx_foreach_predecessor(blk, pred)
129 _mesa_set_add(work_list, pred);
130 }
131
132 /* Use pass flags to communicate that we've visited this block */
133 blk->pass_flags = true;
134 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
135
136 _mesa_set_destroy(work_list, NULL);
137
138 ctx->has_liveness = true;
139 }
140