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
41 static int 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
65 static void 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
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.
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);
111 place_floats_early(pred, worklist);
115 * A node in the Bad block must stay in the bad block,
116 * so don't compute a new block for it.
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)) {
129 depth = get_Block_dom_depth(pred_block);
131 /* Avoid that the node is placed in the Start block if we are not
132 in the backend phase. */
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));
142 set_nodes_block(n, b);
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
150 irn_arity = get_irn_arity(n);
154 * Simplest case: End node. Predecessors are keep-alives,
155 * no need to move out of dead block.
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);
162 } else if (is_Block(n)) {
164 * Blocks: Predecessors are control flow, no need to move
165 * them out of dead block.
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);
172 } else if (is_Phi(n)) {
174 ir_node *curr_block = get_nodes_block(n);
175 int in_dead_block = is_Block_unreachable(curr_block);
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.
181 pred = get_nodes_block(n);
182 if (!irn_visited(pred))
183 waitq_put(worklist, pred);
185 for (i = irn_arity - 1; i >= 0; --i) {
186 ir_node *pred = get_irn_n(n, i);
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));
194 waitq_put(worklist, pred);
199 ir_node *curr_block = get_nodes_block(n);
200 int in_dead_block = is_Block_unreachable(curr_block);
203 * All other nodes: move nodes from dead blocks into the same block.
205 pred = get_nodes_block(n);
206 if (!irn_visited(pred))
207 waitq_put(worklist, pred);
209 for (i = irn_arity - 1; i >= 0; --i) {
210 ir_node *pred = get_irn_n(n, i);
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);
218 waitq_put(worklist, pred);
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
231 * @param worklist a worklist, used for the algorithm, empty on in/output
233 static void place_early(waitq *worklist)
236 inc_irg_visited(current_ir_graph);
238 /* this inits the worklist */
239 place_floats_early(get_irg_end(current_ir_graph), worklist);
241 /* Work the content of the worklist. */
242 while (!waitq_empty(worklist)) {
243 ir_node *n = waitq_get(worklist);
245 place_floats_early(n, worklist);
247 set_irg_pinned(current_ir_graph, op_pin_state_pinned);
251 * Compute the deepest common dominator tree ancestor of block and dca.
253 * @param dca the deepest common dominator tree ancestor so far,
255 * @param block a block
257 * @return the deepest common dominator tree ancestor of block and dca
259 static ir_node *calc_dom_dca(ir_node *dca, ir_node *block)
261 assert(block != NULL);
263 /* we do not want to place nodes in dead blocks */
264 if (is_Block_dead(block))
267 /* We found a first legal placement. */
268 if (!dca) return block;
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);
274 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
275 dca = get_Block_idom(dca);
278 while (block != dca) {
279 block = get_Block_idom(block); dca = get_Block_idom(dca);
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.
289 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer, ir_node *producer)
291 /* Compute the last block into which we can place a node so that it is
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);
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);
304 if (!is_Block_unreachable(new_block))
305 dca = calc_dom_dca(dca, new_block);
309 dca = calc_dom_dca(dca, get_nodes_block(consumer));
314 static inline int get_block_loop_depth(ir_node *block)
316 return get_loop_depth(get_irn_loop(block));
320 * Move n to a block with less loop depth than it's current block. The
321 * new block must be dominated by early.
323 * @param n the node that should be moved
324 * @param early the earliest block we can n move to
326 static void move_out_of_loops(ir_node *n, ir_node *early)
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);
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)) {
345 if (best != get_nodes_block(n))
346 set_nodes_block(n, best);
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.
353 * @param node the definition node
354 * @param dca the deepest common ancestor block so far, initially
357 * @return the deepest common dominator ancestor of all blocks of node's users
359 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca)
363 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
364 ir_node *succ = get_irn_out(node, i);
368 * This consumer is the End node, a keep alive edge.
369 * This is not a real consumer, so we ignore it
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);
379 /* ignore if succ is in dead code */
380 ir_node *succ_blk = get_nodes_block(succ);
381 if (is_Block_unreachable(succ_blk))
383 dca = consumer_dom_dca(dca, succ, node);
390 * Put all the Proj nodes of a node into a given block.
392 * @param node the mode_T node
393 * @param block the block to put the Proj nodes to
395 static void set_projs_block(ir_node *node, ir_node *block)
399 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
400 ir_node *succ = get_irn_out(node, i);
402 assert(is_Proj(succ));
404 if (get_irn_mode(succ) == mode_T) {
405 set_projs_block(succ, block);
407 set_nodes_block(succ, block);
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
419 * @param n the node to be placed
420 * @param worklist a worklist, all successors of non-floating nodes are
423 static void place_floats_late(ir_node *n, pdeq *worklist)
428 assert(!irn_visited(n)); /* no multiple placement */
432 /* no need to place block nodes, control nodes are already placed. */
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);
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.
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);
460 if (! is_Block_dead(early_blk)) {
461 /* do only move things that where not dead */
462 ir_op *op = get_irn_op(n);
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) &&
468 (op != op_SymConst) &&
471 /* deepest common ancestor in the dominator tree of all nodes'
472 blocks depending on us; our final placement has to dominate
474 ir_node *dca = get_deepest_common_dom_ancestor(n, 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));
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);
498 * Place floating nodes on the given worklist as late as possible using
499 * the dominance tree.
501 * @param worklist the worklist containing the nodes to place
503 static void place_late(waitq *worklist)
506 inc_irg_visited(current_ir_graph);
508 /* This fills the worklist initially. */
509 place_floats_late(get_irg_start_block(current_ir_graph), worklist);
511 /* And now empty the worklist again... */
512 while (!waitq_empty(worklist)) {
513 ir_node *n = waitq_get(worklist);
515 place_floats_late(n, worklist);
519 /* Code Placement. */
520 void place_code(ir_graph *irg)
523 ir_graph *rem = current_ir_graph;
525 current_ir_graph = irg;
526 remove_critical_cf_edges(irg);
528 /* Handle graph state */
529 assert(get_irg_phase_state(irg) != phase_building);
530 assure_irg_outs(irg);
533 if (1 || get_irg_loopinfo_state(irg) != loopinfo_consistent) {
534 free_loop_information(irg);
535 construct_cf_backedges(irg);
538 /* Place all floating nodes as early as possible. This guarantees
539 a legal code placement. */
540 worklist = new_waitq();
541 place_early(worklist);
543 /* Note: place_early changes only blocks, no data edges. So, the
544 * data out edges are still valid, no need to recalculate them here. */
546 /* Now move the nodes down in the dominator tree. This reduces the
547 unnecessary executions of the node. */
548 place_late(worklist);
550 set_irg_outs_inconsistent(irg);
551 set_irg_loopinfo_inconsistent(irg);
553 current_ir_graph = rem;
557 * Wrapper for place_code() inside the place_code pass.
559 static void place_code_wrapper(ir_graph *irg)
561 set_opt_global_cse(1);
562 optimize_graph_df(irg);
564 set_opt_global_cse(0);
567 ir_graph_pass_t *place_code_pass(const char *name)
569 return def_graph_pass(name ? name : "place", place_code_wrapper);