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 != NULL && vrp->bits_set == vrp->bits_not_set)
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);
3143 static bool complement_values(const ir_node *a, const ir_node *b)
3145 if (is_Not(a) && get_Not_op(a) == b)
3147 if (is_Not(b) && get_Not_op(b) == a)
3149 if (is_Const(a) && is_Const(b)) {
3150 ir_tarval *tv_a = get_Const_tarval(a);
3151 ir_tarval *tv_b = get_Const_tarval(b);
3152 return tarval_not(tv_a) == tv_b;
3160 static ir_node *transform_node_And(ir_node *n)
3162 ir_node *c, *oldn = n;
3163 ir_node *a = get_And_left(n);
3164 ir_node *b = get_And_right(n);
3166 vrp_attr *a_vrp, *b_vrp;
3168 if (is_Cmp(a) && is_Cmp(b)) {
3169 ir_node *a_left = get_Cmp_left(a);
3170 ir_node *a_right = get_Cmp_right(a);
3171 ir_node *b_left = get_Cmp_left(b);
3172 ir_node *b_right = get_Cmp_right(b);
3173 ir_relation a_relation = get_Cmp_relation(a);
3174 ir_relation b_relation = get_Cmp_relation(b);
3175 /* we can combine the relations of two compares with the same
3177 if (a_left == b_left && b_left == b_right) {
3178 dbg_info *dbgi = get_irn_dbg_info(n);
3179 ir_node *block = get_nodes_block(n);
3180 ir_relation new_relation = a_relation & b_relation;
3181 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3183 /* Cmp(a==b) and Cmp(c==d) can be optimized to Cmp((a^b)|(c^d)==0) */
3184 if (a_relation == b_relation && a_relation == ir_relation_equal
3185 && !mode_is_float(get_irn_mode(a_left))
3186 && !mode_is_float(get_irn_mode(b_left))) {
3187 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
3188 dbg_info *dbgi = get_irn_dbg_info(n);
3189 ir_node *block = get_nodes_block(n);
3190 ir_mode *a_mode = get_irn_mode(a_left);
3191 ir_mode *b_mode = get_irn_mode(b_left);
3192 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3193 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3194 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
3195 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
3196 ir_graph *irg = get_irn_irg(n);
3197 ir_node *zero = create_zero_const(irg, b_mode);
3198 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3200 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
3201 dbg_info *dbgi = get_irn_dbg_info(n);
3202 ir_node *block = get_nodes_block(n);
3203 ir_mode *a_mode = get_irn_mode(a_left);
3204 ir_mode *b_mode = get_irn_mode(b_left);
3205 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
3206 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
3207 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
3208 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
3209 ir_graph *irg = get_irn_irg(n);
3210 ir_node *zero = create_zero_const(irg, a_mode);
3211 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
3216 mode = get_irn_mode(n);
3217 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3220 ir_node *or_left = get_Or_left(a);
3221 ir_node *or_right = get_Or_right(a);
3222 if (complement_values(or_left, b)) {
3223 /* (a|b) & ~a => b & ~a */
3224 dbg_info *dbgi = get_irn_dbg_info(n);
3225 ir_node *block = get_nodes_block(n);
3226 return new_rd_And(dbgi, block, or_right, b, mode);
3227 } else if (complement_values(or_right, b)) {
3228 /* (a|b) & ~b => a & ~b */
3229 dbg_info *dbgi = get_irn_dbg_info(n);
3230 ir_node *block = get_nodes_block(n);
3231 return new_rd_And(dbgi, block, or_left, b, mode);
3232 } else if (is_Not(b)) {
3233 ir_node *op = get_Not_op(b);
3235 ir_node *ba = get_And_left(op);
3236 ir_node *bb = get_And_right(op);
3238 /* it's enough to test the following cases due to normalization! */
3239 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3240 /* (a|b) & ~(a&b) = a^b */
3241 ir_node *block = get_nodes_block(n);
3243 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3251 ir_node *or_left = get_Or_left(b);
3252 ir_node *or_right = get_Or_right(b);
3253 if (complement_values(or_left, a)) {
3254 /* (a|b) & ~a => b & ~a */
3255 dbg_info *dbgi = get_irn_dbg_info(n);
3256 ir_node *block = get_nodes_block(n);
3257 return new_rd_And(dbgi, block, or_right, a, mode);
3258 } else if (complement_values(or_right, a)) {
3259 /* (a|b) & ~b => a & ~b */
3260 dbg_info *dbgi = get_irn_dbg_info(n);
3261 ir_node *block = get_nodes_block(n);
3262 return new_rd_And(dbgi, block, or_left, a, mode);
3263 } else if (is_Not(a)) {
3264 ir_node *op = get_Not_op(a);
3266 ir_node *aa = get_And_left(op);
3267 ir_node *ab = get_And_right(op);
3269 /* it's enough to test the following cases due to normalization! */
3270 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3271 /* (a|b) & ~(a&b) = a^b */
3272 ir_node *block = get_nodes_block(n);
3274 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3275 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3282 ir_node *al = get_Eor_left(a);
3283 ir_node *ar = get_Eor_right(a);
3286 /* (b ^ a) & b -> ~a & b */
3287 dbg_info *dbg = get_irn_dbg_info(n);
3288 ir_node *block = get_nodes_block(n);
3290 ar = new_rd_Not(dbg, block, ar, mode);
3291 n = new_rd_And(dbg, block, ar, b, mode);
3292 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3296 /* (a ^ b) & b -> ~a & b */
3297 dbg_info *dbg = get_irn_dbg_info(n);
3298 ir_node *block = get_nodes_block(n);
3300 al = new_rd_Not(dbg, block, al, mode);
3301 n = new_rd_And(dbg, block, al, b, mode);
3302 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3307 ir_node *bl = get_Eor_left(b);
3308 ir_node *br = get_Eor_right(b);
3311 /* a & (a ^ b) -> a & ~b */
3312 dbg_info *dbg = get_irn_dbg_info(n);
3313 ir_node *block = get_nodes_block(n);
3315 br = new_rd_Not(dbg, block, br, mode);
3316 n = new_rd_And(dbg, block, br, a, mode);
3317 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3321 /* a & (b ^ a) -> a & ~b */
3322 dbg_info *dbg = get_irn_dbg_info(n);
3323 ir_node *block = get_nodes_block(n);
3325 bl = new_rd_Not(dbg, block, bl, mode);
3326 n = new_rd_And(dbg, block, bl, a, mode);
3327 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3331 if (is_Not(a) && is_Not(b)) {
3332 /* ~a & ~b = ~(a|b) */
3333 ir_node *block = get_nodes_block(n);
3334 ir_mode *mode = get_irn_mode(n);
3338 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3339 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3340 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3344 b_vrp = vrp_get_info(b);
3345 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3346 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3352 a_vrp = vrp_get_info(a);
3353 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3354 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3358 n = transform_bitwise_distributive(n, transform_node_And);
3360 n = transform_node_bitop_shift(n);
3363 } /* transform_node_And */
3365 /* the order of the values is important! */
3366 typedef enum const_class {
3372 static const_class classify_const(const ir_node* n)
3374 if (is_Const(n)) return const_const;
3375 if (is_irn_constlike(n)) return const_like;
3380 * Determines whether r is more constlike or has a larger index (in that order)
3383 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3385 const const_class l_order = classify_const(l);
3386 const const_class r_order = classify_const(r);
3388 l_order > r_order ||
3389 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3395 static ir_node *transform_node_Eor(ir_node *n)
3397 ir_node *c, *oldn = n;
3398 ir_node *a = get_Eor_left(n);
3399 ir_node *b = get_Eor_right(n);
3400 ir_mode *mode = get_irn_mode(n);
3402 /* we can combine the relations of two compares with the same operands */
3403 if (is_Cmp(a) && is_Cmp(b)) {
3404 ir_node *a_left = get_Cmp_left(a);
3405 ir_node *a_right = get_Cmp_left(a);
3406 ir_node *b_left = get_Cmp_left(b);
3407 ir_node *b_right = get_Cmp_right(b);
3408 if (a_left == b_left && b_left == b_right) {
3409 dbg_info *dbgi = get_irn_dbg_info(n);
3410 ir_node *block = get_nodes_block(n);
3411 ir_relation a_relation = get_Cmp_relation(a);
3412 ir_relation b_relation = get_Cmp_relation(b);
3413 ir_relation new_relation = a_relation ^ b_relation;
3414 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3418 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3420 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3421 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3422 dbg_info *dbg = get_irn_dbg_info(n);
3423 ir_node *block = get_nodes_block(n);
3424 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3425 ir_node *new_left = get_Not_op(a);
3426 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3429 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3430 dbg_info *dbg = get_irn_dbg_info(n);
3431 ir_node *block = get_nodes_block(n);
3432 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3433 ir_node *new_right = get_Not_op(b);
3434 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3439 /* x ^ 1...1 -> ~1 */
3440 if (is_Const(b) && is_Const_all_one(b)) {
3441 n = new_r_Not(get_nodes_block(n), a, mode);
3442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3447 vrp_attr *a_vrp = vrp_get_info(a);
3448 vrp_attr *b_vrp = vrp_get_info(b);
3449 if (a_vrp != NULL && b_vrp != NULL) {
3450 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
3452 if (tarval_is_null(vrp_val)) {
3453 dbg_info *dbgi = get_irn_dbg_info(n);
3454 return new_rd_Add(dbgi, get_nodes_block(n), a, b, mode);
3459 n = transform_bitwise_distributive(n, transform_node_Eor);
3461 n = transform_node_bitop_shift(n);
3464 } /* transform_node_Eor */
3469 static ir_node *transform_node_Not(ir_node *n)
3471 ir_node *c, *oldn = n;
3472 ir_node *a = get_Not_op(n);
3473 ir_mode *mode = get_irn_mode(n);
3475 HANDLE_UNOP_PHI(tarval_not,a,c);
3477 /* check for a boolean Not */
3479 dbg_info *dbgi = get_irn_dbg_info(a);
3480 ir_node *block = get_nodes_block(a);
3481 ir_relation relation = get_Cmp_relation(a);
3482 relation = get_negated_relation(relation);
3483 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3484 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3488 /* normalize ~(a ^ b) => a ^ ~b */
3490 dbg_info *dbg = get_irn_dbg_info(n);
3491 ir_node *block = get_nodes_block(n);
3492 ir_node *eor_right = get_Eor_right(a);
3493 ir_node *eor_left = get_Eor_left(a);
3494 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3495 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3499 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3500 if (is_Minus(a)) { /* ~-x -> x + -1 */
3501 dbg_info *dbg = get_irn_dbg_info(n);
3502 ir_graph *irg = get_irn_irg(n);
3503 ir_node *block = get_nodes_block(n);
3504 ir_node *add_l = get_Minus_op(a);
3505 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3506 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3507 } else if (is_Add(a)) {
3508 ir_node *add_r = get_Add_right(a);
3509 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3510 /* ~(x + -1) = -x */
3511 ir_node *op = get_Add_left(a);
3512 ir_node *blk = get_nodes_block(n);
3513 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3514 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3522 * Transform a Minus.
3526 * -(a >>u (size-1)) = a >>s (size-1)
3527 * -(a >>s (size-1)) = a >>u (size-1)
3528 * -(a * const) -> a * -const
3530 static ir_node *transform_node_Minus(ir_node *n)
3532 ir_node *c, *oldn = n;
3533 ir_node *a = get_Minus_op(n);
3536 HANDLE_UNOP_PHI(tarval_neg,a,c);
3538 mode = get_irn_mode(a);
3539 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3540 /* the following rules are only to twos-complement */
3543 ir_node *op = get_Not_op(a);
3544 ir_tarval *tv = get_mode_one(mode);
3545 ir_node *blk = get_nodes_block(n);
3546 ir_graph *irg = get_irn_irg(blk);
3547 ir_node *c = new_r_Const(irg, tv);
3548 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3549 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3553 ir_node *c = get_Shr_right(a);
3556 ir_tarval *tv = get_Const_tarval(c);
3558 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3559 /* -(a >>u (size-1)) = a >>s (size-1) */
3560 ir_node *v = get_Shr_left(a);
3562 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3563 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3569 ir_node *c = get_Shrs_right(a);
3572 ir_tarval *tv = get_Const_tarval(c);
3574 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3575 /* -(a >>s (size-1)) = a >>u (size-1) */
3576 ir_node *v = get_Shrs_left(a);
3578 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3579 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3586 /* - (a-b) = b - a */
3587 ir_node *la = get_Sub_left(a);
3588 ir_node *ra = get_Sub_right(a);
3589 ir_node *blk = get_nodes_block(n);
3591 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3592 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3596 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3597 ir_node *mul_l = get_Mul_left(a);
3598 ir_node *mul_r = get_Mul_right(a);
3599 ir_tarval *tv = value_of(mul_r);
3600 if (tv != tarval_bad) {
3601 tv = tarval_neg(tv);
3602 if (tv != tarval_bad) {
3603 ir_graph *irg = get_irn_irg(n);
3604 ir_node *cnst = new_r_Const(irg, tv);
3605 dbg_info *dbg = get_irn_dbg_info(a);
3606 ir_node *block = get_nodes_block(a);
3607 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3608 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3615 } /* transform_node_Minus */
3618 * Transform a Proj(Load) with a non-null address.
3620 static ir_node *transform_node_Proj_Load(ir_node *proj)
3622 if (get_opt_ldst_only_null_ptr_exceptions()) {
3623 if (get_irn_mode(proj) == mode_X) {
3624 ir_node *load = get_Proj_pred(proj);
3626 /* get the Load address */
3627 const ir_node *addr = get_Load_ptr(load);
3628 const ir_node *confirm;
3630 if (value_not_null(addr, &confirm)) {
3631 if (confirm == NULL) {
3632 /* this node may float if it did not depend on a Confirm */
3633 set_irn_pinned(load, op_pin_state_floats);
3635 if (get_Proj_proj(proj) == pn_Load_X_except) {
3636 ir_graph *irg = get_irn_irg(proj);
3637 DBG_OPT_EXC_REM(proj);
3638 return new_r_Bad(irg, mode_X);
3640 ir_node *blk = get_nodes_block(load);
3641 return new_r_Jmp(blk);
3647 } /* transform_node_Proj_Load */
3650 * Transform a Proj(Store) with a non-null address.
3652 static ir_node *transform_node_Proj_Store(ir_node *proj)
3654 if (get_opt_ldst_only_null_ptr_exceptions()) {
3655 if (get_irn_mode(proj) == mode_X) {
3656 ir_node *store = get_Proj_pred(proj);
3658 /* get the load/store address */
3659 const ir_node *addr = get_Store_ptr(store);
3660 const ir_node *confirm;
3662 if (value_not_null(addr, &confirm)) {
3663 if (confirm == NULL) {
3664 /* this node may float if it did not depend on a Confirm */
3665 set_irn_pinned(store, op_pin_state_floats);
3667 if (get_Proj_proj(proj) == pn_Store_X_except) {
3668 ir_graph *irg = get_irn_irg(proj);
3669 DBG_OPT_EXC_REM(proj);
3670 return new_r_Bad(irg, mode_X);
3672 ir_node *blk = get_nodes_block(store);
3673 return new_r_Jmp(blk);
3679 } /* transform_node_Proj_Store */
3682 * Transform a Proj(Div) with a non-zero value.
3683 * Removes the exceptions and routes the memory to the NoMem node.
3685 static ir_node *transform_node_Proj_Div(ir_node *proj)
3687 ir_node *div = get_Proj_pred(proj);
3688 ir_node *b = get_Div_right(div);
3689 ir_node *res, *new_mem;
3690 const ir_node *confirm;
3693 if (value_not_zero(b, &confirm)) {
3694 /* div(x, y) && y != 0 */
3695 if (confirm == NULL) {
3696 /* we are sure we have a Const != 0 */
3697 new_mem = get_Div_mem(div);
3698 new_mem = skip_Pin(new_mem);
3699 set_Div_mem(div, new_mem);
3700 set_irn_pinned(div, op_pin_state_floats);
3703 proj_nr = get_Proj_proj(proj);
3705 case pn_Div_X_regular:
3706 return new_r_Jmp(get_nodes_block(div));
3708 case pn_Div_X_except: {
3709 ir_graph *irg = get_irn_irg(proj);
3710 /* we found an exception handler, remove it */
3711 DBG_OPT_EXC_REM(proj);
3712 return new_r_Bad(irg, mode_X);
3716 ir_graph *irg = get_irn_irg(proj);
3717 res = get_Div_mem(div);
3718 new_mem = get_irg_no_mem(irg);
3721 /* This node can only float up to the Confirm block */
3722 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3724 set_irn_pinned(div, op_pin_state_floats);
3725 /* this is a Div without exception, we can remove the memory edge */
3726 set_Div_mem(div, new_mem);
3732 } /* transform_node_Proj_Div */
3735 * Transform a Proj(Mod) with a non-zero value.
3736 * Removes the exceptions and routes the memory to the NoMem node.
3738 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3740 ir_node *mod = get_Proj_pred(proj);
3741 ir_node *b = get_Mod_right(mod);
3742 ir_node *res, *new_mem;
3743 const ir_node *confirm;
3746 if (value_not_zero(b, &confirm)) {
3747 /* mod(x, y) && y != 0 */
3748 proj_nr = get_Proj_proj(proj);
3750 if (confirm == NULL) {
3751 /* we are sure we have a Const != 0 */
3752 new_mem = get_Mod_mem(mod);
3753 new_mem = skip_Pin(new_mem);
3754 set_Mod_mem(mod, new_mem);
3755 set_irn_pinned(mod, op_pin_state_floats);
3760 case pn_Mod_X_regular:
3761 return new_r_Jmp(get_irn_n(mod, -1));
3763 case pn_Mod_X_except: {
3764 ir_graph *irg = get_irn_irg(proj);
3765 /* we found an exception handler, remove it */
3766 DBG_OPT_EXC_REM(proj);
3767 return new_r_Bad(irg, mode_X);
3771 ir_graph *irg = get_irn_irg(proj);
3772 res = get_Mod_mem(mod);
3773 new_mem = get_irg_no_mem(irg);
3776 /* This node can only float up to the Confirm block */
3777 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3779 /* this is a Mod without exception, we can remove the memory edge */
3780 set_Mod_mem(mod, new_mem);
3784 if (get_Mod_left(mod) == b) {
3785 /* a % a = 0 if a != 0 */
3786 ir_graph *irg = get_irn_irg(proj);
3787 ir_mode *mode = get_irn_mode(proj);
3788 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3790 DBG_OPT_CSTEVAL(mod, res);
3796 } /* transform_node_Proj_Mod */
3799 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3801 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3803 ir_node *n = get_Proj_pred(proj);
3804 ir_node *b = get_Cond_selector(n);
3806 if (mode_is_int(get_irn_mode(b))) {
3807 ir_tarval *tb = value_of(b);
3809 if (tb != tarval_bad) {
3810 /* we have a constant switch */
3811 long num = get_Proj_proj(proj);
3813 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3814 if (get_tarval_long(tb) == num) {
3815 /* Do NOT create a jump here, or we will have 2 control flow ops
3816 * in a block. This case is optimized away in optimize_cf(). */
3819 ir_graph *irg = get_irn_irg(proj);
3820 /* this case will NEVER be taken, kill it */
3821 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3822 return new_r_Bad(irg, mode_X);
3826 long num = get_Proj_proj(proj);
3827 vrp_attr *b_vrp = vrp_get_info(b);
3828 if (num != get_Cond_default_proj(n) && b_vrp) {
3829 /* Try handling with vrp data. We only remove dead parts. */
3830 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3832 if (b_vrp->range_type == VRP_RANGE) {
3833 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3834 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3836 if ((cmp_result & ir_relation_greater) == cmp_result
3837 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3838 ir_graph *irg = get_irn_irg(proj);
3839 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3840 return new_r_Bad(irg, mode_X);
3842 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3843 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3844 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3846 if ((cmp_result & ir_relation_less_equal) == cmp_result
3847 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3848 ir_graph *irg = get_irn_irg(proj);
3849 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3850 return new_r_Bad(irg, mode_X);
3855 tarval_and( b_vrp->bits_set, tp),
3857 ) == ir_relation_equal)) {
3858 ir_graph *irg = get_irn_irg(proj);
3859 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3860 return new_r_Bad(irg, mode_X);
3866 tarval_not(b_vrp->bits_not_set)),
3867 tarval_not(b_vrp->bits_not_set))
3868 == ir_relation_equal)) {
3869 ir_graph *irg = get_irn_irg(proj);
3870 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
3871 return new_r_Bad(irg, mode_X);
3880 * return true if the operation returns a value with exactly 1 bit set
3882 static bool is_single_bit(const ir_node *node)
3884 /* a first implementation, could be extended with vrp and others... */
3886 ir_node *shl_l = get_Shl_left(node);
3887 ir_mode *mode = get_irn_mode(node);
3888 int modulo = get_mode_modulo_shift(mode);
3889 /* this works if we shift a 1 and we have modulo shift */
3890 if (is_Const(shl_l) && is_Const_one(shl_l)
3891 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3894 } else if (is_Const(node)) {
3895 ir_tarval *tv = get_Const_tarval(node);
3896 return tarval_is_single_bit(tv);
3902 * Normalizes and optimizes Cmp nodes.
3904 static ir_node *transform_node_Cmp(ir_node *n)
3906 ir_node *left = get_Cmp_left(n);
3907 ir_node *right = get_Cmp_right(n);
3908 ir_mode *mode = get_irn_mode(left);
3909 ir_tarval *tv = NULL;
3910 bool changed = false;
3911 bool changedc = false;
3912 ir_relation relation = get_Cmp_relation(n);
3913 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3915 /* mask out impossible relations */
3916 ir_relation new_relation = relation & possible;
3917 if (new_relation != relation) {
3918 relation = new_relation;
3922 /* Remove unnecessary conversions */
3923 if (is_Conv(left) && is_Conv(right)) {
3924 ir_node *op_left = get_Conv_op(left);
3925 ir_node *op_right = get_Conv_op(right);
3926 ir_mode *mode_left = get_irn_mode(op_left);
3927 ir_mode *mode_right = get_irn_mode(op_right);
3929 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3930 && mode_left != mode_b && mode_right != mode_b) {
3931 ir_node *block = get_nodes_block(n);
3933 if (mode_left == mode_right) {
3937 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3938 } else if (smaller_mode(mode_left, mode_right)) {
3939 left = new_r_Conv(block, op_left, mode_right);
3942 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3943 } else if (smaller_mode(mode_right, mode_left)) {
3945 right = new_r_Conv(block, op_right, mode_left);
3947 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3949 mode = get_irn_mode(left);
3952 if (is_Conv(left) && is_Const(right)) {
3953 ir_node *op_left = get_Conv_op(left);
3954 ir_mode *mode_left = get_irn_mode(op_left);
3955 if (smaller_mode(mode_left, mode) && mode_left != mode_b) {
3956 ir_tarval *tv = get_Const_tarval(right);
3957 tarval_int_overflow_mode_t last_mode
3958 = tarval_get_integer_overflow_mode();
3960 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
3961 new_tv = tarval_convert_to(tv, mode_left);
3962 tarval_set_integer_overflow_mode(last_mode);
3963 if (new_tv != tarval_bad) {
3964 ir_graph *irg = get_irn_irg(n);
3966 right = new_r_Const(irg, new_tv);
3967 mode = get_irn_mode(left);
3969 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3975 * Optimize -a CMP -b into b CMP a.
3976 * This works only for modes where unary Minus cannot Overflow.
3977 * Note that two-complement integers can Overflow so it will NOT work.
3979 if (!mode_overflow_on_unary_Minus(mode) &&
3980 is_Minus(left) && is_Minus(right)) {
3981 left = get_Minus_op(left);
3982 right = get_Minus_op(right);
3983 relation = get_inversed_relation(relation);
3985 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3988 /* remove operation on both sides if possible */
3989 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3991 * The following operations are NOT safe for floating point operations, for instance
3992 * 1.0 + inf == 2.0 + inf, =/=> x == y
3994 if (mode_is_int(mode)) {
3995 unsigned lop = get_irn_opcode(left);
3997 if (lop == get_irn_opcode(right)) {
3998 ir_node *ll, *lr, *rl, *rr;
4000 /* same operation on both sides, try to remove */
4004 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4005 left = get_unop_op(left);
4006 right = get_unop_op(right);
4008 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4011 ll = get_Add_left(left);
4012 lr = get_Add_right(left);
4013 rl = get_Add_left(right);
4014 rr = get_Add_right(right);
4017 /* X + a CMP X + b ==> a CMP b */
4021 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4022 } else if (ll == rr) {
4023 /* X + a CMP b + X ==> a CMP b */
4027 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4028 } else if (lr == rl) {
4029 /* a + X CMP X + b ==> a CMP b */
4033 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4034 } else if (lr == rr) {
4035 /* a + X CMP b + X ==> a CMP b */
4039 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4043 ll = get_Sub_left(left);
4044 lr = get_Sub_right(left);
4045 rl = get_Sub_left(right);
4046 rr = get_Sub_right(right);
4049 /* X - a CMP X - b ==> a CMP b */
4053 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4054 } else if (lr == rr) {
4055 /* a - X CMP b - X ==> a CMP b */
4059 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4063 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4064 /* a ROTL X CMP b ROTL X ==> a CMP b */
4065 left = get_Rotl_left(left);
4066 right = get_Rotl_left(right);
4068 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4076 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4077 if (is_Add(left) || is_Sub(left)) {
4078 ir_node *ll = get_binop_left(left);
4079 ir_node *lr = get_binop_right(left);
4081 if (lr == right && is_Add(left)) {
4087 ir_graph *irg = get_irn_irg(n);
4089 right = create_zero_const(irg, mode);
4091 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4094 if (is_Add(right) || is_Sub(right)) {
4095 ir_node *rl = get_binop_left(right);
4096 ir_node *rr = get_binop_right(right);
4098 if (rr == left && is_Add(right)) {
4104 ir_graph *irg = get_irn_irg(n);
4106 right = create_zero_const(irg, mode);
4108 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4112 if (is_And(left) && is_Const(right)) {
4113 ir_node *ll = get_binop_left(left);
4114 ir_node *lr = get_binop_right(left);
4115 if (is_Shr(ll) && is_Const(lr)) {
4116 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4117 ir_node *block = get_nodes_block(n);
4118 ir_mode *mode = get_irn_mode(left);
4120 ir_node *llr = get_Shr_right(ll);
4121 if (is_Const(llr)) {
4122 dbg_info *dbg = get_irn_dbg_info(left);
4123 ir_graph *irg = get_irn_irg(left);
4125 ir_tarval *c1 = get_Const_tarval(llr);
4126 ir_tarval *c2 = get_Const_tarval(lr);
4127 ir_tarval *c3 = get_Const_tarval(right);
4128 ir_tarval *mask = tarval_shl(c2, c1);
4129 ir_tarval *value = tarval_shl(c3, c1);
4131 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4132 right = new_r_Const(irg, value);
4137 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4139 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4140 right = get_Eor_right(left);
4141 left = get_Eor_left(left);
4144 } /* mode_is_int(...) */
4147 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4148 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4149 if (mode_is_int(mode) && is_And(left)
4150 && (relation == ir_relation_equal
4151 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4152 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4153 ir_node *and0 = get_And_left(left);
4154 ir_node *and1 = get_And_right(left);
4155 if (and1 == right) {
4156 ir_node *tmp = and0;
4160 if (and0 == right && is_single_bit(and0)) {
4161 ir_graph *irg = get_irn_irg(n);
4163 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4164 right = create_zero_const(irg, mode);
4169 /* replace mode_b compares with ands/ors */
4170 if (mode == mode_b) {
4171 ir_node *block = get_nodes_block(n);
4175 case ir_relation_less_equal:
4176 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4178 case ir_relation_less:
4179 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4181 case ir_relation_greater_equal:
4182 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4184 case ir_relation_greater:
4185 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4187 case ir_relation_less_greater:
4188 bres = new_r_Eor(block, left, right, mode_b);
4190 case ir_relation_equal:
4191 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4194 #ifdef DEBUG_libfirm
4195 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4200 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4206 * First step: normalize the compare op
4207 * by placing the constant on the right side
4208 * or moving the lower address node to the left.
4210 if (!operands_are_normalized(left, right)) {
4215 relation = get_inversed_relation(relation);
4220 * Second step: Try to reduce the magnitude
4221 * of a constant. This may help to generate better code
4222 * later and may help to normalize more compares.
4223 * Of course this is only possible for integer values.
4225 tv = value_of(right);
4226 if (tv != tarval_bad) {
4227 ir_mode *mode = get_irn_mode(right);
4229 /* TODO extend to arbitrary constants */
4230 if (is_Conv(left) && tarval_is_null(tv)) {
4231 ir_node *op = get_Conv_op(left);
4232 ir_mode *op_mode = get_irn_mode(op);
4235 * UpConv(x) REL 0 ==> x REL 0
4236 * Don't do this for float values as it's unclear whether it is a
4237 * win. (on the other side it makes detection/creation of fabs hard)
4239 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4240 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4241 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4242 !mode_is_float(mode)) {
4243 tv = get_mode_null(op_mode);
4247 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4251 if (tv != tarval_bad) {
4252 /* the following optimization is possible on modes without Overflow
4253 * on Unary Minus or on == and !=:
4254 * -a CMP c ==> a swap(CMP) -c
4256 * Beware: for two-complement Overflow may occur, so only == and != can
4257 * be optimized, see this:
4258 * -MININT < 0 =/=> MININT > 0 !!!
4260 if (is_Minus(left) &&
4261 (!mode_overflow_on_unary_Minus(mode) ||
4262 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4263 tv = tarval_neg(tv);
4265 if (tv != tarval_bad) {
4266 left = get_Minus_op(left);
4267 relation = get_inversed_relation(relation);
4269 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4271 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4272 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4273 tv = tarval_not(tv);
4275 if (tv != tarval_bad) {
4276 left = get_Not_op(left);
4278 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4282 /* for integer modes, we have more */
4283 if (mode_is_int(mode) && !is_Const(left)) {
4284 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4285 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4286 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4287 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4289 if (tv != tarval_bad) {
4290 relation ^= ir_relation_equal;
4292 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4295 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4296 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4297 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4298 tv = tarval_add(tv, get_mode_one(mode));
4300 if (tv != tarval_bad) {
4301 relation ^= ir_relation_equal;
4303 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4307 /* the following reassociations work only for == and != */
4308 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4309 if (tv != tarval_bad) {
4310 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4312 ir_node *c1 = get_Sub_right(left);
4313 ir_tarval *tv2 = value_of(c1);
4315 if (tv2 != tarval_bad) {
4316 tv2 = tarval_add(tv, value_of(c1));
4318 if (tv2 != tarval_bad) {
4319 left = get_Sub_left(left);
4322 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4326 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4327 else if (is_Add(left)) {
4328 ir_node *a_l = get_Add_left(left);
4329 ir_node *a_r = get_Add_right(left);
4333 if (is_Const(a_l)) {
4335 tv2 = value_of(a_l);
4338 tv2 = value_of(a_r);
4341 if (tv2 != tarval_bad) {
4342 tv2 = tarval_sub(tv, tv2, NULL);
4344 if (tv2 != tarval_bad) {
4348 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4352 /* -a == c ==> a == -c, -a != c ==> a != -c */
4353 else if (is_Minus(left)) {
4354 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4356 if (tv2 != tarval_bad) {
4357 left = get_Minus_op(left);
4360 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4367 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4368 switch (get_irn_opcode(left)) {
4372 c1 = get_And_right(left);
4375 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4376 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4378 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
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);
4388 if (tarval_is_single_bit(tv)) {
4390 * optimization for AND:
4392 * And(x, C) == C ==> And(x, C) != 0
4393 * And(x, C) != C ==> And(X, C) == 0
4395 * if C is a single Bit constant.
4398 /* check for Constant's match. We have check hare the tarvals,
4399 because our const might be changed */
4400 if (get_Const_tarval(c1) == tv) {
4401 /* fine: do the transformation */
4402 tv = get_mode_null(get_tarval_mode(tv));
4403 relation ^= ir_relation_less_equal_greater;
4405 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4411 c1 = get_Or_right(left);
4412 if (is_Const(c1) && tarval_is_null(tv)) {
4414 * Or(x, C) == 0 && C != 0 ==> FALSE
4415 * Or(x, C) != 0 && C != 0 ==> TRUE
4417 if (! tarval_is_null(get_Const_tarval(c1))) {
4418 /* TODO: move to constant evaluation */
4419 ir_graph *irg = get_irn_irg(n);
4420 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4421 c1 = new_r_Const(irg, tv);
4422 DBG_OPT_CSTEVAL(n, c1);
4429 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4431 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4434 c1 = get_Shl_right(left);
4436 ir_graph *irg = get_irn_irg(c1);
4437 ir_tarval *tv1 = get_Const_tarval(c1);
4438 ir_mode *mode = get_irn_mode(left);
4439 ir_tarval *minus1 = get_mode_all_one(mode);
4440 ir_tarval *amask = tarval_shr(minus1, tv1);
4441 ir_tarval *cmask = tarval_shl(minus1, tv1);
4444 if (tarval_and(tv, cmask) != tv) {
4445 /* condition not met */
4446 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4447 c1 = new_r_Const(irg, tv);
4448 DBG_OPT_CSTEVAL(n, c1);
4451 sl = get_Shl_left(left);
4452 blk = get_nodes_block(n);
4453 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4454 tv = tarval_shr(tv, tv1);
4456 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4461 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4463 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4466 c1 = get_Shr_right(left);
4468 ir_graph *irg = get_irn_irg(c1);
4469 ir_tarval *tv1 = get_Const_tarval(c1);
4470 ir_mode *mode = get_irn_mode(left);
4471 ir_tarval *minus1 = get_mode_all_one(mode);
4472 ir_tarval *amask = tarval_shl(minus1, tv1);
4473 ir_tarval *cmask = tarval_shr(minus1, tv1);
4476 if (tarval_and(tv, cmask) != tv) {
4477 /* condition not met */
4478 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4479 c1 = new_r_Const(irg, tv);
4480 DBG_OPT_CSTEVAL(n, c1);
4483 sl = get_Shr_left(left);
4484 blk = get_nodes_block(n);
4485 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4486 tv = tarval_shl(tv, tv1);
4488 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4493 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4495 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4498 c1 = get_Shrs_right(left);
4500 ir_graph *irg = get_irn_irg(c1);
4501 ir_tarval *tv1 = get_Const_tarval(c1);
4502 ir_mode *mode = get_irn_mode(left);
4503 ir_tarval *minus1 = get_mode_all_one(mode);
4504 ir_tarval *amask = tarval_shl(minus1, tv1);
4505 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4508 cond = tarval_sub(cond, tv1, NULL);
4509 cond = tarval_shrs(tv, cond);
4511 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4512 /* condition not met */
4513 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4514 c1 = new_r_Const(irg, tv);
4515 DBG_OPT_CSTEVAL(n, c1);
4518 sl = get_Shrs_left(left);
4519 blk = get_nodes_block(n);
4520 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4521 tv = tarval_shl(tv, tv1);
4523 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4528 } /* tarval != bad */
4531 if (changedc) { /* need a new Const */
4532 ir_graph *irg = get_irn_irg(n);
4533 right = new_r_Const(irg, tv);
4537 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4538 ir_node *op = get_Proj_pred(left);
4540 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4541 ir_node *c = get_binop_right(op);
4544 ir_tarval *tv = get_Const_tarval(c);
4546 if (tarval_is_single_bit(tv)) {
4547 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4548 ir_node *v = get_binop_left(op);
4549 ir_node *blk = get_irn_n(op, -1);
4550 ir_graph *irg = get_irn_irg(op);
4551 ir_mode *mode = get_irn_mode(v);
4553 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4554 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4556 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4563 dbg_info *dbgi = get_irn_dbg_info(n);
4564 ir_node *block = get_nodes_block(n);
4566 /* create a new compare */
4567 n = new_rd_Cmp(dbgi, block, left, right, relation);
4574 * Optimize CopyB(mem, x, x) into a Nop.
4576 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4578 ir_node *copyb = get_Proj_pred(proj);
4579 ir_node *a = get_CopyB_dst(copyb);
4580 ir_node *b = get_CopyB_src(copyb);
4583 switch (get_Proj_proj(proj)) {
4584 case pn_CopyB_X_regular:
4585 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4586 DBG_OPT_EXC_REM(proj);
4587 proj = new_r_Jmp(get_nodes_block(copyb));
4589 case pn_CopyB_X_except: {
4590 ir_graph *irg = get_irn_irg(proj);
4591 DBG_OPT_EXC_REM(proj);
4592 proj = new_r_Bad(irg, mode_X);
4600 } /* transform_node_Proj_CopyB */
4603 * Optimize Bounds(idx, idx, upper) into idx.
4605 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4607 ir_node *oldn = proj;
4608 ir_node *bound = get_Proj_pred(proj);
4609 ir_node *idx = get_Bound_index(bound);
4610 ir_node *pred = skip_Proj(idx);
4613 if (idx == get_Bound_lower(bound))
4615 else if (is_Bound(pred)) {
4617 * idx was Bounds checked previously, it is still valid if
4618 * lower <= pred_lower && pred_upper <= upper.
4620 ir_node *lower = get_Bound_lower(bound);
4621 ir_node *upper = get_Bound_upper(bound);
4622 if (get_Bound_lower(pred) == lower &&
4623 get_Bound_upper(pred) == upper) {
4625 * One could expect that we simply return the previous
4626 * Bound here. However, this would be wrong, as we could
4627 * add an exception Proj to a new location then.
4628 * So, we must turn in into a tuple.
4634 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4635 switch (get_Proj_proj(proj)) {
4637 DBG_OPT_EXC_REM(proj);
4638 proj = get_Bound_mem(bound);
4640 case pn_Bound_X_except:
4641 DBG_OPT_EXC_REM(proj);
4642 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4646 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4648 case pn_Bound_X_regular:
4649 DBG_OPT_EXC_REM(proj);
4650 proj = new_r_Jmp(get_nodes_block(bound));
4657 } /* transform_node_Proj_Bound */
4660 * Does all optimizations on nodes that must be done on its Projs
4661 * because of creating new nodes.
4663 static ir_node *transform_node_Proj(ir_node *proj)
4665 ir_node *n = get_Proj_pred(proj);
4667 if (n->op->ops.transform_node_Proj)
4668 return n->op->ops.transform_node_Proj(proj);
4670 } /* transform_node_Proj */
4673 * Test whether a block is unreachable
4674 * Note: That this only returns true when
4675 * IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE is set.
4676 * This is important, as you easily end up producing invalid constructs in the
4677 * unreachable code when optimizing away edges into the unreachable code.
4678 * So only set this flag when you iterate localopts to the fixpoint.
4679 * When you reach the fixpoint then all unreachable code is dead
4680 * (= can't be reached by firm edges) and you won't see the invalid constructs
4683 static bool is_block_unreachable(const ir_node *block)
4685 const ir_graph *irg = get_irn_irg(block);
4686 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4688 return get_Block_dom_depth(block) < 0;
4691 static ir_node *transform_node_Block(ir_node *block)
4693 ir_graph *irg = get_irn_irg(block);
4694 int arity = get_irn_arity(block);
4695 ir_node *bad = NULL;
4698 if (!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
4701 for (i = 0; i < arity; ++i) {
4702 ir_node *const pred = get_Block_cfgpred(block, i);
4703 if (is_Bad(pred) || !is_block_unreachable(get_nodes_block(pred)))
4706 bad = new_r_Bad(irg, mode_X);
4707 set_irn_n(block, i, bad);
4713 static ir_node *transform_node_Phi(ir_node *phi)
4715 int n = get_irn_arity(phi);
4716 ir_mode *mode = get_irn_mode(phi);
4717 ir_node *block = get_nodes_block(phi);
4718 ir_graph *irg = get_irn_irg(phi);
4719 ir_node *bad = NULL;
4722 /* Set phi-operands for bad-block inputs to bad */
4723 for (i = 0; i < n; ++i) {
4724 if (!is_Bad(get_Phi_pred(phi, i))) {
4725 ir_node *pred = get_Block_cfgpred(block, i);
4726 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4728 bad = new_r_Bad(irg, mode);
4729 set_irn_n(phi, i, bad);
4734 /* Move Pin nodes down through Phi nodes. */
4735 if (mode == mode_M) {
4736 n = get_irn_arity(phi);
4738 /* Beware of Phi0 */
4742 bool has_pin = false;
4744 NEW_ARR_A(ir_node *, in, n);
4746 for (i = 0; i < n; ++i) {
4747 ir_node *pred = get_irn_n(phi, i);
4750 in[i] = get_Pin_op(pred);
4752 } else if (is_Bad(pred)) {
4762 /* Move the Pin nodes "behind" the Phi. */
4763 block = get_irn_n(phi, -1);
4764 new_phi = new_r_Phi(block, n, in, mode_M);
4765 return new_r_Pin(block, new_phi);
4768 /* Move Confirms down through Phi nodes. */
4769 else if (mode_is_reference(mode)) {
4770 n = get_irn_arity(phi);
4772 /* Beware of Phi0 */
4774 ir_node *pred = get_irn_n(phi, 0);
4775 ir_node *bound, *new_phi, *block, **in;
4776 ir_relation relation;
4777 bool has_confirm = false;
4779 if (! is_Confirm(pred))
4782 bound = get_Confirm_bound(pred);
4783 relation = get_Confirm_relation(pred);
4785 NEW_ARR_A(ir_node *, in, n);
4786 in[0] = get_Confirm_value(pred);
4788 for (i = 1; i < n; ++i) {
4789 pred = get_irn_n(phi, i);
4791 if (is_Confirm(pred) &&
4792 get_Confirm_bound(pred) == bound &&
4793 get_Confirm_relation(pred) == relation) {
4794 in[i] = get_Confirm_value(pred);
4796 } else if (is_Bad(pred)) {
4806 /* move the Confirm nodes "behind" the Phi */
4807 block = get_irn_n(phi, -1);
4808 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4809 return new_r_Confirm(block, new_phi, bound, relation);
4816 * Returns the operands of a commutative bin-op, if one operand is
4817 * a const, it is returned as the second one.
4819 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4821 ir_node *op_a = get_binop_left(binop);
4822 ir_node *op_b = get_binop_right(binop);
4824 assert(is_op_commutative(get_irn_op(binop)));
4826 if (is_Const(op_a)) {
4833 } /* get_comm_Binop_Ops */
4836 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4837 * Such pattern may arise in bitfield stores.
4839 * value c4 value c4 & c2
4840 * AND c3 AND c1 | c3
4847 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4850 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4852 ir_node *irn_and, *c1;
4854 ir_node *and_l, *c3;
4855 ir_node *value, *c4;
4856 ir_node *new_and, *new_const, *block;
4857 ir_mode *mode = get_irn_mode(irn_or);
4859 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4863 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4864 if (!is_Const(c1) || !is_And(irn_and))
4867 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4871 tv1 = get_Const_tarval(c1);
4872 tv2 = get_Const_tarval(c2);
4874 tv = tarval_or(tv1, tv2);
4875 if (tarval_is_all_one(tv)) {
4876 /* the AND does NOT clear a bit with isn't set by the OR */
4877 set_Or_left(irn_or, or_l);
4878 set_Or_right(irn_or, c1);
4880 /* check for more */
4887 get_comm_Binop_Ops(or_l, &and_l, &c3);
4888 if (!is_Const(c3) || !is_And(and_l))
4891 get_comm_Binop_Ops(and_l, &value, &c4);
4895 /* ok, found the pattern, check for conditions */
4896 assert(mode == get_irn_mode(irn_and));
4897 assert(mode == get_irn_mode(or_l));
4898 assert(mode == get_irn_mode(and_l));
4900 tv3 = get_Const_tarval(c3);
4901 tv4 = get_Const_tarval(c4);
4903 tv = tarval_or(tv4, tv2);
4904 if (!tarval_is_all_one(tv)) {
4905 /* have at least one 0 at the same bit position */
4909 if (tv3 != tarval_andnot(tv3, tv4)) {
4910 /* bit in the or_mask is outside the and_mask */
4914 if (tv1 != tarval_andnot(tv1, tv2)) {
4915 /* bit in the or_mask is outside the and_mask */
4919 /* ok, all conditions met */
4920 block = get_irn_n(irn_or, -1);
4921 irg = get_irn_irg(block);
4923 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4925 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4927 set_Or_left(irn_or, new_and);
4928 set_Or_right(irn_or, new_const);
4930 /* check for more */
4932 } /* transform_node_Or_bf_store */
4935 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4937 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4939 ir_mode *mode = get_irn_mode(irn_or);
4940 ir_node *shl, *shr, *block;
4941 ir_node *irn, *x, *c1, *c2, *n;
4942 ir_tarval *tv1, *tv2;
4944 /* some backends can't handle rotl */
4945 if (!be_get_backend_param()->support_rotl)
4948 if (! mode_is_int(mode))
4951 shl = get_binop_left(irn_or);
4952 shr = get_binop_right(irn_or);
4961 } else if (!is_Shl(shl)) {
4963 } else if (!is_Shr(shr)) {
4966 x = get_Shl_left(shl);
4967 if (x != get_Shr_left(shr))
4970 c1 = get_Shl_right(shl);
4971 c2 = get_Shr_right(shr);
4972 if (is_Const(c1) && is_Const(c2)) {
4973 tv1 = get_Const_tarval(c1);
4974 if (! tarval_is_long(tv1))
4977 tv2 = get_Const_tarval(c2);
4978 if (! tarval_is_long(tv2))
4981 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4982 != (int) get_mode_size_bits(mode))
4985 /* yet, condition met */
4986 block = get_nodes_block(irn_or);
4988 n = new_r_Rotl(block, x, c1, mode);
4990 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4994 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4995 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4996 if (!ir_is_negated_value(c1, c2)) {
5000 /* yet, condition met */
5001 block = get_nodes_block(irn_or);
5002 n = new_r_Rotl(block, x, c1, mode);
5003 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
5005 } /* transform_node_Or_Rotl */
5007 static bool is_cmp_unequal(const ir_node *node)
5009 ir_relation relation = get_Cmp_relation(node);
5010 ir_node *left = get_Cmp_left(node);
5011 ir_node *right = get_Cmp_right(node);
5012 ir_mode *mode = get_irn_mode(left);
5014 if (relation == ir_relation_less_greater)
5017 if (!mode_is_signed(mode) && is_Const(right) && is_Const_null(right))
5018 return relation == ir_relation_greater;
5023 * returns true for Cmp(x == 0) or Cmp(x != 0)
5025 static bool is_cmp_equality_zero(const ir_node *node)
5027 ir_relation relation;
5028 ir_node *right = get_Cmp_right(node);
5030 if (!is_Const(right) || !is_Const_null(right))
5032 relation = get_Cmp_relation(node);
5033 return relation == ir_relation_equal
5034 || relation == ir_relation_less_greater
5035 || (!mode_is_signed(get_irn_mode(right))
5036 && relation == ir_relation_greater);
5042 static ir_node *transform_node_Or(ir_node *n)
5044 ir_node *c, *oldn = n;
5045 ir_node *a = get_Or_left(n);
5046 ir_node *b = get_Or_right(n);
5049 if (is_Not(a) && is_Not(b)) {
5050 /* ~a | ~b = ~(a&b) */
5051 ir_node *block = get_nodes_block(n);
5053 mode = get_irn_mode(n);
5056 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5057 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5058 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5062 /* we can combine the relations of two compares with the same operands */
5063 if (is_Cmp(a) && is_Cmp(b)) {
5064 ir_node *a_left = get_Cmp_left(a);
5065 ir_node *a_right = get_Cmp_right(a);
5066 ir_node *b_left = get_Cmp_left(b);
5067 ir_node *b_right = get_Cmp_right(b);
5068 if (a_left == b_left && b_left == b_right) {
5069 dbg_info *dbgi = get_irn_dbg_info(n);
5070 ir_node *block = get_nodes_block(n);
5071 ir_relation a_relation = get_Cmp_relation(a);
5072 ir_relation b_relation = get_Cmp_relation(b);
5073 ir_relation new_relation = a_relation | b_relation;
5074 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
5076 /* Cmp(a!=b) or Cmp(c!=d) => Cmp((a^b)|(c^d) != 0) */
5077 if (is_cmp_unequal(a) && is_cmp_unequal(b)
5078 && !mode_is_float(get_irn_mode(a_left))
5079 && !mode_is_float(get_irn_mode(b_left))) {
5080 if (values_in_mode(get_irn_mode(a_left), get_irn_mode(b_left))) {
5081 ir_graph *irg = get_irn_irg(n);
5082 dbg_info *dbgi = get_irn_dbg_info(n);
5083 ir_node *block = get_nodes_block(n);
5084 ir_mode *a_mode = get_irn_mode(a_left);
5085 ir_mode *b_mode = get_irn_mode(b_left);
5086 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5087 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5088 ir_node *conv = new_rd_Conv(dbgi, block, xora, b_mode);
5089 ir_node *or = new_rd_Or(dbgi, block, conv, xorb, b_mode);
5090 ir_node *zero = create_zero_const(irg, b_mode);
5091 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5093 if (values_in_mode(get_irn_mode(b_left), get_irn_mode(a_left))) {
5094 ir_graph *irg = get_irn_irg(n);
5095 dbg_info *dbgi = get_irn_dbg_info(n);
5096 ir_node *block = get_nodes_block(n);
5097 ir_mode *a_mode = get_irn_mode(a_left);
5098 ir_mode *b_mode = get_irn_mode(b_left);
5099 ir_node *xora = new_rd_Eor(dbgi, block, a_left, a_right, a_mode);
5100 ir_node *xorb = new_rd_Eor(dbgi, block, b_left, b_right, b_mode);
5101 ir_node *conv = new_rd_Conv(dbgi, block, xorb, a_mode);
5102 ir_node *or = new_rd_Or(dbgi, block, xora, conv, a_mode);
5103 ir_node *zero = create_zero_const(irg, a_mode);
5104 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
5109 mode = get_irn_mode(n);
5110 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5112 n = transform_node_Or_bf_store(n);
5113 n = transform_node_Or_Rotl(n);
5117 n = transform_bitwise_distributive(n, transform_node_Or);
5119 n = transform_node_bitop_shift(n);
5124 vrp_attr *a_vrp = vrp_get_info(a);
5125 vrp_attr *b_vrp = vrp_get_info(b);
5126 if (a_vrp != NULL && b_vrp != NULL) {
5127 ir_tarval *vrp_val = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
5129 if (tarval_is_null(vrp_val)) {
5130 dbg_info *dbgi = get_irn_dbg_info(n);
5131 return new_rd_Add(dbgi, get_nodes_block(n), a, b, mode);
5137 } /* transform_node_Or */
5141 static ir_node *transform_node(ir_node *n);
5144 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5146 * Should be moved to reassociation?
5148 static ir_node *transform_node_shift(ir_node *n)
5150 ir_node *left, *right;
5152 ir_mode *count_mode;
5153 ir_tarval *tv1, *tv2, *res;
5154 ir_node *in[2], *irn, *block;
5158 left = get_binop_left(n);
5160 /* different operations */
5161 if (get_irn_op(left) != get_irn_op(n))
5164 right = get_binop_right(n);
5165 tv1 = value_of(right);
5166 if (tv1 == tarval_bad)
5169 tv2 = value_of(get_binop_right(left));
5170 if (tv2 == tarval_bad)
5173 count_mode = get_tarval_mode(tv1);
5174 if (get_tarval_mode(tv2) != count_mode) {
5175 /* TODO: search bigger mode or something and convert... */
5179 mode = get_irn_mode(n);
5180 modulo_shf = get_mode_modulo_shift(mode);
5182 if (modulo_shf > 0) {
5183 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
5185 /* I'm not so sure what happens in one complement... */
5186 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
5187 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
5188 * above will be invalid) */
5189 assert(modulo_shf<=0 || is_po2(modulo_shf));
5191 tv1 = tarval_and(tv1, modulo_mask);
5192 tv2 = tarval_and(tv2, modulo_mask);
5194 res = tarval_add(tv1, tv2);
5195 irg = get_irn_irg(n);
5197 /* beware: a simple replacement works only, if res < modulo shift */
5199 int bits = get_mode_size_bits(mode);
5200 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
5201 res = tarval_mod(res, modulo);
5203 long bits = get_mode_size_bits(mode);
5204 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
5206 /* shifting too much */
5207 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
5209 ir_node *block = get_nodes_block(n);
5210 dbg_info *dbgi = get_irn_dbg_info(n);
5211 ir_mode *smode = get_irn_mode(right);
5212 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5213 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5216 return new_r_Const(irg, get_mode_null(mode));
5220 /* ok, we can replace it */
5221 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5222 block = get_nodes_block(n);
5224 in[0] = get_binop_left(left);
5225 in[1] = new_r_Const(irg, res);
5227 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5229 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5231 return transform_node(irn);
5236 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5238 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5239 * (also with x >>s c1 when c1>=c2)
5241 static ir_node *transform_node_shl_shr(ir_node *n)
5244 ir_node *right = get_binop_right(n);
5254 ir_tarval *tv_shift;
5257 ir_relation relation;
5260 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5262 if (!is_Const(right))
5265 left = get_binop_left(n);
5266 mode = get_irn_mode(n);
5267 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5268 ir_node *shr_right = get_binop_right(left);
5270 if (!is_Const(shr_right))
5273 x = get_binop_left(left);
5274 tv_shr = get_Const_tarval(shr_right);
5275 tv_shl = get_Const_tarval(right);
5277 if (is_Shrs(left)) {
5278 /* shrs variant only allowed if c1 >= c2 */
5279 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5282 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5285 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5287 tv_mask = tarval_shl(tv_mask, tv_shl);
5288 } else if (is_Shr(n) && is_Shl(left)) {
5289 ir_node *shl_right = get_Shl_right(left);
5291 if (!is_Const(shl_right))
5294 x = get_Shl_left(left);
5295 tv_shr = get_Const_tarval(right);
5296 tv_shl = get_Const_tarval(shl_right);
5298 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5299 tv_mask = tarval_shr(tv_mask, tv_shr);
5304 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5305 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5308 assert(tv_mask != tarval_bad);
5309 assert(get_tarval_mode(tv_mask) == mode);
5311 block = get_nodes_block(n);
5312 irg = get_irn_irg(block);
5313 dbgi = get_irn_dbg_info(n);
5315 relation = tarval_cmp(tv_shl, tv_shr);
5316 if (relation == ir_relation_less || relation == ir_relation_equal) {
5317 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5318 new_const = new_r_Const(irg, tv_shift);
5320 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5322 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5325 assert(relation == ir_relation_greater);
5326 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5327 new_const = new_r_Const(irg, tv_shift);
5328 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5331 new_const = new_r_Const(irg, tv_mask);
5332 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5337 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5339 ir_mode *mode = get_tarval_mode(tv);
5340 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5341 return tarval_mod(tv, modulo_tv);
5344 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5345 ir_node *left, ir_node *right, ir_mode *mode);
5348 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5349 * then we can use that to minimize the value of Add(x, const) or
5350 * Sub(Const, x). In particular this often avoids 1 instruction in some
5351 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5352 * by Shift(x, Minus(y)) which does not need an explicit Const constructed.
5354 static ir_node *transform_node_shift_modulo(ir_node *n,
5355 new_shift_func new_shift)
5357 ir_mode *mode = get_irn_mode(n);
5358 int modulo = get_mode_modulo_shift(mode);
5359 ir_node *newop = NULL;
5360 ir_mode *mode_right;
5367 if (get_mode_arithmetic(mode) != irma_twos_complement)
5369 if (!is_po2(modulo))
5372 irg = get_irn_irg(n);
5373 block = get_nodes_block(n);
5374 right = get_binop_right(n);
5375 mode_right = get_irn_mode(right);
5376 if (is_Const(right)) {
5377 ir_tarval *tv = get_Const_tarval(right);
5378 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5383 newop = new_r_Const(irg, tv_mod);
5384 } else if (is_Add(right)) {
5385 ir_node *add_right = get_Add_right(right);
5386 if (is_Const(add_right)) {
5387 ir_tarval *tv = get_Const_tarval(add_right);
5388 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5393 newconst = new_r_Const(irg, tv_mod);
5394 newop = new_r_Add(block, get_Add_left(right), newconst,
5397 } else if (is_Sub(right)) {
5398 ir_node *sub_left = get_Sub_left(right);
5399 if (is_Const(sub_left)) {
5400 ir_tarval *tv = get_Const_tarval(sub_left);
5401 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5406 newconst = new_r_Const(irg, tv_mod);
5407 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5414 if (newop != NULL) {
5415 dbg_info *dbgi = get_irn_dbg_info(n);
5416 ir_node *left = get_binop_left(n);
5417 return new_shift(dbgi, block, left, newop, mode);
5425 static ir_node *transform_node_Shr(ir_node *n)
5427 ir_node *c, *oldn = n;
5428 ir_node *left = get_Shr_left(n);
5429 ir_node *right = get_Shr_right(n);
5430 ir_mode *mode = get_irn_mode(n);
5432 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5433 n = transform_node_shift(n);
5436 n = transform_node_shift_modulo(n, new_rd_Shr);
5438 n = transform_node_shl_shr(n);
5440 n = transform_node_shift_bitop(n);
5443 } /* transform_node_Shr */
5448 static ir_node *transform_node_Shrs(ir_node *n)
5450 ir_node *c, *oldn = n;
5451 ir_node *a = get_Shrs_left(n);
5452 ir_node *b = get_Shrs_right(n);
5453 ir_mode *mode = get_irn_mode(n);
5455 if (is_oversize_shift(n)) {
5456 ir_node *block = get_nodes_block(n);
5457 dbg_info *dbgi = get_irn_dbg_info(n);
5458 ir_mode *cmode = get_irn_mode(b);
5459 long val = get_mode_size_bits(cmode)-1;
5460 ir_graph *irg = get_irn_irg(n);
5461 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5462 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5465 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5466 n = transform_node_shift(n);
5469 n = transform_node_shift_modulo(n, new_rd_Shrs);
5471 n = transform_node_shift_bitop(n);
5474 } /* transform_node_Shrs */
5479 static ir_node *transform_node_Shl(ir_node *n)
5481 ir_node *c, *oldn = n;
5482 ir_node *a = get_Shl_left(n);
5483 ir_node *b = get_Shl_right(n);
5484 ir_mode *mode = get_irn_mode(n);
5486 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5487 n = transform_node_shift(n);
5490 n = transform_node_shift_modulo(n, new_rd_Shl);
5492 n = transform_node_shl_shr(n);
5494 n = transform_node_shift_bitop(n);
5497 } /* transform_node_Shl */
5502 static ir_node *transform_node_Rotl(ir_node *n)
5504 ir_node *c, *oldn = n;
5505 ir_node *a = get_Rotl_left(n);
5506 ir_node *b = get_Rotl_right(n);
5507 ir_mode *mode = get_irn_mode(n);
5509 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5510 n = transform_node_shift(n);
5513 n = transform_node_shift_bitop(n);
5516 } /* transform_node_Rotl */
5521 static ir_node *transform_node_Conv(ir_node *n)
5523 ir_node *c, *oldn = n;
5524 ir_mode *mode = get_irn_mode(n);
5525 ir_node *a = get_Conv_op(n);
5527 if (mode != mode_b && is_const_Phi(a)) {
5528 /* Do NOT optimize mode_b Conv's, this leads to remaining
5529 * Phib nodes later, because the conv_b_lower operation
5530 * is instantly reverted, when it tries to insert a Convb.
5532 c = apply_conv_on_phi(a, mode);
5534 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5539 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5540 ir_graph *irg = get_irn_irg(n);
5541 return new_r_Unknown(irg, mode);
5544 if (mode_is_reference(mode) &&
5545 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5547 ir_node *l = get_Add_left(a);
5548 ir_node *r = get_Add_right(a);
5549 dbg_info *dbgi = get_irn_dbg_info(a);
5550 ir_node *block = get_nodes_block(n);
5552 ir_node *lop = get_Conv_op(l);
5553 if (get_irn_mode(lop) == mode) {
5554 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5555 n = new_rd_Add(dbgi, block, lop, r, mode);
5560 ir_node *rop = get_Conv_op(r);
5561 if (get_irn_mode(rop) == mode) {
5562 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5563 n = new_rd_Add(dbgi, block, l, rop, mode);
5570 } /* transform_node_Conv */
5573 * Remove dead blocks and nodes in dead blocks
5574 * in keep alive list. We do not generate a new End node.
5576 static ir_node *transform_node_End(ir_node *n)
5578 int i, j, n_keepalives = get_End_n_keepalives(n);
5581 NEW_ARR_A(ir_node *, in, n_keepalives);
5583 for (i = j = 0; i < n_keepalives; ++i) {
5584 ir_node *ka = get_End_keepalive(n, i);
5586 /* no need to keep Bad */
5589 /* do not keep unreachable code */
5590 block = is_Block(ka) ? ka : get_nodes_block(ka);
5591 if (is_block_unreachable(block))
5595 if (j != n_keepalives)
5596 set_End_keepalives(n, j, in);
5598 } /* transform_node_End */
5600 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5602 if (is_Minus(a) && get_Minus_op(a) == b)
5604 if (is_Minus(b) && get_Minus_op(b) == a)
5606 if (is_Sub(a) && is_Sub(b)) {
5607 ir_node *a_left = get_Sub_left(a);
5608 ir_node *a_right = get_Sub_right(a);
5609 ir_node *b_left = get_Sub_left(b);
5610 ir_node *b_right = get_Sub_right(b);
5612 if (a_left == b_right && a_right == b_left)
5619 static const ir_node *skip_upconv(const ir_node *node)
5621 while (is_Conv(node)) {
5622 ir_mode *mode = get_irn_mode(node);
5623 const ir_node *op = get_Conv_op(node);
5624 ir_mode *op_mode = get_irn_mode(op);
5625 if (!smaller_mode(op_mode, mode))
5632 int ir_mux_is_abs(const ir_node *sel, const ir_node *mux_false,
5633 const ir_node *mux_true)
5638 ir_relation relation;
5644 * Note further that these optimization work even for floating point
5645 * with NaN's because -NaN == NaN.
5646 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5649 mode = get_irn_mode(mux_true);
5650 if (mode_honor_signed_zeros(mode))
5653 /* must be <, <=, >=, > */
5654 relation = get_Cmp_relation(sel);
5655 if ((relation & ir_relation_less_greater) == 0)
5658 if (!ir_is_negated_value(mux_true, mux_false))
5661 mux_true = skip_upconv(mux_true);
5662 mux_false = skip_upconv(mux_false);
5664 /* must be x cmp 0 */
5665 cmp_right = get_Cmp_right(sel);
5666 if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
5669 cmp_left = get_Cmp_left(sel);
5670 if (cmp_left == mux_false) {
5671 if (relation & ir_relation_less) {
5674 assert(relation & ir_relation_greater);
5677 } else if (cmp_left == mux_true) {
5678 if (relation & ir_relation_less) {
5681 assert(relation & ir_relation_greater);
5689 ir_node *ir_get_abs_op(const ir_node *sel, ir_node *mux_false,
5692 ir_node *cmp_left = get_Cmp_left(sel);
5693 return cmp_left == skip_upconv(mux_false) ? mux_false : mux_true;
5696 bool ir_is_optimizable_mux(const ir_node *sel, const ir_node *mux_false,
5697 const ir_node *mux_true)
5699 /* this code should return true each time transform_node_Mux would
5700 * optimize the Mux completely away */
5702 ir_mode *mode = get_irn_mode(mux_false);
5703 if (get_mode_arithmetic(mode) == irma_twos_complement
5704 && ir_mux_is_abs(sel, mux_false, mux_true))
5707 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5708 const ir_node *cmp_r = get_Cmp_right(sel);
5709 const ir_node *cmp_l = get_Cmp_left(sel);
5710 const ir_node *f = mux_false;
5711 const ir_node *t = mux_true;
5713 if (is_Const(t) && is_Const_null(t)) {
5718 if (is_And(cmp_l) && f == cmp_r) {
5719 ir_node *and_r = get_And_right(cmp_l);
5722 if (and_r == t && is_single_bit(and_r))
5724 and_l = get_And_left(cmp_l);
5725 if (and_l == t && is_single_bit(and_l))
5734 * Optimize a Mux into some simpler cases.
5736 static ir_node *transform_node_Mux(ir_node *n)
5739 ir_node *sel = get_Mux_sel(n);
5740 ir_mode *mode = get_irn_mode(n);
5741 ir_node *t = get_Mux_true(n);
5742 ir_node *f = get_Mux_false(n);
5743 ir_graph *irg = get_irn_irg(n);
5745 /* implement integer abs: abs(x) = x^(x >>s 31) - (x >>s 31) */
5746 if (get_mode_arithmetic(mode) == irma_twos_complement) {
5747 int abs = ir_mux_is_abs(sel, f, t);
5749 dbg_info *dbgi = get_irn_dbg_info(n);
5750 ir_node *block = get_nodes_block(n);
5751 ir_node *op = ir_get_abs_op(sel, f, t);
5752 int bits = get_mode_size_bits(mode);
5753 ir_node *shiftconst = new_r_Const_long(irg, mode_Iu, bits-1);
5754 ir_node *sext = new_rd_Shrs(dbgi, block, op, shiftconst, mode);
5755 ir_node *xorn = new_rd_Eor(dbgi, block, op, sext, mode);
5758 res = new_rd_Sub(dbgi, block, xorn, sext, mode);
5760 res = new_rd_Sub(dbgi, block, sext, xorn, mode);
5766 /* first normalization step: try to move a constant to the false side,
5767 * 0 preferred on false side too */
5768 if (is_Cmp(sel) && is_Const(t) &&
5769 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5770 dbg_info *seldbgi = get_irn_dbg_info(sel);
5771 ir_node *block = get_nodes_block(sel);
5772 ir_relation relation = get_Cmp_relation(sel);
5777 /* Mux(x, a, b) => Mux(not(x), b, a) */
5778 relation = get_negated_relation(relation);
5779 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5780 get_Cmp_right(sel), relation);
5781 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5784 /* the following optimisations create new mode_b nodes, so only do them
5785 * before mode_b lowering */
5786 if (!is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED)) {
5788 ir_node* block = get_nodes_block(n);
5790 ir_node* c1 = get_Mux_sel(t);
5791 ir_node* t1 = get_Mux_true(t);
5792 ir_node* f1 = get_Mux_false(t);
5794 /* Mux(cond0, Mux(cond1, x, y), y) => Mux(cond0 && cond1, x, y) */
5795 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5796 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5801 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5802 } else if (f == t1) {
5803 /* Mux(cond0, Mux(cond1, x, y), x) */
5804 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5805 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5806 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5811 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5813 } else if (is_Mux(f)) {
5814 ir_node* block = get_nodes_block(n);
5816 ir_node* c1 = get_Mux_sel(f);
5817 ir_node* t1 = get_Mux_true(f);
5818 ir_node* f1 = get_Mux_false(f);
5820 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5821 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5822 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5827 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5828 } else if (t == f1) {
5829 /* Mux(cond0, x, Mux(cond1, y, x)) */
5830 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5831 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5832 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5837 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5841 /* note: after normalization, false can only happen on default */
5842 if (mode == mode_b) {
5843 dbg_info *dbg = get_irn_dbg_info(n);
5844 ir_node *block = get_nodes_block(n);
5847 ir_tarval *tv_t = get_Const_tarval(t);
5848 if (tv_t == tarval_b_true) {
5850 /* Muxb(sel, true, false) = sel */
5851 assert(get_Const_tarval(f) == tarval_b_false);
5852 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5855 /* Muxb(sel, true, x) = Or(sel, x) */
5856 n = new_rd_Or(dbg, block, sel, f, mode_b);
5857 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5861 } else if (is_Const(f)) {
5862 ir_tarval *tv_f = get_Const_tarval(f);
5863 if (tv_f == tarval_b_true) {
5864 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5865 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5867 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5870 /* Muxb(sel, x, false) = And(sel, x) */
5871 assert(tv_f == tarval_b_false);
5872 n = new_rd_And(dbg, block, sel, t, mode_b);
5873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5879 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5880 * value to integer. */
5881 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5882 ir_tarval *a = get_Const_tarval(t);
5883 ir_tarval *b = get_Const_tarval(f);
5885 if (tarval_is_one(a) && tarval_is_null(b)) {
5886 ir_node *block = get_nodes_block(n);
5887 ir_node *conv = new_r_Conv(block, sel, mode);
5889 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5891 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5892 ir_node *block = get_nodes_block(n);
5893 ir_node *not_ = new_r_Not(block, sel, mode_b);
5894 ir_node *conv = new_r_Conv(block, not_, mode);
5896 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5902 if (is_Cmp(sel) && mode_is_int(mode) && is_cmp_equality_zero(sel)) {
5903 ir_relation relation = get_Cmp_relation(sel);
5904 ir_node *cmp_r = get_Cmp_right(sel);
5905 ir_node *cmp_l = get_Cmp_left(sel);
5906 ir_node *block = get_nodes_block(n);
5908 if (is_And(cmp_l) && f == cmp_r) {
5909 ir_node *and_r = get_And_right(cmp_l);
5912 if (and_r == t && is_single_bit(and_r)) {
5913 if (relation == ir_relation_equal) {
5914 /* Mux((a & (1<<n)) == 0, (1<<n), 0) == (a&(1<<n)) xor ((1<<n)) */
5915 n = new_rd_Eor(get_irn_dbg_info(n),
5916 block, cmp_l, t, mode);
5917 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5919 /* Mux((a & (1<<n)) != 0, (1<<n), 0) == a & (1<<n) */
5921 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5925 and_l = get_And_left(cmp_l);
5926 if (and_l == t && is_single_bit(and_l)) {
5927 if (relation == ir_relation_equal) {
5928 /* ((1 << n) & a) == 0, (1 << n), 0) */
5929 n = new_rd_Eor(get_irn_dbg_info(n),
5930 block, cmp_l, t, mode);
5931 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5933 /* ((1 << n) & a) != 0, (1 << n), 0) */
5935 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5946 * optimize Sync nodes that have other syncs as input we simply add the inputs
5947 * of the other sync to our own inputs
5949 static ir_node *transform_node_Sync(ir_node *n)
5951 int arity = get_Sync_n_preds(n);
5954 for (i = 0; i < arity;) {
5955 ir_node *pred = get_Sync_pred(n, i);
5959 /* Remove Bad predecessors */
5966 /* Remove duplicate predecessors */
5967 for (j = 0; j < i; ++j) {
5968 if (get_Sync_pred(n, j) == pred) {
5977 if (!is_Sync(pred)) {
5985 pred_arity = get_Sync_n_preds(pred);
5986 for (j = 0; j < pred_arity; ++j) {
5987 ir_node *pred_pred = get_Sync_pred(pred, j);
5992 add_irn_n(n, pred_pred);
5996 if (get_Sync_pred(n, k) == pred_pred) break;
6002 ir_graph *irg = get_irn_irg(n);
6003 return new_r_Bad(irg, mode_M);
6006 return get_Sync_pred(n, 0);
6009 /* rehash the sync node */
6014 static ir_node *transform_node_Load(ir_node *n)
6016 /* if our memory predecessor is a load from the same address, then reuse the
6017 * previous result */
6018 ir_node *mem = get_Load_mem(n);
6023 /* don't touch volatile loads */
6024 if (get_Load_volatility(n) == volatility_is_volatile)
6026 mem_pred = get_Proj_pred(mem);
6027 if (is_Load(mem_pred)) {
6028 ir_node *pred_load = mem_pred;
6030 /* conservatively compare the 2 loads. TODO: This could be less strict
6031 * with fixup code in some situations (like smaller/bigger modes) */
6032 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
6034 if (get_Load_mode(pred_load) != get_Load_mode(n))
6036 /* all combinations of aligned/unaligned pred/n should be fine so we do
6037 * not compare the unaligned attribute */
6039 ir_node *block = get_nodes_block(n);
6040 ir_node *jmp = new_r_Jmp(block);
6041 ir_graph *irg = get_irn_irg(n);
6042 ir_node *bad = new_r_Bad(irg, mode_X);
6043 ir_mode *mode = get_Load_mode(n);
6044 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
6045 ir_node *in[] = { mem, res, jmp, bad };
6046 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6049 } else if (is_Store(mem_pred)) {
6050 ir_node *pred_store = mem_pred;
6051 ir_node *value = get_Store_value(pred_store);
6053 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
6055 if (get_irn_mode(value) != get_Load_mode(n))
6057 /* all combinations of aligned/unaligned pred/n should be fine so we do
6058 * not compare the unaligned attribute */
6060 ir_node *block = get_nodes_block(n);
6061 ir_node *jmp = new_r_Jmp(block);
6062 ir_graph *irg = get_irn_irg(n);
6063 ir_node *bad = new_r_Bad(irg, mode_X);
6064 ir_node *res = value;
6065 ir_node *in[] = { mem, res, jmp, bad };
6066 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
6075 * optimize a trampoline Call into a direct Call
6077 static ir_node *transform_node_Call(ir_node *call)
6079 ir_node *callee = get_Call_ptr(call);
6080 ir_node *adr, *mem, *res, *bl, **in;
6081 ir_type *ctp, *mtp, *tp;
6085 size_t i, n_res, n_param;
6088 if (! is_Proj(callee))
6090 callee = get_Proj_pred(callee);
6091 if (! is_Builtin(callee))
6093 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6096 mem = get_Call_mem(call);
6098 if (skip_Proj(mem) == callee) {
6099 /* memory is routed to the trampoline, skip */
6100 mem = get_Builtin_mem(callee);
6103 /* build a new call type */
6104 mtp = get_Call_type(call);
6105 tdb = get_type_dbg_info(mtp);
6107 n_res = get_method_n_ress(mtp);
6108 n_param = get_method_n_params(mtp);
6109 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6111 for (i = 0; i < n_res; ++i)
6112 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6114 NEW_ARR_A(ir_node *, in, n_param + 1);
6116 /* FIXME: we don't need a new pointer type in every step */
6117 irg = get_irn_irg(call);
6118 tp = get_irg_frame_type(irg);
6119 tp = new_type_pointer(tp);
6120 set_method_param_type(ctp, 0, tp);
6122 in[0] = get_Builtin_param(callee, 2);
6123 for (i = 0; i < n_param; ++i) {
6124 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6125 in[i + 1] = get_Call_param(call, i);
6127 var = get_method_variadicity(mtp);
6128 set_method_variadicity(ctp, var);
6129 /* When we resolve a trampoline, the function must be called by a this-call */
6130 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6131 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6133 adr = get_Builtin_param(callee, 1);
6135 db = get_irn_dbg_info(call);
6136 bl = get_nodes_block(call);
6138 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6139 if (get_irn_pinned(call) == op_pin_state_floats)
6140 set_irn_pinned(res, op_pin_state_floats);
6142 } /* transform_node_Call */
6145 * Tries several [inplace] [optimizing] transformations and returns an
6146 * equivalent node. The difference to equivalent_node() is that these
6147 * transformations _do_ generate new nodes, and thus the old node must
6148 * not be freed even if the equivalent node isn't the old one.
6150 static ir_node *transform_node(ir_node *n)
6155 * Transform_node is the only "optimizing transformation" that might
6156 * return a node with a different opcode. We iterate HERE until fixpoint
6157 * to get the final result.
6161 if (n->op->ops.transform_node != NULL)
6162 n = n->op->ops.transform_node(n);
6163 } while (oldn != n);
6166 } /* transform_node */
6169 * Sets the default transform node operation for an ir_op_ops.
6171 * @param code the opcode for the default operation
6172 * @param ops the operations initialized
6177 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6181 ops->transform_node = transform_node_##a; \
6183 #define CASE_PROJ(a) \
6185 ops->transform_node_Proj = transform_node_Proj_##a; \
6187 #define CASE_PROJ_EX(a) \
6189 ops->transform_node = transform_node_##a; \
6190 ops->transform_node_Proj = transform_node_Proj_##a; \
6231 } /* firm_set_default_transform_node */
6234 /* **************** Common Subexpression Elimination **************** */
6236 /** The size of the hash table used, should estimate the number of nodes
6238 #define N_IR_NODES 512
6240 /** Compares two exception attributes */
6241 static int node_cmp_exception(const ir_node *a, const ir_node *b)
6243 const except_attr *ea = &a->attr.except;
6244 const except_attr *eb = &b->attr.except;
6245 return ea->pin_state != eb->pin_state;
6248 /** Compares the attributes of two Const nodes. */
6249 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
6251 return get_Const_tarval(a) != get_Const_tarval(b);
6254 /** Compares the attributes of two Proj nodes. */
6255 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
6257 return a->attr.proj.proj != b->attr.proj.proj;
6260 /** Compares the attributes of two Alloc nodes. */
6261 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
6263 const alloc_attr *pa = &a->attr.alloc;
6264 const alloc_attr *pb = &b->attr.alloc;
6265 if (pa->where != pb->where || pa->type != pb->type)
6267 return node_cmp_exception(a, b);
6270 /** Compares the attributes of two Free nodes. */
6271 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
6273 const free_attr *pa = &a->attr.free;
6274 const free_attr *pb = &b->attr.free;
6275 return (pa->where != pb->where) || (pa->type != pb->type);
6278 /** Compares the attributes of two SymConst nodes. */
6279 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
6281 const symconst_attr *pa = &a->attr.symc;
6282 const symconst_attr *pb = &b->attr.symc;
6283 return (pa->kind != pb->kind)
6284 || (pa->sym.type_p != pb->sym.type_p);
6287 /** Compares the attributes of two Call nodes. */
6288 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
6290 const call_attr *pa = &a->attr.call;
6291 const call_attr *pb = &b->attr.call;
6292 if (pa->type != pb->type || pa->tail_call != pb->tail_call)
6294 return node_cmp_exception(a, b);
6297 /** Compares the attributes of two Sel nodes. */
6298 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
6300 const ir_entity *a_ent = get_Sel_entity(a);
6301 const ir_entity *b_ent = get_Sel_entity(b);
6302 return a_ent != b_ent;
6305 /** Compares the attributes of two Phi nodes. */
6306 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
6308 /* we can only enter this function if both nodes have the same number of inputs,
6309 hence it is enough to check if one of them is a Phi0 */
6311 /* check the Phi0 pos attribute */
6312 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6317 /** Compares the attributes of two Conv nodes. */
6318 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
6320 return get_Conv_strict(a) != get_Conv_strict(b);
6323 /** Compares the attributes of two Cast nodes. */
6324 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
6326 return get_Cast_type(a) != get_Cast_type(b);
6329 /** Compares the attributes of two Load nodes. */
6330 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
6332 if (get_Load_volatility(a) == volatility_is_volatile ||
6333 get_Load_volatility(b) == volatility_is_volatile)
6334 /* NEVER do CSE on volatile Loads */
6336 /* do not CSE Loads with different alignment. Be conservative. */
6337 if (get_Load_unaligned(a) != get_Load_unaligned(b))
6339 if (get_Load_mode(a) != get_Load_mode(b))
6341 return node_cmp_exception(a, b);
6344 /** Compares the attributes of two Store nodes. */
6345 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
6347 /* do not CSE Stores with different alignment. Be conservative. */
6348 if (get_Store_unaligned(a) != get_Store_unaligned(b))
6350 /* NEVER do CSE on volatile Stores */
6351 if (get_Store_volatility(a) == volatility_is_volatile ||
6352 get_Store_volatility(b) == volatility_is_volatile)
6354 return node_cmp_exception(a, b);
6357 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
6359 if (get_CopyB_type(a) != get_CopyB_type(b))
6362 return node_cmp_exception(a, b);
6365 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
6367 return node_cmp_exception(a, b);
6370 /** Compares the attributes of two Div nodes. */
6371 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
6373 const div_attr *ma = &a->attr.div;
6374 const div_attr *mb = &b->attr.div;
6375 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
6377 return node_cmp_exception(a, b);
6380 /** Compares the attributes of two Mod nodes. */
6381 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
6383 const mod_attr *ma = &a->attr.mod;
6384 const mod_attr *mb = &b->attr.mod;
6385 if (ma->resmode != mb->resmode)
6387 return node_cmp_exception(a, b);
6390 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
6392 const cmp_attr *ma = &a->attr.cmp;
6393 const cmp_attr *mb = &b->attr.cmp;
6394 return ma->relation != mb->relation;
6397 /** Compares the attributes of two Confirm nodes. */
6398 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
6400 const confirm_attr *ma = &a->attr.confirm;
6401 const confirm_attr *mb = &b->attr.confirm;
6402 return ma->relation != mb->relation;
6405 /** Compares the attributes of two Builtin nodes. */
6406 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6408 if (get_Builtin_kind(a) != get_Builtin_kind(b))
6410 if (get_Builtin_type(a) != get_Builtin_type(b))
6412 return node_cmp_exception(a, b);
6415 /** Compares the attributes of two ASM nodes. */
6416 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6419 const ir_asm_constraint *ca;
6420 const ir_asm_constraint *cb;
6423 if (get_ASM_text(a) != get_ASM_text(b))
6426 /* Should we really check the constraints here? Should be better, but is strange. */
6427 n = get_ASM_n_input_constraints(a);
6428 if (n != get_ASM_n_input_constraints(b))
6431 ca = get_ASM_input_constraints(a);
6432 cb = get_ASM_input_constraints(b);
6433 for (i = 0; i < n; ++i) {
6434 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6435 || ca[i].mode != cb[i].mode)
6439 n = get_ASM_n_output_constraints(a);
6440 if (n != get_ASM_n_output_constraints(b))
6443 ca = get_ASM_output_constraints(a);
6444 cb = get_ASM_output_constraints(b);
6445 for (i = 0; i < n; ++i) {
6446 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
6447 || ca[i].mode != cb[i].mode)
6451 n = get_ASM_n_clobbers(a);
6452 if (n != get_ASM_n_clobbers(b))
6455 cla = get_ASM_clobbers(a);
6456 clb = get_ASM_clobbers(b);
6457 for (i = 0; i < n; ++i) {
6458 if (cla[i] != clb[i])
6462 return node_cmp_exception(a, b);
6465 /** Compares the inexistent attributes of two Dummy nodes. */
6466 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6470 /* Dummy nodes never equal by definition */
6474 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
6476 if (get_InstOf_type(a) != get_InstOf_type(b))
6478 return node_cmp_exception(a, b);
6482 * Set the default node attribute compare operation for an ir_op_ops.
6484 * @param code the opcode for the default operation
6485 * @param ops the operations initialized
6490 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6494 ops->node_cmp_attr = node_cmp_attr_##a; \
6527 } /* firm_set_default_node_cmp_attr */
6530 * Compare function for two nodes in the value table. Gets two
6531 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6533 int identities_cmp(const void *elt, const void *key)
6535 ir_node *a = (ir_node *)elt;
6536 ir_node *b = (ir_node *)key;
6539 if (a == b) return 0;
6541 if ((get_irn_op(a) != get_irn_op(b)) ||
6542 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6544 /* compare if a's in and b's in are of equal length */
6545 irn_arity_a = get_irn_arity(a);
6546 if (irn_arity_a != get_irn_arity(b))
6549 /* blocks are never the same */
6553 if (get_irn_pinned(a) == op_pin_state_pinned) {
6554 /* for pinned nodes, the block inputs must be equal */
6555 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6558 ir_node *block_a = get_nodes_block(a);
6559 ir_node *block_b = get_nodes_block(b);
6560 if (! get_opt_global_cse()) {
6561 /* for block-local CSE both nodes must be in the same Block */
6562 if (block_a != block_b)
6565 /* The optimistic approach would be to do nothing here.
6566 * However doing GCSE optimistically produces a lot of partially dead code which appears
6567 * to be worse in practice than the missed opportunities.
6568 * So we use a very conservative variant here and only CSE if 1 value dominates the
6570 if (!block_dominates(block_a, block_b)
6571 && !block_dominates(block_b, block_a))
6576 /* compare a->in[0..ins] with b->in[0..ins] */
6577 for (i = 0; i < irn_arity_a; ++i) {
6578 ir_node *pred_a = get_irn_n(a, i);
6579 ir_node *pred_b = get_irn_n(b, i);
6580 if (pred_a != pred_b) {
6581 /* if both predecessors are CSE neutral they might be different */
6582 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6588 * here, we already now that the nodes are identical except their
6591 if (a->op->ops.node_cmp_attr)
6592 return a->op->ops.node_cmp_attr(a, b);
6595 } /* identities_cmp */
6598 * Calculate a hash value of a node.
6600 * @param node The IR-node
6602 unsigned ir_node_hash(const ir_node *node)
6604 return node->op->ops.hash(node);
6605 } /* ir_node_hash */
6608 void new_identities(ir_graph *irg)
6610 if (irg->value_table != NULL)
6611 del_pset(irg->value_table);
6612 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6613 } /* new_identities */
6615 void del_identities(ir_graph *irg)
6617 if (irg->value_table != NULL)
6618 del_pset(irg->value_table);
6619 } /* del_identities */
6621 /* Normalize a node by putting constants (and operands with larger
6622 * node index) on the right (operator side). */
6623 void ir_normalize_node(ir_node *n)
6625 if (is_op_commutative(get_irn_op(n))) {
6626 ir_node *l = get_binop_left(n);
6627 ir_node *r = get_binop_right(n);
6629 /* For commutative operators perform a OP b == b OP a but keep
6630 * constants on the RIGHT side. This helps greatly in some
6631 * optimizations. Moreover we use the idx number to make the form
6633 if (!operands_are_normalized(l, r)) {
6634 set_binop_left(n, r);
6635 set_binop_right(n, l);
6639 } /* ir_normalize_node */
6642 * Return the canonical node computing the same value as n.
6643 * Looks up the node in a hash table, enters it in the table
6644 * if it isn't there yet.
6646 * @param n the node to look up
6648 * @return a node that computes the same value as n or n if no such
6649 * node could be found
6651 ir_node *identify_remember(ir_node *n)
6653 ir_graph *irg = get_irn_irg(n);
6654 pset *value_table = irg->value_table;
6657 if (value_table == NULL)
6660 ir_normalize_node(n);
6661 /* lookup or insert in hash table with given hash key. */
6662 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6665 /* n is reachable again */
6666 edges_node_revival(nn);
6670 } /* identify_remember */
6673 * During construction we set the op_pin_state_pinned flag in the graph right
6674 * when the optimization is performed. The flag turning on procedure global
6675 * cse could be changed between two allocations. This way we are safe.
6677 * @param n The node to lookup
6679 static inline ir_node *identify_cons(ir_node *n)
6683 n = identify_remember(n);
6684 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6685 ir_graph *irg = get_irn_irg(n);
6686 set_irg_pinned(irg, op_pin_state_floats);
6689 } /* identify_cons */
6691 /* Add a node to the identities value table. */
6692 void add_identities(ir_node *node)
6699 identify_remember(node);
6702 /* Visit each node in the value table of a graph. */
6703 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6706 ir_graph *rem = current_ir_graph;
6708 current_ir_graph = irg;
6709 foreach_pset(irg->value_table, ir_node*, node) {
6712 current_ir_graph = rem;
6713 } /* visit_all_identities */
6716 * These optimizations deallocate nodes from the obstack.
6717 * It can only be called if it is guaranteed that no other nodes
6718 * reference this one, i.e., right after construction of a node.
6720 * @param n The node to optimize
6722 ir_node *optimize_node(ir_node *n)
6725 ir_graph *irg = get_irn_irg(n);
6726 unsigned iro = get_irn_opcode(n);
6729 /* Always optimize Phi nodes: part of the construction. */
6730 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6732 /* constant expression evaluation / constant folding */
6733 if (get_opt_constant_folding()) {
6734 /* neither constants nor Tuple values can be evaluated */
6735 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6736 /* try to evaluate */
6737 tv = computed_value(n);
6738 if (tv != tarval_bad) {
6743 * we MUST copy the node here temporarily, because it's still
6744 * needed for DBG_OPT_CSTEVAL
6746 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6747 oldn = (ir_node*)alloca(node_size);
6749 memcpy(oldn, n, node_size);
6750 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6752 /* ARG, copy the in array, we need it for statistics */
6753 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6755 /* note the inplace edges module */
6756 edges_node_deleted(n);
6758 /* evaluation was successful -- replace the node. */
6759 irg_kill_node(irg, n);
6760 nw = new_r_Const(irg, tv);
6762 DBG_OPT_CSTEVAL(oldn, nw);
6768 /* remove unnecessary nodes */
6769 if (get_opt_algebraic_simplification() ||
6770 (iro == iro_Phi) || /* always optimize these nodes. */
6772 (iro == iro_Proj) ||
6773 (iro == iro_Block) ) /* Flags tested local. */
6774 n = equivalent_node(n);
6776 /* Common Subexpression Elimination.
6778 * Checks whether n is already available.
6779 * The block input is used to distinguish different subexpressions. Right
6780 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6781 * subexpressions within a block.
6784 n = identify_cons(n);
6787 edges_node_deleted(oldn);
6789 /* We found an existing, better node, so we can deallocate the old node. */
6790 irg_kill_node(irg, oldn);
6794 /* Some more constant expression evaluation that does not allow to
6796 iro = get_irn_opcode(n);
6797 if (get_opt_algebraic_simplification() ||
6798 (iro == iro_Cond) ||
6799 (iro == iro_Proj)) /* Flags tested local. */
6800 n = transform_node(n);
6802 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6803 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6805 n = identify_remember(o);
6811 } /* optimize_node */
6815 * These optimizations never deallocate nodes (in place). This can cause dead
6816 * nodes lying on the obstack. Remove these by a dead node elimination,
6817 * i.e., a copying garbage collection.
6819 ir_node *optimize_in_place_2(ir_node *n)
6823 unsigned iro = get_irn_opcode(n);
6825 if (!get_opt_optimize() && !is_Phi(n)) return n;
6827 if (iro == iro_Deleted)
6830 /* constant expression evaluation / constant folding */
6831 if (get_opt_constant_folding()) {
6832 /* neither constants nor Tuple values can be evaluated */
6833 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6834 /* try to evaluate */
6835 tv = computed_value(n);
6836 if (tv != tarval_bad) {
6837 /* evaluation was successful -- replace the node. */
6838 ir_graph *irg = get_irn_irg(n);
6840 n = new_r_Const(irg, tv);
6842 DBG_OPT_CSTEVAL(oldn, n);
6848 /* remove unnecessary nodes */
6849 if (get_opt_constant_folding() ||
6850 (iro == iro_Phi) || /* always optimize these nodes. */
6851 (iro == iro_Id) || /* ... */
6852 (iro == iro_Proj) || /* ... */
6853 (iro == iro_Block) ) /* Flags tested local. */
6854 n = equivalent_node(n);
6856 /** common subexpression elimination **/
6857 /* Checks whether n is already available. */
6858 /* The block input is used to distinguish different subexpressions. Right
6859 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6860 subexpressions within a block. */
6861 if (get_opt_cse()) {
6863 n = identify_remember(o);
6868 /* Some more constant expression evaluation. */
6869 iro = get_irn_opcode(n);
6870 if (get_opt_constant_folding() ||
6871 (iro == iro_Cond) ||
6872 (iro == iro_Proj)) /* Flags tested local. */
6873 n = transform_node(n);
6875 /* Now we can verify the node, as it has no dead inputs any more. */
6878 /* Now we have a legal, useful node. Enter it in hash table for cse.
6879 Blocks should be unique anyways. (Except the successor of start:
6880 is cse with the start block!) */
6881 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6883 n = identify_remember(o);
6889 } /* optimize_in_place_2 */
6892 * Wrapper for external use, set proper status bits after optimization.
6894 ir_node *optimize_in_place(ir_node *n)
6896 ir_graph *irg = get_irn_irg(n);
6897 /* Handle graph state */
6898 assert(get_irg_phase_state(irg) != phase_building);
6900 if (get_opt_global_cse())
6901 set_irg_pinned(irg, op_pin_state_floats);
6903 /* FIXME: Maybe we could also test whether optimizing the node can
6904 change the control graph. */
6905 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
6906 return optimize_in_place_2(n);
6907 } /* optimize_in_place */
6910 * Calculate a hash value of a Const node.
6912 static unsigned hash_Const(const ir_node *node)
6916 /* special value for const, as they only differ in their tarval. */
6917 h = HASH_PTR(node->attr.con.tarval);
6923 * Calculate a hash value of a SymConst node.
6925 static unsigned hash_SymConst(const ir_node *node)
6929 /* all others are pointers */
6930 h = HASH_PTR(node->attr.symc.sym.type_p);
6933 } /* hash_SymConst */
6936 * Set the default hash operation in an ir_op_ops.
6938 * @param code the opcode for the default operation
6939 * @param ops the operations initialized
6944 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6948 ops->hash = hash_##a; \
6951 /* hash function already set */
6952 if (ops->hash != NULL)
6959 /* use input/mode default hash if no function was given */
6960 ops->hash = firm_default_hash;
6968 * Sets the default operation for an ir_ops.
6970 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6972 ops = firm_set_default_hash(code, ops);
6973 ops = firm_set_default_computed_value(code, ops);
6974 ops = firm_set_default_equivalent_node(code, ops);
6975 ops = firm_set_default_transform_node(code, ops);
6976 ops = firm_set_default_node_cmp_attr(code, ops);
6977 ops = firm_set_default_get_type_attr(code, ops);
6978 ops = firm_set_default_get_entity_attr(code, ops);
6981 } /* firm_set_default_operations */