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.
16 # include "strength_red.h"
19 # include "irnode_t.h"
21 # include "irloop_t.h"
24 # include "irdump_t.h"
25 # include "firmstat.h"
28 /** Counter for verbose information about optimization. */
29 static int n_reduced_expressions;
30 static int n_made_new_phis;
32 /** Detect basic iteration variables.
34 * The variable is represented by a subgraph as this:
47 * Where op is a Add or Sub node and init is loop invariant.
50 * So far we only accept Phi nodes with two predecessors.
51 * We could expand this to Phi nodes where all predecessors
52 * are either op or loop invariant.
54 * @param info After call contains the induction variable information.
59 induct_var_info *is_induction_variable(induct_var_info *info) {
62 int op_pred, Store_in_op, Store_in_phi;
63 ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
65 ir_node *cmp_const_block;
67 info->operation_code = NULL; /* The opcode of "op". */
68 info->increment = NULL; /* The value which increase or decrease the iteration variable. */
69 info->init = NULL; /* The start value of the iteration variable. */
70 info->op = NULL; /* The operation which increase or decrease the iteration variable. */
71 info->l_itervar_phi = NULL; /* The iteration variable. */
72 info->new_phi = NULL; /* The new iteration variable. */
73 info->new_increment = NULL; /* The new increment which replace the old one.*/
74 info->new_init = NULL; /* The new init value of the iteration variable. */
75 info->new_op = NULL; /* The new operation that we need after replace. */
76 info->new_cmp = NULL; /* The new Cmp which replaces the old one. */
77 info->cmp = NULL; /* The Cmp which breaks the loop and compares the iteration variable with a constant. */
78 info->cmp_const = NULL; /* The other operand of Cmp. */
79 info->cmp_init_block = NULL; /* The initial block of the Cmp. */
80 info->reducible_node = NULL; /* The reducible nodes are save here. */
81 info->is_reducible = 0; /* To save information if anything is reducible. */
82 info->phi_pred = 0; /* To save the value of iteration variable predecessors. */
83 info->init_pred_pos = -1; /* To save the position of iteration variable start value. */
84 info->op_pred_pos = -1; /* To save the backedge of iteration variable. */
85 info->l_itervar_phi = NULL; /* Information about loop of itervar_phi. */
87 assert(get_irn_op(info->itervar_phi) == op_Phi);
90 * The necessary conditions for the phi node:
91 * We can handle currently Phi's with 2 predecessors, one must be a backedge.
93 if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
96 for (i = 0; i < 2; ++i) {
97 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
98 ir_op *op = get_irn_op(pred);
100 /* Compute if the induction variable is added or subtracted with a constant. */
101 if (op == op_Add || op == op_Sub) {
102 ir_node *n_l = get_binop_left(pred);
103 ir_node *n_r = get_binop_right(pred);
105 if (n_l == info->itervar_phi) {
106 info->operation_code = op;
107 info->increment = n_r;
108 info->op_pred_pos = i;
109 info->init_pred_pos = i ^ 1;
112 else if (n_r == info->itervar_phi) {
113 info->operation_code = op;
114 info->increment = n_l;
115 info->op_pred_pos = i;
116 info->init_pred_pos = i ^ 1;
121 /* check if we found something */
122 if (! info->operation_code)
125 /* Compute the position of the backedge. */
126 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
127 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
128 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
131 /* irregular control flow detected. */
136 * the block of the init code should dominate the loop, else
137 * we have an irregular control flow
139 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
140 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
144 op_pred = get_irn_n_outs(info->op);
148 /* Information about loop of itervar_phi. */
149 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
152 info->phi_pred = get_irn_n_outs(info->itervar_phi);
153 loop_head = get_nodes_block(info->itervar_phi);
156 * This "for" searches for the Cmp successor of the
157 * iter_var to reduce and marks if the iter_var have a Store
158 * successor or a successor out of loop.
160 for (i = 0; i < info->phi_pred; i++) {
161 ir_node *out = get_irn_out(info->itervar_phi, i);
162 ir_op *out_op = get_irn_op(out);
164 if (out_op == op_Store)
166 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
167 /* a Cmp can have more than one successor therefore we need this loop.*/
168 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
169 ir_node *proj = get_irn_out(out, q);
171 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
172 cmp_pred_bl = get_irn_out(proj, r);
174 /* The wanted Cmp must be followed by a Cond successor
176 if (get_irn_op(cmp_pred_bl) != op_Cond)
179 /* the binary Cond should have two successors */
180 if (get_irn_n_outs(cmp_pred_bl) != 2)
183 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
184 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
186 if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
187 is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
188 if (get_Cmp_left(out) == info->itervar_phi)
189 cmp_const = get_Cmp_right(out);
191 cmp_const = get_Cmp_left(out);
194 if (info->cmp == NULL) {
197 info->cmp_const = cmp_const;
200 /* We have more then one cmp with our requests, that mean cmp isn't found */
209 for (i = 0; i < op_pred; ++i) {
210 ir_node *out = get_irn_out(info->op, i);
211 ir_op *out_op = get_irn_op(out);
213 if (out_op == op_Store)
215 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
216 /* a Cmp can have more as one successor therefore
217 I need this for loop. */
218 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
219 ir_node *proj = get_irn_out(out, q);
221 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
222 cmp_pred_bl = get_irn_out(proj, r);
224 /* The wanted Cmp must be followed by a Cond successor. */
225 if (get_irn_op(cmp_pred_bl) != op_Cond)
228 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
229 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
231 if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
232 is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
233 if (get_Cmp_left(out) == info->op)
234 cmp_const = get_Cmp_right(out);
236 cmp_const = get_Cmp_left(out);
239 if (info->cmp == NULL) {
242 info->cmp_const = cmp_const;
245 /* We have more then one cmp with our requests, that mean cmp isn't found*/
254 if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
255 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
256 (info->phi_pred == 1 && Store_in_op == 0))
257 info->is_reducible = 1;
259 /* Search for loop invariant of Cmp.*/
260 if (info->cmp != NULL) {
261 cmp_const_block = get_nodes_block(info->cmp_const);
262 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
263 get_Block_dom_depth(cmp_const_block))
264 info->cmp_init_block = get_nodes_block(info->init);
266 info->cmp_init_block = cmp_const_block;
272 * Creates a new Add node with the correct mode from its two operands.
274 static INLINE ir_node *
275 my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
276 ir_mode *m = get_irn_mode(op1);
277 ir_mode *m2 = get_irn_mode(op2);
279 if (mode_is_reference(m2))
282 return new_r_Add(irg, b, op1, op2, m);
286 * Creates a new Sub node with the correct mode from its two operands.
288 static INLINE ir_node *
289 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
290 ir_mode *m = get_irn_mode(op1);
291 ir_mode *m2 = get_irn_mode(op2);
293 if (mode_is_reference(m) && mode_is_reference(m2))
294 m = mode_Is; /* FIXME: may be other mode! */
295 else if (mode_is_reference(m2))
297 return new_r_Sub(irg, b, op1, op2, m);
301 * Reduce an Add, Sub or Mul node
303 * @param *reduce_var The node to reduce.
304 * @param *ivi Contains the induction variable information.
306 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
308 ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
310 /* Essential conditions for a reducible node. */
311 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
314 iter_varblk = get_nodes_block(ivi->itervar_phi);
315 init_block = get_nodes_block(ivi->init);
316 irg_startblk = get_irg_start_block(current_ir_graph);
318 /* The "new_init" and the "new_cmp_const" must not be in the start block.*/
319 if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
320 init_block != iter_varblk)
321 block_init = init_block;
323 block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
325 /* To avoid that cmp is placed in the start block.*/
326 if (ivi->cmp_init_block == irg_startblk)
327 ivi->cmp_init_block = iter_varblk;
329 if (get_irn_op(reduce_var) == op_Mul) {
330 ir_node *mul_init = NULL;
331 ir_node *mul_const = NULL;
333 /* Search for constant and init of strong. */
334 ir_node *mul_right = get_Mul_right(reduce_var);
335 ir_node *mul_left = get_Mul_left(reduce_var);
336 ir_op *mul_right_op = get_irn_op(mul_right);
337 ir_op *mul_left_op = get_irn_op(mul_left);
342 ir_node *increment_block;
345 n_reduced_expressions++;
347 if (mul_right_op == op_Const) {
348 mul_const = mul_right;
351 else if (mul_left_op == op_Const) {
352 mul_const = mul_left;
353 mul_init = mul_right;
356 if (mul_const == NULL || mul_init == NULL)
359 increment_block = get_nodes_block(ivi->increment);
360 c_block = get_nodes_block(mul_const);
362 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
363 block_inc = increment_block;
367 if (! ivi->is_reducible){
370 /* Essential condition for the constant of strong. */
371 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
372 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
376 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
377 printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
378 printf("reducing operation is : "); DDMN(reduce_var);
379 printf("in graph : "); DDMG(current_ir_graph);
382 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
383 get_irn_mode(mul_const));
384 if (!(get_irn_op(mul_init) == op_Phi)){
385 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
386 get_irn_mode(mul_const));
387 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
390 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
391 get_irn_mode(mul_const));
393 /* Generate a new basic induction variable. Break the data flow loop
394 initially by using an Unknown node. */
396 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
398 in[ivi->init_pred_pos] = ivi->new_init;
399 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
400 get_irn_mode(mul_const));
401 mark_irn_visited(ivi->new_phi);
403 if (ivi->operation_code == op_Add)
404 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
405 ivi->new_increment,ivi-> new_phi);
406 else if (ivi->operation_code == op_Sub)
407 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
410 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
412 // This for search for a reducible successor of reduc_var.
413 reduce_var_pred = get_irn_n_outs(reduce_var);
414 if (reduce_var_pred == 1) {
415 ir_node *old_ind =get_irn_out(reduce_var, 0);
416 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
417 get_irn_op(old_ind) == op_Mul){
418 ivi->is_reducible = 1;
419 ivi->reducible_node = old_ind;
422 /* Replace the use of the strength reduced value. */
423 exchange(reduce_var, ivi->new_phi);
426 else { /* ivi->is_reducible */
427 if (ivi->new_phi == NULL) {
428 ivi->init = new_r_Mul (current_ir_graph, block_init,
429 mul_const, ivi->init,
430 get_irn_mode(mul_const));
432 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
433 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
434 ivi->increment = new_r_Mul (current_ir_graph, block_inc,
435 ivi->increment, mul_const, get_irn_mode(mul_const));
438 ivi->new_init = new_r_Mul (current_ir_graph, block_init,
439 mul_const, ivi->new_init,
440 get_irn_mode(mul_const));
441 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
442 ivi->new_increment, mul_const,
443 get_irn_mode(mul_const));
445 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
446 printf("\nReducing operation is : "); DDMN(reduce_var);
447 printf("in graph : "); DDMG(current_ir_graph);
452 else if (get_irn_op (reduce_var) == op_Add){
453 ir_node *add_init = NULL;
454 ir_node *add_const = NULL;
456 /* Search for constant of add. */
457 ir_node *add_right = get_Add_right(reduce_var);
458 ir_node *add_left = get_Add_left(reduce_var);
459 ir_op *add_right_op = get_irn_op(add_right);
460 ir_op *add_left_op = get_irn_op(add_left);
462 ++n_reduced_expressions;
464 if (add_right_op != op_Const)
465 add_init = add_right;
466 else if (add_left_op != op_Const)
468 if (add_right_op == op_Const || add_right_op == op_SymConst)
469 add_const = add_right;
470 else if (add_left_op == op_Const || add_left_op == op_SymConst)
471 add_const = add_left;
472 if (add_const == NULL)
474 if (ivi->new_phi == NULL) {
475 ivi->init = my_new_r_Add(current_ir_graph, block_init,
476 add_const, ivi->init);
477 if (ivi->cmp != NULL)
478 ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
479 add_const, ivi->cmp_const);
482 ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
483 add_const, ivi->new_init);
485 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
486 printf("\nReducing operation is : "); DDMN(reduce_var);
487 printf("in graph : "); DDMG(current_ir_graph);
491 else if (get_irn_op(reduce_var) == op_Sub) {
492 ir_node *sub_init = NULL;
493 ir_node *sub_const = NULL;
494 ir_node *sub_right = get_Sub_right(reduce_var);
495 ir_node *sub_left = get_Sub_left(reduce_var);
496 ir_op *sub_right_op = get_irn_op(sub_right);
497 ir_op *sub_left_op = get_irn_op(sub_left);
499 ++n_reduced_expressions;
501 /* Search for constant of Sub. */
502 if (sub_right_op != op_Const)
503 sub_init = sub_right;
504 else if (sub_left_op != op_Const)
506 if (sub_right_op == op_Const)
507 sub_const = sub_right;
508 else if (sub_left_op == op_Const)
509 sub_const = sub_left;
511 if (sub_const == NULL)
514 if (ivi->new_phi == NULL) {
515 ivi->init = my_new_r_Sub(current_ir_graph, block_init,
516 ivi->init, sub_const);
517 if (ivi->cmp != NULL)
518 ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
519 ivi->cmp_const,sub_const);
521 ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
522 ivi->new_init, sub_const);
523 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
524 printf("\nReducing operation is : "); DDMN(reduce_var);
525 printf("in graph : "); DDMG(current_ir_graph);
533 * Search for reducible successor of iteration variable.
534 * If such successor is found it will be reduced and returned,
537 * @param ivi Contains information about the induction variable.
538 * @param out A successor of iteration variable.
540 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
542 ir_node *reduced = NULL;
545 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
546 if (reduce(out, ivi))
550 out = get_irn_out(out, 0);
556 * Post walker: Find a Phi node that is a iteration variable and
559 * @param itervar_phi The iteration variable of a loop.
560 * @param env Free environment pointer.
562 static void reduce_itervar(ir_node *itervar_phi, void *env)
565 /* check if a iteration variable be reduced.*/
568 if (get_irn_op(itervar_phi) != op_Phi)
570 /* A candidate is found.*/
571 ivi.itervar_phi = itervar_phi;
573 /* It musss be a induction variable.*/
574 if (is_induction_variable(&ivi)) {
577 for (i = 0; i < ivi.phi_pred; i++) {
578 ir_node *out = get_irn_out(ivi.itervar_phi, i);
579 ir_op *out_op = get_irn_op(out);
580 /* Reduce a induction variable.*/
581 if (ivi.is_reducible) {
582 if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
583 ir_node *irn_reduced = reducible(out, &ivi);
584 if (irn_reduced != NULL){
586 exchange(irn_reduced, ivi.itervar_phi);
590 /* Reduce a multiplication*/
591 else if (out_op == op_Mul)
592 if (reduce(out, &ivi) && ivi.is_reducible) {
593 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
596 exchange(reduced, ivi.new_phi);
598 ivi.is_reducible = 0;
599 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
600 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
601 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
605 op_out = get_irn_n_outs(ivi.op);
606 for (i = 0; i < op_out; i++){
607 ir_node *out = get_irn_out(ivi.op, i);
608 ir_op *out_op = get_irn_op(out);
609 /* Try to reduce the second successor of the "ivi.op"*/
610 if (op_out == 2 && out != ivi.itervar_phi){
611 ir_node *reduced = reducible(out, &ivi);
613 exchange( reduced, ivi.op);
615 /* Try to reduce a multiplication, that is successor of "ivi.op".*/
616 else if (out_op == op_Mul)
617 if (reduce(out, &ivi) && ivi.is_reducible){
618 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
620 exchange(reduced, ivi.new_phi);
621 ivi.is_reducible = 0;
622 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
623 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
624 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
627 /* Set some predecessors and modes after reduce.*/
628 if (ivi.is_reducible && reduced) {
629 if(get_irn_op(ivi.op) == op_Add)
630 if(get_Add_left(ivi.op) == ivi.itervar_phi)
631 set_Add_right(ivi.op, ivi.increment);
633 set_Add_left(ivi.op, ivi.increment);
634 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
635 set_Sub_right(ivi.op, ivi.increment);
637 set_Sub_right(ivi.op, ivi.increment);
638 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
639 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
640 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
641 if (ivi.cmp != NULL){
642 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
643 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
644 set_Cmp_right(ivi.cmp, ivi.cmp_const);
646 set_Cmp_left(ivi.cmp, ivi.cmp_const);
652 /* Performs strength reduction for the passed graph. */
653 void reduce_strength(ir_graph *irg) {
654 ir_graph *rem = current_ir_graph;
657 if (!get_optimize() || !get_opt_strength_red()) return;
659 current_ir_graph = irg;
661 n_reduced_expressions = 0;
663 /* -- Precompute some information -- */
664 /* Call algorithm that computes the backedges */
665 construct_cf_backedges(irg);
666 /* Call algorithm that computes the dominator trees. */
668 /* Call algorithm that computes the out edges */
669 assure_irg_outs(irg);
671 /* -- Search expressions that can be optimized -- */
672 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
674 if (get_opt_strength_red_verbose()) {
675 printf ("\n %d made new_phis und ", n_made_new_phis);
676 printf("reduced %d iteration variables "
677 "in \n graph %s.%s.\n", n_reduced_expressions,
678 get_type_name(get_entity_owner(get_irg_entity(irg))),
679 get_entity_name(get_irg_entity(irg)));
683 set_irg_outs_inconsistent(irg);
684 set_irg_loopinfo_inconsistent(irg);
687 current_ir_graph = rem;