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 } /* computed_value_Add */
138 * Return the value of a Sub.
139 * Special case: a - a
141 static ir_tarval *computed_value_Sub(const ir_node *n)
143 ir_mode *mode = get_irn_mode(n);
144 ir_node *a = get_Sub_left(n);
145 ir_node *b = get_Sub_right(n);
150 if (! mode_is_float(mode)) {
153 return get_mode_null(mode);
159 if ((ta != tarval_bad) && (tb != tarval_bad))
160 return tarval_sub(ta, tb, mode);
163 } /* computed_value_Sub */
166 * Return the value of a Carry.
167 * Special : a op 0, 0 op b
169 static ir_tarval *computed_value_Carry(const ir_node *n)
171 ir_node *a = get_binop_left(n);
172 ir_node *b = get_binop_right(n);
173 ir_mode *m = get_irn_mode(n);
174 ir_tarval *ta = value_of(a);
175 ir_tarval *tb = value_of(b);
177 if ((ta != tarval_bad) && (tb != tarval_bad)) {
179 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
181 if (tarval_is_null(ta) || tarval_is_null(tb))
182 return get_mode_null(m);
185 } /* computed_value_Carry */
188 * Return the value of a Borrow.
191 static ir_tarval *computed_value_Borrow(const ir_node *n)
193 ir_node *a = get_binop_left(n);
194 ir_node *b = get_binop_right(n);
195 ir_mode *m = get_irn_mode(n);
196 ir_tarval *ta = value_of(a);
197 ir_tarval *tb = value_of(b);
199 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
201 } else if (tarval_is_null(ta)) {
202 return get_mode_null(m);
205 } /* computed_value_Borrow */
208 * Return the value of an unary Minus.
210 static ir_tarval *computed_value_Minus(const ir_node *n)
212 ir_node *a = get_Minus_op(n);
213 ir_tarval *ta = value_of(a);
215 if (ta != tarval_bad)
216 return tarval_neg(ta);
219 } /* computed_value_Minus */
222 * Return the value of a Mul.
224 static ir_tarval *computed_value_Mul(const ir_node *n)
226 ir_node *a = get_Mul_left(n);
227 ir_node *b = get_Mul_right(n);
228 ir_tarval *ta = value_of(a);
229 ir_tarval *tb = value_of(b);
232 mode = get_irn_mode(n);
233 if (mode != get_irn_mode(a)) {
234 /* n * n = 2n bit multiplication */
235 ta = tarval_convert_to(ta, mode);
236 tb = tarval_convert_to(tb, mode);
239 if (ta != tarval_bad && tb != tarval_bad) {
240 return tarval_mul(ta, tb);
242 /* a * 0 != 0 if a == NaN or a == Inf */
243 if (!mode_is_float(mode)) {
244 /* a*0 = 0 or 0*b = 0 */
245 if (ta == get_mode_null(mode))
247 if (tb == get_mode_null(mode))
252 } /* computed_value_Mul */
255 * Return the value of an And.
256 * Special case: a & 0, 0 & b
258 static ir_tarval *computed_value_And(const ir_node *n)
260 ir_node *a = get_And_left(n);
261 ir_node *b = get_And_right(n);
262 ir_tarval *ta = value_of(a);
263 ir_tarval *tb = value_of(b);
265 if ((ta != tarval_bad) && (tb != tarval_bad)) {
266 return tarval_and (ta, tb);
268 if (tarval_is_null(ta)) return ta;
269 if (tarval_is_null(tb)) return tb;
272 } /* computed_value_And */
275 * Return the value of an Or.
276 * Special case: a | 1...1, 1...1 | b
278 static ir_tarval *computed_value_Or(const ir_node *n)
280 ir_node *a = get_Or_left(n);
281 ir_node *b = get_Or_right(n);
282 ir_tarval *ta = value_of(a);
283 ir_tarval *tb = value_of(b);
285 if ((ta != tarval_bad) && (tb != tarval_bad)) {
286 return tarval_or (ta, tb);
288 if (tarval_is_all_one(ta)) return ta;
289 if (tarval_is_all_one(tb)) return tb;
292 } /* computed_value_Or */
295 * Return the value of an Eor.
297 static ir_tarval *computed_value_Eor(const ir_node *n)
299 ir_node *a = get_Eor_left(n);
300 ir_node *b = get_Eor_right(n);
305 return get_mode_null(get_irn_mode(n));
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_eor(ta, tb);
314 } /* computed_value_Eor */
317 * Return the value of a Not.
319 static ir_tarval *computed_value_Not(const ir_node *n)
321 ir_node *a = get_Not_op(n);
322 ir_tarval *ta = value_of(a);
324 if (ta != tarval_bad)
325 return tarval_not(ta);
328 } /* computed_value_Not */
331 * Return the value of a Shl.
333 static ir_tarval *computed_value_Shl(const ir_node *n)
335 ir_node *a = get_Shl_left(n);
336 ir_node *b = get_Shl_right(n);
338 ir_tarval *ta = value_of(a);
339 ir_tarval *tb = value_of(b);
341 if ((ta != tarval_bad) && (tb != tarval_bad)) {
342 return tarval_shl(ta, tb);
345 } /* computed_value_Shl */
348 * Return the value of a Shr.
350 static ir_tarval *computed_value_Shr(const ir_node *n)
352 ir_node *a = get_Shr_left(n);
353 ir_node *b = get_Shr_right(n);
355 ir_tarval *ta = value_of(a);
356 ir_tarval *tb = value_of(b);
358 if ((ta != tarval_bad) && (tb != tarval_bad)) {
359 return tarval_shr(ta, tb);
362 } /* computed_value_Shr */
365 * Return the value of a Shrs.
367 static ir_tarval *computed_value_Shrs(const ir_node *n)
369 ir_node *a = get_Shrs_left(n);
370 ir_node *b = get_Shrs_right(n);
372 ir_tarval *ta = value_of(a);
373 ir_tarval *tb = value_of(b);
375 if ((ta != tarval_bad) && (tb != tarval_bad)) {
376 return tarval_shrs(ta, tb);
379 } /* computed_value_Shrs */
382 * Return the value of a Rotl.
384 static ir_tarval *computed_value_Rotl(const ir_node *n)
386 ir_node *a = get_Rotl_left(n);
387 ir_node *b = get_Rotl_right(n);
389 ir_tarval *ta = value_of(a);
390 ir_tarval *tb = value_of(b);
392 if ((ta != tarval_bad) && (tb != tarval_bad)) {
393 return tarval_rotl(ta, tb);
396 } /* computed_value_Rotl */
399 * Return the value of a Conv.
401 static ir_tarval *computed_value_Conv(const ir_node *n)
403 ir_node *a = get_Conv_op(n);
404 ir_tarval *ta = value_of(a);
406 if (ta != tarval_bad)
407 return tarval_convert_to(ta, get_irn_mode(n));
410 } /* computed_value_Conv */
413 * Calculate the value of a Mux: can be evaluated, if the
414 * sel and the right input are known.
416 static ir_tarval *computed_value_Mux(const ir_node *n)
418 ir_node *sel = get_Mux_sel(n);
419 ir_tarval *ts = value_of(sel);
421 if (ts == get_tarval_b_true()) {
422 ir_node *v = get_Mux_true(n);
425 else if (ts == get_tarval_b_false()) {
426 ir_node *v = get_Mux_false(n);
430 } /* computed_value_Mux */
433 * Calculate the value of a Confirm: can be evaluated,
434 * if it has the form Confirm(x, '=', Const).
436 static ir_tarval *computed_value_Confirm(const ir_node *n)
438 if (get_Confirm_relation(n) == ir_relation_equal) {
439 ir_tarval *tv = value_of(get_Confirm_bound(n));
440 if (tv != tarval_bad)
443 return value_of(get_Confirm_value(n));
444 } /* computed_value_Confirm */
447 * gives a (conservative) estimation of possible relation when comparing
450 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
451 const ir_node *right)
453 ir_relation possible = ir_relation_true;
454 ir_tarval *tv_l = value_of(left);
455 ir_tarval *tv_r = value_of(right);
456 ir_mode *mode = get_irn_mode(left);
457 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
458 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
460 /* both values known - evaluate them */
461 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
462 possible = tarval_cmp(tv_l, tv_r);
463 /* we can return now, won't get any better */
466 /* a == a is never less or greater (but might be equal or unordered) */
468 possible &= ~ir_relation_less_greater;
469 /* unordered results only happen for float compares */
470 if (!mode_is_float(mode))
471 possible &= ~ir_relation_unordered;
472 /* values can never be less than the least representable number or
473 * greater than the greatest representable number */
475 possible &= ~ir_relation_greater;
477 possible &= ~ir_relation_less;
479 possible &= ~ir_relation_greater;
481 possible &= ~ir_relation_less;
482 /* maybe vrp can tell us more */
483 possible &= vrp_cmp(left, right);
484 /* Alloc nodes never return null (but throw an exception) */
485 if (is_Alloc(left) && tarval_is_null(tv_r))
486 possible &= ~ir_relation_equal;
492 * Return the value of a Cmp.
494 * The basic idea here is to determine which relations are possible and which
495 * one are definitely impossible.
497 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
499 ir_node *left = get_Cmp_left(cmp);
500 ir_node *right = get_Cmp_right(cmp);
501 ir_relation possible = ir_get_possible_cmp_relations(left, right);
502 ir_relation relation = get_Cmp_relation(cmp);
504 /* if none of the requested relations is possible, return false */
505 if ((possible & relation) == ir_relation_false)
506 return tarval_b_false;
507 /* if possible relations are a subset of the requested ones return true */
508 if ((possible & ~relation) == ir_relation_false)
509 return tarval_b_true;
511 return computed_value_Cmp_Confirm(cmp, left, right, relation);
515 * Calculate the value of an integer Div.
516 * Special case: 0 / b
518 static ir_tarval *do_computed_value_Div(const ir_node *div)
520 const ir_node *a = get_Div_left(div);
521 const ir_node *b = get_Div_right(div);
522 const ir_mode *mode = get_Div_resmode(div);
523 ir_tarval *ta = value_of(a);
525 const ir_node *dummy;
527 /* cannot optimize 0 / b = 0 because of NaN */
528 if (!mode_is_float(mode)) {
529 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
530 return ta; /* 0 / b == 0 if b != 0 */
533 if (ta != tarval_bad && tb != tarval_bad)
534 return tarval_div(ta, tb);
536 } /* do_computed_value_Div */
539 * Calculate the value of an integer Mod of two nodes.
540 * Special case: a % 1
542 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
544 ir_tarval *ta = value_of(a);
545 ir_tarval *tb = value_of(b);
547 /* Compute a % 1 or c1 % c2 */
548 if (tarval_is_one(tb))
549 return get_mode_null(get_irn_mode(a));
550 if (ta != tarval_bad && tb != tarval_bad)
551 return tarval_mod(ta, tb);
553 } /* do_computed_value_Mod */
556 * Return the value of a Proj(Div).
558 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
560 long proj_nr = get_Proj_proj(n);
561 if (proj_nr != pn_Div_res)
564 return do_computed_value_Div(get_Proj_pred(n));
565 } /* computed_value_Proj_Div */
568 * Return the value of a Proj(Mod).
570 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
572 long proj_nr = get_Proj_proj(n);
574 if (proj_nr == pn_Mod_res) {
575 const ir_node *mod = get_Proj_pred(n);
576 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
579 } /* computed_value_Proj_Mod */
582 * Return the value of a Proj.
584 static ir_tarval *computed_value_Proj(const ir_node *proj)
586 ir_node *n = get_Proj_pred(proj);
588 if (n->op->ops.computed_value_Proj != NULL)
589 return n->op->ops.computed_value_Proj(proj);
591 } /* computed_value_Proj */
594 * If the parameter n can be computed, return its value, else tarval_bad.
595 * Performs constant folding.
597 * @param n The node this should be evaluated
599 ir_tarval *computed_value(const ir_node *n)
601 vrp_attr *vrp = vrp_get_info(n);
602 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
603 return vrp->bits_set;
605 if (n->op->ops.computed_value)
606 return n->op->ops.computed_value(n);
608 } /* computed_value */
611 * Set the default computed_value evaluator in an ir_op_ops.
613 * @param code the opcode for the default operation
614 * @param ops the operations initialized
619 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
623 ops->computed_value = computed_value_##a; \
625 #define CASE_PROJ(a) \
627 ops->computed_value_Proj = computed_value_Proj_##a; \
662 } /* firm_set_default_computed_value */
665 * Optimize operations that are commutative and have neutral 0,
666 * so a op 0 = 0 op a = a.
668 static ir_node *equivalent_node_neutral_zero(ir_node *n)
672 ir_node *a = get_binop_left(n);
673 ir_node *b = get_binop_right(n);
678 /* After running compute_node there is only one constant predecessor.
679 Find this predecessors value and remember the other node: */
680 if ((tv = value_of(a)) != tarval_bad) {
682 } else if ((tv = value_of(b)) != tarval_bad) {
687 /* If this predecessors constant value is zero, the operation is
688 * unnecessary. Remove it.
690 * Beware: If n is a Add, the mode of on and n might be different
691 * which happens in this rare construction: NULL + 3.
692 * Then, a Conv would be needed which we cannot include here.
694 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
697 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
701 } /* equivalent_node_neutral_zero */
704 * Eor is commutative and has neutral 0.
706 static ir_node *equivalent_node_Eor(ir_node *n)
712 n = equivalent_node_neutral_zero(n);
713 if (n != oldn) return n;
716 b = get_Eor_right(n);
719 ir_node *aa = get_Eor_left(a);
720 ir_node *ab = get_Eor_right(a);
723 /* (a ^ b) ^ a -> b */
725 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
727 } else if (ab == b) {
728 /* (a ^ b) ^ b -> a */
730 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
735 ir_node *ba = get_Eor_left(b);
736 ir_node *bb = get_Eor_right(b);
739 /* a ^ (a ^ b) -> b */
741 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
743 } else if (bb == a) {
744 /* a ^ (b ^ a) -> b */
746 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
754 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
756 * The second one looks strange, but this construct
757 * is used heavily in the LCC sources :-).
759 * Beware: The Mode of an Add may be different than the mode of its
760 * predecessors, so we could not return a predecessors in all cases.
762 static ir_node *equivalent_node_Add(ir_node *n)
765 ir_node *left, *right;
766 ir_mode *mode = get_irn_mode(n);
768 n = equivalent_node_neutral_zero(n);
772 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
773 if (mode_is_float(mode)) {
774 ir_graph *irg = get_irn_irg(n);
775 if (get_irg_fp_model(irg) & fp_strict_algebraic)
779 left = get_Add_left(n);
780 right = get_Add_right(n);
783 if (get_Sub_right(left) == right) {
786 n = get_Sub_left(left);
787 if (mode == get_irn_mode(n)) {
788 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
794 if (get_Sub_right(right) == left) {
797 n = get_Sub_left(right);
798 if (mode == get_irn_mode(n)) {
799 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
805 } /* equivalent_node_Add */
808 * optimize operations that are not commutative but have neutral 0 on left,
811 static ir_node *equivalent_node_left_zero(ir_node *n)
815 ir_node *a = get_binop_left(n);
816 ir_node *b = get_binop_right(n);
817 ir_tarval *tb = value_of(b);
819 if (tarval_is_null(tb)) {
822 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
825 } /* equivalent_node_left_zero */
827 #define equivalent_node_Shl equivalent_node_left_zero
828 #define equivalent_node_Shr equivalent_node_left_zero
829 #define equivalent_node_Shrs equivalent_node_left_zero
830 #define equivalent_node_Rotl equivalent_node_left_zero
833 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
835 * The second one looks strange, but this construct
836 * is used heavily in the LCC sources :-).
838 * Beware: The Mode of a Sub may be different than the mode of its
839 * predecessors, so we could not return a predecessors in all cases.
841 static ir_node *equivalent_node_Sub(ir_node *n)
845 ir_mode *mode = get_irn_mode(n);
848 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
849 if (mode_is_float(mode)) {
850 ir_graph *irg = get_irn_irg(n);
851 if (get_irg_fp_model(irg) & fp_strict_algebraic)
855 b = get_Sub_right(n);
858 /* Beware: modes might be different */
859 if (tarval_is_null(tb)) {
860 ir_node *a = get_Sub_left(n);
861 if (mode == get_irn_mode(a)) {
864 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
868 } /* equivalent_node_Sub */
872 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
875 * -(-a) == a, but might overflow two times.
876 * We handle it anyway here but the better way would be a
877 * flag. This would be needed for Pascal for instance.
879 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
882 ir_node *pred = get_unop_op(n);
884 /* optimize symmetric unop */
885 if (get_irn_op(pred) == get_irn_op(n)) {
886 n = get_unop_op(pred);
887 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
890 } /* equivalent_node_idempotent_unop */
892 /** Optimize Not(Not(x)) == x. */
893 #define equivalent_node_Not equivalent_node_idempotent_unop
895 /** -(-x) == x ??? Is this possible or can --x raise an
896 out of bounds exception if min =! max? */
897 #define equivalent_node_Minus equivalent_node_idempotent_unop
900 * Optimize a * 1 = 1 * a = a.
902 static ir_node *equivalent_node_Mul(ir_node *n)
905 ir_node *a = get_Mul_left(n);
907 /* we can handle here only the n * n = n bit cases */
908 if (get_irn_mode(n) == get_irn_mode(a)) {
909 ir_node *b = get_Mul_right(n);
913 * Mul is commutative and has again an other neutral element.
914 * Constants are place right, so check this case first.
917 if (tarval_is_one(tv)) {
919 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
922 if (tarval_is_one(tv)) {
924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
929 } /* equivalent_node_Mul */
932 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
934 static ir_node *equivalent_node_Or(ir_node *n)
938 ir_node *a = get_Or_left(n);
939 ir_node *b = get_Or_right(n);
943 n = a; /* idempotence */
944 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
947 /* constants are normalized to right, check this side first */
949 if (tarval_is_null(tv)) {
951 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
955 if (tarval_is_null(tv)) {
957 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
962 } /* equivalent_node_Or */
965 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
967 static ir_node *equivalent_node_And(ir_node *n)
971 ir_node *a = get_And_left(n);
972 ir_node *b = get_And_right(n);
976 n = a; /* idempotence */
977 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
980 /* constants are normalized to right, check this side first */
982 if (tarval_is_all_one(tv)) {
984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
987 if (tv != get_tarval_bad()) {
988 ir_mode *mode = get_irn_mode(n);
989 if (!mode_is_signed(mode) && is_Conv(a)) {
990 ir_node *convop = get_Conv_op(a);
991 ir_mode *convopmode = get_irn_mode(convop);
992 if (!mode_is_signed(convopmode)) {
993 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
994 /* Conv(X) & all_one(mode(X)) = Conv(X) */
996 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1003 if (tarval_is_all_one(tv)) {
1005 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1009 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1012 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1017 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1020 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1025 } /* equivalent_node_And */
1028 * Try to remove useless Conv's:
1030 static ir_node *equivalent_node_Conv(ir_node *n)
1033 ir_node *a = get_Conv_op(n);
1035 ir_mode *n_mode = get_irn_mode(n);
1036 ir_mode *a_mode = get_irn_mode(a);
1039 if (n_mode == a_mode) { /* No Conv necessary */
1040 if (get_Conv_strict(n)) {
1043 /* neither Minus nor Confirm change the precision,
1044 so we can "look-through" */
1047 p = get_Minus_op(p);
1048 } else if (is_Confirm(p)) {
1049 p = get_Confirm_value(p);
1055 if (is_Conv(p) && get_Conv_strict(p)) {
1056 /* we known already, that a_mode == n_mode, and neither
1057 Minus change the mode, so the second Conv
1059 assert(get_irn_mode(p) == n_mode);
1061 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1065 ir_node *pred = get_Proj_pred(p);
1066 if (is_Load(pred)) {
1067 /* Loads always return with the exact precision of n_mode */
1068 assert(get_Load_mode(pred) == n_mode);
1070 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1073 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1074 pred = get_Proj_pred(pred);
1075 if (is_Start(pred)) {
1076 /* Arguments always return with the exact precision,
1077 as strictConv's are place before Call -- if the
1078 caller was compiled with the same setting.
1079 Otherwise, the semantics is probably still right. */
1080 assert(get_irn_mode(p) == n_mode);
1082 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1088 /* special case: the immediate predecessor is also a Conv */
1089 if (! get_Conv_strict(a)) {
1090 /* first one is not strict, kick it */
1092 a_mode = get_irn_mode(a);
1096 /* else both are strict conv, second is superfluous */
1098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1103 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1106 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1107 ir_node *b = get_Conv_op(a);
1108 ir_mode *b_mode = get_irn_mode(b);
1110 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1111 /* both are strict conv */
1112 if (smaller_mode(a_mode, n_mode)) {
1113 /* both are strict, but the first is smaller, so
1114 the second cannot remove more precision, remove the
1116 set_Conv_strict(n, 0);
1119 if (n_mode == b_mode) {
1120 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1121 if (n_mode == mode_b) {
1122 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1123 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1125 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1126 if (values_in_mode(b_mode, a_mode)) {
1127 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1133 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1134 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1135 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1136 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1138 if (float_mantissa >= int_mantissa) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1145 if (smaller_mode(b_mode, a_mode)) {
1146 if (get_Conv_strict(n))
1147 set_Conv_strict(b, 1);
1148 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1156 } /* equivalent_node_Conv */
1159 * - fold Phi-nodes, iff they have only one predecessor except
1162 static ir_node *equivalent_node_Phi(ir_node *n)
1168 ir_node *first_val = NULL; /* to shutup gcc */
1170 if (!get_opt_optimize() &&
1171 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1174 n_preds = get_Phi_n_preds(n);
1176 block = get_nodes_block(n);
1178 /* Phi of dead Region without predecessors. */
1182 /* Find first non-self-referencing input */
1183 for (i = 0; i < n_preds; ++i) {
1184 first_val = get_Phi_pred(n, i);
1185 /* not self pointer */
1186 if (first_val != n) {
1187 /* then found first value. */
1192 /* search for rest of inputs, determine if any of these
1193 are non-self-referencing */
1194 while (++i < n_preds) {
1195 ir_node *scnd_val = get_Phi_pred(n, i);
1196 if (scnd_val != n && scnd_val != first_val) {
1201 if (i >= n_preds && !is_Dummy(first_val)) {
1202 /* Fold, if no multiple distinct non-self-referencing inputs */
1204 DBG_OPT_PHI(oldn, n);
1207 } /* equivalent_node_Phi */
1210 * Optimize Proj(Tuple).
1212 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1214 ir_node *oldn = proj;
1215 ir_node *tuple = get_Proj_pred(proj);
1217 /* Remove the Tuple/Proj combination. */
1218 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1219 DBG_OPT_TUPLE(oldn, tuple, proj);
1222 } /* equivalent_node_Proj_Tuple */
1225 * Optimize a / 1 = a.
1227 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1229 ir_node *oldn = proj;
1230 ir_node *div = get_Proj_pred(proj);
1231 ir_node *b = get_Div_right(div);
1232 ir_tarval *tb = value_of(b);
1234 /* Div is not commutative. */
1235 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1236 switch (get_Proj_proj(proj)) {
1238 proj = get_Div_mem(div);
1239 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1243 proj = get_Div_left(div);
1244 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1248 /* we cannot replace the exception Proj's here, this is done in
1249 transform_node_Proj_Div() */
1254 } /* equivalent_node_Proj_Div */
1257 * Optimize CopyB(mem, x, x) into a Nop.
1259 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1261 ir_node *oldn = proj;
1262 ir_node *copyb = get_Proj_pred(proj);
1263 ir_node *a = get_CopyB_dst(copyb);
1264 ir_node *b = get_CopyB_src(copyb);
1267 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1268 switch (get_Proj_proj(proj)) {
1270 proj = get_CopyB_mem(copyb);
1271 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1276 } /* equivalent_node_Proj_CopyB */
1279 * Optimize Bounds(idx, idx, upper) into idx.
1281 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1283 ir_node *oldn = proj;
1284 ir_node *bound = get_Proj_pred(proj);
1285 ir_node *idx = get_Bound_index(bound);
1286 ir_node *pred = skip_Proj(idx);
1289 if (idx == get_Bound_lower(bound))
1291 else if (is_Bound(pred)) {
1293 * idx was Bounds checked previously, it is still valid if
1294 * lower <= pred_lower && pred_upper <= upper.
1296 ir_node *lower = get_Bound_lower(bound);
1297 ir_node *upper = get_Bound_upper(bound);
1298 if (get_Bound_lower(pred) == lower &&
1299 get_Bound_upper(pred) == upper) {
1301 * One could expect that we simply return the previous
1302 * Bound here. However, this would be wrong, as we could
1303 * add an exception Proj to a new location then.
1304 * So, we must turn in into a tuple.
1310 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1311 switch (get_Proj_proj(proj)) {
1313 DBG_OPT_EXC_REM(proj);
1314 proj = get_Bound_mem(bound);
1318 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1321 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1326 } /* equivalent_node_Proj_Bound */
1329 * Does all optimizations on nodes that must be done on its Projs
1330 * because of creating new nodes.
1332 static ir_node *equivalent_node_Proj(ir_node *proj)
1334 ir_node *n = get_Proj_pred(proj);
1335 if (n->op->ops.equivalent_node_Proj)
1336 return n->op->ops.equivalent_node_Proj(proj);
1338 } /* equivalent_node_Proj */
1343 static ir_node *equivalent_node_Id(ir_node *n)
1351 DBG_OPT_ID(oldn, n);
1353 } /* equivalent_node_Id */
1358 static ir_node *equivalent_node_Mux(ir_node *n)
1360 ir_node *oldn = n, *sel = get_Mux_sel(n);
1362 ir_tarval *ts = value_of(sel);
1364 /* Mux(true, f, t) == t */
1365 if (ts == tarval_b_true) {
1366 n = get_Mux_true(n);
1367 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1370 /* Mux(false, f, t) == f */
1371 if (ts == tarval_b_false) {
1372 n = get_Mux_false(n);
1373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1376 n_t = get_Mux_true(n);
1377 n_f = get_Mux_false(n);
1379 /* Mux(v, x, T) == x */
1380 if (is_Unknown(n_f)) {
1382 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1385 /* Mux(v, T, x) == x */
1386 if (is_Unknown(n_t)) {
1388 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1392 /* Mux(v, x, x) == x */
1395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1398 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1399 ir_relation relation = get_Cmp_relation(sel);
1400 ir_node *f = get_Mux_false(n);
1401 ir_node *t = get_Mux_true(n);
1404 * Note further that these optimization work even for floating point
1405 * with NaN's because -NaN == NaN.
1406 * However, if +0 and -0 is handled differently, we cannot use the first one.
1408 ir_node *const cmp_l = get_Cmp_left(sel);
1409 ir_node *const cmp_r = get_Cmp_right(sel);
1412 case ir_relation_equal:
1413 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1414 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1416 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1421 case ir_relation_less_greater:
1422 case ir_relation_unordered_less_greater:
1423 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1424 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1435 * Note: normalization puts the constant on the right side,
1436 * so we check only one case.
1438 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1439 /* Mux(t CMP 0, X, t) */
1440 if (is_Minus(f) && get_Minus_op(f) == t) {
1441 /* Mux(t CMP 0, -t, t) */
1442 if (relation == ir_relation_equal) {
1443 /* Mux(t == 0, -t, t) ==> -t */
1445 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1446 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1447 /* Mux(t != 0, -t, t) ==> t */
1449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1459 * Remove Confirm nodes if setting is on.
1460 * Replace Confirms(x, '=', Constlike) by Constlike.
1462 static ir_node *equivalent_node_Confirm(ir_node *n)
1464 ir_node *pred = get_Confirm_value(n);
1465 ir_relation relation = get_Confirm_relation(n);
1467 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1469 * rare case: two identical Confirms one after another,
1470 * replace the second one with the first.
1473 pred = get_Confirm_value(n);
1479 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1480 * perform no actual computation, as, e.g., the Id nodes. It does not create
1481 * new nodes. It is therefore safe to free n if the node returned is not n.
1482 * If a node returns a Tuple we can not just skip it. If the size of the
1483 * in array fits, we transform n into a tuple (e.g., Div).
1485 ir_node *equivalent_node(ir_node *n)
1487 if (n->op->ops.equivalent_node)
1488 return n->op->ops.equivalent_node(n);
1490 } /* equivalent_node */
1493 * Sets the default equivalent node operation for an ir_op_ops.
1495 * @param code the opcode for the default operation
1496 * @param ops the operations initialized
1501 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1505 ops->equivalent_node = equivalent_node_##a; \
1507 #define CASE_PROJ(a) \
1509 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1543 } /* firm_set_default_equivalent_node */
1546 * Returns non-zero if a node is a Phi node
1547 * with all predecessors constant.
1549 static int is_const_Phi(ir_node *n)
1553 if (! is_Phi(n) || get_irn_arity(n) == 0)
1555 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1556 if (! is_Const(get_irn_n(n, i)))
1560 } /* is_const_Phi */
1562 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1563 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1566 * in reality eval_func should be tarval (*eval_func)() but incomplete
1567 * declarations are bad style and generate noisy warnings
1569 typedef void (*eval_func)(void);
1572 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1574 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1576 if (eval == (eval_func) tarval_sub) {
1577 tarval_sub_type func = (tarval_sub_type)eval;
1579 return func(a, b, mode);
1581 tarval_binop_type func = (tarval_binop_type)eval;
1588 * Apply an evaluator on a binop with a constant operators (and one Phi).
1590 * @param phi the Phi node
1591 * @param other the other operand
1592 * @param eval an evaluator function
1593 * @param mode the mode of the result, may be different from the mode of the Phi!
1594 * @param left if non-zero, other is the left operand, else the right
1596 * @return a new Phi node if the conversion was successful, NULL else
1598 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1604 int i, n = get_irn_arity(phi);
1606 NEW_ARR_A(void *, res, n);
1608 for (i = 0; i < n; ++i) {
1609 pred = get_irn_n(phi, i);
1610 tv = get_Const_tarval(pred);
1611 tv = do_eval(eval, other, tv, mode);
1613 if (tv == tarval_bad) {
1614 /* folding failed, bad */
1620 for (i = 0; i < n; ++i) {
1621 pred = get_irn_n(phi, i);
1622 tv = get_Const_tarval(pred);
1623 tv = do_eval(eval, tv, other, mode);
1625 if (tv == tarval_bad) {
1626 /* folding failed, bad */
1632 irg = get_irn_irg(phi);
1633 for (i = 0; i < n; ++i) {
1634 pred = get_irn_n(phi, i);
1635 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1637 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1638 } /* apply_binop_on_phi */
1641 * Apply an evaluator on a binop with two constant Phi.
1643 * @param a the left Phi node
1644 * @param b the right Phi node
1645 * @param eval an evaluator function
1646 * @param mode the mode of the result, may be different from the mode of the Phi!
1648 * @return a new Phi node if the conversion was successful, NULL else
1650 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1652 ir_tarval *tv_l, *tv_r, *tv;
1658 if (get_nodes_block(a) != get_nodes_block(b))
1661 n = get_irn_arity(a);
1662 NEW_ARR_A(void *, res, n);
1664 for (i = 0; i < n; ++i) {
1665 pred = get_irn_n(a, i);
1666 tv_l = get_Const_tarval(pred);
1667 pred = get_irn_n(b, i);
1668 tv_r = get_Const_tarval(pred);
1669 tv = do_eval(eval, tv_l, tv_r, mode);
1671 if (tv == tarval_bad) {
1672 /* folding failed, bad */
1677 irg = get_irn_irg(a);
1678 for (i = 0; i < n; ++i) {
1679 pred = get_irn_n(a, i);
1680 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1682 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1683 } /* apply_binop_on_2_phis */
1686 * Apply an evaluator on a unop with a constant operator (a Phi).
1688 * @param phi the Phi node
1689 * @param eval an evaluator function
1691 * @return a new Phi node if the conversion was successful, NULL else
1693 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1700 int i, n = get_irn_arity(phi);
1702 NEW_ARR_A(void *, res, n);
1703 for (i = 0; i < n; ++i) {
1704 pred = get_irn_n(phi, i);
1705 tv = get_Const_tarval(pred);
1708 if (tv == tarval_bad) {
1709 /* folding failed, bad */
1714 mode = get_irn_mode(phi);
1715 irg = get_irn_irg(phi);
1716 for (i = 0; i < n; ++i) {
1717 pred = get_irn_n(phi, i);
1718 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1720 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1721 } /* apply_unop_on_phi */
1724 * Apply a conversion on a constant operator (a Phi).
1726 * @param phi the Phi node
1728 * @return a new Phi node if the conversion was successful, NULL else
1730 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1736 int i, n = get_irn_arity(phi);
1738 NEW_ARR_A(void *, res, n);
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 tv = get_Const_tarval(pred);
1742 tv = tarval_convert_to(tv, mode);
1744 if (tv == tarval_bad) {
1745 /* folding failed, bad */
1750 irg = get_irn_irg(phi);
1751 for (i = 0; i < n; ++i) {
1752 pred = get_irn_n(phi, i);
1753 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1755 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1756 } /* apply_conv_on_phi */
1759 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1760 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1761 * If possible, remove the Conv's.
1763 static ir_node *transform_node_AddSub(ir_node *n)
1765 ir_mode *mode = get_irn_mode(n);
1767 if (mode_is_reference(mode)) {
1768 ir_node *left = get_binop_left(n);
1769 ir_node *right = get_binop_right(n);
1770 unsigned ref_bits = get_mode_size_bits(mode);
1772 if (is_Conv(left)) {
1773 ir_mode *lmode = get_irn_mode(left);
1774 unsigned bits = get_mode_size_bits(lmode);
1776 if (ref_bits == bits &&
1777 mode_is_int(lmode) &&
1778 get_mode_arithmetic(lmode) == irma_twos_complement) {
1779 ir_node *pre = get_Conv_op(left);
1780 ir_mode *pre_mode = get_irn_mode(pre);
1782 if (mode_is_int(pre_mode) &&
1783 get_mode_size_bits(pre_mode) == bits &&
1784 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1785 /* ok, this conv just changes to sign, moreover the calculation
1786 * is done with same number of bits as our address mode, so
1787 * we can ignore the conv as address calculation can be viewed
1788 * as either signed or unsigned
1790 set_binop_left(n, pre);
1795 if (is_Conv(right)) {
1796 ir_mode *rmode = get_irn_mode(right);
1797 unsigned bits = get_mode_size_bits(rmode);
1799 if (ref_bits == bits &&
1800 mode_is_int(rmode) &&
1801 get_mode_arithmetic(rmode) == irma_twos_complement) {
1802 ir_node *pre = get_Conv_op(right);
1803 ir_mode *pre_mode = get_irn_mode(pre);
1805 if (mode_is_int(pre_mode) &&
1806 get_mode_size_bits(pre_mode) == bits &&
1807 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1808 /* ok, this conv just changes to sign, moreover the calculation
1809 * is done with same number of bits as our address mode, so
1810 * we can ignore the conv as address calculation can be viewed
1811 * as either signed or unsigned
1813 set_binop_right(n, pre);
1818 /* let address arithmetic use unsigned modes */
1819 if (is_Const(right)) {
1820 ir_mode *rmode = get_irn_mode(right);
1822 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1823 /* convert a AddP(P, *s) into AddP(P, *u) */
1824 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1826 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1827 set_binop_right(n, pre);
1833 } /* transform_node_AddSub */
1835 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1838 if (is_Const(b) && is_const_Phi(a)) { \
1839 /* check for Op(Phi, Const) */ \
1840 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1842 else if (is_Const(a) && is_const_Phi(b)) { \
1843 /* check for Op(Const, Phi) */ \
1844 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1846 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1847 /* check for Op(Phi, Phi) */ \
1848 c = apply_binop_on_2_phis(a, b, eval, mode); \
1851 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1856 #define HANDLE_UNOP_PHI(eval, a, c) \
1859 if (is_const_Phi(a)) { \
1860 /* check for Op(Phi) */ \
1861 c = apply_unop_on_phi(a, eval); \
1863 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1870 * Do the AddSub optimization, then Transform
1871 * Constant folding on Phi
1872 * Add(a,a) -> Mul(a, 2)
1873 * Add(Mul(a, x), a) -> Mul(a, x+1)
1874 * if the mode is integer or float.
1875 * Transform Add(a,-b) into Sub(a,b).
1876 * Reassociation might fold this further.
1878 static ir_node *transform_node_Add(ir_node *n)
1881 ir_node *a, *b, *c, *oldn = n;
1882 vrp_attr *a_vrp, *b_vrp;
1884 n = transform_node_AddSub(n);
1886 a = get_Add_left(n);
1887 b = get_Add_right(n);
1889 mode = get_irn_mode(n);
1891 if (mode_is_reference(mode)) {
1892 ir_mode *lmode = get_irn_mode(a);
1894 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1895 /* an Add(a, NULL) is a hidden Conv */
1896 dbg_info *dbg = get_irn_dbg_info(n);
1897 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
1901 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
1903 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1904 if (mode_is_float(mode)) {
1905 ir_graph *irg = get_irn_irg(n);
1906 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1910 if (mode_is_num(mode)) {
1911 ir_graph *irg = get_irn_irg(n);
1912 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1913 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
1914 && a == b && mode_is_int(mode)) {
1915 ir_node *block = get_nodes_block(n);
1918 get_irn_dbg_info(n),
1921 new_r_Const_long(irg, mode, 2),
1923 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1928 get_irn_dbg_info(n),
1933 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1938 get_irn_dbg_info(n),
1943 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1946 if (get_mode_arithmetic(mode) == irma_twos_complement) {
1947 /* Here we rely on constants be on the RIGHT side */
1949 ir_node *op = get_Not_op(a);
1951 if (is_Const(b) && is_Const_one(b)) {
1953 ir_node *blk = get_nodes_block(n);
1954 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
1955 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
1960 n = new_r_Const(irg, get_mode_minus_one(mode));
1961 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
1966 ir_node *op = get_Not_op(b);
1970 n = new_r_Const(irg, get_mode_minus_one(mode));
1971 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
1978 a_vrp = vrp_get_info(a);
1979 b_vrp = vrp_get_info(b);
1981 if (a_vrp && b_vrp) {
1982 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
1984 if (tarval_is_null(c)) {
1985 dbg_info *dbgi = get_irn_dbg_info(n);
1986 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
1990 } /* transform_node_Add */
1993 * returns -cnst or NULL if impossible
1995 static ir_node *const_negate(ir_node *cnst)
1997 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
1998 dbg_info *dbgi = get_irn_dbg_info(cnst);
1999 ir_graph *irg = get_irn_irg(cnst);
2000 if (tv == tarval_bad) return NULL;
2001 return new_rd_Const(dbgi, irg, tv);
2005 * Do the AddSub optimization, then Transform
2006 * Constant folding on Phi
2007 * Sub(0,a) -> Minus(a)
2008 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2009 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2010 * Sub(Add(a, x), x) -> a
2011 * Sub(x, Add(x, a)) -> -a
2012 * Sub(x, Const) -> Add(x, -Const)
2014 static ir_node *transform_node_Sub(ir_node *n)
2020 n = transform_node_AddSub(n);
2022 a = get_Sub_left(n);
2023 b = get_Sub_right(n);
2025 mode = get_irn_mode(n);
2027 if (mode_is_int(mode)) {
2028 ir_mode *lmode = get_irn_mode(a);
2030 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2031 /* a Sub(a, NULL) is a hidden Conv */
2032 dbg_info *dbg = get_irn_dbg_info(n);
2033 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2034 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2038 if (mode == lmode &&
2039 get_mode_arithmetic(mode) == irma_twos_complement &&
2041 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2043 dbg_info *dbg = get_irn_dbg_info(n);
2044 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2045 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2051 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2053 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2054 if (mode_is_float(mode)) {
2055 ir_graph *irg = get_irn_irg(n);
2056 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2060 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2061 /* a - C -> a + (-C) */
2062 ir_node *cnst = const_negate(b);
2064 ir_node *block = get_nodes_block(n);
2065 dbg_info *dbgi = get_irn_dbg_info(n);
2067 n = new_rd_Add(dbgi, block, a, cnst, mode);
2068 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2073 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2074 dbg_info *dbg = get_irn_dbg_info(n);
2075 ir_node *block = get_nodes_block(n);
2076 ir_node *left = get_Minus_op(a);
2077 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2079 n = new_rd_Minus(dbg, block, add, mode);
2080 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2082 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2083 dbg_info *dbg = get_irn_dbg_info(n);
2084 ir_node *block = get_nodes_block(n);
2085 ir_node *right = get_Minus_op(b);
2087 n = new_rd_Add(dbg, block, a, right, mode);
2088 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2090 } else if (is_Sub(b)) {
2091 /* a - (b - c) -> a + (c - b)
2092 * -> (a - b) + c iff (b - c) is a pointer */
2093 dbg_info *s_dbg = get_irn_dbg_info(b);
2094 ir_node *s_left = get_Sub_left(b);
2095 ir_node *s_right = get_Sub_right(b);
2096 ir_mode *s_mode = get_irn_mode(b);
2097 if (mode_is_reference(s_mode)) {
2098 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2099 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2100 dbg_info *a_dbg = get_irn_dbg_info(n);
2103 s_right = new_r_Conv(lowest_block, s_right, mode);
2104 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2106 ir_node *s_block = get_nodes_block(b);
2107 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2108 dbg_info *a_dbg = get_irn_dbg_info(n);
2109 ir_node *a_block = get_nodes_block(n);
2111 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2113 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2115 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2116 ir_node *m_right = get_Mul_right(b);
2117 if (is_Const(m_right)) {
2118 ir_node *cnst2 = const_negate(m_right);
2119 if (cnst2 != NULL) {
2120 dbg_info *m_dbg = get_irn_dbg_info(b);
2121 ir_node *m_block = get_nodes_block(b);
2122 ir_node *m_left = get_Mul_left(b);
2123 ir_mode *m_mode = get_irn_mode(b);
2124 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2125 dbg_info *a_dbg = get_irn_dbg_info(n);
2126 ir_node *a_block = get_nodes_block(n);
2128 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2135 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2136 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2138 get_irn_dbg_info(n),
2142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2146 if (mode_wrap_around(mode)) {
2147 ir_node *left = get_Add_left(a);
2148 ir_node *right = get_Add_right(a);
2150 /* FIXME: Does the Conv's work only for two complement or generally? */
2152 if (mode != get_irn_mode(right)) {
2153 /* This Sub is an effective Cast */
2154 right = new_r_Conv(get_nodes_block(n), right, mode);
2157 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2159 } else if (right == b) {
2160 if (mode != get_irn_mode(left)) {
2161 /* This Sub is an effective Cast */
2162 left = new_r_Conv(get_nodes_block(n), left, mode);
2165 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2171 if (mode_wrap_around(mode)) {
2172 ir_node *left = get_Add_left(b);
2173 ir_node *right = get_Add_right(b);
2175 /* FIXME: Does the Conv's work only for two complement or generally? */
2177 ir_mode *r_mode = get_irn_mode(right);
2179 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2180 if (mode != r_mode) {
2181 /* This Sub is an effective Cast */
2182 n = new_r_Conv(get_nodes_block(n), n, mode);
2184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2186 } else if (right == a) {
2187 ir_mode *l_mode = get_irn_mode(left);
2189 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2190 if (mode != l_mode) {
2191 /* This Sub is an effective Cast */
2192 n = new_r_Conv(get_nodes_block(n), n, mode);
2194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2199 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2200 ir_mode *mode = get_irn_mode(a);
2202 if (mode == get_irn_mode(b)) {
2204 ir_node *op_a = get_Conv_op(a);
2205 ir_node *op_b = get_Conv_op(b);
2207 /* check if it's allowed to skip the conv */
2208 ma = get_irn_mode(op_a);
2209 mb = get_irn_mode(op_b);
2211 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2212 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2215 set_Sub_right(n, b);
2221 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2222 if (!is_reassoc_running() && is_Mul(a)) {
2223 ir_node *ma = get_Mul_left(a);
2224 ir_node *mb = get_Mul_right(a);
2227 ir_node *blk = get_nodes_block(n);
2228 ir_graph *irg = get_irn_irg(n);
2230 get_irn_dbg_info(n),
2234 get_irn_dbg_info(n),
2237 new_r_Const(irg, get_mode_one(mode)),
2240 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2242 } else if (mb == b) {
2243 ir_node *blk = get_nodes_block(n);
2244 ir_graph *irg = get_irn_irg(n);
2246 get_irn_dbg_info(n),
2250 get_irn_dbg_info(n),
2253 new_r_Const(irg, get_mode_one(mode)),
2256 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2260 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2261 ir_node *x = get_Sub_left(a);
2262 ir_node *y = get_Sub_right(a);
2263 ir_node *blk = get_nodes_block(n);
2264 ir_mode *m_b = get_irn_mode(b);
2265 ir_mode *m_y = get_irn_mode(y);
2269 /* Determine the right mode for the Add. */
2272 else if (mode_is_reference(m_b))
2274 else if (mode_is_reference(m_y))
2278 * Both modes are different but none is reference,
2279 * happens for instance in SubP(SubP(P, Iu), Is).
2280 * We have two possibilities here: Cast or ignore.
2281 * Currently we ignore this case.
2286 add = new_r_Add(blk, y, b, add_mode);
2288 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2289 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2293 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2294 if (is_Const(a) && is_Not(b)) {
2295 /* c - ~X = X + (c+1) */
2296 ir_tarval *tv = get_Const_tarval(a);
2298 tv = tarval_add(tv, get_mode_one(mode));
2299 if (tv != tarval_bad) {
2300 ir_node *blk = get_nodes_block(n);
2301 ir_graph *irg = get_irn_irg(n);
2302 ir_node *c = new_r_Const(irg, tv);
2303 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2304 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2310 } /* transform_node_Sub */
2313 * Several transformation done on n*n=2n bits mul.
2314 * These transformations must be done here because new nodes may be produced.
2316 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2319 ir_node *a = get_Mul_left(n);
2320 ir_node *b = get_Mul_right(n);
2321 ir_tarval *ta = value_of(a);
2322 ir_tarval *tb = value_of(b);
2323 ir_mode *smode = get_irn_mode(a);
2325 if (ta == get_mode_one(smode)) {
2326 /* (L)1 * (L)b = (L)b */
2327 ir_node *blk = get_nodes_block(n);
2328 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2329 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2332 else if (ta == get_mode_minus_one(smode)) {
2333 /* (L)-1 * (L)b = (L)b */
2334 ir_node *blk = get_nodes_block(n);
2335 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2336 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2340 if (tb == get_mode_one(smode)) {
2341 /* (L)a * (L)1 = (L)a */
2342 ir_node *blk = get_irn_n(a, -1);
2343 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2344 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2347 else if (tb == get_mode_minus_one(smode)) {
2348 /* (L)a * (L)-1 = (L)-a */
2349 ir_node *blk = get_nodes_block(n);
2350 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2351 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2352 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2359 * Transform Mul(a,-1) into -a.
2360 * Do constant evaluation of Phi nodes.
2361 * Do architecture dependent optimizations on Mul nodes
2363 static ir_node *transform_node_Mul(ir_node *n)
2365 ir_node *c, *oldn = n;
2366 ir_mode *mode = get_irn_mode(n);
2367 ir_node *a = get_Mul_left(n);
2368 ir_node *b = get_Mul_right(n);
2370 if (is_Bad(a) || is_Bad(b))
2373 if (mode != get_irn_mode(a))
2374 return transform_node_Mul2n(n, mode);
2376 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2378 if (mode_is_signed(mode)) {
2381 if (value_of(a) == get_mode_minus_one(mode))
2383 else if (value_of(b) == get_mode_minus_one(mode))
2386 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2387 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2392 if (is_Const(b)) { /* (-a) * const -> a * -const */
2393 ir_node *cnst = const_negate(b);
2395 dbg_info *dbgi = get_irn_dbg_info(n);
2396 ir_node *block = get_nodes_block(n);
2397 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2398 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2401 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2402 dbg_info *dbgi = get_irn_dbg_info(n);
2403 ir_node *block = get_nodes_block(n);
2404 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2405 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2407 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2408 ir_node *sub_l = get_Sub_left(b);
2409 ir_node *sub_r = get_Sub_right(b);
2410 dbg_info *dbgi = get_irn_dbg_info(n);
2411 ir_node *block = get_nodes_block(n);
2412 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2413 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2414 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2417 } else if (is_Minus(b)) {
2418 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2419 ir_node *sub_l = get_Sub_left(a);
2420 ir_node *sub_r = get_Sub_right(a);
2421 dbg_info *dbgi = get_irn_dbg_info(n);
2422 ir_node *block = get_nodes_block(n);
2423 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2424 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2425 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2428 } else if (is_Shl(a)) {
2429 ir_node *const shl_l = get_Shl_left(a);
2430 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2431 /* (1 << x) * b -> b << x */
2432 dbg_info *const dbgi = get_irn_dbg_info(n);
2433 ir_node *const block = get_nodes_block(n);
2434 ir_node *const shl_r = get_Shl_right(a);
2435 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2436 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2439 } else if (is_Shl(b)) {
2440 ir_node *const shl_l = get_Shl_left(b);
2441 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2442 /* a * (1 << x) -> a << x */
2443 dbg_info *const dbgi = get_irn_dbg_info(n);
2444 ir_node *const block = get_nodes_block(n);
2445 ir_node *const shl_r = get_Shl_right(b);
2446 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2447 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2451 if (get_mode_arithmetic(mode) == irma_ieee754) {
2453 ir_tarval *tv = get_Const_tarval(a);
2454 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2455 && !tarval_is_negative(tv)) {
2456 /* 2.0 * b = b + b */
2457 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2458 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2462 else if (is_Const(b)) {
2463 ir_tarval *tv = get_Const_tarval(b);
2464 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2465 && !tarval_is_negative(tv)) {
2466 /* a * 2.0 = a + a */
2467 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2468 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2473 return arch_dep_replace_mul_with_shifts(n);
2474 } /* transform_node_Mul */
2477 * Transform a Div Node.
2479 static ir_node *transform_node_Div(ir_node *n)
2481 ir_mode *mode = get_Div_resmode(n);
2482 ir_node *a = get_Div_left(n);
2483 ir_node *b = get_Div_right(n);
2485 const ir_node *dummy;
2487 if (mode_is_int(mode)) {
2488 if (is_Const(b) && is_const_Phi(a)) {
2489 /* check for Div(Phi, Const) */
2490 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2492 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2495 } else if (is_Const(a) && is_const_Phi(b)) {
2496 /* check for Div(Const, Phi) */
2497 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2499 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2502 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2503 /* check for Div(Phi, Phi) */
2504 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2506 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2511 if (a == b && value_not_zero(a, &dummy)) {
2512 ir_graph *irg = get_irn_irg(n);
2513 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2514 value = new_r_Const(irg, get_mode_one(mode));
2515 DBG_OPT_CSTEVAL(n, value);
2518 if (mode_is_signed(mode) && is_Const(b)) {
2519 ir_tarval *tv = get_Const_tarval(b);
2521 if (tv == get_mode_minus_one(mode)) {
2523 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2524 DBG_OPT_CSTEVAL(n, value);
2528 /* Try architecture dependent optimization */
2529 value = arch_dep_replace_div_by_const(n);
2532 assert(mode_is_float(mode));
2534 /* Optimize x/c to x*(1/c) */
2535 if (get_mode_arithmetic(mode) == irma_ieee754) {
2536 ir_tarval *tv = value_of(b);
2538 if (tv != tarval_bad) {
2539 int rem = tarval_fp_ops_enabled();
2542 * Floating point constant folding might be disabled here to
2544 * However, as we check for exact result, doing it is safe.
2547 tarval_enable_fp_ops(1);
2548 tv = tarval_div(get_mode_one(mode), tv);
2549 tarval_enable_fp_ops(rem);
2551 /* Do the transformation if the result is either exact or we are
2552 not using strict rules. */
2553 if (tv != tarval_bad &&
2554 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2555 ir_node *block = get_nodes_block(n);
2556 ir_graph *irg = get_irn_irg(block);
2557 ir_node *c = new_r_Const(irg, tv);
2558 dbg_info *dbgi = get_irn_dbg_info(n);
2559 value = new_rd_Mul(dbgi, block, a, c, mode);
2572 /* Turn Div into a tuple (mem, jmp, bad, value) */
2573 mem = get_Div_mem(n);
2574 blk = get_nodes_block(n);
2575 irg = get_irn_irg(blk);
2577 /* skip a potential Pin */
2578 mem = skip_Pin(mem);
2579 turn_into_tuple(n, pn_Div_max);
2580 set_Tuple_pred(n, pn_Div_M, mem);
2581 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2582 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2583 set_Tuple_pred(n, pn_Div_res, value);
2586 } /* transform_node_Div */
2589 * Transform a Mod node.
2591 static ir_node *transform_node_Mod(ir_node *n)
2593 ir_mode *mode = get_Mod_resmode(n);
2594 ir_node *a = get_Mod_left(n);
2595 ir_node *b = get_Mod_right(n);
2600 if (is_Const(b) && is_const_Phi(a)) {
2601 /* check for Div(Phi, Const) */
2602 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2604 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2608 else if (is_Const(a) && is_const_Phi(b)) {
2609 /* check for Div(Const, Phi) */
2610 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2612 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2616 else if (is_const_Phi(a) && is_const_Phi(b)) {
2617 /* check for Div(Phi, Phi) */
2618 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2620 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2627 irg = get_irn_irg(n);
2628 if (tv != tarval_bad) {
2629 value = new_r_Const(irg, tv);
2631 DBG_OPT_CSTEVAL(n, value);
2634 ir_node *a = get_Mod_left(n);
2635 ir_node *b = get_Mod_right(n);
2636 const ir_node *dummy;
2638 if (a == b && value_not_zero(a, &dummy)) {
2639 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2640 value = new_r_Const(irg, get_mode_null(mode));
2641 DBG_OPT_CSTEVAL(n, value);
2644 if (mode_is_signed(mode) && is_Const(b)) {
2645 ir_tarval *tv = get_Const_tarval(b);
2647 if (tv == get_mode_minus_one(mode)) {
2649 value = new_r_Const(irg, get_mode_null(mode));
2650 DBG_OPT_CSTEVAL(n, value);
2654 /* Try architecture dependent optimization */
2655 value = arch_dep_replace_mod_by_const(n);
2664 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2665 mem = get_Mod_mem(n);
2666 blk = get_nodes_block(n);
2667 irg = get_irn_irg(blk);
2669 /* skip a potential Pin */
2670 mem = skip_Pin(mem);
2671 turn_into_tuple(n, pn_Mod_max);
2672 set_Tuple_pred(n, pn_Mod_M, mem);
2673 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2674 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2675 set_Tuple_pred(n, pn_Mod_res, value);
2678 } /* transform_node_Mod */
2681 * Transform a Cond node.
2683 * Replace the Cond by a Jmp if it branches on a constant
2686 static ir_node *transform_node_Cond(ir_node *n)
2689 ir_node *a = get_Cond_selector(n);
2690 ir_tarval *ta = value_of(a);
2691 ir_graph *irg = get_irn_irg(n);
2694 /* we need block info which is not available in floating irgs */
2695 if (get_irg_pinned(irg) == op_pin_state_floats)
2698 if ((ta != tarval_bad) &&
2699 (get_irn_mode(a) == mode_b) &&
2700 (get_opt_unreachable_code())) {
2701 /* It's a boolean Cond, branching on a boolean constant.
2702 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2703 ir_node *blk = get_nodes_block(n);
2704 jmp = new_r_Jmp(blk);
2705 turn_into_tuple(n, pn_Cond_max);
2706 if (ta == tarval_b_true) {
2707 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2708 set_Tuple_pred(n, pn_Cond_true, jmp);
2710 set_Tuple_pred(n, pn_Cond_false, jmp);
2711 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2713 /* We might generate an endless loop, so keep it alive. */
2714 add_End_keepalive(get_irg_end(irg), blk);
2717 } /* transform_node_Cond */
2720 * Prototype of a recursive transform function
2721 * for bitwise distributive transformations.
2723 typedef ir_node* (*recursive_transform)(ir_node *n);
2726 * makes use of distributive laws for and, or, eor
2727 * and(a OP c, b OP c) -> and(a, b) OP c
2728 * note, might return a different op than n
2730 static ir_node *transform_bitwise_distributive(ir_node *n,
2731 recursive_transform trans_func)
2734 ir_node *a = get_binop_left(n);
2735 ir_node *b = get_binop_right(n);
2736 ir_op *op = get_irn_op(a);
2737 ir_op *op_root = get_irn_op(n);
2739 if (op != get_irn_op(b))
2742 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2743 if (op == op_Conv) {
2744 ir_node *a_op = get_Conv_op(a);
2745 ir_node *b_op = get_Conv_op(b);
2746 ir_mode *a_mode = get_irn_mode(a_op);
2747 ir_mode *b_mode = get_irn_mode(b_op);
2748 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2749 ir_node *blk = get_nodes_block(n);
2752 set_binop_left(n, a_op);
2753 set_binop_right(n, b_op);
2754 set_irn_mode(n, a_mode);
2756 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2758 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2764 /* nothing to gain here */
2768 if (op == op_Shrs || op == op_Shr || op == op_Shl
2769 || op == op_And || op == op_Or || op == op_Eor) {
2770 ir_node *a_left = get_binop_left(a);
2771 ir_node *a_right = get_binop_right(a);
2772 ir_node *b_left = get_binop_left(b);
2773 ir_node *b_right = get_binop_right(b);
2775 ir_node *op1 = NULL;
2776 ir_node *op2 = NULL;
2778 if (is_op_commutative(op)) {
2779 if (a_left == b_left) {
2783 } else if (a_left == b_right) {
2787 } else if (a_right == b_left) {
2793 if (a_right == b_right) {
2800 /* (a sop c) & (b sop c) => (a & b) sop c */
2801 ir_node *blk = get_nodes_block(n);
2803 ir_node *new_n = exact_copy(n);
2804 set_binop_left(new_n, op1);
2805 set_binop_right(new_n, op2);
2806 new_n = trans_func(new_n);
2808 if (op_root == op_Eor && op == op_Or) {
2809 dbg_info *dbgi = get_irn_dbg_info(n);
2810 ir_mode *mode = get_irn_mode(c);
2812 c = new_rd_Not(dbgi, blk, c, mode);
2813 n = new_rd_And(dbgi, blk, new_n, c, mode);
2816 set_nodes_block(n, blk);
2817 set_binop_left(n, new_n);
2818 set_binop_right(n, c);
2822 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2831 * Create a 0 constant of given mode.
2833 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2835 ir_tarval *tv = get_mode_null(mode);
2836 ir_node *cnst = new_r_Const(irg, tv);
2844 static ir_node *transform_node_And(ir_node *n)
2846 ir_node *c, *oldn = n;
2847 ir_node *a = get_And_left(n);
2848 ir_node *b = get_And_right(n);
2850 vrp_attr *a_vrp, *b_vrp;
2852 if (is_Cmp(a) && is_Cmp(b)) {
2853 ir_node *a_left = get_Cmp_left(a);
2854 ir_node *a_right = get_Cmp_right(a);
2855 ir_node *b_left = get_Cmp_left(b);
2856 ir_node *b_right = get_Cmp_right(b);
2857 ir_relation a_relation = get_Cmp_relation(a);
2858 ir_relation b_relation = get_Cmp_relation(b);
2859 /* we can combine the relations of two compares with the same
2861 if (a_left == b_left && b_left == b_right) {
2862 dbg_info *dbgi = get_irn_dbg_info(n);
2863 ir_node *block = get_nodes_block(n);
2864 ir_relation new_relation = a_relation & b_relation;
2865 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2867 /* Cmp(a==0) and Cmp(b==0) can be optimized to Cmp(a|b==0) */
2868 if (is_Const(a_right) && is_Const_null(a_right)
2869 && is_Const(b_right) && is_Const_null(b_right)
2870 && a_relation == b_relation && a_relation == ir_relation_equal
2871 && !mode_is_float(get_irn_mode(a_left))
2872 && !mode_is_float(get_irn_mode(b_left))) {
2873 dbg_info *dbgi = get_irn_dbg_info(n);
2874 ir_node *block = get_nodes_block(n);
2875 ir_mode *mode = get_irn_mode(a_left);
2876 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
2877 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
2878 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
2879 ir_graph *irg = get_irn_irg(n);
2880 ir_node *zero = create_zero_const(irg, mode);
2881 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
2885 mode = get_irn_mode(n);
2886 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
2890 ir_node *op = get_Not_op(b);
2892 ir_node *ba = get_And_left(op);
2893 ir_node *bb = get_And_right(op);
2895 /* it's enough to test the following cases due to normalization! */
2896 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2897 /* (a|b) & ~(a&b) = a^b */
2898 ir_node *block = get_nodes_block(n);
2900 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
2901 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2909 ir_node *op = get_Not_op(a);
2911 ir_node *aa = get_And_left(op);
2912 ir_node *ab = get_And_right(op);
2914 /* it's enough to test the following cases due to normalization! */
2915 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2916 /* (a|b) & ~(a&b) = a^b */
2917 ir_node *block = get_nodes_block(n);
2919 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
2920 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2927 ir_node *al = get_Eor_left(a);
2928 ir_node *ar = get_Eor_right(a);
2931 /* (b ^ a) & b -> ~a & b */
2932 dbg_info *dbg = get_irn_dbg_info(n);
2933 ir_node *block = get_nodes_block(n);
2935 ar = new_rd_Not(dbg, block, ar, mode);
2936 n = new_rd_And(dbg, block, ar, b, mode);
2937 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2941 /* (a ^ b) & b -> ~a & b */
2942 dbg_info *dbg = get_irn_dbg_info(n);
2943 ir_node *block = get_nodes_block(n);
2945 al = new_rd_Not(dbg, block, al, mode);
2946 n = new_rd_And(dbg, block, al, b, mode);
2947 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2952 ir_node *bl = get_Eor_left(b);
2953 ir_node *br = get_Eor_right(b);
2956 /* a & (a ^ b) -> a & ~b */
2957 dbg_info *dbg = get_irn_dbg_info(n);
2958 ir_node *block = get_nodes_block(n);
2960 br = new_rd_Not(dbg, block, br, mode);
2961 n = new_rd_And(dbg, block, br, a, mode);
2962 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2966 /* a & (b ^ a) -> a & ~b */
2967 dbg_info *dbg = get_irn_dbg_info(n);
2968 ir_node *block = get_nodes_block(n);
2970 bl = new_rd_Not(dbg, block, bl, mode);
2971 n = new_rd_And(dbg, block, bl, a, mode);
2972 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2976 if (is_Not(a) && is_Not(b)) {
2977 /* ~a & ~b = ~(a|b) */
2978 ir_node *block = get_nodes_block(n);
2979 ir_mode *mode = get_irn_mode(n);
2983 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
2984 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
2985 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2989 b_vrp = vrp_get_info(b);
2990 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
2991 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
2997 a_vrp = vrp_get_info(a);
2998 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
2999 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3003 n = transform_bitwise_distributive(n, transform_node_And);
3006 } /* transform_node_And */
3008 /* the order of the values is important! */
3009 typedef enum const_class {
3015 static const_class classify_const(const ir_node* n)
3017 if (is_Const(n)) return const_const;
3018 if (is_irn_constlike(n)) return const_like;
3023 * Determines whether r is more constlike or has a larger index (in that order)
3026 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3028 const const_class l_order = classify_const(l);
3029 const const_class r_order = classify_const(r);
3031 l_order > r_order ||
3032 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3038 static ir_node *transform_node_Eor(ir_node *n)
3040 ir_node *c, *oldn = n;
3041 ir_node *a = get_Eor_left(n);
3042 ir_node *b = get_Eor_right(n);
3043 ir_mode *mode = get_irn_mode(n);
3045 /* we can combine the relations of two compares with the same operands */
3046 if (is_Cmp(a) && is_Cmp(b)) {
3047 ir_node *a_left = get_Cmp_left(a);
3048 ir_node *a_right = get_Cmp_left(a);
3049 ir_node *b_left = get_Cmp_left(b);
3050 ir_node *b_right = get_Cmp_right(b);
3051 if (a_left == b_left && b_left == b_right) {
3052 dbg_info *dbgi = get_irn_dbg_info(n);
3053 ir_node *block = get_nodes_block(n);
3054 ir_relation a_relation = get_Cmp_relation(a);
3055 ir_relation b_relation = get_Cmp_relation(b);
3056 ir_relation new_relation = a_relation ^ b_relation;
3057 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3061 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3063 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3064 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3065 dbg_info *dbg = get_irn_dbg_info(n);
3066 ir_node *block = get_nodes_block(n);
3067 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3068 ir_node *new_left = get_Not_op(a);
3069 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3070 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3072 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3073 dbg_info *dbg = get_irn_dbg_info(n);
3074 ir_node *block = get_nodes_block(n);
3075 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3076 ir_node *new_right = get_Not_op(b);
3077 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3078 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3082 /* x ^ 1...1 -> ~1 */
3083 if (is_Const(b) && is_Const_all_one(b)) {
3084 n = new_r_Not(get_nodes_block(n), a, mode);
3085 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3089 n = transform_bitwise_distributive(n, transform_node_Eor);
3091 } /* transform_node_Eor */
3096 static ir_node *transform_node_Not(ir_node *n)
3098 ir_node *c, *oldn = n;
3099 ir_node *a = get_Not_op(n);
3100 ir_mode *mode = get_irn_mode(n);
3102 HANDLE_UNOP_PHI(tarval_not,a,c);
3104 /* check for a boolean Not */
3106 dbg_info *dbgi = get_irn_dbg_info(a);
3107 ir_node *block = get_nodes_block(a);
3108 ir_relation relation = get_Cmp_relation(a);
3109 relation = get_negated_relation(relation);
3110 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3115 /* normalize ~(a ^ b) => a ^ ~b */
3117 dbg_info *dbg = get_irn_dbg_info(n);
3118 ir_node *block = get_nodes_block(n);
3119 ir_node *eor_right = get_Eor_right(a);
3120 ir_node *eor_left = get_Eor_left(a);
3121 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3122 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3126 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3127 if (is_Minus(a)) { /* ~-x -> x + -1 */
3128 dbg_info *dbg = get_irn_dbg_info(n);
3129 ir_graph *irg = get_irn_irg(n);
3130 ir_node *block = get_nodes_block(n);
3131 ir_node *add_l = get_Minus_op(a);
3132 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3133 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3134 } else if (is_Add(a)) {
3135 ir_node *add_r = get_Add_right(a);
3136 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3137 /* ~(x + -1) = -x */
3138 ir_node *op = get_Add_left(a);
3139 ir_node *blk = get_nodes_block(n);
3140 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3149 * Transform a Minus.
3153 * -(a >>u (size-1)) = a >>s (size-1)
3154 * -(a >>s (size-1)) = a >>u (size-1)
3155 * -(a * const) -> a * -const
3157 static ir_node *transform_node_Minus(ir_node *n)
3159 ir_node *c, *oldn = n;
3160 ir_node *a = get_Minus_op(n);
3163 HANDLE_UNOP_PHI(tarval_neg,a,c);
3165 mode = get_irn_mode(a);
3166 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3167 /* the following rules are only to twos-complement */
3170 ir_node *op = get_Not_op(a);
3171 ir_tarval *tv = get_mode_one(mode);
3172 ir_node *blk = get_nodes_block(n);
3173 ir_graph *irg = get_irn_irg(blk);
3174 ir_node *c = new_r_Const(irg, tv);
3175 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3176 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3180 ir_node *c = get_Shr_right(a);
3183 ir_tarval *tv = get_Const_tarval(c);
3185 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3186 /* -(a >>u (size-1)) = a >>s (size-1) */
3187 ir_node *v = get_Shr_left(a);
3189 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3190 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3196 ir_node *c = get_Shrs_right(a);
3199 ir_tarval *tv = get_Const_tarval(c);
3201 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3202 /* -(a >>s (size-1)) = a >>u (size-1) */
3203 ir_node *v = get_Shrs_left(a);
3205 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3206 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3213 /* - (a-b) = b - a */
3214 ir_node *la = get_Sub_left(a);
3215 ir_node *ra = get_Sub_right(a);
3216 ir_node *blk = get_nodes_block(n);
3218 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3219 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3223 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3224 ir_node *mul_l = get_Mul_left(a);
3225 ir_node *mul_r = get_Mul_right(a);
3226 ir_tarval *tv = value_of(mul_r);
3227 if (tv != tarval_bad) {
3228 tv = tarval_neg(tv);
3229 if (tv != tarval_bad) {
3230 ir_graph *irg = get_irn_irg(n);
3231 ir_node *cnst = new_r_Const(irg, tv);
3232 dbg_info *dbg = get_irn_dbg_info(a);
3233 ir_node *block = get_nodes_block(a);
3234 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3235 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3242 } /* transform_node_Minus */
3245 * Transform a Proj(Load) with a non-null address.
3247 static ir_node *transform_node_Proj_Load(ir_node *proj)
3249 if (get_opt_ldst_only_null_ptr_exceptions()) {
3250 if (get_irn_mode(proj) == mode_X) {
3251 ir_node *load = get_Proj_pred(proj);
3253 /* get the Load address */
3254 const ir_node *addr = get_Load_ptr(load);
3255 const ir_node *confirm;
3257 if (value_not_null(addr, &confirm)) {
3258 if (confirm == NULL) {
3259 /* this node may float if it did not depend on a Confirm */
3260 set_irn_pinned(load, op_pin_state_floats);
3262 if (get_Proj_proj(proj) == pn_Load_X_except) {
3263 ir_graph *irg = get_irn_irg(proj);
3264 DBG_OPT_EXC_REM(proj);
3265 return new_r_Bad(irg, mode_X);
3267 ir_node *blk = get_nodes_block(load);
3268 return new_r_Jmp(blk);
3274 } /* transform_node_Proj_Load */
3277 * Transform a Proj(Store) with a non-null address.
3279 static ir_node *transform_node_Proj_Store(ir_node *proj)
3281 if (get_opt_ldst_only_null_ptr_exceptions()) {
3282 if (get_irn_mode(proj) == mode_X) {
3283 ir_node *store = get_Proj_pred(proj);
3285 /* get the load/store address */
3286 const ir_node *addr = get_Store_ptr(store);
3287 const ir_node *confirm;
3289 if (value_not_null(addr, &confirm)) {
3290 if (confirm == NULL) {
3291 /* this node may float if it did not depend on a Confirm */
3292 set_irn_pinned(store, op_pin_state_floats);
3294 if (get_Proj_proj(proj) == pn_Store_X_except) {
3295 ir_graph *irg = get_irn_irg(proj);
3296 DBG_OPT_EXC_REM(proj);
3297 return new_r_Bad(irg, mode_X);
3299 ir_node *blk = get_nodes_block(store);
3300 return new_r_Jmp(blk);
3306 } /* transform_node_Proj_Store */
3309 * Transform a Proj(Div) with a non-zero value.
3310 * Removes the exceptions and routes the memory to the NoMem node.
3312 static ir_node *transform_node_Proj_Div(ir_node *proj)
3314 ir_node *div = get_Proj_pred(proj);
3315 ir_node *b = get_Div_right(div);
3316 ir_node *res, *new_mem;
3317 const ir_node *confirm;
3320 if (value_not_zero(b, &confirm)) {
3321 /* div(x, y) && y != 0 */
3322 if (confirm == NULL) {
3323 /* we are sure we have a Const != 0 */
3324 new_mem = get_Div_mem(div);
3325 new_mem = skip_Pin(new_mem);
3326 set_Div_mem(div, new_mem);
3327 set_irn_pinned(div, op_pin_state_floats);
3330 proj_nr = get_Proj_proj(proj);
3332 case pn_Div_X_regular:
3333 return new_r_Jmp(get_nodes_block(div));
3335 case pn_Div_X_except: {
3336 ir_graph *irg = get_irn_irg(proj);
3337 /* we found an exception handler, remove it */
3338 DBG_OPT_EXC_REM(proj);
3339 return new_r_Bad(irg, mode_X);
3343 ir_graph *irg = get_irn_irg(proj);
3344 res = get_Div_mem(div);
3345 new_mem = get_irg_no_mem(irg);
3348 /* This node can only float up to the Confirm block */
3349 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3351 set_irn_pinned(div, op_pin_state_floats);
3352 /* this is a Div without exception, we can remove the memory edge */
3353 set_Div_mem(div, new_mem);
3359 } /* transform_node_Proj_Div */
3362 * Transform a Proj(Mod) with a non-zero value.
3363 * Removes the exceptions and routes the memory to the NoMem node.
3365 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3367 ir_node *mod = get_Proj_pred(proj);
3368 ir_node *b = get_Mod_right(mod);
3369 ir_node *res, *new_mem;
3370 const ir_node *confirm;
3373 if (value_not_zero(b, &confirm)) {
3374 /* mod(x, y) && y != 0 */
3375 proj_nr = get_Proj_proj(proj);
3377 if (confirm == NULL) {
3378 /* we are sure we have a Const != 0 */
3379 new_mem = get_Mod_mem(mod);
3380 new_mem = skip_Pin(new_mem);
3381 set_Mod_mem(mod, new_mem);
3382 set_irn_pinned(mod, op_pin_state_floats);
3387 case pn_Mod_X_regular:
3388 return new_r_Jmp(get_irn_n(mod, -1));
3390 case pn_Mod_X_except: {
3391 ir_graph *irg = get_irn_irg(proj);
3392 /* we found an exception handler, remove it */
3393 DBG_OPT_EXC_REM(proj);
3394 return new_r_Bad(irg, mode_X);
3398 ir_graph *irg = get_irn_irg(proj);
3399 res = get_Mod_mem(mod);
3400 new_mem = get_irg_no_mem(irg);
3403 /* This node can only float up to the Confirm block */
3404 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3406 /* this is a Mod without exception, we can remove the memory edge */
3407 set_Mod_mem(mod, new_mem);
3411 if (get_Mod_left(mod) == b) {
3412 /* a % a = 0 if a != 0 */
3413 ir_graph *irg = get_irn_irg(proj);
3414 ir_mode *mode = get_irn_mode(proj);
3415 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3417 DBG_OPT_CSTEVAL(mod, res);
3423 } /* transform_node_Proj_Mod */
3426 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3428 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3430 ir_node *n = get_Proj_pred(proj);
3431 ir_node *b = get_Cond_selector(n);
3433 if (!get_opt_unreachable_code())
3436 if (mode_is_int(get_irn_mode(b))) {
3437 ir_tarval *tb = value_of(b);
3439 if (tb != tarval_bad) {
3440 /* we have a constant switch */
3441 long num = get_Proj_proj(proj);
3443 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3444 if (get_tarval_long(tb) == num) {
3445 /* Do NOT create a jump here, or we will have 2 control flow ops
3446 * in a block. This case is optimized away in optimize_cf(). */
3449 ir_graph *irg = get_irn_irg(proj);
3450 /* this case will NEVER be taken, kill it */
3451 return new_r_Bad(irg, mode_X);
3455 long num = get_Proj_proj(proj);
3456 vrp_attr *b_vrp = vrp_get_info(b);
3457 if (num != get_Cond_default_proj(n) && b_vrp) {
3458 /* Try handling with vrp data. We only remove dead parts. */
3459 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3461 if (b_vrp->range_type == VRP_RANGE) {
3462 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3463 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3465 if ((cmp_result & ir_relation_greater) == cmp_result
3466 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3467 ir_graph *irg = get_irn_irg(proj);
3468 return new_r_Bad(irg, mode_X);
3470 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3471 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3472 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3474 if ((cmp_result & ir_relation_less_equal) == cmp_result
3475 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3476 ir_graph *irg = get_irn_irg(proj);
3477 return new_r_Bad(irg, mode_X);
3482 tarval_and( b_vrp->bits_set, tp),
3484 ) == ir_relation_equal)) {
3485 ir_graph *irg = get_irn_irg(proj);
3486 return new_r_Bad(irg, mode_X);
3492 tarval_not(b_vrp->bits_not_set)),
3493 tarval_not(b_vrp->bits_not_set))
3494 == ir_relation_equal)) {
3495 ir_graph *irg = get_irn_irg(proj);
3496 return new_r_Bad(irg, mode_X);
3505 * return true if the operation returns a value with exactly 1 bit set
3507 static bool is_single_bit(const ir_node *node)
3509 /* a first implementation, could be extended with vrp and others... */
3511 ir_node *shl_l = get_Shl_left(node);
3512 ir_mode *mode = get_irn_mode(node);
3513 int modulo = get_mode_modulo_shift(mode);
3514 /* this works if we shift a 1 and we have modulo shift */
3515 if (is_Const(shl_l) && is_Const_one(shl_l)
3516 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3519 } else if (is_Const(node)) {
3520 ir_tarval *tv = get_Const_tarval(node);
3521 return tarval_is_single_bit(tv);
3527 * Normalizes and optimizes Cmp nodes.
3529 static ir_node *transform_node_Cmp(ir_node *n)
3531 ir_node *left = get_Cmp_left(n);
3532 ir_node *right = get_Cmp_right(n);
3533 ir_mode *mode = get_irn_mode(left);
3534 ir_tarval *tv = NULL;
3535 bool changed = false;
3536 bool changedc = false;
3537 ir_relation relation = get_Cmp_relation(n);
3538 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3540 /* mask out impossible relations */
3541 ir_relation new_relation = relation & possible;
3542 if (new_relation != relation) {
3543 relation = new_relation;
3547 /* Remove unnecessary conversions */
3548 /* TODO handle conv+constant */
3549 if (is_Conv(left) && is_Conv(right)) {
3550 ir_node *op_left = get_Conv_op(left);
3551 ir_node *op_right = get_Conv_op(right);
3552 ir_mode *mode_left = get_irn_mode(op_left);
3553 ir_mode *mode_right = get_irn_mode(op_right);
3555 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3556 && mode_left != mode_b && mode_right != mode_b) {
3557 ir_node *block = get_nodes_block(n);
3559 if (mode_left == mode_right) {
3563 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3564 } else if (smaller_mode(mode_left, mode_right)) {
3565 left = new_r_Conv(block, op_left, mode_right);
3568 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3569 } else if (smaller_mode(mode_right, mode_left)) {
3571 right = new_r_Conv(block, op_right, mode_left);
3573 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3579 * Optimize -a CMP -b into b CMP a.
3580 * This works only for modes where unary Minus cannot Overflow.
3581 * Note that two-complement integers can Overflow so it will NOT work.
3583 if (!mode_overflow_on_unary_Minus(mode) &&
3584 is_Minus(left) && is_Minus(right)) {
3585 left = get_Minus_op(left);
3586 right = get_Minus_op(right);
3587 relation = get_inversed_relation(relation);
3589 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3592 /* remove operation on both sides if possible */
3593 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3595 * The following operations are NOT safe for floating point operations, for instance
3596 * 1.0 + inf == 2.0 + inf, =/=> x == y
3598 if (mode_is_int(mode)) {
3599 unsigned lop = get_irn_opcode(left);
3601 if (lop == get_irn_opcode(right)) {
3602 ir_node *ll, *lr, *rl, *rr;
3604 /* same operation on both sides, try to remove */
3608 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3609 left = get_unop_op(left);
3610 right = get_unop_op(right);
3612 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3615 ll = get_Add_left(left);
3616 lr = get_Add_right(left);
3617 rl = get_Add_left(right);
3618 rr = get_Add_right(right);
3621 /* X + a CMP X + b ==> a CMP b */
3625 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3626 } else if (ll == rr) {
3627 /* X + a CMP b + X ==> a CMP b */
3631 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3632 } else if (lr == rl) {
3633 /* a + X CMP X + b ==> a CMP b */
3637 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3638 } else if (lr == rr) {
3639 /* a + X CMP b + X ==> a CMP b */
3643 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3647 ll = get_Sub_left(left);
3648 lr = get_Sub_right(left);
3649 rl = get_Sub_left(right);
3650 rr = get_Sub_right(right);
3653 /* X - a CMP X - b ==> a CMP b */
3657 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3658 } else if (lr == rr) {
3659 /* a - X CMP b - X ==> a CMP b */
3663 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3667 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3668 /* a ROTL X CMP b ROTL X ==> a CMP b */
3669 left = get_Rotl_left(left);
3670 right = get_Rotl_left(right);
3672 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3680 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3681 if (is_Add(left) || is_Sub(left)) {
3682 ir_node *ll = get_binop_left(left);
3683 ir_node *lr = get_binop_right(left);
3685 if (lr == right && is_Add(left)) {
3691 ir_graph *irg = get_irn_irg(n);
3693 right = create_zero_const(irg, mode);
3695 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3698 if (is_Add(right) || is_Sub(right)) {
3699 ir_node *rl = get_binop_left(right);
3700 ir_node *rr = get_binop_right(right);
3702 if (rr == left && is_Add(right)) {
3708 ir_graph *irg = get_irn_irg(n);
3710 right = create_zero_const(irg, mode);
3712 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3716 if (is_And(left) && is_Const(right)) {
3717 ir_node *ll = get_binop_left(left);
3718 ir_node *lr = get_binop_right(left);
3719 if (is_Shr(ll) && is_Const(lr)) {
3720 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
3721 ir_node *block = get_nodes_block(n);
3722 ir_mode *mode = get_irn_mode(left);
3724 ir_node *llr = get_Shr_right(ll);
3725 if (is_Const(llr)) {
3726 dbg_info *dbg = get_irn_dbg_info(left);
3727 ir_graph *irg = get_irn_irg(left);
3729 ir_tarval *c1 = get_Const_tarval(llr);
3730 ir_tarval *c2 = get_Const_tarval(lr);
3731 ir_tarval *c3 = get_Const_tarval(right);
3732 ir_tarval *mask = tarval_shl(c2, c1);
3733 ir_tarval *value = tarval_shl(c3, c1);
3735 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
3736 right = new_r_Const(irg, value);
3741 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
3743 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
3744 right = get_Eor_right(left);
3745 left = get_Eor_left(left);
3748 } /* mode_is_int(...) */
3751 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
3752 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
3753 if (mode_is_int(mode) && is_And(left)
3754 && (relation == ir_relation_equal
3755 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
3756 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
3757 ir_node *and0 = get_And_left(left);
3758 ir_node *and1 = get_And_right(left);
3759 if (and1 == right) {
3760 ir_node *tmp = and0;
3764 if (and0 == right && is_single_bit(and0)) {
3765 ir_graph *irg = get_irn_irg(n);
3767 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
3768 right = create_zero_const(irg, mode);
3773 /* replace mode_b compares with ands/ors */
3774 if (mode == mode_b) {
3775 ir_node *block = get_nodes_block(n);
3779 case ir_relation_less_equal:
3780 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
3782 case ir_relation_less:
3783 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
3785 case ir_relation_greater_equal:
3786 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
3788 case ir_relation_greater:
3789 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
3791 case ir_relation_less_greater:
3792 bres = new_r_Eor(block, left, right, mode_b);
3794 case ir_relation_equal:
3795 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
3798 #ifdef DEBUG_libfirm
3799 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
3804 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3810 * First step: normalize the compare op
3811 * by placing the constant on the right side
3812 * or moving the lower address node to the left.
3814 if (!operands_are_normalized(left, right)) {
3819 relation = get_inversed_relation(relation);
3824 * Second step: Try to reduce the magnitude
3825 * of a constant. This may help to generate better code
3826 * later and may help to normalize more compares.
3827 * Of course this is only possible for integer values.
3829 tv = value_of(right);
3830 if (tv != tarval_bad) {
3831 ir_mode *mode = get_irn_mode(right);
3833 /* TODO extend to arbitrary constants */
3834 if (is_Conv(left) && tarval_is_null(tv)) {
3835 ir_node *op = get_Conv_op(left);
3836 ir_mode *op_mode = get_irn_mode(op);
3839 * UpConv(x) REL 0 ==> x REL 0
3840 * Don't do this for float values as it's unclear whether it is a
3841 * win. (on the other side it makes detection/creation of fabs hard)
3843 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3844 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
3845 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
3846 !mode_is_float(mode)) {
3847 tv = get_mode_null(op_mode);
3851 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3855 if (tv != tarval_bad) {
3856 /* the following optimization is possible on modes without Overflow
3857 * on Unary Minus or on == and !=:
3858 * -a CMP c ==> a swap(CMP) -c
3860 * Beware: for two-complement Overflow may occur, so only == and != can
3861 * be optimized, see this:
3862 * -MININT < 0 =/=> MININT > 0 !!!
3864 if (is_Minus(left) &&
3865 (!mode_overflow_on_unary_Minus(mode) ||
3866 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
3867 tv = tarval_neg(tv);
3869 if (tv != tarval_bad) {
3870 left = get_Minus_op(left);
3871 relation = get_inversed_relation(relation);
3873 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3875 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
3876 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3877 tv = tarval_not(tv);
3879 if (tv != tarval_bad) {
3880 left = get_Not_op(left);
3882 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3886 /* for integer modes, we have more */
3887 if (mode_is_int(mode)) {
3888 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3889 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
3890 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
3891 tv = tarval_sub(tv, get_mode_one(mode), NULL);
3893 if (tv != tarval_bad) {
3894 relation ^= ir_relation_equal;
3896 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3899 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3900 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
3901 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
3902 tv = tarval_add(tv, get_mode_one(mode));
3904 if (tv != tarval_bad) {
3905 relation ^= ir_relation_equal;
3907 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3911 /* the following reassociations work only for == and != */
3912 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3913 if (tv != tarval_bad) {
3914 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3916 ir_node *c1 = get_Sub_right(left);
3917 ir_tarval *tv2 = value_of(c1);
3919 if (tv2 != tarval_bad) {
3920 tv2 = tarval_add(tv, value_of(c1));
3922 if (tv2 != tarval_bad) {
3923 left = get_Sub_left(left);
3926 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3930 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3931 else if (is_Add(left)) {
3932 ir_node *a_l = get_Add_left(left);
3933 ir_node *a_r = get_Add_right(left);
3937 if (is_Const(a_l)) {
3939 tv2 = value_of(a_l);
3942 tv2 = value_of(a_r);
3945 if (tv2 != tarval_bad) {
3946 tv2 = tarval_sub(tv, tv2, NULL);
3948 if (tv2 != tarval_bad) {
3952 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3956 /* -a == c ==> a == -c, -a != c ==> a != -c */
3957 else if (is_Minus(left)) {
3958 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
3960 if (tv2 != tarval_bad) {
3961 left = get_Minus_op(left);
3964 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3971 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3972 switch (get_irn_opcode(left)) {
3976 c1 = get_And_right(left);
3979 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
3980 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
3982 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
3984 /* TODO: move to constant evaluation */
3985 ir_graph *irg = get_irn_irg(n);
3986 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
3987 c1 = new_r_Const(irg, tv);
3988 DBG_OPT_CSTEVAL(n, c1);
3992 if (tarval_is_single_bit(tv)) {
3994 * optimization for AND:
3996 * And(x, C) == C ==> And(x, C) != 0
3997 * And(x, C) != C ==> And(X, C) == 0
3999 * if C is a single Bit constant.
4002 /* check for Constant's match. We have check hare the tarvals,
4003 because our const might be changed */
4004 if (get_Const_tarval(c1) == tv) {
4005 /* fine: do the transformation */
4006 tv = get_mode_null(get_tarval_mode(tv));
4007 relation ^= ir_relation_less_equal_greater;
4009 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4015 c1 = get_Or_right(left);
4016 if (is_Const(c1) && tarval_is_null(tv)) {
4018 * Or(x, C) == 0 && C != 0 ==> FALSE
4019 * Or(x, C) != 0 && C != 0 ==> TRUE
4021 if (! tarval_is_null(get_Const_tarval(c1))) {
4022 /* TODO: move to constant evaluation */
4023 ir_graph *irg = get_irn_irg(n);
4024 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4025 c1 = new_r_Const(irg, tv);
4026 DBG_OPT_CSTEVAL(n, c1);
4033 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4035 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4038 c1 = get_Shl_right(left);
4040 ir_graph *irg = get_irn_irg(c1);
4041 ir_tarval *tv1 = get_Const_tarval(c1);
4042 ir_mode *mode = get_irn_mode(left);
4043 ir_tarval *minus1 = get_mode_all_one(mode);
4044 ir_tarval *amask = tarval_shr(minus1, tv1);
4045 ir_tarval *cmask = tarval_shl(minus1, tv1);
4048 if (tarval_and(tv, cmask) != tv) {
4049 /* condition not met */
4050 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4051 c1 = new_r_Const(irg, tv);
4052 DBG_OPT_CSTEVAL(n, c1);
4055 sl = get_Shl_left(left);
4056 blk = get_nodes_block(n);
4057 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4058 tv = tarval_shr(tv, tv1);
4060 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4065 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4067 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4070 c1 = get_Shr_right(left);
4072 ir_graph *irg = get_irn_irg(c1);
4073 ir_tarval *tv1 = get_Const_tarval(c1);
4074 ir_mode *mode = get_irn_mode(left);
4075 ir_tarval *minus1 = get_mode_all_one(mode);
4076 ir_tarval *amask = tarval_shl(minus1, tv1);
4077 ir_tarval *cmask = tarval_shr(minus1, tv1);
4080 if (tarval_and(tv, cmask) != tv) {
4081 /* condition not met */
4082 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4083 c1 = new_r_Const(irg, tv);
4084 DBG_OPT_CSTEVAL(n, c1);
4087 sl = get_Shr_left(left);
4088 blk = get_nodes_block(n);
4089 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4090 tv = tarval_shl(tv, tv1);
4092 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4097 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4099 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4102 c1 = get_Shrs_right(left);
4104 ir_graph *irg = get_irn_irg(c1);
4105 ir_tarval *tv1 = get_Const_tarval(c1);
4106 ir_mode *mode = get_irn_mode(left);
4107 ir_tarval *minus1 = get_mode_all_one(mode);
4108 ir_tarval *amask = tarval_shl(minus1, tv1);
4109 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4112 cond = tarval_sub(cond, tv1, NULL);
4113 cond = tarval_shrs(tv, cond);
4115 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4116 /* condition not met */
4117 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4118 c1 = new_r_Const(irg, tv);
4119 DBG_OPT_CSTEVAL(n, c1);
4122 sl = get_Shrs_left(left);
4123 blk = get_nodes_block(n);
4124 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4125 tv = tarval_shl(tv, tv1);
4127 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4132 } /* tarval != bad */
4135 if (changedc) { /* need a new Const */
4136 ir_graph *irg = get_irn_irg(n);
4137 right = new_r_Const(irg, tv);
4141 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4142 ir_node *op = get_Proj_pred(left);
4144 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4145 ir_node *c = get_binop_right(op);
4148 ir_tarval *tv = get_Const_tarval(c);
4150 if (tarval_is_single_bit(tv)) {
4151 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4152 ir_node *v = get_binop_left(op);
4153 ir_node *blk = get_irn_n(op, -1);
4154 ir_graph *irg = get_irn_irg(op);
4155 ir_mode *mode = get_irn_mode(v);
4157 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4158 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4160 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4167 dbg_info *dbgi = get_irn_dbg_info(n);
4168 ir_node *block = get_nodes_block(n);
4170 /* create a new compare */
4171 n = new_rd_Cmp(dbgi, block, left, right, relation);
4178 * Optimize CopyB(mem, x, x) into a Nop.
4180 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4182 ir_node *copyb = get_Proj_pred(proj);
4183 ir_node *a = get_CopyB_dst(copyb);
4184 ir_node *b = get_CopyB_src(copyb);
4187 switch (get_Proj_proj(proj)) {
4188 case pn_CopyB_X_regular:
4189 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4190 DBG_OPT_EXC_REM(proj);
4191 proj = new_r_Jmp(get_nodes_block(copyb));
4193 case pn_CopyB_X_except: {
4194 ir_graph *irg = get_irn_irg(proj);
4195 DBG_OPT_EXC_REM(proj);
4196 proj = new_r_Bad(irg, mode_X);
4204 } /* transform_node_Proj_CopyB */
4207 * Optimize Bounds(idx, idx, upper) into idx.
4209 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4211 ir_node *oldn = proj;
4212 ir_node *bound = get_Proj_pred(proj);
4213 ir_node *idx = get_Bound_index(bound);
4214 ir_node *pred = skip_Proj(idx);
4217 if (idx == get_Bound_lower(bound))
4219 else if (is_Bound(pred)) {
4221 * idx was Bounds checked previously, it is still valid if
4222 * lower <= pred_lower && pred_upper <= upper.
4224 ir_node *lower = get_Bound_lower(bound);
4225 ir_node *upper = get_Bound_upper(bound);
4226 if (get_Bound_lower(pred) == lower &&
4227 get_Bound_upper(pred) == upper) {
4229 * One could expect that we simply return the previous
4230 * Bound here. However, this would be wrong, as we could
4231 * add an exception Proj to a new location then.
4232 * So, we must turn in into a tuple.
4238 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4239 switch (get_Proj_proj(proj)) {
4241 DBG_OPT_EXC_REM(proj);
4242 proj = get_Bound_mem(bound);
4244 case pn_Bound_X_except:
4245 DBG_OPT_EXC_REM(proj);
4246 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4250 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4252 case pn_Bound_X_regular:
4253 DBG_OPT_EXC_REM(proj);
4254 proj = new_r_Jmp(get_nodes_block(bound));
4261 } /* transform_node_Proj_Bound */
4264 * Does all optimizations on nodes that must be done on its Projs
4265 * because of creating new nodes.
4267 static ir_node *transform_node_Proj(ir_node *proj)
4269 ir_node *n = get_Proj_pred(proj);
4271 if (n->op->ops.transform_node_Proj)
4272 return n->op->ops.transform_node_Proj(proj);
4274 } /* transform_node_Proj */
4276 static bool is_block_unreachable(const ir_node *block)
4278 const ir_graph *irg = get_irn_irg(block);
4279 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4281 return get_Block_dom_depth(block) < 0;
4284 static ir_node *transform_node_Block(ir_node *block)
4286 ir_graph *irg = get_irn_irg(block);
4287 int arity = get_irn_arity(block);
4288 ir_node *bad = NULL;
4291 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4294 for (i = 0; i < arity; ++i) {
4295 ir_node *pred = get_Block_cfgpred(block, i);
4296 ir_node *pred_block = get_nodes_block(pred);
4297 if (!is_Bad(pred) && !is_block_unreachable(pred_block))
4300 bad = new_r_Bad(irg, mode_X);
4301 set_irn_n(block, i, bad);
4307 static ir_node *transform_node_Phi(ir_node *phi)
4309 int n = get_irn_arity(phi);
4310 ir_mode *mode = get_irn_mode(phi);
4311 ir_node *block = get_nodes_block(phi);
4312 ir_graph *irg = get_irn_irg(phi);
4313 ir_node *bad = NULL;
4316 /* Set phi-operands for bad-block inputs to bad */
4317 for (i = 0; i < n; ++i) {
4318 ir_node *pred = get_Block_cfgpred(block, i);
4319 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4321 bad = new_r_Bad(irg, mode);
4322 set_irn_n(phi, i, bad);
4326 /* Move Confirms down through Phi nodes. */
4327 if (mode_is_reference(mode)) {
4328 n = get_irn_arity(phi);
4330 /* Beware of Phi0 */
4332 ir_node *pred = get_irn_n(phi, 0);
4333 ir_node *bound, *new_phi, *block, **in;
4334 ir_relation relation;
4336 if (! is_Confirm(pred))
4339 bound = get_Confirm_bound(pred);
4340 relation = get_Confirm_relation(pred);
4342 NEW_ARR_A(ir_node *, in, n);
4343 in[0] = get_Confirm_value(pred);
4345 for (i = 1; i < n; ++i) {
4346 pred = get_irn_n(phi, i);
4348 if (! is_Confirm(pred) ||
4349 get_Confirm_bound(pred) != bound ||
4350 get_Confirm_relation(pred) != relation)
4352 in[i] = get_Confirm_value(pred);
4354 /* move the Confirm nodes "behind" the Phi */
4355 block = get_irn_n(phi, -1);
4356 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4357 return new_r_Confirm(block, new_phi, bound, relation);
4364 * Returns the operands of a commutative bin-op, if one operand is
4365 * a const, it is returned as the second one.
4367 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4369 ir_node *op_a = get_binop_left(binop);
4370 ir_node *op_b = get_binop_right(binop);
4372 assert(is_op_commutative(get_irn_op(binop)));
4374 if (is_Const(op_a)) {
4381 } /* get_comm_Binop_Ops */
4384 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4385 * Such pattern may arise in bitfield stores.
4387 * value c4 value c4 & c2
4388 * AND c3 AND c1 | c3
4395 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4398 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4400 ir_node *irn_and, *c1;
4402 ir_node *and_l, *c3;
4403 ir_node *value, *c4;
4404 ir_node *new_and, *new_const, *block;
4405 ir_mode *mode = get_irn_mode(irn_or);
4407 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4411 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4412 if (!is_Const(c1) || !is_And(irn_and))
4415 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4419 tv1 = get_Const_tarval(c1);
4420 tv2 = get_Const_tarval(c2);
4422 tv = tarval_or(tv1, tv2);
4423 if (tarval_is_all_one(tv)) {
4424 /* the AND does NOT clear a bit with isn't set by the OR */
4425 set_Or_left(irn_or, or_l);
4426 set_Or_right(irn_or, c1);
4428 /* check for more */
4435 get_comm_Binop_Ops(or_l, &and_l, &c3);
4436 if (!is_Const(c3) || !is_And(and_l))
4439 get_comm_Binop_Ops(and_l, &value, &c4);
4443 /* ok, found the pattern, check for conditions */
4444 assert(mode == get_irn_mode(irn_and));
4445 assert(mode == get_irn_mode(or_l));
4446 assert(mode == get_irn_mode(and_l));
4448 tv3 = get_Const_tarval(c3);
4449 tv4 = get_Const_tarval(c4);
4451 tv = tarval_or(tv4, tv2);
4452 if (!tarval_is_all_one(tv)) {
4453 /* have at least one 0 at the same bit position */
4457 if (tv3 != tarval_andnot(tv3, tv4)) {
4458 /* bit in the or_mask is outside the and_mask */
4462 if (tv1 != tarval_andnot(tv1, tv2)) {
4463 /* bit in the or_mask is outside the and_mask */
4467 /* ok, all conditions met */
4468 block = get_irn_n(irn_or, -1);
4469 irg = get_irn_irg(block);
4471 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4473 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4475 set_Or_left(irn_or, new_and);
4476 set_Or_right(irn_or, new_const);
4478 /* check for more */
4480 } /* transform_node_Or_bf_store */
4483 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4485 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4487 ir_mode *mode = get_irn_mode(irn_or);
4488 ir_node *shl, *shr, *block;
4489 ir_node *irn, *x, *c1, *c2, *n;
4490 ir_tarval *tv1, *tv2;
4492 /* some backends can't handle rotl */
4493 if (!be_get_backend_param()->support_rotl)
4496 if (! mode_is_int(mode))
4499 shl = get_binop_left(irn_or);
4500 shr = get_binop_right(irn_or);
4509 } else if (!is_Shl(shl)) {
4511 } else if (!is_Shr(shr)) {
4514 x = get_Shl_left(shl);
4515 if (x != get_Shr_left(shr))
4518 c1 = get_Shl_right(shl);
4519 c2 = get_Shr_right(shr);
4520 if (is_Const(c1) && is_Const(c2)) {
4521 tv1 = get_Const_tarval(c1);
4522 if (! tarval_is_long(tv1))
4525 tv2 = get_Const_tarval(c2);
4526 if (! tarval_is_long(tv2))
4529 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4530 != (int) get_mode_size_bits(mode))
4533 /* yet, condition met */
4534 block = get_nodes_block(irn_or);
4536 n = new_r_Rotl(block, x, c1, mode);
4538 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4542 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4543 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4544 if (!ir_is_negated_value(c1, c2)) {
4548 /* yet, condition met */
4549 block = get_nodes_block(irn_or);
4550 n = new_r_Rotl(block, x, c1, mode);
4551 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4553 } /* transform_node_Or_Rotl */
4555 static bool is_cmp_unequal_zero(const ir_node *node)
4557 ir_relation relation = get_Cmp_relation(node);
4558 ir_node *left = get_Cmp_left(node);
4559 ir_node *right = get_Cmp_right(node);
4560 ir_mode *mode = get_irn_mode(left);
4562 if (!is_Const(right) || !is_Const_null(right))
4564 if (mode_is_signed(mode)) {
4565 return relation == ir_relation_less_greater;
4567 return relation == ir_relation_greater;
4574 static ir_node *transform_node_Or(ir_node *n)
4576 ir_node *c, *oldn = n;
4577 ir_node *a = get_Or_left(n);
4578 ir_node *b = get_Or_right(n);
4581 if (is_Not(a) && is_Not(b)) {
4582 /* ~a | ~b = ~(a&b) */
4583 ir_node *block = get_nodes_block(n);
4585 mode = get_irn_mode(n);
4588 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4589 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4590 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4594 /* we can combine the relations of two compares with the same operands */
4595 if (is_Cmp(a) && is_Cmp(b)) {
4596 ir_node *a_left = get_Cmp_left(a);
4597 ir_node *a_right = get_Cmp_left(a);
4598 ir_node *b_left = get_Cmp_left(b);
4599 ir_node *b_right = get_Cmp_right(b);
4600 if (a_left == b_left && b_left == b_right) {
4601 dbg_info *dbgi = get_irn_dbg_info(n);
4602 ir_node *block = get_nodes_block(n);
4603 ir_relation a_relation = get_Cmp_relation(a);
4604 ir_relation b_relation = get_Cmp_relation(b);
4605 ir_relation new_relation = a_relation | b_relation;
4606 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4608 /* Cmp(a!=0) or Cmp(b!=0) => Cmp(a|b != 0) */
4609 if (is_cmp_unequal_zero(a) && is_cmp_unequal_zero(b)
4610 && !mode_is_float(get_irn_mode(a_left))
4611 && !mode_is_float(get_irn_mode(b_left))) {
4612 ir_graph *irg = get_irn_irg(n);
4613 dbg_info *dbgi = get_irn_dbg_info(n);
4614 ir_node *block = get_nodes_block(n);
4615 ir_mode *mode = get_irn_mode(a_left);
4616 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
4617 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
4618 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
4619 ir_node *zero = create_zero_const(irg, mode);
4620 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4624 mode = get_irn_mode(n);
4625 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4627 n = transform_node_Or_bf_store(n);
4628 n = transform_node_Or_Rotl(n);
4632 n = transform_bitwise_distributive(n, transform_node_Or);
4635 } /* transform_node_Or */
4639 static ir_node *transform_node(ir_node *n);
4642 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4644 * Should be moved to reassociation?
4646 static ir_node *transform_node_shift(ir_node *n)
4648 ir_node *left, *right;
4650 ir_tarval *tv1, *tv2, *res;
4651 ir_node *in[2], *irn, *block;
4655 left = get_binop_left(n);
4657 /* different operations */
4658 if (get_irn_op(left) != get_irn_op(n))
4661 right = get_binop_right(n);
4662 tv1 = value_of(right);
4663 if (tv1 == tarval_bad)
4666 tv2 = value_of(get_binop_right(left));
4667 if (tv2 == tarval_bad)
4670 mode = get_irn_mode(n);
4671 modulo_shf = get_mode_modulo_shift(mode);
4673 if (modulo_shf > 0) {
4674 ir_mode *sc_mode = get_tarval_mode(tv1);
4675 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, sc_mode);
4677 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4678 * above will be invalid) */
4679 assert(modulo_shf<=0 || is_po2(modulo_shf));
4681 tv1 = tarval_and(tv1, modulo_mask);
4682 tv2 = tarval_and(tv2, modulo_mask);
4684 res = tarval_add(tv1, tv2);
4685 irg = get_irn_irg(n);
4687 /* beware: a simple replacement works only, if res < modulo shift */
4689 if (modulo_shf > 0) {
4690 ir_tarval *modulo = new_tarval_from_long(modulo_shf,
4691 get_tarval_mode(res));
4693 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4695 /* shifting too much */
4696 if (!(tarval_cmp(res, modulo) & ir_relation_less)) {
4698 ir_node *block = get_nodes_block(n);
4699 dbg_info *dbgi = get_irn_dbg_info(n);
4700 ir_mode *smode = get_irn_mode(right);
4701 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4702 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4705 return new_r_Const(irg, get_mode_null(mode));
4709 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4712 /* ok, we can replace it */
4713 block = get_nodes_block(n);
4715 in[0] = get_binop_left(left);
4716 in[1] = new_r_Const(irg, res);
4718 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4720 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4722 return transform_node(irn);
4723 } /* transform_node_shift */
4726 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4728 * - and, or, xor instead of &
4729 * - Shl, Shr, Shrs, rotl instead of >>
4730 * (with a special case for Or/Xor + Shrs)
4732 static ir_node *transform_node_bitop_shift(ir_node *n)
4735 ir_node *right = get_binop_right(n);
4736 ir_mode *mode = get_irn_mode(n);
4737 ir_node *bitop_left;
4738 ir_node *bitop_right;
4748 ir_tarval *tv_shift;
4750 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4752 if (!is_Const(right))
4755 left = get_binop_left(n);
4756 op_left = get_irn_op(left);
4757 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4760 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4761 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4762 /* TODO: test if sign bit is affectes */
4766 bitop_right = get_binop_right(left);
4767 if (!is_Const(bitop_right))
4770 bitop_left = get_binop_left(left);
4772 block = get_nodes_block(n);
4773 dbgi = get_irn_dbg_info(n);
4774 tv1 = get_Const_tarval(bitop_right);
4775 tv2 = get_Const_tarval(right);
4777 assert(get_tarval_mode(tv1) == mode);
4780 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
4781 tv_shift = tarval_shl(tv1, tv2);
4782 } else if (is_Shr(n)) {
4783 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
4784 tv_shift = tarval_shr(tv1, tv2);
4785 } else if (is_Shrs(n)) {
4786 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
4787 tv_shift = tarval_shrs(tv1, tv2);
4790 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
4791 tv_shift = tarval_rotl(tv1, tv2);
4794 assert(get_tarval_mode(tv_shift) == mode);
4795 irg = get_irn_irg(n);
4796 new_const = new_r_Const(irg, tv_shift);
4798 if (op_left == op_And) {
4799 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
4800 } else if (op_left == op_Or) {
4801 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
4803 assert(op_left == op_Eor);
4804 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
4812 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4814 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4815 * (also with x >>s c1 when c1>=c2)
4817 static ir_node *transform_node_shl_shr(ir_node *n)
4820 ir_node *right = get_binop_right(n);
4830 ir_tarval *tv_shift;
4833 ir_relation relation;
4836 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4838 if (!is_Const(right))
4841 left = get_binop_left(n);
4842 mode = get_irn_mode(n);
4843 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4844 ir_node *shr_right = get_binop_right(left);
4846 if (!is_Const(shr_right))
4849 x = get_binop_left(left);
4850 tv_shr = get_Const_tarval(shr_right);
4851 tv_shl = get_Const_tarval(right);
4853 if (is_Shrs(left)) {
4854 /* shrs variant only allowed if c1 >= c2 */
4855 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
4858 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4861 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4863 tv_mask = tarval_shl(tv_mask, tv_shl);
4864 } else if (is_Shr(n) && is_Shl(left)) {
4865 ir_node *shl_right = get_Shl_right(left);
4867 if (!is_Const(shl_right))
4870 x = get_Shl_left(left);
4871 tv_shr = get_Const_tarval(right);
4872 tv_shl = get_Const_tarval(shl_right);
4874 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4875 tv_mask = tarval_shr(tv_mask, tv_shr);
4880 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
4881 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
4884 assert(tv_mask != tarval_bad);
4885 assert(get_tarval_mode(tv_mask) == mode);
4887 block = get_nodes_block(n);
4888 irg = get_irn_irg(block);
4889 dbgi = get_irn_dbg_info(n);
4891 relation = tarval_cmp(tv_shl, tv_shr);
4892 if (relation == ir_relation_less || relation == ir_relation_equal) {
4893 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4894 new_const = new_r_Const(irg, tv_shift);
4896 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
4898 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
4901 assert(relation == ir_relation_greater);
4902 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4903 new_const = new_r_Const(irg, tv_shift);
4904 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
4907 new_const = new_r_Const(irg, tv_mask);
4908 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
4913 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
4915 ir_mode *mode = get_tarval_mode(tv);
4916 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
4917 return tarval_mod(tv, modulo_tv);
4920 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
4921 ir_node *left, ir_node *right, ir_mode *mode);
4924 * Normalisation: if we have a shl/shr with modulo_shift behaviour
4925 * then we can use that to minimize the value of Add(x, const) or
4926 * Sub(Const, x). In particular this often avoids 1 instruction in some
4927 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
4928 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
4930 static ir_node *transform_node_shift_modulo(ir_node *n,
4931 new_shift_func new_shift)
4933 ir_mode *mode = get_irn_mode(n);
4934 int modulo = get_mode_modulo_shift(mode);
4935 ir_node *newop = NULL;
4936 ir_mode *mode_right;
4943 if (get_mode_arithmetic(mode) != irma_twos_complement)
4945 if (!is_po2(modulo))
4948 irg = get_irn_irg(n);
4949 block = get_nodes_block(n);
4950 right = get_binop_right(n);
4951 mode_right = get_irn_mode(right);
4952 if (is_Const(right)) {
4953 ir_tarval *tv = get_Const_tarval(right);
4954 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
4959 newop = new_r_Const(irg, tv_mod);
4960 } else if (is_Add(right)) {
4961 ir_node *add_right = get_Add_right(right);
4962 if (is_Const(add_right)) {
4963 ir_tarval *tv = get_Const_tarval(add_right);
4964 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
4969 newconst = new_r_Const(irg, tv_mod);
4970 newop = new_r_Add(block, get_Add_left(right), newconst,
4973 } else if (is_Sub(right)) {
4974 ir_node *sub_left = get_Sub_left(right);
4975 if (is_Const(sub_left)) {
4976 ir_tarval *tv = get_Const_tarval(sub_left);
4977 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
4982 newconst = new_r_Const(irg, tv_mod);
4983 newop = new_r_Sub(block, newconst, get_Sub_right(right),
4990 if (newop != NULL) {
4991 dbg_info *dbgi = get_irn_dbg_info(n);
4992 ir_node *left = get_binop_left(n);
4993 return new_shift(dbgi, block, left, newop, mode);
5001 static ir_node *transform_node_Shr(ir_node *n)
5003 ir_node *c, *oldn = n;
5004 ir_node *left = get_Shr_left(n);
5005 ir_node *right = get_Shr_right(n);
5006 ir_mode *mode = get_irn_mode(n);
5008 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5009 n = transform_node_shift(n);
5012 n = transform_node_shift_modulo(n, new_rd_Shr);
5014 n = transform_node_shl_shr(n);
5016 n = transform_node_bitop_shift(n);
5019 } /* transform_node_Shr */
5024 static ir_node *transform_node_Shrs(ir_node *n)
5026 ir_node *c, *oldn = n;
5027 ir_node *a = get_Shrs_left(n);
5028 ir_node *b = get_Shrs_right(n);
5029 ir_mode *mode = get_irn_mode(n);
5031 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5032 n = transform_node_shift(n);
5035 n = transform_node_shift_modulo(n, new_rd_Shrs);
5037 n = transform_node_bitop_shift(n);
5040 } /* transform_node_Shrs */
5045 static ir_node *transform_node_Shl(ir_node *n)
5047 ir_node *c, *oldn = n;
5048 ir_node *a = get_Shl_left(n);
5049 ir_node *b = get_Shl_right(n);
5050 ir_mode *mode = get_irn_mode(n);
5052 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5053 n = transform_node_shift(n);
5056 n = transform_node_shift_modulo(n, new_rd_Shl);
5058 n = transform_node_shl_shr(n);
5060 n = transform_node_bitop_shift(n);
5063 } /* transform_node_Shl */
5068 static ir_node *transform_node_Rotl(ir_node *n)
5070 ir_node *c, *oldn = n;
5071 ir_node *a = get_Rotl_left(n);
5072 ir_node *b = get_Rotl_right(n);
5073 ir_mode *mode = get_irn_mode(n);
5075 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5076 n = transform_node_shift(n);
5079 n = transform_node_bitop_shift(n);
5082 } /* transform_node_Rotl */
5087 static ir_node *transform_node_Conv(ir_node *n)
5089 ir_node *c, *oldn = n;
5090 ir_mode *mode = get_irn_mode(n);
5091 ir_node *a = get_Conv_op(n);
5093 if (mode != mode_b && is_const_Phi(a)) {
5094 /* Do NOT optimize mode_b Conv's, this leads to remaining
5095 * Phib nodes later, because the conv_b_lower operation
5096 * is instantly reverted, when it tries to insert a Convb.
5098 c = apply_conv_on_phi(a, mode);
5100 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5105 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5106 ir_graph *irg = get_irn_irg(n);
5107 return new_r_Unknown(irg, mode);
5110 if (mode_is_reference(mode) &&
5111 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5113 ir_node *l = get_Add_left(a);
5114 ir_node *r = get_Add_right(a);
5115 dbg_info *dbgi = get_irn_dbg_info(a);
5116 ir_node *block = get_nodes_block(n);
5118 ir_node *lop = get_Conv_op(l);
5119 if (get_irn_mode(lop) == mode) {
5120 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5121 n = new_rd_Add(dbgi, block, lop, r, mode);
5126 ir_node *rop = get_Conv_op(r);
5127 if (get_irn_mode(rop) == mode) {
5128 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5129 n = new_rd_Add(dbgi, block, l, rop, mode);
5136 } /* transform_node_Conv */
5139 * Remove dead blocks and nodes in dead blocks
5140 * in keep alive list. We do not generate a new End node.
5142 static ir_node *transform_node_End(ir_node *n)
5144 int i, j, n_keepalives = get_End_n_keepalives(n);
5147 NEW_ARR_A(ir_node *, in, n_keepalives);
5149 for (i = j = 0; i < n_keepalives; ++i) {
5150 ir_node *ka = get_End_keepalive(n, i);
5152 /* no need to keep Bad */
5155 /* dont keep unreachable code */
5156 block = is_Block(ka) ? ka : get_nodes_block(ka);
5157 if (is_block_unreachable(block))
5161 if (j != n_keepalives)
5162 set_End_keepalives(n, j, in);
5164 } /* transform_node_End */
5166 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5168 if (is_Minus(a) && get_Minus_op(a) == b)
5170 if (is_Minus(b) && get_Minus_op(b) == a)
5172 if (is_Sub(a) && is_Sub(b)) {
5173 ir_node *a_left = get_Sub_left(a);
5174 ir_node *a_right = get_Sub_right(a);
5175 ir_node *b_left = get_Sub_left(b);
5176 ir_node *b_right = get_Sub_right(b);
5178 if (a_left == b_right && a_right == b_left)
5186 * Optimize a Mux into some simpler cases.
5188 static ir_node *transform_node_Mux(ir_node *n)
5190 ir_node *oldn = n, *sel = get_Mux_sel(n);
5191 ir_mode *mode = get_irn_mode(n);
5192 ir_node *t = get_Mux_true(n);
5193 ir_node *f = get_Mux_false(n);
5194 ir_graph *irg = get_irn_irg(n);
5196 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5200 ir_node* block = get_nodes_block(n);
5202 ir_node* c1 = get_Mux_sel(t);
5203 ir_node* t1 = get_Mux_true(t);
5204 ir_node* f1 = get_Mux_false(t);
5206 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5207 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5208 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5213 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5214 } else if (f == t1) {
5215 /* Mux(cond0, Mux(cond1, x, y), x) */
5216 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5217 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5218 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5223 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5225 } else if (is_Mux(f)) {
5226 ir_node* block = get_nodes_block(n);
5228 ir_node* c1 = get_Mux_sel(f);
5229 ir_node* t1 = get_Mux_true(f);
5230 ir_node* f1 = get_Mux_false(f);
5232 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5233 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5234 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5239 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5240 } else if (t == f1) {
5241 /* Mux(cond0, x, Mux(cond1, y, x)) */
5242 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5243 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5244 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5249 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5253 /* first normalization step: try to move a constant to the false side,
5254 * 0 preferred on false side too */
5255 if (is_Cmp(sel) && is_Const(t) &&
5256 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5257 dbg_info *seldbgi = get_irn_dbg_info(sel);
5258 ir_node *block = get_nodes_block(sel);
5259 ir_relation relation = get_Cmp_relation(sel);
5264 /* Mux(x, a, b) => Mux(not(x), b, a) */
5265 relation = get_negated_relation(relation);
5266 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5267 get_Cmp_right(sel), relation);
5268 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5271 /* note: after normalization, false can only happen on default */
5272 if (mode == mode_b) {
5273 dbg_info *dbg = get_irn_dbg_info(n);
5274 ir_node *block = get_nodes_block(n);
5277 ir_tarval *tv_t = get_Const_tarval(t);
5278 if (tv_t == tarval_b_true) {
5280 /* Muxb(sel, true, false) = sel */
5281 assert(get_Const_tarval(f) == tarval_b_false);
5282 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5285 /* Muxb(sel, true, x) = Or(sel, x) */
5286 n = new_rd_Or(dbg, block, sel, f, mode_b);
5287 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5291 } else if (is_Const(f)) {
5292 ir_tarval *tv_f = get_Const_tarval(f);
5293 if (tv_f == tarval_b_true) {
5294 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5295 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5296 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5297 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5300 /* Muxb(sel, x, false) = And(sel, x) */
5301 assert(tv_f == tarval_b_false);
5302 n = new_rd_And(dbg, block, sel, t, mode_b);
5303 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5309 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5310 * value to integer. */
5311 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5312 ir_tarval *a = get_Const_tarval(t);
5313 ir_tarval *b = get_Const_tarval(f);
5315 if (tarval_is_one(a) && tarval_is_null(b)) {
5316 ir_node *block = get_nodes_block(n);
5317 ir_node *conv = new_r_Conv(block, sel, mode);
5319 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5321 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5322 ir_node *block = get_nodes_block(n);
5323 ir_node *not_ = new_r_Not(block, sel, mode_b);
5324 ir_node *conv = new_r_Conv(block, not_, mode);
5326 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5332 ir_node *cmp_r = get_Cmp_right(sel);
5333 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5334 ir_node *block = get_nodes_block(n);
5335 ir_node *cmp_l = get_Cmp_left(sel);
5337 if (mode_is_int(mode)) {
5338 ir_relation relation = get_Cmp_relation(sel);
5340 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5341 /* Mux((a & b) != 0, c, 0) */
5342 ir_node *and_r = get_And_right(cmp_l);
5345 if (and_r == t && f == cmp_r) {
5346 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5347 if (relation == ir_relation_less_greater) {
5348 /* Mux((a & 2^C) != 0, 2^C, 0) */
5350 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5352 /* Mux((a & 2^C) == 0, 2^C, 0) */
5353 n = new_rd_Eor(get_irn_dbg_info(n),
5354 block, cmp_l, t, mode);
5355 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5360 if (is_Shl(and_r)) {
5361 ir_node *shl_l = get_Shl_left(and_r);
5362 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5363 if (and_r == t && f == cmp_r) {
5364 if (relation == ir_relation_less_greater) {
5365 /* (a & (1 << n)) != 0, (1 << n), 0) */
5367 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5369 /* (a & (1 << n)) == 0, (1 << n), 0) */
5370 n = new_rd_Eor(get_irn_dbg_info(n),
5371 block, cmp_l, t, mode);
5372 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5378 and_l = get_And_left(cmp_l);
5379 if (is_Shl(and_l)) {
5380 ir_node *shl_l = get_Shl_left(and_l);
5381 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5382 if (and_l == t && f == cmp_r) {
5383 if (relation == ir_relation_less_greater) {
5384 /* ((1 << n) & a) != 0, (1 << n), 0) */
5386 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5388 /* ((1 << n) & a) == 0, (1 << n), 0) */
5389 n = new_rd_Eor(get_irn_dbg_info(n),
5390 block, cmp_l, t, mode);
5391 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5406 * optimize Sync nodes that have other syncs as input we simply add the inputs
5407 * of the other sync to our own inputs
5409 static ir_node *transform_node_Sync(ir_node *n)
5411 int arity = get_Sync_n_preds(n);
5414 for (i = 0; i < arity;) {
5415 ir_node *pred = get_Sync_pred(n, i);
5419 /* Remove Bad predecessors */
5426 /* Remove duplicate predecessors */
5427 for (j = 0; j < i; ++j) {
5428 if (get_Sync_pred(n, j) == pred) {
5437 if (!is_Sync(pred)) {
5445 pred_arity = get_Sync_n_preds(pred);
5446 for (j = 0; j < pred_arity; ++j) {
5447 ir_node *pred_pred = get_Sync_pred(pred, j);
5452 add_irn_n(n, pred_pred);
5456 if (get_Sync_pred(n, k) == pred_pred) break;
5462 ir_graph *irg = get_irn_irg(n);
5463 return new_r_Bad(irg, mode_M);
5466 return get_Sync_pred(n, 0);
5469 /* rehash the sync node */
5474 static ir_node *transform_node_Load(ir_node *n)
5476 /* if our memory predecessor is a load from the same address, then reuse the
5477 * previous result */
5478 ir_node *mem = get_Load_mem(n);
5483 /* don't touch volatile loads */
5484 if (get_Load_volatility(n) == volatility_is_volatile)
5486 mem_pred = get_Proj_pred(mem);
5487 if (is_Load(mem_pred)) {
5488 ir_node *pred_load = mem_pred;
5490 /* conservatively compare the 2 loads. TODO: This could be less strict
5491 * with fixup code in some situations (like smaller/bigger modes) */
5492 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5494 if (get_Load_mode(pred_load) != get_Load_mode(n))
5496 /* all combinations of aligned/unaligned pred/n should be fine so we do
5497 * not compare the unaligned attribute */
5499 ir_node *block = get_nodes_block(n);
5500 ir_node *jmp = new_r_Jmp(block);
5501 ir_graph *irg = get_irn_irg(n);
5502 ir_node *bad = new_r_Bad(irg, mode_X);
5503 ir_mode *mode = get_Load_mode(n);
5504 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5505 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5506 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5509 } else if (is_Store(mem_pred)) {
5510 ir_node *pred_store = mem_pred;
5511 ir_node *value = get_Store_value(pred_store);
5513 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5515 if (get_irn_mode(value) != get_Load_mode(n))
5517 /* all combinations of aligned/unaligned pred/n should be fine so we do
5518 * not compare the unaligned attribute */
5520 ir_node *block = get_nodes_block(n);
5521 ir_node *jmp = new_r_Jmp(block);
5522 ir_graph *irg = get_irn_irg(n);
5523 ir_node *bad = new_r_Bad(irg, mode_X);
5524 ir_node *res = value;
5525 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5526 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5535 * optimize a trampoline Call into a direct Call
5537 static ir_node *transform_node_Call(ir_node *call)
5539 ir_node *callee = get_Call_ptr(call);
5540 ir_node *adr, *mem, *res, *bl, **in;
5541 ir_type *ctp, *mtp, *tp;
5545 size_t i, n_res, n_param;
5548 if (! is_Proj(callee))
5550 callee = get_Proj_pred(callee);
5551 if (! is_Builtin(callee))
5553 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5556 mem = get_Call_mem(call);
5558 if (skip_Proj(mem) == callee) {
5559 /* memory is routed to the trampoline, skip */
5560 mem = get_Builtin_mem(callee);
5563 /* build a new call type */
5564 mtp = get_Call_type(call);
5565 tdb = get_type_dbg_info(mtp);
5567 n_res = get_method_n_ress(mtp);
5568 n_param = get_method_n_params(mtp);
5569 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5571 for (i = 0; i < n_res; ++i)
5572 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5574 NEW_ARR_A(ir_node *, in, n_param + 1);
5576 /* FIXME: we don't need a new pointer type in every step */
5577 irg = get_irn_irg(call);
5578 tp = get_irg_frame_type(irg);
5579 tp = new_type_pointer(tp);
5580 set_method_param_type(ctp, 0, tp);
5582 in[0] = get_Builtin_param(callee, 2);
5583 for (i = 0; i < n_param; ++i) {
5584 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5585 in[i + 1] = get_Call_param(call, i);
5587 var = get_method_variadicity(mtp);
5588 set_method_variadicity(ctp, var);
5589 /* When we resolve a trampoline, the function must be called by a this-call */
5590 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5591 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5593 adr = get_Builtin_param(callee, 1);
5595 db = get_irn_dbg_info(call);
5596 bl = get_nodes_block(call);
5598 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5599 if (get_irn_pinned(call) == op_pin_state_floats)
5600 set_irn_pinned(res, op_pin_state_floats);
5602 } /* transform_node_Call */
5605 * Tries several [inplace] [optimizing] transformations and returns an
5606 * equivalent node. The difference to equivalent_node() is that these
5607 * transformations _do_ generate new nodes, and thus the old node must
5608 * not be freed even if the equivalent node isn't the old one.
5610 static ir_node *transform_node(ir_node *n)
5615 * Transform_node is the only "optimizing transformation" that might
5616 * return a node with a different opcode. We iterate HERE until fixpoint
5617 * to get the final result.
5621 if (n->op->ops.transform_node != NULL)
5622 n = n->op->ops.transform_node(n);
5623 } while (oldn != n);
5626 } /* transform_node */
5629 * Sets the default transform node operation for an ir_op_ops.
5631 * @param code the opcode for the default operation
5632 * @param ops the operations initialized
5637 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5641 ops->transform_node = transform_node_##a; \
5643 #define CASE_PROJ(a) \
5645 ops->transform_node_Proj = transform_node_Proj_##a; \
5647 #define CASE_PROJ_EX(a) \
5649 ops->transform_node = transform_node_##a; \
5650 ops->transform_node_Proj = transform_node_Proj_##a; \
5691 } /* firm_set_default_transform_node */
5694 /* **************** Common Subexpression Elimination **************** */
5696 /** The size of the hash table used, should estimate the number of nodes
5698 #define N_IR_NODES 512
5700 /** Compares the attributes of two Const nodes. */
5701 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
5703 return get_Const_tarval(a) != get_Const_tarval(b);
5706 /** Compares the attributes of two Proj nodes. */
5707 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
5709 return a->attr.proj.proj != b->attr.proj.proj;
5712 /** Compares the attributes of two Alloc nodes. */
5713 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
5715 const alloc_attr *pa = &a->attr.alloc;
5716 const alloc_attr *pb = &b->attr.alloc;
5717 return (pa->where != pb->where) || (pa->type != pb->type);
5720 /** Compares the attributes of two Free nodes. */
5721 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
5723 const free_attr *pa = &a->attr.free;
5724 const free_attr *pb = &b->attr.free;
5725 return (pa->where != pb->where) || (pa->type != pb->type);
5728 /** Compares the attributes of two SymConst nodes. */
5729 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
5731 const symconst_attr *pa = &a->attr.symc;
5732 const symconst_attr *pb = &b->attr.symc;
5733 return (pa->kind != pb->kind)
5734 || (pa->sym.type_p != pb->sym.type_p);
5737 /** Compares the attributes of two Call nodes. */
5738 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
5740 const call_attr *pa = &a->attr.call;
5741 const call_attr *pb = &b->attr.call;
5742 return (pa->type != pb->type)
5743 || (pa->tail_call != pb->tail_call);
5746 /** Compares the attributes of two Sel nodes. */
5747 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
5749 const ir_entity *a_ent = get_Sel_entity(a);
5750 const ir_entity *b_ent = get_Sel_entity(b);
5751 return a_ent != b_ent;
5754 /** Compares the attributes of two Phi nodes. */
5755 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
5757 /* we can only enter this function if both nodes have the same number of inputs,
5758 hence it is enough to check if one of them is a Phi0 */
5760 /* check the Phi0 pos attribute */
5761 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5766 /** Compares the attributes of two Conv nodes. */
5767 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
5769 return get_Conv_strict(a) != get_Conv_strict(b);
5772 /** Compares the attributes of two Cast nodes. */
5773 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
5775 return get_Cast_type(a) != get_Cast_type(b);
5778 /** Compares the attributes of two Load nodes. */
5779 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
5781 if (get_Load_volatility(a) == volatility_is_volatile ||
5782 get_Load_volatility(b) == volatility_is_volatile)
5783 /* NEVER do CSE on volatile Loads */
5785 /* do not CSE Loads with different alignment. Be conservative. */
5786 if (get_Load_unaligned(a) != get_Load_unaligned(b))
5789 return get_Load_mode(a) != get_Load_mode(b);
5792 /** Compares the attributes of two Store nodes. */
5793 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
5795 /* do not CSE Stores with different alignment. Be conservative. */
5796 if (get_Store_unaligned(a) != get_Store_unaligned(b))
5799 /* NEVER do CSE on volatile Stores */
5800 return (get_Store_volatility(a) == volatility_is_volatile ||
5801 get_Store_volatility(b) == volatility_is_volatile);
5804 /** Compares two exception attributes */
5805 static int node_cmp_exception(const ir_node *a, const ir_node *b)
5807 const except_attr *ea = &a->attr.except;
5808 const except_attr *eb = &b->attr.except;
5810 return ea->pin_state != eb->pin_state;
5813 #define node_cmp_attr_Bound node_cmp_exception
5815 /** Compares the attributes of two Div nodes. */
5816 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
5818 const div_attr *ma = &a->attr.div;
5819 const div_attr *mb = &b->attr.div;
5820 return ma->exc.pin_state != mb->exc.pin_state ||
5821 ma->resmode != mb->resmode ||
5822 ma->no_remainder != mb->no_remainder;
5825 /** Compares the attributes of two Mod nodes. */
5826 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
5828 const mod_attr *ma = &a->attr.mod;
5829 const mod_attr *mb = &b->attr.mod;
5830 return ma->exc.pin_state != mb->exc.pin_state ||
5831 ma->resmode != mb->resmode;
5834 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
5836 const cmp_attr *ma = &a->attr.cmp;
5837 const cmp_attr *mb = &b->attr.cmp;
5838 return ma->relation != mb->relation;
5841 /** Compares the attributes of two Confirm nodes. */
5842 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
5844 const confirm_attr *ma = &a->attr.confirm;
5845 const confirm_attr *mb = &b->attr.confirm;
5846 return ma->relation != mb->relation;
5849 /** Compares the attributes of two Builtin nodes. */
5850 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
5852 /* no need to compare the type, equal kind means equal type */
5853 return get_Builtin_kind(a) != get_Builtin_kind(b);
5856 /** Compares the attributes of two ASM nodes. */
5857 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
5860 const ir_asm_constraint *ca;
5861 const ir_asm_constraint *cb;
5864 if (get_ASM_text(a) != get_ASM_text(b))
5867 /* Should we really check the constraints here? Should be better, but is strange. */
5868 n = get_ASM_n_input_constraints(a);
5869 if (n != get_ASM_n_input_constraints(b))
5872 ca = get_ASM_input_constraints(a);
5873 cb = get_ASM_input_constraints(b);
5874 for (i = 0; i < n; ++i) {
5875 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5876 || ca[i].mode != cb[i].mode)
5880 n = get_ASM_n_output_constraints(a);
5881 if (n != get_ASM_n_output_constraints(b))
5884 ca = get_ASM_output_constraints(a);
5885 cb = get_ASM_output_constraints(b);
5886 for (i = 0; i < n; ++i) {
5887 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5888 || ca[i].mode != cb[i].mode)
5892 n = get_ASM_n_clobbers(a);
5893 if (n != get_ASM_n_clobbers(b))
5896 cla = get_ASM_clobbers(a);
5897 clb = get_ASM_clobbers(b);
5898 for (i = 0; i < n; ++i) {
5899 if (cla[i] != clb[i])
5905 /** Compares the inexistent attributes of two Dummy nodes. */
5906 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
5914 * Set the default node attribute compare operation for an ir_op_ops.
5916 * @param code the opcode for the default operation
5917 * @param ops the operations initialized
5922 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5926 ops->node_cmp_attr = node_cmp_attr_##a; \
5958 } /* firm_set_default_node_cmp_attr */
5961 * Compare function for two nodes in the value table. Gets two
5962 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5964 int identities_cmp(const void *elt, const void *key)
5966 ir_node *a = (ir_node *)elt;
5967 ir_node *b = (ir_node *)key;
5970 if (a == b) return 0;
5972 if ((get_irn_op(a) != get_irn_op(b)) ||
5973 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5975 /* compare if a's in and b's in are of equal length */
5976 irn_arity_a = get_irn_arity(a);
5977 if (irn_arity_a != get_irn_arity(b))
5980 /* blocks are never the same */
5984 if (get_irn_pinned(a) == op_pin_state_pinned) {
5985 /* for pinned nodes, the block inputs must be equal */
5986 if (get_irn_n(a, -1) != get_irn_n(b, -1))
5988 } else if (! get_opt_global_cse()) {
5989 /* for block-local CSE both nodes must be in the same Block */
5990 if (get_nodes_block(a) != get_nodes_block(b))
5994 /* compare a->in[0..ins] with b->in[0..ins] */
5995 for (i = 0; i < irn_arity_a; ++i) {
5996 ir_node *pred_a = get_irn_n(a, i);
5997 ir_node *pred_b = get_irn_n(b, i);
5998 if (pred_a != pred_b) {
5999 /* if both predecessors are CSE neutral they might be different */
6000 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6006 * here, we already now that the nodes are identical except their
6009 if (a->op->ops.node_cmp_attr)
6010 return a->op->ops.node_cmp_attr(a, b);
6013 } /* identities_cmp */
6016 * Calculate a hash value of a node.
6018 * @param node The IR-node
6020 unsigned ir_node_hash(const ir_node *node)
6022 return node->op->ops.hash(node);
6023 } /* ir_node_hash */
6026 void new_identities(ir_graph *irg)
6028 if (irg->value_table != NULL)
6029 del_pset(irg->value_table);
6030 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6031 } /* new_identities */
6033 void del_identities(ir_graph *irg)
6035 if (irg->value_table != NULL)
6036 del_pset(irg->value_table);
6037 } /* del_identities */
6039 /* Normalize a node by putting constants (and operands with larger
6040 * node index) on the right (operator side). */
6041 void ir_normalize_node(ir_node *n)
6043 if (is_op_commutative(get_irn_op(n))) {
6044 ir_node *l = get_binop_left(n);
6045 ir_node *r = get_binop_right(n);
6047 /* For commutative operators perform a OP b == b OP a but keep
6048 * constants on the RIGHT side. This helps greatly in some
6049 * optimizations. Moreover we use the idx number to make the form
6051 if (!operands_are_normalized(l, r)) {
6052 set_binop_left(n, r);
6053 set_binop_right(n, l);
6057 } /* ir_normalize_node */
6060 * Return the canonical node computing the same value as n.
6061 * Looks up the node in a hash table, enters it in the table
6062 * if it isn't there yet.
6064 * @param n the node to look up
6066 * @return a node that computes the same value as n or n if no such
6067 * node could be found
6069 ir_node *identify_remember(ir_node *n)
6071 ir_graph *irg = get_irn_irg(n);
6072 pset *value_table = irg->value_table;
6075 if (value_table == NULL)
6078 ir_normalize_node(n);
6079 /* lookup or insert in hash table with given hash key. */
6080 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6083 /* n is reachable again */
6084 edges_node_revival(nn);
6088 } /* identify_remember */
6091 * During construction we set the op_pin_state_pinned flag in the graph right
6092 * when the optimization is performed. The flag turning on procedure global
6093 * cse could be changed between two allocations. This way we are safe.
6095 * @param n The node to lookup
6097 static inline ir_node *identify_cons(ir_node *n)
6101 n = identify_remember(n);
6102 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6103 ir_graph *irg = get_irn_irg(n);
6104 set_irg_pinned(irg, op_pin_state_floats);
6107 } /* identify_cons */
6109 /* Add a node to the identities value table. */
6110 void add_identities(ir_node *node)
6117 identify_remember(node);
6120 /* Visit each node in the value table of a graph. */
6121 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6124 ir_graph *rem = current_ir_graph;
6126 current_ir_graph = irg;
6127 foreach_pset(irg->value_table, ir_node*, node) {
6130 current_ir_graph = rem;
6131 } /* visit_all_identities */
6134 * These optimizations deallocate nodes from the obstack.
6135 * It can only be called if it is guaranteed that no other nodes
6136 * reference this one, i.e., right after construction of a node.
6138 * @param n The node to optimize
6140 ir_node *optimize_node(ir_node *n)
6143 ir_graph *irg = get_irn_irg(n);
6144 unsigned iro = get_irn_opcode(n);
6147 /* Always optimize Phi nodes: part of the construction. */
6148 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6150 /* constant expression evaluation / constant folding */
6151 if (get_opt_constant_folding()) {
6152 /* neither constants nor Tuple values can be evaluated */
6153 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6154 /* try to evaluate */
6155 tv = computed_value(n);
6156 if (tv != tarval_bad) {
6161 * we MUST copy the node here temporarily, because it's still
6162 * needed for DBG_OPT_CSTEVAL
6164 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6165 oldn = (ir_node*)alloca(node_size);
6167 memcpy(oldn, n, node_size);
6168 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6170 /* ARG, copy the in array, we need it for statistics */
6171 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6173 /* note the inplace edges module */
6174 edges_node_deleted(n);
6176 /* evaluation was successful -- replace the node. */
6177 irg_kill_node(irg, n);
6178 nw = new_r_Const(irg, tv);
6180 DBG_OPT_CSTEVAL(oldn, nw);
6186 /* remove unnecessary nodes */
6187 if (get_opt_algebraic_simplification() ||
6188 (iro == iro_Phi) || /* always optimize these nodes. */
6190 (iro == iro_Proj) ||
6191 (iro == iro_Block) ) /* Flags tested local. */
6192 n = equivalent_node(n);
6194 /* Common Subexpression Elimination.
6196 * Checks whether n is already available.
6197 * The block input is used to distinguish different subexpressions. Right
6198 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6199 * subexpressions within a block.
6202 n = identify_cons(n);
6205 edges_node_deleted(oldn);
6207 /* We found an existing, better node, so we can deallocate the old node. */
6208 irg_kill_node(irg, oldn);
6212 /* Some more constant expression evaluation that does not allow to
6214 iro = get_irn_opcode(n);
6215 if (get_opt_algebraic_simplification() ||
6216 (iro == iro_Cond) ||
6217 (iro == iro_Proj)) /* Flags tested local. */
6218 n = transform_node(n);
6220 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6221 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6223 n = identify_remember(o);
6229 } /* optimize_node */
6233 * These optimizations never deallocate nodes (in place). This can cause dead
6234 * nodes lying on the obstack. Remove these by a dead node elimination,
6235 * i.e., a copying garbage collection.
6237 ir_node *optimize_in_place_2(ir_node *n)
6241 unsigned iro = get_irn_opcode(n);
6243 if (!get_opt_optimize() && !is_Phi(n)) return n;
6245 if (iro == iro_Deleted)
6248 /* constant expression evaluation / constant folding */
6249 if (get_opt_constant_folding()) {
6250 /* neither constants nor Tuple values can be evaluated */
6251 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6252 /* try to evaluate */
6253 tv = computed_value(n);
6254 if (tv != tarval_bad) {
6255 /* evaluation was successful -- replace the node. */
6256 ir_graph *irg = get_irn_irg(n);
6258 n = new_r_Const(irg, tv);
6260 DBG_OPT_CSTEVAL(oldn, n);
6266 /* remove unnecessary nodes */
6267 if (get_opt_constant_folding() ||
6268 (iro == iro_Phi) || /* always optimize these nodes. */
6269 (iro == iro_Id) || /* ... */
6270 (iro == iro_Proj) || /* ... */
6271 (iro == iro_Block) ) /* Flags tested local. */
6272 n = equivalent_node(n);
6274 /** common subexpression elimination **/
6275 /* Checks whether n is already available. */
6276 /* The block input is used to distinguish different subexpressions. Right
6277 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6278 subexpressions within a block. */
6279 if (get_opt_cse()) {
6281 n = identify_remember(o);
6286 /* Some more constant expression evaluation. */
6287 iro = get_irn_opcode(n);
6288 if (get_opt_constant_folding() ||
6289 (iro == iro_Cond) ||
6290 (iro == iro_Proj)) /* Flags tested local. */
6291 n = transform_node(n);
6293 /* Now we can verify the node, as it has no dead inputs any more. */
6296 /* Now we have a legal, useful node. Enter it in hash table for cse.
6297 Blocks should be unique anyways. (Except the successor of start:
6298 is cse with the start block!) */
6299 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6301 n = identify_remember(o);
6307 } /* optimize_in_place_2 */
6310 * Wrapper for external use, set proper status bits after optimization.
6312 ir_node *optimize_in_place(ir_node *n)
6314 ir_graph *irg = get_irn_irg(n);
6315 /* Handle graph state */
6316 assert(get_irg_phase_state(irg) != phase_building);
6318 if (get_opt_global_cse())
6319 set_irg_pinned(irg, op_pin_state_floats);
6321 /* FIXME: Maybe we could also test whether optimizing the node can
6322 change the control graph. */
6323 set_irg_doms_inconsistent(irg);
6324 return optimize_in_place_2(n);
6325 } /* optimize_in_place */
6328 * Calculate a hash value of a Const node.
6330 static unsigned hash_Const(const ir_node *node)
6334 /* special value for const, as they only differ in their tarval. */
6335 h = HASH_PTR(node->attr.con.tarval);
6341 * Calculate a hash value of a SymConst node.
6343 static unsigned hash_SymConst(const ir_node *node)
6347 /* all others are pointers */
6348 h = HASH_PTR(node->attr.symc.sym.type_p);
6351 } /* hash_SymConst */
6354 * Set the default hash operation in an ir_op_ops.
6356 * @param code the opcode for the default operation
6357 * @param ops the operations initialized
6362 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6366 ops->hash = hash_##a; \
6369 /* hash function already set */
6370 if (ops->hash != NULL)
6377 /* use input/mode default hash if no function was given */
6378 ops->hash = firm_default_hash;
6386 * Sets the default operation for an ir_ops.
6388 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6390 ops = firm_set_default_hash(code, ops);
6391 ops = firm_set_default_computed_value(code, ops);
6392 ops = firm_set_default_equivalent_node(code, ops);
6393 ops = firm_set_default_transform_node(code, ops);
6394 ops = firm_set_default_node_cmp_attr(code, ops);
6395 ops = firm_set_default_get_type_attr(code, ops);
6396 ops = firm_set_default_get_entity_attr(code, ops);
6399 } /* firm_set_default_operations */