X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbepeephole.c;h=ae296f48d5ca715ae3a2dad71ecb19e9b2215222;hb=c6571686bfbfb3c87ae24ae1dc568e685d6cd49a;hp=5d3065b0f89e5213d6774f0779763d5554d2e60d;hpb=4ebf91ab4349975628962fcae404cdf0c340f49e;p=libfirm diff --git a/ir/be/bepeephole.c b/ir/be/bepeephole.c index 5d3065b0f..ae296f48d 100644 --- a/ir/be/bepeephole.c +++ b/ir/be/bepeephole.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -21,10 +21,10 @@ * @file * @brief Peephole optimisation framework keeps track of which registers contain which values * @author Matthias Braun - * @version $Id$ */ #include "config.h" +#include "array_t.h" #include "bepeephole.h" #include "iredges_t.h" @@ -32,11 +32,13 @@ #include "irprintf.h" #include "ircons.h" #include "irgmod.h" +#include "heights.h" #include "error.h" #include "beirg.h" #include "belive_t.h" #include "bearch.h" +#include "beintlive_t.h" #include "benode.h" #include "besched.h" #include "bemodule.h" @@ -46,39 +48,32 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) static const arch_env_t *arch_env; static be_lv_t *lv; static ir_node *current_node; -ir_node ***register_values; +ir_node **register_values; static void clear_reg_value(ir_node *node) { - const arch_register_t *reg; - const arch_register_class_t *cls; - unsigned reg_idx; - unsigned cls_idx; + const arch_register_t *reg; + unsigned reg_idx; if (!mode_is_data(get_irn_mode(node))) return; - reg = arch_get_irn_register(node); + reg = arch_get_irn_register(node); if (reg == NULL) { panic("No register assigned at %+F", node); } - if (arch_register_type_is(reg, virtual)) + if (reg->type & arch_register_type_virtual) return; - cls = arch_register_get_class(reg); - reg_idx = arch_register_get_index(reg); - cls_idx = arch_register_class_index(cls); + reg_idx = reg->global_index; - //assert(register_values[cls_idx][reg_idx] != NULL); DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name)); - register_values[cls_idx][reg_idx] = NULL; + register_values[reg_idx] = NULL; } static void set_reg_value(ir_node *node) { - const arch_register_t *reg; - const arch_register_class_t *cls; - unsigned reg_idx; - unsigned cls_idx; + const arch_register_t *reg; + unsigned reg_idx; if (!mode_is_data(get_irn_mode(node))) return; @@ -87,14 +82,12 @@ static void set_reg_value(ir_node *node) if (reg == NULL) { panic("No register assigned at %+F", node); } - if (arch_register_type_is(reg, virtual)) + if (reg->type & arch_register_type_virtual) return; - cls = arch_register_get_class(reg); - reg_idx = arch_register_get_index(reg); - cls_idx = arch_register_class_index(cls); + reg_idx = reg->global_index; DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node)); - register_values[cls_idx][reg_idx] = node; + register_values[reg_idx] = node; } static void clear_defs(ir_node *node) @@ -131,26 +124,34 @@ void be_peephole_new_node(ir_node * nw) /** * must be called from peephole optimisations before a node will be killed * and its users will be redirected to new_node. - * so bepeephole can update it's internal state. + * so bepeephole can update its internal state. * - * Note: killing a node and rewiring os only allowed if new_node produces + * Note: killing a node and rewiring is only allowed if new_node produces * the same registers as old_node. */ static void be_peephole_before_exchange(const ir_node *old_node, ir_node *new_node) { - const arch_register_t *reg; - const arch_register_class_t *cls; - unsigned reg_idx; - unsigned cls_idx; + const arch_register_t *reg; + unsigned reg_idx; + bool old_is_current = false; DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node)); + assert(sched_is_scheduled(skip_Proj_const(old_node))); + assert(sched_is_scheduled(skip_Proj(new_node))); + if (current_node == old_node) { + old_is_current = true; + /* next node to be processed will be killed. Its scheduling predecessor * must be processed next. */ current_node = sched_next(current_node); assert (!is_Bad(current_node)); + + /* we can't handle liveness updates correctly when exchange current node + * with something behind it */ + assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node))); } if (!mode_is_data(get_irn_mode(old_node))) @@ -163,12 +164,9 @@ static void be_peephole_before_exchange(const ir_node *old_node, assert(reg == arch_get_irn_register(new_node) && "KILLING a node and replacing by different register is not allowed"); - cls = arch_register_get_class(reg); - reg_idx = arch_register_get_index(reg); - cls_idx = arch_register_class_index(cls); - - if (register_values[cls_idx][reg_idx] == old_node) { - register_values[cls_idx][reg_idx] = new_node; + reg_idx = reg->global_index; + if (register_values[reg_idx] == old_node || old_is_current) { + register_values[reg_idx] = new_node; } be_liveness_remove(lv, old_node); @@ -187,20 +185,13 @@ void be_peephole_exchange(ir_node *old, ir_node *nw) */ static void process_block(ir_node *block, void *data) { - unsigned n_classes; - unsigned i; int l; (void) data; /* construct initial register assignment */ - n_classes = arch_env_get_n_reg_class(arch_env); - for (i = 0; i < n_classes; ++i) { - const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i); - unsigned n_regs = arch_register_class_n_regs(cls); - memset(register_values[i], 0, sizeof(ir_node*) * n_regs); - } + memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers); - assert(lv->nodes && "live sets must be computed"); + assert(lv->sets_valid && "live sets must be computed"); DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block)); be_lv_foreach(lv, block, be_lv_state_end, l) { ir_node *node = be_lv_get_irn(lv, block, l); @@ -233,136 +224,74 @@ static void process_block(ir_node *block, void *data) } } -static void kill_node_and_preds(ir_node *node) +/** + * Check whether the node has only one user. Explicitly ignore the anchor. + */ +bool be_has_only_one_user(ir_node *node) { - int arity, i; + int n = get_irn_n_edges(node); + int n_users; + const ir_edge_t *edge; - arity = get_irn_arity(node); - for (i = 0; i < arity; ++i) { - ir_node *pred = get_irn_n(node, i); + if (n <= 1) + return 1; - set_irn_n(node, i, new_Bad()); - if (get_irn_n_edges(pred) != 0) + n_users = 0; + foreach_out_edge(node, edge) { + ir_node *src = get_edge_src_irn(edge); + /* ignore anchor and keep-alive edges */ + if (is_Anchor(src) || is_End(src)) continue; - - kill_node_and_preds(pred); + n_users++; } - if (!is_Proj(node)) - sched_remove(node); - kill_node(node); + return n_users == 1; } -/** - * Walk through the block schedule and skip all barrier nodes. - */ -static void skip_barrier(ir_node *block, ir_graph *irg) +bool be_can_move_before(ir_heights_t *heights, const ir_node *node, + const ir_node *before) { - ir_node *irn; - - sched_foreach_reverse(block, irn) { - int arity; - unsigned *used; - unsigned n_used; - const ir_edge_t *edge, *next; - - if (!be_is_Barrier(irn)) - continue; - - /* track which outputs are actually used, as we have to create - * keep nodes for unused outputs */ - arity = get_irn_arity(irn); - rbitset_alloca(used, arity); - - foreach_out_edge_safe(irn, edge, next) { - ir_node *proj = get_edge_src_irn(edge); - int pn; - ir_node *pred; - - if (is_Anchor(proj)) + int node_arity = get_irn_arity(node); + ir_node *schedpoint = sched_next(node); + + while (schedpoint != before) { + int i; + unsigned n_outs = arch_get_irn_n_outs(schedpoint); + + /* the node must not use our computed values */ + if (heights_reachable_in_block(heights, schedpoint, node)) + return false; + + /* the node must not overwrite registers of our inputs */ + for (i = 0; i < node_arity; ++i) { + ir_node *in = get_irn_n(node, i); + const arch_register_t *reg = arch_get_irn_register(in); + const arch_register_req_t *in_req + = arch_get_irn_register_req_in(node, i); + unsigned o; + if (reg == NULL) continue; - - pn = (int) get_Proj_proj(proj); - pred = get_irn_n(irn, pn); - - rbitset_set(used, pn); - - edges_reroute_kind(proj, pred, EDGE_KIND_NORMAL, irg); - edges_reroute_kind(proj, pred, EDGE_KIND_DEP, irg); - } - - /* the barrier also had the effect of a Keep for unused inputs. - * we now have to create an explicit Keep for them */ - n_used = rbitset_popcount(used, arity); - if (n_used < (unsigned) arity) { - int n_in = arity - (int) n_used; - ir_node **in = ALLOCAN(ir_node*, n_in); - int i = 0; - int n = 0; - ir_node *keep; - - for (i = 0; i < arity; ++i) { - if (rbitset_is_set(used, i)) + for (o = 0; o < n_outs; ++o) { + const arch_register_t *outreg + = arch_get_irn_register_out(schedpoint, o); + const arch_register_req_t *outreq + = arch_get_irn_register_req_out(schedpoint, o); + if (outreg == NULL) continue; - assert(n < n_in); - in[n++] = get_irn_n(irn, i); + if (outreg->global_index >= reg->global_index + && outreg->global_index + < (unsigned)reg->global_index + in_req->width) + return false; + if (reg->global_index >= outreg->global_index + && reg->global_index + < (unsigned)outreg->global_index + outreq->width) + return false; } - keep = be_new_Barrier(get_nodes_block(irn), n_in, in); - keep_alive(keep); - sched_add_before(irn, keep); } - kill_node_and_preds(irn); - break; - } -} - -/** - * Kill the Barrier nodes for better peephole optimization. - */ -static void kill_barriers(ir_graph *irg) -{ - ir_node *end_blk = get_irg_end_block(irg); - ir_node *start_blk = get_irg_start_block(irg); - int i; - - /* skip the barrier on all return blocks */ - for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) { - ir_node *be_ret = get_Block_cfgpred(end_blk, i); - ir_node *ret_blk = get_nodes_block(be_ret); - - if (ret_blk == start_blk) - continue; - - skip_barrier(ret_blk, irg); - } - - /* skip the barrier on the start block */ - start_blk = get_irg_start_block(irg); - skip_barrier(start_blk, irg); -} - -/** - * Check whether the node has only one user. Explicitly ignore the anchor. - */ -static int has_only_one_user(ir_node *node) -{ - int n = get_irn_n_edges(node); - const ir_edge_t *edge; - - if (n <= 1) - return 1; - - if (n > 2) - return 0; - - foreach_out_edge(node, edge) { - ir_node *src = get_edge_src_irn(edge); - if (is_Anchor(src)) - return 1; + schedpoint = sched_next(schedpoint); } - - return 0; + return true; } /* @@ -379,28 +308,12 @@ ir_node *be_peephole_IncSP_IncSP(ir_node *node) if (!be_is_IncSP(pred)) return node; - if (!has_only_one_user(pred)) + if (!be_has_only_one_user(pred)) return node; pred_offs = be_get_IncSP_offset(pred); curr_offs = be_get_IncSP_offset(node); - - if (pred_offs == BE_STACK_FRAME_SIZE_EXPAND) { - if (curr_offs != BE_STACK_FRAME_SIZE_SHRINK) { - return node; - } - offs = 0; - } else if (pred_offs == BE_STACK_FRAME_SIZE_SHRINK) { - if (curr_offs != BE_STACK_FRAME_SIZE_EXPAND) { - return node; - } - offs = 0; - } else if (curr_offs == BE_STACK_FRAME_SIZE_EXPAND || - curr_offs == BE_STACK_FRAME_SIZE_SHRINK) { - return node; - } else { - offs = curr_offs + pred_offs; - } + offs = curr_offs + pred_offs; /* add node offset to pred and remove our IncSP */ be_set_IncSP_offset(pred, offs); @@ -409,41 +322,26 @@ ir_node *be_peephole_IncSP_IncSP(ir_node *node) return pred; } -void be_peephole_opt(be_irg_t *birg) +void be_peephole_opt(ir_graph *irg) { - ir_graph *irg = be_get_birg_irg(birg); - unsigned n_classes; - unsigned i; - - /* barrier nodes are used for register allocations. They hinders - * peephole optimizations, so remove them here. */ - kill_barriers(irg); - +#if 0 /* we sometimes find BadE nodes in float apps like optest_float.c or * kahansum.c for example... */ - be_liveness_invalidate(birg->lv); - be_liveness_assure_sets(be_assure_liveness(birg)); - - arch_env = be_get_birg_arch_env(birg); - lv = be_get_birg_liveness(birg); - - n_classes = arch_env_get_n_reg_class(arch_env); - register_values = XMALLOCN(ir_node**, n_classes); - for (i = 0; i < n_classes; ++i) { - const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i); - unsigned n_regs = arch_register_class_n_regs(cls); - register_values[i] = XMALLOCN(ir_node*, n_regs); - } + be_invalidate_live_sets(irg); +#endif + be_assure_live_sets(irg); + + arch_env = be_get_irg_arch_env(irg); + lv = be_get_irg_liveness(irg); + + register_values = XMALLOCN(ir_node*, arch_env->n_registers); irg_block_walk_graph(irg, process_block, NULL, NULL); - for (i = 0; i < n_classes; ++i) { - xfree(register_values[i]); - } xfree(register_values); } -BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole); +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole) void be_init_peephole(void) { FIRM_DBG_REGISTER(dbg, "firm.be.peephole");