]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/domwalk.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / domwalk.c
CommitLineData
6de9cd9a 1/* Generic dominator tree walker
8d9254fc 2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9dcd6f09 9the Free Software Foundation; either version 3, or (at your option)
6de9cd9a
DN
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
c7131fb2 24#include "backend.h"
60393bbc 25#include "cfganal.h"
6de9cd9a 26#include "domwalk.h"
3daacdcd 27#include "dumpfile.h"
6de9cd9a 28
b8698a0f 29/* This file implements a generic walker for dominator trees.
6de9cd9a
DN
30
31 To understand the dominator walker one must first have a grasp of dominators,
32 immediate dominators and the dominator tree.
33
34 Dominators
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.
39
40 Immediate Dominators:
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.
44
45 Dominator tree:
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.
49
50
51 [ Note this walker can also walk the post-dominator tree, which is
454ff5cb 52 defined in a similar manner. i.e., block B1 is said to post-dominate
6de9cd9a
DN
53 block B2 if all paths from B2 to the exit block must pass through
54 B1. ]
55
56 For example, given the CFG
57
58 1
59 |
60 2
61 / \
62 3 4
63 / \
64 +---------->5 6
65 | / \ /
66 | +--->8 7
67 | | / |
68 | +--9 11
69 | / |
70 +--- 10 ---> 12
b8698a0f
L
71
72
6de9cd9a
DN
73 We have a dominator tree which looks like
74
75 1
76 |
77 2
78 / \
79 / \
80 3 4
81 / / \ \
82 | | | |
83 5 6 7 12
84 | |
85 8 11
86 |
87 9
88 |
89 10
b8698a0f
L
90
91
92
6de9cd9a
DN
93 The dominator tree is the basis for a number of analysis, transformation
94 and optimization algorithms that operate on a semi-global basis.
b8698a0f 95
6de9cd9a
DN
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.
b8698a0f 100
6de9cd9a
DN
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,
b8698a0f 104 one after visiting the dominator children. There is a callback
6de9cd9a
DN
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.
b8698a0f 108
6de9cd9a
DN
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.
b8698a0f 113
6de9cd9a
DN
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).
b8698a0f 117
6de9cd9a
DN
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.
121
110abdbc 122 And (of course), we use the dominator walker to drive our dominator
6de9cd9a
DN
123 optimizer, which is a semi-global optimizer.
124
125 TODO:
126
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. */
130
076b4605 131static int
51007dc1 132cmp_bb_postorder (const void *a, const void *b, void *data)
076b4605 133{
e5df270e
AM
134 basic_block bb1 = *(const basic_block *)(a);
135 basic_block bb2 = *(const basic_block *)(b);
51007dc1 136 int *bb_postorder = (int *)data;
076b4605 137 /* Place higher completion number first (pop off lower number first). */
e5df270e
AM
138 return bb_postorder[bb2->index] - bb_postorder[bb1->index];
139}
140
141/* Permute array BBS of N basic blocks in postorder,
142 i.e. by descending number in BB_POSTORDER array. */
143
144static void
51007dc1 145sort_bbs_postorder (basic_block *bbs, int n, int *bb_postorder)
e5df270e
AM
146{
147 if (__builtin_expect (n == 2, true))
148 {
149 basic_block bb0 = bbs[0], bb1 = bbs[1];
150 if (bb_postorder[bb0->index] < bb_postorder[bb1->index])
151 bbs[0] = bb1, bbs[1] = bb0;
152 }
153 else if (__builtin_expect (n == 3, true))
154 {
155 basic_block bb0 = bbs[0], bb1 = bbs[1], bb2 = bbs[2];
156 if (bb_postorder[bb0->index] < bb_postorder[bb1->index])
157 std::swap (bb0, bb1);
158 if (bb_postorder[bb1->index] < bb_postorder[bb2->index])
159 {
160 std::swap (bb1, bb2);
161 if (bb_postorder[bb0->index] < bb_postorder[bb1->index])
162 std::swap (bb0, bb1);
163 }
164 bbs[0] = bb0, bbs[1] = bb1, bbs[2] = bb2;
165 }
166 else
51007dc1 167 gcc_sort_r (bbs, n, sizeof *bbs, cmp_bb_postorder, bb_postorder);
076b4605
RB
168}
169
9972bbbc
DM
170/* Set EDGE_EXECUTABLE on every edge within FN's CFG. */
171
172void
173set_all_edges_as_executable (function *fn)
174{
175 basic_block bb;
176 FOR_ALL_BB_FN (bb, fn)
177 {
178 edge_iterator ei;
179 edge e;
180 FOR_EACH_EDGE (e, ei, bb->succs)
181 e->flags |= EDGE_EXECUTABLE;
182 }
183}
184
185/* Constructor for a dom walker. */
3daacdcd 186
3daacdcd 187dom_walker::dom_walker (cdi_direction direction,
9972bbbc 188 enum reachability reachability,
d2552094 189 int *bb_index_to_rpo)
3daacdcd 190 : m_dom_direction (direction),
e37240b0
RB
191 m_reachability (reachability),
192 m_user_bb_to_rpo (bb_index_to_rpo != NULL),
d2552094
RB
193 m_unreachable_dom (NULL),
194 m_bb_to_rpo (bb_index_to_rpo)
3daacdcd 195{
3daacdcd
JL
196}
197
d2552094
RB
198/* Destructor. */
199
200dom_walker::~dom_walker ()
201{
202 if (! m_user_bb_to_rpo)
203 free (m_bb_to_rpo);
204}
205
3daacdcd
JL
206/* Return TRUE if BB is reachable, false otherwise. */
207
208bool
209dom_walker::bb_reachable (struct function *fun, basic_block bb)
210{
211 /* If we're not skipping unreachable blocks, then assume everything
212 is reachable. */
e37240b0 213 if (m_reachability == ALL_BLOCKS)
3daacdcd
JL
214 return true;
215
216 /* If any of the predecessor edges that do not come from blocks dominated
217 by us are still marked as possibly executable consider this block
218 reachable. */
219 bool reachable = false;
220 if (!m_unreachable_dom)
221 {
222 reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (fun);
223 edge_iterator ei;
224 edge e;
225 FOR_EACH_EDGE (e, ei, bb->preds)
226 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
227 reachable |= (e->flags & EDGE_EXECUTABLE);
228 }
229
230 return reachable;
231}
232
233/* BB has been determined to be unreachable. Propagate that property
234 to incoming and outgoing edges of BB as appropriate. */
235
236void
237dom_walker::propagate_unreachable_to_edges (basic_block bb,
238 FILE *dump_file,
1a817418 239 dump_flags_t dump_flags)
3daacdcd
JL
240{
241 if (dump_file && (dump_flags & TDF_DETAILS))
242 fprintf (dump_file, "Marking all outgoing edges of unreachable "
243 "BB %d as not executable\n", bb->index);
244
245 edge_iterator ei;
246 edge e;
247 FOR_EACH_EDGE (e, ei, bb->succs)
248 e->flags &= ~EDGE_EXECUTABLE;
249
250 FOR_EACH_EDGE (e, ei, bb->preds)
251 {
252 if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
253 {
254 if (dump_file && (dump_flags & TDF_DETAILS))
255 fprintf (dump_file, "Marking backedge from BB %d into "
256 "unreachable BB %d as not executable\n",
257 e->src->index, bb->index);
258 e->flags &= ~EDGE_EXECUTABLE;
259 }
260 }
261
262 if (!m_unreachable_dom)
263 m_unreachable_dom = bb;
264}
265
d2552094
RB
266const edge dom_walker::STOP = (edge)-1;
267
6de9cd9a 268/* Recursively walk the dominator tree.
6de9cd9a
DN
269 BB is the basic block we are currently visiting. */
270
271void
4d9192b5 272dom_walker::walk (basic_block bb)
6de9cd9a 273{
e37240b0
RB
274 /* Compute the basic-block index to RPO mapping lazily. */
275 if (!m_bb_to_rpo
276 && m_dom_direction == CDI_DOMINATORS)
277 {
278 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
279 int postorder_num = pre_and_rev_post_order_compute (NULL, postorder,
280 true);
281 m_bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
282 for (int i = 0; i < postorder_num; ++i)
283 m_bb_to_rpo[postorder[i]] = i;
284 free (postorder);
285 }
286
287 /* Set up edge flags if need be. */
288 if (m_reachability == REACHABLE_BLOCKS)
289 set_all_edges_as_executable (cfun);
290
6de9cd9a 291 basic_block dest;
0cae8d31
DM
292 basic_block *worklist = XNEWVEC (basic_block,
293 n_basic_blocks_for_fn (cfun) * 2);
df648b94 294 int sp = 0;
0bca51f0 295
df648b94 296 while (true)
6de9cd9a 297 {
df648b94 298 /* Don't worry about unreachable blocks. */
515f36eb 299 if (EDGE_COUNT (bb->preds) > 0
fefa31b5
DM
300 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
301 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
6de9cd9a 302 {
d2552094 303 edge taken_edge = NULL;
3daacdcd 304
4d9192b5
TS
305 /* Callback for subclasses to do custom things before we have walked
306 the dominator children, but before we walk statements. */
3daacdcd
JL
307 if (this->bb_reachable (cfun, bb))
308 {
d2552094
RB
309 taken_edge = before_dom_children (bb);
310 if (taken_edge && taken_edge != STOP)
3daacdcd
JL
311 {
312 edge_iterator ei;
313 edge e;
314 FOR_EACH_EDGE (e, ei, bb->succs)
315 if (e != taken_edge)
316 e->flags &= ~EDGE_EXECUTABLE;
317 }
318 }
319 else
320 propagate_unreachable_to_edges (bb, dump_file, dump_flags);
df648b94
JH
321
322 /* Mark the current BB to be popped out of the recursion stack
fa10beec 323 once children are processed. */
df648b94
JH
324 worklist[sp++] = bb;
325 worklist[sp++] = NULL;
326
d2552094
RB
327 /* If the callback returned NONE then we are supposed to
328 stop and not even propagate EDGE_EXECUTABLE further. */
329 if (taken_edge != STOP)
330 {
331 int saved_sp = sp;
332 for (dest = first_dom_son (m_dom_direction, bb);
333 dest; dest = next_dom_son (m_dom_direction, dest))
334 worklist[sp++] = dest;
dc3b4a20
RB
335 /* Sort worklist after RPO order if requested. */
336 if (sp - saved_sp > 1
337 && m_dom_direction == CDI_DOMINATORS
338 && m_bb_to_rpo)
51007dc1
RB
339 sort_bbs_postorder (&worklist[saved_sp], sp - saved_sp,
340 m_bb_to_rpo);
d2552094 341 }
6de9cd9a 342 }
ccf5c864 343 /* NULL is used to mark pop operations in the recursion stack. */
df648b94 344 while (sp > 0 && !worklist[sp - 1])
6de9cd9a 345 {
df648b94
JH
346 --sp;
347 bb = worklist[--sp];
df648b94 348
4d9192b5
TS
349 /* Callback allowing subclasses to do custom things after we have
350 walked dominator children, but before we walk statements. */
3daacdcd
JL
351 if (bb_reachable (cfun, bb))
352 after_dom_children (bb);
353 else if (m_unreachable_dom == bb)
354 m_unreachable_dom = NULL;
6de9cd9a 355 }
df648b94 356 if (sp)
076b4605 357 bb = worklist[--sp];
6de9cd9a 358 else
df648b94 359 break;
6de9cd9a 360 }
df648b94 361 free (worklist);
6de9cd9a 362}