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, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 #include "irgraph_t.h"
32 #include "pdeq.h" /* Fuer code placement */
37 #include "irbackedge_t.h"
44 #include "iredges_t.h"
47 /*------------------------------------------------------------------*/
48 /* apply optimizations of iropt to all nodes. */
49 /*------------------------------------------------------------------*/
52 * A wrapper around optimize_inplace_2() to be called from a walker.
54 static void optimize_in_place_wrapper (ir_node *n, void *env) {
55 ir_node *optimized = optimize_in_place_2(n);
56 if (optimized != n) exchange (n, optimized);
60 * Do local optimizations for a node.
62 * @param n the IR-node where to start. Typically the End node
65 * @note current_ir_graph must be set
67 static INLINE void do_local_optimize(ir_node *n) {
68 /* Handle graph state */
69 assert(get_irg_phase_state(current_ir_graph) != phase_building);
71 if (get_opt_global_cse())
72 set_irg_pinned(current_ir_graph, op_pin_state_floats);
73 set_irg_outs_inconsistent(current_ir_graph);
74 set_irg_doms_inconsistent(current_ir_graph);
75 set_irg_loopinfo_inconsistent(current_ir_graph);
77 /* Clean the value_table in irg for the CSE. */
78 del_identities(current_ir_graph->value_table);
79 current_ir_graph->value_table = new_identities();
81 /* walk over the graph */
82 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
85 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
86 void local_optimize_node(ir_node *n) {
87 ir_graph *rem = current_ir_graph;
88 current_ir_graph = get_irn_irg(n);
92 current_ir_graph = rem;
96 * Block-Walker: uses dominance depth to mark dead blocks.
98 static void kill_dead_blocks(ir_node *block, void *env)
100 if (get_Block_dom_depth(block) < 0) {
102 * Note that the new dominance code correctly handles
103 * the End block, i.e. it is always reachable from Start
105 set_Block_dead(block);
109 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
110 void local_optimize_graph(ir_graph *irg) {
111 ir_graph *rem = current_ir_graph;
112 current_ir_graph = irg;
114 if (get_irg_dom_state(irg) == dom_consistent)
115 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
117 do_local_optimize(get_irg_end(irg));
119 current_ir_graph = rem;
123 * Enqueue all users of a node to a wait queue.
124 * Handles mode_T nodes.
126 static void enqueue_users(ir_node *n, pdeq *waitq) {
127 const ir_edge_t *edge;
129 foreach_out_edge(n, edge) {
130 ir_node *succ = get_edge_src_irn(edge);
132 if (get_irn_link(succ) != waitq) {
133 pdeq_putr(waitq, succ);
134 set_irn_link(succ, waitq);
136 if (get_irn_mode(succ) == mode_T) {
137 /* A mode_T node has Proj's. Because most optimizations
138 run on the Proj's we have to enqueue them also. */
139 enqueue_users(succ, waitq);
145 * Data flow optimization walker.
146 * Optimizes all nodes and enqueue it's users
149 static void opt_walker(ir_node *n, void *env) {
153 optimized = optimize_in_place_2(n);
154 set_irn_link(optimized, NULL);
156 if (optimized != n) {
157 enqueue_users(n, waitq);
158 exchange(n, optimized);
162 /* Applies local optimizations to all nodes in the graph until fixpoint. */
163 void optimize_graph_df(ir_graph *irg) {
164 pdeq *waitq = new_pdeq();
165 int state = edges_activated(irg);
166 ir_graph *rem = current_ir_graph;
168 current_ir_graph = irg;
173 if (get_opt_global_cse())
174 set_irg_pinned(current_ir_graph, op_pin_state_floats);
176 /* Clean the value_table in irg for the CSE. */
177 del_identities(irg->value_table);
178 irg->value_table = new_identities();
180 if (get_irg_dom_state(irg) == dom_consistent)
181 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
183 /* invalidate info */
184 set_irg_outs_inconsistent(irg);
185 set_irg_doms_inconsistent(irg);
186 set_irg_loopinfo_inconsistent(irg);
188 /* walk over the graph */
189 irg_walk_graph(irg, NULL, opt_walker, waitq);
191 /* finish the wait queue */
192 while (! pdeq_empty(waitq)) {
193 ir_node *n = pdeq_getl(waitq);
195 opt_walker(n, waitq);
201 edges_deactivate(irg);
203 current_ir_graph = rem;
207 /*------------------------------------------------------------------*/
208 /* Routines for dead node elimination / copying garbage collection */
209 /* of the obstack. */
210 /*------------------------------------------------------------------*/
213 * Remember the new node in the old node by using a field all nodes have.
215 #define set_new_node(oldn, newn) set_irn_link(oldn, newn)
218 * Get this new node, before the old node is forgotten.
220 #define get_new_node(oldn) get_irn_link(oldn)
223 * Check if a new node was set.
225 #define has_new_node(n) (get_new_node(n) != NULL)
228 * We use the block_visited flag to mark that we have computed the
229 * number of useful predecessors for this block.
230 * Further we encode the new arity in this flag in the old blocks.
231 * Remembering the arity is useful, as it saves a lot of pointer
232 * accesses. This function is called for all Phi and Block nodes
236 compute_new_arity(ir_node *b) {
237 int i, res, irn_arity;
240 irg_v = get_irg_block_visited(current_ir_graph);
241 block_v = get_Block_block_visited(b);
242 if (block_v >= irg_v) {
243 /* we computed the number of preds for this block and saved it in the
245 return block_v - irg_v;
247 /* compute the number of good predecessors */
248 res = irn_arity = get_irn_arity(b);
249 for (i = 0; i < irn_arity; i++)
250 if (get_irn_opcode(get_irn_n(b, i)) == iro_Bad) res--;
251 /* save it in the flag. */
252 set_Block_block_visited(b, irg_v + res);
258 * Copies the node to the new obstack. The Ins of the new node point to
259 * the predecessors on the old obstack. For block/phi nodes not all
260 * predecessors might be copied. n->link points to the new node.
261 * For Phi and Block nodes the function allocates in-arrays with an arity
262 * only for useful predecessors. The arity is determined by counting
263 * the non-bad predecessors of the block.
265 * @param n The node to be copied
266 * @param env if non-NULL, the node number attribute will be copied to the new node
268 * Note: Also used for loop unrolling.
270 static void copy_node(ir_node *n, void *env) {
273 ir_op *op = get_irn_op(n);
275 /* The end node looses it's flexible in array. This doesn't matter,
276 as dead node elimination builds End by hand, inlineing doesn't use
278 /* assert(op == op_End || ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC)); */
281 /* node copied already */
283 } else if (op == op_Block) {
285 new_arity = compute_new_arity(n);
286 n->attr.block.graph_arr = NULL;
288 block = get_nodes_block(n);
290 new_arity = compute_new_arity(block);
292 new_arity = get_irn_arity(n);
295 nn = new_ir_node(get_irn_dbg_info(n),
302 /* Copy the attributes. These might point to additional data. If this
303 was allocated on the old obstack the pointers now are dangling. This
304 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
305 copy_node_attr(n, nn);
309 int copy_node_nr = env != NULL;
311 /* for easier debugging, we want to copy the node numbers too */
312 nn->node_nr = n->node_nr;
318 hook_dead_node_elim_subst(current_ir_graph, n, nn);
322 * Copies new predecessors of old node to new node remembered in link.
323 * Spare the Bad predecessors of Phi and Block nodes.
326 copy_preds(ir_node *n, void *env) {
330 nn = get_new_node(n);
332 /* printf("\n old node: "); DDMSG2(n);
333 printf(" new node: "); DDMSG2(nn);
334 printf(" arities: old: %d, new: %d\n", get_irn_arity(n), get_irn_arity(nn)); */
337 /* Don't copy Bad nodes. */
339 irn_arity = get_irn_arity(n);
340 for (i = 0; i < irn_arity; i++)
341 if (! is_Bad(get_irn_n(n, i))) {
342 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
343 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
346 /* repair the block visited flag from above misuse. Repair it in both
347 graphs so that the old one can still be used. */
348 set_Block_block_visited(nn, 0);
349 set_Block_block_visited(n, 0);
350 /* Local optimization could not merge two subsequent blocks if
351 in array contained Bads. Now it's possible.
352 We don't call optimize_in_place as it requires
353 that the fields in ir_graph are set properly. */
354 if ((get_opt_control_flow_straightening()) &&
355 (get_Block_n_cfgpreds(nn) == 1) &&
356 (get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp)) {
357 ir_node *old = get_nodes_block(get_Block_cfgpred(nn, 0));
359 /* Jmp jumps into the block it is in -- deal self cycle. */
360 assert(is_Bad(get_new_node(get_irg_bad(current_ir_graph))));
361 exchange(nn, get_new_node(get_irg_bad(current_ir_graph)));
366 } else if (get_irn_op(n) == op_Phi) {
367 /* Don't copy node if corresponding predecessor in block is Bad.
368 The Block itself should not be Bad. */
369 block = get_nodes_block(n);
370 set_irn_n(nn, -1, get_new_node(block));
372 irn_arity = get_irn_arity(n);
373 for (i = 0; i < irn_arity; i++)
374 if (! is_Bad(get_irn_n(block, i))) {
375 set_irn_n(nn, j, get_new_node(get_irn_n(n, i)));
376 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
379 /* If the pre walker reached this Phi after the post walker visited the
380 block block_visited is > 0. */
381 set_Block_block_visited(get_nodes_block(n), 0);
382 /* Compacting the Phi's ins might generate Phis with only one
384 if (get_irn_arity(nn) == 1)
385 exchange(nn, get_irn_n(nn, 0));
387 irn_arity = get_irn_arity(n);
388 for (i = -1; i < irn_arity; i++)
389 set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
391 /* Now the new node is complete. We can add it to the hash table for CSE.
392 @@@ inlining aborts if we identify End. Why? */
393 if (get_irn_op(nn) != op_End)
394 add_identities(current_ir_graph->value_table, nn);
398 * Copies the graph recursively, compacts the keep-alives of the end node.
400 * @param irg the graph to be copied
401 * @param copy_node_nr If non-zero, the node number will be copied
403 static void copy_graph(ir_graph *irg, int copy_node_nr) {
404 ir_node *oe, *ne, *ob, *nb, *om, *nm; /* old end, new end, old bad, new bad, old NoMem, new NoMem */
405 ir_node *ka; /* keep alive */
409 /* Some nodes must be copied by hand, sigh */
410 vfl = get_irg_visited(irg);
411 set_irg_visited(irg, vfl + 1);
413 oe = get_irg_end(irg);
414 mark_irn_visited(oe);
415 /* copy the end node by hand, allocate dynamic in array! */
416 ne = new_ir_node(get_irn_dbg_info(oe),
423 /* Copy the attributes. Well, there might be some in the future... */
424 copy_node_attr(oe, ne);
425 set_new_node(oe, ne);
427 /* copy the Bad node */
428 ob = get_irg_bad(irg);
429 mark_irn_visited(ob);
430 nb = new_ir_node(get_irn_dbg_info(ob),
437 copy_node_attr(ob, nb);
438 set_new_node(ob, nb);
440 /* copy the NoMem node */
441 om = get_irg_no_mem(irg);
442 mark_irn_visited(om);
443 nm = new_ir_node(get_irn_dbg_info(om),
450 copy_node_attr(om, nm);
451 set_new_node(om, nm);
453 /* copy the live nodes */
454 set_irg_visited(irg, vfl);
455 irg_walk(get_nodes_block(oe), copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
457 /* Note: from yet, the visited flag of the graph is equal to vfl + 1 */
459 /* visit the anchors as well */
460 for (i = anchor_max - 1; i >= 0; --i) {
461 ir_node *n = irg->anchors[i];
463 if (n && (get_irn_visited(n) <= vfl)) {
464 set_irg_visited(irg, vfl);
465 irg_walk(n, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
469 /* copy_preds for the end node ... */
470 set_nodes_block(ne, get_new_node(get_nodes_block(oe)));
472 /*- ... and now the keep alives. -*/
473 /* First pick the not marked block nodes and walk them. We must pick these
474 first as else we will oversee blocks reachable from Phis. */
475 irn_arity = get_End_n_keepalives(oe);
476 for (i = 0; i < irn_arity; i++) {
477 ka = get_End_keepalive(oe, i);
479 if (get_irn_visited(ka) <= vfl) {
480 /* We must keep the block alive and copy everything reachable */
481 set_irg_visited(irg, vfl);
482 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
484 add_End_keepalive(ne, get_new_node(ka));
488 /* Now pick other nodes. Here we will keep all! */
489 irn_arity = get_End_n_keepalives(oe);
490 for (i = 0; i < irn_arity; i++) {
491 ka = get_End_keepalive(oe, i);
493 if (get_irn_visited(ka) <= vfl) {
494 /* We didn't copy the node yet. */
495 set_irg_visited(irg, vfl);
496 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
498 add_End_keepalive(ne, get_new_node(ka));
502 /* start block sometimes only reached after keep alives */
503 set_nodes_block(nb, get_new_node(get_nodes_block(ob)));
504 set_nodes_block(nm, get_new_node(get_nodes_block(om)));
508 * Copies the graph reachable from current_ir_graph->end to the obstack
509 * in current_ir_graph and fixes the environment.
510 * Then fixes the fields in current_ir_graph containing nodes of the
513 * @param copy_node_nr If non-zero, the node number will be copied
516 copy_graph_env(int copy_node_nr) {
517 ir_graph *irg = current_ir_graph;
518 ir_node *old_end, *n;
521 /* remove end_except and end_reg nodes */
522 old_end = get_irg_end(irg);
523 set_irg_end_except (irg, old_end);
524 set_irg_end_reg (irg, old_end);
526 /* Not all nodes remembered in irg might be reachable
527 from the end node. Assure their link is set to NULL, so that
528 we can test whether new nodes have been computed. */
529 for (i = anchor_max - 1; i >= 0; --i) {
531 set_new_node(irg->anchors[i], NULL);
533 /* we use the block walk flag for removing Bads from Blocks ins. */
534 inc_irg_block_visited(irg);
537 copy_graph(irg, copy_node_nr);
539 /* fix the fields in irg */
540 old_end = get_irg_end(irg);
541 for (i = anchor_max - 1; i >= 0; --i) {
544 irg->anchors[i] = get_new_node(n);
550 * Copies all reachable nodes to a new obstack. Removes bad inputs
551 * from block nodes and the corresponding inputs from Phi nodes.
552 * Merges single exit blocks with single entry blocks and removes
554 * Adds all new nodes to a new hash table for CSE. Does not
555 * perform CSE, so the hash table might contain common subexpressions.
558 dead_node_elimination(ir_graph *irg) {
559 if (get_opt_optimize() && get_opt_dead_node_elimination()) {
561 int rem_ipview = get_interprocedural_view();
562 struct obstack *graveyard_obst = NULL;
563 struct obstack *rebirth_obst = NULL;
564 assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
566 /* inform statistics that we started a dead-node elimination run */
567 hook_dead_node_elim(irg, 1);
569 /* Remember external state of current_ir_graph. */
570 rem = current_ir_graph;
571 current_ir_graph = irg;
572 set_interprocedural_view(0);
574 assert(get_irg_phase_state(irg) != phase_building);
576 /* Handle graph state */
577 free_callee_info(irg);
581 /* @@@ so far we loose loops when copying */
582 free_loop_information(irg);
584 set_irg_doms_inconsistent(irg);
586 /* A quiet place, where the old obstack can rest in peace,
587 until it will be cremated. */
588 graveyard_obst = irg->obst;
590 /* A new obstack, where the reachable nodes will be copied to. */
591 rebirth_obst = xmalloc(sizeof(*rebirth_obst));
592 irg->obst = rebirth_obst;
593 obstack_init(irg->obst);
594 irg->last_node_idx = 0;
596 /* We also need a new value table for CSE */
597 del_identities(irg->value_table);
598 irg->value_table = new_identities();
600 /* Copy the graph from the old to the new obstack */
601 copy_graph_env(/*copy_node_nr=*/1);
603 /* Free memory from old unoptimized obstack */
604 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
605 xfree (graveyard_obst); /* ... then free it. */
607 /* inform statistics that the run is over */
608 hook_dead_node_elim(irg, 0);
610 current_ir_graph = rem;
611 set_interprocedural_view(rem_ipview);
616 * Relink bad predecessors of a block and store the old in array to the
617 * link field. This function is called by relink_bad_predecessors().
618 * The array of link field starts with the block operand at position 0.
619 * If block has bad predecessors, create a new in array without bad preds.
620 * Otherwise let in array untouched.
622 static void relink_bad_block_predecessors(ir_node *n, void *env) {
623 ir_node **new_in, *irn;
624 int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
626 /* if link field of block is NULL, look for bad predecessors otherwise
627 this is already done */
628 if (get_irn_op(n) == op_Block &&
629 get_irn_link(n) == NULL) {
631 /* save old predecessors in link field (position 0 is the block operand)*/
632 set_irn_link(n, get_irn_in(n));
634 /* count predecessors without bad nodes */
635 old_irn_arity = get_irn_arity(n);
636 for (i = 0; i < old_irn_arity; i++)
637 if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
639 /* arity changing: set new predecessors without bad nodes */
640 if (new_irn_arity < old_irn_arity) {
641 /* Get new predecessor array. We do not resize the array, as we must
642 keep the old one to update Phis. */
643 new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
645 /* set new predecessors in array */
648 for (i = 0; i < old_irn_arity; i++) {
649 irn = get_irn_n(n, i);
651 new_in[new_irn_n] = irn;
652 is_backedge(n, i) ? set_backedge(n, new_irn_n-1) : set_not_backedge(n, new_irn_n-1);
656 //ARR_SETLEN(int, n->attr.block.backedge, new_irn_arity);
657 ARR_SHRINKLEN(n->attr.block.backedge, new_irn_arity);
660 } /* ir node has bad predecessors */
662 } /* Block is not relinked */
666 * Relinks Bad predecessors from Blocks and Phis called by walker
667 * remove_bad_predecesors(). If n is a Block, call
668 * relink_bad_block_redecessors(). If n is a Phi-node, call also the relinking
669 * function of Phi's Block. If this block has bad predecessors, relink preds
672 static void relink_bad_predecessors(ir_node *n, void *env) {
673 ir_node *block, **old_in;
674 int i, old_irn_arity, new_irn_arity;
676 /* relink bad predecessors of a block */
677 if (get_irn_op(n) == op_Block)
678 relink_bad_block_predecessors(n, env);
680 /* If Phi node relink its block and its predecessors */
681 if (get_irn_op(n) == op_Phi) {
683 /* Relink predecessors of phi's block */
684 block = get_nodes_block(n);
685 if (get_irn_link(block) == NULL)
686 relink_bad_block_predecessors(block, env);
688 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
689 old_irn_arity = ARR_LEN(old_in);
691 /* Relink Phi predecessors if count of predecessors changed */
692 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
693 /* set new predecessors in array
694 n->in[0] remains the same block */
696 for(i = 1; i < old_irn_arity; i++)
697 if (!is_Bad((ir_node *)old_in[i])) {
698 n->in[new_irn_arity] = n->in[i];
699 is_backedge(n, i) ? set_backedge(n, new_irn_arity) : set_not_backedge(n, new_irn_arity);
703 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
704 ARR_SETLEN(int, n->attr.phi_backedge, new_irn_arity);
707 } /* n is a Phi node */
711 * Removes Bad Bad predecessors from Blocks and the corresponding
712 * inputs to Phi nodes as in dead_node_elimination but without
714 * On walking up set the link field to NULL, on walking down call
715 * relink_bad_predecessors() (This function stores the old in array
716 * to the link field and sets a new in array if arity of predecessors
719 void remove_bad_predecessors(ir_graph *irg) {
720 irg_walk_graph(irg, firm_clear_link, relink_bad_predecessors, NULL);
727 __)|_| | \_/ | \_/(/_ |_/\__|__
729 The following stuff implements a facility that automatically patches
730 registered ir_node pointers to the new node when a dead node elimination occurs.
733 struct _survive_dce_t {
737 hook_entry_t dead_node_elim;
738 hook_entry_t dead_node_elim_subst;
741 typedef struct _survive_dce_list_t {
742 struct _survive_dce_list_t *next;
744 } survive_dce_list_t;
746 static void dead_node_hook(void *context, ir_graph *irg, int start)
748 survive_dce_t *sd = context;
750 /* Create a new map before the dead node elimination is performed. */
752 sd->new_places = pmap_create_ex(pmap_count(sd->places));
755 /* Patch back all nodes if dead node elimination is over and something is to be done. */
757 pmap_destroy(sd->places);
758 sd->places = sd->new_places;
759 sd->new_places = NULL;
764 * Hook called when dead node elimination replaces old by nw.
766 static void dead_node_subst_hook(void *context, ir_graph *irg, ir_node *old, ir_node *nw)
768 survive_dce_t *sd = context;
769 survive_dce_list_t *list = pmap_get(sd->places, old);
771 /* If the node is to be patched back, write the new address to all registered locations. */
773 survive_dce_list_t *p;
775 for(p = list; p; p = p->next)
778 pmap_insert(sd->new_places, nw, list);
783 * Make a new Survive DCE environment.
785 survive_dce_t *new_survive_dce(void)
787 survive_dce_t *res = xmalloc(sizeof(res[0]));
788 obstack_init(&res->obst);
789 res->places = pmap_create();
790 res->new_places = NULL;
792 res->dead_node_elim.hook._hook_dead_node_elim = dead_node_hook;
793 res->dead_node_elim.context = res;
794 res->dead_node_elim.next = NULL;
796 res->dead_node_elim_subst.hook._hook_dead_node_elim_subst = dead_node_subst_hook;
797 res->dead_node_elim_subst.context = res;
798 res->dead_node_elim_subst.next = NULL;
800 register_hook(hook_dead_node_elim, &res->dead_node_elim);
801 register_hook(hook_dead_node_elim_subst, &res->dead_node_elim_subst);
806 * Free a Survive DCE environment.
808 void free_survive_dce(survive_dce_t *sd)
810 obstack_free(&sd->obst, NULL);
811 pmap_destroy(sd->places);
812 unregister_hook(hook_dead_node_elim, &sd->dead_node_elim);
813 unregister_hook(hook_dead_node_elim_subst, &sd->dead_node_elim_subst);
818 * Register a node pointer to be patched upon DCE.
819 * When DCE occurs, the node pointer specified by @p place will be
820 * patched to the new address of the node it is pointing to.
822 * @param sd The Survive DCE environment.
823 * @param place The address of the node pointer.
825 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place)
828 ir_node *irn = *place;
829 survive_dce_list_t *curr = pmap_get(sd->places, irn);
830 survive_dce_list_t *nw = obstack_alloc(&sd->obst, sizeof(nw[0]));
835 pmap_insert(sd->places, irn, nw);
839 /*--------------------------------------------------------------------*/
840 /* Functionality for inlining */
841 /*--------------------------------------------------------------------*/
844 * Copy node for inlineing. Updates attributes that change when
845 * inlineing but not for dead node elimination.
847 * Copies the node by calling copy_node() and then updates the entity if
848 * it's a local one. env must be a pointer of the frame type of the
849 * inlined procedure. The new entities must be in the link field of
853 copy_node_inline (ir_node *n, void *env) {
855 ir_type *frame_tp = (ir_type *)env;
858 if (get_irn_op(n) == op_Sel) {
859 nn = get_new_node (n);
861 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
862 set_Sel_entity(nn, get_entity_link(get_Sel_entity(n)));
864 } else if (get_irn_op(n) == op_Block) {
865 nn = get_new_node (n);
866 nn->attr.block.irg = current_ir_graph;
871 * Walker: checks if P_value_arg_base is used.
873 static void find_addr(ir_node *node, void *env) {
874 int *allow_inline = env;
875 if (is_Proj(node) && get_irn_op(get_Proj_pred(node)) == op_Start) {
876 if (get_Proj_proj(node) == pn_Start_P_value_arg_base)
882 * currently, we cannot inline two cases:
883 * - call with compound arguments
884 * - graphs that take the address of a parameter
886 * check these conditions here
888 static int can_inline(ir_node *call, ir_graph *called_graph)
890 ir_type *call_type = get_Call_type(call);
891 int params, ress, i, res;
892 assert(is_Method_type(call_type));
894 params = get_method_n_params(call_type);
895 ress = get_method_n_ress(call_type);
897 /* check parameters for compound arguments */
898 for (i = 0; i < params; ++i) {
899 ir_type *p_type = get_method_param_type(call_type, i);
901 if (is_compound_type(p_type))
905 /* check results for compound arguments */
906 for (i = 0; i < ress; ++i) {
907 ir_type *r_type = get_method_res_type(call_type, i);
909 if (is_compound_type(r_type))
914 irg_walk_graph(called_graph, find_addr, NULL, &res);
919 /* Inlines a method at the given call site. */
920 int inline_method(ir_node *call, ir_graph *called_graph) {
922 ir_node *post_call, *post_bl;
923 ir_node *in[pn_Start_max];
924 ir_node *end, *end_bl;
928 int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
930 ir_type *called_frame;
931 irg_inline_property prop = get_irg_inline_property(called_graph);
933 if ( (prop < irg_inline_forced) &&
934 (!get_opt_optimize() || !get_opt_inline() || (prop == irg_inline_forbidden))) return 0;
936 /* Do not inline variadic functions. */
937 if (get_method_variadicity(get_entity_type(get_irg_entity(called_graph))) == variadicity_variadic)
940 assert(get_method_n_params(get_entity_type(get_irg_entity(called_graph))) ==
941 get_method_n_params(get_Call_type(call)));
944 * currently, we cannot inline two cases:
945 * - call with compound arguments
946 * - graphs that take the address of a parameter
948 if (! can_inline(call, called_graph))
951 /* -- Turn off optimizations, this can cause problems when allocating new nodes. -- */
952 rem_opt = get_opt_optimize();
955 /* Handle graph state */
956 assert(get_irg_phase_state(current_ir_graph) != phase_building);
957 assert(get_irg_pinned(current_ir_graph) == op_pin_state_pinned);
958 assert(get_irg_pinned(called_graph) == op_pin_state_pinned);
959 set_irg_outs_inconsistent(current_ir_graph);
960 set_irg_extblk_inconsistent(current_ir_graph);
961 set_irg_doms_inconsistent(current_ir_graph);
962 set_irg_loopinfo_inconsistent(current_ir_graph);
963 set_irg_callee_info_state(current_ir_graph, irg_callee_info_inconsistent);
965 /* -- Check preconditions -- */
966 assert(is_Call(call));
967 /* @@@ does not work for InterfaceIII.java after cgana
968 assert(get_Call_type(call) == get_entity_type(get_irg_entity(called_graph)));
969 assert(smaller_type(get_entity_type(get_irg_entity(called_graph)),
970 get_Call_type(call)));
972 if (called_graph == current_ir_graph) {
973 set_optimize(rem_opt);
977 /* here we know we WILL inline, so inform the statistics */
978 hook_inline(call, called_graph);
980 /* -- Decide how to handle exception control flow: Is there a handler
981 for the Call node, or do we branch directly to End on an exception?
983 0 There is a handler.
985 2 Exception handling not represented in Firm. -- */
987 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
988 for (proj = get_irn_link(call); proj; proj = get_irn_link(proj)) {
989 assert(is_Proj(proj));
990 if (get_Proj_proj(proj) == pn_Call_X_except) Xproj = proj;
991 if (get_Proj_proj(proj) == pn_Call_M_except) Mproj = proj;
993 if (Mproj) { assert(Xproj); exc_handling = 0; } /* Mproj */
994 else if (Xproj) { exc_handling = 1; } /* !Mproj && Xproj */
995 else { exc_handling = 2; } /* !Mproj && !Xproj */
1000 the procedure and later replaces the Start node of the called graph.
1001 Post_call is the old Call node and collects the results of the called
1002 graph. Both will end up being a tuple. -- */
1003 post_bl = get_nodes_block(call);
1004 set_irg_current_block(current_ir_graph, post_bl);
1005 /* XxMxPxPxPxT of Start + parameter of Call */
1006 in[pn_Start_X_initial_exec] = new_Jmp();
1007 in[pn_Start_M] = get_Call_mem(call);
1008 in[pn_Start_P_frame_base] = get_irg_frame(current_ir_graph);
1009 in[pn_Start_P_globals] = get_irg_globals(current_ir_graph);
1010 in[pn_Start_P_tls] = get_irg_tls(current_ir_graph);
1011 in[pn_Start_T_args] = new_Tuple(get_Call_n_params(call), get_Call_param_arr(call));
1012 /* in[pn_Start_P_value_arg_base] = ??? */
1013 assert(pn_Start_P_value_arg_base == pn_Start_max - 1 && "pn_Start_P_value_arg_base not supported, fix");
1014 pre_call = new_Tuple(pn_Start_max - 1, in);
1018 The new block gets the ins of the old block, pre_call and all its
1019 predecessors and all Phi nodes. -- */
1020 part_block(pre_call);
1022 /* -- Prepare state for dead node elimination -- */
1023 /* Visited flags in calling irg must be >= flag in called irg.
1024 Else walker and arity computation will not work. */
1025 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
1026 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
1027 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
1028 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
1029 /* Set pre_call as new Start node in link field of the start node of
1030 calling graph and pre_calls block as new block for the start block
1032 Further mark these nodes so that they are not visited by the
1034 set_irn_link(get_irg_start(called_graph), pre_call);
1035 set_irn_visited(get_irg_start(called_graph), get_irg_visited(current_ir_graph));
1036 set_irn_link(get_irg_start_block(called_graph), get_nodes_block(pre_call));
1037 set_irn_visited(get_irg_start_block(called_graph), get_irg_visited(current_ir_graph));
1038 set_irn_link(get_irg_bad(called_graph), get_irg_bad(current_ir_graph));
1039 set_irn_visited(get_irg_bad(called_graph), get_irg_visited(current_ir_graph));
1041 /* Initialize for compaction of in arrays */
1042 inc_irg_block_visited(current_ir_graph);
1044 /* -- Replicate local entities of the called_graph -- */
1045 /* copy the entities. */
1046 called_frame = get_irg_frame_type(called_graph);
1047 for (i = 0; i < get_class_n_members(called_frame); i++) {
1048 ir_entity *new_ent, *old_ent;
1049 old_ent = get_class_member(called_frame, i);
1050 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
1051 set_entity_link(old_ent, new_ent);
1054 /* visited is > than that of called graph. With this trick visited will
1055 remain unchanged so that an outer walker, e.g., searching the call nodes
1056 to inline, calling this inline will not visit the inlined nodes. */
1057 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
1059 /* -- Performing dead node elimination inlines the graph -- */
1060 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
1062 /* @@@ endless loops are not copied!! -- they should be, I think... */
1063 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
1064 get_irg_frame_type(called_graph));
1066 /* Repair called_graph */
1067 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
1068 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
1069 set_Block_block_visited(get_irg_start_block(called_graph), 0);
1071 /* -- Merge the end of the inlined procedure with the call site -- */
1072 /* We will turn the old Call node into a Tuple with the following
1075 0: Phi of all Memories of Return statements.
1076 1: Jmp from new Block that merges the control flow from all exception
1077 predecessors of the old end block.
1078 2: Tuple of all arguments.
1079 3: Phi of Exception memories.
1080 In case the old Call directly branches to End on an exception we don't
1081 need the block merging all exceptions nor the Phi of the exception
1085 /* -- Precompute some values -- */
1086 end_bl = get_new_node(get_irg_end_block(called_graph));
1087 end = get_new_node(get_irg_end(called_graph));
1088 arity = get_irn_arity(end_bl); /* arity = n_exc + n_ret */
1089 n_res = get_method_n_ress(get_Call_type(call));
1091 res_pred = xmalloc (n_res * sizeof(*res_pred));
1092 cf_pred = xmalloc (arity * sizeof(*res_pred));
1094 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
1096 /* -- archive keepalives -- */
1097 irn_arity = get_irn_arity(end);
1098 for (i = 0; i < irn_arity; i++)
1099 add_End_keepalive(get_irg_end(current_ir_graph), get_irn_n(end, i));
1101 /* The new end node will die. We need not free as the in array is on the obstack:
1102 copy_node() only generated 'D' arrays. */
1104 /* -- Replace Return nodes by Jump nodes. -- */
1106 for (i = 0; i < arity; i++) {
1108 ret = get_irn_n(end_bl, i);
1109 if (is_Return(ret)) {
1110 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_block(ret));
1114 set_irn_in(post_bl, n_ret, cf_pred);
1116 /* -- Build a Tuple for all results of the method.
1117 Add Phi node if there was more than one Return. -- */
1118 turn_into_tuple(post_call, 4);
1119 /* First the Memory-Phi */
1121 for (i = 0; i < arity; i++) {
1122 ret = get_irn_n(end_bl, i);
1123 if (is_Return(ret)) {
1124 cf_pred[n_ret] = get_Return_mem(ret);
1128 phi = new_Phi(n_ret, cf_pred, mode_M);
1129 set_Tuple_pred(call, pn_Call_M_regular, phi);
1130 /* Conserve Phi-list for further inlinings -- but might be optimized */
1131 if (get_nodes_block(phi) == post_bl) {
1132 set_irn_link(phi, get_irn_link(post_bl));
1133 set_irn_link(post_bl, phi);
1135 /* Now the real results */
1137 for (j = 0; j < n_res; j++) {
1139 for (i = 0; i < arity; i++) {
1140 ret = get_irn_n(end_bl, i);
1141 if (get_irn_op(ret) == op_Return) {
1142 cf_pred[n_ret] = get_Return_res(ret, j);
1147 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
1151 /* Conserve Phi-list for further inlinings -- but might be optimized */
1152 if (get_nodes_block(phi) == post_bl) {
1153 set_irn_link(phi, get_irn_link(post_bl));
1154 set_irn_link(post_bl, phi);
1157 set_Tuple_pred(call, pn_Call_T_result, new_Tuple(n_res, res_pred));
1159 set_Tuple_pred(call, pn_Call_T_result, new_Bad());
1161 /* Finally the exception control flow.
1162 We have two (three) possible situations:
1163 First if the Call branches to an exception handler: We need to add a Phi node to
1164 collect the memory containing the exception objects. Further we need
1165 to add another block to get a correct representation of this Phi. To
1166 this block we add a Jmp that resolves into the X output of the Call
1167 when the Call is turned into a tuple.
1168 Second the Call branches to End, the exception is not handled. Just
1169 add all inlined exception branches to the End node.
1170 Third: there is no Exception edge at all. Handle as case two. */
1171 if (exc_handling == 0) {
1173 for (i = 0; i < arity; i++) {
1175 ret = get_irn_n(end_bl, i);
1176 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
1177 cf_pred[n_exc] = ret;
1182 new_Block(n_exc, cf_pred); /* watch it: current_block is changed! */
1183 set_Tuple_pred(call, pn_Call_X_except, new_Jmp());
1184 /* The Phi for the memories with the exception objects */
1186 for (i = 0; i < arity; i++) {
1188 ret = skip_Proj(get_irn_n(end_bl, i));
1190 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 3);
1192 } else if (is_fragile_op(ret)) {
1193 /* We rely that all cfops have the memory output at the same position. */
1194 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 0);
1196 } else if (get_irn_op(ret) == op_Raise) {
1197 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 1);
1201 set_Tuple_pred(call, pn_Call_M_except, new_Phi(n_exc, cf_pred, mode_M));
1203 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1204 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1207 ir_node *main_end_bl;
1208 int main_end_bl_arity;
1209 ir_node **end_preds;
1211 /* assert(exc_handling == 1 || no exceptions. ) */
1213 for (i = 0; i < arity; i++) {
1214 ir_node *ret = get_irn_n(end_bl, i);
1216 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
1217 cf_pred[n_exc] = ret;
1221 main_end_bl = get_irg_end_block(current_ir_graph);
1222 main_end_bl_arity = get_irn_arity(main_end_bl);
1223 end_preds = xmalloc ((n_exc + main_end_bl_arity) * sizeof(*end_preds));
1225 for (i = 0; i < main_end_bl_arity; ++i)
1226 end_preds[i] = get_irn_n(main_end_bl, i);
1227 for (i = 0; i < n_exc; ++i)
1228 end_preds[main_end_bl_arity + i] = cf_pred[i];
1229 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
1230 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1231 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1237 /* -- Turn CSE back on. -- */
1238 set_optimize(rem_opt);
1243 /********************************************************************/
1244 /* Apply inlineing to small methods. */
1245 /********************************************************************/
1247 /** Represents a possible inlinable call in a graph. */
1248 typedef struct _call_entry call_entry;
1249 struct _call_entry {
1250 ir_node *call; /**< the Call */
1251 ir_graph *callee; /**< the callee called here */
1252 call_entry *next; /**< for linking the next one */
1256 * environment for inlining small irgs
1258 typedef struct _inline_env_t {
1259 struct obstack obst; /**< an obstack where call_entries are allocated on. */
1260 call_entry *head; /**< the head of the call entry list */
1261 call_entry *tail; /**< the tail of the call entry list */
1265 * Returns the irg called from a Call node. If the irg is not
1266 * known, NULL is returned.
1268 static ir_graph *get_call_called_irg(ir_node *call) {
1270 ir_graph *called_irg = NULL;
1272 addr = get_Call_ptr(call);
1273 if (is_SymConst(addr) && get_SymConst_kind(addr) == symconst_addr_ent) {
1274 called_irg = get_entity_irg(get_SymConst_entity(addr));
1281 * Walker: Collect all calls to known graphs inside a graph.
1283 static void collect_calls(ir_node *call, void *env) {
1284 if (is_Call(call)) {
1285 ir_graph *called_irg = get_call_called_irg(call);
1287 /* The Call node calls a locally defined method. Remember to inline. */
1288 inline_env_t *ienv = env;
1289 call_entry *entry = obstack_alloc(&ienv->obst, sizeof(*entry));
1291 entry->callee = called_irg;
1294 if (ienv->tail == NULL)
1297 ienv->tail->next = entry;
1304 * Inlines all small methods at call sites where the called address comes
1305 * from a Const node that references the entity representing the called
1307 * The size argument is a rough measure for the code size of the method:
1308 * Methods where the obstack containing the firm graph is smaller than
1311 void inline_small_irgs(ir_graph *irg, int size) {
1312 ir_graph *rem = current_ir_graph;
1315 DEBUG_ONLY(firm_dbg_module_t *dbg;)
1317 if (!(get_opt_optimize() && get_opt_inline())) return;
1319 FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1321 current_ir_graph = irg;
1322 /* Handle graph state */
1323 assert(get_irg_phase_state(irg) != phase_building);
1324 free_callee_info(irg);
1326 /* Find Call nodes to inline.
1327 (We can not inline during a walk of the graph, as inlineing the same
1328 method several times changes the visited flag of the walked graph:
1329 after the first inlineing visited of the callee equals visited of
1330 the caller. With the next inlineing both are increased.) */
1331 obstack_init(&env.obst);
1332 env.head = env.tail = NULL;
1333 irg_walk_graph(irg, NULL, collect_calls, &env);
1335 if (env.head != NULL) {
1336 /* There are calls to inline */
1337 collect_phiprojs(irg);
1338 for (entry = env.head; entry != NULL; entry = entry->next) {
1339 ir_graph *callee = entry->callee;
1340 if (((_obstack_memory_used(callee->obst) - (int)obstack_room(callee->obst)) < size) ||
1341 (get_irg_inline_property(callee) >= irg_inline_forced)) {
1342 inline_method(entry->call, callee);
1346 obstack_free(&env.obst, NULL);
1347 current_ir_graph = rem;
1351 * Environment for inlining irgs.
1354 int n_nodes; /**< Number of nodes in graph except Id, Tuple, Proj, Start, End. */
1355 int n_nodes_orig; /**< for statistics */
1356 call_entry *call_head; /**< The head of the list of all call nodes in this graph. */
1357 call_entry *call_tail; /**< The tail of the list of all call nodes in this graph .*/
1358 int n_call_nodes; /**< Number of Call nodes in the graph. */
1359 int n_call_nodes_orig; /**< for statistics */
1360 int n_callers; /**< Number of known graphs that call this graphs. */
1361 int n_callers_orig; /**< for statistics */
1362 int got_inline; /**< Set, if at leat one call inside this graph was inlined. */
1366 * Allocate a new environment for inlining.
1368 static inline_irg_env *alloc_inline_irg_env(struct obstack *obst) {
1369 inline_irg_env *env = obstack_alloc(obst, sizeof(*env));
1370 env->n_nodes = -2; /* do not count count Start, End */
1371 env->n_nodes_orig = -2; /* do not count Start, End */
1372 env->call_head = NULL;
1373 env->call_tail = NULL;
1374 env->n_call_nodes = 0;
1375 env->n_call_nodes_orig = 0;
1377 env->n_callers_orig = 0;
1378 env->got_inline = 0;
1382 typedef struct walker_env {
1383 struct obstack *obst; /**< the obstack for allocations. */
1384 inline_irg_env *x; /**< the inline environment */
1385 int ignore_runtime; /**< the ignore runtime flag */
1389 * post-walker: collect all calls in the inline-environment
1390 * of a graph and sum some statistics.
1392 static void collect_calls2(ir_node *call, void *ctx) {
1394 inline_irg_env *x = env->x;
1395 ir_op *op = get_irn_op(call);
1399 /* count meaningful nodes in irg */
1400 if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1405 if (op != op_Call) return;
1407 /* check, if it's a runtime call */
1408 if (env->ignore_runtime) {
1409 ir_node *symc = get_Call_ptr(call);
1411 if (is_SymConst(symc) && get_SymConst_kind(symc) == symconst_addr_ent) {
1412 ir_entity *ent = get_SymConst_entity(symc);
1414 if (get_entity_additional_properties(ent) & mtp_property_runtime)
1419 /* collect all call nodes */
1421 ++x->n_call_nodes_orig;
1423 callee = get_call_called_irg(call);
1425 inline_irg_env *callee_env = get_irg_link(callee);
1426 /* count all static callers */
1427 ++callee_env->n_callers;
1428 ++callee_env->n_callers_orig;
1430 /* link it in the list of possible inlinable entries */
1431 entry = obstack_alloc(env->obst, sizeof(*entry));
1433 entry->callee = callee;
1435 if (x->call_tail == NULL)
1436 x->call_head = entry;
1438 x->call_tail->next = entry;
1439 x->call_tail = entry;
1444 * Returns TRUE if the number of callers in 0 in the irg's environment,
1445 * hence this irg is a leave.
1447 INLINE static int is_leave(ir_graph *irg) {
1448 inline_irg_env *env = get_irg_link(irg);
1449 return env->n_call_nodes == 0;
1453 * Returns TRUE if the number of callers is smaller size in the irg's environment.
1455 INLINE static int is_smaller(ir_graph *callee, int size) {
1456 inline_irg_env *env = get_irg_link(callee);
1457 return env->n_nodes < size;
1461 * Append the nodes of the list src to the nodes of the list in environment dst.
1463 static void append_call_list(struct obstack *obst, inline_irg_env *dst, call_entry *src) {
1464 call_entry *entry, *nentry;
1466 /* Note that the src list points to Call nodes in the inlined graph, but
1467 we need Call nodes in our graph. Luckily the inliner leaves this information
1468 in the link field. */
1469 for (entry = src; entry != NULL; entry = entry->next) {
1470 nentry = obstack_alloc(obst, sizeof(*nentry));
1471 nentry->call = get_irn_link(entry->call);
1472 nentry->callee = entry->callee;
1473 nentry->next = NULL;
1474 dst->call_tail->next = nentry;
1475 dst->call_tail = nentry;
1480 * Inlines small leave methods at call sites where the called address comes
1481 * from a Const node that references the entity representing the called
1483 * The size argument is a rough measure for the code size of the method:
1484 * Methods where the obstack containing the firm graph is smaller than
1487 void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_runtime) {
1488 inline_irg_env *env;
1494 call_entry *entry, *tail;
1495 const call_entry *centry;
1496 struct obstack obst;
1497 DEBUG_ONLY(firm_dbg_module_t *dbg;)
1499 if (!(get_opt_optimize() && get_opt_inline())) return;
1501 FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1502 rem = current_ir_graph;
1503 obstack_init(&obst);
1505 /* extend all irgs by a temporary data structure for inlining. */
1506 n_irgs = get_irp_n_irgs();
1507 for (i = 0; i < n_irgs; ++i)
1508 set_irg_link(get_irp_irg(i), alloc_inline_irg_env(&obst));
1510 /* Precompute information in temporary data structure. */
1512 wenv.ignore_runtime = ignore_runtime;
1513 for (i = 0; i < n_irgs; ++i) {
1514 ir_graph *irg = get_irp_irg(i);
1516 assert(get_irg_phase_state(irg) != phase_building);
1517 free_callee_info(irg);
1519 wenv.x = get_irg_link(irg);
1520 irg_walk_graph(irg, NULL, collect_calls2, &wenv);
1523 /* -- and now inline. -- */
1525 /* Inline leaves recursively -- we might construct new leaves. */
1529 for (i = 0; i < n_irgs; ++i) {
1531 int phiproj_computed = 0;
1533 current_ir_graph = get_irp_irg(i);
1534 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1537 for (entry = env->call_head; entry != NULL; entry = entry->next) {
1540 if (env->n_nodes > maxsize) break;
1543 callee = entry->callee;
1545 if (is_leave(callee) && is_smaller(callee, leavesize)) {
1546 if (!phiproj_computed) {
1547 phiproj_computed = 1;
1548 collect_phiprojs(current_ir_graph);
1550 did_inline = inline_method(call, callee);
1553 /* Do some statistics */
1554 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1556 env->got_inline = 1;
1557 --env->n_call_nodes;
1558 env->n_nodes += callee_env->n_nodes;
1559 --callee_env->n_callers;
1561 /* remove this call from the list */
1563 tail->next = entry->next;
1565 env->call_head = entry->next;
1571 env->call_tail = tail;
1573 } while (did_inline);
1575 /* inline other small functions. */
1576 for (i = 0; i < n_irgs; ++i) {
1578 int phiproj_computed = 0;
1580 current_ir_graph = get_irp_irg(i);
1581 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1583 /* note that the list of possible calls is updated during the process */
1585 for (entry = env->call_head; entry != NULL; entry = entry->next) {
1589 callee = entry->callee;
1591 if (((is_smaller(callee, size) && (env->n_nodes < maxsize)) || /* small function */
1592 (get_irg_inline_property(callee) >= irg_inline_forced))) {
1593 if (!phiproj_computed) {
1594 phiproj_computed = 1;
1595 collect_phiprojs(current_ir_graph);
1597 if (inline_method(call, callee)) {
1598 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1600 /* callee was inline. Append it's call list. */
1601 env->got_inline = 1;
1602 --env->n_call_nodes;
1603 append_call_list(&obst, env, callee_env->call_head);
1604 env->n_call_nodes += callee_env->n_call_nodes;
1605 env->n_nodes += callee_env->n_nodes;
1606 --callee_env->n_callers;
1608 /* after we have inlined callee, all called methods inside callee
1609 are now called once more */
1610 for (centry = callee_env->call_head; centry != NULL; centry = centry->next) {
1611 inline_irg_env *penv = get_irg_link(centry->callee);
1615 /* remove this call from the list */
1617 tail->next = entry->next;
1619 env->call_head = entry->next;
1625 env->call_tail = tail;
1628 for (i = 0; i < n_irgs; ++i) {
1629 irg = get_irp_irg(i);
1630 env = (inline_irg_env *)get_irg_link(irg);
1632 if (env->got_inline) {
1633 /* this irg got calls inlined */
1634 set_irg_outs_inconsistent(irg);
1635 set_irg_doms_inconsistent(irg);
1637 optimize_graph_df(irg);
1640 if (env->got_inline || (env->n_callers_orig != env->n_callers))
1641 DB((dbg, SET_LEVEL_1, "Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1642 env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1643 env->n_callers_orig, env->n_callers,
1644 get_entity_name(get_irg_entity(irg))));
1647 obstack_free(&obst, NULL);
1648 current_ir_graph = rem;
1651 /*******************************************************************/
1652 /* Code Placement. Pins all floating nodes to a block where they */
1653 /* will be executed only if needed. */
1654 /*******************************************************************/
1657 * Returns non-zero, is a block is not reachable from Start.
1659 * @param block the block to test
1662 is_Block_unreachable(ir_node *block) {
1663 return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
1667 * Find the earliest correct block for N. --- Place N into the
1668 * same Block as its dominance-deepest Input.
1670 * We have to avoid calls to get_nodes_block() here
1671 * because the graph is floating.
1673 * move_out_of_loops() expects that place_floats_early() have placed
1674 * all "living" nodes into a living block. That's why we must
1675 * move nodes in dead block with "live" successors into a valid
1677 * We move them just into the same block as it's successor (or
1678 * in case of a Phi into the effective use block). For Phi successors,
1679 * this may still be a dead block, but then there is no real use, as
1680 * the control flow will be dead later.
1683 place_floats_early(ir_node *n, waitq *worklist)
1687 /* we must not run into an infinite loop */
1688 assert(irn_not_visited(n));
1689 mark_irn_visited(n);
1691 /* Place floating nodes. */
1692 if (get_irn_pinned(n) == op_pin_state_floats) {
1693 ir_node *curr_block = get_irn_n(n, -1);
1694 int in_dead_block = is_Block_unreachable(curr_block);
1696 ir_node *b = NULL; /* The block to place this node in */
1698 assert(is_no_Block(n));
1700 if (is_irn_start_block_placed(n)) {
1701 /* These nodes will not be placed by the loop below. */
1702 b = get_irg_start_block(current_ir_graph);
1706 /* find the block for this node. */
1707 irn_arity = get_irn_arity(n);
1708 for (i = 0; i < irn_arity; i++) {
1709 ir_node *pred = get_irn_n(n, i);
1710 ir_node *pred_block;
1712 if ((irn_not_visited(pred))
1713 && (get_irn_pinned(pred) == op_pin_state_floats)) {
1716 * If the current node is NOT in a dead block, but one of its
1717 * predecessors is, we must move the predecessor to a live block.
1718 * Such thing can happen, if global CSE chose a node from a dead block.
1719 * We move it simply to our block.
1720 * Note that neither Phi nor End nodes are floating, so we don't
1721 * need to handle them here.
1723 if (! in_dead_block) {
1724 if (get_irn_pinned(pred) == op_pin_state_floats &&
1725 is_Block_unreachable(get_irn_n(pred, -1)))
1726 set_nodes_block(pred, curr_block);
1728 place_floats_early(pred, worklist);
1732 * A node in the Bad block must stay in the bad block,
1733 * so don't compute a new block for it.
1738 /* Because all loops contain at least one op_pin_state_pinned node, now all
1739 our inputs are either op_pin_state_pinned or place_early() has already
1740 been finished on them. We do not have any unfinished inputs! */
1741 pred_block = get_irn_n(pred, -1);
1742 if ((!is_Block_dead(pred_block)) &&
1743 (get_Block_dom_depth(pred_block) > depth)) {
1745 depth = get_Block_dom_depth(pred_block);
1747 /* Avoid that the node is placed in the Start block */
1748 if ((depth == 1) && (get_Block_dom_depth(get_irn_n(n, -1)) > 1)) {
1749 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1750 assert(b != get_irg_start_block(current_ir_graph));
1755 set_nodes_block(n, b);
1759 * Add predecessors of non floating nodes and non-floating predecessors
1760 * of floating nodes to worklist and fix their blocks if the are in dead block.
1762 irn_arity = get_irn_arity(n);
1764 if (get_irn_op(n) == op_End) {
1766 * Simplest case: End node. Predecessors are keep-alives,
1767 * no need to move out of dead block.
1769 for (i = -1; i < irn_arity; ++i) {
1770 ir_node *pred = get_irn_n(n, i);
1771 if (irn_not_visited(pred))
1772 waitq_put(worklist, pred);
1775 else if (is_Block(n)) {
1777 * Blocks: Predecessors are control flow, no need to move
1778 * them out of dead block.
1780 for (i = irn_arity - 1; i >= 0; --i) {
1781 ir_node *pred = get_irn_n(n, i);
1782 if (irn_not_visited(pred))
1783 waitq_put(worklist, pred);
1786 else if (is_Phi(n)) {
1788 ir_node *curr_block = get_irn_n(n, -1);
1789 int in_dead_block = is_Block_unreachable(curr_block);
1792 * Phi nodes: move nodes from dead blocks into the effective use
1793 * of the Phi-input if the Phi is not in a bad block.
1795 pred = get_irn_n(n, -1);
1796 if (irn_not_visited(pred))
1797 waitq_put(worklist, pred);
1799 for (i = irn_arity - 1; i >= 0; --i) {
1800 ir_node *pred = get_irn_n(n, i);
1802 if (irn_not_visited(pred)) {
1803 if (! in_dead_block &&
1804 get_irn_pinned(pred) == op_pin_state_floats &&
1805 is_Block_unreachable(get_irn_n(pred, -1))) {
1806 set_nodes_block(pred, get_Block_cfgpred_block(curr_block, i));
1808 waitq_put(worklist, pred);
1814 ir_node *curr_block = get_irn_n(n, -1);
1815 int in_dead_block = is_Block_unreachable(curr_block);
1818 * All other nodes: move nodes from dead blocks into the same block.
1820 pred = get_irn_n(n, -1);
1821 if (irn_not_visited(pred))
1822 waitq_put(worklist, pred);
1824 for (i = irn_arity - 1; i >= 0; --i) {
1825 ir_node *pred = get_irn_n(n, i);
1827 if (irn_not_visited(pred)) {
1828 if (! in_dead_block &&
1829 get_irn_pinned(pred) == op_pin_state_floats &&
1830 is_Block_unreachable(get_irn_n(pred, -1))) {
1831 set_nodes_block(pred, curr_block);
1833 waitq_put(worklist, pred);
1840 * Floating nodes form subgraphs that begin at nodes as Const, Load,
1841 * Start, Call and that end at op_pin_state_pinned nodes as Store, Call. Place_early
1842 * places all floating nodes reachable from its argument through floating
1843 * nodes and adds all beginnings at op_pin_state_pinned nodes to the worklist.
1845 static INLINE void place_early(waitq *worklist) {
1847 inc_irg_visited(current_ir_graph);
1849 /* this inits the worklist */
1850 place_floats_early(get_irg_end(current_ir_graph), worklist);
1852 /* Work the content of the worklist. */
1853 while (!waitq_empty(worklist)) {
1854 ir_node *n = waitq_get(worklist);
1855 if (irn_not_visited(n))
1856 place_floats_early(n, worklist);
1859 set_irg_outs_inconsistent(current_ir_graph);
1860 set_irg_pinned(current_ir_graph, op_pin_state_pinned);
1864 * Compute the deepest common ancestor of block and dca.
1866 static ir_node *calc_dca(ir_node *dca, ir_node *block)
1870 /* we do not want to place nodes in dead blocks */
1871 if (is_Block_dead(block))
1874 /* We found a first legal placement. */
1875 if (!dca) return block;
1877 /* Find a placement that is dominates both, dca and block. */
1878 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1879 block = get_Block_idom(block);
1881 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
1882 dca = get_Block_idom(dca);
1885 while (block != dca)
1886 { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1891 /** Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
1892 * I.e., DCA is the block where we might place PRODUCER.
1893 * A data flow edge points from producer to consumer.
1896 consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
1898 ir_node *block = NULL;
1900 /* Compute the latest block into which we can place a node so that it is
1902 if (get_irn_op(consumer) == op_Phi) {
1903 /* our consumer is a Phi-node, the effective use is in all those
1904 blocks through which the Phi-node reaches producer */
1906 ir_node *phi_block = get_nodes_block(consumer);
1907 irn_arity = get_irn_arity(consumer);
1909 for (i = 0; i < irn_arity; i++) {
1910 if (get_irn_n(consumer, i) == producer) {
1911 ir_node *new_block = get_nodes_block(get_Block_cfgpred(phi_block, i));
1913 if (! is_Block_unreachable(new_block))
1914 block = calc_dca(block, new_block);
1919 block = get_irn_n(producer, -1);
1922 assert(is_no_Block(consumer));
1923 block = get_nodes_block(consumer);
1926 /* Compute the deepest common ancestor of block and dca. */
1927 return calc_dca(dca, block);
1930 /* FIXME: the name clashes here with the function from ana/field_temperature.c
1932 static INLINE int get_irn_loop_depth(ir_node *n) {
1933 return get_loop_depth(get_irn_loop(n));
1937 * Move n to a block with less loop depth than it's current block. The
1938 * new block must be dominated by early.
1940 * @param n the node that should be moved
1941 * @param early the earliest block we can n move to
1943 static void move_out_of_loops(ir_node *n, ir_node *early)
1945 ir_node *best, *dca;
1949 /* Find the region deepest in the dominator tree dominating
1950 dca with the least loop nesting depth, but still dominated
1951 by our early placement. */
1952 dca = get_nodes_block(n);
1955 while (dca != early) {
1956 dca = get_Block_idom(dca);
1957 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
1958 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1962 if (best != get_nodes_block(n)) {
1964 printf("Moving out of loop: "); DDMN(n);
1965 printf(" Outermost block: "); DDMN(early);
1966 printf(" Best block: "); DDMN(best);
1967 printf(" Innermost block: "); DDMN(get_nodes_block(n));
1969 set_nodes_block(n, best);
1974 * Find the latest legal block for N and place N into the
1975 * `optimal' Block between the latest and earliest legal block.
1976 * The `optimal' block is the dominance-deepest block of those
1977 * with the least loop-nesting-depth. This places N out of as many
1978 * loops as possible and then makes it as control dependent as
1981 static void place_floats_late(ir_node *n, pdeq *worklist)
1986 assert(irn_not_visited(n)); /* no multiple placement */
1988 mark_irn_visited(n);
1990 /* no need to place block nodes, control nodes are already placed. */
1991 if ((get_irn_op(n) != op_Block) &&
1993 (get_irn_mode(n) != mode_X)) {
1994 /* Remember the early_blk placement of this block to move it
1995 out of loop no further than the early_blk placement. */
1996 early_blk = get_irn_n(n, -1);
1999 * BEWARE: Here we also get code, that is live, but
2000 * was in a dead block. If the node is life, but because
2001 * of CSE in a dead block, we still might need it.
2004 /* Assure that our users are all placed, except the Phi-nodes.
2005 --- Each data flow cycle contains at least one Phi-node. We
2006 have to break the `user has to be placed before the
2007 producer' dependence cycle and the Phi-nodes are the
2008 place to do so, because we need to base our placement on the
2009 final region of our users, which is OK with Phi-nodes, as they
2010 are op_pin_state_pinned, and they never have to be placed after a
2011 producer of one of their inputs in the same block anyway. */
2012 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2013 ir_node *succ = get_irn_out(n, i);
2014 if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
2015 place_floats_late(succ, worklist);
2018 if (! is_Block_dead(early_blk)) {
2019 /* do only move things that where not dead */
2020 ir_op *op = get_irn_op(n);
2022 /* We have to determine the final block of this node... except for
2023 constants and Projs */
2024 if ((get_irn_pinned(n) == op_pin_state_floats) &&
2026 (op != op_SymConst) &&
2029 ir_node *dca = NULL; /* deepest common ancestor in the
2030 dominator tree of all nodes'
2031 blocks depending on us; our final
2032 placement has to dominate DCA. */
2033 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2034 ir_node *succ = get_irn_out(n, i);
2037 if (get_irn_op(succ) == op_End) {
2039 * This consumer is the End node, a keep alive edge.
2040 * This is not a real consumer, so we ignore it
2045 /* ignore if succ is in dead code */
2046 succ_blk = get_irn_n(succ, -1);
2047 if (is_Block_unreachable(succ_blk))
2049 dca = consumer_dom_dca(dca, succ, n);
2052 set_nodes_block(n, dca);
2053 move_out_of_loops(n, early_blk);
2059 /* Add predecessors of all non-floating nodes on list. (Those of floating
2060 nodes are placed already and therefore are marked.) */
2061 for (i = 0; i < get_irn_n_outs(n); i++) {
2062 ir_node *succ = get_irn_out(n, i);
2063 if (irn_not_visited(get_irn_out(n, i))) {
2064 pdeq_putr(worklist, succ);
2069 static INLINE void place_late(waitq *worklist) {
2071 inc_irg_visited(current_ir_graph);
2073 /* This fills the worklist initially. */
2074 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
2076 /* And now empty the worklist again... */
2077 while (!waitq_empty(worklist)) {
2078 ir_node *n = waitq_get(worklist);
2079 if (irn_not_visited(n))
2080 place_floats_late(n, worklist);
2084 void place_code(ir_graph *irg) {
2086 ir_graph *rem = current_ir_graph;
2088 current_ir_graph = irg;
2090 if (!(get_opt_optimize() && get_opt_global_cse())) return;
2092 /* Handle graph state */
2093 assert(get_irg_phase_state(irg) != phase_building);
2096 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
2097 free_loop_information(irg);
2098 construct_backedges(irg);
2101 /* Place all floating nodes as early as possible. This guarantees
2102 a legal code placement. */
2103 worklist = new_waitq();
2104 place_early(worklist);
2106 /* place_early() invalidates the outs, place_late needs them. */
2107 compute_irg_outs(irg);
2109 /* Now move the nodes down in the dominator tree. This reduces the
2110 unnecessary executions of the node. */
2111 place_late(worklist);
2113 set_irg_outs_inconsistent(current_ir_graph);
2114 set_irg_loopinfo_inconsistent(current_ir_graph);
2115 del_waitq(worklist);
2116 current_ir_graph = rem;
2120 * Called by walker of remove_critical_cf_edges().
2122 * Place an empty block to an edge between a blocks of multiple
2123 * predecessors and a block of multiple successors.
2126 * @param env Environment of walker. The changed field.
2128 static void walk_critical_cf_edges(ir_node *n, void *env) {
2130 ir_node *pre, *block, *jmp;
2132 ir_graph *irg = get_irn_irg(n);
2134 /* Block has multiple predecessors */
2135 arity = get_irn_arity(n);
2137 if (n == get_irg_end_block(irg))
2138 return; /* No use to add a block here. */
2140 for (i = 0; i < arity; ++i) {
2143 pre = get_irn_n(n, i);
2144 cfop = get_irn_op(skip_Proj(pre));
2145 /* Predecessor has multiple successors. Insert new control flow edge but
2146 ignore exception edges. */
2147 if (! is_op_fragile(cfop) && is_op_forking(cfop)) {
2148 /* set predecessor of new block */
2149 block = new_r_Block(irg, 1, &pre);
2150 /* insert new jmp node to new block */
2151 jmp = new_r_Jmp(irg, block);
2152 /* set successor of new block */
2153 set_irn_n(n, i, jmp);
2155 } /* predecessor has multiple successors */
2156 } /* for all predecessors */
2157 } /* n is a multi-entry block */
2160 void remove_critical_cf_edges(ir_graph *irg) {
2163 irg_block_walk_graph(irg, NULL, walk_critical_cf_edges, &changed);
2165 /* control flow changed */
2166 set_irg_outs_inconsistent(irg);
2167 set_irg_extblk_inconsistent(irg);
2168 set_irg_doms_inconsistent(irg);
2169 set_irg_loopinfo_inconsistent(irg);