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 !irg_is_constrained(get_irn_irg(n), IR_GRAPH_CONSTRAINT_CONSTRUCTION))
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)
1514 int n = get_irn_arity(phi);
1515 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1517 for (int i = 0; i < n; ++i) {
1518 ir_node *pred = get_irn_n(phi, i);
1519 ir_tarval *tv = get_Const_tarval(pred);
1520 tv = do_eval(eval, other, tv, mode);
1522 if (tv == tarval_bad) {
1523 /* folding failed, bad */
1529 for (int i = 0; i < n; ++i) {
1530 ir_node *pred = get_irn_n(phi, i);
1531 ir_tarval *tv = get_Const_tarval(pred);
1532 tv = do_eval(eval, tv, other, mode);
1534 if (tv == tarval_bad) {
1535 /* folding failed, bad */
1541 ir_graph *irg = get_irn_irg(phi);
1542 ir_node **res = ALLOCAN(ir_node*, n);
1543 for (int i = 0; i < n; ++i) {
1544 res[i] = new_r_Const(irg, tvs[i]);
1546 ir_node *block = get_nodes_block(phi);
1547 return new_r_Phi(block, n, res, mode);
1551 * Apply an evaluator on a binop with two constant Phi.
1553 * @param a the left Phi node
1554 * @param b the right Phi node
1555 * @param eval an evaluator function
1556 * @param mode the mode of the result, may be different from the mode of the Phi!
1558 * @return a new Phi node if the conversion was successful, NULL else
1560 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1562 if (get_nodes_block(a) != get_nodes_block(b))
1565 int n = get_irn_arity(a);
1566 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1567 for (int i = 0; i < n; ++i) {
1568 ir_node *pred_a = get_irn_n(a, i);
1569 ir_tarval *tv_l = get_Const_tarval(pred_a);
1570 ir_node *pred_b = get_irn_n(b, i);
1571 ir_tarval *tv_r = get_Const_tarval(pred_b);
1572 ir_tarval *tv = do_eval(eval, tv_l, tv_r, mode);
1574 if (tv == tarval_bad) {
1575 /* folding failed, bad */
1580 ir_graph *irg = get_irn_irg(a);
1581 ir_node **res = ALLOCAN(ir_node*, n);
1582 for (int i = 0; i < n; ++i) {
1583 res[i] = new_r_Const(irg, tvs[i]);
1585 ir_node *block = get_nodes_block(a);
1586 return new_r_Phi(block, n, res, mode);
1590 * Apply an evaluator on a unop with a constant operator (a Phi).
1592 * @param phi the Phi node
1593 * @param eval an evaluator function
1595 * @return a new Phi node if the conversion was successful, NULL else
1597 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1599 int n = get_irn_arity(phi);
1600 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1601 for (int i = 0; i < n; ++i) {
1602 ir_node *pred = get_irn_n(phi, i);
1603 ir_tarval *tv = get_Const_tarval(pred);
1606 if (tv == tarval_bad) {
1607 /* folding failed, bad */
1612 ir_graph *irg = get_irn_irg(phi);
1613 ir_node **res = ALLOCAN(ir_node*, n);
1614 for (int i = 0; i < n; ++i) {
1615 res[i] = new_r_Const(irg, tvs[i]);
1617 ir_node *block = get_nodes_block(phi);
1618 ir_mode *mode = get_irn_mode(phi);
1619 return new_r_Phi(block, n, res, mode);
1623 * Apply a conversion on a constant operator (a Phi).
1625 * @param phi the Phi node
1627 * @return a new Phi node if the conversion was successful, NULL else
1629 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1631 int n = get_irn_arity(phi);
1632 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1633 for (int i = 0; i < n; ++i) {
1634 ir_node *pred = get_irn_n(phi, i);
1635 ir_tarval *tv = get_Const_tarval(pred);
1636 tv = tarval_convert_to(tv, mode);
1638 if (tv == tarval_bad) {
1639 /* folding failed, bad */
1644 ir_graph *irg = get_irn_irg(phi);
1645 ir_node **res = ALLOCAN(ir_node*, n);
1646 for (int i = 0; i < n; ++i) {
1647 res[i] = new_r_Const(irg, tvs[i]);
1649 ir_node *block = get_nodes_block(phi);
1650 return new_r_Phi(block, n, res, mode);
1654 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1655 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1656 * If possible, remove the Conv's.
1658 static ir_node *transform_node_AddSub(ir_node *n)
1660 ir_mode *mode = get_irn_mode(n);
1662 if (mode_is_reference(mode)) {
1663 ir_node *left = get_binop_left(n);
1664 ir_node *right = get_binop_right(n);
1665 unsigned ref_bits = get_mode_size_bits(mode);
1667 if (is_Conv(left)) {
1668 ir_mode *lmode = get_irn_mode(left);
1669 unsigned bits = get_mode_size_bits(lmode);
1671 if (ref_bits == bits &&
1672 mode_is_int(lmode) &&
1673 get_mode_arithmetic(lmode) == irma_twos_complement) {
1674 ir_node *pre = get_Conv_op(left);
1675 ir_mode *pre_mode = get_irn_mode(pre);
1677 if (mode_is_int(pre_mode) &&
1678 get_mode_size_bits(pre_mode) == bits &&
1679 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1680 /* ok, this conv just changes to sign, moreover the calculation
1681 * is done with same number of bits as our address mode, so
1682 * we can ignore the conv as address calculation can be viewed
1683 * as either signed or unsigned
1685 set_binop_left(n, pre);
1690 if (is_Conv(right)) {
1691 ir_mode *rmode = get_irn_mode(right);
1692 unsigned bits = get_mode_size_bits(rmode);
1694 if (ref_bits == bits &&
1695 mode_is_int(rmode) &&
1696 get_mode_arithmetic(rmode) == irma_twos_complement) {
1697 ir_node *pre = get_Conv_op(right);
1698 ir_mode *pre_mode = get_irn_mode(pre);
1700 if (mode_is_int(pre_mode) &&
1701 get_mode_size_bits(pre_mode) == bits &&
1702 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1703 /* ok, this conv just changes to sign, moreover the calculation
1704 * is done with same number of bits as our address mode, so
1705 * we can ignore the conv as address calculation can be viewed
1706 * as either signed or unsigned
1708 set_binop_right(n, pre);
1713 /* let address arithmetic use unsigned modes */
1714 if (is_Const(right)) {
1715 ir_mode *rmode = get_irn_mode(right);
1717 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1718 /* convert a AddP(P, *s) into AddP(P, *u) */
1719 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1721 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1722 set_binop_right(n, pre);
1730 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1733 if (is_Const(b) && is_const_Phi(a)) { \
1734 /* check for Op(Phi, Const) */ \
1735 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1737 else if (is_Const(a) && is_const_Phi(b)) { \
1738 /* check for Op(Const, Phi) */ \
1739 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1741 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1742 /* check for Op(Phi, Phi) */ \
1743 c = apply_binop_on_2_phis(a, b, eval, mode); \
1746 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1751 #define HANDLE_UNOP_PHI(eval, a, c) \
1754 if (is_const_Phi(a)) { \
1755 /* check for Op(Phi) */ \
1756 c = apply_unop_on_phi(a, eval); \
1758 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1765 * Create a 0 constant of given mode.
1767 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1769 ir_tarval *tv = get_mode_null(mode);
1770 ir_node *cnst = new_r_Const(irg, tv);
1775 static bool is_shiftop(const ir_node *n)
1777 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1780 /* the order of the values is important! */
1781 typedef enum const_class {
1787 static const_class classify_const(const ir_node* n)
1789 if (is_Const(n)) return const_const;
1790 if (is_irn_constlike(n)) return const_like;
1795 * Determines whether r is more constlike or has a larger index (in that order)
1798 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1800 const const_class l_order = classify_const(l);
1801 const const_class r_order = classify_const(r);
1803 l_order > r_order ||
1804 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1807 static bool is_cmp_unequal(const ir_node *node)
1809 ir_relation relation = get_Cmp_relation(node);
1810 ir_node *left = get_Cmp_left(node);
1811 ir_node *right = get_Cmp_right(node);
1812 ir_mode *mode = get_irn_mode(left);
1814 if (relation == ir_relation_less_greater)
1817 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1818 return relation == ir_relation_greater;
1823 * returns true for Cmp(x == 0) or Cmp(x != 0)
1825 static bool is_cmp_equality_zero(const ir_node *node)
1827 ir_relation relation;
1828 ir_node *right = get_Cmp_right(node);
1830 if (!is_Const(right) || !is_Const_null(right))
1832 relation = get_Cmp_relation(node);
1833 return relation == ir_relation_equal
1834 || relation == ir_relation_less_greater
1835 || (!mode_is_signed(get_irn_mode(right))
1836 && relation == ir_relation_greater);
1840 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1841 * Such pattern may arise in bitfield stores.
1843 * value c4 value c4 & c2
1844 * AND c3 AND c1 | c3
1851 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1854 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1856 ir_node *irn_and, *c1;
1858 ir_node *and_l, *c3;
1859 ir_node *value, *c4;
1860 ir_node *new_and, *new_const, *block;
1861 ir_mode *mode = get_irn_mode(irn_or);
1863 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1867 irn_and = get_binop_left(irn_or);
1868 c1 = get_binop_right(irn_or);
1869 if (!is_Const(c1) || !is_And(irn_and))
1872 or_l = get_binop_left(irn_and);
1873 c2 = get_binop_right(irn_and);
1877 tv1 = get_Const_tarval(c1);
1878 tv2 = get_Const_tarval(c2);
1880 tv = tarval_or(tv1, tv2);
1881 if (tarval_is_all_one(tv)) {
1882 /* the AND does NOT clear a bit with isn't set by the OR */
1883 set_binop_left(irn_or, or_l);
1884 set_binop_right(irn_or, c1);
1886 /* check for more */
1890 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
1893 and_l = get_binop_left(or_l);
1894 c3 = get_binop_right(or_l);
1895 if (!is_Const(c3) || !is_And(and_l))
1898 value = get_binop_left(and_l);
1899 c4 = get_binop_right(and_l);
1903 /* ok, found the pattern, check for conditions */
1904 assert(mode == get_irn_mode(irn_and));
1905 assert(mode == get_irn_mode(or_l));
1906 assert(mode == get_irn_mode(and_l));
1908 tv3 = get_Const_tarval(c3);
1909 tv4 = get_Const_tarval(c4);
1911 tv = tarval_or(tv4, tv2);
1912 if (!tarval_is_all_one(tv)) {
1913 /* have at least one 0 at the same bit position */
1917 if (tv3 != tarval_andnot(tv3, tv4)) {
1918 /* bit in the or_mask is outside the and_mask */
1922 if (tv1 != tarval_andnot(tv1, tv2)) {
1923 /* bit in the or_mask is outside the and_mask */
1927 /* ok, all conditions met */
1928 block = get_nodes_block(irn_or);
1929 irg = get_irn_irg(block);
1931 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
1933 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
1935 set_binop_left(irn_or, new_and);
1936 set_binop_right(irn_or, new_const);
1938 /* check for more */
1943 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
1945 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
1947 ir_mode *mode = get_irn_mode(irn_or);
1948 ir_node *shl, *shr, *block;
1949 ir_node *irn, *x, *c1, *c2, *n;
1950 ir_tarval *tv1, *tv2;
1952 /* some backends can't handle rotl */
1953 if (!be_get_backend_param()->support_rotl)
1956 if (! mode_is_int(mode))
1959 shl = get_binop_left(irn_or);
1960 shr = get_binop_right(irn_or);
1969 } else if (!is_Shl(shl)) {
1971 } else if (!is_Shr(shr)) {
1974 x = get_Shl_left(shl);
1975 if (x != get_Shr_left(shr))
1978 c1 = get_Shl_right(shl);
1979 c2 = get_Shr_right(shr);
1980 if (is_Const(c1) && is_Const(c2)) {
1981 tv1 = get_Const_tarval(c1);
1982 if (! tarval_is_long(tv1))
1985 tv2 = get_Const_tarval(c2);
1986 if (! tarval_is_long(tv2))
1989 if (get_tarval_long(tv1) + get_tarval_long(tv2)
1990 != (int) get_mode_size_bits(mode))
1993 /* yet, condition met */
1994 block = get_nodes_block(irn_or);
1996 n = new_r_Rotl(block, x, c1, mode);
1998 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2002 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2003 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2004 if (!ir_is_negated_value(c1, c2)) {
2008 /* yet, condition met */
2009 block = get_nodes_block(irn_or);
2010 n = new_r_Rotl(block, x, c1, mode);
2011 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2016 * Prototype of a recursive transform function
2017 * for bitwise distributive transformations.
2019 typedef ir_node* (*recursive_transform)(ir_node *n);
2022 * makes use of distributive laws for and, or, eor
2023 * and(a OP c, b OP c) -> and(a, b) OP c
2024 * note, might return a different op than n
2026 static ir_node *transform_bitwise_distributive(ir_node *n,
2027 recursive_transform trans_func)
2030 ir_node *a = get_binop_left(n);
2031 ir_node *b = get_binop_right(n);
2032 ir_op *op = get_irn_op(a);
2033 ir_op *op_root = get_irn_op(n);
2035 if (op != get_irn_op(b))
2038 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2039 if (op == op_Conv) {
2040 ir_node *a_op = get_Conv_op(a);
2041 ir_node *b_op = get_Conv_op(b);
2042 ir_mode *a_mode = get_irn_mode(a_op);
2043 ir_mode *b_mode = get_irn_mode(b_op);
2044 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2045 ir_node *blk = get_nodes_block(n);
2048 set_binop_left(n, a_op);
2049 set_binop_right(n, b_op);
2050 set_irn_mode(n, a_mode);
2052 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2054 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2060 /* nothing to gain here */
2064 if (op == op_Shrs || op == op_Shr || op == op_Shl
2065 || op == op_And || op == op_Or || op == op_Eor) {
2066 ir_node *a_left = get_binop_left(a);
2067 ir_node *a_right = get_binop_right(a);
2068 ir_node *b_left = get_binop_left(b);
2069 ir_node *b_right = get_binop_right(b);
2071 ir_node *op1 = NULL;
2072 ir_node *op2 = NULL;
2074 if (is_op_commutative(op)) {
2075 if (a_left == b_left) {
2079 } else if (a_left == b_right) {
2083 } else if (a_right == b_left) {
2089 if (a_right == b_right) {
2096 /* (a sop c) & (b sop c) => (a & b) sop c */
2097 ir_node *blk = get_nodes_block(n);
2099 ir_node *new_n = exact_copy(n);
2100 set_binop_left(new_n, op1);
2101 set_binop_right(new_n, op2);
2102 new_n = trans_func(new_n);
2104 if (op_root == op_Eor && op == op_Or) {
2105 dbg_info *dbgi = get_irn_dbg_info(n);
2106 ir_mode *mode = get_irn_mode(c);
2108 c = new_rd_Not(dbgi, blk, c, mode);
2109 n = new_rd_And(dbgi, blk, new_n, c, mode);
2112 set_nodes_block(n, blk);
2113 set_binop_left(n, new_n);
2114 set_binop_right(n, c);
2118 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2127 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2129 * - and, or, xor instead of &
2130 * - Shl, Shr, Shrs, rotl instead of >>
2131 * (with a special case for Or/Xor + Shrs)
2133 * This normalisation is usually good for the backend since << C can often be
2134 * matched as address-mode.
2136 static ir_node *transform_node_bitop_shift(ir_node *n)
2138 ir_graph *irg = get_irn_irg(n);
2139 ir_node *left = get_binop_left(n);
2140 ir_node *right = get_binop_right(n);
2141 ir_mode *mode = get_irn_mode(n);
2142 ir_node *shift_left;
2143 ir_node *shift_right;
2145 dbg_info *dbg_bitop;
2146 dbg_info *dbg_shift;
2152 ir_tarval *tv_bitop;
2154 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2157 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2158 if (!is_Const(right) || !is_shiftop(left))
2161 shift_left = get_binop_left(left);
2162 shift_right = get_binop_right(left);
2163 if (!is_Const(shift_right))
2166 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2167 if (is_Shrs(left)) {
2168 /* TODO this could be improved */
2172 irg = get_irn_irg(n);
2173 block = get_nodes_block(n);
2174 dbg_bitop = get_irn_dbg_info(n);
2175 dbg_shift = get_irn_dbg_info(left);
2176 tv1 = get_Const_tarval(shift_right);
2177 tv2 = get_Const_tarval(right);
2178 assert(get_tarval_mode(tv2) == mode);
2181 tv_bitop = tarval_shr(tv2, tv1);
2183 /* Check whether we have lost some bits during the right shift. */
2185 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2187 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2190 } else if (is_Shr(left)) {
2193 * TODO this can be improved by checking whether
2194 * the left shift produces an overflow
2198 tv_bitop = tarval_shl(tv2, tv1);
2200 assert(is_Rotl(left));
2201 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2203 new_const = new_r_Const(irg, tv_bitop);
2206 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2207 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2208 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2211 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2215 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2216 } else if (is_Shr(left)) {
2217 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2219 assert(is_Rotl(left));
2220 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2226 static bool complement_values(const ir_node *a, const ir_node *b)
2228 if (is_Not(a) && get_Not_op(a) == b)
2230 if (is_Not(b) && get_Not_op(b) == a)
2232 if (is_Const(a) && is_Const(b)) {
2233 ir_tarval *tv_a = get_Const_tarval(a);
2234 ir_tarval *tv_b = get_Const_tarval(b);
2235 return tarval_not(tv_a) == tv_b;
2240 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2243 * for associative operations fold:
2244 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2245 * This is a "light" version of the reassociation phase
2247 static ir_node *fold_constant_associativity(ir_node *node,
2248 tv_fold_binop_func fold)
2253 ir_node *right = get_binop_right(node);
2254 ir_node *left_right;
2261 if (!is_Const(right))
2264 op = get_irn_op(node);
2265 left = get_binop_left(node);
2266 if (get_irn_op(left) != op)
2269 left_right = get_binop_right(left);
2270 if (!is_Const(left_right))
2273 left_left = get_binop_left(left);
2274 c0 = get_Const_tarval(left_right);
2275 c1 = get_Const_tarval(right);
2276 irg = get_irn_irg(node);
2277 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2279 new_c = fold(c0, c1);
2280 if (new_c == tarval_bad)
2282 new_const = new_r_Const(irg, new_c);
2283 new_node = exact_copy(node);
2284 set_binop_left(new_node, left_left);
2285 set_binop_right(new_node, new_const);
2292 static ir_node *transform_node_Or_(ir_node *n)
2295 ir_node *a = get_binop_left(n);
2296 ir_node *b = get_binop_right(n);
2300 n = fold_constant_associativity(n, tarval_or);
2304 if (is_Not(a) && is_Not(b)) {
2305 /* ~a | ~b = ~(a&b) */
2306 ir_node *block = get_nodes_block(n);
2308 mode = get_irn_mode(n);
2311 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2312 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2313 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2317 /* we can combine the relations of two compares with the same operands */
2318 if (is_Cmp(a) && is_Cmp(b)) {
2319 ir_node *a_left = get_Cmp_left(a);
2320 ir_node *a_right = get_Cmp_right(a);
2321 ir_node *b_left = get_Cmp_left(b);
2322 ir_node *b_right = get_Cmp_right(b);
2323 if (a_left == b_left && b_left == b_right) {
2324 dbg_info *dbgi = get_irn_dbg_info(n);
2325 ir_node *block = get_nodes_block(n);
2326 ir_relation a_relation = get_Cmp_relation(a);
2327 ir_relation b_relation = get_Cmp_relation(b);
2328 ir_relation new_relation = a_relation | b_relation;
2329 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2331 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2332 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2333 && !mode_is_float(get_irn_mode(a_left))
2334 && !mode_is_float(get_irn_mode(b_left))) {
2335 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2336 ir_graph *irg = get_irn_irg(n);
2337 dbg_info *dbgi = get_irn_dbg_info(n);
2338 ir_node *block = get_nodes_block(n);
2339 ir_mode *a_mode = get_irn_mode(a_left);
2340 ir_mode *b_mode = get_irn_mode(b_left);
2341 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2342 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2343 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2344 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2345 ir_node *zero = create_zero_const(irg, b_mode);
2346 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2348 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2349 ir_graph *irg = get_irn_irg(n);
2350 dbg_info *dbgi = get_irn_dbg_info(n);
2351 ir_node *block = get_nodes_block(n);
2352 ir_mode *a_mode = get_irn_mode(a_left);
2353 ir_mode *b_mode = get_irn_mode(b_left);
2354 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2355 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2356 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2357 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2358 ir_node *zero = create_zero_const(irg, a_mode);
2359 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2364 mode = get_irn_mode(n);
2365 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2367 n = transform_node_Or_bf_store(n);
2370 n = transform_node_Or_Rotl(n);
2374 n = transform_bitwise_distributive(n, transform_node_Or_);
2377 n = transform_node_bitop_shift(n);
2384 static ir_node *transform_node_Or(ir_node *n)
2386 if (is_Or_Eor_Add(n)) {
2387 dbg_info *dbgi = get_irn_dbg_info(n);
2388 ir_node *block = get_nodes_block(n);
2389 ir_node *left = get_Or_left(n);
2390 ir_node *right = get_Or_right(n);
2391 ir_mode *mode = get_irn_mode(n);
2392 return new_rd_Add(dbgi, block, left, right, mode);
2394 return transform_node_Or_(n);
2400 static ir_node *transform_node_Eor_(ir_node *n)
2403 ir_node *a = get_binop_left(n);
2404 ir_node *b = get_binop_right(n);
2405 ir_mode *mode = get_irn_mode(n);
2408 n = fold_constant_associativity(n, tarval_eor);
2412 /* we can combine the relations of two compares with the same operands */
2413 if (is_Cmp(a) && is_Cmp(b)) {
2414 ir_node *a_left = get_Cmp_left(a);
2415 ir_node *a_right = get_Cmp_left(a);
2416 ir_node *b_left = get_Cmp_left(b);
2417 ir_node *b_right = get_Cmp_right(b);
2418 if (a_left == b_left && b_left == b_right) {
2419 dbg_info *dbgi = get_irn_dbg_info(n);
2420 ir_node *block = get_nodes_block(n);
2421 ir_relation a_relation = get_Cmp_relation(a);
2422 ir_relation b_relation = get_Cmp_relation(b);
2423 ir_relation new_relation = a_relation ^ b_relation;
2424 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2428 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2430 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2431 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2432 dbg_info *dbg = get_irn_dbg_info(n);
2433 ir_node *block = get_nodes_block(n);
2434 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2435 ir_node *new_left = get_Not_op(a);
2436 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2439 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2440 dbg_info *dbg = get_irn_dbg_info(n);
2441 ir_node *block = get_nodes_block(n);
2442 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2443 ir_node *new_right = get_Not_op(b);
2444 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2445 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2449 /* x ^ 1...1 -> ~1 */
2450 if (is_Const(b) && is_Const_all_one(b)) {
2451 n = new_r_Not(get_nodes_block(n), a, mode);
2452 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2456 n = transform_bitwise_distributive(n, transform_node_Eor_);
2459 n = transform_node_bitop_shift(n);
2466 static ir_node *transform_node_Eor(ir_node *n)
2468 if (is_Or_Eor_Add(n)) {
2469 dbg_info *dbgi = get_irn_dbg_info(n);
2470 ir_node *block = get_nodes_block(n);
2471 ir_node *left = get_Eor_left(n);
2472 ir_node *right = get_Eor_right(n);
2473 ir_mode *mode = get_irn_mode(n);
2474 return new_rd_Add(dbgi, block, left, right, mode);
2476 return transform_node_Eor_(n);
2480 * Do the AddSub optimization, then Transform
2481 * Constant folding on Phi
2482 * Add(a,a) -> Mul(a, 2)
2483 * Add(Mul(a, x), a) -> Mul(a, x+1)
2484 * if the mode is integer or float.
2485 * Transform Add(a,-b) into Sub(a,b).
2486 * Reassociation might fold this further.
2488 static ir_node *transform_node_Add(ir_node *n)
2496 n = fold_constant_associativity(n, tarval_add);
2500 n = transform_node_AddSub(n);
2504 a = get_Add_left(n);
2505 b = get_Add_right(n);
2506 mode = get_irn_mode(n);
2508 if (mode_is_reference(mode)) {
2509 ir_mode *lmode = get_irn_mode(a);
2511 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2512 /* an Add(a, NULL) is a hidden Conv */
2513 dbg_info *dbg = get_irn_dbg_info(n);
2514 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2518 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2519 ir_tarval *tv = get_Const_tarval(b);
2520 ir_tarval *min = get_mode_min(mode);
2521 /* if all bits are set, then this has the same effect as a Not.
2522 * Note that the following == gives false for different modes which
2523 * is exactly what we want */
2525 dbg_info *dbgi = get_irn_dbg_info(n);
2526 ir_graph *irg = get_irn_irg(n);
2527 ir_node *block = get_nodes_block(n);
2528 ir_node *cnst = new_r_Const(irg, min);
2529 return new_rd_Eor(dbgi, block, a, cnst, mode);
2533 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2535 /* for FP the following optimizations are only allowed if
2536 * fp_strict_algebraic is disabled */
2537 if (mode_is_float(mode)) {
2538 ir_graph *irg = get_irn_irg(n);
2539 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2543 if (mode_is_num(mode)) {
2544 ir_graph *irg = get_irn_irg(n);
2545 /* the following code leads to endless recursion when Mul are replaced
2546 * by a simple instruction chain */
2547 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2548 && a == b && mode_is_int(mode)) {
2549 ir_node *block = get_nodes_block(n);
2552 get_irn_dbg_info(n),
2555 new_r_Const_long(irg, mode, 2),
2557 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2562 get_irn_dbg_info(n),
2567 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2572 get_irn_dbg_info(n),
2577 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2580 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2581 /* Here we rely on constants be on the RIGHT side */
2583 ir_node *op = get_Not_op(a);
2585 if (is_Const(b) && is_Const_one(b)) {
2587 ir_node *blk = get_nodes_block(n);
2588 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2596 if (is_Or_Eor_Add(n)) {
2597 n = transform_node_Or_(n);
2600 n = transform_node_Eor_(n);
2609 * returns -cnst or NULL if impossible
2611 static ir_node *const_negate(ir_node *cnst)
2613 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2614 dbg_info *dbgi = get_irn_dbg_info(cnst);
2615 ir_graph *irg = get_irn_irg(cnst);
2616 if (tv == tarval_bad) return NULL;
2617 return new_rd_Const(dbgi, irg, tv);
2621 * Do the AddSub optimization, then Transform
2622 * Constant folding on Phi
2623 * Sub(0,a) -> Minus(a)
2624 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2625 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2626 * Sub(Add(a, x), x) -> a
2627 * Sub(x, Add(x, a)) -> -a
2628 * Sub(x, Const) -> Add(x, -Const)
2630 static ir_node *transform_node_Sub(ir_node *n)
2636 n = transform_node_AddSub(n);
2638 a = get_Sub_left(n);
2639 b = get_Sub_right(n);
2641 mode = get_irn_mode(n);
2643 if (mode_is_int(mode)) {
2644 ir_mode *lmode = get_irn_mode(a);
2646 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2647 /* a Sub(a, NULL) is a hidden Conv */
2648 dbg_info *dbg = get_irn_dbg_info(n);
2649 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2650 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2654 if (mode == lmode &&
2655 get_mode_arithmetic(mode) == irma_twos_complement &&
2657 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2659 dbg_info *dbg = get_irn_dbg_info(n);
2660 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2661 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2667 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2669 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2670 if (mode_is_float(mode)) {
2671 ir_graph *irg = get_irn_irg(n);
2672 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2676 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2677 /* a - C -> a + (-C) */
2678 ir_node *cnst = const_negate(b);
2680 ir_node *block = get_nodes_block(n);
2681 dbg_info *dbgi = get_irn_dbg_info(n);
2683 n = new_rd_Add(dbgi, block, a, cnst, mode);
2684 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2689 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2690 dbg_info *dbg = get_irn_dbg_info(n);
2691 ir_node *block = get_nodes_block(n);
2692 ir_node *left = get_Minus_op(a);
2693 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2695 n = new_rd_Minus(dbg, block, add, mode);
2696 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2698 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2699 dbg_info *dbg = get_irn_dbg_info(n);
2700 ir_node *block = get_nodes_block(n);
2701 ir_node *right = get_Minus_op(b);
2703 n = new_rd_Add(dbg, block, a, right, mode);
2704 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2706 } else if (is_Sub(b)) {
2707 /* a - (b - c) -> a + (c - b)
2708 * -> (a - b) + c iff (b - c) is a pointer */
2709 dbg_info *s_dbg = get_irn_dbg_info(b);
2710 ir_node *s_left = get_Sub_left(b);
2711 ir_node *s_right = get_Sub_right(b);
2712 ir_mode *s_mode = get_irn_mode(b);
2713 if (mode_is_reference(s_mode)) {
2714 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2715 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2716 dbg_info *a_dbg = get_irn_dbg_info(n);
2719 s_right = new_r_Conv(lowest_block, s_right, mode);
2720 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2722 ir_node *s_block = get_nodes_block(b);
2723 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2724 dbg_info *a_dbg = get_irn_dbg_info(n);
2725 ir_node *a_block = get_nodes_block(n);
2727 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2729 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2732 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2733 ir_node *m_right = get_Mul_right(b);
2734 if (is_Const(m_right)) {
2735 ir_node *cnst2 = const_negate(m_right);
2736 if (cnst2 != NULL) {
2737 dbg_info *m_dbg = get_irn_dbg_info(b);
2738 ir_node *m_block = get_nodes_block(b);
2739 ir_node *m_left = get_Mul_left(b);
2740 ir_mode *m_mode = get_irn_mode(b);
2741 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2742 dbg_info *a_dbg = get_irn_dbg_info(n);
2743 ir_node *a_block = get_nodes_block(n);
2745 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2746 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2753 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2754 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2756 get_irn_dbg_info(n),
2760 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2763 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2764 ir_node *left = get_binop_left(a);
2765 ir_node *right = get_binop_right(a);
2767 /* FIXME: Does the Conv's work only for two complement or generally? */
2769 if (mode != get_irn_mode(right)) {
2770 /* This Sub is an effective Cast */
2771 right = new_r_Conv(get_nodes_block(n), right, mode);
2774 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2776 } else if (right == b) {
2777 if (mode != get_irn_mode(left)) {
2778 /* This Sub is an effective Cast */
2779 left = new_r_Conv(get_nodes_block(n), left, mode);
2782 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2786 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2787 ir_node *left = get_binop_left(b);
2788 ir_node *right = get_binop_right(b);
2790 /* FIXME: Does the Conv's work only for two complement or generally? */
2792 ir_mode *r_mode = get_irn_mode(right);
2794 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2795 if (mode != r_mode) {
2796 /* This Sub is an effective Cast */
2797 n = new_r_Conv(get_nodes_block(n), n, mode);
2799 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2801 } else if (right == a) {
2802 ir_mode *l_mode = get_irn_mode(left);
2804 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2805 if (mode != l_mode) {
2806 /* This Sub is an effective Cast */
2807 n = new_r_Conv(get_nodes_block(n), n, mode);
2809 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2813 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2814 ir_mode *mode = get_irn_mode(a);
2816 if (mode == get_irn_mode(b)) {
2818 ir_node *op_a = get_Conv_op(a);
2819 ir_node *op_b = get_Conv_op(b);
2821 /* check if it's allowed to skip the conv */
2822 ma = get_irn_mode(op_a);
2823 mb = get_irn_mode(op_b);
2825 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2826 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2829 set_Sub_right(n, b);
2835 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2836 if (!is_reassoc_running() && is_Mul(a)) {
2837 ir_node *ma = get_Mul_left(a);
2838 ir_node *mb = get_Mul_right(a);
2841 ir_node *blk = get_nodes_block(n);
2842 ir_graph *irg = get_irn_irg(n);
2844 get_irn_dbg_info(n),
2848 get_irn_dbg_info(n),
2851 new_r_Const(irg, get_mode_one(mode)),
2854 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2856 } else if (mb == b) {
2857 ir_node *blk = get_nodes_block(n);
2858 ir_graph *irg = get_irn_irg(n);
2860 get_irn_dbg_info(n),
2864 get_irn_dbg_info(n),
2867 new_r_Const(irg, get_mode_one(mode)),
2870 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2874 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2875 ir_node *x = get_Sub_left(a);
2876 ir_node *y = get_Sub_right(a);
2877 ir_node *blk = get_nodes_block(n);
2878 ir_mode *m_b = get_irn_mode(b);
2879 ir_mode *m_y = get_irn_mode(y);
2883 /* Determine the right mode for the Add. */
2886 else if (mode_is_reference(m_b))
2888 else if (mode_is_reference(m_y))
2892 * Both modes are different but none is reference,
2893 * happens for instance in SubP(SubP(P, Iu), Is).
2894 * We have two possibilities here: Cast or ignore.
2895 * Currently we ignore this case.
2900 add = new_r_Add(blk, y, b, add_mode);
2902 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2903 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2907 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2908 /* c - ~X = X + (c+1) */
2909 if (is_Const(a) && is_Not(b)) {
2910 ir_tarval *tv = get_Const_tarval(a);
2912 tv = tarval_add(tv, get_mode_one(mode));
2913 if (tv != tarval_bad) {
2914 ir_node *blk = get_nodes_block(n);
2915 ir_graph *irg = get_irn_irg(n);
2916 ir_node *c = new_r_Const(irg, tv);
2917 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2918 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2922 /* x-(x&y) = x & ~y */
2924 ir_node *and_left = get_And_left(b);
2925 ir_node *and_right = get_And_right(b);
2926 if (and_right == a) {
2927 ir_node *tmp = and_left;
2928 and_left = and_right;
2931 if (and_left == a) {
2932 dbg_info *dbgi = get_irn_dbg_info(n);
2933 ir_node *block = get_nodes_block(n);
2934 ir_mode *mode = get_irn_mode(n);
2935 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2936 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2945 * Several transformation done on n*n=2n bits mul.
2946 * These transformations must be done here because new nodes may be produced.
2948 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2951 ir_node *a = get_Mul_left(n);
2952 ir_node *b = get_Mul_right(n);
2953 ir_tarval *ta = value_of(a);
2954 ir_tarval *tb = value_of(b);
2955 ir_mode *smode = get_irn_mode(a);
2957 if (ta == get_mode_one(smode)) {
2958 /* (L)1 * (L)b = (L)b */
2959 ir_node *blk = get_nodes_block(n);
2960 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2961 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2964 else if (ta == get_mode_minus_one(smode)) {
2965 /* (L)-1 * (L)b = (L)b */
2966 ir_node *blk = get_nodes_block(n);
2967 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2968 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2969 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2972 if (tb == get_mode_one(smode)) {
2973 /* (L)a * (L)1 = (L)a */
2974 ir_node *blk = get_nodes_block(a);
2975 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2976 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2979 else if (tb == get_mode_minus_one(smode)) {
2980 /* (L)a * (L)-1 = (L)-a */
2981 ir_node *blk = get_nodes_block(n);
2982 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2983 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2991 * Transform Mul(a,-1) into -a.
2992 * Do constant evaluation of Phi nodes.
2993 * Do architecture dependent optimizations on Mul nodes
2995 static ir_node *transform_node_Mul(ir_node *n)
2997 ir_node *c, *oldn = n;
2998 ir_mode *mode = get_irn_mode(n);
2999 ir_node *a = get_Mul_left(n);
3000 ir_node *b = get_Mul_right(n);
3002 n = fold_constant_associativity(n, tarval_mul);
3006 if (mode != get_irn_mode(a))
3007 return transform_node_Mul2n(n, mode);
3009 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3011 if (mode_is_signed(mode)) {
3014 if (value_of(a) == get_mode_minus_one(mode))
3016 else if (value_of(b) == get_mode_minus_one(mode))
3019 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3020 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3025 if (is_Const(b)) { /* (-a) * const -> a * -const */
3026 ir_node *cnst = const_negate(b);
3028 dbg_info *dbgi = get_irn_dbg_info(n);
3029 ir_node *block = get_nodes_block(n);
3030 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3034 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3035 dbg_info *dbgi = get_irn_dbg_info(n);
3036 ir_node *block = get_nodes_block(n);
3037 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3040 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3041 ir_node *sub_l = get_Sub_left(b);
3042 ir_node *sub_r = get_Sub_right(b);
3043 dbg_info *dbgi = get_irn_dbg_info(n);
3044 ir_node *block = get_nodes_block(n);
3045 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3046 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3047 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3050 } else if (is_Minus(b)) {
3051 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3052 ir_node *sub_l = get_Sub_left(a);
3053 ir_node *sub_r = get_Sub_right(a);
3054 dbg_info *dbgi = get_irn_dbg_info(n);
3055 ir_node *block = get_nodes_block(n);
3056 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3057 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3058 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3061 } else if (is_Shl(a)) {
3062 ir_node *const shl_l = get_Shl_left(a);
3063 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3064 /* (1 << x) * b -> b << x */
3065 dbg_info *const dbgi = get_irn_dbg_info(n);
3066 ir_node *const block = get_nodes_block(n);
3067 ir_node *const shl_r = get_Shl_right(a);
3068 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3069 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3072 } else if (is_Shl(b)) {
3073 ir_node *const shl_l = get_Shl_left(b);
3074 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3075 /* a * (1 << x) -> a << x */
3076 dbg_info *const dbgi = get_irn_dbg_info(n);
3077 ir_node *const block = get_nodes_block(n);
3078 ir_node *const shl_r = get_Shl_right(b);
3079 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3080 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3084 if (get_mode_arithmetic(mode) == irma_ieee754
3085 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3087 ir_tarval *tv = get_Const_tarval(a);
3088 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3089 && !tarval_is_negative(tv)) {
3090 /* 2.0 * b = b + b */
3091 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3092 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3096 else if (is_Const(b)) {
3097 ir_tarval *tv = get_Const_tarval(b);
3098 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3099 && !tarval_is_negative(tv)) {
3100 /* a * 2.0 = a + a */
3101 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3102 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3107 return arch_dep_replace_mul_with_shifts(n);
3111 * Transform a Div Node.
3113 static ir_node *transform_node_Div(ir_node *n)
3115 ir_mode *mode = get_Div_resmode(n);
3116 ir_node *a = get_Div_left(n);
3117 ir_node *b = get_Div_right(n);
3119 const ir_node *dummy;
3121 if (mode_is_int(mode)) {
3122 if (is_Const(b) && is_const_Phi(a)) {
3123 /* check for Div(Phi, Const) */
3124 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3126 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3129 } else if (is_Const(a) && is_const_Phi(b)) {
3130 /* check for Div(Const, Phi) */
3131 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3133 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3136 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3137 /* check for Div(Phi, Phi) */
3138 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3140 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3145 if (a == b && value_not_zero(a, &dummy)) {
3146 ir_graph *irg = get_irn_irg(n);
3147 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3148 value = new_r_Const(irg, get_mode_one(mode));
3149 DBG_OPT_CSTEVAL(n, value);
3152 if (mode_is_signed(mode) && is_Const(b)) {
3153 ir_tarval *tv = get_Const_tarval(b);
3155 if (tv == get_mode_minus_one(mode)) {
3157 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3158 DBG_OPT_CSTEVAL(n, value);
3162 /* Try architecture dependent optimization */
3163 value = arch_dep_replace_div_by_const(n);
3166 assert(mode_is_float(mode));
3168 /* Optimize x/c to x*(1/c) */
3169 if (get_mode_arithmetic(mode) == irma_ieee754) {
3170 ir_tarval *tv = value_of(b);
3172 if (tv != tarval_bad) {
3173 int rem = tarval_fp_ops_enabled();
3176 * Floating point constant folding might be disabled here to
3178 * However, as we check for exact result, doing it is safe.
3181 tarval_enable_fp_ops(1);
3182 tv = tarval_div(get_mode_one(mode), tv);
3183 tarval_enable_fp_ops(rem);
3185 /* Do the transformation if the result is either exact or we are
3186 not using strict rules. */
3187 if (tv != tarval_bad &&
3188 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3189 ir_node *block = get_nodes_block(n);
3190 ir_graph *irg = get_irn_irg(block);
3191 ir_node *c = new_r_Const(irg, tv);
3192 dbg_info *dbgi = get_irn_dbg_info(n);
3193 value = new_rd_Mul(dbgi, block, a, c, mode);
3206 /* Turn Div into a tuple (mem, jmp, bad, value) */
3207 mem = get_Div_mem(n);
3208 blk = get_nodes_block(n);
3209 irg = get_irn_irg(blk);
3211 /* skip a potential Pin */
3212 mem = skip_Pin(mem);
3213 turn_into_tuple(n, pn_Div_max+1);
3214 set_Tuple_pred(n, pn_Div_M, mem);
3215 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3216 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3217 set_Tuple_pred(n, pn_Div_res, value);
3223 * Transform a Mod node.
3225 static ir_node *transform_node_Mod(ir_node *n)
3227 ir_mode *mode = get_Mod_resmode(n);
3228 ir_node *a = get_Mod_left(n);
3229 ir_node *b = get_Mod_right(n);
3234 if (is_Const(b) && is_const_Phi(a)) {
3235 /* check for Div(Phi, Const) */
3236 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3238 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3242 else if (is_Const(a) && is_const_Phi(b)) {
3243 /* check for Div(Const, Phi) */
3244 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3246 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3250 else if (is_const_Phi(a) && is_const_Phi(b)) {
3251 /* check for Div(Phi, Phi) */
3252 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3254 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3261 irg = get_irn_irg(n);
3262 if (tv != tarval_bad) {
3263 value = new_r_Const(irg, tv);
3265 DBG_OPT_CSTEVAL(n, value);
3268 ir_node *a = get_Mod_left(n);
3269 ir_node *b = get_Mod_right(n);
3270 const ir_node *dummy;
3272 if (a == b && value_not_zero(a, &dummy)) {
3273 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3274 value = new_r_Const(irg, get_mode_null(mode));
3275 DBG_OPT_CSTEVAL(n, value);
3278 if (mode_is_signed(mode) && is_Const(b)) {
3279 ir_tarval *tv = get_Const_tarval(b);
3281 if (tv == get_mode_minus_one(mode)) {
3283 value = new_r_Const(irg, get_mode_null(mode));
3284 DBG_OPT_CSTEVAL(n, value);
3288 /* Try architecture dependent optimization */
3289 value = arch_dep_replace_mod_by_const(n);
3298 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3299 mem = get_Mod_mem(n);
3300 blk = get_nodes_block(n);
3301 irg = get_irn_irg(blk);
3303 /* skip a potential Pin */
3304 mem = skip_Pin(mem);
3305 turn_into_tuple(n, pn_Mod_max+1);
3306 set_Tuple_pred(n, pn_Mod_M, mem);
3307 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3308 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3309 set_Tuple_pred(n, pn_Mod_res, value);
3315 * Transform a Cond node.
3317 * Replace the Cond by a Jmp if it branches on a constant
3320 static ir_node *transform_node_Cond(ir_node *n)
3322 ir_node *a = get_Cond_selector(n);
3323 ir_graph *irg = get_irn_irg(n);
3327 /* we need block info which is not available in floating irgs */
3328 if (get_irg_pinned(irg) == op_pin_state_floats)
3332 if (ta == tarval_bad && is_Cmp(a)) {
3333 /* try again with a direct call to compute_cmp, as we don't care
3334 * about the MODEB_LOWERED flag here */
3335 ta = compute_cmp_ext(a);
3338 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3339 /* It's a boolean Cond, branching on a boolean constant.
3340 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3341 ir_node *blk = get_nodes_block(n);
3342 jmp = new_r_Jmp(blk);
3343 turn_into_tuple(n, pn_Cond_max+1);
3344 if (ta == tarval_b_true) {
3345 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3346 set_Tuple_pred(n, pn_Cond_true, jmp);
3348 set_Tuple_pred(n, pn_Cond_false, jmp);
3349 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3351 /* We might generate an endless loop, so keep it alive. */
3352 add_End_keepalive(get_irg_end(irg), blk);
3353 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3358 static ir_node *transform_node_Switch(ir_node *n)
3360 ir_node *op = get_Switch_selector(n);
3361 ir_tarval *val = value_of(op);
3362 if (val != tarval_bad) {
3363 dbg_info *dbgi = get_irn_dbg_info(n);
3364 ir_graph *irg = get_irn_irg(n);
3365 unsigned n_outs = get_Switch_n_outs(n);
3366 ir_node *block = get_nodes_block(n);
3367 ir_node *bad = new_r_Bad(irg, mode_X);
3368 ir_node **in = XMALLOCN(ir_node*, n_outs);
3369 const ir_switch_table *table = get_Switch_table(n);
3370 size_t n_entries = ir_switch_table_get_n_entries(table);
3374 for (i = 0; i < n_entries; ++i) {
3375 const ir_switch_table_entry *entry
3376 = ir_switch_table_get_entry_const(table, i);
3377 ir_tarval *min = entry->min;
3378 ir_tarval *max = entry->max;
3381 if ((min == max && min == val)
3382 || (tarval_cmp(val, min) != ir_relation_less
3383 && tarval_cmp(val, max) != ir_relation_greater)) {
3388 for (o = 0; o < n_outs; ++o) {
3389 if (o == (unsigned)jmp_pn) {
3390 in[o] = new_rd_Jmp(dbgi, block);
3395 return new_r_Tuple(block, (int)n_outs, in);
3401 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3403 * - and, or, xor instead of &
3404 * - Shl, Shr, Shrs, rotl instead of >>
3405 * (with a special case for Or/Xor + Shrs)
3407 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3409 static ir_node *transform_node_shift_bitop(ir_node *n)
3411 ir_graph *irg = get_irn_irg(n);
3412 ir_node *right = get_binop_right(n);
3413 ir_mode *mode = get_irn_mode(n);
3415 ir_node *bitop_left;
3416 ir_node *bitop_right;
3425 ir_tarval *tv_shift;
3427 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3430 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3432 if (!is_Const(right))
3435 left = get_binop_left(n);
3436 op_left = get_irn_op(left);
3437 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3440 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3441 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3442 /* TODO: test if sign bit is affectes */
3446 bitop_right = get_binop_right(left);
3447 if (!is_Const(bitop_right))
3450 bitop_left = get_binop_left(left);
3452 block = get_nodes_block(n);
3453 dbgi = get_irn_dbg_info(n);
3454 tv1 = get_Const_tarval(bitop_right);
3455 tv2 = get_Const_tarval(right);
3457 assert(get_tarval_mode(tv1) == mode);
3460 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3461 tv_shift = tarval_shl(tv1, tv2);
3462 } else if (is_Shr(n)) {
3463 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3464 tv_shift = tarval_shr(tv1, tv2);
3465 } else if (is_Shrs(n)) {
3466 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3467 tv_shift = tarval_shrs(tv1, tv2);
3470 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3471 tv_shift = tarval_rotl(tv1, tv2);
3474 assert(get_tarval_mode(tv_shift) == mode);
3475 irg = get_irn_irg(n);
3476 new_const = new_r_Const(irg, tv_shift);
3478 if (op_left == op_And) {
3479 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3480 } else if (op_left == op_Or) {
3481 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3483 assert(op_left == op_Eor);
3484 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3493 static ir_node *transform_node_And(ir_node *n)
3495 ir_node *c, *oldn = n;
3496 ir_node *a = get_And_left(n);
3497 ir_node *b = get_And_right(n);
3500 n = fold_constant_associativity(n, tarval_and);
3504 if (is_Cmp(a) && is_Cmp(b)) {
3505 ir_node *a_left = get_Cmp_left(a);
3506 ir_node *a_right = get_Cmp_right(a);
3507 ir_node *b_left = get_Cmp_left(b);
3508 ir_node *b_right = get_Cmp_right(b);
3509 ir_relation a_relation = get_Cmp_relation(a);
3510 ir_relation b_relation = get_Cmp_relation(b);
3511 /* we can combine the relations of two compares with the same
3513 if (a_left == b_left && b_left == b_right) {
3514 dbg_info *dbgi = get_irn_dbg_info(n);
3515 ir_node *block = get_nodes_block(n);
3516 ir_relation new_relation = a_relation & b_relation;
3517 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3519 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3520 if (a_relation == b_relation && a_relation == ir_relation_equal
3521 && !mode_is_float(get_irn_mode(a_left))
3522 && !mode_is_float(get_irn_mode(b_left))) {
3523 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3524 dbg_info *dbgi = get_irn_dbg_info(n);
3525 ir_node *block = get_nodes_block(n);
3526 ir_mode *a_mode = get_irn_mode(a_left);
3527 ir_mode *b_mode = get_irn_mode(b_left);
3528 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3529 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3530 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3531 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3532 ir_graph *irg = get_irn_irg(n);
3533 ir_node *zero = create_zero_const(irg, b_mode);
3534 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3536 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3537 dbg_info *dbgi = get_irn_dbg_info(n);
3538 ir_node *block = get_nodes_block(n);
3539 ir_mode *a_mode = get_irn_mode(a_left);
3540 ir_mode *b_mode = get_irn_mode(b_left);
3541 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3542 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3543 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3544 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3545 ir_graph *irg = get_irn_irg(n);
3546 ir_node *zero = create_zero_const(irg, a_mode);
3547 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3552 mode = get_irn_mode(n);
3553 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3555 if (is_Or(a) || is_Or_Eor_Add(a)) {
3556 ir_node *or_left = get_binop_left(a);
3557 ir_node *or_right = get_binop_right(a);
3558 if (complement_values(or_left, b)) {
3559 /* (a|b) & ~a => b & ~a */
3560 dbg_info *dbgi = get_irn_dbg_info(n);
3561 ir_node *block = get_nodes_block(n);
3562 return new_rd_And(dbgi, block, or_right, b, mode);
3563 } else if (complement_values(or_right, b)) {
3564 /* (a|b) & ~b => a & ~b */
3565 dbg_info *dbgi = get_irn_dbg_info(n);
3566 ir_node *block = get_nodes_block(n);
3567 return new_rd_And(dbgi, block, or_left, b, mode);
3568 } else if (is_Not(b)) {
3569 ir_node *op = get_Not_op(b);
3571 ir_node *ba = get_And_left(op);
3572 ir_node *bb = get_And_right(op);
3574 /* it's enough to test the following cases due to normalization! */
3575 if (or_left == ba && or_right == bb) {
3576 /* (a|b) & ~(a&b) = a^b */
3577 ir_node *block = get_nodes_block(n);
3579 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3580 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3586 if (is_Or(b) || is_Or_Eor_Add(b)) {
3587 ir_node *or_left = get_binop_left(b);
3588 ir_node *or_right = get_binop_right(b);
3589 if (complement_values(or_left, a)) {
3590 /* (a|b) & ~a => b & ~a */
3591 dbg_info *dbgi = get_irn_dbg_info(n);
3592 ir_node *block = get_nodes_block(n);
3593 return new_rd_And(dbgi, block, or_right, a, mode);
3594 } else if (complement_values(or_right, a)) {
3595 /* (a|b) & ~b => a & ~b */
3596 dbg_info *dbgi = get_irn_dbg_info(n);
3597 ir_node *block = get_nodes_block(n);
3598 return new_rd_And(dbgi, block, or_left, a, mode);
3599 } else if (is_Not(a)) {
3600 ir_node *op = get_Not_op(a);
3602 ir_node *aa = get_And_left(op);
3603 ir_node *ab = get_And_right(op);
3605 /* it's enough to test the following cases due to normalization! */
3606 if (or_left == aa && or_right == ab) {
3607 /* (a|b) & ~(a&b) = a^b */
3608 ir_node *block = get_nodes_block(n);
3610 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3611 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3617 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3618 ir_node *al = get_binop_left(a);
3619 ir_node *ar = get_binop_right(a);
3622 /* (b ^ a) & b -> ~a & b */
3623 dbg_info *dbg = get_irn_dbg_info(n);
3624 ir_node *block = get_nodes_block(n);
3626 ar = new_rd_Not(dbg, block, ar, mode);
3627 n = new_rd_And(dbg, block, ar, b, mode);
3628 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3632 /* (a ^ b) & b -> ~a & b */
3633 dbg_info *dbg = get_irn_dbg_info(n);
3634 ir_node *block = get_nodes_block(n);
3636 al = new_rd_Not(dbg, block, al, mode);
3637 n = new_rd_And(dbg, block, al, b, mode);
3638 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3642 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3643 ir_node *bl = get_binop_left(b);
3644 ir_node *br = get_binop_right(b);
3647 /* a & (a ^ b) -> a & ~b */
3648 dbg_info *dbg = get_irn_dbg_info(n);
3649 ir_node *block = get_nodes_block(n);
3651 br = new_rd_Not(dbg, block, br, mode);
3652 n = new_rd_And(dbg, block, br, a, mode);
3653 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3657 /* a & (b ^ a) -> a & ~b */
3658 dbg_info *dbg = get_irn_dbg_info(n);
3659 ir_node *block = get_nodes_block(n);
3661 bl = new_rd_Not(dbg, block, bl, mode);
3662 n = new_rd_And(dbg, block, bl, a, mode);
3663 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3667 if (is_Not(a) && is_Not(b)) {
3668 /* ~a & ~b = ~(a|b) */
3669 ir_node *block = get_nodes_block(n);
3670 ir_mode *mode = get_irn_mode(n);
3674 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3675 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3676 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3681 vrp_attr *b_vrp = vrp_get_info(b);
3682 ir_tarval *a_val = get_Const_tarval(a);
3683 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3689 vrp_attr *a_vrp = vrp_get_info(a);
3690 ir_tarval *b_val = get_Const_tarval(b);
3691 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3696 n = transform_bitwise_distributive(n, transform_node_And);
3698 n = transform_node_bitop_shift(n);
3706 static ir_node *transform_node_Not(ir_node *n)
3708 ir_node *c, *oldn = n;
3709 ir_node *a = get_Not_op(n);
3710 ir_mode *mode = get_irn_mode(n);
3712 HANDLE_UNOP_PHI(tarval_not,a,c);
3714 /* check for a boolean Not */
3716 dbg_info *dbgi = get_irn_dbg_info(a);
3717 ir_node *block = get_nodes_block(a);
3718 ir_relation relation = get_Cmp_relation(a);
3719 relation = get_negated_relation(relation);
3720 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3721 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3725 /* normalize ~(a ^ b) => a ^ ~b */
3726 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3727 dbg_info *dbg = get_irn_dbg_info(n);
3728 ir_node *block = get_nodes_block(n);
3729 ir_node *eor_right = get_binop_right(a);
3730 ir_node *eor_left = get_binop_left(a);
3731 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3732 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3736 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3737 if (is_Minus(a)) { /* ~-x -> x + -1 */
3738 dbg_info *dbg = get_irn_dbg_info(n);
3739 ir_graph *irg = get_irn_irg(n);
3740 ir_node *block = get_nodes_block(n);
3741 ir_node *add_l = get_Minus_op(a);
3742 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3743 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3744 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3745 ir_node *add_r = get_binop_right(a);
3746 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3747 /* ~(x + -1) = -x */
3748 ir_node *op = get_binop_left(a);
3749 ir_node *blk = get_nodes_block(n);
3750 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3751 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3759 * Transform a Minus.
3763 * -(a >>u (size-1)) = a >>s (size-1)
3764 * -(a >>s (size-1)) = a >>u (size-1)
3765 * -(a * const) -> a * -const
3767 static ir_node *transform_node_Minus(ir_node *n)
3769 ir_node *c, *oldn = n;
3770 ir_node *a = get_Minus_op(n);
3773 HANDLE_UNOP_PHI(tarval_neg,a,c);
3775 mode = get_irn_mode(a);
3776 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3777 /* the following rules are only to twos-complement */
3780 ir_node *op = get_Not_op(a);
3781 ir_tarval *tv = get_mode_one(mode);
3782 ir_node *blk = get_nodes_block(n);
3783 ir_graph *irg = get_irn_irg(blk);
3784 ir_node *c = new_r_Const(irg, tv);
3785 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3786 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3790 ir_node *c = get_Shr_right(a);
3793 ir_tarval *tv = get_Const_tarval(c);
3795 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3796 /* -(a >>u (size-1)) = a >>s (size-1) */
3797 ir_node *v = get_Shr_left(a);
3799 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3800 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3806 ir_node *c = get_Shrs_right(a);
3809 ir_tarval *tv = get_Const_tarval(c);
3811 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3812 /* -(a >>s (size-1)) = a >>u (size-1) */
3813 ir_node *v = get_Shrs_left(a);
3815 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3816 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3823 /* - (a-b) = b - a */
3824 ir_node *la = get_Sub_left(a);
3825 ir_node *ra = get_Sub_right(a);
3826 ir_node *blk = get_nodes_block(n);
3828 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3829 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3833 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3834 ir_node *mul_l = get_Mul_left(a);
3835 ir_node *mul_r = get_Mul_right(a);
3836 ir_tarval *tv = value_of(mul_r);
3837 if (tv != tarval_bad) {
3838 tv = tarval_neg(tv);
3839 if (tv != tarval_bad) {
3840 ir_graph *irg = get_irn_irg(n);
3841 ir_node *cnst = new_r_Const(irg, tv);
3842 dbg_info *dbg = get_irn_dbg_info(a);
3843 ir_node *block = get_nodes_block(a);
3844 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3845 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3855 * Transform a Proj(Load) with a non-null address.
3857 static ir_node *transform_node_Proj_Load(ir_node *proj)
3859 if (get_irn_mode(proj) == mode_X) {
3860 ir_node *load = get_Proj_pred(proj);
3862 /* get the Load address */
3863 const ir_node *addr = get_Load_ptr(load);
3864 const ir_node *confirm;
3866 if (value_not_null(addr, &confirm)) {
3867 if (confirm == NULL) {
3868 /* this node may float if it did not depend on a Confirm */
3869 set_irn_pinned(load, op_pin_state_floats);
3871 if (get_Proj_proj(proj) == pn_Load_X_except) {
3872 ir_graph *irg = get_irn_irg(proj);
3873 DBG_OPT_EXC_REM(proj);
3874 return new_r_Bad(irg, mode_X);
3876 ir_node *blk = get_nodes_block(load);
3877 return new_r_Jmp(blk);
3885 * Transform a Proj(Store) with a non-null address.
3887 static ir_node *transform_node_Proj_Store(ir_node *proj)
3889 if (get_irn_mode(proj) == mode_X) {
3890 ir_node *store = get_Proj_pred(proj);
3892 /* get the load/store address */
3893 const ir_node *addr = get_Store_ptr(store);
3894 const ir_node *confirm;
3896 if (value_not_null(addr, &confirm)) {
3897 if (confirm == NULL) {
3898 /* this node may float if it did not depend on a Confirm */
3899 set_irn_pinned(store, op_pin_state_floats);
3901 if (get_Proj_proj(proj) == pn_Store_X_except) {
3902 ir_graph *irg = get_irn_irg(proj);
3903 DBG_OPT_EXC_REM(proj);
3904 return new_r_Bad(irg, mode_X);
3906 ir_node *blk = get_nodes_block(store);
3907 return new_r_Jmp(blk);
3915 * Transform a Proj(Div) with a non-zero value.
3916 * Removes the exceptions and routes the memory to the NoMem node.
3918 static ir_node *transform_node_Proj_Div(ir_node *proj)
3920 ir_node *div = get_Proj_pred(proj);
3921 ir_node *b = get_Div_right(div);
3922 ir_node *res, *new_mem;
3923 const ir_node *confirm;
3926 if (value_not_zero(b, &confirm)) {
3927 /* div(x, y) && y != 0 */
3928 if (confirm == NULL) {
3929 /* we are sure we have a Const != 0 */
3930 new_mem = get_Div_mem(div);
3931 new_mem = skip_Pin(new_mem);
3932 set_Div_mem(div, new_mem);
3933 set_irn_pinned(div, op_pin_state_floats);
3936 proj_nr = get_Proj_proj(proj);
3938 case pn_Div_X_regular:
3939 return new_r_Jmp(get_nodes_block(div));
3941 case pn_Div_X_except: {
3942 ir_graph *irg = get_irn_irg(proj);
3943 /* we found an exception handler, remove it */
3944 DBG_OPT_EXC_REM(proj);
3945 return new_r_Bad(irg, mode_X);
3949 ir_graph *irg = get_irn_irg(proj);
3950 res = get_Div_mem(div);
3951 new_mem = get_irg_no_mem(irg);
3954 /* This node can only float up to the Confirm block */
3955 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3957 set_irn_pinned(div, op_pin_state_floats);
3958 /* this is a Div without exception, we can remove the memory edge */
3959 set_Div_mem(div, new_mem);
3968 * Transform a Proj(Mod) with a non-zero value.
3969 * Removes the exceptions and routes the memory to the NoMem node.
3971 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3973 ir_node *mod = get_Proj_pred(proj);
3974 ir_node *b = get_Mod_right(mod);
3975 ir_node *res, *new_mem;
3976 const ir_node *confirm;
3979 if (value_not_zero(b, &confirm)) {
3980 /* mod(x, y) && y != 0 */
3981 proj_nr = get_Proj_proj(proj);
3983 if (confirm == NULL) {
3984 /* we are sure we have a Const != 0 */
3985 new_mem = get_Mod_mem(mod);
3986 new_mem = skip_Pin(new_mem);
3987 set_Mod_mem(mod, new_mem);
3988 set_irn_pinned(mod, op_pin_state_floats);
3993 case pn_Mod_X_regular:
3994 return new_r_Jmp(get_nodes_block(mod));
3996 case pn_Mod_X_except: {
3997 ir_graph *irg = get_irn_irg(proj);
3998 /* we found an exception handler, remove it */
3999 DBG_OPT_EXC_REM(proj);
4000 return new_r_Bad(irg, mode_X);
4004 ir_graph *irg = get_irn_irg(proj);
4005 res = get_Mod_mem(mod);
4006 new_mem = get_irg_no_mem(irg);
4009 /* This node can only float up to the Confirm block */
4010 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4012 /* this is a Mod without exception, we can remove the memory edge */
4013 set_Mod_mem(mod, new_mem);
4017 if (get_Mod_left(mod) == b) {
4018 /* a % a = 0 if a != 0 */
4019 ir_graph *irg = get_irn_irg(proj);
4020 ir_mode *mode = get_irn_mode(proj);
4021 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4023 DBG_OPT_CSTEVAL(mod, res);
4032 * return true if the operation returns a value with exactly 1 bit set
4034 static bool is_single_bit(const ir_node *node)
4036 /* a first implementation, could be extended with vrp and others... */
4038 ir_node *shl_l = get_Shl_left(node);
4039 ir_mode *mode = get_irn_mode(node);
4040 int modulo = get_mode_modulo_shift(mode);
4041 /* this works if we shift a 1 and we have modulo shift */
4042 if (is_Const(shl_l) && is_Const_one(shl_l)
4043 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4046 } else if (is_Const(node)) {
4047 ir_tarval *tv = get_Const_tarval(node);
4048 return tarval_is_single_bit(tv);
4054 * checks if node just flips a bit in another node and returns that other node
4055 * if so. @p tv should be a value having just 1 bit set
4057 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4060 return get_Not_op(node);
4062 ir_node *right = get_Eor_right(node);
4063 if (is_Const(right)) {
4064 ir_tarval *right_tv = get_Const_tarval(right);
4065 ir_mode *mode = get_irn_mode(node);
4066 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4067 return get_Eor_left(node);
4074 * Normalizes and optimizes Cmp nodes.
4076 static ir_node *transform_node_Cmp(ir_node *n)
4078 ir_node *left = get_Cmp_left(n);
4079 ir_node *right = get_Cmp_right(n);
4080 ir_mode *mode = get_irn_mode(left);
4081 ir_tarval *tv = NULL;
4082 bool changed = false;
4083 bool changedc = false;
4084 ir_relation relation = get_Cmp_relation(n);
4085 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4087 /* mask out impossible relations */
4088 ir_relation new_relation = relation & possible;
4089 if (new_relation != relation) {
4090 relation = new_relation;
4094 /* Remove unnecessary conversions */
4095 if (!mode_is_float(mode)
4096 || be_get_backend_param()->mode_float_arithmetic == NULL) {
4097 if (is_Conv(left) && is_Conv(right)) {
4098 ir_node *op_left = get_Conv_op(left);
4099 ir_node *op_right = get_Conv_op(right);
4100 ir_mode *mode_left = get_irn_mode(op_left);
4101 ir_mode *mode_right = get_irn_mode(op_right);
4103 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4104 && mode_left != mode_b && mode_right != mode_b) {
4105 ir_node *block = get_nodes_block(n);
4107 if (mode_left == mode_right) {
4111 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4112 } else if (smaller_mode(mode_left, mode_right)) {
4113 left = new_r_Conv(block, op_left, mode_right);
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4117 } else if (smaller_mode(mode_right, mode_left)) {
4119 right = new_r_Conv(block, op_right, mode_left);
4121 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4123 mode = get_irn_mode(left);
4126 if (is_Conv(left) && is_Const(right)) {
4127 ir_node *op_left = get_Conv_op(left);
4128 ir_mode *mode_left = get_irn_mode(op_left);
4129 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4130 ir_tarval *tv = get_Const_tarval(right);
4131 tarval_int_overflow_mode_t last_mode
4132 = tarval_get_integer_overflow_mode();
4134 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4135 new_tv = tarval_convert_to(tv, mode_left);
4136 tarval_set_integer_overflow_mode(last_mode);
4137 if (new_tv != tarval_bad) {
4138 ir_graph *irg = get_irn_irg(n);
4140 right = new_r_Const(irg, new_tv);
4141 mode = get_irn_mode(left);
4143 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4150 * Optimize -a CMP -b into b CMP a.
4151 * This works only for modes where unary Minus cannot Overflow.
4152 * Note that two-complement integers can Overflow so it will NOT work.
4154 if (!mode_overflow_on_unary_Minus(mode) &&
4155 is_Minus(left) && is_Minus(right)) {
4156 left = get_Minus_op(left);
4157 right = get_Minus_op(right);
4158 relation = get_inversed_relation(relation);
4160 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4163 /* remove operation on both sides if possible */
4164 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4166 * The following operations are NOT safe for floating point operations, for instance
4167 * 1.0 + inf == 2.0 + inf, =/=> x == y
4169 if (mode_is_int(mode)) {
4170 unsigned lop = get_irn_opcode(left);
4172 if (lop == get_irn_opcode(right)) {
4173 ir_node *ll, *lr, *rl, *rr;
4175 /* same operation on both sides, try to remove */
4179 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4180 left = get_unop_op(left);
4181 right = get_unop_op(right);
4183 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4186 ll = get_Add_left(left);
4187 lr = get_Add_right(left);
4188 rl = get_Add_left(right);
4189 rr = get_Add_right(right);
4192 /* X + a CMP X + b ==> a CMP b */
4196 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4197 } else if (ll == rr) {
4198 /* X + a CMP b + X ==> a CMP b */
4202 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4203 } else if (lr == rl) {
4204 /* a + X CMP X + b ==> a CMP b */
4208 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4209 } else if (lr == rr) {
4210 /* a + X CMP b + X ==> a CMP b */
4214 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4218 ll = get_Sub_left(left);
4219 lr = get_Sub_right(left);
4220 rl = get_Sub_left(right);
4221 rr = get_Sub_right(right);
4224 /* X - a CMP X - b ==> a CMP b */
4228 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4229 } else if (lr == rr) {
4230 /* a - X CMP b - X ==> a CMP b */
4234 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4238 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4239 /* a ROTL X CMP b ROTL X ==> a CMP b */
4240 left = get_Rotl_left(left);
4241 right = get_Rotl_left(right);
4243 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4251 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4252 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4253 ir_node *ll = get_binop_left(left);
4254 ir_node *lr = get_binop_right(left);
4256 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4262 ir_graph *irg = get_irn_irg(n);
4264 right = create_zero_const(irg, mode);
4266 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4269 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4270 ir_node *rl = get_binop_left(right);
4271 ir_node *rr = get_binop_right(right);
4273 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4279 ir_graph *irg = get_irn_irg(n);
4281 right = create_zero_const(irg, mode);
4283 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4287 if (is_And(left) && is_Const(right)) {
4288 ir_node *ll = get_binop_left(left);
4289 ir_node *lr = get_binop_right(left);
4290 if (is_Shr(ll) && is_Const(lr)) {
4291 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4292 ir_node *block = get_nodes_block(n);
4293 ir_mode *mode = get_irn_mode(left);
4295 ir_node *llr = get_Shr_right(ll);
4296 if (is_Const(llr)) {
4297 dbg_info *dbg = get_irn_dbg_info(left);
4298 ir_graph *irg = get_irn_irg(left);
4300 ir_tarval *c1 = get_Const_tarval(llr);
4301 ir_tarval *c2 = get_Const_tarval(lr);
4302 ir_tarval *c3 = get_Const_tarval(right);
4303 ir_tarval *mask = tarval_shl(c2, c1);
4304 ir_tarval *value = tarval_shl(c3, c1);
4306 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4307 right = new_r_Const(irg, value);
4312 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4314 if (is_Const(right) && is_Const_null(right) &&
4315 (is_Eor(left) || is_Or_Eor_Add(left))) {
4316 right = get_Eor_right(left);
4317 left = get_Eor_left(left);
4323 if (mode_is_int(mode) && is_And(left)) {
4324 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4325 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4326 if (relation == ir_relation_equal
4327 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4328 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4329 ir_node *and0 = get_And_left(left);
4330 ir_node *and1 = get_And_right(left);
4331 if (and1 == right) {
4332 ir_node *tmp = and0;
4336 if (and0 == right && is_single_bit(and0)) {
4337 ir_graph *irg = get_irn_irg(n);
4339 relation == ir_relation_equal ? ir_relation_less_greater
4340 : ir_relation_equal;
4341 right = create_zero_const(irg, mode);
4347 if (is_Const(right) && is_Const_null(right) &&
4348 (relation == ir_relation_equal
4349 || (relation == ir_relation_less_greater)
4350 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4352 /* instead of flipping the bit before the bit-test operation negate
4354 ir_node *and0 = get_And_left(left);
4355 ir_node *and1 = get_And_right(left);
4356 if (is_Const(and1)) {
4357 ir_tarval *tv = get_Const_tarval(and1);
4358 if (tarval_is_single_bit(tv)) {
4359 ir_node *flipped = flips_bit(and0, tv);
4360 if (flipped != NULL) {
4361 dbg_info *dbgi = get_irn_dbg_info(left);
4362 ir_node *block = get_nodes_block(left);
4363 relation = get_negated_relation(relation);
4364 left = new_rd_And(dbgi, block, flipped, and1, mode);
4373 /* replace mode_b compares with ands/ors */
4374 if (mode == mode_b) {
4375 ir_node *block = get_nodes_block(n);
4379 case ir_relation_less_equal:
4380 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4382 case ir_relation_less:
4383 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4385 case ir_relation_greater_equal:
4386 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4388 case ir_relation_greater:
4389 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4391 case ir_relation_less_greater:
4392 bres = new_r_Eor(block, left, right, mode_b);
4394 case ir_relation_equal:
4395 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4398 #ifdef DEBUG_libfirm
4399 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4404 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4410 * First step: normalize the compare op
4411 * by placing the constant on the right side
4412 * or moving the lower address node to the left.
4414 if (!operands_are_normalized(left, right)) {
4419 relation = get_inversed_relation(relation);
4424 * Second step: Try to reduce the magnitude
4425 * of a constant. This may help to generate better code
4426 * later and may help to normalize more compares.
4427 * Of course this is only possible for integer values.
4429 tv = value_of(right);
4430 if (tv != tarval_bad) {
4431 ir_mode *mode = get_irn_mode(right);
4433 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4434 * cmp(ct,c2) | cmp(cf,c2) | result
4435 * -----------|------------|--------
4436 * true | true | True
4437 * false | false | False
4439 * false | true | not(x)
4442 ir_node *mux_true = get_Mux_true(left);
4443 ir_node *mux_false = get_Mux_false(left);
4444 if (is_Const(mux_true) && is_Const(mux_false)) {
4445 /* we can fold true/false constant separately */
4446 ir_tarval *tv_true = get_Const_tarval(mux_true);
4447 ir_tarval *tv_false = get_Const_tarval(mux_false);
4448 ir_relation r_true = tarval_cmp(tv_true, tv);
4449 ir_relation r_false = tarval_cmp(tv_false, tv);
4450 if (r_true != ir_relation_false
4451 || r_false != ir_relation_false) {
4452 bool rel_true = (r_true & relation) != 0;
4453 bool rel_false = (r_false & relation) != 0;
4454 ir_node *cond = get_Mux_sel(left);
4455 if (rel_true == rel_false) {
4456 relation = rel_true ? ir_relation_true
4457 : ir_relation_false;
4458 } else if (rel_true) {
4461 dbg_info *dbgi = get_irn_dbg_info(n);
4462 ir_node *block = get_nodes_block(n);
4463 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4470 /* TODO extend to arbitrary constants */
4471 if (is_Conv(left) && tarval_is_null(tv)) {
4472 ir_node *op = get_Conv_op(left);
4473 ir_mode *op_mode = get_irn_mode(op);
4476 * UpConv(x) REL 0 ==> x REL 0
4477 * Don't do this for float values as it's unclear whether it is a
4478 * win. (on the other side it makes detection/creation of fabs hard)
4480 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4481 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4482 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4483 !mode_is_float(mode)) {
4484 tv = get_mode_null(op_mode);
4488 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4492 if (tv != tarval_bad) {
4493 /* the following optimization is possible on modes without Overflow
4494 * on Unary Minus or on == and !=:
4495 * -a CMP c ==> a swap(CMP) -c
4497 * Beware: for two-complement Overflow may occur, so only == and != can
4498 * be optimized, see this:
4499 * -MININT < 0 =/=> MININT > 0 !!!
4501 if (is_Minus(left) &&
4502 (!mode_overflow_on_unary_Minus(mode) ||
4503 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4504 tv = tarval_neg(tv);
4506 if (tv != tarval_bad) {
4507 left = get_Minus_op(left);
4508 relation = get_inversed_relation(relation);
4510 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4512 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4513 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4514 tv = tarval_not(tv);
4516 if (tv != tarval_bad) {
4517 left = get_Not_op(left);
4519 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4523 /* for integer modes, we have more */
4524 if (mode_is_int(mode) && !is_Const(left)) {
4525 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4526 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4527 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4528 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4530 if (tv != tarval_bad) {
4531 relation ^= ir_relation_equal;
4533 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4536 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4537 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4538 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4539 tv = tarval_add(tv, get_mode_one(mode));
4541 if (tv != tarval_bad) {
4542 relation ^= ir_relation_equal;
4544 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4548 /* the following reassociations work only for == and != */
4549 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4550 if (tv != tarval_bad) {
4551 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4553 ir_node *c1 = get_Sub_right(left);
4554 ir_tarval *tv2 = value_of(c1);
4556 if (tv2 != tarval_bad) {
4557 tv2 = tarval_add(tv, value_of(c1));
4559 if (tv2 != tarval_bad) {
4560 left = get_Sub_left(left);
4563 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4567 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4568 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4569 ir_node *a_l = get_binop_left(left);
4570 ir_node *a_r = get_binop_right(left);
4574 if (is_Const(a_l)) {
4576 tv2 = value_of(a_l);
4579 tv2 = value_of(a_r);
4582 if (tv2 != tarval_bad) {
4583 tv2 = tarval_sub(tv, tv2, NULL);
4585 if (tv2 != tarval_bad) {
4589 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4593 /* -a == c ==> a == -c, -a != c ==> a != -c */
4594 else if (is_Minus(left)) {
4595 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4597 if (tv2 != tarval_bad) {
4598 left = get_Minus_op(left);
4601 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4608 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4609 switch (get_irn_opcode(left)) {
4613 c1 = get_And_right(left);
4616 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4617 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4619 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4621 /* TODO: move to constant evaluation */
4622 ir_graph *irg = get_irn_irg(n);
4623 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4624 c1 = new_r_Const(irg, tv);
4625 DBG_OPT_CSTEVAL(n, c1);
4629 if (tarval_is_single_bit(tv)) {
4631 * optimization for AND:
4633 * And(x, C) == C ==> And(x, C) != 0
4634 * And(x, C) != C ==> And(X, C) == 0
4636 * if C is a single Bit constant.
4639 /* check for Constant's match. We have check hare the tarvals,
4640 because our const might be changed */
4641 if (get_Const_tarval(c1) == tv) {
4642 /* fine: do the transformation */
4643 tv = get_mode_null(get_tarval_mode(tv));
4644 relation ^= ir_relation_less_equal_greater;
4646 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4652 c1 = get_Or_right(left);
4653 if (is_Const(c1) && tarval_is_null(tv)) {
4655 * Or(x, C) == 0 && C != 0 ==> FALSE
4656 * Or(x, C) != 0 && C != 0 ==> TRUE
4658 if (! tarval_is_null(get_Const_tarval(c1))) {
4659 /* TODO: move to constant evaluation */
4660 ir_graph *irg = get_irn_irg(n);
4661 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4662 c1 = new_r_Const(irg, tv);
4663 DBG_OPT_CSTEVAL(n, c1);
4670 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4672 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4675 c1 = get_Shl_right(left);
4677 ir_graph *irg = get_irn_irg(c1);
4678 ir_tarval *tv1 = get_Const_tarval(c1);
4679 ir_mode *mode = get_irn_mode(left);
4680 ir_tarval *minus1 = get_mode_all_one(mode);
4681 ir_tarval *amask = tarval_shr(minus1, tv1);
4682 ir_tarval *cmask = tarval_shl(minus1, tv1);
4685 if (tarval_and(tv, cmask) != tv) {
4686 /* condition not met */
4687 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4688 c1 = new_r_Const(irg, tv);
4689 DBG_OPT_CSTEVAL(n, c1);
4692 sl = get_Shl_left(left);
4693 blk = get_nodes_block(n);
4694 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4695 tv = tarval_shr(tv, tv1);
4697 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4702 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4704 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4707 c1 = get_Shr_right(left);
4709 ir_graph *irg = get_irn_irg(c1);
4710 ir_tarval *tv1 = get_Const_tarval(c1);
4711 ir_mode *mode = get_irn_mode(left);
4712 ir_tarval *minus1 = get_mode_all_one(mode);
4713 ir_tarval *amask = tarval_shl(minus1, tv1);
4714 ir_tarval *cmask = tarval_shr(minus1, tv1);
4717 if (tarval_and(tv, cmask) != tv) {
4718 /* condition not met */
4719 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4720 c1 = new_r_Const(irg, tv);
4721 DBG_OPT_CSTEVAL(n, c1);
4724 sl = get_Shr_left(left);
4725 blk = get_nodes_block(n);
4726 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4727 tv = tarval_shl(tv, tv1);
4729 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4734 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4736 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4739 c1 = get_Shrs_right(left);
4741 ir_graph *irg = get_irn_irg(c1);
4742 ir_tarval *tv1 = get_Const_tarval(c1);
4743 ir_mode *mode = get_irn_mode(left);
4744 ir_tarval *minus1 = get_mode_all_one(mode);
4745 ir_tarval *amask = tarval_shl(minus1, tv1);
4746 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4749 cond = tarval_sub(cond, tv1, NULL);
4750 cond = tarval_shrs(tv, cond);
4752 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4753 /* condition not met */
4754 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4755 c1 = new_r_Const(irg, tv);
4756 DBG_OPT_CSTEVAL(n, c1);
4759 sl = get_Shrs_left(left);
4760 blk = get_nodes_block(n);
4761 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4762 tv = tarval_shl(tv, tv1);
4764 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4772 if (changedc) { /* need a new Const */
4773 ir_graph *irg = get_irn_irg(n);
4774 right = new_r_Const(irg, tv);
4778 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4779 ir_node *op = get_Proj_pred(left);
4781 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4782 ir_node *c = get_binop_right(op);
4785 ir_tarval *tv = get_Const_tarval(c);
4787 if (tarval_is_single_bit(tv)) {
4788 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4789 ir_node *v = get_binop_left(op);
4790 ir_node *blk = get_nodes_block(op);
4791 ir_graph *irg = get_irn_irg(op);
4792 ir_mode *mode = get_irn_mode(v);
4794 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4795 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4797 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4804 dbg_info *dbgi = get_irn_dbg_info(n);
4805 ir_node *block = get_nodes_block(n);
4807 /* create a new compare */
4808 n = new_rd_Cmp(dbgi, block, left, right, relation);
4815 * Optimize CopyB(mem, x, x) into a Nop.
4817 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4819 ir_node *copyb = get_Proj_pred(proj);
4820 ir_node *a = get_CopyB_dst(copyb);
4821 ir_node *b = get_CopyB_src(copyb);
4824 switch (get_Proj_proj(proj)) {
4825 case pn_CopyB_X_regular:
4826 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4827 DBG_OPT_EXC_REM(proj);
4828 proj = new_r_Jmp(get_nodes_block(copyb));
4830 case pn_CopyB_X_except: {
4831 ir_graph *irg = get_irn_irg(proj);
4832 DBG_OPT_EXC_REM(proj);
4833 proj = new_r_Bad(irg, mode_X);
4844 * Optimize Bounds(idx, idx, upper) into idx.
4846 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4848 ir_node *oldn = proj;
4849 ir_node *bound = get_Proj_pred(proj);
4850 ir_node *idx = get_Bound_index(bound);
4851 ir_node *pred = skip_Proj(idx);
4854 if (idx == get_Bound_lower(bound))
4856 else if (is_Bound(pred)) {
4858 * idx was Bounds checked previously, it is still valid if
4859 * lower <= pred_lower && pred_upper <= upper.
4861 ir_node *lower = get_Bound_lower(bound);
4862 ir_node *upper = get_Bound_upper(bound);
4863 if (get_Bound_lower(pred) == lower &&
4864 get_Bound_upper(pred) == upper) {
4866 * One could expect that we simply return the previous
4867 * Bound here. However, this would be wrong, as we could
4868 * add an exception Proj to a new location then.
4869 * So, we must turn in into a tuple.
4875 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4876 switch (get_Proj_proj(proj)) {
4878 DBG_OPT_EXC_REM(proj);
4879 proj = get_Bound_mem(bound);
4881 case pn_Bound_X_except:
4882 DBG_OPT_EXC_REM(proj);
4883 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4887 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4889 case pn_Bound_X_regular:
4890 DBG_OPT_EXC_REM(proj);
4891 proj = new_r_Jmp(get_nodes_block(bound));
4901 * Does all optimizations on nodes that must be done on its Projs
4902 * because of creating new nodes.
4904 static ir_node *transform_node_Proj(ir_node *proj)
4906 ir_node *n = get_Proj_pred(proj);
4908 if (n->op->ops.transform_node_Proj)
4909 return n->op->ops.transform_node_Proj(proj);
4914 * Test whether a block is unreachable
4915 * Note: That this only returns true when
4916 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4917 * This is important, as you easily end up producing invalid constructs in the
4918 * unreachable code when optimizing away edges into the unreachable code.
4919 * So only set this flag when you iterate localopts to the fixpoint.
4920 * When you reach the fixpoint then all unreachable code is dead
4921 * (= can't be reached by firm edges) and you won't see the invalid constructs
4924 static bool is_block_unreachable(const ir_node *block)
4926 const ir_graph *irg = get_irn_irg(block);
4927 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4929 return get_Block_dom_depth(block) < 0;
4932 static ir_node *transform_node_Block(ir_node *block)
4934 ir_graph *irg = get_irn_irg(block);
4935 int arity = get_irn_arity(block);
4936 ir_node *bad = NULL;
4939 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4942 for (i = 0; i < arity; ++i) {
4943 ir_node *const pred = get_Block_cfgpred(block, i);
4944 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4947 bad = new_r_Bad(irg, mode_X);
4948 set_irn_n(block, i, bad);
4954 static ir_node *transform_node_Phi(ir_node *phi)
4956 int n = get_irn_arity(phi);
4957 ir_mode *mode = get_irn_mode(phi);
4958 ir_node *block = get_nodes_block(phi);
4959 ir_graph *irg = get_irn_irg(phi);
4960 ir_node *bad = NULL;
4963 /* Set phi-operands for bad-block inputs to bad */
4964 for (i = 0; i < n; ++i) {
4965 if (!is_Bad(get_Phi_pred(phi, i))) {
4966 ir_node *pred = get_Block_cfgpred(block, i);
4967 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4969 bad = new_r_Bad(irg, mode);
4970 set_irn_n(phi, i, bad);
4975 /* Move Pin nodes down through Phi nodes. */
4976 if (mode == mode_M) {
4977 n = get_irn_arity(phi);
4979 /* Beware of Phi0 */
4983 bool has_pin = false;
4985 NEW_ARR_A(ir_node *, in, n);
4987 for (i = 0; i < n; ++i) {
4988 ir_node *pred = get_irn_n(phi, i);
4991 in[i] = get_Pin_op(pred);
4993 } else if (is_Bad(pred)) {
5003 /* Move the Pin nodes "behind" the Phi. */
5004 new_phi = new_r_Phi(block, n, in, mode_M);
5005 return new_r_Pin(block, new_phi);
5008 /* Move Confirms down through Phi nodes. */
5009 else if (mode_is_reference(mode)) {
5010 n = get_irn_arity(phi);
5012 /* Beware of Phi0 */
5014 ir_node *pred = get_irn_n(phi, 0);
5015 ir_node *bound, *new_phi, **in;
5016 ir_relation relation;
5017 bool has_confirm = false;
5019 if (! is_Confirm(pred))
5022 bound = get_Confirm_bound(pred);
5023 relation = get_Confirm_relation(pred);
5025 NEW_ARR_A(ir_node *, in, n);
5026 in[0] = get_Confirm_value(pred);
5028 for (i = 1; i < n; ++i) {
5029 pred = get_irn_n(phi, i);
5031 if (is_Confirm(pred) &&
5032 get_Confirm_bound(pred) == bound &&
5033 get_Confirm_relation(pred) == relation) {
5034 in[i] = get_Confirm_value(pred);
5036 } else if (is_Bad(pred)) {
5046 /* move the Confirm nodes "behind" the Phi */
5047 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5048 return new_r_Confirm(block, new_phi, bound, relation);
5055 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5057 * Should be moved to reassociation?
5059 static ir_node *transform_node_shift(ir_node *n)
5061 ir_node *left, *right;
5063 ir_mode *count_mode;
5064 ir_tarval *tv1, *tv2, *res;
5065 ir_node *in[2], *irn, *block;
5069 left = get_binop_left(n);
5071 /* different operations */
5072 if (get_irn_op(left) != get_irn_op(n))
5075 right = get_binop_right(n);
5076 tv1 = value_of(right);
5077 if (tv1 == tarval_bad)
5080 tv2 = value_of(get_binop_right(left));
5081 if (tv2 == tarval_bad)
5084 count_mode = get_tarval_mode(tv1);
5085 if (get_tarval_mode(tv2) != count_mode) {
5086 /* TODO: search bigger mode or something and convert... */
5090 mode = get_irn_mode(n);
5091 modulo_shf = get_mode_modulo_shift(mode);
5093 if (modulo_shf > 0) {
5094 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5096 /* I'm not so sure what happens in one complement... */
5097 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5098 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5099 * above will be invalid) */
5100 assert(modulo_shf<=0 || is_po2(modulo_shf));
5102 tv1 = tarval_and(tv1, modulo_mask);
5103 tv2 = tarval_and(tv2, modulo_mask);
5105 res = tarval_add(tv1, tv2);
5106 irg = get_irn_irg(n);
5108 /* beware: a simple replacement works only, if res < modulo shift */
5110 int bits = get_mode_size_bits(mode);
5111 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5112 res = tarval_mod(res, modulo);
5114 long bits = get_mode_size_bits(mode);
5115 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5117 /* shifting too much */
5118 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5120 ir_node *block = get_nodes_block(n);
5121 dbg_info *dbgi = get_irn_dbg_info(n);
5122 ir_mode *smode = get_irn_mode(right);
5123 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5124 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5127 return new_r_Const(irg, get_mode_null(mode));
5131 /* ok, we can replace it */
5132 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5133 block = get_nodes_block(n);
5135 in[0] = get_binop_left(left);
5136 in[1] = new_r_Const(irg, res);
5138 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5140 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5147 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5149 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5150 * (also with x >>s c1 when c1>=c2)
5152 static ir_node *transform_node_shl_shr(ir_node *n)
5155 ir_node *right = get_binop_right(n);
5165 ir_tarval *tv_shift;
5168 ir_relation relation;
5171 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5173 if (!is_Const(right))
5176 left = get_binop_left(n);
5177 mode = get_irn_mode(n);
5178 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5179 ir_node *shr_right = get_binop_right(left);
5181 if (!is_Const(shr_right))
5184 x = get_binop_left(left);
5185 tv_shr = get_Const_tarval(shr_right);
5186 tv_shl = get_Const_tarval(right);
5188 if (is_Shrs(left)) {
5189 /* shrs variant only allowed if c1 >= c2 */
5190 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5193 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5196 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5198 tv_mask = tarval_shl(tv_mask, tv_shl);
5199 } else if (is_Shr(n) && is_Shl(left)) {
5200 ir_node *shl_right = get_Shl_right(left);
5202 if (!is_Const(shl_right))
5205 x = get_Shl_left(left);
5206 tv_shr = get_Const_tarval(right);
5207 tv_shl = get_Const_tarval(shl_right);
5209 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5210 tv_mask = tarval_shr(tv_mask, tv_shr);
5215 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5216 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5219 assert(tv_mask != tarval_bad);
5220 assert(get_tarval_mode(tv_mask) == mode);
5222 block = get_nodes_block(n);
5223 irg = get_irn_irg(block);
5224 dbgi = get_irn_dbg_info(n);
5226 relation = tarval_cmp(tv_shl, tv_shr);
5227 if (relation == ir_relation_less || relation == ir_relation_equal) {
5228 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5229 new_const = new_r_Const(irg, tv_shift);
5231 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5233 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5236 assert(relation == ir_relation_greater);
5237 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5238 new_const = new_r_Const(irg, tv_shift);
5239 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5242 new_const = new_r_Const(irg, tv_mask);
5243 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5248 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5250 ir_mode *mode = get_tarval_mode(tv);
5251 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5252 return tarval_mod(tv, modulo_tv);
5255 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5256 ir_node *left, ir_node *right, ir_mode *mode);
5259 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5260 * then we can use that to minimize the value of Add(x, const) or
5261 * Sub(Const, x). In particular this often avoids 1 instruction in some
5262 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5263 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5265 static ir_node *transform_node_shift_modulo(ir_node *n,
5266 new_shift_func new_shift)
5268 ir_mode *mode = get_irn_mode(n);
5269 int modulo = get_mode_modulo_shift(mode);
5270 ir_node *newop = NULL;
5271 ir_mode *mode_right;
5278 if (get_mode_arithmetic(mode) != irma_twos_complement)
5280 if (!is_po2(modulo))
5283 irg = get_irn_irg(n);
5284 block = get_nodes_block(n);
5285 right = get_binop_right(n);
5286 mode_right = get_irn_mode(right);
5287 if (is_Const(right)) {
5288 ir_tarval *tv = get_Const_tarval(right);
5289 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5294 newop = new_r_Const(irg, tv_mod);
5295 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5296 ir_node *add_right = get_binop_right(right);
5297 if (is_Const(add_right)) {
5298 ir_tarval *tv = get_Const_tarval(add_right);
5299 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5304 newconst = new_r_Const(irg, tv_mod);
5305 newop = new_r_Add(block, get_binop_left(right), newconst,
5308 } else if (is_Sub(right)) {
5309 ir_node *sub_left = get_Sub_left(right);
5310 if (is_Const(sub_left)) {
5311 ir_tarval *tv = get_Const_tarval(sub_left);
5312 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5317 newconst = new_r_Const(irg, tv_mod);
5318 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5325 if (newop != NULL) {
5326 dbg_info *dbgi = get_irn_dbg_info(n);
5327 ir_node *left = get_binop_left(n);
5328 return new_shift(dbgi, block, left, newop, mode);
5336 static ir_node *transform_node_Shr(ir_node *n)
5338 ir_node *c, *oldn = n;
5339 ir_node *left = get_Shr_left(n);
5340 ir_node *right = get_Shr_right(n);
5341 ir_mode *mode = get_irn_mode(n);
5343 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5344 n = transform_node_shift(n);
5347 n = transform_node_shift_modulo(n, new_rd_Shr);
5349 n = transform_node_shl_shr(n);
5351 n = transform_node_shift_bitop(n);
5359 static ir_node *transform_node_Shrs(ir_node *n)
5362 ir_node *a = get_Shrs_left(n);
5363 ir_node *b = get_Shrs_right(n);
5364 ir_mode *mode = get_irn_mode(n);
5368 if (is_oversize_shift(n)) {
5369 ir_node *block = get_nodes_block(n);
5370 dbg_info *dbgi = get_irn_dbg_info(n);
5371 ir_mode *cmode = get_irn_mode(b);
5372 long val = get_mode_size_bits(cmode)-1;
5373 ir_graph *irg = get_irn_irg(n);
5374 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5375 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5378 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5379 n = transform_node_shift(n);
5383 n = transform_node_shift_modulo(n, new_rd_Shrs);
5386 n = transform_node_shift_bitop(n);
5390 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5391 attr = vrp_get_info(a);
5393 unsigned bits = get_mode_size_bits(mode);
5394 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5395 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5396 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5397 dbg_info *dbgi = get_irn_dbg_info(n);
5398 ir_node *block = get_nodes_block(n);
5399 return new_rd_Shr(dbgi, block, a, b, mode);
5409 static ir_node *transform_node_Shl(ir_node *n)
5411 ir_node *c, *oldn = n;
5412 ir_node *a = get_Shl_left(n);
5413 ir_node *b = get_Shl_right(n);
5414 ir_mode *mode = get_irn_mode(n);
5416 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5417 n = transform_node_shift(n);
5420 n = transform_node_shift_modulo(n, new_rd_Shl);
5422 n = transform_node_shl_shr(n);
5424 n = transform_node_shift_bitop(n);
5432 static ir_node *transform_node_Rotl(ir_node *n)
5434 ir_node *c, *oldn = n;
5435 ir_node *a = get_Rotl_left(n);
5436 ir_node *b = get_Rotl_right(n);
5437 ir_mode *mode = get_irn_mode(n);
5439 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5440 n = transform_node_shift(n);
5443 n = transform_node_shift_bitop(n);
5449 * returns mode size for may_leave_out_middle_mode
5451 static unsigned get_significand_size(ir_mode *mode)
5453 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5454 switch (arithmetic) {
5456 case irma_x86_extended_float:
5457 return get_mode_mantissa_size(mode) + 1;
5458 case irma_twos_complement:
5459 return get_mode_size_bits(mode);
5461 panic("Conv node with irma_none mode?");
5463 panic("unexpected mode_arithmetic in get_significand_size");
5467 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5468 * as converting from @p m0 to @p m1 and then to @p m2.
5469 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5470 * gives the following truth table:
5471 * s -> b -> m : true
5472 * s -> m -> b : !signed(s) || signed(m)
5473 * m -> b -> s : true
5474 * m -> s -> b : false
5475 * b -> s -> m : false
5476 * b -> m -> s : true
5478 * s -> b -> b : true
5479 * s -> s -> b : false
5481 * additional float constraints:
5483 * F -> I -> I: signedness of Is must match
5484 * I -> F -> I: signedness of Is must match
5485 * I -> I -> F: signedness of Is must match
5489 * at least 1 float involved: signedness must match
5491 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5493 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5494 if (n_floats == 1) {
5496 int n_signed = mode_is_signed(m0) + mode_is_signed(m1)
5497 + mode_is_signed(m2);
5498 /* we assume that float modes are always signed */
5499 if ((n_signed & 1) != 1)
5502 /* because overflow gives strange results we don't touch this case */
5505 } else if (n_floats == 2 && !mode_is_float(m1)) {
5509 unsigned size0 = get_significand_size(m0);
5510 unsigned size1 = get_significand_size(m1);
5511 unsigned size2 = get_significand_size(m2);
5512 if (size1 < size2 && size0 >= size1)
5516 return !mode_is_signed(m0) || mode_is_signed(m1);
5522 static ir_node *transform_node_Conv(ir_node *n)
5524 ir_node *c, *oldn = n;
5525 ir_mode *mode = get_irn_mode(n);
5526 ir_node *a = get_Conv_op(n);
5529 ir_mode *a_mode = get_irn_mode(a);
5530 ir_node *b = get_Conv_op(a);
5531 ir_mode *b_mode = get_irn_mode(b);
5532 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5533 dbg_info *dbgi = get_irn_dbg_info(n);
5534 ir_node *block = get_nodes_block(n);
5535 return new_rd_Conv(dbgi, block, b, mode);
5539 if (mode != mode_b && is_const_Phi(a)) {
5540 /* Do NOT optimize mode_b Conv's, this leads to remaining
5541 * Phib nodes later, because the conv_b_lower operation
5542 * is instantly reverted, when it tries to insert a Convb.
5544 c = apply_conv_on_phi(a, mode);
5546 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5551 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5552 ir_graph *irg = get_irn_irg(n);
5553 return new_r_Unknown(irg, mode);
5556 if (mode_is_reference(mode) &&
5557 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5559 ir_node *l = get_Add_left(a);
5560 ir_node *r = get_Add_right(a);
5561 dbg_info *dbgi = get_irn_dbg_info(a);
5562 ir_node *block = get_nodes_block(n);
5564 ir_node *lop = get_Conv_op(l);
5565 if (get_irn_mode(lop) == mode) {
5566 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5567 n = new_rd_Add(dbgi, block, lop, r, mode);
5572 ir_node *rop = get_Conv_op(r);
5573 if (get_irn_mode(rop) == mode) {
5574 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5575 n = new_rd_Add(dbgi, block, l, rop, mode);
5585 * Remove dead blocks and nodes in dead blocks
5586 * in keep alive list. We do not generate a new End node.
5588 static ir_node *transform_node_End(ir_node *n)
5590 int i, j, n_keepalives = get_End_n_keepalives(n);
5593 NEW_ARR_A(ir_node *, in, n_keepalives);
5595 for (i = j = 0; i < n_keepalives; ++i) {
5596 ir_node *ka = get_End_keepalive(n, i);
5598 /* no need to keep Bad */
5601 /* do not keep unreachable code */
5602 block = is_Block(ka) ? ka : get_nodes_block(ka);
5603 if (is_block_unreachable(block))
5607 if (j != n_keepalives)
5608 set_End_keepalives(n, j, in);
5612 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5614 if (is_Minus(a) && get_Minus_op(a) == b)
5616 if (is_Minus(b) && get_Minus_op(b) == a)
5618 if (is_Sub(a) && is_Sub(b)) {
5619 ir_node *a_left = get_Sub_left(a);
5620 ir_node *a_right = get_Sub_right(a);
5621 ir_node *b_left = get_Sub_left(b);
5622 ir_node *b_right = get_Sub_right(b);
5624 if (a_left == b_right && a_right == b_left)
5631 static const ir_node *skip_upconv(const ir_node *node)
5633 while (is_Conv(node)) {
5634 ir_mode *mode = get_irn_mode(node);
5635 const ir_node *op = get_Conv_op(node);
5636 ir_mode *op_mode = get_irn_mode(op);
5637 if (!smaller_mode(op_mode, mode))
5644 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5645 const ir_node *mux_true)
5650 ir_relation relation;
5656 * Note further that these optimization work even for floating point
5657 * with NaN's because -NaN == NaN.
5658 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5661 mode = get_irn_mode(mux_true);
5662 if (mode_honor_signed_zeros(mode))
5665 /* must be <, <=, >=, > */
5666 relation = get_Cmp_relation(sel);
5667 if ((relation & ir_relation_less_greater) == 0)
5670 if (!ir_is_negated_value(mux_true, mux_false))
5673 mux_true = skip_upconv(mux_true);
5674 mux_false = skip_upconv(mux_false);
5676 /* must be x cmp 0 */
5677 cmp_right = get_Cmp_right(sel);
5678 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5681 cmp_left = get_Cmp_left(sel);
5682 if (cmp_left == mux_false) {
5683 if (relation & ir_relation_less) {
5686 assert(relation & ir_relation_greater);
5689 } else if (cmp_left == mux_true) {
5690 if (relation & ir_relation_less) {
5693 assert(relation & ir_relation_greater);
5701 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5704 ir_node *cmp_left = get_Cmp_left(sel);
5705 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5708 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5709 const ir_node *mux_true)
5711 /* this code should return true each time transform_node_Mux would
5712 * optimize the Mux completely away */
5714 ir_mode *mode = get_irn_mode(mux_false);
5715 if (get_mode_arithmetic(mode) == irma_twos_complement
5716 && ir_mux_is_abs(sel, mux_false, mux_true))
5719 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5720 const ir_node *cmp_r = get_Cmp_right(sel);
5721 const ir_node *cmp_l = get_Cmp_left(sel);
5722 const ir_node *f = mux_false;
5723 const ir_node *t = mux_true;
5725 if (is_Const(t) && is_Const_null(t)) {
5730 if (is_And(cmp_l) && f == cmp_r) {
5731 ir_node *and_r = get_And_right(cmp_l);
5734 if (and_r == t && is_single_bit(and_r))
5736 and_l = get_And_left(cmp_l);
5737 if (and_l == t && is_single_bit(and_l))
5746 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5748 static ir_node *transform_Mux_set(ir_node *n)
5750 ir_node *cond = get_Mux_sel(n);
5755 ir_relation relation;
5769 left = get_Cmp_left(cond);
5770 mode = get_irn_mode(left);
5771 if (!mode_is_int(mode) && !mode_is_reference(mode))
5773 dest_mode = get_irn_mode(n);
5774 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5776 right = get_Cmp_right(cond);
5777 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5778 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5779 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5780 && relation != ir_relation_greater))
5785 case ir_relation_less:
5786 /* a < b -> (a - b) >> 31 */
5790 case ir_relation_less_equal:
5791 /* a <= b -> ~(a - b) >> 31 */
5796 case ir_relation_greater:
5797 /* a > b -> (b - a) >> 31 */
5801 case ir_relation_greater_equal:
5802 /* a >= b -> ~(a - b) >> 31 */
5811 dbgi = get_irn_dbg_info(n);
5812 block = get_nodes_block(n);
5813 irg = get_irn_irg(block);
5814 bits = get_mode_size_bits(dest_mode);
5815 tv = new_tarval_from_long(bits-1, mode_Iu);
5816 shift_cnt = new_rd_Const(dbgi, irg, tv);
5818 if (mode != dest_mode) {
5819 a = new_rd_Conv(dbgi, block, a, dest_mode);
5820 b = new_rd_Conv(dbgi, block, b, dest_mode);
5823 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5825 res = new_rd_Not(dbgi, block, res, dest_mode);
5827 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5832 * Optimize a Mux into some simpler cases.
5834 static ir_node *transform_node_Mux(ir_node *n)
5837 ir_node *sel = get_Mux_sel(n);
5838 ir_mode *mode = get_irn_mode(n);
5839 ir_node *t = get_Mux_true(n);
5840 ir_node *f = get_Mux_false(n);
5841 ir_graph *irg = get_irn_irg(n);
5843 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5844 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5845 int abs = ir_mux_is_abs(sel, f, t);
5847 dbg_info *dbgi = get_irn_dbg_info(n);
5848 ir_node *block = get_nodes_block(n);
5849 ir_node *op = ir_get_abs_op(sel, f, t);
5850 int bits = get_mode_size_bits(mode);
5851 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5852 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5853 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5856 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5858 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5864 /* first normalization step: try to move a constant to the false side,
5865 * 0 preferred on false side too */
5866 if (is_Cmp(sel) && is_Const(t) &&
5867 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5868 dbg_info *seldbgi = get_irn_dbg_info(sel);
5869 ir_node *block = get_nodes_block(sel);
5870 ir_relation relation = get_Cmp_relation(sel);
5875 /* Mux(x, a, b) => Mux(not(x), b, a) */
5876 relation = get_negated_relation(relation);
5877 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5878 get_Cmp_right(sel), relation);
5879 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5882 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5883 n = transform_Mux_set(n);
5888 /* the following optimisations create new mode_b nodes, so only do them
5889 * before mode_b lowering */
5890 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5892 ir_node* block = get_nodes_block(n);
5894 ir_node* c1 = get_Mux_sel(t);
5895 ir_node* t1 = get_Mux_true(t);
5896 ir_node* f1 = get_Mux_false(t);
5898 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5899 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5900 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5901 return new_r_Mux(block, and_, f1, t1, mode);
5902 } else if (f == t1) {
5903 /* Mux(cond0, Mux(cond1, x, y), x) */
5904 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5905 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5906 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5907 return new_r_Mux(block, and_, t1, f1, mode);
5909 } else if (is_Mux(f)) {
5910 ir_node* block = get_nodes_block(n);
5912 ir_node* c1 = get_Mux_sel(f);
5913 ir_node* t1 = get_Mux_true(f);
5914 ir_node* f1 = get_Mux_false(f);
5916 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5917 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5918 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5919 return new_r_Mux(block, or_, f1, t1, mode);
5920 } else if (t == f1) {
5921 /* Mux(cond0, x, Mux(cond1, y, x)) */
5922 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5923 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5924 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5925 return new_r_Mux(block, or_, t1, f1, mode);
5929 /* note: after normalization, false can only happen on default */
5930 if (mode == mode_b) {
5931 dbg_info *dbg = get_irn_dbg_info(n);
5932 ir_node *block = get_nodes_block(n);
5935 ir_tarval *tv_t = get_Const_tarval(t);
5936 if (tv_t == tarval_b_true) {
5938 /* Muxb(sel, true, false) = sel */
5939 assert(get_Const_tarval(f) == tarval_b_false);
5940 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5943 /* Muxb(sel, true, x) = Or(sel, x) */
5944 n = new_rd_Or(dbg, block, sel, f, mode_b);
5945 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5949 } else if (is_Const(f)) {
5950 ir_tarval *tv_f = get_Const_tarval(f);
5951 if (tv_f == tarval_b_true) {
5952 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5953 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5954 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5955 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5958 /* Muxb(sel, x, false) = And(sel, x) */
5959 assert(tv_f == tarval_b_false);
5960 n = new_rd_And(dbg, block, sel, t, mode_b);
5961 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5968 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5969 ir_relation relation = get_Cmp_relation(sel);
5970 ir_node *cmp_r = get_Cmp_right(sel);
5971 ir_node *cmp_l = get_Cmp_left(sel);
5972 ir_node *block = get_nodes_block(n);
5974 if (is_And(cmp_l) && f == cmp_r) {
5975 ir_node *and_r = get_And_right(cmp_l);
5978 if (and_r == t && is_single_bit(and_r)) {
5979 if (relation == ir_relation_equal) {
5980 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5981 n = new_rd_Eor(get_irn_dbg_info(n),
5982 block, cmp_l, t, mode);
5983 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5985 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5987 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5991 and_l = get_And_left(cmp_l);
5992 if (and_l == t && is_single_bit(and_l)) {
5993 if (relation == ir_relation_equal) {
5994 /* ((1 << n) & a) == 0, (1 << n), 0) */
5995 n = new_rd_Eor(get_irn_dbg_info(n),
5996 block, cmp_l, t, mode);
5997 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5999 /* ((1 << n) & a) != 0, (1 << n), 0) */
6001 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6012 * optimize Sync nodes that have other syncs as input we simply add the inputs
6013 * of the other sync to our own inputs
6015 static ir_node *transform_node_Sync(ir_node *n)
6017 int arity = get_Sync_n_preds(n);
6020 for (i = 0; i < arity;) {
6021 ir_node *pred = get_Sync_pred(n, i);
6025 /* Remove Bad predecessors */
6032 /* Remove duplicate predecessors */
6033 for (j = 0; j < i; ++j) {
6034 if (get_Sync_pred(n, j) == pred) {
6043 if (!is_Sync(pred)) {
6051 pred_arity = get_Sync_n_preds(pred);
6052 for (j = 0; j < pred_arity; ++j) {
6053 ir_node *pred_pred = get_Sync_pred(pred, j);
6058 add_irn_n(n, pred_pred);
6062 if (get_Sync_pred(n, k) == pred_pred)
6069 ir_graph *irg = get_irn_irg(n);
6070 return new_r_Bad(irg, mode_M);
6073 return get_Sync_pred(n, 0);
6076 /* rehash the sync node */
6081 static ir_node *transform_node_Load(ir_node *n)
6083 /* don't touch volatile loads */
6084 if (get_Load_volatility(n) == volatility_is_volatile)
6087 ir_node *ptr = get_Load_ptr(n);
6088 const ir_node *confirm;
6089 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
6090 set_irn_pinned(n, op_pin_state_floats);
6093 /* if our memory predecessor is a load from the same address, then reuse the
6094 * previous result */
6095 ir_node *mem = get_Load_mem(n);
6098 ir_node *mem_pred = get_Proj_pred(mem);
6099 if (is_Load(mem_pred)) {
6100 ir_node *pred_load = mem_pred;
6102 /* conservatively compare the 2 loads. TODO: This could be less strict
6103 * with fixup code in some situations (like smaller/bigger modes) */
6104 if (get_Load_ptr(pred_load) != ptr)
6106 if (get_Load_mode(pred_load) != get_Load_mode(n))
6108 /* all combinations of aligned/unaligned pred/n should be fine so we do
6109 * not compare the unaligned attribute */
6111 ir_node *block = get_nodes_block(n);
6112 ir_node *jmp = new_r_Jmp(block);
6113 ir_graph *irg = get_irn_irg(n);
6114 ir_node *bad = new_r_Bad(irg, mode_X);
6115 ir_mode *mode = get_Load_mode(n);
6116 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6117 ir_node *in[] = { mem, res, jmp, bad };
6118 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6121 } else if (is_Store(mem_pred)) {
6122 ir_node *pred_store = mem_pred;
6123 ir_node *value = get_Store_value(pred_store);
6125 if (get_Store_ptr(pred_store) != ptr)
6127 if (get_irn_mode(value) != get_Load_mode(n))
6129 /* all combinations of aligned/unaligned pred/n should be fine so we do
6130 * not compare the unaligned attribute */
6132 ir_node *block = get_nodes_block(n);
6133 ir_node *jmp = new_r_Jmp(block);
6134 ir_graph *irg = get_irn_irg(n);
6135 ir_node *bad = new_r_Bad(irg, mode_X);
6136 ir_node *res = value;
6137 ir_node *in[] = { mem, res, jmp, bad };
6138 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6146 static ir_node *transform_node_Store(ir_node *n)
6148 /* don't touch volatile stores */
6149 if (get_Store_volatility(n) == volatility_is_volatile)
6152 ir_node *ptr = get_Store_ptr(n);
6153 const ir_node *confirm;
6154 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
6155 set_irn_pinned(n, op_pin_state_floats);
6161 * optimize a trampoline Call into a direct Call
6163 static ir_node *transform_node_Call(ir_node *call)
6165 ir_node *callee = get_Call_ptr(call);
6166 ir_node *adr, *mem, *res, *bl, **in;
6167 ir_type *ctp, *mtp, *tp;
6171 size_t i, n_res, n_param;
6174 if (! is_Proj(callee))
6176 callee = get_Proj_pred(callee);
6177 if (! is_Builtin(callee))
6179 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6182 mem = get_Call_mem(call);
6184 if (skip_Proj(mem) == callee) {
6185 /* memory is routed to the trampoline, skip */
6186 mem = get_Builtin_mem(callee);
6189 /* build a new call type */
6190 mtp = get_Call_type(call);
6191 tdb = get_type_dbg_info(mtp);
6193 n_res = get_method_n_ress(mtp);
6194 n_param = get_method_n_params(mtp);
6195 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6197 for (i = 0; i < n_res; ++i)
6198 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6200 NEW_ARR_A(ir_node *, in, n_param + 1);
6202 /* FIXME: we don't need a new pointer type in every step */
6203 irg = get_irn_irg(call);
6204 tp = get_irg_frame_type(irg);
6205 tp = new_type_pointer(tp);
6206 set_method_param_type(ctp, 0, tp);
6208 in[0] = get_Builtin_param(callee, 2);
6209 for (i = 0; i < n_param; ++i) {
6210 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6211 in[i + 1] = get_Call_param(call, i);
6213 var = get_method_variadicity(mtp);
6214 set_method_variadicity(ctp, var);
6215 /* When we resolve a trampoline, the function must be called by a this-call */
6216 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6217 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6219 adr = get_Builtin_param(callee, 1);
6221 db = get_irn_dbg_info(call);
6222 bl = get_nodes_block(call);
6224 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6225 if (get_irn_pinned(call) == op_pin_state_floats)
6226 set_irn_pinned(res, op_pin_state_floats);
6231 * Tries several [inplace] [optimizing] transformations and returns an
6232 * equivalent node. The difference to equivalent_node() is that these
6233 * transformations _do_ generate new nodes, and thus the old node must
6234 * not be freed even if the equivalent node isn't the old one.
6236 static ir_node *transform_node(ir_node *n)
6242 iro = get_irn_opcode_(n);
6243 /* constant expression evaluation / constant folding */
6244 if (get_opt_constant_folding()) {
6245 /* neither constants nor Tuple values can be evaluated */
6246 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6247 /* try to evaluate */
6248 ir_tarval *tv = computed_value(n);
6249 if (tv != tarval_bad) {
6250 /* evaluation was successful -- replace the node. */
6251 ir_graph *irg = get_irn_irg(n);
6253 n = new_r_Const(irg, tv);
6255 DBG_OPT_CSTEVAL(old_n, n);
6261 /* remove unnecessary nodes */
6262 if (get_opt_constant_folding() ||
6263 (iro == iro_Phi) || /* always optimize these nodes. */
6264 (iro == iro_Id) || /* ... */
6265 (iro == iro_Proj) || /* ... */
6266 (iro == iro_Block)) { /* Flags tested local. */
6267 n = equivalent_node(n);
6272 /* Some more constant expression evaluation. */
6273 if (get_opt_algebraic_simplification() ||
6274 (iro == iro_Cond) ||
6275 (iro == iro_Proj)) { /* Flags tested local. */
6276 if (n->op->ops.transform_node != NULL) {
6277 n = n->op->ops.transform_node(n);
6287 static void register_computed_value_func(ir_op *op, computed_value_func func)
6289 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6290 op->ops.computed_value = func;
6293 static void register_computed_value_func_proj(ir_op *op,
6294 computed_value_func func)
6296 assert(op->ops.computed_value_Proj == NULL
6297 || op->ops.computed_value_Proj == func);
6298 op->ops.computed_value_Proj = func;
6301 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6303 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6304 op->ops.equivalent_node = func;
6307 static void register_equivalent_node_func_proj(ir_op *op,
6308 equivalent_node_func func)
6310 assert(op->ops.equivalent_node_Proj == NULL
6311 || op->ops.equivalent_node_Proj == func);
6312 op->ops.equivalent_node_Proj = func;
6315 static void register_transform_node_func(ir_op *op, transform_node_func func)
6317 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6318 op->ops.transform_node = func;
6321 static void register_transform_node_func_proj(ir_op *op,
6322 transform_node_func func)
6324 assert(op->ops.transform_node_Proj == NULL
6325 || op->ops.transform_node_Proj == func);
6326 op->ops.transform_node_Proj = func;
6329 void ir_register_opt_node_ops(void)
6331 register_computed_value_func(op_Add, computed_value_Add);
6332 register_computed_value_func(op_And, computed_value_And);
6333 register_computed_value_func(op_Borrow, computed_value_Borrow);
6334 register_computed_value_func(op_Carry, computed_value_Carry);
6335 register_computed_value_func(op_Cmp, computed_value_Cmp);
6336 register_computed_value_func(op_Confirm, computed_value_Confirm);
6337 register_computed_value_func(op_Const, computed_value_Const);
6338 register_computed_value_func(op_Conv, computed_value_Conv);
6339 register_computed_value_func(op_Eor, computed_value_Eor);
6340 register_computed_value_func(op_Minus, computed_value_Minus);
6341 register_computed_value_func(op_Mul, computed_value_Mul);
6342 register_computed_value_func(op_Mux, computed_value_Mux);
6343 register_computed_value_func(op_Not, computed_value_Not);
6344 register_computed_value_func(op_Or, computed_value_Or);
6345 register_computed_value_func(op_Proj, computed_value_Proj);
6346 register_computed_value_func(op_Rotl, computed_value_Rotl);
6347 register_computed_value_func(op_Shl, computed_value_Shl);
6348 register_computed_value_func(op_Shr, computed_value_Shr);
6349 register_computed_value_func(op_Shrs, computed_value_Shrs);
6350 register_computed_value_func(op_Sub, computed_value_Sub);
6351 register_computed_value_func(op_SymConst, computed_value_SymConst);
6352 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6353 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6355 register_equivalent_node_func(op_Add, equivalent_node_Add);
6356 register_equivalent_node_func(op_And, equivalent_node_And);
6357 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6358 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6359 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6360 register_equivalent_node_func(op_Id, equivalent_node_Id);
6361 register_equivalent_node_func(op_Minus, equivalent_node_idempotent_unop);
6362 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6363 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6364 register_equivalent_node_func(op_Not, equivalent_node_idempotent_unop);
6365 register_equivalent_node_func(op_Or, equivalent_node_Or);
6366 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6367 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6368 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6369 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6370 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6371 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6372 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6373 register_equivalent_node_func_proj(op_Bound, equivalent_node_Proj_Bound);
6374 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6375 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6376 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6378 register_transform_node_func(op_Add, transform_node_Add);
6379 register_transform_node_func(op_And, transform_node_And);
6380 register_transform_node_func(op_Block, transform_node_Block);
6381 register_transform_node_func(op_Call, transform_node_Call);
6382 register_transform_node_func(op_Cmp, transform_node_Cmp);
6383 register_transform_node_func(op_Cond, transform_node_Cond);
6384 register_transform_node_func(op_Conv, transform_node_Conv);
6385 register_transform_node_func(op_Div, transform_node_Div);
6386 register_transform_node_func(op_End, transform_node_End);
6387 register_transform_node_func(op_Eor, transform_node_Eor);
6388 register_transform_node_func(op_Load, transform_node_Load);
6389 register_transform_node_func(op_Minus, transform_node_Minus);
6390 register_transform_node_func(op_Mod, transform_node_Mod);
6391 register_transform_node_func(op_Mul, transform_node_Mul);
6392 register_transform_node_func(op_Mux, transform_node_Mux);
6393 register_transform_node_func(op_Not, transform_node_Not);
6394 register_transform_node_func(op_Or, transform_node_Or);
6395 register_transform_node_func(op_Phi, transform_node_Phi);
6396 register_transform_node_func(op_Proj, transform_node_Proj);
6397 register_transform_node_func(op_Rotl, transform_node_Rotl);
6398 register_transform_node_func(op_Shl, transform_node_Shl);
6399 register_transform_node_func(op_Shrs, transform_node_Shrs);
6400 register_transform_node_func(op_Shr, transform_node_Shr);
6401 register_transform_node_func(op_Store, transform_node_Store);
6402 register_transform_node_func(op_Sub, transform_node_Sub);
6403 register_transform_node_func(op_Switch, transform_node_Switch);
6404 register_transform_node_func(op_Sync, transform_node_Sync);
6405 register_transform_node_func_proj(op_Bound, transform_node_Proj_Bound);
6406 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6407 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6408 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6409 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6410 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6413 /* **************** Common Subexpression Elimination **************** */
6415 /** The size of the hash table used, should estimate the number of nodes
6417 #define N_IR_NODES 512
6419 int identities_cmp(const void *elt, const void *key)
6421 ir_node *a = (ir_node *)elt;
6422 ir_node *b = (ir_node *)key;
6425 if (a == b) return 0;
6427 if ((get_irn_op(a) != get_irn_op(b)) ||
6428 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6430 /* compare if a's in and b's in are of equal length */
6431 irn_arity_a = get_irn_arity(a);
6432 if (irn_arity_a != get_irn_arity(b))
6435 /* blocks are never the same */
6439 if (get_irn_pinned(a) == op_pin_state_pinned) {
6440 /* for pinned nodes, the block inputs must be equal */
6441 if (get_nodes_block(a) != get_nodes_block(b))
6444 ir_node *block_a = get_nodes_block(a);
6445 ir_node *block_b = get_nodes_block(b);
6446 if (! get_opt_global_cse()) {
6447 /* for block-local CSE both nodes must be in the same Block */
6448 if (block_a != block_b)
6451 /* The optimistic approach would be to do nothing here.
6452 * However doing GCSE optimistically produces a lot of partially dead code which appears
6453 * to be worse in practice than the missed opportunities.
6454 * So we use a very conservative variant here and only CSE if 1 value dominates the
6456 if (!block_dominates(block_a, block_b)
6457 && !block_dominates(block_b, block_a))
6459 /* respect the workaround rule: do not move nodes which are only
6460 * held by keepalive edges */
6461 if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6466 /* compare a->in[0..ins] with b->in[0..ins] */
6467 for (i = 0; i < irn_arity_a; ++i) {
6468 ir_node *pred_a = get_irn_n(a, i);
6469 ir_node *pred_b = get_irn_n(b, i);
6470 if (pred_a != pred_b) {
6471 /* if both predecessors are CSE neutral they might be different */
6472 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6478 * here, we already now that the nodes are identical except their
6481 if (a->op->ops.node_cmp_attr)
6482 return a->op->ops.node_cmp_attr(a, b);
6487 unsigned ir_node_hash(const ir_node *node)
6489 return node->op->ops.hash(node);
6492 void new_identities(ir_graph *irg)
6494 if (irg->value_table != NULL)
6495 del_pset(irg->value_table);
6496 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6499 void del_identities(ir_graph *irg)
6501 if (irg->value_table != NULL)
6502 del_pset(irg->value_table);
6505 static int cmp_node_nr(const void *a, const void *b)
6507 ir_node **p1 = (ir_node**)a;
6508 ir_node **p2 = (ir_node**)b;
6509 long n1 = get_irn_node_nr(*p1);
6510 long n2 = get_irn_node_nr(*p2);
6511 return (n1>n2) - (n1<n2);
6514 void ir_normalize_node(ir_node *n)
6516 if (is_op_commutative(get_irn_op(n))) {
6517 ir_node *l = get_binop_left(n);
6518 ir_node *r = get_binop_right(n);
6520 /* For commutative operators perform a OP b == b OP a but keep
6521 * constants on the RIGHT side. This helps greatly in some
6522 * optimizations. Moreover we use the idx number to make the form
6524 if (!operands_are_normalized(l, r)) {
6525 set_binop_left(n, r);
6526 set_binop_right(n, l);
6529 } else if (is_Sync(n)) {
6530 /* we assume that most of the time the inputs of a Sync node are already
6531 * sorted, so check this first as a shortcut */
6532 bool ins_sorted = true;
6533 int arity = get_irn_arity(n);
6534 const ir_node *last = get_irn_n(n, 0);
6536 for (i = 1; i < arity; ++i) {
6537 const ir_node *node = get_irn_n(n, i);
6538 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6546 ir_node **ins = get_irn_in(n)+1;
6547 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6548 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6549 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6550 set_irn_in(n, arity, new_ins);
6556 ir_node *identify_remember(ir_node *n)
6558 ir_graph *irg = get_irn_irg(n);
6559 pset *value_table = irg->value_table;
6562 if (value_table == NULL)
6565 ir_normalize_node(n);
6566 /* lookup or insert in hash table with given hash key. */
6567 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6570 /* n is reachable again */
6571 edges_node_revival(nn);
6578 * During construction we set the op_pin_state_pinned flag in the graph right
6579 * when the optimization is performed. The flag turning on procedure global
6580 * cse could be changed between two allocations. This way we are safe.
6582 * @param n The node to lookup
6584 static inline ir_node *identify_cons(ir_node *n)
6588 n = identify_remember(n);
6589 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6590 ir_graph *irg = get_irn_irg(n);
6591 set_irg_pinned(irg, op_pin_state_floats);
6596 void add_identities(ir_node *node)
6603 identify_remember(node);
6606 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6608 ir_graph *rem = current_ir_graph;
6610 current_ir_graph = irg;
6611 foreach_pset(irg->value_table, ir_node, node) {
6614 current_ir_graph = rem;
6617 ir_node *optimize_node(ir_node *n)
6620 ir_graph *irg = get_irn_irg(n);
6621 unsigned iro = get_irn_opcode(n);
6624 /* Always optimize Phi nodes: part of the construction. */
6625 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6627 /* constant expression evaluation / constant folding */
6628 if (get_opt_constant_folding()) {
6629 /* neither constants nor Tuple values can be evaluated */
6630 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6631 /* try to evaluate */
6632 tv = computed_value(n);
6633 if (tv != tarval_bad) {
6638 * we MUST copy the node here temporarily, because it's still
6639 * needed for DBG_OPT_CSTEVAL
6641 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6642 oldn = (ir_node*)alloca(node_size);
6644 memcpy(oldn, n, node_size);
6645 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6647 /* ARG, copy the in array, we need it for statistics */
6648 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6650 /* note the inplace edges module */
6651 edges_node_deleted(n);
6653 /* evaluation was successful -- replace the node. */
6654 irg_kill_node(irg, n);
6655 nw = new_r_Const(irg, tv);
6657 DBG_OPT_CSTEVAL(oldn, nw);
6663 /* remove unnecessary nodes */
6664 if (get_opt_algebraic_simplification() ||
6665 (iro == iro_Phi) || /* always optimize these nodes. */
6667 (iro == iro_Proj) ||
6668 (iro == iro_Block) ) /* Flags tested local. */
6669 n = equivalent_node(n);
6671 /* Common Subexpression Elimination.
6673 * Checks whether n is already available.
6674 * The block input is used to distinguish different subexpressions. Right
6675 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6676 * subexpressions within a block.
6679 n = identify_cons(n);
6682 edges_node_deleted(oldn);
6684 /* We found an existing, better node, so we can deallocate the old node. */
6685 irg_kill_node(irg, oldn);
6689 /* Some more constant expression evaluation that does not allow to
6691 iro = get_irn_opcode(n);
6692 if (get_opt_algebraic_simplification() ||
6693 (iro == iro_Cond) ||
6694 (iro == iro_Proj)) { /* Flags tested local. */
6695 n = transform_node(n);
6698 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6699 if (get_opt_cse()) {
6701 n = identify_remember(o);
6709 ir_node *optimize_in_place_2(ir_node *n)
6711 if (!get_opt_optimize() && !is_Phi(n)) return n;
6716 /** common subexpression elimination **/
6717 /* Checks whether n is already available. */
6718 /* The block input is used to distinguish different subexpressions.
6719 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6720 * only finds common subexpressions within a block. */
6721 if (get_opt_cse()) {
6723 n = identify_remember(n);
6726 /* we have another existing node now, we do not optimize it here */
6731 n = transform_node(n);
6733 /* Now we can verify the node, as it has no dead inputs any more. */
6736 /* Now we have a legal, useful node. Enter it in hash table for cse.
6738 * Note: This is only necessary because some of the optimisations
6739 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6740 * bad practice and should be fixed sometime.
6742 if (get_opt_cse()) {
6744 n = identify_remember(o);
6752 ir_node *optimize_in_place(ir_node *n)
6754 ir_graph *irg = get_irn_irg(n);
6756 if (get_opt_global_cse())
6757 set_irg_pinned(irg, op_pin_state_floats);
6759 /* FIXME: Maybe we could also test whether optimizing the node can
6760 change the control graph. */
6761 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6762 return optimize_in_place_2(n);