removed assertion
[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   assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
416   free_outs(current_ir_graph);
417
418   /* @@@ so far we loose loops when copying */
419   set_irg_loop(current_ir_graph, NULL);
420
421   if (get_optimize() && get_opt_dead_node_elimination()) {
422
423     /* A quiet place, where the old obstack can rest in peace,
424        until it will be cremated. */
425     graveyard_obst = irg->obst;
426
427     /* A new obstack, where the reachable nodes will be copied to. */
428     rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
429     current_ir_graph->obst = rebirth_obst;
430     obstack_init (current_ir_graph->obst);
431
432     /* We also need a new hash table for cse */
433     del_identities (irg->value_table);
434     irg->value_table = new_identities ();
435
436     /* Copy the graph from the old to the new obstack */
437     copy_graph_env();
438
439     /* Free memory from old unoptimized obstack */
440     obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
441     xfree (graveyard_obst);           /* ... then free it.           */
442   }
443
444   current_ir_graph = rem;
445 }
446
447 /* Relink bad predeseccors of a block and store the old in array to the
448    link field. This function is called by relink_bad_predecessors().
449    The array of link field starts with the block operand at position 0.
450    If block has bad predecessors, create a new in array without bad preds.
451    Otherwise let in array untouched. */
452 static void relink_bad_block_predecessors(ir_node *n, void *env) {
453   ir_node **new_in, *irn;
454   int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
455
456   /* if link field of block is NULL, look for bad predecessors otherwise
457      this is allready done */
458   if (get_irn_op(n) == op_Block &&
459       get_irn_link(n) == NULL) {
460
461     /* save old predecessors in link field (position 0 is the block operand)*/
462     set_irn_link(n, (void *)get_irn_in(n));
463
464     /* count predecessors without bad nodes */
465     old_irn_arity = get_irn_arity(n);
466     for (i = 0; i < old_irn_arity; i++)
467       if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
468
469     /* arity changing: set new predecessors without bad nodes */
470     if (new_irn_arity < old_irn_arity) {
471       /* get new predecessor array without Block predecessor */
472       new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
473
474       /* set new predeseccors in array */
475       new_in[0] = NULL;
476       new_irn_n = 1;
477       for (i = 1; i < old_irn_arity; i++) {
478         irn = get_irn_n(n, i);
479         if (!is_Bad(irn)) new_in[new_irn_n++] = irn;
480       }
481       n->in = new_in;
482     } /* ir node has bad predecessors */
483
484   } /* Block is not relinked */
485 }
486
487 /* Relinks Bad predecesors from Bocks and Phis called by walker
488    remove_bad_predecesors(). If n is a Block, call
489    relink_bad_block_redecessors(). If n is a Phinode, call also the relinking
490    function of Phi's Block. If this block has bad predecessors, relink preds
491    of the Phinode. */
492 static void relink_bad_predecessors(ir_node *n, void *env) {
493   ir_node *block, **old_in;
494   int i, old_irn_arity, new_irn_arity;
495
496   /* relink bad predeseccors of a block */
497   if (get_irn_op(n) == op_Block)
498     relink_bad_block_predecessors(n, env);
499
500   /* If Phi node relink its block and its predecessors */
501   if (get_irn_op(n) == op_Phi) {
502
503     /* Relink predeseccors of phi's block */
504     block = get_nodes_Block(n);
505     if (get_irn_link(block) == NULL)
506       relink_bad_block_predecessors(block, env);
507
508     old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
509     old_irn_arity = ARR_LEN(old_in);
510
511     /* Relink Phi predeseccors if count of predeseccors changed */
512     if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
513       /* set new predeseccors in array
514          n->in[0] remains the same block */
515       new_irn_arity = 1;
516       for(i = 1; i < old_irn_arity; i++)
517         if (!is_Bad((ir_node *)old_in[i])) n->in[new_irn_arity++] = n->in[i];
518
519       ARR_SETLEN(ir_node *, n->in, new_irn_arity);
520     }
521
522   } /* n is a Phi node */
523 }
524
525 /* Removes Bad Bad predecesors from Blocks and the corresponding
526    inputs to Phi nodes as in dead_node_elimination but without
527    copying the graph.
528    On walking up set the link field to NULL, on walking down call
529    relink_bad_predecessors() (This function stores the old in array
530    to the link field and sets a new in array if arity of predecessors
531    changes) */
532 void remove_bad_predecessors(ir_graph *irg) {
533   irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
534 }
535
536
537 /**********************************************************************/
538 /*  Funcionality for inlining                                         */
539 /**********************************************************************/
540
541 /* Copy node for inlineing.  Updates attributes that change when
542  * inlineing but not for dead node elimination.
543  *
544  * Copies the node by calling copy_node and then updates the entity if
545  * it's a local one.  env must be a pointer of the frame type of the
546  * inlined procedure. The new entities must be in the link field of
547  * the entities. */
548 static INLINE void
549 copy_node_inline (ir_node *n, void *env) {
550   ir_node *new;
551   type *frame_tp = (type *)env;
552
553   copy_node(n, NULL);
554   if (get_irn_op(n) == op_Sel) {
555     new = get_new_node (n);
556     assert(get_irn_op(new) == op_Sel);
557     if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
558       set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
559     }
560   } else if (get_irn_op(n) == op_Block) {
561     new = get_new_node (n);
562     new->attr.block.irg = current_ir_graph;
563   }
564 }
565
566
567 void inline_method(ir_node *call, ir_graph *called_graph) {
568   ir_node *pre_call;
569   ir_node *post_call, *post_bl;
570   ir_node *in[5];
571   ir_node *end, *end_bl;
572   ir_node **res_pred;
573   ir_node **cf_pred;
574   ir_node *ret, *phi;
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   assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
976
977   /* Find Call nodes to inline.
978      (We can not inline during a walk of the graph, as inlineing the same
979      method several times changes the visited flag of the walked graph:
980      after the first inlineing visited of the callee equals visited of
981      the caller.  With the next inlineing both are increased.) */
982   pos = 0;
983   irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
984
985   if ((pos > 0) && (pos < MAX_INLINE)) {
986     /* There are calls to inline */
987     collect_phiprojs(irg);
988     for (i = 0; i < pos; i++) {
989       tarval *tv;
990       ir_graph *callee;
991       tv = get_Const_tarval(get_Call_ptr(calls[i]));
992       callee = get_entity_irg(tarval_to_entity(tv));
993       if ((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) {
994         inline_method(calls[i], callee);
995       }
996     }
997   }
998
999   current_ir_graph = rem;
1000 }
1001
1002 typedef struct {
1003   int n_nodes;       /* Nodes in graph except Id, Tuple, Proj, Start, End */
1004   int n_nodes_orig;  /* for statistics */
1005   eset *call_nodes;  /* All call nodes in this graph */
1006   int n_call_nodes;
1007   int n_call_nodes_orig; /* for statistics */
1008   int n_callers;   /* Number of known graphs that call this graphs. */
1009   int n_callers_orig; /* for statistics */
1010 } inline_irg_env;
1011
1012 static inline_irg_env *new_inline_irg_env(void) {
1013   inline_irg_env *env = malloc(sizeof(inline_irg_env));
1014   env->n_nodes = -2; /* uncount Start, End */
1015   env->n_nodes_orig = -2; /* uncount Start, End */
1016   env->call_nodes = eset_create();
1017   env->n_call_nodes = 0;
1018   env->n_call_nodes_orig = 0;
1019   env->n_callers = 0;
1020   env->n_callers_orig = 0;
1021   return env;
1022 }
1023
1024 static void free_inline_irg_env(inline_irg_env *env) {
1025   eset_destroy(env->call_nodes);
1026   free(env);
1027 }
1028
1029 static void collect_calls2(ir_node *call, void *env) {
1030   inline_irg_env *x = (inline_irg_env *)env;
1031   ir_op *op = get_irn_op(call);
1032
1033   /* count nodes in irg */
1034   if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1035     x->n_nodes++;
1036     x->n_nodes_orig++;
1037   }
1038
1039   if (op != op_Call) return;
1040
1041   /* collect all call nodes */
1042   eset_insert(x->call_nodes, (void *)call);
1043   x->n_call_nodes++;
1044   x->n_call_nodes_orig++;
1045
1046   /* count all static callers */
1047   ir_graph *callee = get_call_called_irg(call);
1048   if (callee) {
1049     ((inline_irg_env *)get_irg_link(callee))->n_callers++;
1050     ((inline_irg_env *)get_irg_link(callee))->n_callers_orig++;
1051   }
1052 }
1053
1054 INLINE static int is_leave(ir_graph *irg) {
1055   return (((inline_irg_env *)get_irg_link(irg))->n_call_nodes == 0);
1056 }
1057
1058 INLINE static int is_smaller(ir_graph *callee, int size) {
1059   return (((inline_irg_env *)get_irg_link(callee))->n_nodes < size);
1060 }
1061
1062
1063 /* Inlines small leave methods at call sites where the called address comes
1064    from a Const node that references the entity representing the called
1065    method.
1066    The size argument is a rough measure for the code size of the method:
1067    Methods where the obstack containing the firm graph is smaller than
1068    size are inlined. */
1069 void inline_leave_functions(int maxsize, int leavesize, int size) {
1070   inline_irg_env *env;
1071   int i, n_irgs = get_irp_n_irgs();
1072   ir_graph *rem = current_ir_graph;
1073   int did_inline = 1;
1074
1075   if (!(get_optimize() && get_opt_inline())) return;
1076
1077   /* extend all irgs by a temporary datastructure for inlineing. */
1078   for (i = 0; i < n_irgs; ++i)
1079     set_irg_link(get_irp_irg(i), new_inline_irg_env());
1080
1081   /* Precompute information in temporary datastructure. */
1082   for (i = 0; i < n_irgs; ++i) {
1083     current_ir_graph = get_irp_irg(i);
1084     assert(get_irg_phase_state(current_ir_graph) != phase_building);
1085     assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1086
1087     irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
1088              get_irg_link(current_ir_graph));
1089     env = (inline_irg_env *)get_irg_link(current_ir_graph);
1090   }
1091
1092   /* and now inline.
1093      Inline leaves recursively -- we might construct new leaves. */
1094   //int itercnt = 1;
1095   while (did_inline) {
1096     //printf("iteration %d\n", itercnt++);
1097     did_inline = 0;
1098     for (i = 0; i < n_irgs; ++i) {
1099       ir_node *call;
1100       eset *walkset;
1101       int phiproj_computed = 0;
1102
1103       current_ir_graph = get_irp_irg(i);
1104       env = (inline_irg_env *)get_irg_link(current_ir_graph);
1105
1106       /* we can not walk and change a set, nor remove from it.
1107          So recompute.*/
1108       walkset = env->call_nodes;
1109       env->call_nodes = eset_create();
1110       for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1111         ir_graph *callee = get_call_called_irg(call);
1112         if (env->n_nodes > maxsize) break;
1113         if (callee && is_leave(callee) && is_smaller(callee, leavesize)) {
1114           if (!phiproj_computed) {
1115             phiproj_computed = 1;
1116             collect_phiprojs(current_ir_graph);
1117           }
1118           inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1119           // printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)),
1120           //     get_entity_name(get_irg_entity(callee)));
1121           inline_method(call, callee);
1122           did_inline = 1;
1123           env->n_call_nodes--;
1124           eset_insert_all(env->call_nodes, callee_env->call_nodes);
1125           env->n_call_nodes += callee_env->n_call_nodes;
1126           env->n_nodes += callee_env->n_nodes;
1127           callee_env->n_callers--;
1128         } else {
1129           eset_insert(env->call_nodes, call);
1130         }
1131       }
1132       eset_destroy(walkset);
1133     }
1134   }
1135
1136   //printf("Non leaves\n");
1137   /* inline other small functions. */
1138   for (i = 0; i < n_irgs; ++i) {
1139     ir_node *call;
1140     eset *walkset;
1141     int phiproj_computed = 0;
1142
1143     current_ir_graph = get_irp_irg(i);
1144     env = (inline_irg_env *)get_irg_link(current_ir_graph);
1145
1146     /* we can not walk and change a set, nor remove from it.
1147        So recompute.*/
1148     walkset = env->call_nodes;
1149     env->call_nodes = eset_create();
1150     for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1151       ir_graph *callee = get_call_called_irg(call);
1152       if (env->n_nodes > maxsize) break;
1153       if (callee && is_smaller(callee, size)) {
1154         if (!phiproj_computed) {
1155           phiproj_computed = 1;
1156           collect_phiprojs(current_ir_graph);
1157         }
1158         inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1159         //printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)),
1160         //       get_entity_name(get_irg_entity(callee)));
1161         inline_method(call, callee);
1162         did_inline = 1;
1163         env->n_call_nodes--;
1164         eset_insert_all(env->call_nodes, callee_env->call_nodes);
1165         env->n_call_nodes += callee_env->n_call_nodes;
1166         env->n_nodes += callee_env->n_nodes;
1167         callee_env->n_callers--;
1168       } else {
1169         eset_insert(env->call_nodes, call);
1170       }
1171     }
1172     eset_destroy(walkset);
1173   }
1174
1175   for (i = 0; i < n_irgs; ++i) {
1176     current_ir_graph = get_irp_irg(i);
1177 #if 0
1178     env = (inline_irg_env *)get_irg_link(current_ir_graph);
1179     if ((env->n_call_nodes_orig != env->n_call_nodes) ||
1180         (env->n_callers_orig != env->n_callers))
1181       printf("Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1182              env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1183              env->n_callers_orig, env->n_callers,
1184              get_entity_name(get_irg_entity(current_ir_graph)));
1185 #endif
1186     free_inline_irg_env((inline_irg_env *)get_irg_link(current_ir_graph));
1187   }
1188
1189   current_ir_graph = rem;
1190 }
1191
1192 /********************************************************************/
1193 /*  Code Placement.  Pinns all floating nodes to a block where they */
1194 /*  will be executed only if needed.                                */
1195 /********************************************************************/
1196
1197 static pdeq *worklist;          /* worklist of ir_node*s */
1198
1199 /* Find the earliest correct block for N.  --- Place N into the
1200    same Block as its dominance-deepest Input.  */
1201 static void
1202 place_floats_early (ir_node *n)
1203 {
1204   int i, start, irn_arity;
1205
1206   /* we must not run into an infinite loop */
1207   assert (irn_not_visited(n));
1208   mark_irn_visited(n);
1209
1210   /* Place floating nodes. */
1211   if (get_op_pinned(get_irn_op(n)) == floats) {
1212     int depth = 0;
1213     ir_node *b = new_Bad();   /* The block to place this node in */
1214
1215     assert(get_irn_op(n) != op_Block);
1216
1217     if ((get_irn_op(n) == op_Const) ||
1218         (get_irn_op(n) == op_SymConst) ||
1219         (is_Bad(n)) ||
1220         (get_irn_op(n) == op_Unknown)) {
1221       /* These nodes will not be placed by the loop below. */
1222       b = get_irg_start_block(current_ir_graph);
1223       depth = 1;
1224     }
1225
1226     /* find the block for this node. */
1227     irn_arity = get_irn_arity(n);
1228     for (i = 0; i < irn_arity; i++) {
1229       ir_node *dep = get_irn_n(n, i);
1230       ir_node *dep_block;
1231       if ((irn_not_visited(dep)) &&
1232           (get_op_pinned(get_irn_op(dep)) == floats)) {
1233         place_floats_early (dep);
1234       }
1235       /* Because all loops contain at least one pinned node, now all
1236          our inputs are either pinned or place_early has already
1237          been finished on them.  We do not have any unfinished inputs!  */
1238       dep_block = get_nodes_Block(dep);
1239       if ((!is_Bad(dep_block)) &&
1240           (get_Block_dom_depth(dep_block) > depth)) {
1241         b = dep_block;
1242         depth = get_Block_dom_depth(dep_block);
1243       }
1244       /* Avoid that the node is placed in the Start block */
1245       if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
1246         b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1247         assert(b != get_irg_start_block(current_ir_graph));
1248         depth = 2;
1249       }
1250     }
1251     set_nodes_Block(n, b);
1252   }
1253
1254   /* Add predecessors of non floating nodes on worklist. */
1255   start = (get_irn_op(n) == op_Block) ? 0 : -1;
1256   irn_arity = get_irn_arity(n);
1257   for (i = start; i < irn_arity; i++) {
1258     ir_node *pred = get_irn_n(n, i);
1259     if (irn_not_visited(pred)) {
1260       pdeq_putr (worklist, pred);
1261     }
1262   }
1263 }
1264
1265 /* Floating nodes form subgraphs that begin at nodes as Const, Load,
1266    Start, Call and end at pinned nodes as Store, Call.  Place_early
1267    places all floating nodes reachable from its argument through floating
1268    nodes and adds all beginnings at pinned nodes to the worklist. */
1269 static INLINE void place_early (void) {
1270   assert(worklist);
1271   inc_irg_visited(current_ir_graph);
1272
1273   /* this inits the worklist */
1274   place_floats_early (get_irg_end(current_ir_graph));
1275
1276   /* Work the content of the worklist. */
1277   while (!pdeq_empty (worklist)) {
1278     ir_node *n = pdeq_getl (worklist);
1279     if (irn_not_visited(n)) place_floats_early (n);
1280   }
1281
1282   set_irg_outs_inconsistent(current_ir_graph);
1283   current_ir_graph->pinned = pinned;
1284 }
1285
1286
1287 /* deepest common dominance ancestor of DCA and CONSUMER of PRODUCER */
1288 static ir_node *
1289 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
1290 {
1291   ir_node *block = NULL;
1292
1293   /* Compute the latest block into which we can place a node so that it is
1294      before consumer. */
1295   if (get_irn_op(consumer) == op_Phi) {
1296     /* our comsumer is a Phi-node, the effective use is in all those
1297        blocks through which the Phi-node reaches producer */
1298     int i, irn_arity;
1299     ir_node *phi_block = get_nodes_Block(consumer);
1300     irn_arity = get_irn_arity(consumer);
1301     for (i = 0;  i < irn_arity; i++) {
1302       if (get_irn_n(consumer, i) == producer) {
1303         block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
1304       }
1305     }
1306   } else {
1307     assert(is_no_Block(consumer));
1308     block = get_nodes_Block(consumer);
1309   }
1310
1311   /* Compute the deepest common ancestor of block and dca. */
1312   assert(block);
1313   if (!dca) return block;
1314   while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1315     block = get_Block_idom(block);
1316   while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
1317     dca = get_Block_idom(dca);
1318   while (block != dca)
1319     { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1320
1321   return dca;
1322 }
1323
1324 static INLINE int get_irn_loop_depth(ir_node *n) {
1325   return get_loop_depth(get_irn_loop(n));
1326 }
1327
1328 /* Move n to a block with less loop depth than it's current block. The
1329    new block must be dominated by early. */
1330 static void
1331 move_out_of_loops (ir_node *n, ir_node *early)
1332 {
1333   ir_node *best, *dca;
1334   assert(n && early);
1335
1336
1337   /* Find the region deepest in the dominator tree dominating
1338      dca with the least loop nesting depth, but still dominated
1339      by our early placement. */
1340   dca = get_nodes_Block(n);
1341   best = dca;
1342   while (dca != early) {
1343     dca = get_Block_idom(dca);
1344     if (!dca) break; /* should we put assert(dca)? */
1345     if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1346       best = dca;
1347     }
1348   }
1349   if (best != get_nodes_Block(n)) {
1350     /* debug output
1351     printf("Moving out of loop: "); DDMN(n);
1352     printf(" Outermost block: "); DDMN(early);
1353     printf(" Best block: "); DDMN(best);
1354     printf(" Innermost block: "); DDMN(get_nodes_Block(n));
1355     */
1356     set_nodes_Block(n, best);
1357   }
1358 }
1359
1360 /* Find the latest legal block for N and place N into the
1361    `optimal' Block between the latest and earliest legal block.
1362    The `optimal' block is the dominance-deepest block of those
1363    with the least loop-nesting-depth.  This places N out of as many
1364    loops as possible and then makes it as controldependant as
1365    possible. */
1366 static void
1367 place_floats_late (ir_node *n)
1368 {
1369   int i;
1370   ir_node *early;
1371
1372   assert (irn_not_visited(n)); /* no multiple placement */
1373
1374   /* no need to place block nodes, control nodes are already placed. */
1375   if ((get_irn_op(n) != op_Block) &&
1376       (!is_cfop(n)) &&
1377       (get_irn_mode(n) != mode_X)) {
1378     /* Remember the early palacement of this block to move it
1379        out of loop no further than the early placement. */
1380     early = get_nodes_Block(n);
1381     /* Assure that our users are all placed, except the Phi-nodes.
1382        --- Each dataflow cycle contains at least one Phi-node.  We
1383        have to break the `user has to be placed before the
1384        producer' dependance cycle and the Phi-nodes are the
1385        place to do so, because we need to base our placement on the
1386        final region of our users, which is OK with Phi-nodes, as they
1387        are pinned, and they never have to be placed after a
1388        producer of one of their inputs in the same block anyway. */
1389     for (i = 0; i < get_irn_n_outs(n); i++) {
1390       ir_node *succ = get_irn_out(n, i);
1391       if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
1392         place_floats_late (succ);
1393     }
1394
1395     /* We have to determine the final block of this node... except for
1396        constants. */
1397     if ((get_op_pinned(get_irn_op(n)) == floats) &&
1398         (get_irn_op(n) != op_Const) &&
1399         (get_irn_op(n) != op_SymConst)) {
1400       ir_node *dca = NULL;      /* deepest common ancestor in the
1401                                    dominator tree of all nodes'
1402                                    blocks depending on us; our final
1403                                    placement has to dominate DCA. */
1404       for (i = 0; i < get_irn_n_outs(n); i++) {
1405         dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
1406       }
1407       set_nodes_Block(n, dca);
1408
1409       move_out_of_loops (n, early);
1410     }
1411   }
1412
1413   mark_irn_visited(n);
1414
1415   /* Add predecessors of all non-floating nodes on list. (Those of floating
1416      nodes are placeded already and therefore are marked.)  */
1417   for (i = 0; i < get_irn_n_outs(n); i++) {
1418     if (irn_not_visited(get_irn_out(n, i))) {
1419       pdeq_putr (worklist, get_irn_out(n, i));
1420     }
1421   }
1422 }
1423
1424 static INLINE void place_late(void) {
1425   assert(worklist);
1426   inc_irg_visited(current_ir_graph);
1427
1428   /* This fills the worklist initially. */
1429   place_floats_late(get_irg_start_block(current_ir_graph));
1430   /* And now empty the worklist again... */
1431   while (!pdeq_empty (worklist)) {
1432     ir_node *n = pdeq_getl (worklist);
1433     if (irn_not_visited(n)) place_floats_late(n);
1434   }
1435 }
1436
1437 void place_code(ir_graph *irg) {
1438   ir_graph *rem = current_ir_graph;
1439   current_ir_graph = irg;
1440
1441   if (!(get_optimize() && get_opt_global_cse())) return;
1442
1443   /* Handle graph state */
1444   assert(get_irg_phase_state(irg) != phase_building);
1445   if (get_irg_dom_state(irg) != dom_consistent)
1446     compute_doms(irg);
1447
1448   construct_backedges(irg);
1449
1450   /* Place all floating nodes as early as possible. This guarantees
1451      a legal code placement. */
1452   worklist = new_pdeq ();
1453   place_early();
1454
1455   /* place_early invalidates the outs, place_late needs them. */
1456   compute_outs(irg);
1457   /* Now move the nodes down in the dominator tree. This reduces the
1458      unnecessary executions of the node. */
1459   place_late();
1460
1461   set_irg_outs_inconsistent(current_ir_graph);
1462   del_pdeq (worklist);
1463   current_ir_graph = rem;
1464 }
1465
1466
1467
1468 /********************************************************************/
1469 /* Control flow optimization.                                       */
1470 /* Removes Bad control flow predecessors and empty blocks.  A block */
1471 /* is empty if it contains only a Jmp node.                         */
1472 /* Blocks can only be removed if they are not needed for the        */
1473 /* semantics of Phi nodes.                                          */
1474 /********************************************************************/
1475
1476 /* Removes Tuples from Block control flow predecessors.
1477    Optimizes blocks with equivalent_node().
1478    Replaces n by Bad if n is unreachable control flow. */
1479 static void merge_blocks(ir_node *n, void *env) {
1480   int i;
1481   set_irn_link(n, NULL);
1482
1483   if (get_irn_op(n) == op_Block) {
1484     /* Remove Tuples */
1485     for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1486       /* GL @@@ : is this possible? if (get_opt_normalize()) -- added, all tests go throug.
1487          A different order of optimizations might cause problems. */
1488       if (get_opt_normalize())
1489         set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1490   } else if (get_optimize() && (get_irn_mode(n) == mode_X)) {
1491     /* We will soon visit a block.  Optimize it before visiting! */
1492     ir_node *b = get_nodes_Block(n);
1493     ir_node *new = equivalent_node(b);
1494     while (irn_not_visited(b) && (!is_Bad(new)) && (new != b)) {
1495       /* We would have to run gigo if new is bad, so we
1496          promote it directly below. */
1497       assert(((b == new) ||
1498               get_opt_control_flow_straightening() ||
1499               get_opt_control_flow_weak_simplification()) &&
1500              ("strange flag setting"));
1501       exchange (b, new);
1502       b = new;
1503       new = equivalent_node(b);
1504     }
1505     /* GL @@@ get_opt_normalize hinzugefuegt, 5.5.2003 */
1506     if (is_Bad(new) && get_opt_normalize()) exchange (n, new_Bad());
1507   }
1508 }
1509
1510 /* Collects all Phi nodes in link list of Block.
1511    Marks all blocks "block_visited" if they contain a node other
1512    than Jmp. */
1513 static void collect_nodes(ir_node *n, void *env) {
1514   if (is_no_Block(n)) {
1515     ir_node *b = get_nodes_Block(n);
1516
1517     if ((get_irn_op(n) == op_Phi)) {
1518       /* Collect Phi nodes to compact ins along with block's ins. */
1519       set_irn_link(n, get_irn_link(b));
1520       set_irn_link(b, n);
1521     } else if (get_irn_op(n) != op_Jmp) {  /* Check for non empty block. */
1522       mark_Block_block_visited(b);
1523     }
1524   }
1525 }
1526
1527 /* Returns true if pred is pred of block */
1528 static int is_pred_of(ir_node *pred, ir_node *b) {
1529   int i;
1530   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1531     ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1532     if (b_pred == pred) return 1;
1533   }
1534   return 0;
1535 }
1536
1537 static int test_whether_dispensable(ir_node *b, int pos) {
1538   int i, j, n_preds = 1;
1539   int dispensable = 1;
1540   ir_node *cfop = get_Block_cfgpred(b, pos);
1541   ir_node *pred = get_nodes_Block(cfop);
1542
1543   if (get_Block_block_visited(pred) + 1
1544       < get_irg_block_visited(current_ir_graph)) {
1545     if (!get_optimize() || !get_opt_control_flow_strong_simplification()) {
1546       /* Mark block so that is will not be removed. */
1547       set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1548       return 1;
1549     }
1550     /* Seems to be empty. */
1551     if (!get_irn_link(b)) {
1552       /* There are no Phi nodes ==> dispensable. */
1553       n_preds = get_Block_n_cfgpreds(pred);
1554     } else {
1555       /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1556          Work preds < pos as if they were already removed. */
1557       for (i = 0; i < pos; i++) {
1558         ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1559         if (get_Block_block_visited(b_pred) + 1
1560             < get_irg_block_visited(current_ir_graph)) {
1561           for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1562             ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1563             if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1564           }
1565         } else {
1566           if (is_pred_of(b_pred, pred)) dispensable = 0;
1567         }
1568       }
1569       for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1570         ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1571         if (is_pred_of(b_pred, pred)) dispensable = 0;
1572       }
1573       if (!dispensable) {
1574         set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1575         n_preds = 1;
1576       } else {
1577         n_preds = get_Block_n_cfgpreds(pred);
1578       }
1579     }
1580   }
1581
1582   return n_preds;
1583 }
1584
1585 static void optimize_blocks(ir_node *b, void *env) {
1586   int i, j, k, max_preds, n_preds;
1587   ir_node *pred, *phi;
1588   ir_node **in;
1589
1590   /* Count the number of predecessor if this block is merged with pred blocks
1591      that are empty. */
1592   max_preds = 0;
1593   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1594     max_preds += test_whether_dispensable(b, i);
1595   }
1596   in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1597
1598 /**
1599   printf(" working on "); DDMN(b);
1600   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1601     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1602     if (is_Bad(get_Block_cfgpred(b, i))) {
1603       printf("  removing Bad %i\n ", i);
1604     } else if (get_Block_block_visited(pred) +1
1605                < get_irg_block_visited(current_ir_graph)) {
1606       printf("  removing pred %i ", i); DDMN(pred);
1607     } else { printf("  Nothing to do for "); DDMN(pred); }
1608   }
1609   * end Debug output **/
1610
1611   /** Fix the Phi nodes **/
1612   phi = get_irn_link(b);
1613   while (phi) {
1614     assert(get_irn_op(phi) == op_Phi);
1615     /* Find the new predecessors for the Phi */
1616     n_preds = 0;
1617     for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1618       pred = get_nodes_Block(get_Block_cfgpred(b, i));
1619       if (is_Bad(get_Block_cfgpred(b, i))) {
1620         /* Do nothing */
1621       } else if (get_Block_block_visited(pred) +1
1622                  < get_irg_block_visited(current_ir_graph)) {
1623         /* It's an empty block and not yet visited. */
1624         ir_node *phi_pred = get_Phi_pred(phi, i);
1625         for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1626           if (get_nodes_Block(phi_pred) == pred) {
1627             assert(get_irn_op(phi_pred) == op_Phi);  /* Block is empty!! */
1628             in[n_preds] = get_Phi_pred(phi_pred, j);
1629           } else {
1630             in[n_preds] = phi_pred;
1631           }
1632           n_preds++;
1633         }
1634         /* The Phi_pred node is replaced now if it is a Phi.
1635            In Schleifen kann offenbar der entfernte Phi Knoten legal verwendet werden.
1636            Daher muss der Phiknoten durch den neuen ersetzt werden.
1637            Weiter muss der alte Phiknoten entfernt werden (durch ersetzen oder
1638            durch einen Bad) damit er aus den keep_alive verschwinden kann.
1639            Man sollte also, falls keine Schleife vorliegt, exchange mit new_Bad
1640            aufrufen.  */
1641         if (get_nodes_Block(phi_pred) == pred) {
1642           /* remove the Phi as it might be kept alive. Further there
1643              might be other users. */
1644           exchange(phi_pred, phi);  /* geht, ist aber doch semantisch falsch! Warum?? */
1645         }
1646       } else {
1647         in[n_preds] = get_Phi_pred(phi, i);
1648         n_preds ++;
1649       }
1650     }
1651     /* Fix the node */
1652     set_irn_in(phi, n_preds, in);
1653
1654     phi = get_irn_link(phi);
1655   }
1656
1657 /**
1658       This happens only if merge between loop backedge and single loop entry. **/
1659   for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1660     pred = get_nodes_Block(get_Block_cfgpred(b, k));
1661     if (get_Block_block_visited(pred) +1
1662         < get_irg_block_visited(current_ir_graph)) {
1663       phi = get_irn_link(pred);
1664       while (phi) {
1665         if (get_irn_op(phi) == op_Phi) {
1666           set_nodes_Block(phi, b);
1667
1668           n_preds = 0;
1669           for (i = 0; i < k; i++) {
1670             pred = get_nodes_Block(get_Block_cfgpred(b, i));
1671             if (is_Bad(get_Block_cfgpred(b, i))) {
1672               /* Do nothing */
1673             } else if (get_Block_block_visited(pred) +1
1674                        < get_irg_block_visited(current_ir_graph)) {
1675               /* It's an empty block and not yet visited. */
1676               for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1677                 /* @@@ Hier brauche ich Schleifeninformation!!! Kontrollflusskante
1678                    muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1679                    Anweisungen.) Trotzdem tuts bisher!! */
1680                 in[n_preds] = phi;
1681                 n_preds++;
1682               }
1683             } else {
1684               in[n_preds] = phi;
1685               n_preds++;
1686             }
1687           }
1688           for (i = 0; i < get_Phi_n_preds(phi); i++) {
1689             in[n_preds] = get_Phi_pred(phi, i);
1690             n_preds++;
1691           }
1692           for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1693             pred = get_nodes_Block(get_Block_cfgpred(b, i));
1694             if (is_Bad(get_Block_cfgpred(b, i))) {
1695               /* Do nothing */
1696             } else if (get_Block_block_visited(pred) +1
1697                        < get_irg_block_visited(current_ir_graph)) {
1698               /* It's an empty block and not yet visited. */
1699               for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1700                 in[n_preds] = phi;
1701                 n_preds++;
1702               }
1703             } else {
1704               in[n_preds] = phi;
1705               n_preds++;
1706             }
1707           }
1708           set_irn_in(phi, n_preds, in);
1709         }
1710         phi = get_irn_link(phi);
1711       }
1712     }
1713   }
1714
1715   /** Fix the block **/
1716   n_preds = 0;
1717   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1718     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1719     if (is_Bad(get_Block_cfgpred(b, i))) {
1720       /* Do nothing */
1721     } else if (get_Block_block_visited(pred) +1
1722                < get_irg_block_visited(current_ir_graph)) {
1723       /* It's an empty block and not yet visited. */
1724       assert(get_Block_n_cfgpreds(b) > 1);
1725                         /* Else it should be optimized by equivalent_node. */
1726       for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1727         in[n_preds] = get_Block_cfgpred(pred, j);
1728         n_preds++;
1729       }
1730       /* Remove block as it might be kept alive. */
1731       exchange(pred, b/*new_Bad()*/);
1732     } else {
1733       in[n_preds] = get_Block_cfgpred(b, i);
1734       n_preds ++;
1735     }
1736   }
1737   set_irn_in(b, n_preds, in);
1738   free(in);
1739 }
1740
1741 void optimize_cf(ir_graph *irg) {
1742   int i;
1743   ir_node **in;
1744   ir_node *end = get_irg_end(irg);
1745   ir_graph *rem = current_ir_graph;
1746   current_ir_graph = irg;
1747
1748   /* Handle graph state */
1749   assert(get_irg_phase_state(irg) != phase_building);
1750   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1751     set_irg_outs_inconsistent(current_ir_graph);
1752   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1753     set_irg_dom_inconsistent(current_ir_graph);
1754
1755   /* Use block visited flag to mark non-empty blocks. */
1756   inc_irg_block_visited(irg);
1757   irg_walk(end, merge_blocks, collect_nodes, NULL);
1758
1759   /* Optimize the standard code. */
1760   irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1761
1762   /* Walk all keep alives, optimize them if block, add to new in-array
1763      for end if useful. */
1764   in = NEW_ARR_F (ir_node *, 1);
1765   in[0] = get_nodes_Block(end);
1766   inc_irg_visited(current_ir_graph);
1767   for(i = 0; i < get_End_n_keepalives(end); i++) {
1768     ir_node *ka = get_End_keepalive(end, i);
1769     if (irn_not_visited(ka)) {
1770       if ((get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1771         set_irg_block_visited(current_ir_graph,  /* Don't walk all the way to Start. */
1772                               get_irg_block_visited(current_ir_graph)-1);
1773         irg_block_walk(ka, optimize_blocks, NULL, NULL);
1774         mark_irn_visited(ka);
1775         ARR_APP1 (ir_node *, in, ka);
1776       } else if (get_irn_op(ka) == op_Phi) {
1777         mark_irn_visited(ka);
1778         ARR_APP1 (ir_node *, in, ka);
1779       }
1780     }
1781   }
1782   /* DEL_ARR_F(end->in);   GL @@@ tut nicht ! */
1783   end->in = in;
1784
1785   current_ir_graph = rem;
1786 }
1787
1788
1789 /**
1790  * Called by walker of remove_critical_cf_edges.
1791  *
1792  * Place an empty block to an edge between a blocks of multiple
1793  * predecessors and a block of multiple sucessors.
1794  *
1795  * @param n IR node
1796  * @param env Envirnment of walker. This field is unused and has
1797  *            the value NULL.
1798  */
1799 static void walk_critical_cf_edges(ir_node *n, void *env) {
1800   int arity, i;
1801   ir_node *pre, *block, **in, *jmp;
1802
1803   /* Block has multiple predecessors */
1804   if ((op_Block == get_irn_op(n)) &&
1805       (get_irn_arity(n) > 1)) {
1806     arity = get_irn_arity(n);
1807
1808     if (n == get_irg_end_block(current_ir_graph))
1809       return;  // No use to add a block here.
1810
1811     for (i=0; i<arity; i++) {
1812       pre = get_irn_n(n, i);
1813       /* Predecessor has multiple sucessors. Insert new flow edge */
1814       if ((NULL != pre) &&
1815           (op_Proj == get_irn_op(pre)) &&
1816           op_Raise != get_irn_op(skip_Proj(pre))) {
1817
1818         /* set predecessor array for new block */
1819         in = NEW_ARR_D (ir_node *, current_ir_graph->obst, 1);
1820         /* set predecessor of new block */
1821         in[0] = pre;
1822         block = new_Block(1, in);
1823         /* insert new jmp node to new block */
1824         switch_block(block);
1825         jmp = new_Jmp();
1826         switch_block(n);
1827         /* set sucessor of new block */
1828         set_irn_n(n, i, jmp);
1829
1830       } /* predecessor has multiple sucessors */
1831     } /* for all predecessors */
1832   } /* n is a block */
1833 }
1834
1835 void remove_critical_cf_edges(ir_graph *irg) {
1836   if (get_opt_critical_edges())
1837     irg_walk_graph(irg, NULL, walk_critical_cf_edges, NULL);
1838 }