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