06889a6e1cfb4738df48fefbc698e3764167d658
[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 /* FIXME: the name clashes here with the function from ana/field_temperature.c
308  * please rename. */
309 static INLINE int get_irn_loop_depth(ir_node *n) {
310         return get_loop_depth(get_irn_loop(n));
311 }
312
313 /**
314  * Move n to a block with less loop depth than it's current block. The
315  * new block must be dominated by early.
316  *
317  * @param n      the node that should be moved
318  * @param early  the earliest block we can n move to
319  */
320 static void move_out_of_loops(ir_node *n, ir_node *early) {
321         ir_node *best, *dca;
322         assert(n && early);
323
324
325         /* Find the region deepest in the dominator tree dominating
326            dca with the least loop nesting depth, but still dominated
327            by our early placement. */
328         dca = get_nodes_block(n);
329
330         best = dca;
331         while (dca != early) {
332                 dca = get_Block_idom(dca);
333                 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
334                 if (get_irn_loop_depth(dca) < get_irn_loop_depth(best)) {
335                         best = dca;
336                 }
337         }
338         if (best != get_nodes_block(n))
339                 set_nodes_block(n, best);
340 }
341
342 /**
343  * Calculate the deepest common ancestor in the dominator tree of all nodes'
344  * blocks depending on node; our final placement has to dominate DCA.
345  *
346  * @param node  the definition node
347  * @param dca   the deepest common ancestor block so far, initially
348  *              NULL
349  *
350  * @return the deepest common dominator ancestor of all blocks of node's users
351  */
352 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca) {
353         int i;
354
355         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
356                 ir_node *succ = get_irn_out(node, i);
357
358                 if (is_End(succ)) {
359                         /*
360                          * This consumer is the End node, a keep alive edge.
361                          * This is not a real consumer, so we ignore it
362                          */
363                         continue;
364                 }
365
366                 if (is_Proj(succ)) {
367                         /* Proj nodes are in the same block as node, so
368                          * the users of Proj are our users. */
369                         dca = get_deepest_common_dom_ancestor(succ, dca);
370                 } else {
371                         /* ignore if succ is in dead code */
372                         ir_node *succ_blk = get_nodes_block(succ);
373                         if (is_Block_unreachable(succ_blk))
374                                 continue;
375                         dca = consumer_dom_dca(dca, succ, node);
376                 }
377         }
378         return dca;
379 }
380
381 /**
382  * Put all the Proj nodes of a node into a given block.
383  *
384  * @param node   the mode_T node
385  * @param block  the block to put the Proj nodes to
386  */
387 static void set_projs_block(ir_node *node, ir_node *block) {
388         int i;
389
390         for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
391                 ir_node *succ = get_irn_out(node, i);
392
393                 assert(is_Proj(succ));
394
395                 if (get_irn_mode(succ) == mode_T) {
396                         set_projs_block(succ, block);
397                 }
398                 set_nodes_block(succ, block);
399         }
400 }
401
402 /**
403  * Find the latest legal block for N and place N into the
404  * `optimal' Block between the latest and earliest legal block.
405  * The `optimal' block is the dominance-deepest block of those
406  * with the least loop-nesting-depth.  This places N out of as many
407  * loops as possible and then makes it as control dependent as
408  * possible.
409  *
410  * @param n         the node to be placed
411  * @param worklist  a worklist, all successors of non-floating nodes are
412  *                  placed here
413  */
414 static void place_floats_late(ir_node *n, pdeq *worklist) {
415         int i, n_outs;
416         ir_node *early_blk;
417
418         assert(!irn_visited(n)); /* no multiple placement */
419
420         mark_irn_visited(n);
421
422         /* no need to place block nodes, control nodes are already placed. */
423         if (!is_Block(n) &&
424             (!is_cfop(n)) &&
425             (get_irn_mode(n) != mode_X)) {
426                 /* Remember the early_blk placement of this block to move it
427                    out of loop no further than the early_blk placement. */
428                 early_blk = get_nodes_block(n);
429
430                 /*
431                  * BEWARE: Here we also get code, that is live, but
432                  * was in a dead block.  If the node is life, but because
433                  * of CSE in a dead block, we still might need it.
434                  */
435
436                 /* Assure that our users are all placed, except the Phi-nodes.
437                 --- Each data flow cycle contains at least one Phi-node.  We
438                     have to break the `user has to be placed before the
439                     producer' dependence cycle and the Phi-nodes are the
440                     place to do so, because we need to base our placement on the
441                     final region of our users, which is OK with Phi-nodes, as they
442                     are op_pin_state_pinned, and they never have to be placed after a
443                     producer of one of their inputs in the same block anyway. */
444                 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
445                         ir_node *succ = get_irn_out(n, i);
446                         if (!irn_visited(succ) && !is_Phi(succ))
447                                 place_floats_late(succ, worklist);
448                 }
449
450                 if (! is_Block_dead(early_blk)) {
451                         /* do only move things that where not dead */
452                         ir_op *op = get_irn_op(n);
453
454                         /* We have to determine the final block of this node... except for
455                            constants and Projs */
456                         if ((get_irn_pinned(n) == op_pin_state_floats) &&
457                             (op != op_Const)    &&
458                             (op != op_SymConst) &&
459                             (op != op_Proj))
460                         {
461                                 /* deepest common ancestor in the dominator tree of all nodes'
462                                    blocks depending on us; our final placement has to dominate
463                                    DCA. */
464                                 ir_node *dca = get_deepest_common_dom_ancestor(n, NULL);
465                                 if (dca != NULL) {
466                                         set_nodes_block(n, dca);
467                                         move_out_of_loops(n, early_blk);
468                                         if (get_irn_mode(n) == mode_T) {
469                                                 set_projs_block(n, get_nodes_block(n));
470                                         }
471                                 }
472                         }
473                 }
474         }
475
476         /* Add successors of all non-floating nodes on list. (Those of floating
477            nodes are placed already and therefore are marked.)  */
478         n_outs = get_irn_n_outs(n);
479         for (i = 0; i < n_outs; i++) {
480                 ir_node *succ = get_irn_out(n, i);
481                 if (!irn_visited(succ)) {
482                         pdeq_putr(worklist, succ);
483                 }
484         }
485 }
486
487 /**
488  * Place floating nodes on the given worklist as late as possible using
489  * the dominance tree.
490  *
491  * @param worklist   the worklist containing the nodes to place
492  */
493 static void place_late(waitq *worklist) {
494         assert(worklist);
495         inc_irg_visited(current_ir_graph);
496
497         /* This fills the worklist initially. */
498         place_floats_late(get_irg_start_block(current_ir_graph), worklist);
499
500         /* And now empty the worklist again... */
501         while (!waitq_empty(worklist)) {
502                 ir_node *n = waitq_get(worklist);
503                 if (!irn_visited(n))
504                         place_floats_late(n, worklist);
505         }
506 }
507
508 /* Code Placement. */
509 void place_code(ir_graph *irg) {
510         waitq *worklist;
511         ir_graph *rem = current_ir_graph;
512
513         current_ir_graph = irg;
514         remove_critical_cf_edges(irg);
515
516         /* Handle graph state */
517         assert(get_irg_phase_state(irg) != phase_building);
518         assure_doms(irg);
519
520         if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
521                 free_loop_information(irg);
522                 construct_cf_backedges(irg);
523         }
524
525         /* Place all floating nodes as early as possible. This guarantees
526          a legal code placement. */
527         worklist = new_waitq();
528         place_early(worklist);
529
530         /* Note: place_early changes only blocks, no data edges. So, the
531          * data out edges are still valid, no need to recalculate them here. */
532
533         /* Now move the nodes down in the dominator tree. This reduces the
534            unnecessary executions of the node. */
535         place_late(worklist);
536
537         set_irg_outs_inconsistent(irg);
538         set_irg_loopinfo_inconsistent(irg);
539         del_waitq(worklist);
540         current_ir_graph = rem;
541 }