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 Move nodes to a block where they will be executed the least
24 * @author Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
27 * The idea here is to push nodes as deep into the dominance tree as their
28 * dependencies allow. After pushing them back up out of as many loops as
35 #include "iroptimize.h"
41 #include "opt_manage.h"
43 static bool is_block_reachable(ir_node *block)
45 return get_Block_dom_depth(block) >= 0;
49 * Find the earliest correct block for node n. --- Place n into the
50 * same Block as its dominance-deepest Input.
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 its 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 static void place_floats_early(ir_node *n, waitq *worklist)
71 /* we must not run into an infinite loop */
72 if (irn_visited_else_mark(n))
75 /* The algorithm relies on the fact that all predecessors of a block are
76 * moved up after a call to place_float_early of the predecessors
77 * (see the loop below).
78 * However we have to break cycles somewhere. Relying on the visited flag
79 * will result in nodes not being moved up despite their place_floats_early
81 * Instead we break cycles at pinned nodes which won't move anyway:
82 * This works because in firm each cycle contains a Phi or Block node
85 if (get_irn_pinned(n) != op_pin_state_floats) {
86 /* we can't move pinned nodes */
87 arity = get_irn_arity(n);
88 for (i = 0; i < arity; ++i) {
89 ir_node *pred = get_irn_n(n, i);
90 pdeq_putr(worklist, pred);
93 pdeq_putr(worklist, get_nodes_block(n));
97 block = get_nodes_block(n);
99 /* first move predecessors up */
100 arity = get_irn_arity(n);
101 place_floats_early(block, worklist);
102 for (i = 0; i < arity; ++i) {
103 ir_node *pred = get_irn_n(n, i);
104 place_floats_early(pred, worklist);
107 /* determine earliest point */
110 for (i = 0; i < arity; ++i) {
111 ir_node *pred = get_irn_n(n, i);
112 ir_node *pred_block = get_nodes_block(pred);
113 int pred_depth = get_Block_dom_depth(pred_block);
114 if (pred_depth > new_depth) {
115 new_depth = pred_depth;
116 new_block = pred_block;
120 /* avoid moving nodes into the start block if we are not in the backend */
121 irg = get_irn_irg(n);
122 start_block = get_irg_start_block(irg);
123 if (new_block == start_block && block != start_block &&
124 get_irg_phase_state(irg) != phase_backend) {
125 assert(get_Block_n_cfg_outs(start_block) == 1);
126 new_block = get_Block_cfg_out(start_block, 0);
129 /* Set the new block */
130 if (new_block != NULL)
131 set_nodes_block(n, new_block);
135 * Floating nodes form subgraphs that begin at nodes as Const, Load,
136 * Start, Call and that end at op_pin_state_pinned nodes as Store, Call.
137 * Place_early places all floating nodes reachable from its argument through
138 * floating nodes and adds all beginnings at op_pin_state_pinned nodes to the
141 * @param worklist a worklist, used for the algorithm, empty on in/output
143 static void place_early(ir_graph *irg, waitq *worklist)
146 inc_irg_visited(irg);
148 /* this inits the worklist */
149 place_floats_early(get_irg_end(irg), worklist);
151 /* Work the content of the worklist. */
152 while (!waitq_empty(worklist)) {
153 ir_node *n = (ir_node*)waitq_get(worklist);
155 place_floats_early(n, worklist);
157 set_irg_pinned(irg, op_pin_state_pinned);
161 * Compute the deepest common dominator tree ancestor of block and dca.
163 * @param dca the deepest common dominator tree ancestor so far,
165 * @param block a block
167 * @return the deepest common dominator tree ancestor of block and dca
169 static ir_node *calc_dom_dca(ir_node *dca, ir_node *block)
171 assert(block != NULL);
173 /* We found a first legal placement. */
177 /* Find a placement that is dominates both, dca and block. */
178 while (get_Block_dom_depth(block) > get_Block_dom_depth(dca))
179 block = get_Block_idom(block);
181 while (get_Block_dom_depth(dca) > get_Block_dom_depth(block)) {
182 dca = get_Block_idom(dca);
185 while (block != dca) {
186 block = get_Block_idom(block); dca = get_Block_idom(dca);
192 * Deepest common dominance ancestor of DCA and CONSUMER of PRODUCER.
193 * I.e., DCA is the block where we might place PRODUCER.
194 * A data flow edge points from producer to consumer.
196 static ir_node *consumer_dom_dca(ir_node *dca, ir_node *consumer,
199 /* Compute the last block into which we can place a node so that it is
201 if (is_Phi(consumer)) {
202 /* our consumer is a Phi-node, the effective use is in all those
203 blocks through which the Phi-node reaches producer */
204 ir_node *phi_block = get_nodes_block(consumer);
205 int arity = get_irn_arity(consumer);
208 for (i = 0; i < arity; i++) {
209 if (get_Phi_pred(consumer, i) == producer) {
210 ir_node *new_block = get_Block_cfgpred_block(phi_block, i);
211 if (is_Bad(new_block))
214 assert(is_block_reachable(new_block));
215 dca = calc_dom_dca(dca, new_block);
219 dca = calc_dom_dca(dca, get_nodes_block(consumer));
224 static inline int get_block_loop_depth(ir_node *block)
226 return get_loop_depth(get_irn_loop(block));
230 * Move n to a block with less loop depth than its current block. The
231 * new block must be dominated by early.
233 * @param n the node that should be moved
234 * @param early the earliest block we can n move to
236 static void move_out_of_loops(ir_node *n, ir_node *early)
238 ir_node *block = get_nodes_block(n);
239 ir_node *best = block;
240 int best_depth = get_block_loop_depth(best);
242 /* Find the region deepest in the dominator tree dominating
243 dca with the least loop nesting depth, but still dominated
244 by our early placement. */
245 while (block != early) {
246 ir_node *idom = get_Block_idom(block);
247 int idom_depth = get_block_loop_depth(idom);
248 if (idom_depth < best_depth) {
250 best_depth = idom_depth;
254 if (best != get_nodes_block(n))
255 set_nodes_block(n, best);
259 * Calculate the deepest common ancestor in the dominator tree of all nodes'
260 * blocks depending on node; our final placement has to dominate DCA.
262 * @param node the definition node
263 * @param dca the deepest common ancestor block so far, initially
266 * @return the deepest common dominator ancestor of all blocks of node's users
268 static ir_node *get_deepest_common_dom_ancestor(ir_node *node, ir_node *dca)
272 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
273 ir_node *succ = get_irn_out(node, i);
275 /* keepalive edges are special and don't respect the dominance */
280 /* Proj nodes are in the same block as node, so
281 * the users of Proj are our users. */
282 dca = get_deepest_common_dom_ancestor(succ, dca);
284 assert(is_block_reachable(get_nodes_block(succ)));
285 dca = consumer_dom_dca(dca, succ, node);
292 * Put all the Proj nodes of a node into a given block.
294 * @param node the mode_T node
295 * @param block the block to put the Proj nodes to
297 static void set_projs_block(ir_node *node, ir_node *block)
301 for (i = get_irn_n_outs(node) - 1; i >= 0; --i) {
302 ir_node *succ = get_irn_out(node, i);
304 assert(is_Proj(succ));
306 if (get_irn_mode(succ) == mode_T) {
307 set_projs_block(succ, block);
309 set_nodes_block(succ, block);
314 * Find the latest legal block for N and place N into the
315 * `optimal' Block between the latest and earliest legal block.
316 * The `optimal' block is the dominance-deepest block of those
317 * with the least loop-nesting-depth. This places N out of as many
318 * loops as possible and then makes it as control dependent as
321 static void place_floats_late(ir_node *n, pdeq *worklist)
328 if (irn_visited_else_mark(n))
331 n_outs = get_irn_n_outs(n);
332 /* break cycles at pinned nodes (see place place_floats_early) as to why */
333 if (get_irn_pinned(n) != op_pin_state_floats) {
334 for (i = 0; i < n_outs; ++i) {
335 ir_node *succ = get_irn_out(n, i);
336 pdeq_putr(worklist, succ);
341 /* place our users */
342 for (i = 0; i < n_outs; ++i) {
343 ir_node *succ = get_irn_out(n, i);
344 place_floats_late(succ, worklist);
347 /* no point in moving Projs around, they are moved with their predecessor */
350 /* some nodes should simply stay in the startblock */
351 if (is_irn_start_block_placed(n)) {
353 ir_graph *irg = get_irn_irg(n);
354 ir_node *start_block = get_irg_start_block(irg);
355 assert(get_nodes_block(n) == start_block);
360 block = get_nodes_block(n);
361 assert(is_block_reachable(block));
363 /* deepest common ancestor in the dominator tree of all nodes'
364 blocks depending on us; our final placement has to dominate
366 dca = get_deepest_common_dom_ancestor(n, NULL);
368 set_nodes_block(n, dca);
369 move_out_of_loops(n, block);
370 if (get_irn_mode(n) == mode_T) {
371 set_projs_block(n, get_nodes_block(n));
377 * Place floating nodes on the given worklist as late as possible using
378 * the dominance tree.
380 * @param worklist the worklist containing the nodes to place
382 static void place_late(ir_graph *irg, waitq *worklist)
385 inc_irg_visited(irg);
387 /* This fills the worklist initially. */
388 place_floats_late(get_irg_start_block(irg), worklist);
390 /* And now empty the worklist again... */
391 while (!waitq_empty(worklist)) {
392 ir_node *n = (ir_node*)waitq_get(worklist);
394 place_floats_late(n, worklist);
398 /* Code Placement. */
399 static ir_graph_state_t do_codeplacement(ir_graph *irg)
403 /* Handle graph state */
404 assert(get_irg_phase_state(irg) != phase_building);
406 /* Place all floating nodes as early as possible. This guarantees
407 a legal code placement. */
408 worklist = new_waitq();
409 place_early(irg, worklist);
411 /* While GCSE might place nodes in unreachable blocks,
412 * these are now placed in reachable blocks. */
414 /* Note: place_early changes only blocks, no data edges. So, the
415 * data out edges are still valid, no need to recalculate them here. */
417 /* Now move the nodes down in the dominator tree. This reduces the
418 unnecessary executions of the node. */
419 place_late(irg, worklist);
425 static optdesc_t opt_codeplacement = {
427 IR_GRAPH_STATE_NO_CRITICAL_EDGES |
428 IR_GRAPH_STATE_CONSISTENT_OUTS |
429 IR_GRAPH_STATE_CONSISTENT_DOMINANCE |
430 IR_GRAPH_STATE_CONSISTENT_LOOPINFO,
434 void place_code(ir_graph *irg)
436 perform_irg_optimization(irg, &opt_codeplacement);
440 * Wrapper for place_code() inside the place_code pass.
442 static void place_code_wrapper(ir_graph *irg)
444 set_opt_global_cse(1);
445 optimize_graph_df(irg);
447 set_opt_global_cse(0);
451 static ir_graph_state_t do_gcse(ir_graph *irg)
453 set_opt_global_cse(1);
454 optimize_graph_df(irg);
455 do_codeplacement(irg);
456 set_opt_global_cse(0);
460 static optdesc_t opt_gcse = {
462 IR_GRAPH_STATE_NO_CRITICAL_EDGES |
463 IR_GRAPH_STATE_CONSISTENT_OUTS |
464 IR_GRAPH_STATE_CONSISTENT_DOMINANCE |
465 IR_GRAPH_STATE_CONSISTENT_LOOPINFO,
470 ir_graph_pass_t *place_code_pass(const char *name)
472 return def_graph_pass(name ? name : "place", place_code_wrapper);