Path: blob/21.2-virgl/src/compiler/nir/nir_liveness.c
4546 views
/*1* Copyright © 2014 Intel 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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Jason Ekstrand ([email protected])24*/2526#include "nir.h"27#include "nir_worklist.h"28#include "nir_vla.h"2930/*31* Basic liveness analysis. This works only in SSA form.32*33* This liveness pass treats phi nodes as being melded to the space between34* blocks so that the destinations of a phi are in the livein of the block35* in which it resides and the sources are in the liveout of the36* corresponding block. By formulating the liveness information in this37* way, we ensure that the definition of any variable dominates its entire38* live range. This is true because the only way that the definition of an39* SSA value may not dominate a use is if the use is in a phi node and the40* uses in phi no are in the live-out of the corresponding predecessor41* block but not in the live-in of the block containing the phi node.42*/4344struct live_ssa_defs_state {45unsigned bitset_words;4647/* Used in propagate_across_edge() */48BITSET_WORD *tmp_live;4950nir_block_worklist worklist;51};5253/* Initialize the liveness data to zero and add the given block to the54* worklist.55*/56static void57init_liveness_block(nir_block *block,58struct live_ssa_defs_state *state)59{60block->live_in = reralloc(block, block->live_in, BITSET_WORD,61state->bitset_words);62memset(block->live_in, 0, state->bitset_words * sizeof(BITSET_WORD));6364block->live_out = reralloc(block, block->live_out, BITSET_WORD,65state->bitset_words);66memset(block->live_out, 0, state->bitset_words * sizeof(BITSET_WORD));6768nir_block_worklist_push_head(&state->worklist, block);69}7071static bool72set_src_live(nir_src *src, void *void_live)73{74BITSET_WORD *live = void_live;7576if (!src->is_ssa)77return true;7879if (nir_src_is_undef(*src))80return true; /* undefined variables are never live */8182BITSET_SET(live, src->ssa->index);8384return true;85}8687static bool88set_ssa_def_dead(nir_ssa_def *def, void *void_live)89{90BITSET_WORD *live = void_live;9192BITSET_CLEAR(live, def->index);9394return true;95}9697/** Propagates the live in of succ across the edge to the live out of pred98*99* Phi nodes exist "between" blocks and all the phi nodes at the start of a100* block act "in parallel". When we propagate from the live_in of one101* block to the live out of the other, we have to kill any writes from phis102* and make live any sources.103*104* Returns true if updating live out of pred added anything105*/106static bool107propagate_across_edge(nir_block *pred, nir_block *succ,108struct live_ssa_defs_state *state)109{110BITSET_WORD *live = state->tmp_live;111memcpy(live, succ->live_in, state->bitset_words * sizeof *live);112113nir_foreach_instr(instr, succ) {114if (instr->type != nir_instr_type_phi)115break;116nir_phi_instr *phi = nir_instr_as_phi(instr);117118assert(phi->dest.is_ssa);119set_ssa_def_dead(&phi->dest.ssa, live);120}121122nir_foreach_instr(instr, succ) {123if (instr->type != nir_instr_type_phi)124break;125nir_phi_instr *phi = nir_instr_as_phi(instr);126127nir_foreach_phi_src(src, phi) {128if (src->pred == pred) {129set_src_live(&src->src, live);130break;131}132}133}134135BITSET_WORD progress = 0;136for (unsigned i = 0; i < state->bitset_words; ++i) {137progress |= live[i] & ~pred->live_out[i];138pred->live_out[i] |= live[i];139}140return progress != 0;141}142143void144nir_live_ssa_defs_impl(nir_function_impl *impl)145{146struct live_ssa_defs_state state = {147.bitset_words = BITSET_WORDS(impl->ssa_alloc),148};149state.tmp_live = rzalloc_array(impl, BITSET_WORD, state.bitset_words),150151/* Number the instructions so we can do cheap interference tests using the152* instruction index.153*/154nir_metadata_require(impl, nir_metadata_instr_index);155156nir_block_worklist_init(&state.worklist, impl->num_blocks, NULL);157158/* Allocate live_in and live_out sets and add all of the blocks to the159* worklist.160*/161nir_foreach_block(block, impl) {162init_liveness_block(block, &state);163}164165166/* We're now ready to work through the worklist and update the liveness167* sets of each of the blocks. By the time we get to this point, every168* block in the function implementation has been pushed onto the169* worklist in reverse order. As long as we keep the worklist170* up-to-date as we go, everything will get covered.171*/172while (!nir_block_worklist_is_empty(&state.worklist)) {173/* We pop them off in the reverse order we pushed them on. This way174* the first walk of the instructions is backwards so we only walk175* once in the case of no control flow.176*/177nir_block *block = nir_block_worklist_pop_head(&state.worklist);178179memcpy(block->live_in, block->live_out,180state.bitset_words * sizeof(BITSET_WORD));181182nir_if *following_if = nir_block_get_following_if(block);183if (following_if)184set_src_live(&following_if->condition, block->live_in);185186nir_foreach_instr_reverse(instr, block) {187/* Phi nodes are handled seperately so we want to skip them. Since188* we are going backwards and they are at the beginning, we can just189* break as soon as we see one.190*/191if (instr->type == nir_instr_type_phi)192break;193194nir_foreach_ssa_def(instr, set_ssa_def_dead, block->live_in);195nir_foreach_src(instr, set_src_live, block->live_in);196}197198/* Walk over all of the predecessors of the current block updating199* their live in with the live out of this one. If anything has200* changed, add the predecessor to the work list so that we ensure201* that the new information is used.202*/203set_foreach(block->predecessors, entry) {204nir_block *pred = (nir_block *)entry->key;205if (propagate_across_edge(pred, block, &state))206nir_block_worklist_push_tail(&state.worklist, pred);207}208}209210ralloc_free(state.tmp_live);211nir_block_worklist_fini(&state.worklist);212}213214/** Return the live set at a cursor215*216* Note: The bitset returned may be the live_in or live_out from the block in217* which the instruction lives. Do not ralloc_free() it directly;218* instead, provide a mem_ctx and free that.219*/220const BITSET_WORD *221nir_get_live_ssa_defs(nir_cursor cursor, void *mem_ctx)222{223nir_block *block = nir_cursor_current_block(cursor);224nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);225assert(impl->valid_metadata & nir_metadata_live_ssa_defs);226227switch (cursor.option) {228case nir_cursor_before_block:229return cursor.block->live_in;230231case nir_cursor_after_block:232return cursor.block->live_out;233234case nir_cursor_before_instr:235if (cursor.instr == nir_block_first_instr(cursor.instr->block))236return cursor.instr->block->live_in;237break;238239case nir_cursor_after_instr:240if (cursor.instr == nir_block_last_instr(cursor.instr->block))241return cursor.instr->block->live_out;242break;243}244245/* If we got here, we're an instruction cursor mid-block */246const unsigned bitset_words = BITSET_WORDS(impl->ssa_alloc);247BITSET_WORD *live = ralloc_array(mem_ctx, BITSET_WORD, bitset_words);248memcpy(live, block->live_out, bitset_words * sizeof(BITSET_WORD));249250nir_foreach_instr_reverse(instr, block) {251if (cursor.option == nir_cursor_after_instr && instr == cursor.instr)252break;253254/* If someone asked for liveness in the middle of a bunch of phis,255* that's an error. Since we are going backwards and they are at the256* beginning, we can just blow up as soon as we see one.257*/258assert(instr->type != nir_instr_type_phi);259if (instr->type == nir_instr_type_phi)260break;261262nir_foreach_ssa_def(instr, set_ssa_def_dead, live);263nir_foreach_src(instr, set_src_live, live);264265if (cursor.option == nir_cursor_before_instr && instr == cursor.instr)266break;267}268269return live;270}271272static bool273src_does_not_use_def(nir_src *src, void *def)274{275return !src->is_ssa || src->ssa != (nir_ssa_def *)def;276}277278static bool279search_for_use_after_instr(nir_instr *start, nir_ssa_def *def)280{281/* Only look for a use strictly after the given instruction */282struct exec_node *node = start->node.next;283while (!exec_node_is_tail_sentinel(node)) {284nir_instr *instr = exec_node_data(nir_instr, node, node);285if (!nir_foreach_src(instr, src_does_not_use_def, def))286return true;287node = node->next;288}289290/* If uses are considered to be in the block immediately preceding the if291* so we need to also check the following if condition, if any.292*/293nir_if *following_if = nir_block_get_following_if(start->block);294if (following_if && following_if->condition.is_ssa &&295following_if->condition.ssa == def)296return true;297298return false;299}300301/* Returns true if def is live at instr assuming that def comes before302* instr in a pre DFS search of the dominance tree.303*/304static bool305nir_ssa_def_is_live_at(nir_ssa_def *def, nir_instr *instr)306{307if (BITSET_TEST(instr->block->live_out, def->index)) {308/* Since def dominates instr, if def is in the liveout of the block,309* it's live at instr310*/311return true;312} else {313if (BITSET_TEST(instr->block->live_in, def->index) ||314def->parent_instr->block == instr->block) {315/* In this case it is either live coming into instr's block or it316* is defined in the same block. In this case, we simply need to317* see if it is used after instr.318*/319return search_for_use_after_instr(instr, def);320} else {321return false;322}323}324}325326bool327nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b)328{329if (a->parent_instr == b->parent_instr) {330/* Two variables defined at the same time interfere assuming at331* least one isn't dead.332*/333return true;334} else if (a->parent_instr->type == nir_instr_type_ssa_undef ||335b->parent_instr->type == nir_instr_type_ssa_undef) {336/* If either variable is an ssa_undef, then there's no interference */337return false;338} else if (a->parent_instr->index < b->parent_instr->index) {339return nir_ssa_def_is_live_at(a, b->parent_instr);340} else {341return nir_ssa_def_is_live_at(b, a->parent_instr);342}343}344345/* Takes an SSA def's defs and uses and expands the live interval to cover346* that range. Control flow effects are handled separately.347*/348static bool def_cb(nir_ssa_def *def, void *state)349{350nir_instr_liveness *liveness = state;351nir_instr *instr = def->parent_instr;352int index = def->index;353354liveness->defs[index].start = MIN2(liveness->defs[index].start, instr->index);355356nir_foreach_use(src, def) {357liveness->defs[index].end = MAX2(liveness->defs[index].end,358src->parent_instr->index);359}360361return true;362}363364nir_instr_liveness *365nir_live_ssa_defs_per_instr(nir_function_impl *impl)366{367/* We'll use block-level live_ssa_defs to expand our per-instr ranges for368* control flow.369*/370nir_metadata_require(impl,371nir_metadata_block_index |372nir_metadata_instr_index |373nir_metadata_live_ssa_defs);374375/* Make our struct. */376nir_instr_liveness *liveness = ralloc(NULL, nir_instr_liveness);377liveness->defs = rzalloc_array(liveness, nir_liveness_bounds,378impl->ssa_alloc);379380/* Set our starts so we can use MIN2() as we accumulate bounds. */381for (int i = 0; i < impl->ssa_alloc; i++)382liveness->defs->start = ~0;383384nir_foreach_block(block, impl) {385unsigned index;386BITSET_FOREACH_SET(index, block->live_in, impl->ssa_alloc) {387liveness->defs[index].start = MIN2(liveness->defs[index].start,388block->start_ip);389}390391nir_foreach_instr(instr, block) {392nir_foreach_ssa_def(instr, def_cb, liveness);393};394395/* track an if src's use. We need to make sure that our value is live396* across the if reference, where we don't have an instr->index397* representing the use. Mark it as live through the end of the block.398*/399nir_if *nif = nir_block_get_following_if(block);400if (nif) {401if (nif->condition.is_ssa) {402liveness->defs[nif->condition.ssa->index].end = MAX2(403liveness->defs[nif->condition.ssa->index].end, block->end_ip);404}405}406407BITSET_FOREACH_SET(index, block->live_out, impl->ssa_alloc) {408liveness->defs[index].end = MAX2(liveness->defs[index].end,409block->end_ip);410}411}412413return liveness;414}415416417