2 * Copyright (C) 1995-2011 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 iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
50 #include "firm_types.h"
51 #include "bitfiddle.h"
56 static bool is_Or_Eor_Add(const ir_node *node)
58 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
59 ir_node *left = get_binop_left(node);
60 ir_node *right = get_binop_right(node);
61 vrp_attr *vrp_left = vrp_get_info(left);
62 vrp_attr *vrp_right = vrp_get_info(right);
63 if (vrp_left != NULL && vrp_right != NULL) {
65 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
66 return tarval_is_null(vrp_val);
73 * Returns the tarval of a Const node or tarval_bad for all other nodes.
75 static ir_tarval *default_value_of(const ir_node *n)
78 return get_Const_tarval(n); /* might return tarval_bad */
83 value_of_func value_of_ptr = default_value_of;
85 void set_value_of_func(value_of_func func)
90 value_of_ptr = default_value_of;
94 * Return the value of a Constant.
96 static ir_tarval *computed_value_Const(const ir_node *n)
98 return get_Const_tarval(n);
102 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
104 static ir_tarval *computed_value_SymConst(const ir_node *n)
109 switch (get_SymConst_kind(n)) {
110 case symconst_type_size:
111 type = get_SymConst_type(n);
112 if (get_type_state(type) == layout_fixed)
113 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
115 case symconst_type_align:
116 type = get_SymConst_type(n);
117 if (get_type_state(type) == layout_fixed)
118 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
120 case symconst_ofs_ent:
121 ent = get_SymConst_entity(n);
122 type = get_entity_owner(ent);
123 if (get_type_state(type) == layout_fixed)
124 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
133 * Return the value of an Add.
135 static ir_tarval *computed_value_Add(const ir_node *n)
137 ir_node *a = get_Add_left(n);
138 ir_node *b = get_Add_right(n);
140 ir_tarval *ta = value_of(a);
141 ir_tarval *tb = value_of(b);
143 if ((ta != tarval_bad) && (tb != tarval_bad))
144 return tarval_add(ta, tb);
147 if ((is_Not(a) && get_Not_op(a) == b)
148 || (is_Not(b) && get_Not_op(b) == a)) {
149 return get_mode_all_one(get_irn_mode(n));
156 * Return the value of a Sub.
157 * Special case: a - a
159 static ir_tarval *computed_value_Sub(const ir_node *n)
161 ir_mode *mode = get_irn_mode(n);
162 ir_node *a = get_Sub_left(n);
163 ir_node *b = get_Sub_right(n);
168 if (! mode_is_float(mode)) {
171 return get_mode_null(mode);
177 if ((ta != tarval_bad) && (tb != tarval_bad))
178 return tarval_sub(ta, tb, mode);
184 * Return the value of a Carry.
185 * Special : a op 0, 0 op b
187 static ir_tarval *computed_value_Carry(const ir_node *n)
189 ir_node *a = get_binop_left(n);
190 ir_node *b = get_binop_right(n);
191 ir_mode *m = get_irn_mode(n);
192 ir_tarval *ta = value_of(a);
193 ir_tarval *tb = value_of(b);
195 if ((ta != tarval_bad) && (tb != tarval_bad)) {
197 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
199 if (tarval_is_null(ta) || tarval_is_null(tb))
200 return get_mode_null(m);
206 * Return the value of a Borrow.
209 static ir_tarval *computed_value_Borrow(const ir_node *n)
211 ir_node *a = get_binop_left(n);
212 ir_node *b = get_binop_right(n);
213 ir_mode *m = get_irn_mode(n);
214 ir_tarval *ta = value_of(a);
215 ir_tarval *tb = value_of(b);
217 if ((ta != tarval_bad) && (tb != tarval_bad)) {
218 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
219 } else if (tarval_is_null(ta)) {
220 return get_mode_null(m);
226 * Return the value of an unary Minus.
228 static ir_tarval *computed_value_Minus(const ir_node *n)
230 ir_node *a = get_Minus_op(n);
231 ir_tarval *ta = value_of(a);
233 if (ta != tarval_bad)
234 return tarval_neg(ta);
240 * Return the value of a Mul.
242 static ir_tarval *computed_value_Mul(const ir_node *n)
244 ir_node *a = get_Mul_left(n);
245 ir_node *b = get_Mul_right(n);
246 ir_tarval *ta = value_of(a);
247 ir_tarval *tb = value_of(b);
250 mode = get_irn_mode(n);
251 if (mode != get_irn_mode(a)) {
252 /* n * n = 2n bit multiplication */
253 ta = tarval_convert_to(ta, mode);
254 tb = tarval_convert_to(tb, mode);
257 if (ta != tarval_bad && tb != tarval_bad) {
258 return tarval_mul(ta, tb);
260 /* a * 0 != 0 if a == NaN or a == Inf */
261 if (!mode_is_float(mode)) {
262 /* a*0 = 0 or 0*b = 0 */
263 if (ta == get_mode_null(mode))
265 if (tb == get_mode_null(mode))
273 * Return the value of an And.
274 * Special case: a & 0, 0 & b
276 static ir_tarval *computed_value_And(const ir_node *n)
278 ir_node *a = get_And_left(n);
279 ir_node *b = get_And_right(n);
280 ir_tarval *ta = value_of(a);
281 ir_tarval *tb = value_of(b);
283 if ((ta != tarval_bad) && (tb != tarval_bad)) {
284 return tarval_and (ta, tb);
287 if (tarval_is_null(ta)) return ta;
288 if (tarval_is_null(tb)) return tb;
291 if ((is_Not(a) && get_Not_op(a) == b)
292 || (is_Not(b) && get_Not_op(b) == a)) {
293 return get_mode_null(get_irn_mode(n));
300 * Return the value of an Or.
301 * Special case: a | 1...1, 1...1 | b
303 static ir_tarval *computed_value_Or(const ir_node *n)
305 ir_node *a = get_Or_left(n);
306 ir_node *b = get_Or_right(n);
307 ir_tarval *ta = value_of(a);
308 ir_tarval *tb = value_of(b);
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_or (ta, tb);
314 if (tarval_is_all_one(ta)) return ta;
315 if (tarval_is_all_one(tb)) return tb;
318 if ((is_Not(a) && get_Not_op(a) == b)
319 || (is_Not(b) && get_Not_op(b) == a)) {
320 return get_mode_all_one(get_irn_mode(n));
326 * Return the value of an Eor.
328 static ir_tarval *computed_value_Eor(const ir_node *n)
330 ir_node *a = get_Eor_left(n);
331 ir_node *b = get_Eor_right(n);
336 return get_mode_null(get_irn_mode(n));
338 if ((is_Not(a) && get_Not_op(a) == b)
339 || (is_Not(b) && get_Not_op(b) == a)) {
340 return get_mode_all_one(get_irn_mode(n));
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_eor(ta, tb);
353 * Return the value of a Not.
355 static ir_tarval *computed_value_Not(const ir_node *n)
357 ir_node *a = get_Not_op(n);
358 ir_tarval *ta = value_of(a);
360 if (ta != tarval_bad)
361 return tarval_not(ta);
367 * Tests whether a shift shifts more bits than available in the mode
369 static bool is_oversize_shift(const ir_node *n)
371 ir_node *count = get_binop_right(n);
372 ir_mode *mode = get_irn_mode(n);
373 ir_tarval *tv = value_of(count);
376 if (tv == tarval_bad)
378 if (!tarval_is_long(tv))
380 shiftval = get_tarval_long(tv);
381 modulo_shift = get_mode_modulo_shift(mode);
382 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
385 return shiftval >= (long)get_mode_size_bits(mode);
389 * Return the value of a Shl.
391 static ir_tarval *computed_value_Shl(const ir_node *n)
393 ir_node *a = get_Shl_left(n);
394 ir_node *b = get_Shl_right(n);
396 ir_tarval *ta = value_of(a);
397 ir_tarval *tb = value_of(b);
399 if ((ta != tarval_bad) && (tb != tarval_bad)) {
400 return tarval_shl(ta, tb);
403 if (is_oversize_shift(n))
404 return get_mode_null(get_irn_mode(n));
410 * Return the value of a Shr.
412 static ir_tarval *computed_value_Shr(const ir_node *n)
414 ir_node *a = get_Shr_left(n);
415 ir_node *b = get_Shr_right(n);
417 ir_tarval *ta = value_of(a);
418 ir_tarval *tb = value_of(b);
420 if ((ta != tarval_bad) && (tb != tarval_bad)) {
421 return tarval_shr(ta, tb);
423 if (is_oversize_shift(n))
424 return get_mode_null(get_irn_mode(n));
430 * Return the value of a Shrs.
432 static ir_tarval *computed_value_Shrs(const ir_node *n)
434 ir_node *a = get_Shrs_left(n);
435 ir_node *b = get_Shrs_right(n);
437 ir_tarval *ta = value_of(a);
438 ir_tarval *tb = value_of(b);
440 if ((ta != tarval_bad) && (tb != tarval_bad)) {
441 return tarval_shrs(ta, tb);
447 * Return the value of a Rotl.
449 static ir_tarval *computed_value_Rotl(const ir_node *n)
451 ir_node *a = get_Rotl_left(n);
452 ir_node *b = get_Rotl_right(n);
454 ir_tarval *ta = value_of(a);
455 ir_tarval *tb = value_of(b);
457 if ((ta != tarval_bad) && (tb != tarval_bad)) {
458 return tarval_rotl(ta, tb);
463 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
465 ir_mode *mode = get_irn_mode(node);
466 if (get_mode_arithmetic(mode) != irma_twos_complement
467 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
471 ir_node *count = get_Shl_right(node);
472 if (is_Const(count)) {
473 ir_tarval *tv = get_Const_tarval(count);
474 if (tarval_is_long(tv)) {
475 long shiftval = get_tarval_long(tv);
476 long destbits = get_mode_size_bits(dest_mode);
477 if (shiftval >= destbits
478 && shiftval < (long)get_mode_modulo_shift(mode))
484 ir_node *right = get_And_right(node);
485 if (is_Const(right)) {
486 ir_tarval *tv = get_Const_tarval(right);
487 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
488 return tarval_is_null(conved);
495 * Return the value of a Conv.
497 static ir_tarval *computed_value_Conv(const ir_node *n)
499 ir_node *a = get_Conv_op(n);
500 ir_tarval *ta = value_of(a);
501 ir_mode *mode = get_irn_mode(n);
503 if (ta != tarval_bad)
504 return tarval_convert_to(ta, get_irn_mode(n));
506 if (ir_zero_when_converted(a, mode))
507 return get_mode_null(mode);
513 * Calculate the value of a Mux: can be evaluated, if the
514 * sel and the right input are known.
516 static ir_tarval *computed_value_Mux(const ir_node *n)
518 ir_node *sel = get_Mux_sel(n);
519 ir_tarval *ts = value_of(sel);
521 if (ts == get_tarval_b_true()) {
522 ir_node *v = get_Mux_true(n);
525 else if (ts == get_tarval_b_false()) {
526 ir_node *v = get_Mux_false(n);
533 * Calculate the value of a Confirm: can be evaluated,
534 * if it has the form Confirm(x, '=', Const).
536 static ir_tarval *computed_value_Confirm(const ir_node *n)
538 if (get_Confirm_relation(n) == ir_relation_equal) {
539 ir_tarval *tv = value_of(get_Confirm_bound(n));
540 if (tv != tarval_bad)
543 return value_of(get_Confirm_value(n));
547 * gives a (conservative) estimation of possible relation when comparing
550 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
551 const ir_node *right)
553 ir_relation possible = ir_relation_true;
554 ir_tarval *tv_l = value_of(left);
555 ir_tarval *tv_r = value_of(right);
556 ir_mode *mode = get_irn_mode(left);
557 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
558 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
560 /* both values known - evaluate them */
561 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
562 possible = tarval_cmp(tv_l, tv_r);
563 /* we can return now, won't get any better */
566 /* a == a is never less or greater (but might be equal or unordered) */
568 possible &= ~ir_relation_less_greater;
569 /* unordered results only happen for float compares */
570 if (!mode_is_float(mode))
571 possible &= ~ir_relation_unordered;
572 /* values can never be less than the least representable number or
573 * greater than the greatest representable number */
575 possible &= ~ir_relation_greater;
577 possible &= ~ir_relation_less;
579 possible &= ~ir_relation_greater;
581 possible &= ~ir_relation_less;
582 /* maybe vrp can tell us more */
583 possible &= vrp_cmp(left, right);
584 /* Alloc nodes never return null (but throw an exception) */
585 if (is_Alloc(left) && tarval_is_null(tv_r))
586 possible &= ~ir_relation_equal;
587 /* stuff known through confirm nodes */
588 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
589 possible &= get_Confirm_relation(left);
591 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
592 ir_relation relation = get_Confirm_relation(right);
593 relation = get_inversed_relation(relation);
594 possible &= relation;
600 static ir_tarval *compute_cmp(const ir_node *cmp)
602 ir_node *left = get_Cmp_left(cmp);
603 ir_node *right = get_Cmp_right(cmp);
604 ir_relation possible = ir_get_possible_cmp_relations(left, right);
605 ir_relation relation = get_Cmp_relation(cmp);
607 /* if none of the requested relations is possible, return false */
608 if ((possible & relation) == ir_relation_false)
609 return tarval_b_false;
610 /* if possible relations are a subset of the requested ones return true */
611 if ((possible & ~relation) == ir_relation_false)
612 return tarval_b_true;
614 return computed_value_Cmp_Confirm(cmp, left, right, relation);
618 * some people want to call compute_cmp directly, in this case we have to
619 * test the constant folding flag again
621 static ir_tarval *compute_cmp_ext(const ir_node *cmp)
623 if (!get_opt_constant_folding())
625 return compute_cmp(cmp);
629 * Return the value of a Cmp.
631 * The basic idea here is to determine which relations are possible and which
632 * one are definitely impossible.
634 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
636 /* we can't construct Constb after lowering mode_b nodes */
637 if (irg_is_constrained(get_irn_irg(cmp), IR_GRAPH_CONSTRAINT_MODEB_LOWERED))
640 return compute_cmp(cmp);
644 * Calculate the value of an integer Div.
645 * Special case: 0 / b
647 static ir_tarval *do_computed_value_Div(const ir_node *div)
649 const ir_node *a = get_Div_left(div);
650 const ir_node *b = get_Div_right(div);
651 const ir_mode *mode = get_Div_resmode(div);
652 ir_tarval *ta = value_of(a);
654 const ir_node *dummy;
656 /* cannot optimize 0 / b = 0 because of NaN */
657 if (!mode_is_float(mode)) {
658 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
659 return ta; /* 0 / b == 0 if b != 0 */
662 if (ta != tarval_bad && tb != tarval_bad)
663 return tarval_div(ta, tb);
668 * Calculate the value of an integer Mod of two nodes.
669 * Special case: a % 1
671 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
673 ir_tarval *ta = value_of(a);
674 ir_tarval *tb = value_of(b);
676 /* Compute a % 1 or c1 % c2 */
677 if (tarval_is_one(tb))
678 return get_mode_null(get_irn_mode(a));
679 if (ta != tarval_bad && tb != tarval_bad)
680 return tarval_mod(ta, tb);
685 * Return the value of a Proj(Div).
687 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
689 long proj_nr = get_Proj_proj(n);
690 if (proj_nr != pn_Div_res)
693 return do_computed_value_Div(get_Proj_pred(n));
697 * Return the value of a Proj(Mod).
699 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
701 long proj_nr = get_Proj_proj(n);
703 if (proj_nr == pn_Mod_res) {
704 const ir_node *mod = get_Proj_pred(n);
705 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
711 * Return the value of a Proj.
713 static ir_tarval *computed_value_Proj(const ir_node *proj)
715 ir_node *n = get_Proj_pred(proj);
717 if (n->op->ops.computed_value_Proj != NULL)
718 return n->op->ops.computed_value_Proj(proj);
723 * If the parameter n can be computed, return its value, else tarval_bad.
724 * Performs constant folding.
726 * @param n The node this should be evaluated
728 ir_tarval *computed_value(const ir_node *n)
730 vrp_attr *vrp = vrp_get_info(n);
731 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
732 return vrp->bits_set;
734 if (n->op->ops.computed_value)
735 return n->op->ops.computed_value(n);
740 * Optimize operations that are commutative and have neutral 0,
741 * so a op 0 = 0 op a = a.
743 static ir_node *equivalent_node_neutral_zero(ir_node *n)
747 ir_node *a = get_binop_left(n);
748 ir_node *b = get_binop_right(n);
753 /* After running compute_node there is only one constant predecessor.
754 Find this predecessors value and remember the other node: */
755 if ((tv = value_of(a)) != tarval_bad) {
757 } else if ((tv = value_of(b)) != tarval_bad) {
762 /* If this predecessors constant value is zero, the operation is
763 * unnecessary. Remove it.
765 * Beware: If n is a Add, the mode of on and n might be different
766 * which happens in this rare construction: NULL + 3.
767 * Then, a Conv would be needed which we cannot include here.
769 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
772 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
779 * Eor is commutative and has neutral 0.
781 static ir_node *equivalent_node_Eor(ir_node *n)
787 n = equivalent_node_neutral_zero(n);
788 if (n != oldn) return n;
791 b = get_Eor_right(n);
793 if (is_Eor(a) || is_Or_Eor_Add(a)) {
794 ir_node *aa = get_binop_left(a);
795 ir_node *ab = get_binop_right(a);
798 /* (a ^ b) ^ a -> b */
800 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
802 } else if (ab == b) {
803 /* (a ^ b) ^ b -> a */
805 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
809 if (is_Eor(b) || is_Or_Eor_Add(b)) {
810 ir_node *ba = get_binop_left(b);
811 ir_node *bb = get_binop_right(b);
814 /* a ^ (a ^ b) -> b */
816 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
818 } else if (bb == a) {
819 /* a ^ (b ^ a) -> b */
821 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
829 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
831 * The second one looks strange, but this construct
832 * is used heavily in the LCC sources :-).
834 * Beware: The Mode of an Add may be different than the mode of its
835 * predecessors, so we could not return a predecessors in all cases.
837 static ir_node *equivalent_node_Add(ir_node *n)
840 ir_node *left, *right;
841 ir_mode *mode = get_irn_mode(n);
843 n = equivalent_node_neutral_zero(n);
847 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
848 if (mode_is_float(mode)) {
849 ir_graph *irg = get_irn_irg(n);
850 if (get_irg_fp_model(irg) & fp_strict_algebraic)
854 left = get_Add_left(n);
855 right = get_Add_right(n);
858 if (get_Sub_right(left) == right) {
861 n = get_Sub_left(left);
862 if (mode == get_irn_mode(n)) {
863 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
869 if (get_Sub_right(right) == left) {
872 n = get_Sub_left(right);
873 if (mode == get_irn_mode(n)) {
874 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
883 * optimize operations that are not commutative but have neutral 0 on left,
886 static ir_node *equivalent_node_left_zero(ir_node *n)
890 ir_node *a = get_binop_left(n);
891 ir_node *b = get_binop_right(n);
892 ir_tarval *tb = value_of(b);
894 if (tarval_is_null(tb)) {
897 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
903 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
905 * The second one looks strange, but this construct
906 * is used heavily in the LCC sources :-).
908 * Beware: The Mode of a Sub may be different than the mode of its
909 * predecessors, so we could not return a predecessors in all cases.
911 static ir_node *equivalent_node_Sub(ir_node *n)
915 ir_mode *mode = get_irn_mode(n);
918 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
919 if (mode_is_float(mode)) {
920 ir_graph *irg = get_irn_irg(n);
921 if (get_irg_fp_model(irg) & fp_strict_algebraic)
925 b = get_Sub_right(n);
928 /* Beware: modes might be different */
929 if (tarval_is_null(tb)) {
930 ir_node *a = get_Sub_left(n);
931 if (mode == get_irn_mode(a)) {
934 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
942 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
945 * -(-a) == a, but might overflow two times.
946 * We handle it anyway here but the better way would be a
947 * flag. This would be needed for Pascal for instance.
949 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
952 ir_node *pred = get_unop_op(n);
954 /* optimize symmetric unop */
955 if (get_irn_op(pred) == get_irn_op(n)) {
956 n = get_unop_op(pred);
957 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
963 * Optimize a * 1 = 1 * a = a.
965 static ir_node *equivalent_node_Mul(ir_node *n)
968 ir_node *a = get_Mul_left(n);
970 /* we can handle here only the n * n = n bit cases */
971 if (get_irn_mode(n) == get_irn_mode(a)) {
972 ir_node *b = get_Mul_right(n);
976 * Mul is commutative and has again an other neutral element.
977 * Constants are place right, so check this case first.
980 if (tarval_is_one(tv)) {
982 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
985 if (tarval_is_one(tv)) {
987 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
995 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
997 static ir_node *equivalent_node_Or(ir_node *n)
1001 ir_node *a = get_Or_left(n);
1002 ir_node *b = get_Or_right(n);
1006 n = a; /* idempotence */
1007 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1010 /* constants are normalized to right, check this side first */
1012 if (tarval_is_null(tv)) {
1014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1018 if (tarval_is_null(tv)) {
1020 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1028 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1030 static ir_node *equivalent_node_And(ir_node *n)
1034 ir_node *a = get_And_left(n);
1035 ir_node *b = get_And_right(n);
1039 n = a; /* idempotence */
1040 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1043 /* constants are normalized to right, check this side first */
1045 if (tarval_is_all_one(tv)) {
1047 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1050 if (tv != get_tarval_bad()) {
1051 ir_mode *mode = get_irn_mode(n);
1052 if (!mode_is_signed(mode) && is_Conv(a)) {
1053 ir_node *convop = get_Conv_op(a);
1054 ir_mode *convopmode = get_irn_mode(convop);
1055 if (!mode_is_signed(convopmode)) {
1056 /* Check Conv(all_one) & Const = all_one */
1057 ir_tarval *one = get_mode_all_one(convopmode);
1058 ir_tarval *conv = tarval_convert_to(one, mode);
1059 ir_tarval *tand = tarval_and(conv, tv);
1061 if (tarval_is_all_one(tand)) {
1062 /* Conv(X) & Const = X */
1064 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1071 if (tarval_is_all_one(tv)) {
1073 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1077 if ((is_Or(a) || is_Or_Eor_Add(a))
1078 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1080 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1084 if ((is_Or(b) || is_Or_Eor_Add(b))
1085 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1087 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1094 * Try to remove useless Conv's:
1096 static ir_node *equivalent_node_Conv(ir_node *n)
1099 ir_node *a = get_Conv_op(n);
1101 ir_mode *n_mode = get_irn_mode(n);
1102 ir_mode *a_mode = get_irn_mode(a);
1105 if (n_mode == a_mode) { /* No Conv necessary */
1106 if (get_Conv_strict(n)) {
1109 /* neither Minus nor Confirm change the precision,
1110 so we can "look-through" */
1113 p = get_Minus_op(p);
1114 } else if (is_Confirm(p)) {
1115 p = get_Confirm_value(p);
1121 if (is_Conv(p) && get_Conv_strict(p)) {
1122 /* we known already, that a_mode == n_mode, and neither
1123 Minus change the mode, so the second Conv
1125 assert(get_irn_mode(p) == n_mode);
1127 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1131 ir_node *pred = get_Proj_pred(p);
1132 if (is_Load(pred)) {
1133 /* Loads always return with the exact precision of n_mode */
1134 assert(get_Load_mode(pred) == n_mode);
1136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1139 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1140 pred = get_Proj_pred(pred);
1141 if (is_Start(pred)) {
1142 /* Arguments always return with the exact precision,
1143 as strictConv's are place before Call -- if the
1144 caller was compiled with the same setting.
1145 Otherwise, the semantics is probably still right. */
1146 assert(get_irn_mode(p) == n_mode);
1148 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1154 /* special case: the immediate predecessor is also a Conv */
1155 if (! get_Conv_strict(a)) {
1156 /* first one is not strict, kick it */
1158 a_mode = get_irn_mode(a);
1162 /* else both are strict conv, second is superfluous */
1164 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1172 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1173 ir_node *b = get_Conv_op(a);
1174 ir_mode *b_mode = get_irn_mode(b);
1176 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1177 /* both are strict conv */
1178 if (smaller_mode(a_mode, n_mode)) {
1179 /* both are strict, but the first is smaller, so
1180 the second cannot remove more precision, remove the
1182 set_Conv_strict(n, 0);
1185 if (n_mode == b_mode) {
1186 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1187 if (n_mode == mode_b) {
1188 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1189 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1191 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1192 if (values_in_mode(b_mode, a_mode)) {
1193 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1199 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1200 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1201 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1202 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1204 if (float_mantissa >= int_mantissa) {
1206 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1211 if (smaller_mode(b_mode, a_mode)) {
1212 if (get_Conv_strict(n))
1213 set_Conv_strict(b, 1);
1214 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1225 * - fold Phi-nodes, iff they have only one predecessor except
1228 static ir_node *equivalent_node_Phi(ir_node *n)
1233 ir_node *first_val = NULL; /* to shutup gcc */
1235 if (!get_opt_optimize() &&
1236 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1239 n_preds = get_Phi_n_preds(n);
1241 /* Phi of dead Region without predecessors. */
1245 /* Find first non-self-referencing input */
1246 for (i = 0; i < n_preds; ++i) {
1247 first_val = get_Phi_pred(n, i);
1248 /* not self pointer */
1249 if (first_val != n) {
1250 /* then found first value. */
1255 /* search for rest of inputs, determine if any of these
1256 are non-self-referencing */
1257 while (++i < n_preds) {
1258 ir_node *scnd_val = get_Phi_pred(n, i);
1259 if (scnd_val != n && scnd_val != first_val) {
1264 if (i >= n_preds && !is_Dummy(first_val)) {
1265 /* Fold, if no multiple distinct non-self-referencing inputs */
1267 DBG_OPT_PHI(oldn, n);
1273 * Optimize Proj(Tuple).
1275 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1277 ir_node *oldn = proj;
1278 ir_node *tuple = get_Proj_pred(proj);
1280 /* Remove the Tuple/Proj combination. */
1281 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1282 DBG_OPT_TUPLE(oldn, tuple, proj);
1288 * Optimize a / 1 = a.
1290 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1292 ir_node *oldn = proj;
1293 ir_node *div = get_Proj_pred(proj);
1294 ir_node *b = get_Div_right(div);
1295 ir_tarval *tb = value_of(b);
1297 /* Div is not commutative. */
1298 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1299 switch (get_Proj_proj(proj)) {
1301 proj = get_Div_mem(div);
1302 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1306 proj = get_Div_left(div);
1307 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1311 /* we cannot replace the exception Proj's here, this is done in
1312 transform_node_Proj_Div() */
1320 * Optimize CopyB(mem, x, x) into a Nop.
1322 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1324 ir_node *oldn = proj;
1325 ir_node *copyb = get_Proj_pred(proj);
1326 ir_node *a = get_CopyB_dst(copyb);
1327 ir_node *b = get_CopyB_src(copyb);
1330 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1331 switch (get_Proj_proj(proj)) {
1333 proj = get_CopyB_mem(copyb);
1334 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1342 * Optimize Bounds(idx, idx, upper) into idx.
1344 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1346 ir_node *oldn = proj;
1347 ir_node *bound = get_Proj_pred(proj);
1348 ir_node *idx = get_Bound_index(bound);
1349 ir_node *pred = skip_Proj(idx);
1352 if (idx == get_Bound_lower(bound))
1354 else if (is_Bound(pred)) {
1356 * idx was Bounds checked previously, it is still valid if
1357 * lower <= pred_lower && pred_upper <= upper.
1359 ir_node *lower = get_Bound_lower(bound);
1360 ir_node *upper = get_Bound_upper(bound);
1361 if (get_Bound_lower(pred) == lower &&
1362 get_Bound_upper(pred) == upper) {
1364 * One could expect that we simply return the previous
1365 * Bound here. However, this would be wrong, as we could
1366 * add an exception Proj to a new location then.
1367 * So, we must turn in into a tuple.
1373 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1374 switch (get_Proj_proj(proj)) {
1376 DBG_OPT_EXC_REM(proj);
1377 proj = get_Bound_mem(bound);
1381 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1384 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1392 * Does all optimizations on nodes that must be done on its Projs
1393 * because of creating new nodes.
1395 static ir_node *equivalent_node_Proj(ir_node *proj)
1397 ir_node *n = get_Proj_pred(proj);
1398 if (n->op->ops.equivalent_node_Proj)
1399 return n->op->ops.equivalent_node_Proj(proj);
1406 static ir_node *equivalent_node_Id(ir_node *n)
1414 DBG_OPT_ID(oldn, n);
1421 static ir_node *equivalent_node_Mux(ir_node *n)
1423 ir_node *oldn = n, *sel = get_Mux_sel(n);
1425 ir_tarval *ts = value_of(sel);
1427 if (ts == tarval_bad && is_Cmp(sel)) {
1428 /* try again with a direct call to compute_cmp, as we don't care
1429 * about the MODEB_LOWERED flag here */
1430 ts = compute_cmp_ext(sel);
1433 /* Mux(true, f, t) == t */
1434 if (ts == tarval_b_true) {
1435 n = get_Mux_true(n);
1436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1439 /* Mux(false, f, t) == f */
1440 if (ts == tarval_b_false) {
1441 n = get_Mux_false(n);
1442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1445 n_t = get_Mux_true(n);
1446 n_f = get_Mux_false(n);
1448 /* Mux(v, x, T) == x */
1449 if (is_Unknown(n_f)) {
1451 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1454 /* Mux(v, T, x) == x */
1455 if (is_Unknown(n_t)) {
1457 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1461 /* Mux(v, x, x) == x */
1464 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1467 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1468 ir_relation relation = get_Cmp_relation(sel);
1469 ir_node *f = get_Mux_false(n);
1470 ir_node *t = get_Mux_true(n);
1473 * Note further that these optimization work even for floating point
1474 * with NaN's because -NaN == NaN.
1475 * However, if +0 and -0 is handled differently, we cannot use the first one.
1477 ir_node *const cmp_l = get_Cmp_left(sel);
1478 ir_node *const cmp_r = get_Cmp_right(sel);
1481 case ir_relation_equal:
1482 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1483 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1485 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1490 case ir_relation_less_greater:
1491 case ir_relation_unordered_less_greater:
1492 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1493 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1504 * Note: normalization puts the constant on the right side,
1505 * so we check only one case.
1507 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1508 /* Mux(t CMP 0, X, t) */
1509 if (is_Minus(f) && get_Minus_op(f) == t) {
1510 /* Mux(t CMP 0, -t, t) */
1511 if (relation == ir_relation_equal) {
1512 /* Mux(t == 0, -t, t) ==> -t */
1514 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1515 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1516 /* Mux(t != 0, -t, t) ==> t */
1518 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1528 * Remove Confirm nodes if setting is on.
1529 * Replace Confirms(x, '=', Constlike) by Constlike.
1531 static ir_node *equivalent_node_Confirm(ir_node *n)
1533 ir_node *pred = get_Confirm_value(n);
1534 ir_relation relation = get_Confirm_relation(n);
1536 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1538 * rare case: two identical Confirms one after another,
1539 * replace the second one with the first.
1542 pred = get_Confirm_value(n);
1548 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1549 * perform no actual computation, as, e.g., the Id nodes. It does not create
1550 * new nodes. It is therefore safe to free n if the node returned is not n.
1551 * If a node returns a Tuple we can not just skip it. If the size of the
1552 * in array fits, we transform n into a tuple (e.g., Div).
1554 ir_node *equivalent_node(ir_node *n)
1556 if (n->op->ops.equivalent_node)
1557 return n->op->ops.equivalent_node(n);
1562 * Returns non-zero if a node is a Phi node
1563 * with all predecessors constant.
1565 static int is_const_Phi(ir_node *n)
1569 if (! is_Phi(n) || get_irn_arity(n) == 0)
1571 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1572 if (! is_Const(get_irn_n(n, i)))
1578 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1579 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1582 * in reality eval_func should be tarval (*eval_func)() but incomplete
1583 * declarations are bad style and generate noisy warnings
1585 typedef void (*eval_func)(void);
1588 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1590 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1592 if (eval == (eval_func) tarval_sub) {
1593 tarval_sub_type func = (tarval_sub_type)eval;
1595 return func(a, b, mode);
1597 tarval_binop_type func = (tarval_binop_type)eval;
1604 * Apply an evaluator on a binop with a constant operators (and one Phi).
1606 * @param phi the Phi node
1607 * @param other the other operand
1608 * @param eval an evaluator function
1609 * @param mode the mode of the result, may be different from the mode of the Phi!
1610 * @param left if non-zero, other is the left operand, else the right
1612 * @return a new Phi node if the conversion was successful, NULL else
1614 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1620 int i, n = get_irn_arity(phi);
1622 NEW_ARR_A(void *, res, n);
1624 for (i = 0; i < n; ++i) {
1625 pred = get_irn_n(phi, i);
1626 tv = get_Const_tarval(pred);
1627 tv = do_eval(eval, other, tv, mode);
1629 if (tv == tarval_bad) {
1630 /* folding failed, bad */
1636 for (i = 0; i < n; ++i) {
1637 pred = get_irn_n(phi, i);
1638 tv = get_Const_tarval(pred);
1639 tv = do_eval(eval, tv, other, mode);
1641 if (tv == tarval_bad) {
1642 /* folding failed, bad */
1648 irg = get_irn_irg(phi);
1649 for (i = 0; i < n; ++i) {
1650 pred = get_irn_n(phi, i);
1651 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1653 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1657 * Apply an evaluator on a binop with two constant Phi.
1659 * @param a the left Phi node
1660 * @param b the right Phi node
1661 * @param eval an evaluator function
1662 * @param mode the mode of the result, may be different from the mode of the Phi!
1664 * @return a new Phi node if the conversion was successful, NULL else
1666 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1668 ir_tarval *tv_l, *tv_r, *tv;
1674 if (get_nodes_block(a) != get_nodes_block(b))
1677 n = get_irn_arity(a);
1678 NEW_ARR_A(void *, res, n);
1680 for (i = 0; i < n; ++i) {
1681 pred = get_irn_n(a, i);
1682 tv_l = get_Const_tarval(pred);
1683 pred = get_irn_n(b, i);
1684 tv_r = get_Const_tarval(pred);
1685 tv = do_eval(eval, tv_l, tv_r, mode);
1687 if (tv == tarval_bad) {
1688 /* folding failed, bad */
1693 irg = get_irn_irg(a);
1694 for (i = 0; i < n; ++i) {
1695 pred = get_irn_n(a, i);
1696 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1698 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1702 * Apply an evaluator on a unop with a constant operator (a Phi).
1704 * @param phi the Phi node
1705 * @param eval an evaluator function
1707 * @return a new Phi node if the conversion was successful, NULL else
1709 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1716 int i, n = get_irn_arity(phi);
1718 NEW_ARR_A(void *, res, n);
1719 for (i = 0; i < n; ++i) {
1720 pred = get_irn_n(phi, i);
1721 tv = get_Const_tarval(pred);
1724 if (tv == tarval_bad) {
1725 /* folding failed, bad */
1730 mode = get_irn_mode(phi);
1731 irg = get_irn_irg(phi);
1732 for (i = 0; i < n; ++i) {
1733 pred = get_irn_n(phi, i);
1734 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1736 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1740 * Apply a conversion on a constant operator (a Phi).
1742 * @param phi the Phi node
1744 * @return a new Phi node if the conversion was successful, NULL else
1746 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1752 int i, n = get_irn_arity(phi);
1754 NEW_ARR_A(void *, res, n);
1755 for (i = 0; i < n; ++i) {
1756 pred = get_irn_n(phi, i);
1757 tv = get_Const_tarval(pred);
1758 tv = tarval_convert_to(tv, mode);
1760 if (tv == tarval_bad) {
1761 /* folding failed, bad */
1766 irg = get_irn_irg(phi);
1767 for (i = 0; i < n; ++i) {
1768 pred = get_irn_n(phi, i);
1769 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1771 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1775 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1776 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1777 * If possible, remove the Conv's.
1779 static ir_node *transform_node_AddSub(ir_node *n)
1781 ir_mode *mode = get_irn_mode(n);
1783 if (mode_is_reference(mode)) {
1784 ir_node *left = get_binop_left(n);
1785 ir_node *right = get_binop_right(n);
1786 unsigned ref_bits = get_mode_size_bits(mode);
1788 if (is_Conv(left)) {
1789 ir_mode *lmode = get_irn_mode(left);
1790 unsigned bits = get_mode_size_bits(lmode);
1792 if (ref_bits == bits &&
1793 mode_is_int(lmode) &&
1794 get_mode_arithmetic(lmode) == irma_twos_complement) {
1795 ir_node *pre = get_Conv_op(left);
1796 ir_mode *pre_mode = get_irn_mode(pre);
1798 if (mode_is_int(pre_mode) &&
1799 get_mode_size_bits(pre_mode) == bits &&
1800 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1801 /* ok, this conv just changes to sign, moreover the calculation
1802 * is done with same number of bits as our address mode, so
1803 * we can ignore the conv as address calculation can be viewed
1804 * as either signed or unsigned
1806 set_binop_left(n, pre);
1811 if (is_Conv(right)) {
1812 ir_mode *rmode = get_irn_mode(right);
1813 unsigned bits = get_mode_size_bits(rmode);
1815 if (ref_bits == bits &&
1816 mode_is_int(rmode) &&
1817 get_mode_arithmetic(rmode) == irma_twos_complement) {
1818 ir_node *pre = get_Conv_op(right);
1819 ir_mode *pre_mode = get_irn_mode(pre);
1821 if (mode_is_int(pre_mode) &&
1822 get_mode_size_bits(pre_mode) == bits &&
1823 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1824 /* ok, this conv just changes to sign, moreover the calculation
1825 * is done with same number of bits as our address mode, so
1826 * we can ignore the conv as address calculation can be viewed
1827 * as either signed or unsigned
1829 set_binop_right(n, pre);
1834 /* let address arithmetic use unsigned modes */
1835 if (is_Const(right)) {
1836 ir_mode *rmode = get_irn_mode(right);
1838 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1839 /* convert a AddP(P, *s) into AddP(P, *u) */
1840 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1842 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1843 set_binop_right(n, pre);
1851 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1854 if (is_Const(b) && is_const_Phi(a)) { \
1855 /* check for Op(Phi, Const) */ \
1856 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1858 else if (is_Const(a) && is_const_Phi(b)) { \
1859 /* check for Op(Const, Phi) */ \
1860 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1862 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1863 /* check for Op(Phi, Phi) */ \
1864 c = apply_binop_on_2_phis(a, b, eval, mode); \
1867 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1872 #define HANDLE_UNOP_PHI(eval, a, c) \
1875 if (is_const_Phi(a)) { \
1876 /* check for Op(Phi) */ \
1877 c = apply_unop_on_phi(a, eval); \
1879 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1886 * Create a 0 constant of given mode.
1888 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1890 ir_tarval *tv = get_mode_null(mode);
1891 ir_node *cnst = new_r_Const(irg, tv);
1896 static bool is_shiftop(const ir_node *n)
1898 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1901 /* the order of the values is important! */
1902 typedef enum const_class {
1908 static const_class classify_const(const ir_node* n)
1910 if (is_Const(n)) return const_const;
1911 if (is_irn_constlike(n)) return const_like;
1916 * Determines whether r is more constlike or has a larger index (in that order)
1919 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1921 const const_class l_order = classify_const(l);
1922 const const_class r_order = classify_const(r);
1924 l_order > r_order ||
1925 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1928 static bool is_cmp_unequal(const ir_node *node)
1930 ir_relation relation = get_Cmp_relation(node);
1931 ir_node *left = get_Cmp_left(node);
1932 ir_node *right = get_Cmp_right(node);
1933 ir_mode *mode = get_irn_mode(left);
1935 if (relation == ir_relation_less_greater)
1938 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1939 return relation == ir_relation_greater;
1944 * returns true for Cmp(x == 0) or Cmp(x != 0)
1946 static bool is_cmp_equality_zero(const ir_node *node)
1948 ir_relation relation;
1949 ir_node *right = get_Cmp_right(node);
1951 if (!is_Const(right) || !is_Const_null(right))
1953 relation = get_Cmp_relation(node);
1954 return relation == ir_relation_equal
1955 || relation == ir_relation_less_greater
1956 || (!mode_is_signed(get_irn_mode(right))
1957 && relation == ir_relation_greater);
1961 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1962 * Such pattern may arise in bitfield stores.
1964 * value c4 value c4 & c2
1965 * AND c3 AND c1 | c3
1972 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1975 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1977 ir_node *irn_and, *c1;
1979 ir_node *and_l, *c3;
1980 ir_node *value, *c4;
1981 ir_node *new_and, *new_const, *block;
1982 ir_mode *mode = get_irn_mode(irn_or);
1984 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1988 irn_and = get_binop_left(irn_or);
1989 c1 = get_binop_right(irn_or);
1990 if (!is_Const(c1) || !is_And(irn_and))
1993 or_l = get_binop_left(irn_and);
1994 c2 = get_binop_right(irn_and);
1998 tv1 = get_Const_tarval(c1);
1999 tv2 = get_Const_tarval(c2);
2001 tv = tarval_or(tv1, tv2);
2002 if (tarval_is_all_one(tv)) {
2003 /* the AND does NOT clear a bit with isn't set by the OR */
2004 set_binop_left(irn_or, or_l);
2005 set_binop_right(irn_or, c1);
2007 /* check for more */
2011 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2014 and_l = get_binop_left(or_l);
2015 c3 = get_binop_right(or_l);
2016 if (!is_Const(c3) || !is_And(and_l))
2019 value = get_binop_left(and_l);
2020 c4 = get_binop_right(and_l);
2024 /* ok, found the pattern, check for conditions */
2025 assert(mode == get_irn_mode(irn_and));
2026 assert(mode == get_irn_mode(or_l));
2027 assert(mode == get_irn_mode(and_l));
2029 tv3 = get_Const_tarval(c3);
2030 tv4 = get_Const_tarval(c4);
2032 tv = tarval_or(tv4, tv2);
2033 if (!tarval_is_all_one(tv)) {
2034 /* have at least one 0 at the same bit position */
2038 if (tv3 != tarval_andnot(tv3, tv4)) {
2039 /* bit in the or_mask is outside the and_mask */
2043 if (tv1 != tarval_andnot(tv1, tv2)) {
2044 /* bit in the or_mask is outside the and_mask */
2048 /* ok, all conditions met */
2049 block = get_nodes_block(irn_or);
2050 irg = get_irn_irg(block);
2052 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2054 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2056 set_binop_left(irn_or, new_and);
2057 set_binop_right(irn_or, new_const);
2059 /* check for more */
2064 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2066 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2068 ir_mode *mode = get_irn_mode(irn_or);
2069 ir_node *shl, *shr, *block;
2070 ir_node *irn, *x, *c1, *c2, *n;
2071 ir_tarval *tv1, *tv2;
2073 /* some backends can't handle rotl */
2074 if (!be_get_backend_param()->support_rotl)
2077 if (! mode_is_int(mode))
2080 shl = get_binop_left(irn_or);
2081 shr = get_binop_right(irn_or);
2090 } else if (!is_Shl(shl)) {
2092 } else if (!is_Shr(shr)) {
2095 x = get_Shl_left(shl);
2096 if (x != get_Shr_left(shr))
2099 c1 = get_Shl_right(shl);
2100 c2 = get_Shr_right(shr);
2101 if (is_Const(c1) && is_Const(c2)) {
2102 tv1 = get_Const_tarval(c1);
2103 if (! tarval_is_long(tv1))
2106 tv2 = get_Const_tarval(c2);
2107 if (! tarval_is_long(tv2))
2110 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2111 != (int) get_mode_size_bits(mode))
2114 /* yet, condition met */
2115 block = get_nodes_block(irn_or);
2117 n = new_r_Rotl(block, x, c1, mode);
2119 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2123 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2124 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2125 if (!ir_is_negated_value(c1, c2)) {
2129 /* yet, condition met */
2130 block = get_nodes_block(irn_or);
2131 n = new_r_Rotl(block, x, c1, mode);
2132 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2137 * Prototype of a recursive transform function
2138 * for bitwise distributive transformations.
2140 typedef ir_node* (*recursive_transform)(ir_node *n);
2143 * makes use of distributive laws for and, or, eor
2144 * and(a OP c, b OP c) -> and(a, b) OP c
2145 * note, might return a different op than n
2147 static ir_node *transform_bitwise_distributive(ir_node *n,
2148 recursive_transform trans_func)
2151 ir_node *a = get_binop_left(n);
2152 ir_node *b = get_binop_right(n);
2153 ir_op *op = get_irn_op(a);
2154 ir_op *op_root = get_irn_op(n);
2156 if (op != get_irn_op(b))
2159 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2160 if (op == op_Conv) {
2161 ir_node *a_op = get_Conv_op(a);
2162 ir_node *b_op = get_Conv_op(b);
2163 ir_mode *a_mode = get_irn_mode(a_op);
2164 ir_mode *b_mode = get_irn_mode(b_op);
2165 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2166 ir_node *blk = get_nodes_block(n);
2169 set_binop_left(n, a_op);
2170 set_binop_right(n, b_op);
2171 set_irn_mode(n, a_mode);
2173 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2175 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2181 /* nothing to gain here */
2185 if (op == op_Shrs || op == op_Shr || op == op_Shl
2186 || op == op_And || op == op_Or || op == op_Eor) {
2187 ir_node *a_left = get_binop_left(a);
2188 ir_node *a_right = get_binop_right(a);
2189 ir_node *b_left = get_binop_left(b);
2190 ir_node *b_right = get_binop_right(b);
2192 ir_node *op1 = NULL;
2193 ir_node *op2 = NULL;
2195 if (is_op_commutative(op)) {
2196 if (a_left == b_left) {
2200 } else if (a_left == b_right) {
2204 } else if (a_right == b_left) {
2210 if (a_right == b_right) {
2217 /* (a sop c) & (b sop c) => (a & b) sop c */
2218 ir_node *blk = get_nodes_block(n);
2220 ir_node *new_n = exact_copy(n);
2221 set_binop_left(new_n, op1);
2222 set_binop_right(new_n, op2);
2223 new_n = trans_func(new_n);
2225 if (op_root == op_Eor && op == op_Or) {
2226 dbg_info *dbgi = get_irn_dbg_info(n);
2227 ir_mode *mode = get_irn_mode(c);
2229 c = new_rd_Not(dbgi, blk, c, mode);
2230 n = new_rd_And(dbgi, blk, new_n, c, mode);
2233 set_nodes_block(n, blk);
2234 set_binop_left(n, new_n);
2235 set_binop_right(n, c);
2239 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2248 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2250 * - and, or, xor instead of &
2251 * - Shl, Shr, Shrs, rotl instead of >>
2252 * (with a special case for Or/Xor + Shrs)
2254 * This normalisation is usually good for the backend since << C can often be
2255 * matched as address-mode.
2257 static ir_node *transform_node_bitop_shift(ir_node *n)
2259 ir_graph *irg = get_irn_irg(n);
2260 ir_node *left = get_binop_left(n);
2261 ir_node *right = get_binop_right(n);
2262 ir_mode *mode = get_irn_mode(n);
2263 ir_node *shift_left;
2264 ir_node *shift_right;
2266 dbg_info *dbg_bitop;
2267 dbg_info *dbg_shift;
2273 ir_tarval *tv_bitop;
2275 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2278 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2279 if (!is_Const(right) || !is_shiftop(left))
2282 shift_left = get_binop_left(left);
2283 shift_right = get_binop_right(left);
2284 if (!is_Const(shift_right))
2287 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2288 if (is_Shrs(left)) {
2289 /* TODO this could be improved */
2293 irg = get_irn_irg(n);
2294 block = get_nodes_block(n);
2295 dbg_bitop = get_irn_dbg_info(n);
2296 dbg_shift = get_irn_dbg_info(left);
2297 tv1 = get_Const_tarval(shift_right);
2298 tv2 = get_Const_tarval(right);
2299 assert(get_tarval_mode(tv2) == mode);
2302 tv_bitop = tarval_shr(tv2, tv1);
2304 /* Check whether we have lost some bits during the right shift. */
2306 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2308 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2311 } else if (is_Shr(left)) {
2314 * TODO this can be improved by checking whether
2315 * the left shift produces an overflow
2319 tv_bitop = tarval_shl(tv2, tv1);
2321 assert(is_Rotl(left));
2322 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2324 new_const = new_r_Const(irg, tv_bitop);
2327 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2328 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2329 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2332 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2336 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2337 } else if (is_Shr(left)) {
2338 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2340 assert(is_Rotl(left));
2341 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2347 static bool complement_values(const ir_node *a, const ir_node *b)
2349 if (is_Not(a) && get_Not_op(a) == b)
2351 if (is_Not(b) && get_Not_op(b) == a)
2353 if (is_Const(a) && is_Const(b)) {
2354 ir_tarval *tv_a = get_Const_tarval(a);
2355 ir_tarval *tv_b = get_Const_tarval(b);
2356 return tarval_not(tv_a) == tv_b;
2361 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2364 * for associative operations fold:
2365 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2366 * This is a "light" version of the reassociation phase
2368 static ir_node *fold_constant_associativity(ir_node *node,
2369 tv_fold_binop_func fold)
2374 ir_node *right = get_binop_right(node);
2375 ir_node *left_right;
2382 if (!is_Const(right))
2385 op = get_irn_op(node);
2386 left = get_binop_left(node);
2387 if (get_irn_op(left) != op)
2390 left_right = get_binop_right(left);
2391 if (!is_Const(left_right))
2394 left_left = get_binop_left(left);
2395 c0 = get_Const_tarval(left_right);
2396 c1 = get_Const_tarval(right);
2397 irg = get_irn_irg(node);
2398 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2400 new_c = fold(c0, c1);
2401 if (new_c == tarval_bad)
2403 new_const = new_r_Const(irg, new_c);
2404 new_node = exact_copy(node);
2405 set_binop_left(new_node, left_left);
2406 set_binop_right(new_node, new_const);
2413 static ir_node *transform_node_Or_(ir_node *n)
2416 ir_node *a = get_binop_left(n);
2417 ir_node *b = get_binop_right(n);
2421 n = fold_constant_associativity(n, tarval_or);
2425 if (is_Not(a) && is_Not(b)) {
2426 /* ~a | ~b = ~(a&b) */
2427 ir_node *block = get_nodes_block(n);
2429 mode = get_irn_mode(n);
2432 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2433 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2434 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2438 /* we can combine the relations of two compares with the same operands */
2439 if (is_Cmp(a) && is_Cmp(b)) {
2440 ir_node *a_left = get_Cmp_left(a);
2441 ir_node *a_right = get_Cmp_right(a);
2442 ir_node *b_left = get_Cmp_left(b);
2443 ir_node *b_right = get_Cmp_right(b);
2444 if (a_left == b_left && b_left == b_right) {
2445 dbg_info *dbgi = get_irn_dbg_info(n);
2446 ir_node *block = get_nodes_block(n);
2447 ir_relation a_relation = get_Cmp_relation(a);
2448 ir_relation b_relation = get_Cmp_relation(b);
2449 ir_relation new_relation = a_relation | b_relation;
2450 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2452 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2453 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2454 && !mode_is_float(get_irn_mode(a_left))
2455 && !mode_is_float(get_irn_mode(b_left))) {
2456 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2457 ir_graph *irg = get_irn_irg(n);
2458 dbg_info *dbgi = get_irn_dbg_info(n);
2459 ir_node *block = get_nodes_block(n);
2460 ir_mode *a_mode = get_irn_mode(a_left);
2461 ir_mode *b_mode = get_irn_mode(b_left);
2462 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2463 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2464 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2465 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2466 ir_node *zero = create_zero_const(irg, b_mode);
2467 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2469 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2470 ir_graph *irg = get_irn_irg(n);
2471 dbg_info *dbgi = get_irn_dbg_info(n);
2472 ir_node *block = get_nodes_block(n);
2473 ir_mode *a_mode = get_irn_mode(a_left);
2474 ir_mode *b_mode = get_irn_mode(b_left);
2475 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2476 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2477 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2478 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2479 ir_node *zero = create_zero_const(irg, a_mode);
2480 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2485 mode = get_irn_mode(n);
2486 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2488 n = transform_node_Or_bf_store(n);
2491 n = transform_node_Or_Rotl(n);
2495 n = transform_bitwise_distributive(n, transform_node_Or_);
2498 n = transform_node_bitop_shift(n);
2505 static ir_node *transform_node_Or(ir_node *n)
2507 if (is_Or_Eor_Add(n)) {
2508 dbg_info *dbgi = get_irn_dbg_info(n);
2509 ir_node *block = get_nodes_block(n);
2510 ir_node *left = get_Or_left(n);
2511 ir_node *right = get_Or_right(n);
2512 ir_mode *mode = get_irn_mode(n);
2513 return new_rd_Add(dbgi, block, left, right, mode);
2515 return transform_node_Or_(n);
2521 static ir_node *transform_node_Eor_(ir_node *n)
2524 ir_node *a = get_binop_left(n);
2525 ir_node *b = get_binop_right(n);
2526 ir_mode *mode = get_irn_mode(n);
2529 n = fold_constant_associativity(n, tarval_eor);
2533 /* we can combine the relations of two compares with the same operands */
2534 if (is_Cmp(a) && is_Cmp(b)) {
2535 ir_node *a_left = get_Cmp_left(a);
2536 ir_node *a_right = get_Cmp_left(a);
2537 ir_node *b_left = get_Cmp_left(b);
2538 ir_node *b_right = get_Cmp_right(b);
2539 if (a_left == b_left && b_left == b_right) {
2540 dbg_info *dbgi = get_irn_dbg_info(n);
2541 ir_node *block = get_nodes_block(n);
2542 ir_relation a_relation = get_Cmp_relation(a);
2543 ir_relation b_relation = get_Cmp_relation(b);
2544 ir_relation new_relation = a_relation ^ b_relation;
2545 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2549 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2551 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2552 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2553 dbg_info *dbg = get_irn_dbg_info(n);
2554 ir_node *block = get_nodes_block(n);
2555 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2556 ir_node *new_left = get_Not_op(a);
2557 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2558 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2560 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2561 dbg_info *dbg = get_irn_dbg_info(n);
2562 ir_node *block = get_nodes_block(n);
2563 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2564 ir_node *new_right = get_Not_op(b);
2565 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2566 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2570 /* x ^ 1...1 -> ~1 */
2571 if (is_Const(b) && is_Const_all_one(b)) {
2572 n = new_r_Not(get_nodes_block(n), a, mode);
2573 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2577 n = transform_bitwise_distributive(n, transform_node_Eor_);
2580 n = transform_node_bitop_shift(n);
2587 static ir_node *transform_node_Eor(ir_node *n)
2589 if (is_Or_Eor_Add(n)) {
2590 dbg_info *dbgi = get_irn_dbg_info(n);
2591 ir_node *block = get_nodes_block(n);
2592 ir_node *left = get_Eor_left(n);
2593 ir_node *right = get_Eor_right(n);
2594 ir_mode *mode = get_irn_mode(n);
2595 return new_rd_Add(dbgi, block, left, right, mode);
2597 return transform_node_Eor_(n);
2601 * Do the AddSub optimization, then Transform
2602 * Constant folding on Phi
2603 * Add(a,a) -> Mul(a, 2)
2604 * Add(Mul(a, x), a) -> Mul(a, x+1)
2605 * if the mode is integer or float.
2606 * Transform Add(a,-b) into Sub(a,b).
2607 * Reassociation might fold this further.
2609 static ir_node *transform_node_Add(ir_node *n)
2617 n = fold_constant_associativity(n, tarval_add);
2621 n = transform_node_AddSub(n);
2625 a = get_Add_left(n);
2626 b = get_Add_right(n);
2627 mode = get_irn_mode(n);
2629 if (mode_is_reference(mode)) {
2630 ir_mode *lmode = get_irn_mode(a);
2632 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2633 /* an Add(a, NULL) is a hidden Conv */
2634 dbg_info *dbg = get_irn_dbg_info(n);
2635 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2639 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2640 ir_tarval *tv = get_Const_tarval(b);
2641 ir_tarval *min = get_mode_min(mode);
2642 /* if all bits are set, then this has the same effect as a Not.
2643 * Note that the following == gives false for different modes which
2644 * is exactly what we want */
2646 dbg_info *dbgi = get_irn_dbg_info(n);
2647 ir_graph *irg = get_irn_irg(n);
2648 ir_node *block = get_nodes_block(n);
2649 ir_node *cnst = new_r_Const(irg, min);
2650 return new_rd_Eor(dbgi, block, a, cnst, mode);
2654 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2656 /* for FP the following optimizations are only allowed if
2657 * fp_strict_algebraic is disabled */
2658 if (mode_is_float(mode)) {
2659 ir_graph *irg = get_irn_irg(n);
2660 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2664 if (mode_is_num(mode)) {
2665 ir_graph *irg = get_irn_irg(n);
2666 /* the following code leads to endless recursion when Mul are replaced
2667 * by a simple instruction chain */
2668 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2669 && a == b && mode_is_int(mode)) {
2670 ir_node *block = get_nodes_block(n);
2673 get_irn_dbg_info(n),
2676 new_r_Const_long(irg, mode, 2),
2678 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2683 get_irn_dbg_info(n),
2688 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2693 get_irn_dbg_info(n),
2698 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2701 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2702 /* Here we rely on constants be on the RIGHT side */
2704 ir_node *op = get_Not_op(a);
2706 if (is_Const(b) && is_Const_one(b)) {
2708 ir_node *blk = get_nodes_block(n);
2709 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2710 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2717 if (is_Or_Eor_Add(n)) {
2718 n = transform_node_Or_(n);
2721 n = transform_node_Eor_(n);
2730 * returns -cnst or NULL if impossible
2732 static ir_node *const_negate(ir_node *cnst)
2734 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2735 dbg_info *dbgi = get_irn_dbg_info(cnst);
2736 ir_graph *irg = get_irn_irg(cnst);
2737 if (tv == tarval_bad) return NULL;
2738 return new_rd_Const(dbgi, irg, tv);
2742 * Do the AddSub optimization, then Transform
2743 * Constant folding on Phi
2744 * Sub(0,a) -> Minus(a)
2745 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2746 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2747 * Sub(Add(a, x), x) -> a
2748 * Sub(x, Add(x, a)) -> -a
2749 * Sub(x, Const) -> Add(x, -Const)
2751 static ir_node *transform_node_Sub(ir_node *n)
2757 n = transform_node_AddSub(n);
2759 a = get_Sub_left(n);
2760 b = get_Sub_right(n);
2762 mode = get_irn_mode(n);
2764 if (mode_is_int(mode)) {
2765 ir_mode *lmode = get_irn_mode(a);
2767 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2768 /* a Sub(a, NULL) is a hidden Conv */
2769 dbg_info *dbg = get_irn_dbg_info(n);
2770 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2771 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2775 if (mode == lmode &&
2776 get_mode_arithmetic(mode) == irma_twos_complement &&
2778 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2780 dbg_info *dbg = get_irn_dbg_info(n);
2781 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2782 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2788 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2790 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2791 if (mode_is_float(mode)) {
2792 ir_graph *irg = get_irn_irg(n);
2793 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2797 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2798 /* a - C -> a + (-C) */
2799 ir_node *cnst = const_negate(b);
2801 ir_node *block = get_nodes_block(n);
2802 dbg_info *dbgi = get_irn_dbg_info(n);
2804 n = new_rd_Add(dbgi, block, a, cnst, mode);
2805 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2810 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2811 dbg_info *dbg = get_irn_dbg_info(n);
2812 ir_node *block = get_nodes_block(n);
2813 ir_node *left = get_Minus_op(a);
2814 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2816 n = new_rd_Minus(dbg, block, add, mode);
2817 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2819 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2820 dbg_info *dbg = get_irn_dbg_info(n);
2821 ir_node *block = get_nodes_block(n);
2822 ir_node *right = get_Minus_op(b);
2824 n = new_rd_Add(dbg, block, a, right, mode);
2825 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2827 } else if (is_Sub(b)) {
2828 /* a - (b - c) -> a + (c - b)
2829 * -> (a - b) + c iff (b - c) is a pointer */
2830 dbg_info *s_dbg = get_irn_dbg_info(b);
2831 ir_node *s_left = get_Sub_left(b);
2832 ir_node *s_right = get_Sub_right(b);
2833 ir_mode *s_mode = get_irn_mode(b);
2834 if (mode_is_reference(s_mode)) {
2835 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2836 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2837 dbg_info *a_dbg = get_irn_dbg_info(n);
2840 s_right = new_r_Conv(lowest_block, s_right, mode);
2841 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2843 ir_node *s_block = get_nodes_block(b);
2844 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2845 dbg_info *a_dbg = get_irn_dbg_info(n);
2846 ir_node *a_block = get_nodes_block(n);
2848 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2850 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2853 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2854 ir_node *m_right = get_Mul_right(b);
2855 if (is_Const(m_right)) {
2856 ir_node *cnst2 = const_negate(m_right);
2857 if (cnst2 != NULL) {
2858 dbg_info *m_dbg = get_irn_dbg_info(b);
2859 ir_node *m_block = get_nodes_block(b);
2860 ir_node *m_left = get_Mul_left(b);
2861 ir_mode *m_mode = get_irn_mode(b);
2862 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2863 dbg_info *a_dbg = get_irn_dbg_info(n);
2864 ir_node *a_block = get_nodes_block(n);
2866 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2867 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2874 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2875 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2877 get_irn_dbg_info(n),
2881 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2884 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2885 ir_node *left = get_binop_left(a);
2886 ir_node *right = get_binop_right(a);
2888 /* FIXME: Does the Conv's work only for two complement or generally? */
2890 if (mode != get_irn_mode(right)) {
2891 /* This Sub is an effective Cast */
2892 right = new_r_Conv(get_nodes_block(n), right, mode);
2895 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2897 } else if (right == b) {
2898 if (mode != get_irn_mode(left)) {
2899 /* This Sub is an effective Cast */
2900 left = new_r_Conv(get_nodes_block(n), left, mode);
2903 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2907 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2908 ir_node *left = get_binop_left(b);
2909 ir_node *right = get_binop_right(b);
2911 /* FIXME: Does the Conv's work only for two complement or generally? */
2913 ir_mode *r_mode = get_irn_mode(right);
2915 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2916 if (mode != r_mode) {
2917 /* This Sub is an effective Cast */
2918 n = new_r_Conv(get_nodes_block(n), n, mode);
2920 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2922 } else if (right == a) {
2923 ir_mode *l_mode = get_irn_mode(left);
2925 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2926 if (mode != l_mode) {
2927 /* This Sub is an effective Cast */
2928 n = new_r_Conv(get_nodes_block(n), n, mode);
2930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2934 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2935 ir_mode *mode = get_irn_mode(a);
2937 if (mode == get_irn_mode(b)) {
2939 ir_node *op_a = get_Conv_op(a);
2940 ir_node *op_b = get_Conv_op(b);
2942 /* check if it's allowed to skip the conv */
2943 ma = get_irn_mode(op_a);
2944 mb = get_irn_mode(op_b);
2946 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2947 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2950 set_Sub_right(n, b);
2956 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2957 if (!is_reassoc_running() && is_Mul(a)) {
2958 ir_node *ma = get_Mul_left(a);
2959 ir_node *mb = get_Mul_right(a);
2962 ir_node *blk = get_nodes_block(n);
2963 ir_graph *irg = get_irn_irg(n);
2965 get_irn_dbg_info(n),
2969 get_irn_dbg_info(n),
2972 new_r_Const(irg, get_mode_one(mode)),
2975 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2977 } else if (mb == b) {
2978 ir_node *blk = get_nodes_block(n);
2979 ir_graph *irg = get_irn_irg(n);
2981 get_irn_dbg_info(n),
2985 get_irn_dbg_info(n),
2988 new_r_Const(irg, get_mode_one(mode)),
2991 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2995 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2996 ir_node *x = get_Sub_left(a);
2997 ir_node *y = get_Sub_right(a);
2998 ir_node *blk = get_nodes_block(n);
2999 ir_mode *m_b = get_irn_mode(b);
3000 ir_mode *m_y = get_irn_mode(y);
3004 /* Determine the right mode for the Add. */
3007 else if (mode_is_reference(m_b))
3009 else if (mode_is_reference(m_y))
3013 * Both modes are different but none is reference,
3014 * happens for instance in SubP(SubP(P, Iu), Is).
3015 * We have two possibilities here: Cast or ignore.
3016 * Currently we ignore this case.
3021 add = new_r_Add(blk, y, b, add_mode);
3023 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3024 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3028 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3029 /* c - ~X = X + (c+1) */
3030 if (is_Const(a) && is_Not(b)) {
3031 ir_tarval *tv = get_Const_tarval(a);
3033 tv = tarval_add(tv, get_mode_one(mode));
3034 if (tv != tarval_bad) {
3035 ir_node *blk = get_nodes_block(n);
3036 ir_graph *irg = get_irn_irg(n);
3037 ir_node *c = new_r_Const(irg, tv);
3038 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3039 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3043 /* x-(x&y) = x & ~y */
3045 ir_node *and_left = get_And_left(b);
3046 ir_node *and_right = get_And_right(b);
3047 if (and_right == a) {
3048 ir_node *tmp = and_left;
3049 and_left = and_right;
3052 if (and_left == a) {
3053 dbg_info *dbgi = get_irn_dbg_info(n);
3054 ir_node *block = get_nodes_block(n);
3055 ir_mode *mode = get_irn_mode(n);
3056 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3057 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
3066 * Several transformation done on n*n=2n bits mul.
3067 * These transformations must be done here because new nodes may be produced.
3069 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3072 ir_node *a = get_Mul_left(n);
3073 ir_node *b = get_Mul_right(n);
3074 ir_tarval *ta = value_of(a);
3075 ir_tarval *tb = value_of(b);
3076 ir_mode *smode = get_irn_mode(a);
3078 if (ta == get_mode_one(smode)) {
3079 /* (L)1 * (L)b = (L)b */
3080 ir_node *blk = get_nodes_block(n);
3081 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3082 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3085 else if (ta == get_mode_minus_one(smode)) {
3086 /* (L)-1 * (L)b = (L)b */
3087 ir_node *blk = get_nodes_block(n);
3088 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3089 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3090 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3093 if (tb == get_mode_one(smode)) {
3094 /* (L)a * (L)1 = (L)a */
3095 ir_node *blk = get_nodes_block(a);
3096 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3097 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3100 else if (tb == get_mode_minus_one(smode)) {
3101 /* (L)a * (L)-1 = (L)-a */
3102 ir_node *blk = get_nodes_block(n);
3103 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3104 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3105 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3112 * Transform Mul(a,-1) into -a.
3113 * Do constant evaluation of Phi nodes.
3114 * Do architecture dependent optimizations on Mul nodes
3116 static ir_node *transform_node_Mul(ir_node *n)
3118 ir_node *c, *oldn = n;
3119 ir_mode *mode = get_irn_mode(n);
3120 ir_node *a = get_Mul_left(n);
3121 ir_node *b = get_Mul_right(n);
3123 n = fold_constant_associativity(n, tarval_mul);
3127 if (mode != get_irn_mode(a))
3128 return transform_node_Mul2n(n, mode);
3130 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3132 if (mode_is_signed(mode)) {
3135 if (value_of(a) == get_mode_minus_one(mode))
3137 else if (value_of(b) == get_mode_minus_one(mode))
3140 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3146 if (is_Const(b)) { /* (-a) * const -> a * -const */
3147 ir_node *cnst = const_negate(b);
3149 dbg_info *dbgi = get_irn_dbg_info(n);
3150 ir_node *block = get_nodes_block(n);
3151 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3152 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3155 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3156 dbg_info *dbgi = get_irn_dbg_info(n);
3157 ir_node *block = get_nodes_block(n);
3158 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3159 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3161 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3162 ir_node *sub_l = get_Sub_left(b);
3163 ir_node *sub_r = get_Sub_right(b);
3164 dbg_info *dbgi = get_irn_dbg_info(n);
3165 ir_node *block = get_nodes_block(n);
3166 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3167 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3168 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3171 } else if (is_Minus(b)) {
3172 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3173 ir_node *sub_l = get_Sub_left(a);
3174 ir_node *sub_r = get_Sub_right(a);
3175 dbg_info *dbgi = get_irn_dbg_info(n);
3176 ir_node *block = get_nodes_block(n);
3177 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3178 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3182 } else if (is_Shl(a)) {
3183 ir_node *const shl_l = get_Shl_left(a);
3184 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3185 /* (1 << x) * b -> b << x */
3186 dbg_info *const dbgi = get_irn_dbg_info(n);
3187 ir_node *const block = get_nodes_block(n);
3188 ir_node *const shl_r = get_Shl_right(a);
3189 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3190 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3193 } else if (is_Shl(b)) {
3194 ir_node *const shl_l = get_Shl_left(b);
3195 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3196 /* a * (1 << x) -> a << x */
3197 dbg_info *const dbgi = get_irn_dbg_info(n);
3198 ir_node *const block = get_nodes_block(n);
3199 ir_node *const shl_r = get_Shl_right(b);
3200 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3201 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3205 if (get_mode_arithmetic(mode) == irma_ieee754
3206 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3208 ir_tarval *tv = get_Const_tarval(a);
3209 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3210 && !tarval_is_negative(tv)) {
3211 /* 2.0 * b = b + b */
3212 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3217 else if (is_Const(b)) {
3218 ir_tarval *tv = get_Const_tarval(b);
3219 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3220 && !tarval_is_negative(tv)) {
3221 /* a * 2.0 = a + a */
3222 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3223 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3228 return arch_dep_replace_mul_with_shifts(n);
3232 * Transform a Div Node.
3234 static ir_node *transform_node_Div(ir_node *n)
3236 ir_mode *mode = get_Div_resmode(n);
3237 ir_node *a = get_Div_left(n);
3238 ir_node *b = get_Div_right(n);
3240 const ir_node *dummy;
3242 if (mode_is_int(mode)) {
3243 if (is_Const(b) && is_const_Phi(a)) {
3244 /* check for Div(Phi, Const) */
3245 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3247 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3250 } else if (is_Const(a) && is_const_Phi(b)) {
3251 /* check for Div(Const, Phi) */
3252 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3254 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3257 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3258 /* check for Div(Phi, Phi) */
3259 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3261 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3266 if (a == b && value_not_zero(a, &dummy)) {
3267 ir_graph *irg = get_irn_irg(n);
3268 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3269 value = new_r_Const(irg, get_mode_one(mode));
3270 DBG_OPT_CSTEVAL(n, value);
3273 if (mode_is_signed(mode) && is_Const(b)) {
3274 ir_tarval *tv = get_Const_tarval(b);
3276 if (tv == get_mode_minus_one(mode)) {
3278 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3279 DBG_OPT_CSTEVAL(n, value);
3283 /* Try architecture dependent optimization */
3284 value = arch_dep_replace_div_by_const(n);
3287 assert(mode_is_float(mode));
3289 /* Optimize x/c to x*(1/c) */
3290 if (get_mode_arithmetic(mode) == irma_ieee754) {
3291 ir_tarval *tv = value_of(b);
3293 if (tv != tarval_bad) {
3294 int rem = tarval_fp_ops_enabled();
3297 * Floating point constant folding might be disabled here to
3299 * However, as we check for exact result, doing it is safe.
3302 tarval_enable_fp_ops(1);
3303 tv = tarval_div(get_mode_one(mode), tv);
3304 tarval_enable_fp_ops(rem);
3306 /* Do the transformation if the result is either exact or we are
3307 not using strict rules. */
3308 if (tv != tarval_bad &&
3309 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3310 ir_node *block = get_nodes_block(n);
3311 ir_graph *irg = get_irn_irg(block);
3312 ir_node *c = new_r_Const(irg, tv);
3313 dbg_info *dbgi = get_irn_dbg_info(n);
3314 value = new_rd_Mul(dbgi, block, a, c, mode);
3327 /* Turn Div into a tuple (mem, jmp, bad, value) */
3328 mem = get_Div_mem(n);
3329 blk = get_nodes_block(n);
3330 irg = get_irn_irg(blk);
3332 /* skip a potential Pin */
3333 mem = skip_Pin(mem);
3334 turn_into_tuple(n, pn_Div_max+1);
3335 set_Tuple_pred(n, pn_Div_M, mem);
3336 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3337 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3338 set_Tuple_pred(n, pn_Div_res, value);
3344 * Transform a Mod node.
3346 static ir_node *transform_node_Mod(ir_node *n)
3348 ir_mode *mode = get_Mod_resmode(n);
3349 ir_node *a = get_Mod_left(n);
3350 ir_node *b = get_Mod_right(n);
3355 if (is_Const(b) && is_const_Phi(a)) {
3356 /* check for Div(Phi, Const) */
3357 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3359 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3363 else if (is_Const(a) && is_const_Phi(b)) {
3364 /* check for Div(Const, Phi) */
3365 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3367 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3371 else if (is_const_Phi(a) && is_const_Phi(b)) {
3372 /* check for Div(Phi, Phi) */
3373 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3375 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3382 irg = get_irn_irg(n);
3383 if (tv != tarval_bad) {
3384 value = new_r_Const(irg, tv);
3386 DBG_OPT_CSTEVAL(n, value);
3389 ir_node *a = get_Mod_left(n);
3390 ir_node *b = get_Mod_right(n);
3391 const ir_node *dummy;
3393 if (a == b && value_not_zero(a, &dummy)) {
3394 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3395 value = new_r_Const(irg, get_mode_null(mode));
3396 DBG_OPT_CSTEVAL(n, value);
3399 if (mode_is_signed(mode) && is_Const(b)) {
3400 ir_tarval *tv = get_Const_tarval(b);
3402 if (tv == get_mode_minus_one(mode)) {
3404 value = new_r_Const(irg, get_mode_null(mode));
3405 DBG_OPT_CSTEVAL(n, value);
3409 /* Try architecture dependent optimization */
3410 value = arch_dep_replace_mod_by_const(n);
3419 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3420 mem = get_Mod_mem(n);
3421 blk = get_nodes_block(n);
3422 irg = get_irn_irg(blk);
3424 /* skip a potential Pin */
3425 mem = skip_Pin(mem);
3426 turn_into_tuple(n, pn_Mod_max+1);
3427 set_Tuple_pred(n, pn_Mod_M, mem);
3428 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3429 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3430 set_Tuple_pred(n, pn_Mod_res, value);
3436 * Transform a Cond node.
3438 * Replace the Cond by a Jmp if it branches on a constant
3441 static ir_node *transform_node_Cond(ir_node *n)
3443 ir_node *a = get_Cond_selector(n);
3444 ir_graph *irg = get_irn_irg(n);
3448 /* we need block info which is not available in floating irgs */
3449 if (get_irg_pinned(irg) == op_pin_state_floats)
3453 if (ta == tarval_bad && is_Cmp(a)) {
3454 /* try again with a direct call to compute_cmp, as we don't care
3455 * about the MODEB_LOWERED flag here */
3456 ta = compute_cmp_ext(a);
3459 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3460 /* It's a boolean Cond, branching on a boolean constant.
3461 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3462 ir_node *blk = get_nodes_block(n);
3463 jmp = new_r_Jmp(blk);
3464 turn_into_tuple(n, pn_Cond_max+1);
3465 if (ta == tarval_b_true) {
3466 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3467 set_Tuple_pred(n, pn_Cond_true, jmp);
3469 set_Tuple_pred(n, pn_Cond_false, jmp);
3470 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3472 /* We might generate an endless loop, so keep it alive. */
3473 add_End_keepalive(get_irg_end(irg), blk);
3474 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3479 static ir_node *transform_node_Switch(ir_node *n)
3481 ir_node *op = get_Switch_selector(n);
3482 ir_tarval *val = value_of(op);
3483 if (val != tarval_bad) {
3484 dbg_info *dbgi = get_irn_dbg_info(n);
3485 ir_graph *irg = get_irn_irg(n);
3486 unsigned n_outs = get_Switch_n_outs(n);
3487 ir_node *block = get_nodes_block(n);
3488 ir_node *bad = new_r_Bad(irg, mode_X);
3489 ir_node **in = XMALLOCN(ir_node*, n_outs);
3490 const ir_switch_table *table = get_Switch_table(n);
3491 size_t n_entries = ir_switch_table_get_n_entries(table);
3495 for (i = 0; i < n_entries; ++i) {
3496 const ir_switch_table_entry *entry
3497 = ir_switch_table_get_entry_const(table, i);
3498 ir_tarval *min = entry->min;
3499 ir_tarval *max = entry->max;
3502 if ((min == max && min == val)
3503 || (tarval_cmp(val, min) != ir_relation_less
3504 && tarval_cmp(val, max) != ir_relation_greater)) {
3509 for (o = 0; o < n_outs; ++o) {
3510 if (o == (unsigned)jmp_pn) {
3511 in[o] = new_rd_Jmp(dbgi, block);
3516 return new_r_Tuple(block, (int)n_outs, in);
3522 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3524 * - and, or, xor instead of &
3525 * - Shl, Shr, Shrs, rotl instead of >>
3526 * (with a special case for Or/Xor + Shrs)
3528 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3530 static ir_node *transform_node_shift_bitop(ir_node *n)
3532 ir_graph *irg = get_irn_irg(n);
3533 ir_node *right = get_binop_right(n);
3534 ir_mode *mode = get_irn_mode(n);
3536 ir_node *bitop_left;
3537 ir_node *bitop_right;
3546 ir_tarval *tv_shift;
3548 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3551 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3553 if (!is_Const(right))
3556 left = get_binop_left(n);
3557 op_left = get_irn_op(left);
3558 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3561 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3562 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3563 /* TODO: test if sign bit is affectes */
3567 bitop_right = get_binop_right(left);
3568 if (!is_Const(bitop_right))
3571 bitop_left = get_binop_left(left);
3573 block = get_nodes_block(n);
3574 dbgi = get_irn_dbg_info(n);
3575 tv1 = get_Const_tarval(bitop_right);
3576 tv2 = get_Const_tarval(right);
3578 assert(get_tarval_mode(tv1) == mode);
3581 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3582 tv_shift = tarval_shl(tv1, tv2);
3583 } else if (is_Shr(n)) {
3584 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3585 tv_shift = tarval_shr(tv1, tv2);
3586 } else if (is_Shrs(n)) {
3587 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3588 tv_shift = tarval_shrs(tv1, tv2);
3591 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3592 tv_shift = tarval_rotl(tv1, tv2);
3595 assert(get_tarval_mode(tv_shift) == mode);
3596 irg = get_irn_irg(n);
3597 new_const = new_r_Const(irg, tv_shift);
3599 if (op_left == op_And) {
3600 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3601 } else if (op_left == op_Or) {
3602 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3604 assert(op_left == op_Eor);
3605 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3614 static ir_node *transform_node_And(ir_node *n)
3616 ir_node *c, *oldn = n;
3617 ir_node *a = get_And_left(n);
3618 ir_node *b = get_And_right(n);
3621 n = fold_constant_associativity(n, tarval_and);
3625 if (is_Cmp(a) && is_Cmp(b)) {
3626 ir_node *a_left = get_Cmp_left(a);
3627 ir_node *a_right = get_Cmp_right(a);
3628 ir_node *b_left = get_Cmp_left(b);
3629 ir_node *b_right = get_Cmp_right(b);
3630 ir_relation a_relation = get_Cmp_relation(a);
3631 ir_relation b_relation = get_Cmp_relation(b);
3632 /* we can combine the relations of two compares with the same
3634 if (a_left == b_left && b_left == b_right) {
3635 dbg_info *dbgi = get_irn_dbg_info(n);
3636 ir_node *block = get_nodes_block(n);
3637 ir_relation new_relation = a_relation & b_relation;
3638 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3640 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3641 if (a_relation == b_relation && a_relation == ir_relation_equal
3642 && !mode_is_float(get_irn_mode(a_left))
3643 && !mode_is_float(get_irn_mode(b_left))) {
3644 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3645 dbg_info *dbgi = get_irn_dbg_info(n);
3646 ir_node *block = get_nodes_block(n);
3647 ir_mode *a_mode = get_irn_mode(a_left);
3648 ir_mode *b_mode = get_irn_mode(b_left);
3649 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3650 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3651 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3652 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3653 ir_graph *irg = get_irn_irg(n);
3654 ir_node *zero = create_zero_const(irg, b_mode);
3655 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3657 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3658 dbg_info *dbgi = get_irn_dbg_info(n);
3659 ir_node *block = get_nodes_block(n);
3660 ir_mode *a_mode = get_irn_mode(a_left);
3661 ir_mode *b_mode = get_irn_mode(b_left);
3662 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3663 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3664 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3665 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3666 ir_graph *irg = get_irn_irg(n);
3667 ir_node *zero = create_zero_const(irg, a_mode);
3668 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3673 mode = get_irn_mode(n);
3674 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3676 if (is_Or(a) || is_Or_Eor_Add(a)) {
3677 ir_node *or_left = get_binop_left(a);
3678 ir_node *or_right = get_binop_right(a);
3679 if (complement_values(or_left, b)) {
3680 /* (a|b) & ~a => b & ~a */
3681 dbg_info *dbgi = get_irn_dbg_info(n);
3682 ir_node *block = get_nodes_block(n);
3683 return new_rd_And(dbgi, block, or_right, b, mode);
3684 } else if (complement_values(or_right, b)) {
3685 /* (a|b) & ~b => a & ~b */
3686 dbg_info *dbgi = get_irn_dbg_info(n);
3687 ir_node *block = get_nodes_block(n);
3688 return new_rd_And(dbgi, block, or_left, b, mode);
3689 } else if (is_Not(b)) {
3690 ir_node *op = get_Not_op(b);
3692 ir_node *ba = get_And_left(op);
3693 ir_node *bb = get_And_right(op);
3695 /* it's enough to test the following cases due to normalization! */
3696 if (or_left == ba && or_right == bb) {
3697 /* (a|b) & ~(a&b) = a^b */
3698 ir_node *block = get_nodes_block(n);
3700 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3701 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3707 if (is_Or(b) || is_Or_Eor_Add(b)) {
3708 ir_node *or_left = get_binop_left(b);
3709 ir_node *or_right = get_binop_right(b);
3710 if (complement_values(or_left, a)) {
3711 /* (a|b) & ~a => b & ~a */
3712 dbg_info *dbgi = get_irn_dbg_info(n);
3713 ir_node *block = get_nodes_block(n);
3714 return new_rd_And(dbgi, block, or_right, a, mode);
3715 } else if (complement_values(or_right, a)) {
3716 /* (a|b) & ~b => a & ~b */
3717 dbg_info *dbgi = get_irn_dbg_info(n);
3718 ir_node *block = get_nodes_block(n);
3719 return new_rd_And(dbgi, block, or_left, a, mode);
3720 } else if (is_Not(a)) {
3721 ir_node *op = get_Not_op(a);
3723 ir_node *aa = get_And_left(op);
3724 ir_node *ab = get_And_right(op);
3726 /* it's enough to test the following cases due to normalization! */
3727 if (or_left == aa && or_right == ab) {
3728 /* (a|b) & ~(a&b) = a^b */
3729 ir_node *block = get_nodes_block(n);
3731 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3732 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3738 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3739 ir_node *al = get_binop_left(a);
3740 ir_node *ar = get_binop_right(a);
3743 /* (b ^ a) & b -> ~a & b */
3744 dbg_info *dbg = get_irn_dbg_info(n);
3745 ir_node *block = get_nodes_block(n);
3747 ar = new_rd_Not(dbg, block, ar, mode);
3748 n = new_rd_And(dbg, block, ar, b, mode);
3749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3753 /* (a ^ b) & b -> ~a & b */
3754 dbg_info *dbg = get_irn_dbg_info(n);
3755 ir_node *block = get_nodes_block(n);
3757 al = new_rd_Not(dbg, block, al, mode);
3758 n = new_rd_And(dbg, block, al, b, mode);
3759 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3763 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3764 ir_node *bl = get_binop_left(b);
3765 ir_node *br = get_binop_right(b);
3768 /* a & (a ^ b) -> a & ~b */
3769 dbg_info *dbg = get_irn_dbg_info(n);
3770 ir_node *block = get_nodes_block(n);
3772 br = new_rd_Not(dbg, block, br, mode);
3773 n = new_rd_And(dbg, block, br, a, mode);
3774 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3778 /* a & (b ^ a) -> a & ~b */
3779 dbg_info *dbg = get_irn_dbg_info(n);
3780 ir_node *block = get_nodes_block(n);
3782 bl = new_rd_Not(dbg, block, bl, mode);
3783 n = new_rd_And(dbg, block, bl, a, mode);
3784 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3788 if (is_Not(a) && is_Not(b)) {
3789 /* ~a & ~b = ~(a|b) */
3790 ir_node *block = get_nodes_block(n);
3791 ir_mode *mode = get_irn_mode(n);
3795 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3796 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3797 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3802 vrp_attr *b_vrp = vrp_get_info(b);
3803 ir_tarval *a_val = get_Const_tarval(a);
3804 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3810 vrp_attr *a_vrp = vrp_get_info(a);
3811 ir_tarval *b_val = get_Const_tarval(b);
3812 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3817 n = transform_bitwise_distributive(n, transform_node_And);
3819 n = transform_node_bitop_shift(n);
3827 static ir_node *transform_node_Not(ir_node *n)
3829 ir_node *c, *oldn = n;
3830 ir_node *a = get_Not_op(n);
3831 ir_mode *mode = get_irn_mode(n);
3833 HANDLE_UNOP_PHI(tarval_not,a,c);
3835 /* check for a boolean Not */
3837 dbg_info *dbgi = get_irn_dbg_info(a);
3838 ir_node *block = get_nodes_block(a);
3839 ir_relation relation = get_Cmp_relation(a);
3840 relation = get_negated_relation(relation);
3841 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3842 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3846 /* normalize ~(a ^ b) => a ^ ~b */
3847 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3848 dbg_info *dbg = get_irn_dbg_info(n);
3849 ir_node *block = get_nodes_block(n);
3850 ir_node *eor_right = get_binop_right(a);
3851 ir_node *eor_left = get_binop_left(a);
3852 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3853 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3857 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3858 if (is_Minus(a)) { /* ~-x -> x + -1 */
3859 dbg_info *dbg = get_irn_dbg_info(n);
3860 ir_graph *irg = get_irn_irg(n);
3861 ir_node *block = get_nodes_block(n);
3862 ir_node *add_l = get_Minus_op(a);
3863 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3864 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3865 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3866 ir_node *add_r = get_binop_right(a);
3867 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3868 /* ~(x + -1) = -x */
3869 ir_node *op = get_binop_left(a);
3870 ir_node *blk = get_nodes_block(n);
3871 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3872 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3880 * Transform a Minus.
3884 * -(a >>u (size-1)) = a >>s (size-1)
3885 * -(a >>s (size-1)) = a >>u (size-1)
3886 * -(a * const) -> a * -const
3888 static ir_node *transform_node_Minus(ir_node *n)
3890 ir_node *c, *oldn = n;
3891 ir_node *a = get_Minus_op(n);
3894 HANDLE_UNOP_PHI(tarval_neg,a,c);
3896 mode = get_irn_mode(a);
3897 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3898 /* the following rules are only to twos-complement */
3901 ir_node *op = get_Not_op(a);
3902 ir_tarval *tv = get_mode_one(mode);
3903 ir_node *blk = get_nodes_block(n);
3904 ir_graph *irg = get_irn_irg(blk);
3905 ir_node *c = new_r_Const(irg, tv);
3906 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3907 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3911 ir_node *c = get_Shr_right(a);
3914 ir_tarval *tv = get_Const_tarval(c);
3916 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3917 /* -(a >>u (size-1)) = a >>s (size-1) */
3918 ir_node *v = get_Shr_left(a);
3920 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3921 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3927 ir_node *c = get_Shrs_right(a);
3930 ir_tarval *tv = get_Const_tarval(c);
3932 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3933 /* -(a >>s (size-1)) = a >>u (size-1) */
3934 ir_node *v = get_Shrs_left(a);
3936 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3937 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3944 /* - (a-b) = b - a */
3945 ir_node *la = get_Sub_left(a);
3946 ir_node *ra = get_Sub_right(a);
3947 ir_node *blk = get_nodes_block(n);
3949 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3950 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3954 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3955 ir_node *mul_l = get_Mul_left(a);
3956 ir_node *mul_r = get_Mul_right(a);
3957 ir_tarval *tv = value_of(mul_r);
3958 if (tv != tarval_bad) {
3959 tv = tarval_neg(tv);
3960 if (tv != tarval_bad) {
3961 ir_graph *irg = get_irn_irg(n);
3962 ir_node *cnst = new_r_Const(irg, tv);
3963 dbg_info *dbg = get_irn_dbg_info(a);
3964 ir_node *block = get_nodes_block(a);
3965 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3966 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3976 * Transform a Proj(Load) with a non-null address.
3978 static ir_node *transform_node_Proj_Load(ir_node *proj)
3980 if (get_irn_mode(proj) == mode_X) {
3981 ir_node *load = get_Proj_pred(proj);
3983 /* get the Load address */
3984 const ir_node *addr = get_Load_ptr(load);
3985 const ir_node *confirm;
3987 if (value_not_null(addr, &confirm)) {
3988 if (confirm == NULL) {
3989 /* this node may float if it did not depend on a Confirm */
3990 set_irn_pinned(load, op_pin_state_floats);
3992 if (get_Proj_proj(proj) == pn_Load_X_except) {
3993 ir_graph *irg = get_irn_irg(proj);
3994 DBG_OPT_EXC_REM(proj);
3995 return new_r_Bad(irg, mode_X);
3997 ir_node *blk = get_nodes_block(load);
3998 return new_r_Jmp(blk);
4006 * Transform a Proj(Store) with a non-null address.
4008 static ir_node *transform_node_Proj_Store(ir_node *proj)
4010 if (get_irn_mode(proj) == mode_X) {
4011 ir_node *store = get_Proj_pred(proj);
4013 /* get the load/store address */
4014 const ir_node *addr = get_Store_ptr(store);
4015 const ir_node *confirm;
4017 if (value_not_null(addr, &confirm)) {
4018 if (confirm == NULL) {
4019 /* this node may float if it did not depend on a Confirm */
4020 set_irn_pinned(store, op_pin_state_floats);
4022 if (get_Proj_proj(proj) == pn_Store_X_except) {
4023 ir_graph *irg = get_irn_irg(proj);
4024 DBG_OPT_EXC_REM(proj);
4025 return new_r_Bad(irg, mode_X);
4027 ir_node *blk = get_nodes_block(store);
4028 return new_r_Jmp(blk);
4036 * Transform a Proj(Div) with a non-zero value.
4037 * Removes the exceptions and routes the memory to the NoMem node.
4039 static ir_node *transform_node_Proj_Div(ir_node *proj)
4041 ir_node *div = get_Proj_pred(proj);
4042 ir_node *b = get_Div_right(div);
4043 ir_node *res, *new_mem;
4044 const ir_node *confirm;
4047 if (value_not_zero(b, &confirm)) {
4048 /* div(x, y) && y != 0 */
4049 if (confirm == NULL) {
4050 /* we are sure we have a Const != 0 */
4051 new_mem = get_Div_mem(div);
4052 new_mem = skip_Pin(new_mem);
4053 set_Div_mem(div, new_mem);
4054 set_irn_pinned(div, op_pin_state_floats);
4057 proj_nr = get_Proj_proj(proj);
4059 case pn_Div_X_regular:
4060 return new_r_Jmp(get_nodes_block(div));
4062 case pn_Div_X_except: {
4063 ir_graph *irg = get_irn_irg(proj);
4064 /* we found an exception handler, remove it */
4065 DBG_OPT_EXC_REM(proj);
4066 return new_r_Bad(irg, mode_X);
4070 ir_graph *irg = get_irn_irg(proj);
4071 res = get_Div_mem(div);
4072 new_mem = get_irg_no_mem(irg);
4075 /* This node can only float up to the Confirm block */
4076 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4078 set_irn_pinned(div, op_pin_state_floats);
4079 /* this is a Div without exception, we can remove the memory edge */
4080 set_Div_mem(div, new_mem);
4089 * Transform a Proj(Mod) with a non-zero value.
4090 * Removes the exceptions and routes the memory to the NoMem node.
4092 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4094 ir_node *mod = get_Proj_pred(proj);
4095 ir_node *b = get_Mod_right(mod);
4096 ir_node *res, *new_mem;
4097 const ir_node *confirm;
4100 if (value_not_zero(b, &confirm)) {
4101 /* mod(x, y) && y != 0 */
4102 proj_nr = get_Proj_proj(proj);
4104 if (confirm == NULL) {
4105 /* we are sure we have a Const != 0 */
4106 new_mem = get_Mod_mem(mod);
4107 new_mem = skip_Pin(new_mem);
4108 set_Mod_mem(mod, new_mem);
4109 set_irn_pinned(mod, op_pin_state_floats);
4114 case pn_Mod_X_regular:
4115 return new_r_Jmp(get_nodes_block(mod));
4117 case pn_Mod_X_except: {
4118 ir_graph *irg = get_irn_irg(proj);
4119 /* we found an exception handler, remove it */
4120 DBG_OPT_EXC_REM(proj);
4121 return new_r_Bad(irg, mode_X);
4125 ir_graph *irg = get_irn_irg(proj);
4126 res = get_Mod_mem(mod);
4127 new_mem = get_irg_no_mem(irg);
4130 /* This node can only float up to the Confirm block */
4131 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4133 /* this is a Mod without exception, we can remove the memory edge */
4134 set_Mod_mem(mod, new_mem);
4138 if (get_Mod_left(mod) == b) {
4139 /* a % a = 0 if a != 0 */
4140 ir_graph *irg = get_irn_irg(proj);
4141 ir_mode *mode = get_irn_mode(proj);
4142 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4144 DBG_OPT_CSTEVAL(mod, res);
4153 * return true if the operation returns a value with exactly 1 bit set
4155 static bool is_single_bit(const ir_node *node)
4157 /* a first implementation, could be extended with vrp and others... */
4159 ir_node *shl_l = get_Shl_left(node);
4160 ir_mode *mode = get_irn_mode(node);
4161 int modulo = get_mode_modulo_shift(mode);
4162 /* this works if we shift a 1 and we have modulo shift */
4163 if (is_Const(shl_l) && is_Const_one(shl_l)
4164 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4167 } else if (is_Const(node)) {
4168 ir_tarval *tv = get_Const_tarval(node);
4169 return tarval_is_single_bit(tv);
4175 * checks if node just flips a bit in another node and returns that other node
4176 * if so. @p tv should be a value having just 1 bit set
4178 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4181 return get_Not_op(node);
4183 ir_node *right = get_Eor_right(node);
4184 if (is_Const(right)) {
4185 ir_tarval *right_tv = get_Const_tarval(right);
4186 ir_mode *mode = get_irn_mode(node);
4187 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4188 return get_Eor_left(node);
4195 * Normalizes and optimizes Cmp nodes.
4197 static ir_node *transform_node_Cmp(ir_node *n)
4199 ir_node *left = get_Cmp_left(n);
4200 ir_node *right = get_Cmp_right(n);
4201 ir_mode *mode = get_irn_mode(left);
4202 ir_tarval *tv = NULL;
4203 bool changed = false;
4204 bool changedc = false;
4205 ir_relation relation = get_Cmp_relation(n);
4206 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4208 /* mask out impossible relations */
4209 ir_relation new_relation = relation & possible;
4210 if (new_relation != relation) {
4211 relation = new_relation;
4215 /* Remove unnecessary conversions */
4216 if (is_Conv(left) && is_Conv(right)) {
4217 ir_node *op_left = get_Conv_op(left);
4218 ir_node *op_right = get_Conv_op(right);
4219 ir_mode *mode_left = get_irn_mode(op_left);
4220 ir_mode *mode_right = get_irn_mode(op_right);
4222 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4223 && mode_left != mode_b && mode_right != mode_b) {
4224 ir_node *block = get_nodes_block(n);
4226 if (mode_left == mode_right) {
4230 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4231 } else if (smaller_mode(mode_left, mode_right)) {
4232 left = new_r_Conv(block, op_left, mode_right);
4235 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4236 } else if (smaller_mode(mode_right, mode_left)) {
4238 right = new_r_Conv(block, op_right, mode_left);
4240 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4242 mode = get_irn_mode(left);
4245 if (is_Conv(left) && is_Const(right)) {
4246 ir_node *op_left = get_Conv_op(left);
4247 ir_mode *mode_left = get_irn_mode(op_left);
4248 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4249 ir_tarval *tv = get_Const_tarval(right);
4250 tarval_int_overflow_mode_t last_mode
4251 = tarval_get_integer_overflow_mode();
4253 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4254 new_tv = tarval_convert_to(tv, mode_left);
4255 tarval_set_integer_overflow_mode(last_mode);
4256 if (new_tv != tarval_bad) {
4257 ir_graph *irg = get_irn_irg(n);
4259 right = new_r_Const(irg, new_tv);
4260 mode = get_irn_mode(left);
4262 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4268 * Optimize -a CMP -b into b CMP a.
4269 * This works only for modes where unary Minus cannot Overflow.
4270 * Note that two-complement integers can Overflow so it will NOT work.
4272 if (!mode_overflow_on_unary_Minus(mode) &&
4273 is_Minus(left) && is_Minus(right)) {
4274 left = get_Minus_op(left);
4275 right = get_Minus_op(right);
4276 relation = get_inversed_relation(relation);
4278 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4281 /* remove operation on both sides if possible */
4282 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4284 * The following operations are NOT safe for floating point operations, for instance
4285 * 1.0 + inf == 2.0 + inf, =/=> x == y
4287 if (mode_is_int(mode)) {
4288 unsigned lop = get_irn_opcode(left);
4290 if (lop == get_irn_opcode(right)) {
4291 ir_node *ll, *lr, *rl, *rr;
4293 /* same operation on both sides, try to remove */
4297 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4298 left = get_unop_op(left);
4299 right = get_unop_op(right);
4301 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4304 ll = get_Add_left(left);
4305 lr = get_Add_right(left);
4306 rl = get_Add_left(right);
4307 rr = get_Add_right(right);
4310 /* X + a CMP X + b ==> a CMP b */
4314 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4315 } else if (ll == rr) {
4316 /* X + a CMP b + X ==> a CMP b */
4320 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4321 } else if (lr == rl) {
4322 /* a + X CMP X + b ==> a CMP b */
4326 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4327 } else if (lr == rr) {
4328 /* a + X CMP b + X ==> a CMP b */
4332 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4336 ll = get_Sub_left(left);
4337 lr = get_Sub_right(left);
4338 rl = get_Sub_left(right);
4339 rr = get_Sub_right(right);
4342 /* X - a CMP X - b ==> a CMP b */
4346 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4347 } else if (lr == rr) {
4348 /* a - X CMP b - X ==> a CMP b */
4352 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4356 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4357 /* a ROTL X CMP b ROTL X ==> a CMP b */
4358 left = get_Rotl_left(left);
4359 right = get_Rotl_left(right);
4361 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4369 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4370 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4371 ir_node *ll = get_binop_left(left);
4372 ir_node *lr = get_binop_right(left);
4374 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4380 ir_graph *irg = get_irn_irg(n);
4382 right = create_zero_const(irg, mode);
4384 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4387 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4388 ir_node *rl = get_binop_left(right);
4389 ir_node *rr = get_binop_right(right);
4391 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4397 ir_graph *irg = get_irn_irg(n);
4399 right = create_zero_const(irg, mode);
4401 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4405 if (is_And(left) && is_Const(right)) {
4406 ir_node *ll = get_binop_left(left);
4407 ir_node *lr = get_binop_right(left);
4408 if (is_Shr(ll) && is_Const(lr)) {
4409 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4410 ir_node *block = get_nodes_block(n);
4411 ir_mode *mode = get_irn_mode(left);
4413 ir_node *llr = get_Shr_right(ll);
4414 if (is_Const(llr)) {
4415 dbg_info *dbg = get_irn_dbg_info(left);
4416 ir_graph *irg = get_irn_irg(left);
4418 ir_tarval *c1 = get_Const_tarval(llr);
4419 ir_tarval *c2 = get_Const_tarval(lr);
4420 ir_tarval *c3 = get_Const_tarval(right);
4421 ir_tarval *mask = tarval_shl(c2, c1);
4422 ir_tarval *value = tarval_shl(c3, c1);
4424 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4425 right = new_r_Const(irg, value);
4430 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4432 if (is_Const(right) && is_Const_null(right) &&
4433 (is_Eor(left) || is_Or_Eor_Add(left))) {
4434 right = get_Eor_right(left);
4435 left = get_Eor_left(left);
4441 if (mode_is_int(mode) && is_And(left)) {
4442 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4443 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4444 if (relation == ir_relation_equal
4445 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4446 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4447 ir_node *and0 = get_And_left(left);
4448 ir_node *and1 = get_And_right(left);
4449 if (and1 == right) {
4450 ir_node *tmp = and0;
4454 if (and0 == right && is_single_bit(and0)) {
4455 ir_graph *irg = get_irn_irg(n);
4457 relation == ir_relation_equal ? ir_relation_less_greater
4458 : ir_relation_equal;
4459 right = create_zero_const(irg, mode);
4465 if (is_Const(right) && is_Const_null(right) &&
4466 (relation == ir_relation_equal
4467 || (relation == ir_relation_less_greater)
4468 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4470 /* instead of flipping the bit before the bit-test operation negate
4472 ir_node *and0 = get_And_left(left);
4473 ir_node *and1 = get_And_right(left);
4474 if (is_Const(and1)) {
4475 ir_tarval *tv = get_Const_tarval(and1);
4476 if (tarval_is_single_bit(tv)) {
4477 ir_node *flipped = flips_bit(and0, tv);
4478 if (flipped != NULL) {
4479 dbg_info *dbgi = get_irn_dbg_info(left);
4480 ir_node *block = get_nodes_block(left);
4481 relation = get_negated_relation(relation);
4482 left = new_rd_And(dbgi, block, flipped, and1, mode);
4491 /* replace mode_b compares with ands/ors */
4492 if (mode == mode_b) {
4493 ir_node *block = get_nodes_block(n);
4497 case ir_relation_less_equal:
4498 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4500 case ir_relation_less:
4501 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4503 case ir_relation_greater_equal:
4504 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4506 case ir_relation_greater:
4507 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4509 case ir_relation_less_greater:
4510 bres = new_r_Eor(block, left, right, mode_b);
4512 case ir_relation_equal:
4513 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4516 #ifdef DEBUG_libfirm
4517 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4522 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4528 * First step: normalize the compare op
4529 * by placing the constant on the right side
4530 * or moving the lower address node to the left.
4532 if (!operands_are_normalized(left, right)) {
4537 relation = get_inversed_relation(relation);
4542 * Second step: Try to reduce the magnitude
4543 * of a constant. This may help to generate better code
4544 * later and may help to normalize more compares.
4545 * Of course this is only possible for integer values.
4547 tv = value_of(right);
4548 if (tv != tarval_bad) {
4549 ir_mode *mode = get_irn_mode(right);
4551 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4552 * cmp(ct,c2) | cmp(cf,c2) | result
4553 * -----------|------------|--------
4554 * true | true | True
4555 * false | false | False
4557 * false | true | not(x)
4560 ir_node *mux_true = get_Mux_true(left);
4561 ir_node *mux_false = get_Mux_false(left);
4562 if (is_Const(mux_true) && is_Const(mux_false)) {
4563 /* we can fold true/false constant separately */
4564 ir_tarval *tv_true = get_Const_tarval(mux_true);
4565 ir_tarval *tv_false = get_Const_tarval(mux_false);
4566 ir_relation r_true = tarval_cmp(tv_true, tv);
4567 ir_relation r_false = tarval_cmp(tv_false, tv);
4568 if (r_true != ir_relation_false
4569 || r_false != ir_relation_false) {
4570 bool rel_true = (r_true & relation) != 0;
4571 bool rel_false = (r_false & relation) != 0;
4572 ir_node *cond = get_Mux_sel(left);
4573 if (rel_true == rel_false) {
4574 relation = rel_true ? ir_relation_true
4575 : ir_relation_false;
4576 } else if (rel_true) {
4579 dbg_info *dbgi = get_irn_dbg_info(n);
4580 ir_node *block = get_nodes_block(n);
4581 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4588 /* TODO extend to arbitrary constants */
4589 if (is_Conv(left) && tarval_is_null(tv)) {
4590 ir_node *op = get_Conv_op(left);
4591 ir_mode *op_mode = get_irn_mode(op);
4594 * UpConv(x) REL 0 ==> x REL 0
4595 * Don't do this for float values as it's unclear whether it is a
4596 * win. (on the other side it makes detection/creation of fabs hard)
4598 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4599 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4600 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4601 !mode_is_float(mode)) {
4602 tv = get_mode_null(op_mode);
4606 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4610 if (tv != tarval_bad) {
4611 /* the following optimization is possible on modes without Overflow
4612 * on Unary Minus or on == and !=:
4613 * -a CMP c ==> a swap(CMP) -c
4615 * Beware: for two-complement Overflow may occur, so only == and != can
4616 * be optimized, see this:
4617 * -MININT < 0 =/=> MININT > 0 !!!
4619 if (is_Minus(left) &&
4620 (!mode_overflow_on_unary_Minus(mode) ||
4621 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4622 tv = tarval_neg(tv);
4624 if (tv != tarval_bad) {
4625 left = get_Minus_op(left);
4626 relation = get_inversed_relation(relation);
4628 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4630 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4631 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4632 tv = tarval_not(tv);
4634 if (tv != tarval_bad) {
4635 left = get_Not_op(left);
4637 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4641 /* for integer modes, we have more */
4642 if (mode_is_int(mode) && !is_Const(left)) {
4643 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4644 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4645 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4646 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4648 if (tv != tarval_bad) {
4649 relation ^= ir_relation_equal;
4651 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4654 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4655 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4656 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4657 tv = tarval_add(tv, get_mode_one(mode));
4659 if (tv != tarval_bad) {
4660 relation ^= ir_relation_equal;
4662 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4666 /* the following reassociations work only for == and != */
4667 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4668 if (tv != tarval_bad) {
4669 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4671 ir_node *c1 = get_Sub_right(left);
4672 ir_tarval *tv2 = value_of(c1);
4674 if (tv2 != tarval_bad) {
4675 tv2 = tarval_add(tv, value_of(c1));
4677 if (tv2 != tarval_bad) {
4678 left = get_Sub_left(left);
4681 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4685 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4686 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4687 ir_node *a_l = get_binop_left(left);
4688 ir_node *a_r = get_binop_right(left);
4692 if (is_Const(a_l)) {
4694 tv2 = value_of(a_l);
4697 tv2 = value_of(a_r);
4700 if (tv2 != tarval_bad) {
4701 tv2 = tarval_sub(tv, tv2, NULL);
4703 if (tv2 != tarval_bad) {
4707 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4711 /* -a == c ==> a == -c, -a != c ==> a != -c */
4712 else if (is_Minus(left)) {
4713 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4715 if (tv2 != tarval_bad) {
4716 left = get_Minus_op(left);
4719 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4726 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4727 switch (get_irn_opcode(left)) {
4731 c1 = get_And_right(left);
4734 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4735 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4737 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4739 /* TODO: move to constant evaluation */
4740 ir_graph *irg = get_irn_irg(n);
4741 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4742 c1 = new_r_Const(irg, tv);
4743 DBG_OPT_CSTEVAL(n, c1);
4747 if (tarval_is_single_bit(tv)) {
4749 * optimization for AND:
4751 * And(x, C) == C ==> And(x, C) != 0
4752 * And(x, C) != C ==> And(X, C) == 0
4754 * if C is a single Bit constant.
4757 /* check for Constant's match. We have check hare the tarvals,
4758 because our const might be changed */
4759 if (get_Const_tarval(c1) == tv) {
4760 /* fine: do the transformation */
4761 tv = get_mode_null(get_tarval_mode(tv));
4762 relation ^= ir_relation_less_equal_greater;
4764 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4770 c1 = get_Or_right(left);
4771 if (is_Const(c1) && tarval_is_null(tv)) {
4773 * Or(x, C) == 0 && C != 0 ==> FALSE
4774 * Or(x, C) != 0 && C != 0 ==> TRUE
4776 if (! tarval_is_null(get_Const_tarval(c1))) {
4777 /* TODO: move to constant evaluation */
4778 ir_graph *irg = get_irn_irg(n);
4779 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4780 c1 = new_r_Const(irg, tv);
4781 DBG_OPT_CSTEVAL(n, c1);
4788 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4790 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4793 c1 = get_Shl_right(left);
4795 ir_graph *irg = get_irn_irg(c1);
4796 ir_tarval *tv1 = get_Const_tarval(c1);
4797 ir_mode *mode = get_irn_mode(left);
4798 ir_tarval *minus1 = get_mode_all_one(mode);
4799 ir_tarval *amask = tarval_shr(minus1, tv1);
4800 ir_tarval *cmask = tarval_shl(minus1, tv1);
4803 if (tarval_and(tv, cmask) != tv) {
4804 /* condition not met */
4805 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4806 c1 = new_r_Const(irg, tv);
4807 DBG_OPT_CSTEVAL(n, c1);
4810 sl = get_Shl_left(left);
4811 blk = get_nodes_block(n);
4812 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4813 tv = tarval_shr(tv, tv1);
4815 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4820 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4822 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4825 c1 = get_Shr_right(left);
4827 ir_graph *irg = get_irn_irg(c1);
4828 ir_tarval *tv1 = get_Const_tarval(c1);
4829 ir_mode *mode = get_irn_mode(left);
4830 ir_tarval *minus1 = get_mode_all_one(mode);
4831 ir_tarval *amask = tarval_shl(minus1, tv1);
4832 ir_tarval *cmask = tarval_shr(minus1, tv1);
4835 if (tarval_and(tv, cmask) != tv) {
4836 /* condition not met */
4837 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4838 c1 = new_r_Const(irg, tv);
4839 DBG_OPT_CSTEVAL(n, c1);
4842 sl = get_Shr_left(left);
4843 blk = get_nodes_block(n);
4844 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4845 tv = tarval_shl(tv, tv1);
4847 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4852 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4854 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4857 c1 = get_Shrs_right(left);
4859 ir_graph *irg = get_irn_irg(c1);
4860 ir_tarval *tv1 = get_Const_tarval(c1);
4861 ir_mode *mode = get_irn_mode(left);
4862 ir_tarval *minus1 = get_mode_all_one(mode);
4863 ir_tarval *amask = tarval_shl(minus1, tv1);
4864 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4867 cond = tarval_sub(cond, tv1, NULL);
4868 cond = tarval_shrs(tv, cond);
4870 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4871 /* condition not met */
4872 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4873 c1 = new_r_Const(irg, tv);
4874 DBG_OPT_CSTEVAL(n, c1);
4877 sl = get_Shrs_left(left);
4878 blk = get_nodes_block(n);
4879 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4880 tv = tarval_shl(tv, tv1);
4882 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4890 if (changedc) { /* need a new Const */
4891 ir_graph *irg = get_irn_irg(n);
4892 right = new_r_Const(irg, tv);
4896 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4897 ir_node *op = get_Proj_pred(left);
4899 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4900 ir_node *c = get_binop_right(op);
4903 ir_tarval *tv = get_Const_tarval(c);
4905 if (tarval_is_single_bit(tv)) {
4906 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4907 ir_node *v = get_binop_left(op);
4908 ir_node *blk = get_nodes_block(op);
4909 ir_graph *irg = get_irn_irg(op);
4910 ir_mode *mode = get_irn_mode(v);
4912 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4913 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4915 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4922 dbg_info *dbgi = get_irn_dbg_info(n);
4923 ir_node *block = get_nodes_block(n);
4925 /* create a new compare */
4926 n = new_rd_Cmp(dbgi, block, left, right, relation);
4933 * Optimize CopyB(mem, x, x) into a Nop.
4935 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4937 ir_node *copyb = get_Proj_pred(proj);
4938 ir_node *a = get_CopyB_dst(copyb);
4939 ir_node *b = get_CopyB_src(copyb);
4942 switch (get_Proj_proj(proj)) {
4943 case pn_CopyB_X_regular:
4944 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4945 DBG_OPT_EXC_REM(proj);
4946 proj = new_r_Jmp(get_nodes_block(copyb));
4948 case pn_CopyB_X_except: {
4949 ir_graph *irg = get_irn_irg(proj);
4950 DBG_OPT_EXC_REM(proj);
4951 proj = new_r_Bad(irg, mode_X);
4962 * Optimize Bounds(idx, idx, upper) into idx.
4964 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4966 ir_node *oldn = proj;
4967 ir_node *bound = get_Proj_pred(proj);
4968 ir_node *idx = get_Bound_index(bound);
4969 ir_node *pred = skip_Proj(idx);
4972 if (idx == get_Bound_lower(bound))
4974 else if (is_Bound(pred)) {
4976 * idx was Bounds checked previously, it is still valid if
4977 * lower <= pred_lower && pred_upper <= upper.
4979 ir_node *lower = get_Bound_lower(bound);
4980 ir_node *upper = get_Bound_upper(bound);
4981 if (get_Bound_lower(pred) == lower &&
4982 get_Bound_upper(pred) == upper) {
4984 * One could expect that we simply return the previous
4985 * Bound here. However, this would be wrong, as we could
4986 * add an exception Proj to a new location then.
4987 * So, we must turn in into a tuple.
4993 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4994 switch (get_Proj_proj(proj)) {
4996 DBG_OPT_EXC_REM(proj);
4997 proj = get_Bound_mem(bound);
4999 case pn_Bound_X_except:
5000 DBG_OPT_EXC_REM(proj);
5001 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5005 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5007 case pn_Bound_X_regular:
5008 DBG_OPT_EXC_REM(proj);
5009 proj = new_r_Jmp(get_nodes_block(bound));
5019 * Does all optimizations on nodes that must be done on its Projs
5020 * because of creating new nodes.
5022 static ir_node *transform_node_Proj(ir_node *proj)
5024 ir_node *n = get_Proj_pred(proj);
5026 if (n->op->ops.transform_node_Proj)
5027 return n->op->ops.transform_node_Proj(proj);
5032 * Test whether a block is unreachable
5033 * Note: That this only returns true when
5034 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
5035 * This is important, as you easily end up producing invalid constructs in the
5036 * unreachable code when optimizing away edges into the unreachable code.
5037 * So only set this flag when you iterate localopts to the fixpoint.
5038 * When you reach the fixpoint then all unreachable code is dead
5039 * (= can't be reached by firm edges) and you won't see the invalid constructs
5042 static bool is_block_unreachable(const ir_node *block)
5044 const ir_graph *irg = get_irn_irg(block);
5045 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
5047 return get_Block_dom_depth(block) < 0;
5050 static ir_node *transform_node_Block(ir_node *block)
5052 ir_graph *irg = get_irn_irg(block);
5053 int arity = get_irn_arity(block);
5054 ir_node *bad = NULL;
5057 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
5060 for (i = 0; i < arity; ++i) {
5061 ir_node *const pred = get_Block_cfgpred(block, i);
5062 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5065 bad = new_r_Bad(irg, mode_X);
5066 set_irn_n(block, i, bad);
5072 static ir_node *transform_node_Phi(ir_node *phi)
5074 int n = get_irn_arity(phi);
5075 ir_mode *mode = get_irn_mode(phi);
5076 ir_node *block = get_nodes_block(phi);
5077 ir_graph *irg = get_irn_irg(phi);
5078 ir_node *bad = NULL;
5081 /* Set phi-operands for bad-block inputs to bad */
5082 for (i = 0; i < n; ++i) {
5083 if (!is_Bad(get_Phi_pred(phi, i))) {
5084 ir_node *pred = get_Block_cfgpred(block, i);
5085 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5087 bad = new_r_Bad(irg, mode);
5088 set_irn_n(phi, i, bad);
5093 /* Move Pin nodes down through Phi nodes. */
5094 if (mode == mode_M) {
5095 n = get_irn_arity(phi);
5097 /* Beware of Phi0 */
5101 bool has_pin = false;
5103 NEW_ARR_A(ir_node *, in, n);
5105 for (i = 0; i < n; ++i) {
5106 ir_node *pred = get_irn_n(phi, i);
5109 in[i] = get_Pin_op(pred);
5111 } else if (is_Bad(pred)) {
5121 /* Move the Pin nodes "behind" the Phi. */
5122 new_phi = new_r_Phi(block, n, in, mode_M);
5123 return new_r_Pin(block, new_phi);
5126 /* Move Confirms down through Phi nodes. */
5127 else if (mode_is_reference(mode)) {
5128 n = get_irn_arity(phi);
5130 /* Beware of Phi0 */
5132 ir_node *pred = get_irn_n(phi, 0);
5133 ir_node *bound, *new_phi, *block, **in;
5134 ir_relation relation;
5135 bool has_confirm = false;
5137 if (! is_Confirm(pred))
5140 bound = get_Confirm_bound(pred);
5141 relation = get_Confirm_relation(pred);
5143 NEW_ARR_A(ir_node *, in, n);
5144 in[0] = get_Confirm_value(pred);
5146 for (i = 1; i < n; ++i) {
5147 pred = get_irn_n(phi, i);
5149 if (is_Confirm(pred) &&
5150 get_Confirm_bound(pred) == bound &&
5151 get_Confirm_relation(pred) == relation) {
5152 in[i] = get_Confirm_value(pred);
5154 } else if (is_Bad(pred)) {
5164 /* move the Confirm nodes "behind" the Phi */
5165 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5166 return new_r_Confirm(block, new_phi, bound, relation);
5173 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5175 * Should be moved to reassociation?
5177 static ir_node *transform_node_shift(ir_node *n)
5179 ir_node *left, *right;
5181 ir_mode *count_mode;
5182 ir_tarval *tv1, *tv2, *res;
5183 ir_node *in[2], *irn, *block;
5187 left = get_binop_left(n);
5189 /* different operations */
5190 if (get_irn_op(left) != get_irn_op(n))
5193 right = get_binop_right(n);
5194 tv1 = value_of(right);
5195 if (tv1 == tarval_bad)
5198 tv2 = value_of(get_binop_right(left));
5199 if (tv2 == tarval_bad)
5202 count_mode = get_tarval_mode(tv1);
5203 if (get_tarval_mode(tv2) != count_mode) {
5204 /* TODO: search bigger mode or something and convert... */
5208 mode = get_irn_mode(n);
5209 modulo_shf = get_mode_modulo_shift(mode);
5211 if (modulo_shf > 0) {
5212 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5214 /* I'm not so sure what happens in one complement... */
5215 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5216 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5217 * above will be invalid) */
5218 assert(modulo_shf<=0 || is_po2(modulo_shf));
5220 tv1 = tarval_and(tv1, modulo_mask);
5221 tv2 = tarval_and(tv2, modulo_mask);
5223 res = tarval_add(tv1, tv2);
5224 irg = get_irn_irg(n);
5226 /* beware: a simple replacement works only, if res < modulo shift */
5228 int bits = get_mode_size_bits(mode);
5229 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5230 res = tarval_mod(res, modulo);
5232 long bits = get_mode_size_bits(mode);
5233 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5235 /* shifting too much */
5236 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5238 ir_node *block = get_nodes_block(n);
5239 dbg_info *dbgi = get_irn_dbg_info(n);
5240 ir_mode *smode = get_irn_mode(right);
5241 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5242 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5245 return new_r_Const(irg, get_mode_null(mode));
5249 /* ok, we can replace it */
5250 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5251 block = get_nodes_block(n);
5253 in[0] = get_binop_left(left);
5254 in[1] = new_r_Const(irg, res);
5256 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5258 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5265 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5267 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5268 * (also with x >>s c1 when c1>=c2)
5270 static ir_node *transform_node_shl_shr(ir_node *n)
5273 ir_node *right = get_binop_right(n);
5283 ir_tarval *tv_shift;
5286 ir_relation relation;
5289 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5291 if (!is_Const(right))
5294 left = get_binop_left(n);
5295 mode = get_irn_mode(n);
5296 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5297 ir_node *shr_right = get_binop_right(left);
5299 if (!is_Const(shr_right))
5302 x = get_binop_left(left);
5303 tv_shr = get_Const_tarval(shr_right);
5304 tv_shl = get_Const_tarval(right);
5306 if (is_Shrs(left)) {
5307 /* shrs variant only allowed if c1 >= c2 */
5308 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5311 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5314 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5316 tv_mask = tarval_shl(tv_mask, tv_shl);
5317 } else if (is_Shr(n) && is_Shl(left)) {
5318 ir_node *shl_right = get_Shl_right(left);
5320 if (!is_Const(shl_right))
5323 x = get_Shl_left(left);
5324 tv_shr = get_Const_tarval(right);
5325 tv_shl = get_Const_tarval(shl_right);
5327 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5328 tv_mask = tarval_shr(tv_mask, tv_shr);
5333 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5334 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5337 assert(tv_mask != tarval_bad);
5338 assert(get_tarval_mode(tv_mask) == mode);
5340 block = get_nodes_block(n);
5341 irg = get_irn_irg(block);
5342 dbgi = get_irn_dbg_info(n);
5344 relation = tarval_cmp(tv_shl, tv_shr);
5345 if (relation == ir_relation_less || relation == ir_relation_equal) {
5346 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5347 new_const = new_r_Const(irg, tv_shift);
5349 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5351 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5354 assert(relation == ir_relation_greater);
5355 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5356 new_const = new_r_Const(irg, tv_shift);
5357 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5360 new_const = new_r_Const(irg, tv_mask);
5361 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5366 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5368 ir_mode *mode = get_tarval_mode(tv);
5369 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5370 return tarval_mod(tv, modulo_tv);
5373 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5374 ir_node *left, ir_node *right, ir_mode *mode);
5377 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5378 * then we can use that to minimize the value of Add(x, const) or
5379 * Sub(Const, x). In particular this often avoids 1 instruction in some
5380 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5381 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5383 static ir_node *transform_node_shift_modulo(ir_node *n,
5384 new_shift_func new_shift)
5386 ir_mode *mode = get_irn_mode(n);
5387 int modulo = get_mode_modulo_shift(mode);
5388 ir_node *newop = NULL;
5389 ir_mode *mode_right;
5396 if (get_mode_arithmetic(mode) != irma_twos_complement)
5398 if (!is_po2(modulo))
5401 irg = get_irn_irg(n);
5402 block = get_nodes_block(n);
5403 right = get_binop_right(n);
5404 mode_right = get_irn_mode(right);
5405 if (is_Const(right)) {
5406 ir_tarval *tv = get_Const_tarval(right);
5407 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5412 newop = new_r_Const(irg, tv_mod);
5413 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5414 ir_node *add_right = get_binop_right(right);
5415 if (is_Const(add_right)) {
5416 ir_tarval *tv = get_Const_tarval(add_right);
5417 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5422 newconst = new_r_Const(irg, tv_mod);
5423 newop = new_r_Add(block, get_binop_left(right), newconst,
5426 } else if (is_Sub(right)) {
5427 ir_node *sub_left = get_Sub_left(right);
5428 if (is_Const(sub_left)) {
5429 ir_tarval *tv = get_Const_tarval(sub_left);
5430 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5435 newconst = new_r_Const(irg, tv_mod);
5436 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5443 if (newop != NULL) {
5444 dbg_info *dbgi = get_irn_dbg_info(n);
5445 ir_node *left = get_binop_left(n);
5446 return new_shift(dbgi, block, left, newop, mode);
5454 static ir_node *transform_node_Shr(ir_node *n)
5456 ir_node *c, *oldn = n;
5457 ir_node *left = get_Shr_left(n);
5458 ir_node *right = get_Shr_right(n);
5459 ir_mode *mode = get_irn_mode(n);
5461 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5462 n = transform_node_shift(n);
5465 n = transform_node_shift_modulo(n, new_rd_Shr);
5467 n = transform_node_shl_shr(n);
5469 n = transform_node_shift_bitop(n);
5477 static ir_node *transform_node_Shrs(ir_node *n)
5480 ir_node *a = get_Shrs_left(n);
5481 ir_node *b = get_Shrs_right(n);
5482 ir_mode *mode = get_irn_mode(n);
5486 if (is_oversize_shift(n)) {
5487 ir_node *block = get_nodes_block(n);
5488 dbg_info *dbgi = get_irn_dbg_info(n);
5489 ir_mode *cmode = get_irn_mode(b);
5490 long val = get_mode_size_bits(cmode)-1;
5491 ir_graph *irg = get_irn_irg(n);
5492 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5493 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5496 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5497 n = transform_node_shift(n);
5501 n = transform_node_shift_modulo(n, new_rd_Shrs);
5504 n = transform_node_shift_bitop(n);
5508 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5509 attr = vrp_get_info(a);
5511 unsigned bits = get_mode_size_bits(mode);
5512 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5513 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5514 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5515 dbg_info *dbgi = get_irn_dbg_info(n);
5516 ir_node *block = get_nodes_block(n);
5517 return new_rd_Shr(dbgi, block, a, b, mode);
5527 static ir_node *transform_node_Shl(ir_node *n)
5529 ir_node *c, *oldn = n;
5530 ir_node *a = get_Shl_left(n);
5531 ir_node *b = get_Shl_right(n);
5532 ir_mode *mode = get_irn_mode(n);
5534 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5535 n = transform_node_shift(n);
5538 n = transform_node_shift_modulo(n, new_rd_Shl);
5540 n = transform_node_shl_shr(n);
5542 n = transform_node_shift_bitop(n);
5550 static ir_node *transform_node_Rotl(ir_node *n)
5552 ir_node *c, *oldn = n;
5553 ir_node *a = get_Rotl_left(n);
5554 ir_node *b = get_Rotl_right(n);
5555 ir_mode *mode = get_irn_mode(n);
5557 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5558 n = transform_node_shift(n);
5561 n = transform_node_shift_bitop(n);
5569 static ir_node *transform_node_Conv(ir_node *n)
5571 ir_node *c, *oldn = n;
5572 ir_mode *mode = get_irn_mode(n);
5573 ir_node *a = get_Conv_op(n);
5575 if (mode != mode_b && is_const_Phi(a)) {
5576 /* Do NOT optimize mode_b Conv's, this leads to remaining
5577 * Phib nodes later, because the conv_b_lower operation
5578 * is instantly reverted, when it tries to insert a Convb.
5580 c = apply_conv_on_phi(a, mode);
5582 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5587 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5588 ir_graph *irg = get_irn_irg(n);
5589 return new_r_Unknown(irg, mode);
5592 if (mode_is_reference(mode) &&
5593 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5595 ir_node *l = get_Add_left(a);
5596 ir_node *r = get_Add_right(a);
5597 dbg_info *dbgi = get_irn_dbg_info(a);
5598 ir_node *block = get_nodes_block(n);
5600 ir_node *lop = get_Conv_op(l);
5601 if (get_irn_mode(lop) == mode) {
5602 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5603 n = new_rd_Add(dbgi, block, lop, r, mode);
5608 ir_node *rop = get_Conv_op(r);
5609 if (get_irn_mode(rop) == mode) {
5610 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5611 n = new_rd_Add(dbgi, block, l, rop, mode);
5621 * Remove dead blocks and nodes in dead blocks
5622 * in keep alive list. We do not generate a new End node.
5624 static ir_node *transform_node_End(ir_node *n)
5626 int i, j, n_keepalives = get_End_n_keepalives(n);
5629 NEW_ARR_A(ir_node *, in, n_keepalives);
5631 for (i = j = 0; i < n_keepalives; ++i) {
5632 ir_node *ka = get_End_keepalive(n, i);
5634 /* no need to keep Bad */
5637 /* do not keep unreachable code */
5638 block = is_Block(ka) ? ka : get_nodes_block(ka);
5639 if (is_block_unreachable(block))
5643 if (j != n_keepalives)
5644 set_End_keepalives(n, j, in);
5648 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5650 if (is_Minus(a) && get_Minus_op(a) == b)
5652 if (is_Minus(b) && get_Minus_op(b) == a)
5654 if (is_Sub(a) && is_Sub(b)) {
5655 ir_node *a_left = get_Sub_left(a);
5656 ir_node *a_right = get_Sub_right(a);
5657 ir_node *b_left = get_Sub_left(b);
5658 ir_node *b_right = get_Sub_right(b);
5660 if (a_left == b_right && a_right == b_left)
5667 static const ir_node *skip_upconv(const ir_node *node)
5669 while (is_Conv(node)) {
5670 ir_mode *mode = get_irn_mode(node);
5671 const ir_node *op = get_Conv_op(node);
5672 ir_mode *op_mode = get_irn_mode(op);
5673 if (!smaller_mode(op_mode, mode))
5680 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5681 const ir_node *mux_true)
5686 ir_relation relation;
5692 * Note further that these optimization work even for floating point
5693 * with NaN's because -NaN == NaN.
5694 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5697 mode = get_irn_mode(mux_true);
5698 if (mode_honor_signed_zeros(mode))
5701 /* must be <, <=, >=, > */
5702 relation = get_Cmp_relation(sel);
5703 if ((relation & ir_relation_less_greater) == 0)
5706 if (!ir_is_negated_value(mux_true, mux_false))
5709 mux_true = skip_upconv(mux_true);
5710 mux_false = skip_upconv(mux_false);
5712 /* must be x cmp 0 */
5713 cmp_right = get_Cmp_right(sel);
5714 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5717 cmp_left = get_Cmp_left(sel);
5718 if (cmp_left == mux_false) {
5719 if (relation & ir_relation_less) {
5722 assert(relation & ir_relation_greater);
5725 } else if (cmp_left == mux_true) {
5726 if (relation & ir_relation_less) {
5729 assert(relation & ir_relation_greater);
5737 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5740 ir_node *cmp_left = get_Cmp_left(sel);
5741 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5744 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5745 const ir_node *mux_true)
5747 /* this code should return true each time transform_node_Mux would
5748 * optimize the Mux completely away */
5750 ir_mode *mode = get_irn_mode(mux_false);
5751 if (get_mode_arithmetic(mode) == irma_twos_complement
5752 && ir_mux_is_abs(sel, mux_false, mux_true))
5755 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5756 const ir_node *cmp_r = get_Cmp_right(sel);
5757 const ir_node *cmp_l = get_Cmp_left(sel);
5758 const ir_node *f = mux_false;
5759 const ir_node *t = mux_true;
5761 if (is_Const(t) && is_Const_null(t)) {
5766 if (is_And(cmp_l) && f == cmp_r) {
5767 ir_node *and_r = get_And_right(cmp_l);
5770 if (and_r == t && is_single_bit(and_r))
5772 and_l = get_And_left(cmp_l);
5773 if (and_l == t && is_single_bit(and_l))
5782 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5784 static ir_node *transform_Mux_set(ir_node *n)
5786 ir_node *cond = get_Mux_sel(n);
5791 ir_relation relation;
5805 left = get_Cmp_left(cond);
5806 mode = get_irn_mode(left);
5807 if (!mode_is_int(mode) && !mode_is_reference(mode))
5809 dest_mode = get_irn_mode(n);
5810 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5812 right = get_Cmp_right(cond);
5813 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5814 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5815 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5816 && relation != ir_relation_greater))
5821 case ir_relation_less:
5822 /* a < b -> (a - b) >> 31 */
5826 case ir_relation_less_equal:
5827 /* a <= b -> ~(a - b) >> 31 */
5832 case ir_relation_greater:
5833 /* a > b -> (b - a) >> 31 */
5837 case ir_relation_greater_equal:
5838 /* a >= b -> ~(a - b) >> 31 */
5847 dbgi = get_irn_dbg_info(n);
5848 block = get_nodes_block(n);
5849 irg = get_irn_irg(block);
5850 bits = get_mode_size_bits(dest_mode);
5851 tv = new_tarval_from_long(bits-1, mode_Iu);
5852 shift_cnt = new_rd_Const(dbgi, irg, tv);
5854 if (mode != dest_mode) {
5855 a = new_rd_Conv(dbgi, block, a, dest_mode);
5856 b = new_rd_Conv(dbgi, block, b, dest_mode);
5859 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5861 res = new_rd_Not(dbgi, block, res, dest_mode);
5863 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5868 * Optimize a Mux into some simpler cases.
5870 static ir_node *transform_node_Mux(ir_node *n)
5873 ir_node *sel = get_Mux_sel(n);
5874 ir_mode *mode = get_irn_mode(n);
5875 ir_node *t = get_Mux_true(n);
5876 ir_node *f = get_Mux_false(n);
5877 ir_graph *irg = get_irn_irg(n);
5879 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5880 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5881 int abs = ir_mux_is_abs(sel, f, t);
5883 dbg_info *dbgi = get_irn_dbg_info(n);
5884 ir_node *block = get_nodes_block(n);
5885 ir_node *op = ir_get_abs_op(sel, f, t);
5886 int bits = get_mode_size_bits(mode);
5887 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5888 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5889 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5892 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5894 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5900 /* first normalization step: try to move a constant to the false side,
5901 * 0 preferred on false side too */
5902 if (is_Cmp(sel) && is_Const(t) &&
5903 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5904 dbg_info *seldbgi = get_irn_dbg_info(sel);
5905 ir_node *block = get_nodes_block(sel);
5906 ir_relation relation = get_Cmp_relation(sel);
5911 /* Mux(x, a, b) => Mux(not(x), b, a) */
5912 relation = get_negated_relation(relation);
5913 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5914 get_Cmp_right(sel), relation);
5915 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5918 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5919 n = transform_Mux_set(n);
5924 /* the following optimisations create new mode_b nodes, so only do them
5925 * before mode_b lowering */
5926 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5928 ir_node* block = get_nodes_block(n);
5930 ir_node* c1 = get_Mux_sel(t);
5931 ir_node* t1 = get_Mux_true(t);
5932 ir_node* f1 = get_Mux_false(t);
5934 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5935 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5936 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5937 return new_r_Mux(block, and_, f1, t1, mode);
5938 } else if (f == t1) {
5939 /* Mux(cond0, Mux(cond1, x, y), x) */
5940 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5941 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5942 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5943 return new_r_Mux(block, and_, t1, f1, mode);
5945 } else if (is_Mux(f)) {
5946 ir_node* block = get_nodes_block(n);
5948 ir_node* c1 = get_Mux_sel(f);
5949 ir_node* t1 = get_Mux_true(f);
5950 ir_node* f1 = get_Mux_false(f);
5952 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5953 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5954 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5955 return new_r_Mux(block, or_, f1, t1, mode);
5956 } else if (t == f1) {
5957 /* Mux(cond0, x, Mux(cond1, y, x)) */
5958 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5959 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5960 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5961 return new_r_Mux(block, or_, t1, f1, mode);
5965 /* note: after normalization, false can only happen on default */
5966 if (mode == mode_b) {
5967 dbg_info *dbg = get_irn_dbg_info(n);
5968 ir_node *block = get_nodes_block(n);
5971 ir_tarval *tv_t = get_Const_tarval(t);
5972 if (tv_t == tarval_b_true) {
5974 /* Muxb(sel, true, false) = sel */
5975 assert(get_Const_tarval(f) == tarval_b_false);
5976 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5979 /* Muxb(sel, true, x) = Or(sel, x) */
5980 n = new_rd_Or(dbg, block, sel, f, mode_b);
5981 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5985 } else if (is_Const(f)) {
5986 ir_tarval *tv_f = get_Const_tarval(f);
5987 if (tv_f == tarval_b_true) {
5988 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5989 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5990 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5991 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5994 /* Muxb(sel, x, false) = And(sel, x) */
5995 assert(tv_f == tarval_b_false);
5996 n = new_rd_And(dbg, block, sel, t, mode_b);
5997 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6004 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6005 ir_relation relation = get_Cmp_relation(sel);
6006 ir_node *cmp_r = get_Cmp_right(sel);
6007 ir_node *cmp_l = get_Cmp_left(sel);
6008 ir_node *block = get_nodes_block(n);
6010 if (is_And(cmp_l) && f == cmp_r) {
6011 ir_node *and_r = get_And_right(cmp_l);
6014 if (and_r == t && is_single_bit(and_r)) {
6015 if (relation == ir_relation_equal) {
6016 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6017 n = new_rd_Eor(get_irn_dbg_info(n),
6018 block, cmp_l, t, mode);
6019 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6021 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6023 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6027 and_l = get_And_left(cmp_l);
6028 if (and_l == t && is_single_bit(and_l)) {
6029 if (relation == ir_relation_equal) {
6030 /* ((1 << n) & a) == 0, (1 << n), 0) */
6031 n = new_rd_Eor(get_irn_dbg_info(n),
6032 block, cmp_l, t, mode);
6033 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6035 /* ((1 << n) & a) != 0, (1 << n), 0) */
6037 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6048 * optimize Sync nodes that have other syncs as input we simply add the inputs
6049 * of the other sync to our own inputs
6051 static ir_node *transform_node_Sync(ir_node *n)
6053 int arity = get_Sync_n_preds(n);
6056 for (i = 0; i < arity;) {
6057 ir_node *pred = get_Sync_pred(n, i);
6061 /* Remove Bad predecessors */
6068 /* Remove duplicate predecessors */
6069 for (j = 0; j < i; ++j) {
6070 if (get_Sync_pred(n, j) == pred) {
6079 if (!is_Sync(pred)) {
6087 pred_arity = get_Sync_n_preds(pred);
6088 for (j = 0; j < pred_arity; ++j) {
6089 ir_node *pred_pred = get_Sync_pred(pred, j);
6094 add_irn_n(n, pred_pred);
6098 if (get_Sync_pred(n, k) == pred_pred)
6105 ir_graph *irg = get_irn_irg(n);
6106 return new_r_Bad(irg, mode_M);
6109 return get_Sync_pred(n, 0);
6112 /* rehash the sync node */
6117 static ir_node *transform_node_Load(ir_node *n)
6119 /* if our memory predecessor is a load from the same address, then reuse the
6120 * previous result */
6121 ir_node *mem = get_Load_mem(n);
6126 /* don't touch volatile loads */
6127 if (get_Load_volatility(n) == volatility_is_volatile)
6129 mem_pred = get_Proj_pred(mem);
6130 if (is_Load(mem_pred)) {
6131 ir_node *pred_load = mem_pred;
6133 /* conservatively compare the 2 loads. TODO: This could be less strict
6134 * with fixup code in some situations (like smaller/bigger modes) */
6135 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6137 if (get_Load_mode(pred_load) != get_Load_mode(n))
6139 /* all combinations of aligned/unaligned pred/n should be fine so we do
6140 * not compare the unaligned attribute */
6142 ir_node *block = get_nodes_block(n);
6143 ir_node *jmp = new_r_Jmp(block);
6144 ir_graph *irg = get_irn_irg(n);
6145 ir_node *bad = new_r_Bad(irg, mode_X);
6146 ir_mode *mode = get_Load_mode(n);
6147 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6148 ir_node *in[] = { mem, res, jmp, bad };
6149 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6152 } else if (is_Store(mem_pred)) {
6153 ir_node *pred_store = mem_pred;
6154 ir_node *value = get_Store_value(pred_store);
6156 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6158 if (get_irn_mode(value) != get_Load_mode(n))
6160 /* all combinations of aligned/unaligned pred/n should be fine so we do
6161 * not compare the unaligned attribute */
6163 ir_node *block = get_nodes_block(n);
6164 ir_node *jmp = new_r_Jmp(block);
6165 ir_graph *irg = get_irn_irg(n);
6166 ir_node *bad = new_r_Bad(irg, mode_X);
6167 ir_node *res = value;
6168 ir_node *in[] = { mem, res, jmp, bad };
6169 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6178 * optimize a trampoline Call into a direct Call
6180 static ir_node *transform_node_Call(ir_node *call)
6182 ir_node *callee = get_Call_ptr(call);
6183 ir_node *adr, *mem, *res, *bl, **in;
6184 ir_type *ctp, *mtp, *tp;
6188 size_t i, n_res, n_param;
6191 if (! is_Proj(callee))
6193 callee = get_Proj_pred(callee);
6194 if (! is_Builtin(callee))
6196 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6199 mem = get_Call_mem(call);
6201 if (skip_Proj(mem) == callee) {
6202 /* memory is routed to the trampoline, skip */
6203 mem = get_Builtin_mem(callee);
6206 /* build a new call type */
6207 mtp = get_Call_type(call);
6208 tdb = get_type_dbg_info(mtp);
6210 n_res = get_method_n_ress(mtp);
6211 n_param = get_method_n_params(mtp);
6212 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6214 for (i = 0; i < n_res; ++i)
6215 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6217 NEW_ARR_A(ir_node *, in, n_param + 1);
6219 /* FIXME: we don't need a new pointer type in every step */
6220 irg = get_irn_irg(call);
6221 tp = get_irg_frame_type(irg);
6222 tp = new_type_pointer(tp);
6223 set_method_param_type(ctp, 0, tp);
6225 in[0] = get_Builtin_param(callee, 2);
6226 for (i = 0; i < n_param; ++i) {
6227 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6228 in[i + 1] = get_Call_param(call, i);
6230 var = get_method_variadicity(mtp);
6231 set_method_variadicity(ctp, var);
6232 /* When we resolve a trampoline, the function must be called by a this-call */
6233 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6234 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6236 adr = get_Builtin_param(callee, 1);
6238 db = get_irn_dbg_info(call);
6239 bl = get_nodes_block(call);
6241 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6242 if (get_irn_pinned(call) == op_pin_state_floats)
6243 set_irn_pinned(res, op_pin_state_floats);
6248 * Tries several [inplace] [optimizing] transformations and returns an
6249 * equivalent node. The difference to equivalent_node() is that these
6250 * transformations _do_ generate new nodes, and thus the old node must
6251 * not be freed even if the equivalent node isn't the old one.
6253 static ir_node *transform_node(ir_node *n)
6259 iro = get_irn_opcode_(n);
6260 /* constant expression evaluation / constant folding */
6261 if (get_opt_constant_folding()) {
6262 /* neither constants nor Tuple values can be evaluated */
6263 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6264 /* try to evaluate */
6265 ir_tarval *tv = computed_value(n);
6266 if (tv != tarval_bad) {
6267 /* evaluation was successful -- replace the node. */
6268 ir_graph *irg = get_irn_irg(n);
6270 n = new_r_Const(irg, tv);
6272 DBG_OPT_CSTEVAL(old_n, n);
6278 /* remove unnecessary nodes */
6279 if (get_opt_constant_folding() ||
6280 (iro == iro_Phi) || /* always optimize these nodes. */
6281 (iro == iro_Id) || /* ... */
6282 (iro == iro_Proj) || /* ... */
6283 (iro == iro_Block)) { /* Flags tested local. */
6284 n = equivalent_node(n);
6289 /* Some more constant expression evaluation. */
6290 if (get_opt_algebraic_simplification() ||
6291 (iro == iro_Cond) ||
6292 (iro == iro_Proj)) { /* Flags tested local. */
6293 if (n->op->ops.transform_node != NULL) {
6294 n = n->op->ops.transform_node(n);
6304 static void register_computed_value_func(ir_op *op, computed_value_func func)
6306 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6307 op->ops.computed_value = func;
6310 static void register_computed_value_func_proj(ir_op *op,
6311 computed_value_func func)
6313 assert(op->ops.computed_value_Proj == NULL
6314 || op->ops.computed_value_Proj == func);
6315 op->ops.computed_value_Proj = func;
6318 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6320 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6321 op->ops.equivalent_node = func;
6324 static void register_equivalent_node_func_proj(ir_op *op,
6325 equivalent_node_func func)
6327 assert(op->ops.equivalent_node_Proj == NULL
6328 || op->ops.equivalent_node_Proj == func);
6329 op->ops.equivalent_node_Proj = func;
6332 static void register_transform_node_func(ir_op *op, transform_node_func func)
6334 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6335 op->ops.transform_node = func;
6338 static void register_transform_node_func_proj(ir_op *op,
6339 transform_node_func func)
6341 assert(op->ops.transform_node_Proj == NULL
6342 || op->ops.transform_node_Proj == func);
6343 op->ops.transform_node_Proj = func;
6346 void ir_register_opt_node_ops(void)
6348 register_computed_value_func(op_Add, computed_value_Add);
6349 register_computed_value_func(op_And, computed_value_And);
6350 register_computed_value_func(op_Borrow, computed_value_Borrow);
6351 register_computed_value_func(op_Carry, computed_value_Carry);
6352 register_computed_value_func(op_Cmp, computed_value_Cmp);
6353 register_computed_value_func(op_Confirm, computed_value_Confirm);
6354 register_computed_value_func(op_Const, computed_value_Const);
6355 register_computed_value_func(op_Conv, computed_value_Conv);
6356 register_computed_value_func(op_Eor, computed_value_Eor);
6357 register_computed_value_func(op_Minus, computed_value_Minus);
6358 register_computed_value_func(op_Mul, computed_value_Mul);
6359 register_computed_value_func(op_Mux, computed_value_Mux);
6360 register_computed_value_func(op_Not, computed_value_Not);
6361 register_computed_value_func(op_Or, computed_value_Or);
6362 register_computed_value_func(op_Proj, computed_value_Proj);
6363 register_computed_value_func(op_Rotl, computed_value_Rotl);
6364 register_computed_value_func(op_Shl, computed_value_Shl);
6365 register_computed_value_func(op_Shr, computed_value_Shr);
6366 register_computed_value_func(op_Shrs, computed_value_Shrs);
6367 register_computed_value_func(op_Sub, computed_value_Sub);
6368 register_computed_value_func(op_SymConst, computed_value_SymConst);
6369 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6370 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6372 register_equivalent_node_func(op_Add, equivalent_node_Add);
6373 register_equivalent_node_func(op_And, equivalent_node_And);
6374 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6375 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6376 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6377 register_equivalent_node_func(op_Id, equivalent_node_Id);
6378 register_equivalent_node_func(op_Minus, equivalent_node_idempotent_unop);
6379 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6380 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6381 register_equivalent_node_func(op_Not, equivalent_node_idempotent_unop);
6382 register_equivalent_node_func(op_Or, equivalent_node_Or);
6383 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6384 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6385 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6386 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6387 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6388 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6389 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6390 register_equivalent_node_func_proj(op_Bound, equivalent_node_Proj_Bound);
6391 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6392 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6393 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6395 register_transform_node_func(op_Add, transform_node_Add);
6396 register_transform_node_func(op_And, transform_node_And);
6397 register_transform_node_func(op_Block, transform_node_Block);
6398 register_transform_node_func(op_Call, transform_node_Call);
6399 register_transform_node_func(op_Cmp, transform_node_Cmp);
6400 register_transform_node_func(op_Cond, transform_node_Cond);
6401 register_transform_node_func(op_Conv, transform_node_Conv);
6402 register_transform_node_func(op_Div, transform_node_Div);
6403 register_transform_node_func(op_End, transform_node_End);
6404 register_transform_node_func(op_Eor, transform_node_Eor);
6405 register_transform_node_func(op_Load, transform_node_Load);
6406 register_transform_node_func(op_Minus, transform_node_Minus);
6407 register_transform_node_func(op_Mod, transform_node_Mod);
6408 register_transform_node_func(op_Mul, transform_node_Mul);
6409 register_transform_node_func(op_Mux, transform_node_Mux);
6410 register_transform_node_func(op_Not, transform_node_Not);
6411 register_transform_node_func(op_Or, transform_node_Or);
6412 register_transform_node_func(op_Phi, transform_node_Phi);
6413 register_transform_node_func(op_Proj, transform_node_Proj);
6414 register_transform_node_func(op_Rotl, transform_node_Rotl);
6415 register_transform_node_func(op_Shl, transform_node_Shl);
6416 register_transform_node_func(op_Shrs, transform_node_Shrs);
6417 register_transform_node_func(op_Shr, transform_node_Shr);
6418 register_transform_node_func(op_Sub, transform_node_Sub);
6419 register_transform_node_func(op_Switch, transform_node_Switch);
6420 register_transform_node_func(op_Sync, transform_node_Sync);
6421 register_transform_node_func_proj(op_Bound, transform_node_Proj_Bound);
6422 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6423 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6424 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6425 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6426 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6429 /* **************** Common Subexpression Elimination **************** */
6431 /** The size of the hash table used, should estimate the number of nodes
6433 #define N_IR_NODES 512
6435 int identities_cmp(const void *elt, const void *key)
6437 ir_node *a = (ir_node *)elt;
6438 ir_node *b = (ir_node *)key;
6441 if (a == b) return 0;
6443 if ((get_irn_op(a) != get_irn_op(b)) ||
6444 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6446 /* compare if a's in and b's in are of equal length */
6447 irn_arity_a = get_irn_arity(a);
6448 if (irn_arity_a != get_irn_arity(b))
6451 /* blocks are never the same */
6455 if (get_irn_pinned(a) == op_pin_state_pinned) {
6456 /* for pinned nodes, the block inputs must be equal */
6457 if (get_nodes_block(a) != get_nodes_block(b))
6460 ir_node *block_a = get_nodes_block(a);
6461 ir_node *block_b = get_nodes_block(b);
6462 if (! get_opt_global_cse()) {
6463 /* for block-local CSE both nodes must be in the same Block */
6464 if (block_a != block_b)
6467 /* The optimistic approach would be to do nothing here.
6468 * However doing GCSE optimistically produces a lot of partially dead code which appears
6469 * to be worse in practice than the missed opportunities.
6470 * So we use a very conservative variant here and only CSE if 1 value dominates the
6472 if (!block_dominates(block_a, block_b)
6473 && !block_dominates(block_b, block_a))
6478 /* compare a->in[0..ins] with b->in[0..ins] */
6479 for (i = 0; i < irn_arity_a; ++i) {
6480 ir_node *pred_a = get_irn_n(a, i);
6481 ir_node *pred_b = get_irn_n(b, i);
6482 if (pred_a != pred_b) {
6483 /* if both predecessors are CSE neutral they might be different */
6484 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6490 * here, we already now that the nodes are identical except their
6493 if (a->op->ops.node_cmp_attr)
6494 return a->op->ops.node_cmp_attr(a, b);
6499 unsigned ir_node_hash(const ir_node *node)
6501 return node->op->ops.hash(node);
6504 void new_identities(ir_graph *irg)
6506 if (irg->value_table != NULL)
6507 del_pset(irg->value_table);
6508 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6511 void del_identities(ir_graph *irg)
6513 if (irg->value_table != NULL)
6514 del_pset(irg->value_table);
6517 static int cmp_node_nr(const void *a, const void *b)
6519 ir_node **p1 = (ir_node**)a;
6520 ir_node **p2 = (ir_node**)b;
6521 long n1 = get_irn_node_nr(*p1);
6522 long n2 = get_irn_node_nr(*p2);
6523 return (n1>n2) - (n1<n2);
6526 void ir_normalize_node(ir_node *n)
6528 if (is_op_commutative(get_irn_op(n))) {
6529 ir_node *l = get_binop_left(n);
6530 ir_node *r = get_binop_right(n);
6532 /* For commutative operators perform a OP b == b OP a but keep
6533 * constants on the RIGHT side. This helps greatly in some
6534 * optimizations. Moreover we use the idx number to make the form
6536 if (!operands_are_normalized(l, r)) {
6537 set_binop_left(n, r);
6538 set_binop_right(n, l);
6541 } else if (is_Sync(n)) {
6542 /* we assume that most of the time the inputs of a Sync node are already
6543 * sorted, so check this first as a shortcut */
6544 bool ins_sorted = true;
6545 int arity = get_irn_arity(n);
6546 const ir_node *last = get_irn_n(n, 0);
6548 for (i = 1; i < arity; ++i) {
6549 const ir_node *node = get_irn_n(n, i);
6550 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6558 ir_node **ins = get_irn_in(n)+1;
6559 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6560 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6561 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6562 set_irn_in(n, arity, new_ins);
6568 ir_node *identify_remember(ir_node *n)
6570 ir_graph *irg = get_irn_irg(n);
6571 pset *value_table = irg->value_table;
6574 if (value_table == NULL)
6577 ir_normalize_node(n);
6578 /* lookup or insert in hash table with given hash key. */
6579 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6582 /* n is reachable again */
6583 edges_node_revival(nn);
6590 * During construction we set the op_pin_state_pinned flag in the graph right
6591 * when the optimization is performed. The flag turning on procedure global
6592 * cse could be changed between two allocations. This way we are safe.
6594 * @param n The node to lookup
6596 static inline ir_node *identify_cons(ir_node *n)
6600 n = identify_remember(n);
6601 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6602 ir_graph *irg = get_irn_irg(n);
6603 set_irg_pinned(irg, op_pin_state_floats);
6608 void add_identities(ir_node *node)
6615 identify_remember(node);
6618 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6620 ir_graph *rem = current_ir_graph;
6622 current_ir_graph = irg;
6623 foreach_pset(irg->value_table, ir_node, node) {
6626 current_ir_graph = rem;
6629 ir_node *optimize_node(ir_node *n)
6632 ir_graph *irg = get_irn_irg(n);
6633 unsigned iro = get_irn_opcode(n);
6636 /* Always optimize Phi nodes: part of the construction. */
6637 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6639 /* constant expression evaluation / constant folding */
6640 if (get_opt_constant_folding()) {
6641 /* neither constants nor Tuple values can be evaluated */
6642 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6643 /* try to evaluate */
6644 tv = computed_value(n);
6645 if (tv != tarval_bad) {
6650 * we MUST copy the node here temporarily, because it's still
6651 * needed for DBG_OPT_CSTEVAL
6653 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6654 oldn = (ir_node*)alloca(node_size);
6656 memcpy(oldn, n, node_size);
6657 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6659 /* ARG, copy the in array, we need it for statistics */
6660 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6662 /* note the inplace edges module */
6663 edges_node_deleted(n);
6665 /* evaluation was successful -- replace the node. */
6666 irg_kill_node(irg, n);
6667 nw = new_r_Const(irg, tv);
6669 DBG_OPT_CSTEVAL(oldn, nw);
6675 /* remove unnecessary nodes */
6676 if (get_opt_algebraic_simplification() ||
6677 (iro == iro_Phi) || /* always optimize these nodes. */
6679 (iro == iro_Proj) ||
6680 (iro == iro_Block) ) /* Flags tested local. */
6681 n = equivalent_node(n);
6683 /* Common Subexpression Elimination.
6685 * Checks whether n is already available.
6686 * The block input is used to distinguish different subexpressions. Right
6687 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6688 * subexpressions within a block.
6691 n = identify_cons(n);
6694 edges_node_deleted(oldn);
6696 /* We found an existing, better node, so we can deallocate the old node. */
6697 irg_kill_node(irg, oldn);
6701 /* Some more constant expression evaluation that does not allow to
6703 iro = get_irn_opcode(n);
6704 if (get_opt_algebraic_simplification() ||
6705 (iro == iro_Cond) ||
6706 (iro == iro_Proj)) { /* Flags tested local. */
6707 n = transform_node(n);
6710 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6711 if (get_opt_cse()) {
6713 n = identify_remember(o);
6721 ir_node *optimize_in_place_2(ir_node *n)
6723 if (!get_opt_optimize() && !is_Phi(n)) return n;
6728 /** common subexpression elimination **/
6729 /* Checks whether n is already available. */
6730 /* The block input is used to distinguish different subexpressions.
6731 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6732 * only finds common subexpressions within a block. */
6733 if (get_opt_cse()) {
6735 n = identify_remember(n);
6738 /* we have another existing node now, we do not optimize it here */
6743 n = transform_node(n);
6745 /* Now we can verify the node, as it has no dead inputs any more. */
6748 /* Now we have a legal, useful node. Enter it in hash table for cse.
6750 * Note: This is only necessary because some of the optimisations
6751 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6752 * bad practice and should be fixed sometime.
6754 if (get_opt_cse()) {
6756 n = identify_remember(o);
6764 ir_node *optimize_in_place(ir_node *n)
6766 ir_graph *irg = get_irn_irg(n);
6767 /* Handle graph state */
6768 assert(get_irg_phase_state(irg) != phase_building);
6770 if (get_opt_global_cse())
6771 set_irg_pinned(irg, op_pin_state_floats);
6773 /* FIXME: Maybe we could also test whether optimizing the node can
6774 change the control graph. */
6775 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6776 return optimize_in_place_2(n);