becopyilp: Inline struct size_red_t into struct ilp_env_t.
[libfirm] / ir / opt / reassoc.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Reassociation
9  * @author  Michael Beck
10  */
11 #include "config.h"
12
13 #include "iroptimize.h"
14 #include "iropt_t.h"
15 #include "irnode_t.h"
16 #include "irgraph_t.h"
17 #include "irmode_t.h"
18 #include "ircons_t.h"
19 #include "irgmod.h"
20 #include "iropt_dbg.h"
21 #include "irflag_t.h"
22 #include "irgwalk.h"
23 #include "irouts.h"
24 #include "reassoc_t.h"
25 #include "opt_init.h"
26 #include "irhooks.h"
27 #include "irloop.h"
28 #include "pdeq.h"
29 #include "debug.h"
30 #include "irpass.h"
31
32 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
33
34 typedef enum {
35         NO_CONSTANT   = 0,    /**< node is not constant */
36         REAL_CONSTANT = 1,    /**< node is a Const that is suitable for constant folding */
37         REGION_CONST  = 4     /**< node is a constant expression in the current context,
38                                    use 4 here to simplify implementation of get_comm_Binop_ops() */
39 } const_class_t;
40
41 /**
42  * returns whether a node is constant ie is a constant or
43  * is loop invariant (called region constant)
44  *
45  * @param n     the node to be checked for constant
46  * @param block a block that might be in a loop
47  */
48 static const_class_t get_const_class(const ir_node *n, const ir_node *block)
49 {
50         if (is_Const(n))
51                 return REAL_CONSTANT;
52
53         /* constant nodes which can't be folded are region constants */
54         if (is_irn_constlike(n))
55                 return REGION_CONST;
56
57         /*
58          * Beware: Bad nodes are always loop-invariant, but
59          * cannot handled in later code, so filter them here.
60          */
61         if (! is_Bad(n) && is_loop_invariant(n, block))
62                 return REGION_CONST;
63
64         return NO_CONSTANT;
65 }
66
67 /**
68  * returns the operands of a commutative bin-op, if one operand is
69  * a region constant, it is returned as the second one.
70  *
71  * Beware: Real constants must be returned with higher priority than
72  * region constants, because they might be folded.
73  */
74 static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
75 {
76         ir_node *op_a = get_binop_left(binop);
77         ir_node *op_b = get_binop_right(binop);
78         ir_node *block = get_nodes_block(binop);
79         int class_a = get_const_class(op_a, block);
80         int class_b = get_const_class(op_b, block);
81
82         assert(is_op_commutative(get_irn_op(binop)));
83
84         switch (class_a + 2*class_b) {
85         case REAL_CONSTANT + 2*REAL_CONSTANT:
86                 /* if both are constants, one might be a
87                  * pointer constant like NULL, return the other
88                  */
89                 if (mode_is_reference(get_irn_mode(op_a))) {
90                         *a = op_a;
91                         *c = op_b;
92                 } else {
93                         *a = op_b;
94                         *c = op_a;
95                 }
96                 break;
97         case REAL_CONSTANT + 2*NO_CONSTANT:
98         case REAL_CONSTANT + 2*REGION_CONST:
99         case REGION_CONST  + 2*NO_CONSTANT:
100                 *a = op_b;
101                 *c = op_a;
102                 break;
103         default:
104                 *a = op_a;
105                 *c = op_b;
106                 break;
107         }
108 }
109
110 /**
111  * reassociate a Sub: x - c = x + (-c)
112  */
113 static int reassoc_Sub(ir_node **in)
114 {
115         ir_node *n = *in;
116         ir_node *right = get_Sub_right(n);
117         ir_mode *rmode = get_irn_mode(right);
118         ir_node *block;
119
120         /* cannot handle SubIs(P, P) */
121         if (mode_is_reference(rmode))
122                 return 0;
123
124         block = get_nodes_block(n);
125
126         /* handles rule R6:
127          * convert x - c => x + (-c)
128          */
129         if (get_const_class(right, block) == REAL_CONSTANT) {
130                 ir_node *left  = get_Sub_left(n);
131                 ir_mode *mode;
132                 dbg_info *dbi;
133                 ir_node *irn;
134
135                 switch (get_const_class(left, block)) {
136                 case REAL_CONSTANT:
137                         irn = optimize_in_place(n);
138                         if (irn != n) {
139                                 exchange(n, irn);
140                                 *in = irn;
141                                 return 1;
142                         }
143                         return 0;
144                 case NO_CONSTANT:
145                         break;
146                 default:
147                         /* already constant, nothing to do */
148                         return 0;
149                 }
150
151                 mode = get_irn_mode(n);
152                 dbi  = get_irn_dbg_info(n);
153
154                 /* Beware of SubP(P, Is) */
155                 irn = new_rd_Minus(dbi, block, right, rmode);
156                 irn = new_rd_Add(dbi, block, left, irn, mode);
157
158                 DBG((dbg, LEVEL_5, "Applied: %n - %n => %n + (-%n)\n",
159                         get_Sub_left(n), right, get_Sub_left(n), right));
160
161                 if (n == irn)
162                         return 0;
163
164                 exchange(n, irn);
165                 *in = irn;
166
167                 return 1;
168         }
169         return 0;
170 }
171
172 /** Retrieve a mode from the operands. We need this, because
173  * Add and Sub are allowed to operate on (P, Is)
174  */
175 static ir_mode *get_mode_from_ops(ir_node *op1, ir_node *op2)
176 {
177         ir_mode *m1, *m2;
178
179         m1 = get_irn_mode(op1);
180         if (mode_is_reference(m1))
181                 return m1;
182
183         m2 = get_irn_mode(op2);
184         if (mode_is_reference(m2))
185                 return m2;
186
187         assert(m1 == m2);
188
189         return m1;
190 }
191
192 /**
193  * reassociate a commutative Binop
194  *
195  * BEWARE: this rule leads to a potential loop, if
196  * two operands are region constants and the third is a
197  * constant, so avoid this situation.
198  */
199 static int reassoc_commutative(ir_node **node)
200 {
201         ir_node *n     = *node;
202         ir_op   *op    = get_irn_op(n);
203         ir_node *block = get_nodes_block(n);
204         ir_node *t1, *c1;
205
206         get_comm_Binop_ops(n, &t1, &c1);
207
208         if (get_irn_op(t1) == op) {
209                 ir_node *t2, *c2;
210                 const_class_t c_c1, c_c2, c_t2;
211
212                 get_comm_Binop_ops(t1, &t2, &c2);
213
214                 /* do not optimize Bad nodes, will fail later */
215                 if (is_Bad(t2))
216                         return 0;
217
218                 c_c1 = get_const_class(c1, block);
219                 c_c2 = get_const_class(c2, block);
220                 c_t2 = get_const_class(t2, block);
221
222                 if ( ((c_c1 > NO_CONSTANT) & (c_t2 > NO_CONSTANT)) &&
223                      ((((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0) || ((c_c1 & c_c2 & c_t2) == REGION_CONST)) ) {
224                         /* All three are constant and either all are constant expressions
225                          * or two of them are:
226                          * then applying this rule would lead into a cycle
227                          *
228                          * Note that if t2 is a constant so is c2 hence we save one test.
229                          */
230                         return 0;
231                 }
232
233                 if ((c_c1 != NO_CONSTANT) /* & (c_c2 != NO_CONSTANT) */) {
234                         /* handles rules R7, R8, R9, R10:
235                          * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
236                          */
237                         ir_node *irn, *in[2];
238                         ir_mode *mode, *mode_c1 = get_irn_mode(c1), *mode_c2 = get_irn_mode(c2);
239                         ir_graph *irg = get_irn_irg(c1);
240
241                         /* It might happen, that c1 and c2 have different modes, for
242                          * instance Is and Iu.
243                          * Handle this here.
244                          */
245                         if (mode_c1 != mode_c2) {
246                                 if (mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
247                                         /* get the bigger one */
248                                         if (get_mode_size_bits(mode_c1) > get_mode_size_bits(mode_c2))
249                                                 c2 = new_r_Conv(block, c2, mode_c1);
250                                         else if (get_mode_size_bits(mode_c1) < get_mode_size_bits(mode_c2))
251                                                 c1 = new_r_Conv(block, c1, mode_c2);
252                                         else {
253                                                 /* Try to cast the real const */
254                                                 if (c_c1 == REAL_CONSTANT)
255                                                         c1 = new_r_Conv(block, c1, mode_c2);
256                                                 else
257                                                         c2 = new_r_Conv(block, c2, mode_c1);
258                                         }
259                                 }
260                         }
261
262                         in[0] = c1;
263                         in[1] = c2;
264
265                         mode  = get_mode_from_ops(in[0], in[1]);
266                         in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
267                         in[0] = t2;
268
269                         mode = get_mode_from_ops(in[0], in[1]);
270                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
271
272                         DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
273                              c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
274                              t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
275                         /*
276                          * In some rare cases it can really happen that we get the same
277                          * node back. This might be happen in dead loops, were the Phi
278                          * nodes are already gone away. So check this.
279                          */
280                         if (n != irn) {
281                                 exchange(n, irn);
282                                 *node = irn;
283                                 return 1;
284                         }
285                 }
286         }
287         if (get_irn_op(c1) == op) {
288                 ir_node *t = c1;
289                 c1 = t1;
290                 t1 = t;
291         }
292         if (get_irn_op(t1) == op) {
293                 ir_node *l = get_binop_left(t1);
294                 ir_node *r = get_binop_right(t1);
295                 const_class_t c_r;
296
297                 if (r == c1) {
298                         ir_node *t = r;
299                         r = l;
300                         l = t;
301                 }
302                 c_r = get_const_class(r, block);
303                 if (c_r != NO_CONSTANT) {
304                         /*
305                          * Beware: don't do the following op if a constant was
306                          * placed below, else we will fall into a loop.
307                          */
308                         return 0;
309                 }
310
311                 if (l == c1) {
312                         /* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
313                         ir_mode *mode_res = get_irn_mode(n);
314                         ir_mode *mode_c1  = get_irn_mode(c1);
315                         ir_graph *irg     = get_irn_irg(c1);
316                         ir_node *irn, *in[2];
317
318                         in[0] = c1;
319                         in[1] = c1;
320
321                         in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode_c1, 2, in));
322                         in[0] = r;
323
324                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode_res, 2, in));
325
326                         DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
327                                 c1, get_irn_opname(n), l, get_irn_opname(n), r,
328                                 r, get_irn_opname(n), c1, get_irn_opname(n), c1));
329
330                         if (n != irn) {
331                                 exchange(n, irn);
332                                 *node = irn;
333                                 return 1;
334                         }
335                 }
336         }
337         return 0;
338 }
339
340 /**
341  * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
342  */
343 static int reassoc_Mul(ir_node **node)
344 {
345         ir_node *n = *node;
346         ir_node *add_sub, *c;
347         ir_op *op;
348
349         if (reassoc_commutative(&n))
350                 return 1;
351
352         get_comm_Binop_ops(n, &add_sub, &c);
353         op = get_irn_op(add_sub);
354
355         /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
356         if (op == op_Add || op == op_Sub) {
357                 ir_mode *mode = get_irn_mode(n);
358                 ir_node *irn, *block, *t1, *t2, *in[2];
359
360                 block = get_nodes_block(n);
361                 t1 = get_binop_left(add_sub);
362                 t2 = get_binop_right(add_sub);
363
364                 /* we can only multiplication rules on integer arithmetic */
365                 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
366                         ir_graph *irg = get_irn_irg(t1);
367                         in[0] = new_rd_Mul(NULL, block, c, t1, mode);
368                         in[1] = new_rd_Mul(NULL, block, c, t2, mode);
369
370                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
371
372                         /* In some cases it might happen that the new irn is equal the old one, for
373                          * instance in:
374                          * (x - 1) * y == x * y - y
375                          * will be transformed back by simpler optimization
376                          * We could switch simple optimizations off, but this only happens iff y
377                          * is a loop-invariant expression and that it is not clear if the new form
378                          * is better.
379                          * So, we let the old one.
380                          */
381                         if (irn != n) {
382                                 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
383                                         t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
384                                 exchange(n, irn);
385                                 *node = irn;
386
387                                 return 1;
388                         }
389                 }
390         }
391         return 0;
392 }
393
394 /**
395  * Reassociate Shl. We transform Shl(x, const) into Mul's if possible.
396  */
397 static int reassoc_Shl(ir_node **node)
398 {
399         ir_node   *n = *node;
400         ir_node   *c = get_Shl_right(n);
401         ir_node   *x, *blk, *irn;
402         ir_graph  *irg;
403         ir_mode   *mode;
404         ir_tarval *tv;
405
406         if (! is_Const(c))
407                 return 0;
408
409         x = get_Shl_left(n);
410         mode = get_irn_mode(x);
411
412         tv = get_mode_one(mode);
413         tv = tarval_shl(tv, get_Const_tarval(c));
414
415         if (tv == tarval_bad)
416                 return 0;
417
418         blk = get_nodes_block(n);
419         irg = get_irn_irg(blk);
420         c   = new_r_Const(irg, tv);
421         irn = new_rd_Mul(get_irn_dbg_info(n), blk, x, c, mode);
422
423         if (irn != n) {
424                 exchange(n, irn);
425                 *node = irn;
426                 return 1;
427         }
428         return 0;
429 }
430
431 /**
432  * The walker for the reassociation.
433  */
434 static void wq_walker(ir_node *n, void *env)
435 {
436         waitq *const wq = (waitq*)env;
437
438         set_irn_link(n, NULL);
439         if (!is_Block(n)) {
440                 waitq_put(wq, n);
441                 set_irn_link(n, wq);
442         }
443 }
444
445 /**
446  * The walker for the reassociation.
447  */
448 static void do_reassociation(waitq *const wq)
449 {
450         int i, res, changed;
451         ir_node *n;
452
453         while (! waitq_empty(wq)) {
454                 n = (ir_node*)waitq_get(wq);
455                 set_irn_link(n, NULL);
456
457                 hook_reassociate(1);
458
459                 /* reassociation must run until a fixpoint is reached. */
460                 changed = 0;
461                 do {
462                         ir_op    *op   = get_irn_op(n);
463                         ir_mode  *mode = get_irn_mode(n);
464
465                         res = 0;
466
467                         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
468                         if (mode_is_float(mode) && get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic)
469                                 break;
470
471                         if (op->ops.reassociate) {
472                                 res = op->ops.reassociate(&n);
473
474                                 changed |= res;
475                         }
476                 } while (res == 1);
477                 hook_reassociate(0);
478
479                 if (changed) {
480                         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
481                                 ir_node *pred = get_irn_n(n, i);
482
483                                 if (get_irn_link(pred) != wq) {
484                                         waitq_put(wq, pred);
485                                         set_irn_link(pred, wq);
486                                 }
487                         }
488                 }
489         }
490 }
491
492 /**
493  * Returns the earliest were a,b are available.
494  * Note that we know that a, b both dominate
495  * the block of the previous operation, so one must dominate the other.
496  *
497  * If the earliest block is the start block, return curr_blk instead
498  */
499 static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
500 {
501         ir_node *blk_a = get_nodes_block(a);
502         ir_node *blk_b = get_nodes_block(b);
503         ir_node *res;
504
505         /* if blk_a != blk_b, one must dominate the other */
506         if (block_dominates(blk_a, blk_b))
507                 res = blk_b;
508         else
509                 res = blk_a;
510         if (res == get_irg_start_block(get_irn_irg(curr_blk)))
511                 return curr_blk;
512         return res;
513 }
514
515 /**
516  * Checks whether a node is a Constant expression.
517  * The following trees are constant expressions:
518  *
519  * Const, SymConst, Const + SymConst
520  *
521  * Handling SymConsts as const might be not a good idea for all
522  * architectures ...
523  */
524 static int is_constant_expr(ir_node *irn)
525 {
526         switch (get_irn_opcode(irn)) {
527         case iro_Const:
528         case iro_SymConst:
529                 return 1;
530
531         case iro_Add: {
532                 ir_node *const l = get_Add_left(irn);
533                 if (!is_Const(l) && !is_SymConst(l))
534                         return 0;
535                 ir_node *const r = get_Add_right(irn);
536                 if (!is_Const(r) && !is_SymConst(r))
537                         return 0;
538                 return 1;
539         }
540
541         default:
542                 return 0;
543         }
544 }
545
546 /**
547  * Apply distributive Law for Mul and Add/Sub
548  */
549 static int reverse_rule_distributive(ir_node **node)
550 {
551         ir_node *n = *node;
552         ir_node *left  = get_binop_left(n);
553         ir_node *right = get_binop_right(n);
554         ir_node *x, *blk, *curr_blk;
555         ir_node *a, *b, *irn;
556         ir_op *op;
557         ir_mode *mode;
558         dbg_info *dbg;
559
560         op = get_irn_op(left);
561         if (op != get_irn_op(right))
562                 return 0;
563
564         if (op == op_Shl) {
565                 x = get_Shl_right(left);
566
567                 if (x == get_Shl_right(right)) {
568                         /* (a << x) +/- (b << x) ==> (a +/- b) << x */
569                         a = get_Shl_left(left);
570                         b = get_Shl_left(right);
571                         goto transform;
572                 }
573         } else if (op == op_Mul) {
574                 x = get_Mul_left(left);
575
576                 if (x == get_Mul_left(right)) {
577                         /* (x * a) +/- (x * b) ==> (a +/- b) * x */
578                         a = get_Mul_right(left);
579                         b = get_Mul_right(right);
580                         goto transform;
581                 } else if (x == get_Mul_right(right)) {
582                         /* (x * a) +/- (b * x) ==> (a +/- b) * x */
583                         a = get_Mul_right(left);
584                         b = get_Mul_left(right);
585                         goto transform;
586                 }
587
588                 x = get_Mul_right(left);
589
590                 if (x == get_Mul_right(right)) {
591                         /* (a * x) +/- (b * x) ==> (a +/- b) * x */
592                         a = get_Mul_left(left);
593                         b = get_Mul_left(right);
594                         goto transform;
595                 } else if (x == get_Mul_left(right)) {
596                         /* (a * x) +/- (x * b) ==> (a +/- b) * x */
597                         a = get_Mul_left(left);
598                         b = get_Mul_right(right);
599                         goto transform;
600                 }
601         }
602         return 0;
603
604 transform:
605         curr_blk = get_nodes_block(n);
606
607         blk = earliest_block(a, b, curr_blk);
608
609         dbg  = get_irn_dbg_info(n);
610         mode = get_irn_mode(n);
611
612         if (is_Add(n))
613                 irn = new_rd_Add(dbg, blk, a, b, mode);
614         else
615                 irn = new_rd_Sub(dbg, blk, a, b, mode);
616
617         blk  = earliest_block(irn, x, curr_blk);
618
619         if (op == op_Mul)
620                 irn = new_rd_Mul(dbg, blk, irn, x, mode);
621         else
622                 irn = new_rd_Shl(dbg, blk, irn, x, mode);
623
624         exchange(n, irn);
625         *node = irn;
626         return 1;
627 }
628
629 /**
630  * Move Constants towards the root.
631  */
632 static int move_consts_up(ir_node **node)
633 {
634         ir_node *n = *node;
635         ir_op *op;
636         ir_node *l, *r, *a, *b, *c, *blk, *irn, *in[2];
637         ir_mode *mode, *ma, *mb;
638         dbg_info *dbg;
639         ir_graph *irg;
640
641         l = get_binop_left(n);
642         r = get_binop_right(n);
643
644         /* check if one is already a constant expression */
645         if (is_constant_expr(l) || is_constant_expr(r))
646                 return 0;
647
648         dbg = get_irn_dbg_info(n);
649         op = get_irn_op(n);
650         if (get_irn_op(l) == op) {
651                 /* (a .op. b) .op. r */
652                 a = get_binop_left(l);
653                 b = get_binop_right(l);
654
655                 if (is_constant_expr(a)) {
656                         /* (C .op. b) .op. r ==> (r .op. b) .op. C */
657                         c = a;
658                         a = r;
659                         blk = get_nodes_block(l);
660                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
661                         goto transform;
662                 } else if (is_constant_expr(b)) {
663                         /* (a .op. C) .op. r ==> (a .op. r) .op. C */
664                         c = b;
665                         b = r;
666                         blk = get_nodes_block(l);
667                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
668                         goto transform;
669                 }
670         }
671         if (get_irn_op(r) == op) {
672                 /* l .op. (a .op. b) */
673                 a = get_binop_left(r);
674                 b = get_binop_right(r);
675
676                 if (is_constant_expr(a)) {
677                         /* l .op. (C .op. b) ==> (l .op. b) .op. C */
678                         c = a;
679                         a = l;
680                         blk = get_nodes_block(r);
681                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
682                         goto transform;
683                 } else if (is_constant_expr(b)) {
684                         /* l .op. (a .op. C) ==> (a .op. l) .op. C */
685                         c = b;
686                         b = l;
687                         blk = get_nodes_block(r);
688                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
689                         goto transform;
690                 }
691         }
692         return 0;
693
694 transform:
695         /* In some cases a and b might be both of different integer mode, and c a SymConst.
696          * in that case we could either
697          * 1.) cast into unsigned mode
698          * 2.) ignore
699          * we implement the second here
700          */
701         ma = get_irn_mode(a);
702         mb = get_irn_mode(b);
703         if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
704                 return 0;
705
706         /* check if (a .op. b) can be calculated in the same block is the old instruction */
707         if (! block_dominates(get_nodes_block(a), blk))
708                 return 0;
709         if (! block_dominates(get_nodes_block(b), blk))
710                 return 0;
711         /* ok */
712         in[0] = a;
713         in[1] = b;
714
715         mode = get_mode_from_ops(a, b);
716         irg  = get_irn_irg(blk);
717         in[0] = irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
718
719         /* beware: optimize_node might have changed the opcode, check again */
720         if (is_Add(irn) || is_Sub(irn)) {
721                 reverse_rule_distributive(&in[0]);
722         }
723         in[1] = c;
724
725         mode = get_mode_from_ops(in[0], in[1]);
726         irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
727
728         exchange(n, irn);
729         *node = irn;
730         return 1;
731 }
732
733 /**
734  * Apply the rules in reverse order, removing code that was not collapsed
735  */
736 static void reverse_rules(ir_node *node, void *env)
737 {
738         (void)env;
739
740         ir_graph *irg  = get_irn_irg(node);
741         ir_mode  *mode = get_irn_mode(node);
742         int res;
743
744         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
745         if (mode_is_float(mode) && get_irg_fp_model(irg) & fp_strict_algebraic)
746                 return;
747
748         do {
749                 ir_op *op = get_irn_op(node);
750
751                 res = 0;
752                 if (is_op_commutative(op)) {
753                         res = move_consts_up(&node);
754                 }
755                 /* beware: move_consts_up might have changed the opcode, check again */
756                 if (is_Add(node) || is_Sub(node)) {
757                         res = reverse_rule_distributive(&node);
758                 }
759         } while (res);
760 }
761
762 /*
763  * do the reassociation
764  */
765 void optimize_reassociation(ir_graph *irg)
766 {
767         assert(get_irg_pinned(irg) != op_pin_state_floats &&
768                 "Reassociation needs pinned graph to work properly");
769
770         assure_irg_properties(irg,
771                 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
772                 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
773
774         waitq *const wq = new_waitq();
775
776         /* disable some optimizations while reassoc is running to prevent endless loops */
777         set_reassoc_running(1);
778         {
779                 /* now we have collected enough information, optimize */
780                 irg_walk_graph(irg, NULL, wq_walker, wq);
781                 do_reassociation(wq);
782
783                 /* reverse those rules that do not result in collapsed constants */
784                 irg_walk_graph(irg, NULL, reverse_rules, NULL);
785         }
786         set_reassoc_running(0);
787
788         del_waitq(wq);
789
790         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
791 }
792
793 /* create a pass for the reassociation */
794 ir_graph_pass_t *optimize_reassociation_pass(const char *name)
795 {
796         return def_graph_pass(name ? name : "reassoc", optimize_reassociation);
797 }
798
799 static void register_node_reassoc_func(ir_op *op, reassociate_func func)
800 {
801         op->ops.reassociate = func;
802 }
803
804 void ir_register_reassoc_node_ops(void)
805 {
806         register_node_reassoc_func(op_Add, reassoc_commutative);
807         register_node_reassoc_func(op_And, reassoc_commutative);
808         register_node_reassoc_func(op_Eor, reassoc_commutative);
809         register_node_reassoc_func(op_Mul, reassoc_Mul);
810         register_node_reassoc_func(op_Or,  reassoc_commutative);
811         register_node_reassoc_func(op_Sub, reassoc_Sub);
812         register_node_reassoc_func(op_Shl, reassoc_Shl);
813 }
814
815 /* initialize the reassociation by adding operations to some opcodes */
816 void firm_init_reassociation(void)
817 {
818         FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");
819 }