X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fifconv.c;h=ff4764f1ed363d57b39c98fcb1b36a58d5351743;hb=9b2a2164abf78dad99dc2b0371c9e6a728d333b0;hp=3ef3cb1ad06d263b6badac6120a07a2192290102;hpb=67819c6a042ffb2f914455a9c943851fd8556a36;p=libfirm diff --git a/ir/opt/ifconv.c b/ir/opt/ifconv.c index 3ef3cb1ad..ff4764f1e 100644 --- a/ir/opt/ifconv.c +++ b/ir/opt/ifconv.c @@ -9,6 +9,635 @@ * 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 +#endif +#ifdef HAVE_ALLOCA_H +#include +#endif + +#include + +#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); + + +/** + * 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; +} + + +static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency) +{ + int arity; + int i; + + /* No need to find the conditional block if this block cannot be emptied and + * therefore not moved + */ + if (!can_empty_block(start)) return NULL; + + arity = get_irn_arity(start); + for (i = 0; i < arity; ++i) { + ir_node* pred = get_irn_n(start, i); + ir_node* pred_block = get_nodes_block(pred); + + if (pred_block == dependency) { + assert(is_Proj(pred)); + assert(get_irn_mode(pred) == mode_X); + return pred; + } + + if (is_Proj(pred)) { + assert(get_irn_mode(pred) == mode_X); + return NULL; + } + + if (is_cdep_on(pred_block, dependency)) { + return walk_to_projx(pred_block, dependency); + } + } + return NULL; +} + + +/** + * 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 = exact_copy(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; +} + + +/** + * 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); +} + + +/** + * Remove the jth predecessors from the ith predecessor of block and add it to block + */ +static void split_block(ir_node* block, int i, int j) +{ + ir_node* pred_block = get_nodes_block(get_irn_n(block, i)); + int arity = get_irn_arity(block); + int new_pred_arity; + ir_node* phi; + ir_node **ins; + ir_node **pred_ins; + int k; + + DB((dbg, LEVEL_1, "Splitting predecessor %d of predecessor %d of %+F\n", j, i, block)); + + NEW_ARR_A(ir_node*, ins, arity + 1); + + for (phi = get_block_blockinfo(block)->phi; phi != NULL; phi = get_irn_link(phi)) { + ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j); + + for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k); + ins[k++] = copy; + for (; k < arity; ++k) ins[k] = get_irn_n(phi, k); + ins[k] = get_irn_n(phi, i); + assert(k == arity); + set_irn_in(phi, arity + 1, ins); + } + + for (k = 0; k < i; ++k) ins[k] = get_irn_n(block, k); + ins[k++] = get_irn_n(pred_block, j); + for (; k < arity; ++k) ins[k] = get_irn_n(block, k); + ins[k] = get_irn_n(block, i); + assert(k == arity); + set_irn_in(block, arity + 1, ins); + + new_pred_arity = get_irn_arity(pred_block) - 1; + NEW_ARR_A(ir_node*, pred_ins, new_pred_arity); + + for (phi = get_block_blockinfo(pred_block)->phi; phi != NULL; phi = get_irn_link(phi)) { + for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(phi, k); + for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1); + assert(k == new_pred_arity); + if (new_pred_arity > 1) { + set_irn_in(phi, new_pred_arity, pred_ins); + } else { + exchange(phi, pred_ins[0]); + } + } + + for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(pred_block, k); + for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(pred_block, k + 1); + assert(k == new_pred_arity); + if (new_pred_arity > 1) { + set_irn_in(pred_block, new_pred_arity, pred_ins); + } else { + exchange(pred_block, get_nodes_block(pred_ins[0])); + } +} + + +static void prepare_path(ir_node* block, int i, const ir_node* dependency) +{ + ir_node* pred = get_nodes_block(get_irn_n(block, i)); + int pred_arity; + int j; + + DB((dbg, LEVEL_1, "Preparing predecessor %d of %+F\n", i, block)); + + pred_arity = get_irn_arity(pred); + for (j = 0; j < pred_arity; ++j) { + ir_node* pred_pred = get_nodes_block(get_irn_n(pred, j)); + + if (is_cdep_on(pred_pred, dependency)) { + prepare_path(pred, j, dependency); + split_block(block, i, j); + break; + } + } +} + + +static void if_conv_walker(ir_node* block, void* env) +{ + 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) { + ir_node* pred; + cdep* cdep; + + pred = get_nodes_block(get_irn_n(block, i)); + for (cdep = find_cdep(pred); cdep != NULL; cdep = cdep->next) { + const ir_node* dependency = cdep->node; + ir_node* projx0 = walk_to_projx(pred, dependency); + ir_node* cond; + int j; + + if (projx0 == NULL) continue; + + cond = get_Proj_pred(projx0); + if (get_irn_op(cond) != op_Cond) continue; + /* We only handle boolean decisions, no switches */ + if (get_irn_mode(get_Cond_selector(cond)) != mode_b) continue; + + for (j = i + 1; j < arity; ++j) { + ir_node* projx1; + ir_node* conds[0]; + ir_node* vals[2]; + ir_node* psi; + ir_node* psi_block; + ir_node* phi; + + pred = get_nodes_block(get_irn_n(block, j)); + + if (!is_cdep_on(pred, dependency)) continue; + + projx1 = walk_to_projx(pred, dependency); + + if (projx1 == NULL) continue; + + DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n", + cond, projx0, projx1 + )); + + prepare_path(block, i, dependency); + prepare_path(block, j, dependency); + arity = get_irn_arity(block); + + 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) { +#if 1 + DB((dbg, LEVEL_1, "Welding block %+F and %+F\n", block, psi_block)); + get_block_blockinfo(block)->has_pinned |= get_block_blockinfo(psi_block)->has_pinned; + set_irn_in(block, get_irn_arity(psi_block), get_irn_in(psi_block) + 1); + exchange_cdep(psi_block, block); + exchange(psi_block, block); +#else + 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); +#endif + 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); + + dump_ir_block_graph(irg, "_02_ifconv"); + local_optimize_graph(irg); + dump_ir_block_graph(irg, "_03_postopt"); + + irg_walk_graph(irg, NULL, optimise_psis, NULL); + + dump_ir_block_graph(irg, "_04_postifconv"); + + obstack_free(&obst, NULL); + + free_dom(irg); + free_cdep(irg); +} + +#else + /** * @file ifconv.c * If conversion. @@ -29,17 +658,16 @@ #ifdef HAVE_ALLOCA_H #include #endif -#ifdef HAVE_MALLOC_H -#include -#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" @@ -93,7 +721,7 @@ static ir_node *optimize_mux_chain(ir_node *mux) 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); @@ -180,7 +808,7 @@ static const opt_if_conv_info_t default_info = { }; /** 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. @@ -289,7 +917,7 @@ static int _can_move_to(ir_node *expr, ir_node *dest_block, int depth, const opt } 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; } @@ -552,9 +1180,9 @@ static void _find_conds(ir_node *irn, unsigned long visited_nr, 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)); } } @@ -838,7 +1466,7 @@ static void annotate_cond_info_post(ir_node *irn, void *data) /* * 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); @@ -953,16 +1581,16 @@ void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params) 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)); @@ -993,4 +1621,8 @@ void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params) DBG((dbg, LEVEL_1, "muxes made: %d\n", muxes_made)); obstack_free(&obst, NULL); + + dump_ir_block_graph(irg, "_ifconv_hack"); } + +#endif