Remove irg from the reassoc env.
[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 //#define NEW_REASSOC
47
48 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
49
50 typedef struct walker_t {
51         waitq    *wq;        /**< a wait queue */
52 } walker_t;
53
54 typedef enum {
55         NO_CONSTANT   = 0,    /**< node is not constant */
56         REAL_CONSTANT = 1,    /**< node is a Const that is suitable for constant folding */
57         REGION_CONST  = 4     /**< node is a constant expression in the current context,
58                                    use 4 here to simplify implementation of get_comm_Binop_ops() */
59 } const_class_t;
60
61 /**
62  * returns whether a node is constant ie is a constant or
63  * is loop invariant (called region constant)
64  *
65  * @param n     the node to be checked for constant
66  * @param block a block that might be in a loop
67  */
68 static const_class_t get_const_class(const ir_node *n, const ir_node *block)
69 {
70         if (is_Const(n))
71                 return REAL_CONSTANT;
72
73         /* constant nodes which can't be folded are region constants */
74         if (is_irn_constlike(n))
75                 return REGION_CONST;
76
77         /*
78          * Beware: Bad nodes are always loop-invariant, but
79          * cannot handled in later code, so filter them here.
80          */
81         if (! is_Bad(n) && is_loop_invariant(n, block))
82                 return REGION_CONST;
83
84         return NO_CONSTANT;
85 }  /* get_const_class */
86
87 /**
88  * returns the operands of a commutative bin-op, if one operand is
89  * a region constant, it is returned as the second one.
90  *
91  * Beware: Real constants must be returned with higher priority than
92  * region constants, because they might be folded.
93  */
94 static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
95 {
96         ir_node *op_a = get_binop_left(binop);
97         ir_node *op_b = get_binop_right(binop);
98         ir_node *block = get_nodes_block(binop);
99         int class_a = get_const_class(op_a, block);
100         int class_b = get_const_class(op_b, block);
101
102         assert(is_op_commutative(get_irn_op(binop)));
103
104         switch (class_a + 2*class_b) {
105         case REAL_CONSTANT + 2*REAL_CONSTANT:
106                 /* if both are constants, one might be a
107                  * pointer constant like NULL, return the other
108                  */
109                 if (mode_is_reference(get_irn_mode(op_a))) {
110                         *a = op_a;
111                         *c = op_b;
112                 } else {
113                         *a = op_b;
114                         *c = op_a;
115                 }
116                 break;
117         case REAL_CONSTANT + 2*NO_CONSTANT:
118         case REAL_CONSTANT + 2*REGION_CONST:
119         case REGION_CONST  + 2*NO_CONSTANT:
120                 *a = op_b;
121                 *c = op_a;
122                 break;
123         default:
124                 *a = op_a;
125                 *c = op_b;
126                 break;
127         }
128 }  /* get_comm_Binop_ops */
129
130 /**
131  * reassociate a Sub: x - c = x + (-c)
132  */
133 static int reassoc_Sub(ir_node **in)
134 {
135         ir_node *n = *in;
136         ir_node *right = get_Sub_right(n);
137         ir_mode *rmode = get_irn_mode(right);
138         ir_node *block;
139
140         /* cannot handle SubIs(P, P) */
141         if (mode_is_reference(rmode))
142                 return 0;
143
144         block = get_nodes_block(n);
145
146         /* handles rule R6:
147          * convert x - c => x + (-c)
148          */
149         if (get_const_class(right, block) == REAL_CONSTANT) {
150                 ir_node *left  = get_Sub_left(n);
151                 ir_mode *mode;
152                 dbg_info *dbi;
153                 ir_node *irn;
154
155                 switch (get_const_class(left, block)) {
156                 case REAL_CONSTANT:
157                         irn = optimize_in_place(n);
158                         if (irn != n) {
159                                 exchange(n, irn);
160                                 *in = irn;
161                                 return 1;
162                         }
163                         return 0;
164                 case NO_CONSTANT:
165                         break;
166                 default:
167                         /* already constant, nothing to do */
168                         return 0;
169                 }
170
171                 mode = get_irn_mode(n);
172                 dbi  = get_irn_dbg_info(n);
173
174                 /* Beware of SubP(P, Is) */
175                 irn = new_rd_Minus(dbi, block, right, rmode);
176                 irn = new_rd_Add(dbi, block, left, irn, mode);
177
178                 DBG((dbg, LEVEL_5, "Applied: %n - %n => %n + (-%n)\n",
179                         get_Sub_left(n), right, get_Sub_left(n), right));
180
181                 if (n == irn)
182                         return 0;
183
184                 exchange(n, irn);
185                 *in = irn;
186
187                 return 1;
188         }
189         return 0;
190 }  /* reassoc_Sub */
191
192 /** Retrieve a mode from the operands. We need this, because
193  * Add and Sub are allowed to operate on (P, Is)
194  */
195 static ir_mode *get_mode_from_ops(ir_node *op1, ir_node *op2)
196 {
197         ir_mode *m1, *m2;
198
199         m1 = get_irn_mode(op1);
200         if (mode_is_reference(m1))
201                 return m1;
202
203         m2 = get_irn_mode(op2);
204         if (mode_is_reference(m2))
205                 return m2;
206
207         assert(m1 == m2);
208
209         return m1;
210 }  /* get_mode_from_ops */
211
212 #ifndef NEW_REASSOC
213
214 /**
215  * reassociate a commutative Binop
216  *
217  * BEWARE: this rule leads to a potential loop, if
218  * two operands are region constants and the third is a
219  * constant, so avoid this situation.
220  */
221 static int reassoc_commutative(ir_node **node)
222 {
223         ir_node *n     = *node;
224         ir_op   *op    = get_irn_op(n);
225         ir_node *block = get_nodes_block(n);
226         ir_node *t1, *c1;
227
228         get_comm_Binop_ops(n, &t1, &c1);
229
230         if (get_irn_op(t1) == op) {
231                 ir_node *t2, *c2;
232                 const_class_t c_c1, c_c2, c_t2;
233
234                 get_comm_Binop_ops(t1, &t2, &c2);
235
236                 /* do not optimize Bad nodes, will fail later */
237                 if (is_Bad(t2))
238                         return 0;
239
240                 c_c1 = get_const_class(c1, block);
241                 c_c2 = get_const_class(c2, block);
242                 c_t2 = get_const_class(t2, block);
243
244                 if ( ((c_c1 > NO_CONSTANT) & (c_t2 > NO_CONSTANT)) &&
245                      ((((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0) || ((c_c1 & c_c2 & c_t2) == REGION_CONST)) ) {
246                         /* All three are constant and either all are constant expressions
247                          * or two of them are:
248                          * then applying this rule would lead into a cycle
249                          *
250                          * Note that if t2 is a constant so is c2 hence we save one test.
251                          */
252                         return 0;
253                 }
254
255                 if ((c_c1 != NO_CONSTANT) /* & (c_c2 != NO_CONSTANT) */) {
256                         /* handles rules R7, R8, R9, R10:
257                          * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
258                          */
259                         ir_node *irn, *in[2];
260                         ir_mode *mode, *mode_c1 = get_irn_mode(c1), *mode_c2 = get_irn_mode(c2);
261                         ir_graph *irg = get_irn_irg(c1);
262
263                         /* It might happen, that c1 and c2 have different modes, for
264                          * instance Is and Iu.
265                          * Handle this here.
266                          */
267                         if (mode_c1 != mode_c2) {
268                                 if (mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
269                                         /* get the bigger one */
270                                         if (get_mode_size_bits(mode_c1) > get_mode_size_bits(mode_c2))
271                                                 c2 = new_r_Conv(block, c2, mode_c1);
272                                         else if (get_mode_size_bits(mode_c1) < get_mode_size_bits(mode_c2))
273                                                 c1 = new_r_Conv(block, c1, mode_c2);
274                                         else {
275                                                 /* Try to cast the real const */
276                                                 if (c_c1 == REAL_CONSTANT)
277                                                         c1 = new_r_Conv(block, c1, mode_c2);
278                                                 else
279                                                         c2 = new_r_Conv(block, c2, mode_c1);
280                                         }
281                                 }
282                         }
283
284                         in[0] = c1;
285                         in[1] = c2;
286
287                         mode  = get_mode_from_ops(in[0], in[1]);
288                         in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
289                         in[0] = t2;
290
291                         mode = get_mode_from_ops(in[0], in[1]);
292                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
293
294                         DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
295                              c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
296                              t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
297                         /*
298                          * In some rare cases it can really happen that we get the same
299                          * node back. This might be happen in dead loops, were the Phi
300                          * nodes are already gone away. So check this.
301                          */
302                         if (n != irn) {
303                                 exchange(n, irn);
304                                 *node = irn;
305                                 return 1;
306                         }
307                 }
308         }
309         if (get_irn_op(c1) == op) {
310                 ir_node *t = c1;
311                 c1 = t1;
312                 t1 = t;
313         }
314         if (get_irn_op(t1) == op) {
315                 ir_node *l = get_binop_left(t1);
316                 ir_node *r = get_binop_right(t1);
317                 const_class_t c_r;
318
319                 if (r == c1) {
320                         ir_node *t = r;
321                         r = l;
322                         l = t;
323                 }
324                 c_r = get_const_class(r, block);
325                 if (c_r != NO_CONSTANT) {
326                         /*
327                          * Beware: don't do the following op if a constant was
328                          * placed below, else we will fall into a loop.
329                          */
330                         return 0;
331                 }
332
333                 if (l == c1) {
334                         /* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
335                         ir_mode *mode_res = get_irn_mode(n);
336                         ir_mode *mode_c1  = get_irn_mode(c1);
337                         ir_graph *irg     = get_irn_irg(c1);
338                         ir_node *irn, *in[2];
339
340                         in[0] = c1;
341                         in[1] = c1;
342
343                         in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode_c1, 2, in));
344                         in[0] = r;
345
346                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode_res, 2, in));
347
348                         DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
349                                 c1, get_irn_opname(n), l, get_irn_opname(n), r,
350                                 r, get_irn_opname(n), c1, get_irn_opname(n), c1));
351
352                         if (n != irn) {
353                                 exchange(n, irn);
354                                 *node = irn;
355                                 return 1;
356                         }
357                 }
358         }
359         return 0;
360 }  /* reassoc_commutative */
361
362 #else
363
364 static ir_op          *commutative_op;
365 static ir_node        *commutative_block;
366 static struct obstack  commutative_args;
367
368 static void collect_args(ir_node *node)
369 {
370         ir_node *left  = get_binop_left(node);
371         ir_node *right = get_binop_right(node);
372
373         if (get_irn_op(left) == commutative_op
374                         && (!get_irn_outs_computed(left) || get_irn_n_outs(left) == 1)) {
375                 collect_args(left);
376         } else {
377                 obstack_ptr_grow(&commutative_args, left);
378         }
379
380         if (get_irn_op(right) == commutative_op
381                         && (!get_irn_outs_computed(right) || get_irn_n_outs(right) == 1)) {
382                 collect_args(right);
383         } else {
384                 obstack_ptr_grow(&commutative_args, right);
385         }
386
387 #ifndef NDEBUG
388         {
389                 ir_mode *mode = get_irn_mode(node);
390                 if (is_Add(node) && mode_is_reference(mode)) {
391                         assert(get_irn_mode(left) == mode || get_irn_mode(right) == mode);
392                 } else {
393                         assert(get_irn_mode(left) == mode);
394                         assert(get_irn_mode(right) == mode);
395                 }
396         }
397 #endif
398 }
399
400 static int compare_nodes(const ir_node *node1, const ir_node *node2)
401 {
402         const_class_t class1 = get_const_class(node1, commutative_block);
403         const_class_t class2 = get_const_class(node2, commutative_block);
404
405         if (class1 == class2)
406                 return 0;
407         // return get_irn_idx(node1) - get_irn_idx(node2);
408
409         if (class1 < class2)
410                 return -1;
411
412         assert(class1 > class2);
413         return 1;
414 }
415
416 static int compare_node_ptr(const void *e1, const void *e2)
417 {
418         const ir_node *node1  = *((const ir_node *const*) e1);
419         const ir_node *node2  = *((const ir_node *const*) e2);
420         return compare_nodes(node1, node2);
421 }
422
423 static int reassoc_commutative(ir_node **n)
424 {
425         int       i;
426         int       n_args;
427         ir_node  *last;
428         ir_node **args;
429         ir_mode  *mode;
430         ir_node  *node = *n;
431
432         commutative_op    = get_irn_op(node);
433         commutative_block = get_nodes_block(node);
434
435         /* collect all nodes with same op type */
436         collect_args(node);
437
438         n_args = obstack_object_size(&commutative_args) / sizeof(ir_node*);
439         args   = obstack_finish(&commutative_args);
440
441         /* shortcut: in most cases there's nothing to do */
442         if (n_args == 2 && compare_nodes(args[0], args[1]) <= 0) {
443                 obstack_free(&commutative_args, args);
444                 return 0;
445         }
446
447         /* sort the arguments */
448         qsort(args, n_args, sizeof(ir_node*), compare_node_ptr);
449
450         /* build new tree */
451         last = args[n_args-1];
452         mode = get_irn_mode(last);
453         for (i = n_args-2; i >= 0; --i) {
454                 ir_mode *mode_right;
455                 ir_node *new_node;
456                 ir_node *in[2];
457                 ir_graph *irg = get_irn_irg(last);
458
459                 in[0] = last;
460                 in[1] = args[i];
461
462                 /* AddP violates the assumption that all modes in args are equal...
463                  * we need some hacks to cope with this */
464                 mode_right = get_irn_mode(in[1]);
465                 if (mode_is_reference(mode_right)) {
466                         assert(is_Add(node) && mode_is_reference(get_irn_mode(node)));
467                         mode = get_irn_mode(in[1]);
468                 }
469                 if (mode_right != mode) {
470                         assert(is_Add(node) && mode_is_reference(get_irn_mode(node)));
471                         in[1] = new_r_Conv(irg, commutative_block,in[1], mode);
472                 }
473
474                 /* TODO: produce useful debug info! */
475                 new_node = new_ir_node(NULL, irg, commutative_block,
476                                        commutative_op, mode, 2, in);
477                 new_node = optimize_node(new_node);
478                 last     = new_node;
479         }
480
481         /* CSE often returns the old node again, only exchange if needed */
482         if (last != node) {
483                 exchange(node, last);
484                 *n = last;
485                 return 1;
486         }
487         return 0;
488 }
489
490 #endif
491
492 #define reassoc_Add  reassoc_commutative
493 #define reassoc_And  reassoc_commutative
494 #define reassoc_Or   reassoc_commutative
495 #define reassoc_Eor  reassoc_commutative
496
497 /**
498  * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
499  */
500 static int reassoc_Mul(ir_node **node)
501 {
502         ir_node *n = *node;
503         ir_node *add_sub, *c;
504         ir_op *op;
505
506         if (reassoc_commutative(&n))
507                 return 1;
508
509         get_comm_Binop_ops(n, &add_sub, &c);
510         op = get_irn_op(add_sub);
511
512         /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
513         if (op == op_Add || op == op_Sub) {
514                 ir_mode *mode = get_irn_mode(n);
515                 ir_node *irn, *block, *t1, *t2, *in[2];
516
517                 block = get_nodes_block(n);
518                 t1 = get_binop_left(add_sub);
519                 t2 = get_binop_right(add_sub);
520
521                 /* we can only multiplication rules on integer arithmetic */
522                 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
523                         ir_graph *irg = get_irn_irg(t1);
524                         in[0] = new_rd_Mul(NULL, block, c, t1, mode);
525                         in[1] = new_rd_Mul(NULL, block, c, t2, mode);
526
527                         irn   = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
528
529                         /* In some cases it might happen that the new irn is equal the old one, for
530                          * instance in:
531                          * (x - 1) * y == x * y - y
532                          * will be transformed back by simpler optimization
533                          * We could switch simple optimizations off, but this only happens iff y
534                          * is a loop-invariant expression and that it is not clear if the new form
535                          * is better.
536                          * So, we let the old one.
537                          */
538                         if (irn != n) {
539                                 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
540                                         t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
541                                 exchange(n, irn);
542                                 *node = irn;
543
544                                 return 1;
545                         }
546                 }
547         }
548         return 0;
549 }  /* reassoc_Mul */
550
551 /**
552  * Reassociate Shl. We transform Shl(x, const) into Mul's if possible.
553  */
554 static int reassoc_Shl(ir_node **node)
555 {
556         ir_node   *n = *node;
557         ir_node   *c = get_Shl_right(n);
558         ir_node   *x, *blk, *irn;
559         ir_graph  *irg;
560         ir_mode   *mode;
561         ir_tarval *tv;
562
563         if (! is_Const(c))
564                 return 0;
565
566         x = get_Shl_left(n);
567         mode = get_irn_mode(x);
568
569         tv = get_mode_one(mode);
570         tv = tarval_shl(tv, get_Const_tarval(c));
571
572         if (tv == tarval_bad)
573                 return 0;
574
575         blk = get_nodes_block(n);
576         irg = get_irn_irg(blk);
577         c   = new_r_Const(irg, tv);
578         irn = new_rd_Mul(get_irn_dbg_info(n), blk, x, c, mode);
579
580         if (irn != n) {
581                 exchange(n, irn);
582                 *node = irn;
583                 return 1;
584         }
585         return 0;
586 }  /* reassoc_Shl */
587
588 /**
589  * The walker for the reassociation.
590  */
591 static void wq_walker(ir_node *n, void *env)
592 {
593         walker_t *wenv = (walker_t*)env;
594
595         set_irn_link(n, NULL);
596         if (!is_Block(n)) {
597                 waitq_put(wenv->wq, n);
598                 set_irn_link(n, wenv->wq);
599         }
600 }  /* wq_walker */
601
602 /**
603  * The walker for the reassociation.
604  */
605 static void do_reassociation(walker_t *wenv)
606 {
607         int i, res, changed;
608         ir_node *n;
609
610         while (! waitq_empty(wenv->wq)) {
611                 n = (ir_node*)waitq_get(wenv->wq);
612                 set_irn_link(n, NULL);
613
614                 hook_reassociate(1);
615
616                 /* reassociation must run until a fixpoint is reached. */
617                 changed = 0;
618                 do {
619                         ir_op    *op   = get_irn_op(n);
620                         ir_mode  *mode = get_irn_mode(n);
621
622                         res = 0;
623
624                         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
625                         if (mode_is_float(mode) && get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic)
626                                 break;
627
628                         if (op->ops.reassociate) {
629                                 res = op->ops.reassociate(&n);
630
631                                 changed |= res;
632                         }
633                 } while (res == 1);
634                 hook_reassociate(0);
635
636                 if (changed) {
637                         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
638                                 ir_node *pred = get_irn_n(n, i);
639
640                                 if (get_irn_link(pred) != wenv->wq) {
641                                         waitq_put(wenv->wq, pred);
642                                         set_irn_link(pred, wenv->wq);
643                                 }
644                         }
645                 }
646         }
647 }  /* do_reassociation */
648
649 /**
650  * Returns the earliest were a,b are available.
651  * Note that we know that a, b both dominate
652  * the block of the previous operation, so one must dominate the other.
653  *
654  * If the earliest block is the start block, return curr_blk instead
655  */
656 static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
657 {
658         ir_node *blk_a = get_nodes_block(a);
659         ir_node *blk_b = get_nodes_block(b);
660         ir_node *res;
661
662         /* if blk_a != blk_b, one must dominate the other */
663         if (block_dominates(blk_a, blk_b))
664                 res = blk_b;
665         else
666                 res = blk_a;
667         if (res == get_irg_start_block(get_irn_irg(curr_blk)))
668                 return curr_blk;
669         return res;
670 }  /* earliest_block */
671
672 /**
673  * Checks whether a node is a Constant expression.
674  * The following trees are constant expressions:
675  *
676  * Const, SymConst, Const + SymConst
677  *
678  * Handling SymConsts as const might be not a good idea for all
679  * architectures ...
680  */
681 static int is_constant_expr(ir_node *irn)
682 {
683         ir_op *op;
684
685         switch (get_irn_opcode(irn)) {
686         case iro_Const:
687         case iro_SymConst:
688                 return 1;
689         case iro_Add:
690                 op = get_irn_op(get_Add_left(irn));
691                 if (op != op_Const && op != op_SymConst)
692                         return 0;
693                 op = get_irn_op(get_Add_right(irn));
694                 if (op != op_Const && op != op_SymConst)
695                         return 0;
696                 return 1;
697         default:
698                 return 0;
699         }
700 }  /* is_constant_expr */
701
702 /**
703  * Apply distributive Law for Mul and Add/Sub
704  */
705 static int reverse_rule_distributive(ir_node **node)
706 {
707         ir_node *n = *node;
708         ir_node *left  = get_binop_left(n);
709         ir_node *right = get_binop_right(n);
710         ir_node *x, *blk, *curr_blk;
711         ir_node *a, *b, *irn;
712         ir_op *op;
713         ir_mode *mode;
714         dbg_info *dbg;
715
716         op = get_irn_op(left);
717         if (op != get_irn_op(right))
718                 return 0;
719
720         if (op == op_Shl) {
721                 x = get_Shl_right(left);
722
723                 if (x == get_Shl_right(right)) {
724                         /* (a << x) +/- (b << x) ==> (a +/- b) << x */
725                         a = get_Shl_left(left);
726                         b = get_Shl_left(right);
727                         goto transform;
728                 }
729         } else if (op == op_Mul) {
730                 x = get_Mul_left(left);
731
732                 if (x == get_Mul_left(right)) {
733                         /* (x * a) +/- (x * b) ==> (a +/- b) * x */
734                         a = get_Mul_right(left);
735                         b = get_Mul_right(right);
736                         goto transform;
737                 } else if (x == get_Mul_right(right)) {
738                         /* (x * a) +/- (b * x) ==> (a +/- b) * x */
739                         a = get_Mul_right(left);
740                         b = get_Mul_left(right);
741                         goto transform;
742                 }
743
744                 x = get_Mul_right(left);
745
746                 if (x == get_Mul_right(right)) {
747                         /* (a * x) +/- (b * x) ==> (a +/- b) * x */
748                         a = get_Mul_left(left);
749                         b = get_Mul_left(right);
750                         goto transform;
751                 } else if (x == get_Mul_left(right)) {
752                         /* (a * x) +/- (x * b) ==> (a +/- b) * x */
753                         a = get_Mul_left(left);
754                         b = get_Mul_right(right);
755                         goto transform;
756                 }
757         }
758         return 0;
759
760 transform:
761         curr_blk = get_nodes_block(n);
762
763         blk = earliest_block(a, b, curr_blk);
764
765         dbg  = get_irn_dbg_info(n);
766         mode = get_irn_mode(n);
767
768         if (is_Add(n))
769                 irn = new_rd_Add(dbg, blk, a, b, mode);
770         else
771                 irn = new_rd_Sub(dbg, blk, a, b, mode);
772
773         blk  = earliest_block(irn, x, curr_blk);
774
775         if (op == op_Mul)
776                 irn = new_rd_Mul(dbg, blk, irn, x, mode);
777         else
778                 irn = new_rd_Shl(dbg, blk, irn, x, mode);
779
780         exchange(n, irn);
781         *node = irn;
782         return 1;
783 }  /* reverse_rule_distributive */
784
785 /**
786  * Move Constants towards the root.
787  */
788 static int move_consts_up(ir_node **node)
789 {
790         ir_node *n = *node;
791         ir_op *op;
792         ir_node *l, *r, *a, *b, *c, *blk, *irn, *in[2];
793         ir_mode *mode, *ma, *mb;
794         dbg_info *dbg;
795         ir_graph *irg;
796
797         l = get_binop_left(n);
798         r = get_binop_right(n);
799
800         /* check if one is already a constant expression */
801         if (is_constant_expr(l) || is_constant_expr(r))
802                 return 0;
803
804         dbg = get_irn_dbg_info(n);
805         op = get_irn_op(n);
806         if (get_irn_op(l) == op) {
807                 /* (a .op. b) .op. r */
808                 a = get_binop_left(l);
809                 b = get_binop_right(l);
810
811                 if (is_constant_expr(a)) {
812                         /* (C .op. b) .op. r ==> (r .op. b) .op. C */
813                         c = a;
814                         a = r;
815                         blk = get_nodes_block(l);
816                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
817                         goto transform;
818                 } else if (is_constant_expr(b)) {
819                         /* (a .op. C) .op. r ==> (a .op. r) .op. C */
820                         c = b;
821                         b = r;
822                         blk = get_nodes_block(l);
823                         dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
824                         goto transform;
825                 }
826         }
827         if (get_irn_op(r) == op) {
828                 /* l .op. (a .op. b) */
829                 a = get_binop_left(r);
830                 b = get_binop_right(r);
831
832                 if (is_constant_expr(a)) {
833                         /* l .op. (C .op. b) ==> (l .op. b) .op. C */
834                         c = a;
835                         a = l;
836                         blk = get_nodes_block(r);
837                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
838                         goto transform;
839                 } else if (is_constant_expr(b)) {
840                         /* l .op. (a .op. C) ==> (a .op. l) .op. C */
841                         c = b;
842                         b = l;
843                         blk = get_nodes_block(r);
844                         dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
845                         goto transform;
846                 }
847         }
848         return 0;
849
850 transform:
851         /* In some cases a and b might be both of different integer mode, and c a SymConst.
852          * in that case we could either
853          * 1.) cast into unsigned mode
854          * 2.) ignore
855          * we implement the second here
856          */
857         ma = get_irn_mode(a);
858         mb = get_irn_mode(b);
859         if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
860                 return 0;
861
862         /* check if (a .op. b) can be calculated in the same block is the old instruction */
863         if (! block_dominates(get_nodes_block(a), blk))
864                 return 0;
865         if (! block_dominates(get_nodes_block(b), blk))
866                 return 0;
867         /* ok */
868         in[0] = a;
869         in[1] = b;
870
871         mode = get_mode_from_ops(a, b);
872         irg  = get_irn_irg(blk);
873         in[0] = irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
874
875         /* beware: optimize_node might have changed the opcode, check again */
876         if (is_Add(irn) || is_Sub(irn)) {
877                 reverse_rule_distributive(&in[0]);
878         }
879         in[1] = c;
880
881         mode = get_mode_from_ops(in[0], in[1]);
882         irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
883
884         exchange(n, irn);
885         *node = irn;
886         return 1;
887 }  /* move_consts_up */
888
889 /**
890  * Apply the rules in reverse order, removing code that was not collapsed
891  */
892 static void reverse_rules(ir_node *node, void *env)
893 {
894         (void)env;
895
896         ir_graph *irg  = get_irn_irg(node);
897         ir_mode  *mode = get_irn_mode(node);
898         int res;
899
900         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
901         if (mode_is_float(mode) && get_irg_fp_model(irg) & fp_strict_algebraic)
902                 return;
903
904         do {
905                 ir_op *op = get_irn_op(node);
906
907                 res = 0;
908                 if (is_op_commutative(op)) {
909                         res = move_consts_up(&node);
910                 }
911                 /* beware: move_consts_up might have changed the opcode, check again */
912                 if (is_Add(node) || is_Sub(node)) {
913                         res = reverse_rule_distributive(&node);
914                 }
915         } while (res);
916 }
917
918 /*
919  * do the reassociation
920  */
921 void optimize_reassociation(ir_graph *irg)
922 {
923         walker_t env;
924
925         assert(get_irg_phase_state(irg) != phase_building);
926         assert(get_irg_pinned(irg) != op_pin_state_floats &&
927                 "Reassociation needs pinned graph to work properly");
928
929         assure_irg_properties(irg,
930                 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
931                 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
932
933 #ifdef NEW_REASSOC
934         assire_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
935         obstack_init(&commutative_args);
936 #endif
937
938         env.wq  = new_waitq();
939
940         /* disable some optimizations while reassoc is running to prevent endless loops */
941         set_reassoc_running(1);
942         {
943                 /* now we have collected enough information, optimize */
944                 irg_walk_graph(irg, NULL, wq_walker, &env);
945                 do_reassociation(&env);
946
947                 /* reverse those rules that do not result in collapsed constants */
948                 irg_walk_graph(irg, NULL, reverse_rules, NULL);
949         }
950         set_reassoc_running(0);
951
952 #ifdef NEW_REASSOC
953         obstack_free(&commutative_args, NULL);
954 #endif
955
956         del_waitq(env.wq);
957
958         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
959 }  /* optimize_reassociation */
960
961 /* create a pass for the reassociation */
962 ir_graph_pass_t *optimize_reassociation_pass(const char *name)
963 {
964         return def_graph_pass(name ? name : "reassoc", optimize_reassociation);
965 }  /* optimize_reassociation_pass */
966
967 static void register_node_reassoc_func(ir_op *op, reassociate_func func)
968 {
969         op->ops.reassociate = func;
970 }
971
972 void ir_register_reassoc_node_ops(void)
973 {
974         register_node_reassoc_func(op_Mul, reassoc_Mul);
975         register_node_reassoc_func(op_Add, reassoc_Add);
976         register_node_reassoc_func(op_Sub, reassoc_Sub);
977         register_node_reassoc_func(op_And, reassoc_And);
978         register_node_reassoc_func(op_Or,  reassoc_Or);
979         register_node_reassoc_func(op_Eor, reassoc_Eor);
980         register_node_reassoc_func(op_Shl, reassoc_Shl);
981 }
982
983 /* initialize the reassociation by adding operations to some opcodes */
984 void firm_init_reassociation(void)
985 {
986         FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");
987 }  /* firm_init_reassociation */