#include "irgraph.h"
#include "tv.h"
+//#define AVOID_PHIB
+
DEBUG_ONLY(static firm_dbg_module_t *dbg);
/**
return value;
}
+ irg = get_irn_irg(block);
+ assert(block != get_irg_start_block(irg));
+
// blocks with only 1 pred need no phi
n_cfgpreds = get_Block_n_cfgpreds(block);
if(n_cfgpreds == 1) {
ir_node *pred_block = get_Block_cfgpred_block(block, 0);
- ir_node *value = search_def_and_create_phis(pred_block, mode);
+ ir_node *value = search_def_and_create_phis(pred_block, mode);
set_irn_link(block, value);
mark_irn_visited(block);
for(i = 0; i < n_cfgpreds; ++i)
in[i] = new_Unknown(mode);
- irg = get_irn_irg(block);
phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
set_irn_link(block, phi);
mark_irn_visited(block);
const ir_edge_t *next;
ir_node *value;
- assert(n_vals > 0);
+ assert(n_vals == 2);
irg = get_irn_irg(vals[0]);
inc_irg_visited(irg);
// Only fix the users of the first, i.e. the original node
value = vals[0];
- // this can happen when fixing phi preds, we mustn't fix the users
- if(get_nodes_block(value) != blocks[0]) return;
-
foreach_out_edge_safe(value, edge, next) {
ir_node *user = get_edge_src_irn(edge);
int j = get_edge_src_pos(edge);
if(get_irn_op(user) == op_End)
continue;
+ if (user_block == blocks[1])
+ continue;
+
DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
if(is_Phi(user)) {
kill that edge later) */
} condeval_env_t;
+static ir_node *copy_and_fix_node(const condeval_env_t *env, ir_node *block,
+ ir_node *copy_block, int j, ir_node *node) {
+ int i, arity;
+ ir_node *copy;
+
+ /* 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 */
+ if(get_irn_visited(copy) >= env->visited_nr) {
+ copy = get_irn_link(copy);
+ }
+ } else {
+ copy = exact_copy(node);
+ set_nodes_block(copy, copy_block);
+
+ assert(get_irn_mode(copy) != mode_X);
+
+ arity = get_irn_arity(copy);
+ for(i = 0; i < arity; ++i) {
+ ir_node *pred = get_irn_n(copy, i);
+ ir_node *new_pred;
+
+ if(get_nodes_block(pred) != block)
+ continue;
+
+ if(get_irn_visited(pred) >= env->visited_nr) {
+ new_pred = get_irn_link(pred);
+ } else {
+ new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
+ }
+ set_irn_n(copy, i, new_pred);
+ }
+ }
+
+ set_irn_link(node, copy);
+ set_irn_visited(node, env->visited_nr);
+
+ return copy;
+}
+
static void copy_and_fix(const condeval_env_t *env, ir_node *block,
ir_node *copy_block, int j) {
const ir_edge_t *edge;
/* ignore control flow */
if (mode == mode_X || is_Cond(node))
continue;
-#if 1
+#ifdef AVOID_PHIB
/* 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 it's users */
}
#endif
- /* we can evaluate Phis right now, all other nodes get copied */
- if (is_Phi(node)) {
- copy = get_Phi_pred(node, j);
- } else {
- copy = exact_copy(node);
- set_nodes_block(copy, copy_block);
- }
-
- set_irn_link(node, copy);
- set_irn_visited(node, env->visited_nr);
+ 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 */
if (mode == mode_X || is_Cond(node))
continue;
+#ifdef AVOID_PHIB
if (mode == mode_b)
continue;
+#endif
DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
}
}
+/**
+ * returns wether the cmp evaluates to true or false, or can't be evaluated!
+ * 1: true, 0: false, -1: can't evaluate
+ */
static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
// does the compare evaluate to true?
if(cmp_result == pn_Cmp_False)
- return 0;
+ return -1;
if((cmp_result & pnc) != cmp_result)
return 0;
tarval *tv_const = get_Const_tarval(env->cnst);
tarval *tv = get_Const_tarval(value);
- if(!eval_cmp(env->pnc, tv, tv_const)) {
+ if(eval_cmp(env->pnc, tv, tv_const) <= 0) {
return NULL;
}
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 jump */
if(get_nodes_block(value) != block) {
return NULL;
}
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;
}
}
mark_irn_visited(value);
if(is_Const(value)) {
- tarval *tv = get_Const_tarval(value);
+ tarval *tv = get_Const_tarval(value);
if(tv != env->tv)
return NULL;
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_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);