Path: blob/21.2-virgl/src/amd/compiler/aco_dominance.cpp
4550 views
/*1* Copyright © 2018 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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22*/2324#ifndef ACO_DOMINANCE_CPP25#define ACO_DOMINANCE_CPP2627#include "aco_ir.h"2829/*30* Implements the algorithms for computing the dominator tree from31* "A Simple, Fast Dominance Algorithm" by Cooper, Harvey, and Kennedy.32*33* Different from the paper, our CFG allows to compute the dominator tree34* in a single pass as it is guaranteed that the dominating predecessors35* are processed before the current block.36*/3738namespace aco {3940void41dominator_tree(Program* program)42{43program->blocks[0].logical_idom = 0;44program->blocks[0].linear_idom = 0;4546for (unsigned i = 1; i < program->blocks.size(); i++) {47Block& block = program->blocks[i];48int new_logical_idom = -1;49int new_linear_idom = -1;50for (unsigned pred_idx : block.logical_preds) {51if ((int)program->blocks[pred_idx].logical_idom == -1)52continue;5354if (new_logical_idom == -1) {55new_logical_idom = pred_idx;56continue;57}5859while ((int)pred_idx != new_logical_idom) {60if ((int)pred_idx > new_logical_idom)61pred_idx = program->blocks[pred_idx].logical_idom;62if ((int)pred_idx < new_logical_idom)63new_logical_idom = program->blocks[new_logical_idom].logical_idom;64}65}6667for (unsigned pred_idx : block.linear_preds) {68if ((int)program->blocks[pred_idx].linear_idom == -1)69continue;7071if (new_linear_idom == -1) {72new_linear_idom = pred_idx;73continue;74}7576while ((int)pred_idx != new_linear_idom) {77if ((int)pred_idx > new_linear_idom)78pred_idx = program->blocks[pred_idx].linear_idom;79if ((int)pred_idx < new_linear_idom)80new_linear_idom = program->blocks[new_linear_idom].linear_idom;81}82}8384block.logical_idom = new_logical_idom;85block.linear_idom = new_linear_idom;86}87}8889} // namespace aco90#endif919293