X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Floop.c;h=7d88ca56d00356bcb7fd32e923a8e43aebebea5c;hb=6f068af98daa4725d60e5d23a8f98ec2841cfa44;hp=bcc64ea1a6c6be41cec0fce2e4e7a8cc3163b10e;hpb=3fc1c705e6d20c74d46c916d97a44aea4377476b;p=libfirm diff --git a/ir/opt/loop.c b/ir/opt/loop.c index bcc64ea1a..7d88ca56d 100644 --- a/ir/opt/loop.c +++ b/ir/opt/loop.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -24,7 +24,6 @@ * * @version $Id$ */ - #include "config.h" #include "iroptimize.h" @@ -168,7 +167,7 @@ unsigned count_proj:1; /* Count projections */ unsigned max_cc_size; /* Maximum condition chain size [nodes] */ unsigned max_branches; -unsigned max_unrolled_loop_size; /* [nodes] */ +unsigned max_unrolled_loop_size; /* [nodes] */ unsigned allow_const_unrolling:1; unsigned allow_invar_unrolling:1; unsigned invar_unrolling_min_size; /* [nodes] */ @@ -179,23 +178,23 @@ static loop_opt_params_t opt_params; /* Loop analysis informations */ typedef struct loop_info_t { - unsigned nodes; /* node count */ - unsigned ld_st; /* load and store nodes */ - unsigned branches; /* number of conditions */ - unsigned calls; /* number of calls */ - unsigned cf_outs; /* number of cf edges which leave the loop */ - entry_edge cf_out; /* single loop leaving cf edge */ - int be_src_pos; /* position of the single own backedge in the head */ + unsigned nodes; /* node count */ + unsigned ld_st; /* load and store nodes */ + unsigned branches; /* number of conditions */ + unsigned calls; /* number of calls */ + unsigned cf_outs; /* number of cf edges which leave the loop */ + entry_edge cf_out; /* single loop leaving cf edge */ + int be_src_pos; /* position of the single own backedge in the head */ /* for inversion */ - unsigned cc_size; /* nodes in the condition chain */ + unsigned cc_size; /* nodes in the condition chain */ /* for unrolling */ - unsigned max_unroll; /* Number of unrolls satisfying max_loop_size */ - unsigned exit_cond; /* 1 if condition==true exits the loop. */ - unsigned latest_value:1; /* 1 if condition is checked against latest counter value */ - unsigned needs_backedge:1; /* 0 if loop is completely unrolled */ - unsigned decreasing:1; /* Step operation is_Sub, or step is<0 */ + unsigned max_unroll; /* Number of unrolls satisfying max_loop_size */ + unsigned exit_cond; /* 1 if condition==true exits the loop. */ + unsigned latest_value:1; /* 1 if condition is checked against latest counter value */ + unsigned needs_backedge:1; /* 0 if loop is completely unrolled */ + unsigned decreasing:1; /* Step operation is_Sub, or step is<0 */ /* IV informations of a simple loop */ ir_node *start_val; @@ -206,8 +205,8 @@ typedef struct loop_info_t { ir_tarval *count_tar; /* Number of loop iterations */ - ir_node *duff_cond; /* Duff mod */ - unrolling_kind_flag unroll_kind; /* constant or invariant unrolling */ + ir_node *duff_cond; /* Duff mod */ + unrolling_kind_flag unroll_kind; /* constant or invariant unrolling */ } loop_info_t; /* Information about the current loop */ @@ -379,7 +378,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode, int fi { int i; int n_cfgpreds; - ir_graph *irg; + ir_graph *irg = get_irn_irg(block); ir_node *phi; ir_node **in; @@ -389,7 +388,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode, int fi * Dead and bad blocks. */ if (get_irn_arity(block) < 1 || is_Bad(block)) { DB((dbg, LEVEL_5, "ssa bad %N\n", block)); - return new_Bad(); + return new_r_Bad(irg, mode); } if (block == ssa_second_def_block && !first) { @@ -404,7 +403,6 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode, int fi return value; } - irg = get_irn_irg(block); assert(block != get_irg_start_block(irg)); /* a Block with only 1 predecessor needs no Phi */ @@ -425,7 +423,7 @@ static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode, int fi /* create a new Phi */ NEW_ARR_A(ir_node*, in, n_cfgpreds); for (i = 0; i < n_cfgpreds; ++i) - in[i] = new_Unknown(mode); + in[i] = new_r_Dummy(irg, mode); phi = new_r_Phi(block, n_cfgpreds, in, mode); /* Important: always keep block phi list up to date. */ @@ -583,7 +581,7 @@ static unsigned is_nodes_block_marked(ir_node* node) /* Extends a nodes ins by node new. * NOTE: This is slow if a node n needs to be extended more than once. */ -static void extend_irn(ir_node *n, ir_node *new, int new_is_backedge) +static void extend_irn(ir_node *n, ir_node *newnode, int new_is_backedge) { ir_node **ins; int i; @@ -609,7 +607,7 @@ static void extend_irn(ir_node *n, ir_node *new, int new_is_backedge) for(i = 0; i < arity; ++i) { ins[i] = get_irn_n(n, i); } - ins[i] = new; + ins[i] = newnode; set_irn_in(n, new_arity, ins); @@ -703,7 +701,7 @@ static ir_node *copy_node(ir_node *node) * Order of ins is important for later usage. */ static void copy_walk(ir_node *node, walker_condition *walk_condition, - ir_loop *set_loop) + ir_loop *set_loop) { int i; int arity; @@ -785,8 +783,8 @@ static void copy_walk(ir_node *node, walker_condition *walk_condition, * Order of ins is important for later usage. * Takes copy_index, to phase-link copy at specific index. */ -static void copy_walk_n(ir_node *node, - walker_condition *walk_condition, int copy_index) +static void copy_walk_n(ir_node *node, walker_condition *walk_condition, + int copy_index) { int i; int arity; @@ -860,8 +858,8 @@ static void copy_walk_n(ir_node *node, /* Removes alle Blocks with non marked predecessors from the condition chain. */ static void unmark_not_allowed_cc_blocks(void) { - int blocks = ARR_LEN(cc_blocks); - int i; + size_t blocks = ARR_LEN(cc_blocks); + size_t i; for(i = 0; i < blocks; ++i) { ir_node *block = cc_blocks[i]; @@ -889,8 +887,8 @@ static void unmark_not_allowed_cc_blocks(void) * TODO: invert head for unrolling? */ static void unmark_cc_blocks(void) { - int blocks = ARR_LEN(cc_blocks); - int i; + size_t blocks = ARR_LEN(cc_blocks); + size_t i; for(i = 0; i < blocks; ++i) { ir_node *block = cc_blocks[i]; @@ -1021,7 +1019,7 @@ static unsigned find_condition_chain(ir_node *block) * / A* B / | * / /\ / ? | * / C* => D | - * / D Head | + * / D Head | * / A \_| * C */ @@ -1059,10 +1057,11 @@ static void fix_copy_inversion(void) ir_node **ins; ir_node **phis; ir_node *phi, *next; - ir_node *head_cp = get_inversion_copy(loop_head); - int arity = get_irn_arity(head_cp); - int backedges = get_backedge_n(head_cp, 0); - int new_arity = arity - backedges; + ir_node *head_cp = get_inversion_copy(loop_head); + ir_graph *irg = get_irn_irg(head_cp); + int arity = get_irn_arity(head_cp); + int backedges = get_backedge_n(head_cp, 0); + int new_arity = arity - backedges; int pos; int i; @@ -1075,7 +1074,7 @@ static void fix_copy_inversion(void) ins[pos++] = get_irn_n(head_cp, i); } - new_head = new_Block(new_arity, ins); + new_head = new_r_Block(irg, new_arity, ins); phis = NEW_ARR_F(ir_node *, 0); @@ -1114,9 +1113,10 @@ static void fix_head_inversion(void) ir_node **ins; ir_node *phi, *next; ir_node **phis; - int arity = get_irn_arity(loop_head); - int backedges = get_backedge_n(loop_head, 0); - int new_arity = backedges; + ir_graph *irg = get_irn_irg(loop_head); + int arity = get_irn_arity(loop_head); + int backedges = get_backedge_n(loop_head, 0); + int new_arity = backedges; int pos; int i; @@ -1129,7 +1129,7 @@ static void fix_head_inversion(void) ins[pos++] = get_irn_n(loop_head, i); } - new_head = new_Block(new_arity, ins); + new_head = new_r_Block(irg, new_arity, ins); phis = NEW_ARR_F(ir_node *, 0); @@ -1180,7 +1180,7 @@ static void fix_head_inversion(void) /* Does the loop inversion. */ static void inversion_walk(entry_edge *head_entries) { - int i; + size_t i; /* * The order of rewiring bottom-up is crucial. @@ -1434,7 +1434,7 @@ static void unrolling_fix_loop_head_inv(void) ir_node *last_pred = get_unroll_copy(pred, unroll_nr - 1); ins[0] = last_pred; - ins[1] = get_irn_link(phi); + ins[1] = (ir_node*)get_irn_link(phi); set_irn_in(phi, 2, ins); DB((dbg, LEVEL_4, "Rewire ins of loophead phi %N to pred %N and duffs entry %N \n" , phi, ins[0], ins[1])); } @@ -1463,7 +1463,8 @@ static void correct_phis(ir_node *node, void *env) static void place_copies(int copies) { ir_node *loophead = loop_head; - int c, i; + size_t i; + int c; int be_src_pos = loop_info.be_src_pos; /* Serialize loops by fixing their head ins. @@ -1523,7 +1524,7 @@ static void place_copies(int copies) ir_node *duff_phi; lower_phi = get_unroll_copy(phi, c + 1); - duff_phi = get_irn_link(phi); + duff_phi = (ir_node*)get_irn_link(phi); DB((dbg, LEVEL_4, "DD Link of %N is %N\n" , phi, duff_phi)); /* */ @@ -1562,9 +1563,9 @@ static void place_copies(int copies) ir_node *pred = get_irn_n(phi, be_src_pos); ir_node *last_pred; - /* It is possible, that the value used - * in the OWN backedge path is NOT assigned in this loop. */ - if (is_in_loop(pred)) + /* It is possible, that the value used + * in the OWN backedge path is NOT assigned in this loop. */ + if (is_in_loop(pred)) last_pred = get_unroll_copy(pred, copies); else last_pred = pred; @@ -1579,11 +1580,12 @@ static void place_copies(int copies) /* Copies the cur_loop several times. */ static void copy_loop(entry_edge *cur_loop_outs, int copies) { - int i, c; + int c; ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED); for (c = 0; c < copies; ++c) { + size_t i; inc_irg_visited(current_ir_graph); @@ -1657,10 +1659,9 @@ static ir_node *new_Abs(ir_node *op, ir_mode *mode) ir_graph *irg = get_irn_irg(op); ir_node *block = get_nodes_block(op); ir_node *zero = new_r_Const(irg, get_mode_null(mode)); - ir_node *cmp = new_r_Cmp(block, op, zero); - ir_node *cond = new_r_Proj(cmp, mode_b, pn_Cmp_Lt); + ir_node *cmp = new_r_Cmp(block, op, zero, ir_relation_less); ir_node *minus_op = new_r_Minus(block, op, mode); - ir_node *mux = new_r_Mux(block, cond, op, minus_op, mode); + ir_node *mux = new_r_Mux(block, cmp, op, minus_op, mode); return mux; } @@ -1674,8 +1675,8 @@ static void create_duffs_block(void) ir_mode *mode; ir_node *block1, *count_block, *duff_block; - ir_node *ems, *ems_divmod, *ems_mod_proj, *cmp_null, - *cmp_proj, *ems_mode_cond, *x_true, *x_false, *const_null; + ir_node *ems, *ems_mod, *ems_div, *ems_mod_proj, *cmp_null, + *ems_mode_cond, *x_true, *x_false, *const_null; ir_node *true_val, *false_val; ir_node *ins[2]; @@ -1712,21 +1713,25 @@ static void create_duffs_block(void) ems = new_Sub(loop_info.end_val, loop_info.start_val, get_irn_mode(loop_info.end_val)); - DB((dbg, LEVEL_4, "divmod ins %N %N\n", ems, loop_info.step)); - ems_divmod = new_r_DivMod(block1, + DB((dbg, LEVEL_4, "mod ins %N %N\n", ems, loop_info.step)); + ems_mod = new_r_Mod(block1, + new_NoMem(), + ems, + loop_info.step, + mode, + op_pin_state_pinned); + ems_div = new_r_Div(block1, new_NoMem(), ems, loop_info.step, mode, op_pin_state_pinned); - DB((dbg, LEVEL_4, "New module node %N\n", ems_divmod)); - - ems_mod_proj = new_r_Proj(ems_divmod, mode_Iu, pn_DivMod_res_mod); - cmp_null = new_r_Cmp(block1, ems_mod_proj, const_null); - cmp_proj = new_r_Proj(cmp_null, mode_b, pn_Cmp_Eq); - ems_mode_cond = new_r_Cond(block1, cmp_proj); + DB((dbg, LEVEL_4, "New module node %N\n", ems_mod)); + ems_mod_proj = new_r_Proj(ems_mod, mode_Iu, pn_Mod_res); + cmp_null = new_r_Cmp(block1, ems_mod_proj, const_null, ir_relation_less); + ems_mode_cond = new_r_Cond(block1, cmp_null); /* ems % step == 0 */ x_true = new_r_Proj(ems_mode_cond, mode_X, pn_Cond_true); @@ -1766,13 +1771,11 @@ static void create_duffs_block(void) correction = new_r_Phi(count_block, 2, ins, mode); - count = new_r_Proj(ems_divmod, mode, pn_DivMod_res_div); + count = new_r_Proj(ems_div, mode, pn_Div_res); /* (end - start) / step + correction */ count = new_Add(count, correction, mode); - cmp_bad_count = new_r_Cmp(count_block, count, const_null); - /* We preconditioned the loop to be tail-controlled. * So, if count is something 'wrong' like 0, * negative/positive (depending on step direction), @@ -1781,12 +1784,14 @@ static void create_duffs_block(void) /* Depending on step direction, we have to check for > or < 0 */ if (loop_info.decreasing == 1) { - bad_count_neg = new_r_Proj(cmp_bad_count, mode_b, pn_Cmp_Lt); + cmp_bad_count = new_r_Cmp(count_block, count, const_null, + ir_relation_less); } else { - bad_count_neg = new_r_Proj(cmp_bad_count, mode_b, pn_Cmp_Gt); + cmp_bad_count = new_r_Cmp(count_block, count, const_null, + ir_relation_greater); } - bad_count_neg = new_r_Cond(count_block, bad_count_neg); + bad_count_neg = new_r_Cond(count_block, cmp_bad_count); good_count = new_Proj(bad_count_neg, mode_X, pn_Cond_true); bad_count = new_Proj(ems_mode_cond, mode_X, pn_Cond_false); @@ -1985,38 +1990,17 @@ static unsigned get_const_pred(ir_node *node, ir_node **const_pred, ir_node **ot return 1; } -/* Returns the mathematically inverted pn_Cmp. */ -static pn_Cmp get_math_inverted_case(pn_Cmp proj) -{ - switch(proj) { - case pn_Cmp_Eq: - return pn_Cmp_Lg; - case pn_Cmp_Lg: - return pn_Cmp_Eq; - case pn_Cmp_Lt: - return pn_Cmp_Ge; - case pn_Cmp_Le: - return pn_Cmp_Gt; - case pn_Cmp_Gt: - return pn_Cmp_Le; - case pn_Cmp_Ge: - return pn_Cmp_Lt; - default: - panic("Unhandled pn_Cmp."); - } -} - /* Returns 1 if loop exits within 2 steps of the iv. * Norm_proj means we do not exit the loop.*/ static unsigned simulate_next(ir_tarval **count_tar, ir_tarval *stepped, ir_tarval *step_tar, ir_tarval *end_tar, - pn_Cmp norm_proj) + ir_relation norm_proj) { ir_tarval *next; DB((dbg, LEVEL_4, "Loop taken if (stepped)%ld %s (end)%ld ", get_tarval_long(stepped), - get_pnc_string((norm_proj)), + get_relation_string((norm_proj)), get_tarval_long(end_tar))); DB((dbg, LEVEL_4, "comparing latest value %d\n", loop_info.latest_value)); @@ -2027,7 +2011,7 @@ static unsigned simulate_next(ir_tarval **count_tar, DB((dbg, LEVEL_4, "Result: (stepped)%ld IS %s (end)%ld\n", get_tarval_long(stepped), - get_pnc_string(tarval_cmp(stepped, end_tar)), + get_relation_string(tarval_cmp(stepped, end_tar)), get_tarval_long(end_tar))); /* next step */ @@ -2039,7 +2023,7 @@ static unsigned simulate_next(ir_tarval **count_tar, DB((dbg, LEVEL_4, "Loop taken if %ld %s %ld ", get_tarval_long(next), - get_pnc_string(norm_proj), + get_relation_string(norm_proj), get_tarval_long(end_tar))); DB((dbg, LEVEL_4, "comparing latest value %d\n", loop_info.latest_value)); @@ -2064,7 +2048,7 @@ static unsigned simulate_next(ir_tarval **count_tar, static ir_node *is_simple_loop(void) { int arity, i; - ir_node *loop_block, *exit_block, *projx, *cond, *projres, *loop_condition; + ir_node *loop_block, *exit_block, *projx, *cond, *cmp; /* Maximum of one condition, and no endless loops. */ if (loop_info.cf_outs != 1) @@ -2119,13 +2103,12 @@ static ir_node *is_simple_loop(void) /* find value on which loop exit depends */ projx = loop_info.cf_out.pred; cond = get_irn_n(projx, 0); - projres = get_irn_n(cond, 0); - loop_condition = get_irn_n(projres, 0); + cmp = get_irn_n(cond, 0); - if (!is_Cmp(loop_condition)) + if (!is_Cmp(cmp)) return NULL; - DB((dbg, LEVEL_5, "projection is %s\n", get_pnc_string(get_Proj_proj(projx)))); + DB((dbg, LEVEL_5, "projection is %s\n", get_relation_string(get_Proj_proj(projx)))); switch(get_Proj_proj(projx)) { case pn_Cond_false: @@ -2139,8 +2122,7 @@ static ir_node *is_simple_loop(void) } DB((dbg, LEVEL_4, "Valid Cmp.\n")); - - return projres; + return cmp; } /* Returns 1 if all nodes are mode_Iu or mode_Is. */ @@ -2162,10 +2144,10 @@ static unsigned are_mode_I(ir_node *n1, ir_node* n2, ir_node *n3) static unsigned get_unroll_decision_invariant(void) { - ir_node *projres, *loop_condition, *iteration_path; - unsigned success, is_latest_val; - ir_tarval *step_tar; - ir_mode *mode; + ir_node *projres, *loop_condition, *iteration_path; + unsigned success, is_latest_val; + ir_tarval *step_tar; + ir_mode *mode; /* RETURN if loop is not 'simple' */ @@ -2369,15 +2351,16 @@ static unsigned get_preferred_factor_constant(ir_tarval *count_tar) /* TODO split. */ static unsigned get_unroll_decision_constant(void) { - ir_node *projres, *loop_condition, *iteration_path; - unsigned success, is_latest_val; - ir_tarval *start_tar, *end_tar, *step_tar, *diff_tar, *count_tar, *stepped; - pn_Cmp proj_proj, norm_proj; - ir_mode *mode; + ir_node *cmp, *iteration_path; + unsigned success, is_latest_val; + ir_tarval *start_tar, *end_tar, *step_tar, *diff_tar, *count_tar; + ir_tarval *stepped; + ir_relation proj_proj, norm_proj; + ir_mode *mode; /* RETURN if loop is not 'simple' */ - projres = is_simple_loop(); - if (projres == NULL) + cmp = is_simple_loop(); + if (cmp == NULL) return 0; /* One in of the loop condition needs to be loop invariant. => end_val @@ -2395,9 +2378,7 @@ static unsigned get_unroll_decision_constant(void) /\ */ - loop_condition = get_irn_n(projres, 0); - - success = get_const_pred(loop_condition, &loop_info.end_val, &iteration_path); + success = get_const_pred(cmp, &loop_info.end_val, &iteration_path); if (! success) return 0; @@ -2529,17 +2510,16 @@ static unsigned get_unroll_decision_constant(void) DB((dbg, LEVEL_4, "stepped to %ld\n", get_tarval_long(stepped))); - proj_proj = get_Proj_proj(projres); + proj_proj = get_Cmp_relation(cmp); /* Assure that norm_proj is the stay-in-loop case. */ if (loop_info.exit_cond == 1) - norm_proj = get_math_inverted_case(proj_proj); + norm_proj = get_negated_relation(proj_proj); else norm_proj = proj_proj; - DB((dbg, LEVEL_4, "normalized projection %s\n", get_pnc_string(norm_proj))); - + DB((dbg, LEVEL_4, "normalized projection %s\n", get_relation_string(norm_proj))); /* Executed at most once (stay in counting loop if a Eq b) */ - if (norm_proj == pn_Cmp_Eq) + if (norm_proj == ir_relation_equal) /* TODO Might be worth a warning. */ return 0; @@ -2611,7 +2591,7 @@ static void unroll_loop(void) if (opt_params.allow_invar_unrolling) unroll_nr = get_unroll_decision_invariant(); if (unroll_nr > 1) - loop_info.unroll_kind = invariant; + loop_info.unroll_kind = invariant; } DB((dbg, LEVEL_2, " *** Unrolling %d times ***\n", unroll_nr)); @@ -2714,13 +2694,13 @@ static void init_analyze(ir_loop *loop) static void find_innermost_loop(ir_loop *loop) { /* descend into sons */ - int sons = get_loop_n_sons(loop); + size_t sons = get_loop_n_sons(loop); if (sons == 0) { ARR_APP1(ir_loop *, loops, loop); } else { - int s; - for (s=0; s