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 block = get_nodes_block(n);
2080 dbg_bitop = get_irn_dbg_info(n);
2081 dbg_shift = get_irn_dbg_info(left);
2082 tv1 = get_Const_tarval(shift_right);
2083 tv2 = get_Const_tarval(right);
2084 assert(get_tarval_mode(tv2) == mode);
2087 tv_bitop = tarval_shr(tv2, tv1);
2089 /* Check whether we have lost some bits during the right shift. */
2091 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2093 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2096 } else if (is_Shr(left)) {
2099 * TODO this can be improved by checking whether
2100 * the left shift produces an overflow
2104 tv_bitop = tarval_shl(tv2, tv1);
2106 assert(is_Rotl(left));
2107 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2109 new_const = new_r_Const(irg, tv_bitop);
2112 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2113 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2114 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2117 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2121 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2122 } else if (is_Shr(left)) {
2123 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2125 assert(is_Rotl(left));
2126 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2132 static bool complement_values(const ir_node *a, const ir_node *b)
2134 if (is_Not(a) && get_Not_op(a) == b)
2136 if (is_Not(b) && get_Not_op(b) == a)
2138 if (is_Const(a) && is_Const(b)) {
2139 ir_tarval *tv_a = get_Const_tarval(a);
2140 ir_tarval *tv_b = get_Const_tarval(b);
2141 return tarval_not(tv_a) == tv_b;
2146 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2149 * for associative operations fold:
2150 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2151 * This is a "light" version of the reassociation phase
2153 static ir_node *fold_constant_associativity(ir_node *node,
2154 tv_fold_binop_func fold)
2159 ir_node *right = get_binop_right(node);
2160 ir_node *left_right;
2167 if (!is_Const(right))
2170 op = get_irn_op(node);
2171 left = get_binop_left(node);
2172 if (get_irn_op(left) != op)
2175 left_right = get_binop_right(left);
2176 if (!is_Const(left_right))
2179 left_left = get_binop_left(left);
2180 c0 = get_Const_tarval(left_right);
2181 c1 = get_Const_tarval(right);
2182 irg = get_irn_irg(node);
2183 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2185 new_c = fold(c0, c1);
2186 if (new_c == tarval_bad)
2188 new_const = new_r_Const(irg, new_c);
2189 new_node = exact_copy(node);
2190 set_binop_left(new_node, left_left);
2191 set_binop_right(new_node, new_const);
2198 static ir_node *transform_node_Or_(ir_node *n)
2201 ir_node *a = get_binop_left(n);
2202 ir_node *b = get_binop_right(n);
2206 n = fold_constant_associativity(n, tarval_or);
2210 if (is_Not(a) && is_Not(b)) {
2211 /* ~a | ~b = ~(a&b) */
2212 ir_node *block = get_nodes_block(n);
2214 mode = get_irn_mode(n);
2217 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2218 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2223 /* we can combine the relations of two compares with the same operands */
2224 if (is_Cmp(a) && is_Cmp(b)) {
2225 ir_node *a_left = get_Cmp_left(a);
2226 ir_node *a_right = get_Cmp_right(a);
2227 ir_node *b_left = get_Cmp_left(b);
2228 ir_node *b_right = get_Cmp_right(b);
2229 if (a_left == b_left && b_left == b_right) {
2230 dbg_info *dbgi = get_irn_dbg_info(n);
2231 ir_node *block = get_nodes_block(n);
2232 ir_relation a_relation = get_Cmp_relation(a);
2233 ir_relation b_relation = get_Cmp_relation(b);
2234 ir_relation new_relation = a_relation | b_relation;
2235 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2237 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2238 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2239 && !mode_is_float(get_irn_mode(a_left))
2240 && !mode_is_float(get_irn_mode(b_left))) {
2241 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2242 ir_graph *irg = get_irn_irg(n);
2243 dbg_info *dbgi = get_irn_dbg_info(n);
2244 ir_node *block = get_nodes_block(n);
2245 ir_mode *a_mode = get_irn_mode(a_left);
2246 ir_mode *b_mode = get_irn_mode(b_left);
2247 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2248 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2249 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2250 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2251 ir_node *zero = create_zero_const(irg, b_mode);
2252 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2254 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2255 ir_graph *irg = get_irn_irg(n);
2256 dbg_info *dbgi = get_irn_dbg_info(n);
2257 ir_node *block = get_nodes_block(n);
2258 ir_mode *a_mode = get_irn_mode(a_left);
2259 ir_mode *b_mode = get_irn_mode(b_left);
2260 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2261 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2262 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2263 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2264 ir_node *zero = create_zero_const(irg, a_mode);
2265 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2270 mode = get_irn_mode(n);
2271 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2273 n = transform_node_Or_bf_store(n);
2276 n = transform_node_Or_Rotl(n);
2280 n = transform_bitwise_distributive(n, transform_node_Or_);
2283 n = transform_node_bitop_shift(n);
2290 static ir_node *transform_node_Or(ir_node *n)
2292 if (is_Or_Eor_Add(n)) {
2293 dbg_info *dbgi = get_irn_dbg_info(n);
2294 ir_node *block = get_nodes_block(n);
2295 ir_node *left = get_Or_left(n);
2296 ir_node *right = get_Or_right(n);
2297 ir_mode *mode = get_irn_mode(n);
2298 return new_rd_Add(dbgi, block, left, right, mode);
2300 return transform_node_Or_(n);
2306 static ir_node *transform_node_Eor_(ir_node *n)
2309 ir_node *a = get_binop_left(n);
2310 ir_node *b = get_binop_right(n);
2311 ir_mode *mode = get_irn_mode(n);
2314 n = fold_constant_associativity(n, tarval_eor);
2318 /* we can combine the relations of two compares with the same operands */
2319 if (is_Cmp(a) && is_Cmp(b)) {
2320 ir_node *a_left = get_Cmp_left(a);
2321 ir_node *a_right = get_Cmp_left(a);
2322 ir_node *b_left = get_Cmp_left(b);
2323 ir_node *b_right = get_Cmp_right(b);
2324 if (a_left == b_left && b_left == b_right) {
2325 dbg_info *dbgi = get_irn_dbg_info(n);
2326 ir_node *block = get_nodes_block(n);
2327 ir_relation a_relation = get_Cmp_relation(a);
2328 ir_relation b_relation = get_Cmp_relation(b);
2329 ir_relation new_relation = a_relation ^ b_relation;
2330 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2334 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2336 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2337 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2338 dbg_info *dbg = get_irn_dbg_info(n);
2339 ir_node *block = get_nodes_block(n);
2340 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2341 ir_node *new_left = get_Not_op(a);
2342 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2343 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2345 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2346 dbg_info *dbg = get_irn_dbg_info(n);
2347 ir_node *block = get_nodes_block(n);
2348 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2349 ir_node *new_right = get_Not_op(b);
2350 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2351 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2355 /* x ^ 1...1 -> ~1 */
2356 if (is_Const(b) && is_Const_all_one(b)) {
2357 n = new_r_Not(get_nodes_block(n), a, mode);
2358 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2362 n = transform_bitwise_distributive(n, transform_node_Eor_);
2365 n = transform_node_bitop_shift(n);
2372 static ir_node *transform_node_Eor(ir_node *n)
2374 if (is_Or_Eor_Add(n)) {
2375 dbg_info *dbgi = get_irn_dbg_info(n);
2376 ir_node *block = get_nodes_block(n);
2377 ir_node *left = get_Eor_left(n);
2378 ir_node *right = get_Eor_right(n);
2379 ir_mode *mode = get_irn_mode(n);
2380 return new_rd_Add(dbgi, block, left, right, mode);
2382 return transform_node_Eor_(n);
2386 * Do the AddSub optimization, then Transform
2387 * Constant folding on Phi
2388 * Add(a,a) -> Mul(a, 2)
2389 * Add(Mul(a, x), a) -> Mul(a, x+1)
2390 * if the mode is integer or float.
2391 * Transform Add(a,-b) into Sub(a,b).
2392 * Reassociation might fold this further.
2394 static ir_node *transform_node_Add(ir_node *n)
2402 n = fold_constant_associativity(n, tarval_add);
2406 n = transform_node_AddSub(n);
2410 a = get_Add_left(n);
2411 b = get_Add_right(n);
2412 mode = get_irn_mode(n);
2414 if (mode_is_reference(mode)) {
2415 ir_mode *lmode = get_irn_mode(a);
2417 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2418 /* an Add(a, NULL) is a hidden Conv */
2419 dbg_info *dbg = get_irn_dbg_info(n);
2420 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2424 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2425 ir_tarval *tv = get_Const_tarval(b);
2426 ir_tarval *min = get_mode_min(mode);
2427 /* if all bits are set, then this has the same effect as a Not.
2428 * Note that the following == gives false for different modes which
2429 * is exactly what we want */
2431 dbg_info *dbgi = get_irn_dbg_info(n);
2432 ir_graph *irg = get_irn_irg(n);
2433 ir_node *block = get_nodes_block(n);
2434 ir_node *cnst = new_r_Const(irg, min);
2435 return new_rd_Eor(dbgi, block, a, cnst, mode);
2439 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2441 /* for FP the following optimizations are only allowed if
2442 * fp_strict_algebraic is disabled */
2443 if (mode_is_float(mode)) {
2444 ir_graph *irg = get_irn_irg(n);
2445 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2449 if (mode_is_num(mode)) {
2450 ir_graph *irg = get_irn_irg(n);
2451 /* the following code leads to endless recursion when Mul are replaced
2452 * by a simple instruction chain */
2453 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2454 && a == b && mode_is_int(mode)) {
2455 ir_node *block = get_nodes_block(n);
2458 get_irn_dbg_info(n),
2461 new_r_Const_long(irg, mode, 2),
2463 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2468 get_irn_dbg_info(n),
2473 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2478 get_irn_dbg_info(n),
2483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2486 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2487 /* Here we rely on constants be on the RIGHT side */
2489 ir_node *op = get_Not_op(a);
2491 if (is_Const(b) && is_Const_one(b)) {
2493 ir_node *blk = get_nodes_block(n);
2494 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2502 if (is_Or_Eor_Add(n)) {
2503 n = transform_node_Or_(n);
2506 n = transform_node_Eor_(n);
2515 * returns -cnst or NULL if impossible
2517 static ir_node *const_negate(ir_node *cnst)
2519 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2520 dbg_info *dbgi = get_irn_dbg_info(cnst);
2521 ir_graph *irg = get_irn_irg(cnst);
2522 if (tv == tarval_bad) return NULL;
2523 return new_rd_Const(dbgi, irg, tv);
2527 * Do the AddSub optimization, then Transform
2528 * Constant folding on Phi
2529 * Sub(0,a) -> Minus(a)
2530 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2531 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2532 * Sub(Add(a, x), x) -> a
2533 * Sub(x, Add(x, a)) -> -a
2534 * Sub(x, Const) -> Add(x, -Const)
2536 static ir_node *transform_node_Sub(ir_node *n)
2542 n = transform_node_AddSub(n);
2544 a = get_Sub_left(n);
2545 b = get_Sub_right(n);
2547 mode = get_irn_mode(n);
2549 if (mode_is_int(mode)) {
2550 ir_mode *lmode = get_irn_mode(a);
2552 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2553 /* a Sub(a, NULL) is a hidden Conv */
2554 dbg_info *dbg = get_irn_dbg_info(n);
2555 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2556 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2560 if (mode == lmode &&
2561 get_mode_arithmetic(mode) == irma_twos_complement &&
2563 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2565 dbg_info *dbg = get_irn_dbg_info(n);
2566 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2567 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2573 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2575 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2576 if (mode_is_float(mode)) {
2577 ir_graph *irg = get_irn_irg(n);
2578 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2582 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2583 /* a - C -> a + (-C) */
2584 ir_node *cnst = const_negate(b);
2586 ir_node *block = get_nodes_block(n);
2587 dbg_info *dbgi = get_irn_dbg_info(n);
2589 n = new_rd_Add(dbgi, block, a, cnst, mode);
2590 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2595 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2596 dbg_info *dbg = get_irn_dbg_info(n);
2597 ir_node *block = get_nodes_block(n);
2598 ir_node *left = get_Minus_op(a);
2599 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2601 n = new_rd_Minus(dbg, block, add, mode);
2602 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2604 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2605 dbg_info *dbg = get_irn_dbg_info(n);
2606 ir_node *block = get_nodes_block(n);
2607 ir_node *right = get_Minus_op(b);
2609 n = new_rd_Add(dbg, block, a, right, mode);
2610 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2612 } else if (is_Sub(b)) {
2613 /* a - (b - c) -> a + (c - b)
2614 * -> (a - b) + c iff (b - c) is a pointer */
2615 dbg_info *s_dbg = get_irn_dbg_info(b);
2616 ir_node *s_left = get_Sub_left(b);
2617 ir_node *s_right = get_Sub_right(b);
2618 ir_mode *s_mode = get_irn_mode(b);
2619 if (mode_is_reference(s_mode)) {
2620 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2621 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2622 dbg_info *a_dbg = get_irn_dbg_info(n);
2625 s_right = new_r_Conv(lowest_block, s_right, mode);
2626 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2628 ir_node *s_block = get_nodes_block(b);
2629 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2630 dbg_info *a_dbg = get_irn_dbg_info(n);
2631 ir_node *a_block = get_nodes_block(n);
2633 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2635 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2639 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2640 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2642 get_irn_dbg_info(n),
2646 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2649 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2650 ir_node *left = get_binop_left(a);
2651 ir_node *right = get_binop_right(a);
2653 /* FIXME: Does the Conv's work only for two complement or generally? */
2655 if (mode != get_irn_mode(right)) {
2656 /* This Sub is an effective Cast */
2657 right = new_r_Conv(get_nodes_block(n), right, mode);
2660 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2662 } else if (right == b) {
2663 if (mode != get_irn_mode(left)) {
2664 /* This Sub is an effective Cast */
2665 left = new_r_Conv(get_nodes_block(n), left, mode);
2668 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2672 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2673 ir_node *left = get_binop_left(b);
2674 ir_node *right = get_binop_right(b);
2676 /* FIXME: Does the Conv's work only for two complement or generally? */
2678 ir_mode *r_mode = get_irn_mode(right);
2680 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2681 if (mode != r_mode) {
2682 /* This Sub is an effective Cast */
2683 n = new_r_Conv(get_nodes_block(n), n, mode);
2685 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2687 } else if (right == a) {
2688 ir_mode *l_mode = get_irn_mode(left);
2690 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2691 if (mode != l_mode) {
2692 /* This Sub is an effective Cast */
2693 n = new_r_Conv(get_nodes_block(n), n, mode);
2695 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2699 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2700 ir_mode *mode = get_irn_mode(a);
2702 if (mode == get_irn_mode(b)) {
2704 ir_node *op_a = get_Conv_op(a);
2705 ir_node *op_b = get_Conv_op(b);
2707 /* check if it's allowed to skip the conv */
2708 ma = get_irn_mode(op_a);
2709 mb = get_irn_mode(op_b);
2711 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2712 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2715 set_Sub_right(n, b);
2721 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2722 if (!is_reassoc_running() && is_Mul(a)) {
2723 ir_node *ma = get_Mul_left(a);
2724 ir_node *mb = get_Mul_right(a);
2727 ir_node *blk = get_nodes_block(n);
2728 ir_graph *irg = get_irn_irg(n);
2730 get_irn_dbg_info(n),
2734 get_irn_dbg_info(n),
2737 new_r_Const(irg, get_mode_one(mode)),
2740 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2742 } else if (mb == b) {
2743 ir_node *blk = get_nodes_block(n);
2744 ir_graph *irg = get_irn_irg(n);
2746 get_irn_dbg_info(n),
2750 get_irn_dbg_info(n),
2753 new_r_Const(irg, get_mode_one(mode)),
2756 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2760 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2761 ir_node *x = get_Sub_left(a);
2762 ir_node *y = get_Sub_right(a);
2763 ir_node *blk = get_nodes_block(n);
2764 ir_mode *m_b = get_irn_mode(b);
2765 ir_mode *m_y = get_irn_mode(y);
2769 /* Determine the right mode for the Add. */
2772 else if (mode_is_reference(m_b))
2774 else if (mode_is_reference(m_y))
2778 * Both modes are different but none is reference,
2779 * happens for instance in SubP(SubP(P, Iu), Is).
2780 * We have two possibilities here: Cast or ignore.
2781 * Currently we ignore this case.
2786 add = new_r_Add(blk, y, b, add_mode);
2788 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2789 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2793 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2794 /* c - ~X = X + (c+1) */
2795 if (is_Const(a) && is_Not(b)) {
2796 ir_tarval *tv = get_Const_tarval(a);
2798 tv = tarval_add(tv, get_mode_one(mode));
2799 if (tv != tarval_bad) {
2800 ir_node *blk = get_nodes_block(n);
2801 ir_graph *irg = get_irn_irg(n);
2802 ir_node *c = new_r_Const(irg, tv);
2803 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2804 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2808 /* x-(x&y) = x & ~y */
2810 ir_node *and_left = get_And_left(b);
2811 ir_node *and_right = get_And_right(b);
2812 if (and_right == a) {
2813 ir_node *tmp = and_left;
2814 and_left = and_right;
2817 if (and_left == a) {
2818 dbg_info *dbgi = get_irn_dbg_info(n);
2819 ir_node *block = get_nodes_block(n);
2820 ir_mode *mode = get_irn_mode(n);
2821 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2822 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2831 * Several transformation done on n*n=2n bits mul.
2832 * These transformations must be done here because new nodes may be produced.
2834 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2837 ir_node *a = get_Mul_left(n);
2838 ir_node *b = get_Mul_right(n);
2839 ir_tarval *ta = value_of(a);
2840 ir_tarval *tb = value_of(b);
2841 ir_mode *smode = get_irn_mode(a);
2843 if (ta == get_mode_one(smode)) {
2844 /* (L)1 * (L)b = (L)b */
2845 ir_node *blk = get_nodes_block(n);
2846 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2847 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2850 else if (ta == get_mode_minus_one(smode)) {
2851 /* (L)-1 * (L)b = (L)b */
2852 ir_node *blk = get_nodes_block(n);
2853 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2854 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2855 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2858 if (tb == get_mode_one(smode)) {
2859 /* (L)a * (L)1 = (L)a */
2860 ir_node *blk = get_nodes_block(a);
2861 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2865 else if (tb == get_mode_minus_one(smode)) {
2866 /* (L)a * (L)-1 = (L)-a */
2867 ir_node *blk = get_nodes_block(n);
2868 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2869 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2870 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2877 * Transform Mul(a,-1) into -a.
2878 * Do constant evaluation of Phi nodes.
2879 * Do architecture dependent optimizations on Mul nodes
2881 static ir_node *transform_node_Mul(ir_node *n)
2883 ir_node *c, *oldn = n;
2884 ir_mode *mode = get_irn_mode(n);
2885 ir_node *a = get_Mul_left(n);
2886 ir_node *b = get_Mul_right(n);
2888 n = fold_constant_associativity(n, tarval_mul);
2892 if (mode != get_irn_mode(a))
2893 return transform_node_Mul2n(n, mode);
2895 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2897 if (mode_is_signed(mode)) {
2900 if (value_of(a) == get_mode_minus_one(mode))
2902 else if (value_of(b) == get_mode_minus_one(mode))
2905 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2906 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2911 if (is_Const(b)) { /* (-a) * const -> a * -const */
2912 ir_node *cnst = const_negate(b);
2914 dbg_info *dbgi = get_irn_dbg_info(n);
2915 ir_node *block = get_nodes_block(n);
2916 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2917 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2920 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2921 dbg_info *dbgi = get_irn_dbg_info(n);
2922 ir_node *block = get_nodes_block(n);
2923 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2926 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2927 ir_node *sub_l = get_Sub_left(b);
2928 ir_node *sub_r = get_Sub_right(b);
2929 dbg_info *dbgi = get_irn_dbg_info(n);
2930 ir_node *block = get_nodes_block(n);
2931 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2932 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2933 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2936 } else if (is_Minus(b)) {
2937 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2938 ir_node *sub_l = get_Sub_left(a);
2939 ir_node *sub_r = get_Sub_right(a);
2940 dbg_info *dbgi = get_irn_dbg_info(n);
2941 ir_node *block = get_nodes_block(n);
2942 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2943 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2944 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2947 } else if (is_Shl(a)) {
2948 ir_node *const shl_l = get_Shl_left(a);
2949 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2950 /* (1 << x) * b -> b << x */
2951 dbg_info *const dbgi = get_irn_dbg_info(n);
2952 ir_node *const block = get_nodes_block(n);
2953 ir_node *const shl_r = get_Shl_right(a);
2954 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2955 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2958 } else if (is_Shl(b)) {
2959 ir_node *const shl_l = get_Shl_left(b);
2960 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2961 /* a * (1 << x) -> a << x */
2962 dbg_info *const dbgi = get_irn_dbg_info(n);
2963 ir_node *const block = get_nodes_block(n);
2964 ir_node *const shl_r = get_Shl_right(b);
2965 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2966 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2970 if (get_mode_arithmetic(mode) == irma_ieee754
2971 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
2973 ir_tarval *tv = get_Const_tarval(a);
2974 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2975 && !tarval_is_negative(tv)) {
2976 /* 2.0 * b = b + b */
2977 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2978 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2982 else if (is_Const(b)) {
2983 ir_tarval *tv = get_Const_tarval(b);
2984 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2985 && !tarval_is_negative(tv)) {
2986 /* a * 2.0 = a + a */
2987 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2993 return arch_dep_replace_mul_with_shifts(n);
2997 * Transform a Div Node.
2999 static ir_node *transform_node_Div(ir_node *n)
3001 ir_mode *mode = get_Div_resmode(n);
3002 ir_node *a = get_Div_left(n);
3003 ir_node *b = get_Div_right(n);
3005 const ir_node *dummy;
3007 if (mode_is_int(mode)) {
3008 if (is_Const(b) && is_const_Phi(a)) {
3009 /* check for Div(Phi, Const) */
3010 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3012 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3015 } else if (is_Const(a) && is_const_Phi(b)) {
3016 /* check for Div(Const, Phi) */
3017 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3019 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3022 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3023 /* check for Div(Phi, Phi) */
3024 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3026 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3031 if (a == b && value_not_zero(a, &dummy)) {
3032 ir_graph *irg = get_irn_irg(n);
3033 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3034 value = new_r_Const(irg, get_mode_one(mode));
3035 DBG_OPT_CSTEVAL(n, value);
3038 if (mode_is_signed(mode) && is_Const(b)) {
3039 ir_tarval *tv = get_Const_tarval(b);
3041 if (tv == get_mode_minus_one(mode)) {
3043 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3044 DBG_OPT_CSTEVAL(n, value);
3048 /* Try architecture dependent optimization */
3049 value = arch_dep_replace_div_by_const(n);
3052 assert(mode_is_float(mode));
3054 /* Optimize x/c to x*(1/c) */
3055 ir_tarval *tv = value_of(b);
3057 if (tv != tarval_bad) {
3058 tv = tarval_div(get_mode_one(mode), tv);
3060 /* Do the transformation if the result is either exact or we are
3061 not using strict rules. */
3062 if (tv != tarval_bad &&
3063 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3064 ir_node *block = get_nodes_block(n);
3065 ir_graph *irg = get_irn_irg(block);
3066 ir_node *c = new_r_Const(irg, tv);
3067 dbg_info *dbgi = get_irn_dbg_info(n);
3068 value = new_rd_Mul(dbgi, block, a, c, mode);
3080 /* Turn Div into a tuple (mem, jmp, bad, value) */
3081 mem = get_Div_mem(n);
3082 blk = get_nodes_block(n);
3083 irg = get_irn_irg(blk);
3085 /* skip a potential Pin */
3086 mem = skip_Pin(mem);
3087 ir_node *const in[] = {
3089 [pn_Div_res] = value,
3090 [pn_Div_X_regular] = new_r_Jmp(blk),
3091 [pn_Div_X_except] = new_r_Bad(irg, mode_X),
3093 turn_into_tuple(n, ARRAY_SIZE(in), in);
3099 * Transform a Mod node.
3101 static ir_node *transform_node_Mod(ir_node *n)
3103 ir_mode *mode = get_Mod_resmode(n);
3104 ir_node *a = get_Mod_left(n);
3105 ir_node *b = get_Mod_right(n);
3110 if (is_Const(b) && is_const_Phi(a)) {
3111 /* check for Div(Phi, Const) */
3112 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3114 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3118 else if (is_Const(a) && is_const_Phi(b)) {
3119 /* check for Div(Const, Phi) */
3120 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3122 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3126 else if (is_const_Phi(a) && is_const_Phi(b)) {
3127 /* check for Div(Phi, Phi) */
3128 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3130 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3137 irg = get_irn_irg(n);
3138 if (tv != tarval_bad) {
3139 value = new_r_Const(irg, tv);
3141 DBG_OPT_CSTEVAL(n, value);
3144 ir_node *a = get_Mod_left(n);
3145 ir_node *b = get_Mod_right(n);
3146 const ir_node *dummy;
3148 if (a == b && value_not_zero(a, &dummy)) {
3149 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3150 value = new_r_Const(irg, get_mode_null(mode));
3151 DBG_OPT_CSTEVAL(n, value);
3154 if (mode_is_signed(mode) && is_Const(b)) {
3155 ir_tarval *tv = get_Const_tarval(b);
3157 if (tv == get_mode_minus_one(mode)) {
3159 value = new_r_Const(irg, get_mode_null(mode));
3160 DBG_OPT_CSTEVAL(n, value);
3164 /* Try architecture dependent optimization */
3165 value = arch_dep_replace_mod_by_const(n);
3174 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3175 mem = get_Mod_mem(n);
3176 blk = get_nodes_block(n);
3177 irg = get_irn_irg(blk);
3179 /* skip a potential Pin */
3180 mem = skip_Pin(mem);
3181 ir_node *const in[] = {
3183 [pn_Mod_res] = value,
3184 [pn_Mod_X_regular] = new_r_Jmp(blk),
3185 [pn_Mod_X_except] = new_r_Bad(irg, mode_X),
3187 turn_into_tuple(n, ARRAY_SIZE(in), in);
3193 * Transform a Cond node.
3195 * Replace the Cond by a Jmp if it branches on a constant
3198 static ir_node *transform_node_Cond(ir_node *n)
3200 ir_node *a = get_Cond_selector(n);
3201 ir_graph *irg = get_irn_irg(n);
3204 /* we need block info which is not available in floating irgs */
3205 if (get_irg_pinned(irg) == op_pin_state_floats)
3209 if (ta == tarval_bad && is_Cmp(a)) {
3210 /* try again with a direct call to compute_cmp, as we don't care
3211 * about the MODEB_LOWERED flag here */
3212 ta = compute_cmp_ext(a);
3215 if (ta != tarval_bad) {
3216 /* It's branching on a boolean constant.
3217 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3218 ir_node *const blk = get_nodes_block(n);
3219 ir_node *const jmp = new_r_Jmp(blk);
3220 ir_node *const bad = new_r_Bad(irg, mode_X);
3221 bool const cond = ta == tarval_b_true;
3222 ir_node *const in[] = {
3223 [pn_Cond_false] = cond ? bad : jmp,
3224 [pn_Cond_true] = cond ? jmp : bad,
3226 turn_into_tuple(n, ARRAY_SIZE(in), in);
3227 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3232 static ir_node *transform_node_Switch(ir_node *n)
3234 ir_node *op = get_Switch_selector(n);
3235 ir_tarval *val = value_of(op);
3236 if (val != tarval_bad) {
3237 dbg_info *dbgi = get_irn_dbg_info(n);
3238 ir_graph *irg = get_irn_irg(n);
3239 unsigned n_outs = get_Switch_n_outs(n);
3240 ir_node *block = get_nodes_block(n);
3241 ir_node *bad = new_r_Bad(irg, mode_X);
3242 ir_node **in = XMALLOCN(ir_node*, n_outs);
3243 const ir_switch_table *table = get_Switch_table(n);
3244 size_t n_entries = ir_switch_table_get_n_entries(table);
3248 for (i = 0; i < n_entries; ++i) {
3249 const ir_switch_table_entry *entry
3250 = ir_switch_table_get_entry_const(table, i);
3251 ir_tarval *min = entry->min;
3252 ir_tarval *max = entry->max;
3255 if ((min == max && min == val)
3256 || (tarval_cmp(val, min) != ir_relation_less
3257 && tarval_cmp(val, max) != ir_relation_greater)) {
3262 for (o = 0; o < n_outs; ++o) {
3263 if (o == (unsigned)jmp_pn) {
3264 in[o] = new_rd_Jmp(dbgi, block);
3269 return new_r_Tuple(block, (int)n_outs, in);
3275 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3277 * - and, or, xor instead of &
3278 * - Shl, Shr, Shrs, rotl instead of >>
3279 * (with a special case for Or/Xor + Shrs)
3281 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3283 static ir_node *transform_node_shift_bitop(ir_node *n)
3285 ir_graph *irg = get_irn_irg(n);
3286 ir_node *right = get_binop_right(n);
3287 ir_mode *mode = get_irn_mode(n);
3289 ir_node *bitop_left;
3290 ir_node *bitop_right;
3299 ir_tarval *tv_shift;
3301 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3304 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3306 if (!is_Const(right))
3309 left = get_binop_left(n);
3310 op_left = get_irn_op(left);
3311 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3314 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3315 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3316 /* TODO: test if sign bit is affectes */
3320 bitop_right = get_binop_right(left);
3321 if (!is_Const(bitop_right))
3324 bitop_left = get_binop_left(left);
3326 block = get_nodes_block(n);
3327 dbgi = get_irn_dbg_info(n);
3328 tv1 = get_Const_tarval(bitop_right);
3329 tv2 = get_Const_tarval(right);
3331 assert(get_tarval_mode(tv1) == mode);
3334 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3335 tv_shift = tarval_shl(tv1, tv2);
3336 } else if (is_Shr(n)) {
3337 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3338 tv_shift = tarval_shr(tv1, tv2);
3339 } else if (is_Shrs(n)) {
3340 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3341 tv_shift = tarval_shrs(tv1, tv2);
3344 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3345 tv_shift = tarval_rotl(tv1, tv2);
3348 assert(get_tarval_mode(tv_shift) == mode);
3349 irg = get_irn_irg(n);
3350 new_const = new_r_Const(irg, tv_shift);
3352 if (op_left == op_And) {
3353 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3354 } else if (op_left == op_Or) {
3355 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3357 assert(op_left == op_Eor);
3358 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3367 static ir_node *transform_node_And(ir_node *n)
3369 ir_node *c, *oldn = n;
3370 ir_node *a = get_And_left(n);
3371 ir_node *b = get_And_right(n);
3374 n = fold_constant_associativity(n, tarval_and);
3378 if (is_Cmp(a) && is_Cmp(b)) {
3379 ir_node *a_left = get_Cmp_left(a);
3380 ir_node *a_right = get_Cmp_right(a);
3381 ir_node *b_left = get_Cmp_left(b);
3382 ir_node *b_right = get_Cmp_right(b);
3383 ir_relation a_relation = get_Cmp_relation(a);
3384 ir_relation b_relation = get_Cmp_relation(b);
3385 /* we can combine the relations of two compares with the same
3387 if (a_left == b_left && b_left == b_right) {
3388 dbg_info *dbgi = get_irn_dbg_info(n);
3389 ir_node *block = get_nodes_block(n);
3390 ir_relation new_relation = a_relation & b_relation;
3391 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3393 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3394 if (a_relation == b_relation && a_relation == ir_relation_equal
3395 && !mode_is_float(get_irn_mode(a_left))
3396 && !mode_is_float(get_irn_mode(b_left))) {
3397 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3398 dbg_info *dbgi = get_irn_dbg_info(n);
3399 ir_node *block = get_nodes_block(n);
3400 ir_mode *a_mode = get_irn_mode(a_left);
3401 ir_mode *b_mode = get_irn_mode(b_left);
3402 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3403 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3404 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3405 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3406 ir_graph *irg = get_irn_irg(n);
3407 ir_node *zero = create_zero_const(irg, b_mode);
3408 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3410 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3411 dbg_info *dbgi = get_irn_dbg_info(n);
3412 ir_node *block = get_nodes_block(n);
3413 ir_mode *a_mode = get_irn_mode(a_left);
3414 ir_mode *b_mode = get_irn_mode(b_left);
3415 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3416 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3417 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3418 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3419 ir_graph *irg = get_irn_irg(n);
3420 ir_node *zero = create_zero_const(irg, a_mode);
3421 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3426 mode = get_irn_mode(n);
3427 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3429 if (is_Or(a) || is_Or_Eor_Add(a)) {
3430 ir_node *or_left = get_binop_left(a);
3431 ir_node *or_right = get_binop_right(a);
3432 if (complement_values(or_left, b)) {
3433 /* (a|b) & ~a => b & ~a */
3434 dbg_info *dbgi = get_irn_dbg_info(n);
3435 ir_node *block = get_nodes_block(n);
3436 return new_rd_And(dbgi, block, or_right, b, mode);
3437 } else if (complement_values(or_right, b)) {
3438 /* (a|b) & ~b => a & ~b */
3439 dbg_info *dbgi = get_irn_dbg_info(n);
3440 ir_node *block = get_nodes_block(n);
3441 return new_rd_And(dbgi, block, or_left, b, mode);
3442 } else if (is_Not(b)) {
3443 ir_node *op = get_Not_op(b);
3445 ir_node *ba = get_And_left(op);
3446 ir_node *bb = get_And_right(op);
3448 /* it's enough to test the following cases due to normalization! */
3449 if (or_left == ba && or_right == bb) {
3450 /* (a|b) & ~(a&b) = a^b */
3451 ir_node *block = get_nodes_block(n);
3453 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3454 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3460 if (is_Or(b) || is_Or_Eor_Add(b)) {
3461 ir_node *or_left = get_binop_left(b);
3462 ir_node *or_right = get_binop_right(b);
3463 if (complement_values(or_left, a)) {
3464 /* (a|b) & ~a => b & ~a */
3465 dbg_info *dbgi = get_irn_dbg_info(n);
3466 ir_node *block = get_nodes_block(n);
3467 return new_rd_And(dbgi, block, or_right, a, mode);
3468 } else if (complement_values(or_right, a)) {
3469 /* (a|b) & ~b => a & ~b */
3470 dbg_info *dbgi = get_irn_dbg_info(n);
3471 ir_node *block = get_nodes_block(n);
3472 return new_rd_And(dbgi, block, or_left, a, mode);
3473 } else if (is_Not(a)) {
3474 ir_node *op = get_Not_op(a);
3476 ir_node *aa = get_And_left(op);
3477 ir_node *ab = get_And_right(op);
3479 /* it's enough to test the following cases due to normalization! */
3480 if (or_left == aa && or_right == ab) {
3481 /* (a|b) & ~(a&b) = a^b */
3482 ir_node *block = get_nodes_block(n);
3484 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3485 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3491 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3492 ir_node *al = get_binop_left(a);
3493 ir_node *ar = get_binop_right(a);
3496 /* (b ^ a) & b -> ~a & b */
3497 dbg_info *dbg = get_irn_dbg_info(n);
3498 ir_node *block = get_nodes_block(n);
3500 ar = new_rd_Not(dbg, block, ar, mode);
3501 n = new_rd_And(dbg, block, ar, b, mode);
3502 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3506 /* (a ^ b) & b -> ~a & b */
3507 dbg_info *dbg = get_irn_dbg_info(n);
3508 ir_node *block = get_nodes_block(n);
3510 al = new_rd_Not(dbg, block, al, mode);
3511 n = new_rd_And(dbg, block, al, b, mode);
3512 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3516 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3517 ir_node *bl = get_binop_left(b);
3518 ir_node *br = get_binop_right(b);
3521 /* a & (a ^ b) -> a & ~b */
3522 dbg_info *dbg = get_irn_dbg_info(n);
3523 ir_node *block = get_nodes_block(n);
3525 br = new_rd_Not(dbg, block, br, mode);
3526 n = new_rd_And(dbg, block, br, a, mode);
3527 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3531 /* a & (b ^ a) -> a & ~b */
3532 dbg_info *dbg = get_irn_dbg_info(n);
3533 ir_node *block = get_nodes_block(n);
3535 bl = new_rd_Not(dbg, block, bl, mode);
3536 n = new_rd_And(dbg, block, bl, a, mode);
3537 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3541 if (is_Not(a) && is_Not(b)) {
3542 /* ~a & ~b = ~(a|b) */
3543 ir_node *block = get_nodes_block(n);
3544 ir_mode *mode = get_irn_mode(n);
3548 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3549 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3555 vrp_attr *b_vrp = vrp_get_info(b);
3556 ir_tarval *a_val = get_Const_tarval(a);
3557 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3563 vrp_attr *a_vrp = vrp_get_info(a);
3564 ir_tarval *b_val = get_Const_tarval(b);
3565 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3570 n = transform_bitwise_distributive(n, transform_node_And);
3572 n = transform_node_bitop_shift(n);
3580 static ir_node *transform_node_Not(ir_node *n)
3582 ir_node *c, *oldn = n;
3583 ir_node *a = get_Not_op(n);
3584 ir_mode *mode = get_irn_mode(n);
3586 HANDLE_UNOP_PHI(tarval_not,a,c);
3588 /* check for a boolean Not */
3590 dbg_info *dbgi = get_irn_dbg_info(a);
3591 ir_node *block = get_nodes_block(a);
3592 ir_relation relation = get_Cmp_relation(a);
3593 relation = get_negated_relation(relation);
3594 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3595 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3599 /* normalize ~(a ^ b) => a ^ ~b */
3600 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3601 dbg_info *dbg = get_irn_dbg_info(n);
3602 ir_node *block = get_nodes_block(n);
3603 ir_node *eor_right = get_binop_right(a);
3604 ir_node *eor_left = get_binop_left(a);
3605 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3606 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3610 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3611 if (is_Minus(a)) { /* ~-x -> x + -1 */
3612 dbg_info *dbg = get_irn_dbg_info(n);
3613 ir_graph *irg = get_irn_irg(n);
3614 ir_node *block = get_nodes_block(n);
3615 ir_node *add_l = get_Minus_op(a);
3616 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3617 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3618 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3619 ir_node *add_r = get_binop_right(a);
3620 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3621 /* ~(x + -1) = -x */
3622 ir_node *op = get_binop_left(a);
3623 ir_node *blk = get_nodes_block(n);
3624 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3625 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3633 * Transform a Minus.
3637 * -(a >>u (size-1)) = a >>s (size-1)
3638 * -(a >>s (size-1)) = a >>u (size-1)
3639 * -(a * const) -> a * -const
3641 static ir_node *transform_node_Minus(ir_node *n)
3643 ir_node *c, *oldn = n;
3644 ir_node *a = get_Minus_op(n);
3647 HANDLE_UNOP_PHI(tarval_neg,a,c);
3649 mode = get_irn_mode(a);
3650 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3651 /* the following rules are only to twos-complement */
3654 ir_node *op = get_Not_op(a);
3655 ir_tarval *tv = get_mode_one(mode);
3656 ir_node *blk = get_nodes_block(n);
3657 ir_graph *irg = get_irn_irg(blk);
3658 ir_node *c = new_r_Const(irg, tv);
3659 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3660 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3664 ir_node *c = get_Shr_right(a);
3667 ir_tarval *tv = get_Const_tarval(c);
3669 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3670 /* -(a >>u (size-1)) = a >>s (size-1) */
3671 ir_node *v = get_Shr_left(a);
3673 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3674 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3680 ir_node *c = get_Shrs_right(a);
3683 ir_tarval *tv = get_Const_tarval(c);
3685 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3686 /* -(a >>s (size-1)) = a >>u (size-1) */
3687 ir_node *v = get_Shrs_left(a);
3689 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3690 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3697 /* - (a-b) = b - a */
3698 ir_node *la = get_Sub_left(a);
3699 ir_node *ra = get_Sub_right(a);
3700 ir_node *blk = get_nodes_block(n);
3702 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3703 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3707 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3708 ir_node *mul_l = get_Mul_left(a);
3709 ir_node *mul_r = get_Mul_right(a);
3710 ir_tarval *tv = value_of(mul_r);
3711 if (tv != tarval_bad) {
3712 tv = tarval_neg(tv);
3713 if (tv != tarval_bad) {
3714 ir_graph *irg = get_irn_irg(n);
3715 ir_node *cnst = new_r_Const(irg, tv);
3716 dbg_info *dbg = get_irn_dbg_info(a);
3717 ir_node *block = get_nodes_block(a);
3718 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3719 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3729 * Transform a Proj(Load) with a non-null address.
3731 static ir_node *transform_node_Proj_Load(ir_node *proj)
3733 if (get_irn_mode(proj) == mode_X) {
3734 ir_node *load = get_Proj_pred(proj);
3736 /* get the Load address */
3737 const ir_node *addr = get_Load_ptr(load);
3738 const ir_node *confirm;
3740 if (value_not_null(addr, &confirm)) {
3741 if (confirm == NULL) {
3742 /* this node may float if it did not depend on a Confirm */
3743 set_irn_pinned(load, op_pin_state_floats);
3745 if (get_Proj_proj(proj) == pn_Load_X_except) {
3746 ir_graph *irg = get_irn_irg(proj);
3747 DBG_OPT_EXC_REM(proj);
3748 return new_r_Bad(irg, mode_X);
3750 ir_node *blk = get_nodes_block(load);
3751 return new_r_Jmp(blk);
3759 * Transform a Proj(Store) with a non-null address.
3761 static ir_node *transform_node_Proj_Store(ir_node *proj)
3763 if (get_irn_mode(proj) == mode_X) {
3764 ir_node *store = get_Proj_pred(proj);
3766 /* get the load/store address */
3767 const ir_node *addr = get_Store_ptr(store);
3768 const ir_node *confirm;
3770 if (value_not_null(addr, &confirm)) {
3771 if (confirm == NULL) {
3772 /* this node may float if it did not depend on a Confirm */
3773 set_irn_pinned(store, op_pin_state_floats);
3775 if (get_Proj_proj(proj) == pn_Store_X_except) {
3776 ir_graph *irg = get_irn_irg(proj);
3777 DBG_OPT_EXC_REM(proj);
3778 return new_r_Bad(irg, mode_X);
3780 ir_node *blk = get_nodes_block(store);
3781 return new_r_Jmp(blk);
3789 * Transform a Proj(Div) with a non-zero value.
3790 * Removes the exceptions and routes the memory to the NoMem node.
3792 static ir_node *transform_node_Proj_Div(ir_node *proj)
3794 ir_node *div = get_Proj_pred(proj);
3795 ir_node *b = get_Div_right(div);
3796 ir_node *res, *new_mem;
3797 const ir_node *confirm;
3800 if (value_not_zero(b, &confirm)) {
3801 /* div(x, y) && y != 0 */
3802 if (confirm == NULL) {
3803 /* we are sure we have a Const != 0 */
3804 new_mem = get_Div_mem(div);
3805 new_mem = skip_Pin(new_mem);
3806 set_Div_mem(div, new_mem);
3807 set_irn_pinned(div, op_pin_state_floats);
3810 proj_nr = get_Proj_proj(proj);
3812 case pn_Div_X_regular:
3813 return new_r_Jmp(get_nodes_block(div));
3815 case pn_Div_X_except: {
3816 ir_graph *irg = get_irn_irg(proj);
3817 /* we found an exception handler, remove it */
3818 DBG_OPT_EXC_REM(proj);
3819 return new_r_Bad(irg, mode_X);
3823 ir_graph *irg = get_irn_irg(proj);
3824 res = get_Div_mem(div);
3825 new_mem = get_irg_no_mem(irg);
3828 /* This node can only float up to the Confirm block */
3829 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3831 set_irn_pinned(div, op_pin_state_floats);
3832 /* this is a Div without exception, we can remove the memory edge */
3833 set_Div_mem(div, new_mem);
3842 * Transform a Proj(Mod) with a non-zero value.
3843 * Removes the exceptions and routes the memory to the NoMem node.
3845 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3847 ir_node *mod = get_Proj_pred(proj);
3848 ir_node *b = get_Mod_right(mod);
3849 ir_node *res, *new_mem;
3850 const ir_node *confirm;
3853 if (value_not_zero(b, &confirm)) {
3854 /* mod(x, y) && y != 0 */
3855 proj_nr = get_Proj_proj(proj);
3857 if (confirm == NULL) {
3858 /* we are sure we have a Const != 0 */
3859 new_mem = get_Mod_mem(mod);
3860 new_mem = skip_Pin(new_mem);
3861 set_Mod_mem(mod, new_mem);
3862 set_irn_pinned(mod, op_pin_state_floats);
3867 case pn_Mod_X_regular:
3868 return new_r_Jmp(get_nodes_block(mod));
3870 case pn_Mod_X_except: {
3871 ir_graph *irg = get_irn_irg(proj);
3872 /* we found an exception handler, remove it */
3873 DBG_OPT_EXC_REM(proj);
3874 return new_r_Bad(irg, mode_X);
3878 ir_graph *irg = get_irn_irg(proj);
3879 res = get_Mod_mem(mod);
3880 new_mem = get_irg_no_mem(irg);
3883 /* This node can only float up to the Confirm block */
3884 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3886 /* this is a Mod without exception, we can remove the memory edge */
3887 set_Mod_mem(mod, new_mem);
3891 if (get_Mod_left(mod) == b) {
3892 /* a % a = 0 if a != 0 */
3893 ir_graph *irg = get_irn_irg(proj);
3894 ir_mode *mode = get_irn_mode(proj);
3895 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3897 DBG_OPT_CSTEVAL(mod, res);
3906 * return true if the operation returns a value with exactly 1 bit set
3908 static bool is_single_bit(const ir_node *node)
3910 /* a first implementation, could be extended with vrp and others... */
3912 ir_node *shl_l = get_Shl_left(node);
3913 ir_mode *mode = get_irn_mode(node);
3914 int modulo = get_mode_modulo_shift(mode);
3915 /* this works if we shift a 1 and we have modulo shift */
3916 if (is_Const(shl_l) && is_Const_one(shl_l)
3917 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3920 } else if (is_Const(node)) {
3921 ir_tarval *tv = get_Const_tarval(node);
3922 return tarval_is_single_bit(tv);
3928 * checks if node just flips a bit in another node and returns that other node
3929 * if so. @p tv should be a value having just 1 bit set
3931 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
3934 return get_Not_op(node);
3936 ir_node *right = get_Eor_right(node);
3937 if (is_Const(right)) {
3938 ir_tarval *right_tv = get_Const_tarval(right);
3939 ir_mode *mode = get_irn_mode(node);
3940 if (tarval_and(right_tv, tv) != get_mode_null(mode))
3941 return get_Eor_left(node);
3948 * Normalizes and optimizes Cmp nodes.
3950 static ir_node *transform_node_Cmp(ir_node *n)
3952 ir_node *left = get_Cmp_left(n);
3953 ir_node *right = get_Cmp_right(n);
3954 ir_mode *mode = get_irn_mode(left);
3955 ir_tarval *tv = NULL;
3956 bool changed = false;
3957 bool changedc = false;
3958 ir_relation relation = get_Cmp_relation(n);
3959 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3961 /* mask out impossible relations */
3962 ir_relation new_relation = relation & possible;
3963 if (new_relation != relation) {
3964 relation = new_relation;
3968 /* Remove unnecessary conversions */
3969 if (!mode_is_float(mode)
3970 || be_get_backend_param()->mode_float_arithmetic == NULL) {
3971 if (is_Conv(left) && is_Conv(right)) {
3972 ir_node *op_left = get_Conv_op(left);
3973 ir_node *op_right = get_Conv_op(right);
3974 ir_mode *mode_left = get_irn_mode(op_left);
3975 ir_mode *mode_right = get_irn_mode(op_right);
3977 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3978 && mode_left != mode_b && mode_right != mode_b) {
3979 ir_node *block = get_nodes_block(n);
3981 if (mode_left == mode_right) {
3985 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3986 } else if (smaller_mode(mode_left, mode_right)) {
3987 left = new_r_Conv(block, op_left, mode_right);
3990 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3991 } else if (smaller_mode(mode_right, mode_left)) {
3993 right = new_r_Conv(block, op_right, mode_left);
3995 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3997 mode = get_irn_mode(left);
4000 if (is_Conv(left) && is_Const(right)) {
4001 ir_node *op_left = get_Conv_op(left);
4002 ir_mode *mode_left = get_irn_mode(op_left);
4003 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4004 ir_tarval *tv = get_Const_tarval(right);
4005 tarval_int_overflow_mode_t last_mode
4006 = tarval_get_integer_overflow_mode();
4008 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4009 new_tv = tarval_convert_to(tv, mode_left);
4010 tarval_set_integer_overflow_mode(last_mode);
4011 if (new_tv != tarval_bad) {
4012 ir_graph *irg = get_irn_irg(n);
4014 right = new_r_Const(irg, new_tv);
4015 mode = get_irn_mode(left);
4017 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4024 * Optimize -a CMP -b into b CMP a.
4025 * This works only for modes where unary Minus cannot Overflow.
4026 * Note that two-complement integers can Overflow so it will NOT work.
4028 if (!mode_overflow_on_unary_Minus(mode) &&
4029 is_Minus(left) && is_Minus(right)) {
4030 left = get_Minus_op(left);
4031 right = get_Minus_op(right);
4032 relation = get_inversed_relation(relation);
4034 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4037 /* remove operation on both sides if possible */
4038 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4040 * The following operations are NOT safe for floating point operations, for instance
4041 * 1.0 + inf == 2.0 + inf, =/=> x == y
4043 if (mode_is_int(mode)) {
4044 unsigned lop = get_irn_opcode(left);
4046 if (lop == get_irn_opcode(right)) {
4047 ir_node *ll, *lr, *rl, *rr;
4049 /* same operation on both sides, try to remove */
4053 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4054 left = get_unop_op(left);
4055 right = get_unop_op(right);
4057 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4060 ll = get_Add_left(left);
4061 lr = get_Add_right(left);
4062 rl = get_Add_left(right);
4063 rr = get_Add_right(right);
4066 /* X + a CMP X + b ==> a CMP b */
4070 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4071 } else if (ll == rr) {
4072 /* X + a CMP b + X ==> a CMP b */
4076 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4077 } else if (lr == rl) {
4078 /* a + X CMP X + b ==> a CMP b */
4082 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4083 } else if (lr == rr) {
4084 /* a + X CMP b + X ==> a CMP b */
4088 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4092 ll = get_Sub_left(left);
4093 lr = get_Sub_right(left);
4094 rl = get_Sub_left(right);
4095 rr = get_Sub_right(right);
4098 /* X - a CMP X - b ==> a CMP b */
4102 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4103 } else if (lr == rr) {
4104 /* a - X CMP b - X ==> a CMP b */
4108 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4112 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4113 /* a ROTL X CMP b ROTL X ==> a CMP b */
4114 left = get_Rotl_left(left);
4115 right = get_Rotl_left(right);
4117 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4125 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4126 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4127 ir_node *ll = get_binop_left(left);
4128 ir_node *lr = get_binop_right(left);
4130 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4136 ir_graph *irg = get_irn_irg(n);
4138 right = create_zero_const(irg, mode);
4140 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4143 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4144 ir_node *rl = get_binop_left(right);
4145 ir_node *rr = get_binop_right(right);
4147 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4153 ir_graph *irg = get_irn_irg(n);
4155 right = create_zero_const(irg, mode);
4157 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4161 if (is_And(left) && is_Const(right)) {
4162 ir_node *ll = get_binop_left(left);
4163 ir_node *lr = get_binop_right(left);
4164 if (is_Shr(ll) && is_Const(lr)) {
4165 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4166 ir_node *block = get_nodes_block(n);
4167 ir_mode *mode = get_irn_mode(left);
4169 ir_node *llr = get_Shr_right(ll);
4170 if (is_Const(llr)) {
4171 dbg_info *dbg = get_irn_dbg_info(left);
4172 ir_graph *irg = get_irn_irg(left);
4174 ir_tarval *c1 = get_Const_tarval(llr);
4175 ir_tarval *c2 = get_Const_tarval(lr);
4176 ir_tarval *c3 = get_Const_tarval(right);
4177 ir_tarval *mask = tarval_shl(c2, c1);
4178 ir_tarval *value = tarval_shl(c3, c1);
4180 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4181 right = new_r_Const(irg, value);
4186 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4188 if (is_Const(right) && is_Const_null(right) &&
4189 (is_Eor(left) || is_Or_Eor_Add(left))) {
4190 right = get_Eor_right(left);
4191 left = get_Eor_left(left);
4197 if (mode_is_int(mode) && is_And(left)) {
4198 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4199 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4200 if (relation == ir_relation_equal
4201 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4202 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4203 ir_node *and0 = get_And_left(left);
4204 ir_node *and1 = get_And_right(left);
4205 if (and1 == right) {
4206 ir_node *tmp = and0;
4210 if (and0 == right && is_single_bit(and0)) {
4211 ir_graph *irg = get_irn_irg(n);
4213 relation == ir_relation_equal ? ir_relation_less_greater
4214 : ir_relation_equal;
4215 right = create_zero_const(irg, mode);
4221 if (is_Const(right) && is_Const_null(right) &&
4222 (relation == ir_relation_equal
4223 || (relation == ir_relation_less_greater)
4224 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4226 /* instead of flipping the bit before the bit-test operation negate
4228 ir_node *and0 = get_And_left(left);
4229 ir_node *and1 = get_And_right(left);
4230 if (is_Const(and1)) {
4231 ir_tarval *tv = get_Const_tarval(and1);
4232 if (tarval_is_single_bit(tv)) {
4233 ir_node *flipped = flips_bit(and0, tv);
4234 if (flipped != NULL) {
4235 dbg_info *dbgi = get_irn_dbg_info(left);
4236 ir_node *block = get_nodes_block(left);
4237 relation = get_negated_relation(relation);
4238 left = new_rd_And(dbgi, block, flipped, and1, mode);
4247 /* replace mode_b compares with ands/ors */
4248 if (mode == mode_b) {
4249 ir_node *block = get_nodes_block(n);
4253 case ir_relation_less_equal:
4254 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4256 case ir_relation_less:
4257 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4259 case ir_relation_greater_equal:
4260 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4262 case ir_relation_greater:
4263 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4265 case ir_relation_less_greater:
4266 bres = new_r_Eor(block, left, right, mode_b);
4268 case ir_relation_equal:
4269 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4272 #ifdef DEBUG_libfirm
4273 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4278 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4284 * First step: normalize the compare op
4285 * by placing the constant on the right side
4286 * or moving the lower address node to the left.
4288 if (!operands_are_normalized(left, right)) {
4293 relation = get_inversed_relation(relation);
4298 * Second step: Try to reduce the magnitude
4299 * of a constant. This may help to generate better code
4300 * later and may help to normalize more compares.
4301 * Of course this is only possible for integer values.
4303 tv = value_of(right);
4304 if (tv != tarval_bad) {
4305 ir_mode *mode = get_irn_mode(right);
4307 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4308 * cmp(ct,c2) | cmp(cf,c2) | result
4309 * -----------|------------|--------
4310 * true | true | True
4311 * false | false | False
4313 * false | true | not(x)
4316 ir_node *mux_true = get_Mux_true(left);
4317 ir_node *mux_false = get_Mux_false(left);
4318 if (is_Const(mux_true) && is_Const(mux_false)) {
4319 /* we can fold true/false constant separately */
4320 ir_tarval *tv_true = get_Const_tarval(mux_true);
4321 ir_tarval *tv_false = get_Const_tarval(mux_false);
4322 ir_relation r_true = tarval_cmp(tv_true, tv);
4323 ir_relation r_false = tarval_cmp(tv_false, tv);
4324 if (r_true != ir_relation_false
4325 || r_false != ir_relation_false) {
4326 bool rel_true = (r_true & relation) != 0;
4327 bool rel_false = (r_false & relation) != 0;
4328 ir_node *cond = get_Mux_sel(left);
4329 if (rel_true == rel_false) {
4330 relation = rel_true ? ir_relation_true
4331 : ir_relation_false;
4332 } else if (rel_true) {
4335 dbg_info *dbgi = get_irn_dbg_info(n);
4336 ir_node *block = get_nodes_block(n);
4337 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4344 /* TODO extend to arbitrary constants */
4345 if (is_Conv(left) && tarval_is_null(tv)) {
4346 ir_node *op = get_Conv_op(left);
4347 ir_mode *op_mode = get_irn_mode(op);
4350 * UpConv(x) REL 0 ==> x REL 0
4351 * Don't do this for float values as it's unclear whether it is a
4352 * win. (on the other side it makes detection/creation of fabs hard)
4354 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4355 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4356 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4357 !mode_is_float(mode)) {
4358 tv = get_mode_null(op_mode);
4362 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4366 if (tv != tarval_bad) {
4367 /* the following optimization is possible on modes without Overflow
4368 * on Unary Minus or on == and !=:
4369 * -a CMP c ==> a swap(CMP) -c
4371 * Beware: for two-complement Overflow may occur, so only == and != can
4372 * be optimized, see this:
4373 * -MININT < 0 =/=> MININT > 0 !!!
4375 if (is_Minus(left) &&
4376 (!mode_overflow_on_unary_Minus(mode) ||
4377 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4378 tv = tarval_neg(tv);
4380 if (tv != tarval_bad) {
4381 left = get_Minus_op(left);
4382 relation = get_inversed_relation(relation);
4384 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4386 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4387 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4388 tv = tarval_not(tv);
4390 if (tv != tarval_bad) {
4391 left = get_Not_op(left);
4393 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4397 /* for integer modes, we have more */
4398 if (mode_is_int(mode) && !is_Const(left)) {
4399 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4400 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4401 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4402 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4404 if (tv != tarval_bad) {
4405 relation ^= ir_relation_equal;
4407 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4410 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4411 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4412 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4413 tv = tarval_add(tv, get_mode_one(mode));
4415 if (tv != tarval_bad) {
4416 relation ^= ir_relation_equal;
4418 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4422 /* the following reassociations work only for == and != */
4423 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4424 if (tv != tarval_bad) {
4425 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4427 ir_node *c1 = get_Sub_right(left);
4428 ir_tarval *tv2 = value_of(c1);
4430 if (tv2 != tarval_bad) {
4431 tv2 = tarval_add(tv, value_of(c1));
4433 if (tv2 != tarval_bad) {
4434 left = get_Sub_left(left);
4437 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4441 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4442 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4443 ir_node *a_l = get_binop_left(left);
4444 ir_node *a_r = get_binop_right(left);
4448 if (is_Const(a_l)) {
4450 tv2 = value_of(a_l);
4453 tv2 = value_of(a_r);
4456 if (tv2 != tarval_bad) {
4457 tv2 = tarval_sub(tv, tv2, NULL);
4459 if (tv2 != tarval_bad) {
4463 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4467 /* -a == c ==> a == -c, -a != c ==> a != -c */
4468 else if (is_Minus(left)) {
4469 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4471 if (tv2 != tarval_bad) {
4472 left = get_Minus_op(left);
4475 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4482 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4483 switch (get_irn_opcode(left)) {
4487 c1 = get_And_right(left);
4490 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4491 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4493 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4495 /* TODO: move to constant evaluation */
4496 ir_graph *irg = get_irn_irg(n);
4497 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4498 c1 = new_r_Const(irg, tv);
4499 DBG_OPT_CSTEVAL(n, c1);
4503 if (tarval_is_single_bit(tv)) {
4505 * optimization for AND:
4507 * And(x, C) == C ==> And(x, C) != 0
4508 * And(x, C) != C ==> And(X, C) == 0
4510 * if C is a single Bit constant.
4513 /* check for Constant's match. We have check hare the tarvals,
4514 because our const might be changed */
4515 if (get_Const_tarval(c1) == tv) {
4516 /* fine: do the transformation */
4517 tv = get_mode_null(get_tarval_mode(tv));
4518 relation ^= ir_relation_less_equal_greater;
4520 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4526 c1 = get_Or_right(left);
4527 if (is_Const(c1) && tarval_is_null(tv)) {
4529 * Or(x, C) == 0 && C != 0 ==> FALSE
4530 * Or(x, C) != 0 && C != 0 ==> TRUE
4532 if (! tarval_is_null(get_Const_tarval(c1))) {
4533 /* TODO: move to constant evaluation */
4534 ir_graph *irg = get_irn_irg(n);
4535 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4536 c1 = new_r_Const(irg, tv);
4537 DBG_OPT_CSTEVAL(n, c1);
4544 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4546 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4549 c1 = get_Shl_right(left);
4551 ir_graph *irg = get_irn_irg(c1);
4552 ir_tarval *tv1 = get_Const_tarval(c1);
4553 ir_mode *mode = get_irn_mode(left);
4554 ir_tarval *minus1 = get_mode_all_one(mode);
4555 ir_tarval *amask = tarval_shr(minus1, tv1);
4556 ir_tarval *cmask = tarval_shl(minus1, tv1);
4559 if (tarval_and(tv, cmask) != tv) {
4560 /* condition not met */
4561 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4562 c1 = new_r_Const(irg, tv);
4563 DBG_OPT_CSTEVAL(n, c1);
4566 sl = get_Shl_left(left);
4567 blk = get_nodes_block(n);
4568 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4569 tv = tarval_shr(tv, tv1);
4571 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4576 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4578 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4581 c1 = get_Shr_right(left);
4583 ir_graph *irg = get_irn_irg(c1);
4584 ir_tarval *tv1 = get_Const_tarval(c1);
4585 ir_mode *mode = get_irn_mode(left);
4586 ir_tarval *minus1 = get_mode_all_one(mode);
4587 ir_tarval *amask = tarval_shl(minus1, tv1);
4588 ir_tarval *cmask = tarval_shr(minus1, tv1);
4591 if (tarval_and(tv, cmask) != tv) {
4592 /* condition not met */
4593 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4594 c1 = new_r_Const(irg, tv);
4595 DBG_OPT_CSTEVAL(n, c1);
4598 sl = get_Shr_left(left);
4599 blk = get_nodes_block(n);
4600 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4601 tv = tarval_shl(tv, tv1);
4603 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4608 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4610 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4613 c1 = get_Shrs_right(left);
4615 ir_graph *irg = get_irn_irg(c1);
4616 ir_tarval *tv1 = get_Const_tarval(c1);
4617 ir_mode *mode = get_irn_mode(left);
4618 ir_tarval *minus1 = get_mode_all_one(mode);
4619 ir_tarval *amask = tarval_shl(minus1, tv1);
4620 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4623 cond = tarval_sub(cond, tv1, NULL);
4624 cond = tarval_shrs(tv, cond);
4626 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4627 /* condition not met */
4628 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4629 c1 = new_r_Const(irg, tv);
4630 DBG_OPT_CSTEVAL(n, c1);
4633 sl = get_Shrs_left(left);
4634 blk = get_nodes_block(n);
4635 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4636 tv = tarval_shl(tv, tv1);
4638 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4646 if (changedc) { /* need a new Const */
4647 ir_graph *irg = get_irn_irg(n);
4648 right = new_r_Const(irg, tv);
4652 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4653 ir_node *op = get_Proj_pred(left);
4655 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4656 ir_node *c = get_binop_right(op);
4659 ir_tarval *tv = get_Const_tarval(c);
4661 if (tarval_is_single_bit(tv)) {
4662 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4663 ir_node *v = get_binop_left(op);
4664 ir_node *blk = get_nodes_block(op);
4665 ir_graph *irg = get_irn_irg(op);
4666 ir_mode *mode = get_irn_mode(v);
4668 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4669 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4671 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4678 dbg_info *dbgi = get_irn_dbg_info(n);
4679 ir_node *block = get_nodes_block(n);
4681 /* create a new compare */
4682 n = new_rd_Cmp(dbgi, block, left, right, relation);
4689 * Optimize CopyB(mem, x, x) into a Nop.
4691 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4693 ir_node *copyb = get_Proj_pred(proj);
4694 ir_node *a = get_CopyB_dst(copyb);
4695 ir_node *b = get_CopyB_src(copyb);
4698 switch (get_Proj_proj(proj)) {
4699 case pn_CopyB_X_regular:
4700 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4701 DBG_OPT_EXC_REM(proj);
4702 proj = new_r_Jmp(get_nodes_block(copyb));
4704 case pn_CopyB_X_except: {
4705 ir_graph *irg = get_irn_irg(proj);
4706 DBG_OPT_EXC_REM(proj);
4707 proj = new_r_Bad(irg, mode_X);
4718 * Does all optimizations on nodes that must be done on its Projs
4719 * because of creating new nodes.
4721 static ir_node *transform_node_Proj(ir_node *proj)
4723 ir_node *n = get_Proj_pred(proj);
4725 if (n->op->ops.transform_node_Proj)
4726 return n->op->ops.transform_node_Proj(proj);
4731 * Test whether a block is unreachable
4732 * Note: That this only returns true when
4733 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4734 * This is important, as you easily end up producing invalid constructs in the
4735 * unreachable code when optimizing away edges into the unreachable code.
4736 * So only set this flag when you iterate localopts to the fixpoint.
4737 * When you reach the fixpoint then all unreachable code is dead
4738 * (= can't be reached by firm edges) and you won't see the invalid constructs
4741 static bool is_block_unreachable(const ir_node *block)
4743 const ir_graph *irg = get_irn_irg(block);
4744 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4746 return get_Block_dom_depth(block) < 0;
4749 static ir_node *transform_node_Block(ir_node *block)
4751 ir_graph *irg = get_irn_irg(block);
4752 int arity = get_irn_arity(block);
4753 ir_node *bad = NULL;
4756 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4759 for (i = 0; i < arity; ++i) {
4760 ir_node *const pred = get_Block_cfgpred(block, i);
4761 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4764 bad = new_r_Bad(irg, mode_X);
4765 set_irn_n(block, i, bad);
4771 static ir_node *transform_node_Phi(ir_node *phi)
4773 int n = get_irn_arity(phi);
4774 ir_mode *mode = get_irn_mode(phi);
4775 ir_node *block = get_nodes_block(phi);
4776 ir_graph *irg = get_irn_irg(phi);
4777 ir_node *bad = NULL;
4780 /* Set phi-operands for bad-block inputs to bad */
4781 for (i = 0; i < n; ++i) {
4782 if (!is_Bad(get_Phi_pred(phi, i))) {
4783 ir_node *pred = get_Block_cfgpred(block, i);
4784 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4786 bad = new_r_Bad(irg, mode);
4787 set_irn_n(phi, i, bad);
4792 /* Move Pin nodes down through Phi nodes. */
4793 if (mode == mode_M) {
4794 n = get_irn_arity(phi);
4796 /* Beware of Phi0 */
4800 bool has_pin = false;
4802 NEW_ARR_A(ir_node *, in, n);
4804 for (i = 0; i < n; ++i) {
4805 ir_node *pred = get_irn_n(phi, i);
4808 in[i] = get_Pin_op(pred);
4810 } else if (is_Bad(pred)) {
4820 /* Move the Pin nodes "behind" the Phi. */
4821 new_phi = new_r_Phi(block, n, in, mode_M);
4822 return new_r_Pin(block, new_phi);
4825 /* Move Confirms down through Phi nodes. */
4826 else if (mode_is_reference(mode)) {
4827 n = get_irn_arity(phi);
4829 /* Beware of Phi0 */
4831 ir_node *pred = get_irn_n(phi, 0);
4832 ir_node *bound, *new_phi, **in;
4833 ir_relation relation;
4834 bool has_confirm = false;
4836 if (! is_Confirm(pred))
4839 bound = get_Confirm_bound(pred);
4840 relation = get_Confirm_relation(pred);
4842 NEW_ARR_A(ir_node *, in, n);
4843 in[0] = get_Confirm_value(pred);
4845 for (i = 1; i < n; ++i) {
4846 pred = get_irn_n(phi, i);
4848 if (is_Confirm(pred) &&
4849 get_Confirm_bound(pred) == bound &&
4850 get_Confirm_relation(pred) == relation) {
4851 in[i] = get_Confirm_value(pred);
4853 } else if (is_Bad(pred)) {
4863 /* move the Confirm nodes "behind" the Phi */
4864 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4865 return new_r_Confirm(block, new_phi, bound, relation);
4872 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4874 * Should be moved to reassociation?
4876 static ir_node *transform_node_shift(ir_node *n)
4878 ir_node *left, *right;
4880 ir_mode *count_mode;
4881 ir_tarval *tv1, *tv2, *res;
4882 ir_node *in[2], *irn, *block;
4886 left = get_binop_left(n);
4888 /* different operations */
4889 if (get_irn_op(left) != get_irn_op(n))
4892 right = get_binop_right(n);
4893 tv1 = value_of(right);
4894 if (tv1 == tarval_bad)
4897 tv2 = value_of(get_binop_right(left));
4898 if (tv2 == tarval_bad)
4901 count_mode = get_tarval_mode(tv1);
4902 if (get_tarval_mode(tv2) != count_mode) {
4903 /* TODO: search bigger mode or something and convert... */
4907 mode = get_irn_mode(n);
4908 modulo_shf = get_mode_modulo_shift(mode);
4910 if (modulo_shf > 0) {
4911 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4913 /* I'm not so sure what happens in one complement... */
4914 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4915 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4916 * above will be invalid) */
4917 assert(modulo_shf<=0 || is_po2(modulo_shf));
4919 tv1 = tarval_and(tv1, modulo_mask);
4920 tv2 = tarval_and(tv2, modulo_mask);
4922 res = tarval_add(tv1, tv2);
4923 irg = get_irn_irg(n);
4925 /* beware: a simple replacement works only, if res < modulo shift */
4927 int bits = get_mode_size_bits(mode);
4928 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4929 res = tarval_mod(res, modulo);
4931 long bits = get_mode_size_bits(mode);
4932 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4934 /* shifting too much */
4935 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4937 ir_node *block = get_nodes_block(n);
4938 dbg_info *dbgi = get_irn_dbg_info(n);
4939 ir_mode *smode = get_irn_mode(right);
4940 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4941 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4944 return new_r_Const(irg, get_mode_null(mode));
4948 /* ok, we can replace it */
4949 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4950 block = get_nodes_block(n);
4952 in[0] = get_binop_left(left);
4953 in[1] = new_r_Const(irg, res);
4955 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4957 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4964 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4966 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4967 * (also with x >>s c1 when c1>=c2)
4969 static ir_node *transform_node_shl_shr(ir_node *n)
4972 ir_node *right = get_binop_right(n);
4982 ir_tarval *tv_shift;
4985 ir_relation relation;
4988 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4990 if (!is_Const(right))
4993 left = get_binop_left(n);
4994 mode = get_irn_mode(n);
4995 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4996 ir_node *shr_right = get_binop_right(left);
4998 if (!is_Const(shr_right))
5001 x = get_binop_left(left);
5002 tv_shr = get_Const_tarval(shr_right);
5003 tv_shl = get_Const_tarval(right);
5005 if (is_Shrs(left)) {
5006 /* shrs variant only allowed if c1 >= c2 */
5007 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5010 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5013 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5015 tv_mask = tarval_shl(tv_mask, tv_shl);
5016 } else if (is_Shr(n) && is_Shl(left)) {
5017 ir_node *shl_right = get_Shl_right(left);
5019 if (!is_Const(shl_right))
5022 x = get_Shl_left(left);
5023 tv_shr = get_Const_tarval(right);
5024 tv_shl = get_Const_tarval(shl_right);
5026 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5027 tv_mask = tarval_shr(tv_mask, tv_shr);
5032 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5033 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5036 assert(tv_mask != tarval_bad);
5037 assert(get_tarval_mode(tv_mask) == mode);
5039 block = get_nodes_block(n);
5040 irg = get_irn_irg(block);
5041 dbgi = get_irn_dbg_info(n);
5043 relation = tarval_cmp(tv_shl, tv_shr);
5044 if (relation == ir_relation_less || relation == ir_relation_equal) {
5045 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5046 new_const = new_r_Const(irg, tv_shift);
5048 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5050 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5053 assert(relation == ir_relation_greater);
5054 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5055 new_const = new_r_Const(irg, tv_shift);
5056 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5059 new_const = new_r_Const(irg, tv_mask);
5060 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5065 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5067 ir_mode *mode = get_tarval_mode(tv);
5068 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5069 return tarval_mod(tv, modulo_tv);
5072 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5073 ir_node *left, ir_node *right, ir_mode *mode);
5076 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5077 * then we can use that to minimize the value of Add(x, const) or
5078 * Sub(Const, x). In particular this often avoids 1 instruction in some
5079 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5080 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5082 static ir_node *transform_node_shift_modulo(ir_node *n,
5083 new_shift_func new_shift)
5085 ir_mode *mode = get_irn_mode(n);
5086 int modulo = get_mode_modulo_shift(mode);
5087 ir_node *newop = NULL;
5088 ir_mode *mode_right;
5095 if (get_mode_arithmetic(mode) != irma_twos_complement)
5097 if (!is_po2(modulo))
5100 irg = get_irn_irg(n);
5101 block = get_nodes_block(n);
5102 right = get_binop_right(n);
5103 mode_right = get_irn_mode(right);
5104 if (is_Const(right)) {
5105 ir_tarval *tv = get_Const_tarval(right);
5106 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5111 newop = new_r_Const(irg, tv_mod);
5112 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5113 ir_node *add_right = get_binop_right(right);
5114 if (is_Const(add_right)) {
5115 ir_tarval *tv = get_Const_tarval(add_right);
5116 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5121 newconst = new_r_Const(irg, tv_mod);
5122 newop = new_r_Add(block, get_binop_left(right), newconst,
5125 } else if (is_Sub(right)) {
5126 ir_node *sub_left = get_Sub_left(right);
5127 if (is_Const(sub_left)) {
5128 ir_tarval *tv = get_Const_tarval(sub_left);
5129 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5134 newconst = new_r_Const(irg, tv_mod);
5135 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5142 if (newop != NULL) {
5143 dbg_info *dbgi = get_irn_dbg_info(n);
5144 ir_node *left = get_binop_left(n);
5145 return new_shift(dbgi, block, left, newop, mode);
5153 static ir_node *transform_node_Shr(ir_node *n)
5155 ir_node *c, *oldn = n;
5156 ir_node *left = get_Shr_left(n);
5157 ir_node *right = get_Shr_right(n);
5158 ir_mode *mode = get_irn_mode(n);
5160 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5161 n = transform_node_shift(n);
5164 n = transform_node_shift_modulo(n, new_rd_Shr);
5166 n = transform_node_shl_shr(n);
5168 n = transform_node_shift_bitop(n);
5176 static ir_node *transform_node_Shrs(ir_node *n)
5179 ir_node *a = get_Shrs_left(n);
5180 ir_node *b = get_Shrs_right(n);
5181 ir_mode *mode = get_irn_mode(n);
5185 if (is_oversize_shift(n)) {
5186 ir_node *block = get_nodes_block(n);
5187 dbg_info *dbgi = get_irn_dbg_info(n);
5188 ir_mode *cmode = get_irn_mode(b);
5189 long val = get_mode_size_bits(cmode)-1;
5190 ir_graph *irg = get_irn_irg(n);
5191 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5192 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5195 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5196 n = transform_node_shift(n);
5200 n = transform_node_shift_modulo(n, new_rd_Shrs);
5203 n = transform_node_shift_bitop(n);
5207 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5208 attr = vrp_get_info(a);
5210 unsigned bits = get_mode_size_bits(mode);
5211 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5212 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5213 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5214 dbg_info *dbgi = get_irn_dbg_info(n);
5215 ir_node *block = get_nodes_block(n);
5216 return new_rd_Shr(dbgi, block, a, b, mode);
5226 static ir_node *transform_node_Shl(ir_node *n)
5228 ir_node *c, *oldn = n;
5229 ir_node *a = get_Shl_left(n);
5230 ir_node *b = get_Shl_right(n);
5231 ir_mode *mode = get_irn_mode(n);
5233 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5234 n = transform_node_shift(n);
5237 n = transform_node_shift_modulo(n, new_rd_Shl);
5239 n = transform_node_shl_shr(n);
5241 n = transform_node_shift_bitop(n);
5249 static ir_node *transform_node_Rotl(ir_node *n)
5251 ir_node *c, *oldn = n;
5252 ir_node *a = get_Rotl_left(n);
5253 ir_node *b = get_Rotl_right(n);
5254 ir_mode *mode = get_irn_mode(n);
5256 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5257 n = transform_node_shift(n);
5260 n = transform_node_shift_bitop(n);
5266 * returns mode size for may_leave_out_middle_mode
5268 static unsigned get_significand_size(ir_mode *mode)
5270 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5271 switch (arithmetic) {
5273 case irma_x86_extended_float:
5274 return get_mode_mantissa_size(mode) + 1;
5275 case irma_twos_complement:
5276 return get_mode_size_bits(mode);
5278 panic("Conv node with irma_none mode?");
5280 panic("unexpected mode_arithmetic in get_significand_size");
5284 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5285 * as converting from @p m0 to @p m1 and then to @p m2.
5286 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5287 * gives the following truth table:
5288 * s -> b -> m : true
5289 * s -> m -> b : !signed(s) || signed(m)
5290 * m -> b -> s : true
5291 * m -> s -> b : false
5292 * b -> s -> m : false
5293 * b -> m -> s : true
5295 * s -> b -> b : true
5296 * s -> s -> b : false
5298 * additional float constraints:
5300 * F -> I -> I: signedness of Is must match
5301 * I -> F -> I: signedness of Is must match
5302 * I -> I -> F: signedness of Is must match
5306 * at least 1 float involved: signedness must match
5308 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5310 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5311 if (n_floats == 1) {
5312 /* because overflow gives strange results we don't touch this case */
5314 } else if (n_floats == 2 && !mode_is_float(m1)) {
5318 unsigned size0 = get_significand_size(m0);
5319 unsigned size1 = get_significand_size(m1);
5320 unsigned size2 = get_significand_size(m2);
5321 if (size1 < size2 && size0 >= size1)
5325 return !mode_is_signed(m0) || mode_is_signed(m1);
5331 static ir_node *transform_node_Conv(ir_node *n)
5333 ir_node *c, *oldn = n;
5334 ir_mode *mode = get_irn_mode(n);
5335 ir_node *a = get_Conv_op(n);
5338 ir_mode *a_mode = get_irn_mode(a);
5339 ir_node *b = get_Conv_op(a);
5340 ir_mode *b_mode = get_irn_mode(b);
5341 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5342 dbg_info *dbgi = get_irn_dbg_info(n);
5343 ir_node *block = get_nodes_block(n);
5344 return new_rd_Conv(dbgi, block, b, mode);
5348 if (mode != mode_b && is_const_Phi(a)) {
5349 /* Do NOT optimize mode_b Conv's, this leads to remaining
5350 * Phib nodes later, because the conv_b_lower operation
5351 * is instantly reverted, when it tries to insert a Convb.
5353 c = apply_conv_on_phi(a, mode);
5355 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5360 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5361 ir_graph *irg = get_irn_irg(n);
5362 return new_r_Unknown(irg, mode);
5365 if (mode_is_reference(mode) &&
5366 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5368 ir_node *l = get_Add_left(a);
5369 ir_node *r = get_Add_right(a);
5370 dbg_info *dbgi = get_irn_dbg_info(a);
5371 ir_node *block = get_nodes_block(n);
5373 ir_node *lop = get_Conv_op(l);
5374 if (get_irn_mode(lop) == mode) {
5375 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5376 n = new_rd_Add(dbgi, block, lop, r, mode);
5381 ir_node *rop = get_Conv_op(r);
5382 if (get_irn_mode(rop) == mode) {
5383 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5384 n = new_rd_Add(dbgi, block, l, rop, mode);
5394 * Remove dead blocks and nodes in dead blocks
5395 * in keep alive list. We do not generate a new End node.
5397 static ir_node *transform_node_End(ir_node *n)
5399 int i, j, n_keepalives = get_End_n_keepalives(n);
5402 NEW_ARR_A(ir_node *, in, n_keepalives);
5404 for (i = j = 0; i < n_keepalives; ++i) {
5405 ir_node *ka = get_End_keepalive(n, i);
5407 /* no need to keep Bad */
5410 /* do not keep unreachable code */
5411 block = is_Block(ka) ? ka : get_nodes_block(ka);
5412 if (is_block_unreachable(block))
5416 if (j != n_keepalives)
5417 set_End_keepalives(n, j, in);
5421 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5423 if (is_Minus(a) && get_Minus_op(a) == b)
5425 if (is_Minus(b) && get_Minus_op(b) == a)
5427 if (is_Sub(a) && is_Sub(b)) {
5428 ir_node *a_left = get_Sub_left(a);
5429 ir_node *a_right = get_Sub_right(a);
5430 ir_node *b_left = get_Sub_left(b);
5431 ir_node *b_right = get_Sub_right(b);
5433 if (a_left == b_right && a_right == b_left)
5440 static const ir_node *skip_upconv(const ir_node *node)
5442 while (is_Conv(node)) {
5443 ir_mode *mode = get_irn_mode(node);
5444 const ir_node *op = get_Conv_op(node);
5445 ir_mode *op_mode = get_irn_mode(op);
5446 if (!smaller_mode(op_mode, mode))
5453 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5454 const ir_node *mux_true)
5459 ir_relation relation;
5465 * Note further that these optimization work even for floating point
5466 * with NaN's because -NaN == NaN.
5467 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5470 mode = get_irn_mode(mux_true);
5471 if (mode_honor_signed_zeros(mode))
5474 /* must be <, <=, >=, > */
5475 relation = get_Cmp_relation(sel);
5476 if ((relation & ir_relation_less_greater) == 0)
5479 if (!ir_is_negated_value(mux_true, mux_false))
5482 mux_true = skip_upconv(mux_true);
5483 mux_false = skip_upconv(mux_false);
5485 /* must be x cmp 0 */
5486 cmp_right = get_Cmp_right(sel);
5487 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5490 cmp_left = get_Cmp_left(sel);
5491 if (cmp_left == mux_false) {
5492 if (relation & ir_relation_less) {
5495 assert(relation & ir_relation_greater);
5498 } else if (cmp_left == mux_true) {
5499 if (relation & ir_relation_less) {
5502 assert(relation & ir_relation_greater);
5510 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5513 ir_node *cmp_left = get_Cmp_left(sel);
5514 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5517 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5518 const ir_node *mux_true)
5520 /* this code should return true each time transform_node_Mux would
5521 * optimize the Mux completely away */
5523 ir_mode *mode = get_irn_mode(mux_false);
5524 if (get_mode_arithmetic(mode) == irma_twos_complement
5525 && ir_mux_is_abs(sel, mux_false, mux_true))
5528 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5529 const ir_node *cmp_r = get_Cmp_right(sel);
5530 const ir_node *cmp_l = get_Cmp_left(sel);
5531 const ir_node *f = mux_false;
5532 const ir_node *t = mux_true;
5534 if (is_Const(t) && is_Const_null(t)) {
5539 if (is_And(cmp_l) && f == cmp_r) {
5540 ir_node *and_r = get_And_right(cmp_l);
5543 if (and_r == t && is_single_bit(and_r))
5545 and_l = get_And_left(cmp_l);
5546 if (and_l == t && is_single_bit(and_l))
5555 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5557 static ir_node *transform_Mux_set(ir_node *n)
5559 ir_node *cond = get_Mux_sel(n);
5564 ir_relation relation;
5578 left = get_Cmp_left(cond);
5579 mode = get_irn_mode(left);
5580 if (!mode_is_int(mode) && !mode_is_reference(mode))
5582 dest_mode = get_irn_mode(n);
5583 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5585 right = get_Cmp_right(cond);
5586 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5587 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5588 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5589 && relation != ir_relation_greater))
5594 case ir_relation_less:
5595 /* a < b -> (a - b) >> 31 */
5599 case ir_relation_less_equal:
5600 /* a <= b -> ~(a - b) >> 31 */
5605 case ir_relation_greater:
5606 /* a > b -> (b - a) >> 31 */
5610 case ir_relation_greater_equal:
5611 /* a >= b -> ~(a - b) >> 31 */
5620 dbgi = get_irn_dbg_info(n);
5621 block = get_nodes_block(n);
5622 irg = get_irn_irg(block);
5623 bits = get_mode_size_bits(dest_mode);
5624 tv = new_tarval_from_long(bits-1, mode_Iu);
5625 shift_cnt = new_rd_Const(dbgi, irg, tv);
5627 if (mode != dest_mode) {
5628 a = new_rd_Conv(dbgi, block, a, dest_mode);
5629 b = new_rd_Conv(dbgi, block, b, dest_mode);
5632 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5634 res = new_rd_Not(dbgi, block, res, dest_mode);
5636 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5641 * Optimize a Mux into some simpler cases.
5643 static ir_node *transform_node_Mux(ir_node *n)
5646 ir_node *sel = get_Mux_sel(n);
5647 ir_mode *mode = get_irn_mode(n);
5648 ir_node *t = get_Mux_true(n);
5649 ir_node *f = get_Mux_false(n);
5650 ir_graph *irg = get_irn_irg(n);
5652 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5653 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5654 int abs = ir_mux_is_abs(sel, f, t);
5656 dbg_info *dbgi = get_irn_dbg_info(n);
5657 ir_node *block = get_nodes_block(n);
5658 ir_node *op = ir_get_abs_op(sel, f, t);
5659 int bits = get_mode_size_bits(mode);
5660 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5661 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5662 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5665 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5667 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5673 /* first normalization step: try to move a constant to the false side,
5674 * 0 preferred on false side too */
5675 if (is_Cmp(sel) && is_Const(t) &&
5676 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5677 dbg_info *seldbgi = get_irn_dbg_info(sel);
5678 ir_node *block = get_nodes_block(sel);
5679 ir_relation relation = get_Cmp_relation(sel);
5684 /* Mux(x, a, b) => Mux(not(x), b, a) */
5685 relation = get_negated_relation(relation);
5686 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5687 get_Cmp_right(sel), relation);
5688 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5691 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5692 n = transform_Mux_set(n);
5697 /* the following optimisations create new mode_b nodes, so only do them
5698 * before mode_b lowering */
5699 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5701 ir_node* block = get_nodes_block(n);
5703 ir_node* c1 = get_Mux_sel(t);
5704 ir_node* t1 = get_Mux_true(t);
5705 ir_node* f1 = get_Mux_false(t);
5707 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5708 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5709 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5710 return new_r_Mux(block, and_, f1, t1, mode);
5711 } else if (f == t1) {
5712 /* Mux(cond0, Mux(cond1, x, y), x) */
5713 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5714 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5715 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5716 return new_r_Mux(block, and_, t1, f1, mode);
5718 } else if (is_Mux(f)) {
5719 ir_node* block = get_nodes_block(n);
5721 ir_node* c1 = get_Mux_sel(f);
5722 ir_node* t1 = get_Mux_true(f);
5723 ir_node* f1 = get_Mux_false(f);
5725 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5726 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5727 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5728 return new_r_Mux(block, or_, f1, t1, mode);
5729 } else if (t == f1) {
5730 /* Mux(cond0, x, Mux(cond1, y, x)) */
5731 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5732 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5733 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5734 return new_r_Mux(block, or_, t1, f1, mode);
5738 /* note: after normalization, false can only happen on default */
5739 if (mode == mode_b) {
5740 dbg_info *dbg = get_irn_dbg_info(n);
5741 ir_node *block = get_nodes_block(n);
5744 ir_tarval *tv_t = get_Const_tarval(t);
5745 if (tv_t == tarval_b_true) {
5747 /* Muxb(sel, true, false) = sel */
5748 assert(get_Const_tarval(f) == tarval_b_false);
5749 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5752 /* Muxb(sel, true, x) = Or(sel, x) */
5753 n = new_rd_Or(dbg, block, sel, f, mode_b);
5754 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5758 } else if (is_Const(f)) {
5759 ir_tarval *tv_f = get_Const_tarval(f);
5760 if (tv_f == tarval_b_true) {
5761 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5762 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5763 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5764 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5767 /* Muxb(sel, x, false) = And(sel, x) */
5768 assert(tv_f == tarval_b_false);
5769 n = new_rd_And(dbg, block, sel, t, mode_b);
5770 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5777 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5778 ir_relation relation = get_Cmp_relation(sel);
5779 ir_node *cmp_r = get_Cmp_right(sel);
5780 ir_node *cmp_l = get_Cmp_left(sel);
5781 ir_node *block = get_nodes_block(n);
5783 if (is_And(cmp_l) && f == cmp_r) {
5784 ir_node *and_r = get_And_right(cmp_l);
5787 if (and_r == t && is_single_bit(and_r)) {
5788 if (relation == ir_relation_equal) {
5789 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5790 n = new_rd_Eor(get_irn_dbg_info(n),
5791 block, cmp_l, t, mode);
5792 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5794 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5796 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5800 and_l = get_And_left(cmp_l);
5801 if (and_l == t && is_single_bit(and_l)) {
5802 if (relation == ir_relation_equal) {
5803 /* ((1 << n) & a) == 0, (1 << n), 0) */
5804 n = new_rd_Eor(get_irn_dbg_info(n),
5805 block, cmp_l, t, mode);
5806 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5808 /* ((1 << n) & a) != 0, (1 << n), 0) */
5810 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5821 * optimize Sync nodes that have other syncs as input we simply add the inputs
5822 * of the other sync to our own inputs
5824 static ir_node *transform_node_Sync(ir_node *n)
5826 int arity = get_Sync_n_preds(n);
5829 for (i = 0; i < arity;) {
5830 ir_node *pred = get_Sync_pred(n, i);
5834 /* Remove Bad predecessors */
5841 /* Remove duplicate predecessors */
5842 for (j = 0; j < i; ++j) {
5843 if (get_Sync_pred(n, j) == pred) {
5852 if (!is_Sync(pred)) {
5860 pred_arity = get_Sync_n_preds(pred);
5861 for (j = 0; j < pred_arity; ++j) {
5862 ir_node *pred_pred = get_Sync_pred(pred, j);
5867 add_irn_n(n, pred_pred);
5871 if (get_Sync_pred(n, k) == pred_pred)
5878 ir_graph *irg = get_irn_irg(n);
5879 return new_r_Bad(irg, mode_M);
5882 return get_Sync_pred(n, 0);
5885 /* rehash the sync node */
5890 static ir_node *create_load_replacement_tuple(ir_node *n, ir_node *mem,
5893 ir_node *block = get_nodes_block(n);
5894 ir_graph *irg = get_irn_irg(n);
5895 ir_node *in[pn_Load_max+1];
5897 in[pn_Load_M] = mem;
5898 in[pn_Load_res] = res;
5899 if (ir_throws_exception(n)) {
5900 in[pn_Load_X_regular] = new_r_Jmp(block);
5901 in[pn_Load_X_except] = new_r_Bad(irg, mode_X);
5903 assert(pn_Load_max == 4);
5905 ir_node *tuple = new_r_Tuple(block, n_in, in);
5909 static ir_node *transform_node_Load(ir_node *n)
5911 /* don't touch volatile loads */
5912 if (get_Load_volatility(n) == volatility_is_volatile)
5915 ir_node *ptr = get_Load_ptr(n);
5916 const ir_node *confirm;
5917 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5918 set_irn_pinned(n, op_pin_state_floats);
5921 /* if our memory predecessor is a load from the same address, then reuse the
5922 * previous result */
5923 ir_node *mem = get_Load_mem(n);
5926 ir_node *mem_pred = get_Proj_pred(mem);
5927 if (is_Load(mem_pred)) {
5928 ir_node *pred_load = mem_pred;
5930 /* conservatively compare the 2 loads. TODO: This could be less strict
5931 * with fixup code in some situations (like smaller/bigger modes) */
5932 if (get_Load_ptr(pred_load) != ptr)
5934 if (get_Load_mode(pred_load) != get_Load_mode(n))
5936 /* all combinations of aligned/unaligned pred/n should be fine so we do
5937 * not compare the unaligned attribute */
5938 ir_mode *mode = get_Load_mode(n);
5939 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5940 return create_load_replacement_tuple(n, mem, res);
5941 } else if (is_Store(mem_pred)) {
5942 ir_node *pred_store = mem_pred;
5943 ir_node *value = get_Store_value(pred_store);
5945 if (get_Store_ptr(pred_store) != ptr)
5947 if (get_irn_mode(value) != get_Load_mode(n))
5949 /* all combinations of aligned/unaligned pred/n should be fine so we do
5950 * not compare the unaligned attribute */
5951 return create_load_replacement_tuple(n, mem, value);
5957 static ir_node *transform_node_Store(ir_node *n)
5959 /* don't touch volatile stores */
5960 if (get_Store_volatility(n) == volatility_is_volatile)
5963 ir_node *ptr = get_Store_ptr(n);
5964 const ir_node *confirm;
5965 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5966 set_irn_pinned(n, op_pin_state_floats);
5972 * optimize a trampoline Call into a direct Call
5974 static ir_node *transform_node_Call(ir_node *call)
5976 ir_node *callee = get_Call_ptr(call);
5977 ir_node *adr, *mem, *res, *bl, **in;
5978 ir_type *ctp, *mtp, *tp;
5982 size_t i, n_res, n_param;
5985 if (! is_Proj(callee))
5987 callee = get_Proj_pred(callee);
5988 if (! is_Builtin(callee))
5990 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5993 mem = get_Call_mem(call);
5995 if (skip_Proj(mem) == callee) {
5996 /* memory is routed to the trampoline, skip */
5997 mem = get_Builtin_mem(callee);
6000 /* build a new call type */
6001 mtp = get_Call_type(call);
6002 tdb = get_type_dbg_info(mtp);
6004 n_res = get_method_n_ress(mtp);
6005 n_param = get_method_n_params(mtp);
6006 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6008 for (i = 0; i < n_res; ++i)
6009 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6011 NEW_ARR_A(ir_node *, in, n_param + 1);
6013 /* FIXME: we don't need a new pointer type in every step */
6014 irg = get_irn_irg(call);
6015 tp = get_irg_frame_type(irg);
6016 tp = new_type_pointer(tp);
6017 set_method_param_type(ctp, 0, tp);
6019 in[0] = get_Builtin_param(callee, 2);
6020 for (i = 0; i < n_param; ++i) {
6021 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6022 in[i + 1] = get_Call_param(call, i);
6024 var = get_method_variadicity(mtp);
6025 set_method_variadicity(ctp, var);
6026 /* When we resolve a trampoline, the function must be called by a this-call */
6027 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6028 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6030 adr = get_Builtin_param(callee, 1);
6032 db = get_irn_dbg_info(call);
6033 bl = get_nodes_block(call);
6035 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6036 if (get_irn_pinned(call) == op_pin_state_floats)
6037 set_irn_pinned(res, op_pin_state_floats);
6042 * Tries several [inplace] [optimizing] transformations and returns an
6043 * equivalent node. The difference to equivalent_node() is that these
6044 * transformations _do_ generate new nodes, and thus the old node must
6045 * not be freed even if the equivalent node isn't the old one.
6047 static ir_node *transform_node(ir_node *n)
6053 iro = get_irn_opcode_(n);
6054 /* constant expression evaluation / constant folding */
6055 if (get_opt_constant_folding()) {
6056 /* neither constants nor Tuple values can be evaluated */
6057 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6058 /* try to evaluate */
6059 ir_tarval *tv = computed_value(n);
6060 if (tv != tarval_bad) {
6061 /* evaluation was successful -- replace the node. */
6062 ir_graph *irg = get_irn_irg(n);
6064 n = new_r_Const(irg, tv);
6066 DBG_OPT_CSTEVAL(old_n, n);
6072 /* remove unnecessary nodes */
6073 if (get_opt_constant_folding() ||
6074 (iro == iro_Phi) || /* always optimize these nodes. */
6075 (iro == iro_Id) || /* ... */
6076 (iro == iro_Proj) || /* ... */
6077 (iro == iro_Block)) { /* Flags tested local. */
6078 n = equivalent_node(n);
6083 /* Some more constant expression evaluation. */
6084 if (get_opt_algebraic_simplification() ||
6085 (iro == iro_Cond) ||
6086 (iro == iro_Proj)) { /* Flags tested local. */
6087 if (n->op->ops.transform_node != NULL) {
6088 n = n->op->ops.transform_node(n);
6098 static void register_computed_value_func(ir_op *op, computed_value_func func)
6100 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6101 op->ops.computed_value = func;
6104 static void register_computed_value_func_proj(ir_op *op,
6105 computed_value_func func)
6107 assert(op->ops.computed_value_Proj == NULL
6108 || op->ops.computed_value_Proj == func);
6109 op->ops.computed_value_Proj = func;
6112 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6114 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6115 op->ops.equivalent_node = func;
6118 static void register_equivalent_node_func_proj(ir_op *op,
6119 equivalent_node_func func)
6121 assert(op->ops.equivalent_node_Proj == NULL
6122 || op->ops.equivalent_node_Proj == func);
6123 op->ops.equivalent_node_Proj = func;
6126 static void register_transform_node_func(ir_op *op, transform_node_func func)
6128 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6129 op->ops.transform_node = func;
6132 static void register_transform_node_func_proj(ir_op *op,
6133 transform_node_func func)
6135 assert(op->ops.transform_node_Proj == NULL
6136 || op->ops.transform_node_Proj == func);
6137 op->ops.transform_node_Proj = func;
6140 void ir_register_opt_node_ops(void)
6142 register_computed_value_func(op_Add, computed_value_Add);
6143 register_computed_value_func(op_And, computed_value_And);
6144 register_computed_value_func(op_Cmp, computed_value_Cmp);
6145 register_computed_value_func(op_Confirm, computed_value_Confirm);
6146 register_computed_value_func(op_Const, computed_value_Const);
6147 register_computed_value_func(op_Conv, computed_value_Conv);
6148 register_computed_value_func(op_Eor, computed_value_Eor);
6149 register_computed_value_func(op_Minus, computed_value_Minus);
6150 register_computed_value_func(op_Mul, computed_value_Mul);
6151 register_computed_value_func(op_Mux, computed_value_Mux);
6152 register_computed_value_func(op_Not, computed_value_Not);
6153 register_computed_value_func(op_Or, computed_value_Or);
6154 register_computed_value_func(op_Proj, computed_value_Proj);
6155 register_computed_value_func(op_Rotl, computed_value_Rotl);
6156 register_computed_value_func(op_Shl, computed_value_Shl);
6157 register_computed_value_func(op_Shr, computed_value_Shr);
6158 register_computed_value_func(op_Shrs, computed_value_Shrs);
6159 register_computed_value_func(op_Sub, computed_value_Sub);
6160 register_computed_value_func(op_SymConst, computed_value_SymConst);
6161 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6162 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6164 register_equivalent_node_func(op_Add, equivalent_node_Add);
6165 register_equivalent_node_func(op_And, equivalent_node_And);
6166 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6167 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6168 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6169 register_equivalent_node_func(op_Id, equivalent_node_Id);
6170 register_equivalent_node_func(op_Minus, equivalent_node_involution);
6171 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6172 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6173 register_equivalent_node_func(op_Not, equivalent_node_involution);
6174 register_equivalent_node_func(op_Or, equivalent_node_Or);
6175 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6176 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6177 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6178 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6179 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6180 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6181 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6182 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6183 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6184 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6186 register_transform_node_func(op_Add, transform_node_Add);
6187 register_transform_node_func(op_And, transform_node_And);
6188 register_transform_node_func(op_Block, transform_node_Block);
6189 register_transform_node_func(op_Call, transform_node_Call);
6190 register_transform_node_func(op_Cmp, transform_node_Cmp);
6191 register_transform_node_func(op_Cond, transform_node_Cond);
6192 register_transform_node_func(op_Conv, transform_node_Conv);
6193 register_transform_node_func(op_Div, transform_node_Div);
6194 register_transform_node_func(op_End, transform_node_End);
6195 register_transform_node_func(op_Eor, transform_node_Eor);
6196 register_transform_node_func(op_Load, transform_node_Load);
6197 register_transform_node_func(op_Minus, transform_node_Minus);
6198 register_transform_node_func(op_Mod, transform_node_Mod);
6199 register_transform_node_func(op_Mul, transform_node_Mul);
6200 register_transform_node_func(op_Mux, transform_node_Mux);
6201 register_transform_node_func(op_Not, transform_node_Not);
6202 register_transform_node_func(op_Or, transform_node_Or);
6203 register_transform_node_func(op_Phi, transform_node_Phi);
6204 register_transform_node_func(op_Proj, transform_node_Proj);
6205 register_transform_node_func(op_Rotl, transform_node_Rotl);
6206 register_transform_node_func(op_Shl, transform_node_Shl);
6207 register_transform_node_func(op_Shrs, transform_node_Shrs);
6208 register_transform_node_func(op_Shr, transform_node_Shr);
6209 register_transform_node_func(op_Store, transform_node_Store);
6210 register_transform_node_func(op_Sub, transform_node_Sub);
6211 register_transform_node_func(op_Switch, transform_node_Switch);
6212 register_transform_node_func(op_Sync, transform_node_Sync);
6213 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6214 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6215 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6216 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6217 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6220 /* **************** Common Subexpression Elimination **************** */
6222 /** The size of the hash table used, should estimate the number of nodes
6224 #define N_IR_NODES 512
6226 int identities_cmp(const void *elt, const void *key)
6228 ir_node *a = (ir_node *)elt;
6229 ir_node *b = (ir_node *)key;
6232 if (a == b) return 0;
6234 if ((get_irn_op(a) != get_irn_op(b)) ||
6235 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6237 /* compare if a's in and b's in are of equal length */
6238 irn_arity_a = get_irn_arity(a);
6239 if (irn_arity_a != get_irn_arity(b))
6242 /* blocks are never the same */
6246 if (get_irn_pinned(a) == op_pin_state_pinned) {
6247 /* for pinned nodes, the block inputs must be equal */
6248 if (get_nodes_block(a) != get_nodes_block(b))
6251 ir_node *block_a = get_nodes_block(a);
6252 ir_node *block_b = get_nodes_block(b);
6253 if (! get_opt_global_cse()) {
6254 /* for block-local CSE both nodes must be in the same Block */
6255 if (block_a != block_b)
6258 /* The optimistic approach would be to do nothing here.
6259 * However doing GCSE optimistically produces a lot of partially dead code which appears
6260 * to be worse in practice than the missed opportunities.
6261 * So we use a very conservative variant here and only CSE if 1 value dominates the
6263 if (!block_dominates(block_a, block_b)
6264 && !block_dominates(block_b, block_a))
6266 /* respect the workaround rule: do not move nodes which are only
6267 * held by keepalive edges */
6268 if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6273 /* compare a->in[0..ins] with b->in[0..ins] */
6274 for (i = 0; i < irn_arity_a; ++i) {
6275 ir_node *pred_a = get_irn_n(a, i);
6276 ir_node *pred_b = get_irn_n(b, i);
6277 if (pred_a != pred_b) {
6278 /* if both predecessors are CSE neutral they might be different */
6279 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6285 * here, we already now that the nodes are identical except their
6288 if (a->op->ops.node_cmp_attr)
6289 return a->op->ops.node_cmp_attr(a, b);
6294 unsigned ir_node_hash(const ir_node *node)
6296 return node->op->ops.hash(node);
6299 void new_identities(ir_graph *irg)
6301 if (irg->value_table != NULL)
6302 del_pset(irg->value_table);
6303 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6306 void del_identities(ir_graph *irg)
6308 if (irg->value_table != NULL)
6309 del_pset(irg->value_table);
6312 static int cmp_node_nr(const void *a, const void *b)
6314 ir_node **p1 = (ir_node**)a;
6315 ir_node **p2 = (ir_node**)b;
6316 long n1 = get_irn_node_nr(*p1);
6317 long n2 = get_irn_node_nr(*p2);
6318 return (n1>n2) - (n1<n2);
6321 void ir_normalize_node(ir_node *n)
6323 if (is_op_commutative(get_irn_op(n))) {
6324 ir_node *l = get_binop_left(n);
6325 ir_node *r = get_binop_right(n);
6327 /* For commutative operators perform a OP b == b OP a but keep
6328 * constants on the RIGHT side. This helps greatly in some
6329 * optimizations. Moreover we use the idx number to make the form
6331 if (!operands_are_normalized(l, r)) {
6332 set_binop_left(n, r);
6333 set_binop_right(n, l);
6336 } else if (is_Sync(n)) {
6337 /* we assume that most of the time the inputs of a Sync node are already
6338 * sorted, so check this first as a shortcut */
6339 bool ins_sorted = true;
6340 int arity = get_irn_arity(n);
6341 const ir_node *last = get_irn_n(n, 0);
6343 for (i = 1; i < arity; ++i) {
6344 const ir_node *node = get_irn_n(n, i);
6345 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6353 ir_node **ins = get_irn_in(n)+1;
6354 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6355 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6356 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6357 set_irn_in(n, arity, new_ins);
6363 ir_node *identify_remember(ir_node *n)
6365 ir_graph *irg = get_irn_irg(n);
6366 pset *value_table = irg->value_table;
6369 if (value_table == NULL)
6372 ir_normalize_node(n);
6373 /* lookup or insert in hash table with given hash key. */
6374 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6377 /* n is reachable again */
6378 edges_node_revival(nn);
6385 * During construction we set the op_pin_state_pinned flag in the graph right
6386 * when the optimization is performed. The flag turning on procedure global
6387 * cse could be changed between two allocations. This way we are safe.
6389 * @param n The node to lookup
6391 static inline ir_node *identify_cons(ir_node *n)
6395 n = identify_remember(n);
6396 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6397 ir_graph *irg = get_irn_irg(n);
6398 set_irg_pinned(irg, op_pin_state_floats);
6403 void add_identities(ir_node *node)
6410 identify_remember(node);
6413 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6415 ir_graph *rem = current_ir_graph;
6417 current_ir_graph = irg;
6418 foreach_pset(irg->value_table, ir_node, node) {
6421 current_ir_graph = rem;
6424 ir_node *optimize_node(ir_node *n)
6427 ir_graph *irg = get_irn_irg(n);
6428 unsigned iro = get_irn_opcode(n);
6431 /* Always optimize Phi nodes: part of the construction. */
6432 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6434 /* constant expression evaluation / constant folding */
6435 if (get_opt_constant_folding()) {
6436 /* neither constants nor Tuple values can be evaluated */
6437 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6438 /* try to evaluate */
6439 tv = computed_value(n);
6440 if (tv != tarval_bad) {
6445 * we MUST copy the node here temporarily, because it's still
6446 * needed for DBG_OPT_CSTEVAL
6448 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6449 oldn = (ir_node*)alloca(node_size);
6451 memcpy(oldn, n, node_size);
6452 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6454 /* ARG, copy the in array, we need it for statistics */
6455 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6457 /* note the inplace edges module */
6458 edges_node_deleted(n);
6460 /* evaluation was successful -- replace the node. */
6461 irg_kill_node(irg, n);
6462 nw = new_r_Const(irg, tv);
6464 DBG_OPT_CSTEVAL(oldn, nw);
6470 /* remove unnecessary nodes */
6471 if (get_opt_algebraic_simplification() ||
6472 (iro == iro_Phi) || /* always optimize these nodes. */
6474 (iro == iro_Proj) ||
6475 (iro == iro_Block) ) /* Flags tested local. */
6476 n = equivalent_node(n);
6478 /* Common Subexpression Elimination.
6480 * Checks whether n is already available.
6481 * The block input is used to distinguish different subexpressions. Right
6482 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6483 * subexpressions within a block.
6486 n = identify_cons(n);
6489 edges_node_deleted(oldn);
6491 /* We found an existing, better node, so we can deallocate the old node. */
6492 irg_kill_node(irg, oldn);
6496 /* Some more constant expression evaluation that does not allow to
6498 iro = get_irn_opcode(n);
6499 if (get_opt_algebraic_simplification() ||
6500 (iro == iro_Cond) ||
6501 (iro == iro_Proj)) { /* Flags tested local. */
6502 n = transform_node(n);
6505 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6506 if (get_opt_cse()) {
6508 n = identify_remember(o);
6516 ir_node *optimize_in_place_2(ir_node *n)
6518 if (!get_opt_optimize() && !is_Phi(n)) return n;
6523 /** common subexpression elimination **/
6524 /* Checks whether n is already available. */
6525 /* The block input is used to distinguish different subexpressions.
6526 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6527 * only finds common subexpressions within a block. */
6528 if (get_opt_cse()) {
6530 n = identify_remember(n);
6533 /* we have another existing node now, we do not optimize it here */
6538 n = transform_node(n);
6540 /* Now we can verify the node, as it has no dead inputs any more. */
6543 /* Now we have a legal, useful node. Enter it in hash table for cse.
6545 * Note: This is only necessary because some of the optimisations
6546 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6547 * bad practice and should be fixed sometime.
6549 if (get_opt_cse()) {
6551 n = identify_remember(o);
6559 ir_node *optimize_in_place(ir_node *n)
6561 ir_graph *irg = get_irn_irg(n);
6563 if (get_opt_global_cse())
6564 set_irg_pinned(irg, op_pin_state_floats);
6566 /* FIXME: Maybe we could also test whether optimizing the node can
6567 change the control graph. */
6568 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6569 return optimize_in_place_2(n);