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