doxygen docu fixed
[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
17 /*
18
19 reducible(o)
20    while (reducible)
21      o = reduce(o)
22
23 reduce_itervar(induct_var_info *iv)
24   for each (out o of iv) {
25     if (o is reducible) {
26        if (o is strong (Mul))
27          iv_new = reduce(o), remember_pattern(o)
28        else     // o is not strong (Add ...)
29          if (o is the only user)
30            iv_new = reducible(o)
31     }
32   }
33
34 */
35
36 # include "strength_red.h"
37
38 # include "irouts.h"
39 # include "irnode_t.h"
40 # include "irgwalk.h"
41 # include "irloop_t.h"
42 # include "ircons.h"
43 # include "irgmod.h"
44 # include "irdump_t.h"
45 # include "firmstat.h"
46
47
48 /** Counter for verbose information about optimization. */
49 static int n_reduced_expressions;
50 static int n_made_new_phis;
51
52 /** Detect basic iteration variables.
53  *
54  * The variable ir represented by a subgraph as this:
55  *
56  *       init
57  *       /|\
58  *        |
59  *   |-- Phi
60  *   |   /|\
61  *   |    |
62  *   |-->op
63  *
64  * Where op is a Add or Sub, and init is loop invariant.
65  * @@@ So far we only accept Phi nodes with two predecessors.
66  * We could expand this to Phi nodes where all preds are either
67  * op or loop invariant.
68  *
69  * @param n     A phi node.
70  * @param info  After call contains the induction variable information.
71  */
72
73 induct_var_info *is_induction_variable (induct_var_info *info) {
74
75   int i;
76   int op_pred, Store_in_op, Store_in_phi, cmp_in_phi;
77
78   info->c                = NULL;
79   info->cmp              = NULL;
80   info->cmp_const        = NULL;
81   info->cmp_init_block   = NULL;
82   info->increment        = NULL;
83   info->init             = NULL;
84   info->l_itervar_phi    = NULL;
85   info->new_add          = NULL;
86   info->new_cmp          = NULL;
87   info->new_increment    = NULL;
88   info->new_init         = NULL;
89   info->new_op           = NULL;
90   info->new_phi          = NULL;
91   info->operation_code   = NULL;
92   info->op               = NULL;
93   info->old_ind          = NULL;
94   info->reducible_node   = NULL;
95   info->out_loop_res     = 1;
96   info->reducible        = 0;
97   info->phi_pred         = 0;
98   info->strong_reduced   = 0;
99   info->init_pred_pos    = -1;
100   info->op_pred_pos      = -1;
101
102   assert(get_irn_op(info->itervar_phi) == op_Phi);
103
104   /* The necessary conditions for the phi node. */
105   if (get_irn_arity(info->itervar_phi) != 2             ||
106       !has_backedges(get_nodes_block(info->itervar_phi))  )
107     return NULL;
108
109   for (i = 0; i < 2; ++i) {
110     ir_node *pred = get_Phi_pred(info->itervar_phi, i);
111     ir_op *op = get_irn_op(pred);
112
113     /* Compute if the induction variable is added or substracted with a constant . */
114     if (op == op_Add || op == op_Sub) {
115       ir_node *n_l = get_binop_left(pred);
116       ir_node *n_r = get_binop_right(pred);
117
118       if (n_l == info->itervar_phi) {
119         info->operation_code = op;
120         info->increment      = n_r;
121         info->op_pred_pos    = i;
122         info->init_pred_pos  = i ^ 1;
123         break;
124       }
125       else if (n_r == info->itervar_phi) {
126         info->operation_code = op;
127         info->increment      = n_l;
128         info->op_pred_pos    = i;
129         info->init_pred_pos  = i ^ 1;
130         break;
131       }
132     }
133   }
134   /* check if we found something */
135   if (! info->operation_code)
136     return NULL;
137
138   /* Compute the position of the backedge. */
139   if (is_backedge(get_nodes_block(info->itervar_phi), info->op_pred_pos)){
140     info->op     = get_Phi_pred(info->itervar_phi, info->op_pred_pos);
141     info->init   = get_Phi_pred(info->itervar_phi, info->init_pred_pos);
142   }
143   else {
144     /* irregular control flow detected. */
145     return NULL;
146   }
147
148   if (get_Block_dom_depth(get_nodes_block(info->init))  >=
149       get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
150     return NULL;
151   }
152
153   /* This "for" marks if the iteration operation have a Store successor .*/
154   op_pred      = get_irn_n_outs(info->op);
155   Store_in_op  = 0;
156   Store_in_phi = 0;
157   cmp_in_phi   = 0;
158   for (i = 0; i < op_pred; ++i){
159     ir_node *out  = get_irn_out(info->op, i);
160     ir_op *out_op = get_irn_op(out);
161     if (out_op == op_Store)
162       Store_in_op++;
163   }
164
165   /* Information about loop of itervar_phi. */
166   info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
167
168   /* This "for" searches for the Cmp successor of the
169      iter_var to reduce and marks if the iter_var have a Store
170      successor or a successor out of loop.*/
171   info->phi_pred = get_irn_n_outs(info->itervar_phi);
172   for (i = 0; i < info->phi_pred; ++i) {
173     ir_node *out = get_irn_out(info->itervar_phi, i);
174     ir_op   *out_op = get_irn_op(out);
175
176     if ((get_irn_loop(get_nodes_block(out)) != info->l_itervar_phi) &&
177         ( get_Block_dom_depth(get_nodes_block(out))  >
178           get_Block_dom_depth(get_nodes_block(info->itervar_phi))))
179       info->out_loop_res = 0;
180
181     if (out_op == op_Store)
182       Store_in_phi++;
183     else if (out_op == op_Cmp){
184       info->cmp = out;
185       cmp_in_phi++;
186     }
187   }
188
189   if((info->phi_pred == 3 && op_pred == 1 && Store_in_phi == 0 && cmp_in_phi == 1)  ||
190      (info->phi_pred == 2 && op_pred == 2 && Store_in_op == 0 && info->cmp != NULL )  ||
191      (info->phi_pred == 1 && Store_in_op == 0))
192     info->reducible = 1;
193
194   // Search for loop invariant of Cmp.
195   if (info->cmp != NULL) {
196     ir_node *cmp_const_block;
197
198     if (get_Cmp_left(info->cmp) == info->itervar_phi)
199       info->cmp_const = get_Cmp_right(info->cmp);
200     else
201       info->cmp_const = get_Cmp_left(info->cmp);
202
203     cmp_const_block = get_nodes_block(info->cmp_const);
204     if (get_Block_dom_depth(get_nodes_block(info->init)) >=
205         get_Block_dom_depth(cmp_const_block))
206       info->cmp_init_block = get_nodes_block(info->init);
207     else
208       info->cmp_init_block = cmp_const_block;
209   }
210   return info;
211 }
212
213 /**
214  * Creates a new Add node from operands.
215  */
216 static INLINE ir_node *
217 my_new_r_Add (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
218   ir_mode *m  = get_irn_mode(op1);
219   ir_mode *m2 = get_irn_mode(op2);
220
221   if (mode_is_reference(m2))
222     m = m2;
223   return new_r_Add(irg, b, op1, op2, m);
224 }
225
226 /**
227  * Creates a new Sub node from operands.
228  */
229 static INLINE ir_node *
230 my_new_r_Sub (ir_graph *irg, ir_node *b, ir_node *op1, ir_node *op2) {
231   ir_mode *m  = get_irn_mode(op1);
232   ir_mode *m2 = get_irn_mode(op2);
233
234   if (mode_is_reference(m) && mode_is_reference(m2))
235     m = mode_Is;        /* FIXME: may be other mode! */
236   else if (mode_is_reference(m2))
237     m = m2;
238   return new_r_Sub(irg, b, op1, op2, m);
239 }
240
241 /* Reduce a Add, Sub or Mul node
242  *
243  * @param *reduce_var  The node to reduce.
244  * @param *ivi         Contains the induction variable information.
245  */
246 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
247 {
248   // Essential conditions for a reducable node.
249   if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
250     return 0;
251
252   if (get_irn_op(reduce_var) == op_Mul) {
253     ir_node *mul_init  = NULL;
254     ir_node *mul_const = NULL;
255
256     // Search for constant and init of strong.
257     ir_node  *mul_right = get_Mul_right(reduce_var);
258     ir_node  *mul_left  = get_Mul_left(reduce_var);
259     ir_op *mul_right_op = get_irn_op(mul_right);
260     ir_op  *mul_left_op = get_irn_op(mul_left);
261
262     ir_node *in[2], *block_init;
263     ir_node *block_inc;
264
265     ir_node *init_block;
266     ir_node *increment_block;
267     ir_node *c_block;
268
269     n_reduced_expressions++;
270
271     if (mul_right_op == op_Const) {
272       mul_const = mul_right;
273       mul_init  = mul_left;
274     }
275     else if (mul_left_op == op_Const) {
276       mul_const = mul_left;
277       mul_init  = mul_right;
278     }
279
280     if (mul_const == NULL || mul_init == NULL)
281       return 0;
282
283     init_block      = get_nodes_block(mul_init);
284     increment_block = get_nodes_block(ivi->increment);
285     c_block         = get_nodes_block(mul_const);
286
287     if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
288       block_inc = increment_block;
289     else
290       block_inc = c_block;
291
292     if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(c_block))
293       block_init = init_block;
294     else
295       block_init = c_block;
296
297     if (! ivi->reducible){
298       int reduce_var_pred;
299
300       // Essential condition for the constant of strong.
301       if (get_Block_dom_depth(get_nodes_block(mul_const))  >=
302           get_Block_dom_depth(get_nodes_block(ivi->itervar_phi)))
303         return 0;
304
305       n_made_new_phis++;
306       if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
307         printf("The new Phi node is : "); DDMN(ivi->itervar_phi);
308         printf("reducing operation is : "); DDMN(reduce_var);
309         printf("in graph : "); DDMG(current_ir_graph);
310       }
311
312       ivi->new_increment  = new_r_Mul (current_ir_graph, block_inc, ivi->increment, mul_const,
313                                        get_irn_mode(mul_const));
314       if (!(get_irn_op(mul_init) == op_Phi)){
315         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
316                                    get_irn_mode(mul_const));
317         ivi->new_init = my_new_r_Add(current_ir_graph, block_init, ivi->new_init,
318                                     ivi->new_increment);
319       } else
320         ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
321                                    get_irn_mode(mul_const));
322
323       /* Generate a new basic induction variable. Break the data flow loop
324          initially by using an Unknown node. */
325
326       in[ivi->op_pred_pos]   = new_Unknown(get_irn_mode(ivi->new_init));
327
328       in[ivi->init_pred_pos] = ivi->new_init;
329       ivi->new_phi = new_r_Phi(current_ir_graph, get_nodes_block(ivi->itervar_phi), 2, in,
330                                get_irn_mode(mul_const));
331       mark_irn_visited(ivi->new_phi);
332
333       if (ivi->operation_code == op_Add)
334         ivi->new_op = my_new_r_Add(current_ir_graph, get_nodes_block(ivi->op),
335                                   ivi->new_increment,ivi-> new_phi);
336       else if (ivi->operation_code == op_Sub)
337         ivi->new_op = my_new_r_Sub(current_ir_graph, get_nodes_block(ivi->op),ivi-> new_phi,
338                                    ivi->new_increment);
339
340       set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
341
342
343
344
345
346       // This for search for a reducible successor of reduc_var.
347       reduce_var_pred =  get_irn_n_outs(reduce_var);
348       if (reduce_var_pred == 1) {
349         ir_node *old_ind =get_irn_out(reduce_var, 0);
350         if(get_irn_op(old_ind) == op_Add || get_irn_op(old_ind) == op_Sub ||
351            get_irn_op(old_ind) == op_Mul){
352           ivi->reducible = 1;
353           ivi->reducible_node = old_ind;
354         }
355       }
356       /* Replace the use of the strength reduced value. */
357       exchange(reduce_var, ivi->new_phi);
358       return 1;
359     }
360     else { /* ivi->reducible */
361       if(ivi->new_phi == NULL){
362         ivi->init = new_r_Mul (current_ir_graph, get_nodes_block(ivi->init),
363                                mul_const, ivi->init,
364                                get_irn_mode(mul_const));
365         if(ivi->cmp != NULL)
366           ivi->cmp_const = new_r_Mul (current_ir_graph, ivi->cmp_init_block,
367                                       ivi->cmp_const, mul_const, get_irn_mode(mul_const));
368         ivi->increment = new_r_Mul (current_ir_graph, block_init,
369                                     ivi->increment, mul_const, get_irn_mode(mul_const));
370       }else {
371         ivi->new_init = new_r_Mul (current_ir_graph, get_nodes_block(ivi->init),
372                                    mul_const, ivi->new_init,
373                                    get_irn_mode(mul_const));
374         ivi->new_increment = new_r_Mul (current_ir_graph, block_init,
375                                         ivi->new_increment, mul_const,
376                                         get_irn_mode(mul_const));
377       }
378       if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
379         printf("\nReducing operation is : "); DDMN(reduce_var);
380         printf("in graph : "); DDMG(current_ir_graph);
381       }
382       return 1;
383     }
384
385   }else if (get_irn_op (reduce_var) == op_Add){
386     ir_node *add_init  = NULL;
387     ir_node *add_const = NULL;
388
389     // Search for constant of add.
390     ir_node  *add_right = get_Add_right(reduce_var);
391     ir_node  *add_left  = get_Add_left(reduce_var);
392     ir_op *add_right_op = get_irn_op(add_right);
393     ir_op  *add_left_op = get_irn_op(add_left);
394
395     n_reduced_expressions++;
396
397     if (add_right_op != op_Const)
398       add_init = add_right;
399     else if (add_left_op != op_Const)
400       add_init = add_left;
401     if (add_right_op == op_Const || add_right_op == op_SymConst)
402       add_const = add_right;
403     else if (add_left_op == op_Const || add_left_op == op_SymConst)
404       add_const = add_left;
405     if (add_const == NULL) return 0;
406     if (ivi->new_phi == NULL){
407       ivi->init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init),
408                                 add_const, ivi->init);
409       if(ivi->cmp != NULL)
410         ivi->cmp_const = my_new_r_Add (current_ir_graph, ivi->cmp_init_block,
411                                        add_const, ivi->cmp_const);
412     } else{
413       ivi->new_init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init),
414                                     add_const, ivi->new_init);
415     }
416     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
417       printf("\nReducing operation is : "); DDMN(reduce_var);
418       printf("in graph : "); DDMG(current_ir_graph);
419     }
420     return 1;
421   } else if(get_irn_op(reduce_var) == op_Sub ){
422     ir_node *sub_init  = NULL;
423     ir_node *sub_const = NULL;
424     // Search for constant of sub.
425     ir_node  *sub_right = get_Sub_right(reduce_var);
426     ir_node  *sub_left  = get_Sub_left(reduce_var);
427     ir_op *sub_right_op = get_irn_op(sub_right);
428     ir_op  *sub_left_op = get_irn_op(sub_left);
429
430     n_reduced_expressions++;
431
432     if (sub_right_op != op_Const)
433       sub_init = sub_right;
434     else if (sub_left_op != op_Const)
435       sub_init = sub_left;
436     if (sub_right_op == op_Const)
437       sub_const = sub_right;
438     else if (sub_left_op == op_Const)
439       sub_const = sub_left;
440
441     if (sub_const == NULL) return 0;
442
443     if (ivi->new_phi == NULL) {
444       ivi->init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
445                                 ivi->init, sub_const);
446       if (ivi->cmp != NULL)
447         ivi->cmp_const =my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
448                                       ivi->cmp_const,sub_const);
449     } else
450       ivi->new_init = my_new_r_Sub (current_ir_graph, get_nodes_block(ivi->init),
451                                     ivi->new_init, sub_const);
452     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
453       printf("\nReducing operation is : "); DDMN(reduce_var);
454       printf("in graph : "); DDMG(current_ir_graph);
455     }
456     return 1;
457   }
458   return 0;
459 }
460
461 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
462 {
463   ir_node *reduced = NULL;
464   int pred;
465
466   for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
467     if (reduce(out, ivi))
468       reduced = out;
469     else
470       return reduced;
471     out = get_irn_out(out, 0);
472   }
473   return reduced;
474 }
475
476 /**
477  * Reduce a node.
478  *
479  * @param *itervar_phi   The iteration variable of a loop.
480  * @param *env           Free environment pointer.
481  */
482 static void reduce_itervar(ir_node *itervar_phi, void *env)
483 {
484   induct_var_info ivi;
485
486   if (get_irn_op(itervar_phi) != op_Phi)
487     return;
488
489   ivi.itervar_phi = itervar_phi;
490
491   /* This "if" finds the iteration variable. */
492   if (is_induction_variable(&ivi)) {
493     int i, op_out;
494
495     for (i = 0; i < ivi.phi_pred; i++) {
496       ir_node *out = get_irn_out(ivi.itervar_phi, i);
497       ir_op   *out_op = get_irn_op(out);
498       if(ivi.reducible){
499         if(ivi.phi_pred == 3 && out != ivi.op && out !=ivi.cmp){
500           ir_node *reduced = reducible(out, &ivi);
501           if (reduced != NULL)
502             exchange( reduced, ivi.itervar_phi);
503         }
504       } else if (out_op == op_Mul)
505         if(reduce(out, &ivi) && ivi.reducible){
506           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
507           if(reduced != NULL)
508             exchange(reduced, ivi.new_phi);
509           ivi.reducible = 0;
510           set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
511           set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
512           set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
513         }
514     }
515
516     op_out = get_irn_n_outs(ivi.op);
517     for (i = 0; i < op_out; i++){
518       ir_node *out = get_irn_out(ivi.op, i);
519       ir_op   *out_op = get_irn_op(out);
520       if(op_out == 2 && out != ivi.itervar_phi){
521         ir_node *reduced = reducible(out, &ivi);
522         if(reduced != NULL)
523           exchange( reduced, ivi.op);
524       }else if (out_op == op_Mul)
525         if(reduce(out, &ivi) && ivi.reducible){
526           ir_node *reduced = reducible(ivi.reducible_node, &ivi);
527           if(reduced != NULL)
528             exchange(reduced, ivi.new_phi);
529           ivi.reducible = 0;
530           set_Phi_pred(ivi.new_phi, ivi.init_pred_pos, ivi.new_init);
531           set_irn_mode(ivi.new_phi,get_irn_mode(ivi.new_init));
532           set_irn_mode(ivi.new_op,get_irn_mode(ivi.new_phi));
533         }
534     }
535
536     if(ivi.reducible){
537       if(get_irn_op(ivi.op) == op_Add)
538         if(get_Add_left(ivi.op) == ivi.itervar_phi)
539           set_Add_right(ivi.op, ivi.increment);
540         else
541           set_Add_left(ivi.op, ivi.increment);
542       else if(get_Sub_left(ivi.op) == ivi.itervar_phi)
543         set_Sub_right(ivi.op, ivi.increment);
544       else
545         set_Sub_right(ivi.op, ivi.increment);
546       set_Phi_pred(ivi.itervar_phi, ivi.init_pred_pos, ivi.init);
547       set_irn_mode(ivi.itervar_phi, get_irn_mode(ivi.init));
548       set_irn_mode(ivi.op, get_irn_mode(ivi.itervar_phi));
549       if (ivi.cmp != NULL){
550         set_irn_mode(ivi.cmp_const, get_irn_mode(ivi.itervar_phi));
551         if(get_Cmp_left(ivi.cmp) == ivi.itervar_phi)
552           set_Cmp_right(ivi.cmp, ivi.cmp_const);
553         else
554           set_Cmp_left(ivi.cmp, ivi.cmp_const);
555       }
556     }
557   }
558 }
559
560 /* Performs strength reduction for the passed graph. */
561 void reduce_strength(ir_graph *irg) {
562   ir_graph *rem = current_ir_graph;
563
564   if (!get_optimize() || !get_opt_strength_red()) return;
565
566   current_ir_graph = irg;
567
568   n_reduced_expressions = 0;
569   n_made_new_phis = 0;
570   /* -- Precompute some information -- */
571   /* Call algorithm that computes the backedges */
572   construct_cf_backedges(irg);
573   /* Call algorithm that computes the dominator trees. */
574   compute_doms(irg);
575   /* Call algorithm that computes the out edges */
576   compute_outs(irg);
577
578   /* -- Search expressions that can be optimized -- */
579   irg_walk_graph(irg, NULL, reduce_itervar, NULL);
580
581   if (get_opt_strength_red_verbose()) {
582     printf ("\n %d made new_phis und  ", n_made_new_phis);
583     printf("reduced %d iteration variables "
584            "in \n graph %s.%s.\n", n_reduced_expressions,
585        get_type_name(get_entity_owner(get_irg_entity(irg))),
586        get_entity_name(get_irg_entity(irg)));
587   }
588
589   current_ir_graph = rem;
590 }