X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fescape_ana.c;h=d37ca7c1d469b0f0988d86e11c114ffe141a2379;hb=b2dba3350d751a784b350c19ddd839566e0eb3f0;hp=2d0bf162f4ff07384ba9760403d63e437031e38b;hpb=26c150fa48e3f1663ede80a4f91384cbee59afb8;p=libfirm diff --git a/ir/opt/escape_ana.c b/ir/opt/escape_ana.c index 2d0bf162f..d37ca7c1d 100644 --- a/ir/opt/escape_ana.c +++ b/ir/opt/escape_ana.c @@ -1,3 +1,22 @@ +/* + * Copyright (C) 1995-2007 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. + */ + /* * Project: libFIRM * File name: ir/opt/escape_ana.c @@ -7,12 +26,12 @@ * Created: 03.11.2005 * CVS-ID: $Id$ * Copyright: (c) 1999-2005 Universität Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. */ -/** @file escape_ana.c +/** + * @file escape_ana.c * - * escape analysis. + * A fast and simple Escape analysis. */ #ifdef HAVE_CONFIG_H @@ -21,11 +40,14 @@ #include "irgraph_t.h" #include "irnode_t.h" +#include "type_t.h" +#include "irgwalk.h" #include "irouts.h" #include "analyze_irg_args.h" #include "irgmod.h" #include "ircons.h" #include "escape_ana.h" +#include "debug.h" /** * walker environment @@ -33,7 +55,8 @@ typedef struct _walk_env { ir_node *found_allocs; /**< list of all found non-escaped allocs */ ir_node *dead_allocs; /**< list of all found dead alloc */ - unsigned nr_changed; /**< number of changed allocs */ + unsigned nr_removed; /**< number of removed allocs (placed of frame) */ + unsigned nr_changed; /**< number of changed allocs (allocated on stack now) */ unsigned nr_deads; /**< number of dead allocs */ /* these fields are only used in the global escape analysis */ @@ -42,6 +65,9 @@ typedef struct _walk_env { } walk_env_t; +/** debug handle */ +DEBUG_ONLY(firm_dbg_module_t *dbgHandle;) + /** * checks whether a Raise leaves a method */ @@ -91,7 +117,7 @@ static int is_method_leaving_raise(ir_node *raise) * determine if a value calculated by n "escape", ie * is stored somewhere we could not track */ -static int do_escape(ir_node *n) { +static int can_escape(ir_node *n) { int i, j, k; /* should always be pointer mode or we made some mistake */ @@ -120,7 +146,7 @@ static int do_escape(ir_node *n) { case iro_Call: { /* most complicated case */ ir_node *ptr = get_Call_ptr(succ); - entity *ent; + ir_entity *ent; if (get_irn_op(ptr) == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) { @@ -136,11 +162,16 @@ static int do_escape(ir_node *n) { } } } - else { + else if (get_irn_op(ptr) == op_Sel) { /* go through all possible callees */ for (k = get_Call_n_callees(succ) - 1; k >= 0; --k) { ent = get_Call_callee(succ, k); + if (ent == unknown_entity) { + /* we don't know what will be called, a possible escape */ + return 1; + } + for (j = get_Call_n_params(succ) - 1; j >= 0; --j) { if (get_Call_param(succ, j) == n) { /* n is the j'th param of the call */ @@ -151,6 +182,9 @@ static int do_escape(ir_node *n) { } } } + else /* we don't know want will called */ + return 1; + break; } @@ -198,7 +232,7 @@ static int do_escape(ir_node *n) { if (! mode_is_reference(get_irn_mode(succ))) continue; - if (do_escape(succ)) + if (can_escape(succ)) return 1; } return 0; @@ -241,7 +275,7 @@ static void find_allocations(ir_node *alloc, void *ctx) return; } - if (! do_escape(adr)) { + if (! can_escape(adr)) { set_irn_link(alloc, env->found_allocs); env->found_allocs = alloc; } @@ -252,10 +286,10 @@ static void find_allocations(ir_node *alloc, void *ctx) */ static void transform_allocs(ir_graph *irg, walk_env_t *env) { - ir_node *alloc, *next, *mem, *sel; - type *ftp; - entity *ent; - char name[32]; + ir_node *alloc, *next, *mem, *sel, *size; + ir_type *ftp, *atp, *tp; + ir_entity *ent; + char name[128]; unsigned nr = 0; dbg_info *dbg; @@ -263,6 +297,8 @@ static void transform_allocs(ir_graph *irg, walk_env_t *env) for (alloc = env->dead_allocs; alloc; alloc = next) { next = get_irn_link(alloc); + DBG((dbgHandle, LEVEL_1, "%+F allocation of %+F unused, deleted.\n", irg, alloc)); + mem = get_Alloc_mem(alloc); turn_into_tuple(alloc, pn_Alloc_max); set_Tuple_pred(alloc, pn_Alloc_M, mem); @@ -275,28 +311,66 @@ static void transform_allocs(ir_graph *irg, walk_env_t *env) ftp = get_irg_frame_type(irg); for (alloc = env->found_allocs; alloc; alloc = next) { next = get_irn_link(alloc); - dbg = get_irn_dbg_info(alloc); + size = get_Alloc_size(alloc); + atp = get_Alloc_type(alloc); + + tp = NULL; + if (get_irn_op(size) == op_SymConst && get_SymConst_kind(size) == symconst_type_size) { + /* if the size is a type size and the types matched */ + assert(atp == get_SymConst_type(size)); + tp = atp; + } + else if (is_Const(size)) { + tarval *tv = get_Const_tarval(size); + + if (tv != tarval_bad && tarval_is_long(tv) && + get_type_state(atp) == layout_fixed && + get_tarval_long(tv) == get_type_size_bytes(atp)) { + /* a already lowered type size */ + tp = atp; + } + } - snprintf(name, sizeof(name), "_not_escaped_%u", nr++); - ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg); + if (tp && tp != firm_unknown_type) { + /* we could determine the type, so we could place it on the frame */ + dbg = get_irn_dbg_info(alloc); - sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc), - get_irg_no_mem(irg), get_irg_frame(irg), ent); - mem = get_Alloc_mem(alloc); + DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on frame\n", irg, alloc, tp)); - turn_into_tuple(alloc, pn_Alloc_max); - set_Tuple_pred(alloc, pn_Alloc_M, mem); - set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg)); - set_Tuple_pred(alloc, pn_Alloc_res, sel); + snprintf(name, sizeof(name), "%s_NE_%u", get_entity_name(get_irg_entity(irg)), nr++); + ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg); + + sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc), + get_irg_no_mem(irg), get_irg_frame(irg), ent); + mem = get_Alloc_mem(alloc); + + turn_into_tuple(alloc, pn_Alloc_max); + set_Tuple_pred(alloc, pn_Alloc_M, mem); + set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg)); + set_Tuple_pred(alloc, pn_Alloc_res, sel); - ++env->nr_changed; + ++env->nr_removed; + } + else { + /* + * We could not determine the type or it is variable size. + * At least, we could place it on the stack + */ + DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on stack\n", irg, alloc)); + set_Alloc_where(alloc, stack_alloc); + + ++env->nr_changed; + } } - if (env->nr_changed | env->nr_deads) { + /* if allocs were removed somehow */ + if (env->nr_removed | env->nr_deads) { set_irg_outs_inconsistent(irg); - if (env->nr_deads) - set_irg_dom_inconsistent(irg); + if (env->nr_deads) { + /* exception control flow might have been changed */ + set_irg_doms_inconsistent(irg); + } } } @@ -316,6 +390,7 @@ void escape_enalysis_irg(ir_graph *irg) env.found_allocs = NULL; env.dead_allocs = NULL; + env.nr_removed = 0; env.nr_changed = 0; env.nr_deads = 0; @@ -337,6 +412,8 @@ void escape_analysis(int run_scalar_replace) return; } + FIRM_DBG_REGISTER(dbgHandle, "firm.opt.escape_ana"); + /* * We treat memory for speed: we first collect all info in a * list of environments, than do the transformation. @@ -359,7 +436,7 @@ void escape_analysis(int run_scalar_replace) irg_walk_graph(irg, NULL, find_allocations, env); if (env->found_allocs || env->dead_allocs) { - env->nr_changed = 0; + env->nr_removed = 0; env->nr_deads = 0; env->irg = irg; env->next = elist;