X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=sidebyside;f=ir%2Fbe%2Fbeirgmod.c;h=0cfb3f46cea75c03af69a64223d2f7206c7ee362;hb=bd019d8cd785c1a433c19dee44bf3f9e82397286;hp=e692c5bd684b82355ce7f2f5e29738f0f6da3918;hpb=205396c4f4f5abe7abd6dc2350c8c398a7623afc;p=libfirm diff --git a/ir/be/beirgmod.c b/ir/be/beirgmod.c index e692c5bd6..0cfb3f46c 100644 --- a/ir/be/beirgmod.c +++ b/ir/be/beirgmod.c @@ -1,20 +1,34 @@ +/* + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ + /** * @file - * @brief + * @brief Backend IRG modification routines. + * @author Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig + * @date 04.05.2005 + * * This file contains the following IRG modifications for be routines: * - insertion of Perm nodes * - empty block elimination * - a simple dead node elimination (set inputs of unreachable nodes to BAD) - * - * @author Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig - * @date 04.05.2005 - * @version $Id$ - * Copyright: (c) Universitaet Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include @@ -36,20 +50,22 @@ #include "iredges_t.h" #include "irgraph_t.h" #include "irgopt.h" +#include "irgmod.h" #include "irprintf_t.h" #include "irgwalk.h" #include "be_t.h" #include "bechordal_t.h" -#include "bearch_t.h" -#include "besched_t.h" +#include "bearch.h" +#include "besched.h" #include "belive_t.h" -#include "benode_t.h" +#include "benode.h" #include "beutil.h" #include "beinsn_t.h" #include "bessaconstr.h" - +#include "beirg.h" #include "beirgmod.h" +#include "bemodule.h" DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) @@ -62,57 +78,53 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) */ -ir_node *insert_Perm_after(be_irg_t *birg, - const arch_register_class_t *cls, +ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls, ir_node *pos) { - const arch_env_t *arch_env = birg->main_env->arch_env; - be_lv_t *lv = birg->lv; + be_lv_t *lv = be_get_irg_liveness(irg); ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos); - ir_graph *irg = get_irn_irg(bl); - pset *live = pset_new_ptr_default(); + ir_nodeset_t live; + ir_nodeset_iterator_t iter; - ir_node *curr, *irn, *perm, **nodes; - int i, n; + ir_node *irn, *perm, **nodes; + size_t i, n; DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos)); - be_liveness_nodes_live_at(lv, arch_env, cls, pos, live); + ir_nodeset_init(&live); + be_liveness_nodes_live_at(lv, cls, pos, &live); - n = pset_count(live); - - if(n == 0) { - del_pset(live); + n = ir_nodeset_size(&live); + if (n == 0) { + ir_nodeset_destroy(&live); return NULL; } - nodes = xmalloc(n * sizeof(nodes[0])); + nodes = XMALLOCN(ir_node*, n); DBG((dbg, LEVEL_1, "live:\n")); - for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) { + i = 0; + foreach_ir_nodeset(&live, irn, iter) { DBG((dbg, LEVEL_1, "\t%+F\n", irn)); nodes[i] = irn; + i++; } - del_pset(live); + ir_nodeset_destroy(&live); - perm = be_new_Perm(cls, irg, bl, n, nodes); + perm = be_new_Perm(cls, bl, n, nodes); sched_add_after(pos, perm); free(nodes); - curr = perm; for (i = 0; i < n; ++i) { ir_node *perm_op = get_irn_n(perm, i); - const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op); + const arch_register_t *reg = arch_get_irn_register(perm_op); be_ssa_construction_env_t senv; ir_mode *mode = get_irn_mode(perm_op); - ir_node *proj = new_r_Proj(irg, bl, perm, mode, i); - arch_set_irn_register(arch_env, proj, reg); - - sched_add_after(curr, proj); - curr = proj; + ir_node *proj = new_r_Proj(perm, mode, i); + arch_set_irn_register(proj, reg); - be_ssa_construction_init(&senv, birg); + be_ssa_construction_init(&senv, irg); be_ssa_construction_add_copy(&senv, perm_op); be_ssa_construction_add_copy(&senv, proj); be_ssa_construction_fix_users(&senv, perm_op); @@ -125,64 +137,208 @@ ir_node *insert_Perm_after(be_irg_t *birg, return perm; } +static int blocks_removed; + /** * Post-block-walker: Find blocks containing only one jump and * remove them. */ -static void remove_empty_block(ir_node *block, void *data) { +static void remove_empty_block(ir_node *block) +{ const ir_edge_t *edge, *next; + int i, arity; ir_node *node; - int *changed = data; + ir_node *pred; + ir_node *succ_block; ir_node *jump = NULL; + ir_graph *irg = get_irn_irg(block); - assert(is_Block(block)); + if (irn_visited_else_mark(block)) + return; if (get_Block_n_cfgpreds(block) != 1) - return; + goto check_preds; sched_foreach(block, node) { - if (! is_Jmp(node)) - return; + if (! is_Jmp(node) + && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump)) + goto check_preds; if (jump != NULL) { /* we should never have 2 jumps in a block */ - panic("We should never have 2 jumps in a block"); + panic("found 2 jumps in a block"); } jump = node; } if (jump == NULL) - return; + goto check_preds; - node = get_Block_cfgpred(block, 0); + pred = get_Block_cfgpred(block, 0); + succ_block = NULL; foreach_out_edge_safe(jump, edge, next) { - ir_node *block = get_edge_src_irn(edge); - int pos = get_edge_src_pos(edge); + int pos = get_edge_src_pos(edge); + + assert(succ_block == NULL); + succ_block = get_edge_src_irn(edge); + if (get_Block_entity(succ_block) != NULL + && get_Block_entity(block) != NULL) { + /* + * Currently we can add only one label for a block. + * Therefore we cannot combine them if both block already have one. + */ + goto check_preds; + } + + set_irn_n(succ_block, pos, pred); + } - set_irn_n(block, pos, node); + if (get_Block_entity(block) != NULL) { + /* move the label to the successor block */ + ir_entity *entity = get_Block_entity(block); + set_Block_entity(succ_block, entity); + } + + /* there can be some non-scheduled Pin nodes left in the block, move them + * to the succ block (Pin) or pred block (Sync) */ + foreach_out_edge_safe(block, edge, next) { + node = get_edge_src_irn(edge); + + if (node == jump) + continue; + /* we simply kill Pins, because there are some strange interactions + * between jump threading, which produce PhiMs with Pins, we simply + * kill the pins here, everything is scheduled anyway */ + if (is_Pin(node)) { + exchange(node, get_Pin_op(node)); + continue; + } + if (is_Sync(node)) { + set_nodes_block(node, get_nodes_block(pred)); + continue; + } + if (is_End(node)) { /* End-keep, reroute it to the successor */ + int pos = get_edge_src_pos(edge); + set_irn_n(node, pos, succ_block); + continue; + } + panic("Unexpected node %+F in block %+F with empty schedule", node, block); } - set_Block_cfgpred(block, 0, new_Bad()); - be_kill_node(jump); - *changed = 1; + set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X)); + kill_node(jump); + blocks_removed = 1; + + /* check predecessor */ + remove_empty_block(get_nodes_block(pred)); + return; + +check_preds: + arity = get_Block_n_cfgpreds(block); + for (i = 0; i < arity; ++i) { + ir_node *pred = get_Block_cfgpred_block(block, i); + remove_empty_block(pred); + } } /* removes basic blocks that just contain a jump instruction */ -int be_remove_empty_blocks(ir_graph *irg) { - int changed = 0; +int be_remove_empty_blocks(ir_graph *irg) +{ + ir_node *end; + int i, arity; + + blocks_removed = 0; + + ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED); + inc_irg_visited(irg); + remove_empty_block(get_irg_end_block(irg)); + end = get_irg_end(irg); + arity = get_irn_arity(end); + for (i = 0; i < arity; ++i) { + ir_node *pred = get_irn_n(end, i); + if (!is_Block(pred)) + continue; + remove_empty_block(pred); + } + ir_free_resources(irg, IR_RESOURCE_IRN_VISITED); - irg_block_walk_graph(irg, remove_empty_block, NULL, &changed); - if (changed) { + if (blocks_removed) { /* invalidate analysis info */ - set_irg_doms_inconsistent(irg); - set_irg_extblk_inconsistent(irg); - set_irg_outs_inconsistent(irg); + clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE + | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS); } - return changed; + return blocks_removed; +} + +//--------------------------------------------------------------------------- + +typedef struct remove_dead_nodes_env_t_ { + bitset_t *reachable; + ir_graph *irg; + be_lv_t *lv; +} remove_dead_nodes_env_t; + +/** + * Post-walker: remember all visited nodes in a bitset. + */ +static void mark_dead_nodes_walker(ir_node *node, void *data) +{ + remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data; + bitset_set(env->reachable, get_irn_idx(node)); } +/** + * Post-block-walker: + * Walk through the schedule of every block and remove all dead nodes from it. + */ +static void remove_dead_nodes_walker(ir_node *block, void *data) +{ + remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data; + ir_node *node, *next; + + for (node = sched_first(block); ! sched_is_end(node); node = next) { + /* get next node now, as after calling sched_remove it will be invalid */ + next = sched_next(node); + + if (bitset_is_set(env->reachable, get_irn_idx(node))) + continue; + + if (env->lv != NULL) + be_liveness_remove(env->lv, node); + sched_remove(node); + + /* kill projs */ + if (get_irn_mode(node) == mode_T) { + const ir_edge_t *edge; + const ir_edge_t *next_edge; + foreach_out_edge_safe(node, edge, next_edge) { + ir_node *proj = get_edge_src_irn(edge); + if (!is_Proj(proj)) + continue; + if (env->lv != NULL) + be_liveness_remove(env->lv, proj); + kill_node(proj); + } + } + kill_node(node); + } +} + +void be_remove_dead_nodes_from_schedule(ir_graph *irg) +{ + remove_dead_nodes_env_t env; + env.reachable = bitset_alloca(get_irg_last_idx(irg)); + env.lv = be_get_irg_liveness(irg); + env.irg = irg; + + // mark all reachable nodes + irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env); + + // walk schedule and remove non-marked nodes + irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env); +} + +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod) void be_init_irgmod(void) { FIRM_DBG_REGISTER(dbg, "firm.be.irgmod"); } - -BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);