]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/domwalk.c
coretypes.h: Include machmode.h...
[thirdparty/gcc.git] / gcc / domwalk.c
1 /* Generic dominator tree walker
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "predict.h"
26 #include "vec.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "hard-reg-set.h"
30 #include "input.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "cfganal.h"
35 #include "basic-block.h"
36 #include "domwalk.h"
37 #include "sbitmap.h"
38
39 /* This file implements a generic walker for dominator trees.
40
41 To understand the dominator walker one must first have a grasp of dominators,
42 immediate dominators and the dominator tree.
43
44 Dominators
45 A block B1 is said to dominate B2 if every path from the entry to B2 must
46 pass through B1. Given the dominance relationship, we can proceed to
47 compute immediate dominators. Note it is not important whether or not
48 our definition allows a block to dominate itself.
49
50 Immediate Dominators:
51 Every block in the CFG has no more than one immediate dominator. The
52 immediate dominator of block BB must dominate BB and must not dominate
53 any other dominator of BB and must not be BB itself.
54
55 Dominator tree:
56 If we then construct a tree where each node is a basic block and there
57 is an edge from each block's immediate dominator to the block itself, then
58 we have a dominator tree.
59
60
61 [ Note this walker can also walk the post-dominator tree, which is
62 defined in a similar manner. i.e., block B1 is said to post-dominate
63 block B2 if all paths from B2 to the exit block must pass through
64 B1. ]
65
66 For example, given the CFG
67
68 1
69 |
70 2
71 / \
72 3 4
73 / \
74 +---------->5 6
75 | / \ /
76 | +--->8 7
77 | | / |
78 | +--9 11
79 | / |
80 +--- 10 ---> 12
81
82
83 We have a dominator tree which looks like
84
85 1
86 |
87 2
88 / \
89 / \
90 3 4
91 / / \ \
92 | | | |
93 5 6 7 12
94 | |
95 8 11
96 |
97 9
98 |
99 10
100
101
102
103 The dominator tree is the basis for a number of analysis, transformation
104 and optimization algorithms that operate on a semi-global basis.
105
106 The dominator walker is a generic routine which visits blocks in the CFG
107 via a depth first search of the dominator tree. In the example above
108 the dominator walker might visit blocks in the following order
109 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
110
111 The dominator walker has a number of callbacks to perform actions
112 during the walk of the dominator tree. There are two callbacks
113 which walk statements, one before visiting the dominator children,
114 one after visiting the dominator children. There is a callback
115 before and after each statement walk callback. In addition, the
116 dominator walker manages allocation/deallocation of data structures
117 which are local to each block visited.
118
119 The dominator walker is meant to provide a generic means to build a pass
120 which can analyze or transform/optimize a function based on walking
121 the dominator tree. One simply fills in the dominator walker data
122 structure with the appropriate callbacks and calls the walker.
123
124 We currently use the dominator walker to prune the set of variables
125 which might need PHI nodes (which can greatly improve compile-time
126 performance in some cases).
127
128 We also use the dominator walker to rewrite the function into SSA form
129 which reduces code duplication since the rewriting phase is inherently
130 a walk of the dominator tree.
131
132 And (of course), we use the dominator walker to drive our dominator
133 optimizer, which is a semi-global optimizer.
134
135 TODO:
136
137 Walking statements is based on the block statement iterator abstraction,
138 which is currently an abstraction over walking tree statements. Thus
139 the dominator walker is currently only useful for trees. */
140
141 static int *bb_postorder;
142
143 static int
144 cmp_bb_postorder (const void *a, const void *b)
145 {
146 basic_block bb1 = *(basic_block *)const_cast<void *>(a);
147 basic_block bb2 = *(basic_block *)const_cast<void *>(b);
148 if (bb1->index == bb2->index)
149 return 0;
150 /* Place higher completion number first (pop off lower number first). */
151 if (bb_postorder[bb1->index] > bb_postorder[bb2->index])
152 return -1;
153 return 1;
154 }
155
156 /* Recursively walk the dominator tree.
157 BB is the basic block we are currently visiting. */
158
159 void
160 dom_walker::walk (basic_block bb)
161 {
162 basic_block dest;
163 basic_block *worklist = XNEWVEC (basic_block,
164 n_basic_blocks_for_fn (cfun) * 2);
165 int sp = 0;
166 int *postorder, postorder_num;
167
168 if (m_dom_direction == CDI_DOMINATORS)
169 {
170 postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
171 postorder_num = inverted_post_order_compute (postorder);
172 bb_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
173 for (int i = 0; i < postorder_num; ++i)
174 bb_postorder[postorder[i]] = i;
175 free (postorder);
176 }
177
178 while (true)
179 {
180 /* Don't worry about unreachable blocks. */
181 if (EDGE_COUNT (bb->preds) > 0
182 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
183 || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
184 {
185 /* Callback for subclasses to do custom things before we have walked
186 the dominator children, but before we walk statements. */
187 before_dom_children (bb);
188
189 /* Mark the current BB to be popped out of the recursion stack
190 once children are processed. */
191 worklist[sp++] = bb;
192 worklist[sp++] = NULL;
193
194 int saved_sp = sp;
195 for (dest = first_dom_son (m_dom_direction, bb);
196 dest; dest = next_dom_son (m_dom_direction, dest))
197 worklist[sp++] = dest;
198 if (m_dom_direction == CDI_DOMINATORS)
199 switch (sp - saved_sp)
200 {
201 case 0:
202 case 1:
203 break;
204 default:
205 qsort (&worklist[saved_sp], sp - saved_sp,
206 sizeof (basic_block), cmp_bb_postorder);
207 }
208 }
209 /* NULL is used to mark pop operations in the recursion stack. */
210 while (sp > 0 && !worklist[sp - 1])
211 {
212 --sp;
213 bb = worklist[--sp];
214
215 /* Callback allowing subclasses to do custom things after we have
216 walked dominator children, but before we walk statements. */
217 after_dom_children (bb);
218 }
219 if (sp)
220 bb = worklist[--sp];
221 else
222 break;
223 }
224 if (m_dom_direction == CDI_DOMINATORS)
225 {
226 free (bb_postorder);
227 bb_postorder = NULL;
228 }
229 free (worklist);
230 }