Indentation.
[libfirm] / ir / opt / condeval.c
index 093a522..4b0facf 100644 (file)
@@ -44,8 +44,9 @@
 #include "irtools.h"
 #include "irgraph.h"
 #include "tv.h"
+#include "opt_confirms.h"
 
-//#define AVOID_PHIB
+#undef AVOID_PHIB
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg);
 
@@ -76,11 +77,11 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
        ir_node *phi;
        ir_node **in;
 
-       // This is needed because we create bads sometimes
+       /* This is needed because we create bads sometimes */
        if(is_Bad(block))
                return new_Bad();
 
-       // already processed this block?
+       /* already processed this block? */
        if(irn_visited(block)) {
                ir_node *value = (ir_node*) get_irn_link(block);
                return value;
@@ -89,7 +90,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
        irg = get_irn_irg(block);
        assert(block != get_irg_start_block(irg));
 
-       // blocks with only 1 pred need no phi
+       /* a Blocks with only 1 predecessor need no Phi */
        n_cfgpreds = get_Block_n_cfgpreds(block);
        if(n_cfgpreds == 1) {
                ir_node *pred_block = get_Block_cfgpred_block(block, 0);
@@ -100,7 +101,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
                return value;
        }
 
-       // create a new phi
+       /* create a new Phi */
        NEW_ARR_A(ir_node*, in, n_cfgpreds);
        for(i = 0; i < n_cfgpreds; ++i)
                in[i] = new_Unknown(mode);
@@ -109,7 +110,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
        set_irn_link(block, phi);
        mark_irn_visited(block);
 
-       // set phi preds
+       /* set Phi predecessors */
        for(i = 0; i < n_cfgpreds; ++i) {
                ir_node *pred_block = get_Block_cfgpred_block(block, i);
                ir_node *pred_val = search_def_and_create_phis(pred_block, mode);
@@ -150,7 +151,7 @@ static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_
                mark_irn_visited(value_block);
        }
 
-       // Only fix the users of the first, i.e. the original node
+       /* Only fix the users of the first, i.e. the original node */
        value = vals[0];
 
        foreach_out_edge_safe(value, edge, next) {
@@ -159,8 +160,8 @@ static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_
                ir_node *user_block = get_nodes_block(user);
                ir_node *newval;
 
-               // ignore keeps
-               if(get_irn_op(user) == op_End)
+               /* ignore keeps */
+               if (is_End(user))
                        continue;
 
                if (user_block == blocks[1])
@@ -175,7 +176,7 @@ static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_
                        newval = search_def_and_create_phis(user_block, mode);
                }
 
-               // don't fix newly created phis from the SSA construction
+               /* don't fix newly created Phis from the SSA construction */
                if (newval != user) {
                        DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
                        set_irn_n(user, j, newval);
@@ -197,10 +198,11 @@ static void split_critical_edge(ir_node *block, int pos) {
 
 typedef struct condeval_env_t {
        ir_node       *true_block;
-       pn_Cmp         pnc;
+       ir_node       *cmp;        /**< The Compare node that might be partial evaluated */
+       pn_Cmp         pnc;        /**< The Compare mode of the Compare node. */
        ir_node       *cnst;
        tarval        *tv;
-       unsigned long  visited_nr;
+       ir_visited_t   visited_nr;
 
        ir_node       *cnst_pred;   /**< the block before the constant */
        int            cnst_pos;    /**< the pos to the constant block (needed to
@@ -215,7 +217,7 @@ static ir_node *copy_and_fix_node(const condeval_env_t *env, ir_node *block,
        /* we can evaluate Phis right now, all other nodes get copied */
        if (is_Phi(node)) {
                copy = get_Phi_pred(node, j);
-               /* we might have to evaluate a phi-cascades */
+               /* we might have to evaluate a Phi-cascade */
                if(get_irn_visited(copy) >= env->visited_nr) {
                        copy = get_irn_link(copy);
                }
@@ -269,7 +271,7 @@ static void copy_and_fix(const condeval_env_t *env, ir_node *block,
                if (mode == mode_X || is_Cond(node))
                        continue;
 #ifdef AVOID_PHIB
-               /* we may not copy mode_b nodes, because this could produce phi with
+               /* we may not copy mode_b nodes, because this could produce Phi with
                 * mode_bs which can't be handled in all backends. Instead we duplicate
                 * the node and move it to its users */
                if (mode == mode_b) {
@@ -303,7 +305,7 @@ static void copy_and_fix(const condeval_env_t *env, ir_node *block,
                copy = copy_and_fix_node(env, block, copy_block, j, node);
 
                /* we might hit values in blocks that have already been processed by a
-                * recursive find_phi_with_const call */
+                * recursive find_phi_with_const() call */
                assert(get_irn_visited(copy) <= env->visited_nr);
                if(get_irn_visited(copy) >= env->visited_nr) {
                        ir_node *prev_copy = get_irn_link(copy);
@@ -346,42 +348,66 @@ static void copy_and_fix(const condeval_env_t *env, ir_node *block,
 /**
  * returns whether the cmp evaluates to true or false, or can't be evaluated!
  * 1: true, 0: false, -1: can't evaluate
+ *
+ * @param pnc       the compare mode of the Compare
+ * @param tv_left   the left tarval
+ * @param tv_right  the right tarval
  */
-static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
-       pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
+static int eval_cmp_tv(pn_Cmp pnc, tarval *tv_left, tarval *tv_right) {
+       pn_Cmp cmp_result = tarval_cmp(tv_left, tv_right);
 
-       // does the compare evaluate to true?
-       if(cmp_result == pn_Cmp_False)
+       /* does the compare evaluate to true? */
+       if (cmp_result == pn_Cmp_False)
                return -1;
-       if((cmp_result & pnc) != cmp_result)
+       if ((cmp_result & pnc) != cmp_result)
                return 0;
 
        return 1;
 }
 
 /**
- * Check for Const or constlike Confirm.
+ * returns whether the cmp evaluates to true or false, or can't be evaluated!
+ * 1: true, 0: false, -1: can't evaluate
+ *
+ * @param env      the environment
+ * @param cand     the candidate node, either a Const or a Confirm
  */
-static int is_Const_or_Confirm(const ir_node *node) {
-       if (is_Confirm(node)) {
-               if (get_Confirm_cmp(node) == pn_Cmp_Eq)
-                       node = get_Confirm_bound(node);
+static int eval_cmp(condeval_env_t *env, ir_node *cand) {
+       if (is_Const(cand)) {
+               tarval *tv_cand   = get_Const_tarval(cand);
+               tarval *tv_cmp    = get_Const_tarval(env->cnst);
+
+               return eval_cmp_tv(env->pnc, tv_cand, tv_cmp);
+       } else { /* a Confirm */
+               tarval *res = computed_value_Cmp_Confirm(env->cmp, cand, env->cnst, env->pnc);
+
+               if (res == tarval_bad)
+                       return -1;
+               return res == tarval_b_true;
        }
+}
+
+/**
+ * Check for Const or Confirm with Const.
+ */
+static int is_Const_or_Confirm(const ir_node *node) {
+       if (is_Confirm(node))
+               node = get_Confirm_bound(node);
        return is_Const(node);
 }
 
 /**
- * get the tarval of a COnst or constlike Confirm
+ * get the tarval of a Const or Confirm with
  */
 static tarval *get_Const_or_Confirm_tarval(const ir_node *node) {
        if (is_Confirm(node)) {
-               if (get_Confirm_cmp(node) == pn_Cmp_Eq)
+               if (get_Confirm_bound(node))
                        node = get_Confirm_bound(node);
        }
        return get_Const_tarval(node);
 }
 
-static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
+static ir_node *find_const_or_confirm(condeval_env_t *env, ir_node *jump, ir_node *value)
 {
        ir_node *block = get_nodes_block(jump);
 
@@ -389,11 +415,8 @@ static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
                return NULL;
        mark_irn_visited(value);
 
-       if(is_Const_or_Confirm(value)) {
-               tarval *tv_const = get_Const_tarval(env->cnst);
-               tarval *tv       = get_Const_or_Confirm_tarval(value);
-
-               if(eval_cmp(env->pnc, tv, tv_const) <= 0) {
+       if (is_Const_or_Confirm(value)) {
+               if (eval_cmp(env, value) <= 0) {
                        return NULL;
                }
 
@@ -403,12 +426,12 @@ static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
                        env->true_block, block
                ));
 
-               // adjust true_block to point directly towards our jump
+               /* 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
+               /* we need a bigger visited nr when going back */
                env->visited_nr++;
 
                return block;
@@ -417,7 +440,7 @@ static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
        if(is_Phi(value)) {
                int i, arity;
 
-               /* the phi has to be in the same block as the jump */
+               /* the Phi has to be in the same Block as the Jmp */
                if(get_nodes_block(value) != block) {
                        return NULL;
                }
@@ -428,7 +451,7 @@ static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
                        ir_node *phi_pred = get_Phi_pred(value, i);
                        ir_node *cfgpred  = get_Block_cfgpred(block, i);
 
-                       copy_block = find_const(env, cfgpred, phi_pred);
+                       copy_block = find_const_or_confirm(env, cfgpred, phi_pred);
                        if(copy_block == NULL)
                                continue;
 
@@ -458,10 +481,10 @@ static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
        }
        mark_irn_visited(value);
 
-       if(is_Const_or_Confirm(value)) {
+       if (is_Const_or_Confirm(value)) {
                tarval *tv = get_Const_or_Confirm_tarval(value);
 
-               if(tv != env->tv)
+               if (tv != env->tv)
                        return NULL;
 
                DB((
@@ -470,12 +493,12 @@ static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
                        env->true_block, block
                ));
 
-               // adjust true_block to point directly towards our jump
+               /* 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
+               /* we need a bigger visited nr when going back */
                env->visited_nr++;
 
                return block;
@@ -483,7 +506,7 @@ static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
        if(is_Phi(value)) {
                int i, arity;
 
-               // the phi has to be in the same block as the jump
+               /* the Phi has to be in the same Block as the Jmp */
                if(get_nodes_block(value) != block)
                        return NULL;
 
@@ -505,7 +528,7 @@ static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
                                env->cnst_pos  = i;
                        }
 
-                       // return now as we can't process more possibilities in 1 run
+                       /* return now as we can't process more possibilities in 1 run */
                        return copy_block;
                }
        }
@@ -539,15 +562,16 @@ static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
                }
 
                /* negate condition when we're looking for the false block */
-               if(env->tv == get_tarval_b_false()) {
+               if(env->tv == tarval_b_false) {
                        pnc = get_negated_pnc(pnc, get_irn_mode(right));
                }
 
-               // (recursively) look if a pred of a phi is a constant
+               /* (recursively) look if a pred of a Phi is a constant or a Confirm */
+               env->cmp  = cmp;
                env->pnc  = pnc;
                env->cnst = right;
 
-               return find_const(env, jump, left);
+               return find_const_or_confirm(env, jump, left);
        }
 
        return NULL;
@@ -592,7 +616,7 @@ static void cond_eval(ir_node* block, void* data)
                return;
 
        selector = get_Cond_selector(cond);
-       // TODO handle switch Conds
+       /* TODO handle switch Conds */
        if (get_irn_mode(selector) != mode_b)
                return;
 
@@ -608,28 +632,28 @@ static void cond_eval(ir_node* block, void* data)
                                tarval *tv_left  = get_Const_tarval(left);
                                tarval *tv_right = get_Const_tarval(right);
 
-                               selector_evaluated = eval_cmp(pnc, tv_left, tv_right);
+                               selector_evaluated = eval_cmp_tv(pnc, tv_left, tv_right);
                                if(selector_evaluated < 0)
                                        return;
                        }
                }
-       } else if(is_Const_or_Confirm(selector)) {
+       } else if (is_Const_or_Confirm(selector)) {
                tarval *tv = get_Const_or_Confirm_tarval(selector);
-               if(tv == get_tarval_b_true()) {
+               if(tv == tarval_b_true) {
                        selector_evaluated = 1;
                } else {
-                       assert(tv == get_tarval_b_false());
+                       assert(tv == tarval_b_false);
                        selector_evaluated = 0;
                }
        }
 
        env.cnst_pred = NULL;
        if (get_Proj_proj(projx) == pn_Cond_false) {
-               env.tv = get_tarval_b_false();
+               env.tv = tarval_b_false;
                if(selector_evaluated >= 0)
                        selector_evaluated = !selector_evaluated;
        } else {
-               env.tv = get_tarval_b_true();
+               env.tv = tarval_b_true;
        }
 
        if(selector_evaluated == 0) {
@@ -645,7 +669,7 @@ static void cond_eval(ir_node* block, void* data)
                return;
        }
 
-       // (recursively) look if a pred of a phi is a constant
+       /* (recursively) look if a pred of a Phi is a constant or a Confirm */
        env.true_block = block;
        inc_irg_visited(current_ir_graph);
        env.visited_nr = get_irg_visited(current_ir_graph);
@@ -655,12 +679,12 @@ static void cond_eval(ir_node* block, void* data)
                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
+        * 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 */
+       /* shorten Phis */
        foreach_out_edge_safe(env.cnst_pred, edge, next) {
                ir_node *node = get_edge_src_irn(edge);
 
@@ -686,8 +710,7 @@ void opt_cond_eval(ir_graph* irg)
        normalize_proj_nodes(irg);
 
        edges_assure(irg);
-       set_using_irn_link(irg);
-       set_using_irn_visited(irg);
+       ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
 
        changed = 0;
        do {
@@ -696,8 +719,7 @@ void opt_cond_eval(ir_graph* irg)
                changed |= rerun;
        } while (rerun);
 
-       clear_using_irn_visited(irg);
-       clear_using_irn_link(irg);
+       ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
 
        if (changed) {
                /* control flow changed, some blocks may become dead */