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