1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott ([email protected])
25 *
26 */
27
28 #include "nir.h"
29
30 /*
31 * Implements the algorithms for computing the dominance tree and the
32 * dominance frontier from "A Simple, Fast Dominance Algorithm" by Cooper,
33 * Harvey, and Kennedy.
34 */
35
36 static bool
init_block(nir_block * block,nir_function_impl * impl)37 init_block(nir_block *block, nir_function_impl *impl)
38 {
39 if (block == nir_start_block(impl))
40 block->imm_dom = block;
41 else
42 block->imm_dom = NULL;
43 block->num_dom_children = 0;
44
45 /* See nir_block_dominates */
46 block->dom_pre_index = UINT32_MAX;
47 block->dom_post_index = 0;
48
49 _mesa_set_clear(block->dom_frontier, NULL);
50
51 return true;
52 }
53
54 static nir_block *
intersect(nir_block * b1,nir_block * b2)55 intersect(nir_block *b1, nir_block *b2)
56 {
57 while (b1 != b2) {
58 /*
59 * Note, the comparisons here are the opposite of what the paper says
60 * because we index blocks from beginning -> end (i.e. reverse
61 * post-order) instead of post-order like they assume.
62 */
63 while (b1->index > b2->index)
64 b1 = b1->imm_dom;
65 while (b2->index > b1->index)
66 b2 = b2->imm_dom;
67 }
68
69 return b1;
70 }
71
72 static bool
calc_dominance(nir_block * block)73 calc_dominance(nir_block *block)
74 {
75 nir_block *new_idom = NULL;
76 set_foreach(block->predecessors, entry) {
77 nir_block *pred = (nir_block *)entry->key;
78
79 if (pred->imm_dom) {
80 if (new_idom)
81 new_idom = intersect(pred, new_idom);
82 else
83 new_idom = pred;
84 }
85 }
86
87 if (block->imm_dom != new_idom) {
88 block->imm_dom = new_idom;
89 return true;
90 }
91
92 return false;
93 }
94
95 static bool
calc_dom_frontier(nir_block * block)96 calc_dom_frontier(nir_block *block)
97 {
98 if (block->predecessors->entries > 1) {
99 set_foreach(block->predecessors, entry) {
100 nir_block *runner = (nir_block *)entry->key;
101
102 /* Skip unreachable predecessors */
103 if (runner->imm_dom == NULL)
104 continue;
105
106 while (runner != block->imm_dom) {
107 _mesa_set_add(runner->dom_frontier, block);
108 runner = runner->imm_dom;
109 }
110 }
111 }
112
113 return true;
114 }
115
116 /*
117 * Compute each node's children in the dominance tree from the immediate
118 * dominator information. We do this in three stages:
119 *
120 * 1. Calculate the number of children each node has
121 * 2. Allocate arrays, setting the number of children to 0 again
122 * 3. For each node, add itself to its parent's list of children, using
123 * num_dom_children as an index - at the end of this step, num_dom_children
124 * for each node will be the same as it was at the end of step #1.
125 */
126
127 static void
calc_dom_children(nir_function_impl * impl)128 calc_dom_children(nir_function_impl *impl)
129 {
130 void *mem_ctx = ralloc_parent(impl);
131
132 nir_foreach_block_unstructured(block, impl) {
133 if (block->imm_dom)
134 block->imm_dom->num_dom_children++;
135 }
136
137 nir_foreach_block_unstructured(block, impl) {
138 block->dom_children = ralloc_array(mem_ctx, nir_block *,
139 block->num_dom_children);
140 block->num_dom_children = 0;
141 }
142
143 nir_foreach_block_unstructured(block, impl) {
144 if (block->imm_dom) {
145 block->imm_dom->dom_children[block->imm_dom->num_dom_children++] = block;
146 }
147 }
148 }
149
150 static void
calc_dfs_indicies(nir_block * block,uint32_t * index)151 calc_dfs_indicies(nir_block *block, uint32_t *index)
152 {
153 /* UINT32_MAX has special meaning. See nir_block_dominates. */
154 assert(*index < UINT32_MAX - 2);
155
156 block->dom_pre_index = (*index)++;
157
158 for (unsigned i = 0; i < block->num_dom_children; i++)
159 calc_dfs_indicies(block->dom_children[i], index);
160
161 block->dom_post_index = (*index)++;
162 }
163
164 void
nir_calc_dominance_impl(nir_function_impl * impl)165 nir_calc_dominance_impl(nir_function_impl *impl)
166 {
167 if (impl->valid_metadata & nir_metadata_dominance)
168 return;
169
170 nir_metadata_require(impl, nir_metadata_block_index);
171
172 nir_foreach_block_unstructured(block, impl) {
173 init_block(block, impl);
174 }
175
176 bool progress = true;
177 while (progress) {
178 progress = false;
179 nir_foreach_block_unstructured(block, impl) {
180 if (block != nir_start_block(impl))
181 progress |= calc_dominance(block);
182 }
183 }
184
185 nir_foreach_block_unstructured(block, impl) {
186 calc_dom_frontier(block);
187 }
188
189 nir_block *start_block = nir_start_block(impl);
190 start_block->imm_dom = NULL;
191
192 calc_dom_children(impl);
193
194 uint32_t dfs_index = 1;
195 calc_dfs_indicies(start_block, &dfs_index);
196 }
197
198 void
nir_calc_dominance(nir_shader * shader)199 nir_calc_dominance(nir_shader *shader)
200 {
201 nir_foreach_function_impl(impl, shader) {
202 nir_calc_dominance_impl(impl);
203 }
204 }
205
206 static nir_block *
block_return_if_reachable(nir_block * b)207 block_return_if_reachable(nir_block *b)
208 {
209 return (b && nir_block_is_reachable(b)) ? b : NULL;
210 }
211
212 /**
213 * Computes the least common ancestor of two blocks. If one of the blocks
214 * is null or unreachable, the other block is returned or NULL if it's
215 * unreachable.
216 */
217 nir_block *
nir_dominance_lca(nir_block * b1,nir_block * b2)218 nir_dominance_lca(nir_block *b1, nir_block *b2)
219 {
220 if (b1 == NULL || !nir_block_is_reachable(b1))
221 return block_return_if_reachable(b2);
222
223 if (b2 == NULL || !nir_block_is_reachable(b2))
224 return block_return_if_reachable(b1);
225
226 assert(nir_cf_node_get_function(&b1->cf_node) ==
227 nir_cf_node_get_function(&b2->cf_node));
228
229 assert(nir_cf_node_get_function(&b1->cf_node)->valid_metadata &
230 nir_metadata_dominance);
231
232 return intersect(b1, b2);
233 }
234
235 /**
236 * Returns true if parent dominates child according to the following
237 * definition:
238 *
239 * "The block A dominates the block B if every path from the start block
240 * to block B passes through A."
241 *
242 * This means, in particular, that any unreachable block is dominated by every
243 * other block and an unreachable block does not dominate anything except
244 * another unreachable block.
245 */
246 bool
nir_block_dominates(nir_block * parent,nir_block * child)247 nir_block_dominates(nir_block *parent, nir_block *child)
248 {
249 assert(nir_cf_node_get_function(&parent->cf_node) ==
250 nir_cf_node_get_function(&child->cf_node));
251
252 assert(nir_cf_node_get_function(&parent->cf_node)->valid_metadata &
253 nir_metadata_dominance);
254
255 /* If a block is unreachable, then nir_block::dom_pre_index == UINT32_MAX
256 * and nir_block::dom_post_index == 0. This allows us to trivially handle
257 * unreachable blocks here with zero extra work.
258 */
259 return child->dom_pre_index >= parent->dom_pre_index &&
260 child->dom_post_index <= parent->dom_post_index;
261 }
262
263 bool
nir_block_is_unreachable(nir_block * block)264 nir_block_is_unreachable(nir_block *block)
265 {
266 assert(nir_cf_node_get_function(&block->cf_node)->valid_metadata &
267 nir_metadata_dominance);
268 assert(nir_cf_node_get_function(&block->cf_node)->valid_metadata &
269 nir_metadata_block_index);
270
271 /* Unreachable blocks have no dominator. The only reachable block with no
272 * dominator is the start block which has index 0.
273 */
274 return block->index > 0 && block->imm_dom == NULL;
275 }
276
277 void
nir_dump_dom_tree_impl(nir_function_impl * impl,FILE * fp)278 nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp)
279 {
280 fprintf(fp, "digraph doms_%s {\n", impl->function->name);
281
282 nir_foreach_block_unstructured(block, impl) {
283 if (block->imm_dom)
284 fprintf(fp, "\t%u -> %u\n", block->imm_dom->index, block->index);
285 }
286
287 fprintf(fp, "}\n\n");
288 }
289
290 void
nir_dump_dom_tree(nir_shader * shader,FILE * fp)291 nir_dump_dom_tree(nir_shader *shader, FILE *fp)
292 {
293 nir_foreach_function_impl(impl, shader) {
294 nir_dump_dom_tree_impl(impl, fp);
295 }
296 }
297
298 void
nir_dump_dom_frontier_impl(nir_function_impl * impl,FILE * fp)299 nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp)
300 {
301 nir_foreach_block_unstructured(block, impl) {
302 fprintf(fp, "DF(%u) = {", block->index);
303 set_foreach(block->dom_frontier, entry) {
304 nir_block *df = (nir_block *)entry->key;
305 fprintf(fp, "%u, ", df->index);
306 }
307 fprintf(fp, "}\n");
308 }
309 }
310
311 void
nir_dump_dom_frontier(nir_shader * shader,FILE * fp)312 nir_dump_dom_frontier(nir_shader *shader, FILE *fp)
313 {
314 nir_foreach_function_impl(impl, shader) {
315 nir_dump_dom_frontier_impl(impl, fp);
316 }
317 }
318
319 void
nir_dump_cfg_impl(nir_function_impl * impl,FILE * fp)320 nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp)
321 {
322 fprintf(fp, "digraph cfg_%s {\n", impl->function->name);
323
324 nir_foreach_block_unstructured(block, impl) {
325 if (block->successors[0])
326 fprintf(fp, "\t%u -> %u\n", block->index, block->successors[0]->index);
327 if (block->successors[1])
328 fprintf(fp, "\t%u -> %u\n", block->index, block->successors[1]->index);
329 }
330
331 fprintf(fp, "}\n\n");
332 }
333
334 void
nir_dump_cfg(nir_shader * shader,FILE * fp)335 nir_dump_cfg(nir_shader *shader, FILE *fp)
336 {
337 nir_foreach_function_impl(impl, shader) {
338 nir_dump_cfg_impl(impl, fp);
339 }
340 }
341