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