2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @file strength_red.c
25 * File name: ir/opt/strength_red.c
26 * Purpose: Make strength reduction .
27 * Author: Beyhan Veliev
31 * Copyright: (c) 2004 Universität Karlsruhe
37 # include "strength_red.h"
40 # include "irnode_t.h"
42 # include "irloop_t.h"
45 # include "irdump_t.h"
46 # include "firmstat.h"
49 /** Counter for verbose information about optimization. */
50 static int n_reduced_expressions;
51 static int n_made_new_phis;
53 /** Detect basic iteration variables.
55 * The variable is represented by a subgraph as this:
68 * Where op is a Add or Sub node and init is loop invariant.
71 * So far we only accept Phi nodes with two predecessors.
72 * We could expand this to Phi nodes where all predecessors
73 * are either op or loop invariant.
75 * @param info After call contains the induction variable information.
80 induct_var_info *is_induction_variable(induct_var_info *info) {
83 int op_pred, Store_in_op, Store_in_phi;
84 ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
86 ir_node *cmp_const_block;
88 info->operation_code = NULL; /* The opcode of "op". */
89 info->increment = NULL; /* The value which increase or decrease the iteration variable. */
90 info->init = NULL; /* The start value of the iteration variable. */
91 info->op = NULL; /* The operation which increase or decrease the iteration variable. */
92 info->l_itervar_phi = NULL; /* The iteration variable. */
93 info->new_phi = NULL; /* The new iteration variable. */
94 info->new_increment = NULL; /* The new increment which replace the old one.*/
95 info->new_init = NULL; /* The new init value of the iteration variable. */
96 info->new_op = NULL; /* The new operation that we need after replace. */
97 info->new_cmp = NULL; /* The new Cmp which replaces the old one. */
98 info->cmp = NULL; /* The Cmp which breaks the loop and compares the iteration variable with a constant. */
99 info->cmp_const = NULL; /* The other operand of Cmp. */
100 info->cmp_init_block = NULL; /* The initial block of the Cmp. */
101 info->reducible_node = NULL; /* The reducible nodes are save here. */
102 info->is_reducible = 0; /* To save information if anything is reducible. */
103 info->phi_pred = 0; /* To save the value of iteration variable predecessors. */
104 info->init_pred_pos = -1; /* To save the position of iteration variable start value. */
105 info->op_pred_pos = -1; /* To save the backedge of iteration variable. */
106 info->l_itervar_phi = NULL; /* Information about loop of itervar_phi. */
108 assert(get_irn_op(info->itervar_phi) == op_Phi);
111 * The necessary conditions for the phi node:
112 * We can handle currently Phi's with 2 predecessors, one must be a backedge.
114 if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
117 for (i = 0; i < 2; ++i) {
118 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
119 ir_op *op = get_irn_op(pred);
121 /* Compute if the induction variable is added or subtracted with a constant. */
122 if (op == op_Add || op == op_Sub) {
123 ir_node *n_l = get_binop_left(pred);
124 ir_node *n_r = get_binop_right(pred);
126 if (n_l == info->itervar_phi) {
127 info->operation_code = op;
128 info->increment = n_r;
129 info->op_pred_pos = i;
130 info->init_pred_pos = i ^ 1;
133 else if (n_r == info->itervar_phi) {
134 info->operation_code = op;
135 info->increment = n_l;
136 info->op_pred_pos = i;
137 info->init_pred_pos = i ^ 1;
142 /* check if we found something */
143 if (! info->operation_code)
146 /* Compute the position of the backedge. */
147 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
148 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
149 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
152 /* irregular control flow detected. */
157 * the block of the init code should dominate the loop, else
158 * we have an irregular control flow
160 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
161 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
165 op_pred = get_irn_n_outs(info->op);
169 /* Information about loop of itervar_phi. */
170 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
173 info->phi_pred = get_irn_n_outs(info->itervar_phi);
174 loop_head = get_nodes_block(info->itervar_phi);
177 * This "for" searches for the Cmp successor of the
178 * iter_var to reduce and marks if the iter_var have a Store
179 * successor or a successor out of loop.
181 for (i = 0; i < info->phi_pred; i++) {
182 ir_node *out = get_irn_out(info->itervar_phi, i);
183 ir_op *out_op = get_irn_op(out);
185 if (out_op == op_Store)
187 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
188 /* a Cmp can have more than one successor therefore we need this loop.*/
189 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
190 ir_node *proj = get_irn_out(out, q);
192 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
193 cmp_pred_bl = get_irn_out(proj, r);
195 /* The wanted Cmp must be followed by a Cond successor
197 if (get_irn_op(cmp_pred_bl) != op_Cond)
200 /* the binary Cond should have two successors */
201 if (get_irn_n_outs(cmp_pred_bl) != 2)
204 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
205 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
207 if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
208 is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
209 if (get_Cmp_left(out) == info->itervar_phi)
210 cmp_const = get_Cmp_right(out);
212 cmp_const = get_Cmp_left(out);
215 if (info->cmp == NULL) {
218 info->cmp_const = cmp_const;
221 /* We have more then one cmp with our requests, that mean cmp isn't found */
230 for (i = 0; i < op_pred; ++i) {
231 ir_node *out = get_irn_out(info->op, i);
232 ir_op *out_op = get_irn_op(out);
234 if (out_op == op_Store)
236 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
237 /* a Cmp can have more as one successor therefore
238 I need this for loop. */
239 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
240 ir_node *proj = get_irn_out(out, q);
242 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
243 cmp_pred_bl = get_irn_out(proj, r);
245 /* The wanted Cmp must be followed by a Cond successor. */
246 if (get_irn_op(cmp_pred_bl) != op_Cond)
249 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
250 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
252 if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
253 is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
254 if (get_Cmp_left(out) == info->op)
255 cmp_const = get_Cmp_right(out);
257 cmp_const = get_Cmp_left(out);
260 if (info->cmp == NULL) {
263 info->cmp_const = cmp_const;
266 /* We have more then one cmp with our requests, that mean cmp isn't found*/
275 if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
276 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
277 (info->phi_pred == 1 && Store_in_op == 0))
278 info->is_reducible = 1;
280 /* Search for loop invariant of Cmp.*/
281 if (info->cmp != NULL) {
282 cmp_const_block = get_nodes_block(info->cmp_const);
283 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
284 get_Block_dom_depth(cmp_const_block))
285 info->cmp_init_block = get_nodes_block(info->init);
287 info->cmp_init_block = cmp_const_block;
293 * Creates a new Add node with the correct mode from its two operands.
295 static INLINE ir_node *
296 my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
297 ir_mode *m = get_irn_mode(op1);
298 ir_mode *m2 = get_irn_mode(op2);
300 if (mode_is_reference(m2))
303 return new_r_Add(irg, b, op1, op2, m);
307 * Creates a new Sub node with the correct mode from its two operands.
309 static INLINE ir_node *
310 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
311 ir_mode *m = get_irn_mode(op1);
312 ir_mode *m2 = get_irn_mode(op2);
314 if (mode_is_reference(m) && mode_is_reference(m2))
315 m = mode_Is; /* FIXME: may be other mode! */
316 else if (mode_is_reference(m2))
318 return new_r_Sub(irg, b, op1, op2, m);
322 * Reduce an Add, Sub or Mul node
324 * @param *reduce_var The node to reduce.
325 * @param *ivi Contains the induction variable information.
327 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
329 ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
331 /* Essential conditions for a reducible node. */
332 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
335 iter_varblk = get_nodes_block(ivi->itervar_phi);
336 init_block = get_nodes_block(ivi->init);
337 irg_startblk = get_irg_start_block(current_ir_graph);
339 /* The "new_init" and the "new_cmp_const" must not be in the start block.*/
340 if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
341 init_block != iter_varblk)
342 block_init = init_block;
344 block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
346 /* To avoid that cmp is placed in the start block.*/
347 if (ivi->cmp_init_block == irg_startblk)
348 ivi->cmp_init_block = iter_varblk;
350 if (get_irn_op(reduce_var) == op_Mul) {
351 ir_node *mul_init = NULL;
352 ir_node *mul_const = NULL;
354 /* Search for constant and init of strong. */
355 ir_node *mul_right = get_Mul_right(reduce_var);
356 ir_node *mul_left = get_Mul_left(reduce_var);
357 ir_op *mul_right_op = get_irn_op(mul_right);
358 ir_op *mul_left_op = get_irn_op(mul_left);
363 ir_node *increment_block;
366 n_reduced_expressions++;
368 if (mul_right_op == op_Const) {
369 mul_const = mul_right;
372 else if (mul_left_op == op_Const) {
373 mul_const = mul_left;
374 mul_init = mul_right;
377 if (mul_const == NULL || mul_init == NULL)
380 increment_block = get_nodes_block(ivi->increment);
381 c_block = get_nodes_block(mul_const);
383 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
384 block_inc = increment_block;
388 if (! ivi->is_reducible){
391 /* Essential condition for the constant of strong. */
392 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
393 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
397 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
398 printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
399 printf("reducing operation is : "); DDMN(reduce_var);
400 printf("in graph : "); DDMG(current_ir_graph);
403 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
404 get_irn_mode(mul_const));
405 if (!(get_irn_op(mul_init) == op_Phi)){
406 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
407 get_irn_mode(mul_const));
408 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
411 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
412 get_irn_mode(mul_const));
414 /* Generate a new basic induction variable. Break the data flow loop
415 initially by using an Unknown node. */
417 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
419 in[ivi->init_pred_pos] = ivi->new_init;
420 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
421 get_irn_mode(mul_const));
422 mark_irn_visited(ivi->new_phi);
424 if (ivi->operation_code == op_Add)
425 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
426 ivi->new_increment,ivi-> new_phi);
427 else if (ivi->operation_code == op_Sub)
428 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
431 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
433 // This for search for a reducible successor of reduc_var.
434 reduce_var_pred = get_irn_n_outs(reduce_var);
435 if (reduce_var_pred == 1) {
436 ir_node *old_ind =get_irn_out(reduce_var, 0);
437 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
438 get_irn_op(old_ind) == op_Mul){
439 ivi->is_reducible = 1;
440 ivi->reducible_node = old_ind;
443 /* Replace the use of the strength reduced value. */
444 exchange(reduce_var, ivi->new_phi);
447 else { /* ivi->is_reducible */
448 if (ivi->new_phi == NULL) {
449 ivi->init = new_r_Mul (current_ir_graph, block_init,
450 mul_const, ivi->init,
451 get_irn_mode(mul_const));
453 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
454 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
455 ivi->increment = new_r_Mul (current_ir_graph, block_inc,
456 ivi->increment, mul_const, get_irn_mode(mul_const));
459 ivi->new_init = new_r_Mul (current_ir_graph, block_init,
460 mul_const, ivi->new_init,
461 get_irn_mode(mul_const));
462 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
463 ivi->new_increment, mul_const,
464 get_irn_mode(mul_const));
466 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
467 printf("\nReducing operation is : "); DDMN(reduce_var);
468 printf("in graph : "); DDMG(current_ir_graph);
473 else if (get_irn_op (reduce_var) == op_Add){
474 ir_node *add_init = NULL;
475 ir_node *add_const = NULL;
477 /* Search for constant of add. */
478 ir_node *add_right = get_Add_right(reduce_var);
479 ir_node *add_left = get_Add_left(reduce_var);
480 ir_op *add_right_op = get_irn_op(add_right);
481 ir_op *add_left_op = get_irn_op(add_left);
483 ++n_reduced_expressions;
485 if (add_right_op != op_Const)
486 add_init = add_right;
487 else if (add_left_op != op_Const)
489 if (add_right_op == op_Const || add_right_op == op_SymConst)
490 add_const = add_right;
491 else if (add_left_op == op_Const || add_left_op == op_SymConst)
492 add_const = add_left;
493 if (add_const == NULL)
495 if (ivi->new_phi == NULL) {
496 ivi->init = my_new_r_Add(current_ir_graph, block_init,
497 add_const, ivi->init);
498 if (ivi->cmp != NULL)
499 ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
500 add_const, ivi->cmp_const);
503 ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
504 add_const, ivi->new_init);
506 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
507 printf("\nReducing operation is : "); DDMN(reduce_var);
508 printf("in graph : "); DDMG(current_ir_graph);
512 else if (get_irn_op(reduce_var) == op_Sub) {
513 ir_node *sub_init = NULL;
514 ir_node *sub_const = NULL;
515 ir_node *sub_right = get_Sub_right(reduce_var);
516 ir_node *sub_left = get_Sub_left(reduce_var);
517 ir_op *sub_right_op = get_irn_op(sub_right);
518 ir_op *sub_left_op = get_irn_op(sub_left);
520 ++n_reduced_expressions;
522 /* Search for constant of Sub. */
523 if (sub_right_op != op_Const)
524 sub_init = sub_right;
525 else if (sub_left_op != op_Const)
527 if (sub_right_op == op_Const)
528 sub_const = sub_right;
529 else if (sub_left_op == op_Const)
530 sub_const = sub_left;
532 if (sub_const == NULL)
535 if (ivi->new_phi == NULL) {
536 ivi->init = my_new_r_Sub(current_ir_graph, block_init,
537 ivi->init, sub_const);
538 if (ivi->cmp != NULL)
539 ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
540 ivi->cmp_const,sub_const);
542 ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
543 ivi->new_init, sub_const);
544 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
545 printf("\nReducing operation is : "); DDMN(reduce_var);
546 printf("in graph : "); DDMG(current_ir_graph);
554 * Search for reducible successor of iteration variable.
555 * If such successor is found it will be reduced and returned,
558 * @param ivi Contains information about the induction variable.
559 * @param out A successor of iteration variable.
561 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
563 ir_node *reduced = NULL;
566 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
567 if (reduce(out, ivi))
571 out = get_irn_out(out, 0);
577 * Post walker: Find a Phi node that is a iteration variable and
580 * @param itervar_phi The iteration variable of a loop.
581 * @param env Free environment pointer.
583 static void reduce_itervar(ir_node *itervar_phi, void *env)
586 /* check if a iteration variable be reduced.*/
589 if (! is_Phi(itervar_phi))
592 /* A potential candidate is found.*/
593 ivi.itervar_phi = itervar_phi;
595 /* check if it's a induction variable.*/
596 if (is_induction_variable(&ivi)) {
599 for (i = 0; i < ivi.phi_pred; i++) {
600 ir_node *out = get_irn_out(ivi.itervar_phi, i);
601 ir_op *out_op = get_irn_op(out);
602 /* Reduce a induction variable.*/
603 if (ivi.is_reducible) {
604 if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
605 ir_node *irn_reduced = reducible(out, &ivi);
606 if (irn_reduced != NULL){
608 exchange(irn_reduced, ivi.itervar_phi);
612 /* Reduce a multiplication*/
613 else if (out_op == op_Mul)
614 if (reduce(out, &ivi) && ivi.is_reducible) {
615 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
618 exchange(reduced, ivi.new_phi);
620 ivi.is_reducible = 0;
621 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
622 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
623 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
627 op_out = get_irn_n_outs(ivi.op);
628 for (i = 0; i < op_out; i++){
629 ir_node *out = get_irn_out(ivi.op, i);
630 ir_op *out_op = get_irn_op(out);
631 /* Try to reduce the second successor of the "ivi.op"*/
632 if (op_out == 2 && out != ivi.itervar_phi){
633 ir_node *reduced = reducible(out, &ivi);
635 exchange( reduced, ivi.op);
637 /* Try to reduce a multiplication, that is successor of "ivi.op".*/
638 else if (out_op == op_Mul)
639 if (reduce(out, &ivi) && ivi.is_reducible){
640 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
642 exchange(reduced, ivi.new_phi);
643 ivi.is_reducible = 0;
644 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
645 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
646 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
649 /* Set some predecessors and modes after reduce.*/
650 if (ivi.is_reducible && reduced) {
651 if(get_irn_op(ivi.op) == op_Add)
652 if(get_Add_left(ivi.op) == ivi.itervar_phi)
653 set_Add_right(ivi.op, ivi.increment);
655 set_Add_left(ivi.op, ivi.increment);
656 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
657 set_Sub_right(ivi.op, ivi.increment);
659 set_Sub_right(ivi.op, ivi.increment);
660 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
661 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
662 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
663 if (ivi.cmp != NULL){
664 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
665 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
666 set_Cmp_right(ivi.cmp, ivi.cmp_const);
668 set_Cmp_left(ivi.cmp, ivi.cmp_const);
674 /* Performs strength reduction for the passed graph. */
675 void reduce_strength(ir_graph *irg) {
676 ir_graph *rem = current_ir_graph;
679 if (!get_optimize() || !get_opt_strength_red()) return;
681 current_ir_graph = irg;
683 n_reduced_expressions = 0;
685 /* -- Precompute some information -- */
686 /* Call algorithm that computes the backedges */
687 construct_cf_backedges(irg);
688 /* Call algorithm that computes the dominator trees. */
690 /* Call algorithm that computes the out edges */
691 assure_irg_outs(irg);
693 /* -- Search expressions that can be optimized -- */
694 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
696 if (get_opt_strength_red_verbose()) {
697 printf ("\n %d made new_phis und ", n_made_new_phis);
698 printf("reduced %d iteration variables "
699 "in \n graph %s.%s.\n", n_reduced_expressions,
700 get_type_name(get_entity_owner(get_irg_entity(irg))),
701 get_entity_name(get_irg_entity(irg)));
705 set_irg_outs_inconsistent(irg);
706 set_irg_loopinfo_inconsistent(irg);
709 current_ir_graph = rem;