2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
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,
37 * Returns non-zero, is a block is not reachable from Start.
39 * @param block the block to test
42 is_Block_unreachable(ir_node *block) {
43 return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
47 * Find the earliest correct block for node n. --- Place n into the
48 * same Block as its dominance-deepest Input.
50 * We have to avoid calls to get_nodes_block() here
51 * because the graph is floating.
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
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.
62 * @param n the node to be placed
63 * @param worklist a worklist, predecessors of non-floating nodes are placed here
66 place_floats_early(ir_node *n, waitq *worklist) {
69 /* we must not run into an infinite loop */
70 assert(!irn_visited(n));
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);
78 ir_node *b = NULL; /* The block to place this node in */
80 assert(is_no_Block(n));
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);
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);
94 if (!irn_visited(pred)
95 && (get_irn_pinned(pred) == op_pin_state_floats)) {
98 * If the current node is NOT in a dead block, but one of its
99 * predecessors is, we must move the predecessor to a live block.
100 * Such thing can happen, if global CSE chose a node from a dead block.
101 * We move it simply to our block.
102 * Note that neither Phi nor End nodes are floating, so we don't
103 * need to handle them here.
105 if (! in_dead_block) {
106 if (get_irn_pinned(pred) == op_pin_state_floats &&
107 is_Block_unreachable(get_nodes_block(pred)))
108 set_nodes_block(pred, curr_block);
110 place_floats_early(pred, worklist);
114 * A node in the Bad block must stay in the bad block,
115 * so don't compute a new block for it.
120 /* Because all loops contain at least one op_pin_state_pinned node, now all
121 our inputs are either op_pin_state_pinned or place_early() has already
122 been finished on them. We do not have any unfinished inputs! */
123 pred_block = get_nodes_block(pred);
124 if ((!is_Block_dead(pred_block)) &&
125 (get_Block_dom_depth(pred_block) > depth)) {
127 depth = get_Block_dom_depth(pred_block);
129 /* Avoid that the node is placed in the Start block if we are not in the
132 get_Block_dom_depth(get_nodes_block(n)) > 1 &&
133 get_irg_phase_state(current_ir_graph) != phase_backend) {
134 b = get_Block_cfg_out(get_irg_start_block(current_ir_graph), 0);
135 assert(b != get_irg_start_block(current_ir_graph));
140 set_nodes_block(n, b);
144 * Add predecessors of non floating nodes and non-floating predecessors
145 * of floating nodes to worklist and fix their blocks if the are in dead block.
147 irn_arity = get_irn_arity(n);
151 * Simplest case: End node. Predecessors are keep-alives,
152 * no need to move out of dead block.
154 for (i = -1; i < irn_arity; ++i) {
155 ir_node *pred = get_irn_n(n, i);
156 if (!irn_visited(pred))
157 waitq_put(worklist, pred);
159 } else if (is_Block(n)) {
161 * Blocks: Predecessors are control flow, no need to move
162 * them out of dead block.
164 for (i = irn_arity - 1; i >= 0; --i) {
165 ir_node *pred = get_irn_n(n, i);
166 if (!irn_visited(pred))
167 waitq_put(worklist, pred);
169 } else if (is_Phi(n)) {
171 ir_node *curr_block = get_nodes_block(n);
172 int in_dead_block = is_Block_unreachable(curr_block);
175 * Phi nodes: move nodes from dead blocks into the effective use
176 * of the Phi-input if the Phi is not in a bad block.
178 pred = get_nodes_block(n);
179 if (!irn_visited(pred))
180 waitq_put(worklist, pred);
182 for (i = irn_arity - 1; i >= 0; --i) {
183 ir_node *pred = get_irn_n(n, i);
185 if (!irn_visited(pred)) {
186 if (! in_dead_block &&
187 get_irn_pinned(pred) == op_pin_state_floats &&
188 is_Block_unreachable(get_nodes_block(pred))) {
189 set_nodes_block(pred, get_Block_cfgpred_block(curr_block, i));
191 waitq_put(worklist, pred);
196 ir_node *curr_block = get_nodes_block(n);
197 int in_dead_block = is_Block_unreachable(curr_block);
200 * All other nodes: move nodes from dead blocks into the same block.
202 pred = get_nodes_block(n);
203 if (!irn_visited(pred))
204 waitq_put(worklist, pred);
206 for (i = irn_arity - 1; i >= 0; --i) {
207 ir_node *pred = get_irn_n(n, i);
209 if (!irn_visited(pred)) {
210 if (! in_dead_block &&
211 get_irn_pinned(pred) == op_pin_state_floats &&
212 is_Block_unreachable(get_nodes_block(pred))) {
213 set_nodes_block(pred, curr_block);
215 waitq_put(worklist, pred);
222 * Floating nodes form subgraphs that begin at nodes as Const, Load,
223 * Start, Call and that end at op_pin_state_pinned nodes as Store, Call. Place_early
224 * places all floating nodes reachable from its argument through floating
225 * nodes and adds all beginnings at op_pin_state_pinned nodes to the worklist.
227 * @param worklist a worklist, used for the algorithm, empty on in/output
229 static void place_early(waitq *worklist) {
231 inc_irg_visited(current_ir_graph);
233 /* this inits the worklist */
234 place_floats_early(get_irg_end(current_ir_graph), worklist);
236 /* Work the content of the worklist. */
237 while (!waitq_empty(worklist)) {
238 ir_node *n = waitq_get(worklist);
240 place_floats_early(n, worklist);
242 set_irg_pinned(current_ir_graph, op_pin_state_pinned);
246 * Compute the deepest common dominator tree ancestor of block and dca.
248 * @param dca the deepest common dominator tree ancestor so far,
250 * @param block a block
252 * @return the deepest common dominator tree ancestor of block and dca
254 static ir_node *calc_dom_dca(ir_node *dca, ir_node *block) {
257 /* we do not want to place nodes in dead blocks */
258 if (is_Block_dead(block))
261 /* We found a first legal placement. */
262 if (!dca) return block;
264 /* Find a placement that is dominates both, dca and block. */
265 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
266 block = get_Block_idom(block);
268 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
269 dca = get_Block_idom(dca);
272 while (block != dca) {
273 block = get_Block_idom(block); dca = get_Block_idom(dca);
279 * Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
280 * I.e., DCA is the block where we might place PRODUCER.
281 * A data flow edge points from producer to consumer.
283 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
285 /* Compute the last block into which we can place a node so that it is
287 if (is_Phi(consumer)) {
288 /* our consumer is a Phi-node, the effective use is in all those
289 blocks through which the Phi-node reaches producer */
290 ir_node *phi_block = get_nodes_block(consumer);
291 int arity = get_irn_arity(consumer);
294 for (i = 0; i < arity; i++) {
295 if (get_Phi_pred(consumer, i) == producer) {
296 ir_node *new_block = get_Block_cfgpred_block(phi_block, i);
298 if (!is_Block_unreachable(new_block))
299 dca = calc_dom_dca(dca, new_block);
303 dca = calc_dom_dca(dca, get_nodes_block(consumer));
308 static inline int get_block_loop_depth(ir_node *block) {
309 return get_loop_depth(get_irn_loop(block));
313 * Move n to a block with less loop depth than it's current block. The
314 * new block must be dominated by early.
316 * @param n the node that should be moved
317 * @param early the earliest block we can n move to
319 static void move_out_of_loops(ir_node *n, ir_node *early) {
324 /* Find the region deepest in the dominator tree dominating
325 dca with the least loop nesting depth, but still dominated
326 by our early placement. */
327 dca = get_nodes_block(n);
330 while (dca != early) {
331 dca = get_Block_idom(dca);
332 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
333 if (get_block_loop_depth(dca) < get_block_loop_depth(best)) {
337 if (best != get_nodes_block(n))
338 set_nodes_block(n, best);
342 * Calculate the deepest common ancestor in the dominator tree of all nodes'
343 * blocks depending on node; our final placement has to dominate DCA.
345 * @param node the definition node
346 * @param dca the deepest common ancestor block so far, initially
349 * @return the deepest common dominator ancestor of all blocks of node's users
351 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca) {
354 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
355 ir_node *succ = get_irn_out(node, i);
359 * This consumer is the End node, a keep alive edge.
360 * This is not a real consumer, so we ignore it
366 /* Proj nodes are in the same block as node, so
367 * the users of Proj are our users. */
368 dca = get_deepest_common_dom_ancestor(succ, dca);
370 /* ignore if succ is in dead code */
371 ir_node *succ_blk = get_nodes_block(succ);
372 if (is_Block_unreachable(succ_blk))
374 dca = consumer_dom_dca(dca, succ, node);
381 * Put all the Proj nodes of a node into a given block.
383 * @param node the mode_T node
384 * @param block the block to put the Proj nodes to
386 static void set_projs_block(ir_node *node, ir_node *block) {
389 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
390 ir_node *succ = get_irn_out(node, i);
392 assert(is_Proj(succ));
394 if (get_irn_mode(succ) == mode_T) {
395 set_projs_block(succ, block);
397 set_nodes_block(succ, block);
402 * Find the latest legal block for N and place N into the
403 * `optimal' Block between the latest and earliest legal block.
404 * The `optimal' block is the dominance-deepest block of those
405 * with the least loop-nesting-depth. This places N out of as many
406 * loops as possible and then makes it as control dependent as
409 * @param n the node to be placed
410 * @param worklist a worklist, all successors of non-floating nodes are
413 static void place_floats_late(ir_node *n, pdeq *worklist) {
417 assert(!irn_visited(n)); /* no multiple placement */
421 /* no need to place block nodes, control nodes are already placed. */
424 (get_irn_mode(n) != mode_X)) {
425 /* Remember the early_blk placement of this block to move it
426 out of loop no further than the early_blk placement. */
427 early_blk = get_nodes_block(n);
430 * BEWARE: Here we also get code, that is live, but
431 * was in a dead block. If the node is life, but because
432 * of CSE in a dead block, we still might need it.
435 /* Assure that our users are all placed, except the Phi-nodes.
436 --- Each data flow cycle contains at least one Phi-node. We
437 have to break the `user has to be placed before the
438 producer' dependence cycle and the Phi-nodes are the
439 place to do so, because we need to base our placement on the
440 final region of our users, which is OK with Phi-nodes, as they
441 are op_pin_state_pinned, and they never have to be placed after a
442 producer of one of their inputs in the same block anyway. */
443 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
444 ir_node *succ = get_irn_out(n, i);
445 if (!irn_visited(succ) && !is_Phi(succ))
446 place_floats_late(succ, worklist);
449 if (! is_Block_dead(early_blk)) {
450 /* do only move things that where not dead */
451 ir_op *op = get_irn_op(n);
453 /* We have to determine the final block of this node... except for
454 constants and Projs */
455 if ((get_irn_pinned(n) == op_pin_state_floats) &&
457 (op != op_SymConst) &&
460 /* deepest common ancestor in the dominator tree of all nodes'
461 blocks depending on us; our final placement has to dominate
463 ir_node *dca = get_deepest_common_dom_ancestor(n, NULL);
465 set_nodes_block(n, dca);
466 move_out_of_loops(n, early_blk);
467 if (get_irn_mode(n) == mode_T) {
468 set_projs_block(n, get_nodes_block(n));
475 /* Add successors of all non-floating nodes on list. (Those of floating
476 nodes are placed already and therefore are marked.) */
477 n_outs = get_irn_n_outs(n);
478 for (i = 0; i < n_outs; i++) {
479 ir_node *succ = get_irn_out(n, i);
480 if (!irn_visited(succ)) {
481 pdeq_putr(worklist, succ);
487 * Place floating nodes on the given worklist as late as possible using
488 * the dominance tree.
490 * @param worklist the worklist containing the nodes to place
492 static void place_late(waitq *worklist) {
494 inc_irg_visited(current_ir_graph);
496 /* This fills the worklist initially. */
497 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
499 /* And now empty the worklist again... */
500 while (!waitq_empty(worklist)) {
501 ir_node *n = waitq_get(worklist);
503 place_floats_late(n, worklist);
507 /* Code Placement. */
508 void place_code(ir_graph *irg) {
510 ir_graph *rem = current_ir_graph;
512 current_ir_graph = irg;
513 remove_critical_cf_edges(irg);
515 /* Handle graph state */
516 assert(get_irg_phase_state(irg) != phase_building);
519 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
520 free_loop_information(irg);
521 construct_cf_backedges(irg);
524 /* Place all floating nodes as early as possible. This guarantees
525 a legal code placement. */
526 worklist = new_waitq();
527 place_early(worklist);
529 /* Note: place_early changes only blocks, no data edges. So, the
530 * data out edges are still valid, no need to recalculate them here. */
532 /* Now move the nodes down in the dominator tree. This reduces the
533 unnecessary executions of the node. */
534 place_late(worklist);
536 set_irg_outs_inconsistent(irg);
537 set_irg_loopinfo_inconsistent(irg);
539 current_ir_graph = rem;
543 * Wrapper for place_code() inside the place_code pass.
545 static void place_code_wrapper(ir_graph *irg) {
546 set_opt_global_cse(1);
547 optimize_graph_df(irg);
549 set_opt_global_cse(0);
552 ir_graph_pass_t *place_code_pass(const char *name, int verify, int dump) {
553 return def_graph_pass(name ? name : "place", verify, dump, place_code_wrapper);