2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
50 #include "firm_types.h"
51 #include "bitfiddle.h"
56 static bool is_Or_Eor_Add(const ir_node *node)
58 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
59 ir_node *left = get_binop_left(node);
60 ir_node *right = get_binop_right(node);
61 vrp_attr *vrp_left = vrp_get_info(left);
62 vrp_attr *vrp_right = vrp_get_info(right);
63 if (vrp_left != NULL && vrp_right != NULL) {
65 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
66 return tarval_is_null(vrp_val);
73 * Returns the tarval of a Const node or tarval_bad for all other nodes.
75 static ir_tarval *default_value_of(const ir_node *n)
78 return get_Const_tarval(n); /* might return tarval_bad */
83 value_of_func value_of_ptr = default_value_of;
85 void set_value_of_func(value_of_func func)
90 value_of_ptr = default_value_of;
94 * Return the value of a Constant.
96 static ir_tarval *computed_value_Const(const ir_node *n)
98 return get_Const_tarval(n);
102 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
104 static ir_tarval *computed_value_SymConst(const ir_node *n)
109 switch (get_SymConst_kind(n)) {
110 case symconst_type_size:
111 type = get_SymConst_type(n);
112 if (get_type_state(type) == layout_fixed)
113 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
115 case symconst_type_align:
116 type = get_SymConst_type(n);
117 if (get_type_state(type) == layout_fixed)
118 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
120 case symconst_ofs_ent:
121 ent = get_SymConst_entity(n);
122 type = get_entity_owner(ent);
123 if (get_type_state(type) == layout_fixed)
124 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
133 * Return the value of an Add.
135 static ir_tarval *computed_value_Add(const ir_node *n)
137 ir_node *a = get_Add_left(n);
138 ir_node *b = get_Add_right(n);
140 ir_tarval *ta = value_of(a);
141 ir_tarval *tb = value_of(b);
143 if ((ta != tarval_bad) && (tb != tarval_bad))
144 return tarval_add(ta, tb);
147 if ((is_Not(a) && get_Not_op(a) == b)
148 || (is_Not(b) && get_Not_op(b) == a)) {
149 return get_mode_all_one(get_irn_mode(n));
156 * Return the value of a Sub.
157 * Special case: a - a
159 static ir_tarval *computed_value_Sub(const ir_node *n)
161 ir_mode *mode = get_irn_mode(n);
162 ir_node *a = get_Sub_left(n);
163 ir_node *b = get_Sub_right(n);
168 if (! mode_is_float(mode)) {
171 return get_mode_null(mode);
177 if ((ta != tarval_bad) && (tb != tarval_bad))
178 return tarval_sub(ta, tb, mode);
184 * Return the value of a Carry.
185 * Special : a op 0, 0 op b
187 static ir_tarval *computed_value_Carry(const ir_node *n)
189 ir_node *a = get_binop_left(n);
190 ir_node *b = get_binop_right(n);
191 ir_mode *m = get_irn_mode(n);
192 ir_tarval *ta = value_of(a);
193 ir_tarval *tb = value_of(b);
195 if ((ta != tarval_bad) && (tb != tarval_bad)) {
197 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
199 if (tarval_is_null(ta) || tarval_is_null(tb))
200 return get_mode_null(m);
206 * Return the value of a Borrow.
209 static ir_tarval *computed_value_Borrow(const ir_node *n)
211 ir_node *a = get_binop_left(n);
212 ir_node *b = get_binop_right(n);
213 ir_mode *m = get_irn_mode(n);
214 ir_tarval *ta = value_of(a);
215 ir_tarval *tb = value_of(b);
217 if ((ta != tarval_bad) && (tb != tarval_bad)) {
218 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
219 } else if (tarval_is_null(ta)) {
220 return get_mode_null(m);
226 * Return the value of an unary Minus.
228 static ir_tarval *computed_value_Minus(const ir_node *n)
230 ir_node *a = get_Minus_op(n);
231 ir_tarval *ta = value_of(a);
233 if (ta != tarval_bad)
234 return tarval_neg(ta);
240 * Return the value of a Mul.
242 static ir_tarval *computed_value_Mul(const ir_node *n)
244 ir_node *a = get_Mul_left(n);
245 ir_node *b = get_Mul_right(n);
246 ir_tarval *ta = value_of(a);
247 ir_tarval *tb = value_of(b);
250 mode = get_irn_mode(n);
251 if (mode != get_irn_mode(a)) {
252 /* n * n = 2n bit multiplication */
253 ta = tarval_convert_to(ta, mode);
254 tb = tarval_convert_to(tb, mode);
257 if (ta != tarval_bad && tb != tarval_bad) {
258 return tarval_mul(ta, tb);
260 /* a * 0 != 0 if a == NaN or a == Inf */
261 if (!mode_is_float(mode)) {
262 /* a*0 = 0 or 0*b = 0 */
263 if (ta == get_mode_null(mode))
265 if (tb == get_mode_null(mode))
273 * Return the value of an And.
274 * Special case: a & 0, 0 & b
276 static ir_tarval *computed_value_And(const ir_node *n)
278 ir_node *a = get_And_left(n);
279 ir_node *b = get_And_right(n);
280 ir_tarval *ta = value_of(a);
281 ir_tarval *tb = value_of(b);
283 if ((ta != tarval_bad) && (tb != tarval_bad)) {
284 return tarval_and (ta, tb);
287 if (tarval_is_null(ta)) return ta;
288 if (tarval_is_null(tb)) return tb;
291 if ((is_Not(a) && get_Not_op(a) == b)
292 || (is_Not(b) && get_Not_op(b) == a)) {
293 return get_mode_null(get_irn_mode(n));
300 * Return the value of an Or.
301 * Special case: a | 1...1, 1...1 | b
303 static ir_tarval *computed_value_Or(const ir_node *n)
305 ir_node *a = get_Or_left(n);
306 ir_node *b = get_Or_right(n);
307 ir_tarval *ta = value_of(a);
308 ir_tarval *tb = value_of(b);
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_or (ta, tb);
314 if (tarval_is_all_one(ta)) return ta;
315 if (tarval_is_all_one(tb)) return tb;
318 if ((is_Not(a) && get_Not_op(a) == b)
319 || (is_Not(b) && get_Not_op(b) == a)) {
320 return get_mode_all_one(get_irn_mode(n));
326 * Return the value of an Eor.
328 static ir_tarval *computed_value_Eor(const ir_node *n)
330 ir_node *a = get_Eor_left(n);
331 ir_node *b = get_Eor_right(n);
336 return get_mode_null(get_irn_mode(n));
338 if ((is_Not(a) && get_Not_op(a) == b)
339 || (is_Not(b) && get_Not_op(b) == a)) {
340 return get_mode_all_one(get_irn_mode(n));
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_eor(ta, tb);
353 * Return the value of a Not.
355 static ir_tarval *computed_value_Not(const ir_node *n)
357 ir_node *a = get_Not_op(n);
358 ir_tarval *ta = value_of(a);
360 if (ta != tarval_bad)
361 return tarval_not(ta);
367 * Tests whether a shift shifts more bits than available in the mode
369 static bool is_oversize_shift(const ir_node *n)
371 ir_node *count = get_binop_right(n);
372 ir_mode *mode = get_irn_mode(n);
373 ir_tarval *tv = value_of(count);
376 if (tv == tarval_bad)
378 if (!tarval_is_long(tv))
380 shiftval = get_tarval_long(tv);
381 modulo_shift = get_mode_modulo_shift(mode);
382 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
385 return shiftval >= (long)get_mode_size_bits(mode);
389 * Return the value of a Shl.
391 static ir_tarval *computed_value_Shl(const ir_node *n)
393 ir_node *a = get_Shl_left(n);
394 ir_node *b = get_Shl_right(n);
396 ir_tarval *ta = value_of(a);
397 ir_tarval *tb = value_of(b);
399 if ((ta != tarval_bad) && (tb != tarval_bad)) {
400 return tarval_shl(ta, tb);
403 if (is_oversize_shift(n))
404 return get_mode_null(get_irn_mode(n));
410 * Return the value of a Shr.
412 static ir_tarval *computed_value_Shr(const ir_node *n)
414 ir_node *a = get_Shr_left(n);
415 ir_node *b = get_Shr_right(n);
417 ir_tarval *ta = value_of(a);
418 ir_tarval *tb = value_of(b);
420 if ((ta != tarval_bad) && (tb != tarval_bad)) {
421 return tarval_shr(ta, tb);
423 if (is_oversize_shift(n))
424 return get_mode_null(get_irn_mode(n));
430 * Return the value of a Shrs.
432 static ir_tarval *computed_value_Shrs(const ir_node *n)
434 ir_node *a = get_Shrs_left(n);
435 ir_node *b = get_Shrs_right(n);
437 ir_tarval *ta = value_of(a);
438 ir_tarval *tb = value_of(b);
440 if ((ta != tarval_bad) && (tb != tarval_bad)) {
441 return tarval_shrs(ta, tb);
447 * Return the value of a Rotl.
449 static ir_tarval *computed_value_Rotl(const ir_node *n)
451 ir_node *a = get_Rotl_left(n);
452 ir_node *b = get_Rotl_right(n);
454 ir_tarval *ta = value_of(a);
455 ir_tarval *tb = value_of(b);
457 if ((ta != tarval_bad) && (tb != tarval_bad)) {
458 return tarval_rotl(ta, tb);
463 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
465 ir_mode *mode = get_irn_mode(node);
466 if (get_mode_arithmetic(mode) != irma_twos_complement
467 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
471 ir_node *count = get_Shl_right(node);
472 if (is_Const(count)) {
473 ir_tarval *tv = get_Const_tarval(count);
474 if (tarval_is_long(tv)) {
475 long shiftval = get_tarval_long(tv);
476 long destbits = get_mode_size_bits(dest_mode);
477 if (shiftval >= destbits
478 && shiftval < (long)get_mode_modulo_shift(mode))
484 ir_node *right = get_And_right(node);
485 if (is_Const(right)) {
486 ir_tarval *tv = get_Const_tarval(right);
487 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
488 return tarval_is_null(conved);
495 * Return the value of a Conv.
497 static ir_tarval *computed_value_Conv(const ir_node *n)
499 ir_node *a = get_Conv_op(n);
500 ir_tarval *ta = value_of(a);
501 ir_mode *mode = get_irn_mode(n);
503 if (ta != tarval_bad)
504 return tarval_convert_to(ta, get_irn_mode(n));
506 if (ir_zero_when_converted(a, mode))
507 return get_mode_null(mode);
513 * Calculate the value of a Mux: can be evaluated, if the
514 * sel and the right input are known.
516 static ir_tarval *computed_value_Mux(const ir_node *n)
518 ir_node *sel = get_Mux_sel(n);
519 ir_tarval *ts = value_of(sel);
521 if (ts == get_tarval_b_true()) {
522 ir_node *v = get_Mux_true(n);
525 else if (ts == get_tarval_b_false()) {
526 ir_node *v = get_Mux_false(n);
533 * Calculate the value of a Confirm: can be evaluated,
534 * if it has the form Confirm(x, '=', Const).
536 static ir_tarval *computed_value_Confirm(const ir_node *n)
538 if (get_Confirm_relation(n) == ir_relation_equal) {
539 ir_tarval *tv = value_of(get_Confirm_bound(n));
540 if (tv != tarval_bad)
543 return value_of(get_Confirm_value(n));
547 * gives a (conservative) estimation of possible relation when comparing
550 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
551 const ir_node *right)
553 ir_relation possible = ir_relation_true;
554 ir_tarval *tv_l = value_of(left);
555 ir_tarval *tv_r = value_of(right);
556 ir_mode *mode = get_irn_mode(left);
557 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
558 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
560 /* both values known - evaluate them */
561 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
562 possible = tarval_cmp(tv_l, tv_r);
563 /* we can return now, won't get any better */
566 /* a == a is never less or greater (but might be equal or unordered) */
568 possible &= ~ir_relation_less_greater;
569 /* unordered results only happen for float compares */
570 if (!mode_is_float(mode))
571 possible &= ~ir_relation_unordered;
572 /* values can never be less than the least representable number or
573 * greater than the greatest representable number */
575 possible &= ~ir_relation_greater;
577 possible &= ~ir_relation_less;
579 possible &= ~ir_relation_greater;
581 possible &= ~ir_relation_less;
582 /* maybe vrp can tell us more */
583 possible &= vrp_cmp(left, right);
584 /* Alloc nodes never return null (but throw an exception) */
585 if (is_Alloc(left) && tarval_is_null(tv_r))
586 possible &= ~ir_relation_equal;
587 /* stuff known through confirm nodes */
588 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
589 possible &= get_Confirm_relation(left);
591 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
592 ir_relation relation = get_Confirm_relation(right);
593 relation = get_inversed_relation(relation);
594 possible &= relation;
600 static ir_tarval *compute_cmp(const ir_node *cmp)
602 ir_node *left = get_Cmp_left(cmp);
603 ir_node *right = get_Cmp_right(cmp);
604 ir_relation possible = ir_get_possible_cmp_relations(left, right);
605 ir_relation relation = get_Cmp_relation(cmp);
607 /* if none of the requested relations is possible, return false */
608 if ((possible & relation) == ir_relation_false)
609 return tarval_b_false;
610 /* if possible relations are a subset of the requested ones return true */
611 if ((possible & ~relation) == ir_relation_false)
612 return tarval_b_true;
614 return computed_value_Cmp_Confirm(cmp, left, right, relation);
618 * some people want to call compute_cmp directly, in this case we have to
619 * test the constant folding flag again
621 static ir_tarval *compute_cmp_ext(const ir_node *cmp)
623 if (!get_opt_constant_folding())
625 return compute_cmp(cmp);
629 * Return the value of a Cmp.
631 * The basic idea here is to determine which relations are possible and which
632 * one are definitely impossible.
634 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
636 /* we can't construct Constb after lowering mode_b nodes */
637 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
640 return compute_cmp(cmp);
644 * Calculate the value of an integer Div.
645 * Special case: 0 / b
647 static ir_tarval *do_computed_value_Div(const ir_node *div)
649 const ir_node *a = get_Div_left(div);
650 const ir_node *b = get_Div_right(div);
651 const ir_mode *mode = get_Div_resmode(div);
652 ir_tarval *ta = value_of(a);
654 const ir_node *dummy;
656 /* cannot optimize 0 / b = 0 because of NaN */
657 if (!mode_is_float(mode)) {
658 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
659 return ta; /* 0 / b == 0 if b != 0 */
662 if (ta != tarval_bad && tb != tarval_bad)
663 return tarval_div(ta, tb);
668 * Calculate the value of an integer Mod of two nodes.
669 * Special case: a % 1
671 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
673 ir_tarval *ta = value_of(a);
674 ir_tarval *tb = value_of(b);
676 /* Compute a % 1 or c1 % c2 */
677 if (tarval_is_one(tb))
678 return get_mode_null(get_irn_mode(a));
679 if (ta != tarval_bad && tb != tarval_bad)
680 return tarval_mod(ta, tb);
685 * Return the value of a Proj(Div).
687 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
689 long proj_nr = get_Proj_proj(n);
690 if (proj_nr != pn_Div_res)
693 return do_computed_value_Div(get_Proj_pred(n));
697 * Return the value of a Proj(Mod).
699 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
701 long proj_nr = get_Proj_proj(n);
703 if (proj_nr == pn_Mod_res) {
704 const ir_node *mod = get_Proj_pred(n);
705 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
711 * Return the value of a Proj.
713 static ir_tarval *computed_value_Proj(const ir_node *proj)
715 ir_node *n = get_Proj_pred(proj);
717 if (n->op->ops.computed_value_Proj != NULL)
718 return n->op->ops.computed_value_Proj(proj);
723 * If the parameter n can be computed, return its value, else tarval_bad.
724 * Performs constant folding.
726 * @param n The node this should be evaluated
728 ir_tarval *computed_value(const ir_node *n)
730 vrp_attr *vrp = vrp_get_info(n);
731 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
732 return vrp->bits_set;
734 if (n->op->ops.computed_value)
735 return n->op->ops.computed_value(n);
739 void firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
743 ops->computed_value = computed_value_##a; \
745 #define CASE_PROJ(a) \
747 ops->computed_value_Proj = computed_value_Proj_##a; \
783 * Optimize operations that are commutative and have neutral 0,
784 * so a op 0 = 0 op a = a.
786 static ir_node *equivalent_node_neutral_zero(ir_node *n)
790 ir_node *a = get_binop_left(n);
791 ir_node *b = get_binop_right(n);
796 /* After running compute_node there is only one constant predecessor.
797 Find this predecessors value and remember the other node: */
798 if ((tv = value_of(a)) != tarval_bad) {
800 } else if ((tv = value_of(b)) != tarval_bad) {
805 /* If this predecessors constant value is zero, the operation is
806 * unnecessary. Remove it.
808 * Beware: If n is a Add, the mode of on and n might be different
809 * which happens in this rare construction: NULL + 3.
810 * Then, a Conv would be needed which we cannot include here.
812 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
815 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
822 * Eor is commutative and has neutral 0.
824 static ir_node *equivalent_node_Eor(ir_node *n)
830 n = equivalent_node_neutral_zero(n);
831 if (n != oldn) return n;
834 b = get_Eor_right(n);
836 if (is_Eor(a) || is_Or_Eor_Add(a)) {
837 ir_node *aa = get_binop_left(a);
838 ir_node *ab = get_binop_right(a);
841 /* (a ^ b) ^ a -> b */
843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
845 } else if (ab == b) {
846 /* (a ^ b) ^ b -> a */
848 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
852 if (is_Eor(b) || is_Or_Eor_Add(b)) {
853 ir_node *ba = get_binop_left(b);
854 ir_node *bb = get_binop_right(b);
857 /* a ^ (a ^ b) -> b */
859 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
861 } else if (bb == a) {
862 /* a ^ (b ^ a) -> b */
864 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
872 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
874 * The second one looks strange, but this construct
875 * is used heavily in the LCC sources :-).
877 * Beware: The Mode of an Add may be different than the mode of its
878 * predecessors, so we could not return a predecessors in all cases.
880 static ir_node *equivalent_node_Add(ir_node *n)
883 ir_node *left, *right;
884 ir_mode *mode = get_irn_mode(n);
886 n = equivalent_node_neutral_zero(n);
890 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
891 if (mode_is_float(mode)) {
892 ir_graph *irg = get_irn_irg(n);
893 if (get_irg_fp_model(irg) & fp_strict_algebraic)
897 left = get_Add_left(n);
898 right = get_Add_right(n);
901 if (get_Sub_right(left) == right) {
904 n = get_Sub_left(left);
905 if (mode == get_irn_mode(n)) {
906 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
912 if (get_Sub_right(right) == left) {
915 n = get_Sub_left(right);
916 if (mode == get_irn_mode(n)) {
917 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
926 * optimize operations that are not commutative but have neutral 0 on left,
929 static ir_node *equivalent_node_left_zero(ir_node *n)
933 ir_node *a = get_binop_left(n);
934 ir_node *b = get_binop_right(n);
935 ir_tarval *tb = value_of(b);
937 if (tarval_is_null(tb)) {
940 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
945 #define equivalent_node_Shl equivalent_node_left_zero
946 #define equivalent_node_Shr equivalent_node_left_zero
947 #define equivalent_node_Shrs equivalent_node_left_zero
948 #define equivalent_node_Rotl equivalent_node_left_zero
951 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
953 * The second one looks strange, but this construct
954 * is used heavily in the LCC sources :-).
956 * Beware: The Mode of a Sub may be different than the mode of its
957 * predecessors, so we could not return a predecessors in all cases.
959 static ir_node *equivalent_node_Sub(ir_node *n)
963 ir_mode *mode = get_irn_mode(n);
966 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
967 if (mode_is_float(mode)) {
968 ir_graph *irg = get_irn_irg(n);
969 if (get_irg_fp_model(irg) & fp_strict_algebraic)
973 b = get_Sub_right(n);
976 /* Beware: modes might be different */
977 if (tarval_is_null(tb)) {
978 ir_node *a = get_Sub_left(n);
979 if (mode == get_irn_mode(a)) {
982 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
990 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
993 * -(-a) == a, but might overflow two times.
994 * We handle it anyway here but the better way would be a
995 * flag. This would be needed for Pascal for instance.
997 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1000 ir_node *pred = get_unop_op(n);
1002 /* optimize symmetric unop */
1003 if (get_irn_op(pred) == get_irn_op(n)) {
1004 n = get_unop_op(pred);
1005 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1010 /** Optimize Not(Not(x)) == x. */
1011 #define equivalent_node_Not equivalent_node_idempotent_unop
1013 /** -(-x) == x ??? Is this possible or can --x raise an
1014 out of bounds exception if min =! max? */
1015 #define equivalent_node_Minus equivalent_node_idempotent_unop
1018 * Optimize a * 1 = 1 * a = a.
1020 static ir_node *equivalent_node_Mul(ir_node *n)
1023 ir_node *a = get_Mul_left(n);
1025 /* we can handle here only the n * n = n bit cases */
1026 if (get_irn_mode(n) == get_irn_mode(a)) {
1027 ir_node *b = get_Mul_right(n);
1031 * Mul is commutative and has again an other neutral element.
1032 * Constants are place right, so check this case first.
1035 if (tarval_is_one(tv)) {
1037 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1040 if (tarval_is_one(tv)) {
1042 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1050 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1052 static ir_node *equivalent_node_Or(ir_node *n)
1056 ir_node *a = get_Or_left(n);
1057 ir_node *b = get_Or_right(n);
1061 n = a; /* idempotence */
1062 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1065 /* constants are normalized to right, check this side first */
1067 if (tarval_is_null(tv)) {
1069 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1073 if (tarval_is_null(tv)) {
1075 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1083 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1085 static ir_node *equivalent_node_And(ir_node *n)
1089 ir_node *a = get_And_left(n);
1090 ir_node *b = get_And_right(n);
1094 n = a; /* idempotence */
1095 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1098 /* constants are normalized to right, check this side first */
1100 if (tarval_is_all_one(tv)) {
1102 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1105 if (tv != get_tarval_bad()) {
1106 ir_mode *mode = get_irn_mode(n);
1107 if (!mode_is_signed(mode) && is_Conv(a)) {
1108 ir_node *convop = get_Conv_op(a);
1109 ir_mode *convopmode = get_irn_mode(convop);
1110 if (!mode_is_signed(convopmode)) {
1111 /* Check Conv(all_one) & Const = all_one */
1112 ir_tarval *one = get_mode_all_one(convopmode);
1113 ir_tarval *conv = tarval_convert_to(one, mode);
1114 ir_tarval *and = tarval_and(conv, tv);
1116 if (tarval_is_all_one(and)) {
1117 /* Conv(X) & Const = X */
1119 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1126 if (tarval_is_all_one(tv)) {
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1132 if ((is_Or(a) || is_Or_Eor_Add(a))
1133 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1135 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1139 if ((is_Or(b) || is_Or_Eor_Add(b))
1140 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1142 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1149 * Try to remove useless Conv's:
1151 static ir_node *equivalent_node_Conv(ir_node *n)
1154 ir_node *a = get_Conv_op(n);
1156 ir_mode *n_mode = get_irn_mode(n);
1157 ir_mode *a_mode = get_irn_mode(a);
1160 if (n_mode == a_mode) { /* No Conv necessary */
1161 if (get_Conv_strict(n)) {
1164 /* neither Minus nor Confirm change the precision,
1165 so we can "look-through" */
1168 p = get_Minus_op(p);
1169 } else if (is_Confirm(p)) {
1170 p = get_Confirm_value(p);
1176 if (is_Conv(p) && get_Conv_strict(p)) {
1177 /* we known already, that a_mode == n_mode, and neither
1178 Minus change the mode, so the second Conv
1180 assert(get_irn_mode(p) == n_mode);
1182 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1186 ir_node *pred = get_Proj_pred(p);
1187 if (is_Load(pred)) {
1188 /* Loads always return with the exact precision of n_mode */
1189 assert(get_Load_mode(pred) == n_mode);
1191 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1194 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1195 pred = get_Proj_pred(pred);
1196 if (is_Start(pred)) {
1197 /* Arguments always return with the exact precision,
1198 as strictConv's are place before Call -- if the
1199 caller was compiled with the same setting.
1200 Otherwise, the semantics is probably still right. */
1201 assert(get_irn_mode(p) == n_mode);
1203 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1209 /* special case: the immediate predecessor is also a Conv */
1210 if (! get_Conv_strict(a)) {
1211 /* first one is not strict, kick it */
1213 a_mode = get_irn_mode(a);
1217 /* else both are strict conv, second is superfluous */
1219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1224 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1227 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1228 ir_node *b = get_Conv_op(a);
1229 ir_mode *b_mode = get_irn_mode(b);
1231 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1232 /* both are strict conv */
1233 if (smaller_mode(a_mode, n_mode)) {
1234 /* both are strict, but the first is smaller, so
1235 the second cannot remove more precision, remove the
1237 set_Conv_strict(n, 0);
1240 if (n_mode == b_mode) {
1241 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1242 if (n_mode == mode_b) {
1243 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1246 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1247 if (values_in_mode(b_mode, a_mode)) {
1248 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1249 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1254 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1255 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1256 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1257 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1259 if (float_mantissa >= int_mantissa) {
1261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1266 if (smaller_mode(b_mode, a_mode)) {
1267 if (get_Conv_strict(n))
1268 set_Conv_strict(b, 1);
1269 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1280 * - fold Phi-nodes, iff they have only one predecessor except
1283 static ir_node *equivalent_node_Phi(ir_node *n)
1288 ir_node *first_val = NULL; /* to shutup gcc */
1290 if (!get_opt_optimize() &&
1291 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1294 n_preds = get_Phi_n_preds(n);
1296 /* Phi of dead Region without predecessors. */
1300 /* Find first non-self-referencing input */
1301 for (i = 0; i < n_preds; ++i) {
1302 first_val = get_Phi_pred(n, i);
1303 /* not self pointer */
1304 if (first_val != n) {
1305 /* then found first value. */
1310 /* search for rest of inputs, determine if any of these
1311 are non-self-referencing */
1312 while (++i < n_preds) {
1313 ir_node *scnd_val = get_Phi_pred(n, i);
1314 if (scnd_val != n && scnd_val != first_val) {
1319 if (i >= n_preds && !is_Dummy(first_val)) {
1320 /* Fold, if no multiple distinct non-self-referencing inputs */
1322 DBG_OPT_PHI(oldn, n);
1328 * Optimize Proj(Tuple).
1330 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1332 ir_node *oldn = proj;
1333 ir_node *tuple = get_Proj_pred(proj);
1335 /* Remove the Tuple/Proj combination. */
1336 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1337 DBG_OPT_TUPLE(oldn, tuple, proj);
1343 * Optimize a / 1 = a.
1345 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1347 ir_node *oldn = proj;
1348 ir_node *div = get_Proj_pred(proj);
1349 ir_node *b = get_Div_right(div);
1350 ir_tarval *tb = value_of(b);
1352 /* Div is not commutative. */
1353 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1354 switch (get_Proj_proj(proj)) {
1356 proj = get_Div_mem(div);
1357 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1361 proj = get_Div_left(div);
1362 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1366 /* we cannot replace the exception Proj's here, this is done in
1367 transform_node_Proj_Div() */
1375 * Optimize CopyB(mem, x, x) into a Nop.
1377 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1379 ir_node *oldn = proj;
1380 ir_node *copyb = get_Proj_pred(proj);
1381 ir_node *a = get_CopyB_dst(copyb);
1382 ir_node *b = get_CopyB_src(copyb);
1385 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1386 switch (get_Proj_proj(proj)) {
1388 proj = get_CopyB_mem(copyb);
1389 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1397 * Optimize Bounds(idx, idx, upper) into idx.
1399 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1401 ir_node *oldn = proj;
1402 ir_node *bound = get_Proj_pred(proj);
1403 ir_node *idx = get_Bound_index(bound);
1404 ir_node *pred = skip_Proj(idx);
1407 if (idx == get_Bound_lower(bound))
1409 else if (is_Bound(pred)) {
1411 * idx was Bounds checked previously, it is still valid if
1412 * lower <= pred_lower && pred_upper <= upper.
1414 ir_node *lower = get_Bound_lower(bound);
1415 ir_node *upper = get_Bound_upper(bound);
1416 if (get_Bound_lower(pred) == lower &&
1417 get_Bound_upper(pred) == upper) {
1419 * One could expect that we simply return the previous
1420 * Bound here. However, this would be wrong, as we could
1421 * add an exception Proj to a new location then.
1422 * So, we must turn in into a tuple.
1428 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1429 switch (get_Proj_proj(proj)) {
1431 DBG_OPT_EXC_REM(proj);
1432 proj = get_Bound_mem(bound);
1436 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1439 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1447 * Does all optimizations on nodes that must be done on its Projs
1448 * because of creating new nodes.
1450 static ir_node *equivalent_node_Proj(ir_node *proj)
1452 ir_node *n = get_Proj_pred(proj);
1453 if (n->op->ops.equivalent_node_Proj)
1454 return n->op->ops.equivalent_node_Proj(proj);
1461 static ir_node *equivalent_node_Id(ir_node *n)
1469 DBG_OPT_ID(oldn, n);
1476 static ir_node *equivalent_node_Mux(ir_node *n)
1478 ir_node *oldn = n, *sel = get_Mux_sel(n);
1480 ir_tarval *ts = value_of(sel);
1482 if (ts == tarval_bad && is_Cmp(sel)) {
1483 /* try again with a direct call to compute_cmp, as we don't care
1484 * about the MODEB_LOWERED flag here */
1485 ts = compute_cmp_ext(sel);
1488 /* Mux(true, f, t) == t */
1489 if (ts == tarval_b_true) {
1490 n = get_Mux_true(n);
1491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1494 /* Mux(false, f, t) == f */
1495 if (ts == tarval_b_false) {
1496 n = get_Mux_false(n);
1497 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1500 n_t = get_Mux_true(n);
1501 n_f = get_Mux_false(n);
1503 /* Mux(v, x, T) == x */
1504 if (is_Unknown(n_f)) {
1506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1509 /* Mux(v, T, x) == x */
1510 if (is_Unknown(n_t)) {
1512 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1516 /* Mux(v, x, x) == x */
1519 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1522 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1523 ir_relation relation = get_Cmp_relation(sel);
1524 ir_node *f = get_Mux_false(n);
1525 ir_node *t = get_Mux_true(n);
1528 * Note further that these optimization work even for floating point
1529 * with NaN's because -NaN == NaN.
1530 * However, if +0 and -0 is handled differently, we cannot use the first one.
1532 ir_node *const cmp_l = get_Cmp_left(sel);
1533 ir_node *const cmp_r = get_Cmp_right(sel);
1536 case ir_relation_equal:
1537 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1538 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1540 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1545 case ir_relation_less_greater:
1546 case ir_relation_unordered_less_greater:
1547 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1548 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1559 * Note: normalization puts the constant on the right side,
1560 * so we check only one case.
1562 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1563 /* Mux(t CMP 0, X, t) */
1564 if (is_Minus(f) && get_Minus_op(f) == t) {
1565 /* Mux(t CMP 0, -t, t) */
1566 if (relation == ir_relation_equal) {
1567 /* Mux(t == 0, -t, t) ==> -t */
1569 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1570 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1571 /* Mux(t != 0, -t, t) ==> t */
1573 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1583 * Remove Confirm nodes if setting is on.
1584 * Replace Confirms(x, '=', Constlike) by Constlike.
1586 static ir_node *equivalent_node_Confirm(ir_node *n)
1588 ir_node *pred = get_Confirm_value(n);
1589 ir_relation relation = get_Confirm_relation(n);
1591 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1593 * rare case: two identical Confirms one after another,
1594 * replace the second one with the first.
1597 pred = get_Confirm_value(n);
1603 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1604 * perform no actual computation, as, e.g., the Id nodes. It does not create
1605 * new nodes. It is therefore safe to free n if the node returned is not n.
1606 * If a node returns a Tuple we can not just skip it. If the size of the
1607 * in array fits, we transform n into a tuple (e.g., Div).
1609 ir_node *equivalent_node(ir_node *n)
1611 if (n->op->ops.equivalent_node)
1612 return n->op->ops.equivalent_node(n);
1616 void firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1620 ops->equivalent_node = equivalent_node_##a; \
1622 #define CASE_PROJ(a) \
1624 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1659 * Returns non-zero if a node is a Phi node
1660 * with all predecessors constant.
1662 static int is_const_Phi(ir_node *n)
1666 if (! is_Phi(n) || get_irn_arity(n) == 0)
1668 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1669 if (! is_Const(get_irn_n(n, i)))
1675 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1676 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1679 * in reality eval_func should be tarval (*eval_func)() but incomplete
1680 * declarations are bad style and generate noisy warnings
1682 typedef void (*eval_func)(void);
1685 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1687 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1689 if (eval == (eval_func) tarval_sub) {
1690 tarval_sub_type func = (tarval_sub_type)eval;
1692 return func(a, b, mode);
1694 tarval_binop_type func = (tarval_binop_type)eval;
1701 * Apply an evaluator on a binop with a constant operators (and one Phi).
1703 * @param phi the Phi node
1704 * @param other the other operand
1705 * @param eval an evaluator function
1706 * @param mode the mode of the result, may be different from the mode of the Phi!
1707 * @param left if non-zero, other is the left operand, else the right
1709 * @return a new Phi node if the conversion was successful, NULL else
1711 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1717 int i, n = get_irn_arity(phi);
1719 NEW_ARR_A(void *, res, n);
1721 for (i = 0; i < n; ++i) {
1722 pred = get_irn_n(phi, i);
1723 tv = get_Const_tarval(pred);
1724 tv = do_eval(eval, other, tv, mode);
1726 if (tv == tarval_bad) {
1727 /* folding failed, bad */
1733 for (i = 0; i < n; ++i) {
1734 pred = get_irn_n(phi, i);
1735 tv = get_Const_tarval(pred);
1736 tv = do_eval(eval, tv, other, mode);
1738 if (tv == tarval_bad) {
1739 /* folding failed, bad */
1745 irg = get_irn_irg(phi);
1746 for (i = 0; i < n; ++i) {
1747 pred = get_irn_n(phi, i);
1748 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1750 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1754 * Apply an evaluator on a binop with two constant Phi.
1756 * @param a the left Phi node
1757 * @param b the right Phi node
1758 * @param eval an evaluator function
1759 * @param mode the mode of the result, may be different from the mode of the Phi!
1761 * @return a new Phi node if the conversion was successful, NULL else
1763 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1765 ir_tarval *tv_l, *tv_r, *tv;
1771 if (get_nodes_block(a) != get_nodes_block(b))
1774 n = get_irn_arity(a);
1775 NEW_ARR_A(void *, res, n);
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(a, i);
1779 tv_l = get_Const_tarval(pred);
1780 pred = get_irn_n(b, i);
1781 tv_r = get_Const_tarval(pred);
1782 tv = do_eval(eval, tv_l, tv_r, mode);
1784 if (tv == tarval_bad) {
1785 /* folding failed, bad */
1790 irg = get_irn_irg(a);
1791 for (i = 0; i < n; ++i) {
1792 pred = get_irn_n(a, i);
1793 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1795 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1799 * Apply an evaluator on a unop with a constant operator (a Phi).
1801 * @param phi the Phi node
1802 * @param eval an evaluator function
1804 * @return a new Phi node if the conversion was successful, NULL else
1806 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1813 int i, n = get_irn_arity(phi);
1815 NEW_ARR_A(void *, res, n);
1816 for (i = 0; i < n; ++i) {
1817 pred = get_irn_n(phi, i);
1818 tv = get_Const_tarval(pred);
1821 if (tv == tarval_bad) {
1822 /* folding failed, bad */
1827 mode = get_irn_mode(phi);
1828 irg = get_irn_irg(phi);
1829 for (i = 0; i < n; ++i) {
1830 pred = get_irn_n(phi, i);
1831 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1833 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1837 * Apply a conversion on a constant operator (a Phi).
1839 * @param phi the Phi node
1841 * @return a new Phi node if the conversion was successful, NULL else
1843 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1849 int i, n = get_irn_arity(phi);
1851 NEW_ARR_A(void *, res, n);
1852 for (i = 0; i < n; ++i) {
1853 pred = get_irn_n(phi, i);
1854 tv = get_Const_tarval(pred);
1855 tv = tarval_convert_to(tv, mode);
1857 if (tv == tarval_bad) {
1858 /* folding failed, bad */
1863 irg = get_irn_irg(phi);
1864 for (i = 0; i < n; ++i) {
1865 pred = get_irn_n(phi, i);
1866 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1868 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1872 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1873 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1874 * If possible, remove the Conv's.
1876 static ir_node *transform_node_AddSub(ir_node *n)
1878 ir_mode *mode = get_irn_mode(n);
1880 if (mode_is_reference(mode)) {
1881 ir_node *left = get_binop_left(n);
1882 ir_node *right = get_binop_right(n);
1883 unsigned ref_bits = get_mode_size_bits(mode);
1885 if (is_Conv(left)) {
1886 ir_mode *lmode = get_irn_mode(left);
1887 unsigned bits = get_mode_size_bits(lmode);
1889 if (ref_bits == bits &&
1890 mode_is_int(lmode) &&
1891 get_mode_arithmetic(lmode) == irma_twos_complement) {
1892 ir_node *pre = get_Conv_op(left);
1893 ir_mode *pre_mode = get_irn_mode(pre);
1895 if (mode_is_int(pre_mode) &&
1896 get_mode_size_bits(pre_mode) == bits &&
1897 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1898 /* ok, this conv just changes to sign, moreover the calculation
1899 * is done with same number of bits as our address mode, so
1900 * we can ignore the conv as address calculation can be viewed
1901 * as either signed or unsigned
1903 set_binop_left(n, pre);
1908 if (is_Conv(right)) {
1909 ir_mode *rmode = get_irn_mode(right);
1910 unsigned bits = get_mode_size_bits(rmode);
1912 if (ref_bits == bits &&
1913 mode_is_int(rmode) &&
1914 get_mode_arithmetic(rmode) == irma_twos_complement) {
1915 ir_node *pre = get_Conv_op(right);
1916 ir_mode *pre_mode = get_irn_mode(pre);
1918 if (mode_is_int(pre_mode) &&
1919 get_mode_size_bits(pre_mode) == bits &&
1920 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1921 /* ok, this conv just changes to sign, moreover the calculation
1922 * is done with same number of bits as our address mode, so
1923 * we can ignore the conv as address calculation can be viewed
1924 * as either signed or unsigned
1926 set_binop_right(n, pre);
1931 /* let address arithmetic use unsigned modes */
1932 if (is_Const(right)) {
1933 ir_mode *rmode = get_irn_mode(right);
1935 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1936 /* convert a AddP(P, *s) into AddP(P, *u) */
1937 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1939 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1940 set_binop_right(n, pre);
1948 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1951 if (is_Const(b) && is_const_Phi(a)) { \
1952 /* check for Op(Phi, Const) */ \
1953 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1955 else if (is_Const(a) && is_const_Phi(b)) { \
1956 /* check for Op(Const, Phi) */ \
1957 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1959 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1960 /* check for Op(Phi, Phi) */ \
1961 c = apply_binop_on_2_phis(a, b, eval, mode); \
1964 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1969 #define HANDLE_UNOP_PHI(eval, a, c) \
1972 if (is_const_Phi(a)) { \
1973 /* check for Op(Phi) */ \
1974 c = apply_unop_on_phi(a, eval); \
1976 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1983 * Create a 0 constant of given mode.
1985 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1987 ir_tarval *tv = get_mode_null(mode);
1988 ir_node *cnst = new_r_Const(irg, tv);
1993 static bool is_shiftop(const ir_node *n)
1995 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1998 /* the order of the values is important! */
1999 typedef enum const_class {
2005 static const_class classify_const(const ir_node* n)
2007 if (is_Const(n)) return const_const;
2008 if (is_irn_constlike(n)) return const_like;
2013 * Determines whether r is more constlike or has a larger index (in that order)
2016 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2018 const const_class l_order = classify_const(l);
2019 const const_class r_order = classify_const(r);
2021 l_order > r_order ||
2022 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2025 static bool is_cmp_unequal(const ir_node *node)
2027 ir_relation relation = get_Cmp_relation(node);
2028 ir_node *left = get_Cmp_left(node);
2029 ir_node *right = get_Cmp_right(node);
2030 ir_mode *mode = get_irn_mode(left);
2032 if (relation == ir_relation_less_greater)
2035 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2036 return relation == ir_relation_greater;
2041 * returns true for Cmp(x == 0) or Cmp(x != 0)
2043 static bool is_cmp_equality_zero(const ir_node *node)
2045 ir_relation relation;
2046 ir_node *right = get_Cmp_right(node);
2048 if (!is_Const(right) || !is_Const_null(right))
2050 relation = get_Cmp_relation(node);
2051 return relation == ir_relation_equal
2052 || relation == ir_relation_less_greater
2053 || (!mode_is_signed(get_irn_mode(right))
2054 && relation == ir_relation_greater);
2058 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2059 * Such pattern may arise in bitfield stores.
2061 * value c4 value c4 & c2
2062 * AND c3 AND c1 | c3
2069 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2072 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2074 ir_node *irn_and, *c1;
2076 ir_node *and_l, *c3;
2077 ir_node *value, *c4;
2078 ir_node *new_and, *new_const, *block;
2079 ir_mode *mode = get_irn_mode(irn_or);
2081 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2085 irn_and = get_binop_left(irn_or);
2086 c1 = get_binop_right(irn_or);
2087 if (!is_Const(c1) || !is_And(irn_and))
2090 or_l = get_binop_left(irn_and);
2091 c2 = get_binop_right(irn_and);
2095 tv1 = get_Const_tarval(c1);
2096 tv2 = get_Const_tarval(c2);
2098 tv = tarval_or(tv1, tv2);
2099 if (tarval_is_all_one(tv)) {
2100 /* the AND does NOT clear a bit with isn't set by the OR */
2101 set_binop_left(irn_or, or_l);
2102 set_binop_right(irn_or, c1);
2104 /* check for more */
2108 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2111 and_l = get_binop_left(or_l);
2112 c3 = get_binop_right(or_l);
2113 if (!is_Const(c3) || !is_And(and_l))
2116 value = get_binop_left(and_l);
2117 c4 = get_binop_right(and_l);
2121 /* ok, found the pattern, check for conditions */
2122 assert(mode == get_irn_mode(irn_and));
2123 assert(mode == get_irn_mode(or_l));
2124 assert(mode == get_irn_mode(and_l));
2126 tv3 = get_Const_tarval(c3);
2127 tv4 = get_Const_tarval(c4);
2129 tv = tarval_or(tv4, tv2);
2130 if (!tarval_is_all_one(tv)) {
2131 /* have at least one 0 at the same bit position */
2135 if (tv3 != tarval_andnot(tv3, tv4)) {
2136 /* bit in the or_mask is outside the and_mask */
2140 if (tv1 != tarval_andnot(tv1, tv2)) {
2141 /* bit in the or_mask is outside the and_mask */
2145 /* ok, all conditions met */
2146 block = get_irn_n(irn_or, -1);
2147 irg = get_irn_irg(block);
2149 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2151 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2153 set_binop_left(irn_or, new_and);
2154 set_binop_right(irn_or, new_const);
2156 /* check for more */
2161 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2163 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2165 ir_mode *mode = get_irn_mode(irn_or);
2166 ir_node *shl, *shr, *block;
2167 ir_node *irn, *x, *c1, *c2, *n;
2168 ir_tarval *tv1, *tv2;
2170 /* some backends can't handle rotl */
2171 if (!be_get_backend_param()->support_rotl)
2174 if (! mode_is_int(mode))
2177 shl = get_binop_left(irn_or);
2178 shr = get_binop_right(irn_or);
2187 } else if (!is_Shl(shl)) {
2189 } else if (!is_Shr(shr)) {
2192 x = get_Shl_left(shl);
2193 if (x != get_Shr_left(shr))
2196 c1 = get_Shl_right(shl);
2197 c2 = get_Shr_right(shr);
2198 if (is_Const(c1) && is_Const(c2)) {
2199 tv1 = get_Const_tarval(c1);
2200 if (! tarval_is_long(tv1))
2203 tv2 = get_Const_tarval(c2);
2204 if (! tarval_is_long(tv2))
2207 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2208 != (int) get_mode_size_bits(mode))
2211 /* yet, condition met */
2212 block = get_nodes_block(irn_or);
2214 n = new_r_Rotl(block, x, c1, mode);
2216 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2220 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2221 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2222 if (!ir_is_negated_value(c1, c2)) {
2226 /* yet, condition met */
2227 block = get_nodes_block(irn_or);
2228 n = new_r_Rotl(block, x, c1, mode);
2229 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2234 * Prototype of a recursive transform function
2235 * for bitwise distributive transformations.
2237 typedef ir_node* (*recursive_transform)(ir_node *n);
2240 * makes use of distributive laws for and, or, eor
2241 * and(a OP c, b OP c) -> and(a, b) OP c
2242 * note, might return a different op than n
2244 static ir_node *transform_bitwise_distributive(ir_node *n,
2245 recursive_transform trans_func)
2248 ir_node *a = get_binop_left(n);
2249 ir_node *b = get_binop_right(n);
2250 ir_op *op = get_irn_op(a);
2251 ir_op *op_root = get_irn_op(n);
2253 if (op != get_irn_op(b))
2256 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2257 if (op == op_Conv) {
2258 ir_node *a_op = get_Conv_op(a);
2259 ir_node *b_op = get_Conv_op(b);
2260 ir_mode *a_mode = get_irn_mode(a_op);
2261 ir_mode *b_mode = get_irn_mode(b_op);
2262 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2263 ir_node *blk = get_nodes_block(n);
2266 set_binop_left(n, a_op);
2267 set_binop_right(n, b_op);
2268 set_irn_mode(n, a_mode);
2270 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2272 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2278 /* nothing to gain here */
2282 if (op == op_Shrs || op == op_Shr || op == op_Shl
2283 || op == op_And || op == op_Or || op == op_Eor) {
2284 ir_node *a_left = get_binop_left(a);
2285 ir_node *a_right = get_binop_right(a);
2286 ir_node *b_left = get_binop_left(b);
2287 ir_node *b_right = get_binop_right(b);
2289 ir_node *op1 = NULL;
2290 ir_node *op2 = NULL;
2292 if (is_op_commutative(op)) {
2293 if (a_left == b_left) {
2297 } else if (a_left == b_right) {
2301 } else if (a_right == b_left) {
2307 if (a_right == b_right) {
2314 /* (a sop c) & (b sop c) => (a & b) sop c */
2315 ir_node *blk = get_nodes_block(n);
2317 ir_node *new_n = exact_copy(n);
2318 set_binop_left(new_n, op1);
2319 set_binop_right(new_n, op2);
2320 new_n = trans_func(new_n);
2322 if (op_root == op_Eor && op == op_Or) {
2323 dbg_info *dbgi = get_irn_dbg_info(n);
2324 ir_mode *mode = get_irn_mode(c);
2326 c = new_rd_Not(dbgi, blk, c, mode);
2327 n = new_rd_And(dbgi, blk, new_n, c, mode);
2330 set_nodes_block(n, blk);
2331 set_binop_left(n, new_n);
2332 set_binop_right(n, c);
2336 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2345 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2347 * - and, or, xor instead of &
2348 * - Shl, Shr, Shrs, rotl instead of >>
2349 * (with a special case for Or/Xor + Shrs)
2351 * This normalisation is usually good for the backend since << C can often be
2352 * matched as address-mode.
2354 static ir_node *transform_node_bitop_shift(ir_node *n)
2356 ir_graph *irg = get_irn_irg(n);
2357 ir_node *left = get_binop_left(n);
2358 ir_node *right = get_binop_right(n);
2359 ir_mode *mode = get_irn_mode(n);
2360 ir_node *shift_left;
2361 ir_node *shift_right;
2363 dbg_info *dbg_bitop;
2364 dbg_info *dbg_shift;
2370 ir_tarval *tv_bitop;
2372 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2375 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2376 if (!is_Const(right) || !is_shiftop(left))
2379 shift_left = get_binop_left(left);
2380 shift_right = get_binop_right(left);
2381 if (!is_Const(shift_right))
2384 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2385 if (is_Shrs(left)) {
2386 /* TODO this could be improved */
2390 irg = get_irn_irg(n);
2391 block = get_nodes_block(n);
2392 dbg_bitop = get_irn_dbg_info(n);
2393 dbg_shift = get_irn_dbg_info(left);
2394 tv1 = get_Const_tarval(shift_right);
2395 tv2 = get_Const_tarval(right);
2396 assert(get_tarval_mode(tv2) == mode);
2399 tv_bitop = tarval_shr(tv2, tv1);
2401 /* Check whether we have lost some bits during the right shift. */
2403 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2405 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2408 } else if (is_Shr(left)) {
2411 * TODO this can be improved by checking whether
2412 * the left shift produces an overflow
2416 tv_bitop = tarval_shl(tv2, tv1);
2418 assert(is_Rotl(left));
2419 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2421 new_const = new_r_Const(irg, tv_bitop);
2424 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2425 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2426 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2429 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2433 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2434 } else if (is_Shr(left)) {
2435 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2437 assert(is_Rotl(left));
2438 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2444 static bool complement_values(const ir_node *a, const ir_node *b)
2446 if (is_Not(a) && get_Not_op(a) == b)
2448 if (is_Not(b) && get_Not_op(b) == a)
2450 if (is_Const(a) && is_Const(b)) {
2451 ir_tarval *tv_a = get_Const_tarval(a);
2452 ir_tarval *tv_b = get_Const_tarval(b);
2453 return tarval_not(tv_a) == tv_b;
2458 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2461 * for associative operations fold:
2462 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2463 * This is a "light" version of the reassociation phase
2465 static ir_node *fold_constant_associativity(ir_node *node,
2466 tv_fold_binop_func fold)
2471 ir_node *right = get_binop_right(node);
2472 ir_node *left_right;
2479 if (!is_Const(right))
2482 op = get_irn_op(node);
2483 left = get_binop_left(node);
2484 if (get_irn_op(left) != op)
2487 left_right = get_binop_right(left);
2488 if (!is_Const(left_right))
2491 left_left = get_binop_left(left);
2492 c0 = get_Const_tarval(left_right);
2493 c1 = get_Const_tarval(right);
2494 irg = get_irn_irg(node);
2495 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2497 new_c = fold(c0, c1);
2498 if (new_c == tarval_bad)
2500 new_const = new_r_Const(irg, new_c);
2501 new_node = exact_copy(node);
2502 set_binop_left(new_node, left_left);
2503 set_binop_right(new_node, new_const);
2510 static ir_node *transform_node_Or_(ir_node *n)
2513 ir_node *a = get_binop_left(n);
2514 ir_node *b = get_binop_right(n);
2518 n = fold_constant_associativity(n, tarval_or);
2522 if (is_Not(a) && is_Not(b)) {
2523 /* ~a | ~b = ~(a&b) */
2524 ir_node *block = get_nodes_block(n);
2526 mode = get_irn_mode(n);
2529 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2530 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2531 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2535 /* we can combine the relations of two compares with the same operands */
2536 if (is_Cmp(a) && is_Cmp(b)) {
2537 ir_node *a_left = get_Cmp_left(a);
2538 ir_node *a_right = get_Cmp_right(a);
2539 ir_node *b_left = get_Cmp_left(b);
2540 ir_node *b_right = get_Cmp_right(b);
2541 if (a_left == b_left && b_left == b_right) {
2542 dbg_info *dbgi = get_irn_dbg_info(n);
2543 ir_node *block = get_nodes_block(n);
2544 ir_relation a_relation = get_Cmp_relation(a);
2545 ir_relation b_relation = get_Cmp_relation(b);
2546 ir_relation new_relation = a_relation | b_relation;
2547 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2549 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2550 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2551 && !mode_is_float(get_irn_mode(a_left))
2552 && !mode_is_float(get_irn_mode(b_left))) {
2553 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2554 ir_graph *irg = get_irn_irg(n);
2555 dbg_info *dbgi = get_irn_dbg_info(n);
2556 ir_node *block = get_nodes_block(n);
2557 ir_mode *a_mode = get_irn_mode(a_left);
2558 ir_mode *b_mode = get_irn_mode(b_left);
2559 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2560 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2561 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2562 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2563 ir_node *zero = create_zero_const(irg, b_mode);
2564 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2566 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2567 ir_graph *irg = get_irn_irg(n);
2568 dbg_info *dbgi = get_irn_dbg_info(n);
2569 ir_node *block = get_nodes_block(n);
2570 ir_mode *a_mode = get_irn_mode(a_left);
2571 ir_mode *b_mode = get_irn_mode(b_left);
2572 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2573 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2574 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2575 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2576 ir_node *zero = create_zero_const(irg, a_mode);
2577 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2582 mode = get_irn_mode(n);
2583 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2585 n = transform_node_Or_bf_store(n);
2588 n = transform_node_Or_Rotl(n);
2592 n = transform_bitwise_distributive(n, transform_node_Or_);
2595 n = transform_node_bitop_shift(n);
2602 static ir_node *transform_node_Or(ir_node *n)
2604 if (is_Or_Eor_Add(n)) {
2605 dbg_info *dbgi = get_irn_dbg_info(n);
2606 ir_node *block = get_nodes_block(n);
2607 ir_node *left = get_Or_left(n);
2608 ir_node *right = get_Or_right(n);
2609 ir_mode *mode = get_irn_mode(n);
2610 return new_rd_Add(dbgi, block, left, right, mode);
2612 return transform_node_Or_(n);
2618 static ir_node *transform_node_Eor_(ir_node *n)
2621 ir_node *a = get_binop_left(n);
2622 ir_node *b = get_binop_right(n);
2623 ir_mode *mode = get_irn_mode(n);
2626 n = fold_constant_associativity(n, tarval_eor);
2630 /* we can combine the relations of two compares with the same operands */
2631 if (is_Cmp(a) && is_Cmp(b)) {
2632 ir_node *a_left = get_Cmp_left(a);
2633 ir_node *a_right = get_Cmp_left(a);
2634 ir_node *b_left = get_Cmp_left(b);
2635 ir_node *b_right = get_Cmp_right(b);
2636 if (a_left == b_left && b_left == b_right) {
2637 dbg_info *dbgi = get_irn_dbg_info(n);
2638 ir_node *block = get_nodes_block(n);
2639 ir_relation a_relation = get_Cmp_relation(a);
2640 ir_relation b_relation = get_Cmp_relation(b);
2641 ir_relation new_relation = a_relation ^ b_relation;
2642 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2646 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2648 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2649 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2650 dbg_info *dbg = get_irn_dbg_info(n);
2651 ir_node *block = get_nodes_block(n);
2652 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2653 ir_node *new_left = get_Not_op(a);
2654 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2655 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2657 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2658 dbg_info *dbg = get_irn_dbg_info(n);
2659 ir_node *block = get_nodes_block(n);
2660 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2661 ir_node *new_right = get_Not_op(b);
2662 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2663 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2667 /* x ^ 1...1 -> ~1 */
2668 if (is_Const(b) && is_Const_all_one(b)) {
2669 n = new_r_Not(get_nodes_block(n), a, mode);
2670 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2674 n = transform_bitwise_distributive(n, transform_node_Eor_);
2677 n = transform_node_bitop_shift(n);
2684 static ir_node *transform_node_Eor(ir_node *n)
2686 if (is_Or_Eor_Add(n)) {
2687 dbg_info *dbgi = get_irn_dbg_info(n);
2688 ir_node *block = get_nodes_block(n);
2689 ir_node *left = get_Eor_left(n);
2690 ir_node *right = get_Eor_right(n);
2691 ir_mode *mode = get_irn_mode(n);
2692 return new_rd_Add(dbgi, block, left, right, mode);
2694 return transform_node_Eor_(n);
2698 * Do the AddSub optimization, then Transform
2699 * Constant folding on Phi
2700 * Add(a,a) -> Mul(a, 2)
2701 * Add(Mul(a, x), a) -> Mul(a, x+1)
2702 * if the mode is integer or float.
2703 * Transform Add(a,-b) into Sub(a,b).
2704 * Reassociation might fold this further.
2706 static ir_node *transform_node_Add(ir_node *n)
2714 n = fold_constant_associativity(n, tarval_add);
2718 n = transform_node_AddSub(n);
2722 a = get_Add_left(n);
2723 b = get_Add_right(n);
2724 mode = get_irn_mode(n);
2726 if (mode_is_reference(mode)) {
2727 ir_mode *lmode = get_irn_mode(a);
2729 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2730 /* an Add(a, NULL) is a hidden Conv */
2731 dbg_info *dbg = get_irn_dbg_info(n);
2732 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2736 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2737 ir_tarval *tv = get_Const_tarval(b);
2738 ir_tarval *min = get_mode_min(mode);
2739 /* if all bits are set, then this has the same effect as a Not.
2740 * Note that the following == gives false for different modes which
2741 * is exactly what we want */
2743 dbg_info *dbgi = get_irn_dbg_info(n);
2744 ir_graph *irg = get_irn_irg(n);
2745 ir_node *block = get_nodes_block(n);
2746 ir_node *cnst = new_r_Const(irg, min);
2747 return new_rd_Eor(dbgi, block, a, cnst, mode);
2751 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2753 /* for FP the following optimizations are only allowed if
2754 * fp_strict_algebraic is disabled */
2755 if (mode_is_float(mode)) {
2756 ir_graph *irg = get_irn_irg(n);
2757 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2761 if (mode_is_num(mode)) {
2762 ir_graph *irg = get_irn_irg(n);
2763 /* the following code leads to endless recursion when Mul are replaced
2764 * by a simple instruction chain */
2765 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2766 && a == b && mode_is_int(mode)) {
2767 ir_node *block = get_nodes_block(n);
2770 get_irn_dbg_info(n),
2773 new_r_Const_long(irg, mode, 2),
2775 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2780 get_irn_dbg_info(n),
2785 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2790 get_irn_dbg_info(n),
2795 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2798 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2799 /* Here we rely on constants be on the RIGHT side */
2801 ir_node *op = get_Not_op(a);
2803 if (is_Const(b) && is_Const_one(b)) {
2805 ir_node *blk = get_nodes_block(n);
2806 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2807 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2814 if (is_Or_Eor_Add(n)) {
2815 n = transform_node_Or_(n);
2818 n = transform_node_Eor_(n);
2827 * returns -cnst or NULL if impossible
2829 static ir_node *const_negate(ir_node *cnst)
2831 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2832 dbg_info *dbgi = get_irn_dbg_info(cnst);
2833 ir_graph *irg = get_irn_irg(cnst);
2834 if (tv == tarval_bad) return NULL;
2835 return new_rd_Const(dbgi, irg, tv);
2839 * Do the AddSub optimization, then Transform
2840 * Constant folding on Phi
2841 * Sub(0,a) -> Minus(a)
2842 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2843 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2844 * Sub(Add(a, x), x) -> a
2845 * Sub(x, Add(x, a)) -> -a
2846 * Sub(x, Const) -> Add(x, -Const)
2848 static ir_node *transform_node_Sub(ir_node *n)
2854 n = transform_node_AddSub(n);
2856 a = get_Sub_left(n);
2857 b = get_Sub_right(n);
2859 mode = get_irn_mode(n);
2861 if (mode_is_int(mode)) {
2862 ir_mode *lmode = get_irn_mode(a);
2864 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2865 /* a Sub(a, NULL) is a hidden Conv */
2866 dbg_info *dbg = get_irn_dbg_info(n);
2867 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2868 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2872 if (mode == lmode &&
2873 get_mode_arithmetic(mode) == irma_twos_complement &&
2875 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2877 dbg_info *dbg = get_irn_dbg_info(n);
2878 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2879 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2885 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2887 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2888 if (mode_is_float(mode)) {
2889 ir_graph *irg = get_irn_irg(n);
2890 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2894 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2895 /* a - C -> a + (-C) */
2896 ir_node *cnst = const_negate(b);
2898 ir_node *block = get_nodes_block(n);
2899 dbg_info *dbgi = get_irn_dbg_info(n);
2901 n = new_rd_Add(dbgi, block, a, cnst, mode);
2902 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2907 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2908 dbg_info *dbg = get_irn_dbg_info(n);
2909 ir_node *block = get_nodes_block(n);
2910 ir_node *left = get_Minus_op(a);
2911 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2913 n = new_rd_Minus(dbg, block, add, mode);
2914 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2916 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2917 dbg_info *dbg = get_irn_dbg_info(n);
2918 ir_node *block = get_nodes_block(n);
2919 ir_node *right = get_Minus_op(b);
2921 n = new_rd_Add(dbg, block, a, right, mode);
2922 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2924 } else if (is_Sub(b)) {
2925 /* a - (b - c) -> a + (c - b)
2926 * -> (a - b) + c iff (b - c) is a pointer */
2927 dbg_info *s_dbg = get_irn_dbg_info(b);
2928 ir_node *s_left = get_Sub_left(b);
2929 ir_node *s_right = get_Sub_right(b);
2930 ir_mode *s_mode = get_irn_mode(b);
2931 if (mode_is_reference(s_mode)) {
2932 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2933 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2934 dbg_info *a_dbg = get_irn_dbg_info(n);
2937 s_right = new_r_Conv(lowest_block, s_right, mode);
2938 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2940 ir_node *s_block = get_nodes_block(b);
2941 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2942 dbg_info *a_dbg = get_irn_dbg_info(n);
2943 ir_node *a_block = get_nodes_block(n);
2945 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2947 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2950 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2951 ir_node *m_right = get_Mul_right(b);
2952 if (is_Const(m_right)) {
2953 ir_node *cnst2 = const_negate(m_right);
2954 if (cnst2 != NULL) {
2955 dbg_info *m_dbg = get_irn_dbg_info(b);
2956 ir_node *m_block = get_nodes_block(b);
2957 ir_node *m_left = get_Mul_left(b);
2958 ir_mode *m_mode = get_irn_mode(b);
2959 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2960 dbg_info *a_dbg = get_irn_dbg_info(n);
2961 ir_node *a_block = get_nodes_block(n);
2963 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2964 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2971 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2972 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2974 get_irn_dbg_info(n),
2978 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2981 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2982 ir_node *left = get_binop_left(a);
2983 ir_node *right = get_binop_right(a);
2985 /* FIXME: Does the Conv's work only for two complement or generally? */
2987 if (mode != get_irn_mode(right)) {
2988 /* This Sub is an effective Cast */
2989 right = new_r_Conv(get_nodes_block(n), right, mode);
2992 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2994 } else if (right == b) {
2995 if (mode != get_irn_mode(left)) {
2996 /* This Sub is an effective Cast */
2997 left = new_r_Conv(get_nodes_block(n), left, mode);
3000 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3004 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
3005 ir_node *left = get_binop_left(b);
3006 ir_node *right = get_binop_right(b);
3008 /* FIXME: Does the Conv's work only for two complement or generally? */
3010 ir_mode *r_mode = get_irn_mode(right);
3012 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3013 if (mode != r_mode) {
3014 /* This Sub is an effective Cast */
3015 n = new_r_Conv(get_nodes_block(n), n, mode);
3017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3019 } else if (right == a) {
3020 ir_mode *l_mode = get_irn_mode(left);
3022 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3023 if (mode != l_mode) {
3024 /* This Sub is an effective Cast */
3025 n = new_r_Conv(get_nodes_block(n), n, mode);
3027 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3031 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3032 ir_mode *mode = get_irn_mode(a);
3034 if (mode == get_irn_mode(b)) {
3036 ir_node *op_a = get_Conv_op(a);
3037 ir_node *op_b = get_Conv_op(b);
3039 /* check if it's allowed to skip the conv */
3040 ma = get_irn_mode(op_a);
3041 mb = get_irn_mode(op_b);
3043 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3044 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3047 set_Sub_right(n, b);
3053 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3054 if (!is_reassoc_running() && is_Mul(a)) {
3055 ir_node *ma = get_Mul_left(a);
3056 ir_node *mb = get_Mul_right(a);
3059 ir_node *blk = get_nodes_block(n);
3060 ir_graph *irg = get_irn_irg(n);
3062 get_irn_dbg_info(n),
3066 get_irn_dbg_info(n),
3069 new_r_Const(irg, get_mode_one(mode)),
3072 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3074 } else if (mb == b) {
3075 ir_node *blk = get_nodes_block(n);
3076 ir_graph *irg = get_irn_irg(n);
3078 get_irn_dbg_info(n),
3082 get_irn_dbg_info(n),
3085 new_r_Const(irg, get_mode_one(mode)),
3088 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3092 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3093 ir_node *x = get_Sub_left(a);
3094 ir_node *y = get_Sub_right(a);
3095 ir_node *blk = get_nodes_block(n);
3096 ir_mode *m_b = get_irn_mode(b);
3097 ir_mode *m_y = get_irn_mode(y);
3101 /* Determine the right mode for the Add. */
3104 else if (mode_is_reference(m_b))
3106 else if (mode_is_reference(m_y))
3110 * Both modes are different but none is reference,
3111 * happens for instance in SubP(SubP(P, Iu), Is).
3112 * We have two possibilities here: Cast or ignore.
3113 * Currently we ignore this case.
3118 add = new_r_Add(blk, y, b, add_mode);
3120 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3121 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3125 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3126 /* c - ~X = X + (c+1) */
3127 if (is_Const(a) && is_Not(b)) {
3128 ir_tarval *tv = get_Const_tarval(a);
3130 tv = tarval_add(tv, get_mode_one(mode));
3131 if (tv != tarval_bad) {
3132 ir_node *blk = get_nodes_block(n);
3133 ir_graph *irg = get_irn_irg(n);
3134 ir_node *c = new_r_Const(irg, tv);
3135 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3140 /* x-(x&y) = x & ~y */
3142 ir_node *and_left = get_And_left(b);
3143 ir_node *and_right = get_And_right(b);
3144 if (and_right == a) {
3145 ir_node *tmp = and_left;
3146 and_left = and_right;
3149 if (and_left == a) {
3150 dbg_info *dbgi = get_irn_dbg_info(n);
3151 ir_node *block = get_nodes_block(n);
3152 ir_mode *mode = get_irn_mode(n);
3153 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3154 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3163 * Several transformation done on n*n=2n bits mul.
3164 * These transformations must be done here because new nodes may be produced.
3166 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3169 ir_node *a = get_Mul_left(n);
3170 ir_node *b = get_Mul_right(n);
3171 ir_tarval *ta = value_of(a);
3172 ir_tarval *tb = value_of(b);
3173 ir_mode *smode = get_irn_mode(a);
3175 if (ta == get_mode_one(smode)) {
3176 /* (L)1 * (L)b = (L)b */
3177 ir_node *blk = get_nodes_block(n);
3178 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3182 else if (ta == get_mode_minus_one(smode)) {
3183 /* (L)-1 * (L)b = (L)b */
3184 ir_node *blk = get_nodes_block(n);
3185 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3186 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3190 if (tb == get_mode_one(smode)) {
3191 /* (L)a * (L)1 = (L)a */
3192 ir_node *blk = get_irn_n(a, -1);
3193 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3197 else if (tb == get_mode_minus_one(smode)) {
3198 /* (L)a * (L)-1 = (L)-a */
3199 ir_node *blk = get_nodes_block(n);
3200 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3201 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3202 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3209 * Transform Mul(a,-1) into -a.
3210 * Do constant evaluation of Phi nodes.
3211 * Do architecture dependent optimizations on Mul nodes
3213 static ir_node *transform_node_Mul(ir_node *n)
3215 ir_node *c, *oldn = n;
3216 ir_mode *mode = get_irn_mode(n);
3217 ir_node *a = get_Mul_left(n);
3218 ir_node *b = get_Mul_right(n);
3220 n = fold_constant_associativity(n, tarval_mul);
3224 if (mode != get_irn_mode(a))
3225 return transform_node_Mul2n(n, mode);
3227 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3229 if (mode_is_signed(mode)) {
3232 if (value_of(a) == get_mode_minus_one(mode))
3234 else if (value_of(b) == get_mode_minus_one(mode))
3237 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3238 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3243 if (is_Const(b)) { /* (-a) * const -> a * -const */
3244 ir_node *cnst = const_negate(b);
3246 dbg_info *dbgi = get_irn_dbg_info(n);
3247 ir_node *block = get_nodes_block(n);
3248 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3249 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3252 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3253 dbg_info *dbgi = get_irn_dbg_info(n);
3254 ir_node *block = get_nodes_block(n);
3255 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3256 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3258 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3259 ir_node *sub_l = get_Sub_left(b);
3260 ir_node *sub_r = get_Sub_right(b);
3261 dbg_info *dbgi = get_irn_dbg_info(n);
3262 ir_node *block = get_nodes_block(n);
3263 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3264 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3265 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3268 } else if (is_Minus(b)) {
3269 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3270 ir_node *sub_l = get_Sub_left(a);
3271 ir_node *sub_r = get_Sub_right(a);
3272 dbg_info *dbgi = get_irn_dbg_info(n);
3273 ir_node *block = get_nodes_block(n);
3274 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3275 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3276 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3279 } else if (is_Shl(a)) {
3280 ir_node *const shl_l = get_Shl_left(a);
3281 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3282 /* (1 << x) * b -> b << 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(a);
3286 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3287 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3290 } else if (is_Shl(b)) {
3291 ir_node *const shl_l = get_Shl_left(b);
3292 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3293 /* a * (1 << x) -> a << x */
3294 dbg_info *const dbgi = get_irn_dbg_info(n);
3295 ir_node *const block = get_nodes_block(n);
3296 ir_node *const shl_r = get_Shl_right(b);
3297 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3298 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3302 if (get_mode_arithmetic(mode) == irma_ieee754
3303 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3305 ir_tarval *tv = get_Const_tarval(a);
3306 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3307 && !tarval_is_negative(tv)) {
3308 /* 2.0 * b = b + b */
3309 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3310 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3314 else if (is_Const(b)) {
3315 ir_tarval *tv = get_Const_tarval(b);
3316 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3317 && !tarval_is_negative(tv)) {
3318 /* a * 2.0 = a + a */
3319 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3320 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3325 return arch_dep_replace_mul_with_shifts(n);
3329 * Transform a Div Node.
3331 static ir_node *transform_node_Div(ir_node *n)
3333 ir_mode *mode = get_Div_resmode(n);
3334 ir_node *a = get_Div_left(n);
3335 ir_node *b = get_Div_right(n);
3337 const ir_node *dummy;
3339 if (mode_is_int(mode)) {
3340 if (is_Const(b) && is_const_Phi(a)) {
3341 /* check for Div(Phi, Const) */
3342 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3344 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3347 } else if (is_Const(a) && is_const_Phi(b)) {
3348 /* check for Div(Const, Phi) */
3349 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3351 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3354 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3355 /* check for Div(Phi, Phi) */
3356 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3358 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3363 if (a == b && value_not_zero(a, &dummy)) {
3364 ir_graph *irg = get_irn_irg(n);
3365 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3366 value = new_r_Const(irg, get_mode_one(mode));
3367 DBG_OPT_CSTEVAL(n, value);
3370 if (mode_is_signed(mode) && is_Const(b)) {
3371 ir_tarval *tv = get_Const_tarval(b);
3373 if (tv == get_mode_minus_one(mode)) {
3375 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3376 DBG_OPT_CSTEVAL(n, value);
3380 /* Try architecture dependent optimization */
3381 value = arch_dep_replace_div_by_const(n);
3384 assert(mode_is_float(mode));
3386 /* Optimize x/c to x*(1/c) */
3387 if (get_mode_arithmetic(mode) == irma_ieee754) {
3388 ir_tarval *tv = value_of(b);
3390 if (tv != tarval_bad) {
3391 int rem = tarval_fp_ops_enabled();
3394 * Floating point constant folding might be disabled here to
3396 * However, as we check for exact result, doing it is safe.
3399 tarval_enable_fp_ops(1);
3400 tv = tarval_div(get_mode_one(mode), tv);
3401 tarval_enable_fp_ops(rem);
3403 /* Do the transformation if the result is either exact or we are
3404 not using strict rules. */
3405 if (tv != tarval_bad &&
3406 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3407 ir_node *block = get_nodes_block(n);
3408 ir_graph *irg = get_irn_irg(block);
3409 ir_node *c = new_r_Const(irg, tv);
3410 dbg_info *dbgi = get_irn_dbg_info(n);
3411 value = new_rd_Mul(dbgi, block, a, c, mode);
3424 /* Turn Div into a tuple (mem, jmp, bad, value) */
3425 mem = get_Div_mem(n);
3426 blk = get_nodes_block(n);
3427 irg = get_irn_irg(blk);
3429 /* skip a potential Pin */
3430 mem = skip_Pin(mem);
3431 turn_into_tuple(n, pn_Div_max+1);
3432 set_Tuple_pred(n, pn_Div_M, mem);
3433 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3434 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3435 set_Tuple_pred(n, pn_Div_res, value);
3441 * Transform a Mod node.
3443 static ir_node *transform_node_Mod(ir_node *n)
3445 ir_mode *mode = get_Mod_resmode(n);
3446 ir_node *a = get_Mod_left(n);
3447 ir_node *b = get_Mod_right(n);
3452 if (is_Const(b) && is_const_Phi(a)) {
3453 /* check for Div(Phi, Const) */
3454 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3456 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3460 else if (is_Const(a) && is_const_Phi(b)) {
3461 /* check for Div(Const, Phi) */
3462 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3464 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3468 else if (is_const_Phi(a) && is_const_Phi(b)) {
3469 /* check for Div(Phi, Phi) */
3470 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3472 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3479 irg = get_irn_irg(n);
3480 if (tv != tarval_bad) {
3481 value = new_r_Const(irg, tv);
3483 DBG_OPT_CSTEVAL(n, value);
3486 ir_node *a = get_Mod_left(n);
3487 ir_node *b = get_Mod_right(n);
3488 const ir_node *dummy;
3490 if (a == b && value_not_zero(a, &dummy)) {
3491 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3492 value = new_r_Const(irg, get_mode_null(mode));
3493 DBG_OPT_CSTEVAL(n, value);
3496 if (mode_is_signed(mode) && is_Const(b)) {
3497 ir_tarval *tv = get_Const_tarval(b);
3499 if (tv == get_mode_minus_one(mode)) {
3501 value = new_r_Const(irg, get_mode_null(mode));
3502 DBG_OPT_CSTEVAL(n, value);
3506 /* Try architecture dependent optimization */
3507 value = arch_dep_replace_mod_by_const(n);
3516 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3517 mem = get_Mod_mem(n);
3518 blk = get_nodes_block(n);
3519 irg = get_irn_irg(blk);
3521 /* skip a potential Pin */
3522 mem = skip_Pin(mem);
3523 turn_into_tuple(n, pn_Mod_max+1);
3524 set_Tuple_pred(n, pn_Mod_M, mem);
3525 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3526 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3527 set_Tuple_pred(n, pn_Mod_res, value);
3533 * Transform a Cond node.
3535 * Replace the Cond by a Jmp if it branches on a constant
3538 static ir_node *transform_node_Cond(ir_node *n)
3540 ir_node *a = get_Cond_selector(n);
3541 ir_graph *irg = get_irn_irg(n);
3545 /* we need block info which is not available in floating irgs */
3546 if (get_irg_pinned(irg) == op_pin_state_floats)
3550 if (ta == tarval_bad && is_Cmp(a)) {
3551 /* try again with a direct call to compute_cmp, as we don't care
3552 * about the MODEB_LOWERED flag here */
3553 ta = compute_cmp_ext(a);
3556 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3557 /* It's a boolean Cond, branching on a boolean constant.
3558 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3559 ir_node *blk = get_nodes_block(n);
3560 jmp = new_r_Jmp(blk);
3561 turn_into_tuple(n, pn_Cond_max+1);
3562 if (ta == tarval_b_true) {
3563 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3564 set_Tuple_pred(n, pn_Cond_true, jmp);
3566 set_Tuple_pred(n, pn_Cond_false, jmp);
3567 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3569 /* We might generate an endless loop, so keep it alive. */
3570 add_End_keepalive(get_irg_end(irg), blk);
3571 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3576 static ir_node *transform_node_Switch(ir_node *n)
3578 ir_node *op = get_Switch_selector(n);
3579 ir_tarval *val = value_of(op);
3580 if (val != tarval_bad) {
3581 dbg_info *dbgi = get_irn_dbg_info(n);
3582 ir_graph *irg = get_irn_irg(n);
3583 unsigned n_outs = get_Switch_n_outs(n);
3584 ir_node *block = get_nodes_block(n);
3585 ir_node *bad = new_r_Bad(irg, mode_X);
3586 ir_node **in = XMALLOCN(ir_node*, n_outs);
3587 const ir_switch_table *table = get_Switch_table(n);
3588 size_t n_entries = ir_switch_table_get_n_entries(table);
3592 for (i = 0; i < n_entries; ++i) {
3593 const ir_switch_table_entry *entry
3594 = ir_switch_table_get_entry_const(table, i);
3595 ir_tarval *min = entry->min;
3596 ir_tarval *max = entry->max;
3599 if ((min == max && min == val)
3600 || (tarval_cmp(val, min) != ir_relation_less
3601 && tarval_cmp(val, max) != ir_relation_greater)) {
3606 for (o = 0; o < n_outs; ++o) {
3607 if (o == (unsigned)jmp_pn) {
3608 in[o] = new_rd_Jmp(dbgi, block);
3613 return new_r_Tuple(block, (int)n_outs, in);
3619 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3621 * - and, or, xor instead of &
3622 * - Shl, Shr, Shrs, rotl instead of >>
3623 * (with a special case for Or/Xor + Shrs)
3625 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3627 static ir_node *transform_node_shift_bitop(ir_node *n)
3629 ir_graph *irg = get_irn_irg(n);
3630 ir_node *right = get_binop_right(n);
3631 ir_mode *mode = get_irn_mode(n);
3633 ir_node *bitop_left;
3634 ir_node *bitop_right;
3643 ir_tarval *tv_shift;
3645 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3648 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3650 if (!is_Const(right))
3653 left = get_binop_left(n);
3654 op_left = get_irn_op(left);
3655 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3658 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3659 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3660 /* TODO: test if sign bit is affectes */
3664 bitop_right = get_binop_right(left);
3665 if (!is_Const(bitop_right))
3668 bitop_left = get_binop_left(left);
3670 block = get_nodes_block(n);
3671 dbgi = get_irn_dbg_info(n);
3672 tv1 = get_Const_tarval(bitop_right);
3673 tv2 = get_Const_tarval(right);
3675 assert(get_tarval_mode(tv1) == mode);
3678 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3679 tv_shift = tarval_shl(tv1, tv2);
3680 } else if (is_Shr(n)) {
3681 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3682 tv_shift = tarval_shr(tv1, tv2);
3683 } else if (is_Shrs(n)) {
3684 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3685 tv_shift = tarval_shrs(tv1, tv2);
3688 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3689 tv_shift = tarval_rotl(tv1, tv2);
3692 assert(get_tarval_mode(tv_shift) == mode);
3693 irg = get_irn_irg(n);
3694 new_const = new_r_Const(irg, tv_shift);
3696 if (op_left == op_And) {
3697 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3698 } else if (op_left == op_Or) {
3699 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3701 assert(op_left == op_Eor);
3702 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3711 static ir_node *transform_node_And(ir_node *n)
3713 ir_node *c, *oldn = n;
3714 ir_node *a = get_And_left(n);
3715 ir_node *b = get_And_right(n);
3718 n = fold_constant_associativity(n, tarval_and);
3722 if (is_Cmp(a) && is_Cmp(b)) {
3723 ir_node *a_left = get_Cmp_left(a);
3724 ir_node *a_right = get_Cmp_right(a);
3725 ir_node *b_left = get_Cmp_left(b);
3726 ir_node *b_right = get_Cmp_right(b);
3727 ir_relation a_relation = get_Cmp_relation(a);
3728 ir_relation b_relation = get_Cmp_relation(b);
3729 /* we can combine the relations of two compares with the same
3731 if (a_left == b_left && b_left == b_right) {
3732 dbg_info *dbgi = get_irn_dbg_info(n);
3733 ir_node *block = get_nodes_block(n);
3734 ir_relation new_relation = a_relation & b_relation;
3735 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3737 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3738 if (a_relation == b_relation && a_relation == ir_relation_equal
3739 && !mode_is_float(get_irn_mode(a_left))
3740 && !mode_is_float(get_irn_mode(b_left))) {
3741 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3742 dbg_info *dbgi = get_irn_dbg_info(n);
3743 ir_node *block = get_nodes_block(n);
3744 ir_mode *a_mode = get_irn_mode(a_left);
3745 ir_mode *b_mode = get_irn_mode(b_left);
3746 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3747 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3748 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3749 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3750 ir_graph *irg = get_irn_irg(n);
3751 ir_node *zero = create_zero_const(irg, b_mode);
3752 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3754 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3755 dbg_info *dbgi = get_irn_dbg_info(n);
3756 ir_node *block = get_nodes_block(n);
3757 ir_mode *a_mode = get_irn_mode(a_left);
3758 ir_mode *b_mode = get_irn_mode(b_left);
3759 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3760 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3761 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3762 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3763 ir_graph *irg = get_irn_irg(n);
3764 ir_node *zero = create_zero_const(irg, a_mode);
3765 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3770 mode = get_irn_mode(n);
3771 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3773 if (is_Or(a) || is_Or_Eor_Add(a)) {
3774 ir_node *or_left = get_binop_left(a);
3775 ir_node *or_right = get_binop_right(a);
3776 if (complement_values(or_left, b)) {
3777 /* (a|b) & ~a => b & ~a */
3778 dbg_info *dbgi = get_irn_dbg_info(n);
3779 ir_node *block = get_nodes_block(n);
3780 return new_rd_And(dbgi, block, or_right, b, mode);
3781 } else if (complement_values(or_right, b)) {
3782 /* (a|b) & ~b => a & ~b */
3783 dbg_info *dbgi = get_irn_dbg_info(n);
3784 ir_node *block = get_nodes_block(n);
3785 return new_rd_And(dbgi, block, or_left, b, mode);
3786 } else if (is_Not(b)) {
3787 ir_node *op = get_Not_op(b);
3789 ir_node *ba = get_And_left(op);
3790 ir_node *bb = get_And_right(op);
3792 /* it's enough to test the following cases due to normalization! */
3793 if (or_left == ba && or_right == bb) {
3794 /* (a|b) & ~(a&b) = a^b */
3795 ir_node *block = get_nodes_block(n);
3797 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3798 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3804 if (is_Or(b) || is_Or_Eor_Add(b)) {
3805 ir_node *or_left = get_binop_left(b);
3806 ir_node *or_right = get_binop_right(b);
3807 if (complement_values(or_left, a)) {
3808 /* (a|b) & ~a => b & ~a */
3809 dbg_info *dbgi = get_irn_dbg_info(n);
3810 ir_node *block = get_nodes_block(n);
3811 return new_rd_And(dbgi, block, or_right, a, mode);
3812 } else if (complement_values(or_right, a)) {
3813 /* (a|b) & ~b => a & ~b */
3814 dbg_info *dbgi = get_irn_dbg_info(n);
3815 ir_node *block = get_nodes_block(n);
3816 return new_rd_And(dbgi, block, or_left, a, mode);
3817 } else if (is_Not(a)) {
3818 ir_node *op = get_Not_op(a);
3820 ir_node *aa = get_And_left(op);
3821 ir_node *ab = get_And_right(op);
3823 /* it's enough to test the following cases due to normalization! */
3824 if (or_left == aa && or_right == ab) {
3825 /* (a|b) & ~(a&b) = a^b */
3826 ir_node *block = get_nodes_block(n);
3828 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3829 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3835 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3836 ir_node *al = get_binop_left(a);
3837 ir_node *ar = get_binop_right(a);
3840 /* (b ^ a) & b -> ~a & b */
3841 dbg_info *dbg = get_irn_dbg_info(n);
3842 ir_node *block = get_nodes_block(n);
3844 ar = new_rd_Not(dbg, block, ar, mode);
3845 n = new_rd_And(dbg, block, ar, b, mode);
3846 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3850 /* (a ^ b) & b -> ~a & b */
3851 dbg_info *dbg = get_irn_dbg_info(n);
3852 ir_node *block = get_nodes_block(n);
3854 al = new_rd_Not(dbg, block, al, mode);
3855 n = new_rd_And(dbg, block, al, b, mode);
3856 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3860 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3861 ir_node *bl = get_binop_left(b);
3862 ir_node *br = get_binop_right(b);
3865 /* a & (a ^ b) -> a & ~b */
3866 dbg_info *dbg = get_irn_dbg_info(n);
3867 ir_node *block = get_nodes_block(n);
3869 br = new_rd_Not(dbg, block, br, mode);
3870 n = new_rd_And(dbg, block, br, a, mode);
3871 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3875 /* a & (b ^ a) -> a & ~b */
3876 dbg_info *dbg = get_irn_dbg_info(n);
3877 ir_node *block = get_nodes_block(n);
3879 bl = new_rd_Not(dbg, block, bl, mode);
3880 n = new_rd_And(dbg, block, bl, a, mode);
3881 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3885 if (is_Not(a) && is_Not(b)) {
3886 /* ~a & ~b = ~(a|b) */
3887 ir_node *block = get_nodes_block(n);
3888 ir_mode *mode = get_irn_mode(n);
3892 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3893 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3894 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3899 vrp_attr *b_vrp = vrp_get_info(b);
3900 ir_tarval *a_val = get_Const_tarval(a);
3901 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3907 vrp_attr *a_vrp = vrp_get_info(a);
3908 ir_tarval *b_val = get_Const_tarval(b);
3909 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3914 n = transform_bitwise_distributive(n, transform_node_And);
3916 n = transform_node_bitop_shift(n);
3924 static ir_node *transform_node_Not(ir_node *n)
3926 ir_node *c, *oldn = n;
3927 ir_node *a = get_Not_op(n);
3928 ir_mode *mode = get_irn_mode(n);
3930 HANDLE_UNOP_PHI(tarval_not,a,c);
3932 /* check for a boolean Not */
3934 dbg_info *dbgi = get_irn_dbg_info(a);
3935 ir_node *block = get_nodes_block(a);
3936 ir_relation relation = get_Cmp_relation(a);
3937 relation = get_negated_relation(relation);
3938 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3943 /* normalize ~(a ^ b) => a ^ ~b */
3944 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3945 dbg_info *dbg = get_irn_dbg_info(n);
3946 ir_node *block = get_nodes_block(n);
3947 ir_node *eor_right = get_binop_right(a);
3948 ir_node *eor_left = get_binop_left(a);
3949 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3950 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3954 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3955 if (is_Minus(a)) { /* ~-x -> x + -1 */
3956 dbg_info *dbg = get_irn_dbg_info(n);
3957 ir_graph *irg = get_irn_irg(n);
3958 ir_node *block = get_nodes_block(n);
3959 ir_node *add_l = get_Minus_op(a);
3960 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3961 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3962 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3963 ir_node *add_r = get_binop_right(a);
3964 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3965 /* ~(x + -1) = -x */
3966 ir_node *op = get_binop_left(a);
3967 ir_node *blk = get_nodes_block(n);
3968 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3969 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3977 * Transform a Minus.
3981 * -(a >>u (size-1)) = a >>s (size-1)
3982 * -(a >>s (size-1)) = a >>u (size-1)
3983 * -(a * const) -> a * -const
3985 static ir_node *transform_node_Minus(ir_node *n)
3987 ir_node *c, *oldn = n;
3988 ir_node *a = get_Minus_op(n);
3991 HANDLE_UNOP_PHI(tarval_neg,a,c);
3993 mode = get_irn_mode(a);
3994 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3995 /* the following rules are only to twos-complement */
3998 ir_node *op = get_Not_op(a);
3999 ir_tarval *tv = get_mode_one(mode);
4000 ir_node *blk = get_nodes_block(n);
4001 ir_graph *irg = get_irn_irg(blk);
4002 ir_node *c = new_r_Const(irg, tv);
4003 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
4004 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4008 ir_node *c = get_Shr_right(a);
4011 ir_tarval *tv = get_Const_tarval(c);
4013 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4014 /* -(a >>u (size-1)) = a >>s (size-1) */
4015 ir_node *v = get_Shr_left(a);
4017 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4018 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4024 ir_node *c = get_Shrs_right(a);
4027 ir_tarval *tv = get_Const_tarval(c);
4029 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4030 /* -(a >>s (size-1)) = a >>u (size-1) */
4031 ir_node *v = get_Shrs_left(a);
4033 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4034 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4041 /* - (a-b) = b - a */
4042 ir_node *la = get_Sub_left(a);
4043 ir_node *ra = get_Sub_right(a);
4044 ir_node *blk = get_nodes_block(n);
4046 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4047 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4051 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4052 ir_node *mul_l = get_Mul_left(a);
4053 ir_node *mul_r = get_Mul_right(a);
4054 ir_tarval *tv = value_of(mul_r);
4055 if (tv != tarval_bad) {
4056 tv = tarval_neg(tv);
4057 if (tv != tarval_bad) {
4058 ir_graph *irg = get_irn_irg(n);
4059 ir_node *cnst = new_r_Const(irg, tv);
4060 dbg_info *dbg = get_irn_dbg_info(a);
4061 ir_node *block = get_nodes_block(a);
4062 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4063 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4073 * Transform a Proj(Load) with a non-null address.
4075 static ir_node *transform_node_Proj_Load(ir_node *proj)
4077 if (get_irn_mode(proj) == mode_X) {
4078 ir_node *load = get_Proj_pred(proj);
4080 /* get the Load address */
4081 const ir_node *addr = get_Load_ptr(load);
4082 const ir_node *confirm;
4084 if (value_not_null(addr, &confirm)) {
4085 if (confirm == NULL) {
4086 /* this node may float if it did not depend on a Confirm */
4087 set_irn_pinned(load, op_pin_state_floats);
4089 if (get_Proj_proj(proj) == pn_Load_X_except) {
4090 ir_graph *irg = get_irn_irg(proj);
4091 DBG_OPT_EXC_REM(proj);
4092 return new_r_Bad(irg, mode_X);
4094 ir_node *blk = get_nodes_block(load);
4095 return new_r_Jmp(blk);
4103 * Transform a Proj(Store) with a non-null address.
4105 static ir_node *transform_node_Proj_Store(ir_node *proj)
4107 if (get_irn_mode(proj) == mode_X) {
4108 ir_node *store = get_Proj_pred(proj);
4110 /* get the load/store address */
4111 const ir_node *addr = get_Store_ptr(store);
4112 const ir_node *confirm;
4114 if (value_not_null(addr, &confirm)) {
4115 if (confirm == NULL) {
4116 /* this node may float if it did not depend on a Confirm */
4117 set_irn_pinned(store, op_pin_state_floats);
4119 if (get_Proj_proj(proj) == pn_Store_X_except) {
4120 ir_graph *irg = get_irn_irg(proj);
4121 DBG_OPT_EXC_REM(proj);
4122 return new_r_Bad(irg, mode_X);
4124 ir_node *blk = get_nodes_block(store);
4125 return new_r_Jmp(blk);
4133 * Transform a Proj(Div) with a non-zero value.
4134 * Removes the exceptions and routes the memory to the NoMem node.
4136 static ir_node *transform_node_Proj_Div(ir_node *proj)
4138 ir_node *div = get_Proj_pred(proj);
4139 ir_node *b = get_Div_right(div);
4140 ir_node *res, *new_mem;
4141 const ir_node *confirm;
4144 if (value_not_zero(b, &confirm)) {
4145 /* div(x, y) && y != 0 */
4146 if (confirm == NULL) {
4147 /* we are sure we have a Const != 0 */
4148 new_mem = get_Div_mem(div);
4149 new_mem = skip_Pin(new_mem);
4150 set_Div_mem(div, new_mem);
4151 set_irn_pinned(div, op_pin_state_floats);
4154 proj_nr = get_Proj_proj(proj);
4156 case pn_Div_X_regular:
4157 return new_r_Jmp(get_nodes_block(div));
4159 case pn_Div_X_except: {
4160 ir_graph *irg = get_irn_irg(proj);
4161 /* we found an exception handler, remove it */
4162 DBG_OPT_EXC_REM(proj);
4163 return new_r_Bad(irg, mode_X);
4167 ir_graph *irg = get_irn_irg(proj);
4168 res = get_Div_mem(div);
4169 new_mem = get_irg_no_mem(irg);
4172 /* This node can only float up to the Confirm block */
4173 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4175 set_irn_pinned(div, op_pin_state_floats);
4176 /* this is a Div without exception, we can remove the memory edge */
4177 set_Div_mem(div, new_mem);
4186 * Transform a Proj(Mod) with a non-zero value.
4187 * Removes the exceptions and routes the memory to the NoMem node.
4189 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4191 ir_node *mod = get_Proj_pred(proj);
4192 ir_node *b = get_Mod_right(mod);
4193 ir_node *res, *new_mem;
4194 const ir_node *confirm;
4197 if (value_not_zero(b, &confirm)) {
4198 /* mod(x, y) && y != 0 */
4199 proj_nr = get_Proj_proj(proj);
4201 if (confirm == NULL) {
4202 /* we are sure we have a Const != 0 */
4203 new_mem = get_Mod_mem(mod);
4204 new_mem = skip_Pin(new_mem);
4205 set_Mod_mem(mod, new_mem);
4206 set_irn_pinned(mod, op_pin_state_floats);
4211 case pn_Mod_X_regular:
4212 return new_r_Jmp(get_irn_n(mod, -1));
4214 case pn_Mod_X_except: {
4215 ir_graph *irg = get_irn_irg(proj);
4216 /* we found an exception handler, remove it */
4217 DBG_OPT_EXC_REM(proj);
4218 return new_r_Bad(irg, mode_X);
4222 ir_graph *irg = get_irn_irg(proj);
4223 res = get_Mod_mem(mod);
4224 new_mem = get_irg_no_mem(irg);
4227 /* This node can only float up to the Confirm block */
4228 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4230 /* this is a Mod without exception, we can remove the memory edge */
4231 set_Mod_mem(mod, new_mem);
4235 if (get_Mod_left(mod) == b) {
4236 /* a % a = 0 if a != 0 */
4237 ir_graph *irg = get_irn_irg(proj);
4238 ir_mode *mode = get_irn_mode(proj);
4239 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4241 DBG_OPT_CSTEVAL(mod, res);
4250 * return true if the operation returns a value with exactly 1 bit set
4252 static bool is_single_bit(const ir_node *node)
4254 /* a first implementation, could be extended with vrp and others... */
4256 ir_node *shl_l = get_Shl_left(node);
4257 ir_mode *mode = get_irn_mode(node);
4258 int modulo = get_mode_modulo_shift(mode);
4259 /* this works if we shift a 1 and we have modulo shift */
4260 if (is_Const(shl_l) && is_Const_one(shl_l)
4261 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4264 } else if (is_Const(node)) {
4265 ir_tarval *tv = get_Const_tarval(node);
4266 return tarval_is_single_bit(tv);
4272 * checks if node just flips a bit in another node and returns that other node
4273 * if so. @p tv should be a value having just 1 bit set
4275 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4278 return get_Not_op(node);
4280 ir_node *right = get_Eor_right(node);
4281 if (is_Const(right)) {
4282 ir_tarval *right_tv = get_Const_tarval(right);
4283 ir_mode *mode = get_irn_mode(node);
4284 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4285 return get_Eor_left(node);
4292 * Normalizes and optimizes Cmp nodes.
4294 static ir_node *transform_node_Cmp(ir_node *n)
4296 ir_node *left = get_Cmp_left(n);
4297 ir_node *right = get_Cmp_right(n);
4298 ir_mode *mode = get_irn_mode(left);
4299 ir_tarval *tv = NULL;
4300 bool changed = false;
4301 bool changedc = false;
4302 ir_relation relation = get_Cmp_relation(n);
4303 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4305 /* mask out impossible relations */
4306 ir_relation new_relation = relation & possible;
4307 if (new_relation != relation) {
4308 relation = new_relation;
4312 /* Remove unnecessary conversions */
4313 if (is_Conv(left) && is_Conv(right)) {
4314 ir_node *op_left = get_Conv_op(left);
4315 ir_node *op_right = get_Conv_op(right);
4316 ir_mode *mode_left = get_irn_mode(op_left);
4317 ir_mode *mode_right = get_irn_mode(op_right);
4319 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4320 && mode_left != mode_b && mode_right != mode_b) {
4321 ir_node *block = get_nodes_block(n);
4323 if (mode_left == mode_right) {
4327 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4328 } else if (smaller_mode(mode_left, mode_right)) {
4329 left = new_r_Conv(block, op_left, mode_right);
4332 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4333 } else if (smaller_mode(mode_right, mode_left)) {
4335 right = new_r_Conv(block, op_right, mode_left);
4337 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4339 mode = get_irn_mode(left);
4342 if (is_Conv(left) && is_Const(right)) {
4343 ir_node *op_left = get_Conv_op(left);
4344 ir_mode *mode_left = get_irn_mode(op_left);
4345 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4346 ir_tarval *tv = get_Const_tarval(right);
4347 tarval_int_overflow_mode_t last_mode
4348 = tarval_get_integer_overflow_mode();
4350 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4351 new_tv = tarval_convert_to(tv, mode_left);
4352 tarval_set_integer_overflow_mode(last_mode);
4353 if (new_tv != tarval_bad) {
4354 ir_graph *irg = get_irn_irg(n);
4356 right = new_r_Const(irg, new_tv);
4357 mode = get_irn_mode(left);
4359 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4365 * Optimize -a CMP -b into b CMP a.
4366 * This works only for modes where unary Minus cannot Overflow.
4367 * Note that two-complement integers can Overflow so it will NOT work.
4369 if (!mode_overflow_on_unary_Minus(mode) &&
4370 is_Minus(left) && is_Minus(right)) {
4371 left = get_Minus_op(left);
4372 right = get_Minus_op(right);
4373 relation = get_inversed_relation(relation);
4375 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4378 /* remove operation on both sides if possible */
4379 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4381 * The following operations are NOT safe for floating point operations, for instance
4382 * 1.0 + inf == 2.0 + inf, =/=> x == y
4384 if (mode_is_int(mode)) {
4385 unsigned lop = get_irn_opcode(left);
4387 if (lop == get_irn_opcode(right)) {
4388 ir_node *ll, *lr, *rl, *rr;
4390 /* same operation on both sides, try to remove */
4394 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4395 left = get_unop_op(left);
4396 right = get_unop_op(right);
4398 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4401 ll = get_Add_left(left);
4402 lr = get_Add_right(left);
4403 rl = get_Add_left(right);
4404 rr = get_Add_right(right);
4407 /* X + a CMP X + b ==> a CMP b */
4411 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4412 } else if (ll == rr) {
4413 /* X + a CMP b + X ==> a CMP b */
4417 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4418 } else if (lr == rl) {
4419 /* a + X CMP X + b ==> a CMP b */
4423 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4424 } else if (lr == rr) {
4425 /* a + X CMP b + X ==> a CMP b */
4429 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4433 ll = get_Sub_left(left);
4434 lr = get_Sub_right(left);
4435 rl = get_Sub_left(right);
4436 rr = get_Sub_right(right);
4439 /* X - a CMP X - b ==> a CMP b */
4443 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4444 } else if (lr == rr) {
4445 /* a - X CMP b - X ==> a CMP b */
4449 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4453 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4454 /* a ROTL X CMP b ROTL X ==> a CMP b */
4455 left = get_Rotl_left(left);
4456 right = get_Rotl_left(right);
4458 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4466 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4467 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4468 ir_node *ll = get_binop_left(left);
4469 ir_node *lr = get_binop_right(left);
4471 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4477 ir_graph *irg = get_irn_irg(n);
4479 right = create_zero_const(irg, mode);
4481 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4484 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4485 ir_node *rl = get_binop_left(right);
4486 ir_node *rr = get_binop_right(right);
4488 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4494 ir_graph *irg = get_irn_irg(n);
4496 right = create_zero_const(irg, mode);
4498 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4502 if (is_And(left) && is_Const(right)) {
4503 ir_node *ll = get_binop_left(left);
4504 ir_node *lr = get_binop_right(left);
4505 if (is_Shr(ll) && is_Const(lr)) {
4506 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4507 ir_node *block = get_nodes_block(n);
4508 ir_mode *mode = get_irn_mode(left);
4510 ir_node *llr = get_Shr_right(ll);
4511 if (is_Const(llr)) {
4512 dbg_info *dbg = get_irn_dbg_info(left);
4513 ir_graph *irg = get_irn_irg(left);
4515 ir_tarval *c1 = get_Const_tarval(llr);
4516 ir_tarval *c2 = get_Const_tarval(lr);
4517 ir_tarval *c3 = get_Const_tarval(right);
4518 ir_tarval *mask = tarval_shl(c2, c1);
4519 ir_tarval *value = tarval_shl(c3, c1);
4521 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4522 right = new_r_Const(irg, value);
4527 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4529 if (is_Const(right) && is_Const_null(right) &&
4530 (is_Eor(left) || is_Or_Eor_Add(left))) {
4531 right = get_Eor_right(left);
4532 left = get_Eor_left(left);
4538 if (mode_is_int(mode) && is_And(left)) {
4539 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4540 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4541 if (relation == ir_relation_equal
4542 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4543 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4544 ir_node *and0 = get_And_left(left);
4545 ir_node *and1 = get_And_right(left);
4546 if (and1 == right) {
4547 ir_node *tmp = and0;
4551 if (and0 == right && is_single_bit(and0)) {
4552 ir_graph *irg = get_irn_irg(n);
4554 relation == ir_relation_equal ? ir_relation_less_greater
4555 : ir_relation_equal;
4556 right = create_zero_const(irg, mode);
4562 if (is_Const(right) && is_Const_null(right) &&
4563 (relation == ir_relation_equal
4564 || (relation == ir_relation_less_greater)
4565 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4567 /* instead of flipping the bit before the bit-test operation negate
4569 ir_node *and0 = get_And_left(left);
4570 ir_node *and1 = get_And_right(left);
4571 if (is_Const(and1)) {
4572 ir_tarval *tv = get_Const_tarval(and1);
4573 if (tarval_is_single_bit(tv)) {
4574 ir_node *flipped = flips_bit(and0, tv);
4575 if (flipped != NULL) {
4576 dbg_info *dbgi = get_irn_dbg_info(left);
4577 ir_node *block = get_nodes_block(left);
4578 relation = get_negated_relation(relation);
4579 left = new_rd_And(dbgi, block, flipped, and1, mode);
4588 /* replace mode_b compares with ands/ors */
4589 if (mode == mode_b) {
4590 ir_node *block = get_nodes_block(n);
4594 case ir_relation_less_equal:
4595 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4597 case ir_relation_less:
4598 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4600 case ir_relation_greater_equal:
4601 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4603 case ir_relation_greater:
4604 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4606 case ir_relation_less_greater:
4607 bres = new_r_Eor(block, left, right, mode_b);
4609 case ir_relation_equal:
4610 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4613 #ifdef DEBUG_libfirm
4614 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4619 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4625 * First step: normalize the compare op
4626 * by placing the constant on the right side
4627 * or moving the lower address node to the left.
4629 if (!operands_are_normalized(left, right)) {
4634 relation = get_inversed_relation(relation);
4639 * Second step: Try to reduce the magnitude
4640 * of a constant. This may help to generate better code
4641 * later and may help to normalize more compares.
4642 * Of course this is only possible for integer values.
4644 tv = value_of(right);
4645 if (tv != tarval_bad) {
4646 ir_mode *mode = get_irn_mode(right);
4648 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4649 * cmp(ct,c2) | cmp(cf,c2) | result
4650 * -----------|------------|--------
4651 * true | true | True
4652 * false | false | False
4654 * false | true | not(x)
4657 ir_node *mux_true = get_Mux_true(left);
4658 ir_node *mux_false = get_Mux_false(left);
4659 if (is_Const(mux_true) && is_Const(mux_false)) {
4660 /* we can fold true/false constant separately */
4661 ir_tarval *tv_true = get_Const_tarval(mux_true);
4662 ir_tarval *tv_false = get_Const_tarval(mux_false);
4663 ir_relation r_true = tarval_cmp(tv_true, tv);
4664 ir_relation r_false = tarval_cmp(tv_false, tv);
4665 if (r_true != ir_relation_false
4666 || r_false != ir_relation_false) {
4667 bool rel_true = (r_true & relation) != 0;
4668 bool rel_false = (r_false & relation) != 0;
4669 ir_node *cond = get_Mux_sel(left);
4670 if (rel_true == rel_false) {
4671 relation = rel_true ? ir_relation_true
4672 : ir_relation_false;
4673 } else if (rel_true) {
4676 dbg_info *dbgi = get_irn_dbg_info(n);
4677 ir_node *block = get_nodes_block(n);
4678 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4685 /* TODO extend to arbitrary constants */
4686 if (is_Conv(left) && tarval_is_null(tv)) {
4687 ir_node *op = get_Conv_op(left);
4688 ir_mode *op_mode = get_irn_mode(op);
4691 * UpConv(x) REL 0 ==> x REL 0
4692 * Don't do this for float values as it's unclear whether it is a
4693 * win. (on the other side it makes detection/creation of fabs hard)
4695 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4696 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4697 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4698 !mode_is_float(mode)) {
4699 tv = get_mode_null(op_mode);
4703 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4707 if (tv != tarval_bad) {
4708 /* the following optimization is possible on modes without Overflow
4709 * on Unary Minus or on == and !=:
4710 * -a CMP c ==> a swap(CMP) -c
4712 * Beware: for two-complement Overflow may occur, so only == and != can
4713 * be optimized, see this:
4714 * -MININT < 0 =/=> MININT > 0 !!!
4716 if (is_Minus(left) &&
4717 (!mode_overflow_on_unary_Minus(mode) ||
4718 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4719 tv = tarval_neg(tv);
4721 if (tv != tarval_bad) {
4722 left = get_Minus_op(left);
4723 relation = get_inversed_relation(relation);
4725 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4727 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4728 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4729 tv = tarval_not(tv);
4731 if (tv != tarval_bad) {
4732 left = get_Not_op(left);
4734 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4738 /* for integer modes, we have more */
4739 if (mode_is_int(mode) && !is_Const(left)) {
4740 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4741 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4742 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4743 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4745 if (tv != tarval_bad) {
4746 relation ^= ir_relation_equal;
4748 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4751 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4752 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4753 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4754 tv = tarval_add(tv, get_mode_one(mode));
4756 if (tv != tarval_bad) {
4757 relation ^= ir_relation_equal;
4759 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4763 /* the following reassociations work only for == and != */
4764 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4765 if (tv != tarval_bad) {
4766 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4768 ir_node *c1 = get_Sub_right(left);
4769 ir_tarval *tv2 = value_of(c1);
4771 if (tv2 != tarval_bad) {
4772 tv2 = tarval_add(tv, value_of(c1));
4774 if (tv2 != tarval_bad) {
4775 left = get_Sub_left(left);
4778 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4782 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4783 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4784 ir_node *a_l = get_binop_left(left);
4785 ir_node *a_r = get_binop_right(left);
4789 if (is_Const(a_l)) {
4791 tv2 = value_of(a_l);
4794 tv2 = value_of(a_r);
4797 if (tv2 != tarval_bad) {
4798 tv2 = tarval_sub(tv, tv2, NULL);
4800 if (tv2 != tarval_bad) {
4804 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4808 /* -a == c ==> a == -c, -a != c ==> a != -c */
4809 else if (is_Minus(left)) {
4810 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4812 if (tv2 != tarval_bad) {
4813 left = get_Minus_op(left);
4816 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4823 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4824 switch (get_irn_opcode(left)) {
4828 c1 = get_And_right(left);
4831 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4832 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4834 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4836 /* TODO: move to constant evaluation */
4837 ir_graph *irg = get_irn_irg(n);
4838 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4839 c1 = new_r_Const(irg, tv);
4840 DBG_OPT_CSTEVAL(n, c1);
4844 if (tarval_is_single_bit(tv)) {
4846 * optimization for AND:
4848 * And(x, C) == C ==> And(x, C) != 0
4849 * And(x, C) != C ==> And(X, C) == 0
4851 * if C is a single Bit constant.
4854 /* check for Constant's match. We have check hare the tarvals,
4855 because our const might be changed */
4856 if (get_Const_tarval(c1) == tv) {
4857 /* fine: do the transformation */
4858 tv = get_mode_null(get_tarval_mode(tv));
4859 relation ^= ir_relation_less_equal_greater;
4861 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4867 c1 = get_Or_right(left);
4868 if (is_Const(c1) && tarval_is_null(tv)) {
4870 * Or(x, C) == 0 && C != 0 ==> FALSE
4871 * Or(x, C) != 0 && C != 0 ==> TRUE
4873 if (! tarval_is_null(get_Const_tarval(c1))) {
4874 /* TODO: move to constant evaluation */
4875 ir_graph *irg = get_irn_irg(n);
4876 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4877 c1 = new_r_Const(irg, tv);
4878 DBG_OPT_CSTEVAL(n, c1);
4885 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4887 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4890 c1 = get_Shl_right(left);
4892 ir_graph *irg = get_irn_irg(c1);
4893 ir_tarval *tv1 = get_Const_tarval(c1);
4894 ir_mode *mode = get_irn_mode(left);
4895 ir_tarval *minus1 = get_mode_all_one(mode);
4896 ir_tarval *amask = tarval_shr(minus1, tv1);
4897 ir_tarval *cmask = tarval_shl(minus1, tv1);
4900 if (tarval_and(tv, cmask) != tv) {
4901 /* condition not met */
4902 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4903 c1 = new_r_Const(irg, tv);
4904 DBG_OPT_CSTEVAL(n, c1);
4907 sl = get_Shl_left(left);
4908 blk = get_nodes_block(n);
4909 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4910 tv = tarval_shr(tv, tv1);
4912 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4917 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4919 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4922 c1 = get_Shr_right(left);
4924 ir_graph *irg = get_irn_irg(c1);
4925 ir_tarval *tv1 = get_Const_tarval(c1);
4926 ir_mode *mode = get_irn_mode(left);
4927 ir_tarval *minus1 = get_mode_all_one(mode);
4928 ir_tarval *amask = tarval_shl(minus1, tv1);
4929 ir_tarval *cmask = tarval_shr(minus1, tv1);
4932 if (tarval_and(tv, cmask) != tv) {
4933 /* condition not met */
4934 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4935 c1 = new_r_Const(irg, tv);
4936 DBG_OPT_CSTEVAL(n, c1);
4939 sl = get_Shr_left(left);
4940 blk = get_nodes_block(n);
4941 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4942 tv = tarval_shl(tv, tv1);
4944 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4949 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4951 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4954 c1 = get_Shrs_right(left);
4956 ir_graph *irg = get_irn_irg(c1);
4957 ir_tarval *tv1 = get_Const_tarval(c1);
4958 ir_mode *mode = get_irn_mode(left);
4959 ir_tarval *minus1 = get_mode_all_one(mode);
4960 ir_tarval *amask = tarval_shl(minus1, tv1);
4961 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4964 cond = tarval_sub(cond, tv1, NULL);
4965 cond = tarval_shrs(tv, cond);
4967 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4968 /* condition not met */
4969 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4970 c1 = new_r_Const(irg, tv);
4971 DBG_OPT_CSTEVAL(n, c1);
4974 sl = get_Shrs_left(left);
4975 blk = get_nodes_block(n);
4976 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4977 tv = tarval_shl(tv, tv1);
4979 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4987 if (changedc) { /* need a new Const */
4988 ir_graph *irg = get_irn_irg(n);
4989 right = new_r_Const(irg, tv);
4993 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4994 ir_node *op = get_Proj_pred(left);
4996 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4997 ir_node *c = get_binop_right(op);
5000 ir_tarval *tv = get_Const_tarval(c);
5002 if (tarval_is_single_bit(tv)) {
5003 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5004 ir_node *v = get_binop_left(op);
5005 ir_node *blk = get_irn_n(op, -1);
5006 ir_graph *irg = get_irn_irg(op);
5007 ir_mode *mode = get_irn_mode(v);
5009 tv = tarval_sub(tv, get_mode_one(mode), NULL);
5010 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5012 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5019 dbg_info *dbgi = get_irn_dbg_info(n);
5020 ir_node *block = get_nodes_block(n);
5022 /* create a new compare */
5023 n = new_rd_Cmp(dbgi, block, left, right, relation);
5030 * Optimize CopyB(mem, x, x) into a Nop.
5032 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5034 ir_node *copyb = get_Proj_pred(proj);
5035 ir_node *a = get_CopyB_dst(copyb);
5036 ir_node *b = get_CopyB_src(copyb);
5039 switch (get_Proj_proj(proj)) {
5040 case pn_CopyB_X_regular:
5041 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5042 DBG_OPT_EXC_REM(proj);
5043 proj = new_r_Jmp(get_nodes_block(copyb));
5045 case pn_CopyB_X_except: {
5046 ir_graph *irg = get_irn_irg(proj);
5047 DBG_OPT_EXC_REM(proj);
5048 proj = new_r_Bad(irg, mode_X);
5059 * Optimize Bounds(idx, idx, upper) into idx.
5061 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5063 ir_node *oldn = proj;
5064 ir_node *bound = get_Proj_pred(proj);
5065 ir_node *idx = get_Bound_index(bound);
5066 ir_node *pred = skip_Proj(idx);
5069 if (idx == get_Bound_lower(bound))
5071 else if (is_Bound(pred)) {
5073 * idx was Bounds checked previously, it is still valid if
5074 * lower <= pred_lower && pred_upper <= upper.
5076 ir_node *lower = get_Bound_lower(bound);
5077 ir_node *upper = get_Bound_upper(bound);
5078 if (get_Bound_lower(pred) == lower &&
5079 get_Bound_upper(pred) == upper) {
5081 * One could expect that we simply return the previous
5082 * Bound here. However, this would be wrong, as we could
5083 * add an exception Proj to a new location then.
5084 * So, we must turn in into a tuple.
5090 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5091 switch (get_Proj_proj(proj)) {
5093 DBG_OPT_EXC_REM(proj);
5094 proj = get_Bound_mem(bound);
5096 case pn_Bound_X_except:
5097 DBG_OPT_EXC_REM(proj);
5098 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5102 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5104 case pn_Bound_X_regular:
5105 DBG_OPT_EXC_REM(proj);
5106 proj = new_r_Jmp(get_nodes_block(bound));
5116 * Does all optimizations on nodes that must be done on its Projs
5117 * because of creating new nodes.
5119 static ir_node *transform_node_Proj(ir_node *proj)
5121 ir_node *n = get_Proj_pred(proj);
5123 if (n->op->ops.transform_node_Proj)
5124 return n->op->ops.transform_node_Proj(proj);
5129 * Test whether a block is unreachable
5130 * Note: That this only returns true when
5131 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5132 * This is important, as you easily end up producing invalid constructs in the
5133 * unreachable code when optimizing away edges into the unreachable code.
5134 * So only set this flag when you iterate localopts to the fixpoint.
5135 * When you reach the fixpoint then all unreachable code is dead
5136 * (= can't be reached by firm edges) and you won't see the invalid constructs
5139 static bool is_block_unreachable(const ir_node *block)
5141 const ir_graph *irg = get_irn_irg(block);
5142 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5144 return get_Block_dom_depth(block) < 0;
5147 static ir_node *transform_node_Block(ir_node *block)
5149 ir_graph *irg = get_irn_irg(block);
5150 int arity = get_irn_arity(block);
5151 ir_node *bad = NULL;
5154 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5157 for (i = 0; i < arity; ++i) {
5158 ir_node *const pred = get_Block_cfgpred(block, i);
5159 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5162 bad = new_r_Bad(irg, mode_X);
5163 set_irn_n(block, i, bad);
5169 static ir_node *transform_node_Phi(ir_node *phi)
5171 int n = get_irn_arity(phi);
5172 ir_mode *mode = get_irn_mode(phi);
5173 ir_node *block = get_nodes_block(phi);
5174 ir_graph *irg = get_irn_irg(phi);
5175 ir_node *bad = NULL;
5178 /* Set phi-operands for bad-block inputs to bad */
5179 for (i = 0; i < n; ++i) {
5180 if (!is_Bad(get_Phi_pred(phi, i))) {
5181 ir_node *pred = get_Block_cfgpred(block, i);
5182 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5184 bad = new_r_Bad(irg, mode);
5185 set_irn_n(phi, i, bad);
5190 /* Move Pin nodes down through Phi nodes. */
5191 if (mode == mode_M) {
5192 n = get_irn_arity(phi);
5194 /* Beware of Phi0 */
5198 bool has_pin = false;
5200 NEW_ARR_A(ir_node *, in, n);
5202 for (i = 0; i < n; ++i) {
5203 ir_node *pred = get_irn_n(phi, i);
5206 in[i] = get_Pin_op(pred);
5208 } else if (is_Bad(pred)) {
5218 /* Move the Pin nodes "behind" the Phi. */
5219 block = get_irn_n(phi, -1);
5220 new_phi = new_r_Phi(block, n, in, mode_M);
5221 return new_r_Pin(block, new_phi);
5224 /* Move Confirms down through Phi nodes. */
5225 else if (mode_is_reference(mode)) {
5226 n = get_irn_arity(phi);
5228 /* Beware of Phi0 */
5230 ir_node *pred = get_irn_n(phi, 0);
5231 ir_node *bound, *new_phi, *block, **in;
5232 ir_relation relation;
5233 bool has_confirm = false;
5235 if (! is_Confirm(pred))
5238 bound = get_Confirm_bound(pred);
5239 relation = get_Confirm_relation(pred);
5241 NEW_ARR_A(ir_node *, in, n);
5242 in[0] = get_Confirm_value(pred);
5244 for (i = 1; i < n; ++i) {
5245 pred = get_irn_n(phi, i);
5247 if (is_Confirm(pred) &&
5248 get_Confirm_bound(pred) == bound &&
5249 get_Confirm_relation(pred) == relation) {
5250 in[i] = get_Confirm_value(pred);
5252 } else if (is_Bad(pred)) {
5262 /* move the Confirm nodes "behind" the Phi */
5263 block = get_irn_n(phi, -1);
5264 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5265 return new_r_Confirm(block, new_phi, bound, relation);
5272 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5274 * Should be moved to reassociation?
5276 static ir_node *transform_node_shift(ir_node *n)
5278 ir_node *left, *right;
5280 ir_mode *count_mode;
5281 ir_tarval *tv1, *tv2, *res;
5282 ir_node *in[2], *irn, *block;
5286 left = get_binop_left(n);
5288 /* different operations */
5289 if (get_irn_op(left) != get_irn_op(n))
5292 right = get_binop_right(n);
5293 tv1 = value_of(right);
5294 if (tv1 == tarval_bad)
5297 tv2 = value_of(get_binop_right(left));
5298 if (tv2 == tarval_bad)
5301 count_mode = get_tarval_mode(tv1);
5302 if (get_tarval_mode(tv2) != count_mode) {
5303 /* TODO: search bigger mode or something and convert... */
5307 mode = get_irn_mode(n);
5308 modulo_shf = get_mode_modulo_shift(mode);
5310 if (modulo_shf > 0) {
5311 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5313 /* I'm not so sure what happens in one complement... */
5314 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5315 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5316 * above will be invalid) */
5317 assert(modulo_shf<=0 || is_po2(modulo_shf));
5319 tv1 = tarval_and(tv1, modulo_mask);
5320 tv2 = tarval_and(tv2, modulo_mask);
5322 res = tarval_add(tv1, tv2);
5323 irg = get_irn_irg(n);
5325 /* beware: a simple replacement works only, if res < modulo shift */
5327 int bits = get_mode_size_bits(mode);
5328 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5329 res = tarval_mod(res, modulo);
5331 long bits = get_mode_size_bits(mode);
5332 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5334 /* shifting too much */
5335 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5337 ir_node *block = get_nodes_block(n);
5338 dbg_info *dbgi = get_irn_dbg_info(n);
5339 ir_mode *smode = get_irn_mode(right);
5340 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5341 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5344 return new_r_Const(irg, get_mode_null(mode));
5348 /* ok, we can replace it */
5349 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5350 block = get_nodes_block(n);
5352 in[0] = get_binop_left(left);
5353 in[1] = new_r_Const(irg, res);
5355 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5357 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5364 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5366 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5367 * (also with x >>s c1 when c1>=c2)
5369 static ir_node *transform_node_shl_shr(ir_node *n)
5372 ir_node *right = get_binop_right(n);
5382 ir_tarval *tv_shift;
5385 ir_relation relation;
5388 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5390 if (!is_Const(right))
5393 left = get_binop_left(n);
5394 mode = get_irn_mode(n);
5395 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5396 ir_node *shr_right = get_binop_right(left);
5398 if (!is_Const(shr_right))
5401 x = get_binop_left(left);
5402 tv_shr = get_Const_tarval(shr_right);
5403 tv_shl = get_Const_tarval(right);
5405 if (is_Shrs(left)) {
5406 /* shrs variant only allowed if c1 >= c2 */
5407 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5410 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5413 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5415 tv_mask = tarval_shl(tv_mask, tv_shl);
5416 } else if (is_Shr(n) && is_Shl(left)) {
5417 ir_node *shl_right = get_Shl_right(left);
5419 if (!is_Const(shl_right))
5422 x = get_Shl_left(left);
5423 tv_shr = get_Const_tarval(right);
5424 tv_shl = get_Const_tarval(shl_right);
5426 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5427 tv_mask = tarval_shr(tv_mask, tv_shr);
5432 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5433 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5436 assert(tv_mask != tarval_bad);
5437 assert(get_tarval_mode(tv_mask) == mode);
5439 block = get_nodes_block(n);
5440 irg = get_irn_irg(block);
5441 dbgi = get_irn_dbg_info(n);
5443 relation = tarval_cmp(tv_shl, tv_shr);
5444 if (relation == ir_relation_less || relation == ir_relation_equal) {
5445 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5446 new_const = new_r_Const(irg, tv_shift);
5448 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5450 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5453 assert(relation == ir_relation_greater);
5454 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5455 new_const = new_r_Const(irg, tv_shift);
5456 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5459 new_const = new_r_Const(irg, tv_mask);
5460 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5465 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5467 ir_mode *mode = get_tarval_mode(tv);
5468 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5469 return tarval_mod(tv, modulo_tv);
5472 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5473 ir_node *left, ir_node *right, ir_mode *mode);
5476 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5477 * then we can use that to minimize the value of Add(x, const) or
5478 * Sub(Const, x). In particular this often avoids 1 instruction in some
5479 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5480 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5482 static ir_node *transform_node_shift_modulo(ir_node *n,
5483 new_shift_func new_shift)
5485 ir_mode *mode = get_irn_mode(n);
5486 int modulo = get_mode_modulo_shift(mode);
5487 ir_node *newop = NULL;
5488 ir_mode *mode_right;
5495 if (get_mode_arithmetic(mode) != irma_twos_complement)
5497 if (!is_po2(modulo))
5500 irg = get_irn_irg(n);
5501 block = get_nodes_block(n);
5502 right = get_binop_right(n);
5503 mode_right = get_irn_mode(right);
5504 if (is_Const(right)) {
5505 ir_tarval *tv = get_Const_tarval(right);
5506 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5511 newop = new_r_Const(irg, tv_mod);
5512 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5513 ir_node *add_right = get_binop_right(right);
5514 if (is_Const(add_right)) {
5515 ir_tarval *tv = get_Const_tarval(add_right);
5516 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5521 newconst = new_r_Const(irg, tv_mod);
5522 newop = new_r_Add(block, get_binop_left(right), newconst,
5525 } else if (is_Sub(right)) {
5526 ir_node *sub_left = get_Sub_left(right);
5527 if (is_Const(sub_left)) {
5528 ir_tarval *tv = get_Const_tarval(sub_left);
5529 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5534 newconst = new_r_Const(irg, tv_mod);
5535 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5542 if (newop != NULL) {
5543 dbg_info *dbgi = get_irn_dbg_info(n);
5544 ir_node *left = get_binop_left(n);
5545 return new_shift(dbgi, block, left, newop, mode);
5553 static ir_node *transform_node_Shr(ir_node *n)
5555 ir_node *c, *oldn = n;
5556 ir_node *left = get_Shr_left(n);
5557 ir_node *right = get_Shr_right(n);
5558 ir_mode *mode = get_irn_mode(n);
5560 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5561 n = transform_node_shift(n);
5564 n = transform_node_shift_modulo(n, new_rd_Shr);
5566 n = transform_node_shl_shr(n);
5568 n = transform_node_shift_bitop(n);
5576 static ir_node *transform_node_Shrs(ir_node *n)
5579 ir_node *a = get_Shrs_left(n);
5580 ir_node *b = get_Shrs_right(n);
5581 ir_mode *mode = get_irn_mode(n);
5585 if (is_oversize_shift(n)) {
5586 ir_node *block = get_nodes_block(n);
5587 dbg_info *dbgi = get_irn_dbg_info(n);
5588 ir_mode *cmode = get_irn_mode(b);
5589 long val = get_mode_size_bits(cmode)-1;
5590 ir_graph *irg = get_irn_irg(n);
5591 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5592 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5595 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5596 n = transform_node_shift(n);
5600 n = transform_node_shift_modulo(n, new_rd_Shrs);
5603 n = transform_node_shift_bitop(n);
5607 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5608 attr = vrp_get_info(a);
5610 unsigned bits = get_mode_size_bits(mode);
5611 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5612 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5613 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5614 dbg_info *dbgi = get_irn_dbg_info(n);
5615 ir_node *block = get_nodes_block(n);
5616 return new_rd_Shr(dbgi, block, a, b, mode);
5626 static ir_node *transform_node_Shl(ir_node *n)
5628 ir_node *c, *oldn = n;
5629 ir_node *a = get_Shl_left(n);
5630 ir_node *b = get_Shl_right(n);
5631 ir_mode *mode = get_irn_mode(n);
5633 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5634 n = transform_node_shift(n);
5637 n = transform_node_shift_modulo(n, new_rd_Shl);
5639 n = transform_node_shl_shr(n);
5641 n = transform_node_shift_bitop(n);
5649 static ir_node *transform_node_Rotl(ir_node *n)
5651 ir_node *c, *oldn = n;
5652 ir_node *a = get_Rotl_left(n);
5653 ir_node *b = get_Rotl_right(n);
5654 ir_mode *mode = get_irn_mode(n);
5656 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5657 n = transform_node_shift(n);
5660 n = transform_node_shift_bitop(n);
5668 static ir_node *transform_node_Conv(ir_node *n)
5670 ir_node *c, *oldn = n;
5671 ir_mode *mode = get_irn_mode(n);
5672 ir_node *a = get_Conv_op(n);
5674 if (mode != mode_b && is_const_Phi(a)) {
5675 /* Do NOT optimize mode_b Conv's, this leads to remaining
5676 * Phib nodes later, because the conv_b_lower operation
5677 * is instantly reverted, when it tries to insert a Convb.
5679 c = apply_conv_on_phi(a, mode);
5681 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5686 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5687 ir_graph *irg = get_irn_irg(n);
5688 return new_r_Unknown(irg, mode);
5691 if (mode_is_reference(mode) &&
5692 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5694 ir_node *l = get_Add_left(a);
5695 ir_node *r = get_Add_right(a);
5696 dbg_info *dbgi = get_irn_dbg_info(a);
5697 ir_node *block = get_nodes_block(n);
5699 ir_node *lop = get_Conv_op(l);
5700 if (get_irn_mode(lop) == mode) {
5701 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5702 n = new_rd_Add(dbgi, block, lop, r, mode);
5707 ir_node *rop = get_Conv_op(r);
5708 if (get_irn_mode(rop) == mode) {
5709 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5710 n = new_rd_Add(dbgi, block, l, rop, mode);
5720 * Remove dead blocks and nodes in dead blocks
5721 * in keep alive list. We do not generate a new End node.
5723 static ir_node *transform_node_End(ir_node *n)
5725 int i, j, n_keepalives = get_End_n_keepalives(n);
5728 NEW_ARR_A(ir_node *, in, n_keepalives);
5730 for (i = j = 0; i < n_keepalives; ++i) {
5731 ir_node *ka = get_End_keepalive(n, i);
5733 /* no need to keep Bad */
5736 /* do not keep unreachable code */
5737 block = is_Block(ka) ? ka : get_nodes_block(ka);
5738 if (is_block_unreachable(block))
5742 if (j != n_keepalives)
5743 set_End_keepalives(n, j, in);
5747 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5749 if (is_Minus(a) && get_Minus_op(a) == b)
5751 if (is_Minus(b) && get_Minus_op(b) == a)
5753 if (is_Sub(a) && is_Sub(b)) {
5754 ir_node *a_left = get_Sub_left(a);
5755 ir_node *a_right = get_Sub_right(a);
5756 ir_node *b_left = get_Sub_left(b);
5757 ir_node *b_right = get_Sub_right(b);
5759 if (a_left == b_right && a_right == b_left)
5766 static const ir_node *skip_upconv(const ir_node *node)
5768 while (is_Conv(node)) {
5769 ir_mode *mode = get_irn_mode(node);
5770 const ir_node *op = get_Conv_op(node);
5771 ir_mode *op_mode = get_irn_mode(op);
5772 if (!smaller_mode(op_mode, mode))
5779 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5780 const ir_node *mux_true)
5785 ir_relation relation;
5791 * Note further that these optimization work even for floating point
5792 * with NaN's because -NaN == NaN.
5793 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5796 mode = get_irn_mode(mux_true);
5797 if (mode_honor_signed_zeros(mode))
5800 /* must be <, <=, >=, > */
5801 relation = get_Cmp_relation(sel);
5802 if ((relation & ir_relation_less_greater) == 0)
5805 if (!ir_is_negated_value(mux_true, mux_false))
5808 mux_true = skip_upconv(mux_true);
5809 mux_false = skip_upconv(mux_false);
5811 /* must be x cmp 0 */
5812 cmp_right = get_Cmp_right(sel);
5813 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5816 cmp_left = get_Cmp_left(sel);
5817 if (cmp_left == mux_false) {
5818 if (relation & ir_relation_less) {
5821 assert(relation & ir_relation_greater);
5824 } else if (cmp_left == mux_true) {
5825 if (relation & ir_relation_less) {
5828 assert(relation & ir_relation_greater);
5836 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5839 ir_node *cmp_left = get_Cmp_left(sel);
5840 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5843 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5844 const ir_node *mux_true)
5846 /* this code should return true each time transform_node_Mux would
5847 * optimize the Mux completely away */
5849 ir_mode *mode = get_irn_mode(mux_false);
5850 if (get_mode_arithmetic(mode) == irma_twos_complement
5851 && ir_mux_is_abs(sel, mux_false, mux_true))
5854 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5855 const ir_node *cmp_r = get_Cmp_right(sel);
5856 const ir_node *cmp_l = get_Cmp_left(sel);
5857 const ir_node *f = mux_false;
5858 const ir_node *t = mux_true;
5860 if (is_Const(t) && is_Const_null(t)) {
5865 if (is_And(cmp_l) && f == cmp_r) {
5866 ir_node *and_r = get_And_right(cmp_l);
5869 if (and_r == t && is_single_bit(and_r))
5871 and_l = get_And_left(cmp_l);
5872 if (and_l == t && is_single_bit(and_l))
5881 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5883 static ir_node *transform_Mux_set(ir_node *n)
5885 ir_node *cond = get_Mux_sel(n);
5890 ir_relation relation;
5904 left = get_Cmp_left(cond);
5905 mode = get_irn_mode(left);
5906 if (!mode_is_int(mode) && !mode_is_reference(mode))
5908 dest_mode = get_irn_mode(n);
5909 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5911 right = get_Cmp_right(cond);
5912 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5913 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5914 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5915 && relation != ir_relation_greater))
5920 case ir_relation_less:
5921 /* a < b -> (a - b) >> 31 */
5925 case ir_relation_less_equal:
5926 /* a <= b -> ~(a - b) >> 31 */
5931 case ir_relation_greater:
5932 /* a > b -> (b - a) >> 31 */
5936 case ir_relation_greater_equal:
5937 /* a >= b -> ~(a - b) >> 31 */
5946 dbgi = get_irn_dbg_info(n);
5947 block = get_nodes_block(n);
5948 irg = get_irn_irg(block);
5949 bits = get_mode_size_bits(dest_mode);
5950 tv = new_tarval_from_long(bits-1, mode_Iu);
5951 shift_cnt = new_rd_Const(dbgi, irg, tv);
5953 if (mode != dest_mode) {
5954 a = new_rd_Conv(dbgi, block, a, dest_mode);
5955 b = new_rd_Conv(dbgi, block, b, dest_mode);
5958 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5960 res = new_rd_Not(dbgi, block, res, dest_mode);
5962 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5967 * Optimize a Mux into some simpler cases.
5969 static ir_node *transform_node_Mux(ir_node *n)
5972 ir_node *sel = get_Mux_sel(n);
5973 ir_mode *mode = get_irn_mode(n);
5974 ir_node *t = get_Mux_true(n);
5975 ir_node *f = get_Mux_false(n);
5976 ir_graph *irg = get_irn_irg(n);
5978 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5979 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5980 int abs = ir_mux_is_abs(sel, f, t);
5982 dbg_info *dbgi = get_irn_dbg_info(n);
5983 ir_node *block = get_nodes_block(n);
5984 ir_node *op = ir_get_abs_op(sel, f, t);
5985 int bits = get_mode_size_bits(mode);
5986 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5987 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5988 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5991 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5993 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5999 /* first normalization step: try to move a constant to the false side,
6000 * 0 preferred on false side too */
6001 if (is_Cmp(sel) && is_Const(t) &&
6002 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
6003 dbg_info *seldbgi = get_irn_dbg_info(sel);
6004 ir_node *block = get_nodes_block(sel);
6005 ir_relation relation = get_Cmp_relation(sel);
6010 /* Mux(x, a, b) => Mux(not(x), b, a) */
6011 relation = get_negated_relation(relation);
6012 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6013 get_Cmp_right(sel), relation);
6014 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6017 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6018 n = transform_Mux_set(n);
6023 /* the following optimisations create new mode_b nodes, so only do them
6024 * before mode_b lowering */
6025 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6027 ir_node* block = get_nodes_block(n);
6029 ir_node* c1 = get_Mux_sel(t);
6030 ir_node* t1 = get_Mux_true(t);
6031 ir_node* f1 = get_Mux_false(t);
6033 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6034 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6035 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6036 return new_r_Mux(block, and_, f1, t1, mode);
6037 } else if (f == t1) {
6038 /* Mux(cond0, Mux(cond1, x, y), x) */
6039 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6040 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6041 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6042 return new_r_Mux(block, and_, t1, f1, mode);
6044 } else if (is_Mux(f)) {
6045 ir_node* block = get_nodes_block(n);
6047 ir_node* c1 = get_Mux_sel(f);
6048 ir_node* t1 = get_Mux_true(f);
6049 ir_node* f1 = get_Mux_false(f);
6051 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6052 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6053 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6054 return new_r_Mux(block, or_, f1, t1, mode);
6055 } else if (t == f1) {
6056 /* Mux(cond0, x, Mux(cond1, y, x)) */
6057 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6058 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6059 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6060 return new_r_Mux(block, or_, t1, f1, mode);
6064 /* note: after normalization, false can only happen on default */
6065 if (mode == mode_b) {
6066 dbg_info *dbg = get_irn_dbg_info(n);
6067 ir_node *block = get_nodes_block(n);
6070 ir_tarval *tv_t = get_Const_tarval(t);
6071 if (tv_t == tarval_b_true) {
6073 /* Muxb(sel, true, false) = sel */
6074 assert(get_Const_tarval(f) == tarval_b_false);
6075 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6078 /* Muxb(sel, true, x) = Or(sel, x) */
6079 n = new_rd_Or(dbg, block, sel, f, mode_b);
6080 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6084 } else if (is_Const(f)) {
6085 ir_tarval *tv_f = get_Const_tarval(f);
6086 if (tv_f == tarval_b_true) {
6087 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6088 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6089 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6090 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6093 /* Muxb(sel, x, false) = And(sel, x) */
6094 assert(tv_f == tarval_b_false);
6095 n = new_rd_And(dbg, block, sel, t, mode_b);
6096 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6103 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6104 ir_relation relation = get_Cmp_relation(sel);
6105 ir_node *cmp_r = get_Cmp_right(sel);
6106 ir_node *cmp_l = get_Cmp_left(sel);
6107 ir_node *block = get_nodes_block(n);
6109 if (is_And(cmp_l) && f == cmp_r) {
6110 ir_node *and_r = get_And_right(cmp_l);
6113 if (and_r == t && is_single_bit(and_r)) {
6114 if (relation == ir_relation_equal) {
6115 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6116 n = new_rd_Eor(get_irn_dbg_info(n),
6117 block, cmp_l, t, mode);
6118 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6120 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6122 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6126 and_l = get_And_left(cmp_l);
6127 if (and_l == t && is_single_bit(and_l)) {
6128 if (relation == ir_relation_equal) {
6129 /* ((1 << n) & a) == 0, (1 << n), 0) */
6130 n = new_rd_Eor(get_irn_dbg_info(n),
6131 block, cmp_l, t, mode);
6132 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6134 /* ((1 << n) & a) != 0, (1 << n), 0) */
6136 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6147 * optimize Sync nodes that have other syncs as input we simply add the inputs
6148 * of the other sync to our own inputs
6150 static ir_node *transform_node_Sync(ir_node *n)
6152 int arity = get_Sync_n_preds(n);
6155 for (i = 0; i < arity;) {
6156 ir_node *pred = get_Sync_pred(n, i);
6160 /* Remove Bad predecessors */
6167 /* Remove duplicate predecessors */
6168 for (j = 0; j < i; ++j) {
6169 if (get_Sync_pred(n, j) == pred) {
6178 if (!is_Sync(pred)) {
6186 pred_arity = get_Sync_n_preds(pred);
6187 for (j = 0; j < pred_arity; ++j) {
6188 ir_node *pred_pred = get_Sync_pred(pred, j);
6193 add_irn_n(n, pred_pred);
6197 if (get_Sync_pred(n, k) == pred_pred) break;
6203 ir_graph *irg = get_irn_irg(n);
6204 return new_r_Bad(irg, mode_M);
6207 return get_Sync_pred(n, 0);
6210 /* rehash the sync node */
6215 static ir_node *transform_node_Load(ir_node *n)
6217 /* if our memory predecessor is a load from the same address, then reuse the
6218 * previous result */
6219 ir_node *mem = get_Load_mem(n);
6224 /* don't touch volatile loads */
6225 if (get_Load_volatility(n) == volatility_is_volatile)
6227 mem_pred = get_Proj_pred(mem);
6228 if (is_Load(mem_pred)) {
6229 ir_node *pred_load = mem_pred;
6231 /* conservatively compare the 2 loads. TODO: This could be less strict
6232 * with fixup code in some situations (like smaller/bigger modes) */
6233 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6235 if (get_Load_mode(pred_load) != get_Load_mode(n))
6237 /* all combinations of aligned/unaligned pred/n should be fine so we do
6238 * not compare the unaligned attribute */
6240 ir_node *block = get_nodes_block(n);
6241 ir_node *jmp = new_r_Jmp(block);
6242 ir_graph *irg = get_irn_irg(n);
6243 ir_node *bad = new_r_Bad(irg, mode_X);
6244 ir_mode *mode = get_Load_mode(n);
6245 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6246 ir_node *in[] = { mem, res, jmp, bad };
6247 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6250 } else if (is_Store(mem_pred)) {
6251 ir_node *pred_store = mem_pred;
6252 ir_node *value = get_Store_value(pred_store);
6254 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6256 if (get_irn_mode(value) != get_Load_mode(n))
6258 /* all combinations of aligned/unaligned pred/n should be fine so we do
6259 * not compare the unaligned attribute */
6261 ir_node *block = get_nodes_block(n);
6262 ir_node *jmp = new_r_Jmp(block);
6263 ir_graph *irg = get_irn_irg(n);
6264 ir_node *bad = new_r_Bad(irg, mode_X);
6265 ir_node *res = value;
6266 ir_node *in[] = { mem, res, jmp, bad };
6267 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6276 * optimize a trampoline Call into a direct Call
6278 static ir_node *transform_node_Call(ir_node *call)
6280 ir_node *callee = get_Call_ptr(call);
6281 ir_node *adr, *mem, *res, *bl, **in;
6282 ir_type *ctp, *mtp, *tp;
6286 size_t i, n_res, n_param;
6289 if (! is_Proj(callee))
6291 callee = get_Proj_pred(callee);
6292 if (! is_Builtin(callee))
6294 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6297 mem = get_Call_mem(call);
6299 if (skip_Proj(mem) == callee) {
6300 /* memory is routed to the trampoline, skip */
6301 mem = get_Builtin_mem(callee);
6304 /* build a new call type */
6305 mtp = get_Call_type(call);
6306 tdb = get_type_dbg_info(mtp);
6308 n_res = get_method_n_ress(mtp);
6309 n_param = get_method_n_params(mtp);
6310 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6312 for (i = 0; i < n_res; ++i)
6313 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6315 NEW_ARR_A(ir_node *, in, n_param + 1);
6317 /* FIXME: we don't need a new pointer type in every step */
6318 irg = get_irn_irg(call);
6319 tp = get_irg_frame_type(irg);
6320 tp = new_type_pointer(tp);
6321 set_method_param_type(ctp, 0, tp);
6323 in[0] = get_Builtin_param(callee, 2);
6324 for (i = 0; i < n_param; ++i) {
6325 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6326 in[i + 1] = get_Call_param(call, i);
6328 var = get_method_variadicity(mtp);
6329 set_method_variadicity(ctp, var);
6330 /* When we resolve a trampoline, the function must be called by a this-call */
6331 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6332 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6334 adr = get_Builtin_param(callee, 1);
6336 db = get_irn_dbg_info(call);
6337 bl = get_nodes_block(call);
6339 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6340 if (get_irn_pinned(call) == op_pin_state_floats)
6341 set_irn_pinned(res, op_pin_state_floats);
6345 void firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6349 ops->transform_node = transform_node_##a; \
6351 #define CASE_PROJ(a) \
6353 ops->transform_node_Proj = transform_node_Proj_##a; \
6355 #define CASE_PROJ_EX(a) \
6357 ops->transform_node = transform_node_##a; \
6358 ops->transform_node_Proj = transform_node_Proj_##a; \
6400 * Tries several [inplace] [optimizing] transformations and returns an
6401 * equivalent node. The difference to equivalent_node() is that these
6402 * transformations _do_ generate new nodes, and thus the old node must
6403 * not be freed even if the equivalent node isn't the old one.
6405 static ir_node *transform_node(ir_node *n)
6411 iro = get_irn_opcode_(n);
6412 /* constant expression evaluation / constant folding */
6413 if (get_opt_constant_folding()) {
6414 /* neither constants nor Tuple values can be evaluated */
6415 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6416 /* try to evaluate */
6417 ir_tarval *tv = computed_value(n);
6418 if (tv != tarval_bad) {
6419 /* evaluation was successful -- replace the node. */
6420 ir_graph *irg = get_irn_irg(n);
6422 n = new_r_Const(irg, tv);
6424 DBG_OPT_CSTEVAL(old_n, n);
6430 /* remove unnecessary nodes */
6431 if (get_opt_constant_folding() ||
6432 (iro == iro_Phi) || /* always optimize these nodes. */
6433 (iro == iro_Id) || /* ... */
6434 (iro == iro_Proj) || /* ... */
6435 (iro == iro_Block)) { /* Flags tested local. */
6436 n = equivalent_node(n);
6441 /* Some more constant expression evaluation. */
6442 if (get_opt_algebraic_simplification() ||
6443 (iro == iro_Cond) ||
6444 (iro == iro_Proj)) { /* Flags tested local. */
6445 if (n->op->ops.transform_node != NULL) {
6446 n = n->op->ops.transform_node(n);
6456 /* **************** Common Subexpression Elimination **************** */
6458 /** The size of the hash table used, should estimate the number of nodes
6460 #define N_IR_NODES 512
6462 /** Compares two exception attributes */
6463 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6465 const except_attr *ea = &a->attr.except;
6466 const except_attr *eb = &b->attr.except;
6467 return ea->pin_state != eb->pin_state;
6470 /** Compares the attributes of two Const nodes. */
6471 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6473 return get_Const_tarval(a) != get_Const_tarval(b);
6476 /** Compares the attributes of two Proj nodes. */
6477 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6479 return a->attr.proj.proj != b->attr.proj.proj;
6482 /** Compares the attributes of two Alloc nodes. */
6483 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6485 const alloc_attr *pa = &a->attr.alloc;
6486 const alloc_attr *pb = &b->attr.alloc;
6487 if (pa->where != pb->where || pa->type != pb->type)
6489 return node_cmp_exception(a, b);
6492 /** Compares the attributes of two Free nodes. */
6493 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6495 const free_attr *pa = &a->attr.free;
6496 const free_attr *pb = &b->attr.free;
6497 return (pa->where != pb->where) || (pa->type != pb->type);
6500 /** Compares the attributes of two SymConst nodes. */
6501 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6503 const symconst_attr *pa = &a->attr.symc;
6504 const symconst_attr *pb = &b->attr.symc;
6505 return (pa->kind != pb->kind)
6506 || (pa->sym.type_p != pb->sym.type_p);
6509 /** Compares the attributes of two Call nodes. */
6510 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6512 const call_attr *pa = &a->attr.call;
6513 const call_attr *pb = &b->attr.call;
6514 if (pa->type != pb->type)
6516 return node_cmp_exception(a, b);
6519 /** Compares the attributes of two Sel nodes. */
6520 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6522 const ir_entity *a_ent = get_Sel_entity(a);
6523 const ir_entity *b_ent = get_Sel_entity(b);
6524 return a_ent != b_ent;
6527 /** Compares the attributes of two Phi nodes. */
6528 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6531 /* do not CSE Phi-nodes without any inputs when building new graphs */
6532 if (get_irn_arity(a) == 0 &&
6533 get_irg_phase_state(get_irn_irg(a)) == phase_building) {
6539 /** Compares the attributes of two Conv nodes. */
6540 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6542 return get_Conv_strict(a) != get_Conv_strict(b);
6545 /** Compares the attributes of two Cast nodes. */
6546 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6548 return get_Cast_type(a) != get_Cast_type(b);
6551 /** Compares the attributes of two Load nodes. */
6552 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6554 if (get_Load_volatility(a) == volatility_is_volatile ||
6555 get_Load_volatility(b) == volatility_is_volatile)
6556 /* NEVER do CSE on volatile Loads */
6558 /* do not CSE Loads with different alignment. Be conservative. */
6559 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6561 if (get_Load_mode(a) != get_Load_mode(b))
6563 return node_cmp_exception(a, b);
6566 /** Compares the attributes of two Store nodes. */
6567 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6569 /* do not CSE Stores with different alignment. Be conservative. */
6570 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6572 /* NEVER do CSE on volatile Stores */
6573 if (get_Store_volatility(a) == volatility_is_volatile ||
6574 get_Store_volatility(b) == volatility_is_volatile)
6576 return node_cmp_exception(a, b);
6579 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6581 if (get_CopyB_type(a) != get_CopyB_type(b))
6584 return node_cmp_exception(a, b);
6587 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6589 return node_cmp_exception(a, b);
6592 /** Compares the attributes of two Div nodes. */
6593 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6595 const div_attr *ma = &a->attr.div;
6596 const div_attr *mb = &b->attr.div;
6597 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6599 return node_cmp_exception(a, b);
6602 /** Compares the attributes of two Mod nodes. */
6603 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6605 const mod_attr *ma = &a->attr.mod;
6606 const mod_attr *mb = &b->attr.mod;
6607 if (ma->resmode != mb->resmode)
6609 return node_cmp_exception(a, b);
6612 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6614 const cmp_attr *ma = &a->attr.cmp;
6615 const cmp_attr *mb = &b->attr.cmp;
6616 return ma->relation != mb->relation;
6619 /** Compares the attributes of two Confirm nodes. */
6620 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6622 const confirm_attr *ma = &a->attr.confirm;
6623 const confirm_attr *mb = &b->attr.confirm;
6624 return ma->relation != mb->relation;
6627 /** Compares the attributes of two Builtin nodes. */
6628 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6630 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6632 if (get_Builtin_type(a) != get_Builtin_type(b))
6634 return node_cmp_exception(a, b);
6637 /** Compares the attributes of two ASM nodes. */
6638 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6642 const ir_asm_constraint *ca;
6643 const ir_asm_constraint *cb;
6646 if (get_ASM_text(a) != get_ASM_text(b))
6649 /* Should we really check the constraints here? Should be better, but is strange. */
6650 n = get_ASM_n_input_constraints(a);
6651 if (n != get_ASM_n_input_constraints(b))
6654 ca = get_ASM_input_constraints(a);
6655 cb = get_ASM_input_constraints(b);
6656 for (i = 0; i < n; ++i) {
6657 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6658 || ca[i].mode != cb[i].mode)
6662 n = get_ASM_n_output_constraints(a);
6663 if (n != get_ASM_n_output_constraints(b))
6666 ca = get_ASM_output_constraints(a);
6667 cb = get_ASM_output_constraints(b);
6668 for (i = 0; i < n; ++i) {
6669 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6670 || ca[i].mode != cb[i].mode)
6674 n = get_ASM_n_clobbers(a);
6675 if (n != get_ASM_n_clobbers(b))
6678 cla = get_ASM_clobbers(a);
6679 clb = get_ASM_clobbers(b);
6680 for (i = 0; i < n; ++i) {
6681 if (cla[i] != clb[i])
6685 return node_cmp_exception(a, b);
6688 /** Compares the inexistent attributes of two Dummy nodes. */
6689 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6693 /* Dummy nodes never equal by definition */
6697 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6699 if (get_InstOf_type(a) != get_InstOf_type(b))
6701 return node_cmp_exception(a, b);
6704 void firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6708 ops->node_cmp_attr = node_cmp_attr_##a; \
6741 int identities_cmp(const void *elt, const void *key)
6743 ir_node *a = (ir_node *)elt;
6744 ir_node *b = (ir_node *)key;
6747 if (a == b) return 0;
6749 if ((get_irn_op(a) != get_irn_op(b)) ||
6750 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6752 /* compare if a's in and b's in are of equal length */
6753 irn_arity_a = get_irn_arity(a);
6754 if (irn_arity_a != get_irn_arity(b))
6757 /* blocks are never the same */
6761 if (get_irn_pinned(a) == op_pin_state_pinned) {
6762 /* for pinned nodes, the block inputs must be equal */
6763 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6766 ir_node *block_a = get_nodes_block(a);
6767 ir_node *block_b = get_nodes_block(b);
6768 if (! get_opt_global_cse()) {
6769 /* for block-local CSE both nodes must be in the same Block */
6770 if (block_a != block_b)
6773 /* The optimistic approach would be to do nothing here.
6774 * However doing GCSE optimistically produces a lot of partially dead code which appears
6775 * to be worse in practice than the missed opportunities.
6776 * So we use a very conservative variant here and only CSE if 1 value dominates the
6778 if (!block_dominates(block_a, block_b)
6779 && !block_dominates(block_b, block_a))
6784 /* compare a->in[0..ins] with b->in[0..ins] */
6785 for (i = 0; i < irn_arity_a; ++i) {
6786 ir_node *pred_a = get_irn_n(a, i);
6787 ir_node *pred_b = get_irn_n(b, i);
6788 if (pred_a != pred_b) {
6789 /* if both predecessors are CSE neutral they might be different */
6790 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6796 * here, we already now that the nodes are identical except their
6799 if (a->op->ops.node_cmp_attr)
6800 return a->op->ops.node_cmp_attr(a, b);
6805 unsigned ir_node_hash(const ir_node *node)
6807 return node->op->ops.hash(node);
6810 void new_identities(ir_graph *irg)
6812 if (irg->value_table != NULL)
6813 del_pset(irg->value_table);
6814 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6817 void del_identities(ir_graph *irg)
6819 if (irg->value_table != NULL)
6820 del_pset(irg->value_table);
6823 void ir_normalize_node(ir_node *n)
6825 if (is_op_commutative(get_irn_op(n))) {
6826 ir_node *l = get_binop_left(n);
6827 ir_node *r = get_binop_right(n);
6829 /* For commutative operators perform a OP b == b OP a but keep
6830 * constants on the RIGHT side. This helps greatly in some
6831 * optimizations. Moreover we use the idx number to make the form
6833 if (!operands_are_normalized(l, r)) {
6834 set_binop_left(n, r);
6835 set_binop_right(n, l);
6841 ir_node *identify_remember(ir_node *n)
6843 ir_graph *irg = get_irn_irg(n);
6844 pset *value_table = irg->value_table;
6847 if (value_table == NULL)
6850 ir_normalize_node(n);
6851 /* lookup or insert in hash table with given hash key. */
6852 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6855 /* n is reachable again */
6856 edges_node_revival(nn);
6863 * During construction we set the op_pin_state_pinned flag in the graph right
6864 * when the optimization is performed. The flag turning on procedure global
6865 * cse could be changed between two allocations. This way we are safe.
6867 * @param n The node to lookup
6869 static inline ir_node *identify_cons(ir_node *n)
6873 n = identify_remember(n);
6874 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6875 ir_graph *irg = get_irn_irg(n);
6876 set_irg_pinned(irg, op_pin_state_floats);
6881 void add_identities(ir_node *node)
6888 identify_remember(node);
6891 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6894 ir_graph *rem = current_ir_graph;
6896 current_ir_graph = irg;
6897 foreach_pset(irg->value_table, ir_node*, node) {
6900 current_ir_graph = rem;
6903 ir_node *optimize_node(ir_node *n)
6906 ir_graph *irg = get_irn_irg(n);
6907 unsigned iro = get_irn_opcode(n);
6910 /* Always optimize Phi nodes: part of the construction. */
6911 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6913 /* constant expression evaluation / constant folding */
6914 if (get_opt_constant_folding()) {
6915 /* neither constants nor Tuple values can be evaluated */
6916 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6917 /* try to evaluate */
6918 tv = computed_value(n);
6919 if (tv != tarval_bad) {
6924 * we MUST copy the node here temporarily, because it's still
6925 * needed for DBG_OPT_CSTEVAL
6927 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6928 oldn = (ir_node*)alloca(node_size);
6930 memcpy(oldn, n, node_size);
6931 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6933 /* ARG, copy the in array, we need it for statistics */
6934 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6936 /* note the inplace edges module */
6937 edges_node_deleted(n);
6939 /* evaluation was successful -- replace the node. */
6940 irg_kill_node(irg, n);
6941 nw = new_r_Const(irg, tv);
6943 DBG_OPT_CSTEVAL(oldn, nw);
6949 /* remove unnecessary nodes */
6950 if (get_opt_algebraic_simplification() ||
6951 (iro == iro_Phi) || /* always optimize these nodes. */
6953 (iro == iro_Proj) ||
6954 (iro == iro_Block) ) /* Flags tested local. */
6955 n = equivalent_node(n);
6957 /* Common Subexpression Elimination.
6959 * Checks whether n is already available.
6960 * The block input is used to distinguish different subexpressions. Right
6961 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6962 * subexpressions within a block.
6965 n = identify_cons(n);
6968 edges_node_deleted(oldn);
6970 /* We found an existing, better node, so we can deallocate the old node. */
6971 irg_kill_node(irg, oldn);
6975 /* Some more constant expression evaluation that does not allow to
6977 iro = get_irn_opcode(n);
6978 if (get_opt_algebraic_simplification() ||
6979 (iro == iro_Cond) ||
6980 (iro == iro_Proj)) { /* Flags tested local. */
6981 n = transform_node(n);
6984 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6985 if (get_opt_cse()) {
6987 n = identify_remember(o);
6995 ir_node *optimize_in_place_2(ir_node *n)
6997 if (!get_opt_optimize() && !is_Phi(n)) return n;
7002 /** common subexpression elimination **/
7003 /* Checks whether n is already available. */
7004 /* The block input is used to distinguish different subexpressions.
7005 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
7006 * only finds common subexpressions within a block. */
7007 if (get_opt_cse()) {
7009 n = identify_remember(n);
7012 /* we have another existing node now, we do not optimize it here */
7017 n = transform_node(n);
7019 /* Now we can verify the node, as it has no dead inputs any more. */
7022 /* Now we have a legal, useful node. Enter it in hash table for cse.
7024 * Note: This is only necessary because some of the optimisations
7025 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7026 * bad practice and should be fixed sometime.
7028 if (get_opt_cse()) {
7030 n = identify_remember(o);
7038 ir_node *optimize_in_place(ir_node *n)
7040 ir_graph *irg = get_irn_irg(n);
7041 /* Handle graph state */
7042 assert(get_irg_phase_state(irg) != phase_building);
7044 if (get_opt_global_cse())
7045 set_irg_pinned(irg, op_pin_state_floats);
7047 /* FIXME: Maybe we could also test whether optimizing the node can
7048 change the control graph. */
7049 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7050 return optimize_in_place_2(n);
7054 * Calculate a hash value of a Const node.
7056 static unsigned hash_Const(const ir_node *node)
7060 /* special value for const, as they only differ in their tarval. */
7061 h = hash_ptr(node->attr.con.tarval);
7067 * Calculate a hash value of a SymConst node.
7069 static unsigned hash_SymConst(const ir_node *node)
7073 /* all others are pointers */
7074 h = hash_ptr(node->attr.symc.sym.type_p);
7079 void firm_set_default_hash(unsigned code, ir_op_ops *ops)
7083 ops->hash = hash_##a; \
7086 /* hash function already set */
7087 if (ops->hash != NULL)
7094 /* use input/mode default hash if no function was given */
7095 ops->hash = firm_default_hash;