Add temporary fix code for unsupported inlining cases (slow!).
[libfirm] / ir / ir / irgopt.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgopt.c
4  * Purpose:     Optimizations for a whole ir graph, i.e., a procedure.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by: Sebastian Felis
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17
18 # include <assert.h>
19 # include <stdbool.h>
20
21 # include "irprog.h"
22 # include "irgopt.h"
23 # include "irnode_t.h"
24 # include "irgraph_t.h"
25 # include "iropt_t.h"
26 # include "irgwalk.h"
27 # include "ircons.h"
28 # include "irgmod.h"
29 # include "array.h"
30 # include "pset.h"
31 # include "eset.h"
32 # include "pdeq.h"       /* Fuer code placement */
33 # include "irouts.h"
34 # include "irloop.h"
35 # include "irbackedge_t.h"
36 # include "irflag_t.h"
37 # include "firmstat.h"
38
39 /* Defined in iropt.c */
40 pset *new_identities (void);
41 void  del_identities (pset *value_table);
42 void  add_identities   (pset *value_table, ir_node *node);
43
44 /*------------------------------------------------------------------*/
45 /* apply optimizations of iropt to all nodes.                       */
46 /*------------------------------------------------------------------*/
47
48 static void init_link (ir_node *n, void *env) {
49   set_irn_link(n, NULL);
50 }
51
52 #if 0   /* Old version. Avoids Ids.
53        This is not necessary:  we do a postwalk, and get_irn_n
54        removes ids anyways.  So it's much cheaper to call the
55        optimization less often and use the exchange() algorithm. */
56 static void
57 optimize_in_place_wrapper (ir_node *n, void *env) {
58   int i, irn_arity;
59   ir_node *optimized, *old;
60
61   irn_arity = intern_get_irn_arity(n);
62   for (i = 0; i < irn_arity; i++) {
63     /* get_irn_n skips Id nodes, so comparison old != optimized does not
64        show all optimizations. Therefore always set new predecessor. */
65     old = intern_get_irn_intra_n(n, i);
66     optimized = optimize_in_place_2(old);
67     set_irn_n(n, i, optimized);
68   }
69
70   if (intern_get_irn_op(n) == op_Block) {
71     optimized = optimize_in_place_2(n);
72     if (optimized != n) exchange (n, optimized);
73   }
74 }
75 #else
76 static void
77 optimize_in_place_wrapper (ir_node *n, void *env) {
78   ir_node *optimized = optimize_in_place_2(n);
79   if (optimized != n) exchange (n, optimized);
80 }
81 #endif
82
83
84
85 void
86 local_optimize_graph (ir_graph *irg) {
87   ir_graph *rem = current_ir_graph;
88   current_ir_graph = irg;
89
90   /* Handle graph state */
91   assert(get_irg_phase_state(irg) != phase_building);
92   if (get_opt_global_cse())
93     set_irg_pinned(current_ir_graph, floats);
94   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
95     set_irg_outs_inconsistent(current_ir_graph);
96   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
97     set_irg_dom_inconsistent(current_ir_graph);
98
99   /* Clean the value_table in irg for the cse. */
100   del_identities(irg->value_table);
101   irg->value_table = new_identities();
102
103   /* walk over the graph */
104   irg_walk(irg->end, init_link, optimize_in_place_wrapper, NULL);
105
106   current_ir_graph = rem;
107 }
108
109 /*------------------------------------------------------------------*/
110 /* Routines for dead node elimination / copying garbage collection  */
111 /* of the obstack.                                                  */
112 /*------------------------------------------------------------------*/
113
114 /**
115  * Remember the new node in the old node by using a field all nodes have.
116  */
117 static INLINE void
118 set_new_node (ir_node *old, ir_node *new)
119 {
120   old->link = new;
121 }
122
123 /**
124  * Get this new node, before the old node is forgotton.
125  */
126 static INLINE ir_node *
127 get_new_node (ir_node * n)
128 {
129   return n->link;
130 }
131
132 /**
133  * We use the block_visited flag to mark that we have computed the
134  * number of useful predecessors for this block.
135  * Further we encode the new arity in this flag in the old blocks.
136  * Remembering the arity is useful, as it saves a lot of pointer
137  * accesses.  This function is called for all Phi and Block nodes
138  * in a Block.
139  */
140 static INLINE int
141 compute_new_arity(ir_node *b) {
142   int i, res, irn_arity;
143   int irg_v, block_v;
144
145   irg_v = get_irg_block_visited(current_ir_graph);
146   block_v = get_Block_block_visited(b);
147   if (block_v >= irg_v) {
148     /* we computed the number of preds for this block and saved it in the
149        block_v flag */
150     return block_v - irg_v;
151   } else {
152     /* compute the number of good predecessors */
153     res = irn_arity = intern_get_irn_arity(b);
154     for (i = 0; i < irn_arity; i++)
155       if (intern_get_irn_opcode(intern_get_irn_n(b, i)) == iro_Bad) res--;
156     /* save it in the flag. */
157     set_Block_block_visited(b, irg_v + res);
158     return res;
159   }
160 }
161
162 /* TODO: add an ir_op operation */
163 static INLINE void new_backedge_info(ir_node *n) {
164   switch(intern_get_irn_opcode(n)) {
165   case iro_Block:
166     n->attr.block.cg_backedge = NULL;
167     n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
168     break;
169   case iro_Phi:
170     n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
171     break;
172   case iro_Filter:
173     n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
174     break;
175   default: ;
176   }
177 }
178
179 /**
180  * Copies the node to the new obstack. The Ins of the new node point to
181  * the predecessors on the old obstack.  For block/phi nodes not all
182  * predecessors might be copied.  n->link points to the new node.
183  * For Phi and Block nodes the function allocates in-arrays with an arity
184  * only for useful predecessors.  The arity is determined by counting
185  * the non-bad predecessors of the block.
186  */
187 static void
188 copy_node (ir_node *n, void *env) {
189   ir_node *nn, *block;
190   int new_arity;
191
192   /* The end node looses it's flexible in array.  This doesn't matter,
193      as dead node elimination builds End by hand, inlineing doesn't use
194      the End node. */
195   /* assert(n->op == op_End ||  ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC)); */
196
197   if (intern_get_irn_opcode(n) == iro_Block) {
198     block = NULL;
199     new_arity = compute_new_arity(n);
200     n->attr.block.graph_arr = NULL;
201   } else {
202     block = get_nodes_Block(n);
203     if (intern_get_irn_opcode(n) == iro_Phi) {
204       new_arity = compute_new_arity(block);
205     } else {
206       new_arity = intern_get_irn_arity(n);
207     }
208   }
209   nn = new_ir_node(get_irn_dbg_info(n),
210            current_ir_graph,
211            block,
212            intern_get_irn_op(n),
213            intern_get_irn_mode(n),
214            new_arity,
215            get_irn_in(n));
216   /* Copy the attributes.  These might point to additional data.  If this
217      was allocated on the old obstack the pointers now are dangling.  This
218      frees e.g. the memory of the graph_arr allocated in new_immBlock. */
219   copy_attrs(n, nn);
220   new_backedge_info(nn);
221   set_new_node(n, nn);
222
223   /*  printf("\n old node: "); DDMSG2(n);
224       printf(" new node: "); DDMSG2(nn); */
225
226 }
227
228 /**
229  * Copies new predecessors of old node to new node remembered in link.
230  * Spare the Bad predecessors of Phi and Block nodes.
231  */
232 static void
233 copy_preds (ir_node *n, void *env) {
234   ir_node *nn, *block;
235   int i, j, irn_arity;
236
237   nn = get_new_node(n);
238
239   /* printf("\n old node: "); DDMSG2(n);
240      printf(" new node: "); DDMSG2(nn);
241      printf(" arities: old: %d, new: %d\n", intern_get_irn_arity(n), intern_get_irn_arity(nn)); */
242
243   if (intern_get_irn_opcode(n) == iro_Block) {
244     /* Don't copy Bad nodes. */
245     j = 0;
246     irn_arity = intern_get_irn_arity(n);
247     for (i = 0; i < irn_arity; i++)
248       if (intern_get_irn_opcode(intern_get_irn_n(n, i)) != iro_Bad) {
249     set_irn_n (nn, j, get_new_node(intern_get_irn_n(n, i)));
250     /*if (is_backedge(n, i)) set_backedge(nn, j);*/
251     j++;
252       }
253     /* repair the block visited flag from above misuse. Repair it in both
254        graphs so that the old one can still be used. */
255     set_Block_block_visited(nn, 0);
256     set_Block_block_visited(n, 0);
257     /* Local optimization could not merge two subsequent blocks if
258        in array contained Bads.  Now it's possible.
259        We don't call optimize_in_place as it requires
260        that the fields in ir_graph are set properly. */
261     if ((get_opt_control_flow_straightening()) &&
262     (get_Block_n_cfgpreds(nn) == 1) &&
263     (intern_get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp))
264       exchange(nn, get_nodes_Block(get_Block_cfgpred(nn, 0)));
265   } else if (intern_get_irn_opcode(n) == iro_Phi) {
266     /* Don't copy node if corresponding predecessor in block is Bad.
267        The Block itself should not be Bad. */
268     block = get_nodes_Block(n);
269     set_irn_n (nn, -1, get_new_node(block));
270     j = 0;
271     irn_arity = intern_get_irn_arity(n);
272     for (i = 0; i < irn_arity; i++)
273       if (intern_get_irn_opcode(intern_get_irn_n(block, i)) != iro_Bad) {
274     set_irn_n (nn, j, get_new_node(intern_get_irn_n(n, i)));
275     /*if (is_backedge(n, i)) set_backedge(nn, j);*/
276     j++;
277       }
278     /* If the pre walker reached this Phi after the post walker visited the
279        block block_visited is > 0. */
280     set_Block_block_visited(get_nodes_Block(n), 0);
281     /* Compacting the Phi's ins might generate Phis with only one
282        predecessor. */
283     if (intern_get_irn_arity(n) == 1)
284       exchange(n, intern_get_irn_n(n, 0));
285   } else {
286     irn_arity = intern_get_irn_arity(n);
287     for (i = -1; i < irn_arity; i++)
288       set_irn_n (nn, i, get_new_node(intern_get_irn_n(n, i)));
289   }
290   /* Now the new node is complete.  We can add it to the hash table for cse.
291      @@@ inlinening aborts if we identify End. Why? */
292   if(intern_get_irn_op(nn) != op_End)
293     add_identities (current_ir_graph->value_table, nn);
294 }
295
296 /**
297  * Copies the graph recursively, compacts the keepalive of the end node.
298  */
299 static void
300 copy_graph (void) {
301   ir_node *oe, *ne; /* old end, new end */
302   ir_node *ka;      /* keep alive */
303   int i, irn_arity;
304
305   oe = get_irg_end(current_ir_graph);
306   /* copy the end node by hand, allocate dynamic in array! */
307   ne = new_ir_node(get_irn_dbg_info(oe),
308            current_ir_graph,
309            NULL,
310            op_End,
311            mode_X,
312            -1,
313            NULL);
314   /* Copy the attributes.  Well, there might be some in the future... */
315   copy_attrs(oe, ne);
316   set_new_node(oe, ne);
317
318   /* copy the live nodes */
319   irg_walk(get_nodes_Block(oe), copy_node, copy_preds, NULL);
320   /* copy_preds for the end node ... */
321   set_nodes_Block(ne, get_new_node(get_nodes_Block(oe)));
322
323   /*- ... and now the keep alives. -*/
324   /* First pick the not marked block nodes and walk them.  We must pick these
325      first as else we will oversee blocks reachable from Phis. */
326   irn_arity = intern_get_irn_arity(oe);
327   for (i = 0; i < irn_arity; i++) {
328     ka = intern_get_irn_intra_n(oe, i);
329     if ((intern_get_irn_op(ka) == op_Block) &&
330     (get_irn_visited(ka) < get_irg_visited(current_ir_graph))) {
331       /* We must keep the block alive and copy everything reachable */
332       set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
333       irg_walk(ka, copy_node, copy_preds, NULL);
334       add_End_keepalive(ne, get_new_node(ka));
335     }
336   }
337
338   /* Now pick the Phis.  Here we will keep all! */
339   irn_arity = intern_get_irn_arity(oe);
340   for (i = 0; i < irn_arity; i++) {
341     ka = intern_get_irn_intra_n(oe, i);
342     if ((intern_get_irn_op(ka) == op_Phi)) {
343       if (get_irn_visited(ka) < get_irg_visited(current_ir_graph)) {
344     /* We didn't copy the Phi yet.  */
345     set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
346     irg_walk(ka, copy_node, copy_preds, NULL);
347       }
348       add_End_keepalive(ne, get_new_node(ka));
349     }
350   }
351 }
352
353 /**
354  * Copies the graph reachable from current_ir_graph->end to the obstack
355  * in current_ir_graph and fixes the environment.
356  * Then fixes the fields in current_ir_graph containing nodes of the
357  * graph.
358  */
359 static void
360 copy_graph_env (void) {
361   ir_node *old_end;
362   /* Not all nodes remembered in current_ir_graph might be reachable
363      from the end node.  Assure their link is set to NULL, so that
364      we can test whether new nodes have been computed. */
365   set_irn_link(get_irg_frame  (current_ir_graph), NULL);
366   set_irn_link(get_irg_globals(current_ir_graph), NULL);
367   set_irn_link(get_irg_args   (current_ir_graph), NULL);
368
369   /* we use the block walk flag for removing Bads from Blocks ins. */
370   inc_irg_block_visited(current_ir_graph);
371
372   /* copy the graph */
373   copy_graph();
374
375   /* fix the fields in current_ir_graph */
376   old_end = get_irg_end(current_ir_graph);
377   set_irg_end (current_ir_graph, get_new_node(old_end));
378   set_irg_end_except (current_ir_graph, get_irg_end(current_ir_graph));
379   set_irg_end_reg    (current_ir_graph, get_irg_end(current_ir_graph));
380   free_End(old_end);
381   set_irg_end_block  (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
382   if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
383     copy_node (get_irg_frame(current_ir_graph), NULL);
384     copy_preds(get_irg_frame(current_ir_graph), NULL);
385   }
386   if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
387     copy_node (get_irg_globals(current_ir_graph), NULL);
388     copy_preds(get_irg_globals(current_ir_graph), NULL);
389   }
390   if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
391     copy_node (get_irg_args(current_ir_graph), NULL);
392     copy_preds(get_irg_args(current_ir_graph), NULL);
393   }
394   set_irg_start  (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
395
396   set_irg_start_block(current_ir_graph,
397               get_new_node(get_irg_start_block(current_ir_graph)));
398   set_irg_frame  (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
399   set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
400   set_irg_args   (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
401   if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
402     copy_node(get_irg_bad(current_ir_graph), NULL);
403     copy_preds(get_irg_bad(current_ir_graph), NULL);
404   }
405   set_irg_bad(current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
406   /* GL removed: we need unknown with mode for analyses.
407   if (get_irn_link(get_irg_unknown(current_ir_graph)) == NULL) {
408     copy_node(get_irg_unknown(current_ir_graph), NULL);
409     copy_preds(get_irg_unknown(current_ir_graph), NULL);
410   }
411   set_irg_unknown(current_ir_graph, get_new_node(get_irg_unknown(current_ir_graph)));
412   */
413 }
414
415 /**
416  * Copies all reachable nodes to a new obstack.  Removes bad inputs
417  * from block nodes and the corresponding inputs from Phi nodes.
418  * Merges single exit blocks with single entry blocks and removes
419  * 1-input Phis.
420  * Adds all new nodes to a new hash table for cse.  Does not
421  * perform cse, so the hash table might contain common subexpressions.
422  */
423 /* Amroq call this emigrate() */
424 void
425 dead_node_elimination(ir_graph *irg) {
426   ir_graph *rem;
427   int rem_ipview = interprocedural_view;
428   struct obstack *graveyard_obst = NULL;
429   struct obstack *rebirth_obst   = NULL;
430
431   /* Remember external state of current_ir_graph. */
432   rem = current_ir_graph;
433   current_ir_graph = irg;
434   interprocedural_view = 0;
435
436   /* Handle graph state */
437   assert(get_irg_phase_state(current_ir_graph) != phase_building);
438   assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
439   free_outs(current_ir_graph);
440
441   /* @@@ so far we loose loops when copying */
442   free_loop_information(current_ir_graph);
443
444   if (get_opt_optimize() && get_opt_dead_node_elimination()) {
445
446     /* A quiet place, where the old obstack can rest in peace,
447        until it will be cremated. */
448     graveyard_obst = irg->obst;
449
450     /* A new obstack, where the reachable nodes will be copied to. */
451     rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
452     current_ir_graph->obst = rebirth_obst;
453     obstack_init (current_ir_graph->obst);
454
455     /* We also need a new hash table for cse */
456     del_identities (irg->value_table);
457     irg->value_table = new_identities ();
458
459     /* Copy the graph from the old to the new obstack */
460     copy_graph_env();
461
462     /* Free memory from old unoptimized obstack */
463     obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
464     xfree (graveyard_obst);           /* ... then free it.           */
465   }
466
467   current_ir_graph = rem;
468   interprocedural_view = rem_ipview;
469 }
470
471 /**
472  * Relink bad predeseccors of a block and store the old in array to the
473  * link field. This function is called by relink_bad_predecessors().
474  * The array of link field starts with the block operand at position 0.
475  * If block has bad predecessors, create a new in array without bad preds.
476  * Otherwise let in array untouched.
477  */
478 static void relink_bad_block_predecessors(ir_node *n, void *env) {
479   ir_node **new_in, *irn;
480   int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
481
482   /* if link field of block is NULL, look for bad predecessors otherwise
483      this is allready done */
484   if (intern_get_irn_op(n) == op_Block &&
485       get_irn_link(n) == NULL) {
486
487     /* save old predecessors in link field (position 0 is the block operand)*/
488     set_irn_link(n, (void *)get_irn_in(n));
489
490     /* count predecessors without bad nodes */
491     old_irn_arity = intern_get_irn_arity(n);
492     for (i = 0; i < old_irn_arity; i++)
493       if (!is_Bad(intern_get_irn_n(n, i))) new_irn_arity++;
494
495     /* arity changing: set new predecessors without bad nodes */
496     if (new_irn_arity < old_irn_arity) {
497       /* get new predecessor array without Block predecessor */
498       new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
499
500       /* set new predeseccors in array */
501       new_in[0] = NULL;
502       new_irn_n = 1;
503       for (i = 1; i < old_irn_arity; i++) {
504     irn = intern_get_irn_n(n, i);
505     if (!is_Bad(irn)) new_in[new_irn_n++] = irn;
506       }
507       n->in = new_in;
508     } /* ir node has bad predecessors */
509
510   } /* Block is not relinked */
511 }
512
513 /**
514  * Relinks Bad predecesors from Bocks and Phis called by walker
515  * remove_bad_predecesors(). If n is a Block, call
516  * relink_bad_block_redecessors(). If n is a Phinode, call also the relinking
517  * function of Phi's Block. If this block has bad predecessors, relink preds
518  * of the Phinode.
519  */
520 static void relink_bad_predecessors(ir_node *n, void *env) {
521   ir_node *block, **old_in;
522   int i, old_irn_arity, new_irn_arity;
523
524   /* relink bad predeseccors of a block */
525   if (intern_get_irn_op(n) == op_Block)
526     relink_bad_block_predecessors(n, env);
527
528   /* If Phi node relink its block and its predecessors */
529   if (intern_get_irn_op(n) == op_Phi) {
530
531     /* Relink predeseccors of phi's block */
532     block = get_nodes_Block(n);
533     if (get_irn_link(block) == NULL)
534       relink_bad_block_predecessors(block, env);
535
536     old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
537     old_irn_arity = ARR_LEN(old_in);
538
539     /* Relink Phi predeseccors if count of predeseccors changed */
540     if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
541       /* set new predeseccors in array
542      n->in[0] remains the same block */
543       new_irn_arity = 1;
544       for(i = 1; i < old_irn_arity; i++)
545     if (!is_Bad((ir_node *)old_in[i])) n->in[new_irn_arity++] = n->in[i];
546
547       ARR_SETLEN(ir_node *, n->in, new_irn_arity);
548     }
549
550   } /* n is a Phi node */
551 }
552
553 /**
554  * Removes Bad Bad predecesors from Blocks and the corresponding
555  * inputs to Phi nodes as in dead_node_elimination but without
556  * copying the graph.
557  * On walking up set the link field to NULL, on walking down call
558  * relink_bad_predecessors() (This function stores the old in array
559  * to the link field and sets a new in array if arity of predecessors
560  * changes).
561  */
562 void remove_bad_predecessors(ir_graph *irg) {
563   irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
564 }
565
566
567 /*--------------------------------------------------------------------*/
568 /*  Funcionality for inlining                                         */
569 /*--------------------------------------------------------------------*/
570
571 /**
572  * Copy node for inlineing.  Updates attributes that change when
573  * inlineing but not for dead node elimination.
574  *
575  * Copies the node by calling copy_node and then updates the entity if
576  * it's a local one.  env must be a pointer of the frame type of the
577  * inlined procedure. The new entities must be in the link field of
578  * the entities.
579  */
580 static INLINE void
581 copy_node_inline (ir_node *n, void *env) {
582   ir_node *new;
583   type *frame_tp = (type *)env;
584
585   copy_node(n, NULL);
586   if (intern_get_irn_op(n) == op_Sel) {
587     new = get_new_node (n);
588     assert(intern_get_irn_op(new) == op_Sel);
589     if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
590       set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
591     }
592   } else if (intern_get_irn_op(n) == op_Block) {
593     new = get_new_node (n);
594     new->attr.block.irg = current_ir_graph;
595   }
596 }
597
598 static void find_addr(ir_node *node, void *env)
599 {
600   if (get_irn_opcode(node) == iro_Proj) {
601     if (get_Proj_proj(node) == pn_Start_P_value_arg_base)
602       *(int *)env = 0;
603   }
604 }
605
606 /*
607  * currently, we cannot inline two cases:
608  * - call with compound arguments
609  * - graphs that take the address of a parameter
610  *
611  * check these condition here
612  */
613 static int can_inline(ir_node *call, ir_graph *called_graph)
614 {
615   type *call_type = get_Call_type(call);
616   int params, ress, i, res;
617
618   assert(is_method_type(call_type));
619
620   params = get_method_n_params(call_type);
621   ress   = get_method_n_ress(call_type);
622
623   /* check params */
624   for (i = 0; i < params; ++i) {
625     type *p_type = get_method_param_type(call_type, i);
626
627     if (is_compound_type(p_type))
628       return 0;
629   }
630
631   /* check res */
632   for (i = 0; i < ress; ++i) {
633     type *r_type = get_method_res_type(call_type, i);
634
635     if (is_compound_type(r_type))
636       return 0;
637   }
638
639   res = 1;
640   irg_walk_graph(called_graph, find_addr, NULL, &res);
641
642   return res;
643 }
644
645 void inline_method(ir_node *call, ir_graph *called_graph) {
646   ir_node *pre_call;
647   ir_node *post_call, *post_bl;
648   ir_node *in[5];
649   ir_node *end, *end_bl;
650   ir_node **res_pred;
651   ir_node **cf_pred;
652   ir_node *ret, *phi;
653   int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
654   int exc_handling;
655   type *called_frame;
656   irg_inline_property prop = get_irg_inline_property(called_graph);
657
658   if ( (prop != irg_inline_forced) && (!get_opt_optimize() || !get_opt_inline() ||
659       (prop == irg_inline_forbidden))) return;
660
661
662   /*
663    * currently, we cannot inline two cases:
664    * - call with compound arguments
665    * - graphs that take the address of a parameter
666    */
667   if (! can_inline(call, called_graph))
668     return;
669
670   /* --  Turn off optimizations, this can cause problems when allocating new nodes. -- */
671   rem_opt = get_opt_optimize();
672   set_optimize(0);
673
674   /* Handle graph state */
675   assert(get_irg_phase_state(current_ir_graph) != phase_building);
676   assert(get_irg_pinned(current_ir_graph) == pinned);
677   assert(get_irg_pinned(called_graph) == pinned);
678   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
679     set_irg_outs_inconsistent(current_ir_graph);
680
681   /* -- Check preconditions -- */
682   assert(get_irn_op(call) == op_Call);
683   /* @@@ does not work for InterfaceIII.java after cgana
684      assert(get_Call_type(call) == get_entity_type(get_irg_ent(called_graph)));
685      assert(smaller_type(get_entity_type(get_irg_ent(called_graph)),
686      get_Call_type(call)));
687   */
688   assert(get_type_tpop(get_Call_type(call)) == type_method);
689   if (called_graph == current_ir_graph) {
690     set_optimize(rem_opt);
691     return;
692   }
693
694   /* here we know we WILL inline, so inform the statistics */
695   stat_inline(call, called_graph);
696
697   /* -- Decide how to handle exception control flow: Is there a handler
698      for the Call node, or do we branch directly to End on an exception?
699      exc_handling: 0 There is a handler.
700                    1 Branches to End.
701            2 Exception handling not represented in Firm. -- */
702   {
703     ir_node *proj, *Mproj = NULL, *Xproj = NULL;
704     for (proj = (ir_node *)get_irn_link(call); proj; proj = (ir_node *)get_irn_link(proj)) {
705       assert(get_irn_op(proj) == op_Proj);
706       if (get_Proj_proj(proj) == pn_Call_X_except) Xproj = proj;
707       if (get_Proj_proj(proj) == pn_Call_M_except) Mproj = proj;
708     }
709     if      (Mproj) { assert(Xproj); exc_handling = 0; } /*  Mproj           */
710     else if (Xproj) {                exc_handling = 1; } /* !Mproj &&  Xproj   */
711     else            {                exc_handling = 2; } /* !Mproj && !Xproj   */
712   }
713
714
715   /* --
716       the procedure and later replaces the Start node of the called graph.
717       Post_call is the old Call node and collects the results of the called
718       graph. Both will end up being a tuple.  -- */
719   post_bl = get_nodes_Block(call);
720   set_irg_current_block(current_ir_graph, post_bl);
721   /* XxMxPxP of Start + parameter of Call */
722   in[0] = new_Jmp();
723   in[1] = get_Call_mem(call);
724   in[2] = get_irg_frame(current_ir_graph);
725   in[3] = get_irg_globals(current_ir_graph);
726   in[4] = new_Tuple (get_Call_n_params(call), get_Call_param_arr(call));
727   pre_call = new_Tuple(5, in);
728   post_call = call;
729
730   /* --
731       The new block gets the ins of the old block, pre_call and all its
732       predecessors and all Phi nodes. -- */
733   part_block(pre_call);
734
735   /* -- Prepare state for dead node elimination -- */
736   /* Visited flags in calling irg must be >= flag in called irg.
737      Else walker and arity computation will not work. */
738   if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
739     set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
740   if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
741     set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
742   /* Set pre_call as new Start node in link field of the start node of
743      calling graph and pre_calls block as new block for the start block
744      of calling graph.
745      Further mark these nodes so that they are not visited by the
746      copying. */
747   set_irn_link(get_irg_start(called_graph), pre_call);
748   set_irn_visited(get_irg_start(called_graph),
749           get_irg_visited(current_ir_graph));
750   set_irn_link(get_irg_start_block(called_graph),
751            get_nodes_Block(pre_call));
752   set_irn_visited(get_irg_start_block(called_graph),
753           get_irg_visited(current_ir_graph));
754
755   /* Initialize for compaction of in arrays */
756   inc_irg_block_visited(current_ir_graph);
757
758   /* -- Replicate local entities of the called_graph -- */
759   /* copy the entities. */
760   called_frame = get_irg_frame_type(called_graph);
761   for (i = 0; i < get_class_n_members(called_frame); i++) {
762     entity *new_ent, *old_ent;
763     old_ent = get_class_member(called_frame, i);
764     new_ent = copy_entity_own(old_ent, get_cur_frame_type());
765     set_entity_link(old_ent, new_ent);
766   }
767
768   /* visited is > than that of called graph.  With this trick visited will
769      remain unchanged so that an outer walker, e.g., searching the call nodes
770      to inline, calling this inline will not visit the inlined nodes. */
771   set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
772
773   /* -- Performing dead node elimination inlines the graph -- */
774   /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
775      entities. */
776   /* @@@ endless loops are not copied!! -- they should be, I think... */
777   irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
778        get_irg_frame_type(called_graph));
779
780   /* Repair called_graph */
781   set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
782   set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
783   set_Block_block_visited(get_irg_start_block(called_graph), 0);
784
785   /* -- Merge the end of the inlined procedure with the call site -- */
786   /* We will turn the old Call node into a Tuple with the following
787      predecessors:
788        -1:  Block of Tuple.
789        0: Phi of all Memories of Return statements.
790        1: Jmp from new Block that merges the control flow from all exception
791       predecessors of the old end block.
792        2: Tuple of all arguments.
793        3: Phi of Exception memories.
794      In case the old Call directly branches to End on an exception we don't
795      need the block merging all exceptions nor the Phi of the exception
796      memories.
797   */
798
799   /* -- Precompute some values -- */
800   end_bl = get_new_node(get_irg_end_block(called_graph));
801   end = get_new_node(get_irg_end(called_graph));
802   arity = intern_get_irn_arity(end_bl);    /* arity = n_exc + n_ret  */
803   n_res = get_method_n_ress(get_Call_type(call));
804
805   res_pred = (ir_node **) malloc (n_res * sizeof (ir_node *));
806   cf_pred =  (ir_node **) malloc (arity * sizeof (ir_node *));
807
808   set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
809
810   /* -- archive keepalives -- */
811   irn_arity = intern_get_irn_arity(end);
812   for (i = 0; i < irn_arity; i++)
813     add_End_keepalive(get_irg_end(current_ir_graph), intern_get_irn_n(end, i));
814
815   /* The new end node will die.  We need not free as the in array is on the obstack:
816      copy_node only generated 'D' arrays. */
817
818   /* -- Replace Return nodes by Jump nodes. -- */
819   n_ret = 0;
820   for (i = 0; i < arity; i++) {
821     ir_node *ret;
822     ret = intern_get_irn_n(end_bl, i);
823     if (intern_get_irn_op(ret) == op_Return) {
824       cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_Block(ret));
825       n_ret++;
826     }
827   }
828   set_irn_in(post_bl, n_ret, cf_pred);
829
830   /* -- Build a Tuple for all results of the method.
831      Add Phi node if there was more than one Return.  -- */
832   turn_into_tuple(post_call, 4);
833   /* First the Memory-Phi */
834   n_ret = 0;
835   for (i = 0; i < arity; i++) {
836     ret = intern_get_irn_n(end_bl, i);
837     if (intern_get_irn_op(ret) == op_Return) {
838       cf_pred[n_ret] = get_Return_mem(ret);
839       n_ret++;
840     }
841   }
842   phi = new_Phi(n_ret, cf_pred, mode_M);
843   set_Tuple_pred(call, 0, phi);
844   /* Conserve Phi-list for further inlinings -- but might be optimized */
845   if (get_nodes_Block(phi) == post_bl) {
846     set_irn_link(phi, get_irn_link(post_bl));
847     set_irn_link(post_bl, phi);
848   }
849   /* Now the real results */
850   if (n_res > 0) {
851     for (j = 0; j < n_res; j++) {
852       n_ret = 0;
853       for (i = 0; i < arity; i++) {
854     ret = intern_get_irn_n(end_bl, i);
855     if (intern_get_irn_op(ret) == op_Return) {
856       cf_pred[n_ret] = get_Return_res(ret, j);
857       n_ret++;
858     }
859       }
860       phi = new_Phi(n_ret, cf_pred, intern_get_irn_mode(cf_pred[0]));
861       res_pred[j] = phi;
862       /* Conserve Phi-list for further inlinings -- but might be optimized */
863       if (get_nodes_Block(phi) == post_bl) {
864     set_irn_link(phi, get_irn_link(post_bl));
865     set_irn_link(post_bl, phi);
866       }
867     }
868     set_Tuple_pred(call, 2, new_Tuple(n_res, res_pred));
869   } else {
870     set_Tuple_pred(call, 2, new_Bad());
871   }
872   /* Finally the exception control flow.
873      We have two (three) possible situations:
874      First if the Call branches to an exception handler: We need to add a Phi node to
875      collect the memory containing the exception objects.  Further we need
876      to add another block to get a correct representation of this Phi.  To
877      this block we add a Jmp that resolves into the X output of the Call
878      when the Call is turned into a tuple.
879      Second the Call branches to End, the exception is not handled.  Just
880      add all inlined exception branches to the End node.
881      Third: there is no Exception edge at all. Handle as case two. */
882   if (exc_handling == 0) {
883     n_exc = 0;
884     for (i = 0; i < arity; i++) {
885       ir_node *ret;
886       ret = intern_get_irn_n(end_bl, i);
887       if (is_fragile_op(skip_Proj(ret)) || (intern_get_irn_op(skip_Proj(ret)) == op_Raise)) {
888     cf_pred[n_exc] = ret;
889     n_exc++;
890       }
891     }
892     if (n_exc > 0) {
893       new_Block(n_exc, cf_pred);      /* watch it: current_block is changed! */
894       set_Tuple_pred(call, 1, new_Jmp());
895       /* The Phi for the memories with the exception objects */
896       n_exc = 0;
897       for (i = 0; i < arity; i++) {
898     ir_node *ret;
899     ret = skip_Proj(intern_get_irn_n(end_bl, i));
900     if (intern_get_irn_op(ret) == op_Call) {
901       cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 3);
902       n_exc++;
903     } else if (is_fragile_op(ret)) {
904     /* We rely that all cfops have the memory output at the same position. */
905       cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 0);
906       n_exc++;
907     } else if (intern_get_irn_op(ret) == op_Raise) {
908       cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 1);
909       n_exc++;
910     }
911       }
912       set_Tuple_pred(call, 3, new_Phi(n_exc, cf_pred, mode_M));
913     } else {
914       set_Tuple_pred(call, 1, new_Bad());
915       set_Tuple_pred(call, 3, new_Bad());
916     }
917   } else {
918     ir_node *main_end_bl;
919     int main_end_bl_arity;
920     ir_node **end_preds;
921
922     /* assert(exc_handling == 1 || no exceptions. ) */
923     n_exc = 0;
924     for (i = 0; i < arity; i++) {
925       ir_node *ret = intern_get_irn_n(end_bl, i);
926
927       if (is_fragile_op(skip_Proj(ret)) || (intern_get_irn_op(skip_Proj(ret)) == op_Raise)) {
928         cf_pred[n_exc] = ret;
929         n_exc++;
930       }
931     }
932     main_end_bl = get_irg_end_block(current_ir_graph);
933     main_end_bl_arity = intern_get_irn_arity(main_end_bl);
934     end_preds =  (ir_node **) malloc ((n_exc + main_end_bl_arity) * sizeof (ir_node *));
935
936     for (i = 0; i < main_end_bl_arity; ++i)
937       end_preds[i] = intern_get_irn_n(main_end_bl, i);
938     for (i = 0; i < n_exc; ++i)
939       end_preds[main_end_bl_arity + i] = cf_pred[i];
940     set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
941     set_Tuple_pred(call, 1, new_Bad());
942     set_Tuple_pred(call, 3, new_Bad());
943     free(end_preds);
944   }
945   free(res_pred);
946   free(cf_pred);
947
948 #if 0  /* old. now better, correcter, faster implementation. */
949   if (n_exc > 0) {
950     /* -- If the exception control flow from the inlined Call directly
951        branched to the end block we now have the following control
952        flow predecessor pattern: ProjX -> Tuple -> Jmp.  We must
953        remove the Jmp along with it's empty block and add Jmp's
954        predecessors as predecessors of this end block.  No problem if
955        there is no exception, because then branches Bad to End which
956        is fine. --
957        @@@ can't we know this beforehand: by getting the Proj(1) from
958        the Call link list and checking whether it goes to Proj. */
959     /* find the problematic predecessor of the end block. */
960     end_bl = get_irg_end_block(current_ir_graph);
961     for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
962       cf_op = get_Block_cfgpred(end_bl, i);
963       if (intern_get_irn_op(cf_op) == op_Proj) {
964     cf_op = get_Proj_pred(cf_op);
965     if ((intern_get_irn_op(cf_op) == op_Tuple) && (cf_op == call)) {
966       /*  There are unoptimized tuples from inlineing before when no exc */
967       assert(get_Proj_proj(get_Block_cfgpred(end_bl, i)) == pn_Call_X_except);
968       cf_op = get_Tuple_pred(cf_op, pn_Call_X_except);
969       assert(intern_get_irn_op(cf_op) == op_Jmp);
970       break;
971     }
972       }
973     }
974     /* repair */
975     if (i < get_Block_n_cfgpreds(end_bl)) {
976       bl = get_nodes_Block(cf_op);
977       arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
978       cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
979       for (j = 0; j < i; j++)
980     cf_pred[j] = get_Block_cfgpred(end_bl, j);
981       for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
982     cf_pred[j] = get_Block_cfgpred(bl, j-i);
983       for (j = j; j < arity; j++)
984     cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
985       set_irn_in(end_bl, arity, cf_pred);
986       free(cf_pred);
987       /*  Remove the exception pred from post-call Tuple. */
988       set_Tuple_pred(call, pn_Call_X_except, new_Bad());
989     }
990   }
991 #endif
992
993   /* --  Turn cse back on. -- */
994   set_optimize(rem_opt);
995 }
996
997 /********************************************************************/
998 /* Apply inlineing to small methods.                                */
999 /********************************************************************/
1000
1001 static int pos;
1002
1003 /* It makes no sense to inline too many calls in one procedure. Anyways,
1004    I didn't get a version with NEW_ARR_F to run. */
1005 #define MAX_INLINE 1024
1006
1007 /**
1008  * Returns the irg called from a Call node. If the irg is not
1009  * known, NULL is returned.
1010  */
1011 static ir_graph *get_call_called_irg(ir_node *call) {
1012   ir_node *addr;
1013   tarval *tv;
1014   ir_graph *called_irg = NULL;
1015
1016   assert(get_irn_op(call) == op_Call);
1017
1018   addr = get_Call_ptr(call);
1019   if (intern_get_irn_op(addr) == op_Const) {
1020     /* Check whether the constant is the pointer to a compiled entity. */
1021     tv = get_Const_tarval(addr);
1022     if (tarval_to_entity(tv))
1023       called_irg = get_entity_irg(tarval_to_entity(tv));
1024   }
1025   return called_irg;
1026 }
1027
1028 static void collect_calls(ir_node *call, void *env) {
1029
1030   ir_node **calls = (ir_node **)env;
1031   ir_node *addr;
1032   tarval *tv;
1033   ir_graph *called_irg;
1034
1035   if (intern_get_irn_op(call) != op_Call) return;
1036
1037   addr = get_Call_ptr(call);
1038   if (intern_get_irn_op(addr) == op_Const) {
1039     /* Check whether the constant is the pointer to a compiled entity. */
1040     tv = get_Const_tarval(addr);
1041     if (tarval_to_entity(tv)) {
1042       called_irg = get_entity_irg(tarval_to_entity(tv));
1043       if (called_irg && pos < MAX_INLINE) {
1044         /* The Call node calls a locally defined method.  Remember to inline. */
1045         calls[pos] = call;
1046         pos++;
1047       }
1048     }
1049   }
1050 }
1051
1052 /**
1053  * Inlines all small methods at call sites where the called address comes
1054  * from a Const node that references the entity representing the called
1055  * method.
1056  * The size argument is a rough measure for the code size of the method:
1057  * Methods where the obstack containing the firm graph is smaller than
1058  * size are inlined.
1059  */
1060 void inline_small_irgs(ir_graph *irg, int size) {
1061   int i;
1062   ir_node *calls[MAX_INLINE];
1063   ir_graph *rem = current_ir_graph;
1064
1065   if (!(get_opt_optimize() && get_opt_inline())) return;
1066
1067   current_ir_graph = irg;
1068   /* Handle graph state */
1069   assert(get_irg_phase_state(current_ir_graph) != phase_building);
1070   assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1071
1072   /* Find Call nodes to inline.
1073      (We can not inline during a walk of the graph, as inlineing the same
1074      method several times changes the visited flag of the walked graph:
1075      after the first inlineing visited of the callee equals visited of
1076      the caller.  With the next inlineing both are increased.) */
1077   pos = 0;
1078   irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
1079
1080   if ((pos > 0) && (pos < MAX_INLINE)) {
1081     /* There are calls to inline */
1082     collect_phiprojs(irg);
1083     for (i = 0; i < pos; i++) {
1084       tarval *tv;
1085       ir_graph *callee;
1086       tv = get_Const_tarval(get_Call_ptr(calls[i]));
1087       callee = get_entity_irg(tarval_to_entity(tv));
1088       if (((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) ||
1089       (get_irg_inline_property(callee) == irg_inline_forced)) {
1090         inline_method(calls[i], callee);
1091       }
1092     }
1093   }
1094
1095   current_ir_graph = rem;
1096 }
1097
1098 /**
1099  * Environment for inlining irgs.
1100  */
1101 typedef struct {
1102   int n_nodes;       /**< Nodes in graph except Id, Tuple, Proj, Start, End */
1103   int n_nodes_orig;  /**< for statistics */
1104   eset *call_nodes;  /**< All call nodes in this graph */
1105   int n_call_nodes;
1106   int n_call_nodes_orig; /**< for statistics */
1107   int n_callers;   /**< Number of known graphs that call this graphs. */
1108   int n_callers_orig; /**< for statistics */
1109 } inline_irg_env;
1110
1111 static inline_irg_env *new_inline_irg_env(void) {
1112   inline_irg_env *env = malloc(sizeof(inline_irg_env));
1113   env->n_nodes = -2; /* uncount Start, End */
1114   env->n_nodes_orig = -2; /* uncount Start, End */
1115   env->call_nodes = eset_create();
1116   env->n_call_nodes = 0;
1117   env->n_call_nodes_orig = 0;
1118   env->n_callers = 0;
1119   env->n_callers_orig = 0;
1120   return env;
1121 }
1122
1123 static void free_inline_irg_env(inline_irg_env *env) {
1124   eset_destroy(env->call_nodes);
1125   free(env);
1126 }
1127
1128 static void collect_calls2(ir_node *call, void *env) {
1129   inline_irg_env *x = (inline_irg_env *)env;
1130   ir_op *op = intern_get_irn_op(call);
1131   ir_graph *callee;
1132
1133   /* count nodes in irg */
1134   if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1135     x->n_nodes++;
1136     x->n_nodes_orig++;
1137   }
1138
1139   if (op != op_Call) return;
1140
1141   /* collect all call nodes */
1142   eset_insert(x->call_nodes, (void *)call);
1143   x->n_call_nodes++;
1144   x->n_call_nodes_orig++;
1145
1146   /* count all static callers */
1147   callee = get_call_called_irg(call);
1148   if (callee) {
1149     ((inline_irg_env *)get_irg_link(callee))->n_callers++;
1150     ((inline_irg_env *)get_irg_link(callee))->n_callers_orig++;
1151   }
1152 }
1153
1154 INLINE static int is_leave(ir_graph *irg) {
1155   return (((inline_irg_env *)get_irg_link(irg))->n_call_nodes == 0);
1156 }
1157
1158 INLINE static int is_smaller(ir_graph *callee, int size) {
1159   return (((inline_irg_env *)get_irg_link(callee))->n_nodes < size);
1160 }
1161
1162
1163 /**
1164  * Inlines small leave methods at call sites where the called address comes
1165  * from a Const node that references the entity representing the called
1166  * method.
1167  * The size argument is a rough measure for the code size of the method:
1168  * Methods where the obstack containing the firm graph is smaller than
1169  * size are inlined.
1170  */
1171 void inline_leave_functions(int maxsize, int leavesize, int size) {
1172   inline_irg_env *env;
1173   int i, n_irgs = get_irp_n_irgs();
1174   ir_graph *rem = current_ir_graph;
1175   int did_inline = 1;
1176
1177   if (!(get_opt_optimize() && get_opt_inline())) return;
1178
1179   /* extend all irgs by a temporary data structure for inlineing. */
1180   for (i = 0; i < n_irgs; ++i)
1181     set_irg_link(get_irp_irg(i), new_inline_irg_env());
1182
1183   /* Precompute information in temporary data structure. */
1184   for (i = 0; i < n_irgs; ++i) {
1185     current_ir_graph = get_irp_irg(i);
1186     assert(get_irg_phase_state(current_ir_graph) != phase_building);
1187     assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1188
1189     irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
1190          get_irg_link(current_ir_graph));
1191   }
1192
1193   /* and now inline.
1194      Inline leaves recursively -- we might construct new leaves. */
1195   /* int itercnt = 1; */
1196   while (did_inline) {
1197     /* printf("iteration %d\n", itercnt++); */
1198     did_inline = 0;
1199     for (i = 0; i < n_irgs; ++i) {
1200       ir_node *call;
1201       eset *walkset;
1202       int phiproj_computed = 0;
1203
1204       current_ir_graph = get_irp_irg(i);
1205       env = (inline_irg_env *)get_irg_link(current_ir_graph);
1206
1207       /* we can not walk and change a set, nor remove from it.
1208       So recompute.*/
1209       walkset = env->call_nodes;
1210       env->call_nodes = eset_create();
1211       for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1212         inline_irg_env *callee_env;
1213         ir_graph *callee = get_call_called_irg(call);
1214
1215         if (env->n_nodes > maxsize) break;
1216         if (callee &&
1217         ((is_leave(callee) && is_smaller(callee, leavesize)) ||
1218          (get_irg_inline_property(callee) == irg_inline_forced))) {
1219           if (!phiproj_computed) {
1220             phiproj_computed = 1;
1221             collect_phiprojs(current_ir_graph);
1222           }
1223           callee_env = (inline_irg_env *)get_irg_link(callee);
1224 /*         printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)), */
1225 /*         get_entity_name(get_irg_entity(callee))); */
1226           inline_method(call, callee);
1227           did_inline = 1;
1228           env->n_call_nodes--;
1229           eset_insert_all(env->call_nodes, callee_env->call_nodes);
1230           env->n_call_nodes += callee_env->n_call_nodes;
1231           env->n_nodes += callee_env->n_nodes;
1232           callee_env->n_callers--;
1233         } else {
1234           eset_insert(env->call_nodes, call);
1235         }
1236       }
1237       eset_destroy(walkset);
1238     }
1239   }
1240
1241   /* printf("Non leaves\n"); */
1242   /* inline other small functions. */
1243   for (i = 0; i < n_irgs; ++i) {
1244     ir_node *call;
1245     eset *walkset;
1246     int phiproj_computed = 0;
1247
1248     current_ir_graph = get_irp_irg(i);
1249     env = (inline_irg_env *)get_irg_link(current_ir_graph);
1250
1251     /* we can not walk and change a set, nor remove from it.
1252        So recompute.*/
1253     walkset = env->call_nodes;
1254     env->call_nodes = eset_create();
1255     for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1256       inline_irg_env *callee_env;
1257       ir_graph *callee = get_call_called_irg(call);
1258
1259       if (env->n_nodes > maxsize) break;
1260       if (callee && is_smaller(callee, size)) {
1261         if (!phiproj_computed) {
1262             phiproj_computed = 1;
1263             collect_phiprojs(current_ir_graph);
1264         }
1265         callee_env = (inline_irg_env *)get_irg_link(callee);
1266 /*       printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)), */
1267 /*       get_entity_name(get_irg_entity(callee))); */
1268         inline_method(call, callee);
1269         did_inline = 1;
1270         env->n_call_nodes--;
1271         eset_insert_all(env->call_nodes, callee_env->call_nodes);
1272         env->n_call_nodes += callee_env->n_call_nodes;
1273         env->n_nodes += callee_env->n_nodes;
1274         callee_env->n_callers--;
1275       } else {
1276         eset_insert(env->call_nodes, call);
1277       }
1278     }
1279     eset_destroy(walkset);
1280   }
1281
1282   for (i = 0; i < n_irgs; ++i) {
1283     current_ir_graph = get_irp_irg(i);
1284 #if 0
1285     env = (inline_irg_env *)get_irg_link(current_ir_graph);
1286     if ((env->n_call_nodes_orig != env->n_call_nodes) ||
1287     (env->n_callers_orig != env->n_callers))
1288       printf("Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1289          env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1290          env->n_callers_orig, env->n_callers,
1291          get_entity_name(get_irg_entity(current_ir_graph)));
1292 #endif
1293     free_inline_irg_env((inline_irg_env *)get_irg_link(current_ir_graph));
1294   }
1295
1296   current_ir_graph = rem;
1297 }
1298
1299 /*-----------------------------------------------------------------*/
1300 /*  Code Placement.  Pins all floating nodes to a block where they */
1301 /*  will be executed only if needed.                               */
1302 /*-----------------------------------------------------------------*/
1303
1304 /**
1305  * Find the earliest correct block for N.  --- Place N into the
1306  * same Block as its dominance-deepest Input.
1307  */
1308 static void
1309 place_floats_early(ir_node *n, pdeq *worklist)
1310 {
1311   int i, start, irn_arity;
1312
1313   /* we must not run into an infinite loop */
1314   assert (irn_not_visited(n));
1315   mark_irn_visited(n);
1316
1317   /* Place floating nodes. */
1318   if (get_op_pinned(intern_get_irn_op(n)) == floats) {
1319     int depth = 0;
1320     ir_node *b = new_Bad();   /* The block to place this node in */
1321
1322     assert(intern_get_irn_op(n) != op_Block);
1323
1324     if ((intern_get_irn_op(n) == op_Const) ||
1325     (intern_get_irn_op(n) == op_SymConst) ||
1326     (is_Bad(n)) ||
1327     (intern_get_irn_op(n) == op_Unknown)) {
1328       /* These nodes will not be placed by the loop below. */
1329       b = get_irg_start_block(current_ir_graph);
1330       depth = 1;
1331     }
1332
1333     /* find the block for this node. */
1334     irn_arity = intern_get_irn_arity(n);
1335     for (i = 0; i < irn_arity; i++) {
1336       ir_node *dep = intern_get_irn_n(n, i);
1337       ir_node *dep_block;
1338       if ((irn_not_visited(dep)) &&
1339       (get_op_pinned(intern_get_irn_op(dep)) == floats)) {
1340         place_floats_early(dep, worklist);
1341       }
1342       /* Because all loops contain at least one pinned node, now all
1343          our inputs are either pinned or place_early has already
1344          been finished on them.  We do not have any unfinished inputs!  */
1345       dep_block = get_nodes_Block(dep);
1346       if ((!is_Bad(dep_block)) &&
1347           (get_Block_dom_depth(dep_block) > depth)) {
1348         b = dep_block;
1349         depth = get_Block_dom_depth(dep_block);
1350       }
1351       /* Avoid that the node is placed in the Start block */
1352       if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
1353         b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1354         assert(b != get_irg_start_block(current_ir_graph));
1355         depth = 2;
1356       }
1357     }
1358     set_nodes_Block(n, b);
1359   }
1360
1361   /* Add predecessors of non floating nodes on worklist. */
1362   start = (intern_get_irn_op(n) == op_Block) ? 0 : -1;
1363   irn_arity = intern_get_irn_arity(n);
1364   for (i = start; i < irn_arity; i++) {
1365     ir_node *pred = intern_get_irn_n(n, i);
1366     if (irn_not_visited(pred)) {
1367       pdeq_putr (worklist, pred);
1368     }
1369   }
1370 }
1371
1372 /**
1373  * Floating nodes form subgraphs that begin at nodes as Const, Load,
1374  * Start, Call and end at pinned nodes as Store, Call.  Place_early
1375  * places all floating nodes reachable from its argument through floating
1376  * nodes and adds all beginnings at pinned nodes to the worklist.
1377  */
1378 static INLINE void place_early(pdeq* worklist) {
1379   assert(worklist);
1380   inc_irg_visited(current_ir_graph);
1381
1382   /* this inits the worklist */
1383   place_floats_early(get_irg_end(current_ir_graph), worklist);
1384
1385   /* Work the content of the worklist. */
1386   while (!pdeq_empty (worklist)) {
1387     ir_node *n = pdeq_getl (worklist);
1388     if (irn_not_visited(n)) place_floats_early(n, worklist);
1389   }
1390
1391   set_irg_outs_inconsistent(current_ir_graph);
1392   current_ir_graph->pinned = pinned;
1393 }
1394
1395
1396 /** deepest common dominance ancestor of DCA and CONSUMER of PRODUCER. */
1397 static ir_node *
1398 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
1399 {
1400   ir_node *block = NULL;
1401
1402   /* Compute the latest block into which we can place a node so that it is
1403      before consumer. */
1404   if (intern_get_irn_op(consumer) == op_Phi) {
1405     /* our consumer is a Phi-node, the effective use is in all those
1406        blocks through which the Phi-node reaches producer */
1407     int i, irn_arity;
1408     ir_node *phi_block = get_nodes_Block(consumer);
1409     irn_arity = intern_get_irn_arity(consumer);
1410     for (i = 0;  i < irn_arity; i++) {
1411       if (intern_get_irn_n(consumer, i) == producer) {
1412     block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
1413       }
1414     }
1415   } else {
1416     assert(is_no_Block(consumer));
1417     block = get_nodes_Block(consumer);
1418   }
1419
1420   /* Compute the deepest common ancestor of block and dca. */
1421   assert(block);
1422   if (!dca) return block;
1423   while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1424     block = get_Block_idom(block);
1425   while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
1426     dca = get_Block_idom(dca);
1427   while (block != dca)
1428     { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1429
1430   return dca;
1431 }
1432
1433 static INLINE int get_irn_loop_depth(ir_node *n) {
1434   return get_loop_depth(get_irn_loop(n));
1435 }
1436
1437 /**
1438  * Move n to a block with less loop depth than it's current block. The
1439  * new block must be dominated by early.
1440  */
1441 static void
1442 move_out_of_loops (ir_node *n, ir_node *early)
1443 {
1444   ir_node *best, *dca;
1445   assert(n && early);
1446
1447
1448   /* Find the region deepest in the dominator tree dominating
1449      dca with the least loop nesting depth, but still dominated
1450      by our early placement. */
1451   dca = get_nodes_Block(n);
1452   best = dca;
1453   while (dca != early) {
1454     dca = get_Block_idom(dca);
1455     if (!dca) break; /* should we put assert(dca)? */
1456     if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1457       best = dca;
1458     }
1459   }
1460   if (best != get_nodes_Block(n)) {
1461     /* debug output
1462     printf("Moving out of loop: "); DDMN(n);
1463     printf(" Outermost block: "); DDMN(early);
1464     printf(" Best block: "); DDMN(best);
1465     printf(" Innermost block: "); DDMN(get_nodes_Block(n));
1466     */
1467     set_nodes_Block(n, best);
1468   }
1469 }
1470
1471 /**
1472  * Find the latest legal block for N and place N into the
1473  * `optimal' Block between the latest and earliest legal block.
1474  * The `optimal' block is the dominance-deepest block of those
1475  * with the least loop-nesting-depth.  This places N out of as many
1476  * loops as possible and then makes it as control dependant as
1477  * possible.
1478  */
1479 static void
1480 place_floats_late(ir_node *n, pdeq *worklist)
1481 {
1482   int i;
1483   ir_node *early;
1484
1485   assert (irn_not_visited(n)); /* no multiple placement */
1486
1487   /* no need to place block nodes, control nodes are already placed. */
1488   if ((intern_get_irn_op(n) != op_Block) &&
1489       (!is_cfop(n)) &&
1490       (intern_get_irn_mode(n) != mode_X)) {
1491     /* Remember the early placement of this block to move it
1492        out of loop no further than the early placement. */
1493     early = get_nodes_Block(n);
1494     /* Assure that our users are all placed, except the Phi-nodes.
1495        --- Each data flow cycle contains at least one Phi-node.  We
1496        have to break the `user has to be placed before the
1497        producer' dependence cycle and the Phi-nodes are the
1498        place to do so, because we need to base our placement on the
1499        final region of our users, which is OK with Phi-nodes, as they
1500        are pinned, and they never have to be placed after a
1501        producer of one of their inputs in the same block anyway. */
1502     for (i = 0; i < get_irn_n_outs(n); i++) {
1503       ir_node *succ = get_irn_out(n, i);
1504       if (irn_not_visited(succ) && (intern_get_irn_op(succ) != op_Phi))
1505     place_floats_late(succ, worklist);
1506     }
1507
1508     /* We have to determine the final block of this node... except for
1509        constants. */
1510     if ((get_op_pinned(intern_get_irn_op(n)) == floats) &&
1511     (intern_get_irn_op(n) != op_Const) &&
1512     (intern_get_irn_op(n) != op_SymConst)) {
1513       ir_node *dca = NULL;  /* deepest common ancestor in the
1514                    dominator tree of all nodes'
1515                    blocks depending on us; our final
1516                    placement has to dominate DCA. */
1517       for (i = 0; i < get_irn_n_outs(n); i++) {
1518     dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
1519       }
1520       set_nodes_Block(n, dca);
1521
1522       move_out_of_loops (n, early);
1523     }
1524   }
1525
1526   mark_irn_visited(n);
1527
1528   /* Add predecessors of all non-floating nodes on list. (Those of floating
1529      nodes are placeded already and therefore are marked.)  */
1530   for (i = 0; i < get_irn_n_outs(n); i++) {
1531     if (irn_not_visited(get_irn_out(n, i))) {
1532       pdeq_putr (worklist, get_irn_out(n, i));
1533     }
1534   }
1535 }
1536
1537 static INLINE void place_late(pdeq* worklist) {
1538   assert(worklist);
1539   inc_irg_visited(current_ir_graph);
1540
1541   /* This fills the worklist initially. */
1542   place_floats_late(get_irg_start_block(current_ir_graph), worklist);
1543   /* And now empty the worklist again... */
1544   while (!pdeq_empty (worklist)) {
1545     ir_node *n = pdeq_getl (worklist);
1546     if (irn_not_visited(n)) place_floats_late(n, worklist);
1547   }
1548 }
1549
1550 void place_code(ir_graph *irg) {
1551   pdeq* worklist;
1552   ir_graph *rem = current_ir_graph;
1553
1554   current_ir_graph = irg;
1555
1556   if (!(get_opt_optimize() && get_opt_global_cse())) return;
1557
1558   /* Handle graph state */
1559   assert(get_irg_phase_state(irg) != phase_building);
1560   if (get_irg_dom_state(irg) != dom_consistent)
1561     compute_doms(irg);
1562
1563   if (get_irg_loopinfo_state(irg) != loopinfo_consistent) {
1564     free_loop_information(irg);
1565     construct_backedges(irg);
1566   }
1567
1568   /* Place all floating nodes as early as possible. This guarantees
1569      a legal code placement. */
1570   worklist = new_pdeq();
1571   place_early(worklist);
1572
1573   /* place_early invalidates the outs, place_late needs them. */
1574   compute_outs(irg);
1575   /* Now move the nodes down in the dominator tree. This reduces the
1576      unnecessary executions of the node. */
1577   place_late(worklist);
1578
1579   set_irg_outs_inconsistent(current_ir_graph);
1580   set_irg_loopinfo_inconsistent(current_ir_graph);
1581   del_pdeq(worklist);
1582   current_ir_graph = rem;
1583 }
1584
1585
1586
1587 /********************************************************************/
1588 /* Control flow optimization.                                       */
1589 /* Removes Bad control flow predecessors and empty blocks.  A block */
1590 /* is empty if it contains only a Jmp node.                         */
1591 /* Blocks can only be removed if they are not needed for the        */
1592 /* semantics of Phi nodes.                                          */
1593 /********************************************************************/
1594
1595 /**
1596  * Removes Tuples from Block control flow predecessors.
1597  * Optimizes blocks with equivalent_node().
1598  * Replaces n by Bad if n is unreachable control flow.
1599  */
1600 static void merge_blocks(ir_node *n, void *env) {
1601   int i;
1602   set_irn_link(n, NULL);
1603
1604   if (intern_get_irn_op(n) == op_Block) {
1605     /* Remove Tuples */
1606     for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1607       /* GL @@@ : is this possible? if (get_opt_normalize()) -- added, all tests go through.
1608      A different order of optimizations might cause problems. */
1609       if (get_opt_normalize())
1610     set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1611   } else if (get_opt_optimize() && (intern_get_irn_mode(n) == mode_X)) {
1612     /* We will soon visit a block.  Optimize it before visiting! */
1613     ir_node *b = get_nodes_Block(n);
1614     ir_node *new_node = equivalent_node(b);
1615     while (irn_not_visited(b) && (!is_Bad(new_node)) && (new_node != b)) {
1616       /* We would have to run gigo if new is bad, so we
1617      promote it directly below. */
1618       assert(((b == new_node) ||
1619           get_opt_control_flow_straightening() ||
1620           get_opt_control_flow_weak_simplification()) &&
1621          ("strange flag setting"));
1622       exchange (b, new_node);
1623       b = new_node;
1624       new_node = equivalent_node(b);
1625     }
1626     /* GL @@@ get_opt_normalize hinzugefuegt, 5.5.2003 */
1627     if (is_Bad(new_node) && get_opt_normalize()) exchange(n, new_Bad());
1628   }
1629 }
1630
1631 /**
1632  * Collects all Phi nodes in link list of Block.
1633  * Marks all blocks "block_visited" if they contain a node other
1634  * than Jmp.
1635  */
1636 static void collect_nodes(ir_node *n, void *env) {
1637   if (is_no_Block(n)) {
1638     ir_node *b = get_nodes_Block(n);
1639
1640     if ((intern_get_irn_op(n) == op_Phi)) {
1641       /* Collect Phi nodes to compact ins along with block's ins. */
1642       set_irn_link(n, get_irn_link(b));
1643       set_irn_link(b, n);
1644     } else if (intern_get_irn_op(n) != op_Jmp) {  /* Check for non empty block. */
1645       mark_Block_block_visited(b);
1646     }
1647   }
1648 }
1649
1650 /** Returns true if pred is predecessor of block. */
1651 static int is_pred_of(ir_node *pred, ir_node *b) {
1652   int i;
1653   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1654     ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1655     if (b_pred == pred) return 1;
1656   }
1657   return 0;
1658 }
1659
1660 static int test_whether_dispensable(ir_node *b, int pos) {
1661   int i, j, n_preds = 1;
1662   int dispensable = 1;
1663   ir_node *cfop = get_Block_cfgpred(b, pos);
1664   ir_node *pred = get_nodes_Block(cfop);
1665
1666   if (get_Block_block_visited(pred) + 1
1667       < get_irg_block_visited(current_ir_graph)) {
1668     if (!get_opt_optimize() || !get_opt_control_flow_strong_simplification()) {
1669       /* Mark block so that is will not be removed. */
1670       set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1671       return 1;
1672     }
1673     /* Seems to be empty. */
1674     if (!get_irn_link(b)) {
1675       /* There are no Phi nodes ==> dispensable. */
1676       n_preds = get_Block_n_cfgpreds(pred);
1677     } else {
1678       /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1679      Work preds < pos as if they were already removed. */
1680       for (i = 0; i < pos; i++) {
1681     ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1682     if (get_Block_block_visited(b_pred) + 1
1683         < get_irg_block_visited(current_ir_graph)) {
1684       for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1685         ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1686         if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1687       }
1688     } else {
1689       if (is_pred_of(b_pred, pred)) dispensable = 0;
1690     }
1691       }
1692       for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1693     ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1694     if (is_pred_of(b_pred, pred)) dispensable = 0;
1695       }
1696       if (!dispensable) {
1697     set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1698     n_preds = 1;
1699       } else {
1700     n_preds = get_Block_n_cfgpreds(pred);
1701       }
1702     }
1703   }
1704
1705   return n_preds;
1706 }
1707
1708 static void optimize_blocks(ir_node *b, void *env) {
1709   int i, j, k, max_preds, n_preds;
1710   ir_node *pred, *phi;
1711   ir_node **in;
1712
1713   /* Count the number of predecessor if this block is merged with pred blocks
1714      that are empty. */
1715   max_preds = 0;
1716   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1717     max_preds += test_whether_dispensable(b, i);
1718   }
1719   in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1720
1721 /*-
1722   printf(" working on "); DDMN(b);
1723   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1724     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1725     if (is_Bad(get_Block_cfgpred(b, i))) {
1726       printf("  removing Bad %i\n ", i);
1727     } else if (get_Block_block_visited(pred) +1
1728            < get_irg_block_visited(current_ir_graph)) {
1729       printf("  removing pred %i ", i); DDMN(pred);
1730     } else { printf("  Nothing to do for "); DDMN(pred); }
1731   }
1732   * end Debug output -*/
1733
1734   /*- Fix the Phi nodes -*/
1735   phi = get_irn_link(b);
1736   while (phi) {
1737     assert(intern_get_irn_op(phi) == op_Phi);
1738     /* Find the new predecessors for the Phi */
1739     n_preds = 0;
1740     for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1741       pred = get_nodes_Block(get_Block_cfgpred(b, i));
1742       if (is_Bad(get_Block_cfgpred(b, i))) {
1743     /* Do nothing */
1744       } else if (get_Block_block_visited(pred) +1
1745          < get_irg_block_visited(current_ir_graph)) {
1746     /* It's an empty block and not yet visited. */
1747     ir_node *phi_pred = get_Phi_pred(phi, i);
1748     for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1749       if (get_nodes_Block(phi_pred) == pred) {
1750         assert(intern_get_irn_op(phi_pred) == op_Phi);  /* Block is empty!! */
1751         in[n_preds] = get_Phi_pred(phi_pred, j);
1752       } else {
1753         in[n_preds] = phi_pred;
1754       }
1755       n_preds++;
1756     }
1757     /* The Phi_pred node is replaced now if it is a Phi.
1758        In Schleifen kann offenbar der entfernte Phi Knoten legal verwendet werden.
1759        Daher muss der Phiknoten durch den neuen ersetzt werden.
1760        Weiter muss der alte Phiknoten entfernt werden (durch ersetzen oder
1761        durch einen Bad) damit er aus den keep_alive verschwinden kann.
1762        Man sollte also, falls keine Schleife vorliegt, exchange mit new_Bad
1763        aufrufen.  */
1764     if (get_nodes_Block(phi_pred) == pred) {
1765       /* remove the Phi as it might be kept alive. Further there
1766          might be other users. */
1767       exchange(phi_pred, phi);  /* geht, ist aber doch semantisch falsch! Warum?? */
1768     }
1769       } else {
1770     in[n_preds] = get_Phi_pred(phi, i);
1771     n_preds ++;
1772       }
1773     }
1774     /* Fix the node */
1775     set_irn_in(phi, n_preds, in);
1776
1777     phi = get_irn_link(phi);
1778   }
1779
1780 /*-
1781       This happens only if merge between loop backedge and single loop entry. -*/
1782   for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1783     pred = get_nodes_Block(get_Block_cfgpred(b, k));
1784     if (get_Block_block_visited(pred) +1
1785     < get_irg_block_visited(current_ir_graph)) {
1786       phi = get_irn_link(pred);
1787       while (phi) {
1788     if (intern_get_irn_op(phi) == op_Phi) {
1789       set_nodes_Block(phi, b);
1790
1791       n_preds = 0;
1792       for (i = 0; i < k; i++) {
1793         pred = get_nodes_Block(get_Block_cfgpred(b, i));
1794         if (is_Bad(get_Block_cfgpred(b, i))) {
1795           /* Do nothing */
1796         } else if (get_Block_block_visited(pred) +1
1797                < get_irg_block_visited(current_ir_graph)) {
1798           /* It's an empty block and not yet visited. */
1799           for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1800         /* @@@ Hier brauche ich Schleifeninformation!!! Kontrollflusskante
1801            muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1802            Anweisungen.) Trotzdem tuts bisher!! */
1803         in[n_preds] = phi;
1804         n_preds++;
1805           }
1806         } else {
1807           in[n_preds] = phi;
1808           n_preds++;
1809         }
1810       }
1811       for (i = 0; i < get_Phi_n_preds(phi); i++) {
1812         in[n_preds] = get_Phi_pred(phi, i);
1813         n_preds++;
1814       }
1815       for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1816         pred = get_nodes_Block(get_Block_cfgpred(b, i));
1817         if (is_Bad(get_Block_cfgpred(b, i))) {
1818           /* Do nothing */
1819         } else if (get_Block_block_visited(pred) +1
1820                < get_irg_block_visited(current_ir_graph)) {
1821           /* It's an empty block and not yet visited. */
1822           for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1823         in[n_preds] = phi;
1824         n_preds++;
1825           }
1826         } else {
1827           in[n_preds] = phi;
1828           n_preds++;
1829         }
1830       }
1831       set_irn_in(phi, n_preds, in);
1832     }
1833     phi = get_irn_link(phi);
1834       }
1835     }
1836   }
1837
1838   /*- Fix the block -*/
1839   n_preds = 0;
1840   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1841     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1842     if (is_Bad(get_Block_cfgpred(b, i))) {
1843       /* Do nothing */
1844     } else if (get_Block_block_visited(pred) +1
1845            < get_irg_block_visited(current_ir_graph)) {
1846       /* It's an empty block and not yet visited. */
1847       assert(get_Block_n_cfgpreds(b) > 1);
1848                         /* Else it should be optimized by equivalent_node. */
1849       for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1850     in[n_preds] = get_Block_cfgpred(pred, j);
1851     n_preds++;
1852       }
1853       /* Remove block as it might be kept alive. */
1854       exchange(pred, b/*new_Bad()*/);
1855     } else {
1856       in[n_preds] = get_Block_cfgpred(b, i);
1857       n_preds ++;
1858     }
1859   }
1860   set_irn_in(b, n_preds, in);
1861   free(in);
1862 }
1863
1864 void optimize_cf(ir_graph *irg) {
1865   int i;
1866   ir_node **in;
1867   ir_node *end = get_irg_end(irg);
1868   ir_graph *rem = current_ir_graph;
1869   current_ir_graph = irg;
1870
1871   /* Handle graph state */
1872   assert(get_irg_phase_state(irg) != phase_building);
1873   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1874     set_irg_outs_inconsistent(current_ir_graph);
1875   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1876     set_irg_dom_inconsistent(current_ir_graph);
1877
1878   /* Use block visited flag to mark non-empty blocks. */
1879   inc_irg_block_visited(irg);
1880   irg_walk(end, merge_blocks, collect_nodes, NULL);
1881
1882   /* Optimize the standard code. */
1883   irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1884
1885   /* Walk all keep alives, optimize them if block, add to new in-array
1886      for end if useful. */
1887   in = NEW_ARR_F (ir_node *, 1);
1888   in[0] = get_nodes_Block(end);
1889   inc_irg_visited(current_ir_graph);
1890   for(i = 0; i < get_End_n_keepalives(end); i++) {
1891     ir_node *ka = get_End_keepalive(end, i);
1892     if (irn_not_visited(ka)) {
1893       if ((intern_get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1894     set_irg_block_visited(current_ir_graph,  /* Don't walk all the way to Start. */
1895                   get_irg_block_visited(current_ir_graph)-1);
1896     irg_block_walk(ka, optimize_blocks, NULL, NULL);
1897     mark_irn_visited(ka);
1898     ARR_APP1 (ir_node *, in, ka);
1899       } else if (intern_get_irn_op(ka) == op_Phi) {
1900     mark_irn_visited(ka);
1901     ARR_APP1 (ir_node *, in, ka);
1902       }
1903     }
1904   }
1905   /* DEL_ARR_F(end->in);   GL @@@ tut nicht ! */
1906   end->in = in;
1907
1908   current_ir_graph = rem;
1909 }
1910
1911
1912 /**
1913  * Called by walker of remove_critical_cf_edges.
1914  *
1915  * Place an empty block to an edge between a blocks of multiple
1916  * predecessors and a block of multiple successors.
1917  *
1918  * @param n IR node
1919  * @param env Environment of walker. This field is unused and has
1920  *            the value NULL.
1921  */
1922 static void walk_critical_cf_edges(ir_node *n, void *env) {
1923   int arity, i;
1924   ir_node *pre, *block, **in, *jmp;
1925
1926   /* Block has multiple predecessors */
1927   if ((op_Block == intern_get_irn_op(n)) &&
1928       (intern_get_irn_arity(n) > 1)) {
1929     arity = intern_get_irn_arity(n);
1930
1931     if (n == get_irg_end_block(current_ir_graph))
1932       return;  /*  No use to add a block here.      */
1933
1934     for (i=0; i<arity; i++) {
1935       pre = intern_get_irn_n(n, i);
1936       /* Predecessor has multiple successors. Insert new flow edge */
1937       if ((NULL != pre) &&
1938       (op_Proj == intern_get_irn_op(pre)) &&
1939       op_Raise != intern_get_irn_op(skip_Proj(pre))) {
1940
1941     /* set predecessor array for new block */
1942     in = NEW_ARR_D (ir_node *, current_ir_graph->obst, 1);
1943     /* set predecessor of new block */
1944     in[0] = pre;
1945     block = new_Block(1, in);
1946     /* insert new jmp node to new block */
1947     switch_block(block);
1948     jmp = new_Jmp();
1949     switch_block(n);
1950     /* set successor of new block */
1951     set_irn_n(n, i, jmp);
1952
1953       } /* predecessor has multiple successors */
1954     } /* for all predecessors */
1955   } /* n is a block */
1956 }
1957
1958 void remove_critical_cf_edges(ir_graph *irg) {
1959   if (get_opt_critical_edges())
1960     irg_walk_graph(irg, NULL, walk_critical_cf_edges, NULL);
1961 }