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, (bi_instr *) 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         if (ctx->has_liveness)
95                 return;
96 
97         unsigned temp_count = bi_max_temp(ctx);
98 
99         /* Set of bi_block */
100         struct set *work_list = _mesa_set_create(NULL,
101                         _mesa_hash_pointer,
102                         _mesa_key_pointer_equal);
103 
104         struct set *visited = _mesa_set_create(NULL,
105                         _mesa_hash_pointer,
106                         _mesa_key_pointer_equal);
107 
108         list_for_each_entry(bi_block, block, &ctx->blocks, link) {
109                 if (block->live_in)
110                         ralloc_free(block->live_in);
111 
112                 if (block->live_out)
113                         ralloc_free(block->live_out);
114 
115                 block->live_in = rzalloc_array(block, uint8_t, temp_count);
116                 block->live_out = rzalloc_array(block, uint8_t, temp_count);
117         }
118 
119         /* Initialize the work list with the exit block */
120         struct set_entry *cur;
121 
122         cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks));
123 
124         /* Iterate the work list */
125 
126         do {
127                 /* Pop off a block */
128                 bi_block *blk = (struct bi_block *) cur->key;
129                 _mesa_set_remove(work_list, cur);
130 
131                 /* Update its liveness information */
132                 bool progress = liveness_block_update(blk, temp_count);
133 
134                 /* If we made progress, we need to process the predecessors */
135 
136                 if (progress || !_mesa_set_search(visited, blk)) {
137                         bi_foreach_predecessor(blk, pred)
138                                 _mesa_set_add(work_list, pred);
139                 }
140 
141                 _mesa_set_add(visited, blk);
142         } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
143 
144         _mesa_set_destroy(visited, NULL);
145         _mesa_set_destroy(work_list, NULL);
146 
147         ctx->has_liveness = true;
148 }
149 
150 /* Once liveness data is no longer valid, call this */
151 
152 void
bi_invalidate_liveness(bi_context * ctx)153 bi_invalidate_liveness(bi_context *ctx)
154 {
155         ctx->has_liveness = false;
156 }
157