Path: blob/21.2-virgl/src/compiler/nir/nir_dominance.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* Connor Abbott ([email protected])24*25*/2627#include "nir.h"2829/*30* Implements the algorithms for computing the dominance tree and the31* dominance frontier from "A Simple, Fast Dominance Algorithm" by Cooper,32* Harvey, and Kennedy.33*/3435static bool36init_block(nir_block *block, nir_function_impl *impl)37{38if (block == nir_start_block(impl))39block->imm_dom = block;40else41block->imm_dom = NULL;42block->num_dom_children = 0;4344/* See nir_block_dominates */45block->dom_pre_index = UINT32_MAX;46block->dom_post_index = 0;4748_mesa_set_clear(block->dom_frontier, NULL);4950return true;51}5253static nir_block *54intersect(nir_block *b1, nir_block *b2)55{56while (b1 != b2) {57/*58* Note, the comparisons here are the opposite of what the paper says59* because we index blocks from beginning -> end (i.e. reverse60* post-order) instead of post-order like they assume.61*/62while (b1->index > b2->index)63b1 = b1->imm_dom;64while (b2->index > b1->index)65b2 = b2->imm_dom;66}6768return b1;69}7071static bool72calc_dominance(nir_block *block)73{74nir_block *new_idom = NULL;75set_foreach(block->predecessors, entry) {76nir_block *pred = (nir_block *) entry->key;7778if (pred->imm_dom) {79if (new_idom)80new_idom = intersect(pred, new_idom);81else82new_idom = pred;83}84}8586if (block->imm_dom != new_idom) {87block->imm_dom = new_idom;88return true;89}9091return false;92}9394static bool95calc_dom_frontier(nir_block *block)96{97if (block->predecessors->entries > 1) {98set_foreach(block->predecessors, entry) {99nir_block *runner = (nir_block *) entry->key;100101/* Skip unreachable predecessors */102if (runner->imm_dom == NULL)103continue;104105while (runner != block->imm_dom) {106_mesa_set_add(runner->dom_frontier, block);107runner = runner->imm_dom;108}109}110}111112return true;113}114115/*116* Compute each node's children in the dominance tree from the immediate117* dominator information. We do this in three stages:118*119* 1. Calculate the number of children each node has120* 2. Allocate arrays, setting the number of children to 0 again121* 3. For each node, add itself to its parent's list of children, using122* num_dom_children as an index - at the end of this step, num_dom_children123* for each node will be the same as it was at the end of step #1.124*/125126static void127calc_dom_children(nir_function_impl* impl)128{129void *mem_ctx = ralloc_parent(impl);130131nir_foreach_block_unstructured(block, impl) {132if (block->imm_dom)133block->imm_dom->num_dom_children++;134}135136nir_foreach_block_unstructured(block, impl) {137block->dom_children = ralloc_array(mem_ctx, nir_block *,138block->num_dom_children);139block->num_dom_children = 0;140}141142nir_foreach_block_unstructured(block, impl) {143if (block->imm_dom) {144block->imm_dom->dom_children[block->imm_dom->num_dom_children++]145= block;146}147}148}149150static void151calc_dfs_indicies(nir_block *block, uint32_t *index)152{153/* UINT32_MAX has special meaning. See nir_block_dominates. */154assert(*index < UINT32_MAX - 2);155156block->dom_pre_index = (*index)++;157158for (unsigned i = 0; i < block->num_dom_children; i++)159calc_dfs_indicies(block->dom_children[i], index);160161block->dom_post_index = (*index)++;162}163164void165nir_calc_dominance_impl(nir_function_impl *impl)166{167if (impl->valid_metadata & nir_metadata_dominance)168return;169170nir_metadata_require(impl, nir_metadata_block_index);171172173nir_foreach_block_unstructured(block, impl) {174init_block(block, impl);175}176177bool progress = true;178while (progress) {179progress = false;180nir_foreach_block_unstructured(block, impl) {181if (block != nir_start_block(impl))182progress |= calc_dominance(block);183}184}185186nir_foreach_block_unstructured(block, impl) {187calc_dom_frontier(block);188}189190nir_block *start_block = nir_start_block(impl);191start_block->imm_dom = NULL;192193calc_dom_children(impl);194195uint32_t dfs_index = 1;196calc_dfs_indicies(start_block, &dfs_index);197}198199void200nir_calc_dominance(nir_shader *shader)201{202nir_foreach_function(function, shader) {203if (function->impl)204nir_calc_dominance_impl(function->impl);205}206}207208static nir_block *209block_return_if_reachable(nir_block *b)210{211return (b && nir_block_is_reachable(b)) ? b : NULL;212}213214/**215* Computes the least common ancestor of two blocks. If one of the blocks216* is null or unreachable, the other block is returned or NULL if it's217* unreachable.218*/219nir_block *220nir_dominance_lca(nir_block *b1, nir_block *b2)221{222if (b1 == NULL || !nir_block_is_reachable(b1))223return block_return_if_reachable(b2);224225if (b2 == NULL || !nir_block_is_reachable(b2))226return block_return_if_reachable(b1);227228assert(nir_cf_node_get_function(&b1->cf_node) ==229nir_cf_node_get_function(&b2->cf_node));230231assert(nir_cf_node_get_function(&b1->cf_node)->valid_metadata &232nir_metadata_dominance);233234return intersect(b1, b2);235}236237/**238* Returns true if parent dominates child according to the following239* definition:240*241* "The block A dominates the block B if every path from the start block242* to block B passes through A."243*244* This means, in particular, that any unreachable block is dominated by every245* other block and an unreachable block does not dominate anything except246* another unreachable block.247*/248bool249nir_block_dominates(nir_block *parent, nir_block *child)250{251assert(nir_cf_node_get_function(&parent->cf_node) ==252nir_cf_node_get_function(&child->cf_node));253254assert(nir_cf_node_get_function(&parent->cf_node)->valid_metadata &255nir_metadata_dominance);256257/* If a block is unreachable, then nir_block::dom_pre_index == UINT32_MAX258* and nir_block::dom_post_index == 0. This allows us to trivially handle259* unreachable blocks here with zero extra work.260*/261return child->dom_pre_index >= parent->dom_pre_index &&262child->dom_post_index <= parent->dom_post_index;263}264265bool266nir_block_is_unreachable(nir_block *block)267{268assert(nir_cf_node_get_function(&block->cf_node)->valid_metadata &269nir_metadata_dominance);270assert(nir_cf_node_get_function(&block->cf_node)->valid_metadata &271nir_metadata_block_index);272273/* Unreachable blocks have no dominator. The only reachable block with no274* dominator is the start block which has index 0.275*/276return block->index > 0 && block->imm_dom == NULL;277}278279void280nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp)281{282fprintf(fp, "digraph doms_%s {\n", impl->function->name);283284nir_foreach_block_unstructured(block, impl) {285if (block->imm_dom)286fprintf(fp, "\t%u -> %u\n", block->imm_dom->index, block->index);287}288289fprintf(fp, "}\n\n");290}291292void293nir_dump_dom_tree(nir_shader *shader, FILE *fp)294{295nir_foreach_function(function, shader) {296if (function->impl)297nir_dump_dom_tree_impl(function->impl, fp);298}299}300301void302nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp)303{304nir_foreach_block_unstructured(block, impl) {305fprintf(fp, "DF(%u) = {", block->index);306set_foreach(block->dom_frontier, entry) {307nir_block *df = (nir_block *) entry->key;308fprintf(fp, "%u, ", df->index);309}310fprintf(fp, "}\n");311}312}313314void315nir_dump_dom_frontier(nir_shader *shader, FILE *fp)316{317nir_foreach_function(function, shader) {318if (function->impl)319nir_dump_dom_frontier_impl(function->impl, fp);320}321}322323void324nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp)325{326fprintf(fp, "digraph cfg_%s {\n", impl->function->name);327328nir_foreach_block_unstructured(block, impl) {329if (block->successors[0])330fprintf(fp, "\t%u -> %u\n", block->index, block->successors[0]->index);331if (block->successors[1])332fprintf(fp, "\t%u -> %u\n", block->index, block->successors[1]->index);333}334335fprintf(fp, "}\n\n");336}337338void339nir_dump_cfg(nir_shader *shader, FILE *fp)340{341nir_foreach_function(function, shader) {342if (function->impl)343nir_dump_cfg_impl(function->impl, fp);344}345}346347348