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,
36 * Returns non-zero, is a block is not reachable from Start.
38 * @param block the block to test
41 is_Block_unreachable(ir_node *block) {
42 return is_Block_dead(block) || get_Block_dom_depth(block) < 0;
46 * Find the earliest correct block for node n. --- Place n into the
47 * same Block as its dominance-deepest Input.
49 * We have to avoid calls to get_nodes_block() here
50 * because the graph is floating.
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
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.
61 * @param n the node to be placed
62 * @param worklist a worklist, predecessors of non-floating nodes are placed here
65 place_floats_early(ir_node *n, waitq *worklist) {
68 /* we must not run into an infinite loop */
69 assert(!irn_visited(n));
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);
77 ir_node *b = NULL; /* The block to place this node in */
79 assert(is_no_Block(n));
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);
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);
93 if (!irn_visited(pred)
94 && (get_irn_pinned(pred) == op_pin_state_floats)) {
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.
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);
109 place_floats_early(pred, worklist);
113 * A node in the Bad block must stay in the bad block,
114 * so don't compute a new block for it.
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)) {
126 depth = get_Block_dom_depth(pred_block);
128 /* Avoid that the node is placed in the Start block if we are not in the
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));
139 set_nodes_block(n, b);
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.
146 irn_arity = get_irn_arity(n);
150 * Simplest case: End node. Predecessors are keep-alives,
151 * no need to move out of dead block.
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);
158 } else if (is_Block(n)) {
160 * Blocks: Predecessors are control flow, no need to move
161 * them out of dead block.
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);
168 } else if (is_Phi(n)) {
170 ir_node *curr_block = get_nodes_block(n);
171 int in_dead_block = is_Block_unreachable(curr_block);
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.
177 pred = get_nodes_block(n);
178 if (!irn_visited(pred))
179 waitq_put(worklist, pred);
181 for (i = irn_arity - 1; i >= 0; --i) {
182 ir_node *pred = get_irn_n(n, i);
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));
190 waitq_put(worklist, pred);
195 ir_node *curr_block = get_nodes_block(n);
196 int in_dead_block = is_Block_unreachable(curr_block);
199 * All other nodes: move nodes from dead blocks into the same block.
201 pred = get_nodes_block(n);
202 if (!irn_visited(pred))
203 waitq_put(worklist, pred);
205 for (i = irn_arity - 1; i >= 0; --i) {
206 ir_node *pred = get_irn_n(n, i);
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);
214 waitq_put(worklist, pred);
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.
226 * @param worklist a worklist, used for the algorithm, empty on in/output
228 static void place_early(waitq *worklist) {
230 inc_irg_visited(current_ir_graph);
232 /* this inits the worklist */
233 place_floats_early(get_irg_end(current_ir_graph), worklist);
235 /* Work the content of the worklist. */
236 while (!waitq_empty(worklist)) {
237 ir_node *n = waitq_get(worklist);
239 place_floats_early(n, worklist);
241 set_irg_pinned(current_ir_graph, op_pin_state_pinned);
245 * Compute the deepest common dominator tree ancestor of block and dca.
247 * @param dca the deepest common dominator tree ancestor so far,
249 * @param block a block
251 * @return the deepest common dominator tree ancestor of block and dca
253 static ir_node *calc_dom_dca(ir_node *dca, ir_node *block) {
256 /* we do not want to place nodes in dead blocks */
257 if (is_Block_dead(block))
260 /* We found a first legal placement. */
261 if (!dca) return block;
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);
267 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
268 dca = get_Block_idom(dca);
271 while (block != dca) {
272 block = get_Block_idom(block); dca = get_Block_idom(dca);
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.
282 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
284 /* Compute the last block into which we can place a node so that it is
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);
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);
297 if (!is_Block_unreachable(new_block))
298 dca = calc_dom_dca(dca, new_block);
302 dca = calc_dom_dca(dca, get_nodes_block(consumer));
307 /* FIXME: the name clashes here with the function from ana/field_temperature.c
309 static inline int get_irn_loop_depth(ir_node *n) {
310 return get_loop_depth(get_irn_loop(n));
314 * Move n to a block with less loop depth than it's current block. The
315 * new block must be dominated by early.
317 * @param n the node that should be moved
318 * @param early the earliest block we can n move to
320 static void move_out_of_loops(ir_node *n, ir_node *early) {
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);
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)) {
338 if (best != get_nodes_block(n))
339 set_nodes_block(n, best);
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.
346 * @param node the definition node
347 * @param dca the deepest common ancestor block so far, initially
350 * @return the deepest common dominator ancestor of all blocks of node's users
352 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca) {
355 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
356 ir_node *succ = get_irn_out(node, i);
360 * This consumer is the End node, a keep alive edge.
361 * This is not a real consumer, so we ignore it
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);
371 /* ignore if succ is in dead code */
372 ir_node *succ_blk = get_nodes_block(succ);
373 if (is_Block_unreachable(succ_blk))
375 dca = consumer_dom_dca(dca, succ, node);
382 * Put all the Proj nodes of a node into a given block.
384 * @param node the mode_T node
385 * @param block the block to put the Proj nodes to
387 static void set_projs_block(ir_node *node, ir_node *block) {
390 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
391 ir_node *succ = get_irn_out(node, i);
393 assert(is_Proj(succ));
395 if (get_irn_mode(succ) == mode_T) {
396 set_projs_block(succ, block);
398 set_nodes_block(succ, block);
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
410 * @param n the node to be placed
411 * @param worklist a worklist, all successors of non-floating nodes are
414 static void place_floats_late(ir_node *n, pdeq *worklist) {
418 assert(!irn_visited(n)); /* no multiple placement */
422 /* no need to place block nodes, control nodes are already placed. */
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);
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.
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);
450 if (! is_Block_dead(early_blk)) {
451 /* do only move things that where not dead */
452 ir_op *op = get_irn_op(n);
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) &&
458 (op != op_SymConst) &&
461 /* deepest common ancestor in the dominator tree of all nodes'
462 blocks depending on us; our final placement has to dominate
464 ir_node *dca = get_deepest_common_dom_ancestor(n, 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));
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);
488 * Place floating nodes on the given worklist as late as possible using
489 * the dominance tree.
491 * @param worklist the worklist containing the nodes to place
493 static void place_late(waitq *worklist) {
495 inc_irg_visited(current_ir_graph);
497 /* This fills the worklist initially. */
498 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
500 /* And now empty the worklist again... */
501 while (!waitq_empty(worklist)) {
502 ir_node *n = waitq_get(worklist);
504 place_floats_late(n, worklist);
508 /* Code Placement. */
509 void place_code(ir_graph *irg) {
511 ir_graph *rem = current_ir_graph;
513 current_ir_graph = irg;
514 remove_critical_cf_edges(irg);
516 /* Handle graph state */
517 assert(get_irg_phase_state(irg) != phase_building);
520 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
521 free_loop_information(irg);
522 construct_cf_backedges(irg);
525 /* Place all floating nodes as early as possible. This guarantees
526 a legal code placement. */
527 worklist = new_waitq();
528 place_early(worklist);
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. */
533 /* Now move the nodes down in the dominator tree. This reduces the
534 unnecessary executions of the node. */
535 place_late(worklist);
537 set_irg_outs_inconsistent(irg);
538 set_irg_loopinfo_inconsistent(irg);
540 current_ir_graph = rem;