From: Michael Beck Date: Thu, 6 Oct 2005 10:37:29 +0000 (+0000) Subject: Beyhan's fixes included X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=1ee99196ff61a9c856497a46c955a928437b7196;p=libfirm Beyhan's fixes included [r6659] --- diff --git a/ir/opt/strength_red.c b/ir/opt/strength_red.c index 565a9f464..4ed658cc0 100644 --- a/ir/opt/strength_red.c +++ b/ir/opt/strength_red.c @@ -51,29 +51,38 @@ static int n_made_new_phis; /** Detect basic iteration variables. * - * The variable ir represented by a subgraph as this: + * The variable is represented by a subgraph as this: + * @verbatim * * init * /|\ * | - * |-- Phi + * +-- Phi * | /|\ * | | - * |-->op + * +-->op * - * Where op is a Add or Sub, and init is loop invariant. - * @@@ So far we only accept Phi nodes with two predecessors. - * We could expand this to Phi nodes where all preds are either - * op or loop invariant. + * @endverbatim + * + * Where op is a Add or Sub node and init is loop invariant. + * + * @note + * So far we only accept Phi nodes with two predecessors. + * We could expand this to Phi nodes where all predecessors + * are either op or loop invariant. * - * @param n A phi node. * @param info After call contains the induction variable information. + * + * @return + * The info structure. */ +induct_var_info *is_induction_variable(induct_var_info *info) { -induct_var_info *is_induction_variable (induct_var_info *info) { - - int i; - int op_pred, Store_in_op, Store_in_phi, cmp_in_phi; + int i, phi_arity; + int op_pred, Store_in_op, Store_in_phi; + ir_node *cmp_pred_bl, *cond_succ_true, *cond_succ_false, *cmp_const; + ir_node *loop_head, *phi; + ir_node *cmp_const_block; info->c = NULL; info->cmp = NULL; @@ -101,28 +110,33 @@ induct_var_info *is_induction_variable (induct_var_info *info) { assert(get_irn_op(info->itervar_phi) == op_Phi); - /* The necessary conditions for the phi node. */ - if (get_irn_arity(info->itervar_phi) != 2 || - !has_backedges(get_nodes_block(info->itervar_phi)) ) + phi = info->itervar_phi; + phi_arity = get_irn_arity(phi); + + /* + * The necessary conditions for the phi node: + * We can handle currently Phi's with 2 predecessors, one must be a backedge. + */ + if (phi_arity != 2 || !has_backedges(get_nodes_block(phi))) return NULL; for (i = 0; i < 2; ++i) { - ir_node *pred = get_Phi_pred(info->itervar_phi, i); + ir_node *pred = get_Phi_pred(phi, i); ir_op *op = get_irn_op(pred); - /* Compute if the induction variable is added or substracted with a constant . */ + /* Compute if the induction variable is added or subtracted with a constant. */ if (op == op_Add || op == op_Sub) { ir_node *n_l = get_binop_left(pred); ir_node *n_r = get_binop_right(pred); - if (n_l == info->itervar_phi) { + if (n_l == phi) { info->operation_code = op; info->increment = n_r; info->op_pred_pos = i; info->init_pred_pos = i ^ 1; break; } - else if (n_r == info->itervar_phi) { + else if (n_r == phi) { info->operation_code = op; info->increment = n_l; info->op_pred_pos = i; @@ -136,40 +150,40 @@ induct_var_info *is_induction_variable (induct_var_info *info) { return NULL; /* Compute the position of the backedge. */ - if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)){ - info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos); - info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos); + if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) { + info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos); + info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos); } else { /* irregular control flow detected. */ return NULL; } + /* + * the block of the init code should dominate the loop, else + * we have an irregular control flow + */ if (get_Block_dom_depth(get_nodes_block(info->init)) >= get_Block_dom_depth(get_nodes_block(info->itervar_phi))) { return NULL; } - /* This "for" marks if the iteration operation have a Store successor .*/ op_pred = get_irn_n_outs(info->op); Store_in_op = 0; Store_in_phi = 0; - cmp_in_phi = 0; - for (i = 0; i < op_pred; ++i){ - ir_node *out = get_irn_out(info->op, i); - ir_op *out_op = get_irn_op(out); - if (out_op == op_Store) - Store_in_op++; - } /* Information about loop of itervar_phi. */ info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi)); - /* This "for" searches for the Cmp successor of the - iter_var to reduce and marks if the iter_var have a Store - successor or a successor out of loop.*/ + /* + *This "for" searches for the Cmp successor of the + * iter_var to reduce and marks if the iter_var have a Store + * successor or a successor out of loop. + */ info->phi_pred = get_irn_n_outs(info->itervar_phi); - for (i = 0; i < info->phi_pred; ++i) { + loop_head = get_nodes_block(info->itervar_phi); + + for (i = 0; i < info->phi_pred; i++) { ir_node *out = get_irn_out(info->itervar_phi, i); ir_op *out_op = get_irn_op(out); @@ -180,29 +194,77 @@ induct_var_info *is_induction_variable (induct_var_info *info) { if (out_op == op_Store) Store_in_phi++; - else if (out_op == op_Cmp){ - info->cmp = out; - cmp_in_phi++; + else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) { + cmp_pred_bl = get_irn_out(out, 0); + cmp_pred_bl = get_irn_out(cmp_pred_bl, 0); + cond_succ_true = get_irn_out(cmp_pred_bl, 1); + cond_succ_false = get_irn_out(cmp_pred_bl, 0); + + if (is_loop_invariant(get_irn_out(cond_succ_false, 0), loop_head) || + is_loop_invariant(get_irn_out(cond_succ_true, 0), loop_head)) { + if (get_Cmp_left(out) == info->itervar_phi) + cmp_const = get_Cmp_right(out); + else + cmp_const = get_Cmp_left(out); + } + else + continue; + + if (info->cmp == NULL) { + info->cmp = out; + info->cmp_const = cmp_const; + } + else { + info->cmp = NULL; + return NULL; + } } } + for (i = 0; i < op_pred; ++i) { + ir_node *out = get_irn_out(info->op, i); + ir_op *out_op = get_irn_op(out); + if (out_op == op_Store) + Store_in_op++; + else { + if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)){ + cmp_pred_bl = get_irn_out(out, 0); + cmp_pred_bl = get_irn_out(cmp_pred_bl, 0); + cond_succ_true = get_irn_out(cmp_pred_bl, 1); + cond_succ_false = get_irn_out(cmp_pred_bl, 0); + if(is_loop_invariant(get_irn_out(cond_succ_false, 0), loop_head) || + is_loop_invariant(get_irn_out(cond_succ_true, 0), loop_head)){ + if (get_Cmp_left(out) == info->op) + cmp_const = get_Cmp_right(out); + else + cmp_const = get_Cmp_left(out); + } + else + continue; - if((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && cmp_in_phi == 1) || - (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) || - (info->phi_pred == 1 && Store_in_op == 0)) - info->reducible = 1; + if (info->cmp == NULL) { + info->cmp = out; + info->cmp_const = cmp_const; + set_irn_link(info->cmp_const, (void *) 1); + } + else { + info->cmp = NULL; + return NULL; + } + } + } + } - // Search for loop invariant of Cmp. - if (info->cmp != NULL) { - ir_node *cmp_const_block; - if (get_Cmp_left(info->cmp) == info->itervar_phi) - info->cmp_const = get_Cmp_right(info->cmp); - else - info->cmp_const = get_Cmp_left(info->cmp); + if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) || + (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) || + (info->phi_pred == 1 && Store_in_op == 0)) + info->reducible = 1; + // Search for loop invariant of Cmp. + if (info->cmp != NULL){ cmp_const_block = get_nodes_block(info->cmp_const); if (get_Block_dom_depth(get_nodes_block(info->init)) >= - get_Block_dom_depth(cmp_const_block)) + get_Block_dom_depth(cmp_const_block)) info->cmp_init_block = get_nodes_block(info->init); else info->cmp_init_block = cmp_const_block; @@ -214,7 +276,7 @@ induct_var_info *is_induction_variable (induct_var_info *info) { * Creates a new Add node from operands. */ static INLINE ir_node * -my_new_r_Add (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { +my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { ir_mode *m = get_irn_mode(op1); ir_mode *m2 = get_irn_mode(op2); @@ -227,7 +289,7 @@ my_new_r_Add (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { * Creates a new Sub node from operands. */ static INLINE ir_node * -my_new_r_Sub (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { +my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { ir_mode *m = get_irn_mode(op1); ir_mode *m2 = get_irn_mode(op2); @@ -245,7 +307,7 @@ my_new_r_Sub (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) { */ static int reduce(ir_node *reduce_var, induct_var_info *ivi) { - // Essential conditions for a reducable node. + // Essential conditions for a reducible node. if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi) return 0; @@ -280,7 +342,7 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) if (mul_const == NULL || mul_init == NULL) return 0; - init_block = get_nodes_block(mul_init); + init_block = get_nodes_block(ivi->init); increment_block = get_nodes_block(ivi->increment); c_block = get_nodes_block(mul_const); @@ -339,10 +401,6 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op); - - - - // This for search for a reducible successor of reduc_var. reduce_var_pred = get_irn_n_outs(reduce_var); if (reduce_var_pred == 1) { @@ -363,17 +421,17 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) mul_const, ivi->init, get_irn_mode(mul_const)); if(ivi->cmp != NULL) - ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block, - ivi->cmp_const, mul_const, get_irn_mode(mul_const)); - ivi->increment = new_r_Mul (current_ir_graph, block_init, - ivi->increment, mul_const, get_irn_mode(mul_const)); - }else { - ivi->new_init = new_r_Mul (current_ir_graph, get_nodes_block(ivi->init), - mul_const, ivi->new_init, - get_irn_mode(mul_const)); - ivi->new_increment = new_r_Mul (current_ir_graph, block_init, - ivi->new_increment, mul_const, - get_irn_mode(mul_const)); + ivi->cmp_const = new_r_Mul(current_ir_graph, ivi->cmp_init_block, + ivi->cmp_const, mul_const, get_irn_mode(mul_const)); + ivi->increment = new_r_Mul(current_ir_graph, block_inc, + ivi->increment, mul_const, get_irn_mode(mul_const)); + } else { + ivi->new_init = new_r_Mul(current_ir_graph, block_init, + mul_const, ivi->new_init, + get_irn_mode(mul_const)); + ivi->new_increment = new_r_Mul(current_ir_graph, block_inc, + ivi->new_increment, mul_const, + get_irn_mode(mul_const)); } if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) { printf("\nReducing operation is : "); DDMN(reduce_var); @@ -381,8 +439,8 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) } return 1; } - - }else if (get_irn_op (reduce_var) == op_Add){ + } + else if (get_irn_op (reduce_var) == op_Add){ ir_node *add_init = NULL; ir_node *add_const = NULL; @@ -404,24 +462,24 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) add_const = add_left; if (add_const == NULL) return 0; if (ivi->new_phi == NULL){ - ivi->init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init), - add_const, ivi->init); - if(ivi->cmp != NULL) - ivi->cmp_const = my_new_r_Add (current_ir_graph, ivi->cmp_init_block, - add_const, ivi->cmp_const); - } else{ - ivi->new_init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init), - add_const, ivi->new_init); + ivi->init = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->init), + add_const, ivi->init); + if (ivi->cmp != NULL) + ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block, + add_const, ivi->cmp_const); + } else { + ivi->new_init = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->init), + add_const, ivi->new_init); } if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) { printf("\nReducing operation is : "); DDMN(reduce_var); printf("in graph : "); DDMG(current_ir_graph); } return 1; - } else if(get_irn_op(reduce_var) == op_Sub ){ + } + else if (get_irn_op(reduce_var) == op_Sub) { ir_node *sub_init = NULL; ir_node *sub_const = NULL; - // Search for constant of sub. ir_node *sub_right = get_Sub_right(reduce_var); ir_node *sub_left = get_Sub_left(reduce_var); ir_op *sub_right_op = get_irn_op(sub_right); @@ -429,6 +487,7 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) n_reduced_expressions++; + /* Search for constant of Sub. */ if (sub_right_op != op_Const) sub_init = sub_right; else if (sub_left_op != op_Const) @@ -438,13 +497,14 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) else if (sub_left_op == op_Const) sub_const = sub_left; - if (sub_const == NULL) return 0; + if (sub_const == NULL) + return 0; if (ivi->new_phi == NULL) { - ivi->init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init), - ivi->init, sub_const); + ivi->init = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->init), + ivi->init, sub_const); if (ivi->cmp != NULL) - ivi->cmp_const =my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init), + ivi->cmp_const = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->init), ivi->cmp_const,sub_const); } else ivi->new_init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init), @@ -458,6 +518,9 @@ static int reduce(ir_node *reduce_var, induct_var_info *ivi) return 0; } +/** + * What? + */ static ir_node *reducible(ir_node *out, induct_var_info *ivi) { ir_node *reduced = NULL; @@ -474,10 +537,11 @@ static ir_node *reducible(ir_node *out, induct_var_info *ivi) } /** - * Reduce a node. + * Post walker: Find a Phi node that is a iteration variable and + * try to reduce it. * - * @param *itervar_phi The iteration variable of a loop. - * @param *env Free environment pointer. + * @param itervar_phi The iteration variable of a loop. + * @param env Free environment pointer. */ static void reduce_itervar(ir_node *itervar_phi, void *env) { @@ -488,24 +552,26 @@ static void reduce_itervar(ir_node *itervar_phi, void *env) ivi.itervar_phi = itervar_phi; - /* This "if" finds the iteration variable. */ if (is_induction_variable(&ivi)) { int i, op_out; for (i = 0; i < ivi.phi_pred; i++) { ir_node *out = get_irn_out(ivi.itervar_phi, i); ir_op *out_op = get_irn_op(out); - if(ivi.reducible){ - if(ivi.phi_pred == 3 && out != ivi.op && out !=ivi.cmp){ + if (ivi.reducible) { + if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) { ir_node *reduced = reducible(out, &ivi); if (reduced != NULL) exchange( reduced, ivi.itervar_phi); } - } else if (out_op == op_Mul) - if(reduce(out, &ivi) && ivi.reducible){ + } + else if (out_op == op_Mul) + if (reduce(out, &ivi) && ivi.reducible) { ir_node *reduced = reducible(ivi.reducible_node, &ivi); - if(reduced != NULL) + + if (reduced != NULL) exchange(reduced, ivi.new_phi); + ivi.reducible = 0; set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init); set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init)); @@ -517,12 +583,14 @@ static void reduce_itervar(ir_node *itervar_phi, void *env) for (i = 0; i < op_out; i++){ ir_node *out = get_irn_out(ivi.op, i); ir_op *out_op = get_irn_op(out); - if(op_out == 2 && out != ivi.itervar_phi){ + + if (op_out == 2 && out != ivi.itervar_phi){ ir_node *reduced = reducible(out, &ivi); if(reduced != NULL) exchange( reduced, ivi.op); - }else if (out_op == op_Mul) - if(reduce(out, &ivi) && ivi.reducible){ + } + else if (out_op == op_Mul) + if (reduce(out, &ivi) && ivi.reducible){ ir_node *reduced = reducible(ivi.reducible_node, &ivi); if(reduced != NULL) exchange(reduced, ivi.new_phi); @@ -533,7 +601,7 @@ static void reduce_itervar(ir_node *itervar_phi, void *env) } } - if(ivi.reducible){ + if (ivi.reducible) { if(get_irn_op(ivi.op) == op_Add) if(get_Add_left(ivi.op) == ivi.itervar_phi) set_Add_right(ivi.op, ivi.increment); @@ -560,6 +628,7 @@ static void reduce_itervar(ir_node *itervar_phi, void *env) /* Performs strength reduction for the passed graph. */ void reduce_strength(ir_graph *irg) { ir_graph *rem = current_ir_graph; + int modifiyed = 1; if (!get_optimize() || !get_opt_strength_red()) return; @@ -586,5 +655,10 @@ void reduce_strength(ir_graph *irg) { get_entity_name(get_irg_entity(irg))); } + if (modifiyed) { + set_irg_outs_inconsistent(irg); + set_irg_loopinfo_inconsistent(irg); + } + current_ir_graph = rem; }