1 /*
2 * Copyright (C) 2020 Collabora, Ltd.
3 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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 "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 void
bi_liveness_ins_update(uint8_t * live,bi_instr * ins,unsigned max)35 bi_liveness_ins_update(uint8_t *live, bi_instr *ins, unsigned max)
36 {
37 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
38
39 bi_foreach_dest(ins, d) {
40 unsigned node = bi_get_node(ins->dest[d]);
41
42 if (node < max)
43 live[node] &= ~bi_writemask(ins, d);
44 }
45
46 bi_foreach_src(ins, src) {
47 unsigned count = bi_count_read_registers(ins, src);
48 unsigned rmask = BITFIELD_MASK(count);
49 uint8_t mask = (rmask << ins->src[src].offset);
50
51 unsigned node = bi_get_node(ins->src[src]);
52 if (node < max)
53 live[node] |= mask;
54 }
55 }
56
57 static bool
liveness_block_update(bi_block * blk,unsigned temp_count)58 liveness_block_update(bi_block *blk, unsigned temp_count)
59 {
60 bool progress = false;
61
62 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
63 bi_foreach_successor(blk, succ) {
64 for (unsigned i = 0; i < temp_count; ++i)
65 blk->live_out[i] |= succ->live_in[i];
66 }
67
68 uint8_t *live = ralloc_array(blk, uint8_t, temp_count);
69 memcpy(live, blk->live_out, temp_count);
70
71 bi_foreach_instr_in_block_rev(blk, ins)
72 bi_liveness_ins_update(live, ins, temp_count);
73
74 /* To figure out progress, diff live_in */
75
76 for (unsigned i = 0; (i < temp_count) && !progress; ++i)
77 progress |= (blk->live_in[i] != live[i]);
78
79 ralloc_free(blk->live_in);
80 blk->live_in = live;
81
82 return progress;
83 }
84
85 /* Globally, liveness analysis uses a fixed-point algorithm based on a
86 * worklist. We initialize a work list with the exit block. We iterate the work
87 * list to compute live_in from live_out for each block on the work list,
88 * adding the predecessors of the block to the work list if we made progress.
89 */
90
91 void
bi_compute_liveness(bi_context * ctx)92 bi_compute_liveness(bi_context *ctx)
93 {
94 unsigned temp_count = bi_max_temp(ctx);
95
96 u_worklist worklist;
97 bi_worklist_init(ctx, &worklist);
98
99 bi_foreach_block(ctx, block) {
100 if (block->live_in)
101 ralloc_free(block->live_in);
102
103 if (block->live_out)
104 ralloc_free(block->live_out);
105
106 block->live_in = rzalloc_array(block, uint8_t, temp_count);
107 block->live_out = rzalloc_array(block, uint8_t, temp_count);
108
109 bi_worklist_push_tail(&worklist, block);
110 }
111
112 while (!u_worklist_is_empty(&worklist)) {
113 /* Pop off in reverse order since liveness is backwards */
114 bi_block *blk = bi_worklist_pop_tail(&worklist);
115
116 /* Update liveness information. If we made progress, we need to
117 * reprocess the predecessors
118 */
119 if (liveness_block_update(blk, temp_count)) {
120 bi_foreach_predecessor(blk, pred)
121 bi_worklist_push_head(&worklist, *pred);
122 }
123 }
124
125 u_worklist_fini(&worklist);
126 }
127