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;
2467 static ir_node *transform_node_Or_(ir_node *n)
2470 ir_node *a = get_binop_left(n);
2471 ir_node *b = get_binop_right(n);
2475 if (is_Not(a) && is_Not(b)) {
2476 /* ~a | ~b = ~(a&b) */
2477 ir_node *block = get_nodes_block(n);
2479 mode = get_irn_mode(n);
2482 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2483 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2484 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2488 /* we can combine the relations of two compares with the same operands */
2489 if (is_Cmp(a) && is_Cmp(b)) {
2490 ir_node *a_left = get_Cmp_left(a);
2491 ir_node *a_right = get_Cmp_right(a);
2492 ir_node *b_left = get_Cmp_left(b);
2493 ir_node *b_right = get_Cmp_right(b);
2494 if (a_left == b_left && b_left == b_right) {
2495 dbg_info *dbgi = get_irn_dbg_info(n);
2496 ir_node *block = get_nodes_block(n);
2497 ir_relation a_relation = get_Cmp_relation(a);
2498 ir_relation b_relation = get_Cmp_relation(b);
2499 ir_relation new_relation = a_relation | b_relation;
2500 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2502 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2503 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2504 && !mode_is_float(get_irn_mode(a_left))
2505 && !mode_is_float(get_irn_mode(b_left))) {
2506 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2507 ir_graph *irg = get_irn_irg(n);
2508 dbg_info *dbgi = get_irn_dbg_info(n);
2509 ir_node *block = get_nodes_block(n);
2510 ir_mode *a_mode = get_irn_mode(a_left);
2511 ir_mode *b_mode = get_irn_mode(b_left);
2512 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2513 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2514 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2515 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2516 ir_node *zero = create_zero_const(irg, b_mode);
2517 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2519 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2520 ir_graph *irg = get_irn_irg(n);
2521 dbg_info *dbgi = get_irn_dbg_info(n);
2522 ir_node *block = get_nodes_block(n);
2523 ir_mode *a_mode = get_irn_mode(a_left);
2524 ir_mode *b_mode = get_irn_mode(b_left);
2525 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2526 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2527 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2528 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2529 ir_node *zero = create_zero_const(irg, a_mode);
2530 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2535 mode = get_irn_mode(n);
2536 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2538 n = transform_node_Or_bf_store(n);
2541 n = transform_node_Or_Rotl(n);
2545 n = transform_bitwise_distributive(n, transform_node_Or_);
2548 n = transform_node_bitop_shift(n);
2555 static ir_node *transform_node_Or(ir_node *n)
2557 if (is_Or_Eor_Add(n)) {
2558 dbg_info *dbgi = get_irn_dbg_info(n);
2559 ir_node *block = get_nodes_block(n);
2560 ir_node *left = get_Or_left(n);
2561 ir_node *right = get_Or_right(n);
2562 ir_mode *mode = get_irn_mode(n);
2563 return new_rd_Add(dbgi, block, left, right, mode);
2565 return transform_node_Or_(n);
2571 static ir_node *transform_node_Eor_(ir_node *n)
2574 ir_node *a = get_binop_left(n);
2575 ir_node *b = get_binop_right(n);
2576 ir_mode *mode = get_irn_mode(n);
2579 /* we can combine the relations of two compares with the same operands */
2580 if (is_Cmp(a) && is_Cmp(b)) {
2581 ir_node *a_left = get_Cmp_left(a);
2582 ir_node *a_right = get_Cmp_left(a);
2583 ir_node *b_left = get_Cmp_left(b);
2584 ir_node *b_right = get_Cmp_right(b);
2585 if (a_left == b_left && b_left == b_right) {
2586 dbg_info *dbgi = get_irn_dbg_info(n);
2587 ir_node *block = get_nodes_block(n);
2588 ir_relation a_relation = get_Cmp_relation(a);
2589 ir_relation b_relation = get_Cmp_relation(b);
2590 ir_relation new_relation = a_relation ^ b_relation;
2591 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2595 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2597 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2598 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2599 dbg_info *dbg = get_irn_dbg_info(n);
2600 ir_node *block = get_nodes_block(n);
2601 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2602 ir_node *new_left = get_Not_op(a);
2603 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2604 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2606 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2607 dbg_info *dbg = get_irn_dbg_info(n);
2608 ir_node *block = get_nodes_block(n);
2609 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2610 ir_node *new_right = get_Not_op(b);
2611 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2612 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2616 /* x ^ 1...1 -> ~1 */
2617 if (is_Const(b) && is_Const_all_one(b)) {
2618 n = new_r_Not(get_nodes_block(n), a, mode);
2619 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2623 n = transform_bitwise_distributive(n, transform_node_Eor_);
2626 n = transform_node_bitop_shift(n);
2633 static ir_node *transform_node_Eor(ir_node *n)
2635 if (is_Or_Eor_Add(n)) {
2636 dbg_info *dbgi = get_irn_dbg_info(n);
2637 ir_node *block = get_nodes_block(n);
2638 ir_node *left = get_Eor_left(n);
2639 ir_node *right = get_Eor_right(n);
2640 ir_mode *mode = get_irn_mode(n);
2641 return new_rd_Add(dbgi, block, left, right, mode);
2643 return transform_node_Eor_(n);
2649 * Do the AddSub optimization, then Transform
2650 * Constant folding on Phi
2651 * Add(a,a) -> Mul(a, 2)
2652 * Add(Mul(a, x), a) -> Mul(a, x+1)
2653 * if the mode is integer or float.
2654 * Transform Add(a,-b) into Sub(a,b).
2655 * Reassociation might fold this further.
2657 static ir_node *transform_node_Add(ir_node *n)
2665 n = transform_node_AddSub(n);
2669 a = get_Add_left(n);
2670 b = get_Add_right(n);
2671 mode = get_irn_mode(n);
2673 if (mode_is_reference(mode)) {
2674 ir_mode *lmode = get_irn_mode(a);
2676 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2677 /* an Add(a, NULL) is a hidden Conv */
2678 dbg_info *dbg = get_irn_dbg_info(n);
2679 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2683 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2685 /* for FP the following optimizations are only allowed if
2686 * fp_strict_algebraic is disabled */
2687 if (mode_is_float(mode)) {
2688 ir_graph *irg = get_irn_irg(n);
2689 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2693 if (mode_is_num(mode)) {
2694 ir_graph *irg = get_irn_irg(n);
2695 /* the following code leads to endless recursion when Mul are replaced
2696 * by a simple instruction chain */
2697 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2698 && a == b && mode_is_int(mode)) {
2699 ir_node *block = get_nodes_block(n);
2702 get_irn_dbg_info(n),
2705 new_r_Const_long(irg, mode, 2),
2707 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2712 get_irn_dbg_info(n),
2717 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2722 get_irn_dbg_info(n),
2727 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2730 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2731 /* Here we rely on constants be on the RIGHT side */
2733 ir_node *op = get_Not_op(a);
2735 if (is_Const(b) && is_Const_one(b)) {
2737 ir_node *blk = get_nodes_block(n);
2738 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2739 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2746 if (is_Or_Eor_Add(n)) {
2747 n = transform_node_Or_(n);
2750 n = transform_node_Eor_(n);
2759 * returns -cnst or NULL if impossible
2761 static ir_node *const_negate(ir_node *cnst)
2763 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2764 dbg_info *dbgi = get_irn_dbg_info(cnst);
2765 ir_graph *irg = get_irn_irg(cnst);
2766 if (tv == tarval_bad) return NULL;
2767 return new_rd_Const(dbgi, irg, tv);
2771 * Do the AddSub optimization, then Transform
2772 * Constant folding on Phi
2773 * Sub(0,a) -> Minus(a)
2774 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2775 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2776 * Sub(Add(a, x), x) -> a
2777 * Sub(x, Add(x, a)) -> -a
2778 * Sub(x, Const) -> Add(x, -Const)
2780 static ir_node *transform_node_Sub(ir_node *n)
2786 n = transform_node_AddSub(n);
2788 a = get_Sub_left(n);
2789 b = get_Sub_right(n);
2791 mode = get_irn_mode(n);
2793 if (mode_is_int(mode)) {
2794 ir_mode *lmode = get_irn_mode(a);
2796 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2797 /* a Sub(a, NULL) is a hidden Conv */
2798 dbg_info *dbg = get_irn_dbg_info(n);
2799 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2800 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2804 if (mode == lmode &&
2805 get_mode_arithmetic(mode) == irma_twos_complement &&
2807 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2809 dbg_info *dbg = get_irn_dbg_info(n);
2810 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2817 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2819 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2820 if (mode_is_float(mode)) {
2821 ir_graph *irg = get_irn_irg(n);
2822 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2826 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2827 /* a - C -> a + (-C) */
2828 ir_node *cnst = const_negate(b);
2830 ir_node *block = get_nodes_block(n);
2831 dbg_info *dbgi = get_irn_dbg_info(n);
2833 n = new_rd_Add(dbgi, block, a, cnst, mode);
2834 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2839 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2840 dbg_info *dbg = get_irn_dbg_info(n);
2841 ir_node *block = get_nodes_block(n);
2842 ir_node *left = get_Minus_op(a);
2843 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2845 n = new_rd_Minus(dbg, block, add, mode);
2846 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2848 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2849 dbg_info *dbg = get_irn_dbg_info(n);
2850 ir_node *block = get_nodes_block(n);
2851 ir_node *right = get_Minus_op(b);
2853 n = new_rd_Add(dbg, block, a, right, mode);
2854 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2856 } else if (is_Sub(b)) {
2857 /* a - (b - c) -> a + (c - b)
2858 * -> (a - b) + c iff (b - c) is a pointer */
2859 dbg_info *s_dbg = get_irn_dbg_info(b);
2860 ir_node *s_left = get_Sub_left(b);
2861 ir_node *s_right = get_Sub_right(b);
2862 ir_mode *s_mode = get_irn_mode(b);
2863 if (mode_is_reference(s_mode)) {
2864 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2865 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2866 dbg_info *a_dbg = get_irn_dbg_info(n);
2869 s_right = new_r_Conv(lowest_block, s_right, mode);
2870 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2872 ir_node *s_block = get_nodes_block(b);
2873 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2874 dbg_info *a_dbg = get_irn_dbg_info(n);
2875 ir_node *a_block = get_nodes_block(n);
2877 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2879 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2882 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2883 ir_node *m_right = get_Mul_right(b);
2884 if (is_Const(m_right)) {
2885 ir_node *cnst2 = const_negate(m_right);
2886 if (cnst2 != NULL) {
2887 dbg_info *m_dbg = get_irn_dbg_info(b);
2888 ir_node *m_block = get_nodes_block(b);
2889 ir_node *m_left = get_Mul_left(b);
2890 ir_mode *m_mode = get_irn_mode(b);
2891 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2892 dbg_info *a_dbg = get_irn_dbg_info(n);
2893 ir_node *a_block = get_nodes_block(n);
2895 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2896 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2903 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2904 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2906 get_irn_dbg_info(n),
2910 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2913 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2914 ir_node *left = get_binop_left(a);
2915 ir_node *right = get_binop_right(a);
2917 /* FIXME: Does the Conv's work only for two complement or generally? */
2919 if (mode != get_irn_mode(right)) {
2920 /* This Sub is an effective Cast */
2921 right = new_r_Conv(get_nodes_block(n), right, mode);
2924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2926 } else if (right == b) {
2927 if (mode != get_irn_mode(left)) {
2928 /* This Sub is an effective Cast */
2929 left = new_r_Conv(get_nodes_block(n), left, mode);
2932 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2936 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2937 ir_node *left = get_binop_left(b);
2938 ir_node *right = get_binop_right(b);
2940 /* FIXME: Does the Conv's work only for two complement or generally? */
2942 ir_mode *r_mode = get_irn_mode(right);
2944 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2945 if (mode != r_mode) {
2946 /* This Sub is an effective Cast */
2947 n = new_r_Conv(get_nodes_block(n), n, mode);
2949 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2951 } else if (right == a) {
2952 ir_mode *l_mode = get_irn_mode(left);
2954 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2955 if (mode != l_mode) {
2956 /* This Sub is an effective Cast */
2957 n = new_r_Conv(get_nodes_block(n), n, mode);
2959 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2963 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2964 ir_mode *mode = get_irn_mode(a);
2966 if (mode == get_irn_mode(b)) {
2968 ir_node *op_a = get_Conv_op(a);
2969 ir_node *op_b = get_Conv_op(b);
2971 /* check if it's allowed to skip the conv */
2972 ma = get_irn_mode(op_a);
2973 mb = get_irn_mode(op_b);
2975 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2976 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2979 set_Sub_right(n, b);
2985 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2986 if (!is_reassoc_running() && is_Mul(a)) {
2987 ir_node *ma = get_Mul_left(a);
2988 ir_node *mb = get_Mul_right(a);
2991 ir_node *blk = get_nodes_block(n);
2992 ir_graph *irg = get_irn_irg(n);
2994 get_irn_dbg_info(n),
2998 get_irn_dbg_info(n),
3001 new_r_Const(irg, get_mode_one(mode)),
3004 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3006 } else if (mb == b) {
3007 ir_node *blk = get_nodes_block(n);
3008 ir_graph *irg = get_irn_irg(n);
3010 get_irn_dbg_info(n),
3014 get_irn_dbg_info(n),
3017 new_r_Const(irg, get_mode_one(mode)),
3020 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3024 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3025 ir_node *x = get_Sub_left(a);
3026 ir_node *y = get_Sub_right(a);
3027 ir_node *blk = get_nodes_block(n);
3028 ir_mode *m_b = get_irn_mode(b);
3029 ir_mode *m_y = get_irn_mode(y);
3033 /* Determine the right mode for the Add. */
3036 else if (mode_is_reference(m_b))
3038 else if (mode_is_reference(m_y))
3042 * Both modes are different but none is reference,
3043 * happens for instance in SubP(SubP(P, Iu), Is).
3044 * We have two possibilities here: Cast or ignore.
3045 * Currently we ignore this case.
3050 add = new_r_Add(blk, y, b, add_mode);
3052 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3053 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3057 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3058 /* c - ~X = X + (c+1) */
3059 if (is_Const(a) && is_Not(b)) {
3060 ir_tarval *tv = get_Const_tarval(a);
3062 tv = tarval_add(tv, get_mode_one(mode));
3063 if (tv != tarval_bad) {
3064 ir_node *blk = get_nodes_block(n);
3065 ir_graph *irg = get_irn_irg(n);
3066 ir_node *c = new_r_Const(irg, tv);
3067 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3068 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3072 /* x-(x&y) = x & ~y */
3074 ir_node *and_left = get_And_left(b);
3075 ir_node *and_right = get_And_right(b);
3076 if (and_right == a) {
3077 ir_node *tmp = and_left;
3078 and_left = and_right;
3081 if (and_left == a) {
3082 dbg_info *dbgi = get_irn_dbg_info(n);
3083 ir_node *block = get_nodes_block(n);
3084 ir_mode *mode = get_irn_mode(n);
3085 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3086 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3095 * Several transformation done on n*n=2n bits mul.
3096 * These transformations must be done here because new nodes may be produced.
3098 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3101 ir_node *a = get_Mul_left(n);
3102 ir_node *b = get_Mul_right(n);
3103 ir_tarval *ta = value_of(a);
3104 ir_tarval *tb = value_of(b);
3105 ir_mode *smode = get_irn_mode(a);
3107 if (ta == get_mode_one(smode)) {
3108 /* (L)1 * (L)b = (L)b */
3109 ir_node *blk = get_nodes_block(n);
3110 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3111 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3114 else if (ta == get_mode_minus_one(smode)) {
3115 /* (L)-1 * (L)b = (L)b */
3116 ir_node *blk = get_nodes_block(n);
3117 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3118 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3119 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3122 if (tb == get_mode_one(smode)) {
3123 /* (L)a * (L)1 = (L)a */
3124 ir_node *blk = get_irn_n(a, -1);
3125 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3126 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3129 else if (tb == get_mode_minus_one(smode)) {
3130 /* (L)a * (L)-1 = (L)-a */
3131 ir_node *blk = get_nodes_block(n);
3132 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3133 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3141 * Transform Mul(a,-1) into -a.
3142 * Do constant evaluation of Phi nodes.
3143 * Do architecture dependent optimizations on Mul nodes
3145 static ir_node *transform_node_Mul(ir_node *n)
3147 ir_node *c, *oldn = n;
3148 ir_mode *mode = get_irn_mode(n);
3149 ir_node *a = get_Mul_left(n);
3150 ir_node *b = get_Mul_right(n);
3152 if (is_Bad(a) || is_Bad(b))
3155 if (mode != get_irn_mode(a))
3156 return transform_node_Mul2n(n, mode);
3158 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3160 if (mode_is_signed(mode)) {
3163 if (value_of(a) == get_mode_minus_one(mode))
3165 else if (value_of(b) == get_mode_minus_one(mode))
3168 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3169 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3174 if (is_Const(b)) { /* (-a) * const -> a * -const */
3175 ir_node *cnst = const_negate(b);
3177 dbg_info *dbgi = get_irn_dbg_info(n);
3178 ir_node *block = get_nodes_block(n);
3179 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3180 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3183 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3184 dbg_info *dbgi = get_irn_dbg_info(n);
3185 ir_node *block = get_nodes_block(n);
3186 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3189 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3190 ir_node *sub_l = get_Sub_left(b);
3191 ir_node *sub_r = get_Sub_right(b);
3192 dbg_info *dbgi = get_irn_dbg_info(n);
3193 ir_node *block = get_nodes_block(n);
3194 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3195 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3199 } else if (is_Minus(b)) {
3200 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3201 ir_node *sub_l = get_Sub_left(a);
3202 ir_node *sub_r = get_Sub_right(a);
3203 dbg_info *dbgi = get_irn_dbg_info(n);
3204 ir_node *block = get_nodes_block(n);
3205 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3206 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3210 } else if (is_Shl(a)) {
3211 ir_node *const shl_l = get_Shl_left(a);
3212 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3213 /* (1 << x) * b -> b << x */
3214 dbg_info *const dbgi = get_irn_dbg_info(n);
3215 ir_node *const block = get_nodes_block(n);
3216 ir_node *const shl_r = get_Shl_right(a);
3217 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3218 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3221 } else if (is_Shl(b)) {
3222 ir_node *const shl_l = get_Shl_left(b);
3223 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3224 /* a * (1 << x) -> a << x */
3225 dbg_info *const dbgi = get_irn_dbg_info(n);
3226 ir_node *const block = get_nodes_block(n);
3227 ir_node *const shl_r = get_Shl_right(b);
3228 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3229 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3233 if (get_mode_arithmetic(mode) == irma_ieee754
3234 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3236 ir_tarval *tv = get_Const_tarval(a);
3237 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3238 && !tarval_is_negative(tv)) {
3239 /* 2.0 * b = b + b */
3240 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3241 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3245 else if (is_Const(b)) {
3246 ir_tarval *tv = get_Const_tarval(b);
3247 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3248 && !tarval_is_negative(tv)) {
3249 /* a * 2.0 = a + a */
3250 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3251 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3256 return arch_dep_replace_mul_with_shifts(n);
3260 * Transform a Div Node.
3262 static ir_node *transform_node_Div(ir_node *n)
3264 ir_mode *mode = get_Div_resmode(n);
3265 ir_node *a = get_Div_left(n);
3266 ir_node *b = get_Div_right(n);
3268 const ir_node *dummy;
3270 if (mode_is_int(mode)) {
3271 if (is_Const(b) && is_const_Phi(a)) {
3272 /* check for Div(Phi, Const) */
3273 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3275 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3278 } else if (is_Const(a) && is_const_Phi(b)) {
3279 /* check for Div(Const, Phi) */
3280 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3282 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3285 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3286 /* check for Div(Phi, Phi) */
3287 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3289 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3294 if (a == b && value_not_zero(a, &dummy)) {
3295 ir_graph *irg = get_irn_irg(n);
3296 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3297 value = new_r_Const(irg, get_mode_one(mode));
3298 DBG_OPT_CSTEVAL(n, value);
3301 if (mode_is_signed(mode) && is_Const(b)) {
3302 ir_tarval *tv = get_Const_tarval(b);
3304 if (tv == get_mode_minus_one(mode)) {
3306 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3307 DBG_OPT_CSTEVAL(n, value);
3311 /* Try architecture dependent optimization */
3312 value = arch_dep_replace_div_by_const(n);
3315 assert(mode_is_float(mode));
3317 /* Optimize x/c to x*(1/c) */
3318 if (get_mode_arithmetic(mode) == irma_ieee754) {
3319 ir_tarval *tv = value_of(b);
3321 if (tv != tarval_bad) {
3322 int rem = tarval_fp_ops_enabled();
3325 * Floating point constant folding might be disabled here to
3327 * However, as we check for exact result, doing it is safe.
3330 tarval_enable_fp_ops(1);
3331 tv = tarval_div(get_mode_one(mode), tv);
3332 tarval_enable_fp_ops(rem);
3334 /* Do the transformation if the result is either exact or we are
3335 not using strict rules. */
3336 if (tv != tarval_bad &&
3337 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3338 ir_node *block = get_nodes_block(n);
3339 ir_graph *irg = get_irn_irg(block);
3340 ir_node *c = new_r_Const(irg, tv);
3341 dbg_info *dbgi = get_irn_dbg_info(n);
3342 value = new_rd_Mul(dbgi, block, a, c, mode);
3355 /* Turn Div into a tuple (mem, jmp, bad, value) */
3356 mem = get_Div_mem(n);
3357 blk = get_nodes_block(n);
3358 irg = get_irn_irg(blk);
3360 /* skip a potential Pin */
3361 mem = skip_Pin(mem);
3362 turn_into_tuple(n, pn_Div_max+1);
3363 set_Tuple_pred(n, pn_Div_M, mem);
3364 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3365 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3366 set_Tuple_pred(n, pn_Div_res, value);
3372 * Transform a Mod node.
3374 static ir_node *transform_node_Mod(ir_node *n)
3376 ir_mode *mode = get_Mod_resmode(n);
3377 ir_node *a = get_Mod_left(n);
3378 ir_node *b = get_Mod_right(n);
3383 if (is_Const(b) && is_const_Phi(a)) {
3384 /* check for Div(Phi, Const) */
3385 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3387 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3391 else if (is_Const(a) && is_const_Phi(b)) {
3392 /* check for Div(Const, Phi) */
3393 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3395 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3399 else if (is_const_Phi(a) && is_const_Phi(b)) {
3400 /* check for Div(Phi, Phi) */
3401 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3403 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3410 irg = get_irn_irg(n);
3411 if (tv != tarval_bad) {
3412 value = new_r_Const(irg, tv);
3414 DBG_OPT_CSTEVAL(n, value);
3417 ir_node *a = get_Mod_left(n);
3418 ir_node *b = get_Mod_right(n);
3419 const ir_node *dummy;
3421 if (a == b && value_not_zero(a, &dummy)) {
3422 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3423 value = new_r_Const(irg, get_mode_null(mode));
3424 DBG_OPT_CSTEVAL(n, value);
3427 if (mode_is_signed(mode) && is_Const(b)) {
3428 ir_tarval *tv = get_Const_tarval(b);
3430 if (tv == get_mode_minus_one(mode)) {
3432 value = new_r_Const(irg, get_mode_null(mode));
3433 DBG_OPT_CSTEVAL(n, value);
3437 /* Try architecture dependent optimization */
3438 value = arch_dep_replace_mod_by_const(n);
3447 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3448 mem = get_Mod_mem(n);
3449 blk = get_nodes_block(n);
3450 irg = get_irn_irg(blk);
3452 /* skip a potential Pin */
3453 mem = skip_Pin(mem);
3454 turn_into_tuple(n, pn_Mod_max+1);
3455 set_Tuple_pred(n, pn_Mod_M, mem);
3456 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3457 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3458 set_Tuple_pred(n, pn_Mod_res, value);
3464 * Transform a Cond node.
3466 * Replace the Cond by a Jmp if it branches on a constant
3469 static ir_node *transform_node_Cond(ir_node *n)
3471 ir_node *a = get_Cond_selector(n);
3472 ir_graph *irg = get_irn_irg(n);
3476 /* we need block info which is not available in floating irgs */
3477 if (get_irg_pinned(irg) == op_pin_state_floats)
3481 if (ta == tarval_bad && is_Cmp(a)) {
3482 /* try again with a direct call to compute_cmp, as we don't care
3483 * about the MODEB_LOWERED flag here */
3484 ta = compute_cmp(a);
3487 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3488 /* It's a boolean Cond, branching on a boolean constant.
3489 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3490 ir_node *blk = get_nodes_block(n);
3491 jmp = new_r_Jmp(blk);
3492 turn_into_tuple(n, pn_Cond_max+1);
3493 if (ta == tarval_b_true) {
3494 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3495 set_Tuple_pred(n, pn_Cond_true, jmp);
3497 set_Tuple_pred(n, pn_Cond_false, jmp);
3498 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3500 /* We might generate an endless loop, so keep it alive. */
3501 add_End_keepalive(get_irg_end(irg), blk);
3502 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3507 static ir_node *transform_node_Switch(ir_node *n)
3509 ir_node *op = get_Switch_selector(n);
3510 ir_tarval *val = value_of(op);
3511 if (val != tarval_bad) {
3512 dbg_info *dbgi = get_irn_dbg_info(n);
3513 ir_graph *irg = get_irn_irg(n);
3514 unsigned n_outs = get_Switch_n_outs(n);
3515 ir_node *block = get_nodes_block(n);
3516 ir_node *bad = new_r_Bad(irg, mode_X);
3517 ir_node **in = XMALLOCN(ir_node*, n_outs);
3518 const ir_switch_table *table = get_Switch_table(n);
3519 size_t n_entries = ir_switch_table_get_n_entries(table);
3523 for (i = 0; i < n_entries; ++i) {
3524 const ir_switch_table_entry *entry
3525 = ir_switch_table_get_entry_const(table, i);
3526 ir_tarval *min = entry->min;
3527 ir_tarval *max = entry->max;
3530 if ((min == max && min == val)
3531 || (tarval_cmp(val, min) != ir_relation_less
3532 && tarval_cmp(val, max) != ir_relation_greater)) {
3537 for (o = 0; o < n_outs; ++o) {
3538 if (o == (unsigned)jmp_pn) {
3539 in[o] = new_rd_Jmp(dbgi, block);
3544 return new_r_Tuple(block, (int)n_outs, in);
3550 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3552 * - and, or, xor instead of &
3553 * - Shl, Shr, Shrs, rotl instead of >>
3554 * (with a special case for Or/Xor + Shrs)
3556 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3558 static ir_node *transform_node_shift_bitop(ir_node *n)
3560 ir_graph *irg = get_irn_irg(n);
3561 ir_node *right = get_binop_right(n);
3562 ir_mode *mode = get_irn_mode(n);
3564 ir_node *bitop_left;
3565 ir_node *bitop_right;
3574 ir_tarval *tv_shift;
3576 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3579 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3581 if (!is_Const(right))
3584 left = get_binop_left(n);
3585 op_left = get_irn_op(left);
3586 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3589 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3590 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3591 /* TODO: test if sign bit is affectes */
3595 bitop_right = get_binop_right(left);
3596 if (!is_Const(bitop_right))
3599 bitop_left = get_binop_left(left);
3601 block = get_nodes_block(n);
3602 dbgi = get_irn_dbg_info(n);
3603 tv1 = get_Const_tarval(bitop_right);
3604 tv2 = get_Const_tarval(right);
3606 assert(get_tarval_mode(tv1) == mode);
3609 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3610 tv_shift = tarval_shl(tv1, tv2);
3611 } else if (is_Shr(n)) {
3612 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3613 tv_shift = tarval_shr(tv1, tv2);
3614 } else if (is_Shrs(n)) {
3615 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3616 tv_shift = tarval_shrs(tv1, tv2);
3619 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3620 tv_shift = tarval_rotl(tv1, tv2);
3623 assert(get_tarval_mode(tv_shift) == mode);
3624 irg = get_irn_irg(n);
3625 new_const = new_r_Const(irg, tv_shift);
3627 if (op_left == op_And) {
3628 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3629 } else if (op_left == op_Or) {
3630 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3632 assert(op_left == op_Eor);
3633 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3642 static ir_node *transform_node_And(ir_node *n)
3644 ir_node *c, *oldn = n;
3645 ir_node *a = get_And_left(n);
3646 ir_node *b = get_And_right(n);
3649 if (is_Cmp(a) && is_Cmp(b)) {
3650 ir_node *a_left = get_Cmp_left(a);
3651 ir_node *a_right = get_Cmp_right(a);
3652 ir_node *b_left = get_Cmp_left(b);
3653 ir_node *b_right = get_Cmp_right(b);
3654 ir_relation a_relation = get_Cmp_relation(a);
3655 ir_relation b_relation = get_Cmp_relation(b);
3656 /* we can combine the relations of two compares with the same
3658 if (a_left == b_left && b_left == b_right) {
3659 dbg_info *dbgi = get_irn_dbg_info(n);
3660 ir_node *block = get_nodes_block(n);
3661 ir_relation new_relation = a_relation & b_relation;
3662 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3664 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3665 if (a_relation == b_relation && a_relation == ir_relation_equal
3666 && !mode_is_float(get_irn_mode(a_left))
3667 && !mode_is_float(get_irn_mode(b_left))) {
3668 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3669 dbg_info *dbgi = get_irn_dbg_info(n);
3670 ir_node *block = get_nodes_block(n);
3671 ir_mode *a_mode = get_irn_mode(a_left);
3672 ir_mode *b_mode = get_irn_mode(b_left);
3673 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3674 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3675 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3676 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3677 ir_graph *irg = get_irn_irg(n);
3678 ir_node *zero = create_zero_const(irg, b_mode);
3679 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3681 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3682 dbg_info *dbgi = get_irn_dbg_info(n);
3683 ir_node *block = get_nodes_block(n);
3684 ir_mode *a_mode = get_irn_mode(a_left);
3685 ir_mode *b_mode = get_irn_mode(b_left);
3686 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3687 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3688 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3689 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3690 ir_graph *irg = get_irn_irg(n);
3691 ir_node *zero = create_zero_const(irg, a_mode);
3692 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3697 mode = get_irn_mode(n);
3698 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3700 if (is_Or(a) || is_Or_Eor_Add(a)) {
3701 ir_node *or_left = get_binop_left(a);
3702 ir_node *or_right = get_binop_right(a);
3703 if (complement_values(or_left, b)) {
3704 /* (a|b) & ~a => b & ~a */
3705 dbg_info *dbgi = get_irn_dbg_info(n);
3706 ir_node *block = get_nodes_block(n);
3707 return new_rd_And(dbgi, block, or_right, b, mode);
3708 } else if (complement_values(or_right, b)) {
3709 /* (a|b) & ~b => a & ~b */
3710 dbg_info *dbgi = get_irn_dbg_info(n);
3711 ir_node *block = get_nodes_block(n);
3712 return new_rd_And(dbgi, block, or_left, b, mode);
3713 } else if (is_Not(b)) {
3714 ir_node *op = get_Not_op(b);
3716 ir_node *ba = get_And_left(op);
3717 ir_node *bb = get_And_right(op);
3719 /* it's enough to test the following cases due to normalization! */
3720 if (or_left == ba && or_right == bb) {
3721 /* (a|b) & ~(a&b) = a^b */
3722 ir_node *block = get_nodes_block(n);
3724 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3725 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3731 if (is_Or(b) || is_Or_Eor_Add(b)) {
3732 ir_node *or_left = get_binop_left(b);
3733 ir_node *or_right = get_binop_right(b);
3734 if (complement_values(or_left, a)) {
3735 /* (a|b) & ~a => b & ~a */
3736 dbg_info *dbgi = get_irn_dbg_info(n);
3737 ir_node *block = get_nodes_block(n);
3738 return new_rd_And(dbgi, block, or_right, a, mode);
3739 } else if (complement_values(or_right, a)) {
3740 /* (a|b) & ~b => a & ~b */
3741 dbg_info *dbgi = get_irn_dbg_info(n);
3742 ir_node *block = get_nodes_block(n);
3743 return new_rd_And(dbgi, block, or_left, a, mode);
3744 } else if (is_Not(a)) {
3745 ir_node *op = get_Not_op(a);
3747 ir_node *aa = get_And_left(op);
3748 ir_node *ab = get_And_right(op);
3750 /* it's enough to test the following cases due to normalization! */
3751 if (or_left == aa && or_right == ab) {
3752 /* (a|b) & ~(a&b) = a^b */
3753 ir_node *block = get_nodes_block(n);
3755 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3756 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3762 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3763 ir_node *al = get_binop_left(a);
3764 ir_node *ar = get_binop_right(a);
3767 /* (b ^ a) & b -> ~a & b */
3768 dbg_info *dbg = get_irn_dbg_info(n);
3769 ir_node *block = get_nodes_block(n);
3771 ar = new_rd_Not(dbg, block, ar, mode);
3772 n = new_rd_And(dbg, block, ar, b, mode);
3773 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3777 /* (a ^ b) & b -> ~a & b */
3778 dbg_info *dbg = get_irn_dbg_info(n);
3779 ir_node *block = get_nodes_block(n);
3781 al = new_rd_Not(dbg, block, al, mode);
3782 n = new_rd_And(dbg, block, al, b, mode);
3783 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3787 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3788 ir_node *bl = get_binop_left(b);
3789 ir_node *br = get_binop_right(b);
3792 /* a & (a ^ b) -> a & ~b */
3793 dbg_info *dbg = get_irn_dbg_info(n);
3794 ir_node *block = get_nodes_block(n);
3796 br = new_rd_Not(dbg, block, br, mode);
3797 n = new_rd_And(dbg, block, br, a, mode);
3798 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3802 /* a & (b ^ a) -> a & ~b */
3803 dbg_info *dbg = get_irn_dbg_info(n);
3804 ir_node *block = get_nodes_block(n);
3806 bl = new_rd_Not(dbg, block, bl, mode);
3807 n = new_rd_And(dbg, block, bl, a, mode);
3808 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3812 if (is_Not(a) && is_Not(b)) {
3813 /* ~a & ~b = ~(a|b) */
3814 ir_node *block = get_nodes_block(n);
3815 ir_mode *mode = get_irn_mode(n);
3819 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3820 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3821 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3826 vrp_attr *b_vrp = vrp_get_info(b);
3827 ir_tarval *a_val = get_Const_tarval(a);
3828 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3834 vrp_attr *a_vrp = vrp_get_info(a);
3835 ir_tarval *b_val = get_Const_tarval(b);
3836 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3841 n = transform_bitwise_distributive(n, transform_node_And);
3843 n = transform_node_bitop_shift(n);
3851 static ir_node *transform_node_Not(ir_node *n)
3853 ir_node *c, *oldn = n;
3854 ir_node *a = get_Not_op(n);
3855 ir_mode *mode = get_irn_mode(n);
3857 HANDLE_UNOP_PHI(tarval_not,a,c);
3859 /* check for a boolean Not */
3861 dbg_info *dbgi = get_irn_dbg_info(a);
3862 ir_node *block = get_nodes_block(a);
3863 ir_relation relation = get_Cmp_relation(a);
3864 relation = get_negated_relation(relation);
3865 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3870 /* normalize ~(a ^ b) => a ^ ~b */
3871 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3872 dbg_info *dbg = get_irn_dbg_info(n);
3873 ir_node *block = get_nodes_block(n);
3874 ir_node *eor_right = get_binop_right(a);
3875 ir_node *eor_left = get_binop_left(a);
3876 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3877 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3881 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3882 if (is_Minus(a)) { /* ~-x -> x + -1 */
3883 dbg_info *dbg = get_irn_dbg_info(n);
3884 ir_graph *irg = get_irn_irg(n);
3885 ir_node *block = get_nodes_block(n);
3886 ir_node *add_l = get_Minus_op(a);
3887 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3888 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3889 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3890 ir_node *add_r = get_binop_right(a);
3891 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3892 /* ~(x + -1) = -x */
3893 ir_node *op = get_binop_left(a);
3894 ir_node *blk = get_nodes_block(n);
3895 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3896 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3904 * Transform a Minus.
3908 * -(a >>u (size-1)) = a >>s (size-1)
3909 * -(a >>s (size-1)) = a >>u (size-1)
3910 * -(a * const) -> a * -const
3912 static ir_node *transform_node_Minus(ir_node *n)
3914 ir_node *c, *oldn = n;
3915 ir_node *a = get_Minus_op(n);
3918 HANDLE_UNOP_PHI(tarval_neg,a,c);
3920 mode = get_irn_mode(a);
3921 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3922 /* the following rules are only to twos-complement */
3925 ir_node *op = get_Not_op(a);
3926 ir_tarval *tv = get_mode_one(mode);
3927 ir_node *blk = get_nodes_block(n);
3928 ir_graph *irg = get_irn_irg(blk);
3929 ir_node *c = new_r_Const(irg, tv);
3930 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3931 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3935 ir_node *c = get_Shr_right(a);
3938 ir_tarval *tv = get_Const_tarval(c);
3940 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3941 /* -(a >>u (size-1)) = a >>s (size-1) */
3942 ir_node *v = get_Shr_left(a);
3944 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3945 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3951 ir_node *c = get_Shrs_right(a);
3954 ir_tarval *tv = get_Const_tarval(c);
3956 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3957 /* -(a >>s (size-1)) = a >>u (size-1) */
3958 ir_node *v = get_Shrs_left(a);
3960 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3961 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3968 /* - (a-b) = b - a */
3969 ir_node *la = get_Sub_left(a);
3970 ir_node *ra = get_Sub_right(a);
3971 ir_node *blk = get_nodes_block(n);
3973 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3974 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3978 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3979 ir_node *mul_l = get_Mul_left(a);
3980 ir_node *mul_r = get_Mul_right(a);
3981 ir_tarval *tv = value_of(mul_r);
3982 if (tv != tarval_bad) {
3983 tv = tarval_neg(tv);
3984 if (tv != tarval_bad) {
3985 ir_graph *irg = get_irn_irg(n);
3986 ir_node *cnst = new_r_Const(irg, tv);
3987 dbg_info *dbg = get_irn_dbg_info(a);
3988 ir_node *block = get_nodes_block(a);
3989 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3990 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4000 * Transform a Proj(Load) with a non-null address.
4002 static ir_node *transform_node_Proj_Load(ir_node *proj)
4004 if (get_opt_ldst_only_null_ptr_exceptions()) {
4005 if (get_irn_mode(proj) == mode_X) {
4006 ir_node *load = get_Proj_pred(proj);
4008 /* get the Load address */
4009 const ir_node *addr = get_Load_ptr(load);
4010 const ir_node *confirm;
4012 if (value_not_null(addr, &confirm)) {
4013 if (confirm == NULL) {
4014 /* this node may float if it did not depend on a Confirm */
4015 set_irn_pinned(load, op_pin_state_floats);
4017 if (get_Proj_proj(proj) == pn_Load_X_except) {
4018 ir_graph *irg = get_irn_irg(proj);
4019 DBG_OPT_EXC_REM(proj);
4020 return new_r_Bad(irg, mode_X);
4022 ir_node *blk = get_nodes_block(load);
4023 return new_r_Jmp(blk);
4032 * Transform a Proj(Store) with a non-null address.
4034 static ir_node *transform_node_Proj_Store(ir_node *proj)
4036 if (get_opt_ldst_only_null_ptr_exceptions()) {
4037 if (get_irn_mode(proj) == mode_X) {
4038 ir_node *store = get_Proj_pred(proj);
4040 /* get the load/store address */
4041 const ir_node *addr = get_Store_ptr(store);
4042 const ir_node *confirm;
4044 if (value_not_null(addr, &confirm)) {
4045 if (confirm == NULL) {
4046 /* this node may float if it did not depend on a Confirm */
4047 set_irn_pinned(store, op_pin_state_floats);
4049 if (get_Proj_proj(proj) == pn_Store_X_except) {
4050 ir_graph *irg = get_irn_irg(proj);
4051 DBG_OPT_EXC_REM(proj);
4052 return new_r_Bad(irg, mode_X);
4054 ir_node *blk = get_nodes_block(store);
4055 return new_r_Jmp(blk);
4064 * Transform a Proj(Div) with a non-zero value.
4065 * Removes the exceptions and routes the memory to the NoMem node.
4067 static ir_node *transform_node_Proj_Div(ir_node *proj)
4069 ir_node *div = get_Proj_pred(proj);
4070 ir_node *b = get_Div_right(div);
4071 ir_node *res, *new_mem;
4072 const ir_node *confirm;
4075 if (value_not_zero(b, &confirm)) {
4076 /* div(x, y) && y != 0 */
4077 if (confirm == NULL) {
4078 /* we are sure we have a Const != 0 */
4079 new_mem = get_Div_mem(div);
4080 new_mem = skip_Pin(new_mem);
4081 set_Div_mem(div, new_mem);
4082 set_irn_pinned(div, op_pin_state_floats);
4085 proj_nr = get_Proj_proj(proj);
4087 case pn_Div_X_regular:
4088 return new_r_Jmp(get_nodes_block(div));
4090 case pn_Div_X_except: {
4091 ir_graph *irg = get_irn_irg(proj);
4092 /* we found an exception handler, remove it */
4093 DBG_OPT_EXC_REM(proj);
4094 return new_r_Bad(irg, mode_X);
4098 ir_graph *irg = get_irn_irg(proj);
4099 res = get_Div_mem(div);
4100 new_mem = get_irg_no_mem(irg);
4103 /* This node can only float up to the Confirm block */
4104 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4106 set_irn_pinned(div, op_pin_state_floats);
4107 /* this is a Div without exception, we can remove the memory edge */
4108 set_Div_mem(div, new_mem);
4117 * Transform a Proj(Mod) with a non-zero value.
4118 * Removes the exceptions and routes the memory to the NoMem node.
4120 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4122 ir_node *mod = get_Proj_pred(proj);
4123 ir_node *b = get_Mod_right(mod);
4124 ir_node *res, *new_mem;
4125 const ir_node *confirm;
4128 if (value_not_zero(b, &confirm)) {
4129 /* mod(x, y) && y != 0 */
4130 proj_nr = get_Proj_proj(proj);
4132 if (confirm == NULL) {
4133 /* we are sure we have a Const != 0 */
4134 new_mem = get_Mod_mem(mod);
4135 new_mem = skip_Pin(new_mem);
4136 set_Mod_mem(mod, new_mem);
4137 set_irn_pinned(mod, op_pin_state_floats);
4142 case pn_Mod_X_regular:
4143 return new_r_Jmp(get_irn_n(mod, -1));
4145 case pn_Mod_X_except: {
4146 ir_graph *irg = get_irn_irg(proj);
4147 /* we found an exception handler, remove it */
4148 DBG_OPT_EXC_REM(proj);
4149 return new_r_Bad(irg, mode_X);
4153 ir_graph *irg = get_irn_irg(proj);
4154 res = get_Mod_mem(mod);
4155 new_mem = get_irg_no_mem(irg);
4158 /* This node can only float up to the Confirm block */
4159 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4161 /* this is a Mod without exception, we can remove the memory edge */
4162 set_Mod_mem(mod, new_mem);
4166 if (get_Mod_left(mod) == b) {
4167 /* a % a = 0 if a != 0 */
4168 ir_graph *irg = get_irn_irg(proj);
4169 ir_mode *mode = get_irn_mode(proj);
4170 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4172 DBG_OPT_CSTEVAL(mod, res);
4181 * return true if the operation returns a value with exactly 1 bit set
4183 static bool is_single_bit(const ir_node *node)
4185 /* a first implementation, could be extended with vrp and others... */
4187 ir_node *shl_l = get_Shl_left(node);
4188 ir_mode *mode = get_irn_mode(node);
4189 int modulo = get_mode_modulo_shift(mode);
4190 /* this works if we shift a 1 and we have modulo shift */
4191 if (is_Const(shl_l) && is_Const_one(shl_l)
4192 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4195 } else if (is_Const(node)) {
4196 ir_tarval *tv = get_Const_tarval(node);
4197 return tarval_is_single_bit(tv);
4203 * Normalizes and optimizes Cmp nodes.
4205 static ir_node *transform_node_Cmp(ir_node *n)
4207 ir_node *left = get_Cmp_left(n);
4208 ir_node *right = get_Cmp_right(n);
4209 ir_mode *mode = get_irn_mode(left);
4210 ir_tarval *tv = NULL;
4211 bool changed = false;
4212 bool changedc = false;
4213 ir_relation relation = get_Cmp_relation(n);
4214 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4216 /* mask out impossible relations */
4217 ir_relation new_relation = relation & possible;
4218 if (new_relation != relation) {
4219 relation = new_relation;
4223 /* Remove unnecessary conversions */
4224 if (is_Conv(left) && is_Conv(right)) {
4225 ir_node *op_left = get_Conv_op(left);
4226 ir_node *op_right = get_Conv_op(right);
4227 ir_mode *mode_left = get_irn_mode(op_left);
4228 ir_mode *mode_right = get_irn_mode(op_right);
4230 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4231 && mode_left != mode_b && mode_right != mode_b) {
4232 ir_node *block = get_nodes_block(n);
4234 if (mode_left == mode_right) {
4238 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4239 } else if (smaller_mode(mode_left, mode_right)) {
4240 left = new_r_Conv(block, op_left, mode_right);
4243 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4244 } else if (smaller_mode(mode_right, mode_left)) {
4246 right = new_r_Conv(block, op_right, mode_left);
4248 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4250 mode = get_irn_mode(left);
4253 if (is_Conv(left) && is_Const(right)) {
4254 ir_node *op_left = get_Conv_op(left);
4255 ir_mode *mode_left = get_irn_mode(op_left);
4256 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4257 ir_tarval *tv = get_Const_tarval(right);
4258 tarval_int_overflow_mode_t last_mode
4259 = tarval_get_integer_overflow_mode();
4261 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4262 new_tv = tarval_convert_to(tv, mode_left);
4263 tarval_set_integer_overflow_mode(last_mode);
4264 if (new_tv != tarval_bad) {
4265 ir_graph *irg = get_irn_irg(n);
4267 right = new_r_Const(irg, new_tv);
4268 mode = get_irn_mode(left);
4270 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4276 * Optimize -a CMP -b into b CMP a.
4277 * This works only for modes where unary Minus cannot Overflow.
4278 * Note that two-complement integers can Overflow so it will NOT work.
4280 if (!mode_overflow_on_unary_Minus(mode) &&
4281 is_Minus(left) && is_Minus(right)) {
4282 left = get_Minus_op(left);
4283 right = get_Minus_op(right);
4284 relation = get_inversed_relation(relation);
4286 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4289 /* remove operation on both sides if possible */
4290 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4292 * The following operations are NOT safe for floating point operations, for instance
4293 * 1.0 + inf == 2.0 + inf, =/=> x == y
4295 if (mode_is_int(mode)) {
4296 unsigned lop = get_irn_opcode(left);
4298 if (lop == get_irn_opcode(right)) {
4299 ir_node *ll, *lr, *rl, *rr;
4301 /* same operation on both sides, try to remove */
4305 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4306 left = get_unop_op(left);
4307 right = get_unop_op(right);
4309 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4312 ll = get_Add_left(left);
4313 lr = get_Add_right(left);
4314 rl = get_Add_left(right);
4315 rr = get_Add_right(right);
4318 /* X + a CMP X + b ==> a CMP b */
4322 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4323 } else if (ll == rr) {
4324 /* X + a CMP b + X ==> a CMP b */
4328 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4329 } else if (lr == rl) {
4330 /* a + X CMP X + b ==> a CMP b */
4334 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4335 } else if (lr == rr) {
4336 /* a + X CMP b + X ==> a CMP b */
4340 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4344 ll = get_Sub_left(left);
4345 lr = get_Sub_right(left);
4346 rl = get_Sub_left(right);
4347 rr = get_Sub_right(right);
4350 /* X - a CMP X - b ==> a CMP b */
4354 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4355 } else if (lr == rr) {
4356 /* a - X CMP b - X ==> a CMP b */
4360 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4364 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4365 /* a ROTL X CMP b ROTL X ==> a CMP b */
4366 left = get_Rotl_left(left);
4367 right = get_Rotl_left(right);
4369 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4377 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4378 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4379 ir_node *ll = get_binop_left(left);
4380 ir_node *lr = get_binop_right(left);
4382 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4388 ir_graph *irg = get_irn_irg(n);
4390 right = create_zero_const(irg, mode);
4392 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4395 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4396 ir_node *rl = get_binop_left(right);
4397 ir_node *rr = get_binop_right(right);
4399 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4405 ir_graph *irg = get_irn_irg(n);
4407 right = create_zero_const(irg, mode);
4409 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4413 if (is_And(left) && is_Const(right)) {
4414 ir_node *ll = get_binop_left(left);
4415 ir_node *lr = get_binop_right(left);
4416 if (is_Shr(ll) && is_Const(lr)) {
4417 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4418 ir_node *block = get_nodes_block(n);
4419 ir_mode *mode = get_irn_mode(left);
4421 ir_node *llr = get_Shr_right(ll);
4422 if (is_Const(llr)) {
4423 dbg_info *dbg = get_irn_dbg_info(left);
4424 ir_graph *irg = get_irn_irg(left);
4426 ir_tarval *c1 = get_Const_tarval(llr);
4427 ir_tarval *c2 = get_Const_tarval(lr);
4428 ir_tarval *c3 = get_Const_tarval(right);
4429 ir_tarval *mask = tarval_shl(c2, c1);
4430 ir_tarval *value = tarval_shl(c3, c1);
4432 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4433 right = new_r_Const(irg, value);
4438 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4440 if (is_Const(right) && is_Const_null(right) &&
4441 (is_Eor(left) || is_Or_Eor_Add(left))) {
4442 right = get_Eor_right(left);
4443 left = get_Eor_left(left);
4449 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4450 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4451 if (mode_is_int(mode) && is_And(left)
4452 && (relation == ir_relation_equal
4453 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4454 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4455 ir_node *and0 = get_And_left(left);
4456 ir_node *and1 = get_And_right(left);
4457 if (and1 == right) {
4458 ir_node *tmp = and0;
4462 if (and0 == right && is_single_bit(and0)) {
4463 ir_graph *irg = get_irn_irg(n);
4465 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4466 right = create_zero_const(irg, mode);
4471 /* replace mode_b compares with ands/ors */
4472 if (mode == mode_b) {
4473 ir_node *block = get_nodes_block(n);
4477 case ir_relation_less_equal:
4478 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4480 case ir_relation_less:
4481 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4483 case ir_relation_greater_equal:
4484 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4486 case ir_relation_greater:
4487 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4489 case ir_relation_less_greater:
4490 bres = new_r_Eor(block, left, right, mode_b);
4492 case ir_relation_equal:
4493 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4496 #ifdef DEBUG_libfirm
4497 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4502 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4508 * First step: normalize the compare op
4509 * by placing the constant on the right side
4510 * or moving the lower address node to the left.
4512 if (!operands_are_normalized(left, right)) {
4517 relation = get_inversed_relation(relation);
4522 * Second step: Try to reduce the magnitude
4523 * of a constant. This may help to generate better code
4524 * later and may help to normalize more compares.
4525 * Of course this is only possible for integer values.
4527 tv = value_of(right);
4528 if (tv != tarval_bad) {
4529 ir_mode *mode = get_irn_mode(right);
4531 /* TODO extend to arbitrary constants */
4532 if (is_Conv(left) && tarval_is_null(tv)) {
4533 ir_node *op = get_Conv_op(left);
4534 ir_mode *op_mode = get_irn_mode(op);
4537 * UpConv(x) REL 0 ==> x REL 0
4538 * Don't do this for float values as it's unclear whether it is a
4539 * win. (on the other side it makes detection/creation of fabs hard)
4541 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4542 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4543 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4544 !mode_is_float(mode)) {
4545 tv = get_mode_null(op_mode);
4549 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4553 if (tv != tarval_bad) {
4554 /* the following optimization is possible on modes without Overflow
4555 * on Unary Minus or on == and !=:
4556 * -a CMP c ==> a swap(CMP) -c
4558 * Beware: for two-complement Overflow may occur, so only == and != can
4559 * be optimized, see this:
4560 * -MININT < 0 =/=> MININT > 0 !!!
4562 if (is_Minus(left) &&
4563 (!mode_overflow_on_unary_Minus(mode) ||
4564 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4565 tv = tarval_neg(tv);
4567 if (tv != tarval_bad) {
4568 left = get_Minus_op(left);
4569 relation = get_inversed_relation(relation);
4571 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4573 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4574 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4575 tv = tarval_not(tv);
4577 if (tv != tarval_bad) {
4578 left = get_Not_op(left);
4580 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4584 /* for integer modes, we have more */
4585 if (mode_is_int(mode) && !is_Const(left)) {
4586 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4587 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4588 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4589 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4591 if (tv != tarval_bad) {
4592 relation ^= ir_relation_equal;
4594 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4597 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4598 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4599 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4600 tv = tarval_add(tv, get_mode_one(mode));
4602 if (tv != tarval_bad) {
4603 relation ^= ir_relation_equal;
4605 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4609 /* the following reassociations work only for == and != */
4610 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4611 if (tv != tarval_bad) {
4612 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4614 ir_node *c1 = get_Sub_right(left);
4615 ir_tarval *tv2 = value_of(c1);
4617 if (tv2 != tarval_bad) {
4618 tv2 = tarval_add(tv, value_of(c1));
4620 if (tv2 != tarval_bad) {
4621 left = get_Sub_left(left);
4624 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4628 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4629 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4630 ir_node *a_l = get_binop_left(left);
4631 ir_node *a_r = get_binop_right(left);
4635 if (is_Const(a_l)) {
4637 tv2 = value_of(a_l);
4640 tv2 = value_of(a_r);
4643 if (tv2 != tarval_bad) {
4644 tv2 = tarval_sub(tv, tv2, NULL);
4646 if (tv2 != tarval_bad) {
4650 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4654 /* -a == c ==> a == -c, -a != c ==> a != -c */
4655 else if (is_Minus(left)) {
4656 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4658 if (tv2 != tarval_bad) {
4659 left = get_Minus_op(left);
4662 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4669 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4670 switch (get_irn_opcode(left)) {
4674 c1 = get_And_right(left);
4677 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4678 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4680 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4682 /* TODO: move to constant evaluation */
4683 ir_graph *irg = get_irn_irg(n);
4684 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4685 c1 = new_r_Const(irg, tv);
4686 DBG_OPT_CSTEVAL(n, c1);
4690 if (tarval_is_single_bit(tv)) {
4692 * optimization for AND:
4694 * And(x, C) == C ==> And(x, C) != 0
4695 * And(x, C) != C ==> And(X, C) == 0
4697 * if C is a single Bit constant.
4700 /* check for Constant's match. We have check hare the tarvals,
4701 because our const might be changed */
4702 if (get_Const_tarval(c1) == tv) {
4703 /* fine: do the transformation */
4704 tv = get_mode_null(get_tarval_mode(tv));
4705 relation ^= ir_relation_less_equal_greater;
4707 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4713 c1 = get_Or_right(left);
4714 if (is_Const(c1) && tarval_is_null(tv)) {
4716 * Or(x, C) == 0 && C != 0 ==> FALSE
4717 * Or(x, C) != 0 && C != 0 ==> TRUE
4719 if (! tarval_is_null(get_Const_tarval(c1))) {
4720 /* TODO: move to constant evaluation */
4721 ir_graph *irg = get_irn_irg(n);
4722 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4723 c1 = new_r_Const(irg, tv);
4724 DBG_OPT_CSTEVAL(n, c1);
4731 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4733 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4736 c1 = get_Shl_right(left);
4738 ir_graph *irg = get_irn_irg(c1);
4739 ir_tarval *tv1 = get_Const_tarval(c1);
4740 ir_mode *mode = get_irn_mode(left);
4741 ir_tarval *minus1 = get_mode_all_one(mode);
4742 ir_tarval *amask = tarval_shr(minus1, tv1);
4743 ir_tarval *cmask = tarval_shl(minus1, tv1);
4746 if (tarval_and(tv, cmask) != tv) {
4747 /* condition not met */
4748 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4749 c1 = new_r_Const(irg, tv);
4750 DBG_OPT_CSTEVAL(n, c1);
4753 sl = get_Shl_left(left);
4754 blk = get_nodes_block(n);
4755 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4756 tv = tarval_shr(tv, tv1);
4758 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4763 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4765 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4768 c1 = get_Shr_right(left);
4770 ir_graph *irg = get_irn_irg(c1);
4771 ir_tarval *tv1 = get_Const_tarval(c1);
4772 ir_mode *mode = get_irn_mode(left);
4773 ir_tarval *minus1 = get_mode_all_one(mode);
4774 ir_tarval *amask = tarval_shl(minus1, tv1);
4775 ir_tarval *cmask = tarval_shr(minus1, tv1);
4778 if (tarval_and(tv, cmask) != tv) {
4779 /* condition not met */
4780 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4781 c1 = new_r_Const(irg, tv);
4782 DBG_OPT_CSTEVAL(n, c1);
4785 sl = get_Shr_left(left);
4786 blk = get_nodes_block(n);
4787 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4788 tv = tarval_shl(tv, tv1);
4790 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4795 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4797 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4800 c1 = get_Shrs_right(left);
4802 ir_graph *irg = get_irn_irg(c1);
4803 ir_tarval *tv1 = get_Const_tarval(c1);
4804 ir_mode *mode = get_irn_mode(left);
4805 ir_tarval *minus1 = get_mode_all_one(mode);
4806 ir_tarval *amask = tarval_shl(minus1, tv1);
4807 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4810 cond = tarval_sub(cond, tv1, NULL);
4811 cond = tarval_shrs(tv, cond);
4813 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4814 /* condition not met */
4815 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4816 c1 = new_r_Const(irg, tv);
4817 DBG_OPT_CSTEVAL(n, c1);
4820 sl = get_Shrs_left(left);
4821 blk = get_nodes_block(n);
4822 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4823 tv = tarval_shl(tv, tv1);
4825 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4833 if (changedc) { /* need a new Const */
4834 ir_graph *irg = get_irn_irg(n);
4835 right = new_r_Const(irg, tv);
4839 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4840 ir_node *op = get_Proj_pred(left);
4842 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4843 ir_node *c = get_binop_right(op);
4846 ir_tarval *tv = get_Const_tarval(c);
4848 if (tarval_is_single_bit(tv)) {
4849 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4850 ir_node *v = get_binop_left(op);
4851 ir_node *blk = get_irn_n(op, -1);
4852 ir_graph *irg = get_irn_irg(op);
4853 ir_mode *mode = get_irn_mode(v);
4855 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4856 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4858 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4865 dbg_info *dbgi = get_irn_dbg_info(n);
4866 ir_node *block = get_nodes_block(n);
4868 /* create a new compare */
4869 n = new_rd_Cmp(dbgi, block, left, right, relation);
4876 * Optimize CopyB(mem, x, x) into a Nop.
4878 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4880 ir_node *copyb = get_Proj_pred(proj);
4881 ir_node *a = get_CopyB_dst(copyb);
4882 ir_node *b = get_CopyB_src(copyb);
4885 switch (get_Proj_proj(proj)) {
4886 case pn_CopyB_X_regular:
4887 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4888 DBG_OPT_EXC_REM(proj);
4889 proj = new_r_Jmp(get_nodes_block(copyb));
4891 case pn_CopyB_X_except: {
4892 ir_graph *irg = get_irn_irg(proj);
4893 DBG_OPT_EXC_REM(proj);
4894 proj = new_r_Bad(irg, mode_X);
4905 * Optimize Bounds(idx, idx, upper) into idx.
4907 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4909 ir_node *oldn = proj;
4910 ir_node *bound = get_Proj_pred(proj);
4911 ir_node *idx = get_Bound_index(bound);
4912 ir_node *pred = skip_Proj(idx);
4915 if (idx == get_Bound_lower(bound))
4917 else if (is_Bound(pred)) {
4919 * idx was Bounds checked previously, it is still valid if
4920 * lower <= pred_lower && pred_upper <= upper.
4922 ir_node *lower = get_Bound_lower(bound);
4923 ir_node *upper = get_Bound_upper(bound);
4924 if (get_Bound_lower(pred) == lower &&
4925 get_Bound_upper(pred) == upper) {
4927 * One could expect that we simply return the previous
4928 * Bound here. However, this would be wrong, as we could
4929 * add an exception Proj to a new location then.
4930 * So, we must turn in into a tuple.
4936 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4937 switch (get_Proj_proj(proj)) {
4939 DBG_OPT_EXC_REM(proj);
4940 proj = get_Bound_mem(bound);
4942 case pn_Bound_X_except:
4943 DBG_OPT_EXC_REM(proj);
4944 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4948 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4950 case pn_Bound_X_regular:
4951 DBG_OPT_EXC_REM(proj);
4952 proj = new_r_Jmp(get_nodes_block(bound));
4962 * Does all optimizations on nodes that must be done on its Projs
4963 * because of creating new nodes.
4965 static ir_node *transform_node_Proj(ir_node *proj)
4967 ir_node *n = get_Proj_pred(proj);
4969 if (n->op->ops.transform_node_Proj)
4970 return n->op->ops.transform_node_Proj(proj);
4975 * Test whether a block is unreachable
4976 * Note: That this only returns true when
4977 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
4978 * This is important, as you easily end up producing invalid constructs in the
4979 * unreachable code when optimizing away edges into the unreachable code.
4980 * So only set this flag when you iterate localopts to the fixpoint.
4981 * When you reach the fixpoint then all unreachable code is dead
4982 * (= can't be reached by firm edges) and you won't see the invalid constructs
4985 static bool is_block_unreachable(const ir_node *block)
4987 const ir_graph *irg = get_irn_irg(block);
4988 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4990 return get_Block_dom_depth(block) < 0;
4993 static ir_node *transform_node_Block(ir_node *block)
4995 ir_graph *irg = get_irn_irg(block);
4996 int arity = get_irn_arity(block);
4997 ir_node *bad = NULL;
5000 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5003 for (i = 0; i < arity; ++i) {
5004 ir_node *const pred = get_Block_cfgpred(block, i);
5005 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5008 bad = new_r_Bad(irg, mode_X);
5009 set_irn_n(block, i, bad);
5015 static ir_node *transform_node_Phi(ir_node *phi)
5017 int n = get_irn_arity(phi);
5018 ir_mode *mode = get_irn_mode(phi);
5019 ir_node *block = get_nodes_block(phi);
5020 ir_graph *irg = get_irn_irg(phi);
5021 ir_node *bad = NULL;
5024 /* Set phi-operands for bad-block inputs to bad */
5025 for (i = 0; i < n; ++i) {
5026 if (!is_Bad(get_Phi_pred(phi, i))) {
5027 ir_node *pred = get_Block_cfgpred(block, i);
5028 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5030 bad = new_r_Bad(irg, mode);
5031 set_irn_n(phi, i, bad);
5036 /* Move Pin nodes down through Phi nodes. */
5037 if (mode == mode_M) {
5038 n = get_irn_arity(phi);
5040 /* Beware of Phi0 */
5044 bool has_pin = false;
5046 NEW_ARR_A(ir_node *, in, n);
5048 for (i = 0; i < n; ++i) {
5049 ir_node *pred = get_irn_n(phi, i);
5052 in[i] = get_Pin_op(pred);
5054 } else if (is_Bad(pred)) {
5064 /* Move the Pin nodes "behind" the Phi. */
5065 block = get_irn_n(phi, -1);
5066 new_phi = new_r_Phi(block, n, in, mode_M);
5067 return new_r_Pin(block, new_phi);
5070 /* Move Confirms down through Phi nodes. */
5071 else if (mode_is_reference(mode)) {
5072 n = get_irn_arity(phi);
5074 /* Beware of Phi0 */
5076 ir_node *pred = get_irn_n(phi, 0);
5077 ir_node *bound, *new_phi, *block, **in;
5078 ir_relation relation;
5079 bool has_confirm = false;
5081 if (! is_Confirm(pred))
5084 bound = get_Confirm_bound(pred);
5085 relation = get_Confirm_relation(pred);
5087 NEW_ARR_A(ir_node *, in, n);
5088 in[0] = get_Confirm_value(pred);
5090 for (i = 1; i < n; ++i) {
5091 pred = get_irn_n(phi, i);
5093 if (is_Confirm(pred) &&
5094 get_Confirm_bound(pred) == bound &&
5095 get_Confirm_relation(pred) == relation) {
5096 in[i] = get_Confirm_value(pred);
5098 } else if (is_Bad(pred)) {
5108 /* move the Confirm nodes "behind" the Phi */
5109 block = get_irn_n(phi, -1);
5110 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5111 return new_r_Confirm(block, new_phi, bound, relation);
5118 static ir_node *transform_node(ir_node *n);
5121 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5123 * Should be moved to reassociation?
5125 static ir_node *transform_node_shift(ir_node *n)
5127 ir_node *left, *right;
5129 ir_mode *count_mode;
5130 ir_tarval *tv1, *tv2, *res;
5131 ir_node *in[2], *irn, *block;
5135 left = get_binop_left(n);
5137 /* different operations */
5138 if (get_irn_op(left) != get_irn_op(n))
5141 right = get_binop_right(n);
5142 tv1 = value_of(right);
5143 if (tv1 == tarval_bad)
5146 tv2 = value_of(get_binop_right(left));
5147 if (tv2 == tarval_bad)
5150 count_mode = get_tarval_mode(tv1);
5151 if (get_tarval_mode(tv2) != count_mode) {
5152 /* TODO: search bigger mode or something and convert... */
5156 mode = get_irn_mode(n);
5157 modulo_shf = get_mode_modulo_shift(mode);
5159 if (modulo_shf > 0) {
5160 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5162 /* I'm not so sure what happens in one complement... */
5163 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5164 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5165 * above will be invalid) */
5166 assert(modulo_shf<=0 || is_po2(modulo_shf));
5168 tv1 = tarval_and(tv1, modulo_mask);
5169 tv2 = tarval_and(tv2, modulo_mask);
5171 res = tarval_add(tv1, tv2);
5172 irg = get_irn_irg(n);
5174 /* beware: a simple replacement works only, if res < modulo shift */
5176 int bits = get_mode_size_bits(mode);
5177 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5178 res = tarval_mod(res, modulo);
5180 long bits = get_mode_size_bits(mode);
5181 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5183 /* shifting too much */
5184 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5186 ir_node *block = get_nodes_block(n);
5187 dbg_info *dbgi = get_irn_dbg_info(n);
5188 ir_mode *smode = get_irn_mode(right);
5189 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5190 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5193 return new_r_Const(irg, get_mode_null(mode));
5197 /* ok, we can replace it */
5198 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5199 block = get_nodes_block(n);
5201 in[0] = get_binop_left(left);
5202 in[1] = new_r_Const(irg, res);
5204 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5206 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5208 return transform_node(irn);
5213 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5215 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5216 * (also with x >>s c1 when c1>=c2)
5218 static ir_node *transform_node_shl_shr(ir_node *n)
5221 ir_node *right = get_binop_right(n);
5231 ir_tarval *tv_shift;
5234 ir_relation relation;
5237 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5239 if (!is_Const(right))
5242 left = get_binop_left(n);
5243 mode = get_irn_mode(n);
5244 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5245 ir_node *shr_right = get_binop_right(left);
5247 if (!is_Const(shr_right))
5250 x = get_binop_left(left);
5251 tv_shr = get_Const_tarval(shr_right);
5252 tv_shl = get_Const_tarval(right);
5254 if (is_Shrs(left)) {
5255 /* shrs variant only allowed if c1 >= c2 */
5256 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5259 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5262 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5264 tv_mask = tarval_shl(tv_mask, tv_shl);
5265 } else if (is_Shr(n) && is_Shl(left)) {
5266 ir_node *shl_right = get_Shl_right(left);
5268 if (!is_Const(shl_right))
5271 x = get_Shl_left(left);
5272 tv_shr = get_Const_tarval(right);
5273 tv_shl = get_Const_tarval(shl_right);
5275 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5276 tv_mask = tarval_shr(tv_mask, tv_shr);
5281 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5282 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5285 assert(tv_mask != tarval_bad);
5286 assert(get_tarval_mode(tv_mask) == mode);
5288 block = get_nodes_block(n);
5289 irg = get_irn_irg(block);
5290 dbgi = get_irn_dbg_info(n);
5292 relation = tarval_cmp(tv_shl, tv_shr);
5293 if (relation == ir_relation_less || relation == ir_relation_equal) {
5294 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5295 new_const = new_r_Const(irg, tv_shift);
5297 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5299 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5302 assert(relation == ir_relation_greater);
5303 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5304 new_const = new_r_Const(irg, tv_shift);
5305 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5308 new_const = new_r_Const(irg, tv_mask);
5309 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5314 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5316 ir_mode *mode = get_tarval_mode(tv);
5317 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5318 return tarval_mod(tv, modulo_tv);
5321 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5322 ir_node *left, ir_node *right, ir_mode *mode);
5325 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5326 * then we can use that to minimize the value of Add(x, const) or
5327 * Sub(Const, x). In particular this often avoids 1 instruction in some
5328 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5329 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5331 static ir_node *transform_node_shift_modulo(ir_node *n,
5332 new_shift_func new_shift)
5334 ir_mode *mode = get_irn_mode(n);
5335 int modulo = get_mode_modulo_shift(mode);
5336 ir_node *newop = NULL;
5337 ir_mode *mode_right;
5344 if (get_mode_arithmetic(mode) != irma_twos_complement)
5346 if (!is_po2(modulo))
5349 irg = get_irn_irg(n);
5350 block = get_nodes_block(n);
5351 right = get_binop_right(n);
5352 mode_right = get_irn_mode(right);
5353 if (is_Const(right)) {
5354 ir_tarval *tv = get_Const_tarval(right);
5355 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5360 newop = new_r_Const(irg, tv_mod);
5361 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5362 ir_node *add_right = get_binop_right(right);
5363 if (is_Const(add_right)) {
5364 ir_tarval *tv = get_Const_tarval(add_right);
5365 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5370 newconst = new_r_Const(irg, tv_mod);
5371 newop = new_r_Add(block, get_binop_left(right), newconst,
5374 } else if (is_Sub(right)) {
5375 ir_node *sub_left = get_Sub_left(right);
5376 if (is_Const(sub_left)) {
5377 ir_tarval *tv = get_Const_tarval(sub_left);
5378 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5383 newconst = new_r_Const(irg, tv_mod);
5384 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5391 if (newop != NULL) {
5392 dbg_info *dbgi = get_irn_dbg_info(n);
5393 ir_node *left = get_binop_left(n);
5394 return new_shift(dbgi, block, left, newop, mode);
5402 static ir_node *transform_node_Shr(ir_node *n)
5404 ir_node *c, *oldn = n;
5405 ir_node *left = get_Shr_left(n);
5406 ir_node *right = get_Shr_right(n);
5407 ir_mode *mode = get_irn_mode(n);
5409 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5410 n = transform_node_shift(n);
5413 n = transform_node_shift_modulo(n, new_rd_Shr);
5415 n = transform_node_shl_shr(n);
5417 n = transform_node_shift_bitop(n);
5425 static ir_node *transform_node_Shrs(ir_node *n)
5428 ir_node *a = get_Shrs_left(n);
5429 ir_node *b = get_Shrs_right(n);
5430 ir_mode *mode = get_irn_mode(n);
5434 if (is_oversize_shift(n)) {
5435 ir_node *block = get_nodes_block(n);
5436 dbg_info *dbgi = get_irn_dbg_info(n);
5437 ir_mode *cmode = get_irn_mode(b);
5438 long val = get_mode_size_bits(cmode)-1;
5439 ir_graph *irg = get_irn_irg(n);
5440 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5441 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5444 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5445 n = transform_node_shift(n);
5449 n = transform_node_shift_modulo(n, new_rd_Shrs);
5452 n = transform_node_shift_bitop(n);
5456 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5457 attr = vrp_get_info(a);
5459 unsigned bits = get_mode_size_bits(mode);
5460 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5461 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5462 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5463 dbg_info *dbgi = get_irn_dbg_info(n);
5464 ir_node *block = get_nodes_block(n);
5465 return new_rd_Shr(dbgi, block, a, b, mode);
5475 static ir_node *transform_node_Shl(ir_node *n)
5477 ir_node *c, *oldn = n;
5478 ir_node *a = get_Shl_left(n);
5479 ir_node *b = get_Shl_right(n);
5480 ir_mode *mode = get_irn_mode(n);
5482 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5483 n = transform_node_shift(n);
5486 n = transform_node_shift_modulo(n, new_rd_Shl);
5488 n = transform_node_shl_shr(n);
5490 n = transform_node_shift_bitop(n);
5498 static ir_node *transform_node_Rotl(ir_node *n)
5500 ir_node *c, *oldn = n;
5501 ir_node *a = get_Rotl_left(n);
5502 ir_node *b = get_Rotl_right(n);
5503 ir_mode *mode = get_irn_mode(n);
5505 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5506 n = transform_node_shift(n);
5509 n = transform_node_shift_bitop(n);
5517 static ir_node *transform_node_Conv(ir_node *n)
5519 ir_node *c, *oldn = n;
5520 ir_mode *mode = get_irn_mode(n);
5521 ir_node *a = get_Conv_op(n);
5523 if (mode != mode_b && is_const_Phi(a)) {
5524 /* Do NOT optimize mode_b Conv's, this leads to remaining
5525 * Phib nodes later, because the conv_b_lower operation
5526 * is instantly reverted, when it tries to insert a Convb.
5528 c = apply_conv_on_phi(a, mode);
5530 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5535 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5536 ir_graph *irg = get_irn_irg(n);
5537 return new_r_Unknown(irg, mode);
5540 if (mode_is_reference(mode) &&
5541 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5543 ir_node *l = get_Add_left(a);
5544 ir_node *r = get_Add_right(a);
5545 dbg_info *dbgi = get_irn_dbg_info(a);
5546 ir_node *block = get_nodes_block(n);
5548 ir_node *lop = get_Conv_op(l);
5549 if (get_irn_mode(lop) == mode) {
5550 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5551 n = new_rd_Add(dbgi, block, lop, r, mode);
5556 ir_node *rop = get_Conv_op(r);
5557 if (get_irn_mode(rop) == mode) {
5558 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5559 n = new_rd_Add(dbgi, block, l, rop, mode);
5569 * Remove dead blocks and nodes in dead blocks
5570 * in keep alive list. We do not generate a new End node.
5572 static ir_node *transform_node_End(ir_node *n)
5574 int i, j, n_keepalives = get_End_n_keepalives(n);
5577 NEW_ARR_A(ir_node *, in, n_keepalives);
5579 for (i = j = 0; i < n_keepalives; ++i) {
5580 ir_node *ka = get_End_keepalive(n, i);
5582 /* no need to keep Bad */
5585 /* do not keep unreachable code */
5586 block = is_Block(ka) ? ka : get_nodes_block(ka);
5587 if (is_block_unreachable(block))
5591 if (j != n_keepalives)
5592 set_End_keepalives(n, j, in);
5596 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5598 if (is_Minus(a) && get_Minus_op(a) == b)
5600 if (is_Minus(b) && get_Minus_op(b) == a)
5602 if (is_Sub(a) && is_Sub(b)) {
5603 ir_node *a_left = get_Sub_left(a);
5604 ir_node *a_right = get_Sub_right(a);
5605 ir_node *b_left = get_Sub_left(b);
5606 ir_node *b_right = get_Sub_right(b);
5608 if (a_left == b_right && a_right == b_left)
5615 static const ir_node *skip_upconv(const ir_node *node)
5617 while (is_Conv(node)) {
5618 ir_mode *mode = get_irn_mode(node);
5619 const ir_node *op = get_Conv_op(node);
5620 ir_mode *op_mode = get_irn_mode(op);
5621 if (!smaller_mode(op_mode, mode))
5628 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5629 const ir_node *mux_true)
5634 ir_relation relation;
5640 * Note further that these optimization work even for floating point
5641 * with NaN's because -NaN == NaN.
5642 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5645 mode = get_irn_mode(mux_true);
5646 if (mode_honor_signed_zeros(mode))
5649 /* must be <, <=, >=, > */
5650 relation = get_Cmp_relation(sel);
5651 if ((relation & ir_relation_less_greater) == 0)
5654 if (!ir_is_negated_value(mux_true, mux_false))
5657 mux_true = skip_upconv(mux_true);
5658 mux_false = skip_upconv(mux_false);
5660 /* must be x cmp 0 */
5661 cmp_right = get_Cmp_right(sel);
5662 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5665 cmp_left = get_Cmp_left(sel);
5666 if (cmp_left == mux_false) {
5667 if (relation & ir_relation_less) {
5670 assert(relation & ir_relation_greater);
5673 } else if (cmp_left == mux_true) {
5674 if (relation & ir_relation_less) {
5677 assert(relation & ir_relation_greater);
5685 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5688 ir_node *cmp_left = get_Cmp_left(sel);
5689 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5692 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5693 const ir_node *mux_true)
5695 /* this code should return true each time transform_node_Mux would
5696 * optimize the Mux completely away */
5698 ir_mode *mode = get_irn_mode(mux_false);
5699 if (get_mode_arithmetic(mode) == irma_twos_complement
5700 && ir_mux_is_abs(sel, mux_false, mux_true))
5703 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5704 const ir_node *cmp_r = get_Cmp_right(sel);
5705 const ir_node *cmp_l = get_Cmp_left(sel);
5706 const ir_node *f = mux_false;
5707 const ir_node *t = mux_true;
5709 if (is_Const(t) && is_Const_null(t)) {
5714 if (is_And(cmp_l) && f == cmp_r) {
5715 ir_node *and_r = get_And_right(cmp_l);
5718 if (and_r == t && is_single_bit(and_r))
5720 and_l = get_And_left(cmp_l);
5721 if (and_l == t && is_single_bit(and_l))
5730 * Optimize a Mux into some simpler cases.
5732 static ir_node *transform_node_Mux(ir_node *n)
5735 ir_node *sel = get_Mux_sel(n);
5736 ir_mode *mode = get_irn_mode(n);
5737 ir_node *t = get_Mux_true(n);
5738 ir_node *f = get_Mux_false(n);
5739 ir_graph *irg = get_irn_irg(n);
5741 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5742 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5743 int abs = ir_mux_is_abs(sel, f, t);
5745 dbg_info *dbgi = get_irn_dbg_info(n);
5746 ir_node *block = get_nodes_block(n);
5747 ir_node *op = ir_get_abs_op(sel, f, t);
5748 int bits = get_mode_size_bits(mode);
5749 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5750 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5751 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5754 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5756 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5762 /* first normalization step: try to move a constant to the false side,
5763 * 0 preferred on false side too */
5764 if (is_Cmp(sel) && is_Const(t) &&
5765 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5766 dbg_info *seldbgi = get_irn_dbg_info(sel);
5767 ir_node *block = get_nodes_block(sel);
5768 ir_relation relation = get_Cmp_relation(sel);
5773 /* Mux(x, a, b) => Mux(not(x), b, a) */
5774 relation = get_negated_relation(relation);
5775 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5776 get_Cmp_right(sel), relation);
5777 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5780 /* the following optimisations create new mode_b nodes, so only do them
5781 * before mode_b lowering */
5782 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
5784 ir_node* block = get_nodes_block(n);
5786 ir_node* c1 = get_Mux_sel(t);
5787 ir_node* t1 = get_Mux_true(t);
5788 ir_node* f1 = get_Mux_false(t);
5790 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5791 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5792 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5797 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5798 } else if (f == t1) {
5799 /* Mux(cond0, Mux(cond1, x, y), x) */
5800 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5801 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5802 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5807 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5809 } else if (is_Mux(f)) {
5810 ir_node* block = get_nodes_block(n);
5812 ir_node* c1 = get_Mux_sel(f);
5813 ir_node* t1 = get_Mux_true(f);
5814 ir_node* f1 = get_Mux_false(f);
5816 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5817 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5818 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5823 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5824 } else if (t == f1) {
5825 /* Mux(cond0, x, Mux(cond1, y, x)) */
5826 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5827 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5828 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5833 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5837 /* note: after normalization, false can only happen on default */
5838 if (mode == mode_b) {
5839 dbg_info *dbg = get_irn_dbg_info(n);
5840 ir_node *block = get_nodes_block(n);
5843 ir_tarval *tv_t = get_Const_tarval(t);
5844 if (tv_t == tarval_b_true) {
5846 /* Muxb(sel, true, false) = sel */
5847 assert(get_Const_tarval(f) == tarval_b_false);
5848 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5851 /* Muxb(sel, true, x) = Or(sel, x) */
5852 n = new_rd_Or(dbg, block, sel, f, mode_b);
5853 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5857 } else if (is_Const(f)) {
5858 ir_tarval *tv_f = get_Const_tarval(f);
5859 if (tv_f == tarval_b_true) {
5860 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5861 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5863 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5866 /* Muxb(sel, x, false) = And(sel, x) */
5867 assert(tv_f == tarval_b_false);
5868 n = new_rd_And(dbg, block, sel, t, mode_b);
5869 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5875 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5876 * value to integer. */
5877 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5878 ir_tarval *a = get_Const_tarval(t);
5879 ir_tarval *b = get_Const_tarval(f);
5881 if (tarval_is_one(a) && tarval_is_null(b)) {
5882 ir_node *block = get_nodes_block(n);
5883 ir_node *conv = new_r_Conv(block, sel, mode);
5885 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5887 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5888 ir_node *block = get_nodes_block(n);
5889 ir_node *not_ = new_r_Not(block, sel, mode_b);
5890 ir_node *conv = new_r_Conv(block, not_, mode);
5892 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5898 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5899 ir_relation relation = get_Cmp_relation(sel);
5900 ir_node *cmp_r = get_Cmp_right(sel);
5901 ir_node *cmp_l = get_Cmp_left(sel);
5902 ir_node *block = get_nodes_block(n);
5904 if (is_And(cmp_l) && f == cmp_r) {
5905 ir_node *and_r = get_And_right(cmp_l);
5908 if (and_r == t && is_single_bit(and_r)) {
5909 if (relation == ir_relation_equal) {
5910 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5911 n = new_rd_Eor(get_irn_dbg_info(n),
5912 block, cmp_l, t, mode);
5913 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5915 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5917 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5921 and_l = get_And_left(cmp_l);
5922 if (and_l == t && is_single_bit(and_l)) {
5923 if (relation == ir_relation_equal) {
5924 /* ((1 << n) & a) == 0, (1 << n), 0) */
5925 n = new_rd_Eor(get_irn_dbg_info(n),
5926 block, cmp_l, t, mode);
5927 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5929 /* ((1 << n) & a) != 0, (1 << n), 0) */
5931 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5942 * optimize Sync nodes that have other syncs as input we simply add the inputs
5943 * of the other sync to our own inputs
5945 static ir_node *transform_node_Sync(ir_node *n)
5947 int arity = get_Sync_n_preds(n);
5950 for (i = 0; i < arity;) {
5951 ir_node *pred = get_Sync_pred(n, i);
5955 /* Remove Bad predecessors */
5962 /* Remove duplicate predecessors */
5963 for (j = 0; j < i; ++j) {
5964 if (get_Sync_pred(n, j) == pred) {
5973 if (!is_Sync(pred)) {
5981 pred_arity = get_Sync_n_preds(pred);
5982 for (j = 0; j < pred_arity; ++j) {
5983 ir_node *pred_pred = get_Sync_pred(pred, j);
5988 add_irn_n(n, pred_pred);
5992 if (get_Sync_pred(n, k) == pred_pred) break;
5998 ir_graph *irg = get_irn_irg(n);
5999 return new_r_Bad(irg, mode_M);
6002 return get_Sync_pred(n, 0);
6005 /* rehash the sync node */
6010 static ir_node *transform_node_Load(ir_node *n)
6012 /* if our memory predecessor is a load from the same address, then reuse the
6013 * previous result */
6014 ir_node *mem = get_Load_mem(n);
6019 /* don't touch volatile loads */
6020 if (get_Load_volatility(n) == volatility_is_volatile)
6022 mem_pred = get_Proj_pred(mem);
6023 if (is_Load(mem_pred)) {
6024 ir_node *pred_load = mem_pred;
6026 /* conservatively compare the 2 loads. TODO: This could be less strict
6027 * with fixup code in some situations (like smaller/bigger modes) */
6028 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6030 if (get_Load_mode(pred_load) != get_Load_mode(n))
6032 /* all combinations of aligned/unaligned pred/n should be fine so we do
6033 * not compare the unaligned attribute */
6035 ir_node *block = get_nodes_block(n);
6036 ir_node *jmp = new_r_Jmp(block);
6037 ir_graph *irg = get_irn_irg(n);
6038 ir_node *bad = new_r_Bad(irg, mode_X);
6039 ir_mode *mode = get_Load_mode(n);
6040 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6041 ir_node *in[] = { mem, res, jmp, bad };
6042 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6045 } else if (is_Store(mem_pred)) {
6046 ir_node *pred_store = mem_pred;
6047 ir_node *value = get_Store_value(pred_store);
6049 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6051 if (get_irn_mode(value) != get_Load_mode(n))
6053 /* all combinations of aligned/unaligned pred/n should be fine so we do
6054 * not compare the unaligned attribute */
6056 ir_node *block = get_nodes_block(n);
6057 ir_node *jmp = new_r_Jmp(block);
6058 ir_graph *irg = get_irn_irg(n);
6059 ir_node *bad = new_r_Bad(irg, mode_X);
6060 ir_node *res = value;
6061 ir_node *in[] = { mem, res, jmp, bad };
6062 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6071 * optimize a trampoline Call into a direct Call
6073 static ir_node *transform_node_Call(ir_node *call)
6075 ir_node *callee = get_Call_ptr(call);
6076 ir_node *adr, *mem, *res, *bl, **in;
6077 ir_type *ctp, *mtp, *tp;
6081 size_t i, n_res, n_param;
6084 if (! is_Proj(callee))
6086 callee = get_Proj_pred(callee);
6087 if (! is_Builtin(callee))
6089 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6092 mem = get_Call_mem(call);
6094 if (skip_Proj(mem) == callee) {
6095 /* memory is routed to the trampoline, skip */
6096 mem = get_Builtin_mem(callee);
6099 /* build a new call type */
6100 mtp = get_Call_type(call);
6101 tdb = get_type_dbg_info(mtp);
6103 n_res = get_method_n_ress(mtp);
6104 n_param = get_method_n_params(mtp);
6105 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6107 for (i = 0; i < n_res; ++i)
6108 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6110 NEW_ARR_A(ir_node *, in, n_param + 1);
6112 /* FIXME: we don't need a new pointer type in every step */
6113 irg = get_irn_irg(call);
6114 tp = get_irg_frame_type(irg);
6115 tp = new_type_pointer(tp);
6116 set_method_param_type(ctp, 0, tp);
6118 in[0] = get_Builtin_param(callee, 2);
6119 for (i = 0; i < n_param; ++i) {
6120 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6121 in[i + 1] = get_Call_param(call, i);
6123 var = get_method_variadicity(mtp);
6124 set_method_variadicity(ctp, var);
6125 /* When we resolve a trampoline, the function must be called by a this-call */
6126 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6127 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6129 adr = get_Builtin_param(callee, 1);
6131 db = get_irn_dbg_info(call);
6132 bl = get_nodes_block(call);
6134 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6135 if (get_irn_pinned(call) == op_pin_state_floats)
6136 set_irn_pinned(res, op_pin_state_floats);
6141 * Tries several [inplace] [optimizing] transformations and returns an
6142 * equivalent node. The difference to equivalent_node() is that these
6143 * transformations _do_ generate new nodes, and thus the old node must
6144 * not be freed even if the equivalent node isn't the old one.
6146 static ir_node *transform_node(ir_node *n)
6151 * Transform_node is the only "optimizing transformation" that might
6152 * return a node with a different opcode. We iterate HERE until fixpoint
6153 * to get the final result.
6157 if (n->op->ops.transform_node != NULL)
6158 n = n->op->ops.transform_node(n);
6159 } while (oldn != n);
6165 * Sets the default transform node operation for an ir_op_ops.
6167 * @param code the opcode for the default operation
6168 * @param ops the operations initialized
6173 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6177 ops->transform_node = transform_node_##a; \
6179 #define CASE_PROJ(a) \
6181 ops->transform_node_Proj = transform_node_Proj_##a; \
6183 #define CASE_PROJ_EX(a) \
6185 ops->transform_node = transform_node_##a; \
6186 ops->transform_node_Proj = transform_node_Proj_##a; \
6231 /* **************** Common Subexpression Elimination **************** */
6233 /** The size of the hash table used, should estimate the number of nodes
6235 #define N_IR_NODES 512
6237 /** Compares two exception attributes */
6238 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6240 const except_attr *ea = &a->attr.except;
6241 const except_attr *eb = &b->attr.except;
6242 return ea->pin_state != eb->pin_state;
6245 /** Compares the attributes of two Const nodes. */
6246 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6248 return get_Const_tarval(a) != get_Const_tarval(b);
6251 /** Compares the attributes of two Proj nodes. */
6252 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6254 return a->attr.proj.proj != b->attr.proj.proj;
6257 /** Compares the attributes of two Alloc nodes. */
6258 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6260 const alloc_attr *pa = &a->attr.alloc;
6261 const alloc_attr *pb = &b->attr.alloc;
6262 if (pa->where != pb->where || pa->type != pb->type)
6264 return node_cmp_exception(a, b);
6267 /** Compares the attributes of two Free nodes. */
6268 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6270 const free_attr *pa = &a->attr.free;
6271 const free_attr *pb = &b->attr.free;
6272 return (pa->where != pb->where) || (pa->type != pb->type);
6275 /** Compares the attributes of two SymConst nodes. */
6276 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6278 const symconst_attr *pa = &a->attr.symc;
6279 const symconst_attr *pb = &b->attr.symc;
6280 return (pa->kind != pb->kind)
6281 || (pa->sym.type_p != pb->sym.type_p);
6284 /** Compares the attributes of two Call nodes. */
6285 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6287 const call_attr *pa = &a->attr.call;
6288 const call_attr *pb = &b->attr.call;
6289 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6291 return node_cmp_exception(a, b);
6294 /** Compares the attributes of two Sel nodes. */
6295 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6297 const ir_entity *a_ent = get_Sel_entity(a);
6298 const ir_entity *b_ent = get_Sel_entity(b);
6299 return a_ent != b_ent;
6302 /** Compares the attributes of two Phi nodes. */
6303 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6305 /* we can only enter this function if both nodes have the same number of inputs,
6306 hence it is enough to check if one of them is a Phi0 */
6308 /* check the Phi0 pos attribute */
6309 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6314 /** Compares the attributes of two Conv nodes. */
6315 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6317 return get_Conv_strict(a) != get_Conv_strict(b);
6320 /** Compares the attributes of two Cast nodes. */
6321 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6323 return get_Cast_type(a) != get_Cast_type(b);
6326 /** Compares the attributes of two Load nodes. */
6327 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6329 if (get_Load_volatility(a) == volatility_is_volatile ||
6330 get_Load_volatility(b) == volatility_is_volatile)
6331 /* NEVER do CSE on volatile Loads */
6333 /* do not CSE Loads with different alignment. Be conservative. */
6334 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6336 if (get_Load_mode(a) != get_Load_mode(b))
6338 return node_cmp_exception(a, b);
6341 /** Compares the attributes of two Store nodes. */
6342 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6344 /* do not CSE Stores with different alignment. Be conservative. */
6345 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6347 /* NEVER do CSE on volatile Stores */
6348 if (get_Store_volatility(a) == volatility_is_volatile ||
6349 get_Store_volatility(b) == volatility_is_volatile)
6351 return node_cmp_exception(a, b);
6354 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6356 if (get_CopyB_type(a) != get_CopyB_type(b))
6359 return node_cmp_exception(a, b);
6362 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6364 return node_cmp_exception(a, b);
6367 /** Compares the attributes of two Div nodes. */
6368 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6370 const div_attr *ma = &a->attr.div;
6371 const div_attr *mb = &b->attr.div;
6372 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6374 return node_cmp_exception(a, b);
6377 /** Compares the attributes of two Mod nodes. */
6378 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6380 const mod_attr *ma = &a->attr.mod;
6381 const mod_attr *mb = &b->attr.mod;
6382 if (ma->resmode != mb->resmode)
6384 return node_cmp_exception(a, b);
6387 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6389 const cmp_attr *ma = &a->attr.cmp;
6390 const cmp_attr *mb = &b->attr.cmp;
6391 return ma->relation != mb->relation;
6394 /** Compares the attributes of two Confirm nodes. */
6395 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6397 const confirm_attr *ma = &a->attr.confirm;
6398 const confirm_attr *mb = &b->attr.confirm;
6399 return ma->relation != mb->relation;
6402 /** Compares the attributes of two Builtin nodes. */
6403 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6405 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6407 if (get_Builtin_type(a) != get_Builtin_type(b))
6409 return node_cmp_exception(a, b);
6412 /** Compares the attributes of two ASM nodes. */
6413 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6416 const ir_asm_constraint *ca;
6417 const ir_asm_constraint *cb;
6420 if (get_ASM_text(a) != get_ASM_text(b))
6423 /* Should we really check the constraints here? Should be better, but is strange. */
6424 n = get_ASM_n_input_constraints(a);
6425 if (n != get_ASM_n_input_constraints(b))
6428 ca = get_ASM_input_constraints(a);
6429 cb = get_ASM_input_constraints(b);
6430 for (i = 0; i < n; ++i) {
6431 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6432 || ca[i].mode != cb[i].mode)
6436 n = get_ASM_n_output_constraints(a);
6437 if (n != get_ASM_n_output_constraints(b))
6440 ca = get_ASM_output_constraints(a);
6441 cb = get_ASM_output_constraints(b);
6442 for (i = 0; i < n; ++i) {
6443 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6444 || ca[i].mode != cb[i].mode)
6448 n = get_ASM_n_clobbers(a);
6449 if (n != get_ASM_n_clobbers(b))
6452 cla = get_ASM_clobbers(a);
6453 clb = get_ASM_clobbers(b);
6454 for (i = 0; i < n; ++i) {
6455 if (cla[i] != clb[i])
6459 return node_cmp_exception(a, b);
6462 /** Compares the inexistent attributes of two Dummy nodes. */
6463 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6467 /* Dummy nodes never equal by definition */
6471 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6473 if (get_InstOf_type(a) != get_InstOf_type(b))
6475 return node_cmp_exception(a, b);
6479 * Set the default node attribute compare operation for an ir_op_ops.
6481 * @param code the opcode for the default operation
6482 * @param ops the operations initialized
6487 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6491 ops->node_cmp_attr = node_cmp_attr_##a; \
6527 * Compare function for two nodes in the value table. Gets two
6528 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6530 int identities_cmp(const void *elt, const void *key)
6532 ir_node *a = (ir_node *)elt;
6533 ir_node *b = (ir_node *)key;
6536 if (a == b) return 0;
6538 if ((get_irn_op(a) != get_irn_op(b)) ||
6539 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6541 /* compare if a's in and b's in are of equal length */
6542 irn_arity_a = get_irn_arity(a);
6543 if (irn_arity_a != get_irn_arity(b))
6546 /* blocks are never the same */
6550 if (get_irn_pinned(a) == op_pin_state_pinned) {
6551 /* for pinned nodes, the block inputs must be equal */
6552 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6555 ir_node *block_a = get_nodes_block(a);
6556 ir_node *block_b = get_nodes_block(b);
6557 if (! get_opt_global_cse()) {
6558 /* for block-local CSE both nodes must be in the same Block */
6559 if (block_a != block_b)
6562 /* The optimistic approach would be to do nothing here.
6563 * However doing GCSE optimistically produces a lot of partially dead code which appears
6564 * to be worse in practice than the missed opportunities.
6565 * So we use a very conservative variant here and only CSE if 1 value dominates the
6567 if (!block_dominates(block_a, block_b)
6568 && !block_dominates(block_b, block_a))
6573 /* compare a->in[0..ins] with b->in[0..ins] */
6574 for (i = 0; i < irn_arity_a; ++i) {
6575 ir_node *pred_a = get_irn_n(a, i);
6576 ir_node *pred_b = get_irn_n(b, i);
6577 if (pred_a != pred_b) {
6578 /* if both predecessors are CSE neutral they might be different */
6579 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6585 * here, we already now that the nodes are identical except their
6588 if (a->op->ops.node_cmp_attr)
6589 return a->op->ops.node_cmp_attr(a, b);
6595 * Calculate a hash value of a node.
6597 * @param node The IR-node
6599 unsigned ir_node_hash(const ir_node *node)
6601 return node->op->ops.hash(node);
6605 void new_identities(ir_graph *irg)
6607 if (irg->value_table != NULL)
6608 del_pset(irg->value_table);
6609 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6612 void del_identities(ir_graph *irg)
6614 if (irg->value_table != NULL)
6615 del_pset(irg->value_table);
6618 /* Normalize a node by putting constants (and operands with larger
6619 * node index) on the right (operator side). */
6620 void ir_normalize_node(ir_node *n)
6622 if (is_op_commutative(get_irn_op(n))) {
6623 ir_node *l = get_binop_left(n);
6624 ir_node *r = get_binop_right(n);
6626 /* For commutative operators perform a OP b == b OP a but keep
6627 * constants on the RIGHT side. This helps greatly in some
6628 * optimizations. Moreover we use the idx number to make the form
6630 if (!operands_are_normalized(l, r)) {
6631 set_binop_left(n, r);
6632 set_binop_right(n, l);
6639 * Return the canonical node computing the same value as n.
6640 * Looks up the node in a hash table, enters it in the table
6641 * if it isn't there yet.
6643 * @param n the node to look up
6645 * @return a node that computes the same value as n or n if no such
6646 * node could be found
6648 ir_node *identify_remember(ir_node *n)
6650 ir_graph *irg = get_irn_irg(n);
6651 pset *value_table = irg->value_table;
6654 if (value_table == NULL)
6657 ir_normalize_node(n);
6658 /* lookup or insert in hash table with given hash key. */
6659 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6662 /* n is reachable again */
6663 edges_node_revival(nn);
6670 * During construction we set the op_pin_state_pinned flag in the graph right
6671 * when the optimization is performed. The flag turning on procedure global
6672 * cse could be changed between two allocations. This way we are safe.
6674 * @param n The node to lookup
6676 static inline ir_node *identify_cons(ir_node *n)
6680 n = identify_remember(n);
6681 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6682 ir_graph *irg = get_irn_irg(n);
6683 set_irg_pinned(irg, op_pin_state_floats);
6688 /* Add a node to the identities value table. */
6689 void add_identities(ir_node *node)
6696 identify_remember(node);
6699 /* Visit each node in the value table of a graph. */
6700 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6703 ir_graph *rem = current_ir_graph;
6705 current_ir_graph = irg;
6706 foreach_pset(irg->value_table, ir_node*, node) {
6709 current_ir_graph = rem;
6713 * These optimizations deallocate nodes from the obstack.
6714 * It can only be called if it is guaranteed that no other nodes
6715 * reference this one, i.e., right after construction of a node.
6717 * @param n The node to optimize
6719 ir_node *optimize_node(ir_node *n)
6722 ir_graph *irg = get_irn_irg(n);
6723 unsigned iro = get_irn_opcode(n);
6726 /* Always optimize Phi nodes: part of the construction. */
6727 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6729 /* constant expression evaluation / constant folding */
6730 if (get_opt_constant_folding()) {
6731 /* neither constants nor Tuple values can be evaluated */
6732 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6733 /* try to evaluate */
6734 tv = computed_value(n);
6735 if (tv != tarval_bad) {
6740 * we MUST copy the node here temporarily, because it's still
6741 * needed for DBG_OPT_CSTEVAL
6743 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6744 oldn = (ir_node*)alloca(node_size);
6746 memcpy(oldn, n, node_size);
6747 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6749 /* ARG, copy the in array, we need it for statistics */
6750 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6752 /* note the inplace edges module */
6753 edges_node_deleted(n);
6755 /* evaluation was successful -- replace the node. */
6756 irg_kill_node(irg, n);
6757 nw = new_r_Const(irg, tv);
6759 DBG_OPT_CSTEVAL(oldn, nw);
6765 /* remove unnecessary nodes */
6766 if (get_opt_algebraic_simplification() ||
6767 (iro == iro_Phi) || /* always optimize these nodes. */
6769 (iro == iro_Proj) ||
6770 (iro == iro_Block) ) /* Flags tested local. */
6771 n = equivalent_node(n);
6773 /* Common Subexpression Elimination.
6775 * Checks whether n is already available.
6776 * The block input is used to distinguish different subexpressions. Right
6777 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6778 * subexpressions within a block.
6781 n = identify_cons(n);
6784 edges_node_deleted(oldn);
6786 /* We found an existing, better node, so we can deallocate the old node. */
6787 irg_kill_node(irg, oldn);
6791 /* Some more constant expression evaluation that does not allow to
6793 iro = get_irn_opcode(n);
6794 if (get_opt_algebraic_simplification() ||
6795 (iro == iro_Cond) ||
6796 (iro == iro_Proj)) /* Flags tested local. */
6797 n = transform_node(n);
6799 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6800 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6802 n = identify_remember(o);
6812 * These optimizations never deallocate nodes (in place). This can cause dead
6813 * nodes lying on the obstack. Remove these by a dead node elimination,
6814 * i.e., a copying garbage collection.
6816 ir_node *optimize_in_place_2(ir_node *n)
6820 unsigned iro = get_irn_opcode(n);
6822 if (!get_opt_optimize() && !is_Phi(n)) return n;
6824 if (iro == iro_Deleted)
6827 /* constant expression evaluation / constant folding */
6828 if (get_opt_constant_folding()) {
6829 /* neither constants nor Tuple values can be evaluated */
6830 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6831 /* try to evaluate */
6832 tv = computed_value(n);
6833 if (tv != tarval_bad) {
6834 /* evaluation was successful -- replace the node. */
6835 ir_graph *irg = get_irn_irg(n);
6837 n = new_r_Const(irg, tv);
6839 DBG_OPT_CSTEVAL(oldn, n);
6845 /* remove unnecessary nodes */
6846 if (get_opt_constant_folding() ||
6847 (iro == iro_Phi) || /* always optimize these nodes. */
6848 (iro == iro_Id) || /* ... */
6849 (iro == iro_Proj) || /* ... */
6850 (iro == iro_Block) ) /* Flags tested local. */
6851 n = equivalent_node(n);
6853 /** common subexpression elimination **/
6854 /* Checks whether n is already available. */
6855 /* The block input is used to distinguish different subexpressions. Right
6856 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6857 subexpressions within a block. */
6858 if (get_opt_cse()) {
6860 n = identify_remember(o);
6865 /* Some more constant expression evaluation. */
6866 iro = get_irn_opcode(n);
6867 if (get_opt_constant_folding() ||
6868 (iro == iro_Cond) ||
6869 (iro == iro_Proj)) /* Flags tested local. */
6870 n = transform_node(n);
6872 /* Now we can verify the node, as it has no dead inputs any more. */
6875 /* Now we have a legal, useful node. Enter it in hash table for cse.
6876 Blocks should be unique anyways. (Except the successor of start:
6877 is cse with the start block!) */
6878 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6880 n = identify_remember(o);
6889 * Wrapper for external use, set proper status bits after optimization.
6891 ir_node *optimize_in_place(ir_node *n)
6893 ir_graph *irg = get_irn_irg(n);
6894 /* Handle graph state */
6895 assert(get_irg_phase_state(irg) != phase_building);
6897 if (get_opt_global_cse())
6898 set_irg_pinned(irg, op_pin_state_floats);
6900 /* FIXME: Maybe we could also test whether optimizing the node can
6901 change the control graph. */
6902 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
6903 return optimize_in_place_2(n);
6907 * Calculate a hash value of a Const node.
6909 static unsigned hash_Const(const ir_node *node)
6913 /* special value for const, as they only differ in their tarval. */
6914 h = HASH_PTR(node->attr.con.tarval);
6920 * Calculate a hash value of a SymConst node.
6922 static unsigned hash_SymConst(const ir_node *node)
6926 /* all others are pointers */
6927 h = HASH_PTR(node->attr.symc.sym.type_p);
6933 * Set the default hash operation in an ir_op_ops.
6935 * @param code the opcode for the default operation
6936 * @param ops the operations initialized
6941 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6945 ops->hash = hash_##a; \
6948 /* hash function already set */
6949 if (ops->hash != NULL)
6956 /* use input/mode default hash if no function was given */
6957 ops->hash = firm_default_hash;
6965 * Sets the default operation for an ir_ops.
6967 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6969 ops = firm_set_default_hash(code, ops);
6970 ops = firm_set_default_computed_value(code, ops);
6971 ops = firm_set_default_equivalent_node(code, ops);
6972 ops = firm_set_default_transform_node(code, ops);
6973 ops = firm_set_default_node_cmp_attr(code, ops);
6974 ops = firm_set_default_get_type_attr(code, ops);
6975 ops = firm_set_default_get_entity_attr(code, ops);