Add passes for inlining, private methods.
[libfirm] / ir / opt / code_placement.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Code Placement.  Pins all floating nodes to a block where they
23  *           will be executed only if needed.
24  * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
25  *           Michael Beck
26  * @version  $Id$
27  */
28 #include "config.h"
29
30 #include "adt/pdeq.h"
31 #include "irnode_t.h"
32 #include "irouts.h"
33 #include "irgopt.h"
34 #include "irpass.h"
35
36 /**
37  * Returns non-zero, is a block is not reachable from Start.
38  *
39  * @param block  the block to test
40  */
41 static int
42 is_Block_unreachable(ir_node *block) {
43         return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
44 }
45
46 /**
47  * Find the earliest correct block for node n.  --- Place n into the
48  * same Block as its dominance-deepest Input.
49  *
50  * We have to avoid calls to get_nodes_block() here
51  * because the graph is floating.
52  *
53  * move_out_of_loops() expects that place_floats_early() have placed
54  * all "living" nodes into a living block. That's why we must
55  * move nodes in dead block with "live" successors into a valid
56  * block.
57  * We move them just into the same block as it's successor (or
58  * in case of a Phi into the effective use block). For Phi successors,
59  * this may still be a dead block, but then there is no real use, as
60  * the control flow will be dead later.
61  *
62  * @param n         the node to be placed
63  * @param worklist  a worklist, predecessors of non-floating nodes are placed here
64  */
65 static void
66 place_floats_early(ir_node *n, waitq *worklist) {
67         int i, irn_arity;
68
69         /* we must not run into an infinite loop */
70         assert(!irn_visited(n));
71         mark_irn_visited(n);
72
73         /* Place floating nodes. */
74         if (get_irn_pinned(n) == op_pin_state_floats) {
75                 ir_node *curr_block = get_nodes_block(n);
76                 int in_dead_block   = is_Block_unreachable(curr_block);
77                 int depth           = 0;
78                 ir_node *b          = NULL;   /* The block to place this node in */
79
80                 assert(is_no_Block(n));
81
82                 if (is_irn_start_block_placed(n)) {
83                         /* These nodes will not be placed by the loop below. */
84                         b = get_irg_start_block(current_ir_graph);
85                         depth = 1;
86                 }
87
88                 /* find the block for this node. */
89                 irn_arity = get_irn_arity(n);
90                 for (i = 0; i < irn_arity; i++) {
91                         ir_node *pred = get_irn_n(n, i);
92                         ir_node *pred_block;
93
94                         if (!irn_visited(pred)
95                             && (get_irn_pinned(pred) == op_pin_state_floats)) {
96
97                                 /*
98                                  * If the current node is NOT in a dead block, but one of its
99                                  * predecessors is, we must move the predecessor to a live block.
100                                  * Such thing can happen, if global CSE chose a node from a dead block.
101                                  * We move it simply to our block.
102                                  * Note that neither Phi nor End nodes are floating, so we don't
103                                  * need to handle them here.
104                                  */
105                                 if (! in_dead_block) {
106                                         if (get_irn_pinned(pred) == op_pin_state_floats &&
107                                                 is_Block_unreachable(get_nodes_block(pred)))
108                                                 set_nodes_block(pred, curr_block);
109                                 }
110                                 place_floats_early(pred, worklist);
111                         }
112
113                         /*
114                          * A node in the Bad block must stay in the bad block,
115                          * so don't compute a new block for it.
116                          */
117                         if (in_dead_block)
118                                 continue;
119
120                         /* Because all loops contain at least one op_pin_state_pinned node, now all
121                            our inputs are either op_pin_state_pinned or place_early() has already
122                            been finished on them.  We do not have any unfinished inputs!  */
123                         pred_block = get_nodes_block(pred);
124                         if ((!is_Block_dead(pred_block)) &&
125                                 (get_Block_dom_depth(pred_block) > depth)) {
126                                 b = pred_block;
127                                 depth = get_Block_dom_depth(pred_block);
128                         }
129                         /* Avoid that the node is placed in the Start block if we are not in the
130                            backend phase. */
131                         if (depth == 1 &&
132                                         get_Block_dom_depth(get_nodes_block(n)) > 1 &&
133                                         get_irg_phase_state(current_ir_graph) != phase_backend) {
134                                 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
135                                 assert(b != get_irg_start_block(current_ir_graph));
136                                 depth = 2;
137                         }
138                 }
139                 if (b)
140                         set_nodes_block(n, b);
141         }
142
143         /*
144          * Add predecessors of non floating nodes and non-floating predecessors
145          * of floating nodes to worklist and fix their blocks if the are in dead block.
146          */
147         irn_arity = get_irn_arity(n);
148
149         if (is_End(n)) {
150                 /*
151                  * Simplest case: End node. Predecessors are keep-alives,
152                  * no need to move out of dead block.
153                  */
154                 for (i = -1; i < irn_arity; ++i) {
155                         ir_node *pred = get_irn_n(n, i);
156                         if (!irn_visited(pred))
157                                 waitq_put(worklist, pred);
158                 }
159         } else if (is_Block(n)) {
160                 /*
161                  * Blocks: Predecessors are control flow, no need to move
162                  * them out of dead block.
163                  */
164                 for (i = irn_arity - 1; i >= 0; --i) {
165                         ir_node *pred = get_irn_n(n, i);
166                         if (!irn_visited(pred))
167                                 waitq_put(worklist, pred);
168                 }
169         } else if (is_Phi(n)) {
170                 ir_node *pred;
171                 ir_node *curr_block = get_nodes_block(n);
172                 int in_dead_block   = is_Block_unreachable(curr_block);
173
174                 /*
175                  * Phi nodes: move nodes from dead blocks into the effective use
176                  * of the Phi-input if the Phi is not in a bad block.
177                  */
178                 pred = get_nodes_block(n);
179                 if (!irn_visited(pred))
180                         waitq_put(worklist, pred);
181
182                 for (i = irn_arity - 1; i >= 0; --i) {
183                         ir_node *pred = get_irn_n(n, i);
184
185                         if (!irn_visited(pred)) {
186                                 if (! in_dead_block &&
187                                         get_irn_pinned(pred) == op_pin_state_floats &&
188                                         is_Block_unreachable(get_nodes_block(pred))) {
189                                         set_nodes_block(pred, get_Block_cfgpred_block(curr_block, i));
190                                 }
191                                 waitq_put(worklist, pred);
192                         }
193                 }
194         } else {
195                 ir_node *pred;
196                 ir_node *curr_block = get_nodes_block(n);
197                 int in_dead_block   = is_Block_unreachable(curr_block);
198
199                 /*
200                  * All other nodes: move nodes from dead blocks into the same block.
201                  */
202                 pred = get_nodes_block(n);
203                 if (!irn_visited(pred))
204                         waitq_put(worklist, pred);
205
206                 for (i = irn_arity - 1; i >= 0; --i) {
207                         ir_node *pred = get_irn_n(n, i);
208
209                         if (!irn_visited(pred)) {
210                                 if (! in_dead_block &&
211                                         get_irn_pinned(pred) == op_pin_state_floats &&
212                                         is_Block_unreachable(get_nodes_block(pred))) {
213                                         set_nodes_block(pred, curr_block);
214                                 }
215                                 waitq_put(worklist, pred);
216                         }
217                 }
218         }
219 }
220
221 /**
222  * Floating nodes form subgraphs that begin at nodes as Const, Load,
223  * Start, Call and that end at op_pin_state_pinned nodes as Store, Call.  Place_early
224  * places all floating nodes reachable from its argument through floating
225  * nodes and adds all beginnings at op_pin_state_pinned nodes to the worklist.
226  *
227  * @param worklist   a worklist, used for the algorithm, empty on in/output
228  */
229 static void place_early(waitq *worklist) {
230         assert(worklist);
231         inc_irg_visited(current_ir_graph);
232
233         /* this inits the worklist */
234         place_floats_early(get_irg_end(current_ir_graph), worklist);
235
236         /* Work the content of the worklist. */
237         while (!waitq_empty(worklist)) {
238                 ir_node *n = waitq_get(worklist);
239                 if (!irn_visited(n))
240                         place_floats_early(n, worklist);
241         }
242         set_irg_pinned(current_ir_graph, op_pin_state_pinned);
243 }
244
245 /**
246  * Compute the deepest common dominator tree ancestor of block and dca.
247  *
248  * @param dca    the deepest common dominator tree ancestor so far,
249  *               might be NULL
250  * @param block  a block
251  *
252  * @return  the deepest common dominator tree ancestor of block and dca
253  */
254 static ir_node *calc_dom_dca(ir_node *dca, ir_node *block) {
255         assert(block);
256
257         /* we do not want to place nodes in dead blocks */
258         if (is_Block_dead(block))
259                 return dca;
260
261         /* We found a first legal placement. */
262         if (!dca) return block;
263
264         /* Find a placement that is dominates both, dca and block. */
265         while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
266                 block = get_Block_idom(block);
267
268         while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
269                 dca = get_Block_idom(dca);
270         }
271
272         while (block != dca) {
273                 block = get_Block_idom(block); dca = get_Block_idom(dca);
274         }
275         return dca;
276 }
277
278 /**
279  * Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
280  * I.e., DCA is the block where we might place PRODUCER.
281  * A data flow edge points from producer to consumer.
282  */
283 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
284 {
285         /* Compute the last block into which we can place a node so that it is
286            before consumer. */
287         if (is_Phi(consumer)) {
288                 /* our consumer is a Phi-node, the effective use is in all those
289                    blocks through which the Phi-node reaches producer */
290                 ir_node *phi_block = get_nodes_block(consumer);
291                 int      arity     = get_irn_arity(consumer);
292                 int      i;
293
294                 for (i = 0;  i < arity; i++) {
295                         if (get_Phi_pred(consumer, i) == producer) {
296                                 ir_node *new_block = get_Block_cfgpred_block(phi_block, i);
297
298                                 if (!is_Block_unreachable(new_block))
299                                         dca = calc_dom_dca(dca, new_block);
300                         }
301                 }
302         } else {
303                 dca = calc_dom_dca(dca, get_nodes_block(consumer));
304         }
305         return dca;
306 }
307
308 static inline int get_block_loop_depth(ir_node *block) {
309         return get_loop_depth(get_irn_loop(block));
310 }
311
312 /**
313  * Move n to a block with less loop depth than it's current block. The
314  * new block must be dominated by early.
315  *
316  * @param n      the node that should be moved
317  * @param early  the earliest block we can n move to
318  */
319 static void move_out_of_loops(ir_node *n, ir_node *early) {
320         ir_node *best, *dca;
321         assert(n && early);
322
323
324         /* Find the region deepest in the dominator tree dominating
325            dca with the least loop nesting depth, but still dominated
326            by our early placement. */
327         dca = get_nodes_block(n);
328
329         best = dca;
330         while (dca != early) {
331                 dca = get_Block_idom(dca);
332                 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
333                 if (get_block_loop_depth(dca) < get_block_loop_depth(best)) {
334                         best = dca;
335                 }
336         }
337         if (best != get_nodes_block(n))
338                 set_nodes_block(n, best);
339 }
340
341 /**
342  * Calculate the deepest common ancestor in the dominator tree of all nodes'
343  * blocks depending on node; our final placement has to dominate DCA.
344  *
345  * @param node  the definition node
346  * @param dca   the deepest common ancestor block so far, initially
347  *              NULL
348  *
349  * @return the deepest common dominator ancestor of all blocks of node's users
350  */
351 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca) {
352         int i;
353
354         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
355                 ir_node *succ = get_irn_out(node, i);
356
357                 if (is_End(succ)) {
358                         /*
359                          * This consumer is the End node, a keep alive edge.
360                          * This is not a real consumer, so we ignore it
361                          */
362                         continue;
363                 }
364
365                 if (is_Proj(succ)) {
366                         /* Proj nodes are in the same block as node, so
367                          * the users of Proj are our users. */
368                         dca = get_deepest_common_dom_ancestor(succ, dca);
369                 } else {
370                         /* ignore if succ is in dead code */
371                         ir_node *succ_blk = get_nodes_block(succ);
372                         if (is_Block_unreachable(succ_blk))
373                                 continue;
374                         dca = consumer_dom_dca(dca, succ, node);
375                 }
376         }
377         return dca;
378 }
379
380 /**
381  * Put all the Proj nodes of a node into a given block.
382  *
383  * @param node   the mode_T node
384  * @param block  the block to put the Proj nodes to
385  */
386 static void set_projs_block(ir_node *node, ir_node *block) {
387         int i;
388
389         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
390                 ir_node *succ = get_irn_out(node, i);
391
392                 assert(is_Proj(succ));
393
394                 if (get_irn_mode(succ) == mode_T) {
395                         set_projs_block(succ, block);
396                 }
397                 set_nodes_block(succ, block);
398         }
399 }
400
401 /**
402  * Find the latest legal block for N and place N into the
403  * `optimal' Block between the latest and earliest legal block.
404  * The `optimal' block is the dominance-deepest block of those
405  * with the least loop-nesting-depth.  This places N out of as many
406  * loops as possible and then makes it as control dependent as
407  * possible.
408  *
409  * @param n         the node to be placed
410  * @param worklist  a worklist, all successors of non-floating nodes are
411  *                  placed here
412  */
413 static void place_floats_late(ir_node *n, pdeq *worklist) {
414         int i, n_outs;
415         ir_node *early_blk;
416
417         assert(!irn_visited(n)); /* no multiple placement */
418
419         mark_irn_visited(n);
420
421         /* no need to place block nodes, control nodes are already placed. */
422         if (!is_Block(n) &&
423             (!is_cfop(n)) &&
424             (get_irn_mode(n) != mode_X)) {
425                 /* Remember the early_blk placement of this block to move it
426                    out of loop no further than the early_blk placement. */
427                 early_blk = get_nodes_block(n);
428
429                 /*
430                  * BEWARE: Here we also get code, that is live, but
431                  * was in a dead block.  If the node is life, but because
432                  * of CSE in a dead block, we still might need it.
433                  */
434
435                 /* Assure that our users are all placed, except the Phi-nodes.
436                 --- Each data flow cycle contains at least one Phi-node.  We
437                     have to break the `user has to be placed before the
438                     producer' dependence cycle and the Phi-nodes are the
439                     place to do so, because we need to base our placement on the
440                     final region of our users, which is OK with Phi-nodes, as they
441                     are op_pin_state_pinned, and they never have to be placed after a
442                     producer of one of their inputs in the same block anyway. */
443                 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
444                         ir_node *succ = get_irn_out(n, i);
445                         if (!irn_visited(succ) && !is_Phi(succ))
446                                 place_floats_late(succ, worklist);
447                 }
448
449                 if (! is_Block_dead(early_blk)) {
450                         /* do only move things that where not dead */
451                         ir_op *op = get_irn_op(n);
452
453                         /* We have to determine the final block of this node... except for
454                            constants and Projs */
455                         if ((get_irn_pinned(n) == op_pin_state_floats) &&
456                             (op != op_Const)    &&
457                             (op != op_SymConst) &&
458                             (op != op_Proj))
459                         {
460                                 /* deepest common ancestor in the dominator tree of all nodes'
461                                    blocks depending on us; our final placement has to dominate
462                                    DCA. */
463                                 ir_node *dca = get_deepest_common_dom_ancestor(n, NULL);
464                                 if (dca != NULL) {
465                                         set_nodes_block(n, dca);
466                                         move_out_of_loops(n, early_blk);
467                                         if (get_irn_mode(n) == mode_T) {
468                                                 set_projs_block(n, get_nodes_block(n));
469                                         }
470                                 }
471                         }
472                 }
473         }
474
475         /* Add successors of all non-floating nodes on list. (Those of floating
476            nodes are placed already and therefore are marked.)  */
477         n_outs = get_irn_n_outs(n);
478         for (i = 0; i < n_outs; i++) {
479                 ir_node *succ = get_irn_out(n, i);
480                 if (!irn_visited(succ)) {
481                         pdeq_putr(worklist, succ);
482                 }
483         }
484 }
485
486 /**
487  * Place floating nodes on the given worklist as late as possible using
488  * the dominance tree.
489  *
490  * @param worklist   the worklist containing the nodes to place
491  */
492 static void place_late(waitq *worklist) {
493         assert(worklist);
494         inc_irg_visited(current_ir_graph);
495
496         /* This fills the worklist initially. */
497         place_floats_late(get_irg_start_block(current_ir_graph), worklist);
498
499         /* And now empty the worklist again... */
500         while (!waitq_empty(worklist)) {
501                 ir_node *n = waitq_get(worklist);
502                 if (!irn_visited(n))
503                         place_floats_late(n, worklist);
504         }
505 }
506
507 /* Code Placement. */
508 void place_code(ir_graph *irg) {
509         waitq *worklist;
510         ir_graph *rem = current_ir_graph;
511
512         current_ir_graph = irg;
513         remove_critical_cf_edges(irg);
514
515         /* Handle graph state */
516         assert(get_irg_phase_state(irg) != phase_building);
517         assure_doms(irg);
518
519         if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
520                 free_loop_information(irg);
521                 construct_cf_backedges(irg);
522         }
523
524         /* Place all floating nodes as early as possible. This guarantees
525          a legal code placement. */
526         worklist = new_waitq();
527         place_early(worklist);
528
529         /* Note: place_early changes only blocks, no data edges. So, the
530          * data out edges are still valid, no need to recalculate them here. */
531
532         /* Now move the nodes down in the dominator tree. This reduces the
533            unnecessary executions of the node. */
534         place_late(worklist);
535
536         set_irg_outs_inconsistent(irg);
537         set_irg_loopinfo_inconsistent(irg);
538         del_waitq(worklist);
539         current_ir_graph = rem;
540 }
541
542 /**
543  * Wrapper for place_code() inside the place_code pass.
544  */
545 static void place_code_wrapper(ir_graph *irg) {
546         set_opt_global_cse(1);
547         optimize_graph_df(irg);
548         place_code(irg);
549         set_opt_global_cse(0);
550 }
551
552 ir_graph_pass_t *place_code_pass(const char *name) {
553         return def_graph_pass(name ? name : "place", place_code_wrapper);
554 }