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 Control flow optimizations.
23 * @author Goetz Lindenmaier, Michael Beck, Sebastian Hack
25 * Removes Bad control flow predecessors and empty blocks. A block is empty
26 * if it contains only a Jmp node. Blocks can only be removed if they are not
27 * needed for the semantics of Phi nodes. Further, we NEVER remove labeled
28 * blocks (even if we could move the label).
32 #include "iroptimize.h"
39 #include "irgraph_t.h"
49 #include "opt_manage.h"
54 #include "irbackedge_t.h"
59 #include "irnodehashmap.h"
62 #include "iropt_dbg.h"
64 /** An environment for merge_blocks and collect nodes. */
65 typedef struct merge_env {
66 bool changed; /**< Set if the graph was changed. */
67 bool phis_moved; /**< Set if Phi nodes were moved. */
70 /** set or reset the removable property of a block. */
71 static void set_Block_removable(ir_node *block, bool removable)
73 set_Block_mark(block, removable);
76 /** check if a block has the removable property set. */
77 static bool is_Block_removable(const ir_node *block)
79 return get_Block_mark(block);
82 /** checks if a given Cond node is a switch Cond. */
83 static bool is_switch_Cond(const ir_node *cond)
85 ir_node *sel = get_Cond_selector(cond);
86 return get_irn_mode(sel) != mode_b;
89 /** Walker: clear link fields and mark all blocks as removable. */
90 static void clear_link_and_mark_blocks_removable(ir_node *node, void *ctx)
93 set_irn_link(node, NULL);
95 set_Block_removable(node, true);
96 set_Block_phis(node, NULL);
97 } else if (is_Phi(node)) {
98 set_Phi_next(node, NULL);
103 * Collects all Phi nodes in link list of Block.
104 * Marks all blocks "non_removable" if they contain a node other
105 * than Jmp (and Proj).
106 * Links all Proj nodes to their predecessors.
107 * Collects all switch-Conds in a list.
109 static void collect_nodes(ir_node *n, void *ctx)
113 /* Collect Phi nodes to compact ins along with block's ins. */
114 ir_node *block = get_nodes_block(n);
115 add_Block_phi(block, n);
116 } else if (is_Block(n)) {
117 if (get_Block_entity(n) != NULL) {
118 /* block with a jump label attached cannot be removed. */
119 set_Block_removable(n, false);
121 } else if (is_Bad(n) || is_Jmp(n)) {
125 /* Check for non-empty block. */
126 ir_node *block = get_nodes_block(n);
128 set_Block_removable(block, false);
131 /* link Proj nodes */
132 ir_node *pred = get_Proj_pred(n);
133 set_irn_link(n, get_irn_link(pred));
134 set_irn_link(pred, n);
139 /** Returns true if pred is predecessor of block b. */
140 static bool is_pred_of(const ir_node *pred, const ir_node *b)
144 for (i = get_Block_n_cfgpreds(b) - 1; i >= 0; --i) {
145 ir_node *b_pred = get_Block_cfgpred_block(b, i);
152 /** Test whether we can optimize away pred block pos of b.
154 * @param b A block node.
155 * @param pos The position of the predecessor block to judge about.
157 * @returns The number of predecessors
159 * The test is rather tricky.
161 * The situation is something like the following:
170 * b merges the control flow of an if-then-else. We may not remove
171 * the 'then' _and_ the 'else' block of an 'if' if there is a Phi
172 * node in b, even if both are empty. The destruction of this Phi
173 * requires that a copy is added before the merge. We have to
174 * keep one of the case blocks to place the copies in.
176 * To perform the test for pos, we must regard predecessors before pos
177 * as already removed.
179 static unsigned test_whether_dispensable(const ir_node *b, int pos)
181 ir_node *pred = get_Block_cfgpred(b, pos);
182 ir_node *predb = get_nodes_block(pred);
184 if (is_Bad(pred) || !is_Block_removable(predb))
187 /* can't remove self-loops */
189 goto non_dispensable;
190 if (is_unknown_jump(pred))
191 goto non_dispensable;
193 /* Seems to be empty. At least we detected this in collect_nodes. */
194 if (get_Block_phis(b) != NULL) {
195 int n_cfgpreds = get_Block_n_cfgpreds(b);
197 /* there are Phi nodes */
199 /* b's pred blocks and pred's pred blocks must be pairwise disjunct.
200 * Handle all pred blocks with preds < pos as if they were already
202 for (i = 0; i < pos; i++) {
203 ir_node *other_pred = get_Block_cfgpred(b, i);
204 ir_node *other_predb = get_nodes_block(other_pred);
205 if (is_Bad(other_pred))
207 if (is_Block_removable(other_predb)
208 && !Block_block_visited(other_predb)) {
210 for (j = get_Block_n_cfgpreds(other_predb) - 1; j >= 0; --j) {
211 ir_node *other_predpred
212 = get_Block_cfgpred_block(other_predb, j);
213 if (is_pred_of(other_predpred, predb))
214 goto non_dispensable;
216 } else if (is_pred_of(other_predb, predb)) {
217 goto non_dispensable;
220 for (i = pos+1; i < n_cfgpreds; i++) {
221 ir_node *other_predb = get_Block_cfgpred_block(b, i);
222 if (is_pred_of(other_predb, predb))
223 goto non_dispensable;
226 /* we will not dispense already visited blocks */
227 if (Block_block_visited(predb))
229 /* if we get here, the block is dispensable, count useful preds */
230 return get_irn_arity(predb);
233 set_Block_removable(predb, false);
238 * This method removes empty blocks. A block is empty if it only contains Phi
241 * We first adapt Phi nodes, then Block nodes, as we need the old ins
242 * of the Block to adapt the Phi nodes. We do this by computing new
243 * in arrays, and then replacing the old ones. So far we compute new in arrays
244 * for all nodes, not regarding whether there is a possibility for optimization.
246 * For each predecessor p of a Block b there are three cases:
247 * - The predecessor p is a Bad node: just skip it. The in array of b shrinks
249 * - The predecessor p is empty. Remove p. All predecessors of p are now
251 * - The predecessor p is a block containing useful code. Just keep p as is.
253 * For Phi nodes f we have to check the conditions at the Block of f.
254 * For cases 1 and 3 we proceed as for Blocks. For case 2 we can have two
256 * -2a: The old predecessor of the Phi f is a Phi pred_f IN THE BLOCK REMOVED.
257 * In this case we proceed as for blocks. We remove pred_f. All
258 * predecessors of pred_f now are predecessors of f.
259 * -2b: The old predecessor of f is NOT in the block removed. It might be a Phi
260 * too. We have to replicate f for each predecessor of the removed block.
261 * Or, with other words, the removed predecessor block has exactly one
264 * Further there is a special case for self referencing blocks:
267 * then_b else_b then_b else_b
273 * | | | === optimized to ===> \ | | |
280 * If there is a Phi in pred_b, but we remove pred_b, we have to generate a
281 * Phi in loop_b, that has the ins of the Phi in pred_b and a self referencing
284 static void optimize_blocks(ir_node *b, void *ctx)
286 int i, j, k, n, max_preds, n_preds, p_preds = -1;
290 merge_env *env = (merge_env*)ctx;
292 if (get_Block_dom_depth(b) < 0) {
293 /* ignore unreachable blocks */
297 /* Count the number of predecessor if this block is merged with pred blocks
300 for (i = 0, k = get_Block_n_cfgpreds(b); i < k; ++i) {
301 max_preds += test_whether_dispensable(b, i);
303 in = XMALLOCN(ir_node*, max_preds);
305 /*- Fix the Phi nodes of the current block -*/
306 for (phi = get_Block_phis(b); phi != NULL; phi = next) {
307 next = get_Phi_next(phi);
309 /* Find the new predecessors for the Phi */
311 for (i = 0, n = get_Block_n_cfgpreds(b); i < n; ++i) {
312 ir_graph *irg = get_irn_irg(b);
313 ir_node *predx = get_Block_cfgpred(b, i);
316 /* case Phi 1: maintain Bads, as somebody else is responsible to
319 in[p_preds++] = new_r_Bad(irg, get_irn_mode(phi));
323 pred = get_nodes_block(predx);
325 /* case Phi 2: It's an empty block and not yet visited. */
326 if (is_Block_removable(pred) && !Block_block_visited(pred)) {
327 ir_node *phi_pred = get_Phi_pred(phi, i);
329 for (j = 0, k = get_Block_n_cfgpreds(pred); j < k; j++) {
330 ir_node *pred_pred = get_Block_cfgpred(pred, j);
332 if (is_Bad(pred_pred)) {
333 in[p_preds++] = new_r_Bad(irg, get_irn_mode(phi));
337 if (get_nodes_block(phi_pred) == pred) {
339 assert(is_Phi(phi_pred)); /* Block is empty!! */
341 in[p_preds++] = get_Phi_pred(phi_pred, j);
344 in[p_preds++] = phi_pred;
349 in[p_preds++] = get_Phi_pred(phi, i);
352 assert(p_preds == max_preds);
356 exchange(phi, in[0]);
358 set_irn_in(phi, p_preds, in);
363 /*- This happens only if merge between loop backedge and single loop entry.
364 Moreover, it is only needed if predb is the direct dominator of b,
365 else there can be no uses of the Phi's in predb ... -*/
366 for (k = 0, n = get_Block_n_cfgpreds(b); k < n; ++k) {
367 ir_node *pred = get_Block_cfgpred(b, k);
368 ir_node *predb = get_nodes_block(pred);
372 if (is_Block_removable(predb) && !Block_block_visited(predb)) {
375 /* we found a predecessor block at position k that will be removed */
376 for (phi = get_Block_phis(predb); phi != NULL; phi = next_phi) {
378 next_phi = get_Phi_next(phi);
380 if (get_Block_idom(b) != predb) {
381 /* predb is not the dominator. There can't be uses of
382 * pred's Phi nodes, kill them .*/
383 ir_graph *irg = get_irn_irg(b);
384 ir_mode *mode = get_irn_mode(phi);
385 exchange(phi, new_r_Bad(irg, mode));
387 /* predb is the direct dominator of b. There might be uses
388 * of the Phi nodes from predb in further block, so move
389 * this phi from the predecessor into the block b */
390 set_nodes_block(phi, b);
391 set_Phi_next(phi, get_Block_phis(b));
392 set_Block_phis(b, phi);
393 env->phis_moved = true;
395 /* first, copy all 0..k-1 predecessors */
396 for (i = 0; i < k; i++) {
397 ir_node *predx = get_Block_cfgpred(b, i);
401 ir_graph *irg = get_irn_irg(b);
402 ir_mode *mode = get_irn_mode(phi);
403 in[q_preds++] = new_r_Bad(irg, mode);
406 pred_block = get_nodes_block(predx);
407 if (is_Block_removable(pred_block)
408 && !Block_block_visited(pred_block)) {
409 int n_cfgpreds = get_Block_n_cfgpreds(pred_block);
410 /* It's an empty block and not yet visited. */
411 for (j = 0; j < n_cfgpreds; j++) {
412 if (!is_Bad(get_Block_cfgpred(pred_block, j))) {
415 ir_graph *irg = get_irn_irg(b);
416 ir_mode *mode = get_irn_mode(phi);
417 in[q_preds++] = new_r_Bad(irg, mode);
425 /* now we are at k, copy the phi predecessors */
426 pred = get_nodes_block(get_Block_cfgpred(b, k));
427 for (i = 0; i < get_Phi_n_preds(phi); i++) {
428 in[q_preds++] = get_Phi_pred(phi, i);
431 /* and now all the rest */
432 for (i = k+1; i < get_Block_n_cfgpreds(b); i++) {
433 pred = get_Block_cfgpred_block(b, i);
436 ir_graph *irg = get_irn_irg(b);
437 ir_mode *mode = get_irn_mode(phi);
438 in[q_preds++] = new_r_Bad(irg, mode);
439 } else if (is_Block_removable(pred) && !Block_block_visited(pred)) {
440 /* It's an empty block and not yet visited. */
441 for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
442 if (! is_Bad(get_Block_cfgpred(pred, j))) {
445 ir_graph *irg = get_irn_irg(b);
446 ir_mode *mode = get_irn_mode(phi);
447 in[q_preds++] = new_r_Bad(irg, mode);
457 exchange(phi, in[0]);
459 set_irn_in(phi, q_preds, in);
462 assert(q_preds <= max_preds);
463 // assert(p_preds == q_preds && "Wrong Phi Fix");
469 /*- Fix the block -*/
471 for (i = 0; i < get_Block_n_cfgpreds(b); i++) {
472 ir_node *pred = get_Block_cfgpred(b, i);
473 ir_node *predb = get_nodes_block(pred);
474 ir_graph *irg = get_irn_irg(pred);
476 /* case 1: Bad predecessor */
478 in[n_preds++] = new_r_Bad(irg, mode_X);
481 if (is_Block_removable(predb) && !Block_block_visited(predb)) {
482 /* case 2: It's an empty block and not yet visited. */
483 for (j = 0; j < get_Block_n_cfgpreds(predb); j++) {
484 ir_node *predpred = get_Block_cfgpred(predb, j);
486 if (is_Bad(predpred)) {
487 in[n_preds++] = new_r_Bad(irg, mode_X);
491 in[n_preds++] = predpred;
493 /* Remove block+jump as it might be kept alive. */
494 exchange(pred, new_r_Bad(get_irn_irg(b), mode_X));
495 exchange(predb, new_r_Bad(get_irn_irg(b), mode_BB));
498 in[n_preds++] = pred;
501 assert(n_preds == max_preds);
503 set_irn_in(b, n_preds, in);
506 /* see if phi-fix was correct */
507 assert(get_Block_phis(b) == NULL || p_preds == -1 || (n_preds == p_preds));
512 * Optimize boolean Conds, where true and false jump to the same block into a Jmp
513 * Block must contain no Phi nodes.
517 * projA projB => Jmp Bad
521 static bool optimize_pred_cond(ir_node *block, int i, int j)
523 ir_node *projA, *projB, *cond, *pred_block, *jmp, *bad;
526 projA = get_Block_cfgpred(block, i);
527 if (!is_Proj(projA)) return false;
528 projB = get_Block_cfgpred(block, j);
529 if (!is_Proj(projB)) return false;
530 cond = get_Proj_pred(projA);
531 if (!is_Cond(cond)) return false;
533 if (cond != get_Proj_pred(projB)) return false;
534 if (is_switch_Cond(cond)) return false;
536 /* cond should actually be a Jmp */
537 pred_block = get_nodes_block(cond);
538 jmp = new_r_Jmp(pred_block);
539 bad = new_r_Bad(get_irn_irg(block), mode_X);
541 assert(projA != projB);
542 exchange(projA, jmp);
543 exchange(projB, bad);
547 typedef enum block_flags_t {
548 BF_HAS_OPERATIONS = 1 << 0,
549 BF_HAS_PHIS = 1 << 1,
550 BF_IS_UNKNOWN_JUMP_TARGET = 1 << 2,
553 static bool get_block_flag(const ir_nodehashmap_t *infos, const ir_node *block,
556 return PTR_TO_INT(ir_nodehashmap_get(infos, block)) & flag;
559 static void set_block_flag(ir_nodehashmap_t *infos, ir_node *block,
562 int data = PTR_TO_INT(ir_nodehashmap_get(infos, block));
564 ir_nodehashmap_insert(infos, block, INT_TO_PTR(data));
567 static void clear_block_flag(ir_nodehashmap_t *infos, const ir_node *block)
569 ir_nodehashmap_remove(infos, block);
572 static bool has_operations(ir_nodehashmap_t *infos, const ir_node *block)
574 return get_block_flag(infos, block, BF_HAS_OPERATIONS);
577 static void set_has_operations(ir_nodehashmap_t *infos, ir_node *block)
579 set_block_flag(infos, block, BF_HAS_OPERATIONS);
582 static bool has_phis(ir_nodehashmap_t *infos, const ir_node *block)
584 return get_block_flag(infos, block, BF_HAS_PHIS);
587 static void set_has_phis(ir_nodehashmap_t *infos, ir_node *block)
589 set_block_flag(infos, block, BF_HAS_PHIS);
592 static bool is_unknown_jump_target(ir_nodehashmap_t *infos, const ir_node *block)
594 return get_block_flag(infos, block, BF_IS_UNKNOWN_JUMP_TARGET);
597 static void set_is_unknown_jump_target(ir_nodehashmap_t *infos, ir_node *block)
599 set_block_flag(infos, block, BF_IS_UNKNOWN_JUMP_TARGET);
603 * Pre-Walker: fill block info information.
605 static void compute_block_info(ir_node *n, void *x)
607 ir_nodehashmap_t *infos = (ir_nodehashmap_t*)x;
610 int i, max = get_Block_n_cfgpreds(n);
611 for (i=0; i<max; i++) {
612 ir_node *pred = get_Block_cfgpred(n,i);
613 if (is_unknown_jump(pred)) {
614 set_is_unknown_jump_target(infos, n);
617 } else if (is_Phi(n)) {
618 ir_node *block = get_nodes_block(n);
619 set_has_phis(infos, block);
620 } else if (is_Jmp(n) || is_Cond(n) || is_Proj(n)) {
623 ir_node *block = get_nodes_block(n);
624 set_has_operations(infos, block);
628 static void clear_block_info(ir_node *block, void *x)
630 ir_nodehashmap_t *infos = (ir_nodehashmap_t*)x;
631 clear_block_flag(infos, block);
634 typedef struct skip_env {
636 ir_nodehashmap_t block_infos;
640 * Post-Block-walker: Optimize useless if's (boolean Cond nodes
641 * with same true/false target) away.
643 static void optimize_ifs(ir_node *block, void *x)
645 skip_env *env = (skip_env*)x;
647 int n_preds = get_Block_n_cfgpreds(block);
649 if (has_phis(&env->block_infos, block))
652 /* optimize Cond predecessors (might produce Bad predecessors) */
653 for (i = 0; i < n_preds; ++i) {
654 for (j = i+1; j < n_preds; ++j) {
655 optimize_pred_cond(block, i, j);
661 * Pre-Block walker: remove empty blocks (only contain a Jmp)
662 * that are control flow predecessors of the current block.
664 static void remove_empty_blocks(ir_node *block, void *x)
666 skip_env *env = (skip_env*)x;
668 int n_preds = get_Block_n_cfgpreds(block);
670 for (i = 0; i < n_preds; ++i) {
671 ir_node *jmp, *jmp_block;
674 jmp = get_Block_cfgpred(block, i);
677 jmp_block = get_nodes_block(jmp);
678 if (jmp_block == block)
679 continue; /* this infinite loop cannot be optimized any further */
680 if (is_unknown_jump_target(&env->block_infos, jmp_block))
681 continue; /* unknown jump target must not be optimized */
682 if (has_phis(&env->block_infos,jmp_block))
683 continue; /* this block contains Phis and is not skipped */
684 if (Block_block_visited(jmp_block)) {
686 /* otherwise we could break the walker,
687 * if block was reached via
688 * KeepAlive edge -> jmp_block -> A ---> block,
689 * because the walker cannot handle Id nodes.
699 /* jmp_block is an empty block and can be optimized! */
701 n_jpreds = get_Block_n_cfgpreds(jmp_block);
703 * If the jmp block has only one predecessor this is straightforward.
704 * However, if there are more predecessors, we only handle this,
705 * if block has no Phis.
708 ir_node *pred = get_Block_cfgpred(jmp_block, 0);
709 ir_node *pred_block = get_nodes_block(pred);
710 if (has_operations(&env->block_infos,jmp_block)) {
711 if (get_irg_start_block(get_irn_irg(pred_block)) == pred_block)
712 continue; /* must not merge operations into start block */
714 continue; /* must not create partially dead code, especially when it is mode_M */
717 /* skip jmp block by rerouting its predecessor to block
727 /* cleanup: jmp_block might have a Keep edge! */
728 exchange(jmp_block, pred_block);
730 } else if ( !has_phis(&env->block_infos, block) &&
731 !has_operations(&env->block_infos,jmp_block))
733 /* all predecessors can skip the jmp block, so block gets some new
738 * jmp_block C => Bad C | |
742 ir_node **ins = ALLOCAN(ir_node*, n_preds+n_jpreds);
744 /* first copy the old predecessors, because the outer loop (i)
745 * still walks over them */
746 for (j = 0; j < n_preds; ++j) {
747 ins[j] = get_Block_cfgpred(block, j);
749 /* now append the new predecessors */
750 for (j = 0; j < n_jpreds; ++j) {
751 ir_node *pred = get_Block_cfgpred(jmp_block, j);
752 ins[n_preds+j] = pred;
754 set_irn_in(block, n_preds+n_jpreds, ins);
755 /* convert the jmp_block to Bad */
756 ir_graph *irg = get_irn_irg(block);
757 exchange(jmp_block, new_r_Bad(irg, mode_BB));
758 exchange(jmp, new_r_Bad(irg, mode_X));
759 /* let the outer loop walk over the new predecessors as well */
762 // TODO What if jmp_block had a KeepAlive edge?
764 /* This would involve Phis ... */
770 * All cfg optimizations, which do not touch Phi nodes.
772 * Note that this might create critical edges.
774 static void cfgopt_ignoring_phis(ir_graph *irg)
779 ir_nodehashmap_init(&env.block_infos);
781 while (env.changed) {
782 irg_walk_graph(irg, compute_block_info, NULL, &env.block_infos);
785 /* Remove blocks, which only consist of a Jmp */
786 irg_block_walk_graph(irg, remove_empty_blocks, NULL, &env);
788 /* Optimize Cond->Jmp, where then- and else-block are the same. */
789 irg_block_walk_graph(irg, NULL, optimize_ifs, &env);
792 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
793 /* clear block info, because it must be recomputed */
794 irg_block_walk_graph(irg, clear_block_info, NULL, &env.block_infos);
795 /* Removing blocks and Conds might enable more optimizations */
802 ir_nodehashmap_destroy(&env.block_infos);
805 /* Optimizations of the control flow that also require changes of Phi nodes. */
806 static ir_graph_state_t do_cfopt(ir_graph *irg)
810 ir_node *end = get_irg_end(irg);
815 env.phis_moved = false;
817 assert(get_irg_phase_state(irg) != phase_building);
819 /* if the graph is not pinned, we cannot determine empty blocks */
820 assert(get_irg_pinned(irg) != op_pin_state_floats &&
821 "Control flow optimization need a pinned graph");
823 edges_deactivate(irg);
825 /* First the "simple" optimizations, which do not touch Phis */
826 cfgopt_ignoring_phis(irg);
828 /* we use the mark flag to mark removable blocks */
829 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_LINK
830 | IR_RESOURCE_PHI_LIST);
832 /* The switch Cond optimization might expose unreachable code, so we loop */
834 bool changed = false;
839 * This pass collects all Phi nodes in a link list in the block
840 * nodes. Further it performs simple control flow optimizations.
841 * Finally it marks all blocks that do not contain useful
842 * computations, i.e., these blocks might be removed.
844 irg_walk(end, clear_link_and_mark_blocks_removable, collect_nodes, NULL);
849 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
850 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS
851 | IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE);
854 /* assert due to collect_nodes:
855 * 1. removable blocks are now marked as such
856 * 2. phi lists are up to date
859 /* Optimize the standard code.
860 * It walks only over block nodes and adapts these and the Phi nodes in these
861 * blocks, which it finds in a linked list computed before.
864 irg_block_walk_graph(irg, optimize_blocks, NULL, &env);
866 new_end = optimize_in_place(end);
867 if (new_end != end) {
868 set_irg_end(irg, new_end);
871 remove_End_Bads_and_doublets(end);
873 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_LINK
874 | IR_RESOURCE_PHI_LIST);
876 if (env.phis_moved) {
877 /* Bad: when we moved Phi's, we might produce dead Phi nodes
879 Some other phases cannot copy with this, so kill them.
881 n = get_End_n_keepalives(end);
883 NEW_ARR_A(ir_node *, in, n);
884 assure_irg_outs(irg);
886 for (i = j = 0; i < n; ++i) {
887 ir_node *ka = get_End_keepalive(end, i);
892 for (k = get_irn_n_outs(ka) - 1; k >= 0; --k) {
893 ir_node *user = get_irn_out(ka, k);
895 if (user != ka && user != end) {
896 /* Is it a real user or just a self loop ? */
906 set_End_keepalives(end, j, in);
915 static optdesc_t opt_cf = {
917 IR_GRAPH_STATE_NO_UNREACHABLE_CODE,
921 void optimize_cf(ir_graph *irg)
923 perform_irg_optimization(irg, &opt_cf);
926 /* Creates an ir_graph pass for optimize_cf. */
927 ir_graph_pass_t *optimize_cf_pass(const char *name)
929 return def_graph_pass(name ? name : "optimize_cf", optimize_cf);