Debian Codegraph @

src/asahi/compiler/agx_liveness.c

raw
1 /*
2 * Copyright 2021 Alyssa Rosenzweig
3 * Copyright 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "util/sparse_bitset.h"
8 #include "agx_compiler.h"
9
10 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
11 * we compute live_out from live_in. The intrablock pass is linear-time. It
12 * returns whether progress was made. */
13
14 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
15
16 void
17 agx_liveness_ins_update(struct u_sparse_bitset *live, agx_instr *I)
18 {
19 agx_foreach_ssa_dest(I, d)
20 u_sparse_bitset_clear(live, I->dest[d].value);
21
22 agx_foreach_ssa_src(I, s) {
23 /* If the source is not live after this instruction, but becomes live
24 * at this instruction, this is the use that kills the source
25 */
26 I->src[s].kill = !u_sparse_bitset_test(live, I->src[s].value);
27 u_sparse_bitset_set(live, I->src[s].value);
28 }
29 }
30
31 /* Globally, liveness analysis uses a fixed-point algorithm based on a
32 * worklist. We initialize a work list with the exit block. We iterate the work
33 * list to compute live_in from live_out for each block on the work list,
34 * adding the predecessors of the block to the work list if we made progress.
35 */
36
37 void
38 agx_compute_liveness(agx_context *ctx)
39 {
40 u_worklist worklist;
41 u_worklist_init(&worklist, ctx->num_blocks, NULL);
42
43 /* Free any previous liveness, and allocate */
44 agx_foreach_block(ctx, block) {
45 u_sparse_bitset_free(&block->live_in);
46 u_sparse_bitset_free(&block->live_out);
47
48 u_sparse_bitset_init(&block->live_in, ctx->alloc, block);
49 u_sparse_bitset_init(&block->live_out, ctx->alloc, block);
50
51 agx_worklist_push_head(&worklist, block);
52 }
53
54 /* Iterate the work list */
55 while (!u_worklist_is_empty(&worklist)) {
56 /* Pop in reverse order since liveness is a backwards pass */
57 agx_block *blk = agx_worklist_pop_head(&worklist);
58
59 /* Update its liveness information */
60 u_sparse_bitset_dup(&blk->live_in, &blk->live_out);
61
62 agx_foreach_instr_in_block_rev(blk, I) {
63 if (I->op != AGX_OPCODE_PHI)
64 agx_liveness_ins_update(&blk->live_in, I);
65 }
66
67 /* Propagate the live in of the successor (blk) to the live out of
68 * predecessors.
69 *
70 * Phi nodes are logically on the control flow edge and act in parallel.
71 * To handle when propagating, we kill writes from phis and make live the
72 * corresponding sources.
73 */
74 agx_foreach_predecessor(blk, pred) {
75 struct u_sparse_bitset live;
76 u_sparse_bitset_dup(&live, &blk->live_in);
77
78 /* Kill write */
79 agx_foreach_phi_in_block(blk, phi) {
80 assert(phi->dest[0].type == AGX_INDEX_NORMAL);
81 u_sparse_bitset_clear(&live, phi->dest[0].value);
82 }
83
84 /* Make live the corresponding source */
85 agx_foreach_phi_in_block(blk, phi) {
86 agx_index operand = phi->src[agx_predecessor_index(blk, *pred)];
87 if (operand.type == AGX_INDEX_NORMAL) {
88 u_sparse_bitset_set(&live, operand.value);
89 phi->src[agx_predecessor_index(blk, *pred)].kill = false;
90 }
91 }
92
93 if (u_sparse_bitset_merge(&(*pred)->live_out, &live))
94 agx_worklist_push_tail(&worklist, *pred);
95 }
96 }
97
98 u_worklist_fini(&worklist);
99 }