reassoc: Remove pointless macro indirection.
[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 /**
355  * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
356  */
357 static int reassoc_Mul(ir_node **node)
358 {
359         ir_node *n = *node;
360         ir_node *add_sub, *c;
361         ir_op *op;
362
363         if (reassoc_commutative(&n))
364                 return 1;
365
366         get_comm_Binop_ops(n, &add_sub, &c);
367         op = get_irn_op(add_sub);
368
369         /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
370         if (op == op_Add || op == op_Sub) {
371                 ir_mode *mode = get_irn_mode(n);
372                 ir_node *irn, *block, *t1, *t2, *in[2];
373
374                 block = get_nodes_block(n);
375                 t1 = get_binop_left(add_sub);
376                 t2 = get_binop_right(add_sub);
377
378                 /* we can only multiplication rules on integer arithmetic */
379                 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
380                         ir_graph *irg = get_irn_irg(t1);
381                         in[0] = new_rd_Mul(NULL, block, c, t1, mode);
382                         in[1] = new_rd_Mul(NULL, block, c, t2, mode);
383
384                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
385
386                         /* In some cases it might happen that the new irn is equal the old one, for
387                          * instance in:
388                          * (x - 1) * y == x * y - y
389                          * will be transformed back by simpler optimization
390                          * We could switch simple optimizations off, but this only happens iff y
391                          * is a loop-invariant expression and that it is not clear if the new form
392                          * is better.
393                          * So, we let the old one.
394                          */
395                         if (irn != n) {
396                                 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
397                                         t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
398                                 exchange(n, irn);
399                                 *node = irn;
400
401                                 return 1;
402                         }
403                 }
404         }
405         return 0;
406 }
407
408 /**
409  * Reassociate Shl. We transform Shl(x, const) into Mul's if possible.
410  */
411 static int reassoc_Shl(ir_node **node)
412 {
413         ir_node   *n = *node;
414         ir_node   *c = get_Shl_right(n);
415         ir_node   *x, *blk, *irn;
416         ir_graph  *irg;
417         ir_mode   *mode;
418         ir_tarval *tv;
419
420         if (! is_Const(c))
421                 return 0;
422
423         x = get_Shl_left(n);
424         mode = get_irn_mode(x);
425
426         tv = get_mode_one(mode);
427         tv = tarval_shl(tv, get_Const_tarval(c));
428
429         if (tv == tarval_bad)
430                 return 0;
431
432         blk = get_nodes_block(n);
433         irg = get_irn_irg(blk);
434         c   = new_r_Const(irg, tv);
435         irn = new_rd_Mul(get_irn_dbg_info(n), blk, x, c, mode);
436
437         if (irn != n) {
438                 exchange(n, irn);
439                 *node = irn;
440                 return 1;
441         }
442         return 0;
443 }
444
445 /**
446  * The walker for the reassociation.
447  */
448 static void wq_walker(ir_node *n, void *env)
449 {
450         waitq *const wq = (waitq*)env;
451
452         set_irn_link(n, NULL);
453         if (!is_Block(n)) {
454                 waitq_put(wq, n);
455                 set_irn_link(n, wq);
456         }
457 }
458
459 /**
460  * The walker for the reassociation.
461  */
462 static void do_reassociation(waitq *const wq)
463 {
464         int i, res, changed;
465         ir_node *n;
466
467         while (! waitq_empty(wq)) {
468                 n = (ir_node*)waitq_get(wq);
469                 set_irn_link(n, NULL);
470
471                 hook_reassociate(1);
472
473                 /* reassociation must run until a fixpoint is reached. */
474                 changed = 0;
475                 do {
476                         ir_op    *op   = get_irn_op(n);
477                         ir_mode  *mode = get_irn_mode(n);
478
479                         res = 0;
480
481                         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
482                         if (mode_is_float(mode) && get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic)
483                                 break;
484
485                         if (op->ops.reassociate) {
486                                 res = op->ops.reassociate(&n);
487
488                                 changed |= res;
489                         }
490                 } while (res == 1);
491                 hook_reassociate(0);
492
493                 if (changed) {
494                         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
495                                 ir_node *pred = get_irn_n(n, i);
496
497                                 if (get_irn_link(pred) != wq) {
498                                         waitq_put(wq, pred);
499                                         set_irn_link(pred, wq);
500                                 }
501                         }
502                 }
503         }
504 }
505
506 /**
507  * Returns the earliest were a,b are available.
508  * Note that we know that a, b both dominate
509  * the block of the previous operation, so one must dominate the other.
510  *
511  * If the earliest block is the start block, return curr_blk instead
512  */
513 static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
514 {
515         ir_node *blk_a = get_nodes_block(a);
516         ir_node *blk_b = get_nodes_block(b);
517         ir_node *res;
518
519         /* if blk_a != blk_b, one must dominate the other */
520         if (block_dominates(blk_a, blk_b))
521                 res = blk_b;
522         else
523                 res = blk_a;
524         if (res == get_irg_start_block(get_irn_irg(curr_blk)))
525                 return curr_blk;
526         return res;
527 }
528
529 /**
530  * Checks whether a node is a Constant expression.
531  * The following trees are constant expressions:
532  *
533  * Const, SymConst, Const + SymConst
534  *
535  * Handling SymConsts as const might be not a good idea for all
536  * architectures ...
537  */
538 static int is_constant_expr(ir_node *irn)
539 {
540         switch (get_irn_opcode(irn)) {
541         case iro_Const:
542         case iro_SymConst:
543                 return 1;
544
545         case iro_Add: {
546                 ir_node *const l = get_Add_left(irn);
547                 if (!is_Const(l) && !is_SymConst(l))
548                         return 0;
549                 ir_node *const r = get_Add_right(irn);
550                 if (!is_Const(r) && !is_SymConst(r))
551                         return 0;
552                 return 1;
553         }
554
555         default:
556                 return 0;
557         }
558 }
559
560 /**
561  * Apply distributive Law for Mul and Add/Sub
562  */
563 static int reverse_rule_distributive(ir_node **node)
564 {
565         ir_node *n = *node;
566         ir_node *left  = get_binop_left(n);
567         ir_node *right = get_binop_right(n);
568         ir_node *x, *blk, *curr_blk;
569         ir_node *a, *b, *irn;
570         ir_op *op;
571         ir_mode *mode;
572         dbg_info *dbg;
573
574         op = get_irn_op(left);
575         if (op != get_irn_op(right))
576                 return 0;
577
578         if (op == op_Shl) {
579                 x = get_Shl_right(left);
580
581                 if (x == get_Shl_right(right)) {
582                         /* (a << x) +/- (b << x) ==> (a +/- b) << x */
583                         a = get_Shl_left(left);
584                         b = get_Shl_left(right);
585                         goto transform;
586                 }
587         } else if (op == op_Mul) {
588                 x = get_Mul_left(left);
589
590                 if (x == get_Mul_left(right)) {
591                         /* (x * a) +/- (x * b) ==> (a +/- b) * x */
592                         a = get_Mul_right(left);
593                         b = get_Mul_right(right);
594                         goto transform;
595                 } else if (x == get_Mul_right(right)) {
596                         /* (x * a) +/- (b * x) ==> (a +/- b) * x */
597                         a = get_Mul_right(left);
598                         b = get_Mul_left(right);
599                         goto transform;
600                 }
601
602                 x = get_Mul_right(left);
603
604                 if (x == get_Mul_right(right)) {
605                         /* (a * x) +/- (b * x) ==> (a +/- b) * x */
606                         a = get_Mul_left(left);
607                         b = get_Mul_left(right);
608                         goto transform;
609                 } else if (x == get_Mul_left(right)) {
610                         /* (a * x) +/- (x * b) ==> (a +/- b) * x */
611                         a = get_Mul_left(left);
612                         b = get_Mul_right(right);
613                         goto transform;
614                 }
615         }
616         return 0;
617
618 transform:
619         curr_blk = get_nodes_block(n);
620
621         blk = earliest_block(a, b, curr_blk);
622
623         dbg  = get_irn_dbg_info(n);
624         mode = get_irn_mode(n);
625
626         if (is_Add(n))
627                 irn = new_rd_Add(dbg, blk, a, b, mode);
628         else
629                 irn = new_rd_Sub(dbg, blk, a, b, mode);
630
631         blk  = earliest_block(irn, x, curr_blk);
632
633         if (op == op_Mul)
634                 irn = new_rd_Mul(dbg, blk, irn, x, mode);
635         else
636                 irn = new_rd_Shl(dbg, blk, irn, x, mode);
637
638         exchange(n, irn);
639         *node = irn;
640         return 1;
641 }
642
643 /**
644  * Move Constants towards the root.
645  */
646 static int move_consts_up(ir_node **node)
647 {
648         ir_node *n = *node;
649         ir_op *op;
650         ir_node *l, *r, *a, *b, *c, *blk, *irn, *in[2];
651         ir_mode *mode, *ma, *mb;
652         dbg_info *dbg;
653         ir_graph *irg;
654
655         l = get_binop_left(n);
656         r = get_binop_right(n);
657
658         /* check if one is already a constant expression */
659         if (is_constant_expr(l) || is_constant_expr(r))
660                 return 0;
661
662         dbg = get_irn_dbg_info(n);
663         op = get_irn_op(n);
664         if (get_irn_op(l) == op) {
665                 /* (a .op. b) .op. r */
666                 a = get_binop_left(l);
667                 b = get_binop_right(l);
668
669                 if (is_constant_expr(a)) {
670                         /* (C .op. b) .op. r ==> (r .op. b) .op. C */
671                         c = a;
672                         a = r;
673                         blk = get_nodes_block(l);
674                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
675                         goto transform;
676                 } else if (is_constant_expr(b)) {
677                         /* (a .op. C) .op. r ==> (a .op. r) .op. C */
678                         c = b;
679                         b = r;
680                         blk = get_nodes_block(l);
681                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
682                         goto transform;
683                 }
684         }
685         if (get_irn_op(r) == op) {
686                 /* l .op. (a .op. b) */
687                 a = get_binop_left(r);
688                 b = get_binop_right(r);
689
690                 if (is_constant_expr(a)) {
691                         /* l .op. (C .op. b) ==> (l .op. b) .op. C */
692                         c = a;
693                         a = l;
694                         blk = get_nodes_block(r);
695                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
696                         goto transform;
697                 } else if (is_constant_expr(b)) {
698                         /* l .op. (a .op. C) ==> (a .op. l) .op. C */
699                         c = b;
700                         b = l;
701                         blk = get_nodes_block(r);
702                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
703                         goto transform;
704                 }
705         }
706         return 0;
707
708 transform:
709         /* In some cases a and b might be both of different integer mode, and c a SymConst.
710          * in that case we could either
711          * 1.) cast into unsigned mode
712          * 2.) ignore
713          * we implement the second here
714          */
715         ma = get_irn_mode(a);
716         mb = get_irn_mode(b);
717         if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
718                 return 0;
719
720         /* check if (a .op. b) can be calculated in the same block is the old instruction */
721         if (! block_dominates(get_nodes_block(a), blk))
722                 return 0;
723         if (! block_dominates(get_nodes_block(b), blk))
724                 return 0;
725         /* ok */
726         in[0] = a;
727         in[1] = b;
728
729         mode = get_mode_from_ops(a, b);
730         irg  = get_irn_irg(blk);
731         in[0] = irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
732
733         /* beware: optimize_node might have changed the opcode, check again */
734         if (is_Add(irn) || is_Sub(irn)) {
735                 reverse_rule_distributive(&in[0]);
736         }
737         in[1] = c;
738
739         mode = get_mode_from_ops(in[0], in[1]);
740         irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
741
742         exchange(n, irn);
743         *node = irn;
744         return 1;
745 }
746
747 /**
748  * Apply the rules in reverse order, removing code that was not collapsed
749  */
750 static void reverse_rules(ir_node *node, void *env)
751 {
752         (void)env;
753
754         ir_graph *irg  = get_irn_irg(node);
755         ir_mode  *mode = get_irn_mode(node);
756         int res;
757
758         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
759         if (mode_is_float(mode) && get_irg_fp_model(irg) & fp_strict_algebraic)
760                 return;
761
762         do {
763                 ir_op *op = get_irn_op(node);
764
765                 res = 0;
766                 if (is_op_commutative(op)) {
767                         res = move_consts_up(&node);
768                 }
769                 /* beware: move_consts_up might have changed the opcode, check again */
770                 if (is_Add(node) || is_Sub(node)) {
771                         res = reverse_rule_distributive(&node);
772                 }
773         } while (res);
774 }
775
776 /*
777  * do the reassociation
778  */
779 void optimize_reassociation(ir_graph *irg)
780 {
781         assert(get_irg_pinned(irg) != op_pin_state_floats &&
782                 "Reassociation needs pinned graph to work properly");
783
784         assure_irg_properties(irg,
785                 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
786                 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
787
788         waitq *const wq = new_waitq();
789
790         /* disable some optimizations while reassoc is running to prevent endless loops */
791         set_reassoc_running(1);
792         {
793                 /* now we have collected enough information, optimize */
794                 irg_walk_graph(irg, NULL, wq_walker, wq);
795                 do_reassociation(wq);
796
797                 /* reverse those rules that do not result in collapsed constants */
798                 irg_walk_graph(irg, NULL, reverse_rules, NULL);
799         }
800         set_reassoc_running(0);
801
802         del_waitq(wq);
803
804         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
805 }
806
807 /* create a pass for the reassociation */
808 ir_graph_pass_t *optimize_reassociation_pass(const char *name)
809 {
810         return def_graph_pass(name ? name : "reassoc", optimize_reassociation);
811 }
812
813 static void register_node_reassoc_func(ir_op *op, reassociate_func func)
814 {
815         op->ops.reassociate = func;
816 }
817
818 void ir_register_reassoc_node_ops(void)
819 {
820         register_node_reassoc_func(op_Add, reassoc_commutative);
821         register_node_reassoc_func(op_And, reassoc_commutative);
822         register_node_reassoc_func(op_Eor, reassoc_commutative);
823         register_node_reassoc_func(op_Mul, reassoc_Mul);
824         register_node_reassoc_func(op_Or,  reassoc_commutative);
825         register_node_reassoc_func(op_Sub, reassoc_Sub);
826         register_node_reassoc_func(op_Shl, reassoc_Shl);
827 }
828
829 /* initialize the reassociation by adding operations to some opcodes */
830 void firm_init_reassociation(void)
831 {
832         FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");
833 }