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.
108 pan_compute_liveness(
109 struct list_head
*blocks
,
111 pan_liveness_update callback
)
114 /* Set of pan_block */
115 struct set
*work_list
= _mesa_set_create(NULL
,
117 _mesa_key_pointer_equal
);
119 struct set
*visited
= _mesa_set_create(NULL
,
121 _mesa_key_pointer_equal
);
125 list_for_each_entry(pan_block
, block
, blocks
, link
) {
126 block
->live_in
= rzalloc_array(NULL
, uint16_t, temp_count
);
127 block
->live_out
= rzalloc_array(NULL
, uint16_t, temp_count
);
130 /* Initialize the work list with the exit block */
131 struct set_entry
*cur
;
133 cur
= _mesa_set_add(work_list
, pan_exit_block(blocks
));
135 /* Iterate the work list */
138 /* Pop off a block */
139 pan_block
*blk
= (struct pan_block
*) cur
->key
;
140 _mesa_set_remove(work_list
, cur
);
142 /* Update its liveness information */
143 bool progress
= liveness_block_update(blk
, temp_count
, callback
);
145 /* If we made progress, we need to process the predecessors */
147 if (progress
|| !_mesa_set_search(visited
, blk
)) {
148 pan_foreach_predecessor(blk
, pred
)
149 _mesa_set_add(work_list
, pred
);
152 _mesa_set_add(visited
, blk
);
153 } while((cur
= _mesa_set_next_entry(work_list
, NULL
)) != NULL
);
155 _mesa_set_destroy(visited
, NULL
);
156 _mesa_set_destroy(work_list
, NULL
);
160 pan_free_liveness(struct list_head
*blocks
)
162 list_for_each_entry(pan_block
, block
, blocks
, link
) {
164 ralloc_free(block
->live_in
);
167 ralloc_free(block
->live_out
);
169 block
->live_in
= NULL
;
170 block
->live_out
= NULL
;