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 * Returns a equivalent block for another block.
666 * If the block has only one predecessor, this is
667 * the equivalent one. If the only predecessor of a block is
668 * the block itself, this is a dead block.
670 * If both predecessors of a block are the branches of a binary
671 * Cond, the equivalent block is Cond's block.
673 * If all predecessors of a block are bad or lies in a dead
674 * block, the current block is dead as well.
676 static ir_node *equivalent_node_Block(ir_node *n)
682 /* don't optimize labeled blocks */
683 if (has_Block_entity(n))
685 if (!get_Block_matured(n))
688 n_preds = get_Block_n_cfgpreds(n);
690 irg = get_irn_irg(n);
692 /* Straightening: a single entry Block following a single exit Block
695 ir_node *pred = get_Block_cfgpred(n, 0);
698 ir_node *pred_block = get_nodes_block(pred);
699 DBG_OPT_STG(n, pred_block);
702 } else if (n_preds == 2) {
703 /* Test whether Cond jumps twice to this block
704 * The more general case which more than 2 predecessors is handles
705 * in optimize_cf(), we handle only this special case for speed here.
707 ir_node *a = get_Block_cfgpred(n, 0);
708 ir_node *b = get_Block_cfgpred(n, 1);
710 if (is_Proj(a) && is_Proj(b)) {
711 ir_node *cond = get_Proj_pred(a);
713 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
714 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
715 /* Also a single entry Block following a single exit Block.
716 * Phis have twice the same operand and will be optimized away.
718 n = get_nodes_block(cond);
719 DBG_OPT_IFSIM1(oldn, a, b, n);
725 } /* equivalent_node_Block */
727 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
728 See transform_node_Proj_Cond(). */
731 * Optimize operations that are commutative and have neutral 0,
732 * so a op 0 = 0 op a = a.
734 static ir_node *equivalent_node_neutral_zero(ir_node *n)
738 ir_node *a = get_binop_left(n);
739 ir_node *b = get_binop_right(n);
744 /* After running compute_node there is only one constant predecessor.
745 Find this predecessors value and remember the other node: */
746 if ((tv = value_of(a)) != tarval_bad) {
748 } else if ((tv = value_of(b)) != tarval_bad) {
753 /* If this predecessors constant value is zero, the operation is
754 * unnecessary. Remove it.
756 * Beware: If n is a Add, the mode of on and n might be different
757 * which happens in this rare construction: NULL + 3.
758 * Then, a Conv would be needed which we cannot include here.
760 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
763 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
767 } /* equivalent_node_neutral_zero */
770 * Eor is commutative and has neutral 0.
772 static ir_node *equivalent_node_Eor(ir_node *n)
778 n = equivalent_node_neutral_zero(n);
779 if (n != oldn) return n;
782 b = get_Eor_right(n);
785 ir_node *aa = get_Eor_left(a);
786 ir_node *ab = get_Eor_right(a);
789 /* (a ^ b) ^ a -> b */
791 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
793 } else if (ab == b) {
794 /* (a ^ b) ^ b -> a */
796 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
801 ir_node *ba = get_Eor_left(b);
802 ir_node *bb = get_Eor_right(b);
805 /* a ^ (a ^ b) -> b */
807 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
809 } else if (bb == a) {
810 /* a ^ (b ^ a) -> b */
812 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
820 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
822 * The second one looks strange, but this construct
823 * is used heavily in the LCC sources :-).
825 * Beware: The Mode of an Add may be different than the mode of its
826 * predecessors, so we could not return a predecessors in all cases.
828 static ir_node *equivalent_node_Add(ir_node *n)
831 ir_node *left, *right;
832 ir_mode *mode = get_irn_mode(n);
834 n = equivalent_node_neutral_zero(n);
838 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
839 if (mode_is_float(mode)) {
840 ir_graph *irg = get_irn_irg(n);
841 if (get_irg_fp_model(irg) & fp_strict_algebraic)
845 left = get_Add_left(n);
846 right = get_Add_right(n);
849 if (get_Sub_right(left) == right) {
852 n = get_Sub_left(left);
853 if (mode == get_irn_mode(n)) {
854 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
860 if (get_Sub_right(right) == left) {
863 n = get_Sub_left(right);
864 if (mode == get_irn_mode(n)) {
865 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
871 } /* equivalent_node_Add */
874 * optimize operations that are not commutative but have neutral 0 on left,
877 static ir_node *equivalent_node_left_zero(ir_node *n)
881 ir_node *a = get_binop_left(n);
882 ir_node *b = get_binop_right(n);
883 ir_tarval *tb = value_of(b);
885 if (tarval_is_null(tb)) {
888 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
891 } /* equivalent_node_left_zero */
893 #define equivalent_node_Shl equivalent_node_left_zero
894 #define equivalent_node_Shr equivalent_node_left_zero
895 #define equivalent_node_Shrs equivalent_node_left_zero
896 #define equivalent_node_Rotl equivalent_node_left_zero
899 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
901 * The second one looks strange, but this construct
902 * is used heavily in the LCC sources :-).
904 * Beware: The Mode of a Sub may be different than the mode of its
905 * predecessors, so we could not return a predecessors in all cases.
907 static ir_node *equivalent_node_Sub(ir_node *n)
911 ir_mode *mode = get_irn_mode(n);
914 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
915 if (mode_is_float(mode)) {
916 ir_graph *irg = get_irn_irg(n);
917 if (get_irg_fp_model(irg) & fp_strict_algebraic)
921 b = get_Sub_right(n);
924 /* Beware: modes might be different */
925 if (tarval_is_null(tb)) {
926 ir_node *a = get_Sub_left(n);
927 if (mode == get_irn_mode(a)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
934 } /* equivalent_node_Sub */
938 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
941 * -(-a) == a, but might overflow two times.
942 * We handle it anyway here but the better way would be a
943 * flag. This would be needed for Pascal for instance.
945 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
948 ir_node *pred = get_unop_op(n);
950 /* optimize symmetric unop */
951 if (get_irn_op(pred) == get_irn_op(n)) {
952 n = get_unop_op(pred);
953 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
956 } /* equivalent_node_idempotent_unop */
958 /** Optimize Not(Not(x)) == x. */
959 #define equivalent_node_Not equivalent_node_idempotent_unop
961 /** -(-x) == x ??? Is this possible or can --x raise an
962 out of bounds exception if min =! max? */
963 #define equivalent_node_Minus equivalent_node_idempotent_unop
966 * Optimize a * 1 = 1 * a = a.
968 static ir_node *equivalent_node_Mul(ir_node *n)
971 ir_node *a = get_Mul_left(n);
973 /* we can handle here only the n * n = n bit cases */
974 if (get_irn_mode(n) == get_irn_mode(a)) {
975 ir_node *b = get_Mul_right(n);
979 * Mul is commutative and has again an other neutral element.
980 * Constants are place right, so check this case first.
983 if (tarval_is_one(tv)) {
985 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
988 if (tarval_is_one(tv)) {
990 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
995 } /* equivalent_node_Mul */
998 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1000 static ir_node *equivalent_node_Or(ir_node *n)
1004 ir_node *a = get_Or_left(n);
1005 ir_node *b = get_Or_right(n);
1009 n = a; /* idempotence */
1010 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1013 /* constants are normalized to right, check this side first */
1015 if (tarval_is_null(tv)) {
1017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1021 if (tarval_is_null(tv)) {
1023 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1028 } /* equivalent_node_Or */
1031 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1033 static ir_node *equivalent_node_And(ir_node *n)
1037 ir_node *a = get_And_left(n);
1038 ir_node *b = get_And_right(n);
1042 n = a; /* idempotence */
1043 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1046 /* constants are normalized to right, check this side first */
1048 if (tarval_is_all_one(tv)) {
1050 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1053 if (tv != get_tarval_bad()) {
1054 ir_mode *mode = get_irn_mode(n);
1055 if (!mode_is_signed(mode) && is_Conv(a)) {
1056 ir_node *convop = get_Conv_op(a);
1057 ir_mode *convopmode = get_irn_mode(convop);
1058 if (!mode_is_signed(convopmode)) {
1059 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1060 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1062 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1069 if (tarval_is_all_one(tv)) {
1071 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1075 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1078 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1083 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1086 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1091 } /* equivalent_node_And */
1094 * Try to remove useless Conv's:
1096 static ir_node *equivalent_node_Conv(ir_node *n)
1099 ir_node *a = get_Conv_op(n);
1101 ir_mode *n_mode = get_irn_mode(n);
1102 ir_mode *a_mode = get_irn_mode(a);
1105 if (n_mode == a_mode) { /* No Conv necessary */
1106 if (get_Conv_strict(n)) {
1109 /* neither Minus nor Confirm change the precision,
1110 so we can "look-through" */
1113 p = get_Minus_op(p);
1114 } else if (is_Confirm(p)) {
1115 p = get_Confirm_value(p);
1121 if (is_Conv(p) && get_Conv_strict(p)) {
1122 /* we known already, that a_mode == n_mode, and neither
1123 Minus change the mode, so the second Conv
1125 assert(get_irn_mode(p) == n_mode);
1127 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1131 ir_node *pred = get_Proj_pred(p);
1132 if (is_Load(pred)) {
1133 /* Loads always return with the exact precision of n_mode */
1134 assert(get_Load_mode(pred) == n_mode);
1136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1139 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1140 pred = get_Proj_pred(pred);
1141 if (is_Start(pred)) {
1142 /* Arguments always return with the exact precision,
1143 as strictConv's are place before Call -- if the
1144 caller was compiled with the same setting.
1145 Otherwise, the semantics is probably still right. */
1146 assert(get_irn_mode(p) == n_mode);
1148 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1154 /* special case: the immediate predecessor is also a Conv */
1155 if (! get_Conv_strict(a)) {
1156 /* first one is not strict, kick it */
1158 a_mode = get_irn_mode(a);
1162 /* else both are strict conv, second is superfluous */
1164 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1172 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1173 ir_node *b = get_Conv_op(a);
1174 ir_mode *b_mode = get_irn_mode(b);
1176 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1177 /* both are strict conv */
1178 if (smaller_mode(a_mode, n_mode)) {
1179 /* both are strict, but the first is smaller, so
1180 the second cannot remove more precision, remove the
1182 set_Conv_strict(n, 0);
1185 if (n_mode == b_mode) {
1186 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1187 if (n_mode == mode_b) {
1188 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1189 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1191 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1192 if (values_in_mode(b_mode, a_mode)) {
1193 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1199 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1200 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1201 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1202 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1204 if (float_mantissa >= int_mantissa) {
1206 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1211 if (smaller_mode(b_mode, a_mode)) {
1212 if (get_Conv_strict(n))
1213 set_Conv_strict(b, 1);
1214 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1222 } /* equivalent_node_Conv */
1225 * - fold Phi-nodes, iff they have only one predecessor except
1228 static ir_node *equivalent_node_Phi(ir_node *n)
1234 ir_node *first_val = NULL; /* to shutup gcc */
1236 if (!get_opt_optimize() &&
1237 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1240 n_preds = get_Phi_n_preds(n);
1242 block = get_nodes_block(n);
1244 /* Phi of dead Region without predecessors. */
1248 /* Find first non-self-referencing input */
1249 for (i = 0; i < n_preds; ++i) {
1250 first_val = get_Phi_pred(n, i);
1251 /* not self pointer */
1252 if (first_val != n) {
1253 /* then found first value. */
1258 /* search for rest of inputs, determine if any of these
1259 are non-self-referencing */
1260 while (++i < n_preds) {
1261 ir_node *scnd_val = get_Phi_pred(n, i);
1262 if (scnd_val != n && scnd_val != first_val) {
1267 if (i >= n_preds && !is_Dummy(first_val)) {
1268 /* Fold, if no multiple distinct non-self-referencing inputs */
1270 DBG_OPT_PHI(oldn, n);
1273 } /* equivalent_node_Phi */
1276 * Optimize Proj(Tuple).
1278 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1280 ir_node *oldn = proj;
1281 ir_node *tuple = get_Proj_pred(proj);
1283 /* Remove the Tuple/Proj combination. */
1284 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1285 DBG_OPT_TUPLE(oldn, tuple, proj);
1288 } /* equivalent_node_Proj_Tuple */
1291 * Optimize a / 1 = a.
1293 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1295 ir_node *oldn = proj;
1296 ir_node *div = get_Proj_pred(proj);
1297 ir_node *b = get_Div_right(div);
1298 ir_tarval *tb = value_of(b);
1300 /* Div is not commutative. */
1301 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1302 switch (get_Proj_proj(proj)) {
1304 proj = get_Div_mem(div);
1305 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1309 proj = get_Div_left(div);
1310 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1314 /* we cannot replace the exception Proj's here, this is done in
1315 transform_node_Proj_Div() */
1320 } /* equivalent_node_Proj_Div */
1323 * Optimize CopyB(mem, x, x) into a Nop.
1325 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1327 ir_node *oldn = proj;
1328 ir_node *copyb = get_Proj_pred(proj);
1329 ir_node *a = get_CopyB_dst(copyb);
1330 ir_node *b = get_CopyB_src(copyb);
1333 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1334 switch (get_Proj_proj(proj)) {
1336 proj = get_CopyB_mem(copyb);
1337 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1342 } /* equivalent_node_Proj_CopyB */
1345 * Optimize Bounds(idx, idx, upper) into idx.
1347 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1349 ir_node *oldn = proj;
1350 ir_node *bound = get_Proj_pred(proj);
1351 ir_node *idx = get_Bound_index(bound);
1352 ir_node *pred = skip_Proj(idx);
1355 if (idx == get_Bound_lower(bound))
1357 else if (is_Bound(pred)) {
1359 * idx was Bounds checked previously, it is still valid if
1360 * lower <= pred_lower && pred_upper <= upper.
1362 ir_node *lower = get_Bound_lower(bound);
1363 ir_node *upper = get_Bound_upper(bound);
1364 if (get_Bound_lower(pred) == lower &&
1365 get_Bound_upper(pred) == upper) {
1367 * One could expect that we simply return the previous
1368 * Bound here. However, this would be wrong, as we could
1369 * add an exception Proj to a new location then.
1370 * So, we must turn in into a tuple.
1376 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1377 switch (get_Proj_proj(proj)) {
1379 DBG_OPT_EXC_REM(proj);
1380 proj = get_Bound_mem(bound);
1384 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1387 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1392 } /* equivalent_node_Proj_Bound */
1395 * Does all optimizations on nodes that must be done on its Projs
1396 * because of creating new nodes.
1398 static ir_node *equivalent_node_Proj(ir_node *proj)
1400 ir_node *n = get_Proj_pred(proj);
1401 if (n->op->ops.equivalent_node_Proj)
1402 return n->op->ops.equivalent_node_Proj(proj);
1404 } /* equivalent_node_Proj */
1409 static ir_node *equivalent_node_Id(ir_node *n)
1417 DBG_OPT_ID(oldn, n);
1419 } /* equivalent_node_Id */
1424 static ir_node *equivalent_node_Mux(ir_node *n)
1426 ir_node *oldn = n, *sel = get_Mux_sel(n);
1428 ir_tarval *ts = value_of(sel);
1430 /* Mux(true, f, t) == t */
1431 if (ts == tarval_b_true) {
1432 n = get_Mux_true(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1436 /* Mux(false, f, t) == f */
1437 if (ts == tarval_b_false) {
1438 n = get_Mux_false(n);
1439 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1442 n_t = get_Mux_true(n);
1443 n_f = get_Mux_false(n);
1445 /* Mux(v, x, T) == x */
1446 if (is_Unknown(n_f)) {
1448 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1451 /* Mux(v, T, x) == x */
1452 if (is_Unknown(n_t)) {
1454 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1458 /* Mux(v, x, x) == x */
1461 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1464 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1465 ir_relation relation = get_Cmp_relation(sel);
1466 ir_node *f = get_Mux_false(n);
1467 ir_node *t = get_Mux_true(n);
1470 * Note further that these optimization work even for floating point
1471 * with NaN's because -NaN == NaN.
1472 * However, if +0 and -0 is handled differently, we cannot use the first one.
1474 ir_node *const cmp_l = get_Cmp_left(sel);
1475 ir_node *const cmp_r = get_Cmp_right(sel);
1478 case ir_relation_equal:
1479 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1480 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1482 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1487 case ir_relation_less_greater:
1488 case ir_relation_unordered_less_greater:
1489 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1490 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1501 * Note: normalization puts the constant on the right side,
1502 * so we check only one case.
1504 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1505 /* Mux(t CMP 0, X, t) */
1506 if (is_Minus(f) && get_Minus_op(f) == t) {
1507 /* Mux(t CMP 0, -t, t) */
1508 if (relation == ir_relation_equal) {
1509 /* Mux(t == 0, -t, t) ==> -t */
1511 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1512 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1513 /* Mux(t != 0, -t, t) ==> t */
1515 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1525 * Remove Confirm nodes if setting is on.
1526 * Replace Confirms(x, '=', Constlike) by Constlike.
1528 static ir_node *equivalent_node_Confirm(ir_node *n)
1530 ir_node *pred = get_Confirm_value(n);
1531 ir_relation relation = get_Confirm_relation(n);
1533 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1535 * rare case: two identical Confirms one after another,
1536 * replace the second one with the first.
1539 pred = get_Confirm_value(n);
1545 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1546 * perform no actual computation, as, e.g., the Id nodes. It does not create
1547 * new nodes. It is therefore safe to free n if the node returned is not n.
1548 * If a node returns a Tuple we can not just skip it. If the size of the
1549 * in array fits, we transform n into a tuple (e.g., Div).
1551 ir_node *equivalent_node(ir_node *n)
1553 if (n->op->ops.equivalent_node)
1554 return n->op->ops.equivalent_node(n);
1556 } /* equivalent_node */
1559 * Sets the default equivalent node operation for an ir_op_ops.
1561 * @param code the opcode for the default operation
1562 * @param ops the operations initialized
1567 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1571 ops->equivalent_node = equivalent_node_##a; \
1573 #define CASE_PROJ(a) \
1575 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1610 } /* firm_set_default_equivalent_node */
1613 * Returns non-zero if a node is a Phi node
1614 * with all predecessors constant.
1616 static int is_const_Phi(ir_node *n)
1620 if (! is_Phi(n) || get_irn_arity(n) == 0)
1622 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1623 if (! is_Const(get_irn_n(n, i)))
1627 } /* is_const_Phi */
1629 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1630 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1633 * in reality eval_func should be tarval (*eval_func)() but incomplete
1634 * declarations are bad style and generate noisy warnings
1636 typedef void (*eval_func)(void);
1639 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1641 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1643 if (eval == (eval_func) tarval_sub) {
1644 tarval_sub_type func = (tarval_sub_type)eval;
1646 return func(a, b, mode);
1648 tarval_binop_type func = (tarval_binop_type)eval;
1655 * Apply an evaluator on a binop with a constant operators (and one Phi).
1657 * @param phi the Phi node
1658 * @param other the other operand
1659 * @param eval an evaluator function
1660 * @param mode the mode of the result, may be different from the mode of the Phi!
1661 * @param left if non-zero, other is the left operand, else the right
1663 * @return a new Phi node if the conversion was successful, NULL else
1665 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1671 int i, n = get_irn_arity(phi);
1673 NEW_ARR_A(void *, res, n);
1675 for (i = 0; i < n; ++i) {
1676 pred = get_irn_n(phi, i);
1677 tv = get_Const_tarval(pred);
1678 tv = do_eval(eval, other, tv, mode);
1680 if (tv == tarval_bad) {
1681 /* folding failed, bad */
1687 for (i = 0; i < n; ++i) {
1688 pred = get_irn_n(phi, i);
1689 tv = get_Const_tarval(pred);
1690 tv = do_eval(eval, tv, other, mode);
1692 if (tv == tarval_bad) {
1693 /* folding failed, bad */
1699 irg = get_irn_irg(phi);
1700 for (i = 0; i < n; ++i) {
1701 pred = get_irn_n(phi, i);
1702 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1704 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1705 } /* apply_binop_on_phi */
1708 * Apply an evaluator on a binop with two constant Phi.
1710 * @param a the left Phi node
1711 * @param b the right Phi node
1712 * @param eval an evaluator function
1713 * @param mode the mode of the result, may be different from the mode of the Phi!
1715 * @return a new Phi node if the conversion was successful, NULL else
1717 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1719 ir_tarval *tv_l, *tv_r, *tv;
1725 if (get_nodes_block(a) != get_nodes_block(b))
1728 n = get_irn_arity(a);
1729 NEW_ARR_A(void *, res, n);
1731 for (i = 0; i < n; ++i) {
1732 pred = get_irn_n(a, i);
1733 tv_l = get_Const_tarval(pred);
1734 pred = get_irn_n(b, i);
1735 tv_r = get_Const_tarval(pred);
1736 tv = do_eval(eval, tv_l, tv_r, mode);
1738 if (tv == tarval_bad) {
1739 /* folding failed, bad */
1744 irg = get_irn_irg(a);
1745 for (i = 0; i < n; ++i) {
1746 pred = get_irn_n(a, i);
1747 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1749 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1750 } /* apply_binop_on_2_phis */
1753 * Apply an evaluator on a unop with a constant operator (a Phi).
1755 * @param phi the Phi node
1756 * @param eval an evaluator function
1758 * @return a new Phi node if the conversion was successful, NULL else
1760 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1767 int i, n = get_irn_arity(phi);
1769 NEW_ARR_A(void *, res, n);
1770 for (i = 0; i < n; ++i) {
1771 pred = get_irn_n(phi, i);
1772 tv = get_Const_tarval(pred);
1775 if (tv == tarval_bad) {
1776 /* folding failed, bad */
1781 mode = get_irn_mode(phi);
1782 irg = get_irn_irg(phi);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(phi, i);
1785 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1787 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1788 } /* apply_unop_on_phi */
1791 * Apply a conversion on a constant operator (a Phi).
1793 * @param phi the Phi node
1795 * @return a new Phi node if the conversion was successful, NULL else
1797 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1803 int i, n = get_irn_arity(phi);
1805 NEW_ARR_A(void *, res, n);
1806 for (i = 0; i < n; ++i) {
1807 pred = get_irn_n(phi, i);
1808 tv = get_Const_tarval(pred);
1809 tv = tarval_convert_to(tv, mode);
1811 if (tv == tarval_bad) {
1812 /* folding failed, bad */
1817 irg = get_irn_irg(phi);
1818 for (i = 0; i < n; ++i) {
1819 pred = get_irn_n(phi, i);
1820 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1822 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1823 } /* apply_conv_on_phi */
1826 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1827 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1828 * If possible, remove the Conv's.
1830 static ir_node *transform_node_AddSub(ir_node *n)
1832 ir_mode *mode = get_irn_mode(n);
1834 if (mode_is_reference(mode)) {
1835 ir_node *left = get_binop_left(n);
1836 ir_node *right = get_binop_right(n);
1837 unsigned ref_bits = get_mode_size_bits(mode);
1839 if (is_Conv(left)) {
1840 ir_mode *lmode = get_irn_mode(left);
1841 unsigned bits = get_mode_size_bits(lmode);
1843 if (ref_bits == bits &&
1844 mode_is_int(lmode) &&
1845 get_mode_arithmetic(lmode) == irma_twos_complement) {
1846 ir_node *pre = get_Conv_op(left);
1847 ir_mode *pre_mode = get_irn_mode(pre);
1849 if (mode_is_int(pre_mode) &&
1850 get_mode_size_bits(pre_mode) == bits &&
1851 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1852 /* ok, this conv just changes to sign, moreover the calculation
1853 * is done with same number of bits as our address mode, so
1854 * we can ignore the conv as address calculation can be viewed
1855 * as either signed or unsigned
1857 set_binop_left(n, pre);
1862 if (is_Conv(right)) {
1863 ir_mode *rmode = get_irn_mode(right);
1864 unsigned bits = get_mode_size_bits(rmode);
1866 if (ref_bits == bits &&
1867 mode_is_int(rmode) &&
1868 get_mode_arithmetic(rmode) == irma_twos_complement) {
1869 ir_node *pre = get_Conv_op(right);
1870 ir_mode *pre_mode = get_irn_mode(pre);
1872 if (mode_is_int(pre_mode) &&
1873 get_mode_size_bits(pre_mode) == bits &&
1874 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1875 /* ok, this conv just changes to sign, moreover the calculation
1876 * is done with same number of bits as our address mode, so
1877 * we can ignore the conv as address calculation can be viewed
1878 * as either signed or unsigned
1880 set_binop_right(n, pre);
1885 /* let address arithmetic use unsigned modes */
1886 if (is_Const(right)) {
1887 ir_mode *rmode = get_irn_mode(right);
1889 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1890 /* convert a AddP(P, *s) into AddP(P, *u) */
1891 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1893 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1894 set_binop_right(n, pre);
1900 } /* transform_node_AddSub */
1902 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1905 if (is_Const(b) && is_const_Phi(a)) { \
1906 /* check for Op(Phi, Const) */ \
1907 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1909 else if (is_Const(a) && is_const_Phi(b)) { \
1910 /* check for Op(Const, Phi) */ \
1911 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1913 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1914 /* check for Op(Phi, Phi) */ \
1915 c = apply_binop_on_2_phis(a, b, eval, mode); \
1918 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1923 #define HANDLE_UNOP_PHI(eval, a, c) \
1926 if (is_const_Phi(a)) { \
1927 /* check for Op(Phi) */ \
1928 c = apply_unop_on_phi(a, eval); \
1930 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1937 * Do the AddSub optimization, then Transform
1938 * Constant folding on Phi
1939 * Add(a,a) -> Mul(a, 2)
1940 * Add(Mul(a, x), a) -> Mul(a, x+1)
1941 * if the mode is integer or float.
1942 * Transform Add(a,-b) into Sub(a,b).
1943 * Reassociation might fold this further.
1945 static ir_node *transform_node_Add(ir_node *n)
1948 ir_node *a, *b, *c, *oldn = n;
1949 vrp_attr *a_vrp, *b_vrp;
1951 n = transform_node_AddSub(n);
1953 a = get_Add_left(n);
1954 b = get_Add_right(n);
1956 mode = get_irn_mode(n);
1958 if (mode_is_reference(mode)) {
1959 ir_mode *lmode = get_irn_mode(a);
1961 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1962 /* an Add(a, NULL) is a hidden Conv */
1963 dbg_info *dbg = get_irn_dbg_info(n);
1964 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
1968 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
1970 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1971 if (mode_is_float(mode)) {
1972 ir_graph *irg = get_irn_irg(n);
1973 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1977 if (mode_is_num(mode)) {
1978 ir_graph *irg = get_irn_irg(n);
1979 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1980 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
1981 && a == b && mode_is_int(mode)) {
1982 ir_node *block = get_nodes_block(n);
1985 get_irn_dbg_info(n),
1988 new_r_Const_long(irg, mode, 2),
1990 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1995 get_irn_dbg_info(n),
2000 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2005 get_irn_dbg_info(n),
2010 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2013 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2014 /* Here we rely on constants be on the RIGHT side */
2016 ir_node *op = get_Not_op(a);
2018 if (is_Const(b) && is_Const_one(b)) {
2020 ir_node *blk = get_nodes_block(n);
2021 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2022 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2027 n = new_r_Const(irg, get_mode_minus_one(mode));
2028 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2033 ir_node *op = get_Not_op(b);
2037 n = new_r_Const(irg, get_mode_minus_one(mode));
2038 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2045 a_vrp = vrp_get_info(a);
2046 b_vrp = vrp_get_info(b);
2048 if (a_vrp && b_vrp) {
2049 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2051 if (tarval_is_null(c)) {
2052 dbg_info *dbgi = get_irn_dbg_info(n);
2053 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2057 } /* transform_node_Add */
2060 * returns -cnst or NULL if impossible
2062 static ir_node *const_negate(ir_node *cnst)
2064 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2065 dbg_info *dbgi = get_irn_dbg_info(cnst);
2066 ir_graph *irg = get_irn_irg(cnst);
2067 if (tv == tarval_bad) return NULL;
2068 return new_rd_Const(dbgi, irg, tv);
2072 * Do the AddSub optimization, then Transform
2073 * Constant folding on Phi
2074 * Sub(0,a) -> Minus(a)
2075 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2076 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2077 * Sub(Add(a, x), x) -> a
2078 * Sub(x, Add(x, a)) -> -a
2079 * Sub(x, Const) -> Add(x, -Const)
2081 static ir_node *transform_node_Sub(ir_node *n)
2087 n = transform_node_AddSub(n);
2089 a = get_Sub_left(n);
2090 b = get_Sub_right(n);
2092 mode = get_irn_mode(n);
2094 if (mode_is_int(mode)) {
2095 ir_mode *lmode = get_irn_mode(a);
2097 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2098 /* a Sub(a, NULL) is a hidden Conv */
2099 dbg_info *dbg = get_irn_dbg_info(n);
2100 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2105 if (mode == lmode &&
2106 get_mode_arithmetic(mode) == irma_twos_complement &&
2108 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2110 dbg_info *dbg = get_irn_dbg_info(n);
2111 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2112 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2118 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2120 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2121 if (mode_is_float(mode)) {
2122 ir_graph *irg = get_irn_irg(n);
2123 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2127 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2128 /* a - C -> a + (-C) */
2129 ir_node *cnst = const_negate(b);
2131 ir_node *block = get_nodes_block(n);
2132 dbg_info *dbgi = get_irn_dbg_info(n);
2134 n = new_rd_Add(dbgi, block, a, cnst, mode);
2135 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2140 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2141 dbg_info *dbg = get_irn_dbg_info(n);
2142 ir_node *block = get_nodes_block(n);
2143 ir_node *left = get_Minus_op(a);
2144 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2146 n = new_rd_Minus(dbg, block, add, mode);
2147 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2149 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2150 dbg_info *dbg = get_irn_dbg_info(n);
2151 ir_node *block = get_nodes_block(n);
2152 ir_node *right = get_Minus_op(b);
2154 n = new_rd_Add(dbg, block, a, right, mode);
2155 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2157 } else if (is_Sub(b)) {
2158 /* a - (b - c) -> a + (c - b)
2159 * -> (a - b) + c iff (b - c) is a pointer */
2160 dbg_info *s_dbg = get_irn_dbg_info(b);
2161 ir_node *s_left = get_Sub_left(b);
2162 ir_node *s_right = get_Sub_right(b);
2163 ir_mode *s_mode = get_irn_mode(b);
2164 if (mode_is_reference(s_mode)) {
2165 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2166 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2167 dbg_info *a_dbg = get_irn_dbg_info(n);
2170 s_right = new_r_Conv(lowest_block, s_right, mode);
2171 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2173 ir_node *s_block = get_nodes_block(b);
2174 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2175 dbg_info *a_dbg = get_irn_dbg_info(n);
2176 ir_node *a_block = get_nodes_block(n);
2178 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2180 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2182 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2183 ir_node *m_right = get_Mul_right(b);
2184 if (is_Const(m_right)) {
2185 ir_node *cnst2 = const_negate(m_right);
2186 if (cnst2 != NULL) {
2187 dbg_info *m_dbg = get_irn_dbg_info(b);
2188 ir_node *m_block = get_nodes_block(b);
2189 ir_node *m_left = get_Mul_left(b);
2190 ir_mode *m_mode = get_irn_mode(b);
2191 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2192 dbg_info *a_dbg = get_irn_dbg_info(n);
2193 ir_node *a_block = get_nodes_block(n);
2195 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2196 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2202 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2203 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2205 get_irn_dbg_info(n),
2209 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2213 if (mode_wrap_around(mode)) {
2214 ir_node *left = get_Add_left(a);
2215 ir_node *right = get_Add_right(a);
2217 /* FIXME: Does the Conv's work only for two complement or generally? */
2219 if (mode != get_irn_mode(right)) {
2220 /* This Sub is an effective Cast */
2221 right = new_r_Conv(get_nodes_block(n), right, mode);
2224 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2226 } else if (right == b) {
2227 if (mode != get_irn_mode(left)) {
2228 /* This Sub is an effective Cast */
2229 left = new_r_Conv(get_nodes_block(n), left, mode);
2232 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2238 if (mode_wrap_around(mode)) {
2239 ir_node *left = get_Add_left(b);
2240 ir_node *right = get_Add_right(b);
2242 /* FIXME: Does the Conv's work only for two complement or generally? */
2244 ir_mode *r_mode = get_irn_mode(right);
2246 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2247 if (mode != r_mode) {
2248 /* This Sub is an effective Cast */
2249 n = new_r_Conv(get_nodes_block(n), n, mode);
2251 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2253 } else if (right == a) {
2254 ir_mode *l_mode = get_irn_mode(left);
2256 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2257 if (mode != l_mode) {
2258 /* This Sub is an effective Cast */
2259 n = new_r_Conv(get_nodes_block(n), n, mode);
2261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2266 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2267 ir_mode *mode = get_irn_mode(a);
2269 if (mode == get_irn_mode(b)) {
2271 ir_node *op_a = get_Conv_op(a);
2272 ir_node *op_b = get_Conv_op(b);
2274 /* check if it's allowed to skip the conv */
2275 ma = get_irn_mode(op_a);
2276 mb = get_irn_mode(op_b);
2278 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2279 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2282 set_Sub_right(n, b);
2288 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2289 if (!is_reassoc_running() && is_Mul(a)) {
2290 ir_node *ma = get_Mul_left(a);
2291 ir_node *mb = get_Mul_right(a);
2294 ir_node *blk = get_nodes_block(n);
2295 ir_graph *irg = get_irn_irg(n);
2297 get_irn_dbg_info(n),
2301 get_irn_dbg_info(n),
2304 new_r_Const(irg, get_mode_one(mode)),
2307 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2309 } else if (mb == b) {
2310 ir_node *blk = get_nodes_block(n);
2311 ir_graph *irg = get_irn_irg(n);
2313 get_irn_dbg_info(n),
2317 get_irn_dbg_info(n),
2320 new_r_Const(irg, get_mode_one(mode)),
2323 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2327 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2328 ir_node *x = get_Sub_left(a);
2329 ir_node *y = get_Sub_right(a);
2330 ir_node *blk = get_nodes_block(n);
2331 ir_mode *m_b = get_irn_mode(b);
2332 ir_mode *m_y = get_irn_mode(y);
2336 /* Determine the right mode for the Add. */
2339 else if (mode_is_reference(m_b))
2341 else if (mode_is_reference(m_y))
2345 * Both modes are different but none is reference,
2346 * happens for instance in SubP(SubP(P, Iu), Is).
2347 * We have two possibilities here: Cast or ignore.
2348 * Currently we ignore this case.
2353 add = new_r_Add(blk, y, b, add_mode);
2355 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2356 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2360 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2361 if (is_Const(a) && is_Not(b)) {
2362 /* c - ~X = X + (c+1) */
2363 ir_tarval *tv = get_Const_tarval(a);
2365 tv = tarval_add(tv, get_mode_one(mode));
2366 if (tv != tarval_bad) {
2367 ir_node *blk = get_nodes_block(n);
2368 ir_graph *irg = get_irn_irg(n);
2369 ir_node *c = new_r_Const(irg, tv);
2370 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2371 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2377 } /* transform_node_Sub */
2380 * Several transformation done on n*n=2n bits mul.
2381 * These transformations must be done here because new nodes may be produced.
2383 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2386 ir_node *a = get_Mul_left(n);
2387 ir_node *b = get_Mul_right(n);
2388 ir_tarval *ta = value_of(a);
2389 ir_tarval *tb = value_of(b);
2390 ir_mode *smode = get_irn_mode(a);
2392 if (ta == get_mode_one(smode)) {
2393 /* (L)1 * (L)b = (L)b */
2394 ir_node *blk = get_nodes_block(n);
2395 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2396 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2399 else if (ta == get_mode_minus_one(smode)) {
2400 /* (L)-1 * (L)b = (L)b */
2401 ir_node *blk = get_nodes_block(n);
2402 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2403 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2404 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2407 if (tb == get_mode_one(smode)) {
2408 /* (L)a * (L)1 = (L)a */
2409 ir_node *blk = get_irn_n(a, -1);
2410 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2411 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2414 else if (tb == get_mode_minus_one(smode)) {
2415 /* (L)a * (L)-1 = (L)-a */
2416 ir_node *blk = get_nodes_block(n);
2417 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2418 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2419 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2426 * Transform Mul(a,-1) into -a.
2427 * Do constant evaluation of Phi nodes.
2428 * Do architecture dependent optimizations on Mul nodes
2430 static ir_node *transform_node_Mul(ir_node *n)
2432 ir_node *c, *oldn = n;
2433 ir_mode *mode = get_irn_mode(n);
2434 ir_node *a = get_Mul_left(n);
2435 ir_node *b = get_Mul_right(n);
2437 if (is_Bad(a) || is_Bad(b))
2440 if (mode != get_irn_mode(a))
2441 return transform_node_Mul2n(n, mode);
2443 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2445 if (mode_is_signed(mode)) {
2448 if (value_of(a) == get_mode_minus_one(mode))
2450 else if (value_of(b) == get_mode_minus_one(mode))
2453 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2454 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2459 if (is_Const(b)) { /* (-a) * const -> a * -const */
2460 ir_node *cnst = const_negate(b);
2462 dbg_info *dbgi = get_irn_dbg_info(n);
2463 ir_node *block = get_nodes_block(n);
2464 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2465 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2468 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2469 dbg_info *dbgi = get_irn_dbg_info(n);
2470 ir_node *block = get_nodes_block(n);
2471 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2472 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2474 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2475 ir_node *sub_l = get_Sub_left(b);
2476 ir_node *sub_r = get_Sub_right(b);
2477 dbg_info *dbgi = get_irn_dbg_info(n);
2478 ir_node *block = get_nodes_block(n);
2479 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2480 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2481 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2484 } else if (is_Minus(b)) {
2485 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2486 ir_node *sub_l = get_Sub_left(a);
2487 ir_node *sub_r = get_Sub_right(a);
2488 dbg_info *dbgi = get_irn_dbg_info(n);
2489 ir_node *block = get_nodes_block(n);
2490 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2491 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2492 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2495 } else if (is_Shl(a)) {
2496 ir_node *const shl_l = get_Shl_left(a);
2497 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2498 /* (1 << x) * b -> b << x */
2499 dbg_info *const dbgi = get_irn_dbg_info(n);
2500 ir_node *const block = get_nodes_block(n);
2501 ir_node *const shl_r = get_Shl_right(a);
2502 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2503 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2506 } else if (is_Shl(b)) {
2507 ir_node *const shl_l = get_Shl_left(b);
2508 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2509 /* a * (1 << x) -> a << x */
2510 dbg_info *const dbgi = get_irn_dbg_info(n);
2511 ir_node *const block = get_nodes_block(n);
2512 ir_node *const shl_r = get_Shl_right(b);
2513 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2514 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2518 if (get_mode_arithmetic(mode) == irma_ieee754) {
2520 ir_tarval *tv = get_Const_tarval(a);
2521 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2522 && !tarval_is_negative(tv)) {
2523 /* 2.0 * b = b + b */
2524 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2525 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2529 else if (is_Const(b)) {
2530 ir_tarval *tv = get_Const_tarval(b);
2531 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2532 && !tarval_is_negative(tv)) {
2533 /* a * 2.0 = a + a */
2534 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2535 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2540 return arch_dep_replace_mul_with_shifts(n);
2541 } /* transform_node_Mul */
2544 * Transform a Div Node.
2546 static ir_node *transform_node_Div(ir_node *n)
2548 ir_mode *mode = get_Div_resmode(n);
2549 ir_node *a = get_Div_left(n);
2550 ir_node *b = get_Div_right(n);
2552 const ir_node *dummy;
2554 if (mode_is_int(mode)) {
2555 if (is_Const(b) && is_const_Phi(a)) {
2556 /* check for Div(Phi, Const) */
2557 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2559 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2562 } else if (is_Const(a) && is_const_Phi(b)) {
2563 /* check for Div(Const, Phi) */
2564 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2566 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2569 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2570 /* check for Div(Phi, Phi) */
2571 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2573 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2578 if (a == b && value_not_zero(a, &dummy)) {
2579 ir_graph *irg = get_irn_irg(n);
2580 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2581 value = new_r_Const(irg, get_mode_one(mode));
2582 DBG_OPT_CSTEVAL(n, value);
2585 if (mode_is_signed(mode) && is_Const(b)) {
2586 ir_tarval *tv = get_Const_tarval(b);
2588 if (tv == get_mode_minus_one(mode)) {
2590 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2591 DBG_OPT_CSTEVAL(n, value);
2595 /* Try architecture dependent optimization */
2596 value = arch_dep_replace_div_by_const(n);
2599 assert(mode_is_float(mode));
2601 /* Optimize x/c to x*(1/c) */
2602 if (get_mode_arithmetic(mode) == irma_ieee754) {
2603 ir_tarval *tv = value_of(b);
2605 if (tv != tarval_bad) {
2606 int rem = tarval_fp_ops_enabled();
2609 * Floating point constant folding might be disabled here to
2611 * However, as we check for exact result, doing it is safe.
2614 tarval_enable_fp_ops(1);
2615 tv = tarval_div(get_mode_one(mode), tv);
2616 tarval_enable_fp_ops(rem);
2618 /* Do the transformation if the result is either exact or we are
2619 not using strict rules. */
2620 if (tv != tarval_bad &&
2621 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2622 ir_node *block = get_nodes_block(n);
2623 ir_graph *irg = get_irn_irg(block);
2624 ir_node *c = new_r_Const(irg, tv);
2625 dbg_info *dbgi = get_irn_dbg_info(n);
2626 value = new_rd_Mul(dbgi, block, a, c, mode);
2639 /* Turn Div into a tuple (mem, jmp, bad, value) */
2640 mem = get_Div_mem(n);
2641 blk = get_nodes_block(n);
2642 irg = get_irn_irg(blk);
2644 /* skip a potential Pin */
2645 mem = skip_Pin(mem);
2646 turn_into_tuple(n, pn_Div_max);
2647 set_Tuple_pred(n, pn_Div_M, mem);
2648 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2649 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2650 set_Tuple_pred(n, pn_Div_res, value);
2653 } /* transform_node_Div */
2656 * Transform a Mod node.
2658 static ir_node *transform_node_Mod(ir_node *n)
2660 ir_mode *mode = get_Mod_resmode(n);
2661 ir_node *a = get_Mod_left(n);
2662 ir_node *b = get_Mod_right(n);
2667 if (is_Const(b) && is_const_Phi(a)) {
2668 /* check for Div(Phi, Const) */
2669 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2671 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2675 else if (is_Const(a) && is_const_Phi(b)) {
2676 /* check for Div(Const, Phi) */
2677 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2679 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2683 else if (is_const_Phi(a) && is_const_Phi(b)) {
2684 /* check for Div(Phi, Phi) */
2685 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2687 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2694 irg = get_irn_irg(n);
2695 if (tv != tarval_bad) {
2696 value = new_r_Const(irg, tv);
2698 DBG_OPT_CSTEVAL(n, value);
2701 ir_node *a = get_Mod_left(n);
2702 ir_node *b = get_Mod_right(n);
2703 const ir_node *dummy;
2705 if (a == b && value_not_zero(a, &dummy)) {
2706 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2707 value = new_r_Const(irg, get_mode_null(mode));
2708 DBG_OPT_CSTEVAL(n, value);
2711 if (mode_is_signed(mode) && is_Const(b)) {
2712 ir_tarval *tv = get_Const_tarval(b);
2714 if (tv == get_mode_minus_one(mode)) {
2716 value = new_r_Const(irg, get_mode_null(mode));
2717 DBG_OPT_CSTEVAL(n, value);
2721 /* Try architecture dependent optimization */
2722 value = arch_dep_replace_mod_by_const(n);
2731 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2732 mem = get_Mod_mem(n);
2733 blk = get_nodes_block(n);
2734 irg = get_irn_irg(blk);
2736 /* skip a potential Pin */
2737 mem = skip_Pin(mem);
2738 turn_into_tuple(n, pn_Mod_max);
2739 set_Tuple_pred(n, pn_Mod_M, mem);
2740 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2741 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2742 set_Tuple_pred(n, pn_Mod_res, value);
2745 } /* transform_node_Mod */
2748 * Transform a Cond node.
2750 * Replace the Cond by a Jmp if it branches on a constant
2753 static ir_node *transform_node_Cond(ir_node *n)
2756 ir_node *a = get_Cond_selector(n);
2757 ir_tarval *ta = value_of(a);
2758 ir_graph *irg = get_irn_irg(n);
2761 /* we need block info which is not available in floating irgs */
2762 if (get_irg_pinned(irg) == op_pin_state_floats)
2765 if ((ta != tarval_bad) &&
2766 (get_irn_mode(a) == mode_b) &&
2767 (get_opt_unreachable_code())) {
2768 /* It's a boolean Cond, branching on a boolean constant.
2769 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2770 ir_node *blk = get_nodes_block(n);
2771 jmp = new_r_Jmp(blk);
2772 turn_into_tuple(n, pn_Cond_max);
2773 if (ta == tarval_b_true) {
2774 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2775 set_Tuple_pred(n, pn_Cond_true, jmp);
2777 set_Tuple_pred(n, pn_Cond_false, jmp);
2778 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2780 /* We might generate an endless loop, so keep it alive. */
2781 add_End_keepalive(get_irg_end(irg), blk);
2784 } /* transform_node_Cond */
2787 * Prototype of a recursive transform function
2788 * for bitwise distributive transformations.
2790 typedef ir_node* (*recursive_transform)(ir_node *n);
2793 * makes use of distributive laws for and, or, eor
2794 * and(a OP c, b OP c) -> and(a, b) OP c
2795 * note, might return a different op than n
2797 static ir_node *transform_bitwise_distributive(ir_node *n,
2798 recursive_transform trans_func)
2801 ir_node *a = get_binop_left(n);
2802 ir_node *b = get_binop_right(n);
2803 ir_op *op = get_irn_op(a);
2804 ir_op *op_root = get_irn_op(n);
2806 if (op != get_irn_op(b))
2809 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2810 if (op == op_Conv) {
2811 ir_node *a_op = get_Conv_op(a);
2812 ir_node *b_op = get_Conv_op(b);
2813 ir_mode *a_mode = get_irn_mode(a_op);
2814 ir_mode *b_mode = get_irn_mode(b_op);
2815 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2816 ir_node *blk = get_nodes_block(n);
2819 set_binop_left(n, a_op);
2820 set_binop_right(n, b_op);
2821 set_irn_mode(n, a_mode);
2823 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2825 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2831 /* nothing to gain here */
2835 if (op == op_Shrs || op == op_Shr || op == op_Shl
2836 || op == op_And || op == op_Or || op == op_Eor) {
2837 ir_node *a_left = get_binop_left(a);
2838 ir_node *a_right = get_binop_right(a);
2839 ir_node *b_left = get_binop_left(b);
2840 ir_node *b_right = get_binop_right(b);
2842 ir_node *op1 = NULL;
2843 ir_node *op2 = NULL;
2845 if (is_op_commutative(op)) {
2846 if (a_left == b_left) {
2850 } else if (a_left == b_right) {
2854 } else if (a_right == b_left) {
2860 if (a_right == b_right) {
2867 /* (a sop c) & (b sop c) => (a & b) sop c */
2868 ir_node *blk = get_nodes_block(n);
2870 ir_node *new_n = exact_copy(n);
2871 set_binop_left(new_n, op1);
2872 set_binop_right(new_n, op2);
2873 new_n = trans_func(new_n);
2875 if (op_root == op_Eor && op == op_Or) {
2876 dbg_info *dbgi = get_irn_dbg_info(n);
2877 ir_mode *mode = get_irn_mode(c);
2879 c = new_rd_Not(dbgi, blk, c, mode);
2880 n = new_rd_And(dbgi, blk, new_n, c, mode);
2883 set_nodes_block(n, blk);
2884 set_binop_left(n, new_n);
2885 set_binop_right(n, c);
2889 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2898 * Create a 0 constant of given mode.
2900 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2902 ir_tarval *tv = get_mode_null(mode);
2903 ir_node *cnst = new_r_Const(irg, tv);
2911 static ir_node *transform_node_And(ir_node *n)
2913 ir_node *c, *oldn = n;
2914 ir_node *a = get_And_left(n);
2915 ir_node *b = get_And_right(n);
2917 vrp_attr *a_vrp, *b_vrp;
2919 if (is_Cmp(a) && is_Cmp(b)) {
2920 ir_node *a_left = get_Cmp_left(a);
2921 ir_node *a_right = get_Cmp_right(a);
2922 ir_node *b_left = get_Cmp_left(b);
2923 ir_node *b_right = get_Cmp_right(b);
2924 ir_relation a_relation = get_Cmp_relation(a);
2925 ir_relation b_relation = get_Cmp_relation(b);
2926 /* we can combine the relations of two compares with the same
2928 if (a_left == b_left && b_left == b_right) {
2929 dbg_info *dbgi = get_irn_dbg_info(n);
2930 ir_node *block = get_nodes_block(n);
2931 ir_relation new_relation = a_relation & b_relation;
2932 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2934 /* Cmp(a==0) and Cmp(b==0) can be optimized to Cmp(a|b==0) */
2935 if (is_Const(a_right) && is_Const_null(a_right)
2936 && is_Const(b_right) && is_Const_null(b_right)
2937 && a_relation == b_relation && a_relation == ir_relation_equal
2938 && !mode_is_float(get_irn_mode(a_left))
2939 && !mode_is_float(get_irn_mode(b_left))) {
2940 dbg_info *dbgi = get_irn_dbg_info(n);
2941 ir_node *block = get_nodes_block(n);
2942 ir_mode *mode = get_irn_mode(a_left);
2943 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
2944 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
2945 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
2946 ir_graph *irg = get_irn_irg(n);
2947 ir_node *zero = create_zero_const(irg, mode);
2948 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
2952 mode = get_irn_mode(n);
2953 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
2957 ir_node *op = get_Not_op(b);
2959 ir_node *ba = get_And_left(op);
2960 ir_node *bb = get_And_right(op);
2962 /* it's enough to test the following cases due to normalization! */
2963 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2964 /* (a|b) & ~(a&b) = a^b */
2965 ir_node *block = get_nodes_block(n);
2967 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
2968 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2976 ir_node *op = get_Not_op(a);
2978 ir_node *aa = get_And_left(op);
2979 ir_node *ab = get_And_right(op);
2981 /* it's enough to test the following cases due to normalization! */
2982 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2983 /* (a|b) & ~(a&b) = a^b */
2984 ir_node *block = get_nodes_block(n);
2986 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
2987 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2994 ir_node *al = get_Eor_left(a);
2995 ir_node *ar = get_Eor_right(a);
2998 /* (b ^ a) & b -> ~a & b */
2999 dbg_info *dbg = get_irn_dbg_info(n);
3000 ir_node *block = get_nodes_block(n);
3002 ar = new_rd_Not(dbg, block, ar, mode);
3003 n = new_rd_And(dbg, block, ar, b, mode);
3004 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3008 /* (a ^ b) & b -> ~a & b */
3009 dbg_info *dbg = get_irn_dbg_info(n);
3010 ir_node *block = get_nodes_block(n);
3012 al = new_rd_Not(dbg, block, al, mode);
3013 n = new_rd_And(dbg, block, al, b, mode);
3014 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3019 ir_node *bl = get_Eor_left(b);
3020 ir_node *br = get_Eor_right(b);
3023 /* a & (a ^ b) -> a & ~b */
3024 dbg_info *dbg = get_irn_dbg_info(n);
3025 ir_node *block = get_nodes_block(n);
3027 br = new_rd_Not(dbg, block, br, mode);
3028 n = new_rd_And(dbg, block, br, a, mode);
3029 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3033 /* a & (b ^ a) -> a & ~b */
3034 dbg_info *dbg = get_irn_dbg_info(n);
3035 ir_node *block = get_nodes_block(n);
3037 bl = new_rd_Not(dbg, block, bl, mode);
3038 n = new_rd_And(dbg, block, bl, a, mode);
3039 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3043 if (is_Not(a) && is_Not(b)) {
3044 /* ~a & ~b = ~(a|b) */
3045 ir_node *block = get_nodes_block(n);
3046 ir_mode *mode = get_irn_mode(n);
3050 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3051 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3052 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3056 b_vrp = vrp_get_info(b);
3057 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3058 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3064 a_vrp = vrp_get_info(a);
3065 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3066 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3070 n = transform_bitwise_distributive(n, transform_node_And);
3073 } /* transform_node_And */
3075 /* the order of the values is important! */
3076 typedef enum const_class {
3082 static const_class classify_const(const ir_node* n)
3084 if (is_Const(n)) return const_const;
3085 if (is_irn_constlike(n)) return const_like;
3090 * Determines whether r is more constlike or has a larger index (in that order)
3093 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3095 const const_class l_order = classify_const(l);
3096 const const_class r_order = classify_const(r);
3098 l_order > r_order ||
3099 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3105 static ir_node *transform_node_Eor(ir_node *n)
3107 ir_node *c, *oldn = n;
3108 ir_node *a = get_Eor_left(n);
3109 ir_node *b = get_Eor_right(n);
3110 ir_mode *mode = get_irn_mode(n);
3112 /* we can combine the relations of two compares with the same operands */
3113 if (is_Cmp(a) && is_Cmp(b)) {
3114 ir_node *a_left = get_Cmp_left(a);
3115 ir_node *a_right = get_Cmp_left(a);
3116 ir_node *b_left = get_Cmp_left(b);
3117 ir_node *b_right = get_Cmp_right(b);
3118 if (a_left == b_left && b_left == b_right) {
3119 dbg_info *dbgi = get_irn_dbg_info(n);
3120 ir_node *block = get_nodes_block(n);
3121 ir_relation a_relation = get_Cmp_relation(a);
3122 ir_relation b_relation = get_Cmp_relation(b);
3123 ir_relation new_relation = a_relation ^ b_relation;
3124 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3128 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3130 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3131 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3132 dbg_info *dbg = get_irn_dbg_info(n);
3133 ir_node *block = get_nodes_block(n);
3134 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3135 ir_node *new_left = get_Not_op(a);
3136 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3137 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3139 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3140 dbg_info *dbg = get_irn_dbg_info(n);
3141 ir_node *block = get_nodes_block(n);
3142 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3143 ir_node *new_right = get_Not_op(b);
3144 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3145 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3149 /* x ^ 1...1 -> ~1 */
3150 if (is_Const(b) && is_Const_all_one(b)) {
3151 n = new_r_Not(get_nodes_block(n), a, mode);
3152 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3156 n = transform_bitwise_distributive(n, transform_node_Eor);
3158 } /* transform_node_Eor */
3163 static ir_node *transform_node_Not(ir_node *n)
3165 ir_node *c, *oldn = n;
3166 ir_node *a = get_Not_op(n);
3167 ir_mode *mode = get_irn_mode(n);
3169 HANDLE_UNOP_PHI(tarval_not,a,c);
3171 /* check for a boolean Not */
3173 dbg_info *dbgi = get_irn_dbg_info(a);
3174 ir_node *block = get_nodes_block(a);
3175 ir_relation relation = get_Cmp_relation(a);
3176 relation = get_negated_relation(relation);
3177 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3178 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3182 /* normalize ~(a ^ b) => a ^ ~b */
3184 dbg_info *dbg = get_irn_dbg_info(n);
3185 ir_node *block = get_nodes_block(n);
3186 ir_node *eor_right = get_Eor_right(a);
3187 ir_node *eor_left = get_Eor_left(a);
3188 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3189 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3193 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3194 if (is_Minus(a)) { /* ~-x -> x + -1 */
3195 dbg_info *dbg = get_irn_dbg_info(n);
3196 ir_graph *irg = get_irn_irg(n);
3197 ir_node *block = get_nodes_block(n);
3198 ir_node *add_l = get_Minus_op(a);
3199 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3200 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3201 } else if (is_Add(a)) {
3202 ir_node *add_r = get_Add_right(a);
3203 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3204 /* ~(x + -1) = -x */
3205 ir_node *op = get_Add_left(a);
3206 ir_node *blk = get_nodes_block(n);
3207 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3216 * Transform a Minus.
3220 * -(a >>u (size-1)) = a >>s (size-1)
3221 * -(a >>s (size-1)) = a >>u (size-1)
3222 * -(a * const) -> a * -const
3224 static ir_node *transform_node_Minus(ir_node *n)
3226 ir_node *c, *oldn = n;
3227 ir_node *a = get_Minus_op(n);
3230 HANDLE_UNOP_PHI(tarval_neg,a,c);
3232 mode = get_irn_mode(a);
3233 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3234 /* the following rules are only to twos-complement */
3237 ir_node *op = get_Not_op(a);
3238 ir_tarval *tv = get_mode_one(mode);
3239 ir_node *blk = get_nodes_block(n);
3240 ir_graph *irg = get_irn_irg(blk);
3241 ir_node *c = new_r_Const(irg, tv);
3242 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3243 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3247 ir_node *c = get_Shr_right(a);
3250 ir_tarval *tv = get_Const_tarval(c);
3252 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3253 /* -(a >>u (size-1)) = a >>s (size-1) */
3254 ir_node *v = get_Shr_left(a);
3256 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3257 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3263 ir_node *c = get_Shrs_right(a);
3266 ir_tarval *tv = get_Const_tarval(c);
3268 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3269 /* -(a >>s (size-1)) = a >>u (size-1) */
3270 ir_node *v = get_Shrs_left(a);
3272 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3273 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3280 /* - (a-b) = b - a */
3281 ir_node *la = get_Sub_left(a);
3282 ir_node *ra = get_Sub_right(a);
3283 ir_node *blk = get_nodes_block(n);
3285 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3286 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3290 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3291 ir_node *mul_l = get_Mul_left(a);
3292 ir_node *mul_r = get_Mul_right(a);
3293 ir_tarval *tv = value_of(mul_r);
3294 if (tv != tarval_bad) {
3295 tv = tarval_neg(tv);
3296 if (tv != tarval_bad) {
3297 ir_graph *irg = get_irn_irg(n);
3298 ir_node *cnst = new_r_Const(irg, tv);
3299 dbg_info *dbg = get_irn_dbg_info(a);
3300 ir_node *block = get_nodes_block(a);
3301 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3302 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3309 } /* transform_node_Minus */
3312 * Transform a Proj(Load) with a non-null address.
3314 static ir_node *transform_node_Proj_Load(ir_node *proj)
3316 if (get_opt_ldst_only_null_ptr_exceptions()) {
3317 if (get_irn_mode(proj) == mode_X) {
3318 ir_node *load = get_Proj_pred(proj);
3320 /* get the Load address */
3321 const ir_node *addr = get_Load_ptr(load);
3322 const ir_node *confirm;
3324 if (value_not_null(addr, &confirm)) {
3325 if (confirm == NULL) {
3326 /* this node may float if it did not depend on a Confirm */
3327 set_irn_pinned(load, op_pin_state_floats);
3329 if (get_Proj_proj(proj) == pn_Load_X_except) {
3330 ir_graph *irg = get_irn_irg(proj);
3331 DBG_OPT_EXC_REM(proj);
3332 return new_r_Bad(irg, mode_X);
3334 ir_node *blk = get_nodes_block(load);
3335 return new_r_Jmp(blk);
3341 } /* transform_node_Proj_Load */
3344 * Transform a Proj(Store) with a non-null address.
3346 static ir_node *transform_node_Proj_Store(ir_node *proj)
3348 if (get_opt_ldst_only_null_ptr_exceptions()) {
3349 if (get_irn_mode(proj) == mode_X) {
3350 ir_node *store = get_Proj_pred(proj);
3352 /* get the load/store address */
3353 const ir_node *addr = get_Store_ptr(store);
3354 const ir_node *confirm;
3356 if (value_not_null(addr, &confirm)) {
3357 if (confirm == NULL) {
3358 /* this node may float if it did not depend on a Confirm */
3359 set_irn_pinned(store, op_pin_state_floats);
3361 if (get_Proj_proj(proj) == pn_Store_X_except) {
3362 ir_graph *irg = get_irn_irg(proj);
3363 DBG_OPT_EXC_REM(proj);
3364 return new_r_Bad(irg, mode_X);
3366 ir_node *blk = get_nodes_block(store);
3367 return new_r_Jmp(blk);
3373 } /* transform_node_Proj_Store */
3376 * Transform a Proj(Div) with a non-zero value.
3377 * Removes the exceptions and routes the memory to the NoMem node.
3379 static ir_node *transform_node_Proj_Div(ir_node *proj)
3381 ir_node *div = get_Proj_pred(proj);
3382 ir_node *b = get_Div_right(div);
3383 ir_node *res, *new_mem;
3384 const ir_node *confirm;
3387 if (value_not_zero(b, &confirm)) {
3388 /* div(x, y) && y != 0 */
3389 if (confirm == NULL) {
3390 /* we are sure we have a Const != 0 */
3391 new_mem = get_Div_mem(div);
3392 new_mem = skip_Pin(new_mem);
3393 set_Div_mem(div, new_mem);
3394 set_irn_pinned(div, op_pin_state_floats);
3397 proj_nr = get_Proj_proj(proj);
3399 case pn_Div_X_regular:
3400 return new_r_Jmp(get_nodes_block(div));
3402 case pn_Div_X_except: {
3403 ir_graph *irg = get_irn_irg(proj);
3404 /* we found an exception handler, remove it */
3405 DBG_OPT_EXC_REM(proj);
3406 return new_r_Bad(irg, mode_X);
3410 ir_graph *irg = get_irn_irg(proj);
3411 res = get_Div_mem(div);
3412 new_mem = get_irg_no_mem(irg);
3415 /* This node can only float up to the Confirm block */
3416 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3418 set_irn_pinned(div, op_pin_state_floats);
3419 /* this is a Div without exception, we can remove the memory edge */
3420 set_Div_mem(div, new_mem);
3426 } /* transform_node_Proj_Div */
3429 * Transform a Proj(Mod) with a non-zero value.
3430 * Removes the exceptions and routes the memory to the NoMem node.
3432 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3434 ir_node *mod = get_Proj_pred(proj);
3435 ir_node *b = get_Mod_right(mod);
3436 ir_node *res, *new_mem;
3437 const ir_node *confirm;
3440 if (value_not_zero(b, &confirm)) {
3441 /* mod(x, y) && y != 0 */
3442 proj_nr = get_Proj_proj(proj);
3444 if (confirm == NULL) {
3445 /* we are sure we have a Const != 0 */
3446 new_mem = get_Mod_mem(mod);
3447 new_mem = skip_Pin(new_mem);
3448 set_Mod_mem(mod, new_mem);
3449 set_irn_pinned(mod, op_pin_state_floats);
3454 case pn_Mod_X_regular:
3455 return new_r_Jmp(get_irn_n(mod, -1));
3457 case pn_Mod_X_except: {
3458 ir_graph *irg = get_irn_irg(proj);
3459 /* we found an exception handler, remove it */
3460 DBG_OPT_EXC_REM(proj);
3461 return new_r_Bad(irg, mode_X);
3465 ir_graph *irg = get_irn_irg(proj);
3466 res = get_Mod_mem(mod);
3467 new_mem = get_irg_no_mem(irg);
3470 /* This node can only float up to the Confirm block */
3471 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3473 /* this is a Mod without exception, we can remove the memory edge */
3474 set_Mod_mem(mod, new_mem);
3478 if (get_Mod_left(mod) == b) {
3479 /* a % a = 0 if a != 0 */
3480 ir_graph *irg = get_irn_irg(proj);
3481 ir_mode *mode = get_irn_mode(proj);
3482 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3484 DBG_OPT_CSTEVAL(mod, res);
3490 } /* transform_node_Proj_Mod */
3493 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3495 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3497 ir_node *n = get_Proj_pred(proj);
3498 ir_node *b = get_Cond_selector(n);
3500 if (!get_opt_unreachable_code())
3503 if (mode_is_int(get_irn_mode(b))) {
3504 ir_tarval *tb = value_of(b);
3506 if (tb != tarval_bad) {
3507 /* we have a constant switch */
3508 long num = get_Proj_proj(proj);
3510 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3511 if (get_tarval_long(tb) == num) {
3512 /* Do NOT create a jump here, or we will have 2 control flow ops
3513 * in a block. This case is optimized away in optimize_cf(). */
3516 ir_graph *irg = get_irn_irg(proj);
3517 /* this case will NEVER be taken, kill it */
3518 return new_r_Bad(irg, mode_X);
3522 long num = get_Proj_proj(proj);
3523 vrp_attr *b_vrp = vrp_get_info(b);
3524 if (num != get_Cond_default_proj(n) && b_vrp) {
3525 /* Try handling with vrp data. We only remove dead parts. */
3526 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3528 if (b_vrp->range_type == VRP_RANGE) {
3529 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3530 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3532 if ((cmp_result & ir_relation_greater) == cmp_result
3533 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3534 ir_graph *irg = get_irn_irg(proj);
3535 return new_r_Bad(irg, mode_X);
3537 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3538 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3539 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3541 if ((cmp_result & ir_relation_less_equal) == cmp_result
3542 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3543 ir_graph *irg = get_irn_irg(proj);
3544 return new_r_Bad(irg, mode_X);
3549 tarval_and( b_vrp->bits_set, tp),
3551 ) == ir_relation_equal)) {
3552 ir_graph *irg = get_irn_irg(proj);
3553 return new_r_Bad(irg, mode_X);
3559 tarval_not(b_vrp->bits_not_set)),
3560 tarval_not(b_vrp->bits_not_set))
3561 == ir_relation_equal)) {
3562 ir_graph *irg = get_irn_irg(proj);
3563 return new_r_Bad(irg, mode_X);
3572 * return true if the operation returns a value with exactly 1 bit set
3574 static bool is_single_bit(const ir_node *node)
3576 /* a first implementation, could be extended with vrp and others... */
3578 ir_node *shl_l = get_Shl_left(node);
3579 ir_mode *mode = get_irn_mode(node);
3580 int modulo = get_mode_modulo_shift(mode);
3581 /* this works if we shift a 1 and we have modulo shift */
3582 if (is_Const(shl_l) && is_Const_one(shl_l)
3583 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3586 } else if (is_Const(node)) {
3587 ir_tarval *tv = get_Const_tarval(node);
3588 return tarval_is_single_bit(tv);
3594 * Normalizes and optimizes Cmp nodes.
3596 static ir_node *transform_node_Cmp(ir_node *n)
3598 ir_node *left = get_Cmp_left(n);
3599 ir_node *right = get_Cmp_right(n);
3600 ir_mode *mode = get_irn_mode(left);
3601 ir_tarval *tv = NULL;
3602 bool changed = false;
3603 bool changedc = false;
3604 ir_relation relation = get_Cmp_relation(n);
3605 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3607 /* mask out impossible relations */
3608 ir_relation new_relation = relation & possible;
3609 if (new_relation != relation) {
3610 relation = new_relation;
3614 /* Remove unnecessary conversions */
3615 /* TODO handle conv+constant */
3616 if (is_Conv(left) && is_Conv(right)) {
3617 ir_node *op_left = get_Conv_op(left);
3618 ir_node *op_right = get_Conv_op(right);
3619 ir_mode *mode_left = get_irn_mode(op_left);
3620 ir_mode *mode_right = get_irn_mode(op_right);
3622 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3623 && mode_left != mode_b && mode_right != mode_b) {
3624 ir_node *block = get_nodes_block(n);
3626 if (mode_left == mode_right) {
3630 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3631 } else if (smaller_mode(mode_left, mode_right)) {
3632 left = new_r_Conv(block, op_left, mode_right);
3635 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3636 } else if (smaller_mode(mode_right, mode_left)) {
3638 right = new_r_Conv(block, op_right, mode_left);
3640 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3646 * Optimize -a CMP -b into b CMP a.
3647 * This works only for modes where unary Minus cannot Overflow.
3648 * Note that two-complement integers can Overflow so it will NOT work.
3650 if (!mode_overflow_on_unary_Minus(mode) &&
3651 is_Minus(left) && is_Minus(right)) {
3652 left = get_Minus_op(left);
3653 right = get_Minus_op(right);
3654 relation = get_inversed_relation(relation);
3656 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3659 /* remove operation on both sides if possible */
3660 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3662 * The following operations are NOT safe for floating point operations, for instance
3663 * 1.0 + inf == 2.0 + inf, =/=> x == y
3665 if (mode_is_int(mode)) {
3666 unsigned lop = get_irn_opcode(left);
3668 if (lop == get_irn_opcode(right)) {
3669 ir_node *ll, *lr, *rl, *rr;
3671 /* same operation on both sides, try to remove */
3675 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3676 left = get_unop_op(left);
3677 right = get_unop_op(right);
3679 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3682 ll = get_Add_left(left);
3683 lr = get_Add_right(left);
3684 rl = get_Add_left(right);
3685 rr = get_Add_right(right);
3688 /* X + a CMP X + b ==> a CMP b */
3692 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3693 } else if (ll == rr) {
3694 /* X + a CMP b + X ==> a CMP b */
3698 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3699 } else if (lr == rl) {
3700 /* a + X CMP X + b ==> a CMP b */
3704 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3705 } else if (lr == rr) {
3706 /* a + X CMP b + X ==> a CMP b */
3710 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3714 ll = get_Sub_left(left);
3715 lr = get_Sub_right(left);
3716 rl = get_Sub_left(right);
3717 rr = get_Sub_right(right);
3720 /* X - a CMP X - b ==> a CMP b */
3724 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3725 } else if (lr == rr) {
3726 /* a - X CMP b - X ==> a CMP b */
3730 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3734 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3735 /* a ROTL X CMP b ROTL X ==> a CMP b */
3736 left = get_Rotl_left(left);
3737 right = get_Rotl_left(right);
3739 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3747 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3748 if (is_Add(left) || is_Sub(left)) {
3749 ir_node *ll = get_binop_left(left);
3750 ir_node *lr = get_binop_right(left);
3752 if (lr == right && is_Add(left)) {
3758 ir_graph *irg = get_irn_irg(n);
3760 right = create_zero_const(irg, mode);
3762 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3765 if (is_Add(right) || is_Sub(right)) {
3766 ir_node *rl = get_binop_left(right);
3767 ir_node *rr = get_binop_right(right);
3769 if (rr == left && is_Add(right)) {
3775 ir_graph *irg = get_irn_irg(n);
3777 right = create_zero_const(irg, mode);
3779 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3783 if (is_And(left) && is_Const(right)) {
3784 ir_node *ll = get_binop_left(left);
3785 ir_node *lr = get_binop_right(left);
3786 if (is_Shr(ll) && is_Const(lr)) {
3787 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
3788 ir_node *block = get_nodes_block(n);
3789 ir_mode *mode = get_irn_mode(left);
3791 ir_node *llr = get_Shr_right(ll);
3792 if (is_Const(llr)) {
3793 dbg_info *dbg = get_irn_dbg_info(left);
3794 ir_graph *irg = get_irn_irg(left);
3796 ir_tarval *c1 = get_Const_tarval(llr);
3797 ir_tarval *c2 = get_Const_tarval(lr);
3798 ir_tarval *c3 = get_Const_tarval(right);
3799 ir_tarval *mask = tarval_shl(c2, c1);
3800 ir_tarval *value = tarval_shl(c3, c1);
3802 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
3803 right = new_r_Const(irg, value);
3808 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
3810 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
3811 right = get_Eor_right(left);
3812 left = get_Eor_left(left);
3815 } /* mode_is_int(...) */
3818 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
3819 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
3820 if (mode_is_int(mode) && is_And(left)
3821 && (relation == ir_relation_equal
3822 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
3823 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
3824 ir_node *and0 = get_And_left(left);
3825 ir_node *and1 = get_And_right(left);
3826 if (and1 == right) {
3827 ir_node *tmp = and0;
3831 if (and0 == right && is_single_bit(and0)) {
3832 ir_graph *irg = get_irn_irg(n);
3834 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
3835 right = create_zero_const(irg, mode);
3840 /* replace mode_b compares with ands/ors */
3841 if (mode == mode_b) {
3842 ir_node *block = get_nodes_block(n);
3846 case ir_relation_less_equal:
3847 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
3849 case ir_relation_less:
3850 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
3852 case ir_relation_greater_equal:
3853 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
3855 case ir_relation_greater:
3856 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
3858 case ir_relation_less_greater:
3859 bres = new_r_Eor(block, left, right, mode_b);
3861 case ir_relation_equal:
3862 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
3865 #ifdef DEBUG_libfirm
3866 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
3871 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3877 * First step: normalize the compare op
3878 * by placing the constant on the right side
3879 * or moving the lower address node to the left.
3881 if (!operands_are_normalized(left, right)) {
3886 relation = get_inversed_relation(relation);
3891 * Second step: Try to reduce the magnitude
3892 * of a constant. This may help to generate better code
3893 * later and may help to normalize more compares.
3894 * Of course this is only possible for integer values.
3896 tv = value_of(right);
3897 if (tv != tarval_bad) {
3898 ir_mode *mode = get_irn_mode(right);
3900 /* TODO extend to arbitrary constants */
3901 if (is_Conv(left) && tarval_is_null(tv)) {
3902 ir_node *op = get_Conv_op(left);
3903 ir_mode *op_mode = get_irn_mode(op);
3906 * UpConv(x) REL 0 ==> x REL 0
3907 * Don't do this for float values as it's unclear whether it is a
3908 * win. (on the other side it makes detection/creation of fabs hard)
3910 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3911 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
3912 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
3913 !mode_is_float(mode)) {
3914 tv = get_mode_null(op_mode);
3918 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3922 if (tv != tarval_bad) {
3923 /* the following optimization is possible on modes without Overflow
3924 * on Unary Minus or on == and !=:
3925 * -a CMP c ==> a swap(CMP) -c
3927 * Beware: for two-complement Overflow may occur, so only == and != can
3928 * be optimized, see this:
3929 * -MININT < 0 =/=> MININT > 0 !!!
3931 if (is_Minus(left) &&
3932 (!mode_overflow_on_unary_Minus(mode) ||
3933 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
3934 tv = tarval_neg(tv);
3936 if (tv != tarval_bad) {
3937 left = get_Minus_op(left);
3938 relation = get_inversed_relation(relation);
3940 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3942 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
3943 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3944 tv = tarval_not(tv);
3946 if (tv != tarval_bad) {
3947 left = get_Not_op(left);
3949 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3953 /* for integer modes, we have more */
3954 if (mode_is_int(mode)) {
3955 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3956 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
3957 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
3958 tv = tarval_sub(tv, get_mode_one(mode), NULL);
3960 if (tv != tarval_bad) {
3961 relation ^= ir_relation_equal;
3963 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3966 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3967 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
3968 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
3969 tv = tarval_add(tv, get_mode_one(mode));
3971 if (tv != tarval_bad) {
3972 relation ^= ir_relation_equal;
3974 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3978 /* the following reassociations work only for == and != */
3979 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3980 if (tv != tarval_bad) {
3981 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3983 ir_node *c1 = get_Sub_right(left);
3984 ir_tarval *tv2 = value_of(c1);
3986 if (tv2 != tarval_bad) {
3987 tv2 = tarval_add(tv, value_of(c1));
3989 if (tv2 != tarval_bad) {
3990 left = get_Sub_left(left);
3993 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3997 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3998 else if (is_Add(left)) {
3999 ir_node *a_l = get_Add_left(left);
4000 ir_node *a_r = get_Add_right(left);
4004 if (is_Const(a_l)) {
4006 tv2 = value_of(a_l);
4009 tv2 = value_of(a_r);
4012 if (tv2 != tarval_bad) {
4013 tv2 = tarval_sub(tv, tv2, NULL);
4015 if (tv2 != tarval_bad) {
4019 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4023 /* -a == c ==> a == -c, -a != c ==> a != -c */
4024 else if (is_Minus(left)) {
4025 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4027 if (tv2 != tarval_bad) {
4028 left = get_Minus_op(left);
4031 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4038 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4039 switch (get_irn_opcode(left)) {
4043 c1 = get_And_right(left);
4046 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4047 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4049 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4051 /* TODO: move to constant evaluation */
4052 ir_graph *irg = get_irn_irg(n);
4053 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4054 c1 = new_r_Const(irg, tv);
4055 DBG_OPT_CSTEVAL(n, c1);
4059 if (tarval_is_single_bit(tv)) {
4061 * optimization for AND:
4063 * And(x, C) == C ==> And(x, C) != 0
4064 * And(x, C) != C ==> And(X, C) == 0
4066 * if C is a single Bit constant.
4069 /* check for Constant's match. We have check hare the tarvals,
4070 because our const might be changed */
4071 if (get_Const_tarval(c1) == tv) {
4072 /* fine: do the transformation */
4073 tv = get_mode_null(get_tarval_mode(tv));
4074 relation ^= ir_relation_less_equal_greater;
4076 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4082 c1 = get_Or_right(left);
4083 if (is_Const(c1) && tarval_is_null(tv)) {
4085 * Or(x, C) == 0 && C != 0 ==> FALSE
4086 * Or(x, C) != 0 && C != 0 ==> TRUE
4088 if (! tarval_is_null(get_Const_tarval(c1))) {
4089 /* TODO: move to constant evaluation */
4090 ir_graph *irg = get_irn_irg(n);
4091 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4092 c1 = new_r_Const(irg, tv);
4093 DBG_OPT_CSTEVAL(n, c1);
4100 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4102 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4105 c1 = get_Shl_right(left);
4107 ir_graph *irg = get_irn_irg(c1);
4108 ir_tarval *tv1 = get_Const_tarval(c1);
4109 ir_mode *mode = get_irn_mode(left);
4110 ir_tarval *minus1 = get_mode_all_one(mode);
4111 ir_tarval *amask = tarval_shr(minus1, tv1);
4112 ir_tarval *cmask = tarval_shl(minus1, tv1);
4115 if (tarval_and(tv, cmask) != tv) {
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_Shl_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_shr(tv, tv1);
4127 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4132 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4134 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4137 c1 = get_Shr_right(left);
4139 ir_graph *irg = get_irn_irg(c1);
4140 ir_tarval *tv1 = get_Const_tarval(c1);
4141 ir_mode *mode = get_irn_mode(left);
4142 ir_tarval *minus1 = get_mode_all_one(mode);
4143 ir_tarval *amask = tarval_shl(minus1, tv1);
4144 ir_tarval *cmask = tarval_shr(minus1, tv1);
4147 if (tarval_and(tv, cmask) != tv) {
4148 /* condition not met */
4149 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4150 c1 = new_r_Const(irg, tv);
4151 DBG_OPT_CSTEVAL(n, c1);
4154 sl = get_Shr_left(left);
4155 blk = get_nodes_block(n);
4156 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4157 tv = tarval_shl(tv, tv1);
4159 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4164 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4166 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4169 c1 = get_Shrs_right(left);
4171 ir_graph *irg = get_irn_irg(c1);
4172 ir_tarval *tv1 = get_Const_tarval(c1);
4173 ir_mode *mode = get_irn_mode(left);
4174 ir_tarval *minus1 = get_mode_all_one(mode);
4175 ir_tarval *amask = tarval_shl(minus1, tv1);
4176 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4179 cond = tarval_sub(cond, tv1, NULL);
4180 cond = tarval_shrs(tv, cond);
4182 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4183 /* condition not met */
4184 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4185 c1 = new_r_Const(irg, tv);
4186 DBG_OPT_CSTEVAL(n, c1);
4189 sl = get_Shrs_left(left);
4190 blk = get_nodes_block(n);
4191 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4192 tv = tarval_shl(tv, tv1);
4194 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4199 } /* tarval != bad */
4202 if (changedc) { /* need a new Const */
4203 ir_graph *irg = get_irn_irg(n);
4204 right = new_r_Const(irg, tv);
4208 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4209 ir_node *op = get_Proj_pred(left);
4211 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4212 ir_node *c = get_binop_right(op);
4215 ir_tarval *tv = get_Const_tarval(c);
4217 if (tarval_is_single_bit(tv)) {
4218 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4219 ir_node *v = get_binop_left(op);
4220 ir_node *blk = get_irn_n(op, -1);
4221 ir_graph *irg = get_irn_irg(op);
4222 ir_mode *mode = get_irn_mode(v);
4224 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4225 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4227 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4234 dbg_info *dbgi = get_irn_dbg_info(n);
4235 ir_node *block = get_nodes_block(n);
4237 /* create a new compare */
4238 n = new_rd_Cmp(dbgi, block, left, right, relation);
4245 * Optimize CopyB(mem, x, x) into a Nop.
4247 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4249 ir_node *copyb = get_Proj_pred(proj);
4250 ir_node *a = get_CopyB_dst(copyb);
4251 ir_node *b = get_CopyB_src(copyb);
4254 switch (get_Proj_proj(proj)) {
4255 case pn_CopyB_X_regular:
4256 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4257 DBG_OPT_EXC_REM(proj);
4258 proj = new_r_Jmp(get_nodes_block(copyb));
4260 case pn_CopyB_X_except: {
4261 ir_graph *irg = get_irn_irg(proj);
4262 DBG_OPT_EXC_REM(proj);
4263 proj = new_r_Bad(irg, mode_X);
4271 } /* transform_node_Proj_CopyB */
4274 * Optimize Bounds(idx, idx, upper) into idx.
4276 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4278 ir_node *oldn = proj;
4279 ir_node *bound = get_Proj_pred(proj);
4280 ir_node *idx = get_Bound_index(bound);
4281 ir_node *pred = skip_Proj(idx);
4284 if (idx == get_Bound_lower(bound))
4286 else if (is_Bound(pred)) {
4288 * idx was Bounds checked previously, it is still valid if
4289 * lower <= pred_lower && pred_upper <= upper.
4291 ir_node *lower = get_Bound_lower(bound);
4292 ir_node *upper = get_Bound_upper(bound);
4293 if (get_Bound_lower(pred) == lower &&
4294 get_Bound_upper(pred) == upper) {
4296 * One could expect that we simply return the previous
4297 * Bound here. However, this would be wrong, as we could
4298 * add an exception Proj to a new location then.
4299 * So, we must turn in into a tuple.
4305 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4306 switch (get_Proj_proj(proj)) {
4308 DBG_OPT_EXC_REM(proj);
4309 proj = get_Bound_mem(bound);
4311 case pn_Bound_X_except:
4312 DBG_OPT_EXC_REM(proj);
4313 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4317 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4319 case pn_Bound_X_regular:
4320 DBG_OPT_EXC_REM(proj);
4321 proj = new_r_Jmp(get_nodes_block(bound));
4328 } /* transform_node_Proj_Bound */
4331 * Does all optimizations on nodes that must be done on its Projs
4332 * because of creating new nodes.
4334 static ir_node *transform_node_Proj(ir_node *proj)
4336 ir_node *n = get_Proj_pred(proj);
4338 if (n->op->ops.transform_node_Proj)
4339 return n->op->ops.transform_node_Proj(proj);
4341 } /* transform_node_Proj */
4343 static bool is_block_unreachable(const ir_node *block)
4345 const ir_graph *irg = get_irn_irg(block);
4346 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4348 return get_Block_dom_depth(block) < 0;
4351 static ir_node *transform_node_Block(ir_node *block)
4353 ir_graph *irg = get_irn_irg(block);
4354 int arity = get_irn_arity(block);
4355 ir_node *bad = NULL;
4358 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4361 for (i = 0; i < arity; ++i) {
4362 ir_node *pred = get_Block_cfgpred(block, i);
4363 ir_node *pred_block = get_nodes_block(pred);
4364 if (!is_Bad(pred) && !is_block_unreachable(pred_block))
4367 bad = new_r_Bad(irg, mode_X);
4368 set_irn_n(block, i, bad);
4374 static ir_node *transform_node_Phi(ir_node *phi)
4376 int n = get_irn_arity(phi);
4377 ir_mode *mode = get_irn_mode(phi);
4378 ir_node *block = get_nodes_block(phi);
4379 ir_graph *irg = get_irn_irg(phi);
4380 ir_node *bad = NULL;
4383 /* Set phi-operands for bad-block inputs to bad */
4384 for (i = 0; i < n; ++i) {
4385 ir_node *pred = get_Block_cfgpred(block, i);
4386 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4388 bad = new_r_Bad(irg, mode);
4389 set_irn_n(phi, i, bad);
4393 /* Move Confirms down through Phi nodes. */
4394 if (mode_is_reference(mode)) {
4395 n = get_irn_arity(phi);
4397 /* Beware of Phi0 */
4399 ir_node *pred = get_irn_n(phi, 0);
4400 ir_node *bound, *new_phi, *block, **in;
4401 ir_relation relation;
4403 if (! is_Confirm(pred))
4406 bound = get_Confirm_bound(pred);
4407 relation = get_Confirm_relation(pred);
4409 NEW_ARR_A(ir_node *, in, n);
4410 in[0] = get_Confirm_value(pred);
4412 for (i = 1; i < n; ++i) {
4413 pred = get_irn_n(phi, i);
4415 if (! is_Confirm(pred) ||
4416 get_Confirm_bound(pred) != bound ||
4417 get_Confirm_relation(pred) != relation)
4419 in[i] = get_Confirm_value(pred);
4421 /* move the Confirm nodes "behind" the Phi */
4422 block = get_irn_n(phi, -1);
4423 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4424 return new_r_Confirm(block, new_phi, bound, relation);
4431 * Returns the operands of a commutative bin-op, if one operand is
4432 * a const, it is returned as the second one.
4434 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4436 ir_node *op_a = get_binop_left(binop);
4437 ir_node *op_b = get_binop_right(binop);
4439 assert(is_op_commutative(get_irn_op(binop)));
4441 if (is_Const(op_a)) {
4448 } /* get_comm_Binop_Ops */
4451 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4452 * Such pattern may arise in bitfield stores.
4454 * value c4 value c4 & c2
4455 * AND c3 AND c1 | c3
4462 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4465 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4467 ir_node *irn_and, *c1;
4469 ir_node *and_l, *c3;
4470 ir_node *value, *c4;
4471 ir_node *new_and, *new_const, *block;
4472 ir_mode *mode = get_irn_mode(irn_or);
4474 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4478 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4479 if (!is_Const(c1) || !is_And(irn_and))
4482 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4486 tv1 = get_Const_tarval(c1);
4487 tv2 = get_Const_tarval(c2);
4489 tv = tarval_or(tv1, tv2);
4490 if (tarval_is_all_one(tv)) {
4491 /* the AND does NOT clear a bit with isn't set by the OR */
4492 set_Or_left(irn_or, or_l);
4493 set_Or_right(irn_or, c1);
4495 /* check for more */
4502 get_comm_Binop_Ops(or_l, &and_l, &c3);
4503 if (!is_Const(c3) || !is_And(and_l))
4506 get_comm_Binop_Ops(and_l, &value, &c4);
4510 /* ok, found the pattern, check for conditions */
4511 assert(mode == get_irn_mode(irn_and));
4512 assert(mode == get_irn_mode(or_l));
4513 assert(mode == get_irn_mode(and_l));
4515 tv3 = get_Const_tarval(c3);
4516 tv4 = get_Const_tarval(c4);
4518 tv = tarval_or(tv4, tv2);
4519 if (!tarval_is_all_one(tv)) {
4520 /* have at least one 0 at the same bit position */
4524 if (tv3 != tarval_andnot(tv3, tv4)) {
4525 /* bit in the or_mask is outside the and_mask */
4529 if (tv1 != tarval_andnot(tv1, tv2)) {
4530 /* bit in the or_mask is outside the and_mask */
4534 /* ok, all conditions met */
4535 block = get_irn_n(irn_or, -1);
4536 irg = get_irn_irg(block);
4538 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4540 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4542 set_Or_left(irn_or, new_and);
4543 set_Or_right(irn_or, new_const);
4545 /* check for more */
4547 } /* transform_node_Or_bf_store */
4550 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4552 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4554 ir_mode *mode = get_irn_mode(irn_or);
4555 ir_node *shl, *shr, *block;
4556 ir_node *irn, *x, *c1, *c2, *n;
4557 ir_tarval *tv1, *tv2;
4559 /* some backends can't handle rotl */
4560 if (!be_get_backend_param()->support_rotl)
4563 if (! mode_is_int(mode))
4566 shl = get_binop_left(irn_or);
4567 shr = get_binop_right(irn_or);
4576 } else if (!is_Shl(shl)) {
4578 } else if (!is_Shr(shr)) {
4581 x = get_Shl_left(shl);
4582 if (x != get_Shr_left(shr))
4585 c1 = get_Shl_right(shl);
4586 c2 = get_Shr_right(shr);
4587 if (is_Const(c1) && is_Const(c2)) {
4588 tv1 = get_Const_tarval(c1);
4589 if (! tarval_is_long(tv1))
4592 tv2 = get_Const_tarval(c2);
4593 if (! tarval_is_long(tv2))
4596 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4597 != (int) get_mode_size_bits(mode))
4600 /* yet, condition met */
4601 block = get_nodes_block(irn_or);
4603 n = new_r_Rotl(block, x, c1, mode);
4605 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4609 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4610 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4611 if (!ir_is_negated_value(c1, c2)) {
4615 /* yet, condition met */
4616 block = get_nodes_block(irn_or);
4617 n = new_r_Rotl(block, x, c1, mode);
4618 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4620 } /* transform_node_Or_Rotl */
4622 static bool is_cmp_unequal_zero(const ir_node *node)
4624 ir_relation relation = get_Cmp_relation(node);
4625 ir_node *left = get_Cmp_left(node);
4626 ir_node *right = get_Cmp_right(node);
4627 ir_mode *mode = get_irn_mode(left);
4629 if (!is_Const(right) || !is_Const_null(right))
4631 if (mode_is_signed(mode)) {
4632 return relation == ir_relation_less_greater;
4634 return relation == ir_relation_greater;
4641 static ir_node *transform_node_Or(ir_node *n)
4643 ir_node *c, *oldn = n;
4644 ir_node *a = get_Or_left(n);
4645 ir_node *b = get_Or_right(n);
4648 if (is_Not(a) && is_Not(b)) {
4649 /* ~a | ~b = ~(a&b) */
4650 ir_node *block = get_nodes_block(n);
4652 mode = get_irn_mode(n);
4655 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4656 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4657 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4661 /* we can combine the relations of two compares with the same operands */
4662 if (is_Cmp(a) && is_Cmp(b)) {
4663 ir_node *a_left = get_Cmp_left(a);
4664 ir_node *a_right = get_Cmp_left(a);
4665 ir_node *b_left = get_Cmp_left(b);
4666 ir_node *b_right = get_Cmp_right(b);
4667 if (a_left == b_left && b_left == b_right) {
4668 dbg_info *dbgi = get_irn_dbg_info(n);
4669 ir_node *block = get_nodes_block(n);
4670 ir_relation a_relation = get_Cmp_relation(a);
4671 ir_relation b_relation = get_Cmp_relation(b);
4672 ir_relation new_relation = a_relation | b_relation;
4673 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4675 /* Cmp(a!=0) or Cmp(b!=0) => Cmp(a|b != 0) */
4676 if (is_cmp_unequal_zero(a) && is_cmp_unequal_zero(b)
4677 && !mode_is_float(get_irn_mode(a_left))
4678 && !mode_is_float(get_irn_mode(b_left))) {
4679 ir_graph *irg = get_irn_irg(n);
4680 dbg_info *dbgi = get_irn_dbg_info(n);
4681 ir_node *block = get_nodes_block(n);
4682 ir_mode *mode = get_irn_mode(a_left);
4683 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
4684 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
4685 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
4686 ir_node *zero = create_zero_const(irg, mode);
4687 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4691 mode = get_irn_mode(n);
4692 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4694 n = transform_node_Or_bf_store(n);
4695 n = transform_node_Or_Rotl(n);
4699 n = transform_bitwise_distributive(n, transform_node_Or);
4702 } /* transform_node_Or */
4706 static ir_node *transform_node(ir_node *n);
4709 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4711 * Should be moved to reassociation?
4713 static ir_node *transform_node_shift(ir_node *n)
4715 ir_node *left, *right;
4717 ir_tarval *tv1, *tv2, *res;
4718 ir_node *in[2], *irn, *block;
4721 left = get_binop_left(n);
4723 /* different operations */
4724 if (get_irn_op(left) != get_irn_op(n))
4727 right = get_binop_right(n);
4728 tv1 = value_of(right);
4729 if (tv1 == tarval_bad)
4732 tv2 = value_of(get_binop_right(left));
4733 if (tv2 == tarval_bad)
4736 res = tarval_add(tv1, tv2);
4737 mode = get_irn_mode(n);
4738 irg = get_irn_irg(n);
4740 /* beware: a simple replacement works only, if res < modulo shift */
4742 int modulo_shf = get_mode_modulo_shift(mode);
4743 if (modulo_shf > 0) {
4744 ir_tarval *modulo = new_tarval_from_long(modulo_shf,
4745 get_tarval_mode(res));
4747 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4749 /* shifting too much */
4750 if (!(tarval_cmp(res, modulo) & ir_relation_less)) {
4752 ir_node *block = get_nodes_block(n);
4753 dbg_info *dbgi = get_irn_dbg_info(n);
4754 ir_mode *smode = get_irn_mode(right);
4755 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4756 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4759 return new_r_Const(irg, get_mode_null(mode));
4763 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4766 /* ok, we can replace it */
4767 block = get_nodes_block(n);
4769 in[0] = get_binop_left(left);
4770 in[1] = new_r_Const(irg, res);
4772 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4774 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4776 return transform_node(irn);
4777 } /* transform_node_shift */
4780 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4782 * - and, or, xor instead of &
4783 * - Shl, Shr, Shrs, rotl instead of >>
4784 * (with a special case for Or/Xor + Shrs)
4786 static ir_node *transform_node_bitop_shift(ir_node *n)
4789 ir_node *right = get_binop_right(n);
4790 ir_mode *mode = get_irn_mode(n);
4791 ir_node *bitop_left;
4792 ir_node *bitop_right;
4802 ir_tarval *tv_shift;
4804 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4806 if (!is_Const(right))
4809 left = get_binop_left(n);
4810 op_left = get_irn_op(left);
4811 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4814 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4815 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4816 /* TODO: test if sign bit is affectes */
4820 bitop_right = get_binop_right(left);
4821 if (!is_Const(bitop_right))
4824 bitop_left = get_binop_left(left);
4826 block = get_nodes_block(n);
4827 dbgi = get_irn_dbg_info(n);
4828 tv1 = get_Const_tarval(bitop_right);
4829 tv2 = get_Const_tarval(right);
4831 assert(get_tarval_mode(tv1) == mode);
4834 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
4835 tv_shift = tarval_shl(tv1, tv2);
4836 } else if (is_Shr(n)) {
4837 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
4838 tv_shift = tarval_shr(tv1, tv2);
4839 } else if (is_Shrs(n)) {
4840 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
4841 tv_shift = tarval_shrs(tv1, tv2);
4844 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
4845 tv_shift = tarval_rotl(tv1, tv2);
4848 assert(get_tarval_mode(tv_shift) == mode);
4849 irg = get_irn_irg(n);
4850 new_const = new_r_Const(irg, tv_shift);
4852 if (op_left == op_And) {
4853 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
4854 } else if (op_left == op_Or) {
4855 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
4857 assert(op_left == op_Eor);
4858 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
4866 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4868 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4869 * (also with x >>s c1 when c1>=c2)
4871 static ir_node *transform_node_shl_shr(ir_node *n)
4874 ir_node *right = get_binop_right(n);
4884 ir_tarval *tv_shift;
4887 ir_relation relation;
4890 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4892 if (!is_Const(right))
4895 left = get_binop_left(n);
4896 mode = get_irn_mode(n);
4897 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4898 ir_node *shr_right = get_binop_right(left);
4900 if (!is_Const(shr_right))
4903 x = get_binop_left(left);
4904 tv_shr = get_Const_tarval(shr_right);
4905 tv_shl = get_Const_tarval(right);
4907 if (is_Shrs(left)) {
4908 /* shrs variant only allowed if c1 >= c2 */
4909 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
4912 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4915 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4917 tv_mask = tarval_shl(tv_mask, tv_shl);
4918 } else if (is_Shr(n) && is_Shl(left)) {
4919 ir_node *shl_right = get_Shl_right(left);
4921 if (!is_Const(shl_right))
4924 x = get_Shl_left(left);
4925 tv_shr = get_Const_tarval(right);
4926 tv_shl = get_Const_tarval(shl_right);
4928 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4929 tv_mask = tarval_shr(tv_mask, tv_shr);
4934 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
4935 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
4938 assert(tv_mask != tarval_bad);
4939 assert(get_tarval_mode(tv_mask) == mode);
4941 block = get_nodes_block(n);
4942 irg = get_irn_irg(block);
4943 dbgi = get_irn_dbg_info(n);
4945 relation = tarval_cmp(tv_shl, tv_shr);
4946 if (relation == ir_relation_less || relation == ir_relation_equal) {
4947 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4948 new_const = new_r_Const(irg, tv_shift);
4950 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
4952 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
4955 assert(relation == ir_relation_greater);
4956 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4957 new_const = new_r_Const(irg, tv_shift);
4958 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
4961 new_const = new_r_Const(irg, tv_mask);
4962 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
4967 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
4969 ir_mode *mode = get_tarval_mode(tv);
4970 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
4971 return tarval_mod(tv, modulo_tv);
4974 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
4975 ir_node *left, ir_node *right, ir_mode *mode);
4978 * Normalisation: if we have a shl/shr with modulo_shift behaviour
4979 * then we can use that to minimize the value of Add(x, const) or
4980 * Sub(Const, x). In particular this often avoids 1 instruction in some
4981 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
4982 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
4984 static ir_node *transform_node_shift_modulo(ir_node *n,
4985 new_shift_func new_shift)
4987 ir_mode *mode = get_irn_mode(n);
4988 int modulo = get_mode_modulo_shift(mode);
4989 ir_node *newop = NULL;
4990 ir_mode *mode_right;
4997 if (get_mode_arithmetic(mode) != irma_twos_complement)
4999 if (!is_po2(modulo))
5002 irg = get_irn_irg(n);
5003 block = get_nodes_block(n);
5004 right = get_binop_right(n);
5005 mode_right = get_irn_mode(right);
5006 if (is_Const(right)) {
5007 ir_tarval *tv = get_Const_tarval(right);
5008 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5013 newop = new_r_Const(irg, tv_mod);
5014 } else if (is_Add(right)) {
5015 ir_node *add_right = get_Add_right(right);
5016 if (is_Const(add_right)) {
5017 ir_tarval *tv = get_Const_tarval(add_right);
5018 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5023 newconst = new_r_Const(irg, tv_mod);
5024 newop = new_r_Add(block, get_Add_left(right), newconst,
5027 } else if (is_Sub(right)) {
5028 ir_node *sub_left = get_Sub_left(right);
5029 if (is_Const(sub_left)) {
5030 ir_tarval *tv = get_Const_tarval(sub_left);
5031 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5036 newconst = new_r_Const(irg, tv_mod);
5037 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5044 if (newop != NULL) {
5045 dbg_info *dbgi = get_irn_dbg_info(n);
5046 ir_node *left = get_binop_left(n);
5047 return new_shift(dbgi, block, left, newop, mode);
5055 static ir_node *transform_node_Shr(ir_node *n)
5057 ir_node *c, *oldn = n;
5058 ir_node *left = get_Shr_left(n);
5059 ir_node *right = get_Shr_right(n);
5060 ir_mode *mode = get_irn_mode(n);
5062 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5063 n = transform_node_shift(n);
5066 n = transform_node_shift_modulo(n, new_rd_Shr);
5068 n = transform_node_shl_shr(n);
5070 n = transform_node_bitop_shift(n);
5073 } /* transform_node_Shr */
5078 static ir_node *transform_node_Shrs(ir_node *n)
5080 ir_node *c, *oldn = n;
5081 ir_node *a = get_Shrs_left(n);
5082 ir_node *b = get_Shrs_right(n);
5083 ir_mode *mode = get_irn_mode(n);
5085 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5086 n = transform_node_shift(n);
5089 n = transform_node_shift_modulo(n, new_rd_Shrs);
5091 n = transform_node_bitop_shift(n);
5094 } /* transform_node_Shrs */
5099 static ir_node *transform_node_Shl(ir_node *n)
5101 ir_node *c, *oldn = n;
5102 ir_node *a = get_Shl_left(n);
5103 ir_node *b = get_Shl_right(n);
5104 ir_mode *mode = get_irn_mode(n);
5106 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5107 n = transform_node_shift(n);
5110 n = transform_node_shift_modulo(n, new_rd_Shl);
5112 n = transform_node_shl_shr(n);
5114 n = transform_node_bitop_shift(n);
5117 } /* transform_node_Shl */
5122 static ir_node *transform_node_Rotl(ir_node *n)
5124 ir_node *c, *oldn = n;
5125 ir_node *a = get_Rotl_left(n);
5126 ir_node *b = get_Rotl_right(n);
5127 ir_mode *mode = get_irn_mode(n);
5129 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5130 n = transform_node_shift(n);
5133 n = transform_node_bitop_shift(n);
5136 } /* transform_node_Rotl */
5141 static ir_node *transform_node_Conv(ir_node *n)
5143 ir_node *c, *oldn = n;
5144 ir_mode *mode = get_irn_mode(n);
5145 ir_node *a = get_Conv_op(n);
5147 if (mode != mode_b && is_const_Phi(a)) {
5148 /* Do NOT optimize mode_b Conv's, this leads to remaining
5149 * Phib nodes later, because the conv_b_lower operation
5150 * is instantly reverted, when it tries to insert a Convb.
5152 c = apply_conv_on_phi(a, mode);
5154 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5159 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5160 ir_graph *irg = get_irn_irg(n);
5161 return new_r_Unknown(irg, mode);
5164 if (mode_is_reference(mode) &&
5165 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5167 ir_node *l = get_Add_left(a);
5168 ir_node *r = get_Add_right(a);
5169 dbg_info *dbgi = get_irn_dbg_info(a);
5170 ir_node *block = get_nodes_block(n);
5172 ir_node *lop = get_Conv_op(l);
5173 if (get_irn_mode(lop) == mode) {
5174 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5175 n = new_rd_Add(dbgi, block, lop, r, mode);
5180 ir_node *rop = get_Conv_op(r);
5181 if (get_irn_mode(rop) == mode) {
5182 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5183 n = new_rd_Add(dbgi, block, l, rop, mode);
5190 } /* transform_node_Conv */
5193 * Remove dead blocks and nodes in dead blocks
5194 * in keep alive list. We do not generate a new End node.
5196 static ir_node *transform_node_End(ir_node *n)
5198 int i, j, n_keepalives = get_End_n_keepalives(n);
5201 NEW_ARR_A(ir_node *, in, n_keepalives);
5203 for (i = j = 0; i < n_keepalives; ++i) {
5204 ir_node *ka = get_End_keepalive(n, i);
5206 /* no need to keep Bad */
5209 /* dont keep unreachable code */
5210 block = is_Block(ka) ? ka : get_nodes_block(ka);
5211 if (is_block_unreachable(block))
5215 if (j != n_keepalives)
5216 set_End_keepalives(n, j, in);
5218 } /* transform_node_End */
5220 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5222 if (is_Minus(a) && get_Minus_op(a) == b)
5224 if (is_Minus(b) && get_Minus_op(b) == a)
5226 if (is_Sub(a) && is_Sub(b)) {
5227 ir_node *a_left = get_Sub_left(a);
5228 ir_node *a_right = get_Sub_right(a);
5229 ir_node *b_left = get_Sub_left(b);
5230 ir_node *b_right = get_Sub_right(b);
5232 if (a_left == b_right && a_right == b_left)
5240 * Optimize a Mux into some simpler cases.
5242 static ir_node *transform_node_Mux(ir_node *n)
5244 ir_node *oldn = n, *sel = get_Mux_sel(n);
5245 ir_mode *mode = get_irn_mode(n);
5246 ir_node *t = get_Mux_true(n);
5247 ir_node *f = get_Mux_false(n);
5248 ir_graph *irg = get_irn_irg(n);
5250 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5254 ir_node* block = get_nodes_block(n);
5256 ir_node* c1 = get_Mux_sel(t);
5257 ir_node* t1 = get_Mux_true(t);
5258 ir_node* f1 = get_Mux_false(t);
5260 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5261 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5262 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5267 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5268 } else if (f == t1) {
5269 /* Mux(cond0, Mux(cond1, x, y), x) */
5270 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5271 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5272 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5277 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5279 } else if (is_Mux(f)) {
5280 ir_node* block = get_nodes_block(n);
5282 ir_node* c1 = get_Mux_sel(f);
5283 ir_node* t1 = get_Mux_true(f);
5284 ir_node* f1 = get_Mux_false(f);
5286 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5287 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5288 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5293 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5294 } else if (t == f1) {
5295 /* Mux(cond0, x, Mux(cond1, y, x)) */
5296 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5297 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5298 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5303 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5307 /* first normalization step: try to move a constant to the false side,
5308 * 0 preferred on false side too */
5309 if (is_Cmp(sel) && is_Const(t) &&
5310 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5311 dbg_info *seldbgi = get_irn_dbg_info(sel);
5312 ir_node *block = get_nodes_block(sel);
5313 ir_relation relation = get_Cmp_relation(sel);
5318 /* Mux(x, a, b) => Mux(not(x), b, a) */
5319 relation = get_negated_relation(relation);
5320 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5321 get_Cmp_right(sel), relation);
5322 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5325 /* note: after normalization, false can only happen on default */
5326 if (mode == mode_b) {
5327 dbg_info *dbg = get_irn_dbg_info(n);
5328 ir_node *block = get_nodes_block(n);
5331 ir_tarval *tv_t = get_Const_tarval(t);
5332 if (tv_t == tarval_b_true) {
5334 /* Muxb(sel, true, false) = sel */
5335 assert(get_Const_tarval(f) == tarval_b_false);
5336 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5339 /* Muxb(sel, true, x) = Or(sel, x) */
5340 n = new_rd_Or(dbg, block, sel, f, mode_b);
5341 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5345 } else if (is_Const(f)) {
5346 ir_tarval *tv_f = get_Const_tarval(f);
5347 if (tv_f == tarval_b_true) {
5348 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5349 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5350 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5351 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5354 /* Muxb(sel, x, false) = And(sel, x) */
5355 assert(tv_f == tarval_b_false);
5356 n = new_rd_And(dbg, block, sel, t, mode_b);
5357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5363 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5364 * value to integer. */
5365 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5366 ir_tarval *a = get_Const_tarval(t);
5367 ir_tarval *b = get_Const_tarval(f);
5369 if (tarval_is_one(a) && tarval_is_null(b)) {
5370 ir_node *block = get_nodes_block(n);
5371 ir_node *conv = new_r_Conv(block, sel, mode);
5373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5375 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5376 ir_node *block = get_nodes_block(n);
5377 ir_node *not_ = new_r_Not(block, sel, mode_b);
5378 ir_node *conv = new_r_Conv(block, not_, mode);
5380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5386 ir_node *cmp_r = get_Cmp_right(sel);
5387 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5388 ir_node *block = get_nodes_block(n);
5389 ir_node *cmp_l = get_Cmp_left(sel);
5391 if (mode_is_int(mode)) {
5392 ir_relation relation = get_Cmp_relation(sel);
5394 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5395 /* Mux((a & b) != 0, c, 0) */
5396 ir_node *and_r = get_And_right(cmp_l);
5399 if (and_r == t && f == cmp_r) {
5400 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5401 if (relation == ir_relation_less_greater) {
5402 /* Mux((a & 2^C) != 0, 2^C, 0) */
5404 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5406 /* Mux((a & 2^C) == 0, 2^C, 0) */
5407 n = new_rd_Eor(get_irn_dbg_info(n),
5408 block, cmp_l, t, mode);
5409 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5414 if (is_Shl(and_r)) {
5415 ir_node *shl_l = get_Shl_left(and_r);
5416 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5417 if (and_r == t && f == cmp_r) {
5418 if (relation == ir_relation_less_greater) {
5419 /* (a & (1 << n)) != 0, (1 << n), 0) */
5421 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5423 /* (a & (1 << n)) == 0, (1 << n), 0) */
5424 n = new_rd_Eor(get_irn_dbg_info(n),
5425 block, cmp_l, t, mode);
5426 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5432 and_l = get_And_left(cmp_l);
5433 if (is_Shl(and_l)) {
5434 ir_node *shl_l = get_Shl_left(and_l);
5435 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5436 if (and_l == t && f == cmp_r) {
5437 if (relation == ir_relation_less_greater) {
5438 /* ((1 << n) & a) != 0, (1 << n), 0) */
5440 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5442 /* ((1 << n) & a) == 0, (1 << n), 0) */
5443 n = new_rd_Eor(get_irn_dbg_info(n),
5444 block, cmp_l, t, mode);
5445 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5460 * optimize Sync nodes that have other syncs as input we simply add the inputs
5461 * of the other sync to our own inputs
5463 static ir_node *transform_node_Sync(ir_node *n)
5465 int arity = get_Sync_n_preds(n);
5468 for (i = 0; i < arity;) {
5469 ir_node *pred = get_Sync_pred(n, i);
5473 /* Remove Bad predecessors */
5480 /* Remove duplicate predecessors */
5481 for (j = 0; j < i; ++j) {
5482 if (get_Sync_pred(n, j) == pred) {
5491 if (!is_Sync(pred)) {
5499 pred_arity = get_Sync_n_preds(pred);
5500 for (j = 0; j < pred_arity; ++j) {
5501 ir_node *pred_pred = get_Sync_pred(pred, j);
5506 add_irn_n(n, pred_pred);
5510 if (get_Sync_pred(n, k) == pred_pred) break;
5516 ir_graph *irg = get_irn_irg(n);
5517 return new_r_Bad(irg, mode_M);
5520 return get_Sync_pred(n, 0);
5523 /* rehash the sync node */
5528 static ir_node *transform_node_Load(ir_node *n)
5530 /* if our memory predecessor is a load from the same address, then reuse the
5531 * previous result */
5532 ir_node *mem = get_Load_mem(n);
5537 /* don't touch volatile loads */
5538 if (get_Load_volatility(n) == volatility_is_volatile)
5540 mem_pred = get_Proj_pred(mem);
5541 if (is_Load(mem_pred)) {
5542 ir_node *pred_load = mem_pred;
5544 /* conservatively compare the 2 loads. TODO: This could be less strict
5545 * with fixup code in some situations (like smaller/bigger modes) */
5546 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5548 if (get_Load_mode(pred_load) != get_Load_mode(n))
5550 /* all combinations of aligned/unaligned pred/n should be fine so we do
5551 * not compare the unaligned attribute */
5553 ir_node *block = get_nodes_block(n);
5554 ir_node *jmp = new_r_Jmp(block);
5555 ir_graph *irg = get_irn_irg(n);
5556 ir_node *bad = new_r_Bad(irg, mode_X);
5557 ir_mode *mode = get_Load_mode(n);
5558 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5559 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5560 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5563 } else if (is_Store(mem_pred)) {
5564 ir_node *pred_store = mem_pred;
5565 ir_node *value = get_Store_value(pred_store);
5567 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5569 if (get_irn_mode(value) != get_Load_mode(n))
5571 /* all combinations of aligned/unaligned pred/n should be fine so we do
5572 * not compare the unaligned attribute */
5574 ir_node *block = get_nodes_block(n);
5575 ir_node *jmp = new_r_Jmp(block);
5576 ir_graph *irg = get_irn_irg(n);
5577 ir_node *bad = new_r_Bad(irg, mode_X);
5578 ir_node *res = value;
5579 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5580 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5589 * optimize a trampoline Call into a direct Call
5591 static ir_node *transform_node_Call(ir_node *call)
5593 ir_node *callee = get_Call_ptr(call);
5594 ir_node *adr, *mem, *res, *bl, **in;
5595 ir_type *ctp, *mtp, *tp;
5599 size_t i, n_res, n_param;
5602 if (! is_Proj(callee))
5604 callee = get_Proj_pred(callee);
5605 if (! is_Builtin(callee))
5607 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5610 mem = get_Call_mem(call);
5612 if (skip_Proj(mem) == callee) {
5613 /* memory is routed to the trampoline, skip */
5614 mem = get_Builtin_mem(callee);
5617 /* build a new call type */
5618 mtp = get_Call_type(call);
5619 tdb = get_type_dbg_info(mtp);
5621 n_res = get_method_n_ress(mtp);
5622 n_param = get_method_n_params(mtp);
5623 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5625 for (i = 0; i < n_res; ++i)
5626 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5628 NEW_ARR_A(ir_node *, in, n_param + 1);
5630 /* FIXME: we don't need a new pointer type in every step */
5631 irg = get_irn_irg(call);
5632 tp = get_irg_frame_type(irg);
5633 tp = new_type_pointer(tp);
5634 set_method_param_type(ctp, 0, tp);
5636 in[0] = get_Builtin_param(callee, 2);
5637 for (i = 0; i < n_param; ++i) {
5638 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5639 in[i + 1] = get_Call_param(call, i);
5641 var = get_method_variadicity(mtp);
5642 set_method_variadicity(ctp, var);
5643 /* When we resolve a trampoline, the function must be called by a this-call */
5644 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5645 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5647 adr = get_Builtin_param(callee, 1);
5649 db = get_irn_dbg_info(call);
5650 bl = get_nodes_block(call);
5652 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5653 if (get_irn_pinned(call) == op_pin_state_floats)
5654 set_irn_pinned(res, op_pin_state_floats);
5656 } /* transform_node_Call */
5659 * Tries several [inplace] [optimizing] transformations and returns an
5660 * equivalent node. The difference to equivalent_node() is that these
5661 * transformations _do_ generate new nodes, and thus the old node must
5662 * not be freed even if the equivalent node isn't the old one.
5664 static ir_node *transform_node(ir_node *n)
5669 * Transform_node is the only "optimizing transformation" that might
5670 * return a node with a different opcode. We iterate HERE until fixpoint
5671 * to get the final result.
5675 if (n->op->ops.transform_node != NULL)
5676 n = n->op->ops.transform_node(n);
5677 } while (oldn != n);
5680 } /* transform_node */
5683 * Sets the default transform node operation for an ir_op_ops.
5685 * @param code the opcode for the default operation
5686 * @param ops the operations initialized
5691 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5695 ops->transform_node = transform_node_##a; \
5697 #define CASE_PROJ(a) \
5699 ops->transform_node_Proj = transform_node_Proj_##a; \
5701 #define CASE_PROJ_EX(a) \
5703 ops->transform_node = transform_node_##a; \
5704 ops->transform_node_Proj = transform_node_Proj_##a; \
5745 } /* firm_set_default_transform_node */
5748 /* **************** Common Subexpression Elimination **************** */
5750 /** The size of the hash table used, should estimate the number of nodes
5752 #define N_IR_NODES 512
5754 /** Compares the attributes of two Const nodes. */
5755 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
5757 return get_Const_tarval(a) != get_Const_tarval(b);
5760 /** Compares the attributes of two Proj nodes. */
5761 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
5763 return a->attr.proj.proj != b->attr.proj.proj;
5766 /** Compares the attributes of two Alloc nodes. */
5767 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
5769 const alloc_attr *pa = &a->attr.alloc;
5770 const alloc_attr *pb = &b->attr.alloc;
5771 return (pa->where != pb->where) || (pa->type != pb->type);
5774 /** Compares the attributes of two Free nodes. */
5775 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
5777 const free_attr *pa = &a->attr.free;
5778 const free_attr *pb = &b->attr.free;
5779 return (pa->where != pb->where) || (pa->type != pb->type);
5782 /** Compares the attributes of two SymConst nodes. */
5783 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
5785 const symconst_attr *pa = &a->attr.symc;
5786 const symconst_attr *pb = &b->attr.symc;
5787 return (pa->kind != pb->kind)
5788 || (pa->sym.type_p != pb->sym.type_p);
5791 /** Compares the attributes of two Call nodes. */
5792 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
5794 const call_attr *pa = &a->attr.call;
5795 const call_attr *pb = &b->attr.call;
5796 return (pa->type != pb->type)
5797 || (pa->tail_call != pb->tail_call);
5800 /** Compares the attributes of two Sel nodes. */
5801 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
5803 const ir_entity *a_ent = get_Sel_entity(a);
5804 const ir_entity *b_ent = get_Sel_entity(b);
5805 return a_ent != b_ent;
5808 /** Compares the attributes of two Phi nodes. */
5809 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
5811 /* we can only enter this function if both nodes have the same number of inputs,
5812 hence it is enough to check if one of them is a Phi0 */
5814 /* check the Phi0 pos attribute */
5815 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5820 /** Compares the attributes of two Conv nodes. */
5821 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
5823 return get_Conv_strict(a) != get_Conv_strict(b);
5826 /** Compares the attributes of two Cast nodes. */
5827 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
5829 return get_Cast_type(a) != get_Cast_type(b);
5832 /** Compares the attributes of two Load nodes. */
5833 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
5835 if (get_Load_volatility(a) == volatility_is_volatile ||
5836 get_Load_volatility(b) == volatility_is_volatile)
5837 /* NEVER do CSE on volatile Loads */
5839 /* do not CSE Loads with different alignment. Be conservative. */
5840 if (get_Load_unaligned(a) != get_Load_unaligned(b))
5843 return get_Load_mode(a) != get_Load_mode(b);
5846 /** Compares the attributes of two Store nodes. */
5847 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
5849 /* do not CSE Stores with different alignment. Be conservative. */
5850 if (get_Store_unaligned(a) != get_Store_unaligned(b))
5853 /* NEVER do CSE on volatile Stores */
5854 return (get_Store_volatility(a) == volatility_is_volatile ||
5855 get_Store_volatility(b) == volatility_is_volatile);
5858 /** Compares two exception attributes */
5859 static int node_cmp_exception(const ir_node *a, const ir_node *b)
5861 const except_attr *ea = &a->attr.except;
5862 const except_attr *eb = &b->attr.except;
5864 return ea->pin_state != eb->pin_state;
5867 #define node_cmp_attr_Bound node_cmp_exception
5869 /** Compares the attributes of two Div nodes. */
5870 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
5872 const div_attr *ma = &a->attr.div;
5873 const div_attr *mb = &b->attr.div;
5874 return ma->exc.pin_state != mb->exc.pin_state ||
5875 ma->resmode != mb->resmode ||
5876 ma->no_remainder != mb->no_remainder;
5879 /** Compares the attributes of two Mod nodes. */
5880 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
5882 const mod_attr *ma = &a->attr.mod;
5883 const mod_attr *mb = &b->attr.mod;
5884 return ma->exc.pin_state != mb->exc.pin_state ||
5885 ma->resmode != mb->resmode;
5888 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
5890 const cmp_attr *ma = &a->attr.cmp;
5891 const cmp_attr *mb = &b->attr.cmp;
5892 return ma->relation != mb->relation;
5895 /** Compares the attributes of two Confirm nodes. */
5896 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
5898 const confirm_attr *ma = &a->attr.confirm;
5899 const confirm_attr *mb = &b->attr.confirm;
5900 return ma->relation != mb->relation;
5903 /** Compares the attributes of two Builtin nodes. */
5904 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
5906 /* no need to compare the type, equal kind means equal type */
5907 return get_Builtin_kind(a) != get_Builtin_kind(b);
5910 /** Compares the attributes of two ASM nodes. */
5911 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
5914 const ir_asm_constraint *ca;
5915 const ir_asm_constraint *cb;
5918 if (get_ASM_text(a) != get_ASM_text(b))
5921 /* Should we really check the constraints here? Should be better, but is strange. */
5922 n = get_ASM_n_input_constraints(a);
5923 if (n != get_ASM_n_input_constraints(b))
5926 ca = get_ASM_input_constraints(a);
5927 cb = get_ASM_input_constraints(b);
5928 for (i = 0; i < n; ++i) {
5929 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5930 || ca[i].mode != cb[i].mode)
5934 n = get_ASM_n_output_constraints(a);
5935 if (n != get_ASM_n_output_constraints(b))
5938 ca = get_ASM_output_constraints(a);
5939 cb = get_ASM_output_constraints(b);
5940 for (i = 0; i < n; ++i) {
5941 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5942 || ca[i].mode != cb[i].mode)
5946 n = get_ASM_n_clobbers(a);
5947 if (n != get_ASM_n_clobbers(b))
5950 cla = get_ASM_clobbers(a);
5951 clb = get_ASM_clobbers(b);
5952 for (i = 0; i < n; ++i) {
5953 if (cla[i] != clb[i])
5959 /** Compares the inexistent attributes of two Dummy nodes. */
5960 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
5968 * Set the default node attribute compare operation for an ir_op_ops.
5970 * @param code the opcode for the default operation
5971 * @param ops the operations initialized
5976 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5980 ops->node_cmp_attr = node_cmp_attr_##a; \
6012 } /* firm_set_default_node_cmp_attr */
6015 * Compare function for two nodes in the value table. Gets two
6016 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6018 int identities_cmp(const void *elt, const void *key)
6020 ir_node *a = (ir_node *)elt;
6021 ir_node *b = (ir_node *)key;
6024 if (a == b) return 0;
6026 if ((get_irn_op(a) != get_irn_op(b)) ||
6027 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6029 /* compare if a's in and b's in are of equal length */
6030 irn_arity_a = get_irn_arity(a);
6031 if (irn_arity_a != get_irn_arity(b))
6034 /* blocks are never the same */
6038 if (get_irn_pinned(a) == op_pin_state_pinned) {
6039 /* for pinned nodes, the block inputs must be equal */
6040 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6042 } else if (! get_opt_global_cse()) {
6043 /* for block-local CSE both nodes must be in the same Block */
6044 if (get_nodes_block(a) != get_nodes_block(b))
6048 /* compare a->in[0..ins] with b->in[0..ins] */
6049 for (i = 0; i < irn_arity_a; ++i) {
6050 ir_node *pred_a = get_irn_n(a, i);
6051 ir_node *pred_b = get_irn_n(b, i);
6052 if (pred_a != pred_b) {
6053 /* if both predecessors are CSE neutral they might be different */
6054 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6060 * here, we already now that the nodes are identical except their
6063 if (a->op->ops.node_cmp_attr)
6064 return a->op->ops.node_cmp_attr(a, b);
6067 } /* identities_cmp */
6070 * Calculate a hash value of a node.
6072 * @param node The IR-node
6074 unsigned ir_node_hash(const ir_node *node)
6076 return node->op->ops.hash(node);
6077 } /* ir_node_hash */
6080 void new_identities(ir_graph *irg)
6082 if (irg->value_table != NULL)
6083 del_pset(irg->value_table);
6084 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6085 } /* new_identities */
6087 void del_identities(ir_graph *irg)
6089 if (irg->value_table != NULL)
6090 del_pset(irg->value_table);
6091 } /* del_identities */
6093 /* Normalize a node by putting constants (and operands with larger
6094 * node index) on the right (operator side). */
6095 void ir_normalize_node(ir_node *n)
6097 if (is_op_commutative(get_irn_op(n))) {
6098 ir_node *l = get_binop_left(n);
6099 ir_node *r = get_binop_right(n);
6101 /* For commutative operators perform a OP b == b OP a but keep
6102 * constants on the RIGHT side. This helps greatly in some
6103 * optimizations. Moreover we use the idx number to make the form
6105 if (!operands_are_normalized(l, r)) {
6106 set_binop_left(n, r);
6107 set_binop_right(n, l);
6111 } /* ir_normalize_node */
6114 * Return the canonical node computing the same value as n.
6115 * Looks up the node in a hash table, enters it in the table
6116 * if it isn't there yet.
6118 * @param n the node to look up
6120 * @return a node that computes the same value as n or n if no such
6121 * node could be found
6123 ir_node *identify_remember(ir_node *n)
6125 ir_graph *irg = get_irn_irg(n);
6126 pset *value_table = irg->value_table;
6129 if (value_table == NULL)
6132 if (get_opt_global_cse()) {
6133 /* do not remember unreachable nodes */
6135 ir_node *block = get_nodes_block(n);
6136 if (is_block_unreachable(block)) {
6142 ir_normalize_node(n);
6143 /* lookup or insert in hash table with given hash key. */
6144 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6147 /* n is reachable again */
6148 edges_node_revival(nn);
6152 } /* identify_remember */
6155 * During construction we set the op_pin_state_pinned flag in the graph right
6156 * when the optimization is performed. The flag turning on procedure global
6157 * cse could be changed between two allocations. This way we are safe.
6159 * @param n The node to lookup
6161 static inline ir_node *identify_cons(ir_node *n)
6165 n = identify_remember(n);
6166 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6167 ir_graph *irg = get_irn_irg(n);
6168 set_irg_pinned(irg, op_pin_state_floats);
6171 } /* identify_cons */
6173 /* Add a node to the identities value table. */
6174 void add_identities(ir_node *node)
6181 identify_remember(node);
6184 /* Visit each node in the value table of a graph. */
6185 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6188 ir_graph *rem = current_ir_graph;
6190 current_ir_graph = irg;
6191 foreach_pset(irg->value_table, ir_node*, node) {
6194 current_ir_graph = rem;
6195 } /* visit_all_identities */
6198 * These optimizations deallocate nodes from the obstack.
6199 * It can only be called if it is guaranteed that no other nodes
6200 * reference this one, i.e., right after construction of a node.
6202 * @param n The node to optimize
6204 ir_node *optimize_node(ir_node *n)
6207 ir_graph *irg = get_irn_irg(n);
6208 unsigned iro = get_irn_opcode(n);
6211 /* Always optimize Phi nodes: part of the construction. */
6212 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6214 /* constant expression evaluation / constant folding */
6215 if (get_opt_constant_folding()) {
6216 /* neither constants nor Tuple values can be evaluated */
6217 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6218 /* try to evaluate */
6219 tv = computed_value(n);
6220 if (tv != tarval_bad) {
6225 * we MUST copy the node here temporarily, because it's still
6226 * needed for DBG_OPT_CSTEVAL
6228 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6229 oldn = (ir_node*)alloca(node_size);
6231 memcpy(oldn, n, node_size);
6232 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6234 /* ARG, copy the in array, we need it for statistics */
6235 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6237 /* note the inplace edges module */
6238 edges_node_deleted(n);
6240 /* evaluation was successful -- replace the node. */
6241 irg_kill_node(irg, n);
6242 nw = new_r_Const(irg, tv);
6244 DBG_OPT_CSTEVAL(oldn, nw);
6250 /* remove unnecessary nodes */
6251 if (get_opt_algebraic_simplification() ||
6252 (iro == iro_Phi) || /* always optimize these nodes. */
6254 (iro == iro_Proj) ||
6255 (iro == iro_Block) ) /* Flags tested local. */
6256 n = equivalent_node(n);
6258 /* Common Subexpression Elimination.
6260 * Checks whether n is already available.
6261 * The block input is used to distinguish different subexpressions. Right
6262 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6263 * subexpressions within a block.
6266 n = identify_cons(n);
6269 edges_node_deleted(oldn);
6271 /* We found an existing, better node, so we can deallocate the old node. */
6272 irg_kill_node(irg, oldn);
6276 /* Some more constant expression evaluation that does not allow to
6278 iro = get_irn_opcode(n);
6279 if (get_opt_algebraic_simplification() ||
6280 (iro == iro_Cond) ||
6281 (iro == iro_Proj)) /* Flags tested local. */
6282 n = transform_node(n);
6284 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6285 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6287 n = identify_remember(o);
6293 } /* optimize_node */
6297 * These optimizations never deallocate nodes (in place). This can cause dead
6298 * nodes lying on the obstack. Remove these by a dead node elimination,
6299 * i.e., a copying garbage collection.
6301 ir_node *optimize_in_place_2(ir_node *n)
6305 unsigned iro = get_irn_opcode(n);
6307 if (!get_opt_optimize() && !is_Phi(n)) return n;
6309 if (iro == iro_Deleted)
6312 /* constant expression evaluation / constant folding */
6313 if (get_opt_constant_folding()) {
6314 /* neither constants nor Tuple values can be evaluated */
6315 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6316 /* try to evaluate */
6317 tv = computed_value(n);
6318 if (tv != tarval_bad) {
6319 /* evaluation was successful -- replace the node. */
6320 ir_graph *irg = get_irn_irg(n);
6322 n = new_r_Const(irg, tv);
6324 DBG_OPT_CSTEVAL(oldn, n);
6330 /* remove unnecessary nodes */
6331 if (get_opt_constant_folding() ||
6332 (iro == iro_Phi) || /* always optimize these nodes. */
6333 (iro == iro_Id) || /* ... */
6334 (iro == iro_Proj) || /* ... */
6335 (iro == iro_Block) ) /* Flags tested local. */
6336 n = equivalent_node(n);
6338 /** common subexpression elimination **/
6339 /* Checks whether n is already available. */
6340 /* The block input is used to distinguish different subexpressions. Right
6341 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6342 subexpressions within a block. */
6343 if (get_opt_cse()) {
6345 n = identify_remember(o);
6350 /* Some more constant expression evaluation. */
6351 iro = get_irn_opcode(n);
6352 if (get_opt_constant_folding() ||
6353 (iro == iro_Cond) ||
6354 (iro == iro_Proj)) /* Flags tested local. */
6355 n = transform_node(n);
6357 /* Now we can verify the node, as it has no dead inputs any more. */
6360 /* Now we have a legal, useful node. Enter it in hash table for cse.
6361 Blocks should be unique anyways. (Except the successor of start:
6362 is cse with the start block!) */
6363 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6365 n = identify_remember(o);
6371 } /* optimize_in_place_2 */
6374 * Wrapper for external use, set proper status bits after optimization.
6376 ir_node *optimize_in_place(ir_node *n)
6378 ir_graph *irg = get_irn_irg(n);
6379 /* Handle graph state */
6380 assert(get_irg_phase_state(irg) != phase_building);
6382 if (get_opt_global_cse())
6383 set_irg_pinned(irg, op_pin_state_floats);
6385 /* FIXME: Maybe we could also test whether optimizing the node can
6386 change the control graph. */
6387 set_irg_doms_inconsistent(irg);
6388 return optimize_in_place_2(n);
6389 } /* optimize_in_place */
6392 * Calculate a hash value of a Const node.
6394 static unsigned hash_Const(const ir_node *node)
6398 /* special value for const, as they only differ in their tarval. */
6399 h = HASH_PTR(node->attr.con.tarval);
6405 * Calculate a hash value of a SymConst node.
6407 static unsigned hash_SymConst(const ir_node *node)
6411 /* all others are pointers */
6412 h = HASH_PTR(node->attr.symc.sym.type_p);
6415 } /* hash_SymConst */
6418 * Set the default hash operation in an ir_op_ops.
6420 * @param code the opcode for the default operation
6421 * @param ops the operations initialized
6426 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6430 ops->hash = hash_##a; \
6433 /* hash function already set */
6434 if (ops->hash != NULL)
6441 /* use input/mode default hash if no function was given */
6442 ops->hash = firm_default_hash;
6450 * Sets the default operation for an ir_ops.
6452 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6454 ops = firm_set_default_hash(code, ops);
6455 ops = firm_set_default_computed_value(code, ops);
6456 ops = firm_set_default_equivalent_node(code, ops);
6457 ops = firm_set_default_transform_node(code, ops);
6458 ops = firm_set_default_node_cmp_attr(code, ops);
6459 ops = firm_set_default_get_type_attr(code, ops);
6460 ops = firm_set_default_get_entity_attr(code, ops);
6463 } /* firm_set_default_operations */