X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbeirgmod.c;h=472d8410b7d7c4d1308ffbe06e84ba128087760c;hb=d4bbab11371a9c68aa2a716eaa7589b82a28691e;hp=ae92810d9ab663780de71f9d20eb2349ee7dee98;hpb=2adf84106c02caf097c2d6cf1764706bdc437bcc;p=libfirm diff --git a/ir/be/beirgmod.c b/ir/be/beirgmod.c index ae92810d9..472d8410b 100644 --- a/ir/be/beirgmod.c +++ b/ir/be/beirgmod.c @@ -1,18 +1,33 @@ +/* + * 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 + * @version $Id$ + * * 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 "config.h" @@ -20,13 +35,6 @@ #include -#ifdef HAVE_MALLOC_H - #include -#endif -#ifdef HAVE_ALLOCA_H - #include -#endif - #include "hashptr.h" #include "pdeq.h" #include "pset.h" @@ -34,6 +42,7 @@ #include "util.h" #include "debug.h" #include "error.h" +#include "xmalloc.h" #include "irflag_t.h" #include "ircons_t.h" @@ -44,452 +53,25 @@ #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.h" +#include "bearch_t.h" #include "besched_t.h" #include "belive_t.h" #include "benode_t.h" #include "beutil.h" #include "beinsn_t.h" - +#include "bessaconstr.h" +#include "beirg_t.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;) -/* - ____ ____ _ - / ___/ ___| / \ - \___ \___ \ / _ \ - ___) |__) / ___ \ - |____/____/_/ \_\ - ____ _ _ _ - / ___|___ _ __ ___| |_ _ __ _ _ ___| |_(_) ___ _ __ - | | / _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \ - | |__| (_) | | | \__ \ |_| | | |_| | (__| |_| | (_) | | | | - \____\___/|_| |_|___/\__|_| \__,_|\___|\__|_|\___/|_| |_| - -*/ - -/* The problem: Given a value and a set of "copies" that are known to represent - * the same abstract value, rewire all usages of the original value to their - * closest copy while introducing phis as necessary. - * - * Algorithm: Mark all blocks in the iterated dominance frontiers of the value - * and it's copies. Link the copies ordered by dominance to the blocks. - * The we search for each use all all definitions in the current block, if none - * is found, then we search one in the immediate dominator. If we are in a block - * of the dominance frontier, create a phi and search do the same search for the - * phi arguments. - */ - -/** - * Calculates the iterated dominance frontier of a set of blocks. Marks the - * blocks as visited. Sets the link fields of the blocks in the dominance - * frontier to the block itself. - */ -static -void mark_iterated_dominance_frontiers(const be_dom_front_info_t *domfronts, - waitq *worklist) -{ - DBG((dbgssa, LEVEL_3, "Dominance Frontier:")); - while(!pdeq_empty(worklist)) { - int i; - ir_node *block = waitq_get(worklist); - ir_node **domfront = be_get_dominance_frontier(domfronts, block); - int domfront_len = ARR_LEN(domfront); - - for (i = 0; i < domfront_len; ++i) { - ir_node *y = domfront[i]; - if(Block_block_visited(y)) - continue; - - if(!irn_visited(y)) { - set_irn_link(y, NULL); - waitq_put(worklist, y); - } - DBG((dbgssa, LEVEL_3, " %+F", y)); - mark_Block_block_visited(y); - } - } - DBG((dbgssa, LEVEL_3, "\n")); -} - -typedef struct ssa_constr_env_t { - ir_node **new_phis; /**< ARR_F of newly created phis or NULL */ - ir_mode *mode; /**< mode of the value */ -} ssa_constr_env_t; - -static -ir_node *search_def_end_of_block(ssa_constr_env_t *env, ir_node *block); - -static -ir_node *create_phi(ssa_constr_env_t *env, ir_node *block, ir_node *link_with) -{ - int i, n_preds = get_Block_n_cfgpreds(block); - ir_graph *irg = get_irn_irg(block); - ir_node *phi; - ir_node **ins = alloca(n_preds * sizeof(ins[0])); - - assert(n_preds > 1); - - 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); - } - - if(env->mode != mode_M) { - sched_add_after(block, phi); - } - - DBG((dbgssa, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block)); - set_irn_link(link_with, phi); - mark_irn_visited(block); - - 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); - - set_irn_n(phi, i, pred_def); - } - - 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; -} - -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 -ir_node *search_def(ssa_constr_env_t *env, ir_node *at) -{ - ir_node *block = get_nodes_block(at); - ir_node *node; - ir_node *def; - - DBG((dbgssa, LEVEL_3, "\t...searching def at %+F\n", at)); - - /* 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); - } - - /* 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; - } -#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); - } - - - 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; - } - - 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); - } -} - -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; - } - env.mode = get_irn_mode(value); - - set_using_visited(irg); - set_using_block_visited(irg); - set_using_irn_link(irg); - - /* 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); - } - 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)) - 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); - } - - DBG((dbgssa, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def)); - set_irn_n(use, pos, def); - } - - /* 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); - } - } - - clear_using_visited(irg); - clear_using_block_visited(irg); - clear_using_irn_link(irg); - - if(!need_new_phis && env.new_phis != NULL) { - DEL_ARR_F(env.new_phis); - return NULL; - } - 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) -{ - 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); - } - - set_using_visited(irg); - set_using_block_visited(irg); - set_using_irn_link(irg); - - /* 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:\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); - } - } - - /* 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); - } - - clear_using_visited(irg); - clear_using_block_visited(irg); - clear_using_irn_link(irg); -} - /* ___ _ ____ |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___ @@ -499,38 +81,41 @@ void be_ssa_constr_set_ignore(const be_dom_front_info_t *domfronts, be_lv_t *lv, */ -ir_node *insert_Perm_after(const arch_env_t *arch_env, - be_lv_t *lv, +ir_node *insert_Perm_after(be_irg_t *birg, const arch_register_class_t *cls, - be_dom_front_info_t *dom_front, ir_node *pos) { + const arch_env_t *arch_env = birg->main_env->arch_env; + be_lv_t *lv = birg->lv; 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; + 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); - - n = pset_count(live); + ir_nodeset_init(&live); + be_liveness_nodes_live_at(lv, arch_env, cls, pos, &live); + n = ir_nodeset_size(&live); if(n == 0) { - del_pset(live); + 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); sched_add_after(pos, perm); @@ -538,83 +123,152 @@ ir_node *insert_Perm_after(const arch_env_t *arch_env, 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); + 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; - copies[0] = proj; - - be_ssa_construction(dom_front, lv, perm_op, 1, copies, NULL, 0); + be_ssa_construction_init(&senv, birg); + 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 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; - 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; + 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); - set_irn_n(block, pos, node); + set_irn_n(succ_block, pos, pred); + } + + /* 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; + if (is_Block(node)) { + /* a Block->Block edge: This should be the MacroBlock + edge, ignore it. */ + assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge"); + continue; + } + if (is_Pin(node)) { + set_nodes_block(node, succ_block); + 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; + 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); + set_irg_loopinfo_inconsistent(irg); } - return changed; + return blocks_removed; } void be_init_irgmod(void) { - FIRM_DBG_REGISTER(dbgssa, "firm.be.ssaconstr"); FIRM_DBG_REGISTER(dbg, "firm.be.irgmod"); }