add a phase to reverse distributive law rules
[libfirm] / ir / opt / reassoc.c
1 /*
2  * Copyright (C) 1995-2007 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  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "iropt_t.h"
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33 #include "irmode_t.h"
34 #include "ircons_t.h"
35 #include "irgmod.h"
36 #include "iropt_dbg.h"
37 #include "irflag_t.h"
38 #include "irgwalk.h"
39 #include "reassoc_t.h"
40 #include "irhooks.h"
41 #include "irloop.h"
42 #include "pdeq.h"
43 #include "debug.h"
44
45 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
46
47 typedef struct _walker_t {
48         int   changes;        /**< set, if a reassociation take place */
49         waitq *wq;            /**< a wait queue */
50 } walker_t;
51
52 typedef enum {
53         NO_CONSTANT   = 0,    /**< node is not constant */
54         REAL_CONSTANT = 1,    /**< node is a Const that is suitable for constant folding */
55         REGION_CONST  = 4     /**< node is a constant expression in the current context,
56                                    use 4 here to simplify implementation of get_comm_Binop_ops() */
57 } const_class_t;
58
59 /**
60  * returns whether a node is constant ie is a constant or
61  * is loop invariant (called region constant)
62  *
63  * @param n     the node to be checked for constant
64  * @param block a block that might be in a loop
65  */
66 static const_class_t get_const_class(ir_node *n, ir_node *block)
67 {
68         ir_op *op = get_irn_op(n);
69
70         if (op == op_Const)
71                 return REAL_CONSTANT;
72
73         /* although SymConst's are of course real constant, we cannot
74            fold them, so handle them like region constants */
75         if (op == op_SymConst)
76                 return REGION_CONST;
77
78         /*
79          * Beware: Bad nodes are always loop-invariant, but
80          * cannot handled in later code, so filter them here.
81          */
82         if (! is_Bad(n) && is_loop_invariant(n, block))
83                 return REGION_CONST;
84
85         return NO_CONSTANT;
86 }  /* get_const_class */
87
88 /**
89  * returns the operands of a commutative bin-op, if one operand is
90  * a region constant, it is returned as the second one.
91  *
92  * Beware: Real constants must be returned with higher priority than
93  * region constants, because they might be folded.
94  */
95 static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
96 {
97         ir_node *op_a = get_binop_left(binop);
98         ir_node *op_b = get_binop_right(binop);
99         ir_node *block = get_nodes_block(binop);
100         int class_a = get_const_class(op_a, block);
101         int class_b = get_const_class(op_b, block);
102
103         assert(is_op_commutative(get_irn_op(binop)));
104
105         switch (class_a + 2*class_b) {
106         case REAL_CONSTANT + 2*REAL_CONSTANT:
107                 /* if both are constants, one might be a
108                  * pointer constant like NULL, return the other
109                  */
110                 if (mode_is_reference(get_irn_mode(op_a))) {
111                         *a = op_a;
112                         *c = op_b;
113                 } else {
114                         *a = op_b;
115                         *c = op_a;
116                 }
117                 break;
118         case REAL_CONSTANT + 2*NO_CONSTANT:
119         case REAL_CONSTANT + 2*REGION_CONST:
120         case REGION_CONST  + 2*NO_CONSTANT:
121                 *a = op_b;
122                 *c = op_a;
123                 break;
124         default:
125                 *a = op_a;
126                 *c = op_b;
127                 break;
128         }
129 }  /* get_comm_Binop_ops */
130
131 /**
132  * reassociate a Sub: x - c = x + (-c)
133  */
134 static int reassoc_Sub(ir_node **in)
135 {
136         ir_node *n = *in;
137         ir_node *right = get_Sub_right(n);
138         ir_mode *rmode = get_irn_mode(right);
139         ir_node *block;
140
141         /* cannot handle SubIs(P, P) */
142         if (mode_is_reference(rmode))
143                 return 0;
144
145         block = get_nodes_block(n);
146
147         /* handles rule R6:
148          * convert x - c => x + (-c)
149          */
150         if (get_const_class(right, block) == REAL_CONSTANT) {
151                 ir_node *left  = get_Sub_left(n);
152                 ir_mode *mode;
153                 dbg_info *dbi;
154                 ir_node *irn;
155
156                 switch (get_const_class(left, block)) {
157                 case REAL_CONSTANT:
158                         irn = optimize_in_place(n);
159                         if (irn != n) {
160                                 exchange(n, irn);
161                                 *in = irn;
162                                 return 1;
163                         }
164                         return 0;
165                 case NO_CONSTANT:
166                         break;
167                 default:
168                         /* already constant, nothing to do */
169                         return 0;
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, current_ir_graph, block, right, rmode);
176                 irn = new_rd_Add(dbi, current_ir_graph, block, left, irn, get_irn_mode(n));
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 /**
213  * reassociate a commutative Binop
214  *
215  * BEWARE: this rule leads to a potential loop, if
216  * two operands are region constants and the third is a
217  * constant, so avoid this situation.
218  */
219 static int reassoc_commutative(ir_node **node)
220 {
221         ir_node *n     = *node;
222         ir_op *op      = get_irn_op(n);
223         ir_node *block = get_nodes_block(n);
224         ir_node *t1, *c1;
225
226         get_comm_Binop_ops(n, &t1, &c1);
227
228         if (get_irn_op(t1) == op) {
229                 ir_node *t2, *c2;
230                 const_class_t c_c1, c_c2, c_t2;
231
232                 get_comm_Binop_ops(t1, &t2, &c2);
233
234                 /* do not optimize Bad nodes, will fail later */
235                 if (is_Bad(t2))
236                         return 0;
237
238                 c_c1 = get_const_class(c1, block);
239                 c_c2 = get_const_class(c2, block);
240                 c_t2 = get_const_class(t2, block);
241
242                 if ( ((c_c1 > NO_CONSTANT) & (c_t2 > NO_CONSTANT)) &&
243                      ((((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0) || ((c_c1 & c_c2 & c_t2) == REGION_CONST)) ) {
244                         /* All three are constant and either all are constant expressions or two of them are:
245                          * then applying this rule would lead into a cycle
246                          *
247                          * Note that if t2 is a constant so is c2 hence we save one test.
248                          */
249                         return 0;
250                 }
251
252                 if ((c_c1 != NO_CONSTANT) /* & (c_c2 != NO_CONSTANT) */) {
253                         /* handles rules R7, R8, R9, R10:
254                          * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
255                          */
256                         ir_node *irn, *in[2];
257                         ir_mode *mode, *mode_c1 = get_irn_mode(c1), *mode_c2 = get_irn_mode(c2);
258
259                         /* It might happen, that c1 and c2 have different modes, for instance Is and Iu.
260                          * Handle this here.
261                          */
262                         if (mode_c1 != mode_c2) {
263                                 if (mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
264                                         /* get the bigger one */
265                                         if (get_mode_size_bits(mode_c1) > get_mode_size_bits(mode_c2))
266                                                 c2 = new_r_Conv(current_ir_graph, block, c2, mode_c1);
267                                         else if (get_mode_size_bits(mode_c1) < get_mode_size_bits(mode_c2))
268                                                 c1 = new_r_Conv(current_ir_graph, block, c1, mode_c2);
269                                         else {
270                                                 /* Try to cast the real const */
271                                                 if (c_c1 == REAL_CONSTANT)
272                                                         c1 = new_r_Conv(current_ir_graph, block, c1, mode_c2);
273                                                 else
274                                                         c2 = new_r_Conv(current_ir_graph, block, c2, mode_c1);
275                                         }
276                                 }
277                         }
278
279                         in[0] = c1;
280                         in[1] = c2;
281
282                         mode  = get_mode_from_ops(in[0], in[1]);
283                         in[1] = optimize_node(new_ir_node(NULL, current_ir_graph, block, op, mode, 2, in));
284                         in[0] = t2;
285
286                         mode = get_mode_from_ops(in[0], in[1]);
287                         irn   = optimize_node(new_ir_node(NULL, current_ir_graph, block, op, mode, 2, in));
288
289                         DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
290                              c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
291                              t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
292                         /*
293                          * In some rare cases it can really happen that we get the same node back.
294                          * This might be happen in dead loops, were the Phi nodes are already gone away.
295                          * So check this.
296                          */
297                         if (n != irn) {
298                                 exchange(n, irn);
299                                 *node = irn;
300                                 return 1;
301                         }
302                 }
303         }
304         return 0;
305 }  /* reassoc_commutative */
306
307 #define reassoc_Add  reassoc_commutative
308 #define reassoc_And  reassoc_commutative
309 #define reassoc_Or   reassoc_commutative
310 #define reassoc_Eor  reassoc_commutative
311
312 /**
313  * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
314  */
315 static int reassoc_Mul(ir_node **node)
316 {
317         ir_node *n = *node;
318         ir_node *add_sub, *c;
319         ir_op *op;
320
321         if (reassoc_commutative(&n))
322                 return 1;
323
324         get_comm_Binop_ops(n, &add_sub, &c);
325         op = get_irn_op(add_sub);
326
327         /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
328         if (op == op_Add || op == op_Sub) {
329                 ir_mode *mode = get_irn_mode(n);
330                 ir_node *irn, *block, *t1, *t2, *in[2];
331
332                 block = get_nodes_block(n);
333                 t1 = get_binop_left(add_sub);
334                 t2 = get_binop_right(add_sub);
335
336                 /* we can only multiplication rules on integer arithmetic */
337                 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
338                         in[0] = new_rd_Mul(NULL, current_ir_graph, block, c, t1, mode);
339                         in[1] = new_rd_Mul(NULL, current_ir_graph, block, c, t2, mode);
340
341                         mode  = get_mode_from_ops(in[0], in[1]);
342                         irn   = optimize_node(new_ir_node(NULL, current_ir_graph, block, op, mode, 2, in));
343
344                         /* In some cases it might happen that the new irn is equal the old one, for
345                          * instance in:
346                          * (x - 1) * y == x * y - y
347                          * will be transformed back by simpler optimization
348                          * We could switch simple optimizations off, but this only happens iff y
349                          * is a loop-invariant expression and that it is not clear if the new form
350                          * is better.
351                          * So, we let the old one.
352                          */
353                         if (irn != n) {
354                                 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
355                                         t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
356                                 exchange(n, irn);
357                                 *node = irn;
358
359                                 return 1;
360                         }
361                 }
362         }
363         return 0;
364 }  /* reassoc_Mul */
365
366 /**
367  * The walker for the reassociation.
368  */
369 static void wq_walker(ir_node *n, void *env)
370 {
371         walker_t *wenv = env;
372
373         set_irn_link(n, NULL);
374         if (is_no_Block(n)) {
375                 ir_node *blk = get_nodes_block(n);
376
377                 if (is_Block_dead(blk) || get_Block_dom_depth(blk) < 0) {
378                         /* We are in a dead block, do not optimize or we may fall into an endless
379                            loop. We check this here instead of requiring that all dead blocks are removed
380                            which or cf_opt do not guarantee yet. */
381                         return;
382                 }
383                 waitq_put(wenv->wq, n);
384                 set_irn_link(n, wenv->wq);
385         }
386 }  /* wq_walker */
387
388 /**
389  * The walker for the reassociation.
390  */
391 static void do_reassociation(walker_t *wenv)
392 {
393         int i, res, changed;
394         ir_node *n, *blk;
395
396
397         while (! waitq_empty(wenv->wq)) {
398                 n = waitq_get(wenv->wq);
399                 set_irn_link(n, NULL);
400
401                 blk = get_nodes_block(n);
402                 if (is_Block_dead(blk) || get_Block_dom_depth(blk) < 0) {
403                         /* We are in a dead block, do not optimize or we may fall into an endless
404                            loop. We check this here instead of requiring that all dead blocks are removed
405                            which or cf_opt do not guarantee yet. */
406                         continue;
407                 }
408
409
410                 hook_reassociate(1);
411
412                 /* reassociation must run until a fixpoint is reached. */
413                 changed = 0;
414                 do {
415                         ir_op   *op    = get_irn_op(n);
416                         ir_mode *mode  = get_irn_mode(n);
417
418                         res = 0;
419
420                         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
421                         if (mode_is_float(mode) && get_irg_fp_model(current_ir_graph) & fp_strict_algebraic)
422                                 break;
423
424                         if (op->ops.reassociate) {
425                                 res = op->ops.reassociate(&n);
426
427                                 changed |= res;
428                         }
429                 } while (res == 1);
430                 hook_reassociate(0);
431
432                 wenv->changes |= changed;
433
434                 if (changed) {
435                         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
436                                 ir_node *pred = get_irn_n(n, i);
437
438                                 if (get_irn_link(pred) != wenv->wq) {
439                                         waitq_put(wenv->wq, pred);
440                                         set_irn_link(pred, wenv->wq);
441                                 }
442                         }
443                 }
444         }
445 }  /* do_reassociation */
446
447 /**
448  * Returns the earliest were a,b are available.
449  * Note that we know that we know that a, b both dominate
450  * the block of the previous operation, so one must dominate the other.
451  */
452 static ir_node *earliest_block(ir_node *a, ir_node *b) {
453         ir_node *blk_a = get_nodes_block(a);
454         ir_node *blk_b = get_nodes_block(b);
455
456         /* if blk_a != blk_b, one must dominate the other */
457         if (block_dominates(blk_a, blk_b))
458                 return blk_b;
459         else
460                 return blk_a;
461 }
462
463 /**
464  * Apply distributive Law for Mul and Add/Sub
465  */
466 static int reverse_rule_distributive(ir_node **node) {
467         ir_node *n = *node;
468         ir_node *left  = get_binop_left(n);
469         ir_node *right = get_binop_right(n);
470         ir_node *x, *blk;
471         ir_node *a, *b, *irn;
472         ir_mode *mode;
473         dbg_info *dbg;
474
475         if (! is_Mul(left) || !is_Mul(right))
476                 return 0;
477
478         x = get_Mul_left(left);
479
480         if (x == get_Mul_left(right)) {
481                 /* (x * a) +/- (x * b) */
482                 a = get_Mul_right(left);
483                 b = get_Mul_right(right);
484                 goto transform;
485         } else if (x == get_Mul_right(right)) {
486                 /* (x * a) +/- (b * x) */
487                 a = get_Mul_right(left);
488                 b = get_Mul_left(right);
489                 goto transform;
490         }
491
492         x = get_Mul_right(left);
493
494         if (x == get_Mul_right(right)) {
495                 /* (a * x) +/- (b * x) */
496                 a = get_Mul_left(left);
497                 b = get_Mul_left(right);
498                 goto transform;
499         } else if (x == get_Mul_left(right)) {
500                 /* (a * x) +/- (x * b) */
501                 a = get_Mul_left(left);
502                 b = get_Mul_right(right);
503                 goto transform;
504         }
505         return 0;
506
507 transform:
508         blk = earliest_block(a, b);
509
510         dbg  = get_irn_dbg_info(n);
511         mode = get_irn_mode(n);
512
513         if (is_Add(n))
514                 irn = new_rd_Add(dbg, current_ir_graph, blk, a, b, mode);
515         else
516                 irn = new_rd_Sub(dbg, current_ir_graph, blk, a, b, mode);
517
518         blk  = earliest_block(irn, x);
519         irn = new_rd_Mul(dbg, current_ir_graph, blk, irn, x, mode);
520
521         exchange(n, irn);
522         *node = irn;
523         return 1;
524 }  /* reverse_rule_distributive */
525
526
527 /**
528  * Apply the rules in reverse order, removing code that was not collapsed
529  */
530 static void reverse_rules(ir_node *node, void *env) {
531         walker_t *wenv = env;
532         ir_mode *mode = get_irn_mode(node);
533         int res;
534
535         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
536         if (mode_is_float(mode) && get_irg_fp_model(current_ir_graph) & fp_strict_algebraic)
537                 return;
538
539         do {
540                 res = 0;
541                 if (is_Add(node) || is_Sub(node)) {
542                         wenv->changes |= res = reverse_rule_distributive(&node);
543                 }
544         } while (res);
545 }
546
547 /*
548  * do the reassociation
549  */
550 void optimize_reassociation(ir_graph *irg)
551 {
552         walker_t env;
553         irg_loopinfo_state state;
554
555         assert(get_irg_phase_state(irg) != phase_building);
556         assert(get_irg_pinned(irg) != op_pin_state_floats &&
557                 "Reassociation needs pinned graph to work properly");
558
559         /* reassociation needs constant folding */
560         if (!get_opt_reassociation() || !get_opt_constant_folding())
561                 return;
562
563         /* we use dominance to detect dead blocks */
564         assure_doms(irg);
565
566         /*
567          * Calculate loop info, so we could identify loop-invariant
568          * code and threat it like a constant.
569          * We only need control flow loops here but can handle generic
570          * INTRA info as well.
571          */
572         state = get_irg_loopinfo_state(irg);
573         if ((state & loopinfo_inter) ||
574                 (state & (loopinfo_constructed | loopinfo_valid)) != (loopinfo_constructed | loopinfo_valid))
575                 construct_cf_backedges(irg);
576
577         env.changes = 0;
578         env.wq      = new_waitq();
579
580         /* now we have collected enough information, optimize */
581         irg_walk_graph(irg, NULL, wq_walker, &env);
582         do_reassociation(&env);
583
584         /* reverse those rules that do not result in collapsed constants */
585         irg_walk_graph(irg, NULL, reverse_rules, &env);
586
587         /* Handle graph state */
588         if (env.changes) {
589                 set_irg_outs_inconsistent(irg);
590                 set_irg_loopinfo_inconsistent(irg);
591         }
592
593         del_waitq(env.wq);
594 }  /* optimize_reassociation */
595
596 /* Sets the default reassociation operation for an ir_op_ops. */
597 ir_op_ops *firm_set_default_reassoc(ir_opcode code, ir_op_ops *ops)
598 {
599 #define CASE(a) case iro_##a: ops->reassociate  = reassoc_##a; break
600
601         switch (code) {
602         CASE(Mul);
603         CASE(Add);
604         CASE(Sub);
605         CASE(And);
606         CASE(Or);
607         CASE(Eor);
608         default:
609                 /* leave NULL */;
610         }
611
612         return ops;
613 #undef CASE
614 }  /* firm_set_default_reassoc */
615
616 /* initialize the reassociation by adding operations to some opcodes */
617 void firm_init_reassociation(void)
618 {
619         FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");
620 }  /* firm_init_reassociation */