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