3 * File name: ir/ir/iropt.c
4 * Purpose: iropt --- optimizations intertwined with IR construction.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 #include "irgraph_t.h"
29 #include "iredges_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
43 #include "opt_polymorphy.h"
44 #include "opt_confirms.h"
47 /* Make types visible to allow most efficient access */
48 # include "entity_t.h"
51 * return the value of a Constant
53 static tarval *computed_value_Const(ir_node *n)
55 return get_Const_tarval(n);
59 * return the value of a 'sizeof' SymConst
61 static tarval *computed_value_SymConst(ir_node *n)
65 switch (get_SymConst_kind(n)) {
66 case symconst_type_size:
67 type = get_SymConst_type(n);
68 if (get_type_state(type) == layout_fixed)
69 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
71 case symconst_type_align:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
83 * return the value of an Add
85 static tarval *computed_value_Add(ir_node *n)
87 ir_node *a = get_Add_left(n);
88 ir_node *b = get_Add_right(n);
90 tarval *ta = value_of(a);
91 tarval *tb = value_of(b);
93 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
94 return tarval_add(ta, tb);
100 * return the value of a Sub
101 * Special case: a - a
103 static tarval *computed_value_Sub(ir_node *n)
105 ir_node *a = get_Sub_left(n);
106 ir_node *b = get_Sub_right(n);
111 if (a == b && !is_Bad(a))
112 return get_mode_null(get_irn_mode(n));
117 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
118 return tarval_sub(ta, tb);
124 * return the value of a Carry
125 * Special : a op 0, 0 op b
127 static tarval *computed_value_Carry(ir_node *n)
129 ir_node *a = get_binop_left(n);
130 ir_node *b = get_binop_right(n);
131 ir_mode *m = get_irn_mode(n);
133 tarval *ta = value_of(a);
134 tarval *tb = value_of(b);
136 if ((ta != tarval_bad) && (tb != tarval_bad)) {
138 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
140 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
141 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
142 return get_mode_null(m);
148 * return the value of a Borrow
151 static tarval *computed_value_Borrow(ir_node *n)
153 ir_node *a = get_binop_left(n);
154 ir_node *b = get_binop_right(n);
155 ir_mode *m = get_irn_mode(n);
157 tarval *ta = value_of(a);
158 tarval *tb = value_of(b);
160 if ((ta != tarval_bad) && (tb != tarval_bad)) {
161 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
162 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
163 return get_mode_null(m);
169 * return the value of an unary Minus
171 static tarval *computed_value_Minus(ir_node *n)
173 ir_node *a = get_Minus_op(n);
174 tarval *ta = value_of(a);
176 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
177 return tarval_neg(ta);
183 * return the value of a Mul
185 static tarval *computed_value_Mul(ir_node *n)
187 ir_node *a = get_Mul_left(n);
188 ir_node *b = get_Mul_right(n);
190 tarval *ta = value_of(a);
191 tarval *tb = value_of(b);
193 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
194 return tarval_mul(ta, tb);
196 /* a*0 = 0 or 0*b = 0:
197 calls computed_value recursive and returns the 0 with proper
199 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
201 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
208 * return the value of a floating point Quot
210 static tarval *computed_value_Quot(ir_node *n)
212 ir_node *a = get_Quot_left(n);
213 ir_node *b = get_Quot_right(n);
215 tarval *ta = value_of(a);
216 tarval *tb = value_of(b);
218 /* This was missing in original implementation. Why? */
219 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
220 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
221 return tarval_quo(ta, tb);
227 * calculate the value of an integer Div of two nodes
228 * Special case: 0 / b
230 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
232 tarval *ta = value_of(a);
233 tarval *tb = value_of(b);
235 /* Compute c1 / c2 or 0 / a, a != 0 */
236 if (ta != tarval_bad) {
237 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
238 return tarval_div(ta, tb);
239 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
246 * return the value of an integer Div
248 static tarval *computed_value_Div(ir_node *n)
250 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
254 * calculate the value of an integer Mod of two nodes
255 * Special case: a % 1
257 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
259 tarval *ta = value_of(a);
260 tarval *tb = value_of(b);
262 /* Compute c1 % c2 or a % 1 */
263 if (tb != tarval_bad) {
264 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
265 return tarval_mod(ta, tb);
266 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
267 return get_mode_null(get_irn_mode(a));
274 * return the value of an integer Mod
276 static tarval *computed_value_Mod(ir_node *n)
278 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
282 * return the value of an Abs
284 static tarval *computed_value_Abs(ir_node *n)
286 ir_node *a = get_Abs_op(n);
287 tarval *ta = value_of(a);
289 if (ta != tarval_bad)
290 return tarval_abs(ta);
296 * return the value of an And
297 * Special case: a & 0, 0 & b
299 static tarval *computed_value_And(ir_node *n)
301 ir_node *a = get_And_left(n);
302 ir_node *b = get_And_right(n);
304 tarval *ta = value_of(a);
305 tarval *tb = value_of(b);
307 if ((ta != tarval_bad) && (tb != tarval_bad)) {
308 return tarval_and (ta, tb);
312 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
313 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
321 * return the value of an Or
322 * Special case: a | 1...1, 1...1 | b
324 static tarval *computed_value_Or(ir_node *n)
326 ir_node *a = get_Or_left(n);
327 ir_node *b = get_Or_right(n);
329 tarval *ta = value_of(a);
330 tarval *tb = value_of(b);
332 if ((ta != tarval_bad) && (tb != tarval_bad)) {
333 return tarval_or (ta, tb);
336 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
337 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
345 * return the value of an Eor
347 static tarval *computed_value_Eor(ir_node *n)
349 ir_node *a = get_Eor_left(n);
350 ir_node *b = get_Eor_right(n);
355 return get_mode_null(get_irn_mode(n));
360 if ((ta != tarval_bad) && (tb != tarval_bad)) {
361 return tarval_eor (ta, tb);
367 * return the value of a Not
369 static tarval *computed_value_Not(ir_node *n)
371 ir_node *a = get_Not_op(n);
372 tarval *ta = value_of(a);
374 if (ta != tarval_bad)
375 return tarval_not(ta);
381 * return the value of a Shl
383 static tarval *computed_value_Shl(ir_node *n)
385 ir_node *a = get_Shl_left(n);
386 ir_node *b = get_Shl_right(n);
388 tarval *ta = value_of(a);
389 tarval *tb = value_of(b);
391 if ((ta != tarval_bad) && (tb != tarval_bad)) {
392 return tarval_shl (ta, tb);
398 * return the value of a Shr
400 static tarval *computed_value_Shr(ir_node *n)
402 ir_node *a = get_Shr_left(n);
403 ir_node *b = get_Shr_right(n);
405 tarval *ta = value_of(a);
406 tarval *tb = value_of(b);
408 if ((ta != tarval_bad) && (tb != tarval_bad)) {
409 return tarval_shr (ta, tb);
415 * return the value of a Shrs
417 static tarval *computed_value_Shrs(ir_node *n)
419 ir_node *a = get_Shrs_left(n);
420 ir_node *b = get_Shrs_right(n);
422 tarval *ta = value_of(a);
423 tarval *tb = value_of(b);
425 if ((ta != tarval_bad) && (tb != tarval_bad)) {
426 return tarval_shrs (ta, tb);
432 * return the value of a Rot
434 static tarval *computed_value_Rot(ir_node *n)
436 ir_node *a = get_Rot_left(n);
437 ir_node *b = get_Rot_right(n);
439 tarval *ta = value_of(a);
440 tarval *tb = value_of(b);
442 if ((ta != tarval_bad) && (tb != tarval_bad)) {
443 return tarval_rot (ta, tb);
449 * return the value of a Conv
451 static tarval *computed_value_Conv(ir_node *n)
453 ir_node *a = get_Conv_op(n);
454 tarval *ta = value_of(a);
456 if (ta != tarval_bad)
457 return tarval_convert_to(ta, get_irn_mode(n));
463 * return the value of a Proj(Cmp)
465 * This performs a first step of unreachable code elimination.
466 * Proj can not be computed, but folding a Cmp above the Proj here is
467 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
469 * There are several case where we can evaluate a Cmp node, see later.
471 static tarval *computed_value_Proj_Cmp(ir_node *n)
473 ir_node *a = get_Proj_pred(n);
474 ir_node *aa = get_Cmp_left(a);
475 ir_node *ab = get_Cmp_right(a);
476 long proj_nr = get_Proj_proj(n);
479 * BEWARE: a == a is NOT always True for floating Point values, as
480 * NaN != NaN is defined, so we must check this here.
483 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
486 /* This is a trick with the bits used for encoding the Cmp
487 Proj numbers, the following statement is not the same:
488 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
489 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
492 tarval *taa = value_of(aa);
493 tarval *tab = value_of(ab);
494 ir_mode *mode = get_irn_mode(aa);
497 * The predecessors of Cmp are target values. We can evaluate
500 if ((taa != tarval_bad) && (tab != tarval_bad)) {
501 /* strange checks... */
502 pn_Cmp flags = tarval_cmp(taa, tab);
503 if (flags != pn_Cmp_False) {
504 return new_tarval_from_long (proj_nr & flags, mode_b);
507 /* for integer values, we can check against MIN/MAX */
508 else if (mode_is_int(mode)) {
509 /* MIN <=/> x. This results in true/false. */
510 if (taa == get_mode_min(mode)) {
511 /* a compare with the MIN value */
512 if (proj_nr == pn_Cmp_Le)
513 return get_tarval_b_true();
514 else if (proj_nr == pn_Cmp_Gt)
515 return get_tarval_b_false();
517 /* x >=/< MIN. This results in true/false. */
519 if (tab == get_mode_min(mode)) {
520 /* a compare with the MIN value */
521 if (proj_nr == pn_Cmp_Ge)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Lt)
524 return get_tarval_b_false();
526 /* MAX >=/< x. This results in true/false. */
527 else if (taa == get_mode_max(mode)) {
528 if (proj_nr == pn_Cmp_Ge)
529 return get_tarval_b_true();
530 else if (proj_nr == pn_Cmp_Lt)
531 return get_tarval_b_false();
533 /* x <=/> MAX. This results in true/false. */
534 else if (tab == get_mode_max(mode)) {
535 if (proj_nr == pn_Cmp_Le)
536 return get_tarval_b_true();
537 else if (proj_nr == pn_Cmp_Gt)
538 return get_tarval_b_false();
542 * The predecessors are Allocs or (void*)(0) constants. Allocs never
543 * return NULL, they raise an exception. Therefore we can predict
547 ir_node *aaa = skip_Id(skip_Proj(aa));
548 ir_node *aba = skip_Id(skip_Proj(ab));
550 if ( ( (/* aa is ProjP and aaa is Alloc */
551 (get_irn_op(aa) == op_Proj)
552 && (mode_is_reference(get_irn_mode(aa)))
553 && (get_irn_op(aaa) == op_Alloc))
554 && ( (/* ab is NULL */
555 (get_irn_op(ab) == op_Const)
556 && (mode_is_reference(get_irn_mode(ab)))
557 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
558 || (/* ab is other Alloc */
559 (get_irn_op(ab) == op_Proj)
560 && (mode_is_reference(get_irn_mode(ab)))
561 && (get_irn_op(aba) == op_Alloc)
563 || (/* aa is NULL and aba is Alloc */
564 (get_irn_op(aa) == op_Const)
565 && (mode_is_reference(get_irn_mode(aa)))
566 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
567 && (get_irn_op(ab) == op_Proj)
568 && (mode_is_reference(get_irn_mode(ab)))
569 && (get_irn_op(aba) == op_Alloc)))
571 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
574 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
578 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
580 static tarval *computed_value_Proj(ir_node *n)
582 ir_node *a = get_Proj_pred(n);
585 switch (get_irn_opcode(a)) {
587 return computed_value_Proj_Cmp(n);
590 /* compute either the Div or the Mod part */
591 proj_nr = get_Proj_proj(n);
592 if (proj_nr == pn_DivMod_res_div)
593 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
594 else if (proj_nr == pn_DivMod_res_mod)
595 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
599 if (get_Proj_proj(n) == pn_Div_res)
600 return computed_value(a);
604 if (get_Proj_proj(n) == pn_Mod_res)
605 return computed_value(a);
615 * calculate the value of a Mux: can be evaluated, if the
616 * sel and the right input are known
618 static tarval *computed_value_Mux(ir_node *n)
620 ir_node *sel = get_Mux_sel(n);
621 tarval *ts = value_of(sel);
623 if (ts == get_tarval_b_true()) {
624 ir_node *v = get_Mux_true(n);
627 else if (ts == get_tarval_b_false()) {
628 ir_node *v = get_Mux_false(n);
635 * Calculate the value of a Psi: can be evaluated, if a condition is true
636 * and all previous conditions are false. If all conditions are false
637 * we evaluate to the default one.
639 static tarval *computed_value_Psi(ir_node *n)
642 return computed_value_Mux(n);
647 * calculate the value of a Confirm: can be evaluated,
648 * if it has the form Confirm(x, '=', Const).
650 static tarval *computed_value_Confirm(ir_node *n)
652 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
653 value_of(get_Confirm_bound(n)) : tarval_bad;
657 * If the parameter n can be computed, return its value, else tarval_bad.
658 * Performs constant folding.
660 * @param n The node this should be evaluated
662 tarval *computed_value(ir_node *n)
664 if (n->op->ops.computed_value)
665 return n->op->ops.computed_value(n);
670 * set the default computed_value evaluator in an ir_op_ops.
672 * @param code the opcode for the default operation
673 * @param ops the operations initialized
678 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
682 ops->computed_value = computed_value_##a; \
720 * Returns a equivalent block for another block.
721 * If the block has only one predecessor, this is
722 * the equivalent one. If the only predecessor of a block is
723 * the block itself, this is a dead block.
725 * If both predecessors of a block are the branches of a binary
726 * Cond, the equivalent block is Cond's block.
728 * If all predecessors of a block are bad or lies in a dead
729 * block, the current block is dead as well.
731 * Note, that blocks are NEVER turned into Bad's, instead
732 * the dead_block flag is set. So, never test for is_Bad(block),
733 * always use is_dead_Block(block).
735 static ir_node *equivalent_node_Block(ir_node *n)
738 int n_preds = get_Block_n_cfgpreds(n);
740 /* The Block constructor does not call optimize, but mature_immBlock
741 calls the optimization. */
742 assert(get_Block_matured(n));
744 /* Straightening: a single entry Block following a single exit Block
745 can be merged, if it is not the Start block. */
746 /* !!! Beware, all Phi-nodes of n must have been optimized away.
747 This should be true, as the block is matured before optimize is called.
748 But what about Phi-cycles with the Phi0/Id that could not be resolved?
749 Remaining Phi nodes are just Ids. */
750 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
751 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
752 if (predblock == oldn) {
753 /* Jmp jumps into the block it is in -- deal self cycle. */
754 n = set_Block_dead(n);
755 DBG_OPT_DEAD_BLOCK(oldn, n);
756 } else if (get_opt_control_flow_straightening()) {
758 DBG_OPT_STG(oldn, n);
761 else if ((n_preds == 1) &&
762 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
763 ir_node *predblock = get_Block_cfgpred_block(n, 0);
764 if (predblock == oldn) {
765 /* Jmp jumps into the block it is in -- deal self cycle. */
766 n = set_Block_dead(n);
767 DBG_OPT_DEAD_BLOCK(oldn, n);
770 else if ((n_preds == 2) &&
771 (get_opt_control_flow_weak_simplification())) {
772 /* Test whether Cond jumps twice to this block
773 * The more general case which more than 2 predecessors is handles
774 * in optimize_cf(), we handle only this special case for speed here.
776 ir_node *a = get_Block_cfgpred(n, 0);
777 ir_node *b = get_Block_cfgpred(n, 1);
779 if ((get_irn_op(a) == op_Proj) &&
780 (get_irn_op(b) == op_Proj) &&
781 (get_Proj_pred(a) == get_Proj_pred(b)) &&
782 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
783 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
784 /* Also a single entry Block following a single exit Block. Phis have
785 twice the same operand and will be optimized away. */
786 n = get_nodes_block(get_Proj_pred(a));
787 DBG_OPT_IFSIM1(oldn, a, b, n);
790 else if (get_opt_unreachable_code() &&
791 (n != get_irg_start_block(current_ir_graph)) &&
792 (n != get_irg_end_block(current_ir_graph)) ) {
795 /* If all inputs are dead, this block is dead too, except if it is
796 the start or end block. This is one step of unreachable code
798 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
799 ir_node *pred = get_Block_cfgpred(n, i);
802 if (is_Bad(pred)) continue;
803 pred_blk = get_nodes_block(skip_Proj(pred));
805 if (is_Block_dead(pred_blk)) continue;
808 /* really found a living input */
813 n = set_Block_dead(n);
814 DBG_OPT_DEAD_BLOCK(oldn, n);
822 * Returns a equivalent node for a Jmp, a Bad :-)
823 * Of course this only happens if the Block of the Jmp is dead.
825 static ir_node *equivalent_node_Jmp(ir_node *n)
827 /* unreachable code elimination */
828 if (is_Block_dead(get_nodes_block(n)))
834 /** Raise is handled in the same way as Jmp. */
835 #define equivalent_node_Raise equivalent_node_Jmp
838 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
839 See transform_node_Proj_Cond(). */
842 * optimize operations that are commutative and have neutral 0,
843 * so a op 0 = 0 op a = a.
845 static ir_node *equivalent_node_neutral_zero(ir_node *n)
849 ir_node *a = get_binop_left(n);
850 ir_node *b = get_binop_right(n);
855 /* After running compute_node there is only one constant predecessor.
856 Find this predecessors value and remember the other node: */
857 if ((tv = value_of(a)) != tarval_bad) {
859 } else if ((tv = value_of(b)) != tarval_bad) {
864 /* If this predecessors constant value is zero, the operation is
865 * unnecessary. Remove it.
867 * Beware: If n is a Add, the mode of on and n might be different
868 * which happens in this rare construction: NULL + 3.
869 * Then, a Conv would be needed which we cannot include here.
871 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
872 if (get_irn_mode(on) == get_irn_mode(n)) {
875 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
883 * Eor is commutative and has neutral 0.
885 #define equivalent_node_Eor equivalent_node_neutral_zero
888 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
890 * The second one looks strange, but this construct
891 * is used heavily in the LCC sources :-).
893 * Beware: The Mode of an Add may be different than the mode of its
894 * predecessors, so we could not return a predecessors in all cases.
896 static ir_node *equivalent_node_Add(ir_node *n)
899 ir_node *left, *right;
901 n = equivalent_node_neutral_zero(n);
905 left = get_Add_left(n);
906 right = get_Add_right(n);
908 if (get_irn_op(left) == op_Sub) {
909 if (get_Sub_right(left) == right) {
912 n = get_Sub_left(left);
913 if (get_irn_mode(oldn) == get_irn_mode(n)) {
914 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
919 if (get_irn_op(right) == op_Sub) {
920 if (get_Sub_right(right) == left) {
923 n = get_Sub_left(right);
924 if (get_irn_mode(oldn) == get_irn_mode(n)) {
925 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
934 * optimize operations that are not commutative but have neutral 0 on left,
937 static ir_node *equivalent_node_left_zero(ir_node *n)
941 ir_node *a = get_binop_left(n);
942 ir_node *b = get_binop_right(n);
944 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
947 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
953 #define equivalent_node_Shl equivalent_node_left_zero
954 #define equivalent_node_Shr equivalent_node_left_zero
955 #define equivalent_node_Shrs equivalent_node_left_zero
956 #define equivalent_node_Rot equivalent_node_left_zero
959 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
961 * The second one looks strange, but this construct
962 * is used heavily in the LCC sources :-).
964 * Beware: The Mode of a Sub may be different than the mode of its
965 * predecessors, so we could not return a predecessors in all cases.
967 static ir_node *equivalent_node_Sub(ir_node *n)
971 ir_node *a = get_Sub_left(n);
972 ir_node *b = get_Sub_right(n);
974 /* Beware: modes might be different */
975 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
976 if (get_irn_mode(n) == get_irn_mode(a)) {
979 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
982 else if (get_irn_op(a) == op_Add) {
983 ir_mode *mode = get_irn_mode(n);
985 if (mode_wrap_around(mode)) {
986 ir_node *left = get_Add_left(a);
987 ir_node *right = get_Add_right(a);
990 if (get_irn_mode(n) == get_irn_mode(right)) {
992 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
995 else if (right == b) {
996 if (get_irn_mode(n) == get_irn_mode(left)) {
998 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
1009 * Optimize an "idempotent unary op", ie op(op(n)) = n.
1012 * -(-a) == a, but might overflow two times.
1013 * We handle it anyway here but the better way would be a
1014 * flag. This would be needed for Pascal for instance.
1016 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1019 ir_node *pred = get_unop_op(n);
1021 /* optimize symmetric unop */
1022 if (get_irn_op(pred) == get_irn_op(n)) {
1023 n = get_unop_op(pred);
1024 DBG_OPT_ALGSIM2(oldn, pred, n);
1029 /** Not(Not(x)) == x */
1030 #define equivalent_node_Not equivalent_node_idempotent_unop
1032 /** --x == x ??? Is this possible or can --x raise an
1033 out of bounds exception if min =! max? */
1034 #define equivalent_node_Minus equivalent_node_idempotent_unop
1037 * Optimize a * 1 = 1 * a = a.
1039 static ir_node *equivalent_node_Mul(ir_node *n)
1043 ir_node *a = get_Mul_left(n);
1044 ir_node *b = get_Mul_right(n);
1046 /* Mul is commutative and has again an other neutral element. */
1047 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1049 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1050 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1052 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1058 * Optimize a / 1 = a.
1060 static ir_node *equivalent_node_Div(ir_node *n)
1062 ir_node *a = get_Div_left(n);
1063 ir_node *b = get_Div_right(n);
1065 /* Div is not commutative. */
1066 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1067 /* Turn Div into a tuple (mem, bad, a) */
1068 ir_node *mem = get_Div_mem(n);
1069 turn_into_tuple(n, pn_Div_max);
1070 set_Tuple_pred(n, pn_Div_M, mem);
1071 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1072 set_Tuple_pred(n, pn_Div_res, a);
1078 * Optimize a / 1 = a.
1080 static ir_node *equivalent_node_DivMod(ir_node *n)
1082 ir_node *a = get_DivMod_left(n);
1083 ir_node *b = get_DivMod_right(n);
1085 /* Div is not commutative. */
1086 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1087 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1088 ir_node *mem = get_Div_mem(n);
1089 ir_mode *mode = get_irn_mode(b);
1091 turn_into_tuple(n, pn_DivMod_max);
1092 set_Tuple_pred(n, pn_DivMod_M, mem);
1093 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1094 set_Tuple_pred(n, pn_DivMod_res_div, a);
1095 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1101 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1103 static ir_node *equivalent_node_Or(ir_node *n)
1107 ir_node *a = get_Or_left(n);
1108 ir_node *b = get_Or_right(n);
1111 n = a; /* Or has it's own neutral element */
1112 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1113 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1115 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1116 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1118 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1125 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1127 static ir_node *equivalent_node_And(ir_node *n)
1131 ir_node *a = get_And_left(n);
1132 ir_node *b = get_And_right(n);
1135 n = a; /* And has it's own neutral element */
1136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1137 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1139 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1140 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1142 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1148 * Try to remove useless Conv's:
1150 static ir_node *equivalent_node_Conv(ir_node *n)
1153 ir_node *a = get_Conv_op(n);
1156 ir_mode *n_mode = get_irn_mode(n);
1157 ir_mode *a_mode = get_irn_mode(a);
1159 if (n_mode == a_mode) { /* No Conv necessary */
1161 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1162 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1166 n_mode = get_irn_mode(n);
1167 b_mode = get_irn_mode(b);
1169 if (n_mode == b_mode) {
1170 if (n_mode == mode_b) {
1171 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1172 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1174 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1175 if (smaller_mode(b_mode, a_mode)){
1176 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1177 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1186 * A Cast may be removed if the type of the previous node
1187 * is already the type of the Cast.
1189 static ir_node *equivalent_node_Cast(ir_node *n) {
1191 ir_node *pred = get_Cast_op(n);
1193 if (get_irn_type(pred) == get_Cast_type(n)) {
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1201 Several optimizations:
1202 - no Phi in start block.
1203 - remove Id operators that are inputs to Phi
1204 - fold Phi-nodes, iff they have only one predecessor except
1207 static ir_node *equivalent_node_Phi(ir_node *n)
1212 ir_node *block = NULL; /* to shutup gcc */
1213 ir_node *first_val = NULL; /* to shutup gcc */
1215 if (!get_opt_normalize()) return n;
1217 n_preds = get_Phi_n_preds(n);
1219 block = get_nodes_block(n);
1220 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1221 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1222 if ((is_Block_dead(block)) || /* Control dead */
1223 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1224 return new_Bad(); /* in the Start Block. */
1226 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1228 /* If the Block has a Bad pred, we also have one. */
1229 for (i = 0; i < n_preds; ++i)
1230 if (is_Bad(get_Block_cfgpred(block, i)))
1231 set_Phi_pred(n, i, new_Bad());
1233 /* Find first non-self-referencing input */
1234 for (i = 0; i < n_preds; ++i) {
1235 first_val = get_Phi_pred(n, i);
1236 if ( (first_val != n) /* not self pointer */
1238 && (! is_Bad(first_val))
1240 ) { /* value not dead */
1241 break; /* then found first value. */
1246 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1250 /* search for rest of inputs, determine if any of these
1251 are non-self-referencing */
1252 while (++i < n_preds) {
1253 ir_node *scnd_val = get_Phi_pred(n, i);
1254 if ( (scnd_val != n)
1255 && (scnd_val != first_val)
1257 && (! is_Bad(scnd_val))
1265 /* Fold, if no multiple distinct non-self-referencing inputs */
1267 DBG_OPT_PHI(oldn, n);
1273 Several optimizations:
1274 - no Sync in start block.
1275 - fold Sync-nodes, iff they have only one predecessor except
1278 static ir_node *equivalent_node_Sync(ir_node *n)
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_normalize()) return n;
1287 n_preds = get_Sync_n_preds(n);
1289 /* Find first non-self-referencing input */
1290 for (i = 0; i < n_preds; ++i) {
1291 first_val = get_Sync_pred(n, i);
1292 if ((first_val != n) /* not self pointer */ &&
1293 (! is_Bad(first_val))
1294 ) { /* value not dead */
1295 break; /* then found first value. */
1300 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1303 /* search the rest of inputs, determine if any of these
1304 are non-self-referencing */
1305 while (++i < n_preds) {
1306 ir_node *scnd_val = get_Sync_pred(n, i);
1307 if ((scnd_val != n) &&
1308 (scnd_val != first_val) &&
1309 (! is_Bad(scnd_val))
1315 /* Fold, if no multiple distinct non-self-referencing inputs */
1317 DBG_OPT_SYNC(oldn, n);
1323 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324 * ProjX(Load) and ProjX(Store)
1326 static ir_node *equivalent_node_Proj(ir_node *n)
1330 ir_node *a = get_Proj_pred(n);
1332 if ( get_irn_op(a) == op_Tuple) {
1333 /* Remove the Tuple/Proj combination. */
1334 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1335 n = get_Tuple_pred(a, get_Proj_proj(n));
1336 DBG_OPT_TUPLE(oldn, a, n);
1338 assert(0); /* This should not happen! */
1342 else if (get_irn_mode(n) == mode_X) {
1343 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1344 /* Remove dead control flow -- early gigo(). */
1347 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1348 ir_op *op = get_irn_op(a);
1350 if (op == op_Load || op == op_Store) {
1351 /* get the load/store address */
1352 ir_node *addr = get_irn_n(a, 1);
1353 if (value_not_null(addr)) {
1354 /* this node may float if it did not depend on a Confirm */
1355 set_irn_pinned(a, op_pin_state_floats);
1369 static ir_node *equivalent_node_Id(ir_node *n)
1375 } while (get_irn_op(n) == op_Id);
1377 DBG_OPT_ID(oldn, n);
1384 static ir_node *equivalent_node_Mux(ir_node *n)
1386 ir_node *oldn = n, *sel = get_Mux_sel(n);
1387 tarval *ts = value_of(sel);
1389 /* Mux(true, f, t) == t */
1390 if (ts == tarval_b_true) {
1391 n = get_Mux_true(n);
1392 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1394 /* Mux(false, f, t) == f */
1395 else if (ts == tarval_b_false) {
1396 n = get_Mux_false(n);
1397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1399 /* Mux(v, x, x) == x */
1400 else if (get_Mux_false(n) == get_Mux_true(n)) {
1401 n = get_Mux_true(n);
1402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1404 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1405 ir_node *cmp = get_Proj_pred(sel);
1406 long proj_nr = get_Proj_proj(sel);
1407 ir_node *b = get_Mux_false(n);
1408 ir_node *a = get_Mux_true(n);
1411 * Note: normalization puts the constant on the right site,
1412 * so we check only one case.
1414 * Note further that these optimization work even for floating point
1415 * with NaN's because -NaN == NaN.
1416 * However, if +0 and -0 is handled differently, we cannot use the first one.
1418 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1419 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1420 /* Mux(a CMP 0, X, a) */
1421 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1422 /* Mux(a CMP 0, -a, a) */
1423 if (proj_nr == pn_Cmp_Eq) {
1424 /* Mux(a == 0, -a, a) ==> -a */
1426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1428 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1429 /* Mux(a != 0, -a, a) ==> a */
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1434 else if (classify_Const(b) == CNST_NULL) {
1435 /* Mux(a CMP 0, 0, a) */
1436 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1437 /* Mux(a != 0, 0, a) ==> a */
1439 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1441 else if (proj_nr == pn_Cmp_Eq) {
1442 /* Mux(a == 0, 0, a) ==> 0 */
1444 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1454 * Returns a equivalent node of a Psi: if a condition is true
1455 * and all previous conditions are false we know its value.
1456 * If all conditions are false its value is the default one.
1458 static ir_node *equivalent_node_Psi(ir_node *n) {
1460 return equivalent_node_Mux(n);
1465 * Optimize -a CMP -b into b CMP a.
1466 * This works only for for modes where unary Minus
1468 * Note that two-complement integers can Overflow
1469 * so it will NOT work.
1471 static ir_node *equivalent_node_Cmp(ir_node *n)
1473 ir_node *left = get_Cmp_left(n);
1474 ir_node *right = get_Cmp_right(n);
1476 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1477 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1478 left = get_Minus_op(left);
1479 right = get_Minus_op(right);
1480 set_Cmp_left(n, right);
1481 set_Cmp_right(n, left);
1487 * Remove Confirm nodes if setting is on.
1488 * Replace Confirms(x, '=', Constlike) by Constlike.
1490 static ir_node *equivalent_node_Confirm(ir_node *n)
1492 ir_node *pred = get_Confirm_value(n);
1493 pn_Cmp pnc = get_Confirm_cmp(n);
1495 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1497 * rare case: two identical Confirms one after another,
1498 * replace the second one with the first.
1502 if (pnc == pn_Cmp_Eq) {
1503 ir_node *bound = get_Confirm_bound(n);
1506 * Optimize a rare case:
1507 * Confirm(x, '=', Constlike) ==> Constlike
1509 if (is_irn_constlike(bound)) {
1510 DBG_OPT_CONFIRM(n, bound);
1514 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1518 * Optimize CopyB(mem, x, x) into a Nop
1520 static ir_node *equivalent_node_CopyB(ir_node *n)
1522 ir_node *a = get_CopyB_dst(n);
1523 ir_node *b = get_CopyB_src(n);
1526 /* Turn CopyB into a tuple (mem, bad, bad) */
1527 ir_node *mem = get_CopyB_mem(n);
1528 turn_into_tuple(n, pn_CopyB_max);
1529 set_Tuple_pred(n, pn_CopyB_M, mem);
1530 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1531 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1537 * Optimize Bounds(idx, idx, upper) into idx.
1539 static ir_node *equivalent_node_Bound(ir_node *n)
1541 ir_node *idx = get_Bound_index(n);
1542 ir_node *lower = get_Bound_lower(n);
1545 /* By definition lower < upper, so if idx == lower -->
1546 lower <= idx && idx < upper */
1548 /* Turn Bound into a tuple (mem, bad, idx) */
1552 ir_node *pred = skip_Proj(idx);
1554 if (get_irn_op(pred) == op_Bound) {
1556 * idx was Bounds_check previously, it is still valid if
1557 * lower <= pred_lower && pred_upper <= upper.
1559 ir_node *upper = get_Bound_upper(n);
1560 if (get_Bound_lower(pred) == lower &&
1561 get_Bound_upper(pred) == upper) {
1563 * One could expect that we simply return the previous
1564 * Bound here. However, this would be wrong, as we could
1565 * add an exception Proj to a new location than.
1566 * So, we must turn in into a tuple
1573 /* Turn Bound into a tuple (mem, bad, idx) */
1574 ir_node *mem = get_Bound_mem(n);
1575 turn_into_tuple(n, pn_Bound_max);
1576 set_Tuple_pred(n, pn_Bound_M, mem);
1577 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1578 set_Tuple_pred(n, pn_Bound_res, idx);
1584 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1585 * perform no actual computation, as, e.g., the Id nodes. It does not create
1586 * new nodes. It is therefore safe to free n if the node returned is not n.
1587 * If a node returns a Tuple we can not just skip it. If the size of the
1588 * in array fits, we transform n into a tuple (e.g., Div).
1591 equivalent_node(ir_node *n)
1593 if (n->op->ops.equivalent_node)
1594 return n->op->ops.equivalent_node(n);
1599 * sets the default equivalent node operation for an ir_op_ops.
1601 * @param code the opcode for the default operation
1602 * @param ops the operations initialized
1607 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1611 ops->equivalent_node = equivalent_node_##a; \
1653 * Do node specific optimizations of nodes predecessors.
1656 optimize_preds(ir_node *n) {
1657 ir_node *a = NULL, *b = NULL;
1659 /* get the operands we will work on for simple cases. */
1661 a = get_binop_left(n);
1662 b = get_binop_right(n);
1663 } else if (is_unop(n)) {
1667 switch (get_irn_opcode(n)) {
1670 /* We don't want Cast as input to Cmp. */
1671 if (get_irn_op(a) == op_Cast) {
1675 if (get_irn_op(b) == op_Cast) {
1677 set_Cmp_right(n, b);
1686 * Returns non-zero if all Phi predecessors are constants
1688 static int is_const_Phi(ir_node *phi) {
1691 for (i = get_irn_arity(phi) - 1; i >= 0; --i)
1692 if (! is_Const(get_irn_n(phi, i)))
1698 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1699 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1700 * If possible, remove the Conv's.
1702 static ir_node *transform_node_AddSub(ir_node *n)
1704 ir_mode *mode = get_irn_mode(n);
1706 if (mode_is_reference(mode)) {
1707 ir_node *left = get_binop_left(n);
1708 ir_node *right = get_binop_right(n);
1709 int ref_bits = get_mode_size_bits(mode);
1711 if (get_irn_op(left) == op_Conv) {
1712 ir_mode *mode = get_irn_mode(left);
1713 int bits = get_mode_size_bits(mode);
1715 if (ref_bits == bits &&
1716 mode_is_int(mode) &&
1717 get_mode_arithmetic(mode) == irma_twos_complement) {
1718 ir_node *pre = get_Conv_op(left);
1719 ir_mode *pre_mode = get_irn_mode(pre);
1721 if (mode_is_int(pre_mode) &&
1722 get_mode_size_bits(pre_mode) == bits &&
1723 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1724 /* ok, this conv just changes to sign, moreover the calculation
1725 * is done with same number of bits as our address mode, so
1726 * we can ignore the conv as address calculation can be viewed
1727 * as either signed or unsigned
1729 set_binop_left(n, pre);
1734 if (get_irn_op(right) == op_Conv) {
1735 ir_mode *mode = get_irn_mode(right);
1736 int bits = get_mode_size_bits(mode);
1738 if (ref_bits == bits &&
1739 mode_is_int(mode) &&
1740 get_mode_arithmetic(mode) == irma_twos_complement) {
1741 ir_node *pre = get_Conv_op(right);
1742 ir_mode *pre_mode = get_irn_mode(pre);
1744 if (mode_is_int(pre_mode) &&
1745 get_mode_size_bits(pre_mode) == bits &&
1746 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1747 /* ok, this conv just changes to sign, moreover the calculation
1748 * is done with same number of bits as our address mode, so
1749 * we can ignore the conv as address calculation can be viewed
1750 * as either signed or unsigned
1752 set_binop_right(n, pre);
1761 * Do the AddSub optimization, then Transform
1762 * Add(a,a) -> Mul(a, 2)
1763 * Add(Mul(a, x), a) -> Mul(a, x+1)
1764 * if the mode is integer or float.
1765 * Transform Add(a,-b) into Sub(a,b).
1766 * Reassociation might fold this further.
1768 static ir_node *transform_node_Add(ir_node *n)
1773 n = transform_node_AddSub(n);
1775 mode = get_irn_mode(n);
1776 if (mode_is_num(mode)) {
1777 ir_node *a = get_Add_left(n);
1778 ir_node *b = get_Add_right(n);
1781 ir_node *block = get_irn_n(n, -1);
1784 get_irn_dbg_info(n),
1788 new_r_Const_long(current_ir_graph, block, mode, 2),
1790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1792 else if (get_irn_op(a) == op_Minus) {
1794 get_irn_dbg_info(n),
1800 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1802 else if (get_irn_op(b) == op_Minus) {
1804 get_irn_dbg_info(n),
1810 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1812 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1813 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1814 ir_node *ma = get_Mul_left(a);
1815 ir_node *mb = get_Mul_right(a);
1818 ir_node *blk = get_irn_n(n, -1);
1820 get_irn_dbg_info(n), current_ir_graph, blk,
1823 get_irn_dbg_info(n), current_ir_graph, blk,
1825 new_r_Const_long(current_ir_graph, blk, mode, 1),
1828 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1831 ir_node *blk = get_irn_n(n, -1);
1833 get_irn_dbg_info(n), current_ir_graph, blk,
1836 get_irn_dbg_info(n), current_ir_graph, blk,
1838 new_r_Const_long(current_ir_graph, blk, mode, 1),
1841 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1844 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1845 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1846 ir_node *ma = get_Mul_left(b);
1847 ir_node *mb = get_Mul_right(b);
1850 ir_node *blk = get_irn_n(n, -1);
1852 get_irn_dbg_info(n), current_ir_graph, blk,
1855 get_irn_dbg_info(n), current_ir_graph, blk,
1857 new_r_Const_long(current_ir_graph, blk, mode, 1),
1860 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1863 ir_node *blk = get_irn_n(n, -1);
1865 get_irn_dbg_info(n), current_ir_graph, blk,
1868 get_irn_dbg_info(n), current_ir_graph, blk,
1870 new_r_Const_long(current_ir_graph, blk, mode, 1),
1873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1881 * Do the AddSub optimization, then Transform
1882 * Sub(0,a) -> Minus(a)
1883 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1884 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
1886 static ir_node *transform_node_Sub(ir_node *n)
1892 n = transform_node_AddSub(n);
1894 mode = get_irn_mode(n);
1895 a = get_Sub_left(n);
1896 b = get_Sub_right(n);
1897 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1899 get_irn_dbg_info(n),
1904 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1906 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1907 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1908 ir_node *ma = get_Mul_left(a);
1909 ir_node *mb = get_Mul_right(a);
1912 ir_node *blk = get_irn_n(n, -1);
1914 get_irn_dbg_info(n),
1915 current_ir_graph, blk,
1918 get_irn_dbg_info(n),
1919 current_ir_graph, blk,
1921 new_r_Const_long(current_ir_graph, blk, mode, 1),
1924 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1927 ir_node *blk = get_irn_n(n, -1);
1929 get_irn_dbg_info(n),
1930 current_ir_graph, blk,
1933 get_irn_dbg_info(n),
1934 current_ir_graph, blk,
1936 new_r_Const_long(current_ir_graph, blk, mode, 1),
1939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1942 else if (get_irn_op(a) == op_Sub) {
1943 ir_node *x = get_Sub_left(a);
1944 ir_node *y = get_Sub_right(a);
1945 ir_node *blk = get_irn_n(n, -1);
1946 ir_mode *m_b = get_irn_mode(b);
1947 ir_mode *m_y = get_irn_mode(y);
1950 /* Determine the right mode for the Add. */
1953 else if (mode_is_reference(m_b))
1955 else if (mode_is_reference(m_y))
1959 * Both modes are different but none is reference,
1960 * happens for instance in SubP(SubP(P, Iu), Is).
1961 * We have two possibilities here: Cast or ignore.
1962 * Currently we ignore this case.
1967 add = new_r_Add(current_ir_graph, blk, y, b, mode);
1970 set_Sub_right(n, add);
1971 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
1978 * Transform Mul(a,-1) into -a.
1979 * Do architecture dependent optimizations on Mul nodes
1981 static ir_node *transform_node_Mul(ir_node *n) {
1983 ir_mode *mode = get_irn_mode(n);
1985 if (mode_is_signed(mode)) {
1987 ir_node *a = get_Mul_left(n);
1988 ir_node *b = get_Mul_right(n);
1990 if (value_of(a) == get_mode_minus_one(mode))
1992 else if (value_of(b) == get_mode_minus_one(mode))
1995 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1996 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2000 return arch_dep_replace_mul_with_shifts(n);
2004 * transform a Div Node
2006 static ir_node *transform_node_Div(ir_node *n)
2008 tarval *tv = value_of(n);
2011 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2013 if (tv != tarval_bad) {
2014 value = new_Const(get_tarval_mode(tv), tv);
2016 DBG_OPT_CSTEVAL(n, value);
2018 else /* Try architecture dependent optimization */
2019 value = arch_dep_replace_div_by_const(n);
2022 /* Turn Div into a tuple (mem, bad, value) */
2023 ir_node *mem = get_Div_mem(n);
2025 turn_into_tuple(n, pn_Div_max);
2026 set_Tuple_pred(n, pn_Div_M, mem);
2027 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2028 set_Tuple_pred(n, pn_Div_res, value);
2034 * transform a Mod node
2036 static ir_node *transform_node_Mod(ir_node *n)
2038 tarval *tv = value_of(n);
2041 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2043 if (tv != tarval_bad) {
2044 value = new_Const(get_tarval_mode(tv), tv);
2046 DBG_OPT_CSTEVAL(n, value);
2048 else /* Try architecture dependent optimization */
2049 value = arch_dep_replace_mod_by_const(n);
2052 /* Turn Mod into a tuple (mem, bad, value) */
2053 ir_node *mem = get_Mod_mem(n);
2055 turn_into_tuple(n, pn_Mod_max);
2056 set_Tuple_pred(n, pn_Mod_M, mem);
2057 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2058 set_Tuple_pred(n, pn_Mod_res, value);
2064 * transform a DivMod node
2066 static ir_node *transform_node_DivMod(ir_node *n)
2070 ir_node *a = get_DivMod_left(n);
2071 ir_node *b = get_DivMod_right(n);
2072 ir_mode *mode = get_irn_mode(a);
2073 tarval *ta = value_of(a);
2074 tarval *tb = value_of(b);
2076 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2079 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2081 if (tb != tarval_bad) {
2082 if (tb == get_mode_one(get_tarval_mode(tb))) {
2083 b = new_Const (mode, get_mode_null(mode));
2086 DBG_OPT_CSTEVAL(n, b);
2088 else if (ta != tarval_bad) {
2089 tarval *resa, *resb;
2090 resa = tarval_div (ta, tb);
2091 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2092 Jmp for X result!? */
2093 resb = tarval_mod (ta, tb);
2094 if (resb == tarval_bad) return n; /* Causes exception! */
2095 a = new_Const (mode, resa);
2096 b = new_Const (mode, resb);
2099 DBG_OPT_CSTEVAL(n, a);
2100 DBG_OPT_CSTEVAL(n, b);
2102 else { /* Try architecture dependent optimization */
2103 arch_dep_replace_divmod_by_const(&a, &b, n);
2104 evaluated = a != NULL;
2106 } else if (ta == get_mode_null(mode)) {
2107 /* 0 / non-Const = 0 */
2112 if (evaluated) { /* replace by tuple */
2113 ir_node *mem = get_DivMod_mem(n);
2114 turn_into_tuple(n, pn_DivMod_max);
2115 set_Tuple_pred(n, pn_DivMod_M, mem);
2116 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2117 set_Tuple_pred(n, pn_DivMod_res_div, a);
2118 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2125 * Optimize Abs(x) into x if x is Confirmed >= 0
2126 * Optimize Abs(x) into -x if x is Confirmed <= 0
2128 static ir_node *transform_node_Abs(ir_node *n)
2131 ir_node *a = get_Abs_op(n);
2132 value_classify sign = classify_value_sign(a);
2134 if (sign == VALUE_NEGATIVE) {
2135 ir_mode *mode = get_irn_mode(n);
2138 * We can replace the Abs by -x here.
2139 * We even could add a new Confirm here.
2141 * Note that -x would create a new node, so we could
2142 * not run it in the equivalent_node() context.
2144 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2145 get_irn_n(n, -1), a, mode);
2147 DBG_OPT_CONFIRM(oldn, n);
2149 else if (sign == VALUE_POSITIVE) {
2150 /* n is positive, Abs is not needed */
2153 DBG_OPT_CONFIRM(oldn, n);
2160 * transform a Cond node
2162 static ir_node *transform_node_Cond(ir_node *n)
2164 /* Replace the Cond by a Jmp if it branches on a constant
2167 ir_node *a = get_Cond_selector(n);
2168 tarval *ta = value_of(a);
2170 /* we need block info which is not available in floating irgs */
2171 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2174 if ((ta != tarval_bad) &&
2175 (get_irn_mode(a) == mode_b) &&
2176 (get_opt_unreachable_code())) {
2177 /* It's a boolean Cond, branching on a boolean constant.
2178 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2179 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2180 turn_into_tuple(n, pn_Cond_max);
2181 if (ta == tarval_b_true) {
2182 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2183 set_Tuple_pred(n, pn_Cond_true, jmp);
2185 set_Tuple_pred(n, pn_Cond_false, jmp);
2186 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2188 /* We might generate an endless loop, so keep it alive. */
2189 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2197 static ir_node *transform_node_Eor(ir_node *n)
2200 ir_node *a = get_Eor_left(n);
2201 ir_node *b = get_Eor_right(n);
2202 ir_mode *mode = get_irn_mode(n);
2206 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2207 mode, get_mode_null(mode));
2208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2210 else if ((mode == mode_b)
2211 && (get_irn_op(a) == op_Proj)
2212 && (get_irn_mode(a) == mode_b)
2213 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2214 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2215 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2216 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2217 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2221 else if ((mode == mode_b)
2222 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2223 /* The Eor is a Not. Replace it by a Not. */
2224 /* ????!!!Extend to bitfield 1111111. */
2225 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2234 * Transform a boolean Not.
2236 static ir_node *transform_node_Not(ir_node *n)
2239 ir_node *a = get_Not_op(n);
2241 if ( (get_irn_mode(n) == mode_b)
2242 && (get_irn_op(a) == op_Proj)
2243 && (get_irn_mode(a) == mode_b)
2244 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2245 /* We negate a Cmp. The Cmp has the negated result anyways! */
2246 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2247 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2248 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2255 * Transform a Cast_type(Const) into a new Const_type
2257 static ir_node *transform_node_Cast(ir_node *n) {
2259 ir_node *pred = get_Cast_op(n);
2260 ir_type *tp = get_irn_type(n);
2262 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2263 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2264 get_Const_tarval(pred), tp);
2265 DBG_OPT_CSTEVAL(oldn, n);
2266 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2267 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2268 get_SymConst_kind(pred), tp);
2269 DBG_OPT_CSTEVAL(oldn, n);
2276 * Transform a Proj(Div) with a non-zero value.
2277 * Removes the exceptions and routes the memory to the NoMem node.
2279 static ir_node *transform_node_Proj_Div(ir_node *proj)
2281 ir_node *n = get_Proj_pred(proj);
2282 ir_node *b = get_Div_right(n);
2285 if (value_not_zero(b)) {
2286 /* div(x, y) && y != 0 */
2287 proj_nr = get_Proj_proj(proj);
2289 /* this node may float if it did not depend on a Confirm */
2290 set_irn_pinned(n, op_pin_state_floats);
2292 if (proj_nr == pn_Div_X_except) {
2293 /* we found an exception handler, remove it */
2294 DBG_OPT_EXC_REM(proj);
2297 else if (proj_nr == pn_Div_M) {
2298 ir_node *res = get_Div_mem(n);
2299 /* the memory Proj can only be removed if we divide by a
2300 real constant, but the node never produce a new memory */
2301 if (value_of(b) != tarval_bad) {
2302 /* this is a Div by a const, we can remove the memory edge */
2303 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2312 * Transform a Proj(Mod) with a non-zero value.
2313 * Removes the exceptions and routes the memory to the NoMem node.
2315 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2317 ir_node *n = get_Proj_pred(proj);
2318 ir_node *b = get_Mod_right(n);
2321 if (value_not_zero(b)) {
2322 /* mod(x, y) && y != 0 */
2323 proj_nr = get_Proj_proj(proj);
2325 /* this node may float if it did not depend on a Confirm */
2326 set_irn_pinned(n, op_pin_state_floats);
2328 if (proj_nr == pn_Mod_X_except) {
2329 /* we found an exception handler, remove it */
2330 DBG_OPT_EXC_REM(proj);
2332 } else if (proj_nr == pn_Mod_M) {
2333 ir_node *res = get_Mod_mem(n);
2334 /* the memory Proj can only be removed if we divide by a
2335 real constant, but the node never produce a new memory */
2336 if (value_of(b) != tarval_bad) {
2337 /* this is a Mod by a const, we can remove the memory edge */
2338 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2342 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2343 /* a % a = 0 if a != 0 */
2344 ir_mode *mode = get_irn_mode(proj);
2345 ir_node *res = new_Const(mode, get_mode_null(mode));
2347 DBG_OPT_CSTEVAL(n, res);
2355 * Transform a Proj(DivMod) with a non-zero value.
2356 * Removes the exceptions and routes the memory to the NoMem node.
2358 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2360 ir_node *n = get_Proj_pred(proj);
2361 ir_node *b = get_DivMod_right(n);
2364 if (value_not_zero(b)) {
2365 /* DivMod(x, y) && y != 0 */
2366 proj_nr = get_Proj_proj(proj);
2368 /* this node may float if it did not depend on a Confirm */
2369 set_irn_pinned(n, op_pin_state_floats);
2371 if (proj_nr == pn_DivMod_X_except) {
2372 /* we found an exception handler, remove it */
2373 DBG_OPT_EXC_REM(proj);
2376 else if (proj_nr == pn_DivMod_M) {
2377 ir_node *res = get_DivMod_mem(n);
2378 /* the memory Proj can only be removed if we divide by a
2379 real constant, but the node never produce a new memory */
2380 if (value_of(b) != tarval_bad) {
2381 /* this is a DivMod by a const, we can remove the memory edge */
2382 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2386 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2387 /* a % a = 0 if a != 0 */
2388 ir_mode *mode = get_irn_mode(proj);
2389 ir_node *res = new_Const(mode, get_mode_null(mode));
2391 DBG_OPT_CSTEVAL(n, res);
2399 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2401 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2403 if (get_opt_unreachable_code()) {
2404 ir_node *n = get_Proj_pred(proj);
2405 ir_node *b = get_Cond_selector(n);
2407 if (mode_is_int(get_irn_mode(b))) {
2408 tarval *tb = value_of(b);
2410 if (tb != tarval_bad) {
2411 /* we have a constant switch */
2412 long num = get_Proj_proj(proj);
2414 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2415 if (get_tarval_long(tb) == num) {
2416 /* Do NOT create a jump here, or we will have 2 control flow ops
2417 * in a block. This case is optimized away in optimize_cf(). */
2421 /* this case will NEVER be taken, kill it */
2432 * Normalizes and optimizes Cmp nodes.
2434 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2436 if (get_opt_reassociation()) {
2437 ir_node *n = get_Proj_pred(proj);
2438 ir_node *left = get_Cmp_left(n);
2439 ir_node *right = get_Cmp_right(n);
2443 ir_mode *mode = NULL;
2444 long proj_nr = get_Proj_proj(proj);
2447 * First step: normalize the compare op
2448 * by placing the constant on the right site
2449 * or moving the lower address node to the left.
2450 * We ignore the case that both are constants
2451 * this case should be optimized away.
2453 if (get_irn_op(right) == op_Const)
2455 else if (get_irn_op(left) == op_Const) {
2460 proj_nr = get_inversed_pnc(proj_nr);
2463 else if (get_irn_idx(left) > get_irn_idx(right)) {
2469 proj_nr = get_inversed_pnc(proj_nr);
2474 * Second step: Try to reduce the magnitude
2475 * of a constant. This may help to generate better code
2476 * later and may help to normalize more compares.
2477 * Of course this is only possible for integer values.
2480 mode = get_irn_mode(c);
2481 tv = get_Const_tarval(c);
2483 if (tv != tarval_bad) {
2484 /* the following optimization is possible on modes without Overflow
2485 * on Unary Minus or on == and !=:
2486 * -a CMP c ==> a swap(CMP) -c
2488 * Beware: for two-complement Overflow may occur, so only == and != can
2489 * be optimized, see this:
2490 * -MININT < 0 =/=> MININT > 0 !!!
2492 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2493 (!mode_overflow_on_unary_Minus(mode) ||
2494 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2495 left = get_Minus_op(left);
2496 tv = tarval_sub(get_mode_null(mode), tv);
2498 proj_nr = get_inversed_pnc(proj_nr);
2502 /* for integer modes, we have more */
2503 if (mode_is_int(mode)) {
2504 /* Ne includes Unordered which is not possible on integers.
2505 * However, frontends often use this wrong, so fix it here */
2506 if (proj_nr & pn_Cmp_Uo) {
2507 proj_nr &= ~pn_Cmp_Uo;
2508 set_Proj_proj(proj, proj_nr);
2511 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2512 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2513 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2514 tv = tarval_sub(tv, get_mode_one(mode));
2516 proj_nr ^= pn_Cmp_Eq;
2519 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2520 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2521 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2522 tv = tarval_add(tv, get_mode_one(mode));
2524 proj_nr ^= pn_Cmp_Eq;
2528 /* the following reassociations work only for == and != */
2529 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2531 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2532 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2533 right = get_Sub_right(left);
2534 left = get_Sub_left(left);
2536 tv = value_of(right);
2540 if (tv != tarval_bad) {
2541 ir_op *op = get_irn_op(left);
2543 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2545 ir_node *c1 = get_Sub_right(left);
2546 tarval *tv2 = value_of(c1);
2548 if (tv2 != tarval_bad) {
2549 tv2 = tarval_add(tv, value_of(c1));
2551 if (tv2 != tarval_bad) {
2552 left = get_Sub_left(left);
2558 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2559 else if (op == op_Add) {
2560 ir_node *a_l = get_Add_left(left);
2561 ir_node *a_r = get_Add_right(left);
2565 if (get_irn_op(a_l) == op_Const) {
2567 tv2 = value_of(a_l);
2571 tv2 = value_of(a_r);
2574 if (tv2 != tarval_bad) {
2575 tv2 = tarval_sub(tv, tv2);
2577 if (tv2 != tarval_bad) {
2584 /* -a == c ==> a == -c, -a != c ==> a != -c */
2585 else if (op == op_Minus) {
2586 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2588 if (tv2 != tarval_bad) {
2589 left = get_Minus_op(left);
2596 /* the following reassociations work only for <= */
2597 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2598 if (tv != tarval_bad) {
2599 ir_op *op = get_irn_op(left);
2601 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2609 * optimization for AND:
2611 * And(x, C) == C ==> And(x, C) != 0
2612 * And(x, C) != C ==> And(X, C) == 0
2614 * if C is a single Bit constant.
2616 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2617 (get_irn_op(left) == op_And)) {
2618 if (is_single_bit_tarval(tv)) {
2619 /* check for Constant's match. We have check hare the tarvals,
2620 because our const might be changed */
2621 ir_node *la = get_And_left(left);
2622 ir_node *ra = get_And_right(left);
2623 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2624 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2625 /* fine: do the transformation */
2626 tv = get_mode_null(get_tarval_mode(tv));
2627 proj_nr ^= pn_Cmp_Leg;
2632 } /* tarval != bad */
2636 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2638 if (changed & 2) /* need a new Const */
2639 right = new_Const(mode, tv);
2641 /* create a new compare */
2642 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2645 set_Proj_pred(proj, n);
2646 set_Proj_proj(proj, proj_nr);
2653 * Does all optimizations on nodes that must be done on it's Proj's
2654 * because of creating new nodes.
2656 static ir_node *transform_node_Proj(ir_node *proj)
2658 ir_node *n = get_Proj_pred(proj);
2660 switch (get_irn_opcode(n)) {
2662 return transform_node_Proj_Div(proj);
2665 return transform_node_Proj_Mod(proj);
2668 return transform_node_Proj_DivMod(proj);
2671 return transform_node_Proj_Cond(proj);
2674 return transform_node_Proj_Cmp(proj);
2677 /* should not happen, but if it does will be optimized away */
2678 return equivalent_node_Proj(proj);
2687 * Move Confirms down through Phi nodes.
2689 static ir_node *transform_node_Phi(ir_node *phi) {
2691 ir_mode *mode = get_irn_mode(phi);
2693 if (mode_is_reference(mode)) {
2694 n = get_irn_arity(phi);
2696 /* Beware of Phi0 */
2698 ir_node *pred = get_irn_n(phi, 0);
2699 ir_node *bound, *new_Phi, *block, **in;
2702 if (! is_Confirm(pred))
2705 bound = get_Confirm_bound(pred);
2706 pnc = get_Confirm_cmp(pred);
2708 NEW_ARR_A(ir_node *, in, n);
2709 in[0] = get_Confirm_value(pred);
2711 for (i = 1; i < n; ++i) {
2712 pred = get_irn_n(phi, i);
2714 if (! is_Confirm(pred) ||
2715 get_Confirm_bound(pred) != bound ||
2716 get_Confirm_cmp(pred) != pnc)
2718 in[i] = get_Confirm_value(pred);
2720 /* move the Confirm nodes "behind" the Phi */
2721 block = get_irn_n(phi, -1);
2722 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2723 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2730 * returns the operands of a commutative bin-op, if one operand is
2731 * a const, it is returned as the second one.
2733 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2735 ir_node *op_a = get_binop_left(binop);
2736 ir_node *op_b = get_binop_right(binop);
2738 assert(is_op_commutative(get_irn_op(binop)));
2740 if (get_irn_op(op_a) == op_Const) {
2751 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2752 * Such pattern may arise in bitfield stores.
2754 * value c4 value c4 & c2
2755 * AND c3 AND c1 | c3
2760 static ir_node *transform_node_Or_bf_store(ir_node *or)
2764 ir_node *and_l, *c3;
2765 ir_node *value, *c4;
2766 ir_node *new_and, *new_const, *block;
2767 ir_mode *mode = get_irn_mode(or);
2769 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2771 get_comm_Binop_Ops(or, &and, &c1);
2772 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2775 get_comm_Binop_Ops(and, &or_l, &c2);
2776 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2779 get_comm_Binop_Ops(or_l, &and_l, &c3);
2780 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2783 get_comm_Binop_Ops(and_l, &value, &c4);
2784 if (get_irn_op(c4) != op_Const)
2787 /* ok, found the pattern, check for conditions */
2788 assert(mode == get_irn_mode(and));
2789 assert(mode == get_irn_mode(or_l));
2790 assert(mode == get_irn_mode(and_l));
2792 tv1 = get_Const_tarval(c1);
2793 tv2 = get_Const_tarval(c2);
2794 tv3 = get_Const_tarval(c3);
2795 tv4 = get_Const_tarval(c4);
2797 tv = tarval_or(tv4, tv2);
2798 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2799 /* have at least one 0 at the same bit position */
2803 n_tv4 = tarval_not(tv4);
2804 if (tv3 != tarval_and(tv3, n_tv4)) {
2805 /* bit in the or_mask is outside the and_mask */
2809 n_tv2 = tarval_not(tv2);
2810 if (tv1 != tarval_and(tv1, n_tv2)) {
2811 /* bit in the or_mask is outside the and_mask */
2815 /* ok, all conditions met */
2816 block = get_irn_n(or, -1);
2818 new_and = new_r_And(current_ir_graph, block,
2819 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2821 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2823 set_Or_left(or, new_and);
2824 set_Or_right(or, new_const);
2826 /* check for more */
2827 return transform_node_Or_bf_store(or);
2831 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2833 static ir_node *transform_node_Or_Rot(ir_node *or)
2835 ir_mode *mode = get_irn_mode(or);
2836 ir_node *shl, *shr, *block;
2837 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2840 if (! mode_is_int(mode))
2843 shl = get_binop_left(or);
2844 shr = get_binop_right(or);
2846 if (get_irn_op(shl) == op_Shr) {
2847 if (get_irn_op(shr) != op_Shl)
2854 else if (get_irn_op(shl) != op_Shl)
2856 else if (get_irn_op(shr) != op_Shr)
2859 x = get_Shl_left(shl);
2860 if (x != get_Shr_left(shr))
2863 c1 = get_Shl_right(shl);
2864 c2 = get_Shr_right(shr);
2865 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2866 tv1 = get_Const_tarval(c1);
2867 if (! tarval_is_long(tv1))
2870 tv2 = get_Const_tarval(c2);
2871 if (! tarval_is_long(tv2))
2874 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2875 != get_mode_size_bits(mode))
2878 /* yet, condition met */
2879 block = get_irn_n(or, -1);
2881 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2883 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2886 else if (get_irn_op(c1) == op_Sub) {
2890 if (get_Sub_right(sub) != v)
2893 c1 = get_Sub_left(sub);
2894 if (get_irn_op(c1) != op_Const)
2897 tv1 = get_Const_tarval(c1);
2898 if (! tarval_is_long(tv1))
2901 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2904 /* yet, condition met */
2905 block = get_nodes_block(or);
2907 /* a Rot right is not supported, so use a rot left */
2908 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2910 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2913 else if (get_irn_op(c2) == op_Sub) {
2917 c1 = get_Sub_left(sub);
2918 if (get_irn_op(c1) != op_Const)
2921 tv1 = get_Const_tarval(c1);
2922 if (! tarval_is_long(tv1))
2925 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2928 /* yet, condition met */
2929 block = get_irn_n(or, -1);
2932 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2934 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2944 static ir_node *transform_node_Or(ir_node *or)
2946 or = transform_node_Or_bf_store(or);
2947 or = transform_node_Or_Rot(or);
2953 static ir_node *transform_node(ir_node *n);
2956 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2958 * Should be moved to reassociation?
2960 static ir_node *transform_node_shift(ir_node *n)
2962 ir_node *left, *right;
2963 tarval *tv1, *tv2, *res;
2965 int modulo_shf, flag;
2967 left = get_binop_left(n);
2969 /* different operations */
2970 if (get_irn_op(left) != get_irn_op(n))
2973 right = get_binop_right(n);
2974 tv1 = value_of(right);
2975 if (tv1 == tarval_bad)
2978 tv2 = value_of(get_binop_right(left));
2979 if (tv2 == tarval_bad)
2982 res = tarval_add(tv1, tv2);
2984 /* beware: a simple replacement works only, if res < modulo shift */
2985 mode = get_irn_mode(n);
2989 modulo_shf = get_mode_modulo_shift(mode);
2990 if (modulo_shf > 0) {
2991 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2993 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3000 /* ok, we can replace it */
3001 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3003 in[0] = get_binop_left(left);
3004 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3006 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3008 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3010 return transform_node(irn);
3015 #define transform_node_Shr transform_node_shift
3016 #define transform_node_Shrs transform_node_shift
3017 #define transform_node_Shl transform_node_shift
3020 * Remove dead blocks and nodes in dead blocks
3021 * in keep alive list. We do not generate a new End node.
3023 static ir_node *transform_node_End(ir_node *n) {
3024 int i, n_keepalives = get_End_n_keepalives(n);
3026 for (i = 0; i < n_keepalives; ++i) {
3027 ir_node *ka = get_End_keepalive(n, i);
3029 if (is_Block_dead(ka)) {
3030 set_End_keepalive(n, i, new_Bad());
3033 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3034 set_End_keepalive(n, i, new_Bad());
3040 * Optimize a Mux into some simpler cases.
3042 static ir_node *transform_node_Mux(ir_node *n)
3044 ir_node *oldn = n, *sel = get_Mux_sel(n);
3045 ir_mode *mode = get_irn_mode(n);
3047 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3048 ir_node *cmp = get_Proj_pred(sel);
3049 long proj_nr = get_Proj_proj(sel);
3050 ir_node *f = get_Mux_false(n);
3051 ir_node *t = get_Mux_true(n);
3053 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3054 ir_node *block = get_irn_n(n, -1);
3057 * Note: normalization puts the constant on the right site,
3058 * so we check only one case.
3060 * Note further that these optimization work even for floating point
3061 * with NaN's because -NaN == NaN.
3062 * However, if +0 and -0 is handled differently, we cannot use the first one.
3064 if (get_irn_op(f) == op_Minus &&
3065 get_Minus_op(f) == t &&
3066 get_Cmp_left(cmp) == t) {
3068 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3069 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3070 n = new_rd_Abs(get_irn_dbg_info(n),
3074 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3077 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3078 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3079 n = new_rd_Abs(get_irn_dbg_info(n),
3083 n = new_rd_Minus(get_irn_dbg_info(n),
3088 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3092 else if (get_irn_op(t) == op_Minus &&
3093 get_Minus_op(t) == f &&
3094 get_Cmp_left(cmp) == f) {
3096 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3097 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3098 n = new_rd_Abs(get_irn_dbg_info(n),
3102 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3105 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3106 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3107 n = new_rd_Abs(get_irn_dbg_info(n),
3111 n = new_rd_Minus(get_irn_dbg_info(n),
3116 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3121 if (mode_is_int(mode) && mode_is_signed(mode) &&
3122 get_mode_arithmetic(mode) == irma_twos_complement) {
3123 ir_node *x = get_Cmp_left(cmp);
3125 /* the following optimization works only with signed integer two-complement mode */
3127 if (mode == get_irn_mode(x)) {
3129 * FIXME: this restriction is two rigid, as it would still
3130 * work if mode(x) = Hs and mode == Is, but at least it removes
3133 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3134 classify_Const(t) == CNST_ALL_ONE &&
3135 classify_Const(f) == CNST_NULL) {
3137 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3141 n = new_rd_Shrs(get_irn_dbg_info(n),
3142 current_ir_graph, block, x,
3143 new_r_Const_long(current_ir_graph, block, mode_Iu,
3144 get_mode_size_bits(mode) - 1),
3146 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3149 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3150 classify_Const(t) == CNST_ONE &&
3151 classify_Const(f) == CNST_NULL) {
3153 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3157 n = new_rd_Shr(get_irn_dbg_info(n),
3158 current_ir_graph, block,
3159 new_r_Minus(current_ir_graph, block, x, mode),
3160 new_r_Const_long(current_ir_graph, block, mode_Iu,
3161 get_mode_size_bits(mode) - 1),
3163 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3170 return arch_transform_node_Mux(n);
3174 * Optimize a Psi into some simpler cases.
3176 static ir_node *transform_node_Psi(ir_node *n) {
3178 return transform_node_Mux(n);
3184 * Tries several [inplace] [optimizing] transformations and returns an
3185 * equivalent node. The difference to equivalent_node() is that these
3186 * transformations _do_ generate new nodes, and thus the old node must
3187 * not be freed even if the equivalent node isn't the old one.
3189 static ir_node *transform_node(ir_node *n)
3191 if (n->op->ops.transform_node)
3192 n = n->op->ops.transform_node(n);
3197 * sSets the default transform node operation for an ir_op_ops.
3199 * @param code the opcode for the default operation
3200 * @param ops the operations initialized
3205 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3209 ops->transform_node = transform_node_##a; \
3243 /* **************** Common Subexpression Elimination **************** */
3245 /** The size of the hash table used, should estimate the number of nodes
3247 #define N_IR_NODES 512
3249 /** Compares the attributes of two Const nodes. */
3250 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3252 return (get_Const_tarval(a) != get_Const_tarval(b))
3253 || (get_Const_type(a) != get_Const_type(b));
3256 /** Compares the attributes of two Proj nodes. */
3257 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3259 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3262 /** Compares the attributes of two Filter nodes. */
3263 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3265 return get_Filter_proj(a) != get_Filter_proj(b);
3268 /** Compares the attributes of two Alloc nodes. */
3269 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3271 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3272 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3275 /** Compares the attributes of two Free nodes. */
3276 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3278 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3279 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3282 /** Compares the attributes of two SymConst nodes. */
3283 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3285 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3286 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3287 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3290 /** Compares the attributes of two Call nodes. */
3291 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3293 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3296 /** Compares the attributes of two Sel nodes. */
3297 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3299 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3300 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3301 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3302 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3303 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3306 /** Compares the attributes of two Phi nodes. */
3307 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3309 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3312 /** Compares the attributes of two Cast nodes. */
3313 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3315 return get_Cast_type(a) != get_Cast_type(b);
3318 /** Compares the attributes of two Load nodes. */
3319 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3321 if (get_Load_volatility(a) == volatility_is_volatile ||
3322 get_Load_volatility(b) == volatility_is_volatile)
3323 /* NEVER do CSE on volatile Loads */
3326 return get_Load_mode(a) != get_Load_mode(b);
3329 /** Compares the attributes of two Store nodes. */
3330 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3332 /* NEVER do CSE on volatile Stores */
3333 return (get_Store_volatility(a) == volatility_is_volatile ||
3334 get_Store_volatility(b) == volatility_is_volatile);
3337 /** Compares the attributes of two Confirm nodes. */
3338 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3340 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3344 * Set the default node attribute compare operation for an ir_op_ops.
3346 * @param code the opcode for the default operation
3347 * @param ops the operations initialized
3352 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3356 ops->node_cmp_attr = node_cmp_attr_##a; \
3382 * Compare function for two nodes in the hash table. Gets two
3383 * nodes as parameters. Returns 0 if the nodes are a cse.
3385 int identities_cmp(const void *elt, const void *key)
3393 if (a == b) return 0;
3395 if ((get_irn_op(a) != get_irn_op(b)) ||
3396 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3398 /* compare if a's in and b's in are of equal length */
3399 irn_arity_a = get_irn_intra_arity (a);
3400 if (irn_arity_a != get_irn_intra_arity(b))
3403 /* for block-local cse and op_pin_state_pinned nodes: */
3404 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3405 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3409 /* compare a->in[0..ins] with b->in[0..ins] */
3410 for (i = 0; i < irn_arity_a; i++)
3411 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3415 * here, we already now that the nodes are identical except their
3418 if (a->op->ops.node_cmp_attr)
3419 return a->op->ops.node_cmp_attr(a, b);
3425 * Calculate a hash value of a node.
3428 ir_node_hash (ir_node *node)
3433 if (node->op == op_Const) {
3434 /* special value for const, as they only differ in their tarval. */
3435 h = HASH_PTR(node->attr.con.tv);
3436 h = 9*h + HASH_PTR(get_irn_mode(node));
3437 } else if (node->op == op_SymConst) {
3438 /* special value for const, as they only differ in their symbol. */
3439 h = HASH_PTR(node->attr.i.sym.type_p);
3440 h = 9*h + HASH_PTR(get_irn_mode(node));
3443 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3444 h = irn_arity = get_irn_intra_arity(node);
3446 /* consider all in nodes... except the block if not a control flow. */
3447 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3448 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3452 h = 9*h + HASH_PTR(get_irn_mode(node));
3454 h = 9*h + HASH_PTR(get_irn_op(node));
3461 new_identities(void) {
3462 return new_pset(identities_cmp, N_IR_NODES);
3466 del_identities(pset *value_table) {
3467 del_pset(value_table);
3471 * Return the canonical node computing the same value as n.
3472 * Looks up the node in a hash table.
3474 * For Const nodes this is performed in the constructor, too. Const
3475 * nodes are extremely time critical because of their frequent use in
3476 * constant string arrays.
3478 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3482 if (!value_table) return n;
3484 if (get_opt_reassociation()) {
3485 if (is_op_commutative(get_irn_op(n))) {
3486 ir_node *l = get_binop_left(n);
3487 ir_node *r = get_binop_right(n);
3489 /* for commutative operators perform a OP b == b OP a */
3490 if (get_irn_idx(l) > get_irn_idx(r)) {
3491 set_binop_left(n, r);
3492 set_binop_right(n, l);
3497 o = pset_find(value_table, n, ir_node_hash (n));
3506 * During construction we set the op_pin_state_pinned flag in the graph right when the
3507 * optimization is performed. The flag turning on procedure global cse could
3508 * be changed between two allocations. This way we are safe.
3510 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3513 n = identify(value_table, n);
3514 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3515 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3520 * Return the canonical node computing the same value as n.
3521 * Looks up the node in a hash table, enters it in the table
3522 * if it isn't there yet.
3524 ir_node *identify_remember(pset *value_table, ir_node *n)
3528 if (!value_table) return n;
3530 if (get_opt_reassociation()) {
3531 if (is_op_commutative(get_irn_op(n))) {
3532 ir_node *l = get_binop_left(n);
3533 ir_node *r = get_binop_right(n);
3535 /* for commutative operators perform a OP b == b OP a */
3537 set_binop_left(n, r);
3538 set_binop_right(n, l);
3543 /* lookup or insert in hash table with given hash key. */
3544 o = pset_insert (value_table, n, ir_node_hash (n));
3553 /* Add a node to the identities value table. */
3554 void add_identities(pset *value_table, ir_node *node) {
3555 if (get_opt_cse() && is_no_Block(node))
3556 identify_remember(value_table, node);
3559 /* Visit each node in the value table of a graph. */
3560 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3562 ir_graph *rem = current_ir_graph;
3564 current_ir_graph = irg;
3565 foreach_pset(irg->value_table, node)
3567 current_ir_graph = rem;
3571 * garbage in, garbage out. If a node has a dead input, i.e., the
3572 * Bad node is input to the node, return the Bad node.
3574 static INLINE ir_node *gigo(ir_node *node)
3577 ir_op *op = get_irn_op(node);
3579 /* remove garbage blocks by looking at control flow that leaves the block
3580 and replacing the control flow by Bad. */
3581 if (get_irn_mode(node) == mode_X) {
3582 ir_node *block = get_nodes_block(skip_Proj(node));
3584 /* Don't optimize nodes in immature blocks. */
3585 if (!get_Block_matured(block)) return node;
3586 /* Don't optimize End, may have Bads. */
3587 if (op == op_End) return node;
3589 if (is_Block(block)) {
3590 irn_arity = get_irn_arity(block);
3591 for (i = 0; i < irn_arity; i++) {
3592 if (!is_Bad(get_irn_n(block, i)))
3595 if (i == irn_arity) return new_Bad();
3599 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3600 blocks predecessors is dead. */
3601 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3602 irn_arity = get_irn_arity(node);
3605 * Beware: we can only read the block of a non-floating node.
3607 if (is_irn_pinned_in_irg(node) &&
3608 is_Block_dead(get_nodes_block(node)))
3611 for (i = 0; i < irn_arity; i++) {
3612 ir_node *pred = get_irn_n(node, i);
3617 /* Propagating Unknowns here seems to be a bad idea, because
3618 sometimes we need a node as a input and did not want that
3620 However, i might be useful to move this into a later phase
3621 (it you thing optimizing such code is useful). */
3622 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3623 return new_Unknown(get_irn_mode(node));
3628 /* With this code we violate the agreement that local_optimize
3629 only leaves Bads in Block, Phi and Tuple nodes. */
3630 /* If Block has only Bads as predecessors it's garbage. */
3631 /* If Phi has only Bads as predecessors it's garbage. */
3632 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3633 irn_arity = get_irn_arity(node);
3634 for (i = 0; i < irn_arity; i++) {
3635 if (!is_Bad(get_irn_n(node, i))) break;
3637 if (i == irn_arity) node = new_Bad();
3644 * These optimizations deallocate nodes from the obstack.
3645 * It can only be called if it is guaranteed that no other nodes
3646 * reference this one, i.e., right after construction of a node.
3648 * current_ir_graph must be set to the graph of the node!
3650 ir_node *optimize_node(ir_node *n)
3654 opcode iro = get_irn_opcode(n);
3656 /* Always optimize Phi nodes: part of the construction. */
3657 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3659 /* constant expression evaluation / constant folding */
3660 if (get_opt_constant_folding()) {
3661 /* neither constants nor Tuple values can be evaluated */
3662 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3663 /* try to evaluate */
3664 tv = computed_value(n);
3665 if (tv != tarval_bad) {
3667 ir_type *old_tp = get_irn_type(n);
3668 int i, arity = get_irn_arity(n);
3672 * Try to recover the type of the new expression.
3674 for (i = 0; i < arity && !old_tp; ++i)
3675 old_tp = get_irn_type(get_irn_n(n, i));
3678 * we MUST copy the node here temporary, because it's still needed
3679 * for DBG_OPT_CSTEVAL
3681 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3682 oldn = alloca(node_size);
3684 memcpy(oldn, n, node_size);
3685 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3687 /* ARG, copy the in array, we need it for statistics */
3688 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3690 /* note the inplace edges module */
3691 edges_node_deleted(n, current_ir_graph);
3693 /* evaluation was successful -- replace the node. */
3694 irg_kill_node(current_ir_graph, n);
3695 nw = new_Const(get_tarval_mode (tv), tv);
3697 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3698 set_Const_type(nw, old_tp);
3699 DBG_OPT_CSTEVAL(oldn, nw);
3705 /* remove unnecessary nodes */
3706 if (get_opt_constant_folding() ||
3707 (iro == iro_Phi) || /* always optimize these nodes. */
3709 (iro == iro_Proj) ||
3710 (iro == iro_Block) ) /* Flags tested local. */
3711 n = equivalent_node (n);
3713 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3715 /* Common Subexpression Elimination.
3717 * Checks whether n is already available.
3718 * The block input is used to distinguish different subexpressions. Right
3719 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3720 * subexpressions within a block.
3723 n = identify_cons (current_ir_graph->value_table, n);
3726 edges_node_deleted(oldn, current_ir_graph);
3728 /* We found an existing, better node, so we can deallocate the old node. */
3729 irg_kill_node(current_ir_graph, oldn);
3733 /* Some more constant expression evaluation that does not allow to
3735 iro = get_irn_opcode(n);
3736 if (get_opt_constant_folding() ||
3737 (iro == iro_Cond) ||
3738 (iro == iro_Proj) ||
3739 (iro == iro_Sel)) /* Flags tested local. */
3740 n = transform_node (n);
3742 /* Remove nodes with dead (Bad) input.
3743 Run always for transformation induced Bads. */
3746 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3747 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3748 n = identify_remember (current_ir_graph->value_table, n);
3756 * These optimizations never deallocate nodes (in place). This can cause dead
3757 * nodes lying on the obstack. Remove these by a dead node elimination,
3758 * i.e., a copying garbage collection.
3760 ir_node *optimize_in_place_2(ir_node *n)
3764 opcode iro = get_irn_opcode(n);
3766 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3768 /* constant expression evaluation / constant folding */
3769 if (get_opt_constant_folding()) {
3770 /* neither constants nor Tuple values can be evaluated */
3771 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3772 /* try to evaluate */
3773 tv = computed_value(n);
3774 if (tv != tarval_bad) {
3775 /* evaluation was successful -- replace the node. */
3776 ir_type *old_tp = get_irn_type(n);
3777 int i, arity = get_irn_arity(n);
3780 * Try to recover the type of the new expression.
3782 for (i = 0; i < arity && !old_tp; ++i)
3783 old_tp = get_irn_type(get_irn_n(n, i));
3785 n = new_Const(get_tarval_mode(tv), tv);
3787 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3788 set_Const_type(n, old_tp);
3790 DBG_OPT_CSTEVAL(oldn, n);
3796 /* remove unnecessary nodes */
3797 if (get_opt_constant_folding() ||
3798 (iro == iro_Phi) || /* always optimize these nodes. */
3799 (iro == iro_Id) || /* ... */
3800 (iro == iro_Proj) || /* ... */
3801 (iro == iro_Block) ) /* Flags tested local. */
3802 n = equivalent_node(n);
3804 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3806 /** common subexpression elimination **/
3807 /* Checks whether n is already available. */
3808 /* The block input is used to distinguish different subexpressions. Right
3809 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3810 subexpressions within a block. */
3811 if (get_opt_cse()) {
3812 n = identify(current_ir_graph->value_table, n);
3815 /* Some more constant expression evaluation. */
3816 iro = get_irn_opcode(n);
3817 if (get_opt_constant_folding() ||
3818 (iro == iro_Cond) ||
3819 (iro == iro_Proj) ||
3820 (iro == iro_Sel)) /* Flags tested local. */
3821 n = transform_node(n);
3823 /* Remove nodes with dead (Bad) input.
3824 Run always for transformation induced Bads. */
3827 /* Now we can verify the node, as it has no dead inputs any more. */
3830 /* Now we have a legal, useful node. Enter it in hash table for cse.
3831 Blocks should be unique anyways. (Except the successor of start:
3832 is cse with the start block!) */
3833 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3834 n = identify_remember(current_ir_graph->value_table, n);
3840 * Wrapper for external use, set proper status bits after optimization.
3842 ir_node *optimize_in_place(ir_node *n)
3844 /* Handle graph state */
3845 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3847 if (get_opt_global_cse())
3848 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3849 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3850 set_irg_outs_inconsistent(current_ir_graph);
3852 /* FIXME: Maybe we could also test whether optimizing the node can
3853 change the control graph. */
3854 set_irg_doms_inconsistent(current_ir_graph);
3855 return optimize_in_place_2 (n);
3859 * Sets the default operation for an ir_ops.
3861 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3863 ops = firm_set_default_computed_value(code, ops);
3864 ops = firm_set_default_equivalent_node(code, ops);
3865 ops = firm_set_default_transform_node(code, ops);
3866 ops = firm_set_default_node_cmp_attr(code, ops);
3867 ops = firm_set_default_get_type(code, ops);
3868 ops = firm_set_default_get_type_attr(code, ops);
3869 ops = firm_set_default_get_entity_attr(code, ops);