2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
38 #include "iroptimize.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
46 #include "opt_polymorphy.h"
51 #include "firm_types.h"
52 #include "bitfiddle.h"
55 /* Make types visible to allow most efficient access */
58 static bool is_Or_Eor_Add(const ir_node *node)
60 if (is_Or(node) || is_Eor(node) || is_Add(node)) {
61 ir_node *left = get_binop_left(node);
62 ir_node *right = get_binop_right(node);
63 vrp_attr *vrp_left = vrp_get_info(left);
64 vrp_attr *vrp_right = vrp_get_info(right);
65 if (vrp_left != NULL && vrp_right != NULL) {
67 = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
68 return tarval_is_null(vrp_val);
75 * Returns the tarval of a Const node or tarval_bad for all other nodes.
77 static ir_tarval *default_value_of(const ir_node *n)
80 return get_Const_tarval(n); /* might return tarval_bad */
85 value_of_func value_of_ptr = default_value_of;
87 /* * Set a new value_of function. */
88 void set_value_of_func(value_of_func func)
93 value_of_ptr = default_value_of;
97 * Return the value of a Constant.
99 static ir_tarval *computed_value_Const(const ir_node *n)
101 return get_Const_tarval(n);
105 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
107 static ir_tarval *computed_value_SymConst(const ir_node *n)
112 switch (get_SymConst_kind(n)) {
113 case symconst_type_size:
114 type = get_SymConst_type(n);
115 if (get_type_state(type) == layout_fixed)
116 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
118 case symconst_type_align:
119 type = get_SymConst_type(n);
120 if (get_type_state(type) == layout_fixed)
121 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
123 case symconst_ofs_ent:
124 ent = get_SymConst_entity(n);
125 type = get_entity_owner(ent);
126 if (get_type_state(type) == layout_fixed)
127 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
136 * Return the value of an Add.
138 static ir_tarval *computed_value_Add(const ir_node *n)
140 ir_node *a = get_Add_left(n);
141 ir_node *b = get_Add_right(n);
143 ir_tarval *ta = value_of(a);
144 ir_tarval *tb = value_of(b);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_add(ta, tb);
150 if ((is_Not(a) && get_Not_op(a) == b)
151 || (is_Not(b) && get_Not_op(b) == a)) {
152 return get_mode_all_one(get_irn_mode(n));
159 * Return the value of a Sub.
160 * Special case: a - a
162 static ir_tarval *computed_value_Sub(const ir_node *n)
164 ir_mode *mode = get_irn_mode(n);
165 ir_node *a = get_Sub_left(n);
166 ir_node *b = get_Sub_right(n);
171 if (! mode_is_float(mode)) {
174 return get_mode_null(mode);
180 if ((ta != tarval_bad) && (tb != tarval_bad))
181 return tarval_sub(ta, tb, mode);
187 * Return the value of a Carry.
188 * Special : a op 0, 0 op b
190 static ir_tarval *computed_value_Carry(const ir_node *n)
192 ir_node *a = get_binop_left(n);
193 ir_node *b = get_binop_right(n);
194 ir_mode *m = get_irn_mode(n);
195 ir_tarval *ta = value_of(a);
196 ir_tarval *tb = value_of(b);
198 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
202 if (tarval_is_null(ta) || tarval_is_null(tb))
203 return get_mode_null(m);
209 * Return the value of a Borrow.
212 static ir_tarval *computed_value_Borrow(const ir_node *n)
214 ir_node *a = get_binop_left(n);
215 ir_node *b = get_binop_right(n);
216 ir_mode *m = get_irn_mode(n);
217 ir_tarval *ta = value_of(a);
218 ir_tarval *tb = value_of(b);
220 if ((ta != tarval_bad) && (tb != tarval_bad)) {
221 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
222 } else if (tarval_is_null(ta)) {
223 return get_mode_null(m);
229 * Return the value of an unary Minus.
231 static ir_tarval *computed_value_Minus(const ir_node *n)
233 ir_node *a = get_Minus_op(n);
234 ir_tarval *ta = value_of(a);
236 if (ta != tarval_bad)
237 return tarval_neg(ta);
243 * Return the value of a Mul.
245 static ir_tarval *computed_value_Mul(const ir_node *n)
247 ir_node *a = get_Mul_left(n);
248 ir_node *b = get_Mul_right(n);
249 ir_tarval *ta = value_of(a);
250 ir_tarval *tb = value_of(b);
253 mode = get_irn_mode(n);
254 if (mode != get_irn_mode(a)) {
255 /* n * n = 2n bit multiplication */
256 ta = tarval_convert_to(ta, mode);
257 tb = tarval_convert_to(tb, mode);
260 if (ta != tarval_bad && tb != tarval_bad) {
261 return tarval_mul(ta, tb);
263 /* a * 0 != 0 if a == NaN or a == Inf */
264 if (!mode_is_float(mode)) {
265 /* a*0 = 0 or 0*b = 0 */
266 if (ta == get_mode_null(mode))
268 if (tb == get_mode_null(mode))
276 * Return the value of an And.
277 * Special case: a & 0, 0 & b
279 static ir_tarval *computed_value_And(const ir_node *n)
281 ir_node *a = get_And_left(n);
282 ir_node *b = get_And_right(n);
283 ir_tarval *ta = value_of(a);
284 ir_tarval *tb = value_of(b);
286 if ((ta != tarval_bad) && (tb != tarval_bad)) {
287 return tarval_and (ta, tb);
290 if (tarval_is_null(ta)) return ta;
291 if (tarval_is_null(tb)) return tb;
294 if ((is_Not(a) && get_Not_op(a) == b)
295 || (is_Not(b) && get_Not_op(b) == a)) {
296 return get_mode_null(get_irn_mode(n));
303 * Return the value of an Or.
304 * Special case: a | 1...1, 1...1 | b
306 static ir_tarval *computed_value_Or(const ir_node *n)
308 ir_node *a = get_Or_left(n);
309 ir_node *b = get_Or_right(n);
310 ir_tarval *ta = value_of(a);
311 ir_tarval *tb = value_of(b);
313 if ((ta != tarval_bad) && (tb != tarval_bad)) {
314 return tarval_or (ta, tb);
317 if (tarval_is_all_one(ta)) return ta;
318 if (tarval_is_all_one(tb)) return tb;
321 if ((is_Not(a) && get_Not_op(a) == b)
322 || (is_Not(b) && get_Not_op(b) == a)) {
323 return get_mode_all_one(get_irn_mode(n));
329 * Return the value of an Eor.
331 static ir_tarval *computed_value_Eor(const ir_node *n)
333 ir_node *a = get_Eor_left(n);
334 ir_node *b = get_Eor_right(n);
339 return get_mode_null(get_irn_mode(n));
341 if ((is_Not(a) && get_Not_op(a) == b)
342 || (is_Not(b) && get_Not_op(b) == a)) {
343 return get_mode_all_one(get_irn_mode(n));
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_eor(ta, tb);
356 * Return the value of a Not.
358 static ir_tarval *computed_value_Not(const ir_node *n)
360 ir_node *a = get_Not_op(n);
361 ir_tarval *ta = value_of(a);
363 if (ta != tarval_bad)
364 return tarval_not(ta);
370 * Tests whether a shift shifts more bits than available in the mode
372 static bool is_oversize_shift(const ir_node *n)
374 ir_node *count = get_binop_right(n);
375 ir_mode *mode = get_irn_mode(n);
376 ir_tarval *tv = value_of(count);
379 if (tv == tarval_bad)
381 if (!tarval_is_long(tv))
383 shiftval = get_tarval_long(tv);
384 modulo_shift = get_mode_modulo_shift(mode);
385 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
388 return shiftval >= (long)get_mode_size_bits(mode);
392 * Return the value of a Shl.
394 static ir_tarval *computed_value_Shl(const ir_node *n)
396 ir_node *a = get_Shl_left(n);
397 ir_node *b = get_Shl_right(n);
399 ir_tarval *ta = value_of(a);
400 ir_tarval *tb = value_of(b);
402 if ((ta != tarval_bad) && (tb != tarval_bad)) {
403 return tarval_shl(ta, tb);
406 if (is_oversize_shift(n))
407 return get_mode_null(get_irn_mode(n));
413 * Return the value of a Shr.
415 static ir_tarval *computed_value_Shr(const ir_node *n)
417 ir_node *a = get_Shr_left(n);
418 ir_node *b = get_Shr_right(n);
420 ir_tarval *ta = value_of(a);
421 ir_tarval *tb = value_of(b);
423 if ((ta != tarval_bad) && (tb != tarval_bad)) {
424 return tarval_shr(ta, tb);
426 if (is_oversize_shift(n))
427 return get_mode_null(get_irn_mode(n));
433 * Return the value of a Shrs.
435 static ir_tarval *computed_value_Shrs(const ir_node *n)
437 ir_node *a = get_Shrs_left(n);
438 ir_node *b = get_Shrs_right(n);
440 ir_tarval *ta = value_of(a);
441 ir_tarval *tb = value_of(b);
443 if ((ta != tarval_bad) && (tb != tarval_bad)) {
444 return tarval_shrs(ta, tb);
450 * Return the value of a Rotl.
452 static ir_tarval *computed_value_Rotl(const ir_node *n)
454 ir_node *a = get_Rotl_left(n);
455 ir_node *b = get_Rotl_right(n);
457 ir_tarval *ta = value_of(a);
458 ir_tarval *tb = value_of(b);
460 if ((ta != tarval_bad) && (tb != tarval_bad)) {
461 return tarval_rotl(ta, tb);
466 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
468 ir_mode *mode = get_irn_mode(node);
469 if (get_mode_arithmetic(mode) != irma_twos_complement
470 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
474 ir_node *count = get_Shl_right(node);
475 if (is_Const(count)) {
476 ir_tarval *tv = get_Const_tarval(count);
477 if (tarval_is_long(tv)) {
478 long shiftval = get_tarval_long(tv);
479 long destbits = get_mode_size_bits(dest_mode);
480 if (shiftval >= destbits
481 && shiftval < (long)get_mode_modulo_shift(mode))
487 ir_node *right = get_And_right(node);
488 if (is_Const(right)) {
489 ir_tarval *tv = get_Const_tarval(right);
490 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
491 return tarval_is_null(conved);
498 * Return the value of a Conv.
500 static ir_tarval *computed_value_Conv(const ir_node *n)
502 ir_node *a = get_Conv_op(n);
503 ir_tarval *ta = value_of(a);
504 ir_mode *mode = get_irn_mode(n);
506 if (ta != tarval_bad)
507 return tarval_convert_to(ta, get_irn_mode(n));
509 if (ir_zero_when_converted(a, mode))
510 return get_mode_null(mode);
516 * Calculate the value of a Mux: can be evaluated, if the
517 * sel and the right input are known.
519 static ir_tarval *computed_value_Mux(const ir_node *n)
521 ir_node *sel = get_Mux_sel(n);
522 ir_tarval *ts = value_of(sel);
524 if (ts == get_tarval_b_true()) {
525 ir_node *v = get_Mux_true(n);
528 else if (ts == get_tarval_b_false()) {
529 ir_node *v = get_Mux_false(n);
536 * Calculate the value of a Confirm: can be evaluated,
537 * if it has the form Confirm(x, '=', Const).
539 static ir_tarval *computed_value_Confirm(const ir_node *n)
541 if (get_Confirm_relation(n) == ir_relation_equal) {
542 ir_tarval *tv = value_of(get_Confirm_bound(n));
543 if (tv != tarval_bad)
546 return value_of(get_Confirm_value(n));
550 * gives a (conservative) estimation of possible relation when comparing
553 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
554 const ir_node *right)
556 ir_relation possible = ir_relation_true;
557 ir_tarval *tv_l = value_of(left);
558 ir_tarval *tv_r = value_of(right);
559 ir_mode *mode = get_irn_mode(left);
560 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
561 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
563 /* both values known - evaluate them */
564 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
565 possible = tarval_cmp(tv_l, tv_r);
566 /* we can return now, won't get any better */
569 /* a == a is never less or greater (but might be equal or unordered) */
571 possible &= ~ir_relation_less_greater;
572 /* unordered results only happen for float compares */
573 if (!mode_is_float(mode))
574 possible &= ~ir_relation_unordered;
575 /* values can never be less than the least representable number or
576 * greater than the greatest representable number */
578 possible &= ~ir_relation_greater;
580 possible &= ~ir_relation_less;
582 possible &= ~ir_relation_greater;
584 possible &= ~ir_relation_less;
585 /* maybe vrp can tell us more */
586 possible &= vrp_cmp(left, right);
587 /* Alloc nodes never return null (but throw an exception) */
588 if (is_Alloc(left) && tarval_is_null(tv_r))
589 possible &= ~ir_relation_equal;
590 /* stuff known through confirm nodes */
591 if (is_Confirm(left) && get_Confirm_bound(left) == right) {
592 possible &= get_Confirm_relation(left);
594 if (is_Confirm(right) && get_Confirm_bound(right) == left) {
595 ir_relation relation = get_Confirm_relation(right);
596 relation = get_inversed_relation(relation);
597 possible &= relation;
603 static ir_tarval *compute_cmp(const ir_node *cmp)
605 ir_node *left = get_Cmp_left(cmp);
606 ir_node *right = get_Cmp_right(cmp);
607 ir_relation possible = ir_get_possible_cmp_relations(left, right);
608 ir_relation relation = get_Cmp_relation(cmp);
610 /* if none of the requested relations is possible, return false */
611 if ((possible & relation) == ir_relation_false)
612 return tarval_b_false;
613 /* if possible relations are a subset of the requested ones return true */
614 if ((possible & ~relation) == ir_relation_false)
615 return tarval_b_true;
617 return computed_value_Cmp_Confirm(cmp, left, right, relation);
621 * Return the value of a Cmp.
623 * The basic idea here is to determine which relations are possible and which
624 * one are definitely impossible.
626 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
628 /* we can't construct Constb after lowering mode_b nodes */
629 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
632 return compute_cmp(cmp);
636 * Calculate the value of an integer Div.
637 * Special case: 0 / b
639 static ir_tarval *do_computed_value_Div(const ir_node *div)
641 const ir_node *a = get_Div_left(div);
642 const ir_node *b = get_Div_right(div);
643 const ir_mode *mode = get_Div_resmode(div);
644 ir_tarval *ta = value_of(a);
646 const ir_node *dummy;
648 /* cannot optimize 0 / b = 0 because of NaN */
649 if (!mode_is_float(mode)) {
650 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
651 return ta; /* 0 / b == 0 if b != 0 */
654 if (ta != tarval_bad && tb != tarval_bad)
655 return tarval_div(ta, tb);
660 * Calculate the value of an integer Mod of two nodes.
661 * Special case: a % 1
663 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
665 ir_tarval *ta = value_of(a);
666 ir_tarval *tb = value_of(b);
668 /* Compute a % 1 or c1 % c2 */
669 if (tarval_is_one(tb))
670 return get_mode_null(get_irn_mode(a));
671 if (ta != tarval_bad && tb != tarval_bad)
672 return tarval_mod(ta, tb);
677 * Return the value of a Proj(Div).
679 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
681 long proj_nr = get_Proj_proj(n);
682 if (proj_nr != pn_Div_res)
685 return do_computed_value_Div(get_Proj_pred(n));
689 * Return the value of a Proj(Mod).
691 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
693 long proj_nr = get_Proj_proj(n);
695 if (proj_nr == pn_Mod_res) {
696 const ir_node *mod = get_Proj_pred(n);
697 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
703 * Return the value of a Proj.
705 static ir_tarval *computed_value_Proj(const ir_node *proj)
707 ir_node *n = get_Proj_pred(proj);
709 if (n->op->ops.computed_value_Proj != NULL)
710 return n->op->ops.computed_value_Proj(proj);
715 * If the parameter n can be computed, return its value, else tarval_bad.
716 * Performs constant folding.
718 * @param n The node this should be evaluated
720 ir_tarval *computed_value(const ir_node *n)
722 vrp_attr *vrp = vrp_get_info(n);
723 if (vrp != NULL && vrp->bits_set == vrp->bits_not_set)
724 return vrp->bits_set;
726 if (n->op->ops.computed_value)
727 return n->op->ops.computed_value(n);
732 * Set the default computed_value evaluator in an ir_op_ops.
734 * @param code the opcode for the default operation
735 * @param ops the operations initialized
740 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
744 ops->computed_value = computed_value_##a; \
746 #define CASE_PROJ(a) \
748 ops->computed_value_Proj = computed_value_Proj_##a; \
786 * Optimize operations that are commutative and have neutral 0,
787 * so a op 0 = 0 op a = a.
789 static ir_node *equivalent_node_neutral_zero(ir_node *n)
793 ir_node *a = get_binop_left(n);
794 ir_node *b = get_binop_right(n);
799 /* After running compute_node there is only one constant predecessor.
800 Find this predecessors value and remember the other node: */
801 if ((tv = value_of(a)) != tarval_bad) {
803 } else if ((tv = value_of(b)) != tarval_bad) {
808 /* If this predecessors constant value is zero, the operation is
809 * unnecessary. Remove it.
811 * Beware: If n is a Add, the mode of on and n might be different
812 * which happens in this rare construction: NULL + 3.
813 * Then, a Conv would be needed which we cannot include here.
815 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
818 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
825 * Eor is commutative and has neutral 0.
827 static ir_node *equivalent_node_Eor(ir_node *n)
833 n = equivalent_node_neutral_zero(n);
834 if (n != oldn) return n;
837 b = get_Eor_right(n);
839 if (is_Eor(a) || is_Or_Eor_Add(a)) {
840 ir_node *aa = get_binop_left(a);
841 ir_node *ab = get_binop_right(a);
844 /* (a ^ b) ^ a -> b */
846 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
848 } else if (ab == b) {
849 /* (a ^ b) ^ b -> a */
851 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
855 if (is_Eor(b) || is_Or_Eor_Add(b)) {
856 ir_node *ba = get_binop_left(b);
857 ir_node *bb = get_binop_right(b);
860 /* a ^ (a ^ b) -> b */
862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
864 } else if (bb == a) {
865 /* a ^ (b ^ a) -> b */
867 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
875 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
877 * The second one looks strange, but this construct
878 * is used heavily in the LCC sources :-).
880 * Beware: The Mode of an Add may be different than the mode of its
881 * predecessors, so we could not return a predecessors in all cases.
883 static ir_node *equivalent_node_Add(ir_node *n)
886 ir_node *left, *right;
887 ir_mode *mode = get_irn_mode(n);
889 n = equivalent_node_neutral_zero(n);
893 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
894 if (mode_is_float(mode)) {
895 ir_graph *irg = get_irn_irg(n);
896 if (get_irg_fp_model(irg) & fp_strict_algebraic)
900 left = get_Add_left(n);
901 right = get_Add_right(n);
904 if (get_Sub_right(left) == right) {
907 n = get_Sub_left(left);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915 if (get_Sub_right(right) == left) {
918 n = get_Sub_left(right);
919 if (mode == get_irn_mode(n)) {
920 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
929 * optimize operations that are not commutative but have neutral 0 on left,
932 static ir_node *equivalent_node_left_zero(ir_node *n)
936 ir_node *a = get_binop_left(n);
937 ir_node *b = get_binop_right(n);
938 ir_tarval *tb = value_of(b);
940 if (tarval_is_null(tb)) {
943 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
948 #define equivalent_node_Shl equivalent_node_left_zero
949 #define equivalent_node_Shr equivalent_node_left_zero
950 #define equivalent_node_Shrs equivalent_node_left_zero
951 #define equivalent_node_Rotl equivalent_node_left_zero
954 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
956 * The second one looks strange, but this construct
957 * is used heavily in the LCC sources :-).
959 * Beware: The Mode of a Sub may be different than the mode of its
960 * predecessors, so we could not return a predecessors in all cases.
962 static ir_node *equivalent_node_Sub(ir_node *n)
966 ir_mode *mode = get_irn_mode(n);
969 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
970 if (mode_is_float(mode)) {
971 ir_graph *irg = get_irn_irg(n);
972 if (get_irg_fp_model(irg) & fp_strict_algebraic)
976 b = get_Sub_right(n);
979 /* Beware: modes might be different */
980 if (tarval_is_null(tb)) {
981 ir_node *a = get_Sub_left(n);
982 if (mode == get_irn_mode(a)) {
985 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
993 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
996 * -(-a) == a, but might overflow two times.
997 * We handle it anyway here but the better way would be a
998 * flag. This would be needed for Pascal for instance.
1000 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1003 ir_node *pred = get_unop_op(n);
1005 /* optimize symmetric unop */
1006 if (get_irn_op(pred) == get_irn_op(n)) {
1007 n = get_unop_op(pred);
1008 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1013 /** Optimize Not(Not(x)) == x. */
1014 #define equivalent_node_Not equivalent_node_idempotent_unop
1016 /** -(-x) == x ??? Is this possible or can --x raise an
1017 out of bounds exception if min =! max? */
1018 #define equivalent_node_Minus equivalent_node_idempotent_unop
1021 * Optimize a * 1 = 1 * a = a.
1023 static ir_node *equivalent_node_Mul(ir_node *n)
1026 ir_node *a = get_Mul_left(n);
1028 /* we can handle here only the n * n = n bit cases */
1029 if (get_irn_mode(n) == get_irn_mode(a)) {
1030 ir_node *b = get_Mul_right(n);
1034 * Mul is commutative and has again an other neutral element.
1035 * Constants are place right, so check this case first.
1038 if (tarval_is_one(tv)) {
1040 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1043 if (tarval_is_one(tv)) {
1045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1053 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1055 static ir_node *equivalent_node_Or(ir_node *n)
1059 ir_node *a = get_Or_left(n);
1060 ir_node *b = get_Or_right(n);
1064 n = a; /* idempotence */
1065 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1068 /* constants are normalized to right, check this side first */
1070 if (tarval_is_null(tv)) {
1072 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1076 if (tarval_is_null(tv)) {
1078 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1086 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1088 static ir_node *equivalent_node_And(ir_node *n)
1092 ir_node *a = get_And_left(n);
1093 ir_node *b = get_And_right(n);
1097 n = a; /* idempotence */
1098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1101 /* constants are normalized to right, check this side first */
1103 if (tarval_is_all_one(tv)) {
1105 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1108 if (tv != get_tarval_bad()) {
1109 ir_mode *mode = get_irn_mode(n);
1110 if (!mode_is_signed(mode) && is_Conv(a)) {
1111 ir_node *convop = get_Conv_op(a);
1112 ir_mode *convopmode = get_irn_mode(convop);
1113 if (!mode_is_signed(convopmode)) {
1114 /* Check Conv(all_one) & Const = all_one */
1115 ir_tarval *one = get_mode_all_one(convopmode);
1116 ir_tarval *conv = tarval_convert_to(one, mode);
1117 ir_tarval *and = tarval_and(conv, tv);
1119 if (tarval_is_all_one(and)) {
1120 /* Conv(X) & Const = X */
1122 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1129 if (tarval_is_all_one(tv)) {
1131 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1135 if ((is_Or(a) || is_Or_Eor_Add(a))
1136 && (b == get_binop_left(a) || b == get_binop_right(a))) {
1138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1142 if ((is_Or(b) || is_Or_Eor_Add(b))
1143 && (a == get_binop_left(b) || a == get_binop_right(b))) {
1145 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1152 * Try to remove useless Conv's:
1154 static ir_node *equivalent_node_Conv(ir_node *n)
1157 ir_node *a = get_Conv_op(n);
1159 ir_mode *n_mode = get_irn_mode(n);
1160 ir_mode *a_mode = get_irn_mode(a);
1163 if (n_mode == a_mode) { /* No Conv necessary */
1164 if (get_Conv_strict(n)) {
1167 /* neither Minus nor Confirm change the precision,
1168 so we can "look-through" */
1171 p = get_Minus_op(p);
1172 } else if (is_Confirm(p)) {
1173 p = get_Confirm_value(p);
1179 if (is_Conv(p) && get_Conv_strict(p)) {
1180 /* we known already, that a_mode == n_mode, and neither
1181 Minus change the mode, so the second Conv
1183 assert(get_irn_mode(p) == n_mode);
1185 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1189 ir_node *pred = get_Proj_pred(p);
1190 if (is_Load(pred)) {
1191 /* Loads always return with the exact precision of n_mode */
1192 assert(get_Load_mode(pred) == n_mode);
1194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1197 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1198 pred = get_Proj_pred(pred);
1199 if (is_Start(pred)) {
1200 /* Arguments always return with the exact precision,
1201 as strictConv's are place before Call -- if the
1202 caller was compiled with the same setting.
1203 Otherwise, the semantics is probably still right. */
1204 assert(get_irn_mode(p) == n_mode);
1206 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1212 /* special case: the immediate predecessor is also a Conv */
1213 if (! get_Conv_strict(a)) {
1214 /* first one is not strict, kick it */
1216 a_mode = get_irn_mode(a);
1220 /* else both are strict conv, second is superfluous */
1222 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1230 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1231 ir_node *b = get_Conv_op(a);
1232 ir_mode *b_mode = get_irn_mode(b);
1234 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1235 /* both are strict conv */
1236 if (smaller_mode(a_mode, n_mode)) {
1237 /* both are strict, but the first is smaller, so
1238 the second cannot remove more precision, remove the
1240 set_Conv_strict(n, 0);
1243 if (n_mode == b_mode) {
1244 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1245 if (n_mode == mode_b) {
1246 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1247 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1249 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1250 if (values_in_mode(b_mode, a_mode)) {
1251 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1252 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1257 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1258 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1259 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1260 unsigned float_mantissa = get_mode_mantissa_size(a_mode);
1262 if (float_mantissa >= int_mantissa) {
1264 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1269 if (smaller_mode(b_mode, a_mode)) {
1270 if (get_Conv_strict(n))
1271 set_Conv_strict(b, 1);
1272 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1273 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1283 * - fold Phi-nodes, iff they have only one predecessor except
1286 static ir_node *equivalent_node_Phi(ir_node *n)
1291 ir_node *first_val = NULL; /* to shutup gcc */
1293 if (!get_opt_optimize() &&
1294 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1297 n_preds = get_Phi_n_preds(n);
1299 /* Phi of dead Region without predecessors. */
1303 /* Find first non-self-referencing input */
1304 for (i = 0; i < n_preds; ++i) {
1305 first_val = get_Phi_pred(n, i);
1306 /* not self pointer */
1307 if (first_val != n) {
1308 /* then found first value. */
1313 /* search for rest of inputs, determine if any of these
1314 are non-self-referencing */
1315 while (++i < n_preds) {
1316 ir_node *scnd_val = get_Phi_pred(n, i);
1317 if (scnd_val != n && scnd_val != first_val) {
1322 if (i >= n_preds && !is_Dummy(first_val)) {
1323 /* Fold, if no multiple distinct non-self-referencing inputs */
1325 DBG_OPT_PHI(oldn, n);
1331 * Optimize Proj(Tuple).
1333 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1335 ir_node *oldn = proj;
1336 ir_node *tuple = get_Proj_pred(proj);
1338 /* Remove the Tuple/Proj combination. */
1339 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1340 DBG_OPT_TUPLE(oldn, tuple, proj);
1346 * Optimize a / 1 = a.
1348 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1350 ir_node *oldn = proj;
1351 ir_node *div = get_Proj_pred(proj);
1352 ir_node *b = get_Div_right(div);
1353 ir_tarval *tb = value_of(b);
1355 /* Div is not commutative. */
1356 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1357 switch (get_Proj_proj(proj)) {
1359 proj = get_Div_mem(div);
1360 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1364 proj = get_Div_left(div);
1365 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1369 /* we cannot replace the exception Proj's here, this is done in
1370 transform_node_Proj_Div() */
1378 * Optimize CopyB(mem, x, x) into a Nop.
1380 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1382 ir_node *oldn = proj;
1383 ir_node *copyb = get_Proj_pred(proj);
1384 ir_node *a = get_CopyB_dst(copyb);
1385 ir_node *b = get_CopyB_src(copyb);
1388 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1389 switch (get_Proj_proj(proj)) {
1391 proj = get_CopyB_mem(copyb);
1392 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1400 * Optimize Bounds(idx, idx, upper) into idx.
1402 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1404 ir_node *oldn = proj;
1405 ir_node *bound = get_Proj_pred(proj);
1406 ir_node *idx = get_Bound_index(bound);
1407 ir_node *pred = skip_Proj(idx);
1410 if (idx == get_Bound_lower(bound))
1412 else if (is_Bound(pred)) {
1414 * idx was Bounds checked previously, it is still valid if
1415 * lower <= pred_lower && pred_upper <= upper.
1417 ir_node *lower = get_Bound_lower(bound);
1418 ir_node *upper = get_Bound_upper(bound);
1419 if (get_Bound_lower(pred) == lower &&
1420 get_Bound_upper(pred) == upper) {
1422 * One could expect that we simply return the previous
1423 * Bound here. However, this would be wrong, as we could
1424 * add an exception Proj to a new location then.
1425 * So, we must turn in into a tuple.
1431 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1432 switch (get_Proj_proj(proj)) {
1434 DBG_OPT_EXC_REM(proj);
1435 proj = get_Bound_mem(bound);
1439 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1442 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1450 * Does all optimizations on nodes that must be done on its Projs
1451 * because of creating new nodes.
1453 static ir_node *equivalent_node_Proj(ir_node *proj)
1455 ir_node *n = get_Proj_pred(proj);
1456 if (n->op->ops.equivalent_node_Proj)
1457 return n->op->ops.equivalent_node_Proj(proj);
1464 static ir_node *equivalent_node_Id(ir_node *n)
1472 DBG_OPT_ID(oldn, n);
1479 static ir_node *equivalent_node_Mux(ir_node *n)
1481 ir_node *oldn = n, *sel = get_Mux_sel(n);
1483 ir_tarval *ts = value_of(sel);
1485 if (ts == tarval_bad && is_Cmp(sel)) {
1486 /* try again with a direct call to compute_cmp, as we don't care
1487 * about the MODEB_LOWERED flag here */
1488 ts = compute_cmp(sel);
1491 /* Mux(true, f, t) == t */
1492 if (ts == tarval_b_true) {
1493 n = get_Mux_true(n);
1494 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1497 /* Mux(false, f, t) == f */
1498 if (ts == tarval_b_false) {
1499 n = get_Mux_false(n);
1500 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1503 n_t = get_Mux_true(n);
1504 n_f = get_Mux_false(n);
1506 /* Mux(v, x, T) == x */
1507 if (is_Unknown(n_f)) {
1509 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1512 /* Mux(v, T, x) == x */
1513 if (is_Unknown(n_t)) {
1515 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1519 /* Mux(v, x, x) == x */
1522 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1525 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1526 ir_relation relation = get_Cmp_relation(sel);
1527 ir_node *f = get_Mux_false(n);
1528 ir_node *t = get_Mux_true(n);
1531 * Note further that these optimization work even for floating point
1532 * with NaN's because -NaN == NaN.
1533 * However, if +0 and -0 is handled differently, we cannot use the first one.
1535 ir_node *const cmp_l = get_Cmp_left(sel);
1536 ir_node *const cmp_r = get_Cmp_right(sel);
1539 case ir_relation_equal:
1540 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1541 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1543 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1548 case ir_relation_less_greater:
1549 case ir_relation_unordered_less_greater:
1550 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1551 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1553 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1562 * Note: normalization puts the constant on the right side,
1563 * so we check only one case.
1565 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1566 /* Mux(t CMP 0, X, t) */
1567 if (is_Minus(f) && get_Minus_op(f) == t) {
1568 /* Mux(t CMP 0, -t, t) */
1569 if (relation == ir_relation_equal) {
1570 /* Mux(t == 0, -t, t) ==> -t */
1572 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1573 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1574 /* Mux(t != 0, -t, t) ==> t */
1576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1586 * Remove Confirm nodes if setting is on.
1587 * Replace Confirms(x, '=', Constlike) by Constlike.
1589 static ir_node *equivalent_node_Confirm(ir_node *n)
1591 ir_node *pred = get_Confirm_value(n);
1592 ir_relation relation = get_Confirm_relation(n);
1594 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1596 * rare case: two identical Confirms one after another,
1597 * replace the second one with the first.
1600 pred = get_Confirm_value(n);
1606 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1607 * perform no actual computation, as, e.g., the Id nodes. It does not create
1608 * new nodes. It is therefore safe to free n if the node returned is not n.
1609 * If a node returns a Tuple we can not just skip it. If the size of the
1610 * in array fits, we transform n into a tuple (e.g., Div).
1612 ir_node *equivalent_node(ir_node *n)
1614 if (n->op->ops.equivalent_node)
1615 return n->op->ops.equivalent_node(n);
1620 * Sets the default equivalent node operation for an ir_op_ops.
1622 * @param code the opcode for the default operation
1623 * @param ops the operations initialized
1628 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1632 ops->equivalent_node = equivalent_node_##a; \
1634 #define CASE_PROJ(a) \
1636 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1673 * Returns non-zero if a node is a Phi node
1674 * with all predecessors constant.
1676 static int is_const_Phi(ir_node *n)
1680 if (! is_Phi(n) || get_irn_arity(n) == 0)
1682 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1683 if (! is_Const(get_irn_n(n, i)))
1689 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1690 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1693 * in reality eval_func should be tarval (*eval_func)() but incomplete
1694 * declarations are bad style and generate noisy warnings
1696 typedef void (*eval_func)(void);
1699 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1701 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1703 if (eval == (eval_func) tarval_sub) {
1704 tarval_sub_type func = (tarval_sub_type)eval;
1706 return func(a, b, mode);
1708 tarval_binop_type func = (tarval_binop_type)eval;
1715 * Apply an evaluator on a binop with a constant operators (and one Phi).
1717 * @param phi the Phi node
1718 * @param other the other operand
1719 * @param eval an evaluator function
1720 * @param mode the mode of the result, may be different from the mode of the Phi!
1721 * @param left if non-zero, other is the left operand, else the right
1723 * @return a new Phi node if the conversion was successful, NULL else
1725 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1731 int i, n = get_irn_arity(phi);
1733 NEW_ARR_A(void *, res, n);
1735 for (i = 0; i < n; ++i) {
1736 pred = get_irn_n(phi, i);
1737 tv = get_Const_tarval(pred);
1738 tv = do_eval(eval, other, tv, mode);
1740 if (tv == tarval_bad) {
1741 /* folding failed, bad */
1747 for (i = 0; i < n; ++i) {
1748 pred = get_irn_n(phi, i);
1749 tv = get_Const_tarval(pred);
1750 tv = do_eval(eval, tv, other, mode);
1752 if (tv == tarval_bad) {
1753 /* folding failed, bad */
1759 irg = get_irn_irg(phi);
1760 for (i = 0; i < n; ++i) {
1761 pred = get_irn_n(phi, i);
1762 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1764 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1768 * Apply an evaluator on a binop with two constant Phi.
1770 * @param a the left Phi node
1771 * @param b the right Phi node
1772 * @param eval an evaluator function
1773 * @param mode the mode of the result, may be different from the mode of the Phi!
1775 * @return a new Phi node if the conversion was successful, NULL else
1777 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1779 ir_tarval *tv_l, *tv_r, *tv;
1785 if (get_nodes_block(a) != get_nodes_block(b))
1788 n = get_irn_arity(a);
1789 NEW_ARR_A(void *, res, n);
1791 for (i = 0; i < n; ++i) {
1792 pred = get_irn_n(a, i);
1793 tv_l = get_Const_tarval(pred);
1794 pred = get_irn_n(b, i);
1795 tv_r = get_Const_tarval(pred);
1796 tv = do_eval(eval, tv_l, tv_r, mode);
1798 if (tv == tarval_bad) {
1799 /* folding failed, bad */
1804 irg = get_irn_irg(a);
1805 for (i = 0; i < n; ++i) {
1806 pred = get_irn_n(a, i);
1807 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1809 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1813 * Apply an evaluator on a unop with a constant operator (a Phi).
1815 * @param phi the Phi node
1816 * @param eval an evaluator function
1818 * @return a new Phi node if the conversion was successful, NULL else
1820 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1827 int i, n = get_irn_arity(phi);
1829 NEW_ARR_A(void *, res, n);
1830 for (i = 0; i < n; ++i) {
1831 pred = get_irn_n(phi, i);
1832 tv = get_Const_tarval(pred);
1835 if (tv == tarval_bad) {
1836 /* folding failed, bad */
1841 mode = get_irn_mode(phi);
1842 irg = get_irn_irg(phi);
1843 for (i = 0; i < n; ++i) {
1844 pred = get_irn_n(phi, i);
1845 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1847 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1851 * Apply a conversion on a constant operator (a Phi).
1853 * @param phi the Phi node
1855 * @return a new Phi node if the conversion was successful, NULL else
1857 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1863 int i, n = get_irn_arity(phi);
1865 NEW_ARR_A(void *, res, n);
1866 for (i = 0; i < n; ++i) {
1867 pred = get_irn_n(phi, i);
1868 tv = get_Const_tarval(pred);
1869 tv = tarval_convert_to(tv, mode);
1871 if (tv == tarval_bad) {
1872 /* folding failed, bad */
1877 irg = get_irn_irg(phi);
1878 for (i = 0; i < n; ++i) {
1879 pred = get_irn_n(phi, i);
1880 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1882 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1886 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1887 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1888 * If possible, remove the Conv's.
1890 static ir_node *transform_node_AddSub(ir_node *n)
1892 ir_mode *mode = get_irn_mode(n);
1894 if (mode_is_reference(mode)) {
1895 ir_node *left = get_binop_left(n);
1896 ir_node *right = get_binop_right(n);
1897 unsigned ref_bits = get_mode_size_bits(mode);
1899 if (is_Conv(left)) {
1900 ir_mode *lmode = get_irn_mode(left);
1901 unsigned bits = get_mode_size_bits(lmode);
1903 if (ref_bits == bits &&
1904 mode_is_int(lmode) &&
1905 get_mode_arithmetic(lmode) == irma_twos_complement) {
1906 ir_node *pre = get_Conv_op(left);
1907 ir_mode *pre_mode = get_irn_mode(pre);
1909 if (mode_is_int(pre_mode) &&
1910 get_mode_size_bits(pre_mode) == bits &&
1911 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1912 /* ok, this conv just changes to sign, moreover the calculation
1913 * is done with same number of bits as our address mode, so
1914 * we can ignore the conv as address calculation can be viewed
1915 * as either signed or unsigned
1917 set_binop_left(n, pre);
1922 if (is_Conv(right)) {
1923 ir_mode *rmode = get_irn_mode(right);
1924 unsigned bits = get_mode_size_bits(rmode);
1926 if (ref_bits == bits &&
1927 mode_is_int(rmode) &&
1928 get_mode_arithmetic(rmode) == irma_twos_complement) {
1929 ir_node *pre = get_Conv_op(right);
1930 ir_mode *pre_mode = get_irn_mode(pre);
1932 if (mode_is_int(pre_mode) &&
1933 get_mode_size_bits(pre_mode) == bits &&
1934 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1935 /* ok, this conv just changes to sign, moreover the calculation
1936 * is done with same number of bits as our address mode, so
1937 * we can ignore the conv as address calculation can be viewed
1938 * as either signed or unsigned
1940 set_binop_right(n, pre);
1945 /* let address arithmetic use unsigned modes */
1946 if (is_Const(right)) {
1947 ir_mode *rmode = get_irn_mode(right);
1949 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1950 /* convert a AddP(P, *s) into AddP(P, *u) */
1951 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1953 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1954 set_binop_right(n, pre);
1962 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1965 if (is_Const(b) && is_const_Phi(a)) { \
1966 /* check for Op(Phi, Const) */ \
1967 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1969 else if (is_Const(a) && is_const_Phi(b)) { \
1970 /* check for Op(Const, Phi) */ \
1971 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1973 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1974 /* check for Op(Phi, Phi) */ \
1975 c = apply_binop_on_2_phis(a, b, eval, mode); \
1978 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1983 #define HANDLE_UNOP_PHI(eval, a, c) \
1986 if (is_const_Phi(a)) { \
1987 /* check for Op(Phi) */ \
1988 c = apply_unop_on_phi(a, eval); \
1990 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1997 * Create a 0 constant of given mode.
1999 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2001 ir_tarval *tv = get_mode_null(mode);
2002 ir_node *cnst = new_r_Const(irg, tv);
2007 static bool is_shiftop(const ir_node *n)
2009 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2012 /* the order of the values is important! */
2013 typedef enum const_class {
2019 static const_class classify_const(const ir_node* n)
2021 if (is_Const(n)) return const_const;
2022 if (is_irn_constlike(n)) return const_like;
2027 * Determines whether r is more constlike or has a larger index (in that order)
2030 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
2032 const const_class l_order = classify_const(l);
2033 const const_class r_order = classify_const(r);
2035 l_order > r_order ||
2036 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
2039 static bool is_cmp_unequal(const ir_node *node)
2041 ir_relation relation = get_Cmp_relation(node);
2042 ir_node *left = get_Cmp_left(node);
2043 ir_node *right = get_Cmp_right(node);
2044 ir_mode *mode = get_irn_mode(left);
2046 if (relation == ir_relation_less_greater)
2049 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
2050 return relation == ir_relation_greater;
2055 * returns true for Cmp(x == 0) or Cmp(x != 0)
2057 static bool is_cmp_equality_zero(const ir_node *node)
2059 ir_relation relation;
2060 ir_node *right = get_Cmp_right(node);
2062 if (!is_Const(right) || !is_Const_null(right))
2064 relation = get_Cmp_relation(node);
2065 return relation == ir_relation_equal
2066 || relation == ir_relation_less_greater
2067 || (!mode_is_signed(get_irn_mode(right))
2068 && relation == ir_relation_greater);
2072 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2073 * Such pattern may arise in bitfield stores.
2075 * value c4 value c4 & c2
2076 * AND c3 AND c1 | c3
2083 * AND c1 ===> OR if (c1 | c2) == 0x111..11
2086 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
2088 ir_node *irn_and, *c1;
2090 ir_node *and_l, *c3;
2091 ir_node *value, *c4;
2092 ir_node *new_and, *new_const, *block;
2093 ir_mode *mode = get_irn_mode(irn_or);
2095 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
2099 irn_and = get_binop_left(irn_or);
2100 c1 = get_binop_right(irn_or);
2101 if (!is_Const(c1) || !is_And(irn_and))
2104 or_l = get_binop_left(irn_and);
2105 c2 = get_binop_right(irn_and);
2109 tv1 = get_Const_tarval(c1);
2110 tv2 = get_Const_tarval(c2);
2112 tv = tarval_or(tv1, tv2);
2113 if (tarval_is_all_one(tv)) {
2114 /* the AND does NOT clear a bit with isn't set by the OR */
2115 set_binop_left(irn_or, or_l);
2116 set_binop_right(irn_or, c1);
2118 /* check for more */
2122 if (!is_Or(or_l) && !is_Or_Eor_Add(or_l))
2125 and_l = get_binop_left(or_l);
2126 c3 = get_binop_right(or_l);
2127 if (!is_Const(c3) || !is_And(and_l))
2130 value = get_binop_left(and_l);
2131 c4 = get_binop_right(and_l);
2135 /* ok, found the pattern, check for conditions */
2136 assert(mode == get_irn_mode(irn_and));
2137 assert(mode == get_irn_mode(or_l));
2138 assert(mode == get_irn_mode(and_l));
2140 tv3 = get_Const_tarval(c3);
2141 tv4 = get_Const_tarval(c4);
2143 tv = tarval_or(tv4, tv2);
2144 if (!tarval_is_all_one(tv)) {
2145 /* have at least one 0 at the same bit position */
2149 if (tv3 != tarval_andnot(tv3, tv4)) {
2150 /* bit in the or_mask is outside the and_mask */
2154 if (tv1 != tarval_andnot(tv1, tv2)) {
2155 /* bit in the or_mask is outside the and_mask */
2159 /* ok, all conditions met */
2160 block = get_irn_n(irn_or, -1);
2161 irg = get_irn_irg(block);
2163 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
2165 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
2167 set_binop_left(irn_or, new_and);
2168 set_binop_right(irn_or, new_const);
2170 /* check for more */
2175 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
2177 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
2179 ir_mode *mode = get_irn_mode(irn_or);
2180 ir_node *shl, *shr, *block;
2181 ir_node *irn, *x, *c1, *c2, *n;
2182 ir_tarval *tv1, *tv2;
2184 /* some backends can't handle rotl */
2185 if (!be_get_backend_param()->support_rotl)
2188 if (! mode_is_int(mode))
2191 shl = get_binop_left(irn_or);
2192 shr = get_binop_right(irn_or);
2201 } else if (!is_Shl(shl)) {
2203 } else if (!is_Shr(shr)) {
2206 x = get_Shl_left(shl);
2207 if (x != get_Shr_left(shr))
2210 c1 = get_Shl_right(shl);
2211 c2 = get_Shr_right(shr);
2212 if (is_Const(c1) && is_Const(c2)) {
2213 tv1 = get_Const_tarval(c1);
2214 if (! tarval_is_long(tv1))
2217 tv2 = get_Const_tarval(c2);
2218 if (! tarval_is_long(tv2))
2221 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2222 != (int) get_mode_size_bits(mode))
2225 /* yet, condition met */
2226 block = get_nodes_block(irn_or);
2228 n = new_r_Rotl(block, x, c1, mode);
2230 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
2234 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
2235 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
2236 if (!ir_is_negated_value(c1, c2)) {
2240 /* yet, condition met */
2241 block = get_nodes_block(irn_or);
2242 n = new_r_Rotl(block, x, c1, mode);
2243 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
2248 * Prototype of a recursive transform function
2249 * for bitwise distributive transformations.
2251 typedef ir_node* (*recursive_transform)(ir_node *n);
2254 * makes use of distributive laws for and, or, eor
2255 * and(a OP c, b OP c) -> and(a, b) OP c
2256 * note, might return a different op than n
2258 static ir_node *transform_bitwise_distributive(ir_node *n,
2259 recursive_transform trans_func)
2262 ir_node *a = get_binop_left(n);
2263 ir_node *b = get_binop_right(n);
2264 ir_op *op = get_irn_op(a);
2265 ir_op *op_root = get_irn_op(n);
2267 if (op != get_irn_op(b))
2270 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2271 if (op == op_Conv) {
2272 ir_node *a_op = get_Conv_op(a);
2273 ir_node *b_op = get_Conv_op(b);
2274 ir_mode *a_mode = get_irn_mode(a_op);
2275 ir_mode *b_mode = get_irn_mode(b_op);
2276 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2277 ir_node *blk = get_nodes_block(n);
2280 set_binop_left(n, a_op);
2281 set_binop_right(n, b_op);
2282 set_irn_mode(n, a_mode);
2284 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2286 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2292 /* nothing to gain here */
2296 if (op == op_Shrs || op == op_Shr || op == op_Shl
2297 || op == op_And || op == op_Or || op == op_Eor) {
2298 ir_node *a_left = get_binop_left(a);
2299 ir_node *a_right = get_binop_right(a);
2300 ir_node *b_left = get_binop_left(b);
2301 ir_node *b_right = get_binop_right(b);
2303 ir_node *op1 = NULL;
2304 ir_node *op2 = NULL;
2306 if (is_op_commutative(op)) {
2307 if (a_left == b_left) {
2311 } else if (a_left == b_right) {
2315 } else if (a_right == b_left) {
2321 if (a_right == b_right) {
2328 /* (a sop c) & (b sop c) => (a & b) sop c */
2329 ir_node *blk = get_nodes_block(n);
2331 ir_node *new_n = exact_copy(n);
2332 set_binop_left(new_n, op1);
2333 set_binop_right(new_n, op2);
2334 new_n = trans_func(new_n);
2336 if (op_root == op_Eor && op == op_Or) {
2337 dbg_info *dbgi = get_irn_dbg_info(n);
2338 ir_mode *mode = get_irn_mode(c);
2340 c = new_rd_Not(dbgi, blk, c, mode);
2341 n = new_rd_And(dbgi, blk, new_n, c, mode);
2344 set_nodes_block(n, blk);
2345 set_binop_left(n, new_n);
2346 set_binop_right(n, c);
2350 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2359 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
2361 * - and, or, xor instead of &
2362 * - Shl, Shr, Shrs, rotl instead of >>
2363 * (with a special case for Or/Xor + Shrs)
2365 * This normalisation is usually good for the backend since << C can often be
2366 * matched as address-mode.
2368 static ir_node *transform_node_bitop_shift(ir_node *n)
2370 ir_graph *irg = get_irn_irg(n);
2371 ir_node *left = get_binop_left(n);
2372 ir_node *right = get_binop_right(n);
2373 ir_mode *mode = get_irn_mode(n);
2374 ir_node *shift_left;
2375 ir_node *shift_right;
2377 dbg_info *dbg_bitop;
2378 dbg_info *dbg_shift;
2384 ir_tarval *tv_bitop;
2386 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2389 assert(is_And(n) || is_Or(n) || is_Eor(n) || is_Or_Eor_Add(n));
2390 if (!is_Const(right) || !is_shiftop(left))
2393 shift_left = get_binop_left(left);
2394 shift_right = get_binop_right(left);
2395 if (!is_Const(shift_right))
2398 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2399 if (is_Shrs(left)) {
2400 /* TODO this could be improved */
2404 irg = get_irn_irg(n);
2405 block = get_nodes_block(n);
2406 dbg_bitop = get_irn_dbg_info(n);
2407 dbg_shift = get_irn_dbg_info(left);
2408 tv1 = get_Const_tarval(shift_right);
2409 tv2 = get_Const_tarval(right);
2410 assert(get_tarval_mode(tv2) == mode);
2413 tv_bitop = tarval_shr(tv2, tv1);
2415 /* Check whether we have lost some bits during the right shift. */
2417 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
2419 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
2422 } else if (is_Shr(left)) {
2425 * TODO this can be improved by checking whether
2426 * the left shift produces an overflow
2430 tv_bitop = tarval_shl(tv2, tv1);
2432 assert(is_Rotl(left));
2433 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
2435 new_const = new_r_Const(irg, tv_bitop);
2438 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
2439 } else if (is_Or(n) || is_Or_Eor_Add(n)) {
2440 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
2443 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
2447 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
2448 } else if (is_Shr(left)) {
2449 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
2451 assert(is_Rotl(left));
2452 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
2458 static bool complement_values(const ir_node *a, const ir_node *b)
2460 if (is_Not(a) && get_Not_op(a) == b)
2462 if (is_Not(b) && get_Not_op(b) == a)
2464 if (is_Const(a) && is_Const(b)) {
2465 ir_tarval *tv_a = get_Const_tarval(a);
2466 ir_tarval *tv_b = get_Const_tarval(b);
2467 return tarval_not(tv_a) == tv_b;
2472 typedef ir_tarval *(tv_fold_binop_func)(ir_tarval *a, ir_tarval *b);
2475 * for associative operations fold:
2476 * op(op(x, c0), c1) to op(x, op(c0, c1)) with constants folded.
2477 * This is a "light" version of the reassociation phase
2479 static ir_node *fold_constant_associativity(ir_node *node,
2480 tv_fold_binop_func fold)
2485 ir_node *right = get_binop_right(node);
2486 ir_node *left_right;
2493 if (!is_Const(right))
2496 op = get_irn_op(node);
2497 left = get_binop_left(node);
2498 if (get_irn_op(left) != op)
2501 left_right = get_binop_right(left);
2502 if (!is_Const(left_right))
2505 left_left = get_binop_left(left);
2506 c0 = get_Const_tarval(left_right);
2507 c1 = get_Const_tarval(right);
2508 irg = get_irn_irg(node);
2509 if (get_tarval_mode(c0) != get_tarval_mode(c1))
2511 new_c = fold(c0, c1);
2512 if (new_c == tarval_bad)
2514 new_const = new_r_Const(irg, new_c);
2515 new_node = exact_copy(node);
2516 set_binop_left(new_node, left_left);
2517 set_binop_right(new_node, new_const);
2524 static ir_node *transform_node_Or_(ir_node *n)
2527 ir_node *a = get_binop_left(n);
2528 ir_node *b = get_binop_right(n);
2532 n = fold_constant_associativity(n, tarval_or);
2536 if (is_Not(a) && is_Not(b)) {
2537 /* ~a | ~b = ~(a&b) */
2538 ir_node *block = get_nodes_block(n);
2540 mode = get_irn_mode(n);
2543 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
2544 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2545 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2549 /* we can combine the relations of two compares with the same operands */
2550 if (is_Cmp(a) && is_Cmp(b)) {
2551 ir_node *a_left = get_Cmp_left(a);
2552 ir_node *a_right = get_Cmp_right(a);
2553 ir_node *b_left = get_Cmp_left(b);
2554 ir_node *b_right = get_Cmp_right(b);
2555 if (a_left == b_left && b_left == b_right) {
2556 dbg_info *dbgi = get_irn_dbg_info(n);
2557 ir_node *block = get_nodes_block(n);
2558 ir_relation a_relation = get_Cmp_relation(a);
2559 ir_relation b_relation = get_Cmp_relation(b);
2560 ir_relation new_relation = a_relation | b_relation;
2561 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2563 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
2564 if (is_cmp_unequal(a) && is_cmp_unequal(b)
2565 && !mode_is_float(get_irn_mode(a_left))
2566 && !mode_is_float(get_irn_mode(b_left))) {
2567 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
2568 ir_graph *irg = get_irn_irg(n);
2569 dbg_info *dbgi = get_irn_dbg_info(n);
2570 ir_node *block = get_nodes_block(n);
2571 ir_mode *a_mode = get_irn_mode(a_left);
2572 ir_mode *b_mode = get_irn_mode(b_left);
2573 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2574 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2575 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
2576 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
2577 ir_node *zero = create_zero_const(irg, b_mode);
2578 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2580 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
2581 ir_graph *irg = get_irn_irg(n);
2582 dbg_info *dbgi = get_irn_dbg_info(n);
2583 ir_node *block = get_nodes_block(n);
2584 ir_mode *a_mode = get_irn_mode(a_left);
2585 ir_mode *b_mode = get_irn_mode(b_left);
2586 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
2587 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
2588 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
2589 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
2590 ir_node *zero = create_zero_const(irg, a_mode);
2591 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
2596 mode = get_irn_mode(n);
2597 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
2599 n = transform_node_Or_bf_store(n);
2602 n = transform_node_Or_Rotl(n);
2606 n = transform_bitwise_distributive(n, transform_node_Or_);
2609 n = transform_node_bitop_shift(n);
2616 static ir_node *transform_node_Or(ir_node *n)
2618 if (is_Or_Eor_Add(n)) {
2619 dbg_info *dbgi = get_irn_dbg_info(n);
2620 ir_node *block = get_nodes_block(n);
2621 ir_node *left = get_Or_left(n);
2622 ir_node *right = get_Or_right(n);
2623 ir_mode *mode = get_irn_mode(n);
2624 return new_rd_Add(dbgi, block, left, right, mode);
2626 return transform_node_Or_(n);
2632 static ir_node *transform_node_Eor_(ir_node *n)
2635 ir_node *a = get_binop_left(n);
2636 ir_node *b = get_binop_right(n);
2637 ir_mode *mode = get_irn_mode(n);
2640 n = fold_constant_associativity(n, tarval_eor);
2644 /* we can combine the relations of two compares with the same operands */
2645 if (is_Cmp(a) && is_Cmp(b)) {
2646 ir_node *a_left = get_Cmp_left(a);
2647 ir_node *a_right = get_Cmp_left(a);
2648 ir_node *b_left = get_Cmp_left(b);
2649 ir_node *b_right = get_Cmp_right(b);
2650 if (a_left == b_left && b_left == b_right) {
2651 dbg_info *dbgi = get_irn_dbg_info(n);
2652 ir_node *block = get_nodes_block(n);
2653 ir_relation a_relation = get_Cmp_relation(a);
2654 ir_relation b_relation = get_Cmp_relation(b);
2655 ir_relation new_relation = a_relation ^ b_relation;
2656 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2660 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
2662 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
2663 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
2664 dbg_info *dbg = get_irn_dbg_info(n);
2665 ir_node *block = get_nodes_block(n);
2666 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
2667 ir_node *new_left = get_Not_op(a);
2668 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
2669 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2671 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
2672 dbg_info *dbg = get_irn_dbg_info(n);
2673 ir_node *block = get_nodes_block(n);
2674 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
2675 ir_node *new_right = get_Not_op(b);
2676 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
2677 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2681 /* x ^ 1...1 -> ~1 */
2682 if (is_Const(b) && is_Const_all_one(b)) {
2683 n = new_r_Not(get_nodes_block(n), a, mode);
2684 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2688 n = transform_bitwise_distributive(n, transform_node_Eor_);
2691 n = transform_node_bitop_shift(n);
2698 static ir_node *transform_node_Eor(ir_node *n)
2700 if (is_Or_Eor_Add(n)) {
2701 dbg_info *dbgi = get_irn_dbg_info(n);
2702 ir_node *block = get_nodes_block(n);
2703 ir_node *left = get_Eor_left(n);
2704 ir_node *right = get_Eor_right(n);
2705 ir_mode *mode = get_irn_mode(n);
2706 return new_rd_Add(dbgi, block, left, right, mode);
2708 return transform_node_Eor_(n);
2712 * Do the AddSub optimization, then Transform
2713 * Constant folding on Phi
2714 * Add(a,a) -> Mul(a, 2)
2715 * Add(Mul(a, x), a) -> Mul(a, x+1)
2716 * if the mode is integer or float.
2717 * Transform Add(a,-b) into Sub(a,b).
2718 * Reassociation might fold this further.
2720 static ir_node *transform_node_Add(ir_node *n)
2728 n = fold_constant_associativity(n, tarval_add);
2732 n = transform_node_AddSub(n);
2736 a = get_Add_left(n);
2737 b = get_Add_right(n);
2738 mode = get_irn_mode(n);
2740 if (mode_is_reference(mode)) {
2741 ir_mode *lmode = get_irn_mode(a);
2743 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2744 /* an Add(a, NULL) is a hidden Conv */
2745 dbg_info *dbg = get_irn_dbg_info(n);
2746 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2750 if (is_Const(b) && get_mode_arithmetic(mode) == irma_twos_complement) {
2751 ir_tarval *tv = get_Const_tarval(b);
2752 ir_tarval *min = get_mode_min(mode);
2753 /* if all bits are set, then this has the same effect as a Not.
2754 * Note that the following == gives false for different modes which
2755 * is exactly what we want */
2757 dbg_info *dbgi = get_irn_dbg_info(n);
2758 ir_graph *irg = get_irn_irg(n);
2759 ir_node *block = get_nodes_block(n);
2760 ir_node *cnst = new_r_Const(irg, min);
2761 return new_rd_Eor(dbgi, block, a, cnst, mode);
2765 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2767 /* for FP the following optimizations are only allowed if
2768 * fp_strict_algebraic is disabled */
2769 if (mode_is_float(mode)) {
2770 ir_graph *irg = get_irn_irg(n);
2771 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2775 if (mode_is_num(mode)) {
2776 ir_graph *irg = get_irn_irg(n);
2777 /* the following code leads to endless recursion when Mul are replaced
2778 * by a simple instruction chain */
2779 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2780 && a == b && mode_is_int(mode)) {
2781 ir_node *block = get_nodes_block(n);
2784 get_irn_dbg_info(n),
2787 new_r_Const_long(irg, mode, 2),
2789 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2794 get_irn_dbg_info(n),
2799 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2804 get_irn_dbg_info(n),
2809 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2812 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2813 /* Here we rely on constants be on the RIGHT side */
2815 ir_node *op = get_Not_op(a);
2817 if (is_Const(b) && is_Const_one(b)) {
2819 ir_node *blk = get_nodes_block(n);
2820 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2821 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2828 if (is_Or_Eor_Add(n)) {
2829 n = transform_node_Or_(n);
2832 n = transform_node_Eor_(n);
2841 * returns -cnst or NULL if impossible
2843 static ir_node *const_negate(ir_node *cnst)
2845 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2846 dbg_info *dbgi = get_irn_dbg_info(cnst);
2847 ir_graph *irg = get_irn_irg(cnst);
2848 if (tv == tarval_bad) return NULL;
2849 return new_rd_Const(dbgi, irg, tv);
2853 * Do the AddSub optimization, then Transform
2854 * Constant folding on Phi
2855 * Sub(0,a) -> Minus(a)
2856 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2857 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2858 * Sub(Add(a, x), x) -> a
2859 * Sub(x, Add(x, a)) -> -a
2860 * Sub(x, Const) -> Add(x, -Const)
2862 static ir_node *transform_node_Sub(ir_node *n)
2868 n = transform_node_AddSub(n);
2870 a = get_Sub_left(n);
2871 b = get_Sub_right(n);
2873 mode = get_irn_mode(n);
2875 if (mode_is_int(mode)) {
2876 ir_mode *lmode = get_irn_mode(a);
2878 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2879 /* a Sub(a, NULL) is a hidden Conv */
2880 dbg_info *dbg = get_irn_dbg_info(n);
2881 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2882 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2886 if (mode == lmode &&
2887 get_mode_arithmetic(mode) == irma_twos_complement &&
2889 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2891 dbg_info *dbg = get_irn_dbg_info(n);
2892 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2893 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2899 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2901 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2902 if (mode_is_float(mode)) {
2903 ir_graph *irg = get_irn_irg(n);
2904 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2908 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2909 /* a - C -> a + (-C) */
2910 ir_node *cnst = const_negate(b);
2912 ir_node *block = get_nodes_block(n);
2913 dbg_info *dbgi = get_irn_dbg_info(n);
2915 n = new_rd_Add(dbgi, block, a, cnst, mode);
2916 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2921 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2922 dbg_info *dbg = get_irn_dbg_info(n);
2923 ir_node *block = get_nodes_block(n);
2924 ir_node *left = get_Minus_op(a);
2925 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2927 n = new_rd_Minus(dbg, block, add, mode);
2928 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2930 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2931 dbg_info *dbg = get_irn_dbg_info(n);
2932 ir_node *block = get_nodes_block(n);
2933 ir_node *right = get_Minus_op(b);
2935 n = new_rd_Add(dbg, block, a, right, mode);
2936 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2938 } else if (is_Sub(b)) {
2939 /* a - (b - c) -> a + (c - b)
2940 * -> (a - b) + c iff (b - c) is a pointer */
2941 dbg_info *s_dbg = get_irn_dbg_info(b);
2942 ir_node *s_left = get_Sub_left(b);
2943 ir_node *s_right = get_Sub_right(b);
2944 ir_mode *s_mode = get_irn_mode(b);
2945 if (mode_is_reference(s_mode)) {
2946 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2947 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2948 dbg_info *a_dbg = get_irn_dbg_info(n);
2951 s_right = new_r_Conv(lowest_block, s_right, mode);
2952 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2954 ir_node *s_block = get_nodes_block(b);
2955 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2956 dbg_info *a_dbg = get_irn_dbg_info(n);
2957 ir_node *a_block = get_nodes_block(n);
2959 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2961 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2964 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2965 ir_node *m_right = get_Mul_right(b);
2966 if (is_Const(m_right)) {
2967 ir_node *cnst2 = const_negate(m_right);
2968 if (cnst2 != NULL) {
2969 dbg_info *m_dbg = get_irn_dbg_info(b);
2970 ir_node *m_block = get_nodes_block(b);
2971 ir_node *m_left = get_Mul_left(b);
2972 ir_mode *m_mode = get_irn_mode(b);
2973 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2974 dbg_info *a_dbg = get_irn_dbg_info(n);
2975 ir_node *a_block = get_nodes_block(n);
2977 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2978 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2985 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2986 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2988 get_irn_dbg_info(n),
2992 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2995 if ((is_Add(a) || is_Or_Eor_Add(a)) && mode_wrap_around(mode)) {
2996 ir_node *left = get_binop_left(a);
2997 ir_node *right = get_binop_right(a);
2999 /* FIXME: Does the Conv's work only for two complement or generally? */
3001 if (mode != get_irn_mode(right)) {
3002 /* This Sub is an effective Cast */
3003 right = new_r_Conv(get_nodes_block(n), right, mode);
3006 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3008 } else if (right == b) {
3009 if (mode != get_irn_mode(left)) {
3010 /* This Sub is an effective Cast */
3011 left = new_r_Conv(get_nodes_block(n), left, mode);
3014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3018 if ((is_Add(b) || is_Or_Eor_Add(b)) && mode_wrap_around(mode)) {
3019 ir_node *left = get_binop_left(b);
3020 ir_node *right = get_binop_right(b);
3022 /* FIXME: Does the Conv's work only for two complement or generally? */
3024 ir_mode *r_mode = get_irn_mode(right);
3026 n = new_r_Minus(get_nodes_block(n), right, r_mode);
3027 if (mode != r_mode) {
3028 /* This Sub is an effective Cast */
3029 n = new_r_Conv(get_nodes_block(n), n, mode);
3031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3033 } else if (right == a) {
3034 ir_mode *l_mode = get_irn_mode(left);
3036 n = new_r_Minus(get_nodes_block(n), left, l_mode);
3037 if (mode != l_mode) {
3038 /* This Sub is an effective Cast */
3039 n = new_r_Conv(get_nodes_block(n), n, mode);
3041 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
3045 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
3046 ir_mode *mode = get_irn_mode(a);
3048 if (mode == get_irn_mode(b)) {
3050 ir_node *op_a = get_Conv_op(a);
3051 ir_node *op_b = get_Conv_op(b);
3053 /* check if it's allowed to skip the conv */
3054 ma = get_irn_mode(op_a);
3055 mb = get_irn_mode(op_b);
3057 if (mode_is_reference(ma) && mode_is_reference(mb)) {
3058 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
3061 set_Sub_right(n, b);
3067 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
3068 if (!is_reassoc_running() && is_Mul(a)) {
3069 ir_node *ma = get_Mul_left(a);
3070 ir_node *mb = get_Mul_right(a);
3073 ir_node *blk = get_nodes_block(n);
3074 ir_graph *irg = get_irn_irg(n);
3076 get_irn_dbg_info(n),
3080 get_irn_dbg_info(n),
3083 new_r_Const(irg, get_mode_one(mode)),
3086 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3088 } else if (mb == b) {
3089 ir_node *blk = get_nodes_block(n);
3090 ir_graph *irg = get_irn_irg(n);
3092 get_irn_dbg_info(n),
3096 get_irn_dbg_info(n),
3099 new_r_Const(irg, get_mode_one(mode)),
3102 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
3106 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
3107 ir_node *x = get_Sub_left(a);
3108 ir_node *y = get_Sub_right(a);
3109 ir_node *blk = get_nodes_block(n);
3110 ir_mode *m_b = get_irn_mode(b);
3111 ir_mode *m_y = get_irn_mode(y);
3115 /* Determine the right mode for the Add. */
3118 else if (mode_is_reference(m_b))
3120 else if (mode_is_reference(m_y))
3124 * Both modes are different but none is reference,
3125 * happens for instance in SubP(SubP(P, Iu), Is).
3126 * We have two possibilities here: Cast or ignore.
3127 * Currently we ignore this case.
3132 add = new_r_Add(blk, y, b, add_mode);
3134 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
3135 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
3139 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3140 /* c - ~X = X + (c+1) */
3141 if (is_Const(a) && is_Not(b)) {
3142 ir_tarval *tv = get_Const_tarval(a);
3144 tv = tarval_add(tv, get_mode_one(mode));
3145 if (tv != tarval_bad) {
3146 ir_node *blk = get_nodes_block(n);
3147 ir_graph *irg = get_irn_irg(n);
3148 ir_node *c = new_r_Const(irg, tv);
3149 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
3150 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
3154 /* x-(x&y) = x & ~y */
3156 ir_node *and_left = get_And_left(b);
3157 ir_node *and_right = get_And_right(b);
3158 if (and_right == a) {
3159 ir_node *tmp = and_left;
3160 and_left = and_right;
3163 if (and_left == a) {
3164 dbg_info *dbgi = get_irn_dbg_info(n);
3165 ir_node *block = get_nodes_block(n);
3166 ir_mode *mode = get_irn_mode(n);
3167 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
3168 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
3177 * Several transformation done on n*n=2n bits mul.
3178 * These transformations must be done here because new nodes may be produced.
3180 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
3183 ir_node *a = get_Mul_left(n);
3184 ir_node *b = get_Mul_right(n);
3185 ir_tarval *ta = value_of(a);
3186 ir_tarval *tb = value_of(b);
3187 ir_mode *smode = get_irn_mode(a);
3189 if (ta == get_mode_one(smode)) {
3190 /* (L)1 * (L)b = (L)b */
3191 ir_node *blk = get_nodes_block(n);
3192 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
3193 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3196 else if (ta == get_mode_minus_one(smode)) {
3197 /* (L)-1 * (L)b = (L)b */
3198 ir_node *blk = get_nodes_block(n);
3199 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
3200 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3201 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3204 if (tb == get_mode_one(smode)) {
3205 /* (L)a * (L)1 = (L)a */
3206 ir_node *blk = get_irn_n(a, -1);
3207 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
3208 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
3211 else if (tb == get_mode_minus_one(smode)) {
3212 /* (L)a * (L)-1 = (L)-a */
3213 ir_node *blk = get_nodes_block(n);
3214 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
3215 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
3216 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3223 * Transform Mul(a,-1) into -a.
3224 * Do constant evaluation of Phi nodes.
3225 * Do architecture dependent optimizations on Mul nodes
3227 static ir_node *transform_node_Mul(ir_node *n)
3229 ir_node *c, *oldn = n;
3230 ir_mode *mode = get_irn_mode(n);
3231 ir_node *a = get_Mul_left(n);
3232 ir_node *b = get_Mul_right(n);
3234 n = fold_constant_associativity(n, tarval_mul);
3238 if (mode != get_irn_mode(a))
3239 return transform_node_Mul2n(n, mode);
3241 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
3243 if (mode_is_signed(mode)) {
3246 if (value_of(a) == get_mode_minus_one(mode))
3248 else if (value_of(b) == get_mode_minus_one(mode))
3251 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
3252 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3257 if (is_Const(b)) { /* (-a) * const -> a * -const */
3258 ir_node *cnst = const_negate(b);
3260 dbg_info *dbgi = get_irn_dbg_info(n);
3261 ir_node *block = get_nodes_block(n);
3262 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
3263 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
3266 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
3267 dbg_info *dbgi = get_irn_dbg_info(n);
3268 ir_node *block = get_nodes_block(n);
3269 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
3270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
3272 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
3273 ir_node *sub_l = get_Sub_left(b);
3274 ir_node *sub_r = get_Sub_right(b);
3275 dbg_info *dbgi = get_irn_dbg_info(n);
3276 ir_node *block = get_nodes_block(n);
3277 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3278 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
3279 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3282 } else if (is_Minus(b)) {
3283 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
3284 ir_node *sub_l = get_Sub_left(a);
3285 ir_node *sub_r = get_Sub_right(a);
3286 dbg_info *dbgi = get_irn_dbg_info(n);
3287 ir_node *block = get_nodes_block(n);
3288 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
3289 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
3290 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
3293 } else if (is_Shl(a)) {
3294 ir_node *const shl_l = get_Shl_left(a);
3295 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3296 /* (1 << x) * b -> b << x */
3297 dbg_info *const dbgi = get_irn_dbg_info(n);
3298 ir_node *const block = get_nodes_block(n);
3299 ir_node *const shl_r = get_Shl_right(a);
3300 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
3301 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3304 } else if (is_Shl(b)) {
3305 ir_node *const shl_l = get_Shl_left(b);
3306 if (is_Const(shl_l) && is_Const_one(shl_l)) {
3307 /* a * (1 << x) -> a << x */
3308 dbg_info *const dbgi = get_irn_dbg_info(n);
3309 ir_node *const block = get_nodes_block(n);
3310 ir_node *const shl_r = get_Shl_right(b);
3311 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
3312 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
3316 if (get_mode_arithmetic(mode) == irma_ieee754
3317 || get_mode_arithmetic(mode) == irma_x86_extended_float) {
3319 ir_tarval *tv = get_Const_tarval(a);
3320 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3321 && !tarval_is_negative(tv)) {
3322 /* 2.0 * b = b + b */
3323 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
3324 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3328 else if (is_Const(b)) {
3329 ir_tarval *tv = get_Const_tarval(b);
3330 if (tarval_get_exponent(tv) == 1 && tarval_zero_mantissa(tv)
3331 && !tarval_is_negative(tv)) {
3332 /* a * 2.0 = a + a */
3333 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
3334 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
3339 return arch_dep_replace_mul_with_shifts(n);
3343 * Transform a Div Node.
3345 static ir_node *transform_node_Div(ir_node *n)
3347 ir_mode *mode = get_Div_resmode(n);
3348 ir_node *a = get_Div_left(n);
3349 ir_node *b = get_Div_right(n);
3351 const ir_node *dummy;
3353 if (mode_is_int(mode)) {
3354 if (is_Const(b) && is_const_Phi(a)) {
3355 /* check for Div(Phi, Const) */
3356 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3358 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3361 } else if (is_Const(a) && is_const_Phi(b)) {
3362 /* check for Div(Const, Phi) */
3363 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3365 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3368 } else if (is_const_Phi(a) && is_const_Phi(b)) {
3369 /* check for Div(Phi, Phi) */
3370 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3372 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3377 if (a == b && value_not_zero(a, &dummy)) {
3378 ir_graph *irg = get_irn_irg(n);
3379 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
3380 value = new_r_Const(irg, get_mode_one(mode));
3381 DBG_OPT_CSTEVAL(n, value);
3384 if (mode_is_signed(mode) && is_Const(b)) {
3385 ir_tarval *tv = get_Const_tarval(b);
3387 if (tv == get_mode_minus_one(mode)) {
3389 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3390 DBG_OPT_CSTEVAL(n, value);
3394 /* Try architecture dependent optimization */
3395 value = arch_dep_replace_div_by_const(n);
3398 assert(mode_is_float(mode));
3400 /* Optimize x/c to x*(1/c) */
3401 if (get_mode_arithmetic(mode) == irma_ieee754) {
3402 ir_tarval *tv = value_of(b);
3404 if (tv != tarval_bad) {
3405 int rem = tarval_fp_ops_enabled();
3408 * Floating point constant folding might be disabled here to
3410 * However, as we check for exact result, doing it is safe.
3413 tarval_enable_fp_ops(1);
3414 tv = tarval_div(get_mode_one(mode), tv);
3415 tarval_enable_fp_ops(rem);
3417 /* Do the transformation if the result is either exact or we are
3418 not using strict rules. */
3419 if (tv != tarval_bad &&
3420 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3421 ir_node *block = get_nodes_block(n);
3422 ir_graph *irg = get_irn_irg(block);
3423 ir_node *c = new_r_Const(irg, tv);
3424 dbg_info *dbgi = get_irn_dbg_info(n);
3425 value = new_rd_Mul(dbgi, block, a, c, mode);
3438 /* Turn Div into a tuple (mem, jmp, bad, value) */
3439 mem = get_Div_mem(n);
3440 blk = get_nodes_block(n);
3441 irg = get_irn_irg(blk);
3443 /* skip a potential Pin */
3444 mem = skip_Pin(mem);
3445 turn_into_tuple(n, pn_Div_max+1);
3446 set_Tuple_pred(n, pn_Div_M, mem);
3447 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3448 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
3449 set_Tuple_pred(n, pn_Div_res, value);
3455 * Transform a Mod node.
3457 static ir_node *transform_node_Mod(ir_node *n)
3459 ir_mode *mode = get_Mod_resmode(n);
3460 ir_node *a = get_Mod_left(n);
3461 ir_node *b = get_Mod_right(n);
3466 if (is_Const(b) && is_const_Phi(a)) {
3467 /* check for Div(Phi, Const) */
3468 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3470 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3474 else if (is_Const(a) && is_const_Phi(b)) {
3475 /* check for Div(Const, Phi) */
3476 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3478 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3482 else if (is_const_Phi(a) && is_const_Phi(b)) {
3483 /* check for Div(Phi, Phi) */
3484 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3486 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3493 irg = get_irn_irg(n);
3494 if (tv != tarval_bad) {
3495 value = new_r_Const(irg, tv);
3497 DBG_OPT_CSTEVAL(n, value);
3500 ir_node *a = get_Mod_left(n);
3501 ir_node *b = get_Mod_right(n);
3502 const ir_node *dummy;
3504 if (a == b && value_not_zero(a, &dummy)) {
3505 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3506 value = new_r_Const(irg, get_mode_null(mode));
3507 DBG_OPT_CSTEVAL(n, value);
3510 if (mode_is_signed(mode) && is_Const(b)) {
3511 ir_tarval *tv = get_Const_tarval(b);
3513 if (tv == get_mode_minus_one(mode)) {
3515 value = new_r_Const(irg, get_mode_null(mode));
3516 DBG_OPT_CSTEVAL(n, value);
3520 /* Try architecture dependent optimization */
3521 value = arch_dep_replace_mod_by_const(n);
3530 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3531 mem = get_Mod_mem(n);
3532 blk = get_nodes_block(n);
3533 irg = get_irn_irg(blk);
3535 /* skip a potential Pin */
3536 mem = skip_Pin(mem);
3537 turn_into_tuple(n, pn_Mod_max+1);
3538 set_Tuple_pred(n, pn_Mod_M, mem);
3539 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3540 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
3541 set_Tuple_pred(n, pn_Mod_res, value);
3547 * Transform a Cond node.
3549 * Replace the Cond by a Jmp if it branches on a constant
3552 static ir_node *transform_node_Cond(ir_node *n)
3554 ir_node *a = get_Cond_selector(n);
3555 ir_graph *irg = get_irn_irg(n);
3559 /* we need block info which is not available in floating irgs */
3560 if (get_irg_pinned(irg) == op_pin_state_floats)
3564 if (ta == tarval_bad && is_Cmp(a)) {
3565 /* try again with a direct call to compute_cmp, as we don't care
3566 * about the MODEB_LOWERED flag here */
3567 ta = compute_cmp(a);
3570 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
3571 /* It's a boolean Cond, branching on a boolean constant.
3572 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3573 ir_node *blk = get_nodes_block(n);
3574 jmp = new_r_Jmp(blk);
3575 turn_into_tuple(n, pn_Cond_max+1);
3576 if (ta == tarval_b_true) {
3577 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
3578 set_Tuple_pred(n, pn_Cond_true, jmp);
3580 set_Tuple_pred(n, pn_Cond_false, jmp);
3581 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
3583 /* We might generate an endless loop, so keep it alive. */
3584 add_End_keepalive(get_irg_end(irg), blk);
3585 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3590 static ir_node *transform_node_Switch(ir_node *n)
3592 ir_node *op = get_Switch_selector(n);
3593 ir_tarval *val = value_of(op);
3594 if (val != tarval_bad) {
3595 dbg_info *dbgi = get_irn_dbg_info(n);
3596 ir_graph *irg = get_irn_irg(n);
3597 unsigned n_outs = get_Switch_n_outs(n);
3598 ir_node *block = get_nodes_block(n);
3599 ir_node *bad = new_r_Bad(irg, mode_X);
3600 ir_node **in = XMALLOCN(ir_node*, n_outs);
3601 const ir_switch_table *table = get_Switch_table(n);
3602 size_t n_entries = ir_switch_table_get_n_entries(table);
3606 for (i = 0; i < n_entries; ++i) {
3607 const ir_switch_table_entry *entry
3608 = ir_switch_table_get_entry_const(table, i);
3609 ir_tarval *min = entry->min;
3610 ir_tarval *max = entry->max;
3613 if ((min == max && min == val)
3614 || (tarval_cmp(val, min) != ir_relation_less
3615 && tarval_cmp(val, max) != ir_relation_greater)) {
3620 for (o = 0; o < n_outs; ++o) {
3621 if (o == (unsigned)jmp_pn) {
3622 in[o] = new_rd_Jmp(dbgi, block);
3627 return new_r_Tuple(block, (int)n_outs, in);
3633 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
3635 * - and, or, xor instead of &
3636 * - Shl, Shr, Shrs, rotl instead of >>
3637 * (with a special case for Or/Xor + Shrs)
3639 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
3641 static ir_node *transform_node_shift_bitop(ir_node *n)
3643 ir_graph *irg = get_irn_irg(n);
3644 ir_node *right = get_binop_right(n);
3645 ir_mode *mode = get_irn_mode(n);
3647 ir_node *bitop_left;
3648 ir_node *bitop_right;
3657 ir_tarval *tv_shift;
3659 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3662 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
3664 if (!is_Const(right))
3667 left = get_binop_left(n);
3668 op_left = get_irn_op(left);
3669 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3672 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3673 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3674 /* TODO: test if sign bit is affectes */
3678 bitop_right = get_binop_right(left);
3679 if (!is_Const(bitop_right))
3682 bitop_left = get_binop_left(left);
3684 block = get_nodes_block(n);
3685 dbgi = get_irn_dbg_info(n);
3686 tv1 = get_Const_tarval(bitop_right);
3687 tv2 = get_Const_tarval(right);
3689 assert(get_tarval_mode(tv1) == mode);
3692 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3693 tv_shift = tarval_shl(tv1, tv2);
3694 } else if (is_Shr(n)) {
3695 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3696 tv_shift = tarval_shr(tv1, tv2);
3697 } else if (is_Shrs(n)) {
3698 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3699 tv_shift = tarval_shrs(tv1, tv2);
3702 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3703 tv_shift = tarval_rotl(tv1, tv2);
3706 assert(get_tarval_mode(tv_shift) == mode);
3707 irg = get_irn_irg(n);
3708 new_const = new_r_Const(irg, tv_shift);
3710 if (op_left == op_And) {
3711 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3712 } else if (op_left == op_Or) {
3713 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3715 assert(op_left == op_Eor);
3716 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3725 static ir_node *transform_node_And(ir_node *n)
3727 ir_node *c, *oldn = n;
3728 ir_node *a = get_And_left(n);
3729 ir_node *b = get_And_right(n);
3732 n = fold_constant_associativity(n, tarval_and);
3736 if (is_Cmp(a) && is_Cmp(b)) {
3737 ir_node *a_left = get_Cmp_left(a);
3738 ir_node *a_right = get_Cmp_right(a);
3739 ir_node *b_left = get_Cmp_left(b);
3740 ir_node *b_right = get_Cmp_right(b);
3741 ir_relation a_relation = get_Cmp_relation(a);
3742 ir_relation b_relation = get_Cmp_relation(b);
3743 /* we can combine the relations of two compares with the same
3745 if (a_left == b_left && b_left == b_right) {
3746 dbg_info *dbgi = get_irn_dbg_info(n);
3747 ir_node *block = get_nodes_block(n);
3748 ir_relation new_relation = a_relation & b_relation;
3749 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3751 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3752 if (a_relation == b_relation && a_relation == ir_relation_equal
3753 && !mode_is_float(get_irn_mode(a_left))
3754 && !mode_is_float(get_irn_mode(b_left))) {
3755 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3756 dbg_info *dbgi = get_irn_dbg_info(n);
3757 ir_node *block = get_nodes_block(n);
3758 ir_mode *a_mode = get_irn_mode(a_left);
3759 ir_mode *b_mode = get_irn_mode(b_left);
3760 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3761 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3762 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3763 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3764 ir_graph *irg = get_irn_irg(n);
3765 ir_node *zero = create_zero_const(irg, b_mode);
3766 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3768 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3769 dbg_info *dbgi = get_irn_dbg_info(n);
3770 ir_node *block = get_nodes_block(n);
3771 ir_mode *a_mode = get_irn_mode(a_left);
3772 ir_mode *b_mode = get_irn_mode(b_left);
3773 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3774 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3775 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3776 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3777 ir_graph *irg = get_irn_irg(n);
3778 ir_node *zero = create_zero_const(irg, a_mode);
3779 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3784 mode = get_irn_mode(n);
3785 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3787 if (is_Or(a) || is_Or_Eor_Add(a)) {
3788 ir_node *or_left = get_binop_left(a);
3789 ir_node *or_right = get_binop_right(a);
3790 if (complement_values(or_left, b)) {
3791 /* (a|b) & ~a => b & ~a */
3792 dbg_info *dbgi = get_irn_dbg_info(n);
3793 ir_node *block = get_nodes_block(n);
3794 return new_rd_And(dbgi, block, or_right, b, mode);
3795 } else if (complement_values(or_right, b)) {
3796 /* (a|b) & ~b => a & ~b */
3797 dbg_info *dbgi = get_irn_dbg_info(n);
3798 ir_node *block = get_nodes_block(n);
3799 return new_rd_And(dbgi, block, or_left, b, mode);
3800 } else if (is_Not(b)) {
3801 ir_node *op = get_Not_op(b);
3803 ir_node *ba = get_And_left(op);
3804 ir_node *bb = get_And_right(op);
3806 /* it's enough to test the following cases due to normalization! */
3807 if (or_left == ba && or_right == bb) {
3808 /* (a|b) & ~(a&b) = a^b */
3809 ir_node *block = get_nodes_block(n);
3811 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3812 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3818 if (is_Or(b) || is_Or_Eor_Add(b)) {
3819 ir_node *or_left = get_binop_left(b);
3820 ir_node *or_right = get_binop_right(b);
3821 if (complement_values(or_left, a)) {
3822 /* (a|b) & ~a => b & ~a */
3823 dbg_info *dbgi = get_irn_dbg_info(n);
3824 ir_node *block = get_nodes_block(n);
3825 return new_rd_And(dbgi, block, or_right, a, mode);
3826 } else if (complement_values(or_right, a)) {
3827 /* (a|b) & ~b => a & ~b */
3828 dbg_info *dbgi = get_irn_dbg_info(n);
3829 ir_node *block = get_nodes_block(n);
3830 return new_rd_And(dbgi, block, or_left, a, mode);
3831 } else if (is_Not(a)) {
3832 ir_node *op = get_Not_op(a);
3834 ir_node *aa = get_And_left(op);
3835 ir_node *ab = get_And_right(op);
3837 /* it's enough to test the following cases due to normalization! */
3838 if (or_left == aa && or_right == ab) {
3839 /* (a|b) & ~(a&b) = a^b */
3840 ir_node *block = get_nodes_block(n);
3842 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3849 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3850 ir_node *al = get_binop_left(a);
3851 ir_node *ar = get_binop_right(a);
3854 /* (b ^ a) & b -> ~a & b */
3855 dbg_info *dbg = get_irn_dbg_info(n);
3856 ir_node *block = get_nodes_block(n);
3858 ar = new_rd_Not(dbg, block, ar, mode);
3859 n = new_rd_And(dbg, block, ar, b, mode);
3860 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3864 /* (a ^ b) & b -> ~a & b */
3865 dbg_info *dbg = get_irn_dbg_info(n);
3866 ir_node *block = get_nodes_block(n);
3868 al = new_rd_Not(dbg, block, al, mode);
3869 n = new_rd_And(dbg, block, al, b, mode);
3870 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3874 if (is_Eor(b) || is_Or_Eor_Add(b)) {
3875 ir_node *bl = get_binop_left(b);
3876 ir_node *br = get_binop_right(b);
3879 /* a & (a ^ b) -> a & ~b */
3880 dbg_info *dbg = get_irn_dbg_info(n);
3881 ir_node *block = get_nodes_block(n);
3883 br = new_rd_Not(dbg, block, br, mode);
3884 n = new_rd_And(dbg, block, br, a, mode);
3885 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3889 /* a & (b ^ a) -> a & ~b */
3890 dbg_info *dbg = get_irn_dbg_info(n);
3891 ir_node *block = get_nodes_block(n);
3893 bl = new_rd_Not(dbg, block, bl, mode);
3894 n = new_rd_And(dbg, block, bl, a, mode);
3895 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3899 if (is_Not(a) && is_Not(b)) {
3900 /* ~a & ~b = ~(a|b) */
3901 ir_node *block = get_nodes_block(n);
3902 ir_mode *mode = get_irn_mode(n);
3906 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3907 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3908 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3913 vrp_attr *b_vrp = vrp_get_info(b);
3914 ir_tarval *a_val = get_Const_tarval(a);
3915 if (b_vrp != NULL && tarval_or(a_val, b_vrp->bits_not_set) == a_val) {
3921 vrp_attr *a_vrp = vrp_get_info(a);
3922 ir_tarval *b_val = get_Const_tarval(b);
3923 if (a_vrp != NULL && tarval_or(b_val, a_vrp->bits_not_set) == b_val) {
3928 n = transform_bitwise_distributive(n, transform_node_And);
3930 n = transform_node_bitop_shift(n);
3938 static ir_node *transform_node_Not(ir_node *n)
3940 ir_node *c, *oldn = n;
3941 ir_node *a = get_Not_op(n);
3942 ir_mode *mode = get_irn_mode(n);
3944 HANDLE_UNOP_PHI(tarval_not,a,c);
3946 /* check for a boolean Not */
3948 dbg_info *dbgi = get_irn_dbg_info(a);
3949 ir_node *block = get_nodes_block(a);
3950 ir_relation relation = get_Cmp_relation(a);
3951 relation = get_negated_relation(relation);
3952 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3953 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3957 /* normalize ~(a ^ b) => a ^ ~b */
3958 if (is_Eor(a) || is_Or_Eor_Add(a)) {
3959 dbg_info *dbg = get_irn_dbg_info(n);
3960 ir_node *block = get_nodes_block(n);
3961 ir_node *eor_right = get_binop_right(a);
3962 ir_node *eor_left = get_binop_left(a);
3963 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3964 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3968 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3969 if (is_Minus(a)) { /* ~-x -> x + -1 */
3970 dbg_info *dbg = get_irn_dbg_info(n);
3971 ir_graph *irg = get_irn_irg(n);
3972 ir_node *block = get_nodes_block(n);
3973 ir_node *add_l = get_Minus_op(a);
3974 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3975 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3976 } else if (is_Add(a) || is_Or_Eor_Add(a)) {
3977 ir_node *add_r = get_binop_right(a);
3978 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3979 /* ~(x + -1) = -x */
3980 ir_node *op = get_binop_left(a);
3981 ir_node *blk = get_nodes_block(n);
3982 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3983 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3991 * Transform a Minus.
3995 * -(a >>u (size-1)) = a >>s (size-1)
3996 * -(a >>s (size-1)) = a >>u (size-1)
3997 * -(a * const) -> a * -const
3999 static ir_node *transform_node_Minus(ir_node *n)
4001 ir_node *c, *oldn = n;
4002 ir_node *a = get_Minus_op(n);
4005 HANDLE_UNOP_PHI(tarval_neg,a,c);
4007 mode = get_irn_mode(a);
4008 if (get_mode_arithmetic(mode) == irma_twos_complement) {
4009 /* the following rules are only to twos-complement */
4012 ir_node *op = get_Not_op(a);
4013 ir_tarval *tv = get_mode_one(mode);
4014 ir_node *blk = get_nodes_block(n);
4015 ir_graph *irg = get_irn_irg(blk);
4016 ir_node *c = new_r_Const(irg, tv);
4017 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
4018 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
4022 ir_node *c = get_Shr_right(a);
4025 ir_tarval *tv = get_Const_tarval(c);
4027 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4028 /* -(a >>u (size-1)) = a >>s (size-1) */
4029 ir_node *v = get_Shr_left(a);
4031 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4032 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4038 ir_node *c = get_Shrs_right(a);
4041 ir_tarval *tv = get_Const_tarval(c);
4043 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
4044 /* -(a >>s (size-1)) = a >>u (size-1) */
4045 ir_node *v = get_Shrs_left(a);
4047 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
4048 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
4055 /* - (a-b) = b - a */
4056 ir_node *la = get_Sub_left(a);
4057 ir_node *ra = get_Sub_right(a);
4058 ir_node *blk = get_nodes_block(n);
4060 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
4061 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
4065 if (is_Mul(a)) { /* -(a * const) -> a * -const */
4066 ir_node *mul_l = get_Mul_left(a);
4067 ir_node *mul_r = get_Mul_right(a);
4068 ir_tarval *tv = value_of(mul_r);
4069 if (tv != tarval_bad) {
4070 tv = tarval_neg(tv);
4071 if (tv != tarval_bad) {
4072 ir_graph *irg = get_irn_irg(n);
4073 ir_node *cnst = new_r_Const(irg, tv);
4074 dbg_info *dbg = get_irn_dbg_info(a);
4075 ir_node *block = get_nodes_block(a);
4076 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
4077 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
4087 * Transform a Proj(Load) with a non-null address.
4089 static ir_node *transform_node_Proj_Load(ir_node *proj)
4091 if (get_opt_ldst_only_null_ptr_exceptions()) {
4092 if (get_irn_mode(proj) == mode_X) {
4093 ir_node *load = get_Proj_pred(proj);
4095 /* get the Load address */
4096 const ir_node *addr = get_Load_ptr(load);
4097 const ir_node *confirm;
4099 if (value_not_null(addr, &confirm)) {
4100 if (confirm == NULL) {
4101 /* this node may float if it did not depend on a Confirm */
4102 set_irn_pinned(load, op_pin_state_floats);
4104 if (get_Proj_proj(proj) == pn_Load_X_except) {
4105 ir_graph *irg = get_irn_irg(proj);
4106 DBG_OPT_EXC_REM(proj);
4107 return new_r_Bad(irg, mode_X);
4109 ir_node *blk = get_nodes_block(load);
4110 return new_r_Jmp(blk);
4119 * Transform a Proj(Store) with a non-null address.
4121 static ir_node *transform_node_Proj_Store(ir_node *proj)
4123 if (get_opt_ldst_only_null_ptr_exceptions()) {
4124 if (get_irn_mode(proj) == mode_X) {
4125 ir_node *store = get_Proj_pred(proj);
4127 /* get the load/store address */
4128 const ir_node *addr = get_Store_ptr(store);
4129 const ir_node *confirm;
4131 if (value_not_null(addr, &confirm)) {
4132 if (confirm == NULL) {
4133 /* this node may float if it did not depend on a Confirm */
4134 set_irn_pinned(store, op_pin_state_floats);
4136 if (get_Proj_proj(proj) == pn_Store_X_except) {
4137 ir_graph *irg = get_irn_irg(proj);
4138 DBG_OPT_EXC_REM(proj);
4139 return new_r_Bad(irg, mode_X);
4141 ir_node *blk = get_nodes_block(store);
4142 return new_r_Jmp(blk);
4151 * Transform a Proj(Div) with a non-zero value.
4152 * Removes the exceptions and routes the memory to the NoMem node.
4154 static ir_node *transform_node_Proj_Div(ir_node *proj)
4156 ir_node *div = get_Proj_pred(proj);
4157 ir_node *b = get_Div_right(div);
4158 ir_node *res, *new_mem;
4159 const ir_node *confirm;
4162 if (value_not_zero(b, &confirm)) {
4163 /* div(x, y) && y != 0 */
4164 if (confirm == NULL) {
4165 /* we are sure we have a Const != 0 */
4166 new_mem = get_Div_mem(div);
4167 new_mem = skip_Pin(new_mem);
4168 set_Div_mem(div, new_mem);
4169 set_irn_pinned(div, op_pin_state_floats);
4172 proj_nr = get_Proj_proj(proj);
4174 case pn_Div_X_regular:
4175 return new_r_Jmp(get_nodes_block(div));
4177 case pn_Div_X_except: {
4178 ir_graph *irg = get_irn_irg(proj);
4179 /* we found an exception handler, remove it */
4180 DBG_OPT_EXC_REM(proj);
4181 return new_r_Bad(irg, mode_X);
4185 ir_graph *irg = get_irn_irg(proj);
4186 res = get_Div_mem(div);
4187 new_mem = get_irg_no_mem(irg);
4190 /* This node can only float up to the Confirm block */
4191 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4193 set_irn_pinned(div, op_pin_state_floats);
4194 /* this is a Div without exception, we can remove the memory edge */
4195 set_Div_mem(div, new_mem);
4204 * Transform a Proj(Mod) with a non-zero value.
4205 * Removes the exceptions and routes the memory to the NoMem node.
4207 static ir_node *transform_node_Proj_Mod(ir_node *proj)
4209 ir_node *mod = get_Proj_pred(proj);
4210 ir_node *b = get_Mod_right(mod);
4211 ir_node *res, *new_mem;
4212 const ir_node *confirm;
4215 if (value_not_zero(b, &confirm)) {
4216 /* mod(x, y) && y != 0 */
4217 proj_nr = get_Proj_proj(proj);
4219 if (confirm == NULL) {
4220 /* we are sure we have a Const != 0 */
4221 new_mem = get_Mod_mem(mod);
4222 new_mem = skip_Pin(new_mem);
4223 set_Mod_mem(mod, new_mem);
4224 set_irn_pinned(mod, op_pin_state_floats);
4229 case pn_Mod_X_regular:
4230 return new_r_Jmp(get_irn_n(mod, -1));
4232 case pn_Mod_X_except: {
4233 ir_graph *irg = get_irn_irg(proj);
4234 /* we found an exception handler, remove it */
4235 DBG_OPT_EXC_REM(proj);
4236 return new_r_Bad(irg, mode_X);
4240 ir_graph *irg = get_irn_irg(proj);
4241 res = get_Mod_mem(mod);
4242 new_mem = get_irg_no_mem(irg);
4245 /* This node can only float up to the Confirm block */
4246 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4248 /* this is a Mod without exception, we can remove the memory edge */
4249 set_Mod_mem(mod, new_mem);
4253 if (get_Mod_left(mod) == b) {
4254 /* a % a = 0 if a != 0 */
4255 ir_graph *irg = get_irn_irg(proj);
4256 ir_mode *mode = get_irn_mode(proj);
4257 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4259 DBG_OPT_CSTEVAL(mod, res);
4268 * return true if the operation returns a value with exactly 1 bit set
4270 static bool is_single_bit(const ir_node *node)
4272 /* a first implementation, could be extended with vrp and others... */
4274 ir_node *shl_l = get_Shl_left(node);
4275 ir_mode *mode = get_irn_mode(node);
4276 int modulo = get_mode_modulo_shift(mode);
4277 /* this works if we shift a 1 and we have modulo shift */
4278 if (is_Const(shl_l) && is_Const_one(shl_l)
4279 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
4282 } else if (is_Const(node)) {
4283 ir_tarval *tv = get_Const_tarval(node);
4284 return tarval_is_single_bit(tv);
4290 * checks if node just flips a bit in another node and returns that other node
4291 * if so. @p tv should be a value having just 1 bit set
4293 static ir_node *flips_bit(const ir_node *node, ir_tarval *tv)
4296 return get_Not_op(node);
4298 ir_node *right = get_Eor_right(node);
4299 if (is_Const(right)) {
4300 ir_tarval *right_tv = get_Const_tarval(right);
4301 ir_mode *mode = get_irn_mode(node);
4302 if (tarval_and(right_tv, tv) != get_mode_null(mode))
4303 return get_Eor_left(node);
4310 * Normalizes and optimizes Cmp nodes.
4312 static ir_node *transform_node_Cmp(ir_node *n)
4314 ir_node *left = get_Cmp_left(n);
4315 ir_node *right = get_Cmp_right(n);
4316 ir_mode *mode = get_irn_mode(left);
4317 ir_tarval *tv = NULL;
4318 bool changed = false;
4319 bool changedc = false;
4320 ir_relation relation = get_Cmp_relation(n);
4321 ir_relation possible = ir_get_possible_cmp_relations(left, right);
4323 /* mask out impossible relations */
4324 ir_relation new_relation = relation & possible;
4325 if (new_relation != relation) {
4326 relation = new_relation;
4330 /* Remove unnecessary conversions */
4331 if (is_Conv(left) && is_Conv(right)) {
4332 ir_node *op_left = get_Conv_op(left);
4333 ir_node *op_right = get_Conv_op(right);
4334 ir_mode *mode_left = get_irn_mode(op_left);
4335 ir_mode *mode_right = get_irn_mode(op_right);
4337 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4338 && mode_left != mode_b && mode_right != mode_b) {
4339 ir_node *block = get_nodes_block(n);
4341 if (mode_left == mode_right) {
4345 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4346 } else if (smaller_mode(mode_left, mode_right)) {
4347 left = new_r_Conv(block, op_left, mode_right);
4350 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4351 } else if (smaller_mode(mode_right, mode_left)) {
4353 right = new_r_Conv(block, op_right, mode_left);
4355 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4357 mode = get_irn_mode(left);
4360 if (is_Conv(left) && is_Const(right)) {
4361 ir_node *op_left = get_Conv_op(left);
4362 ir_mode *mode_left = get_irn_mode(op_left);
4363 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
4364 ir_tarval *tv = get_Const_tarval(right);
4365 tarval_int_overflow_mode_t last_mode
4366 = tarval_get_integer_overflow_mode();
4368 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
4369 new_tv = tarval_convert_to(tv, mode_left);
4370 tarval_set_integer_overflow_mode(last_mode);
4371 if (new_tv != tarval_bad) {
4372 ir_graph *irg = get_irn_irg(n);
4374 right = new_r_Const(irg, new_tv);
4375 mode = get_irn_mode(left);
4377 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4383 * Optimize -a CMP -b into b CMP a.
4384 * This works only for modes where unary Minus cannot Overflow.
4385 * Note that two-complement integers can Overflow so it will NOT work.
4387 if (!mode_overflow_on_unary_Minus(mode) &&
4388 is_Minus(left) && is_Minus(right)) {
4389 left = get_Minus_op(left);
4390 right = get_Minus_op(right);
4391 relation = get_inversed_relation(relation);
4393 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4396 /* remove operation on both sides if possible */
4397 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4399 * The following operations are NOT safe for floating point operations, for instance
4400 * 1.0 + inf == 2.0 + inf, =/=> x == y
4402 if (mode_is_int(mode)) {
4403 unsigned lop = get_irn_opcode(left);
4405 if (lop == get_irn_opcode(right)) {
4406 ir_node *ll, *lr, *rl, *rr;
4408 /* same operation on both sides, try to remove */
4412 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4413 left = get_unop_op(left);
4414 right = get_unop_op(right);
4416 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4419 ll = get_Add_left(left);
4420 lr = get_Add_right(left);
4421 rl = get_Add_left(right);
4422 rr = get_Add_right(right);
4425 /* X + a CMP X + b ==> a CMP b */
4429 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4430 } else if (ll == rr) {
4431 /* X + a CMP b + X ==> a CMP b */
4435 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4436 } else if (lr == rl) {
4437 /* a + X CMP X + b ==> a CMP b */
4441 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4442 } else if (lr == rr) {
4443 /* a + X CMP b + X ==> a CMP b */
4447 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4451 ll = get_Sub_left(left);
4452 lr = get_Sub_right(left);
4453 rl = get_Sub_left(right);
4454 rr = get_Sub_right(right);
4457 /* X - a CMP X - b ==> a CMP b */
4461 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4462 } else if (lr == rr) {
4463 /* a - X CMP b - X ==> a CMP b */
4467 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4471 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4472 /* a ROTL X CMP b ROTL X ==> a CMP b */
4473 left = get_Rotl_left(left);
4474 right = get_Rotl_left(right);
4476 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4484 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4485 if (is_Add(left) || is_Sub(left) || is_Or_Eor_Add(left)) {
4486 ir_node *ll = get_binop_left(left);
4487 ir_node *lr = get_binop_right(left);
4489 if (lr == right && (is_Add(left) || is_Or_Eor_Add(left))) {
4495 ir_graph *irg = get_irn_irg(n);
4497 right = create_zero_const(irg, mode);
4499 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4502 if (is_Add(right) || is_Sub(right) || is_Or_Eor_Add(right)) {
4503 ir_node *rl = get_binop_left(right);
4504 ir_node *rr = get_binop_right(right);
4506 if (rr == left && (is_Add(right) || is_Or_Eor_Add(right))) {
4512 ir_graph *irg = get_irn_irg(n);
4514 right = create_zero_const(irg, mode);
4516 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4520 if (is_And(left) && is_Const(right)) {
4521 ir_node *ll = get_binop_left(left);
4522 ir_node *lr = get_binop_right(left);
4523 if (is_Shr(ll) && is_Const(lr)) {
4524 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4525 ir_node *block = get_nodes_block(n);
4526 ir_mode *mode = get_irn_mode(left);
4528 ir_node *llr = get_Shr_right(ll);
4529 if (is_Const(llr)) {
4530 dbg_info *dbg = get_irn_dbg_info(left);
4531 ir_graph *irg = get_irn_irg(left);
4533 ir_tarval *c1 = get_Const_tarval(llr);
4534 ir_tarval *c2 = get_Const_tarval(lr);
4535 ir_tarval *c3 = get_Const_tarval(right);
4536 ir_tarval *mask = tarval_shl(c2, c1);
4537 ir_tarval *value = tarval_shl(c3, c1);
4539 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4540 right = new_r_Const(irg, value);
4545 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4547 if (is_Const(right) && is_Const_null(right) &&
4548 (is_Eor(left) || is_Or_Eor_Add(left))) {
4549 right = get_Eor_right(left);
4550 left = get_Eor_left(left);
4556 if (mode_is_int(mode) && is_And(left)) {
4557 /* a complicated Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4558 * by the simpler Cmp(And(1bit, val), 0) negated pnc */
4559 if (relation == ir_relation_equal
4560 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4561 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less)) {
4562 ir_node *and0 = get_And_left(left);
4563 ir_node *and1 = get_And_right(left);
4564 if (and1 == right) {
4565 ir_node *tmp = and0;
4569 if (and0 == right && is_single_bit(and0)) {
4570 ir_graph *irg = get_irn_irg(n);
4572 relation == ir_relation_equal ? ir_relation_less_greater
4573 : ir_relation_equal;
4574 right = create_zero_const(irg, mode);
4580 if (is_Const(right) && is_Const_null(right) &&
4581 (relation == ir_relation_equal
4582 || (relation == ir_relation_less_greater)
4583 || (!mode_is_signed(mode) && relation == ir_relation_greater))) {
4585 /* instead of flipping the bit before the bit-test operation negate
4587 ir_node *and0 = get_And_left(left);
4588 ir_node *and1 = get_And_right(left);
4589 if (is_Const(and1)) {
4590 ir_tarval *tv = get_Const_tarval(and1);
4591 if (tarval_is_single_bit(tv)) {
4592 ir_node *flipped = flips_bit(and0, tv);
4593 if (flipped != NULL) {
4594 dbg_info *dbgi = get_irn_dbg_info(left);
4595 ir_node *block = get_nodes_block(left);
4596 relation = get_negated_relation(relation);
4597 left = new_rd_And(dbgi, block, flipped, and1, mode);
4606 /* replace mode_b compares with ands/ors */
4607 if (mode == mode_b) {
4608 ir_node *block = get_nodes_block(n);
4612 case ir_relation_less_equal:
4613 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4615 case ir_relation_less:
4616 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4618 case ir_relation_greater_equal:
4619 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4621 case ir_relation_greater:
4622 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4624 case ir_relation_less_greater:
4625 bres = new_r_Eor(block, left, right, mode_b);
4627 case ir_relation_equal:
4628 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4631 #ifdef DEBUG_libfirm
4632 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4637 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4643 * First step: normalize the compare op
4644 * by placing the constant on the right side
4645 * or moving the lower address node to the left.
4647 if (!operands_are_normalized(left, right)) {
4652 relation = get_inversed_relation(relation);
4657 * Second step: Try to reduce the magnitude
4658 * of a constant. This may help to generate better code
4659 * later and may help to normalize more compares.
4660 * Of course this is only possible for integer values.
4662 tv = value_of(right);
4663 if (tv != tarval_bad) {
4664 ir_mode *mode = get_irn_mode(right);
4666 /* cmp(mux(x, cf, ct), c2) can be eliminated:
4667 * cmp(ct,c2) | cmp(cf,c2) | result
4668 * -----------|------------|--------
4669 * true | true | True
4670 * false | false | False
4672 * false | true | not(x)
4675 ir_node *mux_true = get_Mux_true(left);
4676 ir_node *mux_false = get_Mux_false(left);
4677 if (is_Const(mux_true) && is_Const(mux_false)) {
4678 /* we can fold true/false constant separately */
4679 ir_tarval *tv_true = get_Const_tarval(mux_true);
4680 ir_tarval *tv_false = get_Const_tarval(mux_false);
4681 ir_relation r_true = tarval_cmp(tv_true, tv);
4682 ir_relation r_false = tarval_cmp(tv_false, tv);
4683 if (r_true != ir_relation_false
4684 || r_false != ir_relation_false) {
4685 bool rel_true = (r_true & relation) != 0;
4686 bool rel_false = (r_false & relation) != 0;
4687 ir_node *cond = get_Mux_sel(left);
4688 if (rel_true == rel_false) {
4689 relation = rel_true ? ir_relation_true
4690 : ir_relation_false;
4691 } else if (rel_true) {
4694 dbg_info *dbgi = get_irn_dbg_info(n);
4695 ir_node *block = get_nodes_block(n);
4696 ir_node *notn = new_rd_Not(dbgi, block, cond, mode_b);
4703 /* TODO extend to arbitrary constants */
4704 if (is_Conv(left) && tarval_is_null(tv)) {
4705 ir_node *op = get_Conv_op(left);
4706 ir_mode *op_mode = get_irn_mode(op);
4709 * UpConv(x) REL 0 ==> x REL 0
4710 * Don't do this for float values as it's unclear whether it is a
4711 * win. (on the other side it makes detection/creation of fabs hard)
4713 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4714 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4715 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4716 !mode_is_float(mode)) {
4717 tv = get_mode_null(op_mode);
4721 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4725 if (tv != tarval_bad) {
4726 /* the following optimization is possible on modes without Overflow
4727 * on Unary Minus or on == and !=:
4728 * -a CMP c ==> a swap(CMP) -c
4730 * Beware: for two-complement Overflow may occur, so only == and != can
4731 * be optimized, see this:
4732 * -MININT < 0 =/=> MININT > 0 !!!
4734 if (is_Minus(left) &&
4735 (!mode_overflow_on_unary_Minus(mode) ||
4736 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4737 tv = tarval_neg(tv);
4739 if (tv != tarval_bad) {
4740 left = get_Minus_op(left);
4741 relation = get_inversed_relation(relation);
4743 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4745 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4746 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4747 tv = tarval_not(tv);
4749 if (tv != tarval_bad) {
4750 left = get_Not_op(left);
4752 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4756 /* for integer modes, we have more */
4757 if (mode_is_int(mode) && !is_Const(left)) {
4758 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4759 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4760 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4761 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4763 if (tv != tarval_bad) {
4764 relation ^= ir_relation_equal;
4766 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4769 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4770 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4771 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4772 tv = tarval_add(tv, get_mode_one(mode));
4774 if (tv != tarval_bad) {
4775 relation ^= ir_relation_equal;
4777 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4781 /* the following reassociations work only for == and != */
4782 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4783 if (tv != tarval_bad) {
4784 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4786 ir_node *c1 = get_Sub_right(left);
4787 ir_tarval *tv2 = value_of(c1);
4789 if (tv2 != tarval_bad) {
4790 tv2 = tarval_add(tv, value_of(c1));
4792 if (tv2 != tarval_bad) {
4793 left = get_Sub_left(left);
4796 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4800 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4801 else if (is_Add(left) || is_Or_Eor_Add(left)) {
4802 ir_node *a_l = get_binop_left(left);
4803 ir_node *a_r = get_binop_right(left);
4807 if (is_Const(a_l)) {
4809 tv2 = value_of(a_l);
4812 tv2 = value_of(a_r);
4815 if (tv2 != tarval_bad) {
4816 tv2 = tarval_sub(tv, tv2, NULL);
4818 if (tv2 != tarval_bad) {
4822 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4826 /* -a == c ==> a == -c, -a != c ==> a != -c */
4827 else if (is_Minus(left)) {
4828 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4830 if (tv2 != tarval_bad) {
4831 left = get_Minus_op(left);
4834 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4841 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4842 switch (get_irn_opcode(left)) {
4846 c1 = get_And_right(left);
4849 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4850 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4852 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4854 /* TODO: move to constant evaluation */
4855 ir_graph *irg = get_irn_irg(n);
4856 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4857 c1 = new_r_Const(irg, tv);
4858 DBG_OPT_CSTEVAL(n, c1);
4862 if (tarval_is_single_bit(tv)) {
4864 * optimization for AND:
4866 * And(x, C) == C ==> And(x, C) != 0
4867 * And(x, C) != C ==> And(X, C) == 0
4869 * if C is a single Bit constant.
4872 /* check for Constant's match. We have check hare the tarvals,
4873 because our const might be changed */
4874 if (get_Const_tarval(c1) == tv) {
4875 /* fine: do the transformation */
4876 tv = get_mode_null(get_tarval_mode(tv));
4877 relation ^= ir_relation_less_equal_greater;
4879 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4885 c1 = get_Or_right(left);
4886 if (is_Const(c1) && tarval_is_null(tv)) {
4888 * Or(x, C) == 0 && C != 0 ==> FALSE
4889 * Or(x, C) != 0 && C != 0 ==> TRUE
4891 if (! tarval_is_null(get_Const_tarval(c1))) {
4892 /* TODO: move to constant evaluation */
4893 ir_graph *irg = get_irn_irg(n);
4894 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4895 c1 = new_r_Const(irg, tv);
4896 DBG_OPT_CSTEVAL(n, c1);
4903 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4905 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4908 c1 = get_Shl_right(left);
4910 ir_graph *irg = get_irn_irg(c1);
4911 ir_tarval *tv1 = get_Const_tarval(c1);
4912 ir_mode *mode = get_irn_mode(left);
4913 ir_tarval *minus1 = get_mode_all_one(mode);
4914 ir_tarval *amask = tarval_shr(minus1, tv1);
4915 ir_tarval *cmask = tarval_shl(minus1, tv1);
4918 if (tarval_and(tv, cmask) != tv) {
4919 /* condition not met */
4920 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4921 c1 = new_r_Const(irg, tv);
4922 DBG_OPT_CSTEVAL(n, c1);
4925 sl = get_Shl_left(left);
4926 blk = get_nodes_block(n);
4927 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4928 tv = tarval_shr(tv, tv1);
4930 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4935 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4937 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4940 c1 = get_Shr_right(left);
4942 ir_graph *irg = get_irn_irg(c1);
4943 ir_tarval *tv1 = get_Const_tarval(c1);
4944 ir_mode *mode = get_irn_mode(left);
4945 ir_tarval *minus1 = get_mode_all_one(mode);
4946 ir_tarval *amask = tarval_shl(minus1, tv1);
4947 ir_tarval *cmask = tarval_shr(minus1, tv1);
4950 if (tarval_and(tv, cmask) != tv) {
4951 /* condition not met */
4952 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4953 c1 = new_r_Const(irg, tv);
4954 DBG_OPT_CSTEVAL(n, c1);
4957 sl = get_Shr_left(left);
4958 blk = get_nodes_block(n);
4959 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4960 tv = tarval_shl(tv, tv1);
4962 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4967 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4969 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4972 c1 = get_Shrs_right(left);
4974 ir_graph *irg = get_irn_irg(c1);
4975 ir_tarval *tv1 = get_Const_tarval(c1);
4976 ir_mode *mode = get_irn_mode(left);
4977 ir_tarval *minus1 = get_mode_all_one(mode);
4978 ir_tarval *amask = tarval_shl(minus1, tv1);
4979 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4982 cond = tarval_sub(cond, tv1, NULL);
4983 cond = tarval_shrs(tv, cond);
4985 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4986 /* condition not met */
4987 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4988 c1 = new_r_Const(irg, tv);
4989 DBG_OPT_CSTEVAL(n, c1);
4992 sl = get_Shrs_left(left);
4993 blk = get_nodes_block(n);
4994 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4995 tv = tarval_shl(tv, tv1);
4997 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
5005 if (changedc) { /* need a new Const */
5006 ir_graph *irg = get_irn_irg(n);
5007 right = new_r_Const(irg, tv);
5011 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
5012 ir_node *op = get_Proj_pred(left);
5014 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
5015 ir_node *c = get_binop_right(op);
5018 ir_tarval *tv = get_Const_tarval(c);
5020 if (tarval_is_single_bit(tv)) {
5021 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
5022 ir_node *v = get_binop_left(op);
5023 ir_node *blk = get_irn_n(op, -1);
5024 ir_graph *irg = get_irn_irg(op);
5025 ir_mode *mode = get_irn_mode(v);
5027 tv = tarval_sub(tv, get_mode_one(mode), NULL);
5028 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
5030 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
5037 dbg_info *dbgi = get_irn_dbg_info(n);
5038 ir_node *block = get_nodes_block(n);
5040 /* create a new compare */
5041 n = new_rd_Cmp(dbgi, block, left, right, relation);
5048 * Optimize CopyB(mem, x, x) into a Nop.
5050 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
5052 ir_node *copyb = get_Proj_pred(proj);
5053 ir_node *a = get_CopyB_dst(copyb);
5054 ir_node *b = get_CopyB_src(copyb);
5057 switch (get_Proj_proj(proj)) {
5058 case pn_CopyB_X_regular:
5059 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
5060 DBG_OPT_EXC_REM(proj);
5061 proj = new_r_Jmp(get_nodes_block(copyb));
5063 case pn_CopyB_X_except: {
5064 ir_graph *irg = get_irn_irg(proj);
5065 DBG_OPT_EXC_REM(proj);
5066 proj = new_r_Bad(irg, mode_X);
5077 * Optimize Bounds(idx, idx, upper) into idx.
5079 static ir_node *transform_node_Proj_Bound(ir_node *proj)
5081 ir_node *oldn = proj;
5082 ir_node *bound = get_Proj_pred(proj);
5083 ir_node *idx = get_Bound_index(bound);
5084 ir_node *pred = skip_Proj(idx);
5087 if (idx == get_Bound_lower(bound))
5089 else if (is_Bound(pred)) {
5091 * idx was Bounds checked previously, it is still valid if
5092 * lower <= pred_lower && pred_upper <= upper.
5094 ir_node *lower = get_Bound_lower(bound);
5095 ir_node *upper = get_Bound_upper(bound);
5096 if (get_Bound_lower(pred) == lower &&
5097 get_Bound_upper(pred) == upper) {
5099 * One could expect that we simply return the previous
5100 * Bound here. However, this would be wrong, as we could
5101 * add an exception Proj to a new location then.
5102 * So, we must turn in into a tuple.
5108 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
5109 switch (get_Proj_proj(proj)) {
5111 DBG_OPT_EXC_REM(proj);
5112 proj = get_Bound_mem(bound);
5114 case pn_Bound_X_except:
5115 DBG_OPT_EXC_REM(proj);
5116 proj = new_r_Bad(get_irn_irg(proj), mode_X);
5120 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
5122 case pn_Bound_X_regular:
5123 DBG_OPT_EXC_REM(proj);
5124 proj = new_r_Jmp(get_nodes_block(bound));
5134 * Does all optimizations on nodes that must be done on its Projs
5135 * because of creating new nodes.
5137 static ir_node *transform_node_Proj(ir_node *proj)
5139 ir_node *n = get_Proj_pred(proj);
5141 if (n->op->ops.transform_node_Proj)
5142 return n->op->ops.transform_node_Proj(proj);
5147 * Test whether a block is unreachable
5148 * Note: That this only returns true when
5149 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
5150 * This is important, as you easily end up producing invalid constructs in the
5151 * unreachable code when optimizing away edges into the unreachable code.
5152 * So only set this flag when you iterate localopts to the fixpoint.
5153 * When you reach the fixpoint then all unreachable code is dead
5154 * (= can't be reached by firm edges) and you won't see the invalid constructs
5157 static bool is_block_unreachable(const ir_node *block)
5159 const ir_graph *irg = get_irn_irg(block);
5160 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5162 return get_Block_dom_depth(block) < 0;
5165 static ir_node *transform_node_Block(ir_node *block)
5167 ir_graph *irg = get_irn_irg(block);
5168 int arity = get_irn_arity(block);
5169 ir_node *bad = NULL;
5172 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
5175 for (i = 0; i < arity; ++i) {
5176 ir_node *const pred = get_Block_cfgpred(block, i);
5177 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
5180 bad = new_r_Bad(irg, mode_X);
5181 set_irn_n(block, i, bad);
5187 static ir_node *transform_node_Phi(ir_node *phi)
5189 int n = get_irn_arity(phi);
5190 ir_mode *mode = get_irn_mode(phi);
5191 ir_node *block = get_nodes_block(phi);
5192 ir_graph *irg = get_irn_irg(phi);
5193 ir_node *bad = NULL;
5196 /* Set phi-operands for bad-block inputs to bad */
5197 for (i = 0; i < n; ++i) {
5198 if (!is_Bad(get_Phi_pred(phi, i))) {
5199 ir_node *pred = get_Block_cfgpred(block, i);
5200 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
5202 bad = new_r_Bad(irg, mode);
5203 set_irn_n(phi, i, bad);
5208 /* Move Pin nodes down through Phi nodes. */
5209 if (mode == mode_M) {
5210 n = get_irn_arity(phi);
5212 /* Beware of Phi0 */
5216 bool has_pin = false;
5218 NEW_ARR_A(ir_node *, in, n);
5220 for (i = 0; i < n; ++i) {
5221 ir_node *pred = get_irn_n(phi, i);
5224 in[i] = get_Pin_op(pred);
5226 } else if (is_Bad(pred)) {
5236 /* Move the Pin nodes "behind" the Phi. */
5237 block = get_irn_n(phi, -1);
5238 new_phi = new_r_Phi(block, n, in, mode_M);
5239 return new_r_Pin(block, new_phi);
5242 /* Move Confirms down through Phi nodes. */
5243 else if (mode_is_reference(mode)) {
5244 n = get_irn_arity(phi);
5246 /* Beware of Phi0 */
5248 ir_node *pred = get_irn_n(phi, 0);
5249 ir_node *bound, *new_phi, *block, **in;
5250 ir_relation relation;
5251 bool has_confirm = false;
5253 if (! is_Confirm(pred))
5256 bound = get_Confirm_bound(pred);
5257 relation = get_Confirm_relation(pred);
5259 NEW_ARR_A(ir_node *, in, n);
5260 in[0] = get_Confirm_value(pred);
5262 for (i = 1; i < n; ++i) {
5263 pred = get_irn_n(phi, i);
5265 if (is_Confirm(pred) &&
5266 get_Confirm_bound(pred) == bound &&
5267 get_Confirm_relation(pred) == relation) {
5268 in[i] = get_Confirm_value(pred);
5270 } else if (is_Bad(pred)) {
5280 /* move the Confirm nodes "behind" the Phi */
5281 block = get_irn_n(phi, -1);
5282 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
5283 return new_r_Confirm(block, new_phi, bound, relation);
5290 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5292 * Should be moved to reassociation?
5294 static ir_node *transform_node_shift(ir_node *n)
5296 ir_node *left, *right;
5298 ir_mode *count_mode;
5299 ir_tarval *tv1, *tv2, *res;
5300 ir_node *in[2], *irn, *block;
5304 left = get_binop_left(n);
5306 /* different operations */
5307 if (get_irn_op(left) != get_irn_op(n))
5310 right = get_binop_right(n);
5311 tv1 = value_of(right);
5312 if (tv1 == tarval_bad)
5315 tv2 = value_of(get_binop_right(left));
5316 if (tv2 == tarval_bad)
5319 count_mode = get_tarval_mode(tv1);
5320 if (get_tarval_mode(tv2) != count_mode) {
5321 /* TODO: search bigger mode or something and convert... */
5325 mode = get_irn_mode(n);
5326 modulo_shf = get_mode_modulo_shift(mode);
5328 if (modulo_shf > 0) {
5329 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5331 /* I'm not so sure what happens in one complement... */
5332 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5333 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5334 * above will be invalid) */
5335 assert(modulo_shf<=0 || is_po2(modulo_shf));
5337 tv1 = tarval_and(tv1, modulo_mask);
5338 tv2 = tarval_and(tv2, modulo_mask);
5340 res = tarval_add(tv1, tv2);
5341 irg = get_irn_irg(n);
5343 /* beware: a simple replacement works only, if res < modulo shift */
5345 int bits = get_mode_size_bits(mode);
5346 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5347 res = tarval_mod(res, modulo);
5349 long bits = get_mode_size_bits(mode);
5350 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5352 /* shifting too much */
5353 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5355 ir_node *block = get_nodes_block(n);
5356 dbg_info *dbgi = get_irn_dbg_info(n);
5357 ir_mode *smode = get_irn_mode(right);
5358 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5359 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5362 return new_r_Const(irg, get_mode_null(mode));
5366 /* ok, we can replace it */
5367 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5368 block = get_nodes_block(n);
5370 in[0] = get_binop_left(left);
5371 in[1] = new_r_Const(irg, res);
5373 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5375 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5382 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5384 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5385 * (also with x >>s c1 when c1>=c2)
5387 static ir_node *transform_node_shl_shr(ir_node *n)
5390 ir_node *right = get_binop_right(n);
5400 ir_tarval *tv_shift;
5403 ir_relation relation;
5406 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5408 if (!is_Const(right))
5411 left = get_binop_left(n);
5412 mode = get_irn_mode(n);
5413 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5414 ir_node *shr_right = get_binop_right(left);
5416 if (!is_Const(shr_right))
5419 x = get_binop_left(left);
5420 tv_shr = get_Const_tarval(shr_right);
5421 tv_shl = get_Const_tarval(right);
5423 if (is_Shrs(left)) {
5424 /* shrs variant only allowed if c1 >= c2 */
5425 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5428 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5431 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5433 tv_mask = tarval_shl(tv_mask, tv_shl);
5434 } else if (is_Shr(n) && is_Shl(left)) {
5435 ir_node *shl_right = get_Shl_right(left);
5437 if (!is_Const(shl_right))
5440 x = get_Shl_left(left);
5441 tv_shr = get_Const_tarval(right);
5442 tv_shl = get_Const_tarval(shl_right);
5444 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5445 tv_mask = tarval_shr(tv_mask, tv_shr);
5450 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5451 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5454 assert(tv_mask != tarval_bad);
5455 assert(get_tarval_mode(tv_mask) == mode);
5457 block = get_nodes_block(n);
5458 irg = get_irn_irg(block);
5459 dbgi = get_irn_dbg_info(n);
5461 relation = tarval_cmp(tv_shl, tv_shr);
5462 if (relation == ir_relation_less || relation == ir_relation_equal) {
5463 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5464 new_const = new_r_Const(irg, tv_shift);
5466 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5468 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5471 assert(relation == ir_relation_greater);
5472 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5473 new_const = new_r_Const(irg, tv_shift);
5474 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5477 new_const = new_r_Const(irg, tv_mask);
5478 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5483 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5485 ir_mode *mode = get_tarval_mode(tv);
5486 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5487 return tarval_mod(tv, modulo_tv);
5490 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5491 ir_node *left, ir_node *right, ir_mode *mode);
5494 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5495 * then we can use that to minimize the value of Add(x, const) or
5496 * Sub(Const, x). In particular this often avoids 1 instruction in some
5497 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5498 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5500 static ir_node *transform_node_shift_modulo(ir_node *n,
5501 new_shift_func new_shift)
5503 ir_mode *mode = get_irn_mode(n);
5504 int modulo = get_mode_modulo_shift(mode);
5505 ir_node *newop = NULL;
5506 ir_mode *mode_right;
5513 if (get_mode_arithmetic(mode) != irma_twos_complement)
5515 if (!is_po2(modulo))
5518 irg = get_irn_irg(n);
5519 block = get_nodes_block(n);
5520 right = get_binop_right(n);
5521 mode_right = get_irn_mode(right);
5522 if (is_Const(right)) {
5523 ir_tarval *tv = get_Const_tarval(right);
5524 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5529 newop = new_r_Const(irg, tv_mod);
5530 } else if (is_Add(right) || is_Or_Eor_Add(right)) {
5531 ir_node *add_right = get_binop_right(right);
5532 if (is_Const(add_right)) {
5533 ir_tarval *tv = get_Const_tarval(add_right);
5534 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5539 newconst = new_r_Const(irg, tv_mod);
5540 newop = new_r_Add(block, get_binop_left(right), newconst,
5543 } else if (is_Sub(right)) {
5544 ir_node *sub_left = get_Sub_left(right);
5545 if (is_Const(sub_left)) {
5546 ir_tarval *tv = get_Const_tarval(sub_left);
5547 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5552 newconst = new_r_Const(irg, tv_mod);
5553 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5560 if (newop != NULL) {
5561 dbg_info *dbgi = get_irn_dbg_info(n);
5562 ir_node *left = get_binop_left(n);
5563 return new_shift(dbgi, block, left, newop, mode);
5571 static ir_node *transform_node_Shr(ir_node *n)
5573 ir_node *c, *oldn = n;
5574 ir_node *left = get_Shr_left(n);
5575 ir_node *right = get_Shr_right(n);
5576 ir_mode *mode = get_irn_mode(n);
5578 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5579 n = transform_node_shift(n);
5582 n = transform_node_shift_modulo(n, new_rd_Shr);
5584 n = transform_node_shl_shr(n);
5586 n = transform_node_shift_bitop(n);
5594 static ir_node *transform_node_Shrs(ir_node *n)
5597 ir_node *a = get_Shrs_left(n);
5598 ir_node *b = get_Shrs_right(n);
5599 ir_mode *mode = get_irn_mode(n);
5603 if (is_oversize_shift(n)) {
5604 ir_node *block = get_nodes_block(n);
5605 dbg_info *dbgi = get_irn_dbg_info(n);
5606 ir_mode *cmode = get_irn_mode(b);
5607 long val = get_mode_size_bits(cmode)-1;
5608 ir_graph *irg = get_irn_irg(n);
5609 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5610 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5613 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5614 n = transform_node_shift(n);
5618 n = transform_node_shift_modulo(n, new_rd_Shrs);
5621 n = transform_node_shift_bitop(n);
5625 /* normalisation: use Shr when sign bit is guaranteed to be cleared */
5626 attr = vrp_get_info(a);
5628 unsigned bits = get_mode_size_bits(mode);
5629 ir_tarval *scount = new_tarval_from_long(bits-1, mode_Iu);
5630 ir_tarval *sign = tarval_shl(get_mode_one(mode), scount);
5631 if (tarval_is_null(tarval_and(attr->bits_not_set, sign))) {
5632 dbg_info *dbgi = get_irn_dbg_info(n);
5633 ir_node *block = get_nodes_block(n);
5634 return new_rd_Shr(dbgi, block, a, b, mode);
5644 static ir_node *transform_node_Shl(ir_node *n)
5646 ir_node *c, *oldn = n;
5647 ir_node *a = get_Shl_left(n);
5648 ir_node *b = get_Shl_right(n);
5649 ir_mode *mode = get_irn_mode(n);
5651 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5652 n = transform_node_shift(n);
5655 n = transform_node_shift_modulo(n, new_rd_Shl);
5657 n = transform_node_shl_shr(n);
5659 n = transform_node_shift_bitop(n);
5667 static ir_node *transform_node_Rotl(ir_node *n)
5669 ir_node *c, *oldn = n;
5670 ir_node *a = get_Rotl_left(n);
5671 ir_node *b = get_Rotl_right(n);
5672 ir_mode *mode = get_irn_mode(n);
5674 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5675 n = transform_node_shift(n);
5678 n = transform_node_shift_bitop(n);
5686 static ir_node *transform_node_Conv(ir_node *n)
5688 ir_node *c, *oldn = n;
5689 ir_mode *mode = get_irn_mode(n);
5690 ir_node *a = get_Conv_op(n);
5692 if (mode != mode_b && is_const_Phi(a)) {
5693 /* Do NOT optimize mode_b Conv's, this leads to remaining
5694 * Phib nodes later, because the conv_b_lower operation
5695 * is instantly reverted, when it tries to insert a Convb.
5697 c = apply_conv_on_phi(a, mode);
5699 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5704 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5705 ir_graph *irg = get_irn_irg(n);
5706 return new_r_Unknown(irg, mode);
5709 if (mode_is_reference(mode) &&
5710 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5712 ir_node *l = get_Add_left(a);
5713 ir_node *r = get_Add_right(a);
5714 dbg_info *dbgi = get_irn_dbg_info(a);
5715 ir_node *block = get_nodes_block(n);
5717 ir_node *lop = get_Conv_op(l);
5718 if (get_irn_mode(lop) == mode) {
5719 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5720 n = new_rd_Add(dbgi, block, lop, r, mode);
5725 ir_node *rop = get_Conv_op(r);
5726 if (get_irn_mode(rop) == mode) {
5727 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5728 n = new_rd_Add(dbgi, block, l, rop, mode);
5738 * Remove dead blocks and nodes in dead blocks
5739 * in keep alive list. We do not generate a new End node.
5741 static ir_node *transform_node_End(ir_node *n)
5743 int i, j, n_keepalives = get_End_n_keepalives(n);
5746 NEW_ARR_A(ir_node *, in, n_keepalives);
5748 for (i = j = 0; i < n_keepalives; ++i) {
5749 ir_node *ka = get_End_keepalive(n, i);
5751 /* no need to keep Bad */
5754 /* do not keep unreachable code */
5755 block = is_Block(ka) ? ka : get_nodes_block(ka);
5756 if (is_block_unreachable(block))
5760 if (j != n_keepalives)
5761 set_End_keepalives(n, j, in);
5765 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5767 if (is_Minus(a) && get_Minus_op(a) == b)
5769 if (is_Minus(b) && get_Minus_op(b) == a)
5771 if (is_Sub(a) && is_Sub(b)) {
5772 ir_node *a_left = get_Sub_left(a);
5773 ir_node *a_right = get_Sub_right(a);
5774 ir_node *b_left = get_Sub_left(b);
5775 ir_node *b_right = get_Sub_right(b);
5777 if (a_left == b_right && a_right == b_left)
5784 static const ir_node *skip_upconv(const ir_node *node)
5786 while (is_Conv(node)) {
5787 ir_mode *mode = get_irn_mode(node);
5788 const ir_node *op = get_Conv_op(node);
5789 ir_mode *op_mode = get_irn_mode(op);
5790 if (!smaller_mode(op_mode, mode))
5797 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5798 const ir_node *mux_true)
5803 ir_relation relation;
5809 * Note further that these optimization work even for floating point
5810 * with NaN's because -NaN == NaN.
5811 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5814 mode = get_irn_mode(mux_true);
5815 if (mode_honor_signed_zeros(mode))
5818 /* must be <, <=, >=, > */
5819 relation = get_Cmp_relation(sel);
5820 if ((relation & ir_relation_less_greater) == 0)
5823 if (!ir_is_negated_value(mux_true, mux_false))
5826 mux_true = skip_upconv(mux_true);
5827 mux_false = skip_upconv(mux_false);
5829 /* must be x cmp 0 */
5830 cmp_right = get_Cmp_right(sel);
5831 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5834 cmp_left = get_Cmp_left(sel);
5835 if (cmp_left == mux_false) {
5836 if (relation & ir_relation_less) {
5839 assert(relation & ir_relation_greater);
5842 } else if (cmp_left == mux_true) {
5843 if (relation & ir_relation_less) {
5846 assert(relation & ir_relation_greater);
5854 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5857 ir_node *cmp_left = get_Cmp_left(sel);
5858 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5861 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5862 const ir_node *mux_true)
5864 /* this code should return true each time transform_node_Mux would
5865 * optimize the Mux completely away */
5867 ir_mode *mode = get_irn_mode(mux_false);
5868 if (get_mode_arithmetic(mode) == irma_twos_complement
5869 && ir_mux_is_abs(sel, mux_false, mux_true))
5872 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5873 const ir_node *cmp_r = get_Cmp_right(sel);
5874 const ir_node *cmp_l = get_Cmp_left(sel);
5875 const ir_node *f = mux_false;
5876 const ir_node *t = mux_true;
5878 if (is_Const(t) && is_Const_null(t)) {
5883 if (is_And(cmp_l) && f == cmp_r) {
5884 ir_node *and_r = get_And_right(cmp_l);
5887 if (and_r == t && is_single_bit(and_r))
5889 and_l = get_And_left(cmp_l);
5890 if (and_l == t && is_single_bit(and_l))
5899 * Optimize a Mux(c, 0, 1) node (sometimes called a "set" instruction)
5901 static ir_node *transform_Mux_set(ir_node *n)
5903 ir_node *cond = get_Mux_sel(n);
5908 ir_relation relation;
5922 left = get_Cmp_left(cond);
5923 mode = get_irn_mode(left);
5924 if (!mode_is_int(mode) && !mode_is_reference(mode))
5926 dest_mode = get_irn_mode(n);
5927 if (!mode_is_int(dest_mode) && !mode_is_reference(dest_mode))
5929 right = get_Cmp_right(cond);
5930 relation = get_Cmp_relation(cond) & ~ir_relation_unordered;
5931 if (get_mode_size_bits(mode) >= get_mode_size_bits(dest_mode)
5932 && !(mode_is_signed(mode) && is_Const(right) && is_Const_null(right)
5933 && relation != ir_relation_greater))
5938 case ir_relation_less:
5939 /* a < b -> (a - b) >> 31 */
5943 case ir_relation_less_equal:
5944 /* a <= b -> ~(a - b) >> 31 */
5949 case ir_relation_greater:
5950 /* a > b -> (b - a) >> 31 */
5954 case ir_relation_greater_equal:
5955 /* a >= b -> ~(a - b) >> 31 */
5964 dbgi = get_irn_dbg_info(n);
5965 block = get_nodes_block(n);
5966 irg = get_irn_irg(block);
5967 bits = get_mode_size_bits(dest_mode);
5968 tv = new_tarval_from_long(bits-1, mode_Iu);
5969 shift_cnt = new_rd_Const(dbgi, irg, tv);
5971 if (mode != dest_mode) {
5972 a = new_rd_Conv(dbgi, block, a, dest_mode);
5973 b = new_rd_Conv(dbgi, block, b, dest_mode);
5976 res = new_rd_Sub(dbgi, block, a, b, dest_mode);
5978 res = new_rd_Not(dbgi, block, res, dest_mode);
5980 res = new_rd_Shr(dbgi, block, res, shift_cnt, dest_mode);
5985 * Optimize a Mux into some simpler cases.
5987 static ir_node *transform_node_Mux(ir_node *n)
5990 ir_node *sel = get_Mux_sel(n);
5991 ir_mode *mode = get_irn_mode(n);
5992 ir_node *t = get_Mux_true(n);
5993 ir_node *f = get_Mux_false(n);
5994 ir_graph *irg = get_irn_irg(n);
5996 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5997 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5998 int abs = ir_mux_is_abs(sel, f, t);
6000 dbg_info *dbgi = get_irn_dbg_info(n);
6001 ir_node *block = get_nodes_block(n);
6002 ir_node *op = ir_get_abs_op(sel, f, t);
6003 int bits = get_mode_size_bits(mode);
6004 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
6005 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
6006 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
6009 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
6011 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
6017 /* first normalization step: try to move a constant to the false side,
6018 * 0 preferred on false side too */
6019 if (is_Cmp(sel) && is_Const(t) &&
6020 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
6021 dbg_info *seldbgi = get_irn_dbg_info(sel);
6022 ir_node *block = get_nodes_block(sel);
6023 ir_relation relation = get_Cmp_relation(sel);
6028 /* Mux(x, a, b) => Mux(not(x), b, a) */
6029 relation = get_negated_relation(relation);
6030 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
6031 get_Cmp_right(sel), relation);
6032 return new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
6035 if (is_Const(f) && is_Const_null(f) && is_Const(t) && is_Const_one(t)) {
6036 n = transform_Mux_set(n);
6041 /* the following optimisations create new mode_b nodes, so only do them
6042 * before mode_b lowering */
6043 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
6045 ir_node* block = get_nodes_block(n);
6047 ir_node* c1 = get_Mux_sel(t);
6048 ir_node* t1 = get_Mux_true(t);
6049 ir_node* f1 = get_Mux_false(t);
6051 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
6052 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
6053 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6054 return new_r_Mux(block, and_, f1, t1, mode);
6055 } else if (f == t1) {
6056 /* Mux(cond0, Mux(cond1, x, y), x) */
6057 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6058 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
6059 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6060 return new_r_Mux(block, and_, t1, f1, mode);
6062 } else if (is_Mux(f)) {
6063 ir_node* block = get_nodes_block(n);
6065 ir_node* c1 = get_Mux_sel(f);
6066 ir_node* t1 = get_Mux_true(f);
6067 ir_node* f1 = get_Mux_false(f);
6069 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
6070 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
6071 DBG_OPT_ALGSIM0(oldn, f1, FS_OPT_MUX_COMBINE);
6072 return new_r_Mux(block, or_, f1, t1, mode);
6073 } else if (t == f1) {
6074 /* Mux(cond0, x, Mux(cond1, y, x)) */
6075 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
6076 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
6077 DBG_OPT_ALGSIM0(oldn, t1, FS_OPT_MUX_COMBINE);
6078 return new_r_Mux(block, or_, t1, f1, mode);
6082 /* note: after normalization, false can only happen on default */
6083 if (mode == mode_b) {
6084 dbg_info *dbg = get_irn_dbg_info(n);
6085 ir_node *block = get_nodes_block(n);
6088 ir_tarval *tv_t = get_Const_tarval(t);
6089 if (tv_t == tarval_b_true) {
6091 /* Muxb(sel, true, false) = sel */
6092 assert(get_Const_tarval(f) == tarval_b_false);
6093 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
6096 /* Muxb(sel, true, x) = Or(sel, x) */
6097 n = new_rd_Or(dbg, block, sel, f, mode_b);
6098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
6102 } else if (is_Const(f)) {
6103 ir_tarval *tv_f = get_Const_tarval(f);
6104 if (tv_f == tarval_b_true) {
6105 /* Muxb(sel, x, true) = Or(Not(sel), x) */
6106 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
6107 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
6108 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
6111 /* Muxb(sel, x, false) = And(sel, x) */
6112 assert(tv_f == tarval_b_false);
6113 n = new_rd_And(dbg, block, sel, t, mode_b);
6114 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
6121 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
6122 ir_relation relation = get_Cmp_relation(sel);
6123 ir_node *cmp_r = get_Cmp_right(sel);
6124 ir_node *cmp_l = get_Cmp_left(sel);
6125 ir_node *block = get_nodes_block(n);
6127 if (is_And(cmp_l) && f == cmp_r) {
6128 ir_node *and_r = get_And_right(cmp_l);
6131 if (and_r == t && is_single_bit(and_r)) {
6132 if (relation == ir_relation_equal) {
6133 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
6134 n = new_rd_Eor(get_irn_dbg_info(n),
6135 block, cmp_l, t, mode);
6136 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6138 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
6140 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6144 and_l = get_And_left(cmp_l);
6145 if (and_l == t && is_single_bit(and_l)) {
6146 if (relation == ir_relation_equal) {
6147 /* ((1 << n) & a) == 0, (1 << n), 0) */
6148 n = new_rd_Eor(get_irn_dbg_info(n),
6149 block, cmp_l, t, mode);
6150 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6152 /* ((1 << n) & a) != 0, (1 << n), 0) */
6154 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
6165 * optimize Sync nodes that have other syncs as input we simply add the inputs
6166 * of the other sync to our own inputs
6168 static ir_node *transform_node_Sync(ir_node *n)
6170 int arity = get_Sync_n_preds(n);
6173 for (i = 0; i < arity;) {
6174 ir_node *pred = get_Sync_pred(n, i);
6178 /* Remove Bad predecessors */
6185 /* Remove duplicate predecessors */
6186 for (j = 0; j < i; ++j) {
6187 if (get_Sync_pred(n, j) == pred) {
6196 if (!is_Sync(pred)) {
6204 pred_arity = get_Sync_n_preds(pred);
6205 for (j = 0; j < pred_arity; ++j) {
6206 ir_node *pred_pred = get_Sync_pred(pred, j);
6211 add_irn_n(n, pred_pred);
6215 if (get_Sync_pred(n, k) == pred_pred) break;
6221 ir_graph *irg = get_irn_irg(n);
6222 return new_r_Bad(irg, mode_M);
6225 return get_Sync_pred(n, 0);
6228 /* rehash the sync node */
6233 static ir_node *transform_node_Load(ir_node *n)
6235 /* if our memory predecessor is a load from the same address, then reuse the
6236 * previous result */
6237 ir_node *mem = get_Load_mem(n);
6242 /* don't touch volatile loads */
6243 if (get_Load_volatility(n) == volatility_is_volatile)
6245 mem_pred = get_Proj_pred(mem);
6246 if (is_Load(mem_pred)) {
6247 ir_node *pred_load = mem_pred;
6249 /* conservatively compare the 2 loads. TODO: This could be less strict
6250 * with fixup code in some situations (like smaller/bigger modes) */
6251 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6253 if (get_Load_mode(pred_load) != get_Load_mode(n))
6255 /* all combinations of aligned/unaligned pred/n should be fine so we do
6256 * not compare the unaligned attribute */
6258 ir_node *block = get_nodes_block(n);
6259 ir_node *jmp = new_r_Jmp(block);
6260 ir_graph *irg = get_irn_irg(n);
6261 ir_node *bad = new_r_Bad(irg, mode_X);
6262 ir_mode *mode = get_Load_mode(n);
6263 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6264 ir_node *in[] = { mem, res, jmp, bad };
6265 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6268 } else if (is_Store(mem_pred)) {
6269 ir_node *pred_store = mem_pred;
6270 ir_node *value = get_Store_value(pred_store);
6272 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6274 if (get_irn_mode(value) != get_Load_mode(n))
6276 /* all combinations of aligned/unaligned pred/n should be fine so we do
6277 * not compare the unaligned attribute */
6279 ir_node *block = get_nodes_block(n);
6280 ir_node *jmp = new_r_Jmp(block);
6281 ir_graph *irg = get_irn_irg(n);
6282 ir_node *bad = new_r_Bad(irg, mode_X);
6283 ir_node *res = value;
6284 ir_node *in[] = { mem, res, jmp, bad };
6285 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6294 * optimize a trampoline Call into a direct Call
6296 static ir_node *transform_node_Call(ir_node *call)
6298 ir_node *callee = get_Call_ptr(call);
6299 ir_node *adr, *mem, *res, *bl, **in;
6300 ir_type *ctp, *mtp, *tp;
6304 size_t i, n_res, n_param;
6307 if (! is_Proj(callee))
6309 callee = get_Proj_pred(callee);
6310 if (! is_Builtin(callee))
6312 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6315 mem = get_Call_mem(call);
6317 if (skip_Proj(mem) == callee) {
6318 /* memory is routed to the trampoline, skip */
6319 mem = get_Builtin_mem(callee);
6322 /* build a new call type */
6323 mtp = get_Call_type(call);
6324 tdb = get_type_dbg_info(mtp);
6326 n_res = get_method_n_ress(mtp);
6327 n_param = get_method_n_params(mtp);
6328 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6330 for (i = 0; i < n_res; ++i)
6331 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6333 NEW_ARR_A(ir_node *, in, n_param + 1);
6335 /* FIXME: we don't need a new pointer type in every step */
6336 irg = get_irn_irg(call);
6337 tp = get_irg_frame_type(irg);
6338 tp = new_type_pointer(tp);
6339 set_method_param_type(ctp, 0, tp);
6341 in[0] = get_Builtin_param(callee, 2);
6342 for (i = 0; i < n_param; ++i) {
6343 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6344 in[i + 1] = get_Call_param(call, i);
6346 var = get_method_variadicity(mtp);
6347 set_method_variadicity(ctp, var);
6348 /* When we resolve a trampoline, the function must be called by a this-call */
6349 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6350 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6352 adr = get_Builtin_param(callee, 1);
6354 db = get_irn_dbg_info(call);
6355 bl = get_nodes_block(call);
6357 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6358 if (get_irn_pinned(call) == op_pin_state_floats)
6359 set_irn_pinned(res, op_pin_state_floats);
6364 * Sets the default transform node operation for an ir_op_ops.
6366 * @param code the opcode for the default operation
6367 * @param ops the operations initialized
6372 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6376 ops->transform_node = transform_node_##a; \
6378 #define CASE_PROJ(a) \
6380 ops->transform_node_Proj = transform_node_Proj_##a; \
6382 #define CASE_PROJ_EX(a) \
6384 ops->transform_node = transform_node_##a; \
6385 ops->transform_node_Proj = transform_node_Proj_##a; \
6430 * Tries several [inplace] [optimizing] transformations and returns an
6431 * equivalent node. The difference to equivalent_node() is that these
6432 * transformations _do_ generate new nodes, and thus the old node must
6433 * not be freed even if the equivalent node isn't the old one.
6435 static ir_node *transform_node(ir_node *n)
6441 iro = get_irn_opcode_(n);
6442 /* constant expression evaluation / constant folding */
6443 if (get_opt_constant_folding()) {
6444 /* neither constants nor Tuple values can be evaluated */
6445 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6446 /* try to evaluate */
6447 ir_tarval *tv = computed_value(n);
6448 if (tv != tarval_bad) {
6449 /* evaluation was successful -- replace the node. */
6450 ir_graph *irg = get_irn_irg(n);
6452 n = new_r_Const(irg, tv);
6454 DBG_OPT_CSTEVAL(old_n, n);
6460 /* remove unnecessary nodes */
6461 if (get_opt_constant_folding() ||
6462 (iro == iro_Phi) || /* always optimize these nodes. */
6463 (iro == iro_Id) || /* ... */
6464 (iro == iro_Proj) || /* ... */
6465 (iro == iro_Block)) { /* Flags tested local. */
6466 n = equivalent_node(n);
6471 /* Some more constant expression evaluation. */
6472 if (get_opt_algebraic_simplification() ||
6473 (iro == iro_Cond) ||
6474 (iro == iro_Proj)) { /* Flags tested local. */
6475 if (n->op->ops.transform_node != NULL) {
6476 n = n->op->ops.transform_node(n);
6486 /* **************** Common Subexpression Elimination **************** */
6488 /** The size of the hash table used, should estimate the number of nodes
6490 #define N_IR_NODES 512
6492 /** Compares two exception attributes */
6493 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6495 const except_attr *ea = &a->attr.except;
6496 const except_attr *eb = &b->attr.except;
6497 return ea->pin_state != eb->pin_state;
6500 /** Compares the attributes of two Const nodes. */
6501 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6503 return get_Const_tarval(a) != get_Const_tarval(b);
6506 /** Compares the attributes of two Proj nodes. */
6507 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6509 return a->attr.proj.proj != b->attr.proj.proj;
6512 /** Compares the attributes of two Alloc nodes. */
6513 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6515 const alloc_attr *pa = &a->attr.alloc;
6516 const alloc_attr *pb = &b->attr.alloc;
6517 if (pa->where != pb->where || pa->type != pb->type)
6519 return node_cmp_exception(a, b);
6522 /** Compares the attributes of two Free nodes. */
6523 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6525 const free_attr *pa = &a->attr.free;
6526 const free_attr *pb = &b->attr.free;
6527 return (pa->where != pb->where) || (pa->type != pb->type);
6530 /** Compares the attributes of two SymConst nodes. */
6531 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6533 const symconst_attr *pa = &a->attr.symc;
6534 const symconst_attr *pb = &b->attr.symc;
6535 return (pa->kind != pb->kind)
6536 || (pa->sym.type_p != pb->sym.type_p);
6539 /** Compares the attributes of two Call nodes. */
6540 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6542 const call_attr *pa = &a->attr.call;
6543 const call_attr *pb = &b->attr.call;
6544 if (pa->type != pb->type)
6546 return node_cmp_exception(a, b);
6549 /** Compares the attributes of two Sel nodes. */
6550 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6552 const ir_entity *a_ent = get_Sel_entity(a);
6553 const ir_entity *b_ent = get_Sel_entity(b);
6554 return a_ent != b_ent;
6557 /** Compares the attributes of two Phi nodes. */
6558 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6560 /* we can only enter this function if both nodes have the same number of inputs,
6561 hence it is enough to check if one of them is a Phi0 */
6563 /* check the Phi0 pos attribute */
6564 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6569 /** Compares the attributes of two Conv nodes. */
6570 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6572 return get_Conv_strict(a) != get_Conv_strict(b);
6575 /** Compares the attributes of two Cast nodes. */
6576 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6578 return get_Cast_type(a) != get_Cast_type(b);
6581 /** Compares the attributes of two Load nodes. */
6582 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6584 if (get_Load_volatility(a) == volatility_is_volatile ||
6585 get_Load_volatility(b) == volatility_is_volatile)
6586 /* NEVER do CSE on volatile Loads */
6588 /* do not CSE Loads with different alignment. Be conservative. */
6589 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6591 if (get_Load_mode(a) != get_Load_mode(b))
6593 return node_cmp_exception(a, b);
6596 /** Compares the attributes of two Store nodes. */
6597 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6599 /* do not CSE Stores with different alignment. Be conservative. */
6600 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6602 /* NEVER do CSE on volatile Stores */
6603 if (get_Store_volatility(a) == volatility_is_volatile ||
6604 get_Store_volatility(b) == volatility_is_volatile)
6606 return node_cmp_exception(a, b);
6609 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6611 if (get_CopyB_type(a) != get_CopyB_type(b))
6614 return node_cmp_exception(a, b);
6617 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6619 return node_cmp_exception(a, b);
6622 /** Compares the attributes of two Div nodes. */
6623 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6625 const div_attr *ma = &a->attr.div;
6626 const div_attr *mb = &b->attr.div;
6627 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6629 return node_cmp_exception(a, b);
6632 /** Compares the attributes of two Mod nodes. */
6633 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6635 const mod_attr *ma = &a->attr.mod;
6636 const mod_attr *mb = &b->attr.mod;
6637 if (ma->resmode != mb->resmode)
6639 return node_cmp_exception(a, b);
6642 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6644 const cmp_attr *ma = &a->attr.cmp;
6645 const cmp_attr *mb = &b->attr.cmp;
6646 return ma->relation != mb->relation;
6649 /** Compares the attributes of two Confirm nodes. */
6650 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6652 const confirm_attr *ma = &a->attr.confirm;
6653 const confirm_attr *mb = &b->attr.confirm;
6654 return ma->relation != mb->relation;
6657 /** Compares the attributes of two Builtin nodes. */
6658 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6660 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6662 if (get_Builtin_type(a) != get_Builtin_type(b))
6664 return node_cmp_exception(a, b);
6667 /** Compares the attributes of two ASM nodes. */
6668 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6671 const ir_asm_constraint *ca;
6672 const ir_asm_constraint *cb;
6675 if (get_ASM_text(a) != get_ASM_text(b))
6678 /* Should we really check the constraints here? Should be better, but is strange. */
6679 n = get_ASM_n_input_constraints(a);
6680 if (n != get_ASM_n_input_constraints(b))
6683 ca = get_ASM_input_constraints(a);
6684 cb = get_ASM_input_constraints(b);
6685 for (i = 0; i < n; ++i) {
6686 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6687 || ca[i].mode != cb[i].mode)
6691 n = get_ASM_n_output_constraints(a);
6692 if (n != get_ASM_n_output_constraints(b))
6695 ca = get_ASM_output_constraints(a);
6696 cb = get_ASM_output_constraints(b);
6697 for (i = 0; i < n; ++i) {
6698 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6699 || ca[i].mode != cb[i].mode)
6703 n = get_ASM_n_clobbers(a);
6704 if (n != get_ASM_n_clobbers(b))
6707 cla = get_ASM_clobbers(a);
6708 clb = get_ASM_clobbers(b);
6709 for (i = 0; i < n; ++i) {
6710 if (cla[i] != clb[i])
6714 return node_cmp_exception(a, b);
6717 /** Compares the inexistent attributes of two Dummy nodes. */
6718 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6722 /* Dummy nodes never equal by definition */
6726 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6728 if (get_InstOf_type(a) != get_InstOf_type(b))
6730 return node_cmp_exception(a, b);
6734 * Set the default node attribute compare operation for an ir_op_ops.
6736 * @param code the opcode for the default operation
6737 * @param ops the operations initialized
6742 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6746 ops->node_cmp_attr = node_cmp_attr_##a; \
6782 * Compare function for two nodes in the value table. Gets two
6783 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6785 int identities_cmp(const void *elt, const void *key)
6787 ir_node *a = (ir_node *)elt;
6788 ir_node *b = (ir_node *)key;
6791 if (a == b) return 0;
6793 if ((get_irn_op(a) != get_irn_op(b)) ||
6794 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6796 /* compare if a's in and b's in are of equal length */
6797 irn_arity_a = get_irn_arity(a);
6798 if (irn_arity_a != get_irn_arity(b))
6801 /* blocks are never the same */
6805 if (get_irn_pinned(a) == op_pin_state_pinned) {
6806 /* for pinned nodes, the block inputs must be equal */
6807 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6810 ir_node *block_a = get_nodes_block(a);
6811 ir_node *block_b = get_nodes_block(b);
6812 if (! get_opt_global_cse()) {
6813 /* for block-local CSE both nodes must be in the same Block */
6814 if (block_a != block_b)
6817 /* The optimistic approach would be to do nothing here.
6818 * However doing GCSE optimistically produces a lot of partially dead code which appears
6819 * to be worse in practice than the missed opportunities.
6820 * So we use a very conservative variant here and only CSE if 1 value dominates the
6822 if (!block_dominates(block_a, block_b)
6823 && !block_dominates(block_b, block_a))
6828 /* compare a->in[0..ins] with b->in[0..ins] */
6829 for (i = 0; i < irn_arity_a; ++i) {
6830 ir_node *pred_a = get_irn_n(a, i);
6831 ir_node *pred_b = get_irn_n(b, i);
6832 if (pred_a != pred_b) {
6833 /* if both predecessors are CSE neutral they might be different */
6834 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6840 * here, we already now that the nodes are identical except their
6843 if (a->op->ops.node_cmp_attr)
6844 return a->op->ops.node_cmp_attr(a, b);
6850 * Calculate a hash value of a node.
6852 * @param node The IR-node
6854 unsigned ir_node_hash(const ir_node *node)
6856 return node->op->ops.hash(node);
6860 void new_identities(ir_graph *irg)
6862 if (irg->value_table != NULL)
6863 del_pset(irg->value_table);
6864 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6867 void del_identities(ir_graph *irg)
6869 if (irg->value_table != NULL)
6870 del_pset(irg->value_table);
6873 /* Normalize a node by putting constants (and operands with larger
6874 * node index) on the right (operator side). */
6875 void ir_normalize_node(ir_node *n)
6877 if (is_op_commutative(get_irn_op(n))) {
6878 ir_node *l = get_binop_left(n);
6879 ir_node *r = get_binop_right(n);
6881 /* For commutative operators perform a OP b == b OP a but keep
6882 * constants on the RIGHT side. This helps greatly in some
6883 * optimizations. Moreover we use the idx number to make the form
6885 if (!operands_are_normalized(l, r)) {
6886 set_binop_left(n, r);
6887 set_binop_right(n, l);
6894 * Return the canonical node computing the same value as n.
6895 * Looks up the node in a hash table, enters it in the table
6896 * if it isn't there yet.
6898 * @param n the node to look up
6900 * @return a node that computes the same value as n or n if no such
6901 * node could be found
6903 ir_node *identify_remember(ir_node *n)
6905 ir_graph *irg = get_irn_irg(n);
6906 pset *value_table = irg->value_table;
6909 if (value_table == NULL)
6912 ir_normalize_node(n);
6913 /* lookup or insert in hash table with given hash key. */
6914 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6917 /* n is reachable again */
6918 edges_node_revival(nn);
6925 * During construction we set the op_pin_state_pinned flag in the graph right
6926 * when the optimization is performed. The flag turning on procedure global
6927 * cse could be changed between two allocations. This way we are safe.
6929 * @param n The node to lookup
6931 static inline ir_node *identify_cons(ir_node *n)
6935 n = identify_remember(n);
6936 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6937 ir_graph *irg = get_irn_irg(n);
6938 set_irg_pinned(irg, op_pin_state_floats);
6943 /* Add a node to the identities value table. */
6944 void add_identities(ir_node *node)
6951 identify_remember(node);
6954 /* Visit each node in the value table of a graph. */
6955 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6958 ir_graph *rem = current_ir_graph;
6960 current_ir_graph = irg;
6961 foreach_pset(irg->value_table, ir_node*, node) {
6964 current_ir_graph = rem;
6968 * These optimizations deallocate nodes from the obstack.
6969 * It can only be called if it is guaranteed that no other nodes
6970 * reference this one, i.e., right after construction of a node.
6972 * @param n The node to optimize
6974 ir_node *optimize_node(ir_node *n)
6977 ir_graph *irg = get_irn_irg(n);
6978 unsigned iro = get_irn_opcode(n);
6981 /* Always optimize Phi nodes: part of the construction. */
6982 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6984 /* constant expression evaluation / constant folding */
6985 if (get_opt_constant_folding()) {
6986 /* neither constants nor Tuple values can be evaluated */
6987 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6988 /* try to evaluate */
6989 tv = computed_value(n);
6990 if (tv != tarval_bad) {
6995 * we MUST copy the node here temporarily, because it's still
6996 * needed for DBG_OPT_CSTEVAL
6998 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6999 oldn = (ir_node*)alloca(node_size);
7001 memcpy(oldn, n, node_size);
7002 CLONE_ARR_A(ir_node *, oldn->in, n->in);
7004 /* ARG, copy the in array, we need it for statistics */
7005 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
7007 /* note the inplace edges module */
7008 edges_node_deleted(n);
7010 /* evaluation was successful -- replace the node. */
7011 irg_kill_node(irg, n);
7012 nw = new_r_Const(irg, tv);
7014 DBG_OPT_CSTEVAL(oldn, nw);
7020 /* remove unnecessary nodes */
7021 if (get_opt_algebraic_simplification() ||
7022 (iro == iro_Phi) || /* always optimize these nodes. */
7024 (iro == iro_Proj) ||
7025 (iro == iro_Block) ) /* Flags tested local. */
7026 n = equivalent_node(n);
7028 /* Common Subexpression Elimination.
7030 * Checks whether n is already available.
7031 * The block input is used to distinguish different subexpressions. Right
7032 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
7033 * subexpressions within a block.
7036 n = identify_cons(n);
7039 edges_node_deleted(oldn);
7041 /* We found an existing, better node, so we can deallocate the old node. */
7042 irg_kill_node(irg, oldn);
7046 /* Some more constant expression evaluation that does not allow to
7048 iro = get_irn_opcode(n);
7049 if (get_opt_algebraic_simplification() ||
7050 (iro == iro_Cond) ||
7051 (iro == iro_Proj)) { /* Flags tested local. */
7052 n = transform_node(n);
7055 /* Now we have a legal, useful node. Enter it in hash table for CSE */
7056 if (get_opt_cse()) {
7058 n = identify_remember(o);
7068 * These optimizations never deallocate nodes (in place). This can cause dead
7069 * nodes lying on the obstack. Remove these by a dead node elimination,
7070 * i.e., a copying garbage collection.
7072 ir_node *optimize_in_place_2(ir_node *n)
7074 if (!get_opt_optimize() && !is_Phi(n)) return n;
7079 /** common subexpression elimination **/
7080 /* Checks whether n is already available. */
7081 /* The block input is used to distinguish different subexpressions.
7082 * Right now all nodes are op_pin_state_pinned to blocks, i.e., the cse
7083 * only finds common subexpressions within a block. */
7084 if (get_opt_cse()) {
7086 n = identify_remember(n);
7089 /* we have another existing node now, we do not optimize it here */
7094 n = transform_node(n);
7096 /* Now we can verify the node, as it has no dead inputs any more. */
7099 /* Now we have a legal, useful node. Enter it in hash table for cse.
7100 * Blocks should be unique anyways. (Except the successor of start:
7101 * is cse with the start block!)
7103 * Note: This is only necessary because some of the optimisations
7104 * operate in-place (set_XXX_bla, turn_into_tuple, ...) which is considered
7105 * bad practice and should be fixed sometime.
7107 if (get_opt_cse()) {
7109 n = identify_remember(o);
7118 * Wrapper for external use, set proper status bits after optimization.
7120 ir_node *optimize_in_place(ir_node *n)
7122 ir_graph *irg = get_irn_irg(n);
7123 /* Handle graph state */
7124 assert(get_irg_phase_state(irg) != phase_building);
7126 if (get_opt_global_cse())
7127 set_irg_pinned(irg, op_pin_state_floats);
7129 /* FIXME: Maybe we could also test whether optimizing the node can
7130 change the control graph. */
7131 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
7132 return optimize_in_place_2(n);
7136 * Calculate a hash value of a Const node.
7138 static unsigned hash_Const(const ir_node *node)
7142 /* special value for const, as they only differ in their tarval. */
7143 h = HASH_PTR(node->attr.con.tarval);
7149 * Calculate a hash value of a SymConst node.
7151 static unsigned hash_SymConst(const ir_node *node)
7155 /* all others are pointers */
7156 h = HASH_PTR(node->attr.symc.sym.type_p);
7162 * Set the default hash operation in an ir_op_ops.
7164 * @param code the opcode for the default operation
7165 * @param ops the operations initialized
7170 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
7174 ops->hash = hash_##a; \
7177 /* hash function already set */
7178 if (ops->hash != NULL)
7185 /* use input/mode default hash if no function was given */
7186 ops->hash = firm_default_hash;
7194 * Sets the default operation for an ir_ops.
7196 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
7198 ops = firm_set_default_hash(code, ops);
7199 ops = firm_set_default_computed_value(code, ops);
7200 ops = firm_set_default_equivalent_node(code, ops);
7201 ops = firm_set_default_transform_node(code, ops);
7202 ops = firm_set_default_node_cmp_attr(code, ops);
7203 ops = firm_set_default_get_type_attr(code, ops);
7204 ops = firm_set_default_get_entity_attr(code, ops);