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.
19 # include "strength_red.h"
22 # include "irnode_t.h"
24 # include "irloop_t.h"
27 # include "irdump_t.h"
28 # include "firmstat.h"
31 /** Counter for verbose information about optimization. */
32 static int n_reduced_expressions;
33 static int n_made_new_phis;
35 /** Detect basic iteration variables.
37 * The variable is represented by a subgraph as this:
50 * Where op is a Add or Sub node and init is loop invariant.
53 * So far we only accept Phi nodes with two predecessors.
54 * We could expand this to Phi nodes where all predecessors
55 * are either op or loop invariant.
57 * @param info After call contains the induction variable information.
62 induct_var_info *is_induction_variable(induct_var_info *info) {
65 int op_pred, Store_in_op, Store_in_phi;
66 ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
68 ir_node *cmp_const_block;
70 info->operation_code = NULL; /* The opcode of "op". */
71 info->increment = NULL; /* The value which increase or decrease the iteration variable. */
72 info->init = NULL; /* The start value of the iteration variable. */
73 info->op = NULL; /* The operation which increase or decrease the iteration variable. */
74 info->l_itervar_phi = NULL; /* The iteration variable. */
75 info->new_phi = NULL; /* The new iteration variable. */
76 info->new_increment = NULL; /* The new increment which replace the old one.*/
77 info->new_init = NULL; /* The new init value of the iteration variable. */
78 info->new_op = NULL; /* The new operation that we need after replace. */
79 info->new_cmp = NULL; /* The new Cmp which replaces the old one. */
80 info->cmp = NULL; /* The Cmp which breaks the loop and compares the iteration variable with a constant. */
81 info->cmp_const = NULL; /* The other operand of Cmp. */
82 info->cmp_init_block = NULL; /* The initial block of the Cmp. */
83 info->reducible_node = NULL; /* The reducible nodes are save here. */
84 info->is_reducible = 0; /* To save information if anything is reducible. */
85 info->phi_pred = 0; /* To save the value of iteration variable predecessors. */
86 info->init_pred_pos = -1; /* To save the position of iteration variable start value. */
87 info->op_pred_pos = -1; /* To save the backedge of iteration variable. */
88 info->l_itervar_phi = NULL; /* Information about loop of itervar_phi. */
90 assert(get_irn_op(info->itervar_phi) == op_Phi);
93 * The necessary conditions for the phi node:
94 * We can handle currently Phi's with 2 predecessors, one must be a backedge.
96 if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
99 for (i = 0; i < 2; ++i) {
100 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
101 ir_op *op = get_irn_op(pred);
103 /* Compute if the induction variable is added or subtracted with a constant. */
104 if (op == op_Add || op == op_Sub) {
105 ir_node *n_l = get_binop_left(pred);
106 ir_node *n_r = get_binop_right(pred);
108 if (n_l == info->itervar_phi) {
109 info->operation_code = op;
110 info->increment = n_r;
111 info->op_pred_pos = i;
112 info->init_pred_pos = i ^ 1;
115 else if (n_r == info->itervar_phi) {
116 info->operation_code = op;
117 info->increment = n_l;
118 info->op_pred_pos = i;
119 info->init_pred_pos = i ^ 1;
124 /* check if we found something */
125 if (! info->operation_code)
128 /* Compute the position of the backedge. */
129 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
130 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
131 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
134 /* irregular control flow detected. */
139 * the block of the init code should dominate the loop, else
140 * we have an irregular control flow
142 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
143 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
147 op_pred = get_irn_n_outs(info->op);
151 /* Information about loop of itervar_phi. */
152 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
155 info->phi_pred = get_irn_n_outs(info->itervar_phi);
156 loop_head = get_nodes_block(info->itervar_phi);
159 * This "for" searches for the Cmp successor of the
160 * iter_var to reduce and marks if the iter_var have a Store
161 * successor or a successor out of loop.
163 for (i = 0; i < info->phi_pred; i++) {
164 ir_node *out = get_irn_out(info->itervar_phi, i);
165 ir_op *out_op = get_irn_op(out);
167 if (out_op == op_Store)
169 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
170 /* a Cmp can have more than one successor therefore we need this loop.*/
171 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
172 ir_node *proj = get_irn_out(out, q);
174 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
175 cmp_pred_bl = get_irn_out(proj, r);
177 /* The wanted Cmp must be followed by a Cond successor
179 if (get_irn_op(cmp_pred_bl) != op_Cond)
182 /* the binary Cond should have two successors */
183 if (get_irn_n_outs(cmp_pred_bl) != 2)
186 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
187 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
189 if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
190 is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
191 if (get_Cmp_left(out) == info->itervar_phi)
192 cmp_const = get_Cmp_right(out);
194 cmp_const = get_Cmp_left(out);
197 if (info->cmp == NULL) {
200 info->cmp_const = cmp_const;
203 /* We have more then one cmp with our requests, that mean cmp isn't found */
212 for (i = 0; i < op_pred; ++i) {
213 ir_node *out = get_irn_out(info->op, i);
214 ir_op *out_op = get_irn_op(out);
216 if (out_op == op_Store)
218 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
219 /* a Cmp can have more as one successor therefore
220 I need this for loop. */
221 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
222 ir_node *proj = get_irn_out(out, q);
224 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
225 cmp_pred_bl = get_irn_out(proj, r);
227 /* The wanted Cmp must be followed by a Cond successor. */
228 if (get_irn_op(cmp_pred_bl) != op_Cond)
231 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
232 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
234 if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
235 is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
236 if (get_Cmp_left(out) == info->op)
237 cmp_const = get_Cmp_right(out);
239 cmp_const = get_Cmp_left(out);
242 if (info->cmp == NULL) {
245 info->cmp_const = cmp_const;
248 /* We have more then one cmp with our requests, that mean cmp isn't found*/
257 if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
258 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
259 (info->phi_pred == 1 && Store_in_op == 0))
260 info->is_reducible = 1;
262 /* Search for loop invariant of Cmp.*/
263 if (info->cmp != NULL) {
264 cmp_const_block = get_nodes_block(info->cmp_const);
265 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
266 get_Block_dom_depth(cmp_const_block))
267 info->cmp_init_block = get_nodes_block(info->init);
269 info->cmp_init_block = cmp_const_block;
275 * Creates a new Add node with the correct mode from its two operands.
277 static INLINE ir_node *
278 my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
279 ir_mode *m = get_irn_mode(op1);
280 ir_mode *m2 = get_irn_mode(op2);
282 if (mode_is_reference(m2))
285 return new_r_Add(irg, b, op1, op2, m);
289 * Creates a new Sub node with the correct mode from its two operands.
291 static INLINE ir_node *
292 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
293 ir_mode *m = get_irn_mode(op1);
294 ir_mode *m2 = get_irn_mode(op2);
296 if (mode_is_reference(m) && mode_is_reference(m2))
297 m = mode_Is; /* FIXME: may be other mode! */
298 else if (mode_is_reference(m2))
300 return new_r_Sub(irg, b, op1, op2, m);
304 * Reduce an Add, Sub or Mul node
306 * @param *reduce_var The node to reduce.
307 * @param *ivi Contains the induction variable information.
309 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
311 ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
313 /* Essential conditions for a reducible node. */
314 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
317 iter_varblk = get_nodes_block(ivi->itervar_phi);
318 init_block = get_nodes_block(ivi->init);
319 irg_startblk = get_irg_start_block(current_ir_graph);
321 /* The "new_init" and the "new_cmp_const" must not be in the start block.*/
322 if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
323 init_block != iter_varblk)
324 block_init = init_block;
326 block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
328 /* To avoid that cmp is placed in the start block.*/
329 if (ivi->cmp_init_block == irg_startblk)
330 ivi->cmp_init_block = iter_varblk;
332 if (get_irn_op(reduce_var) == op_Mul) {
333 ir_node *mul_init = NULL;
334 ir_node *mul_const = NULL;
336 /* Search for constant and init of strong. */
337 ir_node *mul_right = get_Mul_right(reduce_var);
338 ir_node *mul_left = get_Mul_left(reduce_var);
339 ir_op *mul_right_op = get_irn_op(mul_right);
340 ir_op *mul_left_op = get_irn_op(mul_left);
345 ir_node *increment_block;
348 n_reduced_expressions++;
350 if (mul_right_op == op_Const) {
351 mul_const = mul_right;
354 else if (mul_left_op == op_Const) {
355 mul_const = mul_left;
356 mul_init = mul_right;
359 if (mul_const == NULL || mul_init == NULL)
362 increment_block = get_nodes_block(ivi->increment);
363 c_block = get_nodes_block(mul_const);
365 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
366 block_inc = increment_block;
370 if (! ivi->is_reducible){
373 /* Essential condition for the constant of strong. */
374 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
375 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
379 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
380 printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
381 printf("reducing operation is : "); DDMN(reduce_var);
382 printf("in graph : "); DDMG(current_ir_graph);
385 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
386 get_irn_mode(mul_const));
387 if (!(get_irn_op(mul_init) == op_Phi)){
388 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
389 get_irn_mode(mul_const));
390 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
393 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
394 get_irn_mode(mul_const));
396 /* Generate a new basic induction variable. Break the data flow loop
397 initially by using an Unknown node. */
399 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
401 in[ivi->init_pred_pos] = ivi->new_init;
402 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
403 get_irn_mode(mul_const));
404 mark_irn_visited(ivi->new_phi);
406 if (ivi->operation_code == op_Add)
407 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
408 ivi->new_increment,ivi-> new_phi);
409 else if (ivi->operation_code == op_Sub)
410 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
413 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
415 // This for search for a reducible successor of reduc_var.
416 reduce_var_pred = get_irn_n_outs(reduce_var);
417 if (reduce_var_pred == 1) {
418 ir_node *old_ind =get_irn_out(reduce_var, 0);
419 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
420 get_irn_op(old_ind) == op_Mul){
421 ivi->is_reducible = 1;
422 ivi->reducible_node = old_ind;
425 /* Replace the use of the strength reduced value. */
426 exchange(reduce_var, ivi->new_phi);
429 else { /* ivi->is_reducible */
430 if (ivi->new_phi == NULL) {
431 ivi->init = new_r_Mul (current_ir_graph, block_init,
432 mul_const, ivi->init,
433 get_irn_mode(mul_const));
435 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
436 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
437 ivi->increment = new_r_Mul (current_ir_graph, block_inc,
438 ivi->increment, mul_const, get_irn_mode(mul_const));
441 ivi->new_init = new_r_Mul (current_ir_graph, block_init,
442 mul_const, ivi->new_init,
443 get_irn_mode(mul_const));
444 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
445 ivi->new_increment, mul_const,
446 get_irn_mode(mul_const));
448 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
449 printf("\nReducing operation is : "); DDMN(reduce_var);
450 printf("in graph : "); DDMG(current_ir_graph);
455 else if (get_irn_op (reduce_var) == op_Add){
456 ir_node *add_init = NULL;
457 ir_node *add_const = NULL;
459 /* Search for constant of add. */
460 ir_node *add_right = get_Add_right(reduce_var);
461 ir_node *add_left = get_Add_left(reduce_var);
462 ir_op *add_right_op = get_irn_op(add_right);
463 ir_op *add_left_op = get_irn_op(add_left);
465 ++n_reduced_expressions;
467 if (add_right_op != op_Const)
468 add_init = add_right;
469 else if (add_left_op != op_Const)
471 if (add_right_op == op_Const || add_right_op == op_SymConst)
472 add_const = add_right;
473 else if (add_left_op == op_Const || add_left_op == op_SymConst)
474 add_const = add_left;
475 if (add_const == NULL)
477 if (ivi->new_phi == NULL) {
478 ivi->init = my_new_r_Add(current_ir_graph, block_init,
479 add_const, ivi->init);
480 if (ivi->cmp != NULL)
481 ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
482 add_const, ivi->cmp_const);
485 ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
486 add_const, ivi->new_init);
488 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
489 printf("\nReducing operation is : "); DDMN(reduce_var);
490 printf("in graph : "); DDMG(current_ir_graph);
494 else if (get_irn_op(reduce_var) == op_Sub) {
495 ir_node *sub_init = NULL;
496 ir_node *sub_const = NULL;
497 ir_node *sub_right = get_Sub_right(reduce_var);
498 ir_node *sub_left = get_Sub_left(reduce_var);
499 ir_op *sub_right_op = get_irn_op(sub_right);
500 ir_op *sub_left_op = get_irn_op(sub_left);
502 ++n_reduced_expressions;
504 /* Search for constant of Sub. */
505 if (sub_right_op != op_Const)
506 sub_init = sub_right;
507 else if (sub_left_op != op_Const)
509 if (sub_right_op == op_Const)
510 sub_const = sub_right;
511 else if (sub_left_op == op_Const)
512 sub_const = sub_left;
514 if (sub_const == NULL)
517 if (ivi->new_phi == NULL) {
518 ivi->init = my_new_r_Sub(current_ir_graph, block_init,
519 ivi->init, sub_const);
520 if (ivi->cmp != NULL)
521 ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
522 ivi->cmp_const,sub_const);
524 ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
525 ivi->new_init, sub_const);
526 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
527 printf("\nReducing operation is : "); DDMN(reduce_var);
528 printf("in graph : "); DDMG(current_ir_graph);
536 * Search for reducible successor of iteration variable.
537 * If such successor is found it will be reduced and returned,
540 * @param ivi Contains information about the induction variable.
541 * @param out A successor of iteration variable.
543 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
545 ir_node *reduced = NULL;
548 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
549 if (reduce(out, ivi))
553 out = get_irn_out(out, 0);
559 * Post walker: Find a Phi node that is a iteration variable and
562 * @param itervar_phi The iteration variable of a loop.
563 * @param env Free environment pointer.
565 static void reduce_itervar(ir_node *itervar_phi, void *env)
568 /* check if a iteration variable be reduced.*/
571 if (! is_Phi(itervar_phi))
574 /* A potential candidate is found.*/
575 ivi.itervar_phi = itervar_phi;
577 /* check if it's a induction variable.*/
578 if (is_induction_variable(&ivi)) {
581 for (i = 0; i < ivi.phi_pred; i++) {
582 ir_node *out = get_irn_out(ivi.itervar_phi, i);
583 ir_op *out_op = get_irn_op(out);
584 /* Reduce a induction variable.*/
585 if (ivi.is_reducible) {
586 if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
587 ir_node *irn_reduced = reducible(out, &ivi);
588 if (irn_reduced != NULL){
590 exchange(irn_reduced, ivi.itervar_phi);
594 /* Reduce a multiplication*/
595 else if (out_op == op_Mul)
596 if (reduce(out, &ivi) && ivi.is_reducible) {
597 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
600 exchange(reduced, ivi.new_phi);
602 ivi.is_reducible = 0;
603 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
604 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
605 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
609 op_out = get_irn_n_outs(ivi.op);
610 for (i = 0; i < op_out; i++){
611 ir_node *out = get_irn_out(ivi.op, i);
612 ir_op *out_op = get_irn_op(out);
613 /* Try to reduce the second successor of the "ivi.op"*/
614 if (op_out == 2 && out != ivi.itervar_phi){
615 ir_node *reduced = reducible(out, &ivi);
617 exchange( reduced, ivi.op);
619 /* Try to reduce a multiplication, that is successor of "ivi.op".*/
620 else if (out_op == op_Mul)
621 if (reduce(out, &ivi) && ivi.is_reducible){
622 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
624 exchange(reduced, ivi.new_phi);
625 ivi.is_reducible = 0;
626 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
627 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
628 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
631 /* Set some predecessors and modes after reduce.*/
632 if (ivi.is_reducible && reduced) {
633 if(get_irn_op(ivi.op) == op_Add)
634 if(get_Add_left(ivi.op) == ivi.itervar_phi)
635 set_Add_right(ivi.op, ivi.increment);
637 set_Add_left(ivi.op, ivi.increment);
638 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
639 set_Sub_right(ivi.op, ivi.increment);
641 set_Sub_right(ivi.op, ivi.increment);
642 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
643 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
644 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
645 if (ivi.cmp != NULL){
646 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
647 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
648 set_Cmp_right(ivi.cmp, ivi.cmp_const);
650 set_Cmp_left(ivi.cmp, ivi.cmp_const);
656 /* Performs strength reduction for the passed graph. */
657 void reduce_strength(ir_graph *irg) {
658 ir_graph *rem = current_ir_graph;
661 if (!get_optimize() || !get_opt_strength_red()) return;
663 current_ir_graph = irg;
665 n_reduced_expressions = 0;
667 /* -- Precompute some information -- */
668 /* Call algorithm that computes the backedges */
669 construct_cf_backedges(irg);
670 /* Call algorithm that computes the dominator trees. */
672 /* Call algorithm that computes the out edges */
673 assure_irg_outs(irg);
675 /* -- Search expressions that can be optimized -- */
676 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
678 if (get_opt_strength_red_verbose()) {
679 printf ("\n %d made new_phis und ", n_made_new_phis);
680 printf("reduced %d iteration variables "
681 "in \n graph %s.%s.\n", n_reduced_expressions,
682 get_type_name(get_entity_owner(get_irg_entity(irg))),
683 get_entity_name(get_irg_entity(irg)));
687 set_irg_outs_inconsistent(irg);
688 set_irg_loopinfo_inconsistent(irg);
691 current_ir_graph = rem;