1 /*
2 * Copyright (C) 2019-2020 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "util/list.h"
25 #include "util/set.h"
26 #include "util/u_memory.h"
27 #include "pan_ir.h"
28
29 /* Routines for liveness analysis. Liveness is tracked per byte per node. Per
30 * byte granularity is necessary for proper handling of int8 */
31
32 void
pan_liveness_gen(uint16_t * live,unsigned node,unsigned max,uint16_t mask)33 pan_liveness_gen(uint16_t *live, unsigned node, unsigned max, uint16_t mask)
34 {
35 if (node >= max)
36 return;
37
38 live[node] |= mask;
39 }
40
41 void
pan_liveness_kill(uint16_t * live,unsigned node,unsigned max,uint16_t mask)42 pan_liveness_kill(uint16_t *live, unsigned node, unsigned max, uint16_t mask)
43 {
44 if (node >= max)
45 return;
46
47 live[node] &= ~mask;
48 }
49
50 bool
pan_liveness_get(uint16_t * live,unsigned node,uint16_t max)51 pan_liveness_get(uint16_t *live, unsigned node, uint16_t max)
52 {
53 if (node >= max)
54 return false;
55
56 return live[node];
57 }
58
59 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
60
61 static void
liveness_block_live_out(pan_block * blk,unsigned temp_count)62 liveness_block_live_out(pan_block *blk, unsigned temp_count)
63 {
64 pan_foreach_successor(blk, succ) {
65 for (unsigned i = 0; i < temp_count; ++i)
66 blk->live_out[i] |= succ->live_in[i];
67 }
68 }
69
70 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
71 * we compute live_out from live_in. The intrablock pass is linear-time. It
72 * returns whether progress was made. */
73
74 static bool
liveness_block_update(pan_block * blk,unsigned temp_count,pan_liveness_update callback)75 liveness_block_update(pan_block *blk, unsigned temp_count,
76 pan_liveness_update callback)
77 {
78 bool progress = false;
79
80 liveness_block_live_out(blk, temp_count);
81
82 uint16_t *live = ralloc_array(blk, uint16_t, temp_count);
83 memcpy(live, blk->live_out, temp_count * sizeof(uint16_t));
84
85 pan_foreach_instr_in_block_rev(blk, ins)
86 callback(live, (void *)ins, temp_count);
87
88 /* To figure out progress, diff live_in */
89
90 for (unsigned i = 0; (i < temp_count) && !progress; ++i)
91 progress |= (blk->live_in[i] != live[i]);
92
93 ralloc_free(blk->live_in);
94 blk->live_in = live;
95
96 return progress;
97 }
98
99 /* Globally, liveness analysis uses a fixed-point algorithm based on a
100 * worklist. We initialize a work list with the exit block. We iterate the work
101 * list to compute live_in from live_out for each block on the work list,
102 * adding the predecessors of the block to the work list if we made progress.
103 */
104
105 void
pan_compute_liveness(struct list_head * blocks,unsigned temp_count,pan_liveness_update callback)106 pan_compute_liveness(struct list_head *blocks, unsigned temp_count,
107 pan_liveness_update callback)
108 {
109
110 /* Set of pan_block */
111 struct set *work_list =
112 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
113
114 struct set *visited =
115 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
116
117 /* Free any previous liveness, and allocate */
118
119 pan_free_liveness(blocks);
120
121 list_for_each_entry(pan_block, block, blocks, link) {
122 block->live_in = rzalloc_array(block, uint16_t, temp_count);
123 block->live_out = rzalloc_array(block, uint16_t, temp_count);
124 }
125
126 /* Initialize the work list with the exit block */
127 struct set_entry *cur;
128
129 cur = _mesa_set_add(work_list, pan_exit_block(blocks));
130
131 /* Iterate the work list */
132
133 do {
134 /* Pop off a block */
135 pan_block *blk = (struct pan_block *)cur->key;
136 _mesa_set_remove(work_list, cur);
137
138 /* Update its liveness information */
139 bool progress = liveness_block_update(blk, temp_count, callback);
140
141 /* If we made progress, we need to process the predecessors */
142
143 if (progress || !_mesa_set_search(visited, blk)) {
144 pan_foreach_predecessor(blk, pred)
145 _mesa_set_add(work_list, pred);
146 }
147
148 _mesa_set_add(visited, blk);
149 } while ((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
150
151 _mesa_set_destroy(visited, NULL);
152 _mesa_set_destroy(work_list, NULL);
153 }
154
155 void
pan_free_liveness(struct list_head * blocks)156 pan_free_liveness(struct list_head *blocks)
157 {
158 list_for_each_entry(pan_block, block, blocks, link) {
159 if (block->live_in)
160 ralloc_free(block->live_in);
161
162 if (block->live_out)
163 ralloc_free(block->live_out);
164
165 block->live_in = NULL;
166 block->live_out = NULL;
167 }
168 }
169