report removed cycles
[libfirm] / ir / opt / strength_red.c
1 /**
2  *
3  * @file strength_red.c
4  *
5  * Project:     libFIRM
6  * File name:   ir/opt/strength_red.c
7  * Purpose:     Make strength reduction .
8  * Author:      Beyhan Veliev
9  * Modified by:
10  * Created:     22.8.2004
11  * CVS-ID:      $Id$
12  * Copyright:   (c) 2004 Universität Karlsruhe
13  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
14  */
15
16 # include "strength_red.h"
17
18 # include "irouts.h"
19 # include "irnode_t.h"
20 # include "irgwalk.h"
21 # include "irloop_t.h"
22 # include "ircons.h"
23 # include "irgmod.h"
24 # include "irdump_t.h"
25 # include "firmstat.h"
26
27
28 /** Counter for verbose information about optimization. */
29 static int n_reduced_expressions;
30 static int n_made_new_phis;
31
32 /** Detect basic iteration variables.
33  *
34  * The variable is represented by a subgraph as this:
35  * @verbatim
36  *
37  *       init
38  *       /|\
39  *        |
40  *   +-- Phi
41  *   |   /|\
42  *   |    |
43  *   +-->op
44  *
45  * @endverbatim
46  *
47  * Where op is a Add or Sub node and init is loop invariant.
48  *
49  * @note
50  * So far we only accept Phi nodes with two predecessors.
51  * We could expand this to Phi nodes where all predecessors
52  * are either op or loop invariant.
53  *
54  * @param info  After call contains the induction variable information.
55  *
56  * @return
57  *   The info structure.
58  */
59 induct_var_info *is_induction_variable(induct_var_info *info) {
60
61   int i, q, r;
62   int op_pred, Store_in_op, Store_in_phi;
63   ir_node *cmp_pred_bl, *cond_succ_0, *cond_succ_1, *cmp_const;
64   ir_node *loop_head;
65   ir_node *cmp_const_block;
66
67   info->operation_code   = NULL; /* The opcode of "op". */
68   info->increment        = NULL; /* The value which increase or decrease the iteration variable. */
69   info->init             = NULL; /* The start value of the iteration variable. */
70   info->op               = NULL; /* The operation which increase or decrease the iteration variable. */
71   info->l_itervar_phi    = NULL; /* The iteration variable. */
72   info->new_phi          = NULL; /* The new iteration variable. */
73   info->new_increment    = NULL; /* The new increment which replace the old one.*/
74   info->new_init         = NULL; /* The new init value of the iteration variable. */
75   info->new_op           = NULL; /* The new operation that we need after replace. */
76   info->new_cmp          = NULL; /* The new Cmp which replaces the old one. */
77   info->cmp              = NULL; /* The Cmp which breaks the loop and compares the iteration variable with a constant. */
78   info->cmp_const        = NULL; /* The other operand of Cmp. */
79   info->cmp_init_block   = NULL; /* The initial block of the Cmp. */
80   info->reducible_node   = NULL; /* The reducible nodes are save here. */
81   info->is_reducible     = 0;    /* To save information if anything is reducible. */
82   info->phi_pred         = 0;    /* To save the value of iteration variable predecessors. */
83   info->init_pred_pos    = -1;   /* To save the position of iteration variable start value. */
84   info->op_pred_pos      = -1;   /* To save the backedge of iteration variable. */
85   info->l_itervar_phi    = NULL; /* Information about loop of itervar_phi. */
86
87   assert(get_irn_op(info->itervar_phi) == op_Phi);
88
89   /*
90    * The necessary conditions for the phi node:
91    * We can handle currently Phi's with 2 predecessors, one must be a backedge.
92    */
93   if (get_irn_arity(info->itervar_phi) != 2 || !has_backedges(get_nodes_block(info->itervar_phi)))
94     return NULL;
95
96   for (i = 0; i < 2; ++i) {
97     ir_node *pred = get_Phi_pred(info->itervar_phi, i);
98     ir_op *op = get_irn_op(pred);
99
100     /* Compute if the induction variable is added or subtracted with a constant. */
101     if (op == op_Add || op == op_Sub) {
102       ir_node *n_l = get_binop_left(pred);
103       ir_node *n_r = get_binop_right(pred);
104
105       if (n_l == info->itervar_phi) {
106         info->operation_code = op;
107         info->increment      = n_r;
108         info->op_pred_pos    = i;
109         info->init_pred_pos  = i ^ 1;
110         break;
111       }
112       else if (n_r == info->itervar_phi) {
113         info->operation_code = op;
114         info->increment      = n_l;
115         info->op_pred_pos    = i;
116         info->init_pred_pos  = i ^ 1;
117         break;
118       }
119     }
120   }
121   /* check if we found something */
122   if (! info->operation_code)
123     return NULL;
124
125   /* Compute the position of the backedge. */
126   if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)) {
127     info->op   = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
128     info->init = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
129   }
130   else {
131     /* irregular control flow detected. */
132     return NULL;
133   }
134
135   /*
136    * the block of the init code should dominate the loop, else
137    * we have an irregular control flow
138    */
139   if (get_Block_dom_depth(get_nodes_block(info->init))  >=
140       get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
141     return NULL;
142   }
143
144   op_pred      = get_irn_n_outs(info->op);
145   Store_in_op  = 0;
146   Store_in_phi = 0;
147
148   /* Information about loop of itervar_phi. */
149   info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
150
151
152   info->phi_pred = get_irn_n_outs(info->itervar_phi);
153   loop_head = get_nodes_block(info->itervar_phi);
154
155   /*
156    * This "for" searches for the Cmp successor of the
157    * iter_var to reduce and marks if the iter_var have a Store
158    * successor or a successor out of loop.
159    */
160   for (i = 0; i < info->phi_pred; i++) {
161     ir_node *out = get_irn_out(info->itervar_phi, i);
162     ir_op   *out_op = get_irn_op(out);
163
164     if (out_op == op_Store)
165       Store_in_phi++;
166     else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
167       /* a Cmp can have more than one successor therefore we need this loop.*/
168       for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
169         ir_node *proj = get_irn_out(out, q);
170
171         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
172           cmp_pred_bl = get_irn_out(proj, r);
173
174           /* The wanted Cmp must be followed by a Cond successor
175              not by a Mux. */
176           if (get_irn_op(cmp_pred_bl) != op_Cond)
177             continue;
178
179           /* the binary Cond should have two successors */
180           if (get_irn_n_outs(cmp_pred_bl) != 2)
181             continue;
182
183           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
184           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
185
186           if (is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head) ||
187             is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head)) {
188             if (get_Cmp_left(out) == info->itervar_phi)
189               cmp_const = get_Cmp_right(out);
190             else
191               cmp_const = get_Cmp_left(out);
192           } else
193             continue;
194           if (info->cmp == NULL) {
195             /* A cmp is found.*/
196             info->cmp       = out;
197             info->cmp_const = cmp_const;
198           }
199           else {
200             /* We have more then one cmp with our requests, that mean cmp isn't found */
201             info->cmp = NULL;
202             return NULL;
203           }
204         }
205       }
206     }
207   }
208
209   for (i = 0; i < op_pred; ++i) {
210     ir_node *out  = get_irn_out(info->op, i);
211     ir_op *out_op = get_irn_op(out);
212
213     if (out_op == op_Store)
214       Store_in_op++;
215     else if (out_op == op_Cmp && !is_loop_invariant(out, loop_head)) {
216       /* a Cmp can have more as one successor therefore
217          I need this for loop. */
218       for (q = get_irn_n_outs(out) - 1; q >= 0; --q) {
219         ir_node *proj = get_irn_out(out, q);
220
221         for (r = get_irn_n_outs(proj) -1; r >= 0; --r) {
222           cmp_pred_bl = get_irn_out(proj, r);
223
224           /* The wanted Cmp must be followed by a Cond successor. */
225           if (get_irn_op(cmp_pred_bl) != op_Cond)
226             continue;
227
228           cond_succ_0 = get_irn_out(cmp_pred_bl, 0);
229           cond_succ_1 = get_irn_out(cmp_pred_bl, 1);
230
231           if (is_loop_invariant(get_irn_out(cond_succ_0, 0), loop_head) ||
232             is_loop_invariant(get_irn_out(cond_succ_1, 0), loop_head)) {
233             if (get_Cmp_left(out) == info->op)
234               cmp_const = get_Cmp_right(out);
235             else
236               cmp_const = get_Cmp_left(out);
237           } else
238             continue;
239           if (info->cmp == NULL) {
240             /* A cmp is found*/
241             info->cmp = out;
242             info->cmp_const = cmp_const;
243           }
244           else {
245             /* We have more then one cmp with our requests, that mean cmp isn't found*/
246             info->cmp = NULL;
247             return NULL;
248           }
249         }
250       }
251     }
252   }
253
254   if ((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && info->cmp != NULL)  ||
255       (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL )  ||
256       (info->phi_pred == 1 && Store_in_op == 0))
257     info->is_reducible = 1;
258
259   /* Search for loop invariant of Cmp.*/
260   if (info->cmp != NULL) {
261     cmp_const_block = get_nodes_block(info->cmp_const);
262     if (get_Block_dom_depth(get_nodes_block(info->init)) >=
263               get_Block_dom_depth(cmp_const_block))
264       info->cmp_init_block = get_nodes_block(info->init);
265     else
266       info->cmp_init_block = cmp_const_block;
267   }
268   return info;
269 }
270
271 /**
272  * Creates a new Add node with the correct mode from its two operands.
273  */
274 static INLINE ir_node *
275 my_new_r_Add(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
276   ir_mode *m  = get_irn_mode(op1);
277   ir_mode *m2 = get_irn_mode(op2);
278
279   if (mode_is_reference(m2))
280     m = m2;
281
282   return new_r_Add(irg, b, op1, op2, m);
283 }
284
285 /**
286  * Creates a new Sub node with the correct mode from its two operands.
287  */
288 static INLINE ir_node *
289 my_new_r_Sub(ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
290   ir_mode *m  = get_irn_mode(op1);
291   ir_mode *m2 = get_irn_mode(op2);
292
293   if (mode_is_reference(m) && mode_is_reference(m2))
294     m = mode_Is;        /* FIXME: may be other mode! */
295   else if (mode_is_reference(m2))
296     m = m2;
297   return new_r_Sub(irg, b, op1, op2, m);
298 }
299
300 /**
301  * Reduce an Add, Sub or Mul node
302  *
303  * @param *reduce_var  The node to reduce.
304  * @param *ivi         Contains the induction variable information.
305  */
306 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
307 {
308   ir_node *iter_varblk, *init_block, *irg_startblk, *block_init;
309
310   /* Essential conditions for a reducible node. */
311   if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
312     return 0;
313
314   iter_varblk  = get_nodes_block(ivi->itervar_phi);
315   init_block   = get_nodes_block(ivi->init);
316   irg_startblk = get_irg_start_block(current_ir_graph);
317
318   /* The "new_init" and the "new_cmp_const" must not be in the start block.*/
319   if (get_Block_dom_depth(init_block) > get_Block_dom_depth(irg_startblk) &&
320       init_block != iter_varblk)
321     block_init = init_block;
322   else
323     block_init = get_nodes_block(get_Block_cfgpred(iter_varblk, ivi->init_pred_pos));
324
325   /* To avoid that cmp is placed in the start block.*/
326   if (ivi->cmp_init_block == irg_startblk)
327     ivi->cmp_init_block = iter_varblk;
328
329   if (get_irn_op(reduce_var) == op_Mul) {
330     ir_node *mul_init  = NULL;
331     ir_node *mul_const = NULL;
332
333     /* Search for constant and init of strong. */
334     ir_node  *mul_right = get_Mul_right(reduce_var);
335     ir_node  *mul_left  = get_Mul_left(reduce_var);
336     ir_op *mul_right_op = get_irn_op(mul_right);
337     ir_op  *mul_left_op = get_irn_op(mul_left);
338
339     ir_node *in[2];
340     ir_node *block_inc;
341
342     ir_node *increment_block;
343     ir_node *c_block;
344
345     n_reduced_expressions++;
346
347     if (mul_right_op == op_Const) {
348       mul_const = mul_right;
349       mul_init  = mul_left;
350     }
351     else if (mul_left_op == op_Const) {
352       mul_const = mul_left;
353       mul_init  = mul_right;
354     }
355
356     if (mul_const == NULL || mul_init == NULL)
357       return 0;
358
359     increment_block = get_nodes_block(ivi->increment);
360     c_block         = get_nodes_block(mul_const);
361
362     if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
363       block_inc = increment_block;
364     else
365       block_inc = c_block;
366
367     if (! ivi->is_reducible){
368       int reduce_var_pred;
369
370       /* Essential condition for the constant of strong. */
371       if (get_Block_dom_depth(get_nodes_block(mul_const))  >=
372           get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
373         return 0;
374
375       n_made_new_phis++;
376       if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
377         printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
378         printf("reducing operation is : "); DDMN(reduce_var);
379         printf("in graph : "); DDMG(current_ir_graph);
380       }
381
382       ivi->new_increment  = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
383                                        get_irn_mode(mul_const));
384       if (!(get_irn_op(mul_init) == op_Phi)){
385         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
386                                    get_irn_mode(mul_const));
387         ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
388                                     ivi->new_increment);
389       } else
390         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
391                                    get_irn_mode(mul_const));
392
393       /* Generate a new basic induction variable. Break the data flow loop
394          initially by using an Unknown node. */
395
396       in[ivi->op_pred_pos]   = new_Unknown(get_irn_mode(ivi->new_init));
397
398       in[ivi->init_pred_pos] = ivi->new_init;
399       ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
400                                get_irn_mode(mul_const));
401       mark_irn_visited(ivi->new_phi);
402
403       if (ivi->operation_code == op_Add)
404         ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
405                                   ivi->new_increment,ivi-> new_phi);
406       else if (ivi->operation_code == op_Sub)
407         ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
408                                    ivi->new_increment);
409
410       set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
411
412       // This for search for a reducible successor of reduc_var.
413       reduce_var_pred =  get_irn_n_outs(reduce_var);
414       if (reduce_var_pred == 1) {
415         ir_node *old_ind =get_irn_out(reduce_var, 0);
416         if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
417            get_irn_op(old_ind) == op_Mul){
418           ivi->is_reducible = 1;
419           ivi->reducible_node = old_ind;
420         }
421       }
422       /* Replace the use of the strength reduced value. */
423       exchange(reduce_var, ivi->new_phi);
424       return 1;
425     }
426     else { /* ivi->is_reducible */
427       if (ivi->new_phi == NULL) {
428         ivi->init = new_r_Mul (current_ir_graph, block_init,
429                                mul_const, ivi->init,
430                                get_irn_mode(mul_const));
431         if(ivi->cmp != NULL)
432           ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
433                                       ivi->cmp_const, mul_const, get_irn_mode(mul_const));
434         ivi->increment = new_r_Mul (current_ir_graph, block_inc,
435                                     ivi->increment, mul_const, get_irn_mode(mul_const));
436       }
437       else {
438         ivi->new_init = new_r_Mul (current_ir_graph, block_init,
439                                    mul_const, ivi->new_init,
440                                    get_irn_mode(mul_const));
441         ivi->new_increment = new_r_Mul (current_ir_graph, block_inc,
442                                         ivi->new_increment, mul_const,
443                                         get_irn_mode(mul_const));
444       }
445       if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
446         printf("\nReducing operation is : "); DDMN(reduce_var);
447         printf("in graph : "); DDMG(current_ir_graph);
448       }
449       return 1;
450     }
451   }
452   else if (get_irn_op (reduce_var) == op_Add){
453     ir_node *add_init  = NULL;
454     ir_node *add_const = NULL;
455
456     /* Search for constant of add. */
457     ir_node  *add_right = get_Add_right(reduce_var);
458     ir_node  *add_left  = get_Add_left(reduce_var);
459     ir_op *add_right_op = get_irn_op(add_right);
460     ir_op  *add_left_op = get_irn_op(add_left);
461
462     ++n_reduced_expressions;
463
464     if (add_right_op != op_Const)
465       add_init = add_right;
466     else if (add_left_op != op_Const)
467       add_init = add_left;
468     if (add_right_op == op_Const || add_right_op == op_SymConst)
469       add_const = add_right;
470     else if (add_left_op == op_Const || add_left_op == op_SymConst)
471       add_const = add_left;
472     if (add_const == NULL)
473       return 0;
474     if (ivi->new_phi == NULL) {
475       ivi->init = my_new_r_Add(current_ir_graph, block_init,
476                                add_const, ivi->init);
477       if (ivi->cmp != NULL)
478         ivi->cmp_const = my_new_r_Add(current_ir_graph, ivi->cmp_init_block,
479                                       add_const, ivi->cmp_const);
480     }
481     else {
482       ivi->new_init = my_new_r_Add(current_ir_graph, block_init,
483                                    add_const, ivi->new_init);
484     }
485     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
486       printf("\nReducing operation is : "); DDMN(reduce_var);
487       printf("in graph : "); DDMG(current_ir_graph);
488     }
489     return 1;
490   }
491   else if (get_irn_op(reduce_var) == op_Sub) {
492     ir_node *sub_init  = NULL;
493     ir_node *sub_const = NULL;
494     ir_node  *sub_right = get_Sub_right(reduce_var);
495     ir_node  *sub_left  = get_Sub_left(reduce_var);
496     ir_op *sub_right_op = get_irn_op(sub_right);
497     ir_op  *sub_left_op = get_irn_op(sub_left);
498
499     ++n_reduced_expressions;
500
501     /* Search for constant of Sub. */
502     if (sub_right_op != op_Const)
503       sub_init = sub_right;
504     else if (sub_left_op != op_Const)
505       sub_init = sub_left;
506     if (sub_right_op == op_Const)
507       sub_const = sub_right;
508     else if (sub_left_op == op_Const)
509       sub_const = sub_left;
510
511     if (sub_const == NULL)
512       return 0;
513
514     if (ivi->new_phi == NULL) {
515       ivi->init = my_new_r_Sub(current_ir_graph,  block_init,
516                                ivi->init, sub_const);
517       if (ivi->cmp != NULL)
518         ivi->cmp_const = my_new_r_Sub(current_ir_graph, ivi->cmp_init_block,
519                                       ivi->cmp_const,sub_const);
520     } else
521       ivi->new_init = my_new_r_Sub (current_ir_graph, block_init,
522                                     ivi->new_init, sub_const);
523     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
524       printf("\nReducing operation is : "); DDMN(reduce_var);
525       printf("in graph : "); DDMG(current_ir_graph);
526     }
527     return 1;
528   }
529   return 0;
530 }
531
532 /**
533  * Search for reducible successor of iteration variable.
534  * If such successor is found it will be reduced and returned,
535  * else return NULL.
536  *
537  * @param ivi   Contains information about the induction variable.
538  * @param out   A successor of iteration variable.
539  */
540 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
541 {
542   ir_node *reduced = NULL;
543   int pred;
544
545   for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
546     if (reduce(out, ivi))
547       reduced = out;
548     else
549       return reduced;
550     out = get_irn_out(out, 0);
551   }
552   return reduced;
553 }
554
555 /**
556  * Post walker: Find a Phi node that is a iteration variable and
557  * try to reduce it.
558  *
559  * @param itervar_phi   The iteration variable of a loop.
560  * @param env           Free environment pointer.
561  */
562 static void reduce_itervar(ir_node *itervar_phi, void *env)
563 {
564   induct_var_info ivi;
565   /* check if a iteration variable be reduced.*/
566   int reduced = 0;
567
568   if (get_irn_op(itervar_phi) != op_Phi)
569     return;
570   /* A candidate is found.*/
571   ivi.itervar_phi = itervar_phi;
572
573   /* It musss be a induction variable.*/
574   if (is_induction_variable(&ivi)) {
575     int i, op_out;
576
577     for (i = 0; i < ivi.phi_pred; i++) {
578       ir_node *out = get_irn_out(ivi.itervar_phi, i);
579       ir_op   *out_op = get_irn_op(out);
580       /* Reduce a induction variable.*/
581       if (ivi.is_reducible) {
582         if (ivi.phi_pred == 3 && out != ivi.op && out != ivi.cmp) {
583           ir_node *irn_reduced = reducible(out, &ivi);
584           if (irn_reduced != NULL){
585             reduced = 1;
586             exchange(irn_reduced, ivi.itervar_phi);
587           }
588         }
589       }
590       /* Reduce a multiplication*/
591       else if (out_op == op_Mul)
592         if (reduce(out, &ivi) && ivi.is_reducible) {
593           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
594
595           if (reduced != NULL)
596             exchange(reduced, ivi.new_phi);
597
598           ivi.is_reducible = 0;
599           set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
600           set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
601           set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
602         }
603     }
604
605     op_out = get_irn_n_outs(ivi.op);
606     for (i = 0; i < op_out; i++){
607       ir_node *out = get_irn_out(ivi.op, i);
608       ir_op   *out_op = get_irn_op(out);
609       /* Try to reduce the second successor of the "ivi.op"*/
610       if (op_out == 2 && out != ivi.itervar_phi){
611         ir_node *reduced = reducible(out, &ivi);
612         if(reduced != NULL)
613           exchange( reduced, ivi.op);
614       }
615       /* Try to reduce a multiplication, that is successor of "ivi.op".*/
616       else if (out_op == op_Mul)
617         if (reduce(out, &ivi) && ivi.is_reducible){
618           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
619           if(reduced != NULL)
620             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));
625         }
626     }
627     /* Set some predecessors and modes after reduce.*/
628     if (ivi.is_reducible && reduced) {
629       if(get_irn_op(ivi.op) == op_Add)
630         if(get_Add_left(ivi.op) == ivi.itervar_phi)
631           set_Add_right(ivi.op, ivi.increment);
632         else
633           set_Add_left(ivi.op, ivi.increment);
634       else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
635         set_Sub_right(ivi.op, ivi.increment);
636       else
637         set_Sub_right(ivi.op, ivi.increment);
638       set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
639       set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
640       set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
641       if (ivi.cmp != NULL){
642         set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
643         if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
644           set_Cmp_right(ivi.cmp, ivi.cmp_const);
645         else
646           set_Cmp_left(ivi.cmp, ivi.cmp_const);
647       }
648     }
649   }
650 }
651
652 /* Performs strength reduction for the passed graph. */
653 void reduce_strength(ir_graph *irg) {
654   ir_graph *rem = current_ir_graph;
655   int modifiyed = 1;
656
657   if (!get_optimize() || !get_opt_strength_red()) return;
658
659   current_ir_graph = irg;
660
661   n_reduced_expressions = 0;
662   n_made_new_phis = 0;
663   /* -- Precompute some information -- */
664   /* Call algorithm that computes the backedges */
665   construct_cf_backedges(irg);
666   /* Call algorithm that computes the dominator trees. */
667   assure_doms(irg);
668   /* Call algorithm that computes the out edges */
669   assure_irg_outs(irg);
670
671   /* -- Search expressions that can be optimized -- */
672   irg_walk_graph(irg, NULL, reduce_itervar, NULL);
673
674   if (get_opt_strength_red_verbose()) {
675     printf ("\n %d made new_phis und  ", n_made_new_phis);
676     printf("reduced %d iteration variables "
677            "in \n graph %s.%s.\n", n_reduced_expressions,
678        get_type_name(get_entity_owner(get_irg_entity(irg))),
679        get_entity_name(get_irg_entity(irg)));
680   }
681
682   if (modifiyed) {
683     set_irg_outs_inconsistent(irg);
684     set_irg_loopinfo_inconsistent(irg);
685   }
686
687   current_ir_graph = rem;
688 }