Path: blob/21.2-virgl/src/freedreno/ir3/ir3_dominance.c
4565 views
/*1* Copyright © 2014 Intel Corporation2* Copyright © 2021 Valve Corporation3*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, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*23*/2425#include "ir3.h"26#include "ralloc.h"2728/*29* Implements the algorithms for computing the dominance tree and the30* dominance frontier from "A Simple, Fast Dominance Algorithm" by Cooper,31* Harvey, and Kennedy.32*/3334static struct ir3_block *35intersect(struct ir3_block *b1, struct ir3_block *b2)36{37while (b1 != b2) {38/*39* Note, the comparisons here are the opposite of what the paper says40* because we index blocks from beginning -> end (i.e. reverse41* post-order) instead of post-order like they assume.42*/43while (b1->index > b2->index)44b1 = b1->imm_dom;45while (b2->index > b1->index)46b2 = b2->imm_dom;47}4849return b1;50}5152static bool53calc_dominance(struct ir3_block *block)54{55struct ir3_block *new_idom = NULL;56for (unsigned i = 0; i < block->predecessors_count; i++) {57struct ir3_block *pred = block->predecessors[i];5859if (pred->imm_dom) {60if (new_idom)61new_idom = intersect(pred, new_idom);62else63new_idom = pred;64}65}6667if (block->imm_dom != new_idom) {68block->imm_dom = new_idom;69return true;70}7172return false;73}7475static unsigned76calc_dfs_indices(struct ir3_block *block, unsigned index)77{78block->dom_pre_index = index++;79for (unsigned i = 0; i < block->dom_children_count; i++)80index = calc_dfs_indices(block->dom_children[i], index);81block->dom_post_index = index++;82return index;83}8485void86ir3_calc_dominance(struct ir3 *ir)87{88unsigned i = 0;89foreach_block (block, &ir->block_list) {90block->index = i++;91if (block == ir3_start_block(ir))92block->imm_dom = block;93else94block->imm_dom = NULL;95block->dom_children = NULL;96block->dom_children_count = block->dom_children_sz = 0;97}9899bool progress = true;100while (progress) {101progress = false;102foreach_block (block, &ir->block_list) {103if (block != ir3_start_block(ir))104progress |= calc_dominance(block);105}106}107108ir3_start_block(ir)->imm_dom = NULL;109110foreach_block (block, &ir->block_list) {111if (block->imm_dom)112array_insert(block->imm_dom, block->imm_dom->dom_children, block);113}114115calc_dfs_indices(ir3_start_block(ir), 0);116}117118/* Return true if a dominates b. This includes if a == b. */119bool120ir3_block_dominates(struct ir3_block *a, struct ir3_block *b)121{122return a->dom_pre_index <= b->dom_pre_index &&123a->dom_post_index >= b->dom_post_index;124}125126127