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;
1988 n = transform_node_AddSub(n);
1990 a = get_Add_left(n);
1991 b = get_Add_right(n);
1993 mode = get_irn_mode(n);
1995 if (mode_is_reference(mode)) {
1996 ir_mode *lmode = get_irn_mode(a);
1998 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1999 /* an Add(a, NULL) is a hidden Conv */
2000 dbg_info *dbg = get_irn_dbg_info(n);
2001 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2005 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2007 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2008 if (mode_is_float(mode)) {
2009 ir_graph *irg = get_irn_irg(n);
2010 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2014 if (mode_is_num(mode)) {
2015 ir_graph *irg = get_irn_irg(n);
2016 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2017 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2018 && a == b && mode_is_int(mode)) {
2019 ir_node *block = get_nodes_block(n);
2022 get_irn_dbg_info(n),
2025 new_r_Const_long(irg, mode, 2),
2027 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2032 get_irn_dbg_info(n),
2037 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2042 get_irn_dbg_info(n),
2047 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2050 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2051 /* Here we rely on constants be on the RIGHT side */
2053 ir_node *op = get_Not_op(a);
2055 if (is_Const(b) && is_Const_one(b)) {
2057 ir_node *blk = get_nodes_block(n);
2058 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2059 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2067 } /* transform_node_Add */
2070 * returns -cnst or NULL if impossible
2072 static ir_node *const_negate(ir_node *cnst)
2074 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2075 dbg_info *dbgi = get_irn_dbg_info(cnst);
2076 ir_graph *irg = get_irn_irg(cnst);
2077 if (tv == tarval_bad) return NULL;
2078 return new_rd_Const(dbgi, irg, tv);
2082 * Do the AddSub optimization, then Transform
2083 * Constant folding on Phi
2084 * Sub(0,a) -> Minus(a)
2085 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2086 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2087 * Sub(Add(a, x), x) -> a
2088 * Sub(x, Add(x, a)) -> -a
2089 * Sub(x, Const) -> Add(x, -Const)
2091 static ir_node *transform_node_Sub(ir_node *n)
2097 n = transform_node_AddSub(n);
2099 a = get_Sub_left(n);
2100 b = get_Sub_right(n);
2102 mode = get_irn_mode(n);
2104 if (mode_is_int(mode)) {
2105 ir_mode *lmode = get_irn_mode(a);
2107 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2108 /* a Sub(a, NULL) is a hidden Conv */
2109 dbg_info *dbg = get_irn_dbg_info(n);
2110 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2115 if (mode == lmode &&
2116 get_mode_arithmetic(mode) == irma_twos_complement &&
2118 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2120 dbg_info *dbg = get_irn_dbg_info(n);
2121 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2122 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2128 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2130 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2131 if (mode_is_float(mode)) {
2132 ir_graph *irg = get_irn_irg(n);
2133 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2137 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2138 /* a - C -> a + (-C) */
2139 ir_node *cnst = const_negate(b);
2141 ir_node *block = get_nodes_block(n);
2142 dbg_info *dbgi = get_irn_dbg_info(n);
2144 n = new_rd_Add(dbgi, block, a, cnst, mode);
2145 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2150 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2151 dbg_info *dbg = get_irn_dbg_info(n);
2152 ir_node *block = get_nodes_block(n);
2153 ir_node *left = get_Minus_op(a);
2154 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2156 n = new_rd_Minus(dbg, block, add, mode);
2157 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2159 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2160 dbg_info *dbg = get_irn_dbg_info(n);
2161 ir_node *block = get_nodes_block(n);
2162 ir_node *right = get_Minus_op(b);
2164 n = new_rd_Add(dbg, block, a, right, mode);
2165 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2167 } else if (is_Sub(b)) {
2168 /* a - (b - c) -> a + (c - b)
2169 * -> (a - b) + c iff (b - c) is a pointer */
2170 dbg_info *s_dbg = get_irn_dbg_info(b);
2171 ir_node *s_left = get_Sub_left(b);
2172 ir_node *s_right = get_Sub_right(b);
2173 ir_mode *s_mode = get_irn_mode(b);
2174 if (mode_is_reference(s_mode)) {
2175 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2176 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2177 dbg_info *a_dbg = get_irn_dbg_info(n);
2180 s_right = new_r_Conv(lowest_block, s_right, mode);
2181 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2183 ir_node *s_block = get_nodes_block(b);
2184 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2185 dbg_info *a_dbg = get_irn_dbg_info(n);
2186 ir_node *a_block = get_nodes_block(n);
2188 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2190 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2193 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2194 ir_node *m_right = get_Mul_right(b);
2195 if (is_Const(m_right)) {
2196 ir_node *cnst2 = const_negate(m_right);
2197 if (cnst2 != NULL) {
2198 dbg_info *m_dbg = get_irn_dbg_info(b);
2199 ir_node *m_block = get_nodes_block(b);
2200 ir_node *m_left = get_Mul_left(b);
2201 ir_mode *m_mode = get_irn_mode(b);
2202 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2203 dbg_info *a_dbg = get_irn_dbg_info(n);
2204 ir_node *a_block = get_nodes_block(n);
2206 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2207 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2214 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2215 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2217 get_irn_dbg_info(n),
2221 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2225 if (mode_wrap_around(mode)) {
2226 ir_node *left = get_Add_left(a);
2227 ir_node *right = get_Add_right(a);
2229 /* FIXME: Does the Conv's work only for two complement or generally? */
2231 if (mode != get_irn_mode(right)) {
2232 /* This Sub is an effective Cast */
2233 right = new_r_Conv(get_nodes_block(n), right, mode);
2236 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2238 } else if (right == b) {
2239 if (mode != get_irn_mode(left)) {
2240 /* This Sub is an effective Cast */
2241 left = new_r_Conv(get_nodes_block(n), left, mode);
2244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2250 if (mode_wrap_around(mode)) {
2251 ir_node *left = get_Add_left(b);
2252 ir_node *right = get_Add_right(b);
2254 /* FIXME: Does the Conv's work only for two complement or generally? */
2256 ir_mode *r_mode = get_irn_mode(right);
2258 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2259 if (mode != r_mode) {
2260 /* This Sub is an effective Cast */
2261 n = new_r_Conv(get_nodes_block(n), n, mode);
2263 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2265 } else if (right == a) {
2266 ir_mode *l_mode = get_irn_mode(left);
2268 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2269 if (mode != l_mode) {
2270 /* This Sub is an effective Cast */
2271 n = new_r_Conv(get_nodes_block(n), n, mode);
2273 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2278 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2279 ir_mode *mode = get_irn_mode(a);
2281 if (mode == get_irn_mode(b)) {
2283 ir_node *op_a = get_Conv_op(a);
2284 ir_node *op_b = get_Conv_op(b);
2286 /* check if it's allowed to skip the conv */
2287 ma = get_irn_mode(op_a);
2288 mb = get_irn_mode(op_b);
2290 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2291 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2294 set_Sub_right(n, b);
2300 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2301 if (!is_reassoc_running() && is_Mul(a)) {
2302 ir_node *ma = get_Mul_left(a);
2303 ir_node *mb = get_Mul_right(a);
2306 ir_node *blk = get_nodes_block(n);
2307 ir_graph *irg = get_irn_irg(n);
2309 get_irn_dbg_info(n),
2313 get_irn_dbg_info(n),
2316 new_r_Const(irg, get_mode_one(mode)),
2319 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2321 } else if (mb == b) {
2322 ir_node *blk = get_nodes_block(n);
2323 ir_graph *irg = get_irn_irg(n);
2325 get_irn_dbg_info(n),
2329 get_irn_dbg_info(n),
2332 new_r_Const(irg, get_mode_one(mode)),
2335 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2339 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2340 ir_node *x = get_Sub_left(a);
2341 ir_node *y = get_Sub_right(a);
2342 ir_node *blk = get_nodes_block(n);
2343 ir_mode *m_b = get_irn_mode(b);
2344 ir_mode *m_y = get_irn_mode(y);
2348 /* Determine the right mode for the Add. */
2351 else if (mode_is_reference(m_b))
2353 else if (mode_is_reference(m_y))
2357 * Both modes are different but none is reference,
2358 * happens for instance in SubP(SubP(P, Iu), Is).
2359 * We have two possibilities here: Cast or ignore.
2360 * Currently we ignore this case.
2365 add = new_r_Add(blk, y, b, add_mode);
2367 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2368 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2372 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2373 /* c - ~X = X + (c+1) */
2374 if (is_Const(a) && is_Not(b)) {
2375 ir_tarval *tv = get_Const_tarval(a);
2377 tv = tarval_add(tv, get_mode_one(mode));
2378 if (tv != tarval_bad) {
2379 ir_node *blk = get_nodes_block(n);
2380 ir_graph *irg = get_irn_irg(n);
2381 ir_node *c = new_r_Const(irg, tv);
2382 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2387 /* x-(x&y) = x & ~y */
2389 ir_node *and_left = get_And_left(b);
2390 ir_node *and_right = get_And_right(b);
2391 if (and_right == a) {
2392 ir_node *tmp = and_left;
2393 and_left = and_right;
2396 if (and_left == a) {
2397 dbg_info *dbgi = get_irn_dbg_info(n);
2398 ir_node *block = get_nodes_block(n);
2399 ir_mode *mode = get_irn_mode(n);
2400 ir_node *notn = new_rd_Not(dbgi, block, and_right, mode);
2401 ir_node *and = new_rd_And(dbgi, block, a, notn, mode);
2407 } /* transform_node_Sub */
2410 * Several transformation done on n*n=2n bits mul.
2411 * These transformations must be done here because new nodes may be produced.
2413 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2416 ir_node *a = get_Mul_left(n);
2417 ir_node *b = get_Mul_right(n);
2418 ir_tarval *ta = value_of(a);
2419 ir_tarval *tb = value_of(b);
2420 ir_mode *smode = get_irn_mode(a);
2422 if (ta == get_mode_one(smode)) {
2423 /* (L)1 * (L)b = (L)b */
2424 ir_node *blk = get_nodes_block(n);
2425 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2429 else if (ta == get_mode_minus_one(smode)) {
2430 /* (L)-1 * (L)b = (L)b */
2431 ir_node *blk = get_nodes_block(n);
2432 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2433 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2434 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2437 if (tb == get_mode_one(smode)) {
2438 /* (L)a * (L)1 = (L)a */
2439 ir_node *blk = get_irn_n(a, -1);
2440 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2441 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2444 else if (tb == get_mode_minus_one(smode)) {
2445 /* (L)a * (L)-1 = (L)-a */
2446 ir_node *blk = get_nodes_block(n);
2447 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2448 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2449 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2456 * Transform Mul(a,-1) into -a.
2457 * Do constant evaluation of Phi nodes.
2458 * Do architecture dependent optimizations on Mul nodes
2460 static ir_node *transform_node_Mul(ir_node *n)
2462 ir_node *c, *oldn = n;
2463 ir_mode *mode = get_irn_mode(n);
2464 ir_node *a = get_Mul_left(n);
2465 ir_node *b = get_Mul_right(n);
2467 if (is_Bad(a) || is_Bad(b))
2470 if (mode != get_irn_mode(a))
2471 return transform_node_Mul2n(n, mode);
2473 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2475 if (mode_is_signed(mode)) {
2478 if (value_of(a) == get_mode_minus_one(mode))
2480 else if (value_of(b) == get_mode_minus_one(mode))
2483 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2484 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2489 if (is_Const(b)) { /* (-a) * const -> a * -const */
2490 ir_node *cnst = const_negate(b);
2492 dbg_info *dbgi = get_irn_dbg_info(n);
2493 ir_node *block = get_nodes_block(n);
2494 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2495 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2498 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2499 dbg_info *dbgi = get_irn_dbg_info(n);
2500 ir_node *block = get_nodes_block(n);
2501 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2502 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2504 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2505 ir_node *sub_l = get_Sub_left(b);
2506 ir_node *sub_r = get_Sub_right(b);
2507 dbg_info *dbgi = get_irn_dbg_info(n);
2508 ir_node *block = get_nodes_block(n);
2509 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2510 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2511 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2514 } else if (is_Minus(b)) {
2515 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2516 ir_node *sub_l = get_Sub_left(a);
2517 ir_node *sub_r = get_Sub_right(a);
2518 dbg_info *dbgi = get_irn_dbg_info(n);
2519 ir_node *block = get_nodes_block(n);
2520 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2521 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2522 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2525 } else if (is_Shl(a)) {
2526 ir_node *const shl_l = get_Shl_left(a);
2527 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2528 /* (1 << x) * b -> b << x */
2529 dbg_info *const dbgi = get_irn_dbg_info(n);
2530 ir_node *const block = get_nodes_block(n);
2531 ir_node *const shl_r = get_Shl_right(a);
2532 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2533 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2536 } else if (is_Shl(b)) {
2537 ir_node *const shl_l = get_Shl_left(b);
2538 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2539 /* a * (1 << x) -> a << x */
2540 dbg_info *const dbgi = get_irn_dbg_info(n);
2541 ir_node *const block = get_nodes_block(n);
2542 ir_node *const shl_r = get_Shl_right(b);
2543 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2544 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2548 if (get_mode_arithmetic(mode) == irma_ieee754) {
2550 ir_tarval *tv = get_Const_tarval(a);
2551 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2552 && !tarval_is_negative(tv)) {
2553 /* 2.0 * b = b + b */
2554 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2555 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2559 else if (is_Const(b)) {
2560 ir_tarval *tv = get_Const_tarval(b);
2561 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2562 && !tarval_is_negative(tv)) {
2563 /* a * 2.0 = a + a */
2564 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2565 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2570 return arch_dep_replace_mul_with_shifts(n);
2571 } /* transform_node_Mul */
2574 * Transform a Div Node.
2576 static ir_node *transform_node_Div(ir_node *n)
2578 ir_mode *mode = get_Div_resmode(n);
2579 ir_node *a = get_Div_left(n);
2580 ir_node *b = get_Div_right(n);
2582 const ir_node *dummy;
2584 if (mode_is_int(mode)) {
2585 if (is_Const(b) && is_const_Phi(a)) {
2586 /* check for Div(Phi, Const) */
2587 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2589 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2592 } else if (is_Const(a) && is_const_Phi(b)) {
2593 /* check for Div(Const, Phi) */
2594 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2596 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2599 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2600 /* check for Div(Phi, Phi) */
2601 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2603 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2608 if (a == b && value_not_zero(a, &dummy)) {
2609 ir_graph *irg = get_irn_irg(n);
2610 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2611 value = new_r_Const(irg, get_mode_one(mode));
2612 DBG_OPT_CSTEVAL(n, value);
2615 if (mode_is_signed(mode) && is_Const(b)) {
2616 ir_tarval *tv = get_Const_tarval(b);
2618 if (tv == get_mode_minus_one(mode)) {
2620 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2621 DBG_OPT_CSTEVAL(n, value);
2625 /* Try architecture dependent optimization */
2626 value = arch_dep_replace_div_by_const(n);
2629 assert(mode_is_float(mode));
2631 /* Optimize x/c to x*(1/c) */
2632 if (get_mode_arithmetic(mode) == irma_ieee754) {
2633 ir_tarval *tv = value_of(b);
2635 if (tv != tarval_bad) {
2636 int rem = tarval_fp_ops_enabled();
2639 * Floating point constant folding might be disabled here to
2641 * However, as we check for exact result, doing it is safe.
2644 tarval_enable_fp_ops(1);
2645 tv = tarval_div(get_mode_one(mode), tv);
2646 tarval_enable_fp_ops(rem);
2648 /* Do the transformation if the result is either exact or we are
2649 not using strict rules. */
2650 if (tv != tarval_bad &&
2651 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2652 ir_node *block = get_nodes_block(n);
2653 ir_graph *irg = get_irn_irg(block);
2654 ir_node *c = new_r_Const(irg, tv);
2655 dbg_info *dbgi = get_irn_dbg_info(n);
2656 value = new_rd_Mul(dbgi, block, a, c, mode);
2669 /* Turn Div into a tuple (mem, jmp, bad, value) */
2670 mem = get_Div_mem(n);
2671 blk = get_nodes_block(n);
2672 irg = get_irn_irg(blk);
2674 /* skip a potential Pin */
2675 mem = skip_Pin(mem);
2676 turn_into_tuple(n, pn_Div_max+1);
2677 set_Tuple_pred(n, pn_Div_M, mem);
2678 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2679 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2680 set_Tuple_pred(n, pn_Div_res, value);
2683 } /* transform_node_Div */
2686 * Transform a Mod node.
2688 static ir_node *transform_node_Mod(ir_node *n)
2690 ir_mode *mode = get_Mod_resmode(n);
2691 ir_node *a = get_Mod_left(n);
2692 ir_node *b = get_Mod_right(n);
2697 if (is_Const(b) && is_const_Phi(a)) {
2698 /* check for Div(Phi, Const) */
2699 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2701 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2705 else if (is_Const(a) && is_const_Phi(b)) {
2706 /* check for Div(Const, Phi) */
2707 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2709 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2713 else if (is_const_Phi(a) && is_const_Phi(b)) {
2714 /* check for Div(Phi, Phi) */
2715 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2717 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2724 irg = get_irn_irg(n);
2725 if (tv != tarval_bad) {
2726 value = new_r_Const(irg, tv);
2728 DBG_OPT_CSTEVAL(n, value);
2731 ir_node *a = get_Mod_left(n);
2732 ir_node *b = get_Mod_right(n);
2733 const ir_node *dummy;
2735 if (a == b && value_not_zero(a, &dummy)) {
2736 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2737 value = new_r_Const(irg, get_mode_null(mode));
2738 DBG_OPT_CSTEVAL(n, value);
2741 if (mode_is_signed(mode) && is_Const(b)) {
2742 ir_tarval *tv = get_Const_tarval(b);
2744 if (tv == get_mode_minus_one(mode)) {
2746 value = new_r_Const(irg, get_mode_null(mode));
2747 DBG_OPT_CSTEVAL(n, value);
2751 /* Try architecture dependent optimization */
2752 value = arch_dep_replace_mod_by_const(n);
2761 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2762 mem = get_Mod_mem(n);
2763 blk = get_nodes_block(n);
2764 irg = get_irn_irg(blk);
2766 /* skip a potential Pin */
2767 mem = skip_Pin(mem);
2768 turn_into_tuple(n, pn_Mod_max+1);
2769 set_Tuple_pred(n, pn_Mod_M, mem);
2770 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2771 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2772 set_Tuple_pred(n, pn_Mod_res, value);
2775 } /* transform_node_Mod */
2778 * Transform a Cond node.
2780 * Replace the Cond by a Jmp if it branches on a constant
2783 static ir_node *transform_node_Cond(ir_node *n)
2786 ir_node *a = get_Cond_selector(n);
2787 ir_graph *irg = get_irn_irg(n);
2791 /* we need block info which is not available in floating irgs */
2792 if (get_irg_pinned(irg) == op_pin_state_floats)
2795 /* we do not handle switches here */
2796 if (get_irn_mode(a) != mode_b)
2800 if (ta == tarval_bad && is_Cmp(a)) {
2801 /* try again with a direct call to compute_cmp, as we don't care
2802 * about the MODEB_LOWERED flag here */
2803 ta = compute_cmp(a);
2806 if (ta != tarval_bad && get_irn_mode(a) == mode_b) {
2807 /* It's a boolean Cond, branching on a boolean constant.
2808 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2809 ir_node *blk = get_nodes_block(n);
2810 jmp = new_r_Jmp(blk);
2811 turn_into_tuple(n, pn_Cond_max+1);
2812 if (ta == tarval_b_true) {
2813 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2814 set_Tuple_pred(n, pn_Cond_true, jmp);
2816 set_Tuple_pred(n, pn_Cond_false, jmp);
2817 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2819 /* We might generate an endless loop, so keep it alive. */
2820 add_End_keepalive(get_irg_end(irg), blk);
2821 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
2824 } /* transform_node_Cond */
2827 * Prototype of a recursive transform function
2828 * for bitwise distributive transformations.
2830 typedef ir_node* (*recursive_transform)(ir_node *n);
2833 * makes use of distributive laws for and, or, eor
2834 * and(a OP c, b OP c) -> and(a, b) OP c
2835 * note, might return a different op than n
2837 static ir_node *transform_bitwise_distributive(ir_node *n,
2838 recursive_transform trans_func)
2841 ir_node *a = get_binop_left(n);
2842 ir_node *b = get_binop_right(n);
2843 ir_op *op = get_irn_op(a);
2844 ir_op *op_root = get_irn_op(n);
2846 if (op != get_irn_op(b))
2849 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2850 if (op == op_Conv) {
2851 ir_node *a_op = get_Conv_op(a);
2852 ir_node *b_op = get_Conv_op(b);
2853 ir_mode *a_mode = get_irn_mode(a_op);
2854 ir_mode *b_mode = get_irn_mode(b_op);
2855 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2856 ir_node *blk = get_nodes_block(n);
2859 set_binop_left(n, a_op);
2860 set_binop_right(n, b_op);
2861 set_irn_mode(n, a_mode);
2863 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2865 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2871 /* nothing to gain here */
2875 if (op == op_Shrs || op == op_Shr || op == op_Shl
2876 || op == op_And || op == op_Or || op == op_Eor) {
2877 ir_node *a_left = get_binop_left(a);
2878 ir_node *a_right = get_binop_right(a);
2879 ir_node *b_left = get_binop_left(b);
2880 ir_node *b_right = get_binop_right(b);
2882 ir_node *op1 = NULL;
2883 ir_node *op2 = NULL;
2885 if (is_op_commutative(op)) {
2886 if (a_left == b_left) {
2890 } else if (a_left == b_right) {
2894 } else if (a_right == b_left) {
2900 if (a_right == b_right) {
2907 /* (a sop c) & (b sop c) => (a & b) sop c */
2908 ir_node *blk = get_nodes_block(n);
2910 ir_node *new_n = exact_copy(n);
2911 set_binop_left(new_n, op1);
2912 set_binop_right(new_n, op2);
2913 new_n = trans_func(new_n);
2915 if (op_root == op_Eor && op == op_Or) {
2916 dbg_info *dbgi = get_irn_dbg_info(n);
2917 ir_mode *mode = get_irn_mode(c);
2919 c = new_rd_Not(dbgi, blk, c, mode);
2920 n = new_rd_And(dbgi, blk, new_n, c, mode);
2923 set_nodes_block(n, blk);
2924 set_binop_left(n, new_n);
2925 set_binop_right(n, c);
2929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2938 * Create a 0 constant of given mode.
2940 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2942 ir_tarval *tv = get_mode_null(mode);
2943 ir_node *cnst = new_r_Const(irg, tv);
2948 static bool is_shiftop(const ir_node *n)
2950 return is_Shl(n) || is_Shr(n) || is_Shrs(n) || is_Rotl(n);
2954 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
2956 * - and, or, xor instead of &
2957 * - Shl, Shr, Shrs, rotl instead of >>
2958 * (with a special case for Or/Xor + Shrs)
2960 * This normalisation is good for things like x-(x&y) esp. in 186.crafty.
2962 static ir_node *transform_node_shift_bitop(ir_node *n)
2964 ir_graph *irg = get_irn_irg(n);
2965 ir_node *right = get_binop_right(n);
2966 ir_mode *mode = get_irn_mode(n);
2968 ir_node *bitop_left;
2969 ir_node *bitop_right;
2978 ir_tarval *tv_shift;
2980 if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
2983 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
2985 if (!is_Const(right))
2988 left = get_binop_left(n);
2989 op_left = get_irn_op(left);
2990 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
2993 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
2994 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
2995 /* TODO: test if sign bit is affectes */
2999 bitop_right = get_binop_right(left);
3000 if (!is_Const(bitop_right))
3003 bitop_left = get_binop_left(left);
3005 block = get_nodes_block(n);
3006 dbgi = get_irn_dbg_info(n);
3007 tv1 = get_Const_tarval(bitop_right);
3008 tv2 = get_Const_tarval(right);
3010 assert(get_tarval_mode(tv1) == mode);
3013 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
3014 tv_shift = tarval_shl(tv1, tv2);
3015 } else if (is_Shr(n)) {
3016 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
3017 tv_shift = tarval_shr(tv1, tv2);
3018 } else if (is_Shrs(n)) {
3019 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
3020 tv_shift = tarval_shrs(tv1, tv2);
3023 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
3024 tv_shift = tarval_rotl(tv1, tv2);
3027 assert(get_tarval_mode(tv_shift) == mode);
3028 irg = get_irn_irg(n);
3029 new_const = new_r_Const(irg, tv_shift);
3031 if (op_left == op_And) {
3032 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
3033 } else if (op_left == op_Or) {
3034 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
3036 assert(op_left == op_Eor);
3037 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
3044 * normalisation: (x >> c1) & c2 to (x & (c2<<c1)) >> c1
3046 * - and, or, xor instead of &
3047 * - Shl, Shr, Shrs, rotl instead of >>
3048 * (with a special case for Or/Xor + Shrs)
3050 * This normalisation is usually good for the backend since << C can often be
3051 * matched as address-mode.
3053 static ir_node *transform_node_bitop_shift(ir_node *n)
3055 ir_graph *irg = get_irn_irg(n);
3056 ir_node *left = get_binop_left(n);
3057 ir_node *right = get_binop_right(n);
3058 ir_mode *mode = get_irn_mode(n);
3059 ir_node *shift_left;
3060 ir_node *shift_right;
3062 dbg_info *dbg_bitop;
3063 dbg_info *dbg_shift;
3069 ir_tarval *tv_bitop;
3071 if (!is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
3074 assert(is_And(n) || is_Or(n) || is_Eor(n));
3075 if (!is_Const(right) || !is_shiftop(left))
3078 shift_left = get_binop_left(left);
3079 shift_right = get_binop_right(left);
3080 if (!is_Const(shift_right))
3083 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
3084 if (is_Shrs(left)) {
3085 /* TODO this could be improved */
3089 irg = get_irn_irg(n);
3090 block = get_nodes_block(n);
3091 dbg_bitop = get_irn_dbg_info(n);
3092 dbg_shift = get_irn_dbg_info(left);
3093 tv1 = get_Const_tarval(shift_right);
3094 tv2 = get_Const_tarval(right);
3095 assert(get_tarval_mode(tv2) == mode);
3098 tv_bitop = tarval_shr(tv2, tv1);
3100 /* Check whether we have lost some bits during the right shift. */
3101 if (is_Or(n) || is_Eor(n)) {
3102 ir_tarval *tv_back_again = tarval_shl(tv_bitop, tv1);
3104 if (tarval_cmp(tv_back_again, tv2) != ir_relation_equal)
3107 } else if (is_Shr(left)) {
3108 if (is_Or(n) || is_Eor(n)) {
3110 * TODO this can be improved by checking whether
3111 * the left shift produces an overflow
3115 tv_bitop = tarval_shl(tv2, tv1);
3117 assert(is_Rotl(left));
3118 tv_bitop = tarval_rotl(tv2, tarval_neg(tv1));
3120 new_const = new_r_Const(irg, tv_bitop);
3123 new_bitop = new_rd_And(dbg_bitop, block, shift_left, new_const, mode);
3124 } else if (is_Or(n)) {
3125 new_bitop = new_rd_Or(dbg_bitop, block, shift_left, new_const, mode);
3128 new_bitop = new_rd_Eor(dbg_bitop, block, shift_left, new_const, mode);
3132 new_shift = new_rd_Shl(dbg_shift, block, new_bitop, shift_right, mode);
3133 } else if (is_Shr(left)) {
3134 new_shift = new_rd_Shr(dbg_shift, block, new_bitop, shift_right, mode);
3136 assert(is_Rotl(left));
3137 new_shift = new_rd_Rotl(dbg_shift, block, new_bitop, shift_right, mode);
3146 static ir_node *transform_node_And(ir_node *n)
3148 ir_node *c, *oldn = n;
3149 ir_node *a = get_And_left(n);
3150 ir_node *b = get_And_right(n);
3152 vrp_attr *a_vrp, *b_vrp;
3154 if (is_Cmp(a) && is_Cmp(b)) {
3155 ir_node *a_left = get_Cmp_left(a);
3156 ir_node *a_right = get_Cmp_right(a);
3157 ir_node *b_left = get_Cmp_left(b);
3158 ir_node *b_right = get_Cmp_right(b);
3159 ir_relation a_relation = get_Cmp_relation(a);
3160 ir_relation b_relation = get_Cmp_relation(b);
3161 /* we can combine the relations of two compares with the same
3163 if (a_left == b_left && b_left == b_right) {
3164 dbg_info *dbgi = get_irn_dbg_info(n);
3165 ir_node *block = get_nodes_block(n);
3166 ir_relation new_relation = a_relation & b_relation;
3167 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3169 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3170 if (a_relation == b_relation && a_relation == ir_relation_equal
3171 && !mode_is_float(get_irn_mode(a_left))
3172 && !mode_is_float(get_irn_mode(b_left))) {
3173 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3174 dbg_info *dbgi = get_irn_dbg_info(n);
3175 ir_node *block = get_nodes_block(n);
3176 ir_mode *a_mode = get_irn_mode(a_left);
3177 ir_mode *b_mode = get_irn_mode(b_left);
3178 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3179 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3180 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3181 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3182 ir_graph *irg = get_irn_irg(n);
3183 ir_node *zero = create_zero_const(irg, b_mode);
3184 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3186 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3187 dbg_info *dbgi = get_irn_dbg_info(n);
3188 ir_node *block = get_nodes_block(n);
3189 ir_mode *a_mode = get_irn_mode(a_left);
3190 ir_mode *b_mode = get_irn_mode(b_left);
3191 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3192 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3193 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3194 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3195 ir_graph *irg = get_irn_irg(n);
3196 ir_node *zero = create_zero_const(irg, a_mode);
3197 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3202 mode = get_irn_mode(n);
3203 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3207 ir_node *op = get_Not_op(b);
3209 ir_node *ba = get_And_left(op);
3210 ir_node *bb = get_And_right(op);
3212 /* it's enough to test the following cases due to normalization! */
3213 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3214 /* (a|b) & ~(a&b) = a^b */
3215 ir_node *block = get_nodes_block(n);
3217 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3218 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3226 ir_node *op = get_Not_op(a);
3228 ir_node *aa = get_And_left(op);
3229 ir_node *ab = get_And_right(op);
3231 /* it's enough to test the following cases due to normalization! */
3232 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3233 /* (a|b) & ~(a&b) = a^b */
3234 ir_node *block = get_nodes_block(n);
3236 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3237 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3244 ir_node *al = get_Eor_left(a);
3245 ir_node *ar = get_Eor_right(a);
3248 /* (b ^ a) & b -> ~a & b */
3249 dbg_info *dbg = get_irn_dbg_info(n);
3250 ir_node *block = get_nodes_block(n);
3252 ar = new_rd_Not(dbg, block, ar, mode);
3253 n = new_rd_And(dbg, block, ar, b, mode);
3254 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3258 /* (a ^ b) & b -> ~a & b */
3259 dbg_info *dbg = get_irn_dbg_info(n);
3260 ir_node *block = get_nodes_block(n);
3262 al = new_rd_Not(dbg, block, al, mode);
3263 n = new_rd_And(dbg, block, al, b, mode);
3264 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3269 ir_node *bl = get_Eor_left(b);
3270 ir_node *br = get_Eor_right(b);
3273 /* a & (a ^ b) -> a & ~b */
3274 dbg_info *dbg = get_irn_dbg_info(n);
3275 ir_node *block = get_nodes_block(n);
3277 br = new_rd_Not(dbg, block, br, mode);
3278 n = new_rd_And(dbg, block, br, a, mode);
3279 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3283 /* a & (b ^ a) -> a & ~b */
3284 dbg_info *dbg = get_irn_dbg_info(n);
3285 ir_node *block = get_nodes_block(n);
3287 bl = new_rd_Not(dbg, block, bl, mode);
3288 n = new_rd_And(dbg, block, bl, a, mode);
3289 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3293 if (is_Not(a) && is_Not(b)) {
3294 /* ~a & ~b = ~(a|b) */
3295 ir_node *block = get_nodes_block(n);
3296 ir_mode *mode = get_irn_mode(n);
3300 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3301 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3302 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3306 b_vrp = vrp_get_info(b);
3307 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3308 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3314 a_vrp = vrp_get_info(a);
3315 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3316 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3320 n = transform_bitwise_distributive(n, transform_node_And);
3322 n = transform_node_bitop_shift(n);
3325 } /* transform_node_And */
3327 /* the order of the values is important! */
3328 typedef enum const_class {
3334 static const_class classify_const(const ir_node* n)
3336 if (is_Const(n)) return const_const;
3337 if (is_irn_constlike(n)) return const_like;
3342 * Determines whether r is more constlike or has a larger index (in that order)
3345 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3347 const const_class l_order = classify_const(l);
3348 const const_class r_order = classify_const(r);
3350 l_order > r_order ||
3351 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3357 static ir_node *transform_node_Eor(ir_node *n)
3359 ir_node *c, *oldn = n;
3360 ir_node *a = get_Eor_left(n);
3361 ir_node *b = get_Eor_right(n);
3362 ir_mode *mode = get_irn_mode(n);
3364 /* we can combine the relations of two compares with the same operands */
3365 if (is_Cmp(a) && is_Cmp(b)) {
3366 ir_node *a_left = get_Cmp_left(a);
3367 ir_node *a_right = get_Cmp_left(a);
3368 ir_node *b_left = get_Cmp_left(b);
3369 ir_node *b_right = get_Cmp_right(b);
3370 if (a_left == b_left && b_left == b_right) {
3371 dbg_info *dbgi = get_irn_dbg_info(n);
3372 ir_node *block = get_nodes_block(n);
3373 ir_relation a_relation = get_Cmp_relation(a);
3374 ir_relation b_relation = get_Cmp_relation(b);
3375 ir_relation new_relation = a_relation ^ b_relation;
3376 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3380 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3382 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3383 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3384 dbg_info *dbg = get_irn_dbg_info(n);
3385 ir_node *block = get_nodes_block(n);
3386 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3387 ir_node *new_left = get_Not_op(a);
3388 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3391 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3392 dbg_info *dbg = get_irn_dbg_info(n);
3393 ir_node *block = get_nodes_block(n);
3394 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3395 ir_node *new_right = get_Not_op(b);
3396 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3401 /* x ^ 1...1 -> ~1 */
3402 if (is_Const(b) && is_Const_all_one(b)) {
3403 n = new_r_Not(get_nodes_block(n), a, mode);
3404 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3408 if (mode_is_int(mode)) {
3409 vrp_attr *a_vrp = vrp_get_info(a);
3410 vrp_attr *b_vrp = vrp_get_info(b);
3411 if (a_vrp != NULL && b_vrp != NULL) {
3412 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
3414 if (tarval_is_null(vrp_val)) {
3415 dbg_info *dbgi = get_irn_dbg_info(n);
3416 return new_rd_Add(dbgi, get_nodes_block(n), a, b, mode);
3421 n = transform_bitwise_distributive(n, transform_node_Eor);
3423 n = transform_node_bitop_shift(n);
3426 } /* transform_node_Eor */
3431 static ir_node *transform_node_Not(ir_node *n)
3433 ir_node *c, *oldn = n;
3434 ir_node *a = get_Not_op(n);
3435 ir_mode *mode = get_irn_mode(n);
3437 HANDLE_UNOP_PHI(tarval_not,a,c);
3439 /* check for a boolean Not */
3441 dbg_info *dbgi = get_irn_dbg_info(a);
3442 ir_node *block = get_nodes_block(a);
3443 ir_relation relation = get_Cmp_relation(a);
3444 relation = get_negated_relation(relation);
3445 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3450 /* normalize ~(a ^ b) => a ^ ~b */
3452 dbg_info *dbg = get_irn_dbg_info(n);
3453 ir_node *block = get_nodes_block(n);
3454 ir_node *eor_right = get_Eor_right(a);
3455 ir_node *eor_left = get_Eor_left(a);
3456 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3457 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3461 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3462 if (is_Minus(a)) { /* ~-x -> x + -1 */
3463 dbg_info *dbg = get_irn_dbg_info(n);
3464 ir_graph *irg = get_irn_irg(n);
3465 ir_node *block = get_nodes_block(n);
3466 ir_node *add_l = get_Minus_op(a);
3467 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3468 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3469 } else if (is_Add(a)) {
3470 ir_node *add_r = get_Add_right(a);
3471 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3472 /* ~(x + -1) = -x */
3473 ir_node *op = get_Add_left(a);
3474 ir_node *blk = get_nodes_block(n);
3475 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3476 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3484 * Transform a Minus.
3488 * -(a >>u (size-1)) = a >>s (size-1)
3489 * -(a >>s (size-1)) = a >>u (size-1)
3490 * -(a * const) -> a * -const
3492 static ir_node *transform_node_Minus(ir_node *n)
3494 ir_node *c, *oldn = n;
3495 ir_node *a = get_Minus_op(n);
3498 HANDLE_UNOP_PHI(tarval_neg,a,c);
3500 mode = get_irn_mode(a);
3501 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3502 /* the following rules are only to twos-complement */
3505 ir_node *op = get_Not_op(a);
3506 ir_tarval *tv = get_mode_one(mode);
3507 ir_node *blk = get_nodes_block(n);
3508 ir_graph *irg = get_irn_irg(blk);
3509 ir_node *c = new_r_Const(irg, tv);
3510 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3511 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3515 ir_node *c = get_Shr_right(a);
3518 ir_tarval *tv = get_Const_tarval(c);
3520 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3521 /* -(a >>u (size-1)) = a >>s (size-1) */
3522 ir_node *v = get_Shr_left(a);
3524 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3525 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3531 ir_node *c = get_Shrs_right(a);
3534 ir_tarval *tv = get_Const_tarval(c);
3536 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3537 /* -(a >>s (size-1)) = a >>u (size-1) */
3538 ir_node *v = get_Shrs_left(a);
3540 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3541 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3548 /* - (a-b) = b - a */
3549 ir_node *la = get_Sub_left(a);
3550 ir_node *ra = get_Sub_right(a);
3551 ir_node *blk = get_nodes_block(n);
3553 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3554 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3558 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3559 ir_node *mul_l = get_Mul_left(a);
3560 ir_node *mul_r = get_Mul_right(a);
3561 ir_tarval *tv = value_of(mul_r);
3562 if (tv != tarval_bad) {
3563 tv = tarval_neg(tv);
3564 if (tv != tarval_bad) {
3565 ir_graph *irg = get_irn_irg(n);
3566 ir_node *cnst = new_r_Const(irg, tv);
3567 dbg_info *dbg = get_irn_dbg_info(a);
3568 ir_node *block = get_nodes_block(a);
3569 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3570 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3577 } /* transform_node_Minus */
3580 * Transform a Proj(Load) with a non-null address.
3582 static ir_node *transform_node_Proj_Load(ir_node *proj)
3584 if (get_opt_ldst_only_null_ptr_exceptions()) {
3585 if (get_irn_mode(proj) == mode_X) {
3586 ir_node *load = get_Proj_pred(proj);
3588 /* get the Load address */
3589 const ir_node *addr = get_Load_ptr(load);
3590 const ir_node *confirm;
3592 if (value_not_null(addr, &confirm)) {
3593 if (confirm == NULL) {
3594 /* this node may float if it did not depend on a Confirm */
3595 set_irn_pinned(load, op_pin_state_floats);
3597 if (get_Proj_proj(proj) == pn_Load_X_except) {
3598 ir_graph *irg = get_irn_irg(proj);
3599 DBG_OPT_EXC_REM(proj);
3600 return new_r_Bad(irg, mode_X);
3602 ir_node *blk = get_nodes_block(load);
3603 return new_r_Jmp(blk);
3609 } /* transform_node_Proj_Load */
3612 * Transform a Proj(Store) with a non-null address.
3614 static ir_node *transform_node_Proj_Store(ir_node *proj)
3616 if (get_opt_ldst_only_null_ptr_exceptions()) {
3617 if (get_irn_mode(proj) == mode_X) {
3618 ir_node *store = get_Proj_pred(proj);
3620 /* get the load/store address */
3621 const ir_node *addr = get_Store_ptr(store);
3622 const ir_node *confirm;
3624 if (value_not_null(addr, &confirm)) {
3625 if (confirm == NULL) {
3626 /* this node may float if it did not depend on a Confirm */
3627 set_irn_pinned(store, op_pin_state_floats);
3629 if (get_Proj_proj(proj) == pn_Store_X_except) {
3630 ir_graph *irg = get_irn_irg(proj);
3631 DBG_OPT_EXC_REM(proj);
3632 return new_r_Bad(irg, mode_X);
3634 ir_node *blk = get_nodes_block(store);
3635 return new_r_Jmp(blk);
3641 } /* transform_node_Proj_Store */
3644 * Transform a Proj(Div) with a non-zero value.
3645 * Removes the exceptions and routes the memory to the NoMem node.
3647 static ir_node *transform_node_Proj_Div(ir_node *proj)
3649 ir_node *div = get_Proj_pred(proj);
3650 ir_node *b = get_Div_right(div);
3651 ir_node *res, *new_mem;
3652 const ir_node *confirm;
3655 if (value_not_zero(b, &confirm)) {
3656 /* div(x, y) && y != 0 */
3657 if (confirm == NULL) {
3658 /* we are sure we have a Const != 0 */
3659 new_mem = get_Div_mem(div);
3660 new_mem = skip_Pin(new_mem);
3661 set_Div_mem(div, new_mem);
3662 set_irn_pinned(div, op_pin_state_floats);
3665 proj_nr = get_Proj_proj(proj);
3667 case pn_Div_X_regular:
3668 return new_r_Jmp(get_nodes_block(div));
3670 case pn_Div_X_except: {
3671 ir_graph *irg = get_irn_irg(proj);
3672 /* we found an exception handler, remove it */
3673 DBG_OPT_EXC_REM(proj);
3674 return new_r_Bad(irg, mode_X);
3678 ir_graph *irg = get_irn_irg(proj);
3679 res = get_Div_mem(div);
3680 new_mem = get_irg_no_mem(irg);
3683 /* This node can only float up to the Confirm block */
3684 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3686 set_irn_pinned(div, op_pin_state_floats);
3687 /* this is a Div without exception, we can remove the memory edge */
3688 set_Div_mem(div, new_mem);
3694 } /* transform_node_Proj_Div */
3697 * Transform a Proj(Mod) with a non-zero value.
3698 * Removes the exceptions and routes the memory to the NoMem node.
3700 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3702 ir_node *mod = get_Proj_pred(proj);
3703 ir_node *b = get_Mod_right(mod);
3704 ir_node *res, *new_mem;
3705 const ir_node *confirm;
3708 if (value_not_zero(b, &confirm)) {
3709 /* mod(x, y) && y != 0 */
3710 proj_nr = get_Proj_proj(proj);
3712 if (confirm == NULL) {
3713 /* we are sure we have a Const != 0 */
3714 new_mem = get_Mod_mem(mod);
3715 new_mem = skip_Pin(new_mem);
3716 set_Mod_mem(mod, new_mem);
3717 set_irn_pinned(mod, op_pin_state_floats);
3722 case pn_Mod_X_regular:
3723 return new_r_Jmp(get_irn_n(mod, -1));
3725 case pn_Mod_X_except: {
3726 ir_graph *irg = get_irn_irg(proj);
3727 /* we found an exception handler, remove it */
3728 DBG_OPT_EXC_REM(proj);
3729 return new_r_Bad(irg, mode_X);
3733 ir_graph *irg = get_irn_irg(proj);
3734 res = get_Mod_mem(mod);
3735 new_mem = get_irg_no_mem(irg);
3738 /* This node can only float up to the Confirm block */
3739 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3741 /* this is a Mod without exception, we can remove the memory edge */
3742 set_Mod_mem(mod, new_mem);
3746 if (get_Mod_left(mod) == b) {
3747 /* a % a = 0 if a != 0 */
3748 ir_graph *irg = get_irn_irg(proj);
3749 ir_mode *mode = get_irn_mode(proj);
3750 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3752 DBG_OPT_CSTEVAL(mod, res);
3758 } /* transform_node_Proj_Mod */
3761 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3763 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3765 ir_node *n = get_Proj_pred(proj);
3766 ir_node *b = get_Cond_selector(n);
3768 if (mode_is_int(get_irn_mode(b))) {
3769 ir_tarval *tb = value_of(b);
3771 if (tb != tarval_bad) {
3772 /* we have a constant switch */
3773 long num = get_Proj_proj(proj);
3775 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3776 if (get_tarval_long(tb) == num) {
3777 /* Do NOT create a jump here, or we will have 2 control flow ops
3778 * in a block. This case is optimized away in optimize_cf(). */
3781 ir_graph *irg = get_irn_irg(proj);
3782 /* this case will NEVER be taken, kill it */
3783 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3784 return new_r_Bad(irg, mode_X);
3788 long num = get_Proj_proj(proj);
3789 vrp_attr *b_vrp = vrp_get_info(b);
3790 if (num != get_Cond_default_proj(n) && b_vrp) {
3791 /* Try handling with vrp data. We only remove dead parts. */
3792 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3794 if (b_vrp->range_type == VRP_RANGE) {
3795 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3796 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3798 if ((cmp_result & ir_relation_greater) == cmp_result
3799 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3800 ir_graph *irg = get_irn_irg(proj);
3801 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3802 return new_r_Bad(irg, mode_X);
3804 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3805 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3806 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3808 if ((cmp_result & ir_relation_less_equal) == cmp_result
3809 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3810 ir_graph *irg = get_irn_irg(proj);
3811 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3812 return new_r_Bad(irg, mode_X);
3817 tarval_and( b_vrp->bits_set, tp),
3819 ) == ir_relation_equal)) {
3820 ir_graph *irg = get_irn_irg(proj);
3821 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3822 return new_r_Bad(irg, mode_X);
3828 tarval_not(b_vrp->bits_not_set)),
3829 tarval_not(b_vrp->bits_not_set))
3830 == ir_relation_equal)) {
3831 ir_graph *irg = get_irn_irg(proj);
3832 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3833 return new_r_Bad(irg, mode_X);
3842 * return true if the operation returns a value with exactly 1 bit set
3844 static bool is_single_bit(const ir_node *node)
3846 /* a first implementation, could be extended with vrp and others... */
3848 ir_node *shl_l = get_Shl_left(node);
3849 ir_mode *mode = get_irn_mode(node);
3850 int modulo = get_mode_modulo_shift(mode);
3851 /* this works if we shift a 1 and we have modulo shift */
3852 if (is_Const(shl_l) && is_Const_one(shl_l)
3853 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3856 } else if (is_Const(node)) {
3857 ir_tarval *tv = get_Const_tarval(node);
3858 return tarval_is_single_bit(tv);
3864 * Normalizes and optimizes Cmp nodes.
3866 static ir_node *transform_node_Cmp(ir_node *n)
3868 ir_node *left = get_Cmp_left(n);
3869 ir_node *right = get_Cmp_right(n);
3870 ir_mode *mode = get_irn_mode(left);
3871 ir_tarval *tv = NULL;
3872 bool changed = false;
3873 bool changedc = false;
3874 ir_relation relation = get_Cmp_relation(n);
3875 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3877 /* mask out impossible relations */
3878 ir_relation new_relation = relation & possible;
3879 if (new_relation != relation) {
3880 relation = new_relation;
3884 /* Remove unnecessary conversions */
3885 if (is_Conv(left) && is_Conv(right)) {
3886 ir_node *op_left = get_Conv_op(left);
3887 ir_node *op_right = get_Conv_op(right);
3888 ir_mode *mode_left = get_irn_mode(op_left);
3889 ir_mode *mode_right = get_irn_mode(op_right);
3891 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3892 && mode_left != mode_b && mode_right != mode_b) {
3893 ir_node *block = get_nodes_block(n);
3895 if (mode_left == mode_right) {
3899 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3900 } else if (smaller_mode(mode_left, mode_right)) {
3901 left = new_r_Conv(block, op_left, mode_right);
3904 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3905 } else if (smaller_mode(mode_right, mode_left)) {
3907 right = new_r_Conv(block, op_right, mode_left);
3909 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3911 mode = get_irn_mode(left);
3914 if (is_Conv(left) && is_Const(right)) {
3915 ir_node *op_left = get_Conv_op(left);
3916 ir_mode *mode_left = get_irn_mode(op_left);
3917 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3918 ir_tarval *tv = get_Const_tarval(right);
3919 tarval_int_overflow_mode_t last_mode
3920 = tarval_get_integer_overflow_mode();
3922 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3923 new_tv = tarval_convert_to(tv, mode_left);
3924 tarval_set_integer_overflow_mode(last_mode);
3925 if (new_tv != tarval_bad) {
3926 ir_graph *irg = get_irn_irg(n);
3928 right = new_r_Const(irg, new_tv);
3929 mode = get_irn_mode(left);
3931 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3937 * Optimize -a CMP -b into b CMP a.
3938 * This works only for modes where unary Minus cannot Overflow.
3939 * Note that two-complement integers can Overflow so it will NOT work.
3941 if (!mode_overflow_on_unary_Minus(mode) &&
3942 is_Minus(left) && is_Minus(right)) {
3943 left = get_Minus_op(left);
3944 right = get_Minus_op(right);
3945 relation = get_inversed_relation(relation);
3947 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3950 /* remove operation on both sides if possible */
3951 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3953 * The following operations are NOT safe for floating point operations, for instance
3954 * 1.0 + inf == 2.0 + inf, =/=> x == y
3956 if (mode_is_int(mode)) {
3957 unsigned lop = get_irn_opcode(left);
3959 if (lop == get_irn_opcode(right)) {
3960 ir_node *ll, *lr, *rl, *rr;
3962 /* same operation on both sides, try to remove */
3966 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3967 left = get_unop_op(left);
3968 right = get_unop_op(right);
3970 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3973 ll = get_Add_left(left);
3974 lr = get_Add_right(left);
3975 rl = get_Add_left(right);
3976 rr = get_Add_right(right);
3979 /* X + a CMP X + b ==> a CMP b */
3983 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3984 } else if (ll == rr) {
3985 /* X + a CMP b + X ==> a CMP b */
3989 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3990 } else if (lr == rl) {
3991 /* a + X CMP X + b ==> a CMP b */
3995 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3996 } else if (lr == rr) {
3997 /* a + X CMP b + X ==> a CMP b */
4001 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4005 ll = get_Sub_left(left);
4006 lr = get_Sub_right(left);
4007 rl = get_Sub_left(right);
4008 rr = get_Sub_right(right);
4011 /* X - a CMP X - b ==> a CMP b */
4015 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4016 } else if (lr == rr) {
4017 /* a - X CMP b - X ==> a CMP b */
4021 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4025 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4026 /* a ROTL X CMP b ROTL X ==> a CMP b */
4027 left = get_Rotl_left(left);
4028 right = get_Rotl_left(right);
4030 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4038 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4039 if (is_Add(left) || is_Sub(left)) {
4040 ir_node *ll = get_binop_left(left);
4041 ir_node *lr = get_binop_right(left);
4043 if (lr == right && is_Add(left)) {
4049 ir_graph *irg = get_irn_irg(n);
4051 right = create_zero_const(irg, mode);
4053 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4056 if (is_Add(right) || is_Sub(right)) {
4057 ir_node *rl = get_binop_left(right);
4058 ir_node *rr = get_binop_right(right);
4060 if (rr == left && is_Add(right)) {
4066 ir_graph *irg = get_irn_irg(n);
4068 right = create_zero_const(irg, mode);
4070 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4074 if (is_And(left) && is_Const(right)) {
4075 ir_node *ll = get_binop_left(left);
4076 ir_node *lr = get_binop_right(left);
4077 if (is_Shr(ll) && is_Const(lr)) {
4078 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4079 ir_node *block = get_nodes_block(n);
4080 ir_mode *mode = get_irn_mode(left);
4082 ir_node *llr = get_Shr_right(ll);
4083 if (is_Const(llr)) {
4084 dbg_info *dbg = get_irn_dbg_info(left);
4085 ir_graph *irg = get_irn_irg(left);
4087 ir_tarval *c1 = get_Const_tarval(llr);
4088 ir_tarval *c2 = get_Const_tarval(lr);
4089 ir_tarval *c3 = get_Const_tarval(right);
4090 ir_tarval *mask = tarval_shl(c2, c1);
4091 ir_tarval *value = tarval_shl(c3, c1);
4093 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4094 right = new_r_Const(irg, value);
4099 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4101 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4102 right = get_Eor_right(left);
4103 left = get_Eor_left(left);
4106 } /* mode_is_int(...) */
4109 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4110 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4111 if (mode_is_int(mode) && is_And(left)
4112 && (relation == ir_relation_equal
4113 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4114 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4115 ir_node *and0 = get_And_left(left);
4116 ir_node *and1 = get_And_right(left);
4117 if (and1 == right) {
4118 ir_node *tmp = and0;
4122 if (and0 == right && is_single_bit(and0)) {
4123 ir_graph *irg = get_irn_irg(n);
4125 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4126 right = create_zero_const(irg, mode);
4131 /* replace mode_b compares with ands/ors */
4132 if (mode == mode_b) {
4133 ir_node *block = get_nodes_block(n);
4137 case ir_relation_less_equal:
4138 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4140 case ir_relation_less:
4141 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4143 case ir_relation_greater_equal:
4144 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4146 case ir_relation_greater:
4147 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4149 case ir_relation_less_greater:
4150 bres = new_r_Eor(block, left, right, mode_b);
4152 case ir_relation_equal:
4153 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4156 #ifdef DEBUG_libfirm
4157 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4162 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4168 * First step: normalize the compare op
4169 * by placing the constant on the right side
4170 * or moving the lower address node to the left.
4172 if (!operands_are_normalized(left, right)) {
4177 relation = get_inversed_relation(relation);
4182 * Second step: Try to reduce the magnitude
4183 * of a constant. This may help to generate better code
4184 * later and may help to normalize more compares.
4185 * Of course this is only possible for integer values.
4187 tv = value_of(right);
4188 if (tv != tarval_bad) {
4189 ir_mode *mode = get_irn_mode(right);
4191 /* TODO extend to arbitrary constants */
4192 if (is_Conv(left) && tarval_is_null(tv)) {
4193 ir_node *op = get_Conv_op(left);
4194 ir_mode *op_mode = get_irn_mode(op);
4197 * UpConv(x) REL 0 ==> x REL 0
4198 * Don't do this for float values as it's unclear whether it is a
4199 * win. (on the other side it makes detection/creation of fabs hard)
4201 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4202 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4203 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4204 !mode_is_float(mode)) {
4205 tv = get_mode_null(op_mode);
4209 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4213 if (tv != tarval_bad) {
4214 /* the following optimization is possible on modes without Overflow
4215 * on Unary Minus or on == and !=:
4216 * -a CMP c ==> a swap(CMP) -c
4218 * Beware: for two-complement Overflow may occur, so only == and != can
4219 * be optimized, see this:
4220 * -MININT < 0 =/=> MININT > 0 !!!
4222 if (is_Minus(left) &&
4223 (!mode_overflow_on_unary_Minus(mode) ||
4224 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4225 tv = tarval_neg(tv);
4227 if (tv != tarval_bad) {
4228 left = get_Minus_op(left);
4229 relation = get_inversed_relation(relation);
4231 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4233 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4234 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4235 tv = tarval_not(tv);
4237 if (tv != tarval_bad) {
4238 left = get_Not_op(left);
4240 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4244 /* for integer modes, we have more */
4245 if (mode_is_int(mode) && !is_Const(left)) {
4246 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4247 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4248 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4249 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4251 if (tv != tarval_bad) {
4252 relation ^= ir_relation_equal;
4254 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4257 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4258 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4259 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4260 tv = tarval_add(tv, get_mode_one(mode));
4262 if (tv != tarval_bad) {
4263 relation ^= ir_relation_equal;
4265 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4269 /* the following reassociations work only for == and != */
4270 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4271 if (tv != tarval_bad) {
4272 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4274 ir_node *c1 = get_Sub_right(left);
4275 ir_tarval *tv2 = value_of(c1);
4277 if (tv2 != tarval_bad) {
4278 tv2 = tarval_add(tv, value_of(c1));
4280 if (tv2 != tarval_bad) {
4281 left = get_Sub_left(left);
4284 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4288 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4289 else if (is_Add(left)) {
4290 ir_node *a_l = get_Add_left(left);
4291 ir_node *a_r = get_Add_right(left);
4295 if (is_Const(a_l)) {
4297 tv2 = value_of(a_l);
4300 tv2 = value_of(a_r);
4303 if (tv2 != tarval_bad) {
4304 tv2 = tarval_sub(tv, tv2, NULL);
4306 if (tv2 != tarval_bad) {
4310 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4314 /* -a == c ==> a == -c, -a != c ==> a != -c */
4315 else if (is_Minus(left)) {
4316 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4318 if (tv2 != tarval_bad) {
4319 left = get_Minus_op(left);
4322 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4329 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4330 switch (get_irn_opcode(left)) {
4334 c1 = get_And_right(left);
4337 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4338 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4340 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4342 /* TODO: move to constant evaluation */
4343 ir_graph *irg = get_irn_irg(n);
4344 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4345 c1 = new_r_Const(irg, tv);
4346 DBG_OPT_CSTEVAL(n, c1);
4350 if (tarval_is_single_bit(tv)) {
4352 * optimization for AND:
4354 * And(x, C) == C ==> And(x, C) != 0
4355 * And(x, C) != C ==> And(X, C) == 0
4357 * if C is a single Bit constant.
4360 /* check for Constant's match. We have check hare the tarvals,
4361 because our const might be changed */
4362 if (get_Const_tarval(c1) == tv) {
4363 /* fine: do the transformation */
4364 tv = get_mode_null(get_tarval_mode(tv));
4365 relation ^= ir_relation_less_equal_greater;
4367 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4373 c1 = get_Or_right(left);
4374 if (is_Const(c1) && tarval_is_null(tv)) {
4376 * Or(x, C) == 0 && C != 0 ==> FALSE
4377 * Or(x, C) != 0 && C != 0 ==> TRUE
4379 if (! tarval_is_null(get_Const_tarval(c1))) {
4380 /* TODO: move to constant evaluation */
4381 ir_graph *irg = get_irn_irg(n);
4382 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4383 c1 = new_r_Const(irg, tv);
4384 DBG_OPT_CSTEVAL(n, c1);
4391 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4393 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4396 c1 = get_Shl_right(left);
4398 ir_graph *irg = get_irn_irg(c1);
4399 ir_tarval *tv1 = get_Const_tarval(c1);
4400 ir_mode *mode = get_irn_mode(left);
4401 ir_tarval *minus1 = get_mode_all_one(mode);
4402 ir_tarval *amask = tarval_shr(minus1, tv1);
4403 ir_tarval *cmask = tarval_shl(minus1, tv1);
4406 if (tarval_and(tv, cmask) != tv) {
4407 /* condition not met */
4408 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4409 c1 = new_r_Const(irg, tv);
4410 DBG_OPT_CSTEVAL(n, c1);
4413 sl = get_Shl_left(left);
4414 blk = get_nodes_block(n);
4415 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4416 tv = tarval_shr(tv, tv1);
4418 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4423 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4425 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4428 c1 = get_Shr_right(left);
4430 ir_graph *irg = get_irn_irg(c1);
4431 ir_tarval *tv1 = get_Const_tarval(c1);
4432 ir_mode *mode = get_irn_mode(left);
4433 ir_tarval *minus1 = get_mode_all_one(mode);
4434 ir_tarval *amask = tarval_shl(minus1, tv1);
4435 ir_tarval *cmask = tarval_shr(minus1, tv1);
4438 if (tarval_and(tv, cmask) != tv) {
4439 /* condition not met */
4440 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4441 c1 = new_r_Const(irg, tv);
4442 DBG_OPT_CSTEVAL(n, c1);
4445 sl = get_Shr_left(left);
4446 blk = get_nodes_block(n);
4447 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4448 tv = tarval_shl(tv, tv1);
4450 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4455 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4457 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4460 c1 = get_Shrs_right(left);
4462 ir_graph *irg = get_irn_irg(c1);
4463 ir_tarval *tv1 = get_Const_tarval(c1);
4464 ir_mode *mode = get_irn_mode(left);
4465 ir_tarval *minus1 = get_mode_all_one(mode);
4466 ir_tarval *amask = tarval_shl(minus1, tv1);
4467 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4470 cond = tarval_sub(cond, tv1, NULL);
4471 cond = tarval_shrs(tv, cond);
4473 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4474 /* condition not met */
4475 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4476 c1 = new_r_Const(irg, tv);
4477 DBG_OPT_CSTEVAL(n, c1);
4480 sl = get_Shrs_left(left);
4481 blk = get_nodes_block(n);
4482 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4483 tv = tarval_shl(tv, tv1);
4485 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4490 } /* tarval != bad */
4493 if (changedc) { /* need a new Const */
4494 ir_graph *irg = get_irn_irg(n);
4495 right = new_r_Const(irg, tv);
4499 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4500 ir_node *op = get_Proj_pred(left);
4502 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4503 ir_node *c = get_binop_right(op);
4506 ir_tarval *tv = get_Const_tarval(c);
4508 if (tarval_is_single_bit(tv)) {
4509 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4510 ir_node *v = get_binop_left(op);
4511 ir_node *blk = get_irn_n(op, -1);
4512 ir_graph *irg = get_irn_irg(op);
4513 ir_mode *mode = get_irn_mode(v);
4515 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4516 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4518 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4525 dbg_info *dbgi = get_irn_dbg_info(n);
4526 ir_node *block = get_nodes_block(n);
4528 /* create a new compare */
4529 n = new_rd_Cmp(dbgi, block, left, right, relation);
4536 * Optimize CopyB(mem, x, x) into a Nop.
4538 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4540 ir_node *copyb = get_Proj_pred(proj);
4541 ir_node *a = get_CopyB_dst(copyb);
4542 ir_node *b = get_CopyB_src(copyb);
4545 switch (get_Proj_proj(proj)) {
4546 case pn_CopyB_X_regular:
4547 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4548 DBG_OPT_EXC_REM(proj);
4549 proj = new_r_Jmp(get_nodes_block(copyb));
4551 case pn_CopyB_X_except: {
4552 ir_graph *irg = get_irn_irg(proj);
4553 DBG_OPT_EXC_REM(proj);
4554 proj = new_r_Bad(irg, mode_X);
4562 } /* transform_node_Proj_CopyB */
4565 * Optimize Bounds(idx, idx, upper) into idx.
4567 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4569 ir_node *oldn = proj;
4570 ir_node *bound = get_Proj_pred(proj);
4571 ir_node *idx = get_Bound_index(bound);
4572 ir_node *pred = skip_Proj(idx);
4575 if (idx == get_Bound_lower(bound))
4577 else if (is_Bound(pred)) {
4579 * idx was Bounds checked previously, it is still valid if
4580 * lower <= pred_lower && pred_upper <= upper.
4582 ir_node *lower = get_Bound_lower(bound);
4583 ir_node *upper = get_Bound_upper(bound);
4584 if (get_Bound_lower(pred) == lower &&
4585 get_Bound_upper(pred) == upper) {
4587 * One could expect that we simply return the previous
4588 * Bound here. However, this would be wrong, as we could
4589 * add an exception Proj to a new location then.
4590 * So, we must turn in into a tuple.
4596 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4597 switch (get_Proj_proj(proj)) {
4599 DBG_OPT_EXC_REM(proj);
4600 proj = get_Bound_mem(bound);
4602 case pn_Bound_X_except:
4603 DBG_OPT_EXC_REM(proj);
4604 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4608 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4610 case pn_Bound_X_regular:
4611 DBG_OPT_EXC_REM(proj);
4612 proj = new_r_Jmp(get_nodes_block(bound));
4619 } /* transform_node_Proj_Bound */
4622 * Does all optimizations on nodes that must be done on its Projs
4623 * because of creating new nodes.
4625 static ir_node *transform_node_Proj(ir_node *proj)
4627 ir_node *n = get_Proj_pred(proj);
4629 if (n->op->ops.transform_node_Proj)
4630 return n->op->ops.transform_node_Proj(proj);
4632 } /* transform_node_Proj */
4635 * Test whether a block is unreachable
4636 * Note: That this only returns true when
4637 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
4638 * This is important, as you easily end up producing invalid constructs in the
4639 * unreachable code when optimizing away edges into the unreachable code.
4640 * So only set this flag when you iterate localopts to the fixpoint.
4641 * When you reach the fixpoint then all unreachable code is dead
4642 * (= can't be reached by firm edges) and you won't see the invalid constructs
4645 static bool is_block_unreachable(const ir_node *block)
4647 const ir_graph *irg = get_irn_irg(block);
4648 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4650 return get_Block_dom_depth(block) < 0;
4653 static ir_node *transform_node_Block(ir_node *block)
4655 ir_graph *irg = get_irn_irg(block);
4656 int arity = get_irn_arity(block);
4657 ir_node *bad = NULL;
4660 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4663 for (i = 0; i < arity; ++i) {
4664 ir_node *const pred = get_Block_cfgpred(block, i);
4665 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4668 bad = new_r_Bad(irg, mode_X);
4669 set_irn_n(block, i, bad);
4675 static ir_node *transform_node_Phi(ir_node *phi)
4677 int n = get_irn_arity(phi);
4678 ir_mode *mode = get_irn_mode(phi);
4679 ir_node *block = get_nodes_block(phi);
4680 ir_graph *irg = get_irn_irg(phi);
4681 ir_node *bad = NULL;
4684 /* Set phi-operands for bad-block inputs to bad */
4685 for (i = 0; i < n; ++i) {
4686 if (!is_Bad(get_Phi_pred(phi, i))) {
4687 ir_node *pred = get_Block_cfgpred(block, i);
4688 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4690 bad = new_r_Bad(irg, mode);
4691 set_irn_n(phi, i, bad);
4696 /* Move Pin nodes down through Phi nodes. */
4697 if (mode == mode_M) {
4698 n = get_irn_arity(phi);
4700 /* Beware of Phi0 */
4704 bool has_pin = false;
4706 NEW_ARR_A(ir_node *, in, n);
4708 for (i = 0; i < n; ++i) {
4709 ir_node *pred = get_irn_n(phi, i);
4712 in[i] = get_Pin_op(pred);
4714 } else if (is_Bad(pred)) {
4724 /* Move the Pin nodes "behind" the Phi. */
4725 block = get_irn_n(phi, -1);
4726 new_phi = new_r_Phi(block, n, in, mode_M);
4727 return new_r_Pin(block, new_phi);
4730 /* Move Confirms down through Phi nodes. */
4731 else if (mode_is_reference(mode)) {
4732 n = get_irn_arity(phi);
4734 /* Beware of Phi0 */
4736 ir_node *pred = get_irn_n(phi, 0);
4737 ir_node *bound, *new_phi, *block, **in;
4738 ir_relation relation;
4739 bool has_confirm = false;
4741 if (! is_Confirm(pred))
4744 bound = get_Confirm_bound(pred);
4745 relation = get_Confirm_relation(pred);
4747 NEW_ARR_A(ir_node *, in, n);
4748 in[0] = get_Confirm_value(pred);
4750 for (i = 1; i < n; ++i) {
4751 pred = get_irn_n(phi, i);
4753 if (is_Confirm(pred) &&
4754 get_Confirm_bound(pred) == bound &&
4755 get_Confirm_relation(pred) == relation) {
4756 in[i] = get_Confirm_value(pred);
4758 } else if (is_Bad(pred)) {
4768 /* move the Confirm nodes "behind" the Phi */
4769 block = get_irn_n(phi, -1);
4770 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4771 return new_r_Confirm(block, new_phi, bound, relation);
4778 * Returns the operands of a commutative bin-op, if one operand is
4779 * a const, it is returned as the second one.
4781 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4783 ir_node *op_a = get_binop_left(binop);
4784 ir_node *op_b = get_binop_right(binop);
4786 assert(is_op_commutative(get_irn_op(binop)));
4788 if (is_Const(op_a)) {
4795 } /* get_comm_Binop_Ops */
4798 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4799 * Such pattern may arise in bitfield stores.
4801 * value c4 value c4 & c2
4802 * AND c3 AND c1 | c3
4809 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4812 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4814 ir_node *irn_and, *c1;
4816 ir_node *and_l, *c3;
4817 ir_node *value, *c4;
4818 ir_node *new_and, *new_const, *block;
4819 ir_mode *mode = get_irn_mode(irn_or);
4821 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4825 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4826 if (!is_Const(c1) || !is_And(irn_and))
4829 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4833 tv1 = get_Const_tarval(c1);
4834 tv2 = get_Const_tarval(c2);
4836 tv = tarval_or(tv1, tv2);
4837 if (tarval_is_all_one(tv)) {
4838 /* the AND does NOT clear a bit with isn't set by the OR */
4839 set_Or_left(irn_or, or_l);
4840 set_Or_right(irn_or, c1);
4842 /* check for more */
4849 get_comm_Binop_Ops(or_l, &and_l, &c3);
4850 if (!is_Const(c3) || !is_And(and_l))
4853 get_comm_Binop_Ops(and_l, &value, &c4);
4857 /* ok, found the pattern, check for conditions */
4858 assert(mode == get_irn_mode(irn_and));
4859 assert(mode == get_irn_mode(or_l));
4860 assert(mode == get_irn_mode(and_l));
4862 tv3 = get_Const_tarval(c3);
4863 tv4 = get_Const_tarval(c4);
4865 tv = tarval_or(tv4, tv2);
4866 if (!tarval_is_all_one(tv)) {
4867 /* have at least one 0 at the same bit position */
4871 if (tv3 != tarval_andnot(tv3, tv4)) {
4872 /* bit in the or_mask is outside the and_mask */
4876 if (tv1 != tarval_andnot(tv1, tv2)) {
4877 /* bit in the or_mask is outside the and_mask */
4881 /* ok, all conditions met */
4882 block = get_irn_n(irn_or, -1);
4883 irg = get_irn_irg(block);
4885 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4887 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4889 set_Or_left(irn_or, new_and);
4890 set_Or_right(irn_or, new_const);
4892 /* check for more */
4894 } /* transform_node_Or_bf_store */
4897 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4899 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4901 ir_mode *mode = get_irn_mode(irn_or);
4902 ir_node *shl, *shr, *block;
4903 ir_node *irn, *x, *c1, *c2, *n;
4904 ir_tarval *tv1, *tv2;
4906 /* some backends can't handle rotl */
4907 if (!be_get_backend_param()->support_rotl)
4910 if (! mode_is_int(mode))
4913 shl = get_binop_left(irn_or);
4914 shr = get_binop_right(irn_or);
4923 } else if (!is_Shl(shl)) {
4925 } else if (!is_Shr(shr)) {
4928 x = get_Shl_left(shl);
4929 if (x != get_Shr_left(shr))
4932 c1 = get_Shl_right(shl);
4933 c2 = get_Shr_right(shr);
4934 if (is_Const(c1) && is_Const(c2)) {
4935 tv1 = get_Const_tarval(c1);
4936 if (! tarval_is_long(tv1))
4939 tv2 = get_Const_tarval(c2);
4940 if (! tarval_is_long(tv2))
4943 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4944 != (int) get_mode_size_bits(mode))
4947 /* yet, condition met */
4948 block = get_nodes_block(irn_or);
4950 n = new_r_Rotl(block, x, c1, mode);
4952 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4956 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4957 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4958 if (!ir_is_negated_value(c1, c2)) {
4962 /* yet, condition met */
4963 block = get_nodes_block(irn_or);
4964 n = new_r_Rotl(block, x, c1, mode);
4965 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4967 } /* transform_node_Or_Rotl */
4969 static bool is_cmp_unequal(const ir_node *node)
4971 ir_relation relation = get_Cmp_relation(node);
4972 ir_node *left = get_Cmp_left(node);
4973 ir_node *right = get_Cmp_right(node);
4974 ir_mode *mode = get_irn_mode(left);
4976 if (relation == ir_relation_less_greater)
4979 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
4980 return relation == ir_relation_greater;
4985 * returns true for Cmp(x == 0) or Cmp(x != 0)
4987 static bool is_cmp_equality_zero(const ir_node *node)
4989 ir_relation relation;
4990 ir_node *right = get_Cmp_right(node);
4992 if (!is_Const(right) || !is_Const_null(right))
4994 relation = get_Cmp_relation(node);
4995 return relation == ir_relation_equal
4996 || relation == ir_relation_less_greater
4997 || (!mode_is_signed(get_irn_mode(right))
4998 && relation == ir_relation_greater);
5004 static ir_node *transform_node_Or(ir_node *n)
5006 ir_node *c, *oldn = n;
5007 ir_node *a = get_Or_left(n);
5008 ir_node *b = get_Or_right(n);
5011 if (is_Not(a) && is_Not(b)) {
5012 /* ~a | ~b = ~(a&b) */
5013 ir_node *block = get_nodes_block(n);
5015 mode = get_irn_mode(n);
5018 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5019 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5020 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5024 /* we can combine the relations of two compares with the same operands */
5025 if (is_Cmp(a) && is_Cmp(b)) {
5026 ir_node *a_left = get_Cmp_left(a);
5027 ir_node *a_right = get_Cmp_right(a);
5028 ir_node *b_left = get_Cmp_left(b);
5029 ir_node *b_right = get_Cmp_right(b);
5030 if (a_left == b_left && b_left == b_right) {
5031 dbg_info *dbgi = get_irn_dbg_info(n);
5032 ir_node *block = get_nodes_block(n);
5033 ir_relation a_relation = get_Cmp_relation(a);
5034 ir_relation b_relation = get_Cmp_relation(b);
5035 ir_relation new_relation = a_relation | b_relation;
5036 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
5038 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
5039 if (is_cmp_unequal(a) && is_cmp_unequal(b)
5040 && !mode_is_float(get_irn_mode(a_left))
5041 && !mode_is_float(get_irn_mode(b_left))) {
5042 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
5043 ir_graph *irg = get_irn_irg(n);
5044 dbg_info *dbgi = get_irn_dbg_info(n);
5045 ir_node *block = get_nodes_block(n);
5046 ir_mode *a_mode = get_irn_mode(a_left);
5047 ir_mode *b_mode = get_irn_mode(b_left);
5048 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5049 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5050 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
5051 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
5052 ir_node *zero = create_zero_const(irg, b_mode);
5053 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5055 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
5056 ir_graph *irg = get_irn_irg(n);
5057 dbg_info *dbgi = get_irn_dbg_info(n);
5058 ir_node *block = get_nodes_block(n);
5059 ir_mode *a_mode = get_irn_mode(a_left);
5060 ir_mode *b_mode = get_irn_mode(b_left);
5061 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5062 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5063 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
5064 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
5065 ir_node *zero = create_zero_const(irg, a_mode);
5066 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5071 mode = get_irn_mode(n);
5072 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5074 n = transform_node_Or_bf_store(n);
5075 n = transform_node_Or_Rotl(n);
5079 n = transform_bitwise_distributive(n, transform_node_Or);
5081 n = transform_node_bitop_shift(n);
5085 if (mode_is_int(mode)) {
5086 vrp_attr *a_vrp = vrp_get_info(a);
5087 vrp_attr *b_vrp = vrp_get_info(b);
5088 if (a_vrp != NULL && b_vrp != NULL) {
5089 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
5091 if (tarval_is_null(vrp_val)) {
5092 dbg_info *dbgi = get_irn_dbg_info(n);
5093 return new_rd_Add(dbgi, get_nodes_block(n), a, b, mode);
5099 } /* transform_node_Or */
5103 static ir_node *transform_node(ir_node *n);
5106 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5108 * Should be moved to reassociation?
5110 static ir_node *transform_node_shift(ir_node *n)
5112 ir_node *left, *right;
5114 ir_mode *count_mode;
5115 ir_tarval *tv1, *tv2, *res;
5116 ir_node *in[2], *irn, *block;
5120 left = get_binop_left(n);
5122 /* different operations */
5123 if (get_irn_op(left) != get_irn_op(n))
5126 right = get_binop_right(n);
5127 tv1 = value_of(right);
5128 if (tv1 == tarval_bad)
5131 tv2 = value_of(get_binop_right(left));
5132 if (tv2 == tarval_bad)
5135 count_mode = get_tarval_mode(tv1);
5136 if (get_tarval_mode(tv2) != count_mode) {
5137 /* TODO: search bigger mode or something and convert... */
5141 mode = get_irn_mode(n);
5142 modulo_shf = get_mode_modulo_shift(mode);
5144 if (modulo_shf > 0) {
5145 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5147 /* I'm not so sure what happens in one complement... */
5148 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5149 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5150 * above will be invalid) */
5151 assert(modulo_shf<=0 || is_po2(modulo_shf));
5153 tv1 = tarval_and(tv1, modulo_mask);
5154 tv2 = tarval_and(tv2, modulo_mask);
5156 res = tarval_add(tv1, tv2);
5157 irg = get_irn_irg(n);
5159 /* beware: a simple replacement works only, if res < modulo shift */
5161 int bits = get_mode_size_bits(mode);
5162 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5163 res = tarval_mod(res, modulo);
5165 long bits = get_mode_size_bits(mode);
5166 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5168 /* shifting too much */
5169 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5171 ir_node *block = get_nodes_block(n);
5172 dbg_info *dbgi = get_irn_dbg_info(n);
5173 ir_mode *smode = get_irn_mode(right);
5174 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5175 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5178 return new_r_Const(irg, get_mode_null(mode));
5182 /* ok, we can replace it */
5183 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5184 block = get_nodes_block(n);
5186 in[0] = get_binop_left(left);
5187 in[1] = new_r_Const(irg, res);
5189 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5191 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5193 return transform_node(irn);
5198 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5200 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5201 * (also with x >>s c1 when c1>=c2)
5203 static ir_node *transform_node_shl_shr(ir_node *n)
5206 ir_node *right = get_binop_right(n);
5216 ir_tarval *tv_shift;
5219 ir_relation relation;
5222 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5224 if (!is_Const(right))
5227 left = get_binop_left(n);
5228 mode = get_irn_mode(n);
5229 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5230 ir_node *shr_right = get_binop_right(left);
5232 if (!is_Const(shr_right))
5235 x = get_binop_left(left);
5236 tv_shr = get_Const_tarval(shr_right);
5237 tv_shl = get_Const_tarval(right);
5239 if (is_Shrs(left)) {
5240 /* shrs variant only allowed if c1 >= c2 */
5241 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5244 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5247 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5249 tv_mask = tarval_shl(tv_mask, tv_shl);
5250 } else if (is_Shr(n) && is_Shl(left)) {
5251 ir_node *shl_right = get_Shl_right(left);
5253 if (!is_Const(shl_right))
5256 x = get_Shl_left(left);
5257 tv_shr = get_Const_tarval(right);
5258 tv_shl = get_Const_tarval(shl_right);
5260 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5261 tv_mask = tarval_shr(tv_mask, tv_shr);
5266 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5267 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5270 assert(tv_mask != tarval_bad);
5271 assert(get_tarval_mode(tv_mask) == mode);
5273 block = get_nodes_block(n);
5274 irg = get_irn_irg(block);
5275 dbgi = get_irn_dbg_info(n);
5277 relation = tarval_cmp(tv_shl, tv_shr);
5278 if (relation == ir_relation_less || relation == ir_relation_equal) {
5279 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5280 new_const = new_r_Const(irg, tv_shift);
5282 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5284 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5287 assert(relation == ir_relation_greater);
5288 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5289 new_const = new_r_Const(irg, tv_shift);
5290 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5293 new_const = new_r_Const(irg, tv_mask);
5294 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5299 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5301 ir_mode *mode = get_tarval_mode(tv);
5302 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5303 return tarval_mod(tv, modulo_tv);
5306 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5307 ir_node *left, ir_node *right, ir_mode *mode);
5310 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5311 * then we can use that to minimize the value of Add(x, const) or
5312 * Sub(Const, x). In particular this often avoids 1 instruction in some
5313 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5314 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5316 static ir_node *transform_node_shift_modulo(ir_node *n,
5317 new_shift_func new_shift)
5319 ir_mode *mode = get_irn_mode(n);
5320 int modulo = get_mode_modulo_shift(mode);
5321 ir_node *newop = NULL;
5322 ir_mode *mode_right;
5329 if (get_mode_arithmetic(mode) != irma_twos_complement)
5331 if (!is_po2(modulo))
5334 irg = get_irn_irg(n);
5335 block = get_nodes_block(n);
5336 right = get_binop_right(n);
5337 mode_right = get_irn_mode(right);
5338 if (is_Const(right)) {
5339 ir_tarval *tv = get_Const_tarval(right);
5340 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5345 newop = new_r_Const(irg, tv_mod);
5346 } else if (is_Add(right)) {
5347 ir_node *add_right = get_Add_right(right);
5348 if (is_Const(add_right)) {
5349 ir_tarval *tv = get_Const_tarval(add_right);
5350 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5355 newconst = new_r_Const(irg, tv_mod);
5356 newop = new_r_Add(block, get_Add_left(right), newconst,
5359 } else if (is_Sub(right)) {
5360 ir_node *sub_left = get_Sub_left(right);
5361 if (is_Const(sub_left)) {
5362 ir_tarval *tv = get_Const_tarval(sub_left);
5363 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5368 newconst = new_r_Const(irg, tv_mod);
5369 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5376 if (newop != NULL) {
5377 dbg_info *dbgi = get_irn_dbg_info(n);
5378 ir_node *left = get_binop_left(n);
5379 return new_shift(dbgi, block, left, newop, mode);
5387 static ir_node *transform_node_Shr(ir_node *n)
5389 ir_node *c, *oldn = n;
5390 ir_node *left = get_Shr_left(n);
5391 ir_node *right = get_Shr_right(n);
5392 ir_mode *mode = get_irn_mode(n);
5394 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5395 n = transform_node_shift(n);
5398 n = transform_node_shift_modulo(n, new_rd_Shr);
5400 n = transform_node_shl_shr(n);
5402 n = transform_node_shift_bitop(n);
5405 } /* transform_node_Shr */
5410 static ir_node *transform_node_Shrs(ir_node *n)
5412 ir_node *c, *oldn = n;
5413 ir_node *a = get_Shrs_left(n);
5414 ir_node *b = get_Shrs_right(n);
5415 ir_mode *mode = get_irn_mode(n);
5417 if (is_oversize_shift(n)) {
5418 ir_node *block = get_nodes_block(n);
5419 dbg_info *dbgi = get_irn_dbg_info(n);
5420 ir_mode *cmode = get_irn_mode(b);
5421 long val = get_mode_size_bits(cmode)-1;
5422 ir_graph *irg = get_irn_irg(n);
5423 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5424 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5427 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5428 n = transform_node_shift(n);
5431 n = transform_node_shift_modulo(n, new_rd_Shrs);
5433 n = transform_node_shift_bitop(n);
5436 } /* transform_node_Shrs */
5441 static ir_node *transform_node_Shl(ir_node *n)
5443 ir_node *c, *oldn = n;
5444 ir_node *a = get_Shl_left(n);
5445 ir_node *b = get_Shl_right(n);
5446 ir_mode *mode = get_irn_mode(n);
5448 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5449 n = transform_node_shift(n);
5452 n = transform_node_shift_modulo(n, new_rd_Shl);
5454 n = transform_node_shl_shr(n);
5456 n = transform_node_shift_bitop(n);
5459 } /* transform_node_Shl */
5464 static ir_node *transform_node_Rotl(ir_node *n)
5466 ir_node *c, *oldn = n;
5467 ir_node *a = get_Rotl_left(n);
5468 ir_node *b = get_Rotl_right(n);
5469 ir_mode *mode = get_irn_mode(n);
5471 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5472 n = transform_node_shift(n);
5475 n = transform_node_shift_bitop(n);
5478 } /* transform_node_Rotl */
5483 static ir_node *transform_node_Conv(ir_node *n)
5485 ir_node *c, *oldn = n;
5486 ir_mode *mode = get_irn_mode(n);
5487 ir_node *a = get_Conv_op(n);
5489 if (mode != mode_b && is_const_Phi(a)) {
5490 /* Do NOT optimize mode_b Conv's, this leads to remaining
5491 * Phib nodes later, because the conv_b_lower operation
5492 * is instantly reverted, when it tries to insert a Convb.
5494 c = apply_conv_on_phi(a, mode);
5496 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5501 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5502 ir_graph *irg = get_irn_irg(n);
5503 return new_r_Unknown(irg, mode);
5506 if (mode_is_reference(mode) &&
5507 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5509 ir_node *l = get_Add_left(a);
5510 ir_node *r = get_Add_right(a);
5511 dbg_info *dbgi = get_irn_dbg_info(a);
5512 ir_node *block = get_nodes_block(n);
5514 ir_node *lop = get_Conv_op(l);
5515 if (get_irn_mode(lop) == mode) {
5516 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5517 n = new_rd_Add(dbgi, block, lop, r, mode);
5522 ir_node *rop = get_Conv_op(r);
5523 if (get_irn_mode(rop) == mode) {
5524 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5525 n = new_rd_Add(dbgi, block, l, rop, mode);
5532 } /* transform_node_Conv */
5535 * Remove dead blocks and nodes in dead blocks
5536 * in keep alive list. We do not generate a new End node.
5538 static ir_node *transform_node_End(ir_node *n)
5540 int i, j, n_keepalives = get_End_n_keepalives(n);
5543 NEW_ARR_A(ir_node *, in, n_keepalives);
5545 for (i = j = 0; i < n_keepalives; ++i) {
5546 ir_node *ka = get_End_keepalive(n, i);
5548 /* no need to keep Bad */
5551 /* do not keep unreachable code */
5552 block = is_Block(ka) ? ka : get_nodes_block(ka);
5553 if (is_block_unreachable(block))
5557 if (j != n_keepalives)
5558 set_End_keepalives(n, j, in);
5560 } /* transform_node_End */
5562 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5564 if (is_Minus(a) && get_Minus_op(a) == b)
5566 if (is_Minus(b) && get_Minus_op(b) == a)
5568 if (is_Sub(a) && is_Sub(b)) {
5569 ir_node *a_left = get_Sub_left(a);
5570 ir_node *a_right = get_Sub_right(a);
5571 ir_node *b_left = get_Sub_left(b);
5572 ir_node *b_right = get_Sub_right(b);
5574 if (a_left == b_right && a_right == b_left)
5581 static const ir_node *skip_upconv(const ir_node *node)
5583 while (is_Conv(node)) {
5584 ir_mode *mode = get_irn_mode(node);
5585 const ir_node *op = get_Conv_op(node);
5586 ir_mode *op_mode = get_irn_mode(op);
5587 if (!smaller_mode(op_mode, mode))
5594 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5595 const ir_node *mux_true)
5600 ir_relation relation;
5606 * Note further that these optimization work even for floating point
5607 * with NaN's because -NaN == NaN.
5608 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5611 mode = get_irn_mode(mux_true);
5612 if (mode_honor_signed_zeros(mode))
5615 /* must be <, <=, >=, > */
5616 relation = get_Cmp_relation(sel);
5617 if ((relation & ir_relation_less_greater) == 0)
5620 if (!ir_is_negated_value(mux_true, mux_false))
5623 mux_true = skip_upconv(mux_true);
5624 mux_false = skip_upconv(mux_false);
5626 /* must be x cmp 0 */
5627 cmp_right = get_Cmp_right(sel);
5628 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5631 cmp_left = get_Cmp_left(sel);
5632 if (cmp_left == mux_false) {
5633 if (relation & ir_relation_less) {
5636 assert(relation & ir_relation_greater);
5639 } else if (cmp_left == mux_true) {
5640 if (relation & ir_relation_less) {
5643 assert(relation & ir_relation_greater);
5651 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5654 ir_node *cmp_left = get_Cmp_left(sel);
5655 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5658 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5659 const ir_node *mux_true)
5661 /* this code should return true each time transform_node_Mux would
5662 * optimize the Mux completely away */
5664 ir_mode *mode = get_irn_mode(mux_false);
5665 if (get_mode_arithmetic(mode) == irma_twos_complement
5666 && ir_mux_is_abs(sel, mux_false, mux_true))
5669 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5670 const ir_node *cmp_r = get_Cmp_right(sel);
5671 const ir_node *cmp_l = get_Cmp_left(sel);
5672 const ir_node *f = mux_false;
5673 const ir_node *t = mux_true;
5675 if (is_Const(t) && is_Const_null(t)) {
5680 if (is_And(cmp_l) && f == cmp_r) {
5681 ir_node *and_r = get_And_right(cmp_l);
5684 if (and_r == t && is_single_bit(and_r))
5686 and_l = get_And_left(cmp_l);
5687 if (and_l == t && is_single_bit(and_l))
5696 * Optimize a Mux into some simpler cases.
5698 static ir_node *transform_node_Mux(ir_node *n)
5701 ir_node *sel = get_Mux_sel(n);
5702 ir_mode *mode = get_irn_mode(n);
5703 ir_node *t = get_Mux_true(n);
5704 ir_node *f = get_Mux_false(n);
5705 ir_graph *irg = get_irn_irg(n);
5707 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5708 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5709 int abs = ir_mux_is_abs(sel, f, t);
5711 dbg_info *dbgi = get_irn_dbg_info(n);
5712 ir_node *block = get_nodes_block(n);
5713 ir_node *op = ir_get_abs_op(sel, f, t);
5714 int bits = get_mode_size_bits(mode);
5715 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5716 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5717 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5720 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5722 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5728 /* first normalization step: try to move a constant to the false side,
5729 * 0 preferred on false side too */
5730 if (is_Cmp(sel) && is_Const(t) &&
5731 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5732 dbg_info *seldbgi = get_irn_dbg_info(sel);
5733 ir_node *block = get_nodes_block(sel);
5734 ir_relation relation = get_Cmp_relation(sel);
5739 /* Mux(x, a, b) => Mux(not(x), b, a) */
5740 relation = get_negated_relation(relation);
5741 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5742 get_Cmp_right(sel), relation);
5743 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5746 /* the following optimisations create new mode_b nodes, so only do them
5747 * before mode_b lowering */
5748 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
5750 ir_node* block = get_nodes_block(n);
5752 ir_node* c1 = get_Mux_sel(t);
5753 ir_node* t1 = get_Mux_true(t);
5754 ir_node* f1 = get_Mux_false(t);
5756 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5757 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5758 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5763 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5764 } else if (f == t1) {
5765 /* Mux(cond0, Mux(cond1, x, y), x) */
5766 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5767 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5768 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5773 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5775 } else if (is_Mux(f)) {
5776 ir_node* block = get_nodes_block(n);
5778 ir_node* c1 = get_Mux_sel(f);
5779 ir_node* t1 = get_Mux_true(f);
5780 ir_node* f1 = get_Mux_false(f);
5782 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5783 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5784 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5789 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5790 } else if (t == f1) {
5791 /* Mux(cond0, x, Mux(cond1, y, x)) */
5792 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5793 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5794 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5799 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5803 /* note: after normalization, false can only happen on default */
5804 if (mode == mode_b) {
5805 dbg_info *dbg = get_irn_dbg_info(n);
5806 ir_node *block = get_nodes_block(n);
5809 ir_tarval *tv_t = get_Const_tarval(t);
5810 if (tv_t == tarval_b_true) {
5812 /* Muxb(sel, true, false) = sel */
5813 assert(get_Const_tarval(f) == tarval_b_false);
5814 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5817 /* Muxb(sel, true, x) = Or(sel, x) */
5818 n = new_rd_Or(dbg, block, sel, f, mode_b);
5819 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5823 } else if (is_Const(f)) {
5824 ir_tarval *tv_f = get_Const_tarval(f);
5825 if (tv_f == tarval_b_true) {
5826 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5827 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5828 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5829 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5832 /* Muxb(sel, x, false) = And(sel, x) */
5833 assert(tv_f == tarval_b_false);
5834 n = new_rd_And(dbg, block, sel, t, mode_b);
5835 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5841 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5842 * value to integer. */
5843 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5844 ir_tarval *a = get_Const_tarval(t);
5845 ir_tarval *b = get_Const_tarval(f);
5847 if (tarval_is_one(a) && tarval_is_null(b)) {
5848 ir_node *block = get_nodes_block(n);
5849 ir_node *conv = new_r_Conv(block, sel, mode);
5851 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5853 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5854 ir_node *block = get_nodes_block(n);
5855 ir_node *not_ = new_r_Not(block, sel, mode_b);
5856 ir_node *conv = new_r_Conv(block, not_, mode);
5858 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5864 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5865 ir_relation relation = get_Cmp_relation(sel);
5866 ir_node *cmp_r = get_Cmp_right(sel);
5867 ir_node *cmp_l = get_Cmp_left(sel);
5868 ir_node *block = get_nodes_block(n);
5870 if (is_And(cmp_l) && f == cmp_r) {
5871 ir_node *and_r = get_And_right(cmp_l);
5874 if (and_r == t && is_single_bit(and_r)) {
5875 if (relation == ir_relation_equal) {
5876 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5877 n = new_rd_Eor(get_irn_dbg_info(n),
5878 block, cmp_l, t, mode);
5879 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5881 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5883 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5887 and_l = get_And_left(cmp_l);
5888 if (and_l == t && is_single_bit(and_l)) {
5889 if (relation == ir_relation_equal) {
5890 /* ((1 << n) & a) == 0, (1 << n), 0) */
5891 n = new_rd_Eor(get_irn_dbg_info(n),
5892 block, cmp_l, t, mode);
5893 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5895 /* ((1 << n) & a) != 0, (1 << n), 0) */
5897 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5908 * optimize Sync nodes that have other syncs as input we simply add the inputs
5909 * of the other sync to our own inputs
5911 static ir_node *transform_node_Sync(ir_node *n)
5913 int arity = get_Sync_n_preds(n);
5916 for (i = 0; i < arity;) {
5917 ir_node *pred = get_Sync_pred(n, i);
5921 /* Remove Bad predecessors */
5928 /* Remove duplicate predecessors */
5929 for (j = 0; j < i; ++j) {
5930 if (get_Sync_pred(n, j) == pred) {
5939 if (!is_Sync(pred)) {
5947 pred_arity = get_Sync_n_preds(pred);
5948 for (j = 0; j < pred_arity; ++j) {
5949 ir_node *pred_pred = get_Sync_pred(pred, j);
5954 add_irn_n(n, pred_pred);
5958 if (get_Sync_pred(n, k) == pred_pred) break;
5964 ir_graph *irg = get_irn_irg(n);
5965 return new_r_Bad(irg, mode_M);
5968 return get_Sync_pred(n, 0);
5971 /* rehash the sync node */
5976 static ir_node *transform_node_Load(ir_node *n)
5978 /* if our memory predecessor is a load from the same address, then reuse the
5979 * previous result */
5980 ir_node *mem = get_Load_mem(n);
5985 /* don't touch volatile loads */
5986 if (get_Load_volatility(n) == volatility_is_volatile)
5988 mem_pred = get_Proj_pred(mem);
5989 if (is_Load(mem_pred)) {
5990 ir_node *pred_load = mem_pred;
5992 /* conservatively compare the 2 loads. TODO: This could be less strict
5993 * with fixup code in some situations (like smaller/bigger modes) */
5994 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5996 if (get_Load_mode(pred_load) != get_Load_mode(n))
5998 /* all combinations of aligned/unaligned pred/n should be fine so we do
5999 * not compare the unaligned attribute */
6001 ir_node *block = get_nodes_block(n);
6002 ir_node *jmp = new_r_Jmp(block);
6003 ir_graph *irg = get_irn_irg(n);
6004 ir_node *bad = new_r_Bad(irg, mode_X);
6005 ir_mode *mode = get_Load_mode(n);
6006 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6007 ir_node *in[] = { mem, res, jmp, bad };
6008 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6011 } else if (is_Store(mem_pred)) {
6012 ir_node *pred_store = mem_pred;
6013 ir_node *value = get_Store_value(pred_store);
6015 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6017 if (get_irn_mode(value) != get_Load_mode(n))
6019 /* all combinations of aligned/unaligned pred/n should be fine so we do
6020 * not compare the unaligned attribute */
6022 ir_node *block = get_nodes_block(n);
6023 ir_node *jmp = new_r_Jmp(block);
6024 ir_graph *irg = get_irn_irg(n);
6025 ir_node *bad = new_r_Bad(irg, mode_X);
6026 ir_node *res = value;
6027 ir_node *in[] = { mem, res, jmp, bad };
6028 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6037 * optimize a trampoline Call into a direct Call
6039 static ir_node *transform_node_Call(ir_node *call)
6041 ir_node *callee = get_Call_ptr(call);
6042 ir_node *adr, *mem, *res, *bl, **in;
6043 ir_type *ctp, *mtp, *tp;
6047 size_t i, n_res, n_param;
6050 if (! is_Proj(callee))
6052 callee = get_Proj_pred(callee);
6053 if (! is_Builtin(callee))
6055 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6058 mem = get_Call_mem(call);
6060 if (skip_Proj(mem) == callee) {
6061 /* memory is routed to the trampoline, skip */
6062 mem = get_Builtin_mem(callee);
6065 /* build a new call type */
6066 mtp = get_Call_type(call);
6067 tdb = get_type_dbg_info(mtp);
6069 n_res = get_method_n_ress(mtp);
6070 n_param = get_method_n_params(mtp);
6071 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6073 for (i = 0; i < n_res; ++i)
6074 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6076 NEW_ARR_A(ir_node *, in, n_param + 1);
6078 /* FIXME: we don't need a new pointer type in every step */
6079 irg = get_irn_irg(call);
6080 tp = get_irg_frame_type(irg);
6081 tp = new_type_pointer(tp);
6082 set_method_param_type(ctp, 0, tp);
6084 in[0] = get_Builtin_param(callee, 2);
6085 for (i = 0; i < n_param; ++i) {
6086 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6087 in[i + 1] = get_Call_param(call, i);
6089 var = get_method_variadicity(mtp);
6090 set_method_variadicity(ctp, var);
6091 /* When we resolve a trampoline, the function must be called by a this-call */
6092 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6093 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6095 adr = get_Builtin_param(callee, 1);
6097 db = get_irn_dbg_info(call);
6098 bl = get_nodes_block(call);
6100 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6101 if (get_irn_pinned(call) == op_pin_state_floats)
6102 set_irn_pinned(res, op_pin_state_floats);
6104 } /* transform_node_Call */
6107 * Tries several [inplace] [optimizing] transformations and returns an
6108 * equivalent node. The difference to equivalent_node() is that these
6109 * transformations _do_ generate new nodes, and thus the old node must
6110 * not be freed even if the equivalent node isn't the old one.
6112 static ir_node *transform_node(ir_node *n)
6117 * Transform_node is the only "optimizing transformation" that might
6118 * return a node with a different opcode. We iterate HERE until fixpoint
6119 * to get the final result.
6123 if (n->op->ops.transform_node != NULL)
6124 n = n->op->ops.transform_node(n);
6125 } while (oldn != n);
6128 } /* transform_node */
6131 * Sets the default transform node operation for an ir_op_ops.
6133 * @param code the opcode for the default operation
6134 * @param ops the operations initialized
6139 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6143 ops->transform_node = transform_node_##a; \
6145 #define CASE_PROJ(a) \
6147 ops->transform_node_Proj = transform_node_Proj_##a; \
6149 #define CASE_PROJ_EX(a) \
6151 ops->transform_node = transform_node_##a; \
6152 ops->transform_node_Proj = transform_node_Proj_##a; \
6193 } /* firm_set_default_transform_node */
6196 /* **************** Common Subexpression Elimination **************** */
6198 /** The size of the hash table used, should estimate the number of nodes
6200 #define N_IR_NODES 512
6202 /** Compares two exception attributes */
6203 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6205 const except_attr *ea = &a->attr.except;
6206 const except_attr *eb = &b->attr.except;
6207 return ea->pin_state != eb->pin_state;
6210 /** Compares the attributes of two Const nodes. */
6211 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6213 return get_Const_tarval(a) != get_Const_tarval(b);
6216 /** Compares the attributes of two Proj nodes. */
6217 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6219 return a->attr.proj.proj != b->attr.proj.proj;
6222 /** Compares the attributes of two Alloc nodes. */
6223 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6225 const alloc_attr *pa = &a->attr.alloc;
6226 const alloc_attr *pb = &b->attr.alloc;
6227 if (pa->where != pb->where || pa->type != pb->type)
6229 return node_cmp_exception(a, b);
6232 /** Compares the attributes of two Free nodes. */
6233 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6235 const free_attr *pa = &a->attr.free;
6236 const free_attr *pb = &b->attr.free;
6237 return (pa->where != pb->where) || (pa->type != pb->type);
6240 /** Compares the attributes of two SymConst nodes. */
6241 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6243 const symconst_attr *pa = &a->attr.symc;
6244 const symconst_attr *pb = &b->attr.symc;
6245 return (pa->kind != pb->kind)
6246 || (pa->sym.type_p != pb->sym.type_p);
6249 /** Compares the attributes of two Call nodes. */
6250 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6252 const call_attr *pa = &a->attr.call;
6253 const call_attr *pb = &b->attr.call;
6254 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6256 return node_cmp_exception(a, b);
6259 /** Compares the attributes of two Sel nodes. */
6260 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6262 const ir_entity *a_ent = get_Sel_entity(a);
6263 const ir_entity *b_ent = get_Sel_entity(b);
6264 return a_ent != b_ent;
6267 /** Compares the attributes of two Phi nodes. */
6268 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6270 /* we can only enter this function if both nodes have the same number of inputs,
6271 hence it is enough to check if one of them is a Phi0 */
6273 /* check the Phi0 pos attribute */
6274 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6279 /** Compares the attributes of two Conv nodes. */
6280 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6282 return get_Conv_strict(a) != get_Conv_strict(b);
6285 /** Compares the attributes of two Cast nodes. */
6286 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6288 return get_Cast_type(a) != get_Cast_type(b);
6291 /** Compares the attributes of two Load nodes. */
6292 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6294 if (get_Load_volatility(a) == volatility_is_volatile ||
6295 get_Load_volatility(b) == volatility_is_volatile)
6296 /* NEVER do CSE on volatile Loads */
6298 /* do not CSE Loads with different alignment. Be conservative. */
6299 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6301 if (get_Load_mode(a) != get_Load_mode(b))
6303 return node_cmp_exception(a, b);
6306 /** Compares the attributes of two Store nodes. */
6307 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6309 /* do not CSE Stores with different alignment. Be conservative. */
6310 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6312 /* NEVER do CSE on volatile Stores */
6313 if (get_Store_volatility(a) == volatility_is_volatile ||
6314 get_Store_volatility(b) == volatility_is_volatile)
6316 return node_cmp_exception(a, b);
6319 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6321 if (get_CopyB_type(a) != get_CopyB_type(b))
6324 return node_cmp_exception(a, b);
6327 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6329 return node_cmp_exception(a, b);
6332 /** Compares the attributes of two Div nodes. */
6333 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6335 const div_attr *ma = &a->attr.div;
6336 const div_attr *mb = &b->attr.div;
6337 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6339 return node_cmp_exception(a, b);
6342 /** Compares the attributes of two Mod nodes. */
6343 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6345 const mod_attr *ma = &a->attr.mod;
6346 const mod_attr *mb = &b->attr.mod;
6347 if (ma->resmode != mb->resmode)
6349 return node_cmp_exception(a, b);
6352 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6354 const cmp_attr *ma = &a->attr.cmp;
6355 const cmp_attr *mb = &b->attr.cmp;
6356 return ma->relation != mb->relation;
6359 /** Compares the attributes of two Confirm nodes. */
6360 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6362 const confirm_attr *ma = &a->attr.confirm;
6363 const confirm_attr *mb = &b->attr.confirm;
6364 return ma->relation != mb->relation;
6367 /** Compares the attributes of two Builtin nodes. */
6368 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6370 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6372 if (get_Builtin_type(a) != get_Builtin_type(b))
6374 return node_cmp_exception(a, b);
6377 /** Compares the attributes of two ASM nodes. */
6378 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6381 const ir_asm_constraint *ca;
6382 const ir_asm_constraint *cb;
6385 if (get_ASM_text(a) != get_ASM_text(b))
6388 /* Should we really check the constraints here? Should be better, but is strange. */
6389 n = get_ASM_n_input_constraints(a);
6390 if (n != get_ASM_n_input_constraints(b))
6393 ca = get_ASM_input_constraints(a);
6394 cb = get_ASM_input_constraints(b);
6395 for (i = 0; i < n; ++i) {
6396 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6397 || ca[i].mode != cb[i].mode)
6401 n = get_ASM_n_output_constraints(a);
6402 if (n != get_ASM_n_output_constraints(b))
6405 ca = get_ASM_output_constraints(a);
6406 cb = get_ASM_output_constraints(b);
6407 for (i = 0; i < n; ++i) {
6408 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6409 || ca[i].mode != cb[i].mode)
6413 n = get_ASM_n_clobbers(a);
6414 if (n != get_ASM_n_clobbers(b))
6417 cla = get_ASM_clobbers(a);
6418 clb = get_ASM_clobbers(b);
6419 for (i = 0; i < n; ++i) {
6420 if (cla[i] != clb[i])
6424 return node_cmp_exception(a, b);
6427 /** Compares the inexistent attributes of two Dummy nodes. */
6428 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6432 /* Dummy nodes never equal by definition */
6436 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6438 if (get_InstOf_type(a) != get_InstOf_type(b))
6440 return node_cmp_exception(a, b);
6444 * Set the default node attribute compare operation for an ir_op_ops.
6446 * @param code the opcode for the default operation
6447 * @param ops the operations initialized
6452 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6456 ops->node_cmp_attr = node_cmp_attr_##a; \
6489 } /* firm_set_default_node_cmp_attr */
6492 * Compare function for two nodes in the value table. Gets two
6493 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6495 int identities_cmp(const void *elt, const void *key)
6497 ir_node *a = (ir_node *)elt;
6498 ir_node *b = (ir_node *)key;
6501 if (a == b) return 0;
6503 if ((get_irn_op(a) != get_irn_op(b)) ||
6504 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6506 /* compare if a's in and b's in are of equal length */
6507 irn_arity_a = get_irn_arity(a);
6508 if (irn_arity_a != get_irn_arity(b))
6511 /* blocks are never the same */
6515 if (get_irn_pinned(a) == op_pin_state_pinned) {
6516 /* for pinned nodes, the block inputs must be equal */
6517 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6520 ir_node *block_a = get_nodes_block(a);
6521 ir_node *block_b = get_nodes_block(b);
6522 if (! get_opt_global_cse()) {
6523 /* for block-local CSE both nodes must be in the same Block */
6524 if (block_a != block_b)
6527 /* The optimistic approach would be to do nothing here.
6528 * However doing GCSE optimistically produces a lot of partially dead code which appears
6529 * to be worse in practice than the missed opportunities.
6530 * So we use a very conservative variant here and only CSE if 1 value dominates the
6532 if (!block_dominates(block_a, block_b)
6533 && !block_dominates(block_b, block_a))
6538 /* compare a->in[0..ins] with b->in[0..ins] */
6539 for (i = 0; i < irn_arity_a; ++i) {
6540 ir_node *pred_a = get_irn_n(a, i);
6541 ir_node *pred_b = get_irn_n(b, i);
6542 if (pred_a != pred_b) {
6543 /* if both predecessors are CSE neutral they might be different */
6544 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6550 * here, we already now that the nodes are identical except their
6553 if (a->op->ops.node_cmp_attr)
6554 return a->op->ops.node_cmp_attr(a, b);
6557 } /* identities_cmp */
6560 * Calculate a hash value of a node.
6562 * @param node The IR-node
6564 unsigned ir_node_hash(const ir_node *node)
6566 return node->op->ops.hash(node);
6567 } /* ir_node_hash */
6570 void new_identities(ir_graph *irg)
6572 if (irg->value_table != NULL)
6573 del_pset(irg->value_table);
6574 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6575 } /* new_identities */
6577 void del_identities(ir_graph *irg)
6579 if (irg->value_table != NULL)
6580 del_pset(irg->value_table);
6581 } /* del_identities */
6583 /* Normalize a node by putting constants (and operands with larger
6584 * node index) on the right (operator side). */
6585 void ir_normalize_node(ir_node *n)
6587 if (is_op_commutative(get_irn_op(n))) {
6588 ir_node *l = get_binop_left(n);
6589 ir_node *r = get_binop_right(n);
6591 /* For commutative operators perform a OP b == b OP a but keep
6592 * constants on the RIGHT side. This helps greatly in some
6593 * optimizations. Moreover we use the idx number to make the form
6595 if (!operands_are_normalized(l, r)) {
6596 set_binop_left(n, r);
6597 set_binop_right(n, l);
6601 } /* ir_normalize_node */
6604 * Return the canonical node computing the same value as n.
6605 * Looks up the node in a hash table, enters it in the table
6606 * if it isn't there yet.
6608 * @param n the node to look up
6610 * @return a node that computes the same value as n or n if no such
6611 * node could be found
6613 ir_node *identify_remember(ir_node *n)
6615 ir_graph *irg = get_irn_irg(n);
6616 pset *value_table = irg->value_table;
6619 if (value_table == NULL)
6622 ir_normalize_node(n);
6623 /* lookup or insert in hash table with given hash key. */
6624 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6627 /* n is reachable again */
6628 edges_node_revival(nn);
6632 } /* identify_remember */
6635 * During construction we set the op_pin_state_pinned flag in the graph right
6636 * when the optimization is performed. The flag turning on procedure global
6637 * cse could be changed between two allocations. This way we are safe.
6639 * @param n The node to lookup
6641 static inline ir_node *identify_cons(ir_node *n)
6645 n = identify_remember(n);
6646 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6647 ir_graph *irg = get_irn_irg(n);
6648 set_irg_pinned(irg, op_pin_state_floats);
6651 } /* identify_cons */
6653 /* Add a node to the identities value table. */
6654 void add_identities(ir_node *node)
6661 identify_remember(node);
6664 /* Visit each node in the value table of a graph. */
6665 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6668 ir_graph *rem = current_ir_graph;
6670 current_ir_graph = irg;
6671 foreach_pset(irg->value_table, ir_node*, node) {
6674 current_ir_graph = rem;
6675 } /* visit_all_identities */
6678 * These optimizations deallocate nodes from the obstack.
6679 * It can only be called if it is guaranteed that no other nodes
6680 * reference this one, i.e., right after construction of a node.
6682 * @param n The node to optimize
6684 ir_node *optimize_node(ir_node *n)
6687 ir_graph *irg = get_irn_irg(n);
6688 unsigned iro = get_irn_opcode(n);
6691 /* Always optimize Phi nodes: part of the construction. */
6692 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6694 /* constant expression evaluation / constant folding */
6695 if (get_opt_constant_folding()) {
6696 /* neither constants nor Tuple values can be evaluated */
6697 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6698 /* try to evaluate */
6699 tv = computed_value(n);
6700 if (tv != tarval_bad) {
6705 * we MUST copy the node here temporarily, because it's still
6706 * needed for DBG_OPT_CSTEVAL
6708 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6709 oldn = (ir_node*)alloca(node_size);
6711 memcpy(oldn, n, node_size);
6712 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6714 /* ARG, copy the in array, we need it for statistics */
6715 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6717 /* note the inplace edges module */
6718 edges_node_deleted(n);
6720 /* evaluation was successful -- replace the node. */
6721 irg_kill_node(irg, n);
6722 nw = new_r_Const(irg, tv);
6724 DBG_OPT_CSTEVAL(oldn, nw);
6730 /* remove unnecessary nodes */
6731 if (get_opt_algebraic_simplification() ||
6732 (iro == iro_Phi) || /* always optimize these nodes. */
6734 (iro == iro_Proj) ||
6735 (iro == iro_Block) ) /* Flags tested local. */
6736 n = equivalent_node(n);
6738 /* Common Subexpression Elimination.
6740 * Checks whether n is already available.
6741 * The block input is used to distinguish different subexpressions. Right
6742 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6743 * subexpressions within a block.
6746 n = identify_cons(n);
6749 edges_node_deleted(oldn);
6751 /* We found an existing, better node, so we can deallocate the old node. */
6752 irg_kill_node(irg, oldn);
6756 /* Some more constant expression evaluation that does not allow to
6758 iro = get_irn_opcode(n);
6759 if (get_opt_algebraic_simplification() ||
6760 (iro == iro_Cond) ||
6761 (iro == iro_Proj)) /* Flags tested local. */
6762 n = transform_node(n);
6764 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6765 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6767 n = identify_remember(o);
6773 } /* optimize_node */
6777 * These optimizations never deallocate nodes (in place). This can cause dead
6778 * nodes lying on the obstack. Remove these by a dead node elimination,
6779 * i.e., a copying garbage collection.
6781 ir_node *optimize_in_place_2(ir_node *n)
6785 unsigned iro = get_irn_opcode(n);
6787 if (!get_opt_optimize() && !is_Phi(n)) return n;
6789 if (iro == iro_Deleted)
6792 /* constant expression evaluation / constant folding */
6793 if (get_opt_constant_folding()) {
6794 /* neither constants nor Tuple values can be evaluated */
6795 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6796 /* try to evaluate */
6797 tv = computed_value(n);
6798 if (tv != tarval_bad) {
6799 /* evaluation was successful -- replace the node. */
6800 ir_graph *irg = get_irn_irg(n);
6802 n = new_r_Const(irg, tv);
6804 DBG_OPT_CSTEVAL(oldn, n);
6810 /* remove unnecessary nodes */
6811 if (get_opt_constant_folding() ||
6812 (iro == iro_Phi) || /* always optimize these nodes. */
6813 (iro == iro_Id) || /* ... */
6814 (iro == iro_Proj) || /* ... */
6815 (iro == iro_Block) ) /* Flags tested local. */
6816 n = equivalent_node(n);
6818 /** common subexpression elimination **/
6819 /* Checks whether n is already available. */
6820 /* The block input is used to distinguish different subexpressions. Right
6821 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6822 subexpressions within a block. */
6823 if (get_opt_cse()) {
6825 n = identify_remember(o);
6830 /* Some more constant expression evaluation. */
6831 iro = get_irn_opcode(n);
6832 if (get_opt_constant_folding() ||
6833 (iro == iro_Cond) ||
6834 (iro == iro_Proj)) /* Flags tested local. */
6835 n = transform_node(n);
6837 /* Now we can verify the node, as it has no dead inputs any more. */
6840 /* Now we have a legal, useful node. Enter it in hash table for cse.
6841 Blocks should be unique anyways. (Except the successor of start:
6842 is cse with the start block!) */
6843 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6845 n = identify_remember(o);
6851 } /* optimize_in_place_2 */
6854 * Wrapper for external use, set proper status bits after optimization.
6856 ir_node *optimize_in_place(ir_node *n)
6858 ir_graph *irg = get_irn_irg(n);
6859 /* Handle graph state */
6860 assert(get_irg_phase_state(irg) != phase_building);
6862 if (get_opt_global_cse())
6863 set_irg_pinned(irg, op_pin_state_floats);
6865 /* FIXME: Maybe we could also test whether optimizing the node can
6866 change the control graph. */
6867 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
6868 return optimize_in_place_2(n);
6869 } /* optimize_in_place */
6872 * Calculate a hash value of a Const node.
6874 static unsigned hash_Const(const ir_node *node)
6878 /* special value for const, as they only differ in their tarval. */
6879 h = HASH_PTR(node->attr.con.tarval);
6885 * Calculate a hash value of a SymConst node.
6887 static unsigned hash_SymConst(const ir_node *node)
6891 /* all others are pointers */
6892 h = HASH_PTR(node->attr.symc.sym.type_p);
6895 } /* hash_SymConst */
6898 * Set the default hash operation in an ir_op_ops.
6900 * @param code the opcode for the default operation
6901 * @param ops the operations initialized
6906 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6910 ops->hash = hash_##a; \
6913 /* hash function already set */
6914 if (ops->hash != NULL)
6921 /* use input/mode default hash if no function was given */
6922 ops->hash = firm_default_hash;
6930 * Sets the default operation for an ir_ops.
6932 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6934 ops = firm_set_default_hash(code, ops);
6935 ops = firm_set_default_computed_value(code, ops);
6936 ops = firm_set_default_equivalent_node(code, ops);
6937 ops = firm_set_default_transform_node(code, ops);
6938 ops = firm_set_default_node_cmp_attr(code, ops);
6939 ops = firm_set_default_get_type_attr(code, ops);
6940 ops = firm_set_default_get_entity_attr(code, ops);
6943 } /* firm_set_default_operations */