2
* Copyright (C) 2020 Collabora, Ltd.
3
* Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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:
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
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
26
#include "util/u_memory.h"
27
#include "util/list.h"
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. */
35
bi_liveness_ins_update(uint8_t *live, bi_instr *ins, unsigned max)
37
/* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
39
bi_foreach_dest(ins, d) {
40
unsigned node = bi_get_node(ins->dest[d]);
43
live[node] &= ~bi_writemask(ins, d);
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);
51
unsigned node = bi_get_node(ins->src[src]);
58
liveness_block_update(bi_block *blk, unsigned temp_count)
60
bool progress = false;
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];
68
uint8_t *live = ralloc_array(blk, uint8_t, temp_count);
69
memcpy(live, blk->live_out, temp_count);
71
bi_foreach_instr_in_block_rev(blk, ins)
72
bi_liveness_ins_update(live, ins, temp_count);
74
/* To figure out progress, diff live_in */
76
for (unsigned i = 0; (i < temp_count) && !progress; ++i)
77
progress |= (blk->live_in[i] != live[i]);
79
ralloc_free(blk->live_in);
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.
92
bi_compute_liveness(bi_context *ctx)
94
if (ctx->has_liveness)
97
unsigned temp_count = bi_max_temp(ctx);
100
struct set *work_list = _mesa_set_create(NULL,
102
_mesa_key_pointer_equal);
104
struct set *visited = _mesa_set_create(NULL,
106
_mesa_key_pointer_equal);
108
bi_foreach_block(ctx, block) {
110
ralloc_free(block->live_in);
113
ralloc_free(block->live_out);
115
block->live_in = rzalloc_array(block, uint8_t, temp_count);
116
block->live_out = rzalloc_array(block, uint8_t, temp_count);
119
/* Initialize the work list with the exit block */
120
struct set_entry *cur;
122
cur = _mesa_set_add(work_list, bi_exit_block(&ctx->blocks));
124
/* Iterate the work list */
127
/* Pop off a block */
128
bi_block *blk = (struct bi_block *) cur->key;
129
_mesa_set_remove(work_list, cur);
131
/* Update its liveness information */
132
bool progress = liveness_block_update(blk, temp_count);
134
/* If we made progress, we need to process the predecessors */
136
if (progress || !_mesa_set_search(visited, blk)) {
137
bi_foreach_predecessor(blk, pred)
138
_mesa_set_add(work_list, pred);
141
_mesa_set_add(visited, blk);
142
} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
144
_mesa_set_destroy(visited, NULL);
145
_mesa_set_destroy(work_list, NULL);
147
ctx->has_liveness = true;
150
/* Once liveness data is no longer valid, call this */
153
bi_invalidate_liveness(bi_context *ctx)
155
ctx->has_liveness = false;