Implemented cf optimizations.
[libfirm] / ir / ir / irgopt.c
1 /* Coyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Author: Christian Schaefer
5 **
6 ** Optimizations for a whole ir graph, i.e., a procedure.
7 */
8
9 /* $Id$ */
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 # include <assert.h>
16
17 # include "irprog.h"
18 # include "irgopt.h"
19 # include "irnode_t.h"
20 # include "irgraph_t.h"
21 # include "iropt_t.h"
22 # include "irgwalk.h"
23 # include "ircons.h"
24 # include "misc.h"
25 # include "irgmod.h"
26 # include "array.h"
27 # include "pset.h"
28 # include "pdeq.h"       /* Fuer code placement */
29 # include "irouts.h"
30
31 /* Defined in iropt.c */
32 pset *new_identities (void);
33 void  del_identities (pset *value_table);
34 void  add_identities   (pset *value_table, ir_node *node);
35
36 /********************************************************************/
37 /* apply optimizations of iropt to all nodes.                       */
38 /********************************************************************/
39
40 void init_link (ir_node *n, void *env) {
41   set_irn_link(n, NULL);
42 }
43
44 void
45 optimize_in_place_wrapper (ir_node *n, void *env) {
46   int start, i;
47   ir_node *optimized;
48
49   if (get_irn_op(n) == op_Block)
50     start = 0;
51   else
52     start = -1;
53
54   /* optimize all sons after recursion, i.e., the sons' sons are
55      optimized already. */
56   for (i = start; i < get_irn_arity(n); i++) {
57     optimized = optimize_in_place_2(get_irn_n(n, i));
58     set_irn_n(n, i, optimized);
59     assert(get_irn_op(optimized) != op_Id);
60   }
61 }
62
63 void
64 local_optimize_graph (ir_graph *irg) {
65   ir_graph *rem = current_ir_graph;
66   current_ir_graph = irg;
67
68   /* Handle graph state */
69   assert(get_irg_phase_state(irg) != phase_building);
70   if (get_opt_global_cse())
71     set_irg_pinned(current_ir_graph, floats);
72   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
73     set_irg_outs_inconsistent(current_ir_graph);
74   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
75     set_irg_dom_inconsistent(current_ir_graph);
76
77   /* Clean the value_table in irg for the cse. */
78   del_identities(irg->value_table);
79   irg->value_table = new_identities();
80
81   /* walk over the graph */
82   irg_walk(irg->end, init_link, optimize_in_place_wrapper, NULL);
83
84   current_ir_graph = rem;
85 }
86
87 /********************************************************************/
88 /* Routines for dead node elimination / copying garbage collection  */
89 /* of the obstack.                                                  */
90 /********************************************************************/
91
92 /* Remeber the new node in the old node by using a field all nodes have. */
93 inline void
94 set_new_node (ir_node *old, ir_node *new)
95 {
96   old->link = new;
97 }
98
99 /* Get this new node, before the old node is forgotton.*/
100 inline ir_node *
101 get_new_node (ir_node * n)
102 {
103   return n->link;
104 }
105
106 /* We use the block_visited flag to mark that we have computed the
107    number of useful predecessors for this block.
108    Further we encode the new arity in this flag in the old blocks.
109    Remembering the arity is useful, as it saves a lot of pointer
110    accesses.  This function is called for all Phi and Block nodes
111    in a Block. */
112 inline int
113 compute_new_arity(ir_node *b) {
114   int i, res;
115   int irg_v, block_v;
116
117   irg_v = get_irg_block_visited(current_ir_graph);
118   block_v = get_Block_block_visited(b);
119   if (block_v >= irg_v) {
120     /* we computed the number of preds for this block and saved it in the
121        block_v flag */
122     return block_v - irg_v;
123   } else {
124     /* compute the number of good predecessors */
125     res = get_irn_arity(b);
126     for (i = 0; i < get_irn_arity(b); i++)
127       if (get_irn_opcode(get_irn_n(b, i)) == iro_Bad) res--;
128     /* save it in the flag. */
129     set_Block_block_visited(b, irg_v + res);
130     return res;
131   }
132 }
133
134 /* Copies the node to the new obstack. The Ins of the new node point to
135    the predecessors on the old obstack.  For block/phi nodes not all
136    predecessors might be copied.  n->link points to the new node.
137    For Phi and Block nodes the function allocates in-arrays with an arity
138    only for useful predecessors.  The arity is determined by counting
139    the non-bad predecessors of the block. */
140 void
141 copy_node (ir_node *n, void *env) {
142   ir_node *nn, *block;
143   int new_arity;
144
145   if (get_irn_opcode(n) == iro_Block) {
146     block = NULL;
147     new_arity = compute_new_arity(n);
148   } else {
149     block = get_nodes_Block(n);
150     if (get_irn_opcode(n) == iro_Phi) {
151       new_arity = compute_new_arity(block);
152     } else {
153       new_arity = get_irn_arity(n);
154     }
155   }
156   nn = new_ir_node(current_ir_graph,
157                    block,
158                    get_irn_op(n),
159                    get_irn_mode(n),
160                    new_arity,
161                    get_irn_in(n));
162   /* Copy the attributes.  These might point to additional data.  If this
163      was allocated on the old obstack the pointers now are dangling.  This
164      frees e.g. the memory of the graph_arr allocated in new_immBlock. */
165   copy_attrs(n, nn);
166   set_new_node(n, nn);
167
168   /*  printf("\n old node: "); DDMSG2(n);
169       printf(" new node: "); DDMSG2(nn); */
170
171 }
172
173 /* Copies new predecessors of old node to new node remembered in link.
174    Spare the Bad predecessors of Phi and Block nodes. */
175 void
176 copy_preds (ir_node *n, void *env) {
177   ir_node *nn, *block, *on;
178   int i, j;
179
180   nn = get_new_node(n);
181
182   /* printf("\n old node: "); DDMSG2(n);
183      printf(" new node: "); DDMSG2(nn);
184      printf(" arities: old: %d, new: %d\n", get_irn_arity(n), get_irn_arity(nn)); */
185
186   if (get_irn_opcode(n) == iro_Block) {
187     /* Don't copy Bad nodes. */
188     j = 0;
189     for (i = 0; i < get_irn_arity(n); i++)
190       if (get_irn_opcode(get_irn_n(n, i)) != iro_Bad) {
191         set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
192         j++;
193       }
194     /* repair the block visited flag from above misuse. Repair it in both
195        graphs so that the old one can still be used. */
196     set_Block_block_visited(nn, 0);
197     set_Block_block_visited(n, 0);
198     /* Local optimization could not merge two subsequent blocks if
199        in array contained Bads.  Now it's possible.
200        We don't call optimize_in_place as it requires
201        that the fields in ir_graph are set properly. */
202     if (get_Block_n_cfgpreds(nn) == 1
203         && get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp)
204       exchange(nn, get_nodes_Block(get_Block_cfgpred(nn, 0)));
205   } else if (get_irn_opcode(n) == iro_Phi) {
206     /* Don't copy node if corresponding predecessor in block is Bad.
207        The Block itself should not be Bad. */
208     block = get_nodes_Block(n);
209     set_irn_n (nn, -1, get_new_node(block));
210     j = 0;
211     for (i = 0; i < get_irn_arity(n); i++)
212       if (get_irn_opcode(get_irn_n(block, i)) != iro_Bad) {
213         set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
214         j++;
215       }
216     /* If the pre walker reached this Phi after the post walker visited the
217        block block_visited is > 0. */
218     set_Block_block_visited(get_nodes_Block(n), 0);
219     /* Compacting the Phi's ins might generate Phis with only one
220        predecessor. */
221     if (get_irn_arity(n) == 1)
222       exchange(n, get_irn_n(n, 0));
223   } else {
224     for (i = -1; i < get_irn_arity(n); i++)
225       set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
226   }
227   /* Now the new node is complete.  We can add it to the hash table for cse.
228      @@@ inlinening aborts if we identify End. Why? */
229   if(get_irn_op(nn) != op_End)
230     add_identities (current_ir_graph->value_table, nn);
231 }
232
233 /* Copies the graph recursively, compacts the keepalive of the end node. */
234 void
235 copy_graph () {
236   ir_node *oe, *ne; /* old end, new end */
237   ir_node *ka;      /* keep alive */
238   int i;
239
240   oe = get_irg_end(current_ir_graph);
241   /* copy the end node by hand, allocate dynamic in array! */
242   ne = new_ir_node(current_ir_graph,
243                    NULL,
244                    op_End,
245                    mode_X,
246                    -1,
247                    NULL);
248   /* Copy the attributes.  Well, there might be some in the future... */
249   copy_attrs(oe, ne);
250   set_new_node(oe, ne);
251
252   /* copy the live nodes */
253   irg_walk(get_nodes_Block(oe), copy_node, copy_preds, NULL);
254   /* copy_preds for the end node ... */
255   set_nodes_Block(ne, get_new_node(get_nodes_Block(oe)));
256
257   /** ... and now the keep alives. **/
258   /* First pick the not marked block nodes and walk them.  We must pick these
259      first as else we will oversee blocks reachable from Phis. */
260   for (i = 0; i < get_irn_arity(oe); i++) {
261     ka = get_irn_n(oe, i);
262     if ((get_irn_op(ka) == op_Block) &&
263         (get_irn_visited(ka) < get_irg_visited(current_ir_graph))) {
264       /* We must keep the block alive and copy everything reachable */
265       set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
266       irg_walk(ka, copy_node, copy_preds, NULL);
267       add_End_keepalive(ne, get_new_node(ka));
268     }
269   }
270
271   /* Now pick the Phis.  Here we will keep all! */
272   for (i = 0; i < get_irn_arity(oe); i++) {
273     ka = get_irn_n(oe, i);
274     if ((get_irn_op(ka) == op_Phi)) {
275       if (get_irn_visited(ka) < get_irg_visited(current_ir_graph)) {
276         /* We didn't copy the Phi yet.  */
277         set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
278         irg_walk(ka, copy_node, copy_preds, NULL);
279       }
280       add_End_keepalive(ne, get_new_node(ka));
281     }
282   }
283 }
284
285 /* Copies the graph reachable from current_ir_graph->end to the obstack
286    in current_ir_graph and fixes the environment.
287    Then fixes the fields in current_ir_graph containing nodes of the
288    graph.  */
289 void
290 copy_graph_env () {
291   /* Not all nodes remembered in current_ir_graph might be reachable
292      from the end node.  Assure their link is set to NULL, so that
293      we can test whether new nodes have been computed. */
294   set_irn_link(get_irg_frame  (current_ir_graph), NULL);
295   set_irn_link(get_irg_globals(current_ir_graph), NULL);
296   set_irn_link(get_irg_args   (current_ir_graph), NULL);
297
298   /* we use the block walk flag for removing Bads from Blocks ins. */
299   inc_irg_block_visited(current_ir_graph);
300
301   /* copy the graph */
302   copy_graph();
303
304   /* fix the fields in current_ir_graph */
305   free_End(get_irg_end(current_ir_graph));
306   set_irg_end        (current_ir_graph, get_new_node(get_irg_end(current_ir_graph)));
307   set_irg_end_block  (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
308   if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
309     copy_node (get_irg_frame(current_ir_graph), NULL);
310     copy_preds(get_irg_frame(current_ir_graph), NULL);
311   }
312   if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
313     copy_node (get_irg_globals(current_ir_graph), NULL);
314     copy_preds(get_irg_globals(current_ir_graph), NULL);
315   }
316   if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
317     copy_node (get_irg_args(current_ir_graph), NULL);
318     copy_preds(get_irg_args(current_ir_graph), NULL);
319   }
320   set_irg_start  (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
321
322   set_irg_start_block(current_ir_graph,
323                       get_new_node(get_irg_start_block(current_ir_graph)));
324   set_irg_frame  (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
325   set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
326   set_irg_args   (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
327   if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
328     copy_node(get_irg_bad(current_ir_graph), NULL);
329     copy_preds(get_irg_bad(current_ir_graph), NULL);
330   }
331   set_irg_bad(current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
332 }
333
334 /* Copies all reachable nodes to a new obstack.  Removes bad inputs
335    from block nodes and the corresponding inputs from Phi nodes.
336    Merges single exit blocks with single entry blocks and removes
337    1-input Phis.
338    Adds all new nodes to a new hash table for cse.  Does not
339    perform cse, so the hash table might contain common subexpressions. */
340 /* Amroq call this emigrate() */
341 void
342 dead_node_elimination(ir_graph *irg) {
343   ir_graph *rem;
344   struct obstack *graveyard_obst = NULL;
345   struct obstack *rebirth_obst   = NULL;
346
347   /* Remember external state of current_ir_graph. */
348   rem = current_ir_graph;
349   current_ir_graph = irg;
350
351   /* Handle graph state */
352   assert(get_irg_phase_state(current_ir_graph) != phase_building);
353   free_outs(current_ir_graph);
354
355   if (get_optimize() && get_opt_dead_node_elimination()) {
356
357     /* A quiet place, where the old obstack can rest in peace,
358        until it will be cremated. */
359     graveyard_obst = irg->obst;
360
361     /* A new obstack, where the reachable nodes will be copied to. */
362     rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
363     current_ir_graph->obst = rebirth_obst;
364     obstack_init (current_ir_graph->obst);
365
366     /* We also need a new hash table for cse */
367     del_identities (irg->value_table);
368     irg->value_table = new_identities ();
369
370     /* Copy the graph from the old to the new obstack */
371     copy_graph_env();
372
373     /* Free memory from old unoptimized obstack */
374     obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
375     xfree (graveyard_obst);           /* ... then free it.           */
376   }
377
378   current_ir_graph = rem;
379 }
380
381 /**********************************************************************/
382 /*  Funcionality for inlining                                         */
383 /**********************************************************************/
384
385 /* Copy node for inlineing.  Copies the node by calling copy_node and
386    then updates the entity if it's a local one.  env must be a pointer
387    to the frame type of the procedure. The new entities must be in
388    the link field of the entities. */
389 void
390 copy_node_inline (ir_node *n, void *env) {
391   ir_node *new;
392   type *frame_tp = (type *)env;
393
394   copy_node(n, NULL);
395   if (get_irn_op(n) == op_Sel) {
396     new = get_new_node (n);
397     assert(get_irn_op(new) == op_Sel);
398     if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
399       set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
400     }
401   }
402 }
403
404 void inline_method(ir_node *call, ir_graph *called_graph) {
405   ir_node *pre_call;
406   ir_node *post_call, *post_bl;
407   ir_node *in[5];
408   ir_node *end, *end_bl;
409   ir_node **res_pred;
410   ir_node **cf_pred;
411   ir_node *ret, *phi;
412   ir_node *cf_op, *bl;
413   int arity, n_ret, n_exc, n_res, i, j;
414   type *called_frame, *caller_frame;
415
416   if (!get_opt_inline()) return;
417
418   /* Handle graph state */
419   assert(get_irg_phase_state(current_ir_graph) != phase_building);
420   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
421     set_irg_outs_inconsistent(current_ir_graph);
422
423   /** Check preconditions **/
424   assert(get_irn_op(call) == op_Call);
425   assert(get_Call_type(call) == get_entity_type(get_irg_ent(called_graph)));
426   assert(get_type_tpop(get_Call_type(call)) == type_method);
427   if (called_graph == current_ir_graph) return;
428
429   /** Part the Call node into two nodes.  Pre_call collects the parameters of
430           the procedure and later replaces the Start node of the called graph.
431           Post_call is the old Call node and collects the results of the called
432           graph. Both will end up being a tuple.  **/
433   post_bl = get_nodes_Block(call);
434   set_irg_current_block(current_ir_graph, post_bl);
435   /* XxMxPxP of Start + parameter of Call */
436   in[0] = new_Jmp();
437   in[1] = get_Call_mem(call);
438   in[2] = get_irg_frame(current_ir_graph);
439   in[3] = get_irg_globals(current_ir_graph);
440   in[4] = new_Tuple (get_Call_n_params(call),
441                                          get_Call_param_arr(call));
442   pre_call = new_Tuple(5, in);
443   post_call = call;
444
445   /** Part the block of the Call node into two blocks.
446       The new block gets the ins of the old block, pre_call and all its
447       predecessors and all Phi nodes. **/
448   part_block(pre_call);
449
450   /** Prepare state for dead node elimination **/
451   /* Visited flags in calling irg must be >= flag in called irg.
452      Else walker and arity computation will not work. */
453   if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
454     set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);   /***/
455   if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
456     set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
457   /* Set pre_call as new Start node in link field of the start node of
458          calling graph and pre_calls block as new block for the start block
459          of calling graph.
460          Further mark these nodes so that they are not visited by the
461          copying. */
462   set_irn_link(get_irg_start(called_graph), pre_call);
463   set_irn_visited(get_irg_start(called_graph),
464                                   get_irg_visited(current_ir_graph));/***/
465   set_irn_link(get_irg_start_block(called_graph),
466                            get_nodes_Block(pre_call));
467   set_irn_visited(get_irg_start_block(called_graph),
468                                   get_irg_visited(current_ir_graph));  /***/
469
470   /* Initialize for compaction of in arrays */
471   inc_irg_block_visited(current_ir_graph);
472   /*
473           set_Block_block_visited(get_irg_start_block(called_graph),
474           get_irg_block_visited(current_ir_graph) +1 +1); /* count for self edge */
475
476   /*** Replicate local entities of the called_graph ***/
477   /* copy the entities. */
478   called_frame = get_irg_frame_type(called_graph);
479   for (i = 0; i < get_class_n_member(called_frame); i++) {
480     entity *new_ent, *old_ent;
481     old_ent = get_class_member(called_frame, i);
482     new_ent = copy_entity_own(old_ent, get_cur_frame_type());
483     set_entity_link(old_ent, new_ent);
484   }
485
486   /* visited is > than that of called graph.  With this trick visited will
487      remain unchanged so that an outer walker, e.g., searching the call nodes
488      to inline, calling this inline will not visit the inlined nodes. */
489   set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
490
491   /** Performing dead node elimination inlines the graph **/
492   /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
493      entities. */
494   /* @@@ endless loops are not copied!! */
495   irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
496                    get_irg_frame_type(called_graph));
497
498   /* Repair called_graph */
499   set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
500   set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
501   set_Block_block_visited(get_irg_start_block(called_graph), 0);
502
503   /*** Merge the end of the inlined procedure with the call site ***/
504   /* We will turn the old Call node into a Tuple with the following
505      predecessors:
506      -1:  Block of Tuple.
507      0: Phi of all Memories of Return statements.
508      1: Jmp from new Block that merges the control flow from all exception
509          predecessors of the old end block.
510      2: Tuple of all arguments.
511      3: Phi of Exception memories.
512   */
513
514   /** Precompute some values **/
515   end_bl = get_new_node(get_irg_end_block(called_graph));
516   end = get_new_node(get_irg_end(called_graph));
517   arity = get_irn_arity(end_bl);    /* arity = n_exc + n_ret  */
518   n_res = get_method_n_res(get_Call_type(call));
519
520   res_pred = (ir_node **) malloc (n_res * sizeof (ir_node *));
521   cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
522
523   set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
524
525   /** archive keepalives **/
526   for (i = 0; i < get_irn_arity(end); i++)
527     add_End_keepalive(get_irg_end(current_ir_graph), get_irn_n(end, i));
528   /* The new end node will die, but the in array is not on the obstack ... */
529   free_End(end);
530
531   /** Collect control flow from Return blocks to post_calls block. Replace
532       Return nodes by Jump nodes. **/
533   n_ret = 0;
534   for (i = 0; i < arity; i++) {
535     ir_node *ret;
536     ret = get_irn_n(end_bl, i);
537     if (get_irn_op(ret) == op_Return) {
538       cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_Block(ret));
539       n_ret++;
540     }
541   }
542   set_irn_in(post_bl, n_ret, cf_pred);
543
544   /** Collect results from Return nodes to post_call. Post_call is
545       turned into a tuple. **/
546   turn_into_tuple(post_call, 4);
547   /* First the Memory-Phi */
548   n_ret = 0;
549   for (i = 0; i < arity; i++) {
550     ret = get_irn_n(end_bl, i);
551     if (get_irn_op(ret) == op_Return) {
552       cf_pred[n_ret] = get_Return_mem(ret);
553       n_ret++;
554     }
555   }
556   phi = new_Phi(n_ret, cf_pred, mode_M);
557   set_Tuple_pred(call, 0, phi);
558   set_irn_link(phi, get_irn_link(post_bl));  /* Conserve Phi-list for further inlinings */
559   set_irn_link(post_bl, phi);
560   /* Now the real results */
561   if (n_res > 0) {
562     for (j = 0; j < n_res; j++) {
563       n_ret = 0;
564       for (i = 0; i < arity; i++) {
565                 ret = get_irn_n(end_bl, i);
566                 if (get_irn_op(ret) == op_Return) {
567                   cf_pred[n_ret] = get_Return_res(ret, j);
568                   n_ret++;
569                 }
570       }
571       phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
572       res_pred[j] = phi;
573       set_irn_link(phi, get_irn_link(post_bl));  /* Conserve Phi-list for further inlinings */
574       set_irn_link(post_bl, phi);
575     }
576     set_Tuple_pred(call, 2, new_Tuple(n_res, res_pred));
577   } else {
578     set_Tuple_pred(call, 2, new_Bad());
579   }
580   /* Finally the exception control flow.  We need to add a Phi node to
581      collect the memory containing the exception objects.  Further we need
582      to add another block to get a correct representation of this Phi.  To
583      this block we add a Jmp that resolves into the X output of the Call
584      when the Call is turned into a tuple. */
585   n_exc = 0;
586   for (i = 0; i < arity; i++) {
587     ir_node *ret;
588     ret = get_irn_n(end_bl, i);
589     if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
590       cf_pred[n_exc] = ret;
591       n_exc++;
592     }
593   }
594   if (n_exc > 0) {
595     new_Block(n_exc, cf_pred);      /* whatch it: current_block is changed! */
596     set_Tuple_pred(call, 1, new_Jmp());
597     /* The Phi for the memories with the exception objects */
598     n_exc = 0;
599     for (i = 0; i < arity; i++) {
600       ir_node *ret;
601       ret = skip_Proj(get_irn_n(end_bl, i));
602       if (get_irn_op(ret) == op_Call) {
603                 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 3);
604                 n_exc++;
605       } else if (is_fragile_op(ret)) {
606                 /* We rely that all cfops have the memory output at the same position. */
607                 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 0);
608                 n_exc++;
609       } else if (get_irn_op(ret) == op_Raise) {
610                 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 1);
611                 n_exc++;
612       }
613     }
614     set_Tuple_pred(call, 3, new_Phi(n_exc, cf_pred, mode_M));
615   } else {
616     set_Tuple_pred(call, 1, new_Bad());
617     set_Tuple_pred(call, 3, new_Bad());
618   }
619   free(res_pred);
620   free(cf_pred);
621
622   /*** Correct the control flow to the end node.
623        If the exception control flow from the Call directly branched to the
624        end block we now have the following control flow predecessor pattern:
625        ProjX -> Tuple -> Jmp.
626        We must remove the Jmp along with it's empty block and add Jmp's
627        predecessors as predecessors of this end block. ***/
628   /* find the problematic predecessor of the end block. */
629   end_bl = get_irg_end_block(current_ir_graph);
630   for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
631     cf_op = get_Block_cfgpred(end_bl, i);
632     if (get_irn_op(cf_op) == op_Proj) {
633       cf_op = get_Proj_pred(cf_op);
634       if (get_irn_op(cf_op) == op_Tuple) {
635                 cf_op = get_Tuple_pred(cf_op, 1);
636                 assert(get_irn_op(cf_op) == op_Jmp);
637                 break;
638       }
639     }
640   }
641   /* repair */
642   if (i < get_Block_n_cfgpreds(end_bl)) {
643     bl = get_nodes_Block(cf_op);
644     arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
645     cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
646     for (j = 0; j < i; j++)
647       cf_pred[j] = get_Block_cfgpred(end_bl, j);
648     for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
649       cf_pred[j] = get_Block_cfgpred(bl, j-i);
650     for (j = j; j < arity; j++)
651       cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
652     set_irn_in(end_bl, arity, cf_pred);
653     free(cf_pred);
654   }
655 }
656
657 /********************************************************************/
658 /* Apply inlineing to small methods.                                */
659 /********************************************************************/
660
661 static int pos;
662
663 /* It makes no sense to inline too many calls in one procedure. Anyways,
664    I didn't get a version with NEW_ARR_F to run. */
665 #define MAX_INLINE 1024
666
667 static void collect_calls(ir_node *call, void *env) {
668   ir_node **calls = (ir_node **)env;
669   ir_node *addr;
670   tarval *tv;
671   ir_graph *called_irg;
672
673   if (get_irn_op(call) != op_Call) return;
674
675   addr = get_Call_ptr(call);
676   if (get_irn_op(addr) == op_Const) {
677     /* Check whether the constant is the pointer to a compiled entity. */
678     tv = get_Const_tarval(addr);
679     if (tv->u.p.ent) {
680       called_irg = get_entity_irg(tv->u.p.ent);
681       if (called_irg && pos < MAX_INLINE) {
682         /* The Call node calls a locally defined method.  Remember to inline. */
683         calls[pos] = call;
684         pos++;
685       }
686     }
687   }
688 }
689
690
691 /* Inlines all small methods at call sites where the called address comes
692    from a Const node that references the entity representing the called
693    method.
694    The size argument is a rough measure for the code size of the method:
695    Methods where the obstack containing the firm graph is smaller than
696    size are inlined. */
697 void inline_small_irgs(ir_graph *irg, int size) {
698   int i;
699   ir_node *calls[MAX_INLINE];
700   ir_graph *rem = current_ir_graph;
701
702   if (!(get_optimize() && get_opt_inline())) return;
703
704   /*DDME(get_irg_ent(current_ir_graph));*/
705
706   current_ir_graph = irg;
707   /* Handle graph state */
708   assert(get_irg_phase_state(current_ir_graph) != phase_building);
709
710   /* Find Call nodes to inline.
711      (We can not inline during a walk of the graph, as inlineing the same
712      method several times changes the visited flag of the walked graph:
713      after the first inlineing visited of the callee equals visited of
714      the caller.  With the next inlineing both are increased.) */
715   pos = 0;
716   irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
717
718   if ((pos > 0) && (pos < MAX_INLINE)) {
719     /* There are calls to inline */
720     collect_phiprojs(irg);
721     for (i = 0; i < pos; i++) {
722       tarval *tv;
723       ir_graph *callee;
724       tv = get_Const_tarval(get_Call_ptr(calls[i]));
725       callee = get_entity_irg(tv->u.p.ent);
726       if ((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) {
727         /*printf(" inlineing "); DDME(tv->u.p.ent);*/
728         inline_method(calls[i], callee);
729       }
730     }
731   }
732
733   current_ir_graph = rem;
734 }
735
736
737 /********************************************************************/
738 /*  Code Placement.  Pinns all floating nodes to a block where they */
739 /*  will be executed only if needed.                                */
740 /********************************************************************/
741
742 static pdeq *worklist;          /* worklist of ir_node*s */
743
744 /* Find the earliest correct block for N.  --- Place N into the
745    same Block as its dominance-deepest Input.  */
746 static void
747 place_floats_early (ir_node *n)
748 {
749   int i, start;
750
751   /* we must not run into an infinite loop */
752   assert (irn_not_visited(n));
753   mark_irn_visited(n);
754
755   /* Place floating nodes. */
756   if (get_op_pinned(get_irn_op(n)) == floats) {
757     int depth = 0;
758     ir_node *b = new_Bad();   /* The block to place this node in */
759
760     assert(get_irn_op(n) != op_Block);
761
762     if ((get_irn_op(n) == op_Const) ||
763         (get_irn_op(n) == op_SymConst) ||
764         (is_Bad(n))) {
765       /* These nodes will not be placed by the loop below. */
766       b = get_irg_start_block(current_ir_graph);
767       depth = 1;
768     }
769
770     /* find the block for this node. */
771     for (i = 0; i < get_irn_arity(n); i++) {
772       ir_node *dep = get_irn_n(n, i);
773       ir_node *dep_block;
774       if ((irn_not_visited(dep)) &&
775           (get_op_pinned(get_irn_op(dep)) == floats)) {
776         place_floats_early (dep);
777       }
778       /* Because all loops contain at least one pinned node, now all
779          our inputs are either pinned or place_early has already
780          been finished on them.  We do not have any unfinished inputs!  */
781       dep_block = get_nodes_Block(dep);
782       if ((!is_Bad(dep_block)) &&
783           (get_Block_dom_depth(dep_block) > depth)) {
784         b = dep_block;
785         depth = get_Block_dom_depth(dep_block);
786       }
787       /* Avoid that the node is placed in the Start block */
788       if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
789         b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
790         assert(b != get_irg_start_block(current_ir_graph));
791         depth = 2;
792       }
793     }
794     set_nodes_Block(n, b);
795   }
796
797   /* Add predecessors of non floating nodes on worklist. */
798   start = (get_irn_op(n) == op_Block) ? 0 : -1;
799   for (i = start; i < get_irn_arity(n); i++) {
800     ir_node *pred = get_irn_n(n, i);
801     if (irn_not_visited(pred)) {
802       pdeq_putr (worklist, pred);
803     }
804   }
805 }
806
807 /* Floating nodes form subgraphs that begin at nodes as Const, Load,
808    Start, Call and end at pinned nodes as Store, Call.  Place_early
809    places all floating nodes reachable from its argument through floating
810    nodes and adds all beginnings at pinned nodes to the worklist. */
811 inline void place_early () {
812   int i;
813   bool del_me;
814
815   assert(worklist);
816   inc_irg_visited(current_ir_graph);
817
818   /* this inits the worklist */
819   place_floats_early (get_irg_end(current_ir_graph));
820
821   /* Work the content of the worklist. */
822   while (!pdeq_empty (worklist)) {
823     ir_node *n = pdeq_getl (worklist);
824     if (irn_not_visited(n)) place_floats_early (n);
825   }
826
827   set_irg_outs_inconsistent(current_ir_graph);
828   current_ir_graph->pinned = pinned;
829 }
830
831
832 /* deepest common dominance ancestor of DCA and CONSUMER of PRODUCER */
833 static ir_node *
834 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
835 {
836   ir_node *block;
837
838   /* Compute the latest block into which we can place a node so that it is
839      before consumer. */
840   if (get_irn_op(consumer) == op_Phi) {
841     /* our comsumer is a Phi-node, the effective use is in all those
842        blocks through which the Phi-node reaches producer */
843     int i;
844     ir_node *phi_block = get_nodes_Block(consumer);
845     for (i = 0;  i < get_irn_arity(consumer); i++) {
846       if (get_irn_n(consumer, i) == producer) {
847         block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
848       }
849     }
850   } else {
851     assert(is_no_Block(consumer));
852     block = get_nodes_Block(consumer);
853   }
854
855   /* Compute the deepest common ancestor of block and dca. */
856   assert(block);
857   if (!dca) return block;
858   while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
859     block = get_Block_idom(block);
860   while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
861     dca = get_Block_idom(dca);
862   while (block != dca)
863     { block = get_Block_idom(block); dca = get_Block_idom(dca); }
864
865   return dca;
866 }
867
868 #if 0
869 /* @@@ Needs loop informations.  Will implement later interprocedural. */
870 static void
871 move_out_of_loops (ir_node *n, ir_node *dca)
872 {
873   assert(dca);
874
875   /* Find the region deepest in the dominator tree dominating
876      dca with the least loop nesting depth, but still dominated
877      by our early placement. */
878   ir_node *best = dca;
879   while (dca != get_nodes_Block(n)) {
880     dca = get_Block_idom(dca);
881     if (!dca) break; /* should we put assert(dca)? */
882     if (get_Block_loop_depth(dca) < get_Block_loop_depth(best)) {
883       best = dca;
884     }
885   }
886   if (get_Block_dom_depth(best) >= get_Block_dom_depth(get_nodes_Block(n)))
887     set_nodes_Block(n, best);
888 }
889 #endif
890
891 /* Find the latest legal block for N and place N into the
892    `optimal' Block between the latest and earliest legal block.
893    The `optimal' block is the dominance-deepest block of those
894    with the least loop-nesting-depth.  This places N out of as many
895    loops as possible and then makes it as controldependant as
896    possible. */
897 static void
898 place_floats_late (ir_node *n)
899 {
900   int i;
901
902   assert (irn_not_visited(n)); /* no multiple placement */
903
904   /* no need to place block nodes, control nodes are already placed. */
905   if ((get_irn_op(n) != op_Block) && (!is_cfop(n)) && (get_irn_mode(n) != mode_X)) {
906     /* Assure that our users are all placed, except the Phi-nodes.
907        --- Each dataflow cycle contains at least one Phi-node.  We
908        have to break the `user has to be placed before the
909        producer' dependance cycle and the Phi-nodes are the
910        place to do so, because we need to base our placement on the
911        final region of our users, which is OK with Phi-nodes, as they
912        are pinned, and they never have to be placed after a
913        producer of one of their inputs in the same block anyway. */
914     for (i = 0; i < get_irn_n_outs(n); i++) {
915       ir_node *succ = get_irn_out(n, i);
916       if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
917         place_floats_late (succ);
918     }
919
920     /* We have to determine the final block of this node... except for constants. */
921     if ((get_op_pinned(get_irn_op(n)) == floats) &&
922         (get_irn_op(n) != op_Const) &&
923         (get_irn_op(n) != op_SymConst)) {
924       ir_node *dca = NULL;      /* deepest common ancestor in the
925                                    dominator tree of all nodes'
926                                    blocks depending on us; our final
927                                    placement has to dominate DCA. */
928       for (i = 0; i < get_irn_n_outs(n); i++) {
929         dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
930       }
931       set_nodes_Block(n, dca);
932 #if 0
933       move_out_of_loops (n, dca);
934 #endif
935     }
936   }
937
938   mark_irn_visited(n);
939
940   /* Add predecessors of all non-floating nodes on list. (Those of floating
941      nodes are placeded already and therefore are marked.)  */
942   for (i = 0; i < get_irn_n_outs(n); i++) {
943     if (irn_not_visited(get_irn_out(n, i))) {
944       pdeq_putr (worklist, get_irn_out(n, i));
945     }
946   }
947 }
948
949 inline void place_late() {
950   assert(worklist);
951   inc_irg_visited(current_ir_graph);
952
953   /* This fills the worklist initially. */
954   place_floats_late(get_irg_start_block(current_ir_graph));
955   /* And now empty the worklist again... */
956   while (!pdeq_empty (worklist)) {
957     ir_node *n = pdeq_getl (worklist);
958     if (irn_not_visited(n)) place_floats_late(n);
959   }
960 }
961
962 void place_code(ir_graph *irg) {
963   ir_graph *rem = current_ir_graph;
964   current_ir_graph = irg;
965
966   if (!(get_optimize() && get_opt_global_cse())) return;
967
968   /* Handle graph state */
969   assert(get_irg_phase_state(irg) != phase_building);
970   if (get_irg_dom_state(irg) != dom_consistent)
971     compute_doms(irg);
972
973   /* Place all floating nodes as early as possible. This guarantees
974      a legal code placement. */
975   worklist = new_pdeq ();
976   place_early();
977
978   /* place_early invalidates the outs, place_late needs them. */
979   compute_outs(irg);
980   /* Now move the nodes down in the dominator tree. This reduces the
981      unnecessary executions of the node. */
982   place_late();
983
984   set_irg_outs_inconsistent(current_ir_graph);
985   del_pdeq (worklist);
986   current_ir_graph = rem;
987 }
988
989
990
991 /********************************************************************/
992 /* Control flow optimization.                                       */
993 /* Removes Bad control flow predecessors and empty blocks.  A block */
994 /* is empty if it contains only a Jmp node.                         */
995 /* */
996 /********************************************************************/
997
998
999 static void merge_blocks(ir_node *n, void *env) {
1000   int i;
1001   set_irn_link(n, NULL);
1002
1003   if (get_irn_op(n) == op_Block) {
1004     /* Remove Tuples */
1005     for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1006       set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1007   } else if (get_irn_mode(n) == mode_X) {
1008     /* We will soon visit a block.  Optimize it before visiting! */
1009     ir_node *b = get_nodes_Block(n);
1010     ir_node *new = equivalent_node(b);
1011     while (irn_not_visited(b) && (!is_Bad(new)) && (new != b)) {
1012       /* We would have to run gigo if new is bad. */
1013       if (get_optimize() && get_opt_control_flow()) exchange (b, new);
1014       b = new;
1015       new = equivalent_node(b);
1016     }
1017     if (is_Bad(new)) exchange (n, new_Bad());
1018   }
1019 }
1020
1021 static void collect_nodes(ir_node *n, void *env) {
1022   int i;
1023   if (is_no_Block(n)) {
1024     ir_node *b = get_nodes_Block(n);
1025
1026     if ((get_irn_op(n) == op_Phi)) {
1027       /* Collect Phi nodes to compact ins along with block's ins. */
1028       set_irn_link(n, get_irn_link(b));
1029       set_irn_link(b, n);
1030     } else if (get_irn_op(n) != op_Jmp) {  /* Check for non empty block. */
1031       mark_Block_block_visited(b);
1032     }
1033   }
1034 }
1035
1036 /* Returns true if pred is pred of block */
1037 int is_pred_of(ir_node *pred, ir_node *b) {
1038   int i;
1039   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1040     ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1041     if (b_pred == pred) return 1;
1042   }
1043   return 0;
1044 }
1045
1046 int test_whether_dispensable(ir_node *b, int pos) {
1047   int i, j, n_preds = 1;
1048   int dispensable = 1;
1049   ir_node *cfop = get_Block_cfgpred(b, pos);
1050   ir_node *pred = get_nodes_Block(cfop);
1051
1052   if (get_Block_block_visited(pred) + 1
1053       < get_irg_block_visited(current_ir_graph)) {
1054     if (!get_optimize() || !get_opt_control_flow()) {
1055       /* Mark block so that is will not be removed. */
1056       set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1057       return 1;
1058     }
1059     /* Seems to be empty. */
1060     if (!get_irn_link(b)) {
1061       /* There are no Phi nodes ==> dispensable. */
1062       n_preds = get_Block_n_cfgpreds(pred);
1063     } else {
1064       /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1065          Work preds < pos as if they were already removed. */
1066       for (i = 0; i < pos; i++) {
1067         ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1068         if (get_Block_block_visited(b_pred) + 1
1069             < get_irg_block_visited(current_ir_graph)) {
1070           for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1071             ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1072             if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1073           }
1074         } else {
1075           if (is_pred_of(b_pred, pred)) dispensable = 0;
1076         }
1077       }
1078       for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1079         ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1080         if (is_pred_of(b_pred, pred)) dispensable = 0;
1081       }
1082       if (!dispensable) {
1083         set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1084         n_preds = 1;
1085       } else {
1086         n_preds = get_Block_n_cfgpreds(pred);
1087       }
1088     }
1089   }
1090
1091   return n_preds;
1092 }
1093
1094 void optimize_blocks(ir_node *b, void *env) {
1095   int i, j, k, max_preds, n_preds;
1096   ir_node *pred, *phi;
1097   ir_node **in;
1098
1099   max_preds = 0;
1100   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1101     pred = get_Block_cfgpred(b, i);
1102     max_preds += test_whether_dispensable(b, i);
1103   }
1104   in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1105
1106
1107   /** Debug output **
1108   printf(" working on "); DDMN(b);
1109   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1110     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1111     if (is_Bad(get_Block_cfgpred(b, i))) {
1112       printf("  removing Bad %i\n ", i);
1113     } else if (get_Block_block_visited(pred) +1
1114                < get_irg_block_visited(current_ir_graph)) {
1115       printf("  removing pred %i ", i); DDMN(pred);
1116     } else { printf("  Nothing to do for "); DDMN(pred); }
1117   }
1118  ** end Debug output **/
1119
1120   /** Fix the Phi nodes **/
1121   phi = get_irn_link(b);
1122   while (phi) {
1123     assert(get_irn_op(phi) == op_Phi);
1124     /* Find the new predecessors for the Phi */
1125     n_preds = 0;
1126     for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1127       pred = get_nodes_Block(get_Block_cfgpred(b, i));
1128       if (is_Bad(get_Block_cfgpred(b, i))) {
1129         /* Do nothing */
1130       } else if (get_Block_block_visited(pred) +1
1131                  < get_irg_block_visited(current_ir_graph)) {
1132         /* It's an empty block and not yet visited. */
1133         ir_node *phi_pred = get_Phi_pred(phi, i);
1134         for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1135           if (get_nodes_Block(phi_pred) == pred) {
1136             assert(get_irn_op(phi_pred) == op_Phi);  /* Block is empty!! */
1137             in[n_preds] = get_Phi_pred(phi_pred, j);
1138           } else {
1139             in[n_preds] = phi_pred;
1140           }
1141           n_preds++;
1142         }
1143 #if 0
1144         /* @@@ hier brauche ich schleifeninformation!!! Wenn keine Rueckwaertskante
1145            dann darfs auch keine Verwendung geben. */
1146         if (get_nodes_Block(phi_pred) == pred) {
1147           /* remove the Phi as it might be kept alive. Further there
1148              might be other users. */
1149           exchange(phi_pred, phi);  /* geht, is aber doch semantisch falsch! */
1150         }
1151 #endif
1152       } else {
1153         in[n_preds] = get_Phi_pred(phi, i);
1154         n_preds ++;
1155       }
1156     }
1157     /* Fix the node */
1158     set_irn_in(phi, n_preds, in);
1159     phi = get_irn_link(phi);
1160   }
1161
1162   /** Move Phi nodes from removed blocks to this one.
1163       This happens only if merge between loop backedge and single loop entry. **/
1164   for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1165     pred = get_nodes_Block(get_Block_cfgpred(b, k));
1166     if (get_Block_block_visited(pred) +1
1167         < get_irg_block_visited(current_ir_graph)) {
1168       phi = get_irn_link(pred);
1169       while (phi) {
1170         if (get_irn_op(phi) == op_Phi) {
1171           set_nodes_Block(phi, b);
1172
1173           n_preds = 0;
1174           for (i = 0; i < k; i++) {
1175             pred = get_nodes_Block(get_Block_cfgpred(b, i));
1176             if (is_Bad(get_Block_cfgpred(b, i))) {
1177               /* Do nothing */
1178             } else if (get_Block_block_visited(pred) +1
1179                        < get_irg_block_visited(current_ir_graph)) {
1180               /* It's an empty block and not yet visited. */
1181               for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1182                 /* @@@ Hier brauche ich schleifeninformation!!! Kontrllflusskante
1183                    muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1184                    Anweisungen.) Trotzdem tuts bisher!! */
1185                 in[n_preds] = phi;
1186                 n_preds++;
1187               }
1188             } else {
1189               in[n_preds] = phi;
1190               n_preds++;
1191             }
1192           }
1193           for (i = 0; i < get_Phi_n_preds(phi); i++) {
1194             in[n_preds] = get_Phi_pred(phi, i);
1195             n_preds++;
1196           }
1197           for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1198             pred = get_nodes_Block(get_Block_cfgpred(b, i));
1199             if (is_Bad(get_Block_cfgpred(b, i))) {
1200               /* Do nothing */
1201             } else if (get_Block_block_visited(pred) +1
1202                        < get_irg_block_visited(current_ir_graph)) {
1203               /* It's an empty block and not yet visited. */
1204               for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1205                 in[n_preds] = phi;
1206                 n_preds++;
1207               }
1208             } else {
1209               in[n_preds] = phi;
1210               n_preds++;
1211             }
1212           }
1213           set_irn_in(phi, n_preds, in);
1214         }
1215         phi = get_irn_link(phi);
1216       }
1217     }
1218   }
1219
1220   /** Fix the block **/
1221   n_preds = 0;
1222   for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1223     pred = get_nodes_Block(get_Block_cfgpred(b, i));
1224     if (is_Bad(get_Block_cfgpred(b, i))) {
1225       /* Do nothing */
1226     } else if (get_Block_block_visited(pred) +1
1227                < get_irg_block_visited(current_ir_graph)) {
1228       /* It's an empty block and not yet visited. */
1229       assert(get_Block_n_cfgpreds(b) > 1);
1230                         /* Else it should be optimized by equivalent_node. */
1231       for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1232         in[n_preds] = get_Block_cfgpred(pred, j);
1233         n_preds++;
1234       }
1235       /* Remove block as it might be kept alive. */
1236       exchange(pred, b/*new_Bad()*/);
1237     } else {
1238       in[n_preds] = get_Block_cfgpred(b, i);
1239       n_preds ++;
1240     }
1241   }
1242   set_irn_in(b, n_preds, in);
1243   free(in);
1244 }
1245
1246 void optimize_cf(ir_graph *irg) {
1247   int i;
1248   ir_node **in;
1249   ir_node *end = get_irg_end(irg);
1250   ir_graph *rem = current_ir_graph;
1251   current_ir_graph = irg;
1252
1253
1254   /* Handle graph state */
1255   assert(get_irg_phase_state(irg) != phase_building);
1256   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1257     set_irg_outs_inconsistent(current_ir_graph);
1258   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1259     set_irg_dom_inconsistent(current_ir_graph);
1260
1261   //DDME(get_irg_ent(irg));
1262
1263   /* Use block visited flag to mark non-empty blocks. */
1264   inc_irg_block_visited(irg);
1265   irg_walk(end, merge_blocks, collect_nodes, NULL);
1266
1267   /* Optimize the standard code. */
1268   irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1269
1270   /* Walk all keep alives, optimize them if block, add to new in-array
1271      for end if useful. */
1272   in = NEW_ARR_F (ir_node *, 1);
1273   in[0] = get_nodes_Block(end);
1274
1275   inc_irg_visited(current_ir_graph);
1276   for(i = 0; i < get_End_n_keepalives(end); i++) {
1277     ir_node *ka = get_End_keepalive(end, i);
1278     if (irn_not_visited(ka))
1279       if ((get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1280         set_irg_block_visited(current_ir_graph,  /* Don't walk all the way to Start. */
1281                               get_irg_block_visited(current_ir_graph)-1);
1282         irg_block_walk(ka, optimize_blocks, NULL, NULL);
1283         mark_irn_visited(ka);
1284         ARR_APP1 (ir_node *, in, ka);
1285       } else if (get_irn_op(ka) == op_Phi) {
1286         mark_irn_visited(ka);
1287         ARR_APP1 (ir_node *, in, ka);
1288       }
1289     }
1290   /* DEL_ARR_F(end->in);   GL @@@ tut nicht ! */
1291   end->in = in;
1292
1293   current_ir_graph = rem;
1294 }