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