2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Optimizations for a whole ir graph, i.e., a procedure.
23 * @author Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
34 #include "irgraph_t.h"
37 #include "iroptimize.h"
44 #include "adt/array.h"
48 #include "adt/xmalloc.h"
52 #include "irbackedge_t.h"
59 #include "iredges_t.h"
62 /*------------------------------------------------------------------*/
63 /* apply optimizations of iropt to all nodes. */
64 /*------------------------------------------------------------------*/
67 * A wrapper around optimize_inplace_2() to be called from a walker.
69 static void optimize_in_place_wrapper (ir_node *n, void *env) {
70 ir_node *optimized = optimize_in_place_2(n);
74 exchange (n, optimized);
79 * Do local optimizations for a node.
81 * @param n the IR-node where to start. Typically the End node
84 * @note current_ir_graph must be set
86 static INLINE void do_local_optimize(ir_node *n) {
87 /* Handle graph state */
88 assert(get_irg_phase_state(current_ir_graph) != phase_building);
90 if (get_opt_global_cse())
91 set_irg_pinned(current_ir_graph, op_pin_state_floats);
92 set_irg_outs_inconsistent(current_ir_graph);
93 set_irg_doms_inconsistent(current_ir_graph);
94 set_irg_loopinfo_inconsistent(current_ir_graph);
96 /* Clean the value_table in irg for the CSE. */
97 del_identities(current_ir_graph->value_table);
98 current_ir_graph->value_table = new_identities();
100 /* walk over the graph */
101 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
104 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
105 void local_optimize_node(ir_node *n) {
106 ir_graph *rem = current_ir_graph;
107 current_ir_graph = get_irn_irg(n);
109 do_local_optimize(n);
111 current_ir_graph = rem;
115 * Block-Walker: uses dominance depth to mark dead blocks.
117 static void kill_dead_blocks(ir_node *block, void *env) {
120 if (get_Block_dom_depth(block) < 0) {
122 * Note that the new dominance code correctly handles
123 * the End block, i.e. it is always reachable from Start
125 set_Block_dead(block);
129 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
130 void local_optimize_graph(ir_graph *irg) {
131 ir_graph *rem = current_ir_graph;
132 current_ir_graph = irg;
134 if (get_irg_dom_state(irg) == dom_consistent)
135 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
137 do_local_optimize(get_irg_end(irg));
139 current_ir_graph = rem;
143 * Enqueue all users of a node to a wait queue.
144 * Handles mode_T nodes.
146 static void enqueue_users(ir_node *n, pdeq *waitq) {
147 const ir_edge_t *edge;
149 foreach_out_edge(n, edge) {
150 ir_node *succ = get_edge_src_irn(edge);
152 if (get_irn_link(succ) != waitq) {
153 pdeq_putr(waitq, succ);
154 set_irn_link(succ, waitq);
156 if (get_irn_mode(succ) == mode_T) {
157 /* A mode_T node has Proj's. Because most optimizations
158 run on the Proj's we have to enqueue them also. */
159 enqueue_users(succ, waitq);
165 * Data flow optimization walker.
166 * Optimizes all nodes and enqueue it's users
169 static void opt_walker(ir_node *n, void *env) {
173 optimized = optimize_in_place_2(n);
174 set_irn_link(optimized, NULL);
176 if (optimized != n) {
177 enqueue_users(n, waitq);
178 exchange(n, optimized);
182 /* Applies local optimizations to all nodes in the graph until fixpoint. */
183 void optimize_graph_df(ir_graph *irg) {
184 pdeq *waitq = new_pdeq();
185 ir_graph *rem = current_ir_graph;
189 current_ir_graph = irg;
191 state = edges_assure(irg);
193 if (get_opt_global_cse())
194 set_irg_pinned(current_ir_graph, op_pin_state_floats);
196 /* Clean the value_table in irg for the CSE. */
197 del_identities(irg->value_table);
198 irg->value_table = new_identities();
200 if (get_irg_dom_state(irg) == dom_consistent)
201 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
203 /* invalidate info */
204 set_irg_outs_inconsistent(irg);
205 set_irg_doms_inconsistent(irg);
206 set_irg_loopinfo_inconsistent(irg);
208 set_using_irn_link(irg);
210 end = get_irg_end(irg);
211 n_ka = get_End_n_keepalives(end);
213 /* walk over the graph, but don't touch keep-alives */
214 irg_walk(get_irg_end_block(irg), NULL, opt_walker, waitq);
217 * Optimize keep-alives by removing superfluous ones.
218 * Beware: the last transformation might add new keep-alives
219 * that keep blocks that are where visited! So, check only the
220 * "old" keep-alives, not the new ones!
222 * FIXME: it might be better to completely remove this
223 * optimization here ...
225 for (i = n_ka - 1; i >= 0; --i) {
226 ir_node *ka = get_End_keepalive(end, i);
228 if (irn_visited(ka) && !is_irn_keep(ka)) {
229 /* this node can be regularly visited, no need to keep it */
230 set_End_keepalive(end, i, get_irg_bad(irg));
233 /* now walk again and visit all not yet visited nodes */
234 set_irg_visited(current_ir_graph, get_irg_visited(irg) - 1);
235 irg_walk(get_irg_end(irg), NULL, opt_walker, waitq);
237 /* finish the wait queue */
238 while (! pdeq_empty(waitq)) {
239 ir_node *n = pdeq_getl(waitq);
241 opt_walker(n, waitq);
246 clear_using_irn_link(irg);
249 edges_deactivate(irg);
251 current_ir_graph = rem;
255 /*------------------------------------------------------------------*/
256 /* Routines for dead node elimination / copying garbage collection */
257 /* of the obstack. */
258 /*------------------------------------------------------------------*/
261 * Remember the new node in the old node by using a field all nodes have.
263 #define set_new_node(oldn, newn) set_irn_link(oldn, newn)
266 * Get this new node, before the old node is forgotten.
268 #define get_new_node(oldn) get_irn_link(oldn)
271 * Check if a new node was set.
273 #define has_new_node(n) (get_new_node(n) != NULL)
276 * We use the block_visited flag to mark that we have computed the
277 * number of useful predecessors for this block.
278 * Further we encode the new arity in this flag in the old blocks.
279 * Remembering the arity is useful, as it saves a lot of pointer
280 * accesses. This function is called for all Phi and Block nodes
284 compute_new_arity(ir_node *b) {
285 int i, res, irn_arity;
288 irg_v = get_irg_block_visited(current_ir_graph);
289 block_v = get_Block_block_visited(b);
290 if (block_v >= irg_v) {
291 /* we computed the number of preds for this block and saved it in the
293 return block_v - irg_v;
295 /* compute the number of good predecessors */
296 res = irn_arity = get_irn_arity(b);
297 for (i = 0; i < irn_arity; i++)
298 if (is_Bad(get_irn_n(b, i))) res--;
299 /* save it in the flag. */
300 set_Block_block_visited(b, irg_v + res);
306 * Copies the node to the new obstack. The Ins of the new node point to
307 * the predecessors on the old obstack. For block/phi nodes not all
308 * predecessors might be copied. n->link points to the new node.
309 * For Phi and Block nodes the function allocates in-arrays with an arity
310 * only for useful predecessors. The arity is determined by counting
311 * the non-bad predecessors of the block.
313 * @param n The node to be copied
314 * @param env if non-NULL, the node number attribute will be copied to the new node
316 * Note: Also used for loop unrolling.
318 static void copy_node(ir_node *n, void *env) {
321 ir_op *op = get_irn_op(n);
325 /* node copied already */
327 } else if (op == op_Block) {
329 new_arity = compute_new_arity(n);
330 n->attr.block.graph_arr = NULL;
332 block = get_nodes_block(n);
334 new_arity = compute_new_arity(block);
336 new_arity = get_irn_arity(n);
339 nn = new_ir_node(get_irn_dbg_info(n),
346 /* Copy the attributes. These might point to additional data. If this
347 was allocated on the old obstack the pointers now are dangling. This
348 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
349 if (op == op_Block) {
350 /* we cannot allow blocks WITHOUT macroblock input */
351 set_Block_MacroBlock(nn, get_Block_MacroBlock(n));
353 copy_node_attr(n, nn);
357 int copy_node_nr = env != NULL;
359 /* for easier debugging, we want to copy the node numbers too */
360 nn->node_nr = n->node_nr;
366 hook_dead_node_elim_subst(current_ir_graph, n, nn);
370 * Copies new predecessors of old node to new node remembered in link.
371 * Spare the Bad predecessors of Phi and Block nodes.
373 static void copy_preds(ir_node *n, void *env) {
378 nn = get_new_node(n);
381 /* copy the macro block header */
382 ir_node *mbh = get_Block_MacroBlock(n);
385 /* this block is a macroblock header */
386 set_Block_MacroBlock(nn, nn);
388 /* get the macro block header */
389 ir_node *nmbh = get_new_node(mbh);
390 assert(nmbh != NULL);
391 set_Block_MacroBlock(nn, nmbh);
394 /* Don't copy Bad nodes. */
396 irn_arity = get_irn_arity(n);
397 for (i = 0; i < irn_arity; i++) {
398 if (! is_Bad(get_irn_n(n, i))) {
399 ir_node *pred = get_irn_n(n, i);
400 set_irn_n(nn, j, get_new_node(pred));
404 /* repair the block visited flag from above misuse. Repair it in both
405 graphs so that the old one can still be used. */
406 set_Block_block_visited(nn, 0);
407 set_Block_block_visited(n, 0);
408 /* Local optimization could not merge two subsequent blocks if
409 in array contained Bads. Now it's possible.
410 We don't call optimize_in_place as it requires
411 that the fields in ir_graph are set properly. */
412 if ((get_opt_control_flow_straightening()) &&
413 (get_Block_n_cfgpreds(nn) == 1) &&
414 is_Jmp(get_Block_cfgpred(nn, 0))) {
415 ir_node *old = get_nodes_block(get_Block_cfgpred(nn, 0));
417 /* Jmp jumps into the block it is in -- deal self cycle. */
418 assert(is_Bad(get_new_node(get_irg_bad(current_ir_graph))));
419 exchange(nn, get_new_node(get_irg_bad(current_ir_graph)));
424 } else if (is_Phi(n) && get_irn_arity(n) > 0) {
425 /* Don't copy node if corresponding predecessor in block is Bad.
426 The Block itself should not be Bad. */
427 block = get_nodes_block(n);
428 set_nodes_block(nn, get_new_node(block));
430 irn_arity = get_irn_arity(n);
431 for (i = 0; i < irn_arity; i++) {
432 if (! is_Bad(get_irn_n(block, i))) {
433 ir_node *pred = get_irn_n(n, i);
434 set_irn_n(nn, j, get_new_node(pred));
435 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
439 /* If the pre walker reached this Phi after the post walker visited the
440 block block_visited is > 0. */
441 set_Block_block_visited(get_nodes_block(n), 0);
442 /* Compacting the Phi's ins might generate Phis with only one
444 if (get_irn_arity(nn) == 1)
445 exchange(nn, get_irn_n(nn, 0));
447 irn_arity = get_irn_arity(n);
448 for (i = -1; i < irn_arity; i++)
449 set_irn_n(nn, i, get_new_node(get_irn_n(n, i)));
451 /* Now the new node is complete. We can add it to the hash table for CSE.
452 @@@ inlining aborts if we identify End. Why? */
454 add_identities(current_ir_graph->value_table, nn);
458 * Copies the graph recursively, compacts the keep-alives of the end node.
460 * @param irg the graph to be copied
461 * @param copy_node_nr If non-zero, the node number will be copied
463 static void copy_graph(ir_graph *irg, int copy_node_nr) {
464 ir_node *oe, *ne, *ob, *nb, *om, *nm; /* old end, new end, old bad, new bad, old NoMem, new NoMem */
465 ir_node *ka; /* keep alive */
469 /* Some nodes must be copied by hand, sigh */
470 vfl = get_irg_visited(irg);
471 set_irg_visited(irg, vfl + 1);
473 oe = get_irg_end(irg);
474 mark_irn_visited(oe);
475 /* copy the end node by hand, allocate dynamic in array! */
476 ne = new_ir_node(get_irn_dbg_info(oe),
483 /* Copy the attributes. Well, there might be some in the future... */
484 copy_node_attr(oe, ne);
485 set_new_node(oe, ne);
487 /* copy the Bad node */
488 ob = get_irg_bad(irg);
489 mark_irn_visited(ob);
490 nb = new_ir_node(get_irn_dbg_info(ob),
497 copy_node_attr(ob, nb);
498 set_new_node(ob, nb);
500 /* copy the NoMem node */
501 om = get_irg_no_mem(irg);
502 mark_irn_visited(om);
503 nm = new_ir_node(get_irn_dbg_info(om),
510 copy_node_attr(om, nm);
511 set_new_node(om, nm);
513 /* copy the live nodes */
514 set_irg_visited(irg, vfl);
515 irg_walk(get_nodes_block(oe), copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
517 /* Note: from yet, the visited flag of the graph is equal to vfl + 1 */
519 /* visit the anchors as well */
520 for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
521 ir_node *n = get_irg_anchor(irg, i);
523 if (n && (get_irn_visited(n) <= vfl)) {
524 set_irg_visited(irg, vfl);
525 irg_walk(n, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
529 /* copy_preds for the end node ... */
530 set_nodes_block(ne, get_new_node(get_nodes_block(oe)));
532 /*- ... and now the keep alives. -*/
533 /* First pick the not marked block nodes and walk them. We must pick these
534 first as else we will oversee blocks reachable from Phis. */
535 irn_arity = get_End_n_keepalives(oe);
536 for (i = 0; i < irn_arity; i++) {
537 ka = get_End_keepalive(oe, i);
539 if (get_irn_visited(ka) <= vfl) {
540 /* We must keep the block alive and copy everything reachable */
541 set_irg_visited(irg, vfl);
542 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
544 add_End_keepalive(ne, get_new_node(ka));
548 /* Now pick other nodes. Here we will keep all! */
549 irn_arity = get_End_n_keepalives(oe);
550 for (i = 0; i < irn_arity; i++) {
551 ka = get_End_keepalive(oe, i);
553 if (get_irn_visited(ka) <= vfl) {
554 /* We didn't copy the node yet. */
555 set_irg_visited(irg, vfl);
556 irg_walk(ka, copy_node, copy_preds, INT_TO_PTR(copy_node_nr));
558 add_End_keepalive(ne, get_new_node(ka));
562 /* start block sometimes only reached after keep alives */
563 set_nodes_block(nb, get_new_node(get_nodes_block(ob)));
564 set_nodes_block(nm, get_new_node(get_nodes_block(om)));
568 * Copies the graph reachable from current_ir_graph->end to the obstack
569 * in current_ir_graph and fixes the environment.
570 * Then fixes the fields in current_ir_graph containing nodes of the
573 * @param copy_node_nr If non-zero, the node number will be copied
576 copy_graph_env(int copy_node_nr) {
577 ir_graph *irg = current_ir_graph;
578 ir_node *old_end, *new_anchor;
581 /* remove end_except and end_reg nodes */
582 old_end = get_irg_end(irg);
583 set_irg_end_except (irg, old_end);
584 set_irg_end_reg (irg, old_end);
586 /* Not all nodes remembered in irg might be reachable
587 from the end node. Assure their link is set to NULL, so that
588 we can test whether new nodes have been computed. */
589 for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
590 ir_node *n = get_irg_anchor(irg, i);
592 set_new_node(n, NULL);
594 /* we use the block walk flag for removing Bads from Blocks ins. */
595 inc_irg_block_visited(irg);
598 copy_graph(irg, copy_node_nr);
601 old_end = get_irg_end(irg);
602 new_anchor = new_Anchor(irg);
604 for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
605 ir_node *n = get_irg_anchor(irg, i);
607 set_irn_n(new_anchor, i, get_new_node(n));
610 irg->anchor = new_anchor;
612 /* ensure the new anchor is placed in the endblock */
613 set_nodes_block(new_anchor, get_irg_end_block(irg));
617 * Copies all reachable nodes to a new obstack. Removes bad inputs
618 * from block nodes and the corresponding inputs from Phi nodes.
619 * Merges single exit blocks with single entry blocks and removes
621 * Adds all new nodes to a new hash table for CSE. Does not
622 * perform CSE, so the hash table might contain common subexpressions.
624 void dead_node_elimination(ir_graph *irg) {
626 #ifdef INTERPROCEDURAL_VIEW
627 int rem_ipview = get_interprocedural_view();
629 struct obstack *graveyard_obst = NULL;
630 struct obstack *rebirth_obst = NULL;
631 assert(! edges_activated(irg) && "dead node elimination requires disabled edges");
633 /* inform statistics that we started a dead-node elimination run */
634 hook_dead_node_elim(irg, 1);
636 /* Remember external state of current_ir_graph. */
637 rem = current_ir_graph;
638 current_ir_graph = irg;
639 #ifdef INTERPROCEDURAL_VIEW
640 set_interprocedural_view(0);
643 assert(get_irg_phase_state(irg) != phase_building);
645 /* Handle graph state */
646 free_callee_info(irg);
650 /* @@@ so far we loose loops when copying */
651 free_loop_information(irg);
653 set_irg_doms_inconsistent(irg);
655 /* A quiet place, where the old obstack can rest in peace,
656 until it will be cremated. */
657 graveyard_obst = irg->obst;
659 /* A new obstack, where the reachable nodes will be copied to. */
660 rebirth_obst = xmalloc(sizeof(*rebirth_obst));
661 irg->obst = rebirth_obst;
662 obstack_init(irg->obst);
663 irg->last_node_idx = 0;
665 /* We also need a new value table for CSE */
666 del_identities(irg->value_table);
667 irg->value_table = new_identities();
669 /* Copy the graph from the old to the new obstack */
670 copy_graph_env(/*copy_node_nr=*/1);
672 /* Free memory from old unoptimized obstack */
673 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
674 xfree(graveyard_obst); /* ... then free it. */
676 /* inform statistics that the run is over */
677 hook_dead_node_elim(irg, 0);
679 current_ir_graph = rem;
680 #ifdef INTERPROCEDURAL_VIEW
681 set_interprocedural_view(rem_ipview);
686 * Relink bad predecessors of a block and store the old in array to the
687 * link field. This function is called by relink_bad_predecessors().
688 * The array of link field starts with the block operand at position 0.
689 * If block has bad predecessors, create a new in array without bad preds.
690 * Otherwise let in array untouched.
692 static void relink_bad_block_predecessors(ir_node *n, void *env) {
693 ir_node **new_in, *irn;
694 int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
697 /* if link field of block is NULL, look for bad predecessors otherwise
698 this is already done */
699 if (is_Block(n) && get_irn_link(n) == NULL) {
700 /* save old predecessors in link field (position 0 is the block operand)*/
701 set_irn_link(n, get_irn_in(n));
703 /* count predecessors without bad nodes */
704 old_irn_arity = get_irn_arity(n);
705 for (i = 0; i < old_irn_arity; i++)
706 if (!is_Bad(get_irn_n(n, i)))
709 /* arity changing: set new predecessors without bad nodes */
710 if (new_irn_arity < old_irn_arity) {
711 /* Get new predecessor array. We do not resize the array, as we must
712 keep the old one to update Phis. */
713 new_in = NEW_ARR_D(ir_node *, current_ir_graph->obst, (new_irn_arity+1));
715 /* set new predecessors in array */
718 for (i = 0; i < old_irn_arity; i++) {
719 irn = get_irn_n(n, i);
721 new_in[new_irn_n] = irn;
722 is_backedge(n, i) ? set_backedge(n, new_irn_n-1) : set_not_backedge(n, new_irn_n-1);
726 /* ARR_SETLEN(int, n->attr.block.backedge, new_irn_arity); */
727 ARR_SHRINKLEN(n->attr.block.backedge, new_irn_arity);
729 } /* ir node has bad predecessors */
730 } /* Block is not relinked */
734 * Relinks Bad predecessors from Blocks and Phis called by walker
735 * remove_bad_predecesors(). If n is a Block, call
736 * relink_bad_block_redecessors(). If n is a Phi-node, call also the relinking
737 * function of Phi's Block. If this block has bad predecessors, relink preds
740 static void relink_bad_predecessors(ir_node *n, void *env) {
741 ir_node *block, **old_in;
742 int i, old_irn_arity, new_irn_arity;
744 /* relink bad predecessors of a block */
746 relink_bad_block_predecessors(n, env);
748 /* If Phi node relink its block and its predecessors */
750 /* Relink predecessors of phi's block */
751 block = get_nodes_block(n);
752 if (get_irn_link(block) == NULL)
753 relink_bad_block_predecessors(block, env);
755 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
756 old_irn_arity = ARR_LEN(old_in);
758 /* Relink Phi predecessors if count of predecessors changed */
759 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
760 /* set new predecessors in array
761 n->in[0] remains the same block */
763 for(i = 1; i < old_irn_arity; i++)
764 if (!is_Bad(old_in[i])) {
765 n->in[new_irn_arity] = n->in[i];
766 is_backedge(n, i) ? set_backedge(n, new_irn_arity) : set_not_backedge(n, new_irn_arity);
770 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
771 ARR_SETLEN(int, n->attr.phi.u.backedge, new_irn_arity);
773 } /* n is a Phi node */
777 * Removes Bad Bad predecessors from Blocks and the corresponding
778 * inputs to Phi nodes as in dead_node_elimination but without
780 * On walking up set the link field to NULL, on walking down call
781 * relink_bad_predecessors() (This function stores the old in array
782 * to the link field and sets a new in array if arity of predecessors
785 void remove_bad_predecessors(ir_graph *irg) {
786 panic("Fix backedge handling first");
787 irg_walk_graph(irg, firm_clear_link, relink_bad_predecessors, NULL);
794 __)|_| | \_/ | \_/(/_ |_/\__|__
796 The following stuff implements a facility that automatically patches
797 registered ir_node pointers to the new node when a dead node elimination occurs.
800 struct _survive_dce_t {
804 hook_entry_t dead_node_elim;
805 hook_entry_t dead_node_elim_subst;
808 typedef struct _survive_dce_list_t {
809 struct _survive_dce_list_t *next;
811 } survive_dce_list_t;
813 static void dead_node_hook(void *context, ir_graph *irg, int start) {
814 survive_dce_t *sd = context;
817 /* Create a new map before the dead node elimination is performed. */
819 sd->new_places = pmap_create_ex(pmap_count(sd->places));
821 /* Patch back all nodes if dead node elimination is over and something is to be done. */
822 pmap_destroy(sd->places);
823 sd->places = sd->new_places;
824 sd->new_places = NULL;
829 * Hook called when dead node elimination replaces old by nw.
831 static void dead_node_subst_hook(void *context, ir_graph *irg, ir_node *old, ir_node *nw) {
832 survive_dce_t *sd = context;
833 survive_dce_list_t *list = pmap_get(sd->places, old);
836 /* If the node is to be patched back, write the new address to all registered locations. */
838 survive_dce_list_t *p;
840 for (p = list; p; p = p->next)
843 pmap_insert(sd->new_places, nw, list);
848 * Make a new Survive DCE environment.
850 survive_dce_t *new_survive_dce(void) {
851 survive_dce_t *res = xmalloc(sizeof(res[0]));
852 obstack_init(&res->obst);
853 res->places = pmap_create();
854 res->new_places = NULL;
856 res->dead_node_elim.hook._hook_dead_node_elim = dead_node_hook;
857 res->dead_node_elim.context = res;
858 res->dead_node_elim.next = NULL;
860 res->dead_node_elim_subst.hook._hook_dead_node_elim_subst = dead_node_subst_hook;
861 res->dead_node_elim_subst.context = res;
862 res->dead_node_elim_subst.next = NULL;
864 #ifndef FIRM_ENABLE_HOOKS
865 assert(0 && "need hooks enabled");
868 register_hook(hook_dead_node_elim, &res->dead_node_elim);
869 register_hook(hook_dead_node_elim_subst, &res->dead_node_elim_subst);
874 * Free a Survive DCE environment.
876 void free_survive_dce(survive_dce_t *sd) {
877 obstack_free(&sd->obst, NULL);
878 pmap_destroy(sd->places);
879 unregister_hook(hook_dead_node_elim, &sd->dead_node_elim);
880 unregister_hook(hook_dead_node_elim_subst, &sd->dead_node_elim_subst);
885 * Register a node pointer to be patched upon DCE.
886 * When DCE occurs, the node pointer specified by @p place will be
887 * patched to the new address of the node it is pointing to.
889 * @param sd The Survive DCE environment.
890 * @param place The address of the node pointer.
892 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place) {
893 if (*place != NULL) {
894 ir_node *irn = *place;
895 survive_dce_list_t *curr = pmap_get(sd->places, irn);
896 survive_dce_list_t *nw = obstack_alloc(&sd->obst, sizeof(nw[0]));
901 pmap_insert(sd->places, irn, nw);
905 /*--------------------------------------------------------------------*/
906 /* Functionality for inlining */
907 /*--------------------------------------------------------------------*/
910 * Copy node for inlineing. Updates attributes that change when
911 * inlineing but not for dead node elimination.
913 * Copies the node by calling copy_node() and then updates the entity if
914 * it's a local one. env must be a pointer of the frame type of the
915 * inlined procedure. The new entities must be in the link field of
919 copy_node_inline(ir_node *n, void *env) {
921 ir_type *frame_tp = (ir_type *)env;
925 nn = get_new_node (n);
927 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
928 set_Sel_entity(nn, get_entity_link(get_Sel_entity(n)));
930 } else if (is_Block(n)) {
931 nn = get_new_node (n);
932 nn->attr.block.irg = current_ir_graph;
937 * Walker: checks if P_value_arg_base is used.
939 static void find_addr(ir_node *node, void *env) {
940 int *allow_inline = env;
942 is_Start(get_Proj_pred(node)) &&
943 get_Proj_proj(node) == pn_Start_P_value_arg_base) {
949 * Check if we can inline a given call.
950 * Currently, we cannot inline two cases:
951 * - call with compound arguments
952 * - graphs that take the address of a parameter
954 * check these conditions here
956 static int can_inline(ir_node *call, ir_graph *called_graph) {
957 ir_type *call_type = get_Call_type(call);
958 int params, ress, i, res;
959 assert(is_Method_type(call_type));
961 params = get_method_n_params(call_type);
962 ress = get_method_n_ress(call_type);
964 /* check parameters for compound arguments */
965 for (i = 0; i < params; ++i) {
966 ir_type *p_type = get_method_param_type(call_type, i);
968 if (is_compound_type(p_type))
972 /* check results for compound arguments */
973 for (i = 0; i < ress; ++i) {
974 ir_type *r_type = get_method_res_type(call_type, i);
976 if (is_compound_type(r_type))
981 irg_walk_graph(called_graph, find_addr, NULL, &res);
987 exc_handler = 0, /**< There is a handler. */
988 exc_to_end = 1, /**< Branches to End. */
989 exc_no_handler = 2 /**< Exception handling not represented. */
992 /* Inlines a method at the given call site. */
993 int inline_method(ir_node *call, ir_graph *called_graph) {
995 ir_node *post_call, *post_bl;
996 ir_node *in[pn_Start_max];
997 ir_node *end, *end_bl;
1001 int arity, n_ret, n_exc, n_res, i, n, j, rem_opt, irn_arity;
1002 enum exc_mode exc_handling;
1003 ir_type *called_frame, *curr_frame;
1004 irg_inline_property prop = get_irg_inline_property(called_graph);
1007 if (prop == irg_inline_forbidden)
1010 ent = get_irg_entity(called_graph);
1012 /* Do not inline variadic functions. */
1013 if (get_method_variadicity(get_entity_type(ent)) == variadicity_variadic)
1016 assert(get_method_n_params(get_entity_type(ent)) ==
1017 get_method_n_params(get_Call_type(call)));
1020 * We cannot inline a recursive call. The graph must be copied before
1021 * the call the inline_method() using create_irg_copy().
1023 if (called_graph == current_ir_graph)
1027 * currently, we cannot inline two cases:
1028 * - call with compound arguments
1029 * - graphs that take the address of a parameter
1031 if (! can_inline(call, called_graph))
1034 /* -- Turn off optimizations, this can cause problems when allocating new nodes. -- */
1035 rem_opt = get_opt_optimize();
1038 /* Handle graph state */
1039 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1040 assert(get_irg_pinned(current_ir_graph) == op_pin_state_pinned);
1041 assert(get_irg_pinned(called_graph) == op_pin_state_pinned);
1042 set_irg_outs_inconsistent(current_ir_graph);
1043 set_irg_extblk_inconsistent(current_ir_graph);
1044 set_irg_doms_inconsistent(current_ir_graph);
1045 set_irg_loopinfo_inconsistent(current_ir_graph);
1046 set_irg_callee_info_state(current_ir_graph, irg_callee_info_inconsistent);
1048 /* -- Check preconditions -- */
1049 assert(is_Call(call));
1051 /* here we know we WILL inline, so inform the statistics */
1052 hook_inline(call, called_graph);
1054 /* -- Decide how to handle exception control flow: Is there a handler
1055 for the Call node, or do we branch directly to End on an exception?
1057 0 There is a handler.
1059 2 Exception handling not represented in Firm. -- */
1061 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
1062 for (proj = get_irn_link(call); proj; proj = get_irn_link(proj)) {
1063 long proj_nr = get_Proj_proj(proj);
1064 if (proj_nr == pn_Call_X_except) Xproj = proj;
1065 if (proj_nr == pn_Call_M_except) Mproj = proj;
1067 if (Mproj) { assert(Xproj); exc_handling = exc_handler; } /* Mproj */
1068 else if (Xproj) { exc_handling = exc_to_end; } /* !Mproj && Xproj */
1069 else { exc_handling = exc_no_handler; } /* !Mproj && !Xproj */
1073 the procedure and later replaces the Start node of the called graph.
1074 Post_call is the old Call node and collects the results of the called
1075 graph. Both will end up being a tuple. -- */
1076 post_bl = get_nodes_block(call);
1077 set_irg_current_block(current_ir_graph, post_bl);
1078 /* XxMxPxPxPxT of Start + parameter of Call */
1079 in[pn_Start_X_initial_exec] = new_Jmp();
1080 in[pn_Start_M] = get_Call_mem(call);
1081 in[pn_Start_P_frame_base] = get_irg_frame(current_ir_graph);
1082 in[pn_Start_P_globals] = get_irg_globals(current_ir_graph);
1083 in[pn_Start_P_tls] = get_irg_tls(current_ir_graph);
1084 in[pn_Start_T_args] = new_Tuple(get_Call_n_params(call), get_Call_param_arr(call));
1085 /* in[pn_Start_P_value_arg_base] = ??? */
1086 assert(pn_Start_P_value_arg_base == pn_Start_max - 1 && "pn_Start_P_value_arg_base not supported, fix");
1087 pre_call = new_Tuple(pn_Start_max - 1, in);
1091 The new block gets the ins of the old block, pre_call and all its
1092 predecessors and all Phi nodes. -- */
1093 part_block(pre_call);
1095 /* -- Prepare state for dead node elimination -- */
1096 /* Visited flags in calling irg must be >= flag in called irg.
1097 Else walker and arity computation will not work. */
1098 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
1099 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
1100 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
1101 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
1102 /* Set pre_call as new Start node in link field of the start node of
1103 calling graph and pre_calls block as new block for the start block
1105 Further mark these nodes so that they are not visited by the
1107 set_irn_link(get_irg_start(called_graph), pre_call);
1108 set_irn_visited(get_irg_start(called_graph), get_irg_visited(current_ir_graph));
1109 set_irn_link(get_irg_start_block(called_graph), get_nodes_block(pre_call));
1110 set_irn_visited(get_irg_start_block(called_graph), get_irg_visited(current_ir_graph));
1111 set_irn_link(get_irg_bad(called_graph), get_irg_bad(current_ir_graph));
1112 set_irn_visited(get_irg_bad(called_graph), get_irg_visited(current_ir_graph));
1114 /* Initialize for compaction of in arrays */
1115 inc_irg_block_visited(current_ir_graph);
1117 /* -- Replicate local entities of the called_graph -- */
1118 /* copy the entities. */
1119 called_frame = get_irg_frame_type(called_graph);
1120 curr_frame = get_irg_frame_type(current_ir_graph);
1121 for (i = 0, n = get_class_n_members(called_frame); i < n; ++i) {
1122 ir_entity *new_ent, *old_ent;
1123 old_ent = get_class_member(called_frame, i);
1124 new_ent = copy_entity_own(old_ent, curr_frame);
1125 set_entity_link(old_ent, new_ent);
1128 /* visited is > than that of called graph. With this trick visited will
1129 remain unchanged so that an outer walker, e.g., searching the call nodes
1130 to inline, calling this inline will not visit the inlined nodes. */
1131 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
1133 /* -- Performing dead node elimination inlines the graph -- */
1134 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
1136 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
1137 get_irg_frame_type(called_graph));
1139 /* Repair called_graph */
1140 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
1141 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
1142 set_Block_block_visited(get_irg_start_block(called_graph), 0);
1144 /* -- Merge the end of the inlined procedure with the call site -- */
1145 /* We will turn the old Call node into a Tuple with the following
1148 0: Phi of all Memories of Return statements.
1149 1: Jmp from new Block that merges the control flow from all exception
1150 predecessors of the old end block.
1151 2: Tuple of all arguments.
1152 3: Phi of Exception memories.
1153 In case the old Call directly branches to End on an exception we don't
1154 need the block merging all exceptions nor the Phi of the exception
1158 /* -- Precompute some values -- */
1159 end_bl = get_new_node(get_irg_end_block(called_graph));
1160 end = get_new_node(get_irg_end(called_graph));
1161 arity = get_irn_arity(end_bl); /* arity = n_exc + n_ret */
1162 n_res = get_method_n_ress(get_Call_type(call));
1164 res_pred = xmalloc(n_res * sizeof(*res_pred));
1165 cf_pred = xmalloc(arity * sizeof(*res_pred));
1167 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
1169 /* -- archive keepalives -- */
1170 irn_arity = get_irn_arity(end);
1171 for (i = 0; i < irn_arity; i++) {
1172 ir_node *ka = get_End_keepalive(end, i);
1174 add_End_keepalive(get_irg_end(current_ir_graph), ka);
1177 /* The new end node will die. We need not free as the in array is on the obstack:
1178 copy_node() only generated 'D' arrays. */
1180 /* -- Replace Return nodes by Jump nodes. -- */
1182 for (i = 0; i < arity; i++) {
1184 ret = get_irn_n(end_bl, i);
1185 if (is_Return(ret)) {
1186 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_block(ret));
1190 set_irn_in(post_bl, n_ret, cf_pred);
1192 /* -- Build a Tuple for all results of the method.
1193 Add Phi node if there was more than one Return. -- */
1194 turn_into_tuple(post_call, pn_Call_max);
1195 /* First the Memory-Phi */
1197 for (i = 0; i < arity; i++) {
1198 ret = get_irn_n(end_bl, i);
1199 if (is_Return(ret)) {
1200 cf_pred[n_ret] = get_Return_mem(ret);
1204 phi = new_Phi(n_ret, cf_pred, mode_M);
1205 set_Tuple_pred(call, pn_Call_M_regular, phi);
1206 /* Conserve Phi-list for further inlinings -- but might be optimized */
1207 if (get_nodes_block(phi) == post_bl) {
1208 set_irn_link(phi, get_irn_link(post_bl));
1209 set_irn_link(post_bl, phi);
1211 /* Now the real results */
1213 for (j = 0; j < n_res; j++) {
1215 for (i = 0; i < arity; i++) {
1216 ret = get_irn_n(end_bl, i);
1217 if (is_Return(ret)) {
1218 cf_pred[n_ret] = get_Return_res(ret, j);
1223 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
1227 /* Conserve Phi-list for further inlinings -- but might be optimized */
1228 if (get_nodes_block(phi) == post_bl) {
1229 set_Phi_next(phi, get_Block_phis(post_bl));
1230 set_Block_phis(post_bl, phi);
1233 set_Tuple_pred(call, pn_Call_T_result, new_Tuple(n_res, res_pred));
1235 set_Tuple_pred(call, pn_Call_T_result, new_Bad());
1237 /* handle the regular call */
1238 set_Tuple_pred(call, pn_Call_X_regular, new_Jmp());
1240 /* For now, we cannot inline calls with value_base */
1241 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
1243 /* Finally the exception control flow.
1244 We have two (three) possible situations:
1245 First if the Call branches to an exception handler: We need to add a Phi node to
1246 collect the memory containing the exception objects. Further we need
1247 to add another block to get a correct representation of this Phi. To
1248 this block we add a Jmp that resolves into the X output of the Call
1249 when the Call is turned into a tuple.
1250 Second the Call branches to End, the exception is not handled. Just
1251 add all inlined exception branches to the End node.
1252 Third: there is no Exception edge at all. Handle as case two. */
1253 if (exc_handling == exc_handler) {
1255 for (i = 0; i < arity; i++) {
1257 ret = get_irn_n(end_bl, i);
1258 irn = skip_Proj(ret);
1259 if (is_fragile_op(irn) || is_Raise(irn)) {
1260 cf_pred[n_exc] = ret;
1265 new_Block(n_exc, cf_pred); /* watch it: current_block is changed! */
1266 set_Tuple_pred(call, pn_Call_X_except, new_Jmp());
1267 /* The Phi for the memories with the exception objects */
1269 for (i = 0; i < arity; i++) {
1271 ret = skip_Proj(get_irn_n(end_bl, i));
1273 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 3);
1275 } else if (is_fragile_op(ret)) {
1276 /* We rely that all cfops have the memory output at the same position. */
1277 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 0);
1279 } else if (is_Raise(ret)) {
1280 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_block(ret), ret, mode_M, 1);
1284 set_Tuple_pred(call, pn_Call_M_except, new_Phi(n_exc, cf_pred, mode_M));
1286 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1287 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1290 ir_node *main_end_bl;
1291 int main_end_bl_arity;
1292 ir_node **end_preds;
1294 /* assert(exc_handling == 1 || no exceptions. ) */
1296 for (i = 0; i < arity; i++) {
1297 ir_node *ret = get_irn_n(end_bl, i);
1298 ir_node *irn = skip_Proj(ret);
1300 if (is_fragile_op(irn) || is_Raise(irn)) {
1301 cf_pred[n_exc] = ret;
1305 main_end_bl = get_irg_end_block(current_ir_graph);
1306 main_end_bl_arity = get_irn_arity(main_end_bl);
1307 end_preds = xmalloc((n_exc + main_end_bl_arity) * sizeof(*end_preds));
1309 for (i = 0; i < main_end_bl_arity; ++i)
1310 end_preds[i] = get_irn_n(main_end_bl, i);
1311 for (i = 0; i < n_exc; ++i)
1312 end_preds[main_end_bl_arity + i] = cf_pred[i];
1313 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
1314 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
1315 set_Tuple_pred(call, pn_Call_M_except, new_Bad());
1321 /* -- Turn CSE back on. -- */
1322 set_optimize(rem_opt);
1327 /********************************************************************/
1328 /* Apply inlineing to small methods. */
1329 /********************************************************************/
1331 /** Represents a possible inlinable call in a graph. */
1332 typedef struct _call_entry call_entry;
1333 struct _call_entry {
1334 ir_node *call; /**< the Call */
1335 ir_graph *callee; /**< the callee called here */
1336 call_entry *next; /**< for linking the next one */
1337 unsigned weight; /**< the weight of the call */
1341 * environment for inlining small irgs
1343 typedef struct _inline_env_t {
1344 struct obstack obst; /**< an obstack where call_entries are allocated on. */
1345 call_entry *head; /**< the head of the call entry list */
1346 call_entry *tail; /**< the tail of the call entry list */
1350 * Returns the irg called from a Call node. If the irg is not
1351 * known, NULL is returned.
1353 * @param call the call node
1355 static ir_graph *get_call_called_irg(ir_node *call) {
1358 addr = get_Call_ptr(call);
1359 if (is_Global(addr)) {
1360 ir_entity *ent = get_Global_entity(addr);
1361 return get_entity_irg(ent);
1368 * Walker: Collect all calls to known graphs inside a graph.
1370 static void collect_calls(ir_node *call, void *env) {
1371 if (is_Call(call)) {
1372 ir_graph *called_irg = get_call_called_irg(call);
1374 if (called_irg != NULL) {
1375 /* The Call node calls a locally defined method. Remember to inline. */
1376 inline_env_t *ienv = env;
1377 call_entry *entry = obstack_alloc(&ienv->obst, sizeof(*entry));
1379 entry->callee = called_irg;
1383 if (ienv->tail == NULL)
1386 ienv->tail->next = entry;
1393 * Inlines all small methods at call sites where the called address comes
1394 * from a Const node that references the entity representing the called
1396 * The size argument is a rough measure for the code size of the method:
1397 * Methods where the obstack containing the firm graph is smaller than
1400 void inline_small_irgs(ir_graph *irg, int size) {
1401 ir_graph *rem = current_ir_graph;
1404 DEBUG_ONLY(firm_dbg_module_t *dbg;)
1406 FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1408 current_ir_graph = irg;
1409 /* Handle graph state */
1410 assert(get_irg_phase_state(irg) != phase_building);
1411 free_callee_info(irg);
1413 /* Find Call nodes to inline.
1414 (We can not inline during a walk of the graph, as inlineing the same
1415 method several times changes the visited flag of the walked graph:
1416 after the first inlineing visited of the callee equals visited of
1417 the caller. With the next inlineing both are increased.) */
1418 obstack_init(&env.obst);
1419 env.head = env.tail = NULL;
1420 irg_walk_graph(irg, NULL, collect_calls, &env);
1422 if (env.head != NULL) {
1423 /* There are calls to inline */
1424 collect_phiprojs(irg);
1425 for (entry = env.head; entry != NULL; entry = entry->next) {
1426 ir_graph *callee = entry->callee;
1427 if (((_obstack_memory_used(callee->obst) - (int)obstack_room(callee->obst)) < size) ||
1428 (get_irg_inline_property(callee) >= irg_inline_forced)) {
1429 inline_method(entry->call, callee);
1433 obstack_free(&env.obst, NULL);
1434 current_ir_graph = rem;
1438 * Environment for inlining irgs.
1441 int n_nodes; /**< Number of nodes in graph except Id, Tuple, Proj, Start, End. */
1442 int n_nodes_orig; /**< for statistics */
1443 call_entry *call_head; /**< The head of the list of all call nodes in this graph. */
1444 call_entry *call_tail; /**< The tail of the list of all call nodes in this graph .*/
1445 int n_call_nodes; /**< Number of Call nodes in the graph. */
1446 int n_call_nodes_orig; /**< for statistics */
1447 int n_callers; /**< Number of known graphs that call this graphs. */
1448 int n_callers_orig; /**< for statistics */
1449 int got_inline; /**< Set, if at leat one call inside this graph was inlined. */
1453 * Allocate a new environment for inlining.
1455 static inline_irg_env *alloc_inline_irg_env(struct obstack *obst) {
1456 inline_irg_env *env = obstack_alloc(obst, sizeof(*env));
1457 env->n_nodes = -2; /* do not count count Start, End */
1458 env->n_nodes_orig = -2; /* do not count Start, End */
1459 env->call_head = NULL;
1460 env->call_tail = NULL;
1461 env->n_call_nodes = 0;
1462 env->n_call_nodes_orig = 0;
1464 env->n_callers_orig = 0;
1465 env->got_inline = 0;
1469 typedef struct walker_env {
1470 struct obstack *obst; /**< the obstack for allocations. */
1471 inline_irg_env *x; /**< the inline environment */
1472 char ignore_runtime; /**< the ignore runtime flag */
1473 char ignore_callers; /**< if set, do change callers data */
1477 * post-walker: collect all calls in the inline-environment
1478 * of a graph and sum some statistics.
1480 static void collect_calls2(ir_node *call, void *ctx) {
1482 inline_irg_env *x = env->x;
1483 ir_opcode code = get_irn_opcode(call);
1487 /* count meaningful nodes in irg */
1488 if (code != iro_Proj && code != iro_Tuple && code != iro_Sync) {
1493 if (code != iro_Call) return;
1495 /* check, if it's a runtime call */
1496 if (env->ignore_runtime) {
1497 ir_node *symc = get_Call_ptr(call);
1499 if (is_Global(symc)) {
1500 ir_entity *ent = get_Global_entity(symc);
1502 if (get_entity_additional_properties(ent) & mtp_property_runtime)
1507 /* collect all call nodes */
1509 ++x->n_call_nodes_orig;
1511 callee = get_call_called_irg(call);
1512 if (callee != NULL) {
1513 if (! env->ignore_callers) {
1514 inline_irg_env *callee_env = get_irg_link(callee);
1515 /* count all static callers */
1516 ++callee_env->n_callers;
1517 ++callee_env->n_callers_orig;
1520 /* link it in the list of possible inlinable entries */
1521 entry = obstack_alloc(env->obst, sizeof(*entry));
1523 entry->callee = callee;
1525 if (x->call_tail == NULL)
1526 x->call_head = entry;
1528 x->call_tail->next = entry;
1529 x->call_tail = entry;
1534 * Returns TRUE if the number of callers is 0 in the irg's environment,
1535 * hence this irg is a leave.
1537 INLINE static int is_leave(ir_graph *irg) {
1538 inline_irg_env *env = get_irg_link(irg);
1539 return env->n_call_nodes == 0;
1543 * Returns TRUE if the number of nodes in the callee is
1544 * smaller then size in the irg's environment.
1546 INLINE static int is_smaller(ir_graph *callee, int size) {
1547 inline_irg_env *env = get_irg_link(callee);
1548 return env->n_nodes < size;
1552 * Append the nodes of the list src to the nodes of the list in environment dst.
1554 static void append_call_list(struct obstack *obst, inline_irg_env *dst, call_entry *src) {
1555 call_entry *entry, *nentry;
1557 /* Note that the src list points to Call nodes in the inlined graph, but
1558 we need Call nodes in our graph. Luckily the inliner leaves this information
1559 in the link field. */
1560 for (entry = src; entry != NULL; entry = entry->next) {
1561 nentry = obstack_alloc(obst, sizeof(*nentry));
1562 nentry->call = get_irn_link(entry->call);
1563 nentry->callee = entry->callee;
1564 nentry->next = NULL;
1565 dst->call_tail->next = nentry;
1566 dst->call_tail = nentry;
1571 * Inlines small leave methods at call sites where the called address comes
1572 * from a Const node that references the entity representing the called
1574 * The size argument is a rough measure for the code size of the method:
1575 * Methods where the obstack containing the firm graph is smaller than
1578 void inline_leave_functions(int maxsize, int leavesize, int size, int ignore_runtime) {
1579 inline_irg_env *env;
1585 call_entry *entry, *tail;
1586 const call_entry *centry;
1587 struct obstack obst;
1588 pmap *copied_graphs;
1589 pmap_entry *pm_entry;
1590 DEBUG_ONLY(firm_dbg_module_t *dbg;)
1592 FIRM_DBG_REGISTER(dbg, "firm.opt.inline");
1593 rem = current_ir_graph;
1594 obstack_init(&obst);
1596 /* a map for the copied graphs, used to inline recursive calls */
1597 copied_graphs = pmap_create();
1599 /* extend all irgs by a temporary data structure for inlining. */
1600 n_irgs = get_irp_n_irgs();
1601 for (i = 0; i < n_irgs; ++i)
1602 set_irg_link(get_irp_irg(i), alloc_inline_irg_env(&obst));
1604 /* Precompute information in temporary data structure. */
1606 wenv.ignore_runtime = ignore_runtime;
1607 wenv.ignore_callers = 0;
1608 for (i = 0; i < n_irgs; ++i) {
1609 ir_graph *irg = get_irp_irg(i);
1611 assert(get_irg_phase_state(irg) != phase_building);
1612 free_callee_info(irg);
1614 wenv.x = get_irg_link(irg);
1615 irg_walk_graph(irg, NULL, collect_calls2, &wenv);
1618 /* -- and now inline. -- */
1620 /* Inline leaves recursively -- we might construct new leaves. */
1624 for (i = 0; i < n_irgs; ++i) {
1626 int phiproj_computed = 0;
1628 current_ir_graph = get_irp_irg(i);
1629 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1632 for (entry = env->call_head; entry != NULL; entry = entry->next) {
1635 if (env->n_nodes > maxsize) break;
1638 callee = entry->callee;
1640 if (is_leave(callee) && (
1641 is_smaller(callee, leavesize) || (get_irg_inline_property(callee) >= irg_inline_forced))) {
1642 if (!phiproj_computed) {
1643 phiproj_computed = 1;
1644 collect_phiprojs(current_ir_graph);
1646 did_inline = inline_method(call, callee);
1649 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1651 /* was inlined, must be recomputed */
1652 phiproj_computed = 0;
1654 /* Do some statistics */
1655 env->got_inline = 1;
1656 --env->n_call_nodes;
1657 env->n_nodes += callee_env->n_nodes;
1658 --callee_env->n_callers;
1660 /* remove this call from the list */
1662 tail->next = entry->next;
1664 env->call_head = entry->next;
1670 env->call_tail = tail;
1672 } while (did_inline);
1674 /* inline other small functions. */
1675 for (i = 0; i < n_irgs; ++i) {
1677 int phiproj_computed = 0;
1679 current_ir_graph = get_irp_irg(i);
1680 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1682 /* note that the list of possible calls is updated during the process */
1684 for (entry = env->call_head; entry != NULL; entry = entry->next) {
1689 callee = entry->callee;
1691 e = pmap_find(copied_graphs, callee);
1694 * Remap callee if we have a copy.
1695 * FIXME: Should we do this only for recursive Calls ?
1700 if (((is_smaller(callee, size) && (env->n_nodes < maxsize)) || /* small function */
1701 (get_irg_inline_property(callee) >= irg_inline_forced))) {
1702 if (current_ir_graph == callee) {
1704 * Recursive call: we cannot directly inline because we cannot walk
1705 * the graph and change it. So we have to make a copy of the graph
1709 inline_irg_env *callee_env;
1713 * No copy yet, create one.
1714 * Note that recursive methods are never leaves, so it is sufficient
1715 * to test this condition here.
1717 copy = create_irg_copy(callee);
1719 /* create_irg_copy() destroys the Proj links, recompute them */
1720 phiproj_computed = 0;
1722 /* allocate new environment */
1723 callee_env = alloc_inline_irg_env(&obst);
1724 set_irg_link(copy, callee_env);
1726 wenv.x = callee_env;
1727 wenv.ignore_callers = 1;
1728 irg_walk_graph(copy, NULL, collect_calls2, &wenv);
1731 * Enter the entity of the original graph. This is needed
1732 * for inline_method(). However, note that ent->irg still points
1733 * to callee, NOT to copy.
1735 set_irg_entity(copy, get_irg_entity(callee));
1737 pmap_insert(copied_graphs, callee, copy);
1740 /* we have only one caller: the original graph */
1741 callee_env->n_callers = 1;
1742 callee_env->n_callers_orig = 1;
1744 if (! phiproj_computed) {
1745 phiproj_computed = 1;
1746 collect_phiprojs(current_ir_graph);
1748 did_inline = inline_method(call, callee);
1750 inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
1752 /* was inlined, must be recomputed */
1753 phiproj_computed = 0;
1755 /* callee was inline. Append it's call list. */
1756 env->got_inline = 1;
1757 --env->n_call_nodes;
1758 append_call_list(&obst, env, callee_env->call_head);
1759 env->n_call_nodes += callee_env->n_call_nodes;
1760 env->n_nodes += callee_env->n_nodes;
1761 --callee_env->n_callers;
1763 /* after we have inlined callee, all called methods inside callee
1764 are now called once more */
1765 for (centry = callee_env->call_head; centry != NULL; centry = centry->next) {
1766 inline_irg_env *penv = get_irg_link(centry->callee);
1770 /* remove this call from the list */
1772 tail->next = entry->next;
1774 env->call_head = entry->next;
1780 env->call_tail = tail;
1783 for (i = 0; i < n_irgs; ++i) {
1784 irg = get_irp_irg(i);
1785 env = (inline_irg_env *)get_irg_link(irg);
1787 if (env->got_inline) {
1788 /* this irg got calls inlined */
1789 set_irg_outs_inconsistent(irg);
1790 set_irg_doms_inconsistent(irg);
1792 optimize_graph_df(irg);
1795 if (env->got_inline || (env->n_callers_orig != env->n_callers)) {
1796 DB((dbg, SET_LEVEL_1, "Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1797 env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1798 env->n_callers_orig, env->n_callers,
1799 get_entity_name(get_irg_entity(irg))));
1803 /* kill the copied graphs: we don't need them anymore */
1804 foreach_pmap(copied_graphs, pm_entry) {
1805 ir_graph *copy = pm_entry->value;
1807 /* reset the entity, otherwise it will be deleted in the next step ... */
1808 set_irg_entity(copy, NULL);
1809 free_ir_graph(copy);
1811 pmap_destroy(copied_graphs);
1813 obstack_free(&obst, NULL);
1814 current_ir_graph = rem;