Path: blob/21.2-virgl/src/freedreno/ir3/ir3_liveness.c
4565 views
/*1* Copyright (C) 2021 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "ir3_ra.h"24#include "ir3_shader.h"25#include "ralloc.h"2627/* A note on how phi node uses are handled:28*29* - Phi node sources are considered to happen after the end of the30* predecessor block, so the live_out for that block contains phi sources.31* - On the other hand, phi destinations are considered to happen at the start32* of the block, so that live_in does *not* contain phi destinations. This33* is mainly because phi destinations and live-through values have to be34* treated very differently by RA at the beginning of a block.35*/3637static bool38compute_block_liveness(struct ir3_liveness *live, struct ir3_block *block,39BITSET_WORD *tmp_live, unsigned bitset_words)40{41memcpy(tmp_live, live->live_out[block->index],42bitset_words * sizeof(BITSET_WORD));4344/* Process instructions */45foreach_instr_rev (instr, &block->instr_list) {46ra_foreach_dst (dst, instr) {47if (BITSET_TEST(tmp_live, dst->name))48dst->flags &= ~IR3_REG_UNUSED;49else50dst->flags |= IR3_REG_UNUSED;51BITSET_CLEAR(tmp_live, dst->name);52}5354/* Phi node uses occur after the predecessor block */55if (instr->opc != OPC_META_PHI) {56ra_foreach_src (src, instr) {57if (BITSET_TEST(tmp_live, src->def->name))58src->flags &= ~IR3_REG_KILL;59else60src->flags |= IR3_REG_KILL;61}6263ra_foreach_src (src, instr) {64if (BITSET_TEST(tmp_live, src->def->name))65src->flags &= ~IR3_REG_FIRST_KILL;66else67src->flags |= IR3_REG_FIRST_KILL;68BITSET_SET(tmp_live, src->def->name);69}70}71}7273memcpy(live->live_in[block->index], tmp_live,74bitset_words * sizeof(BITSET_WORD));7576bool progress = false;77for (unsigned i = 0; i < block->predecessors_count; i++) {78const struct ir3_block *pred = block->predecessors[i];79for (unsigned j = 0; j < bitset_words; j++) {80if (tmp_live[j] & ~live->live_out[pred->index][j])81progress = true;82live->live_out[pred->index][j] |= tmp_live[j];83}8485/* Process phi sources. */86foreach_instr (phi, &block->instr_list) {87if (phi->opc != OPC_META_PHI)88break;89if (!phi->srcs[i]->def)90continue;91unsigned name = phi->srcs[i]->def->name;92if (!BITSET_TEST(live->live_out[pred->index], name)) {93progress = true;94BITSET_SET(live->live_out[pred->index], name);95}96}97}9899for (unsigned i = 0; i < block->physical_predecessors_count; i++) {100const struct ir3_block *pred = block->physical_predecessors[i];101unsigned name;102BITSET_FOREACH_SET (name, tmp_live, live->definitions_count) {103struct ir3_register *reg = live->definitions[name];104if (!(reg->flags & IR3_REG_SHARED))105continue;106if (!BITSET_TEST(live->live_out[pred->index], name)) {107progress = true;108BITSET_SET(live->live_out[pred->index], name);109}110}111}112113return progress;114}115116struct ir3_liveness *117ir3_calc_liveness(struct ir3_shader_variant *v)118{119struct ir3_liveness *live = rzalloc(NULL, struct ir3_liveness);120121/* Reserve name 0 to mean "doesn't have a name yet" to make the debug122* output nicer.123*/124array_insert(live, live->definitions, NULL);125126/* Build definition <-> name mapping */127unsigned block_count = 0;128foreach_block (block, &v->ir->block_list) {129block->index = block_count++;130foreach_instr (instr, &block->instr_list) {131ra_foreach_dst (dst, instr) {132dst->name = live->definitions_count;133array_insert(live, live->definitions, dst);134}135}136}137138live->block_count = block_count;139140unsigned bitset_words = BITSET_WORDS(live->definitions_count);141BITSET_WORD *tmp_live = ralloc_array(live, BITSET_WORD, bitset_words);142live->live_in = ralloc_array(live, BITSET_WORD *, block_count);143live->live_out = ralloc_array(live, BITSET_WORD *, block_count);144unsigned i = 0;145foreach_block (block, &v->ir->block_list) {146block->index = i++;147live->live_in[block->index] =148rzalloc_array(live, BITSET_WORD, bitset_words);149live->live_out[block->index] =150rzalloc_array(live, BITSET_WORD, bitset_words);151}152153bool progress = true;154while (progress) {155progress = false;156foreach_block_rev (block, &v->ir->block_list) {157progress |=158compute_block_liveness(live, block, tmp_live, bitset_words);159}160}161162return live;163}164165/* Return true if "def" is live after "instr". It's assumed that "def"166* dominates "instr".167*/168bool169ir3_def_live_after(struct ir3_liveness *live, struct ir3_register *def,170struct ir3_instruction *instr)171{172/* If it's live out then it's definitely live at the instruction. */173if (BITSET_TEST(live->live_out[instr->block->index], def->name))174return true;175176/* If it's not live in and not defined in the same block then the live177* range can't extend to the instruction.178*/179if (def->instr->block != instr->block &&180!BITSET_TEST(live->live_in[instr->block->index], def->name))181return false;182183/* Ok, now comes the tricky case, where "def" is killed somewhere in184* "instr"'s block and we have to check if it's before or after.185*/186foreach_instr_rev (test_instr, &instr->block->instr_list) {187if (test_instr == instr)188break;189190for (unsigned i = 0; i < test_instr->srcs_count; i++) {191if (test_instr->srcs[i]->def == def)192return true;193}194}195196return false;197}198199200