added new licence header
[libfirm] / ir / opt / strength_red.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  *
22  * @file strength_red.c
23  *
24  * Project:     libFIRM
25  * File name:   ir/opt/strength_red.c
26  * Purpose:     Make strength reduction .
27  * Author:      Beyhan Veliev
28  * Modified by:
29  * Created:     22.8.2004
30  * CVS-ID:      $Id$
31  * Copyright:   (c) 2004 Universität Karlsruhe
32  */
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 # include "strength_red.h"
38
39 # include "irouts.h"
40 # include "irnode_t.h"
41 # include "irgwalk.h"
42 # include "irloop_t.h"
43 # include "ircons.h"
44 # include "irgmod.h"
45 # include "irdump_t.h"
46 # include "firmstat.h"
47
48
49 /** Counter for verbose information about optimization. */
50 static int n_reduced_expressions;
51 static int n_made_new_phis;
52
53 /** Detect basic iteration variables.
54  *
55  * The variable is represented by a subgraph as this:
56  * @verbatim
57  *
58  *       init
59  *        ^
60  *        |
61  *   +-- Phi
62  *   |    ^
63  *   |    |
64  *   +-->op
65  *
66  * @endverbatim
67  *
68  * Where op is a Add or Sub node and init is loop invariant.
69  *
70  * @note
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.
74  *
75  * @param info  After call contains the induction variable information.
76  *
77  * @return
78  *   The info structure.
79  */
80 induct_var_info *is_induction_variable(induct_var_info *info) {
81
82   int i, q, r;
83   int op_pred, Store_in_op, Store_in_phi;
84   ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
85   ir_node *loop_head;
86   ir_node *cmp_const_block;
87
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. */
107
108   assert(get_irn_op(info->itervar_phi) == op_Phi);
109
110   /*
111    * The necessary conditions for the phi node:
112    * We can handle currently Phi's with 2 predecessors, one must be a backedge.
113    */
114   if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
115     return NULL;
116
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);
120
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);
125
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;
131         break;
132       }
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;
138         break;
139       }
140     }
141   }
142   /* check if we found something */
143   if (! info->operation_code)
144     return NULL;
145
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);
150   }
151   else {
152     /* irregular control flow detected. */
153     return NULL;
154   }
155
156   /*
157    * the block of the init code should dominate the loop, else
158    * we have an irregular control flow
159    */
160   if (get_Block_dom_depth(get_nodes_block(info->init))  >=
161       get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
162     return NULL;
163   }
164
165   op_pred      = get_irn_n_outs(info->op);
166   Store_in_op  = 0;
167   Store_in_phi = 0;
168
169   /* Information about loop of itervar_phi. */
170   info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
171
172
173   info->phi_pred = get_irn_n_outs(info->itervar_phi);
174   loop_head = get_nodes_block(info->itervar_phi);
175
176   /*
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.
180    */
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);
184
185     if (out_op == op_Store)
186       Store_in_phi++;
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);
191
192         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
193           cmp_pred_bl = get_irn_out(proj, r);
194
195           /* The wanted Cmp must be followed by a Cond successor
196              not by a Mux. */
197           if (get_irn_op(cmp_pred_bl) != op_Cond)
198             continue;
199
200           /* the binary Cond should have two successors */
201           if (get_irn_n_outs(cmp_pred_bl) != 2)
202             continue;
203
204           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
205           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
206
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);
211             else
212               cmp_const = get_Cmp_left(out);
213           } else
214             continue;
215           if (info->cmp == NULL) {
216             /* A cmp is found.*/
217             info->cmp       = out;
218             info->cmp_const = cmp_const;
219           }
220           else {
221             /* We have more then one cmp with our requests, that mean cmp isn't found */
222             info->cmp = NULL;
223             return NULL;
224           }
225         }
226       }
227     }
228   }
229
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);
233
234     if (out_op == op_Store)
235       Store_in_op++;
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);
241
242         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
243           cmp_pred_bl = get_irn_out(proj, r);
244
245           /* The wanted Cmp must be followed by a Cond successor. */
246           if (get_irn_op(cmp_pred_bl) != op_Cond)
247             continue;
248
249           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
250           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
251
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);
256             else
257               cmp_const = get_Cmp_left(out);
258           } else
259             continue;
260           if (info->cmp == NULL) {
261             /* A cmp is found*/
262             info->cmp = out;
263             info->cmp_const = cmp_const;
264           }
265           else {
266             /* We have more then one cmp with our requests, that mean cmp isn't found*/
267             info->cmp = NULL;
268             return NULL;
269           }
270         }
271       }
272     }
273   }
274
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;
279
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);
286     else
287       info->cmp_init_block = cmp_const_block;
288   }
289   return info;
290 }
291
292 /**
293  * Creates a new Add node with the correct mode from its two operands.
294  */
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);
299
300   if (mode_is_reference(m2))
301     m = m2;
302
303   return new_r_Add(irg, b, op1, op2, m);
304 }
305
306 /**
307  * Creates a new Sub node with the correct mode from its two operands.
308  */
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);
313
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))
317     m = m2;
318   return new_r_Sub(irg, b, op1, op2, m);
319 }
320
321 /**
322  * Reduce an Add, Sub or Mul node
323  *
324  * @param *reduce_var  The node to reduce.
325  * @param *ivi         Contains the induction variable information.
326  */
327 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
328 {
329   ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
330
331   /* Essential conditions for a reducible node. */
332   if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
333     return 0;
334
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);
338
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;
343   else
344     block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
345
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;
349
350   if (get_irn_op(reduce_var) == op_Mul) {
351     ir_node *mul_init  = NULL;
352     ir_node *mul_const = NULL;
353
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);
359
360     ir_node *in[2];
361     ir_node *block_inc;
362
363     ir_node *increment_block;
364     ir_node *c_block;
365
366     n_reduced_expressions++;
367
368     if (mul_right_op == op_Const) {
369       mul_const = mul_right;
370       mul_init  = mul_left;
371     }
372     else if (mul_left_op == op_Const) {
373       mul_const = mul_left;
374       mul_init  = mul_right;
375     }
376
377     if (mul_const == NULL || mul_init == NULL)
378       return 0;
379
380     increment_block = get_nodes_block(ivi->increment);
381     c_block         = get_nodes_block(mul_const);
382
383     if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
384       block_inc = increment_block;
385     else
386       block_inc = c_block;
387
388     if (! ivi->is_reducible){
389       int reduce_var_pred;
390
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)))
394         return 0;
395
396       n_made_new_phis++;
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);
401       }
402
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,
409                                     ivi->new_increment);
410       } else
411         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
412                                    get_irn_mode(mul_const));
413
414       /* Generate a new basic induction variable. Break the data flow loop
415          initially by using an Unknown node. */
416
417       in[ivi->op_pred_pos]   = new_Unknown(get_irn_mode(ivi->new_init));
418
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);
423
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,
429                                    ivi->new_increment);
430
431       set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
432
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;
441         }
442       }
443       /* Replace the use of the strength reduced value. */
444       exchange(reduce_var, ivi->new_phi);
445       return 1;
446     }
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));
452         if(ivi->cmp != NULL)
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));
457       }
458       else {
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));
465       }
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);
469       }
470       return 1;
471     }
472   }
473   else if (get_irn_op (reduce_var) == op_Add){
474     ir_node *add_init  = NULL;
475     ir_node *add_const = NULL;
476
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);
482
483     ++n_reduced_expressions;
484
485     if (add_right_op != op_Const)
486       add_init = add_right;
487     else if (add_left_op != op_Const)
488       add_init = add_left;
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)
494       return 0;
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);
501     }
502     else {
503       ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
504                                    add_const, ivi->new_init);
505     }
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);
509     }
510     return 1;
511   }
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);
519
520     ++n_reduced_expressions;
521
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)
526       sub_init = sub_left;
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;
531
532     if (sub_const == NULL)
533       return 0;
534
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);
541     } else
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);
547     }
548     return 1;
549   }
550   return 0;
551 }
552
553 /**
554  * Search for reducible successor of iteration variable.
555  * If such successor is found it will be reduced and returned,
556  * else return NULL.
557  *
558  * @param ivi   Contains information about the induction variable.
559  * @param out   A successor of iteration variable.
560  */
561 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
562 {
563   ir_node *reduced = NULL;
564   int pred;
565
566   for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
567     if (reduce(out, ivi))
568       reduced = out;
569     else
570       return reduced;
571     out = get_irn_out(out, 0);
572   }
573   return reduced;
574 }
575
576 /**
577  * Post walker: Find a Phi node that is a iteration variable and
578  * try to reduce it.
579  *
580  * @param itervar_phi   The iteration variable of a loop.
581  * @param env           Free environment pointer.
582  */
583 static void reduce_itervar(ir_node *itervar_phi, void *env)
584 {
585   induct_var_info ivi;
586   /* check if a iteration variable be reduced.*/
587   int reduced = 0;
588
589   if (! is_Phi(itervar_phi))
590     return;
591
592   /* A potential candidate is found.*/
593   ivi.itervar_phi = itervar_phi;
594
595   /* check if it's a induction variable.*/
596   if (is_induction_variable(&ivi)) {
597     int i, op_out;
598
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){
607             reduced = 1;
608             exchange(irn_reduced, ivi.itervar_phi);
609           }
610         }
611       }
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);
616
617           if (reduced != NULL)
618             exchange(reduced, ivi.new_phi);
619
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));
624         }
625     }
626
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);
634         if(reduced != NULL)
635           exchange( reduced, ivi.op);
636       }
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);
641           if(reduced != NULL)
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));
647         }
648     }
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);
654         else
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);
658       else
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);
667         else
668           set_Cmp_left(ivi.cmp, ivi.cmp_const);
669       }
670     }
671   }
672 }
673
674 /* Performs strength reduction for the passed graph. */
675 void reduce_strength(ir_graph *irg) {
676   ir_graph *rem = current_ir_graph;
677   int modifiyed = 1;
678
679   if (!get_optimize() || !get_opt_strength_red()) return;
680
681   current_ir_graph = irg;
682
683   n_reduced_expressions = 0;
684   n_made_new_phis = 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. */
689   assure_doms(irg);
690   /* Call algorithm that computes the out edges */
691   assure_irg_outs(irg);
692
693   /* -- Search expressions that can be optimized -- */
694   irg_walk_graph(irg, NULL, reduce_itervar, NULL);
695
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)));
702   }
703
704   if (modifiyed) {
705     set_irg_outs_inconsistent(irg);
706     set_irg_loopinfo_inconsistent(irg);
707   }
708
709   current_ir_graph = rem;
710 }