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"
57 static bool is_Or_Eor_Add(const ir_node *node)
59 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
60 ir_node *left = get_binop_left(node);
61 ir_node *right = get_binop_right(node);
62 vrp_attr *vrp_left = vrp_get_info(left);
63 vrp_attr *vrp_right = vrp_get_info(right);
64 if (vrp_left != NULL && vrp_right != NULL) {
66 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
67 return tarval_is_null(vrp_val);
74 * Returns the tarval of a Const node or tarval_bad for all other nodes.
76 static ir_tarval *default_value_of(const ir_node *n)
79 return get_Const_tarval(n); /* might return tarval_bad */
84 value_of_func value_of_ptr = default_value_of;
86 void set_value_of_func(value_of_func func)
91 value_of_ptr = default_value_of;
95 * Return the value of a Constant.
97 static ir_tarval *computed_value_Const(const ir_node *n)
99 return get_Const_tarval(n);
103 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
105 static ir_tarval *computed_value_SymConst(const ir_node *n)
110 switch (get_SymConst_kind(n)) {
111 case symconst_type_size:
112 type = get_SymConst_type(n);
113 if (get_type_state(type) == layout_fixed)
114 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
116 case symconst_type_align:
117 type = get_SymConst_type(n);
118 if (get_type_state(type) == layout_fixed)
119 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
121 case symconst_ofs_ent:
122 ent = get_SymConst_entity(n);
123 type = get_entity_owner(ent);
124 if (get_type_state(type) == layout_fixed)
125 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
134 * Return the value of an Add.
136 static ir_tarval *computed_value_Add(const ir_node *n)
138 ir_node *a = get_Add_left(n);
139 ir_node *b = get_Add_right(n);
141 ir_tarval *ta = value_of(a);
142 ir_tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad))
145 return tarval_add(ta, tb);
148 if ((is_Not(a) && get_Not_op(a) == b)
149 || (is_Not(b) && get_Not_op(b) == a)) {
150 return get_mode_all_one(get_irn_mode(n));
157 * Return the value of a Sub.
158 * Special case: a - a
160 static ir_tarval *computed_value_Sub(const ir_node *n)
162 ir_mode *mode = get_irn_mode(n);
163 ir_node *a = get_Sub_left(n);
164 ir_node *b = get_Sub_right(n);
169 if (! mode_is_float(mode)) {
172 return get_mode_null(mode);
178 if ((ta != tarval_bad) && (tb != tarval_bad))
179 return tarval_sub(ta, tb, mode);
185 * Return the value of a Carry.
186 * Special : a op 0, 0 op b
188 static ir_tarval *computed_value_Carry(const ir_node *n)
190 ir_node *a = get_binop_left(n);
191 ir_node *b = get_binop_right(n);
192 ir_mode *m = get_irn_mode(n);
193 ir_tarval *ta = value_of(a);
194 ir_tarval *tb = value_of(b);
196 if ((ta != tarval_bad) && (tb != tarval_bad)) {
198 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
200 if (tarval_is_null(ta) || tarval_is_null(tb))
201 return get_mode_null(m);
207 * Return the value of a Borrow.
210 static ir_tarval *computed_value_Borrow(const ir_node *n)
212 ir_node *a = get_binop_left(n);
213 ir_node *b = get_binop_right(n);
214 ir_mode *m = get_irn_mode(n);
215 ir_tarval *ta = value_of(a);
216 ir_tarval *tb = value_of(b);
218 if ((ta != tarval_bad) && (tb != tarval_bad)) {
219 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
220 } else if (tarval_is_null(ta)) {
221 return get_mode_null(m);
227 * Return the value of an unary Minus.
229 static ir_tarval *computed_value_Minus(const ir_node *n)
231 ir_node *a = get_Minus_op(n);
232 ir_tarval *ta = value_of(a);
234 if (ta != tarval_bad)
235 return tarval_neg(ta);
241 * Return the value of a Mul.
243 static ir_tarval *computed_value_Mul(const ir_node *n)
245 ir_node *a = get_Mul_left(n);
246 ir_node *b = get_Mul_right(n);
247 ir_tarval *ta = value_of(a);
248 ir_tarval *tb = value_of(b);
251 mode = get_irn_mode(n);
252 if (mode != get_irn_mode(a)) {
253 /* n * n = 2n bit multiplication */
254 ta = tarval_convert_to(ta, mode);
255 tb = tarval_convert_to(tb, mode);
258 if (ta != tarval_bad && tb != tarval_bad) {
259 return tarval_mul(ta, tb);
261 /* a * 0 != 0 if a == NaN or a == Inf */
262 if (!mode_is_float(mode)) {
263 /* a*0 = 0 or 0*b = 0 */
264 if (ta == get_mode_null(mode))
266 if (tb == get_mode_null(mode))
274 * Return the value of an And.
275 * Special case: a & 0, 0 & b
277 static ir_tarval *computed_value_And(const ir_node *n)
279 ir_node *a = get_And_left(n);
280 ir_node *b = get_And_right(n);
281 ir_tarval *ta = value_of(a);
282 ir_tarval *tb = value_of(b);
284 if ((ta != tarval_bad) && (tb != tarval_bad)) {
285 return tarval_and (ta, tb);
288 if (tarval_is_null(ta)) return ta;
289 if (tarval_is_null(tb)) return tb;
292 if ((is_Not(a) && get_Not_op(a) == b)
293 || (is_Not(b) && get_Not_op(b) == a)) {
294 return get_mode_null(get_irn_mode(n));
301 * Return the value of an Or.
302 * Special case: a | 1...1, 1...1 | b
304 static ir_tarval *computed_value_Or(const ir_node *n)
306 ir_node *a = get_Or_left(n);
307 ir_node *b = get_Or_right(n);
308 ir_tarval *ta = value_of(a);
309 ir_tarval *tb = value_of(b);
311 if ((ta != tarval_bad) && (tb != tarval_bad)) {
312 return tarval_or (ta, tb);
315 if (tarval_is_all_one(ta)) return ta;
316 if (tarval_is_all_one(tb)) return tb;
319 if ((is_Not(a) && get_Not_op(a) == b)
320 || (is_Not(b) && get_Not_op(b) == a)) {
321 return get_mode_all_one(get_irn_mode(n));
327 * Return the value of an Eor.
329 static ir_tarval *computed_value_Eor(const ir_node *n)
331 ir_node *a = get_Eor_left(n);
332 ir_node *b = get_Eor_right(n);
337 return get_mode_null(get_irn_mode(n));
339 if ((is_Not(a) && get_Not_op(a) == b)
340 || (is_Not(b) && get_Not_op(b) == a)) {
341 return get_mode_all_one(get_irn_mode(n));
347 if ((ta != tarval_bad) && (tb != tarval_bad)) {
348 return tarval_eor(ta, tb);
354 * Return the value of a Not.
356 static ir_tarval *computed_value_Not(const ir_node *n)
358 ir_node *a = get_Not_op(n);
359 ir_tarval *ta = value_of(a);
361 if (ta != tarval_bad)
362 return tarval_not(ta);
368 * Tests whether a shift shifts more bits than available in the mode
370 static bool is_oversize_shift(const ir_node *n)
372 ir_node *count = get_binop_right(n);
373 ir_mode *mode = get_irn_mode(n);
374 ir_tarval *tv = value_of(count);
377 if (tv == tarval_bad)
379 if (!tarval_is_long(tv))
381 shiftval = get_tarval_long(tv);
382 modulo_shift = get_mode_modulo_shift(mode);
383 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
386 return shiftval >= (long)get_mode_size_bits(mode);
390 * Return the value of a Shl.
392 static ir_tarval *computed_value_Shl(const ir_node *n)
394 ir_node *a = get_Shl_left(n);
395 ir_node *b = get_Shl_right(n);
397 ir_tarval *ta = value_of(a);
398 ir_tarval *tb = value_of(b);
400 if ((ta != tarval_bad) && (tb != tarval_bad)) {
401 return tarval_shl(ta, tb);
404 if (is_oversize_shift(n))
405 return get_mode_null(get_irn_mode(n));
411 * Return the value of a Shr.
413 static ir_tarval *computed_value_Shr(const ir_node *n)
415 ir_node *a = get_Shr_left(n);
416 ir_node *b = get_Shr_right(n);
418 ir_tarval *ta = value_of(a);
419 ir_tarval *tb = value_of(b);
421 if ((ta != tarval_bad) && (tb != tarval_bad)) {
422 return tarval_shr(ta, tb);
424 if (is_oversize_shift(n))
425 return get_mode_null(get_irn_mode(n));
431 * Return the value of a Shrs.
433 static ir_tarval *computed_value_Shrs(const ir_node *n)
435 ir_node *a = get_Shrs_left(n);
436 ir_node *b = get_Shrs_right(n);
438 ir_tarval *ta = value_of(a);
439 ir_tarval *tb = value_of(b);
441 if ((ta != tarval_bad) && (tb != tarval_bad)) {
442 return tarval_shrs(ta, tb);
448 * Return the value of a Rotl.
450 static ir_tarval *computed_value_Rotl(const ir_node *n)
452 ir_node *a = get_Rotl_left(n);
453 ir_node *b = get_Rotl_right(n);
455 ir_tarval *ta = value_of(a);
456 ir_tarval *tb = value_of(b);
458 if ((ta != tarval_bad) && (tb != tarval_bad)) {
459 return tarval_rotl(ta, tb);
464 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
466 ir_mode *mode = get_irn_mode(node);
467 if (get_mode_arithmetic(mode) != irma_twos_complement
468 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
472 ir_node *count = get_Shl_right(node);
473 if (is_Const(count)) {
474 ir_tarval *tv = get_Const_tarval(count);
475 if (tarval_is_long(tv)) {
476 long shiftval = get_tarval_long(tv);
477 long destbits = get_mode_size_bits(dest_mode);
478 if (shiftval >= destbits
479 && shiftval < (long)get_mode_modulo_shift(mode))
485 ir_node *right = get_And_right(node);
486 if (is_Const(right)) {
487 ir_tarval *tv = get_Const_tarval(right);
488 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
489 return tarval_is_null(conved);
496 * Return the value of a Conv.
498 static ir_tarval *computed_value_Conv(const ir_node *n)
500 ir_node *a = get_Conv_op(n);
501 ir_tarval *ta = value_of(a);
502 ir_mode *mode = get_irn_mode(n);
504 if (ta != tarval_bad)
505 return tarval_convert_to(ta, get_irn_mode(n));
507 if (ir_zero_when_converted(a, mode))
508 return get_mode_null(mode);
514 * Calculate the value of a Mux: can be evaluated, if the
515 * sel and the right input are known.
517 static ir_tarval *computed_value_Mux(const ir_node *n)
519 ir_node *sel = get_Mux_sel(n);
520 ir_tarval *ts = value_of(sel);
522 if (ts == get_tarval_b_true()) {
523 ir_node *v = get_Mux_true(n);
526 else if (ts == get_tarval_b_false()) {
527 ir_node *v = get_Mux_false(n);
534 * Calculate the value of a Confirm: can be evaluated,
535 * if it has the form Confirm(x, '=', Const).
537 static ir_tarval *computed_value_Confirm(const ir_node *n)
539 if (get_Confirm_relation(n) == ir_relation_equal) {
540 ir_tarval *tv = value_of(get_Confirm_bound(n));
541 if (tv != tarval_bad)
544 return value_of(get_Confirm_value(n));
548 * gives a (conservative) estimation of possible relation when comparing
551 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
552 const ir_node *right)
554 ir_relation possible = ir_relation_true;
555 ir_tarval *tv_l = value_of(left);
556 ir_tarval *tv_r = value_of(right);
557 ir_mode *mode = get_irn_mode(left);
558 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
559 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
561 /* both values known - evaluate them */
562 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
563 possible = tarval_cmp(tv_l, tv_r);
564 /* we can return now, won't get any better */
567 /* a == a is never less or greater (but might be equal or unordered) */
569 possible &= ~ir_relation_less_greater;
570 /* unordered results only happen for float compares */
571 if (!mode_is_float(mode))
572 possible &= ~ir_relation_unordered;
573 /* values can never be less than the least representable number or
574 * greater than the greatest representable number */
576 possible &= ~ir_relation_greater;
578 possible &= ~ir_relation_less;
580 possible &= ~ir_relation_greater;
582 possible &= ~ir_relation_less;
583 /* maybe vrp can tell us more */
584 possible &= vrp_cmp(left, right);
585 /* Alloc nodes never return null (but throw an exception) */
586 if (is_Alloc(left) && tarval_is_null(tv_r))
587 possible &= ~ir_relation_equal;
588 /* stuff known through confirm nodes */
589 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
590 possible &= get_Confirm_relation(left);
592 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
593 ir_relation relation = get_Confirm_relation(right);
594 relation = get_inversed_relation(relation);
595 possible &= relation;
601 static ir_tarval *compute_cmp(const ir_node *cmp)
603 ir_node *left = get_Cmp_left(cmp);
604 ir_node *right = get_Cmp_right(cmp);
605 ir_relation possible = ir_get_possible_cmp_relations(left, right);
606 ir_relation relation = get_Cmp_relation(cmp);
608 /* if none of the requested relations is possible, return false */
609 if ((possible & relation) == ir_relation_false)
610 return tarval_b_false;
611 /* if possible relations are a subset of the requested ones return true */
612 if ((possible & ~relation) == ir_relation_false)
613 return tarval_b_true;
615 return computed_value_Cmp_Confirm(cmp, left, right, relation);
619 * some people want to call compute_cmp directly, in this case we have to
620 * test the constant folding flag again
622 static ir_tarval *compute_cmp_ext(const ir_node *cmp)
624 if (!get_opt_constant_folding())
626 return compute_cmp(cmp);
630 * Return the value of a Cmp.
632 * The basic idea here is to determine which relations are possible and which
633 * one are definitely impossible.
635 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
637 /* we can't construct Constb after lowering mode_b nodes */
638 if (irg_is_constrained(get_irn_irg(cmp), IR_GRAPH_CONSTRAINT_MODEB_LOWERED))
641 return compute_cmp(cmp);
645 * Calculate the value of an integer Div.
646 * Special case: 0 / b
648 static ir_tarval *do_computed_value_Div(const ir_node *div)
650 const ir_node *a = get_Div_left(div);
651 const ir_node *b = get_Div_right(div);
652 const ir_mode *mode = get_Div_resmode(div);
653 ir_tarval *ta = value_of(a);
655 const ir_node *dummy;
657 /* cannot optimize 0 / b = 0 because of NaN */
658 if (!mode_is_float(mode)) {
659 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
660 return ta; /* 0 / b == 0 if b != 0 */
663 if (ta != tarval_bad && tb != tarval_bad)
664 return tarval_div(ta, tb);
669 * Calculate the value of an integer Mod of two nodes.
670 * Special case: a % 1
672 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
674 ir_tarval *ta = value_of(a);
675 ir_tarval *tb = value_of(b);
677 /* Compute a % 1 or c1 % c2 */
678 if (tarval_is_one(tb))
679 return get_mode_null(get_irn_mode(a));
680 if (ta != tarval_bad && tb != tarval_bad)
681 return tarval_mod(ta, tb);
686 * Return the value of a Proj(Div).
688 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
690 long proj_nr = get_Proj_proj(n);
691 if (proj_nr != pn_Div_res)
694 return do_computed_value_Div(get_Proj_pred(n));
698 * Return the value of a Proj(Mod).
700 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
702 long proj_nr = get_Proj_proj(n);
704 if (proj_nr == pn_Mod_res) {
705 const ir_node *mod = get_Proj_pred(n);
706 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
712 * Return the value of a Proj.
714 static ir_tarval *computed_value_Proj(const ir_node *proj)
716 ir_node *n = get_Proj_pred(proj);
718 if (n->op->ops.computed_value_Proj != NULL)
719 return n->op->ops.computed_value_Proj(proj);
724 * If the parameter n can be computed, return its value, else tarval_bad.
725 * Performs constant folding.
727 * @param n The node this should be evaluated
729 ir_tarval *computed_value(const ir_node *n)
731 vrp_attr *vrp = vrp_get_info(n);
732 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
733 return vrp->bits_set;
735 if (n->op->ops.computed_value)
736 return n->op->ops.computed_value(n);
741 * Optimize operations that are commutative and have neutral 0,
742 * so a op 0 = 0 op a = a.
744 static ir_node *equivalent_node_neutral_zero(ir_node *n)
748 ir_node *a = get_binop_left(n);
749 ir_node *b = get_binop_right(n);
754 /* After running compute_node there is only one constant predecessor.
755 Find this predecessors value and remember the other node: */
756 if ((tv = value_of(a)) != tarval_bad) {
758 } else if ((tv = value_of(b)) != tarval_bad) {
763 /* If this predecessors constant value is zero, the operation is
764 * unnecessary. Remove it.
766 * Beware: If n is a Add, the mode of on and n might be different
767 * which happens in this rare construction: NULL + 3.
768 * Then, a Conv would be needed which we cannot include here.
770 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
773 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
780 * Eor is commutative and has neutral 0.
782 static ir_node *equivalent_node_Eor(ir_node *n)
788 n = equivalent_node_neutral_zero(n);
789 if (n != oldn) return n;
792 b = get_Eor_right(n);
794 if (is_Eor(a) || is_Or_Eor_Add(a)) {
795 ir_node *aa = get_binop_left(a);
796 ir_node *ab = get_binop_right(a);
799 /* (a ^ b) ^ a -> b */
801 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
803 } else if (ab == b) {
804 /* (a ^ b) ^ b -> a */
806 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
810 if (is_Eor(b) || is_Or_Eor_Add(b)) {
811 ir_node *ba = get_binop_left(b);
812 ir_node *bb = get_binop_right(b);
815 /* a ^ (a ^ b) -> b */
817 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
819 } else if (bb == a) {
820 /* a ^ (b ^ a) -> b */
822 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
830 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
832 * The second one looks strange, but this construct
833 * is used heavily in the LCC sources :-).
835 * Beware: The Mode of an Add may be different than the mode of its
836 * predecessors, so we could not return a predecessors in all cases.
838 static ir_node *equivalent_node_Add(ir_node *n)
841 ir_node *left, *right;
842 ir_mode *mode = get_irn_mode(n);
844 n = equivalent_node_neutral_zero(n);
848 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
849 if (mode_is_float(mode)) {
850 ir_graph *irg = get_irn_irg(n);
851 if (get_irg_fp_model(irg) & fp_strict_algebraic)
855 left = get_Add_left(n);
856 right = get_Add_right(n);
859 if (get_Sub_right(left) == right) {
862 n = get_Sub_left(left);
863 if (mode == get_irn_mode(n)) {
864 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
870 if (get_Sub_right(right) == left) {
873 n = get_Sub_left(right);
874 if (mode == get_irn_mode(n)) {
875 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
884 * optimize operations that are not commutative but have neutral 0 on left,
887 static ir_node *equivalent_node_left_zero(ir_node *n)
891 ir_node *a = get_binop_left(n);
892 ir_node *b = get_binop_right(n);
893 ir_tarval *tb = value_of(b);
895 if (tarval_is_null(tb)) {
898 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
904 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
906 * The second one looks strange, but this construct
907 * is used heavily in the LCC sources :-).
909 * Beware: The Mode of a Sub may be different than the mode of its
910 * predecessors, so we could not return a predecessors in all cases.
912 static ir_node *equivalent_node_Sub(ir_node *n)
916 ir_mode *mode = get_irn_mode(n);
919 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
920 if (mode_is_float(mode)) {
921 ir_graph *irg = get_irn_irg(n);
922 if (get_irg_fp_model(irg) & fp_strict_algebraic)
926 b = get_Sub_right(n);
929 /* Beware: modes might be different */
930 if (tarval_is_null(tb)) {
931 ir_node *a = get_Sub_left(n);
932 if (mode == get_irn_mode(a)) {
935 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
943 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
946 * -(-a) == a, but might overflow two times.
947 * We handle it anyway here but the better way would be a
948 * flag. This would be needed for Pascal for instance.
950 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
953 ir_node *pred = get_unop_op(n);
955 /* optimize symmetric unop */
956 if (get_irn_op(pred) == get_irn_op(n)) {
957 n = get_unop_op(pred);
958 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
964 * Optimize a * 1 = 1 * a = a.
966 static ir_node *equivalent_node_Mul(ir_node *n)
969 ir_node *a = get_Mul_left(n);
971 /* we can handle here only the n * n = n bit cases */
972 if (get_irn_mode(n) == get_irn_mode(a)) {
973 ir_node *b = get_Mul_right(n);
977 * Mul is commutative and has again an other neutral element.
978 * Constants are place right, so check this case first.
981 if (tarval_is_one(tv)) {
983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
986 if (tarval_is_one(tv)) {
988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
996 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
998 static ir_node *equivalent_node_Or(ir_node *n)
1002 ir_node *a = get_Or_left(n);
1003 ir_node *b = get_Or_right(n);
1007 n = a; /* idempotence */
1008 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1011 /* constants are normalized to right, check this side first */
1013 if (tarval_is_null(tv)) {
1015 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1019 if (tarval_is_null(tv)) {
1021 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1029 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1031 static ir_node *equivalent_node_And(ir_node *n)
1035 ir_node *a = get_And_left(n);
1036 ir_node *b = get_And_right(n);
1040 n = a; /* idempotence */
1041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1044 /* constants are normalized to right, check this side first */
1046 if (tarval_is_all_one(tv)) {
1048 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1051 if (tv != get_tarval_bad()) {
1052 ir_mode *mode = get_irn_mode(n);
1053 if (!mode_is_signed(mode) && is_Conv(a)) {
1054 ir_node *convop = get_Conv_op(a);
1055 ir_mode *convopmode = get_irn_mode(convop);
1056 if (!mode_is_signed(convopmode)) {
1057 /* Check Conv(all_one) & Const = all_one */
1058 ir_tarval *one = get_mode_all_one(convopmode);
1059 ir_tarval *conv = tarval_convert_to(one, mode);
1060 ir_tarval *tand = tarval_and(conv, tv);
1062 if (tarval_is_all_one(tand)) {
1063 /* Conv(X) & Const = X */
1065 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1072 if (tarval_is_all_one(tv)) {
1074 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1078 if ((is_Or(a) || is_Or_Eor_Add(a))
1079 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1081 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1085 if ((is_Or(b) || is_Or_Eor_Add(b))
1086 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1088 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1095 * Try to remove useless Conv's:
1097 static ir_node *equivalent_node_Conv(ir_node *n)
1100 ir_node *a = get_Conv_op(n);
1102 ir_mode *n_mode = get_irn_mode(n);
1103 ir_mode *a_mode = get_irn_mode(a);
1105 if (n_mode == a_mode) { /* No Conv necessary */
1107 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1109 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1110 ir_node *b = get_Conv_op(a);
1111 ir_mode *b_mode = get_irn_mode(b);
1113 if (n_mode == b_mode && values_in_mode(b_mode, a_mode)) {
1115 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1123 * - fold Phi-nodes, iff they have only one predecessor except
1126 static ir_node *equivalent_node_Phi(ir_node *n)
1131 ir_node *first_val = NULL; /* to shutup gcc */
1133 if (!get_opt_optimize() &&
1134 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1137 n_preds = get_Phi_n_preds(n);
1139 /* Phi of dead Region without predecessors. */
1143 /* Find first non-self-referencing input */
1144 for (i = 0; i < n_preds; ++i) {
1145 first_val = get_Phi_pred(n, i);
1146 /* not self pointer */
1147 if (first_val != n) {
1148 /* then found first value. */
1153 /* search for rest of inputs, determine if any of these
1154 are non-self-referencing */
1155 while (++i < n_preds) {
1156 ir_node *scnd_val = get_Phi_pred(n, i);
1157 if (scnd_val != n && scnd_val != first_val) {
1162 if (i >= n_preds && !is_Dummy(first_val)) {
1163 /* Fold, if no multiple distinct non-self-referencing inputs */
1165 DBG_OPT_PHI(oldn, n);
1171 * Optimize Proj(Tuple).
1173 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1175 ir_node *oldn = proj;
1176 ir_node *tuple = get_Proj_pred(proj);
1178 /* Remove the Tuple/Proj combination. */
1179 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1180 DBG_OPT_TUPLE(oldn, tuple, proj);
1186 * Optimize a / 1 = a.
1188 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1190 ir_node *oldn = proj;
1191 ir_node *div = get_Proj_pred(proj);
1192 ir_node *b = get_Div_right(div);
1193 ir_tarval *tb = value_of(b);
1195 /* Div is not commutative. */
1196 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1197 switch (get_Proj_proj(proj)) {
1199 proj = get_Div_mem(div);
1200 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1204 proj = get_Div_left(div);
1205 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1209 /* we cannot replace the exception Proj's here, this is done in
1210 transform_node_Proj_Div() */
1218 * Optimize CopyB(mem, x, x) into a Nop.
1220 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1222 ir_node *oldn = proj;
1223 ir_node *copyb = get_Proj_pred(proj);
1224 ir_node *a = get_CopyB_dst(copyb);
1225 ir_node *b = get_CopyB_src(copyb);
1228 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1229 switch (get_Proj_proj(proj)) {
1231 proj = get_CopyB_mem(copyb);
1232 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1240 * Optimize Bounds(idx, idx, upper) into idx.
1242 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1244 ir_node *oldn = proj;
1245 ir_node *bound = get_Proj_pred(proj);
1246 ir_node *idx = get_Bound_index(bound);
1247 ir_node *pred = skip_Proj(idx);
1250 if (idx == get_Bound_lower(bound))
1252 else if (is_Bound(pred)) {
1254 * idx was Bounds checked previously, it is still valid if
1255 * lower <= pred_lower && pred_upper <= upper.
1257 ir_node *lower = get_Bound_lower(bound);
1258 ir_node *upper = get_Bound_upper(bound);
1259 if (get_Bound_lower(pred) == lower &&
1260 get_Bound_upper(pred) == upper) {
1262 * One could expect that we simply return the previous
1263 * Bound here. However, this would be wrong, as we could
1264 * add an exception Proj to a new location then.
1265 * So, we must turn in into a tuple.
1271 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1272 switch (get_Proj_proj(proj)) {
1274 DBG_OPT_EXC_REM(proj);
1275 proj = get_Bound_mem(bound);
1279 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1282 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1290 * Does all optimizations on nodes that must be done on its Projs
1291 * because of creating new nodes.
1293 static ir_node *equivalent_node_Proj(ir_node *proj)
1295 ir_node *n = get_Proj_pred(proj);
1296 if (n->op->ops.equivalent_node_Proj)
1297 return n->op->ops.equivalent_node_Proj(proj);
1304 static ir_node *equivalent_node_Id(ir_node *n)
1312 DBG_OPT_ID(oldn, n);
1319 static ir_node *equivalent_node_Mux(ir_node *n)
1321 ir_node *oldn = n, *sel = get_Mux_sel(n);
1323 ir_tarval *ts = value_of(sel);
1325 if (ts == tarval_bad && is_Cmp(sel)) {
1326 /* try again with a direct call to compute_cmp, as we don't care
1327 * about the MODEB_LOWERED flag here */
1328 ts = compute_cmp_ext(sel);
1331 /* Mux(true, f, t) == t */
1332 if (ts == tarval_b_true) {
1333 n = get_Mux_true(n);
1334 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1337 /* Mux(false, f, t) == f */
1338 if (ts == tarval_b_false) {
1339 n = get_Mux_false(n);
1340 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1343 n_t = get_Mux_true(n);
1344 n_f = get_Mux_false(n);
1346 /* Mux(v, x, T) == x */
1347 if (is_Unknown(n_f)) {
1349 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1352 /* Mux(v, T, x) == x */
1353 if (is_Unknown(n_t)) {
1355 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1359 /* Mux(v, x, x) == x */
1362 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1365 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1366 ir_relation relation = get_Cmp_relation(sel);
1367 ir_node *f = get_Mux_false(n);
1368 ir_node *t = get_Mux_true(n);
1371 * Note further that these optimization work even for floating point
1372 * with NaN's because -NaN == NaN.
1373 * However, if +0 and -0 is handled differently, we cannot use the first one.
1375 ir_node *const cmp_l = get_Cmp_left(sel);
1376 ir_node *const cmp_r = get_Cmp_right(sel);
1379 case ir_relation_equal:
1380 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1381 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1388 case ir_relation_less_greater:
1389 case ir_relation_unordered_less_greater:
1390 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1391 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1402 * Note: normalization puts the constant on the right side,
1403 * so we check only one case.
1405 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1406 /* Mux(t CMP 0, X, t) */
1407 if (is_Minus(f) && get_Minus_op(f) == t) {
1408 /* Mux(t CMP 0, -t, t) */
1409 if (relation == ir_relation_equal) {
1410 /* Mux(t == 0, -t, t) ==> -t */
1412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1413 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1414 /* Mux(t != 0, -t, t) ==> t */
1416 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1426 * Remove Confirm nodes if setting is on.
1427 * Replace Confirms(x, '=', Constlike) by Constlike.
1429 static ir_node *equivalent_node_Confirm(ir_node *n)
1431 ir_node *pred = get_Confirm_value(n);
1432 ir_relation relation = get_Confirm_relation(n);
1434 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1436 * rare case: two identical Confirms one after another,
1437 * replace the second one with the first.
1440 pred = get_Confirm_value(n);
1446 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1447 * perform no actual computation, as, e.g., the Id nodes. It does not create
1448 * new nodes. It is therefore safe to free n if the node returned is not n.
1449 * If a node returns a Tuple we can not just skip it. If the size of the
1450 * in array fits, we transform n into a tuple (e.g., Div).
1452 ir_node *equivalent_node(ir_node *n)
1454 if (n->op->ops.equivalent_node)
1455 return n->op->ops.equivalent_node(n);
1460 * Returns non-zero if a node is a Phi node
1461 * with all predecessors constant.
1463 static int is_const_Phi(ir_node *n)
1467 if (! is_Phi(n) || get_irn_arity(n) == 0)
1469 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1470 if (! is_Const(get_irn_n(n, i)))
1476 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1477 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1480 * in reality eval_func should be tarval (*eval_func)() but incomplete
1481 * declarations are bad style and generate noisy warnings
1483 typedef void (*eval_func)(void);
1486 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1488 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1490 if (eval == (eval_func) tarval_sub) {
1491 tarval_sub_type func = (tarval_sub_type)eval;
1493 return func(a, b, mode);
1495 tarval_binop_type func = (tarval_binop_type)eval;
1502 * Apply an evaluator on a binop with a constant operators (and one Phi).
1504 * @param phi the Phi node
1505 * @param other the other operand
1506 * @param eval an evaluator function
1507 * @param mode the mode of the result, may be different from the mode of the Phi!
1508 * @param left if non-zero, other is the left operand, else the right
1510 * @return a new Phi node if the conversion was successful, NULL else
1512 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1518 int i, n = get_irn_arity(phi);
1520 NEW_ARR_A(void *, res, n);
1522 for (i = 0; i < n; ++i) {
1523 pred = get_irn_n(phi, i);
1524 tv = get_Const_tarval(pred);
1525 tv = do_eval(eval, other, tv, mode);
1527 if (tv == tarval_bad) {
1528 /* folding failed, bad */
1534 for (i = 0; i < n; ++i) {
1535 pred = get_irn_n(phi, i);
1536 tv = get_Const_tarval(pred);
1537 tv = do_eval(eval, tv, other, mode);
1539 if (tv == tarval_bad) {
1540 /* folding failed, bad */
1546 irg = get_irn_irg(phi);
1547 for (i = 0; i < n; ++i) {
1548 pred = get_irn_n(phi, i);
1549 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1551 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1555 * Apply an evaluator on a binop with two constant Phi.
1557 * @param a the left Phi node
1558 * @param b the right Phi node
1559 * @param eval an evaluator function
1560 * @param mode the mode of the result, may be different from the mode of the Phi!
1562 * @return a new Phi node if the conversion was successful, NULL else
1564 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1566 ir_tarval *tv_l, *tv_r, *tv;
1572 if (get_nodes_block(a) != get_nodes_block(b))
1575 n = get_irn_arity(a);
1576 NEW_ARR_A(void *, res, n);
1578 for (i = 0; i < n; ++i) {
1579 pred = get_irn_n(a, i);
1580 tv_l = get_Const_tarval(pred);
1581 pred = get_irn_n(b, i);
1582 tv_r = get_Const_tarval(pred);
1583 tv = do_eval(eval, tv_l, tv_r, mode);
1585 if (tv == tarval_bad) {
1586 /* folding failed, bad */
1591 irg = get_irn_irg(a);
1592 for (i = 0; i < n; ++i) {
1593 pred = get_irn_n(a, i);
1594 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1596 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1600 * Apply an evaluator on a unop with a constant operator (a Phi).
1602 * @param phi the Phi node
1603 * @param eval an evaluator function
1605 * @return a new Phi node if the conversion was successful, NULL else
1607 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1614 int i, n = get_irn_arity(phi);
1616 NEW_ARR_A(void *, res, n);
1617 for (i = 0; i < n; ++i) {
1618 pred = get_irn_n(phi, i);
1619 tv = get_Const_tarval(pred);
1622 if (tv == tarval_bad) {
1623 /* folding failed, bad */
1628 mode = get_irn_mode(phi);
1629 irg = get_irn_irg(phi);
1630 for (i = 0; i < n; ++i) {
1631 pred = get_irn_n(phi, i);
1632 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1634 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1638 * Apply a conversion on a constant operator (a Phi).
1640 * @param phi the Phi node
1642 * @return a new Phi node if the conversion was successful, NULL else
1644 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1650 int i, n = get_irn_arity(phi);
1652 NEW_ARR_A(void *, res, n);
1653 for (i = 0; i < n; ++i) {
1654 pred = get_irn_n(phi, i);
1655 tv = get_Const_tarval(pred);
1656 tv = tarval_convert_to(tv, mode);
1658 if (tv == tarval_bad) {
1659 /* folding failed, bad */
1664 irg = get_irn_irg(phi);
1665 for (i = 0; i < n; ++i) {
1666 pred = get_irn_n(phi, i);
1667 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1669 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1673 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1674 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1675 * If possible, remove the Conv's.
1677 static ir_node *transform_node_AddSub(ir_node *n)
1679 ir_mode *mode = get_irn_mode(n);
1681 if (mode_is_reference(mode)) {
1682 ir_node *left = get_binop_left(n);
1683 ir_node *right = get_binop_right(n);
1684 unsigned ref_bits = get_mode_size_bits(mode);
1686 if (is_Conv(left)) {
1687 ir_mode *lmode = get_irn_mode(left);
1688 unsigned bits = get_mode_size_bits(lmode);
1690 if (ref_bits == bits &&
1691 mode_is_int(lmode) &&
1692 get_mode_arithmetic(lmode) == irma_twos_complement) {
1693 ir_node *pre = get_Conv_op(left);
1694 ir_mode *pre_mode = get_irn_mode(pre);
1696 if (mode_is_int(pre_mode) &&
1697 get_mode_size_bits(pre_mode) == bits &&
1698 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1699 /* ok, this conv just changes to sign, moreover the calculation
1700 * is done with same number of bits as our address mode, so
1701 * we can ignore the conv as address calculation can be viewed
1702 * as either signed or unsigned
1704 set_binop_left(n, pre);
1709 if (is_Conv(right)) {
1710 ir_mode *rmode = get_irn_mode(right);
1711 unsigned bits = get_mode_size_bits(rmode);
1713 if (ref_bits == bits &&
1714 mode_is_int(rmode) &&
1715 get_mode_arithmetic(rmode) == irma_twos_complement) {
1716 ir_node *pre = get_Conv_op(right);
1717 ir_mode *pre_mode = get_irn_mode(pre);
1719 if (mode_is_int(pre_mode) &&
1720 get_mode_size_bits(pre_mode) == bits &&
1721 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1722 /* ok, this conv just changes to sign, moreover the calculation
1723 * is done with same number of bits as our address mode, so
1724 * we can ignore the conv as address calculation can be viewed
1725 * as either signed or unsigned
1727 set_binop_right(n, pre);
1732 /* let address arithmetic use unsigned modes */
1733 if (is_Const(right)) {
1734 ir_mode *rmode = get_irn_mode(right);
1736 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1737 /* convert a AddP(P, *s) into AddP(P, *u) */
1738 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1740 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1741 set_binop_right(n, pre);
1749 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1752 if (is_Const(b) && is_const_Phi(a)) { \
1753 /* check for Op(Phi, Const) */ \
1754 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1756 else if (is_Const(a) && is_const_Phi(b)) { \
1757 /* check for Op(Const, Phi) */ \
1758 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1760 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1761 /* check for Op(Phi, Phi) */ \
1762 c = apply_binop_on_2_phis(a, b, eval, mode); \
1765 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1770 #define HANDLE_UNOP_PHI(eval, a, c) \
1773 if (is_const_Phi(a)) { \
1774 /* check for Op(Phi) */ \
1775 c = apply_unop_on_phi(a, eval); \
1777 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1784 * Create a 0 constant of given mode.
1786 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1788 ir_tarval *tv = get_mode_null(mode);
1789 ir_node *cnst = new_r_Const(irg, tv);
1794 static bool is_shiftop(const ir_node *n)
1796 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1799 /* the order of the values is important! */
1800 typedef enum const_class {
1806 static const_class classify_const(const ir_node* n)
1808 if (is_Const(n)) return const_const;
1809 if (is_irn_constlike(n)) return const_like;
1814 * Determines whether r is more constlike or has a larger index (in that order)
1817 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1819 const const_class l_order = classify_const(l);
1820 const const_class r_order = classify_const(r);
1822 l_order > r_order ||
1823 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1826 static bool is_cmp_unequal(const ir_node *node)
1828 ir_relation relation = get_Cmp_relation(node);
1829 ir_node *left = get_Cmp_left(node);
1830 ir_node *right = get_Cmp_right(node);
1831 ir_mode *mode = get_irn_mode(left);
1833 if (relation == ir_relation_less_greater)
1836 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1837 return relation == ir_relation_greater;
1842 * returns true for Cmp(x == 0) or Cmp(x != 0)
1844 static bool is_cmp_equality_zero(const ir_node *node)
1846 ir_relation relation;
1847 ir_node *right = get_Cmp_right(node);
1849 if (!is_Const(right) || !is_Const_null(right))
1851 relation = get_Cmp_relation(node);
1852 return relation == ir_relation_equal
1853 || relation == ir_relation_less_greater
1854 || (!mode_is_signed(get_irn_mode(right))
1855 && relation == ir_relation_greater);
1859 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1860 * Such pattern may arise in bitfield stores.
1862 * value c4 value c4 & c2
1863 * AND c3 AND c1 | c3
1870 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1873 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1875 ir_node *irn_and, *c1;
1877 ir_node *and_l, *c3;
1878 ir_node *value, *c4;
1879 ir_node *new_and, *new_const, *block;
1880 ir_mode *mode = get_irn_mode(irn_or);
1882 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1886 irn_and = get_binop_left(irn_or);
1887 c1 = get_binop_right(irn_or);
1888 if (!is_Const(c1) || !is_And(irn_and))
1891 or_l = get_binop_left(irn_and);
1892 c2 = get_binop_right(irn_and);
1896 tv1 = get_Const_tarval(c1);
1897 tv2 = get_Const_tarval(c2);
1899 tv = tarval_or(tv1, tv2);
1900 if (tarval_is_all_one(tv)) {
1901 /* the AND does NOT clear a bit with isn't set by the OR */
1902 set_binop_left(irn_or, or_l);
1903 set_binop_right(irn_or, c1);
1905 /* check for more */
1909 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
1912 and_l = get_binop_left(or_l);
1913 c3 = get_binop_right(or_l);
1914 if (!is_Const(c3) || !is_And(and_l))
1917 value = get_binop_left(and_l);
1918 c4 = get_binop_right(and_l);
1922 /* ok, found the pattern, check for conditions */
1923 assert(mode == get_irn_mode(irn_and));
1924 assert(mode == get_irn_mode(or_l));
1925 assert(mode == get_irn_mode(and_l));
1927 tv3 = get_Const_tarval(c3);
1928 tv4 = get_Const_tarval(c4);
1930 tv = tarval_or(tv4, tv2);
1931 if (!tarval_is_all_one(tv)) {
1932 /* have at least one 0 at the same bit position */
1936 if (tv3 != tarval_andnot(tv3, tv4)) {
1937 /* bit in the or_mask is outside the and_mask */
1941 if (tv1 != tarval_andnot(tv1, tv2)) {
1942 /* bit in the or_mask is outside the and_mask */
1946 /* ok, all conditions met */
1947 block = get_nodes_block(irn_or);
1948 irg = get_irn_irg(block);
1950 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
1952 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
1954 set_binop_left(irn_or, new_and);
1955 set_binop_right(irn_or, new_const);
1957 /* check for more */
1962 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
1964 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
1966 ir_mode *mode = get_irn_mode(irn_or);
1967 ir_node *shl, *shr, *block;
1968 ir_node *irn, *x, *c1, *c2, *n;
1969 ir_tarval *tv1, *tv2;
1971 /* some backends can't handle rotl */
1972 if (!be_get_backend_param()->support_rotl)
1975 if (! mode_is_int(mode))
1978 shl = get_binop_left(irn_or);
1979 shr = get_binop_right(irn_or);
1988 } else if (!is_Shl(shl)) {
1990 } else if (!is_Shr(shr)) {
1993 x = get_Shl_left(shl);
1994 if (x != get_Shr_left(shr))
1997 c1 = get_Shl_right(shl);
1998 c2 = get_Shr_right(shr);
1999 if (is_Const(c1) && is_Const(c2)) {
2000 tv1 = get_Const_tarval(c1);
2001 if (! tarval_is_long(tv1))
2004 tv2 = get_Const_tarval(c2);
2005 if (! tarval_is_long(tv2))
2008 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2009 != (int) get_mode_size_bits(mode))
2012 /* yet, condition met */
2013 block = get_nodes_block(irn_or);
2015 n = new_r_Rotl(block, x, c1, mode);
2017 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2021 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2022 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2023 if (!ir_is_negated_value(c1, c2)) {
2027 /* yet, condition met */
2028 block = get_nodes_block(irn_or);
2029 n = new_r_Rotl(block, x, c1, mode);
2030 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2035 * Prototype of a recursive transform function
2036 * for bitwise distributive transformations.
2038 typedef ir_node* (*recursive_transform)(ir_node *n);
2041 * makes use of distributive laws for and, or, eor
2042 * and(a OP c, b OP c) -> and(a, b) OP c
2043 * note, might return a different op than n
2045 static ir_node *transform_bitwise_distributive(ir_node *n,
2046 recursive_transform trans_func)
2049 ir_node *a = get_binop_left(n);
2050 ir_node *b = get_binop_right(n);
2051 ir_op *op = get_irn_op(a);
2052 ir_op *op_root = get_irn_op(n);
2054 if (op != get_irn_op(b))
2057 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2058 if (op == op_Conv) {
2059 ir_node *a_op = get_Conv_op(a);
2060 ir_node *b_op = get_Conv_op(b);
2061 ir_mode *a_mode = get_irn_mode(a_op);
2062 ir_mode *b_mode = get_irn_mode(b_op);
2063 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2064 ir_node *blk = get_nodes_block(n);
2067 set_binop_left(n, a_op);
2068 set_binop_right(n, b_op);
2069 set_irn_mode(n, a_mode);
2071 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2073 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2079 /* nothing to gain here */
2083 if (op == op_Shrs || op == op_Shr || op == op_Shl
2084 || op == op_And || op == op_Or || op == op_Eor) {
2085 ir_node *a_left = get_binop_left(a);
2086 ir_node *a_right = get_binop_right(a);
2087 ir_node *b_left = get_binop_left(b);
2088 ir_node *b_right = get_binop_right(b);
2090 ir_node *op1 = NULL;
2091 ir_node *op2 = NULL;
2093 if (is_op_commutative(op)) {
2094 if (a_left == b_left) {
2098 } else if (a_left == b_right) {
2102 } else if (a_right == b_left) {
2108 if (a_right == b_right) {
2115 /* (a sop c) & (b sop c) => (a & b) sop c */
2116 ir_node *blk = get_nodes_block(n);
2118 ir_node *new_n = exact_copy(n);
2119 set_binop_left(new_n, op1);
2120 set_binop_right(new_n, op2);
2121 new_n = trans_func(new_n);
2123 if (op_root == op_Eor && op == op_Or) {
2124 dbg_info *dbgi = get_irn_dbg_info(n);
2125 ir_mode *mode = get_irn_mode(c);
2127 c = new_rd_Not(dbgi, blk, c, mode);
2128 n = new_rd_And(dbgi, blk, new_n, c, mode);
2131 set_nodes_block(n, blk);
2132 set_binop_left(n, new_n);
2133 set_binop_right(n, c);
2137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2146 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2148 * - and, or, xor instead of &
2149 * - Shl, Shr, Shrs, rotl instead of >>
2150 * (with a special case for Or/Xor + Shrs)
2152 * This normalisation is usually good for the backend since << C can often be
2153 * matched as address-mode.
2155 static ir_node *transform_node_bitop_shift(ir_node *n)
2157 ir_graph *irg = get_irn_irg(n);
2158 ir_node *left = get_binop_left(n);
2159 ir_node *right = get_binop_right(n);
2160 ir_mode *mode = get_irn_mode(n);
2161 ir_node *shift_left;
2162 ir_node *shift_right;
2164 dbg_info *dbg_bitop;
2165 dbg_info *dbg_shift;
2171 ir_tarval *tv_bitop;
2173 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2176 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2177 if (!is_Const(right) || !is_shiftop(left))
2180 shift_left = get_binop_left(left);
2181 shift_right = get_binop_right(left);
2182 if (!is_Const(shift_right))
2185 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2186 if (is_Shrs(left)) {
2187 /* TODO this could be improved */
2191 irg = get_irn_irg(n);
2192 block = get_nodes_block(n);
2193 dbg_bitop = get_irn_dbg_info(n);
2194 dbg_shift = get_irn_dbg_info(left);
2195 tv1 = get_Const_tarval(shift_right);
2196 tv2 = get_Const_tarval(right);
2197 assert(get_tarval_mode(tv2) == mode);
2200 tv_bitop = tarval_shr(tv2, tv1);
2202 /* Check whether we have lost some bits during the right shift. */
2204 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2206 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2209 } else if (is_Shr(left)) {
2212 * TODO this can be improved by checking whether
2213 * the left shift produces an overflow
2217 tv_bitop = tarval_shl(tv2, tv1);
2219 assert(is_Rotl(left));
2220 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2222 new_const = new_r_Const(irg, tv_bitop);
2225 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2226 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2227 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2230 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2234 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2235 } else if (is_Shr(left)) {
2236 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2238 assert(is_Rotl(left));
2239 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2245 static bool complement_values(const ir_node *a, const ir_node *b)
2247 if (is_Not(a) && get_Not_op(a) == b)
2249 if (is_Not(b) && get_Not_op(b) == a)
2251 if (is_Const(a) && is_Const(b)) {
2252 ir_tarval *tv_a = get_Const_tarval(a);
2253 ir_tarval *tv_b = get_Const_tarval(b);
2254 return tarval_not(tv_a) == tv_b;
2259 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2262 * for associative operations fold:
2263 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2264 * This is a "light" version of the reassociation phase
2266 static ir_node *fold_constant_associativity(ir_node *node,
2267 tv_fold_binop_func fold)
2272 ir_node *right = get_binop_right(node);
2273 ir_node *left_right;
2280 if (!is_Const(right))
2283 op = get_irn_op(node);
2284 left = get_binop_left(node);
2285 if (get_irn_op(left) != op)
2288 left_right = get_binop_right(left);
2289 if (!is_Const(left_right))
2292 left_left = get_binop_left(left);
2293 c0 = get_Const_tarval(left_right);
2294 c1 = get_Const_tarval(right);
2295 irg = get_irn_irg(node);
2296 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2298 new_c = fold(c0, c1);
2299 if (new_c == tarval_bad)
2301 new_const = new_r_Const(irg, new_c);
2302 new_node = exact_copy(node);
2303 set_binop_left(new_node, left_left);
2304 set_binop_right(new_node, new_const);
2311 static ir_node *transform_node_Or_(ir_node *n)
2314 ir_node *a = get_binop_left(n);
2315 ir_node *b = get_binop_right(n);
2319 n = fold_constant_associativity(n, tarval_or);
2323 if (is_Not(a) && is_Not(b)) {
2324 /* ~a | ~b = ~(a&b) */
2325 ir_node *block = get_nodes_block(n);
2327 mode = get_irn_mode(n);
2330 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2331 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2336 /* we can combine the relations of two compares with the same operands */
2337 if (is_Cmp(a) && is_Cmp(b)) {
2338 ir_node *a_left = get_Cmp_left(a);
2339 ir_node *a_right = get_Cmp_right(a);
2340 ir_node *b_left = get_Cmp_left(b);
2341 ir_node *b_right = get_Cmp_right(b);
2342 if (a_left == b_left && b_left == b_right) {
2343 dbg_info *dbgi = get_irn_dbg_info(n);
2344 ir_node *block = get_nodes_block(n);
2345 ir_relation a_relation = get_Cmp_relation(a);
2346 ir_relation b_relation = get_Cmp_relation(b);
2347 ir_relation new_relation = a_relation | b_relation;
2348 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2350 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2351 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2352 && !mode_is_float(get_irn_mode(a_left))
2353 && !mode_is_float(get_irn_mode(b_left))) {
2354 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2355 ir_graph *irg = get_irn_irg(n);
2356 dbg_info *dbgi = get_irn_dbg_info(n);
2357 ir_node *block = get_nodes_block(n);
2358 ir_mode *a_mode = get_irn_mode(a_left);
2359 ir_mode *b_mode = get_irn_mode(b_left);
2360 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2361 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2362 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2363 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2364 ir_node *zero = create_zero_const(irg, b_mode);
2365 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2367 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2368 ir_graph *irg = get_irn_irg(n);
2369 dbg_info *dbgi = get_irn_dbg_info(n);
2370 ir_node *block = get_nodes_block(n);
2371 ir_mode *a_mode = get_irn_mode(a_left);
2372 ir_mode *b_mode = get_irn_mode(b_left);
2373 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2374 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2375 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2376 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2377 ir_node *zero = create_zero_const(irg, a_mode);
2378 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2383 mode = get_irn_mode(n);
2384 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2386 n = transform_node_Or_bf_store(n);
2389 n = transform_node_Or_Rotl(n);
2393 n = transform_bitwise_distributive(n, transform_node_Or_);
2396 n = transform_node_bitop_shift(n);
2403 static ir_node *transform_node_Or(ir_node *n)
2405 if (is_Or_Eor_Add(n)) {
2406 dbg_info *dbgi = get_irn_dbg_info(n);
2407 ir_node *block = get_nodes_block(n);
2408 ir_node *left = get_Or_left(n);
2409 ir_node *right = get_Or_right(n);
2410 ir_mode *mode = get_irn_mode(n);
2411 return new_rd_Add(dbgi, block, left, right, mode);
2413 return transform_node_Or_(n);
2419 static ir_node *transform_node_Eor_(ir_node *n)
2422 ir_node *a = get_binop_left(n);
2423 ir_node *b = get_binop_right(n);
2424 ir_mode *mode = get_irn_mode(n);
2427 n = fold_constant_associativity(n, tarval_eor);
2431 /* we can combine the relations of two compares with the same operands */
2432 if (is_Cmp(a) && is_Cmp(b)) {
2433 ir_node *a_left = get_Cmp_left(a);
2434 ir_node *a_right = get_Cmp_left(a);
2435 ir_node *b_left = get_Cmp_left(b);
2436 ir_node *b_right = get_Cmp_right(b);
2437 if (a_left == b_left && b_left == b_right) {
2438 dbg_info *dbgi = get_irn_dbg_info(n);
2439 ir_node *block = get_nodes_block(n);
2440 ir_relation a_relation = get_Cmp_relation(a);
2441 ir_relation b_relation = get_Cmp_relation(b);
2442 ir_relation new_relation = a_relation ^ b_relation;
2443 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2447 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2449 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2450 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2451 dbg_info *dbg = get_irn_dbg_info(n);
2452 ir_node *block = get_nodes_block(n);
2453 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2454 ir_node *new_left = get_Not_op(a);
2455 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2456 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2458 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2459 dbg_info *dbg = get_irn_dbg_info(n);
2460 ir_node *block = get_nodes_block(n);
2461 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2462 ir_node *new_right = get_Not_op(b);
2463 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2464 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2468 /* x ^ 1...1 -> ~1 */
2469 if (is_Const(b) && is_Const_all_one(b)) {
2470 n = new_r_Not(get_nodes_block(n), a, mode);
2471 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2475 n = transform_bitwise_distributive(n, transform_node_Eor_);
2478 n = transform_node_bitop_shift(n);
2485 static ir_node *transform_node_Eor(ir_node *n)
2487 if (is_Or_Eor_Add(n)) {
2488 dbg_info *dbgi = get_irn_dbg_info(n);
2489 ir_node *block = get_nodes_block(n);
2490 ir_node *left = get_Eor_left(n);
2491 ir_node *right = get_Eor_right(n);
2492 ir_mode *mode = get_irn_mode(n);
2493 return new_rd_Add(dbgi, block, left, right, mode);
2495 return transform_node_Eor_(n);
2499 * Do the AddSub optimization, then Transform
2500 * Constant folding on Phi
2501 * Add(a,a) -> Mul(a, 2)
2502 * Add(Mul(a, x), a) -> Mul(a, x+1)
2503 * if the mode is integer or float.
2504 * Transform Add(a,-b) into Sub(a,b).
2505 * Reassociation might fold this further.
2507 static ir_node *transform_node_Add(ir_node *n)
2515 n = fold_constant_associativity(n, tarval_add);
2519 n = transform_node_AddSub(n);
2523 a = get_Add_left(n);
2524 b = get_Add_right(n);
2525 mode = get_irn_mode(n);
2527 if (mode_is_reference(mode)) {
2528 ir_mode *lmode = get_irn_mode(a);
2530 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2531 /* an Add(a, NULL) is a hidden Conv */
2532 dbg_info *dbg = get_irn_dbg_info(n);
2533 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2537 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2538 ir_tarval *tv = get_Const_tarval(b);
2539 ir_tarval *min = get_mode_min(mode);
2540 /* if all bits are set, then this has the same effect as a Not.
2541 * Note that the following == gives false for different modes which
2542 * is exactly what we want */
2544 dbg_info *dbgi = get_irn_dbg_info(n);
2545 ir_graph *irg = get_irn_irg(n);
2546 ir_node *block = get_nodes_block(n);
2547 ir_node *cnst = new_r_Const(irg, min);
2548 return new_rd_Eor(dbgi, block, a, cnst, mode);
2552 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2554 /* for FP the following optimizations are only allowed if
2555 * fp_strict_algebraic is disabled */
2556 if (mode_is_float(mode)) {
2557 ir_graph *irg = get_irn_irg(n);
2558 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2562 if (mode_is_num(mode)) {
2563 ir_graph *irg = get_irn_irg(n);
2564 /* the following code leads to endless recursion when Mul are replaced
2565 * by a simple instruction chain */
2566 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2567 && a == b && mode_is_int(mode)) {
2568 ir_node *block = get_nodes_block(n);
2571 get_irn_dbg_info(n),
2574 new_r_Const_long(irg, mode, 2),
2576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2581 get_irn_dbg_info(n),
2586 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2591 get_irn_dbg_info(n),
2596 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2599 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2600 /* Here we rely on constants be on the RIGHT side */
2602 ir_node *op = get_Not_op(a);
2604 if (is_Const(b) && is_Const_one(b)) {
2606 ir_node *blk = get_nodes_block(n);
2607 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2608 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2615 if (is_Or_Eor_Add(n)) {
2616 n = transform_node_Or_(n);
2619 n = transform_node_Eor_(n);
2628 * returns -cnst or NULL if impossible
2630 static ir_node *const_negate(ir_node *cnst)
2632 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2633 dbg_info *dbgi = get_irn_dbg_info(cnst);
2634 ir_graph *irg = get_irn_irg(cnst);
2635 if (tv == tarval_bad) return NULL;
2636 return new_rd_Const(dbgi, irg, tv);
2640 * Do the AddSub optimization, then Transform
2641 * Constant folding on Phi
2642 * Sub(0,a) -> Minus(a)
2643 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2644 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2645 * Sub(Add(a, x), x) -> a
2646 * Sub(x, Add(x, a)) -> -a
2647 * Sub(x, Const) -> Add(x, -Const)
2649 static ir_node *transform_node_Sub(ir_node *n)
2655 n = transform_node_AddSub(n);
2657 a = get_Sub_left(n);
2658 b = get_Sub_right(n);
2660 mode = get_irn_mode(n);
2662 if (mode_is_int(mode)) {
2663 ir_mode *lmode = get_irn_mode(a);
2665 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2666 /* a Sub(a, NULL) is a hidden Conv */
2667 dbg_info *dbg = get_irn_dbg_info(n);
2668 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2669 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2673 if (mode == lmode &&
2674 get_mode_arithmetic(mode) == irma_twos_complement &&
2676 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2678 dbg_info *dbg = get_irn_dbg_info(n);
2679 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2680 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2686 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2688 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2689 if (mode_is_float(mode)) {
2690 ir_graph *irg = get_irn_irg(n);
2691 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2695 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2696 /* a - C -> a + (-C) */
2697 ir_node *cnst = const_negate(b);
2699 ir_node *block = get_nodes_block(n);
2700 dbg_info *dbgi = get_irn_dbg_info(n);
2702 n = new_rd_Add(dbgi, block, a, cnst, mode);
2703 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2708 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2709 dbg_info *dbg = get_irn_dbg_info(n);
2710 ir_node *block = get_nodes_block(n);
2711 ir_node *left = get_Minus_op(a);
2712 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2714 n = new_rd_Minus(dbg, block, add, mode);
2715 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2717 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2718 dbg_info *dbg = get_irn_dbg_info(n);
2719 ir_node *block = get_nodes_block(n);
2720 ir_node *right = get_Minus_op(b);
2722 n = new_rd_Add(dbg, block, a, right, mode);
2723 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2725 } else if (is_Sub(b)) {
2726 /* a - (b - c) -> a + (c - b)
2727 * -> (a - b) + c iff (b - c) is a pointer */
2728 dbg_info *s_dbg = get_irn_dbg_info(b);
2729 ir_node *s_left = get_Sub_left(b);
2730 ir_node *s_right = get_Sub_right(b);
2731 ir_mode *s_mode = get_irn_mode(b);
2732 if (mode_is_reference(s_mode)) {
2733 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2734 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2735 dbg_info *a_dbg = get_irn_dbg_info(n);
2738 s_right = new_r_Conv(lowest_block, s_right, mode);
2739 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2741 ir_node *s_block = get_nodes_block(b);
2742 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2743 dbg_info *a_dbg = get_irn_dbg_info(n);
2744 ir_node *a_block = get_nodes_block(n);
2746 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2748 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2751 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2752 ir_node *m_right = get_Mul_right(b);
2753 if (is_Const(m_right)) {
2754 ir_node *cnst2 = const_negate(m_right);
2755 if (cnst2 != NULL) {
2756 dbg_info *m_dbg = get_irn_dbg_info(b);
2757 ir_node *m_block = get_nodes_block(b);
2758 ir_node *m_left = get_Mul_left(b);
2759 ir_mode *m_mode = get_irn_mode(b);
2760 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2761 dbg_info *a_dbg = get_irn_dbg_info(n);
2762 ir_node *a_block = get_nodes_block(n);
2764 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2765 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2772 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2773 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2775 get_irn_dbg_info(n),
2779 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2782 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2783 ir_node *left = get_binop_left(a);
2784 ir_node *right = get_binop_right(a);
2786 /* FIXME: Does the Conv's work only for two complement or generally? */
2788 if (mode != get_irn_mode(right)) {
2789 /* This Sub is an effective Cast */
2790 right = new_r_Conv(get_nodes_block(n), right, mode);
2793 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2795 } else if (right == b) {
2796 if (mode != get_irn_mode(left)) {
2797 /* This Sub is an effective Cast */
2798 left = new_r_Conv(get_nodes_block(n), left, mode);
2801 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2805 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2806 ir_node *left = get_binop_left(b);
2807 ir_node *right = get_binop_right(b);
2809 /* FIXME: Does the Conv's work only for two complement or generally? */
2811 ir_mode *r_mode = get_irn_mode(right);
2813 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2814 if (mode != r_mode) {
2815 /* This Sub is an effective Cast */
2816 n = new_r_Conv(get_nodes_block(n), n, mode);
2818 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2820 } else if (right == a) {
2821 ir_mode *l_mode = get_irn_mode(left);
2823 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2824 if (mode != l_mode) {
2825 /* This Sub is an effective Cast */
2826 n = new_r_Conv(get_nodes_block(n), n, mode);
2828 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2832 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2833 ir_mode *mode = get_irn_mode(a);
2835 if (mode == get_irn_mode(b)) {
2837 ir_node *op_a = get_Conv_op(a);
2838 ir_node *op_b = get_Conv_op(b);
2840 /* check if it's allowed to skip the conv */
2841 ma = get_irn_mode(op_a);
2842 mb = get_irn_mode(op_b);
2844 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2845 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2848 set_Sub_right(n, b);
2854 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2855 if (!is_reassoc_running() && is_Mul(a)) {
2856 ir_node *ma = get_Mul_left(a);
2857 ir_node *mb = get_Mul_right(a);
2860 ir_node *blk = get_nodes_block(n);
2861 ir_graph *irg = get_irn_irg(n);
2863 get_irn_dbg_info(n),
2867 get_irn_dbg_info(n),
2870 new_r_Const(irg, get_mode_one(mode)),
2873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2875 } else if (mb == b) {
2876 ir_node *blk = get_nodes_block(n);
2877 ir_graph *irg = get_irn_irg(n);
2879 get_irn_dbg_info(n),
2883 get_irn_dbg_info(n),
2886 new_r_Const(irg, get_mode_one(mode)),
2889 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2893 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2894 ir_node *x = get_Sub_left(a);
2895 ir_node *y = get_Sub_right(a);
2896 ir_node *blk = get_nodes_block(n);
2897 ir_mode *m_b = get_irn_mode(b);
2898 ir_mode *m_y = get_irn_mode(y);
2902 /* Determine the right mode for the Add. */
2905 else if (mode_is_reference(m_b))
2907 else if (mode_is_reference(m_y))
2911 * Both modes are different but none is reference,
2912 * happens for instance in SubP(SubP(P, Iu), Is).
2913 * We have two possibilities here: Cast or ignore.
2914 * Currently we ignore this case.
2919 add = new_r_Add(blk, y, b, add_mode);
2921 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2922 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2926 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2927 /* c - ~X = X + (c+1) */
2928 if (is_Const(a) && is_Not(b)) {
2929 ir_tarval *tv = get_Const_tarval(a);
2931 tv = tarval_add(tv, get_mode_one(mode));
2932 if (tv != tarval_bad) {
2933 ir_node *blk = get_nodes_block(n);
2934 ir_graph *irg = get_irn_irg(n);
2935 ir_node *c = new_r_Const(irg, tv);
2936 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2937 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2941 /* x-(x&y) = x & ~y */
2943 ir_node *and_left = get_And_left(b);
2944 ir_node *and_right = get_And_right(b);
2945 if (and_right == a) {
2946 ir_node *tmp = and_left;
2947 and_left = and_right;
2950 if (and_left == a) {
2951 dbg_info *dbgi = get_irn_dbg_info(n);
2952 ir_node *block = get_nodes_block(n);
2953 ir_mode *mode = get_irn_mode(n);
2954 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2955 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2964 * Several transformation done on n*n=2n bits mul.
2965 * These transformations must be done here because new nodes may be produced.
2967 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2970 ir_node *a = get_Mul_left(n);
2971 ir_node *b = get_Mul_right(n);
2972 ir_tarval *ta = value_of(a);
2973 ir_tarval *tb = value_of(b);
2974 ir_mode *smode = get_irn_mode(a);
2976 if (ta == get_mode_one(smode)) {
2977 /* (L)1 * (L)b = (L)b */
2978 ir_node *blk = get_nodes_block(n);
2979 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2980 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2983 else if (ta == get_mode_minus_one(smode)) {
2984 /* (L)-1 * (L)b = (L)b */
2985 ir_node *blk = get_nodes_block(n);
2986 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2987 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2991 if (tb == get_mode_one(smode)) {
2992 /* (L)a * (L)1 = (L)a */
2993 ir_node *blk = get_nodes_block(a);
2994 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2995 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2998 else if (tb == get_mode_minus_one(smode)) {
2999 /* (L)a * (L)-1 = (L)-a */
3000 ir_node *blk = get_nodes_block(n);
3001 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3002 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3003 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3010 * Transform Mul(a,-1) into -a.
3011 * Do constant evaluation of Phi nodes.
3012 * Do architecture dependent optimizations on Mul nodes
3014 static ir_node *transform_node_Mul(ir_node *n)
3016 ir_node *c, *oldn = n;
3017 ir_mode *mode = get_irn_mode(n);
3018 ir_node *a = get_Mul_left(n);
3019 ir_node *b = get_Mul_right(n);
3021 n = fold_constant_associativity(n, tarval_mul);
3025 if (mode != get_irn_mode(a))
3026 return transform_node_Mul2n(n, mode);
3028 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3030 if (mode_is_signed(mode)) {
3033 if (value_of(a) == get_mode_minus_one(mode))
3035 else if (value_of(b) == get_mode_minus_one(mode))
3038 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3039 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3044 if (is_Const(b)) { /* (-a) * const -> a * -const */
3045 ir_node *cnst = const_negate(b);
3047 dbg_info *dbgi = get_irn_dbg_info(n);
3048 ir_node *block = get_nodes_block(n);
3049 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3050 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3053 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3054 dbg_info *dbgi = get_irn_dbg_info(n);
3055 ir_node *block = get_nodes_block(n);
3056 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3057 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3059 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3060 ir_node *sub_l = get_Sub_left(b);
3061 ir_node *sub_r = get_Sub_right(b);
3062 dbg_info *dbgi = get_irn_dbg_info(n);
3063 ir_node *block = get_nodes_block(n);
3064 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3065 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3066 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3069 } else if (is_Minus(b)) {
3070 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3071 ir_node *sub_l = get_Sub_left(a);
3072 ir_node *sub_r = get_Sub_right(a);
3073 dbg_info *dbgi = get_irn_dbg_info(n);
3074 ir_node *block = get_nodes_block(n);
3075 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3076 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3077 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3080 } else if (is_Shl(a)) {
3081 ir_node *const shl_l = get_Shl_left(a);
3082 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3083 /* (1 << x) * b -> b << x */
3084 dbg_info *const dbgi = get_irn_dbg_info(n);
3085 ir_node *const block = get_nodes_block(n);
3086 ir_node *const shl_r = get_Shl_right(a);
3087 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3088 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3091 } else if (is_Shl(b)) {
3092 ir_node *const shl_l = get_Shl_left(b);
3093 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3094 /* a * (1 << x) -> a << x */
3095 dbg_info *const dbgi = get_irn_dbg_info(n);
3096 ir_node *const block = get_nodes_block(n);
3097 ir_node *const shl_r = get_Shl_right(b);
3098 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3099 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3103 if (get_mode_arithmetic(mode) == irma_ieee754
3104 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3106 ir_tarval *tv = get_Const_tarval(a);
3107 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3108 && !tarval_is_negative(tv)) {
3109 /* 2.0 * b = b + b */
3110 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3111 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3115 else if (is_Const(b)) {
3116 ir_tarval *tv = get_Const_tarval(b);
3117 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3118 && !tarval_is_negative(tv)) {
3119 /* a * 2.0 = a + a */
3120 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3121 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3126 return arch_dep_replace_mul_with_shifts(n);
3130 * Transform a Div Node.
3132 static ir_node *transform_node_Div(ir_node *n)
3134 ir_mode *mode = get_Div_resmode(n);
3135 ir_node *a = get_Div_left(n);
3136 ir_node *b = get_Div_right(n);
3138 const ir_node *dummy;
3140 if (mode_is_int(mode)) {
3141 if (is_Const(b) && is_const_Phi(a)) {
3142 /* check for Div(Phi, Const) */
3143 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3145 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3148 } else if (is_Const(a) && is_const_Phi(b)) {
3149 /* check for Div(Const, Phi) */
3150 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3152 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3155 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3156 /* check for Div(Phi, Phi) */
3157 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3159 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3164 if (a == b && value_not_zero(a, &dummy)) {
3165 ir_graph *irg = get_irn_irg(n);
3166 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3167 value = new_r_Const(irg, get_mode_one(mode));
3168 DBG_OPT_CSTEVAL(n, value);
3171 if (mode_is_signed(mode) && is_Const(b)) {
3172 ir_tarval *tv = get_Const_tarval(b);
3174 if (tv == get_mode_minus_one(mode)) {
3176 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3177 DBG_OPT_CSTEVAL(n, value);
3181 /* Try architecture dependent optimization */
3182 value = arch_dep_replace_div_by_const(n);
3185 assert(mode_is_float(mode));
3187 /* Optimize x/c to x*(1/c) */
3188 if (get_mode_arithmetic(mode) == irma_ieee754) {
3189 ir_tarval *tv = value_of(b);
3191 if (tv != tarval_bad) {
3192 int rem = tarval_fp_ops_enabled();
3195 * Floating point constant folding might be disabled here to
3197 * However, as we check for exact result, doing it is safe.
3200 tarval_enable_fp_ops(1);
3201 tv = tarval_div(get_mode_one(mode), tv);
3202 tarval_enable_fp_ops(rem);
3204 /* Do the transformation if the result is either exact or we are
3205 not using strict rules. */
3206 if (tv != tarval_bad &&
3207 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3208 ir_node *block = get_nodes_block(n);
3209 ir_graph *irg = get_irn_irg(block);
3210 ir_node *c = new_r_Const(irg, tv);
3211 dbg_info *dbgi = get_irn_dbg_info(n);
3212 value = new_rd_Mul(dbgi, block, a, c, mode);
3225 /* Turn Div into a tuple (mem, jmp, bad, value) */
3226 mem = get_Div_mem(n);
3227 blk = get_nodes_block(n);
3228 irg = get_irn_irg(blk);
3230 /* skip a potential Pin */
3231 mem = skip_Pin(mem);
3232 turn_into_tuple(n, pn_Div_max+1);
3233 set_Tuple_pred(n, pn_Div_M, mem);
3234 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3235 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3236 set_Tuple_pred(n, pn_Div_res, value);
3242 * Transform a Mod node.
3244 static ir_node *transform_node_Mod(ir_node *n)
3246 ir_mode *mode = get_Mod_resmode(n);
3247 ir_node *a = get_Mod_left(n);
3248 ir_node *b = get_Mod_right(n);
3253 if (is_Const(b) && is_const_Phi(a)) {
3254 /* check for Div(Phi, Const) */
3255 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3257 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3261 else if (is_Const(a) && is_const_Phi(b)) {
3262 /* check for Div(Const, Phi) */
3263 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3265 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3269 else if (is_const_Phi(a) && is_const_Phi(b)) {
3270 /* check for Div(Phi, Phi) */
3271 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3273 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3280 irg = get_irn_irg(n);
3281 if (tv != tarval_bad) {
3282 value = new_r_Const(irg, tv);
3284 DBG_OPT_CSTEVAL(n, value);
3287 ir_node *a = get_Mod_left(n);
3288 ir_node *b = get_Mod_right(n);
3289 const ir_node *dummy;
3291 if (a == b && value_not_zero(a, &dummy)) {
3292 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3293 value = new_r_Const(irg, get_mode_null(mode));
3294 DBG_OPT_CSTEVAL(n, value);
3297 if (mode_is_signed(mode) && is_Const(b)) {
3298 ir_tarval *tv = get_Const_tarval(b);
3300 if (tv == get_mode_minus_one(mode)) {
3302 value = new_r_Const(irg, get_mode_null(mode));
3303 DBG_OPT_CSTEVAL(n, value);
3307 /* Try architecture dependent optimization */
3308 value = arch_dep_replace_mod_by_const(n);
3317 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3318 mem = get_Mod_mem(n);
3319 blk = get_nodes_block(n);
3320 irg = get_irn_irg(blk);
3322 /* skip a potential Pin */
3323 mem = skip_Pin(mem);
3324 turn_into_tuple(n, pn_Mod_max+1);
3325 set_Tuple_pred(n, pn_Mod_M, mem);
3326 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3327 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3328 set_Tuple_pred(n, pn_Mod_res, value);
3334 * Transform a Cond node.
3336 * Replace the Cond by a Jmp if it branches on a constant
3339 static ir_node *transform_node_Cond(ir_node *n)
3341 ir_node *a = get_Cond_selector(n);
3342 ir_graph *irg = get_irn_irg(n);
3346 /* we need block info which is not available in floating irgs */
3347 if (get_irg_pinned(irg) == op_pin_state_floats)
3351 if (ta == tarval_bad && is_Cmp(a)) {
3352 /* try again with a direct call to compute_cmp, as we don't care
3353 * about the MODEB_LOWERED flag here */
3354 ta = compute_cmp_ext(a);
3357 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3358 /* It's a boolean Cond, branching on a boolean constant.
3359 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3360 ir_node *blk = get_nodes_block(n);
3361 jmp = new_r_Jmp(blk);
3362 turn_into_tuple(n, pn_Cond_max+1);
3363 if (ta == tarval_b_true) {
3364 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3365 set_Tuple_pred(n, pn_Cond_true, jmp);
3367 set_Tuple_pred(n, pn_Cond_false, jmp);
3368 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3370 /* We might generate an endless loop, so keep it alive. */
3371 add_End_keepalive(get_irg_end(irg), blk);
3372 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3377 static ir_node *transform_node_Switch(ir_node *n)
3379 ir_node *op = get_Switch_selector(n);
3380 ir_tarval *val = value_of(op);
3381 if (val != tarval_bad) {
3382 dbg_info *dbgi = get_irn_dbg_info(n);
3383 ir_graph *irg = get_irn_irg(n);
3384 unsigned n_outs = get_Switch_n_outs(n);
3385 ir_node *block = get_nodes_block(n);
3386 ir_node *bad = new_r_Bad(irg, mode_X);
3387 ir_node **in = XMALLOCN(ir_node*, n_outs);
3388 const ir_switch_table *table = get_Switch_table(n);
3389 size_t n_entries = ir_switch_table_get_n_entries(table);
3393 for (i = 0; i < n_entries; ++i) {
3394 const ir_switch_table_entry *entry
3395 = ir_switch_table_get_entry_const(table, i);
3396 ir_tarval *min = entry->min;
3397 ir_tarval *max = entry->max;
3400 if ((min == max && min == val)
3401 || (tarval_cmp(val, min) != ir_relation_less
3402 && tarval_cmp(val, max) != ir_relation_greater)) {
3407 for (o = 0; o < n_outs; ++o) {
3408 if (o == (unsigned)jmp_pn) {
3409 in[o] = new_rd_Jmp(dbgi, block);
3414 return new_r_Tuple(block, (int)n_outs, in);
3420 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3422 * - and, or, xor instead of &
3423 * - Shl, Shr, Shrs, rotl instead of >>
3424 * (with a special case for Or/Xor + Shrs)
3426 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3428 static ir_node *transform_node_shift_bitop(ir_node *n)
3430 ir_graph *irg = get_irn_irg(n);
3431 ir_node *right = get_binop_right(n);
3432 ir_mode *mode = get_irn_mode(n);
3434 ir_node *bitop_left;
3435 ir_node *bitop_right;
3444 ir_tarval *tv_shift;
3446 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3449 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3451 if (!is_Const(right))
3454 left = get_binop_left(n);
3455 op_left = get_irn_op(left);
3456 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3459 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3460 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3461 /* TODO: test if sign bit is affectes */
3465 bitop_right = get_binop_right(left);
3466 if (!is_Const(bitop_right))
3469 bitop_left = get_binop_left(left);
3471 block = get_nodes_block(n);
3472 dbgi = get_irn_dbg_info(n);
3473 tv1 = get_Const_tarval(bitop_right);
3474 tv2 = get_Const_tarval(right);
3476 assert(get_tarval_mode(tv1) == mode);
3479 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3480 tv_shift = tarval_shl(tv1, tv2);
3481 } else if (is_Shr(n)) {
3482 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3483 tv_shift = tarval_shr(tv1, tv2);
3484 } else if (is_Shrs(n)) {
3485 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3486 tv_shift = tarval_shrs(tv1, tv2);
3489 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3490 tv_shift = tarval_rotl(tv1, tv2);
3493 assert(get_tarval_mode(tv_shift) == mode);
3494 irg = get_irn_irg(n);
3495 new_const = new_r_Const(irg, tv_shift);
3497 if (op_left == op_And) {
3498 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3499 } else if (op_left == op_Or) {
3500 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3502 assert(op_left == op_Eor);
3503 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3512 static ir_node *transform_node_And(ir_node *n)
3514 ir_node *c, *oldn = n;
3515 ir_node *a = get_And_left(n);
3516 ir_node *b = get_And_right(n);
3519 n = fold_constant_associativity(n, tarval_and);
3523 if (is_Cmp(a) && is_Cmp(b)) {
3524 ir_node *a_left = get_Cmp_left(a);
3525 ir_node *a_right = get_Cmp_right(a);
3526 ir_node *b_left = get_Cmp_left(b);
3527 ir_node *b_right = get_Cmp_right(b);
3528 ir_relation a_relation = get_Cmp_relation(a);
3529 ir_relation b_relation = get_Cmp_relation(b);
3530 /* we can combine the relations of two compares with the same
3532 if (a_left == b_left && b_left == b_right) {
3533 dbg_info *dbgi = get_irn_dbg_info(n);
3534 ir_node *block = get_nodes_block(n);
3535 ir_relation new_relation = a_relation & b_relation;
3536 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3538 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3539 if (a_relation == b_relation && a_relation == ir_relation_equal
3540 && !mode_is_float(get_irn_mode(a_left))
3541 && !mode_is_float(get_irn_mode(b_left))) {
3542 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3543 dbg_info *dbgi = get_irn_dbg_info(n);
3544 ir_node *block = get_nodes_block(n);
3545 ir_mode *a_mode = get_irn_mode(a_left);
3546 ir_mode *b_mode = get_irn_mode(b_left);
3547 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3548 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3549 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3550 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3551 ir_graph *irg = get_irn_irg(n);
3552 ir_node *zero = create_zero_const(irg, b_mode);
3553 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3555 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3556 dbg_info *dbgi = get_irn_dbg_info(n);
3557 ir_node *block = get_nodes_block(n);
3558 ir_mode *a_mode = get_irn_mode(a_left);
3559 ir_mode *b_mode = get_irn_mode(b_left);
3560 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3561 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3562 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3563 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3564 ir_graph *irg = get_irn_irg(n);
3565 ir_node *zero = create_zero_const(irg, a_mode);
3566 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3571 mode = get_irn_mode(n);
3572 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3574 if (is_Or(a) || is_Or_Eor_Add(a)) {
3575 ir_node *or_left = get_binop_left(a);
3576 ir_node *or_right = get_binop_right(a);
3577 if (complement_values(or_left, b)) {
3578 /* (a|b) & ~a => b & ~a */
3579 dbg_info *dbgi = get_irn_dbg_info(n);
3580 ir_node *block = get_nodes_block(n);
3581 return new_rd_And(dbgi, block, or_right, b, mode);
3582 } else if (complement_values(or_right, b)) {
3583 /* (a|b) & ~b => a & ~b */
3584 dbg_info *dbgi = get_irn_dbg_info(n);
3585 ir_node *block = get_nodes_block(n);
3586 return new_rd_And(dbgi, block, or_left, b, mode);
3587 } else if (is_Not(b)) {
3588 ir_node *op = get_Not_op(b);
3590 ir_node *ba = get_And_left(op);
3591 ir_node *bb = get_And_right(op);
3593 /* it's enough to test the following cases due to normalization! */
3594 if (or_left == ba && or_right == bb) {
3595 /* (a|b) & ~(a&b) = a^b */
3596 ir_node *block = get_nodes_block(n);
3598 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3599 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3605 if (is_Or(b) || is_Or_Eor_Add(b)) {
3606 ir_node *or_left = get_binop_left(b);
3607 ir_node *or_right = get_binop_right(b);
3608 if (complement_values(or_left, a)) {
3609 /* (a|b) & ~a => b & ~a */
3610 dbg_info *dbgi = get_irn_dbg_info(n);
3611 ir_node *block = get_nodes_block(n);
3612 return new_rd_And(dbgi, block, or_right, a, mode);
3613 } else if (complement_values(or_right, a)) {
3614 /* (a|b) & ~b => a & ~b */
3615 dbg_info *dbgi = get_irn_dbg_info(n);
3616 ir_node *block = get_nodes_block(n);
3617 return new_rd_And(dbgi, block, or_left, a, mode);
3618 } else if (is_Not(a)) {
3619 ir_node *op = get_Not_op(a);
3621 ir_node *aa = get_And_left(op);
3622 ir_node *ab = get_And_right(op);
3624 /* it's enough to test the following cases due to normalization! */
3625 if (or_left == aa && or_right == ab) {
3626 /* (a|b) & ~(a&b) = a^b */
3627 ir_node *block = get_nodes_block(n);
3629 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3630 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3636 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3637 ir_node *al = get_binop_left(a);
3638 ir_node *ar = get_binop_right(a);
3641 /* (b ^ a) & b -> ~a & b */
3642 dbg_info *dbg = get_irn_dbg_info(n);
3643 ir_node *block = get_nodes_block(n);
3645 ar = new_rd_Not(dbg, block, ar, mode);
3646 n = new_rd_And(dbg, block, ar, b, mode);
3647 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3651 /* (a ^ b) & b -> ~a & b */
3652 dbg_info *dbg = get_irn_dbg_info(n);
3653 ir_node *block = get_nodes_block(n);
3655 al = new_rd_Not(dbg, block, al, mode);
3656 n = new_rd_And(dbg, block, al, b, mode);
3657 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3661 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3662 ir_node *bl = get_binop_left(b);
3663 ir_node *br = get_binop_right(b);
3666 /* a & (a ^ b) -> a & ~b */
3667 dbg_info *dbg = get_irn_dbg_info(n);
3668 ir_node *block = get_nodes_block(n);
3670 br = new_rd_Not(dbg, block, br, mode);
3671 n = new_rd_And(dbg, block, br, a, mode);
3672 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3676 /* a & (b ^ a) -> a & ~b */
3677 dbg_info *dbg = get_irn_dbg_info(n);
3678 ir_node *block = get_nodes_block(n);
3680 bl = new_rd_Not(dbg, block, bl, mode);
3681 n = new_rd_And(dbg, block, bl, a, mode);
3682 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3686 if (is_Not(a) && is_Not(b)) {
3687 /* ~a & ~b = ~(a|b) */
3688 ir_node *block = get_nodes_block(n);
3689 ir_mode *mode = get_irn_mode(n);
3693 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3694 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3695 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3700 vrp_attr *b_vrp = vrp_get_info(b);
3701 ir_tarval *a_val = get_Const_tarval(a);
3702 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3708 vrp_attr *a_vrp = vrp_get_info(a);
3709 ir_tarval *b_val = get_Const_tarval(b);
3710 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3715 n = transform_bitwise_distributive(n, transform_node_And);
3717 n = transform_node_bitop_shift(n);
3725 static ir_node *transform_node_Not(ir_node *n)
3727 ir_node *c, *oldn = n;
3728 ir_node *a = get_Not_op(n);
3729 ir_mode *mode = get_irn_mode(n);
3731 HANDLE_UNOP_PHI(tarval_not,a,c);
3733 /* check for a boolean Not */
3735 dbg_info *dbgi = get_irn_dbg_info(a);
3736 ir_node *block = get_nodes_block(a);
3737 ir_relation relation = get_Cmp_relation(a);
3738 relation = get_negated_relation(relation);
3739 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3740 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3744 /* normalize ~(a ^ b) => a ^ ~b */
3745 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3746 dbg_info *dbg = get_irn_dbg_info(n);
3747 ir_node *block = get_nodes_block(n);
3748 ir_node *eor_right = get_binop_right(a);
3749 ir_node *eor_left = get_binop_left(a);
3750 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3751 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3755 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3756 if (is_Minus(a)) { /* ~-x -> x + -1 */
3757 dbg_info *dbg = get_irn_dbg_info(n);
3758 ir_graph *irg = get_irn_irg(n);
3759 ir_node *block = get_nodes_block(n);
3760 ir_node *add_l = get_Minus_op(a);
3761 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3762 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3763 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3764 ir_node *add_r = get_binop_right(a);
3765 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3766 /* ~(x + -1) = -x */
3767 ir_node *op = get_binop_left(a);
3768 ir_node *blk = get_nodes_block(n);
3769 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3770 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3778 * Transform a Minus.
3782 * -(a >>u (size-1)) = a >>s (size-1)
3783 * -(a >>s (size-1)) = a >>u (size-1)
3784 * -(a * const) -> a * -const
3786 static ir_node *transform_node_Minus(ir_node *n)
3788 ir_node *c, *oldn = n;
3789 ir_node *a = get_Minus_op(n);
3792 HANDLE_UNOP_PHI(tarval_neg,a,c);
3794 mode = get_irn_mode(a);
3795 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3796 /* the following rules are only to twos-complement */
3799 ir_node *op = get_Not_op(a);
3800 ir_tarval *tv = get_mode_one(mode);
3801 ir_node *blk = get_nodes_block(n);
3802 ir_graph *irg = get_irn_irg(blk);
3803 ir_node *c = new_r_Const(irg, tv);
3804 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3805 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3809 ir_node *c = get_Shr_right(a);
3812 ir_tarval *tv = get_Const_tarval(c);
3814 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3815 /* -(a >>u (size-1)) = a >>s (size-1) */
3816 ir_node *v = get_Shr_left(a);
3818 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3819 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3825 ir_node *c = get_Shrs_right(a);
3828 ir_tarval *tv = get_Const_tarval(c);
3830 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3831 /* -(a >>s (size-1)) = a >>u (size-1) */
3832 ir_node *v = get_Shrs_left(a);
3834 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3835 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3842 /* - (a-b) = b - a */
3843 ir_node *la = get_Sub_left(a);
3844 ir_node *ra = get_Sub_right(a);
3845 ir_node *blk = get_nodes_block(n);
3847 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3848 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3852 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3853 ir_node *mul_l = get_Mul_left(a);
3854 ir_node *mul_r = get_Mul_right(a);
3855 ir_tarval *tv = value_of(mul_r);
3856 if (tv != tarval_bad) {
3857 tv = tarval_neg(tv);
3858 if (tv != tarval_bad) {
3859 ir_graph *irg = get_irn_irg(n);
3860 ir_node *cnst = new_r_Const(irg, tv);
3861 dbg_info *dbg = get_irn_dbg_info(a);
3862 ir_node *block = get_nodes_block(a);
3863 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3864 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3874 * Transform a Proj(Load) with a non-null address.
3876 static ir_node *transform_node_Proj_Load(ir_node *proj)
3878 if (get_irn_mode(proj) == mode_X) {
3879 ir_node *load = get_Proj_pred(proj);
3881 /* get the Load address */
3882 const ir_node *addr = get_Load_ptr(load);
3883 const ir_node *confirm;
3885 if (value_not_null(addr, &confirm)) {
3886 if (confirm == NULL) {
3887 /* this node may float if it did not depend on a Confirm */
3888 set_irn_pinned(load, op_pin_state_floats);
3890 if (get_Proj_proj(proj) == pn_Load_X_except) {
3891 ir_graph *irg = get_irn_irg(proj);
3892 DBG_OPT_EXC_REM(proj);
3893 return new_r_Bad(irg, mode_X);
3895 ir_node *blk = get_nodes_block(load);
3896 return new_r_Jmp(blk);
3904 * Transform a Proj(Store) with a non-null address.
3906 static ir_node *transform_node_Proj_Store(ir_node *proj)
3908 if (get_irn_mode(proj) == mode_X) {
3909 ir_node *store = get_Proj_pred(proj);
3911 /* get the load/store address */
3912 const ir_node *addr = get_Store_ptr(store);
3913 const ir_node *confirm;
3915 if (value_not_null(addr, &confirm)) {
3916 if (confirm == NULL) {
3917 /* this node may float if it did not depend on a Confirm */
3918 set_irn_pinned(store, op_pin_state_floats);
3920 if (get_Proj_proj(proj) == pn_Store_X_except) {
3921 ir_graph *irg = get_irn_irg(proj);
3922 DBG_OPT_EXC_REM(proj);
3923 return new_r_Bad(irg, mode_X);
3925 ir_node *blk = get_nodes_block(store);
3926 return new_r_Jmp(blk);
3934 * Transform a Proj(Div) with a non-zero value.
3935 * Removes the exceptions and routes the memory to the NoMem node.
3937 static ir_node *transform_node_Proj_Div(ir_node *proj)
3939 ir_node *div = get_Proj_pred(proj);
3940 ir_node *b = get_Div_right(div);
3941 ir_node *res, *new_mem;
3942 const ir_node *confirm;
3945 if (value_not_zero(b, &confirm)) {
3946 /* div(x, y) && y != 0 */
3947 if (confirm == NULL) {
3948 /* we are sure we have a Const != 0 */
3949 new_mem = get_Div_mem(div);
3950 new_mem = skip_Pin(new_mem);
3951 set_Div_mem(div, new_mem);
3952 set_irn_pinned(div, op_pin_state_floats);
3955 proj_nr = get_Proj_proj(proj);
3957 case pn_Div_X_regular:
3958 return new_r_Jmp(get_nodes_block(div));
3960 case pn_Div_X_except: {
3961 ir_graph *irg = get_irn_irg(proj);
3962 /* we found an exception handler, remove it */
3963 DBG_OPT_EXC_REM(proj);
3964 return new_r_Bad(irg, mode_X);
3968 ir_graph *irg = get_irn_irg(proj);
3969 res = get_Div_mem(div);
3970 new_mem = get_irg_no_mem(irg);
3973 /* This node can only float up to the Confirm block */
3974 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3976 set_irn_pinned(div, op_pin_state_floats);
3977 /* this is a Div without exception, we can remove the memory edge */
3978 set_Div_mem(div, new_mem);
3987 * Transform a Proj(Mod) with a non-zero value.
3988 * Removes the exceptions and routes the memory to the NoMem node.
3990 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3992 ir_node *mod = get_Proj_pred(proj);
3993 ir_node *b = get_Mod_right(mod);
3994 ir_node *res, *new_mem;
3995 const ir_node *confirm;
3998 if (value_not_zero(b, &confirm)) {
3999 /* mod(x, y) && y != 0 */
4000 proj_nr = get_Proj_proj(proj);
4002 if (confirm == NULL) {
4003 /* we are sure we have a Const != 0 */
4004 new_mem = get_Mod_mem(mod);
4005 new_mem = skip_Pin(new_mem);
4006 set_Mod_mem(mod, new_mem);
4007 set_irn_pinned(mod, op_pin_state_floats);
4012 case pn_Mod_X_regular:
4013 return new_r_Jmp(get_nodes_block(mod));
4015 case pn_Mod_X_except: {
4016 ir_graph *irg = get_irn_irg(proj);
4017 /* we found an exception handler, remove it */
4018 DBG_OPT_EXC_REM(proj);
4019 return new_r_Bad(irg, mode_X);
4023 ir_graph *irg = get_irn_irg(proj);
4024 res = get_Mod_mem(mod);
4025 new_mem = get_irg_no_mem(irg);
4028 /* This node can only float up to the Confirm block */
4029 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4031 /* this is a Mod without exception, we can remove the memory edge */
4032 set_Mod_mem(mod, new_mem);
4036 if (get_Mod_left(mod) == b) {
4037 /* a % a = 0 if a != 0 */
4038 ir_graph *irg = get_irn_irg(proj);
4039 ir_mode *mode = get_irn_mode(proj);
4040 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4042 DBG_OPT_CSTEVAL(mod, res);
4051 * return true if the operation returns a value with exactly 1 bit set
4053 static bool is_single_bit(const ir_node *node)
4055 /* a first implementation, could be extended with vrp and others... */
4057 ir_node *shl_l = get_Shl_left(node);
4058 ir_mode *mode = get_irn_mode(node);
4059 int modulo = get_mode_modulo_shift(mode);
4060 /* this works if we shift a 1 and we have modulo shift */
4061 if (is_Const(shl_l) && is_Const_one(shl_l)
4062 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4065 } else if (is_Const(node)) {
4066 ir_tarval *tv = get_Const_tarval(node);
4067 return tarval_is_single_bit(tv);
4073 * checks if node just flips a bit in another node and returns that other node
4074 * if so. @p tv should be a value having just 1 bit set
4076 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4079 return get_Not_op(node);
4081 ir_node *right = get_Eor_right(node);
4082 if (is_Const(right)) {
4083 ir_tarval *right_tv = get_Const_tarval(right);
4084 ir_mode *mode = get_irn_mode(node);
4085 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4086 return get_Eor_left(node);
4093 * Normalizes and optimizes Cmp nodes.
4095 static ir_node *transform_node_Cmp(ir_node *n)
4097 ir_node *left = get_Cmp_left(n);
4098 ir_node *right = get_Cmp_right(n);
4099 ir_mode *mode = get_irn_mode(left);
4100 ir_tarval *tv = NULL;
4101 bool changed = false;
4102 bool changedc = false;
4103 ir_relation relation = get_Cmp_relation(n);
4104 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4106 /* mask out impossible relations */
4107 ir_relation new_relation = relation & possible;
4108 if (new_relation != relation) {
4109 relation = new_relation;
4113 /* Remove unnecessary conversions */
4114 if (!mode_is_float(mode)
4115 || be_get_backend_param()->mode_float_arithmetic == NULL) {
4116 if (is_Conv(left) && is_Conv(right)) {
4117 ir_node *op_left = get_Conv_op(left);
4118 ir_node *op_right = get_Conv_op(right);
4119 ir_mode *mode_left = get_irn_mode(op_left);
4120 ir_mode *mode_right = get_irn_mode(op_right);
4122 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4123 && mode_left != mode_b && mode_right != mode_b) {
4124 ir_node *block = get_nodes_block(n);
4126 if (mode_left == mode_right) {
4130 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4131 } else if (smaller_mode(mode_left, mode_right)) {
4132 left = new_r_Conv(block, op_left, mode_right);
4135 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4136 } else if (smaller_mode(mode_right, mode_left)) {
4138 right = new_r_Conv(block, op_right, mode_left);
4140 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4142 mode = get_irn_mode(left);
4145 if (is_Conv(left) && is_Const(right)) {
4146 ir_node *op_left = get_Conv_op(left);
4147 ir_mode *mode_left = get_irn_mode(op_left);
4148 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4149 ir_tarval *tv = get_Const_tarval(right);
4150 tarval_int_overflow_mode_t last_mode
4151 = tarval_get_integer_overflow_mode();
4153 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4154 new_tv = tarval_convert_to(tv, mode_left);
4155 tarval_set_integer_overflow_mode(last_mode);
4156 if (new_tv != tarval_bad) {
4157 ir_graph *irg = get_irn_irg(n);
4159 right = new_r_Const(irg, new_tv);
4160 mode = get_irn_mode(left);
4162 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4169 * Optimize -a CMP -b into b CMP a.
4170 * This works only for modes where unary Minus cannot Overflow.
4171 * Note that two-complement integers can Overflow so it will NOT work.
4173 if (!mode_overflow_on_unary_Minus(mode) &&
4174 is_Minus(left) && is_Minus(right)) {
4175 left = get_Minus_op(left);
4176 right = get_Minus_op(right);
4177 relation = get_inversed_relation(relation);
4179 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4182 /* remove operation on both sides if possible */
4183 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4185 * The following operations are NOT safe for floating point operations, for instance
4186 * 1.0 + inf == 2.0 + inf, =/=> x == y
4188 if (mode_is_int(mode)) {
4189 unsigned lop = get_irn_opcode(left);
4191 if (lop == get_irn_opcode(right)) {
4192 ir_node *ll, *lr, *rl, *rr;
4194 /* same operation on both sides, try to remove */
4198 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4199 left = get_unop_op(left);
4200 right = get_unop_op(right);
4202 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4205 ll = get_Add_left(left);
4206 lr = get_Add_right(left);
4207 rl = get_Add_left(right);
4208 rr = get_Add_right(right);
4211 /* X + a CMP X + b ==> a CMP b */
4215 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4216 } else if (ll == rr) {
4217 /* X + a CMP b + X ==> a CMP b */
4221 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4222 } else if (lr == rl) {
4223 /* a + X CMP X + b ==> a CMP b */
4227 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4228 } else if (lr == rr) {
4229 /* a + X CMP b + X ==> a CMP b */
4233 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4237 ll = get_Sub_left(left);
4238 lr = get_Sub_right(left);
4239 rl = get_Sub_left(right);
4240 rr = get_Sub_right(right);
4243 /* X - a CMP X - b ==> a CMP b */
4247 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4248 } else if (lr == rr) {
4249 /* a - X CMP b - X ==> a CMP b */
4253 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4257 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4258 /* a ROTL X CMP b ROTL X ==> a CMP b */
4259 left = get_Rotl_left(left);
4260 right = get_Rotl_left(right);
4262 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4270 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4271 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4272 ir_node *ll = get_binop_left(left);
4273 ir_node *lr = get_binop_right(left);
4275 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4281 ir_graph *irg = get_irn_irg(n);
4283 right = create_zero_const(irg, mode);
4285 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4288 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4289 ir_node *rl = get_binop_left(right);
4290 ir_node *rr = get_binop_right(right);
4292 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4298 ir_graph *irg = get_irn_irg(n);
4300 right = create_zero_const(irg, mode);
4302 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4306 if (is_And(left) && is_Const(right)) {
4307 ir_node *ll = get_binop_left(left);
4308 ir_node *lr = get_binop_right(left);
4309 if (is_Shr(ll) && is_Const(lr)) {
4310 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4311 ir_node *block = get_nodes_block(n);
4312 ir_mode *mode = get_irn_mode(left);
4314 ir_node *llr = get_Shr_right(ll);
4315 if (is_Const(llr)) {
4316 dbg_info *dbg = get_irn_dbg_info(left);
4317 ir_graph *irg = get_irn_irg(left);
4319 ir_tarval *c1 = get_Const_tarval(llr);
4320 ir_tarval *c2 = get_Const_tarval(lr);
4321 ir_tarval *c3 = get_Const_tarval(right);
4322 ir_tarval *mask = tarval_shl(c2, c1);
4323 ir_tarval *value = tarval_shl(c3, c1);
4325 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4326 right = new_r_Const(irg, value);
4331 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4333 if (is_Const(right) && is_Const_null(right) &&
4334 (is_Eor(left) || is_Or_Eor_Add(left))) {
4335 right = get_Eor_right(left);
4336 left = get_Eor_left(left);
4342 if (mode_is_int(mode) && is_And(left)) {
4343 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4344 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4345 if (relation == ir_relation_equal
4346 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4347 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4348 ir_node *and0 = get_And_left(left);
4349 ir_node *and1 = get_And_right(left);
4350 if (and1 == right) {
4351 ir_node *tmp = and0;
4355 if (and0 == right && is_single_bit(and0)) {
4356 ir_graph *irg = get_irn_irg(n);
4358 relation == ir_relation_equal ? ir_relation_less_greater
4359 : ir_relation_equal;
4360 right = create_zero_const(irg, mode);
4366 if (is_Const(right) && is_Const_null(right) &&
4367 (relation == ir_relation_equal
4368 || (relation == ir_relation_less_greater)
4369 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4371 /* instead of flipping the bit before the bit-test operation negate
4373 ir_node *and0 = get_And_left(left);
4374 ir_node *and1 = get_And_right(left);
4375 if (is_Const(and1)) {
4376 ir_tarval *tv = get_Const_tarval(and1);
4377 if (tarval_is_single_bit(tv)) {
4378 ir_node *flipped = flips_bit(and0, tv);
4379 if (flipped != NULL) {
4380 dbg_info *dbgi = get_irn_dbg_info(left);
4381 ir_node *block = get_nodes_block(left);
4382 relation = get_negated_relation(relation);
4383 left = new_rd_And(dbgi, block, flipped, and1, mode);
4392 /* replace mode_b compares with ands/ors */
4393 if (mode == mode_b) {
4394 ir_node *block = get_nodes_block(n);
4398 case ir_relation_less_equal:
4399 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4401 case ir_relation_less:
4402 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4404 case ir_relation_greater_equal:
4405 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4407 case ir_relation_greater:
4408 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4410 case ir_relation_less_greater:
4411 bres = new_r_Eor(block, left, right, mode_b);
4413 case ir_relation_equal:
4414 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4417 #ifdef DEBUG_libfirm
4418 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4423 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4429 * First step: normalize the compare op
4430 * by placing the constant on the right side
4431 * or moving the lower address node to the left.
4433 if (!operands_are_normalized(left, right)) {
4438 relation = get_inversed_relation(relation);
4443 * Second step: Try to reduce the magnitude
4444 * of a constant. This may help to generate better code
4445 * later and may help to normalize more compares.
4446 * Of course this is only possible for integer values.
4448 tv = value_of(right);
4449 if (tv != tarval_bad) {
4450 ir_mode *mode = get_irn_mode(right);
4452 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4453 * cmp(ct,c2) | cmp(cf,c2) | result
4454 * -----------|------------|--------
4455 * true | true | True
4456 * false | false | False
4458 * false | true | not(x)
4461 ir_node *mux_true = get_Mux_true(left);
4462 ir_node *mux_false = get_Mux_false(left);
4463 if (is_Const(mux_true) && is_Const(mux_false)) {
4464 /* we can fold true/false constant separately */
4465 ir_tarval *tv_true = get_Const_tarval(mux_true);
4466 ir_tarval *tv_false = get_Const_tarval(mux_false);
4467 ir_relation r_true = tarval_cmp(tv_true, tv);
4468 ir_relation r_false = tarval_cmp(tv_false, tv);
4469 if (r_true != ir_relation_false
4470 || r_false != ir_relation_false) {
4471 bool rel_true = (r_true & relation) != 0;
4472 bool rel_false = (r_false & relation) != 0;
4473 ir_node *cond = get_Mux_sel(left);
4474 if (rel_true == rel_false) {
4475 relation = rel_true ? ir_relation_true
4476 : ir_relation_false;
4477 } else if (rel_true) {
4480 dbg_info *dbgi = get_irn_dbg_info(n);
4481 ir_node *block = get_nodes_block(n);
4482 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4489 /* TODO extend to arbitrary constants */
4490 if (is_Conv(left) && tarval_is_null(tv)) {
4491 ir_node *op = get_Conv_op(left);
4492 ir_mode *op_mode = get_irn_mode(op);
4495 * UpConv(x) REL 0 ==> x REL 0
4496 * Don't do this for float values as it's unclear whether it is a
4497 * win. (on the other side it makes detection/creation of fabs hard)
4499 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4500 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4501 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4502 !mode_is_float(mode)) {
4503 tv = get_mode_null(op_mode);
4507 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4511 if (tv != tarval_bad) {
4512 /* the following optimization is possible on modes without Overflow
4513 * on Unary Minus or on == and !=:
4514 * -a CMP c ==> a swap(CMP) -c
4516 * Beware: for two-complement Overflow may occur, so only == and != can
4517 * be optimized, see this:
4518 * -MININT < 0 =/=> MININT > 0 !!!
4520 if (is_Minus(left) &&
4521 (!mode_overflow_on_unary_Minus(mode) ||
4522 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4523 tv = tarval_neg(tv);
4525 if (tv != tarval_bad) {
4526 left = get_Minus_op(left);
4527 relation = get_inversed_relation(relation);
4529 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4531 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4532 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4533 tv = tarval_not(tv);
4535 if (tv != tarval_bad) {
4536 left = get_Not_op(left);
4538 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4542 /* for integer modes, we have more */
4543 if (mode_is_int(mode) && !is_Const(left)) {
4544 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4545 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4546 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4547 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4549 if (tv != tarval_bad) {
4550 relation ^= ir_relation_equal;
4552 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4555 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4556 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4557 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4558 tv = tarval_add(tv, get_mode_one(mode));
4560 if (tv != tarval_bad) {
4561 relation ^= ir_relation_equal;
4563 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4567 /* the following reassociations work only for == and != */
4568 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4569 if (tv != tarval_bad) {
4570 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4572 ir_node *c1 = get_Sub_right(left);
4573 ir_tarval *tv2 = value_of(c1);
4575 if (tv2 != tarval_bad) {
4576 tv2 = tarval_add(tv, value_of(c1));
4578 if (tv2 != tarval_bad) {
4579 left = get_Sub_left(left);
4582 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4586 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4587 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4588 ir_node *a_l = get_binop_left(left);
4589 ir_node *a_r = get_binop_right(left);
4593 if (is_Const(a_l)) {
4595 tv2 = value_of(a_l);
4598 tv2 = value_of(a_r);
4601 if (tv2 != tarval_bad) {
4602 tv2 = tarval_sub(tv, tv2, NULL);
4604 if (tv2 != tarval_bad) {
4608 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4612 /* -a == c ==> a == -c, -a != c ==> a != -c */
4613 else if (is_Minus(left)) {
4614 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4616 if (tv2 != tarval_bad) {
4617 left = get_Minus_op(left);
4620 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4627 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4628 switch (get_irn_opcode(left)) {
4632 c1 = get_And_right(left);
4635 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4636 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4638 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4640 /* TODO: move to constant evaluation */
4641 ir_graph *irg = get_irn_irg(n);
4642 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4643 c1 = new_r_Const(irg, tv);
4644 DBG_OPT_CSTEVAL(n, c1);
4648 if (tarval_is_single_bit(tv)) {
4650 * optimization for AND:
4652 * And(x, C) == C ==> And(x, C) != 0
4653 * And(x, C) != C ==> And(X, C) == 0
4655 * if C is a single Bit constant.
4658 /* check for Constant's match. We have check hare the tarvals,
4659 because our const might be changed */
4660 if (get_Const_tarval(c1) == tv) {
4661 /* fine: do the transformation */
4662 tv = get_mode_null(get_tarval_mode(tv));
4663 relation ^= ir_relation_less_equal_greater;
4665 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4671 c1 = get_Or_right(left);
4672 if (is_Const(c1) && tarval_is_null(tv)) {
4674 * Or(x, C) == 0 && C != 0 ==> FALSE
4675 * Or(x, C) != 0 && C != 0 ==> TRUE
4677 if (! tarval_is_null(get_Const_tarval(c1))) {
4678 /* TODO: move to constant evaluation */
4679 ir_graph *irg = get_irn_irg(n);
4680 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4681 c1 = new_r_Const(irg, tv);
4682 DBG_OPT_CSTEVAL(n, c1);
4689 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4691 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4694 c1 = get_Shl_right(left);
4696 ir_graph *irg = get_irn_irg(c1);
4697 ir_tarval *tv1 = get_Const_tarval(c1);
4698 ir_mode *mode = get_irn_mode(left);
4699 ir_tarval *minus1 = get_mode_all_one(mode);
4700 ir_tarval *amask = tarval_shr(minus1, tv1);
4701 ir_tarval *cmask = tarval_shl(minus1, tv1);
4704 if (tarval_and(tv, cmask) != tv) {
4705 /* condition not met */
4706 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4707 c1 = new_r_Const(irg, tv);
4708 DBG_OPT_CSTEVAL(n, c1);
4711 sl = get_Shl_left(left);
4712 blk = get_nodes_block(n);
4713 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4714 tv = tarval_shr(tv, tv1);
4716 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4721 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4723 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4726 c1 = get_Shr_right(left);
4728 ir_graph *irg = get_irn_irg(c1);
4729 ir_tarval *tv1 = get_Const_tarval(c1);
4730 ir_mode *mode = get_irn_mode(left);
4731 ir_tarval *minus1 = get_mode_all_one(mode);
4732 ir_tarval *amask = tarval_shl(minus1, tv1);
4733 ir_tarval *cmask = tarval_shr(minus1, tv1);
4736 if (tarval_and(tv, cmask) != tv) {
4737 /* condition not met */
4738 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4739 c1 = new_r_Const(irg, tv);
4740 DBG_OPT_CSTEVAL(n, c1);
4743 sl = get_Shr_left(left);
4744 blk = get_nodes_block(n);
4745 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4746 tv = tarval_shl(tv, tv1);
4748 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4753 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4755 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4758 c1 = get_Shrs_right(left);
4760 ir_graph *irg = get_irn_irg(c1);
4761 ir_tarval *tv1 = get_Const_tarval(c1);
4762 ir_mode *mode = get_irn_mode(left);
4763 ir_tarval *minus1 = get_mode_all_one(mode);
4764 ir_tarval *amask = tarval_shl(minus1, tv1);
4765 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4768 cond = tarval_sub(cond, tv1, NULL);
4769 cond = tarval_shrs(tv, cond);
4771 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4772 /* condition not met */
4773 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4774 c1 = new_r_Const(irg, tv);
4775 DBG_OPT_CSTEVAL(n, c1);
4778 sl = get_Shrs_left(left);
4779 blk = get_nodes_block(n);
4780 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4781 tv = tarval_shl(tv, tv1);
4783 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4791 if (changedc) { /* need a new Const */
4792 ir_graph *irg = get_irn_irg(n);
4793 right = new_r_Const(irg, tv);
4797 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4798 ir_node *op = get_Proj_pred(left);
4800 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4801 ir_node *c = get_binop_right(op);
4804 ir_tarval *tv = get_Const_tarval(c);
4806 if (tarval_is_single_bit(tv)) {
4807 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4808 ir_node *v = get_binop_left(op);
4809 ir_node *blk = get_nodes_block(op);
4810 ir_graph *irg = get_irn_irg(op);
4811 ir_mode *mode = get_irn_mode(v);
4813 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4814 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4816 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4823 dbg_info *dbgi = get_irn_dbg_info(n);
4824 ir_node *block = get_nodes_block(n);
4826 /* create a new compare */
4827 n = new_rd_Cmp(dbgi, block, left, right, relation);
4834 * Optimize CopyB(mem, x, x) into a Nop.
4836 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4838 ir_node *copyb = get_Proj_pred(proj);
4839 ir_node *a = get_CopyB_dst(copyb);
4840 ir_node *b = get_CopyB_src(copyb);
4843 switch (get_Proj_proj(proj)) {
4844 case pn_CopyB_X_regular:
4845 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4846 DBG_OPT_EXC_REM(proj);
4847 proj = new_r_Jmp(get_nodes_block(copyb));
4849 case pn_CopyB_X_except: {
4850 ir_graph *irg = get_irn_irg(proj);
4851 DBG_OPT_EXC_REM(proj);
4852 proj = new_r_Bad(irg, mode_X);
4863 * Optimize Bounds(idx, idx, upper) into idx.
4865 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4867 ir_node *oldn = proj;
4868 ir_node *bound = get_Proj_pred(proj);
4869 ir_node *idx = get_Bound_index(bound);
4870 ir_node *pred = skip_Proj(idx);
4873 if (idx == get_Bound_lower(bound))
4875 else if (is_Bound(pred)) {
4877 * idx was Bounds checked previously, it is still valid if
4878 * lower <= pred_lower && pred_upper <= upper.
4880 ir_node *lower = get_Bound_lower(bound);
4881 ir_node *upper = get_Bound_upper(bound);
4882 if (get_Bound_lower(pred) == lower &&
4883 get_Bound_upper(pred) == upper) {
4885 * One could expect that we simply return the previous
4886 * Bound here. However, this would be wrong, as we could
4887 * add an exception Proj to a new location then.
4888 * So, we must turn in into a tuple.
4894 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4895 switch (get_Proj_proj(proj)) {
4897 DBG_OPT_EXC_REM(proj);
4898 proj = get_Bound_mem(bound);
4900 case pn_Bound_X_except:
4901 DBG_OPT_EXC_REM(proj);
4902 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4906 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4908 case pn_Bound_X_regular:
4909 DBG_OPT_EXC_REM(proj);
4910 proj = new_r_Jmp(get_nodes_block(bound));
4920 * Does all optimizations on nodes that must be done on its Projs
4921 * because of creating new nodes.
4923 static ir_node *transform_node_Proj(ir_node *proj)
4925 ir_node *n = get_Proj_pred(proj);
4927 if (n->op->ops.transform_node_Proj)
4928 return n->op->ops.transform_node_Proj(proj);
4933 * Test whether a block is unreachable
4934 * Note: That this only returns true when
4935 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4936 * This is important, as you easily end up producing invalid constructs in the
4937 * unreachable code when optimizing away edges into the unreachable code.
4938 * So only set this flag when you iterate localopts to the fixpoint.
4939 * When you reach the fixpoint then all unreachable code is dead
4940 * (= can't be reached by firm edges) and you won't see the invalid constructs
4943 static bool is_block_unreachable(const ir_node *block)
4945 const ir_graph *irg = get_irn_irg(block);
4946 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4948 return get_Block_dom_depth(block) < 0;
4951 static ir_node *transform_node_Block(ir_node *block)
4953 ir_graph *irg = get_irn_irg(block);
4954 int arity = get_irn_arity(block);
4955 ir_node *bad = NULL;
4958 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4961 for (i = 0; i < arity; ++i) {
4962 ir_node *const pred = get_Block_cfgpred(block, i);
4963 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4966 bad = new_r_Bad(irg, mode_X);
4967 set_irn_n(block, i, bad);
4973 static ir_node *transform_node_Phi(ir_node *phi)
4975 int n = get_irn_arity(phi);
4976 ir_mode *mode = get_irn_mode(phi);
4977 ir_node *block = get_nodes_block(phi);
4978 ir_graph *irg = get_irn_irg(phi);
4979 ir_node *bad = NULL;
4982 /* Set phi-operands for bad-block inputs to bad */
4983 for (i = 0; i < n; ++i) {
4984 if (!is_Bad(get_Phi_pred(phi, i))) {
4985 ir_node *pred = get_Block_cfgpred(block, i);
4986 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4988 bad = new_r_Bad(irg, mode);
4989 set_irn_n(phi, i, bad);
4994 /* Move Pin nodes down through Phi nodes. */
4995 if (mode == mode_M) {
4996 n = get_irn_arity(phi);
4998 /* Beware of Phi0 */
5002 bool has_pin = false;
5004 NEW_ARR_A(ir_node *, in, n);
5006 for (i = 0; i < n; ++i) {
5007 ir_node *pred = get_irn_n(phi, i);
5010 in[i] = get_Pin_op(pred);
5012 } else if (is_Bad(pred)) {
5022 /* Move the Pin nodes "behind" the Phi. */
5023 new_phi = new_r_Phi(block, n, in, mode_M);
5024 return new_r_Pin(block, new_phi);
5027 /* Move Confirms down through Phi nodes. */
5028 else if (mode_is_reference(mode)) {
5029 n = get_irn_arity(phi);
5031 /* Beware of Phi0 */
5033 ir_node *pred = get_irn_n(phi, 0);
5034 ir_node *bound, *new_phi, **in;
5035 ir_relation relation;
5036 bool has_confirm = false;
5038 if (! is_Confirm(pred))
5041 bound = get_Confirm_bound(pred);
5042 relation = get_Confirm_relation(pred);
5044 NEW_ARR_A(ir_node *, in, n);
5045 in[0] = get_Confirm_value(pred);
5047 for (i = 1; i < n; ++i) {
5048 pred = get_irn_n(phi, i);
5050 if (is_Confirm(pred) &&
5051 get_Confirm_bound(pred) == bound &&
5052 get_Confirm_relation(pred) == relation) {
5053 in[i] = get_Confirm_value(pred);
5055 } else if (is_Bad(pred)) {
5065 /* move the Confirm nodes "behind" the Phi */
5066 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5067 return new_r_Confirm(block, new_phi, bound, relation);
5074 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5076 * Should be moved to reassociation?
5078 static ir_node *transform_node_shift(ir_node *n)
5080 ir_node *left, *right;
5082 ir_mode *count_mode;
5083 ir_tarval *tv1, *tv2, *res;
5084 ir_node *in[2], *irn, *block;
5088 left = get_binop_left(n);
5090 /* different operations */
5091 if (get_irn_op(left) != get_irn_op(n))
5094 right = get_binop_right(n);
5095 tv1 = value_of(right);
5096 if (tv1 == tarval_bad)
5099 tv2 = value_of(get_binop_right(left));
5100 if (tv2 == tarval_bad)
5103 count_mode = get_tarval_mode(tv1);
5104 if (get_tarval_mode(tv2) != count_mode) {
5105 /* TODO: search bigger mode or something and convert... */
5109 mode = get_irn_mode(n);
5110 modulo_shf = get_mode_modulo_shift(mode);
5112 if (modulo_shf > 0) {
5113 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5115 /* I'm not so sure what happens in one complement... */
5116 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5117 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5118 * above will be invalid) */
5119 assert(modulo_shf<=0 || is_po2(modulo_shf));
5121 tv1 = tarval_and(tv1, modulo_mask);
5122 tv2 = tarval_and(tv2, modulo_mask);
5124 res = tarval_add(tv1, tv2);
5125 irg = get_irn_irg(n);
5127 /* beware: a simple replacement works only, if res < modulo shift */
5129 int bits = get_mode_size_bits(mode);
5130 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5131 res = tarval_mod(res, modulo);
5133 long bits = get_mode_size_bits(mode);
5134 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5136 /* shifting too much */
5137 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5139 ir_node *block = get_nodes_block(n);
5140 dbg_info *dbgi = get_irn_dbg_info(n);
5141 ir_mode *smode = get_irn_mode(right);
5142 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5143 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5146 return new_r_Const(irg, get_mode_null(mode));
5150 /* ok, we can replace it */
5151 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5152 block = get_nodes_block(n);
5154 in[0] = get_binop_left(left);
5155 in[1] = new_r_Const(irg, res);
5157 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5159 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5166 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5168 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5169 * (also with x >>s c1 when c1>=c2)
5171 static ir_node *transform_node_shl_shr(ir_node *n)
5174 ir_node *right = get_binop_right(n);
5184 ir_tarval *tv_shift;
5187 ir_relation relation;
5190 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5192 if (!is_Const(right))
5195 left = get_binop_left(n);
5196 mode = get_irn_mode(n);
5197 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5198 ir_node *shr_right = get_binop_right(left);
5200 if (!is_Const(shr_right))
5203 x = get_binop_left(left);
5204 tv_shr = get_Const_tarval(shr_right);
5205 tv_shl = get_Const_tarval(right);
5207 if (is_Shrs(left)) {
5208 /* shrs variant only allowed if c1 >= c2 */
5209 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5212 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5215 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5217 tv_mask = tarval_shl(tv_mask, tv_shl);
5218 } else if (is_Shr(n) && is_Shl(left)) {
5219 ir_node *shl_right = get_Shl_right(left);
5221 if (!is_Const(shl_right))
5224 x = get_Shl_left(left);
5225 tv_shr = get_Const_tarval(right);
5226 tv_shl = get_Const_tarval(shl_right);
5228 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5229 tv_mask = tarval_shr(tv_mask, tv_shr);
5234 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5235 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5238 assert(tv_mask != tarval_bad);
5239 assert(get_tarval_mode(tv_mask) == mode);
5241 block = get_nodes_block(n);
5242 irg = get_irn_irg(block);
5243 dbgi = get_irn_dbg_info(n);
5245 relation = tarval_cmp(tv_shl, tv_shr);
5246 if (relation == ir_relation_less || relation == ir_relation_equal) {
5247 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5248 new_const = new_r_Const(irg, tv_shift);
5250 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5252 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5255 assert(relation == ir_relation_greater);
5256 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5257 new_const = new_r_Const(irg, tv_shift);
5258 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5261 new_const = new_r_Const(irg, tv_mask);
5262 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5267 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5269 ir_mode *mode = get_tarval_mode(tv);
5270 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5271 return tarval_mod(tv, modulo_tv);
5274 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5275 ir_node *left, ir_node *right, ir_mode *mode);
5278 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5279 * then we can use that to minimize the value of Add(x, const) or
5280 * Sub(Const, x). In particular this often avoids 1 instruction in some
5281 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5282 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5284 static ir_node *transform_node_shift_modulo(ir_node *n,
5285 new_shift_func new_shift)
5287 ir_mode *mode = get_irn_mode(n);
5288 int modulo = get_mode_modulo_shift(mode);
5289 ir_node *newop = NULL;
5290 ir_mode *mode_right;
5297 if (get_mode_arithmetic(mode) != irma_twos_complement)
5299 if (!is_po2(modulo))
5302 irg = get_irn_irg(n);
5303 block = get_nodes_block(n);
5304 right = get_binop_right(n);
5305 mode_right = get_irn_mode(right);
5306 if (is_Const(right)) {
5307 ir_tarval *tv = get_Const_tarval(right);
5308 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5313 newop = new_r_Const(irg, tv_mod);
5314 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5315 ir_node *add_right = get_binop_right(right);
5316 if (is_Const(add_right)) {
5317 ir_tarval *tv = get_Const_tarval(add_right);
5318 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5323 newconst = new_r_Const(irg, tv_mod);
5324 newop = new_r_Add(block, get_binop_left(right), newconst,
5327 } else if (is_Sub(right)) {
5328 ir_node *sub_left = get_Sub_left(right);
5329 if (is_Const(sub_left)) {
5330 ir_tarval *tv = get_Const_tarval(sub_left);
5331 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5336 newconst = new_r_Const(irg, tv_mod);
5337 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5344 if (newop != NULL) {
5345 dbg_info *dbgi = get_irn_dbg_info(n);
5346 ir_node *left = get_binop_left(n);
5347 return new_shift(dbgi, block, left, newop, mode);
5355 static ir_node *transform_node_Shr(ir_node *n)
5357 ir_node *c, *oldn = n;
5358 ir_node *left = get_Shr_left(n);
5359 ir_node *right = get_Shr_right(n);
5360 ir_mode *mode = get_irn_mode(n);
5362 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5363 n = transform_node_shift(n);
5366 n = transform_node_shift_modulo(n, new_rd_Shr);
5368 n = transform_node_shl_shr(n);
5370 n = transform_node_shift_bitop(n);
5378 static ir_node *transform_node_Shrs(ir_node *n)
5381 ir_node *a = get_Shrs_left(n);
5382 ir_node *b = get_Shrs_right(n);
5383 ir_mode *mode = get_irn_mode(n);
5387 if (is_oversize_shift(n)) {
5388 ir_node *block = get_nodes_block(n);
5389 dbg_info *dbgi = get_irn_dbg_info(n);
5390 ir_mode *cmode = get_irn_mode(b);
5391 long val = get_mode_size_bits(cmode)-1;
5392 ir_graph *irg = get_irn_irg(n);
5393 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5394 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5397 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5398 n = transform_node_shift(n);
5402 n = transform_node_shift_modulo(n, new_rd_Shrs);
5405 n = transform_node_shift_bitop(n);
5409 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5410 attr = vrp_get_info(a);
5412 unsigned bits = get_mode_size_bits(mode);
5413 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5414 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5415 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5416 dbg_info *dbgi = get_irn_dbg_info(n);
5417 ir_node *block = get_nodes_block(n);
5418 return new_rd_Shr(dbgi, block, a, b, mode);
5428 static ir_node *transform_node_Shl(ir_node *n)
5430 ir_node *c, *oldn = n;
5431 ir_node *a = get_Shl_left(n);
5432 ir_node *b = get_Shl_right(n);
5433 ir_mode *mode = get_irn_mode(n);
5435 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5436 n = transform_node_shift(n);
5439 n = transform_node_shift_modulo(n, new_rd_Shl);
5441 n = transform_node_shl_shr(n);
5443 n = transform_node_shift_bitop(n);
5451 static ir_node *transform_node_Rotl(ir_node *n)
5453 ir_node *c, *oldn = n;
5454 ir_node *a = get_Rotl_left(n);
5455 ir_node *b = get_Rotl_right(n);
5456 ir_mode *mode = get_irn_mode(n);
5458 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5459 n = transform_node_shift(n);
5462 n = transform_node_shift_bitop(n);
5468 * returns mode size for may_leave_out_middle_mode
5470 static unsigned get_significand_size(ir_mode *mode)
5472 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5473 switch (arithmetic) {
5475 case irma_x86_extended_float:
5476 return get_mode_mantissa_size(mode) + 1;
5477 case irma_twos_complement:
5478 return get_mode_size_bits(mode);
5480 panic("Conv node with irma_none mode?");
5482 panic("unexpected mode_arithmetic in get_significand_size");
5486 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5487 * as converting from @p m0 to @p m1 and then to @p m2.
5488 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5489 * gives the following truth table:
5490 * s -> b -> m : true
5491 * s -> m -> b : !signed(s) || signed(m)
5492 * m -> b -> s : true
5493 * m -> s -> b : false
5494 * b -> s -> m : false
5495 * b -> m -> s : true
5497 * s -> b -> b : true
5498 * s -> s -> b : false
5500 * additional float constraints:
5502 * F -> I -> I: signedness of Is must match
5503 * I -> F -> I: signedness of Is must match
5504 * I -> I -> F: signedness of Is must match
5508 * at least 1 float involved: signedness must match
5510 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5512 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5513 if (n_floats == 1) {
5515 int n_signed = mode_is_signed(m0) + mode_is_signed(m1)
5516 + mode_is_signed(m2);
5517 /* we assume that float modes are always signed */
5518 if ((n_signed & 1) != 1)
5521 /* because overflow gives strange results we don't touch this case */
5524 } else if (n_floats == 2 && !mode_is_float(m1)) {
5528 unsigned size0 = get_significand_size(m0);
5529 unsigned size1 = get_significand_size(m1);
5530 unsigned size2 = get_significand_size(m2);
5531 if (size1 < size2 && size0 >= size1)
5535 return !mode_is_signed(m0) || mode_is_signed(m1);
5541 static ir_node *transform_node_Conv(ir_node *n)
5543 ir_node *c, *oldn = n;
5544 ir_mode *mode = get_irn_mode(n);
5545 ir_node *a = get_Conv_op(n);
5548 ir_mode *a_mode = get_irn_mode(a);
5549 ir_node *b = get_Conv_op(a);
5550 ir_mode *b_mode = get_irn_mode(b);
5551 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5552 dbg_info *dbgi = get_irn_dbg_info(n);
5553 ir_node *block = get_nodes_block(n);
5554 return new_rd_Conv(dbgi, block, b, mode);
5558 if (mode != mode_b && is_const_Phi(a)) {
5559 /* Do NOT optimize mode_b Conv's, this leads to remaining
5560 * Phib nodes later, because the conv_b_lower operation
5561 * is instantly reverted, when it tries to insert a Convb.
5563 c = apply_conv_on_phi(a, mode);
5565 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5570 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5571 ir_graph *irg = get_irn_irg(n);
5572 return new_r_Unknown(irg, mode);
5575 if (mode_is_reference(mode) &&
5576 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5578 ir_node *l = get_Add_left(a);
5579 ir_node *r = get_Add_right(a);
5580 dbg_info *dbgi = get_irn_dbg_info(a);
5581 ir_node *block = get_nodes_block(n);
5583 ir_node *lop = get_Conv_op(l);
5584 if (get_irn_mode(lop) == mode) {
5585 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5586 n = new_rd_Add(dbgi, block, lop, r, mode);
5591 ir_node *rop = get_Conv_op(r);
5592 if (get_irn_mode(rop) == mode) {
5593 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5594 n = new_rd_Add(dbgi, block, l, rop, mode);
5600 /* shrink mode of load if possible. */
5602 ir_node *pred = get_Proj_pred(a);
5603 if (is_Load(pred)) {
5604 /* only do it if we are the only user (otherwise the risk is too
5605 * great that we end up with 2 loads instead of one). */
5606 ir_graph *irg = get_irn_irg(n);
5607 if (edges_activated(irg) && get_irn_n_edges(a) == 1) {
5608 ir_mode *load_mode = get_Load_mode(pred);
5609 if (!mode_is_float(load_mode) && !mode_is_float(mode) &&
5610 get_mode_size_bits(mode) <= get_mode_size_bits(load_mode)
5611 && !be_get_backend_param()->byte_order_big_endian) {
5612 set_Load_mode(pred, mode);
5613 set_irn_mode(a, mode);
5624 * Remove dead blocks and nodes in dead blocks
5625 * in keep alive list. We do not generate a new End node.
5627 static ir_node *transform_node_End(ir_node *n)
5629 int i, j, n_keepalives = get_End_n_keepalives(n);
5632 NEW_ARR_A(ir_node *, in, n_keepalives);
5634 for (i = j = 0; i < n_keepalives; ++i) {
5635 ir_node *ka = get_End_keepalive(n, i);
5637 /* no need to keep Bad */
5640 /* do not keep unreachable code */
5641 block = is_Block(ka) ? ka : get_nodes_block(ka);
5642 if (is_block_unreachable(block))
5646 if (j != n_keepalives)
5647 set_End_keepalives(n, j, in);
5651 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5653 if (is_Minus(a) && get_Minus_op(a) == b)
5655 if (is_Minus(b) && get_Minus_op(b) == a)
5657 if (is_Sub(a) && is_Sub(b)) {
5658 ir_node *a_left = get_Sub_left(a);
5659 ir_node *a_right = get_Sub_right(a);
5660 ir_node *b_left = get_Sub_left(b);
5661 ir_node *b_right = get_Sub_right(b);
5663 if (a_left == b_right && a_right == b_left)
5670 static const ir_node *skip_upconv(const ir_node *node)
5672 while (is_Conv(node)) {
5673 ir_mode *mode = get_irn_mode(node);
5674 const ir_node *op = get_Conv_op(node);
5675 ir_mode *op_mode = get_irn_mode(op);
5676 if (!smaller_mode(op_mode, mode))
5683 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5684 const ir_node *mux_true)
5689 ir_relation relation;
5695 * Note further that these optimization work even for floating point
5696 * with NaN's because -NaN == NaN.
5697 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5700 mode = get_irn_mode(mux_true);
5701 if (mode_honor_signed_zeros(mode))
5704 /* must be <, <=, >=, > */
5705 relation = get_Cmp_relation(sel);
5706 if ((relation & ir_relation_less_greater) == 0)
5709 if (!ir_is_negated_value(mux_true, mux_false))
5712 mux_true = skip_upconv(mux_true);
5713 mux_false = skip_upconv(mux_false);
5715 /* must be x cmp 0 */
5716 cmp_right = get_Cmp_right(sel);
5717 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5720 cmp_left = get_Cmp_left(sel);
5721 if (cmp_left == mux_false) {
5722 if (relation & ir_relation_less) {
5725 assert(relation & ir_relation_greater);
5728 } else if (cmp_left == mux_true) {
5729 if (relation & ir_relation_less) {
5732 assert(relation & ir_relation_greater);
5740 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5743 ir_node *cmp_left = get_Cmp_left(sel);
5744 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5747 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5748 const ir_node *mux_true)
5750 /* this code should return true each time transform_node_Mux would
5751 * optimize the Mux completely away */
5753 ir_mode *mode = get_irn_mode(mux_false);
5754 if (get_mode_arithmetic(mode) == irma_twos_complement
5755 && ir_mux_is_abs(sel, mux_false, mux_true))
5758 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5759 const ir_node *cmp_r = get_Cmp_right(sel);
5760 const ir_node *cmp_l = get_Cmp_left(sel);
5761 const ir_node *f = mux_false;
5762 const ir_node *t = mux_true;
5764 if (is_Const(t) && is_Const_null(t)) {
5769 if (is_And(cmp_l) && f == cmp_r) {
5770 ir_node *and_r = get_And_right(cmp_l);
5773 if (and_r == t && is_single_bit(and_r))
5775 and_l = get_And_left(cmp_l);
5776 if (and_l == t && is_single_bit(and_l))
5785 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5787 static ir_node *transform_Mux_set(ir_node *n)
5789 ir_node *cond = get_Mux_sel(n);
5794 ir_relation relation;
5808 left = get_Cmp_left(cond);
5809 mode = get_irn_mode(left);
5810 if (!mode_is_int(mode) && !mode_is_reference(mode))
5812 dest_mode = get_irn_mode(n);
5813 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5815 right = get_Cmp_right(cond);
5816 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5817 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5818 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5819 && relation != ir_relation_greater))
5824 case ir_relation_less:
5825 /* a < b -> (a - b) >> 31 */
5829 case ir_relation_less_equal:
5830 /* a <= b -> ~(a - b) >> 31 */
5835 case ir_relation_greater:
5836 /* a > b -> (b - a) >> 31 */
5840 case ir_relation_greater_equal:
5841 /* a >= b -> ~(a - b) >> 31 */
5850 dbgi = get_irn_dbg_info(n);
5851 block = get_nodes_block(n);
5852 irg = get_irn_irg(block);
5853 bits = get_mode_size_bits(dest_mode);
5854 tv = new_tarval_from_long(bits-1, mode_Iu);
5855 shift_cnt = new_rd_Const(dbgi, irg, tv);
5857 if (mode != dest_mode) {
5858 a = new_rd_Conv(dbgi, block, a, dest_mode);
5859 b = new_rd_Conv(dbgi, block, b, dest_mode);
5862 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5864 res = new_rd_Not(dbgi, block, res, dest_mode);
5866 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5871 * Optimize a Mux into some simpler cases.
5873 static ir_node *transform_node_Mux(ir_node *n)
5876 ir_node *sel = get_Mux_sel(n);
5877 ir_mode *mode = get_irn_mode(n);
5878 ir_node *t = get_Mux_true(n);
5879 ir_node *f = get_Mux_false(n);
5880 ir_graph *irg = get_irn_irg(n);
5882 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5883 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5884 int abs = ir_mux_is_abs(sel, f, t);
5886 dbg_info *dbgi = get_irn_dbg_info(n);
5887 ir_node *block = get_nodes_block(n);
5888 ir_node *op = ir_get_abs_op(sel, f, t);
5889 int bits = get_mode_size_bits(mode);
5890 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5891 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5892 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5895 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5897 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5903 /* first normalization step: try to move a constant to the false side,
5904 * 0 preferred on false side too */
5905 if (is_Cmp(sel) && is_Const(t) &&
5906 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5907 dbg_info *seldbgi = get_irn_dbg_info(sel);
5908 ir_node *block = get_nodes_block(sel);
5909 ir_relation relation = get_Cmp_relation(sel);
5914 /* Mux(x, a, b) => Mux(not(x), b, a) */
5915 relation = get_negated_relation(relation);
5916 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5917 get_Cmp_right(sel), relation);
5918 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5921 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5922 n = transform_Mux_set(n);
5927 /* the following optimisations create new mode_b nodes, so only do them
5928 * before mode_b lowering */
5929 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5931 ir_node* block = get_nodes_block(n);
5933 ir_node* c1 = get_Mux_sel(t);
5934 ir_node* t1 = get_Mux_true(t);
5935 ir_node* f1 = get_Mux_false(t);
5937 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5938 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5939 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5940 return new_r_Mux(block, and_, f1, t1, mode);
5941 } else if (f == t1) {
5942 /* Mux(cond0, Mux(cond1, x, y), x) */
5943 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5944 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5945 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5946 return new_r_Mux(block, and_, t1, f1, mode);
5948 } else if (is_Mux(f)) {
5949 ir_node* block = get_nodes_block(n);
5951 ir_node* c1 = get_Mux_sel(f);
5952 ir_node* t1 = get_Mux_true(f);
5953 ir_node* f1 = get_Mux_false(f);
5955 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5956 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5957 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5958 return new_r_Mux(block, or_, f1, t1, mode);
5959 } else if (t == f1) {
5960 /* Mux(cond0, x, Mux(cond1, y, x)) */
5961 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5962 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5963 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5964 return new_r_Mux(block, or_, t1, f1, mode);
5968 /* note: after normalization, false can only happen on default */
5969 if (mode == mode_b) {
5970 dbg_info *dbg = get_irn_dbg_info(n);
5971 ir_node *block = get_nodes_block(n);
5974 ir_tarval *tv_t = get_Const_tarval(t);
5975 if (tv_t == tarval_b_true) {
5977 /* Muxb(sel, true, false) = sel */
5978 assert(get_Const_tarval(f) == tarval_b_false);
5979 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5982 /* Muxb(sel, true, x) = Or(sel, x) */
5983 n = new_rd_Or(dbg, block, sel, f, mode_b);
5984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5988 } else if (is_Const(f)) {
5989 ir_tarval *tv_f = get_Const_tarval(f);
5990 if (tv_f == tarval_b_true) {
5991 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5992 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5993 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5994 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5997 /* Muxb(sel, x, false) = And(sel, x) */
5998 assert(tv_f == tarval_b_false);
5999 n = new_rd_And(dbg, block, sel, t, mode_b);
6000 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6007 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6008 ir_relation relation = get_Cmp_relation(sel);
6009 ir_node *cmp_r = get_Cmp_right(sel);
6010 ir_node *cmp_l = get_Cmp_left(sel);
6011 ir_node *block = get_nodes_block(n);
6013 if (is_And(cmp_l) && f == cmp_r) {
6014 ir_node *and_r = get_And_right(cmp_l);
6017 if (and_r == t && is_single_bit(and_r)) {
6018 if (relation == ir_relation_equal) {
6019 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6020 n = new_rd_Eor(get_irn_dbg_info(n),
6021 block, cmp_l, t, mode);
6022 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6024 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6026 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6030 and_l = get_And_left(cmp_l);
6031 if (and_l == t && is_single_bit(and_l)) {
6032 if (relation == ir_relation_equal) {
6033 /* ((1 << n) & a) == 0, (1 << n), 0) */
6034 n = new_rd_Eor(get_irn_dbg_info(n),
6035 block, cmp_l, t, mode);
6036 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6038 /* ((1 << n) & a) != 0, (1 << n), 0) */
6040 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6051 * optimize Sync nodes that have other syncs as input we simply add the inputs
6052 * of the other sync to our own inputs
6054 static ir_node *transform_node_Sync(ir_node *n)
6056 int arity = get_Sync_n_preds(n);
6059 for (i = 0; i < arity;) {
6060 ir_node *pred = get_Sync_pred(n, i);
6064 /* Remove Bad predecessors */
6071 /* Remove duplicate predecessors */
6072 for (j = 0; j < i; ++j) {
6073 if (get_Sync_pred(n, j) == pred) {
6082 if (!is_Sync(pred)) {
6090 pred_arity = get_Sync_n_preds(pred);
6091 for (j = 0; j < pred_arity; ++j) {
6092 ir_node *pred_pred = get_Sync_pred(pred, j);
6097 add_irn_n(n, pred_pred);
6101 if (get_Sync_pred(n, k) == pred_pred)
6108 ir_graph *irg = get_irn_irg(n);
6109 return new_r_Bad(irg, mode_M);
6112 return get_Sync_pred(n, 0);
6115 /* rehash the sync node */
6120 static ir_node *transform_node_Load(ir_node *n)
6122 /* if our memory predecessor is a load from the same address, then reuse the
6123 * previous result */
6124 ir_node *mem = get_Load_mem(n);
6129 /* don't touch volatile loads */
6130 if (get_Load_volatility(n) == volatility_is_volatile)
6132 mem_pred = get_Proj_pred(mem);
6133 if (is_Load(mem_pred)) {
6134 ir_node *pred_load = mem_pred;
6136 /* conservatively compare the 2 loads. TODO: This could be less strict
6137 * with fixup code in some situations (like smaller/bigger modes) */
6138 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6140 if (get_Load_mode(pred_load) != get_Load_mode(n))
6142 /* all combinations of aligned/unaligned pred/n should be fine so we do
6143 * not compare the unaligned attribute */
6145 ir_node *block = get_nodes_block(n);
6146 ir_node *jmp = new_r_Jmp(block);
6147 ir_graph *irg = get_irn_irg(n);
6148 ir_node *bad = new_r_Bad(irg, mode_X);
6149 ir_mode *mode = get_Load_mode(n);
6150 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6151 ir_node *in[] = { mem, res, jmp, bad };
6152 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6155 } else if (is_Store(mem_pred)) {
6156 ir_node *pred_store = mem_pred;
6157 ir_node *value = get_Store_value(pred_store);
6159 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6161 if (get_irn_mode(value) != get_Load_mode(n))
6163 /* all combinations of aligned/unaligned pred/n should be fine so we do
6164 * not compare the unaligned attribute */
6166 ir_node *block = get_nodes_block(n);
6167 ir_node *jmp = new_r_Jmp(block);
6168 ir_graph *irg = get_irn_irg(n);
6169 ir_node *bad = new_r_Bad(irg, mode_X);
6170 ir_node *res = value;
6171 ir_node *in[] = { mem, res, jmp, bad };
6172 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6181 * optimize a trampoline Call into a direct Call
6183 static ir_node *transform_node_Call(ir_node *call)
6185 ir_node *callee = get_Call_ptr(call);
6186 ir_node *adr, *mem, *res, *bl, **in;
6187 ir_type *ctp, *mtp, *tp;
6191 size_t i, n_res, n_param;
6194 if (! is_Proj(callee))
6196 callee = get_Proj_pred(callee);
6197 if (! is_Builtin(callee))
6199 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6202 mem = get_Call_mem(call);
6204 if (skip_Proj(mem) == callee) {
6205 /* memory is routed to the trampoline, skip */
6206 mem = get_Builtin_mem(callee);
6209 /* build a new call type */
6210 mtp = get_Call_type(call);
6211 tdb = get_type_dbg_info(mtp);
6213 n_res = get_method_n_ress(mtp);
6214 n_param = get_method_n_params(mtp);
6215 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6217 for (i = 0; i < n_res; ++i)
6218 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6220 NEW_ARR_A(ir_node *, in, n_param + 1);
6222 /* FIXME: we don't need a new pointer type in every step */
6223 irg = get_irn_irg(call);
6224 tp = get_irg_frame_type(irg);
6225 tp = new_type_pointer(tp);
6226 set_method_param_type(ctp, 0, tp);
6228 in[0] = get_Builtin_param(callee, 2);
6229 for (i = 0; i < n_param; ++i) {
6230 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6231 in[i + 1] = get_Call_param(call, i);
6233 var = get_method_variadicity(mtp);
6234 set_method_variadicity(ctp, var);
6235 /* When we resolve a trampoline, the function must be called by a this-call */
6236 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6237 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6239 adr = get_Builtin_param(callee, 1);
6241 db = get_irn_dbg_info(call);
6242 bl = get_nodes_block(call);
6244 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6245 if (get_irn_pinned(call) == op_pin_state_floats)
6246 set_irn_pinned(res, op_pin_state_floats);
6251 * Tries several [inplace] [optimizing] transformations and returns an
6252 * equivalent node. The difference to equivalent_node() is that these
6253 * transformations _do_ generate new nodes, and thus the old node must
6254 * not be freed even if the equivalent node isn't the old one.
6256 static ir_node *transform_node(ir_node *n)
6262 iro = get_irn_opcode_(n);
6263 /* constant expression evaluation / constant folding */
6264 if (get_opt_constant_folding()) {
6265 /* neither constants nor Tuple values can be evaluated */
6266 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6267 /* try to evaluate */
6268 ir_tarval *tv = computed_value(n);
6269 if (tv != tarval_bad) {
6270 /* evaluation was successful -- replace the node. */
6271 ir_graph *irg = get_irn_irg(n);
6273 n = new_r_Const(irg, tv);
6275 DBG_OPT_CSTEVAL(old_n, n);
6281 /* remove unnecessary nodes */
6282 if (get_opt_constant_folding() ||
6283 (iro == iro_Phi) || /* always optimize these nodes. */
6284 (iro == iro_Id) || /* ... */
6285 (iro == iro_Proj) || /* ... */
6286 (iro == iro_Block)) { /* Flags tested local. */
6287 n = equivalent_node(n);
6292 /* Some more constant expression evaluation. */
6293 if (get_opt_algebraic_simplification() ||
6294 (iro == iro_Cond) ||
6295 (iro == iro_Proj)) { /* Flags tested local. */
6296 if (n->op->ops.transform_node != NULL) {
6297 n = n->op->ops.transform_node(n);
6307 static void register_computed_value_func(ir_op *op, computed_value_func func)
6309 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6310 op->ops.computed_value = func;
6313 static void register_computed_value_func_proj(ir_op *op,
6314 computed_value_func func)
6316 assert(op->ops.computed_value_Proj == NULL
6317 || op->ops.computed_value_Proj == func);
6318 op->ops.computed_value_Proj = func;
6321 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6323 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6324 op->ops.equivalent_node = func;
6327 static void register_equivalent_node_func_proj(ir_op *op,
6328 equivalent_node_func func)
6330 assert(op->ops.equivalent_node_Proj == NULL
6331 || op->ops.equivalent_node_Proj == func);
6332 op->ops.equivalent_node_Proj = func;
6335 static void register_transform_node_func(ir_op *op, transform_node_func func)
6337 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6338 op->ops.transform_node = func;
6341 static void register_transform_node_func_proj(ir_op *op,
6342 transform_node_func func)
6344 assert(op->ops.transform_node_Proj == NULL
6345 || op->ops.transform_node_Proj == func);
6346 op->ops.transform_node_Proj = func;
6349 void ir_register_opt_node_ops(void)
6351 register_computed_value_func(op_Add, computed_value_Add);
6352 register_computed_value_func(op_And, computed_value_And);
6353 register_computed_value_func(op_Borrow, computed_value_Borrow);
6354 register_computed_value_func(op_Carry, computed_value_Carry);
6355 register_computed_value_func(op_Cmp, computed_value_Cmp);
6356 register_computed_value_func(op_Confirm, computed_value_Confirm);
6357 register_computed_value_func(op_Const, computed_value_Const);
6358 register_computed_value_func(op_Conv, computed_value_Conv);
6359 register_computed_value_func(op_Eor, computed_value_Eor);
6360 register_computed_value_func(op_Minus, computed_value_Minus);
6361 register_computed_value_func(op_Mul, computed_value_Mul);
6362 register_computed_value_func(op_Mux, computed_value_Mux);
6363 register_computed_value_func(op_Not, computed_value_Not);
6364 register_computed_value_func(op_Or, computed_value_Or);
6365 register_computed_value_func(op_Proj, computed_value_Proj);
6366 register_computed_value_func(op_Rotl, computed_value_Rotl);
6367 register_computed_value_func(op_Shl, computed_value_Shl);
6368 register_computed_value_func(op_Shr, computed_value_Shr);
6369 register_computed_value_func(op_Shrs, computed_value_Shrs);
6370 register_computed_value_func(op_Sub, computed_value_Sub);
6371 register_computed_value_func(op_SymConst, computed_value_SymConst);
6372 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6373 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6375 register_equivalent_node_func(op_Add, equivalent_node_Add);
6376 register_equivalent_node_func(op_And, equivalent_node_And);
6377 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6378 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6379 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6380 register_equivalent_node_func(op_Id, equivalent_node_Id);
6381 register_equivalent_node_func(op_Minus, equivalent_node_idempotent_unop);
6382 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6383 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6384 register_equivalent_node_func(op_Not, equivalent_node_idempotent_unop);
6385 register_equivalent_node_func(op_Or, equivalent_node_Or);
6386 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6387 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6388 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6389 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6390 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6391 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6392 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6393 register_equivalent_node_func_proj(op_Bound, equivalent_node_Proj_Bound);
6394 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6395 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6396 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6398 register_transform_node_func(op_Add, transform_node_Add);
6399 register_transform_node_func(op_And, transform_node_And);
6400 register_transform_node_func(op_Block, transform_node_Block);
6401 register_transform_node_func(op_Call, transform_node_Call);
6402 register_transform_node_func(op_Cmp, transform_node_Cmp);
6403 register_transform_node_func(op_Cond, transform_node_Cond);
6404 register_transform_node_func(op_Conv, transform_node_Conv);
6405 register_transform_node_func(op_Div, transform_node_Div);
6406 register_transform_node_func(op_End, transform_node_End);
6407 register_transform_node_func(op_Eor, transform_node_Eor);
6408 register_transform_node_func(op_Load, transform_node_Load);
6409 register_transform_node_func(op_Minus, transform_node_Minus);
6410 register_transform_node_func(op_Mod, transform_node_Mod);
6411 register_transform_node_func(op_Mul, transform_node_Mul);
6412 register_transform_node_func(op_Mux, transform_node_Mux);
6413 register_transform_node_func(op_Not, transform_node_Not);
6414 register_transform_node_func(op_Or, transform_node_Or);
6415 register_transform_node_func(op_Phi, transform_node_Phi);
6416 register_transform_node_func(op_Proj, transform_node_Proj);
6417 register_transform_node_func(op_Rotl, transform_node_Rotl);
6418 register_transform_node_func(op_Shl, transform_node_Shl);
6419 register_transform_node_func(op_Shrs, transform_node_Shrs);
6420 register_transform_node_func(op_Shr, transform_node_Shr);
6421 register_transform_node_func(op_Sub, transform_node_Sub);
6422 register_transform_node_func(op_Switch, transform_node_Switch);
6423 register_transform_node_func(op_Sync, transform_node_Sync);
6424 register_transform_node_func_proj(op_Bound, transform_node_Proj_Bound);
6425 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6426 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6427 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6428 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6429 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6432 /* **************** Common Subexpression Elimination **************** */
6434 /** The size of the hash table used, should estimate the number of nodes
6436 #define N_IR_NODES 512
6438 int identities_cmp(const void *elt, const void *key)
6440 ir_node *a = (ir_node *)elt;
6441 ir_node *b = (ir_node *)key;
6444 if (a == b) return 0;
6446 if ((get_irn_op(a) != get_irn_op(b)) ||
6447 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6449 /* compare if a's in and b's in are of equal length */
6450 irn_arity_a = get_irn_arity(a);
6451 if (irn_arity_a != get_irn_arity(b))
6454 /* blocks are never the same */
6458 if (get_irn_pinned(a) == op_pin_state_pinned) {
6459 /* for pinned nodes, the block inputs must be equal */
6460 if (get_nodes_block(a) != get_nodes_block(b))
6463 ir_node *block_a = get_nodes_block(a);
6464 ir_node *block_b = get_nodes_block(b);
6465 if (! get_opt_global_cse()) {
6466 /* for block-local CSE both nodes must be in the same Block */
6467 if (block_a != block_b)
6470 /* The optimistic approach would be to do nothing here.
6471 * However doing GCSE optimistically produces a lot of partially dead code which appears
6472 * to be worse in practice than the missed opportunities.
6473 * So we use a very conservative variant here and only CSE if 1 value dominates the
6475 if (!block_dominates(block_a, block_b)
6476 && !block_dominates(block_b, block_a))
6481 /* compare a->in[0..ins] with b->in[0..ins] */
6482 for (i = 0; i < irn_arity_a; ++i) {
6483 ir_node *pred_a = get_irn_n(a, i);
6484 ir_node *pred_b = get_irn_n(b, i);
6485 if (pred_a != pred_b) {
6486 /* if both predecessors are CSE neutral they might be different */
6487 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6493 * here, we already now that the nodes are identical except their
6496 if (a->op->ops.node_cmp_attr)
6497 return a->op->ops.node_cmp_attr(a, b);
6502 unsigned ir_node_hash(const ir_node *node)
6504 return node->op->ops.hash(node);
6507 void new_identities(ir_graph *irg)
6509 if (irg->value_table != NULL)
6510 del_pset(irg->value_table);
6511 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6514 void del_identities(ir_graph *irg)
6516 if (irg->value_table != NULL)
6517 del_pset(irg->value_table);
6520 static int cmp_node_nr(const void *a, const void *b)
6522 ir_node **p1 = (ir_node**)a;
6523 ir_node **p2 = (ir_node**)b;
6524 long n1 = get_irn_node_nr(*p1);
6525 long n2 = get_irn_node_nr(*p2);
6526 return (n1>n2) - (n1<n2);
6529 void ir_normalize_node(ir_node *n)
6531 if (is_op_commutative(get_irn_op(n))) {
6532 ir_node *l = get_binop_left(n);
6533 ir_node *r = get_binop_right(n);
6535 /* For commutative operators perform a OP b == b OP a but keep
6536 * constants on the RIGHT side. This helps greatly in some
6537 * optimizations. Moreover we use the idx number to make the form
6539 if (!operands_are_normalized(l, r)) {
6540 set_binop_left(n, r);
6541 set_binop_right(n, l);
6544 } else if (is_Sync(n)) {
6545 /* we assume that most of the time the inputs of a Sync node are already
6546 * sorted, so check this first as a shortcut */
6547 bool ins_sorted = true;
6548 int arity = get_irn_arity(n);
6549 const ir_node *last = get_irn_n(n, 0);
6551 for (i = 1; i < arity; ++i) {
6552 const ir_node *node = get_irn_n(n, i);
6553 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6561 ir_node **ins = get_irn_in(n)+1;
6562 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6563 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6564 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6565 set_irn_in(n, arity, new_ins);
6571 ir_node *identify_remember(ir_node *n)
6573 ir_graph *irg = get_irn_irg(n);
6574 pset *value_table = irg->value_table;
6577 if (value_table == NULL)
6580 ir_normalize_node(n);
6581 /* lookup or insert in hash table with given hash key. */
6582 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6585 /* n is reachable again */
6586 edges_node_revival(nn);
6593 * During construction we set the op_pin_state_pinned flag in the graph right
6594 * when the optimization is performed. The flag turning on procedure global
6595 * cse could be changed between two allocations. This way we are safe.
6597 * @param n The node to lookup
6599 static inline ir_node *identify_cons(ir_node *n)
6603 n = identify_remember(n);
6604 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6605 ir_graph *irg = get_irn_irg(n);
6606 set_irg_pinned(irg, op_pin_state_floats);
6611 void add_identities(ir_node *node)
6618 identify_remember(node);
6621 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6623 ir_graph *rem = current_ir_graph;
6625 current_ir_graph = irg;
6626 foreach_pset(irg->value_table, ir_node, node) {
6629 current_ir_graph = rem;
6632 ir_node *optimize_node(ir_node *n)
6635 ir_graph *irg = get_irn_irg(n);
6636 unsigned iro = get_irn_opcode(n);
6639 /* Always optimize Phi nodes: part of the construction. */
6640 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6642 /* constant expression evaluation / constant folding */
6643 if (get_opt_constant_folding()) {
6644 /* neither constants nor Tuple values can be evaluated */
6645 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6646 /* try to evaluate */
6647 tv = computed_value(n);
6648 if (tv != tarval_bad) {
6653 * we MUST copy the node here temporarily, because it's still
6654 * needed for DBG_OPT_CSTEVAL
6656 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6657 oldn = (ir_node*)alloca(node_size);
6659 memcpy(oldn, n, node_size);
6660 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6662 /* ARG, copy the in array, we need it for statistics */
6663 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6665 /* note the inplace edges module */
6666 edges_node_deleted(n);
6668 /* evaluation was successful -- replace the node. */
6669 irg_kill_node(irg, n);
6670 nw = new_r_Const(irg, tv);
6672 DBG_OPT_CSTEVAL(oldn, nw);
6678 /* remove unnecessary nodes */
6679 if (get_opt_algebraic_simplification() ||
6680 (iro == iro_Phi) || /* always optimize these nodes. */
6682 (iro == iro_Proj) ||
6683 (iro == iro_Block) ) /* Flags tested local. */
6684 n = equivalent_node(n);
6686 /* Common Subexpression Elimination.
6688 * Checks whether n is already available.
6689 * The block input is used to distinguish different subexpressions. Right
6690 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6691 * subexpressions within a block.
6694 n = identify_cons(n);
6697 edges_node_deleted(oldn);
6699 /* We found an existing, better node, so we can deallocate the old node. */
6700 irg_kill_node(irg, oldn);
6704 /* Some more constant expression evaluation that does not allow to
6706 iro = get_irn_opcode(n);
6707 if (get_opt_algebraic_simplification() ||
6708 (iro == iro_Cond) ||
6709 (iro == iro_Proj)) { /* Flags tested local. */
6710 n = transform_node(n);
6713 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6714 if (get_opt_cse()) {
6716 n = identify_remember(o);
6724 ir_node *optimize_in_place_2(ir_node *n)
6726 if (!get_opt_optimize() && !is_Phi(n)) return n;
6731 /** common subexpression elimination **/
6732 /* Checks whether n is already available. */
6733 /* The block input is used to distinguish different subexpressions.
6734 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6735 * only finds common subexpressions within a block. */
6736 if (get_opt_cse()) {
6738 n = identify_remember(n);
6741 /* we have another existing node now, we do not optimize it here */
6746 n = transform_node(n);
6748 /* Now we can verify the node, as it has no dead inputs any more. */
6751 /* Now we have a legal, useful node. Enter it in hash table for cse.
6753 * Note: This is only necessary because some of the optimisations
6754 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6755 * bad practice and should be fixed sometime.
6757 if (get_opt_cse()) {
6759 n = identify_remember(o);
6767 ir_node *optimize_in_place(ir_node *n)
6769 ir_graph *irg = get_irn_irg(n);
6770 /* Handle graph state */
6771 assert(get_irg_phase_state(irg) != phase_building);
6773 if (get_opt_global_cse())
6774 set_irg_pinned(irg, op_pin_state_floats);
6776 /* FIXME: Maybe we could also test whether optimizing the node can
6777 change the control graph. */
6778 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6779 return optimize_in_place_2(n);