# include "ircons.h"
# include "irgmod.h"
# include "irdump.h"
+# include "firmstat.h"
/* The information needed for an induction variable */
-struct induct_var_info {
+typedef struct _induct_var_info {
ir_op *operation_code;
ir_node *increment, *init, *op;
int be_pos;
int init_pred_pos;
int op_pred_pos;
-};
-static struct induct_var_info ivi;
+} induct_var_info;
/** Counter for verbose information about optimization. */
static int n_reduced_expressions;
* We could expand this to Phi nodes where all preds are either
* op or loop invariant.
*
- * @param n A phi node.
+ * @param n A phi node.
+ * @param info After call contains the induction variable information
*/
-static struct induct_var_info *is_induction_variable (ir_node *n) {
+static induct_var_info *is_induction_variable (ir_node *n, induct_var_info *info) {
ir_node *phi_pred_0, *phi_pred_1, *add_r, *add_l, *sub_r, *sub_l ;
ir_op *phi_pred_0_op, *phi_pred_1_op;
- struct induct_var_info *info;
- info = &ivi;
info->operation_code = NULL;
- info->increment = NULL;
- info->init = NULL;
- info->op = NULL;
- info->be_pos = -1;
- info->init_pred_pos = -1;
- info->op_pred_pos = -1;
+ info->increment = NULL;
+ info->init = NULL;
+ info->op = NULL;
+ info->be_pos = -1;
+ info->init_pred_pos = -1;
+ info->op_pred_pos = -1;
assert(get_irn_op(n) == op_Phi);
phi_pred_1 = get_Phi_pred(n, 1);
/*The operation of the predecessors. */
- phi_pred_0_op = get_irn_op(get_Phi_pred(n, 0));
- phi_pred_1_op = get_irn_op(get_Phi_pred(n, 1));
+ phi_pred_0_op = get_irn_op(phi_pred_0);
+ phi_pred_1_op = get_irn_op(phi_pred_1);
/*Compute if the induction variable is added or substracted wiht a constant . */
if (phi_pred_0_op == op_Add){
add_l = get_Add_left(phi_pred_0);
add_r = get_Add_right(phi_pred_0);
info->op_pred_pos = 0;
+ info->init_pred_pos = 1;
if (add_l == n){
info->increment = add_r;
} else if (add_r == n){
add_l = get_Add_left(phi_pred_1);
add_r = get_Add_right(phi_pred_1);
info->op_pred_pos = 1;
+ info->init_pred_pos = 0;
if (add_l == n){
info->increment = add_r;
} else if (add_r == n){
sub_r = get_Sub_right(phi_pred_0);
sub_l = get_Sub_left(phi_pred_0);
info->op_pred_pos = 0;
+ info->init_pred_pos = 1;
if (sub_l == n){
info->increment = sub_r;
} else if (sub_r == n){
sub_r = get_Sub_right(phi_pred_1);
sub_l = get_Sub_left(phi_pred_1);
info->op_pred_pos = 1;
+ info->init_pred_pos = 0;
if (sub_l == n){
info->increment = sub_r;
} else return NULL;
return NULL;
/*Compute the position of the backedge. */
- if (is_backedge(get_nodes_block(n), 0)){
- info->be_pos = 0;
- info->init_pred_pos = 1;
- info->op = get_Phi_pred(n, 0);
- info->init = get_Phi_pred(n, 1);
- } else if (is_backedge(get_nodes_block(n), 1)){
- info->be_pos = 1;
- info->init_pred_pos = 0;
- info->op = get_Phi_pred(n, 1);
- info->init = get_Phi_pred(n, 0);
+ if (is_backedge(get_nodes_block(n), info->op_pred_pos)){
+ info->be_pos = info->op_pred_pos;
+ info->op = get_Phi_pred(n, info->op_pred_pos);
+ info->init = get_Phi_pred(n, info->init_pred_pos);
}
- if (info->be_pos == 0) {
- if (get_Block_dom_depth(get_nodes_block(phi_pred_1)) >=
- get_Block_dom_depth(get_nodes_block(n))) {
- return NULL;
- }
- } else if (get_Block_dom_depth(get_nodes_block(phi_pred_0)) >=
- get_Block_dom_depth(get_nodes_block(n))) return NULL;
-
- if (get_Block_dom_depth(get_nodes_block(info->increment)) >=
- get_Block_dom_depth(get_nodes_block(n))) return NULL;
+ if (get_Block_dom_depth(get_nodes_block(info->init)) >=
+ get_Block_dom_depth(get_nodes_block(n))) {
+ return NULL;
+ }
return info;
}
*/
void reduce_itervar(ir_node *itervar_phi, void *env) {
- ir_node *strong = NULL, *cmp = NULL, *c, *cmp_const;
- int phi_pred, strong_in_Phi = 0, cmp_in_phi = 0, out_loop_res = 1;
+ ir_node *strong = NULL, *cmp = NULL, *c, *cmp_const, *old_add;
+ int phi_pred, strong_in_Phi = 0, cmp_in_phi = 0, out_loop_res = 1, Store_in_phi = 0;
+ int op_pred, Store_in_op = 0, strong_in_op = 0;
- // This "if" finds the node to reduce.
+ induct_var_info ivi;
// This "if" finds the Phi predecessors for the node that must be reduced.
if ((get_irn_op(itervar_phi) == op_Phi) &&
- is_induction_variable(itervar_phi) != NULL ) {
+ is_induction_variable(itervar_phi, &ivi) != NULL ) {
phi_pred = get_irn_n_outs(itervar_phi);
ir_loop *l_itervar_phi = get_irn_loop(get_nodes_block(itervar_phi));
-
for (int i = 0; i < phi_pred; i++) {
ir_node *out = get_irn_out(itervar_phi, i);
ir_op *out_op = get_irn_op(out);
if (get_irn_loop(get_nodes_block(out)) != l_itervar_phi)
out_loop_res = 0;
- if (out_op == op_Mul){
+ if ( out_op == op_Store)
+ Store_in_phi++;
+ if (out_op == op_Mul) {
strong = out;
- strong_in_Phi++;
+ strong_in_Phi++;
}else if (out_op == op_Cmp){
cmp = out;
cmp_in_phi++;
}
}
- if (strong == NULL || (strong_in_Phi > 1)) return;
- if(get_irn_op(get_Mul_right(strong)) == op_Phi)
+ if (strong == NULL){
+ op_pred = get_irn_n_outs(ivi.op);
+ for(int i = 0; i < op_pred; i++){
+ ir_node *out = get_irn_out(ivi.op, i);
+ ir_op *out_op = get_irn_op(out);
+ if(out_op == op_Store)
+ Store_in_op++;
+ if(out_op == op_Mul){
+ strong = out;
+ strong_in_op++;
+ }
+ }
+ }
+ if (strong == NULL || (strong_in_Phi > 1) || (strong_in_op > 1)) return;
+
+ if (get_irn_loop(get_nodes_block(strong)) != l_itervar_phi) return;
+
+ ir_op *mul_right_op = get_irn_op(get_Mul_right(strong));
+ if(mul_right_op == op_Phi || mul_right_op == op_Add || mul_right_op == op_Sub)
c = get_Mul_left(strong);
else
c = get_Mul_right(strong);
if (get_Block_dom_depth(get_nodes_block(c)) >=
- get_Block_dom_depth(get_nodes_block(itervar_phi))) return;
+ get_Block_dom_depth(get_nodes_block(itervar_phi))) return;
if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
printf("The constant of Reducing node is: "); DDMN(c);
printf(" in graph "); DDMG(current_ir_graph);
}
- ir_node *inc , *init , *new_phi, *in[2], *new_op = NULL, *block_init, *block_inc;
+ ir_node *inc , *init , *new_phi, *in[2], *new_op = NULL, *block_init;
+ ir_node *block_inc, *new_add, *symconst;
ir_node *init_block = get_nodes_block(ivi.init);
ir_node *increment_block = get_nodes_block(ivi.increment);
else
block_init = c_block;
+ /* we will do a strength reduction */
+ stat_strength_red(current_ir_graph, strong, cmp);
+
/* Compute new loop invariant increment and initialization values. */
inc = new_r_Mul (current_ir_graph, block_inc, ivi.increment, c, get_irn_mode(c));
init = new_r_Mul (current_ir_graph, block_init, ivi.init, c, get_irn_mode(ivi.init));
if (ivi.operation_code == op_Add)
new_op = new_r_Add(current_ir_graph, get_nodes_block(ivi.op), inc, new_phi,
- get_irn_mode(inc));
+ get_irn_mode(inc));
else if (ivi.operation_code == op_Sub)
new_op = new_r_Sub(current_ir_graph, get_nodes_block(ivi.op), new_phi, inc,
- get_irn_mode(inc));
+ get_irn_mode(inc));
set_Phi_pred(new_phi, ivi.op_pred_pos, new_op);
+ if(strong_in_op){
+ if(get_irn_n_outs(strong) > 1) return;
+ else old_add = get_irn_out(strong, 0);
+ if(get_irn_op(old_add) != op_Add) return;
+ if(get_Add_left(old_add) == strong)
+ symconst = get_Add_right(old_add);
+ else
+ symconst = get_Add_left(old_add);
+ new_add = new_r_Add(current_ir_graph, get_nodes_block(old_add),
+ new_op, symconst, get_irn_mode(symconst));
+ exchange(old_add, new_add);
+ }
+
/* Replace the use of the strength reduced value. */
exchange(strong, new_phi);
- if (cmp == NULL || cmp_in_phi > 1 || out_loop_res == 0) return;
+ if (cmp == NULL || cmp_in_phi > 1 || out_loop_res == 0 || Store_in_phi != 0 || Store_in_op != 0) return;
if (get_irn_op(get_Cmp_left(cmp)) == op_Const)
cmp_const = get_Cmp_left(cmp);
if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
printf("It is possibale to exchange the Cmp with a new Cmp \n");
printf("The constant of Cmp node is: "); DDMN(cmp_const);
- printf("The Phi node is"); DDMN(itervar_phi);
+ printf("The Phi node is "); DDMN(itervar_phi);
printf("Cmp node: "); DDMN(cmp);
printf(" in graph "); DDMG(current_ir_graph);
}
block_init = cmp_const_block;
new_cmp_const = new_r_Mul (current_ir_graph, block_init, cmp_const,
- c, get_irn_mode(ivi.init));
+ c, get_irn_mode(ivi.init));
new_cmp = new_r_Cmp (current_ir_graph, get_nodes_block(cmp),
- new_phi, new_cmp_const);
+ new_phi, new_cmp_const);
exchange(cmp, new_cmp);
} else return;
}
if (get_opt_strength_red_verbose())
printf("Reduced %d iteration variables in graph %s.%s\n.", n_reduced_expressions,
- get_type_name(get_entity_owner(get_irg_entity(irg))),
- get_entity_name(get_irg_entity(irg)));
+ get_type_name(get_entity_owner(get_irg_entity(irg))),
+ get_entity_name(get_irg_entity(irg)));
current_ir_graph = rem;
}