2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
46 #include "opt_polymorphy.h"
51 #include "firm_types.h"
52 #include "bitfiddle.h"
55 /* Make types visible to allow most efficient access */
58 static bool is_Or_Eor_Add(const ir_node *node)
60 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
61 ir_node *left = get_binop_left(node);
62 ir_node *right = get_binop_right(node);
63 vrp_attr *vrp_left = vrp_get_info(left);
64 vrp_attr *vrp_right = vrp_get_info(right);
65 if (vrp_left != NULL && vrp_right != NULL) {
67 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
68 return tarval_is_null(vrp_val);
75 * Returns the tarval of a Const node or tarval_bad for all other nodes.
77 static ir_tarval *default_value_of(const ir_node *n)
80 return get_Const_tarval(n); /* might return tarval_bad */
85 value_of_func value_of_ptr = default_value_of;
87 /* * Set a new value_of function. */
88 void set_value_of_func(value_of_func func)
93 value_of_ptr = default_value_of;
97 * Return the value of a Constant.
99 static ir_tarval *computed_value_Const(const ir_node *n)
101 return get_Const_tarval(n);
105 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
107 static ir_tarval *computed_value_SymConst(const ir_node *n)
112 switch (get_SymConst_kind(n)) {
113 case symconst_type_size:
114 type = get_SymConst_type(n);
115 if (get_type_state(type) == layout_fixed)
116 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
118 case symconst_type_align:
119 type = get_SymConst_type(n);
120 if (get_type_state(type) == layout_fixed)
121 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
123 case symconst_ofs_ent:
124 ent = get_SymConst_entity(n);
125 type = get_entity_owner(ent);
126 if (get_type_state(type) == layout_fixed)
127 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
136 * Return the value of an Add.
138 static ir_tarval *computed_value_Add(const ir_node *n)
140 ir_node *a = get_Add_left(n);
141 ir_node *b = get_Add_right(n);
143 ir_tarval *ta = value_of(a);
144 ir_tarval *tb = value_of(b);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_add(ta, tb);
150 if ((is_Not(a) && get_Not_op(a) == b)
151 || (is_Not(b) && get_Not_op(b) == a)) {
152 return get_mode_all_one(get_irn_mode(n));
159 * Return the value of a Sub.
160 * Special case: a - a
162 static ir_tarval *computed_value_Sub(const ir_node *n)
164 ir_mode *mode = get_irn_mode(n);
165 ir_node *a = get_Sub_left(n);
166 ir_node *b = get_Sub_right(n);
171 if (! mode_is_float(mode)) {
174 return get_mode_null(mode);
180 if ((ta != tarval_bad) && (tb != tarval_bad))
181 return tarval_sub(ta, tb, mode);
187 * Return the value of a Carry.
188 * Special : a op 0, 0 op b
190 static ir_tarval *computed_value_Carry(const ir_node *n)
192 ir_node *a = get_binop_left(n);
193 ir_node *b = get_binop_right(n);
194 ir_mode *m = get_irn_mode(n);
195 ir_tarval *ta = value_of(a);
196 ir_tarval *tb = value_of(b);
198 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
202 if (tarval_is_null(ta) || tarval_is_null(tb))
203 return get_mode_null(m);
209 * Return the value of a Borrow.
212 static ir_tarval *computed_value_Borrow(const ir_node *n)
214 ir_node *a = get_binop_left(n);
215 ir_node *b = get_binop_right(n);
216 ir_mode *m = get_irn_mode(n);
217 ir_tarval *ta = value_of(a);
218 ir_tarval *tb = value_of(b);
220 if ((ta != tarval_bad) && (tb != tarval_bad)) {
221 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
222 } else if (tarval_is_null(ta)) {
223 return get_mode_null(m);
229 * Return the value of an unary Minus.
231 static ir_tarval *computed_value_Minus(const ir_node *n)
233 ir_node *a = get_Minus_op(n);
234 ir_tarval *ta = value_of(a);
236 if (ta != tarval_bad)
237 return tarval_neg(ta);
243 * Return the value of a Mul.
245 static ir_tarval *computed_value_Mul(const ir_node *n)
247 ir_node *a = get_Mul_left(n);
248 ir_node *b = get_Mul_right(n);
249 ir_tarval *ta = value_of(a);
250 ir_tarval *tb = value_of(b);
253 mode = get_irn_mode(n);
254 if (mode != get_irn_mode(a)) {
255 /* n * n = 2n bit multiplication */
256 ta = tarval_convert_to(ta, mode);
257 tb = tarval_convert_to(tb, mode);
260 if (ta != tarval_bad && tb != tarval_bad) {
261 return tarval_mul(ta, tb);
263 /* a * 0 != 0 if a == NaN or a == Inf */
264 if (!mode_is_float(mode)) {
265 /* a*0 = 0 or 0*b = 0 */
266 if (ta == get_mode_null(mode))
268 if (tb == get_mode_null(mode))
276 * Return the value of an And.
277 * Special case: a & 0, 0 & b
279 static ir_tarval *computed_value_And(const ir_node *n)
281 ir_node *a = get_And_left(n);
282 ir_node *b = get_And_right(n);
283 ir_tarval *ta = value_of(a);
284 ir_tarval *tb = value_of(b);
286 if ((ta != tarval_bad) && (tb != tarval_bad)) {
287 return tarval_and (ta, tb);
290 if (tarval_is_null(ta)) return ta;
291 if (tarval_is_null(tb)) return tb;
294 if ((is_Not(a) && get_Not_op(a) == b)
295 || (is_Not(b) && get_Not_op(b) == a)) {
296 return get_mode_null(get_irn_mode(n));
303 * Return the value of an Or.
304 * Special case: a | 1...1, 1...1 | b
306 static ir_tarval *computed_value_Or(const ir_node *n)
308 ir_node *a = get_Or_left(n);
309 ir_node *b = get_Or_right(n);
310 ir_tarval *ta = value_of(a);
311 ir_tarval *tb = value_of(b);
313 if ((ta != tarval_bad) && (tb != tarval_bad)) {
314 return tarval_or (ta, tb);
317 if (tarval_is_all_one(ta)) return ta;
318 if (tarval_is_all_one(tb)) return tb;
321 if ((is_Not(a) && get_Not_op(a) == b)
322 || (is_Not(b) && get_Not_op(b) == a)) {
323 return get_mode_all_one(get_irn_mode(n));
329 * Return the value of an Eor.
331 static ir_tarval *computed_value_Eor(const ir_node *n)
333 ir_node *a = get_Eor_left(n);
334 ir_node *b = get_Eor_right(n);
339 return get_mode_null(get_irn_mode(n));
341 if ((is_Not(a) && get_Not_op(a) == b)
342 || (is_Not(b) && get_Not_op(b) == a)) {
343 return get_mode_all_one(get_irn_mode(n));
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_eor(ta, tb);
356 * Return the value of a Not.
358 static ir_tarval *computed_value_Not(const ir_node *n)
360 ir_node *a = get_Not_op(n);
361 ir_tarval *ta = value_of(a);
363 if (ta != tarval_bad)
364 return tarval_not(ta);
370 * Tests whether a shift shifts more bits than available in the mode
372 static bool is_oversize_shift(const ir_node *n)
374 ir_node *count = get_binop_right(n);
375 ir_mode *mode = get_irn_mode(n);
376 ir_tarval *tv = value_of(count);
379 if (tv == tarval_bad)
381 if (!tarval_is_long(tv))
383 shiftval = get_tarval_long(tv);
384 modulo_shift = get_mode_modulo_shift(mode);
385 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
388 return shiftval >= (long)get_mode_size_bits(mode);
392 * Return the value of a Shl.
394 static ir_tarval *computed_value_Shl(const ir_node *n)
396 ir_node *a = get_Shl_left(n);
397 ir_node *b = get_Shl_right(n);
399 ir_tarval *ta = value_of(a);
400 ir_tarval *tb = value_of(b);
402 if ((ta != tarval_bad) && (tb != tarval_bad)) {
403 return tarval_shl(ta, tb);
406 if (is_oversize_shift(n))
407 return get_mode_null(get_irn_mode(n));
413 * Return the value of a Shr.
415 static ir_tarval *computed_value_Shr(const ir_node *n)
417 ir_node *a = get_Shr_left(n);
418 ir_node *b = get_Shr_right(n);
420 ir_tarval *ta = value_of(a);
421 ir_tarval *tb = value_of(b);
423 if ((ta != tarval_bad) && (tb != tarval_bad)) {
424 return tarval_shr(ta, tb);
426 if (is_oversize_shift(n))
427 return get_mode_null(get_irn_mode(n));
433 * Return the value of a Shrs.
435 static ir_tarval *computed_value_Shrs(const ir_node *n)
437 ir_node *a = get_Shrs_left(n);
438 ir_node *b = get_Shrs_right(n);
440 ir_tarval *ta = value_of(a);
441 ir_tarval *tb = value_of(b);
443 if ((ta != tarval_bad) && (tb != tarval_bad)) {
444 return tarval_shrs(ta, tb);
450 * Return the value of a Rotl.
452 static ir_tarval *computed_value_Rotl(const ir_node *n)
454 ir_node *a = get_Rotl_left(n);
455 ir_node *b = get_Rotl_right(n);
457 ir_tarval *ta = value_of(a);
458 ir_tarval *tb = value_of(b);
460 if ((ta != tarval_bad) && (tb != tarval_bad)) {
461 return tarval_rotl(ta, tb);
466 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
468 ir_mode *mode = get_irn_mode(node);
469 if (get_mode_arithmetic(mode) != irma_twos_complement
470 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
474 ir_node *count = get_Shl_right(node);
475 if (is_Const(count)) {
476 ir_tarval *tv = get_Const_tarval(count);
477 if (tarval_is_long(tv)) {
478 long shiftval = get_tarval_long(tv);
479 long destbits = get_mode_size_bits(dest_mode);
480 if (shiftval >= destbits
481 && shiftval < (long)get_mode_modulo_shift(mode))
487 ir_node *right = get_And_right(node);
488 if (is_Const(right)) {
489 ir_tarval *tv = get_Const_tarval(right);
490 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
491 return tarval_is_null(conved);
498 * Return the value of a Conv.
500 static ir_tarval *computed_value_Conv(const ir_node *n)
502 ir_node *a = get_Conv_op(n);
503 ir_tarval *ta = value_of(a);
504 ir_mode *mode = get_irn_mode(n);
506 if (ta != tarval_bad)
507 return tarval_convert_to(ta, get_irn_mode(n));
509 if (ir_zero_when_converted(a, mode))
510 return get_mode_null(mode);
516 * Calculate the value of a Mux: can be evaluated, if the
517 * sel and the right input are known.
519 static ir_tarval *computed_value_Mux(const ir_node *n)
521 ir_node *sel = get_Mux_sel(n);
522 ir_tarval *ts = value_of(sel);
524 if (ts == get_tarval_b_true()) {
525 ir_node *v = get_Mux_true(n);
528 else if (ts == get_tarval_b_false()) {
529 ir_node *v = get_Mux_false(n);
536 * Calculate the value of a Confirm: can be evaluated,
537 * if it has the form Confirm(x, '=', Const).
539 static ir_tarval *computed_value_Confirm(const ir_node *n)
541 if (get_Confirm_relation(n) == ir_relation_equal) {
542 ir_tarval *tv = value_of(get_Confirm_bound(n));
543 if (tv != tarval_bad)
546 return value_of(get_Confirm_value(n));
550 * gives a (conservative) estimation of possible relation when comparing
553 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
554 const ir_node *right)
556 ir_relation possible = ir_relation_true;
557 ir_tarval *tv_l = value_of(left);
558 ir_tarval *tv_r = value_of(right);
559 ir_mode *mode = get_irn_mode(left);
560 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
561 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
563 /* both values known - evaluate them */
564 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
565 possible = tarval_cmp(tv_l, tv_r);
566 /* we can return now, won't get any better */
569 /* a == a is never less or greater (but might be equal or unordered) */
571 possible &= ~ir_relation_less_greater;
572 /* unordered results only happen for float compares */
573 if (!mode_is_float(mode))
574 possible &= ~ir_relation_unordered;
575 /* values can never be less than the least representable number or
576 * greater than the greatest representable number */
578 possible &= ~ir_relation_greater;
580 possible &= ~ir_relation_less;
582 possible &= ~ir_relation_greater;
584 possible &= ~ir_relation_less;
585 /* maybe vrp can tell us more */
586 possible &= vrp_cmp(left, right);
587 /* Alloc nodes never return null (but throw an exception) */
588 if (is_Alloc(left) && tarval_is_null(tv_r))
589 possible &= ~ir_relation_equal;
590 /* stuff known through confirm nodes */
591 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
592 possible &= get_Confirm_relation(left);
594 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
595 ir_relation relation = get_Confirm_relation(right);
596 relation = get_inversed_relation(relation);
597 possible &= relation;
603 static ir_tarval *compute_cmp(const ir_node *cmp)
605 ir_node *left = get_Cmp_left(cmp);
606 ir_node *right = get_Cmp_right(cmp);
607 ir_relation possible = ir_get_possible_cmp_relations(left, right);
608 ir_relation relation = get_Cmp_relation(cmp);
610 /* if none of the requested relations is possible, return false */
611 if ((possible & relation) == ir_relation_false)
612 return tarval_b_false;
613 /* if possible relations are a subset of the requested ones return true */
614 if ((possible & ~relation) == ir_relation_false)
615 return tarval_b_true;
617 return computed_value_Cmp_Confirm(cmp, left, right, relation);
621 * Return the value of a Cmp.
623 * The basic idea here is to determine which relations are possible and which
624 * one are definitely impossible.
626 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
628 /* we can't construct Constb after lowering mode_b nodes */
629 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
632 return compute_cmp(cmp);
636 * Calculate the value of an integer Div.
637 * Special case: 0 / b
639 static ir_tarval *do_computed_value_Div(const ir_node *div)
641 const ir_node *a = get_Div_left(div);
642 const ir_node *b = get_Div_right(div);
643 const ir_mode *mode = get_Div_resmode(div);
644 ir_tarval *ta = value_of(a);
646 const ir_node *dummy;
648 /* cannot optimize 0 / b = 0 because of NaN */
649 if (!mode_is_float(mode)) {
650 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
651 return ta; /* 0 / b == 0 if b != 0 */
654 if (ta != tarval_bad && tb != tarval_bad)
655 return tarval_div(ta, tb);
660 * Calculate the value of an integer Mod of two nodes.
661 * Special case: a % 1
663 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
665 ir_tarval *ta = value_of(a);
666 ir_tarval *tb = value_of(b);
668 /* Compute a % 1 or c1 % c2 */
669 if (tarval_is_one(tb))
670 return get_mode_null(get_irn_mode(a));
671 if (ta != tarval_bad && tb != tarval_bad)
672 return tarval_mod(ta, tb);
677 * Return the value of a Proj(Div).
679 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
681 long proj_nr = get_Proj_proj(n);
682 if (proj_nr != pn_Div_res)
685 return do_computed_value_Div(get_Proj_pred(n));
689 * Return the value of a Proj(Mod).
691 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
693 long proj_nr = get_Proj_proj(n);
695 if (proj_nr == pn_Mod_res) {
696 const ir_node *mod = get_Proj_pred(n);
697 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
703 * Return the value of a Proj.
705 static ir_tarval *computed_value_Proj(const ir_node *proj)
707 ir_node *n = get_Proj_pred(proj);
709 if (n->op->ops.computed_value_Proj != NULL)
710 return n->op->ops.computed_value_Proj(proj);
715 * If the parameter n can be computed, return its value, else tarval_bad.
716 * Performs constant folding.
718 * @param n The node this should be evaluated
720 ir_tarval *computed_value(const ir_node *n)
722 vrp_attr *vrp = vrp_get_info(n);
723 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
724 return vrp->bits_set;
726 if (n->op->ops.computed_value)
727 return n->op->ops.computed_value(n);
731 void firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
735 ops->computed_value = computed_value_##a; \
737 #define CASE_PROJ(a) \
739 ops->computed_value_Proj = computed_value_Proj_##a; \
775 * Optimize operations that are commutative and have neutral 0,
776 * so a op 0 = 0 op a = a.
778 static ir_node *equivalent_node_neutral_zero(ir_node *n)
782 ir_node *a = get_binop_left(n);
783 ir_node *b = get_binop_right(n);
788 /* After running compute_node there is only one constant predecessor.
789 Find this predecessors value and remember the other node: */
790 if ((tv = value_of(a)) != tarval_bad) {
792 } else if ((tv = value_of(b)) != tarval_bad) {
797 /* If this predecessors constant value is zero, the operation is
798 * unnecessary. Remove it.
800 * Beware: If n is a Add, the mode of on and n might be different
801 * which happens in this rare construction: NULL + 3.
802 * Then, a Conv would be needed which we cannot include here.
804 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
807 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
814 * Eor is commutative and has neutral 0.
816 static ir_node *equivalent_node_Eor(ir_node *n)
822 n = equivalent_node_neutral_zero(n);
823 if (n != oldn) return n;
826 b = get_Eor_right(n);
828 if (is_Eor(a) || is_Or_Eor_Add(a)) {
829 ir_node *aa = get_binop_left(a);
830 ir_node *ab = get_binop_right(a);
833 /* (a ^ b) ^ a -> b */
835 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
837 } else if (ab == b) {
838 /* (a ^ b) ^ b -> a */
840 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
844 if (is_Eor(b) || is_Or_Eor_Add(b)) {
845 ir_node *ba = get_binop_left(b);
846 ir_node *bb = get_binop_right(b);
849 /* a ^ (a ^ b) -> b */
851 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
853 } else if (bb == a) {
854 /* a ^ (b ^ a) -> b */
856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
864 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
866 * The second one looks strange, but this construct
867 * is used heavily in the LCC sources :-).
869 * Beware: The Mode of an Add may be different than the mode of its
870 * predecessors, so we could not return a predecessors in all cases.
872 static ir_node *equivalent_node_Add(ir_node *n)
875 ir_node *left, *right;
876 ir_mode *mode = get_irn_mode(n);
878 n = equivalent_node_neutral_zero(n);
882 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
883 if (mode_is_float(mode)) {
884 ir_graph *irg = get_irn_irg(n);
885 if (get_irg_fp_model(irg) & fp_strict_algebraic)
889 left = get_Add_left(n);
890 right = get_Add_right(n);
893 if (get_Sub_right(left) == right) {
896 n = get_Sub_left(left);
897 if (mode == get_irn_mode(n)) {
898 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
904 if (get_Sub_right(right) == left) {
907 n = get_Sub_left(right);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
918 * optimize operations that are not commutative but have neutral 0 on left,
921 static ir_node *equivalent_node_left_zero(ir_node *n)
925 ir_node *a = get_binop_left(n);
926 ir_node *b = get_binop_right(n);
927 ir_tarval *tb = value_of(b);
929 if (tarval_is_null(tb)) {
932 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
937 #define equivalent_node_Shl equivalent_node_left_zero
938 #define equivalent_node_Shr equivalent_node_left_zero
939 #define equivalent_node_Shrs equivalent_node_left_zero
940 #define equivalent_node_Rotl equivalent_node_left_zero
943 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
945 * The second one looks strange, but this construct
946 * is used heavily in the LCC sources :-).
948 * Beware: The Mode of a Sub may be different than the mode of its
949 * predecessors, so we could not return a predecessors in all cases.
951 static ir_node *equivalent_node_Sub(ir_node *n)
955 ir_mode *mode = get_irn_mode(n);
958 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
959 if (mode_is_float(mode)) {
960 ir_graph *irg = get_irn_irg(n);
961 if (get_irg_fp_model(irg) & fp_strict_algebraic)
965 b = get_Sub_right(n);
968 /* Beware: modes might be different */
969 if (tarval_is_null(tb)) {
970 ir_node *a = get_Sub_left(n);
971 if (mode == get_irn_mode(a)) {
974 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
982 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
985 * -(-a) == a, but might overflow two times.
986 * We handle it anyway here but the better way would be a
987 * flag. This would be needed for Pascal for instance.
989 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
992 ir_node *pred = get_unop_op(n);
994 /* optimize symmetric unop */
995 if (get_irn_op(pred) == get_irn_op(n)) {
996 n = get_unop_op(pred);
997 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1002 /** Optimize Not(Not(x)) == x. */
1003 #define equivalent_node_Not equivalent_node_idempotent_unop
1005 /** -(-x) == x ??? Is this possible or can --x raise an
1006 out of bounds exception if min =! max? */
1007 #define equivalent_node_Minus equivalent_node_idempotent_unop
1010 * Optimize a * 1 = 1 * a = a.
1012 static ir_node *equivalent_node_Mul(ir_node *n)
1015 ir_node *a = get_Mul_left(n);
1017 /* we can handle here only the n * n = n bit cases */
1018 if (get_irn_mode(n) == get_irn_mode(a)) {
1019 ir_node *b = get_Mul_right(n);
1023 * Mul is commutative and has again an other neutral element.
1024 * Constants are place right, so check this case first.
1027 if (tarval_is_one(tv)) {
1029 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1032 if (tarval_is_one(tv)) {
1034 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1042 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1044 static ir_node *equivalent_node_Or(ir_node *n)
1048 ir_node *a = get_Or_left(n);
1049 ir_node *b = get_Or_right(n);
1053 n = a; /* idempotence */
1054 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1057 /* constants are normalized to right, check this side first */
1059 if (tarval_is_null(tv)) {
1061 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1065 if (tarval_is_null(tv)) {
1067 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1075 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1077 static ir_node *equivalent_node_And(ir_node *n)
1081 ir_node *a = get_And_left(n);
1082 ir_node *b = get_And_right(n);
1086 n = a; /* idempotence */
1087 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1090 /* constants are normalized to right, check this side first */
1092 if (tarval_is_all_one(tv)) {
1094 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1097 if (tv != get_tarval_bad()) {
1098 ir_mode *mode = get_irn_mode(n);
1099 if (!mode_is_signed(mode) && is_Conv(a)) {
1100 ir_node *convop = get_Conv_op(a);
1101 ir_mode *convopmode = get_irn_mode(convop);
1102 if (!mode_is_signed(convopmode)) {
1103 /* Check Conv(all_one) & Const = all_one */
1104 ir_tarval *one = get_mode_all_one(convopmode);
1105 ir_tarval *conv = tarval_convert_to(one, mode);
1106 ir_tarval *and = tarval_and(conv, tv);
1108 if (tarval_is_all_one(and)) {
1109 /* Conv(X) & Const = X */
1111 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1118 if (tarval_is_all_one(tv)) {
1120 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1124 if ((is_Or(a) || is_Or_Eor_Add(a))
1125 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1127 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1131 if ((is_Or(b) || is_Or_Eor_Add(b))
1132 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1141 * Try to remove useless Conv's:
1143 static ir_node *equivalent_node_Conv(ir_node *n)
1146 ir_node *a = get_Conv_op(n);
1148 ir_mode *n_mode = get_irn_mode(n);
1149 ir_mode *a_mode = get_irn_mode(a);
1152 if (n_mode == a_mode) { /* No Conv necessary */
1153 if (get_Conv_strict(n)) {
1156 /* neither Minus nor Confirm change the precision,
1157 so we can "look-through" */
1160 p = get_Minus_op(p);
1161 } else if (is_Confirm(p)) {
1162 p = get_Confirm_value(p);
1168 if (is_Conv(p) && get_Conv_strict(p)) {
1169 /* we known already, that a_mode == n_mode, and neither
1170 Minus change the mode, so the second Conv
1172 assert(get_irn_mode(p) == n_mode);
1174 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1178 ir_node *pred = get_Proj_pred(p);
1179 if (is_Load(pred)) {
1180 /* Loads always return with the exact precision of n_mode */
1181 assert(get_Load_mode(pred) == n_mode);
1183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1186 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1187 pred = get_Proj_pred(pred);
1188 if (is_Start(pred)) {
1189 /* Arguments always return with the exact precision,
1190 as strictConv's are place before Call -- if the
1191 caller was compiled with the same setting.
1192 Otherwise, the semantics is probably still right. */
1193 assert(get_irn_mode(p) == n_mode);
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1201 /* special case: the immediate predecessor is also a Conv */
1202 if (! get_Conv_strict(a)) {
1203 /* first one is not strict, kick it */
1205 a_mode = get_irn_mode(a);
1209 /* else both are strict conv, second is superfluous */
1211 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1216 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1219 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1220 ir_node *b = get_Conv_op(a);
1221 ir_mode *b_mode = get_irn_mode(b);
1223 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1224 /* both are strict conv */
1225 if (smaller_mode(a_mode, n_mode)) {
1226 /* both are strict, but the first is smaller, so
1227 the second cannot remove more precision, remove the
1229 set_Conv_strict(n, 0);
1232 if (n_mode == b_mode) {
1233 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1234 if (n_mode == mode_b) {
1235 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1236 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1238 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1239 if (values_in_mode(b_mode, a_mode)) {
1240 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1241 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1246 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1247 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1248 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1249 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1251 if (float_mantissa >= int_mantissa) {
1253 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1258 if (smaller_mode(b_mode, a_mode)) {
1259 if (get_Conv_strict(n))
1260 set_Conv_strict(b, 1);
1261 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1262 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1272 * - fold Phi-nodes, iff they have only one predecessor except
1275 static ir_node *equivalent_node_Phi(ir_node *n)
1280 ir_node *first_val = NULL; /* to shutup gcc */
1282 if (!get_opt_optimize() &&
1283 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1286 n_preds = get_Phi_n_preds(n);
1288 /* Phi of dead Region without predecessors. */
1292 /* Find first non-self-referencing input */
1293 for (i = 0; i < n_preds; ++i) {
1294 first_val = get_Phi_pred(n, i);
1295 /* not self pointer */
1296 if (first_val != n) {
1297 /* then found first value. */
1302 /* search for rest of inputs, determine if any of these
1303 are non-self-referencing */
1304 while (++i < n_preds) {
1305 ir_node *scnd_val = get_Phi_pred(n, i);
1306 if (scnd_val != n && scnd_val != first_val) {
1311 if (i >= n_preds && !is_Dummy(first_val)) {
1312 /* Fold, if no multiple distinct non-self-referencing inputs */
1314 DBG_OPT_PHI(oldn, n);
1320 * Optimize Proj(Tuple).
1322 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1324 ir_node *oldn = proj;
1325 ir_node *tuple = get_Proj_pred(proj);
1327 /* Remove the Tuple/Proj combination. */
1328 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1329 DBG_OPT_TUPLE(oldn, tuple, proj);
1335 * Optimize a / 1 = a.
1337 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1339 ir_node *oldn = proj;
1340 ir_node *div = get_Proj_pred(proj);
1341 ir_node *b = get_Div_right(div);
1342 ir_tarval *tb = value_of(b);
1344 /* Div is not commutative. */
1345 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1346 switch (get_Proj_proj(proj)) {
1348 proj = get_Div_mem(div);
1349 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1353 proj = get_Div_left(div);
1354 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1358 /* we cannot replace the exception Proj's here, this is done in
1359 transform_node_Proj_Div() */
1367 * Optimize CopyB(mem, x, x) into a Nop.
1369 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1371 ir_node *oldn = proj;
1372 ir_node *copyb = get_Proj_pred(proj);
1373 ir_node *a = get_CopyB_dst(copyb);
1374 ir_node *b = get_CopyB_src(copyb);
1377 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1378 switch (get_Proj_proj(proj)) {
1380 proj = get_CopyB_mem(copyb);
1381 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1389 * Optimize Bounds(idx, idx, upper) into idx.
1391 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1393 ir_node *oldn = proj;
1394 ir_node *bound = get_Proj_pred(proj);
1395 ir_node *idx = get_Bound_index(bound);
1396 ir_node *pred = skip_Proj(idx);
1399 if (idx == get_Bound_lower(bound))
1401 else if (is_Bound(pred)) {
1403 * idx was Bounds checked previously, it is still valid if
1404 * lower <= pred_lower && pred_upper <= upper.
1406 ir_node *lower = get_Bound_lower(bound);
1407 ir_node *upper = get_Bound_upper(bound);
1408 if (get_Bound_lower(pred) == lower &&
1409 get_Bound_upper(pred) == upper) {
1411 * One could expect that we simply return the previous
1412 * Bound here. However, this would be wrong, as we could
1413 * add an exception Proj to a new location then.
1414 * So, we must turn in into a tuple.
1420 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1421 switch (get_Proj_proj(proj)) {
1423 DBG_OPT_EXC_REM(proj);
1424 proj = get_Bound_mem(bound);
1428 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1431 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1439 * Does all optimizations on nodes that must be done on its Projs
1440 * because of creating new nodes.
1442 static ir_node *equivalent_node_Proj(ir_node *proj)
1444 ir_node *n = get_Proj_pred(proj);
1445 if (n->op->ops.equivalent_node_Proj)
1446 return n->op->ops.equivalent_node_Proj(proj);
1453 static ir_node *equivalent_node_Id(ir_node *n)
1461 DBG_OPT_ID(oldn, n);
1468 static ir_node *equivalent_node_Mux(ir_node *n)
1470 ir_node *oldn = n, *sel = get_Mux_sel(n);
1472 ir_tarval *ts = value_of(sel);
1474 if (ts == tarval_bad && is_Cmp(sel)) {
1475 /* try again with a direct call to compute_cmp, as we don't care
1476 * about the MODEB_LOWERED flag here */
1477 ts = compute_cmp(sel);
1480 /* Mux(true, f, t) == t */
1481 if (ts == tarval_b_true) {
1482 n = get_Mux_true(n);
1483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1486 /* Mux(false, f, t) == f */
1487 if (ts == tarval_b_false) {
1488 n = get_Mux_false(n);
1489 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1492 n_t = get_Mux_true(n);
1493 n_f = get_Mux_false(n);
1495 /* Mux(v, x, T) == x */
1496 if (is_Unknown(n_f)) {
1498 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1501 /* Mux(v, T, x) == x */
1502 if (is_Unknown(n_t)) {
1504 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1508 /* Mux(v, x, x) == x */
1511 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1514 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1515 ir_relation relation = get_Cmp_relation(sel);
1516 ir_node *f = get_Mux_false(n);
1517 ir_node *t = get_Mux_true(n);
1520 * Note further that these optimization work even for floating point
1521 * with NaN's because -NaN == NaN.
1522 * However, if +0 and -0 is handled differently, we cannot use the first one.
1524 ir_node *const cmp_l = get_Cmp_left(sel);
1525 ir_node *const cmp_r = get_Cmp_right(sel);
1528 case ir_relation_equal:
1529 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1530 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1537 case ir_relation_less_greater:
1538 case ir_relation_unordered_less_greater:
1539 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1540 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1542 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1551 * Note: normalization puts the constant on the right side,
1552 * so we check only one case.
1554 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1555 /* Mux(t CMP 0, X, t) */
1556 if (is_Minus(f) && get_Minus_op(f) == t) {
1557 /* Mux(t CMP 0, -t, t) */
1558 if (relation == ir_relation_equal) {
1559 /* Mux(t == 0, -t, t) ==> -t */
1561 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1562 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1563 /* Mux(t != 0, -t, t) ==> t */
1565 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1575 * Remove Confirm nodes if setting is on.
1576 * Replace Confirms(x, '=', Constlike) by Constlike.
1578 static ir_node *equivalent_node_Confirm(ir_node *n)
1580 ir_node *pred = get_Confirm_value(n);
1581 ir_relation relation = get_Confirm_relation(n);
1583 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1585 * rare case: two identical Confirms one after another,
1586 * replace the second one with the first.
1589 pred = get_Confirm_value(n);
1595 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1596 * perform no actual computation, as, e.g., the Id nodes. It does not create
1597 * new nodes. It is therefore safe to free n if the node returned is not n.
1598 * If a node returns a Tuple we can not just skip it. If the size of the
1599 * in array fits, we transform n into a tuple (e.g., Div).
1601 ir_node *equivalent_node(ir_node *n)
1603 if (n->op->ops.equivalent_node)
1604 return n->op->ops.equivalent_node(n);
1608 void firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1612 ops->equivalent_node = equivalent_node_##a; \
1614 #define CASE_PROJ(a) \
1616 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1651 * Returns non-zero if a node is a Phi node
1652 * with all predecessors constant.
1654 static int is_const_Phi(ir_node *n)
1658 if (! is_Phi(n) || get_irn_arity(n) == 0)
1660 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1661 if (! is_Const(get_irn_n(n, i)))
1667 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1668 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1671 * in reality eval_func should be tarval (*eval_func)() but incomplete
1672 * declarations are bad style and generate noisy warnings
1674 typedef void (*eval_func)(void);
1677 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1679 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1681 if (eval == (eval_func) tarval_sub) {
1682 tarval_sub_type func = (tarval_sub_type)eval;
1684 return func(a, b, mode);
1686 tarval_binop_type func = (tarval_binop_type)eval;
1693 * Apply an evaluator on a binop with a constant operators (and one Phi).
1695 * @param phi the Phi node
1696 * @param other the other operand
1697 * @param eval an evaluator function
1698 * @param mode the mode of the result, may be different from the mode of the Phi!
1699 * @param left if non-zero, other is the left operand, else the right
1701 * @return a new Phi node if the conversion was successful, NULL else
1703 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1709 int i, n = get_irn_arity(phi);
1711 NEW_ARR_A(void *, res, n);
1713 for (i = 0; i < n; ++i) {
1714 pred = get_irn_n(phi, i);
1715 tv = get_Const_tarval(pred);
1716 tv = do_eval(eval, other, tv, mode);
1718 if (tv == tarval_bad) {
1719 /* folding failed, bad */
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = do_eval(eval, tv, other, mode);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1737 irg = get_irn_irg(phi);
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1742 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1746 * Apply an evaluator on a binop with two constant Phi.
1748 * @param a the left Phi node
1749 * @param b the right Phi node
1750 * @param eval an evaluator function
1751 * @param mode the mode of the result, may be different from the mode of the Phi!
1753 * @return a new Phi node if the conversion was successful, NULL else
1755 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1757 ir_tarval *tv_l, *tv_r, *tv;
1763 if (get_nodes_block(a) != get_nodes_block(b))
1766 n = get_irn_arity(a);
1767 NEW_ARR_A(void *, res, n);
1769 for (i = 0; i < n; ++i) {
1770 pred = get_irn_n(a, i);
1771 tv_l = get_Const_tarval(pred);
1772 pred = get_irn_n(b, i);
1773 tv_r = get_Const_tarval(pred);
1774 tv = do_eval(eval, tv_l, tv_r, mode);
1776 if (tv == tarval_bad) {
1777 /* folding failed, bad */
1782 irg = get_irn_irg(a);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(a, i);
1785 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1787 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1791 * Apply an evaluator on a unop with a constant operator (a Phi).
1793 * @param phi the Phi node
1794 * @param eval an evaluator function
1796 * @return a new Phi node if the conversion was successful, NULL else
1798 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1805 int i, n = get_irn_arity(phi);
1807 NEW_ARR_A(void *, res, n);
1808 for (i = 0; i < n; ++i) {
1809 pred = get_irn_n(phi, i);
1810 tv = get_Const_tarval(pred);
1813 if (tv == tarval_bad) {
1814 /* folding failed, bad */
1819 mode = get_irn_mode(phi);
1820 irg = get_irn_irg(phi);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1825 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1829 * Apply a conversion on a constant operator (a Phi).
1831 * @param phi the Phi node
1833 * @return a new Phi node if the conversion was successful, NULL else
1835 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1841 int i, n = get_irn_arity(phi);
1843 NEW_ARR_A(void *, res, n);
1844 for (i = 0; i < n; ++i) {
1845 pred = get_irn_n(phi, i);
1846 tv = get_Const_tarval(pred);
1847 tv = tarval_convert_to(tv, mode);
1849 if (tv == tarval_bad) {
1850 /* folding failed, bad */
1855 irg = get_irn_irg(phi);
1856 for (i = 0; i < n; ++i) {
1857 pred = get_irn_n(phi, i);
1858 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1860 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1864 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1865 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1866 * If possible, remove the Conv's.
1868 static ir_node *transform_node_AddSub(ir_node *n)
1870 ir_mode *mode = get_irn_mode(n);
1872 if (mode_is_reference(mode)) {
1873 ir_node *left = get_binop_left(n);
1874 ir_node *right = get_binop_right(n);
1875 unsigned ref_bits = get_mode_size_bits(mode);
1877 if (is_Conv(left)) {
1878 ir_mode *lmode = get_irn_mode(left);
1879 unsigned bits = get_mode_size_bits(lmode);
1881 if (ref_bits == bits &&
1882 mode_is_int(lmode) &&
1883 get_mode_arithmetic(lmode) == irma_twos_complement) {
1884 ir_node *pre = get_Conv_op(left);
1885 ir_mode *pre_mode = get_irn_mode(pre);
1887 if (mode_is_int(pre_mode) &&
1888 get_mode_size_bits(pre_mode) == bits &&
1889 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1890 /* ok, this conv just changes to sign, moreover the calculation
1891 * is done with same number of bits as our address mode, so
1892 * we can ignore the conv as address calculation can be viewed
1893 * as either signed or unsigned
1895 set_binop_left(n, pre);
1900 if (is_Conv(right)) {
1901 ir_mode *rmode = get_irn_mode(right);
1902 unsigned bits = get_mode_size_bits(rmode);
1904 if (ref_bits == bits &&
1905 mode_is_int(rmode) &&
1906 get_mode_arithmetic(rmode) == irma_twos_complement) {
1907 ir_node *pre = get_Conv_op(right);
1908 ir_mode *pre_mode = get_irn_mode(pre);
1910 if (mode_is_int(pre_mode) &&
1911 get_mode_size_bits(pre_mode) == bits &&
1912 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1913 /* ok, this conv just changes to sign, moreover the calculation
1914 * is done with same number of bits as our address mode, so
1915 * we can ignore the conv as address calculation can be viewed
1916 * as either signed or unsigned
1918 set_binop_right(n, pre);
1923 /* let address arithmetic use unsigned modes */
1924 if (is_Const(right)) {
1925 ir_mode *rmode = get_irn_mode(right);
1927 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1928 /* convert a AddP(P, *s) into AddP(P, *u) */
1929 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1931 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1932 set_binop_right(n, pre);
1940 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1943 if (is_Const(b) && is_const_Phi(a)) { \
1944 /* check for Op(Phi, Const) */ \
1945 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1947 else if (is_Const(a) && is_const_Phi(b)) { \
1948 /* check for Op(Const, Phi) */ \
1949 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1951 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1952 /* check for Op(Phi, Phi) */ \
1953 c = apply_binop_on_2_phis(a, b, eval, mode); \
1956 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1961 #define HANDLE_UNOP_PHI(eval, a, c) \
1964 if (is_const_Phi(a)) { \
1965 /* check for Op(Phi) */ \
1966 c = apply_unop_on_phi(a, eval); \
1968 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1975 * Create a 0 constant of given mode.
1977 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1979 ir_tarval *tv = get_mode_null(mode);
1980 ir_node *cnst = new_r_Const(irg, tv);
1985 static bool is_shiftop(const ir_node *n)
1987 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1990 /* the order of the values is important! */
1991 typedef enum const_class {
1997 static const_class classify_const(const ir_node* n)
1999 if (is_Const(n)) return const_const;
2000 if (is_irn_constlike(n)) return const_like;
2005 * Determines whether r is more constlike or has a larger index (in that order)
2008 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2010 const const_class l_order = classify_const(l);
2011 const const_class r_order = classify_const(r);
2013 l_order > r_order ||
2014 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2017 static bool is_cmp_unequal(const ir_node *node)
2019 ir_relation relation = get_Cmp_relation(node);
2020 ir_node *left = get_Cmp_left(node);
2021 ir_node *right = get_Cmp_right(node);
2022 ir_mode *mode = get_irn_mode(left);
2024 if (relation == ir_relation_less_greater)
2027 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2028 return relation == ir_relation_greater;
2033 * returns true for Cmp(x == 0) or Cmp(x != 0)
2035 static bool is_cmp_equality_zero(const ir_node *node)
2037 ir_relation relation;
2038 ir_node *right = get_Cmp_right(node);
2040 if (!is_Const(right) || !is_Const_null(right))
2042 relation = get_Cmp_relation(node);
2043 return relation == ir_relation_equal
2044 || relation == ir_relation_less_greater
2045 || (!mode_is_signed(get_irn_mode(right))
2046 && relation == ir_relation_greater);
2050 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2051 * Such pattern may arise in bitfield stores.
2053 * value c4 value c4 & c2
2054 * AND c3 AND c1 | c3
2061 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2064 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2066 ir_node *irn_and, *c1;
2068 ir_node *and_l, *c3;
2069 ir_node *value, *c4;
2070 ir_node *new_and, *new_const, *block;
2071 ir_mode *mode = get_irn_mode(irn_or);
2073 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2077 irn_and = get_binop_left(irn_or);
2078 c1 = get_binop_right(irn_or);
2079 if (!is_Const(c1) || !is_And(irn_and))
2082 or_l = get_binop_left(irn_and);
2083 c2 = get_binop_right(irn_and);
2087 tv1 = get_Const_tarval(c1);
2088 tv2 = get_Const_tarval(c2);
2090 tv = tarval_or(tv1, tv2);
2091 if (tarval_is_all_one(tv)) {
2092 /* the AND does NOT clear a bit with isn't set by the OR */
2093 set_binop_left(irn_or, or_l);
2094 set_binop_right(irn_or, c1);
2096 /* check for more */
2100 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2103 and_l = get_binop_left(or_l);
2104 c3 = get_binop_right(or_l);
2105 if (!is_Const(c3) || !is_And(and_l))
2108 value = get_binop_left(and_l);
2109 c4 = get_binop_right(and_l);
2113 /* ok, found the pattern, check for conditions */
2114 assert(mode == get_irn_mode(irn_and));
2115 assert(mode == get_irn_mode(or_l));
2116 assert(mode == get_irn_mode(and_l));
2118 tv3 = get_Const_tarval(c3);
2119 tv4 = get_Const_tarval(c4);
2121 tv = tarval_or(tv4, tv2);
2122 if (!tarval_is_all_one(tv)) {
2123 /* have at least one 0 at the same bit position */
2127 if (tv3 != tarval_andnot(tv3, tv4)) {
2128 /* bit in the or_mask is outside the and_mask */
2132 if (tv1 != tarval_andnot(tv1, tv2)) {
2133 /* bit in the or_mask is outside the and_mask */
2137 /* ok, all conditions met */
2138 block = get_irn_n(irn_or, -1);
2139 irg = get_irn_irg(block);
2141 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2143 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2145 set_binop_left(irn_or, new_and);
2146 set_binop_right(irn_or, new_const);
2148 /* check for more */
2153 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2155 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2157 ir_mode *mode = get_irn_mode(irn_or);
2158 ir_node *shl, *shr, *block;
2159 ir_node *irn, *x, *c1, *c2, *n;
2160 ir_tarval *tv1, *tv2;
2162 /* some backends can't handle rotl */
2163 if (!be_get_backend_param()->support_rotl)
2166 if (! mode_is_int(mode))
2169 shl = get_binop_left(irn_or);
2170 shr = get_binop_right(irn_or);
2179 } else if (!is_Shl(shl)) {
2181 } else if (!is_Shr(shr)) {
2184 x = get_Shl_left(shl);
2185 if (x != get_Shr_left(shr))
2188 c1 = get_Shl_right(shl);
2189 c2 = get_Shr_right(shr);
2190 if (is_Const(c1) && is_Const(c2)) {
2191 tv1 = get_Const_tarval(c1);
2192 if (! tarval_is_long(tv1))
2195 tv2 = get_Const_tarval(c2);
2196 if (! tarval_is_long(tv2))
2199 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2200 != (int) get_mode_size_bits(mode))
2203 /* yet, condition met */
2204 block = get_nodes_block(irn_or);
2206 n = new_r_Rotl(block, x, c1, mode);
2208 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2212 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2213 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2214 if (!ir_is_negated_value(c1, c2)) {
2218 /* yet, condition met */
2219 block = get_nodes_block(irn_or);
2220 n = new_r_Rotl(block, x, c1, mode);
2221 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2226 * Prototype of a recursive transform function
2227 * for bitwise distributive transformations.
2229 typedef ir_node* (*recursive_transform)(ir_node *n);
2232 * makes use of distributive laws for and, or, eor
2233 * and(a OP c, b OP c) -> and(a, b) OP c
2234 * note, might return a different op than n
2236 static ir_node *transform_bitwise_distributive(ir_node *n,
2237 recursive_transform trans_func)
2240 ir_node *a = get_binop_left(n);
2241 ir_node *b = get_binop_right(n);
2242 ir_op *op = get_irn_op(a);
2243 ir_op *op_root = get_irn_op(n);
2245 if (op != get_irn_op(b))
2248 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2249 if (op == op_Conv) {
2250 ir_node *a_op = get_Conv_op(a);
2251 ir_node *b_op = get_Conv_op(b);
2252 ir_mode *a_mode = get_irn_mode(a_op);
2253 ir_mode *b_mode = get_irn_mode(b_op);
2254 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2255 ir_node *blk = get_nodes_block(n);
2258 set_binop_left(n, a_op);
2259 set_binop_right(n, b_op);
2260 set_irn_mode(n, a_mode);
2262 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2264 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2270 /* nothing to gain here */
2274 if (op == op_Shrs || op == op_Shr || op == op_Shl
2275 || op == op_And || op == op_Or || op == op_Eor) {
2276 ir_node *a_left = get_binop_left(a);
2277 ir_node *a_right = get_binop_right(a);
2278 ir_node *b_left = get_binop_left(b);
2279 ir_node *b_right = get_binop_right(b);
2281 ir_node *op1 = NULL;
2282 ir_node *op2 = NULL;
2284 if (is_op_commutative(op)) {
2285 if (a_left == b_left) {
2289 } else if (a_left == b_right) {
2293 } else if (a_right == b_left) {
2299 if (a_right == b_right) {
2306 /* (a sop c) & (b sop c) => (a & b) sop c */
2307 ir_node *blk = get_nodes_block(n);
2309 ir_node *new_n = exact_copy(n);
2310 set_binop_left(new_n, op1);
2311 set_binop_right(new_n, op2);
2312 new_n = trans_func(new_n);
2314 if (op_root == op_Eor && op == op_Or) {
2315 dbg_info *dbgi = get_irn_dbg_info(n);
2316 ir_mode *mode = get_irn_mode(c);
2318 c = new_rd_Not(dbgi, blk, c, mode);
2319 n = new_rd_And(dbgi, blk, new_n, c, mode);
2322 set_nodes_block(n, blk);
2323 set_binop_left(n, new_n);
2324 set_binop_right(n, c);
2328 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2337 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2339 * - and, or, xor instead of &
2340 * - Shl, Shr, Shrs, rotl instead of >>
2341 * (with a special case for Or/Xor + Shrs)
2343 * This normalisation is usually good for the backend since << C can often be
2344 * matched as address-mode.
2346 static ir_node *transform_node_bitop_shift(ir_node *n)
2348 ir_graph *irg = get_irn_irg(n);
2349 ir_node *left = get_binop_left(n);
2350 ir_node *right = get_binop_right(n);
2351 ir_mode *mode = get_irn_mode(n);
2352 ir_node *shift_left;
2353 ir_node *shift_right;
2355 dbg_info *dbg_bitop;
2356 dbg_info *dbg_shift;
2362 ir_tarval *tv_bitop;
2364 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2367 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2368 if (!is_Const(right) || !is_shiftop(left))
2371 shift_left = get_binop_left(left);
2372 shift_right = get_binop_right(left);
2373 if (!is_Const(shift_right))
2376 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2377 if (is_Shrs(left)) {
2378 /* TODO this could be improved */
2382 irg = get_irn_irg(n);
2383 block = get_nodes_block(n);
2384 dbg_bitop = get_irn_dbg_info(n);
2385 dbg_shift = get_irn_dbg_info(left);
2386 tv1 = get_Const_tarval(shift_right);
2387 tv2 = get_Const_tarval(right);
2388 assert(get_tarval_mode(tv2) == mode);
2391 tv_bitop = tarval_shr(tv2, tv1);
2393 /* Check whether we have lost some bits during the right shift. */
2395 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2397 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2400 } else if (is_Shr(left)) {
2403 * TODO this can be improved by checking whether
2404 * the left shift produces an overflow
2408 tv_bitop = tarval_shl(tv2, tv1);
2410 assert(is_Rotl(left));
2411 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2413 new_const = new_r_Const(irg, tv_bitop);
2416 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2417 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2418 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2421 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2425 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2426 } else if (is_Shr(left)) {
2427 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2429 assert(is_Rotl(left));
2430 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2436 static bool complement_values(const ir_node *a, const ir_node *b)
2438 if (is_Not(a) && get_Not_op(a) == b)
2440 if (is_Not(b) && get_Not_op(b) == a)
2442 if (is_Const(a) && is_Const(b)) {
2443 ir_tarval *tv_a = get_Const_tarval(a);
2444 ir_tarval *tv_b = get_Const_tarval(b);
2445 return tarval_not(tv_a) == tv_b;
2450 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2453 * for associative operations fold:
2454 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2455 * This is a "light" version of the reassociation phase
2457 static ir_node *fold_constant_associativity(ir_node *node,
2458 tv_fold_binop_func fold)
2463 ir_node *right = get_binop_right(node);
2464 ir_node *left_right;
2471 if (!is_Const(right))
2474 op = get_irn_op(node);
2475 left = get_binop_left(node);
2476 if (get_irn_op(left) != op)
2479 left_right = get_binop_right(left);
2480 if (!is_Const(left_right))
2483 left_left = get_binop_left(left);
2484 c0 = get_Const_tarval(left_right);
2485 c1 = get_Const_tarval(right);
2486 irg = get_irn_irg(node);
2487 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2489 new_c = fold(c0, c1);
2490 if (new_c == tarval_bad)
2492 new_const = new_r_Const(irg, new_c);
2493 new_node = exact_copy(node);
2494 set_binop_left(new_node, left_left);
2495 set_binop_right(new_node, new_const);
2502 static ir_node *transform_node_Or_(ir_node *n)
2505 ir_node *a = get_binop_left(n);
2506 ir_node *b = get_binop_right(n);
2510 n = fold_constant_associativity(n, tarval_or);
2514 if (is_Not(a) && is_Not(b)) {
2515 /* ~a | ~b = ~(a&b) */
2516 ir_node *block = get_nodes_block(n);
2518 mode = get_irn_mode(n);
2521 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2522 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2523 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2527 /* we can combine the relations of two compares with the same operands */
2528 if (is_Cmp(a) && is_Cmp(b)) {
2529 ir_node *a_left = get_Cmp_left(a);
2530 ir_node *a_right = get_Cmp_right(a);
2531 ir_node *b_left = get_Cmp_left(b);
2532 ir_node *b_right = get_Cmp_right(b);
2533 if (a_left == b_left && b_left == b_right) {
2534 dbg_info *dbgi = get_irn_dbg_info(n);
2535 ir_node *block = get_nodes_block(n);
2536 ir_relation a_relation = get_Cmp_relation(a);
2537 ir_relation b_relation = get_Cmp_relation(b);
2538 ir_relation new_relation = a_relation | b_relation;
2539 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2541 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2542 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2543 && !mode_is_float(get_irn_mode(a_left))
2544 && !mode_is_float(get_irn_mode(b_left))) {
2545 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2546 ir_graph *irg = get_irn_irg(n);
2547 dbg_info *dbgi = get_irn_dbg_info(n);
2548 ir_node *block = get_nodes_block(n);
2549 ir_mode *a_mode = get_irn_mode(a_left);
2550 ir_mode *b_mode = get_irn_mode(b_left);
2551 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2552 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2553 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2554 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2555 ir_node *zero = create_zero_const(irg, b_mode);
2556 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2558 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2559 ir_graph *irg = get_irn_irg(n);
2560 dbg_info *dbgi = get_irn_dbg_info(n);
2561 ir_node *block = get_nodes_block(n);
2562 ir_mode *a_mode = get_irn_mode(a_left);
2563 ir_mode *b_mode = get_irn_mode(b_left);
2564 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2565 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2566 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2567 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2568 ir_node *zero = create_zero_const(irg, a_mode);
2569 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2574 mode = get_irn_mode(n);
2575 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2577 n = transform_node_Or_bf_store(n);
2580 n = transform_node_Or_Rotl(n);
2584 n = transform_bitwise_distributive(n, transform_node_Or_);
2587 n = transform_node_bitop_shift(n);
2594 static ir_node *transform_node_Or(ir_node *n)
2596 if (is_Or_Eor_Add(n)) {
2597 dbg_info *dbgi = get_irn_dbg_info(n);
2598 ir_node *block = get_nodes_block(n);
2599 ir_node *left = get_Or_left(n);
2600 ir_node *right = get_Or_right(n);
2601 ir_mode *mode = get_irn_mode(n);
2602 return new_rd_Add(dbgi, block, left, right, mode);
2604 return transform_node_Or_(n);
2610 static ir_node *transform_node_Eor_(ir_node *n)
2613 ir_node *a = get_binop_left(n);
2614 ir_node *b = get_binop_right(n);
2615 ir_mode *mode = get_irn_mode(n);
2618 n = fold_constant_associativity(n, tarval_eor);
2622 /* we can combine the relations of two compares with the same operands */
2623 if (is_Cmp(a) && is_Cmp(b)) {
2624 ir_node *a_left = get_Cmp_left(a);
2625 ir_node *a_right = get_Cmp_left(a);
2626 ir_node *b_left = get_Cmp_left(b);
2627 ir_node *b_right = get_Cmp_right(b);
2628 if (a_left == b_left && b_left == b_right) {
2629 dbg_info *dbgi = get_irn_dbg_info(n);
2630 ir_node *block = get_nodes_block(n);
2631 ir_relation a_relation = get_Cmp_relation(a);
2632 ir_relation b_relation = get_Cmp_relation(b);
2633 ir_relation new_relation = a_relation ^ b_relation;
2634 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2638 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2640 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2641 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2642 dbg_info *dbg = get_irn_dbg_info(n);
2643 ir_node *block = get_nodes_block(n);
2644 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2645 ir_node *new_left = get_Not_op(a);
2646 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2647 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2649 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2650 dbg_info *dbg = get_irn_dbg_info(n);
2651 ir_node *block = get_nodes_block(n);
2652 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2653 ir_node *new_right = get_Not_op(b);
2654 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2655 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2659 /* x ^ 1...1 -> ~1 */
2660 if (is_Const(b) && is_Const_all_one(b)) {
2661 n = new_r_Not(get_nodes_block(n), a, mode);
2662 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2666 n = transform_bitwise_distributive(n, transform_node_Eor_);
2669 n = transform_node_bitop_shift(n);
2676 static ir_node *transform_node_Eor(ir_node *n)
2678 if (is_Or_Eor_Add(n)) {
2679 dbg_info *dbgi = get_irn_dbg_info(n);
2680 ir_node *block = get_nodes_block(n);
2681 ir_node *left = get_Eor_left(n);
2682 ir_node *right = get_Eor_right(n);
2683 ir_mode *mode = get_irn_mode(n);
2684 return new_rd_Add(dbgi, block, left, right, mode);
2686 return transform_node_Eor_(n);
2690 * Do the AddSub optimization, then Transform
2691 * Constant folding on Phi
2692 * Add(a,a) -> Mul(a, 2)
2693 * Add(Mul(a, x), a) -> Mul(a, x+1)
2694 * if the mode is integer or float.
2695 * Transform Add(a,-b) into Sub(a,b).
2696 * Reassociation might fold this further.
2698 static ir_node *transform_node_Add(ir_node *n)
2706 n = fold_constant_associativity(n, tarval_add);
2710 n = transform_node_AddSub(n);
2714 a = get_Add_left(n);
2715 b = get_Add_right(n);
2716 mode = get_irn_mode(n);
2718 if (mode_is_reference(mode)) {
2719 ir_mode *lmode = get_irn_mode(a);
2721 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2722 /* an Add(a, NULL) is a hidden Conv */
2723 dbg_info *dbg = get_irn_dbg_info(n);
2724 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2728 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2729 ir_tarval *tv = get_Const_tarval(b);
2730 ir_tarval *min = get_mode_min(mode);
2731 /* if all bits are set, then this has the same effect as a Not.
2732 * Note that the following == gives false for different modes which
2733 * is exactly what we want */
2735 dbg_info *dbgi = get_irn_dbg_info(n);
2736 ir_graph *irg = get_irn_irg(n);
2737 ir_node *block = get_nodes_block(n);
2738 ir_node *cnst = new_r_Const(irg, min);
2739 return new_rd_Eor(dbgi, block, a, cnst, mode);
2743 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2745 /* for FP the following optimizations are only allowed if
2746 * fp_strict_algebraic is disabled */
2747 if (mode_is_float(mode)) {
2748 ir_graph *irg = get_irn_irg(n);
2749 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2753 if (mode_is_num(mode)) {
2754 ir_graph *irg = get_irn_irg(n);
2755 /* the following code leads to endless recursion when Mul are replaced
2756 * by a simple instruction chain */
2757 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2758 && a == b && mode_is_int(mode)) {
2759 ir_node *block = get_nodes_block(n);
2762 get_irn_dbg_info(n),
2765 new_r_Const_long(irg, mode, 2),
2767 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2772 get_irn_dbg_info(n),
2777 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2782 get_irn_dbg_info(n),
2787 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2790 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2791 /* Here we rely on constants be on the RIGHT side */
2793 ir_node *op = get_Not_op(a);
2795 if (is_Const(b) && is_Const_one(b)) {
2797 ir_node *blk = get_nodes_block(n);
2798 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2799 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2806 if (is_Or_Eor_Add(n)) {
2807 n = transform_node_Or_(n);
2810 n = transform_node_Eor_(n);
2819 * returns -cnst or NULL if impossible
2821 static ir_node *const_negate(ir_node *cnst)
2823 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2824 dbg_info *dbgi = get_irn_dbg_info(cnst);
2825 ir_graph *irg = get_irn_irg(cnst);
2826 if (tv == tarval_bad) return NULL;
2827 return new_rd_Const(dbgi, irg, tv);
2831 * Do the AddSub optimization, then Transform
2832 * Constant folding on Phi
2833 * Sub(0,a) -> Minus(a)
2834 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2835 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2836 * Sub(Add(a, x), x) -> a
2837 * Sub(x, Add(x, a)) -> -a
2838 * Sub(x, Const) -> Add(x, -Const)
2840 static ir_node *transform_node_Sub(ir_node *n)
2846 n = transform_node_AddSub(n);
2848 a = get_Sub_left(n);
2849 b = get_Sub_right(n);
2851 mode = get_irn_mode(n);
2853 if (mode_is_int(mode)) {
2854 ir_mode *lmode = get_irn_mode(a);
2856 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2857 /* a Sub(a, NULL) is a hidden Conv */
2858 dbg_info *dbg = get_irn_dbg_info(n);
2859 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2860 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2864 if (mode == lmode &&
2865 get_mode_arithmetic(mode) == irma_twos_complement &&
2867 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2869 dbg_info *dbg = get_irn_dbg_info(n);
2870 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2871 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2877 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2879 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2880 if (mode_is_float(mode)) {
2881 ir_graph *irg = get_irn_irg(n);
2882 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2886 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2887 /* a - C -> a + (-C) */
2888 ir_node *cnst = const_negate(b);
2890 ir_node *block = get_nodes_block(n);
2891 dbg_info *dbgi = get_irn_dbg_info(n);
2893 n = new_rd_Add(dbgi, block, a, cnst, mode);
2894 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2899 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2900 dbg_info *dbg = get_irn_dbg_info(n);
2901 ir_node *block = get_nodes_block(n);
2902 ir_node *left = get_Minus_op(a);
2903 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2905 n = new_rd_Minus(dbg, block, add, mode);
2906 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2908 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2909 dbg_info *dbg = get_irn_dbg_info(n);
2910 ir_node *block = get_nodes_block(n);
2911 ir_node *right = get_Minus_op(b);
2913 n = new_rd_Add(dbg, block, a, right, mode);
2914 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2916 } else if (is_Sub(b)) {
2917 /* a - (b - c) -> a + (c - b)
2918 * -> (a - b) + c iff (b - c) is a pointer */
2919 dbg_info *s_dbg = get_irn_dbg_info(b);
2920 ir_node *s_left = get_Sub_left(b);
2921 ir_node *s_right = get_Sub_right(b);
2922 ir_mode *s_mode = get_irn_mode(b);
2923 if (mode_is_reference(s_mode)) {
2924 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2925 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2926 dbg_info *a_dbg = get_irn_dbg_info(n);
2929 s_right = new_r_Conv(lowest_block, s_right, mode);
2930 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2932 ir_node *s_block = get_nodes_block(b);
2933 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2934 dbg_info *a_dbg = get_irn_dbg_info(n);
2935 ir_node *a_block = get_nodes_block(n);
2937 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2942 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2943 ir_node *m_right = get_Mul_right(b);
2944 if (is_Const(m_right)) {
2945 ir_node *cnst2 = const_negate(m_right);
2946 if (cnst2 != NULL) {
2947 dbg_info *m_dbg = get_irn_dbg_info(b);
2948 ir_node *m_block = get_nodes_block(b);
2949 ir_node *m_left = get_Mul_left(b);
2950 ir_mode *m_mode = get_irn_mode(b);
2951 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2952 dbg_info *a_dbg = get_irn_dbg_info(n);
2953 ir_node *a_block = get_nodes_block(n);
2955 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2956 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2963 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2964 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2966 get_irn_dbg_info(n),
2970 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2973 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2974 ir_node *left = get_binop_left(a);
2975 ir_node *right = get_binop_right(a);
2977 /* FIXME: Does the Conv's work only for two complement or generally? */
2979 if (mode != get_irn_mode(right)) {
2980 /* This Sub is an effective Cast */
2981 right = new_r_Conv(get_nodes_block(n), right, mode);
2984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2986 } else if (right == b) {
2987 if (mode != get_irn_mode(left)) {
2988 /* This Sub is an effective Cast */
2989 left = new_r_Conv(get_nodes_block(n), left, mode);
2992 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2996 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2997 ir_node *left = get_binop_left(b);
2998 ir_node *right = get_binop_right(b);
3000 /* FIXME: Does the Conv's work only for two complement or generally? */
3002 ir_mode *r_mode = get_irn_mode(right);
3004 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3005 if (mode != r_mode) {
3006 /* This Sub is an effective Cast */
3007 n = new_r_Conv(get_nodes_block(n), n, mode);
3009 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3011 } else if (right == a) {
3012 ir_mode *l_mode = get_irn_mode(left);
3014 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3015 if (mode != l_mode) {
3016 /* This Sub is an effective Cast */
3017 n = new_r_Conv(get_nodes_block(n), n, mode);
3019 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3023 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3024 ir_mode *mode = get_irn_mode(a);
3026 if (mode == get_irn_mode(b)) {
3028 ir_node *op_a = get_Conv_op(a);
3029 ir_node *op_b = get_Conv_op(b);
3031 /* check if it's allowed to skip the conv */
3032 ma = get_irn_mode(op_a);
3033 mb = get_irn_mode(op_b);
3035 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3036 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3039 set_Sub_right(n, b);
3045 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3046 if (!is_reassoc_running() && is_Mul(a)) {
3047 ir_node *ma = get_Mul_left(a);
3048 ir_node *mb = get_Mul_right(a);
3051 ir_node *blk = get_nodes_block(n);
3052 ir_graph *irg = get_irn_irg(n);
3054 get_irn_dbg_info(n),
3058 get_irn_dbg_info(n),
3061 new_r_Const(irg, get_mode_one(mode)),
3064 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3066 } else if (mb == b) {
3067 ir_node *blk = get_nodes_block(n);
3068 ir_graph *irg = get_irn_irg(n);
3070 get_irn_dbg_info(n),
3074 get_irn_dbg_info(n),
3077 new_r_Const(irg, get_mode_one(mode)),
3080 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3084 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3085 ir_node *x = get_Sub_left(a);
3086 ir_node *y = get_Sub_right(a);
3087 ir_node *blk = get_nodes_block(n);
3088 ir_mode *m_b = get_irn_mode(b);
3089 ir_mode *m_y = get_irn_mode(y);
3093 /* Determine the right mode for the Add. */
3096 else if (mode_is_reference(m_b))
3098 else if (mode_is_reference(m_y))
3102 * Both modes are different but none is reference,
3103 * happens for instance in SubP(SubP(P, Iu), Is).
3104 * We have two possibilities here: Cast or ignore.
3105 * Currently we ignore this case.
3110 add = new_r_Add(blk, y, b, add_mode);
3112 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3113 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3117 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3118 /* c - ~X = X + (c+1) */
3119 if (is_Const(a) && is_Not(b)) {
3120 ir_tarval *tv = get_Const_tarval(a);
3122 tv = tarval_add(tv, get_mode_one(mode));
3123 if (tv != tarval_bad) {
3124 ir_node *blk = get_nodes_block(n);
3125 ir_graph *irg = get_irn_irg(n);
3126 ir_node *c = new_r_Const(irg, tv);
3127 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3128 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3132 /* x-(x&y) = x & ~y */
3134 ir_node *and_left = get_And_left(b);
3135 ir_node *and_right = get_And_right(b);
3136 if (and_right == a) {
3137 ir_node *tmp = and_left;
3138 and_left = and_right;
3141 if (and_left == a) {
3142 dbg_info *dbgi = get_irn_dbg_info(n);
3143 ir_node *block = get_nodes_block(n);
3144 ir_mode *mode = get_irn_mode(n);
3145 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3146 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3155 * Several transformation done on n*n=2n bits mul.
3156 * These transformations must be done here because new nodes may be produced.
3158 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3161 ir_node *a = get_Mul_left(n);
3162 ir_node *b = get_Mul_right(n);
3163 ir_tarval *ta = value_of(a);
3164 ir_tarval *tb = value_of(b);
3165 ir_mode *smode = get_irn_mode(a);
3167 if (ta == get_mode_one(smode)) {
3168 /* (L)1 * (L)b = (L)b */
3169 ir_node *blk = get_nodes_block(n);
3170 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3174 else if (ta == get_mode_minus_one(smode)) {
3175 /* (L)-1 * (L)b = (L)b */
3176 ir_node *blk = get_nodes_block(n);
3177 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3178 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3182 if (tb == get_mode_one(smode)) {
3183 /* (L)a * (L)1 = (L)a */
3184 ir_node *blk = get_irn_n(a, -1);
3185 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3186 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3189 else if (tb == get_mode_minus_one(smode)) {
3190 /* (L)a * (L)-1 = (L)-a */
3191 ir_node *blk = get_nodes_block(n);
3192 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3193 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3201 * Transform Mul(a,-1) into -a.
3202 * Do constant evaluation of Phi nodes.
3203 * Do architecture dependent optimizations on Mul nodes
3205 static ir_node *transform_node_Mul(ir_node *n)
3207 ir_node *c, *oldn = n;
3208 ir_mode *mode = get_irn_mode(n);
3209 ir_node *a = get_Mul_left(n);
3210 ir_node *b = get_Mul_right(n);
3212 n = fold_constant_associativity(n, tarval_mul);
3216 if (mode != get_irn_mode(a))
3217 return transform_node_Mul2n(n, mode);
3219 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3221 if (mode_is_signed(mode)) {
3224 if (value_of(a) == get_mode_minus_one(mode))
3226 else if (value_of(b) == get_mode_minus_one(mode))
3229 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3230 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3235 if (is_Const(b)) { /* (-a) * const -> a * -const */
3236 ir_node *cnst = const_negate(b);
3238 dbg_info *dbgi = get_irn_dbg_info(n);
3239 ir_node *block = get_nodes_block(n);
3240 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3241 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3244 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3245 dbg_info *dbgi = get_irn_dbg_info(n);
3246 ir_node *block = get_nodes_block(n);
3247 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3248 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3250 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3251 ir_node *sub_l = get_Sub_left(b);
3252 ir_node *sub_r = get_Sub_right(b);
3253 dbg_info *dbgi = get_irn_dbg_info(n);
3254 ir_node *block = get_nodes_block(n);
3255 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3256 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3257 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3260 } else if (is_Minus(b)) {
3261 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3262 ir_node *sub_l = get_Sub_left(a);
3263 ir_node *sub_r = get_Sub_right(a);
3264 dbg_info *dbgi = get_irn_dbg_info(n);
3265 ir_node *block = get_nodes_block(n);
3266 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3267 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3268 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3271 } else if (is_Shl(a)) {
3272 ir_node *const shl_l = get_Shl_left(a);
3273 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3274 /* (1 << x) * b -> b << x */
3275 dbg_info *const dbgi = get_irn_dbg_info(n);
3276 ir_node *const block = get_nodes_block(n);
3277 ir_node *const shl_r = get_Shl_right(a);
3278 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3279 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3282 } else if (is_Shl(b)) {
3283 ir_node *const shl_l = get_Shl_left(b);
3284 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3285 /* a * (1 << x) -> a << x */
3286 dbg_info *const dbgi = get_irn_dbg_info(n);
3287 ir_node *const block = get_nodes_block(n);
3288 ir_node *const shl_r = get_Shl_right(b);
3289 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3290 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3294 if (get_mode_arithmetic(mode) == irma_ieee754
3295 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3297 ir_tarval *tv = get_Const_tarval(a);
3298 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3299 && !tarval_is_negative(tv)) {
3300 /* 2.0 * b = b + b */
3301 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3302 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3306 else if (is_Const(b)) {
3307 ir_tarval *tv = get_Const_tarval(b);
3308 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3309 && !tarval_is_negative(tv)) {
3310 /* a * 2.0 = a + a */
3311 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3312 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3317 return arch_dep_replace_mul_with_shifts(n);
3321 * Transform a Div Node.
3323 static ir_node *transform_node_Div(ir_node *n)
3325 ir_mode *mode = get_Div_resmode(n);
3326 ir_node *a = get_Div_left(n);
3327 ir_node *b = get_Div_right(n);
3329 const ir_node *dummy;
3331 if (mode_is_int(mode)) {
3332 if (is_Const(b) && is_const_Phi(a)) {
3333 /* check for Div(Phi, Const) */
3334 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3336 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3339 } else if (is_Const(a) && is_const_Phi(b)) {
3340 /* check for Div(Const, Phi) */
3341 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3343 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3346 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3347 /* check for Div(Phi, Phi) */
3348 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3350 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3355 if (a == b && value_not_zero(a, &dummy)) {
3356 ir_graph *irg = get_irn_irg(n);
3357 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3358 value = new_r_Const(irg, get_mode_one(mode));
3359 DBG_OPT_CSTEVAL(n, value);
3362 if (mode_is_signed(mode) && is_Const(b)) {
3363 ir_tarval *tv = get_Const_tarval(b);
3365 if (tv == get_mode_minus_one(mode)) {
3367 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3368 DBG_OPT_CSTEVAL(n, value);
3372 /* Try architecture dependent optimization */
3373 value = arch_dep_replace_div_by_const(n);
3376 assert(mode_is_float(mode));
3378 /* Optimize x/c to x*(1/c) */
3379 if (get_mode_arithmetic(mode) == irma_ieee754) {
3380 ir_tarval *tv = value_of(b);
3382 if (tv != tarval_bad) {
3383 int rem = tarval_fp_ops_enabled();
3386 * Floating point constant folding might be disabled here to
3388 * However, as we check for exact result, doing it is safe.
3391 tarval_enable_fp_ops(1);
3392 tv = tarval_div(get_mode_one(mode), tv);
3393 tarval_enable_fp_ops(rem);
3395 /* Do the transformation if the result is either exact or we are
3396 not using strict rules. */
3397 if (tv != tarval_bad &&
3398 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3399 ir_node *block = get_nodes_block(n);
3400 ir_graph *irg = get_irn_irg(block);
3401 ir_node *c = new_r_Const(irg, tv);
3402 dbg_info *dbgi = get_irn_dbg_info(n);
3403 value = new_rd_Mul(dbgi, block, a, c, mode);
3416 /* Turn Div into a tuple (mem, jmp, bad, value) */
3417 mem = get_Div_mem(n);
3418 blk = get_nodes_block(n);
3419 irg = get_irn_irg(blk);
3421 /* skip a potential Pin */
3422 mem = skip_Pin(mem);
3423 turn_into_tuple(n, pn_Div_max+1);
3424 set_Tuple_pred(n, pn_Div_M, mem);
3425 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3426 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3427 set_Tuple_pred(n, pn_Div_res, value);
3433 * Transform a Mod node.
3435 static ir_node *transform_node_Mod(ir_node *n)
3437 ir_mode *mode = get_Mod_resmode(n);
3438 ir_node *a = get_Mod_left(n);
3439 ir_node *b = get_Mod_right(n);
3444 if (is_Const(b) && is_const_Phi(a)) {
3445 /* check for Div(Phi, Const) */
3446 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3448 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3452 else if (is_Const(a) && is_const_Phi(b)) {
3453 /* check for Div(Const, Phi) */
3454 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3456 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3460 else if (is_const_Phi(a) && is_const_Phi(b)) {
3461 /* check for Div(Phi, Phi) */
3462 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3464 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3471 irg = get_irn_irg(n);
3472 if (tv != tarval_bad) {
3473 value = new_r_Const(irg, tv);
3475 DBG_OPT_CSTEVAL(n, value);
3478 ir_node *a = get_Mod_left(n);
3479 ir_node *b = get_Mod_right(n);
3480 const ir_node *dummy;
3482 if (a == b && value_not_zero(a, &dummy)) {
3483 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3484 value = new_r_Const(irg, get_mode_null(mode));
3485 DBG_OPT_CSTEVAL(n, value);
3488 if (mode_is_signed(mode) && is_Const(b)) {
3489 ir_tarval *tv = get_Const_tarval(b);
3491 if (tv == get_mode_minus_one(mode)) {
3493 value = new_r_Const(irg, get_mode_null(mode));
3494 DBG_OPT_CSTEVAL(n, value);
3498 /* Try architecture dependent optimization */
3499 value = arch_dep_replace_mod_by_const(n);
3508 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3509 mem = get_Mod_mem(n);
3510 blk = get_nodes_block(n);
3511 irg = get_irn_irg(blk);
3513 /* skip a potential Pin */
3514 mem = skip_Pin(mem);
3515 turn_into_tuple(n, pn_Mod_max+1);
3516 set_Tuple_pred(n, pn_Mod_M, mem);
3517 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3518 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3519 set_Tuple_pred(n, pn_Mod_res, value);
3525 * Transform a Cond node.
3527 * Replace the Cond by a Jmp if it branches on a constant
3530 static ir_node *transform_node_Cond(ir_node *n)
3532 ir_node *a = get_Cond_selector(n);
3533 ir_graph *irg = get_irn_irg(n);
3537 /* we need block info which is not available in floating irgs */
3538 if (get_irg_pinned(irg) == op_pin_state_floats)
3542 if (ta == tarval_bad && is_Cmp(a)) {
3543 /* try again with a direct call to compute_cmp, as we don't care
3544 * about the MODEB_LOWERED flag here */
3545 ta = compute_cmp(a);
3548 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3549 /* It's a boolean Cond, branching on a boolean constant.
3550 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3551 ir_node *blk = get_nodes_block(n);
3552 jmp = new_r_Jmp(blk);
3553 turn_into_tuple(n, pn_Cond_max+1);
3554 if (ta == tarval_b_true) {
3555 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3556 set_Tuple_pred(n, pn_Cond_true, jmp);
3558 set_Tuple_pred(n, pn_Cond_false, jmp);
3559 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3561 /* We might generate an endless loop, so keep it alive. */
3562 add_End_keepalive(get_irg_end(irg), blk);
3563 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3568 static ir_node *transform_node_Switch(ir_node *n)
3570 ir_node *op = get_Switch_selector(n);
3571 ir_tarval *val = value_of(op);
3572 if (val != tarval_bad) {
3573 dbg_info *dbgi = get_irn_dbg_info(n);
3574 ir_graph *irg = get_irn_irg(n);
3575 unsigned n_outs = get_Switch_n_outs(n);
3576 ir_node *block = get_nodes_block(n);
3577 ir_node *bad = new_r_Bad(irg, mode_X);
3578 ir_node **in = XMALLOCN(ir_node*, n_outs);
3579 const ir_switch_table *table = get_Switch_table(n);
3580 size_t n_entries = ir_switch_table_get_n_entries(table);
3584 for (i = 0; i < n_entries; ++i) {
3585 const ir_switch_table_entry *entry
3586 = ir_switch_table_get_entry_const(table, i);
3587 ir_tarval *min = entry->min;
3588 ir_tarval *max = entry->max;
3591 if ((min == max && min == val)
3592 || (tarval_cmp(val, min) != ir_relation_less
3593 && tarval_cmp(val, max) != ir_relation_greater)) {
3598 for (o = 0; o < n_outs; ++o) {
3599 if (o == (unsigned)jmp_pn) {
3600 in[o] = new_rd_Jmp(dbgi, block);
3605 return new_r_Tuple(block, (int)n_outs, in);
3611 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3613 * - and, or, xor instead of &
3614 * - Shl, Shr, Shrs, rotl instead of >>
3615 * (with a special case for Or/Xor + Shrs)
3617 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3619 static ir_node *transform_node_shift_bitop(ir_node *n)
3621 ir_graph *irg = get_irn_irg(n);
3622 ir_node *right = get_binop_right(n);
3623 ir_mode *mode = get_irn_mode(n);
3625 ir_node *bitop_left;
3626 ir_node *bitop_right;
3635 ir_tarval *tv_shift;
3637 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3640 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3642 if (!is_Const(right))
3645 left = get_binop_left(n);
3646 op_left = get_irn_op(left);
3647 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3650 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3651 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3652 /* TODO: test if sign bit is affectes */
3656 bitop_right = get_binop_right(left);
3657 if (!is_Const(bitop_right))
3660 bitop_left = get_binop_left(left);
3662 block = get_nodes_block(n);
3663 dbgi = get_irn_dbg_info(n);
3664 tv1 = get_Const_tarval(bitop_right);
3665 tv2 = get_Const_tarval(right);
3667 assert(get_tarval_mode(tv1) == mode);
3670 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3671 tv_shift = tarval_shl(tv1, tv2);
3672 } else if (is_Shr(n)) {
3673 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3674 tv_shift = tarval_shr(tv1, tv2);
3675 } else if (is_Shrs(n)) {
3676 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3677 tv_shift = tarval_shrs(tv1, tv2);
3680 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3681 tv_shift = tarval_rotl(tv1, tv2);
3684 assert(get_tarval_mode(tv_shift) == mode);
3685 irg = get_irn_irg(n);
3686 new_const = new_r_Const(irg, tv_shift);
3688 if (op_left == op_And) {
3689 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3690 } else if (op_left == op_Or) {
3691 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3693 assert(op_left == op_Eor);
3694 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3703 static ir_node *transform_node_And(ir_node *n)
3705 ir_node *c, *oldn = n;
3706 ir_node *a = get_And_left(n);
3707 ir_node *b = get_And_right(n);
3710 n = fold_constant_associativity(n, tarval_and);
3714 if (is_Cmp(a) && is_Cmp(b)) {
3715 ir_node *a_left = get_Cmp_left(a);
3716 ir_node *a_right = get_Cmp_right(a);
3717 ir_node *b_left = get_Cmp_left(b);
3718 ir_node *b_right = get_Cmp_right(b);
3719 ir_relation a_relation = get_Cmp_relation(a);
3720 ir_relation b_relation = get_Cmp_relation(b);
3721 /* we can combine the relations of two compares with the same
3723 if (a_left == b_left && b_left == b_right) {
3724 dbg_info *dbgi = get_irn_dbg_info(n);
3725 ir_node *block = get_nodes_block(n);
3726 ir_relation new_relation = a_relation & b_relation;
3727 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3729 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3730 if (a_relation == b_relation && a_relation == ir_relation_equal
3731 && !mode_is_float(get_irn_mode(a_left))
3732 && !mode_is_float(get_irn_mode(b_left))) {
3733 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3734 dbg_info *dbgi = get_irn_dbg_info(n);
3735 ir_node *block = get_nodes_block(n);
3736 ir_mode *a_mode = get_irn_mode(a_left);
3737 ir_mode *b_mode = get_irn_mode(b_left);
3738 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3739 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3740 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3741 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3742 ir_graph *irg = get_irn_irg(n);
3743 ir_node *zero = create_zero_const(irg, b_mode);
3744 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3746 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3747 dbg_info *dbgi = get_irn_dbg_info(n);
3748 ir_node *block = get_nodes_block(n);
3749 ir_mode *a_mode = get_irn_mode(a_left);
3750 ir_mode *b_mode = get_irn_mode(b_left);
3751 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3752 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3753 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3754 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3755 ir_graph *irg = get_irn_irg(n);
3756 ir_node *zero = create_zero_const(irg, a_mode);
3757 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3762 mode = get_irn_mode(n);
3763 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3765 if (is_Or(a) || is_Or_Eor_Add(a)) {
3766 ir_node *or_left = get_binop_left(a);
3767 ir_node *or_right = get_binop_right(a);
3768 if (complement_values(or_left, b)) {
3769 /* (a|b) & ~a => b & ~a */
3770 dbg_info *dbgi = get_irn_dbg_info(n);
3771 ir_node *block = get_nodes_block(n);
3772 return new_rd_And(dbgi, block, or_right, b, mode);
3773 } else if (complement_values(or_right, b)) {
3774 /* (a|b) & ~b => a & ~b */
3775 dbg_info *dbgi = get_irn_dbg_info(n);
3776 ir_node *block = get_nodes_block(n);
3777 return new_rd_And(dbgi, block, or_left, b, mode);
3778 } else if (is_Not(b)) {
3779 ir_node *op = get_Not_op(b);
3781 ir_node *ba = get_And_left(op);
3782 ir_node *bb = get_And_right(op);
3784 /* it's enough to test the following cases due to normalization! */
3785 if (or_left == ba && or_right == bb) {
3786 /* (a|b) & ~(a&b) = a^b */
3787 ir_node *block = get_nodes_block(n);
3789 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3790 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3796 if (is_Or(b) || is_Or_Eor_Add(b)) {
3797 ir_node *or_left = get_binop_left(b);
3798 ir_node *or_right = get_binop_right(b);
3799 if (complement_values(or_left, a)) {
3800 /* (a|b) & ~a => b & ~a */
3801 dbg_info *dbgi = get_irn_dbg_info(n);
3802 ir_node *block = get_nodes_block(n);
3803 return new_rd_And(dbgi, block, or_right, a, mode);
3804 } else if (complement_values(or_right, a)) {
3805 /* (a|b) & ~b => a & ~b */
3806 dbg_info *dbgi = get_irn_dbg_info(n);
3807 ir_node *block = get_nodes_block(n);
3808 return new_rd_And(dbgi, block, or_left, a, mode);
3809 } else if (is_Not(a)) {
3810 ir_node *op = get_Not_op(a);
3812 ir_node *aa = get_And_left(op);
3813 ir_node *ab = get_And_right(op);
3815 /* it's enough to test the following cases due to normalization! */
3816 if (or_left == aa && or_right == ab) {
3817 /* (a|b) & ~(a&b) = a^b */
3818 ir_node *block = get_nodes_block(n);
3820 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3821 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3827 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3828 ir_node *al = get_binop_left(a);
3829 ir_node *ar = get_binop_right(a);
3832 /* (b ^ a) & b -> ~a & b */
3833 dbg_info *dbg = get_irn_dbg_info(n);
3834 ir_node *block = get_nodes_block(n);
3836 ar = new_rd_Not(dbg, block, ar, mode);
3837 n = new_rd_And(dbg, block, ar, b, mode);
3838 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3842 /* (a ^ b) & b -> ~a & b */
3843 dbg_info *dbg = get_irn_dbg_info(n);
3844 ir_node *block = get_nodes_block(n);
3846 al = new_rd_Not(dbg, block, al, mode);
3847 n = new_rd_And(dbg, block, al, b, mode);
3848 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3852 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3853 ir_node *bl = get_binop_left(b);
3854 ir_node *br = get_binop_right(b);
3857 /* a & (a ^ b) -> a & ~b */
3858 dbg_info *dbg = get_irn_dbg_info(n);
3859 ir_node *block = get_nodes_block(n);
3861 br = new_rd_Not(dbg, block, br, mode);
3862 n = new_rd_And(dbg, block, br, a, mode);
3863 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3867 /* a & (b ^ a) -> a & ~b */
3868 dbg_info *dbg = get_irn_dbg_info(n);
3869 ir_node *block = get_nodes_block(n);
3871 bl = new_rd_Not(dbg, block, bl, mode);
3872 n = new_rd_And(dbg, block, bl, a, mode);
3873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3877 if (is_Not(a) && is_Not(b)) {
3878 /* ~a & ~b = ~(a|b) */
3879 ir_node *block = get_nodes_block(n);
3880 ir_mode *mode = get_irn_mode(n);
3884 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3885 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3886 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3891 vrp_attr *b_vrp = vrp_get_info(b);
3892 ir_tarval *a_val = get_Const_tarval(a);
3893 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3899 vrp_attr *a_vrp = vrp_get_info(a);
3900 ir_tarval *b_val = get_Const_tarval(b);
3901 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3906 n = transform_bitwise_distributive(n, transform_node_And);
3908 n = transform_node_bitop_shift(n);
3916 static ir_node *transform_node_Not(ir_node *n)
3918 ir_node *c, *oldn = n;
3919 ir_node *a = get_Not_op(n);
3920 ir_mode *mode = get_irn_mode(n);
3922 HANDLE_UNOP_PHI(tarval_not,a,c);
3924 /* check for a boolean Not */
3926 dbg_info *dbgi = get_irn_dbg_info(a);
3927 ir_node *block = get_nodes_block(a);
3928 ir_relation relation = get_Cmp_relation(a);
3929 relation = get_negated_relation(relation);
3930 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3931 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3935 /* normalize ~(a ^ b) => a ^ ~b */
3936 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3937 dbg_info *dbg = get_irn_dbg_info(n);
3938 ir_node *block = get_nodes_block(n);
3939 ir_node *eor_right = get_binop_right(a);
3940 ir_node *eor_left = get_binop_left(a);
3941 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3942 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3946 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3947 if (is_Minus(a)) { /* ~-x -> x + -1 */
3948 dbg_info *dbg = get_irn_dbg_info(n);
3949 ir_graph *irg = get_irn_irg(n);
3950 ir_node *block = get_nodes_block(n);
3951 ir_node *add_l = get_Minus_op(a);
3952 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3953 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3954 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3955 ir_node *add_r = get_binop_right(a);
3956 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3957 /* ~(x + -1) = -x */
3958 ir_node *op = get_binop_left(a);
3959 ir_node *blk = get_nodes_block(n);
3960 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3961 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3969 * Transform a Minus.
3973 * -(a >>u (size-1)) = a >>s (size-1)
3974 * -(a >>s (size-1)) = a >>u (size-1)
3975 * -(a * const) -> a * -const
3977 static ir_node *transform_node_Minus(ir_node *n)
3979 ir_node *c, *oldn = n;
3980 ir_node *a = get_Minus_op(n);
3983 HANDLE_UNOP_PHI(tarval_neg,a,c);
3985 mode = get_irn_mode(a);
3986 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3987 /* the following rules are only to twos-complement */
3990 ir_node *op = get_Not_op(a);
3991 ir_tarval *tv = get_mode_one(mode);
3992 ir_node *blk = get_nodes_block(n);
3993 ir_graph *irg = get_irn_irg(blk);
3994 ir_node *c = new_r_Const(irg, tv);
3995 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3996 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4000 ir_node *c = get_Shr_right(a);
4003 ir_tarval *tv = get_Const_tarval(c);
4005 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4006 /* -(a >>u (size-1)) = a >>s (size-1) */
4007 ir_node *v = get_Shr_left(a);
4009 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4010 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4016 ir_node *c = get_Shrs_right(a);
4019 ir_tarval *tv = get_Const_tarval(c);
4021 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4022 /* -(a >>s (size-1)) = a >>u (size-1) */
4023 ir_node *v = get_Shrs_left(a);
4025 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4026 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4033 /* - (a-b) = b - a */
4034 ir_node *la = get_Sub_left(a);
4035 ir_node *ra = get_Sub_right(a);
4036 ir_node *blk = get_nodes_block(n);
4038 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4039 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4043 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4044 ir_node *mul_l = get_Mul_left(a);
4045 ir_node *mul_r = get_Mul_right(a);
4046 ir_tarval *tv = value_of(mul_r);
4047 if (tv != tarval_bad) {
4048 tv = tarval_neg(tv);
4049 if (tv != tarval_bad) {
4050 ir_graph *irg = get_irn_irg(n);
4051 ir_node *cnst = new_r_Const(irg, tv);
4052 dbg_info *dbg = get_irn_dbg_info(a);
4053 ir_node *block = get_nodes_block(a);
4054 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4055 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4065 * Transform a Proj(Load) with a non-null address.
4067 static ir_node *transform_node_Proj_Load(ir_node *proj)
4069 if (get_opt_ldst_only_null_ptr_exceptions()) {
4070 if (get_irn_mode(proj) == mode_X) {
4071 ir_node *load = get_Proj_pred(proj);
4073 /* get the Load address */
4074 const ir_node *addr = get_Load_ptr(load);
4075 const ir_node *confirm;
4077 if (value_not_null(addr, &confirm)) {
4078 if (confirm == NULL) {
4079 /* this node may float if it did not depend on a Confirm */
4080 set_irn_pinned(load, op_pin_state_floats);
4082 if (get_Proj_proj(proj) == pn_Load_X_except) {
4083 ir_graph *irg = get_irn_irg(proj);
4084 DBG_OPT_EXC_REM(proj);
4085 return new_r_Bad(irg, mode_X);
4087 ir_node *blk = get_nodes_block(load);
4088 return new_r_Jmp(blk);
4097 * Transform a Proj(Store) with a non-null address.
4099 static ir_node *transform_node_Proj_Store(ir_node *proj)
4101 if (get_opt_ldst_only_null_ptr_exceptions()) {
4102 if (get_irn_mode(proj) == mode_X) {
4103 ir_node *store = get_Proj_pred(proj);
4105 /* get the load/store address */
4106 const ir_node *addr = get_Store_ptr(store);
4107 const ir_node *confirm;
4109 if (value_not_null(addr, &confirm)) {
4110 if (confirm == NULL) {
4111 /* this node may float if it did not depend on a Confirm */
4112 set_irn_pinned(store, op_pin_state_floats);
4114 if (get_Proj_proj(proj) == pn_Store_X_except) {
4115 ir_graph *irg = get_irn_irg(proj);
4116 DBG_OPT_EXC_REM(proj);
4117 return new_r_Bad(irg, mode_X);
4119 ir_node *blk = get_nodes_block(store);
4120 return new_r_Jmp(blk);
4129 * Transform a Proj(Div) with a non-zero value.
4130 * Removes the exceptions and routes the memory to the NoMem node.
4132 static ir_node *transform_node_Proj_Div(ir_node *proj)
4134 ir_node *div = get_Proj_pred(proj);
4135 ir_node *b = get_Div_right(div);
4136 ir_node *res, *new_mem;
4137 const ir_node *confirm;
4140 if (value_not_zero(b, &confirm)) {
4141 /* div(x, y) && y != 0 */
4142 if (confirm == NULL) {
4143 /* we are sure we have a Const != 0 */
4144 new_mem = get_Div_mem(div);
4145 new_mem = skip_Pin(new_mem);
4146 set_Div_mem(div, new_mem);
4147 set_irn_pinned(div, op_pin_state_floats);
4150 proj_nr = get_Proj_proj(proj);
4152 case pn_Div_X_regular:
4153 return new_r_Jmp(get_nodes_block(div));
4155 case pn_Div_X_except: {
4156 ir_graph *irg = get_irn_irg(proj);
4157 /* we found an exception handler, remove it */
4158 DBG_OPT_EXC_REM(proj);
4159 return new_r_Bad(irg, mode_X);
4163 ir_graph *irg = get_irn_irg(proj);
4164 res = get_Div_mem(div);
4165 new_mem = get_irg_no_mem(irg);
4168 /* This node can only float up to the Confirm block */
4169 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4171 set_irn_pinned(div, op_pin_state_floats);
4172 /* this is a Div without exception, we can remove the memory edge */
4173 set_Div_mem(div, new_mem);
4182 * Transform a Proj(Mod) with a non-zero value.
4183 * Removes the exceptions and routes the memory to the NoMem node.
4185 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4187 ir_node *mod = get_Proj_pred(proj);
4188 ir_node *b = get_Mod_right(mod);
4189 ir_node *res, *new_mem;
4190 const ir_node *confirm;
4193 if (value_not_zero(b, &confirm)) {
4194 /* mod(x, y) && y != 0 */
4195 proj_nr = get_Proj_proj(proj);
4197 if (confirm == NULL) {
4198 /* we are sure we have a Const != 0 */
4199 new_mem = get_Mod_mem(mod);
4200 new_mem = skip_Pin(new_mem);
4201 set_Mod_mem(mod, new_mem);
4202 set_irn_pinned(mod, op_pin_state_floats);
4207 case pn_Mod_X_regular:
4208 return new_r_Jmp(get_irn_n(mod, -1));
4210 case pn_Mod_X_except: {
4211 ir_graph *irg = get_irn_irg(proj);
4212 /* we found an exception handler, remove it */
4213 DBG_OPT_EXC_REM(proj);
4214 return new_r_Bad(irg, mode_X);
4218 ir_graph *irg = get_irn_irg(proj);
4219 res = get_Mod_mem(mod);
4220 new_mem = get_irg_no_mem(irg);
4223 /* This node can only float up to the Confirm block */
4224 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4226 /* this is a Mod without exception, we can remove the memory edge */
4227 set_Mod_mem(mod, new_mem);
4231 if (get_Mod_left(mod) == b) {
4232 /* a % a = 0 if a != 0 */
4233 ir_graph *irg = get_irn_irg(proj);
4234 ir_mode *mode = get_irn_mode(proj);
4235 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4237 DBG_OPT_CSTEVAL(mod, res);
4246 * return true if the operation returns a value with exactly 1 bit set
4248 static bool is_single_bit(const ir_node *node)
4250 /* a first implementation, could be extended with vrp and others... */
4252 ir_node *shl_l = get_Shl_left(node);
4253 ir_mode *mode = get_irn_mode(node);
4254 int modulo = get_mode_modulo_shift(mode);
4255 /* this works if we shift a 1 and we have modulo shift */
4256 if (is_Const(shl_l) && is_Const_one(shl_l)
4257 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4260 } else if (is_Const(node)) {
4261 ir_tarval *tv = get_Const_tarval(node);
4262 return tarval_is_single_bit(tv);
4268 * checks if node just flips a bit in another node and returns that other node
4269 * if so. @p tv should be a value having just 1 bit set
4271 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4274 return get_Not_op(node);
4276 ir_node *right = get_Eor_right(node);
4277 if (is_Const(right)) {
4278 ir_tarval *right_tv = get_Const_tarval(right);
4279 ir_mode *mode = get_irn_mode(node);
4280 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4281 return get_Eor_left(node);
4288 * Normalizes and optimizes Cmp nodes.
4290 static ir_node *transform_node_Cmp(ir_node *n)
4292 ir_node *left = get_Cmp_left(n);
4293 ir_node *right = get_Cmp_right(n);
4294 ir_mode *mode = get_irn_mode(left);
4295 ir_tarval *tv = NULL;
4296 bool changed = false;
4297 bool changedc = false;
4298 ir_relation relation = get_Cmp_relation(n);
4299 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4301 /* mask out impossible relations */
4302 ir_relation new_relation = relation & possible;
4303 if (new_relation != relation) {
4304 relation = new_relation;
4308 /* Remove unnecessary conversions */
4309 if (is_Conv(left) && is_Conv(right)) {
4310 ir_node *op_left = get_Conv_op(left);
4311 ir_node *op_right = get_Conv_op(right);
4312 ir_mode *mode_left = get_irn_mode(op_left);
4313 ir_mode *mode_right = get_irn_mode(op_right);
4315 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4316 && mode_left != mode_b && mode_right != mode_b) {
4317 ir_node *block = get_nodes_block(n);
4319 if (mode_left == mode_right) {
4323 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4324 } else if (smaller_mode(mode_left, mode_right)) {
4325 left = new_r_Conv(block, op_left, mode_right);
4328 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4329 } else if (smaller_mode(mode_right, mode_left)) {
4331 right = new_r_Conv(block, op_right, mode_left);
4333 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4335 mode = get_irn_mode(left);
4338 if (is_Conv(left) && is_Const(right)) {
4339 ir_node *op_left = get_Conv_op(left);
4340 ir_mode *mode_left = get_irn_mode(op_left);
4341 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4342 ir_tarval *tv = get_Const_tarval(right);
4343 tarval_int_overflow_mode_t last_mode
4344 = tarval_get_integer_overflow_mode();
4346 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4347 new_tv = tarval_convert_to(tv, mode_left);
4348 tarval_set_integer_overflow_mode(last_mode);
4349 if (new_tv != tarval_bad) {
4350 ir_graph *irg = get_irn_irg(n);
4352 right = new_r_Const(irg, new_tv);
4353 mode = get_irn_mode(left);
4355 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4361 * Optimize -a CMP -b into b CMP a.
4362 * This works only for modes where unary Minus cannot Overflow.
4363 * Note that two-complement integers can Overflow so it will NOT work.
4365 if (!mode_overflow_on_unary_Minus(mode) &&
4366 is_Minus(left) && is_Minus(right)) {
4367 left = get_Minus_op(left);
4368 right = get_Minus_op(right);
4369 relation = get_inversed_relation(relation);
4371 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4374 /* remove operation on both sides if possible */
4375 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4377 * The following operations are NOT safe for floating point operations, for instance
4378 * 1.0 + inf == 2.0 + inf, =/=> x == y
4380 if (mode_is_int(mode)) {
4381 unsigned lop = get_irn_opcode(left);
4383 if (lop == get_irn_opcode(right)) {
4384 ir_node *ll, *lr, *rl, *rr;
4386 /* same operation on both sides, try to remove */
4390 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4391 left = get_unop_op(left);
4392 right = get_unop_op(right);
4394 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4397 ll = get_Add_left(left);
4398 lr = get_Add_right(left);
4399 rl = get_Add_left(right);
4400 rr = get_Add_right(right);
4403 /* X + a CMP X + b ==> a CMP b */
4407 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4408 } else if (ll == rr) {
4409 /* X + a CMP b + X ==> a CMP b */
4413 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4414 } else if (lr == rl) {
4415 /* a + X CMP X + b ==> a CMP b */
4419 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4420 } else if (lr == rr) {
4421 /* a + X CMP b + X ==> a CMP b */
4425 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4429 ll = get_Sub_left(left);
4430 lr = get_Sub_right(left);
4431 rl = get_Sub_left(right);
4432 rr = get_Sub_right(right);
4435 /* X - a CMP X - b ==> a CMP b */
4439 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4440 } else if (lr == rr) {
4441 /* a - X CMP b - X ==> a CMP b */
4445 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4449 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4450 /* a ROTL X CMP b ROTL X ==> a CMP b */
4451 left = get_Rotl_left(left);
4452 right = get_Rotl_left(right);
4454 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4462 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4463 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4464 ir_node *ll = get_binop_left(left);
4465 ir_node *lr = get_binop_right(left);
4467 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4473 ir_graph *irg = get_irn_irg(n);
4475 right = create_zero_const(irg, mode);
4477 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4480 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4481 ir_node *rl = get_binop_left(right);
4482 ir_node *rr = get_binop_right(right);
4484 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4490 ir_graph *irg = get_irn_irg(n);
4492 right = create_zero_const(irg, mode);
4494 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4498 if (is_And(left) && is_Const(right)) {
4499 ir_node *ll = get_binop_left(left);
4500 ir_node *lr = get_binop_right(left);
4501 if (is_Shr(ll) && is_Const(lr)) {
4502 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4503 ir_node *block = get_nodes_block(n);
4504 ir_mode *mode = get_irn_mode(left);
4506 ir_node *llr = get_Shr_right(ll);
4507 if (is_Const(llr)) {
4508 dbg_info *dbg = get_irn_dbg_info(left);
4509 ir_graph *irg = get_irn_irg(left);
4511 ir_tarval *c1 = get_Const_tarval(llr);
4512 ir_tarval *c2 = get_Const_tarval(lr);
4513 ir_tarval *c3 = get_Const_tarval(right);
4514 ir_tarval *mask = tarval_shl(c2, c1);
4515 ir_tarval *value = tarval_shl(c3, c1);
4517 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4518 right = new_r_Const(irg, value);
4523 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4525 if (is_Const(right) && is_Const_null(right) &&
4526 (is_Eor(left) || is_Or_Eor_Add(left))) {
4527 right = get_Eor_right(left);
4528 left = get_Eor_left(left);
4534 if (mode_is_int(mode) && is_And(left)) {
4535 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4536 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4537 if (relation == ir_relation_equal
4538 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4539 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4540 ir_node *and0 = get_And_left(left);
4541 ir_node *and1 = get_And_right(left);
4542 if (and1 == right) {
4543 ir_node *tmp = and0;
4547 if (and0 == right && is_single_bit(and0)) {
4548 ir_graph *irg = get_irn_irg(n);
4550 relation == ir_relation_equal ? ir_relation_less_greater
4551 : ir_relation_equal;
4552 right = create_zero_const(irg, mode);
4558 if (is_Const(right) && is_Const_null(right) &&
4559 (relation == ir_relation_equal
4560 || (relation == ir_relation_less_greater)
4561 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4563 /* instead of flipping the bit before the bit-test operation negate
4565 ir_node *and0 = get_And_left(left);
4566 ir_node *and1 = get_And_right(left);
4567 if (is_Const(and1)) {
4568 ir_tarval *tv = get_Const_tarval(and1);
4569 if (tarval_is_single_bit(tv)) {
4570 ir_node *flipped = flips_bit(and0, tv);
4571 if (flipped != NULL) {
4572 dbg_info *dbgi = get_irn_dbg_info(left);
4573 ir_node *block = get_nodes_block(left);
4574 relation = get_negated_relation(relation);
4575 left = new_rd_And(dbgi, block, flipped, and1, mode);
4584 /* replace mode_b compares with ands/ors */
4585 if (mode == mode_b) {
4586 ir_node *block = get_nodes_block(n);
4590 case ir_relation_less_equal:
4591 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4593 case ir_relation_less:
4594 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4596 case ir_relation_greater_equal:
4597 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4599 case ir_relation_greater:
4600 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4602 case ir_relation_less_greater:
4603 bres = new_r_Eor(block, left, right, mode_b);
4605 case ir_relation_equal:
4606 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4609 #ifdef DEBUG_libfirm
4610 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4615 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4621 * First step: normalize the compare op
4622 * by placing the constant on the right side
4623 * or moving the lower address node to the left.
4625 if (!operands_are_normalized(left, right)) {
4630 relation = get_inversed_relation(relation);
4635 * Second step: Try to reduce the magnitude
4636 * of a constant. This may help to generate better code
4637 * later and may help to normalize more compares.
4638 * Of course this is only possible for integer values.
4640 tv = value_of(right);
4641 if (tv != tarval_bad) {
4642 ir_mode *mode = get_irn_mode(right);
4644 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4645 * cmp(ct,c2) | cmp(cf,c2) | result
4646 * -----------|------------|--------
4647 * true | true | True
4648 * false | false | False
4650 * false | true | not(x)
4653 ir_node *mux_true = get_Mux_true(left);
4654 ir_node *mux_false = get_Mux_false(left);
4655 if (is_Const(mux_true) && is_Const(mux_false)) {
4656 /* we can fold true/false constant separately */
4657 ir_tarval *tv_true = get_Const_tarval(mux_true);
4658 ir_tarval *tv_false = get_Const_tarval(mux_false);
4659 ir_relation r_true = tarval_cmp(tv_true, tv);
4660 ir_relation r_false = tarval_cmp(tv_false, tv);
4661 if (r_true != ir_relation_false
4662 || r_false != ir_relation_false) {
4663 bool rel_true = (r_true & relation) != 0;
4664 bool rel_false = (r_false & relation) != 0;
4665 ir_node *cond = get_Mux_sel(left);
4666 if (rel_true == rel_false) {
4667 relation = rel_true ? ir_relation_true
4668 : ir_relation_false;
4669 } else if (rel_true) {
4672 dbg_info *dbgi = get_irn_dbg_info(n);
4673 ir_node *block = get_nodes_block(n);
4674 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4681 /* TODO extend to arbitrary constants */
4682 if (is_Conv(left) && tarval_is_null(tv)) {
4683 ir_node *op = get_Conv_op(left);
4684 ir_mode *op_mode = get_irn_mode(op);
4687 * UpConv(x) REL 0 ==> x REL 0
4688 * Don't do this for float values as it's unclear whether it is a
4689 * win. (on the other side it makes detection/creation of fabs hard)
4691 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4692 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4693 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4694 !mode_is_float(mode)) {
4695 tv = get_mode_null(op_mode);
4699 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4703 if (tv != tarval_bad) {
4704 /* the following optimization is possible on modes without Overflow
4705 * on Unary Minus or on == and !=:
4706 * -a CMP c ==> a swap(CMP) -c
4708 * Beware: for two-complement Overflow may occur, so only == and != can
4709 * be optimized, see this:
4710 * -MININT < 0 =/=> MININT > 0 !!!
4712 if (is_Minus(left) &&
4713 (!mode_overflow_on_unary_Minus(mode) ||
4714 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4715 tv = tarval_neg(tv);
4717 if (tv != tarval_bad) {
4718 left = get_Minus_op(left);
4719 relation = get_inversed_relation(relation);
4721 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4723 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4724 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4725 tv = tarval_not(tv);
4727 if (tv != tarval_bad) {
4728 left = get_Not_op(left);
4730 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4734 /* for integer modes, we have more */
4735 if (mode_is_int(mode) && !is_Const(left)) {
4736 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4737 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4738 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4739 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4741 if (tv != tarval_bad) {
4742 relation ^= ir_relation_equal;
4744 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4747 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4748 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4749 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4750 tv = tarval_add(tv, get_mode_one(mode));
4752 if (tv != tarval_bad) {
4753 relation ^= ir_relation_equal;
4755 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4759 /* the following reassociations work only for == and != */
4760 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4761 if (tv != tarval_bad) {
4762 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4764 ir_node *c1 = get_Sub_right(left);
4765 ir_tarval *tv2 = value_of(c1);
4767 if (tv2 != tarval_bad) {
4768 tv2 = tarval_add(tv, value_of(c1));
4770 if (tv2 != tarval_bad) {
4771 left = get_Sub_left(left);
4774 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4778 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4779 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4780 ir_node *a_l = get_binop_left(left);
4781 ir_node *a_r = get_binop_right(left);
4785 if (is_Const(a_l)) {
4787 tv2 = value_of(a_l);
4790 tv2 = value_of(a_r);
4793 if (tv2 != tarval_bad) {
4794 tv2 = tarval_sub(tv, tv2, NULL);
4796 if (tv2 != tarval_bad) {
4800 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4804 /* -a == c ==> a == -c, -a != c ==> a != -c */
4805 else if (is_Minus(left)) {
4806 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4808 if (tv2 != tarval_bad) {
4809 left = get_Minus_op(left);
4812 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4819 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4820 switch (get_irn_opcode(left)) {
4824 c1 = get_And_right(left);
4827 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4828 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4830 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4832 /* TODO: move to constant evaluation */
4833 ir_graph *irg = get_irn_irg(n);
4834 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4835 c1 = new_r_Const(irg, tv);
4836 DBG_OPT_CSTEVAL(n, c1);
4840 if (tarval_is_single_bit(tv)) {
4842 * optimization for AND:
4844 * And(x, C) == C ==> And(x, C) != 0
4845 * And(x, C) != C ==> And(X, C) == 0
4847 * if C is a single Bit constant.
4850 /* check for Constant's match. We have check hare the tarvals,
4851 because our const might be changed */
4852 if (get_Const_tarval(c1) == tv) {
4853 /* fine: do the transformation */
4854 tv = get_mode_null(get_tarval_mode(tv));
4855 relation ^= ir_relation_less_equal_greater;
4857 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4863 c1 = get_Or_right(left);
4864 if (is_Const(c1) && tarval_is_null(tv)) {
4866 * Or(x, C) == 0 && C != 0 ==> FALSE
4867 * Or(x, C) != 0 && C != 0 ==> TRUE
4869 if (! tarval_is_null(get_Const_tarval(c1))) {
4870 /* TODO: move to constant evaluation */
4871 ir_graph *irg = get_irn_irg(n);
4872 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4873 c1 = new_r_Const(irg, tv);
4874 DBG_OPT_CSTEVAL(n, c1);
4881 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4883 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4886 c1 = get_Shl_right(left);
4888 ir_graph *irg = get_irn_irg(c1);
4889 ir_tarval *tv1 = get_Const_tarval(c1);
4890 ir_mode *mode = get_irn_mode(left);
4891 ir_tarval *minus1 = get_mode_all_one(mode);
4892 ir_tarval *amask = tarval_shr(minus1, tv1);
4893 ir_tarval *cmask = tarval_shl(minus1, tv1);
4896 if (tarval_and(tv, cmask) != tv) {
4897 /* condition not met */
4898 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4899 c1 = new_r_Const(irg, tv);
4900 DBG_OPT_CSTEVAL(n, c1);
4903 sl = get_Shl_left(left);
4904 blk = get_nodes_block(n);
4905 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4906 tv = tarval_shr(tv, tv1);
4908 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4913 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4915 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4918 c1 = get_Shr_right(left);
4920 ir_graph *irg = get_irn_irg(c1);
4921 ir_tarval *tv1 = get_Const_tarval(c1);
4922 ir_mode *mode = get_irn_mode(left);
4923 ir_tarval *minus1 = get_mode_all_one(mode);
4924 ir_tarval *amask = tarval_shl(minus1, tv1);
4925 ir_tarval *cmask = tarval_shr(minus1, tv1);
4928 if (tarval_and(tv, cmask) != tv) {
4929 /* condition not met */
4930 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4931 c1 = new_r_Const(irg, tv);
4932 DBG_OPT_CSTEVAL(n, c1);
4935 sl = get_Shr_left(left);
4936 blk = get_nodes_block(n);
4937 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4938 tv = tarval_shl(tv, tv1);
4940 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4945 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4947 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4950 c1 = get_Shrs_right(left);
4952 ir_graph *irg = get_irn_irg(c1);
4953 ir_tarval *tv1 = get_Const_tarval(c1);
4954 ir_mode *mode = get_irn_mode(left);
4955 ir_tarval *minus1 = get_mode_all_one(mode);
4956 ir_tarval *amask = tarval_shl(minus1, tv1);
4957 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4960 cond = tarval_sub(cond, tv1, NULL);
4961 cond = tarval_shrs(tv, cond);
4963 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4964 /* condition not met */
4965 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4966 c1 = new_r_Const(irg, tv);
4967 DBG_OPT_CSTEVAL(n, c1);
4970 sl = get_Shrs_left(left);
4971 blk = get_nodes_block(n);
4972 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4973 tv = tarval_shl(tv, tv1);
4975 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4983 if (changedc) { /* need a new Const */
4984 ir_graph *irg = get_irn_irg(n);
4985 right = new_r_Const(irg, tv);
4989 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4990 ir_node *op = get_Proj_pred(left);
4992 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4993 ir_node *c = get_binop_right(op);
4996 ir_tarval *tv = get_Const_tarval(c);
4998 if (tarval_is_single_bit(tv)) {
4999 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5000 ir_node *v = get_binop_left(op);
5001 ir_node *blk = get_irn_n(op, -1);
5002 ir_graph *irg = get_irn_irg(op);
5003 ir_mode *mode = get_irn_mode(v);
5005 tv = tarval_sub(tv, get_mode_one(mode), NULL);
5006 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5008 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5015 dbg_info *dbgi = get_irn_dbg_info(n);
5016 ir_node *block = get_nodes_block(n);
5018 /* create a new compare */
5019 n = new_rd_Cmp(dbgi, block, left, right, relation);
5026 * Optimize CopyB(mem, x, x) into a Nop.
5028 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5030 ir_node *copyb = get_Proj_pred(proj);
5031 ir_node *a = get_CopyB_dst(copyb);
5032 ir_node *b = get_CopyB_src(copyb);
5035 switch (get_Proj_proj(proj)) {
5036 case pn_CopyB_X_regular:
5037 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5038 DBG_OPT_EXC_REM(proj);
5039 proj = new_r_Jmp(get_nodes_block(copyb));
5041 case pn_CopyB_X_except: {
5042 ir_graph *irg = get_irn_irg(proj);
5043 DBG_OPT_EXC_REM(proj);
5044 proj = new_r_Bad(irg, mode_X);
5055 * Optimize Bounds(idx, idx, upper) into idx.
5057 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5059 ir_node *oldn = proj;
5060 ir_node *bound = get_Proj_pred(proj);
5061 ir_node *idx = get_Bound_index(bound);
5062 ir_node *pred = skip_Proj(idx);
5065 if (idx == get_Bound_lower(bound))
5067 else if (is_Bound(pred)) {
5069 * idx was Bounds checked previously, it is still valid if
5070 * lower <= pred_lower && pred_upper <= upper.
5072 ir_node *lower = get_Bound_lower(bound);
5073 ir_node *upper = get_Bound_upper(bound);
5074 if (get_Bound_lower(pred) == lower &&
5075 get_Bound_upper(pred) == upper) {
5077 * One could expect that we simply return the previous
5078 * Bound here. However, this would be wrong, as we could
5079 * add an exception Proj to a new location then.
5080 * So, we must turn in into a tuple.
5086 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5087 switch (get_Proj_proj(proj)) {
5089 DBG_OPT_EXC_REM(proj);
5090 proj = get_Bound_mem(bound);
5092 case pn_Bound_X_except:
5093 DBG_OPT_EXC_REM(proj);
5094 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5098 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5100 case pn_Bound_X_regular:
5101 DBG_OPT_EXC_REM(proj);
5102 proj = new_r_Jmp(get_nodes_block(bound));
5112 * Does all optimizations on nodes that must be done on its Projs
5113 * because of creating new nodes.
5115 static ir_node *transform_node_Proj(ir_node *proj)
5117 ir_node *n = get_Proj_pred(proj);
5119 if (n->op->ops.transform_node_Proj)
5120 return n->op->ops.transform_node_Proj(proj);
5125 * Test whether a block is unreachable
5126 * Note: That this only returns true when
5127 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5128 * This is important, as you easily end up producing invalid constructs in the
5129 * unreachable code when optimizing away edges into the unreachable code.
5130 * So only set this flag when you iterate localopts to the fixpoint.
5131 * When you reach the fixpoint then all unreachable code is dead
5132 * (= can't be reached by firm edges) and you won't see the invalid constructs
5135 static bool is_block_unreachable(const ir_node *block)
5137 const ir_graph *irg = get_irn_irg(block);
5138 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5140 return get_Block_dom_depth(block) < 0;
5143 static ir_node *transform_node_Block(ir_node *block)
5145 ir_graph *irg = get_irn_irg(block);
5146 int arity = get_irn_arity(block);
5147 ir_node *bad = NULL;
5150 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5153 for (i = 0; i < arity; ++i) {
5154 ir_node *const pred = get_Block_cfgpred(block, i);
5155 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5158 bad = new_r_Bad(irg, mode_X);
5159 set_irn_n(block, i, bad);
5165 static ir_node *transform_node_Phi(ir_node *phi)
5167 int n = get_irn_arity(phi);
5168 ir_mode *mode = get_irn_mode(phi);
5169 ir_node *block = get_nodes_block(phi);
5170 ir_graph *irg = get_irn_irg(phi);
5171 ir_node *bad = NULL;
5174 /* Set phi-operands for bad-block inputs to bad */
5175 for (i = 0; i < n; ++i) {
5176 if (!is_Bad(get_Phi_pred(phi, i))) {
5177 ir_node *pred = get_Block_cfgpred(block, i);
5178 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5180 bad = new_r_Bad(irg, mode);
5181 set_irn_n(phi, i, bad);
5186 /* Move Pin nodes down through Phi nodes. */
5187 if (mode == mode_M) {
5188 n = get_irn_arity(phi);
5190 /* Beware of Phi0 */
5194 bool has_pin = false;
5196 NEW_ARR_A(ir_node *, in, n);
5198 for (i = 0; i < n; ++i) {
5199 ir_node *pred = get_irn_n(phi, i);
5202 in[i] = get_Pin_op(pred);
5204 } else if (is_Bad(pred)) {
5214 /* Move the Pin nodes "behind" the Phi. */
5215 block = get_irn_n(phi, -1);
5216 new_phi = new_r_Phi(block, n, in, mode_M);
5217 return new_r_Pin(block, new_phi);
5220 /* Move Confirms down through Phi nodes. */
5221 else if (mode_is_reference(mode)) {
5222 n = get_irn_arity(phi);
5224 /* Beware of Phi0 */
5226 ir_node *pred = get_irn_n(phi, 0);
5227 ir_node *bound, *new_phi, *block, **in;
5228 ir_relation relation;
5229 bool has_confirm = false;
5231 if (! is_Confirm(pred))
5234 bound = get_Confirm_bound(pred);
5235 relation = get_Confirm_relation(pred);
5237 NEW_ARR_A(ir_node *, in, n);
5238 in[0] = get_Confirm_value(pred);
5240 for (i = 1; i < n; ++i) {
5241 pred = get_irn_n(phi, i);
5243 if (is_Confirm(pred) &&
5244 get_Confirm_bound(pred) == bound &&
5245 get_Confirm_relation(pred) == relation) {
5246 in[i] = get_Confirm_value(pred);
5248 } else if (is_Bad(pred)) {
5258 /* move the Confirm nodes "behind" the Phi */
5259 block = get_irn_n(phi, -1);
5260 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5261 return new_r_Confirm(block, new_phi, bound, relation);
5268 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5270 * Should be moved to reassociation?
5272 static ir_node *transform_node_shift(ir_node *n)
5274 ir_node *left, *right;
5276 ir_mode *count_mode;
5277 ir_tarval *tv1, *tv2, *res;
5278 ir_node *in[2], *irn, *block;
5282 left = get_binop_left(n);
5284 /* different operations */
5285 if (get_irn_op(left) != get_irn_op(n))
5288 right = get_binop_right(n);
5289 tv1 = value_of(right);
5290 if (tv1 == tarval_bad)
5293 tv2 = value_of(get_binop_right(left));
5294 if (tv2 == tarval_bad)
5297 count_mode = get_tarval_mode(tv1);
5298 if (get_tarval_mode(tv2) != count_mode) {
5299 /* TODO: search bigger mode or something and convert... */
5303 mode = get_irn_mode(n);
5304 modulo_shf = get_mode_modulo_shift(mode);
5306 if (modulo_shf > 0) {
5307 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5309 /* I'm not so sure what happens in one complement... */
5310 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5311 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5312 * above will be invalid) */
5313 assert(modulo_shf<=0 || is_po2(modulo_shf));
5315 tv1 = tarval_and(tv1, modulo_mask);
5316 tv2 = tarval_and(tv2, modulo_mask);
5318 res = tarval_add(tv1, tv2);
5319 irg = get_irn_irg(n);
5321 /* beware: a simple replacement works only, if res < modulo shift */
5323 int bits = get_mode_size_bits(mode);
5324 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5325 res = tarval_mod(res, modulo);
5327 long bits = get_mode_size_bits(mode);
5328 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5330 /* shifting too much */
5331 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5333 ir_node *block = get_nodes_block(n);
5334 dbg_info *dbgi = get_irn_dbg_info(n);
5335 ir_mode *smode = get_irn_mode(right);
5336 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5337 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5340 return new_r_Const(irg, get_mode_null(mode));
5344 /* ok, we can replace it */
5345 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5346 block = get_nodes_block(n);
5348 in[0] = get_binop_left(left);
5349 in[1] = new_r_Const(irg, res);
5351 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5353 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5360 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5362 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5363 * (also with x >>s c1 when c1>=c2)
5365 static ir_node *transform_node_shl_shr(ir_node *n)
5368 ir_node *right = get_binop_right(n);
5378 ir_tarval *tv_shift;
5381 ir_relation relation;
5384 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5386 if (!is_Const(right))
5389 left = get_binop_left(n);
5390 mode = get_irn_mode(n);
5391 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5392 ir_node *shr_right = get_binop_right(left);
5394 if (!is_Const(shr_right))
5397 x = get_binop_left(left);
5398 tv_shr = get_Const_tarval(shr_right);
5399 tv_shl = get_Const_tarval(right);
5401 if (is_Shrs(left)) {
5402 /* shrs variant only allowed if c1 >= c2 */
5403 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5406 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5409 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5411 tv_mask = tarval_shl(tv_mask, tv_shl);
5412 } else if (is_Shr(n) && is_Shl(left)) {
5413 ir_node *shl_right = get_Shl_right(left);
5415 if (!is_Const(shl_right))
5418 x = get_Shl_left(left);
5419 tv_shr = get_Const_tarval(right);
5420 tv_shl = get_Const_tarval(shl_right);
5422 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5423 tv_mask = tarval_shr(tv_mask, tv_shr);
5428 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5429 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5432 assert(tv_mask != tarval_bad);
5433 assert(get_tarval_mode(tv_mask) == mode);
5435 block = get_nodes_block(n);
5436 irg = get_irn_irg(block);
5437 dbgi = get_irn_dbg_info(n);
5439 relation = tarval_cmp(tv_shl, tv_shr);
5440 if (relation == ir_relation_less || relation == ir_relation_equal) {
5441 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5442 new_const = new_r_Const(irg, tv_shift);
5444 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5446 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5449 assert(relation == ir_relation_greater);
5450 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5451 new_const = new_r_Const(irg, tv_shift);
5452 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5455 new_const = new_r_Const(irg, tv_mask);
5456 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5461 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5463 ir_mode *mode = get_tarval_mode(tv);
5464 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5465 return tarval_mod(tv, modulo_tv);
5468 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5469 ir_node *left, ir_node *right, ir_mode *mode);
5472 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5473 * then we can use that to minimize the value of Add(x, const) or
5474 * Sub(Const, x). In particular this often avoids 1 instruction in some
5475 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5476 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5478 static ir_node *transform_node_shift_modulo(ir_node *n,
5479 new_shift_func new_shift)
5481 ir_mode *mode = get_irn_mode(n);
5482 int modulo = get_mode_modulo_shift(mode);
5483 ir_node *newop = NULL;
5484 ir_mode *mode_right;
5491 if (get_mode_arithmetic(mode) != irma_twos_complement)
5493 if (!is_po2(modulo))
5496 irg = get_irn_irg(n);
5497 block = get_nodes_block(n);
5498 right = get_binop_right(n);
5499 mode_right = get_irn_mode(right);
5500 if (is_Const(right)) {
5501 ir_tarval *tv = get_Const_tarval(right);
5502 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5507 newop = new_r_Const(irg, tv_mod);
5508 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5509 ir_node *add_right = get_binop_right(right);
5510 if (is_Const(add_right)) {
5511 ir_tarval *tv = get_Const_tarval(add_right);
5512 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5517 newconst = new_r_Const(irg, tv_mod);
5518 newop = new_r_Add(block, get_binop_left(right), newconst,
5521 } else if (is_Sub(right)) {
5522 ir_node *sub_left = get_Sub_left(right);
5523 if (is_Const(sub_left)) {
5524 ir_tarval *tv = get_Const_tarval(sub_left);
5525 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5530 newconst = new_r_Const(irg, tv_mod);
5531 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5538 if (newop != NULL) {
5539 dbg_info *dbgi = get_irn_dbg_info(n);
5540 ir_node *left = get_binop_left(n);
5541 return new_shift(dbgi, block, left, newop, mode);
5549 static ir_node *transform_node_Shr(ir_node *n)
5551 ir_node *c, *oldn = n;
5552 ir_node *left = get_Shr_left(n);
5553 ir_node *right = get_Shr_right(n);
5554 ir_mode *mode = get_irn_mode(n);
5556 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5557 n = transform_node_shift(n);
5560 n = transform_node_shift_modulo(n, new_rd_Shr);
5562 n = transform_node_shl_shr(n);
5564 n = transform_node_shift_bitop(n);
5572 static ir_node *transform_node_Shrs(ir_node *n)
5575 ir_node *a = get_Shrs_left(n);
5576 ir_node *b = get_Shrs_right(n);
5577 ir_mode *mode = get_irn_mode(n);
5581 if (is_oversize_shift(n)) {
5582 ir_node *block = get_nodes_block(n);
5583 dbg_info *dbgi = get_irn_dbg_info(n);
5584 ir_mode *cmode = get_irn_mode(b);
5585 long val = get_mode_size_bits(cmode)-1;
5586 ir_graph *irg = get_irn_irg(n);
5587 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5588 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5591 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5592 n = transform_node_shift(n);
5596 n = transform_node_shift_modulo(n, new_rd_Shrs);
5599 n = transform_node_shift_bitop(n);
5603 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5604 attr = vrp_get_info(a);
5606 unsigned bits = get_mode_size_bits(mode);
5607 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5608 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5609 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5610 dbg_info *dbgi = get_irn_dbg_info(n);
5611 ir_node *block = get_nodes_block(n);
5612 return new_rd_Shr(dbgi, block, a, b, mode);
5622 static ir_node *transform_node_Shl(ir_node *n)
5624 ir_node *c, *oldn = n;
5625 ir_node *a = get_Shl_left(n);
5626 ir_node *b = get_Shl_right(n);
5627 ir_mode *mode = get_irn_mode(n);
5629 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5630 n = transform_node_shift(n);
5633 n = transform_node_shift_modulo(n, new_rd_Shl);
5635 n = transform_node_shl_shr(n);
5637 n = transform_node_shift_bitop(n);
5645 static ir_node *transform_node_Rotl(ir_node *n)
5647 ir_node *c, *oldn = n;
5648 ir_node *a = get_Rotl_left(n);
5649 ir_node *b = get_Rotl_right(n);
5650 ir_mode *mode = get_irn_mode(n);
5652 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5653 n = transform_node_shift(n);
5656 n = transform_node_shift_bitop(n);
5664 static ir_node *transform_node_Conv(ir_node *n)
5666 ir_node *c, *oldn = n;
5667 ir_mode *mode = get_irn_mode(n);
5668 ir_node *a = get_Conv_op(n);
5670 if (mode != mode_b && is_const_Phi(a)) {
5671 /* Do NOT optimize mode_b Conv's, this leads to remaining
5672 * Phib nodes later, because the conv_b_lower operation
5673 * is instantly reverted, when it tries to insert a Convb.
5675 c = apply_conv_on_phi(a, mode);
5677 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5682 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5683 ir_graph *irg = get_irn_irg(n);
5684 return new_r_Unknown(irg, mode);
5687 if (mode_is_reference(mode) &&
5688 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5690 ir_node *l = get_Add_left(a);
5691 ir_node *r = get_Add_right(a);
5692 dbg_info *dbgi = get_irn_dbg_info(a);
5693 ir_node *block = get_nodes_block(n);
5695 ir_node *lop = get_Conv_op(l);
5696 if (get_irn_mode(lop) == mode) {
5697 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5698 n = new_rd_Add(dbgi, block, lop, r, mode);
5703 ir_node *rop = get_Conv_op(r);
5704 if (get_irn_mode(rop) == mode) {
5705 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5706 n = new_rd_Add(dbgi, block, l, rop, mode);
5716 * Remove dead blocks and nodes in dead blocks
5717 * in keep alive list. We do not generate a new End node.
5719 static ir_node *transform_node_End(ir_node *n)
5721 int i, j, n_keepalives = get_End_n_keepalives(n);
5724 NEW_ARR_A(ir_node *, in, n_keepalives);
5726 for (i = j = 0; i < n_keepalives; ++i) {
5727 ir_node *ka = get_End_keepalive(n, i);
5729 /* no need to keep Bad */
5732 /* do not keep unreachable code */
5733 block = is_Block(ka) ? ka : get_nodes_block(ka);
5734 if (is_block_unreachable(block))
5738 if (j != n_keepalives)
5739 set_End_keepalives(n, j, in);
5743 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5745 if (is_Minus(a) && get_Minus_op(a) == b)
5747 if (is_Minus(b) && get_Minus_op(b) == a)
5749 if (is_Sub(a) && is_Sub(b)) {
5750 ir_node *a_left = get_Sub_left(a);
5751 ir_node *a_right = get_Sub_right(a);
5752 ir_node *b_left = get_Sub_left(b);
5753 ir_node *b_right = get_Sub_right(b);
5755 if (a_left == b_right && a_right == b_left)
5762 static const ir_node *skip_upconv(const ir_node *node)
5764 while (is_Conv(node)) {
5765 ir_mode *mode = get_irn_mode(node);
5766 const ir_node *op = get_Conv_op(node);
5767 ir_mode *op_mode = get_irn_mode(op);
5768 if (!smaller_mode(op_mode, mode))
5775 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5776 const ir_node *mux_true)
5781 ir_relation relation;
5787 * Note further that these optimization work even for floating point
5788 * with NaN's because -NaN == NaN.
5789 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5792 mode = get_irn_mode(mux_true);
5793 if (mode_honor_signed_zeros(mode))
5796 /* must be <, <=, >=, > */
5797 relation = get_Cmp_relation(sel);
5798 if ((relation & ir_relation_less_greater) == 0)
5801 if (!ir_is_negated_value(mux_true, mux_false))
5804 mux_true = skip_upconv(mux_true);
5805 mux_false = skip_upconv(mux_false);
5807 /* must be x cmp 0 */
5808 cmp_right = get_Cmp_right(sel);
5809 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5812 cmp_left = get_Cmp_left(sel);
5813 if (cmp_left == mux_false) {
5814 if (relation & ir_relation_less) {
5817 assert(relation & ir_relation_greater);
5820 } else if (cmp_left == mux_true) {
5821 if (relation & ir_relation_less) {
5824 assert(relation & ir_relation_greater);
5832 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5835 ir_node *cmp_left = get_Cmp_left(sel);
5836 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5839 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5840 const ir_node *mux_true)
5842 /* this code should return true each time transform_node_Mux would
5843 * optimize the Mux completely away */
5845 ir_mode *mode = get_irn_mode(mux_false);
5846 if (get_mode_arithmetic(mode) == irma_twos_complement
5847 && ir_mux_is_abs(sel, mux_false, mux_true))
5850 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5851 const ir_node *cmp_r = get_Cmp_right(sel);
5852 const ir_node *cmp_l = get_Cmp_left(sel);
5853 const ir_node *f = mux_false;
5854 const ir_node *t = mux_true;
5856 if (is_Const(t) && is_Const_null(t)) {
5861 if (is_And(cmp_l) && f == cmp_r) {
5862 ir_node *and_r = get_And_right(cmp_l);
5865 if (and_r == t && is_single_bit(and_r))
5867 and_l = get_And_left(cmp_l);
5868 if (and_l == t && is_single_bit(and_l))
5877 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5879 static ir_node *transform_Mux_set(ir_node *n)
5881 ir_node *cond = get_Mux_sel(n);
5886 ir_relation relation;
5900 left = get_Cmp_left(cond);
5901 mode = get_irn_mode(left);
5902 if (!mode_is_int(mode) && !mode_is_reference(mode))
5904 dest_mode = get_irn_mode(n);
5905 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5907 right = get_Cmp_right(cond);
5908 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5909 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5910 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5911 && relation != ir_relation_greater))
5916 case ir_relation_less:
5917 /* a < b -> (a - b) >> 31 */
5921 case ir_relation_less_equal:
5922 /* a <= b -> ~(a - b) >> 31 */
5927 case ir_relation_greater:
5928 /* a > b -> (b - a) >> 31 */
5932 case ir_relation_greater_equal:
5933 /* a >= b -> ~(a - b) >> 31 */
5942 dbgi = get_irn_dbg_info(n);
5943 block = get_nodes_block(n);
5944 irg = get_irn_irg(block);
5945 bits = get_mode_size_bits(dest_mode);
5946 tv = new_tarval_from_long(bits-1, mode_Iu);
5947 shift_cnt = new_rd_Const(dbgi, irg, tv);
5949 if (mode != dest_mode) {
5950 a = new_rd_Conv(dbgi, block, a, dest_mode);
5951 b = new_rd_Conv(dbgi, block, b, dest_mode);
5954 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5956 res = new_rd_Not(dbgi, block, res, dest_mode);
5958 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5963 * Optimize a Mux into some simpler cases.
5965 static ir_node *transform_node_Mux(ir_node *n)
5968 ir_node *sel = get_Mux_sel(n);
5969 ir_mode *mode = get_irn_mode(n);
5970 ir_node *t = get_Mux_true(n);
5971 ir_node *f = get_Mux_false(n);
5972 ir_graph *irg = get_irn_irg(n);
5974 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5975 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5976 int abs = ir_mux_is_abs(sel, f, t);
5978 dbg_info *dbgi = get_irn_dbg_info(n);
5979 ir_node *block = get_nodes_block(n);
5980 ir_node *op = ir_get_abs_op(sel, f, t);
5981 int bits = get_mode_size_bits(mode);
5982 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5983 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5984 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5987 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5989 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5995 /* first normalization step: try to move a constant to the false side,
5996 * 0 preferred on false side too */
5997 if (is_Cmp(sel) && is_Const(t) &&
5998 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5999 dbg_info *seldbgi = get_irn_dbg_info(sel);
6000 ir_node *block = get_nodes_block(sel);
6001 ir_relation relation = get_Cmp_relation(sel);
6006 /* Mux(x, a, b) => Mux(not(x), b, a) */
6007 relation = get_negated_relation(relation);
6008 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6009 get_Cmp_right(sel), relation);
6010 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6013 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6014 n = transform_Mux_set(n);
6019 /* the following optimisations create new mode_b nodes, so only do them
6020 * before mode_b lowering */
6021 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6023 ir_node* block = get_nodes_block(n);
6025 ir_node* c1 = get_Mux_sel(t);
6026 ir_node* t1 = get_Mux_true(t);
6027 ir_node* f1 = get_Mux_false(t);
6029 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6030 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6031 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6032 return new_r_Mux(block, and_, f1, t1, mode);
6033 } else if (f == t1) {
6034 /* Mux(cond0, Mux(cond1, x, y), x) */
6035 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6036 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6037 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6038 return new_r_Mux(block, and_, t1, f1, mode);
6040 } else if (is_Mux(f)) {
6041 ir_node* block = get_nodes_block(n);
6043 ir_node* c1 = get_Mux_sel(f);
6044 ir_node* t1 = get_Mux_true(f);
6045 ir_node* f1 = get_Mux_false(f);
6047 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6048 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6049 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6050 return new_r_Mux(block, or_, f1, t1, mode);
6051 } else if (t == f1) {
6052 /* Mux(cond0, x, Mux(cond1, y, x)) */
6053 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6054 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6055 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6056 return new_r_Mux(block, or_, t1, f1, mode);
6060 /* note: after normalization, false can only happen on default */
6061 if (mode == mode_b) {
6062 dbg_info *dbg = get_irn_dbg_info(n);
6063 ir_node *block = get_nodes_block(n);
6066 ir_tarval *tv_t = get_Const_tarval(t);
6067 if (tv_t == tarval_b_true) {
6069 /* Muxb(sel, true, false) = sel */
6070 assert(get_Const_tarval(f) == tarval_b_false);
6071 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6074 /* Muxb(sel, true, x) = Or(sel, x) */
6075 n = new_rd_Or(dbg, block, sel, f, mode_b);
6076 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6080 } else if (is_Const(f)) {
6081 ir_tarval *tv_f = get_Const_tarval(f);
6082 if (tv_f == tarval_b_true) {
6083 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6084 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6085 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6086 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6089 /* Muxb(sel, x, false) = And(sel, x) */
6090 assert(tv_f == tarval_b_false);
6091 n = new_rd_And(dbg, block, sel, t, mode_b);
6092 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6099 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6100 ir_relation relation = get_Cmp_relation(sel);
6101 ir_node *cmp_r = get_Cmp_right(sel);
6102 ir_node *cmp_l = get_Cmp_left(sel);
6103 ir_node *block = get_nodes_block(n);
6105 if (is_And(cmp_l) && f == cmp_r) {
6106 ir_node *and_r = get_And_right(cmp_l);
6109 if (and_r == t && is_single_bit(and_r)) {
6110 if (relation == ir_relation_equal) {
6111 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6112 n = new_rd_Eor(get_irn_dbg_info(n),
6113 block, cmp_l, t, mode);
6114 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6116 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6118 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6122 and_l = get_And_left(cmp_l);
6123 if (and_l == t && is_single_bit(and_l)) {
6124 if (relation == ir_relation_equal) {
6125 /* ((1 << n) & a) == 0, (1 << n), 0) */
6126 n = new_rd_Eor(get_irn_dbg_info(n),
6127 block, cmp_l, t, mode);
6128 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6130 /* ((1 << n) & a) != 0, (1 << n), 0) */
6132 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6143 * optimize Sync nodes that have other syncs as input we simply add the inputs
6144 * of the other sync to our own inputs
6146 static ir_node *transform_node_Sync(ir_node *n)
6148 int arity = get_Sync_n_preds(n);
6151 for (i = 0; i < arity;) {
6152 ir_node *pred = get_Sync_pred(n, i);
6156 /* Remove Bad predecessors */
6163 /* Remove duplicate predecessors */
6164 for (j = 0; j < i; ++j) {
6165 if (get_Sync_pred(n, j) == pred) {
6174 if (!is_Sync(pred)) {
6182 pred_arity = get_Sync_n_preds(pred);
6183 for (j = 0; j < pred_arity; ++j) {
6184 ir_node *pred_pred = get_Sync_pred(pred, j);
6189 add_irn_n(n, pred_pred);
6193 if (get_Sync_pred(n, k) == pred_pred) break;
6199 ir_graph *irg = get_irn_irg(n);
6200 return new_r_Bad(irg, mode_M);
6203 return get_Sync_pred(n, 0);
6206 /* rehash the sync node */
6211 static ir_node *transform_node_Load(ir_node *n)
6213 /* if our memory predecessor is a load from the same address, then reuse the
6214 * previous result */
6215 ir_node *mem = get_Load_mem(n);
6220 /* don't touch volatile loads */
6221 if (get_Load_volatility(n) == volatility_is_volatile)
6223 mem_pred = get_Proj_pred(mem);
6224 if (is_Load(mem_pred)) {
6225 ir_node *pred_load = mem_pred;
6227 /* conservatively compare the 2 loads. TODO: This could be less strict
6228 * with fixup code in some situations (like smaller/bigger modes) */
6229 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6231 if (get_Load_mode(pred_load) != get_Load_mode(n))
6233 /* all combinations of aligned/unaligned pred/n should be fine so we do
6234 * not compare the unaligned attribute */
6236 ir_node *block = get_nodes_block(n);
6237 ir_node *jmp = new_r_Jmp(block);
6238 ir_graph *irg = get_irn_irg(n);
6239 ir_node *bad = new_r_Bad(irg, mode_X);
6240 ir_mode *mode = get_Load_mode(n);
6241 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6242 ir_node *in[] = { mem, res, jmp, bad };
6243 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6246 } else if (is_Store(mem_pred)) {
6247 ir_node *pred_store = mem_pred;
6248 ir_node *value = get_Store_value(pred_store);
6250 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6252 if (get_irn_mode(value) != get_Load_mode(n))
6254 /* all combinations of aligned/unaligned pred/n should be fine so we do
6255 * not compare the unaligned attribute */
6257 ir_node *block = get_nodes_block(n);
6258 ir_node *jmp = new_r_Jmp(block);
6259 ir_graph *irg = get_irn_irg(n);
6260 ir_node *bad = new_r_Bad(irg, mode_X);
6261 ir_node *res = value;
6262 ir_node *in[] = { mem, res, jmp, bad };
6263 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6272 * optimize a trampoline Call into a direct Call
6274 static ir_node *transform_node_Call(ir_node *call)
6276 ir_node *callee = get_Call_ptr(call);
6277 ir_node *adr, *mem, *res, *bl, **in;
6278 ir_type *ctp, *mtp, *tp;
6282 size_t i, n_res, n_param;
6285 if (! is_Proj(callee))
6287 callee = get_Proj_pred(callee);
6288 if (! is_Builtin(callee))
6290 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6293 mem = get_Call_mem(call);
6295 if (skip_Proj(mem) == callee) {
6296 /* memory is routed to the trampoline, skip */
6297 mem = get_Builtin_mem(callee);
6300 /* build a new call type */
6301 mtp = get_Call_type(call);
6302 tdb = get_type_dbg_info(mtp);
6304 n_res = get_method_n_ress(mtp);
6305 n_param = get_method_n_params(mtp);
6306 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6308 for (i = 0; i < n_res; ++i)
6309 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6311 NEW_ARR_A(ir_node *, in, n_param + 1);
6313 /* FIXME: we don't need a new pointer type in every step */
6314 irg = get_irn_irg(call);
6315 tp = get_irg_frame_type(irg);
6316 tp = new_type_pointer(tp);
6317 set_method_param_type(ctp, 0, tp);
6319 in[0] = get_Builtin_param(callee, 2);
6320 for (i = 0; i < n_param; ++i) {
6321 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6322 in[i + 1] = get_Call_param(call, i);
6324 var = get_method_variadicity(mtp);
6325 set_method_variadicity(ctp, var);
6326 /* When we resolve a trampoline, the function must be called by a this-call */
6327 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6328 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6330 adr = get_Builtin_param(callee, 1);
6332 db = get_irn_dbg_info(call);
6333 bl = get_nodes_block(call);
6335 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6336 if (get_irn_pinned(call) == op_pin_state_floats)
6337 set_irn_pinned(res, op_pin_state_floats);
6341 void firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6345 ops->transform_node = transform_node_##a; \
6347 #define CASE_PROJ(a) \
6349 ops->transform_node_Proj = transform_node_Proj_##a; \
6351 #define CASE_PROJ_EX(a) \
6353 ops->transform_node = transform_node_##a; \
6354 ops->transform_node_Proj = transform_node_Proj_##a; \
6397 * Tries several [inplace] [optimizing] transformations and returns an
6398 * equivalent node. The difference to equivalent_node() is that these
6399 * transformations _do_ generate new nodes, and thus the old node must
6400 * not be freed even if the equivalent node isn't the old one.
6402 static ir_node *transform_node(ir_node *n)
6408 iro = get_irn_opcode_(n);
6409 /* constant expression evaluation / constant folding */
6410 if (get_opt_constant_folding()) {
6411 /* neither constants nor Tuple values can be evaluated */
6412 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6413 /* try to evaluate */
6414 ir_tarval *tv = computed_value(n);
6415 if (tv != tarval_bad) {
6416 /* evaluation was successful -- replace the node. */
6417 ir_graph *irg = get_irn_irg(n);
6419 n = new_r_Const(irg, tv);
6421 DBG_OPT_CSTEVAL(old_n, n);
6427 /* remove unnecessary nodes */
6428 if (get_opt_constant_folding() ||
6429 (iro == iro_Phi) || /* always optimize these nodes. */
6430 (iro == iro_Id) || /* ... */
6431 (iro == iro_Proj) || /* ... */
6432 (iro == iro_Block)) { /* Flags tested local. */
6433 n = equivalent_node(n);
6438 /* Some more constant expression evaluation. */
6439 if (get_opt_algebraic_simplification() ||
6440 (iro == iro_Cond) ||
6441 (iro == iro_Proj)) { /* Flags tested local. */
6442 if (n->op->ops.transform_node != NULL) {
6443 n = n->op->ops.transform_node(n);
6453 /* **************** Common Subexpression Elimination **************** */
6455 /** The size of the hash table used, should estimate the number of nodes
6457 #define N_IR_NODES 512
6459 /** Compares two exception attributes */
6460 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6462 const except_attr *ea = &a->attr.except;
6463 const except_attr *eb = &b->attr.except;
6464 return ea->pin_state != eb->pin_state;
6467 /** Compares the attributes of two Const nodes. */
6468 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6470 return get_Const_tarval(a) != get_Const_tarval(b);
6473 /** Compares the attributes of two Proj nodes. */
6474 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6476 return a->attr.proj.proj != b->attr.proj.proj;
6479 /** Compares the attributes of two Alloc nodes. */
6480 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6482 const alloc_attr *pa = &a->attr.alloc;
6483 const alloc_attr *pb = &b->attr.alloc;
6484 if (pa->where != pb->where || pa->type != pb->type)
6486 return node_cmp_exception(a, b);
6489 /** Compares the attributes of two Free nodes. */
6490 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6492 const free_attr *pa = &a->attr.free;
6493 const free_attr *pb = &b->attr.free;
6494 return (pa->where != pb->where) || (pa->type != pb->type);
6497 /** Compares the attributes of two SymConst nodes. */
6498 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6500 const symconst_attr *pa = &a->attr.symc;
6501 const symconst_attr *pb = &b->attr.symc;
6502 return (pa->kind != pb->kind)
6503 || (pa->sym.type_p != pb->sym.type_p);
6506 /** Compares the attributes of two Call nodes. */
6507 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6509 const call_attr *pa = &a->attr.call;
6510 const call_attr *pb = &b->attr.call;
6511 if (pa->type != pb->type)
6513 return node_cmp_exception(a, b);
6516 /** Compares the attributes of two Sel nodes. */
6517 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6519 const ir_entity *a_ent = get_Sel_entity(a);
6520 const ir_entity *b_ent = get_Sel_entity(b);
6521 return a_ent != b_ent;
6524 /** Compares the attributes of two Phi nodes. */
6525 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6527 /* we can only enter this function if both nodes have the same number of inputs,
6528 hence it is enough to check if one of them is a Phi0 */
6530 /* check the Phi0 pos attribute */
6531 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6536 /** Compares the attributes of two Conv nodes. */
6537 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6539 return get_Conv_strict(a) != get_Conv_strict(b);
6542 /** Compares the attributes of two Cast nodes. */
6543 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6545 return get_Cast_type(a) != get_Cast_type(b);
6548 /** Compares the attributes of two Load nodes. */
6549 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6551 if (get_Load_volatility(a) == volatility_is_volatile ||
6552 get_Load_volatility(b) == volatility_is_volatile)
6553 /* NEVER do CSE on volatile Loads */
6555 /* do not CSE Loads with different alignment. Be conservative. */
6556 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6558 if (get_Load_mode(a) != get_Load_mode(b))
6560 return node_cmp_exception(a, b);
6563 /** Compares the attributes of two Store nodes. */
6564 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6566 /* do not CSE Stores with different alignment. Be conservative. */
6567 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6569 /* NEVER do CSE on volatile Stores */
6570 if (get_Store_volatility(a) == volatility_is_volatile ||
6571 get_Store_volatility(b) == volatility_is_volatile)
6573 return node_cmp_exception(a, b);
6576 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6578 if (get_CopyB_type(a) != get_CopyB_type(b))
6581 return node_cmp_exception(a, b);
6584 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6586 return node_cmp_exception(a, b);
6589 /** Compares the attributes of two Div nodes. */
6590 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6592 const div_attr *ma = &a->attr.div;
6593 const div_attr *mb = &b->attr.div;
6594 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6596 return node_cmp_exception(a, b);
6599 /** Compares the attributes of two Mod nodes. */
6600 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6602 const mod_attr *ma = &a->attr.mod;
6603 const mod_attr *mb = &b->attr.mod;
6604 if (ma->resmode != mb->resmode)
6606 return node_cmp_exception(a, b);
6609 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6611 const cmp_attr *ma = &a->attr.cmp;
6612 const cmp_attr *mb = &b->attr.cmp;
6613 return ma->relation != mb->relation;
6616 /** Compares the attributes of two Confirm nodes. */
6617 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6619 const confirm_attr *ma = &a->attr.confirm;
6620 const confirm_attr *mb = &b->attr.confirm;
6621 return ma->relation != mb->relation;
6624 /** Compares the attributes of two Builtin nodes. */
6625 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6627 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6629 if (get_Builtin_type(a) != get_Builtin_type(b))
6631 return node_cmp_exception(a, b);
6634 /** Compares the attributes of two ASM nodes. */
6635 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6638 const ir_asm_constraint *ca;
6639 const ir_asm_constraint *cb;
6642 if (get_ASM_text(a) != get_ASM_text(b))
6645 /* Should we really check the constraints here? Should be better, but is strange. */
6646 n = get_ASM_n_input_constraints(a);
6647 if (n != get_ASM_n_input_constraints(b))
6650 ca = get_ASM_input_constraints(a);
6651 cb = get_ASM_input_constraints(b);
6652 for (i = 0; i < n; ++i) {
6653 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6654 || ca[i].mode != cb[i].mode)
6658 n = get_ASM_n_output_constraints(a);
6659 if (n != get_ASM_n_output_constraints(b))
6662 ca = get_ASM_output_constraints(a);
6663 cb = get_ASM_output_constraints(b);
6664 for (i = 0; i < n; ++i) {
6665 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6666 || ca[i].mode != cb[i].mode)
6670 n = get_ASM_n_clobbers(a);
6671 if (n != get_ASM_n_clobbers(b))
6674 cla = get_ASM_clobbers(a);
6675 clb = get_ASM_clobbers(b);
6676 for (i = 0; i < n; ++i) {
6677 if (cla[i] != clb[i])
6681 return node_cmp_exception(a, b);
6684 /** Compares the inexistent attributes of two Dummy nodes. */
6685 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6689 /* Dummy nodes never equal by definition */
6693 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6695 if (get_InstOf_type(a) != get_InstOf_type(b))
6697 return node_cmp_exception(a, b);
6700 void firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6704 ops->node_cmp_attr = node_cmp_attr_##a; \
6738 * Compare function for two nodes in the value table. Gets two
6739 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6741 int identities_cmp(const void *elt, const void *key)
6743 ir_node *a = (ir_node *)elt;
6744 ir_node *b = (ir_node *)key;
6747 if (a == b) return 0;
6749 if ((get_irn_op(a) != get_irn_op(b)) ||
6750 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6752 /* compare if a's in and b's in are of equal length */
6753 irn_arity_a = get_irn_arity(a);
6754 if (irn_arity_a != get_irn_arity(b))
6757 /* blocks are never the same */
6761 if (get_irn_pinned(a) == op_pin_state_pinned) {
6762 /* for pinned nodes, the block inputs must be equal */
6763 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6766 ir_node *block_a = get_nodes_block(a);
6767 ir_node *block_b = get_nodes_block(b);
6768 if (! get_opt_global_cse()) {
6769 /* for block-local CSE both nodes must be in the same Block */
6770 if (block_a != block_b)
6773 /* The optimistic approach would be to do nothing here.
6774 * However doing GCSE optimistically produces a lot of partially dead code which appears
6775 * to be worse in practice than the missed opportunities.
6776 * So we use a very conservative variant here and only CSE if 1 value dominates the
6778 if (!block_dominates(block_a, block_b)
6779 && !block_dominates(block_b, block_a))
6784 /* compare a->in[0..ins] with b->in[0..ins] */
6785 for (i = 0; i < irn_arity_a; ++i) {
6786 ir_node *pred_a = get_irn_n(a, i);
6787 ir_node *pred_b = get_irn_n(b, i);
6788 if (pred_a != pred_b) {
6789 /* if both predecessors are CSE neutral they might be different */
6790 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6796 * here, we already now that the nodes are identical except their
6799 if (a->op->ops.node_cmp_attr)
6800 return a->op->ops.node_cmp_attr(a, b);
6806 * Calculate a hash value of a node.
6808 * @param node The IR-node
6810 unsigned ir_node_hash(const ir_node *node)
6812 return node->op->ops.hash(node);
6816 void new_identities(ir_graph *irg)
6818 if (irg->value_table != NULL)
6819 del_pset(irg->value_table);
6820 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6823 void del_identities(ir_graph *irg)
6825 if (irg->value_table != NULL)
6826 del_pset(irg->value_table);
6829 /* Normalize a node by putting constants (and operands with larger
6830 * node index) on the right (operator side). */
6831 void ir_normalize_node(ir_node *n)
6833 if (is_op_commutative(get_irn_op(n))) {
6834 ir_node *l = get_binop_left(n);
6835 ir_node *r = get_binop_right(n);
6837 /* For commutative operators perform a OP b == b OP a but keep
6838 * constants on the RIGHT side. This helps greatly in some
6839 * optimizations. Moreover we use the idx number to make the form
6841 if (!operands_are_normalized(l, r)) {
6842 set_binop_left(n, r);
6843 set_binop_right(n, l);
6850 * Return the canonical node computing the same value as n.
6851 * Looks up the node in a hash table, enters it in the table
6852 * if it isn't there yet.
6854 * @param n the node to look up
6856 * @return a node that computes the same value as n or n if no such
6857 * node could be found
6859 ir_node *identify_remember(ir_node *n)
6861 ir_graph *irg = get_irn_irg(n);
6862 pset *value_table = irg->value_table;
6865 if (value_table == NULL)
6868 ir_normalize_node(n);
6869 /* lookup or insert in hash table with given hash key. */
6870 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6873 /* n is reachable again */
6874 edges_node_revival(nn);
6881 * During construction we set the op_pin_state_pinned flag in the graph right
6882 * when the optimization is performed. The flag turning on procedure global
6883 * cse could be changed between two allocations. This way we are safe.
6885 * @param n The node to lookup
6887 static inline ir_node *identify_cons(ir_node *n)
6891 n = identify_remember(n);
6892 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6893 ir_graph *irg = get_irn_irg(n);
6894 set_irg_pinned(irg, op_pin_state_floats);
6899 /* Add a node to the identities value table. */
6900 void add_identities(ir_node *node)
6907 identify_remember(node);
6910 /* Visit each node in the value table of a graph. */
6911 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6914 ir_graph *rem = current_ir_graph;
6916 current_ir_graph = irg;
6917 foreach_pset(irg->value_table, ir_node*, node) {
6920 current_ir_graph = rem;
6924 * These optimizations deallocate nodes from the obstack.
6925 * It can only be called if it is guaranteed that no other nodes
6926 * reference this one, i.e., right after construction of a node.
6928 * @param n The node to optimize
6930 ir_node *optimize_node(ir_node *n)
6933 ir_graph *irg = get_irn_irg(n);
6934 unsigned iro = get_irn_opcode(n);
6937 /* Always optimize Phi nodes: part of the construction. */
6938 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6940 /* constant expression evaluation / constant folding */
6941 if (get_opt_constant_folding()) {
6942 /* neither constants nor Tuple values can be evaluated */
6943 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6944 /* try to evaluate */
6945 tv = computed_value(n);
6946 if (tv != tarval_bad) {
6951 * we MUST copy the node here temporarily, because it's still
6952 * needed for DBG_OPT_CSTEVAL
6954 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6955 oldn = (ir_node*)alloca(node_size);
6957 memcpy(oldn, n, node_size);
6958 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6960 /* ARG, copy the in array, we need it for statistics */
6961 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6963 /* note the inplace edges module */
6964 edges_node_deleted(n);
6966 /* evaluation was successful -- replace the node. */
6967 irg_kill_node(irg, n);
6968 nw = new_r_Const(irg, tv);
6970 DBG_OPT_CSTEVAL(oldn, nw);
6976 /* remove unnecessary nodes */
6977 if (get_opt_algebraic_simplification() ||
6978 (iro == iro_Phi) || /* always optimize these nodes. */
6980 (iro == iro_Proj) ||
6981 (iro == iro_Block) ) /* Flags tested local. */
6982 n = equivalent_node(n);
6984 /* Common Subexpression Elimination.
6986 * Checks whether n is already available.
6987 * The block input is used to distinguish different subexpressions. Right
6988 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6989 * subexpressions within a block.
6992 n = identify_cons(n);
6995 edges_node_deleted(oldn);
6997 /* We found an existing, better node, so we can deallocate the old node. */
6998 irg_kill_node(irg, oldn);
7002 /* Some more constant expression evaluation that does not allow to
7004 iro = get_irn_opcode(n);
7005 if (get_opt_algebraic_simplification() ||
7006 (iro == iro_Cond) ||
7007 (iro == iro_Proj)) { /* Flags tested local. */
7008 n = transform_node(n);
7011 /* Now we have a legal, useful node. Enter it in hash table for CSE */
7012 if (get_opt_cse()) {
7014 n = identify_remember(o);
7024 * These optimizations never deallocate nodes (in place). This can cause dead
7025 * nodes lying on the obstack. Remove these by a dead node elimination,
7026 * i.e., a copying garbage collection.
7028 ir_node *optimize_in_place_2(ir_node *n)
7030 if (!get_opt_optimize() && !is_Phi(n)) return n;
7035 /** common subexpression elimination **/
7036 /* Checks whether n is already available. */
7037 /* The block input is used to distinguish different subexpressions.
7038 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
7039 * only finds common subexpressions within a block. */
7040 if (get_opt_cse()) {
7042 n = identify_remember(n);
7045 /* we have another existing node now, we do not optimize it here */
7050 n = transform_node(n);
7052 /* Now we can verify the node, as it has no dead inputs any more. */
7055 /* Now we have a legal, useful node. Enter it in hash table for cse.
7056 * Blocks should be unique anyways. (Except the successor of start:
7057 * is cse with the start block!)
7059 * Note: This is only necessary because some of the optimisations
7060 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7061 * bad practice and should be fixed sometime.
7063 if (get_opt_cse()) {
7065 n = identify_remember(o);
7074 * Wrapper for external use, set proper status bits after optimization.
7076 ir_node *optimize_in_place(ir_node *n)
7078 ir_graph *irg = get_irn_irg(n);
7079 /* Handle graph state */
7080 assert(get_irg_phase_state(irg) != phase_building);
7082 if (get_opt_global_cse())
7083 set_irg_pinned(irg, op_pin_state_floats);
7085 /* FIXME: Maybe we could also test whether optimizing the node can
7086 change the control graph. */
7087 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7088 return optimize_in_place_2(n);
7092 * Calculate a hash value of a Const node.
7094 static unsigned hash_Const(const ir_node *node)
7098 /* special value for const, as they only differ in their tarval. */
7099 h = HASH_PTR(node->attr.con.tarval);
7105 * Calculate a hash value of a SymConst node.
7107 static unsigned hash_SymConst(const ir_node *node)
7111 /* all others are pointers */
7112 h = HASH_PTR(node->attr.symc.sym.type_p);
7117 void firm_set_default_hash(unsigned code, ir_op_ops *ops)
7121 ops->hash = hash_##a; \
7124 /* hash function already set */
7125 if (ops->hash != NULL)
7132 /* use input/mode default hash if no function was given */
7133 ops->hash = firm_default_hash;