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