6 * File name: ir/opt/strength_red.c
7 * Purpose: Make strength reduction .
8 * Author: Beyhan Veliev
12 * Copyright: (c) 2003 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 # include "strength_red.h"
20 # include "irnode_t.h"
22 # include "irloop_t.h"
26 # include "firmstat.h"
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;
38 /** Counter for verbose information about optimization. */
39 static int n_reduced_expressions;
41 /** Detect basic iteration variables.
43 * The variable ir represented by a subgraph as this:
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.
58 * @param n A phi node.
59 * @param info After call contains the induction variable information
61 static induct_var_info *is_induction_variable (ir_node *n, induct_var_info *info) {
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;
66 info->operation_code = NULL;
67 info->increment = NULL;
71 info->init_pred_pos = -1;
72 info->op_pred_pos = -1;
74 assert(get_irn_op(n) == op_Phi);
76 /* The necessary conditions for the phi node. */
77 if (get_irn_arity(n) != 2 ||
78 !has_backedges(get_nodes_block(n)) )
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);
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);
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 info->init_pred_pos = 1;
97 info->increment = add_r;
98 } else if (add_r == n){
99 info->increment = add_l;
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 info->init_pred_pos = 0;
108 info->increment = add_r;
109 } else if (add_r == n){
110 info->increment = add_l;
112 } else if (phi_pred_0_op == op_Sub){
113 info->operation_code = op_Sub;
114 sub_r = get_Sub_right(phi_pred_0);
115 sub_l = get_Sub_left(phi_pred_0);
116 info->op_pred_pos = 0;
117 info->init_pred_pos = 1;
119 info->increment = sub_r;
120 } else if (sub_r == n){
121 info->increment = sub_l;
123 } else if (phi_pred_1_op == op_Sub){
124 info->operation_code = op_Sub;
125 sub_r = get_Sub_right(phi_pred_1);
126 sub_l = get_Sub_left(phi_pred_1);
127 info->op_pred_pos = 1;
128 info->init_pred_pos = 0;
130 info->increment = sub_r;
135 /*Compute the position of the backedge. */
136 if (is_backedge(get_nodes_block(n), info->op_pred_pos)){
137 info->be_pos = info->op_pred_pos;
138 info->op = get_Phi_pred(n, info->op_pred_pos);
139 info->init = get_Phi_pred(n, info->init_pred_pos);
142 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
143 get_Block_dom_depth(get_nodes_block(n))) {
151 const char *get_irg_dump_name(ir_graph *irg);
156 * @param *srong The node to be reduce.
157 * @param *env Free environment pointer.
159 * The node for reduce mus be in a loop whit *phi and *add.The *phi node muss
160 * have 2 predecessors a Const and a Add node. The predecessors of Add node muss
161 * be *phi and a Const node. The nodes a, b, c muss be Const with dom_depth <
165 void reduce_itervar(ir_node *itervar_phi, void *env) {
167 ir_node *strong = NULL, *cmp = NULL, *c, *cmp_const, *old_add;
168 int phi_pred, strong_in_Phi = 0, cmp_in_phi = 0, out_loop_res = 1, Store_in_phi = 0;
169 int op_pred, Store_in_op = 0, strong_in_op = 0;
173 // This "if" finds the Phi predecessors for the node that must be reduced.
174 if ((get_irn_op(itervar_phi) == op_Phi) &&
175 is_induction_variable(itervar_phi, &ivi) != NULL ) {
176 phi_pred = get_irn_n_outs(itervar_phi);
177 ir_loop *l_itervar_phi = get_irn_loop(get_nodes_block(itervar_phi));
178 for (i = 0; i < phi_pred; i++) {
179 ir_node *out = get_irn_out(itervar_phi, i);
180 ir_op *out_op = get_irn_op(out);
181 if (get_irn_loop(get_nodes_block(out)) != l_itervar_phi)
183 if ( out_op == op_Store)
185 if (out_op == op_Mul) {
188 }else if (out_op == op_Cmp){
195 op_pred = get_irn_n_outs(ivi.op);
196 for(i = 0; i < op_pred; i++){
197 ir_node *out = get_irn_out(ivi.op, i);
198 ir_op *out_op = get_irn_op(out);
199 if(out_op == op_Store)
201 if(out_op == op_Mul){
207 if (strong == NULL || (strong_in_Phi > 1) || (strong_in_op > 1)) return;
209 if (get_irn_loop(get_nodes_block(strong)) != l_itervar_phi) return;
211 ir_op *mul_right_op = get_irn_op(get_Mul_right(strong));
212 if(mul_right_op == op_Phi || mul_right_op == op_Add || mul_right_op == op_Sub)
213 c = get_Mul_left(strong);
215 c = get_Mul_right(strong);
218 if (get_Block_dom_depth(get_nodes_block(c)) >=
219 get_Block_dom_depth(get_nodes_block(itervar_phi))) return;
221 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
222 printf("The constant of Reducing node is: "); DDMN(c);
223 printf("The Phi node is"); DDMN(itervar_phi);
224 printf("Reducing node: "); DDMN(strong);
225 printf(" iter var is "); DDMN(ivi.op);
226 printf(" in graph "); DDMG(current_ir_graph);
229 ir_node *inc , *init , *new_phi, *in[2], *new_op = NULL, *block_init;
230 ir_node *block_inc, *new_add, *symconst;
232 ir_node *init_block = get_nodes_block(ivi.init);
233 ir_node *increment_block = get_nodes_block(ivi.increment);
234 ir_node *c_block = get_nodes_block(c) ;
236 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
237 block_inc = increment_block;
241 if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(c_block))
242 block_init = init_block;
244 block_init = c_block;
246 /* we will do a strength reduction */
247 stat_strength_red(current_ir_graph, strong, cmp);
249 /* Compute new loop invariant increment and initialization values. */
250 inc = new_r_Mul (current_ir_graph, block_inc, ivi.increment, c, get_irn_mode(c));
251 init = new_r_Mul (current_ir_graph, block_init, ivi.init, c, get_irn_mode(ivi.init));
253 /* Generate a new basic induction variable. Break the data flow loop
254 initially by using an Unknown node. */
255 in[ivi.op_pred_pos] = new_Unknown(get_irn_mode(init));
256 in[ivi.init_pred_pos] = init;
257 new_phi = new_r_Phi(current_ir_graph, get_nodes_block(itervar_phi), 2, in, get_irn_mode(init));
258 mark_irn_visited(new_phi);
260 if (ivi.operation_code == op_Add)
261 new_op = new_r_Add(current_ir_graph, get_nodes_block(ivi.op), inc, new_phi,
263 else if (ivi.operation_code == op_Sub)
264 new_op = new_r_Sub(current_ir_graph, get_nodes_block(ivi.op), new_phi, inc,
266 set_Phi_pred(new_phi, ivi.op_pred_pos, new_op);
269 if(get_irn_n_outs(strong) > 1) return;
270 else old_add = get_irn_out(strong, 0);
271 if(get_irn_op(old_add) != op_Add) return;
272 if(get_Add_left(old_add) == strong)
273 symconst = get_Add_right(old_add);
275 symconst = get_Add_left(old_add);
276 new_add = new_r_Add(current_ir_graph, get_nodes_block(old_add),
277 new_op, symconst, get_irn_mode(symconst));
278 exchange(old_add, new_add);
281 /* Replace the use of the strength reduced value. */
282 exchange(strong, new_phi);
284 if (cmp == NULL || cmp_in_phi > 1 || out_loop_res == 0 || Store_in_phi != 0 || Store_in_op != 0) return;
286 if (get_irn_op(get_Cmp_left(cmp)) == op_Const)
287 cmp_const = get_Cmp_left(cmp);
289 cmp_const = get_Cmp_right(cmp);
291 if (get_irn_loop(get_nodes_block(cmp)) != l_itervar_phi) return;
293 if (get_opt_strength_red_verbose() && get_firm_verbosity() > 1) {
294 printf("It is possibale to exchange the Cmp with a new Cmp \n");
295 printf("The constant of Cmp node is: "); DDMN(cmp_const);
296 printf("The Phi node is "); DDMN(itervar_phi);
297 printf("Cmp node: "); DDMN(cmp);
298 printf(" in graph "); DDMG(current_ir_graph);
301 ir_node *new_cmp_const, *new_cmp, *cmp_const_block = get_nodes_block(cmp_const);
303 if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(cmp_const_block))
304 block_init = init_block;
306 block_init = cmp_const_block;
308 new_cmp_const = new_r_Mul (current_ir_graph, block_init, cmp_const,
309 c, get_irn_mode(ivi.init));
310 new_cmp = new_r_Cmp (current_ir_graph, get_nodes_block(cmp),
311 new_phi, new_cmp_const);
312 exchange(cmp, new_cmp);
317 /* Performs strength reduction for the passed graph. */
318 void reduce_strength(ir_graph *irg) {
319 ir_graph *rem = current_ir_graph;
320 current_ir_graph = irg;
322 if (!get_optimize() || !get_opt_strength_red()) return;
324 n_reduced_expressions = 0;
326 /* -- Precompute some information -- */
327 /* Call algorithm that computes the backedges */
328 construct_cf_backedges(irg);
329 /* Call algorithm that computes the dominator trees. */
331 /* Call algorithm that computes the out edges */
333 /* -- Search expressions that can be optimized -- */
334 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
336 if (get_opt_strength_red_verbose())
337 printf("Reduced %d iteration variables in graph %s.%s\n.", n_reduced_expressions,
338 get_type_name(get_entity_owner(get_irg_entity(irg))),
339 get_entity_name(get_irg_entity(irg)));
341 current_ir_graph = rem;