* Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
*/
+#if 1
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
+#include <assert.h>
+
+#include "obst.h"
+#include "irnode_t.h"
+#include "cdep.h"
+#include "ircons.h"
+#include "ifconv.h"
+#include "irdom.h"
+#include "irgmod.h"
+#include "irgopt.h"
+#include "irgwalk.h"
+#include "irtools.h"
+#include "return.h"
+#include "array.h"
+
+// debug
+#include "irdump.h"
+#include "debug.h"
+
+DEBUG_ONLY(firm_dbg_module_t *dbg);
+
+static ir_node* walk_to_projx(ir_node* start)
+{
+ ir_node* pred;
+
+ pred = get_nodes_block(start);
+
+ /* if there are multiple control flow predecessors nothing sensible can be
+ * done */
+ if (get_irn_arity(pred) > 1) return NULL;
+
+ pred = get_irn_n(pred, 0);
+ if (get_irn_op(pred) == op_Proj) {
+ assert(get_irn_mode(pred) == mode_X);
+ return pred;
+ } else {
+ return NULL;
+ }
+}
+
+/**
+ * Additional block info.
+ */
+typedef struct block_info {
+ ir_node *phi; /**< head of the Phi list */
+ 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 non-zero if a Block can be emptied.
+ */
+static int can_empty_block(ir_node *block)
+{
+ return !get_block_blockinfo(block)->has_pinned;
+}
+
+
+/**
+ * Copies the DAG starting at node to the ith predecessor block of src_block
+ * -if the node isn't in the src_block, this is a nop and the node is returned
+ * -if the node is a phi in the src_block, the ith predecessor of the phi is
+ * returned
+ * otherwise returns the copy of the passed node
+ */
+static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
+{
+ ir_node* dst_block;
+ ir_node* copy;
+ int arity;
+ int j;
+
+ if (get_nodes_block(node) != src_block) return node;
+ if (get_irn_op(node) == op_Phi) return get_irn_n(node, i);
+
+ copy_irn_to_irg(node, current_ir_graph);
+ copy = get_irn_link(node);
+ dst_block = get_nodes_block(get_irn_n(src_block, i));
+ set_nodes_block(copy, dst_block);
+
+ DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
+ node, dst_block, copy));
+
+ arity = get_irn_arity(node);
+ for (j = 0; j < arity; ++j) {
+ set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
+ DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
+ }
+ return copy;
+}
+
+
+/**
+ * Duplicate and move the contents of ith block predecessor into its
+ * predecessors if the block has multiple control dependencies and only one
+ * successor.
+ * Also bail out if the block contains non-movable nodes, because later
+ * if-conversion would be pointless.
+ */
+static int fission_block(ir_node* block, int i)
+{
+ ir_node* pred = get_irn_n(block, i);
+ ir_node* pred_block;
+ block_info* info;
+ ir_node* phi;
+ int pred_arity;
+ int arity;
+ ir_node** ins;
+ int j;
+
+ if (get_irn_op(pred) != op_Jmp) return 0;
+ pred_block = get_nodes_block(pred);
+
+ if (!has_multiple_cdep(pred_block)) return 0;
+ if (!can_empty_block(pred_block)) return 0;
+
+ DB((dbg, LEVEL_1, "Fissioning block %+F\n", pred_block));
+
+ pred_arity = get_irn_arity(pred_block);
+ arity = get_irn_arity(block);
+ info = get_block_blockinfo(block);
+ NEW_ARR_A(ir_node *, ins, arity + pred_arity - 1);
+ for (phi = info->phi; phi != NULL; phi = get_irn_link(phi)) {
+ for (j = 0; j < i; ++j) ins[j] = get_irn_n(phi, j);
+ for (j = 0; j < pred_arity; ++j) {
+ ins[i + j] = copy_to(get_irn_n(phi, i), pred_block, j);
+ }
+ for (j = i + 1; j < arity; ++j) {
+ ins[pred_arity - 1 + j] = get_irn_n(phi, j);
+ }
+ set_irn_in(phi, arity + pred_arity - 1, ins);
+ }
+ for (j = 0; j < i; ++j) ins[j] = get_irn_n(block, j);
+ for (j = 0; j < pred_arity; ++j) ins[i + j] = get_irn_n(pred_block, j);
+ for (j = i + 1; j < arity; ++j) ins[pred_arity - 1 + j] = get_irn_n(block, j);
+ set_irn_in(block, arity + pred_arity - 1, ins);
+
+ /* Kill all Phis in the fissioned block
+ * This is to make sure they're not kept alive
+ */
+ info = get_block_blockinfo(pred_block);
+ phi = info->phi;
+ while (phi != NULL) {
+ ir_node* next = get_irn_link(phi);
+ exchange(phi, new_Bad());
+ phi = next;
+ }
+ return 1;
+}
+
+
+/**
+ * Remove predecessors i and j from node and add predecessor new_pred
+ */
+static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
+{
+ int arity = get_irn_arity(node);
+ ir_node **ins;
+ int k;
+ int l;
+
+ 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);
+ 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_node* phi;
+ int arity;
+ int i;
+
+ /* Bail out, if there are no Phis at all */
+ if (get_block_blockinfo(block)->phi == NULL) return;
+
+restart:
+ arity = get_irn_arity(block);
+ for (i = 0; i < arity; ++i) {
+ if (fission_block(block, i)) goto restart;
+ }
+ //return;
+
+ arity = get_irn_arity(block);
+ for (i = 0; i < arity; ++i) {
+ ir_node* pred;
+ ir_node* cond;
+ ir_node* projx0;
+ int j;
+
+ projx0 = walk_to_projx(get_irn_n(block, i));
+ if (projx0 == NULL) return;
+ pred = get_Proj_pred(projx0);
+ if (get_irn_op(pred) != op_Cond || get_irn_mode(get_Cond_selector(pred)) != mode_b) continue;
+ cond = pred;
+
+ if (!can_empty_block(get_nodes_block(get_irn_n(block, i)))) {
+ DB((dbg, LEVEL_1, "Cannot empty block %+F\n",
+ get_nodes_block(get_irn_n(block, i))
+ ));
+ continue;
+ }
+
+ for (j = i + 1; j < arity; ++j) {
+ ir_node* projx1;
+ ir_node* psi_block;
+ ir_node* conds[1];
+ ir_node* vals[2];
+ ir_node* psi;
+
+ projx1 = walk_to_projx(get_irn_n(block, j));
+ if (projx1 == NULL) continue;
+ pred = get_Proj_pred(projx1);
+ if (get_irn_op(pred) != op_Cond || get_irn_mode(get_Cond_selector(pred)) != mode_b) continue;
+ if (pred != cond) continue;
+ DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n", cond, projx0, projx1));
+
+ if (!can_empty_block(get_nodes_block(get_irn_n(block, j)))) {
+ DB((dbg, LEVEL_1, "Cannot empty %+F\n", get_nodes_block(get_irn_n(block, j))));
+ continue;
+ }
+
+ conds[0] = get_Cond_selector(cond);
+
+ psi_block = get_nodes_block(cond);
+ phi = get_block_blockinfo(block)->phi;
+ do {
+ ir_node* val_i = get_irn_n(phi, i);
+ ir_node* val_j = get_irn_n(phi, j);
+
+ if (val_i == val_j) {
+ psi = val_i;
+ DB((dbg, LEVEL_2, "Generating no psi, because both values are equal\n"));
+ } else {
+ /* Something is very fishy if two predecessors of a PhiM point into
+ * one block, but not at the same memory node
+ */
+ assert(get_irn_mode(phi) != mode_M);
+ if (get_Proj_proj(projx0) == pn_Cond_true) {
+ vals[0] = val_i;
+ vals[1] = val_j;
+ } else {
+ vals[0] = val_j;
+ vals[1] = val_i;
+ }
+ psi = new_r_Psi(
+ current_ir_graph, psi_block, 1, conds, vals, get_irn_mode(phi)
+ );
+ DB((dbg, LEVEL_2, "Generating %+F for %+F\n", psi, phi));
+ }
+
+ if (arity == 2) {
+ exchange(phi, psi);
+ } else {
+ rewire(phi, i, j, psi);
+ }
+
+ phi = get_irn_link(phi);
+ } while (phi != NULL);
+
+ exchange(get_nodes_block(get_irn_n(block, i)), psi_block);
+ exchange(get_nodes_block(get_irn_n(block, j)), psi_block);
+
+ if (arity == 2) {
+ DB((dbg, LEVEL_1, "Welding block %+F to %+F\n", block, psi_block));
+ get_block_blockinfo(psi_block)->has_pinned |= get_block_blockinfo(block)->has_pinned;
+ exchange(block, psi_block);
+ return;
+ } else {
+ rewire(block, i, j, new_r_Jmp(current_ir_graph, psi_block));
+ goto restart;
+ }
+ }
+ }
+}
+
+/**
+ * Block walker: add additional data
+ */
+static void init_block_link(ir_node *block, void *env)
+{
+ struct obstack *obst = env;
+ block_info *bi = obstack_alloc(obst, sizeof(*bi));
+
+ bi->phi = NULL;
+ bi->has_pinned = 0;
+ set_irn_link(block, bi);
+}
+
+
+/**
+ * Daisy-chain all phis in a block
+ * If a non-movable node is encountered set the has_pinned flag
+ */
+static void collect_phis(ir_node *node, void *env)
+{
+ if (is_Phi(node)) {
+ ir_node *block = get_nodes_block(node);
+ block_info *bi = get_block_blockinfo(block);
+
+ set_irn_link(node, bi->phi);
+ bi->phi = node;
+ }
+ 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)) {
+ ir_node *block = get_nodes_block(node);
+ block_info *bi = get_block_blockinfo(block);
+
+ DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
+ bi->has_pinned = 1;
+ }
+ }
+ }
+}
+
+
+/*
+ * Transform multiple cascaded Psis into one Psi
+ */
+static ir_node* fold_psi(ir_node* psi)
+{
+ 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;
+
+ 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);
+ }
+
+ 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));
+
+ 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;
+}
+
+
+/*
+ * 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;
+ }
+ }
+ 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;
+ }
+
+ 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 (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)
+{
+ int arity = get_Psi_n_conds(psi);
+ 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;
+}
+
+
+static void optimise_psis(ir_node* node, void* env)
+{
+ 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
+}
+
+
+void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params)
+{
+ struct obstack obst;
+
+ if (!get_opt_if_conversion())
+ return;
+
+ FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
+
+ DB((dbg, LEVEL_1, "Running if-conversion on %+F\n", irg));
+
+ dump_ir_block_graph(irg, "_00_pre");
+
+ normalize_one_return(irg);
+ remove_critical_cf_edges(irg);
+
+ dump_ir_block_graph(irg, "_01_normal");
+
+ compute_cdep(irg);
+ assure_doms(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, NULL);
+
+ local_optimize_graph(irg);
+ dump_ir_block_graph(irg, "_02_ifconv");
+
+ irg_walk_graph(irg, NULL, optimise_psis, NULL);
+
+ dump_ir_block_graph(irg, "_03_postifconv");
+
+ obstack_free(&obst, NULL);
+
+ free_dom(irg);
+ free_cdep(irg);
+}
+
+#else
+
/**
* @file ifconv.c
* If conversion.
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
-#endif
#include "irgraph_t.h"
#include "irnode_t.h"
+#include "irgwalk.h"
#include "iropt_t.h"
#include "irgmod.h"
#include "irmode_t.h"
#include "ircons_t.h"
#include "irdom_t.h"
+#include "irgwalk.h"
#include "ifconv.h"
#include "irflag_t.h"
return mux;
res = mux;
- null = get_tarval_null(mode);
+ null = get_mode_null(mode);
minus_one = tarval_sub(null, get_tarval_one(mode));
ops[0] = get_Mux_false(mux);
};
/** The debugging module. */
-static firm_dbg_module_t *dbg;
+DEBUG_ONLY(static firm_dbg_module_t *dbg;)
/**
* A simple check for side effects upto an opcode of a ir node.
}
end:
- DBG((dbg, LEVEL_3, "\t\t\t%Dcan move to %n: %d\n", depth, expr, res));
+ DBG((dbg, LEVEL_3, "\t\t\tcan move to %n: %d\n", expr, res));
return res;
}
list_del_init(&masked_by->list);
}
- DBG((dbg, LEVEL_2, "%D%n (%s branch) "
+ DBG((dbg, LEVEL_2, "%n (%s branch) "
"for pos %d in block %n reached by %n\n",
- depth, cond, proj ? "true" : "false", pos,
+ cond, proj ? "true" : "false", pos,
block, masked_by ? masked_by->cond : NULL));
}
}
/*
* Add this cond info to the list of all cond infos
- * in this graph. This is just done to free the
+ * in this graph. This is just done to xfree the
* set easier afterwards (we save an irg_walk_graph).
*/
list_add(&cwi->cond_info_head, &ci->list);
INIT_LIST_HEAD(&cwi.phi_head);
/* Init the debug stuff. */
- dbg = firm_dbg_register("firm.opt.ifconv");
+ FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
#if 0
- firm_dbg_set_mask(dbg, LEVEL_1);
+ firm_dbg_set_mask(dbg, LEVEL_1|LEVEL_2|LEVEL_3);
#endif
/* if-conversion works better with normalized returns */
normalize_one_return(irg);
/* Ensure, that the dominators are computed. */
- compute_doms(irg);
+ assure_doms(irg);
DBG((dbg, LEVEL_1, "if conversion for irg %s(%p)\n",
get_entity_name(get_irg_entity(irg)), irg));
DBG((dbg, LEVEL_1, "muxes made: %d\n", muxes_made));
obstack_free(&obst, NULL);
+
+ dump_ir_block_graph(irg, "_ifconv_hack");
}
+
+#endif