\!C99
[libfirm] / ir / opt / strength_red.c
1 /**
2  *
3  * @file irsimpeltype.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.2003
11  * CVS-ID:      $Id$
12  * Copyright:   (c) 2003 Universität Karlsruhe
13  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
14  */
15
16
17 # include "strength_red.h"
18
19 # include "irouts.h"
20 # include "irnode_t.h"
21 # include "irgwalk.h"
22 # include "irloop_t.h"
23 # include "ircons.h"
24 # include "irgmod.h"
25 # include "irdump.h"
26 # include "firmstat.h"
27
28
29 /* The information needed for an induction variable */
30 typedef struct _induct_var_info {
31   ir_op   *operation_code;
32   ir_node *increment, *init, *op;
33   int      be_pos;
34   int      init_pred_pos;
35   int      op_pred_pos;
36 } induct_var_info;
37
38 /** Counter for verbose information about optimization. */
39 static int n_reduced_expressions;
40
41 /** Detect basic iteration variables.
42  *
43  * The variable ir represented by a subgraph as this:
44  *
45  *       init
46  *       /|\
47  *        |
48  *   |-- Phi
49  *   |   /|\
50  *   |    |
51  *   |-->op
52  *
53  * Where op is a Add or Sub, and init is loop invariant.
54  * @@@ So far we only accept Phi nodes with two predecessors.
55  * We could expand this to Phi nodes where all preds are either
56  * op or loop invariant.
57  *
58  * @param n     A phi node.
59  * @param info  After call contains the induction variable information
60  */
61 static induct_var_info *is_induction_variable (ir_node *n, induct_var_info *info) {
62
63   ir_node  *phi_pred_0, *phi_pred_1, *add_r, *add_l, *sub_r, *sub_l ;
64   ir_op    *phi_pred_0_op, *phi_pred_1_op;
65
66   info->operation_code = NULL;
67   info->increment      = NULL;
68   info->init           = NULL;
69   info->op             = NULL;
70   info->be_pos         = -1;
71   info->init_pred_pos  = -1;
72   info->op_pred_pos    = -1;
73
74   assert(get_irn_op(n) == op_Phi);
75
76   /* The necessary conditions for the phi node. */
77   if (get_irn_arity(n) != 2             ||
78       !has_backedges(get_nodes_block(n))  )
79     return NULL;
80
81   /* The predecessors  of the phi node. */
82   phi_pred_0 = get_Phi_pred(n, 0);
83   phi_pred_1 = get_Phi_pred(n, 1);
84
85   /*The operation of the predecessors. */
86   phi_pred_0_op = get_irn_op(phi_pred_0);
87   phi_pred_1_op = get_irn_op(phi_pred_1);
88
89   /*Compute if the induction variable is added or substracted wiht a constant . */
90   if (phi_pred_0_op == op_Add){
91     info->operation_code = op_Add;
92     add_l = get_Add_left(phi_pred_0);
93     add_r = get_Add_right(phi_pred_0);
94     info->op_pred_pos = 0;
95     if (add_l == n){
96       info->increment = add_r;
97     } else if (add_r == n){
98       info->increment = add_l;
99     } else return NULL;
100   } else if (phi_pred_1_op == op_Add){
101     info->operation_code = op_Add ;
102     add_l = get_Add_left(phi_pred_1);
103     add_r = get_Add_right(phi_pred_1);
104     info->op_pred_pos = 1;
105     if (add_l == n){
106       info->increment = add_r;
107     } else if (add_r == n){
108       info->increment = add_l;
109     } else return NULL;
110   } else if (phi_pred_0_op == op_Sub){
111     info->operation_code = op_Sub;
112     sub_r = get_Sub_right(phi_pred_0);
113     sub_l = get_Sub_left(phi_pred_0);
114     info->op_pred_pos = 0;
115     if (sub_l == n){
116       info->increment = sub_r;
117     } else if (sub_r == n){
118       info->increment = sub_l;
119     } else return NULL;
120   } else if (phi_pred_1_op == op_Sub){
121     info->operation_code = op_Sub;
122     sub_r = get_Sub_right(phi_pred_1);
123     sub_l = get_Sub_left(phi_pred_1);
124     info->op_pred_pos = 1;
125     if (sub_l == n){
126       info->increment = sub_r;
127     } else return NULL;
128   } else
129     return NULL;
130
131   /*Compute the position of the backedge. */
132   if (is_backedge(get_nodes_block(n), 0)){
133     info->be_pos = 0;
134     info->init_pred_pos = 1;
135     info->op = get_Phi_pred(n, 0);
136     info->init = get_Phi_pred(n, 1);
137   } else if (is_backedge(get_nodes_block(n), 1)){
138     info->be_pos = 1;
139     info->init_pred_pos = 0;
140     info->op = get_Phi_pred(n, 1);
141     info->init = get_Phi_pred(n, 0);
142   }
143
144   if (info->be_pos == 0) {
145     if (get_Block_dom_depth(get_nodes_block(phi_pred_1))  >=
146     get_Block_dom_depth(get_nodes_block(n))) {
147       return NULL;
148     }
149   } else if (get_Block_dom_depth(get_nodes_block(phi_pred_0))  >=
150          get_Block_dom_depth(get_nodes_block(n))) return NULL;
151
152   if (get_Block_dom_depth(get_nodes_block(info->increment))  >=
153       get_Block_dom_depth(get_nodes_block(n))) return NULL;
154
155   return info;
156 }
157
158 /* from irdump.c */
159 const char *get_irg_dump_name(ir_graph *irg);
160
161 /**
162  * Reduce a node.
163  *
164  * @param *srong   The node to be reduce.
165  * @param *env     Free environment pointer.
166  *
167  * The node for reduce mus be in a loop whit *phi and *add.The *phi node muss
168  * have 2 predecessors a Const and a Add node. The predecessors of Add node muss
169  * be *phi and a Const node. The nodes a, b, c  muss be Const with dom_depth <
170  * phi.
171  */
172
173 static void reduce_itervar(ir_node *itervar_phi, void *env) {
174   ir_node *strong = NULL, *cmp = NULL, *c, *cmp_const;
175   int phi_pred, strong_in_Phi = 0, cmp_in_phi = 0, out_loop_res = 1;
176 int i;
177
178   // This "if" finds the node to reduce.
179   induct_var_info ivi;
180
181   // This "if" finds the Phi predecessors for the node that must be reduced.
182   if ((get_irn_op(itervar_phi) == op_Phi)    &&
183       is_induction_variable(itervar_phi, &ivi) != NULL ) {
184     phi_pred = get_irn_n_outs(itervar_phi);
185     ir_loop *l_itervar_phi = get_irn_loop(get_nodes_block(itervar_phi));
186
187     for (i = 0; i < phi_pred; i++) {
188       ir_node *out = get_irn_out(itervar_phi, i);
189       ir_op   *out_op = get_irn_op(out);
190       if (get_irn_loop(get_nodes_block(out)) != l_itervar_phi)
191     out_loop_res = 0;
192       if (out_op == op_Mul){
193     strong = out;
194         strong_in_Phi++;
195       }else if (out_op == op_Cmp){
196     cmp = out;
197     cmp_in_phi++;
198       }
199     }
200     if (strong == NULL || (strong_in_Phi > 1)) return;
201
202     if(get_irn_op(get_Mul_right(strong)) == op_Phi)
203       c = get_Mul_left(strong);
204     else
205       c = get_Mul_right(strong);
206
207
208     if (get_Block_dom_depth(get_nodes_block(c))  >=
209     get_Block_dom_depth(get_nodes_block(itervar_phi))) return;
210
211     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
212       printf("The constant of Reducing node is: "); DDMN(c);
213       printf("The Phi node is"); DDMN(itervar_phi);
214       printf("Reducing node: "); DDMN(strong);
215       printf("  iter var is  "); DDMN(ivi.op);
216       printf("  in graph     "); DDMG(current_ir_graph);
217     }
218
219     ir_node *inc , *init , *new_phi, *in[2], *new_op = NULL, *block_init, *block_inc;
220
221     ir_node *init_block      = get_nodes_block(ivi.init);
222     ir_node *increment_block = get_nodes_block(ivi.increment);
223     ir_node *c_block         = get_nodes_block(c) ;
224
225     if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
226       block_inc = increment_block;
227     else
228       block_inc = c_block;
229
230     if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(c_block))
231       block_init = init_block;
232     else
233       block_init = c_block;
234
235     /* we will do a strength reduction */
236     stat_strength_red(current_ir_graph, strong, cmp);
237
238     /* Compute new loop invariant increment and initialization values. */
239     inc  = new_r_Mul (current_ir_graph, block_inc,  ivi.increment, c, get_irn_mode(c));
240     init = new_r_Mul (current_ir_graph, block_init, ivi.init,      c, get_irn_mode(ivi.init));
241
242     /* Generate a new basic induction variable. Break the data flow loop
243        initially by using an Unknown node. */
244     in[ivi.op_pred_pos]   = new_Unknown(get_irn_mode(init));
245     in[ivi.init_pred_pos] = init;
246     new_phi = new_r_Phi(current_ir_graph, get_nodes_block(itervar_phi), 2, in, get_irn_mode(init));
247     mark_irn_visited(new_phi);
248
249     if (ivi.operation_code == op_Add)
250       new_op = new_r_Add(current_ir_graph, get_nodes_block(ivi.op), inc, new_phi,
251              get_irn_mode(inc));
252     else if (ivi.operation_code == op_Sub)
253       new_op = new_r_Sub(current_ir_graph, get_nodes_block(ivi.op), new_phi, inc,
254              get_irn_mode(inc));
255     set_Phi_pred(new_phi, ivi.op_pred_pos, new_op);
256
257     /* Replace the use of the strength reduced value. */
258     exchange(strong, new_phi);
259
260     if (cmp == NULL || cmp_in_phi > 1 || out_loop_res == 0) return;
261
262     if (get_irn_op(get_Cmp_left(cmp)) == op_Const)
263       cmp_const = get_Cmp_left(cmp);
264     else
265       cmp_const = get_Cmp_right(cmp);
266
267     if (get_irn_loop(get_nodes_block(cmp)) != l_itervar_phi) return;
268
269     if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
270       printf("It is possibale to exchange the Cmp with a new Cmp   \n");
271       printf("The constant of Cmp node is: "); DDMN(cmp_const);
272       printf("The Phi node is"); DDMN(itervar_phi);
273       printf("Cmp node: "); DDMN(cmp);
274       printf("  in graph     "); DDMG(current_ir_graph);
275     }
276
277     ir_node *new_cmp_const, *new_cmp, *cmp_const_block = get_nodes_block(cmp_const);
278
279     if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(cmp_const_block))
280       block_init = init_block;
281     else
282       block_init = cmp_const_block;
283
284     new_cmp_const = new_r_Mul (current_ir_graph, block_init, cmp_const,
285                    c, get_irn_mode(ivi.init));
286     new_cmp = new_r_Cmp (current_ir_graph, get_nodes_block(cmp),
287              new_phi, new_cmp_const);
288     exchange(cmp, new_cmp);
289   } else return;
290 }
291
292
293 /* Performs strength reduction for the passed graph. */
294 void reduce_strength(ir_graph *irg) {
295   ir_graph *rem = current_ir_graph;
296   current_ir_graph = irg;
297
298   if (!get_optimize() || !get_opt_strength_red()) return;
299
300   n_reduced_expressions = 0;
301
302   /* -- Precompute some information -- */
303   /* Call algorithm that computes the backedges */
304   construct_cf_backedges(irg);
305   /* Call algorithm that computes the dominator trees. */
306   compute_doms(irg);
307   /* Call algorithm that computes the out edges */
308   compute_outs(irg);
309   /* -- Search expressions that can be optimized -- */
310   irg_walk_graph(irg, NULL, reduce_itervar, NULL);
311
312   if (get_opt_strength_red_verbose())
313     printf("Reduced %d iteration variables in graph %s.%s\n.", n_reduced_expressions,
314        get_type_name(get_entity_owner(get_irg_entity(irg))),
315        get_entity_name(get_irg_entity(irg)));
316
317   current_ir_graph = rem;
318 }