Fix the way simplifiable node are colored when alignment is in the game
[libfirm] / ir / ana / irextbb.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Extended basis block support.
23  * @author   Michael Beck
24  * @date     5.2005
25  */
26 #include "config.h"
27
28 #include "irextbb_t.h"
29 #include "irgwalk.h"
30 #include "irnode_t.h"
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
33 #include "irouts.h"
34 #include "xmalloc.h"
35 #include "irprintf.h"
36
37 /**
38  * Environment for extbb construction.
39  */
40 typedef struct env {
41         struct obstack *obst;   /**< the obstack where allocations took place */
42         ir_extblk *head;        /**< head of the list of all extended blocks */
43         ir_node *start_block;   /**< the start block of the current graph */
44 } env_t;
45
46 int (is_ir_extbb)(const void *thing)
47 {
48         return _is_ir_extbb(thing);
49 }
50
51 /**
52  * allocate a new extended block header.
53  */
54 static void allocate_extblk(ir_node *block, env_t *env)
55 {
56         ir_extblk *extblk = OALLOC(env->obst, ir_extblk);
57
58         extblk->kind    = k_ir_extblk;
59         extblk->visited = 1;
60         extblk->blks    = (ir_node **)env->head;
61         extblk->link    = block;
62         env->head       = extblk;
63
64         set_Block_extbb(block, extblk);
65         set_irn_link(block, NULL);
66 }
67
68 /**
69  * Returns the number of block successors.
70  * we are interested only in 1, 2 and >2.
71  */
72 static int get_block_n_succs(ir_node *block)
73 {
74         if (edges_activated(current_ir_graph)) {
75                 const ir_edge_t *edge;
76
77                 edge = get_block_succ_first(block);
78                 if (! edge)
79                         return 0;
80                 edge = get_block_succ_next(block, edge);
81                 if (! edge)
82                         return 1;
83                 edge = get_block_succ_next(block, edge);
84                 return edge ? 3 : 2;
85         }
86         return get_Block_n_cfg_outs(block);
87 }
88
89 /**
90  * Pre block-walker. Calculates the extended block info.
91  */
92 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
93 {
94         int n = get_Block_n_cfgpreds(block);
95         env_t *env = (env_t*) ctx;
96
97         if (n <= 0 || n > 1 || block == env->start_block) {
98                 /*
99                  * block is a JOIN-node ie the control flow from
100                  * many other blocks joins here. block is a leader.
101                  * Note that we handle unreachable blocks (n <= 0) here too.
102                  */
103                 allocate_extblk(block, env);
104         }
105         else {    /* we have only one control flow predecessor */
106                 ir_node *add_to = get_Block_cfgpred_block(block, 0);
107
108                 /* blocks with only one BAD predecessors are leaders too */
109                 if (is_Bad(add_to)) {
110                         allocate_extblk(block, env);
111                 } else {
112                         /*
113                          * Only one control flow predecessor. This block belongs
114                          * to the same extended basic block as its predecessor.
115                          */
116                         ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
117
118                         if (!irn_visited_else_mark(cf_op)) {
119                                 ir_node *pred_bl = get_nodes_block(cf_op);
120                                 if (get_block_n_succs(pred_bl) > 2) {
121                                         /* More than two successors means we have a jump table.
122                                          * we cannot include a jump target into the current extended
123                                          * basic block, so create a new one here.
124                                          */
125                                         allocate_extblk(block, env);
126                                 } else {
127                                         /* either the previous block has only one successor or
128                                          * this is the first successor after an if, include it.
129                                          */
130                                         set_Block_extbb(block, NULL);
131                                 }
132                         } else {
133                                 /* already marked, so begin a new extended block here */
134                                 allocate_extblk(block, env);
135                         }
136                 }
137         }
138 }
139
140 /** A special extended block used as sentinel */
141 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD, NULL, NULL };
142
143 /**
144  * Post block-walker. Calculates the extended block info.
145  * During construction, we use the (free) block input of all basic blocks
146  * to point to their previous block.
147  */
148 static void post_walk_calc_extbb(ir_node *block, void *ctx)
149 {
150         ir_extblk *extbb = get_Block_extbb(block);
151         env_t *env = (env_t*) ctx;
152         ir_extblk *sentinel = &_sentinel;
153
154         if (! extbb) {
155                 ir_node *curr, *prev, *list;
156
157                 /*
158                  * Search the leader. It can happen, that we fall into an endless
159                  * loop, because we enter an unreachable loop that is not yet detected.
160                  * We break the loop using a sentinel.
161                  */
162                 for (curr = block; !extbb; curr = prev) {
163                         prev = get_Block_cfgpred_block(curr, 0);
164                         extbb = get_Block_extbb(prev);
165                         set_Block_extbb(curr, sentinel);
166                 }
167
168                 if (extbb == sentinel) {
169                         /* We detect a dead loop. We fix this by allocating a
170                          * special Extended block
171                          */
172                         ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
173
174                         allocate_extblk(block, env);
175                         extbb = get_Block_extbb(block);
176                         set_Block_extbb(block, sentinel);
177                 }
178
179                 /* replace all sentinels by the extbb info */
180                 prev = block;
181                 list = NULL;
182                 for (;;) {
183                         if (get_Block_extbb(prev) != sentinel)
184                                 break;
185                         set_irn_link(prev, list);
186                         list = prev;
187                         prev = get_Block_cfgpred_block(prev, 0);
188                 }
189                 /* arg, the list is in wrong order, turn around and add to the extbb list */
190                 for (curr = list; curr; curr = prev) {
191                         prev = (ir_node*) get_irn_link(curr);
192                         set_irn_link(curr, extbb->link);
193                         extbb->link = curr;
194                         set_Block_extbb(curr, extbb);
195                         ++extbb->visited;
196                 }
197         }
198 }
199
200 void compute_extbb(ir_graph *irg)
201 {
202         env_t env;
203         ir_extblk *extbb, *next;
204
205         if (irg->extbb_obst)
206                 obstack_free(irg->extbb_obst, NULL);
207         else
208                 irg->extbb_obst = XMALLOC(struct obstack);
209         obstack_init(irg->extbb_obst);
210
211         env.obst        = irg->extbb_obst;
212         env.head        = NULL;
213         env.start_block = get_irg_start_block(irg);
214
215         if (! edges_activated(irg)) {
216                 /* we don't have edges */
217                 assure_irg_outs(irg);
218         }
219
220         /* we must mark nodes, so increase the visited flag */
221         inc_irg_visited(irg);
222         irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
223
224         /*
225          * Ok, we have now the list of all extended blocks starting with env.head
226          * every extended block "knowns" the number of blocks in visited and
227          * the blocks are linked in link.
228          * Now we can create arrays that hold the blocks, some kind of "out" edges
229          * for the extended block
230          */
231         for (extbb = env.head; extbb; extbb = next) {
232                 int i, len = (int)extbb->visited;
233                 ir_node *block;
234
235                 next = (ir_extblk *)extbb->blks;
236
237                 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
238
239                 for (block = (ir_node*) extbb->link, i = 0; i < len; ++i) {
240                         ir_node *nblock = (ir_node*) get_irn_link(block);
241
242                         /* ensure that the leader is the first one */
243                         extbb->blks[len - 1 - i] = block;
244                         set_irn_link(block, NULL);
245                         block = nblock;
246                 }
247
248 #ifndef NDEBUG
249                 /* check it */
250                 for (i = len - 1; i > 0; --i) {
251                         ir_node *blk = extbb->blks[i];
252
253                         if (get_Block_n_cfgpreds(blk) != 1) {
254                                 assert(!"Block for more than one predecessors is no leader");
255                         } else if (get_Block_cfgpred_block(blk, 0) != extbb->blks[i - 1]) {
256                                 assert(!"extbb block order wrong");
257                         }
258                 }
259 #endif
260
261                 extbb->link    = NULL;
262                 extbb->visited = 0;
263         }
264
265         set_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
266 }
267
268 void free_extbb(ir_graph *irg)
269 {
270         if (irg->extbb_obst) {
271                 obstack_free(irg->extbb_obst, NULL);
272                 xfree(irg->extbb_obst);
273                 irg->extbb_obst = NULL;
274         }
275         clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
276 }
277
278 ir_extblk *get_nodes_extbb(const ir_node *node)
279 {
280         const ir_node *block = is_Block(node) ? node : get_nodes_block(node);
281         return get_Block_extbb(block);
282 }
283
284 ir_visited_t (get_extbb_visited)(const ir_extblk *blk)
285 {
286         return _get_extbb_visited(blk);
287 }
288
289 void (set_extbb_visited)(ir_extblk *blk, ir_visited_t visited)
290 {
291         _set_extbb_visited(blk, visited);
292 }
293
294 void (mark_extbb_visited)(ir_extblk *blk)
295 {
296         _mark_extbb_visited(blk);
297 }
298
299 int (extbb_visited)(const ir_extblk *blk)
300 {
301         return _extbb_visited(blk);
302 }
303
304 int (extbb_not_visited)(const ir_extblk *blk)
305 {
306         return _extbb_not_visited(blk);
307 }
308
309 void *(get_extbb_link)(const ir_extblk *blk)
310 {
311         return _get_extbb_link(blk);
312 }
313
314 void (set_extbb_link)(ir_extblk *blk, void *link)
315 {
316         _set_extbb_link(blk, link);
317 }
318
319 int (get_extbb_n_blocks)(const ir_extblk *blk)
320 {
321         return _get_extbb_n_blocks(blk);
322 }
323
324 ir_node *(get_extbb_block)(const ir_extblk *blk, int pos)
325 {
326         return _get_extbb_block(blk, pos);
327 }
328
329 ir_node *(get_extbb_leader)(const ir_extblk *blk)
330 {
331         return _get_extbb_leader(blk);
332 }
333
334 long get_extbb_node_nr(const ir_extblk *blk)
335 {
336         return get_irn_node_nr(get_extbb_leader(blk));
337 }
338
339 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
340 {
341         int i;
342         ir_node *node;
343
344         if (extbb_not_visited(blk)) {
345                 mark_extbb_visited(blk);
346
347                 if (pre) pre(blk, env);
348
349                 node = get_extbb_leader(blk);
350                 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
351                         /* find the corresponding predecessor block. */
352                         ir_node *pred = get_Block_cfgpred_block(node, i);
353                         if (is_Block(pred)) {
354                                 /* recursion */
355                                 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
356                         }
357                         else {
358                                 assert(is_Bad(pred));
359                         }
360                 }
361
362                 if (post) post(blk, env);
363         }
364 }
365
366 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
367 {
368         ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
369         ir_extblk *start_blk = get_Block_extbb(start_bl);
370         int i;
371
372         assert(blk);
373         inc_irg_block_visited(current_ir_graph);
374
375         /* assure the start block is the first one */
376         mark_extbb_visited(start_blk);
377         if (post)
378                 post(start_blk, env);
379         irg_extblock_walk_2(blk, pre, post, env);
380
381         /* keepalive: the endless loops ... */
382         if (blk == get_Block_extbb(get_irg_end_block(current_ir_graph))) {
383                 ir_node *node = get_irg_end(current_ir_graph);
384                 int arity = get_irn_arity(node);
385                 for (i = 0; i < arity; i++) {
386                         pred = get_irn_n(node, i);
387                         if (is_Block(pred))
388                                 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
389                         else if (is_Phi(pred)) {
390                                 /* Sometimes the blocks died, but are still reachable through Phis.
391                                  * Make sure the algorithms that try to remove these reach them. */
392                                 ir_node *block = get_nodes_block(pred);
393
394                                 if (! is_Bad(block))
395                                         irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
396                         }
397                 }
398         }
399
400         if (pre)
401                 pre(start_blk, env);
402 }
403
404 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
405 {
406         ir_node *endbl = get_irg_end_block(irg);
407         ir_extblk *blk = get_Block_extbb(endbl);
408         ir_graph *rem  = current_ir_graph;
409         current_ir_graph = irg;
410         irg_extblock_walk(blk, pre, post, env);
411         current_ir_graph = rem;
412 }