Path: blob/21.2-virgl/src/asahi/compiler/agx_liveness.c
4564 views
/*1* Copyright (C) 2021 Alyssa Rosenzweig2* Copyright (C) 2019-2020 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 THE21* SOFTWARE.22*/2324#include "agx_compiler.h"25#include "util/u_memory.h"26#include "util/list.h"27#include "util/set.h"2829/* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,30* we compute live_out from live_in. The intrablock pass is linear-time. It31* returns whether progress was made. */3233/* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */3435void36agx_liveness_ins_update(BITSET_WORD *live, agx_instr *I)37{38agx_foreach_dest(I, d) {39if (I->dest[d].type == AGX_INDEX_NORMAL)40BITSET_CLEAR(live, I->dest[d].value);41}4243agx_foreach_src(I, s) {44if (I->src[s].type == AGX_INDEX_NORMAL) {45/* If the source is not live after this instruction, but becomes live46* at this instruction, this is the use that kills the source */47I->src[s].kill = !BITSET_TEST(live, I->src[s].value);48BITSET_SET(live, I->src[s].value);49}50}51}5253static bool54liveness_block_update(agx_block *blk, unsigned words)55{56bool progress = false;5758/* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */59agx_foreach_successor(blk, succ) {60for (unsigned i = 0; i < words; ++i)61blk->live_out[i] |= succ->live_in[i];62}6364/* live_in is live_out after iteration */65BITSET_WORD *live = ralloc_array(blk, BITSET_WORD, words);66memcpy(live, blk->live_out, words * sizeof(BITSET_WORD));6768agx_foreach_instr_in_block_rev(blk, I)69agx_liveness_ins_update(live, I);7071/* To figure out progress, diff live_in */72for (unsigned i = 0; i < words; ++i)73progress |= (blk->live_in[i] != live[i]);7475ralloc_free(blk->live_in);76blk->live_in = live;7778return progress;79}8081/* Globally, liveness analysis uses a fixed-point algorithm based on a82* worklist. We initialize a work list with the exit block. We iterate the work83* list to compute live_in from live_out for each block on the work list,84* adding the predecessors of the block to the work list if we made progress.85*/8687void88agx_compute_liveness(agx_context *ctx)89{90if (ctx->has_liveness)91return;9293/* Set of agx_block */94struct set *work_list = _mesa_set_create(NULL, _mesa_hash_pointer,95_mesa_key_pointer_equal);9697/* Free any previous liveness, and allocate */98unsigned words = BITSET_WORDS(ctx->alloc);99100agx_foreach_block(ctx, block) {101if (block->live_in)102ralloc_free(block->live_in);103104if (block->live_out)105ralloc_free(block->live_out);106107block->pass_flags = false;108block->live_in = rzalloc_array(block, BITSET_WORD, words);109block->live_out = rzalloc_array(block, BITSET_WORD, words);110}111112/* Initialize the work list with the exit block */113struct set_entry *cur = _mesa_set_add(work_list, agx_exit_block(ctx));114115/* Iterate the work list */116do {117/* Pop off a block */118agx_block *blk = (struct agx_block *) cur->key;119_mesa_set_remove(work_list, cur);120121/* Update its liveness information */122bool progress = liveness_block_update(blk, words);123124/* If we made progress, we need to process the predecessors */125126if (progress || !blk->pass_flags) {127agx_foreach_predecessor(blk, pred)128_mesa_set_add(work_list, pred);129}130131/* Use pass flags to communicate that we've visited this block */132blk->pass_flags = true;133} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);134135_mesa_set_destroy(work_list, NULL);136137ctx->has_liveness = true;138}139140141