X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbeirgmod.c;h=bf6506c110ac6e0a4329d9b31b3bc5ffa3fa46b4;hb=82eea1b6b7887bdc834affa49038734e42012395;hp=1fbef3d922a6f08690435171666dd88c4d5969b0;hpb=87965f5c94d561cb15233b80711123f6edc80b30;p=libfirm diff --git a/ir/be/beirgmod.c b/ir/be/beirgmod.c index 1fbef3d92..bf6506c11 100644 --- a/ir/be/beirgmod.c +++ b/ir/be/beirgmod.c @@ -1,32 +1,37 @@ +/* + * 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: - * - backend dominance information - * - SSA construction for a set of nodes * - 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 -#endif +#include "config.h" #include -#ifdef HAVE_MALLOC_H - #include -#endif -#ifdef HAVE_ALLOCA_H - #include -#endif - #include "hashptr.h" #include "pdeq.h" #include "pset.h" @@ -34,6 +39,7 @@ #include "util.h" #include "debug.h" #include "error.h" +#include "xmalloc.h" #include "irflag_t.h" #include "ircons_t.h" @@ -44,578 +50,299 @@ #include "iredges_t.h" #include "irgraph_t.h" #include "irgopt.h" -#include "irprintf_t.h" +#include "irgmod.h" +#include "irprintf.h" #include "irgwalk.h" #include "be_t.h" #include "bechordal_t.h" #include "bearch.h" -#include "besched_t.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 *dbgssa = NULL;) DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) +static int cmp_node_nr(const void *a, const void *b) +{ + ir_node **p1 = (ir_node**)a; + ir_node **p2 = (ir_node**)b; + long n1 = get_irn_node_nr(*p1); + long n2 = get_irn_node_nr(*p2); + return (n1>n2) - (n1 1); + ir_nodeset_init(&live); + be_liveness_nodes_live_before(lv, cls, pos, &live); - for(i = 0; i < n_preds; ++i) { - ins[i] = new_r_Unknown(irg, env->mode); - } - phi = new_r_Phi(irg, block, n_preds, ins, env->mode); - if(env->new_phis != NULL) { - ARR_APP1(ir_node*, env->new_phis, phi); + n = ir_nodeset_size(&live); + if (n == 0) { + ir_nodeset_destroy(&live); + return NULL; } - if(env->mode != mode_M) { - sched_add_after(block, phi); + nodes = XMALLOCN(ir_node*, n); + + DBG((dbg, LEVEL_1, "live:\n")); + i = 0; + foreach_ir_nodeset(&live, irn, iter) { + DBG((dbg, LEVEL_1, "\t%+F\n", irn)); + nodes[i] = irn; + i++; } + ir_nodeset_destroy(&live); + /* make the input order deterministic */ + qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr); - DBG((dbgssa, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block)); - set_irn_link(link_with, phi); - mark_irn_visited(block); + ir_node *const bl = get_nodes_block(pos); + perm = be_new_Perm(cls, bl, n, nodes); + sched_add_before(pos, perm); + free(nodes); - for(i = 0; i < n_preds; ++i) { - ir_node *pred_block = get_Block_cfgpred_block(block, i); - ir_node *pred_def = search_def_end_of_block(env, pred_block); + for (i = 0; i < n; ++i) { + ir_node *perm_op = get_irn_n(perm, i); + be_ssa_construction_env_t senv; - set_irn_n(phi, i, pred_def); + ir_mode *mode = get_irn_mode(perm_op); + ir_node *proj = new_r_Proj(perm, mode, i); + + 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); + be_ssa_construction_update_liveness_phis(&senv, lv); + be_liveness_update(lv, perm_op); + be_liveness_update(lv, proj); + be_ssa_construction_destroy(&senv); } - return phi; -} - -static -ir_node *get_def_at_idom(ssa_constr_env_t *env, ir_node *block) -{ - ir_node *dom = get_Block_idom(block); - ir_node *def = search_def_end_of_block(env, dom); - - return def; + return perm; } -static -ir_node *search_def_end_of_block(ssa_constr_env_t *env, ir_node *block) -{ - if(irn_visited(block)) { - assert(get_irn_link(block) != NULL); - return get_irn_link(block); - } else if(Block_block_visited(block)) { - return create_phi(env, block, block); - } else { - ir_node *def = get_def_at_idom(env, block); -#if 1 - mark_irn_visited(block); - set_irn_link(block, def); -#endif - - return def; - } -} +static int blocks_removed; -static -ir_node *search_def(ssa_constr_env_t *env, ir_node *at) +/** + * Post-block-walker: Find blocks containing only one jump and + * remove them. + */ +static void remove_empty_block(ir_node *block) { - ir_node *block = get_nodes_block(at); - ir_node *node; - ir_node *def; - - DBG((dbgssa, LEVEL_3, "\t...searching def at %+F\n", at)); + int i; + int arity; + ir_node *pred; + ir_node *succ_block; + ir_node *jump = NULL; + ir_graph *irg = get_irn_irg(block); + ir_entity *entity; + + if (irn_visited_else_mark(block)) + return; - /* no defs in the current block we can do the normal searching */ - if(!irn_visited(block) && !Block_block_visited(block)) { - DBG((dbgssa, LEVEL_3, "\t...continue at idom\n")); - return get_def_at_idom(env, block); - } + if (get_Block_n_cfgpreds(block) != 1) + goto check_preds; - /* there are defs in the current block, walk the linked list to find - the one immediately dominating us - */ - node = block; - def = get_irn_link(node); - while(def != NULL) { -#if 0 - assert(get_nodes_block(def) == block); - if(!value_dominates_intrablock(at, def)) { - DBG((dbgssa, LEVEL_3, "\t...found dominating def %+F\n", def)); - return def; - } -#else - if(!value_dominates(at, def)) { - DBG((dbgssa, LEVEL_3, "\t...found dominating def %+F\n", def)); - return def; + sched_foreach(block, node) { + 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("found 2 jumps in a block"); } -#endif - node = def; - def = get_irn_link(node); - } - - /* block in dominance frontier? create a phi then */ - if(Block_block_visited(block)) { - DBG((dbgssa, LEVEL_3, "\t...create phi at block %+F\n", block)); - assert(!is_Phi(node)); - return create_phi(env, block, node); + jump = node; } - - DBG((dbgssa, LEVEL_3, "\t...continue at idom (after checking block)\n")); - return get_def_at_idom(env, block); -} - -/** - * Adds a definition into the link field of the block. The definitions are - * sorted by dominance. A non-visited block means no definition has been - * inserted yet. - */ -static -void introduce_def_at_block(ir_node *block, ir_node *def) -{ - if(irn_visited(block)) { - ir_node *node = block; - ir_node *current_def; - - while(1) { - current_def = get_irn_link(node); - if(current_def == def) { - /* already in block */ - return; - } - if(current_def == NULL) - break; - if(value_dominates(current_def, def)) - break; - node = current_def; + if (jump == NULL) + goto check_preds; + + entity = get_Block_entity(block); + pred = get_Block_cfgpred(block, 0); + succ_block = NULL; + foreach_out_edge_safe(jump, 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 && entity != 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_link(node, def); - set_irn_link(def, current_def); - } else { - set_irn_link(block, def); - set_irn_link(def, NULL); - mark_irn_visited(block); + set_irn_n(succ_block, pos, pred); } -} -ir_node **be_ssa_construction(const be_dom_front_info_t *domfronts, be_lv_t *lv, - ir_node *value, int copies_len, ir_node **copies, - const ir_nodeset_t *ignore_uses, int need_new_phis) -{ - ir_graph *irg = get_irn_irg(value); - const ir_edge_t *edge, *next; - int i; - ir_node *block; - waitq *worklist; - ssa_constr_env_t env; - - /* We need to collect the phi functions to compute their liveness. */ - if(lv != NULL || need_new_phis) { - env.new_phis = NEW_ARR_F(ir_node*, 0); - } else { - env.new_phis = NULL; + if (entity != NULL) { + /* move the label to the successor block */ + set_Block_entity(succ_block, entity); } - env.mode = get_irn_mode(value); - set_using_visited(irg); - set_using_block_visited(irg); - set_using_irn_link(irg); + /* 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) { + ir_node *const node = get_edge_src_irn(edge); - /* we use the visited flag to indicate blocks in the dominance frontier - * and blocks that already have the relevant value at the end calculated */ - inc_irg_visited(irg); - /* We use the block visited flag to indicate blocks in the dominance - * froniter of some values (and this potentially needing phis) */ - inc_irg_block_visited(irg); - - DBG((dbgssa, LEVEL_1, "Introducing following copies for: %+F\n", value)); - /* compute iterated dominance frontiers and create lists in the block link - * fields that sort usages by dominance. Blocks in the dominance frontier - * are marked by links back to the block. */ - worklist = new_waitq(); - - block = get_nodes_block(value); - introduce_def_at_block(block, value); - waitq_put(worklist, block); - - for(i = 0; i < copies_len; ++i) { - ir_node *copy = copies[i]; - block = get_nodes_block(copy); - - if(!irn_visited(block)) { - waitq_put(worklist, block); + 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; } - introduce_def_at_block(block, copy); - DBG((dbgssa, LEVEL_1, "\t%+F in %+F\n", copy, block)); - } - - mark_iterated_dominance_frontiers(domfronts, worklist); - del_waitq(worklist); - - DBG((dbgssa, LEVEL_2, "New Definitions:\n")); - /* - * Search the valid def for each use and set it. - */ - foreach_out_edge_safe(value, edge, next) { - ir_node *use = get_edge_src_irn(edge); - ir_node *at = use; - int pos = get_edge_src_pos(edge); - - if(ignore_uses != NULL && ir_nodeset_contains(ignore_uses, use)) + if (is_Sync(node)) { + set_nodes_block(node, get_nodes_block(pred)); continue; - - if(is_Phi(use)) { - ir_node *block = get_nodes_block(use); - ir_node *predblock = get_Block_cfgpred_block(block, pos); - at = sched_last(predblock); } - - ir_node *def = search_def(&env, at); - - if(def == NULL) { - panic("no definition found for %+F at position %d\n", use, pos); + 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; } - - DBG((dbgssa, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def)); - set_irn_n(use, pos, def); + panic("Unexpected node %+F in block %+F with empty schedule", node, block); } - /* Recompute the liveness of the original nodes, the copies and the - * inserted phis. */ - if(lv != NULL) { - int n; - - be_liveness_update(lv, value); - for(i = 0; i < copies_len; ++i) { - ir_node *copy = copies[i]; - be_liveness_update(lv, copy); - } - - n = ARR_LEN(env.new_phis); - for(i = 0; i < n; ++i) { - ir_node *phi = env.new_phis[i]; - be_liveness_introduce(lv, phi); - } - } + set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X)); + kill_node(jump); + blocks_removed = 1; - clear_using_visited(irg); - clear_using_block_visited(irg); - clear_using_irn_link(irg); + /* check predecessor */ + remove_empty_block(get_nodes_block(pred)); + return; - if(!need_new_phis && env.new_phis != NULL) { - DEL_ARR_F(env.new_phis); - return NULL; +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); } - return env.new_phis; } -void be_ssa_constr_set_ignore(const be_dom_front_info_t *domfronts, be_lv_t *lv, - pset *nodes, pset *ignores) +/* removes basic blocks that just contain a jump instruction */ +int be_remove_empty_blocks(ir_graph *irg) { - ir_graph *irg; - const ir_edge_t *edge, *next; - int i; - ir_node *block; - waitq *worklist; - ir_node *value; - ssa_constr_env_t env; - - foreach_pset(nodes, value) { - pset_break(nodes); - break; - } - irg = get_irn_irg(value); - - /* We need to collect the phi functions to compute their liveness. */ - if(lv != NULL) { - env.new_phis = NEW_ARR_F(ir_node*, 0); - } + ir_node *end; + int i, arity; - set_using_visited(irg); - set_using_block_visited(irg); - set_using_irn_link(irg); + blocks_removed = 0; - /* we use the visited flag to indicate blocks in the dominance frontier - * and blocks that already have the relevant value at the end calculated */ + ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED); inc_irg_visited(irg); - /* We use the block visited flag to indicate blocks in the dominance - * froniter of some values (and this potentially needing phis) */ - inc_irg_block_visited(irg); - - DBG((dbgssa, LEVEL_1, "Introducing following copies for:\n")); - - /* compute iterated dominance frontiers and create lists in the block link - * fields that sort usages by dominance. Blocks in the dominance frontier - * are marked by links back to the block. */ - worklist = new_waitq(); - - foreach_pset(nodes, value) { - block = get_nodes_block(value); - env.mode = get_irn_mode(value); - - if(!irn_visited(block)) { - waitq_put(worklist, block); - } - introduce_def_at_block(block, value); - DBG((dbgssa, LEVEL_1, "\t%+F in %+F\n", value, block)); - } - - mark_iterated_dominance_frontiers(domfronts, worklist); - del_waitq(worklist); - - /* - * Search the valid def for each use and set it. - */ - foreach_pset(nodes, value) { - foreach_out_edge_safe(value, edge, next) { - ir_node *use = get_edge_src_irn(edge); - ir_node *at = use; - int pos = get_edge_src_pos(edge); - - if(ignores != NULL && pset_find_ptr(ignores, use)) - continue; - - if(is_Phi(use)) { - ir_node *block = get_nodes_block(use); - ir_node *predblock = get_Block_cfgpred_block(block, pos); - at = sched_last(predblock); - } - - ir_node *def = search_def(&env, at); - - if(def == NULL) { - panic("no definition found for %+F input %d\n", use, pos); - } - - DBG((dbgssa, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def)); - set_irn_n(use, pos, def); - } + 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); - /* Recompute the liveness of the original nodes, the copies and the - * inserted phis. */ - if(lv != NULL) { - int n; - - foreach_pset(nodes, value) { - be_liveness_update(lv, value); - } - - n = ARR_LEN(env.new_phis); - for(i = 0; i < n; ++i) { - ir_node *phi = env.new_phis[i]; - be_liveness_introduce(lv, phi); - } - DEL_ARR_F(env.new_phis); + if (blocks_removed) { + /* invalidate analysis info */ + clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE); } - - clear_using_visited(irg); - clear_using_block_visited(irg); - clear_using_irn_link(irg); + 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; -ir_node *insert_Perm_after(const arch_env_t *arch_env, - be_lv_t *lv, - const arch_register_class_t *cls, - be_dom_front_info_t *dom_front, - ir_node *pos) +/** + * Post-walker: remember all visited nodes in a bitset. + */ +static void mark_dead_nodes_walker(ir_node *node, void *data) { - 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_node *curr, *irn, *perm, **nodes; - int i, n; - - DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos)); - - be_liveness_nodes_live_at(lv, arch_env, cls, pos, live); - - n = pset_count(live); - - if(n == 0) { - del_pset(live); - return NULL; - } - - nodes = xmalloc(n * sizeof(nodes[0])); - - DBG((dbg, LEVEL_1, "live:\n")); - for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) { - DBG((dbg, LEVEL_1, "\t%+F\n", irn)); - nodes[i] = irn; - } - del_pset(live); - - perm = be_new_Perm(cls, irg, bl, n, nodes); - sched_add_after(pos, perm); - free(nodes); - - curr = perm; - for (i = 0; i < n; ++i) { - ir_node *copies[1]; - ir_node *perm_op = get_irn_n(perm, i); - const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op); - - 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; - - copies[0] = proj; - - be_ssa_construction(dom_front, lv, perm_op, 1, copies, NULL, 0); - } - - return perm; + remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data; + bitset_set(env->reachable, get_irn_idx(node)); } /** - * Post-block-walker: Find blocks containing only one jump and - * remove them. + * Post-block-walker: + * Walk through the schedule of every block and remove all dead nodes from it. */ -static void remove_empty_block(ir_node *block, void *data) { - const ir_edge_t *edge, *next; - ir_node *node; - int *changed = data; - ir_node *jump = NULL; +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; - assert(is_Block(block)); + 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 (get_Block_n_cfgpreds(block) != 1) - return; + if (bitset_is_set(env->reachable, get_irn_idx(node))) + continue; - sched_foreach(block, node) { - if (! is_Jmp(node)) - return; - if (jump != NULL) { - /* we should never have 2 jumps in a block */ - panic("We should never have 2 jumps in a block"); + if (env->lv != NULL) + be_liveness_remove(env->lv, node); + sched_remove(node); + + /* kill projs */ + if (get_irn_mode(node) == mode_T) { + foreach_out_edge_safe(node, 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); + } } - jump = node; - } - - if (jump == NULL) - return; - - node = get_Block_cfgpred(block, 0); - foreach_out_edge_safe(jump, edge, next) { - ir_node *block = get_edge_src_irn(edge); - int pos = get_edge_src_pos(edge); - - set_irn_n(block, pos, node); + kill_node(node); } - - set_Block_cfgpred(block, 0, new_Bad()); - be_kill_node(jump); - *changed = 1; } -/* removes basic blocks that just contain a jump instruction */ -int be_remove_empty_blocks(ir_graph *irg) { - int changed = 0; +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; - irg_block_walk_graph(irg, remove_empty_block, NULL, &changed); - if (changed) { - /* invalidate analysis info */ - set_irg_doms_inconsistent(irg); - set_irg_extblk_inconsistent(irg); - set_irg_outs_inconsistent(irg); - } - return changed; + // 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(dbgssa, "firm.be.ssaconstr"); FIRM_DBG_REGISTER(dbg, "firm.be.irgmod"); } - -BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);