1 /* Generic dominator tree walker
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 /* This file implements a generic walker for dominator trees.
31 To understand the dominator walker one must first have a grasp of dominators,
32 immediate dominators and the dominator tree.
35 A block B1 is said to dominate B2 if every path from the entry to B2 must
36 pass through B1. Given the dominance relationship, we can proceed to
37 compute immediate dominators. Note it is not important whether or not
38 our definition allows a block to dominate itself.
41 Every block in the CFG has no more than one immediate dominator. The
42 immediate dominator of block BB must dominate BB and must not dominate
43 any other dominator of BB and must not be BB itself.
46 If we then construct a tree where each node is a basic block and there
47 is an edge from each block's immediate dominator to the block itself, then
48 we have a dominator tree.
51 [ Note this walker can also walk the post-dominator tree, which is
52 defined in a similar manner. i.e., block B1 is said to post-dominate
53 block B2 if all paths from B2 to the exit block must pass through
56 For example, given the CFG
73 We have a dominator tree which looks like
93 The dominator tree is the basis for a number of analysis, transformation
94 and optimization algorithms that operate on a semi-global basis.
96 The dominator walker is a generic routine which visits blocks in the CFG
97 via a depth first search of the dominator tree. In the example above
98 the dominator walker might visit blocks in the following order
99 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
101 The dominator walker has a number of callbacks to perform actions
102 during the walk of the dominator tree. There are two callbacks
103 which walk statements, one before visiting the dominator children,
104 one after visiting the dominator children. There is a callback
105 before and after each statement walk callback. In addition, the
106 dominator walker manages allocation/deallocation of data structures
107 which are local to each block visited.
109 The dominator walker is meant to provide a generic means to build a pass
110 which can analyze or transform/optimize a function based on walking
111 the dominator tree. One simply fills in the dominator walker data
112 structure with the appropriate callbacks and calls the walker.
114 We currently use the dominator walker to prune the set of variables
115 which might need PHI nodes (which can greatly improve compile-time
116 performance in some cases).
118 We also use the dominator walker to rewrite the function into SSA form
119 which reduces code duplication since the rewriting phase is inherently
120 a walk of the dominator tree.
122 And (of course), we use the dominator walker to drive our dominator
123 optimizer, which is a semi-global optimizer.
127 Walking statements is based on the block statement iterator abstraction,
128 which is currently an abstraction over walking tree statements. Thus
129 the dominator walker is currently only useful for trees. */
131 /* Reverse postorder index of each basic block. */
132 static int *bb_postorder
;
135 cmp_bb_postorder (const void *a
, const void *b
)
137 basic_block bb1
= *(const basic_block
*)(a
);
138 basic_block bb2
= *(const basic_block
*)(b
);
139 /* Place higher completion number first (pop off lower number first). */
140 return bb_postorder
[bb2
->index
] - bb_postorder
[bb1
->index
];
143 /* Permute array BBS of N basic blocks in postorder,
144 i.e. by descending number in BB_POSTORDER array. */
147 sort_bbs_postorder (basic_block
*bbs
, int n
)
149 if (__builtin_expect (n
== 2, true))
151 basic_block bb0
= bbs
[0], bb1
= bbs
[1];
152 if (bb_postorder
[bb0
->index
] < bb_postorder
[bb1
->index
])
153 bbs
[0] = bb1
, bbs
[1] = bb0
;
155 else if (__builtin_expect (n
== 3, true))
157 basic_block bb0
= bbs
[0], bb1
= bbs
[1], bb2
= bbs
[2];
158 if (bb_postorder
[bb0
->index
] < bb_postorder
[bb1
->index
])
159 std::swap (bb0
, bb1
);
160 if (bb_postorder
[bb1
->index
] < bb_postorder
[bb2
->index
])
162 std::swap (bb1
, bb2
);
163 if (bb_postorder
[bb0
->index
] < bb_postorder
[bb1
->index
])
164 std::swap (bb0
, bb1
);
166 bbs
[0] = bb0
, bbs
[1] = bb1
, bbs
[2] = bb2
;
169 qsort (bbs
, n
, sizeof *bbs
, cmp_bb_postorder
);
172 /* Set EDGE_EXECUTABLE on every edge within FN's CFG. */
175 set_all_edges_as_executable (function
*fn
)
178 FOR_ALL_BB_FN (bb
, fn
)
182 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
183 e
->flags
|= EDGE_EXECUTABLE
;
187 /* Constructor for a dom walker. */
189 dom_walker::dom_walker (cdi_direction direction
,
190 enum reachability reachability
,
191 int *bb_index_to_rpo
)
192 : m_dom_direction (direction
),
193 m_skip_unreachable_blocks (reachability
!= ALL_BLOCKS
),
194 m_user_bb_to_rpo (bb_index_to_rpo
!= NULL
),
195 m_unreachable_dom (NULL
),
196 m_bb_to_rpo (bb_index_to_rpo
)
198 /* Compute the basic-block index to RPO mapping if not provided by
200 if (! m_bb_to_rpo
&& direction
== CDI_DOMINATORS
)
202 int *postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
203 int postorder_num
= pre_and_rev_post_order_compute (NULL
, postorder
,
205 m_bb_to_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
206 for (int i
= 0; i
< postorder_num
; ++i
)
207 m_bb_to_rpo
[postorder
[i
]] = i
;
211 /* Set up edge flags if need be. */
212 switch (reachability
)
217 /* No need to touch edge flags. */
220 case REACHABLE_BLOCKS
:
221 set_all_edges_as_executable (cfun
);
224 case REACHABLE_BLOCKS_PRESERVING_FLAGS
:
225 /* Preserve the edge flags. */
232 dom_walker::~dom_walker ()
234 if (! m_user_bb_to_rpo
)
238 /* Return TRUE if BB is reachable, false otherwise. */
241 dom_walker::bb_reachable (struct function
*fun
, basic_block bb
)
243 /* If we're not skipping unreachable blocks, then assume everything
245 if (!m_skip_unreachable_blocks
)
248 /* If any of the predecessor edges that do not come from blocks dominated
249 by us are still marked as possibly executable consider this block
251 bool reachable
= false;
252 if (!m_unreachable_dom
)
254 reachable
= bb
== ENTRY_BLOCK_PTR_FOR_FN (fun
);
257 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
258 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
259 reachable
|= (e
->flags
& EDGE_EXECUTABLE
);
265 /* BB has been determined to be unreachable. Propagate that property
266 to incoming and outgoing edges of BB as appropriate. */
269 dom_walker::propagate_unreachable_to_edges (basic_block bb
,
271 dump_flags_t dump_flags
)
273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
274 fprintf (dump_file
, "Marking all outgoing edges of unreachable "
275 "BB %d as not executable\n", bb
->index
);
279 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
280 e
->flags
&= ~EDGE_EXECUTABLE
;
282 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
284 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
286 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
287 fprintf (dump_file
, "Marking backedge from BB %d into "
288 "unreachable BB %d as not executable\n",
289 e
->src
->index
, bb
->index
);
290 e
->flags
&= ~EDGE_EXECUTABLE
;
294 if (!m_unreachable_dom
)
295 m_unreachable_dom
= bb
;
298 const edge
dom_walker::STOP
= (edge
)-1;
300 /* Recursively walk the dominator tree.
301 BB is the basic block we are currently visiting. */
304 dom_walker::walk (basic_block bb
)
307 basic_block
*worklist
= XNEWVEC (basic_block
,
308 n_basic_blocks_for_fn (cfun
) * 2);
310 bb_postorder
= m_bb_to_rpo
;
314 /* Don't worry about unreachable blocks. */
315 if (EDGE_COUNT (bb
->preds
) > 0
316 || bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)
317 || bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
319 edge taken_edge
= NULL
;
321 /* Callback for subclasses to do custom things before we have walked
322 the dominator children, but before we walk statements. */
323 if (this->bb_reachable (cfun
, bb
))
325 taken_edge
= before_dom_children (bb
);
326 if (taken_edge
&& taken_edge
!= STOP
)
330 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
332 e
->flags
&= ~EDGE_EXECUTABLE
;
336 propagate_unreachable_to_edges (bb
, dump_file
, dump_flags
);
338 /* Mark the current BB to be popped out of the recursion stack
339 once children are processed. */
341 worklist
[sp
++] = NULL
;
343 /* If the callback returned NONE then we are supposed to
344 stop and not even propagate EDGE_EXECUTABLE further. */
345 if (taken_edge
!= STOP
)
348 for (dest
= first_dom_son (m_dom_direction
, bb
);
349 dest
; dest
= next_dom_son (m_dom_direction
, dest
))
350 worklist
[sp
++] = dest
;
351 if (sp
- saved_sp
> 1 && m_dom_direction
== CDI_DOMINATORS
)
352 sort_bbs_postorder (&worklist
[saved_sp
], sp
- saved_sp
);
355 /* NULL is used to mark pop operations in the recursion stack. */
356 while (sp
> 0 && !worklist
[sp
- 1])
361 /* Callback allowing subclasses to do custom things after we have
362 walked dominator children, but before we walk statements. */
363 if (bb_reachable (cfun
, bb
))
364 after_dom_children (bb
);
365 else if (m_unreachable_dom
== bb
)
366 m_unreachable_dom
= NULL
;