6 * File name: ir/opt/strength_red.c
7 * Purpose: Make strength reduction .
8 * Author: Beyhan Veliev
12 * Copyright: (c) 2004 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 reduce_itervar(induct_var_info *iv)
24 for each (out o of iv) {
26 if (o is strong (Mul))
27 iv_new = reduce(o), remember_pattern(o)
28 else // o is not strong (Add ...)
29 if (o is the only user)
36 # include "strength_red.h"
39 # include "irnode_t.h"
41 # include "irloop_t.h"
44 # include "irdump_t.h"
45 # include "firmstat.h"
48 /** Counter for verbose information about optimization. */
49 static int n_reduced_expressions;
50 static int n_made_new_phis;
52 /** Detect basic iteration variables.
54 * The variable ir represented by a subgraph as this:
66 * Where op is a Add or Sub, and init is loop invariant.
69 * So far we only accept Phi nodes with two predecessors.
70 * We could expand this to Phi nodes where all preds are either
71 * op or loop invariant.
73 * @param info After call contains the induction variable information.
75 induct_var_info *is_induction_variable (induct_var_info *info) {
78 int op_pred, Store_in_op, Store_in_phi;
79 ir_node *cmp_pred_bl, *cond_succ_true, *cond_succ_false, *cmp_const;
81 ir_node *cmp_const_block;
85 info->cmp_const = NULL;
86 info->cmp_init_block = NULL;
87 info->increment = NULL;
89 info->l_itervar_phi = NULL;
92 info->new_increment = NULL;
93 info->new_init = NULL;
96 info->operation_code = NULL;
99 info->reducible_node = NULL;
100 info->out_loop_res = 1;
103 info->strong_reduced = 0;
104 info->init_pred_pos = -1;
105 info->op_pred_pos = -1;
107 assert(get_irn_op(info->itervar_phi) == op_Phi);
109 /* The necessary conditions for the phi node. */
110 if (get_irn_arity(info->itervar_phi) != 2 ||
111 !has_backedges(get_nodes_block(info->itervar_phi)) )
114 for (i = 0; i < 2; ++i) {
115 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
116 ir_op *op = get_irn_op(pred);
118 /* Compute if the induction variable is added or substracted with a constant . */
119 if (op == op_Add || op == op_Sub) {
120 ir_node *n_l = get_binop_left(pred);
121 ir_node *n_r = get_binop_right(pred);
123 if (n_l == info->itervar_phi) {
124 info->operation_code = op;
125 info->increment = n_r;
126 info->op_pred_pos = i;
127 info->init_pred_pos = i ^ 1;
130 else if (n_r == info->itervar_phi) {
131 info->operation_code = op;
132 info->increment = n_l;
133 info->op_pred_pos = i;
134 info->init_pred_pos = i ^ 1;
139 /* check if we found something */
140 if (! info->operation_code)
143 /* Compute the position of the backedge. */
144 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)){
145 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
146 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
149 /* irregular control flow detected. */
153 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
154 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
158 /* This "for" marks if the iteration operation have a Store successor .*/
159 op_pred = get_irn_n_outs(info->op);
163 /* Information about loop of itervar_phi. */
164 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
166 /* This "for" searches for the Cmp successor of the
167 iter_var to reduce and marks if the iter_var have a Store
168 successor or a successor out of loop.*/
169 info->phi_pred = get_irn_n_outs(info->itervar_phi);
170 loop_head = get_nodes_block(info->itervar_phi);
172 for (i = 0; i < info->phi_pred; i++) {
173 ir_node *out = get_irn_out(info->itervar_phi, i);
174 ir_op *out_op = get_irn_op(out);
175 if ((get_irn_loop(get_nodes_block(out)) != info->l_itervar_phi) &&
176 ( get_Block_dom_depth(get_nodes_block(out)) >
177 get_Block_dom_depth(get_nodes_block(info->itervar_phi))))
178 info->out_loop_res = 0;
180 if (out_op == op_Store)
183 if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)){
184 cmp_pred_bl = get_irn_out(out, 0);
185 cmp_pred_bl = get_irn_out(cmp_pred_bl, 0);
186 cond_succ_true = get_irn_out(cmp_pred_bl, 1);
187 cond_succ_false = get_irn_out(cmp_pred_bl, 0);
188 if(is_loop_invariant(get_irn_out(cond_succ_false, 0), loop_head) ||
189 is_loop_invariant(get_irn_out(cond_succ_true, 0), loop_head)){
190 if (get_Cmp_left(out) == info->itervar_phi)
191 cmp_const = get_Cmp_right(out);
193 cmp_const = get_Cmp_left(out);
196 if (info->cmp == NULL) {
198 info->cmp_const = cmp_const;
207 for (i = 0; i < op_pred; ++i){
208 ir_node *out = get_irn_out(info->op, i);
209 ir_op *out_op = get_irn_op(out);
211 if (out_op == op_Store)
214 if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)){
215 cmp_pred_bl = get_irn_out(out, 0);
216 cmp_pred_bl = get_irn_out(cmp_pred_bl, 0);
217 cond_succ_true = get_irn_out(cmp_pred_bl, 1);
218 cond_succ_false = get_irn_out(cmp_pred_bl, 0);
219 if(is_loop_invariant(get_irn_out(cond_succ_false, 0), loop_head) ||
220 is_loop_invariant(get_irn_out(cond_succ_true, 0), loop_head)){
221 if (get_Cmp_left(out) == info->op)
222 cmp_const = get_Cmp_right(out);
224 cmp_const = get_Cmp_left(out);
227 if (info->cmp == NULL) {
229 info->cmp_const = cmp_const;
230 set_irn_link(info->cmp_const, (void *) 1);
240 if((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
241 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
242 (info->phi_pred == 1 && Store_in_op == 0))
245 // Search for loop invariant of Cmp.
246 if(info->cmp != NULL){
247 cmp_const_block = get_nodes_block(info->cmp_const);
248 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
249 get_Block_dom_depth(cmp_const_block))
250 info->cmp_init_block = get_nodes_block(info->init);
252 info->cmp_init_block = cmp_const_block;
258 * Creates a new Add node from operands.
260 static INLINE ir_node *
261 my_new_r_Add (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
262 ir_mode *m = get_irn_mode(op1);
263 ir_mode *m2 = get_irn_mode(op2);
265 if (mode_is_reference(m2))
267 return new_r_Add(irg, b, op1, op2, m);
271 * Creates a new Sub node from operands.
273 static INLINE ir_node *
274 my_new_r_Sub (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
275 ir_mode *m = get_irn_mode(op1);
276 ir_mode *m2 = get_irn_mode(op2);
278 if (mode_is_reference(m) && mode_is_reference(m2))
279 m = mode_Is; /* FIXME: may be other mode! */
280 else if (mode_is_reference(m2))
282 return new_r_Sub(irg, b, op1, op2, m);
285 /* Reduce a Add, Sub or Mul node
287 * @param *reduce_var The node to reduce.
288 * @param *ivi Contains the induction variable information.
290 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
292 /* Essential conditions for a reducible node. */
293 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
296 if (get_irn_op(reduce_var) == op_Mul) {
297 ir_node *mul_init = NULL;
298 ir_node *mul_const = NULL;
300 /* Search for constant and init of strong. */
301 ir_node *mul_right = get_Mul_right(reduce_var);
302 ir_node *mul_left = get_Mul_left(reduce_var);
303 ir_op *mul_right_op = get_irn_op(mul_right);
304 ir_op *mul_left_op = get_irn_op(mul_left);
306 ir_node *in[2], *block_init;
310 ir_node *increment_block;
313 n_reduced_expressions++;
315 if (mul_right_op == op_Const) {
316 mul_const = mul_right;
319 else if (mul_left_op == op_Const) {
320 mul_const = mul_left;
321 mul_init = mul_right;
324 if (mul_const == NULL || mul_init == NULL)
327 init_block = get_nodes_block(mul_init);
328 increment_block = get_nodes_block(ivi->increment);
329 c_block = get_nodes_block(mul_const);
331 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
332 block_inc = increment_block;
336 if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(c_block))
337 block_init = init_block;
339 block_init = c_block;
341 if (! ivi->reducible){
344 /* Essential condition for the constant of strong. */
345 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
346 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
350 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
351 printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
352 printf("reducing operation is : "); DDMN(reduce_var);
353 printf("in graph : "); DDMG(current_ir_graph);
356 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
357 get_irn_mode(mul_const));
358 if (!(get_irn_op(mul_init) == op_Phi)){
359 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
360 get_irn_mode(mul_const));
361 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
364 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
365 get_irn_mode(mul_const));
367 /* Generate a new basic induction variable. Break the data flow loop
368 initially by using an Unknown node. */
370 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
372 in[ivi->init_pred_pos] = ivi->new_init;
373 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
374 get_irn_mode(mul_const));
375 mark_irn_visited(ivi->new_phi);
377 if (ivi->operation_code == op_Add)
378 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
379 ivi->new_increment,ivi-> new_phi);
380 else if (ivi->operation_code == op_Sub)
381 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
384 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
390 // This for search for a reducible successor of reduc_var.
391 reduce_var_pred = get_irn_n_outs(reduce_var);
392 if (reduce_var_pred == 1) {
393 ir_node *old_ind =get_irn_out(reduce_var, 0);
394 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
395 get_irn_op(old_ind) == op_Mul){
397 ivi->reducible_node = old_ind;
400 /* Replace the use of the strength reduced value. */
401 exchange(reduce_var, ivi->new_phi);
404 else { /* ivi->reducible */
405 if(ivi->new_phi == NULL){
406 ivi->init = new_r_Mul (current_ir_graph, get_nodes_block(ivi->init),
407 mul_const, ivi->init,
408 get_irn_mode(mul_const));
410 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
411 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
412 ivi->increment = new_r_Mul (current_ir_graph, block_init,
413 ivi->increment, mul_const, get_irn_mode(mul_const));
415 ivi->new_init = new_r_Mul (current_ir_graph, get_nodes_block(ivi->init),
416 mul_const, ivi->new_init,
417 get_irn_mode(mul_const));
418 ivi->new_increment = new_r_Mul (current_ir_graph, block_init,
419 ivi->new_increment, mul_const,
420 get_irn_mode(mul_const));
422 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
423 printf("\nReducing operation is : "); DDMN(reduce_var);
424 printf("in graph : "); DDMG(current_ir_graph);
429 } else if (get_irn_op (reduce_var) == op_Add) {
430 ir_node *add_init = NULL;
431 ir_node *add_const = NULL;
433 // Search for constant of add.
434 ir_node *add_right = get_Add_right(reduce_var);
435 ir_node *add_left = get_Add_left(reduce_var);
436 ir_op *add_right_op = get_irn_op(add_right);
437 ir_op *add_left_op = get_irn_op(add_left);
439 n_reduced_expressions++;
441 if (add_right_op != op_Const)
442 add_init = add_right;
443 else if (add_left_op != op_Const)
445 if (add_right_op == op_Const || add_right_op == op_SymConst)
446 add_const = add_right;
447 else if (add_left_op == op_Const || add_left_op == op_SymConst)
448 add_const = add_left;
449 if (add_const == NULL) return 0;
450 if (ivi->new_phi == NULL){
451 ivi->init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init),
452 add_const, ivi->init);
454 ivi->cmp_const = my_new_r_Add (current_ir_graph, ivi->cmp_init_block,
455 add_const, ivi->cmp_const);
457 ivi->new_init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init),
458 add_const, ivi->new_init);
460 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
461 printf("\nReducing operation is : "); DDMN(reduce_var);
462 printf("in graph : "); DDMG(current_ir_graph);
465 } else if(get_irn_op(reduce_var) == op_Sub ){
466 ir_node *sub_init = NULL;
467 ir_node *sub_const = NULL;
468 // Search for constant of sub.
469 ir_node *sub_right = get_Sub_right(reduce_var);
470 ir_node *sub_left = get_Sub_left(reduce_var);
471 ir_op *sub_right_op = get_irn_op(sub_right);
472 ir_op *sub_left_op = get_irn_op(sub_left);
474 n_reduced_expressions++;
476 if (sub_right_op != op_Const)
477 sub_init = sub_right;
478 else if (sub_left_op != op_Const)
480 if (sub_right_op == op_Const)
481 sub_const = sub_right;
482 else if (sub_left_op == op_Const)
483 sub_const = sub_left;
485 if (sub_const == NULL) return 0;
487 if (ivi->new_phi == NULL) {
488 ivi->init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
489 ivi->init, sub_const);
490 if (ivi->cmp != NULL)
491 ivi->cmp_const =my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
492 ivi->cmp_const,sub_const);
494 ivi->new_init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
495 ivi->new_init, sub_const);
496 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
497 printf("\nReducing operation is : "); DDMN(reduce_var);
498 printf("in graph : "); DDMG(current_ir_graph);
505 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
507 ir_node *reduced = NULL;
510 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
511 if (reduce(out, ivi))
515 out = get_irn_out(out, 0);
523 * @param *itervar_phi The iteration variable of a loop.
524 * @param *env Free environment pointer.
526 static void reduce_itervar(ir_node *itervar_phi, void *env)
530 if (get_irn_op(itervar_phi) != op_Phi)
533 ivi.itervar_phi = itervar_phi;
535 /* This "if" finds the iteration variable. */
536 if (is_induction_variable(&ivi)) {
539 for (i = 0; i < ivi.phi_pred; i++) {
540 ir_node *out = get_irn_out(ivi.itervar_phi, i);
541 ir_op *out_op = get_irn_op(out);
543 if(ivi.phi_pred == 3 && out != ivi.op && out !=ivi.cmp){
544 ir_node *reduced = reducible(out, &ivi);
546 exchange( reduced, ivi.itervar_phi);
548 } else if (out_op == op_Mul)
549 if(reduce(out, &ivi) && ivi.reducible){
550 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
552 exchange(reduced, ivi.new_phi);
554 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
555 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
556 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
560 op_out = get_irn_n_outs(ivi.op);
561 for (i = 0; i < op_out; i++){
562 ir_node *out = get_irn_out(ivi.op, i);
563 ir_op *out_op = get_irn_op(out);
564 if(op_out == 2 && out != ivi.itervar_phi){
565 ir_node *reduced = reducible(out, &ivi);
567 exchange( reduced, ivi.op);
568 }else if (out_op == op_Mul)
569 if(reduce(out, &ivi) && ivi.reducible){
570 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
572 exchange(reduced, ivi.new_phi);
574 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
575 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
576 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
581 if(get_irn_op(ivi.op) == op_Add)
582 if(get_Add_left(ivi.op) == ivi.itervar_phi)
583 set_Add_right(ivi.op, ivi.increment);
585 set_Add_left(ivi.op, ivi.increment);
586 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
587 set_Sub_right(ivi.op, ivi.increment);
589 set_Sub_right(ivi.op, ivi.increment);
590 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
591 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
592 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
593 if (ivi.cmp != NULL){
594 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
595 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
596 set_Cmp_right(ivi.cmp, ivi.cmp_const);
598 set_Cmp_left(ivi.cmp, ivi.cmp_const);
604 /* Performs strength reduction for the passed graph. */
605 void reduce_strength(ir_graph *irg) {
606 ir_graph *rem = current_ir_graph;
608 if (!get_optimize() || !get_opt_strength_red()) return;
610 current_ir_graph = irg;
612 n_reduced_expressions = 0;
614 /* -- Precompute some information -- */
615 /* Call algorithm that computes the backedges */
616 construct_cf_backedges(irg);
617 /* Call algorithm that computes the dominator trees. */
619 /* Call algorithm that computes the out edges */
622 /* -- Search expressions that can be optimized -- */
623 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
625 if (get_opt_strength_red_verbose()) {
626 printf ("\n %d made new_phis und ", n_made_new_phis);
627 printf("reduced %d iteration variables "
628 "in \n graph %s.%s.\n", n_reduced_expressions,
629 get_type_name(get_entity_owner(get_irg_entity(irg))),
630 get_entity_name(get_irg_entity(irg)));
633 current_ir_graph = rem;