Fixed critical edges construction: Ignore real exception flow, handle regular flow.
[libfirm] / ir / ir / irgopt.c
1 /*
2  * Copyright (C) 1995-2007 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    Optimizations for a whole ir graph, i.e., a procedure.
23  * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
24  *           Michael Beck
25  * @version  $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include "irnode_t.h"
34 #include "irgraph_t.h"
35 #include "irprog_t.h"
36
37 #include "iroptimize.h"
38 #include "ircons_t.h"
39 #include "iropt_t.h"
40 #include "irgopt.h"
41 #include "irgmod.h"
42 #include "irgwalk.h"
43
44 #include "array.h"
45 #include "pset.h"
46 #include "pmap.h"
47 #include "pdeq.h"       /* Fuer code placement */
48 #include "xmalloc.h"
49
50 #include "irouts.h"
51 #include "irloop_t.h"
52 #include "irbackedge_t.h"
53 #include "cgana.h"
54 #include "trouts.h"
55 #include "error.h"
56
57 #include "irflag_t.h"
58 #include "irhooks.h"
59 #include "iredges_t.h"
60 #include "irtools.h"
61
62 /*------------------------------------------------------------------*/
63 /* apply optimizations of iropt to all nodes.                       */
64 /*------------------------------------------------------------------*/
65
66 /**
67  * A wrapper around optimize_inplace_2() to be called from a walker.
68  */
69 static void optimize_in_place_wrapper (ir_node *n, void *env) {
70         ir_node *optimized = optimize_in_place_2(n);
71         (void) env;
72
73         if (optimized != n) {
74                 exchange (n, optimized);
75         }
76 }
77
78 /**
79  * Do local optimizations for a node.
80  *
81  * @param n  the IR-node where to start. Typically the End node
82  *           of a graph
83  *
84  * @note current_ir_graph must be set
85  */
86 static INLINE void do_local_optimize(ir_node *n) {
87         /* Handle graph state */
88         assert(get_irg_phase_state(current_ir_graph) != phase_building);
89
90         if (get_opt_global_cse())
91         set_irg_pinned(current_ir_graph, op_pin_state_floats);
92         set_irg_outs_inconsistent(current_ir_graph);
93         set_irg_doms_inconsistent(current_ir_graph);
94         set_irg_loopinfo_inconsistent(current_ir_graph);
95
96         /* Clean the value_table in irg for the CSE. */
97         del_identities(current_ir_graph->value_table);
98         current_ir_graph->value_table = new_identities();
99
100         /* walk over the graph */
101         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
102 }
103
104 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
105 void local_optimize_node(ir_node *n) {
106         ir_graph *rem = current_ir_graph;
107         current_ir_graph = get_irn_irg(n);
108
109         do_local_optimize(n);
110
111         current_ir_graph = rem;
112 }
113
114 /**
115  * Block-Walker: uses dominance depth to mark dead blocks.
116  */
117 static void kill_dead_blocks(ir_node *block, void *env) {
118         (void) env;
119
120         if (get_Block_dom_depth(block) < 0) {
121                 /*
122                  * Note that the new dominance code correctly handles
123                  * the End block, i.e. it is always reachable from Start
124                  */
125                 set_Block_dead(block);
126         }
127 }
128
129 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
130 void local_optimize_graph(ir_graph *irg) {
131         ir_graph *rem = current_ir_graph;
132         current_ir_graph = irg;
133
134         if (get_irg_dom_state(irg) == dom_consistent)
135                 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
136
137         do_local_optimize(get_irg_end(irg));
138
139         current_ir_graph = rem;
140 }
141
142 /**
143  * Enqueue all users of a node to a wait queue.
144  * Handles mode_T nodes.
145  */
146 static void enqueue_users(ir_node *n, pdeq *waitq) {
147         const ir_edge_t *edge;
148
149         foreach_out_edge(n, edge) {
150                 ir_node *succ = get_edge_src_irn(edge);
151
152                 if (get_irn_link(succ) != waitq) {
153                         pdeq_putr(waitq, succ);
154                         set_irn_link(succ, waitq);
155                 }
156                 if (get_irn_mode(succ) == mode_T) {
157                 /* A mode_T node has Proj's. Because most optimizations
158                         run on the Proj's we have to enqueue them also. */
159                         enqueue_users(succ, waitq);
160                 }
161         }
162 }
163
164 /**
165  * Data flow optimization walker.
166  * Optimizes all nodes and enqueue it's users
167  * if done.
168  */
169 static void opt_walker(ir_node *n, void *env) {
170         pdeq *waitq = env;
171         ir_node *optimized;
172
173         optimized = optimize_in_place_2(n);
174         set_irn_link(optimized, NULL);
175
176         if (optimized != n) {
177                 enqueue_users(n, waitq);
178                 exchange(n, optimized);
179         }
180 }
181
182 /* Applies local optimizations to all nodes in the graph until fixpoint. */
183 void optimize_graph_df(ir_graph *irg) {
184         pdeq     *waitq = new_pdeq();
185         ir_graph *rem = current_ir_graph;
186         ir_node  *end;
187         int      i, state;
188
189         current_ir_graph = irg;
190
191         state = edges_assure(irg);
192
193         if (get_opt_global_cse())
194                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
195
196         /* Clean the value_table in irg for the CSE. */
197         del_identities(irg->value_table);
198         irg->value_table = new_identities();
199
200         if (get_irg_dom_state(irg) == dom_consistent)
201                 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
202
203         /* invalidate info */
204         set_irg_outs_inconsistent(irg);
205         set_irg_doms_inconsistent(irg);
206         set_irg_loopinfo_inconsistent(irg);
207
208         set_using_irn_link(irg);
209
210         /* walk over the graph, but don't touch keep-alives */
211         irg_walk(get_irg_end_block(irg), NULL, opt_walker, waitq);
212
213         end = get_irg_end(irg);
214
215         /* optimize keep-alives by removing superfluous ones */
216         for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
217                 ir_node *ka = get_End_keepalive(end, i);
218
219                 if (irn_visited(ka) && !is_irn_keep(ka)) {
220                         /* this node can be regularly visited, no need to keep it */
221                         set_End_keepalive(end, i, get_irg_bad(irg));
222                 }
223         }
224         /* now walk again and visit all not yet visited nodes */
225         set_irg_visited(current_ir_graph, get_irg_visited(irg) - 1);
226         irg_walk(get_irg_end(irg), NULL, opt_walker, waitq);
227
228         /* finish the wait queue */
229         while (! pdeq_empty(waitq)) {
230                 ir_node *n = pdeq_getl(waitq);
231                 if (! is_Bad(n))
232                         opt_walker(n, waitq);
233         }
234
235         del_pdeq(waitq);
236
237         clear_using_irn_link(irg);
238
239         if (! state)
240                 edges_deactivate(irg);
241
242         current_ir_graph = rem;
243 }
244
245
246 /*------------------------------------------------------------------*/
247 /* Routines for dead node elimination / copying garbage collection  */
248 /* of the obstack.                                                  */
249 /*------------------------------------------------------------------*/
250
251 /**
252  * Remember the new node in the old node by using a field all nodes have.
253  */
254 #define set_new_node(oldn, newn)  set_irn_link(oldn, newn)
255
256 /**
257  * Get this new node, before the old node is forgotten.
258  */
259 #define get_new_node(oldn) get_irn_link(oldn)
260
261 /**
262  * Check if a new node was set.
263  */
264 #define has_new_node(n) (get_new_node(n) != NULL)
265
266 /**
267  * We use the block_visited flag to mark that we have computed the
268  * number of useful predecessors for this block.
269  * Further we encode the new arity in this flag in the old blocks.
270  * Remembering the arity is useful, as it saves a lot of pointer
271  * accesses.  This function is called for all Phi and Block nodes
272  * in a Block.
273  */
274 static INLINE int
275 compute_new_arity(ir_node *b) {
276         int i, res, irn_arity;
277         int irg_v, block_v;
278
279         irg_v = get_irg_block_visited(current_ir_graph);
280         block_v = get_Block_block_visited(b);
281         if (block_v >= irg_v) {
282                 /* we computed the number of preds for this block and saved it in the
283                    block_v flag */
284                 return block_v - irg_v;
285         } else {
286                 /* compute the number of good predecessors */
287                 res = irn_arity = get_irn_arity(b);
288                 for (i = 0; i < irn_arity; i++)
289                         if (is_Bad(get_irn_n(b, i))) res--;
290                         /* save it in the flag. */
291                         set_Block_block_visited(b, irg_v + res);
292                         return res;
293         }
294 }
295
296 /**
297  * Copies the node to the new obstack. The Ins of the new node point to
298  * the predecessors on the old obstack.  For block/phi nodes not all
299  * predecessors might be copied.  n->link points to the new node.
300  * For Phi and Block nodes the function allocates in-arrays with an arity
301  * only for useful predecessors.  The arity is determined by counting
302  * the non-bad predecessors of the block.
303  *
304  * @param n    The node to be copied
305  * @param env  if non-NULL, the node number attribute will be copied to the new node
306  *
307  * Note: Also used for loop unrolling.
308  */
309 static void copy_node(ir_node *n, void *env) {
310         ir_node *nn, *block;
311         int new_arity;
312         ir_op *op = get_irn_op(n);
313         (void) env;
314
315         /* The end node looses it's flexible in array.  This doesn't matter,
316            as dead node elimination builds End by hand, inlineing doesn't use
317            the End node. */
318         /* assert(op == op_End ||  ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC)); */
319
320         if (op == op_Bad) {
321                 /* node copied already */
322                 return;
323         } else if (op == op_Block) {
324                 block = NULL;
325                 new_arity = compute_new_arity(n);
326                 n->attr.block.graph_arr = NULL;
327         } else {
328                 block = get_nodes_block(n);
329                 if (op == op_Phi) {
330                         new_arity = compute_new_arity(block);
331                 } else {
332                         new_arity = get_irn_arity(n);
333                 }
334         }
335         nn = new_ir_node(get_irn_dbg_info(n),
336                 current_ir_graph,
337                 block,
338                 op,
339                 get_irn_mode(n),
340                 new_arity,
341                 get_irn_in(n) + 1);
342                 /* Copy the attributes.  These might point to additional data.  If this
343                 was allocated on the old obstack the pointers now are dangling.  This
344         frees e.g. the memory of the graph_arr allocated in new_immBlock. */
345         copy_node_attr(n, nn);
346
347 #ifdef DEBUG_libfirm
348         {
349                 int copy_node_nr = env != NULL;
350                 if (copy_node_nr) {
351                         /* for easier debugging, we want to copy the node numbers too */
352                         nn->node_nr = n->node_nr;
353                 }
354         }
355 #endif
356
357         set_new_node(n, nn);
358         hook_dead_node_elim_subst(current_ir_graph, n, nn);
359 }
360
361 /**
362  * Copies new predecessors of old node to new node remembered in link.
363  * Spare the Bad predecessors of Phi and Block nodes.
364  */
365 static void copy_preds(ir_node *n, void *env) {
366         ir_node *nn, *block;
367         int i, j, irn_arity;
368         (void) env;
369
370         nn = get_new_node(n);
371
372         if (is_Block(n)) {
373                 /* copy the macro block header */
374                 ir_node *mbh = get_Block_MacroBlock(n);
375
376                 if (mbh == n) {
377                         /* this block is a macroblock header */
378                         set_irn_n(nn, -1, nn);
379                 } else {
380                         /* get the macro block header */
381                         set_irn_n(nn, -1, get_new_node(mbh));
382                 }
383
384                 /* Don't copy Bad nodes. */
385                 j = 0;
386                 irn_arity = get_irn_arity(n);
387                 for (i = 0; i < irn_arity; i++) {
388                         if (! is_Bad(get_irn_n(n, i))) {
389                                 set_irn_n(nn, j, get_new_node(get_irn_n(n, i)));
390                                 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
391                                 j++;
392                         }
393                 }
394                 /* repair the block visited flag from above misuse. Repair it in both
395                    graphs so that the old one can still be used. */
396                 set_Block_block_visited(nn, 0);
397                 set_Block_block_visited(n, 0);
398                 /* Local optimization could not merge two subsequent blocks if
399                    in array contained Bads.  Now it's possible.
400                    We don't call optimize_in_place as it requires
401                    that the fields in ir_graph are set properly. */
402                 if ((get_opt_control_flow_straightening()) &&
403                         (get_Block_n_cfgpreds(nn) == 1) &&
404                         is_Jmp(get_Block_cfgpred(nn, 0))) {
405                         ir_node *old = get_nodes_block(get_Block_cfgpred(nn, 0));
406                         if (nn == old) {
407                                 /* Jmp jumps into the block it is in -- deal self cycle. */
408                                 assert(is_Bad(get_new_node(get_irg_bad(current_ir_graph))));
409                                 exchange(nn, get_new_node(get_irg_bad(current_ir_graph)));
410                         } else {
411                                 exchange(nn, old);
412                         }
413                 }
414         } else if (is_Phi(n) && get_irn_arity(n) > 0) {
415                 /* Don't copy node if corresponding predecessor in block is Bad.
416                    The Block itself should not be Bad. */
417                 block = get_nodes_block(n);
418                 set_irn_n(nn, -1, get_new_node(block));
419                 j = 0;
420                 irn_arity = get_irn_arity(n);
421                 for (i = 0; i < irn_arity; i++) {
422                         if (! is_Bad(get_irn_n(block, i))) {
423                                 set_irn_n(nn, j, get_new_node(get_irn_n(n, i)));
424                                 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
425                                 j++;
426                         }
427                 }
428                 /* If the pre walker reached this Phi after the post walker visited the
429                    block block_visited is > 0. */
430                 set_Block_block_visited(get_nodes_block(n), 0);
431                 /* Compacting the Phi's ins might generate Phis with only one
432                    predecessor. */
433                 if (get_irn_arity(nn) == 1)
434                         exchange(nn, get_irn_n(nn, 0));
435         } else {
436                 irn_arity = get_irn_arity(n);
437                 for (i = -1; i < irn_arity; i++)
438                         set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
439         }
440         /* Now the new node is complete.  We can add it to the hash table for CSE.
441            @@@ inlining aborts if we identify End. Why? */
442         if (!is_End(nn))
443                 add_identities(current_ir_graph->value_table, nn);
444 }
445
446 /**
447  * Copies the graph recursively, compacts the keep-alives of the end node.
448  *
449  * @param irg           the graph to be copied
450  * @param copy_node_nr  If non-zero, the node number will be copied
451  */
452 static void copy_graph(ir_graph *irg, int copy_node_nr) {
453         ir_node *oe, *ne, *ob, *nb, *om, *nm; /* old end, new end, old bad, new bad, old NoMem, new NoMem */
454         ir_node *ka;      /* keep alive */
455         int i, irn_arity;
456         unsigned long vfl;
457
458         /* Some nodes must be copied by hand, sigh */
459         vfl = get_irg_visited(irg);
460         set_irg_visited(irg, vfl + 1);
461
462         oe = get_irg_end(irg);
463         mark_irn_visited(oe);
464         /* copy the end node by hand, allocate dynamic in array! */
465         ne = new_ir_node(get_irn_dbg_info(oe),
466                 irg,
467                 NULL,
468                 op_End,
469                 mode_X,
470                 -1,
471                 NULL);
472         /* Copy the attributes.  Well, there might be some in the future... */
473         copy_node_attr(oe, ne);
474         set_new_node(oe, ne);
475
476         /* copy the Bad node */
477         ob = get_irg_bad(irg);
478         mark_irn_visited(ob);
479         nb = new_ir_node(get_irn_dbg_info(ob),
480                 irg,
481                 NULL,
482                 op_Bad,
483                 mode_T,
484                 0,
485                 NULL);
486         copy_node_attr(ob, nb);
487         set_new_node(ob, nb);
488
489         /* copy the NoMem node */
490         om = get_irg_no_mem(irg);
491         mark_irn_visited(om);
492         nm = new_ir_node(get_irn_dbg_info(om),
493                 irg,
494                 NULL,
495                 op_NoMem,
496                 mode_M,
497                 0,
498                 NULL);
499         copy_node_attr(om, nm);
500         set_new_node(om, nm);
501
502         /* copy the live nodes */
503         set_irg_visited(irg, vfl);
504         irg_walk(get_nodes_block(oe), copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
505
506         /* Note: from yet, the visited flag of the graph is equal to vfl + 1 */
507
508         /* visit the anchors as well */
509         for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
510                 ir_node *n = get_irg_anchor(irg, i);
511
512                 if (n && (get_irn_visited(n) <= vfl)) {
513                         set_irg_visited(irg, vfl);
514                         irg_walk(n, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
515                 }
516         }
517
518         /* copy_preds for the end node ... */
519         set_nodes_block(ne, get_new_node(get_nodes_block(oe)));
520
521         /*- ... and now the keep alives. -*/
522         /* First pick the not marked block nodes and walk them.  We must pick these
523            first as else we will oversee blocks reachable from Phis. */
524         irn_arity = get_End_n_keepalives(oe);
525         for (i = 0; i < irn_arity; i++) {
526                 ka = get_End_keepalive(oe, i);
527                 if (is_Block(ka)) {
528                         if (get_irn_visited(ka) <= vfl) {
529                                 /* We must keep the block alive and copy everything reachable */
530                                 set_irg_visited(irg, vfl);
531                                 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
532                         }
533                         add_End_keepalive(ne, get_new_node(ka));
534                 }
535         }
536
537         /* Now pick other nodes.  Here we will keep all! */
538         irn_arity = get_End_n_keepalives(oe);
539         for (i = 0; i < irn_arity; i++) {
540                 ka = get_End_keepalive(oe, i);
541                 if (!is_Block(ka)) {
542                         if (get_irn_visited(ka) <= vfl) {
543                                 /* We didn't copy the node yet.  */
544                                 set_irg_visited(irg, vfl);
545                                 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
546                         }
547                         add_End_keepalive(ne, get_new_node(ka));
548                 }
549         }
550
551         /* start block sometimes only reached after keep alives */
552         set_nodes_block(nb, get_new_node(get_nodes_block(ob)));
553         set_nodes_block(nm, get_new_node(get_nodes_block(om)));
554 }
555
556 /**
557  * Copies the graph reachable from current_ir_graph->end to the obstack
558  * in current_ir_graph and fixes the environment.
559  * Then fixes the fields in current_ir_graph containing nodes of the
560  * graph.
561  *
562  * @param copy_node_nr  If non-zero, the node number will be copied
563  */
564 static void
565 copy_graph_env(int copy_node_nr) {
566         ir_graph *irg = current_ir_graph;
567         ir_node *old_end, *new_anchor;
568         int i;
569
570         /* remove end_except and end_reg nodes */
571         old_end = get_irg_end(irg);
572         set_irg_end_except (irg, old_end);
573         set_irg_end_reg    (irg, old_end);
574
575         /* Not all nodes remembered in irg might be reachable
576            from the end node.  Assure their link is set to NULL, so that
577            we can test whether new nodes have been computed. */
578         for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
579                 ir_node *n = get_irg_anchor(irg, i);
580                 if (n != NULL)
581                         set_new_node(n, NULL);
582         }
583         /* we use the block walk flag for removing Bads from Blocks ins. */
584         inc_irg_block_visited(irg);
585
586         /* copy the graph */
587         copy_graph(irg, copy_node_nr);
588
589         /* fix the anchor */
590         old_end    = get_irg_end(irg);
591         new_anchor = new_Anchor(irg);
592
593         for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
594                 ir_node *n = get_irg_anchor(irg, i);
595                 if (n)
596                         set_irn_n(new_anchor, i, get_new_node(n));
597         }
598         free_End(old_end);
599         irg->anchor = new_anchor;
600
601         /* ensure the new anchor is placed in the endblock */
602         set_irn_n(new_anchor, -1, get_irg_end_block(irg));
603 }
604
605 /**
606  * Copies all reachable nodes to a new obstack.  Removes bad inputs
607  * from block nodes and the corresponding inputs from Phi nodes.
608  * Merges single exit blocks with single entry blocks and removes
609  * 1-input Phis.
610  * Adds all new nodes to a new hash table for CSE.  Does not
611  * perform CSE, so the hash table might contain common subexpressions.
612  */
613 void dead_node_elimination(ir_graph *irg) {
614         ir_graph *rem;
615 #ifdef INTERPROCEDURAL_VIEW
616         int rem_ipview = get_interprocedural_view();
617 #endif
618         struct obstack *graveyard_obst = NULL;
619         struct obstack *rebirth_obst   = NULL;
620         assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
621
622         /* inform statistics that we started a dead-node elimination run */
623         hook_dead_node_elim(irg, 1);
624
625         /* Remember external state of current_ir_graph. */
626         rem = current_ir_graph;
627         current_ir_graph = irg;
628 #ifdef INTERPROCEDURAL_VIEW
629         set_interprocedural_view(0);
630 #endif
631
632         assert(get_irg_phase_state(irg) != phase_building);
633
634         /* Handle graph state */
635         free_callee_info(irg);
636         free_irg_outs(irg);
637         free_trouts();
638
639         /* @@@ so far we loose loops when copying */
640         free_loop_information(irg);
641
642         set_irg_doms_inconsistent(irg);
643
644         /* A quiet place, where the old obstack can rest in peace,
645            until it will be cremated. */
646         graveyard_obst = irg->obst;
647
648         /* A new obstack, where the reachable nodes will be copied to. */
649         rebirth_obst = xmalloc(sizeof(*rebirth_obst));
650         irg->obst = rebirth_obst;
651         obstack_init(irg->obst);
652         irg->last_node_idx = 0;
653
654         /* We also need a new value table for CSE */
655         del_identities(irg->value_table);
656         irg->value_table = new_identities();
657
658         /* Copy the graph from the old to the new obstack */
659         copy_graph_env(/*copy_node_nr=*/1);
660
661         /* Free memory from old unoptimized obstack */
662         obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
663         xfree(graveyard_obst);            /* ... then free it.           */
664
665         /* inform statistics that the run is over */
666         hook_dead_node_elim(irg, 0);
667
668         current_ir_graph = rem;
669 #ifdef INTERPROCEDURAL_VIEW
670         set_interprocedural_view(rem_ipview);
671 #endif
672 }
673
674 /**
675  * Relink bad predecessors of a block and store the old in array to the
676  * link field. This function is called by relink_bad_predecessors().
677  * The array of link field starts with the block operand at position 0.
678  * If block has bad predecessors, create a new in array without bad preds.
679  * Otherwise let in array untouched.
680  */
681 static void relink_bad_block_predecessors(ir_node *n, void *env) {
682         ir_node **new_in, *irn;
683         int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
684         (void) env;
685
686         /* if link field of block is NULL, look for bad predecessors otherwise
687            this is already done */
688         if (is_Block(n) && get_irn_link(n) == NULL) {
689                 /* save old predecessors in link field (position 0 is the block operand)*/
690                 set_irn_link(n, get_irn_in(n));
691
692                 /* count predecessors without bad nodes */
693                 old_irn_arity = get_irn_arity(n);
694                 for (i = 0; i < old_irn_arity; i++)
695                         if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
696
697                         /* arity changing: set new predecessors without bad nodes */
698                         if (new_irn_arity < old_irn_arity) {
699                                 /* Get new predecessor array. We do not resize the array, as we must
700                                    keep the old one to update Phis. */
701                                 new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
702
703                                 /* set new predecessors in array */
704                                 new_in[0] = NULL;
705                                 new_irn_n = 1;
706                                 for (i = 0; i < old_irn_arity; i++) {
707                                         irn = get_irn_n(n, i);
708                                         if (!is_Bad(irn)) {
709                                                 new_in[new_irn_n] = irn;
710                                                 is_backedge(n, i) ? set_backedge(n, new_irn_n-1) : set_not_backedge(n, new_irn_n-1);
711                                                 ++new_irn_n;
712                                         }
713                                 }
714                                 /* ARR_SETLEN(int, n->attr.block.backedge, new_irn_arity); */
715                                 ARR_SHRINKLEN(n->attr.block.backedge, new_irn_arity);
716                                 n->in = new_in;
717                         } /* ir node has bad predecessors */
718         } /* Block is not relinked */
719 }
720
721 /**
722  * Relinks Bad predecessors from Blocks and Phis called by walker
723  * remove_bad_predecesors(). If n is a Block, call
724  * relink_bad_block_redecessors(). If n is a Phi-node, call also the relinking
725  * function of Phi's Block. If this block has bad predecessors, relink preds
726  * of the Phi-node.
727  */
728 static void relink_bad_predecessors(ir_node *n, void *env) {
729         ir_node *block, **old_in;
730         int i, old_irn_arity, new_irn_arity;
731
732         /* relink bad predecessors of a block */
733         if (is_Block(n))
734                 relink_bad_block_predecessors(n, env);
735
736         /* If Phi node relink its block and its predecessors */
737         if (is_Phi(n)) {
738                 /* Relink predecessors of phi's block */
739                 block = get_nodes_block(n);
740                 if (get_irn_link(block) == NULL)
741                         relink_bad_block_predecessors(block, env);
742
743                 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
744                 old_irn_arity = ARR_LEN(old_in);
745
746                 /* Relink Phi predecessors if count of predecessors changed */
747                 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
748                         /* set new predecessors in array
749                            n->in[0] remains the same block */
750                         new_irn_arity = 1;
751                         for(i = 1; i < old_irn_arity; i++)
752                                 if (!is_Bad((ir_node *)old_in[i])) {
753                                         n->in[new_irn_arity] = n->in[i];
754                                         is_backedge(n, i) ? set_backedge(n, new_irn_arity) : set_not_backedge(n, new_irn_arity);
755                                         ++new_irn_arity;
756                                 }
757
758                                 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
759                                 ARR_SETLEN(int, n->attr.phi_backedge, new_irn_arity);
760                 }
761         } /* n is a Phi node */
762 }
763
764 /*
765  * Removes Bad Bad predecessors from Blocks and the corresponding
766  * inputs to Phi nodes as in dead_node_elimination but without
767  * copying the graph.
768  * On walking up set the link field to NULL, on walking down call
769  * relink_bad_predecessors() (This function stores the old in array
770  * to the link field and sets a new in array if arity of predecessors
771  * changes).
772  */
773 void remove_bad_predecessors(ir_graph *irg) {
774         panic("Fix backedge handling first");
775         irg_walk_graph(irg, firm_clear_link, relink_bad_predecessors, NULL);
776 }
777
778
779 /*
780    __                      _  __ __
781   (_     __    o     _    | \/  |_
782   __)|_| | \_/ | \_/(/_   |_/\__|__
783
784   The following stuff implements a facility that automatically patches
785   registered ir_node pointers to the new node when a dead node elimination occurs.
786 */
787
788 struct _survive_dce_t {
789         struct obstack obst;
790         pmap *places;
791         pmap *new_places;
792         hook_entry_t dead_node_elim;
793         hook_entry_t dead_node_elim_subst;
794 };
795
796 typedef struct _survive_dce_list_t {
797         struct _survive_dce_list_t *next;
798         ir_node **place;
799 } survive_dce_list_t;
800
801 static void dead_node_hook(void *context, ir_graph *irg, int start) {
802         survive_dce_t *sd = context;
803         (void) irg;
804
805         /* Create a new map before the dead node elimination is performed. */
806         if (start) {
807                 sd->new_places = pmap_create_ex(pmap_count(sd->places));
808         } else {
809                 /* Patch back all nodes if dead node elimination is over and something is to be done. */
810                 pmap_destroy(sd->places);
811                 sd->places     = sd->new_places;
812                 sd->new_places = NULL;
813         }
814 }
815
816 /**
817  * Hook called when dead node elimination replaces old by nw.
818  */
819 static void dead_node_subst_hook(void *context, ir_graph *irg, ir_node *old, ir_node *nw) {
820         survive_dce_t *sd = context;
821         survive_dce_list_t *list = pmap_get(sd->places, old);
822         (void) irg;
823
824         /* If the node is to be patched back, write the new address to all registered locations. */
825         if (list) {
826                 survive_dce_list_t *p;
827
828                 for (p = list; p; p = p->next)
829                         *(p->place) = nw;
830
831                 pmap_insert(sd->new_places, nw, list);
832         }
833 }
834
835 /**
836  * Make a new Survive DCE environment.
837  */
838 survive_dce_t *new_survive_dce(void) {
839         survive_dce_t *res = xmalloc(sizeof(res[0]));
840         obstack_init(&res->obst);
841         res->places     = pmap_create();
842         res->new_places = NULL;
843
844         res->dead_node_elim.hook._hook_dead_node_elim = dead_node_hook;
845         res->dead_node_elim.context                   = res;
846         res->dead_node_elim.next                      = NULL;
847
848         res->dead_node_elim_subst.hook._hook_dead_node_elim_subst = dead_node_subst_hook;
849         res->dead_node_elim_subst.context = res;
850         res->dead_node_elim_subst.next    = NULL;
851
852 #ifndef FIRM_ENABLE_HOOKS
853         assert(0 && "need hooks enabled");
854 #endif
855
856         register_hook(hook_dead_node_elim, &res->dead_node_elim);
857         register_hook(hook_dead_node_elim_subst, &res->dead_node_elim_subst);
858         return res;
859 }
860
861 /**
862  * Free a Survive DCE environment.
863  */
864 void free_survive_dce(survive_dce_t *sd) {
865         obstack_free(&sd->obst, NULL);
866         pmap_destroy(sd->places);
867         unregister_hook(hook_dead_node_elim, &sd->dead_node_elim);
868         unregister_hook(hook_dead_node_elim_subst, &sd->dead_node_elim_subst);
869         xfree(sd);
870 }
871
872 /**
873  * Register a node pointer to be patched upon DCE.
874  * When DCE occurs, the node pointer specified by @p place will be
875  * patched to the new address of the node it is pointing to.
876  *
877  * @param sd    The Survive DCE environment.
878  * @param place The address of the node pointer.
879  */
880 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place) {
881         if (*place != NULL) {
882                 ir_node *irn      = *place;
883                 survive_dce_list_t *curr = pmap_get(sd->places, irn);
884                 survive_dce_list_t *nw   = obstack_alloc(&sd->obst, sizeof(nw[0]));
885
886                 nw->next  = curr;
887                 nw->place = place;
888
889                 pmap_insert(sd->places, irn, nw);
890         }
891 }
892
893 /*--------------------------------------------------------------------*/
894 /*  Functionality for inlining                                         */
895 /*--------------------------------------------------------------------*/
896
897 /**
898  * Copy node for inlineing.  Updates attributes that change when
899  * inlineing but not for dead node elimination.
900  *
901  * Copies the node by calling copy_node() and then updates the entity if
902  * it's a local one.  env must be a pointer of the frame type of the
903  * inlined procedure. The new entities must be in the link field of
904  * the entities.
905  */
906 static INLINE void
907 copy_node_inline(ir_node *n, void *env) {
908         ir_node *nn;
909         ir_type *frame_tp = (ir_type *)env;
910
911         copy_node(n, NULL);
912         if (is_Sel(n)) {
913                 nn = get_new_node (n);
914                 assert(is_Sel(nn));
915                 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
916                         set_Sel_entity(nn, get_entity_link(get_Sel_entity(n)));
917                 }
918         } else if (is_Block(n)) {
919                 nn = get_new_node (n);
920                 nn->attr.block.irg = current_ir_graph;
921         }
922 }
923
924 /**
925  * Walker: checks if P_value_arg_base is used.
926  */
927 static void find_addr(ir_node *node, void *env) {
928         int *allow_inline = env;
929         if (is_Proj(node) &&
930                         is_Start(get_Proj_pred(node)) &&
931                         get_Proj_proj(node) == pn_Start_P_value_arg_base) {
932                 *allow_inline = 0;
933         }
934 }
935
936 /**
937  * Check if we can inline a given call.
938  * Currently, we cannot inline two cases:
939  * - call with compound arguments
940  * - graphs that take the address of a parameter
941  *
942  * check these conditions here
943  */
944 static int can_inline(ir_node *call, ir_graph *called_graph) {
945         ir_type *call_type = get_Call_type(call);
946         int params, ress, i, res;
947         assert(is_Method_type(call_type));
948
949         params = get_method_n_params(call_type);
950         ress   = get_method_n_ress(call_type);
951
952         /* check parameters for compound arguments */
953         for (i = 0; i < params; ++i) {
954                 ir_type *p_type = get_method_param_type(call_type, i);
955
956                 if (is_compound_type(p_type))
957                         return 0;
958         }
959
960         /* check results for compound arguments */
961         for (i = 0; i < ress; ++i) {
962                 ir_type *r_type = get_method_res_type(call_type, i);
963
964                 if (is_compound_type(r_type))
965                         return 0;
966         }
967
968         res = 1;
969         irg_walk_graph(called_graph, find_addr, NULL, &res);
970
971         return res;
972 }
973
974 enum exc_mode {
975         exc_handler    = 0, /**< There is a handler. */
976         exc_to_end     = 1, /**< Branches to End. */
977         exc_no_handler = 2  /**< Exception handling not represented. */
978 };
979
980 /* Inlines a method at the given call site. */
981 int inline_method(ir_node *call, ir_graph *called_graph) {
982         ir_node *pre_call;
983         ir_node *post_call, *post_bl;
984         ir_node *in[pn_Start_max];
985         ir_node *end, *end_bl;
986         ir_node **res_pred;
987         ir_node **cf_pred;
988         ir_node *ret, *phi;
989         int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
990         enum exc_mode exc_handling;
991         ir_type *called_frame;
992         irg_inline_property prop = get_irg_inline_property(called_graph);
993
994         if ( (prop < irg_inline_forced) || (prop == irg_inline_forbidden))
995                 return 0;
996
997         /* Do not inline variadic functions. */
998         if (get_method_variadicity(get_entity_type(get_irg_entity(called_graph))) == variadicity_variadic)
999                 return 0;
1000
1001         assert(get_method_n_params(get_entity_type(get_irg_entity(called_graph))) ==
1002                get_method_n_params(get_Call_type(call)));
1003
1004         /*
1005          * currently, we cannot inline two cases:
1006          * - call with compound arguments
1007          * - graphs that take the address of a parameter
1008          */
1009         if (! can_inline(call, called_graph))
1010                 return 0;
1011
1012         /* --  Turn off optimizations, this can cause problems when allocating new nodes. -- */
1013         rem_opt = get_opt_optimize();
1014         set_optimize(0);
1015
1016         /* Handle graph state */
1017         assert(get_irg_phase_state(current_ir_graph) != phase_building);
1018         assert(get_irg_pinned(current_ir_graph) == op_pin_state_pinned);
1019         assert(get_irg_pinned(called_graph) == op_pin_state_pinned);
1020         set_irg_outs_inconsistent(current_ir_graph);
1021         set_irg_extblk_inconsistent(current_ir_graph);
1022         set_irg_doms_inconsistent(current_ir_graph);
1023         set_irg_loopinfo_inconsistent(current_ir_graph);
1024         set_irg_callee_info_state(current_ir_graph, irg_callee_info_inconsistent);
1025
1026         /* -- Check preconditions -- */
1027         assert(is_Call(call));
1028         /* @@@ does not work for InterfaceIII.java after cgana
1029          assert(get_Call_type(call) == get_entity_type(get_irg_entity(called_graph)));
1030          assert(smaller_type(get_entity_type(get_irg_entity(called_graph)),
1031          get_Call_type(call)));
1032         */
1033         if (called_graph == current_ir_graph) {
1034                 set_optimize(rem_opt);
1035                 return 0;
1036         }
1037
1038         /* here we know we WILL inline, so inform the statistics */
1039         hook_inline(call, called_graph);
1040
1041         /* -- Decide how to handle exception control flow: Is there a handler
1042            for the Call node, or do we branch directly to End on an exception?
1043            exc_handling:
1044            0 There is a handler.
1045            1 Branches to End.
1046            2 Exception handling not represented in Firm. -- */
1047         {
1048                 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
1049                 for (proj = get_irn_link(call); proj; proj = get_irn_link(proj)) {
1050                         long proj_nr = get_Proj_proj(proj);
1051                         if (proj_nr == pn_Call_X_except) Xproj = proj;
1052                         if (proj_nr == pn_Call_M_except) Mproj = proj;
1053                 }
1054                 if      (Mproj) { assert(Xproj); exc_handling = exc_handler; } /*  Mproj           */
1055                 else if (Xproj) {                exc_handling = exc_to_end; } /* !Mproj &&  Xproj   */
1056                 else            {                exc_handling = exc_no_handler; } /* !Mproj && !Xproj   */
1057         }
1058
1059         /* --
1060            the procedure and later replaces the Start node of the called graph.
1061            Post_call is the old Call node and collects the results of the called
1062            graph. Both will end up being a tuple.  -- */
1063         post_bl = get_nodes_block(call);
1064         set_irg_current_block(current_ir_graph, post_bl);
1065         /* XxMxPxPxPxT of Start + parameter of Call */
1066         in[pn_Start_X_initial_exec]   = new_Jmp();
1067         in[pn_Start_M]                = get_Call_mem(call);
1068         in[pn_Start_P_frame_base]     = get_irg_frame(current_ir_graph);
1069         in[pn_Start_P_globals]        = get_irg_globals(current_ir_graph);
1070         in[pn_Start_P_tls]            = get_irg_tls(current_ir_graph);
1071         in[pn_Start_T_args]           = new_Tuple(get_Call_n_params(call), get_Call_param_arr(call));
1072         /* in[pn_Start_P_value_arg_base] = ??? */
1073         assert(pn_Start_P_value_arg_base == pn_Start_max - 1 && "pn_Start_P_value_arg_base not supported, fix");
1074         pre_call = new_Tuple(pn_Start_max - 1, in);
1075         post_call = call;
1076
1077         /* --
1078            The new block gets the ins of the old block, pre_call and all its
1079            predecessors and all Phi nodes. -- */
1080         part_block(pre_call);
1081
1082         /* -- Prepare state for dead node elimination -- */
1083         /* Visited flags in calling irg must be >= flag in called irg.
1084            Else walker and arity computation will not work. */
1085         if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
1086                 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
1087         if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
1088                 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
1089         /* Set pre_call as new Start node in link field of the start node of
1090            calling graph and pre_calls block as new block for the start block
1091            of calling graph.
1092            Further mark these nodes so that they are not visited by the
1093            copying. */
1094         set_irn_link(get_irg_start(called_graph), pre_call);
1095         set_irn_visited(get_irg_start(called_graph), get_irg_visited(current_ir_graph));
1096         set_irn_link(get_irg_start_block(called_graph), get_nodes_block(pre_call));
1097         set_irn_visited(get_irg_start_block(called_graph), get_irg_visited(current_ir_graph));
1098         set_irn_link(get_irg_bad(called_graph), get_irg_bad(current_ir_graph));
1099         set_irn_visited(get_irg_bad(called_graph), get_irg_visited(current_ir_graph));
1100
1101         /* Initialize for compaction of in arrays */
1102         inc_irg_block_visited(current_ir_graph);
1103
1104         /* -- Replicate local entities of the called_graph -- */
1105         /* copy the entities. */
1106         called_frame = get_irg_frame_type(called_graph);
1107         for (i = 0; i < get_class_n_members(called_frame); i++) {
1108                 ir_entity *new_ent, *old_ent;
1109                 old_ent = get_class_member(called_frame, i);
1110                 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
1111                 set_entity_link(old_ent, new_ent);
1112         }
1113
1114         /* visited is > than that of called graph.  With this trick visited will
1115            remain unchanged so that an outer walker, e.g., searching the call nodes
1116             to inline, calling this inline will not visit the inlined nodes. */
1117         set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
1118
1119         /* -- Performing dead node elimination inlines the graph -- */
1120         /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
1121            entities. */
1122         irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
1123                  get_irg_frame_type(called_graph));
1124
1125         /* Repair called_graph */
1126         set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
1127         set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
1128         set_Block_block_visited(get_irg_start_block(called_graph), 0);
1129
1130         /* -- Merge the end of the inlined procedure with the call site -- */
1131         /* We will turn the old Call node into a Tuple with the following
1132            predecessors:
1133            -1:  Block of Tuple.
1134            0: Phi of all Memories of Return statements.
1135            1: Jmp from new Block that merges the control flow from all exception
1136            predecessors of the old end block.
1137            2: Tuple of all arguments.
1138            3: Phi of Exception memories.
1139            In case the old Call directly branches to End on an exception we don't
1140            need the block merging all exceptions nor the Phi of the exception
1141            memories.
1142         */
1143
1144         /* -- Precompute some values -- */
1145         end_bl = get_new_node(get_irg_end_block(called_graph));
1146         end = get_new_node(get_irg_end(called_graph));
1147         arity = get_irn_arity(end_bl);    /* arity = n_exc + n_ret  */
1148         n_res = get_method_n_ress(get_Call_type(call));
1149
1150         res_pred = xmalloc(n_res * sizeof(*res_pred));
1151         cf_pred  = xmalloc(arity * sizeof(*res_pred));
1152
1153         set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
1154
1155         /* -- archive keepalives -- */
1156         irn_arity = get_irn_arity(end);
1157         for (i = 0; i < irn_arity; i++) {
1158                 ir_node *ka = get_End_keepalive(end, i);
1159                 if (! is_Bad(ka))
1160                         add_End_keepalive(get_irg_end(current_ir_graph), ka);
1161         }
1162
1163         /* The new end node will die.  We need not free as the in array is on the obstack:
1164            copy_node() only generated 'D' arrays. */
1165
1166         /* -- Replace Return nodes by Jump nodes. -- */
1167         n_ret = 0;
1168         for (i = 0; i < arity; i++) {
1169                 ir_node *ret;
1170                 ret = get_irn_n(end_bl, i);
1171                 if (is_Return(ret)) {
1172                         cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_block(ret));
1173                         n_ret++;
1174                 }
1175         }
1176         set_irn_in(post_bl, n_ret, cf_pred);
1177
1178         /* -- Build a Tuple for all results of the method.
1179            Add Phi node if there was more than one Return.  -- */
1180         turn_into_tuple(post_call, pn_Call_max);
1181         /* First the Memory-Phi */
1182         n_ret = 0;
1183         for (i = 0; i < arity; i++) {
1184                 ret = get_irn_n(end_bl, i);
1185                 if (is_Return(ret)) {
1186                         cf_pred[n_ret] = get_Return_mem(ret);
1187                         n_ret++;
1188                 }
1189         }
1190         phi = new_Phi(n_ret, cf_pred, mode_M);
1191         set_Tuple_pred(call, pn_Call_M_regular, phi);
1192         /* Conserve Phi-list for further inlinings -- but might be optimized */
1193         if (get_nodes_block(phi) == post_bl) {
1194                 set_irn_link(phi, get_irn_link(post_bl));
1195                 set_irn_link(post_bl, phi);
1196         }
1197         /* Now the real results */
1198         if (n_res > 0) {
1199                 for (j = 0; j < n_res; j++) {
1200                         n_ret = 0;
1201                         for (i = 0; i < arity; i++) {
1202                                 ret = get_irn_n(end_bl, i);
1203                                 if (is_Return(ret)) {
1204                                         cf_pred[n_ret] = get_Return_res(ret, j);
1205                                         n_ret++;
1206                                 }
1207                         }
1208                         if (n_ret > 0)
1209                                 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
1210                         else
1211                                 phi = new_Bad();
1212                         res_pred[j] = phi;
1213                         /* Conserve Phi-list for further inlinings -- but might be optimized */
1214                         if (get_nodes_block(phi) == post_bl) {
1215                                 set_irn_link(phi, get_irn_link(post_bl));
1216                                 set_irn_link(post_bl, phi);
1217                         }
1218                 }
1219                 set_Tuple_pred(call, pn_Call_T_result, new_Tuple(n_res, res_pred));
1220         } else {
1221                 set_Tuple_pred(call, pn_Call_T_result, new_Bad());
1222         }
1223         /* handle the regular call */
1224         set_Tuple_pred(call, pn_Call_X_regular, new_Jmp());
1225
1226         /* For now, we cannot inline calls with value_base */
1227         set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
1228
1229         /* Finally the exception control flow.
1230            We have two (three) possible situations:
1231            First if the Call branches to an exception handler: We need to add a Phi node to
1232            collect the memory containing the exception objects.  Further we need
1233            to add another block to get a correct representation of this Phi.  To
1234            this block we add a Jmp that resolves into the X output of the Call
1235            when the Call is turned into a tuple.
1236            Second the Call branches to End, the exception is not handled.  Just
1237            add all inlined exception branches to the End node.
1238            Third: there is no Exception edge at all. Handle as case two. */
1239         if (exc_handling == exc_handler) {
1240                 n_exc = 0;
1241                 for (i = 0; i < arity; i++) {
1242                         ir_node *ret, *irn;
1243                         ret = get_irn_n(end_bl, i);
1244                         irn = skip_Proj(ret);
1245                         if (is_fragile_op(irn) || is_Raise(irn)) {
1246                                 cf_pred[n_exc] = ret;
1247                                 ++n_exc;
1248                         }
1249                 }
1250                 if (n_exc > 0) {
1251                         new_Block(n_exc, cf_pred);      /* watch it: current_block is changed! */
1252                         set_Tuple_pred(call, pn_Call_X_except, new_Jmp());
1253                         /* The Phi for the memories with the exception objects */
1254                         n_exc = 0;
1255                         for (i = 0; i < arity; i++) {
1256                                 ir_node *ret;
1257                                 ret = skip_Proj(get_irn_n(end_bl, i));
1258                                 if (is_Call(ret)) {
1259                                         cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 3);
1260                                         n_exc++;
1261                                 } else if (is_fragile_op(ret)) {
1262                                         /* We rely that all cfops have the memory output at the same position. */
1263                                         cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 0);
1264                                         n_exc++;
1265                                 } else if (is_Raise(ret)) {
1266                                         cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 1);
1267                                         n_exc++;
1268                                 }
1269                         }
1270                         set_Tuple_pred(call, pn_Call_M_except, new_Phi(n_exc, cf_pred, mode_M));
1271                 } else {
1272                         set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1273                         set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1274                 }
1275         } else {
1276                 ir_node *main_end_bl;
1277                 int main_end_bl_arity;
1278                 ir_node **end_preds;
1279
1280                 /* assert(exc_handling == 1 || no exceptions. ) */
1281                 n_exc = 0;
1282                 for (i = 0; i < arity; i++) {
1283                         ir_node *ret = get_irn_n(end_bl, i);
1284                         ir_node *irn = skip_Proj(ret);
1285
1286                         if (is_fragile_op(irn) || is_Raise(irn)) {
1287                                 cf_pred[n_exc] = ret;
1288                                 n_exc++;
1289                         }
1290                 }
1291                 main_end_bl = get_irg_end_block(current_ir_graph);
1292                 main_end_bl_arity = get_irn_arity(main_end_bl);
1293                 end_preds =  xmalloc((n_exc + main_end_bl_arity) * sizeof(*end_preds));
1294
1295                 for (i = 0; i < main_end_bl_arity; ++i)
1296                         end_preds[i] = get_irn_n(main_end_bl, i);
1297                 for (i = 0; i < n_exc; ++i)
1298                         end_preds[main_end_bl_arity + i] = cf_pred[i];
1299                 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
1300                 set_Tuple_pred(call, pn_Call_X_except,  new_Bad());
1301                 set_Tuple_pred(call, pn_Call_M_except,  new_Bad());
1302                 free(end_preds);
1303         }
1304         free(res_pred);
1305         free(cf_pred);
1306
1307         /* --  Turn CSE back on. -- */
1308         set_optimize(rem_opt);
1309
1310         return 1;
1311 }
1312
1313 /********************************************************************/
1314 /* Apply inlineing to small methods.                                */
1315 /********************************************************************/
1316
1317 /** Represents a possible inlinable call in a graph. */
1318 typedef struct _call_entry call_entry;
1319 struct _call_entry {
1320         ir_node    *call;   /**< the Call */
1321         ir_graph   *callee; /**< the callee called here */
1322         call_entry *next;   /**< for linking the next one */
1323 };
1324
1325 /**
1326  * environment for inlining small irgs
1327  */
1328 typedef struct _inline_env_t {
1329         struct obstack obst;  /**< an obstack where call_entries are allocated on. */
1330         call_entry *head;     /**< the head of the call entry list */
1331         call_entry *tail;     /**< the tail of the call entry list */
1332 } inline_env_t;
1333
1334 /**
1335  * Returns the irg called from a Call node. If the irg is not
1336  * known, NULL is returned.
1337  */
1338 static ir_graph *get_call_called_irg(ir_node *call) {
1339         ir_node *addr;
1340         ir_graph *called_irg = NULL;
1341
1342         addr = get_Call_ptr(call);
1343         if (is_SymConst(addr) && get_SymConst_kind(addr) == symconst_addr_ent) {
1344                 called_irg = get_entity_irg(get_SymConst_entity(addr));
1345         }
1346
1347         return called_irg;
1348 }
1349
1350 /**
1351  * Walker: Collect all calls to known graphs inside a graph.
1352  */
1353 static void collect_calls(ir_node *call, void *env) {
1354         if (is_Call(call)) {
1355                 ir_graph *called_irg = get_call_called_irg(call);
1356                 if (called_irg) {
1357                         /* The Call node calls a locally defined method.  Remember to inline. */
1358                         inline_env_t *ienv  = env;
1359                         call_entry   *entry = obstack_alloc(&ienv->obst, sizeof(*entry));
1360                         entry->call   = call;
1361                         entry->callee = called_irg;
1362                         entry->next   = NULL;
1363
1364                         if (ienv->tail == NULL)
1365                                 ienv->head = entry;
1366                         else
1367                                 ienv->tail->next = entry;
1368                         ienv->tail = entry;
1369                 }
1370         }
1371 }
1372
1373 /**
1374  * Inlines all small methods at call sites where the called address comes
1375  * from a Const node that references the entity representing the called
1376  * method.
1377  * The size argument is a rough measure for the code size of the method:
1378  * Methods where the obstack containing the firm graph is smaller than
1379  * size are inlined.
1380  */
1381 void inline_small_irgs(ir_graph *irg, int size) {
1382   ir_graph *rem = current_ir_graph;
1383         inline_env_t env;
1384         call_entry *entry;
1385         DEBUG_ONLY(firm_dbg_module_t *dbg;)
1386
1387         FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1388
1389         current_ir_graph = irg;
1390         /* Handle graph state */
1391         assert(get_irg_phase_state(irg) != phase_building);
1392         free_callee_info(irg);
1393
1394         /* Find Call nodes to inline.
1395            (We can not inline during a walk of the graph, as inlineing the same
1396            method several times changes the visited flag of the walked graph:
1397            after the first inlineing visited of the callee equals visited of
1398            the caller.  With the next inlineing both are increased.) */
1399         obstack_init(&env.obst);
1400         env.head = env.tail = NULL;
1401         irg_walk_graph(irg, NULL, collect_calls, &env);
1402
1403         if (env.head != NULL) {
1404                 /* There are calls to inline */
1405                 collect_phiprojs(irg);
1406                 for (entry = env.head; entry != NULL; entry = entry->next) {
1407                         ir_graph *callee = entry->callee;
1408                         if (((_obstack_memory_used(callee->obst) - (int)obstack_room(callee->obst)) < size) ||
1409                             (get_irg_inline_property(callee) >= irg_inline_forced)) {
1410                                 inline_method(entry->call, callee);
1411                         }
1412                 }
1413         }
1414         obstack_free(&env.obst, NULL);
1415         current_ir_graph = rem;
1416 }
1417
1418 /**
1419  * Environment for inlining irgs.
1420  */
1421 typedef struct {
1422   int n_nodes;             /**< Number of nodes in graph except Id, Tuple, Proj, Start, End. */
1423         int n_nodes_orig;        /**< for statistics */
1424         call_entry *call_head;   /**< The head of the list of all call nodes in this graph. */
1425         call_entry *call_tail;   /**< The tail of the list of all call nodes in this graph .*/
1426         int n_call_nodes;        /**< Number of Call nodes in the graph. */
1427         int n_call_nodes_orig;   /**< for statistics */
1428         int n_callers;           /**< Number of known graphs that call this graphs. */
1429         int n_callers_orig;      /**< for statistics */
1430         int got_inline;          /**< Set, if at leat one call inside this graph was inlined. */
1431 } inline_irg_env;
1432
1433 /**
1434  * Allocate a new environment for inlining.
1435  */
1436 static inline_irg_env *alloc_inline_irg_env(struct obstack *obst) {
1437         inline_irg_env *env    = obstack_alloc(obst, sizeof(*env));
1438         env->n_nodes           = -2; /* do not count count Start, End */
1439         env->n_nodes_orig      = -2; /* do not count Start, End */
1440         env->call_head         = NULL;
1441         env->call_tail         = NULL;
1442         env->n_call_nodes      = 0;
1443         env->n_call_nodes_orig = 0;
1444         env->n_callers         = 0;
1445         env->n_callers_orig    = 0;
1446         env->got_inline        = 0;
1447         return env;
1448 }
1449
1450 typedef struct walker_env {
1451         struct obstack *obst; /**< the obstack for allocations. */
1452         inline_irg_env *x;    /**< the inline environment */
1453         int ignore_runtime;   /**< the ignore runtime flag */
1454 } wenv_t;
1455
1456 /**
1457  * post-walker: collect all calls in the inline-environment
1458  * of a graph and sum some statistics.
1459  */
1460 static void collect_calls2(ir_node *call, void *ctx) {
1461         wenv_t         *env = ctx;
1462         inline_irg_env *x = env->x;
1463         ir_op          *op = get_irn_op(call);
1464         ir_graph       *callee;
1465         call_entry     *entry;
1466
1467         /* count meaningful nodes in irg */
1468         if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1469                 ++x->n_nodes;
1470                 ++x->n_nodes_orig;
1471         }
1472
1473         if (op != op_Call) return;
1474
1475         /* check, if it's a runtime call */
1476         if (env->ignore_runtime) {
1477                 ir_node *symc = get_Call_ptr(call);
1478
1479                 if (is_SymConst(symc) && get_SymConst_kind(symc) == symconst_addr_ent) {
1480                         ir_entity *ent = get_SymConst_entity(symc);
1481
1482                         if (get_entity_additional_properties(ent) & mtp_property_runtime)
1483                                 return;
1484                 }
1485         }
1486
1487         /* collect all call nodes */
1488         ++x->n_call_nodes;
1489         ++x->n_call_nodes_orig;
1490
1491         callee = get_call_called_irg(call);
1492         if (callee) {
1493                 inline_irg_env *callee_env = get_irg_link(callee);
1494                 /* count all static callers */
1495                 ++callee_env->n_callers;
1496                 ++callee_env->n_callers_orig;
1497
1498                 /* link it in the list of possible inlinable entries */
1499                 entry = obstack_alloc(env->obst, sizeof(*entry));
1500                 entry->call   = call;
1501                 entry->callee = callee;
1502                 entry->next   = NULL;
1503                 if (x->call_tail == NULL)
1504                         x->call_head = entry;
1505                 else
1506                         x->call_tail->next = entry;
1507                 x->call_tail = entry;
1508         }
1509 }
1510
1511 /**
1512  * Returns TRUE if the number of callers is 0 in the irg's environment,
1513  * hence this irg is a leave.
1514  */
1515 INLINE static int is_leave(ir_graph *irg) {
1516         inline_irg_env *env = get_irg_link(irg);
1517         return env->n_call_nodes == 0;
1518 }
1519
1520 /**
1521  * Returns TRUE if the number of nodes in the callee is
1522  * smaller then size in the irg's environment.
1523  */
1524 INLINE static int is_smaller(ir_graph *callee, int size) {
1525         inline_irg_env *env = get_irg_link(callee);
1526         return env->n_nodes < size;
1527 }
1528
1529 /**
1530  * Append the nodes of the list src to the nodes of the list in environment dst.
1531  */
1532 static void append_call_list(struct obstack *obst, inline_irg_env *dst, call_entry *src) {
1533         call_entry *entry, *nentry;
1534
1535         /* Note that the src list points to Call nodes in the inlined graph, but
1536            we need Call nodes in our graph. Luckily the inliner leaves this information
1537            in the link field. */
1538         for (entry = src; entry != NULL; entry = entry->next) {
1539                 nentry = obstack_alloc(obst, sizeof(*nentry));
1540                 nentry->call   = get_irn_link(entry->call);
1541                 nentry->callee = entry->callee;
1542                 nentry->next   = NULL;
1543                 dst->call_tail->next = nentry;
1544                 dst->call_tail       = nentry;
1545         }
1546 }
1547
1548 /*
1549  * Inlines small leave methods at call sites where the called address comes
1550  * from a Const node that references the entity representing the called
1551  * method.
1552  * The size argument is a rough measure for the code size of the method:
1553  * Methods where the obstack containing the firm graph is smaller than
1554  * size are inlined.
1555  */
1556 void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_runtime) {
1557         inline_irg_env   *env;
1558         ir_graph         *irg;
1559         int              i, n_irgs;
1560         ir_graph         *rem;
1561         int              did_inline;
1562         wenv_t           wenv;
1563         call_entry       *entry, *tail;
1564         const call_entry *centry;
1565         struct obstack   obst;
1566         DEBUG_ONLY(firm_dbg_module_t *dbg;)
1567
1568         FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1569         rem = current_ir_graph;
1570         obstack_init(&obst);
1571
1572         /* extend all irgs by a temporary data structure for inlining. */
1573         n_irgs = get_irp_n_irgs();
1574         for (i = 0; i < n_irgs; ++i)
1575                 set_irg_link(get_irp_irg(i), alloc_inline_irg_env(&obst));
1576
1577         /* Precompute information in temporary data structure. */
1578         wenv.obst           = &obst;
1579         wenv.ignore_runtime = ignore_runtime;
1580         for (i = 0; i < n_irgs; ++i) {
1581                 ir_graph *irg = get_irp_irg(i);
1582
1583                 assert(get_irg_phase_state(irg) != phase_building);
1584                 free_callee_info(irg);
1585
1586                 wenv.x = get_irg_link(irg);
1587                 irg_walk_graph(irg, NULL, collect_calls2, &wenv);
1588         }
1589
1590         /* -- and now inline. -- */
1591
1592         /* Inline leaves recursively -- we might construct new leaves. */
1593         do {
1594                 did_inline = 0;
1595
1596                 for (i = 0; i < n_irgs; ++i) {
1597                         ir_node *call;
1598                         int phiproj_computed = 0;
1599
1600                         current_ir_graph = get_irp_irg(i);
1601                         env = (inline_irg_env *)get_irg_link(current_ir_graph);
1602
1603                         tail = NULL;
1604                         for (entry = env->call_head; entry != NULL; entry = entry->next) {
1605                                 ir_graph *callee;
1606
1607                                 if (env->n_nodes > maxsize) break;
1608
1609                                 call   = entry->call;
1610                                 callee = entry->callee;
1611
1612                                 if (is_leave(callee) && is_smaller(callee, leavesize)) {
1613                                         if (!phiproj_computed) {
1614                                                 phiproj_computed = 1;
1615                                                 collect_phiprojs(current_ir_graph);
1616                                         }
1617                                         did_inline = inline_method(call, callee);
1618
1619                                         if (did_inline) {
1620                                                 /* Do some statistics */
1621                                                 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1622
1623                                                 env->got_inline = 1;
1624                                                 --env->n_call_nodes;
1625                                                 env->n_nodes += callee_env->n_nodes;
1626                                                 --callee_env->n_callers;
1627
1628                                                 /* remove this call from the list */
1629                                                 if (tail != NULL)
1630                                                         tail->next = entry->next;
1631                                                 else
1632                                                         env->call_head = entry->next;
1633                                                 continue;
1634                                         }
1635                                 }
1636                                 tail = entry;
1637                         }
1638                         env->call_tail = tail;
1639                 }
1640         } while (did_inline);
1641
1642         /* inline other small functions. */
1643         for (i = 0; i < n_irgs; ++i) {
1644                 ir_node *call;
1645                 int phiproj_computed = 0;
1646
1647                 current_ir_graph = get_irp_irg(i);
1648                 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1649
1650                 /* note that the list of possible calls is updated during the process */
1651                 tail = NULL;
1652                 for (entry = env->call_head; entry != NULL; entry = entry->next) {
1653                         ir_graph *callee;
1654
1655                         call   = entry->call;
1656                         callee = entry->callee;
1657
1658                         if (((is_smaller(callee, size) && (env->n_nodes < maxsize)) ||    /* small function */
1659                                 (get_irg_inline_property(callee) >= irg_inline_forced))) {
1660                                 if (!phiproj_computed) {
1661                                         phiproj_computed = 1;
1662                                         collect_phiprojs(current_ir_graph);
1663                                 }
1664                                 if (inline_method(call, callee)) {
1665                                         inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1666
1667                                         /* callee was inline. Append it's call list. */
1668                                         env->got_inline = 1;
1669                                         --env->n_call_nodes;
1670                                         append_call_list(&obst, env, callee_env->call_head);
1671                                         env->n_call_nodes += callee_env->n_call_nodes;
1672                                         env->n_nodes += callee_env->n_nodes;
1673                                         --callee_env->n_callers;
1674
1675                                         /* after we have inlined callee, all called methods inside callee
1676                                            are now called once more */
1677                                         for (centry = callee_env->call_head; centry != NULL; centry = centry->next) {
1678                                                 inline_irg_env *penv = get_irg_link(centry->callee);
1679                                                 ++penv->n_callers;
1680                                         }
1681
1682                                         /* remove this call from the list */
1683                                         if (tail != NULL)
1684                                                 tail->next = entry->next;
1685                                         else
1686                                                 env->call_head = entry->next;
1687                                         continue;
1688                                 }
1689                         }
1690                         tail = entry;
1691                 }
1692                 env->call_tail = tail;
1693         }
1694
1695         for (i = 0; i < n_irgs; ++i) {
1696                 irg = get_irp_irg(i);
1697                 env = (inline_irg_env *)get_irg_link(irg);
1698
1699                 if (env->got_inline) {
1700                         /* this irg got calls inlined */
1701                         set_irg_outs_inconsistent(irg);
1702                         set_irg_doms_inconsistent(irg);
1703
1704                         optimize_graph_df(irg);
1705                         optimize_cf(irg);
1706                 }
1707                 if (env->got_inline || (env->n_callers_orig != env->n_callers)) {
1708                         DB((dbg, SET_LEVEL_1, "Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1709                         env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1710                         env->n_callers_orig, env->n_callers,
1711                         get_entity_name(get_irg_entity(irg))));
1712                 }
1713         }
1714
1715         obstack_free(&obst, NULL);
1716         current_ir_graph = rem;
1717 }
1718
1719 /*******************************************************************/
1720 /*  Code Placement.  Pins all floating nodes to a block where they */
1721 /*  will be executed only if needed.                               */
1722 /*******************************************************************/
1723
1724 /**
1725  * Returns non-zero, is a block is not reachable from Start.
1726  *
1727  * @param block  the block to test
1728  */
1729 static int
1730 is_Block_unreachable(ir_node *block) {
1731         return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
1732 }
1733
1734 /**
1735  * Find the earliest correct block for node n.  --- Place n into the
1736  * same Block as its dominance-deepest Input.
1737  *
1738  * We have to avoid calls to get_nodes_block() here
1739  * because the graph is floating.
1740  *
1741  * move_out_of_loops() expects that place_floats_early() have placed
1742  * all "living" nodes into a living block. That's why we must
1743  * move nodes in dead block with "live" successors into a valid
1744  * block.
1745  * We move them just into the same block as it's successor (or
1746  * in case of a Phi into the effective use block). For Phi successors,
1747  * this may still be a dead block, but then there is no real use, as
1748  * the control flow will be dead later.
1749  *
1750  * @param n         the node to be placed
1751  * @param worklist  a worklist, predecessors of non-floating nodes are placed here
1752  */
1753 static void
1754 place_floats_early(ir_node *n, waitq *worklist) {
1755         int i, irn_arity;
1756
1757         /* we must not run into an infinite loop */
1758         assert(irn_not_visited(n));
1759         mark_irn_visited(n);
1760
1761         /* Place floating nodes. */
1762         if (get_irn_pinned(n) == op_pin_state_floats) {
1763                 ir_node *curr_block = get_nodes_block(n);
1764                 int in_dead_block   = is_Block_unreachable(curr_block);
1765                 int depth           = 0;
1766                 ir_node *b          = NULL;   /* The block to place this node in */
1767
1768                 assert(is_no_Block(n));
1769
1770                 if (is_irn_start_block_placed(n)) {
1771                         /* These nodes will not be placed by the loop below. */
1772                         b = get_irg_start_block(current_ir_graph);
1773                         depth = 1;
1774                 }
1775
1776                 /* find the block for this node. */
1777                 irn_arity = get_irn_arity(n);
1778                 for (i = 0; i < irn_arity; i++) {
1779                         ir_node *pred = get_irn_n(n, i);
1780                         ir_node *pred_block;
1781
1782                         if ((irn_not_visited(pred))
1783                             && (get_irn_pinned(pred) == op_pin_state_floats)) {
1784
1785                                 /*
1786                                  * If the current node is NOT in a dead block, but one of its
1787                                  * predecessors is, we must move the predecessor to a live block.
1788                                  * Such thing can happen, if global CSE chose a node from a dead block.
1789                                  * We move it simply to our block.
1790                                  * Note that neither Phi nor End nodes are floating, so we don't
1791                                  * need to handle them here.
1792                                  */
1793                                 if (! in_dead_block) {
1794                                         if (get_irn_pinned(pred) == op_pin_state_floats &&
1795                                                 is_Block_unreachable(get_nodes_block(pred)))
1796                                                 set_nodes_block(pred, curr_block);
1797                                 }
1798                                 place_floats_early(pred, worklist);
1799                         }
1800
1801                         /*
1802                          * A node in the Bad block must stay in the bad block,
1803                          * so don't compute a new block for it.
1804                          */
1805                         if (in_dead_block)
1806                                 continue;
1807
1808                         /* Because all loops contain at least one op_pin_state_pinned node, now all
1809                            our inputs are either op_pin_state_pinned or place_early() has already
1810                            been finished on them.  We do not have any unfinished inputs!  */
1811                         pred_block = get_nodes_block(pred);
1812                         if ((!is_Block_dead(pred_block)) &&
1813                                 (get_Block_dom_depth(pred_block) > depth)) {
1814                                 b = pred_block;
1815                                 depth = get_Block_dom_depth(pred_block);
1816                         }
1817                         /* Avoid that the node is placed in the Start block */
1818                         if (depth == 1 &&
1819                                         get_Block_dom_depth(get_nodes_block(n)) > 1 &&
1820                                         get_irg_phase_state(current_ir_graph) != phase_backend) {
1821                                 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1822                                 assert(b != get_irg_start_block(current_ir_graph));
1823                                 depth = 2;
1824                         }
1825                 }
1826                 if (b)
1827                         set_nodes_block(n, b);
1828         }
1829
1830         /*
1831          * Add predecessors of non floating nodes and non-floating predecessors
1832          * of floating nodes to worklist and fix their blocks if the are in dead block.
1833          */
1834         irn_arity = get_irn_arity(n);
1835
1836         if (is_End(n)) {
1837                 /*
1838                  * Simplest case: End node. Predecessors are keep-alives,
1839                  * no need to move out of dead block.
1840                  */
1841                 for (i = -1; i < irn_arity; ++i) {
1842                         ir_node *pred = get_irn_n(n, i);
1843                         if (irn_not_visited(pred))
1844                                 waitq_put(worklist, pred);
1845                 }
1846         } else if (is_Block(n)) {
1847                 /*
1848                  * Blocks: Predecessors are control flow, no need to move
1849                  * them out of dead block.
1850                  */
1851                 for (i = irn_arity - 1; i >= 0; --i) {
1852                         ir_node *pred = get_irn_n(n, i);
1853                         if (irn_not_visited(pred))
1854                                 waitq_put(worklist, pred);
1855                 }
1856         } else if (is_Phi(n)) {
1857                 ir_node *pred;
1858                 ir_node *curr_block = get_nodes_block(n);
1859                 int in_dead_block   = is_Block_unreachable(curr_block);
1860
1861                 /*
1862                  * Phi nodes: move nodes from dead blocks into the effective use
1863                  * of the Phi-input if the Phi is not in a bad block.
1864                  */
1865                 pred = get_nodes_block(n);
1866                 if (irn_not_visited(pred))
1867                         waitq_put(worklist, pred);
1868
1869                 for (i = irn_arity - 1; i >= 0; --i) {
1870                         ir_node *pred = get_irn_n(n, i);
1871
1872                         if (irn_not_visited(pred)) {
1873                                 if (! in_dead_block &&
1874                                         get_irn_pinned(pred) == op_pin_state_floats &&
1875                                         is_Block_unreachable(get_nodes_block(pred))) {
1876                                         set_nodes_block(pred, get_Block_cfgpred_block(curr_block, i));
1877                                 }
1878                                 waitq_put(worklist, pred);
1879                         }
1880                 }
1881         } else {
1882                 ir_node *pred;
1883                 ir_node *curr_block = get_nodes_block(n);
1884                 int in_dead_block   = is_Block_unreachable(curr_block);
1885
1886                 /*
1887                  * All other nodes: move nodes from dead blocks into the same block.
1888                  */
1889                 pred = get_nodes_block(n);
1890                 if (irn_not_visited(pred))
1891                         waitq_put(worklist, pred);
1892
1893                 for (i = irn_arity - 1; i >= 0; --i) {
1894                         ir_node *pred = get_irn_n(n, i);
1895
1896                         if (irn_not_visited(pred)) {
1897                                 if (! in_dead_block &&
1898                                         get_irn_pinned(pred) == op_pin_state_floats &&
1899                                         is_Block_unreachable(get_nodes_block(pred))) {
1900                                         set_nodes_block(pred, curr_block);
1901                                 }
1902                                 waitq_put(worklist, pred);
1903                         }
1904                 }
1905         }
1906 }
1907
1908 /**
1909  * Floating nodes form subgraphs that begin at nodes as Const, Load,
1910  * Start, Call and that end at op_pin_state_pinned nodes as Store, Call.  Place_early
1911  * places all floating nodes reachable from its argument through floating
1912  * nodes and adds all beginnings at op_pin_state_pinned nodes to the worklist.
1913  *
1914  * @param worklist   a worklist, used for the algorithm, empty on in/output
1915  */
1916 static void place_early(waitq *worklist) {
1917         assert(worklist);
1918         inc_irg_visited(current_ir_graph);
1919
1920         /* this inits the worklist */
1921         place_floats_early(get_irg_end(current_ir_graph), worklist);
1922
1923         /* Work the content of the worklist. */
1924         while (!waitq_empty(worklist)) {
1925                 ir_node *n = waitq_get(worklist);
1926                 if (irn_not_visited(n))
1927                         place_floats_early(n, worklist);
1928         }
1929
1930         set_irg_outs_inconsistent(current_ir_graph);
1931         set_irg_pinned(current_ir_graph, op_pin_state_pinned);
1932 }
1933
1934 /**
1935  * Compute the deepest common ancestor of block and dca.
1936  */
1937 static ir_node *calc_dca(ir_node *dca, ir_node *block) {
1938         assert(block);
1939
1940         /* we do not want to place nodes in dead blocks */
1941         if (is_Block_dead(block))
1942                 return dca;
1943
1944         /* We found a first legal placement. */
1945         if (!dca) return block;
1946
1947         /* Find a placement that is dominates both, dca and block. */
1948         while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1949                 block = get_Block_idom(block);
1950
1951         while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
1952                 dca = get_Block_idom(dca);
1953         }
1954
1955         while (block != dca) {
1956                 block = get_Block_idom(block); dca = get_Block_idom(dca);
1957         }
1958
1959         return dca;
1960 }
1961
1962 /** Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
1963  * I.e., DCA is the block where we might place PRODUCER.
1964  * A data flow edge points from producer to consumer.
1965  */
1966 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
1967 {
1968         /* Compute the last block into which we can place a node so that it is
1969            before consumer. */
1970         if (is_Phi(consumer)) {
1971                 /* our consumer is a Phi-node, the effective use is in all those
1972                    blocks through which the Phi-node reaches producer */
1973                 ir_node *phi_block = get_nodes_block(consumer);
1974                 int      arity     = get_irn_arity(consumer);
1975                 int      i;
1976
1977                 for (i = 0;  i < arity; i++) {
1978                         if (get_Phi_pred(consumer, i) == producer) {
1979                                 ir_node *new_block = get_Block_cfgpred_block(phi_block, i);
1980
1981                                 if (!is_Block_unreachable(new_block))
1982                                         dca = calc_dca(dca, new_block);
1983                         }
1984                 }
1985         } else {
1986                 dca = calc_dca(dca, get_nodes_block(consumer));
1987         }
1988
1989         return dca;
1990 }
1991
1992 /* FIXME: the name clashes here with the function from ana/field_temperature.c
1993  * please rename. */
1994 static INLINE int get_irn_loop_depth(ir_node *n) {
1995         return get_loop_depth(get_irn_loop(n));
1996 }
1997
1998 /**
1999  * Move n to a block with less loop depth than it's current block. The
2000  * new block must be dominated by early.
2001  *
2002  * @param n      the node that should be moved
2003  * @param early  the earliest block we can n move to
2004  */
2005 static void move_out_of_loops(ir_node *n, ir_node *early) {
2006         ir_node *best, *dca;
2007         assert(n && early);
2008
2009
2010         /* Find the region deepest in the dominator tree dominating
2011            dca with the least loop nesting depth, but still dominated
2012            by our early placement. */
2013         dca = get_nodes_block(n);
2014
2015         best = dca;
2016         while (dca != early) {
2017                 dca = get_Block_idom(dca);
2018                 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
2019                 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
2020                         best = dca;
2021                 }
2022         }
2023         if (best != get_nodes_block(n)) {
2024                 /* debug output
2025                 printf("Moving out of loop: "); DDMN(n);
2026                 printf(" Outermost block: "); DDMN(early);
2027                 printf(" Best block: "); DDMN(best);
2028                 printf(" Innermost block: "); DDMN(get_nodes_block(n));
2029                 */
2030                 set_nodes_block(n, best);
2031         }
2032 }
2033
2034 /* deepest common ancestor in the dominator tree of all nodes'
2035    blocks depending on us; our final placement has to dominate DCA. */
2036 static ir_node *get_deepest_common_ancestor(ir_node *node, ir_node *dca)
2037 {
2038         int i;
2039
2040         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
2041                 ir_node *succ = get_irn_out(node, i);
2042
2043                 if (is_End(succ)) {
2044                         /*
2045                          * This consumer is the End node, a keep alive edge.
2046                          * This is not a real consumer, so we ignore it
2047                          */
2048                         continue;
2049                 }
2050
2051                 if (is_Proj(succ)) {
2052                         dca = get_deepest_common_ancestor(succ, dca);
2053                 } else {
2054                         /* ignore if succ is in dead code */
2055                         ir_node *succ_blk = get_nodes_block(succ);
2056                         if (is_Block_unreachable(succ_blk))
2057                                 continue;
2058                         dca = consumer_dom_dca(dca, succ, node);
2059                 }
2060         }
2061
2062         return dca;
2063 }
2064
2065 static void set_projs_block(ir_node *node, ir_node *block)
2066 {
2067         int i;
2068
2069         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
2070                 ir_node *succ = get_irn_out(node, i);
2071
2072                 assert(is_Proj(succ));
2073
2074                 if(get_irn_mode(succ) == mode_T) {
2075                         set_projs_block(succ, block);
2076                 }
2077                 set_nodes_block(succ, block);
2078         }
2079 }
2080
2081 /**
2082  * Find the latest legal block for N and place N into the
2083  * `optimal' Block between the latest and earliest legal block.
2084  * The `optimal' block is the dominance-deepest block of those
2085  * with the least loop-nesting-depth.  This places N out of as many
2086  * loops as possible and then makes it as control dependent as
2087  * possible.
2088  *
2089  * @param n         the node to be placed
2090  * @param worklist  a worklist, all successors of non-floating nodes are
2091  *                  placed here
2092  */
2093 static void place_floats_late(ir_node *n, pdeq *worklist) {
2094   int i;
2095         ir_node *early_blk;
2096
2097         assert(irn_not_visited(n)); /* no multiple placement */
2098
2099         mark_irn_visited(n);
2100
2101         /* no need to place block nodes, control nodes are already placed. */
2102         if (!is_Block(n) &&
2103             (!is_cfop(n)) &&
2104             (get_irn_mode(n) != mode_X)) {
2105                 /* Remember the early_blk placement of this block to move it
2106                    out of loop no further than the early_blk placement. */
2107                 early_blk = get_nodes_block(n);
2108
2109                 /*
2110                  * BEWARE: Here we also get code, that is live, but
2111                  * was in a dead block.  If the node is life, but because
2112                  * of CSE in a dead block, we still might need it.
2113                  */
2114
2115                 /* Assure that our users are all placed, except the Phi-nodes.
2116                 --- Each data flow cycle contains at least one Phi-node.  We
2117                     have to break the `user has to be placed before the
2118                     producer' dependence cycle and the Phi-nodes are the
2119                     place to do so, because we need to base our placement on the
2120                     final region of our users, which is OK with Phi-nodes, as they
2121                     are op_pin_state_pinned, and they never have to be placed after a
2122                     producer of one of their inputs in the same block anyway. */
2123                 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2124                         ir_node *succ = get_irn_out(n, i);
2125                         if (irn_not_visited(succ) && !is_Phi(succ))
2126                                 place_floats_late(succ, worklist);
2127                 }
2128
2129                 if (! is_Block_dead(early_blk)) {
2130                         /* do only move things that where not dead */
2131                         ir_op *op = get_irn_op(n);
2132
2133                         /* We have to determine the final block of this node... except for
2134                            constants and Projs */
2135                         if ((get_irn_pinned(n) == op_pin_state_floats) &&
2136                             (op != op_Const)    &&
2137                             (op != op_SymConst) &&
2138                             (op != op_Proj))
2139                         {
2140                                 /* deepest common ancestor in the dominator tree of all nodes'
2141                                    blocks depending on us; our final placement has to dominate
2142                                    DCA. */
2143                                 ir_node *dca = get_deepest_common_ancestor(n, NULL);
2144                                 if (dca != NULL) {
2145                                         set_nodes_block(n, dca);
2146                                         move_out_of_loops(n, early_blk);
2147                                         if(get_irn_mode(n) == mode_T) {
2148                                                 set_projs_block(n, get_nodes_block(n));
2149                                         }
2150                                 }
2151                         }
2152                 }
2153         }
2154
2155         /* Add successors of all non-floating nodes on list. (Those of floating
2156            nodes are placed already and therefore are marked.)  */
2157         for (i = 0; i < get_irn_n_outs(n); i++) {
2158                 ir_node *succ = get_irn_out(n, i);
2159                 if (irn_not_visited(get_irn_out(n, i))) {
2160                         pdeq_putr(worklist, succ);
2161                 }
2162         }
2163 }
2164
2165 /**
2166  * Place floating nodes on the given worklist as late as possible using
2167  * the dominance tree.
2168  *
2169  * @param worklist   the worklist containing the nodes to place
2170  */
2171 static void place_late(waitq *worklist) {
2172         assert(worklist);
2173         inc_irg_visited(current_ir_graph);
2174
2175         /* This fills the worklist initially. */
2176         place_floats_late(get_irg_start_block(current_ir_graph), worklist);
2177
2178         /* And now empty the worklist again... */
2179         while (!waitq_empty(worklist)) {
2180                 ir_node *n = waitq_get(worklist);
2181                 if (irn_not_visited(n))
2182                         place_floats_late(n, worklist);
2183         }
2184 }
2185
2186 /* Code Placement. */
2187 void place_code(ir_graph *irg) {
2188         waitq *worklist;
2189         ir_graph *rem = current_ir_graph;
2190
2191         current_ir_graph = irg;
2192
2193         /* Handle graph state */
2194         assert(get_irg_phase_state(irg) != phase_building);
2195         assure_doms(irg);
2196
2197         if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
2198                 free_loop_information(irg);
2199                 construct_cf_backedges(irg);
2200         }
2201
2202         /* Place all floating nodes as early as possible. This guarantees
2203          a legal code placement. */
2204         worklist = new_waitq();
2205         place_early(worklist);
2206
2207         /* place_early() invalidates the outs, place_late needs them. */
2208         compute_irg_outs(irg);
2209
2210         /* Now move the nodes down in the dominator tree. This reduces the
2211            unnecessary executions of the node. */
2212         place_late(worklist);
2213
2214         set_irg_outs_inconsistent(current_ir_graph);
2215         set_irg_loopinfo_inconsistent(current_ir_graph);
2216         del_waitq(worklist);
2217         current_ir_graph = rem;
2218 }
2219
2220 typedef struct cf_env {
2221         char ignore_exc_edges; /**< set if exception edges should be ignored. */
2222         char changed;          /**< flag indicates that the cf graphs has changed. */
2223 } cf_env;
2224
2225 /**
2226  * Called by walker of remove_critical_cf_edges().
2227  *
2228  * Place an empty block to an edge between a blocks of multiple
2229  * predecessors and a block of multiple successors.
2230  *
2231  * @param n   IR node
2232  * @param env Environment of walker.
2233  */
2234 static void walk_critical_cf_edges(ir_node *n, void *env) {
2235         int arity, i;
2236         ir_node *pre, *block, *jmp;
2237         cf_env *cenv = env;
2238         ir_graph *irg = get_irn_irg(n);
2239
2240         /* Block has multiple predecessors */
2241         arity = get_irn_arity(n);
2242         if (arity > 1) {
2243                 if (n == get_irg_end_block(irg))
2244                         return;  /*  No use to add a block here.      */
2245
2246                 for (i = 0; i < arity; ++i) {
2247                         const ir_op *cfop;
2248
2249                         pre = get_irn_n(n, i);
2250                         /* don't count Bad's */
2251                         if (is_Bad(pre))
2252                                 continue;
2253
2254                         cfop = get_irn_op(skip_Proj(pre));
2255                         if (is_op_fragile(cfop)) {
2256                                 if (cenv->ignore_exc_edges && get_Proj_proj(pre) == pn_Generic_X_except)
2257                                         continue;
2258                                 goto insert;
2259                         }
2260                         /* we don't want place nodes in the start block, so handle it like forking */
2261                         if (is_op_forking(cfop) || cfop == op_Start) {
2262                                 /* Predecessor has multiple successors. Insert new control flow edge edges. */
2263 insert:
2264                                 /* set predecessor of new block */
2265                                 block = new_r_Block(irg, 1, &pre);
2266                                 /* insert new jmp node to new block */
2267                                 jmp = new_r_Jmp(irg, block);
2268                                 /* set successor of new block */
2269                                 set_irn_n(n, i, jmp);
2270                                 cenv->changed = 1;
2271                         } /* predecessor has multiple successors */
2272                 } /* for all predecessors */
2273         } /* n is a multi-entry block */
2274 }
2275
2276 void remove_critical_cf_edges(ir_graph *irg) {
2277         cf_env env;
2278
2279         env.ignore_exc_edges = 1;
2280         env.changed          = 0;
2281
2282         irg_block_walk_graph(irg, NULL, walk_critical_cf_edges, &env);
2283         if (env.changed) {
2284                 /* control flow changed */
2285                 set_irg_outs_inconsistent(irg);
2286                 set_irg_extblk_inconsistent(irg);
2287                 set_irg_doms_inconsistent(irg);
2288                 set_irg_loopinfo_inconsistent(irg);
2289         }
2290 }