2
* Copyright (C) 2021 Alyssa Rosenzweig
3
* Copyright (C) 2019-2020 Collabora, Ltd.
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
25
#include "agx_compiler.h"
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. */
34
/* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
37
agx_liveness_ins_update(BITSET_WORD *live, agx_instr *I)
39
agx_foreach_dest(I, d) {
40
if (I->dest[d].type == AGX_INDEX_NORMAL)
41
BITSET_CLEAR(live, I->dest[d].value);
44
agx_foreach_src(I, s) {
45
if (I->src[s].type == AGX_INDEX_NORMAL) {
46
/* If the source is not live after this instruction, but becomes live
47
* at this instruction, this is the use that kills the source */
48
I->src[s].kill = !BITSET_TEST(live, I->src[s].value);
49
BITSET_SET(live, I->src[s].value);
55
liveness_block_update(agx_block *blk, unsigned words)
57
bool progress = false;
59
/* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
60
agx_foreach_successor(blk, succ) {
61
for (unsigned i = 0; i < words; ++i)
62
blk->live_out[i] |= succ->live_in[i];
65
/* live_in is live_out after iteration */
66
BITSET_WORD *live = ralloc_array(blk, BITSET_WORD, words);
67
memcpy(live, blk->live_out, words * sizeof(BITSET_WORD));
69
agx_foreach_instr_in_block_rev(blk, I)
70
agx_liveness_ins_update(live, I);
72
/* To figure out progress, diff live_in */
73
for (unsigned i = 0; i < words; ++i)
74
progress |= (blk->live_in[i] != live[i]);
76
ralloc_free(blk->live_in);
82
/* Globally, liveness analysis uses a fixed-point algorithm based on a
83
* worklist. We initialize a work list with the exit block. We iterate the work
84
* list to compute live_in from live_out for each block on the work list,
85
* adding the predecessors of the block to the work list if we made progress.
89
agx_compute_liveness(agx_context *ctx)
91
if (ctx->has_liveness)
94
/* Set of agx_block */
95
struct set *work_list = _mesa_set_create(NULL, _mesa_hash_pointer,
96
_mesa_key_pointer_equal);
98
/* Free any previous liveness, and allocate */
99
unsigned words = BITSET_WORDS(ctx->alloc);
101
agx_foreach_block(ctx, block) {
103
ralloc_free(block->live_in);
106
ralloc_free(block->live_out);
108
block->pass_flags = false;
109
block->live_in = rzalloc_array(block, BITSET_WORD, words);
110
block->live_out = rzalloc_array(block, BITSET_WORD, words);
113
/* Initialize the work list with the exit block */
114
struct set_entry *cur = _mesa_set_add(work_list, agx_exit_block(ctx));
116
/* Iterate the work list */
118
/* Pop off a block */
119
agx_block *blk = (struct agx_block *) cur->key;
120
_mesa_set_remove(work_list, cur);
122
/* Update its liveness information */
123
bool progress = liveness_block_update(blk, words);
125
/* If we made progress, we need to process the predecessors */
127
if (progress || !blk->pass_flags) {
128
agx_foreach_predecessor(blk, pred)
129
_mesa_set_add(work_list, pred);
132
/* Use pass flags to communicate that we've visited this block */
133
blk->pass_flags = true;
134
} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
136
_mesa_set_destroy(work_list, NULL);
138
ctx->has_liveness = true;