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