3 * File name: ir/ir/irgopt.c
4 * Purpose: Optimizations for a whole ir graph, i.e., a procedure.
5 * Author: Christian Schaefer, Goetz Lindenmaier
6 * Modified by: Sebastian Felis
9 * Copyright: (c) 1998-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 # include "irnode_t.h"
24 # include "irgraph_t.h"
32 # include "pdeq.h" /* Fuer code placement */
35 # include "irbackedge_t.h"
36 # include "irflag_t.h"
37 # include "firmstat.h"
39 /* Defined in iropt.c */
40 pset *new_identities (void);
41 void del_identities (pset *value_table);
42 void add_identities (pset *value_table, ir_node *node);
44 /*------------------------------------------------------------------*/
45 /* apply optimizations of iropt to all nodes. */
46 /*------------------------------------------------------------------*/
48 static void init_link (ir_node *n, void *env) {
49 set_irn_link(n, NULL);
52 #if 0 /* Old version. Avoids Ids.
53 This is not necessary: we do a postwalk, and get_irn_n
54 removes ids anyways. So it's much cheaper to call the
55 optimization less often and use the exchange() algorithm. */
57 optimize_in_place_wrapper (ir_node *n, void *env) {
59 ir_node *optimized, *old;
61 irn_arity = intern_get_irn_arity(n);
62 for (i = 0; i < irn_arity; i++) {
63 /* get_irn_n skips Id nodes, so comparison old != optimized does not
64 show all optimizations. Therefore always set new predecessor. */
65 old = intern_get_irn_intra_n(n, i);
66 optimized = optimize_in_place_2(old);
67 set_irn_n(n, i, optimized);
70 if (intern_get_irn_op(n) == op_Block) {
71 optimized = optimize_in_place_2(n);
72 if (optimized != n) exchange (n, optimized);
77 optimize_in_place_wrapper (ir_node *n, void *env) {
78 ir_node *optimized = optimize_in_place_2(n);
79 if (optimized != n) exchange (n, optimized);
86 local_optimize_graph (ir_graph *irg) {
87 ir_graph *rem = current_ir_graph;
88 current_ir_graph = irg;
90 /* Handle graph state */
91 assert(get_irg_phase_state(irg) != phase_building);
92 if (get_opt_global_cse())
93 set_irg_pinned(current_ir_graph, floats);
94 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
95 set_irg_outs_inconsistent(current_ir_graph);
96 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
97 set_irg_dom_inconsistent(current_ir_graph);
99 /* Clean the value_table in irg for the cse. */
100 del_identities(irg->value_table);
101 irg->value_table = new_identities();
103 /* walk over the graph */
104 irg_walk(irg->end, init_link, optimize_in_place_wrapper, NULL);
106 current_ir_graph = rem;
109 /*------------------------------------------------------------------*/
110 /* Routines for dead node elimination / copying garbage collection */
111 /* of the obstack. */
112 /*------------------------------------------------------------------*/
115 * Remember the new node in the old node by using a field all nodes have.
118 set_new_node (ir_node *old, ir_node *new)
124 * Get this new node, before the old node is forgotton.
126 static INLINE ir_node *
127 get_new_node (ir_node * n)
133 * We use the block_visited flag to mark that we have computed the
134 * number of useful predecessors for this block.
135 * Further we encode the new arity in this flag in the old blocks.
136 * Remembering the arity is useful, as it saves a lot of pointer
137 * accesses. This function is called for all Phi and Block nodes
141 compute_new_arity(ir_node *b) {
142 int i, res, irn_arity;
145 irg_v = get_irg_block_visited(current_ir_graph);
146 block_v = get_Block_block_visited(b);
147 if (block_v >= irg_v) {
148 /* we computed the number of preds for this block and saved it in the
150 return block_v - irg_v;
152 /* compute the number of good predecessors */
153 res = irn_arity = intern_get_irn_arity(b);
154 for (i = 0; i < irn_arity; i++)
155 if (intern_get_irn_opcode(intern_get_irn_n(b, i)) == iro_Bad) res--;
156 /* save it in the flag. */
157 set_Block_block_visited(b, irg_v + res);
162 /* TODO: add an ir_op operation */
163 static INLINE void new_backedge_info(ir_node *n) {
164 switch(intern_get_irn_opcode(n)) {
166 n->attr.block.cg_backedge = NULL;
167 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
170 n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
173 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, intern_get_irn_arity(n));
180 * Copies the node to the new obstack. The Ins of the new node point to
181 * the predecessors on the old obstack. For block/phi nodes not all
182 * predecessors might be copied. n->link points to the new node.
183 * For Phi and Block nodes the function allocates in-arrays with an arity
184 * only for useful predecessors. The arity is determined by counting
185 * the non-bad predecessors of the block.
188 copy_node (ir_node *n, void *env) {
192 /* The end node looses it's flexible in array. This doesn't matter,
193 as dead node elimination builds End by hand, inlineing doesn't use
195 /* assert(n->op == op_End || ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC)); */
197 if (intern_get_irn_opcode(n) == iro_Block) {
199 new_arity = compute_new_arity(n);
200 n->attr.block.graph_arr = NULL;
202 block = get_nodes_Block(n);
203 if (intern_get_irn_opcode(n) == iro_Phi) {
204 new_arity = compute_new_arity(block);
206 new_arity = intern_get_irn_arity(n);
209 nn = new_ir_node(get_irn_dbg_info(n),
212 intern_get_irn_op(n),
213 intern_get_irn_mode(n),
216 /* Copy the attributes. These might point to additional data. If this
217 was allocated on the old obstack the pointers now are dangling. This
218 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
220 new_backedge_info(nn);
223 /* printf("\n old node: "); DDMSG2(n);
224 printf(" new node: "); DDMSG2(nn); */
229 * Copies new predecessors of old node to new node remembered in link.
230 * Spare the Bad predecessors of Phi and Block nodes.
233 copy_preds (ir_node *n, void *env) {
237 nn = get_new_node(n);
239 /* printf("\n old node: "); DDMSG2(n);
240 printf(" new node: "); DDMSG2(nn);
241 printf(" arities: old: %d, new: %d\n", intern_get_irn_arity(n), intern_get_irn_arity(nn)); */
243 if (intern_get_irn_opcode(n) == iro_Block) {
244 /* Don't copy Bad nodes. */
246 irn_arity = intern_get_irn_arity(n);
247 for (i = 0; i < irn_arity; i++)
248 if (intern_get_irn_opcode(intern_get_irn_n(n, i)) != iro_Bad) {
249 set_irn_n (nn, j, get_new_node(intern_get_irn_n(n, i)));
250 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
253 /* repair the block visited flag from above misuse. Repair it in both
254 graphs so that the old one can still be used. */
255 set_Block_block_visited(nn, 0);
256 set_Block_block_visited(n, 0);
257 /* Local optimization could not merge two subsequent blocks if
258 in array contained Bads. Now it's possible.
259 We don't call optimize_in_place as it requires
260 that the fields in ir_graph are set properly. */
261 if ((get_opt_control_flow_straightening()) &&
262 (get_Block_n_cfgpreds(nn) == 1) &&
263 (intern_get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp))
264 exchange(nn, get_nodes_Block(get_Block_cfgpred(nn, 0)));
265 } else if (intern_get_irn_opcode(n) == iro_Phi) {
266 /* Don't copy node if corresponding predecessor in block is Bad.
267 The Block itself should not be Bad. */
268 block = get_nodes_Block(n);
269 set_irn_n (nn, -1, get_new_node(block));
271 irn_arity = intern_get_irn_arity(n);
272 for (i = 0; i < irn_arity; i++)
273 if (intern_get_irn_opcode(intern_get_irn_n(block, i)) != iro_Bad) {
274 set_irn_n (nn, j, get_new_node(intern_get_irn_n(n, i)));
275 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
278 /* If the pre walker reached this Phi after the post walker visited the
279 block block_visited is > 0. */
280 set_Block_block_visited(get_nodes_Block(n), 0);
281 /* Compacting the Phi's ins might generate Phis with only one
283 if (intern_get_irn_arity(n) == 1)
284 exchange(n, intern_get_irn_n(n, 0));
286 irn_arity = intern_get_irn_arity(n);
287 for (i = -1; i < irn_arity; i++)
288 set_irn_n (nn, i, get_new_node(intern_get_irn_n(n, i)));
290 /* Now the new node is complete. We can add it to the hash table for cse.
291 @@@ inlinening aborts if we identify End. Why? */
292 if(intern_get_irn_op(nn) != op_End)
293 add_identities (current_ir_graph->value_table, nn);
297 * Copies the graph recursively, compacts the keepalive of the end node.
301 ir_node *oe, *ne; /* old end, new end */
302 ir_node *ka; /* keep alive */
305 oe = get_irg_end(current_ir_graph);
306 /* copy the end node by hand, allocate dynamic in array! */
307 ne = new_ir_node(get_irn_dbg_info(oe),
314 /* Copy the attributes. Well, there might be some in the future... */
316 set_new_node(oe, ne);
318 /* copy the live nodes */
319 irg_walk(get_nodes_Block(oe), copy_node, copy_preds, NULL);
320 /* copy_preds for the end node ... */
321 set_nodes_Block(ne, get_new_node(get_nodes_Block(oe)));
323 /*- ... and now the keep alives. -*/
324 /* First pick the not marked block nodes and walk them. We must pick these
325 first as else we will oversee blocks reachable from Phis. */
326 irn_arity = intern_get_irn_arity(oe);
327 for (i = 0; i < irn_arity; i++) {
328 ka = intern_get_irn_intra_n(oe, i);
329 if ((intern_get_irn_op(ka) == op_Block) &&
330 (get_irn_visited(ka) < get_irg_visited(current_ir_graph))) {
331 /* We must keep the block alive and copy everything reachable */
332 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
333 irg_walk(ka, copy_node, copy_preds, NULL);
334 add_End_keepalive(ne, get_new_node(ka));
338 /* Now pick the Phis. Here we will keep all! */
339 irn_arity = intern_get_irn_arity(oe);
340 for (i = 0; i < irn_arity; i++) {
341 ka = intern_get_irn_intra_n(oe, i);
342 if ((intern_get_irn_op(ka) == op_Phi)) {
343 if (get_irn_visited(ka) < get_irg_visited(current_ir_graph)) {
344 /* We didn't copy the Phi yet. */
345 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
346 irg_walk(ka, copy_node, copy_preds, NULL);
348 add_End_keepalive(ne, get_new_node(ka));
354 * Copies the graph reachable from current_ir_graph->end to the obstack
355 * in current_ir_graph and fixes the environment.
356 * Then fixes the fields in current_ir_graph containing nodes of the
360 copy_graph_env (void) {
362 /* Not all nodes remembered in current_ir_graph might be reachable
363 from the end node. Assure their link is set to NULL, so that
364 we can test whether new nodes have been computed. */
365 set_irn_link(get_irg_frame (current_ir_graph), NULL);
366 set_irn_link(get_irg_globals(current_ir_graph), NULL);
367 set_irn_link(get_irg_args (current_ir_graph), NULL);
369 /* we use the block walk flag for removing Bads from Blocks ins. */
370 inc_irg_block_visited(current_ir_graph);
375 /* fix the fields in current_ir_graph */
376 old_end = get_irg_end(current_ir_graph);
377 set_irg_end (current_ir_graph, get_new_node(old_end));
378 set_irg_end_except (current_ir_graph, get_irg_end(current_ir_graph));
379 set_irg_end_reg (current_ir_graph, get_irg_end(current_ir_graph));
381 set_irg_end_block (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
382 if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
383 copy_node (get_irg_frame(current_ir_graph), NULL);
384 copy_preds(get_irg_frame(current_ir_graph), NULL);
386 if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
387 copy_node (get_irg_globals(current_ir_graph), NULL);
388 copy_preds(get_irg_globals(current_ir_graph), NULL);
390 if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
391 copy_node (get_irg_args(current_ir_graph), NULL);
392 copy_preds(get_irg_args(current_ir_graph), NULL);
394 set_irg_start (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
396 set_irg_start_block(current_ir_graph,
397 get_new_node(get_irg_start_block(current_ir_graph)));
398 set_irg_frame (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
399 set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
400 set_irg_args (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
401 if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
402 copy_node(get_irg_bad(current_ir_graph), NULL);
403 copy_preds(get_irg_bad(current_ir_graph), NULL);
405 set_irg_bad(current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
406 /* GL removed: we need unknown with mode for analyses.
407 if (get_irn_link(get_irg_unknown(current_ir_graph)) == NULL) {
408 copy_node(get_irg_unknown(current_ir_graph), NULL);
409 copy_preds(get_irg_unknown(current_ir_graph), NULL);
411 set_irg_unknown(current_ir_graph, get_new_node(get_irg_unknown(current_ir_graph)));
416 * Copies all reachable nodes to a new obstack. Removes bad inputs
417 * from block nodes and the corresponding inputs from Phi nodes.
418 * Merges single exit blocks with single entry blocks and removes
420 * Adds all new nodes to a new hash table for cse. Does not
421 * perform cse, so the hash table might contain common subexpressions.
423 /* Amroq call this emigrate() */
425 dead_node_elimination(ir_graph *irg) {
427 int rem_ipview = interprocedural_view;
428 struct obstack *graveyard_obst = NULL;
429 struct obstack *rebirth_obst = NULL;
431 stat_dead_node_elim_start(irg);
433 /* Remember external state of current_ir_graph. */
434 rem = current_ir_graph;
435 current_ir_graph = irg;
436 interprocedural_view = 0;
438 /* Handle graph state */
439 assert(get_irg_phase_state(current_ir_graph) != phase_building);
440 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
441 free_outs(current_ir_graph);
443 /* @@@ so far we loose loops when copying */
444 free_loop_information(current_ir_graph);
446 if (get_opt_optimize() && get_opt_dead_node_elimination()) {
448 /* A quiet place, where the old obstack can rest in peace,
449 until it will be cremated. */
450 graveyard_obst = irg->obst;
452 /* A new obstack, where the reachable nodes will be copied to. */
453 rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
454 current_ir_graph->obst = rebirth_obst;
455 obstack_init (current_ir_graph->obst);
457 /* We also need a new hash table for cse */
458 del_identities (irg->value_table);
459 irg->value_table = new_identities ();
461 /* Copy the graph from the old to the new obstack */
464 /* Free memory from old unoptimized obstack */
465 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
466 xfree (graveyard_obst); /* ... then free it. */
469 stat_dead_node_elim_stop(irg);
471 current_ir_graph = rem;
472 interprocedural_view = rem_ipview;
476 * Relink bad predeseccors of a block and store the old in array to the
477 * link field. This function is called by relink_bad_predecessors().
478 * The array of link field starts with the block operand at position 0.
479 * If block has bad predecessors, create a new in array without bad preds.
480 * Otherwise let in array untouched.
482 static void relink_bad_block_predecessors(ir_node *n, void *env) {
483 ir_node **new_in, *irn;
484 int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
486 /* if link field of block is NULL, look for bad predecessors otherwise
487 this is allready done */
488 if (intern_get_irn_op(n) == op_Block &&
489 get_irn_link(n) == NULL) {
491 /* save old predecessors in link field (position 0 is the block operand)*/
492 set_irn_link(n, (void *)get_irn_in(n));
494 /* count predecessors without bad nodes */
495 old_irn_arity = intern_get_irn_arity(n);
496 for (i = 0; i < old_irn_arity; i++)
497 if (!is_Bad(intern_get_irn_n(n, i))) new_irn_arity++;
499 /* arity changing: set new predecessors without bad nodes */
500 if (new_irn_arity < old_irn_arity) {
501 /* get new predecessor array without Block predecessor */
502 new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
504 /* set new predeseccors in array */
507 for (i = 1; i < old_irn_arity; i++) {
508 irn = intern_get_irn_n(n, i);
509 if (!is_Bad(irn)) new_in[new_irn_n++] = irn;
512 } /* ir node has bad predecessors */
514 } /* Block is not relinked */
518 * Relinks Bad predecesors from Bocks and Phis called by walker
519 * remove_bad_predecesors(). If n is a Block, call
520 * relink_bad_block_redecessors(). If n is a Phinode, call also the relinking
521 * function of Phi's Block. If this block has bad predecessors, relink preds
524 static void relink_bad_predecessors(ir_node *n, void *env) {
525 ir_node *block, **old_in;
526 int i, old_irn_arity, new_irn_arity;
528 /* relink bad predeseccors of a block */
529 if (intern_get_irn_op(n) == op_Block)
530 relink_bad_block_predecessors(n, env);
532 /* If Phi node relink its block and its predecessors */
533 if (intern_get_irn_op(n) == op_Phi) {
535 /* Relink predeseccors of phi's block */
536 block = get_nodes_Block(n);
537 if (get_irn_link(block) == NULL)
538 relink_bad_block_predecessors(block, env);
540 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
541 old_irn_arity = ARR_LEN(old_in);
543 /* Relink Phi predeseccors if count of predeseccors changed */
544 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
545 /* set new predeseccors in array
546 n->in[0] remains the same block */
548 for(i = 1; i < old_irn_arity; i++)
549 if (!is_Bad((ir_node *)old_in[i])) n->in[new_irn_arity++] = n->in[i];
551 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
554 } /* n is a Phi node */
558 * Removes Bad Bad predecesors from Blocks and the corresponding
559 * inputs to Phi nodes as in dead_node_elimination but without
561 * On walking up set the link field to NULL, on walking down call
562 * relink_bad_predecessors() (This function stores the old in array
563 * to the link field and sets a new in array if arity of predecessors
566 void remove_bad_predecessors(ir_graph *irg) {
567 irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
571 /*--------------------------------------------------------------------*/
572 /* Funcionality for inlining */
573 /*--------------------------------------------------------------------*/
576 * Copy node for inlineing. Updates attributes that change when
577 * inlineing but not for dead node elimination.
579 * Copies the node by calling copy_node and then updates the entity if
580 * it's a local one. env must be a pointer of the frame type of the
581 * inlined procedure. The new entities must be in the link field of
585 copy_node_inline (ir_node *n, void *env) {
587 type *frame_tp = (type *)env;
590 if (intern_get_irn_op(n) == op_Sel) {
591 new = get_new_node (n);
592 assert(intern_get_irn_op(new) == op_Sel);
593 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
594 set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
596 } else if (intern_get_irn_op(n) == op_Block) {
597 new = get_new_node (n);
598 new->attr.block.irg = current_ir_graph;
602 static void find_addr(ir_node *node, void *env)
604 if (get_irn_opcode(node) == iro_Proj) {
605 if (get_Proj_proj(node) == pn_Start_P_value_arg_base)
611 * currently, we cannot inline two cases:
612 * - call with compound arguments
613 * - graphs that take the address of a parameter
615 * check these condition here
617 static int can_inline(ir_node *call, ir_graph *called_graph)
619 type *call_type = get_Call_type(call);
620 int params, ress, i, res;
622 assert(is_method_type(call_type));
624 params = get_method_n_params(call_type);
625 ress = get_method_n_ress(call_type);
628 for (i = 0; i < params; ++i) {
629 type *p_type = get_method_param_type(call_type, i);
631 if (is_compound_type(p_type))
636 for (i = 0; i < ress; ++i) {
637 type *r_type = get_method_res_type(call_type, i);
639 if (is_compound_type(r_type))
644 irg_walk_graph(called_graph, find_addr, NULL, &res);
649 void inline_method(ir_node *call, ir_graph *called_graph) {
651 ir_node *post_call, *post_bl;
653 ir_node *end, *end_bl;
657 int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
660 irg_inline_property prop = get_irg_inline_property(called_graph);
662 if ( (prop != irg_inline_forced) && (!get_opt_optimize() || !get_opt_inline() ||
663 (prop == irg_inline_forbidden))) return;
667 * currently, we cannot inline two cases:
668 * - call with compound arguments
669 * - graphs that take the address of a parameter
671 if (! can_inline(call, called_graph))
674 /* -- Turn off optimizations, this can cause problems when allocating new nodes. -- */
675 rem_opt = get_opt_optimize();
678 /* Handle graph state */
679 assert(get_irg_phase_state(current_ir_graph) != phase_building);
680 assert(get_irg_pinned(current_ir_graph) == pinned);
681 assert(get_irg_pinned(called_graph) == pinned);
682 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
683 set_irg_outs_inconsistent(current_ir_graph);
685 /* -- Check preconditions -- */
686 assert(get_irn_op(call) == op_Call);
687 /* @@@ does not work for InterfaceIII.java after cgana
688 assert(get_Call_type(call) == get_entity_type(get_irg_ent(called_graph)));
689 assert(smaller_type(get_entity_type(get_irg_ent(called_graph)),
690 get_Call_type(call)));
692 assert(get_type_tpop(get_Call_type(call)) == type_method);
693 if (called_graph == current_ir_graph) {
694 set_optimize(rem_opt);
698 /* here we know we WILL inline, so inform the statistics */
699 stat_inline(call, called_graph);
701 /* -- Decide how to handle exception control flow: Is there a handler
702 for the Call node, or do we branch directly to End on an exception?
703 exc_handling: 0 There is a handler.
705 2 Exception handling not represented in Firm. -- */
707 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
708 for (proj = (ir_node *)get_irn_link(call); proj; proj = (ir_node *)get_irn_link(proj)) {
709 assert(get_irn_op(proj) == op_Proj);
710 if (get_Proj_proj(proj) == pn_Call_X_except) Xproj = proj;
711 if (get_Proj_proj(proj) == pn_Call_M_except) Mproj = proj;
713 if (Mproj) { assert(Xproj); exc_handling = 0; } /* Mproj */
714 else if (Xproj) { exc_handling = 1; } /* !Mproj && Xproj */
715 else { exc_handling = 2; } /* !Mproj && !Xproj */
720 the procedure and later replaces the Start node of the called graph.
721 Post_call is the old Call node and collects the results of the called
722 graph. Both will end up being a tuple. -- */
723 post_bl = get_nodes_Block(call);
724 set_irg_current_block(current_ir_graph, post_bl);
725 /* XxMxPxP of Start + parameter of Call */
727 in[1] = get_Call_mem(call);
728 in[2] = get_irg_frame(current_ir_graph);
729 in[3] = get_irg_globals(current_ir_graph);
730 in[4] = new_Tuple (get_Call_n_params(call), get_Call_param_arr(call));
731 pre_call = new_Tuple(5, in);
735 The new block gets the ins of the old block, pre_call and all its
736 predecessors and all Phi nodes. -- */
737 part_block(pre_call);
739 /* -- Prepare state for dead node elimination -- */
740 /* Visited flags in calling irg must be >= flag in called irg.
741 Else walker and arity computation will not work. */
742 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
743 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
744 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
745 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
746 /* Set pre_call as new Start node in link field of the start node of
747 calling graph and pre_calls block as new block for the start block
749 Further mark these nodes so that they are not visited by the
751 set_irn_link(get_irg_start(called_graph), pre_call);
752 set_irn_visited(get_irg_start(called_graph),
753 get_irg_visited(current_ir_graph));
754 set_irn_link(get_irg_start_block(called_graph),
755 get_nodes_Block(pre_call));
756 set_irn_visited(get_irg_start_block(called_graph),
757 get_irg_visited(current_ir_graph));
759 /* Initialize for compaction of in arrays */
760 inc_irg_block_visited(current_ir_graph);
762 /* -- Replicate local entities of the called_graph -- */
763 /* copy the entities. */
764 called_frame = get_irg_frame_type(called_graph);
765 for (i = 0; i < get_class_n_members(called_frame); i++) {
766 entity *new_ent, *old_ent;
767 old_ent = get_class_member(called_frame, i);
768 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
769 set_entity_link(old_ent, new_ent);
772 /* visited is > than that of called graph. With this trick visited will
773 remain unchanged so that an outer walker, e.g., searching the call nodes
774 to inline, calling this inline will not visit the inlined nodes. */
775 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
777 /* -- Performing dead node elimination inlines the graph -- */
778 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
780 /* @@@ endless loops are not copied!! -- they should be, I think... */
781 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
782 get_irg_frame_type(called_graph));
784 /* Repair called_graph */
785 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
786 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
787 set_Block_block_visited(get_irg_start_block(called_graph), 0);
789 /* -- Merge the end of the inlined procedure with the call site -- */
790 /* We will turn the old Call node into a Tuple with the following
793 0: Phi of all Memories of Return statements.
794 1: Jmp from new Block that merges the control flow from all exception
795 predecessors of the old end block.
796 2: Tuple of all arguments.
797 3: Phi of Exception memories.
798 In case the old Call directly branches to End on an exception we don't
799 need the block merging all exceptions nor the Phi of the exception
803 /* -- Precompute some values -- */
804 end_bl = get_new_node(get_irg_end_block(called_graph));
805 end = get_new_node(get_irg_end(called_graph));
806 arity = intern_get_irn_arity(end_bl); /* arity = n_exc + n_ret */
807 n_res = get_method_n_ress(get_Call_type(call));
809 res_pred = (ir_node **) malloc (n_res * sizeof (ir_node *));
810 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
812 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
814 /* -- archive keepalives -- */
815 irn_arity = intern_get_irn_arity(end);
816 for (i = 0; i < irn_arity; i++)
817 add_End_keepalive(get_irg_end(current_ir_graph), intern_get_irn_n(end, i));
819 /* The new end node will die. We need not free as the in array is on the obstack:
820 copy_node only generated 'D' arrays. */
822 /* -- Replace Return nodes by Jump nodes. -- */
824 for (i = 0; i < arity; i++) {
826 ret = intern_get_irn_n(end_bl, i);
827 if (intern_get_irn_op(ret) == op_Return) {
828 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_Block(ret));
832 set_irn_in(post_bl, n_ret, cf_pred);
834 /* -- Build a Tuple for all results of the method.
835 Add Phi node if there was more than one Return. -- */
836 turn_into_tuple(post_call, 4);
837 /* First the Memory-Phi */
839 for (i = 0; i < arity; i++) {
840 ret = intern_get_irn_n(end_bl, i);
841 if (intern_get_irn_op(ret) == op_Return) {
842 cf_pred[n_ret] = get_Return_mem(ret);
846 phi = new_Phi(n_ret, cf_pred, mode_M);
847 set_Tuple_pred(call, 0, phi);
848 /* Conserve Phi-list for further inlinings -- but might be optimized */
849 if (get_nodes_Block(phi) == post_bl) {
850 set_irn_link(phi, get_irn_link(post_bl));
851 set_irn_link(post_bl, phi);
853 /* Now the real results */
855 for (j = 0; j < n_res; j++) {
857 for (i = 0; i < arity; i++) {
858 ret = intern_get_irn_n(end_bl, i);
859 if (intern_get_irn_op(ret) == op_Return) {
860 cf_pred[n_ret] = get_Return_res(ret, j);
864 phi = new_Phi(n_ret, cf_pred, intern_get_irn_mode(cf_pred[0]));
866 /* Conserve Phi-list for further inlinings -- but might be optimized */
867 if (get_nodes_Block(phi) == post_bl) {
868 set_irn_link(phi, get_irn_link(post_bl));
869 set_irn_link(post_bl, phi);
872 set_Tuple_pred(call, 2, new_Tuple(n_res, res_pred));
874 set_Tuple_pred(call, 2, new_Bad());
876 /* Finally the exception control flow.
877 We have two (three) possible situations:
878 First if the Call branches to an exception handler: We need to add a Phi node to
879 collect the memory containing the exception objects. Further we need
880 to add another block to get a correct representation of this Phi. To
881 this block we add a Jmp that resolves into the X output of the Call
882 when the Call is turned into a tuple.
883 Second the Call branches to End, the exception is not handled. Just
884 add all inlined exception branches to the End node.
885 Third: there is no Exception edge at all. Handle as case two. */
886 if (exc_handling == 0) {
888 for (i = 0; i < arity; i++) {
890 ret = intern_get_irn_n(end_bl, i);
891 if (is_fragile_op(skip_Proj(ret)) || (intern_get_irn_op(skip_Proj(ret)) == op_Raise)) {
892 cf_pred[n_exc] = ret;
897 new_Block(n_exc, cf_pred); /* watch it: current_block is changed! */
898 set_Tuple_pred(call, 1, new_Jmp());
899 /* The Phi for the memories with the exception objects */
901 for (i = 0; i < arity; i++) {
903 ret = skip_Proj(intern_get_irn_n(end_bl, i));
904 if (intern_get_irn_op(ret) == op_Call) {
905 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 3);
907 } else if (is_fragile_op(ret)) {
908 /* We rely that all cfops have the memory output at the same position. */
909 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 0);
911 } else if (intern_get_irn_op(ret) == op_Raise) {
912 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 1);
916 set_Tuple_pred(call, 3, new_Phi(n_exc, cf_pred, mode_M));
918 set_Tuple_pred(call, 1, new_Bad());
919 set_Tuple_pred(call, 3, new_Bad());
922 ir_node *main_end_bl;
923 int main_end_bl_arity;
926 /* assert(exc_handling == 1 || no exceptions. ) */
928 for (i = 0; i < arity; i++) {
929 ir_node *ret = intern_get_irn_n(end_bl, i);
931 if (is_fragile_op(skip_Proj(ret)) || (intern_get_irn_op(skip_Proj(ret)) == op_Raise)) {
932 cf_pred[n_exc] = ret;
936 main_end_bl = get_irg_end_block(current_ir_graph);
937 main_end_bl_arity = intern_get_irn_arity(main_end_bl);
938 end_preds = (ir_node **) malloc ((n_exc + main_end_bl_arity) * sizeof (ir_node *));
940 for (i = 0; i < main_end_bl_arity; ++i)
941 end_preds[i] = intern_get_irn_n(main_end_bl, i);
942 for (i = 0; i < n_exc; ++i)
943 end_preds[main_end_bl_arity + i] = cf_pred[i];
944 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
945 set_Tuple_pred(call, 1, new_Bad());
946 set_Tuple_pred(call, 3, new_Bad());
952 #if 0 /* old. now better, correcter, faster implementation. */
954 /* -- If the exception control flow from the inlined Call directly
955 branched to the end block we now have the following control
956 flow predecessor pattern: ProjX -> Tuple -> Jmp. We must
957 remove the Jmp along with it's empty block and add Jmp's
958 predecessors as predecessors of this end block. No problem if
959 there is no exception, because then branches Bad to End which
961 @@@ can't we know this beforehand: by getting the Proj(1) from
962 the Call link list and checking whether it goes to Proj. */
963 /* find the problematic predecessor of the end block. */
964 end_bl = get_irg_end_block(current_ir_graph);
965 for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
966 cf_op = get_Block_cfgpred(end_bl, i);
967 if (intern_get_irn_op(cf_op) == op_Proj) {
968 cf_op = get_Proj_pred(cf_op);
969 if ((intern_get_irn_op(cf_op) == op_Tuple) && (cf_op == call)) {
970 /* There are unoptimized tuples from inlineing before when no exc */
971 assert(get_Proj_proj(get_Block_cfgpred(end_bl, i)) == pn_Call_X_except);
972 cf_op = get_Tuple_pred(cf_op, pn_Call_X_except);
973 assert(intern_get_irn_op(cf_op) == op_Jmp);
979 if (i < get_Block_n_cfgpreds(end_bl)) {
980 bl = get_nodes_Block(cf_op);
981 arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
982 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
983 for (j = 0; j < i; j++)
984 cf_pred[j] = get_Block_cfgpred(end_bl, j);
985 for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
986 cf_pred[j] = get_Block_cfgpred(bl, j-i);
987 for (j = j; j < arity; j++)
988 cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
989 set_irn_in(end_bl, arity, cf_pred);
991 /* Remove the exception pred from post-call Tuple. */
992 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
997 /* -- Turn cse back on. -- */
998 set_optimize(rem_opt);
1001 /********************************************************************/
1002 /* Apply inlineing to small methods. */
1003 /********************************************************************/
1007 /* It makes no sense to inline too many calls in one procedure. Anyways,
1008 I didn't get a version with NEW_ARR_F to run. */
1009 #define MAX_INLINE 1024
1012 * Returns the irg called from a Call node. If the irg is not
1013 * known, NULL is returned.
1015 static ir_graph *get_call_called_irg(ir_node *call) {
1018 ir_graph *called_irg = NULL;
1020 assert(get_irn_op(call) == op_Call);
1022 addr = get_Call_ptr(call);
1023 if (intern_get_irn_op(addr) == op_Const) {
1024 /* Check whether the constant is the pointer to a compiled entity. */
1025 tv = get_Const_tarval(addr);
1026 if (tarval_to_entity(tv))
1027 called_irg = get_entity_irg(tarval_to_entity(tv));
1032 static void collect_calls(ir_node *call, void *env) {
1034 ir_node **calls = (ir_node **)env;
1037 ir_graph *called_irg;
1039 if (intern_get_irn_op(call) != op_Call) return;
1041 addr = get_Call_ptr(call);
1042 if (intern_get_irn_op(addr) == op_Const) {
1043 /* Check whether the constant is the pointer to a compiled entity. */
1044 tv = get_Const_tarval(addr);
1045 if (tarval_to_entity(tv)) {
1046 called_irg = get_entity_irg(tarval_to_entity(tv));
1047 if (called_irg && pos < MAX_INLINE) {
1048 /* The Call node calls a locally defined method. Remember to inline. */
1057 * Inlines all small methods at call sites where the called address comes
1058 * from a Const node that references the entity representing the called
1060 * The size argument is a rough measure for the code size of the method:
1061 * Methods where the obstack containing the firm graph is smaller than
1064 void inline_small_irgs(ir_graph *irg, int size) {
1066 ir_node *calls[MAX_INLINE];
1067 ir_graph *rem = current_ir_graph;
1069 if (!(get_opt_optimize() && get_opt_inline())) return;
1071 current_ir_graph = irg;
1072 /* Handle graph state */
1073 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1074 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1076 /* Find Call nodes to inline.
1077 (We can not inline during a walk of the graph, as inlineing the same
1078 method several times changes the visited flag of the walked graph:
1079 after the first inlineing visited of the callee equals visited of
1080 the caller. With the next inlineing both are increased.) */
1082 irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
1084 if ((pos > 0) && (pos < MAX_INLINE)) {
1085 /* There are calls to inline */
1086 collect_phiprojs(irg);
1087 for (i = 0; i < pos; i++) {
1090 tv = get_Const_tarval(get_Call_ptr(calls[i]));
1091 callee = get_entity_irg(tarval_to_entity(tv));
1092 if (((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) ||
1093 (get_irg_inline_property(callee) == irg_inline_forced)) {
1094 inline_method(calls[i], callee);
1099 current_ir_graph = rem;
1103 * Environment for inlining irgs.
1106 int n_nodes; /**< Nodes in graph except Id, Tuple, Proj, Start, End */
1107 int n_nodes_orig; /**< for statistics */
1108 eset *call_nodes; /**< All call nodes in this graph */
1110 int n_call_nodes_orig; /**< for statistics */
1111 int n_callers; /**< Number of known graphs that call this graphs. */
1112 int n_callers_orig; /**< for statistics */
1115 static inline_irg_env *new_inline_irg_env(void) {
1116 inline_irg_env *env = malloc(sizeof(inline_irg_env));
1117 env->n_nodes = -2; /* uncount Start, End */
1118 env->n_nodes_orig = -2; /* uncount Start, End */
1119 env->call_nodes = eset_create();
1120 env->n_call_nodes = 0;
1121 env->n_call_nodes_orig = 0;
1123 env->n_callers_orig = 0;
1127 static void free_inline_irg_env(inline_irg_env *env) {
1128 eset_destroy(env->call_nodes);
1132 static void collect_calls2(ir_node *call, void *env) {
1133 inline_irg_env *x = (inline_irg_env *)env;
1134 ir_op *op = intern_get_irn_op(call);
1137 /* count nodes in irg */
1138 if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1143 if (op != op_Call) return;
1145 /* collect all call nodes */
1146 eset_insert(x->call_nodes, (void *)call);
1148 x->n_call_nodes_orig++;
1150 /* count all static callers */
1151 callee = get_call_called_irg(call);
1153 ((inline_irg_env *)get_irg_link(callee))->n_callers++;
1154 ((inline_irg_env *)get_irg_link(callee))->n_callers_orig++;
1158 INLINE static int is_leave(ir_graph *irg) {
1159 return (((inline_irg_env *)get_irg_link(irg))->n_call_nodes == 0);
1162 INLINE static int is_smaller(ir_graph *callee, int size) {
1163 return (((inline_irg_env *)get_irg_link(callee))->n_nodes < size);
1168 * Inlines small leave methods at call sites where the called address comes
1169 * from a Const node that references the entity representing the called
1171 * The size argument is a rough measure for the code size of the method:
1172 * Methods where the obstack containing the firm graph is smaller than
1175 void inline_leave_functions(int maxsize, int leavesize, int size) {
1176 inline_irg_env *env;
1177 int i, n_irgs = get_irp_n_irgs();
1178 ir_graph *rem = current_ir_graph;
1181 if (!(get_opt_optimize() && get_opt_inline())) return;
1183 /* extend all irgs by a temporary data structure for inlineing. */
1184 for (i = 0; i < n_irgs; ++i)
1185 set_irg_link(get_irp_irg(i), new_inline_irg_env());
1187 /* Precompute information in temporary data structure. */
1188 for (i = 0; i < n_irgs; ++i) {
1189 current_ir_graph = get_irp_irg(i);
1190 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1191 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1193 irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
1194 get_irg_link(current_ir_graph));
1198 Inline leaves recursively -- we might construct new leaves. */
1199 /* int itercnt = 1; */
1200 while (did_inline) {
1201 /* printf("iteration %d\n", itercnt++); */
1203 for (i = 0; i < n_irgs; ++i) {
1206 int phiproj_computed = 0;
1208 current_ir_graph = get_irp_irg(i);
1209 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1211 /* we can not walk and change a set, nor remove from it.
1213 walkset = env->call_nodes;
1214 env->call_nodes = eset_create();
1215 for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1216 inline_irg_env *callee_env;
1217 ir_graph *callee = get_call_called_irg(call);
1219 if (env->n_nodes > maxsize) break;
1221 ((is_leave(callee) && is_smaller(callee, leavesize)) ||
1222 (get_irg_inline_property(callee) == irg_inline_forced))) {
1223 if (!phiproj_computed) {
1224 phiproj_computed = 1;
1225 collect_phiprojs(current_ir_graph);
1227 callee_env = (inline_irg_env *)get_irg_link(callee);
1228 /* printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)), */
1229 /* get_entity_name(get_irg_entity(callee))); */
1230 inline_method(call, callee);
1232 env->n_call_nodes--;
1233 eset_insert_all(env->call_nodes, callee_env->call_nodes);
1234 env->n_call_nodes += callee_env->n_call_nodes;
1235 env->n_nodes += callee_env->n_nodes;
1236 callee_env->n_callers--;
1238 eset_insert(env->call_nodes, call);
1241 eset_destroy(walkset);
1245 /* printf("Non leaves\n"); */
1246 /* inline other small functions. */
1247 for (i = 0; i < n_irgs; ++i) {
1250 int phiproj_computed = 0;
1252 current_ir_graph = get_irp_irg(i);
1253 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1255 /* we can not walk and change a set, nor remove from it.
1257 walkset = env->call_nodes;
1258 env->call_nodes = eset_create();
1259 for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1260 inline_irg_env *callee_env;
1261 ir_graph *callee = get_call_called_irg(call);
1263 if (env->n_nodes > maxsize) break;
1264 if (callee && is_smaller(callee, size)) {
1265 if (!phiproj_computed) {
1266 phiproj_computed = 1;
1267 collect_phiprojs(current_ir_graph);
1269 callee_env = (inline_irg_env *)get_irg_link(callee);
1270 /* printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)), */
1271 /* get_entity_name(get_irg_entity(callee))); */
1272 inline_method(call, callee);
1274 env->n_call_nodes--;
1275 eset_insert_all(env->call_nodes, callee_env->call_nodes);
1276 env->n_call_nodes += callee_env->n_call_nodes;
1277 env->n_nodes += callee_env->n_nodes;
1278 callee_env->n_callers--;
1280 eset_insert(env->call_nodes, call);
1283 eset_destroy(walkset);
1286 for (i = 0; i < n_irgs; ++i) {
1287 current_ir_graph = get_irp_irg(i);
1289 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1290 if ((env->n_call_nodes_orig != env->n_call_nodes) ||
1291 (env->n_callers_orig != env->n_callers))
1292 printf("Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1293 env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1294 env->n_callers_orig, env->n_callers,
1295 get_entity_name(get_irg_entity(current_ir_graph)));
1297 free_inline_irg_env((inline_irg_env *)get_irg_link(current_ir_graph));
1300 current_ir_graph = rem;
1303 /*-----------------------------------------------------------------*/
1304 /* Code Placement. Pins all floating nodes to a block where they */
1305 /* will be executed only if needed. */
1306 /*-----------------------------------------------------------------*/
1309 * Find the earliest correct block for N. --- Place N into the
1310 * same Block as its dominance-deepest Input.
1313 place_floats_early(ir_node *n, pdeq *worklist)
1315 int i, start, irn_arity;
1317 /* we must not run into an infinite loop */
1318 assert (irn_not_visited(n));
1319 mark_irn_visited(n);
1321 /* Place floating nodes. */
1322 if (get_op_pinned(intern_get_irn_op(n)) == floats) {
1324 ir_node *b = new_Bad(); /* The block to place this node in */
1326 assert(intern_get_irn_op(n) != op_Block);
1328 if ((intern_get_irn_op(n) == op_Const) ||
1329 (intern_get_irn_op(n) == op_SymConst) ||
1331 (intern_get_irn_op(n) == op_Unknown)) {
1332 /* These nodes will not be placed by the loop below. */
1333 b = get_irg_start_block(current_ir_graph);
1337 /* find the block for this node. */
1338 irn_arity = intern_get_irn_arity(n);
1339 for (i = 0; i < irn_arity; i++) {
1340 ir_node *dep = intern_get_irn_n(n, i);
1342 if ((irn_not_visited(dep)) &&
1343 (get_op_pinned(intern_get_irn_op(dep)) == floats)) {
1344 place_floats_early(dep, worklist);
1346 /* Because all loops contain at least one pinned node, now all
1347 our inputs are either pinned or place_early has already
1348 been finished on them. We do not have any unfinished inputs! */
1349 dep_block = get_nodes_Block(dep);
1350 if ((!is_Bad(dep_block)) &&
1351 (get_Block_dom_depth(dep_block) > depth)) {
1353 depth = get_Block_dom_depth(dep_block);
1355 /* Avoid that the node is placed in the Start block */
1356 if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
1357 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1358 assert(b != get_irg_start_block(current_ir_graph));
1362 set_nodes_Block(n, b);
1365 /* Add predecessors of non floating nodes on worklist. */
1366 start = (intern_get_irn_op(n) == op_Block) ? 0 : -1;
1367 irn_arity = intern_get_irn_arity(n);
1368 for (i = start; i < irn_arity; i++) {
1369 ir_node *pred = intern_get_irn_n(n, i);
1370 if (irn_not_visited(pred)) {
1371 pdeq_putr (worklist, pred);
1377 * Floating nodes form subgraphs that begin at nodes as Const, Load,
1378 * Start, Call and end at pinned nodes as Store, Call. Place_early
1379 * places all floating nodes reachable from its argument through floating
1380 * nodes and adds all beginnings at pinned nodes to the worklist.
1382 static INLINE void place_early(pdeq* worklist) {
1384 inc_irg_visited(current_ir_graph);
1386 /* this inits the worklist */
1387 place_floats_early(get_irg_end(current_ir_graph), worklist);
1389 /* Work the content of the worklist. */
1390 while (!pdeq_empty (worklist)) {
1391 ir_node *n = pdeq_getl (worklist);
1392 if (irn_not_visited(n)) place_floats_early(n, worklist);
1395 set_irg_outs_inconsistent(current_ir_graph);
1396 current_ir_graph->pinned = pinned;
1400 /** deepest common dominance ancestor of DCA and CONSUMER of PRODUCER. */
1402 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
1404 ir_node *block = NULL;
1406 /* Compute the latest block into which we can place a node so that it is
1408 if (intern_get_irn_op(consumer) == op_Phi) {
1409 /* our consumer is a Phi-node, the effective use is in all those
1410 blocks through which the Phi-node reaches producer */
1412 ir_node *phi_block = get_nodes_Block(consumer);
1413 irn_arity = intern_get_irn_arity(consumer);
1414 for (i = 0; i < irn_arity; i++) {
1415 if (intern_get_irn_n(consumer, i) == producer) {
1416 block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
1420 assert(is_no_Block(consumer));
1421 block = get_nodes_Block(consumer);
1424 /* Compute the deepest common ancestor of block and dca. */
1426 if (!dca) return block;
1427 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1428 block = get_Block_idom(block);
1429 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
1430 dca = get_Block_idom(dca);
1431 while (block != dca)
1432 { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1437 static INLINE int get_irn_loop_depth(ir_node *n) {
1438 return get_loop_depth(get_irn_loop(n));
1442 * Move n to a block with less loop depth than it's current block. The
1443 * new block must be dominated by early.
1446 move_out_of_loops (ir_node *n, ir_node *early)
1448 ir_node *best, *dca;
1452 /* Find the region deepest in the dominator tree dominating
1453 dca with the least loop nesting depth, but still dominated
1454 by our early placement. */
1455 dca = get_nodes_Block(n);
1457 while (dca != early) {
1458 dca = get_Block_idom(dca);
1459 if (!dca) break; /* should we put assert(dca)? */
1460 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1464 if (best != get_nodes_Block(n)) {
1466 printf("Moving out of loop: "); DDMN(n);
1467 printf(" Outermost block: "); DDMN(early);
1468 printf(" Best block: "); DDMN(best);
1469 printf(" Innermost block: "); DDMN(get_nodes_Block(n));
1471 set_nodes_Block(n, best);
1476 * Find the latest legal block for N and place N into the
1477 * `optimal' Block between the latest and earliest legal block.
1478 * The `optimal' block is the dominance-deepest block of those
1479 * with the least loop-nesting-depth. This places N out of as many
1480 * loops as possible and then makes it as control dependant as
1484 place_floats_late(ir_node *n, pdeq *worklist)
1489 assert (irn_not_visited(n)); /* no multiple placement */
1491 /* no need to place block nodes, control nodes are already placed. */
1492 if ((intern_get_irn_op(n) != op_Block) &&
1494 (intern_get_irn_mode(n) != mode_X)) {
1495 /* Remember the early placement of this block to move it
1496 out of loop no further than the early placement. */
1497 early = get_nodes_Block(n);
1498 /* Assure that our users are all placed, except the Phi-nodes.
1499 --- Each data flow cycle contains at least one Phi-node. We
1500 have to break the `user has to be placed before the
1501 producer' dependence cycle and the Phi-nodes are the
1502 place to do so, because we need to base our placement on the
1503 final region of our users, which is OK with Phi-nodes, as they
1504 are pinned, and they never have to be placed after a
1505 producer of one of their inputs in the same block anyway. */
1506 for (i = 0; i < get_irn_n_outs(n); i++) {
1507 ir_node *succ = get_irn_out(n, i);
1508 if (irn_not_visited(succ) && (intern_get_irn_op(succ) != op_Phi))
1509 place_floats_late(succ, worklist);
1512 /* We have to determine the final block of this node... except for
1514 if ((get_op_pinned(intern_get_irn_op(n)) == floats) &&
1515 (intern_get_irn_op(n) != op_Const) &&
1516 (intern_get_irn_op(n) != op_SymConst)) {
1517 ir_node *dca = NULL; /* deepest common ancestor in the
1518 dominator tree of all nodes'
1519 blocks depending on us; our final
1520 placement has to dominate DCA. */
1521 for (i = 0; i < get_irn_n_outs(n); i++) {
1522 dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
1524 set_nodes_Block(n, dca);
1526 move_out_of_loops (n, early);
1530 mark_irn_visited(n);
1532 /* Add predecessors of all non-floating nodes on list. (Those of floating
1533 nodes are placeded already and therefore are marked.) */
1534 for (i = 0; i < get_irn_n_outs(n); i++) {
1535 if (irn_not_visited(get_irn_out(n, i))) {
1536 pdeq_putr (worklist, get_irn_out(n, i));
1541 static INLINE void place_late(pdeq* worklist) {
1543 inc_irg_visited(current_ir_graph);
1545 /* This fills the worklist initially. */
1546 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
1547 /* And now empty the worklist again... */
1548 while (!pdeq_empty (worklist)) {
1549 ir_node *n = pdeq_getl (worklist);
1550 if (irn_not_visited(n)) place_floats_late(n, worklist);
1554 void place_code(ir_graph *irg) {
1556 ir_graph *rem = current_ir_graph;
1558 current_ir_graph = irg;
1560 if (!(get_opt_optimize() && get_opt_global_cse())) return;
1562 /* Handle graph state */
1563 assert(get_irg_phase_state(irg) != phase_building);
1564 if (get_irg_dom_state(irg) != dom_consistent)
1567 if (get_irg_loopinfo_state(irg) != loopinfo_consistent) {
1568 free_loop_information(irg);
1569 construct_backedges(irg);
1572 /* Place all floating nodes as early as possible. This guarantees
1573 a legal code placement. */
1574 worklist = new_pdeq();
1575 place_early(worklist);
1577 /* place_early invalidates the outs, place_late needs them. */
1579 /* Now move the nodes down in the dominator tree. This reduces the
1580 unnecessary executions of the node. */
1581 place_late(worklist);
1583 set_irg_outs_inconsistent(current_ir_graph);
1584 set_irg_loopinfo_inconsistent(current_ir_graph);
1586 current_ir_graph = rem;
1591 /********************************************************************/
1592 /* Control flow optimization. */
1593 /* Removes Bad control flow predecessors and empty blocks. A block */
1594 /* is empty if it contains only a Jmp node. */
1595 /* Blocks can only be removed if they are not needed for the */
1596 /* semantics of Phi nodes. */
1597 /********************************************************************/
1600 * Removes Tuples from Block control flow predecessors.
1601 * Optimizes blocks with equivalent_node().
1602 * Replaces n by Bad if n is unreachable control flow.
1604 static void merge_blocks(ir_node *n, void *env) {
1606 set_irn_link(n, NULL);
1608 if (intern_get_irn_op(n) == op_Block) {
1610 for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1611 /* GL @@@ : is this possible? if (get_opt_normalize()) -- added, all tests go through.
1612 A different order of optimizations might cause problems. */
1613 if (get_opt_normalize())
1614 set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1615 } else if (get_opt_optimize() && (intern_get_irn_mode(n) == mode_X)) {
1616 /* We will soon visit a block. Optimize it before visiting! */
1617 ir_node *b = get_nodes_Block(n);
1618 ir_node *new_node = equivalent_node(b);
1619 while (irn_not_visited(b) && (!is_Bad(new_node)) && (new_node != b)) {
1620 /* We would have to run gigo if new is bad, so we
1621 promote it directly below. */
1622 assert(((b == new_node) ||
1623 get_opt_control_flow_straightening() ||
1624 get_opt_control_flow_weak_simplification()) &&
1625 ("strange flag setting"));
1626 exchange (b, new_node);
1628 new_node = equivalent_node(b);
1630 /* GL @@@ get_opt_normalize hinzugefuegt, 5.5.2003 */
1631 if (is_Bad(new_node) && get_opt_normalize()) exchange(n, new_Bad());
1636 * Collects all Phi nodes in link list of Block.
1637 * Marks all blocks "block_visited" if they contain a node other
1640 static void collect_nodes(ir_node *n, void *env) {
1641 if (is_no_Block(n)) {
1642 ir_node *b = get_nodes_Block(n);
1644 if ((intern_get_irn_op(n) == op_Phi)) {
1645 /* Collect Phi nodes to compact ins along with block's ins. */
1646 set_irn_link(n, get_irn_link(b));
1648 } else if (intern_get_irn_op(n) != op_Jmp) { /* Check for non empty block. */
1649 mark_Block_block_visited(b);
1654 /** Returns true if pred is predecessor of block. */
1655 static int is_pred_of(ir_node *pred, ir_node *b) {
1657 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1658 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1659 if (b_pred == pred) return 1;
1664 static int test_whether_dispensable(ir_node *b, int pos) {
1665 int i, j, n_preds = 1;
1666 int dispensable = 1;
1667 ir_node *cfop = get_Block_cfgpred(b, pos);
1668 ir_node *pred = get_nodes_Block(cfop);
1670 if (get_Block_block_visited(pred) + 1
1671 < get_irg_block_visited(current_ir_graph)) {
1672 if (!get_opt_optimize() || !get_opt_control_flow_strong_simplification()) {
1673 /* Mark block so that is will not be removed. */
1674 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1677 /* Seems to be empty. */
1678 if (!get_irn_link(b)) {
1679 /* There are no Phi nodes ==> dispensable. */
1680 n_preds = get_Block_n_cfgpreds(pred);
1682 /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1683 Work preds < pos as if they were already removed. */
1684 for (i = 0; i < pos; i++) {
1685 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1686 if (get_Block_block_visited(b_pred) + 1
1687 < get_irg_block_visited(current_ir_graph)) {
1688 for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1689 ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1690 if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1693 if (is_pred_of(b_pred, pred)) dispensable = 0;
1696 for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1697 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1698 if (is_pred_of(b_pred, pred)) dispensable = 0;
1701 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1704 n_preds = get_Block_n_cfgpreds(pred);
1712 static void optimize_blocks(ir_node *b, void *env) {
1713 int i, j, k, max_preds, n_preds;
1714 ir_node *pred, *phi;
1717 /* Count the number of predecessor if this block is merged with pred blocks
1720 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1721 max_preds += test_whether_dispensable(b, i);
1723 in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1726 printf(" working on "); DDMN(b);
1727 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1728 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1729 if (is_Bad(get_Block_cfgpred(b, i))) {
1730 printf(" removing Bad %i\n ", i);
1731 } else if (get_Block_block_visited(pred) +1
1732 < get_irg_block_visited(current_ir_graph)) {
1733 printf(" removing pred %i ", i); DDMN(pred);
1734 } else { printf(" Nothing to do for "); DDMN(pred); }
1736 * end Debug output -*/
1738 /*- Fix the Phi nodes -*/
1739 phi = get_irn_link(b);
1741 assert(intern_get_irn_op(phi) == op_Phi);
1742 /* Find the new predecessors for the Phi */
1744 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1745 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1746 if (is_Bad(get_Block_cfgpred(b, i))) {
1748 } else if (get_Block_block_visited(pred) +1
1749 < get_irg_block_visited(current_ir_graph)) {
1750 /* It's an empty block and not yet visited. */
1751 ir_node *phi_pred = get_Phi_pred(phi, i);
1752 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1753 if (get_nodes_Block(phi_pred) == pred) {
1754 assert(intern_get_irn_op(phi_pred) == op_Phi); /* Block is empty!! */
1755 in[n_preds] = get_Phi_pred(phi_pred, j);
1757 in[n_preds] = phi_pred;
1761 /* The Phi_pred node is replaced now if it is a Phi.
1762 In Schleifen kann offenbar der entfernte Phi Knoten legal verwendet werden.
1763 Daher muss der Phiknoten durch den neuen ersetzt werden.
1764 Weiter muss der alte Phiknoten entfernt werden (durch ersetzen oder
1765 durch einen Bad) damit er aus den keep_alive verschwinden kann.
1766 Man sollte also, falls keine Schleife vorliegt, exchange mit new_Bad
1768 if (get_nodes_Block(phi_pred) == pred) {
1769 /* remove the Phi as it might be kept alive. Further there
1770 might be other users. */
1771 exchange(phi_pred, phi); /* geht, ist aber doch semantisch falsch! Warum?? */
1774 in[n_preds] = get_Phi_pred(phi, i);
1779 set_irn_in(phi, n_preds, in);
1781 phi = get_irn_link(phi);
1785 This happens only if merge between loop backedge and single loop entry. -*/
1786 for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1787 pred = get_nodes_Block(get_Block_cfgpred(b, k));
1788 if (get_Block_block_visited(pred) +1
1789 < get_irg_block_visited(current_ir_graph)) {
1790 phi = get_irn_link(pred);
1792 if (intern_get_irn_op(phi) == op_Phi) {
1793 set_nodes_Block(phi, b);
1796 for (i = 0; i < k; i++) {
1797 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1798 if (is_Bad(get_Block_cfgpred(b, i))) {
1800 } else if (get_Block_block_visited(pred) +1
1801 < get_irg_block_visited(current_ir_graph)) {
1802 /* It's an empty block and not yet visited. */
1803 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1804 /* @@@ Hier brauche ich Schleifeninformation!!! Kontrollflusskante
1805 muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1806 Anweisungen.) Trotzdem tuts bisher!! */
1815 for (i = 0; i < get_Phi_n_preds(phi); i++) {
1816 in[n_preds] = get_Phi_pred(phi, i);
1819 for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1820 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1821 if (is_Bad(get_Block_cfgpred(b, i))) {
1823 } else if (get_Block_block_visited(pred) +1
1824 < get_irg_block_visited(current_ir_graph)) {
1825 /* It's an empty block and not yet visited. */
1826 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1835 set_irn_in(phi, n_preds, in);
1837 phi = get_irn_link(phi);
1842 /*- Fix the block -*/
1844 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1845 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1846 if (is_Bad(get_Block_cfgpred(b, i))) {
1848 } else if (get_Block_block_visited(pred) +1
1849 < get_irg_block_visited(current_ir_graph)) {
1850 /* It's an empty block and not yet visited. */
1851 assert(get_Block_n_cfgpreds(b) > 1);
1852 /* Else it should be optimized by equivalent_node. */
1853 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1854 in[n_preds] = get_Block_cfgpred(pred, j);
1857 /* Remove block as it might be kept alive. */
1858 exchange(pred, b/*new_Bad()*/);
1860 in[n_preds] = get_Block_cfgpred(b, i);
1864 set_irn_in(b, n_preds, in);
1868 void optimize_cf(ir_graph *irg) {
1871 ir_node *end = get_irg_end(irg);
1872 ir_graph *rem = current_ir_graph;
1873 current_ir_graph = irg;
1875 /* Handle graph state */
1876 assert(get_irg_phase_state(irg) != phase_building);
1877 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1878 set_irg_outs_inconsistent(current_ir_graph);
1879 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1880 set_irg_dom_inconsistent(current_ir_graph);
1882 /* Use block visited flag to mark non-empty blocks. */
1883 inc_irg_block_visited(irg);
1884 irg_walk(end, merge_blocks, collect_nodes, NULL);
1886 /* Optimize the standard code. */
1887 irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1889 /* Walk all keep alives, optimize them if block, add to new in-array
1890 for end if useful. */
1891 in = NEW_ARR_F (ir_node *, 1);
1892 in[0] = get_nodes_Block(end);
1893 inc_irg_visited(current_ir_graph);
1894 for(i = 0; i < get_End_n_keepalives(end); i++) {
1895 ir_node *ka = get_End_keepalive(end, i);
1896 if (irn_not_visited(ka)) {
1897 if ((intern_get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1898 set_irg_block_visited(current_ir_graph, /* Don't walk all the way to Start. */
1899 get_irg_block_visited(current_ir_graph)-1);
1900 irg_block_walk(ka, optimize_blocks, NULL, NULL);
1901 mark_irn_visited(ka);
1902 ARR_APP1 (ir_node *, in, ka);
1903 } else if (intern_get_irn_op(ka) == op_Phi) {
1904 mark_irn_visited(ka);
1905 ARR_APP1 (ir_node *, in, ka);
1909 /* DEL_ARR_F(end->in); GL @@@ tut nicht ! */
1912 current_ir_graph = rem;
1917 * Called by walker of remove_critical_cf_edges.
1919 * Place an empty block to an edge between a blocks of multiple
1920 * predecessors and a block of multiple successors.
1923 * @param env Environment of walker. This field is unused and has
1926 static void walk_critical_cf_edges(ir_node *n, void *env) {
1928 ir_node *pre, *block, **in, *jmp;
1930 /* Block has multiple predecessors */
1931 if ((op_Block == intern_get_irn_op(n)) &&
1932 (intern_get_irn_arity(n) > 1)) {
1933 arity = intern_get_irn_arity(n);
1935 if (n == get_irg_end_block(current_ir_graph))
1936 return; /* No use to add a block here. */
1938 for (i=0; i<arity; i++) {
1939 pre = intern_get_irn_n(n, i);
1940 /* Predecessor has multiple successors. Insert new flow edge */
1941 if ((NULL != pre) &&
1942 (op_Proj == intern_get_irn_op(pre)) &&
1943 op_Raise != intern_get_irn_op(skip_Proj(pre))) {
1945 /* set predecessor array for new block */
1946 in = NEW_ARR_D (ir_node *, current_ir_graph->obst, 1);
1947 /* set predecessor of new block */
1949 block = new_Block(1, in);
1950 /* insert new jmp node to new block */
1951 switch_block(block);
1954 /* set successor of new block */
1955 set_irn_n(n, i, jmp);
1957 } /* predecessor has multiple successors */
1958 } /* for all predecessors */
1959 } /* n is a block */
1962 void remove_critical_cf_edges(ir_graph *irg) {
1963 if (get_opt_critical_edges())
1964 irg_walk_graph(irg, NULL, walk_critical_cf_edges, NULL);