2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief iropt --- optimizations intertwined with IR construction.
9 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
17 #include "irgraph_t.h"
18 #include "iredges_t.h"
24 #include "iroptimize.h"
26 #include "dbginfo_t.h"
27 #include "iropt_dbg.h"
36 #include "firm_types.h"
37 #include "bitfiddle.h"
40 #include "firmstat_t.h"
44 static bool is_Or_Eor_Add(const ir_node *node)
46 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
47 ir_node *left = get_binop_left(node);
48 ir_node *right = get_binop_right(node);
49 vrp_attr *vrp_left = vrp_get_info(left);
50 vrp_attr *vrp_right = vrp_get_info(right);
51 if (vrp_left != NULL && vrp_right != NULL) {
53 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
54 return tarval_is_null(vrp_val);
61 * Returns the tarval of a Const node or tarval_bad for all other nodes.
63 static ir_tarval *default_value_of(const ir_node *n)
66 return get_Const_tarval(n); /* might return tarval_bad */
71 value_of_func value_of_ptr = default_value_of;
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static ir_tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static ir_tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
121 * Return the value of an Add.
123 static ir_tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 ir_tarval *ta = value_of(a);
129 ir_tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 if ((is_Not(a) && get_Not_op(a) == b)
136 || (is_Not(b) && get_Not_op(b) == a)) {
137 return get_mode_all_one(get_irn_mode(n));
144 * Return the value of a Sub.
145 * Special case: a - a
147 static ir_tarval *computed_value_Sub(const ir_node *n)
149 ir_mode *mode = get_irn_mode(n);
150 ir_node *a = get_Sub_left(n);
151 ir_node *b = get_Sub_right(n);
156 if (! mode_is_float(mode)) {
159 return get_mode_null(mode);
165 if ((ta != tarval_bad) && (tb != tarval_bad))
166 return tarval_sub(ta, tb, mode);
172 * Return the value of an unary Minus.
174 static ir_tarval *computed_value_Minus(const ir_node *n)
176 ir_node *a = get_Minus_op(n);
177 ir_tarval *ta = value_of(a);
179 if (ta != tarval_bad)
180 return tarval_neg(ta);
186 * Return the value of a Mul.
188 static ir_tarval *computed_value_Mul(const ir_node *n)
190 ir_node *a = get_Mul_left(n);
191 ir_node *b = get_Mul_right(n);
192 ir_tarval *ta = value_of(a);
193 ir_tarval *tb = value_of(b);
196 mode = get_irn_mode(n);
197 if (mode != get_irn_mode(a)) {
198 /* n * n = 2n bit multiplication */
199 ta = tarval_convert_to(ta, mode);
200 tb = tarval_convert_to(tb, mode);
203 if (ta != tarval_bad && tb != tarval_bad) {
204 return tarval_mul(ta, tb);
206 /* a * 0 != 0 if a == NaN or a == Inf */
207 if (!mode_is_float(mode)) {
208 /* a*0 = 0 or 0*b = 0 */
209 if (ta == get_mode_null(mode))
211 if (tb == get_mode_null(mode))
219 * Return the value of an And.
220 * Special case: a & 0, 0 & b
222 static ir_tarval *computed_value_And(const ir_node *n)
224 ir_node *a = get_And_left(n);
225 ir_node *b = get_And_right(n);
226 ir_tarval *ta = value_of(a);
227 ir_tarval *tb = value_of(b);
229 if ((ta != tarval_bad) && (tb != tarval_bad)) {
230 return tarval_and (ta, tb);
233 if (tarval_is_null(ta)) return ta;
234 if (tarval_is_null(tb)) return tb;
237 if ((is_Not(a) && get_Not_op(a) == b)
238 || (is_Not(b) && get_Not_op(b) == a)) {
239 return get_mode_null(get_irn_mode(n));
246 * Return the value of an Or.
247 * Special case: a | 1...1, 1...1 | b
249 static ir_tarval *computed_value_Or(const ir_node *n)
251 ir_node *a = get_Or_left(n);
252 ir_node *b = get_Or_right(n);
253 ir_tarval *ta = value_of(a);
254 ir_tarval *tb = value_of(b);
256 if ((ta != tarval_bad) && (tb != tarval_bad)) {
257 return tarval_or (ta, tb);
260 if (tarval_is_all_one(ta)) return ta;
261 if (tarval_is_all_one(tb)) return tb;
264 if ((is_Not(a) && get_Not_op(a) == b)
265 || (is_Not(b) && get_Not_op(b) == a)) {
266 return get_mode_all_one(get_irn_mode(n));
272 * Return the value of an Eor.
274 static ir_tarval *computed_value_Eor(const ir_node *n)
276 ir_node *a = get_Eor_left(n);
277 ir_node *b = get_Eor_right(n);
282 return get_mode_null(get_irn_mode(n));
284 if ((is_Not(a) && get_Not_op(a) == b)
285 || (is_Not(b) && get_Not_op(b) == a)) {
286 return get_mode_all_one(get_irn_mode(n));
292 if ((ta != tarval_bad) && (tb != tarval_bad)) {
293 return tarval_eor(ta, tb);
299 * Return the value of a Not.
301 static ir_tarval *computed_value_Not(const ir_node *n)
303 ir_node *a = get_Not_op(n);
304 ir_tarval *ta = value_of(a);
306 if (ta != tarval_bad)
307 return tarval_not(ta);
313 * Tests whether a shift shifts more bits than available in the mode
315 static bool is_oversize_shift(const ir_node *n)
317 ir_node *count = get_binop_right(n);
318 ir_mode *mode = get_irn_mode(n);
319 ir_tarval *tv = value_of(count);
322 if (tv == tarval_bad)
324 if (!tarval_is_long(tv))
326 shiftval = get_tarval_long(tv);
327 modulo_shift = get_mode_modulo_shift(mode);
328 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
331 return shiftval >= (long)get_mode_size_bits(mode);
335 * Return the value of a Shl.
337 static ir_tarval *computed_value_Shl(const ir_node *n)
339 ir_node *a = get_Shl_left(n);
340 ir_node *b = get_Shl_right(n);
342 ir_tarval *ta = value_of(a);
343 ir_tarval *tb = value_of(b);
345 if ((ta != tarval_bad) && (tb != tarval_bad)) {
346 return tarval_shl(ta, tb);
349 if (is_oversize_shift(n))
350 return get_mode_null(get_irn_mode(n));
356 * Return the value of a Shr.
358 static ir_tarval *computed_value_Shr(const ir_node *n)
360 ir_node *a = get_Shr_left(n);
361 ir_node *b = get_Shr_right(n);
363 ir_tarval *ta = value_of(a);
364 ir_tarval *tb = value_of(b);
366 if ((ta != tarval_bad) && (tb != tarval_bad)) {
367 return tarval_shr(ta, tb);
369 if (is_oversize_shift(n))
370 return get_mode_null(get_irn_mode(n));
376 * Return the value of a Shrs.
378 static ir_tarval *computed_value_Shrs(const ir_node *n)
380 ir_node *a = get_Shrs_left(n);
381 ir_node *b = get_Shrs_right(n);
383 ir_tarval *ta = value_of(a);
384 ir_tarval *tb = value_of(b);
386 if ((ta != tarval_bad) && (tb != tarval_bad)) {
387 return tarval_shrs(ta, tb);
393 * Return the value of a Rotl.
395 static ir_tarval *computed_value_Rotl(const ir_node *n)
397 ir_node *a = get_Rotl_left(n);
398 ir_node *b = get_Rotl_right(n);
400 ir_tarval *ta = value_of(a);
401 ir_tarval *tb = value_of(b);
403 if ((ta != tarval_bad) && (tb != tarval_bad)) {
404 return tarval_rotl(ta, tb);
409 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
411 ir_mode *mode = get_irn_mode(node);
412 if (get_mode_arithmetic(mode) != irma_twos_complement
413 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
417 ir_node *count = get_Shl_right(node);
418 if (is_Const(count)) {
419 ir_tarval *tv = get_Const_tarval(count);
420 if (tarval_is_long(tv)) {
421 long shiftval = get_tarval_long(tv);
422 long destbits = get_mode_size_bits(dest_mode);
423 if (shiftval >= destbits
424 && shiftval < (long)get_mode_modulo_shift(mode))
430 ir_node *right = get_And_right(node);
431 if (is_Const(right)) {
432 ir_tarval *tv = get_Const_tarval(right);
433 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
434 return tarval_is_null(conved);
441 * Return the value of a Conv.
443 static ir_tarval *computed_value_Conv(const ir_node *n)
445 ir_node *a = get_Conv_op(n);
446 ir_tarval *ta = value_of(a);
447 ir_mode *mode = get_irn_mode(n);
449 if (ta != tarval_bad)
450 return tarval_convert_to(ta, get_irn_mode(n));
452 if (ir_zero_when_converted(a, mode))
453 return get_mode_null(mode);
459 * Calculate the value of a Mux: can be evaluated, if the
460 * sel and the right input are known.
462 static ir_tarval *computed_value_Mux(const ir_node *n)
464 ir_node *sel = get_Mux_sel(n);
465 ir_tarval *ts = value_of(sel);
467 if (ts == get_tarval_b_true()) {
468 ir_node *v = get_Mux_true(n);
471 else if (ts == get_tarval_b_false()) {
472 ir_node *v = get_Mux_false(n);
479 * Calculate the value of a Confirm: can be evaluated,
480 * if it has the form Confirm(x, '=', Const).
482 static ir_tarval *computed_value_Confirm(const ir_node *n)
484 if (get_Confirm_relation(n) == ir_relation_equal) {
485 ir_tarval *tv = value_of(get_Confirm_bound(n));
486 if (tv != tarval_bad)
489 return value_of(get_Confirm_value(n));
493 * gives a (conservative) estimation of possible relation when comparing
496 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
497 const ir_node *right)
499 ir_relation possible = ir_relation_true;
500 ir_tarval *tv_l = value_of(left);
501 ir_tarval *tv_r = value_of(right);
502 ir_mode *mode = get_irn_mode(left);
503 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
504 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
506 /* both values known - evaluate them */
507 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
508 possible = tarval_cmp(tv_l, tv_r);
509 /* we can return now, won't get any better */
512 /* a == a is never less or greater (but might be equal or unordered) */
514 possible &= ~ir_relation_less_greater;
515 /* unordered results only happen for float compares */
516 if (!mode_is_float(mode))
517 possible &= ~ir_relation_unordered;
518 /* values can never be less than the least representable number or
519 * greater than the greatest representable number */
521 possible &= ~ir_relation_greater;
523 possible &= ~ir_relation_less;
525 possible &= ~ir_relation_greater;
527 possible &= ~ir_relation_less;
528 /* maybe vrp can tell us more */
529 possible &= vrp_cmp(left, right);
530 /* Alloc nodes never return null (but throw an exception) */
531 if (is_Alloc(left) && tarval_is_null(tv_r))
532 possible &= ~ir_relation_equal;
533 /* stuff known through confirm nodes */
534 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
535 possible &= get_Confirm_relation(left);
537 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
538 ir_relation relation = get_Confirm_relation(right);
539 relation = get_inversed_relation(relation);
540 possible &= relation;
546 static ir_tarval *compute_cmp(const ir_node *cmp)
548 ir_node *left = get_Cmp_left(cmp);
549 ir_node *right = get_Cmp_right(cmp);
550 ir_relation possible = ir_get_possible_cmp_relations(left, right);
551 ir_relation relation = get_Cmp_relation(cmp);
553 /* if none of the requested relations is possible, return false */
554 if ((possible & relation) == ir_relation_false)
555 return tarval_b_false;
556 /* if possible relations are a subset of the requested ones return true */
557 if ((possible & ~relation) == ir_relation_false)
558 return tarval_b_true;
560 return computed_value_Cmp_Confirm(cmp, left, right, relation);
564 * some people want to call compute_cmp directly, in this case we have to
565 * test the constant folding flag again
567 static ir_tarval *compute_cmp_ext(const ir_node *cmp)
569 if (!get_opt_constant_folding())
571 return compute_cmp(cmp);
575 * Return the value of a Cmp.
577 * The basic idea here is to determine which relations are possible and which
578 * one are definitely impossible.
580 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
582 /* we can't construct Constb after lowering mode_b nodes */
583 if (irg_is_constrained(get_irn_irg(cmp), IR_GRAPH_CONSTRAINT_MODEB_LOWERED))
586 return compute_cmp(cmp);
590 * Calculate the value of an integer Div.
591 * Special case: 0 / b
593 static ir_tarval *do_computed_value_Div(const ir_node *div)
595 const ir_node *a = get_Div_left(div);
596 const ir_node *b = get_Div_right(div);
597 const ir_mode *mode = get_Div_resmode(div);
598 ir_tarval *ta = value_of(a);
600 const ir_node *dummy;
602 /* cannot optimize 0 / b = 0 because of NaN */
603 if (!mode_is_float(mode)) {
604 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
605 return ta; /* 0 / b == 0 if b != 0 */
608 if (ta != tarval_bad && tb != tarval_bad)
609 return tarval_div(ta, tb);
614 * Calculate the value of an integer Mod of two nodes.
615 * Special case: a % 1
617 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
619 ir_tarval *ta = value_of(a);
620 ir_tarval *tb = value_of(b);
622 /* Compute a % 1 or c1 % c2 */
623 if (tarval_is_one(tb))
624 return get_mode_null(get_irn_mode(a));
625 if (ta != tarval_bad && tb != tarval_bad)
626 return tarval_mod(ta, tb);
631 * Return the value of a Proj(Div).
633 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
635 long proj_nr = get_Proj_proj(n);
636 if (proj_nr != pn_Div_res)
639 return do_computed_value_Div(get_Proj_pred(n));
643 * Return the value of a Proj(Mod).
645 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
647 long proj_nr = get_Proj_proj(n);
649 if (proj_nr == pn_Mod_res) {
650 const ir_node *mod = get_Proj_pred(n);
651 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
657 * Return the value of a Proj.
659 static ir_tarval *computed_value_Proj(const ir_node *proj)
661 ir_node *n = get_Proj_pred(proj);
663 if (n->op->ops.computed_value_Proj != NULL)
664 return n->op->ops.computed_value_Proj(proj);
669 * If the parameter n can be computed, return its value, else tarval_bad.
670 * Performs constant folding.
672 * @param n The node this should be evaluated
674 ir_tarval *computed_value(const ir_node *n)
676 vrp_attr *vrp = vrp_get_info(n);
677 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
678 return vrp->bits_set;
680 if (n->op->ops.computed_value)
681 return n->op->ops.computed_value(n);
686 * Optimize operations that are commutative and have neutral 0,
687 * so a op 0 = 0 op a = a.
689 static ir_node *equivalent_node_neutral_zero(ir_node *n)
693 ir_node *a = get_binop_left(n);
694 ir_node *b = get_binop_right(n);
699 /* After running compute_node there is only one constant predecessor.
700 Find this predecessors value and remember the other node: */
701 if ((tv = value_of(a)) != tarval_bad) {
703 } else if ((tv = value_of(b)) != tarval_bad) {
708 /* If this predecessors constant value is zero, the operation is
709 * unnecessary. Remove it.
711 * Beware: If n is a Add, the mode of on and n might be different
712 * which happens in this rare construction: NULL + 3.
713 * Then, a Conv would be needed which we cannot include here.
715 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
718 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
725 * Eor is commutative and has neutral 0.
727 static ir_node *equivalent_node_Eor(ir_node *n)
733 n = equivalent_node_neutral_zero(n);
734 if (n != oldn) return n;
737 b = get_Eor_right(n);
739 if (is_Eor(a) || is_Or_Eor_Add(a)) {
740 ir_node *aa = get_binop_left(a);
741 ir_node *ab = get_binop_right(a);
744 /* (a ^ b) ^ a -> b */
746 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
748 } else if (ab == b) {
749 /* (a ^ b) ^ b -> a */
751 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
755 if (is_Eor(b) || is_Or_Eor_Add(b)) {
756 ir_node *ba = get_binop_left(b);
757 ir_node *bb = get_binop_right(b);
760 /* a ^ (a ^ b) -> b */
762 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
764 } else if (bb == a) {
765 /* a ^ (b ^ a) -> b */
767 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
775 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
777 * The second one looks strange, but this construct
778 * is used heavily in the LCC sources :-).
780 * Beware: The Mode of an Add may be different than the mode of its
781 * predecessors, so we could not return a predecessors in all cases.
783 static ir_node *equivalent_node_Add(ir_node *n)
786 ir_node *left, *right;
787 ir_mode *mode = get_irn_mode(n);
789 n = equivalent_node_neutral_zero(n);
793 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
794 if (mode_is_float(mode)) {
795 ir_graph *irg = get_irn_irg(n);
796 if (get_irg_fp_model(irg) & fp_strict_algebraic)
800 left = get_Add_left(n);
801 right = get_Add_right(n);
804 if (get_Sub_right(left) == right) {
807 n = get_Sub_left(left);
808 if (mode == get_irn_mode(n)) {
809 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
815 if (get_Sub_right(right) == left) {
818 n = get_Sub_left(right);
819 if (mode == get_irn_mode(n)) {
820 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
829 * optimize operations that are not commutative but have neutral 0 on left,
832 static ir_node *equivalent_node_left_zero(ir_node *n)
836 ir_node *a = get_binop_left(n);
837 ir_node *b = get_binop_right(n);
838 ir_tarval *tb = value_of(b);
840 if (tarval_is_null(tb)) {
843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
849 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
851 * The second one looks strange, but this construct
852 * is used heavily in the LCC sources :-).
854 * Beware: The Mode of a Sub may be different than the mode of its
855 * predecessors, so we could not return a predecessors in all cases.
857 static ir_node *equivalent_node_Sub(ir_node *n)
861 ir_mode *mode = get_irn_mode(n);
864 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
865 if (mode_is_float(mode)) {
866 ir_graph *irg = get_irn_irg(n);
867 if (get_irg_fp_model(irg) & fp_strict_algebraic)
871 b = get_Sub_right(n);
874 /* Beware: modes might be different */
875 if (tarval_is_null(tb)) {
876 ir_node *a = get_Sub_left(n);
877 if (mode == get_irn_mode(a)) {
880 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
888 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
891 * -(-a) == a, but might overflow two times.
892 * We handle it anyway here but the better way would be a
893 * flag. This would be needed for Pascal for instance.
895 static ir_node *equivalent_node_involution(ir_node *n)
898 ir_node *pred = get_unop_op(n);
899 if (get_irn_op(pred) == get_irn_op(n)) {
900 n = get_unop_op(pred);
901 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_INVOLUTION);
907 * Optimize a * 1 = 1 * a = a.
909 static ir_node *equivalent_node_Mul(ir_node *n)
912 ir_node *a = get_Mul_left(n);
914 /* we can handle here only the n * n = n bit cases */
915 if (get_irn_mode(n) == get_irn_mode(a)) {
916 ir_node *b = get_Mul_right(n);
920 * Mul is commutative and has again an other neutral element.
921 * Constants are place right, so check this case first.
924 if (tarval_is_one(tv)) {
926 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
929 if (tarval_is_one(tv)) {
931 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
939 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
941 static ir_node *equivalent_node_Or(ir_node *n)
945 ir_node *a = get_Or_left(n);
946 ir_node *b = get_Or_right(n);
950 n = a; /* idempotence */
951 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
954 /* constants are normalized to right, check this side first */
956 if (tarval_is_null(tv)) {
958 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
962 if (tarval_is_null(tv)) {
964 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
972 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
974 static ir_node *equivalent_node_And(ir_node *n)
978 ir_node *a = get_And_left(n);
979 ir_node *b = get_And_right(n);
983 n = a; /* idempotence */
984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
987 /* constants are normalized to right, check this side first */
989 if (tarval_is_all_one(tv)) {
991 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
994 if (tv != get_tarval_bad()) {
995 ir_mode *mode = get_irn_mode(n);
996 if (!mode_is_signed(mode) && is_Conv(a)) {
997 ir_node *convop = get_Conv_op(a);
998 ir_mode *convopmode = get_irn_mode(convop);
999 if (!mode_is_signed(convopmode)) {
1000 /* Check Conv(all_one) & Const = all_one */
1001 ir_tarval *one = get_mode_all_one(convopmode);
1002 ir_tarval *conv = tarval_convert_to(one, mode);
1003 ir_tarval *tand = tarval_and(conv, tv);
1005 if (tarval_is_all_one(tand)) {
1006 /* Conv(X) & Const = X */
1008 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1015 if (tarval_is_all_one(tv)) {
1017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1021 if ((is_Or(a) || is_Or_Eor_Add(a))
1022 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1024 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1028 if ((is_Or(b) || is_Or_Eor_Add(b))
1029 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1038 * Try to remove useless Conv's:
1040 static ir_node *equivalent_node_Conv(ir_node *n)
1043 ir_node *a = get_Conv_op(n);
1045 ir_mode *n_mode = get_irn_mode(n);
1046 ir_mode *a_mode = get_irn_mode(a);
1048 if (n_mode == a_mode) { /* No Conv necessary */
1050 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1052 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1053 ir_node *b = get_Conv_op(a);
1054 ir_mode *b_mode = get_irn_mode(b);
1056 if (n_mode == b_mode && values_in_mode(b_mode, a_mode)) {
1058 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1066 * - fold Phi-nodes, iff they have only one predecessor except
1069 static ir_node *equivalent_node_Phi(ir_node *n)
1074 ir_node *first_val = NULL; /* to shutup gcc */
1076 if (!get_opt_optimize() &&
1077 !irg_is_constrained(get_irn_irg(n), IR_GRAPH_CONSTRAINT_CONSTRUCTION))
1080 n_preds = get_Phi_n_preds(n);
1082 /* Phi of dead Region without predecessors. */
1086 /* Find first non-self-referencing input */
1087 for (i = 0; i < n_preds; ++i) {
1088 first_val = get_Phi_pred(n, i);
1089 /* not self pointer */
1090 if (first_val != n) {
1091 /* then found first value. */
1096 /* search for rest of inputs, determine if any of these
1097 are non-self-referencing */
1098 while (++i < n_preds) {
1099 ir_node *scnd_val = get_Phi_pred(n, i);
1100 if (scnd_val != n && scnd_val != first_val) {
1105 if (i >= n_preds && !is_Dummy(first_val)) {
1106 /* Fold, if no multiple distinct non-self-referencing inputs */
1108 DBG_OPT_PHI(oldn, n);
1114 * Optimize Proj(Tuple).
1116 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1118 ir_node *oldn = proj;
1119 ir_node *tuple = get_Proj_pred(proj);
1121 /* Remove the Tuple/Proj combination. */
1122 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1123 DBG_OPT_TUPLE(oldn, tuple, proj);
1129 * Optimize a / 1 = a.
1131 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1133 ir_node *oldn = proj;
1134 ir_node *div = get_Proj_pred(proj);
1135 ir_node *b = get_Div_right(div);
1136 ir_tarval *tb = value_of(b);
1138 /* Div is not commutative. */
1139 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1140 switch (get_Proj_proj(proj)) {
1142 proj = get_Div_mem(div);
1143 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1147 proj = get_Div_left(div);
1148 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1152 /* we cannot replace the exception Proj's here, this is done in
1153 transform_node_Proj_Div() */
1161 * Optimize CopyB(mem, x, x) into a Nop.
1163 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1165 ir_node *oldn = proj;
1166 ir_node *copyb = get_Proj_pred(proj);
1167 ir_node *a = get_CopyB_dst(copyb);
1168 ir_node *b = get_CopyB_src(copyb);
1171 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1172 switch (get_Proj_proj(proj)) {
1174 proj = get_CopyB_mem(copyb);
1175 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1183 * Does all optimizations on nodes that must be done on its Projs
1184 * because of creating new nodes.
1186 static ir_node *equivalent_node_Proj(ir_node *proj)
1188 ir_node *n = get_Proj_pred(proj);
1189 if (n->op->ops.equivalent_node_Proj)
1190 return n->op->ops.equivalent_node_Proj(proj);
1197 static ir_node *equivalent_node_Id(ir_node *n)
1205 DBG_OPT_ID(oldn, n);
1212 static ir_node *equivalent_node_Mux(ir_node *n)
1214 ir_node *oldn = n, *sel = get_Mux_sel(n);
1216 ir_tarval *ts = value_of(sel);
1218 if (ts == tarval_bad && is_Cmp(sel)) {
1219 /* try again with a direct call to compute_cmp, as we don't care
1220 * about the MODEB_LOWERED flag here */
1221 ts = compute_cmp_ext(sel);
1224 /* Mux(true, f, t) == t */
1225 if (ts == tarval_b_true) {
1226 n = get_Mux_true(n);
1227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1230 /* Mux(false, f, t) == f */
1231 if (ts == tarval_b_false) {
1232 n = get_Mux_false(n);
1233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1236 n_t = get_Mux_true(n);
1237 n_f = get_Mux_false(n);
1239 /* Mux(v, x, T) == x */
1240 if (is_Unknown(n_f)) {
1242 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1245 /* Mux(v, T, x) == x */
1246 if (is_Unknown(n_t)) {
1248 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1252 /* Mux(v, x, x) == x */
1255 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1258 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1259 ir_relation relation = get_Cmp_relation(sel);
1260 ir_node *f = get_Mux_false(n);
1261 ir_node *t = get_Mux_true(n);
1264 * Note further that these optimization work even for floating point
1265 * with NaN's because -NaN == NaN.
1266 * However, if +0 and -0 is handled differently, we cannot use the first one.
1268 ir_node *const cmp_l = get_Cmp_left(sel);
1269 ir_node *const cmp_r = get_Cmp_right(sel);
1272 case ir_relation_equal:
1273 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1274 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1281 case ir_relation_less_greater:
1282 case ir_relation_unordered_less_greater:
1283 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1284 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1286 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1295 * Note: normalization puts the constant on the right side,
1296 * so we check only one case.
1298 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1299 /* Mux(t CMP 0, X, t) */
1300 if (is_Minus(f) && get_Minus_op(f) == t) {
1301 /* Mux(t CMP 0, -t, t) */
1302 if (relation == ir_relation_equal) {
1303 /* Mux(t == 0, -t, t) ==> -t */
1305 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1306 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1307 /* Mux(t != 0, -t, t) ==> t */
1309 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1319 * Remove Confirm nodes if setting is on.
1320 * Replace Confirms(x, '=', Constlike) by Constlike.
1322 static ir_node *equivalent_node_Confirm(ir_node *n)
1324 ir_node *pred = get_Confirm_value(n);
1325 ir_relation relation = get_Confirm_relation(n);
1327 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1329 * rare case: two identical Confirms one after another,
1330 * replace the second one with the first.
1333 pred = get_Confirm_value(n);
1339 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1340 * perform no actual computation, as, e.g., the Id nodes. It does not create
1341 * new nodes. It is therefore safe to free n if the node returned is not n.
1342 * If a node returns a Tuple we can not just skip it. If the size of the
1343 * in array fits, we transform n into a tuple (e.g., Div).
1345 ir_node *equivalent_node(ir_node *n)
1347 if (n->op->ops.equivalent_node)
1348 return n->op->ops.equivalent_node(n);
1353 * Returns non-zero if a node is a Phi node
1354 * with all predecessors constant.
1356 static int is_const_Phi(ir_node *n)
1360 if (! is_Phi(n) || get_irn_arity(n) == 0)
1362 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1363 if (! is_Const(get_irn_n(n, i)))
1369 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1370 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1373 * in reality eval_func should be tarval (*eval_func)() but incomplete
1374 * declarations are bad style and generate noisy warnings
1376 typedef void (*eval_func)(void);
1379 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1381 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1383 if (eval == (eval_func) tarval_sub) {
1384 tarval_sub_type func = (tarval_sub_type)eval;
1386 return func(a, b, mode);
1388 tarval_binop_type func = (tarval_binop_type)eval;
1395 * Apply an evaluator on a binop with a constant operators (and one Phi).
1397 * @param phi the Phi node
1398 * @param other the other operand
1399 * @param eval an evaluator function
1400 * @param mode the mode of the result, may be different from the mode of the Phi!
1401 * @param left if non-zero, other is the left operand, else the right
1403 * @return a new Phi node if the conversion was successful, NULL else
1405 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1407 int n = get_irn_arity(phi);
1408 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1410 for (int i = 0; i < n; ++i) {
1411 ir_node *pred = get_irn_n(phi, i);
1412 ir_tarval *tv = get_Const_tarval(pred);
1413 tv = do_eval(eval, other, tv, mode);
1415 if (tv == tarval_bad) {
1416 /* folding failed, bad */
1422 for (int i = 0; i < n; ++i) {
1423 ir_node *pred = get_irn_n(phi, i);
1424 ir_tarval *tv = get_Const_tarval(pred);
1425 tv = do_eval(eval, tv, other, mode);
1427 if (tv == tarval_bad) {
1428 /* folding failed, bad */
1434 ir_graph *irg = get_irn_irg(phi);
1435 ir_node **res = ALLOCAN(ir_node*, n);
1436 for (int i = 0; i < n; ++i) {
1437 res[i] = new_r_Const(irg, tvs[i]);
1439 ir_node *block = get_nodes_block(phi);
1440 return new_r_Phi(block, n, res, mode);
1444 * Apply an evaluator on a binop with two constant Phi.
1446 * @param a the left Phi node
1447 * @param b the right Phi node
1448 * @param eval an evaluator function
1449 * @param mode the mode of the result, may be different from the mode of the Phi!
1451 * @return a new Phi node if the conversion was successful, NULL else
1453 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1455 if (get_nodes_block(a) != get_nodes_block(b))
1458 int n = get_irn_arity(a);
1459 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1460 for (int i = 0; i < n; ++i) {
1461 ir_node *pred_a = get_irn_n(a, i);
1462 ir_tarval *tv_l = get_Const_tarval(pred_a);
1463 ir_node *pred_b = get_irn_n(b, i);
1464 ir_tarval *tv_r = get_Const_tarval(pred_b);
1465 ir_tarval *tv = do_eval(eval, tv_l, tv_r, mode);
1467 if (tv == tarval_bad) {
1468 /* folding failed, bad */
1473 ir_graph *irg = get_irn_irg(a);
1474 ir_node **res = ALLOCAN(ir_node*, n);
1475 for (int i = 0; i < n; ++i) {
1476 res[i] = new_r_Const(irg, tvs[i]);
1478 ir_node *block = get_nodes_block(a);
1479 return new_r_Phi(block, n, res, mode);
1483 * Apply an evaluator on a unop with a constant operator (a Phi).
1485 * @param phi the Phi node
1486 * @param eval an evaluator function
1488 * @return a new Phi node if the conversion was successful, NULL else
1490 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1492 int n = get_irn_arity(phi);
1493 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1494 for (int i = 0; i < n; ++i) {
1495 ir_node *pred = get_irn_n(phi, i);
1496 ir_tarval *tv = get_Const_tarval(pred);
1499 if (tv == tarval_bad) {
1500 /* folding failed, bad */
1505 ir_graph *irg = get_irn_irg(phi);
1506 ir_node **res = ALLOCAN(ir_node*, n);
1507 for (int i = 0; i < n; ++i) {
1508 res[i] = new_r_Const(irg, tvs[i]);
1510 ir_node *block = get_nodes_block(phi);
1511 ir_mode *mode = get_irn_mode(phi);
1512 return new_r_Phi(block, n, res, mode);
1516 * Apply a conversion on a constant operator (a Phi).
1518 * @param phi the Phi node
1520 * @return a new Phi node if the conversion was successful, NULL else
1522 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1524 int n = get_irn_arity(phi);
1525 ir_tarval **tvs = ALLOCAN(ir_tarval*, n);
1526 for (int i = 0; i < n; ++i) {
1527 ir_node *pred = get_irn_n(phi, i);
1528 ir_tarval *tv = get_Const_tarval(pred);
1529 tv = tarval_convert_to(tv, mode);
1531 if (tv == tarval_bad) {
1532 /* folding failed, bad */
1537 ir_graph *irg = get_irn_irg(phi);
1538 ir_node **res = ALLOCAN(ir_node*, n);
1539 for (int i = 0; i < n; ++i) {
1540 res[i] = new_r_Const(irg, tvs[i]);
1542 ir_node *block = get_nodes_block(phi);
1543 return new_r_Phi(block, n, res, mode);
1547 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1548 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1549 * If possible, remove the Conv's.
1551 static ir_node *transform_node_AddSub(ir_node *n)
1553 ir_mode *mode = get_irn_mode(n);
1555 if (mode_is_reference(mode)) {
1556 ir_node *left = get_binop_left(n);
1557 ir_node *right = get_binop_right(n);
1558 unsigned ref_bits = get_mode_size_bits(mode);
1560 if (is_Conv(left)) {
1561 ir_mode *lmode = get_irn_mode(left);
1562 unsigned bits = get_mode_size_bits(lmode);
1564 if (ref_bits == bits &&
1565 mode_is_int(lmode) &&
1566 get_mode_arithmetic(lmode) == irma_twos_complement) {
1567 ir_node *pre = get_Conv_op(left);
1568 ir_mode *pre_mode = get_irn_mode(pre);
1570 if (mode_is_int(pre_mode) &&
1571 get_mode_size_bits(pre_mode) == bits &&
1572 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1573 /* ok, this conv just changes to sign, moreover the calculation
1574 * is done with same number of bits as our address mode, so
1575 * we can ignore the conv as address calculation can be viewed
1576 * as either signed or unsigned
1578 set_binop_left(n, pre);
1583 if (is_Conv(right)) {
1584 ir_mode *rmode = get_irn_mode(right);
1585 unsigned bits = get_mode_size_bits(rmode);
1587 if (ref_bits == bits &&
1588 mode_is_int(rmode) &&
1589 get_mode_arithmetic(rmode) == irma_twos_complement) {
1590 ir_node *pre = get_Conv_op(right);
1591 ir_mode *pre_mode = get_irn_mode(pre);
1593 if (mode_is_int(pre_mode) &&
1594 get_mode_size_bits(pre_mode) == bits &&
1595 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1596 /* ok, this conv just changes to sign, moreover the calculation
1597 * is done with same number of bits as our address mode, so
1598 * we can ignore the conv as address calculation can be viewed
1599 * as either signed or unsigned
1601 set_binop_right(n, pre);
1606 /* let address arithmetic use unsigned modes */
1607 if (is_Const(right)) {
1608 ir_mode *rmode = get_irn_mode(right);
1610 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1611 /* convert a AddP(P, *s) into AddP(P, *u) */
1612 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1614 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1615 set_binop_right(n, pre);
1623 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1626 if (is_Const(b) && is_const_Phi(a)) { \
1627 /* check for Op(Phi, Const) */ \
1628 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1630 else if (is_Const(a) && is_const_Phi(b)) { \
1631 /* check for Op(Const, Phi) */ \
1632 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1634 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1635 /* check for Op(Phi, Phi) */ \
1636 c = apply_binop_on_2_phis(a, b, eval, mode); \
1639 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1644 #define HANDLE_UNOP_PHI(eval, a, c) \
1647 if (is_const_Phi(a)) { \
1648 /* check for Op(Phi) */ \
1649 c = apply_unop_on_phi(a, eval); \
1651 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1658 * Create a 0 constant of given mode.
1660 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1662 ir_tarval *tv = get_mode_null(mode);
1663 ir_node *cnst = new_r_Const(irg, tv);
1668 static bool is_shiftop(const ir_node *n)
1670 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
1673 /* the order of the values is important! */
1674 typedef enum const_class {
1680 static const_class classify_const(const ir_node* n)
1682 if (is_Const(n)) return const_const;
1683 if (is_irn_constlike(n)) return const_like;
1688 * Determines whether r is more constlike or has a larger index (in that order)
1691 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
1693 const const_class l_order = classify_const(l);
1694 const const_class r_order = classify_const(r);
1696 l_order > r_order ||
1697 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
1700 static bool is_cmp_unequal(const ir_node *node)
1702 ir_relation relation = get_Cmp_relation(node);
1703 ir_node *left = get_Cmp_left(node);
1704 ir_node *right = get_Cmp_right(node);
1705 ir_mode *mode = get_irn_mode(left);
1707 if (relation == ir_relation_less_greater)
1710 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
1711 return relation == ir_relation_greater;
1716 * returns true for Cmp(x == 0) or Cmp(x != 0)
1718 static bool is_cmp_equality_zero(const ir_node *node)
1720 ir_relation relation;
1721 ir_node *right = get_Cmp_right(node);
1723 if (!is_Const(right) || !is_Const_null(right))
1725 relation = get_Cmp_relation(node);
1726 return relation == ir_relation_equal
1727 || relation == ir_relation_less_greater
1728 || (!mode_is_signed(get_irn_mode(right))
1729 && relation == ir_relation_greater);
1733 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1734 * Such pattern may arise in bitfield stores.
1736 * value c4 value c4 & c2
1737 * AND c3 AND c1 | c3
1744 * AND c1 ===> OR if (c1 | c2) == 0x111..11
1747 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
1749 ir_node *irn_and, *c1;
1751 ir_node *and_l, *c3;
1752 ir_node *value, *c4;
1753 ir_node *new_and, *new_const, *block;
1754 ir_mode *mode = get_irn_mode(irn_or);
1756 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
1760 irn_and = get_binop_left(irn_or);
1761 c1 = get_binop_right(irn_or);
1762 if (!is_Const(c1) || !is_And(irn_and))
1765 or_l = get_binop_left(irn_and);
1766 c2 = get_binop_right(irn_and);
1770 tv1 = get_Const_tarval(c1);
1771 tv2 = get_Const_tarval(c2);
1773 tv = tarval_or(tv1, tv2);
1774 if (tarval_is_all_one(tv)) {
1775 /* the AND does NOT clear a bit with isn't set by the OR */
1776 set_binop_left(irn_or, or_l);
1777 set_binop_right(irn_or, c1);
1779 /* check for more */
1783 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
1786 and_l = get_binop_left(or_l);
1787 c3 = get_binop_right(or_l);
1788 if (!is_Const(c3) || !is_And(and_l))
1791 value = get_binop_left(and_l);
1792 c4 = get_binop_right(and_l);
1796 /* ok, found the pattern, check for conditions */
1797 assert(mode == get_irn_mode(irn_and));
1798 assert(mode == get_irn_mode(or_l));
1799 assert(mode == get_irn_mode(and_l));
1801 tv3 = get_Const_tarval(c3);
1802 tv4 = get_Const_tarval(c4);
1804 tv = tarval_or(tv4, tv2);
1805 if (!tarval_is_all_one(tv)) {
1806 /* have at least one 0 at the same bit position */
1810 if (tv3 != tarval_andnot(tv3, tv4)) {
1811 /* bit in the or_mask is outside the and_mask */
1815 if (tv1 != tarval_andnot(tv1, tv2)) {
1816 /* bit in the or_mask is outside the and_mask */
1820 /* ok, all conditions met */
1821 block = get_nodes_block(irn_or);
1822 irg = get_irn_irg(block);
1824 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
1826 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
1828 set_binop_left(irn_or, new_and);
1829 set_binop_right(irn_or, new_const);
1831 /* check for more */
1836 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
1838 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
1840 ir_mode *mode = get_irn_mode(irn_or);
1841 ir_node *shl, *shr, *block;
1842 ir_node *irn, *x, *c1, *c2, *n;
1843 ir_tarval *tv1, *tv2;
1845 /* some backends can't handle rotl */
1846 if (!be_get_backend_param()->support_rotl)
1849 if (! mode_is_int(mode))
1852 shl = get_binop_left(irn_or);
1853 shr = get_binop_right(irn_or);
1862 } else if (!is_Shl(shl)) {
1864 } else if (!is_Shr(shr)) {
1867 x = get_Shl_left(shl);
1868 if (x != get_Shr_left(shr))
1871 c1 = get_Shl_right(shl);
1872 c2 = get_Shr_right(shr);
1873 if (is_Const(c1) && is_Const(c2)) {
1874 tv1 = get_Const_tarval(c1);
1875 if (! tarval_is_long(tv1))
1878 tv2 = get_Const_tarval(c2);
1879 if (! tarval_is_long(tv2))
1882 if (get_tarval_long(tv1) + get_tarval_long(tv2)
1883 != (int) get_mode_size_bits(mode))
1886 /* yet, condition met */
1887 block = get_nodes_block(irn_or);
1889 n = new_r_Rotl(block, x, c1, mode);
1891 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
1895 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
1896 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
1897 if (!ir_is_negated_value(c1, c2)) {
1901 /* yet, condition met */
1902 block = get_nodes_block(irn_or);
1903 n = new_r_Rotl(block, x, c1, mode);
1904 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
1909 * Prototype of a recursive transform function
1910 * for bitwise distributive transformations.
1912 typedef ir_node* (*recursive_transform)(ir_node *n);
1915 * makes use of distributive laws for and, or, eor
1916 * and(a OP c, b OP c) -> and(a, b) OP c
1917 * note, might return a different op than n
1919 static ir_node *transform_bitwise_distributive(ir_node *n,
1920 recursive_transform trans_func)
1923 ir_node *a = get_binop_left(n);
1924 ir_node *b = get_binop_right(n);
1925 ir_op *op = get_irn_op(a);
1926 ir_op *op_root = get_irn_op(n);
1928 if (op != get_irn_op(b))
1931 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
1932 if (op == op_Conv) {
1933 ir_node *a_op = get_Conv_op(a);
1934 ir_node *b_op = get_Conv_op(b);
1935 ir_mode *a_mode = get_irn_mode(a_op);
1936 ir_mode *b_mode = get_irn_mode(b_op);
1937 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
1938 ir_node *blk = get_nodes_block(n);
1941 set_binop_left(n, a_op);
1942 set_binop_right(n, b_op);
1943 set_irn_mode(n, a_mode);
1945 n = new_r_Conv(blk, n, get_irn_mode(oldn));
1947 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1953 /* nothing to gain here */
1957 if (op == op_Shrs || op == op_Shr || op == op_Shl
1958 || op == op_And || op == op_Or || op == op_Eor) {
1959 ir_node *a_left = get_binop_left(a);
1960 ir_node *a_right = get_binop_right(a);
1961 ir_node *b_left = get_binop_left(b);
1962 ir_node *b_right = get_binop_right(b);
1964 ir_node *op1 = NULL;
1965 ir_node *op2 = NULL;
1967 if (is_op_commutative(op)) {
1968 if (a_left == b_left) {
1972 } else if (a_left == b_right) {
1976 } else if (a_right == b_left) {
1982 if (a_right == b_right) {
1989 /* (a sop c) & (b sop c) => (a & b) sop c */
1990 ir_node *blk = get_nodes_block(n);
1992 ir_node *new_n = exact_copy(n);
1993 set_binop_left(new_n, op1);
1994 set_binop_right(new_n, op2);
1995 new_n = trans_func(new_n);
1997 if (op_root == op_Eor && op == op_Or) {
1998 dbg_info *dbgi = get_irn_dbg_info(n);
1999 ir_mode *mode = get_irn_mode(c);
2001 c = new_rd_Not(dbgi, blk, c, mode);
2002 n = new_rd_And(dbgi, blk, new_n, c, mode);
2005 set_nodes_block(n, blk);
2006 set_binop_left(n, new_n);
2007 set_binop_right(n, c);
2011 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2020 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2022 * - and, or, xor instead of &
2023 * - Shl, Shr, Shrs, rotl instead of >>
2024 * (with a special case for Or/Xor + Shrs)
2026 * This normalisation is usually good for the backend since << C can often be
2027 * matched as address-mode.
2029 static ir_node *transform_node_bitop_shift(ir_node *n)
2031 ir_graph *irg = get_irn_irg(n);
2032 ir_node *left = get_binop_left(n);
2033 ir_node *right = get_binop_right(n);
2034 ir_mode *mode = get_irn_mode(n);
2035 ir_node *shift_left;
2036 ir_node *shift_right;
2038 dbg_info *dbg_bitop;
2039 dbg_info *dbg_shift;
2045 ir_tarval *tv_bitop;
2047 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
2050 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2051 if (!is_Const(right) || !is_shiftop(left))
2054 shift_left = get_binop_left(left);
2055 shift_right = get_binop_right(left);
2056 if (!is_Const(shift_right))
2059 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2060 if (is_Shrs(left)) {
2061 /* TODO this could be improved */
2065 block = get_nodes_block(n);
2066 dbg_bitop = get_irn_dbg_info(n);
2067 dbg_shift = get_irn_dbg_info(left);
2068 tv1 = get_Const_tarval(shift_right);
2069 tv2 = get_Const_tarval(right);
2070 assert(get_tarval_mode(tv2) == mode);
2073 tv_bitop = tarval_shr(tv2, tv1);
2075 /* Check whether we have lost some bits during the right shift. */
2077 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2079 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2082 } else if (is_Shr(left)) {
2085 * TODO this can be improved by checking whether
2086 * the left shift produces an overflow
2090 tv_bitop = tarval_shl(tv2, tv1);
2092 assert(is_Rotl(left));
2093 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2095 new_const = new_r_Const(irg, tv_bitop);
2098 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2099 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2100 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2103 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2107 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2108 } else if (is_Shr(left)) {
2109 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2111 assert(is_Rotl(left));
2112 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2118 static bool complement_values(const ir_node *a, const ir_node *b)
2120 if (is_Not(a) && get_Not_op(a) == b)
2122 if (is_Not(b) && get_Not_op(b) == a)
2124 if (is_Const(a) && is_Const(b)) {
2125 ir_tarval *tv_a = get_Const_tarval(a);
2126 ir_tarval *tv_b = get_Const_tarval(b);
2127 return tarval_not(tv_a) == tv_b;
2132 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2135 * for associative operations fold:
2136 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2137 * This is a "light" version of the reassociation phase
2139 static ir_node *fold_constant_associativity(ir_node *node,
2140 tv_fold_binop_func fold)
2145 ir_node *right = get_binop_right(node);
2146 ir_node *left_right;
2153 if (!is_Const(right))
2156 op = get_irn_op(node);
2157 left = get_binop_left(node);
2158 if (get_irn_op(left) != op)
2161 left_right = get_binop_right(left);
2162 if (!is_Const(left_right))
2165 left_left = get_binop_left(left);
2166 c0 = get_Const_tarval(left_right);
2167 c1 = get_Const_tarval(right);
2168 irg = get_irn_irg(node);
2169 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2171 new_c = fold(c0, c1);
2172 if (new_c == tarval_bad)
2174 new_const = new_r_Const(irg, new_c);
2175 new_node = exact_copy(node);
2176 set_binop_left(new_node, left_left);
2177 set_binop_right(new_node, new_const);
2184 static ir_node *transform_node_Or_(ir_node *n)
2187 ir_node *a = get_binop_left(n);
2188 ir_node *b = get_binop_right(n);
2192 n = fold_constant_associativity(n, tarval_or);
2196 if (is_Not(a) && is_Not(b)) {
2197 /* ~a | ~b = ~(a&b) */
2198 ir_node *block = get_nodes_block(n);
2200 mode = get_irn_mode(n);
2203 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2204 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2205 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2209 /* we can combine the relations of two compares with the same operands */
2210 if (is_Cmp(a) && is_Cmp(b)) {
2211 ir_node *a_left = get_Cmp_left(a);
2212 ir_node *a_right = get_Cmp_right(a);
2213 ir_node *b_left = get_Cmp_left(b);
2214 ir_node *b_right = get_Cmp_right(b);
2215 if (a_left == b_left && b_left == b_right) {
2216 dbg_info *dbgi = get_irn_dbg_info(n);
2217 ir_node *block = get_nodes_block(n);
2218 ir_relation a_relation = get_Cmp_relation(a);
2219 ir_relation b_relation = get_Cmp_relation(b);
2220 ir_relation new_relation = a_relation | b_relation;
2221 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2223 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2224 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2225 && !mode_is_float(get_irn_mode(a_left))
2226 && !mode_is_float(get_irn_mode(b_left))) {
2227 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2228 ir_graph *irg = get_irn_irg(n);
2229 dbg_info *dbgi = get_irn_dbg_info(n);
2230 ir_node *block = get_nodes_block(n);
2231 ir_mode *a_mode = get_irn_mode(a_left);
2232 ir_mode *b_mode = get_irn_mode(b_left);
2233 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2234 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2235 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2236 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2237 ir_node *zero = create_zero_const(irg, b_mode);
2238 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2240 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2241 ir_graph *irg = get_irn_irg(n);
2242 dbg_info *dbgi = get_irn_dbg_info(n);
2243 ir_node *block = get_nodes_block(n);
2244 ir_mode *a_mode = get_irn_mode(a_left);
2245 ir_mode *b_mode = get_irn_mode(b_left);
2246 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2247 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2248 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2249 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
2250 ir_node *zero = create_zero_const(irg, a_mode);
2251 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_less_greater);
2256 mode = get_irn_mode(n);
2257 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2259 n = transform_node_Or_bf_store(n);
2262 n = transform_node_Or_Rotl(n);
2266 n = transform_bitwise_distributive(n, transform_node_Or_);
2269 n = transform_node_bitop_shift(n);
2276 static ir_node *transform_node_Or(ir_node *n)
2278 if (is_Or_Eor_Add(n)) {
2279 dbg_info *dbgi = get_irn_dbg_info(n);
2280 ir_node *block = get_nodes_block(n);
2281 ir_node *left = get_Or_left(n);
2282 ir_node *right = get_Or_right(n);
2283 ir_mode *mode = get_irn_mode(n);
2284 return new_rd_Add(dbgi, block, left, right, mode);
2286 return transform_node_Or_(n);
2292 static ir_node *transform_node_Eor_(ir_node *n)
2295 ir_node *a = get_binop_left(n);
2296 ir_node *b = get_binop_right(n);
2297 ir_mode *mode = get_irn_mode(n);
2300 n = fold_constant_associativity(n, tarval_eor);
2304 /* we can combine the relations of two compares with the same operands */
2305 if (is_Cmp(a) && is_Cmp(b)) {
2306 ir_node *a_left = get_Cmp_left(a);
2307 ir_node *a_right = get_Cmp_left(a);
2308 ir_node *b_left = get_Cmp_left(b);
2309 ir_node *b_right = get_Cmp_right(b);
2310 if (a_left == b_left && b_left == b_right) {
2311 dbg_info *dbgi = get_irn_dbg_info(n);
2312 ir_node *block = get_nodes_block(n);
2313 ir_relation a_relation = get_Cmp_relation(a);
2314 ir_relation b_relation = get_Cmp_relation(b);
2315 ir_relation new_relation = a_relation ^ b_relation;
2316 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2320 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2322 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2323 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2324 dbg_info *dbg = get_irn_dbg_info(n);
2325 ir_node *block = get_nodes_block(n);
2326 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2327 ir_node *new_left = get_Not_op(a);
2328 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2329 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2331 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2332 dbg_info *dbg = get_irn_dbg_info(n);
2333 ir_node *block = get_nodes_block(n);
2334 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2335 ir_node *new_right = get_Not_op(b);
2336 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2337 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2341 /* x ^ 1...1 -> ~1 */
2342 if (is_Const(b) && is_Const_all_one(b)) {
2343 n = new_r_Not(get_nodes_block(n), a, mode);
2344 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2348 n = transform_bitwise_distributive(n, transform_node_Eor_);
2351 n = transform_node_bitop_shift(n);
2358 static ir_node *transform_node_Eor(ir_node *n)
2360 if (is_Or_Eor_Add(n)) {
2361 dbg_info *dbgi = get_irn_dbg_info(n);
2362 ir_node *block = get_nodes_block(n);
2363 ir_node *left = get_Eor_left(n);
2364 ir_node *right = get_Eor_right(n);
2365 ir_mode *mode = get_irn_mode(n);
2366 return new_rd_Add(dbgi, block, left, right, mode);
2368 return transform_node_Eor_(n);
2372 * Do the AddSub optimization, then Transform
2373 * Constant folding on Phi
2374 * Add(a,a) -> Mul(a, 2)
2375 * Add(Mul(a, x), a) -> Mul(a, x+1)
2376 * if the mode is integer or float.
2377 * Transform Add(a,-b) into Sub(a,b).
2378 * Reassociation might fold this further.
2380 static ir_node *transform_node_Add(ir_node *n)
2388 n = fold_constant_associativity(n, tarval_add);
2392 n = transform_node_AddSub(n);
2396 a = get_Add_left(n);
2397 b = get_Add_right(n);
2398 mode = get_irn_mode(n);
2400 if (mode_is_reference(mode)) {
2401 ir_mode *lmode = get_irn_mode(a);
2403 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2404 /* an Add(a, NULL) is a hidden Conv */
2405 dbg_info *dbg = get_irn_dbg_info(n);
2406 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2410 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2411 ir_tarval *tv = get_Const_tarval(b);
2412 ir_tarval *min = get_mode_min(mode);
2413 /* if all bits are set, then this has the same effect as a Not.
2414 * Note that the following == gives false for different modes which
2415 * is exactly what we want */
2417 dbg_info *dbgi = get_irn_dbg_info(n);
2418 ir_graph *irg = get_irn_irg(n);
2419 ir_node *block = get_nodes_block(n);
2420 ir_node *cnst = new_r_Const(irg, min);
2421 return new_rd_Eor(dbgi, block, a, cnst, mode);
2425 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2427 /* for FP the following optimizations are only allowed if
2428 * fp_strict_algebraic is disabled */
2429 if (mode_is_float(mode)) {
2430 ir_graph *irg = get_irn_irg(n);
2431 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2435 if (mode_is_num(mode)) {
2436 ir_graph *irg = get_irn_irg(n);
2437 /* the following code leads to endless recursion when Mul are replaced
2438 * by a simple instruction chain */
2439 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP)
2440 && a == b && mode_is_int(mode)) {
2441 ir_node *block = get_nodes_block(n);
2444 get_irn_dbg_info(n),
2447 new_r_Const_long(irg, mode, 2),
2449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2454 get_irn_dbg_info(n),
2459 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2464 get_irn_dbg_info(n),
2469 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2472 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2473 /* Here we rely on constants be on the RIGHT side */
2475 ir_node *op = get_Not_op(a);
2477 if (is_Const(b) && is_Const_one(b)) {
2479 ir_node *blk = get_nodes_block(n);
2480 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2481 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2488 if (is_Or_Eor_Add(n)) {
2489 n = transform_node_Or_(n);
2492 n = transform_node_Eor_(n);
2501 * returns -cnst or NULL if impossible
2503 static ir_node *const_negate(ir_node *cnst)
2505 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2506 dbg_info *dbgi = get_irn_dbg_info(cnst);
2507 ir_graph *irg = get_irn_irg(cnst);
2508 if (tv == tarval_bad) return NULL;
2509 return new_rd_Const(dbgi, irg, tv);
2513 * Do the AddSub optimization, then Transform
2514 * Constant folding on Phi
2515 * Sub(0,a) -> Minus(a)
2516 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2517 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2518 * Sub(Add(a, x), x) -> a
2519 * Sub(x, Add(x, a)) -> -a
2520 * Sub(x, Const) -> Add(x, -Const)
2522 static ir_node *transform_node_Sub(ir_node *n)
2528 n = transform_node_AddSub(n);
2530 a = get_Sub_left(n);
2531 b = get_Sub_right(n);
2533 mode = get_irn_mode(n);
2535 if (mode_is_int(mode)) {
2536 ir_mode *lmode = get_irn_mode(a);
2538 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2539 /* a Sub(a, NULL) is a hidden Conv */
2540 dbg_info *dbg = get_irn_dbg_info(n);
2541 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2542 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2546 if (mode == lmode &&
2547 get_mode_arithmetic(mode) == irma_twos_complement &&
2549 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2551 dbg_info *dbg = get_irn_dbg_info(n);
2552 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2553 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2559 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2561 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2562 if (mode_is_float(mode)) {
2563 ir_graph *irg = get_irn_irg(n);
2564 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2568 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2569 /* a - C -> a + (-C) */
2570 ir_node *cnst = const_negate(b);
2572 ir_node *block = get_nodes_block(n);
2573 dbg_info *dbgi = get_irn_dbg_info(n);
2575 n = new_rd_Add(dbgi, block, a, cnst, mode);
2576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2581 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2582 dbg_info *dbg = get_irn_dbg_info(n);
2583 ir_node *block = get_nodes_block(n);
2584 ir_node *left = get_Minus_op(a);
2585 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2587 n = new_rd_Minus(dbg, block, add, mode);
2588 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2590 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2591 dbg_info *dbg = get_irn_dbg_info(n);
2592 ir_node *block = get_nodes_block(n);
2593 ir_node *right = get_Minus_op(b);
2595 n = new_rd_Add(dbg, block, a, right, mode);
2596 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2598 } else if (is_Sub(b)) {
2599 /* a - (b - c) -> a + (c - b)
2600 * -> (a - b) + c iff (b - c) is a pointer */
2601 dbg_info *s_dbg = get_irn_dbg_info(b);
2602 ir_node *s_left = get_Sub_left(b);
2603 ir_node *s_right = get_Sub_right(b);
2604 ir_mode *s_mode = get_irn_mode(b);
2605 if (mode_is_reference(s_mode)) {
2606 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2607 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2608 dbg_info *a_dbg = get_irn_dbg_info(n);
2611 s_right = new_r_Conv(lowest_block, s_right, mode);
2612 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2614 ir_node *s_block = get_nodes_block(b);
2615 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2616 dbg_info *a_dbg = get_irn_dbg_info(n);
2617 ir_node *a_block = get_nodes_block(n);
2619 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2621 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2625 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2626 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2628 get_irn_dbg_info(n),
2632 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2635 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2636 ir_node *left = get_binop_left(a);
2637 ir_node *right = get_binop_right(a);
2639 /* FIXME: Does the Conv's work only for two complement or generally? */
2641 if (mode != get_irn_mode(right)) {
2642 /* This Sub is an effective Cast */
2643 right = new_r_Conv(get_nodes_block(n), right, mode);
2646 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2648 } else if (right == b) {
2649 if (mode != get_irn_mode(left)) {
2650 /* This Sub is an effective Cast */
2651 left = new_r_Conv(get_nodes_block(n), left, mode);
2654 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2658 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
2659 ir_node *left = get_binop_left(b);
2660 ir_node *right = get_binop_right(b);
2662 /* FIXME: Does the Conv's work only for two complement or generally? */
2664 ir_mode *r_mode = get_irn_mode(right);
2666 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2667 if (mode != r_mode) {
2668 /* This Sub is an effective Cast */
2669 n = new_r_Conv(get_nodes_block(n), n, mode);
2671 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2673 } else if (right == a) {
2674 ir_mode *l_mode = get_irn_mode(left);
2676 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2677 if (mode != l_mode) {
2678 /* This Sub is an effective Cast */
2679 n = new_r_Conv(get_nodes_block(n), n, mode);
2681 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2685 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2686 ir_mode *mode = get_irn_mode(a);
2688 if (mode == get_irn_mode(b)) {
2690 ir_node *op_a = get_Conv_op(a);
2691 ir_node *op_b = get_Conv_op(b);
2693 /* check if it's allowed to skip the conv */
2694 ma = get_irn_mode(op_a);
2695 mb = get_irn_mode(op_b);
2697 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2698 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2701 set_Sub_right(n, b);
2707 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2708 if (!is_reassoc_running() && is_Mul(a)) {
2709 ir_node *ma = get_Mul_left(a);
2710 ir_node *mb = get_Mul_right(a);
2713 ir_node *blk = get_nodes_block(n);
2714 ir_graph *irg = get_irn_irg(n);
2716 get_irn_dbg_info(n),
2720 get_irn_dbg_info(n),
2723 new_r_Const(irg, get_mode_one(mode)),
2726 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2728 } else if (mb == b) {
2729 ir_node *blk = get_nodes_block(n);
2730 ir_graph *irg = get_irn_irg(n);
2732 get_irn_dbg_info(n),
2736 get_irn_dbg_info(n),
2739 new_r_Const(irg, get_mode_one(mode)),
2742 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2746 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2747 ir_node *x = get_Sub_left(a);
2748 ir_node *y = get_Sub_right(a);
2749 ir_node *blk = get_nodes_block(n);
2750 ir_mode *m_b = get_irn_mode(b);
2751 ir_mode *m_y = get_irn_mode(y);
2755 /* Determine the right mode for the Add. */
2758 else if (mode_is_reference(m_b))
2760 else if (mode_is_reference(m_y))
2764 * Both modes are different but none is reference,
2765 * happens for instance in SubP(SubP(P, Iu), Is).
2766 * We have two possibilities here: Cast or ignore.
2767 * Currently we ignore this case.
2772 add = new_r_Add(blk, y, b, add_mode);
2774 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2775 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2779 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2780 /* c - ~X = X + (c+1) */
2781 if (is_Const(a) && is_Not(b)) {
2782 ir_tarval *tv = get_Const_tarval(a);
2784 tv = tarval_add(tv, get_mode_one(mode));
2785 if (tv != tarval_bad) {
2786 ir_node *blk = get_nodes_block(n);
2787 ir_graph *irg = get_irn_irg(n);
2788 ir_node *c = new_r_Const(irg, tv);
2789 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2794 /* x-(x&y) = x & ~y */
2796 ir_node *and_left = get_And_left(b);
2797 ir_node *and_right = get_And_right(b);
2798 if (and_right == a) {
2799 ir_node *tmp = and_left;
2800 and_left = and_right;
2803 if (and_left == a) {
2804 dbg_info *dbgi = get_irn_dbg_info(n);
2805 ir_node *block = get_nodes_block(n);
2806 ir_mode *mode = get_irn_mode(n);
2807 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2808 ir_node *andn = new_rd_And(dbgi, block, a, notn, mode);
2817 * Several transformation done on n*n=2n bits mul.
2818 * These transformations must be done here because new nodes may be produced.
2820 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2823 ir_node *a = get_Mul_left(n);
2824 ir_node *b = get_Mul_right(n);
2825 ir_tarval *ta = value_of(a);
2826 ir_tarval *tb = value_of(b);
2827 ir_mode *smode = get_irn_mode(a);
2829 if (ta == get_mode_one(smode)) {
2830 /* (L)1 * (L)b = (L)b */
2831 ir_node *blk = get_nodes_block(n);
2832 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2833 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2836 else if (ta == get_mode_minus_one(smode)) {
2837 /* (L)-1 * (L)b = (L)b */
2838 ir_node *blk = get_nodes_block(n);
2839 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2840 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2841 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2844 if (tb == get_mode_one(smode)) {
2845 /* (L)a * (L)1 = (L)a */
2846 ir_node *blk = get_nodes_block(a);
2847 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2848 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2851 else if (tb == get_mode_minus_one(smode)) {
2852 /* (L)a * (L)-1 = (L)-a */
2853 ir_node *blk = get_nodes_block(n);
2854 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2855 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2863 * Transform Mul(a,-1) into -a.
2864 * Do constant evaluation of Phi nodes.
2865 * Do architecture dependent optimizations on Mul nodes
2867 static ir_node *transform_node_Mul(ir_node *n)
2869 ir_node *c, *oldn = n;
2870 ir_mode *mode = get_irn_mode(n);
2871 ir_node *a = get_Mul_left(n);
2872 ir_node *b = get_Mul_right(n);
2874 n = fold_constant_associativity(n, tarval_mul);
2878 if (mode != get_irn_mode(a))
2879 return transform_node_Mul2n(n, mode);
2881 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2883 if (mode_is_signed(mode)) {
2886 if (value_of(a) == get_mode_minus_one(mode))
2888 else if (value_of(b) == get_mode_minus_one(mode))
2891 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2892 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2897 if (is_Const(b)) { /* (-a) * const -> a * -const */
2898 ir_node *cnst = const_negate(b);
2900 dbg_info *dbgi = get_irn_dbg_info(n);
2901 ir_node *block = get_nodes_block(n);
2902 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2903 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2906 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2907 dbg_info *dbgi = get_irn_dbg_info(n);
2908 ir_node *block = get_nodes_block(n);
2909 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2910 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2912 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2913 ir_node *sub_l = get_Sub_left(b);
2914 ir_node *sub_r = get_Sub_right(b);
2915 dbg_info *dbgi = get_irn_dbg_info(n);
2916 ir_node *block = get_nodes_block(n);
2917 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2918 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2919 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2922 } else if (is_Minus(b)) {
2923 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2924 ir_node *sub_l = get_Sub_left(a);
2925 ir_node *sub_r = get_Sub_right(a);
2926 dbg_info *dbgi = get_irn_dbg_info(n);
2927 ir_node *block = get_nodes_block(n);
2928 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2929 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2933 } else if (is_Shl(a)) {
2934 ir_node *const shl_l = get_Shl_left(a);
2935 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2936 /* (1 << x) * b -> b << x */
2937 dbg_info *const dbgi = get_irn_dbg_info(n);
2938 ir_node *const block = get_nodes_block(n);
2939 ir_node *const shl_r = get_Shl_right(a);
2940 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2941 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2944 } else if (is_Shl(b)) {
2945 ir_node *const shl_l = get_Shl_left(b);
2946 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2947 /* a * (1 << x) -> a << x */
2948 dbg_info *const dbgi = get_irn_dbg_info(n);
2949 ir_node *const block = get_nodes_block(n);
2950 ir_node *const shl_r = get_Shl_right(b);
2951 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2952 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2956 if (get_mode_arithmetic(mode) == irma_ieee754
2957 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
2959 ir_tarval *tv = get_Const_tarval(a);
2960 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2961 && !tarval_is_negative(tv)) {
2962 /* 2.0 * b = b + b */
2963 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2964 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2968 else if (is_Const(b)) {
2969 ir_tarval *tv = get_Const_tarval(b);
2970 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
2971 && !tarval_is_negative(tv)) {
2972 /* a * 2.0 = a + a */
2973 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2974 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2979 return arch_dep_replace_mul_with_shifts(n);
2983 * Transform a Div Node.
2985 static ir_node *transform_node_Div(ir_node *n)
2987 ir_mode *mode = get_Div_resmode(n);
2988 ir_node *a = get_Div_left(n);
2989 ir_node *b = get_Div_right(n);
2991 const ir_node *dummy;
2993 if (mode_is_int(mode)) {
2994 if (is_Const(b) && is_const_Phi(a)) {
2995 /* check for Div(Phi, Const) */
2996 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2998 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3001 } else if (is_Const(a) && is_const_Phi(b)) {
3002 /* check for Div(Const, Phi) */
3003 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3005 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3008 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3009 /* check for Div(Phi, Phi) */
3010 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3012 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3017 if (a == b && value_not_zero(a, &dummy)) {
3018 ir_graph *irg = get_irn_irg(n);
3019 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3020 value = new_r_Const(irg, get_mode_one(mode));
3021 DBG_OPT_CSTEVAL(n, value);
3024 if (mode_is_signed(mode) && is_Const(b)) {
3025 ir_tarval *tv = get_Const_tarval(b);
3027 if (tv == get_mode_minus_one(mode)) {
3029 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3030 DBG_OPT_CSTEVAL(n, value);
3034 /* Try architecture dependent optimization */
3035 value = arch_dep_replace_div_by_const(n);
3038 assert(mode_is_float(mode));
3040 /* Optimize x/c to x*(1/c) */
3041 ir_tarval *tv = value_of(b);
3043 if (tv != tarval_bad) {
3044 tv = tarval_div(get_mode_one(mode), tv);
3046 /* Do the transformation if the result is either exact or we are
3047 not using strict rules. */
3048 if (tv != tarval_bad &&
3049 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3050 ir_node *block = get_nodes_block(n);
3051 ir_graph *irg = get_irn_irg(block);
3052 ir_node *c = new_r_Const(irg, tv);
3053 dbg_info *dbgi = get_irn_dbg_info(n);
3054 value = new_rd_Mul(dbgi, block, a, c, mode);
3066 /* Turn Div into a tuple (mem, jmp, bad, value) */
3067 mem = get_Div_mem(n);
3068 blk = get_nodes_block(n);
3069 irg = get_irn_irg(blk);
3071 /* skip a potential Pin */
3072 mem = skip_Pin(mem);
3073 ir_node *const in[] = {
3075 [pn_Div_res] = value,
3076 [pn_Div_X_regular] = new_r_Jmp(blk),
3077 [pn_Div_X_except] = new_r_Bad(irg, mode_X),
3079 turn_into_tuple(n, ARRAY_SIZE(in), in);
3085 * Transform a Mod node.
3087 static ir_node *transform_node_Mod(ir_node *n)
3089 ir_mode *mode = get_Mod_resmode(n);
3090 ir_node *a = get_Mod_left(n);
3091 ir_node *b = get_Mod_right(n);
3096 if (is_Const(b) && is_const_Phi(a)) {
3097 /* check for Div(Phi, Const) */
3098 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3100 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3104 else if (is_Const(a) && is_const_Phi(b)) {
3105 /* check for Div(Const, Phi) */
3106 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3108 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3112 else if (is_const_Phi(a) && is_const_Phi(b)) {
3113 /* check for Div(Phi, Phi) */
3114 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3116 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3123 irg = get_irn_irg(n);
3124 if (tv != tarval_bad) {
3125 value = new_r_Const(irg, tv);
3127 DBG_OPT_CSTEVAL(n, value);
3130 ir_node *a = get_Mod_left(n);
3131 ir_node *b = get_Mod_right(n);
3132 const ir_node *dummy;
3134 if (a == b && value_not_zero(a, &dummy)) {
3135 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3136 value = new_r_Const(irg, get_mode_null(mode));
3137 DBG_OPT_CSTEVAL(n, value);
3140 if (mode_is_signed(mode) && is_Const(b)) {
3141 ir_tarval *tv = get_Const_tarval(b);
3143 if (tv == get_mode_minus_one(mode)) {
3145 value = new_r_Const(irg, get_mode_null(mode));
3146 DBG_OPT_CSTEVAL(n, value);
3150 /* Try architecture dependent optimization */
3151 value = arch_dep_replace_mod_by_const(n);
3160 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3161 mem = get_Mod_mem(n);
3162 blk = get_nodes_block(n);
3163 irg = get_irn_irg(blk);
3165 /* skip a potential Pin */
3166 mem = skip_Pin(mem);
3167 ir_node *const in[] = {
3169 [pn_Mod_res] = value,
3170 [pn_Mod_X_regular] = new_r_Jmp(blk),
3171 [pn_Mod_X_except] = new_r_Bad(irg, mode_X),
3173 turn_into_tuple(n, ARRAY_SIZE(in), in);
3179 * Transform a Cond node.
3181 * Replace the Cond by a Jmp if it branches on a constant
3184 static ir_node *transform_node_Cond(ir_node *n)
3186 ir_node *a = get_Cond_selector(n);
3187 ir_graph *irg = get_irn_irg(n);
3190 /* we need block info which is not available in floating irgs */
3191 if (get_irg_pinned(irg) == op_pin_state_floats)
3195 if (ta == tarval_bad && is_Cmp(a)) {
3196 /* try again with a direct call to compute_cmp, as we don't care
3197 * about the MODEB_LOWERED flag here */
3198 ta = compute_cmp_ext(a);
3201 if (ta != tarval_bad) {
3202 /* It's branching on a boolean constant.
3203 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3204 ir_node *const blk = get_nodes_block(n);
3205 ir_node *const jmp = new_r_Jmp(blk);
3206 ir_node *const bad = new_r_Bad(irg, mode_X);
3207 bool const cond = ta == tarval_b_true;
3208 ir_node *const in[] = {
3209 [pn_Cond_false] = cond ? bad : jmp,
3210 [pn_Cond_true] = cond ? jmp : bad,
3212 turn_into_tuple(n, ARRAY_SIZE(in), in);
3213 clear_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
3218 static ir_node *transform_node_Switch(ir_node *n)
3220 ir_node *op = get_Switch_selector(n);
3221 ir_tarval *val = value_of(op);
3222 if (val != tarval_bad) {
3223 dbg_info *dbgi = get_irn_dbg_info(n);
3224 ir_graph *irg = get_irn_irg(n);
3225 unsigned n_outs = get_Switch_n_outs(n);
3226 ir_node *block = get_nodes_block(n);
3227 ir_node *bad = new_r_Bad(irg, mode_X);
3228 ir_node **in = XMALLOCN(ir_node*, n_outs);
3229 const ir_switch_table *table = get_Switch_table(n);
3230 size_t n_entries = ir_switch_table_get_n_entries(table);
3234 for (i = 0; i < n_entries; ++i) {
3235 const ir_switch_table_entry *entry
3236 = ir_switch_table_get_entry_const(table, i);
3237 ir_tarval *min = entry->min;
3238 ir_tarval *max = entry->max;
3241 if ((min == max && min == val)
3242 || (tarval_cmp(val, min) != ir_relation_less
3243 && tarval_cmp(val, max) != ir_relation_greater)) {
3248 for (o = 0; o < n_outs; ++o) {
3249 if (o == (unsigned)jmp_pn) {
3250 in[o] = new_rd_Jmp(dbgi, block);
3255 return new_r_Tuple(block, (int)n_outs, in);
3261 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3263 * - and, or, xor instead of &
3264 * - Shl, Shr, Shrs, rotl instead of >>
3265 * (with a special case for Or/Xor + Shrs)
3267 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3269 static ir_node *transform_node_shift_bitop(ir_node *n)
3271 ir_graph *irg = get_irn_irg(n);
3272 ir_node *right = get_binop_right(n);
3273 ir_mode *mode = get_irn_mode(n);
3275 ir_node *bitop_left;
3276 ir_node *bitop_right;
3285 ir_tarval *tv_shift;
3287 if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_NORMALISATION2))
3290 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3292 if (!is_Const(right))
3295 left = get_binop_left(n);
3296 op_left = get_irn_op(left);
3297 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3300 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3301 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3302 /* TODO: test if sign bit is affectes */
3306 bitop_right = get_binop_right(left);
3307 if (!is_Const(bitop_right))
3310 bitop_left = get_binop_left(left);
3312 block = get_nodes_block(n);
3313 dbgi = get_irn_dbg_info(n);
3314 tv1 = get_Const_tarval(bitop_right);
3315 tv2 = get_Const_tarval(right);
3317 assert(get_tarval_mode(tv1) == mode);
3320 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3321 tv_shift = tarval_shl(tv1, tv2);
3322 } else if (is_Shr(n)) {
3323 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3324 tv_shift = tarval_shr(tv1, tv2);
3325 } else if (is_Shrs(n)) {
3326 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3327 tv_shift = tarval_shrs(tv1, tv2);
3330 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3331 tv_shift = tarval_rotl(tv1, tv2);
3334 assert(get_tarval_mode(tv_shift) == mode);
3335 irg = get_irn_irg(n);
3336 new_const = new_r_Const(irg, tv_shift);
3338 if (op_left == op_And) {
3339 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3340 } else if (op_left == op_Or) {
3341 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3343 assert(op_left == op_Eor);
3344 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3353 static ir_node *transform_node_And(ir_node *n)
3355 ir_node *c, *oldn = n;
3356 ir_node *a = get_And_left(n);
3357 ir_node *b = get_And_right(n);
3360 n = fold_constant_associativity(n, tarval_and);
3364 if (is_Cmp(a) && is_Cmp(b)) {
3365 ir_node *a_left = get_Cmp_left(a);
3366 ir_node *a_right = get_Cmp_right(a);
3367 ir_node *b_left = get_Cmp_left(b);
3368 ir_node *b_right = get_Cmp_right(b);
3369 ir_relation a_relation = get_Cmp_relation(a);
3370 ir_relation b_relation = get_Cmp_relation(b);
3371 /* we can combine the relations of two compares with the same
3373 if (a_left == b_left && b_left == b_right) {
3374 dbg_info *dbgi = get_irn_dbg_info(n);
3375 ir_node *block = get_nodes_block(n);
3376 ir_relation new_relation = a_relation & b_relation;
3377 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3379 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3380 if (a_relation == b_relation && a_relation == ir_relation_equal
3381 && !mode_is_float(get_irn_mode(a_left))
3382 && !mode_is_float(get_irn_mode(b_left))) {
3383 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3384 dbg_info *dbgi = get_irn_dbg_info(n);
3385 ir_node *block = get_nodes_block(n);
3386 ir_mode *a_mode = get_irn_mode(a_left);
3387 ir_mode *b_mode = get_irn_mode(b_left);
3388 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3389 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3390 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3391 ir_node *orn = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3392 ir_graph *irg = get_irn_irg(n);
3393 ir_node *zero = create_zero_const(irg, b_mode);
3394 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3396 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3397 dbg_info *dbgi = get_irn_dbg_info(n);
3398 ir_node *block = get_nodes_block(n);
3399 ir_mode *a_mode = get_irn_mode(a_left);
3400 ir_mode *b_mode = get_irn_mode(b_left);
3401 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3402 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3403 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3404 ir_node *orn = new_rd_Or(dbgi, block, xora, conv, a_mode);
3405 ir_graph *irg = get_irn_irg(n);
3406 ir_node *zero = create_zero_const(irg, a_mode);
3407 return new_rd_Cmp(dbgi, block, orn, zero, ir_relation_equal);
3412 mode = get_irn_mode(n);
3413 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3415 if (is_Or(a) || is_Or_Eor_Add(a)) {
3416 ir_node *or_left = get_binop_left(a);
3417 ir_node *or_right = get_binop_right(a);
3418 if (complement_values(or_left, b)) {
3419 /* (a|b) & ~a => b & ~a */
3420 dbg_info *dbgi = get_irn_dbg_info(n);
3421 ir_node *block = get_nodes_block(n);
3422 return new_rd_And(dbgi, block, or_right, b, mode);
3423 } else if (complement_values(or_right, b)) {
3424 /* (a|b) & ~b => a & ~b */
3425 dbg_info *dbgi = get_irn_dbg_info(n);
3426 ir_node *block = get_nodes_block(n);
3427 return new_rd_And(dbgi, block, or_left, b, mode);
3428 } else if (is_Not(b)) {
3429 ir_node *op = get_Not_op(b);
3431 ir_node *ba = get_And_left(op);
3432 ir_node *bb = get_And_right(op);
3434 /* it's enough to test the following cases due to normalization! */
3435 if (or_left == ba && or_right == bb) {
3436 /* (a|b) & ~(a&b) = a^b */
3437 ir_node *block = get_nodes_block(n);
3439 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3440 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3446 if (is_Or(b) || is_Or_Eor_Add(b)) {
3447 ir_node *or_left = get_binop_left(b);
3448 ir_node *or_right = get_binop_right(b);
3449 if (complement_values(or_left, a)) {
3450 /* (a|b) & ~a => b & ~a */
3451 dbg_info *dbgi = get_irn_dbg_info(n);
3452 ir_node *block = get_nodes_block(n);
3453 return new_rd_And(dbgi, block, or_right, a, mode);
3454 } else if (complement_values(or_right, a)) {
3455 /* (a|b) & ~b => a & ~b */
3456 dbg_info *dbgi = get_irn_dbg_info(n);
3457 ir_node *block = get_nodes_block(n);
3458 return new_rd_And(dbgi, block, or_left, a, mode);
3459 } else if (is_Not(a)) {
3460 ir_node *op = get_Not_op(a);
3462 ir_node *aa = get_And_left(op);
3463 ir_node *ab = get_And_right(op);
3465 /* it's enough to test the following cases due to normalization! */
3466 if (or_left == aa && or_right == ab) {
3467 /* (a|b) & ~(a&b) = a^b */
3468 ir_node *block = get_nodes_block(n);
3470 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3471 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3477 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3478 ir_node *al = get_binop_left(a);
3479 ir_node *ar = get_binop_right(a);
3482 /* (b ^ a) & b -> ~a & b */
3483 dbg_info *dbg = get_irn_dbg_info(n);
3484 ir_node *block = get_nodes_block(n);
3486 ar = new_rd_Not(dbg, block, ar, mode);
3487 n = new_rd_And(dbg, block, ar, b, mode);
3488 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3492 /* (a ^ b) & b -> ~a & b */
3493 dbg_info *dbg = get_irn_dbg_info(n);
3494 ir_node *block = get_nodes_block(n);
3496 al = new_rd_Not(dbg, block, al, mode);
3497 n = new_rd_And(dbg, block, al, b, mode);
3498 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3502 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3503 ir_node *bl = get_binop_left(b);
3504 ir_node *br = get_binop_right(b);
3507 /* a & (a ^ b) -> a & ~b */
3508 dbg_info *dbg = get_irn_dbg_info(n);
3509 ir_node *block = get_nodes_block(n);
3511 br = new_rd_Not(dbg, block, br, mode);
3512 n = new_rd_And(dbg, block, br, a, mode);
3513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3517 /* a & (b ^ a) -> a & ~b */
3518 dbg_info *dbg = get_irn_dbg_info(n);
3519 ir_node *block = get_nodes_block(n);
3521 bl = new_rd_Not(dbg, block, bl, mode);
3522 n = new_rd_And(dbg, block, bl, a, mode);
3523 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3527 if (is_Not(a) && is_Not(b)) {
3528 /* ~a & ~b = ~(a|b) */
3529 ir_node *block = get_nodes_block(n);
3530 ir_mode *mode = get_irn_mode(n);
3534 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3535 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3536 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3541 vrp_attr *b_vrp = vrp_get_info(b);
3542 ir_tarval *a_val = get_Const_tarval(a);
3543 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3549 vrp_attr *a_vrp = vrp_get_info(a);
3550 ir_tarval *b_val = get_Const_tarval(b);
3551 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3556 n = transform_bitwise_distributive(n, transform_node_And);
3558 n = transform_node_bitop_shift(n);
3566 static ir_node *transform_node_Not(ir_node *n)
3568 ir_node *c, *oldn = n;
3569 ir_node *a = get_Not_op(n);
3570 ir_mode *mode = get_irn_mode(n);
3572 HANDLE_UNOP_PHI(tarval_not,a,c);
3574 /* check for a boolean Not */
3576 dbg_info *dbgi = get_irn_dbg_info(a);
3577 ir_node *block = get_nodes_block(a);
3578 ir_relation relation = get_Cmp_relation(a);
3579 relation = get_negated_relation(relation);
3580 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3581 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3585 /* normalize ~(a ^ b) => a ^ ~b */
3586 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3587 dbg_info *dbg = get_irn_dbg_info(n);
3588 ir_node *block = get_nodes_block(n);
3589 ir_node *eor_right = get_binop_right(a);
3590 ir_node *eor_left = get_binop_left(a);
3591 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3592 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3596 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3597 if (is_Minus(a)) { /* ~-x -> x + -1 */
3598 dbg_info *dbg = get_irn_dbg_info(n);
3599 ir_graph *irg = get_irn_irg(n);
3600 ir_node *block = get_nodes_block(n);
3601 ir_node *add_l = get_Minus_op(a);
3602 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3603 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3604 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3605 ir_node *add_r = get_binop_right(a);
3606 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3607 /* ~(x + -1) = -x */
3608 ir_node *op = get_binop_left(a);
3609 ir_node *blk = get_nodes_block(n);
3610 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3611 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3619 * Transform a Minus.
3623 * -(a >>u (size-1)) = a >>s (size-1)
3624 * -(a >>s (size-1)) = a >>u (size-1)
3625 * -(a * const) -> a * -const
3627 static ir_node *transform_node_Minus(ir_node *n)
3629 ir_node *c, *oldn = n;
3630 ir_node *a = get_Minus_op(n);
3633 HANDLE_UNOP_PHI(tarval_neg,a,c);
3635 mode = get_irn_mode(a);
3636 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3637 /* the following rules are only to twos-complement */
3640 ir_node *op = get_Not_op(a);
3641 ir_tarval *tv = get_mode_one(mode);
3642 ir_node *blk = get_nodes_block(n);
3643 ir_graph *irg = get_irn_irg(blk);
3644 ir_node *c = new_r_Const(irg, tv);
3645 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3646 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3650 ir_node *c = get_Shr_right(a);
3653 ir_tarval *tv = get_Const_tarval(c);
3655 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3656 /* -(a >>u (size-1)) = a >>s (size-1) */
3657 ir_node *v = get_Shr_left(a);
3659 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3660 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3666 ir_node *c = get_Shrs_right(a);
3669 ir_tarval *tv = get_Const_tarval(c);
3671 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3672 /* -(a >>s (size-1)) = a >>u (size-1) */
3673 ir_node *v = get_Shrs_left(a);
3675 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3676 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3683 /* - (a-b) = b - a */
3684 ir_node *la = get_Sub_left(a);
3685 ir_node *ra = get_Sub_right(a);
3686 ir_node *blk = get_nodes_block(n);
3688 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3689 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3693 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3694 ir_node *mul_l = get_Mul_left(a);
3695 ir_node *mul_r = get_Mul_right(a);
3696 ir_tarval *tv = value_of(mul_r);
3697 if (tv != tarval_bad) {
3698 tv = tarval_neg(tv);
3699 if (tv != tarval_bad) {
3700 ir_graph *irg = get_irn_irg(n);
3701 ir_node *cnst = new_r_Const(irg, tv);
3702 dbg_info *dbg = get_irn_dbg_info(a);
3703 ir_node *block = get_nodes_block(a);
3704 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3705 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3715 * Transform a Proj(Load) with a non-null address.
3717 static ir_node *transform_node_Proj_Load(ir_node *proj)
3719 if (get_irn_mode(proj) == mode_X) {
3720 ir_node *load = get_Proj_pred(proj);
3722 /* get the Load address */
3723 const ir_node *addr = get_Load_ptr(load);
3724 const ir_node *confirm;
3726 if (value_not_null(addr, &confirm)) {
3727 if (confirm == NULL) {
3728 /* this node may float if it did not depend on a Confirm */
3729 set_irn_pinned(load, op_pin_state_floats);
3731 if (get_Proj_proj(proj) == pn_Load_X_except) {
3732 ir_graph *irg = get_irn_irg(proj);
3733 DBG_OPT_EXC_REM(proj);
3734 return new_r_Bad(irg, mode_X);
3736 ir_node *blk = get_nodes_block(load);
3737 return new_r_Jmp(blk);
3745 * Transform a Proj(Store) with a non-null address.
3747 static ir_node *transform_node_Proj_Store(ir_node *proj)
3749 if (get_irn_mode(proj) == mode_X) {
3750 ir_node *store = get_Proj_pred(proj);
3752 /* get the load/store address */
3753 const ir_node *addr = get_Store_ptr(store);
3754 const ir_node *confirm;
3756 if (value_not_null(addr, &confirm)) {
3757 if (confirm == NULL) {
3758 /* this node may float if it did not depend on a Confirm */
3759 set_irn_pinned(store, op_pin_state_floats);
3761 if (get_Proj_proj(proj) == pn_Store_X_except) {
3762 ir_graph *irg = get_irn_irg(proj);
3763 DBG_OPT_EXC_REM(proj);
3764 return new_r_Bad(irg, mode_X);
3766 ir_node *blk = get_nodes_block(store);
3767 return new_r_Jmp(blk);
3775 * Transform a Proj(Div) with a non-zero value.
3776 * Removes the exceptions and routes the memory to the NoMem node.
3778 static ir_node *transform_node_Proj_Div(ir_node *proj)
3780 ir_node *div = get_Proj_pred(proj);
3781 ir_node *b = get_Div_right(div);
3782 ir_node *res, *new_mem;
3783 const ir_node *confirm;
3786 if (value_not_zero(b, &confirm)) {
3787 /* div(x, y) && y != 0 */
3788 if (confirm == NULL) {
3789 /* we are sure we have a Const != 0 */
3790 new_mem = get_Div_mem(div);
3791 new_mem = skip_Pin(new_mem);
3792 set_Div_mem(div, new_mem);
3793 set_irn_pinned(div, op_pin_state_floats);
3796 proj_nr = get_Proj_proj(proj);
3798 case pn_Div_X_regular:
3799 return new_r_Jmp(get_nodes_block(div));
3801 case pn_Div_X_except: {
3802 ir_graph *irg = get_irn_irg(proj);
3803 /* we found an exception handler, remove it */
3804 DBG_OPT_EXC_REM(proj);
3805 return new_r_Bad(irg, mode_X);
3809 ir_graph *irg = get_irn_irg(proj);
3810 res = get_Div_mem(div);
3811 new_mem = get_irg_no_mem(irg);
3814 /* This node can only float up to the Confirm block */
3815 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3817 set_irn_pinned(div, op_pin_state_floats);
3818 /* this is a Div without exception, we can remove the memory edge */
3819 set_Div_mem(div, new_mem);
3828 * Transform a Proj(Mod) with a non-zero value.
3829 * Removes the exceptions and routes the memory to the NoMem node.
3831 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3833 ir_node *mod = get_Proj_pred(proj);
3834 ir_node *b = get_Mod_right(mod);
3835 ir_node *res, *new_mem;
3836 const ir_node *confirm;
3839 if (value_not_zero(b, &confirm)) {
3840 /* mod(x, y) && y != 0 */
3841 proj_nr = get_Proj_proj(proj);
3843 if (confirm == NULL) {
3844 /* we are sure we have a Const != 0 */
3845 new_mem = get_Mod_mem(mod);
3846 new_mem = skip_Pin(new_mem);
3847 set_Mod_mem(mod, new_mem);
3848 set_irn_pinned(mod, op_pin_state_floats);
3853 case pn_Mod_X_regular:
3854 return new_r_Jmp(get_nodes_block(mod));
3856 case pn_Mod_X_except: {
3857 ir_graph *irg = get_irn_irg(proj);
3858 /* we found an exception handler, remove it */
3859 DBG_OPT_EXC_REM(proj);
3860 return new_r_Bad(irg, mode_X);
3864 ir_graph *irg = get_irn_irg(proj);
3865 res = get_Mod_mem(mod);
3866 new_mem = get_irg_no_mem(irg);
3869 /* This node can only float up to the Confirm block */
3870 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3872 /* this is a Mod without exception, we can remove the memory edge */
3873 set_Mod_mem(mod, new_mem);
3877 if (get_Mod_left(mod) == b) {
3878 /* a % a = 0 if a != 0 */
3879 ir_graph *irg = get_irn_irg(proj);
3880 ir_mode *mode = get_irn_mode(proj);
3881 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3883 DBG_OPT_CSTEVAL(mod, res);
3892 * return true if the operation returns a value with exactly 1 bit set
3894 static bool is_single_bit(const ir_node *node)
3896 /* a first implementation, could be extended with vrp and others... */
3898 ir_node *shl_l = get_Shl_left(node);
3899 ir_mode *mode = get_irn_mode(node);
3900 int modulo = get_mode_modulo_shift(mode);
3901 /* this works if we shift a 1 and we have modulo shift */
3902 if (is_Const(shl_l) && is_Const_one(shl_l)
3903 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3906 } else if (is_Const(node)) {
3907 ir_tarval *tv = get_Const_tarval(node);
3908 return tarval_is_single_bit(tv);
3914 * checks if node just flips a bit in another node and returns that other node
3915 * if so. @p tv should be a value having just 1 bit set
3917 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
3920 return get_Not_op(node);
3922 ir_node *right = get_Eor_right(node);
3923 if (is_Const(right)) {
3924 ir_tarval *right_tv = get_Const_tarval(right);
3925 ir_mode *mode = get_irn_mode(node);
3926 if (tarval_and(right_tv, tv) != get_mode_null(mode))
3927 return get_Eor_left(node);
3934 * Normalizes and optimizes Cmp nodes.
3936 static ir_node *transform_node_Cmp(ir_node *n)
3938 ir_node *left = get_Cmp_left(n);
3939 ir_node *right = get_Cmp_right(n);
3940 ir_mode *mode = get_irn_mode(left);
3941 ir_tarval *tv = NULL;
3942 bool changed = false;
3943 bool changedc = false;
3944 ir_relation relation = get_Cmp_relation(n);
3945 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3947 /* mask out impossible relations */
3948 ir_relation new_relation = relation & possible;
3949 if (new_relation != relation) {
3950 relation = new_relation;
3954 /* Remove unnecessary conversions */
3955 if (!mode_is_float(mode)
3956 || be_get_backend_param()->mode_float_arithmetic == NULL) {
3957 if (is_Conv(left) && is_Conv(right)) {
3958 ir_node *op_left = get_Conv_op(left);
3959 ir_node *op_right = get_Conv_op(right);
3960 ir_mode *mode_left = get_irn_mode(op_left);
3961 ir_mode *mode_right = get_irn_mode(op_right);
3963 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3964 && mode_left != mode_b && mode_right != mode_b) {
3965 ir_node *block = get_nodes_block(n);
3967 if (mode_left == mode_right) {
3971 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3972 } else if (smaller_mode(mode_left, mode_right)) {
3973 left = new_r_Conv(block, op_left, mode_right);
3976 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3977 } else if (smaller_mode(mode_right, mode_left)) {
3979 right = new_r_Conv(block, op_right, mode_left);
3981 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3983 mode = get_irn_mode(left);
3986 if (is_Conv(left) && is_Const(right)) {
3987 ir_node *op_left = get_Conv_op(left);
3988 ir_mode *mode_left = get_irn_mode(op_left);
3989 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3990 ir_tarval *tv = get_Const_tarval(right);
3991 tarval_int_overflow_mode_t last_mode
3992 = tarval_get_integer_overflow_mode();
3994 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3995 new_tv = tarval_convert_to(tv, mode_left);
3996 tarval_set_integer_overflow_mode(last_mode);
3997 if (new_tv != tarval_bad) {
3998 ir_graph *irg = get_irn_irg(n);
4000 right = new_r_Const(irg, new_tv);
4001 mode = get_irn_mode(left);
4003 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4010 * Optimize -a CMP -b into b CMP a.
4011 * This works only for modes where unary Minus cannot Overflow.
4012 * Note that two-complement integers can Overflow so it will NOT work.
4014 if (!mode_overflow_on_unary_Minus(mode) &&
4015 is_Minus(left) && is_Minus(right)) {
4016 left = get_Minus_op(left);
4017 right = get_Minus_op(right);
4018 relation = get_inversed_relation(relation);
4020 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4023 /* remove operation on both sides if possible */
4024 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4026 * The following operations are NOT safe for floating point operations, for instance
4027 * 1.0 + inf == 2.0 + inf, =/=> x == y
4029 if (mode_is_int(mode)) {
4030 unsigned lop = get_irn_opcode(left);
4032 if (lop == get_irn_opcode(right)) {
4033 ir_node *ll, *lr, *rl, *rr;
4035 /* same operation on both sides, try to remove */
4039 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4040 left = get_unop_op(left);
4041 right = get_unop_op(right);
4043 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4046 ll = get_Add_left(left);
4047 lr = get_Add_right(left);
4048 rl = get_Add_left(right);
4049 rr = get_Add_right(right);
4052 /* X + a CMP X + b ==> a CMP b */
4056 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4057 } else if (ll == rr) {
4058 /* X + a CMP b + X ==> a CMP b */
4062 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4063 } else if (lr == rl) {
4064 /* a + X CMP X + b ==> a CMP b */
4068 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4069 } else if (lr == rr) {
4070 /* a + X CMP b + X ==> a CMP b */
4074 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4078 ll = get_Sub_left(left);
4079 lr = get_Sub_right(left);
4080 rl = get_Sub_left(right);
4081 rr = get_Sub_right(right);
4084 /* X - a CMP X - b ==> a CMP b */
4088 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4089 } else if (lr == rr) {
4090 /* a - X CMP b - X ==> a CMP b */
4094 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4098 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4099 /* a ROTL X CMP b ROTL X ==> a CMP b */
4100 left = get_Rotl_left(left);
4101 right = get_Rotl_left(right);
4103 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4111 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4112 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4113 ir_node *ll = get_binop_left(left);
4114 ir_node *lr = get_binop_right(left);
4116 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4122 ir_graph *irg = get_irn_irg(n);
4124 right = create_zero_const(irg, mode);
4126 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4129 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4130 ir_node *rl = get_binop_left(right);
4131 ir_node *rr = get_binop_right(right);
4133 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4139 ir_graph *irg = get_irn_irg(n);
4141 right = create_zero_const(irg, mode);
4143 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4147 if (is_And(left) && is_Const(right)) {
4148 ir_node *ll = get_binop_left(left);
4149 ir_node *lr = get_binop_right(left);
4150 if (is_Shr(ll) && is_Const(lr)) {
4151 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4152 ir_node *block = get_nodes_block(n);
4153 ir_mode *mode = get_irn_mode(left);
4155 ir_node *llr = get_Shr_right(ll);
4156 if (is_Const(llr)) {
4157 dbg_info *dbg = get_irn_dbg_info(left);
4158 ir_graph *irg = get_irn_irg(left);
4160 ir_tarval *c1 = get_Const_tarval(llr);
4161 ir_tarval *c2 = get_Const_tarval(lr);
4162 ir_tarval *c3 = get_Const_tarval(right);
4163 ir_tarval *mask = tarval_shl(c2, c1);
4164 ir_tarval *value = tarval_shl(c3, c1);
4166 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4167 right = new_r_Const(irg, value);
4172 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4174 if (is_Const(right) && is_Const_null(right) &&
4175 (is_Eor(left) || is_Or_Eor_Add(left))) {
4176 right = get_Eor_right(left);
4177 left = get_Eor_left(left);
4183 if (mode_is_int(mode) && is_And(left)) {
4184 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4185 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4186 if (relation == ir_relation_equal
4187 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4188 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4189 ir_node *and0 = get_And_left(left);
4190 ir_node *and1 = get_And_right(left);
4191 if (and1 == right) {
4192 ir_node *tmp = and0;
4196 if (and0 == right && is_single_bit(and0)) {
4197 ir_graph *irg = get_irn_irg(n);
4199 relation == ir_relation_equal ? ir_relation_less_greater
4200 : ir_relation_equal;
4201 right = create_zero_const(irg, mode);
4207 if (is_Const(right) && is_Const_null(right) &&
4208 (relation == ir_relation_equal
4209 || (relation == ir_relation_less_greater)
4210 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4212 /* instead of flipping the bit before the bit-test operation negate
4214 ir_node *and0 = get_And_left(left);
4215 ir_node *and1 = get_And_right(left);
4216 if (is_Const(and1)) {
4217 ir_tarval *tv = get_Const_tarval(and1);
4218 if (tarval_is_single_bit(tv)) {
4219 ir_node *flipped = flips_bit(and0, tv);
4220 if (flipped != NULL) {
4221 dbg_info *dbgi = get_irn_dbg_info(left);
4222 ir_node *block = get_nodes_block(left);
4223 relation = get_negated_relation(relation);
4224 left = new_rd_And(dbgi, block, flipped, and1, mode);
4233 /* replace mode_b compares with ands/ors */
4234 if (mode == mode_b) {
4235 ir_node *block = get_nodes_block(n);
4239 case ir_relation_less_equal:
4240 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4242 case ir_relation_less:
4243 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4245 case ir_relation_greater_equal:
4246 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4248 case ir_relation_greater:
4249 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4251 case ir_relation_less_greater:
4252 bres = new_r_Eor(block, left, right, mode_b);
4254 case ir_relation_equal:
4255 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4258 #ifdef DEBUG_libfirm
4259 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4264 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4270 * First step: normalize the compare op
4271 * by placing the constant on the right side
4272 * or moving the lower address node to the left.
4274 if (!operands_are_normalized(left, right)) {
4279 relation = get_inversed_relation(relation);
4284 * Second step: Try to reduce the magnitude
4285 * of a constant. This may help to generate better code
4286 * later and may help to normalize more compares.
4287 * Of course this is only possible for integer values.
4289 tv = value_of(right);
4290 if (tv != tarval_bad) {
4291 ir_mode *mode = get_irn_mode(right);
4293 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4294 * cmp(ct,c2) | cmp(cf,c2) | result
4295 * -----------|------------|--------
4296 * true | true | True
4297 * false | false | False
4299 * false | true | not(x)
4302 ir_node *mux_true = get_Mux_true(left);
4303 ir_node *mux_false = get_Mux_false(left);
4304 if (is_Const(mux_true) && is_Const(mux_false)) {
4305 /* we can fold true/false constant separately */
4306 ir_tarval *tv_true = get_Const_tarval(mux_true);
4307 ir_tarval *tv_false = get_Const_tarval(mux_false);
4308 ir_relation r_true = tarval_cmp(tv_true, tv);
4309 ir_relation r_false = tarval_cmp(tv_false, tv);
4310 if (r_true != ir_relation_false
4311 || r_false != ir_relation_false) {
4312 bool rel_true = (r_true & relation) != 0;
4313 bool rel_false = (r_false & relation) != 0;
4314 ir_node *cond = get_Mux_sel(left);
4315 if (rel_true == rel_false) {
4316 relation = rel_true ? ir_relation_true
4317 : ir_relation_false;
4318 } else if (rel_true) {
4321 dbg_info *dbgi = get_irn_dbg_info(n);
4322 ir_node *block = get_nodes_block(n);
4323 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4330 /* TODO extend to arbitrary constants */
4331 if (is_Conv(left) && tarval_is_null(tv)) {
4332 ir_node *op = get_Conv_op(left);
4333 ir_mode *op_mode = get_irn_mode(op);
4336 * UpConv(x) REL 0 ==> x REL 0
4337 * Don't do this for float values as it's unclear whether it is a
4338 * win. (on the other side it makes detection/creation of fabs hard)
4340 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4341 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4342 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4343 !mode_is_float(mode)) {
4344 tv = get_mode_null(op_mode);
4348 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4352 if (tv != tarval_bad) {
4353 /* the following optimization is possible on modes without Overflow
4354 * on Unary Minus or on == and !=:
4355 * -a CMP c ==> a swap(CMP) -c
4357 * Beware: for two-complement Overflow may occur, so only == and != can
4358 * be optimized, see this:
4359 * -MININT < 0 =/=> MININT > 0 !!!
4361 if (is_Minus(left) &&
4362 (!mode_overflow_on_unary_Minus(mode) ||
4363 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4364 tv = tarval_neg(tv);
4366 if (tv != tarval_bad) {
4367 left = get_Minus_op(left);
4368 relation = get_inversed_relation(relation);
4370 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4372 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4373 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4374 tv = tarval_not(tv);
4376 if (tv != tarval_bad) {
4377 left = get_Not_op(left);
4379 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4383 /* for integer modes, we have more */
4384 if (mode_is_int(mode) && !is_Const(left)) {
4385 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4386 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4387 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4388 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4390 if (tv != tarval_bad) {
4391 relation ^= ir_relation_equal;
4393 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4396 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4397 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4398 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4399 tv = tarval_add(tv, get_mode_one(mode));
4401 if (tv != tarval_bad) {
4402 relation ^= ir_relation_equal;
4404 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4408 /* the following reassociations work only for == and != */
4409 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4410 if (tv != tarval_bad) {
4411 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4413 ir_node *c1 = get_Sub_right(left);
4414 ir_tarval *tv2 = value_of(c1);
4416 if (tv2 != tarval_bad) {
4417 tv2 = tarval_add(tv, value_of(c1));
4419 if (tv2 != tarval_bad) {
4420 left = get_Sub_left(left);
4423 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4427 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4428 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4429 ir_node *a_l = get_binop_left(left);
4430 ir_node *a_r = get_binop_right(left);
4434 if (is_Const(a_l)) {
4436 tv2 = value_of(a_l);
4439 tv2 = value_of(a_r);
4442 if (tv2 != tarval_bad) {
4443 tv2 = tarval_sub(tv, tv2, NULL);
4445 if (tv2 != tarval_bad) {
4449 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4453 /* -a == c ==> a == -c, -a != c ==> a != -c */
4454 else if (is_Minus(left)) {
4455 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4457 if (tv2 != tarval_bad) {
4458 left = get_Minus_op(left);
4461 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4468 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4469 switch (get_irn_opcode(left)) {
4473 c1 = get_And_right(left);
4476 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4477 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4479 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4481 /* TODO: move to constant evaluation */
4482 ir_graph *irg = get_irn_irg(n);
4483 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4484 c1 = new_r_Const(irg, tv);
4485 DBG_OPT_CSTEVAL(n, c1);
4489 if (tarval_is_single_bit(tv)) {
4491 * optimization for AND:
4493 * And(x, C) == C ==> And(x, C) != 0
4494 * And(x, C) != C ==> And(X, C) == 0
4496 * if C is a single Bit constant.
4499 /* check for Constant's match. We have check hare the tarvals,
4500 because our const might be changed */
4501 if (get_Const_tarval(c1) == tv) {
4502 /* fine: do the transformation */
4503 tv = get_mode_null(get_tarval_mode(tv));
4504 relation ^= ir_relation_less_equal_greater;
4506 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4512 c1 = get_Or_right(left);
4513 if (is_Const(c1) && tarval_is_null(tv)) {
4515 * Or(x, C) == 0 && C != 0 ==> FALSE
4516 * Or(x, C) != 0 && C != 0 ==> TRUE
4518 if (! tarval_is_null(get_Const_tarval(c1))) {
4519 /* TODO: move to constant evaluation */
4520 ir_graph *irg = get_irn_irg(n);
4521 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4522 c1 = new_r_Const(irg, tv);
4523 DBG_OPT_CSTEVAL(n, c1);
4530 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4532 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4535 c1 = get_Shl_right(left);
4537 ir_graph *irg = get_irn_irg(c1);
4538 ir_tarval *tv1 = get_Const_tarval(c1);
4539 ir_mode *mode = get_irn_mode(left);
4540 ir_tarval *minus1 = get_mode_all_one(mode);
4541 ir_tarval *amask = tarval_shr(minus1, tv1);
4542 ir_tarval *cmask = tarval_shl(minus1, tv1);
4545 if (tarval_and(tv, cmask) != tv) {
4546 /* condition not met */
4547 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4548 c1 = new_r_Const(irg, tv);
4549 DBG_OPT_CSTEVAL(n, c1);
4552 sl = get_Shl_left(left);
4553 blk = get_nodes_block(n);
4554 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4555 tv = tarval_shr(tv, tv1);
4557 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4562 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4564 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4567 c1 = get_Shr_right(left);
4569 ir_graph *irg = get_irn_irg(c1);
4570 ir_tarval *tv1 = get_Const_tarval(c1);
4571 ir_mode *mode = get_irn_mode(left);
4572 ir_tarval *minus1 = get_mode_all_one(mode);
4573 ir_tarval *amask = tarval_shl(minus1, tv1);
4574 ir_tarval *cmask = tarval_shr(minus1, tv1);
4577 if (tarval_and(tv, cmask) != tv) {
4578 /* condition not met */
4579 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4580 c1 = new_r_Const(irg, tv);
4581 DBG_OPT_CSTEVAL(n, c1);
4584 sl = get_Shr_left(left);
4585 blk = get_nodes_block(n);
4586 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4587 tv = tarval_shl(tv, tv1);
4589 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4594 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4596 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4599 c1 = get_Shrs_right(left);
4601 ir_graph *irg = get_irn_irg(c1);
4602 ir_tarval *tv1 = get_Const_tarval(c1);
4603 ir_mode *mode = get_irn_mode(left);
4604 ir_tarval *minus1 = get_mode_all_one(mode);
4605 ir_tarval *amask = tarval_shl(minus1, tv1);
4606 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4609 cond = tarval_sub(cond, tv1, NULL);
4610 cond = tarval_shrs(tv, cond);
4612 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4613 /* condition not met */
4614 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4615 c1 = new_r_Const(irg, tv);
4616 DBG_OPT_CSTEVAL(n, c1);
4619 sl = get_Shrs_left(left);
4620 blk = get_nodes_block(n);
4621 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4622 tv = tarval_shl(tv, tv1);
4624 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4632 if (changedc) { /* need a new Const */
4633 ir_graph *irg = get_irn_irg(n);
4634 right = new_r_Const(irg, tv);
4638 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4639 ir_node *op = get_Proj_pred(left);
4641 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4642 ir_node *c = get_binop_right(op);
4645 ir_tarval *tv = get_Const_tarval(c);
4647 if (tarval_is_single_bit(tv)) {
4648 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4649 ir_node *v = get_binop_left(op);
4650 ir_node *blk = get_nodes_block(op);
4651 ir_graph *irg = get_irn_irg(op);
4652 ir_mode *mode = get_irn_mode(v);
4654 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4655 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4657 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4664 dbg_info *dbgi = get_irn_dbg_info(n);
4665 ir_node *block = get_nodes_block(n);
4667 /* create a new compare */
4668 n = new_rd_Cmp(dbgi, block, left, right, relation);
4675 * Optimize CopyB(mem, x, x) into a Nop.
4677 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4679 ir_node *copyb = get_Proj_pred(proj);
4680 ir_node *a = get_CopyB_dst(copyb);
4681 ir_node *b = get_CopyB_src(copyb);
4684 switch (get_Proj_proj(proj)) {
4685 case pn_CopyB_X_regular:
4686 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4687 DBG_OPT_EXC_REM(proj);
4688 proj = new_r_Jmp(get_nodes_block(copyb));
4690 case pn_CopyB_X_except: {
4691 ir_graph *irg = get_irn_irg(proj);
4692 DBG_OPT_EXC_REM(proj);
4693 proj = new_r_Bad(irg, mode_X);
4704 * Does all optimizations on nodes that must be done on its Projs
4705 * because of creating new nodes.
4707 static ir_node *transform_node_Proj(ir_node *proj)
4709 ir_node *n = get_Proj_pred(proj);
4711 if (n->op->ops.transform_node_Proj)
4712 return n->op->ops.transform_node_Proj(proj);
4717 * Test whether a block is unreachable
4718 * Note: That this only returns true when
4719 * IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE is set.
4720 * This is important, as you easily end up producing invalid constructs in the
4721 * unreachable code when optimizing away edges into the unreachable code.
4722 * So only set this flag when you iterate localopts to the fixpoint.
4723 * When you reach the fixpoint then all unreachable code is dead
4724 * (= can't be reached by firm edges) and you won't see the invalid constructs
4727 static bool is_block_unreachable(const ir_node *block)
4729 const ir_graph *irg = get_irn_irg(block);
4730 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4732 return get_Block_dom_depth(block) < 0;
4735 static ir_node *transform_node_Block(ir_node *block)
4737 ir_graph *irg = get_irn_irg(block);
4738 int arity = get_irn_arity(block);
4739 ir_node *bad = NULL;
4742 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE))
4745 for (i = 0; i < arity; ++i) {
4746 ir_node *const pred = get_Block_cfgpred(block, i);
4747 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4750 bad = new_r_Bad(irg, mode_X);
4751 set_irn_n(block, i, bad);
4757 static ir_node *transform_node_Phi(ir_node *phi)
4759 int n = get_irn_arity(phi);
4760 ir_mode *mode = get_irn_mode(phi);
4761 ir_node *block = get_nodes_block(phi);
4762 ir_graph *irg = get_irn_irg(phi);
4763 ir_node *bad = NULL;
4766 /* Set phi-operands for bad-block inputs to bad */
4767 for (i = 0; i < n; ++i) {
4768 if (!is_Bad(get_Phi_pred(phi, i))) {
4769 ir_node *pred = get_Block_cfgpred(block, i);
4770 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4772 bad = new_r_Bad(irg, mode);
4773 set_irn_n(phi, i, bad);
4778 /* Move Pin nodes down through Phi nodes. */
4779 if (mode == mode_M) {
4780 n = get_irn_arity(phi);
4782 /* Beware of Phi0 */
4786 bool has_pin = false;
4788 NEW_ARR_A(ir_node *, in, n);
4790 for (i = 0; i < n; ++i) {
4791 ir_node *pred = get_irn_n(phi, i);
4794 in[i] = get_Pin_op(pred);
4796 } else if (is_Bad(pred)) {
4806 /* Move the Pin nodes "behind" the Phi. */
4807 new_phi = new_r_Phi(block, n, in, mode_M);
4808 return new_r_Pin(block, new_phi);
4811 /* Move Confirms down through Phi nodes. */
4812 else if (mode_is_reference(mode)) {
4813 n = get_irn_arity(phi);
4815 /* Beware of Phi0 */
4817 ir_node *pred = get_irn_n(phi, 0);
4818 ir_node *bound, *new_phi, **in;
4819 ir_relation relation;
4820 bool has_confirm = false;
4822 if (! is_Confirm(pred))
4825 bound = get_Confirm_bound(pred);
4826 relation = get_Confirm_relation(pred);
4828 NEW_ARR_A(ir_node *, in, n);
4829 in[0] = get_Confirm_value(pred);
4831 for (i = 1; i < n; ++i) {
4832 pred = get_irn_n(phi, i);
4834 if (is_Confirm(pred) &&
4835 get_Confirm_bound(pred) == bound &&
4836 get_Confirm_relation(pred) == relation) {
4837 in[i] = get_Confirm_value(pred);
4839 } else if (is_Bad(pred)) {
4849 /* move the Confirm nodes "behind" the Phi */
4850 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4851 return new_r_Confirm(block, new_phi, bound, relation);
4858 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4860 * Should be moved to reassociation?
4862 static ir_node *transform_node_shift(ir_node *n)
4864 ir_node *left, *right;
4866 ir_mode *count_mode;
4867 ir_tarval *tv1, *tv2, *res;
4868 ir_node *in[2], *irn, *block;
4872 left = get_binop_left(n);
4874 /* different operations */
4875 if (get_irn_op(left) != get_irn_op(n))
4878 right = get_binop_right(n);
4879 tv1 = value_of(right);
4880 if (tv1 == tarval_bad)
4883 tv2 = value_of(get_binop_right(left));
4884 if (tv2 == tarval_bad)
4887 count_mode = get_tarval_mode(tv1);
4888 if (get_tarval_mode(tv2) != count_mode) {
4889 /* TODO: search bigger mode or something and convert... */
4893 mode = get_irn_mode(n);
4894 modulo_shf = get_mode_modulo_shift(mode);
4896 if (modulo_shf > 0) {
4897 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4899 /* I'm not so sure what happens in one complement... */
4900 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4901 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4902 * above will be invalid) */
4903 assert(modulo_shf<=0 || is_po2(modulo_shf));
4905 tv1 = tarval_and(tv1, modulo_mask);
4906 tv2 = tarval_and(tv2, modulo_mask);
4908 res = tarval_add(tv1, tv2);
4909 irg = get_irn_irg(n);
4911 /* beware: a simple replacement works only, if res < modulo shift */
4913 int bits = get_mode_size_bits(mode);
4914 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4915 res = tarval_mod(res, modulo);
4917 long bits = get_mode_size_bits(mode);
4918 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4920 /* shifting too much */
4921 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4923 ir_node *block = get_nodes_block(n);
4924 dbg_info *dbgi = get_irn_dbg_info(n);
4925 ir_mode *smode = get_irn_mode(right);
4926 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4927 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4930 return new_r_Const(irg, get_mode_null(mode));
4934 /* ok, we can replace it */
4935 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4936 block = get_nodes_block(n);
4938 in[0] = get_binop_left(left);
4939 in[1] = new_r_Const(irg, res);
4941 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4943 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4950 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4952 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4953 * (also with x >>s c1 when c1>=c2)
4955 static ir_node *transform_node_shl_shr(ir_node *n)
4958 ir_node *right = get_binop_right(n);
4968 ir_tarval *tv_shift;
4971 ir_relation relation;
4974 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4976 if (!is_Const(right))
4979 left = get_binop_left(n);
4980 mode = get_irn_mode(n);
4981 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4982 ir_node *shr_right = get_binop_right(left);
4984 if (!is_Const(shr_right))
4987 x = get_binop_left(left);
4988 tv_shr = get_Const_tarval(shr_right);
4989 tv_shl = get_Const_tarval(right);
4991 if (is_Shrs(left)) {
4992 /* shrs variant only allowed if c1 >= c2 */
4993 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
4996 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4999 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5001 tv_mask = tarval_shl(tv_mask, tv_shl);
5002 } else if (is_Shr(n) && is_Shl(left)) {
5003 ir_node *shl_right = get_Shl_right(left);
5005 if (!is_Const(shl_right))
5008 x = get_Shl_left(left);
5009 tv_shr = get_Const_tarval(right);
5010 tv_shl = get_Const_tarval(shl_right);
5012 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5013 tv_mask = tarval_shr(tv_mask, tv_shr);
5018 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5019 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5022 assert(tv_mask != tarval_bad);
5023 assert(get_tarval_mode(tv_mask) == mode);
5025 block = get_nodes_block(n);
5026 irg = get_irn_irg(block);
5027 dbgi = get_irn_dbg_info(n);
5029 relation = tarval_cmp(tv_shl, tv_shr);
5030 if (relation == ir_relation_less || relation == ir_relation_equal) {
5031 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5032 new_const = new_r_Const(irg, tv_shift);
5034 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5036 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5039 assert(relation == ir_relation_greater);
5040 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5041 new_const = new_r_Const(irg, tv_shift);
5042 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5045 new_const = new_r_Const(irg, tv_mask);
5046 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5051 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5053 ir_mode *mode = get_tarval_mode(tv);
5054 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5055 return tarval_mod(tv, modulo_tv);
5058 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5059 ir_node *left, ir_node *right, ir_mode *mode);
5062 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5063 * then we can use that to minimize the value of Add(x, const) or
5064 * Sub(Const, x). In particular this often avoids 1 instruction in some
5065 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5066 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5068 static ir_node *transform_node_shift_modulo(ir_node *n,
5069 new_shift_func new_shift)
5071 ir_mode *mode = get_irn_mode(n);
5072 int modulo = get_mode_modulo_shift(mode);
5073 ir_node *newop = NULL;
5074 ir_mode *mode_right;
5081 if (get_mode_arithmetic(mode) != irma_twos_complement)
5083 if (!is_po2(modulo))
5086 irg = get_irn_irg(n);
5087 block = get_nodes_block(n);
5088 right = get_binop_right(n);
5089 mode_right = get_irn_mode(right);
5090 if (is_Const(right)) {
5091 ir_tarval *tv = get_Const_tarval(right);
5092 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5097 newop = new_r_Const(irg, tv_mod);
5098 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5099 ir_node *add_right = get_binop_right(right);
5100 if (is_Const(add_right)) {
5101 ir_tarval *tv = get_Const_tarval(add_right);
5102 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5107 newconst = new_r_Const(irg, tv_mod);
5108 newop = new_r_Add(block, get_binop_left(right), newconst,
5111 } else if (is_Sub(right)) {
5112 ir_node *sub_left = get_Sub_left(right);
5113 if (is_Const(sub_left)) {
5114 ir_tarval *tv = get_Const_tarval(sub_left);
5115 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5120 newconst = new_r_Const(irg, tv_mod);
5121 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5128 if (newop != NULL) {
5129 dbg_info *dbgi = get_irn_dbg_info(n);
5130 ir_node *left = get_binop_left(n);
5131 return new_shift(dbgi, block, left, newop, mode);
5139 static ir_node *transform_node_Shr(ir_node *n)
5141 ir_node *c, *oldn = n;
5142 ir_node *left = get_Shr_left(n);
5143 ir_node *right = get_Shr_right(n);
5144 ir_mode *mode = get_irn_mode(n);
5146 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5147 n = transform_node_shift(n);
5150 n = transform_node_shift_modulo(n, new_rd_Shr);
5152 n = transform_node_shl_shr(n);
5154 n = transform_node_shift_bitop(n);
5162 static ir_node *transform_node_Shrs(ir_node *n)
5165 ir_node *a = get_Shrs_left(n);
5166 ir_node *b = get_Shrs_right(n);
5167 ir_mode *mode = get_irn_mode(n);
5171 if (is_oversize_shift(n)) {
5172 ir_node *block = get_nodes_block(n);
5173 dbg_info *dbgi = get_irn_dbg_info(n);
5174 ir_mode *cmode = get_irn_mode(b);
5175 long val = get_mode_size_bits(cmode)-1;
5176 ir_graph *irg = get_irn_irg(n);
5177 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5178 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5181 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5182 n = transform_node_shift(n);
5186 n = transform_node_shift_modulo(n, new_rd_Shrs);
5189 n = transform_node_shift_bitop(n);
5193 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5194 attr = vrp_get_info(a);
5196 unsigned bits = get_mode_size_bits(mode);
5197 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5198 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5199 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5200 dbg_info *dbgi = get_irn_dbg_info(n);
5201 ir_node *block = get_nodes_block(n);
5202 return new_rd_Shr(dbgi, block, a, b, mode);
5212 static ir_node *transform_node_Shl(ir_node *n)
5214 ir_node *c, *oldn = n;
5215 ir_node *a = get_Shl_left(n);
5216 ir_node *b = get_Shl_right(n);
5217 ir_mode *mode = get_irn_mode(n);
5219 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5220 n = transform_node_shift(n);
5223 n = transform_node_shift_modulo(n, new_rd_Shl);
5225 n = transform_node_shl_shr(n);
5227 n = transform_node_shift_bitop(n);
5235 static ir_node *transform_node_Rotl(ir_node *n)
5237 ir_node *c, *oldn = n;
5238 ir_node *a = get_Rotl_left(n);
5239 ir_node *b = get_Rotl_right(n);
5240 ir_mode *mode = get_irn_mode(n);
5242 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5243 n = transform_node_shift(n);
5246 n = transform_node_shift_bitop(n);
5252 * returns mode size for may_leave_out_middle_mode
5254 static unsigned get_significand_size(ir_mode *mode)
5256 const ir_mode_arithmetic arithmetic = get_mode_arithmetic(mode);
5257 switch (arithmetic) {
5259 case irma_x86_extended_float:
5260 return get_mode_mantissa_size(mode) + 1;
5261 case irma_twos_complement:
5262 return get_mode_size_bits(mode);
5264 panic("Conv node with irma_none mode?");
5266 panic("unexpected mode_arithmetic in get_significand_size");
5270 * Returns true if a conversion from mode @p m0 to @p m1 has the same effect
5271 * as converting from @p m0 to @p m1 and then to @p m2.
5272 * Classifying the 3 modes as the big(b), middle(m) and small(s) mode this
5273 * gives the following truth table:
5274 * s -> b -> m : true
5275 * s -> m -> b : !signed(s) || signed(m)
5276 * m -> b -> s : true
5277 * m -> s -> b : false
5278 * b -> s -> m : false
5279 * b -> m -> s : true
5281 * s -> b -> b : true
5282 * s -> s -> b : false
5284 * additional float constraints:
5286 * F -> I -> I: signedness of Is must match
5287 * I -> F -> I: signedness of Is must match
5288 * I -> I -> F: signedness of Is must match
5292 * at least 1 float involved: signedness must match
5294 bool may_leave_out_middle_conv(ir_mode *m0, ir_mode *m1, ir_mode *m2)
5296 int n_floats = mode_is_float(m0) + mode_is_float(m1) + mode_is_float(m2);
5297 if (n_floats == 1) {
5298 /* because overflow gives strange results we don't touch this case */
5300 } else if (n_floats == 2 && !mode_is_float(m1)) {
5304 unsigned size0 = get_significand_size(m0);
5305 unsigned size1 = get_significand_size(m1);
5306 unsigned size2 = get_significand_size(m2);
5307 if (size1 < size2 && size0 >= size1)
5311 return !mode_is_signed(m0) || mode_is_signed(m1);
5317 static ir_node *transform_node_Conv(ir_node *n)
5319 ir_node *c, *oldn = n;
5320 ir_mode *mode = get_irn_mode(n);
5321 ir_node *a = get_Conv_op(n);
5324 ir_mode *a_mode = get_irn_mode(a);
5325 ir_node *b = get_Conv_op(a);
5326 ir_mode *b_mode = get_irn_mode(b);
5327 if (may_leave_out_middle_conv(b_mode, a_mode, mode)) {
5328 dbg_info *dbgi = get_irn_dbg_info(n);
5329 ir_node *block = get_nodes_block(n);
5330 return new_rd_Conv(dbgi, block, b, mode);
5334 if (mode != mode_b && is_const_Phi(a)) {
5335 /* Do NOT optimize mode_b Conv's, this leads to remaining
5336 * Phib nodes later, because the conv_b_lower operation
5337 * is instantly reverted, when it tries to insert a Convb.
5339 c = apply_conv_on_phi(a, mode);
5341 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5346 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5347 ir_graph *irg = get_irn_irg(n);
5348 return new_r_Unknown(irg, mode);
5351 if (mode_is_reference(mode) &&
5352 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5354 ir_node *l = get_Add_left(a);
5355 ir_node *r = get_Add_right(a);
5356 dbg_info *dbgi = get_irn_dbg_info(a);
5357 ir_node *block = get_nodes_block(n);
5359 ir_node *lop = get_Conv_op(l);
5360 if (get_irn_mode(lop) == mode) {
5361 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5362 n = new_rd_Add(dbgi, block, lop, r, mode);
5367 ir_node *rop = get_Conv_op(r);
5368 if (get_irn_mode(rop) == mode) {
5369 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5370 n = new_rd_Add(dbgi, block, l, rop, mode);
5380 * Remove dead blocks and nodes in dead blocks
5381 * in keep alive list. We do not generate a new End node.
5383 static ir_node *transform_node_End(ir_node *n)
5385 int i, j, n_keepalives = get_End_n_keepalives(n);
5388 NEW_ARR_A(ir_node *, in, n_keepalives);
5390 for (i = j = 0; i < n_keepalives; ++i) {
5391 ir_node *ka = get_End_keepalive(n, i);
5393 /* no need to keep Bad */
5396 /* do not keep unreachable code */
5397 block = is_Block(ka) ? ka : get_nodes_block(ka);
5398 if (is_block_unreachable(block))
5402 if (j != n_keepalives)
5403 set_End_keepalives(n, j, in);
5407 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5409 if (is_Minus(a) && get_Minus_op(a) == b)
5411 if (is_Minus(b) && get_Minus_op(b) == a)
5413 if (is_Sub(a) && is_Sub(b)) {
5414 ir_node *a_left = get_Sub_left(a);
5415 ir_node *a_right = get_Sub_right(a);
5416 ir_node *b_left = get_Sub_left(b);
5417 ir_node *b_right = get_Sub_right(b);
5419 if (a_left == b_right && a_right == b_left)
5426 static const ir_node *skip_upconv(const ir_node *node)
5428 while (is_Conv(node)) {
5429 ir_mode *mode = get_irn_mode(node);
5430 const ir_node *op = get_Conv_op(node);
5431 ir_mode *op_mode = get_irn_mode(op);
5432 if (!smaller_mode(op_mode, mode))
5439 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5440 const ir_node *mux_true)
5445 ir_relation relation;
5451 * Note further that these optimization work even for floating point
5452 * with NaN's because -NaN == NaN.
5453 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5456 mode = get_irn_mode(mux_true);
5457 if (mode_honor_signed_zeros(mode))
5460 /* must be <, <=, >=, > */
5461 relation = get_Cmp_relation(sel);
5462 if ((relation & ir_relation_less_greater) == 0)
5465 if (!ir_is_negated_value(mux_true, mux_false))
5468 mux_true = skip_upconv(mux_true);
5469 mux_false = skip_upconv(mux_false);
5471 /* must be x cmp 0 */
5472 cmp_right = get_Cmp_right(sel);
5473 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5476 cmp_left = get_Cmp_left(sel);
5477 if (cmp_left == mux_false) {
5478 if (relation & ir_relation_less) {
5481 assert(relation & ir_relation_greater);
5484 } else if (cmp_left == mux_true) {
5485 if (relation & ir_relation_less) {
5488 assert(relation & ir_relation_greater);
5496 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5499 ir_node *cmp_left = get_Cmp_left(sel);
5500 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5503 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5504 const ir_node *mux_true)
5506 /* this code should return true each time transform_node_Mux would
5507 * optimize the Mux completely away */
5509 ir_mode *mode = get_irn_mode(mux_false);
5510 if (get_mode_arithmetic(mode) == irma_twos_complement
5511 && ir_mux_is_abs(sel, mux_false, mux_true))
5514 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5515 const ir_node *cmp_r = get_Cmp_right(sel);
5516 const ir_node *cmp_l = get_Cmp_left(sel);
5517 const ir_node *f = mux_false;
5518 const ir_node *t = mux_true;
5520 if (is_Const(t) && is_Const_null(t)) {
5525 if (is_And(cmp_l) && f == cmp_r) {
5526 ir_node *and_r = get_And_right(cmp_l);
5529 if (and_r == t && is_single_bit(and_r))
5531 and_l = get_And_left(cmp_l);
5532 if (and_l == t && is_single_bit(and_l))
5541 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5543 static ir_node *transform_Mux_set(ir_node *n)
5545 ir_node *cond = get_Mux_sel(n);
5550 ir_relation relation;
5564 left = get_Cmp_left(cond);
5565 mode = get_irn_mode(left);
5566 if (!mode_is_int(mode) && !mode_is_reference(mode))
5568 dest_mode = get_irn_mode(n);
5569 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5571 right = get_Cmp_right(cond);
5572 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5573 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5574 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5575 && relation != ir_relation_greater))
5580 case ir_relation_less:
5581 /* a < b -> (a - b) >> 31 */
5585 case ir_relation_less_equal:
5586 /* a <= b -> ~(a - b) >> 31 */
5591 case ir_relation_greater:
5592 /* a > b -> (b - a) >> 31 */
5596 case ir_relation_greater_equal:
5597 /* a >= b -> ~(a - b) >> 31 */
5606 dbgi = get_irn_dbg_info(n);
5607 block = get_nodes_block(n);
5608 irg = get_irn_irg(block);
5609 bits = get_mode_size_bits(dest_mode);
5610 tv = new_tarval_from_long(bits-1, mode_Iu);
5611 shift_cnt = new_rd_Const(dbgi, irg, tv);
5613 if (mode != dest_mode) {
5614 a = new_rd_Conv(dbgi, block, a, dest_mode);
5615 b = new_rd_Conv(dbgi, block, b, dest_mode);
5618 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5620 res = new_rd_Not(dbgi, block, res, dest_mode);
5622 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5627 * Optimize a Mux into some simpler cases.
5629 static ir_node *transform_node_Mux(ir_node *n)
5632 ir_node *sel = get_Mux_sel(n);
5633 ir_mode *mode = get_irn_mode(n);
5634 ir_node *t = get_Mux_true(n);
5635 ir_node *f = get_Mux_false(n);
5636 ir_graph *irg = get_irn_irg(n);
5638 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5639 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5640 int abs = ir_mux_is_abs(sel, f, t);
5642 dbg_info *dbgi = get_irn_dbg_info(n);
5643 ir_node *block = get_nodes_block(n);
5644 ir_node *op = ir_get_abs_op(sel, f, t);
5645 int bits = get_mode_size_bits(mode);
5646 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5647 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5648 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5651 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5653 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5659 /* first normalization step: try to move a constant to the false side,
5660 * 0 preferred on false side too */
5661 if (is_Cmp(sel) && is_Const(t) &&
5662 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5663 dbg_info *seldbgi = get_irn_dbg_info(sel);
5664 ir_node *block = get_nodes_block(sel);
5665 ir_relation relation = get_Cmp_relation(sel);
5670 /* Mux(x, a, b) => Mux(not(x), b, a) */
5671 relation = get_negated_relation(relation);
5672 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5673 get_Cmp_right(sel), relation);
5674 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5677 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
5678 n = transform_Mux_set(n);
5683 /* the following optimisations create new mode_b nodes, so only do them
5684 * before mode_b lowering */
5685 if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_MODEB_LOWERED)) {
5687 ir_node* block = get_nodes_block(n);
5689 ir_node* c1 = get_Mux_sel(t);
5690 ir_node* t1 = get_Mux_true(t);
5691 ir_node* f1 = get_Mux_false(t);
5693 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5694 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5695 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5696 return new_r_Mux(block, and_, f1, t1, mode);
5697 } else if (f == t1) {
5698 /* Mux(cond0, Mux(cond1, x, y), x) */
5699 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5700 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5701 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5702 return new_r_Mux(block, and_, t1, f1, mode);
5704 } else if (is_Mux(f)) {
5705 ir_node* block = get_nodes_block(n);
5707 ir_node* c1 = get_Mux_sel(f);
5708 ir_node* t1 = get_Mux_true(f);
5709 ir_node* f1 = get_Mux_false(f);
5711 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5712 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5713 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
5714 return new_r_Mux(block, or_, f1, t1, mode);
5715 } else if (t == f1) {
5716 /* Mux(cond0, x, Mux(cond1, y, x)) */
5717 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5718 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5719 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
5720 return new_r_Mux(block, or_, t1, f1, mode);
5724 /* note: after normalization, false can only happen on default */
5725 if (mode == mode_b) {
5726 dbg_info *dbg = get_irn_dbg_info(n);
5727 ir_node *block = get_nodes_block(n);
5730 ir_tarval *tv_t = get_Const_tarval(t);
5731 if (tv_t == tarval_b_true) {
5733 /* Muxb(sel, true, false) = sel */
5734 assert(get_Const_tarval(f) == tarval_b_false);
5735 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5738 /* Muxb(sel, true, x) = Or(sel, x) */
5739 n = new_rd_Or(dbg, block, sel, f, mode_b);
5740 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5744 } else if (is_Const(f)) {
5745 ir_tarval *tv_f = get_Const_tarval(f);
5746 if (tv_f == tarval_b_true) {
5747 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5748 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5750 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5753 /* Muxb(sel, x, false) = And(sel, x) */
5754 assert(tv_f == tarval_b_false);
5755 n = new_rd_And(dbg, block, sel, t, mode_b);
5756 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5763 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5764 ir_relation relation = get_Cmp_relation(sel);
5765 ir_node *cmp_r = get_Cmp_right(sel);
5766 ir_node *cmp_l = get_Cmp_left(sel);
5767 ir_node *block = get_nodes_block(n);
5769 if (is_And(cmp_l) && f == cmp_r) {
5770 ir_node *and_r = get_And_right(cmp_l);
5773 if (and_r == t && is_single_bit(and_r)) {
5774 if (relation == ir_relation_equal) {
5775 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5776 n = new_rd_Eor(get_irn_dbg_info(n),
5777 block, cmp_l, t, mode);
5778 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5780 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5782 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5786 and_l = get_And_left(cmp_l);
5787 if (and_l == t && is_single_bit(and_l)) {
5788 if (relation == ir_relation_equal) {
5789 /* ((1 << n) & a) == 0, (1 << n), 0) */
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 /* ((1 << n) & a) != 0, (1 << n), 0) */
5796 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5807 * optimize Sync nodes that have other syncs as input we simply add the inputs
5808 * of the other sync to our own inputs
5810 static ir_node *transform_node_Sync(ir_node *n)
5812 int arity = get_Sync_n_preds(n);
5815 for (i = 0; i < arity;) {
5816 ir_node *pred = get_Sync_pred(n, i);
5820 /* Remove Bad predecessors */
5827 /* Remove duplicate predecessors */
5828 for (j = 0; j < i; ++j) {
5829 if (get_Sync_pred(n, j) == pred) {
5838 if (!is_Sync(pred)) {
5846 pred_arity = get_Sync_n_preds(pred);
5847 for (j = 0; j < pred_arity; ++j) {
5848 ir_node *pred_pred = get_Sync_pred(pred, j);
5853 add_irn_n(n, pred_pred);
5857 if (get_Sync_pred(n, k) == pred_pred)
5864 ir_graph *irg = get_irn_irg(n);
5865 return new_r_Bad(irg, mode_M);
5868 return get_Sync_pred(n, 0);
5871 /* rehash the sync node */
5876 static ir_node *create_load_replacement_tuple(ir_node *n, ir_node *mem,
5879 ir_node *block = get_nodes_block(n);
5880 ir_graph *irg = get_irn_irg(n);
5881 ir_node *in[pn_Load_max+1];
5883 in[pn_Load_M] = mem;
5884 in[pn_Load_res] = res;
5885 if (ir_throws_exception(n)) {
5886 in[pn_Load_X_regular] = new_r_Jmp(block);
5887 in[pn_Load_X_except] = new_r_Bad(irg, mode_X);
5889 assert(pn_Load_max == 4);
5891 ir_node *tuple = new_r_Tuple(block, n_in, in);
5895 static ir_node *transform_node_Load(ir_node *n)
5897 /* don't touch volatile loads */
5898 if (get_Load_volatility(n) == volatility_is_volatile)
5901 ir_node *ptr = get_Load_ptr(n);
5902 const ir_node *confirm;
5903 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5904 set_irn_pinned(n, op_pin_state_floats);
5907 /* if our memory predecessor is a load from the same address, then reuse the
5908 * previous result */
5909 ir_node *mem = get_Load_mem(n);
5912 ir_node *mem_pred = get_Proj_pred(mem);
5913 if (is_Load(mem_pred)) {
5914 ir_node *pred_load = mem_pred;
5916 /* conservatively compare the 2 loads. TODO: This could be less strict
5917 * with fixup code in some situations (like smaller/bigger modes) */
5918 if (get_Load_ptr(pred_load) != ptr)
5920 if (get_Load_mode(pred_load) != get_Load_mode(n))
5922 /* all combinations of aligned/unaligned pred/n should be fine so we do
5923 * not compare the unaligned attribute */
5924 ir_mode *mode = get_Load_mode(n);
5925 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5926 return create_load_replacement_tuple(n, mem, res);
5927 } else if (is_Store(mem_pred)) {
5928 ir_node *pred_store = mem_pred;
5929 ir_node *value = get_Store_value(pred_store);
5931 if (get_Store_ptr(pred_store) != ptr)
5933 if (get_irn_mode(value) != get_Load_mode(n))
5935 /* all combinations of aligned/unaligned pred/n should be fine so we do
5936 * not compare the unaligned attribute */
5937 return create_load_replacement_tuple(n, mem, value);
5943 static ir_node *transform_node_Store(ir_node *n)
5945 /* don't touch volatile stores */
5946 if (get_Store_volatility(n) == volatility_is_volatile)
5949 ir_node *ptr = get_Store_ptr(n);
5950 const ir_node *confirm;
5951 if (value_not_zero(ptr, &confirm) && confirm == NULL) {
5952 set_irn_pinned(n, op_pin_state_floats);
5958 * optimize a trampoline Call into a direct Call
5960 static ir_node *transform_node_Call(ir_node *call)
5962 ir_node *callee = get_Call_ptr(call);
5963 ir_node *adr, *mem, *res, *bl, **in;
5964 ir_type *ctp, *mtp, *tp;
5968 size_t i, n_res, n_param;
5971 if (! is_Proj(callee))
5973 callee = get_Proj_pred(callee);
5974 if (! is_Builtin(callee))
5976 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5979 mem = get_Call_mem(call);
5981 if (skip_Proj(mem) == callee) {
5982 /* memory is routed to the trampoline, skip */
5983 mem = get_Builtin_mem(callee);
5986 /* build a new call type */
5987 mtp = get_Call_type(call);
5988 tdb = get_type_dbg_info(mtp);
5990 n_res = get_method_n_ress(mtp);
5991 n_param = get_method_n_params(mtp);
5992 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5994 for (i = 0; i < n_res; ++i)
5995 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5997 NEW_ARR_A(ir_node *, in, n_param + 1);
5999 /* FIXME: we don't need a new pointer type in every step */
6000 irg = get_irn_irg(call);
6001 tp = get_irg_frame_type(irg);
6002 tp = new_type_pointer(tp);
6003 set_method_param_type(ctp, 0, tp);
6005 in[0] = get_Builtin_param(callee, 2);
6006 for (i = 0; i < n_param; ++i) {
6007 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6008 in[i + 1] = get_Call_param(call, i);
6010 var = get_method_variadicity(mtp);
6011 set_method_variadicity(ctp, var);
6012 /* When we resolve a trampoline, the function must be called by a this-call */
6013 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6014 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6016 adr = get_Builtin_param(callee, 1);
6018 db = get_irn_dbg_info(call);
6019 bl = get_nodes_block(call);
6021 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6022 if (get_irn_pinned(call) == op_pin_state_floats)
6023 set_irn_pinned(res, op_pin_state_floats);
6028 * Tries several [inplace] [optimizing] transformations and returns an
6029 * equivalent node. The difference to equivalent_node() is that these
6030 * transformations _do_ generate new nodes, and thus the old node must
6031 * not be freed even if the equivalent node isn't the old one.
6033 static ir_node *transform_node(ir_node *n)
6039 iro = get_irn_opcode_(n);
6040 /* constant expression evaluation / constant folding */
6041 if (get_opt_constant_folding()) {
6042 /* neither constants nor Tuple values can be evaluated */
6043 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6044 /* try to evaluate */
6045 ir_tarval *tv = computed_value(n);
6046 if (tv != tarval_bad) {
6047 /* evaluation was successful -- replace the node. */
6048 ir_graph *irg = get_irn_irg(n);
6050 n = new_r_Const(irg, tv);
6052 DBG_OPT_CSTEVAL(old_n, n);
6058 /* remove unnecessary nodes */
6059 if (get_opt_constant_folding() ||
6060 (iro == iro_Phi) || /* always optimize these nodes. */
6061 (iro == iro_Id) || /* ... */
6062 (iro == iro_Proj) || /* ... */
6063 (iro == iro_Block)) { /* Flags tested local. */
6064 n = equivalent_node(n);
6069 /* Some more constant expression evaluation. */
6070 if (get_opt_algebraic_simplification() ||
6071 (iro == iro_Cond) ||
6072 (iro == iro_Proj)) { /* Flags tested local. */
6073 if (n->op->ops.transform_node != NULL) {
6074 n = n->op->ops.transform_node(n);
6084 static void register_computed_value_func(ir_op *op, computed_value_func func)
6086 assert(op->ops.computed_value == NULL || op->ops.computed_value == func);
6087 op->ops.computed_value = func;
6090 static void register_computed_value_func_proj(ir_op *op,
6091 computed_value_func func)
6093 assert(op->ops.computed_value_Proj == NULL
6094 || op->ops.computed_value_Proj == func);
6095 op->ops.computed_value_Proj = func;
6098 static void register_equivalent_node_func(ir_op *op, equivalent_node_func func)
6100 assert(op->ops.equivalent_node == NULL || op->ops.equivalent_node == func);
6101 op->ops.equivalent_node = func;
6104 static void register_equivalent_node_func_proj(ir_op *op,
6105 equivalent_node_func func)
6107 assert(op->ops.equivalent_node_Proj == NULL
6108 || op->ops.equivalent_node_Proj == func);
6109 op->ops.equivalent_node_Proj = func;
6112 static void register_transform_node_func(ir_op *op, transform_node_func func)
6114 assert(op->ops.transform_node == NULL || op->ops.transform_node == func);
6115 op->ops.transform_node = func;
6118 static void register_transform_node_func_proj(ir_op *op,
6119 transform_node_func func)
6121 assert(op->ops.transform_node_Proj == NULL
6122 || op->ops.transform_node_Proj == func);
6123 op->ops.transform_node_Proj = func;
6126 void ir_register_opt_node_ops(void)
6128 register_computed_value_func(op_Add, computed_value_Add);
6129 register_computed_value_func(op_And, computed_value_And);
6130 register_computed_value_func(op_Cmp, computed_value_Cmp);
6131 register_computed_value_func(op_Confirm, computed_value_Confirm);
6132 register_computed_value_func(op_Const, computed_value_Const);
6133 register_computed_value_func(op_Conv, computed_value_Conv);
6134 register_computed_value_func(op_Eor, computed_value_Eor);
6135 register_computed_value_func(op_Minus, computed_value_Minus);
6136 register_computed_value_func(op_Mul, computed_value_Mul);
6137 register_computed_value_func(op_Mux, computed_value_Mux);
6138 register_computed_value_func(op_Not, computed_value_Not);
6139 register_computed_value_func(op_Or, computed_value_Or);
6140 register_computed_value_func(op_Proj, computed_value_Proj);
6141 register_computed_value_func(op_Rotl, computed_value_Rotl);
6142 register_computed_value_func(op_Shl, computed_value_Shl);
6143 register_computed_value_func(op_Shr, computed_value_Shr);
6144 register_computed_value_func(op_Shrs, computed_value_Shrs);
6145 register_computed_value_func(op_Sub, computed_value_Sub);
6146 register_computed_value_func(op_SymConst, computed_value_SymConst);
6147 register_computed_value_func_proj(op_Div, computed_value_Proj_Div);
6148 register_computed_value_func_proj(op_Mod, computed_value_Proj_Mod);
6150 register_equivalent_node_func(op_Add, equivalent_node_Add);
6151 register_equivalent_node_func(op_And, equivalent_node_And);
6152 register_equivalent_node_func(op_Confirm, equivalent_node_Confirm);
6153 register_equivalent_node_func(op_Conv, equivalent_node_Conv);
6154 register_equivalent_node_func(op_Eor, equivalent_node_Eor);
6155 register_equivalent_node_func(op_Id, equivalent_node_Id);
6156 register_equivalent_node_func(op_Minus, equivalent_node_involution);
6157 register_equivalent_node_func(op_Mul, equivalent_node_Mul);
6158 register_equivalent_node_func(op_Mux, equivalent_node_Mux);
6159 register_equivalent_node_func(op_Not, equivalent_node_involution);
6160 register_equivalent_node_func(op_Or, equivalent_node_Or);
6161 register_equivalent_node_func(op_Phi, equivalent_node_Phi);
6162 register_equivalent_node_func(op_Proj, equivalent_node_Proj);
6163 register_equivalent_node_func(op_Rotl, equivalent_node_left_zero);
6164 register_equivalent_node_func(op_Shl, equivalent_node_left_zero);
6165 register_equivalent_node_func(op_Shr, equivalent_node_left_zero);
6166 register_equivalent_node_func(op_Shrs, equivalent_node_left_zero);
6167 register_equivalent_node_func(op_Sub, equivalent_node_Sub);
6168 register_equivalent_node_func_proj(op_CopyB, equivalent_node_Proj_CopyB);
6169 register_equivalent_node_func_proj(op_Div, equivalent_node_Proj_Div);
6170 register_equivalent_node_func_proj(op_Tuple, equivalent_node_Proj_Tuple);
6172 register_transform_node_func(op_Add, transform_node_Add);
6173 register_transform_node_func(op_And, transform_node_And);
6174 register_transform_node_func(op_Block, transform_node_Block);
6175 register_transform_node_func(op_Call, transform_node_Call);
6176 register_transform_node_func(op_Cmp, transform_node_Cmp);
6177 register_transform_node_func(op_Cond, transform_node_Cond);
6178 register_transform_node_func(op_Conv, transform_node_Conv);
6179 register_transform_node_func(op_Div, transform_node_Div);
6180 register_transform_node_func(op_End, transform_node_End);
6181 register_transform_node_func(op_Eor, transform_node_Eor);
6182 register_transform_node_func(op_Load, transform_node_Load);
6183 register_transform_node_func(op_Minus, transform_node_Minus);
6184 register_transform_node_func(op_Mod, transform_node_Mod);
6185 register_transform_node_func(op_Mul, transform_node_Mul);
6186 register_transform_node_func(op_Mux, transform_node_Mux);
6187 register_transform_node_func(op_Not, transform_node_Not);
6188 register_transform_node_func(op_Or, transform_node_Or);
6189 register_transform_node_func(op_Phi, transform_node_Phi);
6190 register_transform_node_func(op_Proj, transform_node_Proj);
6191 register_transform_node_func(op_Rotl, transform_node_Rotl);
6192 register_transform_node_func(op_Shl, transform_node_Shl);
6193 register_transform_node_func(op_Shrs, transform_node_Shrs);
6194 register_transform_node_func(op_Shr, transform_node_Shr);
6195 register_transform_node_func(op_Store, transform_node_Store);
6196 register_transform_node_func(op_Sub, transform_node_Sub);
6197 register_transform_node_func(op_Switch, transform_node_Switch);
6198 register_transform_node_func(op_Sync, transform_node_Sync);
6199 register_transform_node_func_proj(op_CopyB, transform_node_Proj_CopyB);
6200 register_transform_node_func_proj(op_Div, transform_node_Proj_Div);
6201 register_transform_node_func_proj(op_Load, transform_node_Proj_Load);
6202 register_transform_node_func_proj(op_Mod, transform_node_Proj_Mod);
6203 register_transform_node_func_proj(op_Store, transform_node_Proj_Store);
6206 /* **************** Common Subexpression Elimination **************** */
6208 /** The size of the hash table used, should estimate the number of nodes
6210 #define N_IR_NODES 512
6212 int identities_cmp(const void *elt, const void *key)
6214 ir_node *a = (ir_node *)elt;
6215 ir_node *b = (ir_node *)key;
6218 if (a == b) return 0;
6220 if ((get_irn_op(a) != get_irn_op(b)) ||
6221 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6223 /* compare if a's in and b's in are of equal length */
6224 irn_arity_a = get_irn_arity(a);
6225 if (irn_arity_a != get_irn_arity(b))
6228 /* blocks are never the same */
6232 if (get_irn_pinned(a) == op_pin_state_pinned) {
6233 /* for pinned nodes, the block inputs must be equal */
6234 if (get_nodes_block(a) != get_nodes_block(b))
6237 ir_node *block_a = get_nodes_block(a);
6238 ir_node *block_b = get_nodes_block(b);
6239 if (! get_opt_global_cse()) {
6240 /* for block-local CSE both nodes must be in the same Block */
6241 if (block_a != block_b)
6244 /* The optimistic approach would be to do nothing here.
6245 * However doing GCSE optimistically produces a lot of partially dead code which appears
6246 * to be worse in practice than the missed opportunities.
6247 * So we use a very conservative variant here and only CSE if 1 value dominates the
6249 if (!block_dominates(block_a, block_b)
6250 && !block_dominates(block_b, block_a))
6252 /* respect the workaround rule: do not move nodes which are only
6253 * held by keepalive edges */
6254 if (only_used_by_keepalive(a) || only_used_by_keepalive(b))
6259 /* compare a->in[0..ins] with b->in[0..ins] */
6260 for (i = 0; i < irn_arity_a; ++i) {
6261 ir_node *pred_a = get_irn_n(a, i);
6262 ir_node *pred_b = get_irn_n(b, i);
6263 if (pred_a != pred_b) {
6264 /* if both predecessors are CSE neutral they might be different */
6265 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6271 * here, we already now that the nodes are identical except their
6274 if (a->op->ops.node_cmp_attr)
6275 return a->op->ops.node_cmp_attr(a, b);
6280 unsigned ir_node_hash(const ir_node *node)
6282 return node->op->ops.hash(node);
6285 void new_identities(ir_graph *irg)
6287 if (irg->value_table != NULL)
6288 del_pset(irg->value_table);
6289 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6292 void del_identities(ir_graph *irg)
6294 if (irg->value_table != NULL)
6295 del_pset(irg->value_table);
6298 static int cmp_node_nr(const void *a, const void *b)
6300 ir_node **p1 = (ir_node**)a;
6301 ir_node **p2 = (ir_node**)b;
6302 long n1 = get_irn_node_nr(*p1);
6303 long n2 = get_irn_node_nr(*p2);
6304 return (n1>n2) - (n1<n2);
6307 void ir_normalize_node(ir_node *n)
6309 if (is_op_commutative(get_irn_op(n))) {
6310 ir_node *l = get_binop_left(n);
6311 ir_node *r = get_binop_right(n);
6313 /* For commutative operators perform a OP b == b OP a but keep
6314 * constants on the RIGHT side. This helps greatly in some
6315 * optimizations. Moreover we use the idx number to make the form
6317 if (!operands_are_normalized(l, r)) {
6318 set_binop_left(n, r);
6319 set_binop_right(n, l);
6322 } else if (is_Sync(n)) {
6323 /* we assume that most of the time the inputs of a Sync node are already
6324 * sorted, so check this first as a shortcut */
6325 bool ins_sorted = true;
6326 int arity = get_irn_arity(n);
6327 const ir_node *last = get_irn_n(n, 0);
6329 for (i = 1; i < arity; ++i) {
6330 const ir_node *node = get_irn_n(n, i);
6331 if (get_irn_node_nr(node) < get_irn_node_nr(last)) {
6339 ir_node **ins = get_irn_in(n)+1;
6340 ir_node **new_ins = XMALLOCN(ir_node*, arity);
6341 memcpy(new_ins, ins, arity*sizeof(ins[0]));
6342 qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
6343 set_irn_in(n, arity, new_ins);
6349 ir_node *identify_remember(ir_node *n)
6351 ir_graph *irg = get_irn_irg(n);
6352 pset *value_table = irg->value_table;
6355 if (value_table == NULL)
6358 ir_normalize_node(n);
6359 /* lookup or insert in hash table with given hash key. */
6360 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6363 /* n is reachable again */
6364 edges_node_revival(nn);
6371 * During construction we set the op_pin_state_pinned flag in the graph right
6372 * when the optimization is performed. The flag turning on procedure global
6373 * cse could be changed between two allocations. This way we are safe.
6375 * @param n The node to lookup
6377 static inline ir_node *identify_cons(ir_node *n)
6381 n = identify_remember(n);
6382 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6383 ir_graph *irg = get_irn_irg(n);
6384 set_irg_pinned(irg, op_pin_state_floats);
6389 void add_identities(ir_node *node)
6396 identify_remember(node);
6399 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6401 ir_graph *rem = current_ir_graph;
6403 current_ir_graph = irg;
6404 foreach_pset(irg->value_table, ir_node, node) {
6407 current_ir_graph = rem;
6410 ir_node *optimize_node(ir_node *n)
6413 ir_graph *irg = get_irn_irg(n);
6414 unsigned iro = get_irn_opcode(n);
6417 /* Always optimize Phi nodes: part of the construction. */
6418 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6420 /* constant expression evaluation / constant folding */
6421 if (get_opt_constant_folding()) {
6422 /* neither constants nor Tuple values can be evaluated */
6423 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6424 /* try to evaluate */
6425 tv = computed_value(n);
6426 if (tv != tarval_bad) {
6431 * we MUST copy the node here temporarily, because it's still
6432 * needed for DBG_OPT_CSTEVAL
6434 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6435 oldn = (ir_node*)alloca(node_size);
6437 memcpy(oldn, n, node_size);
6438 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6440 /* ARG, copy the in array, we need it for statistics */
6441 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6443 /* note the inplace edges module */
6444 edges_node_deleted(n);
6446 /* evaluation was successful -- replace the node. */
6447 irg_kill_node(irg, n);
6448 nw = new_r_Const(irg, tv);
6450 DBG_OPT_CSTEVAL(oldn, nw);
6456 /* remove unnecessary nodes */
6457 if (get_opt_algebraic_simplification() ||
6458 (iro == iro_Phi) || /* always optimize these nodes. */
6460 (iro == iro_Proj) ||
6461 (iro == iro_Block) ) /* Flags tested local. */
6462 n = equivalent_node(n);
6464 /* Common Subexpression Elimination.
6466 * Checks whether n is already available.
6467 * The block input is used to distinguish different subexpressions. Right
6468 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6469 * subexpressions within a block.
6472 n = identify_cons(n);
6475 edges_node_deleted(oldn);
6477 /* We found an existing, better node, so we can deallocate the old node. */
6478 irg_kill_node(irg, oldn);
6482 /* Some more constant expression evaluation that does not allow to
6484 iro = get_irn_opcode(n);
6485 if (get_opt_algebraic_simplification() ||
6486 (iro == iro_Cond) ||
6487 (iro == iro_Proj)) { /* Flags tested local. */
6488 n = transform_node(n);
6491 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6492 if (get_opt_cse()) {
6494 n = identify_remember(o);
6502 ir_node *optimize_in_place_2(ir_node *n)
6504 if (!get_opt_optimize() && !is_Phi(n)) return n;
6509 /** common subexpression elimination **/
6510 /* Checks whether n is already available. */
6511 /* The block input is used to distinguish different subexpressions.
6512 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
6513 * only finds common subexpressions within a block. */
6514 if (get_opt_cse()) {
6516 n = identify_remember(n);
6519 /* we have another existing node now, we do not optimize it here */
6524 n = transform_node(n);
6526 /* Now we can verify the node, as it has no dead inputs any more. */
6529 /* Now we have a legal, useful node. Enter it in hash table for cse.
6531 * Note: This is only necessary because some of the optimisations
6532 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
6533 * bad practice and should be fixed sometime.
6535 if (get_opt_cse()) {
6537 n = identify_remember(o);
6545 ir_node *optimize_in_place(ir_node *n)
6547 ir_graph *irg = get_irn_irg(n);
6549 if (get_opt_global_cse())
6550 set_irg_pinned(irg, op_pin_state_floats);
6552 /* FIXME: Maybe we could also test whether optimizing the node can
6553 change the control graph. */
6554 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
6555 return optimize_in_place_2(n);