+static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
+ ir_node *value)
+{
+ ir_node *block = get_nodes_block(jump);
+
+ if(irn_visited(value)) {
+ return NULL;
+ }
+ mark_irn_visited(value);
+
+ if(is_Const(value)) {
+ tarval *tv = get_Const_tarval(value);
+
+ if(tv != env->tv)
+ return NULL;
+
+ DB((
+ dbg, LEVEL_1,
+ "> Found condition evaluation candidate %+F->%+F\n",
+ env->true_block, block
+ ));
+
+ // adjust true_block to point directly towards our jump
+ add_pred(env->true_block, jump);
+
+ split_critical_edge(env->true_block, 0);
+
+ // we need a bigger visited nr when going back
+ env->visited_nr++;
+
+ return block;
+ }
+ if(is_Phi(value)) {
+ int i, arity;
+
+ // the phi has to be in the same block as the jump
+ if(get_nodes_block(value) != block)
+ return NULL;
+
+ arity = get_irn_arity(value);
+ for(i = 0; i < arity; ++i) {
+ ir_node *copy_block;
+ ir_node *phi_pred = get_Phi_pred(value, i);
+ ir_node *cfgpred = get_Block_cfgpred(block, i);
+
+ copy_block = find_candidate(env, cfgpred, phi_pred);
+ if(copy_block == NULL)
+ continue;
+
+ /* copy duplicated nodes in copy_block and fix SSA */
+ copy_and_fix(env, block, copy_block, i);
+
+ if(copy_block == get_nodes_block(cfgpred)) {
+ env->cnst_pred = block;
+ env->cnst_pos = i;
+ }
+
+ // return now as we can't process more possibilities in 1 run
+ return copy_block;
+ }
+ }
+ if(is_Proj(value)) {
+ ir_node *left;
+ ir_node *right;
+ int pnc;
+ ir_node *cmp = get_Proj_pred(value);
+ if(!is_Cmp(cmp))
+ return NULL;
+
+ left = get_Cmp_left(cmp);
+ right = get_Cmp_right(cmp);
+ pnc = get_Proj_proj(value);
+
+ /* we assume that the constant is on the right side, swap left/right
+ * if needed */
+ if(is_Const(left)) {
+ ir_node *t = left;
+ left = right;
+ right = t;
+
+ pnc = get_inversed_pnc(pnc);
+ }
+
+ if(!is_Const(right))
+ return 0;
+
+ if(get_nodes_block(left) != block) {
+ return 0;
+ }
+
+ /* negate condition when we're looking for the false block */
+ if(env->tv == get_tarval_b_false()) {
+ pnc = get_negated_pnc(pnc, get_irn_mode(right));
+ }
+
+ // (recursively) look if a pred of a phi is a constant
+ env->pnc = pnc;
+ env->cnst = right;
+
+ return find_const(env, jump, left);
+ }
+
+ return NULL;
+}
+
+/**
+ * Block-walker: searches for the following construct
+ *
+ * Const or Phi with constants
+ * |
+ * Cmp
+ * |
+ * Cond
+ * /
+ * ProjX
+ * /
+ * Block
+ */
+static void cond_eval(ir_node* block, void* data)
+{
+ condeval_env_t env;
+ int *changed = data;
+ ir_node *selector;
+ ir_node *projx;
+ ir_node *cond;
+ ir_node *copy_block;
+ int selector_evaluated;
+ const ir_edge_t *edge, *next;
+ ir_node* bad;
+ size_t cnst_pos;
+
+ if(get_Block_n_cfgpreds(block) != 1)
+ return;
+
+ projx = get_Block_cfgpred(block, 0);
+ if (!is_Proj(projx))
+ return;
+ assert(get_irn_mode(projx) == mode_X);
+
+ cond = get_Proj_pred(projx);
+ if (!is_Cond(cond))
+ return;
+
+ selector = get_Cond_selector(cond);
+ // TODO handle switch Conds
+ if (get_irn_mode(selector) != mode_b)
+ return;
+
+ /* handle cases that can be immediately evalutated */
+ selector_evaluated = -1;
+ if(is_Proj(selector)) {
+ ir_node *cmp = get_Proj_pred(selector);
+ if(is_Cmp(cmp)) {
+ ir_node *left = get_Cmp_left(cmp);
+ ir_node *right = get_Cmp_right(cmp);
+ if(is_Const(left) && is_Const(right)) {
+ int pnc = get_Proj_proj(selector);
+ tarval *tv_left = get_Const_tarval(left);
+ tarval *tv_right = get_Const_tarval(right);
+
+ selector_evaluated = eval_cmp(pnc, tv_left, tv_right);
+ if(selector_evaluated < 0)
+ return;
+ }
+ }
+ } else if(is_Const(selector)) {
+ tarval *tv = get_Const_tarval(selector);
+ if(tv == get_tarval_b_true()) {
+ selector_evaluated = 1;
+ } else {
+ assert(tv == get_tarval_b_false());
+ selector_evaluated = 0;
+ }
+ }
+
+ env.cnst_pred = NULL;
+ if (get_Proj_proj(projx) == pn_Cond_false) {
+ env.tv = get_tarval_b_false();
+ if(selector_evaluated >= 0)
+ selector_evaluated = !selector_evaluated;
+ } else {
+ env.tv = get_tarval_b_true();
+ }
+
+ if(selector_evaluated == 0) {
+ bad = new_Bad();
+ exchange(projx, bad);
+ *changed = 1;
+ return;
+ } else if(selector_evaluated == 1) {
+ dbg_info *dbgi = get_irn_dbg_info(selector);
+ ir_node *jmp = new_rd_Jmp(dbgi, current_ir_graph, get_nodes_block(projx));
+ exchange(projx, jmp);
+ *changed = 1;
+ return;
+ }
+
+ // (recursively) look if a pred of a phi is a constant
+ env.true_block = block;
+ inc_irg_visited(current_ir_graph);
+ env.visited_nr = get_irg_visited(current_ir_graph);
+
+ copy_block = find_candidate(&env, projx, selector);
+ if (copy_block == NULL)
+ return;
+
+ /* we have to remove the edge towards the pred as the pred now
+ * jumps into the true_block. We also have to shorten phis
+ * in our block because of this */
+ bad = new_Bad();
+ cnst_pos = env.cnst_pos;
+
+ /* shorten phis */
+ foreach_out_edge_safe(env.cnst_pred, edge, next) {
+ ir_node *node = get_edge_src_irn(edge);
+
+ if(is_Phi(node))
+ set_Phi_pred(node, cnst_pos, bad);
+ }
+
+ set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
+
+ /* the graph is changed now */
+ *changed = 1;
+}