/*
- * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
#endif
#include <assert.h>
-
#include "iroptimize.h"
#include "obst.h"
#include "irnode_t.h"
#include "irdump.h"
#include "debug.h"
-DEBUG_ONLY(firm_dbg_module_t *dbg);
+DEBUG_ONLY(static firm_dbg_module_t *dbg);
/** allow every Psi to be created. */
static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
/**
* Default options.
*/
-static const opt_if_conv_info_t default_info = {
+static const ir_settings_if_conv_t default_info = {
0, /* doesn't matter for Psi */
default_allow_ifconv
};
int has_pinned; /**< set if the block contains instructions that cannot be moved */
} block_info;
-#define get_block_blockinfo(block) ((block_info *)get_irn_link(block))
+/** Returns the additional block info of a block. */
+static INLINE block_info* get_block_blockinfo(const ir_node* block) {
+ return get_irn_link(block);
+}
+
/**
* Returns non-zero if a Block can be emptied.
*/
-static int can_empty_block(ir_node *block)
-{
+static int can_empty_block(ir_node *block) {
return !get_block_blockinfo(block)->has_pinned;
}
int i;
/* No need to find the conditional block if this block cannot be emptied and
- * therefore not moved
- */
+ * therefore not moved */
if (!can_empty_block(start)) return NULL;
arity = get_irn_arity(start);
NEW_ARR_A(ir_node *, ins, arity - 1);
l = 0;
- for (k = 0; k < i; ++k) ins[l++] = get_irn_n(node, k);
- for (++k; k < j; ++k) ins[l++] = get_irn_n(node, k);
- for (++k; k < arity; ++k) ins[l++] = get_irn_n(node, k);
+ for (k = 0; k < i; ++k) ins[l++] = get_irn_n(node, k);
+ for (++k; k < j; ++k) ins[l++] = get_irn_n(node, k);
+ for (++k; k < arity; ++k) ins[l++] = get_irn_n(node, k);
ins[l++] = new_pred;
assert(l == arity - 1);
set_irn_in(node, l, ins);
static void if_conv_walker(ir_node* block, void* env)
{
+ ir_settings_if_conv_t* opt_info = env;
int arity;
int i;
- opt_if_conv_info_t *opt_info = env;
/* Bail out, if there are no Phis at all */
if (get_block_blockinfo(block)->phi == NULL) return;
arity = get_irn_arity(block);
for (i = 0; i < arity; ++i) {
ir_node* pred0;
- cdep* cdep;
+ ir_cdep* cdep;
pred0 = get_nodes_block(get_irn_n(block, i));
for (cdep = find_cdep(pred0); cdep != NULL; cdep = cdep->next) {
for (j = i + 1; j < arity; ++j) {
ir_node* projx1;
ir_node* conds[1];
- ir_node* vals[2];
- ir_node* psi = NULL;
ir_node* psi_block;
ir_node* phi;
ir_node* pred1;
+ dbg_info* cond_dbg;
pred1 = get_nodes_block(get_irn_n(block, j));
conds[0] = get_Cond_selector(cond);
psi_block = get_nodes_block(cond);
+ cond_dbg = get_irn_dbg_info(cond);
do {
ir_node* val_i = get_irn_n(phi, i);
ir_node* val_j = get_irn_n(phi, j);
+ ir_node* psi;
+ ir_node* next_phi;
if (val_i == val_j) {
psi = val_i;
DB((dbg, LEVEL_2, "Generating no psi, because both values are equal\n"));
} else {
+ ir_node* vals[2];
+
/* Something is very fishy if two predecessors of a PhiM point into
* one block, but not at the same memory node
*/
vals[1] = val_i;
}
- psi = new_r_Psi(
- current_ir_graph, psi_block, 1, conds, vals, get_irn_mode(phi)
- );
+ psi = new_rd_Psi(cond_dbg, current_ir_graph, psi_block, 1, conds, vals, get_irn_mode(phi));
DB((dbg, LEVEL_2, "Generating %+F for %+F\n", psi, phi));
}
+ next_phi = get_irn_link(phi);
+
if (arity == 2) {
exchange(phi, psi);
} else {
rewire(phi, i, j, psi);
}
- phi = get_irn_link(phi);
+ phi = next_phi;
} while (phi != NULL);
exchange(get_nodes_block(get_irn_n(block, i)), psi_block);
/**
* Daisy-chain all phis in a block
- * If a non-movable node is encountered set the has_pinned flag
+ * If a non-movable node is encountered set the has_pinned flag in its block.
*/
-static void collect_phis(ir_node *node, void *env)
-{
+static void collect_phis(ir_node *node, void *env) {
(void) env;
if (is_Phi(node)) {
set_irn_link(node, bi->phi);
bi->phi = node;
- }
- else {
+ } else {
if (is_no_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
/*
* Ignore control flow nodes, these will be removed.
* This ignores Raise. That is surely bad. FIXME.
*/
- if (! is_cfop(node)) {
+ if (!is_cfop(node)) {
ir_node *block = get_nodes_block(node);
block_info *bi = get_block_blockinfo(block);
}
}
-
-/*
- * Transform multiple cascaded Psis into one Psi
- */
-static ir_node* fold_psi(ir_node* psi)
+static void optimise_psis_0(ir_node* psi, void* env)
{
- int arity = get_Psi_n_conds(psi);
- int new_arity = 0;
- int i;
- ir_node* n;
- ir_node** conds;
- ir_node** vals;
- int j;
- int k;
- int a;
- ir_node* new_psi;
+ ir_node* t;
+ ir_node* f;
- for (i = 0; i < arity; ++i) {
- n = get_Psi_val(psi, i);
- if (get_irn_op(n) == op_Psi) {
- new_arity += get_Psi_n_conds(n) + 1;
- } else {
- ++new_arity;
- }
- }
- n = get_Psi_default(psi);
- if (get_irn_op(n) == op_Psi) {
- new_arity += get_Psi_n_conds(n);
- }
+ (void) env;
- if (arity == new_arity) return psi; // no attached Psis found
- DB((dbg, LEVEL_1, "Folding %+F from %d to %d conds\n", psi, arity, new_arity));
+ if (!is_Psi(psi)) return;
- NEW_ARR_A(ir_node *, conds, new_arity);
- NEW_ARR_A(ir_node *, vals, new_arity + 1);
- j = 0;
- for (i = 0; i < arity; ++i) {
- ir_node* c = get_Psi_cond(psi, i);
-
- n = get_Psi_val(psi, i);
- if (get_irn_op(n) == op_Psi) {
- a = get_Psi_n_conds(n);
- for (k = 0; k < a; ++k) {
- conds[j] = new_r_And(
- current_ir_graph, get_nodes_block(psi),
- c, get_Psi_cond(n, k), mode_b
- );
- vals[j] = get_Psi_val(n, k);
- ++j;
- }
- conds[j] = c;
- vals[j] = get_Psi_default(n);
- } else {
- conds[j] = c;
- vals[j] = n;
- }
- ++j;
- }
- n = get_Psi_default(psi);
- if (get_irn_op(n) == op_Psi) {
- a = get_Psi_n_conds(n);
- for (k = 0; k < a; ++k) {
- conds[j] = get_Psi_cond(n, k);
- vals[j] = get_Psi_val(n, k);
- ++j;
- }
- vals[j] = get_Psi_default(n);
- } else {
- vals[j] = n;
- }
- assert(j == new_arity);
- new_psi = new_r_Psi(
- current_ir_graph, get_nodes_block(psi),
- new_arity, conds, vals, get_irn_mode(psi)
- );
- DB((dbg, LEVEL_1, "Folded %+F into new %+F\n", psi, new_psi));
- exchange(psi, new_psi);
- return new_psi;
-}
+ t = get_Psi_val(psi, 0);
+ f = get_Psi_default(psi);
+ DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", psi, t, f));
-/*
- * Merge consecutive psi inputs if the data inputs are the same
- */
-static ir_node* meld_psi(ir_node* psi)
-{
- int arity = get_Psi_n_conds(psi);
- int new_arity;
- ir_node** conds;
- ir_node** vals;
- ir_node* cond;
- ir_node* val;
- int i;
- int j;
- ir_node* new_psi;
-
- new_arity = 1;
- val = get_Psi_val(psi, 0);
- DB((dbg, LEVEL_1, "Pred 0 of %+F is %+F\n", psi, val));
- for (i = 1; i < arity; ++i) {
- ir_node* v = get_Psi_val(psi, i);
- DB((dbg, LEVEL_1, "Pred %2d of %+F is %+F\n", i, psi, v));
- if (val != v) {
- val = v;
- ++new_arity;
- }
+ if (is_Unknown(t)) {
+ DB((dbg, LEVEL_3, "Replace Psi with unknown operand by %+F\n", f));
+ exchange(psi, f);
+ return;
}
- DB((dbg, LEVEL_1, "Default of %+F is %+F\n", psi, get_Psi_default(psi)));
- if (val == get_Psi_default(psi)) --new_arity;
-
- DB((dbg, LEVEL_1, "Melding Psi %+F from %d conds to %d\n", psi, arity, new_arity));
-
- if (new_arity == arity) return psi;
-
- /* If all data inputs of the Psi are equal, exchange the Psi with that value */
- if (new_arity == 0) {
- exchange(psi, val);
- return val;
+ if (is_Unknown(f)) {
+ DB((dbg, LEVEL_3, "Replace Psi with unknown operand by %+F\n", t));
+ exchange(psi, t);
+ return;
}
- NEW_ARR_A(ir_node *, conds, new_arity);
- NEW_ARR_A(ir_node *, vals, new_arity + 1);
- cond = get_Psi_cond(psi, 0);
- val = get_Psi_val(psi, 0);
- j = 0;
- for (i = 1; i < arity; ++i) {
- ir_node* v = get_Psi_val(psi, i);
-
- if (v == val) {
- cond = new_r_Or(
- current_ir_graph, get_nodes_block(psi),
- cond, get_Psi_cond(psi, i), mode_b
- );
- } else {
- conds[j] = cond;
- vals[j] = val;
- ++j;
- val = v;
+ if (is_Psi(t)) {
+ ir_graph* irg = current_ir_graph;
+ ir_node* block = get_nodes_block(psi);
+ ir_mode* mode = get_irn_mode(psi);
+ ir_node* c0 = get_Psi_cond(psi, 0);
+ ir_node* c1 = get_Psi_cond(t, 0);
+ ir_node* t1 = get_Psi_val(t, 0);
+ ir_node* f1 = get_Psi_default(t);
+ if (f == f1) {
+ /* Psi(c0, Psi(c1, x, y), y) -> typical if (c0 && c1) x else y */
+ ir_node* and_ = new_r_And(irg, block, c0, c1, mode_b);
+ ir_node* vals[2] = { t1, f1 };
+ ir_node* new_psi = new_r_Psi(irg, block, 1, &and_, vals, mode);
+ exchange(psi, new_psi);
+ } else if (f == t1) {
+ /* Psi(c0, Psi(c1, x, y), x) */
+ ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
+ ir_node* and_ = new_r_And(irg, block, c0, not_c1, mode_b);
+ ir_node* vals[2] = { f1, t1 };
+ ir_node* new_psi = new_r_Psi(irg, block, 1, &and_, vals, mode);
+ exchange(psi, new_psi);
+ }
+ } else if (is_Psi(f)) {
+ ir_graph* irg = current_ir_graph;
+ ir_node* block = get_nodes_block(psi);
+ ir_mode* mode = get_irn_mode(psi);
+ ir_node* c0 = get_Psi_cond(psi, 0);
+ ir_node* c1 = get_Psi_cond(f, 0);
+ ir_node* t1 = get_Psi_val(f, 0);
+ ir_node* f1 = get_Psi_default(f);
+ if (t == t1) {
+ /* Psi(c0, x, Psi(c1, x, y)) -> typical if (c0 || c1) x else y */
+ ir_node* or_ = new_r_Or(irg, block, c0, c1, mode_b);
+ ir_node* vals[2] = { t1, f1 };
+ ir_node* new_psi = new_r_Psi(irg, block, 1, &or_, vals, mode);
+ exchange(psi, new_psi);
+ } else if (t == f1) {
+ /* Psi(c0, x, Psi(c1, y, x)) */
+ ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
+ ir_node* or_ = new_r_Or(irg, block, c0, not_c1, mode_b);
+ ir_node* vals[2] = { f1, t1 };
+ ir_node* new_psi = new_r_Psi(irg, block, 1, &or_, vals, mode);
+ exchange(psi, new_psi);
}
}
- if (val != get_Psi_default(psi)) {
- conds[j] = cond;
- vals[j] = val;
- ++j;
- }
- vals[j] = get_Psi_default(psi);
- assert(j == new_arity);
- new_psi = new_r_Psi(
- current_ir_graph, get_nodes_block(psi),
- new_arity, conds, vals, get_irn_mode(psi)
- );
- DB((dbg, LEVEL_1, "Molded %+F into %+F\n", psi, new_psi));
- exchange(psi, new_psi);
- return new_psi;
}
-/**
- * Split a Psi with multiple conditions into multiple Psis with one condtition
- * each
- */
-static ir_node* split_psi(ir_node* psi)
+static void optimise_psis_1(ir_node* psi, void* env)
{
- int arity = get_Psi_n_conds(psi);
+ ir_node* t;
+ ir_node* f;
ir_mode* mode;
- ir_node* block;
- ir_node* rval;
- int i;
- if (arity == 1) return psi;
-
- mode = get_irn_mode(psi);
- block = get_nodes_block(psi);
- rval = get_Psi_default(psi);
- for (i = arity - 1; i >= 0; --i) {
- ir_node* conds[1];
- ir_node* vals[2];
-
- conds[0] = get_Psi_cond(psi, i);
- vals[0] = get_Psi_val(psi, i);
- vals[1] = rval;
- rval = new_r_Psi(
- current_ir_graph, block, 1, conds, vals, mode
- );
- }
- exchange(psi, rval);
- return rval;
-}
+ (void) env;
+ if (!is_Psi(psi)) return;
-static void optimise_psis(ir_node* node, void* env)
-{
- (void) env;
+ t = get_Psi_val(psi, 0);
+ f = get_Psi_default(psi);
- if (get_irn_op(node) != op_Psi) return;
-#if 1
- node = fold_psi(node);
-#endif
-#if 1
- node = meld_psi(node);
-#endif
-#if 1
- node = split_psi(node);
-#endif
+ DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", psi, t, f));
+
+ mode = get_irn_mode(psi);
+
+ if (is_Const(t) && is_Const(f) && (mode_is_int(mode))) {
+ ir_node* block = get_nodes_block(psi);
+ ir_node* c = get_Psi_cond(psi, 0);
+ tarval* tv_t = get_Const_tarval(t);
+ tarval* tv_f = get_Const_tarval(f);
+ if (tarval_is_one(tv_t) && tarval_is_null(tv_f)) {
+ ir_node* conv = new_r_Conv(current_ir_graph, block, c, mode);
+ exchange(psi, conv);
+ } else if (tarval_is_null(tv_t) && tarval_is_one(tv_f)) {
+ ir_node* not_ = new_r_Not(current_ir_graph, block, c, mode_b);
+ ir_node* conv = new_r_Conv(current_ir_graph, block, not_, mode);
+ exchange(psi, conv);
+ }
+ }
}
-void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params)
+void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
{
struct obstack obst;
- opt_if_conv_info_t p;
-
- if (! get_opt_if_conversion())
- return;
+ ir_settings_if_conv_t p;
/* get the parameters */
p = (params != NULL ? *params : default_info);
compute_cdep(irg);
assure_doms(irg);
+ set_using_irn_link(irg);
+
obstack_init(&obst);
irg_block_walk_graph(irg, init_block_link, NULL, &obst);
irg_walk_graph(irg, collect_phis, NULL, NULL);
irg_block_walk_graph(irg, NULL, if_conv_walker, &p);
+ clear_using_irn_link(irg);
+
local_optimize_graph(irg);
- irg_walk_graph(irg, NULL, optimise_psis, NULL);
+ irg_walk_graph(irg, NULL, optimise_psis_0, NULL);
+#if 1
+ irg_walk_graph(irg, NULL, optimise_psis_1, NULL);
+#endif
obstack_free(&obst, NULL);
+ /* TODO: graph might be changed, handle more gracefull */
+ set_irg_outs_inconsistent(irg);
free_dom(irg);
+
free_cdep(irg);
}