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