+static void find_cf_and_or_walker(ir_node *block, void *env)
+{
+ int i, i2;
+ int n_cfgpreds = get_Block_n_cfgpreds(block);
+ (void) env;
+
+ if(n_cfgpreds < 2)
+ return;
+
+ /* Find the following structure:
+ *
+ * upper_block
+ * / |
+ * / |
+ * lower_block |
+ * / \ |
+ * ... \ |
+ * block
+ */
+
+restart:
+ for(i = 0; i < n_cfgpreds; ++i) {
+ ir_node *lower_block;
+ ir_node *lower_cf;
+ ir_node *cond;
+ ir_node *cond_selector;
+ block_info_t *block_info;
+ ir_node *lower_pred;
+
+ lower_cf = get_Block_cfgpred(block, i);
+ lower_cf = skip_empty_block(lower_cf);
+ if(!is_Proj(lower_cf))
+ continue;
+
+ lower_block = get_nodes_block(lower_cf);
+ if(get_Block_n_cfgpreds(lower_block) != 1)
+ continue;
+
+ cond = get_Proj_pred(lower_cf);
+ if(!is_Cond(cond))
+ continue;
+
+ cond_selector = get_Cond_selector(cond);
+ if(get_irn_mode(cond_selector) != mode_b)
+ continue;
+
+ /* the block must not produce any side-effects */
+ block_info = get_block_blockinfo(lower_block);
+ if(block_info->has_pinned)
+ continue;
+
+ lower_pred = get_Block_cfgpred_block(lower_block, 0);
+
+ for(i2 = 0; i2 < n_cfgpreds; ++i2) {
+ ir_node *upper_block;
+ ir_node *upper_cf;
+ ir_node *upper_cond;
+ ir_node *upper_cond_selector;
+ ir_node *replacement;
+ ir_graph *irg;
+ cond_pair cpair;
+
+ upper_cf = get_Block_cfgpred(block, i2);
+ upper_cf = skip_empty_block(upper_cf);
+ if(is_Bad(upper_cf))
+ continue;
+ upper_block = get_nodes_block(upper_cf);
+ if(upper_block != lower_pred)
+ continue;
+
+ assert(is_Proj(upper_cf));
+ upper_cond = get_Proj_pred(upper_cf);
+ assert(is_Cond(upper_cond));
+ upper_cond_selector = get_Cond_selector(upper_cond);
+ if(get_irn_mode(upper_cond_selector) != mode_b)
+ continue;
+
+ /* we have found the structure */
+ /* TODO: check phis */
+ if(!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
+ continue;
+
+ /* normalize pncs: we need the true case to jump into the
+ * common block */
+ irg = current_ir_graph;
+ if(get_Proj_proj(lower_cf) == pn_Cond_false) {
+ if(cpair.proj_lo == cond_selector) {
+ ir_mode *mode = get_tarval_mode(cpair.tv_lo);
+ cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
+ cpair.proj_lo = new_r_Proj(irg, lower_block,
+ get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
+ } else {
+ assert(cpair.proj_hi == cond_selector);
+ ir_mode *mode = get_tarval_mode(cpair.tv_hi);
+ cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
+ cpair.proj_hi = new_r_Proj(irg, lower_block,
+ get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
+ }
+ }
+ if(get_Proj_proj(upper_cf) == pn_Cond_false) {
+ if(cpair.proj_lo == upper_cond_selector) {
+ ir_mode *mode = get_tarval_mode(cpair.tv_lo);
+ cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
+ cpair.proj_lo = new_r_Proj(irg, upper_block,
+ get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
+ } else {
+ assert(cpair.proj_hi == upper_cond_selector);
+ ir_mode *mode = get_tarval_mode(cpair.tv_hi);
+ cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
+ cpair.proj_hi = new_r_Proj(irg, upper_block,
+ get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
+ }
+ }
+
+ /* can we optimize the case? */
+ replacement = bool_or(&cpair);
+ if(replacement == NULL)
+ continue;
+
+ /* move all nodes from lower block to upper block */
+ exchange(lower_block, upper_block);
+
+ set_Block_cfgpred(block, i2, new_Bad());
+
+ /* the optimisations expected the true case to jump */
+ if(get_Proj_proj(lower_cf) == pn_Cond_false) {
+ ir_node *block = get_nodes_block(replacement);
+ replacement = new_rd_Not(NULL, current_ir_graph, block,
+ replacement, mode_b);
+ }
+ set_Cond_selector(cond, replacement);
+
+ ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
+ goto restart;
+ }
+ }
+}
+
+void opt_bool(ir_graph *const irg)