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"
38 /* Defined in iropt.c */
39 pset *new_identities (void);
40 void del_identities (pset *value_table);
41 void add_identities (pset *value_table, ir_node *node);
43 /********************************************************************/
44 /* apply optimizations of iropt to all nodes. */
45 /********************************************************************/
47 static void init_link (ir_node *n, void *env) {
48 set_irn_link(n, NULL);
51 #if 0 /* Old version. Avoids Ids.
52 This is not necessary: we do a postwalk, and get_irn_n
53 removes ids anyways. So it's much cheaper to call the
54 optimization less often and use the exchange() algorithm. */
56 optimize_in_place_wrapper (ir_node *n, void *env) {
58 ir_node *optimized, *old;
60 irn_arity = get_irn_arity(n);
61 for (i = 0; i < irn_arity; i++) {
62 /* get_irn_n skips Id nodes, so comparison old != optimized does not
63 show all optimizations. Therefore always set new predecessor. */
64 old = get_irn_n(n, i);
65 optimized = optimize_in_place_2(old);
66 set_irn_n(n, i, optimized);
69 if (get_irn_op(n) == op_Block) {
70 optimized = optimize_in_place_2(n);
71 if (optimized != n) exchange (n, optimized);
76 optimize_in_place_wrapper (ir_node *n, void *env) {
77 ir_node *optimized = optimize_in_place_2(n);
78 if (optimized != n) exchange (n, optimized);
85 local_optimize_graph (ir_graph *irg) {
86 ir_graph *rem = current_ir_graph;
87 current_ir_graph = irg;
89 /* Handle graph state */
90 assert(get_irg_phase_state(irg) != phase_building);
91 if (get_opt_global_cse())
92 set_irg_pinned(current_ir_graph, floats);
93 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
94 set_irg_outs_inconsistent(current_ir_graph);
95 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
96 set_irg_dom_inconsistent(current_ir_graph);
98 /* Clean the value_table in irg for the cse. */
99 del_identities(irg->value_table);
100 irg->value_table = new_identities();
102 /* walk over the graph */
103 irg_walk(irg->end, init_link, optimize_in_place_wrapper, NULL);
105 current_ir_graph = rem;
108 /********************************************************************/
109 /* Routines for dead node elimination / copying garbage collection */
110 /* of the obstack. */
111 /********************************************************************/
113 /* Remeber the new node in the old node by using a field all nodes have. */
115 set_new_node (ir_node *old, ir_node *new)
120 /* Get this new node, before the old node is forgotton.*/
121 static INLINE ir_node *
122 get_new_node (ir_node * n)
127 /* We use the block_visited flag to mark that we have computed the
128 number of useful predecessors for this block.
129 Further we encode the new arity in this flag in the old blocks.
130 Remembering the arity is useful, as it saves a lot of pointer
131 accesses. This function is called for all Phi and Block nodes
134 compute_new_arity(ir_node *b) {
135 int i, res, irn_arity;
138 irg_v = get_irg_block_visited(current_ir_graph);
139 block_v = get_Block_block_visited(b);
140 if (block_v >= irg_v) {
141 /* we computed the number of preds for this block and saved it in the
143 return block_v - irg_v;
145 /* compute the number of good predecessors */
146 res = irn_arity = get_irn_arity(b);
147 for (i = 0; i < irn_arity; i++)
148 if (get_irn_opcode(get_irn_n(b, i)) == iro_Bad) res--;
149 /* save it in the flag. */
150 set_Block_block_visited(b, irg_v + res);
155 static INLINE void new_backedge_info(ir_node *n) {
156 switch(get_irn_opcode(n)) {
158 n->attr.block.cg_backedge = NULL;
159 n->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
162 n->attr.phi_backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
165 n->attr.filter.backedge = new_backedge_arr(current_ir_graph->obst, get_irn_arity(n));
171 /* Copies the node to the new obstack. The Ins of the new node point to
172 the predecessors on the old obstack. For block/phi nodes not all
173 predecessors might be copied. n->link points to the new node.
174 For Phi and Block nodes the function allocates in-arrays with an arity
175 only for useful predecessors. The arity is determined by counting
176 the non-bad predecessors of the block. */
178 copy_node (ir_node *n, void *env) {
182 /* The end node looses it's flexible in array. This doesn't matter,
183 as dead node elimination builds End by hand, inlineing doesn't use
185 //assert(n->op == op_End || ((_ARR_DESCR(n->in))->cookie != ARR_F_MAGIC));
187 if (get_irn_opcode(n) == iro_Block) {
189 new_arity = compute_new_arity(n);
190 n->attr.block.graph_arr = NULL;
192 block = get_nodes_Block(n);
193 if (get_irn_opcode(n) == iro_Phi) {
194 new_arity = compute_new_arity(block);
196 new_arity = get_irn_arity(n);
199 nn = new_ir_node(get_irn_dbg_info(n),
206 /* Copy the attributes. These might point to additional data. If this
207 was allocated on the old obstack the pointers now are dangling. This
208 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
210 new_backedge_info(nn);
213 /* printf("\n old node: "); DDMSG2(n);
214 printf(" new node: "); DDMSG2(nn); */
218 /* Copies new predecessors of old node to new node remembered in link.
219 Spare the Bad predecessors of Phi and Block nodes. */
221 copy_preds (ir_node *n, void *env) {
225 nn = get_new_node(n);
227 /* printf("\n old node: "); DDMSG2(n);
228 printf(" new node: "); DDMSG2(nn);
229 printf(" arities: old: %d, new: %d\n", get_irn_arity(n), get_irn_arity(nn)); */
231 if (get_irn_opcode(n) == iro_Block) {
232 /* Don't copy Bad nodes. */
234 irn_arity = get_irn_arity(n);
235 for (i = 0; i < irn_arity; i++)
236 if (get_irn_opcode(get_irn_n(n, i)) != iro_Bad) {
237 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
238 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
241 /* repair the block visited flag from above misuse. Repair it in both
242 graphs so that the old one can still be used. */
243 set_Block_block_visited(nn, 0);
244 set_Block_block_visited(n, 0);
245 /* Local optimization could not merge two subsequent blocks if
246 in array contained Bads. Now it's possible.
247 We don't call optimize_in_place as it requires
248 that the fields in ir_graph are set properly. */
249 if ((get_opt_control_flow_straightening()) &&
250 (get_Block_n_cfgpreds(nn) == 1) &&
251 (get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp))
252 exchange(nn, get_nodes_Block(get_Block_cfgpred(nn, 0)));
253 } else if (get_irn_opcode(n) == iro_Phi) {
254 /* Don't copy node if corresponding predecessor in block is Bad.
255 The Block itself should not be Bad. */
256 block = get_nodes_Block(n);
257 set_irn_n (nn, -1, get_new_node(block));
259 irn_arity = get_irn_arity(n);
260 for (i = 0; i < irn_arity; i++)
261 if (get_irn_opcode(get_irn_n(block, i)) != iro_Bad) {
262 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
263 /*if (is_backedge(n, i)) set_backedge(nn, j);*/
266 /* If the pre walker reached this Phi after the post walker visited the
267 block block_visited is > 0. */
268 set_Block_block_visited(get_nodes_Block(n), 0);
269 /* Compacting the Phi's ins might generate Phis with only one
271 if (get_irn_arity(n) == 1)
272 exchange(n, get_irn_n(n, 0));
274 irn_arity = get_irn_arity(n);
275 for (i = -1; i < irn_arity; i++)
276 set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
278 /* Now the new node is complete. We can add it to the hash table for cse.
279 @@@ inlinening aborts if we identify End. Why? */
280 if(get_irn_op(nn) != op_End)
281 add_identities (current_ir_graph->value_table, nn);
284 /* Copies the graph recursively, compacts the keepalive of the end node. */
287 ir_node *oe, *ne; /* old end, new end */
288 ir_node *ka; /* keep alive */
291 oe = get_irg_end(current_ir_graph);
292 /* copy the end node by hand, allocate dynamic in array! */
293 ne = new_ir_node(get_irn_dbg_info(oe),
300 /* Copy the attributes. Well, there might be some in the future... */
302 set_new_node(oe, ne);
304 /* copy the live nodes */
305 irg_walk(get_nodes_Block(oe), copy_node, copy_preds, NULL);
306 /* copy_preds for the end node ... */
307 set_nodes_Block(ne, get_new_node(get_nodes_Block(oe)));
309 /** ... and now the keep alives. **/
310 /* First pick the not marked block nodes and walk them. We must pick these
311 first as else we will oversee blocks reachable from Phis. */
312 irn_arity = get_irn_arity(oe);
313 for (i = 0; i < irn_arity; i++) {
314 ka = get_irn_n(oe, i);
315 if ((get_irn_op(ka) == op_Block) &&
316 (get_irn_visited(ka) < get_irg_visited(current_ir_graph))) {
317 /* We must keep the block alive and copy everything reachable */
318 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
319 irg_walk(ka, copy_node, copy_preds, NULL);
320 add_End_keepalive(ne, get_new_node(ka));
324 /* Now pick the Phis. Here we will keep all! */
325 irn_arity = get_irn_arity(oe);
326 for (i = 0; i < irn_arity; i++) {
327 ka = get_irn_n(oe, i);
328 if ((get_irn_op(ka) == op_Phi)) {
329 if (get_irn_visited(ka) < get_irg_visited(current_ir_graph)) {
330 /* We didn't copy the Phi yet. */
331 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
332 irg_walk(ka, copy_node, copy_preds, NULL);
334 add_End_keepalive(ne, get_new_node(ka));
339 /* Copies the graph reachable from current_ir_graph->end to the obstack
340 in current_ir_graph and fixes the environment.
341 Then fixes the fields in current_ir_graph containing nodes of the
344 copy_graph_env (void) {
346 /* Not all nodes remembered in current_ir_graph might be reachable
347 from the end node. Assure their link is set to NULL, so that
348 we can test whether new nodes have been computed. */
349 set_irn_link(get_irg_frame (current_ir_graph), NULL);
350 set_irn_link(get_irg_globals(current_ir_graph), NULL);
351 set_irn_link(get_irg_args (current_ir_graph), NULL);
353 /* we use the block walk flag for removing Bads from Blocks ins. */
354 inc_irg_block_visited(current_ir_graph);
359 /* fix the fields in current_ir_graph */
360 old_end = get_irg_end(current_ir_graph);
361 set_irg_end (current_ir_graph, get_new_node(old_end));
363 set_irg_end_block (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
364 if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
365 copy_node (get_irg_frame(current_ir_graph), NULL);
366 copy_preds(get_irg_frame(current_ir_graph), NULL);
368 if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
369 copy_node (get_irg_globals(current_ir_graph), NULL);
370 copy_preds(get_irg_globals(current_ir_graph), NULL);
372 if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
373 copy_node (get_irg_args(current_ir_graph), NULL);
374 copy_preds(get_irg_args(current_ir_graph), NULL);
376 set_irg_start (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
378 set_irg_start_block(current_ir_graph,
379 get_new_node(get_irg_start_block(current_ir_graph)));
380 set_irg_frame (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
381 set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
382 set_irg_args (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
383 if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
384 copy_node(get_irg_bad(current_ir_graph), NULL);
385 copy_preds(get_irg_bad(current_ir_graph), NULL);
387 set_irg_bad(current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
388 /* GL removed: we need unknown with mode for analyses.
389 if (get_irn_link(get_irg_unknown(current_ir_graph)) == NULL) {
390 copy_node(get_irg_unknown(current_ir_graph), NULL);
391 copy_preds(get_irg_unknown(current_ir_graph), NULL);
393 set_irg_unknown(current_ir_graph, get_new_node(get_irg_unknown(current_ir_graph)));
397 /* Copies all reachable nodes to a new obstack. Removes bad inputs
398 from block nodes and the corresponding inputs from Phi nodes.
399 Merges single exit blocks with single entry blocks and removes
401 Adds all new nodes to a new hash table for cse. Does not
402 perform cse, so the hash table might contain common subexpressions. */
403 /* Amroq call this emigrate() */
405 dead_node_elimination(ir_graph *irg) {
407 struct obstack *graveyard_obst = NULL;
408 struct obstack *rebirth_obst = NULL;
410 /* Remember external state of current_ir_graph. */
411 rem = current_ir_graph;
412 current_ir_graph = irg;
414 /* Handle graph state */
415 assert(get_irg_phase_state(current_ir_graph) != phase_building);
416 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
417 free_outs(current_ir_graph);
419 /* @@@ so far we loose loops when copying */
420 set_irg_loop(current_ir_graph, NULL);
422 if (get_opt_optimize() && get_opt_dead_node_elimination()) {
424 /* A quiet place, where the old obstack can rest in peace,
425 until it will be cremated. */
426 graveyard_obst = irg->obst;
428 /* A new obstack, where the reachable nodes will be copied to. */
429 rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
430 current_ir_graph->obst = rebirth_obst;
431 obstack_init (current_ir_graph->obst);
433 /* We also need a new hash table for cse */
434 del_identities (irg->value_table);
435 irg->value_table = new_identities ();
437 /* Copy the graph from the old to the new obstack */
440 /* Free memory from old unoptimized obstack */
441 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
442 xfree (graveyard_obst); /* ... then free it. */
445 current_ir_graph = rem;
448 /* Relink bad predeseccors of a block and store the old in array to the
449 link field. This function is called by relink_bad_predecessors().
450 The array of link field starts with the block operand at position 0.
451 If block has bad predecessors, create a new in array without bad preds.
452 Otherwise let in array untouched. */
453 static void relink_bad_block_predecessors(ir_node *n, void *env) {
454 ir_node **new_in, *irn;
455 int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
457 /* if link field of block is NULL, look for bad predecessors otherwise
458 this is allready done */
459 if (get_irn_op(n) == op_Block &&
460 get_irn_link(n) == NULL) {
462 /* save old predecessors in link field (position 0 is the block operand)*/
463 set_irn_link(n, (void *)get_irn_in(n));
465 /* count predecessors without bad nodes */
466 old_irn_arity = get_irn_arity(n);
467 for (i = 0; i < old_irn_arity; i++)
468 if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
470 /* arity changing: set new predecessors without bad nodes */
471 if (new_irn_arity < old_irn_arity) {
472 /* get new predecessor array without Block predecessor */
473 new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
475 /* set new predeseccors in array */
478 for (i = 1; i < old_irn_arity; i++) {
479 irn = get_irn_n(n, i);
480 if (!is_Bad(irn)) new_in[new_irn_n++] = irn;
483 } /* ir node has bad predecessors */
485 } /* Block is not relinked */
488 /* Relinks Bad predecesors from Bocks and Phis called by walker
489 remove_bad_predecesors(). If n is a Block, call
490 relink_bad_block_redecessors(). If n is a Phinode, call also the relinking
491 function of Phi's Block. If this block has bad predecessors, relink preds
493 static void relink_bad_predecessors(ir_node *n, void *env) {
494 ir_node *block, **old_in;
495 int i, old_irn_arity, new_irn_arity;
497 /* relink bad predeseccors of a block */
498 if (get_irn_op(n) == op_Block)
499 relink_bad_block_predecessors(n, env);
501 /* If Phi node relink its block and its predecessors */
502 if (get_irn_op(n) == op_Phi) {
504 /* Relink predeseccors of phi's block */
505 block = get_nodes_Block(n);
506 if (get_irn_link(block) == NULL)
507 relink_bad_block_predecessors(block, env);
509 old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
510 old_irn_arity = ARR_LEN(old_in);
512 /* Relink Phi predeseccors if count of predeseccors changed */
513 if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
514 /* set new predeseccors in array
515 n->in[0] remains the same block */
517 for(i = 1; i < old_irn_arity; i++)
518 if (!is_Bad((ir_node *)old_in[i])) n->in[new_irn_arity++] = n->in[i];
520 ARR_SETLEN(ir_node *, n->in, new_irn_arity);
523 } /* n is a Phi node */
526 /* Removes Bad Bad predecesors from Blocks and the corresponding
527 inputs to Phi nodes as in dead_node_elimination but without
529 On walking up set the link field to NULL, on walking down call
530 relink_bad_predecessors() (This function stores the old in array
531 to the link field and sets a new in array if arity of predecessors
533 void remove_bad_predecessors(ir_graph *irg) {
534 irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
538 /**********************************************************************/
539 /* Funcionality for inlining */
540 /**********************************************************************/
542 /* Copy node for inlineing. Updates attributes that change when
543 * inlineing but not for dead node elimination.
545 * Copies the node by calling copy_node and then updates the entity if
546 * it's a local one. env must be a pointer of the frame type of the
547 * inlined procedure. The new entities must be in the link field of
550 copy_node_inline (ir_node *n, void *env) {
552 type *frame_tp = (type *)env;
555 if (get_irn_op(n) == op_Sel) {
556 new = get_new_node (n);
557 assert(get_irn_op(new) == op_Sel);
558 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
559 set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
561 } else if (get_irn_op(n) == op_Block) {
562 new = get_new_node (n);
563 new->attr.block.irg = current_ir_graph;
568 void inline_method(ir_node *call, ir_graph *called_graph) {
570 ir_node *post_call, *post_bl;
572 ir_node *end, *end_bl;
576 int arity, n_ret, n_exc, n_res, i, j, rem_opt, irn_arity;
577 int exc_handling; ir_node *proj;
580 if (!get_opt_optimize() || !get_opt_inline()) return;
581 /* -- Turn off optimizations, this can cause problems when allocating new nodes. -- */
582 rem_opt = get_opt_optimize();
585 /* Handle graph state */
586 assert(get_irg_phase_state(current_ir_graph) != phase_building);
587 assert(get_irg_pinned(current_ir_graph) == pinned);
588 assert(get_irg_pinned(called_graph) == pinned);
589 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
590 set_irg_outs_inconsistent(current_ir_graph);
592 /* -- Check preconditions -- */
593 assert(get_irn_op(call) == op_Call);
594 /* @@@ does not work for InterfaceIII.java after cgana
595 assert(get_Call_type(call) == get_entity_type(get_irg_ent(called_graph)));
596 assert(smaller_type(get_entity_type(get_irg_ent(called_graph)),
597 get_Call_type(call)));
599 assert(get_type_tpop(get_Call_type(call)) == type_method);
600 if (called_graph == current_ir_graph) {
601 set_optimize(rem_opt);
605 /* -- Decide how to handle exception control flow: Is there a handler
606 for the Call node, or do we branch directly to End on an exception?
607 exc_handling: 0 There is a handler.
609 2 Exception handling not represented in Firm. -- */
611 for (proj = (ir_node *)get_irn_link(call); proj; proj = (ir_node *)get_irn_link(proj)) {
612 assert(get_irn_op(proj) == op_Proj);
613 if (get_Proj_proj(proj) == pn_Call_M_except) { exc_handling = 0; break;}
614 if (get_Proj_proj(proj) == pn_Call_X_except) { exc_handling = 1; }
618 ir_node *proj, *Mproj = NULL, *Xproj = NULL;
619 for (proj = (ir_node *)get_irn_link(call); proj; proj = (ir_node *)get_irn_link(proj)) {
620 assert(get_irn_op(proj) == op_Proj);
621 if (get_Proj_proj(proj) == pn_Call_X_except) Xproj = proj;
622 if (get_Proj_proj(proj) == pn_Call_M_except) Mproj = proj;
624 if (Mproj) { assert(Xproj); exc_handling = 0; }
625 else if (Xproj) { exc_handling = 1; }
626 else { exc_handling = 2; }
631 the procedure and later replaces the Start node of the called graph.
632 Post_call is the old Call node and collects the results of the called
633 graph. Both will end up being a tuple. -- */
634 post_bl = get_nodes_Block(call);
635 set_irg_current_block(current_ir_graph, post_bl);
636 /* XxMxPxP of Start + parameter of Call */
638 in[1] = get_Call_mem(call);
639 in[2] = get_irg_frame(current_ir_graph);
640 in[3] = get_irg_globals(current_ir_graph);
641 in[4] = new_Tuple (get_Call_n_params(call), get_Call_param_arr(call));
642 pre_call = new_Tuple(5, in);
646 The new block gets the ins of the old block, pre_call and all its
647 predecessors and all Phi nodes. -- */
648 part_block(pre_call);
650 /* -- Prepare state for dead node elimination -- */
651 /* Visited flags in calling irg must be >= flag in called irg.
652 Else walker and arity computation will not work. */
653 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
654 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1);
655 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
656 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
657 /* Set pre_call as new Start node in link field of the start node of
658 calling graph and pre_calls block as new block for the start block
660 Further mark these nodes so that they are not visited by the
662 set_irn_link(get_irg_start(called_graph), pre_call);
663 set_irn_visited(get_irg_start(called_graph),
664 get_irg_visited(current_ir_graph));
665 set_irn_link(get_irg_start_block(called_graph),
666 get_nodes_Block(pre_call));
667 set_irn_visited(get_irg_start_block(called_graph),
668 get_irg_visited(current_ir_graph));
670 /* Initialize for compaction of in arrays */
671 inc_irg_block_visited(current_ir_graph);
673 /* -- Replicate local entities of the called_graph -- */
674 /* copy the entities. */
675 called_frame = get_irg_frame_type(called_graph);
676 for (i = 0; i < get_class_n_members(called_frame); i++) {
677 entity *new_ent, *old_ent;
678 old_ent = get_class_member(called_frame, i);
679 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
680 set_entity_link(old_ent, new_ent);
683 /* visited is > than that of called graph. With this trick visited will
684 remain unchanged so that an outer walker, e.g., searching the call nodes
685 to inline, calling this inline will not visit the inlined nodes. */
686 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
688 /* -- Performing dead node elimination inlines the graph -- */
689 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
691 /* @@@ endless loops are not copied!! -- they should be, I think... */
692 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
693 get_irg_frame_type(called_graph));
695 /* Repair called_graph */
696 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
697 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
698 set_Block_block_visited(get_irg_start_block(called_graph), 0);
700 /* -- Merge the end of the inlined procedure with the call site -- */
701 /* We will turn the old Call node into a Tuple with the following
704 0: Phi of all Memories of Return statements.
705 1: Jmp from new Block that merges the control flow from all exception
706 predecessors of the old end block.
707 2: Tuple of all arguments.
708 3: Phi of Exception memories.
709 In case the old Call directly branches to End on an exception we don't
710 need the block merging all exceptions nor the Phi of the exception
714 /* -- Precompute some values -- */
715 end_bl = get_new_node(get_irg_end_block(called_graph));
716 end = get_new_node(get_irg_end(called_graph));
717 arity = get_irn_arity(end_bl); /* arity = n_exc + n_ret */
718 n_res = get_method_n_ress(get_Call_type(call));
720 res_pred = (ir_node **) malloc (n_res * sizeof (ir_node *));
721 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
723 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
725 /* -- archive keepalives -- */
726 irn_arity = get_irn_arity(end);
727 for (i = 0; i < irn_arity; i++)
728 add_End_keepalive(get_irg_end(current_ir_graph), get_irn_n(end, i));
730 /* The new end node will die. We need not free as the in array is on the obstack:
731 copy_node only generated 'D' arrays. */
733 /* -- Replace Return nodes by Jump nodes. -- */
735 for (i = 0; i < arity; i++) {
737 ret = get_irn_n(end_bl, i);
738 if (get_irn_op(ret) == op_Return) {
739 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_Block(ret));
743 set_irn_in(post_bl, n_ret, cf_pred);
745 /* -- Build a Tuple for all results of the method.
746 Add Phi node if there was more than one Return. -- */
747 turn_into_tuple(post_call, 4);
748 /* First the Memory-Phi */
750 for (i = 0; i < arity; i++) {
751 ret = get_irn_n(end_bl, i);
752 if (get_irn_op(ret) == op_Return) {
753 cf_pred[n_ret] = get_Return_mem(ret);
757 phi = new_Phi(n_ret, cf_pred, mode_M);
758 set_Tuple_pred(call, 0, phi);
759 /* Conserve Phi-list for further inlinings -- but might be optimized */
760 if (get_nodes_Block(phi) == post_bl) {
761 set_irn_link(phi, get_irn_link(post_bl));
762 set_irn_link(post_bl, phi);
764 /* Now the real results */
766 for (j = 0; j < n_res; j++) {
768 for (i = 0; i < arity; i++) {
769 ret = get_irn_n(end_bl, i);
770 if (get_irn_op(ret) == op_Return) {
771 cf_pred[n_ret] = get_Return_res(ret, j);
775 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
777 /* Conserve Phi-list for further inlinings -- but might be optimized */
778 if (get_nodes_Block(phi) == post_bl) {
779 set_irn_link(phi, get_irn_link(post_bl));
780 set_irn_link(post_bl, phi);
783 set_Tuple_pred(call, 2, new_Tuple(n_res, res_pred));
785 set_Tuple_pred(call, 2, new_Bad());
787 /* Finally the exception control flow.
788 We have two (three) possible situations:
789 First if the Call branches to an exception handler: We need to add a Phi node to
790 collect the memory containing the exception objects. Further we need
791 to add another block to get a correct representation of this Phi. To
792 this block we add a Jmp that resolves into the X output of the Call
793 when the Call is turned into a tuple.
794 Second the Call branches to End, the exception is not handled. Just
795 add all inlined exception branches to the End node.
796 Third: there is no Exception edge at all. Handle as case two. */
797 if (exc_handler == 0) {
799 for (i = 0; i < arity; i++) {
801 ret = get_irn_n(end_bl, i);
802 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
803 cf_pred[n_exc] = ret;
808 new_Block(n_exc, cf_pred); /* watch it: current_block is changed! */
809 set_Tuple_pred(call, 1, new_Jmp());
810 /* The Phi for the memories with the exception objects */
812 for (i = 0; i < arity; i++) {
814 ret = skip_Proj(get_irn_n(end_bl, i));
815 if (get_irn_op(ret) == op_Call) {
816 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 3);
818 } else if (is_fragile_op(ret)) {
819 /* We rely that all cfops have the memory output at the same position. */
820 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 0);
822 } else if (get_irn_op(ret) == op_Raise) {
823 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 1);
827 set_Tuple_pred(call, 3, new_Phi(n_exc, cf_pred, mode_M));
829 set_Tuple_pred(call, 1, new_Bad());
830 set_Tuple_pred(call, 3, new_Bad());
833 ir_node *main_end_bl;
834 int main_end_bl_arity;
837 /* assert(exc_handler == 1 || no exceptions. ) */
839 for (i = 0; i < arity; i++) {
840 ir_node *ret = get_irn_n(end_bl, i);
842 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
843 cf_pred[n_exc] = ret;
847 main_end_bl = get_irg_end_block(current_ir_graph);
848 main_end_bl_arity = get_irn_arity(main_end_bl);
849 end_preds = (ir_node **) malloc ((n_exc + main_end_bl_arity) * sizeof (ir_node *));
851 for (i = 0; i < main_end_bl_arity; ++i)
852 end_preds[i] = get_irn_n(main_end_bl, i);
853 for (i = 0; i < n_exc; ++i)
854 end_preds[main_end_bl_arity + i] = cf_pred[i];
855 set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
856 set_Tuple_pred(call, 1, new_Bad());
857 set_Tuple_pred(call, 3, new_Bad());
863 #if 0 /* old. now better, correcter, faster implementation. */
865 /* -- If the exception control flow from the inlined Call directly
866 branched to the end block we now have the following control
867 flow predecessor pattern: ProjX -> Tuple -> Jmp. We must
868 remove the Jmp along with it's empty block and add Jmp's
869 predecessors as predecessors of this end block. No problem if
870 there is no exception, because then branches Bad to End which
872 @@@ can't we know this beforehand: by getting the Proj(1) from
873 the Call link list and checking whether it goes to Proj. */
874 /* find the problematic predecessor of the end block. */
875 end_bl = get_irg_end_block(current_ir_graph);
876 for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
877 cf_op = get_Block_cfgpred(end_bl, i);
878 if (get_irn_op(cf_op) == op_Proj) {
879 cf_op = get_Proj_pred(cf_op);
880 if ((get_irn_op(cf_op) == op_Tuple) && (cf_op == call)) {
881 // There are unoptimized tuples from inlineing before when no exc
882 assert(get_Proj_proj(get_Block_cfgpred(end_bl, i)) == pn_Call_X_except);
883 cf_op = get_Tuple_pred(cf_op, pn_Call_X_except);
884 assert(get_irn_op(cf_op) == op_Jmp);
890 if (i < get_Block_n_cfgpreds(end_bl)) {
891 bl = get_nodes_Block(cf_op);
892 arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
893 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
894 for (j = 0; j < i; j++)
895 cf_pred[j] = get_Block_cfgpred(end_bl, j);
896 for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
897 cf_pred[j] = get_Block_cfgpred(bl, j-i);
898 for (j = j; j < arity; j++)
899 cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
900 set_irn_in(end_bl, arity, cf_pred);
902 // Remove the exception pred from post-call Tuple.
903 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
908 /* -- Turn cse back on. -- */
909 set_optimize(rem_opt);
912 /********************************************************************/
913 /* Apply inlineing to small methods. */
914 /********************************************************************/
918 /* It makes no sense to inline too many calls in one procedure. Anyways,
919 I didn't get a version with NEW_ARR_F to run. */
920 #define MAX_INLINE 1024
922 /* given an Call node, returns the irg called. NULL if not
924 static ir_graph *get_call_called_irg(ir_node *call) {
927 ir_graph *called_irg = NULL;
929 assert(get_irn_op(call) == op_Call);
931 addr = get_Call_ptr(call);
932 if (get_irn_op(addr) == op_Const) {
933 /* Check whether the constant is the pointer to a compiled entity. */
934 tv = get_Const_tarval(addr);
935 if (tarval_to_entity(tv))
936 called_irg = get_entity_irg(tarval_to_entity(tv));
941 static void collect_calls(ir_node *call, void *env) {
943 ir_node **calls = (ir_node **)env;
946 ir_graph *called_irg;
948 if (get_irn_op(call) != op_Call) return;
950 addr = get_Call_ptr(call);
951 if (get_irn_op(addr) == op_Const) {
952 /* Check whether the constant is the pointer to a compiled entity. */
953 tv = get_Const_tarval(addr);
954 if (tarval_to_entity(tv)) {
955 called_irg = get_entity_irg(tarval_to_entity(tv));
956 if (called_irg && pos < MAX_INLINE) {
957 /* The Call node calls a locally defined method. Remember to inline. */
965 /* Inlines all small methods at call sites where the called address comes
966 from a Const node that references the entity representing the called
968 The size argument is a rough measure for the code size of the method:
969 Methods where the obstack containing the firm graph is smaller than
971 void inline_small_irgs(ir_graph *irg, int size) {
973 ir_node *calls[MAX_INLINE];
974 ir_graph *rem = current_ir_graph;
976 if (!(get_opt_optimize() && get_opt_inline())) return;
978 current_ir_graph = irg;
979 /* Handle graph state */
980 assert(get_irg_phase_state(current_ir_graph) != phase_building);
981 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
983 /* Find Call nodes to inline.
984 (We can not inline during a walk of the graph, as inlineing the same
985 method several times changes the visited flag of the walked graph:
986 after the first inlineing visited of the callee equals visited of
987 the caller. With the next inlineing both are increased.) */
989 irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
991 if ((pos > 0) && (pos < MAX_INLINE)) {
992 /* There are calls to inline */
993 collect_phiprojs(irg);
994 for (i = 0; i < pos; i++) {
997 tv = get_Const_tarval(get_Call_ptr(calls[i]));
998 callee = get_entity_irg(tarval_to_entity(tv));
999 if ((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) {
1000 inline_method(calls[i], callee);
1005 current_ir_graph = rem;
1009 * Environment for inlining irgs.
1012 int n_nodes; /**< Nodes in graph except Id, Tuple, Proj, Start, End */
1013 int n_nodes_orig; /**< for statistics */
1014 eset *call_nodes; /**< All call nodes in this graph */
1016 int n_call_nodes_orig; /**< for statistics */
1017 int n_callers; /**< Number of known graphs that call this graphs. */
1018 int n_callers_orig; /**< for statistics */
1021 static inline_irg_env *new_inline_irg_env(void) {
1022 inline_irg_env *env = malloc(sizeof(inline_irg_env));
1023 env->n_nodes = -2; /* uncount Start, End */
1024 env->n_nodes_orig = -2; /* uncount Start, End */
1025 env->call_nodes = eset_create();
1026 env->n_call_nodes = 0;
1027 env->n_call_nodes_orig = 0;
1029 env->n_callers_orig = 0;
1033 static void free_inline_irg_env(inline_irg_env *env) {
1034 eset_destroy(env->call_nodes);
1038 static void collect_calls2(ir_node *call, void *env) {
1039 inline_irg_env *x = (inline_irg_env *)env;
1040 ir_op *op = get_irn_op(call);
1043 /* count nodes in irg */
1044 if (op != op_Proj && op != op_Tuple && op != op_Sync) {
1049 if (op != op_Call) return;
1051 /* collect all call nodes */
1052 eset_insert(x->call_nodes, (void *)call);
1054 x->n_call_nodes_orig++;
1056 /* count all static callers */
1057 callee = get_call_called_irg(call);
1059 ((inline_irg_env *)get_irg_link(callee))->n_callers++;
1060 ((inline_irg_env *)get_irg_link(callee))->n_callers_orig++;
1064 INLINE static int is_leave(ir_graph *irg) {
1065 return (((inline_irg_env *)get_irg_link(irg))->n_call_nodes == 0);
1068 INLINE static int is_smaller(ir_graph *callee, int size) {
1069 return (((inline_irg_env *)get_irg_link(callee))->n_nodes < size);
1073 /* Inlines small leave methods at call sites where the called address comes
1074 from a Const node that references the entity representing the called
1076 The size argument is a rough measure for the code size of the method:
1077 Methods where the obstack containing the firm graph is smaller than
1078 size are inlined. */
1079 void inline_leave_functions(int maxsize, int leavesize, int size) {
1080 inline_irg_env *env;
1081 int i, n_irgs = get_irp_n_irgs();
1082 ir_graph *rem = current_ir_graph;
1085 if (!(get_opt_optimize() && get_opt_inline())) return;
1087 /* extend all irgs by a temporary data structure for inlineing. */
1088 for (i = 0; i < n_irgs; ++i)
1089 set_irg_link(get_irp_irg(i), new_inline_irg_env());
1091 /* Precompute information in temporary data structure. */
1092 for (i = 0; i < n_irgs; ++i) {
1093 current_ir_graph = get_irp_irg(i);
1094 assert(get_irg_phase_state(current_ir_graph) != phase_building);
1095 assert(get_irg_callee_info_state(current_ir_graph) == irg_callee_info_none);
1097 irg_walk(get_irg_end(current_ir_graph), NULL, collect_calls2,
1098 get_irg_link(current_ir_graph));
1099 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1103 Inline leaves recursively -- we might construct new leaves. */
1105 while (did_inline) {
1106 //printf("iteration %d\n", itercnt++);
1108 for (i = 0; i < n_irgs; ++i) {
1111 int phiproj_computed = 0;
1113 current_ir_graph = get_irp_irg(i);
1114 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1116 /* we can not walk and change a set, nor remove from it.
1118 walkset = env->call_nodes;
1119 env->call_nodes = eset_create();
1120 for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1121 inline_irg_env *callee_env;
1122 ir_graph *callee = get_call_called_irg(call);
1124 if (env->n_nodes > maxsize) break;
1125 if (callee && is_leave(callee) && is_smaller(callee, leavesize)) {
1126 if (!phiproj_computed) {
1127 phiproj_computed = 1;
1128 collect_phiprojs(current_ir_graph);
1130 callee_env = (inline_irg_env *)get_irg_link(callee);
1131 // printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)),
1132 // get_entity_name(get_irg_entity(callee)));
1133 inline_method(call, callee);
1135 env->n_call_nodes--;
1136 eset_insert_all(env->call_nodes, callee_env->call_nodes);
1137 env->n_call_nodes += callee_env->n_call_nodes;
1138 env->n_nodes += callee_env->n_nodes;
1139 callee_env->n_callers--;
1141 eset_insert(env->call_nodes, call);
1144 eset_destroy(walkset);
1148 //printf("Non leaves\n");
1149 /* inline other small functions. */
1150 for (i = 0; i < n_irgs; ++i) {
1153 int phiproj_computed = 0;
1155 current_ir_graph = get_irp_irg(i);
1156 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1158 /* we can not walk and change a set, nor remove from it.
1160 walkset = env->call_nodes;
1161 env->call_nodes = eset_create();
1162 for (call = eset_first(walkset); call; call = eset_next(walkset)) {
1163 inline_irg_env *callee_env;
1164 ir_graph *callee = get_call_called_irg(call);
1166 if (env->n_nodes > maxsize) break;
1167 if (callee && is_smaller(callee, size)) {
1168 if (!phiproj_computed) {
1169 phiproj_computed = 1;
1170 collect_phiprojs(current_ir_graph);
1172 callee_env = (inline_irg_env *)get_irg_link(callee);
1173 // printf(" %s: Inlineing %s.\n", get_entity_name(get_irg_entity(current_ir_graph)),
1174 // get_entity_name(get_irg_entity(callee)));
1175 inline_method(call, callee);
1177 env->n_call_nodes--;
1178 eset_insert_all(env->call_nodes, callee_env->call_nodes);
1179 env->n_call_nodes += callee_env->n_call_nodes;
1180 env->n_nodes += callee_env->n_nodes;
1181 callee_env->n_callers--;
1183 eset_insert(env->call_nodes, call);
1186 eset_destroy(walkset);
1189 for (i = 0; i < n_irgs; ++i) {
1190 current_ir_graph = get_irp_irg(i);
1192 env = (inline_irg_env *)get_irg_link(current_ir_graph);
1193 if ((env->n_call_nodes_orig != env->n_call_nodes) ||
1194 (env->n_callers_orig != env->n_callers))
1195 printf("Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
1196 env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
1197 env->n_callers_orig, env->n_callers,
1198 get_entity_name(get_irg_entity(current_ir_graph)));
1200 free_inline_irg_env((inline_irg_env *)get_irg_link(current_ir_graph));
1203 current_ir_graph = rem;
1206 /********************************************************************/
1207 /* Code Placement. Pins all floating nodes to a block where they */
1208 /* will be executed only if needed. */
1209 /********************************************************************/
1211 /* Find the earliest correct block for N. --- Place N into the
1212 same Block as its dominance-deepest Input. */
1214 place_floats_early(ir_node *n, pdeq *worklist)
1216 int i, start, irn_arity;
1218 /* we must not run into an infinite loop */
1219 assert (irn_not_visited(n));
1220 mark_irn_visited(n);
1222 /* Place floating nodes. */
1223 if (get_op_pinned(get_irn_op(n)) == floats) {
1225 ir_node *b = new_Bad(); /* The block to place this node in */
1227 assert(get_irn_op(n) != op_Block);
1229 if ((get_irn_op(n) == op_Const) ||
1230 (get_irn_op(n) == op_SymConst) ||
1232 (get_irn_op(n) == op_Unknown)) {
1233 /* These nodes will not be placed by the loop below. */
1234 b = get_irg_start_block(current_ir_graph);
1238 /* find the block for this node. */
1239 irn_arity = get_irn_arity(n);
1240 for (i = 0; i < irn_arity; i++) {
1241 ir_node *dep = get_irn_n(n, i);
1243 if ((irn_not_visited(dep)) &&
1244 (get_op_pinned(get_irn_op(dep)) == floats)) {
1245 place_floats_early(dep, worklist);
1247 /* Because all loops contain at least one pinned node, now all
1248 our inputs are either pinned or place_early has already
1249 been finished on them. We do not have any unfinished inputs! */
1250 dep_block = get_nodes_Block(dep);
1251 if ((!is_Bad(dep_block)) &&
1252 (get_Block_dom_depth(dep_block) > depth)) {
1254 depth = get_Block_dom_depth(dep_block);
1256 /* Avoid that the node is placed in the Start block */
1257 if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
1258 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
1259 assert(b != get_irg_start_block(current_ir_graph));
1263 set_nodes_Block(n, b);
1266 /* Add predecessors of non floating nodes on worklist. */
1267 start = (get_irn_op(n) == op_Block) ? 0 : -1;
1268 irn_arity = get_irn_arity(n);
1269 for (i = start; i < irn_arity; i++) {
1270 ir_node *pred = get_irn_n(n, i);
1271 if (irn_not_visited(pred)) {
1272 pdeq_putr (worklist, pred);
1277 /* Floating nodes form subgraphs that begin at nodes as Const, Load,
1278 Start, Call and end at pinned nodes as Store, Call. Place_early
1279 places all floating nodes reachable from its argument through floating
1280 nodes and adds all beginnings at pinned nodes to the worklist. */
1281 static INLINE void place_early(pdeq* worklist) {
1283 inc_irg_visited(current_ir_graph);
1285 /* this inits the worklist */
1286 place_floats_early(get_irg_end(current_ir_graph), worklist);
1288 /* Work the content of the worklist. */
1289 while (!pdeq_empty (worklist)) {
1290 ir_node *n = pdeq_getl (worklist);
1291 if (irn_not_visited(n)) place_floats_early(n, worklist);
1294 set_irg_outs_inconsistent(current_ir_graph);
1295 current_ir_graph->pinned = pinned;
1299 /* deepest common dominance ancestor of DCA and CONSUMER of PRODUCER */
1301 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
1303 ir_node *block = NULL;
1305 /* Compute the latest block into which we can place a node so that it is
1307 if (get_irn_op(consumer) == op_Phi) {
1308 /* our consumer is a Phi-node, the effective use is in all those
1309 blocks through which the Phi-node reaches producer */
1311 ir_node *phi_block = get_nodes_Block(consumer);
1312 irn_arity = get_irn_arity(consumer);
1313 for (i = 0; i < irn_arity; i++) {
1314 if (get_irn_n(consumer, i) == producer) {
1315 block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
1319 assert(is_no_Block(consumer));
1320 block = get_nodes_Block(consumer);
1323 /* Compute the deepest common ancestor of block and dca. */
1325 if (!dca) return block;
1326 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
1327 block = get_Block_idom(block);
1328 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
1329 dca = get_Block_idom(dca);
1330 while (block != dca)
1331 { block = get_Block_idom(block); dca = get_Block_idom(dca); }
1336 static INLINE int get_irn_loop_depth(ir_node *n) {
1337 return get_loop_depth(get_irn_loop(n));
1340 /* Move n to a block with less loop depth than it's current block. The
1341 new block must be dominated by early. */
1343 move_out_of_loops (ir_node *n, ir_node *early)
1345 ir_node *best, *dca;
1349 /* Find the region deepest in the dominator tree dominating
1350 dca with the least loop nesting depth, but still dominated
1351 by our early placement. */
1352 dca = get_nodes_Block(n);
1354 while (dca != early) {
1355 dca = get_Block_idom(dca);
1356 if (!dca) break; /* should we put assert(dca)? */
1357 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
1361 if (best != get_nodes_Block(n)) {
1363 printf("Moving out of loop: "); DDMN(n);
1364 printf(" Outermost block: "); DDMN(early);
1365 printf(" Best block: "); DDMN(best);
1366 printf(" Innermost block: "); DDMN(get_nodes_Block(n));
1368 set_nodes_Block(n, best);
1372 /* Find the latest legal block for N and place N into the
1373 `optimal' Block between the latest and earliest legal block.
1374 The `optimal' block is the dominance-deepest block of those
1375 with the least loop-nesting-depth. This places N out of as many
1376 loops as possible and then makes it as control dependant as
1379 place_floats_late(ir_node *n, pdeq *worklist)
1384 assert (irn_not_visited(n)); /* no multiple placement */
1386 /* no need to place block nodes, control nodes are already placed. */
1387 if ((get_irn_op(n) != op_Block) &&
1389 (get_irn_mode(n) != mode_X)) {
1390 /* Remember the early placement of this block to move it
1391 out of loop no further than the early placement. */
1392 early = get_nodes_Block(n);
1393 /* Assure that our users are all placed, except the Phi-nodes.
1394 --- Each data flow cycle contains at least one Phi-node. We
1395 have to break the `user has to be placed before the
1396 producer' dependence cycle and the Phi-nodes are the
1397 place to do so, because we need to base our placement on the
1398 final region of our users, which is OK with Phi-nodes, as they
1399 are pinned, and they never have to be placed after a
1400 producer of one of their inputs in the same block anyway. */
1401 for (i = 0; i < get_irn_n_outs(n); i++) {
1402 ir_node *succ = get_irn_out(n, i);
1403 if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
1404 place_floats_late(succ, worklist);
1407 /* We have to determine the final block of this node... except for
1409 if ((get_op_pinned(get_irn_op(n)) == floats) &&
1410 (get_irn_op(n) != op_Const) &&
1411 (get_irn_op(n) != op_SymConst)) {
1412 ir_node *dca = NULL; /* deepest common ancestor in the
1413 dominator tree of all nodes'
1414 blocks depending on us; our final
1415 placement has to dominate DCA. */
1416 for (i = 0; i < get_irn_n_outs(n); i++) {
1417 dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
1419 set_nodes_Block(n, dca);
1421 move_out_of_loops (n, early);
1425 mark_irn_visited(n);
1427 /* Add predecessors of all non-floating nodes on list. (Those of floating
1428 nodes are placeded already and therefore are marked.) */
1429 for (i = 0; i < get_irn_n_outs(n); i++) {
1430 if (irn_not_visited(get_irn_out(n, i))) {
1431 pdeq_putr (worklist, get_irn_out(n, i));
1436 static INLINE void place_late(pdeq* worklist) {
1438 inc_irg_visited(current_ir_graph);
1440 /* This fills the worklist initially. */
1441 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
1442 /* And now empty the worklist again... */
1443 while (!pdeq_empty (worklist)) {
1444 ir_node *n = pdeq_getl (worklist);
1445 if (irn_not_visited(n)) place_floats_late(n, worklist);
1449 void place_code(ir_graph *irg) {
1451 ir_graph *rem = current_ir_graph;
1453 current_ir_graph = irg;
1455 if (!(get_opt_optimize() && get_opt_global_cse())) return;
1457 /* Handle graph state */
1458 assert(get_irg_phase_state(irg) != phase_building);
1459 if (get_irg_dom_state(irg) != dom_consistent)
1462 construct_backedges(irg);
1464 /* Place all floating nodes as early as possible. This guarantees
1465 a legal code placement. */
1466 worklist = new_pdeq();
1467 place_early(worklist);
1469 /* place_early invalidates the outs, place_late needs them. */
1471 /* Now move the nodes down in the dominator tree. This reduces the
1472 unnecessary executions of the node. */
1473 place_late(worklist);
1475 set_irg_outs_inconsistent(current_ir_graph);
1477 current_ir_graph = rem;
1482 /********************************************************************/
1483 /* Control flow optimization. */
1484 /* Removes Bad control flow predecessors and empty blocks. A block */
1485 /* is empty if it contains only a Jmp node. */
1486 /* Blocks can only be removed if they are not needed for the */
1487 /* semantics of Phi nodes. */
1488 /********************************************************************/
1490 /* Removes Tuples from Block control flow predecessors.
1491 Optimizes blocks with equivalent_node().
1492 Replaces n by Bad if n is unreachable control flow. */
1493 static void merge_blocks(ir_node *n, void *env) {
1495 set_irn_link(n, NULL);
1497 if (get_irn_op(n) == op_Block) {
1499 for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1500 /* GL @@@ : is this possible? if (get_opt_normalize()) -- added, all tests go through.
1501 A different order of optimizations might cause problems. */
1502 if (get_opt_normalize())
1503 set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1504 } else if (get_opt_optimize() && (get_irn_mode(n) == mode_X)) {
1505 /* We will soon visit a block. Optimize it before visiting! */
1506 ir_node *b = get_nodes_Block(n);
1507 ir_node *new_node = equivalent_node(b);
1508 while (irn_not_visited(b) && (!is_Bad(new_node)) && (new_node != b)) {
1509 /* We would have to run gigo if new is bad, so we
1510 promote it directly below. */
1511 assert(((b == new_node) ||
1512 get_opt_control_flow_straightening() ||
1513 get_opt_control_flow_weak_simplification()) &&
1514 ("strange flag setting"));
1515 exchange (b, new_node);
1517 new_node = equivalent_node(b);
1519 /* GL @@@ get_opt_normalize hinzugefuegt, 5.5.2003 */
1520 if (is_Bad(new_node) && get_opt_normalize()) exchange(n, new_Bad());
1524 /* Collects all Phi nodes in link list of Block.
1525 Marks all blocks "block_visited" if they contain a node other
1527 static void collect_nodes(ir_node *n, void *env) {
1528 if (is_no_Block(n)) {
1529 ir_node *b = get_nodes_Block(n);
1531 if ((get_irn_op(n) == op_Phi)) {
1532 /* Collect Phi nodes to compact ins along with block's ins. */
1533 set_irn_link(n, get_irn_link(b));
1535 } else if (get_irn_op(n) != op_Jmp) { /* Check for non empty block. */
1536 mark_Block_block_visited(b);
1541 /* Returns true if pred is pred of block */
1542 static int is_pred_of(ir_node *pred, ir_node *b) {
1544 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1545 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1546 if (b_pred == pred) return 1;
1551 static int test_whether_dispensable(ir_node *b, int pos) {
1552 int i, j, n_preds = 1;
1553 int dispensable = 1;
1554 ir_node *cfop = get_Block_cfgpred(b, pos);
1555 ir_node *pred = get_nodes_Block(cfop);
1557 if (get_Block_block_visited(pred) + 1
1558 < get_irg_block_visited(current_ir_graph)) {
1559 if (!get_opt_optimize() || !get_opt_control_flow_strong_simplification()) {
1560 /* Mark block so that is will not be removed. */
1561 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1564 /* Seems to be empty. */
1565 if (!get_irn_link(b)) {
1566 /* There are no Phi nodes ==> dispensable. */
1567 n_preds = get_Block_n_cfgpreds(pred);
1569 /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1570 Work preds < pos as if they were already removed. */
1571 for (i = 0; i < pos; i++) {
1572 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1573 if (get_Block_block_visited(b_pred) + 1
1574 < get_irg_block_visited(current_ir_graph)) {
1575 for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1576 ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1577 if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1580 if (is_pred_of(b_pred, pred)) dispensable = 0;
1583 for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1584 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1585 if (is_pred_of(b_pred, pred)) dispensable = 0;
1588 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1591 n_preds = get_Block_n_cfgpreds(pred);
1599 static void optimize_blocks(ir_node *b, void *env) {
1600 int i, j, k, max_preds, n_preds;
1601 ir_node *pred, *phi;
1604 /* Count the number of predecessor if this block is merged with pred blocks
1607 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1608 max_preds += test_whether_dispensable(b, i);
1610 in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1613 printf(" working on "); DDMN(b);
1614 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1615 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1616 if (is_Bad(get_Block_cfgpred(b, i))) {
1617 printf(" removing Bad %i\n ", i);
1618 } else if (get_Block_block_visited(pred) +1
1619 < get_irg_block_visited(current_ir_graph)) {
1620 printf(" removing pred %i ", i); DDMN(pred);
1621 } else { printf(" Nothing to do for "); DDMN(pred); }
1623 * end Debug output **/
1625 /** Fix the Phi nodes **/
1626 phi = get_irn_link(b);
1628 assert(get_irn_op(phi) == op_Phi);
1629 /* Find the new predecessors for the Phi */
1631 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1632 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1633 if (is_Bad(get_Block_cfgpred(b, i))) {
1635 } else if (get_Block_block_visited(pred) +1
1636 < get_irg_block_visited(current_ir_graph)) {
1637 /* It's an empty block and not yet visited. */
1638 ir_node *phi_pred = get_Phi_pred(phi, i);
1639 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1640 if (get_nodes_Block(phi_pred) == pred) {
1641 assert(get_irn_op(phi_pred) == op_Phi); /* Block is empty!! */
1642 in[n_preds] = get_Phi_pred(phi_pred, j);
1644 in[n_preds] = phi_pred;
1648 /* The Phi_pred node is replaced now if it is a Phi.
1649 In Schleifen kann offenbar der entfernte Phi Knoten legal verwendet werden.
1650 Daher muss der Phiknoten durch den neuen ersetzt werden.
1651 Weiter muss der alte Phiknoten entfernt werden (durch ersetzen oder
1652 durch einen Bad) damit er aus den keep_alive verschwinden kann.
1653 Man sollte also, falls keine Schleife vorliegt, exchange mit new_Bad
1655 if (get_nodes_Block(phi_pred) == pred) {
1656 /* remove the Phi as it might be kept alive. Further there
1657 might be other users. */
1658 exchange(phi_pred, phi); /* geht, ist aber doch semantisch falsch! Warum?? */
1661 in[n_preds] = get_Phi_pred(phi, i);
1666 set_irn_in(phi, n_preds, in);
1668 phi = get_irn_link(phi);
1672 This happens only if merge between loop backedge and single loop entry. **/
1673 for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1674 pred = get_nodes_Block(get_Block_cfgpred(b, k));
1675 if (get_Block_block_visited(pred) +1
1676 < get_irg_block_visited(current_ir_graph)) {
1677 phi = get_irn_link(pred);
1679 if (get_irn_op(phi) == op_Phi) {
1680 set_nodes_Block(phi, b);
1683 for (i = 0; i < k; i++) {
1684 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1685 if (is_Bad(get_Block_cfgpred(b, i))) {
1687 } else if (get_Block_block_visited(pred) +1
1688 < get_irg_block_visited(current_ir_graph)) {
1689 /* It's an empty block and not yet visited. */
1690 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1691 /* @@@ Hier brauche ich Schleifeninformation!!! Kontrollflusskante
1692 muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1693 Anweisungen.) Trotzdem tuts bisher!! */
1702 for (i = 0; i < get_Phi_n_preds(phi); i++) {
1703 in[n_preds] = get_Phi_pred(phi, i);
1706 for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1707 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1708 if (is_Bad(get_Block_cfgpred(b, i))) {
1710 } else if (get_Block_block_visited(pred) +1
1711 < get_irg_block_visited(current_ir_graph)) {
1712 /* It's an empty block and not yet visited. */
1713 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1722 set_irn_in(phi, n_preds, in);
1724 phi = get_irn_link(phi);
1729 /** Fix the block **/
1731 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1732 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1733 if (is_Bad(get_Block_cfgpred(b, i))) {
1735 } else if (get_Block_block_visited(pred) +1
1736 < get_irg_block_visited(current_ir_graph)) {
1737 /* It's an empty block and not yet visited. */
1738 assert(get_Block_n_cfgpreds(b) > 1);
1739 /* Else it should be optimized by equivalent_node. */
1740 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1741 in[n_preds] = get_Block_cfgpred(pred, j);
1744 /* Remove block as it might be kept alive. */
1745 exchange(pred, b/*new_Bad()*/);
1747 in[n_preds] = get_Block_cfgpred(b, i);
1751 set_irn_in(b, n_preds, in);
1755 void optimize_cf(ir_graph *irg) {
1758 ir_node *end = get_irg_end(irg);
1759 ir_graph *rem = current_ir_graph;
1760 current_ir_graph = irg;
1762 /* Handle graph state */
1763 assert(get_irg_phase_state(irg) != phase_building);
1764 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1765 set_irg_outs_inconsistent(current_ir_graph);
1766 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1767 set_irg_dom_inconsistent(current_ir_graph);
1769 /* Use block visited flag to mark non-empty blocks. */
1770 inc_irg_block_visited(irg);
1771 irg_walk(end, merge_blocks, collect_nodes, NULL);
1773 /* Optimize the standard code. */
1774 irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1776 /* Walk all keep alives, optimize them if block, add to new in-array
1777 for end if useful. */
1778 in = NEW_ARR_F (ir_node *, 1);
1779 in[0] = get_nodes_Block(end);
1780 inc_irg_visited(current_ir_graph);
1781 for(i = 0; i < get_End_n_keepalives(end); i++) {
1782 ir_node *ka = get_End_keepalive(end, i);
1783 if (irn_not_visited(ka)) {
1784 if ((get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1785 set_irg_block_visited(current_ir_graph, /* Don't walk all the way to Start. */
1786 get_irg_block_visited(current_ir_graph)-1);
1787 irg_block_walk(ka, optimize_blocks, NULL, NULL);
1788 mark_irn_visited(ka);
1789 ARR_APP1 (ir_node *, in, ka);
1790 } else if (get_irn_op(ka) == op_Phi) {
1791 mark_irn_visited(ka);
1792 ARR_APP1 (ir_node *, in, ka);
1796 /* DEL_ARR_F(end->in); GL @@@ tut nicht ! */
1799 current_ir_graph = rem;
1804 * Called by walker of remove_critical_cf_edges.
1806 * Place an empty block to an edge between a blocks of multiple
1807 * predecessors and a block of multiple successors.
1810 * @param env Environment of walker. This field is unused and has
1813 static void walk_critical_cf_edges(ir_node *n, void *env) {
1815 ir_node *pre, *block, **in, *jmp;
1817 /* Block has multiple predecessors */
1818 if ((op_Block == get_irn_op(n)) &&
1819 (get_irn_arity(n) > 1)) {
1820 arity = get_irn_arity(n);
1822 if (n == get_irg_end_block(current_ir_graph))
1823 return; // No use to add a block here.
1825 for (i=0; i<arity; i++) {
1826 pre = get_irn_n(n, i);
1827 /* Predecessor has multiple successors. Insert new flow edge */
1828 if ((NULL != pre) &&
1829 (op_Proj == get_irn_op(pre)) &&
1830 op_Raise != get_irn_op(skip_Proj(pre))) {
1832 /* set predecessor array for new block */
1833 in = NEW_ARR_D (ir_node *, current_ir_graph->obst, 1);
1834 /* set predecessor of new block */
1836 block = new_Block(1, in);
1837 /* insert new jmp node to new block */
1838 switch_block(block);
1841 /* set successor of new block */
1842 set_irn_n(n, i, jmp);
1844 } /* predecessor has multiple successors */
1845 } /* for all predecessors */
1846 } /* n is a block */
1849 void remove_critical_cf_edges(ir_graph *irg) {
1850 if (get_opt_critical_edges())
1851 irg_walk_graph(irg, NULL, walk_critical_cf_edges, NULL);