6 * File name: ir/opt/strength_red.c
7 * Purpose: Make strength reduction .
8 * Author: Beyhan Veliev
12 * Copyright: (c) 2004 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 reduce_itervar(induct_var_info *iv)
24 for each (out o of iv) {
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)
36 # include "strength_red.h"
39 # include "irnode_t.h"
41 # include "irloop_t.h"
44 # include "irdump_t.h"
45 # include "firmstat.h"
48 /** Counter for verbose information about optimization. */
49 static int n_reduced_expressions;
50 static int n_made_new_phis;
52 /** Detect basic iteration variables.
54 * The variable ir represented by a subgraph as this:
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.
69 * @param n A phi node.
70 * @param info After call contains the induction variable information.
73 induct_var_info *is_induction_variable (induct_var_info *info) {
76 int op_pred, Store_in_op, Store_in_phi, cmp_in_phi;
80 info->cmp_const = NULL;
81 info->cmp_init_block = NULL;
82 info->increment = NULL;
84 info->l_itervar_phi = NULL;
87 info->new_increment = NULL;
88 info->new_init = NULL;
91 info->operation_code = NULL;
94 info->reducible_node = NULL;
95 info->out_loop_res = 1;
98 info->strong_reduced = 0;
99 info->init_pred_pos = -1;
100 info->op_pred_pos = -1;
102 assert(get_irn_op(info->itervar_phi) == op_Phi);
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)) )
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);
113 /* Compute if the induction variable is added or substracted wiht 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);
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;
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;
134 /* check if we found something */
135 if (! info->operation_code)
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);
144 /* irregular control flow detected. */
148 if (get_Block_dom_depth(get_nodes_block(info->init)) >=
149 get_Block_dom_depth(get_nodes_block(info->itervar_phi))) {
153 /* This "for" marks if the iteration operation have a Store successor .*/
154 op_pred = get_irn_n_outs(info->op);
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)
165 /* Information about loop of itervar_phi. */
166 info->l_itervar_phi = get_irn_loop(get_nodes_block(info->itervar_phi));
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);
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;
181 if (out_op == op_Store)
183 else if (out_op == op_Cmp){
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))
194 // Search for loop invariant of Cmp.
195 if (info->cmp != NULL) {
196 ir_node *cmp_const_block;
198 if (get_Cmp_left(info->cmp) == info->itervar_phi)
199 info->cmp_const = get_Cmp_right(info->cmp);
201 info->cmp_const = get_Cmp_left(info->cmp);
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);
208 info->cmp_init_block = cmp_const_block;
214 * Creates a new Add node from operands.
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);
221 if (mode_is_reference(m2))
223 return new_r_Add(irg, b, op1, op2, m);
227 * Creates a new Sub node from operands.
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);
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))
238 return new_r_Sub(irg, b, op1, op2, m);
241 /* Reduce a Add, Sub or Mul node
243 * @param *reduce_var The node to reduce.
244 * @param *ivi Contains the induction variable information.
246 static int reduce(ir_node *reduce_var, induct_var_info *ivi)
248 // Essential conditions for a reducable node.
249 if (get_irn_loop(get_nodes_block(reduce_var)) != ivi->l_itervar_phi)
252 if (get_irn_op(reduce_var) == op_Mul) {
253 ir_node *mul_init = NULL;
254 ir_node *mul_const = NULL;
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);
262 ir_node *in[2], *block_init;
266 ir_node *increment_block;
269 n_reduced_expressions++;
271 if (mul_right_op == op_Const) {
272 mul_const = mul_right;
275 else if (mul_left_op == op_Const) {
276 mul_const = mul_left;
277 mul_init = mul_right;
280 if (mul_const == NULL || mul_init == NULL)
283 init_block = get_nodes_block(mul_init);
284 increment_block = get_nodes_block(ivi->increment);
285 c_block = get_nodes_block(mul_const);
287 if (get_Block_dom_depth(increment_block) >= get_Block_dom_depth(c_block))
288 block_inc = increment_block;
292 if (get_Block_dom_depth(init_block) >= get_Block_dom_depth(c_block))
293 block_init = init_block;
295 block_init = c_block;
297 if (! ivi->reducible){
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)))
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);
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,
320 ivi->new_init = new_r_Mul (current_ir_graph, block_init, ivi->init, mul_const,
321 get_irn_mode(mul_const));
323 /* Generate a new basic induction variable. Break the data flow loop
324 initially by using an Unknown node. */
326 in[ivi->op_pred_pos] = new_Unknown(get_irn_mode(ivi->new_init));
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);
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,
340 set_Phi_pred(ivi->new_phi, ivi->op_pred_pos, ivi->new_op);
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){
353 ivi->reducible_node = old_ind;
356 /* Replace the use of the strength reduced value. */
357 exchange(reduce_var, ivi->new_phi);
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));
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));
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));
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);
385 }else if (get_irn_op (reduce_var) == op_Add){
386 ir_node *add_init = NULL;
387 ir_node *add_const = NULL;
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);
395 n_reduced_expressions++;
397 if (add_right_op != op_Const)
398 add_init = add_right;
399 else if (add_left_op != op_Const)
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);
410 ivi->cmp_const = my_new_r_Add (current_ir_graph, ivi->cmp_init_block,
411 add_const, ivi->cmp_const);
413 ivi->new_init = my_new_r_Add (current_ir_graph, get_nodes_block(ivi->init),
414 add_const, ivi->new_init);
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);
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);
430 n_reduced_expressions++;
432 if (sub_right_op != op_Const)
433 sub_init = sub_right;
434 else if (sub_left_op != op_Const)
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;
441 if (sub_const == NULL) return 0;
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);
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);
461 static ir_node *reducible(ir_node *out, induct_var_info *ivi)
463 ir_node *reduced = NULL;
466 for (pred = 1; pred == 1; pred = get_irn_n_outs(out)) {
467 if (reduce(out, ivi))
471 out = get_irn_out(out, 0);
479 * @param *itervar_phi The iteration variable of a loop.
480 * @param *env Free environment pointer.
482 static void reduce_itervar(ir_node *itervar_phi, void *env)
486 if (get_irn_op(itervar_phi) != op_Phi)
489 ivi.itervar_phi = itervar_phi;
491 /* This "if" finds the iteration variable. */
492 if (is_induction_variable(&ivi)) {
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);
499 if(ivi.phi_pred == 3 && out != ivi.op && out !=ivi.cmp){
500 ir_node *reduced = reducible(out, &ivi);
502 exchange( reduced, ivi.itervar_phi);
504 } else if (out_op == op_Mul)
505 if(reduce(out, &ivi) && ivi.reducible){
506 ir_node *reduced = reducible(ivi.reducible_node, &ivi);
508 exchange(reduced, ivi.new_phi);
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));
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);
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);
528 exchange(reduced, ivi.new_phi);
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));
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);
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);
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);
554 set_Cmp_left(ivi.cmp, ivi.cmp_const);
560 /* Performs strength reduction for the passed graph. */
561 void reduce_strength(ir_graph *irg) {
562 ir_graph *rem = current_ir_graph;
564 if (!get_optimize() || !get_opt_strength_red()) return;
566 current_ir_graph = irg;
568 n_reduced_expressions = 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. */
575 /* Call algorithm that computes the out edges */
578 /* -- Search expressions that can be optimized -- */
579 irg_walk_graph(irg, NULL, reduce_itervar, NULL);
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)));
589 current_ir_graph = rem;