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 static inline int get_block_loop_depth(ir_node *block) {
308 return get_loop_depth(get_irn_loop(block));
312 * Move n to a block with less loop depth than it's current block. The
313 * new block must be dominated by early.
315 * @param n the node that should be moved
316 * @param early the earliest block we can n move to
318 static void move_out_of_loops(ir_node *n, ir_node *early) {
323 /* Find the region deepest in the dominator tree dominating
324 dca with the least loop nesting depth, but still dominated
325 by our early placement. */
326 dca = get_nodes_block(n);
329 while (dca != early) {
330 dca = get_Block_idom(dca);
331 if (!dca || is_Bad(dca)) break; /* may be Bad if not reachable from Start */
332 if (get_block_loop_depth(dca) < get_block_loop_depth(best)) {
336 if (best != get_nodes_block(n))
337 set_nodes_block(n, best);
341 * Calculate the deepest common ancestor in the dominator tree of all nodes'
342 * blocks depending on node; our final placement has to dominate DCA.
344 * @param node the definition node
345 * @param dca the deepest common ancestor block so far, initially
348 * @return the deepest common dominator ancestor of all blocks of node's users
350 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca) {
353 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
354 ir_node *succ = get_irn_out(node, i);
358 * This consumer is the End node, a keep alive edge.
359 * This is not a real consumer, so we ignore it
365 /* Proj nodes are in the same block as node, so
366 * the users of Proj are our users. */
367 dca = get_deepest_common_dom_ancestor(succ, dca);
369 /* ignore if succ is in dead code */
370 ir_node *succ_blk = get_nodes_block(succ);
371 if (is_Block_unreachable(succ_blk))
373 dca = consumer_dom_dca(dca, succ, node);
380 * Put all the Proj nodes of a node into a given block.
382 * @param node the mode_T node
383 * @param block the block to put the Proj nodes to
385 static void set_projs_block(ir_node *node, ir_node *block) {
388 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
389 ir_node *succ = get_irn_out(node, i);
391 assert(is_Proj(succ));
393 if (get_irn_mode(succ) == mode_T) {
394 set_projs_block(succ, block);
396 set_nodes_block(succ, block);
401 * Find the latest legal block for N and place N into the
402 * `optimal' Block between the latest and earliest legal block.
403 * The `optimal' block is the dominance-deepest block of those
404 * with the least loop-nesting-depth. This places N out of as many
405 * loops as possible and then makes it as control dependent as
408 * @param n the node to be placed
409 * @param worklist a worklist, all successors of non-floating nodes are
412 static void place_floats_late(ir_node *n, pdeq *worklist) {
416 assert(!irn_visited(n)); /* no multiple placement */
420 /* no need to place block nodes, control nodes are already placed. */
423 (get_irn_mode(n) != mode_X)) {
424 /* Remember the early_blk placement of this block to move it
425 out of loop no further than the early_blk placement. */
426 early_blk = get_nodes_block(n);
429 * BEWARE: Here we also get code, that is live, but
430 * was in a dead block. If the node is life, but because
431 * of CSE in a dead block, we still might need it.
434 /* Assure that our users are all placed, except the Phi-nodes.
435 --- Each data flow cycle contains at least one Phi-node. We
436 have to break the `user has to be placed before the
437 producer' dependence cycle and the Phi-nodes are the
438 place to do so, because we need to base our placement on the
439 final region of our users, which is OK with Phi-nodes, as they
440 are op_pin_state_pinned, and they never have to be placed after a
441 producer of one of their inputs in the same block anyway. */
442 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
443 ir_node *succ = get_irn_out(n, i);
444 if (!irn_visited(succ) && !is_Phi(succ))
445 place_floats_late(succ, worklist);
448 if (! is_Block_dead(early_blk)) {
449 /* do only move things that where not dead */
450 ir_op *op = get_irn_op(n);
452 /* We have to determine the final block of this node... except for
453 constants and Projs */
454 if ((get_irn_pinned(n) == op_pin_state_floats) &&
456 (op != op_SymConst) &&
459 /* deepest common ancestor in the dominator tree of all nodes'
460 blocks depending on us; our final placement has to dominate
462 ir_node *dca = get_deepest_common_dom_ancestor(n, NULL);
464 set_nodes_block(n, dca);
465 move_out_of_loops(n, early_blk);
466 if (get_irn_mode(n) == mode_T) {
467 set_projs_block(n, get_nodes_block(n));
474 /* Add successors of all non-floating nodes on list. (Those of floating
475 nodes are placed already and therefore are marked.) */
476 n_outs = get_irn_n_outs(n);
477 for (i = 0; i < n_outs; i++) {
478 ir_node *succ = get_irn_out(n, i);
479 if (!irn_visited(succ)) {
480 pdeq_putr(worklist, succ);
486 * Place floating nodes on the given worklist as late as possible using
487 * the dominance tree.
489 * @param worklist the worklist containing the nodes to place
491 static void place_late(waitq *worklist) {
493 inc_irg_visited(current_ir_graph);
495 /* This fills the worklist initially. */
496 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
498 /* And now empty the worklist again... */
499 while (!waitq_empty(worklist)) {
500 ir_node *n = waitq_get(worklist);
502 place_floats_late(n, worklist);
506 /* Code Placement. */
507 void place_code(ir_graph *irg) {
509 ir_graph *rem = current_ir_graph;
511 current_ir_graph = irg;
512 remove_critical_cf_edges(irg);
514 /* Handle graph state */
515 assert(get_irg_phase_state(irg) != phase_building);
518 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
519 free_loop_information(irg);
520 construct_cf_backedges(irg);
523 /* Place all floating nodes as early as possible. This guarantees
524 a legal code placement. */
525 worklist = new_waitq();
526 place_early(worklist);
528 /* Note: place_early changes only blocks, no data edges. So, the
529 * data out edges are still valid, no need to recalculate them here. */
531 /* Now move the nodes down in the dominator tree. This reduces the
532 unnecessary executions of the node. */
533 place_late(worklist);
535 set_irg_outs_inconsistent(irg);
536 set_irg_loopinfo_inconsistent(irg);
538 current_ir_graph = rem;