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->cmp = NULL; /* Cmp wich breake the loop and compare iteration variable with a constant.*/
68 info->cmp_const = NULL; /* The oder operand of Cmp. */
69 info->cmp_init_block = NULL; /* The block of cmp.*/
70 info->increment = NULL; /* The volue wich increase or decrease the iteration variable.*/
71 info->init = NULL; /* The start volue of the iteration variable.*/
72 info->l_itervar_phi = NULL; /* The iteration variable.*/
73 info->new_cmp = NULL; /* The new cmp wich replace the old one.*/
74 info->new_increment = NULL; /* The new increment wich replece the old one.*/
75 info->new_init = NULL; /* The new init of the iteration varible.*/
76 info->new_op = NULL; /* The new operation that we need after replece.*/
77 info->new_phi = NULL; /* The new iteration variable.*/
78 info->operation_code = NULL; /* The operation art of "op"*/
79 info->op = NULL; /* The operation wich increase or decrease the iteration variable.*/
80 info->reducible_node = NULL; /* The reducible nodes are save here.*/
81 info->reducible = 0; /* To save information if enything is redicible.*/
82 info->phi_pred = 0; /* To save the volue of iteration variable predecessors.*/
83 info->init_pred_pos = -1; /* To save the position of iteration variable start volue.*/
84 info->op_pred_pos = -1; /* To save the backedge of iteration variable.*/
86 assert(get_irn_op(info->itervar_phi) == op_Phi);
89 * The necessary conditions for the phi node:
90 * We can handle currently Phi's with 2 predecessors, one must be a backedge.
92 if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
95 for (i = 0; i < 2; ++i) {
96 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
97 ir_op *op = get_irn_op(pred);
99 /* Compute if the induction variable is added or subtracted with a constant. */
100 if (op == op_Add || op == op_Sub) {
101 ir_node *n_l = get_binop_left(pred);
102 ir_node *n_r = get_binop_right(pred);
104 if (n_l == info->itervar_phi) {
105 info->operation_code = op;
106 info->increment = n_r;
107 info->op_pred_pos = i;
108 info->init_pred_pos = i ^ 1;
111 else if (n_r == info->itervar_phi) {
112 info->operation_code = op;
113 info->increment = n_l;
114 info->op_pred_pos = i;
115 info->init_pred_pos = i ^ 1;
120 /* check if we found something */
121 if (! info->operation_code)
124 /* Compute the position of the backedge. */
125 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
126 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
127 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
130 /* irregular control flow detected. */
135 * the block of the init code should dominate the loop, else
136 * we have an irregular control flow
138 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
139 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
143 op_pred = get_irn_n_outs(info->op);
147 /* Information about loop of itervar_phi. */
148 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
151 info->phi_pred = get_irn_n_outs(info->itervar_phi);
152 loop_head = get_nodes_block(info->itervar_phi);
155 * This "for" searches for the Cmp successor of the
156 * iter_var to reduce and marks if the iter_var have a Store
157 * successor or a successor out of loop.
159 for (i = 0; i < info->phi_pred; i++) {
160 ir_node *out = get_irn_out(info->itervar_phi, i);
161 ir_op *out_op = get_irn_op(out);
163 if (out_op == op_Store)
165 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
166 /* "Cmp" can have more as one successor therefore we need this loop.*/
167 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
168 ir_node *proj = get_irn_out(out, q);
170 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
171 cmp_pred_bl = get_irn_out(proj, r);
173 /* The wanted "Cmp" must be followed with a "Cond" successor
175 if (get_irn_op(cmp_pred_bl) != op_Cond)
178 /* the binary Cond should have two successors */
179 if (get_irn_n_outs(cmp_pred_bl) != 2)
182 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
183 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
185 if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
186 is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
187 if (get_Cmp_left(out) == info->itervar_phi)
188 cmp_const = get_Cmp_right(out);
190 cmp_const = get_Cmp_left(out);
193 if (info->cmp == NULL) {
196 info->cmp_const = cmp_const;
199 /* We have more then one cmp with our requests, that mean cmp isn't found*/
208 for (i = 0; i < op_pred; ++i) {
209 ir_node *out = get_irn_out(info->op, i);
210 ir_op *out_op = get_irn_op(out);
212 if (out_op == op_Store)
214 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
215 /* "Cmp" can have more as one successor therefore
216 i need this for loop.*/
217 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
218 ir_node *proj = get_irn_out(out, q);
220 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
221 cmp_pred_bl = get_irn_out(proj, r);
223 /* The wanted "Cmp" must be followed with a "Cond" successor. */
224 if (get_irn_op(cmp_pred_bl) != op_Cond)
227 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
228 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
230 if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
231 is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
232 if (get_Cmp_left(out) == info->op)
233 cmp_const = get_Cmp_right(out);
235 cmp_const = get_Cmp_left(out);
238 if (info->cmp == NULL) {
241 info->cmp_const = cmp_const;
244 /* We have more then one cmp with our requests, that mean cmp isn't found*/
253 if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
254 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
255 (info->phi_pred == 1 && Store_in_op == 0))
258 /* Search for loop invariant of Cmp.*/
259 if (info->cmp != NULL) {
260 cmp_const_block = get_nodes_block(info->cmp_const);
261 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
262 get_Block_dom_depth(cmp_const_block))
263 info->cmp_init_block = get_nodes_block(info->init);
265 info->cmp_init_block = cmp_const_block;
271 * Creates a new Add node from operands.
273 static INLINE ir_node *
274 my_new_r_Add(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(m2))
281 return new_r_Add(irg, b, op1, op2, m);
285 * Creates a new Sub node from operands.
287 static INLINE ir_node *
288 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
289 ir_mode *m = get_irn_mode(op1);
290 ir_mode *m2 = get_irn_mode(op2);
292 if (mode_is_reference(m) && mode_is_reference(m2))
293 m = mode_Is; /* FIXME: may be other mode! */
294 else if (mode_is_reference(m2))
296 return new_r_Sub(irg, b, op1, op2, m);
299 /* Reduce a Add, Sub or Mul node
301 * @param *reduce_var The node to reduce.
302 * @param *ivi Contains the induction variable information.
304 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
306 ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
308 // Essential conditions for a reducible node.
309 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
312 iter_varblk = get_nodes_block(ivi->itervar_phi);
313 init_block = get_nodes_block(ivi->init);
314 irg_startblk = get_irg_start_block(current_ir_graph);
316 /* The "new_init" and the "new_cmp_const" mussn't be in the start block.*/
317 if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
318 init_block != iter_varblk)
319 block_init = init_block;
321 block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
323 /* To avoid that cmp is placed in the startblock.*/
324 if (ivi->cmp_init_block == irg_startblk)
325 ivi->cmp_init_block = iter_varblk;
327 if (get_irn_op(reduce_var) == op_Mul) {
328 ir_node *mul_init = NULL;
329 ir_node *mul_const = NULL;
331 // Search for constant and init of strong.
332 ir_node *mul_right = get_Mul_right(reduce_var);
333 ir_node *mul_left = get_Mul_left(reduce_var);
334 ir_op *mul_right_op = get_irn_op(mul_right);
335 ir_op *mul_left_op = get_irn_op(mul_left);
340 ir_node *increment_block;
343 n_reduced_expressions++;
345 if (mul_right_op == op_Const) {
346 mul_const = mul_right;
349 else if (mul_left_op == op_Const) {
350 mul_const = mul_left;
351 mul_init = mul_right;
354 if (mul_const == NULL || mul_init == NULL)
357 increment_block = get_nodes_block(ivi->increment);
358 c_block = get_nodes_block(mul_const);
360 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
361 block_inc = increment_block;
365 if (! ivi->reducible){
368 // Essential condition for the constant of strong.
369 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
370 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
374 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
375 printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
376 printf("reducing operation is : "); DDMN(reduce_var);
377 printf("in graph : "); DDMG(current_ir_graph);
380 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
381 get_irn_mode(mul_const));
382 if (!(get_irn_op(mul_init) == op_Phi)){
383 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
384 get_irn_mode(mul_const));
385 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
388 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
389 get_irn_mode(mul_const));
391 /* Generate a new basic induction variable. Break the data flow loop
392 initially by using an Unknown node. */
394 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
396 in[ivi->init_pred_pos] = ivi->new_init;
397 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
398 get_irn_mode(mul_const));
399 mark_irn_visited(ivi->new_phi);
401 if (ivi->operation_code == op_Add)
402 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
403 ivi->new_increment,ivi-> new_phi);
404 else if (ivi->operation_code == op_Sub)
405 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
408 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
410 // This for search for a reducible successor of reduc_var.
411 reduce_var_pred = get_irn_n_outs(reduce_var);
412 if (reduce_var_pred == 1) {
413 ir_node *old_ind =get_irn_out(reduce_var, 0);
414 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
415 get_irn_op(old_ind) == op_Mul){
417 ivi->reducible_node = old_ind;
420 /* Replace the use of the strength reduced value. */
421 exchange(reduce_var, ivi->new_phi);
424 else { /* ivi->reducible */
425 if(ivi->new_phi == NULL){
426 ivi->init = new_r_Mul (current_ir_graph, block_init,
427 mul_const, ivi->init,
428 get_irn_mode(mul_const));
430 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
431 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
432 ivi->increment = new_r_Mul (current_ir_graph, block_inc,
433 ivi->increment, mul_const, get_irn_mode(mul_const));
435 ivi->new_init = new_r_Mul (current_ir_graph, block_init,
436 mul_const, ivi->new_init,
437 get_irn_mode(mul_const));
438 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
439 ivi->new_increment, mul_const,
440 get_irn_mode(mul_const));
442 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
443 printf("\nReducing operation is : "); DDMN(reduce_var);
444 printf("in graph : "); DDMG(current_ir_graph);
449 else if (get_irn_op (reduce_var) == op_Add){
450 ir_node *add_init = NULL;
451 ir_node *add_const = NULL;
453 // Search for constant of add.
454 ir_node *add_right = get_Add_right(reduce_var);
455 ir_node *add_left = get_Add_left(reduce_var);
456 ir_op *add_right_op = get_irn_op(add_right);
457 ir_op *add_left_op = get_irn_op(add_left);
459 n_reduced_expressions++;
461 if (add_right_op != op_Const)
462 add_init = add_right;
463 else if (add_left_op != op_Const)
465 if (add_right_op == op_Const || add_right_op == op_SymConst)
466 add_const = add_right;
467 else if (add_left_op == op_Const || add_left_op == op_SymConst)
468 add_const = add_left;
469 if (add_const == NULL) return 0;
470 if (ivi->new_phi == NULL){
471 ivi->init = my_new_r_Add(current_ir_graph, block_init,
472 add_const, ivi->init);
473 if (ivi->cmp != NULL)
474 ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
475 add_const, ivi->cmp_const);
477 ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
478 add_const, ivi->new_init);
480 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
481 printf("\nReducing operation is : "); DDMN(reduce_var);
482 printf("in graph : "); DDMG(current_ir_graph);
486 else if (get_irn_op(reduce_var) == op_Sub) {
487 ir_node *sub_init = NULL;
488 ir_node *sub_const = NULL;
489 ir_node *sub_right = get_Sub_right(reduce_var);
490 ir_node *sub_left = get_Sub_left(reduce_var);
491 ir_op *sub_right_op = get_irn_op(sub_right);
492 ir_op *sub_left_op = get_irn_op(sub_left);
494 n_reduced_expressions++;
496 /* Search for constant of Sub. */
497 if (sub_right_op != op_Const)
498 sub_init = sub_right;
499 else if (sub_left_op != op_Const)
501 if (sub_right_op == op_Const)
502 sub_const = sub_right;
503 else if (sub_left_op == op_Const)
504 sub_const = sub_left;
506 if (sub_const == NULL)
509 if (ivi->new_phi == NULL) {
510 ivi->init = my_new_r_Sub(current_ir_graph, block_init,
511 ivi->init, sub_const);
512 if (ivi->cmp != NULL)
513 ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
514 ivi->cmp_const,sub_const);
516 ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
517 ivi->new_init, sub_const);
518 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
519 printf("\nReducing operation is : "); DDMN(reduce_var);
520 printf("in graph : "); DDMG(current_ir_graph);
528 * Search for reducible successor of iteration variable.
529 * If such successor is found it will be reduced and returned,
532 * @param ivi Contains information about the induction variable.
533 * @param out A successor of iteration variable.
535 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
537 ir_node *reduced = NULL;
540 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
541 if (reduce(out, ivi))
545 out = get_irn_out(out, 0);
551 * Post walker: Find a Phi node that is a iteration variable and
554 * @param itervar_phi The iteration variable of a loop.
555 * @param env Free environment pointer.
557 static void reduce_itervar(ir_node *itervar_phi, void *env)
560 /* check if a iteration variable be reduced.*/
563 if (get_irn_op(itervar_phi) != op_Phi)
565 /* A candidate is found.*/
566 ivi.itervar_phi = itervar_phi;
568 /* It musss be a induction variable.*/
569 if (is_induction_variable(&ivi)) {
572 for (i = 0; i < ivi.phi_pred; i++) {
573 ir_node *out = get_irn_out(ivi.itervar_phi, i);
574 ir_op *out_op = get_irn_op(out);
575 /* Reduce a induction variable.*/
577 if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
578 ir_node *irn_reduced = reducible(out, &ivi);
579 if (irn_reduced != NULL){
581 exchange( irn_reduced, ivi.itervar_phi);
585 /* Reduce a multiplication*/
586 else if (out_op == op_Mul)
587 if (reduce(out, &ivi) && ivi.reducible) {
588 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
591 exchange(reduced, ivi.new_phi);
594 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
595 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
596 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
600 op_out = get_irn_n_outs(ivi.op);
601 for (i = 0; i < op_out; i++){
602 ir_node *out = get_irn_out(ivi.op, i);
603 ir_op *out_op = get_irn_op(out);
604 /* Try to reduce the second successor of the "ivi.op"*/
605 if (op_out == 2 && out != ivi.itervar_phi){
606 ir_node *reduced = reducible(out, &ivi);
608 exchange( reduced, ivi.op);
610 /* Try to reduce a multiplication, that is successor of "ivi.op".*/
611 else if (out_op == op_Mul)
612 if (reduce(out, &ivi) && ivi.reducible){
613 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
615 exchange(reduced, ivi.new_phi);
617 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
618 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
619 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
622 /* Set some predecessors and modes after reduce.*/
623 if (ivi.reducible && reduced) {
624 if(get_irn_op(ivi.op) == op_Add)
625 if(get_Add_left(ivi.op) == ivi.itervar_phi)
626 set_Add_right(ivi.op, ivi.increment);
628 set_Add_left(ivi.op, ivi.increment);
629 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
630 set_Sub_right(ivi.op, ivi.increment);
632 set_Sub_right(ivi.op, ivi.increment);
633 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
634 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
635 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
636 if (ivi.cmp != NULL){
637 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
638 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
639 set_Cmp_right(ivi.cmp, ivi.cmp_const);
641 set_Cmp_left(ivi.cmp, ivi.cmp_const);
647 /* Performs strength reduction for the passed graph. */
648 void reduce_strength(ir_graph *irg) {
649 ir_graph *rem = current_ir_graph;
652 if (!get_optimize() || !get_opt_strength_red()) return;
654 current_ir_graph = irg;
656 n_reduced_expressions = 0;
658 /* -- Precompute some information -- */
659 /* Call algorithm that computes the backedges */
660 construct_cf_backedges(irg);
661 /* Call algorithm that computes the dominator trees. */
663 /* Call algorithm that computes the out edges */
664 compute_irg_outs(irg);
666 /* -- Search expressions that can be optimized -- */
667 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
669 if (get_opt_strength_red_verbose()) {
670 printf ("\n %d made new_phis und ", n_made_new_phis);
671 printf("reduced %d iteration variables "
672 "in \n graph %s.%s.\n", n_reduced_expressions,
673 get_type_name(get_entity_owner(get_irg_entity(irg))),
674 get_entity_name(get_irg_entity(irg)));
678 set_irg_outs_inconsistent(irg);
679 set_irg_loopinfo_inconsistent(irg);
682 current_ir_graph = rem;