2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
13 #include "iroptimize.h"
16 #include "irgraph_t.h"
20 #include "iropt_dbg.h"
24 #include "reassoc_t.h"
32 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
35 NO_CONSTANT = 0, /**< node is not constant */
36 REAL_CONSTANT = 1, /**< node is a Const that is suitable for constant folding */
37 REGION_CONST = 4 /**< node is a constant expression in the current context,
38 use 4 here to simplify implementation of get_comm_Binop_ops() */
42 * returns whether a node is constant ie is a constant or
43 * is loop invariant (called region constant)
45 * @param n the node to be checked for constant
46 * @param block a block that might be in a loop
48 static const_class_t get_const_class(const ir_node *n, const ir_node *block)
53 /* constant nodes which can't be folded are region constants */
54 if (is_irn_constlike(n))
58 * Beware: Bad nodes are always loop-invariant, but
59 * cannot handled in later code, so filter them here.
61 if (! is_Bad(n) && is_loop_invariant(n, block))
68 * returns the operands of a commutative bin-op, if one operand is
69 * a region constant, it is returned as the second one.
71 * Beware: Real constants must be returned with higher priority than
72 * region constants, because they might be folded.
74 static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
76 ir_node *op_a = get_binop_left(binop);
77 ir_node *op_b = get_binop_right(binop);
78 ir_node *block = get_nodes_block(binop);
79 int class_a = get_const_class(op_a, block);
80 int class_b = get_const_class(op_b, block);
82 assert(is_op_commutative(get_irn_op(binop)));
84 switch (class_a + 2*class_b) {
85 case REAL_CONSTANT + 2*REAL_CONSTANT:
86 /* if both are constants, one might be a
87 * pointer constant like NULL, return the other
89 if (mode_is_reference(get_irn_mode(op_a))) {
97 case REAL_CONSTANT + 2*NO_CONSTANT:
98 case REAL_CONSTANT + 2*REGION_CONST:
99 case REGION_CONST + 2*NO_CONSTANT:
111 * reassociate a Sub: x - c = x + (-c)
113 static int reassoc_Sub(ir_node **in)
116 ir_node *right = get_Sub_right(n);
117 ir_mode *rmode = get_irn_mode(right);
120 /* cannot handle SubIs(P, P) */
121 if (mode_is_reference(rmode))
124 block = get_nodes_block(n);
127 * convert x - c => x + (-c)
129 if (get_const_class(right, block) == REAL_CONSTANT) {
130 ir_node *left = get_Sub_left(n);
135 switch (get_const_class(left, block)) {
137 irn = optimize_in_place(n);
147 /* already constant, nothing to do */
151 mode = get_irn_mode(n);
152 dbi = get_irn_dbg_info(n);
154 /* Beware of SubP(P, Is) */
155 irn = new_rd_Minus(dbi, block, right, rmode);
156 irn = new_rd_Add(dbi, block, left, irn, mode);
158 DBG((dbg, LEVEL_5, "Applied: %n - %n => %n + (-%n)\n",
159 get_Sub_left(n), right, get_Sub_left(n), right));
172 /** Retrieve a mode from the operands. We need this, because
173 * Add and Sub are allowed to operate on (P, Is)
175 static ir_mode *get_mode_from_ops(ir_node *op1, ir_node *op2)
179 m1 = get_irn_mode(op1);
180 if (mode_is_reference(m1))
183 m2 = get_irn_mode(op2);
184 if (mode_is_reference(m2))
193 * reassociate a commutative Binop
195 * BEWARE: this rule leads to a potential loop, if
196 * two operands are region constants and the third is a
197 * constant, so avoid this situation.
199 static int reassoc_commutative(ir_node **node)
202 ir_op *op = get_irn_op(n);
203 ir_node *block = get_nodes_block(n);
206 get_comm_Binop_ops(n, &t1, &c1);
208 if (get_irn_op(t1) == op) {
210 const_class_t c_c1, c_c2, c_t2;
212 get_comm_Binop_ops(t1, &t2, &c2);
214 /* do not optimize Bad nodes, will fail later */
218 c_c1 = get_const_class(c1, block);
219 c_c2 = get_const_class(c2, block);
220 c_t2 = get_const_class(t2, block);
222 if ( ((c_c1 > NO_CONSTANT) & (c_t2 > NO_CONSTANT)) &&
223 ((((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0) || ((c_c1 & c_c2 & c_t2) == REGION_CONST)) ) {
224 /* All three are constant and either all are constant expressions
225 * or two of them are:
226 * then applying this rule would lead into a cycle
228 * Note that if t2 is a constant so is c2 hence we save one test.
233 if ((c_c1 != NO_CONSTANT) /* & (c_c2 != NO_CONSTANT) */) {
234 /* handles rules R7, R8, R9, R10:
235 * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
237 ir_node *irn, *in[2];
238 ir_mode *mode, *mode_c1 = get_irn_mode(c1), *mode_c2 = get_irn_mode(c2);
239 ir_graph *irg = get_irn_irg(c1);
241 /* It might happen, that c1 and c2 have different modes, for
242 * instance Is and Iu.
245 if (mode_c1 != mode_c2) {
246 if (mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
247 /* get the bigger one */
248 if (get_mode_size_bits(mode_c1) > get_mode_size_bits(mode_c2))
249 c2 = new_r_Conv(block, c2, mode_c1);
250 else if (get_mode_size_bits(mode_c1) < get_mode_size_bits(mode_c2))
251 c1 = new_r_Conv(block, c1, mode_c2);
253 /* Try to cast the real const */
254 if (c_c1 == REAL_CONSTANT)
255 c1 = new_r_Conv(block, c1, mode_c2);
257 c2 = new_r_Conv(block, c2, mode_c1);
265 mode = get_mode_from_ops(in[0], in[1]);
266 in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
269 mode = get_mode_from_ops(in[0], in[1]);
270 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
272 DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
273 c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
274 t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
276 * In some rare cases it can really happen that we get the same
277 * node back. This might be happen in dead loops, were the Phi
278 * nodes are already gone away. So check this.
287 if (get_irn_op(c1) == op) {
292 if (get_irn_op(t1) == op) {
293 ir_node *l = get_binop_left(t1);
294 ir_node *r = get_binop_right(t1);
302 c_r = get_const_class(r, block);
303 if (c_r != NO_CONSTANT) {
305 * Beware: don't do the following op if a constant was
306 * placed below, else we will fall into a loop.
312 /* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
313 ir_mode *mode_res = get_irn_mode(n);
314 ir_mode *mode_c1 = get_irn_mode(c1);
315 ir_graph *irg = get_irn_irg(c1);
316 ir_node *irn, *in[2];
321 in[1] = optimize_node(new_ir_node(NULL, irg, block, op, mode_c1, 2, in));
324 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode_res, 2, in));
326 DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
327 c1, get_irn_opname(n), l, get_irn_opname(n), r,
328 r, get_irn_opname(n), c1, get_irn_opname(n), c1));
341 * Reassociate using commutative law for Mul and distributive law for Mul and Add/Sub:
343 static int reassoc_Mul(ir_node **node)
346 ir_node *add_sub, *c;
349 if (reassoc_commutative(&n))
352 get_comm_Binop_ops(n, &add_sub, &c);
353 op = get_irn_op(add_sub);
355 /* handles rules R11, R12, R13, R14, R15, R16, R17, R18, R19, R20 */
356 if (op == op_Add || op == op_Sub) {
357 ir_mode *mode = get_irn_mode(n);
358 ir_node *irn, *block, *t1, *t2, *in[2];
360 block = get_nodes_block(n);
361 t1 = get_binop_left(add_sub);
362 t2 = get_binop_right(add_sub);
364 /* we can only multiplication rules on integer arithmetic */
365 if (mode_is_int(get_irn_mode(t1)) && mode_is_int(get_irn_mode(t2))) {
366 ir_graph *irg = get_irn_irg(t1);
367 in[0] = new_rd_Mul(NULL, block, c, t1, mode);
368 in[1] = new_rd_Mul(NULL, block, c, t2, mode);
370 irn = optimize_node(new_ir_node(NULL, irg, block, op, mode, 2, in));
372 /* In some cases it might happen that the new irn is equal the old one, for
374 * (x - 1) * y == x * y - y
375 * will be transformed back by simpler optimization
376 * We could switch simple optimizations off, but this only happens iff y
377 * is a loop-invariant expression and that it is not clear if the new form
379 * So, we let the old one.
382 DBG((dbg, LEVEL_5, "Applied: (%n .%s. %n) %n %n => (%n %n %n) .%s. (%n %n %n)\n",
383 t1, get_op_name(op), t2, n, c, t1, n, c, get_op_name(op), t2, n, c));
395 * Reassociate Shl. We transform Shl(x, const) into Mul's if possible.
397 static int reassoc_Shl(ir_node **node)
400 ir_node *c = get_Shl_right(n);
401 ir_node *x, *blk, *irn;
410 mode = get_irn_mode(x);
412 tv = get_mode_one(mode);
413 tv = tarval_shl(tv, get_Const_tarval(c));
415 if (tv == tarval_bad)
418 blk = get_nodes_block(n);
419 irg = get_irn_irg(blk);
420 c = new_r_Const(irg, tv);
421 irn = new_rd_Mul(get_irn_dbg_info(n), blk, x, c, mode);
432 * The walker for the reassociation.
434 static void wq_walker(ir_node *n, void *env)
436 waitq *const wq = (waitq*)env;
438 set_irn_link(n, NULL);
446 * The walker for the reassociation.
448 static void do_reassociation(waitq *const wq)
453 while (! waitq_empty(wq)) {
454 n = (ir_node*)waitq_get(wq);
455 set_irn_link(n, NULL);
459 /* reassociation must run until a fixpoint is reached. */
462 ir_op *op = get_irn_op(n);
463 ir_mode *mode = get_irn_mode(n);
467 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
468 if (mode_is_float(mode) && get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic)
471 if (op->ops.reassociate) {
472 res = op->ops.reassociate(&n);
480 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
481 ir_node *pred = get_irn_n(n, i);
483 if (get_irn_link(pred) != wq) {
485 set_irn_link(pred, wq);
493 * Returns the earliest were a,b are available.
494 * Note that we know that a, b both dominate
495 * the block of the previous operation, so one must dominate the other.
497 * If the earliest block is the start block, return curr_blk instead
499 static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
501 ir_node *blk_a = get_nodes_block(a);
502 ir_node *blk_b = get_nodes_block(b);
505 /* if blk_a != blk_b, one must dominate the other */
506 if (block_dominates(blk_a, blk_b))
510 if (res == get_irg_start_block(get_irn_irg(curr_blk)))
516 * Checks whether a node is a Constant expression.
517 * The following trees are constant expressions:
519 * Const, SymConst, Const + SymConst
521 * Handling SymConsts as const might be not a good idea for all
524 static int is_constant_expr(ir_node *irn)
526 switch (get_irn_opcode(irn)) {
532 ir_node *const l = get_Add_left(irn);
533 if (!is_Const(l) && !is_SymConst(l))
535 ir_node *const r = get_Add_right(irn);
536 if (!is_Const(r) && !is_SymConst(r))
547 * Apply distributive Law for Mul and Add/Sub
549 static int reverse_rule_distributive(ir_node **node)
552 ir_node *left = get_binop_left(n);
553 ir_node *right = get_binop_right(n);
554 ir_node *x, *blk, *curr_blk;
555 ir_node *a, *b, *irn;
560 op = get_irn_op(left);
561 if (op != get_irn_op(right))
565 x = get_Shl_right(left);
567 if (x == get_Shl_right(right)) {
568 /* (a << x) +/- (b << x) ==> (a +/- b) << x */
569 a = get_Shl_left(left);
570 b = get_Shl_left(right);
573 } else if (op == op_Mul) {
574 x = get_Mul_left(left);
576 if (x == get_Mul_left(right)) {
577 /* (x * a) +/- (x * b) ==> (a +/- b) * x */
578 a = get_Mul_right(left);
579 b = get_Mul_right(right);
581 } else if (x == get_Mul_right(right)) {
582 /* (x * a) +/- (b * x) ==> (a +/- b) * x */
583 a = get_Mul_right(left);
584 b = get_Mul_left(right);
588 x = get_Mul_right(left);
590 if (x == get_Mul_right(right)) {
591 /* (a * x) +/- (b * x) ==> (a +/- b) * x */
592 a = get_Mul_left(left);
593 b = get_Mul_left(right);
595 } else if (x == get_Mul_left(right)) {
596 /* (a * x) +/- (x * b) ==> (a +/- b) * x */
597 a = get_Mul_left(left);
598 b = get_Mul_right(right);
605 curr_blk = get_nodes_block(n);
607 blk = earliest_block(a, b, curr_blk);
609 dbg = get_irn_dbg_info(n);
610 mode = get_irn_mode(n);
613 irn = new_rd_Add(dbg, blk, a, b, mode);
615 irn = new_rd_Sub(dbg, blk, a, b, mode);
617 blk = earliest_block(irn, x, curr_blk);
620 irn = new_rd_Mul(dbg, blk, irn, x, mode);
622 irn = new_rd_Shl(dbg, blk, irn, x, mode);
630 * Move Constants towards the root.
632 static int move_consts_up(ir_node **node)
636 ir_node *l, *r, *a, *b, *c, *blk, *irn, *in[2];
637 ir_mode *mode, *ma, *mb;
641 l = get_binop_left(n);
642 r = get_binop_right(n);
644 /* check if one is already a constant expression */
645 if (is_constant_expr(l) || is_constant_expr(r))
648 dbg = get_irn_dbg_info(n);
650 if (get_irn_op(l) == op) {
651 /* (a .op. b) .op. r */
652 a = get_binop_left(l);
653 b = get_binop_right(l);
655 if (is_constant_expr(a)) {
656 /* (C .op. b) .op. r ==> (r .op. b) .op. C */
659 blk = get_nodes_block(l);
660 dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
662 } else if (is_constant_expr(b)) {
663 /* (a .op. C) .op. r ==> (a .op. r) .op. C */
666 blk = get_nodes_block(l);
667 dbg = dbg == get_irn_dbg_info(l) ? dbg : NULL;
671 if (get_irn_op(r) == op) {
672 /* l .op. (a .op. b) */
673 a = get_binop_left(r);
674 b = get_binop_right(r);
676 if (is_constant_expr(a)) {
677 /* l .op. (C .op. b) ==> (l .op. b) .op. C */
680 blk = get_nodes_block(r);
681 dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
683 } else if (is_constant_expr(b)) {
684 /* l .op. (a .op. C) ==> (a .op. l) .op. C */
687 blk = get_nodes_block(r);
688 dbg = dbg == get_irn_dbg_info(r) ? dbg : NULL;
695 /* In some cases a and b might be both of different integer mode, and c a SymConst.
696 * in that case we could either
697 * 1.) cast into unsigned mode
699 * we implement the second here
701 ma = get_irn_mode(a);
702 mb = get_irn_mode(b);
703 if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
706 /* check if (a .op. b) can be calculated in the same block is the old instruction */
707 if (! block_dominates(get_nodes_block(a), blk))
709 if (! block_dominates(get_nodes_block(b), blk))
715 mode = get_mode_from_ops(a, b);
716 irg = get_irn_irg(blk);
717 in[0] = irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
719 /* beware: optimize_node might have changed the opcode, check again */
720 if (is_Add(irn) || is_Sub(irn)) {
721 reverse_rule_distributive(&in[0]);
725 mode = get_mode_from_ops(in[0], in[1]);
726 irn = optimize_node(new_ir_node(dbg, irg, blk, op, mode, 2, in));
734 * Apply the rules in reverse order, removing code that was not collapsed
736 static void reverse_rules(ir_node *node, void *env)
740 ir_graph *irg = get_irn_irg(node);
741 ir_mode *mode = get_irn_mode(node);
744 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
745 if (mode_is_float(mode) && get_irg_fp_model(irg) & fp_strict_algebraic)
749 ir_op *op = get_irn_op(node);
752 if (is_op_commutative(op)) {
753 res = move_consts_up(&node);
755 /* beware: move_consts_up might have changed the opcode, check again */
756 if (is_Add(node) || is_Sub(node)) {
757 res = reverse_rule_distributive(&node);
763 * do the reassociation
765 void optimize_reassociation(ir_graph *irg)
767 assert(get_irg_pinned(irg) != op_pin_state_floats &&
768 "Reassociation needs pinned graph to work properly");
770 assure_irg_properties(irg,
771 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
772 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
774 waitq *const wq = new_waitq();
776 /* disable some optimizations while reassoc is running to prevent endless loops */
777 set_reassoc_running(1);
779 /* now we have collected enough information, optimize */
780 irg_walk_graph(irg, NULL, wq_walker, wq);
781 do_reassociation(wq);
783 /* reverse those rules that do not result in collapsed constants */
784 irg_walk_graph(irg, NULL, reverse_rules, NULL);
786 set_reassoc_running(0);
790 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
793 /* create a pass for the reassociation */
794 ir_graph_pass_t *optimize_reassociation_pass(const char *name)
796 return def_graph_pass(name ? name : "reassoc", optimize_reassociation);
799 static void register_node_reassoc_func(ir_op *op, reassociate_func func)
801 op->ops.reassociate = func;
804 void ir_register_reassoc_node_ops(void)
806 register_node_reassoc_func(op_Add, reassoc_commutative);
807 register_node_reassoc_func(op_And, reassoc_commutative);
808 register_node_reassoc_func(op_Eor, reassoc_commutative);
809 register_node_reassoc_func(op_Mul, reassoc_Mul);
810 register_node_reassoc_func(op_Or, reassoc_commutative);
811 register_node_reassoc_func(op_Sub, reassoc_Sub);
812 register_node_reassoc_func(op_Shl, reassoc_Shl);
815 /* initialize the reassociation by adding operations to some opcodes */
816 void firm_init_reassociation(void)
818 FIRM_DBG_REGISTER(dbg, "firm.opt.reassoc");