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
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
39 #include "iroptimize.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
59 static bool is_Or_Eor_Add(const ir_node *node)
61 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
62 ir_node *left = get_binop_left(node);
63 ir_node *right = get_binop_right(node);
64 vrp_attr *vrp_left = vrp_get_info(left);
65 vrp_attr *vrp_right = vrp_get_info(right);
66 if (vrp_left != NULL && vrp_right != NULL) {
68 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
69 return tarval_is_null(vrp_val);
76 * Returns the tarval of a Const node or tarval_bad for all other nodes.
78 static ir_tarval *default_value_of(const ir_node *n)
81 return get_Const_tarval(n); /* might return tarval_bad */
86 value_of_func value_of_ptr = default_value_of;
88 /* * Set a new value_of function. */
89 void set_value_of_func(value_of_func func)
94 value_of_ptr = default_value_of;
98 * Return the value of a Constant.
100 static ir_tarval *computed_value_Const(const ir_node *n)
102 return get_Const_tarval(n);
106 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
108 static ir_tarval *computed_value_SymConst(const ir_node *n)
113 switch (get_SymConst_kind(n)) {
114 case symconst_type_size:
115 type = get_SymConst_type(n);
116 if (get_type_state(type) == layout_fixed)
117 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
119 case symconst_type_align:
120 type = get_SymConst_type(n);
121 if (get_type_state(type) == layout_fixed)
122 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
124 case symconst_ofs_ent:
125 ent = get_SymConst_entity(n);
126 type = get_entity_owner(ent);
127 if (get_type_state(type) == layout_fixed)
128 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
137 * Return the value of an Add.
139 static ir_tarval *computed_value_Add(const ir_node *n)
141 ir_node *a = get_Add_left(n);
142 ir_node *b = get_Add_right(n);
144 ir_tarval *ta = value_of(a);
145 ir_tarval *tb = value_of(b);
147 if ((ta != tarval_bad) && (tb != tarval_bad))
148 return tarval_add(ta, tb);
151 if ((is_Not(a) && get_Not_op(a) == b)
152 || (is_Not(b) && get_Not_op(b) == a)) {
153 return get_mode_all_one(get_irn_mode(n));
160 * Return the value of a Sub.
161 * Special case: a - a
163 static ir_tarval *computed_value_Sub(const ir_node *n)
165 ir_mode *mode = get_irn_mode(n);
166 ir_node *a = get_Sub_left(n);
167 ir_node *b = get_Sub_right(n);
172 if (! mode_is_float(mode)) {
175 return get_mode_null(mode);
181 if ((ta != tarval_bad) && (tb != tarval_bad))
182 return tarval_sub(ta, tb, mode);
188 * Return the value of a Carry.
189 * Special : a op 0, 0 op b
191 static ir_tarval *computed_value_Carry(const ir_node *n)
193 ir_node *a = get_binop_left(n);
194 ir_node *b = get_binop_right(n);
195 ir_mode *m = get_irn_mode(n);
196 ir_tarval *ta = value_of(a);
197 ir_tarval *tb = value_of(b);
199 if ((ta != tarval_bad) && (tb != tarval_bad)) {
201 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
203 if (tarval_is_null(ta) || tarval_is_null(tb))
204 return get_mode_null(m);
210 * Return the value of a Borrow.
213 static ir_tarval *computed_value_Borrow(const ir_node *n)
215 ir_node *a = get_binop_left(n);
216 ir_node *b = get_binop_right(n);
217 ir_mode *m = get_irn_mode(n);
218 ir_tarval *ta = value_of(a);
219 ir_tarval *tb = value_of(b);
221 if ((ta != tarval_bad) && (tb != tarval_bad)) {
222 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
223 } else if (tarval_is_null(ta)) {
224 return get_mode_null(m);
230 * Return the value of an unary Minus.
232 static ir_tarval *computed_value_Minus(const ir_node *n)
234 ir_node *a = get_Minus_op(n);
235 ir_tarval *ta = value_of(a);
237 if (ta != tarval_bad)
238 return tarval_neg(ta);
244 * Return the value of a Mul.
246 static ir_tarval *computed_value_Mul(const ir_node *n)
248 ir_node *a = get_Mul_left(n);
249 ir_node *b = get_Mul_right(n);
250 ir_tarval *ta = value_of(a);
251 ir_tarval *tb = value_of(b);
254 mode = get_irn_mode(n);
255 if (mode != get_irn_mode(a)) {
256 /* n * n = 2n bit multiplication */
257 ta = tarval_convert_to(ta, mode);
258 tb = tarval_convert_to(tb, mode);
261 if (ta != tarval_bad && tb != tarval_bad) {
262 return tarval_mul(ta, tb);
264 /* a * 0 != 0 if a == NaN or a == Inf */
265 if (!mode_is_float(mode)) {
266 /* a*0 = 0 or 0*b = 0 */
267 if (ta == get_mode_null(mode))
269 if (tb == get_mode_null(mode))
277 * Return the value of an And.
278 * Special case: a & 0, 0 & b
280 static ir_tarval *computed_value_And(const ir_node *n)
282 ir_node *a = get_And_left(n);
283 ir_node *b = get_And_right(n);
284 ir_tarval *ta = value_of(a);
285 ir_tarval *tb = value_of(b);
287 if ((ta != tarval_bad) && (tb != tarval_bad)) {
288 return tarval_and (ta, tb);
291 if (tarval_is_null(ta)) return ta;
292 if (tarval_is_null(tb)) return tb;
295 if ((is_Not(a) && get_Not_op(a) == b)
296 || (is_Not(b) && get_Not_op(b) == a)) {
297 return get_mode_null(get_irn_mode(n));
304 * Return the value of an Or.
305 * Special case: a | 1...1, 1...1 | b
307 static ir_tarval *computed_value_Or(const ir_node *n)
309 ir_node *a = get_Or_left(n);
310 ir_node *b = get_Or_right(n);
311 ir_tarval *ta = value_of(a);
312 ir_tarval *tb = value_of(b);
314 if ((ta != tarval_bad) && (tb != tarval_bad)) {
315 return tarval_or (ta, tb);
318 if (tarval_is_all_one(ta)) return ta;
319 if (tarval_is_all_one(tb)) return tb;
322 if ((is_Not(a) && get_Not_op(a) == b)
323 || (is_Not(b) && get_Not_op(b) == a)) {
324 return get_mode_all_one(get_irn_mode(n));
330 * Return the value of an Eor.
332 static ir_tarval *computed_value_Eor(const ir_node *n)
334 ir_node *a = get_Eor_left(n);
335 ir_node *b = get_Eor_right(n);
340 return get_mode_null(get_irn_mode(n));
342 if ((is_Not(a) && get_Not_op(a) == b)
343 || (is_Not(b) && get_Not_op(b) == a)) {
344 return get_mode_all_one(get_irn_mode(n));
350 if ((ta != tarval_bad) && (tb != tarval_bad)) {
351 return tarval_eor(ta, tb);
357 * Return the value of a Not.
359 static ir_tarval *computed_value_Not(const ir_node *n)
361 ir_node *a = get_Not_op(n);
362 ir_tarval *ta = value_of(a);
364 if (ta != tarval_bad)
365 return tarval_not(ta);
371 * Tests whether a shift shifts more bits than available in the mode
373 static bool is_oversize_shift(const ir_node *n)
375 ir_node *count = get_binop_right(n);
376 ir_mode *mode = get_irn_mode(n);
377 ir_tarval *tv = value_of(count);
380 if (tv == tarval_bad)
382 if (!tarval_is_long(tv))
384 shiftval = get_tarval_long(tv);
385 modulo_shift = get_mode_modulo_shift(mode);
386 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
389 return shiftval >= (long)get_mode_size_bits(mode);
393 * Return the value of a Shl.
395 static ir_tarval *computed_value_Shl(const ir_node *n)
397 ir_node *a = get_Shl_left(n);
398 ir_node *b = get_Shl_right(n);
400 ir_tarval *ta = value_of(a);
401 ir_tarval *tb = value_of(b);
403 if ((ta != tarval_bad) && (tb != tarval_bad)) {
404 return tarval_shl(ta, tb);
407 if (is_oversize_shift(n))
408 return get_mode_null(get_irn_mode(n));
414 * Return the value of a Shr.
416 static ir_tarval *computed_value_Shr(const ir_node *n)
418 ir_node *a = get_Shr_left(n);
419 ir_node *b = get_Shr_right(n);
421 ir_tarval *ta = value_of(a);
422 ir_tarval *tb = value_of(b);
424 if ((ta != tarval_bad) && (tb != tarval_bad)) {
425 return tarval_shr(ta, tb);
427 if (is_oversize_shift(n))
428 return get_mode_null(get_irn_mode(n));
434 * Return the value of a Shrs.
436 static ir_tarval *computed_value_Shrs(const ir_node *n)
438 ir_node *a = get_Shrs_left(n);
439 ir_node *b = get_Shrs_right(n);
441 ir_tarval *ta = value_of(a);
442 ir_tarval *tb = value_of(b);
444 if ((ta != tarval_bad) && (tb != tarval_bad)) {
445 return tarval_shrs(ta, tb);
451 * Return the value of a Rotl.
453 static ir_tarval *computed_value_Rotl(const ir_node *n)
455 ir_node *a = get_Rotl_left(n);
456 ir_node *b = get_Rotl_right(n);
458 ir_tarval *ta = value_of(a);
459 ir_tarval *tb = value_of(b);
461 if ((ta != tarval_bad) && (tb != tarval_bad)) {
462 return tarval_rotl(ta, tb);
467 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
469 ir_mode *mode = get_irn_mode(node);
470 if (get_mode_arithmetic(mode) != irma_twos_complement
471 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
475 ir_node *count = get_Shl_right(node);
476 if (is_Const(count)) {
477 ir_tarval *tv = get_Const_tarval(count);
478 if (tarval_is_long(tv)) {
479 long shiftval = get_tarval_long(tv);
480 long destbits = get_mode_size_bits(dest_mode);
481 if (shiftval >= destbits
482 && shiftval < (long)get_mode_modulo_shift(mode))
488 ir_node *right = get_And_right(node);
489 if (is_Const(right)) {
490 ir_tarval *tv = get_Const_tarval(right);
491 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
492 return tarval_is_null(conved);
499 * Return the value of a Conv.
501 static ir_tarval *computed_value_Conv(const ir_node *n)
503 ir_node *a = get_Conv_op(n);
504 ir_tarval *ta = value_of(a);
505 ir_mode *mode = get_irn_mode(n);
507 if (ta != tarval_bad)
508 return tarval_convert_to(ta, get_irn_mode(n));
510 if (ir_zero_when_converted(a, mode))
511 return get_mode_null(mode);
517 * Calculate the value of a Mux: can be evaluated, if the
518 * sel and the right input are known.
520 static ir_tarval *computed_value_Mux(const ir_node *n)
522 ir_node *sel = get_Mux_sel(n);
523 ir_tarval *ts = value_of(sel);
525 if (ts == get_tarval_b_true()) {
526 ir_node *v = get_Mux_true(n);
529 else if (ts == get_tarval_b_false()) {
530 ir_node *v = get_Mux_false(n);
537 * Calculate the value of a Confirm: can be evaluated,
538 * if it has the form Confirm(x, '=', Const).
540 static ir_tarval *computed_value_Confirm(const ir_node *n)
542 if (get_Confirm_relation(n) == ir_relation_equal) {
543 ir_tarval *tv = value_of(get_Confirm_bound(n));
544 if (tv != tarval_bad)
547 return value_of(get_Confirm_value(n));
551 * gives a (conservative) estimation of possible relation when comparing
554 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
555 const ir_node *right)
557 ir_relation possible = ir_relation_true;
558 ir_tarval *tv_l = value_of(left);
559 ir_tarval *tv_r = value_of(right);
560 ir_mode *mode = get_irn_mode(left);
561 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
562 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
564 /* both values known - evaluate them */
565 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
566 possible = tarval_cmp(tv_l, tv_r);
567 /* we can return now, won't get any better */
570 /* a == a is never less or greater (but might be equal or unordered) */
572 possible &= ~ir_relation_less_greater;
573 /* unordered results only happen for float compares */
574 if (!mode_is_float(mode))
575 possible &= ~ir_relation_unordered;
576 /* values can never be less than the least representable number or
577 * greater than the greatest representable number */
579 possible &= ~ir_relation_greater;
581 possible &= ~ir_relation_less;
583 possible &= ~ir_relation_greater;
585 possible &= ~ir_relation_less;
586 /* maybe vrp can tell us more */
587 possible &= vrp_cmp(left, right);
588 /* Alloc nodes never return null (but throw an exception) */
589 if (is_Alloc(left) && tarval_is_null(tv_r))
590 possible &= ~ir_relation_equal;
595 static ir_tarval *compute_cmp(const ir_node *cmp)
597 ir_node *left = get_Cmp_left(cmp);
598 ir_node *right = get_Cmp_right(cmp);
599 ir_relation possible = ir_get_possible_cmp_relations(left, right);
600 ir_relation relation = get_Cmp_relation(cmp);
602 /* if none of the requested relations is possible, return false */
603 if ((possible & relation) == ir_relation_false)
604 return tarval_b_false;
605 /* if possible relations are a subset of the requested ones return true */
606 if ((possible & ~relation) == ir_relation_false)
607 return tarval_b_true;
609 return computed_value_Cmp_Confirm(cmp, left, right, relation);
613 * Return the value of a Cmp.
615 * The basic idea here is to determine which relations are possible and which
616 * one are definitely impossible.
618 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
620 /* we can't construct Constb after lowering mode_b nodes */
621 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
624 return compute_cmp(cmp);
628 * Calculate the value of an integer Div.
629 * Special case: 0 / b
631 static ir_tarval *do_computed_value_Div(const ir_node *div)
633 const ir_node *a = get_Div_left(div);
634 const ir_node *b = get_Div_right(div);
635 const ir_mode *mode = get_Div_resmode(div);
636 ir_tarval *ta = value_of(a);
638 const ir_node *dummy;
640 /* cannot optimize 0 / b = 0 because of NaN */
641 if (!mode_is_float(mode)) {
642 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
643 return ta; /* 0 / b == 0 if b != 0 */
646 if (ta != tarval_bad && tb != tarval_bad)
647 return tarval_div(ta, tb);
652 * Calculate the value of an integer Mod of two nodes.
653 * Special case: a % 1
655 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
657 ir_tarval *ta = value_of(a);
658 ir_tarval *tb = value_of(b);
660 /* Compute a % 1 or c1 % c2 */
661 if (tarval_is_one(tb))
662 return get_mode_null(get_irn_mode(a));
663 if (ta != tarval_bad && tb != tarval_bad)
664 return tarval_mod(ta, tb);
669 * Return the value of a Proj(Div).
671 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
673 long proj_nr = get_Proj_proj(n);
674 if (proj_nr != pn_Div_res)
677 return do_computed_value_Div(get_Proj_pred(n));
681 * Return the value of a Proj(Mod).
683 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
685 long proj_nr = get_Proj_proj(n);
687 if (proj_nr == pn_Mod_res) {
688 const ir_node *mod = get_Proj_pred(n);
689 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
695 * Return the value of a Proj.
697 static ir_tarval *computed_value_Proj(const ir_node *proj)
699 ir_node *n = get_Proj_pred(proj);
701 if (n->op->ops.computed_value_Proj != NULL)
702 return n->op->ops.computed_value_Proj(proj);
707 * If the parameter n can be computed, return its value, else tarval_bad.
708 * Performs constant folding.
710 * @param n The node this should be evaluated
712 ir_tarval *computed_value(const ir_node *n)
714 vrp_attr *vrp = vrp_get_info(n);
715 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
716 return vrp->bits_set;
718 if (n->op->ops.computed_value)
719 return n->op->ops.computed_value(n);
724 * Set the default computed_value evaluator in an ir_op_ops.
726 * @param code the opcode for the default operation
727 * @param ops the operations initialized
732 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
736 ops->computed_value = computed_value_##a; \
738 #define CASE_PROJ(a) \
740 ops->computed_value_Proj = computed_value_Proj_##a; \
778 * Optimize operations that are commutative and have neutral 0,
779 * so a op 0 = 0 op a = a.
781 static ir_node *equivalent_node_neutral_zero(ir_node *n)
785 ir_node *a = get_binop_left(n);
786 ir_node *b = get_binop_right(n);
791 /* After running compute_node there is only one constant predecessor.
792 Find this predecessors value and remember the other node: */
793 if ((tv = value_of(a)) != tarval_bad) {
795 } else if ((tv = value_of(b)) != tarval_bad) {
800 /* If this predecessors constant value is zero, the operation is
801 * unnecessary. Remove it.
803 * Beware: If n is a Add, the mode of on and n might be different
804 * which happens in this rare construction: NULL + 3.
805 * Then, a Conv would be needed which we cannot include here.
807 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
817 * Eor is commutative and has neutral 0.
819 static ir_node *equivalent_node_Eor(ir_node *n)
825 n = equivalent_node_neutral_zero(n);
826 if (n != oldn) return n;
829 b = get_Eor_right(n);
831 if (is_Eor(a) || is_Or_Eor_Add(a)) {
832 ir_node *aa = get_binop_left(a);
833 ir_node *ab = get_binop_right(a);
836 /* (a ^ b) ^ a -> b */
838 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
840 } else if (ab == b) {
841 /* (a ^ b) ^ b -> a */
843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
847 if (is_Eor(b) || is_Or_Eor_Add(b)) {
848 ir_node *ba = get_binop_left(b);
849 ir_node *bb = get_binop_right(b);
852 /* a ^ (a ^ b) -> b */
854 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
856 } else if (bb == a) {
857 /* a ^ (b ^ a) -> b */
859 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
867 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
869 * The second one looks strange, but this construct
870 * is used heavily in the LCC sources :-).
872 * Beware: The Mode of an Add may be different than the mode of its
873 * predecessors, so we could not return a predecessors in all cases.
875 static ir_node *equivalent_node_Add(ir_node *n)
878 ir_node *left, *right;
879 ir_mode *mode = get_irn_mode(n);
881 n = equivalent_node_neutral_zero(n);
885 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
886 if (mode_is_float(mode)) {
887 ir_graph *irg = get_irn_irg(n);
888 if (get_irg_fp_model(irg) & fp_strict_algebraic)
892 left = get_Add_left(n);
893 right = get_Add_right(n);
896 if (get_Sub_right(left) == right) {
899 n = get_Sub_left(left);
900 if (mode == get_irn_mode(n)) {
901 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
907 if (get_Sub_right(right) == left) {
910 n = get_Sub_left(right);
911 if (mode == get_irn_mode(n)) {
912 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
921 * optimize operations that are not commutative but have neutral 0 on left,
924 static ir_node *equivalent_node_left_zero(ir_node *n)
928 ir_node *a = get_binop_left(n);
929 ir_node *b = get_binop_right(n);
930 ir_tarval *tb = value_of(b);
932 if (tarval_is_null(tb)) {
935 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
940 #define equivalent_node_Shl equivalent_node_left_zero
941 #define equivalent_node_Shr equivalent_node_left_zero
942 #define equivalent_node_Shrs equivalent_node_left_zero
943 #define equivalent_node_Rotl equivalent_node_left_zero
946 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
948 * The second one looks strange, but this construct
949 * is used heavily in the LCC sources :-).
951 * Beware: The Mode of a Sub may be different than the mode of its
952 * predecessors, so we could not return a predecessors in all cases.
954 static ir_node *equivalent_node_Sub(ir_node *n)
958 ir_mode *mode = get_irn_mode(n);
961 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
962 if (mode_is_float(mode)) {
963 ir_graph *irg = get_irn_irg(n);
964 if (get_irg_fp_model(irg) & fp_strict_algebraic)
968 b = get_Sub_right(n);
971 /* Beware: modes might be different */
972 if (tarval_is_null(tb)) {
973 ir_node *a = get_Sub_left(n);
974 if (mode == get_irn_mode(a)) {
977 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
985 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
988 * -(-a) == a, but might overflow two times.
989 * We handle it anyway here but the better way would be a
990 * flag. This would be needed for Pascal for instance.
992 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
995 ir_node *pred = get_unop_op(n);
997 /* optimize symmetric unop */
998 if (get_irn_op(pred) == get_irn_op(n)) {
999 n = get_unop_op(pred);
1000 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1005 /** Optimize Not(Not(x)) == x. */
1006 #define equivalent_node_Not equivalent_node_idempotent_unop
1008 /** -(-x) == x ??? Is this possible or can --x raise an
1009 out of bounds exception if min =! max? */
1010 #define equivalent_node_Minus equivalent_node_idempotent_unop
1013 * Optimize a * 1 = 1 * a = a.
1015 static ir_node *equivalent_node_Mul(ir_node *n)
1018 ir_node *a = get_Mul_left(n);
1020 /* we can handle here only the n * n = n bit cases */
1021 if (get_irn_mode(n) == get_irn_mode(a)) {
1022 ir_node *b = get_Mul_right(n);
1026 * Mul is commutative and has again an other neutral element.
1027 * Constants are place right, so check this case first.
1030 if (tarval_is_one(tv)) {
1032 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1035 if (tarval_is_one(tv)) {
1037 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1045 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1047 static ir_node *equivalent_node_Or(ir_node *n)
1051 ir_node *a = get_Or_left(n);
1052 ir_node *b = get_Or_right(n);
1056 n = a; /* idempotence */
1057 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1060 /* constants are normalized to right, check this side first */
1062 if (tarval_is_null(tv)) {
1064 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1068 if (tarval_is_null(tv)) {
1070 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1078 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1080 static ir_node *equivalent_node_And(ir_node *n)
1084 ir_node *a = get_And_left(n);
1085 ir_node *b = get_And_right(n);
1089 n = a; /* idempotence */
1090 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1093 /* constants are normalized to right, check this side first */
1095 if (tarval_is_all_one(tv)) {
1097 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1100 if (tv != get_tarval_bad()) {
1101 ir_mode *mode = get_irn_mode(n);
1102 if (!mode_is_signed(mode) && is_Conv(a)) {
1103 ir_node *convop = get_Conv_op(a);
1104 ir_mode *convopmode = get_irn_mode(convop);
1105 if (!mode_is_signed(convopmode)) {
1106 /* Check Conv(all_one) & Const = all_one */
1107 ir_tarval *one = get_mode_all_one(convopmode);
1108 ir_tarval *conv = tarval_convert_to(one, mode);
1109 ir_tarval *and = tarval_and(conv, tv);
1111 if (tarval_is_all_one(and)) {
1112 /* Conv(X) & Const = X */
1114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1121 if (tarval_is_all_one(tv)) {
1123 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1127 if ((is_Or(a) || is_Or_Eor_Add(a))
1128 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1130 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1134 if ((is_Or(b) || is_Or_Eor_Add(b))
1135 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1144 * Try to remove useless Conv's:
1146 static ir_node *equivalent_node_Conv(ir_node *n)
1149 ir_node *a = get_Conv_op(n);
1151 ir_mode *n_mode = get_irn_mode(n);
1152 ir_mode *a_mode = get_irn_mode(a);
1155 if (n_mode == a_mode) { /* No Conv necessary */
1156 if (get_Conv_strict(n)) {
1159 /* neither Minus nor Confirm change the precision,
1160 so we can "look-through" */
1163 p = get_Minus_op(p);
1164 } else if (is_Confirm(p)) {
1165 p = get_Confirm_value(p);
1171 if (is_Conv(p) && get_Conv_strict(p)) {
1172 /* we known already, that a_mode == n_mode, and neither
1173 Minus change the mode, so the second Conv
1175 assert(get_irn_mode(p) == n_mode);
1177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1181 ir_node *pred = get_Proj_pred(p);
1182 if (is_Load(pred)) {
1183 /* Loads always return with the exact precision of n_mode */
1184 assert(get_Load_mode(pred) == n_mode);
1186 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1189 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1190 pred = get_Proj_pred(pred);
1191 if (is_Start(pred)) {
1192 /* Arguments always return with the exact precision,
1193 as strictConv's are place before Call -- if the
1194 caller was compiled with the same setting.
1195 Otherwise, the semantics is probably still right. */
1196 assert(get_irn_mode(p) == n_mode);
1198 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1204 /* special case: the immediate predecessor is also a Conv */
1205 if (! get_Conv_strict(a)) {
1206 /* first one is not strict, kick it */
1208 a_mode = get_irn_mode(a);
1212 /* else both are strict conv, second is superfluous */
1214 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1222 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1223 ir_node *b = get_Conv_op(a);
1224 ir_mode *b_mode = get_irn_mode(b);
1226 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1227 /* both are strict conv */
1228 if (smaller_mode(a_mode, n_mode)) {
1229 /* both are strict, but the first is smaller, so
1230 the second cannot remove more precision, remove the
1232 set_Conv_strict(n, 0);
1235 if (n_mode == b_mode) {
1236 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1237 if (n_mode == mode_b) {
1238 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1239 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1241 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1242 if (values_in_mode(b_mode, a_mode)) {
1243 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1249 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1250 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1251 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1252 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1254 if (float_mantissa >= int_mantissa) {
1256 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1261 if (smaller_mode(b_mode, a_mode)) {
1262 if (get_Conv_strict(n))
1263 set_Conv_strict(b, 1);
1264 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1265 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1275 * - fold Phi-nodes, iff they have only one predecessor except
1278 static ir_node *equivalent_node_Phi(ir_node *n)
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_optimize() &&
1286 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1289 n_preds = get_Phi_n_preds(n);
1291 /* Phi of dead Region without predecessors. */
1295 /* Find first non-self-referencing input */
1296 for (i = 0; i < n_preds; ++i) {
1297 first_val = get_Phi_pred(n, i);
1298 /* not self pointer */
1299 if (first_val != n) {
1300 /* then found first value. */
1305 /* search for rest of inputs, determine if any of these
1306 are non-self-referencing */
1307 while (++i < n_preds) {
1308 ir_node *scnd_val = get_Phi_pred(n, i);
1309 if (scnd_val != n && scnd_val != first_val) {
1314 if (i >= n_preds && !is_Dummy(first_val)) {
1315 /* Fold, if no multiple distinct non-self-referencing inputs */
1317 DBG_OPT_PHI(oldn, n);
1323 * Optimize Proj(Tuple).
1325 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1327 ir_node *oldn = proj;
1328 ir_node *tuple = get_Proj_pred(proj);
1330 /* Remove the Tuple/Proj combination. */
1331 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1332 DBG_OPT_TUPLE(oldn, tuple, proj);
1338 * Optimize a / 1 = a.
1340 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1342 ir_node *oldn = proj;
1343 ir_node *div = get_Proj_pred(proj);
1344 ir_node *b = get_Div_right(div);
1345 ir_tarval *tb = value_of(b);
1347 /* Div is not commutative. */
1348 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1349 switch (get_Proj_proj(proj)) {
1351 proj = get_Div_mem(div);
1352 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1356 proj = get_Div_left(div);
1357 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1361 /* we cannot replace the exception Proj's here, this is done in
1362 transform_node_Proj_Div() */
1370 * Optimize CopyB(mem, x, x) into a Nop.
1372 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1374 ir_node *oldn = proj;
1375 ir_node *copyb = get_Proj_pred(proj);
1376 ir_node *a = get_CopyB_dst(copyb);
1377 ir_node *b = get_CopyB_src(copyb);
1380 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1381 switch (get_Proj_proj(proj)) {
1383 proj = get_CopyB_mem(copyb);
1384 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1392 * Optimize Bounds(idx, idx, upper) into idx.
1394 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1396 ir_node *oldn = proj;
1397 ir_node *bound = get_Proj_pred(proj);
1398 ir_node *idx = get_Bound_index(bound);
1399 ir_node *pred = skip_Proj(idx);
1402 if (idx == get_Bound_lower(bound))
1404 else if (is_Bound(pred)) {
1406 * idx was Bounds checked previously, it is still valid if
1407 * lower <= pred_lower && pred_upper <= upper.
1409 ir_node *lower = get_Bound_lower(bound);
1410 ir_node *upper = get_Bound_upper(bound);
1411 if (get_Bound_lower(pred) == lower &&
1412 get_Bound_upper(pred) == upper) {
1414 * One could expect that we simply return the previous
1415 * Bound here. However, this would be wrong, as we could
1416 * add an exception Proj to a new location then.
1417 * So, we must turn in into a tuple.
1423 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1424 switch (get_Proj_proj(proj)) {
1426 DBG_OPT_EXC_REM(proj);
1427 proj = get_Bound_mem(bound);
1431 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1434 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1442 * Does all optimizations on nodes that must be done on its Projs
1443 * because of creating new nodes.
1445 static ir_node *equivalent_node_Proj(ir_node *proj)
1447 ir_node *n = get_Proj_pred(proj);
1448 if (n->op->ops.equivalent_node_Proj)
1449 return n->op->ops.equivalent_node_Proj(proj);
1456 static ir_node *equivalent_node_Id(ir_node *n)
1464 DBG_OPT_ID(oldn, n);
1471 static ir_node *equivalent_node_Mux(ir_node *n)
1473 ir_node *oldn = n, *sel = get_Mux_sel(n);
1475 ir_tarval *ts = value_of(sel);
1477 if (ts == tarval_bad && is_Cmp(sel)) {
1478 /* try again with a direct call to compute_cmp, as we don't care
1479 * about the MODEB_LOWERED flag here */
1480 ts = compute_cmp(sel);
1483 /* Mux(true, f, t) == t */
1484 if (ts == tarval_b_true) {
1485 n = get_Mux_true(n);
1486 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1489 /* Mux(false, f, t) == f */
1490 if (ts == tarval_b_false) {
1491 n = get_Mux_false(n);
1492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1495 n_t = get_Mux_true(n);
1496 n_f = get_Mux_false(n);
1498 /* Mux(v, x, T) == x */
1499 if (is_Unknown(n_f)) {
1501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1504 /* Mux(v, T, x) == x */
1505 if (is_Unknown(n_t)) {
1507 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1511 /* Mux(v, x, x) == x */
1514 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1517 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1518 ir_relation relation = get_Cmp_relation(sel);
1519 ir_node *f = get_Mux_false(n);
1520 ir_node *t = get_Mux_true(n);
1523 * Note further that these optimization work even for floating point
1524 * with NaN's because -NaN == NaN.
1525 * However, if +0 and -0 is handled differently, we cannot use the first one.
1527 ir_node *const cmp_l = get_Cmp_left(sel);
1528 ir_node *const cmp_r = get_Cmp_right(sel);
1531 case ir_relation_equal:
1532 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1533 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1535 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1540 case ir_relation_less_greater:
1541 case ir_relation_unordered_less_greater:
1542 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1543 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1545 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1554 * Note: normalization puts the constant on the right side,
1555 * so we check only one case.
1557 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1558 /* Mux(t CMP 0, X, t) */
1559 if (is_Minus(f) && get_Minus_op(f) == t) {
1560 /* Mux(t CMP 0, -t, t) */
1561 if (relation == ir_relation_equal) {
1562 /* Mux(t == 0, -t, t) ==> -t */
1564 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1565 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1566 /* Mux(t != 0, -t, t) ==> t */
1568 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1578 * Remove Confirm nodes if setting is on.
1579 * Replace Confirms(x, '=', Constlike) by Constlike.
1581 static ir_node *equivalent_node_Confirm(ir_node *n)
1583 ir_node *pred = get_Confirm_value(n);
1584 ir_relation relation = get_Confirm_relation(n);
1586 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1588 * rare case: two identical Confirms one after another,
1589 * replace the second one with the first.
1592 pred = get_Confirm_value(n);
1598 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1599 * perform no actual computation, as, e.g., the Id nodes. It does not create
1600 * new nodes. It is therefore safe to free n if the node returned is not n.
1601 * If a node returns a Tuple we can not just skip it. If the size of the
1602 * in array fits, we transform n into a tuple (e.g., Div).
1604 ir_node *equivalent_node(ir_node *n)
1606 if (n->op->ops.equivalent_node)
1607 return n->op->ops.equivalent_node(n);
1612 * Sets the default equivalent node operation for an ir_op_ops.
1614 * @param code the opcode for the default operation
1615 * @param ops the operations initialized
1620 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1624 ops->equivalent_node = equivalent_node_##a; \
1626 #define CASE_PROJ(a) \
1628 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1665 * Returns non-zero if a node is a Phi node
1666 * with all predecessors constant.
1668 static int is_const_Phi(ir_node *n)
1672 if (! is_Phi(n) || get_irn_arity(n) == 0)
1674 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1675 if (! is_Const(get_irn_n(n, i)))
1681 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1682 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1685 * in reality eval_func should be tarval (*eval_func)() but incomplete
1686 * declarations are bad style and generate noisy warnings
1688 typedef void (*eval_func)(void);
1691 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1693 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1695 if (eval == (eval_func) tarval_sub) {
1696 tarval_sub_type func = (tarval_sub_type)eval;
1698 return func(a, b, mode);
1700 tarval_binop_type func = (tarval_binop_type)eval;
1707 * Apply an evaluator on a binop with a constant operators (and one Phi).
1709 * @param phi the Phi node
1710 * @param other the other operand
1711 * @param eval an evaluator function
1712 * @param mode the mode of the result, may be different from the mode of the Phi!
1713 * @param left if non-zero, other is the left operand, else the right
1715 * @return a new Phi node if the conversion was successful, NULL else
1717 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1723 int i, n = get_irn_arity(phi);
1725 NEW_ARR_A(void *, res, n);
1727 for (i = 0; i < n; ++i) {
1728 pred = get_irn_n(phi, i);
1729 tv = get_Const_tarval(pred);
1730 tv = do_eval(eval, other, tv, mode);
1732 if (tv == tarval_bad) {
1733 /* folding failed, bad */
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 tv = get_Const_tarval(pred);
1742 tv = do_eval(eval, tv, other, mode);
1744 if (tv == tarval_bad) {
1745 /* folding failed, bad */
1751 irg = get_irn_irg(phi);
1752 for (i = 0; i < n; ++i) {
1753 pred = get_irn_n(phi, i);
1754 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1756 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1760 * Apply an evaluator on a binop with two constant Phi.
1762 * @param a the left Phi node
1763 * @param b the right Phi node
1764 * @param eval an evaluator function
1765 * @param mode the mode of the result, may be different from the mode of the Phi!
1767 * @return a new Phi node if the conversion was successful, NULL else
1769 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1771 ir_tarval *tv_l, *tv_r, *tv;
1777 if (get_nodes_block(a) != get_nodes_block(b))
1780 n = get_irn_arity(a);
1781 NEW_ARR_A(void *, res, n);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(a, i);
1785 tv_l = get_Const_tarval(pred);
1786 pred = get_irn_n(b, i);
1787 tv_r = get_Const_tarval(pred);
1788 tv = do_eval(eval, tv_l, tv_r, mode);
1790 if (tv == tarval_bad) {
1791 /* folding failed, bad */
1796 irg = get_irn_irg(a);
1797 for (i = 0; i < n; ++i) {
1798 pred = get_irn_n(a, i);
1799 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1801 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1805 * Apply an evaluator on a unop with a constant operator (a Phi).
1807 * @param phi the Phi node
1808 * @param eval an evaluator function
1810 * @return a new Phi node if the conversion was successful, NULL else
1812 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1819 int i, n = get_irn_arity(phi);
1821 NEW_ARR_A(void *, res, n);
1822 for (i = 0; i < n; ++i) {
1823 pred = get_irn_n(phi, i);
1824 tv = get_Const_tarval(pred);
1827 if (tv == tarval_bad) {
1828 /* folding failed, bad */
1833 mode = get_irn_mode(phi);
1834 irg = get_irn_irg(phi);
1835 for (i = 0; i < n; ++i) {
1836 pred = get_irn_n(phi, i);
1837 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1839 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1843 * Apply a conversion on a constant operator (a Phi).
1845 * @param phi the Phi node
1847 * @return a new Phi node if the conversion was successful, NULL else
1849 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1855 int i, n = get_irn_arity(phi);
1857 NEW_ARR_A(void *, res, n);
1858 for (i = 0; i < n; ++i) {
1859 pred = get_irn_n(phi, i);
1860 tv = get_Const_tarval(pred);
1861 tv = tarval_convert_to(tv, mode);
1863 if (tv == tarval_bad) {
1864 /* folding failed, bad */
1869 irg = get_irn_irg(phi);
1870 for (i = 0; i < n; ++i) {
1871 pred = get_irn_n(phi, i);
1872 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1874 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1878 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1879 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1880 * If possible, remove the Conv's.
1882 static ir_node *transform_node_AddSub(ir_node *n)
1884 ir_mode *mode = get_irn_mode(n);
1886 if (mode_is_reference(mode)) {
1887 ir_node *left = get_binop_left(n);
1888 ir_node *right = get_binop_right(n);
1889 unsigned ref_bits = get_mode_size_bits(mode);
1891 if (is_Conv(left)) {
1892 ir_mode *lmode = get_irn_mode(left);
1893 unsigned bits = get_mode_size_bits(lmode);
1895 if (ref_bits == bits &&
1896 mode_is_int(lmode) &&
1897 get_mode_arithmetic(lmode) == irma_twos_complement) {
1898 ir_node *pre = get_Conv_op(left);
1899 ir_mode *pre_mode = get_irn_mode(pre);
1901 if (mode_is_int(pre_mode) &&
1902 get_mode_size_bits(pre_mode) == bits &&
1903 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1904 /* ok, this conv just changes to sign, moreover the calculation
1905 * is done with same number of bits as our address mode, so
1906 * we can ignore the conv as address calculation can be viewed
1907 * as either signed or unsigned
1909 set_binop_left(n, pre);
1914 if (is_Conv(right)) {
1915 ir_mode *rmode = get_irn_mode(right);
1916 unsigned bits = get_mode_size_bits(rmode);
1918 if (ref_bits == bits &&
1919 mode_is_int(rmode) &&
1920 get_mode_arithmetic(rmode) == irma_twos_complement) {
1921 ir_node *pre = get_Conv_op(right);
1922 ir_mode *pre_mode = get_irn_mode(pre);
1924 if (mode_is_int(pre_mode) &&
1925 get_mode_size_bits(pre_mode) == bits &&
1926 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1927 /* ok, this conv just changes to sign, moreover the calculation
1928 * is done with same number of bits as our address mode, so
1929 * we can ignore the conv as address calculation can be viewed
1930 * as either signed or unsigned
1932 set_binop_right(n, pre);
1937 /* let address arithmetic use unsigned modes */
1938 if (is_Const(right)) {
1939 ir_mode *rmode = get_irn_mode(right);
1941 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1942 /* convert a AddP(P, *s) into AddP(P, *u) */
1943 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1945 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1946 set_binop_right(n, pre);
1954 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1957 if (is_Const(b) && is_const_Phi(a)) { \
1958 /* check for Op(Phi, Const) */ \
1959 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1961 else if (is_Const(a) && is_const_Phi(b)) { \
1962 /* check for Op(Const, Phi) */ \
1963 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1965 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1966 /* check for Op(Phi, Phi) */ \
1967 c = apply_binop_on_2_phis(a, b, eval, mode); \
1970 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1975 #define HANDLE_UNOP_PHI(eval, a, c) \
1978 if (is_const_Phi(a)) { \
1979 /* check for Op(Phi) */ \
1980 c = apply_unop_on_phi(a, eval); \
1982 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1989 * Create a 0 constant of given mode.
1991 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1993 ir_tarval *tv = get_mode_null(mode);
1994 ir_node *cnst = new_r_Const(irg, tv);
1999 static bool is_shiftop(const ir_node *n)
2001 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2004 /* the order of the values is important! */
2005 typedef enum const_class {
2011 static const_class classify_const(const ir_node* n)
2013 if (is_Const(n)) return const_const;
2014 if (is_irn_constlike(n)) return const_like;
2019 * Determines whether r is more constlike or has a larger index (in that order)
2022 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2024 const const_class l_order = classify_const(l);
2025 const const_class r_order = classify_const(r);
2027 l_order > r_order ||
2028 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2031 static bool is_cmp_unequal(const ir_node *node)
2033 ir_relation relation = get_Cmp_relation(node);
2034 ir_node *left = get_Cmp_left(node);
2035 ir_node *right = get_Cmp_right(node);
2036 ir_mode *mode = get_irn_mode(left);
2038 if (relation == ir_relation_less_greater)
2041 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2042 return relation == ir_relation_greater;
2047 * returns true for Cmp(x == 0) or Cmp(x != 0)
2049 static bool is_cmp_equality_zero(const ir_node *node)
2051 ir_relation relation;
2052 ir_node *right = get_Cmp_right(node);
2054 if (!is_Const(right) || !is_Const_null(right))
2056 relation = get_Cmp_relation(node);
2057 return relation == ir_relation_equal
2058 || relation == ir_relation_less_greater
2059 || (!mode_is_signed(get_irn_mode(right))
2060 && relation == ir_relation_greater);
2064 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2065 * Such pattern may arise in bitfield stores.
2067 * value c4 value c4 & c2
2068 * AND c3 AND c1 | c3
2075 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2078 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2080 ir_node *irn_and, *c1;
2082 ir_node *and_l, *c3;
2083 ir_node *value, *c4;
2084 ir_node *new_and, *new_const, *block;
2085 ir_mode *mode = get_irn_mode(irn_or);
2087 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2091 irn_and = get_binop_left(irn_or);
2092 c1 = get_binop_right(irn_or);
2093 if (!is_Const(c1) || !is_And(irn_and))
2096 or_l = get_binop_left(irn_and);
2097 c2 = get_binop_right(irn_and);
2101 tv1 = get_Const_tarval(c1);
2102 tv2 = get_Const_tarval(c2);
2104 tv = tarval_or(tv1, tv2);
2105 if (tarval_is_all_one(tv)) {
2106 /* the AND does NOT clear a bit with isn't set by the OR */
2107 set_binop_left(irn_or, or_l);
2108 set_binop_right(irn_or, c1);
2110 /* check for more */
2114 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2117 and_l = get_binop_left(or_l);
2118 c3 = get_binop_right(or_l);
2119 if (!is_Const(c3) || !is_And(and_l))
2122 value = get_binop_left(and_l);
2123 c4 = get_binop_right(and_l);
2127 /* ok, found the pattern, check for conditions */
2128 assert(mode == get_irn_mode(irn_and));
2129 assert(mode == get_irn_mode(or_l));
2130 assert(mode == get_irn_mode(and_l));
2132 tv3 = get_Const_tarval(c3);
2133 tv4 = get_Const_tarval(c4);
2135 tv = tarval_or(tv4, tv2);
2136 if (!tarval_is_all_one(tv)) {
2137 /* have at least one 0 at the same bit position */
2141 if (tv3 != tarval_andnot(tv3, tv4)) {
2142 /* bit in the or_mask is outside the and_mask */
2146 if (tv1 != tarval_andnot(tv1, tv2)) {
2147 /* bit in the or_mask is outside the and_mask */
2151 /* ok, all conditions met */
2152 block = get_irn_n(irn_or, -1);
2153 irg = get_irn_irg(block);
2155 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2157 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2159 set_binop_left(irn_or, new_and);
2160 set_binop_right(irn_or, new_const);
2162 /* check for more */
2167 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2169 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2171 ir_mode *mode = get_irn_mode(irn_or);
2172 ir_node *shl, *shr, *block;
2173 ir_node *irn, *x, *c1, *c2, *n;
2174 ir_tarval *tv1, *tv2;
2176 /* some backends can't handle rotl */
2177 if (!be_get_backend_param()->support_rotl)
2180 if (! mode_is_int(mode))
2183 shl = get_binop_left(irn_or);
2184 shr = get_binop_right(irn_or);
2193 } else if (!is_Shl(shl)) {
2195 } else if (!is_Shr(shr)) {
2198 x = get_Shl_left(shl);
2199 if (x != get_Shr_left(shr))
2202 c1 = get_Shl_right(shl);
2203 c2 = get_Shr_right(shr);
2204 if (is_Const(c1) && is_Const(c2)) {
2205 tv1 = get_Const_tarval(c1);
2206 if (! tarval_is_long(tv1))
2209 tv2 = get_Const_tarval(c2);
2210 if (! tarval_is_long(tv2))
2213 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2214 != (int) get_mode_size_bits(mode))
2217 /* yet, condition met */
2218 block = get_nodes_block(irn_or);
2220 n = new_r_Rotl(block, x, c1, mode);
2222 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2226 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2227 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2228 if (!ir_is_negated_value(c1, c2)) {
2232 /* yet, condition met */
2233 block = get_nodes_block(irn_or);
2234 n = new_r_Rotl(block, x, c1, mode);
2235 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2240 * Prototype of a recursive transform function
2241 * for bitwise distributive transformations.
2243 typedef ir_node* (*recursive_transform)(ir_node *n);
2246 * makes use of distributive laws for and, or, eor
2247 * and(a OP c, b OP c) -> and(a, b) OP c
2248 * note, might return a different op than n
2250 static ir_node *transform_bitwise_distributive(ir_node *n,
2251 recursive_transform trans_func)
2254 ir_node *a = get_binop_left(n);
2255 ir_node *b = get_binop_right(n);
2256 ir_op *op = get_irn_op(a);
2257 ir_op *op_root = get_irn_op(n);
2259 if (op != get_irn_op(b))
2262 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2263 if (op == op_Conv) {
2264 ir_node *a_op = get_Conv_op(a);
2265 ir_node *b_op = get_Conv_op(b);
2266 ir_mode *a_mode = get_irn_mode(a_op);
2267 ir_mode *b_mode = get_irn_mode(b_op);
2268 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2269 ir_node *blk = get_nodes_block(n);
2272 set_binop_left(n, a_op);
2273 set_binop_right(n, b_op);
2274 set_irn_mode(n, a_mode);
2276 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2278 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2284 /* nothing to gain here */
2288 if (op == op_Shrs || op == op_Shr || op == op_Shl
2289 || op == op_And || op == op_Or || op == op_Eor) {
2290 ir_node *a_left = get_binop_left(a);
2291 ir_node *a_right = get_binop_right(a);
2292 ir_node *b_left = get_binop_left(b);
2293 ir_node *b_right = get_binop_right(b);
2295 ir_node *op1 = NULL;
2296 ir_node *op2 = NULL;
2298 if (is_op_commutative(op)) {
2299 if (a_left == b_left) {
2303 } else if (a_left == b_right) {
2307 } else if (a_right == b_left) {
2313 if (a_right == b_right) {
2320 /* (a sop c) & (b sop c) => (a & b) sop c */
2321 ir_node *blk = get_nodes_block(n);
2323 ir_node *new_n = exact_copy(n);
2324 set_binop_left(new_n, op1);
2325 set_binop_right(new_n, op2);
2326 new_n = trans_func(new_n);
2328 if (op_root == op_Eor && op == op_Or) {
2329 dbg_info *dbgi = get_irn_dbg_info(n);
2330 ir_mode *mode = get_irn_mode(c);
2332 c = new_rd_Not(dbgi, blk, c, mode);
2333 n = new_rd_And(dbgi, blk, new_n, c, mode);
2336 set_nodes_block(n, blk);
2337 set_binop_left(n, new_n);
2338 set_binop_right(n, c);
2342 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2351 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2353 * - and, or, xor instead of &
2354 * - Shl, Shr, Shrs, rotl instead of >>
2355 * (with a special case for Or/Xor + Shrs)
2357 * This normalisation is usually good for the backend since << C can often be
2358 * matched as address-mode.
2360 static ir_node *transform_node_bitop_shift(ir_node *n)
2362 ir_graph *irg = get_irn_irg(n);
2363 ir_node *left = get_binop_left(n);
2364 ir_node *right = get_binop_right(n);
2365 ir_mode *mode = get_irn_mode(n);
2366 ir_node *shift_left;
2367 ir_node *shift_right;
2369 dbg_info *dbg_bitop;
2370 dbg_info *dbg_shift;
2376 ir_tarval *tv_bitop;
2378 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2381 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2382 if (!is_Const(right) || !is_shiftop(left))
2385 shift_left = get_binop_left(left);
2386 shift_right = get_binop_right(left);
2387 if (!is_Const(shift_right))
2390 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2391 if (is_Shrs(left)) {
2392 /* TODO this could be improved */
2396 irg = get_irn_irg(n);
2397 block = get_nodes_block(n);
2398 dbg_bitop = get_irn_dbg_info(n);
2399 dbg_shift = get_irn_dbg_info(left);
2400 tv1 = get_Const_tarval(shift_right);
2401 tv2 = get_Const_tarval(right);
2402 assert(get_tarval_mode(tv2) == mode);
2405 tv_bitop = tarval_shr(tv2, tv1);
2407 /* Check whether we have lost some bits during the right shift. */
2409 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2411 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2414 } else if (is_Shr(left)) {
2417 * TODO this can be improved by checking whether
2418 * the left shift produces an overflow
2422 tv_bitop = tarval_shl(tv2, tv1);
2424 assert(is_Rotl(left));
2425 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2427 new_const = new_r_Const(irg, tv_bitop);
2430 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2431 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2432 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2435 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2439 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2440 } else if (is_Shr(left)) {
2441 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2443 assert(is_Rotl(left));
2444 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2450 static bool complement_values(const ir_node *a, const ir_node *b)
2452 if (is_Not(a) && get_Not_op(a) == b)
2454 if (is_Not(b) && get_Not_op(b) == a)
2456 if (is_Const(a) && is_Const(b)) {
2457 ir_tarval *tv_a = get_Const_tarval(a);
2458 ir_tarval *tv_b = get_Const_tarval(b);
2459 return tarval_not(tv_a) == tv_b;
2464 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2467 * for associative operations fold:
2468 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2469 * This is a "light" version of the reassociation phase
2471 static ir_node *fold_constant_associativity(ir_node *node,
2472 tv_fold_binop_func fold)
2477 ir_node *right = get_binop_right(node);
2478 ir_node *left_right;
2485 if (!is_Const(right))
2488 op = get_irn_op(node);
2489 left = get_binop_left(node);
2490 if (get_irn_op(left) != op)
2493 left_right = get_binop_right(left);
2494 if (!is_Const(left_right))
2497 left_left = get_binop_left(left);
2498 c0 = get_Const_tarval(left_right);
2499 c1 = get_Const_tarval(right);
2500 irg = get_irn_irg(node);
2501 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2503 new_c = fold(c0, c1);
2504 if (new_c == tarval_bad)
2506 new_const = new_r_Const(irg, new_c);
2507 new_node = exact_copy(node);
2508 set_binop_left(new_node, left_left);
2509 set_binop_right(new_node, new_const);
2516 static ir_node *transform_node_Or_(ir_node *n)
2519 ir_node *a = get_binop_left(n);
2520 ir_node *b = get_binop_right(n);
2524 n = fold_constant_associativity(n, tarval_or);
2528 if (is_Not(a) && is_Not(b)) {
2529 /* ~a | ~b = ~(a&b) */
2530 ir_node *block = get_nodes_block(n);
2532 mode = get_irn_mode(n);
2535 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2536 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2537 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2541 /* we can combine the relations of two compares with the same operands */
2542 if (is_Cmp(a) && is_Cmp(b)) {
2543 ir_node *a_left = get_Cmp_left(a);
2544 ir_node *a_right = get_Cmp_right(a);
2545 ir_node *b_left = get_Cmp_left(b);
2546 ir_node *b_right = get_Cmp_right(b);
2547 if (a_left == b_left && b_left == b_right) {
2548 dbg_info *dbgi = get_irn_dbg_info(n);
2549 ir_node *block = get_nodes_block(n);
2550 ir_relation a_relation = get_Cmp_relation(a);
2551 ir_relation b_relation = get_Cmp_relation(b);
2552 ir_relation new_relation = a_relation | b_relation;
2553 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2555 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2556 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2557 && !mode_is_float(get_irn_mode(a_left))
2558 && !mode_is_float(get_irn_mode(b_left))) {
2559 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2560 ir_graph *irg = get_irn_irg(n);
2561 dbg_info *dbgi = get_irn_dbg_info(n);
2562 ir_node *block = get_nodes_block(n);
2563 ir_mode *a_mode = get_irn_mode(a_left);
2564 ir_mode *b_mode = get_irn_mode(b_left);
2565 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2566 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2567 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2568 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2569 ir_node *zero = create_zero_const(irg, b_mode);
2570 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2572 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2573 ir_graph *irg = get_irn_irg(n);
2574 dbg_info *dbgi = get_irn_dbg_info(n);
2575 ir_node *block = get_nodes_block(n);
2576 ir_mode *a_mode = get_irn_mode(a_left);
2577 ir_mode *b_mode = get_irn_mode(b_left);
2578 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2579 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2580 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2581 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2582 ir_node *zero = create_zero_const(irg, a_mode);
2583 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2588 mode = get_irn_mode(n);
2589 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2591 n = transform_node_Or_bf_store(n);
2594 n = transform_node_Or_Rotl(n);
2598 n = transform_bitwise_distributive(n, transform_node_Or_);
2601 n = transform_node_bitop_shift(n);
2608 static ir_node *transform_node_Or(ir_node *n)
2610 if (is_Or_Eor_Add(n)) {
2611 dbg_info *dbgi = get_irn_dbg_info(n);
2612 ir_node *block = get_nodes_block(n);
2613 ir_node *left = get_Or_left(n);
2614 ir_node *right = get_Or_right(n);
2615 ir_mode *mode = get_irn_mode(n);
2616 return new_rd_Add(dbgi, block, left, right, mode);
2618 return transform_node_Or_(n);
2624 static ir_node *transform_node_Eor_(ir_node *n)
2627 ir_node *a = get_binop_left(n);
2628 ir_node *b = get_binop_right(n);
2629 ir_mode *mode = get_irn_mode(n);
2632 n = fold_constant_associativity(n, tarval_eor);
2636 /* we can combine the relations of two compares with the same operands */
2637 if (is_Cmp(a) && is_Cmp(b)) {
2638 ir_node *a_left = get_Cmp_left(a);
2639 ir_node *a_right = get_Cmp_left(a);
2640 ir_node *b_left = get_Cmp_left(b);
2641 ir_node *b_right = get_Cmp_right(b);
2642 if (a_left == b_left && b_left == b_right) {
2643 dbg_info *dbgi = get_irn_dbg_info(n);
2644 ir_node *block = get_nodes_block(n);
2645 ir_relation a_relation = get_Cmp_relation(a);
2646 ir_relation b_relation = get_Cmp_relation(b);
2647 ir_relation new_relation = a_relation ^ b_relation;
2648 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2652 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2654 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2655 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2656 dbg_info *dbg = get_irn_dbg_info(n);
2657 ir_node *block = get_nodes_block(n);
2658 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2659 ir_node *new_left = get_Not_op(a);
2660 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2661 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2663 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2664 dbg_info *dbg = get_irn_dbg_info(n);
2665 ir_node *block = get_nodes_block(n);
2666 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2667 ir_node *new_right = get_Not_op(b);
2668 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2669 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2673 /* x ^ 1...1 -> ~1 */
2674 if (is_Const(b) && is_Const_all_one(b)) {
2675 n = new_r_Not(get_nodes_block(n), a, mode);
2676 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2680 n = transform_bitwise_distributive(n, transform_node_Eor_);
2683 n = transform_node_bitop_shift(n);
2690 static ir_node *transform_node_Eor(ir_node *n)
2692 if (is_Or_Eor_Add(n)) {
2693 dbg_info *dbgi = get_irn_dbg_info(n);
2694 ir_node *block = get_nodes_block(n);
2695 ir_node *left = get_Eor_left(n);
2696 ir_node *right = get_Eor_right(n);
2697 ir_mode *mode = get_irn_mode(n);
2698 return new_rd_Add(dbgi, block, left, right, mode);
2700 return transform_node_Eor_(n);
2704 * Do the AddSub optimization, then Transform
2705 * Constant folding on Phi
2706 * Add(a,a) -> Mul(a, 2)
2707 * Add(Mul(a, x), a) -> Mul(a, x+1)
2708 * if the mode is integer or float.
2709 * Transform Add(a,-b) into Sub(a,b).
2710 * Reassociation might fold this further.
2712 static ir_node *transform_node_Add(ir_node *n)
2720 n = fold_constant_associativity(n, tarval_add);
2724 n = transform_node_AddSub(n);
2728 a = get_Add_left(n);
2729 b = get_Add_right(n);
2730 mode = get_irn_mode(n);
2732 if (mode_is_reference(mode)) {
2733 ir_mode *lmode = get_irn_mode(a);
2735 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2736 /* an Add(a, NULL) is a hidden Conv */
2737 dbg_info *dbg = get_irn_dbg_info(n);
2738 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2742 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2743 ir_tarval *tv = get_Const_tarval(b);
2744 ir_tarval *min = get_mode_min(mode);
2745 /* if all bits are set, then this has the same effect as a Not.
2746 * Note that the following == gives false for different modes which
2747 * is exactly what we want */
2749 dbg_info *dbgi = get_irn_dbg_info(n);
2750 ir_graph *irg = get_irn_irg(n);
2751 ir_node *block = get_nodes_block(n);
2752 ir_node *cnst = new_r_Const(irg, min);
2753 return new_rd_Eor(dbgi, block, a, cnst, mode);
2757 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2759 /* for FP the following optimizations are only allowed if
2760 * fp_strict_algebraic is disabled */
2761 if (mode_is_float(mode)) {
2762 ir_graph *irg = get_irn_irg(n);
2763 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2767 if (mode_is_num(mode)) {
2768 ir_graph *irg = get_irn_irg(n);
2769 /* the following code leads to endless recursion when Mul are replaced
2770 * by a simple instruction chain */
2771 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2772 && a == b && mode_is_int(mode)) {
2773 ir_node *block = get_nodes_block(n);
2776 get_irn_dbg_info(n),
2779 new_r_Const_long(irg, mode, 2),
2781 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2786 get_irn_dbg_info(n),
2791 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2796 get_irn_dbg_info(n),
2801 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2804 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2805 /* Here we rely on constants be on the RIGHT side */
2807 ir_node *op = get_Not_op(a);
2809 if (is_Const(b) && is_Const_one(b)) {
2811 ir_node *blk = get_nodes_block(n);
2812 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2813 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2820 if (is_Or_Eor_Add(n)) {
2821 n = transform_node_Or_(n);
2824 n = transform_node_Eor_(n);
2833 * returns -cnst or NULL if impossible
2835 static ir_node *const_negate(ir_node *cnst)
2837 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2838 dbg_info *dbgi = get_irn_dbg_info(cnst);
2839 ir_graph *irg = get_irn_irg(cnst);
2840 if (tv == tarval_bad) return NULL;
2841 return new_rd_Const(dbgi, irg, tv);
2845 * Do the AddSub optimization, then Transform
2846 * Constant folding on Phi
2847 * Sub(0,a) -> Minus(a)
2848 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2849 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2850 * Sub(Add(a, x), x) -> a
2851 * Sub(x, Add(x, a)) -> -a
2852 * Sub(x, Const) -> Add(x, -Const)
2854 static ir_node *transform_node_Sub(ir_node *n)
2860 n = transform_node_AddSub(n);
2862 a = get_Sub_left(n);
2863 b = get_Sub_right(n);
2865 mode = get_irn_mode(n);
2867 if (mode_is_int(mode)) {
2868 ir_mode *lmode = get_irn_mode(a);
2870 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2871 /* a Sub(a, NULL) is a hidden Conv */
2872 dbg_info *dbg = get_irn_dbg_info(n);
2873 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2874 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2878 if (mode == lmode &&
2879 get_mode_arithmetic(mode) == irma_twos_complement &&
2881 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2883 dbg_info *dbg = get_irn_dbg_info(n);
2884 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2885 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2891 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2893 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2894 if (mode_is_float(mode)) {
2895 ir_graph *irg = get_irn_irg(n);
2896 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2900 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2901 /* a - C -> a + (-C) */
2902 ir_node *cnst = const_negate(b);
2904 ir_node *block = get_nodes_block(n);
2905 dbg_info *dbgi = get_irn_dbg_info(n);
2907 n = new_rd_Add(dbgi, block, a, cnst, mode);
2908 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2913 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2914 dbg_info *dbg = get_irn_dbg_info(n);
2915 ir_node *block = get_nodes_block(n);
2916 ir_node *left = get_Minus_op(a);
2917 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2919 n = new_rd_Minus(dbg, block, add, mode);
2920 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2922 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2923 dbg_info *dbg = get_irn_dbg_info(n);
2924 ir_node *block = get_nodes_block(n);
2925 ir_node *right = get_Minus_op(b);
2927 n = new_rd_Add(dbg, block, a, right, mode);
2928 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2930 } else if (is_Sub(b)) {
2931 /* a - (b - c) -> a + (c - b)
2932 * -> (a - b) + c iff (b - c) is a pointer */
2933 dbg_info *s_dbg = get_irn_dbg_info(b);
2934 ir_node *s_left = get_Sub_left(b);
2935 ir_node *s_right = get_Sub_right(b);
2936 ir_mode *s_mode = get_irn_mode(b);
2937 if (mode_is_reference(s_mode)) {
2938 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2939 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2940 dbg_info *a_dbg = get_irn_dbg_info(n);
2943 s_right = new_r_Conv(lowest_block, s_right, mode);
2944 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2946 ir_node *s_block = get_nodes_block(b);
2947 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2948 dbg_info *a_dbg = get_irn_dbg_info(n);
2949 ir_node *a_block = get_nodes_block(n);
2951 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2953 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2956 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2957 ir_node *m_right = get_Mul_right(b);
2958 if (is_Const(m_right)) {
2959 ir_node *cnst2 = const_negate(m_right);
2960 if (cnst2 != NULL) {
2961 dbg_info *m_dbg = get_irn_dbg_info(b);
2962 ir_node *m_block = get_nodes_block(b);
2963 ir_node *m_left = get_Mul_left(b);
2964 ir_mode *m_mode = get_irn_mode(b);
2965 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2966 dbg_info *a_dbg = get_irn_dbg_info(n);
2967 ir_node *a_block = get_nodes_block(n);
2969 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2970 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2977 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2978 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2980 get_irn_dbg_info(n),
2984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2987 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2988 ir_node *left = get_binop_left(a);
2989 ir_node *right = get_binop_right(a);
2991 /* FIXME: Does the Conv's work only for two complement or generally? */
2993 if (mode != get_irn_mode(right)) {
2994 /* This Sub is an effective Cast */
2995 right = new_r_Conv(get_nodes_block(n), right, mode);
2998 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3000 } else if (right == b) {
3001 if (mode != get_irn_mode(left)) {
3002 /* This Sub is an effective Cast */
3003 left = new_r_Conv(get_nodes_block(n), left, mode);
3006 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3010 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
3011 ir_node *left = get_binop_left(b);
3012 ir_node *right = get_binop_right(b);
3014 /* FIXME: Does the Conv's work only for two complement or generally? */
3016 ir_mode *r_mode = get_irn_mode(right);
3018 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3019 if (mode != r_mode) {
3020 /* This Sub is an effective Cast */
3021 n = new_r_Conv(get_nodes_block(n), n, mode);
3023 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3025 } else if (right == a) {
3026 ir_mode *l_mode = get_irn_mode(left);
3028 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3029 if (mode != l_mode) {
3030 /* This Sub is an effective Cast */
3031 n = new_r_Conv(get_nodes_block(n), n, mode);
3033 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3037 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3038 ir_mode *mode = get_irn_mode(a);
3040 if (mode == get_irn_mode(b)) {
3042 ir_node *op_a = get_Conv_op(a);
3043 ir_node *op_b = get_Conv_op(b);
3045 /* check if it's allowed to skip the conv */
3046 ma = get_irn_mode(op_a);
3047 mb = get_irn_mode(op_b);
3049 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3050 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3053 set_Sub_right(n, b);
3059 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3060 if (!is_reassoc_running() && is_Mul(a)) {
3061 ir_node *ma = get_Mul_left(a);
3062 ir_node *mb = get_Mul_right(a);
3065 ir_node *blk = get_nodes_block(n);
3066 ir_graph *irg = get_irn_irg(n);
3068 get_irn_dbg_info(n),
3072 get_irn_dbg_info(n),
3075 new_r_Const(irg, get_mode_one(mode)),
3078 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3080 } else if (mb == b) {
3081 ir_node *blk = get_nodes_block(n);
3082 ir_graph *irg = get_irn_irg(n);
3084 get_irn_dbg_info(n),
3088 get_irn_dbg_info(n),
3091 new_r_Const(irg, get_mode_one(mode)),
3094 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3098 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3099 ir_node *x = get_Sub_left(a);
3100 ir_node *y = get_Sub_right(a);
3101 ir_node *blk = get_nodes_block(n);
3102 ir_mode *m_b = get_irn_mode(b);
3103 ir_mode *m_y = get_irn_mode(y);
3107 /* Determine the right mode for the Add. */
3110 else if (mode_is_reference(m_b))
3112 else if (mode_is_reference(m_y))
3116 * Both modes are different but none is reference,
3117 * happens for instance in SubP(SubP(P, Iu), Is).
3118 * We have two possibilities here: Cast or ignore.
3119 * Currently we ignore this case.
3124 add = new_r_Add(blk, y, b, add_mode);
3126 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3127 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3131 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3132 /* c - ~X = X + (c+1) */
3133 if (is_Const(a) && is_Not(b)) {
3134 ir_tarval *tv = get_Const_tarval(a);
3136 tv = tarval_add(tv, get_mode_one(mode));
3137 if (tv != tarval_bad) {
3138 ir_node *blk = get_nodes_block(n);
3139 ir_graph *irg = get_irn_irg(n);
3140 ir_node *c = new_r_Const(irg, tv);
3141 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3146 /* x-(x&y) = x & ~y */
3148 ir_node *and_left = get_And_left(b);
3149 ir_node *and_right = get_And_right(b);
3150 if (and_right == a) {
3151 ir_node *tmp = and_left;
3152 and_left = and_right;
3155 if (and_left == a) {
3156 dbg_info *dbgi = get_irn_dbg_info(n);
3157 ir_node *block = get_nodes_block(n);
3158 ir_mode *mode = get_irn_mode(n);
3159 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3160 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3169 * Several transformation done on n*n=2n bits mul.
3170 * These transformations must be done here because new nodes may be produced.
3172 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3175 ir_node *a = get_Mul_left(n);
3176 ir_node *b = get_Mul_right(n);
3177 ir_tarval *ta = value_of(a);
3178 ir_tarval *tb = value_of(b);
3179 ir_mode *smode = get_irn_mode(a);
3181 if (ta == get_mode_one(smode)) {
3182 /* (L)1 * (L)b = (L)b */
3183 ir_node *blk = get_nodes_block(n);
3184 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3185 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3188 else if (ta == get_mode_minus_one(smode)) {
3189 /* (L)-1 * (L)b = (L)b */
3190 ir_node *blk = get_nodes_block(n);
3191 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3192 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3193 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3196 if (tb == get_mode_one(smode)) {
3197 /* (L)a * (L)1 = (L)a */
3198 ir_node *blk = get_irn_n(a, -1);
3199 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3200 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3203 else if (tb == get_mode_minus_one(smode)) {
3204 /* (L)a * (L)-1 = (L)-a */
3205 ir_node *blk = get_nodes_block(n);
3206 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3207 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3208 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3215 * Transform Mul(a,-1) into -a.
3216 * Do constant evaluation of Phi nodes.
3217 * Do architecture dependent optimizations on Mul nodes
3219 static ir_node *transform_node_Mul(ir_node *n)
3221 ir_node *c, *oldn = n;
3222 ir_mode *mode = get_irn_mode(n);
3223 ir_node *a = get_Mul_left(n);
3224 ir_node *b = get_Mul_right(n);
3226 n = fold_constant_associativity(n, tarval_mul);
3230 if (mode != get_irn_mode(a))
3231 return transform_node_Mul2n(n, mode);
3233 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3235 if (mode_is_signed(mode)) {
3238 if (value_of(a) == get_mode_minus_one(mode))
3240 else if (value_of(b) == get_mode_minus_one(mode))
3243 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3249 if (is_Const(b)) { /* (-a) * const -> a * -const */
3250 ir_node *cnst = const_negate(b);
3252 dbg_info *dbgi = get_irn_dbg_info(n);
3253 ir_node *block = get_nodes_block(n);
3254 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3255 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3258 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3259 dbg_info *dbgi = get_irn_dbg_info(n);
3260 ir_node *block = get_nodes_block(n);
3261 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3262 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3264 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3265 ir_node *sub_l = get_Sub_left(b);
3266 ir_node *sub_r = get_Sub_right(b);
3267 dbg_info *dbgi = get_irn_dbg_info(n);
3268 ir_node *block = get_nodes_block(n);
3269 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3270 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3271 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3274 } else if (is_Minus(b)) {
3275 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3276 ir_node *sub_l = get_Sub_left(a);
3277 ir_node *sub_r = get_Sub_right(a);
3278 dbg_info *dbgi = get_irn_dbg_info(n);
3279 ir_node *block = get_nodes_block(n);
3280 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3281 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3282 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3285 } else if (is_Shl(a)) {
3286 ir_node *const shl_l = get_Shl_left(a);
3287 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3288 /* (1 << x) * b -> b << x */
3289 dbg_info *const dbgi = get_irn_dbg_info(n);
3290 ir_node *const block = get_nodes_block(n);
3291 ir_node *const shl_r = get_Shl_right(a);
3292 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3293 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3296 } else if (is_Shl(b)) {
3297 ir_node *const shl_l = get_Shl_left(b);
3298 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3299 /* a * (1 << x) -> a << x */
3300 dbg_info *const dbgi = get_irn_dbg_info(n);
3301 ir_node *const block = get_nodes_block(n);
3302 ir_node *const shl_r = get_Shl_right(b);
3303 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3304 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3308 if (get_mode_arithmetic(mode) == irma_ieee754
3309 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3311 ir_tarval *tv = get_Const_tarval(a);
3312 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3313 && !tarval_is_negative(tv)) {
3314 /* 2.0 * b = b + b */
3315 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3316 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3320 else if (is_Const(b)) {
3321 ir_tarval *tv = get_Const_tarval(b);
3322 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3323 && !tarval_is_negative(tv)) {
3324 /* a * 2.0 = a + a */
3325 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3326 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3331 return arch_dep_replace_mul_with_shifts(n);
3335 * Transform a Div Node.
3337 static ir_node *transform_node_Div(ir_node *n)
3339 ir_mode *mode = get_Div_resmode(n);
3340 ir_node *a = get_Div_left(n);
3341 ir_node *b = get_Div_right(n);
3343 const ir_node *dummy;
3345 if (mode_is_int(mode)) {
3346 if (is_Const(b) && is_const_Phi(a)) {
3347 /* check for Div(Phi, Const) */
3348 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3350 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3353 } else if (is_Const(a) && is_const_Phi(b)) {
3354 /* check for Div(Const, Phi) */
3355 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3357 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3360 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3361 /* check for Div(Phi, Phi) */
3362 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3364 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3369 if (a == b && value_not_zero(a, &dummy)) {
3370 ir_graph *irg = get_irn_irg(n);
3371 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3372 value = new_r_Const(irg, get_mode_one(mode));
3373 DBG_OPT_CSTEVAL(n, value);
3376 if (mode_is_signed(mode) && is_Const(b)) {
3377 ir_tarval *tv = get_Const_tarval(b);
3379 if (tv == get_mode_minus_one(mode)) {
3381 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3382 DBG_OPT_CSTEVAL(n, value);
3386 /* Try architecture dependent optimization */
3387 value = arch_dep_replace_div_by_const(n);
3390 assert(mode_is_float(mode));
3392 /* Optimize x/c to x*(1/c) */
3393 if (get_mode_arithmetic(mode) == irma_ieee754) {
3394 ir_tarval *tv = value_of(b);
3396 if (tv != tarval_bad) {
3397 int rem = tarval_fp_ops_enabled();
3400 * Floating point constant folding might be disabled here to
3402 * However, as we check for exact result, doing it is safe.
3405 tarval_enable_fp_ops(1);
3406 tv = tarval_div(get_mode_one(mode), tv);
3407 tarval_enable_fp_ops(rem);
3409 /* Do the transformation if the result is either exact or we are
3410 not using strict rules. */
3411 if (tv != tarval_bad &&
3412 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3413 ir_node *block = get_nodes_block(n);
3414 ir_graph *irg = get_irn_irg(block);
3415 ir_node *c = new_r_Const(irg, tv);
3416 dbg_info *dbgi = get_irn_dbg_info(n);
3417 value = new_rd_Mul(dbgi, block, a, c, mode);
3430 /* Turn Div into a tuple (mem, jmp, bad, value) */
3431 mem = get_Div_mem(n);
3432 blk = get_nodes_block(n);
3433 irg = get_irn_irg(blk);
3435 /* skip a potential Pin */
3436 mem = skip_Pin(mem);
3437 turn_into_tuple(n, pn_Div_max+1);
3438 set_Tuple_pred(n, pn_Div_M, mem);
3439 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3440 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3441 set_Tuple_pred(n, pn_Div_res, value);
3447 * Transform a Mod node.
3449 static ir_node *transform_node_Mod(ir_node *n)
3451 ir_mode *mode = get_Mod_resmode(n);
3452 ir_node *a = get_Mod_left(n);
3453 ir_node *b = get_Mod_right(n);
3458 if (is_Const(b) && is_const_Phi(a)) {
3459 /* check for Div(Phi, Const) */
3460 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3462 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3466 else if (is_Const(a) && is_const_Phi(b)) {
3467 /* check for Div(Const, Phi) */
3468 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3470 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3474 else if (is_const_Phi(a) && is_const_Phi(b)) {
3475 /* check for Div(Phi, Phi) */
3476 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3478 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3485 irg = get_irn_irg(n);
3486 if (tv != tarval_bad) {
3487 value = new_r_Const(irg, tv);
3489 DBG_OPT_CSTEVAL(n, value);
3492 ir_node *a = get_Mod_left(n);
3493 ir_node *b = get_Mod_right(n);
3494 const ir_node *dummy;
3496 if (a == b && value_not_zero(a, &dummy)) {
3497 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3498 value = new_r_Const(irg, get_mode_null(mode));
3499 DBG_OPT_CSTEVAL(n, value);
3502 if (mode_is_signed(mode) && is_Const(b)) {
3503 ir_tarval *tv = get_Const_tarval(b);
3505 if (tv == get_mode_minus_one(mode)) {
3507 value = new_r_Const(irg, get_mode_null(mode));
3508 DBG_OPT_CSTEVAL(n, value);
3512 /* Try architecture dependent optimization */
3513 value = arch_dep_replace_mod_by_const(n);
3522 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3523 mem = get_Mod_mem(n);
3524 blk = get_nodes_block(n);
3525 irg = get_irn_irg(blk);
3527 /* skip a potential Pin */
3528 mem = skip_Pin(mem);
3529 turn_into_tuple(n, pn_Mod_max+1);
3530 set_Tuple_pred(n, pn_Mod_M, mem);
3531 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3532 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3533 set_Tuple_pred(n, pn_Mod_res, value);
3539 * Transform a Cond node.
3541 * Replace the Cond by a Jmp if it branches on a constant
3544 static ir_node *transform_node_Cond(ir_node *n)
3546 ir_node *a = get_Cond_selector(n);
3547 ir_graph *irg = get_irn_irg(n);
3551 /* we need block info which is not available in floating irgs */
3552 if (get_irg_pinned(irg) == op_pin_state_floats)
3556 if (ta == tarval_bad && is_Cmp(a)) {
3557 /* try again with a direct call to compute_cmp, as we don't care
3558 * about the MODEB_LOWERED flag here */
3559 ta = compute_cmp(a);
3562 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3563 /* It's a boolean Cond, branching on a boolean constant.
3564 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3565 ir_node *blk = get_nodes_block(n);
3566 jmp = new_r_Jmp(blk);
3567 turn_into_tuple(n, pn_Cond_max+1);
3568 if (ta == tarval_b_true) {
3569 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3570 set_Tuple_pred(n, pn_Cond_true, jmp);
3572 set_Tuple_pred(n, pn_Cond_false, jmp);
3573 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3575 /* We might generate an endless loop, so keep it alive. */
3576 add_End_keepalive(get_irg_end(irg), blk);
3577 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3582 static ir_node *transform_node_Switch(ir_node *n)
3584 ir_node *op = get_Switch_selector(n);
3585 ir_tarval *val = value_of(op);
3586 if (val != tarval_bad) {
3587 dbg_info *dbgi = get_irn_dbg_info(n);
3588 ir_graph *irg = get_irn_irg(n);
3589 unsigned n_outs = get_Switch_n_outs(n);
3590 ir_node *block = get_nodes_block(n);
3591 ir_node *bad = new_r_Bad(irg, mode_X);
3592 ir_node **in = XMALLOCN(ir_node*, n_outs);
3593 const ir_switch_table *table = get_Switch_table(n);
3594 size_t n_entries = ir_switch_table_get_n_entries(table);
3598 for (i = 0; i < n_entries; ++i) {
3599 const ir_switch_table_entry *entry
3600 = ir_switch_table_get_entry_const(table, i);
3601 ir_tarval *min = entry->min;
3602 ir_tarval *max = entry->max;
3605 if ((min == max && min == val)
3606 || (tarval_cmp(val, min) != ir_relation_less
3607 && tarval_cmp(val, max) != ir_relation_greater)) {
3612 for (o = 0; o < n_outs; ++o) {
3613 if (o == (unsigned)jmp_pn) {
3614 in[o] = new_rd_Jmp(dbgi, block);
3619 return new_r_Tuple(block, (int)n_outs, in);
3625 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3627 * - and, or, xor instead of &
3628 * - Shl, Shr, Shrs, rotl instead of >>
3629 * (with a special case for Or/Xor + Shrs)
3631 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3633 static ir_node *transform_node_shift_bitop(ir_node *n)
3635 ir_graph *irg = get_irn_irg(n);
3636 ir_node *right = get_binop_right(n);
3637 ir_mode *mode = get_irn_mode(n);
3639 ir_node *bitop_left;
3640 ir_node *bitop_right;
3649 ir_tarval *tv_shift;
3651 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3654 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3656 if (!is_Const(right))
3659 left = get_binop_left(n);
3660 op_left = get_irn_op(left);
3661 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3664 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3665 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3666 /* TODO: test if sign bit is affectes */
3670 bitop_right = get_binop_right(left);
3671 if (!is_Const(bitop_right))
3674 bitop_left = get_binop_left(left);
3676 block = get_nodes_block(n);
3677 dbgi = get_irn_dbg_info(n);
3678 tv1 = get_Const_tarval(bitop_right);
3679 tv2 = get_Const_tarval(right);
3681 assert(get_tarval_mode(tv1) == mode);
3684 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3685 tv_shift = tarval_shl(tv1, tv2);
3686 } else if (is_Shr(n)) {
3687 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3688 tv_shift = tarval_shr(tv1, tv2);
3689 } else if (is_Shrs(n)) {
3690 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3691 tv_shift = tarval_shrs(tv1, tv2);
3694 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3695 tv_shift = tarval_rotl(tv1, tv2);
3698 assert(get_tarval_mode(tv_shift) == mode);
3699 irg = get_irn_irg(n);
3700 new_const = new_r_Const(irg, tv_shift);
3702 if (op_left == op_And) {
3703 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3704 } else if (op_left == op_Or) {
3705 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3707 assert(op_left == op_Eor);
3708 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3717 static ir_node *transform_node_And(ir_node *n)
3719 ir_node *c, *oldn = n;
3720 ir_node *a = get_And_left(n);
3721 ir_node *b = get_And_right(n);
3724 n = fold_constant_associativity(n, tarval_and);
3728 if (is_Cmp(a) && is_Cmp(b)) {
3729 ir_node *a_left = get_Cmp_left(a);
3730 ir_node *a_right = get_Cmp_right(a);
3731 ir_node *b_left = get_Cmp_left(b);
3732 ir_node *b_right = get_Cmp_right(b);
3733 ir_relation a_relation = get_Cmp_relation(a);
3734 ir_relation b_relation = get_Cmp_relation(b);
3735 /* we can combine the relations of two compares with the same
3737 if (a_left == b_left && b_left == b_right) {
3738 dbg_info *dbgi = get_irn_dbg_info(n);
3739 ir_node *block = get_nodes_block(n);
3740 ir_relation new_relation = a_relation & b_relation;
3741 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3743 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3744 if (a_relation == b_relation && a_relation == ir_relation_equal
3745 && !mode_is_float(get_irn_mode(a_left))
3746 && !mode_is_float(get_irn_mode(b_left))) {
3747 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3748 dbg_info *dbgi = get_irn_dbg_info(n);
3749 ir_node *block = get_nodes_block(n);
3750 ir_mode *a_mode = get_irn_mode(a_left);
3751 ir_mode *b_mode = get_irn_mode(b_left);
3752 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3753 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3754 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3755 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3756 ir_graph *irg = get_irn_irg(n);
3757 ir_node *zero = create_zero_const(irg, b_mode);
3758 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3760 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3761 dbg_info *dbgi = get_irn_dbg_info(n);
3762 ir_node *block = get_nodes_block(n);
3763 ir_mode *a_mode = get_irn_mode(a_left);
3764 ir_mode *b_mode = get_irn_mode(b_left);
3765 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3766 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3767 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3768 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3769 ir_graph *irg = get_irn_irg(n);
3770 ir_node *zero = create_zero_const(irg, a_mode);
3771 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3776 mode = get_irn_mode(n);
3777 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3779 if (is_Or(a) || is_Or_Eor_Add(a)) {
3780 ir_node *or_left = get_binop_left(a);
3781 ir_node *or_right = get_binop_right(a);
3782 if (complement_values(or_left, b)) {
3783 /* (a|b) & ~a => b & ~a */
3784 dbg_info *dbgi = get_irn_dbg_info(n);
3785 ir_node *block = get_nodes_block(n);
3786 return new_rd_And(dbgi, block, or_right, b, mode);
3787 } else if (complement_values(or_right, b)) {
3788 /* (a|b) & ~b => a & ~b */
3789 dbg_info *dbgi = get_irn_dbg_info(n);
3790 ir_node *block = get_nodes_block(n);
3791 return new_rd_And(dbgi, block, or_left, b, mode);
3792 } else if (is_Not(b)) {
3793 ir_node *op = get_Not_op(b);
3795 ir_node *ba = get_And_left(op);
3796 ir_node *bb = get_And_right(op);
3798 /* it's enough to test the following cases due to normalization! */
3799 if (or_left == ba && or_right == bb) {
3800 /* (a|b) & ~(a&b) = a^b */
3801 ir_node *block = get_nodes_block(n);
3803 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3804 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3810 if (is_Or(b) || is_Or_Eor_Add(b)) {
3811 ir_node *or_left = get_binop_left(b);
3812 ir_node *or_right = get_binop_right(b);
3813 if (complement_values(or_left, a)) {
3814 /* (a|b) & ~a => b & ~a */
3815 dbg_info *dbgi = get_irn_dbg_info(n);
3816 ir_node *block = get_nodes_block(n);
3817 return new_rd_And(dbgi, block, or_right, a, mode);
3818 } else if (complement_values(or_right, a)) {
3819 /* (a|b) & ~b => a & ~b */
3820 dbg_info *dbgi = get_irn_dbg_info(n);
3821 ir_node *block = get_nodes_block(n);
3822 return new_rd_And(dbgi, block, or_left, a, mode);
3823 } else if (is_Not(a)) {
3824 ir_node *op = get_Not_op(a);
3826 ir_node *aa = get_And_left(op);
3827 ir_node *ab = get_And_right(op);
3829 /* it's enough to test the following cases due to normalization! */
3830 if (or_left == aa && or_right == ab) {
3831 /* (a|b) & ~(a&b) = a^b */
3832 ir_node *block = get_nodes_block(n);
3834 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3835 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3841 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3842 ir_node *al = get_binop_left(a);
3843 ir_node *ar = get_binop_right(a);
3846 /* (b ^ a) & b -> ~a & b */
3847 dbg_info *dbg = get_irn_dbg_info(n);
3848 ir_node *block = get_nodes_block(n);
3850 ar = new_rd_Not(dbg, block, ar, mode);
3851 n = new_rd_And(dbg, block, ar, b, mode);
3852 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3856 /* (a ^ b) & b -> ~a & b */
3857 dbg_info *dbg = get_irn_dbg_info(n);
3858 ir_node *block = get_nodes_block(n);
3860 al = new_rd_Not(dbg, block, al, mode);
3861 n = new_rd_And(dbg, block, al, b, mode);
3862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3866 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3867 ir_node *bl = get_binop_left(b);
3868 ir_node *br = get_binop_right(b);
3871 /* a & (a ^ b) -> a & ~b */
3872 dbg_info *dbg = get_irn_dbg_info(n);
3873 ir_node *block = get_nodes_block(n);
3875 br = new_rd_Not(dbg, block, br, mode);
3876 n = new_rd_And(dbg, block, br, a, mode);
3877 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3881 /* a & (b ^ a) -> a & ~b */
3882 dbg_info *dbg = get_irn_dbg_info(n);
3883 ir_node *block = get_nodes_block(n);
3885 bl = new_rd_Not(dbg, block, bl, mode);
3886 n = new_rd_And(dbg, block, bl, a, mode);
3887 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3891 if (is_Not(a) && is_Not(b)) {
3892 /* ~a & ~b = ~(a|b) */
3893 ir_node *block = get_nodes_block(n);
3894 ir_mode *mode = get_irn_mode(n);
3898 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3899 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3900 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3905 vrp_attr *b_vrp = vrp_get_info(b);
3906 ir_tarval *a_val = get_Const_tarval(a);
3907 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3913 vrp_attr *a_vrp = vrp_get_info(a);
3914 ir_tarval *b_val = get_Const_tarval(b);
3915 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3920 n = transform_bitwise_distributive(n, transform_node_And);
3922 n = transform_node_bitop_shift(n);
3930 static ir_node *transform_node_Not(ir_node *n)
3932 ir_node *c, *oldn = n;
3933 ir_node *a = get_Not_op(n);
3934 ir_mode *mode = get_irn_mode(n);
3936 HANDLE_UNOP_PHI(tarval_not,a,c);
3938 /* check for a boolean Not */
3940 dbg_info *dbgi = get_irn_dbg_info(a);
3941 ir_node *block = get_nodes_block(a);
3942 ir_relation relation = get_Cmp_relation(a);
3943 relation = get_negated_relation(relation);
3944 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3945 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3949 /* normalize ~(a ^ b) => a ^ ~b */
3950 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3951 dbg_info *dbg = get_irn_dbg_info(n);
3952 ir_node *block = get_nodes_block(n);
3953 ir_node *eor_right = get_binop_right(a);
3954 ir_node *eor_left = get_binop_left(a);
3955 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3956 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3960 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3961 if (is_Minus(a)) { /* ~-x -> x + -1 */
3962 dbg_info *dbg = get_irn_dbg_info(n);
3963 ir_graph *irg = get_irn_irg(n);
3964 ir_node *block = get_nodes_block(n);
3965 ir_node *add_l = get_Minus_op(a);
3966 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3967 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3968 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3969 ir_node *add_r = get_binop_right(a);
3970 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3971 /* ~(x + -1) = -x */
3972 ir_node *op = get_binop_left(a);
3973 ir_node *blk = get_nodes_block(n);
3974 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3975 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3983 * Transform a Minus.
3987 * -(a >>u (size-1)) = a >>s (size-1)
3988 * -(a >>s (size-1)) = a >>u (size-1)
3989 * -(a * const) -> a * -const
3991 static ir_node *transform_node_Minus(ir_node *n)
3993 ir_node *c, *oldn = n;
3994 ir_node *a = get_Minus_op(n);
3997 HANDLE_UNOP_PHI(tarval_neg,a,c);
3999 mode = get_irn_mode(a);
4000 if (get_mode_arithmetic(mode) == irma_twos_complement) {
4001 /* the following rules are only to twos-complement */
4004 ir_node *op = get_Not_op(a);
4005 ir_tarval *tv = get_mode_one(mode);
4006 ir_node *blk = get_nodes_block(n);
4007 ir_graph *irg = get_irn_irg(blk);
4008 ir_node *c = new_r_Const(irg, tv);
4009 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
4010 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4014 ir_node *c = get_Shr_right(a);
4017 ir_tarval *tv = get_Const_tarval(c);
4019 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4020 /* -(a >>u (size-1)) = a >>s (size-1) */
4021 ir_node *v = get_Shr_left(a);
4023 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4024 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4030 ir_node *c = get_Shrs_right(a);
4033 ir_tarval *tv = get_Const_tarval(c);
4035 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4036 /* -(a >>s (size-1)) = a >>u (size-1) */
4037 ir_node *v = get_Shrs_left(a);
4039 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4040 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4047 /* - (a-b) = b - a */
4048 ir_node *la = get_Sub_left(a);
4049 ir_node *ra = get_Sub_right(a);
4050 ir_node *blk = get_nodes_block(n);
4052 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4053 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4057 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4058 ir_node *mul_l = get_Mul_left(a);
4059 ir_node *mul_r = get_Mul_right(a);
4060 ir_tarval *tv = value_of(mul_r);
4061 if (tv != tarval_bad) {
4062 tv = tarval_neg(tv);
4063 if (tv != tarval_bad) {
4064 ir_graph *irg = get_irn_irg(n);
4065 ir_node *cnst = new_r_Const(irg, tv);
4066 dbg_info *dbg = get_irn_dbg_info(a);
4067 ir_node *block = get_nodes_block(a);
4068 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4069 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4079 * Transform a Proj(Load) with a non-null address.
4081 static ir_node *transform_node_Proj_Load(ir_node *proj)
4083 if (get_opt_ldst_only_null_ptr_exceptions()) {
4084 if (get_irn_mode(proj) == mode_X) {
4085 ir_node *load = get_Proj_pred(proj);
4087 /* get the Load address */
4088 const ir_node *addr = get_Load_ptr(load);
4089 const ir_node *confirm;
4091 if (value_not_null(addr, &confirm)) {
4092 if (confirm == NULL) {
4093 /* this node may float if it did not depend on a Confirm */
4094 set_irn_pinned(load, op_pin_state_floats);
4096 if (get_Proj_proj(proj) == pn_Load_X_except) {
4097 ir_graph *irg = get_irn_irg(proj);
4098 DBG_OPT_EXC_REM(proj);
4099 return new_r_Bad(irg, mode_X);
4101 ir_node *blk = get_nodes_block(load);
4102 return new_r_Jmp(blk);
4111 * Transform a Proj(Store) with a non-null address.
4113 static ir_node *transform_node_Proj_Store(ir_node *proj)
4115 if (get_opt_ldst_only_null_ptr_exceptions()) {
4116 if (get_irn_mode(proj) == mode_X) {
4117 ir_node *store = get_Proj_pred(proj);
4119 /* get the load/store address */
4120 const ir_node *addr = get_Store_ptr(store);
4121 const ir_node *confirm;
4123 if (value_not_null(addr, &confirm)) {
4124 if (confirm == NULL) {
4125 /* this node may float if it did not depend on a Confirm */
4126 set_irn_pinned(store, op_pin_state_floats);
4128 if (get_Proj_proj(proj) == pn_Store_X_except) {
4129 ir_graph *irg = get_irn_irg(proj);
4130 DBG_OPT_EXC_REM(proj);
4131 return new_r_Bad(irg, mode_X);
4133 ir_node *blk = get_nodes_block(store);
4134 return new_r_Jmp(blk);
4143 * Transform a Proj(Div) with a non-zero value.
4144 * Removes the exceptions and routes the memory to the NoMem node.
4146 static ir_node *transform_node_Proj_Div(ir_node *proj)
4148 ir_node *div = get_Proj_pred(proj);
4149 ir_node *b = get_Div_right(div);
4150 ir_node *res, *new_mem;
4151 const ir_node *confirm;
4154 if (value_not_zero(b, &confirm)) {
4155 /* div(x, y) && y != 0 */
4156 if (confirm == NULL) {
4157 /* we are sure we have a Const != 0 */
4158 new_mem = get_Div_mem(div);
4159 new_mem = skip_Pin(new_mem);
4160 set_Div_mem(div, new_mem);
4161 set_irn_pinned(div, op_pin_state_floats);
4164 proj_nr = get_Proj_proj(proj);
4166 case pn_Div_X_regular:
4167 return new_r_Jmp(get_nodes_block(div));
4169 case pn_Div_X_except: {
4170 ir_graph *irg = get_irn_irg(proj);
4171 /* we found an exception handler, remove it */
4172 DBG_OPT_EXC_REM(proj);
4173 return new_r_Bad(irg, mode_X);
4177 ir_graph *irg = get_irn_irg(proj);
4178 res = get_Div_mem(div);
4179 new_mem = get_irg_no_mem(irg);
4182 /* This node can only float up to the Confirm block */
4183 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4185 set_irn_pinned(div, op_pin_state_floats);
4186 /* this is a Div without exception, we can remove the memory edge */
4187 set_Div_mem(div, new_mem);
4196 * Transform a Proj(Mod) with a non-zero value.
4197 * Removes the exceptions and routes the memory to the NoMem node.
4199 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4201 ir_node *mod = get_Proj_pred(proj);
4202 ir_node *b = get_Mod_right(mod);
4203 ir_node *res, *new_mem;
4204 const ir_node *confirm;
4207 if (value_not_zero(b, &confirm)) {
4208 /* mod(x, y) && y != 0 */
4209 proj_nr = get_Proj_proj(proj);
4211 if (confirm == NULL) {
4212 /* we are sure we have a Const != 0 */
4213 new_mem = get_Mod_mem(mod);
4214 new_mem = skip_Pin(new_mem);
4215 set_Mod_mem(mod, new_mem);
4216 set_irn_pinned(mod, op_pin_state_floats);
4221 case pn_Mod_X_regular:
4222 return new_r_Jmp(get_irn_n(mod, -1));
4224 case pn_Mod_X_except: {
4225 ir_graph *irg = get_irn_irg(proj);
4226 /* we found an exception handler, remove it */
4227 DBG_OPT_EXC_REM(proj);
4228 return new_r_Bad(irg, mode_X);
4232 ir_graph *irg = get_irn_irg(proj);
4233 res = get_Mod_mem(mod);
4234 new_mem = get_irg_no_mem(irg);
4237 /* This node can only float up to the Confirm block */
4238 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4240 /* this is a Mod without exception, we can remove the memory edge */
4241 set_Mod_mem(mod, new_mem);
4245 if (get_Mod_left(mod) == b) {
4246 /* a % a = 0 if a != 0 */
4247 ir_graph *irg = get_irn_irg(proj);
4248 ir_mode *mode = get_irn_mode(proj);
4249 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4251 DBG_OPT_CSTEVAL(mod, res);
4260 * return true if the operation returns a value with exactly 1 bit set
4262 static bool is_single_bit(const ir_node *node)
4264 /* a first implementation, could be extended with vrp and others... */
4266 ir_node *shl_l = get_Shl_left(node);
4267 ir_mode *mode = get_irn_mode(node);
4268 int modulo = get_mode_modulo_shift(mode);
4269 /* this works if we shift a 1 and we have modulo shift */
4270 if (is_Const(shl_l) && is_Const_one(shl_l)
4271 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4274 } else if (is_Const(node)) {
4275 ir_tarval *tv = get_Const_tarval(node);
4276 return tarval_is_single_bit(tv);
4282 * checks if node just flips a bit in another node and returns that other node
4283 * if so. @p tv should be a value having just 1 bit set
4285 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4288 return get_Not_op(node);
4290 ir_node *right = get_Eor_right(node);
4291 if (is_Const(right)) {
4292 ir_tarval *right_tv = get_Const_tarval(right);
4293 ir_mode *mode = get_irn_mode(node);
4294 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4295 return get_Eor_left(node);
4302 * Normalizes and optimizes Cmp nodes.
4304 static ir_node *transform_node_Cmp(ir_node *n)
4306 ir_node *left = get_Cmp_left(n);
4307 ir_node *right = get_Cmp_right(n);
4308 ir_mode *mode = get_irn_mode(left);
4309 ir_tarval *tv = NULL;
4310 bool changed = false;
4311 bool changedc = false;
4312 ir_relation relation = get_Cmp_relation(n);
4313 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4315 /* mask out impossible relations */
4316 ir_relation new_relation = relation & possible;
4317 if (new_relation != relation) {
4318 relation = new_relation;
4322 /* Remove unnecessary conversions */
4323 if (is_Conv(left) && is_Conv(right)) {
4324 ir_node *op_left = get_Conv_op(left);
4325 ir_node *op_right = get_Conv_op(right);
4326 ir_mode *mode_left = get_irn_mode(op_left);
4327 ir_mode *mode_right = get_irn_mode(op_right);
4329 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4330 && mode_left != mode_b && mode_right != mode_b) {
4331 ir_node *block = get_nodes_block(n);
4333 if (mode_left == mode_right) {
4337 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4338 } else if (smaller_mode(mode_left, mode_right)) {
4339 left = new_r_Conv(block, op_left, mode_right);
4342 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4343 } else if (smaller_mode(mode_right, mode_left)) {
4345 right = new_r_Conv(block, op_right, mode_left);
4347 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4349 mode = get_irn_mode(left);
4352 if (is_Conv(left) && is_Const(right)) {
4353 ir_node *op_left = get_Conv_op(left);
4354 ir_mode *mode_left = get_irn_mode(op_left);
4355 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4356 ir_tarval *tv = get_Const_tarval(right);
4357 tarval_int_overflow_mode_t last_mode
4358 = tarval_get_integer_overflow_mode();
4360 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4361 new_tv = tarval_convert_to(tv, mode_left);
4362 tarval_set_integer_overflow_mode(last_mode);
4363 if (new_tv != tarval_bad) {
4364 ir_graph *irg = get_irn_irg(n);
4366 right = new_r_Const(irg, new_tv);
4367 mode = get_irn_mode(left);
4369 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4375 * Optimize -a CMP -b into b CMP a.
4376 * This works only for modes where unary Minus cannot Overflow.
4377 * Note that two-complement integers can Overflow so it will NOT work.
4379 if (!mode_overflow_on_unary_Minus(mode) &&
4380 is_Minus(left) && is_Minus(right)) {
4381 left = get_Minus_op(left);
4382 right = get_Minus_op(right);
4383 relation = get_inversed_relation(relation);
4385 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4388 /* remove operation on both sides if possible */
4389 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4391 * The following operations are NOT safe for floating point operations, for instance
4392 * 1.0 + inf == 2.0 + inf, =/=> x == y
4394 if (mode_is_int(mode)) {
4395 unsigned lop = get_irn_opcode(left);
4397 if (lop == get_irn_opcode(right)) {
4398 ir_node *ll, *lr, *rl, *rr;
4400 /* same operation on both sides, try to remove */
4404 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4405 left = get_unop_op(left);
4406 right = get_unop_op(right);
4408 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4411 ll = get_Add_left(left);
4412 lr = get_Add_right(left);
4413 rl = get_Add_left(right);
4414 rr = get_Add_right(right);
4417 /* X + a CMP X + b ==> a CMP b */
4421 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4422 } else if (ll == rr) {
4423 /* X + a CMP b + X ==> a CMP b */
4427 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4428 } else if (lr == rl) {
4429 /* a + X CMP X + b ==> a CMP b */
4433 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4434 } else if (lr == rr) {
4435 /* a + X CMP b + X ==> a CMP b */
4439 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4443 ll = get_Sub_left(left);
4444 lr = get_Sub_right(left);
4445 rl = get_Sub_left(right);
4446 rr = get_Sub_right(right);
4449 /* X - a CMP X - b ==> a CMP b */
4453 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4454 } else if (lr == rr) {
4455 /* a - X CMP b - X ==> a CMP b */
4459 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4463 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4464 /* a ROTL X CMP b ROTL X ==> a CMP b */
4465 left = get_Rotl_left(left);
4466 right = get_Rotl_left(right);
4468 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4476 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4477 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4478 ir_node *ll = get_binop_left(left);
4479 ir_node *lr = get_binop_right(left);
4481 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4487 ir_graph *irg = get_irn_irg(n);
4489 right = create_zero_const(irg, mode);
4491 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4494 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4495 ir_node *rl = get_binop_left(right);
4496 ir_node *rr = get_binop_right(right);
4498 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4504 ir_graph *irg = get_irn_irg(n);
4506 right = create_zero_const(irg, mode);
4508 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4512 if (is_And(left) && is_Const(right)) {
4513 ir_node *ll = get_binop_left(left);
4514 ir_node *lr = get_binop_right(left);
4515 if (is_Shr(ll) && is_Const(lr)) {
4516 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4517 ir_node *block = get_nodes_block(n);
4518 ir_mode *mode = get_irn_mode(left);
4520 ir_node *llr = get_Shr_right(ll);
4521 if (is_Const(llr)) {
4522 dbg_info *dbg = get_irn_dbg_info(left);
4523 ir_graph *irg = get_irn_irg(left);
4525 ir_tarval *c1 = get_Const_tarval(llr);
4526 ir_tarval *c2 = get_Const_tarval(lr);
4527 ir_tarval *c3 = get_Const_tarval(right);
4528 ir_tarval *mask = tarval_shl(c2, c1);
4529 ir_tarval *value = tarval_shl(c3, c1);
4531 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4532 right = new_r_Const(irg, value);
4537 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4539 if (is_Const(right) && is_Const_null(right) &&
4540 (is_Eor(left) || is_Or_Eor_Add(left))) {
4541 right = get_Eor_right(left);
4542 left = get_Eor_left(left);
4548 if (mode_is_int(mode) && is_And(left)) {
4549 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4550 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4551 if (relation == ir_relation_equal
4552 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4553 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4554 ir_node *and0 = get_And_left(left);
4555 ir_node *and1 = get_And_right(left);
4556 if (and1 == right) {
4557 ir_node *tmp = and0;
4561 if (and0 == right && is_single_bit(and0)) {
4562 ir_graph *irg = get_irn_irg(n);
4564 relation == ir_relation_equal ? ir_relation_less_greater
4565 : ir_relation_equal;
4566 right = create_zero_const(irg, mode);
4572 if (is_Const(right) && is_Const_null(right) &&
4573 (relation == ir_relation_equal
4574 || (relation == ir_relation_less_greater)
4575 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4577 /* instead of flipping the bit before the bit-test operation negate
4579 ir_node *and0 = get_And_left(left);
4580 ir_node *and1 = get_And_right(left);
4581 if (is_Const(and1)) {
4582 ir_tarval *tv = get_Const_tarval(and1);
4583 if (tarval_is_single_bit(tv)) {
4584 ir_node *flipped = flips_bit(and0, tv);
4585 if (flipped != NULL) {
4586 dbg_info *dbgi = get_irn_dbg_info(left);
4587 ir_node *block = get_nodes_block(left);
4588 relation = get_negated_relation(relation);
4589 left = new_rd_And(dbgi, block, flipped, and1, mode);
4598 /* replace mode_b compares with ands/ors */
4599 if (mode == mode_b) {
4600 ir_node *block = get_nodes_block(n);
4604 case ir_relation_less_equal:
4605 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4607 case ir_relation_less:
4608 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4610 case ir_relation_greater_equal:
4611 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4613 case ir_relation_greater:
4614 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4616 case ir_relation_less_greater:
4617 bres = new_r_Eor(block, left, right, mode_b);
4619 case ir_relation_equal:
4620 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4623 #ifdef DEBUG_libfirm
4624 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4629 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4635 * First step: normalize the compare op
4636 * by placing the constant on the right side
4637 * or moving the lower address node to the left.
4639 if (!operands_are_normalized(left, right)) {
4644 relation = get_inversed_relation(relation);
4649 * Second step: Try to reduce the magnitude
4650 * of a constant. This may help to generate better code
4651 * later and may help to normalize more compares.
4652 * Of course this is only possible for integer values.
4654 tv = value_of(right);
4655 if (tv != tarval_bad) {
4656 ir_mode *mode = get_irn_mode(right);
4658 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4659 * cmp(ct,c2) | cmp(cf,c2) | result
4660 * -----------|------------|--------
4661 * true | true | True
4662 * false | false | False
4664 * false | true | not(x)
4667 ir_node *mux_true = get_Mux_true(left);
4668 ir_node *mux_false = get_Mux_false(left);
4669 if (is_Const(mux_true) && is_Const(mux_false)) {
4670 /* we can fold true/false constant separately */
4671 ir_tarval *tv_true = get_Const_tarval(mux_true);
4672 ir_tarval *tv_false = get_Const_tarval(mux_false);
4673 ir_relation r_true = tarval_cmp(tv_true, tv);
4674 ir_relation r_false = tarval_cmp(tv_false, tv);
4675 if (r_true != ir_relation_false
4676 || r_false != ir_relation_false) {
4677 bool rel_true = (r_true & relation) != 0;
4678 bool rel_false = (r_false & relation) != 0;
4679 ir_node *cond = get_Mux_sel(left);
4680 if (rel_true == rel_false) {
4681 relation = rel_true ? ir_relation_true
4682 : ir_relation_false;
4683 } else if (rel_true) {
4686 dbg_info *dbgi = get_irn_dbg_info(n);
4687 ir_node *block = get_nodes_block(n);
4688 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4695 /* TODO extend to arbitrary constants */
4696 if (is_Conv(left) && tarval_is_null(tv)) {
4697 ir_node *op = get_Conv_op(left);
4698 ir_mode *op_mode = get_irn_mode(op);
4701 * UpConv(x) REL 0 ==> x REL 0
4702 * Don't do this for float values as it's unclear whether it is a
4703 * win. (on the other side it makes detection/creation of fabs hard)
4705 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4706 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4707 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4708 !mode_is_float(mode)) {
4709 tv = get_mode_null(op_mode);
4713 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4717 if (tv != tarval_bad) {
4718 /* the following optimization is possible on modes without Overflow
4719 * on Unary Minus or on == and !=:
4720 * -a CMP c ==> a swap(CMP) -c
4722 * Beware: for two-complement Overflow may occur, so only == and != can
4723 * be optimized, see this:
4724 * -MININT < 0 =/=> MININT > 0 !!!
4726 if (is_Minus(left) &&
4727 (!mode_overflow_on_unary_Minus(mode) ||
4728 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4729 tv = tarval_neg(tv);
4731 if (tv != tarval_bad) {
4732 left = get_Minus_op(left);
4733 relation = get_inversed_relation(relation);
4735 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4737 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4738 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4739 tv = tarval_not(tv);
4741 if (tv != tarval_bad) {
4742 left = get_Not_op(left);
4744 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4748 /* for integer modes, we have more */
4749 if (mode_is_int(mode) && !is_Const(left)) {
4750 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4751 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4752 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4753 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4755 if (tv != tarval_bad) {
4756 relation ^= ir_relation_equal;
4758 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4761 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4762 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4763 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4764 tv = tarval_add(tv, get_mode_one(mode));
4766 if (tv != tarval_bad) {
4767 relation ^= ir_relation_equal;
4769 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4773 /* the following reassociations work only for == and != */
4774 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4775 if (tv != tarval_bad) {
4776 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4778 ir_node *c1 = get_Sub_right(left);
4779 ir_tarval *tv2 = value_of(c1);
4781 if (tv2 != tarval_bad) {
4782 tv2 = tarval_add(tv, value_of(c1));
4784 if (tv2 != tarval_bad) {
4785 left = get_Sub_left(left);
4788 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4792 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4793 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4794 ir_node *a_l = get_binop_left(left);
4795 ir_node *a_r = get_binop_right(left);
4799 if (is_Const(a_l)) {
4801 tv2 = value_of(a_l);
4804 tv2 = value_of(a_r);
4807 if (tv2 != tarval_bad) {
4808 tv2 = tarval_sub(tv, tv2, NULL);
4810 if (tv2 != tarval_bad) {
4814 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4818 /* -a == c ==> a == -c, -a != c ==> a != -c */
4819 else if (is_Minus(left)) {
4820 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4822 if (tv2 != tarval_bad) {
4823 left = get_Minus_op(left);
4826 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4833 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4834 switch (get_irn_opcode(left)) {
4838 c1 = get_And_right(left);
4841 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4842 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4844 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4846 /* TODO: move to constant evaluation */
4847 ir_graph *irg = get_irn_irg(n);
4848 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4849 c1 = new_r_Const(irg, tv);
4850 DBG_OPT_CSTEVAL(n, c1);
4854 if (tarval_is_single_bit(tv)) {
4856 * optimization for AND:
4858 * And(x, C) == C ==> And(x, C) != 0
4859 * And(x, C) != C ==> And(X, C) == 0
4861 * if C is a single Bit constant.
4864 /* check for Constant's match. We have check hare the tarvals,
4865 because our const might be changed */
4866 if (get_Const_tarval(c1) == tv) {
4867 /* fine: do the transformation */
4868 tv = get_mode_null(get_tarval_mode(tv));
4869 relation ^= ir_relation_less_equal_greater;
4871 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4877 c1 = get_Or_right(left);
4878 if (is_Const(c1) && tarval_is_null(tv)) {
4880 * Or(x, C) == 0 && C != 0 ==> FALSE
4881 * Or(x, C) != 0 && C != 0 ==> TRUE
4883 if (! tarval_is_null(get_Const_tarval(c1))) {
4884 /* TODO: move to constant evaluation */
4885 ir_graph *irg = get_irn_irg(n);
4886 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4887 c1 = new_r_Const(irg, tv);
4888 DBG_OPT_CSTEVAL(n, c1);
4895 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4897 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4900 c1 = get_Shl_right(left);
4902 ir_graph *irg = get_irn_irg(c1);
4903 ir_tarval *tv1 = get_Const_tarval(c1);
4904 ir_mode *mode = get_irn_mode(left);
4905 ir_tarval *minus1 = get_mode_all_one(mode);
4906 ir_tarval *amask = tarval_shr(minus1, tv1);
4907 ir_tarval *cmask = tarval_shl(minus1, tv1);
4910 if (tarval_and(tv, cmask) != tv) {
4911 /* condition not met */
4912 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4913 c1 = new_r_Const(irg, tv);
4914 DBG_OPT_CSTEVAL(n, c1);
4917 sl = get_Shl_left(left);
4918 blk = get_nodes_block(n);
4919 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4920 tv = tarval_shr(tv, tv1);
4922 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4927 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4929 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4932 c1 = get_Shr_right(left);
4934 ir_graph *irg = get_irn_irg(c1);
4935 ir_tarval *tv1 = get_Const_tarval(c1);
4936 ir_mode *mode = get_irn_mode(left);
4937 ir_tarval *minus1 = get_mode_all_one(mode);
4938 ir_tarval *amask = tarval_shl(minus1, tv1);
4939 ir_tarval *cmask = tarval_shr(minus1, tv1);
4942 if (tarval_and(tv, cmask) != tv) {
4943 /* condition not met */
4944 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4945 c1 = new_r_Const(irg, tv);
4946 DBG_OPT_CSTEVAL(n, c1);
4949 sl = get_Shr_left(left);
4950 blk = get_nodes_block(n);
4951 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4952 tv = tarval_shl(tv, tv1);
4954 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4959 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4961 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4964 c1 = get_Shrs_right(left);
4966 ir_graph *irg = get_irn_irg(c1);
4967 ir_tarval *tv1 = get_Const_tarval(c1);
4968 ir_mode *mode = get_irn_mode(left);
4969 ir_tarval *minus1 = get_mode_all_one(mode);
4970 ir_tarval *amask = tarval_shl(minus1, tv1);
4971 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4974 cond = tarval_sub(cond, tv1, NULL);
4975 cond = tarval_shrs(tv, cond);
4977 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4978 /* condition not met */
4979 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4980 c1 = new_r_Const(irg, tv);
4981 DBG_OPT_CSTEVAL(n, c1);
4984 sl = get_Shrs_left(left);
4985 blk = get_nodes_block(n);
4986 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4987 tv = tarval_shl(tv, tv1);
4989 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4997 if (changedc) { /* need a new Const */
4998 ir_graph *irg = get_irn_irg(n);
4999 right = new_r_Const(irg, tv);
5003 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
5004 ir_node *op = get_Proj_pred(left);
5006 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
5007 ir_node *c = get_binop_right(op);
5010 ir_tarval *tv = get_Const_tarval(c);
5012 if (tarval_is_single_bit(tv)) {
5013 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5014 ir_node *v = get_binop_left(op);
5015 ir_node *blk = get_irn_n(op, -1);
5016 ir_graph *irg = get_irn_irg(op);
5017 ir_mode *mode = get_irn_mode(v);
5019 tv = tarval_sub(tv, get_mode_one(mode), NULL);
5020 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5022 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5029 dbg_info *dbgi = get_irn_dbg_info(n);
5030 ir_node *block = get_nodes_block(n);
5032 /* create a new compare */
5033 n = new_rd_Cmp(dbgi, block, left, right, relation);
5040 * Optimize CopyB(mem, x, x) into a Nop.
5042 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5044 ir_node *copyb = get_Proj_pred(proj);
5045 ir_node *a = get_CopyB_dst(copyb);
5046 ir_node *b = get_CopyB_src(copyb);
5049 switch (get_Proj_proj(proj)) {
5050 case pn_CopyB_X_regular:
5051 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5052 DBG_OPT_EXC_REM(proj);
5053 proj = new_r_Jmp(get_nodes_block(copyb));
5055 case pn_CopyB_X_except: {
5056 ir_graph *irg = get_irn_irg(proj);
5057 DBG_OPT_EXC_REM(proj);
5058 proj = new_r_Bad(irg, mode_X);
5069 * Optimize Bounds(idx, idx, upper) into idx.
5071 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5073 ir_node *oldn = proj;
5074 ir_node *bound = get_Proj_pred(proj);
5075 ir_node *idx = get_Bound_index(bound);
5076 ir_node *pred = skip_Proj(idx);
5079 if (idx == get_Bound_lower(bound))
5081 else if (is_Bound(pred)) {
5083 * idx was Bounds checked previously, it is still valid if
5084 * lower <= pred_lower && pred_upper <= upper.
5086 ir_node *lower = get_Bound_lower(bound);
5087 ir_node *upper = get_Bound_upper(bound);
5088 if (get_Bound_lower(pred) == lower &&
5089 get_Bound_upper(pred) == upper) {
5091 * One could expect that we simply return the previous
5092 * Bound here. However, this would be wrong, as we could
5093 * add an exception Proj to a new location then.
5094 * So, we must turn in into a tuple.
5100 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5101 switch (get_Proj_proj(proj)) {
5103 DBG_OPT_EXC_REM(proj);
5104 proj = get_Bound_mem(bound);
5106 case pn_Bound_X_except:
5107 DBG_OPT_EXC_REM(proj);
5108 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5112 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5114 case pn_Bound_X_regular:
5115 DBG_OPT_EXC_REM(proj);
5116 proj = new_r_Jmp(get_nodes_block(bound));
5126 * Does all optimizations on nodes that must be done on its Projs
5127 * because of creating new nodes.
5129 static ir_node *transform_node_Proj(ir_node *proj)
5131 ir_node *n = get_Proj_pred(proj);
5133 if (n->op->ops.transform_node_Proj)
5134 return n->op->ops.transform_node_Proj(proj);
5139 * Test whether a block is unreachable
5140 * Note: That this only returns true when
5141 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5142 * This is important, as you easily end up producing invalid constructs in the
5143 * unreachable code when optimizing away edges into the unreachable code.
5144 * So only set this flag when you iterate localopts to the fixpoint.
5145 * When you reach the fixpoint then all unreachable code is dead
5146 * (= can't be reached by firm edges) and you won't see the invalid constructs
5149 static bool is_block_unreachable(const ir_node *block)
5151 const ir_graph *irg = get_irn_irg(block);
5152 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5154 return get_Block_dom_depth(block) < 0;
5157 static ir_node *transform_node_Block(ir_node *block)
5159 ir_graph *irg = get_irn_irg(block);
5160 int arity = get_irn_arity(block);
5161 ir_node *bad = NULL;
5164 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5167 for (i = 0; i < arity; ++i) {
5168 ir_node *const pred = get_Block_cfgpred(block, i);
5169 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5172 bad = new_r_Bad(irg, mode_X);
5173 set_irn_n(block, i, bad);
5179 static ir_node *transform_node_Phi(ir_node *phi)
5181 int n = get_irn_arity(phi);
5182 ir_mode *mode = get_irn_mode(phi);
5183 ir_node *block = get_nodes_block(phi);
5184 ir_graph *irg = get_irn_irg(phi);
5185 ir_node *bad = NULL;
5188 /* Set phi-operands for bad-block inputs to bad */
5189 for (i = 0; i < n; ++i) {
5190 if (!is_Bad(get_Phi_pred(phi, i))) {
5191 ir_node *pred = get_Block_cfgpred(block, i);
5192 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5194 bad = new_r_Bad(irg, mode);
5195 set_irn_n(phi, i, bad);
5200 /* Move Pin nodes down through Phi nodes. */
5201 if (mode == mode_M) {
5202 n = get_irn_arity(phi);
5204 /* Beware of Phi0 */
5208 bool has_pin = false;
5210 NEW_ARR_A(ir_node *, in, n);
5212 for (i = 0; i < n; ++i) {
5213 ir_node *pred = get_irn_n(phi, i);
5216 in[i] = get_Pin_op(pred);
5218 } else if (is_Bad(pred)) {
5228 /* Move the Pin nodes "behind" the Phi. */
5229 block = get_irn_n(phi, -1);
5230 new_phi = new_r_Phi(block, n, in, mode_M);
5231 return new_r_Pin(block, new_phi);
5234 /* Move Confirms down through Phi nodes. */
5235 else if (mode_is_reference(mode)) {
5236 n = get_irn_arity(phi);
5238 /* Beware of Phi0 */
5240 ir_node *pred = get_irn_n(phi, 0);
5241 ir_node *bound, *new_phi, *block, **in;
5242 ir_relation relation;
5243 bool has_confirm = false;
5245 if (! is_Confirm(pred))
5248 bound = get_Confirm_bound(pred);
5249 relation = get_Confirm_relation(pred);
5251 NEW_ARR_A(ir_node *, in, n);
5252 in[0] = get_Confirm_value(pred);
5254 for (i = 1; i < n; ++i) {
5255 pred = get_irn_n(phi, i);
5257 if (is_Confirm(pred) &&
5258 get_Confirm_bound(pred) == bound &&
5259 get_Confirm_relation(pred) == relation) {
5260 in[i] = get_Confirm_value(pred);
5262 } else if (is_Bad(pred)) {
5272 /* move the Confirm nodes "behind" the Phi */
5273 block = get_irn_n(phi, -1);
5274 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5275 return new_r_Confirm(block, new_phi, bound, relation);
5282 static ir_node *transform_node(ir_node *n);
5285 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5287 * Should be moved to reassociation?
5289 static ir_node *transform_node_shift(ir_node *n)
5291 ir_node *left, *right;
5293 ir_mode *count_mode;
5294 ir_tarval *tv1, *tv2, *res;
5295 ir_node *in[2], *irn, *block;
5299 left = get_binop_left(n);
5301 /* different operations */
5302 if (get_irn_op(left) != get_irn_op(n))
5305 right = get_binop_right(n);
5306 tv1 = value_of(right);
5307 if (tv1 == tarval_bad)
5310 tv2 = value_of(get_binop_right(left));
5311 if (tv2 == tarval_bad)
5314 count_mode = get_tarval_mode(tv1);
5315 if (get_tarval_mode(tv2) != count_mode) {
5316 /* TODO: search bigger mode or something and convert... */
5320 mode = get_irn_mode(n);
5321 modulo_shf = get_mode_modulo_shift(mode);
5323 if (modulo_shf > 0) {
5324 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5326 /* I'm not so sure what happens in one complement... */
5327 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5328 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5329 * above will be invalid) */
5330 assert(modulo_shf<=0 || is_po2(modulo_shf));
5332 tv1 = tarval_and(tv1, modulo_mask);
5333 tv2 = tarval_and(tv2, modulo_mask);
5335 res = tarval_add(tv1, tv2);
5336 irg = get_irn_irg(n);
5338 /* beware: a simple replacement works only, if res < modulo shift */
5340 int bits = get_mode_size_bits(mode);
5341 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5342 res = tarval_mod(res, modulo);
5344 long bits = get_mode_size_bits(mode);
5345 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5347 /* shifting too much */
5348 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5350 ir_node *block = get_nodes_block(n);
5351 dbg_info *dbgi = get_irn_dbg_info(n);
5352 ir_mode *smode = get_irn_mode(right);
5353 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5354 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5357 return new_r_Const(irg, get_mode_null(mode));
5361 /* ok, we can replace it */
5362 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5363 block = get_nodes_block(n);
5365 in[0] = get_binop_left(left);
5366 in[1] = new_r_Const(irg, res);
5368 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5370 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5372 return transform_node(irn);
5377 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5379 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5380 * (also with x >>s c1 when c1>=c2)
5382 static ir_node *transform_node_shl_shr(ir_node *n)
5385 ir_node *right = get_binop_right(n);
5395 ir_tarval *tv_shift;
5398 ir_relation relation;
5401 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5403 if (!is_Const(right))
5406 left = get_binop_left(n);
5407 mode = get_irn_mode(n);
5408 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5409 ir_node *shr_right = get_binop_right(left);
5411 if (!is_Const(shr_right))
5414 x = get_binop_left(left);
5415 tv_shr = get_Const_tarval(shr_right);
5416 tv_shl = get_Const_tarval(right);
5418 if (is_Shrs(left)) {
5419 /* shrs variant only allowed if c1 >= c2 */
5420 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5423 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5426 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5428 tv_mask = tarval_shl(tv_mask, tv_shl);
5429 } else if (is_Shr(n) && is_Shl(left)) {
5430 ir_node *shl_right = get_Shl_right(left);
5432 if (!is_Const(shl_right))
5435 x = get_Shl_left(left);
5436 tv_shr = get_Const_tarval(right);
5437 tv_shl = get_Const_tarval(shl_right);
5439 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5440 tv_mask = tarval_shr(tv_mask, tv_shr);
5445 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5446 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5449 assert(tv_mask != tarval_bad);
5450 assert(get_tarval_mode(tv_mask) == mode);
5452 block = get_nodes_block(n);
5453 irg = get_irn_irg(block);
5454 dbgi = get_irn_dbg_info(n);
5456 relation = tarval_cmp(tv_shl, tv_shr);
5457 if (relation == ir_relation_less || relation == ir_relation_equal) {
5458 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5459 new_const = new_r_Const(irg, tv_shift);
5461 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5463 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5466 assert(relation == ir_relation_greater);
5467 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5468 new_const = new_r_Const(irg, tv_shift);
5469 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5472 new_const = new_r_Const(irg, tv_mask);
5473 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5478 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5480 ir_mode *mode = get_tarval_mode(tv);
5481 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5482 return tarval_mod(tv, modulo_tv);
5485 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5486 ir_node *left, ir_node *right, ir_mode *mode);
5489 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5490 * then we can use that to minimize the value of Add(x, const) or
5491 * Sub(Const, x). In particular this often avoids 1 instruction in some
5492 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5493 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5495 static ir_node *transform_node_shift_modulo(ir_node *n,
5496 new_shift_func new_shift)
5498 ir_mode *mode = get_irn_mode(n);
5499 int modulo = get_mode_modulo_shift(mode);
5500 ir_node *newop = NULL;
5501 ir_mode *mode_right;
5508 if (get_mode_arithmetic(mode) != irma_twos_complement)
5510 if (!is_po2(modulo))
5513 irg = get_irn_irg(n);
5514 block = get_nodes_block(n);
5515 right = get_binop_right(n);
5516 mode_right = get_irn_mode(right);
5517 if (is_Const(right)) {
5518 ir_tarval *tv = get_Const_tarval(right);
5519 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5524 newop = new_r_Const(irg, tv_mod);
5525 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5526 ir_node *add_right = get_binop_right(right);
5527 if (is_Const(add_right)) {
5528 ir_tarval *tv = get_Const_tarval(add_right);
5529 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5534 newconst = new_r_Const(irg, tv_mod);
5535 newop = new_r_Add(block, get_binop_left(right), newconst,
5538 } else if (is_Sub(right)) {
5539 ir_node *sub_left = get_Sub_left(right);
5540 if (is_Const(sub_left)) {
5541 ir_tarval *tv = get_Const_tarval(sub_left);
5542 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5547 newconst = new_r_Const(irg, tv_mod);
5548 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5555 if (newop != NULL) {
5556 dbg_info *dbgi = get_irn_dbg_info(n);
5557 ir_node *left = get_binop_left(n);
5558 return new_shift(dbgi, block, left, newop, mode);
5566 static ir_node *transform_node_Shr(ir_node *n)
5568 ir_node *c, *oldn = n;
5569 ir_node *left = get_Shr_left(n);
5570 ir_node *right = get_Shr_right(n);
5571 ir_mode *mode = get_irn_mode(n);
5573 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5574 n = transform_node_shift(n);
5577 n = transform_node_shift_modulo(n, new_rd_Shr);
5579 n = transform_node_shl_shr(n);
5581 n = transform_node_shift_bitop(n);
5589 static ir_node *transform_node_Shrs(ir_node *n)
5592 ir_node *a = get_Shrs_left(n);
5593 ir_node *b = get_Shrs_right(n);
5594 ir_mode *mode = get_irn_mode(n);
5598 if (is_oversize_shift(n)) {
5599 ir_node *block = get_nodes_block(n);
5600 dbg_info *dbgi = get_irn_dbg_info(n);
5601 ir_mode *cmode = get_irn_mode(b);
5602 long val = get_mode_size_bits(cmode)-1;
5603 ir_graph *irg = get_irn_irg(n);
5604 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5605 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5608 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5609 n = transform_node_shift(n);
5613 n = transform_node_shift_modulo(n, new_rd_Shrs);
5616 n = transform_node_shift_bitop(n);
5620 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5621 attr = vrp_get_info(a);
5623 unsigned bits = get_mode_size_bits(mode);
5624 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5625 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5626 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5627 dbg_info *dbgi = get_irn_dbg_info(n);
5628 ir_node *block = get_nodes_block(n);
5629 return new_rd_Shr(dbgi, block, a, b, mode);
5639 static ir_node *transform_node_Shl(ir_node *n)
5641 ir_node *c, *oldn = n;
5642 ir_node *a = get_Shl_left(n);
5643 ir_node *b = get_Shl_right(n);
5644 ir_mode *mode = get_irn_mode(n);
5646 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5647 n = transform_node_shift(n);
5650 n = transform_node_shift_modulo(n, new_rd_Shl);
5652 n = transform_node_shl_shr(n);
5654 n = transform_node_shift_bitop(n);
5662 static ir_node *transform_node_Rotl(ir_node *n)
5664 ir_node *c, *oldn = n;
5665 ir_node *a = get_Rotl_left(n);
5666 ir_node *b = get_Rotl_right(n);
5667 ir_mode *mode = get_irn_mode(n);
5669 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5670 n = transform_node_shift(n);
5673 n = transform_node_shift_bitop(n);
5681 static ir_node *transform_node_Conv(ir_node *n)
5683 ir_node *c, *oldn = n;
5684 ir_mode *mode = get_irn_mode(n);
5685 ir_node *a = get_Conv_op(n);
5687 if (mode != mode_b && is_const_Phi(a)) {
5688 /* Do NOT optimize mode_b Conv's, this leads to remaining
5689 * Phib nodes later, because the conv_b_lower operation
5690 * is instantly reverted, when it tries to insert a Convb.
5692 c = apply_conv_on_phi(a, mode);
5694 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5699 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5700 ir_graph *irg = get_irn_irg(n);
5701 return new_r_Unknown(irg, mode);
5704 if (mode_is_reference(mode) &&
5705 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5707 ir_node *l = get_Add_left(a);
5708 ir_node *r = get_Add_right(a);
5709 dbg_info *dbgi = get_irn_dbg_info(a);
5710 ir_node *block = get_nodes_block(n);
5712 ir_node *lop = get_Conv_op(l);
5713 if (get_irn_mode(lop) == mode) {
5714 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5715 n = new_rd_Add(dbgi, block, lop, r, mode);
5720 ir_node *rop = get_Conv_op(r);
5721 if (get_irn_mode(rop) == mode) {
5722 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5723 n = new_rd_Add(dbgi, block, l, rop, mode);
5733 * Remove dead blocks and nodes in dead blocks
5734 * in keep alive list. We do not generate a new End node.
5736 static ir_node *transform_node_End(ir_node *n)
5738 int i, j, n_keepalives = get_End_n_keepalives(n);
5741 NEW_ARR_A(ir_node *, in, n_keepalives);
5743 for (i = j = 0; i < n_keepalives; ++i) {
5744 ir_node *ka = get_End_keepalive(n, i);
5746 /* no need to keep Bad */
5749 /* do not keep unreachable code */
5750 block = is_Block(ka) ? ka : get_nodes_block(ka);
5751 if (is_block_unreachable(block))
5755 if (j != n_keepalives)
5756 set_End_keepalives(n, j, in);
5760 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5762 if (is_Minus(a) && get_Minus_op(a) == b)
5764 if (is_Minus(b) && get_Minus_op(b) == a)
5766 if (is_Sub(a) && is_Sub(b)) {
5767 ir_node *a_left = get_Sub_left(a);
5768 ir_node *a_right = get_Sub_right(a);
5769 ir_node *b_left = get_Sub_left(b);
5770 ir_node *b_right = get_Sub_right(b);
5772 if (a_left == b_right && a_right == b_left)
5779 static const ir_node *skip_upconv(const ir_node *node)
5781 while (is_Conv(node)) {
5782 ir_mode *mode = get_irn_mode(node);
5783 const ir_node *op = get_Conv_op(node);
5784 ir_mode *op_mode = get_irn_mode(op);
5785 if (!smaller_mode(op_mode, mode))
5792 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5793 const ir_node *mux_true)
5798 ir_relation relation;
5804 * Note further that these optimization work even for floating point
5805 * with NaN's because -NaN == NaN.
5806 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5809 mode = get_irn_mode(mux_true);
5810 if (mode_honor_signed_zeros(mode))
5813 /* must be <, <=, >=, > */
5814 relation = get_Cmp_relation(sel);
5815 if ((relation & ir_relation_less_greater) == 0)
5818 if (!ir_is_negated_value(mux_true, mux_false))
5821 mux_true = skip_upconv(mux_true);
5822 mux_false = skip_upconv(mux_false);
5824 /* must be x cmp 0 */
5825 cmp_right = get_Cmp_right(sel);
5826 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5829 cmp_left = get_Cmp_left(sel);
5830 if (cmp_left == mux_false) {
5831 if (relation & ir_relation_less) {
5834 assert(relation & ir_relation_greater);
5837 } else if (cmp_left == mux_true) {
5838 if (relation & ir_relation_less) {
5841 assert(relation & ir_relation_greater);
5849 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5852 ir_node *cmp_left = get_Cmp_left(sel);
5853 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5856 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5857 const ir_node *mux_true)
5859 /* this code should return true each time transform_node_Mux would
5860 * optimize the Mux completely away */
5862 ir_mode *mode = get_irn_mode(mux_false);
5863 if (get_mode_arithmetic(mode) == irma_twos_complement
5864 && ir_mux_is_abs(sel, mux_false, mux_true))
5867 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5868 const ir_node *cmp_r = get_Cmp_right(sel);
5869 const ir_node *cmp_l = get_Cmp_left(sel);
5870 const ir_node *f = mux_false;
5871 const ir_node *t = mux_true;
5873 if (is_Const(t) && is_Const_null(t)) {
5878 if (is_And(cmp_l) && f == cmp_r) {
5879 ir_node *and_r = get_And_right(cmp_l);
5882 if (and_r == t && is_single_bit(and_r))
5884 and_l = get_And_left(cmp_l);
5885 if (and_l == t && is_single_bit(and_l))
5894 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5896 static ir_node *transform_Mux_set(ir_node *n)
5898 ir_node *cond = get_Mux_sel(n);
5903 ir_relation relation;
5917 left = get_Cmp_left(cond);
5918 mode = get_irn_mode(left);
5919 if (!mode_is_int(mode) && !mode_is_reference(mode))
5921 dest_mode = get_irn_mode(n);
5922 right = get_Cmp_right(cond);
5923 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5924 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5925 && (!mode_is_signed(mode) || !is_Const(right) || !is_Const_null(right)))
5930 case ir_relation_less:
5931 /* a < b -> (a - b) >> 31 */
5935 case ir_relation_less_equal:
5936 /* a <= b -> ~(a - b) >> 31 */
5941 case ir_relation_greater:
5942 /* a > b -> (b - a) >> 31 */
5946 case ir_relation_greater_equal:
5947 /* a >= b -> ~(a - b) >> 31 */
5956 dbgi = get_irn_dbg_info(n);
5957 block = get_nodes_block(n);
5958 irg = get_irn_irg(block);
5959 bits = get_mode_size_bits(dest_mode);
5960 tv = new_tarval_from_long(bits-1, mode_Iu);
5961 shift_cnt = new_rd_Const(dbgi, irg, tv);
5963 if (mode != dest_mode) {
5964 a = new_rd_Conv(dbgi, block, a, dest_mode);
5965 b = new_rd_Conv(dbgi, block, b, dest_mode);
5968 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5970 res = new_rd_Not(dbgi, block, res, dest_mode);
5972 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5977 * Optimize a Mux into some simpler cases.
5979 static ir_node *transform_node_Mux(ir_node *n)
5982 ir_node *sel = get_Mux_sel(n);
5983 ir_mode *mode = get_irn_mode(n);
5984 ir_node *t = get_Mux_true(n);
5985 ir_node *f = get_Mux_false(n);
5986 ir_graph *irg = get_irn_irg(n);
5988 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5989 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5990 int abs = ir_mux_is_abs(sel, f, t);
5992 dbg_info *dbgi = get_irn_dbg_info(n);
5993 ir_node *block = get_nodes_block(n);
5994 ir_node *op = ir_get_abs_op(sel, f, t);
5995 int bits = get_mode_size_bits(mode);
5996 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5997 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5998 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
6001 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
6003 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
6009 /* first normalization step: try to move a constant to the false side,
6010 * 0 preferred on false side too */
6011 if (is_Cmp(sel) && is_Const(t) &&
6012 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
6013 dbg_info *seldbgi = get_irn_dbg_info(sel);
6014 ir_node *block = get_nodes_block(sel);
6015 ir_relation relation = get_Cmp_relation(sel);
6020 /* Mux(x, a, b) => Mux(not(x), b, a) */
6021 relation = get_negated_relation(relation);
6022 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6023 get_Cmp_right(sel), relation);
6024 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6027 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6028 n = transform_Mux_set(n);
6033 /* the following optimisations create new mode_b nodes, so only do them
6034 * before mode_b lowering */
6035 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6037 ir_node* block = get_nodes_block(n);
6039 ir_node* c1 = get_Mux_sel(t);
6040 ir_node* t1 = get_Mux_true(t);
6041 ir_node* f1 = get_Mux_false(t);
6043 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6044 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6045 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
6050 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
6051 } else if (f == t1) {
6052 /* Mux(cond0, Mux(cond1, x, y), x) */
6053 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6054 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6055 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
6060 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
6062 } else if (is_Mux(f)) {
6063 ir_node* block = get_nodes_block(n);
6065 ir_node* c1 = get_Mux_sel(f);
6066 ir_node* t1 = get_Mux_true(f);
6067 ir_node* f1 = get_Mux_false(f);
6069 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6070 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6071 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
6076 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
6077 } else if (t == f1) {
6078 /* Mux(cond0, x, Mux(cond1, y, x)) */
6079 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6080 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6081 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
6086 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
6090 /* note: after normalization, false can only happen on default */
6091 if (mode == mode_b) {
6092 dbg_info *dbg = get_irn_dbg_info(n);
6093 ir_node *block = get_nodes_block(n);
6096 ir_tarval *tv_t = get_Const_tarval(t);
6097 if (tv_t == tarval_b_true) {
6099 /* Muxb(sel, true, false) = sel */
6100 assert(get_Const_tarval(f) == tarval_b_false);
6101 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6104 /* Muxb(sel, true, x) = Or(sel, x) */
6105 n = new_rd_Or(dbg, block, sel, f, mode_b);
6106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6110 } else if (is_Const(f)) {
6111 ir_tarval *tv_f = get_Const_tarval(f);
6112 if (tv_f == tarval_b_true) {
6113 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6114 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6115 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6116 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6119 /* Muxb(sel, x, false) = And(sel, x) */
6120 assert(tv_f == tarval_b_false);
6121 n = new_rd_And(dbg, block, sel, t, mode_b);
6122 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6128 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
6129 * value to integer. */
6130 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
6131 ir_tarval *a = get_Const_tarval(t);
6132 ir_tarval *b = get_Const_tarval(f);
6134 if (tarval_is_one(a) && tarval_is_null(b)) {
6135 ir_node *block = get_nodes_block(n);
6136 ir_node *conv = new_r_Conv(block, sel, mode);
6138 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
6140 } else if (tarval_is_null(a) && tarval_is_one(b)) {
6141 ir_node *block = get_nodes_block(n);
6142 ir_node *not_ = new_r_Not(block, sel, mode_b);
6143 ir_node *conv = new_r_Conv(block, not_, mode);
6145 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
6151 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6152 ir_relation relation = get_Cmp_relation(sel);
6153 ir_node *cmp_r = get_Cmp_right(sel);
6154 ir_node *cmp_l = get_Cmp_left(sel);
6155 ir_node *block = get_nodes_block(n);
6157 if (is_And(cmp_l) && f == cmp_r) {
6158 ir_node *and_r = get_And_right(cmp_l);
6161 if (and_r == t && is_single_bit(and_r)) {
6162 if (relation == ir_relation_equal) {
6163 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6164 n = new_rd_Eor(get_irn_dbg_info(n),
6165 block, cmp_l, t, mode);
6166 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6168 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6170 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6174 and_l = get_And_left(cmp_l);
6175 if (and_l == t && is_single_bit(and_l)) {
6176 if (relation == ir_relation_equal) {
6177 /* ((1 << n) & a) == 0, (1 << n), 0) */
6178 n = new_rd_Eor(get_irn_dbg_info(n),
6179 block, cmp_l, t, mode);
6180 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6182 /* ((1 << n) & a) != 0, (1 << n), 0) */
6184 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6195 * optimize Sync nodes that have other syncs as input we simply add the inputs
6196 * of the other sync to our own inputs
6198 static ir_node *transform_node_Sync(ir_node *n)
6200 int arity = get_Sync_n_preds(n);
6203 for (i = 0; i < arity;) {
6204 ir_node *pred = get_Sync_pred(n, i);
6208 /* Remove Bad predecessors */
6215 /* Remove duplicate predecessors */
6216 for (j = 0; j < i; ++j) {
6217 if (get_Sync_pred(n, j) == pred) {
6226 if (!is_Sync(pred)) {
6234 pred_arity = get_Sync_n_preds(pred);
6235 for (j = 0; j < pred_arity; ++j) {
6236 ir_node *pred_pred = get_Sync_pred(pred, j);
6241 add_irn_n(n, pred_pred);
6245 if (get_Sync_pred(n, k) == pred_pred) break;
6251 ir_graph *irg = get_irn_irg(n);
6252 return new_r_Bad(irg, mode_M);
6255 return get_Sync_pred(n, 0);
6258 /* rehash the sync node */
6263 static ir_node *transform_node_Load(ir_node *n)
6265 /* if our memory predecessor is a load from the same address, then reuse the
6266 * previous result */
6267 ir_node *mem = get_Load_mem(n);
6272 /* don't touch volatile loads */
6273 if (get_Load_volatility(n) == volatility_is_volatile)
6275 mem_pred = get_Proj_pred(mem);
6276 if (is_Load(mem_pred)) {
6277 ir_node *pred_load = mem_pred;
6279 /* conservatively compare the 2 loads. TODO: This could be less strict
6280 * with fixup code in some situations (like smaller/bigger modes) */
6281 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6283 if (get_Load_mode(pred_load) != get_Load_mode(n))
6285 /* all combinations of aligned/unaligned pred/n should be fine so we do
6286 * not compare the unaligned attribute */
6288 ir_node *block = get_nodes_block(n);
6289 ir_node *jmp = new_r_Jmp(block);
6290 ir_graph *irg = get_irn_irg(n);
6291 ir_node *bad = new_r_Bad(irg, mode_X);
6292 ir_mode *mode = get_Load_mode(n);
6293 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6294 ir_node *in[] = { mem, res, jmp, bad };
6295 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6298 } else if (is_Store(mem_pred)) {
6299 ir_node *pred_store = mem_pred;
6300 ir_node *value = get_Store_value(pred_store);
6302 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6304 if (get_irn_mode(value) != get_Load_mode(n))
6306 /* all combinations of aligned/unaligned pred/n should be fine so we do
6307 * not compare the unaligned attribute */
6309 ir_node *block = get_nodes_block(n);
6310 ir_node *jmp = new_r_Jmp(block);
6311 ir_graph *irg = get_irn_irg(n);
6312 ir_node *bad = new_r_Bad(irg, mode_X);
6313 ir_node *res = value;
6314 ir_node *in[] = { mem, res, jmp, bad };
6315 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6324 * optimize a trampoline Call into a direct Call
6326 static ir_node *transform_node_Call(ir_node *call)
6328 ir_node *callee = get_Call_ptr(call);
6329 ir_node *adr, *mem, *res, *bl, **in;
6330 ir_type *ctp, *mtp, *tp;
6334 size_t i, n_res, n_param;
6337 if (! is_Proj(callee))
6339 callee = get_Proj_pred(callee);
6340 if (! is_Builtin(callee))
6342 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6345 mem = get_Call_mem(call);
6347 if (skip_Proj(mem) == callee) {
6348 /* memory is routed to the trampoline, skip */
6349 mem = get_Builtin_mem(callee);
6352 /* build a new call type */
6353 mtp = get_Call_type(call);
6354 tdb = get_type_dbg_info(mtp);
6356 n_res = get_method_n_ress(mtp);
6357 n_param = get_method_n_params(mtp);
6358 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6360 for (i = 0; i < n_res; ++i)
6361 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6363 NEW_ARR_A(ir_node *, in, n_param + 1);
6365 /* FIXME: we don't need a new pointer type in every step */
6366 irg = get_irn_irg(call);
6367 tp = get_irg_frame_type(irg);
6368 tp = new_type_pointer(tp);
6369 set_method_param_type(ctp, 0, tp);
6371 in[0] = get_Builtin_param(callee, 2);
6372 for (i = 0; i < n_param; ++i) {
6373 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6374 in[i + 1] = get_Call_param(call, i);
6376 var = get_method_variadicity(mtp);
6377 set_method_variadicity(ctp, var);
6378 /* When we resolve a trampoline, the function must be called by a this-call */
6379 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6380 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6382 adr = get_Builtin_param(callee, 1);
6384 db = get_irn_dbg_info(call);
6385 bl = get_nodes_block(call);
6387 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6388 if (get_irn_pinned(call) == op_pin_state_floats)
6389 set_irn_pinned(res, op_pin_state_floats);
6394 * Tries several [inplace] [optimizing] transformations and returns an
6395 * equivalent node. The difference to equivalent_node() is that these
6396 * transformations _do_ generate new nodes, and thus the old node must
6397 * not be freed even if the equivalent node isn't the old one.
6399 static ir_node *transform_node(ir_node *n)
6404 * Transform_node is the only "optimizing transformation" that might
6405 * return a node with a different opcode. We iterate HERE until fixpoint
6406 * to get the final result.
6410 if (n->op->ops.transform_node != NULL)
6411 n = n->op->ops.transform_node(n);
6412 } while (oldn != n);
6418 * Sets the default transform node operation for an ir_op_ops.
6420 * @param code the opcode for the default operation
6421 * @param ops the operations initialized
6426 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6430 ops->transform_node = transform_node_##a; \
6432 #define CASE_PROJ(a) \
6434 ops->transform_node_Proj = transform_node_Proj_##a; \
6436 #define CASE_PROJ_EX(a) \
6438 ops->transform_node = transform_node_##a; \
6439 ops->transform_node_Proj = transform_node_Proj_##a; \
6484 /* **************** Common Subexpression Elimination **************** */
6486 /** The size of the hash table used, should estimate the number of nodes
6488 #define N_IR_NODES 512
6490 /** Compares two exception attributes */
6491 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6493 const except_attr *ea = &a->attr.except;
6494 const except_attr *eb = &b->attr.except;
6495 return ea->pin_state != eb->pin_state;
6498 /** Compares the attributes of two Const nodes. */
6499 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6501 return get_Const_tarval(a) != get_Const_tarval(b);
6504 /** Compares the attributes of two Proj nodes. */
6505 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6507 return a->attr.proj.proj != b->attr.proj.proj;
6510 /** Compares the attributes of two Alloc nodes. */
6511 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6513 const alloc_attr *pa = &a->attr.alloc;
6514 const alloc_attr *pb = &b->attr.alloc;
6515 if (pa->where != pb->where || pa->type != pb->type)
6517 return node_cmp_exception(a, b);
6520 /** Compares the attributes of two Free nodes. */
6521 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6523 const free_attr *pa = &a->attr.free;
6524 const free_attr *pb = &b->attr.free;
6525 return (pa->where != pb->where) || (pa->type != pb->type);
6528 /** Compares the attributes of two SymConst nodes. */
6529 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6531 const symconst_attr *pa = &a->attr.symc;
6532 const symconst_attr *pb = &b->attr.symc;
6533 return (pa->kind != pb->kind)
6534 || (pa->sym.type_p != pb->sym.type_p);
6537 /** Compares the attributes of two Call nodes. */
6538 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6540 const call_attr *pa = &a->attr.call;
6541 const call_attr *pb = &b->attr.call;
6542 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6544 return node_cmp_exception(a, b);
6547 /** Compares the attributes of two Sel nodes. */
6548 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6550 const ir_entity *a_ent = get_Sel_entity(a);
6551 const ir_entity *b_ent = get_Sel_entity(b);
6552 return a_ent != b_ent;
6555 /** Compares the attributes of two Phi nodes. */
6556 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6558 /* we can only enter this function if both nodes have the same number of inputs,
6559 hence it is enough to check if one of them is a Phi0 */
6561 /* check the Phi0 pos attribute */
6562 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6567 /** Compares the attributes of two Conv nodes. */
6568 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6570 return get_Conv_strict(a) != get_Conv_strict(b);
6573 /** Compares the attributes of two Cast nodes. */
6574 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6576 return get_Cast_type(a) != get_Cast_type(b);
6579 /** Compares the attributes of two Load nodes. */
6580 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6582 if (get_Load_volatility(a) == volatility_is_volatile ||
6583 get_Load_volatility(b) == volatility_is_volatile)
6584 /* NEVER do CSE on volatile Loads */
6586 /* do not CSE Loads with different alignment. Be conservative. */
6587 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6589 if (get_Load_mode(a) != get_Load_mode(b))
6591 return node_cmp_exception(a, b);
6594 /** Compares the attributes of two Store nodes. */
6595 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6597 /* do not CSE Stores with different alignment. Be conservative. */
6598 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6600 /* NEVER do CSE on volatile Stores */
6601 if (get_Store_volatility(a) == volatility_is_volatile ||
6602 get_Store_volatility(b) == volatility_is_volatile)
6604 return node_cmp_exception(a, b);
6607 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6609 if (get_CopyB_type(a) != get_CopyB_type(b))
6612 return node_cmp_exception(a, b);
6615 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6617 return node_cmp_exception(a, b);
6620 /** Compares the attributes of two Div nodes. */
6621 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6623 const div_attr *ma = &a->attr.div;
6624 const div_attr *mb = &b->attr.div;
6625 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6627 return node_cmp_exception(a, b);
6630 /** Compares the attributes of two Mod nodes. */
6631 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6633 const mod_attr *ma = &a->attr.mod;
6634 const mod_attr *mb = &b->attr.mod;
6635 if (ma->resmode != mb->resmode)
6637 return node_cmp_exception(a, b);
6640 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6642 const cmp_attr *ma = &a->attr.cmp;
6643 const cmp_attr *mb = &b->attr.cmp;
6644 return ma->relation != mb->relation;
6647 /** Compares the attributes of two Confirm nodes. */
6648 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6650 const confirm_attr *ma = &a->attr.confirm;
6651 const confirm_attr *mb = &b->attr.confirm;
6652 return ma->relation != mb->relation;
6655 /** Compares the attributes of two Builtin nodes. */
6656 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6658 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6660 if (get_Builtin_type(a) != get_Builtin_type(b))
6662 return node_cmp_exception(a, b);
6665 /** Compares the attributes of two ASM nodes. */
6666 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6669 const ir_asm_constraint *ca;
6670 const ir_asm_constraint *cb;
6673 if (get_ASM_text(a) != get_ASM_text(b))
6676 /* Should we really check the constraints here? Should be better, but is strange. */
6677 n = get_ASM_n_input_constraints(a);
6678 if (n != get_ASM_n_input_constraints(b))
6681 ca = get_ASM_input_constraints(a);
6682 cb = get_ASM_input_constraints(b);
6683 for (i = 0; i < n; ++i) {
6684 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6685 || ca[i].mode != cb[i].mode)
6689 n = get_ASM_n_output_constraints(a);
6690 if (n != get_ASM_n_output_constraints(b))
6693 ca = get_ASM_output_constraints(a);
6694 cb = get_ASM_output_constraints(b);
6695 for (i = 0; i < n; ++i) {
6696 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6697 || ca[i].mode != cb[i].mode)
6701 n = get_ASM_n_clobbers(a);
6702 if (n != get_ASM_n_clobbers(b))
6705 cla = get_ASM_clobbers(a);
6706 clb = get_ASM_clobbers(b);
6707 for (i = 0; i < n; ++i) {
6708 if (cla[i] != clb[i])
6712 return node_cmp_exception(a, b);
6715 /** Compares the inexistent attributes of two Dummy nodes. */
6716 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6720 /* Dummy nodes never equal by definition */
6724 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6726 if (get_InstOf_type(a) != get_InstOf_type(b))
6728 return node_cmp_exception(a, b);
6732 * Set the default node attribute compare operation for an ir_op_ops.
6734 * @param code the opcode for the default operation
6735 * @param ops the operations initialized
6740 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6744 ops->node_cmp_attr = node_cmp_attr_##a; \
6780 * Compare function for two nodes in the value table. Gets two
6781 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6783 int identities_cmp(const void *elt, const void *key)
6785 ir_node *a = (ir_node *)elt;
6786 ir_node *b = (ir_node *)key;
6789 if (a == b) return 0;
6791 if ((get_irn_op(a) != get_irn_op(b)) ||
6792 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6794 /* compare if a's in and b's in are of equal length */
6795 irn_arity_a = get_irn_arity(a);
6796 if (irn_arity_a != get_irn_arity(b))
6799 /* blocks are never the same */
6803 if (get_irn_pinned(a) == op_pin_state_pinned) {
6804 /* for pinned nodes, the block inputs must be equal */
6805 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6808 ir_node *block_a = get_nodes_block(a);
6809 ir_node *block_b = get_nodes_block(b);
6810 if (! get_opt_global_cse()) {
6811 /* for block-local CSE both nodes must be in the same Block */
6812 if (block_a != block_b)
6815 /* The optimistic approach would be to do nothing here.
6816 * However doing GCSE optimistically produces a lot of partially dead code which appears
6817 * to be worse in practice than the missed opportunities.
6818 * So we use a very conservative variant here and only CSE if 1 value dominates the
6820 if (!block_dominates(block_a, block_b)
6821 && !block_dominates(block_b, block_a))
6826 /* compare a->in[0..ins] with b->in[0..ins] */
6827 for (i = 0; i < irn_arity_a; ++i) {
6828 ir_node *pred_a = get_irn_n(a, i);
6829 ir_node *pred_b = get_irn_n(b, i);
6830 if (pred_a != pred_b) {
6831 /* if both predecessors are CSE neutral they might be different */
6832 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6838 * here, we already now that the nodes are identical except their
6841 if (a->op->ops.node_cmp_attr)
6842 return a->op->ops.node_cmp_attr(a, b);
6848 * Calculate a hash value of a node.
6850 * @param node The IR-node
6852 unsigned ir_node_hash(const ir_node *node)
6854 return node->op->ops.hash(node);
6858 void new_identities(ir_graph *irg)
6860 if (irg->value_table != NULL)
6861 del_pset(irg->value_table);
6862 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6865 void del_identities(ir_graph *irg)
6867 if (irg->value_table != NULL)
6868 del_pset(irg->value_table);
6871 /* Normalize a node by putting constants (and operands with larger
6872 * node index) on the right (operator side). */
6873 void ir_normalize_node(ir_node *n)
6875 if (is_op_commutative(get_irn_op(n))) {
6876 ir_node *l = get_binop_left(n);
6877 ir_node *r = get_binop_right(n);
6879 /* For commutative operators perform a OP b == b OP a but keep
6880 * constants on the RIGHT side. This helps greatly in some
6881 * optimizations. Moreover we use the idx number to make the form
6883 if (!operands_are_normalized(l, r)) {
6884 set_binop_left(n, r);
6885 set_binop_right(n, l);
6892 * Return the canonical node computing the same value as n.
6893 * Looks up the node in a hash table, enters it in the table
6894 * if it isn't there yet.
6896 * @param n the node to look up
6898 * @return a node that computes the same value as n or n if no such
6899 * node could be found
6901 ir_node *identify_remember(ir_node *n)
6903 ir_graph *irg = get_irn_irg(n);
6904 pset *value_table = irg->value_table;
6907 if (value_table == NULL)
6910 ir_normalize_node(n);
6911 /* lookup or insert in hash table with given hash key. */
6912 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6915 /* n is reachable again */
6916 edges_node_revival(nn);
6923 * During construction we set the op_pin_state_pinned flag in the graph right
6924 * when the optimization is performed. The flag turning on procedure global
6925 * cse could be changed between two allocations. This way we are safe.
6927 * @param n The node to lookup
6929 static inline ir_node *identify_cons(ir_node *n)
6933 n = identify_remember(n);
6934 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6935 ir_graph *irg = get_irn_irg(n);
6936 set_irg_pinned(irg, op_pin_state_floats);
6941 /* Add a node to the identities value table. */
6942 void add_identities(ir_node *node)
6949 identify_remember(node);
6952 /* Visit each node in the value table of a graph. */
6953 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6956 ir_graph *rem = current_ir_graph;
6958 current_ir_graph = irg;
6959 foreach_pset(irg->value_table, ir_node*, node) {
6962 current_ir_graph = rem;
6966 * These optimizations deallocate nodes from the obstack.
6967 * It can only be called if it is guaranteed that no other nodes
6968 * reference this one, i.e., right after construction of a node.
6970 * @param n The node to optimize
6972 ir_node *optimize_node(ir_node *n)
6975 ir_graph *irg = get_irn_irg(n);
6976 unsigned iro = get_irn_opcode(n);
6979 /* Always optimize Phi nodes: part of the construction. */
6980 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6982 /* constant expression evaluation / constant folding */
6983 if (get_opt_constant_folding()) {
6984 /* neither constants nor Tuple values can be evaluated */
6985 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6986 /* try to evaluate */
6987 tv = computed_value(n);
6988 if (tv != tarval_bad) {
6993 * we MUST copy the node here temporarily, because it's still
6994 * needed for DBG_OPT_CSTEVAL
6996 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6997 oldn = (ir_node*)alloca(node_size);
6999 memcpy(oldn, n, node_size);
7000 CLONE_ARR_A(ir_node *, oldn->in, n->in);
7002 /* ARG, copy the in array, we need it for statistics */
7003 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
7005 /* note the inplace edges module */
7006 edges_node_deleted(n);
7008 /* evaluation was successful -- replace the node. */
7009 irg_kill_node(irg, n);
7010 nw = new_r_Const(irg, tv);
7012 DBG_OPT_CSTEVAL(oldn, nw);
7018 /* remove unnecessary nodes */
7019 if (get_opt_algebraic_simplification() ||
7020 (iro == iro_Phi) || /* always optimize these nodes. */
7022 (iro == iro_Proj) ||
7023 (iro == iro_Block) ) /* Flags tested local. */
7024 n = equivalent_node(n);
7026 /* Common Subexpression Elimination.
7028 * Checks whether n is already available.
7029 * The block input is used to distinguish different subexpressions. Right
7030 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
7031 * subexpressions within a block.
7034 n = identify_cons(n);
7037 edges_node_deleted(oldn);
7039 /* We found an existing, better node, so we can deallocate the old node. */
7040 irg_kill_node(irg, oldn);
7044 /* Some more constant expression evaluation that does not allow to
7046 iro = get_irn_opcode(n);
7047 if (get_opt_algebraic_simplification() ||
7048 (iro == iro_Cond) ||
7049 (iro == iro_Proj)) /* Flags tested local. */
7050 n = transform_node(n);
7052 /* Now we have a legal, useful node. Enter it in hash table for CSE */
7053 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
7055 n = identify_remember(o);
7065 * These optimizations never deallocate nodes (in place). This can cause dead
7066 * nodes lying on the obstack. Remove these by a dead node elimination,
7067 * i.e., a copying garbage collection.
7069 ir_node *optimize_in_place_2(ir_node *n)
7073 unsigned iro = get_irn_opcode(n);
7075 if (!get_opt_optimize() && !is_Phi(n)) return n;
7077 if (iro == iro_Deleted)
7080 /* constant expression evaluation / constant folding */
7081 if (get_opt_constant_folding()) {
7082 /* neither constants nor Tuple values can be evaluated */
7083 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
7084 /* try to evaluate */
7085 tv = computed_value(n);
7086 if (tv != tarval_bad) {
7087 /* evaluation was successful -- replace the node. */
7088 ir_graph *irg = get_irn_irg(n);
7090 n = new_r_Const(irg, tv);
7092 DBG_OPT_CSTEVAL(oldn, n);
7098 /* remove unnecessary nodes */
7099 if (get_opt_constant_folding() ||
7100 (iro == iro_Phi) || /* always optimize these nodes. */
7101 (iro == iro_Id) || /* ... */
7102 (iro == iro_Proj) || /* ... */
7103 (iro == iro_Block) ) /* Flags tested local. */
7104 n = equivalent_node(n);
7106 /** common subexpression elimination **/
7107 /* Checks whether n is already available. */
7108 /* The block input is used to distinguish different subexpressions. Right
7109 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
7110 subexpressions within a block. */
7111 if (get_opt_cse()) {
7113 n = identify_remember(o);
7118 /* Some more constant expression evaluation. */
7119 iro = get_irn_opcode(n);
7120 if (get_opt_constant_folding() ||
7121 (iro == iro_Cond) ||
7122 (iro == iro_Proj)) /* Flags tested local. */
7123 n = transform_node(n);
7125 /* Now we can verify the node, as it has no dead inputs any more. */
7128 /* Now we have a legal, useful node. Enter it in hash table for cse.
7129 Blocks should be unique anyways. (Except the successor of start:
7130 is cse with the start block!) */
7131 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
7133 n = identify_remember(o);
7142 * Wrapper for external use, set proper status bits after optimization.
7144 ir_node *optimize_in_place(ir_node *n)
7146 ir_graph *irg = get_irn_irg(n);
7147 /* Handle graph state */
7148 assert(get_irg_phase_state(irg) != phase_building);
7150 if (get_opt_global_cse())
7151 set_irg_pinned(irg, op_pin_state_floats);
7153 /* FIXME: Maybe we could also test whether optimizing the node can
7154 change the control graph. */
7155 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7156 return optimize_in_place_2(n);
7160 * Calculate a hash value of a Const node.
7162 static unsigned hash_Const(const ir_node *node)
7166 /* special value for const, as they only differ in their tarval. */
7167 h = HASH_PTR(node->attr.con.tarval);
7173 * Calculate a hash value of a SymConst node.
7175 static unsigned hash_SymConst(const ir_node *node)
7179 /* all others are pointers */
7180 h = HASH_PTR(node->attr.symc.sym.type_p);
7186 * Set the default hash operation in an ir_op_ops.
7188 * @param code the opcode for the default operation
7189 * @param ops the operations initialized
7194 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
7198 ops->hash = hash_##a; \
7201 /* hash function already set */
7202 if (ops->hash != NULL)
7209 /* use input/mode default hash if no function was given */
7210 ops->hash = firm_default_hash;
7218 * Sets the default operation for an ir_ops.
7220 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
7222 ops = firm_set_default_hash(code, ops);
7223 ops = firm_set_default_computed_value(code, ops);
7224 ops = firm_set_default_equivalent_node(code, ops);
7225 ops = firm_set_default_transform_node(code, ops);
7226 ops = firm_set_default_node_cmp_attr(code, ops);
7227 ops = firm_set_default_get_type_attr(code, ops);
7228 ops = firm_set_default_get_entity_attr(code, ops);