make code a bit more readble
[libfirm] / ir / be / beirgmod.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6
7 #ifdef HAVE_MALLOC_H
8  #include <malloc.h>
9 #endif
10 #ifdef HAVE_ALLOCA_H
11  #include <alloca.h>
12 #endif
13
14 #include "hashptr.h"
15 #include "pdeq.h"
16 #include "pset.h"
17 #include "pmap.h"
18 #include "util.h"
19 #include "debug.h"
20
21 #include "irflag_t.h"
22 #include "ircons_t.h"
23 #include "irnode_t.h"
24 #include "ircons_t.h"
25 #include "irmode_t.h"
26 #include "irdom_t.h"
27 #include "iredges_t.h"
28 #include "irgopt.h"
29 #include "irprintf_t.h"
30 #include "irgwalk.h"
31
32 #include "be_t.h"
33 #include "bechordal_t.h"
34 #include "bearch.h"
35 #include "besched_t.h"
36 #include "belive_t.h"
37 #include "benode_t.h"
38 #include "beutil.h"
39 #include "beinsn_t.h"
40
41 #include "beirgmod.h"
42
43 #define DBG_MODULE "firm.be.irgmod"
44 #define DBG_LEVEL SET_LEVEL_0
45
46 /*
47   ____                  _
48  |  _ \  ___  _ __ ___ (_)_ __   __ _ _ __   ___ ___
49  | | | |/ _ \| '_ ` _ \| | '_ \ / _` | '_ \ / __/ _ \
50  | |_| | (_) | | | | | | | | | | (_| | | | | (_|  __/
51  |____/ \___/|_| |_| |_|_|_| |_|\__,_|_| |_|\___\___|
52  |  ___| __ ___  _ __ | |_(_) ___ _ __ ___
53  | |_ | '__/ _ \| '_ \| __| |/ _ \ '__/ __|
54  |  _|| | | (_) | | | | |_| |  __/ |  \__ \
55  |_|  |_|  \___/|_| |_|\__|_|\___|_|  |___/
56
57 */
58
59 /**
60  * The dominance frontier for a graph.
61  */
62 struct _be_dom_front_info_t {
63         pmap *df_map;         /**< A map, mapping every block to a list of its dominance frontier blocks. */
64         struct obstack obst;  /**< An obstack holding all the frontier data. */
65 };
66
67 /**
68  * A wrapper for get_Block_idom.
69  * This function returns the block itself, if the block is the start
70  * block. Returning NULL would make any != comparison true which
71  * suggests, that the start block is dominated by some other node.
72  * @param bl The block.
73  * @return The immediate dominator of the block.
74  */
75 static INLINE ir_node *get_idom(ir_node *bl)
76 {
77         ir_node *idom = get_Block_idom(bl);
78         return idom == NULL ? bl : idom;
79 }
80
81 /**
82  * Compute the dominance frontier for a given block.
83  *
84  * @param blk   the block where the calculation starts
85  *
86  * @return the list of all blocks in the dominance frontier of blk
87  */
88 static ir_node **compute_df(ir_node *blk, be_dom_front_info_t *info)
89 {
90         ir_node *c;
91         const ir_edge_t *edge;
92         ir_node **df_list = NEW_ARR_F(ir_node *, 0);
93         ir_node **df;
94         int len;
95
96         /* Add local dominance frontiers */
97         foreach_block_succ(blk, edge) {
98                 ir_node *y = edge->src;
99
100                 if (get_idom(y) != blk) {
101                         ARR_APP1(ir_node *, df_list, y);
102                 }
103         }
104
105         /*
106          * Go recursively down the dominance tree and add all blocks
107          * into the dominance frontiers of the children, which are not
108          * dominated by the given block.
109          */
110         for (c = get_Block_dominated_first(blk); c; c = get_Block_dominated_next(c)) {
111                 int i;
112                 ir_node **df_c_list = compute_df(c, info);
113
114                 for (i = ARR_LEN(df_c_list) - 1; i >= 0; --i) {
115                         ir_node *w = df_c_list[i];
116                         if (get_idom(w) != blk)
117                                 ARR_APP1(ir_node *, df_list, w);
118                 }
119         }
120
121         /* now copy the flexible array to the obstack */
122         len = ARR_LEN(df_list);
123         df = NEW_ARR_D(ir_node *, &info->obst, len);
124         memcpy(df, df_list, len * sizeof(df[0]));
125         DEL_ARR_F(df_list);
126
127         pmap_insert(info->df_map, blk, df);
128         return df;
129 }
130
131 be_dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
132 {
133         be_dom_front_info_t *info = xmalloc(sizeof(*info));
134
135         edges_assure(irg);
136         obstack_init(&info->obst);
137         info->df_map = pmap_create();
138         assure_doms(irg);
139         (void)compute_df(get_irg_start_block(irg), info);
140
141         return info;
142 }
143
144 void be_free_dominance_frontiers(be_dom_front_info_t *info)
145 {
146         obstack_free(&info->obst, NULL);
147         pmap_destroy(info->df_map);
148         free(info);
149 }
150
151 /* Get the dominance frontier of a block. */
152 ir_node **be_get_dominance_frontier(be_dom_front_info_t *info, ir_node *block)
153 {
154         return pmap_get(info->df_map, block);
155 }
156
157 static void determine_phi_blocks(pset *copies, pset *copy_blocks, pset *phi_blocks, be_dom_front_info_t *df_info)
158 {
159         ir_node *bl;
160         waitq *worklist = new_waitq();
161         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
162
163         /*
164          * Fill the worklist queue and the rest of the orig blocks array.
165          */
166         for (bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
167                 waitq_put(worklist, bl);
168         }
169
170         while (!pdeq_empty(worklist)) {
171                 ir_node *bl  = waitq_get(worklist);
172                 ir_node **df = be_get_dominance_frontier(df_info, bl);
173                 int i;
174
175                 ir_node *y;
176
177                 DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
178                 DEBUG_ONLY(
179                         for (i = ARR_LEN(df) - 1; i >= 0; --i)
180                                 DBG((dbg, LEVEL_3, "\t%+F\n", df[i]))
181                 );
182
183                 for (i = ARR_LEN(df) - 1; i >= 0; --i) {
184                         y = df[i];
185                         if (!pset_find_ptr(phi_blocks, y)) {
186                                 pset_insert_ptr(phi_blocks, y);
187
188                                 /*
189                                  * Clear the link field of a possible phi block, since
190                                  * the possibly created phi will be stored there. See,
191                                  * search_def()
192                                  */
193                                 set_irn_link(y, NULL);
194
195                                 if(!pset_find_ptr(copy_blocks, y))
196                                         waitq_put(worklist, y);
197                         }
198                 }
199         }
200
201         del_waitq(worklist);
202 }
203
204 /*
205   ____ ____    _
206  / ___/ ___|  / \
207  \___ \___ \ / _ \
208   ___) |__) / ___ \
209  |____/____/_/   \_\
210    ____                _                   _   _
211   / ___|___  _ __  ___| |_ _ __ _   _  ___| |_(_) ___  _ __
212  | |   / _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \
213  | |__| (_) | | | \__ \ |_| |  | |_| | (__| |_| | (_) | | | |
214   \____\___/|_| |_|___/\__|_|   \__,_|\___|\__|_|\___/|_| |_|
215
216 */
217
218 /**
219  * Find the copy of the given original node whose value is 'active'
220  * at a usage.
221  *
222  * The usage is given as a node and a position. Initially, the given operand
223  * points to a node for which copies were introduced. We have to find
224  * the valid copy for this usage. This is done by traversing the
225  * dominance tree upwards. If the usage is a phi function, we start
226  * traversing from the predecessor block which corresponds to the phi
227  * usage.
228  *
229  * @param usage       The node which uses the original node.
230  * @param pos         The position of the argument which corresponds to the original node.
231  * @param copies      A set containing all node which are copies from the original node.
232  * @param copy_blocks A set containing all basic block in which copies of the original node are located.
233  * @param phis        A set where all created phis are recorded.
234  * @param phi_blocks  A set of all blocks where Phis shall be inserted (iterated dominance frontier).
235  * @param mode        The mode for the Phi if one has to be created.
236  * @return            The valid copy for usage.
237  */
238 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phis, pset *phi_blocks, ir_mode *mode)
239 {
240         ir_node *curr_bl;
241         ir_node *start_irn;
242         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
243
244         curr_bl = get_nodes_block(usage);
245
246         DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
247         /*
248          * If the usage is in a phi node, search the copy in the
249          * predecessor denoted by pos.
250          */
251         if(is_Phi(usage)) {
252                 curr_bl = get_Block_cfgpred_block(curr_bl, pos);
253                 start_irn = sched_last(curr_bl);
254         } else {
255                 start_irn = sched_prev(usage);
256         }
257
258         /*
259          * Traverse the dominance tree upwards from the
260          * predecessor block of the usage.
261          */
262         while(curr_bl != NULL) {
263                 ir_node *phim;
264
265             /*
266                  * If this block contains a copy, search the block
267                  * instruction by instruction. If nothing is found
268                  * search for a not scheduled PhiM.
269                  */
270                 if(pset_find_ptr(copy_blocks, curr_bl)) {
271                         ir_node *irn;
272
273                         /* Look at each instruction from last to first. */
274                         sched_foreach_reverse_from(start_irn, irn) {
275
276                                 /* Take the first copy we find. */
277                                 if(pset_find_ptr(copies, irn))
278                                         return irn;
279                         }
280
281                         for(phim = pset_first(copies); phim; phim = pset_next(copies)) {
282                                 if(!is_Phi(phim) || !(get_irn_mode(phim) == mode_M))
283                                         continue;
284
285                                 if(get_nodes_block(phim) == curr_bl) {
286                                         pset_break(copies);
287                                         return phim;
288                                 }
289                         }
290                 }
291
292                 if(pset_find_ptr(phi_blocks, curr_bl)) {
293                         ir_node *phi = get_irn_link(curr_bl);
294
295                         if(phi == NULL) {
296                                 int i, n_preds = get_irn_arity(curr_bl);
297                                 ir_graph *irg = get_irn_irg(curr_bl);
298                                 ir_node **ins = alloca(n_preds * sizeof(ins[0]));
299
300                                 for(i = 0; i < n_preds; ++i)
301                                         ins[i] = new_r_Bad(irg);
302
303                                 phi = new_r_Phi(irg, curr_bl, n_preds, ins, mode);
304                                 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, curr_bl));
305
306                                 set_irn_link(curr_bl, phi);
307                                 if(mode != mode_M)
308                                         sched_add_after(curr_bl, phi);
309
310                                 for(i = 0; i < n_preds; ++i) {
311                                         ir_node *arg = search_def(phi, i, copies, copy_blocks, phis, phi_blocks, mode);
312                                         if(arg == NULL) {
313                                                 ir_node *irn;
314
315                                                 ir_fprintf(stderr, "no definition found for %+F at position %d\nCopies: ", phi, i);
316                                                 for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
317                                                         ir_fprintf(stderr, "%+F ", irn);
318                                                 }
319                                                 ir_fprintf(stderr, "\n\n");
320                                                 assert(arg && "no definition found");
321                                         }
322                                         DBG((dbg, LEVEL_2, "\t\t%+F(%d) -> %+F\n", phi, i, arg));
323                                         set_irn_n(phi, i, arg);
324                                 }
325
326                                 if(phis != NULL)
327                                         pset_insert_ptr(phis, phi);
328                         }
329
330                         return phi;
331                 }
332
333                 /* If were not done yet, look in the immediate dominator */
334                 curr_bl = get_Block_idom(curr_bl);
335                 if(curr_bl)
336                         start_irn = sched_last(curr_bl);
337         }
338
339         return NULL;
340 }
341
342 static void fix_usages(pset *copies, pset *copy_blocks, pset *phi_blocks, pset *phis, pset *ignore_uses)
343 {
344         int n_outs = 0;
345         struct obstack obst;
346         ir_node *irn;
347         int i;
348         struct out {
349                 ir_node *irn;
350                 int pos;
351         } *outs;
352
353         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
354
355         obstack_init(&obst);
356
357         /*
358          * Put all outs into an array.
359          * This is necessary, since the outs would be modified while
360          * iterating on them what could bring the outs module in trouble.
361          */
362         for (irn = pset_first(copies); irn; irn = pset_next(copies)) {
363                 const ir_edge_t *edge;
364                 foreach_out_edge(irn, edge) {
365                         if (!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
366                                 struct out tmp;
367                                 tmp.irn = get_edge_src_irn(edge);
368                                 tmp.pos = get_edge_src_pos(edge);
369                                 obstack_grow(&obst, &tmp, sizeof(tmp));
370                                 n_outs++;
371                         }
372                 }
373         }
374         outs = obstack_finish(&obst);
375
376         /*
377          * Search the valid def for each out and set it.
378          */
379         for(i = 0; i < n_outs; ++i) {
380                 ir_node *irn  = outs[i].irn;
381                 int pos       = outs[i].pos;
382                 ir_mode *mode = get_irn_mode(get_irn_n(irn, pos));
383
384                 ir_node *def;
385
386                 def = search_def(irn, pos, copies, copy_blocks, phis, phi_blocks, mode);
387                 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
388
389                 if(def == NULL) {
390                         ir_fprintf(stderr, "no definition found for %+F at position %d\nCopies: ", irn, pos);
391                         for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
392                                 ir_fprintf(stderr, "%+F ", irn);
393                         }
394                         ir_fprintf(stderr, "\n\n");
395                         assert(def && "no definition found");
396                 }
397                 set_irn_n(irn, pos, def);
398         }
399
400         obstack_free(&obst, NULL);
401 }
402
403 #if 0
404 /**
405  * Remove phis which are not necessary.
406  * During place_phi_functions() phi functions are put on the dominance
407  * frontiers blindly. However some of them will never be used (these
408  * have at least one predecessor which is NULL, see search_def() for
409  * this case). Since place_phi_functions() enters them into the
410  * schedule, we have to remove them from there.
411  *
412  * @param copies The set of all copies made (including the phi functions).
413  */
414 static void remove_odd_phis(pset *copies, pset *unused_copies)
415 {
416         ir_node *irn;
417
418         for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
419                 if(is_Phi(irn)) {
420                         int i, n;
421                         int illegal = 0;
422
423                         assert(sched_is_scheduled(irn) && "phi must be scheduled");
424                         for(i = 0, n = get_irn_arity(irn); i < n && !illegal; ++i)
425                                 illegal = get_irn_n(irn, i) == NULL;
426
427                         if(illegal) {
428                                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
429                                         set_irn_n(irn, i, new_Bad());
430                                 sched_remove(irn);
431                         }
432                 }
433         }
434
435         for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
436                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
437                         set_irn_n(irn, i, new_Bad());
438                 sched_remove(irn);
439         }
440 }
441 #endif
442
443 void be_ssa_constr_phis_ignore(be_dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[], pset *phis, pset *ignore_uses)
444 {
445         pset *irns = pset_new_ptr(n);
446         int i;
447
448         for(i = 0; i < n; ++i)
449                 pset_insert_ptr(irns, nodes[i]);
450         be_ssa_constr_set_phis_ignore(info, lv, irns, phis, ignore_uses);
451         del_pset(irns);
452 }
453
454 void be_ssa_constr_ignore(be_dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[], pset *ignore_uses)
455 {
456         be_ssa_constr_phis_ignore(info, lv, n, nodes, NULL, ignore_uses);
457 }
458
459 void be_ssa_constr(be_dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[])
460 {
461         pset *empty_set = be_empty_set();
462         be_ssa_constr_ignore(info, lv, n, nodes, empty_set);
463 }
464
465 void be_ssa_constr_set_phis_ignore(be_dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *phis, pset *ignore_uses)
466 {
467         int n                  = pset_count(nodes);
468         pset *blocks           = pset_new_ptr(n);
469         pset *phi_blocks       = pset_new_ptr(n);
470         int save_optimize      = get_optimize();
471         int save_normalize     = get_opt_normalize();
472         int phis_set_created   = 0;
473         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
474
475         ir_node *irn;
476
477         /* We need to collect the phi functions to compute their liveness. */
478         if(lv && !phis) {
479                 phis_set_created = 1;
480                 phis = pset_new_ptr_default();
481         }
482
483         DBG((dbg, LEVEL_1, "Introducing following copies for:\n"));
484
485         /* Fill the sets. */
486         for(irn = pset_first(nodes); irn; irn = pset_next(nodes)) {
487                 ir_node *bl = get_nodes_block(irn);
488                 pset_insert_ptr(blocks, bl);
489                 DBG((dbg, LEVEL_1, "\t%+F in %+F\n", irn, bl));
490         }
491
492         /*
493          * Disable optimization so that the phi functions do not
494          * disappear.
495          */
496         set_optimize(0);
497         set_opt_normalize(0);
498
499         /*
500          * Place the phi functions and reroute the usages.
501          */
502         determine_phi_blocks(nodes, blocks, phi_blocks, df);
503         fix_usages(nodes, blocks, phi_blocks, phis, ignore_uses);
504
505         /* reset the optimizations */
506         set_optimize(save_optimize);
507         set_opt_normalize(save_normalize);
508
509         del_pset(phi_blocks);
510         del_pset(blocks);
511
512         /* Recompute the liveness (if wanted) of the original nodes, the copies and the inserted phis. */
513         if(lv) {
514 #if 1
515                 foreach_pset(nodes, irn)
516                         be_liveness_update(lv, irn);
517
518                 foreach_pset(phis, irn)
519                         be_liveness_introduce(lv, irn);
520 #else
521                 be_liveness_recompute(lv);
522 #endif
523         }
524
525         /* Free the phi set of we created it. */
526         if(phis_set_created)
527                 del_pset(phis);
528
529 }
530
531 void be_ssa_constr_set_phis(be_dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *phis)
532 {
533         pset *empty_set = be_empty_set();
534         be_ssa_constr_set_phis_ignore(df, lv, nodes, phis, empty_set);
535 }
536
537 void be_ssa_constr_set_ignore(be_dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *ignore_uses)
538 {
539         be_ssa_constr_set_phis_ignore(df, lv, nodes, NULL, ignore_uses);
540 }
541
542 void be_ssa_constr_set(be_dom_front_info_t *info, be_lv_t *lv, pset *nodes)
543 {
544         pset *empty_set = be_empty_set();
545         be_ssa_constr_set_ignore(info, lv, nodes, empty_set);
546 }
547
548 /*
549   ___                     _     ____
550  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
551   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
552   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
553  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
554
555 */
556
557 ir_node *insert_Perm_after(const arch_env_t *arch_env,
558                                                    be_lv_t *lv,
559                                                    const arch_register_class_t *cls,
560                                                    be_dom_front_info_t *dom_front,
561                                                    ir_node *pos)
562 {
563         ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
564         ir_graph *irg   = get_irn_irg(bl);
565         pset *live      = pset_new_ptr_default();
566         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, "be.node");
567
568         ir_node *curr, *irn, *perm, **nodes;
569         int i, n;
570
571         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
572
573         be_liveness_nodes_live_at(lv, arch_env, cls, pos, live);
574
575         n = pset_count(live);
576
577         if(n == 0) {
578                 del_pset(live);
579                 return NULL;
580         }
581
582         nodes = xmalloc(n * sizeof(nodes[0]));
583
584         DBG((dbg, LEVEL_1, "live:\n"));
585         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
586                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
587                 nodes[i] = irn;
588         }
589         del_pset(live);
590
591         perm = be_new_Perm(cls, irg, bl, n, nodes);
592         sched_add_after(pos, perm);
593         free(nodes);
594
595         curr = perm;
596         for (i = 0; i < n; ++i) {
597                 ir_node *copies[2];
598                 ir_node *perm_op = get_irn_n(perm, i);
599                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
600
601                 ir_mode *mode = get_irn_mode(perm_op);
602                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
603                 arch_set_irn_register(arch_env, proj, reg);
604
605                 sched_add_after(curr, proj);
606                 curr = proj;
607
608                 copies[0] = perm_op;
609                 copies[1] = proj;
610
611                 be_ssa_constr(dom_front, lv, 2, copies);
612         }
613
614         return perm;
615 }
616
617 struct _elr_closure_t {
618         struct obstack obst;
619         const be_chordal_env_t *cenv;
620 };
621
622 static void elr_split_walker(ir_node *bl, void *data)
623 {
624         struct _elr_closure_t *c     = data;
625         const be_chordal_env_t *cenv = c->cenv;
626         const arch_env_t *aenv       = cenv->birg->main_env->arch_env;
627         be_lv_t *lv                  = cenv->birg->lv;
628         be_dom_front_info_t *dom_front = cenv->birg->dom_front;
629         be_insn_t *insn;
630         be_insn_env_t ie;
631
632         be_insn_env_init(&ie, cenv->birg, cenv->cls, &c->obst);
633
634         for(insn = be_scan_insn(&ie, sched_first(bl)); !is_Block(insn->irn); insn = be_scan_insn(&ie, insn->next_insn)) {
635                 ir_node *pred = sched_prev(insn->irn);
636                 if(!is_Block(pred) && !is_Phi(insn->irn))
637                         insert_Perm_after(aenv, lv, cenv->cls, dom_front, insn->irn);
638         }
639 }
640
641 void extreme_liverange_splitting(struct _be_chordal_env_t *cenv)
642 {
643         struct _elr_closure_t c;
644         be_lv_t *lv = cenv->birg->lv;
645
646         c.cenv = cenv;
647         obstack_init(&c.obst);
648         be_liveness_recompute(lv);
649         irg_block_walk_graph(cenv->irg, elr_split_walker, NULL, &c);
650         be_liveness_recompute(lv);
651         obstack_free(&c.obst, NULL);
652 }
653
654 /**
655  * Post-block-walker: Find blocks containing only one jump and
656  * remove them.
657  */
658 static void remove_empty_block(ir_node *block, void *data) {
659         const ir_edge_t *edge, *next;
660         ir_node *node;
661         int *changed = data;
662         ir_node *jump = NULL;
663
664         assert(is_Block(block));
665
666         if (get_Block_n_cfgpreds(block) != 1)
667                 return;
668
669         sched_foreach(block, node) {
670                 if (! is_Jmp(node))
671                         return;
672                 if (jump != NULL) {
673                         /* we should never have 2 jumps in a block */
674                         assert(0 && "We should never have 2 jumps in a block");
675                         return;
676                 }
677                 jump = node;
678         }
679
680         if (jump == NULL)
681                 return;
682
683         node = get_Block_cfgpred(block, 0);
684         foreach_out_edge_safe(jump, edge, next) {
685                 ir_node *block = get_edge_src_irn(edge);
686                 int     pos    = get_edge_src_pos(edge);
687
688                 set_irn_n(block, pos, node);
689         }
690
691         set_Block_cfgpred(block, 0, new_Bad());
692         sched_remove(jump);
693         *changed = 1;
694 }
695
696 /* removes basic blocks that just contain a jump instruction */
697 int be_remove_empty_blocks(ir_graph *irg) {
698         int changed = 0;
699
700         irg_block_walk_graph(irg, remove_empty_block, NULL, &changed);
701         if (changed) {
702                 /* invalidate analysis info */
703                 set_irg_doms_inconsistent(irg);
704                 set_irg_extblk_inconsistent(irg);
705                 set_irg_outs_inconsistent(irg);
706         }
707         return changed;
708 }