1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci * Copyright © 2021 Valve Corporation
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22bf215546Sopenharmony_ci * IN THE SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "ir3.h"
27bf215546Sopenharmony_ci#include "ralloc.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/*
30bf215546Sopenharmony_ci * Implements the algorithms for computing the dominance tree and the
31bf215546Sopenharmony_ci * dominance frontier from "A Simple, Fast Dominance Algorithm" by Cooper,
32bf215546Sopenharmony_ci * Harvey, and Kennedy.
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic struct ir3_block *
36bf215546Sopenharmony_ciintersect(struct ir3_block *b1, struct ir3_block *b2)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   while (b1 != b2) {
39bf215546Sopenharmony_ci      /*
40bf215546Sopenharmony_ci       * Note, the comparisons here are the opposite of what the paper says
41bf215546Sopenharmony_ci       * because we index blocks from beginning -> end (i.e. reverse
42bf215546Sopenharmony_ci       * post-order) instead of post-order like they assume.
43bf215546Sopenharmony_ci       */
44bf215546Sopenharmony_ci      while (b1->index > b2->index)
45bf215546Sopenharmony_ci         b1 = b1->imm_dom;
46bf215546Sopenharmony_ci      while (b2->index > b1->index)
47bf215546Sopenharmony_ci         b2 = b2->imm_dom;
48bf215546Sopenharmony_ci   }
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   return b1;
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic bool
54bf215546Sopenharmony_cicalc_dominance(struct ir3_block *block)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   struct ir3_block *new_idom = NULL;
57bf215546Sopenharmony_ci   for (unsigned i = 0; i < block->predecessors_count; i++) {
58bf215546Sopenharmony_ci      struct ir3_block *pred = block->predecessors[i];
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci      if (pred->imm_dom) {
61bf215546Sopenharmony_ci         if (new_idom)
62bf215546Sopenharmony_ci            new_idom = intersect(pred, new_idom);
63bf215546Sopenharmony_ci         else
64bf215546Sopenharmony_ci            new_idom = pred;
65bf215546Sopenharmony_ci      }
66bf215546Sopenharmony_ci   }
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   if (block->imm_dom != new_idom) {
69bf215546Sopenharmony_ci      block->imm_dom = new_idom;
70bf215546Sopenharmony_ci      return true;
71bf215546Sopenharmony_ci   }
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   return false;
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic unsigned
77bf215546Sopenharmony_cicalc_dfs_indices(struct ir3_block *block, unsigned index)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   block->dom_pre_index = index++;
80bf215546Sopenharmony_ci   for (unsigned i = 0; i < block->dom_children_count; i++)
81bf215546Sopenharmony_ci      index = calc_dfs_indices(block->dom_children[i], index);
82bf215546Sopenharmony_ci   block->dom_post_index = index++;
83bf215546Sopenharmony_ci   return index;
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_civoid
87bf215546Sopenharmony_ciir3_calc_dominance(struct ir3 *ir)
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci   unsigned i = 0;
90bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
91bf215546Sopenharmony_ci      block->index = i++;
92bf215546Sopenharmony_ci      if (block == ir3_start_block(ir))
93bf215546Sopenharmony_ci         block->imm_dom = block;
94bf215546Sopenharmony_ci      else
95bf215546Sopenharmony_ci         block->imm_dom = NULL;
96bf215546Sopenharmony_ci      block->dom_children = NULL;
97bf215546Sopenharmony_ci      block->dom_children_count = block->dom_children_sz = 0;
98bf215546Sopenharmony_ci   }
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   bool progress = true;
101bf215546Sopenharmony_ci   while (progress) {
102bf215546Sopenharmony_ci      progress = false;
103bf215546Sopenharmony_ci      foreach_block (block, &ir->block_list) {
104bf215546Sopenharmony_ci         if (block != ir3_start_block(ir))
105bf215546Sopenharmony_ci            progress |= calc_dominance(block);
106bf215546Sopenharmony_ci      }
107bf215546Sopenharmony_ci   }
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   ir3_start_block(ir)->imm_dom = NULL;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
112bf215546Sopenharmony_ci      if (block->imm_dom)
113bf215546Sopenharmony_ci         array_insert(block->imm_dom, block->imm_dom->dom_children, block);
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   calc_dfs_indices(ir3_start_block(ir), 0);
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci/* Return true if a dominates b. This includes if a == b. */
120bf215546Sopenharmony_cibool
121bf215546Sopenharmony_ciir3_block_dominates(struct ir3_block *a, struct ir3_block *b)
122bf215546Sopenharmony_ci{
123bf215546Sopenharmony_ci   return a->dom_pre_index <= b->dom_pre_index &&
124bf215546Sopenharmony_ci          a->dom_post_index >= b->dom_post_index;
125bf215546Sopenharmony_ci}
126