2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Reassociation
23 * @author Michael Beck
27 #include "iroptimize.h"
30 #include "irgraph_t.h"
34 #include "iropt_dbg.h"
38 #include "reassoc_t.h"
48 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
50 typedef struct walker_t {
51 waitq *wq; /**< a wait queue */
55 NO_CONSTANT = 0, /**< node is not constant */
56 REAL_CONSTANT = 1, /**< node is a Const that is suitable for constant folding */
57 REGION_CONST = 4 /**< node is a constant expression in the current context,
58 use 4 here to simplify implementation of get_comm_Binop_ops() */
62 * returns whether a node is constant ie is a constant or
63 * is loop invariant (called region constant)
65 * @param n the node to be checked for constant
66 * @param block a block that might be in a loop
68 static const_class_t get_const_class(const ir_node *n, const ir_node *block)
73 /* constant nodes which can't be folded are region constants */
74 if (is_irn_constlike(n))
78 * Beware: Bad nodes are always loop-invariant, but
79 * cannot handled in later code, so filter them here.
81 if (! is_Bad(n) && is_loop_invariant(n, block))
85 } /* get_const_class */
88 * returns the operands of a commutative bin-op, if one operand is
89 * a region constant, it is returned as the second one.
91 * Beware: Real constants must be returned with higher priority than
92 * region constants, because they might be folded.
94 static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
96 ir_node *op_a = get_binop_left(binop);
97 ir_node *op_b = get_binop_right(binop);
98 ir_node *block = get_nodes_block(binop);
99 int class_a = get_const_class(op_a, block);
100 int class_b = get_const_class(op_b, block);
102 assert(is_op_commutative(get_irn_op(binop)));
104 switch (class_a + 2*class_b) {
105 case REAL_CONSTANT + 2*REAL_CONSTANT:
106 /* if both are constants, one might be a
107 * pointer constant like NULL, return the other
109 if (mode_is_reference(get_irn_mode(op_a))) {
117 case REAL_CONSTANT + 2*NO_CONSTANT:
118 case REAL_CONSTANT + 2*REGION_CONST:
119 case REGION_CONST + 2*NO_CONSTANT:
128 } /* get_comm_Binop_ops */
131 * reassociate a Sub: x - c = x + (-c)
133 static int reassoc_Sub(ir_node **in)
136 ir_node *right = get_Sub_right(n);
137 ir_mode *rmode = get_irn_mode(right);
140 /* cannot handle SubIs(P, P) */
141 if (mode_is_reference(rmode))
144 block = get_nodes_block(n);
147 * convert x - c => x + (-c)
149 if (get_const_class(right, block) == REAL_CONSTANT) {
150 ir_node *left = get_Sub_left(n);
155 switch (get_const_class(left, block)) {
157 irn = optimize_in_place(n);
167 /* already constant, nothing to do */
171 mode = get_irn_mode(n);
172 dbi = get_irn_dbg_info(n);
174 /* Beware of SubP(P, Is) */
175 irn = new_rd_Minus(dbi, block, right, rmode);
176 irn = new_rd_Add(dbi, block, left, irn, mode);
178 DBG((dbg, LEVEL_5, "Applied: %n - %n => %n + (-%n)\n",
179 get_Sub_left(n), right, get_Sub_left(n), right));
192 /** Retrieve a mode from the operands. We need this, because
193 * Add and Sub are allowed to operate on (P, Is)
195 static ir_mode *get_mode_from_ops(ir_node *op1, ir_node *op2)
199 m1 = get_irn_mode(op1);
200 if (mode_is_reference(m1))
203 m2 = get_irn_mode(op2);
204 if (mode_is_reference(m2))
210 } /* get_mode_from_ops */
215 * reassociate a commutative Binop
217 * BEWARE: this rule leads to a potential loop, if
218 * two operands are region constants and the third is a
219 * constant, so avoid this situation.
221 static int reassoc_commutative(ir_node **node)
224 ir_op *op = get_irn_op(n);
225 ir_node *block = get_nodes_block(n);
228 get_comm_Binop_ops(n, &t1, &c1);
230 if (get_irn_op(t1) == op) {
232 const_class_t c_c1, c_c2, c_t2;
234 get_comm_Binop_ops(t1, &t2, &c2);
236 /* do not optimize Bad nodes, will fail later */
240 c_c1 = get_const_class(c1, block);
241 c_c2 = get_const_class(c2, block);
242 c_t2 = get_const_class(t2, block);
244 if ( ((c_c1 > NO_CONSTANT) & (c_t2 > NO_CONSTANT)) &&
245 ((((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0) || ((c_c1 & c_c2 & c_t2) == REGION_CONST)) ) {
246 /* All three are constant and either all are constant expressions
247 * or two of them are:
248 * then applying this rule would lead into a cycle
250 * Note that if t2 is a constant so is c2 hence we save one test.
255 if ((c_c1 != NO_CONSTANT) /* & (c_c2 != NO_CONSTANT) */) {
256 /* handles rules R7, R8, R9, R10:
257 * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
259 ir_node *irn, *in[2];
260 ir_mode *mode, *mode_c1 = get_irn_mode(c1), *mode_c2 = get_irn_mode(c2);
261 ir_graph *irg = get_irn_irg(c1);
263 /* It might happen, that c1 and c2 have different modes, for
264 * instance Is and Iu.
267 if (mode_c1 != mode_c2) {
268 if (mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
269 /* get the bigger one */
270 if (get_mode_size_bits(mode_c1) > get_mode_size_bits(mode_c2))
271 c2 = new_r_Conv(block, c2, mode_c1);
272 else if (get_mode_size_bits(mode_c1) < get_mode_size_bits(mode_c2))
273 c1 = new_r_Conv(block, c1, mode_c2);
275 /* Try to cast the real const */
276 if (c_c1 == REAL_CONSTANT)
277 c1 = new_r_Conv(block, c1, mode_c2);
279 c2 = new_r_Conv(block, c2, mode_c1);
287 mode = get_mode_from_ops(in[0], in[1]);
288 in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
291 mode = get_mode_from_ops(in[0], in[1]);
292 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
294 DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
295 c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
296 t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
298 * In some rare cases it can really happen that we get the same
299 * node back. This might be happen in dead loops, were the Phi
300 * nodes are already gone away. So check this.
309 if (get_irn_op(c1) == op) {
314 if (get_irn_op(t1) == op) {
315 ir_node *l = get_binop_left(t1);
316 ir_node *r = get_binop_right(t1);
324 c_r = get_const_class(r, block);
325 if (c_r != NO_CONSTANT) {
327 * Beware: don't do the following op if a constant was
328 * placed below, else we will fall into a loop.
334 /* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
335 ir_mode *mode_res = get_irn_mode(n);
336 ir_mode *mode_c1 = get_irn_mode(c1);
337 ir_graph *irg = get_irn_irg(c1);
338 ir_node *irn, *in[2];
343 in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode_c1, 2, in));
346 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode_res, 2, in));
348 DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
349 c1, get_irn_opname(n), l, get_irn_opname(n), r,
350 r, get_irn_opname(n), c1, get_irn_opname(n), c1));
360 } /* reassoc_commutative */
364 static ir_op *commutative_op;
365 static ir_node *commutative_block;
366 static struct obstack commutative_args;
368 static void collect_args(ir_node *node)
370 ir_node *left = get_binop_left(node);
371 ir_node *right = get_binop_right(node);
373 if (get_irn_op(left) == commutative_op
374 && (!get_irn_outs_computed(left) || get_irn_n_outs(left) == 1)) {
377 obstack_ptr_grow(&commutative_args, left);
380 if (get_irn_op(right) == commutative_op
381 && (!get_irn_outs_computed(right) || get_irn_n_outs(right) == 1)) {
384 obstack_ptr_grow(&commutative_args, right);
389 ir_mode *mode = get_irn_mode(node);
390 if (is_Add(node) && mode_is_reference(mode)) {
391 assert(get_irn_mode(left) == mode || get_irn_mode(right) == mode);
393 assert(get_irn_mode(left) == mode);
394 assert(get_irn_mode(right) == mode);
400 static int compare_nodes(const ir_node *node1, const ir_node *node2)
402 const_class_t class1 = get_const_class(node1, commutative_block);
403 const_class_t class2 = get_const_class(node2, commutative_block);
405 if (class1 == class2)
407 // return get_irn_idx(node1) - get_irn_idx(node2);
412 assert(class1 > class2);
416 static int compare_node_ptr(const void *e1, const void *e2)
418 const ir_node *node1 = *((const ir_node *const*) e1);
419 const ir_node *node2 = *((const ir_node *const*) e2);
420 return compare_nodes(node1, node2);
423 static int reassoc_commutative(ir_node **n)
432 commutative_op = get_irn_op(node);
433 commutative_block = get_nodes_block(node);
435 /* collect all nodes with same op type */
438 n_args = obstack_object_size(&commutative_args) / sizeof(ir_node*);
439 args = obstack_finish(&commutative_args);
441 /* shortcut: in most cases there's nothing to do */
442 if (n_args == 2 && compare_nodes(args[0], args[1]) <= 0) {
443 obstack_free(&commutative_args, args);
447 /* sort the arguments */
448 qsort(args, n_args, sizeof(ir_node*), compare_node_ptr);
451 last = args[n_args-1];
452 mode = get_irn_mode(last);
453 for (i = n_args-2; i >= 0; --i) {
457 ir_graph *irg = get_irn_irg(last);
462 /* AddP violates the assumption that all modes in args are equal...
463 * we need some hacks to cope with this */
464 mode_right = get_irn_mode(in[1]);
465 if (mode_is_reference(mode_right)) {
466 assert(is_Add(node) && mode_is_reference(get_irn_mode(node)));
467 mode = get_irn_mode(in[1]);
469 if (mode_right != mode) {
470 assert(is_Add(node) && mode_is_reference(get_irn_mode(node)));
471 in[1] = new_r_Conv(irg, commutative_block,in[1], mode);
474 /* TODO: produce useful debug info! */
475 new_node = new_ir_node(NULL, irg, commutative_block,
476 commutative_op, mode, 2, in);
477 new_node = optimize_node(new_node);
481 /* CSE often returns the old node again, only exchange if needed */
483 exchange(node, last);
492 #define reassoc_Add reassoc_commutative
493 #define reassoc_And reassoc_commutative
494 #define reassoc_Or reassoc_commutative
495 #define reassoc_Eor reassoc_commutative
498 * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
500 static int reassoc_Mul(ir_node **node)
503 ir_node *add_sub, *c;
506 if (reassoc_commutative(&n))
509 get_comm_Binop_ops(n, &add_sub, &c);
510 op = get_irn_op(add_sub);
512 /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
513 if (op == op_Add || op == op_Sub) {
514 ir_mode *mode = get_irn_mode(n);
515 ir_node *irn, *block, *t1, *t2, *in[2];
517 block = get_nodes_block(n);
518 t1 = get_binop_left(add_sub);
519 t2 = get_binop_right(add_sub);
521 /* we can only multiplication rules on integer arithmetic */
522 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
523 ir_graph *irg = get_irn_irg(t1);
524 in[0] = new_rd_Mul(NULL, block, c, t1, mode);
525 in[1] = new_rd_Mul(NULL, block, c, t2, mode);
527 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
529 /* In some cases it might happen that the new irn is equal the old one, for
531 * (x - 1) * y == x * y - y
532 * will be transformed back by simpler optimization
533 * We could switch simple optimizations off, but this only happens iff y
534 * is a loop-invariant expression and that it is not clear if the new form
536 * So, we let the old one.
539 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
540 t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
552 * Reassociate Shl. We transform Shl(x, const) into Mul's if possible.
554 static int reassoc_Shl(ir_node **node)
557 ir_node *c = get_Shl_right(n);
558 ir_node *x, *blk, *irn;
567 mode = get_irn_mode(x);
569 tv = get_mode_one(mode);
570 tv = tarval_shl(tv, get_Const_tarval(c));
572 if (tv == tarval_bad)
575 blk = get_nodes_block(n);
576 irg = get_irn_irg(blk);
577 c = new_r_Const(irg, tv);
578 irn = new_rd_Mul(get_irn_dbg_info(n), blk, x, c, mode);
589 * The walker for the reassociation.
591 static void wq_walker(ir_node *n, void *env)
593 walker_t *wenv = (walker_t*)env;
595 set_irn_link(n, NULL);
597 waitq_put(wenv->wq, n);
598 set_irn_link(n, wenv->wq);
603 * The walker for the reassociation.
605 static void do_reassociation(walker_t *wenv)
610 while (! waitq_empty(wenv->wq)) {
611 n = (ir_node*)waitq_get(wenv->wq);
612 set_irn_link(n, NULL);
616 /* reassociation must run until a fixpoint is reached. */
619 ir_op *op = get_irn_op(n);
620 ir_mode *mode = get_irn_mode(n);
624 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
625 if (mode_is_float(mode) && get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic)
628 if (op->ops.reassociate) {
629 res = op->ops.reassociate(&n);
637 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
638 ir_node *pred = get_irn_n(n, i);
640 if (get_irn_link(pred) != wenv->wq) {
641 waitq_put(wenv->wq, pred);
642 set_irn_link(pred, wenv->wq);
647 } /* do_reassociation */
650 * Returns the earliest were a,b are available.
651 * Note that we know that a, b both dominate
652 * the block of the previous operation, so one must dominate the other.
654 * If the earliest block is the start block, return curr_blk instead
656 static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
658 ir_node *blk_a = get_nodes_block(a);
659 ir_node *blk_b = get_nodes_block(b);
662 /* if blk_a != blk_b, one must dominate the other */
663 if (block_dominates(blk_a, blk_b))
667 if (res == get_irg_start_block(get_irn_irg(curr_blk)))
670 } /* earliest_block */
673 * Checks whether a node is a Constant expression.
674 * The following trees are constant expressions:
676 * Const, SymConst, Const + SymConst
678 * Handling SymConsts as const might be not a good idea for all
681 static int is_constant_expr(ir_node *irn)
685 switch (get_irn_opcode(irn)) {
690 op = get_irn_op(get_Add_left(irn));
691 if (op != op_Const && op != op_SymConst)
693 op = get_irn_op(get_Add_right(irn));
694 if (op != op_Const && op != op_SymConst)
700 } /* is_constant_expr */
703 * Apply distributive Law for Mul and Add/Sub
705 static int reverse_rule_distributive(ir_node **node)
708 ir_node *left = get_binop_left(n);
709 ir_node *right = get_binop_right(n);
710 ir_node *x, *blk, *curr_blk;
711 ir_node *a, *b, *irn;
716 op = get_irn_op(left);
717 if (op != get_irn_op(right))
721 x = get_Shl_right(left);
723 if (x == get_Shl_right(right)) {
724 /* (a << x) +/- (b << x) ==> (a +/- b) << x */
725 a = get_Shl_left(left);
726 b = get_Shl_left(right);
729 } else if (op == op_Mul) {
730 x = get_Mul_left(left);
732 if (x == get_Mul_left(right)) {
733 /* (x * a) +/- (x * b) ==> (a +/- b) * x */
734 a = get_Mul_right(left);
735 b = get_Mul_right(right);
737 } else if (x == get_Mul_right(right)) {
738 /* (x * a) +/- (b * x) ==> (a +/- b) * x */
739 a = get_Mul_right(left);
740 b = get_Mul_left(right);
744 x = get_Mul_right(left);
746 if (x == get_Mul_right(right)) {
747 /* (a * x) +/- (b * x) ==> (a +/- b) * x */
748 a = get_Mul_left(left);
749 b = get_Mul_left(right);
751 } else if (x == get_Mul_left(right)) {
752 /* (a * x) +/- (x * b) ==> (a +/- b) * x */
753 a = get_Mul_left(left);
754 b = get_Mul_right(right);
761 curr_blk = get_nodes_block(n);
763 blk = earliest_block(a, b, curr_blk);
765 dbg = get_irn_dbg_info(n);
766 mode = get_irn_mode(n);
769 irn = new_rd_Add(dbg, blk, a, b, mode);
771 irn = new_rd_Sub(dbg, blk, a, b, mode);
773 blk = earliest_block(irn, x, curr_blk);
776 irn = new_rd_Mul(dbg, blk, irn, x, mode);
778 irn = new_rd_Shl(dbg, blk, irn, x, mode);
783 } /* reverse_rule_distributive */
786 * Move Constants towards the root.
788 static int move_consts_up(ir_node **node)
792 ir_node *l, *r, *a, *b, *c, *blk, *irn, *in[2];
793 ir_mode *mode, *ma, *mb;
797 l = get_binop_left(n);
798 r = get_binop_right(n);
800 /* check if one is already a constant expression */
801 if (is_constant_expr(l) || is_constant_expr(r))
804 dbg = get_irn_dbg_info(n);
806 if (get_irn_op(l) == op) {
807 /* (a .op. b) .op. r */
808 a = get_binop_left(l);
809 b = get_binop_right(l);
811 if (is_constant_expr(a)) {
812 /* (C .op. b) .op. r ==> (r .op. b) .op. C */
815 blk = get_nodes_block(l);
816 dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
818 } else if (is_constant_expr(b)) {
819 /* (a .op. C) .op. r ==> (a .op. r) .op. C */
822 blk = get_nodes_block(l);
823 dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
827 if (get_irn_op(r) == op) {
828 /* l .op. (a .op. b) */
829 a = get_binop_left(r);
830 b = get_binop_right(r);
832 if (is_constant_expr(a)) {
833 /* l .op. (C .op. b) ==> (l .op. b) .op. C */
836 blk = get_nodes_block(r);
837 dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
839 } else if (is_constant_expr(b)) {
840 /* l .op. (a .op. C) ==> (a .op. l) .op. C */
843 blk = get_nodes_block(r);
844 dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
851 /* In some cases a and b might be both of different integer mode, and c a SymConst.
852 * in that case we could either
853 * 1.) cast into unsigned mode
855 * we implement the second here
857 ma = get_irn_mode(a);
858 mb = get_irn_mode(b);
859 if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
862 /* check if (a .op. b) can be calculated in the same block is the old instruction */
863 if (! block_dominates(get_nodes_block(a), blk))
865 if (! block_dominates(get_nodes_block(b), blk))
871 mode = get_mode_from_ops(a, b);
872 irg = get_irn_irg(blk);
873 in[0] = irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
875 /* beware: optimize_node might have changed the opcode, check again */
876 if (is_Add(irn) || is_Sub(irn)) {
877 reverse_rule_distributive(&in[0]);
881 mode = get_mode_from_ops(in[0], in[1]);
882 irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
887 } /* move_consts_up */
890 * Apply the rules in reverse order, removing code that was not collapsed
892 static void reverse_rules(ir_node *node, void *env)
896 ir_graph *irg = get_irn_irg(node);
897 ir_mode *mode = get_irn_mode(node);
900 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
901 if (mode_is_float(mode) && get_irg_fp_model(irg) & fp_strict_algebraic)
905 ir_op *op = get_irn_op(node);
908 if (is_op_commutative(op)) {
909 res = move_consts_up(&node);
911 /* beware: move_consts_up might have changed the opcode, check again */
912 if (is_Add(node) || is_Sub(node)) {
913 res = reverse_rule_distributive(&node);
919 * do the reassociation
921 void optimize_reassociation(ir_graph *irg)
925 assert(get_irg_phase_state(irg) != phase_building);
926 assert(get_irg_pinned(irg) != op_pin_state_floats &&
927 "Reassociation needs pinned graph to work properly");
929 assure_irg_properties(irg,
930 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
931 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
934 assire_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
935 obstack_init(&commutative_args);
938 env.wq = new_waitq();
940 /* disable some optimizations while reassoc is running to prevent endless loops */
941 set_reassoc_running(1);
943 /* now we have collected enough information, optimize */
944 irg_walk_graph(irg, NULL, wq_walker, &env);
945 do_reassociation(&env);
947 /* reverse those rules that do not result in collapsed constants */
948 irg_walk_graph(irg, NULL, reverse_rules, NULL);
950 set_reassoc_running(0);
953 obstack_free(&commutative_args, NULL);
958 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
959 } /* optimize_reassociation */
961 /* create a pass for the reassociation */
962 ir_graph_pass_t *optimize_reassociation_pass(const char *name)
964 return def_graph_pass(name ? name : "reassoc", optimize_reassociation);
965 } /* optimize_reassociation_pass */
967 static void register_node_reassoc_func(ir_op *op, reassociate_func func)
969 op->ops.reassociate = func;
972 void ir_register_reassoc_node_ops(void)
974 register_node_reassoc_func(op_Mul, reassoc_Mul);
975 register_node_reassoc_func(op_Add, reassoc_Add);
976 register_node_reassoc_func(op_Sub, reassoc_Sub);
977 register_node_reassoc_func(op_And, reassoc_And);
978 register_node_reassoc_func(op_Or, reassoc_Or);
979 register_node_reassoc_func(op_Eor, reassoc_Eor);
980 register_node_reassoc_func(op_Shl, reassoc_Shl);
983 /* initialize the reassociation by adding operations to some opcodes */
984 void firm_init_reassociation(void)
986 FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");
987 } /* firm_init_reassociation */