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