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