fixed warnigns
[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_t.h"
38 #include "iroptimize.h"
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 "irprintf.h"
47 #include "firmstat.h"
48
49
50 /** Counter for verbose information about optimization. */
51 static int n_reduced_expressions;
52 static int n_made_new_phis;
53
54 /** Detect basic iteration variables.
55  *
56  * The variable is represented by a subgraph as this:
57  * @verbatim
58  *
59  *       init
60  *        ^
61  *        |
62  *   +-- Phi
63  *   |    ^
64  *   |    |
65  *   +-->op
66  *
67  * @endverbatim
68  *
69  * Where op is a Add or Sub node and init is loop invariant.
70  *
71  * @note
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.
75  *
76  * @param info  After call contains the induction variable information.
77  *
78  * @return
79  *   The info structure.
80  */
81 induct_var_info *is_induction_variable(induct_var_info *info) {
82
83   int i, q, r;
84   int op_pred, Store_in_op, Store_in_phi;
85   ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
86   ir_node *loop_head;
87   ir_node *cmp_const_block;
88
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. */
108
109   assert(get_irn_op(info->itervar_phi) == op_Phi);
110
111   /*
112    * The necessary conditions for the phi node:
113    * We can handle currently Phi's with 2 predecessors, one must be a backedge.
114    */
115   if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
116     return NULL;
117
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);
121
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);
126
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;
132         break;
133       }
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;
139         break;
140       }
141     }
142   }
143   /* check if we found something */
144   if (! info->operation_code)
145     return NULL;
146
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);
151   }
152   else {
153     /* irregular control flow detected. */
154     return NULL;
155   }
156
157   /*
158    * the block of the init code should dominate the loop, else
159    * we have an irregular control flow
160    */
161   if (get_Block_dom_depth(get_nodes_block(info->init))  >=
162       get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
163     return NULL;
164   }
165
166   op_pred      = get_irn_n_outs(info->op);
167   Store_in_op  = 0;
168   Store_in_phi = 0;
169
170   /* Information about loop of itervar_phi. */
171   info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
172
173
174   info->phi_pred = get_irn_n_outs(info->itervar_phi);
175   loop_head = get_nodes_block(info->itervar_phi);
176
177   /*
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.
181    */
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);
185
186     if (out_op == op_Store)
187       Store_in_phi++;
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);
192
193         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
194           cmp_pred_bl = get_irn_out(proj, r);
195
196           /* The wanted Cmp must be followed by a Cond successor
197              not by a Mux. */
198           if (get_irn_op(cmp_pred_bl) != op_Cond)
199             continue;
200
201           /* the binary Cond should have two successors */
202           if (get_irn_n_outs(cmp_pred_bl) != 2)
203             continue;
204
205           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
206           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
207
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);
212             else
213               cmp_const = get_Cmp_left(out);
214           } else
215             continue;
216           if (info->cmp == NULL) {
217             /* A cmp is found.*/
218             info->cmp       = out;
219             info->cmp_const = cmp_const;
220           }
221           else {
222             /* We have more then one cmp with our requests, that mean cmp isn't found */
223             info->cmp = NULL;
224             return NULL;
225           }
226         }
227       }
228     }
229   }
230
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);
234
235     if (out_op == op_Store)
236       Store_in_op++;
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);
242
243         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
244           cmp_pred_bl = get_irn_out(proj, r);
245
246           /* The wanted Cmp must be followed by a Cond successor. */
247           if (get_irn_op(cmp_pred_bl) != op_Cond)
248             continue;
249
250           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
251           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
252
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);
257             else
258               cmp_const = get_Cmp_left(out);
259           } else
260             continue;
261           if (info->cmp == NULL) {
262             /* A cmp is found*/
263             info->cmp = out;
264             info->cmp_const = cmp_const;
265           }
266           else {
267             /* We have more then one cmp with our requests, that mean cmp isn't found*/
268             info->cmp = NULL;
269             return NULL;
270           }
271         }
272       }
273     }
274   }
275
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;
280
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);
287     else
288       info->cmp_init_block = cmp_const_block;
289   }
290   return info;
291 }
292
293 /**
294  * Creates a new Add node with the correct mode from its two operands.
295  */
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);
300
301   if (mode_is_reference(m2))
302     m = m2;
303
304   return new_r_Add(irg, b, op1, op2, m);
305 }
306
307 /**
308  * Creates a new Sub node with the correct mode from its two operands.
309  */
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);
314
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))
318     m = m2;
319   return new_r_Sub(irg, b, op1, op2, m);
320 }
321
322 /**
323  * Reduce an Add, Sub or Mul node
324  *
325  * @param *reduce_var  The node to reduce.
326  * @param *ivi         Contains the induction variable information.
327  */
328 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
329 {
330   ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
331
332   /* Essential conditions for a reducible node. */
333   if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
334     return 0;
335
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);
339
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;
344   else
345     block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
346
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;
350
351   if (get_irn_op(reduce_var) == op_Mul) {
352     ir_node *mul_init  = NULL;
353     ir_node *mul_const = NULL;
354
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);
360
361     ir_node *in[2];
362     ir_node *block_inc;
363
364     ir_node *increment_block;
365     ir_node *c_block;
366
367     n_reduced_expressions++;
368
369     if (mul_right_op == op_Const) {
370       mul_const = mul_right;
371       mul_init  = mul_left;
372     }
373     else if (mul_left_op == op_Const) {
374       mul_const = mul_left;
375       mul_init  = mul_right;
376     }
377
378     if (mul_const == NULL || mul_init == NULL)
379       return 0;
380
381     increment_block = get_nodes_block(ivi->increment);
382     c_block         = get_nodes_block(mul_const);
383
384     if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
385       block_inc = increment_block;
386     else
387       block_inc = c_block;
388
389     if (! ivi->is_reducible){
390       int reduce_var_pred;
391
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)))
395         return 0;
396
397       n_made_new_phis++;
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);
402       }
403
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,
410                                     ivi->new_increment);
411       } else
412         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
413                                    get_irn_mode(mul_const));
414
415       /* Generate a new basic induction variable. Break the data flow loop
416          initially by using an Unknown node. */
417
418       in[ivi->op_pred_pos]   = new_Unknown(get_irn_mode(ivi->new_init));
419
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);
424
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,
430                                    ivi->new_increment);
431
432       set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
433
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;
442         }
443       }
444       /* Replace the use of the strength reduced value. */
445       exchange(reduce_var, ivi->new_phi);
446       return 1;
447     }
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));
453         if(ivi->cmp != NULL)
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));
458       }
459       else {
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));
466       }
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);
470       }
471       return 1;
472     }
473   }
474   else if (get_irn_op (reduce_var) == op_Add){
475     ir_node *add_init  = NULL;
476     ir_node *add_const = NULL;
477
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);
483
484     ++n_reduced_expressions;
485
486     if (add_right_op != op_Const)
487       add_init = add_right;
488     else if (add_left_op != op_Const)
489       add_init = add_left;
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)
495       return 0;
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);
502     }
503     else {
504       ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
505                                    add_const, ivi->new_init);
506     }
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);
510     }
511     return 1;
512   }
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);
520
521     ++n_reduced_expressions;
522
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)
527       sub_init = sub_left;
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;
532
533     if (sub_const == NULL)
534       return 0;
535
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);
542     } else
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);
548     }
549     return 1;
550   }
551   return 0;
552 }
553
554 /**
555  * Search for reducible successor of iteration variable.
556  * If such successor is found it will be reduced and returned,
557  * else return NULL.
558  *
559  * @param ivi   Contains information about the induction variable.
560  * @param out   A successor of iteration variable.
561  */
562 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
563 {
564   ir_node *reduced = NULL;
565   int pred;
566
567   for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
568     if (reduce(out, ivi))
569       reduced = out;
570     else
571       return reduced;
572     out = get_irn_out(out, 0);
573   }
574   return reduced;
575 }
576
577 /**
578  * Post walker: Find a Phi node that is a iteration variable and
579  * try to reduce it.
580  *
581  * @param itervar_phi   The iteration variable of a loop.
582  * @param env           Free environment pointer.
583  */
584 static void reduce_itervar(ir_node *itervar_phi, void *env)
585 {
586   induct_var_info ivi;
587   /* check if a iteration variable be reduced.*/
588   int reduced = 0;
589   (void) env;
590
591   if (! is_Phi(itervar_phi))
592     return;
593
594   /* A potential candidate is found.*/
595   ivi.itervar_phi = itervar_phi;
596
597   /* check if it's a induction variable.*/
598   if (is_induction_variable(&ivi)) {
599     int i, op_out;
600
601     for (i = 0; i < ivi.phi_pred; i++) {
602       ir_node *out = get_irn_out(ivi.itervar_phi, i);
603       ir_op   *out_op = get_irn_op(out);
604       /* Reduce a induction variable.*/
605       if (ivi.is_reducible) {
606         if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
607           ir_node *irn_reduced = reducible(out, &ivi);
608           if (irn_reduced != NULL){
609             reduced = 1;
610             exchange(irn_reduced, ivi.itervar_phi);
611           }
612         }
613       }
614       /* Reduce a multiplication*/
615       else if (out_op == op_Mul)
616         if (reduce(out, &ivi) && ivi.is_reducible) {
617           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
618
619           if (reduced != NULL)
620             exchange(reduced, ivi.new_phi);
621
622           ivi.is_reducible = 0;
623           set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
624           set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
625           set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
626         }
627     }
628
629     op_out = get_irn_n_outs(ivi.op);
630     for (i = 0; i < op_out; i++){
631       ir_node *out = get_irn_out(ivi.op, i);
632       ir_op   *out_op = get_irn_op(out);
633       /* Try to reduce the second successor of the "ivi.op"*/
634       if (op_out == 2 && out != ivi.itervar_phi){
635         ir_node *reduced = reducible(out, &ivi);
636         if(reduced != NULL)
637           exchange( reduced, ivi.op);
638       }
639       /* Try to reduce a multiplication, that is successor of "ivi.op".*/
640       else if (out_op == op_Mul)
641         if (reduce(out, &ivi) && ivi.is_reducible){
642           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
643           if(reduced != NULL)
644             exchange(reduced, ivi.new_phi);
645           ivi.is_reducible = 0;
646           set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
647           set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
648           set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
649         }
650     }
651     /* Set some predecessors and modes after reduce.*/
652     if (ivi.is_reducible && reduced) {
653       if(get_irn_op(ivi.op) == op_Add)
654         if(get_Add_left(ivi.op) == ivi.itervar_phi)
655           set_Add_right(ivi.op, ivi.increment);
656         else
657           set_Add_left(ivi.op, ivi.increment);
658       else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
659         set_Sub_right(ivi.op, ivi.increment);
660       else
661         set_Sub_right(ivi.op, ivi.increment);
662       set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
663       set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
664       set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
665       if (ivi.cmp != NULL){
666         set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
667         if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
668           set_Cmp_right(ivi.cmp, ivi.cmp_const);
669         else
670           set_Cmp_left(ivi.cmp, ivi.cmp_const);
671       }
672     }
673   }
674 }
675
676 /* Performs strength reduction for the passed graph. */
677 void reduce_strength(ir_graph *irg) {
678   ir_graph *rem = current_ir_graph;
679   int modifiyed = 1;
680
681   if (!get_optimize() || !get_opt_strength_red()) return;
682
683   current_ir_graph = irg;
684
685   n_reduced_expressions = 0;
686   n_made_new_phis = 0;
687   /* -- Precompute some information -- */
688   /* Call algorithm that computes the backedges */
689   construct_cf_backedges(irg);
690   /* Call algorithm that computes the dominator trees. */
691   assure_doms(irg);
692   /* Call algorithm that computes the out edges */
693   assure_irg_outs(irg);
694
695   /* -- Search expressions that can be optimized -- */
696   irg_walk_graph(irg, NULL, reduce_itervar, NULL);
697
698   if (get_opt_strength_red_verbose()) {
699     printf ("\n %d made new_phis und  ", n_made_new_phis);
700     printf("reduced %d iteration variables "
701            "in \n graph %s.%s.\n", n_reduced_expressions,
702        get_type_name(get_entity_owner(get_irg_entity(irg))),
703        get_entity_name(get_irg_entity(irg)));
704   }
705
706   if (modifiyed) {
707     set_irg_outs_inconsistent(irg);
708     set_irg_loopinfo_inconsistent(irg);
709   }
710
711   current_ir_graph = rem;
712 }