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_involution(ir_node *n)
953 ir_node *pred = get_unop_op(n);
954 if (get_irn_op(pred) == get_irn_op(n)) {
955 n = get_unop_op(pred);
956 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_INVOLUTION);
962 * Optimize a * 1 = 1 * a = a.
964 static ir_node *equivalent_node_Mul(ir_node *n)
967 ir_node *a = get_Mul_left(n);
969 /* we can handle here only the n * n = n bit cases */
970 if (get_irn_mode(n) == get_irn_mode(a)) {
971 ir_node *b = get_Mul_right(n);
975 * Mul is commutative and has again an other neutral element.
976 * Constants are place right, so check this case first.
979 if (tarval_is_one(tv)) {
981 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
984 if (tarval_is_one(tv)) {
986 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
994 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
996 static ir_node *equivalent_node_Or(ir_node *n)
1000 ir_node *a = get_Or_left(n);
1001 ir_node *b = get_Or_right(n);
1005 n = a; /* idempotence */
1006 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1009 /* constants are normalized to right, check this side first */
1011 if (tarval_is_null(tv)) {
1013 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1017 if (tarval_is_null(tv)) {
1019 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1027 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1029 static ir_node *equivalent_node_And(ir_node *n)
1033 ir_node *a = get_And_left(n);
1034 ir_node *b = get_And_right(n);
1038 n = a; /* idempotence */
1039 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1042 /* constants are normalized to right, check this side first */
1044 if (tarval_is_all_one(tv)) {
1046 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1049 if (tv != get_tarval_bad()) {
1050 ir_mode *mode = get_irn_mode(n);
1051 if (!mode_is_signed(mode) && is_Conv(a)) {
1052 ir_node *convop = get_Conv_op(a);
1053 ir_mode *convopmode = get_irn_mode(convop);
1054 if (!mode_is_signed(convopmode)) {
1055 /* Check Conv(all_one) & Const = all_one */
1056 ir_tarval *one = get_mode_all_one(convopmode);
1057 ir_tarval *conv = tarval_convert_to(one, mode);
1058 ir_tarval *tand = tarval_and(conv, tv);
1060 if (tarval_is_all_one(tand)) {
1061 /* Conv(X) & Const = X */
1063 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1070 if (tarval_is_all_one(tv)) {
1072 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1076 if ((is_Or(a) || is_Or_Eor_Add(a))
1077 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1079 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1083 if ((is_Or(b) || is_Or_Eor_Add(b))
1084 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1086 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1093 * Try to remove useless Conv's:
1095 static ir_node *equivalent_node_Conv(ir_node *n)
1098 ir_node *a = get_Conv_op(n);
1100 ir_mode *n_mode = get_irn_mode(n);
1101 ir_mode *a_mode = get_irn_mode(a);
1103 if (n_mode == a_mode) { /* No Conv necessary */
1105 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1107 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1108 ir_node *b = get_Conv_op(a);
1109 ir_mode *b_mode = get_irn_mode(b);
1111 if (n_mode == b_mode && values_in_mode(b_mode, a_mode)) {
1113 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1121 * - fold Phi-nodes, iff they have only one predecessor except
1124 static ir_node *equivalent_node_Phi(ir_node *n)
1129 ir_node *first_val = NULL; /* to shutup gcc */
1131 if (!get_opt_optimize() &&
1132 !irg_is_constrained(get_irn_irg(n), IR_GRAPH_CONSTRAINT_CONSTRUCTION))
1135 n_preds = get_Phi_n_preds(n);
1137 /* Phi of dead Region without predecessors. */
1141 /* Find first non-self-referencing input */
1142 for (i = 0; i < n_preds; ++i) {
1143 first_val = get_Phi_pred(n, i);
1144 /* not self pointer */
1145 if (first_val != n) {
1146 /* then found first value. */
1151 /* search for rest of inputs, determine if any of these
1152 are non-self-referencing */
1153 while (++i < n_preds) {
1154 ir_node *scnd_val = get_Phi_pred(n, i);
1155 if (scnd_val != n && scnd_val != first_val) {
1160 if (i >= n_preds && !is_Dummy(first_val)) {
1161 /* Fold, if no multiple distinct non-self-referencing inputs */
1163 DBG_OPT_PHI(oldn, n);
1169 * Optimize Proj(Tuple).
1171 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1173 ir_node *oldn = proj;
1174 ir_node *tuple = get_Proj_pred(proj);
1176 /* Remove the Tuple/Proj combination. */
1177 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1178 DBG_OPT_TUPLE(oldn, tuple, proj);
1184 * Optimize a / 1 = a.
1186 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1188 ir_node *oldn = proj;
1189 ir_node *div = get_Proj_pred(proj);
1190 ir_node *b = get_Div_right(div);
1191 ir_tarval *tb = value_of(b);
1193 /* Div is not commutative. */
1194 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1195 switch (get_Proj_proj(proj)) {
1197 proj = get_Div_mem(div);
1198 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1202 proj = get_Div_left(div);
1203 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1207 /* we cannot replace the exception Proj's here, this is done in
1208 transform_node_Proj_Div() */
1216 * Optimize CopyB(mem, x, x) into a Nop.
1218 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1220 ir_node *oldn = proj;
1221 ir_node *copyb = get_Proj_pred(proj);
1222 ir_node *a = get_CopyB_dst(copyb);
1223 ir_node *b = get_CopyB_src(copyb);
1226 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1227 switch (get_Proj_proj(proj)) {
1229 proj = get_CopyB_mem(copyb);
1230 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1238 * Optimize Bounds(idx, idx, upper) into idx.
1240 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1242 ir_node *oldn = proj;
1243 ir_node *bound = get_Proj_pred(proj);
1244 ir_node *idx = get_Bound_index(bound);
1245 ir_node *pred = skip_Proj(idx);
1248 if (idx == get_Bound_lower(bound))
1250 else if (is_Bound(pred)) {
1252 * idx was Bounds checked previously, it is still valid if
1253 * lower <= pred_lower && pred_upper <= upper.
1255 ir_node *lower = get_Bound_lower(bound);
1256 ir_node *upper = get_Bound_upper(bound);
1257 if (get_Bound_lower(pred) == lower &&
1258 get_Bound_upper(pred) == upper) {
1260 * One could expect that we simply return the previous
1261 * Bound here. However, this would be wrong, as we could
1262 * add an exception Proj to a new location then.
1263 * So, we must turn in into a tuple.
1269 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1270 switch (get_Proj_proj(proj)) {
1272 DBG_OPT_EXC_REM(proj);
1273 proj = get_Bound_mem(bound);
1277 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1280 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1288 * Does all optimizations on nodes that must be done on its Projs
1289 * because of creating new nodes.
1291 static ir_node *equivalent_node_Proj(ir_node *proj)
1293 ir_node *n = get_Proj_pred(proj);
1294 if (n->op->ops.equivalent_node_Proj)
1295 return n->op->ops.equivalent_node_Proj(proj);
1302 static ir_node *equivalent_node_Id(ir_node *n)
1310 DBG_OPT_ID(oldn, n);
1317 static ir_node *equivalent_node_Mux(ir_node *n)
1319 ir_node *oldn = n, *sel = get_Mux_sel(n);
1321 ir_tarval *ts = value_of(sel);
1323 if (ts == tarval_bad && is_Cmp(sel)) {
1324 /* try again with a direct call to compute_cmp, as we don't care
1325 * about the MODEB_LOWERED flag here */
1326 ts = compute_cmp_ext(sel);
1329 /* Mux(true, f, t) == t */
1330 if (ts == tarval_b_true) {
1331 n = get_Mux_true(n);
1332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1335 /* Mux(false, f, t) == f */
1336 if (ts == tarval_b_false) {
1337 n = get_Mux_false(n);
1338 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1341 n_t = get_Mux_true(n);
1342 n_f = get_Mux_false(n);
1344 /* Mux(v, x, T) == x */
1345 if (is_Unknown(n_f)) {
1347 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1350 /* Mux(v, T, x) == x */
1351 if (is_Unknown(n_t)) {
1353 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1357 /* Mux(v, x, x) == x */
1360 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1363 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1364 ir_relation relation = get_Cmp_relation(sel);
1365 ir_node *f = get_Mux_false(n);
1366 ir_node *t = get_Mux_true(n);
1369 * Note further that these optimization work even for floating point
1370 * with NaN's because -NaN == NaN.
1371 * However, if +0 and -0 is handled differently, we cannot use the first one.
1373 ir_node *const cmp_l = get_Cmp_left(sel);
1374 ir_node *const cmp_r = get_Cmp_right(sel);
1377 case ir_relation_equal:
1378 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1379 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1386 case ir_relation_less_greater:
1387 case ir_relation_unordered_less_greater:
1388 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1389 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1391 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1400 * Note: normalization puts the constant on the right side,
1401 * so we check only one case.
1403 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1404 /* Mux(t CMP 0, X, t) */
1405 if (is_Minus(f) && get_Minus_op(f) == t) {
1406 /* Mux(t CMP 0, -t, t) */
1407 if (relation == ir_relation_equal) {
1408 /* Mux(t == 0, -t, t) ==> -t */
1410 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1411 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1412 /* Mux(t != 0, -t, t) ==> t */
1414 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1424 * Remove Confirm nodes if setting is on.
1425 * Replace Confirms(x, '=', Constlike) by Constlike.
1427 static ir_node *equivalent_node_Confirm(ir_node *n)
1429 ir_node *pred = get_Confirm_value(n);
1430 ir_relation relation = get_Confirm_relation(n);
1432 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1434 * rare case: two identical Confirms one after another,
1435 * replace the second one with the first.
1438 pred = get_Confirm_value(n);
1444 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1445 * perform no actual computation, as, e.g., the Id nodes. It does not create
1446 * new nodes. It is therefore safe to free n if the node returned is not n.
1447 * If a node returns a Tuple we can not just skip it. If the size of the
1448 * in array fits, we transform n into a tuple (e.g., Div).
1450 ir_node *equivalent_node(ir_node *n)
1452 if (n->op->ops.equivalent_node)
1453 return n->op->ops.equivalent_node(n);
1458 * Returns non-zero if a node is a Phi node
1459 * with all predecessors constant.
1461 static int is_const_Phi(ir_node *n)
1465 if (! is_Phi(n) || get_irn_arity(n) == 0)
1467 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1468 if (! is_Const(get_irn_n(n, i)))
1474 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1475 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1478 * in reality eval_func should be tarval (*eval_func)() but incomplete
1479 * declarations are bad style and generate noisy warnings
1481 typedef void (*eval_func)(void);
1484 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1486 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1488 if (eval == (eval_func) tarval_sub) {
1489 tarval_sub_type func = (tarval_sub_type)eval;
1491 return func(a, b, mode);
1493 tarval_binop_type func = (tarval_binop_type)eval;
1500 * Apply an evaluator on a binop with a constant operators (and one Phi).
1502 * @param phi the Phi node
1503 * @param other the other operand
1504 * @param eval an evaluator function
1505 * @param mode the mode of the result, may be different from the mode of the Phi!
1506 * @param left if non-zero, other is the left operand, else the right
1508 * @return a new Phi node if the conversion was successful, NULL else
1510 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1512 int n = get_irn_arity(phi);
1513 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1515 for (int i = 0; i < n; ++i) {
1516 ir_node *pred = get_irn_n(phi, i);
1517 ir_tarval *tv = get_Const_tarval(pred);
1518 tv = do_eval(eval, other, tv, mode);
1520 if (tv == tarval_bad) {
1521 /* folding failed, bad */
1527 for (int i = 0; i < n; ++i) {
1528 ir_node *pred = get_irn_n(phi, i);
1529 ir_tarval *tv = get_Const_tarval(pred);
1530 tv = do_eval(eval, tv, other, mode);
1532 if (tv == tarval_bad) {
1533 /* folding failed, bad */
1539 ir_graph *irg = get_irn_irg(phi);
1540 ir_node **res = ALLOCAN(ir_node*, n);
1541 for (int i = 0; i < n; ++i) {
1542 res[i] = new_r_Const(irg, tvs[i]);
1544 ir_node *block = get_nodes_block(phi);
1545 return new_r_Phi(block, n, res, mode);
1549 * Apply an evaluator on a binop with two constant Phi.
1551 * @param a the left Phi node
1552 * @param b the right Phi node
1553 * @param eval an evaluator function
1554 * @param mode the mode of the result, may be different from the mode of the Phi!
1556 * @return a new Phi node if the conversion was successful, NULL else
1558 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1560 if (get_nodes_block(a) != get_nodes_block(b))
1563 int n = get_irn_arity(a);
1564 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1565 for (int i = 0; i < n; ++i) {
1566 ir_node *pred_a = get_irn_n(a, i);
1567 ir_tarval *tv_l = get_Const_tarval(pred_a);
1568 ir_node *pred_b = get_irn_n(b, i);
1569 ir_tarval *tv_r = get_Const_tarval(pred_b);
1570 ir_tarval *tv = do_eval(eval, tv_l, tv_r, mode);
1572 if (tv == tarval_bad) {
1573 /* folding failed, bad */
1578 ir_graph *irg = get_irn_irg(a);
1579 ir_node **res = ALLOCAN(ir_node*, n);
1580 for (int i = 0; i < n; ++i) {
1581 res[i] = new_r_Const(irg, tvs[i]);
1583 ir_node *block = get_nodes_block(a);
1584 return new_r_Phi(block, n, res, mode);
1588 * Apply an evaluator on a unop with a constant operator (a Phi).
1590 * @param phi the Phi node
1591 * @param eval an evaluator function
1593 * @return a new Phi node if the conversion was successful, NULL else
1595 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1597 int n = get_irn_arity(phi);
1598 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1599 for (int i = 0; i < n; ++i) {
1600 ir_node *pred = get_irn_n(phi, i);
1601 ir_tarval *tv = get_Const_tarval(pred);
1604 if (tv == tarval_bad) {
1605 /* folding failed, bad */
1610 ir_graph *irg = get_irn_irg(phi);
1611 ir_node **res = ALLOCAN(ir_node*, n);
1612 for (int i = 0; i < n; ++i) {
1613 res[i] = new_r_Const(irg, tvs[i]);
1615 ir_node *block = get_nodes_block(phi);
1616 ir_mode *mode = get_irn_mode(phi);
1617 return new_r_Phi(block, n, res, mode);
1621 * Apply a conversion on a constant operator (a Phi).
1623 * @param phi the Phi node
1625 * @return a new Phi node if the conversion was successful, NULL else
1627 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1629 int n = get_irn_arity(phi);
1630 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1631 for (int i = 0; i < n; ++i) {
1632 ir_node *pred = get_irn_n(phi, i);
1633 ir_tarval *tv = get_Const_tarval(pred);
1634 tv = tarval_convert_to(tv, mode);
1636 if (tv == tarval_bad) {
1637 /* folding failed, bad */
1642 ir_graph *irg = get_irn_irg(phi);
1643 ir_node **res = ALLOCAN(ir_node*, n);
1644 for (int i = 0; i < n; ++i) {
1645 res[i] = new_r_Const(irg, tvs[i]);
1647 ir_node *block = get_nodes_block(phi);
1648 return new_r_Phi(block, n, res, mode);
1652 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1653 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1654 * If possible, remove the Conv's.
1656 static ir_node *transform_node_AddSub(ir_node *n)
1658 ir_mode *mode = get_irn_mode(n);
1660 if (mode_is_reference(mode)) {
1661 ir_node *left = get_binop_left(n);
1662 ir_node *right = get_binop_right(n);
1663 unsigned ref_bits = get_mode_size_bits(mode);
1665 if (is_Conv(left)) {
1666 ir_mode *lmode = get_irn_mode(left);
1667 unsigned bits = get_mode_size_bits(lmode);
1669 if (ref_bits == bits &&
1670 mode_is_int(lmode) &&
1671 get_mode_arithmetic(lmode) == irma_twos_complement) {
1672 ir_node *pre = get_Conv_op(left);
1673 ir_mode *pre_mode = get_irn_mode(pre);
1675 if (mode_is_int(pre_mode) &&
1676 get_mode_size_bits(pre_mode) == bits &&
1677 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1678 /* ok, this conv just changes to sign, moreover the calculation
1679 * is done with same number of bits as our address mode, so
1680 * we can ignore the conv as address calculation can be viewed
1681 * as either signed or unsigned
1683 set_binop_left(n, pre);
1688 if (is_Conv(right)) {
1689 ir_mode *rmode = get_irn_mode(right);
1690 unsigned bits = get_mode_size_bits(rmode);
1692 if (ref_bits == bits &&
1693 mode_is_int(rmode) &&
1694 get_mode_arithmetic(rmode) == irma_twos_complement) {
1695 ir_node *pre = get_Conv_op(right);
1696 ir_mode *pre_mode = get_irn_mode(pre);
1698 if (mode_is_int(pre_mode) &&
1699 get_mode_size_bits(pre_mode) == bits &&
1700 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1701 /* ok, this conv just changes to sign, moreover the calculation
1702 * is done with same number of bits as our address mode, so
1703 * we can ignore the conv as address calculation can be viewed
1704 * as either signed or unsigned
1706 set_binop_right(n, pre);
1711 /* let address arithmetic use unsigned modes */
1712 if (is_Const(right)) {
1713 ir_mode *rmode = get_irn_mode(right);
1715 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1716 /* convert a AddP(P, *s) into AddP(P, *u) */
1717 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1719 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1720 set_binop_right(n, pre);
1728 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1731 if (is_Const(b) && is_const_Phi(a)) { \
1732 /* check for Op(Phi, Const) */ \
1733 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1735 else if (is_Const(a) && is_const_Phi(b)) { \
1736 /* check for Op(Const, Phi) */ \
1737 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1739 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1740 /* check for Op(Phi, Phi) */ \
1741 c = apply_binop_on_2_phis(a, b, eval, mode); \
1744 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1749 #define HANDLE_UNOP_PHI(eval, a, c) \
1752 if (is_const_Phi(a)) { \
1753 /* check for Op(Phi) */ \
1754 c = apply_unop_on_phi(a, eval); \
1756 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1763 * Create a 0 constant of given mode.
1765 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1767 ir_tarval *tv = get_mode_null(mode);
1768 ir_node *cnst = new_r_Const(irg, tv);
1773 static bool is_shiftop(const ir_node *n)
1775 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1778 /* the order of the values is important! */
1779 typedef enum const_class {
1785 static const_class classify_const(const ir_node* n)
1787 if (is_Const(n)) return const_const;
1788 if (is_irn_constlike(n)) return const_like;
1793 * Determines whether r is more constlike or has a larger index (in that order)
1796 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1798 const const_class l_order = classify_const(l);
1799 const const_class r_order = classify_const(r);
1801 l_order > r_order ||
1802 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1805 static bool is_cmp_unequal(const ir_node *node)
1807 ir_relation relation = get_Cmp_relation(node);
1808 ir_node *left = get_Cmp_left(node);
1809 ir_node *right = get_Cmp_right(node);
1810 ir_mode *mode = get_irn_mode(left);
1812 if (relation == ir_relation_less_greater)
1815 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1816 return relation == ir_relation_greater;
1821 * returns true for Cmp(x == 0) or Cmp(x != 0)
1823 static bool is_cmp_equality_zero(const ir_node *node)
1825 ir_relation relation;
1826 ir_node *right = get_Cmp_right(node);
1828 if (!is_Const(right) || !is_Const_null(right))
1830 relation = get_Cmp_relation(node);
1831 return relation == ir_relation_equal
1832 || relation == ir_relation_less_greater
1833 || (!mode_is_signed(get_irn_mode(right))
1834 && relation == ir_relation_greater);
1838 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1839 * Such pattern may arise in bitfield stores.
1841 * value c4 value c4 & c2
1842 * AND c3 AND c1 | c3
1849 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1852 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1854 ir_node *irn_and, *c1;
1856 ir_node *and_l, *c3;
1857 ir_node *value, *c4;
1858 ir_node *new_and, *new_const, *block;
1859 ir_mode *mode = get_irn_mode(irn_or);
1861 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1865 irn_and = get_binop_left(irn_or);
1866 c1 = get_binop_right(irn_or);
1867 if (!is_Const(c1) || !is_And(irn_and))
1870 or_l = get_binop_left(irn_and);
1871 c2 = get_binop_right(irn_and);
1875 tv1 = get_Const_tarval(c1);
1876 tv2 = get_Const_tarval(c2);
1878 tv = tarval_or(tv1, tv2);
1879 if (tarval_is_all_one(tv)) {
1880 /* the AND does NOT clear a bit with isn't set by the OR */
1881 set_binop_left(irn_or, or_l);
1882 set_binop_right(irn_or, c1);
1884 /* check for more */
1888 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
1891 and_l = get_binop_left(or_l);
1892 c3 = get_binop_right(or_l);
1893 if (!is_Const(c3) || !is_And(and_l))
1896 value = get_binop_left(and_l);
1897 c4 = get_binop_right(and_l);
1901 /* ok, found the pattern, check for conditions */
1902 assert(mode == get_irn_mode(irn_and));
1903 assert(mode == get_irn_mode(or_l));
1904 assert(mode == get_irn_mode(and_l));
1906 tv3 = get_Const_tarval(c3);
1907 tv4 = get_Const_tarval(c4);
1909 tv = tarval_or(tv4, tv2);
1910 if (!tarval_is_all_one(tv)) {
1911 /* have at least one 0 at the same bit position */
1915 if (tv3 != tarval_andnot(tv3, tv4)) {
1916 /* bit in the or_mask is outside the and_mask */
1920 if (tv1 != tarval_andnot(tv1, tv2)) {
1921 /* bit in the or_mask is outside the and_mask */
1925 /* ok, all conditions met */
1926 block = get_nodes_block(irn_or);
1927 irg = get_irn_irg(block);
1929 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
1931 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
1933 set_binop_left(irn_or, new_and);
1934 set_binop_right(irn_or, new_const);
1936 /* check for more */
1941 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
1943 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
1945 ir_mode *mode = get_irn_mode(irn_or);
1946 ir_node *shl, *shr, *block;
1947 ir_node *irn, *x, *c1, *c2, *n;
1948 ir_tarval *tv1, *tv2;
1950 /* some backends can't handle rotl */
1951 if (!be_get_backend_param()->support_rotl)
1954 if (! mode_is_int(mode))
1957 shl = get_binop_left(irn_or);
1958 shr = get_binop_right(irn_or);
1967 } else if (!is_Shl(shl)) {
1969 } else if (!is_Shr(shr)) {
1972 x = get_Shl_left(shl);
1973 if (x != get_Shr_left(shr))
1976 c1 = get_Shl_right(shl);
1977 c2 = get_Shr_right(shr);
1978 if (is_Const(c1) && is_Const(c2)) {
1979 tv1 = get_Const_tarval(c1);
1980 if (! tarval_is_long(tv1))
1983 tv2 = get_Const_tarval(c2);
1984 if (! tarval_is_long(tv2))
1987 if (get_tarval_long(tv1) + get_tarval_long(tv2)
1988 != (int) get_mode_size_bits(mode))
1991 /* yet, condition met */
1992 block = get_nodes_block(irn_or);
1994 n = new_r_Rotl(block, x, c1, mode);
1996 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2000 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2001 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2002 if (!ir_is_negated_value(c1, c2)) {
2006 /* yet, condition met */
2007 block = get_nodes_block(irn_or);
2008 n = new_r_Rotl(block, x, c1, mode);
2009 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2014 * Prototype of a recursive transform function
2015 * for bitwise distributive transformations.
2017 typedef ir_node* (*recursive_transform)(ir_node *n);
2020 * makes use of distributive laws for and, or, eor
2021 * and(a OP c, b OP c) -> and(a, b) OP c
2022 * note, might return a different op than n
2024 static ir_node *transform_bitwise_distributive(ir_node *n,
2025 recursive_transform trans_func)
2028 ir_node *a = get_binop_left(n);
2029 ir_node *b = get_binop_right(n);
2030 ir_op *op = get_irn_op(a);
2031 ir_op *op_root = get_irn_op(n);
2033 if (op != get_irn_op(b))
2036 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2037 if (op == op_Conv) {
2038 ir_node *a_op = get_Conv_op(a);
2039 ir_node *b_op = get_Conv_op(b);
2040 ir_mode *a_mode = get_irn_mode(a_op);
2041 ir_mode *b_mode = get_irn_mode(b_op);
2042 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2043 ir_node *blk = get_nodes_block(n);
2046 set_binop_left(n, a_op);
2047 set_binop_right(n, b_op);
2048 set_irn_mode(n, a_mode);
2050 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2052 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2058 /* nothing to gain here */
2062 if (op == op_Shrs || op == op_Shr || op == op_Shl
2063 || op == op_And || op == op_Or || op == op_Eor) {
2064 ir_node *a_left = get_binop_left(a);
2065 ir_node *a_right = get_binop_right(a);
2066 ir_node *b_left = get_binop_left(b);
2067 ir_node *b_right = get_binop_right(b);
2069 ir_node *op1 = NULL;
2070 ir_node *op2 = NULL;
2072 if (is_op_commutative(op)) {
2073 if (a_left == b_left) {
2077 } else if (a_left == b_right) {
2081 } else if (a_right == b_left) {
2087 if (a_right == b_right) {
2094 /* (a sop c) & (b sop c) => (a & b) sop c */
2095 ir_node *blk = get_nodes_block(n);
2097 ir_node *new_n = exact_copy(n);
2098 set_binop_left(new_n, op1);
2099 set_binop_right(new_n, op2);
2100 new_n = trans_func(new_n);
2102 if (op_root == op_Eor && op == op_Or) {
2103 dbg_info *dbgi = get_irn_dbg_info(n);
2104 ir_mode *mode = get_irn_mode(c);
2106 c = new_rd_Not(dbgi, blk, c, mode);
2107 n = new_rd_And(dbgi, blk, new_n, c, mode);
2110 set_nodes_block(n, blk);
2111 set_binop_left(n, new_n);
2112 set_binop_right(n, c);
2116 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2125 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2127 * - and, or, xor instead of &
2128 * - Shl, Shr, Shrs, rotl instead of >>
2129 * (with a special case for Or/Xor + Shrs)
2131 * This normalisation is usually good for the backend since << C can often be
2132 * matched as address-mode.
2134 static ir_node *transform_node_bitop_shift(ir_node *n)
2136 ir_graph *irg = get_irn_irg(n);
2137 ir_node *left = get_binop_left(n);
2138 ir_node *right = get_binop_right(n);
2139 ir_mode *mode = get_irn_mode(n);
2140 ir_node *shift_left;
2141 ir_node *shift_right;
2143 dbg_info *dbg_bitop;
2144 dbg_info *dbg_shift;
2150 ir_tarval *tv_bitop;
2152 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2155 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2156 if (!is_Const(right) || !is_shiftop(left))
2159 shift_left = get_binop_left(left);
2160 shift_right = get_binop_right(left);
2161 if (!is_Const(shift_right))
2164 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2165 if (is_Shrs(left)) {
2166 /* TODO this could be improved */
2170 irg = get_irn_irg(n);
2171 block = get_nodes_block(n);
2172 dbg_bitop = get_irn_dbg_info(n);
2173 dbg_shift = get_irn_dbg_info(left);
2174 tv1 = get_Const_tarval(shift_right);
2175 tv2 = get_Const_tarval(right);
2176 assert(get_tarval_mode(tv2) == mode);
2179 tv_bitop = tarval_shr(tv2, tv1);
2181 /* Check whether we have lost some bits during the right shift. */
2183 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2185 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2188 } else if (is_Shr(left)) {
2191 * TODO this can be improved by checking whether
2192 * the left shift produces an overflow
2196 tv_bitop = tarval_shl(tv2, tv1);
2198 assert(is_Rotl(left));
2199 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2201 new_const = new_r_Const(irg, tv_bitop);
2204 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2205 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2206 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2209 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2213 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2214 } else if (is_Shr(left)) {
2215 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2217 assert(is_Rotl(left));
2218 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2224 static bool complement_values(const ir_node *a, const ir_node *b)
2226 if (is_Not(a) && get_Not_op(a) == b)
2228 if (is_Not(b) && get_Not_op(b) == a)
2230 if (is_Const(a) && is_Const(b)) {
2231 ir_tarval *tv_a = get_Const_tarval(a);
2232 ir_tarval *tv_b = get_Const_tarval(b);
2233 return tarval_not(tv_a) == tv_b;
2238 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2241 * for associative operations fold:
2242 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2243 * This is a "light" version of the reassociation phase
2245 static ir_node *fold_constant_associativity(ir_node *node,
2246 tv_fold_binop_func fold)
2251 ir_node *right = get_binop_right(node);
2252 ir_node *left_right;
2259 if (!is_Const(right))
2262 op = get_irn_op(node);
2263 left = get_binop_left(node);
2264 if (get_irn_op(left) != op)
2267 left_right = get_binop_right(left);
2268 if (!is_Const(left_right))
2271 left_left = get_binop_left(left);
2272 c0 = get_Const_tarval(left_right);
2273 c1 = get_Const_tarval(right);
2274 irg = get_irn_irg(node);
2275 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2277 new_c = fold(c0, c1);
2278 if (new_c == tarval_bad)
2280 new_const = new_r_Const(irg, new_c);
2281 new_node = exact_copy(node);
2282 set_binop_left(new_node, left_left);
2283 set_binop_right(new_node, new_const);
2290 static ir_node *transform_node_Or_(ir_node *n)
2293 ir_node *a = get_binop_left(n);
2294 ir_node *b = get_binop_right(n);
2298 n = fold_constant_associativity(n, tarval_or);
2302 if (is_Not(a) && is_Not(b)) {
2303 /* ~a | ~b = ~(a&b) */
2304 ir_node *block = get_nodes_block(n);
2306 mode = get_irn_mode(n);
2309 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2310 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2311 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2315 /* we can combine the relations of two compares with the same operands */
2316 if (is_Cmp(a) && is_Cmp(b)) {
2317 ir_node *a_left = get_Cmp_left(a);
2318 ir_node *a_right = get_Cmp_right(a);
2319 ir_node *b_left = get_Cmp_left(b);
2320 ir_node *b_right = get_Cmp_right(b);
2321 if (a_left == b_left && b_left == b_right) {
2322 dbg_info *dbgi = get_irn_dbg_info(n);
2323 ir_node *block = get_nodes_block(n);
2324 ir_relation a_relation = get_Cmp_relation(a);
2325 ir_relation b_relation = get_Cmp_relation(b);
2326 ir_relation new_relation = a_relation | b_relation;
2327 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2329 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2330 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2331 && !mode_is_float(get_irn_mode(a_left))
2332 && !mode_is_float(get_irn_mode(b_left))) {
2333 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2334 ir_graph *irg = get_irn_irg(n);
2335 dbg_info *dbgi = get_irn_dbg_info(n);
2336 ir_node *block = get_nodes_block(n);
2337 ir_mode *a_mode = get_irn_mode(a_left);
2338 ir_mode *b_mode = get_irn_mode(b_left);
2339 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2340 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2341 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2342 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2343 ir_node *zero = create_zero_const(irg, b_mode);
2344 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2346 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2347 ir_graph *irg = get_irn_irg(n);
2348 dbg_info *dbgi = get_irn_dbg_info(n);
2349 ir_node *block = get_nodes_block(n);
2350 ir_mode *a_mode = get_irn_mode(a_left);
2351 ir_mode *b_mode = get_irn_mode(b_left);
2352 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2353 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2354 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2355 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2356 ir_node *zero = create_zero_const(irg, a_mode);
2357 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2362 mode = get_irn_mode(n);
2363 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2365 n = transform_node_Or_bf_store(n);
2368 n = transform_node_Or_Rotl(n);
2372 n = transform_bitwise_distributive(n, transform_node_Or_);
2375 n = transform_node_bitop_shift(n);
2382 static ir_node *transform_node_Or(ir_node *n)
2384 if (is_Or_Eor_Add(n)) {
2385 dbg_info *dbgi = get_irn_dbg_info(n);
2386 ir_node *block = get_nodes_block(n);
2387 ir_node *left = get_Or_left(n);
2388 ir_node *right = get_Or_right(n);
2389 ir_mode *mode = get_irn_mode(n);
2390 return new_rd_Add(dbgi, block, left, right, mode);
2392 return transform_node_Or_(n);
2398 static ir_node *transform_node_Eor_(ir_node *n)
2401 ir_node *a = get_binop_left(n);
2402 ir_node *b = get_binop_right(n);
2403 ir_mode *mode = get_irn_mode(n);
2406 n = fold_constant_associativity(n, tarval_eor);
2410 /* we can combine the relations of two compares with the same operands */
2411 if (is_Cmp(a) && is_Cmp(b)) {
2412 ir_node *a_left = get_Cmp_left(a);
2413 ir_node *a_right = get_Cmp_left(a);
2414 ir_node *b_left = get_Cmp_left(b);
2415 ir_node *b_right = get_Cmp_right(b);
2416 if (a_left == b_left && b_left == b_right) {
2417 dbg_info *dbgi = get_irn_dbg_info(n);
2418 ir_node *block = get_nodes_block(n);
2419 ir_relation a_relation = get_Cmp_relation(a);
2420 ir_relation b_relation = get_Cmp_relation(b);
2421 ir_relation new_relation = a_relation ^ b_relation;
2422 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2426 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2428 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2429 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2430 dbg_info *dbg = get_irn_dbg_info(n);
2431 ir_node *block = get_nodes_block(n);
2432 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2433 ir_node *new_left = get_Not_op(a);
2434 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2437 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2438 dbg_info *dbg = get_irn_dbg_info(n);
2439 ir_node *block = get_nodes_block(n);
2440 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2441 ir_node *new_right = get_Not_op(b);
2442 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2447 /* x ^ 1...1 -> ~1 */
2448 if (is_Const(b) && is_Const_all_one(b)) {
2449 n = new_r_Not(get_nodes_block(n), a, mode);
2450 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2454 n = transform_bitwise_distributive(n, transform_node_Eor_);
2457 n = transform_node_bitop_shift(n);
2464 static ir_node *transform_node_Eor(ir_node *n)
2466 if (is_Or_Eor_Add(n)) {
2467 dbg_info *dbgi = get_irn_dbg_info(n);
2468 ir_node *block = get_nodes_block(n);
2469 ir_node *left = get_Eor_left(n);
2470 ir_node *right = get_Eor_right(n);
2471 ir_mode *mode = get_irn_mode(n);
2472 return new_rd_Add(dbgi, block, left, right, mode);
2474 return transform_node_Eor_(n);
2478 * Do the AddSub optimization, then Transform
2479 * Constant folding on Phi
2480 * Add(a,a) -> Mul(a, 2)
2481 * Add(Mul(a, x), a) -> Mul(a, x+1)
2482 * if the mode is integer or float.
2483 * Transform Add(a,-b) into Sub(a,b).
2484 * Reassociation might fold this further.
2486 static ir_node *transform_node_Add(ir_node *n)
2494 n = fold_constant_associativity(n, tarval_add);
2498 n = transform_node_AddSub(n);
2502 a = get_Add_left(n);
2503 b = get_Add_right(n);
2504 mode = get_irn_mode(n);
2506 if (mode_is_reference(mode)) {
2507 ir_mode *lmode = get_irn_mode(a);
2509 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2510 /* an Add(a, NULL) is a hidden Conv */
2511 dbg_info *dbg = get_irn_dbg_info(n);
2512 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2516 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2517 ir_tarval *tv = get_Const_tarval(b);
2518 ir_tarval *min = get_mode_min(mode);
2519 /* if all bits are set, then this has the same effect as a Not.
2520 * Note that the following == gives false for different modes which
2521 * is exactly what we want */
2523 dbg_info *dbgi = get_irn_dbg_info(n);
2524 ir_graph *irg = get_irn_irg(n);
2525 ir_node *block = get_nodes_block(n);
2526 ir_node *cnst = new_r_Const(irg, min);
2527 return new_rd_Eor(dbgi, block, a, cnst, mode);
2531 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2533 /* for FP the following optimizations are only allowed if
2534 * fp_strict_algebraic is disabled */
2535 if (mode_is_float(mode)) {
2536 ir_graph *irg = get_irn_irg(n);
2537 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2541 if (mode_is_num(mode)) {
2542 ir_graph *irg = get_irn_irg(n);
2543 /* the following code leads to endless recursion when Mul are replaced
2544 * by a simple instruction chain */
2545 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2546 && a == b && mode_is_int(mode)) {
2547 ir_node *block = get_nodes_block(n);
2550 get_irn_dbg_info(n),
2553 new_r_Const_long(irg, mode, 2),
2555 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2560 get_irn_dbg_info(n),
2565 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2570 get_irn_dbg_info(n),
2575 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2578 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2579 /* Here we rely on constants be on the RIGHT side */
2581 ir_node *op = get_Not_op(a);
2583 if (is_Const(b) && is_Const_one(b)) {
2585 ir_node *blk = get_nodes_block(n);
2586 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2587 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2594 if (is_Or_Eor_Add(n)) {
2595 n = transform_node_Or_(n);
2598 n = transform_node_Eor_(n);
2607 * returns -cnst or NULL if impossible
2609 static ir_node *const_negate(ir_node *cnst)
2611 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2612 dbg_info *dbgi = get_irn_dbg_info(cnst);
2613 ir_graph *irg = get_irn_irg(cnst);
2614 if (tv == tarval_bad) return NULL;
2615 return new_rd_Const(dbgi, irg, tv);
2619 * Do the AddSub optimization, then Transform
2620 * Constant folding on Phi
2621 * Sub(0,a) -> Minus(a)
2622 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2623 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2624 * Sub(Add(a, x), x) -> a
2625 * Sub(x, Add(x, a)) -> -a
2626 * Sub(x, Const) -> Add(x, -Const)
2628 static ir_node *transform_node_Sub(ir_node *n)
2634 n = transform_node_AddSub(n);
2636 a = get_Sub_left(n);
2637 b = get_Sub_right(n);
2639 mode = get_irn_mode(n);
2641 if (mode_is_int(mode)) {
2642 ir_mode *lmode = get_irn_mode(a);
2644 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2645 /* a Sub(a, NULL) is a hidden Conv */
2646 dbg_info *dbg = get_irn_dbg_info(n);
2647 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2648 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2652 if (mode == lmode &&
2653 get_mode_arithmetic(mode) == irma_twos_complement &&
2655 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2657 dbg_info *dbg = get_irn_dbg_info(n);
2658 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2659 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2665 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2667 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2668 if (mode_is_float(mode)) {
2669 ir_graph *irg = get_irn_irg(n);
2670 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2674 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2675 /* a - C -> a + (-C) */
2676 ir_node *cnst = const_negate(b);
2678 ir_node *block = get_nodes_block(n);
2679 dbg_info *dbgi = get_irn_dbg_info(n);
2681 n = new_rd_Add(dbgi, block, a, cnst, mode);
2682 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2687 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2688 dbg_info *dbg = get_irn_dbg_info(n);
2689 ir_node *block = get_nodes_block(n);
2690 ir_node *left = get_Minus_op(a);
2691 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2693 n = new_rd_Minus(dbg, block, add, mode);
2694 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2696 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2697 dbg_info *dbg = get_irn_dbg_info(n);
2698 ir_node *block = get_nodes_block(n);
2699 ir_node *right = get_Minus_op(b);
2701 n = new_rd_Add(dbg, block, a, right, mode);
2702 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2704 } else if (is_Sub(b)) {
2705 /* a - (b - c) -> a + (c - b)
2706 * -> (a - b) + c iff (b - c) is a pointer */
2707 dbg_info *s_dbg = get_irn_dbg_info(b);
2708 ir_node *s_left = get_Sub_left(b);
2709 ir_node *s_right = get_Sub_right(b);
2710 ir_mode *s_mode = get_irn_mode(b);
2711 if (mode_is_reference(s_mode)) {
2712 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2713 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2714 dbg_info *a_dbg = get_irn_dbg_info(n);
2717 s_right = new_r_Conv(lowest_block, s_right, mode);
2718 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2720 ir_node *s_block = get_nodes_block(b);
2721 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2722 dbg_info *a_dbg = get_irn_dbg_info(n);
2723 ir_node *a_block = get_nodes_block(n);
2725 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2727 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2730 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2731 ir_node *m_right = get_Mul_right(b);
2732 if (is_Const(m_right)) {
2733 ir_node *cnst2 = const_negate(m_right);
2734 if (cnst2 != NULL) {
2735 dbg_info *m_dbg = get_irn_dbg_info(b);
2736 ir_node *m_block = get_nodes_block(b);
2737 ir_node *m_left = get_Mul_left(b);
2738 ir_mode *m_mode = get_irn_mode(b);
2739 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2740 dbg_info *a_dbg = get_irn_dbg_info(n);
2741 ir_node *a_block = get_nodes_block(n);
2743 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2744 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2751 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2752 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2754 get_irn_dbg_info(n),
2758 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2761 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2762 ir_node *left = get_binop_left(a);
2763 ir_node *right = get_binop_right(a);
2765 /* FIXME: Does the Conv's work only for two complement or generally? */
2767 if (mode != get_irn_mode(right)) {
2768 /* This Sub is an effective Cast */
2769 right = new_r_Conv(get_nodes_block(n), right, mode);
2772 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2774 } else if (right == b) {
2775 if (mode != get_irn_mode(left)) {
2776 /* This Sub is an effective Cast */
2777 left = new_r_Conv(get_nodes_block(n), left, mode);
2780 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2784 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2785 ir_node *left = get_binop_left(b);
2786 ir_node *right = get_binop_right(b);
2788 /* FIXME: Does the Conv's work only for two complement or generally? */
2790 ir_mode *r_mode = get_irn_mode(right);
2792 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2793 if (mode != r_mode) {
2794 /* This Sub is an effective Cast */
2795 n = new_r_Conv(get_nodes_block(n), n, mode);
2797 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2799 } else if (right == a) {
2800 ir_mode *l_mode = get_irn_mode(left);
2802 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2803 if (mode != l_mode) {
2804 /* This Sub is an effective Cast */
2805 n = new_r_Conv(get_nodes_block(n), n, mode);
2807 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2811 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2812 ir_mode *mode = get_irn_mode(a);
2814 if (mode == get_irn_mode(b)) {
2816 ir_node *op_a = get_Conv_op(a);
2817 ir_node *op_b = get_Conv_op(b);
2819 /* check if it's allowed to skip the conv */
2820 ma = get_irn_mode(op_a);
2821 mb = get_irn_mode(op_b);
2823 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2824 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2827 set_Sub_right(n, b);
2833 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2834 if (!is_reassoc_running() && is_Mul(a)) {
2835 ir_node *ma = get_Mul_left(a);
2836 ir_node *mb = get_Mul_right(a);
2839 ir_node *blk = get_nodes_block(n);
2840 ir_graph *irg = get_irn_irg(n);
2842 get_irn_dbg_info(n),
2846 get_irn_dbg_info(n),
2849 new_r_Const(irg, get_mode_one(mode)),
2852 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2854 } else if (mb == b) {
2855 ir_node *blk = get_nodes_block(n);
2856 ir_graph *irg = get_irn_irg(n);
2858 get_irn_dbg_info(n),
2862 get_irn_dbg_info(n),
2865 new_r_Const(irg, get_mode_one(mode)),
2868 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2872 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2873 ir_node *x = get_Sub_left(a);
2874 ir_node *y = get_Sub_right(a);
2875 ir_node *blk = get_nodes_block(n);
2876 ir_mode *m_b = get_irn_mode(b);
2877 ir_mode *m_y = get_irn_mode(y);
2881 /* Determine the right mode for the Add. */
2884 else if (mode_is_reference(m_b))
2886 else if (mode_is_reference(m_y))
2890 * Both modes are different but none is reference,
2891 * happens for instance in SubP(SubP(P, Iu), Is).
2892 * We have two possibilities here: Cast or ignore.
2893 * Currently we ignore this case.
2898 add = new_r_Add(blk, y, b, add_mode);
2900 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2901 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2905 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2906 /* c - ~X = X + (c+1) */
2907 if (is_Const(a) && is_Not(b)) {
2908 ir_tarval *tv = get_Const_tarval(a);
2910 tv = tarval_add(tv, get_mode_one(mode));
2911 if (tv != tarval_bad) {
2912 ir_node *blk = get_nodes_block(n);
2913 ir_graph *irg = get_irn_irg(n);
2914 ir_node *c = new_r_Const(irg, tv);
2915 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2916 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2920 /* x-(x&y) = x & ~y */
2922 ir_node *and_left = get_And_left(b);
2923 ir_node *and_right = get_And_right(b);
2924 if (and_right == a) {
2925 ir_node *tmp = and_left;
2926 and_left = and_right;
2929 if (and_left == a) {
2930 dbg_info *dbgi = get_irn_dbg_info(n);
2931 ir_node *block = get_nodes_block(n);
2932 ir_mode *mode = get_irn_mode(n);
2933 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2934 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2943 * Several transformation done on n*n=2n bits mul.
2944 * These transformations must be done here because new nodes may be produced.
2946 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2949 ir_node *a = get_Mul_left(n);
2950 ir_node *b = get_Mul_right(n);
2951 ir_tarval *ta = value_of(a);
2952 ir_tarval *tb = value_of(b);
2953 ir_mode *smode = get_irn_mode(a);
2955 if (ta == get_mode_one(smode)) {
2956 /* (L)1 * (L)b = (L)b */
2957 ir_node *blk = get_nodes_block(n);
2958 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2959 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2962 else if (ta == get_mode_minus_one(smode)) {
2963 /* (L)-1 * (L)b = (L)b */
2964 ir_node *blk = get_nodes_block(n);
2965 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2966 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2967 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2970 if (tb == get_mode_one(smode)) {
2971 /* (L)a * (L)1 = (L)a */
2972 ir_node *blk = get_nodes_block(a);
2973 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2974 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2977 else if (tb == get_mode_minus_one(smode)) {
2978 /* (L)a * (L)-1 = (L)-a */
2979 ir_node *blk = get_nodes_block(n);
2980 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2981 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2982 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2989 * Transform Mul(a,-1) into -a.
2990 * Do constant evaluation of Phi nodes.
2991 * Do architecture dependent optimizations on Mul nodes
2993 static ir_node *transform_node_Mul(ir_node *n)
2995 ir_node *c, *oldn = n;
2996 ir_mode *mode = get_irn_mode(n);
2997 ir_node *a = get_Mul_left(n);
2998 ir_node *b = get_Mul_right(n);
3000 n = fold_constant_associativity(n, tarval_mul);
3004 if (mode != get_irn_mode(a))
3005 return transform_node_Mul2n(n, mode);
3007 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3009 if (mode_is_signed(mode)) {
3012 if (value_of(a) == get_mode_minus_one(mode))
3014 else if (value_of(b) == get_mode_minus_one(mode))
3017 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3018 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3023 if (is_Const(b)) { /* (-a) * const -> a * -const */
3024 ir_node *cnst = const_negate(b);
3026 dbg_info *dbgi = get_irn_dbg_info(n);
3027 ir_node *block = get_nodes_block(n);
3028 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3029 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3032 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3033 dbg_info *dbgi = get_irn_dbg_info(n);
3034 ir_node *block = get_nodes_block(n);
3035 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3036 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3038 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3039 ir_node *sub_l = get_Sub_left(b);
3040 ir_node *sub_r = get_Sub_right(b);
3041 dbg_info *dbgi = get_irn_dbg_info(n);
3042 ir_node *block = get_nodes_block(n);
3043 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3044 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3048 } else if (is_Minus(b)) {
3049 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3050 ir_node *sub_l = get_Sub_left(a);
3051 ir_node *sub_r = get_Sub_right(a);
3052 dbg_info *dbgi = get_irn_dbg_info(n);
3053 ir_node *block = get_nodes_block(n);
3054 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3055 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3056 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3059 } else if (is_Shl(a)) {
3060 ir_node *const shl_l = get_Shl_left(a);
3061 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3062 /* (1 << x) * b -> b << x */
3063 dbg_info *const dbgi = get_irn_dbg_info(n);
3064 ir_node *const block = get_nodes_block(n);
3065 ir_node *const shl_r = get_Shl_right(a);
3066 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3067 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3070 } else if (is_Shl(b)) {
3071 ir_node *const shl_l = get_Shl_left(b);
3072 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3073 /* a * (1 << x) -> a << x */
3074 dbg_info *const dbgi = get_irn_dbg_info(n);
3075 ir_node *const block = get_nodes_block(n);
3076 ir_node *const shl_r = get_Shl_right(b);
3077 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3078 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3082 if (get_mode_arithmetic(mode) == irma_ieee754
3083 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3085 ir_tarval *tv = get_Const_tarval(a);
3086 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3087 && !tarval_is_negative(tv)) {
3088 /* 2.0 * b = b + b */
3089 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3090 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3094 else if (is_Const(b)) {
3095 ir_tarval *tv = get_Const_tarval(b);
3096 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3097 && !tarval_is_negative(tv)) {
3098 /* a * 2.0 = a + a */
3099 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3100 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3105 return arch_dep_replace_mul_with_shifts(n);
3109 * Transform a Div Node.
3111 static ir_node *transform_node_Div(ir_node *n)
3113 ir_mode *mode = get_Div_resmode(n);
3114 ir_node *a = get_Div_left(n);
3115 ir_node *b = get_Div_right(n);
3117 const ir_node *dummy;
3119 if (mode_is_int(mode)) {
3120 if (is_Const(b) && is_const_Phi(a)) {
3121 /* check for Div(Phi, Const) */
3122 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3124 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3127 } else if (is_Const(a) && is_const_Phi(b)) {
3128 /* check for Div(Const, Phi) */
3129 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3131 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3134 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3135 /* check for Div(Phi, Phi) */
3136 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3138 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3143 if (a == b && value_not_zero(a, &dummy)) {
3144 ir_graph *irg = get_irn_irg(n);
3145 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3146 value = new_r_Const(irg, get_mode_one(mode));
3147 DBG_OPT_CSTEVAL(n, value);
3150 if (mode_is_signed(mode) && is_Const(b)) {
3151 ir_tarval *tv = get_Const_tarval(b);
3153 if (tv == get_mode_minus_one(mode)) {
3155 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3156 DBG_OPT_CSTEVAL(n, value);
3160 /* Try architecture dependent optimization */
3161 value = arch_dep_replace_div_by_const(n);
3164 assert(mode_is_float(mode));
3166 /* Optimize x/c to x*(1/c) */
3167 if (get_mode_arithmetic(mode) == irma_ieee754) {
3168 ir_tarval *tv = value_of(b);
3170 if (tv != tarval_bad) {
3171 int rem = tarval_fp_ops_enabled();
3174 * Floating point constant folding might be disabled here to
3176 * However, as we check for exact result, doing it is safe.
3179 tarval_enable_fp_ops(1);
3180 tv = tarval_div(get_mode_one(mode), tv);
3181 tarval_enable_fp_ops(rem);
3183 /* Do the transformation if the result is either exact or we are
3184 not using strict rules. */
3185 if (tv != tarval_bad &&
3186 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3187 ir_node *block = get_nodes_block(n);
3188 ir_graph *irg = get_irn_irg(block);
3189 ir_node *c = new_r_Const(irg, tv);
3190 dbg_info *dbgi = get_irn_dbg_info(n);
3191 value = new_rd_Mul(dbgi, block, a, c, mode);
3204 /* Turn Div into a tuple (mem, jmp, bad, value) */
3205 mem = get_Div_mem(n);
3206 blk = get_nodes_block(n);
3207 irg = get_irn_irg(blk);
3209 /* skip a potential Pin */
3210 mem = skip_Pin(mem);
3211 turn_into_tuple(n, pn_Div_max+1);
3212 set_Tuple_pred(n, pn_Div_M, mem);
3213 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3214 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3215 set_Tuple_pred(n, pn_Div_res, value);
3221 * Transform a Mod node.
3223 static ir_node *transform_node_Mod(ir_node *n)
3225 ir_mode *mode = get_Mod_resmode(n);
3226 ir_node *a = get_Mod_left(n);
3227 ir_node *b = get_Mod_right(n);
3232 if (is_Const(b) && is_const_Phi(a)) {
3233 /* check for Div(Phi, Const) */
3234 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3236 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3240 else if (is_Const(a) && is_const_Phi(b)) {
3241 /* check for Div(Const, Phi) */
3242 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3244 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3248 else if (is_const_Phi(a) && is_const_Phi(b)) {
3249 /* check for Div(Phi, Phi) */
3250 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3252 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3259 irg = get_irn_irg(n);
3260 if (tv != tarval_bad) {
3261 value = new_r_Const(irg, tv);
3263 DBG_OPT_CSTEVAL(n, value);
3266 ir_node *a = get_Mod_left(n);
3267 ir_node *b = get_Mod_right(n);
3268 const ir_node *dummy;
3270 if (a == b && value_not_zero(a, &dummy)) {
3271 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3272 value = new_r_Const(irg, get_mode_null(mode));
3273 DBG_OPT_CSTEVAL(n, value);
3276 if (mode_is_signed(mode) && is_Const(b)) {
3277 ir_tarval *tv = get_Const_tarval(b);
3279 if (tv == get_mode_minus_one(mode)) {
3281 value = new_r_Const(irg, get_mode_null(mode));
3282 DBG_OPT_CSTEVAL(n, value);
3286 /* Try architecture dependent optimization */
3287 value = arch_dep_replace_mod_by_const(n);
3296 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3297 mem = get_Mod_mem(n);
3298 blk = get_nodes_block(n);
3299 irg = get_irn_irg(blk);
3301 /* skip a potential Pin */
3302 mem = skip_Pin(mem);
3303 turn_into_tuple(n, pn_Mod_max+1);
3304 set_Tuple_pred(n, pn_Mod_M, mem);
3305 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3306 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3307 set_Tuple_pred(n, pn_Mod_res, value);
3313 * Transform a Cond node.
3315 * Replace the Cond by a Jmp if it branches on a constant
3318 static ir_node *transform_node_Cond(ir_node *n)
3320 ir_node *a = get_Cond_selector(n);
3321 ir_graph *irg = get_irn_irg(n);
3325 /* we need block info which is not available in floating irgs */
3326 if (get_irg_pinned(irg) == op_pin_state_floats)
3330 if (ta == tarval_bad && is_Cmp(a)) {
3331 /* try again with a direct call to compute_cmp, as we don't care
3332 * about the MODEB_LOWERED flag here */
3333 ta = compute_cmp_ext(a);
3336 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3337 /* It's a boolean Cond, branching on a boolean constant.
3338 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3339 ir_node *blk = get_nodes_block(n);
3340 jmp = new_r_Jmp(blk);
3341 turn_into_tuple(n, pn_Cond_max+1);
3342 if (ta == tarval_b_true) {
3343 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3344 set_Tuple_pred(n, pn_Cond_true, jmp);
3346 set_Tuple_pred(n, pn_Cond_false, jmp);
3347 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3349 /* We might generate an endless loop, so keep it alive. */
3350 add_End_keepalive(get_irg_end(irg), blk);
3351 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3356 static ir_node *transform_node_Switch(ir_node *n)
3358 ir_node *op = get_Switch_selector(n);
3359 ir_tarval *val = value_of(op);
3360 if (val != tarval_bad) {
3361 dbg_info *dbgi = get_irn_dbg_info(n);
3362 ir_graph *irg = get_irn_irg(n);
3363 unsigned n_outs = get_Switch_n_outs(n);
3364 ir_node *block = get_nodes_block(n);
3365 ir_node *bad = new_r_Bad(irg, mode_X);
3366 ir_node **in = XMALLOCN(ir_node*, n_outs);
3367 const ir_switch_table *table = get_Switch_table(n);
3368 size_t n_entries = ir_switch_table_get_n_entries(table);
3372 for (i = 0; i < n_entries; ++i) {
3373 const ir_switch_table_entry *entry
3374 = ir_switch_table_get_entry_const(table, i);
3375 ir_tarval *min = entry->min;
3376 ir_tarval *max = entry->max;
3379 if ((min == max && min == val)
3380 || (tarval_cmp(val, min) != ir_relation_less
3381 && tarval_cmp(val, max) != ir_relation_greater)) {
3386 for (o = 0; o < n_outs; ++o) {
3387 if (o == (unsigned)jmp_pn) {
3388 in[o] = new_rd_Jmp(dbgi, block);
3393 return new_r_Tuple(block, (int)n_outs, in);
3399 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3401 * - and, or, xor instead of &
3402 * - Shl, Shr, Shrs, rotl instead of >>
3403 * (with a special case for Or/Xor + Shrs)
3405 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3407 static ir_node *transform_node_shift_bitop(ir_node *n)
3409 ir_graph *irg = get_irn_irg(n);
3410 ir_node *right = get_binop_right(n);
3411 ir_mode *mode = get_irn_mode(n);
3413 ir_node *bitop_left;
3414 ir_node *bitop_right;
3423 ir_tarval *tv_shift;
3425 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3428 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3430 if (!is_Const(right))
3433 left = get_binop_left(n);
3434 op_left = get_irn_op(left);
3435 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3438 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3439 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3440 /* TODO: test if sign bit is affectes */
3444 bitop_right = get_binop_right(left);
3445 if (!is_Const(bitop_right))
3448 bitop_left = get_binop_left(left);
3450 block = get_nodes_block(n);
3451 dbgi = get_irn_dbg_info(n);
3452 tv1 = get_Const_tarval(bitop_right);
3453 tv2 = get_Const_tarval(right);
3455 assert(get_tarval_mode(tv1) == mode);
3458 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3459 tv_shift = tarval_shl(tv1, tv2);
3460 } else if (is_Shr(n)) {
3461 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3462 tv_shift = tarval_shr(tv1, tv2);
3463 } else if (is_Shrs(n)) {
3464 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3465 tv_shift = tarval_shrs(tv1, tv2);
3468 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3469 tv_shift = tarval_rotl(tv1, tv2);
3472 assert(get_tarval_mode(tv_shift) == mode);
3473 irg = get_irn_irg(n);
3474 new_const = new_r_Const(irg, tv_shift);
3476 if (op_left == op_And) {
3477 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3478 } else if (op_left == op_Or) {
3479 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3481 assert(op_left == op_Eor);
3482 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3491 static ir_node *transform_node_And(ir_node *n)
3493 ir_node *c, *oldn = n;
3494 ir_node *a = get_And_left(n);
3495 ir_node *b = get_And_right(n);
3498 n = fold_constant_associativity(n, tarval_and);
3502 if (is_Cmp(a) && is_Cmp(b)) {
3503 ir_node *a_left = get_Cmp_left(a);
3504 ir_node *a_right = get_Cmp_right(a);
3505 ir_node *b_left = get_Cmp_left(b);
3506 ir_node *b_right = get_Cmp_right(b);
3507 ir_relation a_relation = get_Cmp_relation(a);
3508 ir_relation b_relation = get_Cmp_relation(b);
3509 /* we can combine the relations of two compares with the same
3511 if (a_left == b_left && b_left == b_right) {
3512 dbg_info *dbgi = get_irn_dbg_info(n);
3513 ir_node *block = get_nodes_block(n);
3514 ir_relation new_relation = a_relation & b_relation;
3515 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3517 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3518 if (a_relation == b_relation && a_relation == ir_relation_equal
3519 && !mode_is_float(get_irn_mode(a_left))
3520 && !mode_is_float(get_irn_mode(b_left))) {
3521 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3522 dbg_info *dbgi = get_irn_dbg_info(n);
3523 ir_node *block = get_nodes_block(n);
3524 ir_mode *a_mode = get_irn_mode(a_left);
3525 ir_mode *b_mode = get_irn_mode(b_left);
3526 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3527 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3528 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3529 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3530 ir_graph *irg = get_irn_irg(n);
3531 ir_node *zero = create_zero_const(irg, b_mode);
3532 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3534 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3535 dbg_info *dbgi = get_irn_dbg_info(n);
3536 ir_node *block = get_nodes_block(n);
3537 ir_mode *a_mode = get_irn_mode(a_left);
3538 ir_mode *b_mode = get_irn_mode(b_left);
3539 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3540 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3541 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3542 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3543 ir_graph *irg = get_irn_irg(n);
3544 ir_node *zero = create_zero_const(irg, a_mode);
3545 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3550 mode = get_irn_mode(n);
3551 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3553 if (is_Or(a) || is_Or_Eor_Add(a)) {
3554 ir_node *or_left = get_binop_left(a);
3555 ir_node *or_right = get_binop_right(a);
3556 if (complement_values(or_left, b)) {
3557 /* (a|b) & ~a => b & ~a */
3558 dbg_info *dbgi = get_irn_dbg_info(n);
3559 ir_node *block = get_nodes_block(n);
3560 return new_rd_And(dbgi, block, or_right, b, mode);
3561 } else if (complement_values(or_right, b)) {
3562 /* (a|b) & ~b => a & ~b */
3563 dbg_info *dbgi = get_irn_dbg_info(n);
3564 ir_node *block = get_nodes_block(n);
3565 return new_rd_And(dbgi, block, or_left, b, mode);
3566 } else if (is_Not(b)) {
3567 ir_node *op = get_Not_op(b);
3569 ir_node *ba = get_And_left(op);
3570 ir_node *bb = get_And_right(op);
3572 /* it's enough to test the following cases due to normalization! */
3573 if (or_left == ba && or_right == bb) {
3574 /* (a|b) & ~(a&b) = a^b */
3575 ir_node *block = get_nodes_block(n);
3577 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3578 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3584 if (is_Or(b) || is_Or_Eor_Add(b)) {
3585 ir_node *or_left = get_binop_left(b);
3586 ir_node *or_right = get_binop_right(b);
3587 if (complement_values(or_left, a)) {
3588 /* (a|b) & ~a => b & ~a */
3589 dbg_info *dbgi = get_irn_dbg_info(n);
3590 ir_node *block = get_nodes_block(n);
3591 return new_rd_And(dbgi, block, or_right, a, mode);
3592 } else if (complement_values(or_right, a)) {
3593 /* (a|b) & ~b => a & ~b */
3594 dbg_info *dbgi = get_irn_dbg_info(n);
3595 ir_node *block = get_nodes_block(n);
3596 return new_rd_And(dbgi, block, or_left, a, mode);
3597 } else if (is_Not(a)) {
3598 ir_node *op = get_Not_op(a);
3600 ir_node *aa = get_And_left(op);
3601 ir_node *ab = get_And_right(op);
3603 /* it's enough to test the following cases due to normalization! */
3604 if (or_left == aa && or_right == ab) {
3605 /* (a|b) & ~(a&b) = a^b */
3606 ir_node *block = get_nodes_block(n);
3608 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3609 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3615 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3616 ir_node *al = get_binop_left(a);
3617 ir_node *ar = get_binop_right(a);
3620 /* (b ^ a) & b -> ~a & b */
3621 dbg_info *dbg = get_irn_dbg_info(n);
3622 ir_node *block = get_nodes_block(n);
3624 ar = new_rd_Not(dbg, block, ar, mode);
3625 n = new_rd_And(dbg, block, ar, b, mode);
3626 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3630 /* (a ^ b) & b -> ~a & b */
3631 dbg_info *dbg = get_irn_dbg_info(n);
3632 ir_node *block = get_nodes_block(n);
3634 al = new_rd_Not(dbg, block, al, mode);
3635 n = new_rd_And(dbg, block, al, b, mode);
3636 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3640 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3641 ir_node *bl = get_binop_left(b);
3642 ir_node *br = get_binop_right(b);
3645 /* a & (a ^ b) -> a & ~b */
3646 dbg_info *dbg = get_irn_dbg_info(n);
3647 ir_node *block = get_nodes_block(n);
3649 br = new_rd_Not(dbg, block, br, mode);
3650 n = new_rd_And(dbg, block, br, a, mode);
3651 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3655 /* a & (b ^ a) -> a & ~b */
3656 dbg_info *dbg = get_irn_dbg_info(n);
3657 ir_node *block = get_nodes_block(n);
3659 bl = new_rd_Not(dbg, block, bl, mode);
3660 n = new_rd_And(dbg, block, bl, a, mode);
3661 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3665 if (is_Not(a) && is_Not(b)) {
3666 /* ~a & ~b = ~(a|b) */
3667 ir_node *block = get_nodes_block(n);
3668 ir_mode *mode = get_irn_mode(n);
3672 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3673 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3674 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3679 vrp_attr *b_vrp = vrp_get_info(b);
3680 ir_tarval *a_val = get_Const_tarval(a);
3681 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3687 vrp_attr *a_vrp = vrp_get_info(a);
3688 ir_tarval *b_val = get_Const_tarval(b);
3689 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3694 n = transform_bitwise_distributive(n, transform_node_And);
3696 n = transform_node_bitop_shift(n);
3704 static ir_node *transform_node_Not(ir_node *n)
3706 ir_node *c, *oldn = n;
3707 ir_node *a = get_Not_op(n);
3708 ir_mode *mode = get_irn_mode(n);
3710 HANDLE_UNOP_PHI(tarval_not,a,c);
3712 /* check for a boolean Not */
3714 dbg_info *dbgi = get_irn_dbg_info(a);
3715 ir_node *block = get_nodes_block(a);
3716 ir_relation relation = get_Cmp_relation(a);
3717 relation = get_negated_relation(relation);
3718 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3719 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3723 /* normalize ~(a ^ b) => a ^ ~b */
3724 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3725 dbg_info *dbg = get_irn_dbg_info(n);
3726 ir_node *block = get_nodes_block(n);
3727 ir_node *eor_right = get_binop_right(a);
3728 ir_node *eor_left = get_binop_left(a);
3729 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3730 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3734 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3735 if (is_Minus(a)) { /* ~-x -> x + -1 */
3736 dbg_info *dbg = get_irn_dbg_info(n);
3737 ir_graph *irg = get_irn_irg(n);
3738 ir_node *block = get_nodes_block(n);
3739 ir_node *add_l = get_Minus_op(a);
3740 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3741 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3742 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3743 ir_node *add_r = get_binop_right(a);
3744 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3745 /* ~(x + -1) = -x */
3746 ir_node *op = get_binop_left(a);
3747 ir_node *blk = get_nodes_block(n);
3748 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3757 * Transform a Minus.
3761 * -(a >>u (size-1)) = a >>s (size-1)
3762 * -(a >>s (size-1)) = a >>u (size-1)
3763 * -(a * const) -> a * -const
3765 static ir_node *transform_node_Minus(ir_node *n)
3767 ir_node *c, *oldn = n;
3768 ir_node *a = get_Minus_op(n);
3771 HANDLE_UNOP_PHI(tarval_neg,a,c);
3773 mode = get_irn_mode(a);
3774 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3775 /* the following rules are only to twos-complement */
3778 ir_node *op = get_Not_op(a);
3779 ir_tarval *tv = get_mode_one(mode);
3780 ir_node *blk = get_nodes_block(n);
3781 ir_graph *irg = get_irn_irg(blk);
3782 ir_node *c = new_r_Const(irg, tv);
3783 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3784 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3788 ir_node *c = get_Shr_right(a);
3791 ir_tarval *tv = get_Const_tarval(c);
3793 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3794 /* -(a >>u (size-1)) = a >>s (size-1) */
3795 ir_node *v = get_Shr_left(a);
3797 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3798 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3804 ir_node *c = get_Shrs_right(a);
3807 ir_tarval *tv = get_Const_tarval(c);
3809 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3810 /* -(a >>s (size-1)) = a >>u (size-1) */
3811 ir_node *v = get_Shrs_left(a);
3813 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3814 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3821 /* - (a-b) = b - a */
3822 ir_node *la = get_Sub_left(a);
3823 ir_node *ra = get_Sub_right(a);
3824 ir_node *blk = get_nodes_block(n);
3826 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3827 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3831 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3832 ir_node *mul_l = get_Mul_left(a);
3833 ir_node *mul_r = get_Mul_right(a);
3834 ir_tarval *tv = value_of(mul_r);
3835 if (tv != tarval_bad) {
3836 tv = tarval_neg(tv);
3837 if (tv != tarval_bad) {
3838 ir_graph *irg = get_irn_irg(n);
3839 ir_node *cnst = new_r_Const(irg, tv);
3840 dbg_info *dbg = get_irn_dbg_info(a);
3841 ir_node *block = get_nodes_block(a);
3842 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3843 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3853 * Transform a Proj(Load) with a non-null address.
3855 static ir_node *transform_node_Proj_Load(ir_node *proj)
3857 if (get_irn_mode(proj) == mode_X) {
3858 ir_node *load = get_Proj_pred(proj);
3860 /* get the Load address */
3861 const ir_node *addr = get_Load_ptr(load);
3862 const ir_node *confirm;
3864 if (value_not_null(addr, &confirm)) {
3865 if (confirm == NULL) {
3866 /* this node may float if it did not depend on a Confirm */
3867 set_irn_pinned(load, op_pin_state_floats);
3869 if (get_Proj_proj(proj) == pn_Load_X_except) {
3870 ir_graph *irg = get_irn_irg(proj);
3871 DBG_OPT_EXC_REM(proj);
3872 return new_r_Bad(irg, mode_X);
3874 ir_node *blk = get_nodes_block(load);
3875 return new_r_Jmp(blk);
3883 * Transform a Proj(Store) with a non-null address.
3885 static ir_node *transform_node_Proj_Store(ir_node *proj)
3887 if (get_irn_mode(proj) == mode_X) {
3888 ir_node *store = get_Proj_pred(proj);
3890 /* get the load/store address */
3891 const ir_node *addr = get_Store_ptr(store);
3892 const ir_node *confirm;
3894 if (value_not_null(addr, &confirm)) {
3895 if (confirm == NULL) {
3896 /* this node may float if it did not depend on a Confirm */
3897 set_irn_pinned(store, op_pin_state_floats);
3899 if (get_Proj_proj(proj) == pn_Store_X_except) {
3900 ir_graph *irg = get_irn_irg(proj);
3901 DBG_OPT_EXC_REM(proj);
3902 return new_r_Bad(irg, mode_X);
3904 ir_node *blk = get_nodes_block(store);
3905 return new_r_Jmp(blk);
3913 * Transform a Proj(Div) with a non-zero value.
3914 * Removes the exceptions and routes the memory to the NoMem node.
3916 static ir_node *transform_node_Proj_Div(ir_node *proj)
3918 ir_node *div = get_Proj_pred(proj);
3919 ir_node *b = get_Div_right(div);
3920 ir_node *res, *new_mem;
3921 const ir_node *confirm;
3924 if (value_not_zero(b, &confirm)) {
3925 /* div(x, y) && y != 0 */
3926 if (confirm == NULL) {
3927 /* we are sure we have a Const != 0 */
3928 new_mem = get_Div_mem(div);
3929 new_mem = skip_Pin(new_mem);
3930 set_Div_mem(div, new_mem);
3931 set_irn_pinned(div, op_pin_state_floats);
3934 proj_nr = get_Proj_proj(proj);
3936 case pn_Div_X_regular:
3937 return new_r_Jmp(get_nodes_block(div));
3939 case pn_Div_X_except: {
3940 ir_graph *irg = get_irn_irg(proj);
3941 /* we found an exception handler, remove it */
3942 DBG_OPT_EXC_REM(proj);
3943 return new_r_Bad(irg, mode_X);
3947 ir_graph *irg = get_irn_irg(proj);
3948 res = get_Div_mem(div);
3949 new_mem = get_irg_no_mem(irg);
3952 /* This node can only float up to the Confirm block */
3953 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3955 set_irn_pinned(div, op_pin_state_floats);
3956 /* this is a Div without exception, we can remove the memory edge */
3957 set_Div_mem(div, new_mem);
3966 * Transform a Proj(Mod) with a non-zero value.
3967 * Removes the exceptions and routes the memory to the NoMem node.
3969 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3971 ir_node *mod = get_Proj_pred(proj);
3972 ir_node *b = get_Mod_right(mod);
3973 ir_node *res, *new_mem;
3974 const ir_node *confirm;
3977 if (value_not_zero(b, &confirm)) {
3978 /* mod(x, y) && y != 0 */
3979 proj_nr = get_Proj_proj(proj);
3981 if (confirm == NULL) {
3982 /* we are sure we have a Const != 0 */
3983 new_mem = get_Mod_mem(mod);
3984 new_mem = skip_Pin(new_mem);
3985 set_Mod_mem(mod, new_mem);
3986 set_irn_pinned(mod, op_pin_state_floats);
3991 case pn_Mod_X_regular:
3992 return new_r_Jmp(get_nodes_block(mod));
3994 case pn_Mod_X_except: {
3995 ir_graph *irg = get_irn_irg(proj);
3996 /* we found an exception handler, remove it */
3997 DBG_OPT_EXC_REM(proj);
3998 return new_r_Bad(irg, mode_X);
4002 ir_graph *irg = get_irn_irg(proj);
4003 res = get_Mod_mem(mod);
4004 new_mem = get_irg_no_mem(irg);
4007 /* This node can only float up to the Confirm block */
4008 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4010 /* this is a Mod without exception, we can remove the memory edge */
4011 set_Mod_mem(mod, new_mem);
4015 if (get_Mod_left(mod) == b) {
4016 /* a % a = 0 if a != 0 */
4017 ir_graph *irg = get_irn_irg(proj);
4018 ir_mode *mode = get_irn_mode(proj);
4019 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4021 DBG_OPT_CSTEVAL(mod, res);
4030 * return true if the operation returns a value with exactly 1 bit set
4032 static bool is_single_bit(const ir_node *node)
4034 /* a first implementation, could be extended with vrp and others... */
4036 ir_node *shl_l = get_Shl_left(node);
4037 ir_mode *mode = get_irn_mode(node);
4038 int modulo = get_mode_modulo_shift(mode);
4039 /* this works if we shift a 1 and we have modulo shift */
4040 if (is_Const(shl_l) && is_Const_one(shl_l)
4041 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4044 } else if (is_Const(node)) {
4045 ir_tarval *tv = get_Const_tarval(node);
4046 return tarval_is_single_bit(tv);
4052 * checks if node just flips a bit in another node and returns that other node
4053 * if so. @p tv should be a value having just 1 bit set
4055 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4058 return get_Not_op(node);
4060 ir_node *right = get_Eor_right(node);
4061 if (is_Const(right)) {
4062 ir_tarval *right_tv = get_Const_tarval(right);
4063 ir_mode *mode = get_irn_mode(node);
4064 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4065 return get_Eor_left(node);
4072 * Normalizes and optimizes Cmp nodes.
4074 static ir_node *transform_node_Cmp(ir_node *n)
4076 ir_node *left = get_Cmp_left(n);
4077 ir_node *right = get_Cmp_right(n);
4078 ir_mode *mode = get_irn_mode(left);
4079 ir_tarval *tv = NULL;
4080 bool changed = false;
4081 bool changedc = false;
4082 ir_relation relation = get_Cmp_relation(n);
4083 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4085 /* mask out impossible relations */
4086 ir_relation new_relation = relation & possible;
4087 if (new_relation != relation) {
4088 relation = new_relation;
4092 /* Remove unnecessary conversions */
4093 if (!mode_is_float(mode)
4094 || be_get_backend_param()->mode_float_arithmetic == NULL) {
4095 if (is_Conv(left) && is_Conv(right)) {
4096 ir_node *op_left = get_Conv_op(left);
4097 ir_node *op_right = get_Conv_op(right);
4098 ir_mode *mode_left = get_irn_mode(op_left);
4099 ir_mode *mode_right = get_irn_mode(op_right);
4101 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4102 && mode_left != mode_b && mode_right != mode_b) {
4103 ir_node *block = get_nodes_block(n);
4105 if (mode_left == mode_right) {
4109 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4110 } else if (smaller_mode(mode_left, mode_right)) {
4111 left = new_r_Conv(block, op_left, mode_right);
4114 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4115 } else if (smaller_mode(mode_right, mode_left)) {
4117 right = new_r_Conv(block, op_right, mode_left);
4119 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4121 mode = get_irn_mode(left);
4124 if (is_Conv(left) && is_Const(right)) {
4125 ir_node *op_left = get_Conv_op(left);
4126 ir_mode *mode_left = get_irn_mode(op_left);
4127 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4128 ir_tarval *tv = get_Const_tarval(right);
4129 tarval_int_overflow_mode_t last_mode
4130 = tarval_get_integer_overflow_mode();
4132 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4133 new_tv = tarval_convert_to(tv, mode_left);
4134 tarval_set_integer_overflow_mode(last_mode);
4135 if (new_tv != tarval_bad) {
4136 ir_graph *irg = get_irn_irg(n);
4138 right = new_r_Const(irg, new_tv);
4139 mode = get_irn_mode(left);
4141 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4148 * Optimize -a CMP -b into b CMP a.
4149 * This works only for modes where unary Minus cannot Overflow.
4150 * Note that two-complement integers can Overflow so it will NOT work.
4152 if (!mode_overflow_on_unary_Minus(mode) &&
4153 is_Minus(left) && is_Minus(right)) {
4154 left = get_Minus_op(left);
4155 right = get_Minus_op(right);
4156 relation = get_inversed_relation(relation);
4158 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4161 /* remove operation on both sides if possible */
4162 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4164 * The following operations are NOT safe for floating point operations, for instance
4165 * 1.0 + inf == 2.0 + inf, =/=> x == y
4167 if (mode_is_int(mode)) {
4168 unsigned lop = get_irn_opcode(left);
4170 if (lop == get_irn_opcode(right)) {
4171 ir_node *ll, *lr, *rl, *rr;
4173 /* same operation on both sides, try to remove */
4177 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4178 left = get_unop_op(left);
4179 right = get_unop_op(right);
4181 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4184 ll = get_Add_left(left);
4185 lr = get_Add_right(left);
4186 rl = get_Add_left(right);
4187 rr = get_Add_right(right);
4190 /* X + a CMP X + b ==> a CMP b */
4194 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4195 } else if (ll == rr) {
4196 /* X + a CMP b + X ==> a CMP b */
4200 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4201 } else if (lr == rl) {
4202 /* a + X CMP X + b ==> a CMP b */
4206 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4207 } else if (lr == rr) {
4208 /* a + X CMP b + X ==> a CMP b */
4212 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4216 ll = get_Sub_left(left);
4217 lr = get_Sub_right(left);
4218 rl = get_Sub_left(right);
4219 rr = get_Sub_right(right);
4222 /* X - a CMP X - b ==> a CMP b */
4226 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4227 } else if (lr == rr) {
4228 /* a - X CMP b - X ==> a CMP b */
4232 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4236 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4237 /* a ROTL X CMP b ROTL X ==> a CMP b */
4238 left = get_Rotl_left(left);
4239 right = get_Rotl_left(right);
4241 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4249 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4250 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4251 ir_node *ll = get_binop_left(left);
4252 ir_node *lr = get_binop_right(left);
4254 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4260 ir_graph *irg = get_irn_irg(n);
4262 right = create_zero_const(irg, mode);
4264 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4267 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4268 ir_node *rl = get_binop_left(right);
4269 ir_node *rr = get_binop_right(right);
4271 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4277 ir_graph *irg = get_irn_irg(n);
4279 right = create_zero_const(irg, mode);
4281 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4285 if (is_And(left) && is_Const(right)) {
4286 ir_node *ll = get_binop_left(left);
4287 ir_node *lr = get_binop_right(left);
4288 if (is_Shr(ll) && is_Const(lr)) {
4289 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4290 ir_node *block = get_nodes_block(n);
4291 ir_mode *mode = get_irn_mode(left);
4293 ir_node *llr = get_Shr_right(ll);
4294 if (is_Const(llr)) {
4295 dbg_info *dbg = get_irn_dbg_info(left);
4296 ir_graph *irg = get_irn_irg(left);
4298 ir_tarval *c1 = get_Const_tarval(llr);
4299 ir_tarval *c2 = get_Const_tarval(lr);
4300 ir_tarval *c3 = get_Const_tarval(right);
4301 ir_tarval *mask = tarval_shl(c2, c1);
4302 ir_tarval *value = tarval_shl(c3, c1);
4304 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4305 right = new_r_Const(irg, value);
4310 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4312 if (is_Const(right) && is_Const_null(right) &&
4313 (is_Eor(left) || is_Or_Eor_Add(left))) {
4314 right = get_Eor_right(left);
4315 left = get_Eor_left(left);
4321 if (mode_is_int(mode) && is_And(left)) {
4322 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4323 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4324 if (relation == ir_relation_equal
4325 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4326 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4327 ir_node *and0 = get_And_left(left);
4328 ir_node *and1 = get_And_right(left);
4329 if (and1 == right) {
4330 ir_node *tmp = and0;
4334 if (and0 == right && is_single_bit(and0)) {
4335 ir_graph *irg = get_irn_irg(n);
4337 relation == ir_relation_equal ? ir_relation_less_greater
4338 : ir_relation_equal;
4339 right = create_zero_const(irg, mode);
4345 if (is_Const(right) && is_Const_null(right) &&
4346 (relation == ir_relation_equal
4347 || (relation == ir_relation_less_greater)
4348 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4350 /* instead of flipping the bit before the bit-test operation negate
4352 ir_node *and0 = get_And_left(left);
4353 ir_node *and1 = get_And_right(left);
4354 if (is_Const(and1)) {
4355 ir_tarval *tv = get_Const_tarval(and1);
4356 if (tarval_is_single_bit(tv)) {
4357 ir_node *flipped = flips_bit(and0, tv);
4358 if (flipped != NULL) {
4359 dbg_info *dbgi = get_irn_dbg_info(left);
4360 ir_node *block = get_nodes_block(left);
4361 relation = get_negated_relation(relation);
4362 left = new_rd_And(dbgi, block, flipped, and1, mode);
4371 /* replace mode_b compares with ands/ors */
4372 if (mode == mode_b) {
4373 ir_node *block = get_nodes_block(n);
4377 case ir_relation_less_equal:
4378 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4380 case ir_relation_less:
4381 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4383 case ir_relation_greater_equal:
4384 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4386 case ir_relation_greater:
4387 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4389 case ir_relation_less_greater:
4390 bres = new_r_Eor(block, left, right, mode_b);
4392 case ir_relation_equal:
4393 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4396 #ifdef DEBUG_libfirm
4397 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4402 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4408 * First step: normalize the compare op
4409 * by placing the constant on the right side
4410 * or moving the lower address node to the left.
4412 if (!operands_are_normalized(left, right)) {
4417 relation = get_inversed_relation(relation);
4422 * Second step: Try to reduce the magnitude
4423 * of a constant. This may help to generate better code
4424 * later and may help to normalize more compares.
4425 * Of course this is only possible for integer values.
4427 tv = value_of(right);
4428 if (tv != tarval_bad) {
4429 ir_mode *mode = get_irn_mode(right);
4431 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4432 * cmp(ct,c2) | cmp(cf,c2) | result
4433 * -----------|------------|--------
4434 * true | true | True
4435 * false | false | False
4437 * false | true | not(x)
4440 ir_node *mux_true = get_Mux_true(left);
4441 ir_node *mux_false = get_Mux_false(left);
4442 if (is_Const(mux_true) && is_Const(mux_false)) {
4443 /* we can fold true/false constant separately */
4444 ir_tarval *tv_true = get_Const_tarval(mux_true);
4445 ir_tarval *tv_false = get_Const_tarval(mux_false);
4446 ir_relation r_true = tarval_cmp(tv_true, tv);
4447 ir_relation r_false = tarval_cmp(tv_false, tv);
4448 if (r_true != ir_relation_false
4449 || r_false != ir_relation_false) {
4450 bool rel_true = (r_true & relation) != 0;
4451 bool rel_false = (r_false & relation) != 0;
4452 ir_node *cond = get_Mux_sel(left);
4453 if (rel_true == rel_false) {
4454 relation = rel_true ? ir_relation_true
4455 : ir_relation_false;
4456 } else if (rel_true) {
4459 dbg_info *dbgi = get_irn_dbg_info(n);
4460 ir_node *block = get_nodes_block(n);
4461 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4468 /* TODO extend to arbitrary constants */
4469 if (is_Conv(left) && tarval_is_null(tv)) {
4470 ir_node *op = get_Conv_op(left);
4471 ir_mode *op_mode = get_irn_mode(op);
4474 * UpConv(x) REL 0 ==> x REL 0
4475 * Don't do this for float values as it's unclear whether it is a
4476 * win. (on the other side it makes detection/creation of fabs hard)
4478 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4479 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4480 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4481 !mode_is_float(mode)) {
4482 tv = get_mode_null(op_mode);
4486 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4490 if (tv != tarval_bad) {
4491 /* the following optimization is possible on modes without Overflow
4492 * on Unary Minus or on == and !=:
4493 * -a CMP c ==> a swap(CMP) -c
4495 * Beware: for two-complement Overflow may occur, so only == and != can
4496 * be optimized, see this:
4497 * -MININT < 0 =/=> MININT > 0 !!!
4499 if (is_Minus(left) &&
4500 (!mode_overflow_on_unary_Minus(mode) ||
4501 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4502 tv = tarval_neg(tv);
4504 if (tv != tarval_bad) {
4505 left = get_Minus_op(left);
4506 relation = get_inversed_relation(relation);
4508 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4510 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4511 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4512 tv = tarval_not(tv);
4514 if (tv != tarval_bad) {
4515 left = get_Not_op(left);
4517 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4521 /* for integer modes, we have more */
4522 if (mode_is_int(mode) && !is_Const(left)) {
4523 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4524 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4525 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4526 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4528 if (tv != tarval_bad) {
4529 relation ^= ir_relation_equal;
4531 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4534 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4535 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4536 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4537 tv = tarval_add(tv, get_mode_one(mode));
4539 if (tv != tarval_bad) {
4540 relation ^= ir_relation_equal;
4542 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4546 /* the following reassociations work only for == and != */
4547 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4548 if (tv != tarval_bad) {
4549 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4551 ir_node *c1 = get_Sub_right(left);
4552 ir_tarval *tv2 = value_of(c1);
4554 if (tv2 != tarval_bad) {
4555 tv2 = tarval_add(tv, value_of(c1));
4557 if (tv2 != tarval_bad) {
4558 left = get_Sub_left(left);
4561 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4565 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4566 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4567 ir_node *a_l = get_binop_left(left);
4568 ir_node *a_r = get_binop_right(left);
4572 if (is_Const(a_l)) {
4574 tv2 = value_of(a_l);
4577 tv2 = value_of(a_r);
4580 if (tv2 != tarval_bad) {
4581 tv2 = tarval_sub(tv, tv2, NULL);
4583 if (tv2 != tarval_bad) {
4587 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4591 /* -a == c ==> a == -c, -a != c ==> a != -c */
4592 else if (is_Minus(left)) {
4593 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4595 if (tv2 != tarval_bad) {
4596 left = get_Minus_op(left);
4599 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4606 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4607 switch (get_irn_opcode(left)) {
4611 c1 = get_And_right(left);
4614 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4615 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4617 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4619 /* TODO: move to constant evaluation */
4620 ir_graph *irg = get_irn_irg(n);
4621 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4622 c1 = new_r_Const(irg, tv);
4623 DBG_OPT_CSTEVAL(n, c1);
4627 if (tarval_is_single_bit(tv)) {
4629 * optimization for AND:
4631 * And(x, C) == C ==> And(x, C) != 0
4632 * And(x, C) != C ==> And(X, C) == 0
4634 * if C is a single Bit constant.
4637 /* check for Constant's match. We have check hare the tarvals,
4638 because our const might be changed */
4639 if (get_Const_tarval(c1) == tv) {
4640 /* fine: do the transformation */
4641 tv = get_mode_null(get_tarval_mode(tv));
4642 relation ^= ir_relation_less_equal_greater;
4644 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4650 c1 = get_Or_right(left);
4651 if (is_Const(c1) && tarval_is_null(tv)) {
4653 * Or(x, C) == 0 && C != 0 ==> FALSE
4654 * Or(x, C) != 0 && C != 0 ==> TRUE
4656 if (! tarval_is_null(get_Const_tarval(c1))) {
4657 /* TODO: move to constant evaluation */
4658 ir_graph *irg = get_irn_irg(n);
4659 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4660 c1 = new_r_Const(irg, tv);
4661 DBG_OPT_CSTEVAL(n, c1);
4668 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4670 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4673 c1 = get_Shl_right(left);
4675 ir_graph *irg = get_irn_irg(c1);
4676 ir_tarval *tv1 = get_Const_tarval(c1);
4677 ir_mode *mode = get_irn_mode(left);
4678 ir_tarval *minus1 = get_mode_all_one(mode);
4679 ir_tarval *amask = tarval_shr(minus1, tv1);
4680 ir_tarval *cmask = tarval_shl(minus1, tv1);
4683 if (tarval_and(tv, cmask) != tv) {
4684 /* condition not met */
4685 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4686 c1 = new_r_Const(irg, tv);
4687 DBG_OPT_CSTEVAL(n, c1);
4690 sl = get_Shl_left(left);
4691 blk = get_nodes_block(n);
4692 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4693 tv = tarval_shr(tv, tv1);
4695 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4700 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4702 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4705 c1 = get_Shr_right(left);
4707 ir_graph *irg = get_irn_irg(c1);
4708 ir_tarval *tv1 = get_Const_tarval(c1);
4709 ir_mode *mode = get_irn_mode(left);
4710 ir_tarval *minus1 = get_mode_all_one(mode);
4711 ir_tarval *amask = tarval_shl(minus1, tv1);
4712 ir_tarval *cmask = tarval_shr(minus1, tv1);
4715 if (tarval_and(tv, cmask) != tv) {
4716 /* condition not met */
4717 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4718 c1 = new_r_Const(irg, tv);
4719 DBG_OPT_CSTEVAL(n, c1);
4722 sl = get_Shr_left(left);
4723 blk = get_nodes_block(n);
4724 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4725 tv = tarval_shl(tv, tv1);
4727 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4732 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4734 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4737 c1 = get_Shrs_right(left);
4739 ir_graph *irg = get_irn_irg(c1);
4740 ir_tarval *tv1 = get_Const_tarval(c1);
4741 ir_mode *mode = get_irn_mode(left);
4742 ir_tarval *minus1 = get_mode_all_one(mode);
4743 ir_tarval *amask = tarval_shl(minus1, tv1);
4744 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4747 cond = tarval_sub(cond, tv1, NULL);
4748 cond = tarval_shrs(tv, cond);
4750 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4751 /* condition not met */
4752 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4753 c1 = new_r_Const(irg, tv);
4754 DBG_OPT_CSTEVAL(n, c1);
4757 sl = get_Shrs_left(left);
4758 blk = get_nodes_block(n);
4759 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4760 tv = tarval_shl(tv, tv1);
4762 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4770 if (changedc) { /* need a new Const */
4771 ir_graph *irg = get_irn_irg(n);
4772 right = new_r_Const(irg, tv);
4776 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4777 ir_node *op = get_Proj_pred(left);
4779 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4780 ir_node *c = get_binop_right(op);
4783 ir_tarval *tv = get_Const_tarval(c);
4785 if (tarval_is_single_bit(tv)) {
4786 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4787 ir_node *v = get_binop_left(op);
4788 ir_node *blk = get_nodes_block(op);
4789 ir_graph *irg = get_irn_irg(op);
4790 ir_mode *mode = get_irn_mode(v);
4792 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4793 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4795 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4802 dbg_info *dbgi = get_irn_dbg_info(n);
4803 ir_node *block = get_nodes_block(n);
4805 /* create a new compare */
4806 n = new_rd_Cmp(dbgi, block, left, right, relation);
4813 * Optimize CopyB(mem, x, x) into a Nop.
4815 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4817 ir_node *copyb = get_Proj_pred(proj);
4818 ir_node *a = get_CopyB_dst(copyb);
4819 ir_node *b = get_CopyB_src(copyb);
4822 switch (get_Proj_proj(proj)) {
4823 case pn_CopyB_X_regular:
4824 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4825 DBG_OPT_EXC_REM(proj);
4826 proj = new_r_Jmp(get_nodes_block(copyb));
4828 case pn_CopyB_X_except: {
4829 ir_graph *irg = get_irn_irg(proj);
4830 DBG_OPT_EXC_REM(proj);
4831 proj = new_r_Bad(irg, mode_X);
4842 * Optimize Bounds(idx, idx, upper) into idx.
4844 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4846 ir_node *oldn = proj;
4847 ir_node *bound = get_Proj_pred(proj);
4848 ir_node *idx = get_Bound_index(bound);
4849 ir_node *pred = skip_Proj(idx);
4852 if (idx == get_Bound_lower(bound))
4854 else if (is_Bound(pred)) {
4856 * idx was Bounds checked previously, it is still valid if
4857 * lower <= pred_lower && pred_upper <= upper.
4859 ir_node *lower = get_Bound_lower(bound);
4860 ir_node *upper = get_Bound_upper(bound);
4861 if (get_Bound_lower(pred) == lower &&
4862 get_Bound_upper(pred) == upper) {
4864 * One could expect that we simply return the previous
4865 * Bound here. However, this would be wrong, as we could
4866 * add an exception Proj to a new location then.
4867 * So, we must turn in into a tuple.
4873 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4874 switch (get_Proj_proj(proj)) {
4876 DBG_OPT_EXC_REM(proj);
4877 proj = get_Bound_mem(bound);
4879 case pn_Bound_X_except:
4880 DBG_OPT_EXC_REM(proj);
4881 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4885 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4887 case pn_Bound_X_regular:
4888 DBG_OPT_EXC_REM(proj);
4889 proj = new_r_Jmp(get_nodes_block(bound));
4899 * Does all optimizations on nodes that must be done on its Projs
4900 * because of creating new nodes.
4902 static ir_node *transform_node_Proj(ir_node *proj)
4904 ir_node *n = get_Proj_pred(proj);
4906 if (n->op->ops.transform_node_Proj)
4907 return n->op->ops.transform_node_Proj(proj);
4912 * Test whether a block is unreachable
4913 * Note: That this only returns true when
4914 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4915 * This is important, as you easily end up producing invalid constructs in the
4916 * unreachable code when optimizing away edges into the unreachable code.
4917 * So only set this flag when you iterate localopts to the fixpoint.
4918 * When you reach the fixpoint then all unreachable code is dead
4919 * (= can't be reached by firm edges) and you won't see the invalid constructs
4922 static bool is_block_unreachable(const ir_node *block)
4924 const ir_graph *irg = get_irn_irg(block);
4925 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4927 return get_Block_dom_depth(block) < 0;
4930 static ir_node *transform_node_Block(ir_node *block)
4932 ir_graph *irg = get_irn_irg(block);
4933 int arity = get_irn_arity(block);
4934 ir_node *bad = NULL;
4937 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4940 for (i = 0; i < arity; ++i) {
4941 ir_node *const pred = get_Block_cfgpred(block, i);
4942 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4945 bad = new_r_Bad(irg, mode_X);
4946 set_irn_n(block, i, bad);
4952 static ir_node *transform_node_Phi(ir_node *phi)
4954 int n = get_irn_arity(phi);
4955 ir_mode *mode = get_irn_mode(phi);
4956 ir_node *block = get_nodes_block(phi);
4957 ir_graph *irg = get_irn_irg(phi);
4958 ir_node *bad = NULL;
4961 /* Set phi-operands for bad-block inputs to bad */
4962 for (i = 0; i < n; ++i) {
4963 if (!is_Bad(get_Phi_pred(phi, i))) {
4964 ir_node *pred = get_Block_cfgpred(block, i);
4965 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4967 bad = new_r_Bad(irg, mode);
4968 set_irn_n(phi, i, bad);
4973 /* Move Pin nodes down through Phi nodes. */
4974 if (mode == mode_M) {
4975 n = get_irn_arity(phi);
4977 /* Beware of Phi0 */
4981 bool has_pin = false;
4983 NEW_ARR_A(ir_node *, in, n);
4985 for (i = 0; i < n; ++i) {
4986 ir_node *pred = get_irn_n(phi, i);
4989 in[i] = get_Pin_op(pred);
4991 } else if (is_Bad(pred)) {
5001 /* Move the Pin nodes "behind" the Phi. */
5002 new_phi = new_r_Phi(block, n, in, mode_M);
5003 return new_r_Pin(block, new_phi);
5006 /* Move Confirms down through Phi nodes. */
5007 else if (mode_is_reference(mode)) {
5008 n = get_irn_arity(phi);
5010 /* Beware of Phi0 */
5012 ir_node *pred = get_irn_n(phi, 0);
5013 ir_node *bound, *new_phi, **in;
5014 ir_relation relation;
5015 bool has_confirm = false;
5017 if (! is_Confirm(pred))
5020 bound = get_Confirm_bound(pred);
5021 relation = get_Confirm_relation(pred);
5023 NEW_ARR_A(ir_node *, in, n);
5024 in[0] = get_Confirm_value(pred);
5026 for (i = 1; i < n; ++i) {
5027 pred = get_irn_n(phi, i);
5029 if (is_Confirm(pred) &&
5030 get_Confirm_bound(pred) == bound &&
5031 get_Confirm_relation(pred) == relation) {
5032 in[i] = get_Confirm_value(pred);
5034 } else if (is_Bad(pred)) {
5044 /* move the Confirm nodes "behind" the Phi */
5045 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5046 return new_r_Confirm(block, new_phi, bound, relation);
5053 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5055 * Should be moved to reassociation?
5057 static ir_node *transform_node_shift(ir_node *n)
5059 ir_node *left, *right;
5061 ir_mode *count_mode;
5062 ir_tarval *tv1, *tv2, *res;
5063 ir_node *in[2], *irn, *block;
5067 left = get_binop_left(n);
5069 /* different operations */
5070 if (get_irn_op(left) != get_irn_op(n))
5073 right = get_binop_right(n);
5074 tv1 = value_of(right);
5075 if (tv1 == tarval_bad)
5078 tv2 = value_of(get_binop_right(left));
5079 if (tv2 == tarval_bad)
5082 count_mode = get_tarval_mode(tv1);
5083 if (get_tarval_mode(tv2) != count_mode) {
5084 /* TODO: search bigger mode or something and convert... */
5088 mode = get_irn_mode(n);
5089 modulo_shf = get_mode_modulo_shift(mode);
5091 if (modulo_shf > 0) {
5092 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5094 /* I'm not so sure what happens in one complement... */
5095 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5096 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5097 * above will be invalid) */
5098 assert(modulo_shf<=0 || is_po2(modulo_shf));
5100 tv1 = tarval_and(tv1, modulo_mask);
5101 tv2 = tarval_and(tv2, modulo_mask);
5103 res = tarval_add(tv1, tv2);
5104 irg = get_irn_irg(n);
5106 /* beware: a simple replacement works only, if res < modulo shift */
5108 int bits = get_mode_size_bits(mode);
5109 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5110 res = tarval_mod(res, modulo);
5112 long bits = get_mode_size_bits(mode);
5113 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5115 /* shifting too much */
5116 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5118 ir_node *block = get_nodes_block(n);
5119 dbg_info *dbgi = get_irn_dbg_info(n);
5120 ir_mode *smode = get_irn_mode(right);
5121 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5122 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5125 return new_r_Const(irg, get_mode_null(mode));
5129 /* ok, we can replace it */
5130 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5131 block = get_nodes_block(n);
5133 in[0] = get_binop_left(left);
5134 in[1] = new_r_Const(irg, res);
5136 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5138 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5145 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5147 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5148 * (also with x >>s c1 when c1>=c2)
5150 static ir_node *transform_node_shl_shr(ir_node *n)
5153 ir_node *right = get_binop_right(n);
5163 ir_tarval *tv_shift;
5166 ir_relation relation;
5169 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5171 if (!is_Const(right))
5174 left = get_binop_left(n);
5175 mode = get_irn_mode(n);
5176 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5177 ir_node *shr_right = get_binop_right(left);
5179 if (!is_Const(shr_right))
5182 x = get_binop_left(left);
5183 tv_shr = get_Const_tarval(shr_right);
5184 tv_shl = get_Const_tarval(right);
5186 if (is_Shrs(left)) {
5187 /* shrs variant only allowed if c1 >= c2 */
5188 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5191 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5194 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5196 tv_mask = tarval_shl(tv_mask, tv_shl);
5197 } else if (is_Shr(n) && is_Shl(left)) {
5198 ir_node *shl_right = get_Shl_right(left);
5200 if (!is_Const(shl_right))
5203 x = get_Shl_left(left);
5204 tv_shr = get_Const_tarval(right);
5205 tv_shl = get_Const_tarval(shl_right);
5207 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5208 tv_mask = tarval_shr(tv_mask, tv_shr);
5213 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5214 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5217 assert(tv_mask != tarval_bad);
5218 assert(get_tarval_mode(tv_mask) == mode);
5220 block = get_nodes_block(n);
5221 irg = get_irn_irg(block);
5222 dbgi = get_irn_dbg_info(n);
5224 relation = tarval_cmp(tv_shl, tv_shr);
5225 if (relation == ir_relation_less || relation == ir_relation_equal) {
5226 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5227 new_const = new_r_Const(irg, tv_shift);
5229 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5231 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5234 assert(relation == ir_relation_greater);
5235 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5236 new_const = new_r_Const(irg, tv_shift);
5237 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5240 new_const = new_r_Const(irg, tv_mask);
5241 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5246 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5248 ir_mode *mode = get_tarval_mode(tv);
5249 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5250 return tarval_mod(tv, modulo_tv);
5253 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5254 ir_node *left, ir_node *right, ir_mode *mode);
5257 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5258 * then we can use that to minimize the value of Add(x, const) or
5259 * Sub(Const, x). In particular this often avoids 1 instruction in some
5260 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5261 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5263 static ir_node *transform_node_shift_modulo(ir_node *n,
5264 new_shift_func new_shift)
5266 ir_mode *mode = get_irn_mode(n);
5267 int modulo = get_mode_modulo_shift(mode);
5268 ir_node *newop = NULL;
5269 ir_mode *mode_right;
5276 if (get_mode_arithmetic(mode) != irma_twos_complement)
5278 if (!is_po2(modulo))
5281 irg = get_irn_irg(n);
5282 block = get_nodes_block(n);
5283 right = get_binop_right(n);
5284 mode_right = get_irn_mode(right);
5285 if (is_Const(right)) {
5286 ir_tarval *tv = get_Const_tarval(right);
5287 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5292 newop = new_r_Const(irg, tv_mod);
5293 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5294 ir_node *add_right = get_binop_right(right);
5295 if (is_Const(add_right)) {
5296 ir_tarval *tv = get_Const_tarval(add_right);
5297 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5302 newconst = new_r_Const(irg, tv_mod);
5303 newop = new_r_Add(block, get_binop_left(right), newconst,
5306 } else if (is_Sub(right)) {
5307 ir_node *sub_left = get_Sub_left(right);
5308 if (is_Const(sub_left)) {
5309 ir_tarval *tv = get_Const_tarval(sub_left);
5310 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5315 newconst = new_r_Const(irg, tv_mod);
5316 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5323 if (newop != NULL) {
5324 dbg_info *dbgi = get_irn_dbg_info(n);
5325 ir_node *left = get_binop_left(n);
5326 return new_shift(dbgi, block, left, newop, mode);
5334 static ir_node *transform_node_Shr(ir_node *n)
5336 ir_node *c, *oldn = n;
5337 ir_node *left = get_Shr_left(n);
5338 ir_node *right = get_Shr_right(n);
5339 ir_mode *mode = get_irn_mode(n);
5341 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5342 n = transform_node_shift(n);
5345 n = transform_node_shift_modulo(n, new_rd_Shr);
5347 n = transform_node_shl_shr(n);
5349 n = transform_node_shift_bitop(n);
5357 static ir_node *transform_node_Shrs(ir_node *n)
5360 ir_node *a = get_Shrs_left(n);
5361 ir_node *b = get_Shrs_right(n);
5362 ir_mode *mode = get_irn_mode(n);
5366 if (is_oversize_shift(n)) {
5367 ir_node *block = get_nodes_block(n);
5368 dbg_info *dbgi = get_irn_dbg_info(n);
5369 ir_mode *cmode = get_irn_mode(b);
5370 long val = get_mode_size_bits(cmode)-1;
5371 ir_graph *irg = get_irn_irg(n);
5372 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5373 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5376 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5377 n = transform_node_shift(n);
5381 n = transform_node_shift_modulo(n, new_rd_Shrs);
5384 n = transform_node_shift_bitop(n);
5388 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5389 attr = vrp_get_info(a);
5391 unsigned bits = get_mode_size_bits(mode);
5392 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5393 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5394 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5395 dbg_info *dbgi = get_irn_dbg_info(n);
5396 ir_node *block = get_nodes_block(n);
5397 return new_rd_Shr(dbgi, block, a, b, mode);
5407 static ir_node *transform_node_Shl(ir_node *n)
5409 ir_node *c, *oldn = n;
5410 ir_node *a = get_Shl_left(n);
5411 ir_node *b = get_Shl_right(n);
5412 ir_mode *mode = get_irn_mode(n);
5414 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5415 n = transform_node_shift(n);
5418 n = transform_node_shift_modulo(n, new_rd_Shl);
5420 n = transform_node_shl_shr(n);
5422 n = transform_node_shift_bitop(n);
5430 static ir_node *transform_node_Rotl(ir_node *n)
5432 ir_node *c, *oldn = n;
5433 ir_node *a = get_Rotl_left(n);
5434 ir_node *b = get_Rotl_right(n);
5435 ir_mode *mode = get_irn_mode(n);
5437 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5438 n = transform_node_shift(n);
5441 n = transform_node_shift_bitop(n);
5447 * returns mode size for may_leave_out_middle_mode
5449 static unsigned get_significand_size(ir_mode *mode)
5451 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5452 switch (arithmetic) {
5454 case irma_x86_extended_float:
5455 return get_mode_mantissa_size(mode) + 1;
5456 case irma_twos_complement:
5457 return get_mode_size_bits(mode);
5459 panic("Conv node with irma_none mode?");
5461 panic("unexpected mode_arithmetic in get_significand_size");
5465 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5466 * as converting from @p m0 to @p m1 and then to @p m2.
5467 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5468 * gives the following truth table:
5469 * s -> b -> m : true
5470 * s -> m -> b : !signed(s) || signed(m)
5471 * m -> b -> s : true
5472 * m -> s -> b : false
5473 * b -> s -> m : false
5474 * b -> m -> s : true
5476 * s -> b -> b : true
5477 * s -> s -> b : false
5479 * additional float constraints:
5481 * F -> I -> I: signedness of Is must match
5482 * I -> F -> I: signedness of Is must match
5483 * I -> I -> F: signedness of Is must match
5487 * at least 1 float involved: signedness must match
5489 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5491 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5492 if (n_floats == 1) {
5494 int n_signed = mode_is_signed(m0) + mode_is_signed(m1)
5495 + mode_is_signed(m2);
5496 /* we assume that float modes are always signed */
5497 if ((n_signed & 1) != 1)
5500 /* because overflow gives strange results we don't touch this case */
5503 } else if (n_floats == 2 && !mode_is_float(m1)) {
5507 unsigned size0 = get_significand_size(m0);
5508 unsigned size1 = get_significand_size(m1);
5509 unsigned size2 = get_significand_size(m2);
5510 if (size1 < size2 && size0 >= size1)
5514 return !mode_is_signed(m0) || mode_is_signed(m1);
5520 static ir_node *transform_node_Conv(ir_node *n)
5522 ir_node *c, *oldn = n;
5523 ir_mode *mode = get_irn_mode(n);
5524 ir_node *a = get_Conv_op(n);
5527 ir_mode *a_mode = get_irn_mode(a);
5528 ir_node *b = get_Conv_op(a);
5529 ir_mode *b_mode = get_irn_mode(b);
5530 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5531 dbg_info *dbgi = get_irn_dbg_info(n);
5532 ir_node *block = get_nodes_block(n);
5533 return new_rd_Conv(dbgi, block, b, mode);
5537 if (mode != mode_b && is_const_Phi(a)) {
5538 /* Do NOT optimize mode_b Conv's, this leads to remaining
5539 * Phib nodes later, because the conv_b_lower operation
5540 * is instantly reverted, when it tries to insert a Convb.
5542 c = apply_conv_on_phi(a, mode);
5544 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5549 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5550 ir_graph *irg = get_irn_irg(n);
5551 return new_r_Unknown(irg, mode);
5554 if (mode_is_reference(mode) &&
5555 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5557 ir_node *l = get_Add_left(a);
5558 ir_node *r = get_Add_right(a);
5559 dbg_info *dbgi = get_irn_dbg_info(a);
5560 ir_node *block = get_nodes_block(n);
5562 ir_node *lop = get_Conv_op(l);
5563 if (get_irn_mode(lop) == mode) {
5564 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5565 n = new_rd_Add(dbgi, block, lop, r, mode);
5570 ir_node *rop = get_Conv_op(r);
5571 if (get_irn_mode(rop) == mode) {
5572 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5573 n = new_rd_Add(dbgi, block, l, rop, mode);
5583 * Remove dead blocks and nodes in dead blocks
5584 * in keep alive list. We do not generate a new End node.
5586 static ir_node *transform_node_End(ir_node *n)
5588 int i, j, n_keepalives = get_End_n_keepalives(n);
5591 NEW_ARR_A(ir_node *, in, n_keepalives);
5593 for (i = j = 0; i < n_keepalives; ++i) {
5594 ir_node *ka = get_End_keepalive(n, i);
5596 /* no need to keep Bad */
5599 /* do not keep unreachable code */
5600 block = is_Block(ka) ? ka : get_nodes_block(ka);
5601 if (is_block_unreachable(block))
5605 if (j != n_keepalives)
5606 set_End_keepalives(n, j, in);
5610 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5612 if (is_Minus(a) && get_Minus_op(a) == b)
5614 if (is_Minus(b) && get_Minus_op(b) == a)
5616 if (is_Sub(a) && is_Sub(b)) {
5617 ir_node *a_left = get_Sub_left(a);
5618 ir_node *a_right = get_Sub_right(a);
5619 ir_node *b_left = get_Sub_left(b);
5620 ir_node *b_right = get_Sub_right(b);
5622 if (a_left == b_right && a_right == b_left)
5629 static const ir_node *skip_upconv(const ir_node *node)
5631 while (is_Conv(node)) {
5632 ir_mode *mode = get_irn_mode(node);
5633 const ir_node *op = get_Conv_op(node);
5634 ir_mode *op_mode = get_irn_mode(op);
5635 if (!smaller_mode(op_mode, mode))
5642 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5643 const ir_node *mux_true)
5648 ir_relation relation;
5654 * Note further that these optimization work even for floating point
5655 * with NaN's because -NaN == NaN.
5656 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5659 mode = get_irn_mode(mux_true);
5660 if (mode_honor_signed_zeros(mode))
5663 /* must be <, <=, >=, > */
5664 relation = get_Cmp_relation(sel);
5665 if ((relation & ir_relation_less_greater) == 0)
5668 if (!ir_is_negated_value(mux_true, mux_false))
5671 mux_true = skip_upconv(mux_true);
5672 mux_false = skip_upconv(mux_false);
5674 /* must be x cmp 0 */
5675 cmp_right = get_Cmp_right(sel);
5676 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5679 cmp_left = get_Cmp_left(sel);
5680 if (cmp_left == mux_false) {
5681 if (relation & ir_relation_less) {
5684 assert(relation & ir_relation_greater);
5687 } else if (cmp_left == mux_true) {
5688 if (relation & ir_relation_less) {
5691 assert(relation & ir_relation_greater);
5699 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5702 ir_node *cmp_left = get_Cmp_left(sel);
5703 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5706 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5707 const ir_node *mux_true)
5709 /* this code should return true each time transform_node_Mux would
5710 * optimize the Mux completely away */
5712 ir_mode *mode = get_irn_mode(mux_false);
5713 if (get_mode_arithmetic(mode) == irma_twos_complement
5714 && ir_mux_is_abs(sel, mux_false, mux_true))
5717 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5718 const ir_node *cmp_r = get_Cmp_right(sel);
5719 const ir_node *cmp_l = get_Cmp_left(sel);
5720 const ir_node *f = mux_false;
5721 const ir_node *t = mux_true;
5723 if (is_Const(t) && is_Const_null(t)) {
5728 if (is_And(cmp_l) && f == cmp_r) {
5729 ir_node *and_r = get_And_right(cmp_l);
5732 if (and_r == t && is_single_bit(and_r))
5734 and_l = get_And_left(cmp_l);
5735 if (and_l == t && is_single_bit(and_l))
5744 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5746 static ir_node *transform_Mux_set(ir_node *n)
5748 ir_node *cond = get_Mux_sel(n);
5753 ir_relation relation;
5767 left = get_Cmp_left(cond);
5768 mode = get_irn_mode(left);
5769 if (!mode_is_int(mode) && !mode_is_reference(mode))
5771 dest_mode = get_irn_mode(n);
5772 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5774 right = get_Cmp_right(cond);
5775 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5776 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5777 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5778 && relation != ir_relation_greater))
5783 case ir_relation_less:
5784 /* a < b -> (a - b) >> 31 */
5788 case ir_relation_less_equal:
5789 /* a <= b -> ~(a - b) >> 31 */
5794 case ir_relation_greater:
5795 /* a > b -> (b - a) >> 31 */
5799 case ir_relation_greater_equal:
5800 /* a >= b -> ~(a - b) >> 31 */
5809 dbgi = get_irn_dbg_info(n);
5810 block = get_nodes_block(n);
5811 irg = get_irn_irg(block);
5812 bits = get_mode_size_bits(dest_mode);
5813 tv = new_tarval_from_long(bits-1, mode_Iu);
5814 shift_cnt = new_rd_Const(dbgi, irg, tv);
5816 if (mode != dest_mode) {
5817 a = new_rd_Conv(dbgi, block, a, dest_mode);
5818 b = new_rd_Conv(dbgi, block, b, dest_mode);
5821 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5823 res = new_rd_Not(dbgi, block, res, dest_mode);
5825 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5830 * Optimize a Mux into some simpler cases.
5832 static ir_node *transform_node_Mux(ir_node *n)
5835 ir_node *sel = get_Mux_sel(n);
5836 ir_mode *mode = get_irn_mode(n);
5837 ir_node *t = get_Mux_true(n);
5838 ir_node *f = get_Mux_false(n);
5839 ir_graph *irg = get_irn_irg(n);
5841 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5842 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5843 int abs = ir_mux_is_abs(sel, f, t);
5845 dbg_info *dbgi = get_irn_dbg_info(n);
5846 ir_node *block = get_nodes_block(n);
5847 ir_node *op = ir_get_abs_op(sel, f, t);
5848 int bits = get_mode_size_bits(mode);
5849 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5850 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5851 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5854 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5856 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5862 /* first normalization step: try to move a constant to the false side,
5863 * 0 preferred on false side too */
5864 if (is_Cmp(sel) && is_Const(t) &&
5865 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5866 dbg_info *seldbgi = get_irn_dbg_info(sel);
5867 ir_node *block = get_nodes_block(sel);
5868 ir_relation relation = get_Cmp_relation(sel);
5873 /* Mux(x, a, b) => Mux(not(x), b, a) */
5874 relation = get_negated_relation(relation);
5875 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5876 get_Cmp_right(sel), relation);
5877 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5880 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5881 n = transform_Mux_set(n);
5886 /* the following optimisations create new mode_b nodes, so only do them
5887 * before mode_b lowering */
5888 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5890 ir_node* block = get_nodes_block(n);
5892 ir_node* c1 = get_Mux_sel(t);
5893 ir_node* t1 = get_Mux_true(t);
5894 ir_node* f1 = get_Mux_false(t);
5896 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5897 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5898 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5899 return new_r_Mux(block, and_, f1, t1, mode);
5900 } else if (f == t1) {
5901 /* Mux(cond0, Mux(cond1, x, y), x) */
5902 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5903 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5904 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5905 return new_r_Mux(block, and_, t1, f1, mode);
5907 } else if (is_Mux(f)) {
5908 ir_node* block = get_nodes_block(n);
5910 ir_node* c1 = get_Mux_sel(f);
5911 ir_node* t1 = get_Mux_true(f);
5912 ir_node* f1 = get_Mux_false(f);
5914 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5915 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5916 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5917 return new_r_Mux(block, or_, f1, t1, mode);
5918 } else if (t == f1) {
5919 /* Mux(cond0, x, Mux(cond1, y, x)) */
5920 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5921 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5922 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5923 return new_r_Mux(block, or_, t1, f1, mode);
5927 /* note: after normalization, false can only happen on default */
5928 if (mode == mode_b) {
5929 dbg_info *dbg = get_irn_dbg_info(n);
5930 ir_node *block = get_nodes_block(n);
5933 ir_tarval *tv_t = get_Const_tarval(t);
5934 if (tv_t == tarval_b_true) {
5936 /* Muxb(sel, true, false) = sel */
5937 assert(get_Const_tarval(f) == tarval_b_false);
5938 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5941 /* Muxb(sel, true, x) = Or(sel, x) */
5942 n = new_rd_Or(dbg, block, sel, f, mode_b);
5943 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5947 } else if (is_Const(f)) {
5948 ir_tarval *tv_f = get_Const_tarval(f);
5949 if (tv_f == tarval_b_true) {
5950 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5951 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5952 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5953 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5956 /* Muxb(sel, x, false) = And(sel, x) */
5957 assert(tv_f == tarval_b_false);
5958 n = new_rd_And(dbg, block, sel, t, mode_b);
5959 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5966 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5967 ir_relation relation = get_Cmp_relation(sel);
5968 ir_node *cmp_r = get_Cmp_right(sel);
5969 ir_node *cmp_l = get_Cmp_left(sel);
5970 ir_node *block = get_nodes_block(n);
5972 if (is_And(cmp_l) && f == cmp_r) {
5973 ir_node *and_r = get_And_right(cmp_l);
5976 if (and_r == t && is_single_bit(and_r)) {
5977 if (relation == ir_relation_equal) {
5978 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5979 n = new_rd_Eor(get_irn_dbg_info(n),
5980 block, cmp_l, t, mode);
5981 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5983 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5985 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5989 and_l = get_And_left(cmp_l);
5990 if (and_l == t && is_single_bit(and_l)) {
5991 if (relation == ir_relation_equal) {
5992 /* ((1 << n) & a) == 0, (1 << n), 0) */
5993 n = new_rd_Eor(get_irn_dbg_info(n),
5994 block, cmp_l, t, mode);
5995 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5997 /* ((1 << n) & a) != 0, (1 << n), 0) */
5999 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6010 * optimize Sync nodes that have other syncs as input we simply add the inputs
6011 * of the other sync to our own inputs
6013 static ir_node *transform_node_Sync(ir_node *n)
6015 int arity = get_Sync_n_preds(n);
6018 for (i = 0; i < arity;) {
6019 ir_node *pred = get_Sync_pred(n, i);
6023 /* Remove Bad predecessors */
6030 /* Remove duplicate predecessors */
6031 for (j = 0; j < i; ++j) {
6032 if (get_Sync_pred(n, j) == pred) {
6041 if (!is_Sync(pred)) {
6049 pred_arity = get_Sync_n_preds(pred);
6050 for (j = 0; j < pred_arity; ++j) {
6051 ir_node *pred_pred = get_Sync_pred(pred, j);
6056 add_irn_n(n, pred_pred);
6060 if (get_Sync_pred(n, k) == pred_pred)
6067 ir_graph *irg = get_irn_irg(n);
6068 return new_r_Bad(irg, mode_M);
6071 return get_Sync_pred(n, 0);
6074 /* rehash the sync node */
6079 static ir_node *transform_node_Load(ir_node *n)
6081 /* don't touch volatile loads */
6082 if (get_Load_volatility(n) == volatility_is_volatile)
6085 ir_node *ptr = get_Load_ptr(n);
6086 const ir_node *confirm;
6087 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
6088 set_irn_pinned(n, op_pin_state_floats);
6091 /* if our memory predecessor is a load from the same address, then reuse the
6092 * previous result */
6093 ir_node *mem = get_Load_mem(n);
6096 ir_node *mem_pred = get_Proj_pred(mem);
6097 if (is_Load(mem_pred)) {
6098 ir_node *pred_load = mem_pred;
6100 /* conservatively compare the 2 loads. TODO: This could be less strict
6101 * with fixup code in some situations (like smaller/bigger modes) */
6102 if (get_Load_ptr(pred_load) != ptr)
6104 if (get_Load_mode(pred_load) != get_Load_mode(n))
6106 /* all combinations of aligned/unaligned pred/n should be fine so we do
6107 * not compare the unaligned attribute */
6109 ir_node *block = get_nodes_block(n);
6110 ir_node *jmp = new_r_Jmp(block);
6111 ir_graph *irg = get_irn_irg(n);
6112 ir_node *bad = new_r_Bad(irg, mode_X);
6113 ir_mode *mode = get_Load_mode(n);
6114 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6115 ir_node *in[] = { mem, res, jmp, bad };
6116 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6119 } else if (is_Store(mem_pred)) {
6120 ir_node *pred_store = mem_pred;
6121 ir_node *value = get_Store_value(pred_store);
6123 if (get_Store_ptr(pred_store) != ptr)
6125 if (get_irn_mode(value) != get_Load_mode(n))
6127 /* all combinations of aligned/unaligned pred/n should be fine so we do
6128 * not compare the unaligned attribute */
6130 ir_node *block = get_nodes_block(n);
6131 ir_node *jmp = new_r_Jmp(block);
6132 ir_graph *irg = get_irn_irg(n);
6133 ir_node *bad = new_r_Bad(irg, mode_X);
6134 ir_node *res = value;
6135 ir_node *in[] = { mem, res, jmp, bad };
6136 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6144 static ir_node *transform_node_Store(ir_node *n)
6146 /* don't touch volatile stores */
6147 if (get_Store_volatility(n) == volatility_is_volatile)
6150 ir_node *ptr = get_Store_ptr(n);
6151 const ir_node *confirm;
6152 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
6153 set_irn_pinned(n, op_pin_state_floats);
6159 * optimize a trampoline Call into a direct Call
6161 static ir_node *transform_node_Call(ir_node *call)
6163 ir_node *callee = get_Call_ptr(call);
6164 ir_node *adr, *mem, *res, *bl, **in;
6165 ir_type *ctp, *mtp, *tp;
6169 size_t i, n_res, n_param;
6172 if (! is_Proj(callee))
6174 callee = get_Proj_pred(callee);
6175 if (! is_Builtin(callee))
6177 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6180 mem = get_Call_mem(call);
6182 if (skip_Proj(mem) == callee) {
6183 /* memory is routed to the trampoline, skip */
6184 mem = get_Builtin_mem(callee);
6187 /* build a new call type */
6188 mtp = get_Call_type(call);
6189 tdb = get_type_dbg_info(mtp);
6191 n_res = get_method_n_ress(mtp);
6192 n_param = get_method_n_params(mtp);
6193 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6195 for (i = 0; i < n_res; ++i)
6196 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6198 NEW_ARR_A(ir_node *, in, n_param + 1);
6200 /* FIXME: we don't need a new pointer type in every step */
6201 irg = get_irn_irg(call);
6202 tp = get_irg_frame_type(irg);
6203 tp = new_type_pointer(tp);
6204 set_method_param_type(ctp, 0, tp);
6206 in[0] = get_Builtin_param(callee, 2);
6207 for (i = 0; i < n_param; ++i) {
6208 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6209 in[i + 1] = get_Call_param(call, i);
6211 var = get_method_variadicity(mtp);
6212 set_method_variadicity(ctp, var);
6213 /* When we resolve a trampoline, the function must be called by a this-call */
6214 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6215 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6217 adr = get_Builtin_param(callee, 1);
6219 db = get_irn_dbg_info(call);
6220 bl = get_nodes_block(call);
6222 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6223 if (get_irn_pinned(call) == op_pin_state_floats)
6224 set_irn_pinned(res, op_pin_state_floats);
6229 * Tries several [inplace] [optimizing] transformations and returns an
6230 * equivalent node. The difference to equivalent_node() is that these
6231 * transformations _do_ generate new nodes, and thus the old node must
6232 * not be freed even if the equivalent node isn't the old one.
6234 static ir_node *transform_node(ir_node *n)
6240 iro = get_irn_opcode_(n);
6241 /* constant expression evaluation / constant folding */
6242 if (get_opt_constant_folding()) {
6243 /* neither constants nor Tuple values can be evaluated */
6244 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6245 /* try to evaluate */
6246 ir_tarval *tv = computed_value(n);
6247 if (tv != tarval_bad) {
6248 /* evaluation was successful -- replace the node. */
6249 ir_graph *irg = get_irn_irg(n);
6251 n = new_r_Const(irg, tv);
6253 DBG_OPT_CSTEVAL(old_n, n);
6259 /* remove unnecessary nodes */
6260 if (get_opt_constant_folding() ||
6261 (iro == iro_Phi) || /* always optimize these nodes. */
6262 (iro == iro_Id) || /* ... */
6263 (iro == iro_Proj) || /* ... */
6264 (iro == iro_Block)) { /* Flags tested local. */
6265 n = equivalent_node(n);
6270 /* Some more constant expression evaluation. */
6271 if (get_opt_algebraic_simplification() ||
6272 (iro == iro_Cond) ||
6273 (iro == iro_Proj)) { /* Flags tested local. */
6274 if (n->op->ops.transform_node != NULL) {
6275 n = n->op->ops.transform_node(n);
6285 static void register_computed_value_func(ir_op *op, computed_value_func func)
6287 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6288 op->ops.computed_value = func;
6291 static void register_computed_value_func_proj(ir_op *op,
6292 computed_value_func func)
6294 assert(op->ops.computed_value_Proj == NULL
6295 || op->ops.computed_value_Proj == func);
6296 op->ops.computed_value_Proj = func;
6299 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6301 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6302 op->ops.equivalent_node = func;
6305 static void register_equivalent_node_func_proj(ir_op *op,
6306 equivalent_node_func func)
6308 assert(op->ops.equivalent_node_Proj == NULL
6309 || op->ops.equivalent_node_Proj == func);
6310 op->ops.equivalent_node_Proj = func;
6313 static void register_transform_node_func(ir_op *op, transform_node_func func)
6315 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6316 op->ops.transform_node = func;
6319 static void register_transform_node_func_proj(ir_op *op,
6320 transform_node_func func)
6322 assert(op->ops.transform_node_Proj == NULL
6323 || op->ops.transform_node_Proj == func);
6324 op->ops.transform_node_Proj = func;
6327 void ir_register_opt_node_ops(void)
6329 register_computed_value_func(op_Add, computed_value_Add);
6330 register_computed_value_func(op_And, computed_value_And);
6331 register_computed_value_func(op_Borrow, computed_value_Borrow);
6332 register_computed_value_func(op_Carry, computed_value_Carry);
6333 register_computed_value_func(op_Cmp, computed_value_Cmp);
6334 register_computed_value_func(op_Confirm, computed_value_Confirm);
6335 register_computed_value_func(op_Const, computed_value_Const);
6336 register_computed_value_func(op_Conv, computed_value_Conv);
6337 register_computed_value_func(op_Eor, computed_value_Eor);
6338 register_computed_value_func(op_Minus, computed_value_Minus);
6339 register_computed_value_func(op_Mul, computed_value_Mul);
6340 register_computed_value_func(op_Mux, computed_value_Mux);
6341 register_computed_value_func(op_Not, computed_value_Not);
6342 register_computed_value_func(op_Or, computed_value_Or);
6343 register_computed_value_func(op_Proj, computed_value_Proj);
6344 register_computed_value_func(op_Rotl, computed_value_Rotl);
6345 register_computed_value_func(op_Shl, computed_value_Shl);
6346 register_computed_value_func(op_Shr, computed_value_Shr);
6347 register_computed_value_func(op_Shrs, computed_value_Shrs);
6348 register_computed_value_func(op_Sub, computed_value_Sub);
6349 register_computed_value_func(op_SymConst, computed_value_SymConst);
6350 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6351 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6353 register_equivalent_node_func(op_Add, equivalent_node_Add);
6354 register_equivalent_node_func(op_And, equivalent_node_And);
6355 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6356 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6357 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6358 register_equivalent_node_func(op_Id, equivalent_node_Id);
6359 register_equivalent_node_func(op_Minus, equivalent_node_involution);
6360 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6361 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6362 register_equivalent_node_func(op_Not, equivalent_node_involution);
6363 register_equivalent_node_func(op_Or, equivalent_node_Or);
6364 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6365 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6366 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6367 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6368 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6369 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6370 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6371 register_equivalent_node_func_proj(op_Bound, equivalent_node_Proj_Bound);
6372 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6373 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6374 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6376 register_transform_node_func(op_Add, transform_node_Add);
6377 register_transform_node_func(op_And, transform_node_And);
6378 register_transform_node_func(op_Block, transform_node_Block);
6379 register_transform_node_func(op_Call, transform_node_Call);
6380 register_transform_node_func(op_Cmp, transform_node_Cmp);
6381 register_transform_node_func(op_Cond, transform_node_Cond);
6382 register_transform_node_func(op_Conv, transform_node_Conv);
6383 register_transform_node_func(op_Div, transform_node_Div);
6384 register_transform_node_func(op_End, transform_node_End);
6385 register_transform_node_func(op_Eor, transform_node_Eor);
6386 register_transform_node_func(op_Load, transform_node_Load);
6387 register_transform_node_func(op_Minus, transform_node_Minus);
6388 register_transform_node_func(op_Mod, transform_node_Mod);
6389 register_transform_node_func(op_Mul, transform_node_Mul);
6390 register_transform_node_func(op_Mux, transform_node_Mux);
6391 register_transform_node_func(op_Not, transform_node_Not);
6392 register_transform_node_func(op_Or, transform_node_Or);
6393 register_transform_node_func(op_Phi, transform_node_Phi);
6394 register_transform_node_func(op_Proj, transform_node_Proj);
6395 register_transform_node_func(op_Rotl, transform_node_Rotl);
6396 register_transform_node_func(op_Shl, transform_node_Shl);
6397 register_transform_node_func(op_Shrs, transform_node_Shrs);
6398 register_transform_node_func(op_Shr, transform_node_Shr);
6399 register_transform_node_func(op_Store, transform_node_Store);
6400 register_transform_node_func(op_Sub, transform_node_Sub);
6401 register_transform_node_func(op_Switch, transform_node_Switch);
6402 register_transform_node_func(op_Sync, transform_node_Sync);
6403 register_transform_node_func_proj(op_Bound, transform_node_Proj_Bound);
6404 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6405 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6406 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6407 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6408 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6411 /* **************** Common Subexpression Elimination **************** */
6413 /** The size of the hash table used, should estimate the number of nodes
6415 #define N_IR_NODES 512
6417 int identities_cmp(const void *elt, const void *key)
6419 ir_node *a = (ir_node *)elt;
6420 ir_node *b = (ir_node *)key;
6423 if (a == b) return 0;
6425 if ((get_irn_op(a) != get_irn_op(b)) ||
6426 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6428 /* compare if a's in and b's in are of equal length */
6429 irn_arity_a = get_irn_arity(a);
6430 if (irn_arity_a != get_irn_arity(b))
6433 /* blocks are never the same */
6437 if (get_irn_pinned(a) == op_pin_state_pinned) {
6438 /* for pinned nodes, the block inputs must be equal */
6439 if (get_nodes_block(a) != get_nodes_block(b))
6442 ir_node *block_a = get_nodes_block(a);
6443 ir_node *block_b = get_nodes_block(b);
6444 if (! get_opt_global_cse()) {
6445 /* for block-local CSE both nodes must be in the same Block */
6446 if (block_a != block_b)
6449 /* The optimistic approach would be to do nothing here.
6450 * However doing GCSE optimistically produces a lot of partially dead code which appears
6451 * to be worse in practice than the missed opportunities.
6452 * So we use a very conservative variant here and only CSE if 1 value dominates the
6454 if (!block_dominates(block_a, block_b)
6455 && !block_dominates(block_b, block_a))
6457 /* respect the workaround rule: do not move nodes which are only
6458 * held by keepalive edges */
6459 if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6464 /* compare a->in[0..ins] with b->in[0..ins] */
6465 for (i = 0; i < irn_arity_a; ++i) {
6466 ir_node *pred_a = get_irn_n(a, i);
6467 ir_node *pred_b = get_irn_n(b, i);
6468 if (pred_a != pred_b) {
6469 /* if both predecessors are CSE neutral they might be different */
6470 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6476 * here, we already now that the nodes are identical except their
6479 if (a->op->ops.node_cmp_attr)
6480 return a->op->ops.node_cmp_attr(a, b);
6485 unsigned ir_node_hash(const ir_node *node)
6487 return node->op->ops.hash(node);
6490 void new_identities(ir_graph *irg)
6492 if (irg->value_table != NULL)
6493 del_pset(irg->value_table);
6494 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6497 void del_identities(ir_graph *irg)
6499 if (irg->value_table != NULL)
6500 del_pset(irg->value_table);
6503 static int cmp_node_nr(const void *a, const void *b)
6505 ir_node **p1 = (ir_node**)a;
6506 ir_node **p2 = (ir_node**)b;
6507 long n1 = get_irn_node_nr(*p1);
6508 long n2 = get_irn_node_nr(*p2);
6509 return (n1>n2) - (n1<n2);
6512 void ir_normalize_node(ir_node *n)
6514 if (is_op_commutative(get_irn_op(n))) {
6515 ir_node *l = get_binop_left(n);
6516 ir_node *r = get_binop_right(n);
6518 /* For commutative operators perform a OP b == b OP a but keep
6519 * constants on the RIGHT side. This helps greatly in some
6520 * optimizations. Moreover we use the idx number to make the form
6522 if (!operands_are_normalized(l, r)) {
6523 set_binop_left(n, r);
6524 set_binop_right(n, l);
6527 } else if (is_Sync(n)) {
6528 /* we assume that most of the time the inputs of a Sync node are already
6529 * sorted, so check this first as a shortcut */
6530 bool ins_sorted = true;
6531 int arity = get_irn_arity(n);
6532 const ir_node *last = get_irn_n(n, 0);
6534 for (i = 1; i < arity; ++i) {
6535 const ir_node *node = get_irn_n(n, i);
6536 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6544 ir_node **ins = get_irn_in(n)+1;
6545 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6546 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6547 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6548 set_irn_in(n, arity, new_ins);
6554 ir_node *identify_remember(ir_node *n)
6556 ir_graph *irg = get_irn_irg(n);
6557 pset *value_table = irg->value_table;
6560 if (value_table == NULL)
6563 ir_normalize_node(n);
6564 /* lookup or insert in hash table with given hash key. */
6565 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6568 /* n is reachable again */
6569 edges_node_revival(nn);
6576 * During construction we set the op_pin_state_pinned flag in the graph right
6577 * when the optimization is performed. The flag turning on procedure global
6578 * cse could be changed between two allocations. This way we are safe.
6580 * @param n The node to lookup
6582 static inline ir_node *identify_cons(ir_node *n)
6586 n = identify_remember(n);
6587 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6588 ir_graph *irg = get_irn_irg(n);
6589 set_irg_pinned(irg, op_pin_state_floats);
6594 void add_identities(ir_node *node)
6601 identify_remember(node);
6604 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6606 ir_graph *rem = current_ir_graph;
6608 current_ir_graph = irg;
6609 foreach_pset(irg->value_table, ir_node, node) {
6612 current_ir_graph = rem;
6615 ir_node *optimize_node(ir_node *n)
6618 ir_graph *irg = get_irn_irg(n);
6619 unsigned iro = get_irn_opcode(n);
6622 /* Always optimize Phi nodes: part of the construction. */
6623 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6625 /* constant expression evaluation / constant folding */
6626 if (get_opt_constant_folding()) {
6627 /* neither constants nor Tuple values can be evaluated */
6628 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6629 /* try to evaluate */
6630 tv = computed_value(n);
6631 if (tv != tarval_bad) {
6636 * we MUST copy the node here temporarily, because it's still
6637 * needed for DBG_OPT_CSTEVAL
6639 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6640 oldn = (ir_node*)alloca(node_size);
6642 memcpy(oldn, n, node_size);
6643 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6645 /* ARG, copy the in array, we need it for statistics */
6646 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6648 /* note the inplace edges module */
6649 edges_node_deleted(n);
6651 /* evaluation was successful -- replace the node. */
6652 irg_kill_node(irg, n);
6653 nw = new_r_Const(irg, tv);
6655 DBG_OPT_CSTEVAL(oldn, nw);
6661 /* remove unnecessary nodes */
6662 if (get_opt_algebraic_simplification() ||
6663 (iro == iro_Phi) || /* always optimize these nodes. */
6665 (iro == iro_Proj) ||
6666 (iro == iro_Block) ) /* Flags tested local. */
6667 n = equivalent_node(n);
6669 /* Common Subexpression Elimination.
6671 * Checks whether n is already available.
6672 * The block input is used to distinguish different subexpressions. Right
6673 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6674 * subexpressions within a block.
6677 n = identify_cons(n);
6680 edges_node_deleted(oldn);
6682 /* We found an existing, better node, so we can deallocate the old node. */
6683 irg_kill_node(irg, oldn);
6687 /* Some more constant expression evaluation that does not allow to
6689 iro = get_irn_opcode(n);
6690 if (get_opt_algebraic_simplification() ||
6691 (iro == iro_Cond) ||
6692 (iro == iro_Proj)) { /* Flags tested local. */
6693 n = transform_node(n);
6696 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6697 if (get_opt_cse()) {
6699 n = identify_remember(o);
6707 ir_node *optimize_in_place_2(ir_node *n)
6709 if (!get_opt_optimize() && !is_Phi(n)) return n;
6714 /** common subexpression elimination **/
6715 /* Checks whether n is already available. */
6716 /* The block input is used to distinguish different subexpressions.
6717 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6718 * only finds common subexpressions within a block. */
6719 if (get_opt_cse()) {
6721 n = identify_remember(n);
6724 /* we have another existing node now, we do not optimize it here */
6729 n = transform_node(n);
6731 /* Now we can verify the node, as it has no dead inputs any more. */
6734 /* Now we have a legal, useful node. Enter it in hash table for cse.
6736 * Note: This is only necessary because some of the optimisations
6737 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6738 * bad practice and should be fixed sometime.
6740 if (get_opt_cse()) {
6742 n = identify_remember(o);
6750 ir_node *optimize_in_place(ir_node *n)
6752 ir_graph *irg = get_irn_irg(n);
6754 if (get_opt_global_cse())
6755 set_irg_pinned(irg, op_pin_state_floats);
6757 /* FIXME: Maybe we could also test whether optimizing the node can
6758 change the control graph. */
6759 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6760 return optimize_in_place_2(n);