- if (proj2 == NULL) {
- /* this Cond has only one Proj: must be the defProj */
- assert(get_Cond_defaultProj(cond) == get_Proj_proj(proj1));
- /* convert it into a Jmp */
- jmp = new_r_Jmp(current_ir_graph, blk);
- exchange(proj1, jmp);
- return 1;
- } else if (get_irn_link(proj2) == NULL) {
- /* We have two Proj's here. Check if the Cond has
- a constant argument */
- tarval *tv = value_of(sel);
-
- if (tv != tarval_bad) {
- /* we have a constant switch */
- long num = get_tarval_long(tv);
- long def_num = get_Cond_defaultProj(cond);
-
- if (def_num == get_Proj_proj(proj1)) {
- /* first one is the defProj */
- if (num == get_Proj_proj(proj2)) {
- jmp = new_r_Jmp(current_ir_graph, blk);
- exchange(proj2, jmp);
- exchange(proj1, new_Bad());
- return 1;
- }
- } else if (def_num == get_Proj_proj(proj2)) {
- /* second one is the defProj */
- if (num == get_Proj_proj(proj1)) {
- jmp = new_r_Jmp(current_ir_graph, blk);
- exchange(proj1, jmp);
- exchange(proj2, new_Bad());
- return 1;
- }
- } else {
- /* neither: strange, Cond was not optimized so far */
- if (num == get_Proj_proj(proj1)) {
- jmp = new_r_Jmp(current_ir_graph, blk);
- exchange(proj1, jmp);
- exchange(proj2, new_Bad());
- return 1;
- } else if (num == get_Proj_proj(proj2)) {
- jmp = new_r_Jmp(current_ir_graph, blk);
- exchange(proj2, jmp);
- exchange(proj1, new_Bad());
- return 1;
- }
+/**
+ * Post-Block-walker: Optimize useless if's (boolean Cond nodes
+ * with same true/false target) away.
+ */
+static void optimize_ifs(ir_node *block, void *x)
+{
+ skip_env *env = (skip_env*)x;
+ int i, j;
+ int n_preds = get_Block_n_cfgpreds(block);
+
+ if (has_phis(&env->block_infos, block))
+ return;
+
+ /* optimize Cond predecessors (might produce Bad predecessors) */
+ for (i = 0; i < n_preds; ++i) {
+ for (j = i+1; j < n_preds; ++j) {
+ optimize_pred_cond(block, i, j);
+ }
+ }
+}
+
+/**
+ * Pre-Block walker: remove empty blocks (only contain a Jmp)
+ * that are control flow predecessors of the current block.
+ */
+static void remove_empty_blocks(ir_node *block, void *x)
+{
+ skip_env *env = (skip_env*)x;
+ int i;
+ int n_preds = get_Block_n_cfgpreds(block);
+
+ for (i = 0; i < n_preds; ++i) {
+ ir_node *jmp, *jmp_block;
+ int n_jpreds = 0;
+
+ jmp = get_Block_cfgpred(block, i);
+ if (!is_Jmp(jmp))
+ continue;
+ jmp_block = get_nodes_block(jmp);
+ if (jmp_block == block)
+ continue; /* this infinite loop cannot be optimized any further */
+ if (is_unknown_jump_target(&env->block_infos, jmp_block))
+ continue; /* unknown jump target must not be optimized */
+ if (has_phis(&env->block_infos,jmp_block))
+ continue; /* this block contains Phis and is not skipped */
+ if (Block_block_visited(jmp_block)) {
+ continue;
+ /* otherwise we could break the walker,
+ * if block was reached via
+ * KeepAlive edge -> jmp_block -> A ---> block,
+ * because the walker cannot handle Id nodes.
+ *
+ * A B
+ * \ /
+ * jmp_block
+ * / \
+ * block End
+ */
+ }
+
+ /* jmp_block is an empty block and can be optimized! */
+
+ n_jpreds = get_Block_n_cfgpreds(jmp_block);
+ /**
+ * If the jmp block has only one predecessor this is straightforward.
+ * However, if there are more predecessors, we only handle this,
+ * if block has no Phis.
+ */
+ if (n_jpreds == 1) {
+ ir_node *pred = get_Block_cfgpred(jmp_block, 0);
+ ir_node *pred_block = get_nodes_block(pred);
+ if (has_operations(&env->block_infos,jmp_block)) {
+ if (get_irg_start_block(get_irn_irg(pred_block)) == pred_block)
+ continue; /* must not merge operations into start block */
+ if (!is_Jmp(pred))
+ continue; /* must not create partially dead code, especially when it is mode_M */
+ }
+
+ /* skip jmp block by rerouting its predecessor to block
+ *
+ * A A
+ * | |
+ * jmp_block => |
+ * | |
+ * block block
+ */
+ exchange(jmp, pred);
+
+ /* cleanup: jmp_block might have a Keep edge! */
+ exchange(jmp_block, pred_block);
+ env->changed = true;
+ } else if ( !has_phis(&env->block_infos, block) &&
+ !has_operations(&env->block_infos,jmp_block))
+ {
+ /* all predecessors can skip the jmp block, so block gets some new
+ * predecessors
+ *
+ * A B A B
+ * \ / | |
+ * jmp_block C => Bad C | |
+ * \ / \ | | /
+ * block block
+ */
+ ir_node **ins = ALLOCAN(ir_node*, n_preds+n_jpreds);
+ int j;
+ /* first copy the old predecessors, because the outer loop (i)
+ * still walks over them */
+ for (j = 0; j < n_preds; ++j) {
+ ins[j] = get_Block_cfgpred(block, j);
+ }
+ /* now append the new predecessors */
+ for (j = 0; j < n_jpreds; ++j) {
+ ir_node *pred = get_Block_cfgpred(jmp_block, j);
+ ins[n_preds+j] = pred;