17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2018 Valve Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#ifndef ACO_DOMINANCE_CPP
267ec681f3Smrg#define ACO_DOMINANCE_CPP
277ec681f3Smrg
287ec681f3Smrg#include "aco_ir.h"
297ec681f3Smrg
307ec681f3Smrg/*
317ec681f3Smrg * Implements the algorithms for computing the dominator tree from
327ec681f3Smrg * "A Simple, Fast Dominance Algorithm" by Cooper, Harvey, and Kennedy.
337ec681f3Smrg *
347ec681f3Smrg * Different from the paper, our CFG allows to compute the dominator tree
357ec681f3Smrg * in a single pass as it is guaranteed that the dominating predecessors
367ec681f3Smrg * are processed before the current block.
377ec681f3Smrg */
387ec681f3Smrg
397ec681f3Smrgnamespace aco {
407ec681f3Smrg
417ec681f3Smrgvoid
427ec681f3Smrgdominator_tree(Program* program)
437ec681f3Smrg{
447ec681f3Smrg   program->blocks[0].logical_idom = 0;
457ec681f3Smrg   program->blocks[0].linear_idom = 0;
467ec681f3Smrg
477ec681f3Smrg   for (unsigned i = 1; i < program->blocks.size(); i++) {
487ec681f3Smrg      Block& block = program->blocks[i];
497ec681f3Smrg      int new_logical_idom = -1;
507ec681f3Smrg      int new_linear_idom = -1;
517ec681f3Smrg      for (unsigned pred_idx : block.logical_preds) {
527ec681f3Smrg         if ((int)program->blocks[pred_idx].logical_idom == -1)
537ec681f3Smrg            continue;
547ec681f3Smrg
557ec681f3Smrg         if (new_logical_idom == -1) {
567ec681f3Smrg            new_logical_idom = pred_idx;
577ec681f3Smrg            continue;
587ec681f3Smrg         }
597ec681f3Smrg
607ec681f3Smrg         while ((int)pred_idx != new_logical_idom) {
617ec681f3Smrg            if ((int)pred_idx > new_logical_idom)
627ec681f3Smrg               pred_idx = program->blocks[pred_idx].logical_idom;
637ec681f3Smrg            if ((int)pred_idx < new_logical_idom)
647ec681f3Smrg               new_logical_idom = program->blocks[new_logical_idom].logical_idom;
657ec681f3Smrg         }
667ec681f3Smrg      }
677ec681f3Smrg
687ec681f3Smrg      for (unsigned pred_idx : block.linear_preds) {
697ec681f3Smrg         if ((int)program->blocks[pred_idx].linear_idom == -1)
707ec681f3Smrg            continue;
717ec681f3Smrg
727ec681f3Smrg         if (new_linear_idom == -1) {
737ec681f3Smrg            new_linear_idom = pred_idx;
747ec681f3Smrg            continue;
757ec681f3Smrg         }
767ec681f3Smrg
777ec681f3Smrg         while ((int)pred_idx != new_linear_idom) {
787ec681f3Smrg            if ((int)pred_idx > new_linear_idom)
797ec681f3Smrg               pred_idx = program->blocks[pred_idx].linear_idom;
807ec681f3Smrg            if ((int)pred_idx < new_linear_idom)
817ec681f3Smrg               new_linear_idom = program->blocks[new_linear_idom].linear_idom;
827ec681f3Smrg         }
837ec681f3Smrg      }
847ec681f3Smrg
857ec681f3Smrg      block.logical_idom = new_logical_idom;
867ec681f3Smrg      block.linear_idom = new_linear_idom;
877ec681f3Smrg   }
887ec681f3Smrg}
897ec681f3Smrg
907ec681f3Smrg} // namespace aco
917ec681f3Smrg#endif
92