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