1 /* Coyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Author: Christian Schaefer
6 ** Optimizations for a whole ir graph, i.e., a procedure.
19 # include "irnode_t.h"
20 # include "irgraph_t.h"
28 # include "pdeq.h" /* Fuer code placement */
31 /* Defined in iropt.c */
32 pset *new_identities (void);
33 void del_identities (pset *value_table);
34 void add_identities (pset *value_table, ir_node *node);
36 /********************************************************************/
37 /* apply optimizations of iropt to all nodes. */
38 /********************************************************************/
40 void init_link (ir_node *n, void *env) {
41 set_irn_link(n, NULL);
45 optimize_in_place_wrapper (ir_node *n, void *env) {
49 for (i = 0; i < get_irn_arity(n); i++) {
50 optimized = optimize_in_place_2(get_irn_n(n, i));
51 set_irn_n(n, i, optimized);
54 if (get_irn_op(n) == op_Block) {
55 optimized = optimize_in_place_2(n);
56 if (optimized != n) exchange (n, optimized);
61 local_optimize_graph (ir_graph *irg) {
62 ir_graph *rem = current_ir_graph;
63 current_ir_graph = irg;
65 /* Handle graph state */
66 assert(get_irg_phase_state(irg) != phase_building);
67 if (get_opt_global_cse())
68 set_irg_pinned(current_ir_graph, floats);
69 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
70 set_irg_outs_inconsistent(current_ir_graph);
71 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
72 set_irg_dom_inconsistent(current_ir_graph);
74 /* Clean the value_table in irg for the cse. */
75 del_identities(irg->value_table);
76 irg->value_table = new_identities();
78 /* walk over the graph */
79 irg_walk(irg->end, init_link, optimize_in_place_wrapper, NULL);
81 current_ir_graph = rem;
84 /********************************************************************/
85 /* Routines for dead node elimination / copying garbage collection */
87 /********************************************************************/
89 /* Remeber the new node in the old node by using a field all nodes have. */
91 set_new_node (ir_node *old, ir_node *new)
96 /* Get this new node, before the old node is forgotton.*/
98 get_new_node (ir_node * n)
103 /* We use the block_visited flag to mark that we have computed the
104 number of useful predecessors for this block.
105 Further we encode the new arity in this flag in the old blocks.
106 Remembering the arity is useful, as it saves a lot of pointer
107 accesses. This function is called for all Phi and Block nodes
110 compute_new_arity(ir_node *b) {
114 irg_v = get_irg_block_visited(current_ir_graph);
115 block_v = get_Block_block_visited(b);
116 if (block_v >= irg_v) {
117 /* we computed the number of preds for this block and saved it in the
119 return block_v - irg_v;
121 /* compute the number of good predecessors */
122 res = get_irn_arity(b);
123 for (i = 0; i < get_irn_arity(b); i++)
124 if (get_irn_opcode(get_irn_n(b, i)) == iro_Bad) res--;
125 /* save it in the flag. */
126 set_Block_block_visited(b, irg_v + res);
131 /* Copies the node to the new obstack. The Ins of the new node point to
132 the predecessors on the old obstack. For block/phi nodes not all
133 predecessors might be copied. n->link points to the new node.
134 For Phi and Block nodes the function allocates in-arrays with an arity
135 only for useful predecessors. The arity is determined by counting
136 the non-bad predecessors of the block. */
138 copy_node (ir_node *n, void *env) {
142 if (get_irn_opcode(n) == iro_Block) {
144 new_arity = compute_new_arity(n);
146 block = get_nodes_Block(n);
147 if (get_irn_opcode(n) == iro_Phi) {
148 new_arity = compute_new_arity(block);
150 new_arity = get_irn_arity(n);
153 nn = new_ir_node(get_irn_dbg_info(n),
160 /* Copy the attributes. These might point to additional data. If this
161 was allocated on the old obstack the pointers now are dangling. This
162 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
166 /* printf("\n old node: "); DDMSG2(n);
167 printf(" new node: "); DDMSG2(nn); */
171 /* Copies new predecessors of old node to new node remembered in link.
172 Spare the Bad predecessors of Phi and Block nodes. */
174 copy_preds (ir_node *n, void *env) {
178 nn = get_new_node(n);
180 /* printf("\n old node: "); DDMSG2(n);
181 printf(" new node: "); DDMSG2(nn);
182 printf(" arities: old: %d, new: %d\n", get_irn_arity(n), get_irn_arity(nn)); */
184 if (get_irn_opcode(n) == iro_Block) {
185 /* Don't copy Bad nodes. */
187 for (i = 0; i < get_irn_arity(n); i++)
188 if (get_irn_opcode(get_irn_n(n, i)) != iro_Bad) {
189 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
192 /* repair the block visited flag from above misuse. Repair it in both
193 graphs so that the old one can still be used. */
194 set_Block_block_visited(nn, 0);
195 set_Block_block_visited(n, 0);
196 /* Local optimization could not merge two subsequent blocks if
197 in array contained Bads. Now it's possible.
198 We don't call optimize_in_place as it requires
199 that the fields in ir_graph are set properly. */
200 if ((get_opt_control_flow()) &&
201 (get_Block_n_cfgpreds(nn) == 1) &&
202 (get_irn_op(get_Block_cfgpred(nn, 0)) == op_Jmp))
203 exchange(nn, get_nodes_Block(get_Block_cfgpred(nn, 0)));
204 } else if (get_irn_opcode(n) == iro_Phi) {
205 /* Don't copy node if corresponding predecessor in block is Bad.
206 The Block itself should not be Bad. */
207 block = get_nodes_Block(n);
208 set_irn_n (nn, -1, get_new_node(block));
210 for (i = 0; i < get_irn_arity(n); i++)
211 if (get_irn_opcode(get_irn_n(block, i)) != iro_Bad) {
212 set_irn_n (nn, j, get_new_node(get_irn_n(n, i)));
215 /* If the pre walker reached this Phi after the post walker visited the
216 block block_visited is > 0. */
217 set_Block_block_visited(get_nodes_Block(n), 0);
218 /* Compacting the Phi's ins might generate Phis with only one
220 if (get_irn_arity(n) == 1)
221 exchange(n, get_irn_n(n, 0));
223 for (i = -1; i < get_irn_arity(n); i++)
224 set_irn_n (nn, i, get_new_node(get_irn_n(n, i)));
226 /* Now the new node is complete. We can add it to the hash table for cse.
227 @@@ inlinening aborts if we identify End. Why? */
228 if(get_irn_op(nn) != op_End)
229 add_identities (current_ir_graph->value_table, nn);
232 /* Copies the graph recursively, compacts the keepalive of the end node. */
235 ir_node *oe, *ne; /* old end, new end */
236 ir_node *ka; /* keep alive */
239 oe = get_irg_end(current_ir_graph);
240 /* copy the end node by hand, allocate dynamic in array! */
241 ne = new_ir_node(get_irn_dbg_info(oe),
248 /* Copy the attributes. Well, there might be some in the future... */
250 set_new_node(oe, ne);
252 /* copy the live nodes */
253 irg_walk(get_nodes_Block(oe), copy_node, copy_preds, NULL);
254 /* copy_preds for the end node ... */
255 set_nodes_Block(ne, get_new_node(get_nodes_Block(oe)));
257 /** ... and now the keep alives. **/
258 /* First pick the not marked block nodes and walk them. We must pick these
259 first as else we will oversee blocks reachable from Phis. */
260 for (i = 0; i < get_irn_arity(oe); i++) {
261 ka = get_irn_n(oe, i);
262 if ((get_irn_op(ka) == op_Block) &&
263 (get_irn_visited(ka) < get_irg_visited(current_ir_graph))) {
264 /* We must keep the block alive and copy everything reachable */
265 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
266 irg_walk(ka, copy_node, copy_preds, NULL);
267 add_End_keepalive(ne, get_new_node(ka));
271 /* Now pick the Phis. Here we will keep all! */
272 for (i = 0; i < get_irn_arity(oe); i++) {
273 ka = get_irn_n(oe, i);
274 if ((get_irn_op(ka) == op_Phi)) {
275 if (get_irn_visited(ka) < get_irg_visited(current_ir_graph)) {
276 /* We didn't copy the Phi yet. */
277 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
278 irg_walk(ka, copy_node, copy_preds, NULL);
280 add_End_keepalive(ne, get_new_node(ka));
285 /* Copies the graph reachable from current_ir_graph->end to the obstack
286 in current_ir_graph and fixes the environment.
287 Then fixes the fields in current_ir_graph containing nodes of the
291 /* Not all nodes remembered in current_ir_graph might be reachable
292 from the end node. Assure their link is set to NULL, so that
293 we can test whether new nodes have been computed. */
294 set_irn_link(get_irg_frame (current_ir_graph), NULL);
295 set_irn_link(get_irg_globals(current_ir_graph), NULL);
296 set_irn_link(get_irg_args (current_ir_graph), NULL);
298 /* we use the block walk flag for removing Bads from Blocks ins. */
299 inc_irg_block_visited(current_ir_graph);
304 /* fix the fields in current_ir_graph */
305 free_End(get_irg_end(current_ir_graph));
306 set_irg_end (current_ir_graph, get_new_node(get_irg_end(current_ir_graph)));
307 set_irg_end_block (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
308 if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
309 copy_node (get_irg_frame(current_ir_graph), NULL);
310 copy_preds(get_irg_frame(current_ir_graph), NULL);
312 if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
313 copy_node (get_irg_globals(current_ir_graph), NULL);
314 copy_preds(get_irg_globals(current_ir_graph), NULL);
316 if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
317 copy_node (get_irg_args(current_ir_graph), NULL);
318 copy_preds(get_irg_args(current_ir_graph), NULL);
320 set_irg_start (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
322 set_irg_start_block(current_ir_graph,
323 get_new_node(get_irg_start_block(current_ir_graph)));
324 set_irg_frame (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
325 set_irg_globals(current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
326 set_irg_args (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
327 if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
328 copy_node(get_irg_bad(current_ir_graph), NULL);
329 copy_preds(get_irg_bad(current_ir_graph), NULL);
331 set_irg_bad(current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
332 if (get_irn_link(get_irg_unknown(current_ir_graph)) == NULL) {
333 copy_node(get_irg_unknown(current_ir_graph), NULL);
334 copy_preds(get_irg_unknown(current_ir_graph), NULL);
336 set_irg_unknown(current_ir_graph, get_new_node(get_irg_unknown(current_ir_graph)));
339 /* Copies all reachable nodes to a new obstack. Removes bad inputs
340 from block nodes and the corresponding inputs from Phi nodes.
341 Merges single exit blocks with single entry blocks and removes
343 Adds all new nodes to a new hash table for cse. Does not
344 perform cse, so the hash table might contain common subexpressions. */
345 /* Amroq call this emigrate() */
347 dead_node_elimination(ir_graph *irg) {
349 struct obstack *graveyard_obst = NULL;
350 struct obstack *rebirth_obst = NULL;
352 /* Remember external state of current_ir_graph. */
353 rem = current_ir_graph;
354 current_ir_graph = irg;
356 /* Handle graph state */
357 assert(get_irg_phase_state(current_ir_graph) != phase_building);
358 free_outs(current_ir_graph);
360 if (get_optimize() && get_opt_dead_node_elimination()) {
362 /* A quiet place, where the old obstack can rest in peace,
363 until it will be cremated. */
364 graveyard_obst = irg->obst;
366 /* A new obstack, where the reachable nodes will be copied to. */
367 rebirth_obst = (struct obstack *) xmalloc (sizeof (struct obstack));
368 current_ir_graph->obst = rebirth_obst;
369 obstack_init (current_ir_graph->obst);
371 /* We also need a new hash table for cse */
372 del_identities (irg->value_table);
373 irg->value_table = new_identities ();
375 /* Copy the graph from the old to the new obstack */
378 /* Free memory from old unoptimized obstack */
379 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
380 xfree (graveyard_obst); /* ... then free it. */
383 current_ir_graph = rem;
386 /**********************************************************************/
387 /* Funcionality for inlining */
388 /**********************************************************************/
390 /* Copy node for inlineing. Copies the node by calling copy_node and
391 then updates the entity if it's a local one. env must be a pointer
392 to the frame type of the procedure. The new entities must be in
393 the link field of the entities. */
395 copy_node_inline (ir_node *n, void *env) {
397 type *frame_tp = (type *)env;
400 if (get_irn_op(n) == op_Sel) {
401 new = get_new_node (n);
402 assert(get_irn_op(new) == op_Sel);
403 if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
404 set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
409 void inline_method(ir_node *call, ir_graph *called_graph) {
411 ir_node *post_call, *post_bl;
413 ir_node *end, *end_bl;
417 ir_node *cf_op = NULL, *bl;
418 int arity, n_ret, n_exc, n_res, i, j, rem_opt;
421 if (!get_optimize() || !get_opt_inline()) return;
422 /** Turn off optimizations, this can cause problems when allocating new nodes. **/
423 rem_opt = get_optimize();
426 /* Handle graph state */
427 assert(get_irg_phase_state(current_ir_graph) != phase_building);
428 assert(get_irg_pinned(current_ir_graph) == pinned);
429 assert(get_irg_pinned(called_graph) == pinned);
430 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
431 set_irg_outs_inconsistent(current_ir_graph);
433 /** Check preconditions **/
434 assert(get_irn_op(call) == op_Call);
435 /* assert(get_Call_type(call) == get_entity_type(get_irg_ent(called_graph))); */
437 @@@ TODO does not work for InterfaceIII.java after cgana
438 assert(smaller_type(get_entity_type(get_irg_ent(called_graph)),
439 get_Call_type(call)));
441 assert(get_type_tpop(get_Call_type(call)) == type_method);
442 if (called_graph == current_ir_graph) return;
445 /** Part the Call node into two nodes. Pre_call collects the parameters of
446 the procedure and later replaces the Start node of the called graph.
447 Post_call is the old Call node and collects the results of the called
448 graph. Both will end up being a tuple. **/
449 post_bl = get_nodes_Block(call);
450 set_irg_current_block(current_ir_graph, post_bl);
451 /* XxMxPxP of Start + parameter of Call */
453 in[1] = get_Call_mem(call);
454 in[2] = get_irg_frame(current_ir_graph);
455 in[3] = get_irg_globals(current_ir_graph);
456 in[4] = new_Tuple (get_Call_n_params(call), get_Call_param_arr(call));
457 pre_call = new_Tuple(5, in);
460 /** Part the block of the Call node into two blocks.
461 The new block gets the ins of the old block, pre_call and all its
462 predecessors and all Phi nodes. **/
463 part_block(pre_call);
465 /** Prepare state for dead node elimination **/
466 /* Visited flags in calling irg must be >= flag in called irg.
467 Else walker and arity computation will not work. */
468 if (get_irg_visited(current_ir_graph) <= get_irg_visited(called_graph))
469 set_irg_visited(current_ir_graph, get_irg_visited(called_graph)+1); /***/
470 if (get_irg_block_visited(current_ir_graph)< get_irg_block_visited(called_graph))
471 set_irg_block_visited(current_ir_graph, get_irg_block_visited(called_graph));
472 /* Set pre_call as new Start node in link field of the start node of
473 calling graph and pre_calls block as new block for the start block
475 Further mark these nodes so that they are not visited by the
477 set_irn_link(get_irg_start(called_graph), pre_call);
478 set_irn_visited(get_irg_start(called_graph),
479 get_irg_visited(current_ir_graph));/***/
480 set_irn_link(get_irg_start_block(called_graph),
481 get_nodes_Block(pre_call));
482 set_irn_visited(get_irg_start_block(called_graph),
483 get_irg_visited(current_ir_graph)); /***/
485 /* Initialize for compaction of in arrays */
486 inc_irg_block_visited(current_ir_graph);
488 /*** Replicate local entities of the called_graph ***/
489 /* copy the entities. */
490 called_frame = get_irg_frame_type(called_graph);
491 for (i = 0; i < get_class_n_members(called_frame); i++) {
492 entity *new_ent, *old_ent;
493 old_ent = get_class_member(called_frame, i);
494 new_ent = copy_entity_own(old_ent, get_cur_frame_type());
495 set_entity_link(old_ent, new_ent);
498 /* visited is > than that of called graph. With this trick visited will
499 remain unchanged so that an outer walker, e.g., searching the call nodes
500 to inline, calling this inline will not visit the inlined nodes. */
501 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
503 /** Performing dead node elimination inlines the graph **/
504 /* Copies the nodes to the obstack of current_ir_graph. Updates links to new
506 /* @@@ endless loops are not copied!! */
507 irg_walk(get_irg_end(called_graph), copy_node_inline, copy_preds,
508 get_irg_frame_type(called_graph));
510 /* Repair called_graph */
511 set_irg_visited(called_graph, get_irg_visited(current_ir_graph));
512 set_irg_block_visited(called_graph, get_irg_block_visited(current_ir_graph));
513 set_Block_block_visited(get_irg_start_block(called_graph), 0);
515 /*** Merge the end of the inlined procedure with the call site ***/
516 /* We will turn the old Call node into a Tuple with the following
519 0: Phi of all Memories of Return statements.
520 1: Jmp from new Block that merges the control flow from all exception
521 predecessors of the old end block.
522 2: Tuple of all arguments.
523 3: Phi of Exception memories.
526 /** Precompute some values **/
527 end_bl = get_new_node(get_irg_end_block(called_graph));
528 end = get_new_node(get_irg_end(called_graph));
529 arity = get_irn_arity(end_bl); /* arity = n_exc + n_ret */
530 n_res = get_method_n_ress(get_Call_type(call));
532 res_pred = (ir_node **) malloc (n_res * sizeof (ir_node *));
533 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
535 set_irg_current_block(current_ir_graph, post_bl); /* just to make sure */
537 /** archive keepalives **/
538 for (i = 0; i < get_irn_arity(end); i++)
539 add_End_keepalive(get_irg_end(current_ir_graph), get_irn_n(end, i));
540 /* The new end node will die, but the in array is not on the obstack ... */
543 /** Collect control flow from Return blocks to post_calls block. Replace
544 Return nodes by Jump nodes. **/
546 for (i = 0; i < arity; i++) {
548 ret = get_irn_n(end_bl, i);
549 if (get_irn_op(ret) == op_Return) {
550 cf_pred[n_ret] = new_r_Jmp(current_ir_graph, get_nodes_Block(ret));
554 set_irn_in(post_bl, n_ret, cf_pred);
556 /** Collect results from Return nodes to post_call. Post_call is
557 turned into a tuple. **/
558 turn_into_tuple(post_call, 4);
559 /* First the Memory-Phi */
561 for (i = 0; i < arity; i++) {
562 ret = get_irn_n(end_bl, i);
563 if (get_irn_op(ret) == op_Return) {
564 cf_pred[n_ret] = get_Return_mem(ret);
568 phi = new_Phi(n_ret, cf_pred, mode_M);
569 set_Tuple_pred(call, 0, phi);
570 set_irn_link(phi, get_irn_link(post_bl)); /* Conserve Phi-list for further inlinings */
571 set_irn_link(post_bl, phi);
572 /* Now the real results */
574 for (j = 0; j < n_res; j++) {
576 for (i = 0; i < arity; i++) {
577 ret = get_irn_n(end_bl, i);
578 if (get_irn_op(ret) == op_Return) {
579 cf_pred[n_ret] = get_Return_res(ret, j);
583 phi = new_Phi(n_ret, cf_pred, get_irn_mode(cf_pred[0]));
585 set_irn_link(phi, get_irn_link(post_bl)); /* Conserve Phi-list for further inlinings */
586 set_irn_link(post_bl, phi);
588 set_Tuple_pred(call, 2, new_Tuple(n_res, res_pred));
590 set_Tuple_pred(call, 2, new_Bad());
592 /* Finally the exception control flow. We need to add a Phi node to
593 collect the memory containing the exception objects. Further we need
594 to add another block to get a correct representation of this Phi. To
595 this block we add a Jmp that resolves into the X output of the Call
596 when the Call is turned into a tuple. */
598 for (i = 0; i < arity; i++) {
600 ret = get_irn_n(end_bl, i);
601 if (is_fragile_op(skip_Proj(ret)) || (get_irn_op(skip_Proj(ret)) == op_Raise)) {
602 cf_pred[n_exc] = ret;
607 new_Block(n_exc, cf_pred); /* whatch it: current_block is changed! */
608 set_Tuple_pred(call, 1, new_Jmp());
609 /* The Phi for the memories with the exception objects */
611 for (i = 0; i < arity; i++) {
613 ret = skip_Proj(get_irn_n(end_bl, i));
614 if (get_irn_op(ret) == op_Call) {
615 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 3);
617 } else if (is_fragile_op(ret)) {
618 /* We rely that all cfops have the memory output at the same position. */
619 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 0);
621 } else if (get_irn_op(ret) == op_Raise) {
622 cf_pred[n_exc] = new_r_Proj(current_ir_graph, get_nodes_Block(ret), ret, mode_M, 1);
626 set_Tuple_pred(call, 3, new_Phi(n_exc, cf_pred, mode_M));
628 set_Tuple_pred(call, 1, new_Bad());
629 set_Tuple_pred(call, 3, new_Bad());
634 /*** Correct the control flow to the end node.
635 If the exception control flow from the Call directly branched to the
636 end block we now have the following control flow predecessor pattern:
637 ProjX -> Tuple -> Jmp.
638 We must remove the Jmp along with it's empty block and add Jmp's
639 predecessors as predecessors of this end block. ***/
640 /* find the problematic predecessor of the end block. */
641 end_bl = get_irg_end_block(current_ir_graph);
642 for (i = 0; i < get_Block_n_cfgpreds(end_bl); i++) {
643 cf_op = get_Block_cfgpred(end_bl, i);
644 if (get_irn_op(cf_op) == op_Proj) {
645 cf_op = get_Proj_pred(cf_op);
646 if (get_irn_op(cf_op) == op_Tuple) {
647 cf_op = get_Tuple_pred(cf_op, 1);
648 assert(get_irn_op(cf_op) == op_Jmp);
654 if (i < get_Block_n_cfgpreds(end_bl)) {
655 bl = get_nodes_Block(cf_op);
656 arity = get_Block_n_cfgpreds(end_bl) + get_Block_n_cfgpreds(bl) - 1;
657 cf_pred = (ir_node **) malloc (arity * sizeof (ir_node *));
658 for (j = 0; j < i; j++)
659 cf_pred[j] = get_Block_cfgpred(end_bl, j);
660 for (j = j; j < i + get_Block_n_cfgpreds(bl); j++)
661 cf_pred[j] = get_Block_cfgpred(bl, j-i);
662 for (j = j; j < arity; j++)
663 cf_pred[j] = get_Block_cfgpred(end_bl, j-get_Block_n_cfgpreds(bl) +1);
664 set_irn_in(end_bl, arity, cf_pred);
668 /** Turn cse back on. **/
669 set_optimize(rem_opt);
672 /********************************************************************/
673 /* Apply inlineing to small methods. */
674 /********************************************************************/
678 /* It makes no sense to inline too many calls in one procedure. Anyways,
679 I didn't get a version with NEW_ARR_F to run. */
680 #define MAX_INLINE 1024
682 static void collect_calls(ir_node *call, void *env) {
683 ir_node **calls = (ir_node **)env;
686 ir_graph *called_irg;
688 if (get_irn_op(call) != op_Call) return;
690 addr = get_Call_ptr(call);
691 if (get_irn_op(addr) == op_Const) {
692 /* Check whether the constant is the pointer to a compiled entity. */
693 tv = get_Const_tarval(addr);
695 called_irg = get_entity_irg(tv->u.p.ent);
696 if (called_irg && pos < MAX_INLINE) {
697 /* The Call node calls a locally defined method. Remember to inline. */
706 /* Inlines all small methods at call sites where the called address comes
707 from a Const node that references the entity representing the called
709 The size argument is a rough measure for the code size of the method:
710 Methods where the obstack containing the firm graph is smaller than
712 void inline_small_irgs(ir_graph *irg, int size) {
714 ir_node *calls[MAX_INLINE];
715 ir_graph *rem = current_ir_graph;
717 if (!(get_optimize() && get_opt_inline())) return;
719 /*DDME(get_irg_ent(current_ir_graph));*/
721 current_ir_graph = irg;
722 /* Handle graph state */
723 assert(get_irg_phase_state(current_ir_graph) != phase_building);
725 /* Find Call nodes to inline.
726 (We can not inline during a walk of the graph, as inlineing the same
727 method several times changes the visited flag of the walked graph:
728 after the first inlineing visited of the callee equals visited of
729 the caller. With the next inlineing both are increased.) */
731 irg_walk(get_irg_end(irg), NULL, collect_calls, (void *) calls);
733 if ((pos > 0) && (pos < MAX_INLINE)) {
734 /* There are calls to inline */
735 collect_phiprojs(irg);
736 for (i = 0; i < pos; i++) {
739 tv = get_Const_tarval(get_Call_ptr(calls[i]));
740 callee = get_entity_irg(tv->u.p.ent);
741 if ((_obstack_memory_used(callee->obst) - obstack_room(callee->obst)) < size) {
742 /*printf(" inlineing "); DDME(tv->u.p.ent);*/
743 inline_method(calls[i], callee);
748 current_ir_graph = rem;
752 /********************************************************************/
753 /* Code Placement. Pinns all floating nodes to a block where they */
754 /* will be executed only if needed. */
755 /********************************************************************/
757 static pdeq *worklist; /* worklist of ir_node*s */
759 /* Find the earliest correct block for N. --- Place N into the
760 same Block as its dominance-deepest Input. */
762 place_floats_early (ir_node *n)
766 /* we must not run into an infinite loop */
767 assert (irn_not_visited(n));
770 /* Place floating nodes. */
771 if (get_op_pinned(get_irn_op(n)) == floats) {
773 ir_node *b = new_Bad(); /* The block to place this node in */
775 assert(get_irn_op(n) != op_Block);
777 if ((get_irn_op(n) == op_Const) ||
778 (get_irn_op(n) == op_SymConst) ||
780 /* These nodes will not be placed by the loop below. */
781 b = get_irg_start_block(current_ir_graph);
785 /* find the block for this node. */
786 for (i = 0; i < get_irn_arity(n); i++) {
787 ir_node *dep = get_irn_n(n, i);
789 if ((irn_not_visited(dep)) &&
790 (get_op_pinned(get_irn_op(dep)) == floats)) {
791 place_floats_early (dep);
793 /* Because all loops contain at least one pinned node, now all
794 our inputs are either pinned or place_early has already
795 been finished on them. We do not have any unfinished inputs! */
796 dep_block = get_nodes_Block(dep);
797 if ((!is_Bad(dep_block)) &&
798 (get_Block_dom_depth(dep_block) > depth)) {
800 depth = get_Block_dom_depth(dep_block);
802 /* Avoid that the node is placed in the Start block */
803 if ((depth == 1) && (get_Block_dom_depth(get_nodes_Block(n)) > 1)) {
804 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
805 assert(b != get_irg_start_block(current_ir_graph));
809 set_nodes_Block(n, b);
812 /* Add predecessors of non floating nodes on worklist. */
813 start = (get_irn_op(n) == op_Block) ? 0 : -1;
814 for (i = start; i < get_irn_arity(n); i++) {
815 ir_node *pred = get_irn_n(n, i);
816 if (irn_not_visited(pred)) {
817 pdeq_putr (worklist, pred);
822 /* Floating nodes form subgraphs that begin at nodes as Const, Load,
823 Start, Call and end at pinned nodes as Store, Call. Place_early
824 places all floating nodes reachable from its argument through floating
825 nodes and adds all beginnings at pinned nodes to the worklist. */
826 INLINE void place_early () {
828 inc_irg_visited(current_ir_graph);
830 /* this inits the worklist */
831 place_floats_early (get_irg_end(current_ir_graph));
833 /* Work the content of the worklist. */
834 while (!pdeq_empty (worklist)) {
835 ir_node *n = pdeq_getl (worklist);
836 if (irn_not_visited(n)) place_floats_early (n);
839 set_irg_outs_inconsistent(current_ir_graph);
840 current_ir_graph->pinned = pinned;
844 /* deepest common dominance ancestor of DCA and CONSUMER of PRODUCER */
846 consumer_dom_dca (ir_node *dca, ir_node *consumer, ir_node *producer)
848 ir_node *block = NULL;
850 /* Compute the latest block into which we can place a node so that it is
852 if (get_irn_op(consumer) == op_Phi) {
853 /* our comsumer is a Phi-node, the effective use is in all those
854 blocks through which the Phi-node reaches producer */
856 ir_node *phi_block = get_nodes_Block(consumer);
857 for (i = 0; i < get_irn_arity(consumer); i++) {
858 if (get_irn_n(consumer, i) == producer) {
859 block = get_nodes_Block(get_Block_cfgpred(phi_block, i));
863 assert(is_no_Block(consumer));
864 block = get_nodes_Block(consumer);
867 /* Compute the deepest common ancestor of block and dca. */
869 if (!dca) return block;
870 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
871 block = get_Block_idom(block);
872 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block))
873 dca = get_Block_idom(dca);
875 { block = get_Block_idom(block); dca = get_Block_idom(dca); }
881 /* @@@ Needs loop informations. Will implement later interprocedural. */
883 move_out_of_loops (ir_node *n, ir_node *dca)
887 /* Find the region deepest in the dominator tree dominating
888 dca with the least loop nesting depth, but still dominated
889 by our early placement. */
891 while (dca != get_nodes_Block(n)) {
892 dca = get_Block_idom(dca);
893 if (!dca) break; /* should we put assert(dca)? */
894 if (get_Block_loop_depth(dca) < get_Block_loop_depth(best)) {
898 if (get_Block_dom_depth(best) >= get_Block_dom_depth(get_nodes_Block(n)))
899 set_nodes_Block(n, best);
903 /* Find the latest legal block for N and place N into the
904 `optimal' Block between the latest and earliest legal block.
905 The `optimal' block is the dominance-deepest block of those
906 with the least loop-nesting-depth. This places N out of as many
907 loops as possible and then makes it as controldependant as
910 place_floats_late (ir_node *n)
914 assert (irn_not_visited(n)); /* no multiple placement */
916 /* no need to place block nodes, control nodes are already placed. */
917 if ((get_irn_op(n) != op_Block) && (!is_cfop(n)) && (get_irn_mode(n) != mode_X)) {
918 /* Assure that our users are all placed, except the Phi-nodes.
919 --- Each dataflow cycle contains at least one Phi-node. We
920 have to break the `user has to be placed before the
921 producer' dependance cycle and the Phi-nodes are the
922 place to do so, because we need to base our placement on the
923 final region of our users, which is OK with Phi-nodes, as they
924 are pinned, and they never have to be placed after a
925 producer of one of their inputs in the same block anyway. */
926 for (i = 0; i < get_irn_n_outs(n); i++) {
927 ir_node *succ = get_irn_out(n, i);
928 if (irn_not_visited(succ) && (get_irn_op(succ) != op_Phi))
929 place_floats_late (succ);
932 /* We have to determine the final block of this node... except for constants. */
933 if ((get_op_pinned(get_irn_op(n)) == floats) &&
934 (get_irn_op(n) != op_Const) &&
935 (get_irn_op(n) != op_SymConst)) {
936 ir_node *dca = NULL; /* deepest common ancestor in the
937 dominator tree of all nodes'
938 blocks depending on us; our final
939 placement has to dominate DCA. */
940 for (i = 0; i < get_irn_n_outs(n); i++) {
941 dca = consumer_dom_dca (dca, get_irn_out(n, i), n);
943 set_nodes_Block(n, dca);
945 move_out_of_loops (n, dca);
952 /* Add predecessors of all non-floating nodes on list. (Those of floating
953 nodes are placeded already and therefore are marked.) */
954 for (i = 0; i < get_irn_n_outs(n); i++) {
955 if (irn_not_visited(get_irn_out(n, i))) {
956 pdeq_putr (worklist, get_irn_out(n, i));
961 INLINE void place_late() {
963 inc_irg_visited(current_ir_graph);
965 /* This fills the worklist initially. */
966 place_floats_late(get_irg_start_block(current_ir_graph));
967 /* And now empty the worklist again... */
968 while (!pdeq_empty (worklist)) {
969 ir_node *n = pdeq_getl (worklist);
970 if (irn_not_visited(n)) place_floats_late(n);
974 void place_code(ir_graph *irg) {
975 ir_graph *rem = current_ir_graph;
976 current_ir_graph = irg;
978 if (!(get_optimize() && get_opt_global_cse())) return;
980 /* Handle graph state */
981 assert(get_irg_phase_state(irg) != phase_building);
982 if (get_irg_dom_state(irg) != dom_consistent)
985 /* Place all floating nodes as early as possible. This guarantees
986 a legal code placement. */
987 worklist = new_pdeq ();
990 /* place_early invalidates the outs, place_late needs them. */
992 /* Now move the nodes down in the dominator tree. This reduces the
993 unnecessary executions of the node. */
996 set_irg_outs_inconsistent(current_ir_graph);
998 current_ir_graph = rem;
1003 /********************************************************************/
1004 /* Control flow optimization. */
1005 /* Removes Bad control flow predecessors and empty blocks. A block */
1006 /* is empty if it contains only a Jmp node. */
1008 /********************************************************************/
1011 static void merge_blocks(ir_node *n, void *env) {
1013 set_irn_link(n, NULL);
1015 if (get_irn_op(n) == op_Block) {
1017 for (i = 0; i < get_Block_n_cfgpreds(n); i++)
1018 set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
1019 } else if (get_irn_mode(n) == mode_X) {
1020 /* We will soon visit a block. Optimize it before visiting! */
1021 ir_node *b = get_nodes_Block(n);
1022 ir_node *new = equivalent_node(b);
1023 while (irn_not_visited(b) && (!is_Bad(new)) && (new != b)) {
1024 /* We would have to run gigo if new is bad. */
1025 if (get_optimize() && get_opt_control_flow()) exchange (b, new);
1027 new = equivalent_node(b);
1029 if (is_Bad(new)) exchange (n, new_Bad());
1033 static void collect_nodes(ir_node *n, void *env) {
1034 if (is_no_Block(n)) {
1035 ir_node *b = get_nodes_Block(n);
1037 if ((get_irn_op(n) == op_Phi)) {
1038 /* Collect Phi nodes to compact ins along with block's ins. */
1039 set_irn_link(n, get_irn_link(b));
1041 } else if (get_irn_op(n) != op_Jmp) { /* Check for non empty block. */
1042 mark_Block_block_visited(b);
1047 /* Returns true if pred is pred of block */
1048 int is_pred_of(ir_node *pred, ir_node *b) {
1050 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1051 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1052 if (b_pred == pred) return 1;
1057 int test_whether_dispensable(ir_node *b, int pos) {
1058 int i, j, n_preds = 1;
1059 int dispensable = 1;
1060 ir_node *cfop = get_Block_cfgpred(b, pos);
1061 ir_node *pred = get_nodes_Block(cfop);
1063 if (get_Block_block_visited(pred) + 1
1064 < get_irg_block_visited(current_ir_graph)) {
1065 if (!get_optimize() || !get_opt_control_flow()) {
1066 /* Mark block so that is will not be removed. */
1067 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1070 /* Seems to be empty. */
1071 if (!get_irn_link(b)) {
1072 /* There are no Phi nodes ==> dispensable. */
1073 n_preds = get_Block_n_cfgpreds(pred);
1075 /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
1076 Work preds < pos as if they were already removed. */
1077 for (i = 0; i < pos; i++) {
1078 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1079 if (get_Block_block_visited(b_pred) + 1
1080 < get_irg_block_visited(current_ir_graph)) {
1081 for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) {
1082 ir_node *b_pred_pred = get_nodes_Block(get_Block_cfgpred(b_pred, j));
1083 if (is_pred_of(b_pred_pred, pred)) dispensable = 0;
1086 if (is_pred_of(b_pred, pred)) dispensable = 0;
1089 for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) {
1090 ir_node *b_pred = get_nodes_Block(get_Block_cfgpred(b, i));
1091 if (is_pred_of(b_pred, pred)) dispensable = 0;
1094 set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1);
1097 n_preds = get_Block_n_cfgpreds(pred);
1105 void optimize_blocks(ir_node *b, void *env) {
1106 int i, j, k, max_preds, n_preds;
1107 ir_node *pred, *phi;
1111 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1112 pred = get_Block_cfgpred(b, i);
1113 max_preds += test_whether_dispensable(b, i);
1115 in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
1119 printf(" working on "); DDMN(b);
1120 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1121 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1122 if (is_Bad(get_Block_cfgpred(b, i))) {
1123 printf(" removing Bad %i\n ", i);
1124 } else if (get_Block_block_visited(pred) +1
1125 < get_irg_block_visited(current_ir_graph)) {
1126 printf(" removing pred %i ", i); DDMN(pred);
1127 } else { printf(" Nothing to do for "); DDMN(pred); }
1129 ** end Debug output **/
1131 /** Fix the Phi nodes **/
1132 phi = get_irn_link(b);
1134 assert(get_irn_op(phi) == op_Phi);
1135 /* Find the new predecessors for the Phi */
1137 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1138 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1139 if (is_Bad(get_Block_cfgpred(b, i))) {
1141 } else if (get_Block_block_visited(pred) +1
1142 < get_irg_block_visited(current_ir_graph)) {
1143 /* It's an empty block and not yet visited. */
1144 ir_node *phi_pred = get_Phi_pred(phi, i);
1145 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1146 if (get_nodes_Block(phi_pred) == pred) {
1147 assert(get_irn_op(phi_pred) == op_Phi); /* Block is empty!! */
1148 in[n_preds] = get_Phi_pred(phi_pred, j);
1150 in[n_preds] = phi_pred;
1155 /* @@@ hier brauche ich schleifeninformation!!! Wenn keine Rueckwaertskante
1156 dann darfs auch keine Verwendung geben. */
1157 if (get_nodes_Block(phi_pred) == pred) {
1158 /* remove the Phi as it might be kept alive. Further there
1159 might be other users. */
1160 exchange(phi_pred, phi); /* geht, is aber doch semantisch falsch! */
1164 in[n_preds] = get_Phi_pred(phi, i);
1169 set_irn_in(phi, n_preds, in);
1170 phi = get_irn_link(phi);
1173 /** Move Phi nodes from removed blocks to this one.
1174 This happens only if merge between loop backedge and single loop entry. **/
1175 for (k = 0; k < get_Block_n_cfgpreds(b); k++) {
1176 pred = get_nodes_Block(get_Block_cfgpred(b, k));
1177 if (get_Block_block_visited(pred) +1
1178 < get_irg_block_visited(current_ir_graph)) {
1179 phi = get_irn_link(pred);
1181 if (get_irn_op(phi) == op_Phi) {
1182 set_nodes_Block(phi, b);
1185 for (i = 0; i < k; i++) {
1186 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1187 if (is_Bad(get_Block_cfgpred(b, i))) {
1189 } else if (get_Block_block_visited(pred) +1
1190 < get_irg_block_visited(current_ir_graph)) {
1191 /* It's an empty block and not yet visited. */
1192 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1193 /* @@@ Hier brauche ich schleifeninformation!!! Kontrllflusskante
1194 muss Rueckwaertskante sein! (An allen vier in[n_preds] = phi
1195 Anweisungen.) Trotzdem tuts bisher!! */
1204 for (i = 0; i < get_Phi_n_preds(phi); i++) {
1205 in[n_preds] = get_Phi_pred(phi, i);
1208 for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
1209 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1210 if (is_Bad(get_Block_cfgpred(b, i))) {
1212 } else if (get_Block_block_visited(pred) +1
1213 < get_irg_block_visited(current_ir_graph)) {
1214 /* It's an empty block and not yet visited. */
1215 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1224 set_irn_in(phi, n_preds, in);
1226 phi = get_irn_link(phi);
1231 /** Fix the block **/
1233 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
1234 pred = get_nodes_Block(get_Block_cfgpred(b, i));
1235 if (is_Bad(get_Block_cfgpred(b, i))) {
1237 } else if (get_Block_block_visited(pred) +1
1238 < get_irg_block_visited(current_ir_graph)) {
1239 /* It's an empty block and not yet visited. */
1240 assert(get_Block_n_cfgpreds(b) > 1);
1241 /* Else it should be optimized by equivalent_node. */
1242 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
1243 in[n_preds] = get_Block_cfgpred(pred, j);
1246 /* Remove block as it might be kept alive. */
1247 exchange(pred, b/*new_Bad()*/);
1249 in[n_preds] = get_Block_cfgpred(b, i);
1253 set_irn_in(b, n_preds, in);
1257 void optimize_cf(ir_graph *irg) {
1260 ir_node *end = get_irg_end(irg);
1261 ir_graph *rem = current_ir_graph;
1262 current_ir_graph = irg;
1265 /* Handle graph state */
1266 assert(get_irg_phase_state(irg) != phase_building);
1267 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1268 set_irg_outs_inconsistent(current_ir_graph);
1269 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1270 set_irg_dom_inconsistent(current_ir_graph);
1272 /* Use block visited flag to mark non-empty blocks. */
1273 inc_irg_block_visited(irg);
1274 irg_walk(end, merge_blocks, collect_nodes, NULL);
1276 /* Optimize the standard code. */
1277 irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL);
1279 /* Walk all keep alives, optimize them if block, add to new in-array
1280 for end if useful. */
1281 in = NEW_ARR_F (ir_node *, 1);
1282 in[0] = get_nodes_Block(end);
1284 inc_irg_visited(current_ir_graph);
1285 for(i = 0; i < get_End_n_keepalives(end); i++) {
1286 ir_node *ka = get_End_keepalive(end, i);
1287 if (irn_not_visited(ka)) {
1288 if ((get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) {
1289 set_irg_block_visited(current_ir_graph, /* Don't walk all the way to Start. */
1290 get_irg_block_visited(current_ir_graph)-1);
1291 irg_block_walk(ka, optimize_blocks, NULL, NULL);
1292 mark_irn_visited(ka);
1293 ARR_APP1 (ir_node *, in, ka);
1294 } else if (get_irn_op(ka) == op_Phi) {
1295 mark_irn_visited(ka);
1296 ARR_APP1 (ir_node *, in, ka);
1300 /* DEL_ARR_F(end->in); GL @@@ tut nicht ! */
1303 current_ir_graph = rem;