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
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include "irgraph_t.h"
34 #include "pdeq.h" /* Fuer code placement */
39 #include "irbackedge_t.h"
46 #include "iredges_t.h"
49 /* Defined in iropt.c */
50 pset *new_identities (void);
51 void del_identities (pset *value_table);
52 void add_identities (pset *value_table, ir_node *node);
54 /*------------------------------------------------------------------*/
55 /* apply optimizations of iropt to all nodes. */
56 /*------------------------------------------------------------------*/
58 static void init_link (ir_node *n, void *env) {
59 set_irn_link(n, NULL);
62 #if 0 /* Old version. Avoids Ids.
63 This is not necessary: we do a post walk, and get_irn_n
64 removes ids anyways. So it's much cheaper to call the
65 optimization less often and use the exchange() algorithm. */
67 optimize_in_place_wrapper (ir_node *n, void *env) {
69 ir_node *optimized, *old;
71 irn_arity = get_irn_arity(n);
72 for (i = 0; i < irn_arity; i++) {
73 /* get_irn_n skips Id nodes, so comparison old != optimized does not
74 show all optimizations. Therefore always set new predecessor. */
75 old = get_irn_intra_n(n, i);
76 optimized = optimize_in_place_2(old);
77 set_irn_n(n, i, optimized);
80 if (get_irn_op(n) == op_Block) {
81 optimized = optimize_in_place_2(n);
82 if (optimized != n) exchange (n, optimized);
87 optimize_in_place_wrapper (ir_node *n, void *env) {
88 ir_node *optimized = optimize_in_place_2(n);
89 if (optimized != n) exchange (n, optimized);
94 static INLINE void do_local_optimize(ir_node *n) {
95 /* Handle graph state */
96 assert(get_irg_phase_state(current_ir_graph) != phase_building);
98 if (get_opt_global_cse())
99 set_irg_pinned(current_ir_graph, op_pin_state_floats);
100 set_irg_outs_inconsistent(current_ir_graph);
101 set_irg_doms_inconsistent(current_ir_graph);
102 set_irg_loopinfo_inconsistent(current_ir_graph);
104 /* Clean the value_table in irg for the CSE. */
105 del_identities(current_ir_graph->value_table);
106 current_ir_graph->value_table = new_identities();
108 /* walk over the graph */
109 irg_walk(n, init_link, optimize_in_place_wrapper, NULL);
112 void local_optimize_node(ir_node *n) {
113 ir_graph *rem = current_ir_graph;
114 current_ir_graph = get_irn_irg(n);
116 do_local_optimize(n);
118 current_ir_graph = rem;
122 * Block-Walker: uses dominance depth to mark dead blocks.
124 static void kill_dead_blocks(ir_node *block, void *env)
126 if (get_Block_dom_depth(block) < 0)
127 set_Block_dead(block);
131 local_optimize_graph (ir_graph *irg) {
132 ir_graph *rem = current_ir_graph;
133 current_ir_graph = irg;
135 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
136 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
138 do_local_optimize(irg->end);
140 current_ir_graph = rem;
144 /*------------------------------------------------------------------*/
145 /* Routines for dead node elimination / copying garbage collection */
146 /* of the obstack. */
147 /*------------------------------------------------------------------*/
150 * Remember the new node in the old node by using a field all nodes have.
152 #define set_new_node(oldn, newn) set_irn_link(oldn, newn)
155 * Get this new node, before the old node is forgotten.
157 #define get_new_node(oldn) get_irn_link(oldn)
160 * Check if a new node was set.
162 #define has_new_node(n) (get_new_node(n) != NULL)
165 * We use the block_visited flag to mark that we have computed the
166 * number of useful predecessors for this block.
167 * Further we encode the new arity in this flag in the old blocks.
168 * Remembering the arity is useful, as it saves a lot of pointer
169 * accesses. This function is called for all Phi and Block nodes
173 compute_new_arity(ir_node *b) {
174 int i, res, irn_arity;
177 irg_v = get_irg_block_visited(current_ir_graph);
178 block_v = get_Block_block_visited(b);
179 if (block_v >= irg_v) {
180 /* we computed the number of preds for this block and saved it in the
182 return block_v - irg_v;
184 /* compute the number of good predecessors */
185 res = irn_arity = get_irn_arity(b);
186 for (i = 0; i < irn_arity; i++)
187 if (get_irn_opcode(get_irn_n(b, i)) == iro_Bad) res--;
188 /* save it in the flag. */
189 set_Block_block_visited(b, irg_v + res);
195 * Copies the node to the new obstack. The Ins of the new node point to
196 * the predecessors on the old obstack. For block/phi nodes not all
197 * predecessors might be copied. n->link points to the new node.
198 * For Phi and Block nodes the function allocates in-arrays with an arity
199 * only for useful predecessors. The arity is determined by counting
200 * the non-bad predecessors of the block.
202 * @param n The node to be copied
203 * @param env if non-NULL, the node number attribute will be copied to the new node
205 * Note: Also used for loop unrolling.
207 static void copy_node(ir_node *n, void *env) {
210 ir_op *op = get_irn_op(n);
211 int copy_node_nr = env != NULL;
213 /* The end node looses it's flexible in array. This doesn't matter,
214 as dead node elimination builds End by hand, inlineing doesn't use
216 /* assert(op == op_End || ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC)); */
219 /* node copied already */
221 } else if (op == op_Block) {
223 new_arity = compute_new_arity(n);
224 n->attr.block.graph_arr = NULL;
226 block = get_nodes_block(n);
228 new_arity = compute_new_arity(block);
230 new_arity = get_irn_arity(n);
233 nn = new_ir_node(get_irn_dbg_info(n),
240 /* Copy the attributes. These might point to additional data. If this
241 was allocated on the old obstack the pointers now are dangling. This
242 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
243 copy_node_attr(n, nn);
244 new_backedge_info(nn);
248 /* for easier debugging, we want to copy the node numbers too */
249 nn->node_nr = n->node_nr;
254 hook_dead_node_elim_subst(current_ir_graph, n, nn);
258 * Copies new predecessors of old node to new node remembered in link.
259 * Spare the Bad predecessors of Phi and Block nodes.
262 copy_preds (ir_node *n, void *env) {
266 nn = get_new_node(n);
268 /* printf("\n old node: "); DDMSG2(n);
269 printf(" new node: "); DDMSG2(nn);
270 printf(" arities: old: %d, new: %d\n", get_irn_arity(n), get_irn_arity(nn)); */
273 /* Don't copy Bad nodes. */
275 irn_arity = get_irn_arity(n);
276 for (i = 0; i < irn_arity; i++)
277 if (! is_Bad(get_irn_n(n, i))) {
278 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
279 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
282 /* repair the block visited flag from above misuse. Repair it in both
283 graphs so that the old one can still be used. */
284 set_Block_block_visited(nn, 0);
285 set_Block_block_visited(n, 0);
286 /* Local optimization could not merge two subsequent blocks if
287 in array contained Bads. Now it's possible.
288 We don't call optimize_in_place as it requires
289 that the fields in ir_graph are set properly. */
290 if ((get_opt_control_flow_straightening()) &&
291 (get_Block_n_cfgpreds(nn) == 1) &&
292 (get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp)) {
293 ir_node *old = get_nodes_block(get_Block_cfgpred(nn, 0));
295 /* Jmp jumps into the block it is in -- deal self cycle. */
296 assert(is_Bad(get_new_node(get_irg_bad(current_ir_graph))));
297 exchange(nn, get_new_node(get_irg_bad(current_ir_graph)));
302 } else if (get_irn_op(n) == op_Phi) {
303 /* Don't copy node if corresponding predecessor in block is Bad.
304 The Block itself should not be Bad. */
305 block = get_nodes_block(n);
306 set_irn_n (nn, -1, get_new_node(block));
308 irn_arity = get_irn_arity(n);
309 for (i = 0; i < irn_arity; i++)
310 if (! is_Bad(get_irn_n(block, i))) {
311 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
312 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
315 /* If the pre walker reached this Phi after the post walker visited the
316 block block_visited is > 0. */
317 set_Block_block_visited(get_nodes_block(n), 0);
318 /* Compacting the Phi's ins might generate Phis with only one
320 if (get_irn_arity(nn) == 1)
321 exchange(nn, get_irn_n(nn, 0));
323 irn_arity = get_irn_arity(n);
324 for (i = -1; i < irn_arity; i++)
325 set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
327 /* Now the new node is complete. We can add it to the hash table for CSE.
328 @@@ inlining aborts if we identify End. Why? */
329 if (get_irn_op(nn) != op_End)
330 add_identities (current_ir_graph->value_table, nn);
334 * Copies the graph recursively, compacts the keep-alives of the end node.
336 * @param irg the graph to be copied
337 * @param copy_node_nr If non-zero, the node number will be copied
339 static void copy_graph(ir_graph *irg, int copy_node_nr) {
340 ir_node *oe, *ne, *ob, *nb, *om, *nm; /* old end, new end, old bad, new bad, old NoMem, new NoMem */
341 ir_node *ka; /* keep alive */
344 oe = get_irg_end(irg);
345 /* copy the end node by hand, allocate dynamic in array! */
346 ne = new_ir_node(get_irn_dbg_info(oe),
353 /* Copy the attributes. Well, there might be some in the future... */
354 copy_node_attr(oe, ne);
355 set_new_node(oe, ne);
357 /* copy the Bad node */
358 ob = get_irg_bad(irg);
359 nb = new_ir_node(get_irn_dbg_info(ob),
366 set_new_node(ob, nb);
368 /* copy the NoMem node */
369 om = get_irg_no_mem(irg);
370 nm = new_ir_node(get_irn_dbg_info(om),
377 set_new_node(om, nm);
379 /* copy the live nodes */
380 irg_walk(get_nodes_block(oe), copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
381 /* copy_preds for the end node ... */
382 set_nodes_block(ne, get_new_node(get_nodes_block(oe)));
384 /*- ... and now the keep alives. -*/
385 /* First pick the not marked block nodes and walk them. We must pick these
386 first as else we will oversee blocks reachable from Phis. */
387 irn_arity = get_irn_arity(oe);
388 for (i = 0; i < irn_arity; i++) {
389 ka = get_irn_intra_n(oe, i);
391 (get_irn_visited(ka) < get_irg_visited(irg))) {
392 /* We must keep the block alive and copy everything reachable */
393 set_irg_visited(irg, get_irg_visited(irg)-1);
394 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
395 add_End_keepalive(ne, get_new_node(ka));
399 /* Now pick other nodes. Here we will keep all! */
400 irn_arity = get_irn_arity(oe);
401 for (i = 0; i < irn_arity; i++) {
402 ka = get_irn_intra_n(oe, i);
404 if (get_irn_visited(ka) < get_irg_visited(irg)) {
405 /* We didn't copy the node yet. */
406 set_irg_visited(irg, get_irg_visited(irg)-1);
407 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
409 add_End_keepalive(ne, get_new_node(ka));
413 /* start block sometimes only reached after keep alives */
414 set_nodes_block(nb, get_new_node(get_nodes_block(ob)));
415 set_nodes_block(nm, get_new_node(get_nodes_block(om)));
419 * Copies the graph reachable from current_ir_graph->end to the obstack
420 * in current_ir_graph and fixes the environment.
421 * Then fixes the fields in current_ir_graph containing nodes of the
424 * @param copy_node_nr If non-zero, the node number will be copied
427 copy_graph_env (int copy_node_nr) {
428 ir_graph *irg = current_ir_graph;
429 ir_node *old_end, *n;
430 /* Not all nodes remembered in irg might be reachable
431 from the end node. Assure their link is set to NULL, so that
432 we can test whether new nodes have been computed. */
433 set_irn_link(get_irg_frame (irg), NULL);
434 set_irn_link(get_irg_globals (irg), NULL);
435 set_irn_link(get_irg_args (irg), NULL);
436 set_irn_link(get_irg_initial_mem(irg), NULL);
437 set_irn_link(get_irg_bad (irg), NULL);
438 set_irn_link(get_irg_no_mem (irg), NULL);
440 /* we use the block walk flag for removing Bads from Blocks ins. */
441 inc_irg_block_visited(irg);
444 copy_graph(irg, copy_node_nr);
446 /* fix the fields in irg */
447 old_end = get_irg_end(irg);
448 set_irg_end (irg, get_new_node(old_end));
449 set_irg_end_except (irg, get_irg_end(irg));
450 set_irg_end_reg (irg, get_irg_end(irg));
452 set_irg_end_block (irg, get_new_node(get_irg_end_block(irg)));
454 n = get_irg_frame(irg);
455 if (!has_new_node(n)) {
456 copy_node (n, INT_TO_PTR(copy_node_nr));
459 n = get_irg_globals(irg);
460 if (!has_new_node(n)) {
461 copy_node (n, INT_TO_PTR(copy_node_nr));
464 n = get_irg_initial_mem(irg);
465 if (!has_new_node(n)) {
466 copy_node (n, INT_TO_PTR(copy_node_nr));
469 n = get_irg_args(irg);
470 if (!has_new_node(n)) {
471 copy_node (n, INT_TO_PTR(copy_node_nr));
474 n = get_irg_bad(irg);
475 if (!has_new_node(n)) {
476 copy_node(n, INT_TO_PTR(copy_node_nr));
479 n = get_irg_no_mem(irg);
480 if (!has_new_node(n)) {
481 copy_node(n, INT_TO_PTR(copy_node_nr));
484 set_irg_start (irg, get_new_node(get_irg_start(irg)));
485 set_irg_start_block(irg, get_new_node(get_irg_start_block(irg)));
486 set_irg_frame (irg, get_new_node(get_irg_frame(irg)));
487 set_irg_globals (irg, get_new_node(get_irg_globals(irg)));
488 set_irg_initial_mem(irg, get_new_node(get_irg_initial_mem(irg)));
489 set_irg_args (irg, get_new_node(get_irg_args(irg)));
490 set_irg_bad (irg, get_new_node(get_irg_bad(irg)));
491 set_irg_no_mem (irg, get_new_node(get_irg_no_mem(irg)));
495 * Copies all reachable nodes to a new obstack. Removes bad inputs
496 * from block nodes and the corresponding inputs from Phi nodes.
497 * Merges single exit blocks with single entry blocks and removes
499 * Adds all new nodes to a new hash table for CSE. Does not
500 * perform CSE, so the hash table might contain common subexpressions.
503 dead_node_elimination(ir_graph *irg) {
505 int rem_ipview = get_interprocedural_view();
506 struct obstack *graveyard_obst = NULL;
507 struct obstack *rebirth_obst = NULL;
509 if (get_opt_optimize() && get_opt_dead_node_elimination()) {
510 assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
512 /* inform statistics that we started a dead-node elimination run */
513 hook_dead_node_elim(irg, 1);
515 /* Remember external state of current_ir_graph. */
516 rem = current_ir_graph;
517 current_ir_graph = irg;
518 set_interprocedural_view(0);
520 assert(get_irg_phase_state(current_ir_graph) != phase_building);
522 /* Handle graph state */
523 free_callee_info(current_ir_graph);
524 free_irg_outs(current_ir_graph);
527 /* @@@ so far we loose loops when copying */
528 free_loop_information(current_ir_graph);
530 set_irg_doms_inconsistent(irg);
532 /* A quiet place, where the old obstack can rest in peace,
533 until it will be cremated. */
534 graveyard_obst = irg->obst;
536 /* A new obstack, where the reachable nodes will be copied to. */
537 rebirth_obst = xmalloc (sizeof(*rebirth_obst));
538 current_ir_graph->obst = rebirth_obst;
539 obstack_init (current_ir_graph->obst);
541 /* We also need a new hash table for cse */
542 del_identities (irg->value_table);
543 irg->value_table = new_identities ();
545 /* Copy the graph from the old to the new obstack */
548 /* Free memory from old unoptimized obstack */
549 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
550 xfree (graveyard_obst); /* ... then free it. */
552 /* inform statistics that the run is over */
553 hook_dead_node_elim(irg, 0);
555 current_ir_graph = rem;
556 set_interprocedural_view(rem_ipview);
561 * Relink bad predecessors of a block and store the old in array to the
562 * link field. This function is called by relink_bad_predecessors().
563 * The array of link field starts with the block operand at position 0.
564 * If block has bad predecessors, create a new in array without bad preds.
565 * Otherwise let in array untouched.
567 static void relink_bad_block_predecessors(ir_node *n, void *env) {
568 ir_node **new_in, *irn;
569 int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
571 /* if link field of block is NULL, look for bad predecessors otherwise
572 this is already done */
573 if (get_irn_op(n) == op_Block &&
574 get_irn_link(n) == NULL) {
576 /* save old predecessors in link field (position 0 is the block operand)*/
577 set_irn_link(n, get_irn_in(n));
579 /* count predecessors without bad nodes */
580 old_irn_arity = get_irn_arity(n);
581 for (i = 0; i < old_irn_arity; i++)
582 if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
584 /* arity changing: set new predecessors without bad nodes */
585 if (new_irn_arity < old_irn_arity) {
586 /* Get new predecessor array. We do not resize the array, as we must
587 keep the old one to update Phis. */
588 new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
590 /* set new predecessors in array */
593 for (i = 0; i < old_irn_arity; i++) {
594 irn = get_irn_n(n, i);
596 new_in[new_irn_n] = irn;
597 is_backedge(n, i) ? set_backedge(n, new_irn_n-1) : set_not_backedge(n, new_irn_n-1);
601 //ARR_SETLEN(int, n->attr.block.backedge, new_irn_arity);
602 ARR_SHRINKLEN(n->attr.block.backedge, new_irn_arity);
605 } /* ir node has bad predecessors */
607 } /* Block is not relinked */
611 * Relinks Bad predecessors from Blocks and Phis called by walker
612 * remove_bad_predecesors(). If n is a Block, call
613 * relink_bad_block_redecessors(). If n is a Phi-node, call also the relinking
614 * function of Phi's Block. If this block has bad predecessors, relink preds
617 static void relink_bad_predecessors(ir_node *n, void *env) {
618 ir_node *block, **old_in;
619 int i, old_irn_arity, new_irn_arity;
621 /* relink bad predecessors of a block */
622 if (get_irn_op(n) == op_Block)
623 relink_bad_block_predecessors(n, env);
625 /* If Phi node relink its block and its predecessors */
626 if (get_irn_op(n) == op_Phi) {
628 /* Relink predecessors of phi's block */
629 block = get_nodes_block(n);
630 if (get_irn_link(block) == NULL)
631 relink_bad_block_predecessors(block, env);
633 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
634 old_irn_arity = ARR_LEN(old_in);
636 /* Relink Phi predecessors if count of predecessors changed */
637 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
638 /* set new predecessors in array
639 n->in[0] remains the same block */
641 for(i = 1; i < old_irn_arity; i++)
642 if (!is_Bad((ir_node *)old_in[i])) {
643 n->in[new_irn_arity] = n->in[i];
644 is_backedge(n, i) ? set_backedge(n, new_irn_arity) : set_not_backedge(n, new_irn_arity);
648 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
649 ARR_SETLEN(int, n->attr.phi_backedge, new_irn_arity);
652 } /* n is a Phi node */
656 * Removes Bad Bad predecessors from Blocks and the corresponding
657 * inputs to Phi nodes as in dead_node_elimination but without
659 * On walking up set the link field to NULL, on walking down call
660 * relink_bad_predecessors() (This function stores the old in array
661 * to the link field and sets a new in array if arity of predecessors
664 void remove_bad_predecessors(ir_graph *irg) {
665 irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
672 __)|_| | \_/ | \_/(/_ |_/\__|__
674 The following stuff implements a facility that automatically patches
675 registered ir_node pointers to the new node when a dead node elimination occurs.
678 struct _survive_dce_t {
682 hook_entry_t dead_node_elim;
683 hook_entry_t dead_node_elim_subst;
686 typedef struct _survive_dce_list_t {
687 struct _survive_dce_list_t *next;
689 } survive_dce_list_t;
691 static void dead_node_hook(void *context, ir_graph *irg, int start)
693 survive_dce_t *sd = context;
695 /* Create a new map before the dead node elimination is performed. */
697 sd->new_places = pmap_create_ex(pmap_count(sd->places));
700 /* Patch back all nodes if dead node elimination is over and something is to be done. */
702 pmap_destroy(sd->places);
703 sd->places = sd->new_places;
704 sd->new_places = NULL;
708 static void dead_node_subst_hook(void *context, ir_graph *irg, ir_node *old, ir_node *nw)
710 survive_dce_t *sd = context;
711 survive_dce_list_t *list = pmap_get(sd->places, old);
713 /* If the node is to be patched back, write the new address to all registered locations. */
715 survive_dce_list_t *p;
717 for(p = list; p; p = p->next)
720 pmap_insert(sd->new_places, nw, list);
725 * Make a new Survive DCE environment.
727 survive_dce_t *new_survive_dce(void)
729 survive_dce_t *res = xmalloc(sizeof(res[0]));
730 obstack_init(&res->obst);
731 res->places = pmap_create();
732 res->new_places = NULL;
734 res->dead_node_elim.hook._hook_dead_node_elim = dead_node_hook;
735 res->dead_node_elim.context = res;
736 res->dead_node_elim.next = NULL;
738 res->dead_node_elim_subst.hook._hook_dead_node_elim_subst = dead_node_subst_hook;
739 res->dead_node_elim_subst.context = res;
740 res->dead_node_elim_subst.next = NULL;
742 register_hook(hook_dead_node_elim, &res->dead_node_elim);
743 register_hook(hook_dead_node_elim_subst, &res->dead_node_elim_subst);
748 * Free a Survive DCE environment.
750 void free_survive_dce(survive_dce_t *sd)
752 obstack_free(&sd->obst, NULL);
753 pmap_destroy(sd->places);
754 unregister_hook(hook_dead_node_elim, &sd->dead_node_elim);
755 unregister_hook(hook_dead_node_elim_subst, &sd->dead_node_elim_subst);
760 * Register a node pointer to be patched upon DCE.
761 * When DCE occurs, the node pointer specified by @p place will be
762 * patched to the new address of the node it is pointing to.
764 * @param sd The Survive DCE environment.
765 * @param place The address of the node pointer.
767 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place)
770 ir_node *irn = *place;
771 survive_dce_list_t *curr = pmap_get(sd->places, irn);
772 survive_dce_list_t *nw = obstack_alloc(&sd->obst, sizeof(nw));
777 pmap_insert(sd->places, irn, nw);
781 /*--------------------------------------------------------------------*/
782 /* Functionality for inlining */
783 /*--------------------------------------------------------------------*/
786 * Copy node for inlineing. Updates attributes that change when
787 * inlineing but not for dead node elimination.
789 * Copies the node by calling copy_node() and then updates the entity if
790 * it's a local one. env must be a pointer of the frame type of the
791 * inlined procedure. The new entities must be in the link field of
795 copy_node_inline (ir_node *n, void *env) {
797 ir_type *frame_tp = (ir_type *)env;
800 if (get_irn_op(n) == op_Sel) {
801 nn = get_new_node (n);
803 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
804 set_Sel_entity(nn, get_entity_link(get_Sel_entity(n)));
806 } else if (get_irn_op(n) == op_Block) {
807 nn = get_new_node (n);
808 nn->attr.block.irg = current_ir_graph;
812 static void find_addr(ir_node *node, void *env)
814 if (get_irn_opcode(node) == iro_Proj) {
815 if (get_Proj_proj(node) == pn_Start_P_value_arg_base)
821 * currently, we cannot inline two cases:
822 * - call with compound arguments
823 * - graphs that take the address of a parameter
825 * check these conditions here
827 static int can_inline(ir_node *call, ir_graph *called_graph)
829 ir_type *call_type = get_Call_type(call);
830 int params, ress, i, res;
831 assert(is_Method_type(call_type));
833 params = get_method_n_params(call_type);
834 ress = get_method_n_ress(call_type);
837 for (i = 0; i < params; ++i) {
838 ir_type *p_type = get_method_param_type(call_type, i);
840 if (is_compound_type(p_type))
845 for (i = 0; i < ress; ++i) {
846 ir_type *r_type = get_method_res_type(call_type, i);
848 if (is_compound_type(r_type))
853 irg_walk_graph(called_graph, find_addr, NULL, &res);
858 int inline_method(ir_node *call, ir_graph *called_graph) {
860 ir_node *post_call, *post_bl;
862 ir_node *end, *end_bl;
866 int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
868 ir_type *called_frame;
869 irg_inline_property prop = get_irg_inline_property(called_graph);
871 if ( (prop != irg_inline_forced) &&
872 (!get_opt_optimize() || !get_opt_inline() || (prop == irg_inline_forbidden))) return 0;
874 /* Do not inline variadic functions. */
875 if (get_method_variadicity(get_entity_type(get_irg_entity(called_graph))) == variadicity_variadic)
878 assert(get_method_n_params(get_entity_type(get_irg_entity(called_graph))) ==
879 get_method_n_params(get_Call_type(call)));
882 * currently, we cannot inline two cases:
883 * - call with compound arguments
884 * - graphs that take the address of a parameter
886 if (! can_inline(call, called_graph))
889 /* -- Turn off optimizations, this can cause problems when allocating new nodes. -- */
890 rem_opt = get_opt_optimize();
893 /* Handle graph state */
894 assert(get_irg_phase_state(current_ir_graph) != phase_building);
895 assert(get_irg_pinned(current_ir_graph) == op_pin_state_pinned);
896 assert(get_irg_pinned(called_graph) == op_pin_state_pinned);
897 set_irg_outs_inconsistent(current_ir_graph);
898 set_irg_extblk_inconsistent(current_ir_graph);
899 set_irg_doms_inconsistent(current_ir_graph);
900 set_irg_loopinfo_inconsistent(current_ir_graph);
901 set_irg_callee_info_state(current_ir_graph, irg_callee_info_inconsistent);
903 /* -- Check preconditions -- */
904 assert(is_Call(call));
905 /* @@@ does not work for InterfaceIII.java after cgana
906 assert(get_Call_type(call) == get_entity_type(get_irg_entity(called_graph)));
907 assert(smaller_type(get_entity_type(get_irg_entity(called_graph)),
908 get_Call_type(call)));
910 assert(get_type_tpop(get_Call_type(call)) == type_method);
911 if (called_graph == current_ir_graph) {
912 set_optimize(rem_opt);
916 /* here we know we WILL inline, so inform the statistics */
917 hook_inline(call, called_graph);
919 /* -- Decide how to handle exception control flow: Is there a handler
920 for the Call node, or do we branch directly to End on an exception?
922 0 There is a handler.
924 2 Exception handling not represented in Firm. -- */
926 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
927 for (proj = (ir_node *)get_irn_link(call); proj; proj = (ir_node *)get_irn_link(proj)) {
928 assert(get_irn_op(proj) == op_Proj);
929 if (get_Proj_proj(proj) == pn_Call_X_except) Xproj = proj;
930 if (get_Proj_proj(proj) == pn_Call_M_except) Mproj = proj;
932 if (Mproj) { assert(Xproj); exc_handling = 0; } /* Mproj */
933 else if (Xproj) { exc_handling = 1; } /* !Mproj && Xproj */
934 else { exc_handling = 2; } /* !Mproj && !Xproj */
939 the procedure and later replaces the Start node of the called graph.
940 Post_call is the old Call node and collects the results of the called
941 graph. Both will end up being a tuple. -- */
942 post_bl = get_nodes_block(call);
943 set_irg_current_block(current_ir_graph, post_bl);
944 /* XxMxPxP of Start + parameter of Call */
945 in[pn_Start_X_initial_exec] = new_Jmp();
946 in[pn_Start_M] = get_Call_mem(call);
947 in[pn_Start_P_frame_base] = get_irg_frame(current_ir_graph);
948 in[pn_Start_P_globals] = get_irg_globals(current_ir_graph);
949 in[pn_Start_T_args] = new_Tuple(get_Call_n_params(call), get_Call_param_arr(call));
950 /* in[pn_Start_P_value_arg_base] = ??? */
951 pre_call = new_Tuple(5, in);
955 The new block gets the ins of the old block, pre_call and all its
956 predecessors and all Phi nodes. -- */
957 part_block(pre_call);
959 /* -- Prepare state for dead node elimination -- */
960 /* Visited flags in calling irg must be >= flag in called irg.
961 Else walker and arity computation will not work. */
962 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
963 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
964 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
965 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
966 /* Set pre_call as new Start node in link field of the start node of
967 calling graph and pre_calls block as new block for the start block
969 Further mark these nodes so that they are not visited by the
971 set_irn_link(get_irg_start(called_graph), pre_call);
972 set_irn_visited(get_irg_start(called_graph), get_irg_visited(current_ir_graph));
973 set_irn_link(get_irg_start_block(called_graph), get_nodes_block(pre_call));
974 set_irn_visited(get_irg_start_block(called_graph), get_irg_visited(current_ir_graph));
975 set_irn_link(get_irg_bad(called_graph), get_irg_bad(current_ir_graph));
976 set_irn_visited(get_irg_bad(called_graph), get_irg_visited(current_ir_graph));
978 /* Initialize for compaction of in arrays */
979 inc_irg_block_visited(current_ir_graph);
981 /* -- Replicate local entities of the called_graph -- */
982 /* copy the entities. */
983 called_frame = get_irg_frame_type(called_graph);
984 for (i = 0; i < get_class_n_members(called_frame); i++) {
985 entity *new_ent, *old_ent;
986 old_ent = get_class_member(called_frame, i);
987 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
988 set_entity_link(old_ent, new_ent);
991 /* visited is > than that of called graph. With this trick visited will
992 remain unchanged so that an outer walker, e.g., searching the call nodes
993 to inline, calling this inline will not visit the inlined nodes. */
994 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
996 /* -- Performing dead node elimination inlines the graph -- */
997 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
999 /* @@@ endless loops are not copied!! -- they should be, I think... */
1000 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
1001 get_irg_frame_type(called_graph));
1003 /* Repair called_graph */
1004 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
1005 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
1006 set_Block_block_visited(get_irg_start_block(called_graph), 0);
1008 /* -- Merge the end of the inlined procedure with the call site -- */
1009 /* We will turn the old Call node into a Tuple with the following
1012 0: Phi of all Memories of Return statements.
1013 1: Jmp from new Block that merges the control flow from all exception
1014 predecessors of the old end block.
1015 2: Tuple of all arguments.
1016 3: Phi of Exception memories.
1017 In case the old Call directly branches to End on an exception we don't
1018 need the block merging all exceptions nor the Phi of the exception
1022 /* -- Precompute some values -- */
1023 end_bl = get_new_node(get_irg_end_block(called_graph));
1024 end = get_new_node(get_irg_end(called_graph));
1025 arity = get_irn_arity(end_bl); /* arity = n_exc + n_ret */
1026 n_res = get_method_n_ress(get_Call_type(call));
1028 res_pred = xmalloc (n_res * sizeof(*res_pred));
1029 cf_pred = xmalloc (arity * sizeof(*res_pred));
1031 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
1033 /* -- archive keepalives -- */
1034 irn_arity = get_irn_arity(end);
1035 for (i = 0; i < irn_arity; i++)
1036 add_End_keepalive(get_irg_end(current_ir_graph), get_irn_n(end, i));
1038 /* The new end node will die. We need not free as the in array is on the obstack:
1039 copy_node() only generated 'D' arrays. */
1041 /* -- Replace Return nodes by Jump nodes. -- */
1043 for (i = 0; i < arity; i++) {
1045 ret = get_irn_n(end_bl, i);
1046 if (is_Return(ret)) {
1047 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_block(ret));
1051 set_irn_in(post_bl, n_ret, cf_pred);
1053 /* -- Build a Tuple for all results of the method.
1054 Add Phi node if there was more than one Return. -- */
1055 turn_into_tuple(post_call, 4);
1056 /* First the Memory-Phi */
1058 for (i = 0; i < arity; i++) {
1059 ret = get_irn_n(end_bl, i);
1060 if (is_Return(ret)) {
1061 cf_pred[n_ret] = get_Return_mem(ret);
1065 phi = new_Phi(n_ret, cf_pred, mode_M);
1066 set_Tuple_pred(call, pn_Call_M_regular, phi);
1067 /* Conserve Phi-list for further inlinings -- but might be optimized */
1068 if (get_nodes_block(phi) == post_bl) {
1069 set_irn_link(phi, get_irn_link(post_bl));
1070 set_irn_link(post_bl, phi);
1072 /* Now the real results */
1074 for (j = 0; j < n_res; j++) {
1076 for (i = 0; i < arity; i++) {
1077 ret = get_irn_n(end_bl, i);
1078 if (get_irn_op(ret) == op_Return) {
1079 cf_pred[n_ret] = get_Return_res(ret, j);
1084 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
1088 /* Conserve Phi-list for further inlinings -- but might be optimized */
1089 if (get_nodes_block(phi) == post_bl) {
1090 set_irn_link(phi, get_irn_link(post_bl));
1091 set_irn_link(post_bl, phi);
1094 set_Tuple_pred(call, pn_Call_T_result, new_Tuple(n_res, res_pred));
1096 set_Tuple_pred(call, pn_Call_T_result, new_Bad());
1098 /* Finally the exception control flow.
1099 We have two (three) possible situations:
1100 First if the Call branches to an exception handler: We need to add a Phi node to
1101 collect the memory containing the exception objects. Further we need
1102 to add another block to get a correct representation of this Phi. To
1103 this block we add a Jmp that resolves into the X output of the Call
1104 when the Call is turned into a tuple.
1105 Second the Call branches to End, the exception is not handled. Just
1106 add all inlined exception branches to the End node.
1107 Third: there is no Exception edge at all. Handle as case two. */
1108 if (exc_handling == 0) {
1110 for (i = 0; i < arity; i++) {
1112 ret = get_irn_n(end_bl, i);
1113 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
1114 cf_pred[n_exc] = ret;
1119 new_Block(n_exc, cf_pred); /* watch it: current_block is changed! */
1120 set_Tuple_pred(call, pn_Call_X_except, new_Jmp());
1121 /* The Phi for the memories with the exception objects */
1123 for (i = 0; i < arity; i++) {
1125 ret = skip_Proj(get_irn_n(end_bl, i));
1127 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 3);
1129 } else if (is_fragile_op(ret)) {
1130 /* We rely that all cfops have the memory output at the same position. */
1131 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 0);
1133 } else if (get_irn_op(ret) == op_Raise) {
1134 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 1);
1138 set_Tuple_pred(call, pn_Call_M_except, new_Phi(n_exc, cf_pred, mode_M));
1140 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1141 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1144 ir_node *main_end_bl;
1145 int main_end_bl_arity;
1146 ir_node **end_preds;
1148 /* assert(exc_handling == 1 || no exceptions. ) */
1150 for (i = 0; i < arity; i++) {
1151 ir_node *ret = get_irn_n(end_bl, i);
1153 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
1154 cf_pred[n_exc] = ret;
1158 main_end_bl = get_irg_end_block(current_ir_graph);
1159 main_end_bl_arity = get_irn_arity(main_end_bl);
1160 end_preds = xmalloc ((n_exc + main_end_bl_arity) * sizeof(*end_preds));
1162 for (i = 0; i < main_end_bl_arity; ++i)
1163 end_preds[i] = get_irn_n(main_end_bl, i);
1164 for (i = 0; i < n_exc; ++i)
1165 end_preds[main_end_bl_arity + i] = cf_pred[i];
1166 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
1167 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1168 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1174 #if 0 /* old. now better, correcter, faster implementation. */
1176 /* -- If the exception control flow from the inlined Call directly
1177 branched to the end block we now have the following control
1178 flow predecessor pattern: ProjX -> Tuple -> Jmp. We must
1179 remove the Jmp along with it's empty block and add Jmp's
1180 predecessors as predecessors of this end block. No problem if
1181 there is no exception, because then branches Bad to End which
1183 @@@ can't we know this beforehand: by getting the Proj(1) from
1184 the Call link list and checking whether it goes to Proj. */
1185 /* find the problematic predecessor of the end block. */
1186 end_bl = get_irg_end_block(current_ir_graph);
1187 for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
1188 cf_op = get_Block_cfgpred(end_bl, i);
1189 if (get_irn_op(cf_op) == op_Proj) {
1190 cf_op = get_Proj_pred(cf_op);
1191 if ((get_irn_op(cf_op) == op_Tuple) && (cf_op == call)) {
1192 /* There are unoptimized tuples from inlineing before when no exc */
1193 assert(get_Proj_proj(get_Block_cfgpred(end_bl, i)) == pn_Call_X_except);
1194 cf_op = get_Tuple_pred(cf_op, pn_Call_X_except);
1195 assert(get_irn_op(cf_op) == op_Jmp);
1201 if (i < get_Block_n_cfgpreds(end_bl)) {
1202 bl = get_nodes_block(cf_op);
1203 arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
1204 cf_pred = xmalloc (arity * sizeof(*cf_pred));
1205 for (j = 0; j < i; j++)
1206 cf_pred[j] = get_Block_cfgpred(end_bl, j);
1207 for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
1208 cf_pred[j] = get_Block_cfgpred(bl, j-i);
1209 for (j = j; j < arity; j++)
1210 cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
1211 set_irn_in(end_bl, arity, cf_pred);
1213 /* Remove the exception pred from post-call Tuple. */
1214 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1219 /* -- Turn CSE back on. -- */
1220 set_optimize(rem_opt);
1225 /********************************************************************/
1226 /* Apply inlineing to small methods. */
1227 /********************************************************************/
1229 /* It makes no sense to inline too many calls in one procedure. Anyways,
1230 I didn't get a version with NEW_ARR_F to run. */
1231 #define MAX_INLINE 1024
1234 * environment for inlining small irgs
1236 typedef struct _inline_env_t {
1238 ir_node *calls[MAX_INLINE];
1242 * Returns the irg called from a Call node. If the irg is not
1243 * known, NULL is returned.
1245 static ir_graph *get_call_called_irg(ir_node *call) {
1247 ir_graph *called_irg = NULL;
1249 assert(is_Call(call));
1251 addr = get_Call_ptr(call);
1252 if ((get_irn_op(addr) == op_SymConst) && (get_SymConst_kind (addr) == symconst_addr_ent)) {
1253 called_irg = get_entity_irg(get_SymConst_entity(addr));
1259 static void collect_calls(ir_node *call, void *env) {
1262 if (! is_Call(call)) return;
1264 addr = get_Call_ptr(call);
1266 if (get_irn_op(addr) == op_SymConst) {
1267 if (get_SymConst_kind(addr) == symconst_addr_ent) {
1268 ir_graph *called_irg = get_entity_irg(get_SymConst_entity(addr));
1269 inline_env_t *ienv = (inline_env_t *)env;
1270 if (called_irg && ienv->pos < MAX_INLINE) {
1271 /* The Call node calls a locally defined method. Remember to inline. */
1272 ienv->calls[ienv->pos++] = call;
1279 * Inlines all small methods at call sites where the called address comes
1280 * from a Const node that references the entity representing the called
1282 * The size argument is a rough measure for the code size of the method:
1283 * Methods where the obstack containing the firm graph is smaller than
1286 void inline_small_irgs(ir_graph *irg, int size) {
1288 ir_graph *rem = current_ir_graph;
1289 inline_env_t env /* = {0, NULL}*/;
1291 if (!(get_opt_optimize() && get_opt_inline())) return;
1293 current_ir_graph = irg;
1294 /* Handle graph state */
1295 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1296 free_callee_info(current_ir_graph);
1298 /* Find Call nodes to inline.
1299 (We can not inline during a walk of the graph, as inlineing the same
1300 method several times changes the visited flag of the walked graph:
1301 after the first inlineing visited of the callee equals visited of
1302 the caller. With the next inlineing both are increased.) */
1304 irg_walk(get_irg_end(irg), NULL, collect_calls, &env);
1306 if ((env.pos > 0) && (env.pos < MAX_INLINE)) {
1307 /* There are calls to inline */
1308 collect_phiprojs(irg);
1309 for (i = 0; i < env.pos; i++) {
1311 callee = get_entity_irg(get_SymConst_entity(get_Call_ptr(env.calls[i])));
1312 if (((_obstack_memory_used(callee->obst) - (int)obstack_room(callee->obst)) < size) ||
1313 (get_irg_inline_property(callee) == irg_inline_forced)) {
1314 inline_method(env.calls[i], callee);
1319 current_ir_graph = rem;
1323 * Environment for inlining irgs.
1326 int n_nodes; /**< Nodes in graph except Id, Tuple, Proj, Start, End */
1327 int n_nodes_orig; /**< for statistics */
1328 eset *call_nodes; /**< All call nodes in this graph */
1330 int n_call_nodes_orig; /**< for statistics */
1331 int n_callers; /**< Number of known graphs that call this graphs. */
1332 int n_callers_orig; /**< for statistics */
1336 * Allocate a new environment for inlining.
1338 static inline_irg_env *new_inline_irg_env(void) {
1339 inline_irg_env *env = xmalloc(sizeof(*env));
1340 env->n_nodes = -2; /* do not count count Start, End */
1341 env->n_nodes_orig = -2; /* do not count Start, End */
1342 env->call_nodes = eset_create();
1343 env->n_call_nodes = 0;
1344 env->n_call_nodes_orig = 0;
1346 env->n_callers_orig = 0;
1351 * destroy an environment for inlining.
1353 static void free_inline_irg_env(inline_irg_env *env) {
1354 eset_destroy(env->call_nodes);
1359 * post-walker: collect all calls in the inline-environment
1360 * of a graph and sum some statistics.
1362 static void collect_calls2(ir_node *call, void *env) {
1363 inline_irg_env *x = (inline_irg_env *)env;
1364 ir_op *op = get_irn_op(call);
1367 /* count meaningful nodes in irg */
1368 if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1373 if (op != op_Call) return;
1375 /* collect all call nodes */
1376 eset_insert(x->call_nodes, call);
1378 x->n_call_nodes_orig++;
1380 /* count all static callers */
1381 callee = get_call_called_irg(call);
1383 inline_irg_env *callee_env = get_irg_link(callee);
1384 callee_env->n_callers++;
1385 callee_env->n_callers_orig++;
1390 * Returns TRUE if the number of callers in 0 in the irg's environment,
1391 * hence this irg is a leave.
1393 INLINE static int is_leave(ir_graph *irg) {
1394 return (((inline_irg_env *)get_irg_link(irg))->n_call_nodes == 0);
1398 * Returns TRUE if the number of callers is smaller size in the irg's environment.
1400 INLINE static int is_smaller(ir_graph *callee, int size) {
1401 return (((inline_irg_env *)get_irg_link(callee))->n_nodes < size);
1406 * Inlines small leave methods at call sites where the called address comes
1407 * from a Const node that references the entity representing the called
1409 * The size argument is a rough measure for the code size of the method:
1410 * Methods where the obstack containing the firm graph is smaller than
1413 void inline_leave_functions(int maxsize, int leavesize, int size) {
1414 inline_irg_env *env;
1415 int i, n_irgs = get_irp_n_irgs();
1416 ir_graph *rem = current_ir_graph;
1419 if (!(get_opt_optimize() && get_opt_inline())) return;
1421 /* extend all irgs by a temporary data structure for inlining. */
1422 for (i = 0; i < n_irgs; ++i)
1423 set_irg_link(get_irp_irg(i), new_inline_irg_env());
1425 /* Precompute information in temporary data structure. */
1426 for (i = 0; i < n_irgs; ++i) {
1427 current_ir_graph = get_irp_irg(i);
1428 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1429 free_callee_info(current_ir_graph);
1431 irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
1432 get_irg_link(current_ir_graph));
1435 /* -- and now inline. -- */
1437 /* Inline leaves recursively -- we might construct new leaves. */
1438 while (did_inline) {
1441 for (i = 0; i < n_irgs; ++i) {
1443 int phiproj_computed = 0;
1445 current_ir_graph = get_irp_irg(i);
1446 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1448 for (call = eset_first(env->call_nodes); call; call = eset_next(env->call_nodes)) {
1451 if (get_irn_op(call) == op_Tuple) continue; /* We already have inlined this call. */
1452 callee = get_call_called_irg(call);
1454 if (env->n_nodes > maxsize) continue; // break;
1456 if (callee && (is_leave(callee) && is_smaller(callee, leavesize))) {
1457 if (!phiproj_computed) {
1458 phiproj_computed = 1;
1459 collect_phiprojs(current_ir_graph);
1461 did_inline = inline_method(call, callee);
1464 /* Do some statistics */
1465 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1466 env->n_call_nodes --;
1467 env->n_nodes += callee_env->n_nodes;
1468 callee_env->n_callers--;
1475 /* inline other small functions. */
1476 for (i = 0; i < n_irgs; ++i) {
1479 int phiproj_computed = 0;
1481 current_ir_graph = get_irp_irg(i);
1482 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1484 /* we can not walk and change a set, nor remove from it.
1486 walkset = env->call_nodes;
1487 env->call_nodes = eset_create();
1488 for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1491 if (get_irn_op(call) == op_Tuple) continue; /* We already inlined. */
1492 callee = get_call_called_irg(call);
1495 ((is_smaller(callee, size) && (env->n_nodes < maxsize)) || /* small function */
1496 (get_irg_inline_property(callee) == irg_inline_forced))) {
1497 if (!phiproj_computed) {
1498 phiproj_computed = 1;
1499 collect_phiprojs(current_ir_graph);
1501 if (inline_method(call, callee)) {
1502 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1503 env->n_call_nodes--;
1504 eset_insert_all(env->call_nodes, callee_env->call_nodes); /* @@@ ??? This are the wrong nodes !? Not the copied ones. */
1505 env->n_call_nodes += callee_env->n_call_nodes;
1506 env->n_nodes += callee_env->n_nodes;
1507 callee_env->n_callers--;
1510 eset_insert(env->call_nodes, call);
1513 eset_destroy(walkset);
1516 for (i = 0; i < n_irgs; ++i) {
1517 current_ir_graph = get_irp_irg(i);
1519 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1520 if ((env->n_call_nodes_orig != env->n_call_nodes) ||
1521 (env->n_callers_orig != env->n_callers))
1522 printf("Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1523 env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1524 env->n_callers_orig, env->n_callers,
1525 get_entity_name(get_irg_entity(current_ir_graph)));
1527 free_inline_irg_env((inline_irg_env *)get_irg_link(current_ir_graph));
1530 current_ir_graph = rem;
1533 /*******************************************************************/
1534 /* Code Placement. Pins all floating nodes to a block where they */
1535 /* will be executed only if needed. */
1536 /*******************************************************************/
1539 * Returns non-zero, is a block is not reachable from Start.
1541 * @param block the block to test
1544 is_Block_unreachable(ir_node *block) {
1545 return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
1549 * Find the earliest correct block for N. --- Place N into the
1550 * same Block as its dominance-deepest Input.
1552 * We have to avoid calls to get_nodes_block() here
1553 * because the graph is floating.
1555 * move_out_of_loops() expects that place_floats_early() have placed
1556 * all "living" nodes into a living block. That's why we must
1557 * move nodes in dead block with "live" successors into a valid
1559 * We move them just into the same block as it's successor (or
1560 * in case of a Phi into the effective use block). For Phi successors,
1561 * this may still be a dead block, but then there is no real use, as
1562 * the control flow will be dead later.
1565 place_floats_early(ir_node *n, pdeq *worklist)
1569 /* we must not run into an infinite loop */
1570 assert(irn_not_visited(n));
1571 mark_irn_visited(n);
1573 /* Place floating nodes. */
1574 if (get_irn_pinned(n) == op_pin_state_floats) {
1575 ir_node *curr_block = get_irn_n(n, -1);
1576 int in_dead_block = is_Block_unreachable(curr_block);
1578 ir_node *b = NULL; /* The block to place this node in */
1580 assert(get_irn_op(n) != op_Block);
1582 if ((get_irn_op(n) == op_Const) ||
1583 (get_irn_op(n) == op_SymConst) ||
1585 (get_irn_op(n) == op_Unknown)) {
1586 /* These nodes will not be placed by the loop below. */
1587 b = get_irg_start_block(current_ir_graph);
1591 /* find the block for this node. */
1592 irn_arity = get_irn_arity(n);
1593 for (i = 0; i < irn_arity; i++) {
1594 ir_node *pred = get_irn_n(n, i);
1595 ir_node *pred_block;
1597 if ((irn_not_visited(pred))
1598 && (get_irn_pinned(pred) == op_pin_state_floats)) {
1601 * If the current node is NOT in a dead block, but one of its
1602 * predecessors is, we must move the predecessor to a live block.
1603 * Such thing can happen, if global CSE chose a node from a dead block.
1604 * We move it simple to our block.
1605 * Note that neither Phi nor End nodes are floating, so we don't
1606 * need to handle them here.
1608 if (! in_dead_block) {
1609 if (get_irn_pinned(pred) == op_pin_state_floats &&
1610 is_Block_unreachable(get_irn_n(pred, -1)))
1611 set_nodes_block(pred, curr_block);
1613 place_floats_early(pred, worklist);
1617 * A node in the Bad block must stay in the bad block,
1618 * so don't compute a new block for it.
1623 /* Because all loops contain at least one op_pin_state_pinned node, now all
1624 our inputs are either op_pin_state_pinned or place_early() has already
1625 been finished on them. We do not have any unfinished inputs! */
1626 pred_block = get_irn_n(pred, -1);
1627 if ((!is_Block_dead(pred_block)) &&
1628 (get_Block_dom_depth(pred_block) > depth)) {
1630 depth = get_Block_dom_depth(pred_block);
1632 /* Avoid that the node is placed in the Start block */
1633 if ((depth == 1) && (get_Block_dom_depth(get_irn_n(n, -1)) > 1)) {
1634 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1635 assert(b != get_irg_start_block(current_ir_graph));
1640 set_nodes_block(n, b);
1644 * Add predecessors of non floating nodes and non-floating predecessors
1645 * of floating nodes to worklist and fix their blocks if the are in dead block.
1647 irn_arity = get_irn_arity(n);
1649 if (get_irn_op(n) == op_End) {
1651 * Simplest case: End node. Predecessors are keep-alives,
1652 * no need to move out of dead block.
1654 for (i = -1; i < irn_arity; ++i) {
1655 ir_node *pred = get_irn_n(n, i);
1656 if (irn_not_visited(pred))
1657 pdeq_putr(worklist, pred);
1660 else if (is_Block(n)) {
1662 * Blocks: Predecessors are control flow, no need to move
1663 * them out of dead block.
1665 for (i = irn_arity - 1; i >= 0; --i) {
1666 ir_node *pred = get_irn_n(n, i);
1667 if (irn_not_visited(pred))
1668 pdeq_putr(worklist, pred);
1671 else if (is_Phi(n)) {
1673 ir_node *curr_block = get_irn_n(n, -1);
1674 int in_dead_block = is_Block_unreachable(curr_block);
1677 * Phi nodes: move nodes from dead blocks into the effective use
1678 * of the Phi-input if the Phi is not in a bad block.
1680 pred = get_irn_n(n, -1);
1681 if (irn_not_visited(pred))
1682 pdeq_putr(worklist, pred);
1684 for (i = irn_arity - 1; i >= 0; --i) {
1685 ir_node *pred = get_irn_n(n, i);
1687 if (irn_not_visited(pred)) {
1688 if (! in_dead_block &&
1689 get_irn_pinned(pred) == op_pin_state_floats &&
1690 is_Block_unreachable(get_irn_n(pred, -1))) {
1691 set_nodes_block(pred, get_Block_cfgpred_block(curr_block, i));
1693 pdeq_putr(worklist, pred);
1699 ir_node *curr_block = get_irn_n(n, -1);
1700 int in_dead_block = is_Block_unreachable(curr_block);
1703 * All other nodes: move nodes from dead blocks into the same block.
1705 pred = get_irn_n(n, -1);
1706 if (irn_not_visited(pred))
1707 pdeq_putr(worklist, pred);
1709 for (i = irn_arity - 1; i >= 0; --i) {
1710 ir_node *pred = get_irn_n(n, i);
1712 if (irn_not_visited(pred)) {
1713 if (! in_dead_block &&
1714 get_irn_pinned(pred) == op_pin_state_floats &&
1715 is_Block_unreachable(get_irn_n(pred, -1))) {
1716 set_nodes_block(pred, curr_block);
1718 pdeq_putr(worklist, pred);
1725 * Floating nodes form subgraphs that begin at nodes as Const, Load,
1726 * Start, Call and that end at op_pin_state_pinned nodes as Store, Call. Place_early
1727 * places all floating nodes reachable from its argument through floating
1728 * nodes and adds all beginnings at op_pin_state_pinned nodes to the worklist.
1730 static INLINE void place_early(pdeq *worklist) {
1732 inc_irg_visited(current_ir_graph);
1734 /* this inits the worklist */
1735 place_floats_early(get_irg_end(current_ir_graph), worklist);
1737 /* Work the content of the worklist. */
1738 while (!pdeq_empty(worklist)) {
1739 ir_node *n = pdeq_getl(worklist);
1740 if (irn_not_visited(n))
1741 place_floats_early(n, worklist);
1744 set_irg_outs_inconsistent(current_ir_graph);
1745 set_irg_pinned(current_ir_graph, op_pin_state_pinned);
1749 * Compute the deepest common ancestor of block and dca.
1751 static ir_node *calc_dca(ir_node *dca, ir_node *block)
1755 /* we do not want to place nodes in dead blocks */
1756 if (is_Block_dead(block))
1759 /* We found a first legal placement. */
1760 if (!dca) return block;
1762 /* Find a placement that is dominates both, dca and block. */
1763 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1764 block = get_Block_idom(block);
1766 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
1767 dca = get_Block_idom(dca);
1770 while (block != dca)
1771 { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1776 /** Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
1777 * I.e., DCA is the block where we might place PRODUCER.
1778 * A data flow edge points from producer to consumer.
1781 consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
1783 ir_node *block = NULL;
1785 /* Compute the latest block into which we can place a node so that it is
1787 if (get_irn_op(consumer) == op_Phi) {
1788 /* our consumer is a Phi-node, the effective use is in all those
1789 blocks through which the Phi-node reaches producer */
1791 ir_node *phi_block = get_nodes_block(consumer);
1792 irn_arity = get_irn_arity(consumer);
1794 for (i = 0; i < irn_arity; i++) {
1795 if (get_irn_n(consumer, i) == producer) {
1796 ir_node *new_block = get_nodes_block(get_Block_cfgpred(phi_block, i));
1798 if (! is_Block_unreachable(new_block))
1799 block = calc_dca(block, new_block);
1804 block = get_irn_n(producer, -1);
1807 assert(is_no_Block(consumer));
1808 block = get_nodes_block(consumer);
1811 /* Compute the deepest common ancestor of block and dca. */
1812 return calc_dca(dca, block);
1815 /* FIXME: the name clashes here with the function from ana/field_temperature.c
1817 static INLINE int get_irn_loop_depth(ir_node *n) {
1818 return get_loop_depth(get_irn_loop(n));
1822 * Move n to a block with less loop depth than it's current block. The
1823 * new block must be dominated by early.
1825 * @param n the node that should be moved
1826 * @param early the earliest block we can n move to
1829 move_out_of_loops (ir_node *n, ir_node *early)
1831 ir_node *best, *dca;
1835 /* Find the region deepest in the dominator tree dominating
1836 dca with the least loop nesting depth, but still dominated
1837 by our early placement. */
1838 dca = get_nodes_block(n);
1841 while (dca != early) {
1842 dca = get_Block_idom(dca);
1843 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
1844 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1848 if (best != get_nodes_block(n)) {
1850 printf("Moving out of loop: "); DDMN(n);
1851 printf(" Outermost block: "); DDMN(early);
1852 printf(" Best block: "); DDMN(best);
1853 printf(" Innermost block: "); DDMN(get_nodes_block(n));
1855 set_nodes_block(n, best);
1860 * Find the latest legal block for N and place N into the
1861 * `optimal' Block between the latest and earliest legal block.
1862 * The `optimal' block is the dominance-deepest block of those
1863 * with the least loop-nesting-depth. This places N out of as many
1864 * loops as possible and then makes it as control dependent as
1868 place_floats_late(ir_node *n, pdeq *worklist)
1873 assert(irn_not_visited(n)); /* no multiple placement */
1875 mark_irn_visited(n);
1877 /* no need to place block nodes, control nodes are already placed. */
1878 if ((get_irn_op(n) != op_Block) &&
1880 (get_irn_mode(n) != mode_X)) {
1881 /* Remember the early_blk placement of this block to move it
1882 out of loop no further than the early_blk placement. */
1883 early_blk = get_irn_n(n, -1);
1886 * BEWARE: Here we also get code, that is live, but
1887 * was in a dead block. If the node is life, but because
1888 * of CSE in a dead block, we still might need it.
1891 /* Assure that our users are all placed, except the Phi-nodes.
1892 --- Each data flow cycle contains at least one Phi-node. We
1893 have to break the `user has to be placed before the
1894 producer' dependence cycle and the Phi-nodes are the
1895 place to do so, because we need to base our placement on the
1896 final region of our users, which is OK with Phi-nodes, as they
1897 are op_pin_state_pinned, and they never have to be placed after a
1898 producer of one of their inputs in the same block anyway. */
1899 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
1900 ir_node *succ = get_irn_out(n, i);
1901 if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
1902 place_floats_late(succ, worklist);
1905 if (! is_Block_dead(early_blk)) {
1906 /* do only move things that where not dead */
1908 /* We have to determine the final block of this node... except for
1910 if ((get_irn_pinned(n) == op_pin_state_floats) &&
1911 (get_irn_op(n) != op_Const) &&
1912 (get_irn_op(n) != op_SymConst)) {
1913 ir_node *dca = NULL; /* deepest common ancestor in the
1914 dominator tree of all nodes'
1915 blocks depending on us; our final
1916 placement has to dominate DCA. */
1917 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
1918 ir_node *succ = get_irn_out(n, i);
1921 if (get_irn_op(succ) == op_End) {
1923 * This consumer is the End node, a keep alive edge.
1924 * This is not a real consumer, so we ignore it
1929 /* ignore if succ is in dead code */
1930 succ_blk = get_irn_n(succ, -1);
1931 if (is_Block_unreachable(succ_blk))
1933 dca = consumer_dom_dca(dca, succ, n);
1936 set_nodes_block(n, dca);
1937 move_out_of_loops(n, early_blk);
1943 /* Add predecessors of all non-floating nodes on list. (Those of floating
1944 nodes are placed already and therefore are marked.) */
1945 for (i = 0; i < get_irn_n_outs(n); i++) {
1946 ir_node *succ = get_irn_out(n, i);
1947 if (irn_not_visited(get_irn_out(n, i))) {
1948 pdeq_putr(worklist, succ);
1953 static INLINE void place_late(pdeq *worklist) {
1955 inc_irg_visited(current_ir_graph);
1957 /* This fills the worklist initially. */
1958 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
1960 /* And now empty the worklist again... */
1961 while (!pdeq_empty(worklist)) {
1962 ir_node *n = pdeq_getl(worklist);
1963 if (irn_not_visited(n))
1964 place_floats_late(n, worklist);
1968 void place_code(ir_graph *irg) {
1970 ir_graph *rem = current_ir_graph;
1972 current_ir_graph = irg;
1974 if (!(get_opt_optimize() && get_opt_global_cse())) return;
1976 /* Handle graph state */
1977 assert(get_irg_phase_state(irg) != phase_building);
1978 if (get_irg_dom_state(irg) != dom_consistent)
1981 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
1982 free_loop_information(irg);
1983 construct_backedges(irg);
1986 /* Place all floating nodes as early as possible. This guarantees
1987 a legal code placement. */
1988 worklist = new_pdeq();
1989 place_early(worklist);
1991 /* place_early() invalidates the outs, place_late needs them. */
1992 compute_irg_outs(irg);
1994 /* Now move the nodes down in the dominator tree. This reduces the
1995 unnecessary executions of the node. */
1996 place_late(worklist);
1998 set_irg_outs_inconsistent(current_ir_graph);
1999 set_irg_loopinfo_inconsistent(current_ir_graph);
2001 current_ir_graph = rem;
2005 * Called by walker of remove_critical_cf_edges().
2007 * Place an empty block to an edge between a blocks of multiple
2008 * predecessors and a block of multiple successors.
2011 * @param env Environment of walker. The changed field.
2013 static void walk_critical_cf_edges(ir_node *n, void *env) {
2015 ir_node *pre, *block, *jmp;
2018 /* Block has multiple predecessors */
2019 if (is_Block(n) && (get_irn_arity(n) > 1)) {
2020 if (n == get_irg_end_block(current_ir_graph))
2021 return; /* No use to add a block here. */
2023 arity = get_irn_arity(n);
2024 for (i=0; i<arity; i++) {
2025 pre = get_irn_n(n, i);
2026 /* Predecessor has multiple successors. Insert new control flow edge. */
2027 if (op_Raise != get_irn_op(skip_Proj(pre))) {
2028 /* set predecessor of new block */
2029 block = new_Block(1, &pre);
2030 /* insert new jmp node to new block */
2031 set_cur_block(block);
2034 /* set successor of new block */
2035 set_irn_n(n, i, jmp);
2037 } /* predecessor has multiple successors */
2038 } /* for all predecessors */
2039 } /* n is a block */
2042 void remove_critical_cf_edges(ir_graph *irg) {
2044 irg_walk_graph(irg, NULL, walk_critical_cf_edges, &changed);
2047 /* control flow changed */
2048 set_irg_outs_inconsistent(irg);
2049 set_irg_extblk_inconsistent(irg);
2050 set_irg_doms_inconsistent(current_ir_graph);
2051 set_irg_loopinfo_inconsistent(current_ir_graph);