2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
46 #include "opt_polymorphy.h"
51 #include "firm_types.h"
52 #include "bitfiddle.h"
55 /* Make types visible to allow most efficient access */
58 static bool is_Or_Eor_Add(const ir_node *node)
60 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
61 ir_node *left = get_binop_left(node);
62 ir_node *right = get_binop_right(node);
63 vrp_attr *vrp_left = vrp_get_info(left);
64 vrp_attr *vrp_right = vrp_get_info(right);
65 if (vrp_left != NULL && vrp_right != NULL) {
67 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
68 return tarval_is_null(vrp_val);
75 * Returns the tarval of a Const node or tarval_bad for all other nodes.
77 static ir_tarval *default_value_of(const ir_node *n)
80 return get_Const_tarval(n); /* might return tarval_bad */
85 value_of_func value_of_ptr = default_value_of;
87 /* * Set a new value_of function. */
88 void set_value_of_func(value_of_func func)
93 value_of_ptr = default_value_of;
97 * Return the value of a Constant.
99 static ir_tarval *computed_value_Const(const ir_node *n)
101 return get_Const_tarval(n);
105 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
107 static ir_tarval *computed_value_SymConst(const ir_node *n)
112 switch (get_SymConst_kind(n)) {
113 case symconst_type_size:
114 type = get_SymConst_type(n);
115 if (get_type_state(type) == layout_fixed)
116 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
118 case symconst_type_align:
119 type = get_SymConst_type(n);
120 if (get_type_state(type) == layout_fixed)
121 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
123 case symconst_ofs_ent:
124 ent = get_SymConst_entity(n);
125 type = get_entity_owner(ent);
126 if (get_type_state(type) == layout_fixed)
127 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
136 * Return the value of an Add.
138 static ir_tarval *computed_value_Add(const ir_node *n)
140 ir_node *a = get_Add_left(n);
141 ir_node *b = get_Add_right(n);
143 ir_tarval *ta = value_of(a);
144 ir_tarval *tb = value_of(b);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_add(ta, tb);
150 if ((is_Not(a) && get_Not_op(a) == b)
151 || (is_Not(b) && get_Not_op(b) == a)) {
152 return get_mode_all_one(get_irn_mode(n));
159 * Return the value of a Sub.
160 * Special case: a - a
162 static ir_tarval *computed_value_Sub(const ir_node *n)
164 ir_mode *mode = get_irn_mode(n);
165 ir_node *a = get_Sub_left(n);
166 ir_node *b = get_Sub_right(n);
171 if (! mode_is_float(mode)) {
174 return get_mode_null(mode);
180 if ((ta != tarval_bad) && (tb != tarval_bad))
181 return tarval_sub(ta, tb, mode);
187 * Return the value of a Carry.
188 * Special : a op 0, 0 op b
190 static ir_tarval *computed_value_Carry(const ir_node *n)
192 ir_node *a = get_binop_left(n);
193 ir_node *b = get_binop_right(n);
194 ir_mode *m = get_irn_mode(n);
195 ir_tarval *ta = value_of(a);
196 ir_tarval *tb = value_of(b);
198 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
202 if (tarval_is_null(ta) || tarval_is_null(tb))
203 return get_mode_null(m);
209 * Return the value of a Borrow.
212 static ir_tarval *computed_value_Borrow(const ir_node *n)
214 ir_node *a = get_binop_left(n);
215 ir_node *b = get_binop_right(n);
216 ir_mode *m = get_irn_mode(n);
217 ir_tarval *ta = value_of(a);
218 ir_tarval *tb = value_of(b);
220 if ((ta != tarval_bad) && (tb != tarval_bad)) {
221 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
222 } else if (tarval_is_null(ta)) {
223 return get_mode_null(m);
229 * Return the value of an unary Minus.
231 static ir_tarval *computed_value_Minus(const ir_node *n)
233 ir_node *a = get_Minus_op(n);
234 ir_tarval *ta = value_of(a);
236 if (ta != tarval_bad)
237 return tarval_neg(ta);
243 * Return the value of a Mul.
245 static ir_tarval *computed_value_Mul(const ir_node *n)
247 ir_node *a = get_Mul_left(n);
248 ir_node *b = get_Mul_right(n);
249 ir_tarval *ta = value_of(a);
250 ir_tarval *tb = value_of(b);
253 mode = get_irn_mode(n);
254 if (mode != get_irn_mode(a)) {
255 /* n * n = 2n bit multiplication */
256 ta = tarval_convert_to(ta, mode);
257 tb = tarval_convert_to(tb, mode);
260 if (ta != tarval_bad && tb != tarval_bad) {
261 return tarval_mul(ta, tb);
263 /* a * 0 != 0 if a == NaN or a == Inf */
264 if (!mode_is_float(mode)) {
265 /* a*0 = 0 or 0*b = 0 */
266 if (ta == get_mode_null(mode))
268 if (tb == get_mode_null(mode))
276 * Return the value of an And.
277 * Special case: a & 0, 0 & b
279 static ir_tarval *computed_value_And(const ir_node *n)
281 ir_node *a = get_And_left(n);
282 ir_node *b = get_And_right(n);
283 ir_tarval *ta = value_of(a);
284 ir_tarval *tb = value_of(b);
286 if ((ta != tarval_bad) && (tb != tarval_bad)) {
287 return tarval_and (ta, tb);
290 if (tarval_is_null(ta)) return ta;
291 if (tarval_is_null(tb)) return tb;
294 if ((is_Not(a) && get_Not_op(a) == b)
295 || (is_Not(b) && get_Not_op(b) == a)) {
296 return get_mode_null(get_irn_mode(n));
303 * Return the value of an Or.
304 * Special case: a | 1...1, 1...1 | b
306 static ir_tarval *computed_value_Or(const ir_node *n)
308 ir_node *a = get_Or_left(n);
309 ir_node *b = get_Or_right(n);
310 ir_tarval *ta = value_of(a);
311 ir_tarval *tb = value_of(b);
313 if ((ta != tarval_bad) && (tb != tarval_bad)) {
314 return tarval_or (ta, tb);
317 if (tarval_is_all_one(ta)) return ta;
318 if (tarval_is_all_one(tb)) return tb;
321 if ((is_Not(a) && get_Not_op(a) == b)
322 || (is_Not(b) && get_Not_op(b) == a)) {
323 return get_mode_all_one(get_irn_mode(n));
329 * Return the value of an Eor.
331 static ir_tarval *computed_value_Eor(const ir_node *n)
333 ir_node *a = get_Eor_left(n);
334 ir_node *b = get_Eor_right(n);
339 return get_mode_null(get_irn_mode(n));
341 if ((is_Not(a) && get_Not_op(a) == b)
342 || (is_Not(b) && get_Not_op(b) == a)) {
343 return get_mode_all_one(get_irn_mode(n));
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_eor(ta, tb);
356 * Return the value of a Not.
358 static ir_tarval *computed_value_Not(const ir_node *n)
360 ir_node *a = get_Not_op(n);
361 ir_tarval *ta = value_of(a);
363 if (ta != tarval_bad)
364 return tarval_not(ta);
370 * Tests whether a shift shifts more bits than available in the mode
372 static bool is_oversize_shift(const ir_node *n)
374 ir_node *count = get_binop_right(n);
375 ir_mode *mode = get_irn_mode(n);
376 ir_tarval *tv = value_of(count);
379 if (tv == tarval_bad)
381 if (!tarval_is_long(tv))
383 shiftval = get_tarval_long(tv);
384 modulo_shift = get_mode_modulo_shift(mode);
385 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
388 return shiftval >= (long)get_mode_size_bits(mode);
392 * Return the value of a Shl.
394 static ir_tarval *computed_value_Shl(const ir_node *n)
396 ir_node *a = get_Shl_left(n);
397 ir_node *b = get_Shl_right(n);
399 ir_tarval *ta = value_of(a);
400 ir_tarval *tb = value_of(b);
402 if ((ta != tarval_bad) && (tb != tarval_bad)) {
403 return tarval_shl(ta, tb);
406 if (is_oversize_shift(n))
407 return get_mode_null(get_irn_mode(n));
413 * Return the value of a Shr.
415 static ir_tarval *computed_value_Shr(const ir_node *n)
417 ir_node *a = get_Shr_left(n);
418 ir_node *b = get_Shr_right(n);
420 ir_tarval *ta = value_of(a);
421 ir_tarval *tb = value_of(b);
423 if ((ta != tarval_bad) && (tb != tarval_bad)) {
424 return tarval_shr(ta, tb);
426 if (is_oversize_shift(n))
427 return get_mode_null(get_irn_mode(n));
433 * Return the value of a Shrs.
435 static ir_tarval *computed_value_Shrs(const ir_node *n)
437 ir_node *a = get_Shrs_left(n);
438 ir_node *b = get_Shrs_right(n);
440 ir_tarval *ta = value_of(a);
441 ir_tarval *tb = value_of(b);
443 if ((ta != tarval_bad) && (tb != tarval_bad)) {
444 return tarval_shrs(ta, tb);
450 * Return the value of a Rotl.
452 static ir_tarval *computed_value_Rotl(const ir_node *n)
454 ir_node *a = get_Rotl_left(n);
455 ir_node *b = get_Rotl_right(n);
457 ir_tarval *ta = value_of(a);
458 ir_tarval *tb = value_of(b);
460 if ((ta != tarval_bad) && (tb != tarval_bad)) {
461 return tarval_rotl(ta, tb);
466 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
468 ir_mode *mode = get_irn_mode(node);
469 if (get_mode_arithmetic(mode) != irma_twos_complement
470 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
474 ir_node *count = get_Shl_right(node);
475 if (is_Const(count)) {
476 ir_tarval *tv = get_Const_tarval(count);
477 if (tarval_is_long(tv)) {
478 long shiftval = get_tarval_long(tv);
479 long destbits = get_mode_size_bits(dest_mode);
480 if (shiftval >= destbits
481 && shiftval < (long)get_mode_modulo_shift(mode))
487 ir_node *right = get_And_right(node);
488 if (is_Const(right)) {
489 ir_tarval *tv = get_Const_tarval(right);
490 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
491 return tarval_is_null(conved);
498 * Return the value of a Conv.
500 static ir_tarval *computed_value_Conv(const ir_node *n)
502 ir_node *a = get_Conv_op(n);
503 ir_tarval *ta = value_of(a);
504 ir_mode *mode = get_irn_mode(n);
506 if (ta != tarval_bad)
507 return tarval_convert_to(ta, get_irn_mode(n));
509 if (ir_zero_when_converted(a, mode))
510 return get_mode_null(mode);
516 * Calculate the value of a Mux: can be evaluated, if the
517 * sel and the right input are known.
519 static ir_tarval *computed_value_Mux(const ir_node *n)
521 ir_node *sel = get_Mux_sel(n);
522 ir_tarval *ts = value_of(sel);
524 if (ts == get_tarval_b_true()) {
525 ir_node *v = get_Mux_true(n);
528 else if (ts == get_tarval_b_false()) {
529 ir_node *v = get_Mux_false(n);
536 * Calculate the value of a Confirm: can be evaluated,
537 * if it has the form Confirm(x, '=', Const).
539 static ir_tarval *computed_value_Confirm(const ir_node *n)
541 if (get_Confirm_relation(n) == ir_relation_equal) {
542 ir_tarval *tv = value_of(get_Confirm_bound(n));
543 if (tv != tarval_bad)
546 return value_of(get_Confirm_value(n));
550 * gives a (conservative) estimation of possible relation when comparing
553 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
554 const ir_node *right)
556 ir_relation possible = ir_relation_true;
557 ir_tarval *tv_l = value_of(left);
558 ir_tarval *tv_r = value_of(right);
559 ir_mode *mode = get_irn_mode(left);
560 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
561 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
563 /* both values known - evaluate them */
564 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
565 possible = tarval_cmp(tv_l, tv_r);
566 /* we can return now, won't get any better */
569 /* a == a is never less or greater (but might be equal or unordered) */
571 possible &= ~ir_relation_less_greater;
572 /* unordered results only happen for float compares */
573 if (!mode_is_float(mode))
574 possible &= ~ir_relation_unordered;
575 /* values can never be less than the least representable number or
576 * greater than the greatest representable number */
578 possible &= ~ir_relation_greater;
580 possible &= ~ir_relation_less;
582 possible &= ~ir_relation_greater;
584 possible &= ~ir_relation_less;
585 /* maybe vrp can tell us more */
586 possible &= vrp_cmp(left, right);
587 /* Alloc nodes never return null (but throw an exception) */
588 if (is_Alloc(left) && tarval_is_null(tv_r))
589 possible &= ~ir_relation_equal;
594 static ir_tarval *compute_cmp(const ir_node *cmp)
596 ir_node *left = get_Cmp_left(cmp);
597 ir_node *right = get_Cmp_right(cmp);
598 ir_relation possible = ir_get_possible_cmp_relations(left, right);
599 ir_relation relation = get_Cmp_relation(cmp);
601 /* if none of the requested relations is possible, return false */
602 if ((possible & relation) == ir_relation_false)
603 return tarval_b_false;
604 /* if possible relations are a subset of the requested ones return true */
605 if ((possible & ~relation) == ir_relation_false)
606 return tarval_b_true;
608 return computed_value_Cmp_Confirm(cmp, left, right, relation);
612 * Return the value of a Cmp.
614 * The basic idea here is to determine which relations are possible and which
615 * one are definitely impossible.
617 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
619 /* we can't construct Constb after lowering mode_b nodes */
620 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
623 return compute_cmp(cmp);
627 * Calculate the value of an integer Div.
628 * Special case: 0 / b
630 static ir_tarval *do_computed_value_Div(const ir_node *div)
632 const ir_node *a = get_Div_left(div);
633 const ir_node *b = get_Div_right(div);
634 const ir_mode *mode = get_Div_resmode(div);
635 ir_tarval *ta = value_of(a);
637 const ir_node *dummy;
639 /* cannot optimize 0 / b = 0 because of NaN */
640 if (!mode_is_float(mode)) {
641 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
642 return ta; /* 0 / b == 0 if b != 0 */
645 if (ta != tarval_bad && tb != tarval_bad)
646 return tarval_div(ta, tb);
651 * Calculate the value of an integer Mod of two nodes.
652 * Special case: a % 1
654 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
656 ir_tarval *ta = value_of(a);
657 ir_tarval *tb = value_of(b);
659 /* Compute a % 1 or c1 % c2 */
660 if (tarval_is_one(tb))
661 return get_mode_null(get_irn_mode(a));
662 if (ta != tarval_bad && tb != tarval_bad)
663 return tarval_mod(ta, tb);
668 * Return the value of a Proj(Div).
670 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
672 long proj_nr = get_Proj_proj(n);
673 if (proj_nr != pn_Div_res)
676 return do_computed_value_Div(get_Proj_pred(n));
680 * Return the value of a Proj(Mod).
682 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
684 long proj_nr = get_Proj_proj(n);
686 if (proj_nr == pn_Mod_res) {
687 const ir_node *mod = get_Proj_pred(n);
688 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
694 * Return the value of a Proj.
696 static ir_tarval *computed_value_Proj(const ir_node *proj)
698 ir_node *n = get_Proj_pred(proj);
700 if (n->op->ops.computed_value_Proj != NULL)
701 return n->op->ops.computed_value_Proj(proj);
706 * If the parameter n can be computed, return its value, else tarval_bad.
707 * Performs constant folding.
709 * @param n The node this should be evaluated
711 ir_tarval *computed_value(const ir_node *n)
713 vrp_attr *vrp = vrp_get_info(n);
714 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
715 return vrp->bits_set;
717 if (n->op->ops.computed_value)
718 return n->op->ops.computed_value(n);
723 * Set the default computed_value evaluator in an ir_op_ops.
725 * @param code the opcode for the default operation
726 * @param ops the operations initialized
731 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
735 ops->computed_value = computed_value_##a; \
737 #define CASE_PROJ(a) \
739 ops->computed_value_Proj = computed_value_Proj_##a; \
777 * Optimize operations that are commutative and have neutral 0,
778 * so a op 0 = 0 op a = a.
780 static ir_node *equivalent_node_neutral_zero(ir_node *n)
784 ir_node *a = get_binop_left(n);
785 ir_node *b = get_binop_right(n);
790 /* After running compute_node there is only one constant predecessor.
791 Find this predecessors value and remember the other node: */
792 if ((tv = value_of(a)) != tarval_bad) {
794 } else if ((tv = value_of(b)) != tarval_bad) {
799 /* If this predecessors constant value is zero, the operation is
800 * unnecessary. Remove it.
802 * Beware: If n is a Add, the mode of on and n might be different
803 * which happens in this rare construction: NULL + 3.
804 * Then, a Conv would be needed which we cannot include here.
806 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
809 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
816 * Eor is commutative and has neutral 0.
818 static ir_node *equivalent_node_Eor(ir_node *n)
824 n = equivalent_node_neutral_zero(n);
825 if (n != oldn) return n;
828 b = get_Eor_right(n);
830 if (is_Eor(a) || is_Or_Eor_Add(a)) {
831 ir_node *aa = get_binop_left(a);
832 ir_node *ab = get_binop_right(a);
835 /* (a ^ b) ^ a -> b */
837 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
839 } else if (ab == b) {
840 /* (a ^ b) ^ b -> a */
842 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
846 if (is_Eor(b) || is_Or_Eor_Add(b)) {
847 ir_node *ba = get_binop_left(b);
848 ir_node *bb = get_binop_right(b);
851 /* a ^ (a ^ b) -> b */
853 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
855 } else if (bb == a) {
856 /* a ^ (b ^ a) -> b */
858 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
866 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
868 * The second one looks strange, but this construct
869 * is used heavily in the LCC sources :-).
871 * Beware: The Mode of an Add may be different than the mode of its
872 * predecessors, so we could not return a predecessors in all cases.
874 static ir_node *equivalent_node_Add(ir_node *n)
877 ir_node *left, *right;
878 ir_mode *mode = get_irn_mode(n);
880 n = equivalent_node_neutral_zero(n);
884 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
885 if (mode_is_float(mode)) {
886 ir_graph *irg = get_irn_irg(n);
887 if (get_irg_fp_model(irg) & fp_strict_algebraic)
891 left = get_Add_left(n);
892 right = get_Add_right(n);
895 if (get_Sub_right(left) == right) {
898 n = get_Sub_left(left);
899 if (mode == get_irn_mode(n)) {
900 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
906 if (get_Sub_right(right) == left) {
909 n = get_Sub_left(right);
910 if (mode == get_irn_mode(n)) {
911 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
920 * optimize operations that are not commutative but have neutral 0 on left,
923 static ir_node *equivalent_node_left_zero(ir_node *n)
927 ir_node *a = get_binop_left(n);
928 ir_node *b = get_binop_right(n);
929 ir_tarval *tb = value_of(b);
931 if (tarval_is_null(tb)) {
934 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
939 #define equivalent_node_Shl equivalent_node_left_zero
940 #define equivalent_node_Shr equivalent_node_left_zero
941 #define equivalent_node_Shrs equivalent_node_left_zero
942 #define equivalent_node_Rotl equivalent_node_left_zero
945 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
947 * The second one looks strange, but this construct
948 * is used heavily in the LCC sources :-).
950 * Beware: The Mode of a Sub may be different than the mode of its
951 * predecessors, so we could not return a predecessors in all cases.
953 static ir_node *equivalent_node_Sub(ir_node *n)
957 ir_mode *mode = get_irn_mode(n);
960 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
961 if (mode_is_float(mode)) {
962 ir_graph *irg = get_irn_irg(n);
963 if (get_irg_fp_model(irg) & fp_strict_algebraic)
967 b = get_Sub_right(n);
970 /* Beware: modes might be different */
971 if (tarval_is_null(tb)) {
972 ir_node *a = get_Sub_left(n);
973 if (mode == get_irn_mode(a)) {
976 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
984 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
987 * -(-a) == a, but might overflow two times.
988 * We handle it anyway here but the better way would be a
989 * flag. This would be needed for Pascal for instance.
991 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
994 ir_node *pred = get_unop_op(n);
996 /* optimize symmetric unop */
997 if (get_irn_op(pred) == get_irn_op(n)) {
998 n = get_unop_op(pred);
999 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1004 /** Optimize Not(Not(x)) == x. */
1005 #define equivalent_node_Not equivalent_node_idempotent_unop
1007 /** -(-x) == x ??? Is this possible or can --x raise an
1008 out of bounds exception if min =! max? */
1009 #define equivalent_node_Minus equivalent_node_idempotent_unop
1012 * Optimize a * 1 = 1 * a = a.
1014 static ir_node *equivalent_node_Mul(ir_node *n)
1017 ir_node *a = get_Mul_left(n);
1019 /* we can handle here only the n * n = n bit cases */
1020 if (get_irn_mode(n) == get_irn_mode(a)) {
1021 ir_node *b = get_Mul_right(n);
1025 * Mul is commutative and has again an other neutral element.
1026 * Constants are place right, so check this case first.
1029 if (tarval_is_one(tv)) {
1031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1034 if (tarval_is_one(tv)) {
1036 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1044 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1046 static ir_node *equivalent_node_Or(ir_node *n)
1050 ir_node *a = get_Or_left(n);
1051 ir_node *b = get_Or_right(n);
1055 n = a; /* idempotence */
1056 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1059 /* constants are normalized to right, check this side first */
1061 if (tarval_is_null(tv)) {
1063 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1067 if (tarval_is_null(tv)) {
1069 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1077 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1079 static ir_node *equivalent_node_And(ir_node *n)
1083 ir_node *a = get_And_left(n);
1084 ir_node *b = get_And_right(n);
1088 n = a; /* idempotence */
1089 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1092 /* constants are normalized to right, check this side first */
1094 if (tarval_is_all_one(tv)) {
1096 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1099 if (tv != get_tarval_bad()) {
1100 ir_mode *mode = get_irn_mode(n);
1101 if (!mode_is_signed(mode) && is_Conv(a)) {
1102 ir_node *convop = get_Conv_op(a);
1103 ir_mode *convopmode = get_irn_mode(convop);
1104 if (!mode_is_signed(convopmode)) {
1105 /* Check Conv(all_one) & Const = all_one */
1106 ir_tarval *one = get_mode_all_one(convopmode);
1107 ir_tarval *conv = tarval_convert_to(one, mode);
1108 ir_tarval *and = tarval_and(conv, tv);
1110 if (tarval_is_all_one(and)) {
1111 /* Conv(X) & Const = X */
1113 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1120 if (tarval_is_all_one(tv)) {
1122 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1126 if ((is_Or(a) || is_Or_Eor_Add(a))
1127 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1129 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1133 if ((is_Or(b) || is_Or_Eor_Add(b))
1134 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1136 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143 * Try to remove useless Conv's:
1145 static ir_node *equivalent_node_Conv(ir_node *n)
1148 ir_node *a = get_Conv_op(n);
1150 ir_mode *n_mode = get_irn_mode(n);
1151 ir_mode *a_mode = get_irn_mode(a);
1154 if (n_mode == a_mode) { /* No Conv necessary */
1155 if (get_Conv_strict(n)) {
1158 /* neither Minus nor Confirm change the precision,
1159 so we can "look-through" */
1162 p = get_Minus_op(p);
1163 } else if (is_Confirm(p)) {
1164 p = get_Confirm_value(p);
1170 if (is_Conv(p) && get_Conv_strict(p)) {
1171 /* we known already, that a_mode == n_mode, and neither
1172 Minus change the mode, so the second Conv
1174 assert(get_irn_mode(p) == n_mode);
1176 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1180 ir_node *pred = get_Proj_pred(p);
1181 if (is_Load(pred)) {
1182 /* Loads always return with the exact precision of n_mode */
1183 assert(get_Load_mode(pred) == n_mode);
1185 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1188 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1189 pred = get_Proj_pred(pred);
1190 if (is_Start(pred)) {
1191 /* Arguments always return with the exact precision,
1192 as strictConv's are place before Call -- if the
1193 caller was compiled with the same setting.
1194 Otherwise, the semantics is probably still right. */
1195 assert(get_irn_mode(p) == n_mode);
1197 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1203 /* special case: the immediate predecessor is also a Conv */
1204 if (! get_Conv_strict(a)) {
1205 /* first one is not strict, kick it */
1207 a_mode = get_irn_mode(a);
1211 /* else both are strict conv, second is superfluous */
1213 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1218 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1221 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1222 ir_node *b = get_Conv_op(a);
1223 ir_mode *b_mode = get_irn_mode(b);
1225 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1226 /* both are strict conv */
1227 if (smaller_mode(a_mode, n_mode)) {
1228 /* both are strict, but the first is smaller, so
1229 the second cannot remove more precision, remove the
1231 set_Conv_strict(n, 0);
1234 if (n_mode == b_mode) {
1235 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1236 if (n_mode == mode_b) {
1237 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1238 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1240 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1241 if (values_in_mode(b_mode, a_mode)) {
1242 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1243 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1248 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1249 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1250 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1251 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1253 if (float_mantissa >= int_mantissa) {
1255 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1260 if (smaller_mode(b_mode, a_mode)) {
1261 if (get_Conv_strict(n))
1262 set_Conv_strict(b, 1);
1263 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1264 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1274 * - fold Phi-nodes, iff they have only one predecessor except
1277 static ir_node *equivalent_node_Phi(ir_node *n)
1282 ir_node *first_val = NULL; /* to shutup gcc */
1284 if (!get_opt_optimize() &&
1285 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1288 n_preds = get_Phi_n_preds(n);
1290 /* Phi of dead Region without predecessors. */
1294 /* Find first non-self-referencing input */
1295 for (i = 0; i < n_preds; ++i) {
1296 first_val = get_Phi_pred(n, i);
1297 /* not self pointer */
1298 if (first_val != n) {
1299 /* then found first value. */
1304 /* search for rest of inputs, determine if any of these
1305 are non-self-referencing */
1306 while (++i < n_preds) {
1307 ir_node *scnd_val = get_Phi_pred(n, i);
1308 if (scnd_val != n && scnd_val != first_val) {
1313 if (i >= n_preds && !is_Dummy(first_val)) {
1314 /* Fold, if no multiple distinct non-self-referencing inputs */
1316 DBG_OPT_PHI(oldn, n);
1322 * Optimize Proj(Tuple).
1324 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1326 ir_node *oldn = proj;
1327 ir_node *tuple = get_Proj_pred(proj);
1329 /* Remove the Tuple/Proj combination. */
1330 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1331 DBG_OPT_TUPLE(oldn, tuple, proj);
1337 * Optimize a / 1 = a.
1339 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1341 ir_node *oldn = proj;
1342 ir_node *div = get_Proj_pred(proj);
1343 ir_node *b = get_Div_right(div);
1344 ir_tarval *tb = value_of(b);
1346 /* Div is not commutative. */
1347 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1348 switch (get_Proj_proj(proj)) {
1350 proj = get_Div_mem(div);
1351 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1355 proj = get_Div_left(div);
1356 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1360 /* we cannot replace the exception Proj's here, this is done in
1361 transform_node_Proj_Div() */
1369 * Optimize CopyB(mem, x, x) into a Nop.
1371 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1373 ir_node *oldn = proj;
1374 ir_node *copyb = get_Proj_pred(proj);
1375 ir_node *a = get_CopyB_dst(copyb);
1376 ir_node *b = get_CopyB_src(copyb);
1379 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1380 switch (get_Proj_proj(proj)) {
1382 proj = get_CopyB_mem(copyb);
1383 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1391 * Optimize Bounds(idx, idx, upper) into idx.
1393 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1395 ir_node *oldn = proj;
1396 ir_node *bound = get_Proj_pred(proj);
1397 ir_node *idx = get_Bound_index(bound);
1398 ir_node *pred = skip_Proj(idx);
1401 if (idx == get_Bound_lower(bound))
1403 else if (is_Bound(pred)) {
1405 * idx was Bounds checked previously, it is still valid if
1406 * lower <= pred_lower && pred_upper <= upper.
1408 ir_node *lower = get_Bound_lower(bound);
1409 ir_node *upper = get_Bound_upper(bound);
1410 if (get_Bound_lower(pred) == lower &&
1411 get_Bound_upper(pred) == upper) {
1413 * One could expect that we simply return the previous
1414 * Bound here. However, this would be wrong, as we could
1415 * add an exception Proj to a new location then.
1416 * So, we must turn in into a tuple.
1422 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1423 switch (get_Proj_proj(proj)) {
1425 DBG_OPT_EXC_REM(proj);
1426 proj = get_Bound_mem(bound);
1430 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1433 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1441 * Does all optimizations on nodes that must be done on its Projs
1442 * because of creating new nodes.
1444 static ir_node *equivalent_node_Proj(ir_node *proj)
1446 ir_node *n = get_Proj_pred(proj);
1447 if (n->op->ops.equivalent_node_Proj)
1448 return n->op->ops.equivalent_node_Proj(proj);
1455 static ir_node *equivalent_node_Id(ir_node *n)
1463 DBG_OPT_ID(oldn, n);
1470 static ir_node *equivalent_node_Mux(ir_node *n)
1472 ir_node *oldn = n, *sel = get_Mux_sel(n);
1474 ir_tarval *ts = value_of(sel);
1476 if (ts == tarval_bad && is_Cmp(sel)) {
1477 /* try again with a direct call to compute_cmp, as we don't care
1478 * about the MODEB_LOWERED flag here */
1479 ts = compute_cmp(sel);
1482 /* Mux(true, f, t) == t */
1483 if (ts == tarval_b_true) {
1484 n = get_Mux_true(n);
1485 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1488 /* Mux(false, f, t) == f */
1489 if (ts == tarval_b_false) {
1490 n = get_Mux_false(n);
1491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1494 n_t = get_Mux_true(n);
1495 n_f = get_Mux_false(n);
1497 /* Mux(v, x, T) == x */
1498 if (is_Unknown(n_f)) {
1500 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1503 /* Mux(v, T, x) == x */
1504 if (is_Unknown(n_t)) {
1506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1510 /* Mux(v, x, x) == x */
1513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1516 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1517 ir_relation relation = get_Cmp_relation(sel);
1518 ir_node *f = get_Mux_false(n);
1519 ir_node *t = get_Mux_true(n);
1522 * Note further that these optimization work even for floating point
1523 * with NaN's because -NaN == NaN.
1524 * However, if +0 and -0 is handled differently, we cannot use the first one.
1526 ir_node *const cmp_l = get_Cmp_left(sel);
1527 ir_node *const cmp_r = get_Cmp_right(sel);
1530 case ir_relation_equal:
1531 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1532 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1534 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1539 case ir_relation_less_greater:
1540 case ir_relation_unordered_less_greater:
1541 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1542 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1544 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1553 * Note: normalization puts the constant on the right side,
1554 * so we check only one case.
1556 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1557 /* Mux(t CMP 0, X, t) */
1558 if (is_Minus(f) && get_Minus_op(f) == t) {
1559 /* Mux(t CMP 0, -t, t) */
1560 if (relation == ir_relation_equal) {
1561 /* Mux(t == 0, -t, t) ==> -t */
1563 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1564 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1565 /* Mux(t != 0, -t, t) ==> t */
1567 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1577 * Remove Confirm nodes if setting is on.
1578 * Replace Confirms(x, '=', Constlike) by Constlike.
1580 static ir_node *equivalent_node_Confirm(ir_node *n)
1582 ir_node *pred = get_Confirm_value(n);
1583 ir_relation relation = get_Confirm_relation(n);
1585 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1587 * rare case: two identical Confirms one after another,
1588 * replace the second one with the first.
1591 pred = get_Confirm_value(n);
1597 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1598 * perform no actual computation, as, e.g., the Id nodes. It does not create
1599 * new nodes. It is therefore safe to free n if the node returned is not n.
1600 * If a node returns a Tuple we can not just skip it. If the size of the
1601 * in array fits, we transform n into a tuple (e.g., Div).
1603 ir_node *equivalent_node(ir_node *n)
1605 if (n->op->ops.equivalent_node)
1606 return n->op->ops.equivalent_node(n);
1611 * Sets the default equivalent node operation for an ir_op_ops.
1613 * @param code the opcode for the default operation
1614 * @param ops the operations initialized
1619 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1623 ops->equivalent_node = equivalent_node_##a; \
1625 #define CASE_PROJ(a) \
1627 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1664 * Returns non-zero if a node is a Phi node
1665 * with all predecessors constant.
1667 static int is_const_Phi(ir_node *n)
1671 if (! is_Phi(n) || get_irn_arity(n) == 0)
1673 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1674 if (! is_Const(get_irn_n(n, i)))
1680 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1681 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1684 * in reality eval_func should be tarval (*eval_func)() but incomplete
1685 * declarations are bad style and generate noisy warnings
1687 typedef void (*eval_func)(void);
1690 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1692 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1694 if (eval == (eval_func) tarval_sub) {
1695 tarval_sub_type func = (tarval_sub_type)eval;
1697 return func(a, b, mode);
1699 tarval_binop_type func = (tarval_binop_type)eval;
1706 * Apply an evaluator on a binop with a constant operators (and one Phi).
1708 * @param phi the Phi node
1709 * @param other the other operand
1710 * @param eval an evaluator function
1711 * @param mode the mode of the result, may be different from the mode of the Phi!
1712 * @param left if non-zero, other is the left operand, else the right
1714 * @return a new Phi node if the conversion was successful, NULL else
1716 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1722 int i, n = get_irn_arity(phi);
1724 NEW_ARR_A(void *, res, n);
1726 for (i = 0; i < n; ++i) {
1727 pred = get_irn_n(phi, i);
1728 tv = get_Const_tarval(pred);
1729 tv = do_eval(eval, other, tv, mode);
1731 if (tv == tarval_bad) {
1732 /* folding failed, bad */
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 tv = get_Const_tarval(pred);
1741 tv = do_eval(eval, tv, other, mode);
1743 if (tv == tarval_bad) {
1744 /* folding failed, bad */
1750 irg = get_irn_irg(phi);
1751 for (i = 0; i < n; ++i) {
1752 pred = get_irn_n(phi, i);
1753 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1755 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1759 * Apply an evaluator on a binop with two constant Phi.
1761 * @param a the left Phi node
1762 * @param b the right Phi node
1763 * @param eval an evaluator function
1764 * @param mode the mode of the result, may be different from the mode of the Phi!
1766 * @return a new Phi node if the conversion was successful, NULL else
1768 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1770 ir_tarval *tv_l, *tv_r, *tv;
1776 if (get_nodes_block(a) != get_nodes_block(b))
1779 n = get_irn_arity(a);
1780 NEW_ARR_A(void *, res, n);
1782 for (i = 0; i < n; ++i) {
1783 pred = get_irn_n(a, i);
1784 tv_l = get_Const_tarval(pred);
1785 pred = get_irn_n(b, i);
1786 tv_r = get_Const_tarval(pred);
1787 tv = do_eval(eval, tv_l, tv_r, mode);
1789 if (tv == tarval_bad) {
1790 /* folding failed, bad */
1795 irg = get_irn_irg(a);
1796 for (i = 0; i < n; ++i) {
1797 pred = get_irn_n(a, i);
1798 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1800 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1804 * Apply an evaluator on a unop with a constant operator (a Phi).
1806 * @param phi the Phi node
1807 * @param eval an evaluator function
1809 * @return a new Phi node if the conversion was successful, NULL else
1811 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1818 int i, n = get_irn_arity(phi);
1820 NEW_ARR_A(void *, res, n);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 tv = get_Const_tarval(pred);
1826 if (tv == tarval_bad) {
1827 /* folding failed, bad */
1832 mode = get_irn_mode(phi);
1833 irg = get_irn_irg(phi);
1834 for (i = 0; i < n; ++i) {
1835 pred = get_irn_n(phi, i);
1836 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1838 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1842 * Apply a conversion on a constant operator (a Phi).
1844 * @param phi the Phi node
1846 * @return a new Phi node if the conversion was successful, NULL else
1848 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1854 int i, n = get_irn_arity(phi);
1856 NEW_ARR_A(void *, res, n);
1857 for (i = 0; i < n; ++i) {
1858 pred = get_irn_n(phi, i);
1859 tv = get_Const_tarval(pred);
1860 tv = tarval_convert_to(tv, mode);
1862 if (tv == tarval_bad) {
1863 /* folding failed, bad */
1868 irg = get_irn_irg(phi);
1869 for (i = 0; i < n; ++i) {
1870 pred = get_irn_n(phi, i);
1871 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1873 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1877 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1878 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1879 * If possible, remove the Conv's.
1881 static ir_node *transform_node_AddSub(ir_node *n)
1883 ir_mode *mode = get_irn_mode(n);
1885 if (mode_is_reference(mode)) {
1886 ir_node *left = get_binop_left(n);
1887 ir_node *right = get_binop_right(n);
1888 unsigned ref_bits = get_mode_size_bits(mode);
1890 if (is_Conv(left)) {
1891 ir_mode *lmode = get_irn_mode(left);
1892 unsigned bits = get_mode_size_bits(lmode);
1894 if (ref_bits == bits &&
1895 mode_is_int(lmode) &&
1896 get_mode_arithmetic(lmode) == irma_twos_complement) {
1897 ir_node *pre = get_Conv_op(left);
1898 ir_mode *pre_mode = get_irn_mode(pre);
1900 if (mode_is_int(pre_mode) &&
1901 get_mode_size_bits(pre_mode) == bits &&
1902 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1903 /* ok, this conv just changes to sign, moreover the calculation
1904 * is done with same number of bits as our address mode, so
1905 * we can ignore the conv as address calculation can be viewed
1906 * as either signed or unsigned
1908 set_binop_left(n, pre);
1913 if (is_Conv(right)) {
1914 ir_mode *rmode = get_irn_mode(right);
1915 unsigned bits = get_mode_size_bits(rmode);
1917 if (ref_bits == bits &&
1918 mode_is_int(rmode) &&
1919 get_mode_arithmetic(rmode) == irma_twos_complement) {
1920 ir_node *pre = get_Conv_op(right);
1921 ir_mode *pre_mode = get_irn_mode(pre);
1923 if (mode_is_int(pre_mode) &&
1924 get_mode_size_bits(pre_mode) == bits &&
1925 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1926 /* ok, this conv just changes to sign, moreover the calculation
1927 * is done with same number of bits as our address mode, so
1928 * we can ignore the conv as address calculation can be viewed
1929 * as either signed or unsigned
1931 set_binop_right(n, pre);
1936 /* let address arithmetic use unsigned modes */
1937 if (is_Const(right)) {
1938 ir_mode *rmode = get_irn_mode(right);
1940 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1941 /* convert a AddP(P, *s) into AddP(P, *u) */
1942 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1944 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1945 set_binop_right(n, pre);
1953 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1956 if (is_Const(b) && is_const_Phi(a)) { \
1957 /* check for Op(Phi, Const) */ \
1958 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1960 else if (is_Const(a) && is_const_Phi(b)) { \
1961 /* check for Op(Const, Phi) */ \
1962 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1964 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1965 /* check for Op(Phi, Phi) */ \
1966 c = apply_binop_on_2_phis(a, b, eval, mode); \
1969 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1974 #define HANDLE_UNOP_PHI(eval, a, c) \
1977 if (is_const_Phi(a)) { \
1978 /* check for Op(Phi) */ \
1979 c = apply_unop_on_phi(a, eval); \
1981 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1988 * Create a 0 constant of given mode.
1990 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
1992 ir_tarval *tv = get_mode_null(mode);
1993 ir_node *cnst = new_r_Const(irg, tv);
1998 static bool is_shiftop(const ir_node *n)
2000 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2003 /* the order of the values is important! */
2004 typedef enum const_class {
2010 static const_class classify_const(const ir_node* n)
2012 if (is_Const(n)) return const_const;
2013 if (is_irn_constlike(n)) return const_like;
2018 * Determines whether r is more constlike or has a larger index (in that order)
2021 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2023 const const_class l_order = classify_const(l);
2024 const const_class r_order = classify_const(r);
2026 l_order > r_order ||
2027 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2030 static bool is_cmp_unequal(const ir_node *node)
2032 ir_relation relation = get_Cmp_relation(node);
2033 ir_node *left = get_Cmp_left(node);
2034 ir_node *right = get_Cmp_right(node);
2035 ir_mode *mode = get_irn_mode(left);
2037 if (relation == ir_relation_less_greater)
2040 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2041 return relation == ir_relation_greater;
2046 * returns true for Cmp(x == 0) or Cmp(x != 0)
2048 static bool is_cmp_equality_zero(const ir_node *node)
2050 ir_relation relation;
2051 ir_node *right = get_Cmp_right(node);
2053 if (!is_Const(right) || !is_Const_null(right))
2055 relation = get_Cmp_relation(node);
2056 return relation == ir_relation_equal
2057 || relation == ir_relation_less_greater
2058 || (!mode_is_signed(get_irn_mode(right))
2059 && relation == ir_relation_greater);
2063 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2064 * Such pattern may arise in bitfield stores.
2066 * value c4 value c4 & c2
2067 * AND c3 AND c1 | c3
2074 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2077 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2079 ir_node *irn_and, *c1;
2081 ir_node *and_l, *c3;
2082 ir_node *value, *c4;
2083 ir_node *new_and, *new_const, *block;
2084 ir_mode *mode = get_irn_mode(irn_or);
2086 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2090 irn_and = get_binop_left(irn_or);
2091 c1 = get_binop_right(irn_or);
2092 if (!is_Const(c1) || !is_And(irn_and))
2095 or_l = get_binop_left(irn_and);
2096 c2 = get_binop_right(irn_and);
2100 tv1 = get_Const_tarval(c1);
2101 tv2 = get_Const_tarval(c2);
2103 tv = tarval_or(tv1, tv2);
2104 if (tarval_is_all_one(tv)) {
2105 /* the AND does NOT clear a bit with isn't set by the OR */
2106 set_binop_left(irn_or, or_l);
2107 set_binop_right(irn_or, c1);
2109 /* check for more */
2113 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2116 and_l = get_binop_left(or_l);
2117 c3 = get_binop_right(or_l);
2118 if (!is_Const(c3) || !is_And(and_l))
2121 value = get_binop_left(and_l);
2122 c4 = get_binop_right(and_l);
2126 /* ok, found the pattern, check for conditions */
2127 assert(mode == get_irn_mode(irn_and));
2128 assert(mode == get_irn_mode(or_l));
2129 assert(mode == get_irn_mode(and_l));
2131 tv3 = get_Const_tarval(c3);
2132 tv4 = get_Const_tarval(c4);
2134 tv = tarval_or(tv4, tv2);
2135 if (!tarval_is_all_one(tv)) {
2136 /* have at least one 0 at the same bit position */
2140 if (tv3 != tarval_andnot(tv3, tv4)) {
2141 /* bit in the or_mask is outside the and_mask */
2145 if (tv1 != tarval_andnot(tv1, tv2)) {
2146 /* bit in the or_mask is outside the and_mask */
2150 /* ok, all conditions met */
2151 block = get_irn_n(irn_or, -1);
2152 irg = get_irn_irg(block);
2154 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2156 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2158 set_binop_left(irn_or, new_and);
2159 set_binop_right(irn_or, new_const);
2161 /* check for more */
2166 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2168 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2170 ir_mode *mode = get_irn_mode(irn_or);
2171 ir_node *shl, *shr, *block;
2172 ir_node *irn, *x, *c1, *c2, *n;
2173 ir_tarval *tv1, *tv2;
2175 /* some backends can't handle rotl */
2176 if (!be_get_backend_param()->support_rotl)
2179 if (! mode_is_int(mode))
2182 shl = get_binop_left(irn_or);
2183 shr = get_binop_right(irn_or);
2192 } else if (!is_Shl(shl)) {
2194 } else if (!is_Shr(shr)) {
2197 x = get_Shl_left(shl);
2198 if (x != get_Shr_left(shr))
2201 c1 = get_Shl_right(shl);
2202 c2 = get_Shr_right(shr);
2203 if (is_Const(c1) && is_Const(c2)) {
2204 tv1 = get_Const_tarval(c1);
2205 if (! tarval_is_long(tv1))
2208 tv2 = get_Const_tarval(c2);
2209 if (! tarval_is_long(tv2))
2212 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2213 != (int) get_mode_size_bits(mode))
2216 /* yet, condition met */
2217 block = get_nodes_block(irn_or);
2219 n = new_r_Rotl(block, x, c1, mode);
2221 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2225 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2226 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2227 if (!ir_is_negated_value(c1, c2)) {
2231 /* yet, condition met */
2232 block = get_nodes_block(irn_or);
2233 n = new_r_Rotl(block, x, c1, mode);
2234 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2239 * Prototype of a recursive transform function
2240 * for bitwise distributive transformations.
2242 typedef ir_node* (*recursive_transform)(ir_node *n);
2245 * makes use of distributive laws for and, or, eor
2246 * and(a OP c, b OP c) -> and(a, b) OP c
2247 * note, might return a different op than n
2249 static ir_node *transform_bitwise_distributive(ir_node *n,
2250 recursive_transform trans_func)
2253 ir_node *a = get_binop_left(n);
2254 ir_node *b = get_binop_right(n);
2255 ir_op *op = get_irn_op(a);
2256 ir_op *op_root = get_irn_op(n);
2258 if (op != get_irn_op(b))
2261 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2262 if (op == op_Conv) {
2263 ir_node *a_op = get_Conv_op(a);
2264 ir_node *b_op = get_Conv_op(b);
2265 ir_mode *a_mode = get_irn_mode(a_op);
2266 ir_mode *b_mode = get_irn_mode(b_op);
2267 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2268 ir_node *blk = get_nodes_block(n);
2271 set_binop_left(n, a_op);
2272 set_binop_right(n, b_op);
2273 set_irn_mode(n, a_mode);
2275 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2277 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2283 /* nothing to gain here */
2287 if (op == op_Shrs || op == op_Shr || op == op_Shl
2288 || op == op_And || op == op_Or || op == op_Eor) {
2289 ir_node *a_left = get_binop_left(a);
2290 ir_node *a_right = get_binop_right(a);
2291 ir_node *b_left = get_binop_left(b);
2292 ir_node *b_right = get_binop_right(b);
2294 ir_node *op1 = NULL;
2295 ir_node *op2 = NULL;
2297 if (is_op_commutative(op)) {
2298 if (a_left == b_left) {
2302 } else if (a_left == b_right) {
2306 } else if (a_right == b_left) {
2312 if (a_right == b_right) {
2319 /* (a sop c) & (b sop c) => (a & b) sop c */
2320 ir_node *blk = get_nodes_block(n);
2322 ir_node *new_n = exact_copy(n);
2323 set_binop_left(new_n, op1);
2324 set_binop_right(new_n, op2);
2325 new_n = trans_func(new_n);
2327 if (op_root == op_Eor && op == op_Or) {
2328 dbg_info *dbgi = get_irn_dbg_info(n);
2329 ir_mode *mode = get_irn_mode(c);
2331 c = new_rd_Not(dbgi, blk, c, mode);
2332 n = new_rd_And(dbgi, blk, new_n, c, mode);
2335 set_nodes_block(n, blk);
2336 set_binop_left(n, new_n);
2337 set_binop_right(n, c);
2341 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2350 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2352 * - and, or, xor instead of &
2353 * - Shl, Shr, Shrs, rotl instead of >>
2354 * (with a special case for Or/Xor + Shrs)
2356 * This normalisation is usually good for the backend since << C can often be
2357 * matched as address-mode.
2359 static ir_node *transform_node_bitop_shift(ir_node *n)
2361 ir_graph *irg = get_irn_irg(n);
2362 ir_node *left = get_binop_left(n);
2363 ir_node *right = get_binop_right(n);
2364 ir_mode *mode = get_irn_mode(n);
2365 ir_node *shift_left;
2366 ir_node *shift_right;
2368 dbg_info *dbg_bitop;
2369 dbg_info *dbg_shift;
2375 ir_tarval *tv_bitop;
2377 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2380 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2381 if (!is_Const(right) || !is_shiftop(left))
2384 shift_left = get_binop_left(left);
2385 shift_right = get_binop_right(left);
2386 if (!is_Const(shift_right))
2389 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2390 if (is_Shrs(left)) {
2391 /* TODO this could be improved */
2395 irg = get_irn_irg(n);
2396 block = get_nodes_block(n);
2397 dbg_bitop = get_irn_dbg_info(n);
2398 dbg_shift = get_irn_dbg_info(left);
2399 tv1 = get_Const_tarval(shift_right);
2400 tv2 = get_Const_tarval(right);
2401 assert(get_tarval_mode(tv2) == mode);
2404 tv_bitop = tarval_shr(tv2, tv1);
2406 /* Check whether we have lost some bits during the right shift. */
2408 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2410 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2413 } else if (is_Shr(left)) {
2416 * TODO this can be improved by checking whether
2417 * the left shift produces an overflow
2421 tv_bitop = tarval_shl(tv2, tv1);
2423 assert(is_Rotl(left));
2424 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2426 new_const = new_r_Const(irg, tv_bitop);
2429 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2430 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2431 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2434 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2438 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2439 } else if (is_Shr(left)) {
2440 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2442 assert(is_Rotl(left));
2443 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2449 static bool complement_values(const ir_node *a, const ir_node *b)
2451 if (is_Not(a) && get_Not_op(a) == b)
2453 if (is_Not(b) && get_Not_op(b) == a)
2455 if (is_Const(a) && is_Const(b)) {
2456 ir_tarval *tv_a = get_Const_tarval(a);
2457 ir_tarval *tv_b = get_Const_tarval(b);
2458 return tarval_not(tv_a) == tv_b;
2463 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2466 * for associative operations fold:
2467 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2468 * This is a "light" version of the reassociation phase
2470 static ir_node *fold_constant_associativity(ir_node *node,
2471 tv_fold_binop_func fold)
2476 ir_node *right = get_binop_right(node);
2477 ir_node *left_right;
2484 if (!is_Const(right))
2487 op = get_irn_op(node);
2488 left = get_binop_left(node);
2489 if (get_irn_op(left) != op)
2492 left_right = get_binop_right(left);
2493 if (!is_Const(left_right))
2496 left_left = get_binop_left(left);
2497 c0 = get_Const_tarval(left_right);
2498 c1 = get_Const_tarval(right);
2499 irg = get_irn_irg(node);
2500 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2502 new_c = fold(c0, c1);
2503 if (new_c == tarval_bad)
2505 new_const = new_r_Const(irg, new_c);
2506 new_node = exact_copy(node);
2507 set_binop_left(new_node, left_left);
2508 set_binop_right(new_node, new_const);
2515 static ir_node *transform_node_Or_(ir_node *n)
2518 ir_node *a = get_binop_left(n);
2519 ir_node *b = get_binop_right(n);
2523 n = fold_constant_associativity(n, tarval_or);
2527 if (is_Not(a) && is_Not(b)) {
2528 /* ~a | ~b = ~(a&b) */
2529 ir_node *block = get_nodes_block(n);
2531 mode = get_irn_mode(n);
2534 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2535 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2536 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2540 /* we can combine the relations of two compares with the same operands */
2541 if (is_Cmp(a) && is_Cmp(b)) {
2542 ir_node *a_left = get_Cmp_left(a);
2543 ir_node *a_right = get_Cmp_right(a);
2544 ir_node *b_left = get_Cmp_left(b);
2545 ir_node *b_right = get_Cmp_right(b);
2546 if (a_left == b_left && b_left == b_right) {
2547 dbg_info *dbgi = get_irn_dbg_info(n);
2548 ir_node *block = get_nodes_block(n);
2549 ir_relation a_relation = get_Cmp_relation(a);
2550 ir_relation b_relation = get_Cmp_relation(b);
2551 ir_relation new_relation = a_relation | b_relation;
2552 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2554 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2555 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2556 && !mode_is_float(get_irn_mode(a_left))
2557 && !mode_is_float(get_irn_mode(b_left))) {
2558 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2559 ir_graph *irg = get_irn_irg(n);
2560 dbg_info *dbgi = get_irn_dbg_info(n);
2561 ir_node *block = get_nodes_block(n);
2562 ir_mode *a_mode = get_irn_mode(a_left);
2563 ir_mode *b_mode = get_irn_mode(b_left);
2564 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2565 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2566 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2567 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2568 ir_node *zero = create_zero_const(irg, b_mode);
2569 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2571 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2572 ir_graph *irg = get_irn_irg(n);
2573 dbg_info *dbgi = get_irn_dbg_info(n);
2574 ir_node *block = get_nodes_block(n);
2575 ir_mode *a_mode = get_irn_mode(a_left);
2576 ir_mode *b_mode = get_irn_mode(b_left);
2577 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2578 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2579 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2580 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2581 ir_node *zero = create_zero_const(irg, a_mode);
2582 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2587 mode = get_irn_mode(n);
2588 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2590 n = transform_node_Or_bf_store(n);
2593 n = transform_node_Or_Rotl(n);
2597 n = transform_bitwise_distributive(n, transform_node_Or_);
2600 n = transform_node_bitop_shift(n);
2607 static ir_node *transform_node_Or(ir_node *n)
2609 if (is_Or_Eor_Add(n)) {
2610 dbg_info *dbgi = get_irn_dbg_info(n);
2611 ir_node *block = get_nodes_block(n);
2612 ir_node *left = get_Or_left(n);
2613 ir_node *right = get_Or_right(n);
2614 ir_mode *mode = get_irn_mode(n);
2615 return new_rd_Add(dbgi, block, left, right, mode);
2617 return transform_node_Or_(n);
2623 static ir_node *transform_node_Eor_(ir_node *n)
2626 ir_node *a = get_binop_left(n);
2627 ir_node *b = get_binop_right(n);
2628 ir_mode *mode = get_irn_mode(n);
2631 n = fold_constant_associativity(n, tarval_eor);
2635 /* we can combine the relations of two compares with the same operands */
2636 if (is_Cmp(a) && is_Cmp(b)) {
2637 ir_node *a_left = get_Cmp_left(a);
2638 ir_node *a_right = get_Cmp_left(a);
2639 ir_node *b_left = get_Cmp_left(b);
2640 ir_node *b_right = get_Cmp_right(b);
2641 if (a_left == b_left && b_left == b_right) {
2642 dbg_info *dbgi = get_irn_dbg_info(n);
2643 ir_node *block = get_nodes_block(n);
2644 ir_relation a_relation = get_Cmp_relation(a);
2645 ir_relation b_relation = get_Cmp_relation(b);
2646 ir_relation new_relation = a_relation ^ b_relation;
2647 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2651 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2653 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2654 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2655 dbg_info *dbg = get_irn_dbg_info(n);
2656 ir_node *block = get_nodes_block(n);
2657 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2658 ir_node *new_left = get_Not_op(a);
2659 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2660 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2662 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2663 dbg_info *dbg = get_irn_dbg_info(n);
2664 ir_node *block = get_nodes_block(n);
2665 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2666 ir_node *new_right = get_Not_op(b);
2667 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2668 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2672 /* x ^ 1...1 -> ~1 */
2673 if (is_Const(b) && is_Const_all_one(b)) {
2674 n = new_r_Not(get_nodes_block(n), a, mode);
2675 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2679 n = transform_bitwise_distributive(n, transform_node_Eor_);
2682 n = transform_node_bitop_shift(n);
2689 static ir_node *transform_node_Eor(ir_node *n)
2691 if (is_Or_Eor_Add(n)) {
2692 dbg_info *dbgi = get_irn_dbg_info(n);
2693 ir_node *block = get_nodes_block(n);
2694 ir_node *left = get_Eor_left(n);
2695 ir_node *right = get_Eor_right(n);
2696 ir_mode *mode = get_irn_mode(n);
2697 return new_rd_Add(dbgi, block, left, right, mode);
2699 return transform_node_Eor_(n);
2703 * Do the AddSub optimization, then Transform
2704 * Constant folding on Phi
2705 * Add(a,a) -> Mul(a, 2)
2706 * Add(Mul(a, x), a) -> Mul(a, x+1)
2707 * if the mode is integer or float.
2708 * Transform Add(a,-b) into Sub(a,b).
2709 * Reassociation might fold this further.
2711 static ir_node *transform_node_Add(ir_node *n)
2719 n = fold_constant_associativity(n, tarval_add);
2723 n = transform_node_AddSub(n);
2727 a = get_Add_left(n);
2728 b = get_Add_right(n);
2729 mode = get_irn_mode(n);
2731 if (mode_is_reference(mode)) {
2732 ir_mode *lmode = get_irn_mode(a);
2734 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2735 /* an Add(a, NULL) is a hidden Conv */
2736 dbg_info *dbg = get_irn_dbg_info(n);
2737 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2741 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2742 ir_tarval *tv = get_Const_tarval(b);
2743 ir_tarval *min = get_mode_min(mode);
2744 /* if all bits are set, then this has the same effect as a Not.
2745 * Note that the following == gives false for different modes which
2746 * is exactly what we want */
2748 dbg_info *dbgi = get_irn_dbg_info(n);
2749 ir_graph *irg = get_irn_irg(n);
2750 ir_node *block = get_nodes_block(n);
2751 ir_node *cnst = new_r_Const(irg, min);
2752 return new_rd_Eor(dbgi, block, a, cnst, mode);
2756 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2758 /* for FP the following optimizations are only allowed if
2759 * fp_strict_algebraic is disabled */
2760 if (mode_is_float(mode)) {
2761 ir_graph *irg = get_irn_irg(n);
2762 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2766 if (mode_is_num(mode)) {
2767 ir_graph *irg = get_irn_irg(n);
2768 /* the following code leads to endless recursion when Mul are replaced
2769 * by a simple instruction chain */
2770 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2771 && a == b && mode_is_int(mode)) {
2772 ir_node *block = get_nodes_block(n);
2775 get_irn_dbg_info(n),
2778 new_r_Const_long(irg, mode, 2),
2780 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2785 get_irn_dbg_info(n),
2790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2795 get_irn_dbg_info(n),
2800 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2803 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2804 /* Here we rely on constants be on the RIGHT side */
2806 ir_node *op = get_Not_op(a);
2808 if (is_Const(b) && is_Const_one(b)) {
2810 ir_node *blk = get_nodes_block(n);
2811 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2812 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2819 if (is_Or_Eor_Add(n)) {
2820 n = transform_node_Or_(n);
2823 n = transform_node_Eor_(n);
2832 * returns -cnst or NULL if impossible
2834 static ir_node *const_negate(ir_node *cnst)
2836 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2837 dbg_info *dbgi = get_irn_dbg_info(cnst);
2838 ir_graph *irg = get_irn_irg(cnst);
2839 if (tv == tarval_bad) return NULL;
2840 return new_rd_Const(dbgi, irg, tv);
2844 * Do the AddSub optimization, then Transform
2845 * Constant folding on Phi
2846 * Sub(0,a) -> Minus(a)
2847 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2848 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2849 * Sub(Add(a, x), x) -> a
2850 * Sub(x, Add(x, a)) -> -a
2851 * Sub(x, Const) -> Add(x, -Const)
2853 static ir_node *transform_node_Sub(ir_node *n)
2859 n = transform_node_AddSub(n);
2861 a = get_Sub_left(n);
2862 b = get_Sub_right(n);
2864 mode = get_irn_mode(n);
2866 if (mode_is_int(mode)) {
2867 ir_mode *lmode = get_irn_mode(a);
2869 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2870 /* a Sub(a, NULL) is a hidden Conv */
2871 dbg_info *dbg = get_irn_dbg_info(n);
2872 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2877 if (mode == lmode &&
2878 get_mode_arithmetic(mode) == irma_twos_complement &&
2880 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2882 dbg_info *dbg = get_irn_dbg_info(n);
2883 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2884 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2890 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2892 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2893 if (mode_is_float(mode)) {
2894 ir_graph *irg = get_irn_irg(n);
2895 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2899 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2900 /* a - C -> a + (-C) */
2901 ir_node *cnst = const_negate(b);
2903 ir_node *block = get_nodes_block(n);
2904 dbg_info *dbgi = get_irn_dbg_info(n);
2906 n = new_rd_Add(dbgi, block, a, cnst, mode);
2907 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2912 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2913 dbg_info *dbg = get_irn_dbg_info(n);
2914 ir_node *block = get_nodes_block(n);
2915 ir_node *left = get_Minus_op(a);
2916 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2918 n = new_rd_Minus(dbg, block, add, mode);
2919 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2921 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2922 dbg_info *dbg = get_irn_dbg_info(n);
2923 ir_node *block = get_nodes_block(n);
2924 ir_node *right = get_Minus_op(b);
2926 n = new_rd_Add(dbg, block, a, right, mode);
2927 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2929 } else if (is_Sub(b)) {
2930 /* a - (b - c) -> a + (c - b)
2931 * -> (a - b) + c iff (b - c) is a pointer */
2932 dbg_info *s_dbg = get_irn_dbg_info(b);
2933 ir_node *s_left = get_Sub_left(b);
2934 ir_node *s_right = get_Sub_right(b);
2935 ir_mode *s_mode = get_irn_mode(b);
2936 if (mode_is_reference(s_mode)) {
2937 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2938 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2939 dbg_info *a_dbg = get_irn_dbg_info(n);
2942 s_right = new_r_Conv(lowest_block, s_right, mode);
2943 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2945 ir_node *s_block = get_nodes_block(b);
2946 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2947 dbg_info *a_dbg = get_irn_dbg_info(n);
2948 ir_node *a_block = get_nodes_block(n);
2950 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2952 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2955 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2956 ir_node *m_right = get_Mul_right(b);
2957 if (is_Const(m_right)) {
2958 ir_node *cnst2 = const_negate(m_right);
2959 if (cnst2 != NULL) {
2960 dbg_info *m_dbg = get_irn_dbg_info(b);
2961 ir_node *m_block = get_nodes_block(b);
2962 ir_node *m_left = get_Mul_left(b);
2963 ir_mode *m_mode = get_irn_mode(b);
2964 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2965 dbg_info *a_dbg = get_irn_dbg_info(n);
2966 ir_node *a_block = get_nodes_block(n);
2968 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2969 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2976 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2977 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2979 get_irn_dbg_info(n),
2983 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2986 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2987 ir_node *left = get_binop_left(a);
2988 ir_node *right = get_binop_right(a);
2990 /* FIXME: Does the Conv's work only for two complement or generally? */
2992 if (mode != get_irn_mode(right)) {
2993 /* This Sub is an effective Cast */
2994 right = new_r_Conv(get_nodes_block(n), right, mode);
2997 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2999 } else if (right == b) {
3000 if (mode != get_irn_mode(left)) {
3001 /* This Sub is an effective Cast */
3002 left = new_r_Conv(get_nodes_block(n), left, mode);
3005 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3009 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
3010 ir_node *left = get_binop_left(b);
3011 ir_node *right = get_binop_right(b);
3013 /* FIXME: Does the Conv's work only for two complement or generally? */
3015 ir_mode *r_mode = get_irn_mode(right);
3017 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3018 if (mode != r_mode) {
3019 /* This Sub is an effective Cast */
3020 n = new_r_Conv(get_nodes_block(n), n, mode);
3022 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3024 } else if (right == a) {
3025 ir_mode *l_mode = get_irn_mode(left);
3027 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3028 if (mode != l_mode) {
3029 /* This Sub is an effective Cast */
3030 n = new_r_Conv(get_nodes_block(n), n, mode);
3032 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3036 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3037 ir_mode *mode = get_irn_mode(a);
3039 if (mode == get_irn_mode(b)) {
3041 ir_node *op_a = get_Conv_op(a);
3042 ir_node *op_b = get_Conv_op(b);
3044 /* check if it's allowed to skip the conv */
3045 ma = get_irn_mode(op_a);
3046 mb = get_irn_mode(op_b);
3048 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3049 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3052 set_Sub_right(n, b);
3058 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3059 if (!is_reassoc_running() && is_Mul(a)) {
3060 ir_node *ma = get_Mul_left(a);
3061 ir_node *mb = get_Mul_right(a);
3064 ir_node *blk = get_nodes_block(n);
3065 ir_graph *irg = get_irn_irg(n);
3067 get_irn_dbg_info(n),
3071 get_irn_dbg_info(n),
3074 new_r_Const(irg, get_mode_one(mode)),
3077 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3079 } else if (mb == b) {
3080 ir_node *blk = get_nodes_block(n);
3081 ir_graph *irg = get_irn_irg(n);
3083 get_irn_dbg_info(n),
3087 get_irn_dbg_info(n),
3090 new_r_Const(irg, get_mode_one(mode)),
3093 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3097 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3098 ir_node *x = get_Sub_left(a);
3099 ir_node *y = get_Sub_right(a);
3100 ir_node *blk = get_nodes_block(n);
3101 ir_mode *m_b = get_irn_mode(b);
3102 ir_mode *m_y = get_irn_mode(y);
3106 /* Determine the right mode for the Add. */
3109 else if (mode_is_reference(m_b))
3111 else if (mode_is_reference(m_y))
3115 * Both modes are different but none is reference,
3116 * happens for instance in SubP(SubP(P, Iu), Is).
3117 * We have two possibilities here: Cast or ignore.
3118 * Currently we ignore this case.
3123 add = new_r_Add(blk, y, b, add_mode);
3125 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3130 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3131 /* c - ~X = X + (c+1) */
3132 if (is_Const(a) && is_Not(b)) {
3133 ir_tarval *tv = get_Const_tarval(a);
3135 tv = tarval_add(tv, get_mode_one(mode));
3136 if (tv != tarval_bad) {
3137 ir_node *blk = get_nodes_block(n);
3138 ir_graph *irg = get_irn_irg(n);
3139 ir_node *c = new_r_Const(irg, tv);
3140 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3145 /* x-(x&y) = x & ~y */
3147 ir_node *and_left = get_And_left(b);
3148 ir_node *and_right = get_And_right(b);
3149 if (and_right == a) {
3150 ir_node *tmp = and_left;
3151 and_left = and_right;
3154 if (and_left == a) {
3155 dbg_info *dbgi = get_irn_dbg_info(n);
3156 ir_node *block = get_nodes_block(n);
3157 ir_mode *mode = get_irn_mode(n);
3158 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3159 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3168 * Several transformation done on n*n=2n bits mul.
3169 * These transformations must be done here because new nodes may be produced.
3171 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3174 ir_node *a = get_Mul_left(n);
3175 ir_node *b = get_Mul_right(n);
3176 ir_tarval *ta = value_of(a);
3177 ir_tarval *tb = value_of(b);
3178 ir_mode *smode = get_irn_mode(a);
3180 if (ta == get_mode_one(smode)) {
3181 /* (L)1 * (L)b = (L)b */
3182 ir_node *blk = get_nodes_block(n);
3183 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3187 else if (ta == get_mode_minus_one(smode)) {
3188 /* (L)-1 * (L)b = (L)b */
3189 ir_node *blk = get_nodes_block(n);
3190 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3191 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3192 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3195 if (tb == get_mode_one(smode)) {
3196 /* (L)a * (L)1 = (L)a */
3197 ir_node *blk = get_irn_n(a, -1);
3198 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3199 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3202 else if (tb == get_mode_minus_one(smode)) {
3203 /* (L)a * (L)-1 = (L)-a */
3204 ir_node *blk = get_nodes_block(n);
3205 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3206 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3214 * Transform Mul(a,-1) into -a.
3215 * Do constant evaluation of Phi nodes.
3216 * Do architecture dependent optimizations on Mul nodes
3218 static ir_node *transform_node_Mul(ir_node *n)
3220 ir_node *c, *oldn = n;
3221 ir_mode *mode = get_irn_mode(n);
3222 ir_node *a = get_Mul_left(n);
3223 ir_node *b = get_Mul_right(n);
3225 n = fold_constant_associativity(n, tarval_mul);
3229 if (mode != get_irn_mode(a))
3230 return transform_node_Mul2n(n, mode);
3232 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3234 if (mode_is_signed(mode)) {
3237 if (value_of(a) == get_mode_minus_one(mode))
3239 else if (value_of(b) == get_mode_minus_one(mode))
3242 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3243 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3248 if (is_Const(b)) { /* (-a) * const -> a * -const */
3249 ir_node *cnst = const_negate(b);
3251 dbg_info *dbgi = get_irn_dbg_info(n);
3252 ir_node *block = get_nodes_block(n);
3253 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3254 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3257 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3258 dbg_info *dbgi = get_irn_dbg_info(n);
3259 ir_node *block = get_nodes_block(n);
3260 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3263 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3264 ir_node *sub_l = get_Sub_left(b);
3265 ir_node *sub_r = get_Sub_right(b);
3266 dbg_info *dbgi = get_irn_dbg_info(n);
3267 ir_node *block = get_nodes_block(n);
3268 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3269 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3273 } else if (is_Minus(b)) {
3274 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3275 ir_node *sub_l = get_Sub_left(a);
3276 ir_node *sub_r = get_Sub_right(a);
3277 dbg_info *dbgi = get_irn_dbg_info(n);
3278 ir_node *block = get_nodes_block(n);
3279 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3280 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3281 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3284 } else if (is_Shl(a)) {
3285 ir_node *const shl_l = get_Shl_left(a);
3286 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3287 /* (1 << x) * b -> b << x */
3288 dbg_info *const dbgi = get_irn_dbg_info(n);
3289 ir_node *const block = get_nodes_block(n);
3290 ir_node *const shl_r = get_Shl_right(a);
3291 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3292 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3295 } else if (is_Shl(b)) {
3296 ir_node *const shl_l = get_Shl_left(b);
3297 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3298 /* a * (1 << x) -> a << x */
3299 dbg_info *const dbgi = get_irn_dbg_info(n);
3300 ir_node *const block = get_nodes_block(n);
3301 ir_node *const shl_r = get_Shl_right(b);
3302 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3303 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3307 if (get_mode_arithmetic(mode) == irma_ieee754
3308 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3310 ir_tarval *tv = get_Const_tarval(a);
3311 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3312 && !tarval_is_negative(tv)) {
3313 /* 2.0 * b = b + b */
3314 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3315 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3319 else if (is_Const(b)) {
3320 ir_tarval *tv = get_Const_tarval(b);
3321 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3322 && !tarval_is_negative(tv)) {
3323 /* a * 2.0 = a + a */
3324 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3325 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3330 return arch_dep_replace_mul_with_shifts(n);
3334 * Transform a Div Node.
3336 static ir_node *transform_node_Div(ir_node *n)
3338 ir_mode *mode = get_Div_resmode(n);
3339 ir_node *a = get_Div_left(n);
3340 ir_node *b = get_Div_right(n);
3342 const ir_node *dummy;
3344 if (mode_is_int(mode)) {
3345 if (is_Const(b) && is_const_Phi(a)) {
3346 /* check for Div(Phi, Const) */
3347 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3349 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3352 } else if (is_Const(a) && is_const_Phi(b)) {
3353 /* check for Div(Const, Phi) */
3354 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3356 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3359 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3360 /* check for Div(Phi, Phi) */
3361 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3363 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3368 if (a == b && value_not_zero(a, &dummy)) {
3369 ir_graph *irg = get_irn_irg(n);
3370 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3371 value = new_r_Const(irg, get_mode_one(mode));
3372 DBG_OPT_CSTEVAL(n, value);
3375 if (mode_is_signed(mode) && is_Const(b)) {
3376 ir_tarval *tv = get_Const_tarval(b);
3378 if (tv == get_mode_minus_one(mode)) {
3380 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3381 DBG_OPT_CSTEVAL(n, value);
3385 /* Try architecture dependent optimization */
3386 value = arch_dep_replace_div_by_const(n);
3389 assert(mode_is_float(mode));
3391 /* Optimize x/c to x*(1/c) */
3392 if (get_mode_arithmetic(mode) == irma_ieee754) {
3393 ir_tarval *tv = value_of(b);
3395 if (tv != tarval_bad) {
3396 int rem = tarval_fp_ops_enabled();
3399 * Floating point constant folding might be disabled here to
3401 * However, as we check for exact result, doing it is safe.
3404 tarval_enable_fp_ops(1);
3405 tv = tarval_div(get_mode_one(mode), tv);
3406 tarval_enable_fp_ops(rem);
3408 /* Do the transformation if the result is either exact or we are
3409 not using strict rules. */
3410 if (tv != tarval_bad &&
3411 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3412 ir_node *block = get_nodes_block(n);
3413 ir_graph *irg = get_irn_irg(block);
3414 ir_node *c = new_r_Const(irg, tv);
3415 dbg_info *dbgi = get_irn_dbg_info(n);
3416 value = new_rd_Mul(dbgi, block, a, c, mode);
3429 /* Turn Div into a tuple (mem, jmp, bad, value) */
3430 mem = get_Div_mem(n);
3431 blk = get_nodes_block(n);
3432 irg = get_irn_irg(blk);
3434 /* skip a potential Pin */
3435 mem = skip_Pin(mem);
3436 turn_into_tuple(n, pn_Div_max+1);
3437 set_Tuple_pred(n, pn_Div_M, mem);
3438 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3439 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3440 set_Tuple_pred(n, pn_Div_res, value);
3446 * Transform a Mod node.
3448 static ir_node *transform_node_Mod(ir_node *n)
3450 ir_mode *mode = get_Mod_resmode(n);
3451 ir_node *a = get_Mod_left(n);
3452 ir_node *b = get_Mod_right(n);
3457 if (is_Const(b) && is_const_Phi(a)) {
3458 /* check for Div(Phi, Const) */
3459 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3461 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3465 else if (is_Const(a) && is_const_Phi(b)) {
3466 /* check for Div(Const, Phi) */
3467 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3469 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3473 else if (is_const_Phi(a) && is_const_Phi(b)) {
3474 /* check for Div(Phi, Phi) */
3475 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3477 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3484 irg = get_irn_irg(n);
3485 if (tv != tarval_bad) {
3486 value = new_r_Const(irg, tv);
3488 DBG_OPT_CSTEVAL(n, value);
3491 ir_node *a = get_Mod_left(n);
3492 ir_node *b = get_Mod_right(n);
3493 const ir_node *dummy;
3495 if (a == b && value_not_zero(a, &dummy)) {
3496 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3497 value = new_r_Const(irg, get_mode_null(mode));
3498 DBG_OPT_CSTEVAL(n, value);
3501 if (mode_is_signed(mode) && is_Const(b)) {
3502 ir_tarval *tv = get_Const_tarval(b);
3504 if (tv == get_mode_minus_one(mode)) {
3506 value = new_r_Const(irg, get_mode_null(mode));
3507 DBG_OPT_CSTEVAL(n, value);
3511 /* Try architecture dependent optimization */
3512 value = arch_dep_replace_mod_by_const(n);
3521 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3522 mem = get_Mod_mem(n);
3523 blk = get_nodes_block(n);
3524 irg = get_irn_irg(blk);
3526 /* skip a potential Pin */
3527 mem = skip_Pin(mem);
3528 turn_into_tuple(n, pn_Mod_max+1);
3529 set_Tuple_pred(n, pn_Mod_M, mem);
3530 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3531 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3532 set_Tuple_pred(n, pn_Mod_res, value);
3538 * Transform a Cond node.
3540 * Replace the Cond by a Jmp if it branches on a constant
3543 static ir_node *transform_node_Cond(ir_node *n)
3545 ir_node *a = get_Cond_selector(n);
3546 ir_graph *irg = get_irn_irg(n);
3550 /* we need block info which is not available in floating irgs */
3551 if (get_irg_pinned(irg) == op_pin_state_floats)
3555 if (ta == tarval_bad && is_Cmp(a)) {
3556 /* try again with a direct call to compute_cmp, as we don't care
3557 * about the MODEB_LOWERED flag here */
3558 ta = compute_cmp(a);
3561 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3562 /* It's a boolean Cond, branching on a boolean constant.
3563 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3564 ir_node *blk = get_nodes_block(n);
3565 jmp = new_r_Jmp(blk);
3566 turn_into_tuple(n, pn_Cond_max+1);
3567 if (ta == tarval_b_true) {
3568 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3569 set_Tuple_pred(n, pn_Cond_true, jmp);
3571 set_Tuple_pred(n, pn_Cond_false, jmp);
3572 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3574 /* We might generate an endless loop, so keep it alive. */
3575 add_End_keepalive(get_irg_end(irg), blk);
3576 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3581 static ir_node *transform_node_Switch(ir_node *n)
3583 ir_node *op = get_Switch_selector(n);
3584 ir_tarval *val = value_of(op);
3585 if (val != tarval_bad) {
3586 dbg_info *dbgi = get_irn_dbg_info(n);
3587 ir_graph *irg = get_irn_irg(n);
3588 unsigned n_outs = get_Switch_n_outs(n);
3589 ir_node *block = get_nodes_block(n);
3590 ir_node *bad = new_r_Bad(irg, mode_X);
3591 ir_node **in = XMALLOCN(ir_node*, n_outs);
3592 const ir_switch_table *table = get_Switch_table(n);
3593 size_t n_entries = ir_switch_table_get_n_entries(table);
3597 for (i = 0; i < n_entries; ++i) {
3598 const ir_switch_table_entry *entry
3599 = ir_switch_table_get_entry_const(table, i);
3600 ir_tarval *min = entry->min;
3601 ir_tarval *max = entry->max;
3604 if ((min == max && min == val)
3605 || (tarval_cmp(val, min) != ir_relation_less
3606 && tarval_cmp(val, max) != ir_relation_greater)) {
3611 for (o = 0; o < n_outs; ++o) {
3612 if (o == (unsigned)jmp_pn) {
3613 in[o] = new_rd_Jmp(dbgi, block);
3618 return new_r_Tuple(block, (int)n_outs, in);
3624 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3626 * - and, or, xor instead of &
3627 * - Shl, Shr, Shrs, rotl instead of >>
3628 * (with a special case for Or/Xor + Shrs)
3630 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3632 static ir_node *transform_node_shift_bitop(ir_node *n)
3634 ir_graph *irg = get_irn_irg(n);
3635 ir_node *right = get_binop_right(n);
3636 ir_mode *mode = get_irn_mode(n);
3638 ir_node *bitop_left;
3639 ir_node *bitop_right;
3648 ir_tarval *tv_shift;
3650 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3653 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3655 if (!is_Const(right))
3658 left = get_binop_left(n);
3659 op_left = get_irn_op(left);
3660 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3663 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3664 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3665 /* TODO: test if sign bit is affectes */
3669 bitop_right = get_binop_right(left);
3670 if (!is_Const(bitop_right))
3673 bitop_left = get_binop_left(left);
3675 block = get_nodes_block(n);
3676 dbgi = get_irn_dbg_info(n);
3677 tv1 = get_Const_tarval(bitop_right);
3678 tv2 = get_Const_tarval(right);
3680 assert(get_tarval_mode(tv1) == mode);
3683 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3684 tv_shift = tarval_shl(tv1, tv2);
3685 } else if (is_Shr(n)) {
3686 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3687 tv_shift = tarval_shr(tv1, tv2);
3688 } else if (is_Shrs(n)) {
3689 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3690 tv_shift = tarval_shrs(tv1, tv2);
3693 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3694 tv_shift = tarval_rotl(tv1, tv2);
3697 assert(get_tarval_mode(tv_shift) == mode);
3698 irg = get_irn_irg(n);
3699 new_const = new_r_Const(irg, tv_shift);
3701 if (op_left == op_And) {
3702 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3703 } else if (op_left == op_Or) {
3704 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3706 assert(op_left == op_Eor);
3707 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3716 static ir_node *transform_node_And(ir_node *n)
3718 ir_node *c, *oldn = n;
3719 ir_node *a = get_And_left(n);
3720 ir_node *b = get_And_right(n);
3723 n = fold_constant_associativity(n, tarval_and);
3727 if (is_Cmp(a) && is_Cmp(b)) {
3728 ir_node *a_left = get_Cmp_left(a);
3729 ir_node *a_right = get_Cmp_right(a);
3730 ir_node *b_left = get_Cmp_left(b);
3731 ir_node *b_right = get_Cmp_right(b);
3732 ir_relation a_relation = get_Cmp_relation(a);
3733 ir_relation b_relation = get_Cmp_relation(b);
3734 /* we can combine the relations of two compares with the same
3736 if (a_left == b_left && b_left == b_right) {
3737 dbg_info *dbgi = get_irn_dbg_info(n);
3738 ir_node *block = get_nodes_block(n);
3739 ir_relation new_relation = a_relation & b_relation;
3740 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3742 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3743 if (a_relation == b_relation && a_relation == ir_relation_equal
3744 && !mode_is_float(get_irn_mode(a_left))
3745 && !mode_is_float(get_irn_mode(b_left))) {
3746 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3747 dbg_info *dbgi = get_irn_dbg_info(n);
3748 ir_node *block = get_nodes_block(n);
3749 ir_mode *a_mode = get_irn_mode(a_left);
3750 ir_mode *b_mode = get_irn_mode(b_left);
3751 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3752 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3753 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3754 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3755 ir_graph *irg = get_irn_irg(n);
3756 ir_node *zero = create_zero_const(irg, b_mode);
3757 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3759 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3760 dbg_info *dbgi = get_irn_dbg_info(n);
3761 ir_node *block = get_nodes_block(n);
3762 ir_mode *a_mode = get_irn_mode(a_left);
3763 ir_mode *b_mode = get_irn_mode(b_left);
3764 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3765 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3766 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3767 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3768 ir_graph *irg = get_irn_irg(n);
3769 ir_node *zero = create_zero_const(irg, a_mode);
3770 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3775 mode = get_irn_mode(n);
3776 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3778 if (is_Or(a) || is_Or_Eor_Add(a)) {
3779 ir_node *or_left = get_binop_left(a);
3780 ir_node *or_right = get_binop_right(a);
3781 if (complement_values(or_left, b)) {
3782 /* (a|b) & ~a => b & ~a */
3783 dbg_info *dbgi = get_irn_dbg_info(n);
3784 ir_node *block = get_nodes_block(n);
3785 return new_rd_And(dbgi, block, or_right, b, mode);
3786 } else if (complement_values(or_right, b)) {
3787 /* (a|b) & ~b => a & ~b */
3788 dbg_info *dbgi = get_irn_dbg_info(n);
3789 ir_node *block = get_nodes_block(n);
3790 return new_rd_And(dbgi, block, or_left, b, mode);
3791 } else if (is_Not(b)) {
3792 ir_node *op = get_Not_op(b);
3794 ir_node *ba = get_And_left(op);
3795 ir_node *bb = get_And_right(op);
3797 /* it's enough to test the following cases due to normalization! */
3798 if (or_left == ba && or_right == bb) {
3799 /* (a|b) & ~(a&b) = a^b */
3800 ir_node *block = get_nodes_block(n);
3802 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3803 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3809 if (is_Or(b) || is_Or_Eor_Add(b)) {
3810 ir_node *or_left = get_binop_left(b);
3811 ir_node *or_right = get_binop_right(b);
3812 if (complement_values(or_left, a)) {
3813 /* (a|b) & ~a => b & ~a */
3814 dbg_info *dbgi = get_irn_dbg_info(n);
3815 ir_node *block = get_nodes_block(n);
3816 return new_rd_And(dbgi, block, or_right, a, mode);
3817 } else if (complement_values(or_right, a)) {
3818 /* (a|b) & ~b => a & ~b */
3819 dbg_info *dbgi = get_irn_dbg_info(n);
3820 ir_node *block = get_nodes_block(n);
3821 return new_rd_And(dbgi, block, or_left, a, mode);
3822 } else if (is_Not(a)) {
3823 ir_node *op = get_Not_op(a);
3825 ir_node *aa = get_And_left(op);
3826 ir_node *ab = get_And_right(op);
3828 /* it's enough to test the following cases due to normalization! */
3829 if (or_left == aa && or_right == ab) {
3830 /* (a|b) & ~(a&b) = a^b */
3831 ir_node *block = get_nodes_block(n);
3833 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3834 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3840 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3841 ir_node *al = get_binop_left(a);
3842 ir_node *ar = get_binop_right(a);
3845 /* (b ^ a) & b -> ~a & b */
3846 dbg_info *dbg = get_irn_dbg_info(n);
3847 ir_node *block = get_nodes_block(n);
3849 ar = new_rd_Not(dbg, block, ar, mode);
3850 n = new_rd_And(dbg, block, ar, b, mode);
3851 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3855 /* (a ^ b) & b -> ~a & b */
3856 dbg_info *dbg = get_irn_dbg_info(n);
3857 ir_node *block = get_nodes_block(n);
3859 al = new_rd_Not(dbg, block, al, mode);
3860 n = new_rd_And(dbg, block, al, b, mode);
3861 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3865 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3866 ir_node *bl = get_binop_left(b);
3867 ir_node *br = get_binop_right(b);
3870 /* a & (a ^ b) -> a & ~b */
3871 dbg_info *dbg = get_irn_dbg_info(n);
3872 ir_node *block = get_nodes_block(n);
3874 br = new_rd_Not(dbg, block, br, mode);
3875 n = new_rd_And(dbg, block, br, a, mode);
3876 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3880 /* a & (b ^ a) -> a & ~b */
3881 dbg_info *dbg = get_irn_dbg_info(n);
3882 ir_node *block = get_nodes_block(n);
3884 bl = new_rd_Not(dbg, block, bl, mode);
3885 n = new_rd_And(dbg, block, bl, a, mode);
3886 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3890 if (is_Not(a) && is_Not(b)) {
3891 /* ~a & ~b = ~(a|b) */
3892 ir_node *block = get_nodes_block(n);
3893 ir_mode *mode = get_irn_mode(n);
3897 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3898 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3899 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3904 vrp_attr *b_vrp = vrp_get_info(b);
3905 ir_tarval *a_val = get_Const_tarval(a);
3906 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3912 vrp_attr *a_vrp = vrp_get_info(a);
3913 ir_tarval *b_val = get_Const_tarval(b);
3914 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3919 n = transform_bitwise_distributive(n, transform_node_And);
3921 n = transform_node_bitop_shift(n);
3929 static ir_node *transform_node_Not(ir_node *n)
3931 ir_node *c, *oldn = n;
3932 ir_node *a = get_Not_op(n);
3933 ir_mode *mode = get_irn_mode(n);
3935 HANDLE_UNOP_PHI(tarval_not,a,c);
3937 /* check for a boolean Not */
3939 dbg_info *dbgi = get_irn_dbg_info(a);
3940 ir_node *block = get_nodes_block(a);
3941 ir_relation relation = get_Cmp_relation(a);
3942 relation = get_negated_relation(relation);
3943 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3944 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3948 /* normalize ~(a ^ b) => a ^ ~b */
3949 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3950 dbg_info *dbg = get_irn_dbg_info(n);
3951 ir_node *block = get_nodes_block(n);
3952 ir_node *eor_right = get_binop_right(a);
3953 ir_node *eor_left = get_binop_left(a);
3954 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3955 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3959 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3960 if (is_Minus(a)) { /* ~-x -> x + -1 */
3961 dbg_info *dbg = get_irn_dbg_info(n);
3962 ir_graph *irg = get_irn_irg(n);
3963 ir_node *block = get_nodes_block(n);
3964 ir_node *add_l = get_Minus_op(a);
3965 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3966 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3967 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3968 ir_node *add_r = get_binop_right(a);
3969 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3970 /* ~(x + -1) = -x */
3971 ir_node *op = get_binop_left(a);
3972 ir_node *blk = get_nodes_block(n);
3973 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3974 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3982 * Transform a Minus.
3986 * -(a >>u (size-1)) = a >>s (size-1)
3987 * -(a >>s (size-1)) = a >>u (size-1)
3988 * -(a * const) -> a * -const
3990 static ir_node *transform_node_Minus(ir_node *n)
3992 ir_node *c, *oldn = n;
3993 ir_node *a = get_Minus_op(n);
3996 HANDLE_UNOP_PHI(tarval_neg,a,c);
3998 mode = get_irn_mode(a);
3999 if (get_mode_arithmetic(mode) == irma_twos_complement) {
4000 /* the following rules are only to twos-complement */
4003 ir_node *op = get_Not_op(a);
4004 ir_tarval *tv = get_mode_one(mode);
4005 ir_node *blk = get_nodes_block(n);
4006 ir_graph *irg = get_irn_irg(blk);
4007 ir_node *c = new_r_Const(irg, tv);
4008 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
4009 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4013 ir_node *c = get_Shr_right(a);
4016 ir_tarval *tv = get_Const_tarval(c);
4018 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4019 /* -(a >>u (size-1)) = a >>s (size-1) */
4020 ir_node *v = get_Shr_left(a);
4022 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4023 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4029 ir_node *c = get_Shrs_right(a);
4032 ir_tarval *tv = get_Const_tarval(c);
4034 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4035 /* -(a >>s (size-1)) = a >>u (size-1) */
4036 ir_node *v = get_Shrs_left(a);
4038 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4039 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4046 /* - (a-b) = b - a */
4047 ir_node *la = get_Sub_left(a);
4048 ir_node *ra = get_Sub_right(a);
4049 ir_node *blk = get_nodes_block(n);
4051 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4052 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4056 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4057 ir_node *mul_l = get_Mul_left(a);
4058 ir_node *mul_r = get_Mul_right(a);
4059 ir_tarval *tv = value_of(mul_r);
4060 if (tv != tarval_bad) {
4061 tv = tarval_neg(tv);
4062 if (tv != tarval_bad) {
4063 ir_graph *irg = get_irn_irg(n);
4064 ir_node *cnst = new_r_Const(irg, tv);
4065 dbg_info *dbg = get_irn_dbg_info(a);
4066 ir_node *block = get_nodes_block(a);
4067 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4068 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4078 * Transform a Proj(Load) with a non-null address.
4080 static ir_node *transform_node_Proj_Load(ir_node *proj)
4082 if (get_opt_ldst_only_null_ptr_exceptions()) {
4083 if (get_irn_mode(proj) == mode_X) {
4084 ir_node *load = get_Proj_pred(proj);
4086 /* get the Load address */
4087 const ir_node *addr = get_Load_ptr(load);
4088 const ir_node *confirm;
4090 if (value_not_null(addr, &confirm)) {
4091 if (confirm == NULL) {
4092 /* this node may float if it did not depend on a Confirm */
4093 set_irn_pinned(load, op_pin_state_floats);
4095 if (get_Proj_proj(proj) == pn_Load_X_except) {
4096 ir_graph *irg = get_irn_irg(proj);
4097 DBG_OPT_EXC_REM(proj);
4098 return new_r_Bad(irg, mode_X);
4100 ir_node *blk = get_nodes_block(load);
4101 return new_r_Jmp(blk);
4110 * Transform a Proj(Store) with a non-null address.
4112 static ir_node *transform_node_Proj_Store(ir_node *proj)
4114 if (get_opt_ldst_only_null_ptr_exceptions()) {
4115 if (get_irn_mode(proj) == mode_X) {
4116 ir_node *store = get_Proj_pred(proj);
4118 /* get the load/store address */
4119 const ir_node *addr = get_Store_ptr(store);
4120 const ir_node *confirm;
4122 if (value_not_null(addr, &confirm)) {
4123 if (confirm == NULL) {
4124 /* this node may float if it did not depend on a Confirm */
4125 set_irn_pinned(store, op_pin_state_floats);
4127 if (get_Proj_proj(proj) == pn_Store_X_except) {
4128 ir_graph *irg = get_irn_irg(proj);
4129 DBG_OPT_EXC_REM(proj);
4130 return new_r_Bad(irg, mode_X);
4132 ir_node *blk = get_nodes_block(store);
4133 return new_r_Jmp(blk);
4142 * Transform a Proj(Div) with a non-zero value.
4143 * Removes the exceptions and routes the memory to the NoMem node.
4145 static ir_node *transform_node_Proj_Div(ir_node *proj)
4147 ir_node *div = get_Proj_pred(proj);
4148 ir_node *b = get_Div_right(div);
4149 ir_node *res, *new_mem;
4150 const ir_node *confirm;
4153 if (value_not_zero(b, &confirm)) {
4154 /* div(x, y) && y != 0 */
4155 if (confirm == NULL) {
4156 /* we are sure we have a Const != 0 */
4157 new_mem = get_Div_mem(div);
4158 new_mem = skip_Pin(new_mem);
4159 set_Div_mem(div, new_mem);
4160 set_irn_pinned(div, op_pin_state_floats);
4163 proj_nr = get_Proj_proj(proj);
4165 case pn_Div_X_regular:
4166 return new_r_Jmp(get_nodes_block(div));
4168 case pn_Div_X_except: {
4169 ir_graph *irg = get_irn_irg(proj);
4170 /* we found an exception handler, remove it */
4171 DBG_OPT_EXC_REM(proj);
4172 return new_r_Bad(irg, mode_X);
4176 ir_graph *irg = get_irn_irg(proj);
4177 res = get_Div_mem(div);
4178 new_mem = get_irg_no_mem(irg);
4181 /* This node can only float up to the Confirm block */
4182 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4184 set_irn_pinned(div, op_pin_state_floats);
4185 /* this is a Div without exception, we can remove the memory edge */
4186 set_Div_mem(div, new_mem);
4195 * Transform a Proj(Mod) with a non-zero value.
4196 * Removes the exceptions and routes the memory to the NoMem node.
4198 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4200 ir_node *mod = get_Proj_pred(proj);
4201 ir_node *b = get_Mod_right(mod);
4202 ir_node *res, *new_mem;
4203 const ir_node *confirm;
4206 if (value_not_zero(b, &confirm)) {
4207 /* mod(x, y) && y != 0 */
4208 proj_nr = get_Proj_proj(proj);
4210 if (confirm == NULL) {
4211 /* we are sure we have a Const != 0 */
4212 new_mem = get_Mod_mem(mod);
4213 new_mem = skip_Pin(new_mem);
4214 set_Mod_mem(mod, new_mem);
4215 set_irn_pinned(mod, op_pin_state_floats);
4220 case pn_Mod_X_regular:
4221 return new_r_Jmp(get_irn_n(mod, -1));
4223 case pn_Mod_X_except: {
4224 ir_graph *irg = get_irn_irg(proj);
4225 /* we found an exception handler, remove it */
4226 DBG_OPT_EXC_REM(proj);
4227 return new_r_Bad(irg, mode_X);
4231 ir_graph *irg = get_irn_irg(proj);
4232 res = get_Mod_mem(mod);
4233 new_mem = get_irg_no_mem(irg);
4236 /* This node can only float up to the Confirm block */
4237 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4239 /* this is a Mod without exception, we can remove the memory edge */
4240 set_Mod_mem(mod, new_mem);
4244 if (get_Mod_left(mod) == b) {
4245 /* a % a = 0 if a != 0 */
4246 ir_graph *irg = get_irn_irg(proj);
4247 ir_mode *mode = get_irn_mode(proj);
4248 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4250 DBG_OPT_CSTEVAL(mod, res);
4259 * return true if the operation returns a value with exactly 1 bit set
4261 static bool is_single_bit(const ir_node *node)
4263 /* a first implementation, could be extended with vrp and others... */
4265 ir_node *shl_l = get_Shl_left(node);
4266 ir_mode *mode = get_irn_mode(node);
4267 int modulo = get_mode_modulo_shift(mode);
4268 /* this works if we shift a 1 and we have modulo shift */
4269 if (is_Const(shl_l) && is_Const_one(shl_l)
4270 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4273 } else if (is_Const(node)) {
4274 ir_tarval *tv = get_Const_tarval(node);
4275 return tarval_is_single_bit(tv);
4281 * checks if node just flips a bit in another node and returns that other node
4282 * if so. @p tv should be a value having just 1 bit set
4284 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4287 return get_Not_op(node);
4289 ir_node *right = get_Eor_right(node);
4290 if (is_Const(right)) {
4291 ir_tarval *right_tv = get_Const_tarval(right);
4292 ir_mode *mode = get_irn_mode(node);
4293 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4294 return get_Eor_left(node);
4301 * Normalizes and optimizes Cmp nodes.
4303 static ir_node *transform_node_Cmp(ir_node *n)
4305 ir_node *left = get_Cmp_left(n);
4306 ir_node *right = get_Cmp_right(n);
4307 ir_mode *mode = get_irn_mode(left);
4308 ir_tarval *tv = NULL;
4309 bool changed = false;
4310 bool changedc = false;
4311 ir_relation relation = get_Cmp_relation(n);
4312 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4314 /* mask out impossible relations */
4315 ir_relation new_relation = relation & possible;
4316 if (new_relation != relation) {
4317 relation = new_relation;
4321 /* Remove unnecessary conversions */
4322 if (is_Conv(left) && is_Conv(right)) {
4323 ir_node *op_left = get_Conv_op(left);
4324 ir_node *op_right = get_Conv_op(right);
4325 ir_mode *mode_left = get_irn_mode(op_left);
4326 ir_mode *mode_right = get_irn_mode(op_right);
4328 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4329 && mode_left != mode_b && mode_right != mode_b) {
4330 ir_node *block = get_nodes_block(n);
4332 if (mode_left == mode_right) {
4336 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4337 } else if (smaller_mode(mode_left, mode_right)) {
4338 left = new_r_Conv(block, op_left, mode_right);
4341 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4342 } else if (smaller_mode(mode_right, mode_left)) {
4344 right = new_r_Conv(block, op_right, mode_left);
4346 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4348 mode = get_irn_mode(left);
4351 if (is_Conv(left) && is_Const(right)) {
4352 ir_node *op_left = get_Conv_op(left);
4353 ir_mode *mode_left = get_irn_mode(op_left);
4354 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4355 ir_tarval *tv = get_Const_tarval(right);
4356 tarval_int_overflow_mode_t last_mode
4357 = tarval_get_integer_overflow_mode();
4359 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4360 new_tv = tarval_convert_to(tv, mode_left);
4361 tarval_set_integer_overflow_mode(last_mode);
4362 if (new_tv != tarval_bad) {
4363 ir_graph *irg = get_irn_irg(n);
4365 right = new_r_Const(irg, new_tv);
4366 mode = get_irn_mode(left);
4368 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4374 * Optimize -a CMP -b into b CMP a.
4375 * This works only for modes where unary Minus cannot Overflow.
4376 * Note that two-complement integers can Overflow so it will NOT work.
4378 if (!mode_overflow_on_unary_Minus(mode) &&
4379 is_Minus(left) && is_Minus(right)) {
4380 left = get_Minus_op(left);
4381 right = get_Minus_op(right);
4382 relation = get_inversed_relation(relation);
4384 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4387 /* remove operation on both sides if possible */
4388 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4390 * The following operations are NOT safe for floating point operations, for instance
4391 * 1.0 + inf == 2.0 + inf, =/=> x == y
4393 if (mode_is_int(mode)) {
4394 unsigned lop = get_irn_opcode(left);
4396 if (lop == get_irn_opcode(right)) {
4397 ir_node *ll, *lr, *rl, *rr;
4399 /* same operation on both sides, try to remove */
4403 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4404 left = get_unop_op(left);
4405 right = get_unop_op(right);
4407 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4410 ll = get_Add_left(left);
4411 lr = get_Add_right(left);
4412 rl = get_Add_left(right);
4413 rr = get_Add_right(right);
4416 /* X + a CMP X + b ==> a CMP b */
4420 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4421 } else if (ll == rr) {
4422 /* X + a CMP b + X ==> a CMP b */
4426 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4427 } else if (lr == rl) {
4428 /* a + X CMP X + b ==> a CMP b */
4432 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4433 } else if (lr == rr) {
4434 /* a + X CMP b + X ==> a CMP b */
4438 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4442 ll = get_Sub_left(left);
4443 lr = get_Sub_right(left);
4444 rl = get_Sub_left(right);
4445 rr = get_Sub_right(right);
4448 /* X - a CMP X - b ==> a CMP b */
4452 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4453 } else if (lr == rr) {
4454 /* a - X CMP b - X ==> a CMP b */
4458 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4462 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4463 /* a ROTL X CMP b ROTL X ==> a CMP b */
4464 left = get_Rotl_left(left);
4465 right = get_Rotl_left(right);
4467 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4475 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4476 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4477 ir_node *ll = get_binop_left(left);
4478 ir_node *lr = get_binop_right(left);
4480 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4486 ir_graph *irg = get_irn_irg(n);
4488 right = create_zero_const(irg, mode);
4490 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4493 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4494 ir_node *rl = get_binop_left(right);
4495 ir_node *rr = get_binop_right(right);
4497 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4503 ir_graph *irg = get_irn_irg(n);
4505 right = create_zero_const(irg, mode);
4507 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4511 if (is_And(left) && is_Const(right)) {
4512 ir_node *ll = get_binop_left(left);
4513 ir_node *lr = get_binop_right(left);
4514 if (is_Shr(ll) && is_Const(lr)) {
4515 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4516 ir_node *block = get_nodes_block(n);
4517 ir_mode *mode = get_irn_mode(left);
4519 ir_node *llr = get_Shr_right(ll);
4520 if (is_Const(llr)) {
4521 dbg_info *dbg = get_irn_dbg_info(left);
4522 ir_graph *irg = get_irn_irg(left);
4524 ir_tarval *c1 = get_Const_tarval(llr);
4525 ir_tarval *c2 = get_Const_tarval(lr);
4526 ir_tarval *c3 = get_Const_tarval(right);
4527 ir_tarval *mask = tarval_shl(c2, c1);
4528 ir_tarval *value = tarval_shl(c3, c1);
4530 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4531 right = new_r_Const(irg, value);
4536 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4538 if (is_Const(right) && is_Const_null(right) &&
4539 (is_Eor(left) || is_Or_Eor_Add(left))) {
4540 right = get_Eor_right(left);
4541 left = get_Eor_left(left);
4547 if (mode_is_int(mode) && is_And(left)) {
4548 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4549 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4550 if (relation == ir_relation_equal
4551 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4552 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4553 ir_node *and0 = get_And_left(left);
4554 ir_node *and1 = get_And_right(left);
4555 if (and1 == right) {
4556 ir_node *tmp = and0;
4560 if (and0 == right && is_single_bit(and0)) {
4561 ir_graph *irg = get_irn_irg(n);
4563 relation == ir_relation_equal ? ir_relation_less_greater
4564 : ir_relation_equal;
4565 right = create_zero_const(irg, mode);
4571 if (is_Const(right) && is_Const_null(right) &&
4572 (relation == ir_relation_equal
4573 || (relation == ir_relation_less_greater)
4574 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4576 /* instead of flipping the bit before the bit-test operation negate
4578 ir_node *and0 = get_And_left(left);
4579 ir_node *and1 = get_And_right(left);
4580 if (is_Const(and1)) {
4581 ir_tarval *tv = get_Const_tarval(and1);
4582 if (tarval_is_single_bit(tv)) {
4583 ir_node *flipped = flips_bit(and0, tv);
4584 if (flipped != NULL) {
4585 dbg_info *dbgi = get_irn_dbg_info(left);
4586 ir_node *block = get_nodes_block(left);
4587 relation = get_negated_relation(relation);
4588 left = new_rd_And(dbgi, block, flipped, and1, mode);
4597 /* replace mode_b compares with ands/ors */
4598 if (mode == mode_b) {
4599 ir_node *block = get_nodes_block(n);
4603 case ir_relation_less_equal:
4604 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4606 case ir_relation_less:
4607 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4609 case ir_relation_greater_equal:
4610 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4612 case ir_relation_greater:
4613 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4615 case ir_relation_less_greater:
4616 bres = new_r_Eor(block, left, right, mode_b);
4618 case ir_relation_equal:
4619 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4622 #ifdef DEBUG_libfirm
4623 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4628 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4634 * First step: normalize the compare op
4635 * by placing the constant on the right side
4636 * or moving the lower address node to the left.
4638 if (!operands_are_normalized(left, right)) {
4643 relation = get_inversed_relation(relation);
4648 * Second step: Try to reduce the magnitude
4649 * of a constant. This may help to generate better code
4650 * later and may help to normalize more compares.
4651 * Of course this is only possible for integer values.
4653 tv = value_of(right);
4654 if (tv != tarval_bad) {
4655 ir_mode *mode = get_irn_mode(right);
4657 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4658 * cmp(ct,c2) | cmp(cf,c2) | result
4659 * -----------|------------|--------
4660 * true | true | True
4661 * false | false | False
4663 * false | true | not(x)
4666 ir_node *mux_true = get_Mux_true(left);
4667 ir_node *mux_false = get_Mux_false(left);
4668 if (is_Const(mux_true) && is_Const(mux_false)) {
4669 /* we can fold true/false constant separately */
4670 ir_tarval *tv_true = get_Const_tarval(mux_true);
4671 ir_tarval *tv_false = get_Const_tarval(mux_false);
4672 ir_relation r_true = tarval_cmp(tv_true, tv);
4673 ir_relation r_false = tarval_cmp(tv_false, tv);
4674 if (r_true != ir_relation_false
4675 || r_false != ir_relation_false) {
4676 bool rel_true = (r_true & relation) != 0;
4677 bool rel_false = (r_false & relation) != 0;
4678 ir_node *cond = get_Mux_sel(left);
4679 if (rel_true == rel_false) {
4680 relation = rel_true ? ir_relation_true
4681 : ir_relation_false;
4682 } else if (rel_true) {
4685 dbg_info *dbgi = get_irn_dbg_info(n);
4686 ir_node *block = get_nodes_block(n);
4687 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4694 /* TODO extend to arbitrary constants */
4695 if (is_Conv(left) && tarval_is_null(tv)) {
4696 ir_node *op = get_Conv_op(left);
4697 ir_mode *op_mode = get_irn_mode(op);
4700 * UpConv(x) REL 0 ==> x REL 0
4701 * Don't do this for float values as it's unclear whether it is a
4702 * win. (on the other side it makes detection/creation of fabs hard)
4704 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4705 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4706 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4707 !mode_is_float(mode)) {
4708 tv = get_mode_null(op_mode);
4712 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4716 if (tv != tarval_bad) {
4717 /* the following optimization is possible on modes without Overflow
4718 * on Unary Minus or on == and !=:
4719 * -a CMP c ==> a swap(CMP) -c
4721 * Beware: for two-complement Overflow may occur, so only == and != can
4722 * be optimized, see this:
4723 * -MININT < 0 =/=> MININT > 0 !!!
4725 if (is_Minus(left) &&
4726 (!mode_overflow_on_unary_Minus(mode) ||
4727 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4728 tv = tarval_neg(tv);
4730 if (tv != tarval_bad) {
4731 left = get_Minus_op(left);
4732 relation = get_inversed_relation(relation);
4734 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4736 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4737 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4738 tv = tarval_not(tv);
4740 if (tv != tarval_bad) {
4741 left = get_Not_op(left);
4743 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4747 /* for integer modes, we have more */
4748 if (mode_is_int(mode) && !is_Const(left)) {
4749 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4750 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4751 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4752 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4754 if (tv != tarval_bad) {
4755 relation ^= ir_relation_equal;
4757 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4760 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4761 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4762 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4763 tv = tarval_add(tv, get_mode_one(mode));
4765 if (tv != tarval_bad) {
4766 relation ^= ir_relation_equal;
4768 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4772 /* the following reassociations work only for == and != */
4773 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4774 if (tv != tarval_bad) {
4775 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4777 ir_node *c1 = get_Sub_right(left);
4778 ir_tarval *tv2 = value_of(c1);
4780 if (tv2 != tarval_bad) {
4781 tv2 = tarval_add(tv, value_of(c1));
4783 if (tv2 != tarval_bad) {
4784 left = get_Sub_left(left);
4787 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4791 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4792 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4793 ir_node *a_l = get_binop_left(left);
4794 ir_node *a_r = get_binop_right(left);
4798 if (is_Const(a_l)) {
4800 tv2 = value_of(a_l);
4803 tv2 = value_of(a_r);
4806 if (tv2 != tarval_bad) {
4807 tv2 = tarval_sub(tv, tv2, NULL);
4809 if (tv2 != tarval_bad) {
4813 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4817 /* -a == c ==> a == -c, -a != c ==> a != -c */
4818 else if (is_Minus(left)) {
4819 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4821 if (tv2 != tarval_bad) {
4822 left = get_Minus_op(left);
4825 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4832 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4833 switch (get_irn_opcode(left)) {
4837 c1 = get_And_right(left);
4840 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4841 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4843 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4845 /* TODO: move to constant evaluation */
4846 ir_graph *irg = get_irn_irg(n);
4847 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4848 c1 = new_r_Const(irg, tv);
4849 DBG_OPT_CSTEVAL(n, c1);
4853 if (tarval_is_single_bit(tv)) {
4855 * optimization for AND:
4857 * And(x, C) == C ==> And(x, C) != 0
4858 * And(x, C) != C ==> And(X, C) == 0
4860 * if C is a single Bit constant.
4863 /* check for Constant's match. We have check hare the tarvals,
4864 because our const might be changed */
4865 if (get_Const_tarval(c1) == tv) {
4866 /* fine: do the transformation */
4867 tv = get_mode_null(get_tarval_mode(tv));
4868 relation ^= ir_relation_less_equal_greater;
4870 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4876 c1 = get_Or_right(left);
4877 if (is_Const(c1) && tarval_is_null(tv)) {
4879 * Or(x, C) == 0 && C != 0 ==> FALSE
4880 * Or(x, C) != 0 && C != 0 ==> TRUE
4882 if (! tarval_is_null(get_Const_tarval(c1))) {
4883 /* TODO: move to constant evaluation */
4884 ir_graph *irg = get_irn_irg(n);
4885 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4886 c1 = new_r_Const(irg, tv);
4887 DBG_OPT_CSTEVAL(n, c1);
4894 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4896 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4899 c1 = get_Shl_right(left);
4901 ir_graph *irg = get_irn_irg(c1);
4902 ir_tarval *tv1 = get_Const_tarval(c1);
4903 ir_mode *mode = get_irn_mode(left);
4904 ir_tarval *minus1 = get_mode_all_one(mode);
4905 ir_tarval *amask = tarval_shr(minus1, tv1);
4906 ir_tarval *cmask = tarval_shl(minus1, tv1);
4909 if (tarval_and(tv, cmask) != tv) {
4910 /* condition not met */
4911 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4912 c1 = new_r_Const(irg, tv);
4913 DBG_OPT_CSTEVAL(n, c1);
4916 sl = get_Shl_left(left);
4917 blk = get_nodes_block(n);
4918 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4919 tv = tarval_shr(tv, tv1);
4921 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4926 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4928 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4931 c1 = get_Shr_right(left);
4933 ir_graph *irg = get_irn_irg(c1);
4934 ir_tarval *tv1 = get_Const_tarval(c1);
4935 ir_mode *mode = get_irn_mode(left);
4936 ir_tarval *minus1 = get_mode_all_one(mode);
4937 ir_tarval *amask = tarval_shl(minus1, tv1);
4938 ir_tarval *cmask = tarval_shr(minus1, tv1);
4941 if (tarval_and(tv, cmask) != tv) {
4942 /* condition not met */
4943 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4944 c1 = new_r_Const(irg, tv);
4945 DBG_OPT_CSTEVAL(n, c1);
4948 sl = get_Shr_left(left);
4949 blk = get_nodes_block(n);
4950 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4951 tv = tarval_shl(tv, tv1);
4953 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4958 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4960 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4963 c1 = get_Shrs_right(left);
4965 ir_graph *irg = get_irn_irg(c1);
4966 ir_tarval *tv1 = get_Const_tarval(c1);
4967 ir_mode *mode = get_irn_mode(left);
4968 ir_tarval *minus1 = get_mode_all_one(mode);
4969 ir_tarval *amask = tarval_shl(minus1, tv1);
4970 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4973 cond = tarval_sub(cond, tv1, NULL);
4974 cond = tarval_shrs(tv, cond);
4976 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4977 /* condition not met */
4978 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4979 c1 = new_r_Const(irg, tv);
4980 DBG_OPT_CSTEVAL(n, c1);
4983 sl = get_Shrs_left(left);
4984 blk = get_nodes_block(n);
4985 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4986 tv = tarval_shl(tv, tv1);
4988 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4996 if (changedc) { /* need a new Const */
4997 ir_graph *irg = get_irn_irg(n);
4998 right = new_r_Const(irg, tv);
5002 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
5003 ir_node *op = get_Proj_pred(left);
5005 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
5006 ir_node *c = get_binop_right(op);
5009 ir_tarval *tv = get_Const_tarval(c);
5011 if (tarval_is_single_bit(tv)) {
5012 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5013 ir_node *v = get_binop_left(op);
5014 ir_node *blk = get_irn_n(op, -1);
5015 ir_graph *irg = get_irn_irg(op);
5016 ir_mode *mode = get_irn_mode(v);
5018 tv = tarval_sub(tv, get_mode_one(mode), NULL);
5019 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5021 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5028 dbg_info *dbgi = get_irn_dbg_info(n);
5029 ir_node *block = get_nodes_block(n);
5031 /* create a new compare */
5032 n = new_rd_Cmp(dbgi, block, left, right, relation);
5039 * Optimize CopyB(mem, x, x) into a Nop.
5041 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5043 ir_node *copyb = get_Proj_pred(proj);
5044 ir_node *a = get_CopyB_dst(copyb);
5045 ir_node *b = get_CopyB_src(copyb);
5048 switch (get_Proj_proj(proj)) {
5049 case pn_CopyB_X_regular:
5050 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5051 DBG_OPT_EXC_REM(proj);
5052 proj = new_r_Jmp(get_nodes_block(copyb));
5054 case pn_CopyB_X_except: {
5055 ir_graph *irg = get_irn_irg(proj);
5056 DBG_OPT_EXC_REM(proj);
5057 proj = new_r_Bad(irg, mode_X);
5068 * Optimize Bounds(idx, idx, upper) into idx.
5070 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5072 ir_node *oldn = proj;
5073 ir_node *bound = get_Proj_pred(proj);
5074 ir_node *idx = get_Bound_index(bound);
5075 ir_node *pred = skip_Proj(idx);
5078 if (idx == get_Bound_lower(bound))
5080 else if (is_Bound(pred)) {
5082 * idx was Bounds checked previously, it is still valid if
5083 * lower <= pred_lower && pred_upper <= upper.
5085 ir_node *lower = get_Bound_lower(bound);
5086 ir_node *upper = get_Bound_upper(bound);
5087 if (get_Bound_lower(pred) == lower &&
5088 get_Bound_upper(pred) == upper) {
5090 * One could expect that we simply return the previous
5091 * Bound here. However, this would be wrong, as we could
5092 * add an exception Proj to a new location then.
5093 * So, we must turn in into a tuple.
5099 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5100 switch (get_Proj_proj(proj)) {
5102 DBG_OPT_EXC_REM(proj);
5103 proj = get_Bound_mem(bound);
5105 case pn_Bound_X_except:
5106 DBG_OPT_EXC_REM(proj);
5107 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5111 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5113 case pn_Bound_X_regular:
5114 DBG_OPT_EXC_REM(proj);
5115 proj = new_r_Jmp(get_nodes_block(bound));
5125 * Does all optimizations on nodes that must be done on its Projs
5126 * because of creating new nodes.
5128 static ir_node *transform_node_Proj(ir_node *proj)
5130 ir_node *n = get_Proj_pred(proj);
5132 if (n->op->ops.transform_node_Proj)
5133 return n->op->ops.transform_node_Proj(proj);
5138 * Test whether a block is unreachable
5139 * Note: That this only returns true when
5140 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5141 * This is important, as you easily end up producing invalid constructs in the
5142 * unreachable code when optimizing away edges into the unreachable code.
5143 * So only set this flag when you iterate localopts to the fixpoint.
5144 * When you reach the fixpoint then all unreachable code is dead
5145 * (= can't be reached by firm edges) and you won't see the invalid constructs
5148 static bool is_block_unreachable(const ir_node *block)
5150 const ir_graph *irg = get_irn_irg(block);
5151 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5153 return get_Block_dom_depth(block) < 0;
5156 static ir_node *transform_node_Block(ir_node *block)
5158 ir_graph *irg = get_irn_irg(block);
5159 int arity = get_irn_arity(block);
5160 ir_node *bad = NULL;
5163 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5166 for (i = 0; i < arity; ++i) {
5167 ir_node *const pred = get_Block_cfgpred(block, i);
5168 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5171 bad = new_r_Bad(irg, mode_X);
5172 set_irn_n(block, i, bad);
5178 static ir_node *transform_node_Phi(ir_node *phi)
5180 int n = get_irn_arity(phi);
5181 ir_mode *mode = get_irn_mode(phi);
5182 ir_node *block = get_nodes_block(phi);
5183 ir_graph *irg = get_irn_irg(phi);
5184 ir_node *bad = NULL;
5187 /* Set phi-operands for bad-block inputs to bad */
5188 for (i = 0; i < n; ++i) {
5189 if (!is_Bad(get_Phi_pred(phi, i))) {
5190 ir_node *pred = get_Block_cfgpred(block, i);
5191 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5193 bad = new_r_Bad(irg, mode);
5194 set_irn_n(phi, i, bad);
5199 /* Move Pin nodes down through Phi nodes. */
5200 if (mode == mode_M) {
5201 n = get_irn_arity(phi);
5203 /* Beware of Phi0 */
5207 bool has_pin = false;
5209 NEW_ARR_A(ir_node *, in, n);
5211 for (i = 0; i < n; ++i) {
5212 ir_node *pred = get_irn_n(phi, i);
5215 in[i] = get_Pin_op(pred);
5217 } else if (is_Bad(pred)) {
5227 /* Move the Pin nodes "behind" the Phi. */
5228 block = get_irn_n(phi, -1);
5229 new_phi = new_r_Phi(block, n, in, mode_M);
5230 return new_r_Pin(block, new_phi);
5233 /* Move Confirms down through Phi nodes. */
5234 else if (mode_is_reference(mode)) {
5235 n = get_irn_arity(phi);
5237 /* Beware of Phi0 */
5239 ir_node *pred = get_irn_n(phi, 0);
5240 ir_node *bound, *new_phi, *block, **in;
5241 ir_relation relation;
5242 bool has_confirm = false;
5244 if (! is_Confirm(pred))
5247 bound = get_Confirm_bound(pred);
5248 relation = get_Confirm_relation(pred);
5250 NEW_ARR_A(ir_node *, in, n);
5251 in[0] = get_Confirm_value(pred);
5253 for (i = 1; i < n; ++i) {
5254 pred = get_irn_n(phi, i);
5256 if (is_Confirm(pred) &&
5257 get_Confirm_bound(pred) == bound &&
5258 get_Confirm_relation(pred) == relation) {
5259 in[i] = get_Confirm_value(pred);
5261 } else if (is_Bad(pred)) {
5271 /* move the Confirm nodes "behind" the Phi */
5272 block = get_irn_n(phi, -1);
5273 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5274 return new_r_Confirm(block, new_phi, bound, relation);
5281 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5283 * Should be moved to reassociation?
5285 static ir_node *transform_node_shift(ir_node *n)
5287 ir_node *left, *right;
5289 ir_mode *count_mode;
5290 ir_tarval *tv1, *tv2, *res;
5291 ir_node *in[2], *irn, *block;
5295 left = get_binop_left(n);
5297 /* different operations */
5298 if (get_irn_op(left) != get_irn_op(n))
5301 right = get_binop_right(n);
5302 tv1 = value_of(right);
5303 if (tv1 == tarval_bad)
5306 tv2 = value_of(get_binop_right(left));
5307 if (tv2 == tarval_bad)
5310 count_mode = get_tarval_mode(tv1);
5311 if (get_tarval_mode(tv2) != count_mode) {
5312 /* TODO: search bigger mode or something and convert... */
5316 mode = get_irn_mode(n);
5317 modulo_shf = get_mode_modulo_shift(mode);
5319 if (modulo_shf > 0) {
5320 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5322 /* I'm not so sure what happens in one complement... */
5323 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5324 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5325 * above will be invalid) */
5326 assert(modulo_shf<=0 || is_po2(modulo_shf));
5328 tv1 = tarval_and(tv1, modulo_mask);
5329 tv2 = tarval_and(tv2, modulo_mask);
5331 res = tarval_add(tv1, tv2);
5332 irg = get_irn_irg(n);
5334 /* beware: a simple replacement works only, if res < modulo shift */
5336 int bits = get_mode_size_bits(mode);
5337 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5338 res = tarval_mod(res, modulo);
5340 long bits = get_mode_size_bits(mode);
5341 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5343 /* shifting too much */
5344 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5346 ir_node *block = get_nodes_block(n);
5347 dbg_info *dbgi = get_irn_dbg_info(n);
5348 ir_mode *smode = get_irn_mode(right);
5349 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5350 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5353 return new_r_Const(irg, get_mode_null(mode));
5357 /* ok, we can replace it */
5358 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5359 block = get_nodes_block(n);
5361 in[0] = get_binop_left(left);
5362 in[1] = new_r_Const(irg, res);
5364 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5366 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5373 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5375 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5376 * (also with x >>s c1 when c1>=c2)
5378 static ir_node *transform_node_shl_shr(ir_node *n)
5381 ir_node *right = get_binop_right(n);
5391 ir_tarval *tv_shift;
5394 ir_relation relation;
5397 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5399 if (!is_Const(right))
5402 left = get_binop_left(n);
5403 mode = get_irn_mode(n);
5404 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5405 ir_node *shr_right = get_binop_right(left);
5407 if (!is_Const(shr_right))
5410 x = get_binop_left(left);
5411 tv_shr = get_Const_tarval(shr_right);
5412 tv_shl = get_Const_tarval(right);
5414 if (is_Shrs(left)) {
5415 /* shrs variant only allowed if c1 >= c2 */
5416 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5419 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5422 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5424 tv_mask = tarval_shl(tv_mask, tv_shl);
5425 } else if (is_Shr(n) && is_Shl(left)) {
5426 ir_node *shl_right = get_Shl_right(left);
5428 if (!is_Const(shl_right))
5431 x = get_Shl_left(left);
5432 tv_shr = get_Const_tarval(right);
5433 tv_shl = get_Const_tarval(shl_right);
5435 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5436 tv_mask = tarval_shr(tv_mask, tv_shr);
5441 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5442 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5445 assert(tv_mask != tarval_bad);
5446 assert(get_tarval_mode(tv_mask) == mode);
5448 block = get_nodes_block(n);
5449 irg = get_irn_irg(block);
5450 dbgi = get_irn_dbg_info(n);
5452 relation = tarval_cmp(tv_shl, tv_shr);
5453 if (relation == ir_relation_less || relation == ir_relation_equal) {
5454 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5455 new_const = new_r_Const(irg, tv_shift);
5457 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5459 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5462 assert(relation == ir_relation_greater);
5463 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5464 new_const = new_r_Const(irg, tv_shift);
5465 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5468 new_const = new_r_Const(irg, tv_mask);
5469 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5474 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5476 ir_mode *mode = get_tarval_mode(tv);
5477 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5478 return tarval_mod(tv, modulo_tv);
5481 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5482 ir_node *left, ir_node *right, ir_mode *mode);
5485 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5486 * then we can use that to minimize the value of Add(x, const) or
5487 * Sub(Const, x). In particular this often avoids 1 instruction in some
5488 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5489 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5491 static ir_node *transform_node_shift_modulo(ir_node *n,
5492 new_shift_func new_shift)
5494 ir_mode *mode = get_irn_mode(n);
5495 int modulo = get_mode_modulo_shift(mode);
5496 ir_node *newop = NULL;
5497 ir_mode *mode_right;
5504 if (get_mode_arithmetic(mode) != irma_twos_complement)
5506 if (!is_po2(modulo))
5509 irg = get_irn_irg(n);
5510 block = get_nodes_block(n);
5511 right = get_binop_right(n);
5512 mode_right = get_irn_mode(right);
5513 if (is_Const(right)) {
5514 ir_tarval *tv = get_Const_tarval(right);
5515 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5520 newop = new_r_Const(irg, tv_mod);
5521 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5522 ir_node *add_right = get_binop_right(right);
5523 if (is_Const(add_right)) {
5524 ir_tarval *tv = get_Const_tarval(add_right);
5525 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5530 newconst = new_r_Const(irg, tv_mod);
5531 newop = new_r_Add(block, get_binop_left(right), newconst,
5534 } else if (is_Sub(right)) {
5535 ir_node *sub_left = get_Sub_left(right);
5536 if (is_Const(sub_left)) {
5537 ir_tarval *tv = get_Const_tarval(sub_left);
5538 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5543 newconst = new_r_Const(irg, tv_mod);
5544 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5551 if (newop != NULL) {
5552 dbg_info *dbgi = get_irn_dbg_info(n);
5553 ir_node *left = get_binop_left(n);
5554 return new_shift(dbgi, block, left, newop, mode);
5562 static ir_node *transform_node_Shr(ir_node *n)
5564 ir_node *c, *oldn = n;
5565 ir_node *left = get_Shr_left(n);
5566 ir_node *right = get_Shr_right(n);
5567 ir_mode *mode = get_irn_mode(n);
5569 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5570 n = transform_node_shift(n);
5573 n = transform_node_shift_modulo(n, new_rd_Shr);
5575 n = transform_node_shl_shr(n);
5577 n = transform_node_shift_bitop(n);
5585 static ir_node *transform_node_Shrs(ir_node *n)
5588 ir_node *a = get_Shrs_left(n);
5589 ir_node *b = get_Shrs_right(n);
5590 ir_mode *mode = get_irn_mode(n);
5594 if (is_oversize_shift(n)) {
5595 ir_node *block = get_nodes_block(n);
5596 dbg_info *dbgi = get_irn_dbg_info(n);
5597 ir_mode *cmode = get_irn_mode(b);
5598 long val = get_mode_size_bits(cmode)-1;
5599 ir_graph *irg = get_irn_irg(n);
5600 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5601 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5604 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5605 n = transform_node_shift(n);
5609 n = transform_node_shift_modulo(n, new_rd_Shrs);
5612 n = transform_node_shift_bitop(n);
5616 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5617 attr = vrp_get_info(a);
5619 unsigned bits = get_mode_size_bits(mode);
5620 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5621 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5622 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5623 dbg_info *dbgi = get_irn_dbg_info(n);
5624 ir_node *block = get_nodes_block(n);
5625 return new_rd_Shr(dbgi, block, a, b, mode);
5635 static ir_node *transform_node_Shl(ir_node *n)
5637 ir_node *c, *oldn = n;
5638 ir_node *a = get_Shl_left(n);
5639 ir_node *b = get_Shl_right(n);
5640 ir_mode *mode = get_irn_mode(n);
5642 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5643 n = transform_node_shift(n);
5646 n = transform_node_shift_modulo(n, new_rd_Shl);
5648 n = transform_node_shl_shr(n);
5650 n = transform_node_shift_bitop(n);
5658 static ir_node *transform_node_Rotl(ir_node *n)
5660 ir_node *c, *oldn = n;
5661 ir_node *a = get_Rotl_left(n);
5662 ir_node *b = get_Rotl_right(n);
5663 ir_mode *mode = get_irn_mode(n);
5665 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5666 n = transform_node_shift(n);
5669 n = transform_node_shift_bitop(n);
5677 static ir_node *transform_node_Conv(ir_node *n)
5679 ir_node *c, *oldn = n;
5680 ir_mode *mode = get_irn_mode(n);
5681 ir_node *a = get_Conv_op(n);
5683 if (mode != mode_b && is_const_Phi(a)) {
5684 /* Do NOT optimize mode_b Conv's, this leads to remaining
5685 * Phib nodes later, because the conv_b_lower operation
5686 * is instantly reverted, when it tries to insert a Convb.
5688 c = apply_conv_on_phi(a, mode);
5690 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5695 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5696 ir_graph *irg = get_irn_irg(n);
5697 return new_r_Unknown(irg, mode);
5700 if (mode_is_reference(mode) &&
5701 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5703 ir_node *l = get_Add_left(a);
5704 ir_node *r = get_Add_right(a);
5705 dbg_info *dbgi = get_irn_dbg_info(a);
5706 ir_node *block = get_nodes_block(n);
5708 ir_node *lop = get_Conv_op(l);
5709 if (get_irn_mode(lop) == mode) {
5710 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5711 n = new_rd_Add(dbgi, block, lop, r, mode);
5716 ir_node *rop = get_Conv_op(r);
5717 if (get_irn_mode(rop) == mode) {
5718 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5719 n = new_rd_Add(dbgi, block, l, rop, mode);
5729 * Remove dead blocks and nodes in dead blocks
5730 * in keep alive list. We do not generate a new End node.
5732 static ir_node *transform_node_End(ir_node *n)
5734 int i, j, n_keepalives = get_End_n_keepalives(n);
5737 NEW_ARR_A(ir_node *, in, n_keepalives);
5739 for (i = j = 0; i < n_keepalives; ++i) {
5740 ir_node *ka = get_End_keepalive(n, i);
5742 /* no need to keep Bad */
5745 /* do not keep unreachable code */
5746 block = is_Block(ka) ? ka : get_nodes_block(ka);
5747 if (is_block_unreachable(block))
5751 if (j != n_keepalives)
5752 set_End_keepalives(n, j, in);
5756 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5758 if (is_Minus(a) && get_Minus_op(a) == b)
5760 if (is_Minus(b) && get_Minus_op(b) == a)
5762 if (is_Sub(a) && is_Sub(b)) {
5763 ir_node *a_left = get_Sub_left(a);
5764 ir_node *a_right = get_Sub_right(a);
5765 ir_node *b_left = get_Sub_left(b);
5766 ir_node *b_right = get_Sub_right(b);
5768 if (a_left == b_right && a_right == b_left)
5775 static const ir_node *skip_upconv(const ir_node *node)
5777 while (is_Conv(node)) {
5778 ir_mode *mode = get_irn_mode(node);
5779 const ir_node *op = get_Conv_op(node);
5780 ir_mode *op_mode = get_irn_mode(op);
5781 if (!smaller_mode(op_mode, mode))
5788 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5789 const ir_node *mux_true)
5794 ir_relation relation;
5800 * Note further that these optimization work even for floating point
5801 * with NaN's because -NaN == NaN.
5802 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5805 mode = get_irn_mode(mux_true);
5806 if (mode_honor_signed_zeros(mode))
5809 /* must be <, <=, >=, > */
5810 relation = get_Cmp_relation(sel);
5811 if ((relation & ir_relation_less_greater) == 0)
5814 if (!ir_is_negated_value(mux_true, mux_false))
5817 mux_true = skip_upconv(mux_true);
5818 mux_false = skip_upconv(mux_false);
5820 /* must be x cmp 0 */
5821 cmp_right = get_Cmp_right(sel);
5822 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5825 cmp_left = get_Cmp_left(sel);
5826 if (cmp_left == mux_false) {
5827 if (relation & ir_relation_less) {
5830 assert(relation & ir_relation_greater);
5833 } else if (cmp_left == mux_true) {
5834 if (relation & ir_relation_less) {
5837 assert(relation & ir_relation_greater);
5845 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5848 ir_node *cmp_left = get_Cmp_left(sel);
5849 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5852 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5853 const ir_node *mux_true)
5855 /* this code should return true each time transform_node_Mux would
5856 * optimize the Mux completely away */
5858 ir_mode *mode = get_irn_mode(mux_false);
5859 if (get_mode_arithmetic(mode) == irma_twos_complement
5860 && ir_mux_is_abs(sel, mux_false, mux_true))
5863 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5864 const ir_node *cmp_r = get_Cmp_right(sel);
5865 const ir_node *cmp_l = get_Cmp_left(sel);
5866 const ir_node *f = mux_false;
5867 const ir_node *t = mux_true;
5869 if (is_Const(t) && is_Const_null(t)) {
5874 if (is_And(cmp_l) && f == cmp_r) {
5875 ir_node *and_r = get_And_right(cmp_l);
5878 if (and_r == t && is_single_bit(and_r))
5880 and_l = get_And_left(cmp_l);
5881 if (and_l == t && is_single_bit(and_l))
5890 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5892 static ir_node *transform_Mux_set(ir_node *n)
5894 ir_node *cond = get_Mux_sel(n);
5899 ir_relation relation;
5913 left = get_Cmp_left(cond);
5914 mode = get_irn_mode(left);
5915 if (!mode_is_int(mode) && !mode_is_reference(mode))
5917 dest_mode = get_irn_mode(n);
5918 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5920 right = get_Cmp_right(cond);
5921 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5922 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5923 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5924 && relation != ir_relation_greater))
5929 case ir_relation_less:
5930 /* a < b -> (a - b) >> 31 */
5934 case ir_relation_less_equal:
5935 /* a <= b -> ~(a - b) >> 31 */
5940 case ir_relation_greater:
5941 /* a > b -> (b - a) >> 31 */
5945 case ir_relation_greater_equal:
5946 /* a >= b -> ~(a - b) >> 31 */
5955 dbgi = get_irn_dbg_info(n);
5956 block = get_nodes_block(n);
5957 irg = get_irn_irg(block);
5958 bits = get_mode_size_bits(dest_mode);
5959 tv = new_tarval_from_long(bits-1, mode_Iu);
5960 shift_cnt = new_rd_Const(dbgi, irg, tv);
5962 if (mode != dest_mode) {
5963 a = new_rd_Conv(dbgi, block, a, dest_mode);
5964 b = new_rd_Conv(dbgi, block, b, dest_mode);
5967 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5969 res = new_rd_Not(dbgi, block, res, dest_mode);
5971 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5976 * Optimize a Mux into some simpler cases.
5978 static ir_node *transform_node_Mux(ir_node *n)
5981 ir_node *sel = get_Mux_sel(n);
5982 ir_mode *mode = get_irn_mode(n);
5983 ir_node *t = get_Mux_true(n);
5984 ir_node *f = get_Mux_false(n);
5985 ir_graph *irg = get_irn_irg(n);
5987 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5988 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5989 int abs = ir_mux_is_abs(sel, f, t);
5991 dbg_info *dbgi = get_irn_dbg_info(n);
5992 ir_node *block = get_nodes_block(n);
5993 ir_node *op = ir_get_abs_op(sel, f, t);
5994 int bits = get_mode_size_bits(mode);
5995 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5996 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5997 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
6000 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
6002 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
6008 /* first normalization step: try to move a constant to the false side,
6009 * 0 preferred on false side too */
6010 if (is_Cmp(sel) && is_Const(t) &&
6011 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
6012 dbg_info *seldbgi = get_irn_dbg_info(sel);
6013 ir_node *block = get_nodes_block(sel);
6014 ir_relation relation = get_Cmp_relation(sel);
6019 /* Mux(x, a, b) => Mux(not(x), b, a) */
6020 relation = get_negated_relation(relation);
6021 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6022 get_Cmp_right(sel), relation);
6023 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6026 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6027 n = transform_Mux_set(n);
6032 /* the following optimisations create new mode_b nodes, so only do them
6033 * before mode_b lowering */
6034 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6036 ir_node* block = get_nodes_block(n);
6038 ir_node* c1 = get_Mux_sel(t);
6039 ir_node* t1 = get_Mux_true(t);
6040 ir_node* f1 = get_Mux_false(t);
6042 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6043 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6044 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6045 return new_r_Mux(block, and_, f1, t1, mode);
6046 } else if (f == t1) {
6047 /* Mux(cond0, Mux(cond1, x, y), x) */
6048 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6049 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6050 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6051 return new_r_Mux(block, and_, t1, f1, mode);
6053 } else if (is_Mux(f)) {
6054 ir_node* block = get_nodes_block(n);
6056 ir_node* c1 = get_Mux_sel(f);
6057 ir_node* t1 = get_Mux_true(f);
6058 ir_node* f1 = get_Mux_false(f);
6060 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6061 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6062 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6063 return new_r_Mux(block, or_, f1, t1, mode);
6064 } else if (t == f1) {
6065 /* Mux(cond0, x, Mux(cond1, y, x)) */
6066 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6067 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6068 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6069 return new_r_Mux(block, or_, t1, f1, mode);
6073 /* note: after normalization, false can only happen on default */
6074 if (mode == mode_b) {
6075 dbg_info *dbg = get_irn_dbg_info(n);
6076 ir_node *block = get_nodes_block(n);
6079 ir_tarval *tv_t = get_Const_tarval(t);
6080 if (tv_t == tarval_b_true) {
6082 /* Muxb(sel, true, false) = sel */
6083 assert(get_Const_tarval(f) == tarval_b_false);
6084 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6087 /* Muxb(sel, true, x) = Or(sel, x) */
6088 n = new_rd_Or(dbg, block, sel, f, mode_b);
6089 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6093 } else if (is_Const(f)) {
6094 ir_tarval *tv_f = get_Const_tarval(f);
6095 if (tv_f == tarval_b_true) {
6096 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6097 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6099 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6102 /* Muxb(sel, x, false) = And(sel, x) */
6103 assert(tv_f == tarval_b_false);
6104 n = new_rd_And(dbg, block, sel, t, mode_b);
6105 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6112 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6113 ir_relation relation = get_Cmp_relation(sel);
6114 ir_node *cmp_r = get_Cmp_right(sel);
6115 ir_node *cmp_l = get_Cmp_left(sel);
6116 ir_node *block = get_nodes_block(n);
6118 if (is_And(cmp_l) && f == cmp_r) {
6119 ir_node *and_r = get_And_right(cmp_l);
6122 if (and_r == t && is_single_bit(and_r)) {
6123 if (relation == ir_relation_equal) {
6124 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6125 n = new_rd_Eor(get_irn_dbg_info(n),
6126 block, cmp_l, t, mode);
6127 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6129 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6131 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6135 and_l = get_And_left(cmp_l);
6136 if (and_l == t && is_single_bit(and_l)) {
6137 if (relation == ir_relation_equal) {
6138 /* ((1 << n) & a) == 0, (1 << n), 0) */
6139 n = new_rd_Eor(get_irn_dbg_info(n),
6140 block, cmp_l, t, mode);
6141 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6143 /* ((1 << n) & a) != 0, (1 << n), 0) */
6145 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6156 * optimize Sync nodes that have other syncs as input we simply add the inputs
6157 * of the other sync to our own inputs
6159 static ir_node *transform_node_Sync(ir_node *n)
6161 int arity = get_Sync_n_preds(n);
6164 for (i = 0; i < arity;) {
6165 ir_node *pred = get_Sync_pred(n, i);
6169 /* Remove Bad predecessors */
6176 /* Remove duplicate predecessors */
6177 for (j = 0; j < i; ++j) {
6178 if (get_Sync_pred(n, j) == pred) {
6187 if (!is_Sync(pred)) {
6195 pred_arity = get_Sync_n_preds(pred);
6196 for (j = 0; j < pred_arity; ++j) {
6197 ir_node *pred_pred = get_Sync_pred(pred, j);
6202 add_irn_n(n, pred_pred);
6206 if (get_Sync_pred(n, k) == pred_pred) break;
6212 ir_graph *irg = get_irn_irg(n);
6213 return new_r_Bad(irg, mode_M);
6216 return get_Sync_pred(n, 0);
6219 /* rehash the sync node */
6224 static ir_node *transform_node_Load(ir_node *n)
6226 /* if our memory predecessor is a load from the same address, then reuse the
6227 * previous result */
6228 ir_node *mem = get_Load_mem(n);
6233 /* don't touch volatile loads */
6234 if (get_Load_volatility(n) == volatility_is_volatile)
6236 mem_pred = get_Proj_pred(mem);
6237 if (is_Load(mem_pred)) {
6238 ir_node *pred_load = mem_pred;
6240 /* conservatively compare the 2 loads. TODO: This could be less strict
6241 * with fixup code in some situations (like smaller/bigger modes) */
6242 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6244 if (get_Load_mode(pred_load) != get_Load_mode(n))
6246 /* all combinations of aligned/unaligned pred/n should be fine so we do
6247 * not compare the unaligned attribute */
6249 ir_node *block = get_nodes_block(n);
6250 ir_node *jmp = new_r_Jmp(block);
6251 ir_graph *irg = get_irn_irg(n);
6252 ir_node *bad = new_r_Bad(irg, mode_X);
6253 ir_mode *mode = get_Load_mode(n);
6254 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6255 ir_node *in[] = { mem, res, jmp, bad };
6256 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6259 } else if (is_Store(mem_pred)) {
6260 ir_node *pred_store = mem_pred;
6261 ir_node *value = get_Store_value(pred_store);
6263 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6265 if (get_irn_mode(value) != get_Load_mode(n))
6267 /* all combinations of aligned/unaligned pred/n should be fine so we do
6268 * not compare the unaligned attribute */
6270 ir_node *block = get_nodes_block(n);
6271 ir_node *jmp = new_r_Jmp(block);
6272 ir_graph *irg = get_irn_irg(n);
6273 ir_node *bad = new_r_Bad(irg, mode_X);
6274 ir_node *res = value;
6275 ir_node *in[] = { mem, res, jmp, bad };
6276 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6285 * optimize a trampoline Call into a direct Call
6287 static ir_node *transform_node_Call(ir_node *call)
6289 ir_node *callee = get_Call_ptr(call);
6290 ir_node *adr, *mem, *res, *bl, **in;
6291 ir_type *ctp, *mtp, *tp;
6295 size_t i, n_res, n_param;
6298 if (! is_Proj(callee))
6300 callee = get_Proj_pred(callee);
6301 if (! is_Builtin(callee))
6303 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6306 mem = get_Call_mem(call);
6308 if (skip_Proj(mem) == callee) {
6309 /* memory is routed to the trampoline, skip */
6310 mem = get_Builtin_mem(callee);
6313 /* build a new call type */
6314 mtp = get_Call_type(call);
6315 tdb = get_type_dbg_info(mtp);
6317 n_res = get_method_n_ress(mtp);
6318 n_param = get_method_n_params(mtp);
6319 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6321 for (i = 0; i < n_res; ++i)
6322 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6324 NEW_ARR_A(ir_node *, in, n_param + 1);
6326 /* FIXME: we don't need a new pointer type in every step */
6327 irg = get_irn_irg(call);
6328 tp = get_irg_frame_type(irg);
6329 tp = new_type_pointer(tp);
6330 set_method_param_type(ctp, 0, tp);
6332 in[0] = get_Builtin_param(callee, 2);
6333 for (i = 0; i < n_param; ++i) {
6334 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6335 in[i + 1] = get_Call_param(call, i);
6337 var = get_method_variadicity(mtp);
6338 set_method_variadicity(ctp, var);
6339 /* When we resolve a trampoline, the function must be called by a this-call */
6340 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6341 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6343 adr = get_Builtin_param(callee, 1);
6345 db = get_irn_dbg_info(call);
6346 bl = get_nodes_block(call);
6348 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6349 if (get_irn_pinned(call) == op_pin_state_floats)
6350 set_irn_pinned(res, op_pin_state_floats);
6355 * Sets the default transform node operation for an ir_op_ops.
6357 * @param code the opcode for the default operation
6358 * @param ops the operations initialized
6363 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6367 ops->transform_node = transform_node_##a; \
6369 #define CASE_PROJ(a) \
6371 ops->transform_node_Proj = transform_node_Proj_##a; \
6373 #define CASE_PROJ_EX(a) \
6375 ops->transform_node = transform_node_##a; \
6376 ops->transform_node_Proj = transform_node_Proj_##a; \
6421 * Tries several [inplace] [optimizing] transformations and returns an
6422 * equivalent node. The difference to equivalent_node() is that these
6423 * transformations _do_ generate new nodes, and thus the old node must
6424 * not be freed even if the equivalent node isn't the old one.
6426 static ir_node *transform_node(ir_node *n)
6432 iro = get_irn_opcode_(n);
6433 /* constant expression evaluation / constant folding */
6434 if (get_opt_constant_folding()) {
6435 /* neither constants nor Tuple values can be evaluated */
6436 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6437 /* try to evaluate */
6438 ir_tarval *tv = computed_value(n);
6439 if (tv != tarval_bad) {
6440 /* evaluation was successful -- replace the node. */
6441 ir_graph *irg = get_irn_irg(n);
6443 n = new_r_Const(irg, tv);
6445 DBG_OPT_CSTEVAL(old_n, n);
6451 /* remove unnecessary nodes */
6452 if (get_opt_constant_folding() ||
6453 (iro == iro_Phi) || /* always optimize these nodes. */
6454 (iro == iro_Id) || /* ... */
6455 (iro == iro_Proj) || /* ... */
6456 (iro == iro_Block)) { /* Flags tested local. */
6457 n = equivalent_node(n);
6462 /* Some more constant expression evaluation. */
6463 if (get_opt_algebraic_simplification() ||
6464 (iro == iro_Cond) ||
6465 (iro == iro_Proj)) { /* Flags tested local. */
6466 if (n->op->ops.transform_node != NULL) {
6467 n = n->op->ops.transform_node(n);
6477 /* **************** Common Subexpression Elimination **************** */
6479 /** The size of the hash table used, should estimate the number of nodes
6481 #define N_IR_NODES 512
6483 /** Compares two exception attributes */
6484 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6486 const except_attr *ea = &a->attr.except;
6487 const except_attr *eb = &b->attr.except;
6488 return ea->pin_state != eb->pin_state;
6491 /** Compares the attributes of two Const nodes. */
6492 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6494 return get_Const_tarval(a) != get_Const_tarval(b);
6497 /** Compares the attributes of two Proj nodes. */
6498 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6500 return a->attr.proj.proj != b->attr.proj.proj;
6503 /** Compares the attributes of two Alloc nodes. */
6504 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6506 const alloc_attr *pa = &a->attr.alloc;
6507 const alloc_attr *pb = &b->attr.alloc;
6508 if (pa->where != pb->where || pa->type != pb->type)
6510 return node_cmp_exception(a, b);
6513 /** Compares the attributes of two Free nodes. */
6514 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6516 const free_attr *pa = &a->attr.free;
6517 const free_attr *pb = &b->attr.free;
6518 return (pa->where != pb->where) || (pa->type != pb->type);
6521 /** Compares the attributes of two SymConst nodes. */
6522 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6524 const symconst_attr *pa = &a->attr.symc;
6525 const symconst_attr *pb = &b->attr.symc;
6526 return (pa->kind != pb->kind)
6527 || (pa->sym.type_p != pb->sym.type_p);
6530 /** Compares the attributes of two Call nodes. */
6531 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6533 const call_attr *pa = &a->attr.call;
6534 const call_attr *pb = &b->attr.call;
6535 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6537 return node_cmp_exception(a, b);
6540 /** Compares the attributes of two Sel nodes. */
6541 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6543 const ir_entity *a_ent = get_Sel_entity(a);
6544 const ir_entity *b_ent = get_Sel_entity(b);
6545 return a_ent != b_ent;
6548 /** Compares the attributes of two Phi nodes. */
6549 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6551 /* we can only enter this function if both nodes have the same number of inputs,
6552 hence it is enough to check if one of them is a Phi0 */
6554 /* check the Phi0 pos attribute */
6555 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6560 /** Compares the attributes of two Conv nodes. */
6561 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6563 return get_Conv_strict(a) != get_Conv_strict(b);
6566 /** Compares the attributes of two Cast nodes. */
6567 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6569 return get_Cast_type(a) != get_Cast_type(b);
6572 /** Compares the attributes of two Load nodes. */
6573 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6575 if (get_Load_volatility(a) == volatility_is_volatile ||
6576 get_Load_volatility(b) == volatility_is_volatile)
6577 /* NEVER do CSE on volatile Loads */
6579 /* do not CSE Loads with different alignment. Be conservative. */
6580 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6582 if (get_Load_mode(a) != get_Load_mode(b))
6584 return node_cmp_exception(a, b);
6587 /** Compares the attributes of two Store nodes. */
6588 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6590 /* do not CSE Stores with different alignment. Be conservative. */
6591 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6593 /* NEVER do CSE on volatile Stores */
6594 if (get_Store_volatility(a) == volatility_is_volatile ||
6595 get_Store_volatility(b) == volatility_is_volatile)
6597 return node_cmp_exception(a, b);
6600 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6602 if (get_CopyB_type(a) != get_CopyB_type(b))
6605 return node_cmp_exception(a, b);
6608 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6610 return node_cmp_exception(a, b);
6613 /** Compares the attributes of two Div nodes. */
6614 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6616 const div_attr *ma = &a->attr.div;
6617 const div_attr *mb = &b->attr.div;
6618 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6620 return node_cmp_exception(a, b);
6623 /** Compares the attributes of two Mod nodes. */
6624 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6626 const mod_attr *ma = &a->attr.mod;
6627 const mod_attr *mb = &b->attr.mod;
6628 if (ma->resmode != mb->resmode)
6630 return node_cmp_exception(a, b);
6633 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6635 const cmp_attr *ma = &a->attr.cmp;
6636 const cmp_attr *mb = &b->attr.cmp;
6637 return ma->relation != mb->relation;
6640 /** Compares the attributes of two Confirm nodes. */
6641 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6643 const confirm_attr *ma = &a->attr.confirm;
6644 const confirm_attr *mb = &b->attr.confirm;
6645 return ma->relation != mb->relation;
6648 /** Compares the attributes of two Builtin nodes. */
6649 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6651 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6653 if (get_Builtin_type(a) != get_Builtin_type(b))
6655 return node_cmp_exception(a, b);
6658 /** Compares the attributes of two ASM nodes. */
6659 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6662 const ir_asm_constraint *ca;
6663 const ir_asm_constraint *cb;
6666 if (get_ASM_text(a) != get_ASM_text(b))
6669 /* Should we really check the constraints here? Should be better, but is strange. */
6670 n = get_ASM_n_input_constraints(a);
6671 if (n != get_ASM_n_input_constraints(b))
6674 ca = get_ASM_input_constraints(a);
6675 cb = get_ASM_input_constraints(b);
6676 for (i = 0; i < n; ++i) {
6677 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6678 || ca[i].mode != cb[i].mode)
6682 n = get_ASM_n_output_constraints(a);
6683 if (n != get_ASM_n_output_constraints(b))
6686 ca = get_ASM_output_constraints(a);
6687 cb = get_ASM_output_constraints(b);
6688 for (i = 0; i < n; ++i) {
6689 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6690 || ca[i].mode != cb[i].mode)
6694 n = get_ASM_n_clobbers(a);
6695 if (n != get_ASM_n_clobbers(b))
6698 cla = get_ASM_clobbers(a);
6699 clb = get_ASM_clobbers(b);
6700 for (i = 0; i < n; ++i) {
6701 if (cla[i] != clb[i])
6705 return node_cmp_exception(a, b);
6708 /** Compares the inexistent attributes of two Dummy nodes. */
6709 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6713 /* Dummy nodes never equal by definition */
6717 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6719 if (get_InstOf_type(a) != get_InstOf_type(b))
6721 return node_cmp_exception(a, b);
6725 * Set the default node attribute compare operation for an ir_op_ops.
6727 * @param code the opcode for the default operation
6728 * @param ops the operations initialized
6733 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6737 ops->node_cmp_attr = node_cmp_attr_##a; \
6773 * Compare function for two nodes in the value table. Gets two
6774 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6776 int identities_cmp(const void *elt, const void *key)
6778 ir_node *a = (ir_node *)elt;
6779 ir_node *b = (ir_node *)key;
6782 if (a == b) return 0;
6784 if ((get_irn_op(a) != get_irn_op(b)) ||
6785 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6787 /* compare if a's in and b's in are of equal length */
6788 irn_arity_a = get_irn_arity(a);
6789 if (irn_arity_a != get_irn_arity(b))
6792 /* blocks are never the same */
6796 if (get_irn_pinned(a) == op_pin_state_pinned) {
6797 /* for pinned nodes, the block inputs must be equal */
6798 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6801 ir_node *block_a = get_nodes_block(a);
6802 ir_node *block_b = get_nodes_block(b);
6803 if (! get_opt_global_cse()) {
6804 /* for block-local CSE both nodes must be in the same Block */
6805 if (block_a != block_b)
6808 /* The optimistic approach would be to do nothing here.
6809 * However doing GCSE optimistically produces a lot of partially dead code which appears
6810 * to be worse in practice than the missed opportunities.
6811 * So we use a very conservative variant here and only CSE if 1 value dominates the
6813 if (!block_dominates(block_a, block_b)
6814 && !block_dominates(block_b, block_a))
6819 /* compare a->in[0..ins] with b->in[0..ins] */
6820 for (i = 0; i < irn_arity_a; ++i) {
6821 ir_node *pred_a = get_irn_n(a, i);
6822 ir_node *pred_b = get_irn_n(b, i);
6823 if (pred_a != pred_b) {
6824 /* if both predecessors are CSE neutral they might be different */
6825 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6831 * here, we already now that the nodes are identical except their
6834 if (a->op->ops.node_cmp_attr)
6835 return a->op->ops.node_cmp_attr(a, b);
6841 * Calculate a hash value of a node.
6843 * @param node The IR-node
6845 unsigned ir_node_hash(const ir_node *node)
6847 return node->op->ops.hash(node);
6851 void new_identities(ir_graph *irg)
6853 if (irg->value_table != NULL)
6854 del_pset(irg->value_table);
6855 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6858 void del_identities(ir_graph *irg)
6860 if (irg->value_table != NULL)
6861 del_pset(irg->value_table);
6864 /* Normalize a node by putting constants (and operands with larger
6865 * node index) on the right (operator side). */
6866 void ir_normalize_node(ir_node *n)
6868 if (is_op_commutative(get_irn_op(n))) {
6869 ir_node *l = get_binop_left(n);
6870 ir_node *r = get_binop_right(n);
6872 /* For commutative operators perform a OP b == b OP a but keep
6873 * constants on the RIGHT side. This helps greatly in some
6874 * optimizations. Moreover we use the idx number to make the form
6876 if (!operands_are_normalized(l, r)) {
6877 set_binop_left(n, r);
6878 set_binop_right(n, l);
6885 * Return the canonical node computing the same value as n.
6886 * Looks up the node in a hash table, enters it in the table
6887 * if it isn't there yet.
6889 * @param n the node to look up
6891 * @return a node that computes the same value as n or n if no such
6892 * node could be found
6894 ir_node *identify_remember(ir_node *n)
6896 ir_graph *irg = get_irn_irg(n);
6897 pset *value_table = irg->value_table;
6900 if (value_table == NULL)
6903 ir_normalize_node(n);
6904 /* lookup or insert in hash table with given hash key. */
6905 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6908 /* n is reachable again */
6909 edges_node_revival(nn);
6916 * During construction we set the op_pin_state_pinned flag in the graph right
6917 * when the optimization is performed. The flag turning on procedure global
6918 * cse could be changed between two allocations. This way we are safe.
6920 * @param n The node to lookup
6922 static inline ir_node *identify_cons(ir_node *n)
6926 n = identify_remember(n);
6927 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6928 ir_graph *irg = get_irn_irg(n);
6929 set_irg_pinned(irg, op_pin_state_floats);
6934 /* Add a node to the identities value table. */
6935 void add_identities(ir_node *node)
6942 identify_remember(node);
6945 /* Visit each node in the value table of a graph. */
6946 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6949 ir_graph *rem = current_ir_graph;
6951 current_ir_graph = irg;
6952 foreach_pset(irg->value_table, ir_node*, node) {
6955 current_ir_graph = rem;
6959 * These optimizations deallocate nodes from the obstack.
6960 * It can only be called if it is guaranteed that no other nodes
6961 * reference this one, i.e., right after construction of a node.
6963 * @param n The node to optimize
6965 ir_node *optimize_node(ir_node *n)
6968 ir_graph *irg = get_irn_irg(n);
6969 unsigned iro = get_irn_opcode(n);
6972 /* Always optimize Phi nodes: part of the construction. */
6973 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6975 /* constant expression evaluation / constant folding */
6976 if (get_opt_constant_folding()) {
6977 /* neither constants nor Tuple values can be evaluated */
6978 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6979 /* try to evaluate */
6980 tv = computed_value(n);
6981 if (tv != tarval_bad) {
6986 * we MUST copy the node here temporarily, because it's still
6987 * needed for DBG_OPT_CSTEVAL
6989 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6990 oldn = (ir_node*)alloca(node_size);
6992 memcpy(oldn, n, node_size);
6993 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6995 /* ARG, copy the in array, we need it for statistics */
6996 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6998 /* note the inplace edges module */
6999 edges_node_deleted(n);
7001 /* evaluation was successful -- replace the node. */
7002 irg_kill_node(irg, n);
7003 nw = new_r_Const(irg, tv);
7005 DBG_OPT_CSTEVAL(oldn, nw);
7011 /* remove unnecessary nodes */
7012 if (get_opt_algebraic_simplification() ||
7013 (iro == iro_Phi) || /* always optimize these nodes. */
7015 (iro == iro_Proj) ||
7016 (iro == iro_Block) ) /* Flags tested local. */
7017 n = equivalent_node(n);
7019 /* Common Subexpression Elimination.
7021 * Checks whether n is already available.
7022 * The block input is used to distinguish different subexpressions. Right
7023 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
7024 * subexpressions within a block.
7027 n = identify_cons(n);
7030 edges_node_deleted(oldn);
7032 /* We found an existing, better node, so we can deallocate the old node. */
7033 irg_kill_node(irg, oldn);
7037 /* Some more constant expression evaluation that does not allow to
7039 iro = get_irn_opcode(n);
7040 if (get_opt_algebraic_simplification() ||
7041 (iro == iro_Cond) ||
7042 (iro == iro_Proj)) { /* Flags tested local. */
7043 n = transform_node(n);
7046 /* Now we have a legal, useful node. Enter it in hash table for CSE */
7047 if (get_opt_cse()) {
7049 n = identify_remember(o);
7059 * These optimizations never deallocate nodes (in place). This can cause dead
7060 * nodes lying on the obstack. Remove these by a dead node elimination,
7061 * i.e., a copying garbage collection.
7063 ir_node *optimize_in_place_2(ir_node *n)
7065 if (!get_opt_optimize() && !is_Phi(n)) return n;
7070 /** common subexpression elimination **/
7071 /* Checks whether n is already available. */
7072 /* The block input is used to distinguish different subexpressions.
7073 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
7074 * only finds common subexpressions within a block. */
7075 if (get_opt_cse()) {
7077 n = identify_remember(n);
7080 /* we have another existing node now, we do not optimize it here */
7085 n = transform_node(n);
7087 /* Now we can verify the node, as it has no dead inputs any more. */
7090 /* Now we have a legal, useful node. Enter it in hash table for cse.
7091 * Blocks should be unique anyways. (Except the successor of start:
7092 * is cse with the start block!)
7094 * Note: This is only necessary because some of the optimisations
7095 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7096 * bad practice and should be fixed sometime.
7098 if (get_opt_cse()) {
7100 n = identify_remember(o);
7109 * Wrapper for external use, set proper status bits after optimization.
7111 ir_node *optimize_in_place(ir_node *n)
7113 ir_graph *irg = get_irn_irg(n);
7114 /* Handle graph state */
7115 assert(get_irg_phase_state(irg) != phase_building);
7117 if (get_opt_global_cse())
7118 set_irg_pinned(irg, op_pin_state_floats);
7120 /* FIXME: Maybe we could also test whether optimizing the node can
7121 change the control graph. */
7122 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7123 return optimize_in_place_2(n);
7127 * Calculate a hash value of a Const node.
7129 static unsigned hash_Const(const ir_node *node)
7133 /* special value for const, as they only differ in their tarval. */
7134 h = HASH_PTR(node->attr.con.tarval);
7140 * Calculate a hash value of a SymConst node.
7142 static unsigned hash_SymConst(const ir_node *node)
7146 /* all others are pointers */
7147 h = HASH_PTR(node->attr.symc.sym.type_p);
7153 * Set the default hash operation in an ir_op_ops.
7155 * @param code the opcode for the default operation
7156 * @param ops the operations initialized
7161 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
7165 ops->hash = hash_##a; \
7168 /* hash function already set */
7169 if (ops->hash != NULL)
7176 /* use input/mode default hash if no function was given */
7177 ops->hash = firm_default_hash;
7185 * Sets the default operation for an ir_ops.
7187 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
7189 ops = firm_set_default_hash(code, ops);
7190 ops = firm_set_default_computed_value(code, ops);
7191 ops = firm_set_default_equivalent_node(code, ops);
7192 ops = firm_set_default_transform_node(code, ops);
7193 ops = firm_set_default_node_cmp_attr(code, ops);
7194 ops = firm_set_default_get_type_attr(code, ops);
7195 ops = firm_set_default_get_entity_attr(code, ops);