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_t.h"
38 #include "iroptimize.h"
50 /** Counter for verbose information about optimization. */
51 static int n_reduced_expressions;
52 static int n_made_new_phis;
54 /** Detect basic iteration variables.
56 * The variable is represented by a subgraph as this:
69 * Where op is a Add or Sub node and init is loop invariant.
72 * So far we only accept Phi nodes with two predecessors.
73 * We could expand this to Phi nodes where all predecessors
74 * are either op or loop invariant.
76 * @param info After call contains the induction variable information.
81 induct_var_info *is_induction_variable(induct_var_info *info) {
84 int op_pred, Store_in_op, Store_in_phi;
85 ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
87 ir_node *cmp_const_block;
89 info->operation_code = NULL; /* The opcode of "op". */
90 info->increment = NULL; /* The value which increase or decrease the iteration variable. */
91 info->init = NULL; /* The start value of the iteration variable. */
92 info->op = NULL; /* The operation which increase or decrease the iteration variable. */
93 info->l_itervar_phi = NULL; /* The iteration variable. */
94 info->new_phi = NULL; /* The new iteration variable. */
95 info->new_increment = NULL; /* The new increment which replace the old one.*/
96 info->new_init = NULL; /* The new init value of the iteration variable. */
97 info->new_op = NULL; /* The new operation that we need after replace. */
98 info->new_cmp = NULL; /* The new Cmp which replaces the old one. */
99 info->cmp = NULL; /* The Cmp which breaks the loop and compares the iteration variable with a constant. */
100 info->cmp_const = NULL; /* The other operand of Cmp. */
101 info->cmp_init_block = NULL; /* The initial block of the Cmp. */
102 info->reducible_node = NULL; /* The reducible nodes are save here. */
103 info->is_reducible = 0; /* To save information if anything is reducible. */
104 info->phi_pred = 0; /* To save the value of iteration variable predecessors. */
105 info->init_pred_pos = -1; /* To save the position of iteration variable start value. */
106 info->op_pred_pos = -1; /* To save the backedge of iteration variable. */
107 info->l_itervar_phi = NULL; /* Information about loop of itervar_phi. */
109 assert(get_irn_op(info->itervar_phi) == op_Phi);
112 * The necessary conditions for the phi node:
113 * We can handle currently Phi's with 2 predecessors, one must be a backedge.
115 if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
118 for (i = 0; i < 2; ++i) {
119 ir_node *pred = get_Phi_pred(info->itervar_phi, i);
120 ir_op *op = get_irn_op(pred);
122 /* Compute if the induction variable is added or subtracted with a constant. */
123 if (op == op_Add || op == op_Sub) {
124 ir_node *n_l = get_binop_left(pred);
125 ir_node *n_r = get_binop_right(pred);
127 if (n_l == info->itervar_phi) {
128 info->operation_code = op;
129 info->increment = n_r;
130 info->op_pred_pos = i;
131 info->init_pred_pos = i ^ 1;
134 else if (n_r == info->itervar_phi) {
135 info->operation_code = op;
136 info->increment = n_l;
137 info->op_pred_pos = i;
138 info->init_pred_pos = i ^ 1;
143 /* check if we found something */
144 if (! info->operation_code)
147 /* Compute the position of the backedge. */
148 if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
149 info->op = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
150 info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
153 /* irregular control flow detected. */
158 * the block of the init code should dominate the loop, else
159 * we have an irregular control flow
161 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
162 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
166 op_pred = get_irn_n_outs(info->op);
170 /* Information about loop of itervar_phi. */
171 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
174 info->phi_pred = get_irn_n_outs(info->itervar_phi);
175 loop_head = get_nodes_block(info->itervar_phi);
178 * This "for" searches for the Cmp successor of the
179 * iter_var to reduce and marks if the iter_var have a Store
180 * successor or a successor out of loop.
182 for (i = 0; i < info->phi_pred; i++) {
183 ir_node *out = get_irn_out(info->itervar_phi, i);
184 ir_op *out_op = get_irn_op(out);
186 if (out_op == op_Store)
188 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
189 /* a Cmp can have more than one successor therefore we need this loop.*/
190 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
191 ir_node *proj = get_irn_out(out, q);
193 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
194 cmp_pred_bl = get_irn_out(proj, r);
196 /* The wanted Cmp must be followed by a Cond successor
198 if (get_irn_op(cmp_pred_bl) != op_Cond)
201 /* the binary Cond should have two successors */
202 if (get_irn_n_outs(cmp_pred_bl) != 2)
205 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
206 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
208 if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
209 is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
210 if (get_Cmp_left(out) == info->itervar_phi)
211 cmp_const = get_Cmp_right(out);
213 cmp_const = get_Cmp_left(out);
216 if (info->cmp == NULL) {
219 info->cmp_const = cmp_const;
222 /* We have more then one cmp with our requests, that mean cmp isn't found */
231 for (i = 0; i < op_pred; ++i) {
232 ir_node *out = get_irn_out(info->op, i);
233 ir_op *out_op = get_irn_op(out);
235 if (out_op == op_Store)
237 else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
238 /* a Cmp can have more as one successor therefore
239 I need this for loop. */
240 for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
241 ir_node *proj = get_irn_out(out, q);
243 for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
244 cmp_pred_bl = get_irn_out(proj, r);
246 /* The wanted Cmp must be followed by a Cond successor. */
247 if (get_irn_op(cmp_pred_bl) != op_Cond)
250 cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
251 cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
253 if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
254 is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
255 if (get_Cmp_left(out) == info->op)
256 cmp_const = get_Cmp_right(out);
258 cmp_const = get_Cmp_left(out);
261 if (info->cmp == NULL) {
264 info->cmp_const = cmp_const;
267 /* We have more then one cmp with our requests, that mean cmp isn't found*/
276 if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL) ||
277 (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL ) ||
278 (info->phi_pred == 1 && Store_in_op == 0))
279 info->is_reducible = 1;
281 /* Search for loop invariant of Cmp.*/
282 if (info->cmp != NULL) {
283 cmp_const_block = get_nodes_block(info->cmp_const);
284 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
285 get_Block_dom_depth(cmp_const_block))
286 info->cmp_init_block = get_nodes_block(info->init);
288 info->cmp_init_block = cmp_const_block;
294 * Creates a new Add node with the correct mode from its two operands.
296 static INLINE ir_node *
297 my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
298 ir_mode *m = get_irn_mode(op1);
299 ir_mode *m2 = get_irn_mode(op2);
301 if (mode_is_reference(m2))
304 return new_r_Add(irg, b, op1, op2, m);
308 * Creates a new Sub node with the correct mode from its two operands.
310 static INLINE ir_node *
311 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
312 ir_mode *m = get_irn_mode(op1);
313 ir_mode *m2 = get_irn_mode(op2);
315 if (mode_is_reference(m) && mode_is_reference(m2))
316 m = mode_Is; /* FIXME: may be other mode! */
317 else if (mode_is_reference(m2))
319 return new_r_Sub(irg, b, op1, op2, m);
323 * Reduce an Add, Sub or Mul node
325 * @param *reduce_var The node to reduce.
326 * @param *ivi Contains the induction variable information.
328 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
330 ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
332 /* Essential conditions for a reducible node. */
333 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
336 iter_varblk = get_nodes_block(ivi->itervar_phi);
337 init_block = get_nodes_block(ivi->init);
338 irg_startblk = get_irg_start_block(current_ir_graph);
340 /* The "new_init" and the "new_cmp_const" must not be in the start block.*/
341 if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
342 init_block != iter_varblk)
343 block_init = init_block;
345 block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
347 /* To avoid that cmp is placed in the start block.*/
348 if (ivi->cmp_init_block == irg_startblk)
349 ivi->cmp_init_block = iter_varblk;
351 if (get_irn_op(reduce_var) == op_Mul) {
352 ir_node *mul_init = NULL;
353 ir_node *mul_const = NULL;
355 /* Search for constant and init of strong. */
356 ir_node *mul_right = get_Mul_right(reduce_var);
357 ir_node *mul_left = get_Mul_left(reduce_var);
358 ir_op *mul_right_op = get_irn_op(mul_right);
359 ir_op *mul_left_op = get_irn_op(mul_left);
364 ir_node *increment_block;
367 n_reduced_expressions++;
369 if (mul_right_op == op_Const) {
370 mul_const = mul_right;
373 else if (mul_left_op == op_Const) {
374 mul_const = mul_left;
375 mul_init = mul_right;
378 if (mul_const == NULL || mul_init == NULL)
381 increment_block = get_nodes_block(ivi->increment);
382 c_block = get_nodes_block(mul_const);
384 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
385 block_inc = increment_block;
389 if (! ivi->is_reducible){
392 /* Essential condition for the constant of strong. */
393 if (get_Block_dom_depth(get_nodes_block(mul_const)) >=
394 get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
398 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
399 ir_printf("The new Phi node is : %+F\n", ivi->itervar_phi);
400 ir_printf("reducing operation is : %+F\n", reduce_var);
401 ir_printf("in graph : %+F\n", current_ir_graph);
404 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
405 get_irn_mode(mul_const));
406 if (!(get_irn_op(mul_init) == op_Phi)){
407 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
408 get_irn_mode(mul_const));
409 ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
412 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
413 get_irn_mode(mul_const));
415 /* Generate a new basic induction variable. Break the data flow loop
416 initially by using an Unknown node. */
418 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
420 in[ivi->init_pred_pos] = ivi->new_init;
421 ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
422 get_irn_mode(mul_const));
423 mark_irn_visited(ivi->new_phi);
425 if (ivi->operation_code == op_Add)
426 ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
427 ivi->new_increment,ivi-> new_phi);
428 else if (ivi->operation_code == op_Sub)
429 ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
432 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
434 // This for search for a reducible successor of reduc_var.
435 reduce_var_pred = get_irn_n_outs(reduce_var);
436 if (reduce_var_pred == 1) {
437 ir_node *old_ind =get_irn_out(reduce_var, 0);
438 if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
439 get_irn_op(old_ind) == op_Mul){
440 ivi->is_reducible = 1;
441 ivi->reducible_node = old_ind;
444 /* Replace the use of the strength reduced value. */
445 exchange(reduce_var, ivi->new_phi);
448 else { /* ivi->is_reducible */
449 if (ivi->new_phi == NULL) {
450 ivi->init = new_r_Mul (current_ir_graph, block_init,
451 mul_const, ivi->init,
452 get_irn_mode(mul_const));
454 ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
455 ivi->cmp_const, mul_const, get_irn_mode(mul_const));
456 ivi->increment = new_r_Mul (current_ir_graph, block_inc,
457 ivi->increment, mul_const, get_irn_mode(mul_const));
460 ivi->new_init = new_r_Mul (current_ir_graph, block_init,
461 mul_const, ivi->new_init,
462 get_irn_mode(mul_const));
463 ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
464 ivi->new_increment, mul_const,
465 get_irn_mode(mul_const));
467 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
468 ir_printf("\nReducing operation is : %+F\n", reduce_var);
469 ir_printf("in graph : %+F\n", current_ir_graph);
474 else if (get_irn_op (reduce_var) == op_Add){
475 ir_node *add_init = NULL;
476 ir_node *add_const = NULL;
478 /* Search for constant of add. */
479 ir_node *add_right = get_Add_right(reduce_var);
480 ir_node *add_left = get_Add_left(reduce_var);
481 ir_op *add_right_op = get_irn_op(add_right);
482 ir_op *add_left_op = get_irn_op(add_left);
484 ++n_reduced_expressions;
486 if (add_right_op != op_Const)
487 add_init = add_right;
488 else if (add_left_op != op_Const)
490 if (add_right_op == op_Const || add_right_op == op_SymConst)
491 add_const = add_right;
492 else if (add_left_op == op_Const || add_left_op == op_SymConst)
493 add_const = add_left;
494 if (add_const == NULL)
496 if (ivi->new_phi == NULL) {
497 ivi->init = my_new_r_Add(current_ir_graph, block_init,
498 add_const, ivi->init);
499 if (ivi->cmp != NULL)
500 ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
501 add_const, ivi->cmp_const);
504 ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
505 add_const, ivi->new_init);
507 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
508 ir_printf("\nReducing operation is : %+F\n", reduce_var);
509 ir_printf("in graph : %+F\n", current_ir_graph);
513 else if (get_irn_op(reduce_var) == op_Sub) {
514 ir_node *sub_init = NULL;
515 ir_node *sub_const = NULL;
516 ir_node *sub_right = get_Sub_right(reduce_var);
517 ir_node *sub_left = get_Sub_left(reduce_var);
518 ir_op *sub_right_op = get_irn_op(sub_right);
519 ir_op *sub_left_op = get_irn_op(sub_left);
521 ++n_reduced_expressions;
523 /* Search for constant of Sub. */
524 if (sub_right_op != op_Const)
525 sub_init = sub_right;
526 else if (sub_left_op != op_Const)
528 if (sub_right_op == op_Const)
529 sub_const = sub_right;
530 else if (sub_left_op == op_Const)
531 sub_const = sub_left;
533 if (sub_const == NULL)
536 if (ivi->new_phi == NULL) {
537 ivi->init = my_new_r_Sub(current_ir_graph, block_init,
538 ivi->init, sub_const);
539 if (ivi->cmp != NULL)
540 ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
541 ivi->cmp_const,sub_const);
543 ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
544 ivi->new_init, sub_const);
545 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
546 ir_printf("\nReducing operation is : %+F", reduce_var);
547 ir_printf("in graph : %+F", current_ir_graph);
555 * Search for reducible successor of iteration variable.
556 * If such successor is found it will be reduced and returned,
559 * @param ivi Contains information about the induction variable.
560 * @param out A successor of iteration variable.
562 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
564 ir_node *reduced = NULL;
567 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
568 if (reduce(out, ivi))
572 out = get_irn_out(out, 0);
578 * Post walker: Find a Phi node that is a iteration variable and
581 * @param itervar_phi The iteration variable of a loop.
582 * @param env Free environment pointer.
584 static void reduce_itervar(ir_node *itervar_phi, void *env)
587 /* check if a iteration variable be reduced.*/
590 if (! is_Phi(itervar_phi))
593 /* A potential candidate is found.*/
594 ivi.itervar_phi = itervar_phi;
596 /* check if it's a induction variable.*/
597 if (is_induction_variable(&ivi)) {
600 for (i = 0; i < ivi.phi_pred; i++) {
601 ir_node *out = get_irn_out(ivi.itervar_phi, i);
602 ir_op *out_op = get_irn_op(out);
603 /* Reduce a induction variable.*/
604 if (ivi.is_reducible) {
605 if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
606 ir_node *irn_reduced = reducible(out, &ivi);
607 if (irn_reduced != NULL){
609 exchange(irn_reduced, ivi.itervar_phi);
613 /* Reduce a multiplication*/
614 else if (out_op == op_Mul)
615 if (reduce(out, &ivi) && ivi.is_reducible) {
616 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
619 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));
628 op_out = get_irn_n_outs(ivi.op);
629 for (i = 0; i < op_out; i++){
630 ir_node *out = get_irn_out(ivi.op, i);
631 ir_op *out_op = get_irn_op(out);
632 /* Try to reduce the second successor of the "ivi.op"*/
633 if (op_out == 2 && out != ivi.itervar_phi){
634 ir_node *reduced = reducible(out, &ivi);
636 exchange( reduced, ivi.op);
638 /* Try to reduce a multiplication, that is successor of "ivi.op".*/
639 else if (out_op == op_Mul)
640 if (reduce(out, &ivi) && ivi.is_reducible){
641 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
643 exchange(reduced, ivi.new_phi);
644 ivi.is_reducible = 0;
645 set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
646 set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
647 set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
650 /* Set some predecessors and modes after reduce.*/
651 if (ivi.is_reducible && reduced) {
652 if(get_irn_op(ivi.op) == op_Add)
653 if(get_Add_left(ivi.op) == ivi.itervar_phi)
654 set_Add_right(ivi.op, ivi.increment);
656 set_Add_left(ivi.op, ivi.increment);
657 else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
658 set_Sub_right(ivi.op, ivi.increment);
660 set_Sub_right(ivi.op, ivi.increment);
661 set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
662 set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
663 set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
664 if (ivi.cmp != NULL){
665 set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
666 if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
667 set_Cmp_right(ivi.cmp, ivi.cmp_const);
669 set_Cmp_left(ivi.cmp, ivi.cmp_const);
675 /* Performs strength reduction for the passed graph. */
676 void reduce_strength(ir_graph *irg) {
677 ir_graph *rem = current_ir_graph;
680 if (!get_optimize() || !get_opt_strength_red()) return;
682 current_ir_graph = irg;
684 n_reduced_expressions = 0;
686 /* -- Precompute some information -- */
687 /* Call algorithm that computes the backedges */
688 construct_cf_backedges(irg);
689 /* Call algorithm that computes the dominator trees. */
691 /* Call algorithm that computes the out edges */
692 assure_irg_outs(irg);
694 /* -- Search expressions that can be optimized -- */
695 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
697 if (get_opt_strength_red_verbose()) {
698 printf ("\n %d made new_phis und ", n_made_new_phis);
699 printf("reduced %d iteration variables "
700 "in \n graph %s.%s.\n", n_reduced_expressions,
701 get_type_name(get_entity_owner(get_irg_entity(irg))),
702 get_entity_name(get_irg_entity(irg)));
706 set_irg_outs_inconsistent(irg);
707 set_irg_loopinfo_inconsistent(irg);
710 current_ir_graph = rem;