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
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
39 #include "iroptimize.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
60 * Returns the tarval of a Const node or tarval_bad for all other nodes.
62 static ir_tarval *default_value_of(const ir_node *n)
65 return get_Const_tarval(n); /* might return tarval_bad */
70 value_of_func value_of_ptr = default_value_of;
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static ir_tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
87 } /* computed_value_Const */
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static ir_tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
118 } /* computed_value_SymConst */
121 * Return the value of an Add.
123 static ir_tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 ir_tarval *ta = value_of(a);
129 ir_tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 if ((is_Not(a) && get_Not_op(a) == b)
136 || (is_Not(b) && get_Not_op(b) == a)) {
137 return get_mode_all_one(get_irn_mode(n));
141 } /* computed_value_Add */
144 * Return the value of a Sub.
145 * Special case: a - a
147 static ir_tarval *computed_value_Sub(const ir_node *n)
149 ir_mode *mode = get_irn_mode(n);
150 ir_node *a = get_Sub_left(n);
151 ir_node *b = get_Sub_right(n);
156 if (! mode_is_float(mode)) {
159 return get_mode_null(mode);
165 if ((ta != tarval_bad) && (tb != tarval_bad))
166 return tarval_sub(ta, tb, mode);
169 } /* computed_value_Sub */
172 * Return the value of a Carry.
173 * Special : a op 0, 0 op b
175 static ir_tarval *computed_value_Carry(const ir_node *n)
177 ir_node *a = get_binop_left(n);
178 ir_node *b = get_binop_right(n);
179 ir_mode *m = get_irn_mode(n);
180 ir_tarval *ta = value_of(a);
181 ir_tarval *tb = value_of(b);
183 if ((ta != tarval_bad) && (tb != tarval_bad)) {
185 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
187 if (tarval_is_null(ta) || tarval_is_null(tb))
188 return get_mode_null(m);
191 } /* computed_value_Carry */
194 * Return the value of a Borrow.
197 static ir_tarval *computed_value_Borrow(const ir_node *n)
199 ir_node *a = get_binop_left(n);
200 ir_node *b = get_binop_right(n);
201 ir_mode *m = get_irn_mode(n);
202 ir_tarval *ta = value_of(a);
203 ir_tarval *tb = value_of(b);
205 if ((ta != tarval_bad) && (tb != tarval_bad)) {
206 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
207 } else if (tarval_is_null(ta)) {
208 return get_mode_null(m);
211 } /* computed_value_Borrow */
214 * Return the value of an unary Minus.
216 static ir_tarval *computed_value_Minus(const ir_node *n)
218 ir_node *a = get_Minus_op(n);
219 ir_tarval *ta = value_of(a);
221 if (ta != tarval_bad)
222 return tarval_neg(ta);
225 } /* computed_value_Minus */
228 * Return the value of a Mul.
230 static ir_tarval *computed_value_Mul(const ir_node *n)
232 ir_node *a = get_Mul_left(n);
233 ir_node *b = get_Mul_right(n);
234 ir_tarval *ta = value_of(a);
235 ir_tarval *tb = value_of(b);
238 mode = get_irn_mode(n);
239 if (mode != get_irn_mode(a)) {
240 /* n * n = 2n bit multiplication */
241 ta = tarval_convert_to(ta, mode);
242 tb = tarval_convert_to(tb, mode);
245 if (ta != tarval_bad && tb != tarval_bad) {
246 return tarval_mul(ta, tb);
248 /* a * 0 != 0 if a == NaN or a == Inf */
249 if (!mode_is_float(mode)) {
250 /* a*0 = 0 or 0*b = 0 */
251 if (ta == get_mode_null(mode))
253 if (tb == get_mode_null(mode))
258 } /* computed_value_Mul */
261 * Return the value of an And.
262 * Special case: a & 0, 0 & b
264 static ir_tarval *computed_value_And(const ir_node *n)
266 ir_node *a = get_And_left(n);
267 ir_node *b = get_And_right(n);
268 ir_tarval *ta = value_of(a);
269 ir_tarval *tb = value_of(b);
271 if ((ta != tarval_bad) && (tb != tarval_bad)) {
272 return tarval_and (ta, tb);
275 if (tarval_is_null(ta)) return ta;
276 if (tarval_is_null(tb)) return tb;
279 if ((is_Not(a) && get_Not_op(a) == b)
280 || (is_Not(b) && get_Not_op(b) == a)) {
281 return get_mode_null(get_irn_mode(n));
285 } /* computed_value_And */
288 * Return the value of an Or.
289 * Special case: a | 1...1, 1...1 | b
291 static ir_tarval *computed_value_Or(const ir_node *n)
293 ir_node *a = get_Or_left(n);
294 ir_node *b = get_Or_right(n);
295 ir_tarval *ta = value_of(a);
296 ir_tarval *tb = value_of(b);
298 if ((ta != tarval_bad) && (tb != tarval_bad)) {
299 return tarval_or (ta, tb);
302 if (tarval_is_all_one(ta)) return ta;
303 if (tarval_is_all_one(tb)) return tb;
306 if ((is_Not(a) && get_Not_op(a) == b)
307 || (is_Not(b) && get_Not_op(b) == a)) {
308 return get_mode_all_one(get_irn_mode(n));
311 } /* computed_value_Or */
314 * Return the value of an Eor.
316 static ir_tarval *computed_value_Eor(const ir_node *n)
318 ir_node *a = get_Eor_left(n);
319 ir_node *b = get_Eor_right(n);
324 return get_mode_null(get_irn_mode(n));
326 if ((is_Not(a) && get_Not_op(a) == b)
327 || (is_Not(b) && get_Not_op(b) == a)) {
328 return get_mode_all_one(get_irn_mode(n));
334 if ((ta != tarval_bad) && (tb != tarval_bad)) {
335 return tarval_eor(ta, tb);
338 } /* computed_value_Eor */
341 * Return the value of a Not.
343 static ir_tarval *computed_value_Not(const ir_node *n)
345 ir_node *a = get_Not_op(n);
346 ir_tarval *ta = value_of(a);
348 if (ta != tarval_bad)
349 return tarval_not(ta);
352 } /* computed_value_Not */
355 * Tests whether a shift shifts more bits than available in the mode
357 static bool is_oversize_shift(const ir_node *n)
359 ir_node *count = get_binop_right(n);
360 ir_mode *mode = get_irn_mode(n);
361 ir_tarval *tv = value_of(count);
364 if (tv == tarval_bad)
366 if (!tarval_is_long(tv))
368 shiftval = get_tarval_long(tv);
369 modulo_shift = get_mode_modulo_shift(mode);
370 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
373 return shiftval >= (long)get_mode_size_bits(mode);
377 * Return the value of a Shl.
379 static ir_tarval *computed_value_Shl(const ir_node *n)
381 ir_node *a = get_Shl_left(n);
382 ir_node *b = get_Shl_right(n);
384 ir_tarval *ta = value_of(a);
385 ir_tarval *tb = value_of(b);
387 if ((ta != tarval_bad) && (tb != tarval_bad)) {
388 return tarval_shl(ta, tb);
391 if (is_oversize_shift(n))
392 return get_mode_null(get_irn_mode(n));
395 } /* computed_value_Shl */
398 * Return the value of a Shr.
400 static ir_tarval *computed_value_Shr(const ir_node *n)
402 ir_node *a = get_Shr_left(n);
403 ir_node *b = get_Shr_right(n);
405 ir_tarval *ta = value_of(a);
406 ir_tarval *tb = value_of(b);
408 if ((ta != tarval_bad) && (tb != tarval_bad)) {
409 return tarval_shr(ta, tb);
411 if (is_oversize_shift(n))
412 return get_mode_null(get_irn_mode(n));
415 } /* computed_value_Shr */
418 * Return the value of a Shrs.
420 static ir_tarval *computed_value_Shrs(const ir_node *n)
422 ir_node *a = get_Shrs_left(n);
423 ir_node *b = get_Shrs_right(n);
425 ir_tarval *ta = value_of(a);
426 ir_tarval *tb = value_of(b);
428 if ((ta != tarval_bad) && (tb != tarval_bad)) {
429 return tarval_shrs(ta, tb);
432 } /* computed_value_Shrs */
435 * Return the value of a Rotl.
437 static ir_tarval *computed_value_Rotl(const ir_node *n)
439 ir_node *a = get_Rotl_left(n);
440 ir_node *b = get_Rotl_right(n);
442 ir_tarval *ta = value_of(a);
443 ir_tarval *tb = value_of(b);
445 if ((ta != tarval_bad) && (tb != tarval_bad)) {
446 return tarval_rotl(ta, tb);
449 } /* computed_value_Rotl */
451 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
453 ir_mode *mode = get_irn_mode(node);
454 if (get_mode_arithmetic(mode) != irma_twos_complement
455 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
459 ir_node *count = get_Shl_right(node);
460 if (is_Const(count)) {
461 ir_tarval *tv = get_Const_tarval(count);
462 if (tarval_is_long(tv)) {
463 long shiftval = get_tarval_long(tv);
464 long destbits = get_mode_size_bits(dest_mode);
465 if (shiftval >= destbits
466 && shiftval < (long)get_mode_modulo_shift(mode))
472 ir_node *right = get_And_right(node);
473 if (is_Const(right)) {
474 ir_tarval *tv = get_Const_tarval(right);
475 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
476 return tarval_is_null(conved);
483 * Return the value of a Conv.
485 static ir_tarval *computed_value_Conv(const ir_node *n)
487 ir_node *a = get_Conv_op(n);
488 ir_tarval *ta = value_of(a);
489 ir_mode *mode = get_irn_mode(n);
491 if (ta != tarval_bad)
492 return tarval_convert_to(ta, get_irn_mode(n));
494 if (ir_zero_when_converted(a, mode))
495 return get_mode_null(mode);
498 } /* computed_value_Conv */
501 * Calculate the value of a Mux: can be evaluated, if the
502 * sel and the right input are known.
504 static ir_tarval *computed_value_Mux(const ir_node *n)
506 ir_node *sel = get_Mux_sel(n);
507 ir_tarval *ts = value_of(sel);
509 if (ts == get_tarval_b_true()) {
510 ir_node *v = get_Mux_true(n);
513 else if (ts == get_tarval_b_false()) {
514 ir_node *v = get_Mux_false(n);
518 } /* computed_value_Mux */
521 * Calculate the value of a Confirm: can be evaluated,
522 * if it has the form Confirm(x, '=', Const).
524 static ir_tarval *computed_value_Confirm(const ir_node *n)
526 if (get_Confirm_relation(n) == ir_relation_equal) {
527 ir_tarval *tv = value_of(get_Confirm_bound(n));
528 if (tv != tarval_bad)
531 return value_of(get_Confirm_value(n));
532 } /* computed_value_Confirm */
535 * gives a (conservative) estimation of possible relation when comparing
538 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
539 const ir_node *right)
541 ir_relation possible = ir_relation_true;
542 ir_tarval *tv_l = value_of(left);
543 ir_tarval *tv_r = value_of(right);
544 ir_mode *mode = get_irn_mode(left);
545 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
546 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
548 /* both values known - evaluate them */
549 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
550 possible = tarval_cmp(tv_l, tv_r);
551 /* we can return now, won't get any better */
554 /* a == a is never less or greater (but might be equal or unordered) */
556 possible &= ~ir_relation_less_greater;
557 /* unordered results only happen for float compares */
558 if (!mode_is_float(mode))
559 possible &= ~ir_relation_unordered;
560 /* values can never be less than the least representable number or
561 * greater than the greatest representable number */
563 possible &= ~ir_relation_greater;
565 possible &= ~ir_relation_less;
567 possible &= ~ir_relation_greater;
569 possible &= ~ir_relation_less;
570 /* maybe vrp can tell us more */
571 possible &= vrp_cmp(left, right);
572 /* Alloc nodes never return null (but throw an exception) */
573 if (is_Alloc(left) && tarval_is_null(tv_r))
574 possible &= ~ir_relation_equal;
580 * Return the value of a Cmp.
582 * The basic idea here is to determine which relations are possible and which
583 * one are definitely impossible.
585 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
587 ir_node *left = get_Cmp_left(cmp);
588 ir_node *right = get_Cmp_right(cmp);
589 ir_relation possible = ir_get_possible_cmp_relations(left, right);
590 ir_relation relation = get_Cmp_relation(cmp);
592 /* if none of the requested relations is possible, return false */
593 if ((possible & relation) == ir_relation_false)
594 return tarval_b_false;
595 /* if possible relations are a subset of the requested ones return true */
596 if ((possible & ~relation) == ir_relation_false)
597 return tarval_b_true;
599 return computed_value_Cmp_Confirm(cmp, left, right, relation);
603 * Calculate the value of an integer Div.
604 * Special case: 0 / b
606 static ir_tarval *do_computed_value_Div(const ir_node *div)
608 const ir_node *a = get_Div_left(div);
609 const ir_node *b = get_Div_right(div);
610 const ir_mode *mode = get_Div_resmode(div);
611 ir_tarval *ta = value_of(a);
613 const ir_node *dummy;
615 /* cannot optimize 0 / b = 0 because of NaN */
616 if (!mode_is_float(mode)) {
617 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
618 return ta; /* 0 / b == 0 if b != 0 */
621 if (ta != tarval_bad && tb != tarval_bad)
622 return tarval_div(ta, tb);
624 } /* do_computed_value_Div */
627 * Calculate the value of an integer Mod of two nodes.
628 * Special case: a % 1
630 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
632 ir_tarval *ta = value_of(a);
633 ir_tarval *tb = value_of(b);
635 /* Compute a % 1 or c1 % c2 */
636 if (tarval_is_one(tb))
637 return get_mode_null(get_irn_mode(a));
638 if (ta != tarval_bad && tb != tarval_bad)
639 return tarval_mod(ta, tb);
641 } /* do_computed_value_Mod */
644 * Return the value of a Proj(Div).
646 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
648 long proj_nr = get_Proj_proj(n);
649 if (proj_nr != pn_Div_res)
652 return do_computed_value_Div(get_Proj_pred(n));
653 } /* computed_value_Proj_Div */
656 * Return the value of a Proj(Mod).
658 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
660 long proj_nr = get_Proj_proj(n);
662 if (proj_nr == pn_Mod_res) {
663 const ir_node *mod = get_Proj_pred(n);
664 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
667 } /* computed_value_Proj_Mod */
670 * Return the value of a Proj.
672 static ir_tarval *computed_value_Proj(const ir_node *proj)
674 ir_node *n = get_Proj_pred(proj);
676 if (n->op->ops.computed_value_Proj != NULL)
677 return n->op->ops.computed_value_Proj(proj);
679 } /* computed_value_Proj */
682 * If the parameter n can be computed, return its value, else tarval_bad.
683 * Performs constant folding.
685 * @param n The node this should be evaluated
687 ir_tarval *computed_value(const ir_node *n)
689 vrp_attr *vrp = vrp_get_info(n);
690 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
691 return vrp->bits_set;
693 if (n->op->ops.computed_value)
694 return n->op->ops.computed_value(n);
696 } /* computed_value */
699 * Set the default computed_value evaluator in an ir_op_ops.
701 * @param code the opcode for the default operation
702 * @param ops the operations initialized
707 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
711 ops->computed_value = computed_value_##a; \
713 #define CASE_PROJ(a) \
715 ops->computed_value_Proj = computed_value_Proj_##a; \
750 } /* firm_set_default_computed_value */
753 * Optimize operations that are commutative and have neutral 0,
754 * so a op 0 = 0 op a = a.
756 static ir_node *equivalent_node_neutral_zero(ir_node *n)
760 ir_node *a = get_binop_left(n);
761 ir_node *b = get_binop_right(n);
766 /* After running compute_node there is only one constant predecessor.
767 Find this predecessors value and remember the other node: */
768 if ((tv = value_of(a)) != tarval_bad) {
770 } else if ((tv = value_of(b)) != tarval_bad) {
775 /* If this predecessors constant value is zero, the operation is
776 * unnecessary. Remove it.
778 * Beware: If n is a Add, the mode of on and n might be different
779 * which happens in this rare construction: NULL + 3.
780 * Then, a Conv would be needed which we cannot include here.
782 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
785 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
789 } /* equivalent_node_neutral_zero */
792 * Eor is commutative and has neutral 0.
794 static ir_node *equivalent_node_Eor(ir_node *n)
800 n = equivalent_node_neutral_zero(n);
801 if (n != oldn) return n;
804 b = get_Eor_right(n);
807 ir_node *aa = get_Eor_left(a);
808 ir_node *ab = get_Eor_right(a);
811 /* (a ^ b) ^ a -> b */
813 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
815 } else if (ab == b) {
816 /* (a ^ b) ^ b -> a */
818 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
823 ir_node *ba = get_Eor_left(b);
824 ir_node *bb = get_Eor_right(b);
827 /* a ^ (a ^ b) -> b */
829 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
831 } else if (bb == a) {
832 /* a ^ (b ^ a) -> b */
834 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
842 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
844 * The second one looks strange, but this construct
845 * is used heavily in the LCC sources :-).
847 * Beware: The Mode of an Add may be different than the mode of its
848 * predecessors, so we could not return a predecessors in all cases.
850 static ir_node *equivalent_node_Add(ir_node *n)
853 ir_node *left, *right;
854 ir_mode *mode = get_irn_mode(n);
856 n = equivalent_node_neutral_zero(n);
860 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
861 if (mode_is_float(mode)) {
862 ir_graph *irg = get_irn_irg(n);
863 if (get_irg_fp_model(irg) & fp_strict_algebraic)
867 left = get_Add_left(n);
868 right = get_Add_right(n);
871 if (get_Sub_right(left) == right) {
874 n = get_Sub_left(left);
875 if (mode == get_irn_mode(n)) {
876 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
882 if (get_Sub_right(right) == left) {
885 n = get_Sub_left(right);
886 if (mode == get_irn_mode(n)) {
887 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
893 } /* equivalent_node_Add */
896 * optimize operations that are not commutative but have neutral 0 on left,
899 static ir_node *equivalent_node_left_zero(ir_node *n)
903 ir_node *a = get_binop_left(n);
904 ir_node *b = get_binop_right(n);
905 ir_tarval *tb = value_of(b);
907 if (tarval_is_null(tb)) {
910 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
913 } /* equivalent_node_left_zero */
915 #define equivalent_node_Shl equivalent_node_left_zero
916 #define equivalent_node_Shr equivalent_node_left_zero
917 #define equivalent_node_Shrs equivalent_node_left_zero
918 #define equivalent_node_Rotl equivalent_node_left_zero
921 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
923 * The second one looks strange, but this construct
924 * is used heavily in the LCC sources :-).
926 * Beware: The Mode of a Sub may be different than the mode of its
927 * predecessors, so we could not return a predecessors in all cases.
929 static ir_node *equivalent_node_Sub(ir_node *n)
933 ir_mode *mode = get_irn_mode(n);
936 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
937 if (mode_is_float(mode)) {
938 ir_graph *irg = get_irn_irg(n);
939 if (get_irg_fp_model(irg) & fp_strict_algebraic)
943 b = get_Sub_right(n);
946 /* Beware: modes might be different */
947 if (tarval_is_null(tb)) {
948 ir_node *a = get_Sub_left(n);
949 if (mode == get_irn_mode(a)) {
952 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
956 } /* equivalent_node_Sub */
960 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
963 * -(-a) == a, but might overflow two times.
964 * We handle it anyway here but the better way would be a
965 * flag. This would be needed for Pascal for instance.
967 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
970 ir_node *pred = get_unop_op(n);
972 /* optimize symmetric unop */
973 if (get_irn_op(pred) == get_irn_op(n)) {
974 n = get_unop_op(pred);
975 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
978 } /* equivalent_node_idempotent_unop */
980 /** Optimize Not(Not(x)) == x. */
981 #define equivalent_node_Not equivalent_node_idempotent_unop
983 /** -(-x) == x ??? Is this possible or can --x raise an
984 out of bounds exception if min =! max? */
985 #define equivalent_node_Minus equivalent_node_idempotent_unop
988 * Optimize a * 1 = 1 * a = a.
990 static ir_node *equivalent_node_Mul(ir_node *n)
993 ir_node *a = get_Mul_left(n);
995 /* we can handle here only the n * n = n bit cases */
996 if (get_irn_mode(n) == get_irn_mode(a)) {
997 ir_node *b = get_Mul_right(n);
1001 * Mul is commutative and has again an other neutral element.
1002 * Constants are place right, so check this case first.
1005 if (tarval_is_one(tv)) {
1007 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1010 if (tarval_is_one(tv)) {
1012 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1017 } /* equivalent_node_Mul */
1020 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1022 static ir_node *equivalent_node_Or(ir_node *n)
1026 ir_node *a = get_Or_left(n);
1027 ir_node *b = get_Or_right(n);
1031 n = a; /* idempotence */
1032 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1035 /* constants are normalized to right, check this side first */
1037 if (tarval_is_null(tv)) {
1039 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1043 if (tarval_is_null(tv)) {
1045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1050 } /* equivalent_node_Or */
1053 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1055 static ir_node *equivalent_node_And(ir_node *n)
1059 ir_node *a = get_And_left(n);
1060 ir_node *b = get_And_right(n);
1064 n = a; /* idempotence */
1065 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1068 /* constants are normalized to right, check this side first */
1070 if (tarval_is_all_one(tv)) {
1072 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1075 if (tv != get_tarval_bad()) {
1076 ir_mode *mode = get_irn_mode(n);
1077 if (!mode_is_signed(mode) && is_Conv(a)) {
1078 ir_node *convop = get_Conv_op(a);
1079 ir_mode *convopmode = get_irn_mode(convop);
1080 if (!mode_is_signed(convopmode)) {
1081 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1082 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1084 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1091 if (tarval_is_all_one(tv)) {
1093 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1097 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1100 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1105 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1113 } /* equivalent_node_And */
1116 * Try to remove useless Conv's:
1118 static ir_node *equivalent_node_Conv(ir_node *n)
1121 ir_node *a = get_Conv_op(n);
1123 ir_mode *n_mode = get_irn_mode(n);
1124 ir_mode *a_mode = get_irn_mode(a);
1127 if (n_mode == a_mode) { /* No Conv necessary */
1128 if (get_Conv_strict(n)) {
1131 /* neither Minus nor Confirm change the precision,
1132 so we can "look-through" */
1135 p = get_Minus_op(p);
1136 } else if (is_Confirm(p)) {
1137 p = get_Confirm_value(p);
1143 if (is_Conv(p) && get_Conv_strict(p)) {
1144 /* we known already, that a_mode == n_mode, and neither
1145 Minus change the mode, so the second Conv
1147 assert(get_irn_mode(p) == n_mode);
1149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1153 ir_node *pred = get_Proj_pred(p);
1154 if (is_Load(pred)) {
1155 /* Loads always return with the exact precision of n_mode */
1156 assert(get_Load_mode(pred) == n_mode);
1158 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1161 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1162 pred = get_Proj_pred(pred);
1163 if (is_Start(pred)) {
1164 /* Arguments always return with the exact precision,
1165 as strictConv's are place before Call -- if the
1166 caller was compiled with the same setting.
1167 Otherwise, the semantics is probably still right. */
1168 assert(get_irn_mode(p) == n_mode);
1170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1176 /* special case: the immediate predecessor is also a Conv */
1177 if (! get_Conv_strict(a)) {
1178 /* first one is not strict, kick it */
1180 a_mode = get_irn_mode(a);
1184 /* else both are strict conv, second is superfluous */
1186 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1191 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1194 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1195 ir_node *b = get_Conv_op(a);
1196 ir_mode *b_mode = get_irn_mode(b);
1198 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1199 /* both are strict conv */
1200 if (smaller_mode(a_mode, n_mode)) {
1201 /* both are strict, but the first is smaller, so
1202 the second cannot remove more precision, remove the
1204 set_Conv_strict(n, 0);
1207 if (n_mode == b_mode) {
1208 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1209 if (n_mode == mode_b) {
1210 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1211 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1213 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1214 if (values_in_mode(b_mode, a_mode)) {
1215 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1216 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1221 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1222 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1223 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1224 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1226 if (float_mantissa >= int_mantissa) {
1228 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1233 if (smaller_mode(b_mode, a_mode)) {
1234 if (get_Conv_strict(n))
1235 set_Conv_strict(b, 1);
1236 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1237 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1244 } /* equivalent_node_Conv */
1247 * - fold Phi-nodes, iff they have only one predecessor except
1250 static ir_node *equivalent_node_Phi(ir_node *n)
1255 ir_node *first_val = NULL; /* to shutup gcc */
1257 if (!get_opt_optimize() &&
1258 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1261 n_preds = get_Phi_n_preds(n);
1263 /* Phi of dead Region without predecessors. */
1267 /* Find first non-self-referencing input */
1268 for (i = 0; i < n_preds; ++i) {
1269 first_val = get_Phi_pred(n, i);
1270 /* not self pointer */
1271 if (first_val != n) {
1272 /* then found first value. */
1277 /* search for rest of inputs, determine if any of these
1278 are non-self-referencing */
1279 while (++i < n_preds) {
1280 ir_node *scnd_val = get_Phi_pred(n, i);
1281 if (scnd_val != n && scnd_val != first_val) {
1286 if (i >= n_preds && !is_Dummy(first_val)) {
1287 /* Fold, if no multiple distinct non-self-referencing inputs */
1289 DBG_OPT_PHI(oldn, n);
1292 } /* equivalent_node_Phi */
1295 * Optimize Proj(Tuple).
1297 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1299 ir_node *oldn = proj;
1300 ir_node *tuple = get_Proj_pred(proj);
1302 /* Remove the Tuple/Proj combination. */
1303 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1304 DBG_OPT_TUPLE(oldn, tuple, proj);
1307 } /* equivalent_node_Proj_Tuple */
1310 * Optimize a / 1 = a.
1312 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1314 ir_node *oldn = proj;
1315 ir_node *div = get_Proj_pred(proj);
1316 ir_node *b = get_Div_right(div);
1317 ir_tarval *tb = value_of(b);
1319 /* Div is not commutative. */
1320 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1321 switch (get_Proj_proj(proj)) {
1323 proj = get_Div_mem(div);
1324 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1328 proj = get_Div_left(div);
1329 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1333 /* we cannot replace the exception Proj's here, this is done in
1334 transform_node_Proj_Div() */
1339 } /* equivalent_node_Proj_Div */
1342 * Optimize CopyB(mem, x, x) into a Nop.
1344 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1346 ir_node *oldn = proj;
1347 ir_node *copyb = get_Proj_pred(proj);
1348 ir_node *a = get_CopyB_dst(copyb);
1349 ir_node *b = get_CopyB_src(copyb);
1352 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1353 switch (get_Proj_proj(proj)) {
1355 proj = get_CopyB_mem(copyb);
1356 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1361 } /* equivalent_node_Proj_CopyB */
1364 * Optimize Bounds(idx, idx, upper) into idx.
1366 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1368 ir_node *oldn = proj;
1369 ir_node *bound = get_Proj_pred(proj);
1370 ir_node *idx = get_Bound_index(bound);
1371 ir_node *pred = skip_Proj(idx);
1374 if (idx == get_Bound_lower(bound))
1376 else if (is_Bound(pred)) {
1378 * idx was Bounds checked previously, it is still valid if
1379 * lower <= pred_lower && pred_upper <= upper.
1381 ir_node *lower = get_Bound_lower(bound);
1382 ir_node *upper = get_Bound_upper(bound);
1383 if (get_Bound_lower(pred) == lower &&
1384 get_Bound_upper(pred) == upper) {
1386 * One could expect that we simply return the previous
1387 * Bound here. However, this would be wrong, as we could
1388 * add an exception Proj to a new location then.
1389 * So, we must turn in into a tuple.
1395 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1396 switch (get_Proj_proj(proj)) {
1398 DBG_OPT_EXC_REM(proj);
1399 proj = get_Bound_mem(bound);
1403 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1406 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1411 } /* equivalent_node_Proj_Bound */
1414 * Does all optimizations on nodes that must be done on its Projs
1415 * because of creating new nodes.
1417 static ir_node *equivalent_node_Proj(ir_node *proj)
1419 ir_node *n = get_Proj_pred(proj);
1420 if (n->op->ops.equivalent_node_Proj)
1421 return n->op->ops.equivalent_node_Proj(proj);
1423 } /* equivalent_node_Proj */
1428 static ir_node *equivalent_node_Id(ir_node *n)
1436 DBG_OPT_ID(oldn, n);
1438 } /* equivalent_node_Id */
1443 static ir_node *equivalent_node_Mux(ir_node *n)
1445 ir_node *oldn = n, *sel = get_Mux_sel(n);
1447 ir_tarval *ts = value_of(sel);
1449 /* Mux(true, f, t) == t */
1450 if (ts == tarval_b_true) {
1451 n = get_Mux_true(n);
1452 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1455 /* Mux(false, f, t) == f */
1456 if (ts == tarval_b_false) {
1457 n = get_Mux_false(n);
1458 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1461 n_t = get_Mux_true(n);
1462 n_f = get_Mux_false(n);
1464 /* Mux(v, x, T) == x */
1465 if (is_Unknown(n_f)) {
1467 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1470 /* Mux(v, T, x) == x */
1471 if (is_Unknown(n_t)) {
1473 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1477 /* Mux(v, x, x) == x */
1480 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1483 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1484 ir_relation relation = get_Cmp_relation(sel);
1485 ir_node *f = get_Mux_false(n);
1486 ir_node *t = get_Mux_true(n);
1489 * Note further that these optimization work even for floating point
1490 * with NaN's because -NaN == NaN.
1491 * However, if +0 and -0 is handled differently, we cannot use the first one.
1493 ir_node *const cmp_l = get_Cmp_left(sel);
1494 ir_node *const cmp_r = get_Cmp_right(sel);
1497 case ir_relation_equal:
1498 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1499 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1506 case ir_relation_less_greater:
1507 case ir_relation_unordered_less_greater:
1508 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1509 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1511 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1520 * Note: normalization puts the constant on the right side,
1521 * so we check only one case.
1523 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1524 /* Mux(t CMP 0, X, t) */
1525 if (is_Minus(f) && get_Minus_op(f) == t) {
1526 /* Mux(t CMP 0, -t, t) */
1527 if (relation == ir_relation_equal) {
1528 /* Mux(t == 0, -t, t) ==> -t */
1530 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1531 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1532 /* Mux(t != 0, -t, t) ==> t */
1534 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1544 * Remove Confirm nodes if setting is on.
1545 * Replace Confirms(x, '=', Constlike) by Constlike.
1547 static ir_node *equivalent_node_Confirm(ir_node *n)
1549 ir_node *pred = get_Confirm_value(n);
1550 ir_relation relation = get_Confirm_relation(n);
1552 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1554 * rare case: two identical Confirms one after another,
1555 * replace the second one with the first.
1558 pred = get_Confirm_value(n);
1564 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1565 * perform no actual computation, as, e.g., the Id nodes. It does not create
1566 * new nodes. It is therefore safe to free n if the node returned is not n.
1567 * If a node returns a Tuple we can not just skip it. If the size of the
1568 * in array fits, we transform n into a tuple (e.g., Div).
1570 ir_node *equivalent_node(ir_node *n)
1572 if (n->op->ops.equivalent_node)
1573 return n->op->ops.equivalent_node(n);
1575 } /* equivalent_node */
1578 * Sets the default equivalent node operation for an ir_op_ops.
1580 * @param code the opcode for the default operation
1581 * @param ops the operations initialized
1586 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1590 ops->equivalent_node = equivalent_node_##a; \
1592 #define CASE_PROJ(a) \
1594 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1628 } /* firm_set_default_equivalent_node */
1631 * Returns non-zero if a node is a Phi node
1632 * with all predecessors constant.
1634 static int is_const_Phi(ir_node *n)
1638 if (! is_Phi(n) || get_irn_arity(n) == 0)
1640 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1641 if (! is_Const(get_irn_n(n, i)))
1645 } /* is_const_Phi */
1647 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1648 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1651 * in reality eval_func should be tarval (*eval_func)() but incomplete
1652 * declarations are bad style and generate noisy warnings
1654 typedef void (*eval_func)(void);
1657 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1659 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1661 if (eval == (eval_func) tarval_sub) {
1662 tarval_sub_type func = (tarval_sub_type)eval;
1664 return func(a, b, mode);
1666 tarval_binop_type func = (tarval_binop_type)eval;
1673 * Apply an evaluator on a binop with a constant operators (and one Phi).
1675 * @param phi the Phi node
1676 * @param other the other operand
1677 * @param eval an evaluator function
1678 * @param mode the mode of the result, may be different from the mode of the Phi!
1679 * @param left if non-zero, other is the left operand, else the right
1681 * @return a new Phi node if the conversion was successful, NULL else
1683 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1689 int i, n = get_irn_arity(phi);
1691 NEW_ARR_A(void *, res, n);
1693 for (i = 0; i < n; ++i) {
1694 pred = get_irn_n(phi, i);
1695 tv = get_Const_tarval(pred);
1696 tv = do_eval(eval, other, tv, mode);
1698 if (tv == tarval_bad) {
1699 /* folding failed, bad */
1705 for (i = 0; i < n; ++i) {
1706 pred = get_irn_n(phi, i);
1707 tv = get_Const_tarval(pred);
1708 tv = do_eval(eval, tv, other, mode);
1710 if (tv == tarval_bad) {
1711 /* folding failed, bad */
1717 irg = get_irn_irg(phi);
1718 for (i = 0; i < n; ++i) {
1719 pred = get_irn_n(phi, i);
1720 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1722 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1723 } /* apply_binop_on_phi */
1726 * Apply an evaluator on a binop with two constant Phi.
1728 * @param a the left Phi node
1729 * @param b the right Phi node
1730 * @param eval an evaluator function
1731 * @param mode the mode of the result, may be different from the mode of the Phi!
1733 * @return a new Phi node if the conversion was successful, NULL else
1735 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1737 ir_tarval *tv_l, *tv_r, *tv;
1743 if (get_nodes_block(a) != get_nodes_block(b))
1746 n = get_irn_arity(a);
1747 NEW_ARR_A(void *, res, n);
1749 for (i = 0; i < n; ++i) {
1750 pred = get_irn_n(a, i);
1751 tv_l = get_Const_tarval(pred);
1752 pred = get_irn_n(b, i);
1753 tv_r = get_Const_tarval(pred);
1754 tv = do_eval(eval, tv_l, tv_r, mode);
1756 if (tv == tarval_bad) {
1757 /* folding failed, bad */
1762 irg = get_irn_irg(a);
1763 for (i = 0; i < n; ++i) {
1764 pred = get_irn_n(a, i);
1765 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1767 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1768 } /* apply_binop_on_2_phis */
1771 * Apply an evaluator on a unop with a constant operator (a Phi).
1773 * @param phi the Phi node
1774 * @param eval an evaluator function
1776 * @return a new Phi node if the conversion was successful, NULL else
1778 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1785 int i, n = get_irn_arity(phi);
1787 NEW_ARR_A(void *, res, n);
1788 for (i = 0; i < n; ++i) {
1789 pred = get_irn_n(phi, i);
1790 tv = get_Const_tarval(pred);
1793 if (tv == tarval_bad) {
1794 /* folding failed, bad */
1799 mode = get_irn_mode(phi);
1800 irg = get_irn_irg(phi);
1801 for (i = 0; i < n; ++i) {
1802 pred = get_irn_n(phi, i);
1803 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1805 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1806 } /* apply_unop_on_phi */
1809 * Apply a conversion on a constant operator (a Phi).
1811 * @param phi the Phi node
1813 * @return a new Phi node if the conversion was successful, NULL else
1815 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1821 int i, n = get_irn_arity(phi);
1823 NEW_ARR_A(void *, res, n);
1824 for (i = 0; i < n; ++i) {
1825 pred = get_irn_n(phi, i);
1826 tv = get_Const_tarval(pred);
1827 tv = tarval_convert_to(tv, mode);
1829 if (tv == tarval_bad) {
1830 /* folding failed, bad */
1835 irg = get_irn_irg(phi);
1836 for (i = 0; i < n; ++i) {
1837 pred = get_irn_n(phi, i);
1838 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1840 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1841 } /* apply_conv_on_phi */
1844 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1845 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1846 * If possible, remove the Conv's.
1848 static ir_node *transform_node_AddSub(ir_node *n)
1850 ir_mode *mode = get_irn_mode(n);
1852 if (mode_is_reference(mode)) {
1853 ir_node *left = get_binop_left(n);
1854 ir_node *right = get_binop_right(n);
1855 unsigned ref_bits = get_mode_size_bits(mode);
1857 if (is_Conv(left)) {
1858 ir_mode *lmode = get_irn_mode(left);
1859 unsigned bits = get_mode_size_bits(lmode);
1861 if (ref_bits == bits &&
1862 mode_is_int(lmode) &&
1863 get_mode_arithmetic(lmode) == irma_twos_complement) {
1864 ir_node *pre = get_Conv_op(left);
1865 ir_mode *pre_mode = get_irn_mode(pre);
1867 if (mode_is_int(pre_mode) &&
1868 get_mode_size_bits(pre_mode) == bits &&
1869 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1870 /* ok, this conv just changes to sign, moreover the calculation
1871 * is done with same number of bits as our address mode, so
1872 * we can ignore the conv as address calculation can be viewed
1873 * as either signed or unsigned
1875 set_binop_left(n, pre);
1880 if (is_Conv(right)) {
1881 ir_mode *rmode = get_irn_mode(right);
1882 unsigned bits = get_mode_size_bits(rmode);
1884 if (ref_bits == bits &&
1885 mode_is_int(rmode) &&
1886 get_mode_arithmetic(rmode) == irma_twos_complement) {
1887 ir_node *pre = get_Conv_op(right);
1888 ir_mode *pre_mode = get_irn_mode(pre);
1890 if (mode_is_int(pre_mode) &&
1891 get_mode_size_bits(pre_mode) == bits &&
1892 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1893 /* ok, this conv just changes to sign, moreover the calculation
1894 * is done with same number of bits as our address mode, so
1895 * we can ignore the conv as address calculation can be viewed
1896 * as either signed or unsigned
1898 set_binop_right(n, pre);
1903 /* let address arithmetic use unsigned modes */
1904 if (is_Const(right)) {
1905 ir_mode *rmode = get_irn_mode(right);
1907 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1908 /* convert a AddP(P, *s) into AddP(P, *u) */
1909 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1911 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1912 set_binop_right(n, pre);
1918 } /* transform_node_AddSub */
1920 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1923 if (is_Const(b) && is_const_Phi(a)) { \
1924 /* check for Op(Phi, Const) */ \
1925 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1927 else if (is_Const(a) && is_const_Phi(b)) { \
1928 /* check for Op(Const, Phi) */ \
1929 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1931 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1932 /* check for Op(Phi, Phi) */ \
1933 c = apply_binop_on_2_phis(a, b, eval, mode); \
1936 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1941 #define HANDLE_UNOP_PHI(eval, a, c) \
1944 if (is_const_Phi(a)) { \
1945 /* check for Op(Phi) */ \
1946 c = apply_unop_on_phi(a, eval); \
1948 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1955 * Do the AddSub optimization, then Transform
1956 * Constant folding on Phi
1957 * Add(a,a) -> Mul(a, 2)
1958 * Add(Mul(a, x), a) -> Mul(a, x+1)
1959 * if the mode is integer or float.
1960 * Transform Add(a,-b) into Sub(a,b).
1961 * Reassociation might fold this further.
1963 static ir_node *transform_node_Add(ir_node *n)
1966 ir_node *a, *b, *c, *oldn = n;
1967 vrp_attr *a_vrp, *b_vrp;
1969 n = transform_node_AddSub(n);
1971 a = get_Add_left(n);
1972 b = get_Add_right(n);
1974 mode = get_irn_mode(n);
1976 if (mode_is_reference(mode)) {
1977 ir_mode *lmode = get_irn_mode(a);
1979 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1980 /* an Add(a, NULL) is a hidden Conv */
1981 dbg_info *dbg = get_irn_dbg_info(n);
1982 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
1986 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
1988 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1989 if (mode_is_float(mode)) {
1990 ir_graph *irg = get_irn_irg(n);
1991 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1995 if (mode_is_num(mode)) {
1996 ir_graph *irg = get_irn_irg(n);
1997 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1998 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
1999 && a == b && mode_is_int(mode)) {
2000 ir_node *block = get_nodes_block(n);
2003 get_irn_dbg_info(n),
2006 new_r_Const_long(irg, mode, 2),
2008 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2013 get_irn_dbg_info(n),
2018 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2023 get_irn_dbg_info(n),
2028 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2031 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2032 /* Here we rely on constants be on the RIGHT side */
2034 ir_node *op = get_Not_op(a);
2036 if (is_Const(b) && is_Const_one(b)) {
2038 ir_node *blk = get_nodes_block(n);
2039 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2040 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2047 a_vrp = vrp_get_info(a);
2048 b_vrp = vrp_get_info(b);
2050 if (a_vrp && b_vrp) {
2051 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2053 if (tarval_is_null(vrp_val)) {
2054 dbg_info *dbgi = get_irn_dbg_info(n);
2055 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2059 } /* transform_node_Add */
2062 * returns -cnst or NULL if impossible
2064 static ir_node *const_negate(ir_node *cnst)
2066 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2067 dbg_info *dbgi = get_irn_dbg_info(cnst);
2068 ir_graph *irg = get_irn_irg(cnst);
2069 if (tv == tarval_bad) return NULL;
2070 return new_rd_Const(dbgi, irg, tv);
2074 * Do the AddSub optimization, then Transform
2075 * Constant folding on Phi
2076 * Sub(0,a) -> Minus(a)
2077 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2078 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2079 * Sub(Add(a, x), x) -> a
2080 * Sub(x, Add(x, a)) -> -a
2081 * Sub(x, Const) -> Add(x, -Const)
2083 static ir_node *transform_node_Sub(ir_node *n)
2089 n = transform_node_AddSub(n);
2091 a = get_Sub_left(n);
2092 b = get_Sub_right(n);
2094 mode = get_irn_mode(n);
2096 if (mode_is_int(mode)) {
2097 ir_mode *lmode = get_irn_mode(a);
2099 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2100 /* a Sub(a, NULL) is a hidden Conv */
2101 dbg_info *dbg = get_irn_dbg_info(n);
2102 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2103 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2107 if (mode == lmode &&
2108 get_mode_arithmetic(mode) == irma_twos_complement &&
2110 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2112 dbg_info *dbg = get_irn_dbg_info(n);
2113 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2114 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2120 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2122 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2123 if (mode_is_float(mode)) {
2124 ir_graph *irg = get_irn_irg(n);
2125 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2129 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2130 /* a - C -> a + (-C) */
2131 ir_node *cnst = const_negate(b);
2133 ir_node *block = get_nodes_block(n);
2134 dbg_info *dbgi = get_irn_dbg_info(n);
2136 n = new_rd_Add(dbgi, block, a, cnst, mode);
2137 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2142 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2143 dbg_info *dbg = get_irn_dbg_info(n);
2144 ir_node *block = get_nodes_block(n);
2145 ir_node *left = get_Minus_op(a);
2146 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2148 n = new_rd_Minus(dbg, block, add, mode);
2149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2151 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2152 dbg_info *dbg = get_irn_dbg_info(n);
2153 ir_node *block = get_nodes_block(n);
2154 ir_node *right = get_Minus_op(b);
2156 n = new_rd_Add(dbg, block, a, right, mode);
2157 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2159 } else if (is_Sub(b)) {
2160 /* a - (b - c) -> a + (c - b)
2161 * -> (a - b) + c iff (b - c) is a pointer */
2162 dbg_info *s_dbg = get_irn_dbg_info(b);
2163 ir_node *s_left = get_Sub_left(b);
2164 ir_node *s_right = get_Sub_right(b);
2165 ir_mode *s_mode = get_irn_mode(b);
2166 if (mode_is_reference(s_mode)) {
2167 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2168 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2169 dbg_info *a_dbg = get_irn_dbg_info(n);
2172 s_right = new_r_Conv(lowest_block, s_right, mode);
2173 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2175 ir_node *s_block = get_nodes_block(b);
2176 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2177 dbg_info *a_dbg = get_irn_dbg_info(n);
2178 ir_node *a_block = get_nodes_block(n);
2180 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2182 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2185 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2186 ir_node *m_right = get_Mul_right(b);
2187 if (is_Const(m_right)) {
2188 ir_node *cnst2 = const_negate(m_right);
2189 if (cnst2 != NULL) {
2190 dbg_info *m_dbg = get_irn_dbg_info(b);
2191 ir_node *m_block = get_nodes_block(b);
2192 ir_node *m_left = get_Mul_left(b);
2193 ir_mode *m_mode = get_irn_mode(b);
2194 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2195 dbg_info *a_dbg = get_irn_dbg_info(n);
2196 ir_node *a_block = get_nodes_block(n);
2198 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2199 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2206 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2207 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2209 get_irn_dbg_info(n),
2213 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2217 if (mode_wrap_around(mode)) {
2218 ir_node *left = get_Add_left(a);
2219 ir_node *right = get_Add_right(a);
2221 /* FIXME: Does the Conv's work only for two complement or generally? */
2223 if (mode != get_irn_mode(right)) {
2224 /* This Sub is an effective Cast */
2225 right = new_r_Conv(get_nodes_block(n), right, mode);
2228 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2230 } else if (right == b) {
2231 if (mode != get_irn_mode(left)) {
2232 /* This Sub is an effective Cast */
2233 left = new_r_Conv(get_nodes_block(n), left, mode);
2236 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2242 if (mode_wrap_around(mode)) {
2243 ir_node *left = get_Add_left(b);
2244 ir_node *right = get_Add_right(b);
2246 /* FIXME: Does the Conv's work only for two complement or generally? */
2248 ir_mode *r_mode = get_irn_mode(right);
2250 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2251 if (mode != r_mode) {
2252 /* This Sub is an effective Cast */
2253 n = new_r_Conv(get_nodes_block(n), n, mode);
2255 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2257 } else if (right == a) {
2258 ir_mode *l_mode = get_irn_mode(left);
2260 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2261 if (mode != l_mode) {
2262 /* This Sub is an effective Cast */
2263 n = new_r_Conv(get_nodes_block(n), n, mode);
2265 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2270 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2271 ir_mode *mode = get_irn_mode(a);
2273 if (mode == get_irn_mode(b)) {
2275 ir_node *op_a = get_Conv_op(a);
2276 ir_node *op_b = get_Conv_op(b);
2278 /* check if it's allowed to skip the conv */
2279 ma = get_irn_mode(op_a);
2280 mb = get_irn_mode(op_b);
2282 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2283 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2286 set_Sub_right(n, b);
2292 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2293 if (!is_reassoc_running() && is_Mul(a)) {
2294 ir_node *ma = get_Mul_left(a);
2295 ir_node *mb = get_Mul_right(a);
2298 ir_node *blk = get_nodes_block(n);
2299 ir_graph *irg = get_irn_irg(n);
2301 get_irn_dbg_info(n),
2305 get_irn_dbg_info(n),
2308 new_r_Const(irg, get_mode_one(mode)),
2311 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2313 } else if (mb == b) {
2314 ir_node *blk = get_nodes_block(n);
2315 ir_graph *irg = get_irn_irg(n);
2317 get_irn_dbg_info(n),
2321 get_irn_dbg_info(n),
2324 new_r_Const(irg, get_mode_one(mode)),
2327 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2331 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2332 ir_node *x = get_Sub_left(a);
2333 ir_node *y = get_Sub_right(a);
2334 ir_node *blk = get_nodes_block(n);
2335 ir_mode *m_b = get_irn_mode(b);
2336 ir_mode *m_y = get_irn_mode(y);
2340 /* Determine the right mode for the Add. */
2343 else if (mode_is_reference(m_b))
2345 else if (mode_is_reference(m_y))
2349 * Both modes are different but none is reference,
2350 * happens for instance in SubP(SubP(P, Iu), Is).
2351 * We have two possibilities here: Cast or ignore.
2352 * Currently we ignore this case.
2357 add = new_r_Add(blk, y, b, add_mode);
2359 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2360 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2364 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2365 /* c - ~X = X + (c+1) */
2366 if (is_Const(a) && is_Not(b)) {
2367 ir_tarval *tv = get_Const_tarval(a);
2369 tv = tarval_add(tv, get_mode_one(mode));
2370 if (tv != tarval_bad) {
2371 ir_node *blk = get_nodes_block(n);
2372 ir_graph *irg = get_irn_irg(n);
2373 ir_node *c = new_r_Const(irg, tv);
2374 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2375 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2379 /* x-(x&y) = x & ~y */
2381 ir_node *and_left = get_And_left(b);
2382 ir_node *and_right = get_And_right(b);
2383 if (and_right == a) {
2384 ir_node *tmp = and_left;
2385 and_left = and_right;
2388 if (and_left == a) {
2389 dbg_info *dbgi = get_irn_dbg_info(n);
2390 ir_node *block = get_nodes_block(n);
2391 ir_mode *mode = get_irn_mode(n);
2392 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2393 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
2399 } /* transform_node_Sub */
2402 * Several transformation done on n*n=2n bits mul.
2403 * These transformations must be done here because new nodes may be produced.
2405 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2408 ir_node *a = get_Mul_left(n);
2409 ir_node *b = get_Mul_right(n);
2410 ir_tarval *ta = value_of(a);
2411 ir_tarval *tb = value_of(b);
2412 ir_mode *smode = get_irn_mode(a);
2414 if (ta == get_mode_one(smode)) {
2415 /* (L)1 * (L)b = (L)b */
2416 ir_node *blk = get_nodes_block(n);
2417 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2418 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2421 else if (ta == get_mode_minus_one(smode)) {
2422 /* (L)-1 * (L)b = (L)b */
2423 ir_node *blk = get_nodes_block(n);
2424 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2425 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2429 if (tb == get_mode_one(smode)) {
2430 /* (L)a * (L)1 = (L)a */
2431 ir_node *blk = get_irn_n(a, -1);
2432 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2433 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2436 else if (tb == get_mode_minus_one(smode)) {
2437 /* (L)a * (L)-1 = (L)-a */
2438 ir_node *blk = get_nodes_block(n);
2439 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2440 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2441 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2448 * Transform Mul(a,-1) into -a.
2449 * Do constant evaluation of Phi nodes.
2450 * Do architecture dependent optimizations on Mul nodes
2452 static ir_node *transform_node_Mul(ir_node *n)
2454 ir_node *c, *oldn = n;
2455 ir_mode *mode = get_irn_mode(n);
2456 ir_node *a = get_Mul_left(n);
2457 ir_node *b = get_Mul_right(n);
2459 if (is_Bad(a) || is_Bad(b))
2462 if (mode != get_irn_mode(a))
2463 return transform_node_Mul2n(n, mode);
2465 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2467 if (mode_is_signed(mode)) {
2470 if (value_of(a) == get_mode_minus_one(mode))
2472 else if (value_of(b) == get_mode_minus_one(mode))
2475 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2476 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2481 if (is_Const(b)) { /* (-a) * const -> a * -const */
2482 ir_node *cnst = const_negate(b);
2484 dbg_info *dbgi = get_irn_dbg_info(n);
2485 ir_node *block = get_nodes_block(n);
2486 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2487 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2490 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2491 dbg_info *dbgi = get_irn_dbg_info(n);
2492 ir_node *block = get_nodes_block(n);
2493 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2494 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2496 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2497 ir_node *sub_l = get_Sub_left(b);
2498 ir_node *sub_r = get_Sub_right(b);
2499 dbg_info *dbgi = get_irn_dbg_info(n);
2500 ir_node *block = get_nodes_block(n);
2501 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2502 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2503 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2506 } else if (is_Minus(b)) {
2507 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2508 ir_node *sub_l = get_Sub_left(a);
2509 ir_node *sub_r = get_Sub_right(a);
2510 dbg_info *dbgi = get_irn_dbg_info(n);
2511 ir_node *block = get_nodes_block(n);
2512 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2513 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2514 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2517 } else if (is_Shl(a)) {
2518 ir_node *const shl_l = get_Shl_left(a);
2519 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2520 /* (1 << x) * b -> b << x */
2521 dbg_info *const dbgi = get_irn_dbg_info(n);
2522 ir_node *const block = get_nodes_block(n);
2523 ir_node *const shl_r = get_Shl_right(a);
2524 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2525 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2528 } else if (is_Shl(b)) {
2529 ir_node *const shl_l = get_Shl_left(b);
2530 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2531 /* a * (1 << x) -> a << x */
2532 dbg_info *const dbgi = get_irn_dbg_info(n);
2533 ir_node *const block = get_nodes_block(n);
2534 ir_node *const shl_r = get_Shl_right(b);
2535 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2536 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2540 if (get_mode_arithmetic(mode) == irma_ieee754) {
2542 ir_tarval *tv = get_Const_tarval(a);
2543 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2544 && !tarval_is_negative(tv)) {
2545 /* 2.0 * b = b + b */
2546 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2547 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2551 else if (is_Const(b)) {
2552 ir_tarval *tv = get_Const_tarval(b);
2553 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2554 && !tarval_is_negative(tv)) {
2555 /* a * 2.0 = a + a */
2556 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2557 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2562 return arch_dep_replace_mul_with_shifts(n);
2563 } /* transform_node_Mul */
2566 * Transform a Div Node.
2568 static ir_node *transform_node_Div(ir_node *n)
2570 ir_mode *mode = get_Div_resmode(n);
2571 ir_node *a = get_Div_left(n);
2572 ir_node *b = get_Div_right(n);
2574 const ir_node *dummy;
2576 if (mode_is_int(mode)) {
2577 if (is_Const(b) && is_const_Phi(a)) {
2578 /* check for Div(Phi, Const) */
2579 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2581 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2584 } else if (is_Const(a) && is_const_Phi(b)) {
2585 /* check for Div(Const, Phi) */
2586 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2588 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2591 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2592 /* check for Div(Phi, Phi) */
2593 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2595 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2600 if (a == b && value_not_zero(a, &dummy)) {
2601 ir_graph *irg = get_irn_irg(n);
2602 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2603 value = new_r_Const(irg, get_mode_one(mode));
2604 DBG_OPT_CSTEVAL(n, value);
2607 if (mode_is_signed(mode) && is_Const(b)) {
2608 ir_tarval *tv = get_Const_tarval(b);
2610 if (tv == get_mode_minus_one(mode)) {
2612 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2613 DBG_OPT_CSTEVAL(n, value);
2617 /* Try architecture dependent optimization */
2618 value = arch_dep_replace_div_by_const(n);
2621 assert(mode_is_float(mode));
2623 /* Optimize x/c to x*(1/c) */
2624 if (get_mode_arithmetic(mode) == irma_ieee754) {
2625 ir_tarval *tv = value_of(b);
2627 if (tv != tarval_bad) {
2628 int rem = tarval_fp_ops_enabled();
2631 * Floating point constant folding might be disabled here to
2633 * However, as we check for exact result, doing it is safe.
2636 tarval_enable_fp_ops(1);
2637 tv = tarval_div(get_mode_one(mode), tv);
2638 tarval_enable_fp_ops(rem);
2640 /* Do the transformation if the result is either exact or we are
2641 not using strict rules. */
2642 if (tv != tarval_bad &&
2643 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2644 ir_node *block = get_nodes_block(n);
2645 ir_graph *irg = get_irn_irg(block);
2646 ir_node *c = new_r_Const(irg, tv);
2647 dbg_info *dbgi = get_irn_dbg_info(n);
2648 value = new_rd_Mul(dbgi, block, a, c, mode);
2661 /* Turn Div into a tuple (mem, jmp, bad, value) */
2662 mem = get_Div_mem(n);
2663 blk = get_nodes_block(n);
2664 irg = get_irn_irg(blk);
2666 /* skip a potential Pin */
2667 mem = skip_Pin(mem);
2668 turn_into_tuple(n, pn_Div_max+1);
2669 set_Tuple_pred(n, pn_Div_M, mem);
2670 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2671 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2672 set_Tuple_pred(n, pn_Div_res, value);
2675 } /* transform_node_Div */
2678 * Transform a Mod node.
2680 static ir_node *transform_node_Mod(ir_node *n)
2682 ir_mode *mode = get_Mod_resmode(n);
2683 ir_node *a = get_Mod_left(n);
2684 ir_node *b = get_Mod_right(n);
2689 if (is_Const(b) && is_const_Phi(a)) {
2690 /* check for Div(Phi, Const) */
2691 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2693 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2697 else if (is_Const(a) && is_const_Phi(b)) {
2698 /* check for Div(Const, Phi) */
2699 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2701 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2705 else if (is_const_Phi(a) && is_const_Phi(b)) {
2706 /* check for Div(Phi, Phi) */
2707 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2709 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2716 irg = get_irn_irg(n);
2717 if (tv != tarval_bad) {
2718 value = new_r_Const(irg, tv);
2720 DBG_OPT_CSTEVAL(n, value);
2723 ir_node *a = get_Mod_left(n);
2724 ir_node *b = get_Mod_right(n);
2725 const ir_node *dummy;
2727 if (a == b && value_not_zero(a, &dummy)) {
2728 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2729 value = new_r_Const(irg, get_mode_null(mode));
2730 DBG_OPT_CSTEVAL(n, value);
2733 if (mode_is_signed(mode) && is_Const(b)) {
2734 ir_tarval *tv = get_Const_tarval(b);
2736 if (tv == get_mode_minus_one(mode)) {
2738 value = new_r_Const(irg, get_mode_null(mode));
2739 DBG_OPT_CSTEVAL(n, value);
2743 /* Try architecture dependent optimization */
2744 value = arch_dep_replace_mod_by_const(n);
2753 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2754 mem = get_Mod_mem(n);
2755 blk = get_nodes_block(n);
2756 irg = get_irn_irg(blk);
2758 /* skip a potential Pin */
2759 mem = skip_Pin(mem);
2760 turn_into_tuple(n, pn_Mod_max+1);
2761 set_Tuple_pred(n, pn_Mod_M, mem);
2762 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2763 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2764 set_Tuple_pred(n, pn_Mod_res, value);
2767 } /* transform_node_Mod */
2770 * Transform a Cond node.
2772 * Replace the Cond by a Jmp if it branches on a constant
2775 static ir_node *transform_node_Cond(ir_node *n)
2778 ir_node *a = get_Cond_selector(n);
2779 ir_tarval *ta = value_of(a);
2780 ir_graph *irg = get_irn_irg(n);
2783 /* we need block info which is not available in floating irgs */
2784 if (get_irg_pinned(irg) == op_pin_state_floats)
2787 if ((ta != tarval_bad) &&
2788 (get_irn_mode(a) == mode_b) &&
2789 (get_opt_unreachable_code())) {
2790 /* It's a boolean Cond, branching on a boolean constant.
2791 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2792 ir_node *blk = get_nodes_block(n);
2793 jmp = new_r_Jmp(blk);
2794 turn_into_tuple(n, pn_Cond_max+1);
2795 if (ta == tarval_b_true) {
2796 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2797 set_Tuple_pred(n, pn_Cond_true, jmp);
2799 set_Tuple_pred(n, pn_Cond_false, jmp);
2800 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2802 /* We might generate an endless loop, so keep it alive. */
2803 add_End_keepalive(get_irg_end(irg), blk);
2806 } /* transform_node_Cond */
2809 * Prototype of a recursive transform function
2810 * for bitwise distributive transformations.
2812 typedef ir_node* (*recursive_transform)(ir_node *n);
2815 * makes use of distributive laws for and, or, eor
2816 * and(a OP c, b OP c) -> and(a, b) OP c
2817 * note, might return a different op than n
2819 static ir_node *transform_bitwise_distributive(ir_node *n,
2820 recursive_transform trans_func)
2823 ir_node *a = get_binop_left(n);
2824 ir_node *b = get_binop_right(n);
2825 ir_op *op = get_irn_op(a);
2826 ir_op *op_root = get_irn_op(n);
2828 if (op != get_irn_op(b))
2831 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2832 if (op == op_Conv) {
2833 ir_node *a_op = get_Conv_op(a);
2834 ir_node *b_op = get_Conv_op(b);
2835 ir_mode *a_mode = get_irn_mode(a_op);
2836 ir_mode *b_mode = get_irn_mode(b_op);
2837 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2838 ir_node *blk = get_nodes_block(n);
2841 set_binop_left(n, a_op);
2842 set_binop_right(n, b_op);
2843 set_irn_mode(n, a_mode);
2845 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2847 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2853 /* nothing to gain here */
2857 if (op == op_Shrs || op == op_Shr || op == op_Shl
2858 || op == op_And || op == op_Or || op == op_Eor) {
2859 ir_node *a_left = get_binop_left(a);
2860 ir_node *a_right = get_binop_right(a);
2861 ir_node *b_left = get_binop_left(b);
2862 ir_node *b_right = get_binop_right(b);
2864 ir_node *op1 = NULL;
2865 ir_node *op2 = NULL;
2867 if (is_op_commutative(op)) {
2868 if (a_left == b_left) {
2872 } else if (a_left == b_right) {
2876 } else if (a_right == b_left) {
2882 if (a_right == b_right) {
2889 /* (a sop c) & (b sop c) => (a & b) sop c */
2890 ir_node *blk = get_nodes_block(n);
2892 ir_node *new_n = exact_copy(n);
2893 set_binop_left(new_n, op1);
2894 set_binop_right(new_n, op2);
2895 new_n = trans_func(new_n);
2897 if (op_root == op_Eor && op == op_Or) {
2898 dbg_info *dbgi = get_irn_dbg_info(n);
2899 ir_mode *mode = get_irn_mode(c);
2901 c = new_rd_Not(dbgi, blk, c, mode);
2902 n = new_rd_And(dbgi, blk, new_n, c, mode);
2905 set_nodes_block(n, blk);
2906 set_binop_left(n, new_n);
2907 set_binop_right(n, c);
2911 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2920 * Create a 0 constant of given mode.
2922 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2924 ir_tarval *tv = get_mode_null(mode);
2925 ir_node *cnst = new_r_Const(irg, tv);
2930 static bool is_shiftop(const ir_node *n)
2932 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2936 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
2938 * - and, or, xor instead of &
2939 * - Shl, Shr, Shrs, rotl instead of >>
2940 * (with a special case for Or/Xor + Shrs)
2942 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
2944 static ir_node *transform_node_shift_bitop(ir_node *n)
2946 ir_graph *irg = get_irn_irg(n);
2947 ir_node *right = get_binop_right(n);
2948 ir_mode *mode = get_irn_mode(n);
2950 ir_node *bitop_left;
2951 ir_node *bitop_right;
2960 ir_tarval *tv_shift;
2962 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2965 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
2967 if (!is_Const(right))
2970 left = get_binop_left(n);
2971 op_left = get_irn_op(left);
2972 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
2975 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2976 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
2977 /* TODO: test if sign bit is affectes */
2981 bitop_right = get_binop_right(left);
2982 if (!is_Const(bitop_right))
2985 bitop_left = get_binop_left(left);
2987 block = get_nodes_block(n);
2988 dbgi = get_irn_dbg_info(n);
2989 tv1 = get_Const_tarval(bitop_right);
2990 tv2 = get_Const_tarval(right);
2992 assert(get_tarval_mode(tv1) == mode);
2995 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
2996 tv_shift = tarval_shl(tv1, tv2);
2997 } else if (is_Shr(n)) {
2998 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
2999 tv_shift = tarval_shr(tv1, tv2);
3000 } else if (is_Shrs(n)) {
3001 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3002 tv_shift = tarval_shrs(tv1, tv2);
3005 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3006 tv_shift = tarval_rotl(tv1, tv2);
3009 assert(get_tarval_mode(tv_shift) == mode);
3010 irg = get_irn_irg(n);
3011 new_const = new_r_Const(irg, tv_shift);
3013 if (op_left == op_And) {
3014 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3015 } else if (op_left == op_Or) {
3016 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3018 assert(op_left == op_Eor);
3019 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3026 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
3028 * - and, or, xor instead of &
3029 * - Shl, Shr, Shrs, rotl instead of >>
3030 * (with a special case for Or/Xor + Shrs)
3032 * This normalisation is usually good for the backend since << C can often be
3033 * matched as address-mode.
3035 static ir_node *transform_node_bitop_shift(ir_node *n)
3037 ir_graph *irg = get_irn_irg(n);
3038 ir_node *left = get_binop_left(n);
3039 ir_node *right = get_binop_right(n);
3040 ir_mode *mode = get_irn_mode(n);
3041 ir_node *shift_left;
3042 ir_node *shift_right;
3044 dbg_info *dbg_bitop;
3045 dbg_info *dbg_shift;
3051 ir_tarval *tv_bitop;
3053 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3056 assert(is_And(n) || is_Or(n) || is_Eor(n));
3057 if (!is_Const(right) || !is_shiftop(left))
3060 shift_left = get_binop_left(left);
3061 shift_right = get_binop_right(left);
3062 if (!is_Const(shift_right))
3065 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3066 if (is_Shrs(left)) {
3067 /* TODO this could be improved */
3071 irg = get_irn_irg(n);
3072 block = get_nodes_block(n);
3073 dbg_bitop = get_irn_dbg_info(n);
3074 dbg_shift = get_irn_dbg_info(left);
3075 tv1 = get_Const_tarval(shift_right);
3076 tv2 = get_Const_tarval(right);
3077 assert(get_tarval_mode(tv2) == mode);
3080 tv_bitop = tarval_shr(tv2, tv1);
3081 } else if (is_Shr(left)) {
3082 if (is_Or(n) || is_Eor(n)) {
3084 * TODO this can be improved by checking whether
3085 * the left shift produces an overflow
3089 tv_bitop = tarval_shl(tv2, tv1);
3091 assert(is_Rotl(left));
3092 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
3094 new_const = new_r_Const(irg, tv_bitop);
3097 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
3098 } else if (is_Or(n)) {
3099 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
3102 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
3106 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
3107 } else if (is_Shr(left)) {
3108 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
3110 assert(is_Rotl(left));
3111 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
3120 static ir_node *transform_node_And(ir_node *n)
3122 ir_node *c, *oldn = n;
3123 ir_node *a = get_And_left(n);
3124 ir_node *b = get_And_right(n);
3126 vrp_attr *a_vrp, *b_vrp;
3128 if (is_Cmp(a) && is_Cmp(b)) {
3129 ir_node *a_left = get_Cmp_left(a);
3130 ir_node *a_right = get_Cmp_right(a);
3131 ir_node *b_left = get_Cmp_left(b);
3132 ir_node *b_right = get_Cmp_right(b);
3133 ir_relation a_relation = get_Cmp_relation(a);
3134 ir_relation b_relation = get_Cmp_relation(b);
3135 /* we can combine the relations of two compares with the same
3137 if (a_left == b_left && b_left == b_right) {
3138 dbg_info *dbgi = get_irn_dbg_info(n);
3139 ir_node *block = get_nodes_block(n);
3140 ir_relation new_relation = a_relation & b_relation;
3141 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3143 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3144 if (a_relation == b_relation && a_relation == ir_relation_equal
3145 && !mode_is_float(get_irn_mode(a_left))
3146 && !mode_is_float(get_irn_mode(b_left))) {
3147 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3148 dbg_info *dbgi = get_irn_dbg_info(n);
3149 ir_node *block = get_nodes_block(n);
3150 ir_mode *a_mode = get_irn_mode(a_left);
3151 ir_mode *b_mode = get_irn_mode(b_left);
3152 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3153 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3154 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3155 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3156 ir_graph *irg = get_irn_irg(n);
3157 ir_node *zero = create_zero_const(irg, b_mode);
3158 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3160 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3161 dbg_info *dbgi = get_irn_dbg_info(n);
3162 ir_node *block = get_nodes_block(n);
3163 ir_mode *a_mode = get_irn_mode(a_left);
3164 ir_mode *b_mode = get_irn_mode(b_left);
3165 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3166 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3167 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3168 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3169 ir_graph *irg = get_irn_irg(n);
3170 ir_node *zero = create_zero_const(irg, a_mode);
3171 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3176 mode = get_irn_mode(n);
3177 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3181 ir_node *op = get_Not_op(b);
3183 ir_node *ba = get_And_left(op);
3184 ir_node *bb = get_And_right(op);
3186 /* it's enough to test the following cases due to normalization! */
3187 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3188 /* (a|b) & ~(a&b) = a^b */
3189 ir_node *block = get_nodes_block(n);
3191 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3192 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3200 ir_node *op = get_Not_op(a);
3202 ir_node *aa = get_And_left(op);
3203 ir_node *ab = get_And_right(op);
3205 /* it's enough to test the following cases due to normalization! */
3206 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3207 /* (a|b) & ~(a&b) = a^b */
3208 ir_node *block = get_nodes_block(n);
3210 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3211 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3218 ir_node *al = get_Eor_left(a);
3219 ir_node *ar = get_Eor_right(a);
3222 /* (b ^ a) & b -> ~a & b */
3223 dbg_info *dbg = get_irn_dbg_info(n);
3224 ir_node *block = get_nodes_block(n);
3226 ar = new_rd_Not(dbg, block, ar, mode);
3227 n = new_rd_And(dbg, block, ar, b, mode);
3228 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3232 /* (a ^ b) & b -> ~a & b */
3233 dbg_info *dbg = get_irn_dbg_info(n);
3234 ir_node *block = get_nodes_block(n);
3236 al = new_rd_Not(dbg, block, al, mode);
3237 n = new_rd_And(dbg, block, al, b, mode);
3238 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3243 ir_node *bl = get_Eor_left(b);
3244 ir_node *br = get_Eor_right(b);
3247 /* a & (a ^ b) -> a & ~b */
3248 dbg_info *dbg = get_irn_dbg_info(n);
3249 ir_node *block = get_nodes_block(n);
3251 br = new_rd_Not(dbg, block, br, mode);
3252 n = new_rd_And(dbg, block, br, a, mode);
3253 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3257 /* a & (b ^ a) -> a & ~b */
3258 dbg_info *dbg = get_irn_dbg_info(n);
3259 ir_node *block = get_nodes_block(n);
3261 bl = new_rd_Not(dbg, block, bl, mode);
3262 n = new_rd_And(dbg, block, bl, a, mode);
3263 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3267 if (is_Not(a) && is_Not(b)) {
3268 /* ~a & ~b = ~(a|b) */
3269 ir_node *block = get_nodes_block(n);
3270 ir_mode *mode = get_irn_mode(n);
3274 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3275 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3280 b_vrp = vrp_get_info(b);
3281 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3282 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3288 a_vrp = vrp_get_info(a);
3289 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3290 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3294 n = transform_bitwise_distributive(n, transform_node_And);
3296 n = transform_node_bitop_shift(n);
3299 } /* transform_node_And */
3301 /* the order of the values is important! */
3302 typedef enum const_class {
3308 static const_class classify_const(const ir_node* n)
3310 if (is_Const(n)) return const_const;
3311 if (is_irn_constlike(n)) return const_like;
3316 * Determines whether r is more constlike or has a larger index (in that order)
3319 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3321 const const_class l_order = classify_const(l);
3322 const const_class r_order = classify_const(r);
3324 l_order > r_order ||
3325 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3331 static ir_node *transform_node_Eor(ir_node *n)
3333 ir_node *c, *oldn = n;
3334 ir_node *a = get_Eor_left(n);
3335 ir_node *b = get_Eor_right(n);
3336 ir_mode *mode = get_irn_mode(n);
3338 /* we can combine the relations of two compares with the same operands */
3339 if (is_Cmp(a) && is_Cmp(b)) {
3340 ir_node *a_left = get_Cmp_left(a);
3341 ir_node *a_right = get_Cmp_left(a);
3342 ir_node *b_left = get_Cmp_left(b);
3343 ir_node *b_right = get_Cmp_right(b);
3344 if (a_left == b_left && b_left == b_right) {
3345 dbg_info *dbgi = get_irn_dbg_info(n);
3346 ir_node *block = get_nodes_block(n);
3347 ir_relation a_relation = get_Cmp_relation(a);
3348 ir_relation b_relation = get_Cmp_relation(b);
3349 ir_relation new_relation = a_relation ^ b_relation;
3350 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3354 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3356 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3357 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3358 dbg_info *dbg = get_irn_dbg_info(n);
3359 ir_node *block = get_nodes_block(n);
3360 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3361 ir_node *new_left = get_Not_op(a);
3362 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3363 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3365 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3366 dbg_info *dbg = get_irn_dbg_info(n);
3367 ir_node *block = get_nodes_block(n);
3368 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3369 ir_node *new_right = get_Not_op(b);
3370 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3371 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3375 /* x ^ 1...1 -> ~1 */
3376 if (is_Const(b) && is_Const_all_one(b)) {
3377 n = new_r_Not(get_nodes_block(n), a, mode);
3378 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3382 n = transform_bitwise_distributive(n, transform_node_Eor);
3384 n = transform_node_bitop_shift(n);
3387 } /* transform_node_Eor */
3392 static ir_node *transform_node_Not(ir_node *n)
3394 ir_node *c, *oldn = n;
3395 ir_node *a = get_Not_op(n);
3396 ir_mode *mode = get_irn_mode(n);
3398 HANDLE_UNOP_PHI(tarval_not,a,c);
3400 /* check for a boolean Not */
3402 dbg_info *dbgi = get_irn_dbg_info(a);
3403 ir_node *block = get_nodes_block(a);
3404 ir_relation relation = get_Cmp_relation(a);
3405 relation = get_negated_relation(relation);
3406 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3411 /* normalize ~(a ^ b) => a ^ ~b */
3413 dbg_info *dbg = get_irn_dbg_info(n);
3414 ir_node *block = get_nodes_block(n);
3415 ir_node *eor_right = get_Eor_right(a);
3416 ir_node *eor_left = get_Eor_left(a);
3417 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3418 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3422 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3423 if (is_Minus(a)) { /* ~-x -> x + -1 */
3424 dbg_info *dbg = get_irn_dbg_info(n);
3425 ir_graph *irg = get_irn_irg(n);
3426 ir_node *block = get_nodes_block(n);
3427 ir_node *add_l = get_Minus_op(a);
3428 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3429 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3430 } else if (is_Add(a)) {
3431 ir_node *add_r = get_Add_right(a);
3432 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3433 /* ~(x + -1) = -x */
3434 ir_node *op = get_Add_left(a);
3435 ir_node *blk = get_nodes_block(n);
3436 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3445 * Transform a Minus.
3449 * -(a >>u (size-1)) = a >>s (size-1)
3450 * -(a >>s (size-1)) = a >>u (size-1)
3451 * -(a * const) -> a * -const
3453 static ir_node *transform_node_Minus(ir_node *n)
3455 ir_node *c, *oldn = n;
3456 ir_node *a = get_Minus_op(n);
3459 HANDLE_UNOP_PHI(tarval_neg,a,c);
3461 mode = get_irn_mode(a);
3462 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3463 /* the following rules are only to twos-complement */
3466 ir_node *op = get_Not_op(a);
3467 ir_tarval *tv = get_mode_one(mode);
3468 ir_node *blk = get_nodes_block(n);
3469 ir_graph *irg = get_irn_irg(blk);
3470 ir_node *c = new_r_Const(irg, tv);
3471 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3472 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3476 ir_node *c = get_Shr_right(a);
3479 ir_tarval *tv = get_Const_tarval(c);
3481 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3482 /* -(a >>u (size-1)) = a >>s (size-1) */
3483 ir_node *v = get_Shr_left(a);
3485 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3486 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3492 ir_node *c = get_Shrs_right(a);
3495 ir_tarval *tv = get_Const_tarval(c);
3497 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3498 /* -(a >>s (size-1)) = a >>u (size-1) */
3499 ir_node *v = get_Shrs_left(a);
3501 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3502 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3509 /* - (a-b) = b - a */
3510 ir_node *la = get_Sub_left(a);
3511 ir_node *ra = get_Sub_right(a);
3512 ir_node *blk = get_nodes_block(n);
3514 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3515 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3519 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3520 ir_node *mul_l = get_Mul_left(a);
3521 ir_node *mul_r = get_Mul_right(a);
3522 ir_tarval *tv = value_of(mul_r);
3523 if (tv != tarval_bad) {
3524 tv = tarval_neg(tv);
3525 if (tv != tarval_bad) {
3526 ir_graph *irg = get_irn_irg(n);
3527 ir_node *cnst = new_r_Const(irg, tv);
3528 dbg_info *dbg = get_irn_dbg_info(a);
3529 ir_node *block = get_nodes_block(a);
3530 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3531 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3538 } /* transform_node_Minus */
3541 * Transform a Proj(Load) with a non-null address.
3543 static ir_node *transform_node_Proj_Load(ir_node *proj)
3545 if (get_opt_ldst_only_null_ptr_exceptions()) {
3546 if (get_irn_mode(proj) == mode_X) {
3547 ir_node *load = get_Proj_pred(proj);
3549 /* get the Load address */
3550 const ir_node *addr = get_Load_ptr(load);
3551 const ir_node *confirm;
3553 if (value_not_null(addr, &confirm)) {
3554 if (confirm == NULL) {
3555 /* this node may float if it did not depend on a Confirm */
3556 set_irn_pinned(load, op_pin_state_floats);
3558 if (get_Proj_proj(proj) == pn_Load_X_except) {
3559 ir_graph *irg = get_irn_irg(proj);
3560 DBG_OPT_EXC_REM(proj);
3561 return new_r_Bad(irg, mode_X);
3563 ir_node *blk = get_nodes_block(load);
3564 return new_r_Jmp(blk);
3570 } /* transform_node_Proj_Load */
3573 * Transform a Proj(Store) with a non-null address.
3575 static ir_node *transform_node_Proj_Store(ir_node *proj)
3577 if (get_opt_ldst_only_null_ptr_exceptions()) {
3578 if (get_irn_mode(proj) == mode_X) {
3579 ir_node *store = get_Proj_pred(proj);
3581 /* get the load/store address */
3582 const ir_node *addr = get_Store_ptr(store);
3583 const ir_node *confirm;
3585 if (value_not_null(addr, &confirm)) {
3586 if (confirm == NULL) {
3587 /* this node may float if it did not depend on a Confirm */
3588 set_irn_pinned(store, op_pin_state_floats);
3590 if (get_Proj_proj(proj) == pn_Store_X_except) {
3591 ir_graph *irg = get_irn_irg(proj);
3592 DBG_OPT_EXC_REM(proj);
3593 return new_r_Bad(irg, mode_X);
3595 ir_node *blk = get_nodes_block(store);
3596 return new_r_Jmp(blk);
3602 } /* transform_node_Proj_Store */
3605 * Transform a Proj(Div) with a non-zero value.
3606 * Removes the exceptions and routes the memory to the NoMem node.
3608 static ir_node *transform_node_Proj_Div(ir_node *proj)
3610 ir_node *div = get_Proj_pred(proj);
3611 ir_node *b = get_Div_right(div);
3612 ir_node *res, *new_mem;
3613 const ir_node *confirm;
3616 if (value_not_zero(b, &confirm)) {
3617 /* div(x, y) && y != 0 */
3618 if (confirm == NULL) {
3619 /* we are sure we have a Const != 0 */
3620 new_mem = get_Div_mem(div);
3621 new_mem = skip_Pin(new_mem);
3622 set_Div_mem(div, new_mem);
3623 set_irn_pinned(div, op_pin_state_floats);
3626 proj_nr = get_Proj_proj(proj);
3628 case pn_Div_X_regular:
3629 return new_r_Jmp(get_nodes_block(div));
3631 case pn_Div_X_except: {
3632 ir_graph *irg = get_irn_irg(proj);
3633 /* we found an exception handler, remove it */
3634 DBG_OPT_EXC_REM(proj);
3635 return new_r_Bad(irg, mode_X);
3639 ir_graph *irg = get_irn_irg(proj);
3640 res = get_Div_mem(div);
3641 new_mem = get_irg_no_mem(irg);
3644 /* This node can only float up to the Confirm block */
3645 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3647 set_irn_pinned(div, op_pin_state_floats);
3648 /* this is a Div without exception, we can remove the memory edge */
3649 set_Div_mem(div, new_mem);
3655 } /* transform_node_Proj_Div */
3658 * Transform a Proj(Mod) with a non-zero value.
3659 * Removes the exceptions and routes the memory to the NoMem node.
3661 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3663 ir_node *mod = get_Proj_pred(proj);
3664 ir_node *b = get_Mod_right(mod);
3665 ir_node *res, *new_mem;
3666 const ir_node *confirm;
3669 if (value_not_zero(b, &confirm)) {
3670 /* mod(x, y) && y != 0 */
3671 proj_nr = get_Proj_proj(proj);
3673 if (confirm == NULL) {
3674 /* we are sure we have a Const != 0 */
3675 new_mem = get_Mod_mem(mod);
3676 new_mem = skip_Pin(new_mem);
3677 set_Mod_mem(mod, new_mem);
3678 set_irn_pinned(mod, op_pin_state_floats);
3683 case pn_Mod_X_regular:
3684 return new_r_Jmp(get_irn_n(mod, -1));
3686 case pn_Mod_X_except: {
3687 ir_graph *irg = get_irn_irg(proj);
3688 /* we found an exception handler, remove it */
3689 DBG_OPT_EXC_REM(proj);
3690 return new_r_Bad(irg, mode_X);
3694 ir_graph *irg = get_irn_irg(proj);
3695 res = get_Mod_mem(mod);
3696 new_mem = get_irg_no_mem(irg);
3699 /* This node can only float up to the Confirm block */
3700 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3702 /* this is a Mod without exception, we can remove the memory edge */
3703 set_Mod_mem(mod, new_mem);
3707 if (get_Mod_left(mod) == b) {
3708 /* a % a = 0 if a != 0 */
3709 ir_graph *irg = get_irn_irg(proj);
3710 ir_mode *mode = get_irn_mode(proj);
3711 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3713 DBG_OPT_CSTEVAL(mod, res);
3719 } /* transform_node_Proj_Mod */
3722 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3724 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3726 ir_node *n = get_Proj_pred(proj);
3727 ir_node *b = get_Cond_selector(n);
3729 if (!get_opt_unreachable_code())
3732 if (mode_is_int(get_irn_mode(b))) {
3733 ir_tarval *tb = value_of(b);
3735 if (tb != tarval_bad) {
3736 /* we have a constant switch */
3737 long num = get_Proj_proj(proj);
3739 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3740 if (get_tarval_long(tb) == num) {
3741 /* Do NOT create a jump here, or we will have 2 control flow ops
3742 * in a block. This case is optimized away in optimize_cf(). */
3745 ir_graph *irg = get_irn_irg(proj);
3746 /* this case will NEVER be taken, kill it */
3747 return new_r_Bad(irg, mode_X);
3751 long num = get_Proj_proj(proj);
3752 vrp_attr *b_vrp = vrp_get_info(b);
3753 if (num != get_Cond_default_proj(n) && b_vrp) {
3754 /* Try handling with vrp data. We only remove dead parts. */
3755 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3757 if (b_vrp->range_type == VRP_RANGE) {
3758 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3759 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3761 if ((cmp_result & ir_relation_greater) == cmp_result
3762 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3763 ir_graph *irg = get_irn_irg(proj);
3764 return new_r_Bad(irg, mode_X);
3766 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3767 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3768 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3770 if ((cmp_result & ir_relation_less_equal) == cmp_result
3771 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3772 ir_graph *irg = get_irn_irg(proj);
3773 return new_r_Bad(irg, mode_X);
3778 tarval_and( b_vrp->bits_set, tp),
3780 ) == ir_relation_equal)) {
3781 ir_graph *irg = get_irn_irg(proj);
3782 return new_r_Bad(irg, mode_X);
3788 tarval_not(b_vrp->bits_not_set)),
3789 tarval_not(b_vrp->bits_not_set))
3790 == ir_relation_equal)) {
3791 ir_graph *irg = get_irn_irg(proj);
3792 return new_r_Bad(irg, mode_X);
3801 * return true if the operation returns a value with exactly 1 bit set
3803 static bool is_single_bit(const ir_node *node)
3805 /* a first implementation, could be extended with vrp and others... */
3807 ir_node *shl_l = get_Shl_left(node);
3808 ir_mode *mode = get_irn_mode(node);
3809 int modulo = get_mode_modulo_shift(mode);
3810 /* this works if we shift a 1 and we have modulo shift */
3811 if (is_Const(shl_l) && is_Const_one(shl_l)
3812 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3815 } else if (is_Const(node)) {
3816 ir_tarval *tv = get_Const_tarval(node);
3817 return tarval_is_single_bit(tv);
3823 * Normalizes and optimizes Cmp nodes.
3825 static ir_node *transform_node_Cmp(ir_node *n)
3827 ir_node *left = get_Cmp_left(n);
3828 ir_node *right = get_Cmp_right(n);
3829 ir_mode *mode = get_irn_mode(left);
3830 ir_tarval *tv = NULL;
3831 bool changed = false;
3832 bool changedc = false;
3833 ir_relation relation = get_Cmp_relation(n);
3834 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3836 /* mask out impossible relations */
3837 ir_relation new_relation = relation & possible;
3838 if (new_relation != relation) {
3839 relation = new_relation;
3843 /* Remove unnecessary conversions */
3844 if (is_Conv(left) && is_Conv(right)) {
3845 ir_node *op_left = get_Conv_op(left);
3846 ir_node *op_right = get_Conv_op(right);
3847 ir_mode *mode_left = get_irn_mode(op_left);
3848 ir_mode *mode_right = get_irn_mode(op_right);
3850 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3851 && mode_left != mode_b && mode_right != mode_b) {
3852 ir_node *block = get_nodes_block(n);
3854 if (mode_left == mode_right) {
3858 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3859 } else if (smaller_mode(mode_left, mode_right)) {
3860 left = new_r_Conv(block, op_left, mode_right);
3863 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3864 } else if (smaller_mode(mode_right, mode_left)) {
3866 right = new_r_Conv(block, op_right, mode_left);
3868 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3870 mode = get_irn_mode(left);
3873 if (is_Conv(left) && is_Const(right)) {
3874 ir_node *op_left = get_Conv_op(left);
3875 ir_mode *mode_left = get_irn_mode(op_left);
3876 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3877 ir_tarval *tv = get_Const_tarval(right);
3878 tarval_int_overflow_mode_t last_mode
3879 = tarval_get_integer_overflow_mode();
3881 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3882 new_tv = tarval_convert_to(tv, mode_left);
3883 tarval_set_integer_overflow_mode(last_mode);
3884 if (new_tv != tarval_bad) {
3885 ir_graph *irg = get_irn_irg(n);
3887 right = new_r_Const(irg, new_tv);
3888 mode = get_irn_mode(left);
3890 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3896 * Optimize -a CMP -b into b CMP a.
3897 * This works only for modes where unary Minus cannot Overflow.
3898 * Note that two-complement integers can Overflow so it will NOT work.
3900 if (!mode_overflow_on_unary_Minus(mode) &&
3901 is_Minus(left) && is_Minus(right)) {
3902 left = get_Minus_op(left);
3903 right = get_Minus_op(right);
3904 relation = get_inversed_relation(relation);
3906 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3909 /* remove operation on both sides if possible */
3910 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3912 * The following operations are NOT safe for floating point operations, for instance
3913 * 1.0 + inf == 2.0 + inf, =/=> x == y
3915 if (mode_is_int(mode)) {
3916 unsigned lop = get_irn_opcode(left);
3918 if (lop == get_irn_opcode(right)) {
3919 ir_node *ll, *lr, *rl, *rr;
3921 /* same operation on both sides, try to remove */
3925 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3926 left = get_unop_op(left);
3927 right = get_unop_op(right);
3929 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3932 ll = get_Add_left(left);
3933 lr = get_Add_right(left);
3934 rl = get_Add_left(right);
3935 rr = get_Add_right(right);
3938 /* X + a CMP X + b ==> a CMP b */
3942 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3943 } else if (ll == rr) {
3944 /* X + a CMP b + X ==> a CMP b */
3948 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3949 } else if (lr == rl) {
3950 /* a + X CMP X + b ==> a CMP b */
3954 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3955 } else if (lr == rr) {
3956 /* a + X CMP b + X ==> a CMP b */
3960 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3964 ll = get_Sub_left(left);
3965 lr = get_Sub_right(left);
3966 rl = get_Sub_left(right);
3967 rr = get_Sub_right(right);
3970 /* X - a CMP X - b ==> a CMP b */
3974 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3975 } else if (lr == rr) {
3976 /* a - X CMP b - X ==> a CMP b */
3980 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3984 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3985 /* a ROTL X CMP b ROTL X ==> a CMP b */
3986 left = get_Rotl_left(left);
3987 right = get_Rotl_left(right);
3989 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3997 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3998 if (is_Add(left) || is_Sub(left)) {
3999 ir_node *ll = get_binop_left(left);
4000 ir_node *lr = get_binop_right(left);
4002 if (lr == right && is_Add(left)) {
4008 ir_graph *irg = get_irn_irg(n);
4010 right = create_zero_const(irg, mode);
4012 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4015 if (is_Add(right) || is_Sub(right)) {
4016 ir_node *rl = get_binop_left(right);
4017 ir_node *rr = get_binop_right(right);
4019 if (rr == left && is_Add(right)) {
4025 ir_graph *irg = get_irn_irg(n);
4027 right = create_zero_const(irg, mode);
4029 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4033 if (is_And(left) && is_Const(right)) {
4034 ir_node *ll = get_binop_left(left);
4035 ir_node *lr = get_binop_right(left);
4036 if (is_Shr(ll) && is_Const(lr)) {
4037 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4038 ir_node *block = get_nodes_block(n);
4039 ir_mode *mode = get_irn_mode(left);
4041 ir_node *llr = get_Shr_right(ll);
4042 if (is_Const(llr)) {
4043 dbg_info *dbg = get_irn_dbg_info(left);
4044 ir_graph *irg = get_irn_irg(left);
4046 ir_tarval *c1 = get_Const_tarval(llr);
4047 ir_tarval *c2 = get_Const_tarval(lr);
4048 ir_tarval *c3 = get_Const_tarval(right);
4049 ir_tarval *mask = tarval_shl(c2, c1);
4050 ir_tarval *value = tarval_shl(c3, c1);
4052 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4053 right = new_r_Const(irg, value);
4058 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4060 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4061 right = get_Eor_right(left);
4062 left = get_Eor_left(left);
4065 } /* mode_is_int(...) */
4068 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4069 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4070 if (mode_is_int(mode) && is_And(left)
4071 && (relation == ir_relation_equal
4072 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4073 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4074 ir_node *and0 = get_And_left(left);
4075 ir_node *and1 = get_And_right(left);
4076 if (and1 == right) {
4077 ir_node *tmp = and0;
4081 if (and0 == right && is_single_bit(and0)) {
4082 ir_graph *irg = get_irn_irg(n);
4084 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4085 right = create_zero_const(irg, mode);
4090 /* replace mode_b compares with ands/ors */
4091 if (mode == mode_b) {
4092 ir_node *block = get_nodes_block(n);
4096 case ir_relation_less_equal:
4097 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4099 case ir_relation_less:
4100 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4102 case ir_relation_greater_equal:
4103 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4105 case ir_relation_greater:
4106 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4108 case ir_relation_less_greater:
4109 bres = new_r_Eor(block, left, right, mode_b);
4111 case ir_relation_equal:
4112 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4115 #ifdef DEBUG_libfirm
4116 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4121 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4127 * First step: normalize the compare op
4128 * by placing the constant on the right side
4129 * or moving the lower address node to the left.
4131 if (!operands_are_normalized(left, right)) {
4136 relation = get_inversed_relation(relation);
4141 * Second step: Try to reduce the magnitude
4142 * of a constant. This may help to generate better code
4143 * later and may help to normalize more compares.
4144 * Of course this is only possible for integer values.
4146 tv = value_of(right);
4147 if (tv != tarval_bad) {
4148 ir_mode *mode = get_irn_mode(right);
4150 /* TODO extend to arbitrary constants */
4151 if (is_Conv(left) && tarval_is_null(tv)) {
4152 ir_node *op = get_Conv_op(left);
4153 ir_mode *op_mode = get_irn_mode(op);
4156 * UpConv(x) REL 0 ==> x REL 0
4157 * Don't do this for float values as it's unclear whether it is a
4158 * win. (on the other side it makes detection/creation of fabs hard)
4160 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4161 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4162 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4163 !mode_is_float(mode)) {
4164 tv = get_mode_null(op_mode);
4168 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4172 if (tv != tarval_bad) {
4173 /* the following optimization is possible on modes without Overflow
4174 * on Unary Minus or on == and !=:
4175 * -a CMP c ==> a swap(CMP) -c
4177 * Beware: for two-complement Overflow may occur, so only == and != can
4178 * be optimized, see this:
4179 * -MININT < 0 =/=> MININT > 0 !!!
4181 if (is_Minus(left) &&
4182 (!mode_overflow_on_unary_Minus(mode) ||
4183 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4184 tv = tarval_neg(tv);
4186 if (tv != tarval_bad) {
4187 left = get_Minus_op(left);
4188 relation = get_inversed_relation(relation);
4190 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4192 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4193 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4194 tv = tarval_not(tv);
4196 if (tv != tarval_bad) {
4197 left = get_Not_op(left);
4199 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4203 /* for integer modes, we have more */
4204 if (mode_is_int(mode)) {
4205 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4206 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4207 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4208 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4210 if (tv != tarval_bad) {
4211 relation ^= ir_relation_equal;
4213 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4216 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4217 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4218 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4219 tv = tarval_add(tv, get_mode_one(mode));
4221 if (tv != tarval_bad) {
4222 relation ^= ir_relation_equal;
4224 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4228 /* the following reassociations work only for == and != */
4229 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4230 if (tv != tarval_bad) {
4231 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4233 ir_node *c1 = get_Sub_right(left);
4234 ir_tarval *tv2 = value_of(c1);
4236 if (tv2 != tarval_bad) {
4237 tv2 = tarval_add(tv, value_of(c1));
4239 if (tv2 != tarval_bad) {
4240 left = get_Sub_left(left);
4243 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4247 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4248 else if (is_Add(left)) {
4249 ir_node *a_l = get_Add_left(left);
4250 ir_node *a_r = get_Add_right(left);
4254 if (is_Const(a_l)) {
4256 tv2 = value_of(a_l);
4259 tv2 = value_of(a_r);
4262 if (tv2 != tarval_bad) {
4263 tv2 = tarval_sub(tv, tv2, NULL);
4265 if (tv2 != tarval_bad) {
4269 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4273 /* -a == c ==> a == -c, -a != c ==> a != -c */
4274 else if (is_Minus(left)) {
4275 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4277 if (tv2 != tarval_bad) {
4278 left = get_Minus_op(left);
4281 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4288 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4289 switch (get_irn_opcode(left)) {
4293 c1 = get_And_right(left);
4296 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4297 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4299 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4301 /* TODO: move to constant evaluation */
4302 ir_graph *irg = get_irn_irg(n);
4303 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4304 c1 = new_r_Const(irg, tv);
4305 DBG_OPT_CSTEVAL(n, c1);
4309 if (tarval_is_single_bit(tv)) {
4311 * optimization for AND:
4313 * And(x, C) == C ==> And(x, C) != 0
4314 * And(x, C) != C ==> And(X, C) == 0
4316 * if C is a single Bit constant.
4319 /* check for Constant's match. We have check hare the tarvals,
4320 because our const might be changed */
4321 if (get_Const_tarval(c1) == tv) {
4322 /* fine: do the transformation */
4323 tv = get_mode_null(get_tarval_mode(tv));
4324 relation ^= ir_relation_less_equal_greater;
4326 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4332 c1 = get_Or_right(left);
4333 if (is_Const(c1) && tarval_is_null(tv)) {
4335 * Or(x, C) == 0 && C != 0 ==> FALSE
4336 * Or(x, C) != 0 && C != 0 ==> TRUE
4338 if (! tarval_is_null(get_Const_tarval(c1))) {
4339 /* TODO: move to constant evaluation */
4340 ir_graph *irg = get_irn_irg(n);
4341 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4342 c1 = new_r_Const(irg, tv);
4343 DBG_OPT_CSTEVAL(n, c1);
4350 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4352 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4355 c1 = get_Shl_right(left);
4357 ir_graph *irg = get_irn_irg(c1);
4358 ir_tarval *tv1 = get_Const_tarval(c1);
4359 ir_mode *mode = get_irn_mode(left);
4360 ir_tarval *minus1 = get_mode_all_one(mode);
4361 ir_tarval *amask = tarval_shr(minus1, tv1);
4362 ir_tarval *cmask = tarval_shl(minus1, tv1);
4365 if (tarval_and(tv, cmask) != tv) {
4366 /* condition not met */
4367 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4368 c1 = new_r_Const(irg, tv);
4369 DBG_OPT_CSTEVAL(n, c1);
4372 sl = get_Shl_left(left);
4373 blk = get_nodes_block(n);
4374 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4375 tv = tarval_shr(tv, tv1);
4377 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4382 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4384 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4387 c1 = get_Shr_right(left);
4389 ir_graph *irg = get_irn_irg(c1);
4390 ir_tarval *tv1 = get_Const_tarval(c1);
4391 ir_mode *mode = get_irn_mode(left);
4392 ir_tarval *minus1 = get_mode_all_one(mode);
4393 ir_tarval *amask = tarval_shl(minus1, tv1);
4394 ir_tarval *cmask = tarval_shr(minus1, tv1);
4397 if (tarval_and(tv, cmask) != tv) {
4398 /* condition not met */
4399 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4400 c1 = new_r_Const(irg, tv);
4401 DBG_OPT_CSTEVAL(n, c1);
4404 sl = get_Shr_left(left);
4405 blk = get_nodes_block(n);
4406 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4407 tv = tarval_shl(tv, tv1);
4409 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4414 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4416 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4419 c1 = get_Shrs_right(left);
4421 ir_graph *irg = get_irn_irg(c1);
4422 ir_tarval *tv1 = get_Const_tarval(c1);
4423 ir_mode *mode = get_irn_mode(left);
4424 ir_tarval *minus1 = get_mode_all_one(mode);
4425 ir_tarval *amask = tarval_shl(minus1, tv1);
4426 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4429 cond = tarval_sub(cond, tv1, NULL);
4430 cond = tarval_shrs(tv, cond);
4432 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4433 /* condition not met */
4434 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4435 c1 = new_r_Const(irg, tv);
4436 DBG_OPT_CSTEVAL(n, c1);
4439 sl = get_Shrs_left(left);
4440 blk = get_nodes_block(n);
4441 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4442 tv = tarval_shl(tv, tv1);
4444 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4449 } /* tarval != bad */
4452 if (changedc) { /* need a new Const */
4453 ir_graph *irg = get_irn_irg(n);
4454 right = new_r_Const(irg, tv);
4458 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4459 ir_node *op = get_Proj_pred(left);
4461 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4462 ir_node *c = get_binop_right(op);
4465 ir_tarval *tv = get_Const_tarval(c);
4467 if (tarval_is_single_bit(tv)) {
4468 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4469 ir_node *v = get_binop_left(op);
4470 ir_node *blk = get_irn_n(op, -1);
4471 ir_graph *irg = get_irn_irg(op);
4472 ir_mode *mode = get_irn_mode(v);
4474 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4475 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4477 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4484 dbg_info *dbgi = get_irn_dbg_info(n);
4485 ir_node *block = get_nodes_block(n);
4487 /* create a new compare */
4488 n = new_rd_Cmp(dbgi, block, left, right, relation);
4495 * Optimize CopyB(mem, x, x) into a Nop.
4497 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4499 ir_node *copyb = get_Proj_pred(proj);
4500 ir_node *a = get_CopyB_dst(copyb);
4501 ir_node *b = get_CopyB_src(copyb);
4504 switch (get_Proj_proj(proj)) {
4505 case pn_CopyB_X_regular:
4506 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4507 DBG_OPT_EXC_REM(proj);
4508 proj = new_r_Jmp(get_nodes_block(copyb));
4510 case pn_CopyB_X_except: {
4511 ir_graph *irg = get_irn_irg(proj);
4512 DBG_OPT_EXC_REM(proj);
4513 proj = new_r_Bad(irg, mode_X);
4521 } /* transform_node_Proj_CopyB */
4524 * Optimize Bounds(idx, idx, upper) into idx.
4526 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4528 ir_node *oldn = proj;
4529 ir_node *bound = get_Proj_pred(proj);
4530 ir_node *idx = get_Bound_index(bound);
4531 ir_node *pred = skip_Proj(idx);
4534 if (idx == get_Bound_lower(bound))
4536 else if (is_Bound(pred)) {
4538 * idx was Bounds checked previously, it is still valid if
4539 * lower <= pred_lower && pred_upper <= upper.
4541 ir_node *lower = get_Bound_lower(bound);
4542 ir_node *upper = get_Bound_upper(bound);
4543 if (get_Bound_lower(pred) == lower &&
4544 get_Bound_upper(pred) == upper) {
4546 * One could expect that we simply return the previous
4547 * Bound here. However, this would be wrong, as we could
4548 * add an exception Proj to a new location then.
4549 * So, we must turn in into a tuple.
4555 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4556 switch (get_Proj_proj(proj)) {
4558 DBG_OPT_EXC_REM(proj);
4559 proj = get_Bound_mem(bound);
4561 case pn_Bound_X_except:
4562 DBG_OPT_EXC_REM(proj);
4563 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4567 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4569 case pn_Bound_X_regular:
4570 DBG_OPT_EXC_REM(proj);
4571 proj = new_r_Jmp(get_nodes_block(bound));
4578 } /* transform_node_Proj_Bound */
4581 * Does all optimizations on nodes that must be done on its Projs
4582 * because of creating new nodes.
4584 static ir_node *transform_node_Proj(ir_node *proj)
4586 ir_node *n = get_Proj_pred(proj);
4588 if (n->op->ops.transform_node_Proj)
4589 return n->op->ops.transform_node_Proj(proj);
4591 } /* transform_node_Proj */
4593 static bool is_block_unreachable(const ir_node *block)
4595 const ir_graph *irg = get_irn_irg(block);
4596 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4598 return get_Block_dom_depth(block) < 0;
4601 static ir_node *transform_node_Block(ir_node *block)
4603 ir_graph *irg = get_irn_irg(block);
4604 int arity = get_irn_arity(block);
4605 ir_node *bad = NULL;
4608 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4611 for (i = 0; i < arity; ++i) {
4612 ir_node *pred = get_Block_cfgpred(block, i);
4613 ir_node *pred_block = get_nodes_block(pred);
4614 if (!is_Bad(pred) && !is_block_unreachable(pred_block))
4617 bad = new_r_Bad(irg, mode_X);
4618 set_irn_n(block, i, bad);
4624 static ir_node *transform_node_Phi(ir_node *phi)
4626 int n = get_irn_arity(phi);
4627 ir_mode *mode = get_irn_mode(phi);
4628 ir_node *block = get_nodes_block(phi);
4629 ir_graph *irg = get_irn_irg(phi);
4630 ir_node *bad = NULL;
4633 /* Set phi-operands for bad-block inputs to bad */
4634 for (i = 0; i < n; ++i) {
4635 ir_node *pred = get_Block_cfgpred(block, i);
4636 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4638 bad = new_r_Bad(irg, mode);
4639 set_irn_n(phi, i, bad);
4643 /* Move Confirms down through Phi nodes. */
4644 if (mode_is_reference(mode)) {
4645 n = get_irn_arity(phi);
4647 /* Beware of Phi0 */
4649 ir_node *pred = get_irn_n(phi, 0);
4650 ir_node *bound, *new_phi, *block, **in;
4651 ir_relation relation;
4653 if (! is_Confirm(pred))
4656 bound = get_Confirm_bound(pred);
4657 relation = get_Confirm_relation(pred);
4659 NEW_ARR_A(ir_node *, in, n);
4660 in[0] = get_Confirm_value(pred);
4662 for (i = 1; i < n; ++i) {
4663 pred = get_irn_n(phi, i);
4665 if (! is_Confirm(pred) ||
4666 get_Confirm_bound(pred) != bound ||
4667 get_Confirm_relation(pred) != relation)
4669 in[i] = get_Confirm_value(pred);
4671 /* move the Confirm nodes "behind" the Phi */
4672 block = get_irn_n(phi, -1);
4673 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4674 return new_r_Confirm(block, new_phi, bound, relation);
4681 * Returns the operands of a commutative bin-op, if one operand is
4682 * a const, it is returned as the second one.
4684 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4686 ir_node *op_a = get_binop_left(binop);
4687 ir_node *op_b = get_binop_right(binop);
4689 assert(is_op_commutative(get_irn_op(binop)));
4691 if (is_Const(op_a)) {
4698 } /* get_comm_Binop_Ops */
4701 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4702 * Such pattern may arise in bitfield stores.
4704 * value c4 value c4 & c2
4705 * AND c3 AND c1 | c3
4712 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4715 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4717 ir_node *irn_and, *c1;
4719 ir_node *and_l, *c3;
4720 ir_node *value, *c4;
4721 ir_node *new_and, *new_const, *block;
4722 ir_mode *mode = get_irn_mode(irn_or);
4724 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4728 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4729 if (!is_Const(c1) || !is_And(irn_and))
4732 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4736 tv1 = get_Const_tarval(c1);
4737 tv2 = get_Const_tarval(c2);
4739 tv = tarval_or(tv1, tv2);
4740 if (tarval_is_all_one(tv)) {
4741 /* the AND does NOT clear a bit with isn't set by the OR */
4742 set_Or_left(irn_or, or_l);
4743 set_Or_right(irn_or, c1);
4745 /* check for more */
4752 get_comm_Binop_Ops(or_l, &and_l, &c3);
4753 if (!is_Const(c3) || !is_And(and_l))
4756 get_comm_Binop_Ops(and_l, &value, &c4);
4760 /* ok, found the pattern, check for conditions */
4761 assert(mode == get_irn_mode(irn_and));
4762 assert(mode == get_irn_mode(or_l));
4763 assert(mode == get_irn_mode(and_l));
4765 tv3 = get_Const_tarval(c3);
4766 tv4 = get_Const_tarval(c4);
4768 tv = tarval_or(tv4, tv2);
4769 if (!tarval_is_all_one(tv)) {
4770 /* have at least one 0 at the same bit position */
4774 if (tv3 != tarval_andnot(tv3, tv4)) {
4775 /* bit in the or_mask is outside the and_mask */
4779 if (tv1 != tarval_andnot(tv1, tv2)) {
4780 /* bit in the or_mask is outside the and_mask */
4784 /* ok, all conditions met */
4785 block = get_irn_n(irn_or, -1);
4786 irg = get_irn_irg(block);
4788 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4790 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4792 set_Or_left(irn_or, new_and);
4793 set_Or_right(irn_or, new_const);
4795 /* check for more */
4797 } /* transform_node_Or_bf_store */
4800 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4802 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4804 ir_mode *mode = get_irn_mode(irn_or);
4805 ir_node *shl, *shr, *block;
4806 ir_node *irn, *x, *c1, *c2, *n;
4807 ir_tarval *tv1, *tv2;
4809 /* some backends can't handle rotl */
4810 if (!be_get_backend_param()->support_rotl)
4813 if (! mode_is_int(mode))
4816 shl = get_binop_left(irn_or);
4817 shr = get_binop_right(irn_or);
4826 } else if (!is_Shl(shl)) {
4828 } else if (!is_Shr(shr)) {
4831 x = get_Shl_left(shl);
4832 if (x != get_Shr_left(shr))
4835 c1 = get_Shl_right(shl);
4836 c2 = get_Shr_right(shr);
4837 if (is_Const(c1) && is_Const(c2)) {
4838 tv1 = get_Const_tarval(c1);
4839 if (! tarval_is_long(tv1))
4842 tv2 = get_Const_tarval(c2);
4843 if (! tarval_is_long(tv2))
4846 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4847 != (int) get_mode_size_bits(mode))
4850 /* yet, condition met */
4851 block = get_nodes_block(irn_or);
4853 n = new_r_Rotl(block, x, c1, mode);
4855 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4859 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4860 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4861 if (!ir_is_negated_value(c1, c2)) {
4865 /* yet, condition met */
4866 block = get_nodes_block(irn_or);
4867 n = new_r_Rotl(block, x, c1, mode);
4868 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4870 } /* transform_node_Or_Rotl */
4872 static bool is_cmp_unequal(const ir_node *node)
4874 ir_relation relation = get_Cmp_relation(node);
4875 ir_node *left = get_Cmp_left(node);
4876 ir_node *right = get_Cmp_right(node);
4877 ir_mode *mode = get_irn_mode(left);
4879 if (relation == ir_relation_less_greater)
4882 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
4883 return relation == ir_relation_greater;
4890 static ir_node *transform_node_Or(ir_node *n)
4892 ir_node *c, *oldn = n;
4893 ir_node *a = get_Or_left(n);
4894 ir_node *b = get_Or_right(n);
4897 if (is_Not(a) && is_Not(b)) {
4898 /* ~a | ~b = ~(a&b) */
4899 ir_node *block = get_nodes_block(n);
4901 mode = get_irn_mode(n);
4904 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4905 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4906 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4910 /* we can combine the relations of two compares with the same operands */
4911 if (is_Cmp(a) && is_Cmp(b)) {
4912 ir_node *a_left = get_Cmp_left(a);
4913 ir_node *a_right = get_Cmp_left(a);
4914 ir_node *b_left = get_Cmp_left(b);
4915 ir_node *b_right = get_Cmp_right(b);
4916 if (a_left == b_left && b_left == b_right) {
4917 dbg_info *dbgi = get_irn_dbg_info(n);
4918 ir_node *block = get_nodes_block(n);
4919 ir_relation a_relation = get_Cmp_relation(a);
4920 ir_relation b_relation = get_Cmp_relation(b);
4921 ir_relation new_relation = a_relation | b_relation;
4922 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4924 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
4925 if (is_cmp_unequal(a) && is_cmp_unequal(b)
4926 && !mode_is_float(get_irn_mode(a_left))
4927 && !mode_is_float(get_irn_mode(b_left))) {
4928 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
4929 ir_graph *irg = get_irn_irg(n);
4930 dbg_info *dbgi = get_irn_dbg_info(n);
4931 ir_node *block = get_nodes_block(n);
4932 ir_mode *a_mode = get_irn_mode(a_left);
4933 ir_mode *b_mode = get_irn_mode(b_left);
4934 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
4935 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
4936 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
4937 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
4938 ir_node *zero = create_zero_const(irg, b_mode);
4939 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4941 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
4942 ir_graph *irg = get_irn_irg(n);
4943 dbg_info *dbgi = get_irn_dbg_info(n);
4944 ir_node *block = get_nodes_block(n);
4945 ir_mode *a_mode = get_irn_mode(a_left);
4946 ir_mode *b_mode = get_irn_mode(b_left);
4947 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
4948 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
4949 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
4950 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
4951 ir_node *zero = create_zero_const(irg, a_mode);
4952 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4957 mode = get_irn_mode(n);
4958 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4960 n = transform_node_Or_bf_store(n);
4961 n = transform_node_Or_Rotl(n);
4965 n = transform_bitwise_distributive(n, transform_node_Or);
4967 n = transform_node_bitop_shift(n);
4970 } /* transform_node_Or */
4974 static ir_node *transform_node(ir_node *n);
4977 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4979 * Should be moved to reassociation?
4981 static ir_node *transform_node_shift(ir_node *n)
4983 ir_node *left, *right;
4985 ir_mode *count_mode;
4986 ir_tarval *tv1, *tv2, *res;
4987 ir_node *in[2], *irn, *block;
4991 left = get_binop_left(n);
4993 /* different operations */
4994 if (get_irn_op(left) != get_irn_op(n))
4997 right = get_binop_right(n);
4998 tv1 = value_of(right);
4999 if (tv1 == tarval_bad)
5002 tv2 = value_of(get_binop_right(left));
5003 if (tv2 == tarval_bad)
5006 count_mode = get_tarval_mode(tv1);
5007 if (get_tarval_mode(tv2) != count_mode) {
5008 /* TODO: search bigger mode or something and convert... */
5012 mode = get_irn_mode(n);
5013 modulo_shf = get_mode_modulo_shift(mode);
5015 if (modulo_shf > 0) {
5016 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5018 /* I'm not so sure what happens in one complement... */
5019 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5020 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5021 * above will be invalid) */
5022 assert(modulo_shf<=0 || is_po2(modulo_shf));
5024 tv1 = tarval_and(tv1, modulo_mask);
5025 tv2 = tarval_and(tv2, modulo_mask);
5027 res = tarval_add(tv1, tv2);
5028 irg = get_irn_irg(n);
5030 /* beware: a simple replacement works only, if res < modulo shift */
5032 int bits = get_mode_size_bits(mode);
5033 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5034 res = tarval_mod(res, modulo);
5036 long bits = get_mode_size_bits(mode);
5037 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5039 /* shifting too much */
5040 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5042 ir_node *block = get_nodes_block(n);
5043 dbg_info *dbgi = get_irn_dbg_info(n);
5044 ir_mode *smode = get_irn_mode(right);
5045 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5046 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5049 return new_r_Const(irg, get_mode_null(mode));
5053 /* ok, we can replace it */
5054 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5055 block = get_nodes_block(n);
5057 in[0] = get_binop_left(left);
5058 in[1] = new_r_Const(irg, res);
5060 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5062 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5064 return transform_node(irn);
5069 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5071 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5072 * (also with x >>s c1 when c1>=c2)
5074 static ir_node *transform_node_shl_shr(ir_node *n)
5077 ir_node *right = get_binop_right(n);
5087 ir_tarval *tv_shift;
5090 ir_relation relation;
5093 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5095 if (!is_Const(right))
5098 left = get_binop_left(n);
5099 mode = get_irn_mode(n);
5100 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5101 ir_node *shr_right = get_binop_right(left);
5103 if (!is_Const(shr_right))
5106 x = get_binop_left(left);
5107 tv_shr = get_Const_tarval(shr_right);
5108 tv_shl = get_Const_tarval(right);
5110 if (is_Shrs(left)) {
5111 /* shrs variant only allowed if c1 >= c2 */
5112 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5115 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5118 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5120 tv_mask = tarval_shl(tv_mask, tv_shl);
5121 } else if (is_Shr(n) && is_Shl(left)) {
5122 ir_node *shl_right = get_Shl_right(left);
5124 if (!is_Const(shl_right))
5127 x = get_Shl_left(left);
5128 tv_shr = get_Const_tarval(right);
5129 tv_shl = get_Const_tarval(shl_right);
5131 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5132 tv_mask = tarval_shr(tv_mask, tv_shr);
5137 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5138 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5141 assert(tv_mask != tarval_bad);
5142 assert(get_tarval_mode(tv_mask) == mode);
5144 block = get_nodes_block(n);
5145 irg = get_irn_irg(block);
5146 dbgi = get_irn_dbg_info(n);
5148 relation = tarval_cmp(tv_shl, tv_shr);
5149 if (relation == ir_relation_less || relation == ir_relation_equal) {
5150 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5151 new_const = new_r_Const(irg, tv_shift);
5153 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5155 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5158 assert(relation == ir_relation_greater);
5159 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5160 new_const = new_r_Const(irg, tv_shift);
5161 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5164 new_const = new_r_Const(irg, tv_mask);
5165 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5170 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5172 ir_mode *mode = get_tarval_mode(tv);
5173 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5174 return tarval_mod(tv, modulo_tv);
5177 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5178 ir_node *left, ir_node *right, ir_mode *mode);
5181 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5182 * then we can use that to minimize the value of Add(x, const) or
5183 * Sub(Const, x). In particular this often avoids 1 instruction in some
5184 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5185 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5187 static ir_node *transform_node_shift_modulo(ir_node *n,
5188 new_shift_func new_shift)
5190 ir_mode *mode = get_irn_mode(n);
5191 int modulo = get_mode_modulo_shift(mode);
5192 ir_node *newop = NULL;
5193 ir_mode *mode_right;
5200 if (get_mode_arithmetic(mode) != irma_twos_complement)
5202 if (!is_po2(modulo))
5205 irg = get_irn_irg(n);
5206 block = get_nodes_block(n);
5207 right = get_binop_right(n);
5208 mode_right = get_irn_mode(right);
5209 if (is_Const(right)) {
5210 ir_tarval *tv = get_Const_tarval(right);
5211 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5216 newop = new_r_Const(irg, tv_mod);
5217 } else if (is_Add(right)) {
5218 ir_node *add_right = get_Add_right(right);
5219 if (is_Const(add_right)) {
5220 ir_tarval *tv = get_Const_tarval(add_right);
5221 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5226 newconst = new_r_Const(irg, tv_mod);
5227 newop = new_r_Add(block, get_Add_left(right), newconst,
5230 } else if (is_Sub(right)) {
5231 ir_node *sub_left = get_Sub_left(right);
5232 if (is_Const(sub_left)) {
5233 ir_tarval *tv = get_Const_tarval(sub_left);
5234 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5239 newconst = new_r_Const(irg, tv_mod);
5240 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5247 if (newop != NULL) {
5248 dbg_info *dbgi = get_irn_dbg_info(n);
5249 ir_node *left = get_binop_left(n);
5250 return new_shift(dbgi, block, left, newop, mode);
5258 static ir_node *transform_node_Shr(ir_node *n)
5260 ir_node *c, *oldn = n;
5261 ir_node *left = get_Shr_left(n);
5262 ir_node *right = get_Shr_right(n);
5263 ir_mode *mode = get_irn_mode(n);
5265 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5266 n = transform_node_shift(n);
5269 n = transform_node_shift_modulo(n, new_rd_Shr);
5271 n = transform_node_shl_shr(n);
5273 n = transform_node_shift_bitop(n);
5276 } /* transform_node_Shr */
5281 static ir_node *transform_node_Shrs(ir_node *n)
5283 ir_node *c, *oldn = n;
5284 ir_node *a = get_Shrs_left(n);
5285 ir_node *b = get_Shrs_right(n);
5286 ir_mode *mode = get_irn_mode(n);
5288 if (is_oversize_shift(n)) {
5289 ir_node *block = get_nodes_block(n);
5290 dbg_info *dbgi = get_irn_dbg_info(n);
5291 ir_mode *cmode = get_irn_mode(b);
5292 long val = get_mode_size_bits(cmode)-1;
5293 ir_graph *irg = get_irn_irg(n);
5294 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5295 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5298 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5299 n = transform_node_shift(n);
5302 n = transform_node_shift_modulo(n, new_rd_Shrs);
5304 n = transform_node_shift_bitop(n);
5307 } /* transform_node_Shrs */
5312 static ir_node *transform_node_Shl(ir_node *n)
5314 ir_node *c, *oldn = n;
5315 ir_node *a = get_Shl_left(n);
5316 ir_node *b = get_Shl_right(n);
5317 ir_mode *mode = get_irn_mode(n);
5319 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5320 n = transform_node_shift(n);
5323 n = transform_node_shift_modulo(n, new_rd_Shl);
5325 n = transform_node_shl_shr(n);
5327 n = transform_node_shift_bitop(n);
5330 } /* transform_node_Shl */
5335 static ir_node *transform_node_Rotl(ir_node *n)
5337 ir_node *c, *oldn = n;
5338 ir_node *a = get_Rotl_left(n);
5339 ir_node *b = get_Rotl_right(n);
5340 ir_mode *mode = get_irn_mode(n);
5342 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5343 n = transform_node_shift(n);
5346 n = transform_node_shift_bitop(n);
5349 } /* transform_node_Rotl */
5354 static ir_node *transform_node_Conv(ir_node *n)
5356 ir_node *c, *oldn = n;
5357 ir_mode *mode = get_irn_mode(n);
5358 ir_node *a = get_Conv_op(n);
5360 if (mode != mode_b && is_const_Phi(a)) {
5361 /* Do NOT optimize mode_b Conv's, this leads to remaining
5362 * Phib nodes later, because the conv_b_lower operation
5363 * is instantly reverted, when it tries to insert a Convb.
5365 c = apply_conv_on_phi(a, mode);
5367 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5372 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5373 ir_graph *irg = get_irn_irg(n);
5374 return new_r_Unknown(irg, mode);
5377 if (mode_is_reference(mode) &&
5378 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5380 ir_node *l = get_Add_left(a);
5381 ir_node *r = get_Add_right(a);
5382 dbg_info *dbgi = get_irn_dbg_info(a);
5383 ir_node *block = get_nodes_block(n);
5385 ir_node *lop = get_Conv_op(l);
5386 if (get_irn_mode(lop) == mode) {
5387 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5388 n = new_rd_Add(dbgi, block, lop, r, mode);
5393 ir_node *rop = get_Conv_op(r);
5394 if (get_irn_mode(rop) == mode) {
5395 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5396 n = new_rd_Add(dbgi, block, l, rop, mode);
5403 } /* transform_node_Conv */
5406 * Remove dead blocks and nodes in dead blocks
5407 * in keep alive list. We do not generate a new End node.
5409 static ir_node *transform_node_End(ir_node *n)
5411 int i, j, n_keepalives = get_End_n_keepalives(n);
5414 NEW_ARR_A(ir_node *, in, n_keepalives);
5416 for (i = j = 0; i < n_keepalives; ++i) {
5417 ir_node *ka = get_End_keepalive(n, i);
5419 /* no need to keep Bad */
5422 /* do not keep unreachable code */
5423 block = is_Block(ka) ? ka : get_nodes_block(ka);
5424 if (is_block_unreachable(block))
5428 if (j != n_keepalives)
5429 set_End_keepalives(n, j, in);
5431 } /* transform_node_End */
5433 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5435 if (is_Minus(a) && get_Minus_op(a) == b)
5437 if (is_Minus(b) && get_Minus_op(b) == a)
5439 if (is_Sub(a) && is_Sub(b)) {
5440 ir_node *a_left = get_Sub_left(a);
5441 ir_node *a_right = get_Sub_right(a);
5442 ir_node *b_left = get_Sub_left(b);
5443 ir_node *b_right = get_Sub_right(b);
5445 if (a_left == b_right && a_right == b_left)
5452 static const ir_node *skip_upconv(const ir_node *node)
5454 while (is_Conv(node)) {
5455 ir_mode *mode = get_irn_mode(node);
5456 const ir_node *op = get_Conv_op(node);
5457 ir_mode *op_mode = get_irn_mode(op);
5458 if (!smaller_mode(op_mode, mode))
5465 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_true,
5466 const ir_node *mux_false)
5471 ir_relation relation;
5477 * Note further that these optimization work even for floating point
5478 * with NaN's because -NaN == NaN.
5479 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5482 mode = get_irn_mode(mux_true);
5483 if (mode_honor_signed_zeros(mode))
5486 /* must be <, <=, >=, > */
5487 relation = get_Cmp_relation(sel);
5488 if ((relation & ir_relation_less_greater) == 0)
5491 if (!ir_is_negated_value(mux_true, mux_false))
5494 mux_true = skip_upconv(mux_true);
5495 mux_false = skip_upconv(mux_false);
5497 /* must be x cmp 0 */
5498 cmp_right = get_Cmp_right(sel);
5499 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5502 cmp_left = get_Cmp_left(sel);
5503 if (cmp_left == mux_false) {
5504 if (relation & ir_relation_less) {
5507 assert(relation & ir_relation_greater);
5510 } else if (cmp_left == mux_true) {
5511 if (relation & ir_relation_less) {
5514 assert(relation & ir_relation_greater);
5522 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_true,
5525 ir_node *cmp_left = get_Cmp_left(sel);
5526 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5530 * Optimize a Mux into some simpler cases.
5532 static ir_node *transform_node_Mux(ir_node *n)
5535 ir_node *sel = get_Mux_sel(n);
5536 ir_mode *mode = get_irn_mode(n);
5537 ir_node *t = get_Mux_true(n);
5538 ir_node *f = get_Mux_false(n);
5539 ir_graph *irg = get_irn_irg(n);
5541 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5542 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5543 int abs = ir_mux_is_abs(sel, t, f);
5545 dbg_info *dbgi = get_irn_dbg_info(n);
5546 ir_node *block = get_nodes_block(n);
5547 ir_node *op = ir_get_abs_op(sel, t, f);
5548 int bits = get_mode_size_bits(mode);
5549 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5550 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5551 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5554 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5556 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5562 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5566 ir_node* block = get_nodes_block(n);
5568 ir_node* c1 = get_Mux_sel(t);
5569 ir_node* t1 = get_Mux_true(t);
5570 ir_node* f1 = get_Mux_false(t);
5572 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5573 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5574 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5579 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5580 } else if (f == t1) {
5581 /* Mux(cond0, Mux(cond1, x, y), x) */
5582 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5583 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5584 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5589 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5591 } else if (is_Mux(f)) {
5592 ir_node* block = get_nodes_block(n);
5594 ir_node* c1 = get_Mux_sel(f);
5595 ir_node* t1 = get_Mux_true(f);
5596 ir_node* f1 = get_Mux_false(f);
5598 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5599 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5600 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5605 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5606 } else if (t == f1) {
5607 /* Mux(cond0, x, Mux(cond1, y, x)) */
5608 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5609 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5610 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5615 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5619 /* first normalization step: try to move a constant to the false side,
5620 * 0 preferred on false side too */
5621 if (is_Cmp(sel) && is_Const(t) &&
5622 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5623 dbg_info *seldbgi = get_irn_dbg_info(sel);
5624 ir_node *block = get_nodes_block(sel);
5625 ir_relation relation = get_Cmp_relation(sel);
5630 /* Mux(x, a, b) => Mux(not(x), b, a) */
5631 relation = get_negated_relation(relation);
5632 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5633 get_Cmp_right(sel), relation);
5634 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5637 /* note: after normalization, false can only happen on default */
5638 if (mode == mode_b) {
5639 dbg_info *dbg = get_irn_dbg_info(n);
5640 ir_node *block = get_nodes_block(n);
5643 ir_tarval *tv_t = get_Const_tarval(t);
5644 if (tv_t == tarval_b_true) {
5646 /* Muxb(sel, true, false) = sel */
5647 assert(get_Const_tarval(f) == tarval_b_false);
5648 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5651 /* Muxb(sel, true, x) = Or(sel, x) */
5652 n = new_rd_Or(dbg, block, sel, f, mode_b);
5653 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5657 } else if (is_Const(f)) {
5658 ir_tarval *tv_f = get_Const_tarval(f);
5659 if (tv_f == tarval_b_true) {
5660 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5661 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5662 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5663 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5666 /* Muxb(sel, x, false) = And(sel, x) */
5667 assert(tv_f == tarval_b_false);
5668 n = new_rd_And(dbg, block, sel, t, mode_b);
5669 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5675 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5676 * value to integer. */
5677 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5678 ir_tarval *a = get_Const_tarval(t);
5679 ir_tarval *b = get_Const_tarval(f);
5681 if (tarval_is_one(a) && tarval_is_null(b)) {
5682 ir_node *block = get_nodes_block(n);
5683 ir_node *conv = new_r_Conv(block, sel, mode);
5685 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5687 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5688 ir_node *block = get_nodes_block(n);
5689 ir_node *not_ = new_r_Not(block, sel, mode_b);
5690 ir_node *conv = new_r_Conv(block, not_, mode);
5692 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5698 ir_node *cmp_r = get_Cmp_right(sel);
5699 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5700 ir_node *block = get_nodes_block(n);
5701 ir_node *cmp_l = get_Cmp_left(sel);
5703 if (mode_is_int(mode)) {
5704 ir_relation relation = get_Cmp_relation(sel);
5706 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5707 /* Mux((a & b) != 0, c, 0) */
5708 ir_node *and_r = get_And_right(cmp_l);
5711 if (and_r == t && f == cmp_r) {
5712 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5713 if (relation == ir_relation_less_greater) {
5714 /* Mux((a & 2^C) != 0, 2^C, 0) */
5716 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5718 /* Mux((a & 2^C) == 0, 2^C, 0) */
5719 n = new_rd_Eor(get_irn_dbg_info(n),
5720 block, cmp_l, t, mode);
5721 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5726 if (is_Shl(and_r)) {
5727 ir_node *shl_l = get_Shl_left(and_r);
5728 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5729 if (and_r == t && f == cmp_r) {
5730 if (relation == ir_relation_less_greater) {
5731 /* (a & (1 << n)) != 0, (1 << n), 0) */
5733 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5735 /* (a & (1 << n)) == 0, (1 << n), 0) */
5736 n = new_rd_Eor(get_irn_dbg_info(n),
5737 block, cmp_l, t, mode);
5738 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5744 and_l = get_And_left(cmp_l);
5745 if (is_Shl(and_l)) {
5746 ir_node *shl_l = get_Shl_left(and_l);
5747 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5748 if (and_l == t && f == cmp_r) {
5749 if (relation == ir_relation_less_greater) {
5750 /* ((1 << n) & a) != 0, (1 << n), 0) */
5752 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5754 /* ((1 << n) & a) == 0, (1 << n), 0) */
5755 n = new_rd_Eor(get_irn_dbg_info(n),
5756 block, cmp_l, t, mode);
5757 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5772 * optimize Sync nodes that have other syncs as input we simply add the inputs
5773 * of the other sync to our own inputs
5775 static ir_node *transform_node_Sync(ir_node *n)
5777 int arity = get_Sync_n_preds(n);
5780 for (i = 0; i < arity;) {
5781 ir_node *pred = get_Sync_pred(n, i);
5785 /* Remove Bad predecessors */
5792 /* Remove duplicate predecessors */
5793 for (j = 0; j < i; ++j) {
5794 if (get_Sync_pred(n, j) == pred) {
5803 if (!is_Sync(pred)) {
5811 pred_arity = get_Sync_n_preds(pred);
5812 for (j = 0; j < pred_arity; ++j) {
5813 ir_node *pred_pred = get_Sync_pred(pred, j);
5818 add_irn_n(n, pred_pred);
5822 if (get_Sync_pred(n, k) == pred_pred) break;
5828 ir_graph *irg = get_irn_irg(n);
5829 return new_r_Bad(irg, mode_M);
5832 return get_Sync_pred(n, 0);
5835 /* rehash the sync node */
5840 static ir_node *transform_node_Load(ir_node *n)
5842 /* if our memory predecessor is a load from the same address, then reuse the
5843 * previous result */
5844 ir_node *mem = get_Load_mem(n);
5849 /* don't touch volatile loads */
5850 if (get_Load_volatility(n) == volatility_is_volatile)
5852 mem_pred = get_Proj_pred(mem);
5853 if (is_Load(mem_pred)) {
5854 ir_node *pred_load = mem_pred;
5856 /* conservatively compare the 2 loads. TODO: This could be less strict
5857 * with fixup code in some situations (like smaller/bigger modes) */
5858 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5860 if (get_Load_mode(pred_load) != get_Load_mode(n))
5862 /* all combinations of aligned/unaligned pred/n should be fine so we do
5863 * not compare the unaligned attribute */
5865 ir_node *block = get_nodes_block(n);
5866 ir_node *jmp = new_r_Jmp(block);
5867 ir_graph *irg = get_irn_irg(n);
5868 ir_node *bad = new_r_Bad(irg, mode_X);
5869 ir_mode *mode = get_Load_mode(n);
5870 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5871 ir_node *in[pn_Load_max+1] = { mem, res, jmp, bad };
5872 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5875 } else if (is_Store(mem_pred)) {
5876 ir_node *pred_store = mem_pred;
5877 ir_node *value = get_Store_value(pred_store);
5879 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5881 if (get_irn_mode(value) != get_Load_mode(n))
5883 /* all combinations of aligned/unaligned pred/n should be fine so we do
5884 * not compare the unaligned attribute */
5886 ir_node *block = get_nodes_block(n);
5887 ir_node *jmp = new_r_Jmp(block);
5888 ir_graph *irg = get_irn_irg(n);
5889 ir_node *bad = new_r_Bad(irg, mode_X);
5890 ir_node *res = value;
5891 ir_node *in[pn_Load_max+1] = { mem, res, jmp, bad };
5892 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5901 * optimize a trampoline Call into a direct Call
5903 static ir_node *transform_node_Call(ir_node *call)
5905 ir_node *callee = get_Call_ptr(call);
5906 ir_node *adr, *mem, *res, *bl, **in;
5907 ir_type *ctp, *mtp, *tp;
5911 size_t i, n_res, n_param;
5914 if (! is_Proj(callee))
5916 callee = get_Proj_pred(callee);
5917 if (! is_Builtin(callee))
5919 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5922 mem = get_Call_mem(call);
5924 if (skip_Proj(mem) == callee) {
5925 /* memory is routed to the trampoline, skip */
5926 mem = get_Builtin_mem(callee);
5929 /* build a new call type */
5930 mtp = get_Call_type(call);
5931 tdb = get_type_dbg_info(mtp);
5933 n_res = get_method_n_ress(mtp);
5934 n_param = get_method_n_params(mtp);
5935 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5937 for (i = 0; i < n_res; ++i)
5938 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5940 NEW_ARR_A(ir_node *, in, n_param + 1);
5942 /* FIXME: we don't need a new pointer type in every step */
5943 irg = get_irn_irg(call);
5944 tp = get_irg_frame_type(irg);
5945 tp = new_type_pointer(tp);
5946 set_method_param_type(ctp, 0, tp);
5948 in[0] = get_Builtin_param(callee, 2);
5949 for (i = 0; i < n_param; ++i) {
5950 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5951 in[i + 1] = get_Call_param(call, i);
5953 var = get_method_variadicity(mtp);
5954 set_method_variadicity(ctp, var);
5955 /* When we resolve a trampoline, the function must be called by a this-call */
5956 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5957 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5959 adr = get_Builtin_param(callee, 1);
5961 db = get_irn_dbg_info(call);
5962 bl = get_nodes_block(call);
5964 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5965 if (get_irn_pinned(call) == op_pin_state_floats)
5966 set_irn_pinned(res, op_pin_state_floats);
5968 } /* transform_node_Call */
5971 * Tries several [inplace] [optimizing] transformations and returns an
5972 * equivalent node. The difference to equivalent_node() is that these
5973 * transformations _do_ generate new nodes, and thus the old node must
5974 * not be freed even if the equivalent node isn't the old one.
5976 static ir_node *transform_node(ir_node *n)
5981 * Transform_node is the only "optimizing transformation" that might
5982 * return a node with a different opcode. We iterate HERE until fixpoint
5983 * to get the final result.
5987 if (n->op->ops.transform_node != NULL)
5988 n = n->op->ops.transform_node(n);
5989 } while (oldn != n);
5992 } /* transform_node */
5995 * Sets the default transform node operation for an ir_op_ops.
5997 * @param code the opcode for the default operation
5998 * @param ops the operations initialized
6003 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6007 ops->transform_node = transform_node_##a; \
6009 #define CASE_PROJ(a) \
6011 ops->transform_node_Proj = transform_node_Proj_##a; \
6013 #define CASE_PROJ_EX(a) \
6015 ops->transform_node = transform_node_##a; \
6016 ops->transform_node_Proj = transform_node_Proj_##a; \
6057 } /* firm_set_default_transform_node */
6060 /* **************** Common Subexpression Elimination **************** */
6062 /** The size of the hash table used, should estimate the number of nodes
6064 #define N_IR_NODES 512
6066 /** Compares the attributes of two Const nodes. */
6067 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6069 return get_Const_tarval(a) != get_Const_tarval(b);
6072 /** Compares the attributes of two Proj nodes. */
6073 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6075 return a->attr.proj.proj != b->attr.proj.proj;
6078 /** Compares the attributes of two Alloc nodes. */
6079 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6081 const alloc_attr *pa = &a->attr.alloc;
6082 const alloc_attr *pb = &b->attr.alloc;
6083 return (pa->where != pb->where) || (pa->type != pb->type);
6086 /** Compares the attributes of two Free nodes. */
6087 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6089 const free_attr *pa = &a->attr.free;
6090 const free_attr *pb = &b->attr.free;
6091 return (pa->where != pb->where) || (pa->type != pb->type);
6094 /** Compares the attributes of two SymConst nodes. */
6095 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6097 const symconst_attr *pa = &a->attr.symc;
6098 const symconst_attr *pb = &b->attr.symc;
6099 return (pa->kind != pb->kind)
6100 || (pa->sym.type_p != pb->sym.type_p);
6103 /** Compares the attributes of two Call nodes. */
6104 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6106 const call_attr *pa = &a->attr.call;
6107 const call_attr *pb = &b->attr.call;
6108 return (pa->type != pb->type)
6109 || (pa->tail_call != pb->tail_call);
6112 /** Compares the attributes of two Sel nodes. */
6113 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6115 const ir_entity *a_ent = get_Sel_entity(a);
6116 const ir_entity *b_ent = get_Sel_entity(b);
6117 return a_ent != b_ent;
6120 /** Compares the attributes of two Phi nodes. */
6121 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6123 /* we can only enter this function if both nodes have the same number of inputs,
6124 hence it is enough to check if one of them is a Phi0 */
6126 /* check the Phi0 pos attribute */
6127 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6132 /** Compares the attributes of two Conv nodes. */
6133 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6135 return get_Conv_strict(a) != get_Conv_strict(b);
6138 /** Compares the attributes of two Cast nodes. */
6139 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6141 return get_Cast_type(a) != get_Cast_type(b);
6144 /** Compares the attributes of two Load nodes. */
6145 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6147 if (get_Load_volatility(a) == volatility_is_volatile ||
6148 get_Load_volatility(b) == volatility_is_volatile)
6149 /* NEVER do CSE on volatile Loads */
6151 /* do not CSE Loads with different alignment. Be conservative. */
6152 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6155 return get_Load_mode(a) != get_Load_mode(b);
6158 /** Compares the attributes of two Store nodes. */
6159 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6161 /* do not CSE Stores with different alignment. Be conservative. */
6162 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6165 /* NEVER do CSE on volatile Stores */
6166 return (get_Store_volatility(a) == volatility_is_volatile ||
6167 get_Store_volatility(b) == volatility_is_volatile);
6170 /** Compares two exception attributes */
6171 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6173 const except_attr *ea = &a->attr.except;
6174 const except_attr *eb = &b->attr.except;
6176 return ea->pin_state != eb->pin_state;
6179 #define node_cmp_attr_Bound node_cmp_exception
6181 /** Compares the attributes of two Div nodes. */
6182 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6184 const div_attr *ma = &a->attr.div;
6185 const div_attr *mb = &b->attr.div;
6186 return ma->exc.pin_state != mb->exc.pin_state ||
6187 ma->resmode != mb->resmode ||
6188 ma->no_remainder != mb->no_remainder;
6191 /** Compares the attributes of two Mod nodes. */
6192 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6194 const mod_attr *ma = &a->attr.mod;
6195 const mod_attr *mb = &b->attr.mod;
6196 return ma->exc.pin_state != mb->exc.pin_state ||
6197 ma->resmode != mb->resmode;
6200 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6202 const cmp_attr *ma = &a->attr.cmp;
6203 const cmp_attr *mb = &b->attr.cmp;
6204 return ma->relation != mb->relation;
6207 /** Compares the attributes of two Confirm nodes. */
6208 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6210 const confirm_attr *ma = &a->attr.confirm;
6211 const confirm_attr *mb = &b->attr.confirm;
6212 return ma->relation != mb->relation;
6215 /** Compares the attributes of two Builtin nodes. */
6216 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6218 /* no need to compare the type, equal kind means equal type */
6219 return get_Builtin_kind(a) != get_Builtin_kind(b);
6222 /** Compares the attributes of two ASM nodes. */
6223 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6226 const ir_asm_constraint *ca;
6227 const ir_asm_constraint *cb;
6230 if (get_ASM_text(a) != get_ASM_text(b))
6233 /* Should we really check the constraints here? Should be better, but is strange. */
6234 n = get_ASM_n_input_constraints(a);
6235 if (n != get_ASM_n_input_constraints(b))
6238 ca = get_ASM_input_constraints(a);
6239 cb = get_ASM_input_constraints(b);
6240 for (i = 0; i < n; ++i) {
6241 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6242 || ca[i].mode != cb[i].mode)
6246 n = get_ASM_n_output_constraints(a);
6247 if (n != get_ASM_n_output_constraints(b))
6250 ca = get_ASM_output_constraints(a);
6251 cb = get_ASM_output_constraints(b);
6252 for (i = 0; i < n; ++i) {
6253 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6254 || ca[i].mode != cb[i].mode)
6258 n = get_ASM_n_clobbers(a);
6259 if (n != get_ASM_n_clobbers(b))
6262 cla = get_ASM_clobbers(a);
6263 clb = get_ASM_clobbers(b);
6264 for (i = 0; i < n; ++i) {
6265 if (cla[i] != clb[i])
6271 /** Compares the inexistent attributes of two Dummy nodes. */
6272 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6280 * Set the default node attribute compare operation for an ir_op_ops.
6282 * @param code the opcode for the default operation
6283 * @param ops the operations initialized
6288 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6292 ops->node_cmp_attr = node_cmp_attr_##a; \
6324 } /* firm_set_default_node_cmp_attr */
6327 * Compare function for two nodes in the value table. Gets two
6328 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6330 int identities_cmp(const void *elt, const void *key)
6332 ir_node *a = (ir_node *)elt;
6333 ir_node *b = (ir_node *)key;
6336 if (a == b) return 0;
6338 if ((get_irn_op(a) != get_irn_op(b)) ||
6339 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6341 /* compare if a's in and b's in are of equal length */
6342 irn_arity_a = get_irn_arity(a);
6343 if (irn_arity_a != get_irn_arity(b))
6346 /* blocks are never the same */
6350 if (get_irn_pinned(a) == op_pin_state_pinned) {
6351 /* for pinned nodes, the block inputs must be equal */
6352 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6355 ir_node *block_a = get_nodes_block(a);
6356 ir_node *block_b = get_nodes_block(b);
6357 if (! get_opt_global_cse()) {
6358 /* for block-local CSE both nodes must be in the same Block */
6359 if (block_a != block_b)
6362 /* The optimistic approach would be to do nothing here.
6363 * However doing GCSE optimistically produces a lot of partially dead code which appears
6364 * to be worse in practice than the missed opportunities.
6365 * So we use a very conservative variant here and only CSE if 1 value dominates the
6367 if (!block_dominates(block_a, block_b)
6368 && !block_dominates(block_b, block_a))
6373 /* compare a->in[0..ins] with b->in[0..ins] */
6374 for (i = 0; i < irn_arity_a; ++i) {
6375 ir_node *pred_a = get_irn_n(a, i);
6376 ir_node *pred_b = get_irn_n(b, i);
6377 if (pred_a != pred_b) {
6378 /* if both predecessors are CSE neutral they might be different */
6379 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6385 * here, we already now that the nodes are identical except their
6388 if (a->op->ops.node_cmp_attr)
6389 return a->op->ops.node_cmp_attr(a, b);
6392 } /* identities_cmp */
6395 * Calculate a hash value of a node.
6397 * @param node The IR-node
6399 unsigned ir_node_hash(const ir_node *node)
6401 return node->op->ops.hash(node);
6402 } /* ir_node_hash */
6405 void new_identities(ir_graph *irg)
6407 if (irg->value_table != NULL)
6408 del_pset(irg->value_table);
6409 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6410 } /* new_identities */
6412 void del_identities(ir_graph *irg)
6414 if (irg->value_table != NULL)
6415 del_pset(irg->value_table);
6416 } /* del_identities */
6418 /* Normalize a node by putting constants (and operands with larger
6419 * node index) on the right (operator side). */
6420 void ir_normalize_node(ir_node *n)
6422 if (is_op_commutative(get_irn_op(n))) {
6423 ir_node *l = get_binop_left(n);
6424 ir_node *r = get_binop_right(n);
6426 /* For commutative operators perform a OP b == b OP a but keep
6427 * constants on the RIGHT side. This helps greatly in some
6428 * optimizations. Moreover we use the idx number to make the form
6430 if (!operands_are_normalized(l, r)) {
6431 set_binop_left(n, r);
6432 set_binop_right(n, l);
6436 } /* ir_normalize_node */
6439 * Return the canonical node computing the same value as n.
6440 * Looks up the node in a hash table, enters it in the table
6441 * if it isn't there yet.
6443 * @param n the node to look up
6445 * @return a node that computes the same value as n or n if no such
6446 * node could be found
6448 ir_node *identify_remember(ir_node *n)
6450 ir_graph *irg = get_irn_irg(n);
6451 pset *value_table = irg->value_table;
6454 if (value_table == NULL)
6457 ir_normalize_node(n);
6458 /* lookup or insert in hash table with given hash key. */
6459 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6462 /* n is reachable again */
6463 edges_node_revival(nn);
6467 } /* identify_remember */
6470 * During construction we set the op_pin_state_pinned flag in the graph right
6471 * when the optimization is performed. The flag turning on procedure global
6472 * cse could be changed between two allocations. This way we are safe.
6474 * @param n The node to lookup
6476 static inline ir_node *identify_cons(ir_node *n)
6480 n = identify_remember(n);
6481 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6482 ir_graph *irg = get_irn_irg(n);
6483 set_irg_pinned(irg, op_pin_state_floats);
6486 } /* identify_cons */
6488 /* Add a node to the identities value table. */
6489 void add_identities(ir_node *node)
6496 identify_remember(node);
6499 /* Visit each node in the value table of a graph. */
6500 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6503 ir_graph *rem = current_ir_graph;
6505 current_ir_graph = irg;
6506 foreach_pset(irg->value_table, ir_node*, node) {
6509 current_ir_graph = rem;
6510 } /* visit_all_identities */
6513 * These optimizations deallocate nodes from the obstack.
6514 * It can only be called if it is guaranteed that no other nodes
6515 * reference this one, i.e., right after construction of a node.
6517 * @param n The node to optimize
6519 ir_node *optimize_node(ir_node *n)
6522 ir_graph *irg = get_irn_irg(n);
6523 unsigned iro = get_irn_opcode(n);
6526 /* Always optimize Phi nodes: part of the construction. */
6527 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6529 /* constant expression evaluation / constant folding */
6530 if (get_opt_constant_folding()) {
6531 /* neither constants nor Tuple values can be evaluated */
6532 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6533 /* try to evaluate */
6534 tv = computed_value(n);
6535 if (tv != tarval_bad) {
6540 * we MUST copy the node here temporarily, because it's still
6541 * needed for DBG_OPT_CSTEVAL
6543 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6544 oldn = (ir_node*)alloca(node_size);
6546 memcpy(oldn, n, node_size);
6547 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6549 /* ARG, copy the in array, we need it for statistics */
6550 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6552 /* note the inplace edges module */
6553 edges_node_deleted(n);
6555 /* evaluation was successful -- replace the node. */
6556 irg_kill_node(irg, n);
6557 nw = new_r_Const(irg, tv);
6559 DBG_OPT_CSTEVAL(oldn, nw);
6565 /* remove unnecessary nodes */
6566 if (get_opt_algebraic_simplification() ||
6567 (iro == iro_Phi) || /* always optimize these nodes. */
6569 (iro == iro_Proj) ||
6570 (iro == iro_Block) ) /* Flags tested local. */
6571 n = equivalent_node(n);
6573 /* Common Subexpression Elimination.
6575 * Checks whether n is already available.
6576 * The block input is used to distinguish different subexpressions. Right
6577 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6578 * subexpressions within a block.
6581 n = identify_cons(n);
6584 edges_node_deleted(oldn);
6586 /* We found an existing, better node, so we can deallocate the old node. */
6587 irg_kill_node(irg, oldn);
6591 /* Some more constant expression evaluation that does not allow to
6593 iro = get_irn_opcode(n);
6594 if (get_opt_algebraic_simplification() ||
6595 (iro == iro_Cond) ||
6596 (iro == iro_Proj)) /* Flags tested local. */
6597 n = transform_node(n);
6599 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6600 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6602 n = identify_remember(o);
6608 } /* optimize_node */
6612 * These optimizations never deallocate nodes (in place). This can cause dead
6613 * nodes lying on the obstack. Remove these by a dead node elimination,
6614 * i.e., a copying garbage collection.
6616 ir_node *optimize_in_place_2(ir_node *n)
6620 unsigned iro = get_irn_opcode(n);
6622 if (!get_opt_optimize() && !is_Phi(n)) return n;
6624 if (iro == iro_Deleted)
6627 /* constant expression evaluation / constant folding */
6628 if (get_opt_constant_folding()) {
6629 /* neither constants nor Tuple values can be evaluated */
6630 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6631 /* try to evaluate */
6632 tv = computed_value(n);
6633 if (tv != tarval_bad) {
6634 /* evaluation was successful -- replace the node. */
6635 ir_graph *irg = get_irn_irg(n);
6637 n = new_r_Const(irg, tv);
6639 DBG_OPT_CSTEVAL(oldn, n);
6645 /* remove unnecessary nodes */
6646 if (get_opt_constant_folding() ||
6647 (iro == iro_Phi) || /* always optimize these nodes. */
6648 (iro == iro_Id) || /* ... */
6649 (iro == iro_Proj) || /* ... */
6650 (iro == iro_Block) ) /* Flags tested local. */
6651 n = equivalent_node(n);
6653 /** common subexpression elimination **/
6654 /* Checks whether n is already available. */
6655 /* The block input is used to distinguish different subexpressions. Right
6656 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6657 subexpressions within a block. */
6658 if (get_opt_cse()) {
6660 n = identify_remember(o);
6665 /* Some more constant expression evaluation. */
6666 iro = get_irn_opcode(n);
6667 if (get_opt_constant_folding() ||
6668 (iro == iro_Cond) ||
6669 (iro == iro_Proj)) /* Flags tested local. */
6670 n = transform_node(n);
6672 /* Now we can verify the node, as it has no dead inputs any more. */
6675 /* Now we have a legal, useful node. Enter it in hash table for cse.
6676 Blocks should be unique anyways. (Except the successor of start:
6677 is cse with the start block!) */
6678 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6680 n = identify_remember(o);
6686 } /* optimize_in_place_2 */
6689 * Wrapper for external use, set proper status bits after optimization.
6691 ir_node *optimize_in_place(ir_node *n)
6693 ir_graph *irg = get_irn_irg(n);
6694 /* Handle graph state */
6695 assert(get_irg_phase_state(irg) != phase_building);
6697 if (get_opt_global_cse())
6698 set_irg_pinned(irg, op_pin_state_floats);
6700 /* FIXME: Maybe we could also test whether optimizing the node can
6701 change the control graph. */
6702 set_irg_doms_inconsistent(irg);
6703 return optimize_in_place_2(n);
6704 } /* optimize_in_place */
6707 * Calculate a hash value of a Const node.
6709 static unsigned hash_Const(const ir_node *node)
6713 /* special value for const, as they only differ in their tarval. */
6714 h = HASH_PTR(node->attr.con.tarval);
6720 * Calculate a hash value of a SymConst node.
6722 static unsigned hash_SymConst(const ir_node *node)
6726 /* all others are pointers */
6727 h = HASH_PTR(node->attr.symc.sym.type_p);
6730 } /* hash_SymConst */
6733 * Set the default hash operation in an ir_op_ops.
6735 * @param code the opcode for the default operation
6736 * @param ops the operations initialized
6741 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6745 ops->hash = hash_##a; \
6748 /* hash function already set */
6749 if (ops->hash != NULL)
6756 /* use input/mode default hash if no function was given */
6757 ops->hash = firm_default_hash;
6765 * Sets the default operation for an ir_ops.
6767 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6769 ops = firm_set_default_hash(code, ops);
6770 ops = firm_set_default_computed_value(code, ops);
6771 ops = firm_set_default_equivalent_node(code, ops);
6772 ops = firm_set_default_transform_node(code, ops);
6773 ops = firm_set_default_node_cmp_attr(code, ops);
6774 ops = firm_set_default_get_type_attr(code, ops);
6775 ops = firm_set_default_get_entity_attr(code, ops);
6778 } /* firm_set_default_operations */