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"
54 #include "firmstat_t.h"
58 static bool is_Or_Eor_Add(const ir_node *node)
60 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
61 ir_node *left = get_binop_left(node);
62 ir_node *right = get_binop_right(node);
63 vrp_attr *vrp_left = vrp_get_info(left);
64 vrp_attr *vrp_right = vrp_get_info(right);
65 if (vrp_left != NULL && vrp_right != NULL) {
67 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
68 return tarval_is_null(vrp_val);
75 * Returns the tarval of a Const node or tarval_bad for all other nodes.
77 static ir_tarval *default_value_of(const ir_node *n)
80 return get_Const_tarval(n); /* might return tarval_bad */
85 value_of_func value_of_ptr = default_value_of;
87 void set_value_of_func(value_of_func func)
92 value_of_ptr = default_value_of;
96 * Return the value of a Constant.
98 static ir_tarval *computed_value_Const(const ir_node *n)
100 return get_Const_tarval(n);
104 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
106 static ir_tarval *computed_value_SymConst(const ir_node *n)
111 switch (get_SymConst_kind(n)) {
112 case symconst_type_size:
113 type = get_SymConst_type(n);
114 if (get_type_state(type) == layout_fixed)
115 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
117 case symconst_type_align:
118 type = get_SymConst_type(n);
119 if (get_type_state(type) == layout_fixed)
120 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
122 case symconst_ofs_ent:
123 ent = get_SymConst_entity(n);
124 type = get_entity_owner(ent);
125 if (get_type_state(type) == layout_fixed)
126 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
135 * Return the value of an Add.
137 static ir_tarval *computed_value_Add(const ir_node *n)
139 ir_node *a = get_Add_left(n);
140 ir_node *b = get_Add_right(n);
142 ir_tarval *ta = value_of(a);
143 ir_tarval *tb = value_of(b);
145 if ((ta != tarval_bad) && (tb != tarval_bad))
146 return tarval_add(ta, tb);
149 if ((is_Not(a) && get_Not_op(a) == b)
150 || (is_Not(b) && get_Not_op(b) == a)) {
151 return get_mode_all_one(get_irn_mode(n));
158 * Return the value of a Sub.
159 * Special case: a - a
161 static ir_tarval *computed_value_Sub(const ir_node *n)
163 ir_mode *mode = get_irn_mode(n);
164 ir_node *a = get_Sub_left(n);
165 ir_node *b = get_Sub_right(n);
170 if (! mode_is_float(mode)) {
173 return get_mode_null(mode);
179 if ((ta != tarval_bad) && (tb != tarval_bad))
180 return tarval_sub(ta, tb, mode);
186 * Return the value of an unary Minus.
188 static ir_tarval *computed_value_Minus(const ir_node *n)
190 ir_node *a = get_Minus_op(n);
191 ir_tarval *ta = value_of(a);
193 if (ta != tarval_bad)
194 return tarval_neg(ta);
200 * Return the value of a Mul.
202 static ir_tarval *computed_value_Mul(const ir_node *n)
204 ir_node *a = get_Mul_left(n);
205 ir_node *b = get_Mul_right(n);
206 ir_tarval *ta = value_of(a);
207 ir_tarval *tb = value_of(b);
210 mode = get_irn_mode(n);
211 if (mode != get_irn_mode(a)) {
212 /* n * n = 2n bit multiplication */
213 ta = tarval_convert_to(ta, mode);
214 tb = tarval_convert_to(tb, mode);
217 if (ta != tarval_bad && tb != tarval_bad) {
218 return tarval_mul(ta, tb);
220 /* a * 0 != 0 if a == NaN or a == Inf */
221 if (!mode_is_float(mode)) {
222 /* a*0 = 0 or 0*b = 0 */
223 if (ta == get_mode_null(mode))
225 if (tb == get_mode_null(mode))
233 * Return the value of an And.
234 * Special case: a & 0, 0 & b
236 static ir_tarval *computed_value_And(const ir_node *n)
238 ir_node *a = get_And_left(n);
239 ir_node *b = get_And_right(n);
240 ir_tarval *ta = value_of(a);
241 ir_tarval *tb = value_of(b);
243 if ((ta != tarval_bad) && (tb != tarval_bad)) {
244 return tarval_and (ta, tb);
247 if (tarval_is_null(ta)) return ta;
248 if (tarval_is_null(tb)) return tb;
251 if ((is_Not(a) && get_Not_op(a) == b)
252 || (is_Not(b) && get_Not_op(b) == a)) {
253 return get_mode_null(get_irn_mode(n));
260 * Return the value of an Or.
261 * Special case: a | 1...1, 1...1 | b
263 static ir_tarval *computed_value_Or(const ir_node *n)
265 ir_node *a = get_Or_left(n);
266 ir_node *b = get_Or_right(n);
267 ir_tarval *ta = value_of(a);
268 ir_tarval *tb = value_of(b);
270 if ((ta != tarval_bad) && (tb != tarval_bad)) {
271 return tarval_or (ta, tb);
274 if (tarval_is_all_one(ta)) return ta;
275 if (tarval_is_all_one(tb)) return tb;
278 if ((is_Not(a) && get_Not_op(a) == b)
279 || (is_Not(b) && get_Not_op(b) == a)) {
280 return get_mode_all_one(get_irn_mode(n));
286 * Return the value of an Eor.
288 static ir_tarval *computed_value_Eor(const ir_node *n)
290 ir_node *a = get_Eor_left(n);
291 ir_node *b = get_Eor_right(n);
296 return get_mode_null(get_irn_mode(n));
298 if ((is_Not(a) && get_Not_op(a) == b)
299 || (is_Not(b) && get_Not_op(b) == a)) {
300 return get_mode_all_one(get_irn_mode(n));
306 if ((ta != tarval_bad) && (tb != tarval_bad)) {
307 return tarval_eor(ta, tb);
313 * Return the value of a Not.
315 static ir_tarval *computed_value_Not(const ir_node *n)
317 ir_node *a = get_Not_op(n);
318 ir_tarval *ta = value_of(a);
320 if (ta != tarval_bad)
321 return tarval_not(ta);
327 * Tests whether a shift shifts more bits than available in the mode
329 static bool is_oversize_shift(const ir_node *n)
331 ir_node *count = get_binop_right(n);
332 ir_mode *mode = get_irn_mode(n);
333 ir_tarval *tv = value_of(count);
336 if (tv == tarval_bad)
338 if (!tarval_is_long(tv))
340 shiftval = get_tarval_long(tv);
341 modulo_shift = get_mode_modulo_shift(mode);
342 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
345 return shiftval >= (long)get_mode_size_bits(mode);
349 * Return the value of a Shl.
351 static ir_tarval *computed_value_Shl(const ir_node *n)
353 ir_node *a = get_Shl_left(n);
354 ir_node *b = get_Shl_right(n);
356 ir_tarval *ta = value_of(a);
357 ir_tarval *tb = value_of(b);
359 if ((ta != tarval_bad) && (tb != tarval_bad)) {
360 return tarval_shl(ta, tb);
363 if (is_oversize_shift(n))
364 return get_mode_null(get_irn_mode(n));
370 * Return the value of a Shr.
372 static ir_tarval *computed_value_Shr(const ir_node *n)
374 ir_node *a = get_Shr_left(n);
375 ir_node *b = get_Shr_right(n);
377 ir_tarval *ta = value_of(a);
378 ir_tarval *tb = value_of(b);
380 if ((ta != tarval_bad) && (tb != tarval_bad)) {
381 return tarval_shr(ta, tb);
383 if (is_oversize_shift(n))
384 return get_mode_null(get_irn_mode(n));
390 * Return the value of a Shrs.
392 static ir_tarval *computed_value_Shrs(const ir_node *n)
394 ir_node *a = get_Shrs_left(n);
395 ir_node *b = get_Shrs_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_shrs(ta, tb);
407 * Return the value of a Rotl.
409 static ir_tarval *computed_value_Rotl(const ir_node *n)
411 ir_node *a = get_Rotl_left(n);
412 ir_node *b = get_Rotl_right(n);
414 ir_tarval *ta = value_of(a);
415 ir_tarval *tb = value_of(b);
417 if ((ta != tarval_bad) && (tb != tarval_bad)) {
418 return tarval_rotl(ta, tb);
423 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
425 ir_mode *mode = get_irn_mode(node);
426 if (get_mode_arithmetic(mode) != irma_twos_complement
427 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
431 ir_node *count = get_Shl_right(node);
432 if (is_Const(count)) {
433 ir_tarval *tv = get_Const_tarval(count);
434 if (tarval_is_long(tv)) {
435 long shiftval = get_tarval_long(tv);
436 long destbits = get_mode_size_bits(dest_mode);
437 if (shiftval >= destbits
438 && shiftval < (long)get_mode_modulo_shift(mode))
444 ir_node *right = get_And_right(node);
445 if (is_Const(right)) {
446 ir_tarval *tv = get_Const_tarval(right);
447 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
448 return tarval_is_null(conved);
455 * Return the value of a Conv.
457 static ir_tarval *computed_value_Conv(const ir_node *n)
459 ir_node *a = get_Conv_op(n);
460 ir_tarval *ta = value_of(a);
461 ir_mode *mode = get_irn_mode(n);
463 if (ta != tarval_bad)
464 return tarval_convert_to(ta, get_irn_mode(n));
466 if (ir_zero_when_converted(a, mode))
467 return get_mode_null(mode);
473 * Calculate the value of a Mux: can be evaluated, if the
474 * sel and the right input are known.
476 static ir_tarval *computed_value_Mux(const ir_node *n)
478 ir_node *sel = get_Mux_sel(n);
479 ir_tarval *ts = value_of(sel);
481 if (ts == get_tarval_b_true()) {
482 ir_node *v = get_Mux_true(n);
485 else if (ts == get_tarval_b_false()) {
486 ir_node *v = get_Mux_false(n);
493 * Calculate the value of a Confirm: can be evaluated,
494 * if it has the form Confirm(x, '=', Const).
496 static ir_tarval *computed_value_Confirm(const ir_node *n)
498 if (get_Confirm_relation(n) == ir_relation_equal) {
499 ir_tarval *tv = value_of(get_Confirm_bound(n));
500 if (tv != tarval_bad)
503 return value_of(get_Confirm_value(n));
507 * gives a (conservative) estimation of possible relation when comparing
510 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
511 const ir_node *right)
513 ir_relation possible = ir_relation_true;
514 ir_tarval *tv_l = value_of(left);
515 ir_tarval *tv_r = value_of(right);
516 ir_mode *mode = get_irn_mode(left);
517 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
518 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
520 /* both values known - evaluate them */
521 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
522 possible = tarval_cmp(tv_l, tv_r);
523 /* we can return now, won't get any better */
526 /* a == a is never less or greater (but might be equal or unordered) */
528 possible &= ~ir_relation_less_greater;
529 /* unordered results only happen for float compares */
530 if (!mode_is_float(mode))
531 possible &= ~ir_relation_unordered;
532 /* values can never be less than the least representable number or
533 * greater than the greatest representable number */
535 possible &= ~ir_relation_greater;
537 possible &= ~ir_relation_less;
539 possible &= ~ir_relation_greater;
541 possible &= ~ir_relation_less;
542 /* maybe vrp can tell us more */
543 possible &= vrp_cmp(left, right);
544 /* Alloc nodes never return null (but throw an exception) */
545 if (is_Alloc(left) && tarval_is_null(tv_r))
546 possible &= ~ir_relation_equal;
547 /* stuff known through confirm nodes */
548 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
549 possible &= get_Confirm_relation(left);
551 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
552 ir_relation relation = get_Confirm_relation(right);
553 relation = get_inversed_relation(relation);
554 possible &= relation;
560 static ir_tarval *compute_cmp(const ir_node *cmp)
562 ir_node *left = get_Cmp_left(cmp);
563 ir_node *right = get_Cmp_right(cmp);
564 ir_relation possible = ir_get_possible_cmp_relations(left, right);
565 ir_relation relation = get_Cmp_relation(cmp);
567 /* if none of the requested relations is possible, return false */
568 if ((possible & relation) == ir_relation_false)
569 return tarval_b_false;
570 /* if possible relations are a subset of the requested ones return true */
571 if ((possible & ~relation) == ir_relation_false)
572 return tarval_b_true;
574 return computed_value_Cmp_Confirm(cmp, left, right, relation);
578 * some people want to call compute_cmp directly, in this case we have to
579 * test the constant folding flag again
581 static ir_tarval *compute_cmp_ext(const ir_node *cmp)
583 if (!get_opt_constant_folding())
585 return compute_cmp(cmp);
589 * Return the value of a Cmp.
591 * The basic idea here is to determine which relations are possible and which
592 * one are definitely impossible.
594 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
596 /* we can't construct Constb after lowering mode_b nodes */
597 if (irg_is_constrained(get_irn_irg(cmp), IR_GRAPH_CONSTRAINT_MODEB_LOWERED))
600 return compute_cmp(cmp);
604 * Calculate the value of an integer Div.
605 * Special case: 0 / b
607 static ir_tarval *do_computed_value_Div(const ir_node *div)
609 const ir_node *a = get_Div_left(div);
610 const ir_node *b = get_Div_right(div);
611 const ir_mode *mode = get_Div_resmode(div);
612 ir_tarval *ta = value_of(a);
614 const ir_node *dummy;
616 /* cannot optimize 0 / b = 0 because of NaN */
617 if (!mode_is_float(mode)) {
618 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
619 return ta; /* 0 / b == 0 if b != 0 */
622 if (ta != tarval_bad && tb != tarval_bad)
623 return tarval_div(ta, tb);
628 * Calculate the value of an integer Mod of two nodes.
629 * Special case: a % 1
631 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
633 ir_tarval *ta = value_of(a);
634 ir_tarval *tb = value_of(b);
636 /* Compute a % 1 or c1 % c2 */
637 if (tarval_is_one(tb))
638 return get_mode_null(get_irn_mode(a));
639 if (ta != tarval_bad && tb != tarval_bad)
640 return tarval_mod(ta, tb);
645 * Return the value of a Proj(Div).
647 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
649 long proj_nr = get_Proj_proj(n);
650 if (proj_nr != pn_Div_res)
653 return do_computed_value_Div(get_Proj_pred(n));
657 * Return the value of a Proj(Mod).
659 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
661 long proj_nr = get_Proj_proj(n);
663 if (proj_nr == pn_Mod_res) {
664 const ir_node *mod = get_Proj_pred(n);
665 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
671 * Return the value of a Proj.
673 static ir_tarval *computed_value_Proj(const ir_node *proj)
675 ir_node *n = get_Proj_pred(proj);
677 if (n->op->ops.computed_value_Proj != NULL)
678 return n->op->ops.computed_value_Proj(proj);
683 * If the parameter n can be computed, return its value, else tarval_bad.
684 * Performs constant folding.
686 * @param n The node this should be evaluated
688 ir_tarval *computed_value(const ir_node *n)
690 vrp_attr *vrp = vrp_get_info(n);
691 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
692 return vrp->bits_set;
694 if (n->op->ops.computed_value)
695 return n->op->ops.computed_value(n);
700 * Optimize operations that are commutative and have neutral 0,
701 * so a op 0 = 0 op a = a.
703 static ir_node *equivalent_node_neutral_zero(ir_node *n)
707 ir_node *a = get_binop_left(n);
708 ir_node *b = get_binop_right(n);
713 /* After running compute_node there is only one constant predecessor.
714 Find this predecessors value and remember the other node: */
715 if ((tv = value_of(a)) != tarval_bad) {
717 } else if ((tv = value_of(b)) != tarval_bad) {
722 /* If this predecessors constant value is zero, the operation is
723 * unnecessary. Remove it.
725 * Beware: If n is a Add, the mode of on and n might be different
726 * which happens in this rare construction: NULL + 3.
727 * Then, a Conv would be needed which we cannot include here.
729 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
732 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
739 * Eor is commutative and has neutral 0.
741 static ir_node *equivalent_node_Eor(ir_node *n)
747 n = equivalent_node_neutral_zero(n);
748 if (n != oldn) return n;
751 b = get_Eor_right(n);
753 if (is_Eor(a) || is_Or_Eor_Add(a)) {
754 ir_node *aa = get_binop_left(a);
755 ir_node *ab = get_binop_right(a);
758 /* (a ^ b) ^ a -> b */
760 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
762 } else if (ab == b) {
763 /* (a ^ b) ^ b -> a */
765 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
769 if (is_Eor(b) || is_Or_Eor_Add(b)) {
770 ir_node *ba = get_binop_left(b);
771 ir_node *bb = get_binop_right(b);
774 /* a ^ (a ^ b) -> b */
776 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
778 } else if (bb == a) {
779 /* a ^ (b ^ a) -> b */
781 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
789 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
791 * The second one looks strange, but this construct
792 * is used heavily in the LCC sources :-).
794 * Beware: The Mode of an Add may be different than the mode of its
795 * predecessors, so we could not return a predecessors in all cases.
797 static ir_node *equivalent_node_Add(ir_node *n)
800 ir_node *left, *right;
801 ir_mode *mode = get_irn_mode(n);
803 n = equivalent_node_neutral_zero(n);
807 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
808 if (mode_is_float(mode)) {
809 ir_graph *irg = get_irn_irg(n);
810 if (get_irg_fp_model(irg) & fp_strict_algebraic)
814 left = get_Add_left(n);
815 right = get_Add_right(n);
818 if (get_Sub_right(left) == right) {
821 n = get_Sub_left(left);
822 if (mode == get_irn_mode(n)) {
823 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
829 if (get_Sub_right(right) == left) {
832 n = get_Sub_left(right);
833 if (mode == get_irn_mode(n)) {
834 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
843 * optimize operations that are not commutative but have neutral 0 on left,
846 static ir_node *equivalent_node_left_zero(ir_node *n)
850 ir_node *a = get_binop_left(n);
851 ir_node *b = get_binop_right(n);
852 ir_tarval *tb = value_of(b);
854 if (tarval_is_null(tb)) {
857 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
863 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
865 * The second one looks strange, but this construct
866 * is used heavily in the LCC sources :-).
868 * Beware: The Mode of a Sub may be different than the mode of its
869 * predecessors, so we could not return a predecessors in all cases.
871 static ir_node *equivalent_node_Sub(ir_node *n)
875 ir_mode *mode = get_irn_mode(n);
878 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
879 if (mode_is_float(mode)) {
880 ir_graph *irg = get_irn_irg(n);
881 if (get_irg_fp_model(irg) & fp_strict_algebraic)
885 b = get_Sub_right(n);
888 /* Beware: modes might be different */
889 if (tarval_is_null(tb)) {
890 ir_node *a = get_Sub_left(n);
891 if (mode == get_irn_mode(a)) {
894 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
902 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
905 * -(-a) == a, but might overflow two times.
906 * We handle it anyway here but the better way would be a
907 * flag. This would be needed for Pascal for instance.
909 static ir_node *equivalent_node_involution(ir_node *n)
912 ir_node *pred = get_unop_op(n);
913 if (get_irn_op(pred) == get_irn_op(n)) {
914 n = get_unop_op(pred);
915 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_INVOLUTION);
921 * Optimize a * 1 = 1 * a = a.
923 static ir_node *equivalent_node_Mul(ir_node *n)
926 ir_node *a = get_Mul_left(n);
928 /* we can handle here only the n * n = n bit cases */
929 if (get_irn_mode(n) == get_irn_mode(a)) {
930 ir_node *b = get_Mul_right(n);
934 * Mul is commutative and has again an other neutral element.
935 * Constants are place right, so check this case first.
938 if (tarval_is_one(tv)) {
940 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
943 if (tarval_is_one(tv)) {
945 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
953 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
955 static ir_node *equivalent_node_Or(ir_node *n)
959 ir_node *a = get_Or_left(n);
960 ir_node *b = get_Or_right(n);
964 n = a; /* idempotence */
965 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
968 /* constants are normalized to right, check this side first */
970 if (tarval_is_null(tv)) {
972 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
976 if (tarval_is_null(tv)) {
978 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
986 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
988 static ir_node *equivalent_node_And(ir_node *n)
992 ir_node *a = get_And_left(n);
993 ir_node *b = get_And_right(n);
997 n = a; /* idempotence */
998 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1001 /* constants are normalized to right, check this side first */
1003 if (tarval_is_all_one(tv)) {
1005 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1008 if (tv != get_tarval_bad()) {
1009 ir_mode *mode = get_irn_mode(n);
1010 if (!mode_is_signed(mode) && is_Conv(a)) {
1011 ir_node *convop = get_Conv_op(a);
1012 ir_mode *convopmode = get_irn_mode(convop);
1013 if (!mode_is_signed(convopmode)) {
1014 /* Check Conv(all_one) & Const = all_one */
1015 ir_tarval *one = get_mode_all_one(convopmode);
1016 ir_tarval *conv = tarval_convert_to(one, mode);
1017 ir_tarval *tand = tarval_and(conv, tv);
1019 if (tarval_is_all_one(tand)) {
1020 /* Conv(X) & Const = X */
1022 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1029 if (tarval_is_all_one(tv)) {
1031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1035 if ((is_Or(a) || is_Or_Eor_Add(a))
1036 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1042 if ((is_Or(b) || is_Or_Eor_Add(b))
1043 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1052 * Try to remove useless Conv's:
1054 static ir_node *equivalent_node_Conv(ir_node *n)
1057 ir_node *a = get_Conv_op(n);
1059 ir_mode *n_mode = get_irn_mode(n);
1060 ir_mode *a_mode = get_irn_mode(a);
1062 if (n_mode == a_mode) { /* No Conv necessary */
1064 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1066 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1067 ir_node *b = get_Conv_op(a);
1068 ir_mode *b_mode = get_irn_mode(b);
1070 if (n_mode == b_mode && values_in_mode(b_mode, a_mode)) {
1072 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1080 * - fold Phi-nodes, iff they have only one predecessor except
1083 static ir_node *equivalent_node_Phi(ir_node *n)
1088 ir_node *first_val = NULL; /* to shutup gcc */
1090 if (!get_opt_optimize() &&
1091 !irg_is_constrained(get_irn_irg(n), IR_GRAPH_CONSTRAINT_CONSTRUCTION))
1094 n_preds = get_Phi_n_preds(n);
1096 /* Phi of dead Region without predecessors. */
1100 /* Find first non-self-referencing input */
1101 for (i = 0; i < n_preds; ++i) {
1102 first_val = get_Phi_pred(n, i);
1103 /* not self pointer */
1104 if (first_val != n) {
1105 /* then found first value. */
1110 /* search for rest of inputs, determine if any of these
1111 are non-self-referencing */
1112 while (++i < n_preds) {
1113 ir_node *scnd_val = get_Phi_pred(n, i);
1114 if (scnd_val != n && scnd_val != first_val) {
1119 if (i >= n_preds && !is_Dummy(first_val)) {
1120 /* Fold, if no multiple distinct non-self-referencing inputs */
1122 DBG_OPT_PHI(oldn, n);
1128 * Optimize Proj(Tuple).
1130 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1132 ir_node *oldn = proj;
1133 ir_node *tuple = get_Proj_pred(proj);
1135 /* Remove the Tuple/Proj combination. */
1136 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1137 DBG_OPT_TUPLE(oldn, tuple, proj);
1143 * Optimize a / 1 = a.
1145 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1147 ir_node *oldn = proj;
1148 ir_node *div = get_Proj_pred(proj);
1149 ir_node *b = get_Div_right(div);
1150 ir_tarval *tb = value_of(b);
1152 /* Div is not commutative. */
1153 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1154 switch (get_Proj_proj(proj)) {
1156 proj = get_Div_mem(div);
1157 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1161 proj = get_Div_left(div);
1162 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1166 /* we cannot replace the exception Proj's here, this is done in
1167 transform_node_Proj_Div() */
1175 * Optimize CopyB(mem, x, x) into a Nop.
1177 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1179 ir_node *oldn = proj;
1180 ir_node *copyb = get_Proj_pred(proj);
1181 ir_node *a = get_CopyB_dst(copyb);
1182 ir_node *b = get_CopyB_src(copyb);
1185 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1186 switch (get_Proj_proj(proj)) {
1188 proj = get_CopyB_mem(copyb);
1189 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1197 * Does all optimizations on nodes that must be done on its Projs
1198 * because of creating new nodes.
1200 static ir_node *equivalent_node_Proj(ir_node *proj)
1202 ir_node *n = get_Proj_pred(proj);
1203 if (n->op->ops.equivalent_node_Proj)
1204 return n->op->ops.equivalent_node_Proj(proj);
1211 static ir_node *equivalent_node_Id(ir_node *n)
1219 DBG_OPT_ID(oldn, n);
1226 static ir_node *equivalent_node_Mux(ir_node *n)
1228 ir_node *oldn = n, *sel = get_Mux_sel(n);
1230 ir_tarval *ts = value_of(sel);
1232 if (ts == tarval_bad && is_Cmp(sel)) {
1233 /* try again with a direct call to compute_cmp, as we don't care
1234 * about the MODEB_LOWERED flag here */
1235 ts = compute_cmp_ext(sel);
1238 /* Mux(true, f, t) == t */
1239 if (ts == tarval_b_true) {
1240 n = get_Mux_true(n);
1241 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1244 /* Mux(false, f, t) == f */
1245 if (ts == tarval_b_false) {
1246 n = get_Mux_false(n);
1247 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1250 n_t = get_Mux_true(n);
1251 n_f = get_Mux_false(n);
1253 /* Mux(v, x, T) == x */
1254 if (is_Unknown(n_f)) {
1256 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1259 /* Mux(v, T, x) == x */
1260 if (is_Unknown(n_t)) {
1262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1266 /* Mux(v, x, x) == x */
1269 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1272 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1273 ir_relation relation = get_Cmp_relation(sel);
1274 ir_node *f = get_Mux_false(n);
1275 ir_node *t = get_Mux_true(n);
1278 * Note further that these optimization work even for floating point
1279 * with NaN's because -NaN == NaN.
1280 * However, if +0 and -0 is handled differently, we cannot use the first one.
1282 ir_node *const cmp_l = get_Cmp_left(sel);
1283 ir_node *const cmp_r = get_Cmp_right(sel);
1286 case ir_relation_equal:
1287 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1288 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1290 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1295 case ir_relation_less_greater:
1296 case ir_relation_unordered_less_greater:
1297 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1298 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1300 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1309 * Note: normalization puts the constant on the right side,
1310 * so we check only one case.
1312 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1313 /* Mux(t CMP 0, X, t) */
1314 if (is_Minus(f) && get_Minus_op(f) == t) {
1315 /* Mux(t CMP 0, -t, t) */
1316 if (relation == ir_relation_equal) {
1317 /* Mux(t == 0, -t, t) ==> -t */
1319 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1320 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1321 /* Mux(t != 0, -t, t) ==> t */
1323 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1333 * Remove Confirm nodes if setting is on.
1334 * Replace Confirms(x, '=', Constlike) by Constlike.
1336 static ir_node *equivalent_node_Confirm(ir_node *n)
1338 ir_node *pred = get_Confirm_value(n);
1339 ir_relation relation = get_Confirm_relation(n);
1341 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1343 * rare case: two identical Confirms one after another,
1344 * replace the second one with the first.
1347 pred = get_Confirm_value(n);
1353 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1354 * perform no actual computation, as, e.g., the Id nodes. It does not create
1355 * new nodes. It is therefore safe to free n if the node returned is not n.
1356 * If a node returns a Tuple we can not just skip it. If the size of the
1357 * in array fits, we transform n into a tuple (e.g., Div).
1359 ir_node *equivalent_node(ir_node *n)
1361 if (n->op->ops.equivalent_node)
1362 return n->op->ops.equivalent_node(n);
1367 * Returns non-zero if a node is a Phi node
1368 * with all predecessors constant.
1370 static int is_const_Phi(ir_node *n)
1374 if (! is_Phi(n) || get_irn_arity(n) == 0)
1376 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1377 if (! is_Const(get_irn_n(n, i)))
1383 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1384 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1387 * in reality eval_func should be tarval (*eval_func)() but incomplete
1388 * declarations are bad style and generate noisy warnings
1390 typedef void (*eval_func)(void);
1393 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1395 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1397 if (eval == (eval_func) tarval_sub) {
1398 tarval_sub_type func = (tarval_sub_type)eval;
1400 return func(a, b, mode);
1402 tarval_binop_type func = (tarval_binop_type)eval;
1409 * Apply an evaluator on a binop with a constant operators (and one Phi).
1411 * @param phi the Phi node
1412 * @param other the other operand
1413 * @param eval an evaluator function
1414 * @param mode the mode of the result, may be different from the mode of the Phi!
1415 * @param left if non-zero, other is the left operand, else the right
1417 * @return a new Phi node if the conversion was successful, NULL else
1419 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1421 int n = get_irn_arity(phi);
1422 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1424 for (int i = 0; i < n; ++i) {
1425 ir_node *pred = get_irn_n(phi, i);
1426 ir_tarval *tv = get_Const_tarval(pred);
1427 tv = do_eval(eval, other, tv, mode);
1429 if (tv == tarval_bad) {
1430 /* folding failed, bad */
1436 for (int i = 0; i < n; ++i) {
1437 ir_node *pred = get_irn_n(phi, i);
1438 ir_tarval *tv = get_Const_tarval(pred);
1439 tv = do_eval(eval, tv, other, mode);
1441 if (tv == tarval_bad) {
1442 /* folding failed, bad */
1448 ir_graph *irg = get_irn_irg(phi);
1449 ir_node **res = ALLOCAN(ir_node*, n);
1450 for (int i = 0; i < n; ++i) {
1451 res[i] = new_r_Const(irg, tvs[i]);
1453 ir_node *block = get_nodes_block(phi);
1454 return new_r_Phi(block, n, res, mode);
1458 * Apply an evaluator on a binop with two constant Phi.
1460 * @param a the left Phi node
1461 * @param b the right Phi node
1462 * @param eval an evaluator function
1463 * @param mode the mode of the result, may be different from the mode of the Phi!
1465 * @return a new Phi node if the conversion was successful, NULL else
1467 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1469 if (get_nodes_block(a) != get_nodes_block(b))
1472 int n = get_irn_arity(a);
1473 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1474 for (int i = 0; i < n; ++i) {
1475 ir_node *pred_a = get_irn_n(a, i);
1476 ir_tarval *tv_l = get_Const_tarval(pred_a);
1477 ir_node *pred_b = get_irn_n(b, i);
1478 ir_tarval *tv_r = get_Const_tarval(pred_b);
1479 ir_tarval *tv = do_eval(eval, tv_l, tv_r, mode);
1481 if (tv == tarval_bad) {
1482 /* folding failed, bad */
1487 ir_graph *irg = get_irn_irg(a);
1488 ir_node **res = ALLOCAN(ir_node*, n);
1489 for (int i = 0; i < n; ++i) {
1490 res[i] = new_r_Const(irg, tvs[i]);
1492 ir_node *block = get_nodes_block(a);
1493 return new_r_Phi(block, n, res, mode);
1497 * Apply an evaluator on a unop with a constant operator (a Phi).
1499 * @param phi the Phi node
1500 * @param eval an evaluator function
1502 * @return a new Phi node if the conversion was successful, NULL else
1504 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1506 int n = get_irn_arity(phi);
1507 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1508 for (int i = 0; i < n; ++i) {
1509 ir_node *pred = get_irn_n(phi, i);
1510 ir_tarval *tv = get_Const_tarval(pred);
1513 if (tv == tarval_bad) {
1514 /* folding failed, bad */
1519 ir_graph *irg = get_irn_irg(phi);
1520 ir_node **res = ALLOCAN(ir_node*, n);
1521 for (int i = 0; i < n; ++i) {
1522 res[i] = new_r_Const(irg, tvs[i]);
1524 ir_node *block = get_nodes_block(phi);
1525 ir_mode *mode = get_irn_mode(phi);
1526 return new_r_Phi(block, n, res, mode);
1530 * Apply a conversion on a constant operator (a Phi).
1532 * @param phi the Phi node
1534 * @return a new Phi node if the conversion was successful, NULL else
1536 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1538 int n = get_irn_arity(phi);
1539 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1540 for (int i = 0; i < n; ++i) {
1541 ir_node *pred = get_irn_n(phi, i);
1542 ir_tarval *tv = get_Const_tarval(pred);
1543 tv = tarval_convert_to(tv, mode);
1545 if (tv == tarval_bad) {
1546 /* folding failed, bad */
1551 ir_graph *irg = get_irn_irg(phi);
1552 ir_node **res = ALLOCAN(ir_node*, n);
1553 for (int i = 0; i < n; ++i) {
1554 res[i] = new_r_Const(irg, tvs[i]);
1556 ir_node *block = get_nodes_block(phi);
1557 return new_r_Phi(block, n, res, mode);
1561 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1562 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1563 * If possible, remove the Conv's.
1565 static ir_node *transform_node_AddSub(ir_node *n)
1567 ir_mode *mode = get_irn_mode(n);
1569 if (mode_is_reference(mode)) {
1570 ir_node *left = get_binop_left(n);
1571 ir_node *right = get_binop_right(n);
1572 unsigned ref_bits = get_mode_size_bits(mode);
1574 if (is_Conv(left)) {
1575 ir_mode *lmode = get_irn_mode(left);
1576 unsigned bits = get_mode_size_bits(lmode);
1578 if (ref_bits == bits &&
1579 mode_is_int(lmode) &&
1580 get_mode_arithmetic(lmode) == irma_twos_complement) {
1581 ir_node *pre = get_Conv_op(left);
1582 ir_mode *pre_mode = get_irn_mode(pre);
1584 if (mode_is_int(pre_mode) &&
1585 get_mode_size_bits(pre_mode) == bits &&
1586 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1587 /* ok, this conv just changes to sign, moreover the calculation
1588 * is done with same number of bits as our address mode, so
1589 * we can ignore the conv as address calculation can be viewed
1590 * as either signed or unsigned
1592 set_binop_left(n, pre);
1597 if (is_Conv(right)) {
1598 ir_mode *rmode = get_irn_mode(right);
1599 unsigned bits = get_mode_size_bits(rmode);
1601 if (ref_bits == bits &&
1602 mode_is_int(rmode) &&
1603 get_mode_arithmetic(rmode) == irma_twos_complement) {
1604 ir_node *pre = get_Conv_op(right);
1605 ir_mode *pre_mode = get_irn_mode(pre);
1607 if (mode_is_int(pre_mode) &&
1608 get_mode_size_bits(pre_mode) == bits &&
1609 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1610 /* ok, this conv just changes to sign, moreover the calculation
1611 * is done with same number of bits as our address mode, so
1612 * we can ignore the conv as address calculation can be viewed
1613 * as either signed or unsigned
1615 set_binop_right(n, pre);
1620 /* let address arithmetic use unsigned modes */
1621 if (is_Const(right)) {
1622 ir_mode *rmode = get_irn_mode(right);
1624 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1625 /* convert a AddP(P, *s) into AddP(P, *u) */
1626 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1628 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1629 set_binop_right(n, pre);
1637 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1640 if (is_Const(b) && is_const_Phi(a)) { \
1641 /* check for Op(Phi, Const) */ \
1642 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1644 else if (is_Const(a) && is_const_Phi(b)) { \
1645 /* check for Op(Const, Phi) */ \
1646 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1648 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1649 /* check for Op(Phi, Phi) */ \
1650 c = apply_binop_on_2_phis(a, b, eval, mode); \
1653 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1658 #define HANDLE_UNOP_PHI(eval, a, c) \
1661 if (is_const_Phi(a)) { \
1662 /* check for Op(Phi) */ \
1663 c = apply_unop_on_phi(a, eval); \
1665 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1672 * Create a 0 constant of given mode.
1674 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1676 ir_tarval *tv = get_mode_null(mode);
1677 ir_node *cnst = new_r_Const(irg, tv);
1682 static bool is_shiftop(const ir_node *n)
1684 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1687 /* the order of the values is important! */
1688 typedef enum const_class {
1694 static const_class classify_const(const ir_node* n)
1696 if (is_Const(n)) return const_const;
1697 if (is_irn_constlike(n)) return const_like;
1702 * Determines whether r is more constlike or has a larger index (in that order)
1705 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1707 const const_class l_order = classify_const(l);
1708 const const_class r_order = classify_const(r);
1710 l_order > r_order ||
1711 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1714 static bool is_cmp_unequal(const ir_node *node)
1716 ir_relation relation = get_Cmp_relation(node);
1717 ir_node *left = get_Cmp_left(node);
1718 ir_node *right = get_Cmp_right(node);
1719 ir_mode *mode = get_irn_mode(left);
1721 if (relation == ir_relation_less_greater)
1724 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1725 return relation == ir_relation_greater;
1730 * returns true for Cmp(x == 0) or Cmp(x != 0)
1732 static bool is_cmp_equality_zero(const ir_node *node)
1734 ir_relation relation;
1735 ir_node *right = get_Cmp_right(node);
1737 if (!is_Const(right) || !is_Const_null(right))
1739 relation = get_Cmp_relation(node);
1740 return relation == ir_relation_equal
1741 || relation == ir_relation_less_greater
1742 || (!mode_is_signed(get_irn_mode(right))
1743 && relation == ir_relation_greater);
1747 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1748 * Such pattern may arise in bitfield stores.
1750 * value c4 value c4 & c2
1751 * AND c3 AND c1 | c3
1758 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1761 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1763 ir_node *irn_and, *c1;
1765 ir_node *and_l, *c3;
1766 ir_node *value, *c4;
1767 ir_node *new_and, *new_const, *block;
1768 ir_mode *mode = get_irn_mode(irn_or);
1770 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1774 irn_and = get_binop_left(irn_or);
1775 c1 = get_binop_right(irn_or);
1776 if (!is_Const(c1) || !is_And(irn_and))
1779 or_l = get_binop_left(irn_and);
1780 c2 = get_binop_right(irn_and);
1784 tv1 = get_Const_tarval(c1);
1785 tv2 = get_Const_tarval(c2);
1787 tv = tarval_or(tv1, tv2);
1788 if (tarval_is_all_one(tv)) {
1789 /* the AND does NOT clear a bit with isn't set by the OR */
1790 set_binop_left(irn_or, or_l);
1791 set_binop_right(irn_or, c1);
1793 /* check for more */
1797 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
1800 and_l = get_binop_left(or_l);
1801 c3 = get_binop_right(or_l);
1802 if (!is_Const(c3) || !is_And(and_l))
1805 value = get_binop_left(and_l);
1806 c4 = get_binop_right(and_l);
1810 /* ok, found the pattern, check for conditions */
1811 assert(mode == get_irn_mode(irn_and));
1812 assert(mode == get_irn_mode(or_l));
1813 assert(mode == get_irn_mode(and_l));
1815 tv3 = get_Const_tarval(c3);
1816 tv4 = get_Const_tarval(c4);
1818 tv = tarval_or(tv4, tv2);
1819 if (!tarval_is_all_one(tv)) {
1820 /* have at least one 0 at the same bit position */
1824 if (tv3 != tarval_andnot(tv3, tv4)) {
1825 /* bit in the or_mask is outside the and_mask */
1829 if (tv1 != tarval_andnot(tv1, tv2)) {
1830 /* bit in the or_mask is outside the and_mask */
1834 /* ok, all conditions met */
1835 block = get_nodes_block(irn_or);
1836 irg = get_irn_irg(block);
1838 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
1840 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
1842 set_binop_left(irn_or, new_and);
1843 set_binop_right(irn_or, new_const);
1845 /* check for more */
1850 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
1852 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
1854 ir_mode *mode = get_irn_mode(irn_or);
1855 ir_node *shl, *shr, *block;
1856 ir_node *irn, *x, *c1, *c2, *n;
1857 ir_tarval *tv1, *tv2;
1859 /* some backends can't handle rotl */
1860 if (!be_get_backend_param()->support_rotl)
1863 if (! mode_is_int(mode))
1866 shl = get_binop_left(irn_or);
1867 shr = get_binop_right(irn_or);
1876 } else if (!is_Shl(shl)) {
1878 } else if (!is_Shr(shr)) {
1881 x = get_Shl_left(shl);
1882 if (x != get_Shr_left(shr))
1885 c1 = get_Shl_right(shl);
1886 c2 = get_Shr_right(shr);
1887 if (is_Const(c1) && is_Const(c2)) {
1888 tv1 = get_Const_tarval(c1);
1889 if (! tarval_is_long(tv1))
1892 tv2 = get_Const_tarval(c2);
1893 if (! tarval_is_long(tv2))
1896 if (get_tarval_long(tv1) + get_tarval_long(tv2)
1897 != (int) get_mode_size_bits(mode))
1900 /* yet, condition met */
1901 block = get_nodes_block(irn_or);
1903 n = new_r_Rotl(block, x, c1, mode);
1905 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
1909 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
1910 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
1911 if (!ir_is_negated_value(c1, c2)) {
1915 /* yet, condition met */
1916 block = get_nodes_block(irn_or);
1917 n = new_r_Rotl(block, x, c1, mode);
1918 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
1923 * Prototype of a recursive transform function
1924 * for bitwise distributive transformations.
1926 typedef ir_node* (*recursive_transform)(ir_node *n);
1929 * makes use of distributive laws for and, or, eor
1930 * and(a OP c, b OP c) -> and(a, b) OP c
1931 * note, might return a different op than n
1933 static ir_node *transform_bitwise_distributive(ir_node *n,
1934 recursive_transform trans_func)
1937 ir_node *a = get_binop_left(n);
1938 ir_node *b = get_binop_right(n);
1939 ir_op *op = get_irn_op(a);
1940 ir_op *op_root = get_irn_op(n);
1942 if (op != get_irn_op(b))
1945 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
1946 if (op == op_Conv) {
1947 ir_node *a_op = get_Conv_op(a);
1948 ir_node *b_op = get_Conv_op(b);
1949 ir_mode *a_mode = get_irn_mode(a_op);
1950 ir_mode *b_mode = get_irn_mode(b_op);
1951 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
1952 ir_node *blk = get_nodes_block(n);
1955 set_binop_left(n, a_op);
1956 set_binop_right(n, b_op);
1957 set_irn_mode(n, a_mode);
1959 n = new_r_Conv(blk, n, get_irn_mode(oldn));
1961 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1967 /* nothing to gain here */
1971 if (op == op_Shrs || op == op_Shr || op == op_Shl
1972 || op == op_And || op == op_Or || op == op_Eor) {
1973 ir_node *a_left = get_binop_left(a);
1974 ir_node *a_right = get_binop_right(a);
1975 ir_node *b_left = get_binop_left(b);
1976 ir_node *b_right = get_binop_right(b);
1978 ir_node *op1 = NULL;
1979 ir_node *op2 = NULL;
1981 if (is_op_commutative(op)) {
1982 if (a_left == b_left) {
1986 } else if (a_left == b_right) {
1990 } else if (a_right == b_left) {
1996 if (a_right == b_right) {
2003 /* (a sop c) & (b sop c) => (a & b) sop c */
2004 ir_node *blk = get_nodes_block(n);
2006 ir_node *new_n = exact_copy(n);
2007 set_binop_left(new_n, op1);
2008 set_binop_right(new_n, op2);
2009 new_n = trans_func(new_n);
2011 if (op_root == op_Eor && op == op_Or) {
2012 dbg_info *dbgi = get_irn_dbg_info(n);
2013 ir_mode *mode = get_irn_mode(c);
2015 c = new_rd_Not(dbgi, blk, c, mode);
2016 n = new_rd_And(dbgi, blk, new_n, c, mode);
2019 set_nodes_block(n, blk);
2020 set_binop_left(n, new_n);
2021 set_binop_right(n, c);
2025 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2034 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2036 * - and, or, xor instead of &
2037 * - Shl, Shr, Shrs, rotl instead of >>
2038 * (with a special case for Or/Xor + Shrs)
2040 * This normalisation is usually good for the backend since << C can often be
2041 * matched as address-mode.
2043 static ir_node *transform_node_bitop_shift(ir_node *n)
2045 ir_graph *irg = get_irn_irg(n);
2046 ir_node *left = get_binop_left(n);
2047 ir_node *right = get_binop_right(n);
2048 ir_mode *mode = get_irn_mode(n);
2049 ir_node *shift_left;
2050 ir_node *shift_right;
2052 dbg_info *dbg_bitop;
2053 dbg_info *dbg_shift;
2059 ir_tarval *tv_bitop;
2061 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2064 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2065 if (!is_Const(right) || !is_shiftop(left))
2068 shift_left = get_binop_left(left);
2069 shift_right = get_binop_right(left);
2070 if (!is_Const(shift_right))
2073 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2074 if (is_Shrs(left)) {
2075 /* TODO this could be improved */
2079 irg = get_irn_irg(n);
2080 block = get_nodes_block(n);
2081 dbg_bitop = get_irn_dbg_info(n);
2082 dbg_shift = get_irn_dbg_info(left);
2083 tv1 = get_Const_tarval(shift_right);
2084 tv2 = get_Const_tarval(right);
2085 assert(get_tarval_mode(tv2) == mode);
2088 tv_bitop = tarval_shr(tv2, tv1);
2090 /* Check whether we have lost some bits during the right shift. */
2092 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2094 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2097 } else if (is_Shr(left)) {
2100 * TODO this can be improved by checking whether
2101 * the left shift produces an overflow
2105 tv_bitop = tarval_shl(tv2, tv1);
2107 assert(is_Rotl(left));
2108 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2110 new_const = new_r_Const(irg, tv_bitop);
2113 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2114 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2115 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2118 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2122 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2123 } else if (is_Shr(left)) {
2124 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2126 assert(is_Rotl(left));
2127 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2133 static bool complement_values(const ir_node *a, const ir_node *b)
2135 if (is_Not(a) && get_Not_op(a) == b)
2137 if (is_Not(b) && get_Not_op(b) == a)
2139 if (is_Const(a) && is_Const(b)) {
2140 ir_tarval *tv_a = get_Const_tarval(a);
2141 ir_tarval *tv_b = get_Const_tarval(b);
2142 return tarval_not(tv_a) == tv_b;
2147 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2150 * for associative operations fold:
2151 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2152 * This is a "light" version of the reassociation phase
2154 static ir_node *fold_constant_associativity(ir_node *node,
2155 tv_fold_binop_func fold)
2160 ir_node *right = get_binop_right(node);
2161 ir_node *left_right;
2168 if (!is_Const(right))
2171 op = get_irn_op(node);
2172 left = get_binop_left(node);
2173 if (get_irn_op(left) != op)
2176 left_right = get_binop_right(left);
2177 if (!is_Const(left_right))
2180 left_left = get_binop_left(left);
2181 c0 = get_Const_tarval(left_right);
2182 c1 = get_Const_tarval(right);
2183 irg = get_irn_irg(node);
2184 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2186 new_c = fold(c0, c1);
2187 if (new_c == tarval_bad)
2189 new_const = new_r_Const(irg, new_c);
2190 new_node = exact_copy(node);
2191 set_binop_left(new_node, left_left);
2192 set_binop_right(new_node, new_const);
2199 static ir_node *transform_node_Or_(ir_node *n)
2202 ir_node *a = get_binop_left(n);
2203 ir_node *b = get_binop_right(n);
2207 n = fold_constant_associativity(n, tarval_or);
2211 if (is_Not(a) && is_Not(b)) {
2212 /* ~a | ~b = ~(a&b) */
2213 ir_node *block = get_nodes_block(n);
2215 mode = get_irn_mode(n);
2218 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2219 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2220 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2224 /* we can combine the relations of two compares with the same operands */
2225 if (is_Cmp(a) && is_Cmp(b)) {
2226 ir_node *a_left = get_Cmp_left(a);
2227 ir_node *a_right = get_Cmp_right(a);
2228 ir_node *b_left = get_Cmp_left(b);
2229 ir_node *b_right = get_Cmp_right(b);
2230 if (a_left == b_left && b_left == b_right) {
2231 dbg_info *dbgi = get_irn_dbg_info(n);
2232 ir_node *block = get_nodes_block(n);
2233 ir_relation a_relation = get_Cmp_relation(a);
2234 ir_relation b_relation = get_Cmp_relation(b);
2235 ir_relation new_relation = a_relation | b_relation;
2236 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2238 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2239 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2240 && !mode_is_float(get_irn_mode(a_left))
2241 && !mode_is_float(get_irn_mode(b_left))) {
2242 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2243 ir_graph *irg = get_irn_irg(n);
2244 dbg_info *dbgi = get_irn_dbg_info(n);
2245 ir_node *block = get_nodes_block(n);
2246 ir_mode *a_mode = get_irn_mode(a_left);
2247 ir_mode *b_mode = get_irn_mode(b_left);
2248 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2249 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2250 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2251 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2252 ir_node *zero = create_zero_const(irg, b_mode);
2253 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2255 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2256 ir_graph *irg = get_irn_irg(n);
2257 dbg_info *dbgi = get_irn_dbg_info(n);
2258 ir_node *block = get_nodes_block(n);
2259 ir_mode *a_mode = get_irn_mode(a_left);
2260 ir_mode *b_mode = get_irn_mode(b_left);
2261 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2262 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2263 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2264 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2265 ir_node *zero = create_zero_const(irg, a_mode);
2266 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2271 mode = get_irn_mode(n);
2272 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2274 n = transform_node_Or_bf_store(n);
2277 n = transform_node_Or_Rotl(n);
2281 n = transform_bitwise_distributive(n, transform_node_Or_);
2284 n = transform_node_bitop_shift(n);
2291 static ir_node *transform_node_Or(ir_node *n)
2293 if (is_Or_Eor_Add(n)) {
2294 dbg_info *dbgi = get_irn_dbg_info(n);
2295 ir_node *block = get_nodes_block(n);
2296 ir_node *left = get_Or_left(n);
2297 ir_node *right = get_Or_right(n);
2298 ir_mode *mode = get_irn_mode(n);
2299 return new_rd_Add(dbgi, block, left, right, mode);
2301 return transform_node_Or_(n);
2307 static ir_node *transform_node_Eor_(ir_node *n)
2310 ir_node *a = get_binop_left(n);
2311 ir_node *b = get_binop_right(n);
2312 ir_mode *mode = get_irn_mode(n);
2315 n = fold_constant_associativity(n, tarval_eor);
2319 /* we can combine the relations of two compares with the same operands */
2320 if (is_Cmp(a) && is_Cmp(b)) {
2321 ir_node *a_left = get_Cmp_left(a);
2322 ir_node *a_right = get_Cmp_left(a);
2323 ir_node *b_left = get_Cmp_left(b);
2324 ir_node *b_right = get_Cmp_right(b);
2325 if (a_left == b_left && b_left == b_right) {
2326 dbg_info *dbgi = get_irn_dbg_info(n);
2327 ir_node *block = get_nodes_block(n);
2328 ir_relation a_relation = get_Cmp_relation(a);
2329 ir_relation b_relation = get_Cmp_relation(b);
2330 ir_relation new_relation = a_relation ^ b_relation;
2331 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2335 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2337 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2338 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2339 dbg_info *dbg = get_irn_dbg_info(n);
2340 ir_node *block = get_nodes_block(n);
2341 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2342 ir_node *new_left = get_Not_op(a);
2343 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2344 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2346 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2347 dbg_info *dbg = get_irn_dbg_info(n);
2348 ir_node *block = get_nodes_block(n);
2349 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2350 ir_node *new_right = get_Not_op(b);
2351 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2352 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2356 /* x ^ 1...1 -> ~1 */
2357 if (is_Const(b) && is_Const_all_one(b)) {
2358 n = new_r_Not(get_nodes_block(n), a, mode);
2359 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2363 n = transform_bitwise_distributive(n, transform_node_Eor_);
2366 n = transform_node_bitop_shift(n);
2373 static ir_node *transform_node_Eor(ir_node *n)
2375 if (is_Or_Eor_Add(n)) {
2376 dbg_info *dbgi = get_irn_dbg_info(n);
2377 ir_node *block = get_nodes_block(n);
2378 ir_node *left = get_Eor_left(n);
2379 ir_node *right = get_Eor_right(n);
2380 ir_mode *mode = get_irn_mode(n);
2381 return new_rd_Add(dbgi, block, left, right, mode);
2383 return transform_node_Eor_(n);
2387 * Do the AddSub optimization, then Transform
2388 * Constant folding on Phi
2389 * Add(a,a) -> Mul(a, 2)
2390 * Add(Mul(a, x), a) -> Mul(a, x+1)
2391 * if the mode is integer or float.
2392 * Transform Add(a,-b) into Sub(a,b).
2393 * Reassociation might fold this further.
2395 static ir_node *transform_node_Add(ir_node *n)
2403 n = fold_constant_associativity(n, tarval_add);
2407 n = transform_node_AddSub(n);
2411 a = get_Add_left(n);
2412 b = get_Add_right(n);
2413 mode = get_irn_mode(n);
2415 if (mode_is_reference(mode)) {
2416 ir_mode *lmode = get_irn_mode(a);
2418 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2419 /* an Add(a, NULL) is a hidden Conv */
2420 dbg_info *dbg = get_irn_dbg_info(n);
2421 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2425 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2426 ir_tarval *tv = get_Const_tarval(b);
2427 ir_tarval *min = get_mode_min(mode);
2428 /* if all bits are set, then this has the same effect as a Not.
2429 * Note that the following == gives false for different modes which
2430 * is exactly what we want */
2432 dbg_info *dbgi = get_irn_dbg_info(n);
2433 ir_graph *irg = get_irn_irg(n);
2434 ir_node *block = get_nodes_block(n);
2435 ir_node *cnst = new_r_Const(irg, min);
2436 return new_rd_Eor(dbgi, block, a, cnst, mode);
2440 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2442 /* for FP the following optimizations are only allowed if
2443 * fp_strict_algebraic is disabled */
2444 if (mode_is_float(mode)) {
2445 ir_graph *irg = get_irn_irg(n);
2446 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2450 if (mode_is_num(mode)) {
2451 ir_graph *irg = get_irn_irg(n);
2452 /* the following code leads to endless recursion when Mul are replaced
2453 * by a simple instruction chain */
2454 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2455 && a == b && mode_is_int(mode)) {
2456 ir_node *block = get_nodes_block(n);
2459 get_irn_dbg_info(n),
2462 new_r_Const_long(irg, mode, 2),
2464 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2469 get_irn_dbg_info(n),
2474 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2479 get_irn_dbg_info(n),
2484 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2487 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2488 /* Here we rely on constants be on the RIGHT side */
2490 ir_node *op = get_Not_op(a);
2492 if (is_Const(b) && is_Const_one(b)) {
2494 ir_node *blk = get_nodes_block(n);
2495 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2496 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2503 if (is_Or_Eor_Add(n)) {
2504 n = transform_node_Or_(n);
2507 n = transform_node_Eor_(n);
2516 * returns -cnst or NULL if impossible
2518 static ir_node *const_negate(ir_node *cnst)
2520 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2521 dbg_info *dbgi = get_irn_dbg_info(cnst);
2522 ir_graph *irg = get_irn_irg(cnst);
2523 if (tv == tarval_bad) return NULL;
2524 return new_rd_Const(dbgi, irg, tv);
2528 * Do the AddSub optimization, then Transform
2529 * Constant folding on Phi
2530 * Sub(0,a) -> Minus(a)
2531 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2532 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2533 * Sub(Add(a, x), x) -> a
2534 * Sub(x, Add(x, a)) -> -a
2535 * Sub(x, Const) -> Add(x, -Const)
2537 static ir_node *transform_node_Sub(ir_node *n)
2543 n = transform_node_AddSub(n);
2545 a = get_Sub_left(n);
2546 b = get_Sub_right(n);
2548 mode = get_irn_mode(n);
2550 if (mode_is_int(mode)) {
2551 ir_mode *lmode = get_irn_mode(a);
2553 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2554 /* a Sub(a, NULL) is a hidden Conv */
2555 dbg_info *dbg = get_irn_dbg_info(n);
2556 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2557 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2561 if (mode == lmode &&
2562 get_mode_arithmetic(mode) == irma_twos_complement &&
2564 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2566 dbg_info *dbg = get_irn_dbg_info(n);
2567 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2568 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2574 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2576 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2577 if (mode_is_float(mode)) {
2578 ir_graph *irg = get_irn_irg(n);
2579 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2583 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2584 /* a - C -> a + (-C) */
2585 ir_node *cnst = const_negate(b);
2587 ir_node *block = get_nodes_block(n);
2588 dbg_info *dbgi = get_irn_dbg_info(n);
2590 n = new_rd_Add(dbgi, block, a, cnst, mode);
2591 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2596 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2597 dbg_info *dbg = get_irn_dbg_info(n);
2598 ir_node *block = get_nodes_block(n);
2599 ir_node *left = get_Minus_op(a);
2600 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2602 n = new_rd_Minus(dbg, block, add, mode);
2603 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2605 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2606 dbg_info *dbg = get_irn_dbg_info(n);
2607 ir_node *block = get_nodes_block(n);
2608 ir_node *right = get_Minus_op(b);
2610 n = new_rd_Add(dbg, block, a, right, mode);
2611 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2613 } else if (is_Sub(b)) {
2614 /* a - (b - c) -> a + (c - b)
2615 * -> (a - b) + c iff (b - c) is a pointer */
2616 dbg_info *s_dbg = get_irn_dbg_info(b);
2617 ir_node *s_left = get_Sub_left(b);
2618 ir_node *s_right = get_Sub_right(b);
2619 ir_mode *s_mode = get_irn_mode(b);
2620 if (mode_is_reference(s_mode)) {
2621 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2622 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2623 dbg_info *a_dbg = get_irn_dbg_info(n);
2626 s_right = new_r_Conv(lowest_block, s_right, mode);
2627 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2629 ir_node *s_block = get_nodes_block(b);
2630 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2631 dbg_info *a_dbg = get_irn_dbg_info(n);
2632 ir_node *a_block = get_nodes_block(n);
2634 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2636 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2640 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2641 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2643 get_irn_dbg_info(n),
2647 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2650 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2651 ir_node *left = get_binop_left(a);
2652 ir_node *right = get_binop_right(a);
2654 /* FIXME: Does the Conv's work only for two complement or generally? */
2656 if (mode != get_irn_mode(right)) {
2657 /* This Sub is an effective Cast */
2658 right = new_r_Conv(get_nodes_block(n), right, mode);
2661 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2663 } else if (right == b) {
2664 if (mode != get_irn_mode(left)) {
2665 /* This Sub is an effective Cast */
2666 left = new_r_Conv(get_nodes_block(n), left, mode);
2669 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2673 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2674 ir_node *left = get_binop_left(b);
2675 ir_node *right = get_binop_right(b);
2677 /* FIXME: Does the Conv's work only for two complement or generally? */
2679 ir_mode *r_mode = get_irn_mode(right);
2681 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2682 if (mode != r_mode) {
2683 /* This Sub is an effective Cast */
2684 n = new_r_Conv(get_nodes_block(n), n, mode);
2686 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2688 } else if (right == a) {
2689 ir_mode *l_mode = get_irn_mode(left);
2691 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2692 if (mode != l_mode) {
2693 /* This Sub is an effective Cast */
2694 n = new_r_Conv(get_nodes_block(n), n, mode);
2696 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2700 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2701 ir_mode *mode = get_irn_mode(a);
2703 if (mode == get_irn_mode(b)) {
2705 ir_node *op_a = get_Conv_op(a);
2706 ir_node *op_b = get_Conv_op(b);
2708 /* check if it's allowed to skip the conv */
2709 ma = get_irn_mode(op_a);
2710 mb = get_irn_mode(op_b);
2712 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2713 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2716 set_Sub_right(n, b);
2722 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2723 if (!is_reassoc_running() && is_Mul(a)) {
2724 ir_node *ma = get_Mul_left(a);
2725 ir_node *mb = get_Mul_right(a);
2728 ir_node *blk = get_nodes_block(n);
2729 ir_graph *irg = get_irn_irg(n);
2731 get_irn_dbg_info(n),
2735 get_irn_dbg_info(n),
2738 new_r_Const(irg, get_mode_one(mode)),
2741 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2743 } else if (mb == b) {
2744 ir_node *blk = get_nodes_block(n);
2745 ir_graph *irg = get_irn_irg(n);
2747 get_irn_dbg_info(n),
2751 get_irn_dbg_info(n),
2754 new_r_Const(irg, get_mode_one(mode)),
2757 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2761 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2762 ir_node *x = get_Sub_left(a);
2763 ir_node *y = get_Sub_right(a);
2764 ir_node *blk = get_nodes_block(n);
2765 ir_mode *m_b = get_irn_mode(b);
2766 ir_mode *m_y = get_irn_mode(y);
2770 /* Determine the right mode for the Add. */
2773 else if (mode_is_reference(m_b))
2775 else if (mode_is_reference(m_y))
2779 * Both modes are different but none is reference,
2780 * happens for instance in SubP(SubP(P, Iu), Is).
2781 * We have two possibilities here: Cast or ignore.
2782 * Currently we ignore this case.
2787 add = new_r_Add(blk, y, b, add_mode);
2789 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2794 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2795 /* c - ~X = X + (c+1) */
2796 if (is_Const(a) && is_Not(b)) {
2797 ir_tarval *tv = get_Const_tarval(a);
2799 tv = tarval_add(tv, get_mode_one(mode));
2800 if (tv != tarval_bad) {
2801 ir_node *blk = get_nodes_block(n);
2802 ir_graph *irg = get_irn_irg(n);
2803 ir_node *c = new_r_Const(irg, tv);
2804 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2805 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2809 /* x-(x&y) = x & ~y */
2811 ir_node *and_left = get_And_left(b);
2812 ir_node *and_right = get_And_right(b);
2813 if (and_right == a) {
2814 ir_node *tmp = and_left;
2815 and_left = and_right;
2818 if (and_left == a) {
2819 dbg_info *dbgi = get_irn_dbg_info(n);
2820 ir_node *block = get_nodes_block(n);
2821 ir_mode *mode = get_irn_mode(n);
2822 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2823 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2832 * Several transformation done on n*n=2n bits mul.
2833 * These transformations must be done here because new nodes may be produced.
2835 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2838 ir_node *a = get_Mul_left(n);
2839 ir_node *b = get_Mul_right(n);
2840 ir_tarval *ta = value_of(a);
2841 ir_tarval *tb = value_of(b);
2842 ir_mode *smode = get_irn_mode(a);
2844 if (ta == get_mode_one(smode)) {
2845 /* (L)1 * (L)b = (L)b */
2846 ir_node *blk = get_nodes_block(n);
2847 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2848 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2851 else if (ta == get_mode_minus_one(smode)) {
2852 /* (L)-1 * (L)b = (L)b */
2853 ir_node *blk = get_nodes_block(n);
2854 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2855 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2859 if (tb == get_mode_one(smode)) {
2860 /* (L)a * (L)1 = (L)a */
2861 ir_node *blk = get_nodes_block(a);
2862 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2863 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2866 else if (tb == get_mode_minus_one(smode)) {
2867 /* (L)a * (L)-1 = (L)-a */
2868 ir_node *blk = get_nodes_block(n);
2869 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2870 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2871 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2878 * Transform Mul(a,-1) into -a.
2879 * Do constant evaluation of Phi nodes.
2880 * Do architecture dependent optimizations on Mul nodes
2882 static ir_node *transform_node_Mul(ir_node *n)
2884 ir_node *c, *oldn = n;
2885 ir_mode *mode = get_irn_mode(n);
2886 ir_node *a = get_Mul_left(n);
2887 ir_node *b = get_Mul_right(n);
2889 n = fold_constant_associativity(n, tarval_mul);
2893 if (mode != get_irn_mode(a))
2894 return transform_node_Mul2n(n, mode);
2896 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2898 if (mode_is_signed(mode)) {
2901 if (value_of(a) == get_mode_minus_one(mode))
2903 else if (value_of(b) == get_mode_minus_one(mode))
2906 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2907 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2912 if (is_Const(b)) { /* (-a) * const -> a * -const */
2913 ir_node *cnst = const_negate(b);
2915 dbg_info *dbgi = get_irn_dbg_info(n);
2916 ir_node *block = get_nodes_block(n);
2917 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2918 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2921 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2922 dbg_info *dbgi = get_irn_dbg_info(n);
2923 ir_node *block = get_nodes_block(n);
2924 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2925 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2927 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2928 ir_node *sub_l = get_Sub_left(b);
2929 ir_node *sub_r = get_Sub_right(b);
2930 dbg_info *dbgi = get_irn_dbg_info(n);
2931 ir_node *block = get_nodes_block(n);
2932 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2933 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2934 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2937 } else if (is_Minus(b)) {
2938 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2939 ir_node *sub_l = get_Sub_left(a);
2940 ir_node *sub_r = get_Sub_right(a);
2941 dbg_info *dbgi = get_irn_dbg_info(n);
2942 ir_node *block = get_nodes_block(n);
2943 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2944 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2945 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2948 } else if (is_Shl(a)) {
2949 ir_node *const shl_l = get_Shl_left(a);
2950 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2951 /* (1 << x) * b -> b << x */
2952 dbg_info *const dbgi = get_irn_dbg_info(n);
2953 ir_node *const block = get_nodes_block(n);
2954 ir_node *const shl_r = get_Shl_right(a);
2955 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2956 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2959 } else if (is_Shl(b)) {
2960 ir_node *const shl_l = get_Shl_left(b);
2961 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2962 /* a * (1 << x) -> a << x */
2963 dbg_info *const dbgi = get_irn_dbg_info(n);
2964 ir_node *const block = get_nodes_block(n);
2965 ir_node *const shl_r = get_Shl_right(b);
2966 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2967 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2971 if (get_mode_arithmetic(mode) == irma_ieee754
2972 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
2974 ir_tarval *tv = get_Const_tarval(a);
2975 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2976 && !tarval_is_negative(tv)) {
2977 /* 2.0 * b = b + b */
2978 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2979 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2983 else if (is_Const(b)) {
2984 ir_tarval *tv = get_Const_tarval(b);
2985 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2986 && !tarval_is_negative(tv)) {
2987 /* a * 2.0 = a + a */
2988 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2989 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2994 return arch_dep_replace_mul_with_shifts(n);
2998 * Transform a Div Node.
3000 static ir_node *transform_node_Div(ir_node *n)
3002 ir_mode *mode = get_Div_resmode(n);
3003 ir_node *a = get_Div_left(n);
3004 ir_node *b = get_Div_right(n);
3006 const ir_node *dummy;
3008 if (mode_is_int(mode)) {
3009 if (is_Const(b) && is_const_Phi(a)) {
3010 /* check for Div(Phi, Const) */
3011 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3013 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3016 } else if (is_Const(a) && is_const_Phi(b)) {
3017 /* check for Div(Const, Phi) */
3018 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3020 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3023 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3024 /* check for Div(Phi, Phi) */
3025 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3027 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3032 if (a == b && value_not_zero(a, &dummy)) {
3033 ir_graph *irg = get_irn_irg(n);
3034 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3035 value = new_r_Const(irg, get_mode_one(mode));
3036 DBG_OPT_CSTEVAL(n, value);
3039 if (mode_is_signed(mode) && is_Const(b)) {
3040 ir_tarval *tv = get_Const_tarval(b);
3042 if (tv == get_mode_minus_one(mode)) {
3044 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3045 DBG_OPT_CSTEVAL(n, value);
3049 /* Try architecture dependent optimization */
3050 value = arch_dep_replace_div_by_const(n);
3053 assert(mode_is_float(mode));
3055 /* Optimize x/c to x*(1/c) */
3056 ir_tarval *tv = value_of(b);
3058 if (tv != tarval_bad) {
3059 tv = tarval_div(get_mode_one(mode), tv);
3061 /* Do the transformation if the result is either exact or we are
3062 not using strict rules. */
3063 if (tv != tarval_bad &&
3064 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3065 ir_node *block = get_nodes_block(n);
3066 ir_graph *irg = get_irn_irg(block);
3067 ir_node *c = new_r_Const(irg, tv);
3068 dbg_info *dbgi = get_irn_dbg_info(n);
3069 value = new_rd_Mul(dbgi, block, a, c, mode);
3081 /* Turn Div into a tuple (mem, jmp, bad, value) */
3082 mem = get_Div_mem(n);
3083 blk = get_nodes_block(n);
3084 irg = get_irn_irg(blk);
3086 /* skip a potential Pin */
3087 mem = skip_Pin(mem);
3088 turn_into_tuple(n, pn_Div_max+1);
3089 set_Tuple_pred(n, pn_Div_M, mem);
3090 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3091 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3092 set_Tuple_pred(n, pn_Div_res, value);
3098 * Transform a Mod node.
3100 static ir_node *transform_node_Mod(ir_node *n)
3102 ir_mode *mode = get_Mod_resmode(n);
3103 ir_node *a = get_Mod_left(n);
3104 ir_node *b = get_Mod_right(n);
3109 if (is_Const(b) && is_const_Phi(a)) {
3110 /* check for Div(Phi, Const) */
3111 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3113 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3117 else if (is_Const(a) && is_const_Phi(b)) {
3118 /* check for Div(Const, Phi) */
3119 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3121 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3125 else if (is_const_Phi(a) && is_const_Phi(b)) {
3126 /* check for Div(Phi, Phi) */
3127 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3129 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3136 irg = get_irn_irg(n);
3137 if (tv != tarval_bad) {
3138 value = new_r_Const(irg, tv);
3140 DBG_OPT_CSTEVAL(n, value);
3143 ir_node *a = get_Mod_left(n);
3144 ir_node *b = get_Mod_right(n);
3145 const ir_node *dummy;
3147 if (a == b && value_not_zero(a, &dummy)) {
3148 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3149 value = new_r_Const(irg, get_mode_null(mode));
3150 DBG_OPT_CSTEVAL(n, value);
3153 if (mode_is_signed(mode) && is_Const(b)) {
3154 ir_tarval *tv = get_Const_tarval(b);
3156 if (tv == get_mode_minus_one(mode)) {
3158 value = new_r_Const(irg, get_mode_null(mode));
3159 DBG_OPT_CSTEVAL(n, value);
3163 /* Try architecture dependent optimization */
3164 value = arch_dep_replace_mod_by_const(n);
3173 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3174 mem = get_Mod_mem(n);
3175 blk = get_nodes_block(n);
3176 irg = get_irn_irg(blk);
3178 /* skip a potential Pin */
3179 mem = skip_Pin(mem);
3180 turn_into_tuple(n, pn_Mod_max+1);
3181 set_Tuple_pred(n, pn_Mod_M, mem);
3182 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3183 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3184 set_Tuple_pred(n, pn_Mod_res, value);
3190 * Transform a Cond node.
3192 * Replace the Cond by a Jmp if it branches on a constant
3195 static ir_node *transform_node_Cond(ir_node *n)
3197 ir_node *a = get_Cond_selector(n);
3198 ir_graph *irg = get_irn_irg(n);
3202 /* we need block info which is not available in floating irgs */
3203 if (get_irg_pinned(irg) == op_pin_state_floats)
3207 if (ta == tarval_bad && is_Cmp(a)) {
3208 /* try again with a direct call to compute_cmp, as we don't care
3209 * about the MODEB_LOWERED flag here */
3210 ta = compute_cmp_ext(a);
3213 if (ta != tarval_bad) {
3214 /* It's branching on a boolean constant.
3215 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3216 ir_node *blk = get_nodes_block(n);
3217 jmp = new_r_Jmp(blk);
3218 turn_into_tuple(n, pn_Cond_max+1);
3219 if (ta == tarval_b_true) {
3220 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3221 set_Tuple_pred(n, pn_Cond_true, jmp);
3223 set_Tuple_pred(n, pn_Cond_false, jmp);
3224 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3226 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3231 static ir_node *transform_node_Switch(ir_node *n)
3233 ir_node *op = get_Switch_selector(n);
3234 ir_tarval *val = value_of(op);
3235 if (val != tarval_bad) {
3236 dbg_info *dbgi = get_irn_dbg_info(n);
3237 ir_graph *irg = get_irn_irg(n);
3238 unsigned n_outs = get_Switch_n_outs(n);
3239 ir_node *block = get_nodes_block(n);
3240 ir_node *bad = new_r_Bad(irg, mode_X);
3241 ir_node **in = XMALLOCN(ir_node*, n_outs);
3242 const ir_switch_table *table = get_Switch_table(n);
3243 size_t n_entries = ir_switch_table_get_n_entries(table);
3247 for (i = 0; i < n_entries; ++i) {
3248 const ir_switch_table_entry *entry
3249 = ir_switch_table_get_entry_const(table, i);
3250 ir_tarval *min = entry->min;
3251 ir_tarval *max = entry->max;
3254 if ((min == max && min == val)
3255 || (tarval_cmp(val, min) != ir_relation_less
3256 && tarval_cmp(val, max) != ir_relation_greater)) {
3261 for (o = 0; o < n_outs; ++o) {
3262 if (o == (unsigned)jmp_pn) {
3263 in[o] = new_rd_Jmp(dbgi, block);
3268 return new_r_Tuple(block, (int)n_outs, in);
3274 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3276 * - and, or, xor instead of &
3277 * - Shl, Shr, Shrs, rotl instead of >>
3278 * (with a special case for Or/Xor + Shrs)
3280 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3282 static ir_node *transform_node_shift_bitop(ir_node *n)
3284 ir_graph *irg = get_irn_irg(n);
3285 ir_node *right = get_binop_right(n);
3286 ir_mode *mode = get_irn_mode(n);
3288 ir_node *bitop_left;
3289 ir_node *bitop_right;
3298 ir_tarval *tv_shift;
3300 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3303 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3305 if (!is_Const(right))
3308 left = get_binop_left(n);
3309 op_left = get_irn_op(left);
3310 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3313 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3314 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3315 /* TODO: test if sign bit is affectes */
3319 bitop_right = get_binop_right(left);
3320 if (!is_Const(bitop_right))
3323 bitop_left = get_binop_left(left);
3325 block = get_nodes_block(n);
3326 dbgi = get_irn_dbg_info(n);
3327 tv1 = get_Const_tarval(bitop_right);
3328 tv2 = get_Const_tarval(right);
3330 assert(get_tarval_mode(tv1) == mode);
3333 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3334 tv_shift = tarval_shl(tv1, tv2);
3335 } else if (is_Shr(n)) {
3336 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3337 tv_shift = tarval_shr(tv1, tv2);
3338 } else if (is_Shrs(n)) {
3339 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3340 tv_shift = tarval_shrs(tv1, tv2);
3343 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3344 tv_shift = tarval_rotl(tv1, tv2);
3347 assert(get_tarval_mode(tv_shift) == mode);
3348 irg = get_irn_irg(n);
3349 new_const = new_r_Const(irg, tv_shift);
3351 if (op_left == op_And) {
3352 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3353 } else if (op_left == op_Or) {
3354 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3356 assert(op_left == op_Eor);
3357 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3366 static ir_node *transform_node_And(ir_node *n)
3368 ir_node *c, *oldn = n;
3369 ir_node *a = get_And_left(n);
3370 ir_node *b = get_And_right(n);
3373 n = fold_constant_associativity(n, tarval_and);
3377 if (is_Cmp(a) && is_Cmp(b)) {
3378 ir_node *a_left = get_Cmp_left(a);
3379 ir_node *a_right = get_Cmp_right(a);
3380 ir_node *b_left = get_Cmp_left(b);
3381 ir_node *b_right = get_Cmp_right(b);
3382 ir_relation a_relation = get_Cmp_relation(a);
3383 ir_relation b_relation = get_Cmp_relation(b);
3384 /* we can combine the relations of two compares with the same
3386 if (a_left == b_left && b_left == b_right) {
3387 dbg_info *dbgi = get_irn_dbg_info(n);
3388 ir_node *block = get_nodes_block(n);
3389 ir_relation new_relation = a_relation & b_relation;
3390 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3392 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3393 if (a_relation == b_relation && a_relation == ir_relation_equal
3394 && !mode_is_float(get_irn_mode(a_left))
3395 && !mode_is_float(get_irn_mode(b_left))) {
3396 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3397 dbg_info *dbgi = get_irn_dbg_info(n);
3398 ir_node *block = get_nodes_block(n);
3399 ir_mode *a_mode = get_irn_mode(a_left);
3400 ir_mode *b_mode = get_irn_mode(b_left);
3401 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3402 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3403 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3404 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3405 ir_graph *irg = get_irn_irg(n);
3406 ir_node *zero = create_zero_const(irg, b_mode);
3407 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3409 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3410 dbg_info *dbgi = get_irn_dbg_info(n);
3411 ir_node *block = get_nodes_block(n);
3412 ir_mode *a_mode = get_irn_mode(a_left);
3413 ir_mode *b_mode = get_irn_mode(b_left);
3414 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3415 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3416 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3417 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3418 ir_graph *irg = get_irn_irg(n);
3419 ir_node *zero = create_zero_const(irg, a_mode);
3420 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3425 mode = get_irn_mode(n);
3426 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3428 if (is_Or(a) || is_Or_Eor_Add(a)) {
3429 ir_node *or_left = get_binop_left(a);
3430 ir_node *or_right = get_binop_right(a);
3431 if (complement_values(or_left, b)) {
3432 /* (a|b) & ~a => b & ~a */
3433 dbg_info *dbgi = get_irn_dbg_info(n);
3434 ir_node *block = get_nodes_block(n);
3435 return new_rd_And(dbgi, block, or_right, b, mode);
3436 } else if (complement_values(or_right, b)) {
3437 /* (a|b) & ~b => a & ~b */
3438 dbg_info *dbgi = get_irn_dbg_info(n);
3439 ir_node *block = get_nodes_block(n);
3440 return new_rd_And(dbgi, block, or_left, b, mode);
3441 } else if (is_Not(b)) {
3442 ir_node *op = get_Not_op(b);
3444 ir_node *ba = get_And_left(op);
3445 ir_node *bb = get_And_right(op);
3447 /* it's enough to test the following cases due to normalization! */
3448 if (or_left == ba && or_right == bb) {
3449 /* (a|b) & ~(a&b) = a^b */
3450 ir_node *block = get_nodes_block(n);
3452 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3453 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3459 if (is_Or(b) || is_Or_Eor_Add(b)) {
3460 ir_node *or_left = get_binop_left(b);
3461 ir_node *or_right = get_binop_right(b);
3462 if (complement_values(or_left, a)) {
3463 /* (a|b) & ~a => b & ~a */
3464 dbg_info *dbgi = get_irn_dbg_info(n);
3465 ir_node *block = get_nodes_block(n);
3466 return new_rd_And(dbgi, block, or_right, a, mode);
3467 } else if (complement_values(or_right, a)) {
3468 /* (a|b) & ~b => a & ~b */
3469 dbg_info *dbgi = get_irn_dbg_info(n);
3470 ir_node *block = get_nodes_block(n);
3471 return new_rd_And(dbgi, block, or_left, a, mode);
3472 } else if (is_Not(a)) {
3473 ir_node *op = get_Not_op(a);
3475 ir_node *aa = get_And_left(op);
3476 ir_node *ab = get_And_right(op);
3478 /* it's enough to test the following cases due to normalization! */
3479 if (or_left == aa && or_right == ab) {
3480 /* (a|b) & ~(a&b) = a^b */
3481 ir_node *block = get_nodes_block(n);
3483 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3484 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3490 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3491 ir_node *al = get_binop_left(a);
3492 ir_node *ar = get_binop_right(a);
3495 /* (b ^ a) & b -> ~a & b */
3496 dbg_info *dbg = get_irn_dbg_info(n);
3497 ir_node *block = get_nodes_block(n);
3499 ar = new_rd_Not(dbg, block, ar, mode);
3500 n = new_rd_And(dbg, block, ar, b, mode);
3501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3505 /* (a ^ b) & b -> ~a & b */
3506 dbg_info *dbg = get_irn_dbg_info(n);
3507 ir_node *block = get_nodes_block(n);
3509 al = new_rd_Not(dbg, block, al, mode);
3510 n = new_rd_And(dbg, block, al, b, mode);
3511 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3515 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3516 ir_node *bl = get_binop_left(b);
3517 ir_node *br = get_binop_right(b);
3520 /* a & (a ^ b) -> a & ~b */
3521 dbg_info *dbg = get_irn_dbg_info(n);
3522 ir_node *block = get_nodes_block(n);
3524 br = new_rd_Not(dbg, block, br, mode);
3525 n = new_rd_And(dbg, block, br, a, mode);
3526 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3530 /* a & (b ^ a) -> a & ~b */
3531 dbg_info *dbg = get_irn_dbg_info(n);
3532 ir_node *block = get_nodes_block(n);
3534 bl = new_rd_Not(dbg, block, bl, mode);
3535 n = new_rd_And(dbg, block, bl, a, mode);
3536 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3540 if (is_Not(a) && is_Not(b)) {
3541 /* ~a & ~b = ~(a|b) */
3542 ir_node *block = get_nodes_block(n);
3543 ir_mode *mode = get_irn_mode(n);
3547 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3548 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3549 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3554 vrp_attr *b_vrp = vrp_get_info(b);
3555 ir_tarval *a_val = get_Const_tarval(a);
3556 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3562 vrp_attr *a_vrp = vrp_get_info(a);
3563 ir_tarval *b_val = get_Const_tarval(b);
3564 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3569 n = transform_bitwise_distributive(n, transform_node_And);
3571 n = transform_node_bitop_shift(n);
3579 static ir_node *transform_node_Not(ir_node *n)
3581 ir_node *c, *oldn = n;
3582 ir_node *a = get_Not_op(n);
3583 ir_mode *mode = get_irn_mode(n);
3585 HANDLE_UNOP_PHI(tarval_not,a,c);
3587 /* check for a boolean Not */
3589 dbg_info *dbgi = get_irn_dbg_info(a);
3590 ir_node *block = get_nodes_block(a);
3591 ir_relation relation = get_Cmp_relation(a);
3592 relation = get_negated_relation(relation);
3593 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3594 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3598 /* normalize ~(a ^ b) => a ^ ~b */
3599 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3600 dbg_info *dbg = get_irn_dbg_info(n);
3601 ir_node *block = get_nodes_block(n);
3602 ir_node *eor_right = get_binop_right(a);
3603 ir_node *eor_left = get_binop_left(a);
3604 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3605 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3609 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3610 if (is_Minus(a)) { /* ~-x -> x + -1 */
3611 dbg_info *dbg = get_irn_dbg_info(n);
3612 ir_graph *irg = get_irn_irg(n);
3613 ir_node *block = get_nodes_block(n);
3614 ir_node *add_l = get_Minus_op(a);
3615 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3616 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3617 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3618 ir_node *add_r = get_binop_right(a);
3619 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3620 /* ~(x + -1) = -x */
3621 ir_node *op = get_binop_left(a);
3622 ir_node *blk = get_nodes_block(n);
3623 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3624 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3632 * Transform a Minus.
3636 * -(a >>u (size-1)) = a >>s (size-1)
3637 * -(a >>s (size-1)) = a >>u (size-1)
3638 * -(a * const) -> a * -const
3640 static ir_node *transform_node_Minus(ir_node *n)
3642 ir_node *c, *oldn = n;
3643 ir_node *a = get_Minus_op(n);
3646 HANDLE_UNOP_PHI(tarval_neg,a,c);
3648 mode = get_irn_mode(a);
3649 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3650 /* the following rules are only to twos-complement */
3653 ir_node *op = get_Not_op(a);
3654 ir_tarval *tv = get_mode_one(mode);
3655 ir_node *blk = get_nodes_block(n);
3656 ir_graph *irg = get_irn_irg(blk);
3657 ir_node *c = new_r_Const(irg, tv);
3658 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3659 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3663 ir_node *c = get_Shr_right(a);
3666 ir_tarval *tv = get_Const_tarval(c);
3668 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3669 /* -(a >>u (size-1)) = a >>s (size-1) */
3670 ir_node *v = get_Shr_left(a);
3672 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3673 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3679 ir_node *c = get_Shrs_right(a);
3682 ir_tarval *tv = get_Const_tarval(c);
3684 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3685 /* -(a >>s (size-1)) = a >>u (size-1) */
3686 ir_node *v = get_Shrs_left(a);
3688 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3689 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3696 /* - (a-b) = b - a */
3697 ir_node *la = get_Sub_left(a);
3698 ir_node *ra = get_Sub_right(a);
3699 ir_node *blk = get_nodes_block(n);
3701 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3702 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3706 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3707 ir_node *mul_l = get_Mul_left(a);
3708 ir_node *mul_r = get_Mul_right(a);
3709 ir_tarval *tv = value_of(mul_r);
3710 if (tv != tarval_bad) {
3711 tv = tarval_neg(tv);
3712 if (tv != tarval_bad) {
3713 ir_graph *irg = get_irn_irg(n);
3714 ir_node *cnst = new_r_Const(irg, tv);
3715 dbg_info *dbg = get_irn_dbg_info(a);
3716 ir_node *block = get_nodes_block(a);
3717 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3718 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3728 * Transform a Proj(Load) with a non-null address.
3730 static ir_node *transform_node_Proj_Load(ir_node *proj)
3732 if (get_irn_mode(proj) == mode_X) {
3733 ir_node *load = get_Proj_pred(proj);
3735 /* get the Load address */
3736 const ir_node *addr = get_Load_ptr(load);
3737 const ir_node *confirm;
3739 if (value_not_null(addr, &confirm)) {
3740 if (confirm == NULL) {
3741 /* this node may float if it did not depend on a Confirm */
3742 set_irn_pinned(load, op_pin_state_floats);
3744 if (get_Proj_proj(proj) == pn_Load_X_except) {
3745 ir_graph *irg = get_irn_irg(proj);
3746 DBG_OPT_EXC_REM(proj);
3747 return new_r_Bad(irg, mode_X);
3749 ir_node *blk = get_nodes_block(load);
3750 return new_r_Jmp(blk);
3758 * Transform a Proj(Store) with a non-null address.
3760 static ir_node *transform_node_Proj_Store(ir_node *proj)
3762 if (get_irn_mode(proj) == mode_X) {
3763 ir_node *store = get_Proj_pred(proj);
3765 /* get the load/store address */
3766 const ir_node *addr = get_Store_ptr(store);
3767 const ir_node *confirm;
3769 if (value_not_null(addr, &confirm)) {
3770 if (confirm == NULL) {
3771 /* this node may float if it did not depend on a Confirm */
3772 set_irn_pinned(store, op_pin_state_floats);
3774 if (get_Proj_proj(proj) == pn_Store_X_except) {
3775 ir_graph *irg = get_irn_irg(proj);
3776 DBG_OPT_EXC_REM(proj);
3777 return new_r_Bad(irg, mode_X);
3779 ir_node *blk = get_nodes_block(store);
3780 return new_r_Jmp(blk);
3788 * Transform a Proj(Div) with a non-zero value.
3789 * Removes the exceptions and routes the memory to the NoMem node.
3791 static ir_node *transform_node_Proj_Div(ir_node *proj)
3793 ir_node *div = get_Proj_pred(proj);
3794 ir_node *b = get_Div_right(div);
3795 ir_node *res, *new_mem;
3796 const ir_node *confirm;
3799 if (value_not_zero(b, &confirm)) {
3800 /* div(x, y) && y != 0 */
3801 if (confirm == NULL) {
3802 /* we are sure we have a Const != 0 */
3803 new_mem = get_Div_mem(div);
3804 new_mem = skip_Pin(new_mem);
3805 set_Div_mem(div, new_mem);
3806 set_irn_pinned(div, op_pin_state_floats);
3809 proj_nr = get_Proj_proj(proj);
3811 case pn_Div_X_regular:
3812 return new_r_Jmp(get_nodes_block(div));
3814 case pn_Div_X_except: {
3815 ir_graph *irg = get_irn_irg(proj);
3816 /* we found an exception handler, remove it */
3817 DBG_OPT_EXC_REM(proj);
3818 return new_r_Bad(irg, mode_X);
3822 ir_graph *irg = get_irn_irg(proj);
3823 res = get_Div_mem(div);
3824 new_mem = get_irg_no_mem(irg);
3827 /* This node can only float up to the Confirm block */
3828 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3830 set_irn_pinned(div, op_pin_state_floats);
3831 /* this is a Div without exception, we can remove the memory edge */
3832 set_Div_mem(div, new_mem);
3841 * Transform a Proj(Mod) with a non-zero value.
3842 * Removes the exceptions and routes the memory to the NoMem node.
3844 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3846 ir_node *mod = get_Proj_pred(proj);
3847 ir_node *b = get_Mod_right(mod);
3848 ir_node *res, *new_mem;
3849 const ir_node *confirm;
3852 if (value_not_zero(b, &confirm)) {
3853 /* mod(x, y) && y != 0 */
3854 proj_nr = get_Proj_proj(proj);
3856 if (confirm == NULL) {
3857 /* we are sure we have a Const != 0 */
3858 new_mem = get_Mod_mem(mod);
3859 new_mem = skip_Pin(new_mem);
3860 set_Mod_mem(mod, new_mem);
3861 set_irn_pinned(mod, op_pin_state_floats);
3866 case pn_Mod_X_regular:
3867 return new_r_Jmp(get_nodes_block(mod));
3869 case pn_Mod_X_except: {
3870 ir_graph *irg = get_irn_irg(proj);
3871 /* we found an exception handler, remove it */
3872 DBG_OPT_EXC_REM(proj);
3873 return new_r_Bad(irg, mode_X);
3877 ir_graph *irg = get_irn_irg(proj);
3878 res = get_Mod_mem(mod);
3879 new_mem = get_irg_no_mem(irg);
3882 /* This node can only float up to the Confirm block */
3883 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3885 /* this is a Mod without exception, we can remove the memory edge */
3886 set_Mod_mem(mod, new_mem);
3890 if (get_Mod_left(mod) == b) {
3891 /* a % a = 0 if a != 0 */
3892 ir_graph *irg = get_irn_irg(proj);
3893 ir_mode *mode = get_irn_mode(proj);
3894 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3896 DBG_OPT_CSTEVAL(mod, res);
3905 * return true if the operation returns a value with exactly 1 bit set
3907 static bool is_single_bit(const ir_node *node)
3909 /* a first implementation, could be extended with vrp and others... */
3911 ir_node *shl_l = get_Shl_left(node);
3912 ir_mode *mode = get_irn_mode(node);
3913 int modulo = get_mode_modulo_shift(mode);
3914 /* this works if we shift a 1 and we have modulo shift */
3915 if (is_Const(shl_l) && is_Const_one(shl_l)
3916 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3919 } else if (is_Const(node)) {
3920 ir_tarval *tv = get_Const_tarval(node);
3921 return tarval_is_single_bit(tv);
3927 * checks if node just flips a bit in another node and returns that other node
3928 * if so. @p tv should be a value having just 1 bit set
3930 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
3933 return get_Not_op(node);
3935 ir_node *right = get_Eor_right(node);
3936 if (is_Const(right)) {
3937 ir_tarval *right_tv = get_Const_tarval(right);
3938 ir_mode *mode = get_irn_mode(node);
3939 if (tarval_and(right_tv, tv) != get_mode_null(mode))
3940 return get_Eor_left(node);
3947 * Normalizes and optimizes Cmp nodes.
3949 static ir_node *transform_node_Cmp(ir_node *n)
3951 ir_node *left = get_Cmp_left(n);
3952 ir_node *right = get_Cmp_right(n);
3953 ir_mode *mode = get_irn_mode(left);
3954 ir_tarval *tv = NULL;
3955 bool changed = false;
3956 bool changedc = false;
3957 ir_relation relation = get_Cmp_relation(n);
3958 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3960 /* mask out impossible relations */
3961 ir_relation new_relation = relation & possible;
3962 if (new_relation != relation) {
3963 relation = new_relation;
3967 /* Remove unnecessary conversions */
3968 if (!mode_is_float(mode)
3969 || be_get_backend_param()->mode_float_arithmetic == NULL) {
3970 if (is_Conv(left) && is_Conv(right)) {
3971 ir_node *op_left = get_Conv_op(left);
3972 ir_node *op_right = get_Conv_op(right);
3973 ir_mode *mode_left = get_irn_mode(op_left);
3974 ir_mode *mode_right = get_irn_mode(op_right);
3976 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3977 && mode_left != mode_b && mode_right != mode_b) {
3978 ir_node *block = get_nodes_block(n);
3980 if (mode_left == mode_right) {
3984 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3985 } else if (smaller_mode(mode_left, mode_right)) {
3986 left = new_r_Conv(block, op_left, mode_right);
3989 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3990 } else if (smaller_mode(mode_right, mode_left)) {
3992 right = new_r_Conv(block, op_right, mode_left);
3994 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3996 mode = get_irn_mode(left);
3999 if (is_Conv(left) && is_Const(right)) {
4000 ir_node *op_left = get_Conv_op(left);
4001 ir_mode *mode_left = get_irn_mode(op_left);
4002 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4003 ir_tarval *tv = get_Const_tarval(right);
4004 tarval_int_overflow_mode_t last_mode
4005 = tarval_get_integer_overflow_mode();
4007 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4008 new_tv = tarval_convert_to(tv, mode_left);
4009 tarval_set_integer_overflow_mode(last_mode);
4010 if (new_tv != tarval_bad) {
4011 ir_graph *irg = get_irn_irg(n);
4013 right = new_r_Const(irg, new_tv);
4014 mode = get_irn_mode(left);
4016 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4023 * Optimize -a CMP -b into b CMP a.
4024 * This works only for modes where unary Minus cannot Overflow.
4025 * Note that two-complement integers can Overflow so it will NOT work.
4027 if (!mode_overflow_on_unary_Minus(mode) &&
4028 is_Minus(left) && is_Minus(right)) {
4029 left = get_Minus_op(left);
4030 right = get_Minus_op(right);
4031 relation = get_inversed_relation(relation);
4033 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4036 /* remove operation on both sides if possible */
4037 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4039 * The following operations are NOT safe for floating point operations, for instance
4040 * 1.0 + inf == 2.0 + inf, =/=> x == y
4042 if (mode_is_int(mode)) {
4043 unsigned lop = get_irn_opcode(left);
4045 if (lop == get_irn_opcode(right)) {
4046 ir_node *ll, *lr, *rl, *rr;
4048 /* same operation on both sides, try to remove */
4052 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4053 left = get_unop_op(left);
4054 right = get_unop_op(right);
4056 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4059 ll = get_Add_left(left);
4060 lr = get_Add_right(left);
4061 rl = get_Add_left(right);
4062 rr = get_Add_right(right);
4065 /* X + a CMP X + b ==> a CMP b */
4069 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4070 } else if (ll == rr) {
4071 /* X + a CMP b + X ==> a CMP b */
4075 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4076 } else if (lr == rl) {
4077 /* a + X CMP X + b ==> a CMP b */
4081 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4082 } else if (lr == rr) {
4083 /* a + X CMP b + X ==> a CMP b */
4087 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4091 ll = get_Sub_left(left);
4092 lr = get_Sub_right(left);
4093 rl = get_Sub_left(right);
4094 rr = get_Sub_right(right);
4097 /* X - a CMP X - b ==> a CMP b */
4101 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4102 } else if (lr == rr) {
4103 /* a - X CMP b - X ==> a CMP b */
4107 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4111 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4112 /* a ROTL X CMP b ROTL X ==> a CMP b */
4113 left = get_Rotl_left(left);
4114 right = get_Rotl_left(right);
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4124 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4125 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4126 ir_node *ll = get_binop_left(left);
4127 ir_node *lr = get_binop_right(left);
4129 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4135 ir_graph *irg = get_irn_irg(n);
4137 right = create_zero_const(irg, mode);
4139 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4142 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4143 ir_node *rl = get_binop_left(right);
4144 ir_node *rr = get_binop_right(right);
4146 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4152 ir_graph *irg = get_irn_irg(n);
4154 right = create_zero_const(irg, mode);
4156 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4160 if (is_And(left) && is_Const(right)) {
4161 ir_node *ll = get_binop_left(left);
4162 ir_node *lr = get_binop_right(left);
4163 if (is_Shr(ll) && is_Const(lr)) {
4164 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4165 ir_node *block = get_nodes_block(n);
4166 ir_mode *mode = get_irn_mode(left);
4168 ir_node *llr = get_Shr_right(ll);
4169 if (is_Const(llr)) {
4170 dbg_info *dbg = get_irn_dbg_info(left);
4171 ir_graph *irg = get_irn_irg(left);
4173 ir_tarval *c1 = get_Const_tarval(llr);
4174 ir_tarval *c2 = get_Const_tarval(lr);
4175 ir_tarval *c3 = get_Const_tarval(right);
4176 ir_tarval *mask = tarval_shl(c2, c1);
4177 ir_tarval *value = tarval_shl(c3, c1);
4179 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4180 right = new_r_Const(irg, value);
4185 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4187 if (is_Const(right) && is_Const_null(right) &&
4188 (is_Eor(left) || is_Or_Eor_Add(left))) {
4189 right = get_Eor_right(left);
4190 left = get_Eor_left(left);
4196 if (mode_is_int(mode) && is_And(left)) {
4197 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4198 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4199 if (relation == ir_relation_equal
4200 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4201 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4202 ir_node *and0 = get_And_left(left);
4203 ir_node *and1 = get_And_right(left);
4204 if (and1 == right) {
4205 ir_node *tmp = and0;
4209 if (and0 == right && is_single_bit(and0)) {
4210 ir_graph *irg = get_irn_irg(n);
4212 relation == ir_relation_equal ? ir_relation_less_greater
4213 : ir_relation_equal;
4214 right = create_zero_const(irg, mode);
4220 if (is_Const(right) && is_Const_null(right) &&
4221 (relation == ir_relation_equal
4222 || (relation == ir_relation_less_greater)
4223 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4225 /* instead of flipping the bit before the bit-test operation negate
4227 ir_node *and0 = get_And_left(left);
4228 ir_node *and1 = get_And_right(left);
4229 if (is_Const(and1)) {
4230 ir_tarval *tv = get_Const_tarval(and1);
4231 if (tarval_is_single_bit(tv)) {
4232 ir_node *flipped = flips_bit(and0, tv);
4233 if (flipped != NULL) {
4234 dbg_info *dbgi = get_irn_dbg_info(left);
4235 ir_node *block = get_nodes_block(left);
4236 relation = get_negated_relation(relation);
4237 left = new_rd_And(dbgi, block, flipped, and1, mode);
4246 /* replace mode_b compares with ands/ors */
4247 if (mode == mode_b) {
4248 ir_node *block = get_nodes_block(n);
4252 case ir_relation_less_equal:
4253 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4255 case ir_relation_less:
4256 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4258 case ir_relation_greater_equal:
4259 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4261 case ir_relation_greater:
4262 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4264 case ir_relation_less_greater:
4265 bres = new_r_Eor(block, left, right, mode_b);
4267 case ir_relation_equal:
4268 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4271 #ifdef DEBUG_libfirm
4272 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4277 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4283 * First step: normalize the compare op
4284 * by placing the constant on the right side
4285 * or moving the lower address node to the left.
4287 if (!operands_are_normalized(left, right)) {
4292 relation = get_inversed_relation(relation);
4297 * Second step: Try to reduce the magnitude
4298 * of a constant. This may help to generate better code
4299 * later and may help to normalize more compares.
4300 * Of course this is only possible for integer values.
4302 tv = value_of(right);
4303 if (tv != tarval_bad) {
4304 ir_mode *mode = get_irn_mode(right);
4306 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4307 * cmp(ct,c2) | cmp(cf,c2) | result
4308 * -----------|------------|--------
4309 * true | true | True
4310 * false | false | False
4312 * false | true | not(x)
4315 ir_node *mux_true = get_Mux_true(left);
4316 ir_node *mux_false = get_Mux_false(left);
4317 if (is_Const(mux_true) && is_Const(mux_false)) {
4318 /* we can fold true/false constant separately */
4319 ir_tarval *tv_true = get_Const_tarval(mux_true);
4320 ir_tarval *tv_false = get_Const_tarval(mux_false);
4321 ir_relation r_true = tarval_cmp(tv_true, tv);
4322 ir_relation r_false = tarval_cmp(tv_false, tv);
4323 if (r_true != ir_relation_false
4324 || r_false != ir_relation_false) {
4325 bool rel_true = (r_true & relation) != 0;
4326 bool rel_false = (r_false & relation) != 0;
4327 ir_node *cond = get_Mux_sel(left);
4328 if (rel_true == rel_false) {
4329 relation = rel_true ? ir_relation_true
4330 : ir_relation_false;
4331 } else if (rel_true) {
4334 dbg_info *dbgi = get_irn_dbg_info(n);
4335 ir_node *block = get_nodes_block(n);
4336 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4343 /* TODO extend to arbitrary constants */
4344 if (is_Conv(left) && tarval_is_null(tv)) {
4345 ir_node *op = get_Conv_op(left);
4346 ir_mode *op_mode = get_irn_mode(op);
4349 * UpConv(x) REL 0 ==> x REL 0
4350 * Don't do this for float values as it's unclear whether it is a
4351 * win. (on the other side it makes detection/creation of fabs hard)
4353 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4354 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4355 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4356 !mode_is_float(mode)) {
4357 tv = get_mode_null(op_mode);
4361 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4365 if (tv != tarval_bad) {
4366 /* the following optimization is possible on modes without Overflow
4367 * on Unary Minus or on == and !=:
4368 * -a CMP c ==> a swap(CMP) -c
4370 * Beware: for two-complement Overflow may occur, so only == and != can
4371 * be optimized, see this:
4372 * -MININT < 0 =/=> MININT > 0 !!!
4374 if (is_Minus(left) &&
4375 (!mode_overflow_on_unary_Minus(mode) ||
4376 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4377 tv = tarval_neg(tv);
4379 if (tv != tarval_bad) {
4380 left = get_Minus_op(left);
4381 relation = get_inversed_relation(relation);
4383 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4385 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4386 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4387 tv = tarval_not(tv);
4389 if (tv != tarval_bad) {
4390 left = get_Not_op(left);
4392 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4396 /* for integer modes, we have more */
4397 if (mode_is_int(mode) && !is_Const(left)) {
4398 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4399 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4400 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4401 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4403 if (tv != tarval_bad) {
4404 relation ^= ir_relation_equal;
4406 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4409 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4410 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4411 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4412 tv = tarval_add(tv, get_mode_one(mode));
4414 if (tv != tarval_bad) {
4415 relation ^= ir_relation_equal;
4417 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4421 /* the following reassociations work only for == and != */
4422 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4423 if (tv != tarval_bad) {
4424 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4426 ir_node *c1 = get_Sub_right(left);
4427 ir_tarval *tv2 = value_of(c1);
4429 if (tv2 != tarval_bad) {
4430 tv2 = tarval_add(tv, value_of(c1));
4432 if (tv2 != tarval_bad) {
4433 left = get_Sub_left(left);
4436 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4440 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4441 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4442 ir_node *a_l = get_binop_left(left);
4443 ir_node *a_r = get_binop_right(left);
4447 if (is_Const(a_l)) {
4449 tv2 = value_of(a_l);
4452 tv2 = value_of(a_r);
4455 if (tv2 != tarval_bad) {
4456 tv2 = tarval_sub(tv, tv2, NULL);
4458 if (tv2 != tarval_bad) {
4462 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4466 /* -a == c ==> a == -c, -a != c ==> a != -c */
4467 else if (is_Minus(left)) {
4468 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4470 if (tv2 != tarval_bad) {
4471 left = get_Minus_op(left);
4474 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4481 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4482 switch (get_irn_opcode(left)) {
4486 c1 = get_And_right(left);
4489 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4490 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4492 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4494 /* TODO: move to constant evaluation */
4495 ir_graph *irg = get_irn_irg(n);
4496 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4497 c1 = new_r_Const(irg, tv);
4498 DBG_OPT_CSTEVAL(n, c1);
4502 if (tarval_is_single_bit(tv)) {
4504 * optimization for AND:
4506 * And(x, C) == C ==> And(x, C) != 0
4507 * And(x, C) != C ==> And(X, C) == 0
4509 * if C is a single Bit constant.
4512 /* check for Constant's match. We have check hare the tarvals,
4513 because our const might be changed */
4514 if (get_Const_tarval(c1) == tv) {
4515 /* fine: do the transformation */
4516 tv = get_mode_null(get_tarval_mode(tv));
4517 relation ^= ir_relation_less_equal_greater;
4519 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4525 c1 = get_Or_right(left);
4526 if (is_Const(c1) && tarval_is_null(tv)) {
4528 * Or(x, C) == 0 && C != 0 ==> FALSE
4529 * Or(x, C) != 0 && C != 0 ==> TRUE
4531 if (! tarval_is_null(get_Const_tarval(c1))) {
4532 /* TODO: move to constant evaluation */
4533 ir_graph *irg = get_irn_irg(n);
4534 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4535 c1 = new_r_Const(irg, tv);
4536 DBG_OPT_CSTEVAL(n, c1);
4543 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4545 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4548 c1 = get_Shl_right(left);
4550 ir_graph *irg = get_irn_irg(c1);
4551 ir_tarval *tv1 = get_Const_tarval(c1);
4552 ir_mode *mode = get_irn_mode(left);
4553 ir_tarval *minus1 = get_mode_all_one(mode);
4554 ir_tarval *amask = tarval_shr(minus1, tv1);
4555 ir_tarval *cmask = tarval_shl(minus1, tv1);
4558 if (tarval_and(tv, cmask) != tv) {
4559 /* condition not met */
4560 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4561 c1 = new_r_Const(irg, tv);
4562 DBG_OPT_CSTEVAL(n, c1);
4565 sl = get_Shl_left(left);
4566 blk = get_nodes_block(n);
4567 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4568 tv = tarval_shr(tv, tv1);
4570 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4575 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4577 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4580 c1 = get_Shr_right(left);
4582 ir_graph *irg = get_irn_irg(c1);
4583 ir_tarval *tv1 = get_Const_tarval(c1);
4584 ir_mode *mode = get_irn_mode(left);
4585 ir_tarval *minus1 = get_mode_all_one(mode);
4586 ir_tarval *amask = tarval_shl(minus1, tv1);
4587 ir_tarval *cmask = tarval_shr(minus1, tv1);
4590 if (tarval_and(tv, cmask) != tv) {
4591 /* condition not met */
4592 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4593 c1 = new_r_Const(irg, tv);
4594 DBG_OPT_CSTEVAL(n, c1);
4597 sl = get_Shr_left(left);
4598 blk = get_nodes_block(n);
4599 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4600 tv = tarval_shl(tv, tv1);
4602 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4607 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4609 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4612 c1 = get_Shrs_right(left);
4614 ir_graph *irg = get_irn_irg(c1);
4615 ir_tarval *tv1 = get_Const_tarval(c1);
4616 ir_mode *mode = get_irn_mode(left);
4617 ir_tarval *minus1 = get_mode_all_one(mode);
4618 ir_tarval *amask = tarval_shl(minus1, tv1);
4619 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4622 cond = tarval_sub(cond, tv1, NULL);
4623 cond = tarval_shrs(tv, cond);
4625 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4626 /* condition not met */
4627 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4628 c1 = new_r_Const(irg, tv);
4629 DBG_OPT_CSTEVAL(n, c1);
4632 sl = get_Shrs_left(left);
4633 blk = get_nodes_block(n);
4634 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4635 tv = tarval_shl(tv, tv1);
4637 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4645 if (changedc) { /* need a new Const */
4646 ir_graph *irg = get_irn_irg(n);
4647 right = new_r_Const(irg, tv);
4651 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4652 ir_node *op = get_Proj_pred(left);
4654 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4655 ir_node *c = get_binop_right(op);
4658 ir_tarval *tv = get_Const_tarval(c);
4660 if (tarval_is_single_bit(tv)) {
4661 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4662 ir_node *v = get_binop_left(op);
4663 ir_node *blk = get_nodes_block(op);
4664 ir_graph *irg = get_irn_irg(op);
4665 ir_mode *mode = get_irn_mode(v);
4667 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4668 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4670 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4677 dbg_info *dbgi = get_irn_dbg_info(n);
4678 ir_node *block = get_nodes_block(n);
4680 /* create a new compare */
4681 n = new_rd_Cmp(dbgi, block, left, right, relation);
4688 * Optimize CopyB(mem, x, x) into a Nop.
4690 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4692 ir_node *copyb = get_Proj_pred(proj);
4693 ir_node *a = get_CopyB_dst(copyb);
4694 ir_node *b = get_CopyB_src(copyb);
4697 switch (get_Proj_proj(proj)) {
4698 case pn_CopyB_X_regular:
4699 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4700 DBG_OPT_EXC_REM(proj);
4701 proj = new_r_Jmp(get_nodes_block(copyb));
4703 case pn_CopyB_X_except: {
4704 ir_graph *irg = get_irn_irg(proj);
4705 DBG_OPT_EXC_REM(proj);
4706 proj = new_r_Bad(irg, mode_X);
4717 * Does all optimizations on nodes that must be done on its Projs
4718 * because of creating new nodes.
4720 static ir_node *transform_node_Proj(ir_node *proj)
4722 ir_node *n = get_Proj_pred(proj);
4724 if (n->op->ops.transform_node_Proj)
4725 return n->op->ops.transform_node_Proj(proj);
4730 * Test whether a block is unreachable
4731 * Note: That this only returns true when
4732 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4733 * This is important, as you easily end up producing invalid constructs in the
4734 * unreachable code when optimizing away edges into the unreachable code.
4735 * So only set this flag when you iterate localopts to the fixpoint.
4736 * When you reach the fixpoint then all unreachable code is dead
4737 * (= can't be reached by firm edges) and you won't see the invalid constructs
4740 static bool is_block_unreachable(const ir_node *block)
4742 const ir_graph *irg = get_irn_irg(block);
4743 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4745 return get_Block_dom_depth(block) < 0;
4748 static ir_node *transform_node_Block(ir_node *block)
4750 ir_graph *irg = get_irn_irg(block);
4751 int arity = get_irn_arity(block);
4752 ir_node *bad = NULL;
4755 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4758 for (i = 0; i < arity; ++i) {
4759 ir_node *const pred = get_Block_cfgpred(block, i);
4760 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4763 bad = new_r_Bad(irg, mode_X);
4764 set_irn_n(block, i, bad);
4770 static ir_node *transform_node_Phi(ir_node *phi)
4772 int n = get_irn_arity(phi);
4773 ir_mode *mode = get_irn_mode(phi);
4774 ir_node *block = get_nodes_block(phi);
4775 ir_graph *irg = get_irn_irg(phi);
4776 ir_node *bad = NULL;
4779 /* Set phi-operands for bad-block inputs to bad */
4780 for (i = 0; i < n; ++i) {
4781 if (!is_Bad(get_Phi_pred(phi, i))) {
4782 ir_node *pred = get_Block_cfgpred(block, i);
4783 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4785 bad = new_r_Bad(irg, mode);
4786 set_irn_n(phi, i, bad);
4791 /* Move Pin nodes down through Phi nodes. */
4792 if (mode == mode_M) {
4793 n = get_irn_arity(phi);
4795 /* Beware of Phi0 */
4799 bool has_pin = false;
4801 NEW_ARR_A(ir_node *, in, n);
4803 for (i = 0; i < n; ++i) {
4804 ir_node *pred = get_irn_n(phi, i);
4807 in[i] = get_Pin_op(pred);
4809 } else if (is_Bad(pred)) {
4819 /* Move the Pin nodes "behind" the Phi. */
4820 new_phi = new_r_Phi(block, n, in, mode_M);
4821 return new_r_Pin(block, new_phi);
4824 /* Move Confirms down through Phi nodes. */
4825 else if (mode_is_reference(mode)) {
4826 n = get_irn_arity(phi);
4828 /* Beware of Phi0 */
4830 ir_node *pred = get_irn_n(phi, 0);
4831 ir_node *bound, *new_phi, **in;
4832 ir_relation relation;
4833 bool has_confirm = false;
4835 if (! is_Confirm(pred))
4838 bound = get_Confirm_bound(pred);
4839 relation = get_Confirm_relation(pred);
4841 NEW_ARR_A(ir_node *, in, n);
4842 in[0] = get_Confirm_value(pred);
4844 for (i = 1; i < n; ++i) {
4845 pred = get_irn_n(phi, i);
4847 if (is_Confirm(pred) &&
4848 get_Confirm_bound(pred) == bound &&
4849 get_Confirm_relation(pred) == relation) {
4850 in[i] = get_Confirm_value(pred);
4852 } else if (is_Bad(pred)) {
4862 /* move the Confirm nodes "behind" the Phi */
4863 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4864 return new_r_Confirm(block, new_phi, bound, relation);
4871 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4873 * Should be moved to reassociation?
4875 static ir_node *transform_node_shift(ir_node *n)
4877 ir_node *left, *right;
4879 ir_mode *count_mode;
4880 ir_tarval *tv1, *tv2, *res;
4881 ir_node *in[2], *irn, *block;
4885 left = get_binop_left(n);
4887 /* different operations */
4888 if (get_irn_op(left) != get_irn_op(n))
4891 right = get_binop_right(n);
4892 tv1 = value_of(right);
4893 if (tv1 == tarval_bad)
4896 tv2 = value_of(get_binop_right(left));
4897 if (tv2 == tarval_bad)
4900 count_mode = get_tarval_mode(tv1);
4901 if (get_tarval_mode(tv2) != count_mode) {
4902 /* TODO: search bigger mode or something and convert... */
4906 mode = get_irn_mode(n);
4907 modulo_shf = get_mode_modulo_shift(mode);
4909 if (modulo_shf > 0) {
4910 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4912 /* I'm not so sure what happens in one complement... */
4913 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4914 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4915 * above will be invalid) */
4916 assert(modulo_shf<=0 || is_po2(modulo_shf));
4918 tv1 = tarval_and(tv1, modulo_mask);
4919 tv2 = tarval_and(tv2, modulo_mask);
4921 res = tarval_add(tv1, tv2);
4922 irg = get_irn_irg(n);
4924 /* beware: a simple replacement works only, if res < modulo shift */
4926 int bits = get_mode_size_bits(mode);
4927 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4928 res = tarval_mod(res, modulo);
4930 long bits = get_mode_size_bits(mode);
4931 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4933 /* shifting too much */
4934 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4936 ir_node *block = get_nodes_block(n);
4937 dbg_info *dbgi = get_irn_dbg_info(n);
4938 ir_mode *smode = get_irn_mode(right);
4939 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4940 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4943 return new_r_Const(irg, get_mode_null(mode));
4947 /* ok, we can replace it */
4948 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4949 block = get_nodes_block(n);
4951 in[0] = get_binop_left(left);
4952 in[1] = new_r_Const(irg, res);
4954 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4956 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4963 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4965 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4966 * (also with x >>s c1 when c1>=c2)
4968 static ir_node *transform_node_shl_shr(ir_node *n)
4971 ir_node *right = get_binop_right(n);
4981 ir_tarval *tv_shift;
4984 ir_relation relation;
4987 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4989 if (!is_Const(right))
4992 left = get_binop_left(n);
4993 mode = get_irn_mode(n);
4994 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4995 ir_node *shr_right = get_binop_right(left);
4997 if (!is_Const(shr_right))
5000 x = get_binop_left(left);
5001 tv_shr = get_Const_tarval(shr_right);
5002 tv_shl = get_Const_tarval(right);
5004 if (is_Shrs(left)) {
5005 /* shrs variant only allowed if c1 >= c2 */
5006 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5009 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5012 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5014 tv_mask = tarval_shl(tv_mask, tv_shl);
5015 } else if (is_Shr(n) && is_Shl(left)) {
5016 ir_node *shl_right = get_Shl_right(left);
5018 if (!is_Const(shl_right))
5021 x = get_Shl_left(left);
5022 tv_shr = get_Const_tarval(right);
5023 tv_shl = get_Const_tarval(shl_right);
5025 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5026 tv_mask = tarval_shr(tv_mask, tv_shr);
5031 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5032 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5035 assert(tv_mask != tarval_bad);
5036 assert(get_tarval_mode(tv_mask) == mode);
5038 block = get_nodes_block(n);
5039 irg = get_irn_irg(block);
5040 dbgi = get_irn_dbg_info(n);
5042 relation = tarval_cmp(tv_shl, tv_shr);
5043 if (relation == ir_relation_less || relation == ir_relation_equal) {
5044 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5045 new_const = new_r_Const(irg, tv_shift);
5047 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5049 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5052 assert(relation == ir_relation_greater);
5053 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5054 new_const = new_r_Const(irg, tv_shift);
5055 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5058 new_const = new_r_Const(irg, tv_mask);
5059 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5064 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5066 ir_mode *mode = get_tarval_mode(tv);
5067 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5068 return tarval_mod(tv, modulo_tv);
5071 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5072 ir_node *left, ir_node *right, ir_mode *mode);
5075 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5076 * then we can use that to minimize the value of Add(x, const) or
5077 * Sub(Const, x). In particular this often avoids 1 instruction in some
5078 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5079 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5081 static ir_node *transform_node_shift_modulo(ir_node *n,
5082 new_shift_func new_shift)
5084 ir_mode *mode = get_irn_mode(n);
5085 int modulo = get_mode_modulo_shift(mode);
5086 ir_node *newop = NULL;
5087 ir_mode *mode_right;
5094 if (get_mode_arithmetic(mode) != irma_twos_complement)
5096 if (!is_po2(modulo))
5099 irg = get_irn_irg(n);
5100 block = get_nodes_block(n);
5101 right = get_binop_right(n);
5102 mode_right = get_irn_mode(right);
5103 if (is_Const(right)) {
5104 ir_tarval *tv = get_Const_tarval(right);
5105 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5110 newop = new_r_Const(irg, tv_mod);
5111 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5112 ir_node *add_right = get_binop_right(right);
5113 if (is_Const(add_right)) {
5114 ir_tarval *tv = get_Const_tarval(add_right);
5115 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5120 newconst = new_r_Const(irg, tv_mod);
5121 newop = new_r_Add(block, get_binop_left(right), newconst,
5124 } else if (is_Sub(right)) {
5125 ir_node *sub_left = get_Sub_left(right);
5126 if (is_Const(sub_left)) {
5127 ir_tarval *tv = get_Const_tarval(sub_left);
5128 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5133 newconst = new_r_Const(irg, tv_mod);
5134 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5141 if (newop != NULL) {
5142 dbg_info *dbgi = get_irn_dbg_info(n);
5143 ir_node *left = get_binop_left(n);
5144 return new_shift(dbgi, block, left, newop, mode);
5152 static ir_node *transform_node_Shr(ir_node *n)
5154 ir_node *c, *oldn = n;
5155 ir_node *left = get_Shr_left(n);
5156 ir_node *right = get_Shr_right(n);
5157 ir_mode *mode = get_irn_mode(n);
5159 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5160 n = transform_node_shift(n);
5163 n = transform_node_shift_modulo(n, new_rd_Shr);
5165 n = transform_node_shl_shr(n);
5167 n = transform_node_shift_bitop(n);
5175 static ir_node *transform_node_Shrs(ir_node *n)
5178 ir_node *a = get_Shrs_left(n);
5179 ir_node *b = get_Shrs_right(n);
5180 ir_mode *mode = get_irn_mode(n);
5184 if (is_oversize_shift(n)) {
5185 ir_node *block = get_nodes_block(n);
5186 dbg_info *dbgi = get_irn_dbg_info(n);
5187 ir_mode *cmode = get_irn_mode(b);
5188 long val = get_mode_size_bits(cmode)-1;
5189 ir_graph *irg = get_irn_irg(n);
5190 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5191 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5194 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5195 n = transform_node_shift(n);
5199 n = transform_node_shift_modulo(n, new_rd_Shrs);
5202 n = transform_node_shift_bitop(n);
5206 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5207 attr = vrp_get_info(a);
5209 unsigned bits = get_mode_size_bits(mode);
5210 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5211 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5212 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5213 dbg_info *dbgi = get_irn_dbg_info(n);
5214 ir_node *block = get_nodes_block(n);
5215 return new_rd_Shr(dbgi, block, a, b, mode);
5225 static ir_node *transform_node_Shl(ir_node *n)
5227 ir_node *c, *oldn = n;
5228 ir_node *a = get_Shl_left(n);
5229 ir_node *b = get_Shl_right(n);
5230 ir_mode *mode = get_irn_mode(n);
5232 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5233 n = transform_node_shift(n);
5236 n = transform_node_shift_modulo(n, new_rd_Shl);
5238 n = transform_node_shl_shr(n);
5240 n = transform_node_shift_bitop(n);
5248 static ir_node *transform_node_Rotl(ir_node *n)
5250 ir_node *c, *oldn = n;
5251 ir_node *a = get_Rotl_left(n);
5252 ir_node *b = get_Rotl_right(n);
5253 ir_mode *mode = get_irn_mode(n);
5255 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5256 n = transform_node_shift(n);
5259 n = transform_node_shift_bitop(n);
5265 * returns mode size for may_leave_out_middle_mode
5267 static unsigned get_significand_size(ir_mode *mode)
5269 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5270 switch (arithmetic) {
5272 case irma_x86_extended_float:
5273 return get_mode_mantissa_size(mode) + 1;
5274 case irma_twos_complement:
5275 return get_mode_size_bits(mode);
5277 panic("Conv node with irma_none mode?");
5279 panic("unexpected mode_arithmetic in get_significand_size");
5283 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5284 * as converting from @p m0 to @p m1 and then to @p m2.
5285 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5286 * gives the following truth table:
5287 * s -> b -> m : true
5288 * s -> m -> b : !signed(s) || signed(m)
5289 * m -> b -> s : true
5290 * m -> s -> b : false
5291 * b -> s -> m : false
5292 * b -> m -> s : true
5294 * s -> b -> b : true
5295 * s -> s -> b : false
5297 * additional float constraints:
5299 * F -> I -> I: signedness of Is must match
5300 * I -> F -> I: signedness of Is must match
5301 * I -> I -> F: signedness of Is must match
5305 * at least 1 float involved: signedness must match
5307 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5309 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5310 if (n_floats == 1) {
5311 /* because overflow gives strange results we don't touch this case */
5313 } else if (n_floats == 2 && !mode_is_float(m1)) {
5317 unsigned size0 = get_significand_size(m0);
5318 unsigned size1 = get_significand_size(m1);
5319 unsigned size2 = get_significand_size(m2);
5320 if (size1 < size2 && size0 >= size1)
5324 return !mode_is_signed(m0) || mode_is_signed(m1);
5330 static ir_node *transform_node_Conv(ir_node *n)
5332 ir_node *c, *oldn = n;
5333 ir_mode *mode = get_irn_mode(n);
5334 ir_node *a = get_Conv_op(n);
5337 ir_mode *a_mode = get_irn_mode(a);
5338 ir_node *b = get_Conv_op(a);
5339 ir_mode *b_mode = get_irn_mode(b);
5340 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5341 dbg_info *dbgi = get_irn_dbg_info(n);
5342 ir_node *block = get_nodes_block(n);
5343 return new_rd_Conv(dbgi, block, b, mode);
5347 if (mode != mode_b && is_const_Phi(a)) {
5348 /* Do NOT optimize mode_b Conv's, this leads to remaining
5349 * Phib nodes later, because the conv_b_lower operation
5350 * is instantly reverted, when it tries to insert a Convb.
5352 c = apply_conv_on_phi(a, mode);
5354 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5359 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5360 ir_graph *irg = get_irn_irg(n);
5361 return new_r_Unknown(irg, mode);
5364 if (mode_is_reference(mode) &&
5365 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5367 ir_node *l = get_Add_left(a);
5368 ir_node *r = get_Add_right(a);
5369 dbg_info *dbgi = get_irn_dbg_info(a);
5370 ir_node *block = get_nodes_block(n);
5372 ir_node *lop = get_Conv_op(l);
5373 if (get_irn_mode(lop) == mode) {
5374 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5375 n = new_rd_Add(dbgi, block, lop, r, mode);
5380 ir_node *rop = get_Conv_op(r);
5381 if (get_irn_mode(rop) == mode) {
5382 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5383 n = new_rd_Add(dbgi, block, l, rop, mode);
5393 * Remove dead blocks and nodes in dead blocks
5394 * in keep alive list. We do not generate a new End node.
5396 static ir_node *transform_node_End(ir_node *n)
5398 int i, j, n_keepalives = get_End_n_keepalives(n);
5401 NEW_ARR_A(ir_node *, in, n_keepalives);
5403 for (i = j = 0; i < n_keepalives; ++i) {
5404 ir_node *ka = get_End_keepalive(n, i);
5406 /* no need to keep Bad */
5409 /* do not keep unreachable code */
5410 block = is_Block(ka) ? ka : get_nodes_block(ka);
5411 if (is_block_unreachable(block))
5415 if (j != n_keepalives)
5416 set_End_keepalives(n, j, in);
5420 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5422 if (is_Minus(a) && get_Minus_op(a) == b)
5424 if (is_Minus(b) && get_Minus_op(b) == a)
5426 if (is_Sub(a) && is_Sub(b)) {
5427 ir_node *a_left = get_Sub_left(a);
5428 ir_node *a_right = get_Sub_right(a);
5429 ir_node *b_left = get_Sub_left(b);
5430 ir_node *b_right = get_Sub_right(b);
5432 if (a_left == b_right && a_right == b_left)
5439 static const ir_node *skip_upconv(const ir_node *node)
5441 while (is_Conv(node)) {
5442 ir_mode *mode = get_irn_mode(node);
5443 const ir_node *op = get_Conv_op(node);
5444 ir_mode *op_mode = get_irn_mode(op);
5445 if (!smaller_mode(op_mode, mode))
5452 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5453 const ir_node *mux_true)
5458 ir_relation relation;
5464 * Note further that these optimization work even for floating point
5465 * with NaN's because -NaN == NaN.
5466 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5469 mode = get_irn_mode(mux_true);
5470 if (mode_honor_signed_zeros(mode))
5473 /* must be <, <=, >=, > */
5474 relation = get_Cmp_relation(sel);
5475 if ((relation & ir_relation_less_greater) == 0)
5478 if (!ir_is_negated_value(mux_true, mux_false))
5481 mux_true = skip_upconv(mux_true);
5482 mux_false = skip_upconv(mux_false);
5484 /* must be x cmp 0 */
5485 cmp_right = get_Cmp_right(sel);
5486 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5489 cmp_left = get_Cmp_left(sel);
5490 if (cmp_left == mux_false) {
5491 if (relation & ir_relation_less) {
5494 assert(relation & ir_relation_greater);
5497 } else if (cmp_left == mux_true) {
5498 if (relation & ir_relation_less) {
5501 assert(relation & ir_relation_greater);
5509 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5512 ir_node *cmp_left = get_Cmp_left(sel);
5513 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5516 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5517 const ir_node *mux_true)
5519 /* this code should return true each time transform_node_Mux would
5520 * optimize the Mux completely away */
5522 ir_mode *mode = get_irn_mode(mux_false);
5523 if (get_mode_arithmetic(mode) == irma_twos_complement
5524 && ir_mux_is_abs(sel, mux_false, mux_true))
5527 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5528 const ir_node *cmp_r = get_Cmp_right(sel);
5529 const ir_node *cmp_l = get_Cmp_left(sel);
5530 const ir_node *f = mux_false;
5531 const ir_node *t = mux_true;
5533 if (is_Const(t) && is_Const_null(t)) {
5538 if (is_And(cmp_l) && f == cmp_r) {
5539 ir_node *and_r = get_And_right(cmp_l);
5542 if (and_r == t && is_single_bit(and_r))
5544 and_l = get_And_left(cmp_l);
5545 if (and_l == t && is_single_bit(and_l))
5554 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5556 static ir_node *transform_Mux_set(ir_node *n)
5558 ir_node *cond = get_Mux_sel(n);
5563 ir_relation relation;
5577 left = get_Cmp_left(cond);
5578 mode = get_irn_mode(left);
5579 if (!mode_is_int(mode) && !mode_is_reference(mode))
5581 dest_mode = get_irn_mode(n);
5582 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5584 right = get_Cmp_right(cond);
5585 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5586 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5587 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5588 && relation != ir_relation_greater))
5593 case ir_relation_less:
5594 /* a < b -> (a - b) >> 31 */
5598 case ir_relation_less_equal:
5599 /* a <= b -> ~(a - b) >> 31 */
5604 case ir_relation_greater:
5605 /* a > b -> (b - a) >> 31 */
5609 case ir_relation_greater_equal:
5610 /* a >= b -> ~(a - b) >> 31 */
5619 dbgi = get_irn_dbg_info(n);
5620 block = get_nodes_block(n);
5621 irg = get_irn_irg(block);
5622 bits = get_mode_size_bits(dest_mode);
5623 tv = new_tarval_from_long(bits-1, mode_Iu);
5624 shift_cnt = new_rd_Const(dbgi, irg, tv);
5626 if (mode != dest_mode) {
5627 a = new_rd_Conv(dbgi, block, a, dest_mode);
5628 b = new_rd_Conv(dbgi, block, b, dest_mode);
5631 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5633 res = new_rd_Not(dbgi, block, res, dest_mode);
5635 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5640 * Optimize a Mux into some simpler cases.
5642 static ir_node *transform_node_Mux(ir_node *n)
5645 ir_node *sel = get_Mux_sel(n);
5646 ir_mode *mode = get_irn_mode(n);
5647 ir_node *t = get_Mux_true(n);
5648 ir_node *f = get_Mux_false(n);
5649 ir_graph *irg = get_irn_irg(n);
5651 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5652 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5653 int abs = ir_mux_is_abs(sel, f, t);
5655 dbg_info *dbgi = get_irn_dbg_info(n);
5656 ir_node *block = get_nodes_block(n);
5657 ir_node *op = ir_get_abs_op(sel, f, t);
5658 int bits = get_mode_size_bits(mode);
5659 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5660 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5661 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5664 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5666 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5672 /* first normalization step: try to move a constant to the false side,
5673 * 0 preferred on false side too */
5674 if (is_Cmp(sel) && is_Const(t) &&
5675 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5676 dbg_info *seldbgi = get_irn_dbg_info(sel);
5677 ir_node *block = get_nodes_block(sel);
5678 ir_relation relation = get_Cmp_relation(sel);
5683 /* Mux(x, a, b) => Mux(not(x), b, a) */
5684 relation = get_negated_relation(relation);
5685 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5686 get_Cmp_right(sel), relation);
5687 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5690 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5691 n = transform_Mux_set(n);
5696 /* the following optimisations create new mode_b nodes, so only do them
5697 * before mode_b lowering */
5698 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5700 ir_node* block = get_nodes_block(n);
5702 ir_node* c1 = get_Mux_sel(t);
5703 ir_node* t1 = get_Mux_true(t);
5704 ir_node* f1 = get_Mux_false(t);
5706 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5707 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5708 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5709 return new_r_Mux(block, and_, f1, t1, mode);
5710 } else if (f == t1) {
5711 /* Mux(cond0, Mux(cond1, x, y), x) */
5712 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5713 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5714 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5715 return new_r_Mux(block, and_, t1, f1, mode);
5717 } else if (is_Mux(f)) {
5718 ir_node* block = get_nodes_block(n);
5720 ir_node* c1 = get_Mux_sel(f);
5721 ir_node* t1 = get_Mux_true(f);
5722 ir_node* f1 = get_Mux_false(f);
5724 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5725 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5726 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5727 return new_r_Mux(block, or_, f1, t1, mode);
5728 } else if (t == f1) {
5729 /* Mux(cond0, x, Mux(cond1, y, x)) */
5730 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5731 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5732 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5733 return new_r_Mux(block, or_, t1, f1, mode);
5737 /* note: after normalization, false can only happen on default */
5738 if (mode == mode_b) {
5739 dbg_info *dbg = get_irn_dbg_info(n);
5740 ir_node *block = get_nodes_block(n);
5743 ir_tarval *tv_t = get_Const_tarval(t);
5744 if (tv_t == tarval_b_true) {
5746 /* Muxb(sel, true, false) = sel */
5747 assert(get_Const_tarval(f) == tarval_b_false);
5748 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5751 /* Muxb(sel, true, x) = Or(sel, x) */
5752 n = new_rd_Or(dbg, block, sel, f, mode_b);
5753 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5757 } else if (is_Const(f)) {
5758 ir_tarval *tv_f = get_Const_tarval(f);
5759 if (tv_f == tarval_b_true) {
5760 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5761 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5762 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5763 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5766 /* Muxb(sel, x, false) = And(sel, x) */
5767 assert(tv_f == tarval_b_false);
5768 n = new_rd_And(dbg, block, sel, t, mode_b);
5769 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5776 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5777 ir_relation relation = get_Cmp_relation(sel);
5778 ir_node *cmp_r = get_Cmp_right(sel);
5779 ir_node *cmp_l = get_Cmp_left(sel);
5780 ir_node *block = get_nodes_block(n);
5782 if (is_And(cmp_l) && f == cmp_r) {
5783 ir_node *and_r = get_And_right(cmp_l);
5786 if (and_r == t && is_single_bit(and_r)) {
5787 if (relation == ir_relation_equal) {
5788 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5789 n = new_rd_Eor(get_irn_dbg_info(n),
5790 block, cmp_l, t, mode);
5791 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5793 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5795 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5799 and_l = get_And_left(cmp_l);
5800 if (and_l == t && is_single_bit(and_l)) {
5801 if (relation == ir_relation_equal) {
5802 /* ((1 << n) & a) == 0, (1 << n), 0) */
5803 n = new_rd_Eor(get_irn_dbg_info(n),
5804 block, cmp_l, t, mode);
5805 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5807 /* ((1 << n) & a) != 0, (1 << n), 0) */
5809 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5820 * optimize Sync nodes that have other syncs as input we simply add the inputs
5821 * of the other sync to our own inputs
5823 static ir_node *transform_node_Sync(ir_node *n)
5825 int arity = get_Sync_n_preds(n);
5828 for (i = 0; i < arity;) {
5829 ir_node *pred = get_Sync_pred(n, i);
5833 /* Remove Bad predecessors */
5840 /* Remove duplicate predecessors */
5841 for (j = 0; j < i; ++j) {
5842 if (get_Sync_pred(n, j) == pred) {
5851 if (!is_Sync(pred)) {
5859 pred_arity = get_Sync_n_preds(pred);
5860 for (j = 0; j < pred_arity; ++j) {
5861 ir_node *pred_pred = get_Sync_pred(pred, j);
5866 add_irn_n(n, pred_pred);
5870 if (get_Sync_pred(n, k) == pred_pred)
5877 ir_graph *irg = get_irn_irg(n);
5878 return new_r_Bad(irg, mode_M);
5881 return get_Sync_pred(n, 0);
5884 /* rehash the sync node */
5889 static ir_node *create_load_replacement_tuple(ir_node *n, ir_node *mem,
5892 ir_node *block = get_nodes_block(n);
5893 ir_graph *irg = get_irn_irg(n);
5894 ir_node *in[pn_Load_max+1];
5896 in[pn_Load_M] = mem;
5897 in[pn_Load_res] = res;
5898 if (ir_throws_exception(n)) {
5899 in[pn_Load_X_regular] = new_r_Jmp(block);
5900 in[pn_Load_X_except] = new_r_Bad(irg, mode_X);
5902 assert(pn_Load_max == 4);
5904 ir_node *tuple = new_r_Tuple(block, n_in, in);
5908 static ir_node *transform_node_Load(ir_node *n)
5910 /* don't touch volatile loads */
5911 if (get_Load_volatility(n) == volatility_is_volatile)
5914 ir_node *ptr = get_Load_ptr(n);
5915 const ir_node *confirm;
5916 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5917 set_irn_pinned(n, op_pin_state_floats);
5920 /* if our memory predecessor is a load from the same address, then reuse the
5921 * previous result */
5922 ir_node *mem = get_Load_mem(n);
5925 ir_node *mem_pred = get_Proj_pred(mem);
5926 if (is_Load(mem_pred)) {
5927 ir_node *pred_load = mem_pred;
5929 /* conservatively compare the 2 loads. TODO: This could be less strict
5930 * with fixup code in some situations (like smaller/bigger modes) */
5931 if (get_Load_ptr(pred_load) != ptr)
5933 if (get_Load_mode(pred_load) != get_Load_mode(n))
5935 /* all combinations of aligned/unaligned pred/n should be fine so we do
5936 * not compare the unaligned attribute */
5937 ir_mode *mode = get_Load_mode(n);
5938 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5939 return create_load_replacement_tuple(n, mem, res);
5940 } else if (is_Store(mem_pred)) {
5941 ir_node *pred_store = mem_pred;
5942 ir_node *value = get_Store_value(pred_store);
5944 if (get_Store_ptr(pred_store) != ptr)
5946 if (get_irn_mode(value) != get_Load_mode(n))
5948 /* all combinations of aligned/unaligned pred/n should be fine so we do
5949 * not compare the unaligned attribute */
5950 return create_load_replacement_tuple(n, mem, value);
5956 static ir_node *transform_node_Store(ir_node *n)
5958 /* don't touch volatile stores */
5959 if (get_Store_volatility(n) == volatility_is_volatile)
5962 ir_node *ptr = get_Store_ptr(n);
5963 const ir_node *confirm;
5964 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5965 set_irn_pinned(n, op_pin_state_floats);
5971 * optimize a trampoline Call into a direct Call
5973 static ir_node *transform_node_Call(ir_node *call)
5975 ir_node *callee = get_Call_ptr(call);
5976 ir_node *adr, *mem, *res, *bl, **in;
5977 ir_type *ctp, *mtp, *tp;
5981 size_t i, n_res, n_param;
5984 if (! is_Proj(callee))
5986 callee = get_Proj_pred(callee);
5987 if (! is_Builtin(callee))
5989 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5992 mem = get_Call_mem(call);
5994 if (skip_Proj(mem) == callee) {
5995 /* memory is routed to the trampoline, skip */
5996 mem = get_Builtin_mem(callee);
5999 /* build a new call type */
6000 mtp = get_Call_type(call);
6001 tdb = get_type_dbg_info(mtp);
6003 n_res = get_method_n_ress(mtp);
6004 n_param = get_method_n_params(mtp);
6005 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6007 for (i = 0; i < n_res; ++i)
6008 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6010 NEW_ARR_A(ir_node *, in, n_param + 1);
6012 /* FIXME: we don't need a new pointer type in every step */
6013 irg = get_irn_irg(call);
6014 tp = get_irg_frame_type(irg);
6015 tp = new_type_pointer(tp);
6016 set_method_param_type(ctp, 0, tp);
6018 in[0] = get_Builtin_param(callee, 2);
6019 for (i = 0; i < n_param; ++i) {
6020 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6021 in[i + 1] = get_Call_param(call, i);
6023 var = get_method_variadicity(mtp);
6024 set_method_variadicity(ctp, var);
6025 /* When we resolve a trampoline, the function must be called by a this-call */
6026 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6027 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6029 adr = get_Builtin_param(callee, 1);
6031 db = get_irn_dbg_info(call);
6032 bl = get_nodes_block(call);
6034 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6035 if (get_irn_pinned(call) == op_pin_state_floats)
6036 set_irn_pinned(res, op_pin_state_floats);
6041 * Tries several [inplace] [optimizing] transformations and returns an
6042 * equivalent node. The difference to equivalent_node() is that these
6043 * transformations _do_ generate new nodes, and thus the old node must
6044 * not be freed even if the equivalent node isn't the old one.
6046 static ir_node *transform_node(ir_node *n)
6052 iro = get_irn_opcode_(n);
6053 /* constant expression evaluation / constant folding */
6054 if (get_opt_constant_folding()) {
6055 /* neither constants nor Tuple values can be evaluated */
6056 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6057 /* try to evaluate */
6058 ir_tarval *tv = computed_value(n);
6059 if (tv != tarval_bad) {
6060 /* evaluation was successful -- replace the node. */
6061 ir_graph *irg = get_irn_irg(n);
6063 n = new_r_Const(irg, tv);
6065 DBG_OPT_CSTEVAL(old_n, n);
6071 /* remove unnecessary nodes */
6072 if (get_opt_constant_folding() ||
6073 (iro == iro_Phi) || /* always optimize these nodes. */
6074 (iro == iro_Id) || /* ... */
6075 (iro == iro_Proj) || /* ... */
6076 (iro == iro_Block)) { /* Flags tested local. */
6077 n = equivalent_node(n);
6082 /* Some more constant expression evaluation. */
6083 if (get_opt_algebraic_simplification() ||
6084 (iro == iro_Cond) ||
6085 (iro == iro_Proj)) { /* Flags tested local. */
6086 if (n->op->ops.transform_node != NULL) {
6087 n = n->op->ops.transform_node(n);
6097 static void register_computed_value_func(ir_op *op, computed_value_func func)
6099 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6100 op->ops.computed_value = func;
6103 static void register_computed_value_func_proj(ir_op *op,
6104 computed_value_func func)
6106 assert(op->ops.computed_value_Proj == NULL
6107 || op->ops.computed_value_Proj == func);
6108 op->ops.computed_value_Proj = func;
6111 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6113 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6114 op->ops.equivalent_node = func;
6117 static void register_equivalent_node_func_proj(ir_op *op,
6118 equivalent_node_func func)
6120 assert(op->ops.equivalent_node_Proj == NULL
6121 || op->ops.equivalent_node_Proj == func);
6122 op->ops.equivalent_node_Proj = func;
6125 static void register_transform_node_func(ir_op *op, transform_node_func func)
6127 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6128 op->ops.transform_node = func;
6131 static void register_transform_node_func_proj(ir_op *op,
6132 transform_node_func func)
6134 assert(op->ops.transform_node_Proj == NULL
6135 || op->ops.transform_node_Proj == func);
6136 op->ops.transform_node_Proj = func;
6139 void ir_register_opt_node_ops(void)
6141 register_computed_value_func(op_Add, computed_value_Add);
6142 register_computed_value_func(op_And, computed_value_And);
6143 register_computed_value_func(op_Cmp, computed_value_Cmp);
6144 register_computed_value_func(op_Confirm, computed_value_Confirm);
6145 register_computed_value_func(op_Const, computed_value_Const);
6146 register_computed_value_func(op_Conv, computed_value_Conv);
6147 register_computed_value_func(op_Eor, computed_value_Eor);
6148 register_computed_value_func(op_Minus, computed_value_Minus);
6149 register_computed_value_func(op_Mul, computed_value_Mul);
6150 register_computed_value_func(op_Mux, computed_value_Mux);
6151 register_computed_value_func(op_Not, computed_value_Not);
6152 register_computed_value_func(op_Or, computed_value_Or);
6153 register_computed_value_func(op_Proj, computed_value_Proj);
6154 register_computed_value_func(op_Rotl, computed_value_Rotl);
6155 register_computed_value_func(op_Shl, computed_value_Shl);
6156 register_computed_value_func(op_Shr, computed_value_Shr);
6157 register_computed_value_func(op_Shrs, computed_value_Shrs);
6158 register_computed_value_func(op_Sub, computed_value_Sub);
6159 register_computed_value_func(op_SymConst, computed_value_SymConst);
6160 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6161 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6163 register_equivalent_node_func(op_Add, equivalent_node_Add);
6164 register_equivalent_node_func(op_And, equivalent_node_And);
6165 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6166 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6167 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6168 register_equivalent_node_func(op_Id, equivalent_node_Id);
6169 register_equivalent_node_func(op_Minus, equivalent_node_involution);
6170 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6171 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6172 register_equivalent_node_func(op_Not, equivalent_node_involution);
6173 register_equivalent_node_func(op_Or, equivalent_node_Or);
6174 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6175 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6176 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6177 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6178 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6179 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6180 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6181 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6182 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6183 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6185 register_transform_node_func(op_Add, transform_node_Add);
6186 register_transform_node_func(op_And, transform_node_And);
6187 register_transform_node_func(op_Block, transform_node_Block);
6188 register_transform_node_func(op_Call, transform_node_Call);
6189 register_transform_node_func(op_Cmp, transform_node_Cmp);
6190 register_transform_node_func(op_Cond, transform_node_Cond);
6191 register_transform_node_func(op_Conv, transform_node_Conv);
6192 register_transform_node_func(op_Div, transform_node_Div);
6193 register_transform_node_func(op_End, transform_node_End);
6194 register_transform_node_func(op_Eor, transform_node_Eor);
6195 register_transform_node_func(op_Load, transform_node_Load);
6196 register_transform_node_func(op_Minus, transform_node_Minus);
6197 register_transform_node_func(op_Mod, transform_node_Mod);
6198 register_transform_node_func(op_Mul, transform_node_Mul);
6199 register_transform_node_func(op_Mux, transform_node_Mux);
6200 register_transform_node_func(op_Not, transform_node_Not);
6201 register_transform_node_func(op_Or, transform_node_Or);
6202 register_transform_node_func(op_Phi, transform_node_Phi);
6203 register_transform_node_func(op_Proj, transform_node_Proj);
6204 register_transform_node_func(op_Rotl, transform_node_Rotl);
6205 register_transform_node_func(op_Shl, transform_node_Shl);
6206 register_transform_node_func(op_Shrs, transform_node_Shrs);
6207 register_transform_node_func(op_Shr, transform_node_Shr);
6208 register_transform_node_func(op_Store, transform_node_Store);
6209 register_transform_node_func(op_Sub, transform_node_Sub);
6210 register_transform_node_func(op_Switch, transform_node_Switch);
6211 register_transform_node_func(op_Sync, transform_node_Sync);
6212 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6213 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6214 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6215 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6216 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6219 /* **************** Common Subexpression Elimination **************** */
6221 /** The size of the hash table used, should estimate the number of nodes
6223 #define N_IR_NODES 512
6225 int identities_cmp(const void *elt, const void *key)
6227 ir_node *a = (ir_node *)elt;
6228 ir_node *b = (ir_node *)key;
6231 if (a == b) return 0;
6233 if ((get_irn_op(a) != get_irn_op(b)) ||
6234 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6236 /* compare if a's in and b's in are of equal length */
6237 irn_arity_a = get_irn_arity(a);
6238 if (irn_arity_a != get_irn_arity(b))
6241 /* blocks are never the same */
6245 if (get_irn_pinned(a) == op_pin_state_pinned) {
6246 /* for pinned nodes, the block inputs must be equal */
6247 if (get_nodes_block(a) != get_nodes_block(b))
6250 ir_node *block_a = get_nodes_block(a);
6251 ir_node *block_b = get_nodes_block(b);
6252 if (! get_opt_global_cse()) {
6253 /* for block-local CSE both nodes must be in the same Block */
6254 if (block_a != block_b)
6257 /* The optimistic approach would be to do nothing here.
6258 * However doing GCSE optimistically produces a lot of partially dead code which appears
6259 * to be worse in practice than the missed opportunities.
6260 * So we use a very conservative variant here and only CSE if 1 value dominates the
6262 if (!block_dominates(block_a, block_b)
6263 && !block_dominates(block_b, block_a))
6265 /* respect the workaround rule: do not move nodes which are only
6266 * held by keepalive edges */
6267 if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6272 /* compare a->in[0..ins] with b->in[0..ins] */
6273 for (i = 0; i < irn_arity_a; ++i) {
6274 ir_node *pred_a = get_irn_n(a, i);
6275 ir_node *pred_b = get_irn_n(b, i);
6276 if (pred_a != pred_b) {
6277 /* if both predecessors are CSE neutral they might be different */
6278 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6284 * here, we already now that the nodes are identical except their
6287 if (a->op->ops.node_cmp_attr)
6288 return a->op->ops.node_cmp_attr(a, b);
6293 unsigned ir_node_hash(const ir_node *node)
6295 return node->op->ops.hash(node);
6298 void new_identities(ir_graph *irg)
6300 if (irg->value_table != NULL)
6301 del_pset(irg->value_table);
6302 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6305 void del_identities(ir_graph *irg)
6307 if (irg->value_table != NULL)
6308 del_pset(irg->value_table);
6311 static int cmp_node_nr(const void *a, const void *b)
6313 ir_node **p1 = (ir_node**)a;
6314 ir_node **p2 = (ir_node**)b;
6315 long n1 = get_irn_node_nr(*p1);
6316 long n2 = get_irn_node_nr(*p2);
6317 return (n1>n2) - (n1<n2);
6320 void ir_normalize_node(ir_node *n)
6322 if (is_op_commutative(get_irn_op(n))) {
6323 ir_node *l = get_binop_left(n);
6324 ir_node *r = get_binop_right(n);
6326 /* For commutative operators perform a OP b == b OP a but keep
6327 * constants on the RIGHT side. This helps greatly in some
6328 * optimizations. Moreover we use the idx number to make the form
6330 if (!operands_are_normalized(l, r)) {
6331 set_binop_left(n, r);
6332 set_binop_right(n, l);
6335 } else if (is_Sync(n)) {
6336 /* we assume that most of the time the inputs of a Sync node are already
6337 * sorted, so check this first as a shortcut */
6338 bool ins_sorted = true;
6339 int arity = get_irn_arity(n);
6340 const ir_node *last = get_irn_n(n, 0);
6342 for (i = 1; i < arity; ++i) {
6343 const ir_node *node = get_irn_n(n, i);
6344 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6352 ir_node **ins = get_irn_in(n)+1;
6353 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6354 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6355 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6356 set_irn_in(n, arity, new_ins);
6362 ir_node *identify_remember(ir_node *n)
6364 ir_graph *irg = get_irn_irg(n);
6365 pset *value_table = irg->value_table;
6368 if (value_table == NULL)
6371 ir_normalize_node(n);
6372 /* lookup or insert in hash table with given hash key. */
6373 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6376 /* n is reachable again */
6377 edges_node_revival(nn);
6384 * During construction we set the op_pin_state_pinned flag in the graph right
6385 * when the optimization is performed. The flag turning on procedure global
6386 * cse could be changed between two allocations. This way we are safe.
6388 * @param n The node to lookup
6390 static inline ir_node *identify_cons(ir_node *n)
6394 n = identify_remember(n);
6395 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6396 ir_graph *irg = get_irn_irg(n);
6397 set_irg_pinned(irg, op_pin_state_floats);
6402 void add_identities(ir_node *node)
6409 identify_remember(node);
6412 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6414 ir_graph *rem = current_ir_graph;
6416 current_ir_graph = irg;
6417 foreach_pset(irg->value_table, ir_node, node) {
6420 current_ir_graph = rem;
6423 ir_node *optimize_node(ir_node *n)
6426 ir_graph *irg = get_irn_irg(n);
6427 unsigned iro = get_irn_opcode(n);
6430 /* Always optimize Phi nodes: part of the construction. */
6431 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6433 /* constant expression evaluation / constant folding */
6434 if (get_opt_constant_folding()) {
6435 /* neither constants nor Tuple values can be evaluated */
6436 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6437 /* try to evaluate */
6438 tv = computed_value(n);
6439 if (tv != tarval_bad) {
6444 * we MUST copy the node here temporarily, because it's still
6445 * needed for DBG_OPT_CSTEVAL
6447 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6448 oldn = (ir_node*)alloca(node_size);
6450 memcpy(oldn, n, node_size);
6451 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6453 /* ARG, copy the in array, we need it for statistics */
6454 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6456 /* note the inplace edges module */
6457 edges_node_deleted(n);
6459 /* evaluation was successful -- replace the node. */
6460 irg_kill_node(irg, n);
6461 nw = new_r_Const(irg, tv);
6463 DBG_OPT_CSTEVAL(oldn, nw);
6469 /* remove unnecessary nodes */
6470 if (get_opt_algebraic_simplification() ||
6471 (iro == iro_Phi) || /* always optimize these nodes. */
6473 (iro == iro_Proj) ||
6474 (iro == iro_Block) ) /* Flags tested local. */
6475 n = equivalent_node(n);
6477 /* Common Subexpression Elimination.
6479 * Checks whether n is already available.
6480 * The block input is used to distinguish different subexpressions. Right
6481 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6482 * subexpressions within a block.
6485 n = identify_cons(n);
6488 edges_node_deleted(oldn);
6490 /* We found an existing, better node, so we can deallocate the old node. */
6491 irg_kill_node(irg, oldn);
6495 /* Some more constant expression evaluation that does not allow to
6497 iro = get_irn_opcode(n);
6498 if (get_opt_algebraic_simplification() ||
6499 (iro == iro_Cond) ||
6500 (iro == iro_Proj)) { /* Flags tested local. */
6501 n = transform_node(n);
6504 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6505 if (get_opt_cse()) {
6507 n = identify_remember(o);
6515 ir_node *optimize_in_place_2(ir_node *n)
6517 if (!get_opt_optimize() && !is_Phi(n)) return n;
6522 /** common subexpression elimination **/
6523 /* Checks whether n is already available. */
6524 /* The block input is used to distinguish different subexpressions.
6525 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6526 * only finds common subexpressions within a block. */
6527 if (get_opt_cse()) {
6529 n = identify_remember(n);
6532 /* we have another existing node now, we do not optimize it here */
6537 n = transform_node(n);
6539 /* Now we can verify the node, as it has no dead inputs any more. */
6542 /* Now we have a legal, useful node. Enter it in hash table for cse.
6544 * Note: This is only necessary because some of the optimisations
6545 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6546 * bad practice and should be fixed sometime.
6548 if (get_opt_cse()) {
6550 n = identify_remember(o);
6558 ir_node *optimize_in_place(ir_node *n)
6560 ir_graph *irg = get_irn_irg(n);
6562 if (get_opt_global_cse())
6563 set_irg_pinned(irg, op_pin_state_floats);
6565 /* FIXME: Maybe we could also test whether optimizing the node can
6566 change the control graph. */
6567 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6568 return optimize_in_place_2(n);