2 * Copyright (C) 2019-2020 Collabora, Ltd.
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:
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
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
25 #include "util/u_memory.h"
26 #include "util/list.h"
29 /* Routines for liveness analysis. Liveness is tracked per byte per node. Per
30 * byte granularity is necessary for proper handling of int8 */
33 pan_liveness_gen(uint16_t *live
, unsigned node
, unsigned max
, uint16_t mask
)
42 pan_liveness_kill(uint16_t *live
, unsigned node
, unsigned max
, uint16_t mask
)
51 pan_liveness_get(uint16_t *live
, unsigned node
, uint16_t max
)
59 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
62 liveness_block_live_out(pan_block
*blk
, unsigned temp_count
)
64 pan_foreach_successor(blk
, succ
) {
65 for (unsigned i
= 0; i
< temp_count
; ++i
)
66 blk
->live_out
[i
] |= succ
->live_in
[i
];
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. */
75 liveness_block_update(
76 pan_block
*blk
, unsigned temp_count
,
77 pan_liveness_update callback
)
79 bool progress
= false;
81 liveness_block_live_out(blk
, temp_count
);
83 uint16_t *live
= ralloc_array(blk
, uint16_t, temp_count
);
84 memcpy(live
, blk
->live_out
, temp_count
* sizeof(uint16_t));
86 pan_foreach_instr_in_block_rev(blk
, ins
)
87 callback(live
, (void *) ins
, temp_count
);
89 /* To figure out progress, diff live_in */
91 for (unsigned i
= 0; (i
< temp_count
) && !progress
; ++i
)
92 progress
|= (blk
->live_in
[i
] != live
[i
]);
94 ralloc_free(blk
->live_in
);
101 /* Globally, liveness analysis uses a fixed-point algorithm based on a
102 * worklist. We initialize a work list with the exit block. We iterate the work
103 * list to compute live_in from live_out for each block on the work list,
104 * adding the predecessors of the block to the work list if we made progress.
107 static inline pan_block
*
108 pan_exit_block(struct list_head
*blocks
)
110 pan_block
*last
= list_last_entry(blocks
, pan_block
, link
);
111 assert(last
->nr_successors
== 0);
116 pan_compute_liveness(
117 struct list_head
*blocks
,
119 pan_liveness_update callback
)
122 /* Set of pan_block */
123 struct set
*work_list
= _mesa_set_create(NULL
,
125 _mesa_key_pointer_equal
);
127 struct set
*visited
= _mesa_set_create(NULL
,
129 _mesa_key_pointer_equal
);
133 list_for_each_entry(pan_block
, block
, blocks
, link
) {
134 block
->live_in
= rzalloc_array(NULL
, uint16_t, temp_count
);
135 block
->live_out
= rzalloc_array(NULL
, uint16_t, temp_count
);
138 /* Initialize the work list with the exit block */
139 struct set_entry
*cur
;
141 cur
= _mesa_set_add(work_list
, pan_exit_block(blocks
));
143 /* Iterate the work list */
146 /* Pop off a block */
147 pan_block
*blk
= (struct pan_block
*) cur
->key
;
148 _mesa_set_remove(work_list
, cur
);
150 /* Update its liveness information */
151 bool progress
= liveness_block_update(blk
, temp_count
, callback
);
153 /* If we made progress, we need to process the predecessors */
155 if (progress
|| !_mesa_set_search(visited
, blk
)) {
156 pan_foreach_predecessor(blk
, pred
)
157 _mesa_set_add(work_list
, pred
);
160 _mesa_set_add(visited
, blk
);
161 } while((cur
= _mesa_set_next_entry(work_list
, NULL
)) != NULL
);
163 _mesa_set_destroy(visited
, NULL
);
164 _mesa_set_destroy(work_list
, NULL
);
168 pan_free_liveness(struct list_head
*blocks
)
170 list_for_each_entry(pan_block
, block
, blocks
, link
) {
172 ralloc_free(block
->live_in
);
175 ralloc_free(block
->live_out
);
177 block
->live_in
= NULL
;
178 block
->live_out
= NULL
;