2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
39 #include "iroptimize.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
60 * Returns the tarval of a Const node or tarval_bad for all other nodes.
62 static ir_tarval *default_value_of(const ir_node *n)
65 return get_Const_tarval(n); /* might return tarval_bad */
70 value_of_func value_of_ptr = default_value_of;
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static ir_tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
87 } /* computed_value_Const */
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static ir_tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
118 } /* computed_value_SymConst */
121 * Return the value of an Add.
123 static ir_tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 ir_tarval *ta = value_of(a);
129 ir_tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 if ((is_Not(a) && get_Not_op(a) == b)
136 || (is_Not(b) && get_Not_op(b) == a)) {
137 return get_mode_all_one(get_irn_mode(n));
141 } /* computed_value_Add */
144 * Return the value of a Sub.
145 * Special case: a - a
147 static ir_tarval *computed_value_Sub(const ir_node *n)
149 ir_mode *mode = get_irn_mode(n);
150 ir_node *a = get_Sub_left(n);
151 ir_node *b = get_Sub_right(n);
156 if (! mode_is_float(mode)) {
159 return get_mode_null(mode);
165 if ((ta != tarval_bad) && (tb != tarval_bad))
166 return tarval_sub(ta, tb, mode);
169 } /* computed_value_Sub */
172 * Return the value of a Carry.
173 * Special : a op 0, 0 op b
175 static ir_tarval *computed_value_Carry(const ir_node *n)
177 ir_node *a = get_binop_left(n);
178 ir_node *b = get_binop_right(n);
179 ir_mode *m = get_irn_mode(n);
180 ir_tarval *ta = value_of(a);
181 ir_tarval *tb = value_of(b);
183 if ((ta != tarval_bad) && (tb != tarval_bad)) {
185 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
187 if (tarval_is_null(ta) || tarval_is_null(tb))
188 return get_mode_null(m);
191 } /* computed_value_Carry */
194 * Return the value of a Borrow.
197 static ir_tarval *computed_value_Borrow(const ir_node *n)
199 ir_node *a = get_binop_left(n);
200 ir_node *b = get_binop_right(n);
201 ir_mode *m = get_irn_mode(n);
202 ir_tarval *ta = value_of(a);
203 ir_tarval *tb = value_of(b);
205 if ((ta != tarval_bad) && (tb != tarval_bad)) {
206 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
207 } else if (tarval_is_null(ta)) {
208 return get_mode_null(m);
211 } /* computed_value_Borrow */
214 * Return the value of an unary Minus.
216 static ir_tarval *computed_value_Minus(const ir_node *n)
218 ir_node *a = get_Minus_op(n);
219 ir_tarval *ta = value_of(a);
221 if (ta != tarval_bad)
222 return tarval_neg(ta);
225 } /* computed_value_Minus */
228 * Return the value of a Mul.
230 static ir_tarval *computed_value_Mul(const ir_node *n)
232 ir_node *a = get_Mul_left(n);
233 ir_node *b = get_Mul_right(n);
234 ir_tarval *ta = value_of(a);
235 ir_tarval *tb = value_of(b);
238 mode = get_irn_mode(n);
239 if (mode != get_irn_mode(a)) {
240 /* n * n = 2n bit multiplication */
241 ta = tarval_convert_to(ta, mode);
242 tb = tarval_convert_to(tb, mode);
245 if (ta != tarval_bad && tb != tarval_bad) {
246 return tarval_mul(ta, tb);
248 /* a * 0 != 0 if a == NaN or a == Inf */
249 if (!mode_is_float(mode)) {
250 /* a*0 = 0 or 0*b = 0 */
251 if (ta == get_mode_null(mode))
253 if (tb == get_mode_null(mode))
258 } /* computed_value_Mul */
261 * Return the value of an And.
262 * Special case: a & 0, 0 & b
264 static ir_tarval *computed_value_And(const ir_node *n)
266 ir_node *a = get_And_left(n);
267 ir_node *b = get_And_right(n);
268 ir_tarval *ta = value_of(a);
269 ir_tarval *tb = value_of(b);
271 if ((ta != tarval_bad) && (tb != tarval_bad)) {
272 return tarval_and (ta, tb);
275 if (tarval_is_null(ta)) return ta;
276 if (tarval_is_null(tb)) return tb;
279 if ((is_Not(a) && get_Not_op(a) == b)
280 || (is_Not(b) && get_Not_op(b) == a)) {
281 return get_mode_null(get_irn_mode(n));
285 } /* computed_value_And */
288 * Return the value of an Or.
289 * Special case: a | 1...1, 1...1 | b
291 static ir_tarval *computed_value_Or(const ir_node *n)
293 ir_node *a = get_Or_left(n);
294 ir_node *b = get_Or_right(n);
295 ir_tarval *ta = value_of(a);
296 ir_tarval *tb = value_of(b);
298 if ((ta != tarval_bad) && (tb != tarval_bad)) {
299 return tarval_or (ta, tb);
302 if (tarval_is_all_one(ta)) return ta;
303 if (tarval_is_all_one(tb)) return tb;
306 if ((is_Not(a) && get_Not_op(a) == b)
307 || (is_Not(b) && get_Not_op(b) == a)) {
308 return get_mode_all_one(get_irn_mode(n));
311 } /* computed_value_Or */
314 * Return the value of an Eor.
316 static ir_tarval *computed_value_Eor(const ir_node *n)
318 ir_node *a = get_Eor_left(n);
319 ir_node *b = get_Eor_right(n);
324 return get_mode_null(get_irn_mode(n));
326 if ((is_Not(a) && get_Not_op(a) == b)
327 || (is_Not(b) && get_Not_op(b) == a)) {
328 return get_mode_all_one(get_irn_mode(n));
334 if ((ta != tarval_bad) && (tb != tarval_bad)) {
335 return tarval_eor(ta, tb);
338 } /* computed_value_Eor */
341 * Return the value of a Not.
343 static ir_tarval *computed_value_Not(const ir_node *n)
345 ir_node *a = get_Not_op(n);
346 ir_tarval *ta = value_of(a);
348 if (ta != tarval_bad)
349 return tarval_not(ta);
352 } /* computed_value_Not */
355 * Tests whether a shift shifts more bits than available in the mode
357 static bool is_oversize_shift(const ir_node *n)
359 ir_node *count = get_binop_right(n);
360 ir_mode *mode = get_irn_mode(n);
361 ir_tarval *tv = value_of(count);
364 if (tv == tarval_bad)
366 if (!tarval_is_long(tv))
368 shiftval = get_tarval_long(tv);
369 modulo_shift = get_mode_modulo_shift(mode);
370 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
373 return shiftval >= (long)get_mode_size_bits(mode);
377 * Return the value of a Shl.
379 static ir_tarval *computed_value_Shl(const ir_node *n)
381 ir_node *a = get_Shl_left(n);
382 ir_node *b = get_Shl_right(n);
384 ir_tarval *ta = value_of(a);
385 ir_tarval *tb = value_of(b);
387 if ((ta != tarval_bad) && (tb != tarval_bad)) {
388 return tarval_shl(ta, tb);
391 if (is_oversize_shift(n))
392 return get_mode_null(get_irn_mode(n));
395 } /* computed_value_Shl */
398 * Return the value of a Shr.
400 static ir_tarval *computed_value_Shr(const ir_node *n)
402 ir_node *a = get_Shr_left(n);
403 ir_node *b = get_Shr_right(n);
405 ir_tarval *ta = value_of(a);
406 ir_tarval *tb = value_of(b);
408 if ((ta != tarval_bad) && (tb != tarval_bad)) {
409 return tarval_shr(ta, tb);
411 if (is_oversize_shift(n))
412 return get_mode_null(get_irn_mode(n));
415 } /* computed_value_Shr */
418 * Return the value of a Shrs.
420 static ir_tarval *computed_value_Shrs(const ir_node *n)
422 ir_node *a = get_Shrs_left(n);
423 ir_node *b = get_Shrs_right(n);
425 ir_tarval *ta = value_of(a);
426 ir_tarval *tb = value_of(b);
428 if ((ta != tarval_bad) && (tb != tarval_bad)) {
429 return tarval_shrs(ta, tb);
432 } /* computed_value_Shrs */
435 * Return the value of a Rotl.
437 static ir_tarval *computed_value_Rotl(const ir_node *n)
439 ir_node *a = get_Rotl_left(n);
440 ir_node *b = get_Rotl_right(n);
442 ir_tarval *ta = value_of(a);
443 ir_tarval *tb = value_of(b);
445 if ((ta != tarval_bad) && (tb != tarval_bad)) {
446 return tarval_rotl(ta, tb);
449 } /* computed_value_Rotl */
451 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
453 ir_mode *mode = get_irn_mode(node);
454 if (get_mode_arithmetic(mode) != irma_twos_complement
455 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
459 ir_node *count = get_Shl_right(node);
460 if (is_Const(count)) {
461 ir_tarval *tv = get_Const_tarval(count);
462 if (tarval_is_long(tv)) {
463 long shiftval = get_tarval_long(tv);
464 long destbits = get_mode_size_bits(dest_mode);
465 if (shiftval >= destbits
466 && shiftval < (long)get_mode_modulo_shift(mode))
472 ir_node *right = get_And_right(node);
473 if (is_Const(right)) {
474 ir_tarval *tv = get_Const_tarval(right);
475 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
476 return tarval_is_null(conved);
483 * Return the value of a Conv.
485 static ir_tarval *computed_value_Conv(const ir_node *n)
487 ir_node *a = get_Conv_op(n);
488 ir_tarval *ta = value_of(a);
489 ir_mode *mode = get_irn_mode(n);
491 if (ta != tarval_bad)
492 return tarval_convert_to(ta, get_irn_mode(n));
494 if (ir_zero_when_converted(a, mode))
495 return get_mode_null(mode);
498 } /* computed_value_Conv */
501 * Calculate the value of a Mux: can be evaluated, if the
502 * sel and the right input are known.
504 static ir_tarval *computed_value_Mux(const ir_node *n)
506 ir_node *sel = get_Mux_sel(n);
507 ir_tarval *ts = value_of(sel);
509 if (ts == get_tarval_b_true()) {
510 ir_node *v = get_Mux_true(n);
513 else if (ts == get_tarval_b_false()) {
514 ir_node *v = get_Mux_false(n);
518 } /* computed_value_Mux */
521 * Calculate the value of a Confirm: can be evaluated,
522 * if it has the form Confirm(x, '=', Const).
524 static ir_tarval *computed_value_Confirm(const ir_node *n)
526 if (get_Confirm_relation(n) == ir_relation_equal) {
527 ir_tarval *tv = value_of(get_Confirm_bound(n));
528 if (tv != tarval_bad)
531 return value_of(get_Confirm_value(n));
532 } /* computed_value_Confirm */
535 * gives a (conservative) estimation of possible relation when comparing
538 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
539 const ir_node *right)
541 ir_relation possible = ir_relation_true;
542 ir_tarval *tv_l = value_of(left);
543 ir_tarval *tv_r = value_of(right);
544 ir_mode *mode = get_irn_mode(left);
545 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
546 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
548 /* both values known - evaluate them */
549 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
550 possible = tarval_cmp(tv_l, tv_r);
551 /* we can return now, won't get any better */
554 /* a == a is never less or greater (but might be equal or unordered) */
556 possible &= ~ir_relation_less_greater;
557 /* unordered results only happen for float compares */
558 if (!mode_is_float(mode))
559 possible &= ~ir_relation_unordered;
560 /* values can never be less than the least representable number or
561 * greater than the greatest representable number */
563 possible &= ~ir_relation_greater;
565 possible &= ~ir_relation_less;
567 possible &= ~ir_relation_greater;
569 possible &= ~ir_relation_less;
570 /* maybe vrp can tell us more */
571 possible &= vrp_cmp(left, right);
572 /* Alloc nodes never return null (but throw an exception) */
573 if (is_Alloc(left) && tarval_is_null(tv_r))
574 possible &= ~ir_relation_equal;
579 static ir_tarval *compute_cmp(const ir_node *cmp)
581 ir_node *left = get_Cmp_left(cmp);
582 ir_node *right = get_Cmp_right(cmp);
583 ir_relation possible = ir_get_possible_cmp_relations(left, right);
584 ir_relation relation = get_Cmp_relation(cmp);
586 /* if none of the requested relations is possible, return false */
587 if ((possible & relation) == ir_relation_false)
588 return tarval_b_false;
589 /* if possible relations are a subset of the requested ones return true */
590 if ((possible & ~relation) == ir_relation_false)
591 return tarval_b_true;
593 return computed_value_Cmp_Confirm(cmp, left, right, relation);
597 * Return the value of a Cmp.
599 * The basic idea here is to determine which relations are possible and which
600 * one are definitely impossible.
602 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
604 /* we can't construct Constb after lowering mode_b nodes */
605 if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
608 return compute_cmp(cmp);
612 * Calculate the value of an integer Div.
613 * Special case: 0 / b
615 static ir_tarval *do_computed_value_Div(const ir_node *div)
617 const ir_node *a = get_Div_left(div);
618 const ir_node *b = get_Div_right(div);
619 const ir_mode *mode = get_Div_resmode(div);
620 ir_tarval *ta = value_of(a);
622 const ir_node *dummy;
624 /* cannot optimize 0 / b = 0 because of NaN */
625 if (!mode_is_float(mode)) {
626 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
627 return ta; /* 0 / b == 0 if b != 0 */
630 if (ta != tarval_bad && tb != tarval_bad)
631 return tarval_div(ta, tb);
633 } /* do_computed_value_Div */
636 * Calculate the value of an integer Mod of two nodes.
637 * Special case: a % 1
639 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
641 ir_tarval *ta = value_of(a);
642 ir_tarval *tb = value_of(b);
644 /* Compute a % 1 or c1 % c2 */
645 if (tarval_is_one(tb))
646 return get_mode_null(get_irn_mode(a));
647 if (ta != tarval_bad && tb != tarval_bad)
648 return tarval_mod(ta, tb);
650 } /* do_computed_value_Mod */
653 * Return the value of a Proj(Div).
655 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
657 long proj_nr = get_Proj_proj(n);
658 if (proj_nr != pn_Div_res)
661 return do_computed_value_Div(get_Proj_pred(n));
662 } /* computed_value_Proj_Div */
665 * Return the value of a Proj(Mod).
667 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
669 long proj_nr = get_Proj_proj(n);
671 if (proj_nr == pn_Mod_res) {
672 const ir_node *mod = get_Proj_pred(n);
673 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
676 } /* computed_value_Proj_Mod */
679 * Return the value of a Proj.
681 static ir_tarval *computed_value_Proj(const ir_node *proj)
683 ir_node *n = get_Proj_pred(proj);
685 if (n->op->ops.computed_value_Proj != NULL)
686 return n->op->ops.computed_value_Proj(proj);
688 } /* computed_value_Proj */
691 * If the parameter n can be computed, return its value, else tarval_bad.
692 * Performs constant folding.
694 * @param n The node this should be evaluated
696 ir_tarval *computed_value(const ir_node *n)
698 vrp_attr *vrp = vrp_get_info(n);
699 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
700 return vrp->bits_set;
702 if (n->op->ops.computed_value)
703 return n->op->ops.computed_value(n);
705 } /* computed_value */
708 * Set the default computed_value evaluator in an ir_op_ops.
710 * @param code the opcode for the default operation
711 * @param ops the operations initialized
716 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
720 ops->computed_value = computed_value_##a; \
722 #define CASE_PROJ(a) \
724 ops->computed_value_Proj = computed_value_Proj_##a; \
759 } /* firm_set_default_computed_value */
762 * Optimize operations that are commutative and have neutral 0,
763 * so a op 0 = 0 op a = a.
765 static ir_node *equivalent_node_neutral_zero(ir_node *n)
769 ir_node *a = get_binop_left(n);
770 ir_node *b = get_binop_right(n);
775 /* After running compute_node there is only one constant predecessor.
776 Find this predecessors value and remember the other node: */
777 if ((tv = value_of(a)) != tarval_bad) {
779 } else if ((tv = value_of(b)) != tarval_bad) {
784 /* If this predecessors constant value is zero, the operation is
785 * unnecessary. Remove it.
787 * Beware: If n is a Add, the mode of on and n might be different
788 * which happens in this rare construction: NULL + 3.
789 * Then, a Conv would be needed which we cannot include here.
791 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
794 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
798 } /* equivalent_node_neutral_zero */
801 * Eor is commutative and has neutral 0.
803 static ir_node *equivalent_node_Eor(ir_node *n)
809 n = equivalent_node_neutral_zero(n);
810 if (n != oldn) return n;
813 b = get_Eor_right(n);
816 ir_node *aa = get_Eor_left(a);
817 ir_node *ab = get_Eor_right(a);
820 /* (a ^ b) ^ a -> b */
822 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
824 } else if (ab == b) {
825 /* (a ^ b) ^ b -> a */
827 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
832 ir_node *ba = get_Eor_left(b);
833 ir_node *bb = get_Eor_right(b);
836 /* a ^ (a ^ b) -> b */
838 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
840 } else if (bb == a) {
841 /* a ^ (b ^ a) -> b */
843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
851 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
853 * The second one looks strange, but this construct
854 * is used heavily in the LCC sources :-).
856 * Beware: The Mode of an Add may be different than the mode of its
857 * predecessors, so we could not return a predecessors in all cases.
859 static ir_node *equivalent_node_Add(ir_node *n)
862 ir_node *left, *right;
863 ir_mode *mode = get_irn_mode(n);
865 n = equivalent_node_neutral_zero(n);
869 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
870 if (mode_is_float(mode)) {
871 ir_graph *irg = get_irn_irg(n);
872 if (get_irg_fp_model(irg) & fp_strict_algebraic)
876 left = get_Add_left(n);
877 right = get_Add_right(n);
880 if (get_Sub_right(left) == right) {
883 n = get_Sub_left(left);
884 if (mode == get_irn_mode(n)) {
885 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
891 if (get_Sub_right(right) == left) {
894 n = get_Sub_left(right);
895 if (mode == get_irn_mode(n)) {
896 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
902 } /* equivalent_node_Add */
905 * optimize operations that are not commutative but have neutral 0 on left,
908 static ir_node *equivalent_node_left_zero(ir_node *n)
912 ir_node *a = get_binop_left(n);
913 ir_node *b = get_binop_right(n);
914 ir_tarval *tb = value_of(b);
916 if (tarval_is_null(tb)) {
919 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
922 } /* equivalent_node_left_zero */
924 #define equivalent_node_Shl equivalent_node_left_zero
925 #define equivalent_node_Shr equivalent_node_left_zero
926 #define equivalent_node_Shrs equivalent_node_left_zero
927 #define equivalent_node_Rotl equivalent_node_left_zero
930 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
932 * The second one looks strange, but this construct
933 * is used heavily in the LCC sources :-).
935 * Beware: The Mode of a Sub may be different than the mode of its
936 * predecessors, so we could not return a predecessors in all cases.
938 static ir_node *equivalent_node_Sub(ir_node *n)
942 ir_mode *mode = get_irn_mode(n);
945 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
946 if (mode_is_float(mode)) {
947 ir_graph *irg = get_irn_irg(n);
948 if (get_irg_fp_model(irg) & fp_strict_algebraic)
952 b = get_Sub_right(n);
955 /* Beware: modes might be different */
956 if (tarval_is_null(tb)) {
957 ir_node *a = get_Sub_left(n);
958 if (mode == get_irn_mode(a)) {
961 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
965 } /* equivalent_node_Sub */
969 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
972 * -(-a) == a, but might overflow two times.
973 * We handle it anyway here but the better way would be a
974 * flag. This would be needed for Pascal for instance.
976 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
979 ir_node *pred = get_unop_op(n);
981 /* optimize symmetric unop */
982 if (get_irn_op(pred) == get_irn_op(n)) {
983 n = get_unop_op(pred);
984 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
987 } /* equivalent_node_idempotent_unop */
989 /** Optimize Not(Not(x)) == x. */
990 #define equivalent_node_Not equivalent_node_idempotent_unop
992 /** -(-x) == x ??? Is this possible or can --x raise an
993 out of bounds exception if min =! max? */
994 #define equivalent_node_Minus equivalent_node_idempotent_unop
997 * Optimize a * 1 = 1 * a = a.
999 static ir_node *equivalent_node_Mul(ir_node *n)
1002 ir_node *a = get_Mul_left(n);
1004 /* we can handle here only the n * n = n bit cases */
1005 if (get_irn_mode(n) == get_irn_mode(a)) {
1006 ir_node *b = get_Mul_right(n);
1010 * Mul is commutative and has again an other neutral element.
1011 * Constants are place right, so check this case first.
1014 if (tarval_is_one(tv)) {
1016 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1019 if (tarval_is_one(tv)) {
1021 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1026 } /* equivalent_node_Mul */
1029 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1031 static ir_node *equivalent_node_Or(ir_node *n)
1035 ir_node *a = get_Or_left(n);
1036 ir_node *b = get_Or_right(n);
1040 n = a; /* idempotence */
1041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1044 /* constants are normalized to right, check this side first */
1046 if (tarval_is_null(tv)) {
1048 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1052 if (tarval_is_null(tv)) {
1054 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1059 } /* equivalent_node_Or */
1062 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1064 static ir_node *equivalent_node_And(ir_node *n)
1068 ir_node *a = get_And_left(n);
1069 ir_node *b = get_And_right(n);
1073 n = a; /* idempotence */
1074 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1077 /* constants are normalized to right, check this side first */
1079 if (tarval_is_all_one(tv)) {
1081 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1084 if (tv != get_tarval_bad()) {
1085 ir_mode *mode = get_irn_mode(n);
1086 if (!mode_is_signed(mode) && is_Conv(a)) {
1087 ir_node *convop = get_Conv_op(a);
1088 ir_mode *convopmode = get_irn_mode(convop);
1089 if (!mode_is_signed(convopmode)) {
1090 /* Check Conv(all_one) & Const = all_one */
1091 ir_tarval *one = get_mode_all_one(convopmode);
1092 ir_tarval *conv = tarval_convert_to(one, mode);
1093 ir_tarval *and = tarval_and(conv, tv);
1095 if (tarval_is_all_one(and)) {
1096 /* Conv(X) & Const = X */
1098 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1105 if (tarval_is_all_one(tv)) {
1107 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1111 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1119 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1122 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1127 } /* equivalent_node_And */
1130 * Try to remove useless Conv's:
1132 static ir_node *equivalent_node_Conv(ir_node *n)
1135 ir_node *a = get_Conv_op(n);
1137 ir_mode *n_mode = get_irn_mode(n);
1138 ir_mode *a_mode = get_irn_mode(a);
1141 if (n_mode == a_mode) { /* No Conv necessary */
1142 if (get_Conv_strict(n)) {
1145 /* neither Minus nor Confirm change the precision,
1146 so we can "look-through" */
1149 p = get_Minus_op(p);
1150 } else if (is_Confirm(p)) {
1151 p = get_Confirm_value(p);
1157 if (is_Conv(p) && get_Conv_strict(p)) {
1158 /* we known already, that a_mode == n_mode, and neither
1159 Minus change the mode, so the second Conv
1161 assert(get_irn_mode(p) == n_mode);
1163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1167 ir_node *pred = get_Proj_pred(p);
1168 if (is_Load(pred)) {
1169 /* Loads always return with the exact precision of n_mode */
1170 assert(get_Load_mode(pred) == n_mode);
1172 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1175 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1176 pred = get_Proj_pred(pred);
1177 if (is_Start(pred)) {
1178 /* Arguments always return with the exact precision,
1179 as strictConv's are place before Call -- if the
1180 caller was compiled with the same setting.
1181 Otherwise, the semantics is probably still right. */
1182 assert(get_irn_mode(p) == n_mode);
1184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1190 /* special case: the immediate predecessor is also a Conv */
1191 if (! get_Conv_strict(a)) {
1192 /* first one is not strict, kick it */
1194 a_mode = get_irn_mode(a);
1198 /* else both are strict conv, second is superfluous */
1200 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1205 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1208 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1209 ir_node *b = get_Conv_op(a);
1210 ir_mode *b_mode = get_irn_mode(b);
1212 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1213 /* both are strict conv */
1214 if (smaller_mode(a_mode, n_mode)) {
1215 /* both are strict, but the first is smaller, so
1216 the second cannot remove more precision, remove the
1218 set_Conv_strict(n, 0);
1221 if (n_mode == b_mode) {
1222 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1223 if (n_mode == mode_b) {
1224 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1225 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1227 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1228 if (values_in_mode(b_mode, a_mode)) {
1229 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1230 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1235 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1236 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1237 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1238 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1240 if (float_mantissa >= int_mantissa) {
1242 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1247 if (smaller_mode(b_mode, a_mode)) {
1248 if (get_Conv_strict(n))
1249 set_Conv_strict(b, 1);
1250 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1251 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1258 } /* equivalent_node_Conv */
1261 * - fold Phi-nodes, iff they have only one predecessor except
1264 static ir_node *equivalent_node_Phi(ir_node *n)
1269 ir_node *first_val = NULL; /* to shutup gcc */
1271 if (!get_opt_optimize() &&
1272 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1275 n_preds = get_Phi_n_preds(n);
1277 /* Phi of dead Region without predecessors. */
1281 /* Find first non-self-referencing input */
1282 for (i = 0; i < n_preds; ++i) {
1283 first_val = get_Phi_pred(n, i);
1284 /* not self pointer */
1285 if (first_val != n) {
1286 /* then found first value. */
1291 /* search for rest of inputs, determine if any of these
1292 are non-self-referencing */
1293 while (++i < n_preds) {
1294 ir_node *scnd_val = get_Phi_pred(n, i);
1295 if (scnd_val != n && scnd_val != first_val) {
1300 if (i >= n_preds && !is_Dummy(first_val)) {
1301 /* Fold, if no multiple distinct non-self-referencing inputs */
1303 DBG_OPT_PHI(oldn, n);
1306 } /* equivalent_node_Phi */
1309 * Optimize Proj(Tuple).
1311 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1313 ir_node *oldn = proj;
1314 ir_node *tuple = get_Proj_pred(proj);
1316 /* Remove the Tuple/Proj combination. */
1317 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1318 DBG_OPT_TUPLE(oldn, tuple, proj);
1321 } /* equivalent_node_Proj_Tuple */
1324 * Optimize a / 1 = a.
1326 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1328 ir_node *oldn = proj;
1329 ir_node *div = get_Proj_pred(proj);
1330 ir_node *b = get_Div_right(div);
1331 ir_tarval *tb = value_of(b);
1333 /* Div is not commutative. */
1334 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1335 switch (get_Proj_proj(proj)) {
1337 proj = get_Div_mem(div);
1338 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1342 proj = get_Div_left(div);
1343 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1347 /* we cannot replace the exception Proj's here, this is done in
1348 transform_node_Proj_Div() */
1353 } /* equivalent_node_Proj_Div */
1356 * Optimize CopyB(mem, x, x) into a Nop.
1358 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1360 ir_node *oldn = proj;
1361 ir_node *copyb = get_Proj_pred(proj);
1362 ir_node *a = get_CopyB_dst(copyb);
1363 ir_node *b = get_CopyB_src(copyb);
1366 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1367 switch (get_Proj_proj(proj)) {
1369 proj = get_CopyB_mem(copyb);
1370 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1375 } /* equivalent_node_Proj_CopyB */
1378 * Optimize Bounds(idx, idx, upper) into idx.
1380 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1382 ir_node *oldn = proj;
1383 ir_node *bound = get_Proj_pred(proj);
1384 ir_node *idx = get_Bound_index(bound);
1385 ir_node *pred = skip_Proj(idx);
1388 if (idx == get_Bound_lower(bound))
1390 else if (is_Bound(pred)) {
1392 * idx was Bounds checked previously, it is still valid if
1393 * lower <= pred_lower && pred_upper <= upper.
1395 ir_node *lower = get_Bound_lower(bound);
1396 ir_node *upper = get_Bound_upper(bound);
1397 if (get_Bound_lower(pred) == lower &&
1398 get_Bound_upper(pred) == upper) {
1400 * One could expect that we simply return the previous
1401 * Bound here. However, this would be wrong, as we could
1402 * add an exception Proj to a new location then.
1403 * So, we must turn in into a tuple.
1409 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1410 switch (get_Proj_proj(proj)) {
1412 DBG_OPT_EXC_REM(proj);
1413 proj = get_Bound_mem(bound);
1417 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1420 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1425 } /* equivalent_node_Proj_Bound */
1428 * Does all optimizations on nodes that must be done on its Projs
1429 * because of creating new nodes.
1431 static ir_node *equivalent_node_Proj(ir_node *proj)
1433 ir_node *n = get_Proj_pred(proj);
1434 if (n->op->ops.equivalent_node_Proj)
1435 return n->op->ops.equivalent_node_Proj(proj);
1437 } /* equivalent_node_Proj */
1442 static ir_node *equivalent_node_Id(ir_node *n)
1450 DBG_OPT_ID(oldn, n);
1452 } /* equivalent_node_Id */
1457 static ir_node *equivalent_node_Mux(ir_node *n)
1459 ir_node *oldn = n, *sel = get_Mux_sel(n);
1461 ir_tarval *ts = value_of(sel);
1463 if (ts == tarval_bad && is_Cmp(sel)) {
1464 /* try again with a direct call to compute_cmp, as we don't care
1465 * about the MODEB_LOWERED flag here */
1466 ts = compute_cmp(sel);
1469 /* Mux(true, f, t) == t */
1470 if (ts == tarval_b_true) {
1471 n = get_Mux_true(n);
1472 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1475 /* Mux(false, f, t) == f */
1476 if (ts == tarval_b_false) {
1477 n = get_Mux_false(n);
1478 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1481 n_t = get_Mux_true(n);
1482 n_f = get_Mux_false(n);
1484 /* Mux(v, x, T) == x */
1485 if (is_Unknown(n_f)) {
1487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1490 /* Mux(v, T, x) == x */
1491 if (is_Unknown(n_t)) {
1493 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1497 /* Mux(v, x, x) == x */
1500 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1503 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1504 ir_relation relation = get_Cmp_relation(sel);
1505 ir_node *f = get_Mux_false(n);
1506 ir_node *t = get_Mux_true(n);
1509 * Note further that these optimization work even for floating point
1510 * with NaN's because -NaN == NaN.
1511 * However, if +0 and -0 is handled differently, we cannot use the first one.
1513 ir_node *const cmp_l = get_Cmp_left(sel);
1514 ir_node *const cmp_r = get_Cmp_right(sel);
1517 case ir_relation_equal:
1518 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1519 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1521 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1526 case ir_relation_less_greater:
1527 case ir_relation_unordered_less_greater:
1528 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1529 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1531 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1540 * Note: normalization puts the constant on the right side,
1541 * so we check only one case.
1543 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1544 /* Mux(t CMP 0, X, t) */
1545 if (is_Minus(f) && get_Minus_op(f) == t) {
1546 /* Mux(t CMP 0, -t, t) */
1547 if (relation == ir_relation_equal) {
1548 /* Mux(t == 0, -t, t) ==> -t */
1550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1551 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1552 /* Mux(t != 0, -t, t) ==> t */
1554 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1564 * Remove Confirm nodes if setting is on.
1565 * Replace Confirms(x, '=', Constlike) by Constlike.
1567 static ir_node *equivalent_node_Confirm(ir_node *n)
1569 ir_node *pred = get_Confirm_value(n);
1570 ir_relation relation = get_Confirm_relation(n);
1572 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1574 * rare case: two identical Confirms one after another,
1575 * replace the second one with the first.
1578 pred = get_Confirm_value(n);
1584 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1585 * perform no actual computation, as, e.g., the Id nodes. It does not create
1586 * new nodes. It is therefore safe to free n if the node returned is not n.
1587 * If a node returns a Tuple we can not just skip it. If the size of the
1588 * in array fits, we transform n into a tuple (e.g., Div).
1590 ir_node *equivalent_node(ir_node *n)
1592 if (n->op->ops.equivalent_node)
1593 return n->op->ops.equivalent_node(n);
1595 } /* equivalent_node */
1598 * Sets the default equivalent node operation for an ir_op_ops.
1600 * @param code the opcode for the default operation
1601 * @param ops the operations initialized
1606 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1610 ops->equivalent_node = equivalent_node_##a; \
1612 #define CASE_PROJ(a) \
1614 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1648 } /* firm_set_default_equivalent_node */
1651 * Returns non-zero if a node is a Phi node
1652 * with all predecessors constant.
1654 static int is_const_Phi(ir_node *n)
1658 if (! is_Phi(n) || get_irn_arity(n) == 0)
1660 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1661 if (! is_Const(get_irn_n(n, i)))
1665 } /* is_const_Phi */
1667 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1668 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1671 * in reality eval_func should be tarval (*eval_func)() but incomplete
1672 * declarations are bad style and generate noisy warnings
1674 typedef void (*eval_func)(void);
1677 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1679 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1681 if (eval == (eval_func) tarval_sub) {
1682 tarval_sub_type func = (tarval_sub_type)eval;
1684 return func(a, b, mode);
1686 tarval_binop_type func = (tarval_binop_type)eval;
1693 * Apply an evaluator on a binop with a constant operators (and one Phi).
1695 * @param phi the Phi node
1696 * @param other the other operand
1697 * @param eval an evaluator function
1698 * @param mode the mode of the result, may be different from the mode of the Phi!
1699 * @param left if non-zero, other is the left operand, else the right
1701 * @return a new Phi node if the conversion was successful, NULL else
1703 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1709 int i, n = get_irn_arity(phi);
1711 NEW_ARR_A(void *, res, n);
1713 for (i = 0; i < n; ++i) {
1714 pred = get_irn_n(phi, i);
1715 tv = get_Const_tarval(pred);
1716 tv = do_eval(eval, other, tv, mode);
1718 if (tv == tarval_bad) {
1719 /* folding failed, bad */
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = do_eval(eval, tv, other, mode);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1737 irg = get_irn_irg(phi);
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1742 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1743 } /* apply_binop_on_phi */
1746 * Apply an evaluator on a binop with two constant Phi.
1748 * @param a the left Phi node
1749 * @param b the right Phi node
1750 * @param eval an evaluator function
1751 * @param mode the mode of the result, may be different from the mode of the Phi!
1753 * @return a new Phi node if the conversion was successful, NULL else
1755 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1757 ir_tarval *tv_l, *tv_r, *tv;
1763 if (get_nodes_block(a) != get_nodes_block(b))
1766 n = get_irn_arity(a);
1767 NEW_ARR_A(void *, res, n);
1769 for (i = 0; i < n; ++i) {
1770 pred = get_irn_n(a, i);
1771 tv_l = get_Const_tarval(pred);
1772 pred = get_irn_n(b, i);
1773 tv_r = get_Const_tarval(pred);
1774 tv = do_eval(eval, tv_l, tv_r, mode);
1776 if (tv == tarval_bad) {
1777 /* folding failed, bad */
1782 irg = get_irn_irg(a);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(a, i);
1785 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1787 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1788 } /* apply_binop_on_2_phis */
1791 * Apply an evaluator on a unop with a constant operator (a Phi).
1793 * @param phi the Phi node
1794 * @param eval an evaluator function
1796 * @return a new Phi node if the conversion was successful, NULL else
1798 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1805 int i, n = get_irn_arity(phi);
1807 NEW_ARR_A(void *, res, n);
1808 for (i = 0; i < n; ++i) {
1809 pred = get_irn_n(phi, i);
1810 tv = get_Const_tarval(pred);
1813 if (tv == tarval_bad) {
1814 /* folding failed, bad */
1819 mode = get_irn_mode(phi);
1820 irg = get_irn_irg(phi);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1825 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1826 } /* apply_unop_on_phi */
1829 * Apply a conversion on a constant operator (a Phi).
1831 * @param phi the Phi node
1833 * @return a new Phi node if the conversion was successful, NULL else
1835 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1841 int i, n = get_irn_arity(phi);
1843 NEW_ARR_A(void *, res, n);
1844 for (i = 0; i < n; ++i) {
1845 pred = get_irn_n(phi, i);
1846 tv = get_Const_tarval(pred);
1847 tv = tarval_convert_to(tv, mode);
1849 if (tv == tarval_bad) {
1850 /* folding failed, bad */
1855 irg = get_irn_irg(phi);
1856 for (i = 0; i < n; ++i) {
1857 pred = get_irn_n(phi, i);
1858 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1860 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1861 } /* apply_conv_on_phi */
1864 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1865 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1866 * If possible, remove the Conv's.
1868 static ir_node *transform_node_AddSub(ir_node *n)
1870 ir_mode *mode = get_irn_mode(n);
1872 if (mode_is_reference(mode)) {
1873 ir_node *left = get_binop_left(n);
1874 ir_node *right = get_binop_right(n);
1875 unsigned ref_bits = get_mode_size_bits(mode);
1877 if (is_Conv(left)) {
1878 ir_mode *lmode = get_irn_mode(left);
1879 unsigned bits = get_mode_size_bits(lmode);
1881 if (ref_bits == bits &&
1882 mode_is_int(lmode) &&
1883 get_mode_arithmetic(lmode) == irma_twos_complement) {
1884 ir_node *pre = get_Conv_op(left);
1885 ir_mode *pre_mode = get_irn_mode(pre);
1887 if (mode_is_int(pre_mode) &&
1888 get_mode_size_bits(pre_mode) == bits &&
1889 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1890 /* ok, this conv just changes to sign, moreover the calculation
1891 * is done with same number of bits as our address mode, so
1892 * we can ignore the conv as address calculation can be viewed
1893 * as either signed or unsigned
1895 set_binop_left(n, pre);
1900 if (is_Conv(right)) {
1901 ir_mode *rmode = get_irn_mode(right);
1902 unsigned bits = get_mode_size_bits(rmode);
1904 if (ref_bits == bits &&
1905 mode_is_int(rmode) &&
1906 get_mode_arithmetic(rmode) == irma_twos_complement) {
1907 ir_node *pre = get_Conv_op(right);
1908 ir_mode *pre_mode = get_irn_mode(pre);
1910 if (mode_is_int(pre_mode) &&
1911 get_mode_size_bits(pre_mode) == bits &&
1912 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1913 /* ok, this conv just changes to sign, moreover the calculation
1914 * is done with same number of bits as our address mode, so
1915 * we can ignore the conv as address calculation can be viewed
1916 * as either signed or unsigned
1918 set_binop_right(n, pre);
1923 /* let address arithmetic use unsigned modes */
1924 if (is_Const(right)) {
1925 ir_mode *rmode = get_irn_mode(right);
1927 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1928 /* convert a AddP(P, *s) into AddP(P, *u) */
1929 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1931 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1932 set_binop_right(n, pre);
1938 } /* transform_node_AddSub */
1940 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1943 if (is_Const(b) && is_const_Phi(a)) { \
1944 /* check for Op(Phi, Const) */ \
1945 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1947 else if (is_Const(a) && is_const_Phi(b)) { \
1948 /* check for Op(Const, Phi) */ \
1949 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1951 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1952 /* check for Op(Phi, Phi) */ \
1953 c = apply_binop_on_2_phis(a, b, eval, mode); \
1956 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1961 #define HANDLE_UNOP_PHI(eval, a, c) \
1964 if (is_const_Phi(a)) { \
1965 /* check for Op(Phi) */ \
1966 c = apply_unop_on_phi(a, eval); \
1968 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1975 * Do the AddSub optimization, then Transform
1976 * Constant folding on Phi
1977 * Add(a,a) -> Mul(a, 2)
1978 * Add(Mul(a, x), a) -> Mul(a, x+1)
1979 * if the mode is integer or float.
1980 * Transform Add(a,-b) into Sub(a,b).
1981 * Reassociation might fold this further.
1983 static ir_node *transform_node_Add(ir_node *n)
1986 ir_node *a, *b, *c, *oldn = n;
1987 vrp_attr *a_vrp, *b_vrp;
1989 n = transform_node_AddSub(n);
1991 a = get_Add_left(n);
1992 b = get_Add_right(n);
1994 mode = get_irn_mode(n);
1996 if (mode_is_reference(mode)) {
1997 ir_mode *lmode = get_irn_mode(a);
1999 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2000 /* an Add(a, NULL) is a hidden Conv */
2001 dbg_info *dbg = get_irn_dbg_info(n);
2002 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2006 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2008 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2009 if (mode_is_float(mode)) {
2010 ir_graph *irg = get_irn_irg(n);
2011 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2015 if (mode_is_num(mode)) {
2016 ir_graph *irg = get_irn_irg(n);
2017 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2018 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2019 && a == b && mode_is_int(mode)) {
2020 ir_node *block = get_nodes_block(n);
2023 get_irn_dbg_info(n),
2026 new_r_Const_long(irg, mode, 2),
2028 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2033 get_irn_dbg_info(n),
2038 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2043 get_irn_dbg_info(n),
2048 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2051 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2052 /* Here we rely on constants be on the RIGHT side */
2054 ir_node *op = get_Not_op(a);
2056 if (is_Const(b) && is_Const_one(b)) {
2058 ir_node *blk = get_nodes_block(n);
2059 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2060 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2067 a_vrp = vrp_get_info(a);
2068 b_vrp = vrp_get_info(b);
2070 if (a_vrp && b_vrp) {
2071 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2073 if (tarval_is_null(vrp_val)) {
2074 dbg_info *dbgi = get_irn_dbg_info(n);
2075 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2079 } /* transform_node_Add */
2082 * returns -cnst or NULL if impossible
2084 static ir_node *const_negate(ir_node *cnst)
2086 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2087 dbg_info *dbgi = get_irn_dbg_info(cnst);
2088 ir_graph *irg = get_irn_irg(cnst);
2089 if (tv == tarval_bad) return NULL;
2090 return new_rd_Const(dbgi, irg, tv);
2094 * Do the AddSub optimization, then Transform
2095 * Constant folding on Phi
2096 * Sub(0,a) -> Minus(a)
2097 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2098 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2099 * Sub(Add(a, x), x) -> a
2100 * Sub(x, Add(x, a)) -> -a
2101 * Sub(x, Const) -> Add(x, -Const)
2103 static ir_node *transform_node_Sub(ir_node *n)
2109 n = transform_node_AddSub(n);
2111 a = get_Sub_left(n);
2112 b = get_Sub_right(n);
2114 mode = get_irn_mode(n);
2116 if (mode_is_int(mode)) {
2117 ir_mode *lmode = get_irn_mode(a);
2119 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2120 /* a Sub(a, NULL) is a hidden Conv */
2121 dbg_info *dbg = get_irn_dbg_info(n);
2122 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2127 if (mode == lmode &&
2128 get_mode_arithmetic(mode) == irma_twos_complement &&
2130 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2132 dbg_info *dbg = get_irn_dbg_info(n);
2133 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2140 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2142 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2143 if (mode_is_float(mode)) {
2144 ir_graph *irg = get_irn_irg(n);
2145 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2149 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2150 /* a - C -> a + (-C) */
2151 ir_node *cnst = const_negate(b);
2153 ir_node *block = get_nodes_block(n);
2154 dbg_info *dbgi = get_irn_dbg_info(n);
2156 n = new_rd_Add(dbgi, block, a, cnst, mode);
2157 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2162 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2163 dbg_info *dbg = get_irn_dbg_info(n);
2164 ir_node *block = get_nodes_block(n);
2165 ir_node *left = get_Minus_op(a);
2166 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2168 n = new_rd_Minus(dbg, block, add, mode);
2169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2171 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2172 dbg_info *dbg = get_irn_dbg_info(n);
2173 ir_node *block = get_nodes_block(n);
2174 ir_node *right = get_Minus_op(b);
2176 n = new_rd_Add(dbg, block, a, right, mode);
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2179 } else if (is_Sub(b)) {
2180 /* a - (b - c) -> a + (c - b)
2181 * -> (a - b) + c iff (b - c) is a pointer */
2182 dbg_info *s_dbg = get_irn_dbg_info(b);
2183 ir_node *s_left = get_Sub_left(b);
2184 ir_node *s_right = get_Sub_right(b);
2185 ir_mode *s_mode = get_irn_mode(b);
2186 if (mode_is_reference(s_mode)) {
2187 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2188 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2189 dbg_info *a_dbg = get_irn_dbg_info(n);
2192 s_right = new_r_Conv(lowest_block, s_right, mode);
2193 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2195 ir_node *s_block = get_nodes_block(b);
2196 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2197 dbg_info *a_dbg = get_irn_dbg_info(n);
2198 ir_node *a_block = get_nodes_block(n);
2200 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2205 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2206 ir_node *m_right = get_Mul_right(b);
2207 if (is_Const(m_right)) {
2208 ir_node *cnst2 = const_negate(m_right);
2209 if (cnst2 != NULL) {
2210 dbg_info *m_dbg = get_irn_dbg_info(b);
2211 ir_node *m_block = get_nodes_block(b);
2212 ir_node *m_left = get_Mul_left(b);
2213 ir_mode *m_mode = get_irn_mode(b);
2214 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2215 dbg_info *a_dbg = get_irn_dbg_info(n);
2216 ir_node *a_block = get_nodes_block(n);
2218 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2226 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2227 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2229 get_irn_dbg_info(n),
2233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2237 if (mode_wrap_around(mode)) {
2238 ir_node *left = get_Add_left(a);
2239 ir_node *right = get_Add_right(a);
2241 /* FIXME: Does the Conv's work only for two complement or generally? */
2243 if (mode != get_irn_mode(right)) {
2244 /* This Sub is an effective Cast */
2245 right = new_r_Conv(get_nodes_block(n), right, mode);
2248 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2250 } else if (right == b) {
2251 if (mode != get_irn_mode(left)) {
2252 /* This Sub is an effective Cast */
2253 left = new_r_Conv(get_nodes_block(n), left, mode);
2256 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2262 if (mode_wrap_around(mode)) {
2263 ir_node *left = get_Add_left(b);
2264 ir_node *right = get_Add_right(b);
2266 /* FIXME: Does the Conv's work only for two complement or generally? */
2268 ir_mode *r_mode = get_irn_mode(right);
2270 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2271 if (mode != r_mode) {
2272 /* This Sub is an effective Cast */
2273 n = new_r_Conv(get_nodes_block(n), n, mode);
2275 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2277 } else if (right == a) {
2278 ir_mode *l_mode = get_irn_mode(left);
2280 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2281 if (mode != l_mode) {
2282 /* This Sub is an effective Cast */
2283 n = new_r_Conv(get_nodes_block(n), n, mode);
2285 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2290 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2291 ir_mode *mode = get_irn_mode(a);
2293 if (mode == get_irn_mode(b)) {
2295 ir_node *op_a = get_Conv_op(a);
2296 ir_node *op_b = get_Conv_op(b);
2298 /* check if it's allowed to skip the conv */
2299 ma = get_irn_mode(op_a);
2300 mb = get_irn_mode(op_b);
2302 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2303 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2306 set_Sub_right(n, b);
2312 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2313 if (!is_reassoc_running() && is_Mul(a)) {
2314 ir_node *ma = get_Mul_left(a);
2315 ir_node *mb = get_Mul_right(a);
2318 ir_node *blk = get_nodes_block(n);
2319 ir_graph *irg = get_irn_irg(n);
2321 get_irn_dbg_info(n),
2325 get_irn_dbg_info(n),
2328 new_r_Const(irg, get_mode_one(mode)),
2331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2333 } else if (mb == b) {
2334 ir_node *blk = get_nodes_block(n);
2335 ir_graph *irg = get_irn_irg(n);
2337 get_irn_dbg_info(n),
2341 get_irn_dbg_info(n),
2344 new_r_Const(irg, get_mode_one(mode)),
2347 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2351 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2352 ir_node *x = get_Sub_left(a);
2353 ir_node *y = get_Sub_right(a);
2354 ir_node *blk = get_nodes_block(n);
2355 ir_mode *m_b = get_irn_mode(b);
2356 ir_mode *m_y = get_irn_mode(y);
2360 /* Determine the right mode for the Add. */
2363 else if (mode_is_reference(m_b))
2365 else if (mode_is_reference(m_y))
2369 * Both modes are different but none is reference,
2370 * happens for instance in SubP(SubP(P, Iu), Is).
2371 * We have two possibilities here: Cast or ignore.
2372 * Currently we ignore this case.
2377 add = new_r_Add(blk, y, b, add_mode);
2379 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2384 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2385 /* c - ~X = X + (c+1) */
2386 if (is_Const(a) && is_Not(b)) {
2387 ir_tarval *tv = get_Const_tarval(a);
2389 tv = tarval_add(tv, get_mode_one(mode));
2390 if (tv != tarval_bad) {
2391 ir_node *blk = get_nodes_block(n);
2392 ir_graph *irg = get_irn_irg(n);
2393 ir_node *c = new_r_Const(irg, tv);
2394 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2399 /* x-(x&y) = x & ~y */
2401 ir_node *and_left = get_And_left(b);
2402 ir_node *and_right = get_And_right(b);
2403 if (and_right == a) {
2404 ir_node *tmp = and_left;
2405 and_left = and_right;
2408 if (and_left == a) {
2409 dbg_info *dbgi = get_irn_dbg_info(n);
2410 ir_node *block = get_nodes_block(n);
2411 ir_mode *mode = get_irn_mode(n);
2412 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2413 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
2419 } /* transform_node_Sub */
2422 * Several transformation done on n*n=2n bits mul.
2423 * These transformations must be done here because new nodes may be produced.
2425 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2428 ir_node *a = get_Mul_left(n);
2429 ir_node *b = get_Mul_right(n);
2430 ir_tarval *ta = value_of(a);
2431 ir_tarval *tb = value_of(b);
2432 ir_mode *smode = get_irn_mode(a);
2434 if (ta == get_mode_one(smode)) {
2435 /* (L)1 * (L)b = (L)b */
2436 ir_node *blk = get_nodes_block(n);
2437 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2438 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2441 else if (ta == get_mode_minus_one(smode)) {
2442 /* (L)-1 * (L)b = (L)b */
2443 ir_node *blk = get_nodes_block(n);
2444 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2445 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2446 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2449 if (tb == get_mode_one(smode)) {
2450 /* (L)a * (L)1 = (L)a */
2451 ir_node *blk = get_irn_n(a, -1);
2452 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2453 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2456 else if (tb == get_mode_minus_one(smode)) {
2457 /* (L)a * (L)-1 = (L)-a */
2458 ir_node *blk = get_nodes_block(n);
2459 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2460 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2461 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2468 * Transform Mul(a,-1) into -a.
2469 * Do constant evaluation of Phi nodes.
2470 * Do architecture dependent optimizations on Mul nodes
2472 static ir_node *transform_node_Mul(ir_node *n)
2474 ir_node *c, *oldn = n;
2475 ir_mode *mode = get_irn_mode(n);
2476 ir_node *a = get_Mul_left(n);
2477 ir_node *b = get_Mul_right(n);
2479 if (is_Bad(a) || is_Bad(b))
2482 if (mode != get_irn_mode(a))
2483 return transform_node_Mul2n(n, mode);
2485 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2487 if (mode_is_signed(mode)) {
2490 if (value_of(a) == get_mode_minus_one(mode))
2492 else if (value_of(b) == get_mode_minus_one(mode))
2495 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2496 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2501 if (is_Const(b)) { /* (-a) * const -> a * -const */
2502 ir_node *cnst = const_negate(b);
2504 dbg_info *dbgi = get_irn_dbg_info(n);
2505 ir_node *block = get_nodes_block(n);
2506 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2507 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2510 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2511 dbg_info *dbgi = get_irn_dbg_info(n);
2512 ir_node *block = get_nodes_block(n);
2513 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2514 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2516 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2517 ir_node *sub_l = get_Sub_left(b);
2518 ir_node *sub_r = get_Sub_right(b);
2519 dbg_info *dbgi = get_irn_dbg_info(n);
2520 ir_node *block = get_nodes_block(n);
2521 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2522 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2523 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2526 } else if (is_Minus(b)) {
2527 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2528 ir_node *sub_l = get_Sub_left(a);
2529 ir_node *sub_r = get_Sub_right(a);
2530 dbg_info *dbgi = get_irn_dbg_info(n);
2531 ir_node *block = get_nodes_block(n);
2532 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2533 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2534 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2537 } else if (is_Shl(a)) {
2538 ir_node *const shl_l = get_Shl_left(a);
2539 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2540 /* (1 << x) * b -> b << x */
2541 dbg_info *const dbgi = get_irn_dbg_info(n);
2542 ir_node *const block = get_nodes_block(n);
2543 ir_node *const shl_r = get_Shl_right(a);
2544 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2545 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2548 } else if (is_Shl(b)) {
2549 ir_node *const shl_l = get_Shl_left(b);
2550 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2551 /* a * (1 << x) -> a << x */
2552 dbg_info *const dbgi = get_irn_dbg_info(n);
2553 ir_node *const block = get_nodes_block(n);
2554 ir_node *const shl_r = get_Shl_right(b);
2555 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2556 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2560 if (get_mode_arithmetic(mode) == irma_ieee754) {
2562 ir_tarval *tv = get_Const_tarval(a);
2563 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2564 && !tarval_is_negative(tv)) {
2565 /* 2.0 * b = b + b */
2566 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2567 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2571 else if (is_Const(b)) {
2572 ir_tarval *tv = get_Const_tarval(b);
2573 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2574 && !tarval_is_negative(tv)) {
2575 /* a * 2.0 = a + a */
2576 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2577 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2582 return arch_dep_replace_mul_with_shifts(n);
2583 } /* transform_node_Mul */
2586 * Transform a Div Node.
2588 static ir_node *transform_node_Div(ir_node *n)
2590 ir_mode *mode = get_Div_resmode(n);
2591 ir_node *a = get_Div_left(n);
2592 ir_node *b = get_Div_right(n);
2594 const ir_node *dummy;
2596 if (mode_is_int(mode)) {
2597 if (is_Const(b) && is_const_Phi(a)) {
2598 /* check for Div(Phi, Const) */
2599 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2601 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2604 } else if (is_Const(a) && is_const_Phi(b)) {
2605 /* check for Div(Const, Phi) */
2606 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2608 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2611 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2612 /* check for Div(Phi, Phi) */
2613 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2615 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2620 if (a == b && value_not_zero(a, &dummy)) {
2621 ir_graph *irg = get_irn_irg(n);
2622 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2623 value = new_r_Const(irg, get_mode_one(mode));
2624 DBG_OPT_CSTEVAL(n, value);
2627 if (mode_is_signed(mode) && is_Const(b)) {
2628 ir_tarval *tv = get_Const_tarval(b);
2630 if (tv == get_mode_minus_one(mode)) {
2632 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2633 DBG_OPT_CSTEVAL(n, value);
2637 /* Try architecture dependent optimization */
2638 value = arch_dep_replace_div_by_const(n);
2641 assert(mode_is_float(mode));
2643 /* Optimize x/c to x*(1/c) */
2644 if (get_mode_arithmetic(mode) == irma_ieee754) {
2645 ir_tarval *tv = value_of(b);
2647 if (tv != tarval_bad) {
2648 int rem = tarval_fp_ops_enabled();
2651 * Floating point constant folding might be disabled here to
2653 * However, as we check for exact result, doing it is safe.
2656 tarval_enable_fp_ops(1);
2657 tv = tarval_div(get_mode_one(mode), tv);
2658 tarval_enable_fp_ops(rem);
2660 /* Do the transformation if the result is either exact or we are
2661 not using strict rules. */
2662 if (tv != tarval_bad &&
2663 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2664 ir_node *block = get_nodes_block(n);
2665 ir_graph *irg = get_irn_irg(block);
2666 ir_node *c = new_r_Const(irg, tv);
2667 dbg_info *dbgi = get_irn_dbg_info(n);
2668 value = new_rd_Mul(dbgi, block, a, c, mode);
2681 /* Turn Div into a tuple (mem, jmp, bad, value) */
2682 mem = get_Div_mem(n);
2683 blk = get_nodes_block(n);
2684 irg = get_irn_irg(blk);
2686 /* skip a potential Pin */
2687 mem = skip_Pin(mem);
2688 turn_into_tuple(n, pn_Div_max+1);
2689 set_Tuple_pred(n, pn_Div_M, mem);
2690 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2691 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2692 set_Tuple_pred(n, pn_Div_res, value);
2695 } /* transform_node_Div */
2698 * Transform a Mod node.
2700 static ir_node *transform_node_Mod(ir_node *n)
2702 ir_mode *mode = get_Mod_resmode(n);
2703 ir_node *a = get_Mod_left(n);
2704 ir_node *b = get_Mod_right(n);
2709 if (is_Const(b) && is_const_Phi(a)) {
2710 /* check for Div(Phi, Const) */
2711 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2713 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2717 else if (is_Const(a) && is_const_Phi(b)) {
2718 /* check for Div(Const, Phi) */
2719 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2721 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2725 else if (is_const_Phi(a) && is_const_Phi(b)) {
2726 /* check for Div(Phi, Phi) */
2727 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2729 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2736 irg = get_irn_irg(n);
2737 if (tv != tarval_bad) {
2738 value = new_r_Const(irg, tv);
2740 DBG_OPT_CSTEVAL(n, value);
2743 ir_node *a = get_Mod_left(n);
2744 ir_node *b = get_Mod_right(n);
2745 const ir_node *dummy;
2747 if (a == b && value_not_zero(a, &dummy)) {
2748 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2749 value = new_r_Const(irg, get_mode_null(mode));
2750 DBG_OPT_CSTEVAL(n, value);
2753 if (mode_is_signed(mode) && is_Const(b)) {
2754 ir_tarval *tv = get_Const_tarval(b);
2756 if (tv == get_mode_minus_one(mode)) {
2758 value = new_r_Const(irg, get_mode_null(mode));
2759 DBG_OPT_CSTEVAL(n, value);
2763 /* Try architecture dependent optimization */
2764 value = arch_dep_replace_mod_by_const(n);
2773 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2774 mem = get_Mod_mem(n);
2775 blk = get_nodes_block(n);
2776 irg = get_irn_irg(blk);
2778 /* skip a potential Pin */
2779 mem = skip_Pin(mem);
2780 turn_into_tuple(n, pn_Mod_max+1);
2781 set_Tuple_pred(n, pn_Mod_M, mem);
2782 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2783 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2784 set_Tuple_pred(n, pn_Mod_res, value);
2787 } /* transform_node_Mod */
2790 * Transform a Cond node.
2792 * Replace the Cond by a Jmp if it branches on a constant
2795 static ir_node *transform_node_Cond(ir_node *n)
2798 ir_node *a = get_Cond_selector(n);
2799 ir_graph *irg = get_irn_irg(n);
2803 /* we need block info which is not available in floating irgs */
2804 if (get_irg_pinned(irg) == op_pin_state_floats)
2807 /* we do not handle switches here */
2808 if (get_irn_mode(a) != mode_b)
2812 if (ta == tarval_bad && is_Cmp(a)) {
2813 /* try again with a direct call to compute_cmp, as we don't care
2814 * about the MODEB_LOWERED flag here */
2815 ta = compute_cmp(a);
2818 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
2819 /* It's a boolean Cond, branching on a boolean constant.
2820 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2821 ir_node *blk = get_nodes_block(n);
2822 jmp = new_r_Jmp(blk);
2823 turn_into_tuple(n, pn_Cond_max+1);
2824 if (ta == tarval_b_true) {
2825 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2826 set_Tuple_pred(n, pn_Cond_true, jmp);
2828 set_Tuple_pred(n, pn_Cond_false, jmp);
2829 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2831 /* We might generate an endless loop, so keep it alive. */
2832 add_End_keepalive(get_irg_end(irg), blk);
2833 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
2836 } /* transform_node_Cond */
2839 * Prototype of a recursive transform function
2840 * for bitwise distributive transformations.
2842 typedef ir_node* (*recursive_transform)(ir_node *n);
2845 * makes use of distributive laws for and, or, eor
2846 * and(a OP c, b OP c) -> and(a, b) OP c
2847 * note, might return a different op than n
2849 static ir_node *transform_bitwise_distributive(ir_node *n,
2850 recursive_transform trans_func)
2853 ir_node *a = get_binop_left(n);
2854 ir_node *b = get_binop_right(n);
2855 ir_op *op = get_irn_op(a);
2856 ir_op *op_root = get_irn_op(n);
2858 if (op != get_irn_op(b))
2861 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2862 if (op == op_Conv) {
2863 ir_node *a_op = get_Conv_op(a);
2864 ir_node *b_op = get_Conv_op(b);
2865 ir_mode *a_mode = get_irn_mode(a_op);
2866 ir_mode *b_mode = get_irn_mode(b_op);
2867 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2868 ir_node *blk = get_nodes_block(n);
2871 set_binop_left(n, a_op);
2872 set_binop_right(n, b_op);
2873 set_irn_mode(n, a_mode);
2875 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2877 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2883 /* nothing to gain here */
2887 if (op == op_Shrs || op == op_Shr || op == op_Shl
2888 || op == op_And || op == op_Or || op == op_Eor) {
2889 ir_node *a_left = get_binop_left(a);
2890 ir_node *a_right = get_binop_right(a);
2891 ir_node *b_left = get_binop_left(b);
2892 ir_node *b_right = get_binop_right(b);
2894 ir_node *op1 = NULL;
2895 ir_node *op2 = NULL;
2897 if (is_op_commutative(op)) {
2898 if (a_left == b_left) {
2902 } else if (a_left == b_right) {
2906 } else if (a_right == b_left) {
2912 if (a_right == b_right) {
2919 /* (a sop c) & (b sop c) => (a & b) sop c */
2920 ir_node *blk = get_nodes_block(n);
2922 ir_node *new_n = exact_copy(n);
2923 set_binop_left(new_n, op1);
2924 set_binop_right(new_n, op2);
2925 new_n = trans_func(new_n);
2927 if (op_root == op_Eor && op == op_Or) {
2928 dbg_info *dbgi = get_irn_dbg_info(n);
2929 ir_mode *mode = get_irn_mode(c);
2931 c = new_rd_Not(dbgi, blk, c, mode);
2932 n = new_rd_And(dbgi, blk, new_n, c, mode);
2935 set_nodes_block(n, blk);
2936 set_binop_left(n, new_n);
2937 set_binop_right(n, c);
2941 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2950 * Create a 0 constant of given mode.
2952 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2954 ir_tarval *tv = get_mode_null(mode);
2955 ir_node *cnst = new_r_Const(irg, tv);
2960 static bool is_shiftop(const ir_node *n)
2962 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2966 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
2968 * - and, or, xor instead of &
2969 * - Shl, Shr, Shrs, rotl instead of >>
2970 * (with a special case for Or/Xor + Shrs)
2972 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
2974 static ir_node *transform_node_shift_bitop(ir_node *n)
2976 ir_graph *irg = get_irn_irg(n);
2977 ir_node *right = get_binop_right(n);
2978 ir_mode *mode = get_irn_mode(n);
2980 ir_node *bitop_left;
2981 ir_node *bitop_right;
2990 ir_tarval *tv_shift;
2992 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2995 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
2997 if (!is_Const(right))
3000 left = get_binop_left(n);
3001 op_left = get_irn_op(left);
3002 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
3005 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3006 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
3007 /* TODO: test if sign bit is affectes */
3011 bitop_right = get_binop_right(left);
3012 if (!is_Const(bitop_right))
3015 bitop_left = get_binop_left(left);
3017 block = get_nodes_block(n);
3018 dbgi = get_irn_dbg_info(n);
3019 tv1 = get_Const_tarval(bitop_right);
3020 tv2 = get_Const_tarval(right);
3022 assert(get_tarval_mode(tv1) == mode);
3025 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3026 tv_shift = tarval_shl(tv1, tv2);
3027 } else if (is_Shr(n)) {
3028 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3029 tv_shift = tarval_shr(tv1, tv2);
3030 } else if (is_Shrs(n)) {
3031 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3032 tv_shift = tarval_shrs(tv1, tv2);
3035 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3036 tv_shift = tarval_rotl(tv1, tv2);
3039 assert(get_tarval_mode(tv_shift) == mode);
3040 irg = get_irn_irg(n);
3041 new_const = new_r_Const(irg, tv_shift);
3043 if (op_left == op_And) {
3044 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3045 } else if (op_left == op_Or) {
3046 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3048 assert(op_left == op_Eor);
3049 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3056 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
3058 * - and, or, xor instead of &
3059 * - Shl, Shr, Shrs, rotl instead of >>
3060 * (with a special case for Or/Xor + Shrs)
3062 * This normalisation is usually good for the backend since << C can often be
3063 * matched as address-mode.
3065 static ir_node *transform_node_bitop_shift(ir_node *n)
3067 ir_graph *irg = get_irn_irg(n);
3068 ir_node *left = get_binop_left(n);
3069 ir_node *right = get_binop_right(n);
3070 ir_mode *mode = get_irn_mode(n);
3071 ir_node *shift_left;
3072 ir_node *shift_right;
3074 dbg_info *dbg_bitop;
3075 dbg_info *dbg_shift;
3081 ir_tarval *tv_bitop;
3083 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3086 assert(is_And(n) || is_Or(n) || is_Eor(n));
3087 if (!is_Const(right) || !is_shiftop(left))
3090 shift_left = get_binop_left(left);
3091 shift_right = get_binop_right(left);
3092 if (!is_Const(shift_right))
3095 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3096 if (is_Shrs(left)) {
3097 /* TODO this could be improved */
3101 irg = get_irn_irg(n);
3102 block = get_nodes_block(n);
3103 dbg_bitop = get_irn_dbg_info(n);
3104 dbg_shift = get_irn_dbg_info(left);
3105 tv1 = get_Const_tarval(shift_right);
3106 tv2 = get_Const_tarval(right);
3107 assert(get_tarval_mode(tv2) == mode);
3110 tv_bitop = tarval_shr(tv2, tv1);
3111 } else if (is_Shr(left)) {
3112 if (is_Or(n) || is_Eor(n)) {
3114 * TODO this can be improved by checking whether
3115 * the left shift produces an overflow
3119 tv_bitop = tarval_shl(tv2, tv1);
3121 assert(is_Rotl(left));
3122 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
3124 new_const = new_r_Const(irg, tv_bitop);
3127 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
3128 } else if (is_Or(n)) {
3129 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
3132 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
3136 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
3137 } else if (is_Shr(left)) {
3138 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
3140 assert(is_Rotl(left));
3141 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
3150 static ir_node *transform_node_And(ir_node *n)
3152 ir_node *c, *oldn = n;
3153 ir_node *a = get_And_left(n);
3154 ir_node *b = get_And_right(n);
3156 vrp_attr *a_vrp, *b_vrp;
3158 if (is_Cmp(a) && is_Cmp(b)) {
3159 ir_node *a_left = get_Cmp_left(a);
3160 ir_node *a_right = get_Cmp_right(a);
3161 ir_node *b_left = get_Cmp_left(b);
3162 ir_node *b_right = get_Cmp_right(b);
3163 ir_relation a_relation = get_Cmp_relation(a);
3164 ir_relation b_relation = get_Cmp_relation(b);
3165 /* we can combine the relations of two compares with the same
3167 if (a_left == b_left && b_left == b_right) {
3168 dbg_info *dbgi = get_irn_dbg_info(n);
3169 ir_node *block = get_nodes_block(n);
3170 ir_relation new_relation = a_relation & b_relation;
3171 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3173 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3174 if (a_relation == b_relation && a_relation == ir_relation_equal
3175 && !mode_is_float(get_irn_mode(a_left))
3176 && !mode_is_float(get_irn_mode(b_left))) {
3177 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3178 dbg_info *dbgi = get_irn_dbg_info(n);
3179 ir_node *block = get_nodes_block(n);
3180 ir_mode *a_mode = get_irn_mode(a_left);
3181 ir_mode *b_mode = get_irn_mode(b_left);
3182 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3183 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3184 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3185 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3186 ir_graph *irg = get_irn_irg(n);
3187 ir_node *zero = create_zero_const(irg, b_mode);
3188 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3190 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3191 dbg_info *dbgi = get_irn_dbg_info(n);
3192 ir_node *block = get_nodes_block(n);
3193 ir_mode *a_mode = get_irn_mode(a_left);
3194 ir_mode *b_mode = get_irn_mode(b_left);
3195 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3196 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3197 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3198 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3199 ir_graph *irg = get_irn_irg(n);
3200 ir_node *zero = create_zero_const(irg, a_mode);
3201 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3206 mode = get_irn_mode(n);
3207 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3211 ir_node *op = get_Not_op(b);
3213 ir_node *ba = get_And_left(op);
3214 ir_node *bb = get_And_right(op);
3216 /* it's enough to test the following cases due to normalization! */
3217 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3218 /* (a|b) & ~(a&b) = a^b */
3219 ir_node *block = get_nodes_block(n);
3221 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3222 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3230 ir_node *op = get_Not_op(a);
3232 ir_node *aa = get_And_left(op);
3233 ir_node *ab = get_And_right(op);
3235 /* it's enough to test the following cases due to normalization! */
3236 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3237 /* (a|b) & ~(a&b) = a^b */
3238 ir_node *block = get_nodes_block(n);
3240 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3241 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3248 ir_node *al = get_Eor_left(a);
3249 ir_node *ar = get_Eor_right(a);
3252 /* (b ^ a) & b -> ~a & b */
3253 dbg_info *dbg = get_irn_dbg_info(n);
3254 ir_node *block = get_nodes_block(n);
3256 ar = new_rd_Not(dbg, block, ar, mode);
3257 n = new_rd_And(dbg, block, ar, b, mode);
3258 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3262 /* (a ^ b) & b -> ~a & b */
3263 dbg_info *dbg = get_irn_dbg_info(n);
3264 ir_node *block = get_nodes_block(n);
3266 al = new_rd_Not(dbg, block, al, mode);
3267 n = new_rd_And(dbg, block, al, b, mode);
3268 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3273 ir_node *bl = get_Eor_left(b);
3274 ir_node *br = get_Eor_right(b);
3277 /* a & (a ^ b) -> a & ~b */
3278 dbg_info *dbg = get_irn_dbg_info(n);
3279 ir_node *block = get_nodes_block(n);
3281 br = new_rd_Not(dbg, block, br, mode);
3282 n = new_rd_And(dbg, block, br, a, mode);
3283 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3287 /* a & (b ^ a) -> a & ~b */
3288 dbg_info *dbg = get_irn_dbg_info(n);
3289 ir_node *block = get_nodes_block(n);
3291 bl = new_rd_Not(dbg, block, bl, mode);
3292 n = new_rd_And(dbg, block, bl, a, mode);
3293 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3297 if (is_Not(a) && is_Not(b)) {
3298 /* ~a & ~b = ~(a|b) */
3299 ir_node *block = get_nodes_block(n);
3300 ir_mode *mode = get_irn_mode(n);
3304 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3305 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3306 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3310 b_vrp = vrp_get_info(b);
3311 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3312 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3318 a_vrp = vrp_get_info(a);
3319 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3320 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3324 n = transform_bitwise_distributive(n, transform_node_And);
3326 n = transform_node_bitop_shift(n);
3329 } /* transform_node_And */
3331 /* the order of the values is important! */
3332 typedef enum const_class {
3338 static const_class classify_const(const ir_node* n)
3340 if (is_Const(n)) return const_const;
3341 if (is_irn_constlike(n)) return const_like;
3346 * Determines whether r is more constlike or has a larger index (in that order)
3349 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3351 const const_class l_order = classify_const(l);
3352 const const_class r_order = classify_const(r);
3354 l_order > r_order ||
3355 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3361 static ir_node *transform_node_Eor(ir_node *n)
3363 ir_node *c, *oldn = n;
3364 ir_node *a = get_Eor_left(n);
3365 ir_node *b = get_Eor_right(n);
3366 ir_mode *mode = get_irn_mode(n);
3368 /* we can combine the relations of two compares with the same operands */
3369 if (is_Cmp(a) && is_Cmp(b)) {
3370 ir_node *a_left = get_Cmp_left(a);
3371 ir_node *a_right = get_Cmp_left(a);
3372 ir_node *b_left = get_Cmp_left(b);
3373 ir_node *b_right = get_Cmp_right(b);
3374 if (a_left == b_left && b_left == b_right) {
3375 dbg_info *dbgi = get_irn_dbg_info(n);
3376 ir_node *block = get_nodes_block(n);
3377 ir_relation a_relation = get_Cmp_relation(a);
3378 ir_relation b_relation = get_Cmp_relation(b);
3379 ir_relation new_relation = a_relation ^ b_relation;
3380 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3384 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3386 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3387 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3388 dbg_info *dbg = get_irn_dbg_info(n);
3389 ir_node *block = get_nodes_block(n);
3390 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3391 ir_node *new_left = get_Not_op(a);
3392 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3395 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3396 dbg_info *dbg = get_irn_dbg_info(n);
3397 ir_node *block = get_nodes_block(n);
3398 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3399 ir_node *new_right = get_Not_op(b);
3400 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3401 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3405 /* x ^ 1...1 -> ~1 */
3406 if (is_Const(b) && is_Const_all_one(b)) {
3407 n = new_r_Not(get_nodes_block(n), a, mode);
3408 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3412 n = transform_bitwise_distributive(n, transform_node_Eor);
3414 n = transform_node_bitop_shift(n);
3417 } /* transform_node_Eor */
3422 static ir_node *transform_node_Not(ir_node *n)
3424 ir_node *c, *oldn = n;
3425 ir_node *a = get_Not_op(n);
3426 ir_mode *mode = get_irn_mode(n);
3428 HANDLE_UNOP_PHI(tarval_not,a,c);
3430 /* check for a boolean Not */
3432 dbg_info *dbgi = get_irn_dbg_info(a);
3433 ir_node *block = get_nodes_block(a);
3434 ir_relation relation = get_Cmp_relation(a);
3435 relation = get_negated_relation(relation);
3436 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3441 /* normalize ~(a ^ b) => a ^ ~b */
3443 dbg_info *dbg = get_irn_dbg_info(n);
3444 ir_node *block = get_nodes_block(n);
3445 ir_node *eor_right = get_Eor_right(a);
3446 ir_node *eor_left = get_Eor_left(a);
3447 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3448 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3452 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3453 if (is_Minus(a)) { /* ~-x -> x + -1 */
3454 dbg_info *dbg = get_irn_dbg_info(n);
3455 ir_graph *irg = get_irn_irg(n);
3456 ir_node *block = get_nodes_block(n);
3457 ir_node *add_l = get_Minus_op(a);
3458 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3459 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3460 } else if (is_Add(a)) {
3461 ir_node *add_r = get_Add_right(a);
3462 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3463 /* ~(x + -1) = -x */
3464 ir_node *op = get_Add_left(a);
3465 ir_node *blk = get_nodes_block(n);
3466 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3467 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3475 * Transform a Minus.
3479 * -(a >>u (size-1)) = a >>s (size-1)
3480 * -(a >>s (size-1)) = a >>u (size-1)
3481 * -(a * const) -> a * -const
3483 static ir_node *transform_node_Minus(ir_node *n)
3485 ir_node *c, *oldn = n;
3486 ir_node *a = get_Minus_op(n);
3489 HANDLE_UNOP_PHI(tarval_neg,a,c);
3491 mode = get_irn_mode(a);
3492 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3493 /* the following rules are only to twos-complement */
3496 ir_node *op = get_Not_op(a);
3497 ir_tarval *tv = get_mode_one(mode);
3498 ir_node *blk = get_nodes_block(n);
3499 ir_graph *irg = get_irn_irg(blk);
3500 ir_node *c = new_r_Const(irg, tv);
3501 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3502 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3506 ir_node *c = get_Shr_right(a);
3509 ir_tarval *tv = get_Const_tarval(c);
3511 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3512 /* -(a >>u (size-1)) = a >>s (size-1) */
3513 ir_node *v = get_Shr_left(a);
3515 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3516 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3522 ir_node *c = get_Shrs_right(a);
3525 ir_tarval *tv = get_Const_tarval(c);
3527 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3528 /* -(a >>s (size-1)) = a >>u (size-1) */
3529 ir_node *v = get_Shrs_left(a);
3531 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3532 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3539 /* - (a-b) = b - a */
3540 ir_node *la = get_Sub_left(a);
3541 ir_node *ra = get_Sub_right(a);
3542 ir_node *blk = get_nodes_block(n);
3544 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3545 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3549 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3550 ir_node *mul_l = get_Mul_left(a);
3551 ir_node *mul_r = get_Mul_right(a);
3552 ir_tarval *tv = value_of(mul_r);
3553 if (tv != tarval_bad) {
3554 tv = tarval_neg(tv);
3555 if (tv != tarval_bad) {
3556 ir_graph *irg = get_irn_irg(n);
3557 ir_node *cnst = new_r_Const(irg, tv);
3558 dbg_info *dbg = get_irn_dbg_info(a);
3559 ir_node *block = get_nodes_block(a);
3560 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3561 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3568 } /* transform_node_Minus */
3571 * Transform a Proj(Load) with a non-null address.
3573 static ir_node *transform_node_Proj_Load(ir_node *proj)
3575 if (get_opt_ldst_only_null_ptr_exceptions()) {
3576 if (get_irn_mode(proj) == mode_X) {
3577 ir_node *load = get_Proj_pred(proj);
3579 /* get the Load address */
3580 const ir_node *addr = get_Load_ptr(load);
3581 const ir_node *confirm;
3583 if (value_not_null(addr, &confirm)) {
3584 if (confirm == NULL) {
3585 /* this node may float if it did not depend on a Confirm */
3586 set_irn_pinned(load, op_pin_state_floats);
3588 if (get_Proj_proj(proj) == pn_Load_X_except) {
3589 ir_graph *irg = get_irn_irg(proj);
3590 DBG_OPT_EXC_REM(proj);
3591 return new_r_Bad(irg, mode_X);
3593 ir_node *blk = get_nodes_block(load);
3594 return new_r_Jmp(blk);
3600 } /* transform_node_Proj_Load */
3603 * Transform a Proj(Store) with a non-null address.
3605 static ir_node *transform_node_Proj_Store(ir_node *proj)
3607 if (get_opt_ldst_only_null_ptr_exceptions()) {
3608 if (get_irn_mode(proj) == mode_X) {
3609 ir_node *store = get_Proj_pred(proj);
3611 /* get the load/store address */
3612 const ir_node *addr = get_Store_ptr(store);
3613 const ir_node *confirm;
3615 if (value_not_null(addr, &confirm)) {
3616 if (confirm == NULL) {
3617 /* this node may float if it did not depend on a Confirm */
3618 set_irn_pinned(store, op_pin_state_floats);
3620 if (get_Proj_proj(proj) == pn_Store_X_except) {
3621 ir_graph *irg = get_irn_irg(proj);
3622 DBG_OPT_EXC_REM(proj);
3623 return new_r_Bad(irg, mode_X);
3625 ir_node *blk = get_nodes_block(store);
3626 return new_r_Jmp(blk);
3632 } /* transform_node_Proj_Store */
3635 * Transform a Proj(Div) with a non-zero value.
3636 * Removes the exceptions and routes the memory to the NoMem node.
3638 static ir_node *transform_node_Proj_Div(ir_node *proj)
3640 ir_node *div = get_Proj_pred(proj);
3641 ir_node *b = get_Div_right(div);
3642 ir_node *res, *new_mem;
3643 const ir_node *confirm;
3646 if (value_not_zero(b, &confirm)) {
3647 /* div(x, y) && y != 0 */
3648 if (confirm == NULL) {
3649 /* we are sure we have a Const != 0 */
3650 new_mem = get_Div_mem(div);
3651 new_mem = skip_Pin(new_mem);
3652 set_Div_mem(div, new_mem);
3653 set_irn_pinned(div, op_pin_state_floats);
3656 proj_nr = get_Proj_proj(proj);
3658 case pn_Div_X_regular:
3659 return new_r_Jmp(get_nodes_block(div));
3661 case pn_Div_X_except: {
3662 ir_graph *irg = get_irn_irg(proj);
3663 /* we found an exception handler, remove it */
3664 DBG_OPT_EXC_REM(proj);
3665 return new_r_Bad(irg, mode_X);
3669 ir_graph *irg = get_irn_irg(proj);
3670 res = get_Div_mem(div);
3671 new_mem = get_irg_no_mem(irg);
3674 /* This node can only float up to the Confirm block */
3675 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3677 set_irn_pinned(div, op_pin_state_floats);
3678 /* this is a Div without exception, we can remove the memory edge */
3679 set_Div_mem(div, new_mem);
3685 } /* transform_node_Proj_Div */
3688 * Transform a Proj(Mod) with a non-zero value.
3689 * Removes the exceptions and routes the memory to the NoMem node.
3691 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3693 ir_node *mod = get_Proj_pred(proj);
3694 ir_node *b = get_Mod_right(mod);
3695 ir_node *res, *new_mem;
3696 const ir_node *confirm;
3699 if (value_not_zero(b, &confirm)) {
3700 /* mod(x, y) && y != 0 */
3701 proj_nr = get_Proj_proj(proj);
3703 if (confirm == NULL) {
3704 /* we are sure we have a Const != 0 */
3705 new_mem = get_Mod_mem(mod);
3706 new_mem = skip_Pin(new_mem);
3707 set_Mod_mem(mod, new_mem);
3708 set_irn_pinned(mod, op_pin_state_floats);
3713 case pn_Mod_X_regular:
3714 return new_r_Jmp(get_irn_n(mod, -1));
3716 case pn_Mod_X_except: {
3717 ir_graph *irg = get_irn_irg(proj);
3718 /* we found an exception handler, remove it */
3719 DBG_OPT_EXC_REM(proj);
3720 return new_r_Bad(irg, mode_X);
3724 ir_graph *irg = get_irn_irg(proj);
3725 res = get_Mod_mem(mod);
3726 new_mem = get_irg_no_mem(irg);
3729 /* This node can only float up to the Confirm block */
3730 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3732 /* this is a Mod without exception, we can remove the memory edge */
3733 set_Mod_mem(mod, new_mem);
3737 if (get_Mod_left(mod) == b) {
3738 /* a % a = 0 if a != 0 */
3739 ir_graph *irg = get_irn_irg(proj);
3740 ir_mode *mode = get_irn_mode(proj);
3741 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3743 DBG_OPT_CSTEVAL(mod, res);
3749 } /* transform_node_Proj_Mod */
3752 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3754 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3756 ir_node *n = get_Proj_pred(proj);
3757 ir_node *b = get_Cond_selector(n);
3759 if (mode_is_int(get_irn_mode(b))) {
3760 ir_tarval *tb = value_of(b);
3762 if (tb != tarval_bad) {
3763 /* we have a constant switch */
3764 long num = get_Proj_proj(proj);
3766 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3767 if (get_tarval_long(tb) == num) {
3768 /* Do NOT create a jump here, or we will have 2 control flow ops
3769 * in a block. This case is optimized away in optimize_cf(). */
3772 ir_graph *irg = get_irn_irg(proj);
3773 /* this case will NEVER be taken, kill it */
3774 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3775 return new_r_Bad(irg, mode_X);
3779 long num = get_Proj_proj(proj);
3780 vrp_attr *b_vrp = vrp_get_info(b);
3781 if (num != get_Cond_default_proj(n) && b_vrp) {
3782 /* Try handling with vrp data. We only remove dead parts. */
3783 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3785 if (b_vrp->range_type == VRP_RANGE) {
3786 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3787 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3789 if ((cmp_result & ir_relation_greater) == cmp_result
3790 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3791 ir_graph *irg = get_irn_irg(proj);
3792 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3793 return new_r_Bad(irg, mode_X);
3795 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3796 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3797 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3799 if ((cmp_result & ir_relation_less_equal) == cmp_result
3800 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3801 ir_graph *irg = get_irn_irg(proj);
3802 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3803 return new_r_Bad(irg, mode_X);
3808 tarval_and( b_vrp->bits_set, tp),
3810 ) == ir_relation_equal)) {
3811 ir_graph *irg = get_irn_irg(proj);
3812 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3813 return new_r_Bad(irg, mode_X);
3819 tarval_not(b_vrp->bits_not_set)),
3820 tarval_not(b_vrp->bits_not_set))
3821 == ir_relation_equal)) {
3822 ir_graph *irg = get_irn_irg(proj);
3823 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3824 return new_r_Bad(irg, mode_X);
3833 * return true if the operation returns a value with exactly 1 bit set
3835 static bool is_single_bit(const ir_node *node)
3837 /* a first implementation, could be extended with vrp and others... */
3839 ir_node *shl_l = get_Shl_left(node);
3840 ir_mode *mode = get_irn_mode(node);
3841 int modulo = get_mode_modulo_shift(mode);
3842 /* this works if we shift a 1 and we have modulo shift */
3843 if (is_Const(shl_l) && is_Const_one(shl_l)
3844 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3847 } else if (is_Const(node)) {
3848 ir_tarval *tv = get_Const_tarval(node);
3849 return tarval_is_single_bit(tv);
3855 * Normalizes and optimizes Cmp nodes.
3857 static ir_node *transform_node_Cmp(ir_node *n)
3859 ir_node *left = get_Cmp_left(n);
3860 ir_node *right = get_Cmp_right(n);
3861 ir_mode *mode = get_irn_mode(left);
3862 ir_tarval *tv = NULL;
3863 bool changed = false;
3864 bool changedc = false;
3865 ir_relation relation = get_Cmp_relation(n);
3866 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3868 /* mask out impossible relations */
3869 ir_relation new_relation = relation & possible;
3870 if (new_relation != relation) {
3871 relation = new_relation;
3875 /* Remove unnecessary conversions */
3876 if (is_Conv(left) && is_Conv(right)) {
3877 ir_node *op_left = get_Conv_op(left);
3878 ir_node *op_right = get_Conv_op(right);
3879 ir_mode *mode_left = get_irn_mode(op_left);
3880 ir_mode *mode_right = get_irn_mode(op_right);
3882 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3883 && mode_left != mode_b && mode_right != mode_b) {
3884 ir_node *block = get_nodes_block(n);
3886 if (mode_left == mode_right) {
3890 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3891 } else if (smaller_mode(mode_left, mode_right)) {
3892 left = new_r_Conv(block, op_left, mode_right);
3895 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3896 } else if (smaller_mode(mode_right, mode_left)) {
3898 right = new_r_Conv(block, op_right, mode_left);
3900 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3902 mode = get_irn_mode(left);
3905 if (is_Conv(left) && is_Const(right)) {
3906 ir_node *op_left = get_Conv_op(left);
3907 ir_mode *mode_left = get_irn_mode(op_left);
3908 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3909 ir_tarval *tv = get_Const_tarval(right);
3910 tarval_int_overflow_mode_t last_mode
3911 = tarval_get_integer_overflow_mode();
3913 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3914 new_tv = tarval_convert_to(tv, mode_left);
3915 tarval_set_integer_overflow_mode(last_mode);
3916 if (new_tv != tarval_bad) {
3917 ir_graph *irg = get_irn_irg(n);
3919 right = new_r_Const(irg, new_tv);
3920 mode = get_irn_mode(left);
3922 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3928 * Optimize -a CMP -b into b CMP a.
3929 * This works only for modes where unary Minus cannot Overflow.
3930 * Note that two-complement integers can Overflow so it will NOT work.
3932 if (!mode_overflow_on_unary_Minus(mode) &&
3933 is_Minus(left) && is_Minus(right)) {
3934 left = get_Minus_op(left);
3935 right = get_Minus_op(right);
3936 relation = get_inversed_relation(relation);
3938 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3941 /* remove operation on both sides if possible */
3942 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3944 * The following operations are NOT safe for floating point operations, for instance
3945 * 1.0 + inf == 2.0 + inf, =/=> x == y
3947 if (mode_is_int(mode)) {
3948 unsigned lop = get_irn_opcode(left);
3950 if (lop == get_irn_opcode(right)) {
3951 ir_node *ll, *lr, *rl, *rr;
3953 /* same operation on both sides, try to remove */
3957 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3958 left = get_unop_op(left);
3959 right = get_unop_op(right);
3961 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3964 ll = get_Add_left(left);
3965 lr = get_Add_right(left);
3966 rl = get_Add_left(right);
3967 rr = get_Add_right(right);
3970 /* X + a CMP X + b ==> a CMP b */
3974 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3975 } else if (ll == rr) {
3976 /* X + a CMP b + X ==> a CMP b */
3980 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3981 } else if (lr == rl) {
3982 /* a + X CMP X + b ==> a CMP b */
3986 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3987 } else if (lr == rr) {
3988 /* a + X CMP b + X ==> a CMP b */
3992 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3996 ll = get_Sub_left(left);
3997 lr = get_Sub_right(left);
3998 rl = get_Sub_left(right);
3999 rr = get_Sub_right(right);
4002 /* X - a CMP X - b ==> a CMP b */
4006 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4007 } else if (lr == rr) {
4008 /* a - X CMP b - X ==> a CMP b */
4012 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4016 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4017 /* a ROTL X CMP b ROTL X ==> a CMP b */
4018 left = get_Rotl_left(left);
4019 right = get_Rotl_left(right);
4021 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4029 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4030 if (is_Add(left) || is_Sub(left)) {
4031 ir_node *ll = get_binop_left(left);
4032 ir_node *lr = get_binop_right(left);
4034 if (lr == right && is_Add(left)) {
4040 ir_graph *irg = get_irn_irg(n);
4042 right = create_zero_const(irg, mode);
4044 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4047 if (is_Add(right) || is_Sub(right)) {
4048 ir_node *rl = get_binop_left(right);
4049 ir_node *rr = get_binop_right(right);
4051 if (rr == left && is_Add(right)) {
4057 ir_graph *irg = get_irn_irg(n);
4059 right = create_zero_const(irg, mode);
4061 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4065 if (is_And(left) && is_Const(right)) {
4066 ir_node *ll = get_binop_left(left);
4067 ir_node *lr = get_binop_right(left);
4068 if (is_Shr(ll) && is_Const(lr)) {
4069 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4070 ir_node *block = get_nodes_block(n);
4071 ir_mode *mode = get_irn_mode(left);
4073 ir_node *llr = get_Shr_right(ll);
4074 if (is_Const(llr)) {
4075 dbg_info *dbg = get_irn_dbg_info(left);
4076 ir_graph *irg = get_irn_irg(left);
4078 ir_tarval *c1 = get_Const_tarval(llr);
4079 ir_tarval *c2 = get_Const_tarval(lr);
4080 ir_tarval *c3 = get_Const_tarval(right);
4081 ir_tarval *mask = tarval_shl(c2, c1);
4082 ir_tarval *value = tarval_shl(c3, c1);
4084 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4085 right = new_r_Const(irg, value);
4090 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4092 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4093 right = get_Eor_right(left);
4094 left = get_Eor_left(left);
4097 } /* mode_is_int(...) */
4100 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4101 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4102 if (mode_is_int(mode) && is_And(left)
4103 && (relation == ir_relation_equal
4104 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4105 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4106 ir_node *and0 = get_And_left(left);
4107 ir_node *and1 = get_And_right(left);
4108 if (and1 == right) {
4109 ir_node *tmp = and0;
4113 if (and0 == right && is_single_bit(and0)) {
4114 ir_graph *irg = get_irn_irg(n);
4116 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4117 right = create_zero_const(irg, mode);
4122 /* replace mode_b compares with ands/ors */
4123 if (mode == mode_b) {
4124 ir_node *block = get_nodes_block(n);
4128 case ir_relation_less_equal:
4129 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4131 case ir_relation_less:
4132 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4134 case ir_relation_greater_equal:
4135 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4137 case ir_relation_greater:
4138 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4140 case ir_relation_less_greater:
4141 bres = new_r_Eor(block, left, right, mode_b);
4143 case ir_relation_equal:
4144 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4147 #ifdef DEBUG_libfirm
4148 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4153 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4159 * First step: normalize the compare op
4160 * by placing the constant on the right side
4161 * or moving the lower address node to the left.
4163 if (!operands_are_normalized(left, right)) {
4168 relation = get_inversed_relation(relation);
4173 * Second step: Try to reduce the magnitude
4174 * of a constant. This may help to generate better code
4175 * later and may help to normalize more compares.
4176 * Of course this is only possible for integer values.
4178 tv = value_of(right);
4179 if (tv != tarval_bad) {
4180 ir_mode *mode = get_irn_mode(right);
4182 /* TODO extend to arbitrary constants */
4183 if (is_Conv(left) && tarval_is_null(tv)) {
4184 ir_node *op = get_Conv_op(left);
4185 ir_mode *op_mode = get_irn_mode(op);
4188 * UpConv(x) REL 0 ==> x REL 0
4189 * Don't do this for float values as it's unclear whether it is a
4190 * win. (on the other side it makes detection/creation of fabs hard)
4192 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4193 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4194 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4195 !mode_is_float(mode)) {
4196 tv = get_mode_null(op_mode);
4200 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4204 if (tv != tarval_bad) {
4205 /* the following optimization is possible on modes without Overflow
4206 * on Unary Minus or on == and !=:
4207 * -a CMP c ==> a swap(CMP) -c
4209 * Beware: for two-complement Overflow may occur, so only == and != can
4210 * be optimized, see this:
4211 * -MININT < 0 =/=> MININT > 0 !!!
4213 if (is_Minus(left) &&
4214 (!mode_overflow_on_unary_Minus(mode) ||
4215 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4216 tv = tarval_neg(tv);
4218 if (tv != tarval_bad) {
4219 left = get_Minus_op(left);
4220 relation = get_inversed_relation(relation);
4222 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4224 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4225 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4226 tv = tarval_not(tv);
4228 if (tv != tarval_bad) {
4229 left = get_Not_op(left);
4231 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4235 /* for integer modes, we have more */
4236 if (mode_is_int(mode) && !is_Const(left)) {
4237 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4238 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4239 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4240 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4242 if (tv != tarval_bad) {
4243 relation ^= ir_relation_equal;
4245 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4248 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4249 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4250 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4251 tv = tarval_add(tv, get_mode_one(mode));
4253 if (tv != tarval_bad) {
4254 relation ^= ir_relation_equal;
4256 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4260 /* the following reassociations work only for == and != */
4261 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4262 if (tv != tarval_bad) {
4263 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4265 ir_node *c1 = get_Sub_right(left);
4266 ir_tarval *tv2 = value_of(c1);
4268 if (tv2 != tarval_bad) {
4269 tv2 = tarval_add(tv, value_of(c1));
4271 if (tv2 != tarval_bad) {
4272 left = get_Sub_left(left);
4275 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4279 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4280 else if (is_Add(left)) {
4281 ir_node *a_l = get_Add_left(left);
4282 ir_node *a_r = get_Add_right(left);
4286 if (is_Const(a_l)) {
4288 tv2 = value_of(a_l);
4291 tv2 = value_of(a_r);
4294 if (tv2 != tarval_bad) {
4295 tv2 = tarval_sub(tv, tv2, NULL);
4297 if (tv2 != tarval_bad) {
4301 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4305 /* -a == c ==> a == -c, -a != c ==> a != -c */
4306 else if (is_Minus(left)) {
4307 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4309 if (tv2 != tarval_bad) {
4310 left = get_Minus_op(left);
4313 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4320 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4321 switch (get_irn_opcode(left)) {
4325 c1 = get_And_right(left);
4328 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4329 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4331 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4333 /* TODO: move to constant evaluation */
4334 ir_graph *irg = get_irn_irg(n);
4335 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4336 c1 = new_r_Const(irg, tv);
4337 DBG_OPT_CSTEVAL(n, c1);
4341 if (tarval_is_single_bit(tv)) {
4343 * optimization for AND:
4345 * And(x, C) == C ==> And(x, C) != 0
4346 * And(x, C) != C ==> And(X, C) == 0
4348 * if C is a single Bit constant.
4351 /* check for Constant's match. We have check hare the tarvals,
4352 because our const might be changed */
4353 if (get_Const_tarval(c1) == tv) {
4354 /* fine: do the transformation */
4355 tv = get_mode_null(get_tarval_mode(tv));
4356 relation ^= ir_relation_less_equal_greater;
4358 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4364 c1 = get_Or_right(left);
4365 if (is_Const(c1) && tarval_is_null(tv)) {
4367 * Or(x, C) == 0 && C != 0 ==> FALSE
4368 * Or(x, C) != 0 && C != 0 ==> TRUE
4370 if (! tarval_is_null(get_Const_tarval(c1))) {
4371 /* TODO: move to constant evaluation */
4372 ir_graph *irg = get_irn_irg(n);
4373 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4374 c1 = new_r_Const(irg, tv);
4375 DBG_OPT_CSTEVAL(n, c1);
4382 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4384 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4387 c1 = get_Shl_right(left);
4389 ir_graph *irg = get_irn_irg(c1);
4390 ir_tarval *tv1 = get_Const_tarval(c1);
4391 ir_mode *mode = get_irn_mode(left);
4392 ir_tarval *minus1 = get_mode_all_one(mode);
4393 ir_tarval *amask = tarval_shr(minus1, tv1);
4394 ir_tarval *cmask = tarval_shl(minus1, tv1);
4397 if (tarval_and(tv, cmask) != tv) {
4398 /* condition not met */
4399 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4400 c1 = new_r_Const(irg, tv);
4401 DBG_OPT_CSTEVAL(n, c1);
4404 sl = get_Shl_left(left);
4405 blk = get_nodes_block(n);
4406 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4407 tv = tarval_shr(tv, tv1);
4409 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4414 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4416 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4419 c1 = get_Shr_right(left);
4421 ir_graph *irg = get_irn_irg(c1);
4422 ir_tarval *tv1 = get_Const_tarval(c1);
4423 ir_mode *mode = get_irn_mode(left);
4424 ir_tarval *minus1 = get_mode_all_one(mode);
4425 ir_tarval *amask = tarval_shl(minus1, tv1);
4426 ir_tarval *cmask = tarval_shr(minus1, tv1);
4429 if (tarval_and(tv, cmask) != tv) {
4430 /* condition not met */
4431 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4432 c1 = new_r_Const(irg, tv);
4433 DBG_OPT_CSTEVAL(n, c1);
4436 sl = get_Shr_left(left);
4437 blk = get_nodes_block(n);
4438 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4439 tv = tarval_shl(tv, tv1);
4441 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4446 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4448 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4451 c1 = get_Shrs_right(left);
4453 ir_graph *irg = get_irn_irg(c1);
4454 ir_tarval *tv1 = get_Const_tarval(c1);
4455 ir_mode *mode = get_irn_mode(left);
4456 ir_tarval *minus1 = get_mode_all_one(mode);
4457 ir_tarval *amask = tarval_shl(minus1, tv1);
4458 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4461 cond = tarval_sub(cond, tv1, NULL);
4462 cond = tarval_shrs(tv, cond);
4464 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4465 /* condition not met */
4466 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4467 c1 = new_r_Const(irg, tv);
4468 DBG_OPT_CSTEVAL(n, c1);
4471 sl = get_Shrs_left(left);
4472 blk = get_nodes_block(n);
4473 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4474 tv = tarval_shl(tv, tv1);
4476 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4481 } /* tarval != bad */
4484 if (changedc) { /* need a new Const */
4485 ir_graph *irg = get_irn_irg(n);
4486 right = new_r_Const(irg, tv);
4490 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4491 ir_node *op = get_Proj_pred(left);
4493 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4494 ir_node *c = get_binop_right(op);
4497 ir_tarval *tv = get_Const_tarval(c);
4499 if (tarval_is_single_bit(tv)) {
4500 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4501 ir_node *v = get_binop_left(op);
4502 ir_node *blk = get_irn_n(op, -1);
4503 ir_graph *irg = get_irn_irg(op);
4504 ir_mode *mode = get_irn_mode(v);
4506 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4507 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4509 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4516 dbg_info *dbgi = get_irn_dbg_info(n);
4517 ir_node *block = get_nodes_block(n);
4519 /* create a new compare */
4520 n = new_rd_Cmp(dbgi, block, left, right, relation);
4527 * Optimize CopyB(mem, x, x) into a Nop.
4529 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4531 ir_node *copyb = get_Proj_pred(proj);
4532 ir_node *a = get_CopyB_dst(copyb);
4533 ir_node *b = get_CopyB_src(copyb);
4536 switch (get_Proj_proj(proj)) {
4537 case pn_CopyB_X_regular:
4538 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4539 DBG_OPT_EXC_REM(proj);
4540 proj = new_r_Jmp(get_nodes_block(copyb));
4542 case pn_CopyB_X_except: {
4543 ir_graph *irg = get_irn_irg(proj);
4544 DBG_OPT_EXC_REM(proj);
4545 proj = new_r_Bad(irg, mode_X);
4553 } /* transform_node_Proj_CopyB */
4556 * Optimize Bounds(idx, idx, upper) into idx.
4558 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4560 ir_node *oldn = proj;
4561 ir_node *bound = get_Proj_pred(proj);
4562 ir_node *idx = get_Bound_index(bound);
4563 ir_node *pred = skip_Proj(idx);
4566 if (idx == get_Bound_lower(bound))
4568 else if (is_Bound(pred)) {
4570 * idx was Bounds checked previously, it is still valid if
4571 * lower <= pred_lower && pred_upper <= upper.
4573 ir_node *lower = get_Bound_lower(bound);
4574 ir_node *upper = get_Bound_upper(bound);
4575 if (get_Bound_lower(pred) == lower &&
4576 get_Bound_upper(pred) == upper) {
4578 * One could expect that we simply return the previous
4579 * Bound here. However, this would be wrong, as we could
4580 * add an exception Proj to a new location then.
4581 * So, we must turn in into a tuple.
4587 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4588 switch (get_Proj_proj(proj)) {
4590 DBG_OPT_EXC_REM(proj);
4591 proj = get_Bound_mem(bound);
4593 case pn_Bound_X_except:
4594 DBG_OPT_EXC_REM(proj);
4595 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4599 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4601 case pn_Bound_X_regular:
4602 DBG_OPT_EXC_REM(proj);
4603 proj = new_r_Jmp(get_nodes_block(bound));
4610 } /* transform_node_Proj_Bound */
4613 * Does all optimizations on nodes that must be done on its Projs
4614 * because of creating new nodes.
4616 static ir_node *transform_node_Proj(ir_node *proj)
4618 ir_node *n = get_Proj_pred(proj);
4620 if (n->op->ops.transform_node_Proj)
4621 return n->op->ops.transform_node_Proj(proj);
4623 } /* transform_node_Proj */
4626 * Test wether a block is unreachable
4627 * Note: That this only returns true when
4628 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
4629 * This is important, as you easily end up producing invalid constructs in the
4630 * unreachable code when optimizing away edges into the unreachable code.
4631 * So only set this flag when you iterate localopts to the fixpoint.
4632 * When you reach the fixpoint then all unreachable code is dead
4633 * (= can't be reached by firm edges) and you won't see the invalid constructs
4636 static bool is_block_unreachable(const ir_node *block)
4638 const ir_graph *irg = get_irn_irg(block);
4639 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4641 return get_Block_dom_depth(block) < 0;
4644 static ir_node *transform_node_Block(ir_node *block)
4646 ir_graph *irg = get_irn_irg(block);
4647 int arity = get_irn_arity(block);
4648 ir_node *bad = NULL;
4651 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4654 for (i = 0; i < arity; ++i) {
4655 ir_node *const pred = get_Block_cfgpred(block, i);
4656 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4659 bad = new_r_Bad(irg, mode_X);
4660 set_irn_n(block, i, bad);
4666 static ir_node *transform_node_Phi(ir_node *phi)
4668 int n = get_irn_arity(phi);
4669 ir_mode *mode = get_irn_mode(phi);
4670 ir_node *block = get_nodes_block(phi);
4671 ir_graph *irg = get_irn_irg(phi);
4672 ir_node *bad = NULL;
4675 /* Set phi-operands for bad-block inputs to bad */
4676 for (i = 0; i < n; ++i) {
4677 if (!is_Bad(get_Phi_pred(phi, i))) {
4678 ir_node *pred = get_Block_cfgpred(block, i);
4679 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4681 bad = new_r_Bad(irg, mode);
4682 set_irn_n(phi, i, bad);
4687 /* Move Pin nodes down through Phi nodes. */
4688 if (mode == mode_M) {
4689 n = get_irn_arity(phi);
4691 /* Beware of Phi0 */
4695 bool has_pin = false;
4697 NEW_ARR_A(ir_node *, in, n);
4699 for (i = 0; i < n; ++i) {
4700 ir_node *pred = get_irn_n(phi, i);
4703 in[i] = get_Pin_op(pred);
4705 } else if (is_Bad(pred)) {
4715 /* Move the Pin nodes "behind" the Phi. */
4716 block = get_irn_n(phi, -1);
4717 new_phi = new_r_Phi(block, n, in, mode_M);
4718 return new_r_Pin(block, new_phi);
4721 /* Move Confirms down through Phi nodes. */
4722 else if (mode_is_reference(mode)) {
4723 n = get_irn_arity(phi);
4725 /* Beware of Phi0 */
4727 ir_node *pred = get_irn_n(phi, 0);
4728 ir_node *bound, *new_phi, *block, **in;
4729 ir_relation relation;
4730 bool has_confirm = false;
4732 if (! is_Confirm(pred))
4735 bound = get_Confirm_bound(pred);
4736 relation = get_Confirm_relation(pred);
4738 NEW_ARR_A(ir_node *, in, n);
4739 in[0] = get_Confirm_value(pred);
4741 for (i = 1; i < n; ++i) {
4742 pred = get_irn_n(phi, i);
4744 if (is_Confirm(pred) &&
4745 get_Confirm_bound(pred) == bound &&
4746 get_Confirm_relation(pred) == relation) {
4747 in[i] = get_Confirm_value(pred);
4749 } else if (is_Bad(pred)) {
4759 /* move the Confirm nodes "behind" the Phi */
4760 block = get_irn_n(phi, -1);
4761 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4762 return new_r_Confirm(block, new_phi, bound, relation);
4769 * Returns the operands of a commutative bin-op, if one operand is
4770 * a const, it is returned as the second one.
4772 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4774 ir_node *op_a = get_binop_left(binop);
4775 ir_node *op_b = get_binop_right(binop);
4777 assert(is_op_commutative(get_irn_op(binop)));
4779 if (is_Const(op_a)) {
4786 } /* get_comm_Binop_Ops */
4789 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4790 * Such pattern may arise in bitfield stores.
4792 * value c4 value c4 & c2
4793 * AND c3 AND c1 | c3
4800 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4803 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4805 ir_node *irn_and, *c1;
4807 ir_node *and_l, *c3;
4808 ir_node *value, *c4;
4809 ir_node *new_and, *new_const, *block;
4810 ir_mode *mode = get_irn_mode(irn_or);
4812 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4816 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4817 if (!is_Const(c1) || !is_And(irn_and))
4820 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4824 tv1 = get_Const_tarval(c1);
4825 tv2 = get_Const_tarval(c2);
4827 tv = tarval_or(tv1, tv2);
4828 if (tarval_is_all_one(tv)) {
4829 /* the AND does NOT clear a bit with isn't set by the OR */
4830 set_Or_left(irn_or, or_l);
4831 set_Or_right(irn_or, c1);
4833 /* check for more */
4840 get_comm_Binop_Ops(or_l, &and_l, &c3);
4841 if (!is_Const(c3) || !is_And(and_l))
4844 get_comm_Binop_Ops(and_l, &value, &c4);
4848 /* ok, found the pattern, check for conditions */
4849 assert(mode == get_irn_mode(irn_and));
4850 assert(mode == get_irn_mode(or_l));
4851 assert(mode == get_irn_mode(and_l));
4853 tv3 = get_Const_tarval(c3);
4854 tv4 = get_Const_tarval(c4);
4856 tv = tarval_or(tv4, tv2);
4857 if (!tarval_is_all_one(tv)) {
4858 /* have at least one 0 at the same bit position */
4862 if (tv3 != tarval_andnot(tv3, tv4)) {
4863 /* bit in the or_mask is outside the and_mask */
4867 if (tv1 != tarval_andnot(tv1, tv2)) {
4868 /* bit in the or_mask is outside the and_mask */
4872 /* ok, all conditions met */
4873 block = get_irn_n(irn_or, -1);
4874 irg = get_irn_irg(block);
4876 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4878 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4880 set_Or_left(irn_or, new_and);
4881 set_Or_right(irn_or, new_const);
4883 /* check for more */
4885 } /* transform_node_Or_bf_store */
4888 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4890 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4892 ir_mode *mode = get_irn_mode(irn_or);
4893 ir_node *shl, *shr, *block;
4894 ir_node *irn, *x, *c1, *c2, *n;
4895 ir_tarval *tv1, *tv2;
4897 /* some backends can't handle rotl */
4898 if (!be_get_backend_param()->support_rotl)
4901 if (! mode_is_int(mode))
4904 shl = get_binop_left(irn_or);
4905 shr = get_binop_right(irn_or);
4914 } else if (!is_Shl(shl)) {
4916 } else if (!is_Shr(shr)) {
4919 x = get_Shl_left(shl);
4920 if (x != get_Shr_left(shr))
4923 c1 = get_Shl_right(shl);
4924 c2 = get_Shr_right(shr);
4925 if (is_Const(c1) && is_Const(c2)) {
4926 tv1 = get_Const_tarval(c1);
4927 if (! tarval_is_long(tv1))
4930 tv2 = get_Const_tarval(c2);
4931 if (! tarval_is_long(tv2))
4934 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4935 != (int) get_mode_size_bits(mode))
4938 /* yet, condition met */
4939 block = get_nodes_block(irn_or);
4941 n = new_r_Rotl(block, x, c1, mode);
4943 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4947 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4948 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4949 if (!ir_is_negated_value(c1, c2)) {
4953 /* yet, condition met */
4954 block = get_nodes_block(irn_or);
4955 n = new_r_Rotl(block, x, c1, mode);
4956 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4958 } /* transform_node_Or_Rotl */
4960 static bool is_cmp_unequal(const ir_node *node)
4962 ir_relation relation = get_Cmp_relation(node);
4963 ir_node *left = get_Cmp_left(node);
4964 ir_node *right = get_Cmp_right(node);
4965 ir_mode *mode = get_irn_mode(left);
4967 if (relation == ir_relation_less_greater)
4970 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
4971 return relation == ir_relation_greater;
4976 * returns true for Cmp(x == 0) or Cmp(x != 0)
4978 static bool is_cmp_equality_zero(const ir_node *node)
4980 ir_relation relation;
4981 ir_node *right = get_Cmp_right(node);
4983 if (!is_Const(right) || !is_Const_null(right))
4985 relation = get_Cmp_relation(node);
4986 return relation == ir_relation_equal
4987 || relation == ir_relation_less_greater
4988 || (!mode_is_signed(get_irn_mode(right))
4989 && relation == ir_relation_greater);
4995 static ir_node *transform_node_Or(ir_node *n)
4997 ir_node *c, *oldn = n;
4998 ir_node *a = get_Or_left(n);
4999 ir_node *b = get_Or_right(n);
5002 if (is_Not(a) && is_Not(b)) {
5003 /* ~a | ~b = ~(a&b) */
5004 ir_node *block = get_nodes_block(n);
5006 mode = get_irn_mode(n);
5009 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5010 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5011 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5015 /* we can combine the relations of two compares with the same operands */
5016 if (is_Cmp(a) && is_Cmp(b)) {
5017 ir_node *a_left = get_Cmp_left(a);
5018 ir_node *a_right = get_Cmp_right(a);
5019 ir_node *b_left = get_Cmp_left(b);
5020 ir_node *b_right = get_Cmp_right(b);
5021 if (a_left == b_left && b_left == b_right) {
5022 dbg_info *dbgi = get_irn_dbg_info(n);
5023 ir_node *block = get_nodes_block(n);
5024 ir_relation a_relation = get_Cmp_relation(a);
5025 ir_relation b_relation = get_Cmp_relation(b);
5026 ir_relation new_relation = a_relation | b_relation;
5027 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
5029 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
5030 if (is_cmp_unequal(a) && is_cmp_unequal(b)
5031 && !mode_is_float(get_irn_mode(a_left))
5032 && !mode_is_float(get_irn_mode(b_left))) {
5033 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
5034 ir_graph *irg = get_irn_irg(n);
5035 dbg_info *dbgi = get_irn_dbg_info(n);
5036 ir_node *block = get_nodes_block(n);
5037 ir_mode *a_mode = get_irn_mode(a_left);
5038 ir_mode *b_mode = get_irn_mode(b_left);
5039 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5040 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5041 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
5042 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
5043 ir_node *zero = create_zero_const(irg, b_mode);
5044 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5046 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
5047 ir_graph *irg = get_irn_irg(n);
5048 dbg_info *dbgi = get_irn_dbg_info(n);
5049 ir_node *block = get_nodes_block(n);
5050 ir_mode *a_mode = get_irn_mode(a_left);
5051 ir_mode *b_mode = get_irn_mode(b_left);
5052 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5053 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5054 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
5055 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
5056 ir_node *zero = create_zero_const(irg, a_mode);
5057 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5062 mode = get_irn_mode(n);
5063 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5065 n = transform_node_Or_bf_store(n);
5066 n = transform_node_Or_Rotl(n);
5070 n = transform_bitwise_distributive(n, transform_node_Or);
5072 n = transform_node_bitop_shift(n);
5075 } /* transform_node_Or */
5079 static ir_node *transform_node(ir_node *n);
5082 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5084 * Should be moved to reassociation?
5086 static ir_node *transform_node_shift(ir_node *n)
5088 ir_node *left, *right;
5090 ir_mode *count_mode;
5091 ir_tarval *tv1, *tv2, *res;
5092 ir_node *in[2], *irn, *block;
5096 left = get_binop_left(n);
5098 /* different operations */
5099 if (get_irn_op(left) != get_irn_op(n))
5102 right = get_binop_right(n);
5103 tv1 = value_of(right);
5104 if (tv1 == tarval_bad)
5107 tv2 = value_of(get_binop_right(left));
5108 if (tv2 == tarval_bad)
5111 count_mode = get_tarval_mode(tv1);
5112 if (get_tarval_mode(tv2) != count_mode) {
5113 /* TODO: search bigger mode or something and convert... */
5117 mode = get_irn_mode(n);
5118 modulo_shf = get_mode_modulo_shift(mode);
5120 if (modulo_shf > 0) {
5121 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5123 /* I'm not so sure what happens in one complement... */
5124 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5125 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5126 * above will be invalid) */
5127 assert(modulo_shf<=0 || is_po2(modulo_shf));
5129 tv1 = tarval_and(tv1, modulo_mask);
5130 tv2 = tarval_and(tv2, modulo_mask);
5132 res = tarval_add(tv1, tv2);
5133 irg = get_irn_irg(n);
5135 /* beware: a simple replacement works only, if res < modulo shift */
5137 int bits = get_mode_size_bits(mode);
5138 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5139 res = tarval_mod(res, modulo);
5141 long bits = get_mode_size_bits(mode);
5142 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5144 /* shifting too much */
5145 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5147 ir_node *block = get_nodes_block(n);
5148 dbg_info *dbgi = get_irn_dbg_info(n);
5149 ir_mode *smode = get_irn_mode(right);
5150 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5151 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5154 return new_r_Const(irg, get_mode_null(mode));
5158 /* ok, we can replace it */
5159 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5160 block = get_nodes_block(n);
5162 in[0] = get_binop_left(left);
5163 in[1] = new_r_Const(irg, res);
5165 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5167 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5169 return transform_node(irn);
5174 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5176 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5177 * (also with x >>s c1 when c1>=c2)
5179 static ir_node *transform_node_shl_shr(ir_node *n)
5182 ir_node *right = get_binop_right(n);
5192 ir_tarval *tv_shift;
5195 ir_relation relation;
5198 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5200 if (!is_Const(right))
5203 left = get_binop_left(n);
5204 mode = get_irn_mode(n);
5205 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5206 ir_node *shr_right = get_binop_right(left);
5208 if (!is_Const(shr_right))
5211 x = get_binop_left(left);
5212 tv_shr = get_Const_tarval(shr_right);
5213 tv_shl = get_Const_tarval(right);
5215 if (is_Shrs(left)) {
5216 /* shrs variant only allowed if c1 >= c2 */
5217 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5220 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5223 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5225 tv_mask = tarval_shl(tv_mask, tv_shl);
5226 } else if (is_Shr(n) && is_Shl(left)) {
5227 ir_node *shl_right = get_Shl_right(left);
5229 if (!is_Const(shl_right))
5232 x = get_Shl_left(left);
5233 tv_shr = get_Const_tarval(right);
5234 tv_shl = get_Const_tarval(shl_right);
5236 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5237 tv_mask = tarval_shr(tv_mask, tv_shr);
5242 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5243 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5246 assert(tv_mask != tarval_bad);
5247 assert(get_tarval_mode(tv_mask) == mode);
5249 block = get_nodes_block(n);
5250 irg = get_irn_irg(block);
5251 dbgi = get_irn_dbg_info(n);
5253 relation = tarval_cmp(tv_shl, tv_shr);
5254 if (relation == ir_relation_less || relation == ir_relation_equal) {
5255 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5256 new_const = new_r_Const(irg, tv_shift);
5258 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5260 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5263 assert(relation == ir_relation_greater);
5264 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5265 new_const = new_r_Const(irg, tv_shift);
5266 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5269 new_const = new_r_Const(irg, tv_mask);
5270 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5275 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5277 ir_mode *mode = get_tarval_mode(tv);
5278 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5279 return tarval_mod(tv, modulo_tv);
5282 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5283 ir_node *left, ir_node *right, ir_mode *mode);
5286 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5287 * then we can use that to minimize the value of Add(x, const) or
5288 * Sub(Const, x). In particular this often avoids 1 instruction in some
5289 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5290 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5292 static ir_node *transform_node_shift_modulo(ir_node *n,
5293 new_shift_func new_shift)
5295 ir_mode *mode = get_irn_mode(n);
5296 int modulo = get_mode_modulo_shift(mode);
5297 ir_node *newop = NULL;
5298 ir_mode *mode_right;
5305 if (get_mode_arithmetic(mode) != irma_twos_complement)
5307 if (!is_po2(modulo))
5310 irg = get_irn_irg(n);
5311 block = get_nodes_block(n);
5312 right = get_binop_right(n);
5313 mode_right = get_irn_mode(right);
5314 if (is_Const(right)) {
5315 ir_tarval *tv = get_Const_tarval(right);
5316 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5321 newop = new_r_Const(irg, tv_mod);
5322 } else if (is_Add(right)) {
5323 ir_node *add_right = get_Add_right(right);
5324 if (is_Const(add_right)) {
5325 ir_tarval *tv = get_Const_tarval(add_right);
5326 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5331 newconst = new_r_Const(irg, tv_mod);
5332 newop = new_r_Add(block, get_Add_left(right), newconst,
5335 } else if (is_Sub(right)) {
5336 ir_node *sub_left = get_Sub_left(right);
5337 if (is_Const(sub_left)) {
5338 ir_tarval *tv = get_Const_tarval(sub_left);
5339 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5344 newconst = new_r_Const(irg, tv_mod);
5345 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5352 if (newop != NULL) {
5353 dbg_info *dbgi = get_irn_dbg_info(n);
5354 ir_node *left = get_binop_left(n);
5355 return new_shift(dbgi, block, left, newop, mode);
5363 static ir_node *transform_node_Shr(ir_node *n)
5365 ir_node *c, *oldn = n;
5366 ir_node *left = get_Shr_left(n);
5367 ir_node *right = get_Shr_right(n);
5368 ir_mode *mode = get_irn_mode(n);
5370 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5371 n = transform_node_shift(n);
5374 n = transform_node_shift_modulo(n, new_rd_Shr);
5376 n = transform_node_shl_shr(n);
5378 n = transform_node_shift_bitop(n);
5381 } /* transform_node_Shr */
5386 static ir_node *transform_node_Shrs(ir_node *n)
5388 ir_node *c, *oldn = n;
5389 ir_node *a = get_Shrs_left(n);
5390 ir_node *b = get_Shrs_right(n);
5391 ir_mode *mode = get_irn_mode(n);
5393 if (is_oversize_shift(n)) {
5394 ir_node *block = get_nodes_block(n);
5395 dbg_info *dbgi = get_irn_dbg_info(n);
5396 ir_mode *cmode = get_irn_mode(b);
5397 long val = get_mode_size_bits(cmode)-1;
5398 ir_graph *irg = get_irn_irg(n);
5399 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5400 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5403 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5404 n = transform_node_shift(n);
5407 n = transform_node_shift_modulo(n, new_rd_Shrs);
5409 n = transform_node_shift_bitop(n);
5412 } /* transform_node_Shrs */
5417 static ir_node *transform_node_Shl(ir_node *n)
5419 ir_node *c, *oldn = n;
5420 ir_node *a = get_Shl_left(n);
5421 ir_node *b = get_Shl_right(n);
5422 ir_mode *mode = get_irn_mode(n);
5424 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5425 n = transform_node_shift(n);
5428 n = transform_node_shift_modulo(n, new_rd_Shl);
5430 n = transform_node_shl_shr(n);
5432 n = transform_node_shift_bitop(n);
5435 } /* transform_node_Shl */
5440 static ir_node *transform_node_Rotl(ir_node *n)
5442 ir_node *c, *oldn = n;
5443 ir_node *a = get_Rotl_left(n);
5444 ir_node *b = get_Rotl_right(n);
5445 ir_mode *mode = get_irn_mode(n);
5447 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5448 n = transform_node_shift(n);
5451 n = transform_node_shift_bitop(n);
5454 } /* transform_node_Rotl */
5459 static ir_node *transform_node_Conv(ir_node *n)
5461 ir_node *c, *oldn = n;
5462 ir_mode *mode = get_irn_mode(n);
5463 ir_node *a = get_Conv_op(n);
5465 if (mode != mode_b && is_const_Phi(a)) {
5466 /* Do NOT optimize mode_b Conv's, this leads to remaining
5467 * Phib nodes later, because the conv_b_lower operation
5468 * is instantly reverted, when it tries to insert a Convb.
5470 c = apply_conv_on_phi(a, mode);
5472 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5477 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5478 ir_graph *irg = get_irn_irg(n);
5479 return new_r_Unknown(irg, mode);
5482 if (mode_is_reference(mode) &&
5483 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5485 ir_node *l = get_Add_left(a);
5486 ir_node *r = get_Add_right(a);
5487 dbg_info *dbgi = get_irn_dbg_info(a);
5488 ir_node *block = get_nodes_block(n);
5490 ir_node *lop = get_Conv_op(l);
5491 if (get_irn_mode(lop) == mode) {
5492 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5493 n = new_rd_Add(dbgi, block, lop, r, mode);
5498 ir_node *rop = get_Conv_op(r);
5499 if (get_irn_mode(rop) == mode) {
5500 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5501 n = new_rd_Add(dbgi, block, l, rop, mode);
5508 } /* transform_node_Conv */
5511 * Remove dead blocks and nodes in dead blocks
5512 * in keep alive list. We do not generate a new End node.
5514 static ir_node *transform_node_End(ir_node *n)
5516 int i, j, n_keepalives = get_End_n_keepalives(n);
5519 NEW_ARR_A(ir_node *, in, n_keepalives);
5521 for (i = j = 0; i < n_keepalives; ++i) {
5522 ir_node *ka = get_End_keepalive(n, i);
5524 /* no need to keep Bad */
5527 /* do not keep unreachable code */
5528 block = is_Block(ka) ? ka : get_nodes_block(ka);
5529 if (is_block_unreachable(block))
5533 if (j != n_keepalives)
5534 set_End_keepalives(n, j, in);
5536 } /* transform_node_End */
5538 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5540 if (is_Minus(a) && get_Minus_op(a) == b)
5542 if (is_Minus(b) && get_Minus_op(b) == a)
5544 if (is_Sub(a) && is_Sub(b)) {
5545 ir_node *a_left = get_Sub_left(a);
5546 ir_node *a_right = get_Sub_right(a);
5547 ir_node *b_left = get_Sub_left(b);
5548 ir_node *b_right = get_Sub_right(b);
5550 if (a_left == b_right && a_right == b_left)
5557 static const ir_node *skip_upconv(const ir_node *node)
5559 while (is_Conv(node)) {
5560 ir_mode *mode = get_irn_mode(node);
5561 const ir_node *op = get_Conv_op(node);
5562 ir_mode *op_mode = get_irn_mode(op);
5563 if (!smaller_mode(op_mode, mode))
5570 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5571 const ir_node *mux_true)
5576 ir_relation relation;
5582 * Note further that these optimization work even for floating point
5583 * with NaN's because -NaN == NaN.
5584 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5587 mode = get_irn_mode(mux_true);
5588 if (mode_honor_signed_zeros(mode))
5591 /* must be <, <=, >=, > */
5592 relation = get_Cmp_relation(sel);
5593 if ((relation & ir_relation_less_greater) == 0)
5596 if (!ir_is_negated_value(mux_true, mux_false))
5599 mux_true = skip_upconv(mux_true);
5600 mux_false = skip_upconv(mux_false);
5602 /* must be x cmp 0 */
5603 cmp_right = get_Cmp_right(sel);
5604 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5607 cmp_left = get_Cmp_left(sel);
5608 if (cmp_left == mux_false) {
5609 if (relation & ir_relation_less) {
5612 assert(relation & ir_relation_greater);
5615 } else if (cmp_left == mux_true) {
5616 if (relation & ir_relation_less) {
5619 assert(relation & ir_relation_greater);
5627 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5630 ir_node *cmp_left = get_Cmp_left(sel);
5631 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5634 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5635 const ir_node *mux_true)
5637 /* this code should return true each time transform_node_Mux would
5638 * optimize the Mux completely away */
5640 ir_mode *mode = get_irn_mode(mux_false);
5641 if (get_mode_arithmetic(mode) == irma_twos_complement
5642 && ir_mux_is_abs(sel, mux_false, mux_true))
5645 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5646 const ir_node *cmp_r = get_Cmp_right(sel);
5647 const ir_node *cmp_l = get_Cmp_left(sel);
5648 const ir_node *f = mux_false;
5649 const ir_node *t = mux_true;
5651 if (is_Const(t) && is_Const_null(t)) {
5656 if (is_And(cmp_l) && f == cmp_r) {
5657 ir_node *and_r = get_And_right(cmp_l);
5660 if (and_r == t && is_single_bit(and_r))
5662 and_l = get_And_left(cmp_l);
5663 if (and_l == t && is_single_bit(and_l))
5672 * Optimize a Mux into some simpler cases.
5674 static ir_node *transform_node_Mux(ir_node *n)
5677 ir_node *sel = get_Mux_sel(n);
5678 ir_mode *mode = get_irn_mode(n);
5679 ir_node *t = get_Mux_true(n);
5680 ir_node *f = get_Mux_false(n);
5681 ir_graph *irg = get_irn_irg(n);
5683 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5684 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5685 int abs = ir_mux_is_abs(sel, f, t);
5687 dbg_info *dbgi = get_irn_dbg_info(n);
5688 ir_node *block = get_nodes_block(n);
5689 ir_node *op = ir_get_abs_op(sel, f, t);
5690 int bits = get_mode_size_bits(mode);
5691 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5692 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5693 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5696 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5698 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5704 /* first normalization step: try to move a constant to the false side,
5705 * 0 preferred on false side too */
5706 if (is_Cmp(sel) && is_Const(t) &&
5707 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5708 dbg_info *seldbgi = get_irn_dbg_info(sel);
5709 ir_node *block = get_nodes_block(sel);
5710 ir_relation relation = get_Cmp_relation(sel);
5715 /* Mux(x, a, b) => Mux(not(x), b, a) */
5716 relation = get_negated_relation(relation);
5717 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5718 get_Cmp_right(sel), relation);
5719 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5722 /* the following optimisations create new mode_b nodes, so only do them
5723 * before mode_b lowering */
5724 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
5726 ir_node* block = get_nodes_block(n);
5728 ir_node* c1 = get_Mux_sel(t);
5729 ir_node* t1 = get_Mux_true(t);
5730 ir_node* f1 = get_Mux_false(t);
5732 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5733 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5734 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5739 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5740 } else if (f == t1) {
5741 /* Mux(cond0, Mux(cond1, x, y), x) */
5742 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5743 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5744 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5749 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5751 } else if (is_Mux(f)) {
5752 ir_node* block = get_nodes_block(n);
5754 ir_node* c1 = get_Mux_sel(f);
5755 ir_node* t1 = get_Mux_true(f);
5756 ir_node* f1 = get_Mux_false(f);
5758 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5759 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5760 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5765 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5766 } else if (t == f1) {
5767 /* Mux(cond0, x, Mux(cond1, y, x)) */
5768 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5769 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5770 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5775 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5779 /* note: after normalization, false can only happen on default */
5780 if (mode == mode_b) {
5781 dbg_info *dbg = get_irn_dbg_info(n);
5782 ir_node *block = get_nodes_block(n);
5785 ir_tarval *tv_t = get_Const_tarval(t);
5786 if (tv_t == tarval_b_true) {
5788 /* Muxb(sel, true, false) = sel */
5789 assert(get_Const_tarval(f) == tarval_b_false);
5790 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5793 /* Muxb(sel, true, x) = Or(sel, x) */
5794 n = new_rd_Or(dbg, block, sel, f, mode_b);
5795 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5799 } else if (is_Const(f)) {
5800 ir_tarval *tv_f = get_Const_tarval(f);
5801 if (tv_f == tarval_b_true) {
5802 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5803 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5804 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5805 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5808 /* Muxb(sel, x, false) = And(sel, x) */
5809 assert(tv_f == tarval_b_false);
5810 n = new_rd_And(dbg, block, sel, t, mode_b);
5811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5817 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5818 * value to integer. */
5819 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5820 ir_tarval *a = get_Const_tarval(t);
5821 ir_tarval *b = get_Const_tarval(f);
5823 if (tarval_is_one(a) && tarval_is_null(b)) {
5824 ir_node *block = get_nodes_block(n);
5825 ir_node *conv = new_r_Conv(block, sel, mode);
5827 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5829 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5830 ir_node *block = get_nodes_block(n);
5831 ir_node *not_ = new_r_Not(block, sel, mode_b);
5832 ir_node *conv = new_r_Conv(block, not_, mode);
5834 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5840 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5841 ir_relation relation = get_Cmp_relation(sel);
5842 ir_node *cmp_r = get_Cmp_right(sel);
5843 ir_node *cmp_l = get_Cmp_left(sel);
5844 ir_node *block = get_nodes_block(n);
5846 if (is_And(cmp_l) && f == cmp_r) {
5847 ir_node *and_r = get_And_right(cmp_l);
5850 if (and_r == t && is_single_bit(and_r)) {
5851 if (relation == ir_relation_equal) {
5852 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5853 n = new_rd_Eor(get_irn_dbg_info(n),
5854 block, cmp_l, t, mode);
5855 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5857 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5859 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5863 and_l = get_And_left(cmp_l);
5864 if (and_l == t && is_single_bit(and_l)) {
5865 if (relation == ir_relation_equal) {
5866 /* ((1 << n) & a) == 0, (1 << n), 0) */
5867 n = new_rd_Eor(get_irn_dbg_info(n),
5868 block, cmp_l, t, mode);
5869 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5871 /* ((1 << n) & a) != 0, (1 << n), 0) */
5873 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5884 * optimize Sync nodes that have other syncs as input we simply add the inputs
5885 * of the other sync to our own inputs
5887 static ir_node *transform_node_Sync(ir_node *n)
5889 int arity = get_Sync_n_preds(n);
5892 for (i = 0; i < arity;) {
5893 ir_node *pred = get_Sync_pred(n, i);
5897 /* Remove Bad predecessors */
5904 /* Remove duplicate predecessors */
5905 for (j = 0; j < i; ++j) {
5906 if (get_Sync_pred(n, j) == pred) {
5915 if (!is_Sync(pred)) {
5923 pred_arity = get_Sync_n_preds(pred);
5924 for (j = 0; j < pred_arity; ++j) {
5925 ir_node *pred_pred = get_Sync_pred(pred, j);
5930 add_irn_n(n, pred_pred);
5934 if (get_Sync_pred(n, k) == pred_pred) break;
5940 ir_graph *irg = get_irn_irg(n);
5941 return new_r_Bad(irg, mode_M);
5944 return get_Sync_pred(n, 0);
5947 /* rehash the sync node */
5952 static ir_node *transform_node_Load(ir_node *n)
5954 /* if our memory predecessor is a load from the same address, then reuse the
5955 * previous result */
5956 ir_node *mem = get_Load_mem(n);
5961 /* don't touch volatile loads */
5962 if (get_Load_volatility(n) == volatility_is_volatile)
5964 mem_pred = get_Proj_pred(mem);
5965 if (is_Load(mem_pred)) {
5966 ir_node *pred_load = mem_pred;
5968 /* conservatively compare the 2 loads. TODO: This could be less strict
5969 * with fixup code in some situations (like smaller/bigger modes) */
5970 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5972 if (get_Load_mode(pred_load) != get_Load_mode(n))
5974 /* all combinations of aligned/unaligned pred/n should be fine so we do
5975 * not compare the unaligned attribute */
5977 ir_node *block = get_nodes_block(n);
5978 ir_node *jmp = new_r_Jmp(block);
5979 ir_graph *irg = get_irn_irg(n);
5980 ir_node *bad = new_r_Bad(irg, mode_X);
5981 ir_mode *mode = get_Load_mode(n);
5982 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5983 ir_node *in[] = { mem, res, jmp, bad };
5984 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5987 } else if (is_Store(mem_pred)) {
5988 ir_node *pred_store = mem_pred;
5989 ir_node *value = get_Store_value(pred_store);
5991 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5993 if (get_irn_mode(value) != get_Load_mode(n))
5995 /* all combinations of aligned/unaligned pred/n should be fine so we do
5996 * not compare the unaligned attribute */
5998 ir_node *block = get_nodes_block(n);
5999 ir_node *jmp = new_r_Jmp(block);
6000 ir_graph *irg = get_irn_irg(n);
6001 ir_node *bad = new_r_Bad(irg, mode_X);
6002 ir_node *res = value;
6003 ir_node *in[] = { mem, res, jmp, bad };
6004 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6013 * optimize a trampoline Call into a direct Call
6015 static ir_node *transform_node_Call(ir_node *call)
6017 ir_node *callee = get_Call_ptr(call);
6018 ir_node *adr, *mem, *res, *bl, **in;
6019 ir_type *ctp, *mtp, *tp;
6023 size_t i, n_res, n_param;
6026 if (! is_Proj(callee))
6028 callee = get_Proj_pred(callee);
6029 if (! is_Builtin(callee))
6031 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6034 mem = get_Call_mem(call);
6036 if (skip_Proj(mem) == callee) {
6037 /* memory is routed to the trampoline, skip */
6038 mem = get_Builtin_mem(callee);
6041 /* build a new call type */
6042 mtp = get_Call_type(call);
6043 tdb = get_type_dbg_info(mtp);
6045 n_res = get_method_n_ress(mtp);
6046 n_param = get_method_n_params(mtp);
6047 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6049 for (i = 0; i < n_res; ++i)
6050 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6052 NEW_ARR_A(ir_node *, in, n_param + 1);
6054 /* FIXME: we don't need a new pointer type in every step */
6055 irg = get_irn_irg(call);
6056 tp = get_irg_frame_type(irg);
6057 tp = new_type_pointer(tp);
6058 set_method_param_type(ctp, 0, tp);
6060 in[0] = get_Builtin_param(callee, 2);
6061 for (i = 0; i < n_param; ++i) {
6062 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6063 in[i + 1] = get_Call_param(call, i);
6065 var = get_method_variadicity(mtp);
6066 set_method_variadicity(ctp, var);
6067 /* When we resolve a trampoline, the function must be called by a this-call */
6068 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6069 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6071 adr = get_Builtin_param(callee, 1);
6073 db = get_irn_dbg_info(call);
6074 bl = get_nodes_block(call);
6076 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6077 if (get_irn_pinned(call) == op_pin_state_floats)
6078 set_irn_pinned(res, op_pin_state_floats);
6080 } /* transform_node_Call */
6083 * Tries several [inplace] [optimizing] transformations and returns an
6084 * equivalent node. The difference to equivalent_node() is that these
6085 * transformations _do_ generate new nodes, and thus the old node must
6086 * not be freed even if the equivalent node isn't the old one.
6088 static ir_node *transform_node(ir_node *n)
6093 * Transform_node is the only "optimizing transformation" that might
6094 * return a node with a different opcode. We iterate HERE until fixpoint
6095 * to get the final result.
6099 if (n->op->ops.transform_node != NULL)
6100 n = n->op->ops.transform_node(n);
6101 } while (oldn != n);
6104 } /* transform_node */
6107 * Sets the default transform node operation for an ir_op_ops.
6109 * @param code the opcode for the default operation
6110 * @param ops the operations initialized
6115 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6119 ops->transform_node = transform_node_##a; \
6121 #define CASE_PROJ(a) \
6123 ops->transform_node_Proj = transform_node_Proj_##a; \
6125 #define CASE_PROJ_EX(a) \
6127 ops->transform_node = transform_node_##a; \
6128 ops->transform_node_Proj = transform_node_Proj_##a; \
6169 } /* firm_set_default_transform_node */
6172 /* **************** Common Subexpression Elimination **************** */
6174 /** The size of the hash table used, should estimate the number of nodes
6176 #define N_IR_NODES 512
6178 /** Compares two exception attributes */
6179 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6181 const except_attr *ea = &a->attr.except;
6182 const except_attr *eb = &b->attr.except;
6183 return ea->pin_state != eb->pin_state;
6186 /** Compares the attributes of two Const nodes. */
6187 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6189 return get_Const_tarval(a) != get_Const_tarval(b);
6192 /** Compares the attributes of two Proj nodes. */
6193 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6195 return a->attr.proj.proj != b->attr.proj.proj;
6198 /** Compares the attributes of two Alloc nodes. */
6199 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6201 const alloc_attr *pa = &a->attr.alloc;
6202 const alloc_attr *pb = &b->attr.alloc;
6203 if (pa->where != pb->where || pa->type != pb->type)
6205 return node_cmp_exception(a, b);
6208 /** Compares the attributes of two Free nodes. */
6209 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6211 const free_attr *pa = &a->attr.free;
6212 const free_attr *pb = &b->attr.free;
6213 return (pa->where != pb->where) || (pa->type != pb->type);
6216 /** Compares the attributes of two SymConst nodes. */
6217 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6219 const symconst_attr *pa = &a->attr.symc;
6220 const symconst_attr *pb = &b->attr.symc;
6221 return (pa->kind != pb->kind)
6222 || (pa->sym.type_p != pb->sym.type_p);
6225 /** Compares the attributes of two Call nodes. */
6226 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6228 const call_attr *pa = &a->attr.call;
6229 const call_attr *pb = &b->attr.call;
6230 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6232 return node_cmp_exception(a, b);
6235 /** Compares the attributes of two Sel nodes. */
6236 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6238 const ir_entity *a_ent = get_Sel_entity(a);
6239 const ir_entity *b_ent = get_Sel_entity(b);
6240 return a_ent != b_ent;
6243 /** Compares the attributes of two Phi nodes. */
6244 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6246 /* we can only enter this function if both nodes have the same number of inputs,
6247 hence it is enough to check if one of them is a Phi0 */
6249 /* check the Phi0 pos attribute */
6250 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6255 /** Compares the attributes of two Conv nodes. */
6256 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6258 return get_Conv_strict(a) != get_Conv_strict(b);
6261 /** Compares the attributes of two Cast nodes. */
6262 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6264 return get_Cast_type(a) != get_Cast_type(b);
6267 /** Compares the attributes of two Load nodes. */
6268 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6270 if (get_Load_volatility(a) == volatility_is_volatile ||
6271 get_Load_volatility(b) == volatility_is_volatile)
6272 /* NEVER do CSE on volatile Loads */
6274 /* do not CSE Loads with different alignment. Be conservative. */
6275 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6277 if (get_Load_mode(a) != get_Load_mode(b))
6279 return node_cmp_exception(a, b);
6282 /** Compares the attributes of two Store nodes. */
6283 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6285 /* do not CSE Stores with different alignment. Be conservative. */
6286 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6288 /* NEVER do CSE on volatile Stores */
6289 if (get_Store_volatility(a) == volatility_is_volatile ||
6290 get_Store_volatility(b) == volatility_is_volatile)
6292 return node_cmp_exception(a, b);
6295 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6297 if (get_CopyB_type(a) != get_CopyB_type(b))
6300 return node_cmp_exception(a, b);
6303 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6305 return node_cmp_exception(a, b);
6308 /** Compares the attributes of two Div nodes. */
6309 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6311 const div_attr *ma = &a->attr.div;
6312 const div_attr *mb = &b->attr.div;
6313 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6315 return node_cmp_exception(a, b);
6318 /** Compares the attributes of two Mod nodes. */
6319 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6321 const mod_attr *ma = &a->attr.mod;
6322 const mod_attr *mb = &b->attr.mod;
6323 if (ma->resmode != mb->resmode)
6325 return node_cmp_exception(a, b);
6328 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6330 const cmp_attr *ma = &a->attr.cmp;
6331 const cmp_attr *mb = &b->attr.cmp;
6332 return ma->relation != mb->relation;
6335 /** Compares the attributes of two Confirm nodes. */
6336 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6338 const confirm_attr *ma = &a->attr.confirm;
6339 const confirm_attr *mb = &b->attr.confirm;
6340 return ma->relation != mb->relation;
6343 /** Compares the attributes of two Builtin nodes. */
6344 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6346 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6348 if (get_Builtin_type(a) != get_Builtin_type(b))
6350 return node_cmp_exception(a, b);
6353 /** Compares the attributes of two ASM nodes. */
6354 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6357 const ir_asm_constraint *ca;
6358 const ir_asm_constraint *cb;
6361 if (get_ASM_text(a) != get_ASM_text(b))
6364 /* Should we really check the constraints here? Should be better, but is strange. */
6365 n = get_ASM_n_input_constraints(a);
6366 if (n != get_ASM_n_input_constraints(b))
6369 ca = get_ASM_input_constraints(a);
6370 cb = get_ASM_input_constraints(b);
6371 for (i = 0; i < n; ++i) {
6372 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6373 || ca[i].mode != cb[i].mode)
6377 n = get_ASM_n_output_constraints(a);
6378 if (n != get_ASM_n_output_constraints(b))
6381 ca = get_ASM_output_constraints(a);
6382 cb = get_ASM_output_constraints(b);
6383 for (i = 0; i < n; ++i) {
6384 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6385 || ca[i].mode != cb[i].mode)
6389 n = get_ASM_n_clobbers(a);
6390 if (n != get_ASM_n_clobbers(b))
6393 cla = get_ASM_clobbers(a);
6394 clb = get_ASM_clobbers(b);
6395 for (i = 0; i < n; ++i) {
6396 if (cla[i] != clb[i])
6400 return node_cmp_exception(a, b);
6403 /** Compares the inexistent attributes of two Dummy nodes. */
6404 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6408 /* Dummy nodes never equal by definition */
6412 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6414 if (get_InstOf_type(a) != get_InstOf_type(b))
6416 return node_cmp_exception(a, b);
6420 * Set the default node attribute compare operation for an ir_op_ops.
6422 * @param code the opcode for the default operation
6423 * @param ops the operations initialized
6428 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6432 ops->node_cmp_attr = node_cmp_attr_##a; \
6465 } /* firm_set_default_node_cmp_attr */
6468 * Compare function for two nodes in the value table. Gets two
6469 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6471 int identities_cmp(const void *elt, const void *key)
6473 ir_node *a = (ir_node *)elt;
6474 ir_node *b = (ir_node *)key;
6477 if (a == b) return 0;
6479 if ((get_irn_op(a) != get_irn_op(b)) ||
6480 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6482 /* compare if a's in and b's in are of equal length */
6483 irn_arity_a = get_irn_arity(a);
6484 if (irn_arity_a != get_irn_arity(b))
6487 /* blocks are never the same */
6491 if (get_irn_pinned(a) == op_pin_state_pinned) {
6492 /* for pinned nodes, the block inputs must be equal */
6493 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6496 ir_node *block_a = get_nodes_block(a);
6497 ir_node *block_b = get_nodes_block(b);
6498 if (! get_opt_global_cse()) {
6499 /* for block-local CSE both nodes must be in the same Block */
6500 if (block_a != block_b)
6503 /* The optimistic approach would be to do nothing here.
6504 * However doing GCSE optimistically produces a lot of partially dead code which appears
6505 * to be worse in practice than the missed opportunities.
6506 * So we use a very conservative variant here and only CSE if 1 value dominates the
6508 if (!block_dominates(block_a, block_b)
6509 && !block_dominates(block_b, block_a))
6514 /* compare a->in[0..ins] with b->in[0..ins] */
6515 for (i = 0; i < irn_arity_a; ++i) {
6516 ir_node *pred_a = get_irn_n(a, i);
6517 ir_node *pred_b = get_irn_n(b, i);
6518 if (pred_a != pred_b) {
6519 /* if both predecessors are CSE neutral they might be different */
6520 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6526 * here, we already now that the nodes are identical except their
6529 if (a->op->ops.node_cmp_attr)
6530 return a->op->ops.node_cmp_attr(a, b);
6533 } /* identities_cmp */
6536 * Calculate a hash value of a node.
6538 * @param node The IR-node
6540 unsigned ir_node_hash(const ir_node *node)
6542 return node->op->ops.hash(node);
6543 } /* ir_node_hash */
6546 void new_identities(ir_graph *irg)
6548 if (irg->value_table != NULL)
6549 del_pset(irg->value_table);
6550 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6551 } /* new_identities */
6553 void del_identities(ir_graph *irg)
6555 if (irg->value_table != NULL)
6556 del_pset(irg->value_table);
6557 } /* del_identities */
6559 /* Normalize a node by putting constants (and operands with larger
6560 * node index) on the right (operator side). */
6561 void ir_normalize_node(ir_node *n)
6563 if (is_op_commutative(get_irn_op(n))) {
6564 ir_node *l = get_binop_left(n);
6565 ir_node *r = get_binop_right(n);
6567 /* For commutative operators perform a OP b == b OP a but keep
6568 * constants on the RIGHT side. This helps greatly in some
6569 * optimizations. Moreover we use the idx number to make the form
6571 if (!operands_are_normalized(l, r)) {
6572 set_binop_left(n, r);
6573 set_binop_right(n, l);
6577 } /* ir_normalize_node */
6580 * Return the canonical node computing the same value as n.
6581 * Looks up the node in a hash table, enters it in the table
6582 * if it isn't there yet.
6584 * @param n the node to look up
6586 * @return a node that computes the same value as n or n if no such
6587 * node could be found
6589 ir_node *identify_remember(ir_node *n)
6591 ir_graph *irg = get_irn_irg(n);
6592 pset *value_table = irg->value_table;
6595 if (value_table == NULL)
6598 ir_normalize_node(n);
6599 /* lookup or insert in hash table with given hash key. */
6600 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6603 /* n is reachable again */
6604 edges_node_revival(nn);
6608 } /* identify_remember */
6611 * During construction we set the op_pin_state_pinned flag in the graph right
6612 * when the optimization is performed. The flag turning on procedure global
6613 * cse could be changed between two allocations. This way we are safe.
6615 * @param n The node to lookup
6617 static inline ir_node *identify_cons(ir_node *n)
6621 n = identify_remember(n);
6622 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6623 ir_graph *irg = get_irn_irg(n);
6624 set_irg_pinned(irg, op_pin_state_floats);
6627 } /* identify_cons */
6629 /* Add a node to the identities value table. */
6630 void add_identities(ir_node *node)
6637 identify_remember(node);
6640 /* Visit each node in the value table of a graph. */
6641 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6644 ir_graph *rem = current_ir_graph;
6646 current_ir_graph = irg;
6647 foreach_pset(irg->value_table, ir_node*, node) {
6650 current_ir_graph = rem;
6651 } /* visit_all_identities */
6654 * These optimizations deallocate nodes from the obstack.
6655 * It can only be called if it is guaranteed that no other nodes
6656 * reference this one, i.e., right after construction of a node.
6658 * @param n The node to optimize
6660 ir_node *optimize_node(ir_node *n)
6663 ir_graph *irg = get_irn_irg(n);
6664 unsigned iro = get_irn_opcode(n);
6667 /* Always optimize Phi nodes: part of the construction. */
6668 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6670 /* constant expression evaluation / constant folding */
6671 if (get_opt_constant_folding()) {
6672 /* neither constants nor Tuple values can be evaluated */
6673 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6674 /* try to evaluate */
6675 tv = computed_value(n);
6676 if (tv != tarval_bad) {
6681 * we MUST copy the node here temporarily, because it's still
6682 * needed for DBG_OPT_CSTEVAL
6684 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6685 oldn = (ir_node*)alloca(node_size);
6687 memcpy(oldn, n, node_size);
6688 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6690 /* ARG, copy the in array, we need it for statistics */
6691 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6693 /* note the inplace edges module */
6694 edges_node_deleted(n);
6696 /* evaluation was successful -- replace the node. */
6697 irg_kill_node(irg, n);
6698 nw = new_r_Const(irg, tv);
6700 DBG_OPT_CSTEVAL(oldn, nw);
6706 /* remove unnecessary nodes */
6707 if (get_opt_algebraic_simplification() ||
6708 (iro == iro_Phi) || /* always optimize these nodes. */
6710 (iro == iro_Proj) ||
6711 (iro == iro_Block) ) /* Flags tested local. */
6712 n = equivalent_node(n);
6714 /* Common Subexpression Elimination.
6716 * Checks whether n is already available.
6717 * The block input is used to distinguish different subexpressions. Right
6718 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6719 * subexpressions within a block.
6722 n = identify_cons(n);
6725 edges_node_deleted(oldn);
6727 /* We found an existing, better node, so we can deallocate the old node. */
6728 irg_kill_node(irg, oldn);
6732 /* Some more constant expression evaluation that does not allow to
6734 iro = get_irn_opcode(n);
6735 if (get_opt_algebraic_simplification() ||
6736 (iro == iro_Cond) ||
6737 (iro == iro_Proj)) /* Flags tested local. */
6738 n = transform_node(n);
6740 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6741 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6743 n = identify_remember(o);
6749 } /* optimize_node */
6753 * These optimizations never deallocate nodes (in place). This can cause dead
6754 * nodes lying on the obstack. Remove these by a dead node elimination,
6755 * i.e., a copying garbage collection.
6757 ir_node *optimize_in_place_2(ir_node *n)
6761 unsigned iro = get_irn_opcode(n);
6763 if (!get_opt_optimize() && !is_Phi(n)) return n;
6765 if (iro == iro_Deleted)
6768 /* constant expression evaluation / constant folding */
6769 if (get_opt_constant_folding()) {
6770 /* neither constants nor Tuple values can be evaluated */
6771 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6772 /* try to evaluate */
6773 tv = computed_value(n);
6774 if (tv != tarval_bad) {
6775 /* evaluation was successful -- replace the node. */
6776 ir_graph *irg = get_irn_irg(n);
6778 n = new_r_Const(irg, tv);
6780 DBG_OPT_CSTEVAL(oldn, n);
6786 /* remove unnecessary nodes */
6787 if (get_opt_constant_folding() ||
6788 (iro == iro_Phi) || /* always optimize these nodes. */
6789 (iro == iro_Id) || /* ... */
6790 (iro == iro_Proj) || /* ... */
6791 (iro == iro_Block) ) /* Flags tested local. */
6792 n = equivalent_node(n);
6794 /** common subexpression elimination **/
6795 /* Checks whether n is already available. */
6796 /* The block input is used to distinguish different subexpressions. Right
6797 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6798 subexpressions within a block. */
6799 if (get_opt_cse()) {
6801 n = identify_remember(o);
6806 /* Some more constant expression evaluation. */
6807 iro = get_irn_opcode(n);
6808 if (get_opt_constant_folding() ||
6809 (iro == iro_Cond) ||
6810 (iro == iro_Proj)) /* Flags tested local. */
6811 n = transform_node(n);
6813 /* Now we can verify the node, as it has no dead inputs any more. */
6816 /* Now we have a legal, useful node. Enter it in hash table for cse.
6817 Blocks should be unique anyways. (Except the successor of start:
6818 is cse with the start block!) */
6819 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6821 n = identify_remember(o);
6827 } /* optimize_in_place_2 */
6830 * Wrapper for external use, set proper status bits after optimization.
6832 ir_node *optimize_in_place(ir_node *n)
6834 ir_graph *irg = get_irn_irg(n);
6835 /* Handle graph state */
6836 assert(get_irg_phase_state(irg) != phase_building);
6838 if (get_opt_global_cse())
6839 set_irg_pinned(irg, op_pin_state_floats);
6841 /* FIXME: Maybe we could also test whether optimizing the node can
6842 change the control graph. */
6843 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
6844 return optimize_in_place_2(n);
6845 } /* optimize_in_place */
6848 * Calculate a hash value of a Const node.
6850 static unsigned hash_Const(const ir_node *node)
6854 /* special value for const, as they only differ in their tarval. */
6855 h = HASH_PTR(node->attr.con.tarval);
6861 * Calculate a hash value of a SymConst node.
6863 static unsigned hash_SymConst(const ir_node *node)
6867 /* all others are pointers */
6868 h = HASH_PTR(node->attr.symc.sym.type_p);
6871 } /* hash_SymConst */
6874 * Set the default hash operation in an ir_op_ops.
6876 * @param code the opcode for the default operation
6877 * @param ops the operations initialized
6882 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6886 ops->hash = hash_##a; \
6889 /* hash function already set */
6890 if (ops->hash != NULL)
6897 /* use input/mode default hash if no function was given */
6898 ops->hash = firm_default_hash;
6906 * Sets the default operation for an ir_ops.
6908 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6910 ops = firm_set_default_hash(code, ops);
6911 ops = firm_set_default_computed_value(code, ops);
6912 ops = firm_set_default_equivalent_node(code, ops);
6913 ops = firm_set_default_transform_node(code, ops);
6914 ops = firm_set_default_node_cmp_attr(code, ops);
6915 ops = firm_set_default_get_type_attr(code, ops);
6916 ops = firm_set_default_get_entity_attr(code, ops);
6919 } /* firm_set_default_operations */