remove unused files
[platform/upstream/gcc48.git] / gcc / domwalk.c
1 /* Generic dominator tree walker
2    Copyright (C) 2003-2013 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 "basic-block.h"
26 #include "domwalk.h"
27 #include "sbitmap.h"
28
29 /* This file implements a generic walker for dominator trees.
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
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
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
71
72
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
90
91
92
93   The dominator tree is the basis for a number of analysis, transformation
94   and optimization algorithms that operate on a semi-global basis.
95
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.
100
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.
108
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.
113
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).
117
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
122   And (of course), we use the dominator walker to drive our dominator
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
131 /* Recursively walk the dominator tree.
132
133    WALK_DATA contains a set of callbacks to perform pass-specific
134    actions during the dominator walk as well as a stack of block local
135    data maintained during the dominator walk.
136
137    BB is the basic block we are currently visiting.  */
138
139 void
140 walk_dominator_tree (struct dom_walk_data *walk_data, basic_block bb)
141 {
142   void *bd = NULL;
143   basic_block dest;
144   basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks * 2);
145   int sp = 0;
146   sbitmap visited = sbitmap_alloc (last_basic_block + 1);
147   bitmap_clear (visited);
148   bitmap_set_bit (visited, ENTRY_BLOCK_PTR->index);
149
150   while (true)
151     {
152       /* Don't worry about unreachable blocks.  */
153       if (EDGE_COUNT (bb->preds) > 0
154           || bb == ENTRY_BLOCK_PTR
155           || bb == EXIT_BLOCK_PTR)
156         {
157           /* Callback to initialize the local data structure.  */
158           if (walk_data->initialize_block_local_data)
159             {
160               bool recycled;
161
162               /* First get some local data, reusing any local data
163                  pointer we may have saved.  */
164               if (walk_data->free_block_data.length () > 0)
165                 {
166                   bd = walk_data->free_block_data.pop ();
167                   recycled = 1;
168                 }
169               else
170                 {
171                   bd = xcalloc (1, walk_data->block_local_data_size);
172                   recycled = 0;
173                 }
174
175               /* Push the local data into the local data stack.  */
176               walk_data->block_data_stack.safe_push (bd);
177
178               /* Call the initializer.  */
179               walk_data->initialize_block_local_data (walk_data, bb,
180                                                       recycled);
181
182             }
183
184           /* Callback for operations to execute before we have walked the
185              dominator children, but before we walk statements.  */
186           if (walk_data->before_dom_children)
187             (*walk_data->before_dom_children) (walk_data, bb);
188
189           bitmap_set_bit (visited, bb->index);
190
191           /* Mark the current BB to be popped out of the recursion stack
192              once children are processed.  */
193           worklist[sp++] = bb;
194           worklist[sp++] = NULL;
195
196           for (dest = first_dom_son (walk_data->dom_direction, bb);
197                dest; dest = next_dom_son (walk_data->dom_direction, dest))
198             worklist[sp++] = dest;
199         }
200       /* NULL is used to mark pop operations in the recursion stack.  */
201       while (sp > 0 && !worklist[sp - 1])
202         {
203           --sp;
204           bb = worklist[--sp];
205
206           /* Callback for operations to execute after we have walked the
207              dominator children, but before we walk statements.  */
208           if (walk_data->after_dom_children)
209             (*walk_data->after_dom_children) (walk_data, bb);
210
211           if (walk_data->initialize_block_local_data)
212             {
213               /* And finally pop the record off the block local data stack.  */
214               bd = walk_data->block_data_stack.pop ();
215               /* And save the block data so that we can re-use it.  */
216               walk_data->free_block_data.safe_push (bd);
217             }
218         }
219       if (sp)
220         {
221           int spp;
222           spp = sp - 1;
223           if (walk_data->dom_direction == CDI_DOMINATORS)
224             /* Find the dominator son that has all its predecessors
225                visited and continue with that.  */
226             while (1)
227               {
228                 edge_iterator ei;
229                 edge e;
230                 bool found = true;
231                 bb = worklist[spp];
232                 FOR_EACH_EDGE (e, ei, bb->preds)
233                   {
234                     if (!dominated_by_p (CDI_DOMINATORS, e->src, e->dest)
235                         && !bitmap_bit_p (visited, e->src->index))
236                       {
237                         found = false;
238                         break;
239                       }
240                   }
241                 if (found)
242                   break;
243                 /* If we didn't find a dom child with all visited
244                    predecessors just use the candidate we were checking.
245                    This happens for candidates in irreducible loops.  */
246                 if (!worklist[spp - 1])
247                   break;
248                 --spp;
249               }
250           bb = worklist[spp];
251           worklist[spp] = worklist[--sp];
252         }
253       else
254         break;
255     }
256   free (worklist);
257   sbitmap_free (visited);
258 }
259
260 void
261 init_walk_dominator_tree (struct dom_walk_data *walk_data)
262 {
263   walk_data->free_block_data.create (0);
264   walk_data->block_data_stack.create (0);
265 }
266
267 void
268 fini_walk_dominator_tree (struct dom_walk_data *walk_data)
269 {
270   if (walk_data->initialize_block_local_data)
271     {
272       while (walk_data->free_block_data.length () > 0)
273         free (walk_data->free_block_data.pop ());
274     }
275
276   walk_data->free_block_data.release ();
277   walk_data->block_data_stack.release ();
278 }