2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
50 #include "firm_types.h"
51 #include "bitfiddle.h"
56 static bool is_Or_Eor_Add(const ir_node *node)
58 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
59 ir_node *left = get_binop_left(node);
60 ir_node *right = get_binop_right(node);
61 vrp_attr *vrp_left = vrp_get_info(left);
62 vrp_attr *vrp_right = vrp_get_info(right);
63 if (vrp_left != NULL && vrp_right != NULL) {
65 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
66 return tarval_is_null(vrp_val);
73 * Returns the tarval of a Const node or tarval_bad for all other nodes.
75 static ir_tarval *default_value_of(const ir_node *n)
78 return get_Const_tarval(n); /* might return tarval_bad */
83 value_of_func value_of_ptr = default_value_of;
85 void set_value_of_func(value_of_func func)
90 value_of_ptr = default_value_of;
94 * Return the value of a Constant.
96 static ir_tarval *computed_value_Const(const ir_node *n)
98 return get_Const_tarval(n);
102 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
104 static ir_tarval *computed_value_SymConst(const ir_node *n)
109 switch (get_SymConst_kind(n)) {
110 case symconst_type_size:
111 type = get_SymConst_type(n);
112 if (get_type_state(type) == layout_fixed)
113 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
115 case symconst_type_align:
116 type = get_SymConst_type(n);
117 if (get_type_state(type) == layout_fixed)
118 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
120 case symconst_ofs_ent:
121 ent = get_SymConst_entity(n);
122 type = get_entity_owner(ent);
123 if (get_type_state(type) == layout_fixed)
124 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
133 * Return the value of an Add.
135 static ir_tarval *computed_value_Add(const ir_node *n)
137 ir_node *a = get_Add_left(n);
138 ir_node *b = get_Add_right(n);
140 ir_tarval *ta = value_of(a);
141 ir_tarval *tb = value_of(b);
143 if ((ta != tarval_bad) && (tb != tarval_bad))
144 return tarval_add(ta, tb);
147 if ((is_Not(a) && get_Not_op(a) == b)
148 || (is_Not(b) && get_Not_op(b) == a)) {
149 return get_mode_all_one(get_irn_mode(n));
156 * Return the value of a Sub.
157 * Special case: a - a
159 static ir_tarval *computed_value_Sub(const ir_node *n)
161 ir_mode *mode = get_irn_mode(n);
162 ir_node *a = get_Sub_left(n);
163 ir_node *b = get_Sub_right(n);
168 if (! mode_is_float(mode)) {
171 return get_mode_null(mode);
177 if ((ta != tarval_bad) && (tb != tarval_bad))
178 return tarval_sub(ta, tb, mode);
184 * Return the value of a Carry.
185 * Special : a op 0, 0 op b
187 static ir_tarval *computed_value_Carry(const ir_node *n)
189 ir_node *a = get_binop_left(n);
190 ir_node *b = get_binop_right(n);
191 ir_mode *m = get_irn_mode(n);
192 ir_tarval *ta = value_of(a);
193 ir_tarval *tb = value_of(b);
195 if ((ta != tarval_bad) && (tb != tarval_bad)) {
197 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
199 if (tarval_is_null(ta) || tarval_is_null(tb))
200 return get_mode_null(m);
206 * Return the value of a Borrow.
209 static ir_tarval *computed_value_Borrow(const ir_node *n)
211 ir_node *a = get_binop_left(n);
212 ir_node *b = get_binop_right(n);
213 ir_mode *m = get_irn_mode(n);
214 ir_tarval *ta = value_of(a);
215 ir_tarval *tb = value_of(b);
217 if ((ta != tarval_bad) && (tb != tarval_bad)) {
218 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
219 } else if (tarval_is_null(ta)) {
220 return get_mode_null(m);
226 * Return the value of an unary Minus.
228 static ir_tarval *computed_value_Minus(const ir_node *n)
230 ir_node *a = get_Minus_op(n);
231 ir_tarval *ta = value_of(a);
233 if (ta != tarval_bad)
234 return tarval_neg(ta);
240 * Return the value of a Mul.
242 static ir_tarval *computed_value_Mul(const ir_node *n)
244 ir_node *a = get_Mul_left(n);
245 ir_node *b = get_Mul_right(n);
246 ir_tarval *ta = value_of(a);
247 ir_tarval *tb = value_of(b);
250 mode = get_irn_mode(n);
251 if (mode != get_irn_mode(a)) {
252 /* n * n = 2n bit multiplication */
253 ta = tarval_convert_to(ta, mode);
254 tb = tarval_convert_to(tb, mode);
257 if (ta != tarval_bad && tb != tarval_bad) {
258 return tarval_mul(ta, tb);
260 /* a * 0 != 0 if a == NaN or a == Inf */
261 if (!mode_is_float(mode)) {
262 /* a*0 = 0 or 0*b = 0 */
263 if (ta == get_mode_null(mode))
265 if (tb == get_mode_null(mode))
273 * Return the value of an And.
274 * Special case: a & 0, 0 & b
276 static ir_tarval *computed_value_And(const ir_node *n)
278 ir_node *a = get_And_left(n);
279 ir_node *b = get_And_right(n);
280 ir_tarval *ta = value_of(a);
281 ir_tarval *tb = value_of(b);
283 if ((ta != tarval_bad) && (tb != tarval_bad)) {
284 return tarval_and (ta, tb);
287 if (tarval_is_null(ta)) return ta;
288 if (tarval_is_null(tb)) return tb;
291 if ((is_Not(a) && get_Not_op(a) == b)
292 || (is_Not(b) && get_Not_op(b) == a)) {
293 return get_mode_null(get_irn_mode(n));
300 * Return the value of an Or.
301 * Special case: a | 1...1, 1...1 | b
303 static ir_tarval *computed_value_Or(const ir_node *n)
305 ir_node *a = get_Or_left(n);
306 ir_node *b = get_Or_right(n);
307 ir_tarval *ta = value_of(a);
308 ir_tarval *tb = value_of(b);
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_or (ta, tb);
314 if (tarval_is_all_one(ta)) return ta;
315 if (tarval_is_all_one(tb)) return tb;
318 if ((is_Not(a) && get_Not_op(a) == b)
319 || (is_Not(b) && get_Not_op(b) == a)) {
320 return get_mode_all_one(get_irn_mode(n));
326 * Return the value of an Eor.
328 static ir_tarval *computed_value_Eor(const ir_node *n)
330 ir_node *a = get_Eor_left(n);
331 ir_node *b = get_Eor_right(n);
336 return get_mode_null(get_irn_mode(n));
338 if ((is_Not(a) && get_Not_op(a) == b)
339 || (is_Not(b) && get_Not_op(b) == a)) {
340 return get_mode_all_one(get_irn_mode(n));
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_eor(ta, tb);
353 * Return the value of a Not.
355 static ir_tarval *computed_value_Not(const ir_node *n)
357 ir_node *a = get_Not_op(n);
358 ir_tarval *ta = value_of(a);
360 if (ta != tarval_bad)
361 return tarval_not(ta);
367 * Tests whether a shift shifts more bits than available in the mode
369 static bool is_oversize_shift(const ir_node *n)
371 ir_node *count = get_binop_right(n);
372 ir_mode *mode = get_irn_mode(n);
373 ir_tarval *tv = value_of(count);
376 if (tv == tarval_bad)
378 if (!tarval_is_long(tv))
380 shiftval = get_tarval_long(tv);
381 modulo_shift = get_mode_modulo_shift(mode);
382 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
385 return shiftval >= (long)get_mode_size_bits(mode);
389 * Return the value of a Shl.
391 static ir_tarval *computed_value_Shl(const ir_node *n)
393 ir_node *a = get_Shl_left(n);
394 ir_node *b = get_Shl_right(n);
396 ir_tarval *ta = value_of(a);
397 ir_tarval *tb = value_of(b);
399 if ((ta != tarval_bad) && (tb != tarval_bad)) {
400 return tarval_shl(ta, tb);
403 if (is_oversize_shift(n))
404 return get_mode_null(get_irn_mode(n));
410 * Return the value of a Shr.
412 static ir_tarval *computed_value_Shr(const ir_node *n)
414 ir_node *a = get_Shr_left(n);
415 ir_node *b = get_Shr_right(n);
417 ir_tarval *ta = value_of(a);
418 ir_tarval *tb = value_of(b);
420 if ((ta != tarval_bad) && (tb != tarval_bad)) {
421 return tarval_shr(ta, tb);
423 if (is_oversize_shift(n))
424 return get_mode_null(get_irn_mode(n));
430 * Return the value of a Shrs.
432 static ir_tarval *computed_value_Shrs(const ir_node *n)
434 ir_node *a = get_Shrs_left(n);
435 ir_node *b = get_Shrs_right(n);
437 ir_tarval *ta = value_of(a);
438 ir_tarval *tb = value_of(b);
440 if ((ta != tarval_bad) && (tb != tarval_bad)) {
441 return tarval_shrs(ta, tb);
447 * Return the value of a Rotl.
449 static ir_tarval *computed_value_Rotl(const ir_node *n)
451 ir_node *a = get_Rotl_left(n);
452 ir_node *b = get_Rotl_right(n);
454 ir_tarval *ta = value_of(a);
455 ir_tarval *tb = value_of(b);
457 if ((ta != tarval_bad) && (tb != tarval_bad)) {
458 return tarval_rotl(ta, tb);
463 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
465 ir_mode *mode = get_irn_mode(node);
466 if (get_mode_arithmetic(mode) != irma_twos_complement
467 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
471 ir_node *count = get_Shl_right(node);
472 if (is_Const(count)) {
473 ir_tarval *tv = get_Const_tarval(count);
474 if (tarval_is_long(tv)) {
475 long shiftval = get_tarval_long(tv);
476 long destbits = get_mode_size_bits(dest_mode);
477 if (shiftval >= destbits
478 && shiftval < (long)get_mode_modulo_shift(mode))
484 ir_node *right = get_And_right(node);
485 if (is_Const(right)) {
486 ir_tarval *tv = get_Const_tarval(right);
487 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
488 return tarval_is_null(conved);
495 * Return the value of a Conv.
497 static ir_tarval *computed_value_Conv(const ir_node *n)
499 ir_node *a = get_Conv_op(n);
500 ir_tarval *ta = value_of(a);
501 ir_mode *mode = get_irn_mode(n);
503 if (ta != tarval_bad)
504 return tarval_convert_to(ta, get_irn_mode(n));
506 if (ir_zero_when_converted(a, mode))
507 return get_mode_null(mode);
513 * Calculate the value of a Mux: can be evaluated, if the
514 * sel and the right input are known.
516 static ir_tarval *computed_value_Mux(const ir_node *n)
518 ir_node *sel = get_Mux_sel(n);
519 ir_tarval *ts = value_of(sel);
521 if (ts == get_tarval_b_true()) {
522 ir_node *v = get_Mux_true(n);
525 else if (ts == get_tarval_b_false()) {
526 ir_node *v = get_Mux_false(n);
533 * Calculate the value of a Confirm: can be evaluated,
534 * if it has the form Confirm(x, '=', Const).
536 static ir_tarval *computed_value_Confirm(const ir_node *n)
538 if (get_Confirm_relation(n) == ir_relation_equal) {
539 ir_tarval *tv = value_of(get_Confirm_bound(n));
540 if (tv != tarval_bad)
543 return value_of(get_Confirm_value(n));
547 * gives a (conservative) estimation of possible relation when comparing
550 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
551 const ir_node *right)
553 ir_relation possible = ir_relation_true;
554 ir_tarval *tv_l = value_of(left);
555 ir_tarval *tv_r = value_of(right);
556 ir_mode *mode = get_irn_mode(left);
557 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
558 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
560 /* both values known - evaluate them */
561 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
562 possible = tarval_cmp(tv_l, tv_r);
563 /* we can return now, won't get any better */
566 /* a == a is never less or greater (but might be equal or unordered) */
568 possible &= ~ir_relation_less_greater;
569 /* unordered results only happen for float compares */
570 if (!mode_is_float(mode))
571 possible &= ~ir_relation_unordered;
572 /* values can never be less than the least representable number or
573 * greater than the greatest representable number */
575 possible &= ~ir_relation_greater;
577 possible &= ~ir_relation_less;
579 possible &= ~ir_relation_greater;
581 possible &= ~ir_relation_less;
582 /* maybe vrp can tell us more */
583 possible &= vrp_cmp(left, right);
584 /* Alloc nodes never return null (but throw an exception) */
585 if (is_Alloc(left) && tarval_is_null(tv_r))
586 possible &= ~ir_relation_equal;
587 /* stuff known through confirm nodes */
588 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
589 possible &= get_Confirm_relation(left);
591 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
592 ir_relation relation = get_Confirm_relation(right);
593 relation = get_inversed_relation(relation);
594 possible &= relation;
600 static ir_tarval *compute_cmp(const ir_node *cmp)
602 ir_node *left = get_Cmp_left(cmp);
603 ir_node *right = get_Cmp_right(cmp);
604 ir_relation possible = ir_get_possible_cmp_relations(left, right);
605 ir_relation relation = get_Cmp_relation(cmp);
607 /* if none of the requested relations is possible, return false */
608 if ((possible & relation) == ir_relation_false)
609 return tarval_b_false;
610 /* if possible relations are a subset of the requested ones return true */
611 if ((possible & ~relation) == ir_relation_false)
612 return tarval_b_true;
614 return computed_value_Cmp_Confirm(cmp, left, right, relation);
618 * Return the value of a Cmp.
620 * The basic idea here is to determine which relations are possible and which
621 * one are definitely impossible.
623 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
625 /* we can't construct Constb after lowering mode_b nodes */
626 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
629 return compute_cmp(cmp);
633 * Calculate the value of an integer Div.
634 * Special case: 0 / b
636 static ir_tarval *do_computed_value_Div(const ir_node *div)
638 const ir_node *a = get_Div_left(div);
639 const ir_node *b = get_Div_right(div);
640 const ir_mode *mode = get_Div_resmode(div);
641 ir_tarval *ta = value_of(a);
643 const ir_node *dummy;
645 /* cannot optimize 0 / b = 0 because of NaN */
646 if (!mode_is_float(mode)) {
647 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
648 return ta; /* 0 / b == 0 if b != 0 */
651 if (ta != tarval_bad && tb != tarval_bad)
652 return tarval_div(ta, tb);
657 * Calculate the value of an integer Mod of two nodes.
658 * Special case: a % 1
660 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
662 ir_tarval *ta = value_of(a);
663 ir_tarval *tb = value_of(b);
665 /* Compute a % 1 or c1 % c2 */
666 if (tarval_is_one(tb))
667 return get_mode_null(get_irn_mode(a));
668 if (ta != tarval_bad && tb != tarval_bad)
669 return tarval_mod(ta, tb);
674 * Return the value of a Proj(Div).
676 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
678 long proj_nr = get_Proj_proj(n);
679 if (proj_nr != pn_Div_res)
682 return do_computed_value_Div(get_Proj_pred(n));
686 * Return the value of a Proj(Mod).
688 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
690 long proj_nr = get_Proj_proj(n);
692 if (proj_nr == pn_Mod_res) {
693 const ir_node *mod = get_Proj_pred(n);
694 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
700 * Return the value of a Proj.
702 static ir_tarval *computed_value_Proj(const ir_node *proj)
704 ir_node *n = get_Proj_pred(proj);
706 if (n->op->ops.computed_value_Proj != NULL)
707 return n->op->ops.computed_value_Proj(proj);
712 * If the parameter n can be computed, return its value, else tarval_bad.
713 * Performs constant folding.
715 * @param n The node this should be evaluated
717 ir_tarval *computed_value(const ir_node *n)
719 vrp_attr *vrp = vrp_get_info(n);
720 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
721 return vrp->bits_set;
723 if (n->op->ops.computed_value)
724 return n->op->ops.computed_value(n);
728 void firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
732 ops->computed_value = computed_value_##a; \
734 #define CASE_PROJ(a) \
736 ops->computed_value_Proj = computed_value_Proj_##a; \
772 * Optimize operations that are commutative and have neutral 0,
773 * so a op 0 = 0 op a = a.
775 static ir_node *equivalent_node_neutral_zero(ir_node *n)
779 ir_node *a = get_binop_left(n);
780 ir_node *b = get_binop_right(n);
785 /* After running compute_node there is only one constant predecessor.
786 Find this predecessors value and remember the other node: */
787 if ((tv = value_of(a)) != tarval_bad) {
789 } else if ((tv = value_of(b)) != tarval_bad) {
794 /* If this predecessors constant value is zero, the operation is
795 * unnecessary. Remove it.
797 * Beware: If n is a Add, the mode of on and n might be different
798 * which happens in this rare construction: NULL + 3.
799 * Then, a Conv would be needed which we cannot include here.
801 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
804 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
811 * Eor is commutative and has neutral 0.
813 static ir_node *equivalent_node_Eor(ir_node *n)
819 n = equivalent_node_neutral_zero(n);
820 if (n != oldn) return n;
823 b = get_Eor_right(n);
825 if (is_Eor(a) || is_Or_Eor_Add(a)) {
826 ir_node *aa = get_binop_left(a);
827 ir_node *ab = get_binop_right(a);
830 /* (a ^ b) ^ a -> b */
832 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
834 } else if (ab == b) {
835 /* (a ^ b) ^ b -> a */
837 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
841 if (is_Eor(b) || is_Or_Eor_Add(b)) {
842 ir_node *ba = get_binop_left(b);
843 ir_node *bb = get_binop_right(b);
846 /* a ^ (a ^ b) -> b */
848 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
850 } else if (bb == a) {
851 /* a ^ (b ^ a) -> b */
853 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
861 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
863 * The second one looks strange, but this construct
864 * is used heavily in the LCC sources :-).
866 * Beware: The Mode of an Add may be different than the mode of its
867 * predecessors, so we could not return a predecessors in all cases.
869 static ir_node *equivalent_node_Add(ir_node *n)
872 ir_node *left, *right;
873 ir_mode *mode = get_irn_mode(n);
875 n = equivalent_node_neutral_zero(n);
879 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
880 if (mode_is_float(mode)) {
881 ir_graph *irg = get_irn_irg(n);
882 if (get_irg_fp_model(irg) & fp_strict_algebraic)
886 left = get_Add_left(n);
887 right = get_Add_right(n);
890 if (get_Sub_right(left) == right) {
893 n = get_Sub_left(left);
894 if (mode == get_irn_mode(n)) {
895 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
901 if (get_Sub_right(right) == left) {
904 n = get_Sub_left(right);
905 if (mode == get_irn_mode(n)) {
906 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915 * optimize operations that are not commutative but have neutral 0 on left,
918 static ir_node *equivalent_node_left_zero(ir_node *n)
922 ir_node *a = get_binop_left(n);
923 ir_node *b = get_binop_right(n);
924 ir_tarval *tb = value_of(b);
926 if (tarval_is_null(tb)) {
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
934 #define equivalent_node_Shl equivalent_node_left_zero
935 #define equivalent_node_Shr equivalent_node_left_zero
936 #define equivalent_node_Shrs equivalent_node_left_zero
937 #define equivalent_node_Rotl equivalent_node_left_zero
940 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
942 * The second one looks strange, but this construct
943 * is used heavily in the LCC sources :-).
945 * Beware: The Mode of a Sub may be different than the mode of its
946 * predecessors, so we could not return a predecessors in all cases.
948 static ir_node *equivalent_node_Sub(ir_node *n)
952 ir_mode *mode = get_irn_mode(n);
955 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
956 if (mode_is_float(mode)) {
957 ir_graph *irg = get_irn_irg(n);
958 if (get_irg_fp_model(irg) & fp_strict_algebraic)
962 b = get_Sub_right(n);
965 /* Beware: modes might be different */
966 if (tarval_is_null(tb)) {
967 ir_node *a = get_Sub_left(n);
968 if (mode == get_irn_mode(a)) {
971 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
979 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
982 * -(-a) == a, but might overflow two times.
983 * We handle it anyway here but the better way would be a
984 * flag. This would be needed for Pascal for instance.
986 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
989 ir_node *pred = get_unop_op(n);
991 /* optimize symmetric unop */
992 if (get_irn_op(pred) == get_irn_op(n)) {
993 n = get_unop_op(pred);
994 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
999 /** Optimize Not(Not(x)) == x. */
1000 #define equivalent_node_Not equivalent_node_idempotent_unop
1002 /** -(-x) == x ??? Is this possible or can --x raise an
1003 out of bounds exception if min =! max? */
1004 #define equivalent_node_Minus equivalent_node_idempotent_unop
1007 * Optimize a * 1 = 1 * a = a.
1009 static ir_node *equivalent_node_Mul(ir_node *n)
1012 ir_node *a = get_Mul_left(n);
1014 /* we can handle here only the n * n = n bit cases */
1015 if (get_irn_mode(n) == get_irn_mode(a)) {
1016 ir_node *b = get_Mul_right(n);
1020 * Mul is commutative and has again an other neutral element.
1021 * Constants are place right, so check this case first.
1024 if (tarval_is_one(tv)) {
1026 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1029 if (tarval_is_one(tv)) {
1031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1039 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1041 static ir_node *equivalent_node_Or(ir_node *n)
1045 ir_node *a = get_Or_left(n);
1046 ir_node *b = get_Or_right(n);
1050 n = a; /* idempotence */
1051 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1054 /* constants are normalized to right, check this side first */
1056 if (tarval_is_null(tv)) {
1058 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1062 if (tarval_is_null(tv)) {
1064 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1072 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1074 static ir_node *equivalent_node_And(ir_node *n)
1078 ir_node *a = get_And_left(n);
1079 ir_node *b = get_And_right(n);
1083 n = a; /* idempotence */
1084 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1087 /* constants are normalized to right, check this side first */
1089 if (tarval_is_all_one(tv)) {
1091 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1094 if (tv != get_tarval_bad()) {
1095 ir_mode *mode = get_irn_mode(n);
1096 if (!mode_is_signed(mode) && is_Conv(a)) {
1097 ir_node *convop = get_Conv_op(a);
1098 ir_mode *convopmode = get_irn_mode(convop);
1099 if (!mode_is_signed(convopmode)) {
1100 /* Check Conv(all_one) & Const = all_one */
1101 ir_tarval *one = get_mode_all_one(convopmode);
1102 ir_tarval *conv = tarval_convert_to(one, mode);
1103 ir_tarval *and = tarval_and(conv, tv);
1105 if (tarval_is_all_one(and)) {
1106 /* Conv(X) & Const = X */
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1115 if (tarval_is_all_one(tv)) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1121 if ((is_Or(a) || is_Or_Eor_Add(a))
1122 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1124 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1128 if ((is_Or(b) || is_Or_Eor_Add(b))
1129 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1131 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 * Try to remove useless Conv's:
1140 static ir_node *equivalent_node_Conv(ir_node *n)
1143 ir_node *a = get_Conv_op(n);
1145 ir_mode *n_mode = get_irn_mode(n);
1146 ir_mode *a_mode = get_irn_mode(a);
1149 if (n_mode == a_mode) { /* No Conv necessary */
1150 if (get_Conv_strict(n)) {
1153 /* neither Minus nor Confirm change the precision,
1154 so we can "look-through" */
1157 p = get_Minus_op(p);
1158 } else if (is_Confirm(p)) {
1159 p = get_Confirm_value(p);
1165 if (is_Conv(p) && get_Conv_strict(p)) {
1166 /* we known already, that a_mode == n_mode, and neither
1167 Minus change the mode, so the second Conv
1169 assert(get_irn_mode(p) == n_mode);
1171 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1175 ir_node *pred = get_Proj_pred(p);
1176 if (is_Load(pred)) {
1177 /* Loads always return with the exact precision of n_mode */
1178 assert(get_Load_mode(pred) == n_mode);
1180 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1183 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1184 pred = get_Proj_pred(pred);
1185 if (is_Start(pred)) {
1186 /* Arguments always return with the exact precision,
1187 as strictConv's are place before Call -- if the
1188 caller was compiled with the same setting.
1189 Otherwise, the semantics is probably still right. */
1190 assert(get_irn_mode(p) == n_mode);
1192 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1198 /* special case: the immediate predecessor is also a Conv */
1199 if (! get_Conv_strict(a)) {
1200 /* first one is not strict, kick it */
1202 a_mode = get_irn_mode(a);
1206 /* else both are strict conv, second is superfluous */
1208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1213 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1216 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1217 ir_node *b = get_Conv_op(a);
1218 ir_mode *b_mode = get_irn_mode(b);
1220 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1221 /* both are strict conv */
1222 if (smaller_mode(a_mode, n_mode)) {
1223 /* both are strict, but the first is smaller, so
1224 the second cannot remove more precision, remove the
1226 set_Conv_strict(n, 0);
1229 if (n_mode == b_mode) {
1230 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1231 if (n_mode == mode_b) {
1232 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1233 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1235 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1236 if (values_in_mode(b_mode, a_mode)) {
1237 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1238 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1243 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1244 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1245 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1246 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1248 if (float_mantissa >= int_mantissa) {
1250 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1255 if (smaller_mode(b_mode, a_mode)) {
1256 if (get_Conv_strict(n))
1257 set_Conv_strict(b, 1);
1258 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1259 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1269 * - fold Phi-nodes, iff they have only one predecessor except
1272 static ir_node *equivalent_node_Phi(ir_node *n)
1277 ir_node *first_val = NULL; /* to shutup gcc */
1279 if (!get_opt_optimize() &&
1280 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1283 n_preds = get_Phi_n_preds(n);
1285 /* Phi of dead Region without predecessors. */
1289 /* Find first non-self-referencing input */
1290 for (i = 0; i < n_preds; ++i) {
1291 first_val = get_Phi_pred(n, i);
1292 /* not self pointer */
1293 if (first_val != n) {
1294 /* then found first value. */
1299 /* search for rest of inputs, determine if any of these
1300 are non-self-referencing */
1301 while (++i < n_preds) {
1302 ir_node *scnd_val = get_Phi_pred(n, i);
1303 if (scnd_val != n && scnd_val != first_val) {
1308 if (i >= n_preds && !is_Dummy(first_val)) {
1309 /* Fold, if no multiple distinct non-self-referencing inputs */
1311 DBG_OPT_PHI(oldn, n);
1317 * Optimize Proj(Tuple).
1319 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1321 ir_node *oldn = proj;
1322 ir_node *tuple = get_Proj_pred(proj);
1324 /* Remove the Tuple/Proj combination. */
1325 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1326 DBG_OPT_TUPLE(oldn, tuple, proj);
1332 * Optimize a / 1 = a.
1334 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1336 ir_node *oldn = proj;
1337 ir_node *div = get_Proj_pred(proj);
1338 ir_node *b = get_Div_right(div);
1339 ir_tarval *tb = value_of(b);
1341 /* Div is not commutative. */
1342 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1343 switch (get_Proj_proj(proj)) {
1345 proj = get_Div_mem(div);
1346 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1350 proj = get_Div_left(div);
1351 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1355 /* we cannot replace the exception Proj's here, this is done in
1356 transform_node_Proj_Div() */
1364 * Optimize CopyB(mem, x, x) into a Nop.
1366 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1368 ir_node *oldn = proj;
1369 ir_node *copyb = get_Proj_pred(proj);
1370 ir_node *a = get_CopyB_dst(copyb);
1371 ir_node *b = get_CopyB_src(copyb);
1374 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1375 switch (get_Proj_proj(proj)) {
1377 proj = get_CopyB_mem(copyb);
1378 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1386 * Optimize Bounds(idx, idx, upper) into idx.
1388 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1390 ir_node *oldn = proj;
1391 ir_node *bound = get_Proj_pred(proj);
1392 ir_node *idx = get_Bound_index(bound);
1393 ir_node *pred = skip_Proj(idx);
1396 if (idx == get_Bound_lower(bound))
1398 else if (is_Bound(pred)) {
1400 * idx was Bounds checked previously, it is still valid if
1401 * lower <= pred_lower && pred_upper <= upper.
1403 ir_node *lower = get_Bound_lower(bound);
1404 ir_node *upper = get_Bound_upper(bound);
1405 if (get_Bound_lower(pred) == lower &&
1406 get_Bound_upper(pred) == upper) {
1408 * One could expect that we simply return the previous
1409 * Bound here. However, this would be wrong, as we could
1410 * add an exception Proj to a new location then.
1411 * So, we must turn in into a tuple.
1417 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1418 switch (get_Proj_proj(proj)) {
1420 DBG_OPT_EXC_REM(proj);
1421 proj = get_Bound_mem(bound);
1425 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1428 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1436 * Does all optimizations on nodes that must be done on its Projs
1437 * because of creating new nodes.
1439 static ir_node *equivalent_node_Proj(ir_node *proj)
1441 ir_node *n = get_Proj_pred(proj);
1442 if (n->op->ops.equivalent_node_Proj)
1443 return n->op->ops.equivalent_node_Proj(proj);
1450 static ir_node *equivalent_node_Id(ir_node *n)
1458 DBG_OPT_ID(oldn, n);
1465 static ir_node *equivalent_node_Mux(ir_node *n)
1467 ir_node *oldn = n, *sel = get_Mux_sel(n);
1469 ir_tarval *ts = value_of(sel);
1471 if (ts == tarval_bad && is_Cmp(sel)) {
1472 /* try again with a direct call to compute_cmp, as we don't care
1473 * about the MODEB_LOWERED flag here */
1474 ts = compute_cmp(sel);
1477 /* Mux(true, f, t) == t */
1478 if (ts == tarval_b_true) {
1479 n = get_Mux_true(n);
1480 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1483 /* Mux(false, f, t) == f */
1484 if (ts == tarval_b_false) {
1485 n = get_Mux_false(n);
1486 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1489 n_t = get_Mux_true(n);
1490 n_f = get_Mux_false(n);
1492 /* Mux(v, x, T) == x */
1493 if (is_Unknown(n_f)) {
1495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1498 /* Mux(v, T, x) == x */
1499 if (is_Unknown(n_t)) {
1501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1505 /* Mux(v, x, x) == x */
1508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1511 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1512 ir_relation relation = get_Cmp_relation(sel);
1513 ir_node *f = get_Mux_false(n);
1514 ir_node *t = get_Mux_true(n);
1517 * Note further that these optimization work even for floating point
1518 * with NaN's because -NaN == NaN.
1519 * However, if +0 and -0 is handled differently, we cannot use the first one.
1521 ir_node *const cmp_l = get_Cmp_left(sel);
1522 ir_node *const cmp_r = get_Cmp_right(sel);
1525 case ir_relation_equal:
1526 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1527 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1529 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1534 case ir_relation_less_greater:
1535 case ir_relation_unordered_less_greater:
1536 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1537 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1539 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1548 * Note: normalization puts the constant on the right side,
1549 * so we check only one case.
1551 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1552 /* Mux(t CMP 0, X, t) */
1553 if (is_Minus(f) && get_Minus_op(f) == t) {
1554 /* Mux(t CMP 0, -t, t) */
1555 if (relation == ir_relation_equal) {
1556 /* Mux(t == 0, -t, t) ==> -t */
1558 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1559 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1560 /* Mux(t != 0, -t, t) ==> t */
1562 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1572 * Remove Confirm nodes if setting is on.
1573 * Replace Confirms(x, '=', Constlike) by Constlike.
1575 static ir_node *equivalent_node_Confirm(ir_node *n)
1577 ir_node *pred = get_Confirm_value(n);
1578 ir_relation relation = get_Confirm_relation(n);
1580 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1582 * rare case: two identical Confirms one after another,
1583 * replace the second one with the first.
1586 pred = get_Confirm_value(n);
1592 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1593 * perform no actual computation, as, e.g., the Id nodes. It does not create
1594 * new nodes. It is therefore safe to free n if the node returned is not n.
1595 * If a node returns a Tuple we can not just skip it. If the size of the
1596 * in array fits, we transform n into a tuple (e.g., Div).
1598 ir_node *equivalent_node(ir_node *n)
1600 if (n->op->ops.equivalent_node)
1601 return n->op->ops.equivalent_node(n);
1605 void firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1609 ops->equivalent_node = equivalent_node_##a; \
1611 #define CASE_PROJ(a) \
1613 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1648 * Returns non-zero if a node is a Phi node
1649 * with all predecessors constant.
1651 static int is_const_Phi(ir_node *n)
1655 if (! is_Phi(n) || get_irn_arity(n) == 0)
1657 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1658 if (! is_Const(get_irn_n(n, i)))
1664 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1665 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1668 * in reality eval_func should be tarval (*eval_func)() but incomplete
1669 * declarations are bad style and generate noisy warnings
1671 typedef void (*eval_func)(void);
1674 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1676 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1678 if (eval == (eval_func) tarval_sub) {
1679 tarval_sub_type func = (tarval_sub_type)eval;
1681 return func(a, b, mode);
1683 tarval_binop_type func = (tarval_binop_type)eval;
1690 * Apply an evaluator on a binop with a constant operators (and one Phi).
1692 * @param phi the Phi node
1693 * @param other the other operand
1694 * @param eval an evaluator function
1695 * @param mode the mode of the result, may be different from the mode of the Phi!
1696 * @param left if non-zero, other is the left operand, else the right
1698 * @return a new Phi node if the conversion was successful, NULL else
1700 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1706 int i, n = get_irn_arity(phi);
1708 NEW_ARR_A(void *, res, n);
1710 for (i = 0; i < n; ++i) {
1711 pred = get_irn_n(phi, i);
1712 tv = get_Const_tarval(pred);
1713 tv = do_eval(eval, other, tv, mode);
1715 if (tv == tarval_bad) {
1716 /* folding failed, bad */
1722 for (i = 0; i < n; ++i) {
1723 pred = get_irn_n(phi, i);
1724 tv = get_Const_tarval(pred);
1725 tv = do_eval(eval, tv, other, mode);
1727 if (tv == tarval_bad) {
1728 /* folding failed, bad */
1734 irg = get_irn_irg(phi);
1735 for (i = 0; i < n; ++i) {
1736 pred = get_irn_n(phi, i);
1737 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1739 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1743 * Apply an evaluator on a binop with two constant Phi.
1745 * @param a the left Phi node
1746 * @param b the right Phi node
1747 * @param eval an evaluator function
1748 * @param mode the mode of the result, may be different from the mode of the Phi!
1750 * @return a new Phi node if the conversion was successful, NULL else
1752 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1754 ir_tarval *tv_l, *tv_r, *tv;
1760 if (get_nodes_block(a) != get_nodes_block(b))
1763 n = get_irn_arity(a);
1764 NEW_ARR_A(void *, res, n);
1766 for (i = 0; i < n; ++i) {
1767 pred = get_irn_n(a, i);
1768 tv_l = get_Const_tarval(pred);
1769 pred = get_irn_n(b, i);
1770 tv_r = get_Const_tarval(pred);
1771 tv = do_eval(eval, tv_l, tv_r, mode);
1773 if (tv == tarval_bad) {
1774 /* folding failed, bad */
1779 irg = get_irn_irg(a);
1780 for (i = 0; i < n; ++i) {
1781 pred = get_irn_n(a, i);
1782 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1784 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1788 * Apply an evaluator on a unop with a constant operator (a Phi).
1790 * @param phi the Phi node
1791 * @param eval an evaluator function
1793 * @return a new Phi node if the conversion was successful, NULL else
1795 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1802 int i, n = get_irn_arity(phi);
1804 NEW_ARR_A(void *, res, n);
1805 for (i = 0; i < n; ++i) {
1806 pred = get_irn_n(phi, i);
1807 tv = get_Const_tarval(pred);
1810 if (tv == tarval_bad) {
1811 /* folding failed, bad */
1816 mode = get_irn_mode(phi);
1817 irg = get_irn_irg(phi);
1818 for (i = 0; i < n; ++i) {
1819 pred = get_irn_n(phi, i);
1820 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1822 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1826 * Apply a conversion on a constant operator (a Phi).
1828 * @param phi the Phi node
1830 * @return a new Phi node if the conversion was successful, NULL else
1832 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1838 int i, n = get_irn_arity(phi);
1840 NEW_ARR_A(void *, res, n);
1841 for (i = 0; i < n; ++i) {
1842 pred = get_irn_n(phi, i);
1843 tv = get_Const_tarval(pred);
1844 tv = tarval_convert_to(tv, mode);
1846 if (tv == tarval_bad) {
1847 /* folding failed, bad */
1852 irg = get_irn_irg(phi);
1853 for (i = 0; i < n; ++i) {
1854 pred = get_irn_n(phi, i);
1855 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1857 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1861 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1862 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1863 * If possible, remove the Conv's.
1865 static ir_node *transform_node_AddSub(ir_node *n)
1867 ir_mode *mode = get_irn_mode(n);
1869 if (mode_is_reference(mode)) {
1870 ir_node *left = get_binop_left(n);
1871 ir_node *right = get_binop_right(n);
1872 unsigned ref_bits = get_mode_size_bits(mode);
1874 if (is_Conv(left)) {
1875 ir_mode *lmode = get_irn_mode(left);
1876 unsigned bits = get_mode_size_bits(lmode);
1878 if (ref_bits == bits &&
1879 mode_is_int(lmode) &&
1880 get_mode_arithmetic(lmode) == irma_twos_complement) {
1881 ir_node *pre = get_Conv_op(left);
1882 ir_mode *pre_mode = get_irn_mode(pre);
1884 if (mode_is_int(pre_mode) &&
1885 get_mode_size_bits(pre_mode) == bits &&
1886 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1887 /* ok, this conv just changes to sign, moreover the calculation
1888 * is done with same number of bits as our address mode, so
1889 * we can ignore the conv as address calculation can be viewed
1890 * as either signed or unsigned
1892 set_binop_left(n, pre);
1897 if (is_Conv(right)) {
1898 ir_mode *rmode = get_irn_mode(right);
1899 unsigned bits = get_mode_size_bits(rmode);
1901 if (ref_bits == bits &&
1902 mode_is_int(rmode) &&
1903 get_mode_arithmetic(rmode) == irma_twos_complement) {
1904 ir_node *pre = get_Conv_op(right);
1905 ir_mode *pre_mode = get_irn_mode(pre);
1907 if (mode_is_int(pre_mode) &&
1908 get_mode_size_bits(pre_mode) == bits &&
1909 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1910 /* ok, this conv just changes to sign, moreover the calculation
1911 * is done with same number of bits as our address mode, so
1912 * we can ignore the conv as address calculation can be viewed
1913 * as either signed or unsigned
1915 set_binop_right(n, pre);
1920 /* let address arithmetic use unsigned modes */
1921 if (is_Const(right)) {
1922 ir_mode *rmode = get_irn_mode(right);
1924 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1925 /* convert a AddP(P, *s) into AddP(P, *u) */
1926 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1928 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1929 set_binop_right(n, pre);
1937 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1940 if (is_Const(b) && is_const_Phi(a)) { \
1941 /* check for Op(Phi, Const) */ \
1942 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1944 else if (is_Const(a) && is_const_Phi(b)) { \
1945 /* check for Op(Const, Phi) */ \
1946 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1948 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1949 /* check for Op(Phi, Phi) */ \
1950 c = apply_binop_on_2_phis(a, b, eval, mode); \
1953 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1958 #define HANDLE_UNOP_PHI(eval, a, c) \
1961 if (is_const_Phi(a)) { \
1962 /* check for Op(Phi) */ \
1963 c = apply_unop_on_phi(a, eval); \
1965 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1972 * Create a 0 constant of given mode.
1974 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1976 ir_tarval *tv = get_mode_null(mode);
1977 ir_node *cnst = new_r_Const(irg, tv);
1982 static bool is_shiftop(const ir_node *n)
1984 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1987 /* the order of the values is important! */
1988 typedef enum const_class {
1994 static const_class classify_const(const ir_node* n)
1996 if (is_Const(n)) return const_const;
1997 if (is_irn_constlike(n)) return const_like;
2002 * Determines whether r is more constlike or has a larger index (in that order)
2005 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2007 const const_class l_order = classify_const(l);
2008 const const_class r_order = classify_const(r);
2010 l_order > r_order ||
2011 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2014 static bool is_cmp_unequal(const ir_node *node)
2016 ir_relation relation = get_Cmp_relation(node);
2017 ir_node *left = get_Cmp_left(node);
2018 ir_node *right = get_Cmp_right(node);
2019 ir_mode *mode = get_irn_mode(left);
2021 if (relation == ir_relation_less_greater)
2024 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2025 return relation == ir_relation_greater;
2030 * returns true for Cmp(x == 0) or Cmp(x != 0)
2032 static bool is_cmp_equality_zero(const ir_node *node)
2034 ir_relation relation;
2035 ir_node *right = get_Cmp_right(node);
2037 if (!is_Const(right) || !is_Const_null(right))
2039 relation = get_Cmp_relation(node);
2040 return relation == ir_relation_equal
2041 || relation == ir_relation_less_greater
2042 || (!mode_is_signed(get_irn_mode(right))
2043 && relation == ir_relation_greater);
2047 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2048 * Such pattern may arise in bitfield stores.
2050 * value c4 value c4 & c2
2051 * AND c3 AND c1 | c3
2058 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2061 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2063 ir_node *irn_and, *c1;
2065 ir_node *and_l, *c3;
2066 ir_node *value, *c4;
2067 ir_node *new_and, *new_const, *block;
2068 ir_mode *mode = get_irn_mode(irn_or);
2070 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2074 irn_and = get_binop_left(irn_or);
2075 c1 = get_binop_right(irn_or);
2076 if (!is_Const(c1) || !is_And(irn_and))
2079 or_l = get_binop_left(irn_and);
2080 c2 = get_binop_right(irn_and);
2084 tv1 = get_Const_tarval(c1);
2085 tv2 = get_Const_tarval(c2);
2087 tv = tarval_or(tv1, tv2);
2088 if (tarval_is_all_one(tv)) {
2089 /* the AND does NOT clear a bit with isn't set by the OR */
2090 set_binop_left(irn_or, or_l);
2091 set_binop_right(irn_or, c1);
2093 /* check for more */
2097 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2100 and_l = get_binop_left(or_l);
2101 c3 = get_binop_right(or_l);
2102 if (!is_Const(c3) || !is_And(and_l))
2105 value = get_binop_left(and_l);
2106 c4 = get_binop_right(and_l);
2110 /* ok, found the pattern, check for conditions */
2111 assert(mode == get_irn_mode(irn_and));
2112 assert(mode == get_irn_mode(or_l));
2113 assert(mode == get_irn_mode(and_l));
2115 tv3 = get_Const_tarval(c3);
2116 tv4 = get_Const_tarval(c4);
2118 tv = tarval_or(tv4, tv2);
2119 if (!tarval_is_all_one(tv)) {
2120 /* have at least one 0 at the same bit position */
2124 if (tv3 != tarval_andnot(tv3, tv4)) {
2125 /* bit in the or_mask is outside the and_mask */
2129 if (tv1 != tarval_andnot(tv1, tv2)) {
2130 /* bit in the or_mask is outside the and_mask */
2134 /* ok, all conditions met */
2135 block = get_irn_n(irn_or, -1);
2136 irg = get_irn_irg(block);
2138 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2140 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2142 set_binop_left(irn_or, new_and);
2143 set_binop_right(irn_or, new_const);
2145 /* check for more */
2150 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2152 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2154 ir_mode *mode = get_irn_mode(irn_or);
2155 ir_node *shl, *shr, *block;
2156 ir_node *irn, *x, *c1, *c2, *n;
2157 ir_tarval *tv1, *tv2;
2159 /* some backends can't handle rotl */
2160 if (!be_get_backend_param()->support_rotl)
2163 if (! mode_is_int(mode))
2166 shl = get_binop_left(irn_or);
2167 shr = get_binop_right(irn_or);
2176 } else if (!is_Shl(shl)) {
2178 } else if (!is_Shr(shr)) {
2181 x = get_Shl_left(shl);
2182 if (x != get_Shr_left(shr))
2185 c1 = get_Shl_right(shl);
2186 c2 = get_Shr_right(shr);
2187 if (is_Const(c1) && is_Const(c2)) {
2188 tv1 = get_Const_tarval(c1);
2189 if (! tarval_is_long(tv1))
2192 tv2 = get_Const_tarval(c2);
2193 if (! tarval_is_long(tv2))
2196 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2197 != (int) get_mode_size_bits(mode))
2200 /* yet, condition met */
2201 block = get_nodes_block(irn_or);
2203 n = new_r_Rotl(block, x, c1, mode);
2205 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2209 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2210 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2211 if (!ir_is_negated_value(c1, c2)) {
2215 /* yet, condition met */
2216 block = get_nodes_block(irn_or);
2217 n = new_r_Rotl(block, x, c1, mode);
2218 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2223 * Prototype of a recursive transform function
2224 * for bitwise distributive transformations.
2226 typedef ir_node* (*recursive_transform)(ir_node *n);
2229 * makes use of distributive laws for and, or, eor
2230 * and(a OP c, b OP c) -> and(a, b) OP c
2231 * note, might return a different op than n
2233 static ir_node *transform_bitwise_distributive(ir_node *n,
2234 recursive_transform trans_func)
2237 ir_node *a = get_binop_left(n);
2238 ir_node *b = get_binop_right(n);
2239 ir_op *op = get_irn_op(a);
2240 ir_op *op_root = get_irn_op(n);
2242 if (op != get_irn_op(b))
2245 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2246 if (op == op_Conv) {
2247 ir_node *a_op = get_Conv_op(a);
2248 ir_node *b_op = get_Conv_op(b);
2249 ir_mode *a_mode = get_irn_mode(a_op);
2250 ir_mode *b_mode = get_irn_mode(b_op);
2251 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2252 ir_node *blk = get_nodes_block(n);
2255 set_binop_left(n, a_op);
2256 set_binop_right(n, b_op);
2257 set_irn_mode(n, a_mode);
2259 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2267 /* nothing to gain here */
2271 if (op == op_Shrs || op == op_Shr || op == op_Shl
2272 || op == op_And || op == op_Or || op == op_Eor) {
2273 ir_node *a_left = get_binop_left(a);
2274 ir_node *a_right = get_binop_right(a);
2275 ir_node *b_left = get_binop_left(b);
2276 ir_node *b_right = get_binop_right(b);
2278 ir_node *op1 = NULL;
2279 ir_node *op2 = NULL;
2281 if (is_op_commutative(op)) {
2282 if (a_left == b_left) {
2286 } else if (a_left == b_right) {
2290 } else if (a_right == b_left) {
2296 if (a_right == b_right) {
2303 /* (a sop c) & (b sop c) => (a & b) sop c */
2304 ir_node *blk = get_nodes_block(n);
2306 ir_node *new_n = exact_copy(n);
2307 set_binop_left(new_n, op1);
2308 set_binop_right(new_n, op2);
2309 new_n = trans_func(new_n);
2311 if (op_root == op_Eor && op == op_Or) {
2312 dbg_info *dbgi = get_irn_dbg_info(n);
2313 ir_mode *mode = get_irn_mode(c);
2315 c = new_rd_Not(dbgi, blk, c, mode);
2316 n = new_rd_And(dbgi, blk, new_n, c, mode);
2319 set_nodes_block(n, blk);
2320 set_binop_left(n, new_n);
2321 set_binop_right(n, c);
2325 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2334 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2336 * - and, or, xor instead of &
2337 * - Shl, Shr, Shrs, rotl instead of >>
2338 * (with a special case for Or/Xor + Shrs)
2340 * This normalisation is usually good for the backend since << C can often be
2341 * matched as address-mode.
2343 static ir_node *transform_node_bitop_shift(ir_node *n)
2345 ir_graph *irg = get_irn_irg(n);
2346 ir_node *left = get_binop_left(n);
2347 ir_node *right = get_binop_right(n);
2348 ir_mode *mode = get_irn_mode(n);
2349 ir_node *shift_left;
2350 ir_node *shift_right;
2352 dbg_info *dbg_bitop;
2353 dbg_info *dbg_shift;
2359 ir_tarval *tv_bitop;
2361 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2364 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2365 if (!is_Const(right) || !is_shiftop(left))
2368 shift_left = get_binop_left(left);
2369 shift_right = get_binop_right(left);
2370 if (!is_Const(shift_right))
2373 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2374 if (is_Shrs(left)) {
2375 /* TODO this could be improved */
2379 irg = get_irn_irg(n);
2380 block = get_nodes_block(n);
2381 dbg_bitop = get_irn_dbg_info(n);
2382 dbg_shift = get_irn_dbg_info(left);
2383 tv1 = get_Const_tarval(shift_right);
2384 tv2 = get_Const_tarval(right);
2385 assert(get_tarval_mode(tv2) == mode);
2388 tv_bitop = tarval_shr(tv2, tv1);
2390 /* Check whether we have lost some bits during the right shift. */
2392 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2394 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2397 } else if (is_Shr(left)) {
2400 * TODO this can be improved by checking whether
2401 * the left shift produces an overflow
2405 tv_bitop = tarval_shl(tv2, tv1);
2407 assert(is_Rotl(left));
2408 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2410 new_const = new_r_Const(irg, tv_bitop);
2413 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2414 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2415 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2418 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2422 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2423 } else if (is_Shr(left)) {
2424 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2426 assert(is_Rotl(left));
2427 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2433 static bool complement_values(const ir_node *a, const ir_node *b)
2435 if (is_Not(a) && get_Not_op(a) == b)
2437 if (is_Not(b) && get_Not_op(b) == a)
2439 if (is_Const(a) && is_Const(b)) {
2440 ir_tarval *tv_a = get_Const_tarval(a);
2441 ir_tarval *tv_b = get_Const_tarval(b);
2442 return tarval_not(tv_a) == tv_b;
2447 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2450 * for associative operations fold:
2451 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2452 * This is a "light" version of the reassociation phase
2454 static ir_node *fold_constant_associativity(ir_node *node,
2455 tv_fold_binop_func fold)
2460 ir_node *right = get_binop_right(node);
2461 ir_node *left_right;
2468 if (!is_Const(right))
2471 op = get_irn_op(node);
2472 left = get_binop_left(node);
2473 if (get_irn_op(left) != op)
2476 left_right = get_binop_right(left);
2477 if (!is_Const(left_right))
2480 left_left = get_binop_left(left);
2481 c0 = get_Const_tarval(left_right);
2482 c1 = get_Const_tarval(right);
2483 irg = get_irn_irg(node);
2484 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2486 new_c = fold(c0, c1);
2487 if (new_c == tarval_bad)
2489 new_const = new_r_Const(irg, new_c);
2490 new_node = exact_copy(node);
2491 set_binop_left(new_node, left_left);
2492 set_binop_right(new_node, new_const);
2499 static ir_node *transform_node_Or_(ir_node *n)
2502 ir_node *a = get_binop_left(n);
2503 ir_node *b = get_binop_right(n);
2507 n = fold_constant_associativity(n, tarval_or);
2511 if (is_Not(a) && is_Not(b)) {
2512 /* ~a | ~b = ~(a&b) */
2513 ir_node *block = get_nodes_block(n);
2515 mode = get_irn_mode(n);
2518 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2519 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2524 /* we can combine the relations of two compares with the same operands */
2525 if (is_Cmp(a) && is_Cmp(b)) {
2526 ir_node *a_left = get_Cmp_left(a);
2527 ir_node *a_right = get_Cmp_right(a);
2528 ir_node *b_left = get_Cmp_left(b);
2529 ir_node *b_right = get_Cmp_right(b);
2530 if (a_left == b_left && b_left == b_right) {
2531 dbg_info *dbgi = get_irn_dbg_info(n);
2532 ir_node *block = get_nodes_block(n);
2533 ir_relation a_relation = get_Cmp_relation(a);
2534 ir_relation b_relation = get_Cmp_relation(b);
2535 ir_relation new_relation = a_relation | b_relation;
2536 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2538 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2539 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2540 && !mode_is_float(get_irn_mode(a_left))
2541 && !mode_is_float(get_irn_mode(b_left))) {
2542 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2543 ir_graph *irg = get_irn_irg(n);
2544 dbg_info *dbgi = get_irn_dbg_info(n);
2545 ir_node *block = get_nodes_block(n);
2546 ir_mode *a_mode = get_irn_mode(a_left);
2547 ir_mode *b_mode = get_irn_mode(b_left);
2548 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2549 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2550 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2551 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2552 ir_node *zero = create_zero_const(irg, b_mode);
2553 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2555 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2556 ir_graph *irg = get_irn_irg(n);
2557 dbg_info *dbgi = get_irn_dbg_info(n);
2558 ir_node *block = get_nodes_block(n);
2559 ir_mode *a_mode = get_irn_mode(a_left);
2560 ir_mode *b_mode = get_irn_mode(b_left);
2561 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2562 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2563 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2564 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2565 ir_node *zero = create_zero_const(irg, a_mode);
2566 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2571 mode = get_irn_mode(n);
2572 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2574 n = transform_node_Or_bf_store(n);
2577 n = transform_node_Or_Rotl(n);
2581 n = transform_bitwise_distributive(n, transform_node_Or_);
2584 n = transform_node_bitop_shift(n);
2591 static ir_node *transform_node_Or(ir_node *n)
2593 if (is_Or_Eor_Add(n)) {
2594 dbg_info *dbgi = get_irn_dbg_info(n);
2595 ir_node *block = get_nodes_block(n);
2596 ir_node *left = get_Or_left(n);
2597 ir_node *right = get_Or_right(n);
2598 ir_mode *mode = get_irn_mode(n);
2599 return new_rd_Add(dbgi, block, left, right, mode);
2601 return transform_node_Or_(n);
2607 static ir_node *transform_node_Eor_(ir_node *n)
2610 ir_node *a = get_binop_left(n);
2611 ir_node *b = get_binop_right(n);
2612 ir_mode *mode = get_irn_mode(n);
2615 n = fold_constant_associativity(n, tarval_eor);
2619 /* we can combine the relations of two compares with the same operands */
2620 if (is_Cmp(a) && is_Cmp(b)) {
2621 ir_node *a_left = get_Cmp_left(a);
2622 ir_node *a_right = get_Cmp_left(a);
2623 ir_node *b_left = get_Cmp_left(b);
2624 ir_node *b_right = get_Cmp_right(b);
2625 if (a_left == b_left && b_left == b_right) {
2626 dbg_info *dbgi = get_irn_dbg_info(n);
2627 ir_node *block = get_nodes_block(n);
2628 ir_relation a_relation = get_Cmp_relation(a);
2629 ir_relation b_relation = get_Cmp_relation(b);
2630 ir_relation new_relation = a_relation ^ b_relation;
2631 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2635 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2637 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2638 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2639 dbg_info *dbg = get_irn_dbg_info(n);
2640 ir_node *block = get_nodes_block(n);
2641 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2642 ir_node *new_left = get_Not_op(a);
2643 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2644 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2646 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2647 dbg_info *dbg = get_irn_dbg_info(n);
2648 ir_node *block = get_nodes_block(n);
2649 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2650 ir_node *new_right = get_Not_op(b);
2651 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2652 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2656 /* x ^ 1...1 -> ~1 */
2657 if (is_Const(b) && is_Const_all_one(b)) {
2658 n = new_r_Not(get_nodes_block(n), a, mode);
2659 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2663 n = transform_bitwise_distributive(n, transform_node_Eor_);
2666 n = transform_node_bitop_shift(n);
2673 static ir_node *transform_node_Eor(ir_node *n)
2675 if (is_Or_Eor_Add(n)) {
2676 dbg_info *dbgi = get_irn_dbg_info(n);
2677 ir_node *block = get_nodes_block(n);
2678 ir_node *left = get_Eor_left(n);
2679 ir_node *right = get_Eor_right(n);
2680 ir_mode *mode = get_irn_mode(n);
2681 return new_rd_Add(dbgi, block, left, right, mode);
2683 return transform_node_Eor_(n);
2687 * Do the AddSub optimization, then Transform
2688 * Constant folding on Phi
2689 * Add(a,a) -> Mul(a, 2)
2690 * Add(Mul(a, x), a) -> Mul(a, x+1)
2691 * if the mode is integer or float.
2692 * Transform Add(a,-b) into Sub(a,b).
2693 * Reassociation might fold this further.
2695 static ir_node *transform_node_Add(ir_node *n)
2703 n = fold_constant_associativity(n, tarval_add);
2707 n = transform_node_AddSub(n);
2711 a = get_Add_left(n);
2712 b = get_Add_right(n);
2713 mode = get_irn_mode(n);
2715 if (mode_is_reference(mode)) {
2716 ir_mode *lmode = get_irn_mode(a);
2718 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2719 /* an Add(a, NULL) is a hidden Conv */
2720 dbg_info *dbg = get_irn_dbg_info(n);
2721 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2725 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2726 ir_tarval *tv = get_Const_tarval(b);
2727 ir_tarval *min = get_mode_min(mode);
2728 /* if all bits are set, then this has the same effect as a Not.
2729 * Note that the following == gives false for different modes which
2730 * is exactly what we want */
2732 dbg_info *dbgi = get_irn_dbg_info(n);
2733 ir_graph *irg = get_irn_irg(n);
2734 ir_node *block = get_nodes_block(n);
2735 ir_node *cnst = new_r_Const(irg, min);
2736 return new_rd_Eor(dbgi, block, a, cnst, mode);
2740 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2742 /* for FP the following optimizations are only allowed if
2743 * fp_strict_algebraic is disabled */
2744 if (mode_is_float(mode)) {
2745 ir_graph *irg = get_irn_irg(n);
2746 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2750 if (mode_is_num(mode)) {
2751 ir_graph *irg = get_irn_irg(n);
2752 /* the following code leads to endless recursion when Mul are replaced
2753 * by a simple instruction chain */
2754 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2755 && a == b && mode_is_int(mode)) {
2756 ir_node *block = get_nodes_block(n);
2759 get_irn_dbg_info(n),
2762 new_r_Const_long(irg, mode, 2),
2764 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2769 get_irn_dbg_info(n),
2774 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2779 get_irn_dbg_info(n),
2784 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2787 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2788 /* Here we rely on constants be on the RIGHT side */
2790 ir_node *op = get_Not_op(a);
2792 if (is_Const(b) && is_Const_one(b)) {
2794 ir_node *blk = get_nodes_block(n);
2795 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2796 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2803 if (is_Or_Eor_Add(n)) {
2804 n = transform_node_Or_(n);
2807 n = transform_node_Eor_(n);
2816 * returns -cnst or NULL if impossible
2818 static ir_node *const_negate(ir_node *cnst)
2820 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2821 dbg_info *dbgi = get_irn_dbg_info(cnst);
2822 ir_graph *irg = get_irn_irg(cnst);
2823 if (tv == tarval_bad) return NULL;
2824 return new_rd_Const(dbgi, irg, tv);
2828 * Do the AddSub optimization, then Transform
2829 * Constant folding on Phi
2830 * Sub(0,a) -> Minus(a)
2831 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2832 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2833 * Sub(Add(a, x), x) -> a
2834 * Sub(x, Add(x, a)) -> -a
2835 * Sub(x, Const) -> Add(x, -Const)
2837 static ir_node *transform_node_Sub(ir_node *n)
2843 n = transform_node_AddSub(n);
2845 a = get_Sub_left(n);
2846 b = get_Sub_right(n);
2848 mode = get_irn_mode(n);
2850 if (mode_is_int(mode)) {
2851 ir_mode *lmode = get_irn_mode(a);
2853 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2854 /* a Sub(a, NULL) is a hidden Conv */
2855 dbg_info *dbg = get_irn_dbg_info(n);
2856 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2857 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2861 if (mode == lmode &&
2862 get_mode_arithmetic(mode) == irma_twos_complement &&
2864 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2866 dbg_info *dbg = get_irn_dbg_info(n);
2867 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2868 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2874 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2876 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2877 if (mode_is_float(mode)) {
2878 ir_graph *irg = get_irn_irg(n);
2879 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2883 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2884 /* a - C -> a + (-C) */
2885 ir_node *cnst = const_negate(b);
2887 ir_node *block = get_nodes_block(n);
2888 dbg_info *dbgi = get_irn_dbg_info(n);
2890 n = new_rd_Add(dbgi, block, a, cnst, mode);
2891 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2896 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2897 dbg_info *dbg = get_irn_dbg_info(n);
2898 ir_node *block = get_nodes_block(n);
2899 ir_node *left = get_Minus_op(a);
2900 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2902 n = new_rd_Minus(dbg, block, add, mode);
2903 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2905 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2906 dbg_info *dbg = get_irn_dbg_info(n);
2907 ir_node *block = get_nodes_block(n);
2908 ir_node *right = get_Minus_op(b);
2910 n = new_rd_Add(dbg, block, a, right, mode);
2911 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2913 } else if (is_Sub(b)) {
2914 /* a - (b - c) -> a + (c - b)
2915 * -> (a - b) + c iff (b - c) is a pointer */
2916 dbg_info *s_dbg = get_irn_dbg_info(b);
2917 ir_node *s_left = get_Sub_left(b);
2918 ir_node *s_right = get_Sub_right(b);
2919 ir_mode *s_mode = get_irn_mode(b);
2920 if (mode_is_reference(s_mode)) {
2921 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2922 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2923 dbg_info *a_dbg = get_irn_dbg_info(n);
2926 s_right = new_r_Conv(lowest_block, s_right, mode);
2927 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2929 ir_node *s_block = get_nodes_block(b);
2930 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2931 dbg_info *a_dbg = get_irn_dbg_info(n);
2932 ir_node *a_block = get_nodes_block(n);
2934 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2936 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2939 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2940 ir_node *m_right = get_Mul_right(b);
2941 if (is_Const(m_right)) {
2942 ir_node *cnst2 = const_negate(m_right);
2943 if (cnst2 != NULL) {
2944 dbg_info *m_dbg = get_irn_dbg_info(b);
2945 ir_node *m_block = get_nodes_block(b);
2946 ir_node *m_left = get_Mul_left(b);
2947 ir_mode *m_mode = get_irn_mode(b);
2948 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2949 dbg_info *a_dbg = get_irn_dbg_info(n);
2950 ir_node *a_block = get_nodes_block(n);
2952 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2953 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2960 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2961 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2963 get_irn_dbg_info(n),
2967 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2970 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2971 ir_node *left = get_binop_left(a);
2972 ir_node *right = get_binop_right(a);
2974 /* FIXME: Does the Conv's work only for two complement or generally? */
2976 if (mode != get_irn_mode(right)) {
2977 /* This Sub is an effective Cast */
2978 right = new_r_Conv(get_nodes_block(n), right, mode);
2981 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2983 } else if (right == b) {
2984 if (mode != get_irn_mode(left)) {
2985 /* This Sub is an effective Cast */
2986 left = new_r_Conv(get_nodes_block(n), left, mode);
2989 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2993 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2994 ir_node *left = get_binop_left(b);
2995 ir_node *right = get_binop_right(b);
2997 /* FIXME: Does the Conv's work only for two complement or generally? */
2999 ir_mode *r_mode = get_irn_mode(right);
3001 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3002 if (mode != r_mode) {
3003 /* This Sub is an effective Cast */
3004 n = new_r_Conv(get_nodes_block(n), n, mode);
3006 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3008 } else if (right == a) {
3009 ir_mode *l_mode = get_irn_mode(left);
3011 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3012 if (mode != l_mode) {
3013 /* This Sub is an effective Cast */
3014 n = new_r_Conv(get_nodes_block(n), n, mode);
3016 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3020 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3021 ir_mode *mode = get_irn_mode(a);
3023 if (mode == get_irn_mode(b)) {
3025 ir_node *op_a = get_Conv_op(a);
3026 ir_node *op_b = get_Conv_op(b);
3028 /* check if it's allowed to skip the conv */
3029 ma = get_irn_mode(op_a);
3030 mb = get_irn_mode(op_b);
3032 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3033 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3036 set_Sub_right(n, b);
3042 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3043 if (!is_reassoc_running() && is_Mul(a)) {
3044 ir_node *ma = get_Mul_left(a);
3045 ir_node *mb = get_Mul_right(a);
3048 ir_node *blk = get_nodes_block(n);
3049 ir_graph *irg = get_irn_irg(n);
3051 get_irn_dbg_info(n),
3055 get_irn_dbg_info(n),
3058 new_r_Const(irg, get_mode_one(mode)),
3061 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3063 } else if (mb == b) {
3064 ir_node *blk = get_nodes_block(n);
3065 ir_graph *irg = get_irn_irg(n);
3067 get_irn_dbg_info(n),
3071 get_irn_dbg_info(n),
3074 new_r_Const(irg, get_mode_one(mode)),
3077 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3081 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3082 ir_node *x = get_Sub_left(a);
3083 ir_node *y = get_Sub_right(a);
3084 ir_node *blk = get_nodes_block(n);
3085 ir_mode *m_b = get_irn_mode(b);
3086 ir_mode *m_y = get_irn_mode(y);
3090 /* Determine the right mode for the Add. */
3093 else if (mode_is_reference(m_b))
3095 else if (mode_is_reference(m_y))
3099 * Both modes are different but none is reference,
3100 * happens for instance in SubP(SubP(P, Iu), Is).
3101 * We have two possibilities here: Cast or ignore.
3102 * Currently we ignore this case.
3107 add = new_r_Add(blk, y, b, add_mode);
3109 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3110 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3114 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3115 /* c - ~X = X + (c+1) */
3116 if (is_Const(a) && is_Not(b)) {
3117 ir_tarval *tv = get_Const_tarval(a);
3119 tv = tarval_add(tv, get_mode_one(mode));
3120 if (tv != tarval_bad) {
3121 ir_node *blk = get_nodes_block(n);
3122 ir_graph *irg = get_irn_irg(n);
3123 ir_node *c = new_r_Const(irg, tv);
3124 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3125 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3129 /* x-(x&y) = x & ~y */
3131 ir_node *and_left = get_And_left(b);
3132 ir_node *and_right = get_And_right(b);
3133 if (and_right == a) {
3134 ir_node *tmp = and_left;
3135 and_left = and_right;
3138 if (and_left == a) {
3139 dbg_info *dbgi = get_irn_dbg_info(n);
3140 ir_node *block = get_nodes_block(n);
3141 ir_mode *mode = get_irn_mode(n);
3142 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3143 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3152 * Several transformation done on n*n=2n bits mul.
3153 * These transformations must be done here because new nodes may be produced.
3155 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3158 ir_node *a = get_Mul_left(n);
3159 ir_node *b = get_Mul_right(n);
3160 ir_tarval *ta = value_of(a);
3161 ir_tarval *tb = value_of(b);
3162 ir_mode *smode = get_irn_mode(a);
3164 if (ta == get_mode_one(smode)) {
3165 /* (L)1 * (L)b = (L)b */
3166 ir_node *blk = get_nodes_block(n);
3167 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3168 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3171 else if (ta == get_mode_minus_one(smode)) {
3172 /* (L)-1 * (L)b = (L)b */
3173 ir_node *blk = get_nodes_block(n);
3174 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3175 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3179 if (tb == get_mode_one(smode)) {
3180 /* (L)a * (L)1 = (L)a */
3181 ir_node *blk = get_irn_n(a, -1);
3182 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3183 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3186 else if (tb == get_mode_minus_one(smode)) {
3187 /* (L)a * (L)-1 = (L)-a */
3188 ir_node *blk = get_nodes_block(n);
3189 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3190 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3191 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3198 * Transform Mul(a,-1) into -a.
3199 * Do constant evaluation of Phi nodes.
3200 * Do architecture dependent optimizations on Mul nodes
3202 static ir_node *transform_node_Mul(ir_node *n)
3204 ir_node *c, *oldn = n;
3205 ir_mode *mode = get_irn_mode(n);
3206 ir_node *a = get_Mul_left(n);
3207 ir_node *b = get_Mul_right(n);
3209 n = fold_constant_associativity(n, tarval_mul);
3213 if (mode != get_irn_mode(a))
3214 return transform_node_Mul2n(n, mode);
3216 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3218 if (mode_is_signed(mode)) {
3221 if (value_of(a) == get_mode_minus_one(mode))
3223 else if (value_of(b) == get_mode_minus_one(mode))
3226 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3227 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3232 if (is_Const(b)) { /* (-a) * const -> a * -const */
3233 ir_node *cnst = const_negate(b);
3235 dbg_info *dbgi = get_irn_dbg_info(n);
3236 ir_node *block = get_nodes_block(n);
3237 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3238 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3241 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3242 dbg_info *dbgi = get_irn_dbg_info(n);
3243 ir_node *block = get_nodes_block(n);
3244 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3245 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3247 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3248 ir_node *sub_l = get_Sub_left(b);
3249 ir_node *sub_r = get_Sub_right(b);
3250 dbg_info *dbgi = get_irn_dbg_info(n);
3251 ir_node *block = get_nodes_block(n);
3252 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3253 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3254 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3257 } else if (is_Minus(b)) {
3258 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3259 ir_node *sub_l = get_Sub_left(a);
3260 ir_node *sub_r = get_Sub_right(a);
3261 dbg_info *dbgi = get_irn_dbg_info(n);
3262 ir_node *block = get_nodes_block(n);
3263 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3264 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3265 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3268 } else if (is_Shl(a)) {
3269 ir_node *const shl_l = get_Shl_left(a);
3270 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3271 /* (1 << x) * b -> b << x */
3272 dbg_info *const dbgi = get_irn_dbg_info(n);
3273 ir_node *const block = get_nodes_block(n);
3274 ir_node *const shl_r = get_Shl_right(a);
3275 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3276 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3279 } else if (is_Shl(b)) {
3280 ir_node *const shl_l = get_Shl_left(b);
3281 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3282 /* a * (1 << x) -> a << x */
3283 dbg_info *const dbgi = get_irn_dbg_info(n);
3284 ir_node *const block = get_nodes_block(n);
3285 ir_node *const shl_r = get_Shl_right(b);
3286 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3287 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3291 if (get_mode_arithmetic(mode) == irma_ieee754
3292 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3294 ir_tarval *tv = get_Const_tarval(a);
3295 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3296 && !tarval_is_negative(tv)) {
3297 /* 2.0 * b = b + b */
3298 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3299 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3303 else if (is_Const(b)) {
3304 ir_tarval *tv = get_Const_tarval(b);
3305 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3306 && !tarval_is_negative(tv)) {
3307 /* a * 2.0 = a + a */
3308 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3309 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3314 return arch_dep_replace_mul_with_shifts(n);
3318 * Transform a Div Node.
3320 static ir_node *transform_node_Div(ir_node *n)
3322 ir_mode *mode = get_Div_resmode(n);
3323 ir_node *a = get_Div_left(n);
3324 ir_node *b = get_Div_right(n);
3326 const ir_node *dummy;
3328 if (mode_is_int(mode)) {
3329 if (is_Const(b) && is_const_Phi(a)) {
3330 /* check for Div(Phi, Const) */
3331 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3333 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3336 } else if (is_Const(a) && is_const_Phi(b)) {
3337 /* check for Div(Const, Phi) */
3338 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3340 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3343 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3344 /* check for Div(Phi, Phi) */
3345 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3347 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3352 if (a == b && value_not_zero(a, &dummy)) {
3353 ir_graph *irg = get_irn_irg(n);
3354 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3355 value = new_r_Const(irg, get_mode_one(mode));
3356 DBG_OPT_CSTEVAL(n, value);
3359 if (mode_is_signed(mode) && is_Const(b)) {
3360 ir_tarval *tv = get_Const_tarval(b);
3362 if (tv == get_mode_minus_one(mode)) {
3364 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3365 DBG_OPT_CSTEVAL(n, value);
3369 /* Try architecture dependent optimization */
3370 value = arch_dep_replace_div_by_const(n);
3373 assert(mode_is_float(mode));
3375 /* Optimize x/c to x*(1/c) */
3376 if (get_mode_arithmetic(mode) == irma_ieee754) {
3377 ir_tarval *tv = value_of(b);
3379 if (tv != tarval_bad) {
3380 int rem = tarval_fp_ops_enabled();
3383 * Floating point constant folding might be disabled here to
3385 * However, as we check for exact result, doing it is safe.
3388 tarval_enable_fp_ops(1);
3389 tv = tarval_div(get_mode_one(mode), tv);
3390 tarval_enable_fp_ops(rem);
3392 /* Do the transformation if the result is either exact or we are
3393 not using strict rules. */
3394 if (tv != tarval_bad &&
3395 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3396 ir_node *block = get_nodes_block(n);
3397 ir_graph *irg = get_irn_irg(block);
3398 ir_node *c = new_r_Const(irg, tv);
3399 dbg_info *dbgi = get_irn_dbg_info(n);
3400 value = new_rd_Mul(dbgi, block, a, c, mode);
3413 /* Turn Div into a tuple (mem, jmp, bad, value) */
3414 mem = get_Div_mem(n);
3415 blk = get_nodes_block(n);
3416 irg = get_irn_irg(blk);
3418 /* skip a potential Pin */
3419 mem = skip_Pin(mem);
3420 turn_into_tuple(n, pn_Div_max+1);
3421 set_Tuple_pred(n, pn_Div_M, mem);
3422 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3423 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3424 set_Tuple_pred(n, pn_Div_res, value);
3430 * Transform a Mod node.
3432 static ir_node *transform_node_Mod(ir_node *n)
3434 ir_mode *mode = get_Mod_resmode(n);
3435 ir_node *a = get_Mod_left(n);
3436 ir_node *b = get_Mod_right(n);
3441 if (is_Const(b) && is_const_Phi(a)) {
3442 /* check for Div(Phi, Const) */
3443 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3445 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3449 else if (is_Const(a) && is_const_Phi(b)) {
3450 /* check for Div(Const, Phi) */
3451 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3453 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3457 else if (is_const_Phi(a) && is_const_Phi(b)) {
3458 /* check for Div(Phi, Phi) */
3459 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3461 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3468 irg = get_irn_irg(n);
3469 if (tv != tarval_bad) {
3470 value = new_r_Const(irg, tv);
3472 DBG_OPT_CSTEVAL(n, value);
3475 ir_node *a = get_Mod_left(n);
3476 ir_node *b = get_Mod_right(n);
3477 const ir_node *dummy;
3479 if (a == b && value_not_zero(a, &dummy)) {
3480 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3481 value = new_r_Const(irg, get_mode_null(mode));
3482 DBG_OPT_CSTEVAL(n, value);
3485 if (mode_is_signed(mode) && is_Const(b)) {
3486 ir_tarval *tv = get_Const_tarval(b);
3488 if (tv == get_mode_minus_one(mode)) {
3490 value = new_r_Const(irg, get_mode_null(mode));
3491 DBG_OPT_CSTEVAL(n, value);
3495 /* Try architecture dependent optimization */
3496 value = arch_dep_replace_mod_by_const(n);
3505 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3506 mem = get_Mod_mem(n);
3507 blk = get_nodes_block(n);
3508 irg = get_irn_irg(blk);
3510 /* skip a potential Pin */
3511 mem = skip_Pin(mem);
3512 turn_into_tuple(n, pn_Mod_max+1);
3513 set_Tuple_pred(n, pn_Mod_M, mem);
3514 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3515 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3516 set_Tuple_pred(n, pn_Mod_res, value);
3522 * Transform a Cond node.
3524 * Replace the Cond by a Jmp if it branches on a constant
3527 static ir_node *transform_node_Cond(ir_node *n)
3529 ir_node *a = get_Cond_selector(n);
3530 ir_graph *irg = get_irn_irg(n);
3534 /* we need block info which is not available in floating irgs */
3535 if (get_irg_pinned(irg) == op_pin_state_floats)
3539 if (ta == tarval_bad && is_Cmp(a)) {
3540 /* try again with a direct call to compute_cmp, as we don't care
3541 * about the MODEB_LOWERED flag here */
3542 ta = compute_cmp(a);
3545 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3546 /* It's a boolean Cond, branching on a boolean constant.
3547 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3548 ir_node *blk = get_nodes_block(n);
3549 jmp = new_r_Jmp(blk);
3550 turn_into_tuple(n, pn_Cond_max+1);
3551 if (ta == tarval_b_true) {
3552 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3553 set_Tuple_pred(n, pn_Cond_true, jmp);
3555 set_Tuple_pred(n, pn_Cond_false, jmp);
3556 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3558 /* We might generate an endless loop, so keep it alive. */
3559 add_End_keepalive(get_irg_end(irg), blk);
3560 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3565 static ir_node *transform_node_Switch(ir_node *n)
3567 ir_node *op = get_Switch_selector(n);
3568 ir_tarval *val = value_of(op);
3569 if (val != tarval_bad) {
3570 dbg_info *dbgi = get_irn_dbg_info(n);
3571 ir_graph *irg = get_irn_irg(n);
3572 unsigned n_outs = get_Switch_n_outs(n);
3573 ir_node *block = get_nodes_block(n);
3574 ir_node *bad = new_r_Bad(irg, mode_X);
3575 ir_node **in = XMALLOCN(ir_node*, n_outs);
3576 const ir_switch_table *table = get_Switch_table(n);
3577 size_t n_entries = ir_switch_table_get_n_entries(table);
3581 for (i = 0; i < n_entries; ++i) {
3582 const ir_switch_table_entry *entry
3583 = ir_switch_table_get_entry_const(table, i);
3584 ir_tarval *min = entry->min;
3585 ir_tarval *max = entry->max;
3588 if ((min == max && min == val)
3589 || (tarval_cmp(val, min) != ir_relation_less
3590 && tarval_cmp(val, max) != ir_relation_greater)) {
3595 for (o = 0; o < n_outs; ++o) {
3596 if (o == (unsigned)jmp_pn) {
3597 in[o] = new_rd_Jmp(dbgi, block);
3602 return new_r_Tuple(block, (int)n_outs, in);
3608 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3610 * - and, or, xor instead of &
3611 * - Shl, Shr, Shrs, rotl instead of >>
3612 * (with a special case for Or/Xor + Shrs)
3614 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3616 static ir_node *transform_node_shift_bitop(ir_node *n)
3618 ir_graph *irg = get_irn_irg(n);
3619 ir_node *right = get_binop_right(n);
3620 ir_mode *mode = get_irn_mode(n);
3622 ir_node *bitop_left;
3623 ir_node *bitop_right;
3632 ir_tarval *tv_shift;
3634 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3637 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3639 if (!is_Const(right))
3642 left = get_binop_left(n);
3643 op_left = get_irn_op(left);
3644 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3647 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3648 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3649 /* TODO: test if sign bit is affectes */
3653 bitop_right = get_binop_right(left);
3654 if (!is_Const(bitop_right))
3657 bitop_left = get_binop_left(left);
3659 block = get_nodes_block(n);
3660 dbgi = get_irn_dbg_info(n);
3661 tv1 = get_Const_tarval(bitop_right);
3662 tv2 = get_Const_tarval(right);
3664 assert(get_tarval_mode(tv1) == mode);
3667 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3668 tv_shift = tarval_shl(tv1, tv2);
3669 } else if (is_Shr(n)) {
3670 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3671 tv_shift = tarval_shr(tv1, tv2);
3672 } else if (is_Shrs(n)) {
3673 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3674 tv_shift = tarval_shrs(tv1, tv2);
3677 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3678 tv_shift = tarval_rotl(tv1, tv2);
3681 assert(get_tarval_mode(tv_shift) == mode);
3682 irg = get_irn_irg(n);
3683 new_const = new_r_Const(irg, tv_shift);
3685 if (op_left == op_And) {
3686 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3687 } else if (op_left == op_Or) {
3688 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3690 assert(op_left == op_Eor);
3691 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3700 static ir_node *transform_node_And(ir_node *n)
3702 ir_node *c, *oldn = n;
3703 ir_node *a = get_And_left(n);
3704 ir_node *b = get_And_right(n);
3707 n = fold_constant_associativity(n, tarval_and);
3711 if (is_Cmp(a) && is_Cmp(b)) {
3712 ir_node *a_left = get_Cmp_left(a);
3713 ir_node *a_right = get_Cmp_right(a);
3714 ir_node *b_left = get_Cmp_left(b);
3715 ir_node *b_right = get_Cmp_right(b);
3716 ir_relation a_relation = get_Cmp_relation(a);
3717 ir_relation b_relation = get_Cmp_relation(b);
3718 /* we can combine the relations of two compares with the same
3720 if (a_left == b_left && b_left == b_right) {
3721 dbg_info *dbgi = get_irn_dbg_info(n);
3722 ir_node *block = get_nodes_block(n);
3723 ir_relation new_relation = a_relation & b_relation;
3724 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3726 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3727 if (a_relation == b_relation && a_relation == ir_relation_equal
3728 && !mode_is_float(get_irn_mode(a_left))
3729 && !mode_is_float(get_irn_mode(b_left))) {
3730 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3731 dbg_info *dbgi = get_irn_dbg_info(n);
3732 ir_node *block = get_nodes_block(n);
3733 ir_mode *a_mode = get_irn_mode(a_left);
3734 ir_mode *b_mode = get_irn_mode(b_left);
3735 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3736 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3737 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3738 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3739 ir_graph *irg = get_irn_irg(n);
3740 ir_node *zero = create_zero_const(irg, b_mode);
3741 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3743 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3744 dbg_info *dbgi = get_irn_dbg_info(n);
3745 ir_node *block = get_nodes_block(n);
3746 ir_mode *a_mode = get_irn_mode(a_left);
3747 ir_mode *b_mode = get_irn_mode(b_left);
3748 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3749 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3750 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3751 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3752 ir_graph *irg = get_irn_irg(n);
3753 ir_node *zero = create_zero_const(irg, a_mode);
3754 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3759 mode = get_irn_mode(n);
3760 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3762 if (is_Or(a) || is_Or_Eor_Add(a)) {
3763 ir_node *or_left = get_binop_left(a);
3764 ir_node *or_right = get_binop_right(a);
3765 if (complement_values(or_left, b)) {
3766 /* (a|b) & ~a => b & ~a */
3767 dbg_info *dbgi = get_irn_dbg_info(n);
3768 ir_node *block = get_nodes_block(n);
3769 return new_rd_And(dbgi, block, or_right, b, mode);
3770 } else if (complement_values(or_right, b)) {
3771 /* (a|b) & ~b => a & ~b */
3772 dbg_info *dbgi = get_irn_dbg_info(n);
3773 ir_node *block = get_nodes_block(n);
3774 return new_rd_And(dbgi, block, or_left, b, mode);
3775 } else if (is_Not(b)) {
3776 ir_node *op = get_Not_op(b);
3778 ir_node *ba = get_And_left(op);
3779 ir_node *bb = get_And_right(op);
3781 /* it's enough to test the following cases due to normalization! */
3782 if (or_left == ba && or_right == bb) {
3783 /* (a|b) & ~(a&b) = a^b */
3784 ir_node *block = get_nodes_block(n);
3786 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3787 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3793 if (is_Or(b) || is_Or_Eor_Add(b)) {
3794 ir_node *or_left = get_binop_left(b);
3795 ir_node *or_right = get_binop_right(b);
3796 if (complement_values(or_left, a)) {
3797 /* (a|b) & ~a => b & ~a */
3798 dbg_info *dbgi = get_irn_dbg_info(n);
3799 ir_node *block = get_nodes_block(n);
3800 return new_rd_And(dbgi, block, or_right, a, mode);
3801 } else if (complement_values(or_right, a)) {
3802 /* (a|b) & ~b => a & ~b */
3803 dbg_info *dbgi = get_irn_dbg_info(n);
3804 ir_node *block = get_nodes_block(n);
3805 return new_rd_And(dbgi, block, or_left, a, mode);
3806 } else if (is_Not(a)) {
3807 ir_node *op = get_Not_op(a);
3809 ir_node *aa = get_And_left(op);
3810 ir_node *ab = get_And_right(op);
3812 /* it's enough to test the following cases due to normalization! */
3813 if (or_left == aa && or_right == ab) {
3814 /* (a|b) & ~(a&b) = a^b */
3815 ir_node *block = get_nodes_block(n);
3817 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3818 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3824 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3825 ir_node *al = get_binop_left(a);
3826 ir_node *ar = get_binop_right(a);
3829 /* (b ^ a) & b -> ~a & b */
3830 dbg_info *dbg = get_irn_dbg_info(n);
3831 ir_node *block = get_nodes_block(n);
3833 ar = new_rd_Not(dbg, block, ar, mode);
3834 n = new_rd_And(dbg, block, ar, b, mode);
3835 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3839 /* (a ^ b) & b -> ~a & b */
3840 dbg_info *dbg = get_irn_dbg_info(n);
3841 ir_node *block = get_nodes_block(n);
3843 al = new_rd_Not(dbg, block, al, mode);
3844 n = new_rd_And(dbg, block, al, b, mode);
3845 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3849 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3850 ir_node *bl = get_binop_left(b);
3851 ir_node *br = get_binop_right(b);
3854 /* a & (a ^ b) -> a & ~b */
3855 dbg_info *dbg = get_irn_dbg_info(n);
3856 ir_node *block = get_nodes_block(n);
3858 br = new_rd_Not(dbg, block, br, mode);
3859 n = new_rd_And(dbg, block, br, a, mode);
3860 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3864 /* a & (b ^ a) -> a & ~b */
3865 dbg_info *dbg = get_irn_dbg_info(n);
3866 ir_node *block = get_nodes_block(n);
3868 bl = new_rd_Not(dbg, block, bl, mode);
3869 n = new_rd_And(dbg, block, bl, a, mode);
3870 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3874 if (is_Not(a) && is_Not(b)) {
3875 /* ~a & ~b = ~(a|b) */
3876 ir_node *block = get_nodes_block(n);
3877 ir_mode *mode = get_irn_mode(n);
3881 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3882 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3883 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3888 vrp_attr *b_vrp = vrp_get_info(b);
3889 ir_tarval *a_val = get_Const_tarval(a);
3890 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3896 vrp_attr *a_vrp = vrp_get_info(a);
3897 ir_tarval *b_val = get_Const_tarval(b);
3898 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3903 n = transform_bitwise_distributive(n, transform_node_And);
3905 n = transform_node_bitop_shift(n);
3913 static ir_node *transform_node_Not(ir_node *n)
3915 ir_node *c, *oldn = n;
3916 ir_node *a = get_Not_op(n);
3917 ir_mode *mode = get_irn_mode(n);
3919 HANDLE_UNOP_PHI(tarval_not,a,c);
3921 /* check for a boolean Not */
3923 dbg_info *dbgi = get_irn_dbg_info(a);
3924 ir_node *block = get_nodes_block(a);
3925 ir_relation relation = get_Cmp_relation(a);
3926 relation = get_negated_relation(relation);
3927 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3928 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3932 /* normalize ~(a ^ b) => a ^ ~b */
3933 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3934 dbg_info *dbg = get_irn_dbg_info(n);
3935 ir_node *block = get_nodes_block(n);
3936 ir_node *eor_right = get_binop_right(a);
3937 ir_node *eor_left = get_binop_left(a);
3938 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3939 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3943 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3944 if (is_Minus(a)) { /* ~-x -> x + -1 */
3945 dbg_info *dbg = get_irn_dbg_info(n);
3946 ir_graph *irg = get_irn_irg(n);
3947 ir_node *block = get_nodes_block(n);
3948 ir_node *add_l = get_Minus_op(a);
3949 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3950 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3951 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3952 ir_node *add_r = get_binop_right(a);
3953 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3954 /* ~(x + -1) = -x */
3955 ir_node *op = get_binop_left(a);
3956 ir_node *blk = get_nodes_block(n);
3957 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3958 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3966 * Transform a Minus.
3970 * -(a >>u (size-1)) = a >>s (size-1)
3971 * -(a >>s (size-1)) = a >>u (size-1)
3972 * -(a * const) -> a * -const
3974 static ir_node *transform_node_Minus(ir_node *n)
3976 ir_node *c, *oldn = n;
3977 ir_node *a = get_Minus_op(n);
3980 HANDLE_UNOP_PHI(tarval_neg,a,c);
3982 mode = get_irn_mode(a);
3983 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3984 /* the following rules are only to twos-complement */
3987 ir_node *op = get_Not_op(a);
3988 ir_tarval *tv = get_mode_one(mode);
3989 ir_node *blk = get_nodes_block(n);
3990 ir_graph *irg = get_irn_irg(blk);
3991 ir_node *c = new_r_Const(irg, tv);
3992 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3993 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3997 ir_node *c = get_Shr_right(a);
4000 ir_tarval *tv = get_Const_tarval(c);
4002 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4003 /* -(a >>u (size-1)) = a >>s (size-1) */
4004 ir_node *v = get_Shr_left(a);
4006 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4007 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4013 ir_node *c = get_Shrs_right(a);
4016 ir_tarval *tv = get_Const_tarval(c);
4018 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4019 /* -(a >>s (size-1)) = a >>u (size-1) */
4020 ir_node *v = get_Shrs_left(a);
4022 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4023 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4030 /* - (a-b) = b - a */
4031 ir_node *la = get_Sub_left(a);
4032 ir_node *ra = get_Sub_right(a);
4033 ir_node *blk = get_nodes_block(n);
4035 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4036 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4040 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4041 ir_node *mul_l = get_Mul_left(a);
4042 ir_node *mul_r = get_Mul_right(a);
4043 ir_tarval *tv = value_of(mul_r);
4044 if (tv != tarval_bad) {
4045 tv = tarval_neg(tv);
4046 if (tv != tarval_bad) {
4047 ir_graph *irg = get_irn_irg(n);
4048 ir_node *cnst = new_r_Const(irg, tv);
4049 dbg_info *dbg = get_irn_dbg_info(a);
4050 ir_node *block = get_nodes_block(a);
4051 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4052 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4062 * Transform a Proj(Load) with a non-null address.
4064 static ir_node *transform_node_Proj_Load(ir_node *proj)
4066 if (get_irn_mode(proj) == mode_X) {
4067 ir_node *load = get_Proj_pred(proj);
4069 /* get the Load address */
4070 const ir_node *addr = get_Load_ptr(load);
4071 const ir_node *confirm;
4073 if (value_not_null(addr, &confirm)) {
4074 if (confirm == NULL) {
4075 /* this node may float if it did not depend on a Confirm */
4076 set_irn_pinned(load, op_pin_state_floats);
4078 if (get_Proj_proj(proj) == pn_Load_X_except) {
4079 ir_graph *irg = get_irn_irg(proj);
4080 DBG_OPT_EXC_REM(proj);
4081 return new_r_Bad(irg, mode_X);
4083 ir_node *blk = get_nodes_block(load);
4084 return new_r_Jmp(blk);
4092 * Transform a Proj(Store) with a non-null address.
4094 static ir_node *transform_node_Proj_Store(ir_node *proj)
4096 if (get_irn_mode(proj) == mode_X) {
4097 ir_node *store = get_Proj_pred(proj);
4099 /* get the load/store address */
4100 const ir_node *addr = get_Store_ptr(store);
4101 const ir_node *confirm;
4103 if (value_not_null(addr, &confirm)) {
4104 if (confirm == NULL) {
4105 /* this node may float if it did not depend on a Confirm */
4106 set_irn_pinned(store, op_pin_state_floats);
4108 if (get_Proj_proj(proj) == pn_Store_X_except) {
4109 ir_graph *irg = get_irn_irg(proj);
4110 DBG_OPT_EXC_REM(proj);
4111 return new_r_Bad(irg, mode_X);
4113 ir_node *blk = get_nodes_block(store);
4114 return new_r_Jmp(blk);
4122 * Transform a Proj(Div) with a non-zero value.
4123 * Removes the exceptions and routes the memory to the NoMem node.
4125 static ir_node *transform_node_Proj_Div(ir_node *proj)
4127 ir_node *div = get_Proj_pred(proj);
4128 ir_node *b = get_Div_right(div);
4129 ir_node *res, *new_mem;
4130 const ir_node *confirm;
4133 if (value_not_zero(b, &confirm)) {
4134 /* div(x, y) && y != 0 */
4135 if (confirm == NULL) {
4136 /* we are sure we have a Const != 0 */
4137 new_mem = get_Div_mem(div);
4138 new_mem = skip_Pin(new_mem);
4139 set_Div_mem(div, new_mem);
4140 set_irn_pinned(div, op_pin_state_floats);
4143 proj_nr = get_Proj_proj(proj);
4145 case pn_Div_X_regular:
4146 return new_r_Jmp(get_nodes_block(div));
4148 case pn_Div_X_except: {
4149 ir_graph *irg = get_irn_irg(proj);
4150 /* we found an exception handler, remove it */
4151 DBG_OPT_EXC_REM(proj);
4152 return new_r_Bad(irg, mode_X);
4156 ir_graph *irg = get_irn_irg(proj);
4157 res = get_Div_mem(div);
4158 new_mem = get_irg_no_mem(irg);
4161 /* This node can only float up to the Confirm block */
4162 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4164 set_irn_pinned(div, op_pin_state_floats);
4165 /* this is a Div without exception, we can remove the memory edge */
4166 set_Div_mem(div, new_mem);
4175 * Transform a Proj(Mod) with a non-zero value.
4176 * Removes the exceptions and routes the memory to the NoMem node.
4178 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4180 ir_node *mod = get_Proj_pred(proj);
4181 ir_node *b = get_Mod_right(mod);
4182 ir_node *res, *new_mem;
4183 const ir_node *confirm;
4186 if (value_not_zero(b, &confirm)) {
4187 /* mod(x, y) && y != 0 */
4188 proj_nr = get_Proj_proj(proj);
4190 if (confirm == NULL) {
4191 /* we are sure we have a Const != 0 */
4192 new_mem = get_Mod_mem(mod);
4193 new_mem = skip_Pin(new_mem);
4194 set_Mod_mem(mod, new_mem);
4195 set_irn_pinned(mod, op_pin_state_floats);
4200 case pn_Mod_X_regular:
4201 return new_r_Jmp(get_irn_n(mod, -1));
4203 case pn_Mod_X_except: {
4204 ir_graph *irg = get_irn_irg(proj);
4205 /* we found an exception handler, remove it */
4206 DBG_OPT_EXC_REM(proj);
4207 return new_r_Bad(irg, mode_X);
4211 ir_graph *irg = get_irn_irg(proj);
4212 res = get_Mod_mem(mod);
4213 new_mem = get_irg_no_mem(irg);
4216 /* This node can only float up to the Confirm block */
4217 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4219 /* this is a Mod without exception, we can remove the memory edge */
4220 set_Mod_mem(mod, new_mem);
4224 if (get_Mod_left(mod) == b) {
4225 /* a % a = 0 if a != 0 */
4226 ir_graph *irg = get_irn_irg(proj);
4227 ir_mode *mode = get_irn_mode(proj);
4228 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4230 DBG_OPT_CSTEVAL(mod, res);
4239 * return true if the operation returns a value with exactly 1 bit set
4241 static bool is_single_bit(const ir_node *node)
4243 /* a first implementation, could be extended with vrp and others... */
4245 ir_node *shl_l = get_Shl_left(node);
4246 ir_mode *mode = get_irn_mode(node);
4247 int modulo = get_mode_modulo_shift(mode);
4248 /* this works if we shift a 1 and we have modulo shift */
4249 if (is_Const(shl_l) && is_Const_one(shl_l)
4250 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4253 } else if (is_Const(node)) {
4254 ir_tarval *tv = get_Const_tarval(node);
4255 return tarval_is_single_bit(tv);
4261 * checks if node just flips a bit in another node and returns that other node
4262 * if so. @p tv should be a value having just 1 bit set
4264 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4267 return get_Not_op(node);
4269 ir_node *right = get_Eor_right(node);
4270 if (is_Const(right)) {
4271 ir_tarval *right_tv = get_Const_tarval(right);
4272 ir_mode *mode = get_irn_mode(node);
4273 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4274 return get_Eor_left(node);
4281 * Normalizes and optimizes Cmp nodes.
4283 static ir_node *transform_node_Cmp(ir_node *n)
4285 ir_node *left = get_Cmp_left(n);
4286 ir_node *right = get_Cmp_right(n);
4287 ir_mode *mode = get_irn_mode(left);
4288 ir_tarval *tv = NULL;
4289 bool changed = false;
4290 bool changedc = false;
4291 ir_relation relation = get_Cmp_relation(n);
4292 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4294 /* mask out impossible relations */
4295 ir_relation new_relation = relation & possible;
4296 if (new_relation != relation) {
4297 relation = new_relation;
4301 /* Remove unnecessary conversions */
4302 if (is_Conv(left) && is_Conv(right)) {
4303 ir_node *op_left = get_Conv_op(left);
4304 ir_node *op_right = get_Conv_op(right);
4305 ir_mode *mode_left = get_irn_mode(op_left);
4306 ir_mode *mode_right = get_irn_mode(op_right);
4308 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4309 && mode_left != mode_b && mode_right != mode_b) {
4310 ir_node *block = get_nodes_block(n);
4312 if (mode_left == mode_right) {
4316 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4317 } else if (smaller_mode(mode_left, mode_right)) {
4318 left = new_r_Conv(block, op_left, mode_right);
4321 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4322 } else if (smaller_mode(mode_right, mode_left)) {
4324 right = new_r_Conv(block, op_right, mode_left);
4326 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4328 mode = get_irn_mode(left);
4331 if (is_Conv(left) && is_Const(right)) {
4332 ir_node *op_left = get_Conv_op(left);
4333 ir_mode *mode_left = get_irn_mode(op_left);
4334 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4335 ir_tarval *tv = get_Const_tarval(right);
4336 tarval_int_overflow_mode_t last_mode
4337 = tarval_get_integer_overflow_mode();
4339 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4340 new_tv = tarval_convert_to(tv, mode_left);
4341 tarval_set_integer_overflow_mode(last_mode);
4342 if (new_tv != tarval_bad) {
4343 ir_graph *irg = get_irn_irg(n);
4345 right = new_r_Const(irg, new_tv);
4346 mode = get_irn_mode(left);
4348 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4354 * Optimize -a CMP -b into b CMP a.
4355 * This works only for modes where unary Minus cannot Overflow.
4356 * Note that two-complement integers can Overflow so it will NOT work.
4358 if (!mode_overflow_on_unary_Minus(mode) &&
4359 is_Minus(left) && is_Minus(right)) {
4360 left = get_Minus_op(left);
4361 right = get_Minus_op(right);
4362 relation = get_inversed_relation(relation);
4364 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4367 /* remove operation on both sides if possible */
4368 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4370 * The following operations are NOT safe for floating point operations, for instance
4371 * 1.0 + inf == 2.0 + inf, =/=> x == y
4373 if (mode_is_int(mode)) {
4374 unsigned lop = get_irn_opcode(left);
4376 if (lop == get_irn_opcode(right)) {
4377 ir_node *ll, *lr, *rl, *rr;
4379 /* same operation on both sides, try to remove */
4383 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4384 left = get_unop_op(left);
4385 right = get_unop_op(right);
4387 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4390 ll = get_Add_left(left);
4391 lr = get_Add_right(left);
4392 rl = get_Add_left(right);
4393 rr = get_Add_right(right);
4396 /* X + a CMP X + b ==> a CMP b */
4400 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4401 } else if (ll == rr) {
4402 /* X + a CMP b + X ==> a CMP b */
4406 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4407 } else if (lr == rl) {
4408 /* a + X CMP X + b ==> a CMP b */
4412 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4413 } else if (lr == rr) {
4414 /* a + X CMP b + X ==> a CMP b */
4418 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4422 ll = get_Sub_left(left);
4423 lr = get_Sub_right(left);
4424 rl = get_Sub_left(right);
4425 rr = get_Sub_right(right);
4428 /* X - a CMP X - b ==> a CMP b */
4432 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4433 } else if (lr == rr) {
4434 /* a - X CMP b - X ==> a CMP b */
4438 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4442 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4443 /* a ROTL X CMP b ROTL X ==> a CMP b */
4444 left = get_Rotl_left(left);
4445 right = get_Rotl_left(right);
4447 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4455 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4456 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4457 ir_node *ll = get_binop_left(left);
4458 ir_node *lr = get_binop_right(left);
4460 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4466 ir_graph *irg = get_irn_irg(n);
4468 right = create_zero_const(irg, mode);
4470 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4473 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4474 ir_node *rl = get_binop_left(right);
4475 ir_node *rr = get_binop_right(right);
4477 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4483 ir_graph *irg = get_irn_irg(n);
4485 right = create_zero_const(irg, mode);
4487 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4491 if (is_And(left) && is_Const(right)) {
4492 ir_node *ll = get_binop_left(left);
4493 ir_node *lr = get_binop_right(left);
4494 if (is_Shr(ll) && is_Const(lr)) {
4495 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4496 ir_node *block = get_nodes_block(n);
4497 ir_mode *mode = get_irn_mode(left);
4499 ir_node *llr = get_Shr_right(ll);
4500 if (is_Const(llr)) {
4501 dbg_info *dbg = get_irn_dbg_info(left);
4502 ir_graph *irg = get_irn_irg(left);
4504 ir_tarval *c1 = get_Const_tarval(llr);
4505 ir_tarval *c2 = get_Const_tarval(lr);
4506 ir_tarval *c3 = get_Const_tarval(right);
4507 ir_tarval *mask = tarval_shl(c2, c1);
4508 ir_tarval *value = tarval_shl(c3, c1);
4510 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4511 right = new_r_Const(irg, value);
4516 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4518 if (is_Const(right) && is_Const_null(right) &&
4519 (is_Eor(left) || is_Or_Eor_Add(left))) {
4520 right = get_Eor_right(left);
4521 left = get_Eor_left(left);
4527 if (mode_is_int(mode) && is_And(left)) {
4528 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4529 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4530 if (relation == ir_relation_equal
4531 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4532 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4533 ir_node *and0 = get_And_left(left);
4534 ir_node *and1 = get_And_right(left);
4535 if (and1 == right) {
4536 ir_node *tmp = and0;
4540 if (and0 == right && is_single_bit(and0)) {
4541 ir_graph *irg = get_irn_irg(n);
4543 relation == ir_relation_equal ? ir_relation_less_greater
4544 : ir_relation_equal;
4545 right = create_zero_const(irg, mode);
4551 if (is_Const(right) && is_Const_null(right) &&
4552 (relation == ir_relation_equal
4553 || (relation == ir_relation_less_greater)
4554 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4556 /* instead of flipping the bit before the bit-test operation negate
4558 ir_node *and0 = get_And_left(left);
4559 ir_node *and1 = get_And_right(left);
4560 if (is_Const(and1)) {
4561 ir_tarval *tv = get_Const_tarval(and1);
4562 if (tarval_is_single_bit(tv)) {
4563 ir_node *flipped = flips_bit(and0, tv);
4564 if (flipped != NULL) {
4565 dbg_info *dbgi = get_irn_dbg_info(left);
4566 ir_node *block = get_nodes_block(left);
4567 relation = get_negated_relation(relation);
4568 left = new_rd_And(dbgi, block, flipped, and1, mode);
4577 /* replace mode_b compares with ands/ors */
4578 if (mode == mode_b) {
4579 ir_node *block = get_nodes_block(n);
4583 case ir_relation_less_equal:
4584 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4586 case ir_relation_less:
4587 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4589 case ir_relation_greater_equal:
4590 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4592 case ir_relation_greater:
4593 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4595 case ir_relation_less_greater:
4596 bres = new_r_Eor(block, left, right, mode_b);
4598 case ir_relation_equal:
4599 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4602 #ifdef DEBUG_libfirm
4603 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4608 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4614 * First step: normalize the compare op
4615 * by placing the constant on the right side
4616 * or moving the lower address node to the left.
4618 if (!operands_are_normalized(left, right)) {
4623 relation = get_inversed_relation(relation);
4628 * Second step: Try to reduce the magnitude
4629 * of a constant. This may help to generate better code
4630 * later and may help to normalize more compares.
4631 * Of course this is only possible for integer values.
4633 tv = value_of(right);
4634 if (tv != tarval_bad) {
4635 ir_mode *mode = get_irn_mode(right);
4637 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4638 * cmp(ct,c2) | cmp(cf,c2) | result
4639 * -----------|------------|--------
4640 * true | true | True
4641 * false | false | False
4643 * false | true | not(x)
4646 ir_node *mux_true = get_Mux_true(left);
4647 ir_node *mux_false = get_Mux_false(left);
4648 if (is_Const(mux_true) && is_Const(mux_false)) {
4649 /* we can fold true/false constant separately */
4650 ir_tarval *tv_true = get_Const_tarval(mux_true);
4651 ir_tarval *tv_false = get_Const_tarval(mux_false);
4652 ir_relation r_true = tarval_cmp(tv_true, tv);
4653 ir_relation r_false = tarval_cmp(tv_false, tv);
4654 if (r_true != ir_relation_false
4655 || r_false != ir_relation_false) {
4656 bool rel_true = (r_true & relation) != 0;
4657 bool rel_false = (r_false & relation) != 0;
4658 ir_node *cond = get_Mux_sel(left);
4659 if (rel_true == rel_false) {
4660 relation = rel_true ? ir_relation_true
4661 : ir_relation_false;
4662 } else if (rel_true) {
4665 dbg_info *dbgi = get_irn_dbg_info(n);
4666 ir_node *block = get_nodes_block(n);
4667 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4674 /* TODO extend to arbitrary constants */
4675 if (is_Conv(left) && tarval_is_null(tv)) {
4676 ir_node *op = get_Conv_op(left);
4677 ir_mode *op_mode = get_irn_mode(op);
4680 * UpConv(x) REL 0 ==> x REL 0
4681 * Don't do this for float values as it's unclear whether it is a
4682 * win. (on the other side it makes detection/creation of fabs hard)
4684 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4685 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4686 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4687 !mode_is_float(mode)) {
4688 tv = get_mode_null(op_mode);
4692 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4696 if (tv != tarval_bad) {
4697 /* the following optimization is possible on modes without Overflow
4698 * on Unary Minus or on == and !=:
4699 * -a CMP c ==> a swap(CMP) -c
4701 * Beware: for two-complement Overflow may occur, so only == and != can
4702 * be optimized, see this:
4703 * -MININT < 0 =/=> MININT > 0 !!!
4705 if (is_Minus(left) &&
4706 (!mode_overflow_on_unary_Minus(mode) ||
4707 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4708 tv = tarval_neg(tv);
4710 if (tv != tarval_bad) {
4711 left = get_Minus_op(left);
4712 relation = get_inversed_relation(relation);
4714 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4716 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4717 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4718 tv = tarval_not(tv);
4720 if (tv != tarval_bad) {
4721 left = get_Not_op(left);
4723 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4727 /* for integer modes, we have more */
4728 if (mode_is_int(mode) && !is_Const(left)) {
4729 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4730 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4731 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4732 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4734 if (tv != tarval_bad) {
4735 relation ^= ir_relation_equal;
4737 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4740 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4741 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4742 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4743 tv = tarval_add(tv, get_mode_one(mode));
4745 if (tv != tarval_bad) {
4746 relation ^= ir_relation_equal;
4748 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4752 /* the following reassociations work only for == and != */
4753 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4754 if (tv != tarval_bad) {
4755 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4757 ir_node *c1 = get_Sub_right(left);
4758 ir_tarval *tv2 = value_of(c1);
4760 if (tv2 != tarval_bad) {
4761 tv2 = tarval_add(tv, value_of(c1));
4763 if (tv2 != tarval_bad) {
4764 left = get_Sub_left(left);
4767 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4771 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4772 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4773 ir_node *a_l = get_binop_left(left);
4774 ir_node *a_r = get_binop_right(left);
4778 if (is_Const(a_l)) {
4780 tv2 = value_of(a_l);
4783 tv2 = value_of(a_r);
4786 if (tv2 != tarval_bad) {
4787 tv2 = tarval_sub(tv, tv2, NULL);
4789 if (tv2 != tarval_bad) {
4793 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4797 /* -a == c ==> a == -c, -a != c ==> a != -c */
4798 else if (is_Minus(left)) {
4799 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4801 if (tv2 != tarval_bad) {
4802 left = get_Minus_op(left);
4805 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4812 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4813 switch (get_irn_opcode(left)) {
4817 c1 = get_And_right(left);
4820 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4821 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4823 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4825 /* TODO: move to constant evaluation */
4826 ir_graph *irg = get_irn_irg(n);
4827 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4828 c1 = new_r_Const(irg, tv);
4829 DBG_OPT_CSTEVAL(n, c1);
4833 if (tarval_is_single_bit(tv)) {
4835 * optimization for AND:
4837 * And(x, C) == C ==> And(x, C) != 0
4838 * And(x, C) != C ==> And(X, C) == 0
4840 * if C is a single Bit constant.
4843 /* check for Constant's match. We have check hare the tarvals,
4844 because our const might be changed */
4845 if (get_Const_tarval(c1) == tv) {
4846 /* fine: do the transformation */
4847 tv = get_mode_null(get_tarval_mode(tv));
4848 relation ^= ir_relation_less_equal_greater;
4850 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4856 c1 = get_Or_right(left);
4857 if (is_Const(c1) && tarval_is_null(tv)) {
4859 * Or(x, C) == 0 && C != 0 ==> FALSE
4860 * Or(x, C) != 0 && C != 0 ==> TRUE
4862 if (! tarval_is_null(get_Const_tarval(c1))) {
4863 /* TODO: move to constant evaluation */
4864 ir_graph *irg = get_irn_irg(n);
4865 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4866 c1 = new_r_Const(irg, tv);
4867 DBG_OPT_CSTEVAL(n, c1);
4874 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4876 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4879 c1 = get_Shl_right(left);
4881 ir_graph *irg = get_irn_irg(c1);
4882 ir_tarval *tv1 = get_Const_tarval(c1);
4883 ir_mode *mode = get_irn_mode(left);
4884 ir_tarval *minus1 = get_mode_all_one(mode);
4885 ir_tarval *amask = tarval_shr(minus1, tv1);
4886 ir_tarval *cmask = tarval_shl(minus1, tv1);
4889 if (tarval_and(tv, cmask) != tv) {
4890 /* condition not met */
4891 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4892 c1 = new_r_Const(irg, tv);
4893 DBG_OPT_CSTEVAL(n, c1);
4896 sl = get_Shl_left(left);
4897 blk = get_nodes_block(n);
4898 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4899 tv = tarval_shr(tv, tv1);
4901 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4906 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4908 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4911 c1 = get_Shr_right(left);
4913 ir_graph *irg = get_irn_irg(c1);
4914 ir_tarval *tv1 = get_Const_tarval(c1);
4915 ir_mode *mode = get_irn_mode(left);
4916 ir_tarval *minus1 = get_mode_all_one(mode);
4917 ir_tarval *amask = tarval_shl(minus1, tv1);
4918 ir_tarval *cmask = tarval_shr(minus1, tv1);
4921 if (tarval_and(tv, cmask) != tv) {
4922 /* condition not met */
4923 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4924 c1 = new_r_Const(irg, tv);
4925 DBG_OPT_CSTEVAL(n, c1);
4928 sl = get_Shr_left(left);
4929 blk = get_nodes_block(n);
4930 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4931 tv = tarval_shl(tv, tv1);
4933 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4938 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4940 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4943 c1 = get_Shrs_right(left);
4945 ir_graph *irg = get_irn_irg(c1);
4946 ir_tarval *tv1 = get_Const_tarval(c1);
4947 ir_mode *mode = get_irn_mode(left);
4948 ir_tarval *minus1 = get_mode_all_one(mode);
4949 ir_tarval *amask = tarval_shl(minus1, tv1);
4950 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4953 cond = tarval_sub(cond, tv1, NULL);
4954 cond = tarval_shrs(tv, cond);
4956 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4957 /* condition not met */
4958 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4959 c1 = new_r_Const(irg, tv);
4960 DBG_OPT_CSTEVAL(n, c1);
4963 sl = get_Shrs_left(left);
4964 blk = get_nodes_block(n);
4965 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4966 tv = tarval_shl(tv, tv1);
4968 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4976 if (changedc) { /* need a new Const */
4977 ir_graph *irg = get_irn_irg(n);
4978 right = new_r_Const(irg, tv);
4982 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4983 ir_node *op = get_Proj_pred(left);
4985 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4986 ir_node *c = get_binop_right(op);
4989 ir_tarval *tv = get_Const_tarval(c);
4991 if (tarval_is_single_bit(tv)) {
4992 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4993 ir_node *v = get_binop_left(op);
4994 ir_node *blk = get_irn_n(op, -1);
4995 ir_graph *irg = get_irn_irg(op);
4996 ir_mode *mode = get_irn_mode(v);
4998 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4999 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5001 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5008 dbg_info *dbgi = get_irn_dbg_info(n);
5009 ir_node *block = get_nodes_block(n);
5011 /* create a new compare */
5012 n = new_rd_Cmp(dbgi, block, left, right, relation);
5019 * Optimize CopyB(mem, x, x) into a Nop.
5021 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5023 ir_node *copyb = get_Proj_pred(proj);
5024 ir_node *a = get_CopyB_dst(copyb);
5025 ir_node *b = get_CopyB_src(copyb);
5028 switch (get_Proj_proj(proj)) {
5029 case pn_CopyB_X_regular:
5030 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5031 DBG_OPT_EXC_REM(proj);
5032 proj = new_r_Jmp(get_nodes_block(copyb));
5034 case pn_CopyB_X_except: {
5035 ir_graph *irg = get_irn_irg(proj);
5036 DBG_OPT_EXC_REM(proj);
5037 proj = new_r_Bad(irg, mode_X);
5048 * Optimize Bounds(idx, idx, upper) into idx.
5050 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5052 ir_node *oldn = proj;
5053 ir_node *bound = get_Proj_pred(proj);
5054 ir_node *idx = get_Bound_index(bound);
5055 ir_node *pred = skip_Proj(idx);
5058 if (idx == get_Bound_lower(bound))
5060 else if (is_Bound(pred)) {
5062 * idx was Bounds checked previously, it is still valid if
5063 * lower <= pred_lower && pred_upper <= upper.
5065 ir_node *lower = get_Bound_lower(bound);
5066 ir_node *upper = get_Bound_upper(bound);
5067 if (get_Bound_lower(pred) == lower &&
5068 get_Bound_upper(pred) == upper) {
5070 * One could expect that we simply return the previous
5071 * Bound here. However, this would be wrong, as we could
5072 * add an exception Proj to a new location then.
5073 * So, we must turn in into a tuple.
5079 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5080 switch (get_Proj_proj(proj)) {
5082 DBG_OPT_EXC_REM(proj);
5083 proj = get_Bound_mem(bound);
5085 case pn_Bound_X_except:
5086 DBG_OPT_EXC_REM(proj);
5087 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5091 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5093 case pn_Bound_X_regular:
5094 DBG_OPT_EXC_REM(proj);
5095 proj = new_r_Jmp(get_nodes_block(bound));
5105 * Does all optimizations on nodes that must be done on its Projs
5106 * because of creating new nodes.
5108 static ir_node *transform_node_Proj(ir_node *proj)
5110 ir_node *n = get_Proj_pred(proj);
5112 if (n->op->ops.transform_node_Proj)
5113 return n->op->ops.transform_node_Proj(proj);
5118 * Test whether a block is unreachable
5119 * Note: That this only returns true when
5120 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5121 * This is important, as you easily end up producing invalid constructs in the
5122 * unreachable code when optimizing away edges into the unreachable code.
5123 * So only set this flag when you iterate localopts to the fixpoint.
5124 * When you reach the fixpoint then all unreachable code is dead
5125 * (= can't be reached by firm edges) and you won't see the invalid constructs
5128 static bool is_block_unreachable(const ir_node *block)
5130 const ir_graph *irg = get_irn_irg(block);
5131 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5133 return get_Block_dom_depth(block) < 0;
5136 static ir_node *transform_node_Block(ir_node *block)
5138 ir_graph *irg = get_irn_irg(block);
5139 int arity = get_irn_arity(block);
5140 ir_node *bad = NULL;
5143 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5146 for (i = 0; i < arity; ++i) {
5147 ir_node *const pred = get_Block_cfgpred(block, i);
5148 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5151 bad = new_r_Bad(irg, mode_X);
5152 set_irn_n(block, i, bad);
5158 static ir_node *transform_node_Phi(ir_node *phi)
5160 int n = get_irn_arity(phi);
5161 ir_mode *mode = get_irn_mode(phi);
5162 ir_node *block = get_nodes_block(phi);
5163 ir_graph *irg = get_irn_irg(phi);
5164 ir_node *bad = NULL;
5167 /* Set phi-operands for bad-block inputs to bad */
5168 for (i = 0; i < n; ++i) {
5169 if (!is_Bad(get_Phi_pred(phi, i))) {
5170 ir_node *pred = get_Block_cfgpred(block, i);
5171 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5173 bad = new_r_Bad(irg, mode);
5174 set_irn_n(phi, i, bad);
5179 /* Move Pin nodes down through Phi nodes. */
5180 if (mode == mode_M) {
5181 n = get_irn_arity(phi);
5183 /* Beware of Phi0 */
5187 bool has_pin = false;
5189 NEW_ARR_A(ir_node *, in, n);
5191 for (i = 0; i < n; ++i) {
5192 ir_node *pred = get_irn_n(phi, i);
5195 in[i] = get_Pin_op(pred);
5197 } else if (is_Bad(pred)) {
5207 /* Move the Pin nodes "behind" the Phi. */
5208 block = get_irn_n(phi, -1);
5209 new_phi = new_r_Phi(block, n, in, mode_M);
5210 return new_r_Pin(block, new_phi);
5213 /* Move Confirms down through Phi nodes. */
5214 else if (mode_is_reference(mode)) {
5215 n = get_irn_arity(phi);
5217 /* Beware of Phi0 */
5219 ir_node *pred = get_irn_n(phi, 0);
5220 ir_node *bound, *new_phi, *block, **in;
5221 ir_relation relation;
5222 bool has_confirm = false;
5224 if (! is_Confirm(pred))
5227 bound = get_Confirm_bound(pred);
5228 relation = get_Confirm_relation(pred);
5230 NEW_ARR_A(ir_node *, in, n);
5231 in[0] = get_Confirm_value(pred);
5233 for (i = 1; i < n; ++i) {
5234 pred = get_irn_n(phi, i);
5236 if (is_Confirm(pred) &&
5237 get_Confirm_bound(pred) == bound &&
5238 get_Confirm_relation(pred) == relation) {
5239 in[i] = get_Confirm_value(pred);
5241 } else if (is_Bad(pred)) {
5251 /* move the Confirm nodes "behind" the Phi */
5252 block = get_irn_n(phi, -1);
5253 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5254 return new_r_Confirm(block, new_phi, bound, relation);
5261 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5263 * Should be moved to reassociation?
5265 static ir_node *transform_node_shift(ir_node *n)
5267 ir_node *left, *right;
5269 ir_mode *count_mode;
5270 ir_tarval *tv1, *tv2, *res;
5271 ir_node *in[2], *irn, *block;
5275 left = get_binop_left(n);
5277 /* different operations */
5278 if (get_irn_op(left) != get_irn_op(n))
5281 right = get_binop_right(n);
5282 tv1 = value_of(right);
5283 if (tv1 == tarval_bad)
5286 tv2 = value_of(get_binop_right(left));
5287 if (tv2 == tarval_bad)
5290 count_mode = get_tarval_mode(tv1);
5291 if (get_tarval_mode(tv2) != count_mode) {
5292 /* TODO: search bigger mode or something and convert... */
5296 mode = get_irn_mode(n);
5297 modulo_shf = get_mode_modulo_shift(mode);
5299 if (modulo_shf > 0) {
5300 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5302 /* I'm not so sure what happens in one complement... */
5303 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5304 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5305 * above will be invalid) */
5306 assert(modulo_shf<=0 || is_po2(modulo_shf));
5308 tv1 = tarval_and(tv1, modulo_mask);
5309 tv2 = tarval_and(tv2, modulo_mask);
5311 res = tarval_add(tv1, tv2);
5312 irg = get_irn_irg(n);
5314 /* beware: a simple replacement works only, if res < modulo shift */
5316 int bits = get_mode_size_bits(mode);
5317 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5318 res = tarval_mod(res, modulo);
5320 long bits = get_mode_size_bits(mode);
5321 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5323 /* shifting too much */
5324 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5326 ir_node *block = get_nodes_block(n);
5327 dbg_info *dbgi = get_irn_dbg_info(n);
5328 ir_mode *smode = get_irn_mode(right);
5329 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5330 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5333 return new_r_Const(irg, get_mode_null(mode));
5337 /* ok, we can replace it */
5338 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5339 block = get_nodes_block(n);
5341 in[0] = get_binop_left(left);
5342 in[1] = new_r_Const(irg, res);
5344 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5346 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5353 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5355 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5356 * (also with x >>s c1 when c1>=c2)
5358 static ir_node *transform_node_shl_shr(ir_node *n)
5361 ir_node *right = get_binop_right(n);
5371 ir_tarval *tv_shift;
5374 ir_relation relation;
5377 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5379 if (!is_Const(right))
5382 left = get_binop_left(n);
5383 mode = get_irn_mode(n);
5384 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5385 ir_node *shr_right = get_binop_right(left);
5387 if (!is_Const(shr_right))
5390 x = get_binop_left(left);
5391 tv_shr = get_Const_tarval(shr_right);
5392 tv_shl = get_Const_tarval(right);
5394 if (is_Shrs(left)) {
5395 /* shrs variant only allowed if c1 >= c2 */
5396 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5399 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5402 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5404 tv_mask = tarval_shl(tv_mask, tv_shl);
5405 } else if (is_Shr(n) && is_Shl(left)) {
5406 ir_node *shl_right = get_Shl_right(left);
5408 if (!is_Const(shl_right))
5411 x = get_Shl_left(left);
5412 tv_shr = get_Const_tarval(right);
5413 tv_shl = get_Const_tarval(shl_right);
5415 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5416 tv_mask = tarval_shr(tv_mask, tv_shr);
5421 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5422 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5425 assert(tv_mask != tarval_bad);
5426 assert(get_tarval_mode(tv_mask) == mode);
5428 block = get_nodes_block(n);
5429 irg = get_irn_irg(block);
5430 dbgi = get_irn_dbg_info(n);
5432 relation = tarval_cmp(tv_shl, tv_shr);
5433 if (relation == ir_relation_less || relation == ir_relation_equal) {
5434 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5435 new_const = new_r_Const(irg, tv_shift);
5437 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5439 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5442 assert(relation == ir_relation_greater);
5443 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5444 new_const = new_r_Const(irg, tv_shift);
5445 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5448 new_const = new_r_Const(irg, tv_mask);
5449 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5454 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5456 ir_mode *mode = get_tarval_mode(tv);
5457 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5458 return tarval_mod(tv, modulo_tv);
5461 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5462 ir_node *left, ir_node *right, ir_mode *mode);
5465 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5466 * then we can use that to minimize the value of Add(x, const) or
5467 * Sub(Const, x). In particular this often avoids 1 instruction in some
5468 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5469 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5471 static ir_node *transform_node_shift_modulo(ir_node *n,
5472 new_shift_func new_shift)
5474 ir_mode *mode = get_irn_mode(n);
5475 int modulo = get_mode_modulo_shift(mode);
5476 ir_node *newop = NULL;
5477 ir_mode *mode_right;
5484 if (get_mode_arithmetic(mode) != irma_twos_complement)
5486 if (!is_po2(modulo))
5489 irg = get_irn_irg(n);
5490 block = get_nodes_block(n);
5491 right = get_binop_right(n);
5492 mode_right = get_irn_mode(right);
5493 if (is_Const(right)) {
5494 ir_tarval *tv = get_Const_tarval(right);
5495 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5500 newop = new_r_Const(irg, tv_mod);
5501 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5502 ir_node *add_right = get_binop_right(right);
5503 if (is_Const(add_right)) {
5504 ir_tarval *tv = get_Const_tarval(add_right);
5505 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5510 newconst = new_r_Const(irg, tv_mod);
5511 newop = new_r_Add(block, get_binop_left(right), newconst,
5514 } else if (is_Sub(right)) {
5515 ir_node *sub_left = get_Sub_left(right);
5516 if (is_Const(sub_left)) {
5517 ir_tarval *tv = get_Const_tarval(sub_left);
5518 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5523 newconst = new_r_Const(irg, tv_mod);
5524 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5531 if (newop != NULL) {
5532 dbg_info *dbgi = get_irn_dbg_info(n);
5533 ir_node *left = get_binop_left(n);
5534 return new_shift(dbgi, block, left, newop, mode);
5542 static ir_node *transform_node_Shr(ir_node *n)
5544 ir_node *c, *oldn = n;
5545 ir_node *left = get_Shr_left(n);
5546 ir_node *right = get_Shr_right(n);
5547 ir_mode *mode = get_irn_mode(n);
5549 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5550 n = transform_node_shift(n);
5553 n = transform_node_shift_modulo(n, new_rd_Shr);
5555 n = transform_node_shl_shr(n);
5557 n = transform_node_shift_bitop(n);
5565 static ir_node *transform_node_Shrs(ir_node *n)
5568 ir_node *a = get_Shrs_left(n);
5569 ir_node *b = get_Shrs_right(n);
5570 ir_mode *mode = get_irn_mode(n);
5574 if (is_oversize_shift(n)) {
5575 ir_node *block = get_nodes_block(n);
5576 dbg_info *dbgi = get_irn_dbg_info(n);
5577 ir_mode *cmode = get_irn_mode(b);
5578 long val = get_mode_size_bits(cmode)-1;
5579 ir_graph *irg = get_irn_irg(n);
5580 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5581 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5584 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5585 n = transform_node_shift(n);
5589 n = transform_node_shift_modulo(n, new_rd_Shrs);
5592 n = transform_node_shift_bitop(n);
5596 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5597 attr = vrp_get_info(a);
5599 unsigned bits = get_mode_size_bits(mode);
5600 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5601 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5602 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5603 dbg_info *dbgi = get_irn_dbg_info(n);
5604 ir_node *block = get_nodes_block(n);
5605 return new_rd_Shr(dbgi, block, a, b, mode);
5615 static ir_node *transform_node_Shl(ir_node *n)
5617 ir_node *c, *oldn = n;
5618 ir_node *a = get_Shl_left(n);
5619 ir_node *b = get_Shl_right(n);
5620 ir_mode *mode = get_irn_mode(n);
5622 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5623 n = transform_node_shift(n);
5626 n = transform_node_shift_modulo(n, new_rd_Shl);
5628 n = transform_node_shl_shr(n);
5630 n = transform_node_shift_bitop(n);
5638 static ir_node *transform_node_Rotl(ir_node *n)
5640 ir_node *c, *oldn = n;
5641 ir_node *a = get_Rotl_left(n);
5642 ir_node *b = get_Rotl_right(n);
5643 ir_mode *mode = get_irn_mode(n);
5645 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5646 n = transform_node_shift(n);
5649 n = transform_node_shift_bitop(n);
5657 static ir_node *transform_node_Conv(ir_node *n)
5659 ir_node *c, *oldn = n;
5660 ir_mode *mode = get_irn_mode(n);
5661 ir_node *a = get_Conv_op(n);
5663 if (mode != mode_b && is_const_Phi(a)) {
5664 /* Do NOT optimize mode_b Conv's, this leads to remaining
5665 * Phib nodes later, because the conv_b_lower operation
5666 * is instantly reverted, when it tries to insert a Convb.
5668 c = apply_conv_on_phi(a, mode);
5670 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5675 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5676 ir_graph *irg = get_irn_irg(n);
5677 return new_r_Unknown(irg, mode);
5680 if (mode_is_reference(mode) &&
5681 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5683 ir_node *l = get_Add_left(a);
5684 ir_node *r = get_Add_right(a);
5685 dbg_info *dbgi = get_irn_dbg_info(a);
5686 ir_node *block = get_nodes_block(n);
5688 ir_node *lop = get_Conv_op(l);
5689 if (get_irn_mode(lop) == mode) {
5690 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5691 n = new_rd_Add(dbgi, block, lop, r, mode);
5696 ir_node *rop = get_Conv_op(r);
5697 if (get_irn_mode(rop) == mode) {
5698 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5699 n = new_rd_Add(dbgi, block, l, rop, mode);
5709 * Remove dead blocks and nodes in dead blocks
5710 * in keep alive list. We do not generate a new End node.
5712 static ir_node *transform_node_End(ir_node *n)
5714 int i, j, n_keepalives = get_End_n_keepalives(n);
5717 NEW_ARR_A(ir_node *, in, n_keepalives);
5719 for (i = j = 0; i < n_keepalives; ++i) {
5720 ir_node *ka = get_End_keepalive(n, i);
5722 /* no need to keep Bad */
5725 /* do not keep unreachable code */
5726 block = is_Block(ka) ? ka : get_nodes_block(ka);
5727 if (is_block_unreachable(block))
5731 if (j != n_keepalives)
5732 set_End_keepalives(n, j, in);
5736 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5738 if (is_Minus(a) && get_Minus_op(a) == b)
5740 if (is_Minus(b) && get_Minus_op(b) == a)
5742 if (is_Sub(a) && is_Sub(b)) {
5743 ir_node *a_left = get_Sub_left(a);
5744 ir_node *a_right = get_Sub_right(a);
5745 ir_node *b_left = get_Sub_left(b);
5746 ir_node *b_right = get_Sub_right(b);
5748 if (a_left == b_right && a_right == b_left)
5755 static const ir_node *skip_upconv(const ir_node *node)
5757 while (is_Conv(node)) {
5758 ir_mode *mode = get_irn_mode(node);
5759 const ir_node *op = get_Conv_op(node);
5760 ir_mode *op_mode = get_irn_mode(op);
5761 if (!smaller_mode(op_mode, mode))
5768 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5769 const ir_node *mux_true)
5774 ir_relation relation;
5780 * Note further that these optimization work even for floating point
5781 * with NaN's because -NaN == NaN.
5782 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5785 mode = get_irn_mode(mux_true);
5786 if (mode_honor_signed_zeros(mode))
5789 /* must be <, <=, >=, > */
5790 relation = get_Cmp_relation(sel);
5791 if ((relation & ir_relation_less_greater) == 0)
5794 if (!ir_is_negated_value(mux_true, mux_false))
5797 mux_true = skip_upconv(mux_true);
5798 mux_false = skip_upconv(mux_false);
5800 /* must be x cmp 0 */
5801 cmp_right = get_Cmp_right(sel);
5802 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5805 cmp_left = get_Cmp_left(sel);
5806 if (cmp_left == mux_false) {
5807 if (relation & ir_relation_less) {
5810 assert(relation & ir_relation_greater);
5813 } else if (cmp_left == mux_true) {
5814 if (relation & ir_relation_less) {
5817 assert(relation & ir_relation_greater);
5825 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5828 ir_node *cmp_left = get_Cmp_left(sel);
5829 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5832 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5833 const ir_node *mux_true)
5835 /* this code should return true each time transform_node_Mux would
5836 * optimize the Mux completely away */
5838 ir_mode *mode = get_irn_mode(mux_false);
5839 if (get_mode_arithmetic(mode) == irma_twos_complement
5840 && ir_mux_is_abs(sel, mux_false, mux_true))
5843 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5844 const ir_node *cmp_r = get_Cmp_right(sel);
5845 const ir_node *cmp_l = get_Cmp_left(sel);
5846 const ir_node *f = mux_false;
5847 const ir_node *t = mux_true;
5849 if (is_Const(t) && is_Const_null(t)) {
5854 if (is_And(cmp_l) && f == cmp_r) {
5855 ir_node *and_r = get_And_right(cmp_l);
5858 if (and_r == t && is_single_bit(and_r))
5860 and_l = get_And_left(cmp_l);
5861 if (and_l == t && is_single_bit(and_l))
5870 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5872 static ir_node *transform_Mux_set(ir_node *n)
5874 ir_node *cond = get_Mux_sel(n);
5879 ir_relation relation;
5893 left = get_Cmp_left(cond);
5894 mode = get_irn_mode(left);
5895 if (!mode_is_int(mode) && !mode_is_reference(mode))
5897 dest_mode = get_irn_mode(n);
5898 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5900 right = get_Cmp_right(cond);
5901 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5902 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5903 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5904 && relation != ir_relation_greater))
5909 case ir_relation_less:
5910 /* a < b -> (a - b) >> 31 */
5914 case ir_relation_less_equal:
5915 /* a <= b -> ~(a - b) >> 31 */
5920 case ir_relation_greater:
5921 /* a > b -> (b - a) >> 31 */
5925 case ir_relation_greater_equal:
5926 /* a >= b -> ~(a - b) >> 31 */
5935 dbgi = get_irn_dbg_info(n);
5936 block = get_nodes_block(n);
5937 irg = get_irn_irg(block);
5938 bits = get_mode_size_bits(dest_mode);
5939 tv = new_tarval_from_long(bits-1, mode_Iu);
5940 shift_cnt = new_rd_Const(dbgi, irg, tv);
5942 if (mode != dest_mode) {
5943 a = new_rd_Conv(dbgi, block, a, dest_mode);
5944 b = new_rd_Conv(dbgi, block, b, dest_mode);
5947 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5949 res = new_rd_Not(dbgi, block, res, dest_mode);
5951 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5956 * Optimize a Mux into some simpler cases.
5958 static ir_node *transform_node_Mux(ir_node *n)
5961 ir_node *sel = get_Mux_sel(n);
5962 ir_mode *mode = get_irn_mode(n);
5963 ir_node *t = get_Mux_true(n);
5964 ir_node *f = get_Mux_false(n);
5965 ir_graph *irg = get_irn_irg(n);
5967 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5968 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5969 int abs = ir_mux_is_abs(sel, f, t);
5971 dbg_info *dbgi = get_irn_dbg_info(n);
5972 ir_node *block = get_nodes_block(n);
5973 ir_node *op = ir_get_abs_op(sel, f, t);
5974 int bits = get_mode_size_bits(mode);
5975 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5976 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5977 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5980 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5982 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5988 /* first normalization step: try to move a constant to the false side,
5989 * 0 preferred on false side too */
5990 if (is_Cmp(sel) && is_Const(t) &&
5991 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5992 dbg_info *seldbgi = get_irn_dbg_info(sel);
5993 ir_node *block = get_nodes_block(sel);
5994 ir_relation relation = get_Cmp_relation(sel);
5999 /* Mux(x, a, b) => Mux(not(x), b, a) */
6000 relation = get_negated_relation(relation);
6001 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6002 get_Cmp_right(sel), relation);
6003 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6006 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6007 n = transform_Mux_set(n);
6012 /* the following optimisations create new mode_b nodes, so only do them
6013 * before mode_b lowering */
6014 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6016 ir_node* block = get_nodes_block(n);
6018 ir_node* c1 = get_Mux_sel(t);
6019 ir_node* t1 = get_Mux_true(t);
6020 ir_node* f1 = get_Mux_false(t);
6022 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6023 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6024 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6025 return new_r_Mux(block, and_, f1, t1, mode);
6026 } else if (f == t1) {
6027 /* Mux(cond0, Mux(cond1, x, y), x) */
6028 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6029 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6030 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6031 return new_r_Mux(block, and_, t1, f1, mode);
6033 } else if (is_Mux(f)) {
6034 ir_node* block = get_nodes_block(n);
6036 ir_node* c1 = get_Mux_sel(f);
6037 ir_node* t1 = get_Mux_true(f);
6038 ir_node* f1 = get_Mux_false(f);
6040 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6041 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6042 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6043 return new_r_Mux(block, or_, f1, t1, mode);
6044 } else if (t == f1) {
6045 /* Mux(cond0, x, Mux(cond1, y, x)) */
6046 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6047 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6048 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6049 return new_r_Mux(block, or_, t1, f1, mode);
6053 /* note: after normalization, false can only happen on default */
6054 if (mode == mode_b) {
6055 dbg_info *dbg = get_irn_dbg_info(n);
6056 ir_node *block = get_nodes_block(n);
6059 ir_tarval *tv_t = get_Const_tarval(t);
6060 if (tv_t == tarval_b_true) {
6062 /* Muxb(sel, true, false) = sel */
6063 assert(get_Const_tarval(f) == tarval_b_false);
6064 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6067 /* Muxb(sel, true, x) = Or(sel, x) */
6068 n = new_rd_Or(dbg, block, sel, f, mode_b);
6069 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6073 } else if (is_Const(f)) {
6074 ir_tarval *tv_f = get_Const_tarval(f);
6075 if (tv_f == tarval_b_true) {
6076 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6077 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6078 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6079 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6082 /* Muxb(sel, x, false) = And(sel, x) */
6083 assert(tv_f == tarval_b_false);
6084 n = new_rd_And(dbg, block, sel, t, mode_b);
6085 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6092 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6093 ir_relation relation = get_Cmp_relation(sel);
6094 ir_node *cmp_r = get_Cmp_right(sel);
6095 ir_node *cmp_l = get_Cmp_left(sel);
6096 ir_node *block = get_nodes_block(n);
6098 if (is_And(cmp_l) && f == cmp_r) {
6099 ir_node *and_r = get_And_right(cmp_l);
6102 if (and_r == t && is_single_bit(and_r)) {
6103 if (relation == ir_relation_equal) {
6104 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6105 n = new_rd_Eor(get_irn_dbg_info(n),
6106 block, cmp_l, t, mode);
6107 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6109 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6111 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6115 and_l = get_And_left(cmp_l);
6116 if (and_l == t && is_single_bit(and_l)) {
6117 if (relation == ir_relation_equal) {
6118 /* ((1 << n) & a) == 0, (1 << n), 0) */
6119 n = new_rd_Eor(get_irn_dbg_info(n),
6120 block, cmp_l, t, mode);
6121 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6123 /* ((1 << n) & a) != 0, (1 << n), 0) */
6125 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6136 * optimize Sync nodes that have other syncs as input we simply add the inputs
6137 * of the other sync to our own inputs
6139 static ir_node *transform_node_Sync(ir_node *n)
6141 int arity = get_Sync_n_preds(n);
6144 for (i = 0; i < arity;) {
6145 ir_node *pred = get_Sync_pred(n, i);
6149 /* Remove Bad predecessors */
6156 /* Remove duplicate predecessors */
6157 for (j = 0; j < i; ++j) {
6158 if (get_Sync_pred(n, j) == pred) {
6167 if (!is_Sync(pred)) {
6175 pred_arity = get_Sync_n_preds(pred);
6176 for (j = 0; j < pred_arity; ++j) {
6177 ir_node *pred_pred = get_Sync_pred(pred, j);
6182 add_irn_n(n, pred_pred);
6186 if (get_Sync_pred(n, k) == pred_pred) break;
6192 ir_graph *irg = get_irn_irg(n);
6193 return new_r_Bad(irg, mode_M);
6196 return get_Sync_pred(n, 0);
6199 /* rehash the sync node */
6204 static ir_node *transform_node_Load(ir_node *n)
6206 /* if our memory predecessor is a load from the same address, then reuse the
6207 * previous result */
6208 ir_node *mem = get_Load_mem(n);
6213 /* don't touch volatile loads */
6214 if (get_Load_volatility(n) == volatility_is_volatile)
6216 mem_pred = get_Proj_pred(mem);
6217 if (is_Load(mem_pred)) {
6218 ir_node *pred_load = mem_pred;
6220 /* conservatively compare the 2 loads. TODO: This could be less strict
6221 * with fixup code in some situations (like smaller/bigger modes) */
6222 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6224 if (get_Load_mode(pred_load) != get_Load_mode(n))
6226 /* all combinations of aligned/unaligned pred/n should be fine so we do
6227 * not compare the unaligned attribute */
6229 ir_node *block = get_nodes_block(n);
6230 ir_node *jmp = new_r_Jmp(block);
6231 ir_graph *irg = get_irn_irg(n);
6232 ir_node *bad = new_r_Bad(irg, mode_X);
6233 ir_mode *mode = get_Load_mode(n);
6234 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6235 ir_node *in[] = { mem, res, jmp, bad };
6236 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6239 } else if (is_Store(mem_pred)) {
6240 ir_node *pred_store = mem_pred;
6241 ir_node *value = get_Store_value(pred_store);
6243 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6245 if (get_irn_mode(value) != get_Load_mode(n))
6247 /* all combinations of aligned/unaligned pred/n should be fine so we do
6248 * not compare the unaligned attribute */
6250 ir_node *block = get_nodes_block(n);
6251 ir_node *jmp = new_r_Jmp(block);
6252 ir_graph *irg = get_irn_irg(n);
6253 ir_node *bad = new_r_Bad(irg, mode_X);
6254 ir_node *res = value;
6255 ir_node *in[] = { mem, res, jmp, bad };
6256 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6265 * optimize a trampoline Call into a direct Call
6267 static ir_node *transform_node_Call(ir_node *call)
6269 ir_node *callee = get_Call_ptr(call);
6270 ir_node *adr, *mem, *res, *bl, **in;
6271 ir_type *ctp, *mtp, *tp;
6275 size_t i, n_res, n_param;
6278 if (! is_Proj(callee))
6280 callee = get_Proj_pred(callee);
6281 if (! is_Builtin(callee))
6283 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6286 mem = get_Call_mem(call);
6288 if (skip_Proj(mem) == callee) {
6289 /* memory is routed to the trampoline, skip */
6290 mem = get_Builtin_mem(callee);
6293 /* build a new call type */
6294 mtp = get_Call_type(call);
6295 tdb = get_type_dbg_info(mtp);
6297 n_res = get_method_n_ress(mtp);
6298 n_param = get_method_n_params(mtp);
6299 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6301 for (i = 0; i < n_res; ++i)
6302 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6304 NEW_ARR_A(ir_node *, in, n_param + 1);
6306 /* FIXME: we don't need a new pointer type in every step */
6307 irg = get_irn_irg(call);
6308 tp = get_irg_frame_type(irg);
6309 tp = new_type_pointer(tp);
6310 set_method_param_type(ctp, 0, tp);
6312 in[0] = get_Builtin_param(callee, 2);
6313 for (i = 0; i < n_param; ++i) {
6314 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6315 in[i + 1] = get_Call_param(call, i);
6317 var = get_method_variadicity(mtp);
6318 set_method_variadicity(ctp, var);
6319 /* When we resolve a trampoline, the function must be called by a this-call */
6320 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6321 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6323 adr = get_Builtin_param(callee, 1);
6325 db = get_irn_dbg_info(call);
6326 bl = get_nodes_block(call);
6328 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6329 if (get_irn_pinned(call) == op_pin_state_floats)
6330 set_irn_pinned(res, op_pin_state_floats);
6334 void firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6338 ops->transform_node = transform_node_##a; \
6340 #define CASE_PROJ(a) \
6342 ops->transform_node_Proj = transform_node_Proj_##a; \
6344 #define CASE_PROJ_EX(a) \
6346 ops->transform_node = transform_node_##a; \
6347 ops->transform_node_Proj = transform_node_Proj_##a; \
6389 * Tries several [inplace] [optimizing] transformations and returns an
6390 * equivalent node. The difference to equivalent_node() is that these
6391 * transformations _do_ generate new nodes, and thus the old node must
6392 * not be freed even if the equivalent node isn't the old one.
6394 static ir_node *transform_node(ir_node *n)
6400 iro = get_irn_opcode_(n);
6401 /* constant expression evaluation / constant folding */
6402 if (get_opt_constant_folding()) {
6403 /* neither constants nor Tuple values can be evaluated */
6404 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6405 /* try to evaluate */
6406 ir_tarval *tv = computed_value(n);
6407 if (tv != tarval_bad) {
6408 /* evaluation was successful -- replace the node. */
6409 ir_graph *irg = get_irn_irg(n);
6411 n = new_r_Const(irg, tv);
6413 DBG_OPT_CSTEVAL(old_n, n);
6419 /* remove unnecessary nodes */
6420 if (get_opt_constant_folding() ||
6421 (iro == iro_Phi) || /* always optimize these nodes. */
6422 (iro == iro_Id) || /* ... */
6423 (iro == iro_Proj) || /* ... */
6424 (iro == iro_Block)) { /* Flags tested local. */
6425 n = equivalent_node(n);
6430 /* Some more constant expression evaluation. */
6431 if (get_opt_algebraic_simplification() ||
6432 (iro == iro_Cond) ||
6433 (iro == iro_Proj)) { /* Flags tested local. */
6434 if (n->op->ops.transform_node != NULL) {
6435 n = n->op->ops.transform_node(n);
6445 /* **************** Common Subexpression Elimination **************** */
6447 /** The size of the hash table used, should estimate the number of nodes
6449 #define N_IR_NODES 512
6451 /** Compares two exception attributes */
6452 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6454 const except_attr *ea = &a->attr.except;
6455 const except_attr *eb = &b->attr.except;
6456 return ea->pin_state != eb->pin_state;
6459 /** Compares the attributes of two Const nodes. */
6460 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6462 return get_Const_tarval(a) != get_Const_tarval(b);
6465 /** Compares the attributes of two Proj nodes. */
6466 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6468 return a->attr.proj.proj != b->attr.proj.proj;
6471 /** Compares the attributes of two Alloc nodes. */
6472 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6474 const alloc_attr *pa = &a->attr.alloc;
6475 const alloc_attr *pb = &b->attr.alloc;
6476 if (pa->where != pb->where || pa->type != pb->type)
6478 return node_cmp_exception(a, b);
6481 /** Compares the attributes of two Free nodes. */
6482 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6484 const free_attr *pa = &a->attr.free;
6485 const free_attr *pb = &b->attr.free;
6486 return (pa->where != pb->where) || (pa->type != pb->type);
6489 /** Compares the attributes of two SymConst nodes. */
6490 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6492 const symconst_attr *pa = &a->attr.symc;
6493 const symconst_attr *pb = &b->attr.symc;
6494 return (pa->kind != pb->kind)
6495 || (pa->sym.type_p != pb->sym.type_p);
6498 /** Compares the attributes of two Call nodes. */
6499 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6501 const call_attr *pa = &a->attr.call;
6502 const call_attr *pb = &b->attr.call;
6503 if (pa->type != pb->type)
6505 return node_cmp_exception(a, b);
6508 /** Compares the attributes of two Sel nodes. */
6509 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6511 const ir_entity *a_ent = get_Sel_entity(a);
6512 const ir_entity *b_ent = get_Sel_entity(b);
6513 return a_ent != b_ent;
6516 /** Compares the attributes of two Phi nodes. */
6517 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6519 /* we can only enter this function if both nodes have the same number of inputs,
6520 hence it is enough to check if one of them is a Phi0 */
6522 /* check the Phi0 pos attribute */
6523 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6528 /** Compares the attributes of two Conv nodes. */
6529 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6531 return get_Conv_strict(a) != get_Conv_strict(b);
6534 /** Compares the attributes of two Cast nodes. */
6535 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6537 return get_Cast_type(a) != get_Cast_type(b);
6540 /** Compares the attributes of two Load nodes. */
6541 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6543 if (get_Load_volatility(a) == volatility_is_volatile ||
6544 get_Load_volatility(b) == volatility_is_volatile)
6545 /* NEVER do CSE on volatile Loads */
6547 /* do not CSE Loads with different alignment. Be conservative. */
6548 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6550 if (get_Load_mode(a) != get_Load_mode(b))
6552 return node_cmp_exception(a, b);
6555 /** Compares the attributes of two Store nodes. */
6556 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6558 /* do not CSE Stores with different alignment. Be conservative. */
6559 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6561 /* NEVER do CSE on volatile Stores */
6562 if (get_Store_volatility(a) == volatility_is_volatile ||
6563 get_Store_volatility(b) == volatility_is_volatile)
6565 return node_cmp_exception(a, b);
6568 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6570 if (get_CopyB_type(a) != get_CopyB_type(b))
6573 return node_cmp_exception(a, b);
6576 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6578 return node_cmp_exception(a, b);
6581 /** Compares the attributes of two Div nodes. */
6582 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6584 const div_attr *ma = &a->attr.div;
6585 const div_attr *mb = &b->attr.div;
6586 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6588 return node_cmp_exception(a, b);
6591 /** Compares the attributes of two Mod nodes. */
6592 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6594 const mod_attr *ma = &a->attr.mod;
6595 const mod_attr *mb = &b->attr.mod;
6596 if (ma->resmode != mb->resmode)
6598 return node_cmp_exception(a, b);
6601 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6603 const cmp_attr *ma = &a->attr.cmp;
6604 const cmp_attr *mb = &b->attr.cmp;
6605 return ma->relation != mb->relation;
6608 /** Compares the attributes of two Confirm nodes. */
6609 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6611 const confirm_attr *ma = &a->attr.confirm;
6612 const confirm_attr *mb = &b->attr.confirm;
6613 return ma->relation != mb->relation;
6616 /** Compares the attributes of two Builtin nodes. */
6617 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6619 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6621 if (get_Builtin_type(a) != get_Builtin_type(b))
6623 return node_cmp_exception(a, b);
6626 /** Compares the attributes of two ASM nodes. */
6627 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6631 const ir_asm_constraint *ca;
6632 const ir_asm_constraint *cb;
6635 if (get_ASM_text(a) != get_ASM_text(b))
6638 /* Should we really check the constraints here? Should be better, but is strange. */
6639 n = get_ASM_n_input_constraints(a);
6640 if (n != get_ASM_n_input_constraints(b))
6643 ca = get_ASM_input_constraints(a);
6644 cb = get_ASM_input_constraints(b);
6645 for (i = 0; i < n; ++i) {
6646 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6647 || ca[i].mode != cb[i].mode)
6651 n = get_ASM_n_output_constraints(a);
6652 if (n != get_ASM_n_output_constraints(b))
6655 ca = get_ASM_output_constraints(a);
6656 cb = get_ASM_output_constraints(b);
6657 for (i = 0; i < n; ++i) {
6658 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6659 || ca[i].mode != cb[i].mode)
6663 n = get_ASM_n_clobbers(a);
6664 if (n != get_ASM_n_clobbers(b))
6667 cla = get_ASM_clobbers(a);
6668 clb = get_ASM_clobbers(b);
6669 for (i = 0; i < n; ++i) {
6670 if (cla[i] != clb[i])
6674 return node_cmp_exception(a, b);
6677 /** Compares the inexistent attributes of two Dummy nodes. */
6678 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6682 /* Dummy nodes never equal by definition */
6686 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6688 if (get_InstOf_type(a) != get_InstOf_type(b))
6690 return node_cmp_exception(a, b);
6693 void firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6697 ops->node_cmp_attr = node_cmp_attr_##a; \
6730 int identities_cmp(const void *elt, const void *key)
6732 ir_node *a = (ir_node *)elt;
6733 ir_node *b = (ir_node *)key;
6736 if (a == b) return 0;
6738 if ((get_irn_op(a) != get_irn_op(b)) ||
6739 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6741 /* compare if a's in and b's in are of equal length */
6742 irn_arity_a = get_irn_arity(a);
6743 if (irn_arity_a != get_irn_arity(b))
6746 /* blocks are never the same */
6750 if (get_irn_pinned(a) == op_pin_state_pinned) {
6751 /* for pinned nodes, the block inputs must be equal */
6752 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6755 ir_node *block_a = get_nodes_block(a);
6756 ir_node *block_b = get_nodes_block(b);
6757 if (! get_opt_global_cse()) {
6758 /* for block-local CSE both nodes must be in the same Block */
6759 if (block_a != block_b)
6762 /* The optimistic approach would be to do nothing here.
6763 * However doing GCSE optimistically produces a lot of partially dead code which appears
6764 * to be worse in practice than the missed opportunities.
6765 * So we use a very conservative variant here and only CSE if 1 value dominates the
6767 if (!block_dominates(block_a, block_b)
6768 && !block_dominates(block_b, block_a))
6773 /* compare a->in[0..ins] with b->in[0..ins] */
6774 for (i = 0; i < irn_arity_a; ++i) {
6775 ir_node *pred_a = get_irn_n(a, i);
6776 ir_node *pred_b = get_irn_n(b, i);
6777 if (pred_a != pred_b) {
6778 /* if both predecessors are CSE neutral they might be different */
6779 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6785 * here, we already now that the nodes are identical except their
6788 if (a->op->ops.node_cmp_attr)
6789 return a->op->ops.node_cmp_attr(a, b);
6794 unsigned ir_node_hash(const ir_node *node)
6796 return node->op->ops.hash(node);
6799 void new_identities(ir_graph *irg)
6801 if (irg->value_table != NULL)
6802 del_pset(irg->value_table);
6803 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6806 void del_identities(ir_graph *irg)
6808 if (irg->value_table != NULL)
6809 del_pset(irg->value_table);
6812 void ir_normalize_node(ir_node *n)
6814 if (is_op_commutative(get_irn_op(n))) {
6815 ir_node *l = get_binop_left(n);
6816 ir_node *r = get_binop_right(n);
6818 /* For commutative operators perform a OP b == b OP a but keep
6819 * constants on the RIGHT side. This helps greatly in some
6820 * optimizations. Moreover we use the idx number to make the form
6822 if (!operands_are_normalized(l, r)) {
6823 set_binop_left(n, r);
6824 set_binop_right(n, l);
6830 ir_node *identify_remember(ir_node *n)
6832 ir_graph *irg = get_irn_irg(n);
6833 pset *value_table = irg->value_table;
6836 if (value_table == NULL)
6839 ir_normalize_node(n);
6840 /* lookup or insert in hash table with given hash key. */
6841 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6844 /* n is reachable again */
6845 edges_node_revival(nn);
6852 * During construction we set the op_pin_state_pinned flag in the graph right
6853 * when the optimization is performed. The flag turning on procedure global
6854 * cse could be changed between two allocations. This way we are safe.
6856 * @param n The node to lookup
6858 static inline ir_node *identify_cons(ir_node *n)
6862 n = identify_remember(n);
6863 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6864 ir_graph *irg = get_irn_irg(n);
6865 set_irg_pinned(irg, op_pin_state_floats);
6870 void add_identities(ir_node *node)
6877 identify_remember(node);
6880 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6883 ir_graph *rem = current_ir_graph;
6885 current_ir_graph = irg;
6886 foreach_pset(irg->value_table, ir_node*, node) {
6889 current_ir_graph = rem;
6892 ir_node *optimize_node(ir_node *n)
6895 ir_graph *irg = get_irn_irg(n);
6896 unsigned iro = get_irn_opcode(n);
6899 /* Always optimize Phi nodes: part of the construction. */
6900 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6902 /* constant expression evaluation / constant folding */
6903 if (get_opt_constant_folding()) {
6904 /* neither constants nor Tuple values can be evaluated */
6905 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6906 /* try to evaluate */
6907 tv = computed_value(n);
6908 if (tv != tarval_bad) {
6913 * we MUST copy the node here temporarily, because it's still
6914 * needed for DBG_OPT_CSTEVAL
6916 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6917 oldn = (ir_node*)alloca(node_size);
6919 memcpy(oldn, n, node_size);
6920 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6922 /* ARG, copy the in array, we need it for statistics */
6923 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6925 /* note the inplace edges module */
6926 edges_node_deleted(n);
6928 /* evaluation was successful -- replace the node. */
6929 irg_kill_node(irg, n);
6930 nw = new_r_Const(irg, tv);
6932 DBG_OPT_CSTEVAL(oldn, nw);
6938 /* remove unnecessary nodes */
6939 if (get_opt_algebraic_simplification() ||
6940 (iro == iro_Phi) || /* always optimize these nodes. */
6942 (iro == iro_Proj) ||
6943 (iro == iro_Block) ) /* Flags tested local. */
6944 n = equivalent_node(n);
6946 /* Common Subexpression Elimination.
6948 * Checks whether n is already available.
6949 * The block input is used to distinguish different subexpressions. Right
6950 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6951 * subexpressions within a block.
6954 n = identify_cons(n);
6957 edges_node_deleted(oldn);
6959 /* We found an existing, better node, so we can deallocate the old node. */
6960 irg_kill_node(irg, oldn);
6964 /* Some more constant expression evaluation that does not allow to
6966 iro = get_irn_opcode(n);
6967 if (get_opt_algebraic_simplification() ||
6968 (iro == iro_Cond) ||
6969 (iro == iro_Proj)) { /* Flags tested local. */
6970 n = transform_node(n);
6973 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6974 if (get_opt_cse()) {
6976 n = identify_remember(o);
6984 ir_node *optimize_in_place_2(ir_node *n)
6986 if (!get_opt_optimize() && !is_Phi(n)) return n;
6991 /** common subexpression elimination **/
6992 /* Checks whether n is already available. */
6993 /* The block input is used to distinguish different subexpressions.
6994 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6995 * only finds common subexpressions within a block. */
6996 if (get_opt_cse()) {
6998 n = identify_remember(n);
7001 /* we have another existing node now, we do not optimize it here */
7006 n = transform_node(n);
7008 /* Now we can verify the node, as it has no dead inputs any more. */
7011 /* Now we have a legal, useful node. Enter it in hash table for cse.
7012 * Blocks should be unique anyways. (Except the successor of start:
7013 * is cse with the start block!)
7015 * Note: This is only necessary because some of the optimisations
7016 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7017 * bad practice and should be fixed sometime.
7019 if (get_opt_cse()) {
7021 n = identify_remember(o);
7029 ir_node *optimize_in_place(ir_node *n)
7031 ir_graph *irg = get_irn_irg(n);
7032 /* Handle graph state */
7033 assert(get_irg_phase_state(irg) != phase_building);
7035 if (get_opt_global_cse())
7036 set_irg_pinned(irg, op_pin_state_floats);
7038 /* FIXME: Maybe we could also test whether optimizing the node can
7039 change the control graph. */
7040 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7041 return optimize_in_place_2(n);
7045 * Calculate a hash value of a Const node.
7047 static unsigned hash_Const(const ir_node *node)
7051 /* special value for const, as they only differ in their tarval. */
7052 h = hash_ptr(node->attr.con.tarval);
7058 * Calculate a hash value of a SymConst node.
7060 static unsigned hash_SymConst(const ir_node *node)
7064 /* all others are pointers */
7065 h = hash_ptr(node->attr.symc.sym.type_p);
7070 void firm_set_default_hash(unsigned code, ir_op_ops *ops)
7074 ops->hash = hash_##a; \
7077 /* hash function already set */
7078 if (ops->hash != NULL)
7085 /* use input/mode default hash if no function was given */
7086 ops->hash = firm_default_hash;