Debian Codegraph @

src/panfrost/compiler/bifrost/bi_dominance.c

raw
1 /*
2 * Copyright © 2014 Intel Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler.h"
7
8 /*
9 * Implements the algorithms for computing the dominance tree and the
10 * dominance frontier from "A Simple, Fast Dominance Algorithm" by Cooper,
11 * Harvey, and Kennedy.
12 */
13
14 static bool
15 init_block(bi_block *block, bi_context *ctx)
16 {
17 if (block == bi_entry_block(ctx))
18 block->imm_dom = block;
19 else
20 block->imm_dom = NULL;
21 block->num_dom_children = 0;
22
23 /* See bi_block_dominates */
24 block->dom_pre_index = UINT32_MAX;
25 block->dom_post_index = 0;
26
27 _mesa_set_clear(&block->dom_frontier, NULL);
28
29 return true;
30 }
31
32 static bi_block *
33 intersect(bi_block *b1, bi_block *b2)
34 {
35 while (b1 != b2) {
36 /*
37 * Note, the comparisons here are the opposite of what the paper says
38 * because we index blocks from beginning -> end (i.e. reverse
39 * post-order) instead of post-order like they assume.
40 */
41 while (b1->index > b2->index)
42 b1 = b1->imm_dom;
43 while (b2->index > b1->index)
44 b2 = b2->imm_dom;
45 }
46
47 return b1;
48 }
49
50 static bool
51 calc_dominance(bi_block *block)
52 {
53 bi_block *new_idom = NULL;
54 bi_foreach_predecessor(block, p) {
55 bi_block *pred = (*p);
56
57 if (pred->imm_dom) {
58 if (new_idom)
59 new_idom = intersect(pred, new_idom);
60 else
61 new_idom = pred;
62 }
63 }
64
65 if (block->imm_dom != new_idom) {
66 block->imm_dom = new_idom;
67 return true;
68 }
69
70 return false;
71 }
72
73 static bool
74 calc_dom_frontier(bi_block *block)
75 {
76 if (block->predecessors.size > 1) {
77 bi_foreach_predecessor(block, p) {
78 bi_block *runner = (*p);
79
80 /* Skip unreachable predecessors */
81 if (runner->imm_dom == NULL)
82 continue;
83
84 while (runner != block->imm_dom) {
85 _mesa_set_add(&runner->dom_frontier, block);
86 runner = runner->imm_dom;
87 }
88 }
89 }
90
91 return true;
92 }
93
94 /*
95 * Compute each node's children in the dominance tree from the immediate
96 * dominator information. We do this in three stages:
97 *
98 * 1. Calculate the number of children each node has
99 * 2. Allocate arrays, setting the number of children to 0 again
100 * 3. For each node, add itself to its parent's list of children, using
101 * num_dom_children as an index - at the end of this step, num_dom_children
102 * for each node will be the same as it was at the end of step #1.
103 */
104
105 static void
106 calc_dom_children(bi_context *ctx)
107 {
108 void *mem_ctx = ctx;
109
110 bi_foreach_block(ctx, block) {
111 if (block->imm_dom)
112 block->imm_dom->num_dom_children++;
113 }
114
115 bi_foreach_block(ctx, block) {
116 if (!block->num_dom_children) {
117 block->dom_children = NULL;
118 continue;
119 }
120
121 if (block->num_dom_children <= 3) {
122 block->dom_children = block->_dom_children_storage;
123 } else {
124 block->dom_children =
125 ralloc_array(mem_ctx, bi_block *, block->num_dom_children);
126 }
127 block->num_dom_children = 0;
128 }
129
130 bi_foreach_block(ctx, block) {
131 if (block->imm_dom) {
132 block->imm_dom->dom_children[block->imm_dom->num_dom_children++] =
133 block;
134 }
135 }
136 }
137
138 static void
139 calc_dfs_indices(bi_block *block, uint32_t *index)
140 {
141 /* UINT32_MAX has special meaning. See bi_block_dominates. */
142 assert(*index < UINT32_MAX - 2);
143
144 block->dom_pre_index = (*index)++;
145
146 for (unsigned i = 0; i < block->num_dom_children; i++)
147 calc_dfs_indices(block->dom_children[i], index);
148
149 block->dom_post_index = (*index)++;
150 }
151
152 void
153 bi_calc_dominance(bi_context *ctx)
154 {
155 bi_foreach_block(ctx, block) {
156 init_block(block, ctx);
157 }
158
159 bool progress = true;
160 while (progress) {
161 progress = false;
162 bi_foreach_block(ctx, block) {
163 if (block != bi_entry_block(ctx))
164 progress |= calc_dominance(block);
165 }
166 }
167
168 bi_foreach_block(ctx, block) {
169 calc_dom_frontier(block);
170 }
171
172 bi_block *start_block = bi_entry_block(ctx);
173 start_block->imm_dom = NULL;
174
175 calc_dom_children(ctx);
176
177 uint32_t dfs_index = 1;
178 calc_dfs_indices(start_block, &dfs_index);
179 }
180
181 /**
182 * Returns true if parent dominates child according to the following
183 * definition:
184 *
185 * "The block A dominates the block B if every path from the start block
186 * to block B passes through A."
187 *
188 * This means, in particular, that any unreachable block is dominated by every
189 * other block and an unreachable block does not dominate anything except
190 * another unreachable block.
191 */
192 bool
193 bi_block_dominates(bi_block *parent, bi_block *child)
194 {
195 /* If a block is unreachable, then nir_block::dom_pre_index == UINT32_MAX
196 * and nir_block::dom_post_index == 0. This allows us to trivially handle
197 * unreachable blocks here with zero extra work.
198 */
199 return child->dom_pre_index >= parent->dom_pre_index &&
200 child->dom_post_index <= parent->dom_post_index;
201 }