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"
46 /* Make types visible to allow most efficient access */
47 # include "entity_t.h"
50 * return the value of a Constant
52 static tarval *computed_value_Const(ir_node *n)
54 return get_Const_tarval(n);
58 * return the value of a 'sizeof' SymConst
60 static tarval *computed_value_SymConst(ir_node *n)
62 if ((get_SymConst_kind(n) == symconst_size) &&
63 (get_type_state(get_SymConst_type(n))) == layout_fixed)
64 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
69 * return the value of an Add
71 static tarval *computed_value_Add(ir_node *n)
73 ir_node *a = get_Add_left(n);
74 ir_node *b = get_Add_right(n);
76 tarval *ta = value_of(a);
77 tarval *tb = value_of(b);
79 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
80 return tarval_add(ta, tb);
86 * return the value of a Sub
89 static tarval *computed_value_Sub(ir_node *n)
91 ir_node *a = get_Sub_left(n);
92 ir_node *b = get_Sub_right(n);
97 if (a == b && !is_Bad(a))
98 return get_mode_null(get_irn_mode(n));
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_sub(ta, tb);
110 * return the value of a Carry
111 * Special : a op 0, 0 op b
113 static tarval *computed_value_Carry(ir_node *n)
115 ir_node *a = get_binop_left(n);
116 ir_node *b = get_binop_right(n);
117 ir_mode *m = get_irn_mode(n);
119 tarval *ta = value_of(a);
120 tarval *tb = value_of(b);
122 if ((ta != tarval_bad) && (tb != tarval_bad)) {
124 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
126 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
127 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
128 return get_mode_null(m);
134 * return the value of a Borrow
137 static tarval *computed_value_Borrow(ir_node *n)
139 ir_node *a = get_binop_left(n);
140 ir_node *b = get_binop_right(n);
141 ir_mode *m = get_irn_mode(n);
143 tarval *ta = value_of(a);
144 tarval *tb = value_of(b);
146 if ((ta != tarval_bad) && (tb != tarval_bad)) {
147 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
148 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
149 return get_mode_null(m);
155 * return the value of an unary Minus
157 static tarval *computed_value_Minus(ir_node *n)
159 ir_node *a = get_Minus_op(n);
160 tarval *ta = value_of(a);
162 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
163 return tarval_neg(ta);
169 * return the value of a Mul
171 static tarval *computed_value_Mul(ir_node *n)
173 ir_node *a = get_Mul_left(n);
174 ir_node *b = get_Mul_right(n);
176 tarval *ta = value_of(a);
177 tarval *tb = value_of(b);
179 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
180 return tarval_mul(ta, tb);
182 /* a*0 = 0 or 0*b = 0:
183 calls computed_value recursive and returns the 0 with proper
185 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
187 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
194 * return the value of a floating point Quot
196 static tarval *computed_value_Quot(ir_node *n)
198 ir_node *a = get_Quot_left(n);
199 ir_node *b = get_Quot_right(n);
201 tarval *ta = value_of(a);
202 tarval *tb = value_of(b);
204 /* This was missing in original implementation. Why? */
205 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
206 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
207 return tarval_quo(ta, tb);
213 * calculate the value of an integer Div of two nodes
214 * Special case: 0 / b
216 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
218 tarval *ta = value_of(a);
219 tarval *tb = value_of(b);
221 /* Compute c1 / c2 or 0 / a, a != 0 */
222 if (ta != tarval_bad) {
223 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
224 return tarval_div(ta, tb);
225 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
232 * return the value of an integer Div
234 static tarval *computed_value_Div(ir_node *n)
236 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
240 * calculate the value of an integer Mod of two nodes
241 * Special case: a % 1
243 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
245 tarval *ta = value_of(a);
246 tarval *tb = value_of(b);
248 /* Compute c1 % c2 or a % 1 */
249 if (tb != tarval_bad) {
250 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
251 return tarval_mod(ta, tb);
252 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
253 return get_mode_null(get_irn_mode(a));
260 * return the value of an integer Mod
262 static tarval *computed_value_Mod(ir_node *n)
264 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
268 * return the value of an Abs
270 static tarval *computed_value_Abs(ir_node *n)
272 ir_node *a = get_Abs_op(n);
273 tarval *ta = value_of(a);
275 if (ta != tarval_bad)
276 return tarval_abs(ta);
282 * return the value of an And
283 * Special case: a & 0, 0 & b
285 static tarval *computed_value_And(ir_node *n)
287 ir_node *a = get_And_left(n);
288 ir_node *b = get_And_right(n);
290 tarval *ta = value_of(a);
291 tarval *tb = value_of(b);
293 if ((ta != tarval_bad) && (tb != tarval_bad)) {
294 return tarval_and (ta, tb);
298 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
299 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
307 * return the value of an Or
308 * Special case: a | 1...1, 1...1 | b
310 static tarval *computed_value_Or(ir_node *n)
312 ir_node *a = get_Or_left(n);
313 ir_node *b = get_Or_right(n);
315 tarval *ta = value_of(a);
316 tarval *tb = value_of(b);
318 if ((ta != tarval_bad) && (tb != tarval_bad)) {
319 return tarval_or (ta, tb);
322 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
323 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
331 * return the value of an Eor
333 static tarval *computed_value_Eor(ir_node *n)
335 ir_node *a = get_Eor_left(n);
336 ir_node *b = get_Eor_right(n);
341 return get_mode_null(get_irn_mode(n));
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_eor (ta, tb);
353 * return the value of a Not
355 static tarval *computed_value_Not(ir_node *n)
357 ir_node *a = get_Not_op(n);
358 tarval *ta = value_of(a);
360 if (ta != tarval_bad)
361 return tarval_not(ta);
367 * return the value of a Shl
369 static tarval *computed_value_Shl(ir_node *n)
371 ir_node *a = get_Shl_left(n);
372 ir_node *b = get_Shl_right(n);
374 tarval *ta = value_of(a);
375 tarval *tb = value_of(b);
377 if ((ta != tarval_bad) && (tb != tarval_bad)) {
378 return tarval_shl (ta, tb);
384 * return the value of a Shr
386 static tarval *computed_value_Shr(ir_node *n)
388 ir_node *a = get_Shr_left(n);
389 ir_node *b = get_Shr_right(n);
391 tarval *ta = value_of(a);
392 tarval *tb = value_of(b);
394 if ((ta != tarval_bad) && (tb != tarval_bad)) {
395 return tarval_shr (ta, tb);
401 * return the value of a Shrs
403 static tarval *computed_value_Shrs(ir_node *n)
405 ir_node *a = get_Shrs_left(n);
406 ir_node *b = get_Shrs_right(n);
408 tarval *ta = value_of(a);
409 tarval *tb = value_of(b);
411 if ((ta != tarval_bad) && (tb != tarval_bad)) {
412 return tarval_shrs (ta, tb);
418 * return the value of a Rot
420 static tarval *computed_value_Rot(ir_node *n)
422 ir_node *a = get_Rot_left(n);
423 ir_node *b = get_Rot_right(n);
425 tarval *ta = value_of(a);
426 tarval *tb = value_of(b);
428 if ((ta != tarval_bad) && (tb != tarval_bad)) {
429 return tarval_rot (ta, tb);
435 * return the value of a Conv
437 static tarval *computed_value_Conv(ir_node *n)
439 ir_node *a = get_Conv_op(n);
440 tarval *ta = value_of(a);
442 if (ta != tarval_bad)
443 return tarval_convert_to(ta, get_irn_mode(n));
449 * return the value of a Proj(Cmp)
451 * This performs a first step of unreachable code elimination.
452 * Proj can not be computed, but folding a Cmp above the Proj here is
453 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
455 * There are several case where we can evaluate a Cmp node, see later.
457 static tarval *computed_value_Proj_Cmp(ir_node *n)
459 ir_node *a = get_Proj_pred(n);
460 ir_node *aa = get_Cmp_left(a);
461 ir_node *ab = get_Cmp_right(a);
462 long proj_nr = get_Proj_proj(n);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
469 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
472 /* This is a trick with the bits used for encoding the Cmp
473 Proj numbers, the following statement is not the same:
474 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
478 tarval *taa = value_of(aa);
479 tarval *tab = value_of(ab);
480 ir_mode *mode = get_irn_mode(aa);
483 * The predecessors of Cmp are target values. We can evaluate
486 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487 /* strange checks... */
488 pn_Cmp flags = tarval_cmp(taa, tab);
489 if (flags != pn_Cmp_False) {
490 return new_tarval_from_long (proj_nr & flags, mode_b);
493 /* for integer values, we can check against MIN/MAX */
494 else if (mode_is_int(mode)) {
495 /* MIN <=/> x. This results in true/false. */
496 if (taa == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Le)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Gt)
501 return get_tarval_b_false();
503 /* x >=/< MIN. This results in true/false. */
505 if (tab == get_mode_min(mode)) {
506 /* a compare with the MIN value */
507 if (proj_nr == pn_Cmp_Ge)
508 return get_tarval_b_true();
509 else if (proj_nr == pn_Cmp_Lt)
510 return get_tarval_b_false();
512 /* MAX >=/< x. This results in true/false. */
513 else if (taa == get_mode_max(mode)) {
514 if (proj_nr == pn_Cmp_Ge)
515 return get_tarval_b_true();
516 else if (proj_nr == pn_Cmp_Lt)
517 return get_tarval_b_false();
519 /* x <=/> MAX. This results in true/false. */
520 else if (tab == get_mode_max(mode)) {
521 if (proj_nr == pn_Cmp_Le)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Gt)
524 return get_tarval_b_false();
528 * The predecessors are Allocs or (void*)(0) constants. Allocs never
529 * return NULL, they raise an exception. Therefore we can predict
533 ir_node *aaa = skip_Id(skip_Proj(aa));
534 ir_node *aba = skip_Id(skip_Proj(ab));
536 if ( ( (/* aa is ProjP and aaa is Alloc */
537 (get_irn_op(aa) == op_Proj)
538 && (mode_is_reference(get_irn_mode(aa)))
539 && (get_irn_op(aaa) == op_Alloc))
540 && ( (/* ab is NULL */
541 (get_irn_op(ab) == op_Const)
542 && (mode_is_reference(get_irn_mode(ab)))
543 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
544 || (/* ab is other Alloc */
545 (get_irn_op(ab) == op_Proj)
546 && (mode_is_reference(get_irn_mode(ab)))
547 && (get_irn_op(aba) == op_Alloc)
549 || (/* aa is NULL and aba is Alloc */
550 (get_irn_op(aa) == op_Const)
551 && (mode_is_reference(get_irn_mode(aa)))
552 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
553 && (get_irn_op(ab) == op_Proj)
554 && (mode_is_reference(get_irn_mode(ab)))
555 && (get_irn_op(aba) == op_Alloc)))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
564 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
566 static tarval *computed_value_Proj(ir_node *n)
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(a);
601 * calculate the value of a Mux: can be evaluated, if the
602 * sel and the right input are known
604 static tarval *computed_value_Mux(ir_node *n)
606 ir_node *sel = get_Mux_sel(n);
607 tarval *ts = value_of(sel);
609 if (ts == get_tarval_b_true()) {
610 ir_node *v = get_Mux_true(n);
613 else if (ts == get_tarval_b_false()) {
614 ir_node *v = get_Mux_false(n);
621 * Calculate the value of a Psi: can be evaluated, if a condition is true
622 * and all previous conditions are false. If all conditions are false
623 * we evaluate to the default one.
625 static tarval *computed_value_Psi(ir_node *n)
628 return computed_value_Mux(n);
633 * calculate the value of a Confirm: can be evaluated,
634 * if it has the form Confirm(x, '=', Const).
636 static tarval *computed_value_Confirm(ir_node *n)
638 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
639 value_of(get_Confirm_bound(n)) : tarval_bad;
643 * If the parameter n can be computed, return its value, else tarval_bad.
644 * Performs constant folding.
646 * @param n The node this should be evaluated
648 tarval *computed_value(ir_node *n)
650 if (n->op->ops.computed_value)
651 return n->op->ops.computed_value(n);
656 * set the default computed_value evaluator in an ir_op_ops.
658 * @param code the opcode for the default operation
659 * @param ops the operations initialized
664 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
668 ops->computed_value = computed_value_##a; \
706 * Returns a equivalent block for another block.
707 * If the block has only one predecessor, this is
708 * the equivalent one. If the only predecessor of a block is
709 * the block itself, this is a dead block.
711 * If both predecessors of a block are the branches of a binary
712 * Cond, the equivalent block is Cond's block.
714 * If all predecessors of a block are bad or lies in a dead
715 * block, the current block is dead as well.
717 * Note, that blocks are NEVER turned into Bad's, instead
718 * the dead_block flag is set. So, never test for is_Bad(block),
719 * always use is_dead_Block(block).
721 static ir_node *equivalent_node_Block(ir_node *n)
724 int n_preds = get_Block_n_cfgpreds(n);
726 /* The Block constructor does not call optimize, but mature_immBlock
727 calls the optimization. */
728 assert(get_Block_matured(n));
730 /* Straightening: a single entry Block following a single exit Block
731 can be merged, if it is not the Start block. */
732 /* !!! Beware, all Phi-nodes of n must have been optimized away.
733 This should be true, as the block is matured before optimize is called.
734 But what about Phi-cycles with the Phi0/Id that could not be resolved?
735 Remaining Phi nodes are just Ids. */
736 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
737 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
738 if (predblock == oldn) {
739 /* Jmp jumps into the block it is in -- deal self cycle. */
740 n = set_Block_dead(n);
741 DBG_OPT_DEAD_BLOCK(oldn, n);
742 } else if (get_opt_control_flow_straightening()) {
744 DBG_OPT_STG(oldn, n);
747 else if ((n_preds == 1) &&
748 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
749 ir_node *predblock = get_Block_cfgpred_block(n, 0);
750 if (predblock == oldn) {
751 /* Jmp jumps into the block it is in -- deal self cycle. */
752 n = set_Block_dead(n);
753 DBG_OPT_DEAD_BLOCK(oldn, n);
756 else if ((n_preds == 2) &&
757 (get_opt_control_flow_weak_simplification())) {
758 /* Test whether Cond jumps twice to this block
759 * The more general case which more than 2 predecessors is handles
760 * in optimize_cf(), we handle only this special case for speed here.
762 ir_node *a = get_Block_cfgpred(n, 0);
763 ir_node *b = get_Block_cfgpred(n, 1);
765 if ((get_irn_op(a) == op_Proj) &&
766 (get_irn_op(b) == op_Proj) &&
767 (get_Proj_pred(a) == get_Proj_pred(b)) &&
768 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
769 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
770 /* Also a single entry Block following a single exit Block. Phis have
771 twice the same operand and will be optimized away. */
772 n = get_nodes_block(get_Proj_pred(a));
773 DBG_OPT_IFSIM1(oldn, a, b, n);
776 else if (get_opt_unreachable_code() &&
777 (n != get_irg_start_block(current_ir_graph)) &&
778 (n != get_irg_end_block(current_ir_graph)) ) {
781 /* If all inputs are dead, this block is dead too, except if it is
782 the start or end block. This is one step of unreachable code
784 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
785 ir_node *pred = get_Block_cfgpred(n, i);
788 if (is_Bad(pred)) continue;
789 pred_blk = get_nodes_block(skip_Proj(pred));
791 if (is_Block_dead(pred_blk)) continue;
794 /* really found a living input */
799 n = set_Block_dead(n);
800 DBG_OPT_DEAD_BLOCK(oldn, n);
808 * Returns a equivalent node for a Jmp, a Bad :-)
809 * Of course this only happens if the Block of the Jmp is dead.
811 static ir_node *equivalent_node_Jmp(ir_node *n)
813 /* unreachable code elimination */
814 if (is_Block_dead(get_nodes_block(n)))
820 /** Raise is handled in the same way as Jmp. */
821 #define equivalent_node_Raise equivalent_node_Jmp
824 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
825 See transform_node_Proj_Cond(). */
828 * optimize operations that are commutative and have neutral 0,
829 * so a op 0 = 0 op a = a.
831 static ir_node *equivalent_node_neutral_zero(ir_node *n)
835 ir_node *a = get_binop_left(n);
836 ir_node *b = get_binop_right(n);
841 /* After running compute_node there is only one constant predecessor.
842 Find this predecessors value and remember the other node: */
843 if ((tv = value_of(a)) != tarval_bad) {
845 } else if ((tv = value_of(b)) != tarval_bad) {
850 /* If this predecessors constant value is zero, the operation is
851 * unnecessary. Remove it.
853 * Beware: If n is a Add, the mode of on and n might be different
854 * which happens in this rare construction: NULL + 3.
855 * Then, a Conv would be needed which we cannot include here.
857 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
858 if (get_irn_mode(on) == get_irn_mode(n)) {
861 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
869 * Eor is commutative and has neutral 0.
871 #define equivalent_node_Eor equivalent_node_neutral_zero
874 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
876 * The second one looks strange, but this construct
877 * is used heavily in the LCC sources :-).
879 * Beware: The Mode of an Add may be different than the mode of its
880 * predecessors, so we could not return a predecessors in all cases.
882 static ir_node *equivalent_node_Add(ir_node *n)
885 ir_node *left, *right;
887 n = equivalent_node_neutral_zero(n);
891 left = get_Add_left(n);
892 right = get_Add_right(n);
894 if (get_irn_op(left) == op_Sub) {
895 if (get_Sub_right(left) == right) {
898 n = get_Sub_left(left);
899 if (get_irn_mode(oldn) == get_irn_mode(n)) {
900 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
905 if (get_irn_op(right) == op_Sub) {
906 if (get_Sub_right(right) == left) {
909 n = get_Sub_left(right);
910 if (get_irn_mode(oldn) == get_irn_mode(n)) {
911 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
920 * optimize operations that are not commutative but have neutral 0 on left,
923 static ir_node *equivalent_node_left_zero(ir_node *n)
927 ir_node *a = get_binop_left(n);
928 ir_node *b = get_binop_right(n);
930 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
933 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
939 #define equivalent_node_Shl equivalent_node_left_zero
940 #define equivalent_node_Shr equivalent_node_left_zero
941 #define equivalent_node_Shrs equivalent_node_left_zero
942 #define equivalent_node_Rot equivalent_node_left_zero
945 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
947 * The second one looks strange, but this construct
948 * is used heavily in the LCC sources :-).
950 * Beware: The Mode of a Sub may be different than the mode of its
951 * predecessors, so we could not return a predecessors in all cases.
953 static ir_node *equivalent_node_Sub(ir_node *n)
957 ir_node *a = get_Sub_left(n);
958 ir_node *b = get_Sub_right(n);
960 /* Beware: modes might be different */
961 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
962 if (get_irn_mode(n) == get_irn_mode(a)) {
965 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
968 else if (get_irn_op(a) == op_Add) {
969 ir_mode *mode = get_irn_mode(n);
971 if (mode_wrap_around(mode)) {
972 ir_node *left = get_Add_left(a);
973 ir_node *right = get_Add_right(a);
976 if (get_irn_mode(n) == get_irn_mode(right)) {
978 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
981 else if (right == b) {
982 if (get_irn_mode(n) == get_irn_mode(left)) {
984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
995 * Optimize an "idempotent unary op", ie op(op(n)) = n.
998 * -(-a) == a, but might overflow two times.
999 * We handle it anyway here but the better way would be a
1000 * flag. This would be needed for Pascal for instance.
1002 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1005 ir_node *pred = get_unop_op(n);
1007 /* optimize symmetric unop */
1008 if (get_irn_op(pred) == get_irn_op(n)) {
1009 n = get_unop_op(pred);
1010 DBG_OPT_ALGSIM2(oldn, pred, n);
1015 /** Not(Not(x)) == x */
1016 #define equivalent_node_Not equivalent_node_idempotent_unop
1018 /** --x == x ??? Is this possible or can --x raise an
1019 out of bounds exception if min =! max? */
1020 #define equivalent_node_Minus equivalent_node_idempotent_unop
1023 * Optimize a * 1 = 1 * a = a.
1025 static ir_node *equivalent_node_Mul(ir_node *n)
1029 ir_node *a = get_Mul_left(n);
1030 ir_node *b = get_Mul_right(n);
1032 /* Mul is commutative and has again an other neutral element. */
1033 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1035 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1036 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1044 * Optimize a / 1 = a.
1046 static ir_node *equivalent_node_Div(ir_node *n)
1048 ir_node *a = get_Div_left(n);
1049 ir_node *b = get_Div_right(n);
1051 /* Div is not commutative. */
1052 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1053 /* Turn Div into a tuple (mem, bad, a) */
1054 ir_node *mem = get_Div_mem(n);
1055 turn_into_tuple(n, pn_Div_max);
1056 set_Tuple_pred(n, pn_Div_M, mem);
1057 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1058 set_Tuple_pred(n, pn_Div_res, a);
1064 * Optimize a / 1 = a.
1066 static ir_node *equivalent_node_DivMod(ir_node *n)
1068 ir_node *a = get_DivMod_left(n);
1069 ir_node *b = get_DivMod_right(n);
1071 /* Div is not commutative. */
1072 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1073 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1074 ir_node *mem = get_Div_mem(n);
1075 ir_mode *mode = get_irn_mode(b);
1077 turn_into_tuple(n, pn_DivMod_max);
1078 set_Tuple_pred(n, pn_DivMod_M, mem);
1079 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1080 set_Tuple_pred(n, pn_DivMod_res_div, a);
1081 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1087 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1089 static ir_node *equivalent_node_Or(ir_node *n)
1093 ir_node *a = get_Or_left(n);
1094 ir_node *b = get_Or_right(n);
1097 n = a; /* Or has it's own neutral element */
1098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1099 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1101 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1102 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1104 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1111 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1113 static ir_node *equivalent_node_And(ir_node *n)
1117 ir_node *a = get_And_left(n);
1118 ir_node *b = get_And_right(n);
1121 n = a; /* And has it's own neutral element */
1122 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1123 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1125 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1126 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1134 * Try to remove useless Conv's:
1136 static ir_node *equivalent_node_Conv(ir_node *n)
1139 ir_node *a = get_Conv_op(n);
1142 ir_mode *n_mode = get_irn_mode(n);
1143 ir_mode *a_mode = get_irn_mode(a);
1145 if (n_mode == a_mode) { /* No Conv necessary */
1147 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1148 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1152 n_mode = get_irn_mode(n);
1153 b_mode = get_irn_mode(b);
1155 if (n_mode == b_mode) {
1156 if (n_mode == mode_b) {
1157 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1158 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1160 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1161 if (smaller_mode(b_mode, a_mode)){
1162 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1163 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1172 * A Cast may be removed if the type of the previous node
1173 * is already the type of the Cast.
1175 static ir_node *equivalent_node_Cast(ir_node *n) {
1177 ir_node *pred = get_Cast_op(n);
1179 if (get_irn_type(pred) == get_Cast_type(n)) {
1181 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1187 Several optimizations:
1188 - no Phi in start block.
1189 - remove Id operators that are inputs to Phi
1190 - fold Phi-nodes, iff they have only one predecessor except
1193 static ir_node *equivalent_node_Phi(ir_node *n)
1198 ir_node *block = NULL; /* to shutup gcc */
1199 ir_node *first_val = NULL; /* to shutup gcc */
1201 if (!get_opt_normalize()) return n;
1203 n_preds = get_Phi_n_preds(n);
1205 block = get_nodes_block(n);
1206 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1207 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1208 if ((is_Block_dead(block)) || /* Control dead */
1209 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1210 return new_Bad(); /* in the Start Block. */
1212 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1214 /* If the Block has a Bad pred, we also have one. */
1215 for (i = 0; i < n_preds; ++i)
1216 if (is_Bad(get_Block_cfgpred(block, i)))
1217 set_Phi_pred(n, i, new_Bad());
1219 /* Find first non-self-referencing input */
1220 for (i = 0; i < n_preds; ++i) {
1221 first_val = get_Phi_pred(n, i);
1222 if ( (first_val != n) /* not self pointer */
1224 && (! is_Bad(first_val))
1226 ) { /* value not dead */
1227 break; /* then found first value. */
1232 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1236 /* search for rest of inputs, determine if any of these
1237 are non-self-referencing */
1238 while (++i < n_preds) {
1239 ir_node *scnd_val = get_Phi_pred(n, i);
1240 if ( (scnd_val != n)
1241 && (scnd_val != first_val)
1243 && (! is_Bad(scnd_val))
1251 /* Fold, if no multiple distinct non-self-referencing inputs */
1253 DBG_OPT_PHI(oldn, n);
1259 Several optimizations:
1260 - no Sync in start block.
1261 - fold Sync-nodes, iff they have only one predecessor except
1263 @fixme: are there loop's in Sync's
1265 static ir_node *equivalent_node_Sync(ir_node *n)
1270 ir_node *first_val = NULL; /* to shutup gcc */
1272 if (!get_opt_normalize()) return n;
1274 n_preds = get_Sync_n_preds(n);
1276 /* Find first non-self-referencing input */
1277 for (i = 0; i < n_preds; ++i) {
1278 first_val = get_Sync_pred(n, i);
1279 if ((first_val != n) /* not self pointer */ &&
1280 (! is_Bad(first_val))
1281 ) { /* value not dead */
1282 break; /* then found first value. */
1287 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1290 /* search the rest of inputs, determine if any of these
1291 are non-self-referencing */
1292 while (++i < n_preds) {
1293 ir_node *scnd_val = get_Sync_pred(n, i);
1294 if ((scnd_val != n) &&
1295 (scnd_val != first_val) &&
1296 (! is_Bad(scnd_val))
1302 /* Fold, if no multiple distinct non-self-referencing inputs */
1304 DBG_OPT_SYNC(oldn, n);
1310 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1311 * ProjX(Load) and ProjX(Store)
1313 static ir_node *equivalent_node_Proj(ir_node *n)
1317 ir_node *a = get_Proj_pred(n);
1319 if ( get_irn_op(a) == op_Tuple) {
1320 /* Remove the Tuple/Proj combination. */
1321 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1322 n = get_Tuple_pred(a, get_Proj_proj(n));
1323 DBG_OPT_TUPLE(oldn, a, n);
1325 assert(0); /* This should not happen! */
1329 else if (get_irn_mode(n) == mode_X) {
1330 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1331 /* Remove dead control flow -- early gigo(). */
1334 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1335 ir_op *op = get_irn_op(a);
1337 if (op == op_Load || op == op_Store) {
1338 /* get the load/store address */
1339 ir_node *addr = get_irn_n(a, 1);
1340 if (value_not_null(addr)) {
1341 /* this node may float if it did not depend on a Confirm */
1342 set_irn_pinned(a, op_pin_state_floats);
1356 static ir_node *equivalent_node_Id(ir_node *n)
1362 } while (get_irn_op(n) == op_Id);
1364 DBG_OPT_ID(oldn, n);
1371 static ir_node *equivalent_node_Mux(ir_node *n)
1373 ir_node *oldn = n, *sel = get_Mux_sel(n);
1374 tarval *ts = value_of(sel);
1376 /* Mux(true, f, t) == t */
1377 if (ts == tarval_b_true) {
1378 n = get_Mux_true(n);
1379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1381 /* Mux(false, f, t) == f */
1382 else if (ts == tarval_b_false) {
1383 n = get_Mux_false(n);
1384 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1386 /* Mux(v, x, x) == x */
1387 else if (get_Mux_false(n) == get_Mux_true(n)) {
1388 n = get_Mux_true(n);
1389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1391 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1392 ir_node *cmp = get_Proj_pred(sel);
1393 long proj_nr = get_Proj_proj(sel);
1394 ir_node *b = get_Mux_false(n);
1395 ir_node *a = get_Mux_true(n);
1398 * Note: normalization puts the constant on the right site,
1399 * so we check only one case.
1401 * Note further that these optimization work even for floating point
1402 * with NaN's because -NaN == NaN.
1403 * However, if +0 and -0 is handled differently, we cannot use the first one.
1405 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1406 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1407 /* Mux(a CMP 0, X, a) */
1408 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1409 /* Mux(a CMP 0, -a, a) */
1410 if (proj_nr == pn_Cmp_Eq) {
1411 /* Mux(a == 0, -a, a) ==> -a */
1413 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1415 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1416 /* Mux(a != 0, -a, a) ==> a */
1418 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1421 else if (classify_Const(b) == CNST_NULL) {
1422 /* Mux(a CMP 0, 0, a) */
1423 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1424 /* Mux(a != 0, 0, a) ==> a */
1426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1428 else if (proj_nr == pn_Cmp_Eq) {
1429 /* Mux(a == 0, 0, a) ==> 0 */
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1441 * Returns a equivalent node of a Psi: if a condition is true
1442 * and all previous conditions are false we know its value.
1443 * If all conditions are false its value is the default one.
1445 static ir_node *equivalent_node_Psi(ir_node *n) {
1447 return equivalent_node_Mux(n);
1452 * Optimize -a CMP -b into b CMP a.
1453 * This works only for for modes where unary Minus
1455 * Note that two-complement integers can Overflow
1456 * so it will NOT work.
1458 static ir_node *equivalent_node_Cmp(ir_node *n)
1460 ir_node *left = get_Cmp_left(n);
1461 ir_node *right = get_Cmp_right(n);
1463 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1464 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1465 left = get_Minus_op(left);
1466 right = get_Minus_op(right);
1467 set_Cmp_left(n, right);
1468 set_Cmp_right(n, left);
1474 * Remove Confirm nodes if setting is on.
1475 * Replace Confirms(x, '=', Constlike) by Constlike.
1477 static ir_node *equivalent_node_Confirm(ir_node *n)
1479 ir_node *pred = get_Confirm_value(n);
1480 pn_Cmp pnc = get_Confirm_cmp(n);
1482 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1484 * rare case: two identical Confirms one after another,
1485 * replace the second one with the first.
1489 if (pnc == pn_Cmp_Eq) {
1490 ir_node *bound = get_Confirm_bound(n);
1493 * Optimize a rare case:
1494 * Confirm(x, '=', Constlike) ==> Constlike
1496 if (is_irn_constlike(bound)) {
1497 DBG_OPT_CONFIRM(n, bound);
1501 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1505 * Optimize CopyB(mem, x, x) into a Nop
1507 static ir_node *equivalent_node_CopyB(ir_node *n)
1509 ir_node *a = get_CopyB_dst(n);
1510 ir_node *b = get_CopyB_src(n);
1513 /* Turn CopyB into a tuple (mem, bad, bad) */
1514 ir_node *mem = get_CopyB_mem(n);
1515 turn_into_tuple(n, pn_CopyB_max);
1516 set_Tuple_pred(n, pn_CopyB_M, mem);
1517 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1518 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1524 * Optimize Bounds(idx, idx, upper) into idx.
1526 static ir_node *equivalent_node_Bound(ir_node *n)
1528 ir_node *idx = get_Bound_index(n);
1529 ir_node *lower = get_Bound_lower(n);
1532 /* By definition lower < upper, so if idx == lower -->
1533 lower <= idx && idx < upper */
1535 /* Turn Bound into a tuple (mem, bad, idx) */
1539 ir_node *pred = skip_Proj(idx);
1541 if (get_irn_op(pred) == op_Bound) {
1543 * idx was Bounds_check previously, it is still valid if
1544 * lower <= pred_lower && pred_upper <= upper.
1546 ir_node *upper = get_Bound_upper(n);
1547 if (get_Bound_lower(pred) == lower &&
1548 get_Bound_upper(pred) == upper) {
1550 * One could expect that we simply return the previous
1551 * Bound here. However, this would be wrong, as we could
1552 * add an exception Proj to a new location than.
1553 * So, we must turn in into a tuple
1560 /* Turn Bound into a tuple (mem, bad, idx) */
1561 ir_node *mem = get_Bound_mem(n);
1562 turn_into_tuple(n, pn_Bound_max);
1563 set_Tuple_pred(n, pn_Bound_M_regular, mem);
1564 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1565 set_Tuple_pred(n, pn_Bound_res, idx);
1566 set_Tuple_pred(n, pn_Bound_M_except, mem);
1572 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1573 * perform no actual computation, as, e.g., the Id nodes. It does not create
1574 * new nodes. It is therefore safe to free n if the node returned is not n.
1575 * If a node returns a Tuple we can not just skip it. If the size of the
1576 * in array fits, we transform n into a tuple (e.g., Div).
1579 equivalent_node(ir_node *n)
1581 if (n->op->ops.equivalent_node)
1582 return n->op->ops.equivalent_node(n);
1587 * sets the default equivalent node operation for an ir_op_ops.
1589 * @param code the opcode for the default operation
1590 * @param ops the operations initialized
1595 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1599 ops->equivalent_node = equivalent_node_##a; \
1641 * Do node specific optimizations of nodes predecessors.
1644 optimize_preds(ir_node *n) {
1645 ir_node *a = NULL, *b = NULL;
1647 /* get the operands we will work on for simple cases. */
1649 a = get_binop_left(n);
1650 b = get_binop_right(n);
1651 } else if (is_unop(n)) {
1655 switch (get_irn_opcode(n)) {
1658 /* We don't want Cast as input to Cmp. */
1659 if (get_irn_op(a) == op_Cast) {
1663 if (get_irn_op(b) == op_Cast) {
1665 set_Cmp_right(n, b);
1674 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1675 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1676 * If possible, remove the Conv's.
1678 static ir_node *transform_node_AddSub(ir_node *n)
1680 ir_mode *mode = get_irn_mode(n);
1682 if (mode_is_reference(mode)) {
1683 ir_node *left = get_binop_left(n);
1684 ir_node *right = get_binop_right(n);
1685 int ref_bits = get_mode_size_bits(mode);
1687 if (get_irn_op(left) == op_Conv) {
1688 ir_mode *mode = get_irn_mode(left);
1689 int bits = get_mode_size_bits(mode);
1691 if (ref_bits == bits &&
1692 mode_is_int(mode) &&
1693 get_mode_arithmetic(mode) == irma_twos_complement) {
1694 ir_node *pre = get_Conv_op(left);
1695 ir_mode *pre_mode = get_irn_mode(pre);
1697 if (mode_is_int(pre_mode) &&
1698 get_mode_size_bits(pre_mode) == bits &&
1699 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1700 /* ok, this conv just changes to sign, moreover the calculation
1701 * is done with same number of bits as our address mode, so
1702 * we can ignore the conv as address calculation can be viewed
1703 * as either signed or unsigned
1705 set_binop_left(n, pre);
1710 if (get_irn_op(right) == op_Conv) {
1711 ir_mode *mode = get_irn_mode(right);
1712 int bits = get_mode_size_bits(mode);
1714 if (ref_bits == bits &&
1715 mode_is_int(mode) &&
1716 get_mode_arithmetic(mode) == irma_twos_complement) {
1717 ir_node *pre = get_Conv_op(right);
1718 ir_mode *pre_mode = get_irn_mode(pre);
1720 if (mode_is_int(pre_mode) &&
1721 get_mode_size_bits(pre_mode) == bits &&
1722 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1723 /* ok, this conv just changes to sign, moreover the calculation
1724 * is done with same number of bits as our address mode, so
1725 * we can ignore the conv as address calculation can be viewed
1726 * as either signed or unsigned
1728 set_binop_right(n, pre);
1737 * Do the AddSub optimization, then Transform
1738 * Add(a,a) -> Mul(a, 2)
1739 * Add(Mul(a, x), a) -> Mul(a, x+1)
1740 * if the mode is integer or float.
1741 * Transform Add(a,-b) into Sub(a,b).
1742 * Reassociation might fold this further.
1744 static ir_node *transform_node_Add(ir_node *n)
1749 n = transform_node_AddSub(n);
1751 mode = get_irn_mode(n);
1752 if (mode_is_num(mode)) {
1753 ir_node *a = get_Add_left(n);
1754 ir_node *b = get_Add_right(n);
1757 ir_node *block = get_irn_n(n, -1);
1760 get_irn_dbg_info(n),
1764 new_r_Const_long(current_ir_graph, block, mode, 2),
1766 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1768 else if (get_irn_op(a) == op_Minus) {
1770 get_irn_dbg_info(n),
1776 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1778 else if (get_irn_op(b) == op_Minus) {
1780 get_irn_dbg_info(n),
1786 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1788 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1789 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1790 ir_node *ma = get_Mul_left(a);
1791 ir_node *mb = get_Mul_right(a);
1794 ir_node *blk = get_irn_n(n, -1);
1796 get_irn_dbg_info(n), current_ir_graph, blk,
1799 get_irn_dbg_info(n), current_ir_graph, blk,
1801 new_r_Const_long(current_ir_graph, blk, mode, 1),
1804 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1807 ir_node *blk = get_irn_n(n, -1);
1809 get_irn_dbg_info(n), current_ir_graph, blk,
1812 get_irn_dbg_info(n), current_ir_graph, blk,
1814 new_r_Const_long(current_ir_graph, blk, mode, 1),
1817 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1820 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1821 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1822 ir_node *ma = get_Mul_left(b);
1823 ir_node *mb = get_Mul_right(b);
1826 ir_node *blk = get_irn_n(n, -1);
1828 get_irn_dbg_info(n), current_ir_graph, blk,
1831 get_irn_dbg_info(n), current_ir_graph, blk,
1833 new_r_Const_long(current_ir_graph, blk, mode, 1),
1836 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1839 ir_node *blk = get_irn_n(n, -1);
1841 get_irn_dbg_info(n), current_ir_graph, blk,
1844 get_irn_dbg_info(n), current_ir_graph, blk,
1846 new_r_Const_long(current_ir_graph, blk, mode, 1),
1849 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1857 * Do the AddSub optimization, then Transform
1858 * Sub(0,a) -> Minus(a)
1859 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1861 static ir_node *transform_node_Sub(ir_node *n)
1867 n = transform_node_AddSub(n);
1869 mode = get_irn_mode(n);
1870 a = get_Sub_left(n);
1871 b = get_Sub_right(n);
1872 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1874 get_irn_dbg_info(n),
1879 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1881 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1882 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1883 ir_node *ma = get_Mul_left(a);
1884 ir_node *mb = get_Mul_right(a);
1887 ir_node *blk = get_irn_n(n, -1);
1889 get_irn_dbg_info(n),
1890 current_ir_graph, blk,
1893 get_irn_dbg_info(n),
1894 current_ir_graph, blk,
1896 new_r_Const_long(current_ir_graph, blk, mode, 1),
1899 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1902 ir_node *blk = get_irn_n(n, -1);
1904 get_irn_dbg_info(n),
1905 current_ir_graph, blk,
1908 get_irn_dbg_info(n),
1909 current_ir_graph, blk,
1911 new_r_Const_long(current_ir_graph, blk, mode, 1),
1914 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1922 * Transform Mul(a,-1) into -a.
1923 * Do architecture dependent optimizations on Mul nodes
1925 static ir_node *transform_node_Mul(ir_node *n) {
1927 ir_mode *mode = get_irn_mode(n);
1929 if (mode_is_signed(mode)) {
1931 ir_node *a = get_Mul_left(n);
1932 ir_node *b = get_Mul_right(n);
1934 if (value_of(a) == get_mode_minus_one(mode))
1936 else if (value_of(b) == get_mode_minus_one(mode))
1939 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1940 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1944 return arch_dep_replace_mul_with_shifts(n);
1948 * transform a Div Node
1950 static ir_node *transform_node_Div(ir_node *n)
1952 tarval *tv = value_of(n);
1955 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1957 if (tv != tarval_bad) {
1958 value = new_Const(get_tarval_mode(tv), tv);
1960 DBG_OPT_CSTEVAL(n, value);
1962 else /* Try architecture dependent optimization */
1963 value = arch_dep_replace_div_by_const(n);
1966 /* Turn Div into a tuple (mem, bad, value) */
1967 ir_node *mem = get_Div_mem(n);
1969 turn_into_tuple(n, pn_Div_max);
1970 set_Tuple_pred(n, pn_Div_M, mem);
1971 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1972 set_Tuple_pred(n, pn_Div_res, value);
1978 * transform a Mod node
1980 static ir_node *transform_node_Mod(ir_node *n)
1982 tarval *tv = value_of(n);
1985 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1987 if (tv != tarval_bad) {
1988 value = new_Const(get_tarval_mode(tv), tv);
1990 DBG_OPT_CSTEVAL(n, value);
1992 else /* Try architecture dependent optimization */
1993 value = arch_dep_replace_mod_by_const(n);
1996 /* Turn Mod into a tuple (mem, bad, value) */
1997 ir_node *mem = get_Mod_mem(n);
1999 turn_into_tuple(n, pn_Mod_max);
2000 set_Tuple_pred(n, pn_Mod_M, mem);
2001 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2002 set_Tuple_pred(n, pn_Mod_res, value);
2008 * transform a DivMod node
2010 static ir_node *transform_node_DivMod(ir_node *n)
2014 ir_node *a = get_DivMod_left(n);
2015 ir_node *b = get_DivMod_right(n);
2016 ir_mode *mode = get_irn_mode(a);
2017 tarval *ta = value_of(a);
2018 tarval *tb = value_of(b);
2020 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2023 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2025 if (tb != tarval_bad) {
2026 if (tb == get_mode_one(get_tarval_mode(tb))) {
2027 b = new_Const (mode, get_mode_null(mode));
2030 DBG_OPT_CSTEVAL(n, b);
2032 else if (ta != tarval_bad) {
2033 tarval *resa, *resb;
2034 resa = tarval_div (ta, tb);
2035 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2036 Jmp for X result!? */
2037 resb = tarval_mod (ta, tb);
2038 if (resb == tarval_bad) return n; /* Causes exception! */
2039 a = new_Const (mode, resa);
2040 b = new_Const (mode, resb);
2043 DBG_OPT_CSTEVAL(n, a);
2044 DBG_OPT_CSTEVAL(n, b);
2046 else { /* Try architecture dependent optimization */
2047 arch_dep_replace_divmod_by_const(&a, &b, n);
2048 evaluated = a != NULL;
2050 } else if (ta == get_mode_null(mode)) {
2051 /* 0 / non-Const = 0 */
2056 if (evaluated) { /* replace by tuple */
2057 ir_node *mem = get_DivMod_mem(n);
2058 turn_into_tuple(n, pn_DivMod_max);
2059 set_Tuple_pred(n, pn_DivMod_M, mem);
2060 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2061 set_Tuple_pred(n, pn_DivMod_res_div, a);
2062 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2069 * Optimize Abs(x) into x if x is Confirmed >= 0
2070 * Optimize Abs(x) into -x if x is Confirmed <= 0
2072 static ir_node *transform_node_Abs(ir_node *n)
2075 ir_node *a = get_Abs_op(n);
2076 value_classify sign = classify_value_sign(a);
2078 if (sign == VALUE_NEGATIVE) {
2079 ir_mode *mode = get_irn_mode(n);
2082 * We can replace the Abs by -x here.
2083 * We even could add a new Confirm here.
2085 * Note that -x would create a new node, so we could
2086 * not run it in the equivalent_node() context.
2088 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2089 get_irn_n(n, -1), a, mode);
2091 DBG_OPT_CONFIRM(oldn, n);
2093 else if (sign == VALUE_POSITIVE) {
2094 /* n is positive, Abs is not needed */
2097 DBG_OPT_CONFIRM(oldn, n);
2104 * transform a Cond node
2106 static ir_node *transform_node_Cond(ir_node *n)
2108 /* Replace the Cond by a Jmp if it branches on a constant
2111 ir_node *a = get_Cond_selector(n);
2112 tarval *ta = value_of(a);
2114 /* we need block info which is not available in floating irgs */
2115 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2118 if ((ta != tarval_bad) &&
2119 (get_irn_mode(a) == mode_b) &&
2120 (get_opt_unreachable_code())) {
2121 /* It's a boolean Cond, branching on a boolean constant.
2122 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2123 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2124 turn_into_tuple(n, pn_Cond_max);
2125 if (ta == tarval_b_true) {
2126 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2127 set_Tuple_pred(n, pn_Cond_true, jmp);
2129 set_Tuple_pred(n, pn_Cond_false, jmp);
2130 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2132 /* We might generate an endless loop, so keep it alive. */
2133 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2141 static ir_node *transform_node_Eor(ir_node *n)
2144 ir_node *a = get_Eor_left(n);
2145 ir_node *b = get_Eor_right(n);
2146 ir_mode *mode = get_irn_mode(n);
2150 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2151 mode, get_mode_null(mode));
2152 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2154 else if ((mode == mode_b)
2155 && (get_irn_op(a) == op_Proj)
2156 && (get_irn_mode(a) == mode_b)
2157 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2158 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2159 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2160 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2161 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2165 else if ((mode == mode_b)
2166 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2167 /* The Eor is a Not. Replace it by a Not. */
2168 /* ????!!!Extend to bitfield 1111111. */
2169 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2171 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2178 * Transform a boolean Not.
2180 static ir_node *transform_node_Not(ir_node *n)
2183 ir_node *a = get_Not_op(n);
2185 if ( (get_irn_mode(n) == mode_b)
2186 && (get_irn_op(a) == op_Proj)
2187 && (get_irn_mode(a) == mode_b)
2188 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2189 /* We negate a Cmp. The Cmp has the negated result anyways! */
2190 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2191 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2192 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2199 * Transform a Cast_type(Const) into a new Const_type
2201 static ir_node *transform_node_Cast(ir_node *n) {
2203 ir_node *pred = get_Cast_op(n);
2204 ir_type *tp = get_irn_type(n);
2206 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2207 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2208 get_Const_tarval(pred), tp);
2209 DBG_OPT_CSTEVAL(oldn, n);
2210 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2211 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2212 get_SymConst_kind(pred), tp);
2213 DBG_OPT_CSTEVAL(oldn, n);
2220 * Transform a Proj(Div) with a non-zero value.
2221 * Removes the exceptions and routes the memory to the NoMem node.
2223 static ir_node *transform_node_Proj_Div(ir_node *proj)
2225 ir_node *n = get_Proj_pred(proj);
2226 ir_node *b = get_Div_right(n);
2229 if (value_not_zero(b)) {
2230 /* div(x, y) && y != 0 */
2231 proj_nr = get_Proj_proj(proj);
2233 /* this node may float if it did not depend on a Confirm */
2234 set_irn_pinned(n, op_pin_state_floats);
2236 if (proj_nr == pn_Div_X_except) {
2237 /* we found an exception handler, remove it */
2238 DBG_OPT_EXC_REM(proj);
2241 else if (proj_nr == pn_Div_M) {
2242 ir_node *res = get_Div_mem(n);
2243 /* the memory Proj can only be removed if we divide by a
2244 real constant, but the node never produce a new memory */
2245 if (value_of(b) != tarval_bad) {
2246 /* this is a Div by a const, we can remove the memory edge */
2247 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2256 * Transform a Proj(Mod) with a non-zero value.
2257 * Removes the exceptions and routes the memory to the NoMem node.
2259 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2261 ir_node *n = get_Proj_pred(proj);
2262 ir_node *b = get_Mod_right(n);
2265 if (value_not_zero(b)) {
2266 /* mod(x, y) && y != 0 */
2267 proj_nr = get_Proj_proj(proj);
2269 /* this node may float if it did not depend on a Confirm */
2270 set_irn_pinned(n, op_pin_state_floats);
2272 if (proj_nr == pn_Mod_X_except) {
2273 /* we found an exception handler, remove it */
2274 DBG_OPT_EXC_REM(proj);
2276 } else if (proj_nr == pn_Mod_M) {
2277 ir_node *res = get_Mod_mem(n);
2278 /* the memory Proj can only be removed if we divide by a
2279 real constant, but the node never produce a new memory */
2280 if (value_of(b) != tarval_bad) {
2281 /* this is a Mod by a const, we can remove the memory edge */
2282 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2286 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2287 /* a % a = 0 if a != 0 */
2288 ir_mode *mode = get_irn_mode(proj);
2289 ir_node *res = new_Const(mode, get_mode_null(mode));
2291 DBG_OPT_CSTEVAL(n, res);
2299 * Transform a Proj(DivMod) with a non-zero value.
2300 * Removes the exceptions and routes the memory to the NoMem node.
2302 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2304 ir_node *n = get_Proj_pred(proj);
2305 ir_node *b = get_DivMod_right(n);
2308 if (value_not_zero(b)) {
2309 /* DivMod(x, y) && y != 0 */
2310 proj_nr = get_Proj_proj(proj);
2312 /* this node may float if it did not depend on a Confirm */
2313 set_irn_pinned(n, op_pin_state_floats);
2315 if (proj_nr == pn_DivMod_X_except) {
2316 /* we found an exception handler, remove it */
2317 DBG_OPT_EXC_REM(proj);
2320 else if (proj_nr == pn_DivMod_M) {
2321 ir_node *res = get_DivMod_mem(n);
2322 /* the memory Proj can only be removed if we divide by a
2323 real constant, but the node never produce a new memory */
2324 if (value_of(b) != tarval_bad) {
2325 /* this is a DivMod by a const, we can remove the memory edge */
2326 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2330 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2331 /* a % a = 0 if a != 0 */
2332 ir_mode *mode = get_irn_mode(proj);
2333 ir_node *res = new_Const(mode, get_mode_null(mode));
2335 DBG_OPT_CSTEVAL(n, res);
2343 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2345 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2347 if (get_opt_unreachable_code()) {
2348 ir_node *n = get_Proj_pred(proj);
2349 ir_node *b = get_Cond_selector(n);
2351 if (mode_is_int(get_irn_mode(b))) {
2352 tarval *tb = value_of(b);
2354 if (tb != tarval_bad) {
2355 /* we have a constant switch */
2356 long num = get_Proj_proj(proj);
2358 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2359 if (get_tarval_long(tb) == num) {
2360 /* Do NOT create a jump here, or we will have 2 control flow ops
2361 * in a block. This case is optimized away in optimize_cf(). */
2365 /* this case will NEVER be taken, kill it */
2376 * Normalizes and optimizes Cmp nodes.
2378 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2380 if (get_opt_reassociation()) {
2381 ir_node *n = get_Proj_pred(proj);
2382 ir_node *left = get_Cmp_left(n);
2383 ir_node *right = get_Cmp_right(n);
2387 ir_mode *mode = NULL;
2388 long proj_nr = get_Proj_proj(proj);
2391 * First step: normalize the compare op
2392 * by placing the constant on the right site
2393 * or moving the lower address node to the left.
2394 * We ignore the case that both are constants
2395 * this case should be optimized away.
2397 if (get_irn_op(right) == op_Const)
2399 else if (get_irn_op(left) == op_Const) {
2404 proj_nr = get_inversed_pnc(proj_nr);
2407 else if (get_irn_idx(left) > get_irn_idx(right)) {
2413 proj_nr = get_inversed_pnc(proj_nr);
2418 * Second step: Try to reduce the magnitude
2419 * of a constant. This may help to generate better code
2420 * later and may help to normalize more compares.
2421 * Of course this is only possible for integer values.
2424 mode = get_irn_mode(c);
2425 tv = get_Const_tarval(c);
2427 if (tv != tarval_bad) {
2428 /* the following optimization is possible on modes without Overflow
2429 * on Unary Minus or on == and !=:
2430 * -a CMP c ==> a swap(CMP) -c
2432 * Beware: for two-complement Overflow may occur, so only == and != can
2433 * be optimized, see this:
2434 * -MININT < 0 =/=> MININT > 0 !!!
2436 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2437 (!mode_overflow_on_unary_Minus(mode) ||
2438 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2439 left = get_Minus_op(left);
2440 tv = tarval_sub(get_mode_null(mode), tv);
2442 proj_nr = get_inversed_pnc(proj_nr);
2446 /* for integer modes, we have more */
2447 if (mode_is_int(mode)) {
2448 /* Ne includes Unordered which is not possible on integers.
2449 * However, frontends often use this wrong, so fix it here */
2450 if (proj_nr & pn_Cmp_Uo) {
2451 proj_nr &= ~pn_Cmp_Uo;
2452 set_Proj_proj(proj, proj_nr);
2455 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2456 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2457 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2458 tv = tarval_sub(tv, get_mode_one(mode));
2460 proj_nr ^= pn_Cmp_Eq;
2463 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2464 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2465 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2466 tv = tarval_add(tv, get_mode_one(mode));
2468 proj_nr ^= pn_Cmp_Eq;
2472 /* the following reassociations work only for == and != */
2473 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2475 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2476 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2477 right = get_Sub_right(left);
2478 left = get_Sub_left(left);
2480 tv = value_of(right);
2484 if (tv != tarval_bad) {
2485 ir_op *op = get_irn_op(left);
2487 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2489 ir_node *c1 = get_Sub_right(left);
2490 tarval *tv2 = value_of(c1);
2492 if (tv2 != tarval_bad) {
2493 tv2 = tarval_add(tv, value_of(c1));
2495 if (tv2 != tarval_bad) {
2496 left = get_Sub_left(left);
2502 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2503 else if (op == op_Add) {
2504 ir_node *a_l = get_Add_left(left);
2505 ir_node *a_r = get_Add_right(left);
2509 if (get_irn_op(a_l) == op_Const) {
2511 tv2 = value_of(a_l);
2515 tv2 = value_of(a_r);
2518 if (tv2 != tarval_bad) {
2519 tv2 = tarval_sub(tv, tv2);
2521 if (tv2 != tarval_bad) {
2528 /* -a == c ==> a == -c, -a != c ==> a != -c */
2529 else if (op == op_Minus) {
2530 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2532 if (tv2 != tarval_bad) {
2533 left = get_Minus_op(left);
2540 /* the following reassociations work only for <= */
2541 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2542 if (tv != tarval_bad) {
2543 ir_op *op = get_irn_op(left);
2545 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2553 * optimization for AND:
2555 * And(x, C) == C ==> And(x, C) != 0
2556 * And(x, C) != C ==> And(X, C) == 0
2558 * if C is a single Bit constant.
2560 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2561 (get_irn_op(left) == op_And)) {
2562 if (is_single_bit_tarval(tv)) {
2563 /* check for Constant's match. We have check hare the tarvals,
2564 because our const might be changed */
2565 ir_node *la = get_And_left(left);
2566 ir_node *ra = get_And_right(left);
2567 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2568 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2569 /* fine: do the transformation */
2570 tv = get_mode_null(get_tarval_mode(tv));
2571 proj_nr ^= pn_Cmp_Leg;
2576 } /* tarval != bad */
2580 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2582 if (changed & 2) /* need a new Const */
2583 right = new_Const(mode, tv);
2585 /* create a new compare */
2586 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2589 set_Proj_pred(proj, n);
2590 set_Proj_proj(proj, proj_nr);
2597 * Does all optimizations on nodes that must be done on it's Proj's
2598 * because of creating new nodes.
2600 static ir_node *transform_node_Proj(ir_node *proj)
2602 ir_node *n = get_Proj_pred(proj);
2604 switch (get_irn_opcode(n)) {
2606 return transform_node_Proj_Div(proj);
2609 return transform_node_Proj_Mod(proj);
2612 return transform_node_Proj_DivMod(proj);
2615 return transform_node_Proj_Cond(proj);
2618 return transform_node_Proj_Cmp(proj);
2621 /* should not happen, but if it does will be optimized away */
2622 return equivalent_node_Proj(proj);
2631 * returns the operands of a commutative bin-op, if one operand is
2632 * a const, it is returned as the second one.
2634 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2636 ir_node *op_a = get_binop_left(binop);
2637 ir_node *op_b = get_binop_right(binop);
2639 assert(is_op_commutative(get_irn_op(binop)));
2641 if (get_irn_op(op_a) == op_Const) {
2652 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2653 * Such pattern may arise in bitfield stores.
2655 * value c4 value c4 & c2
2656 * AND c3 AND c1 | c3
2661 static ir_node *transform_node_Or_bf_store(ir_node *or)
2665 ir_node *and_l, *c3;
2666 ir_node *value, *c4;
2667 ir_node *new_and, *new_const, *block;
2668 ir_mode *mode = get_irn_mode(or);
2670 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2672 get_comm_Binop_Ops(or, &and, &c1);
2673 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2676 get_comm_Binop_Ops(and, &or_l, &c2);
2677 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2680 get_comm_Binop_Ops(or_l, &and_l, &c3);
2681 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2684 get_comm_Binop_Ops(and_l, &value, &c4);
2685 if (get_irn_op(c4) != op_Const)
2688 /* ok, found the pattern, check for conditions */
2689 assert(mode == get_irn_mode(and));
2690 assert(mode == get_irn_mode(or_l));
2691 assert(mode == get_irn_mode(and_l));
2693 tv1 = get_Const_tarval(c1);
2694 tv2 = get_Const_tarval(c2);
2695 tv3 = get_Const_tarval(c3);
2696 tv4 = get_Const_tarval(c4);
2698 tv = tarval_or(tv4, tv2);
2699 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2700 /* have at least one 0 at the same bit position */
2704 n_tv4 = tarval_not(tv4);
2705 if (tv3 != tarval_and(tv3, n_tv4)) {
2706 /* bit in the or_mask is outside the and_mask */
2710 n_tv2 = tarval_not(tv2);
2711 if (tv1 != tarval_and(tv1, n_tv2)) {
2712 /* bit in the or_mask is outside the and_mask */
2716 /* ok, all conditions met */
2717 block = get_irn_n(or, -1);
2719 new_and = new_r_And(current_ir_graph, block,
2720 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2722 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2724 set_Or_left(or, new_and);
2725 set_Or_right(or, new_const);
2727 /* check for more */
2728 return transform_node_Or_bf_store(or);
2732 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2734 static ir_node *transform_node_Or_Rot(ir_node *or)
2736 ir_mode *mode = get_irn_mode(or);
2737 ir_node *shl, *shr, *block;
2738 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2741 if (! mode_is_int(mode))
2744 shl = get_binop_left(or);
2745 shr = get_binop_right(or);
2747 if (get_irn_op(shl) == op_Shr) {
2748 if (get_irn_op(shr) != op_Shl)
2755 else if (get_irn_op(shl) != op_Shl)
2757 else if (get_irn_op(shr) != op_Shr)
2760 x = get_Shl_left(shl);
2761 if (x != get_Shr_left(shr))
2764 c1 = get_Shl_right(shl);
2765 c2 = get_Shr_right(shr);
2766 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2767 tv1 = get_Const_tarval(c1);
2768 if (! tarval_is_long(tv1))
2771 tv2 = get_Const_tarval(c2);
2772 if (! tarval_is_long(tv2))
2775 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2776 != get_mode_size_bits(mode))
2779 /* yet, condition met */
2780 block = get_irn_n(or, -1);
2782 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2784 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2787 else if (get_irn_op(c1) == op_Sub) {
2791 if (get_Sub_right(sub) != v)
2794 c1 = get_Sub_left(sub);
2795 if (get_irn_op(c1) != op_Const)
2798 tv1 = get_Const_tarval(c1);
2799 if (! tarval_is_long(tv1))
2802 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2805 /* yet, condition met */
2806 block = get_nodes_block(or);
2808 /* a Rot right is not supported, so use a rot left */
2809 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2811 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2814 else if (get_irn_op(c2) == op_Sub) {
2818 c1 = get_Sub_left(sub);
2819 if (get_irn_op(c1) != op_Const)
2822 tv1 = get_Const_tarval(c1);
2823 if (! tarval_is_long(tv1))
2826 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2829 /* yet, condition met */
2830 block = get_irn_n(or, -1);
2833 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2835 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2845 static ir_node *transform_node_Or(ir_node *or)
2847 or = transform_node_Or_bf_store(or);
2848 or = transform_node_Or_Rot(or);
2854 static ir_node *transform_node(ir_node *n);
2857 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2859 * Should be moved to reassociation?
2861 static ir_node *transform_node_shift(ir_node *n)
2863 ir_node *left, *right;
2864 tarval *tv1, *tv2, *res;
2866 int modulo_shf, flag;
2868 left = get_binop_left(n);
2870 /* different operations */
2871 if (get_irn_op(left) != get_irn_op(n))
2874 right = get_binop_right(n);
2875 tv1 = value_of(right);
2876 if (tv1 == tarval_bad)
2879 tv2 = value_of(get_binop_right(left));
2880 if (tv2 == tarval_bad)
2883 res = tarval_add(tv1, tv2);
2885 /* beware: a simple replacement works only, if res < modulo shift */
2886 mode = get_irn_mode(n);
2890 modulo_shf = get_mode_modulo_shift(mode);
2891 if (modulo_shf > 0) {
2892 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2894 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2901 /* ok, we can replace it */
2902 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2904 in[0] = get_binop_left(left);
2905 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2907 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2909 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2911 return transform_node(irn);
2916 #define transform_node_Shr transform_node_shift
2917 #define transform_node_Shrs transform_node_shift
2918 #define transform_node_Shl transform_node_shift
2921 * Remove dead blocks and nodes in dead blocks
2922 * in keep alive list. We do not generate a new End node.
2924 static ir_node *transform_node_End(ir_node *n) {
2925 int i, n_keepalives = get_End_n_keepalives(n);
2927 for (i = 0; i < n_keepalives; ++i) {
2928 ir_node *ka = get_End_keepalive(n, i);
2930 if (is_Block_dead(ka)) {
2931 set_End_keepalive(n, i, new_Bad());
2934 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2935 set_End_keepalive(n, i, new_Bad());
2941 * Optimize a Mux into some simpler cases.
2943 static ir_node *transform_node_Mux(ir_node *n)
2945 ir_node *oldn = n, *sel = get_Mux_sel(n);
2946 ir_mode *mode = get_irn_mode(n);
2948 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2949 ir_node *cmp = get_Proj_pred(sel);
2950 long proj_nr = get_Proj_proj(sel);
2951 ir_node *f = get_Mux_false(n);
2952 ir_node *t = get_Mux_true(n);
2954 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2955 ir_node *block = get_irn_n(n, -1);
2958 * Note: normalization puts the constant on the right site,
2959 * so we check only one case.
2961 * Note further that these optimization work even for floating point
2962 * with NaN's because -NaN == NaN.
2963 * However, if +0 and -0 is handled differently, we cannot use the first one.
2965 if (get_irn_op(f) == op_Minus &&
2966 get_Minus_op(f) == t &&
2967 get_Cmp_left(cmp) == t) {
2969 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2970 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2971 n = new_rd_Abs(get_irn_dbg_info(n),
2975 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2978 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2979 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2980 n = new_rd_Abs(get_irn_dbg_info(n),
2984 n = new_rd_Minus(get_irn_dbg_info(n),
2989 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2993 else if (get_irn_op(t) == op_Minus &&
2994 get_Minus_op(t) == f &&
2995 get_Cmp_left(cmp) == f) {
2997 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2998 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2999 n = new_rd_Abs(get_irn_dbg_info(n),
3003 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3006 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3007 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3008 n = new_rd_Abs(get_irn_dbg_info(n),
3012 n = new_rd_Minus(get_irn_dbg_info(n),
3017 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3022 if (mode_is_int(mode) && mode_is_signed(mode) &&
3023 get_mode_arithmetic(mode) == irma_twos_complement) {
3024 ir_node *x = get_Cmp_left(cmp);
3026 /* the following optimization works only with signed integer two-complement mode */
3028 if (mode == get_irn_mode(x)) {
3030 * FIXME: this restriction is two rigid, as it would still
3031 * work if mode(x) = Hs and mode == Is, but at least it removes
3034 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3035 classify_Const(t) == CNST_ALL_ONE &&
3036 classify_Const(f) == CNST_NULL) {
3038 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3042 n = new_rd_Shrs(get_irn_dbg_info(n),
3043 current_ir_graph, block, x,
3044 new_r_Const_long(current_ir_graph, block, mode_Iu,
3045 get_mode_size_bits(mode) - 1),
3047 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3050 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3051 classify_Const(t) == CNST_ONE &&
3052 classify_Const(f) == CNST_NULL) {
3054 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3058 n = new_rd_Shr(get_irn_dbg_info(n),
3059 current_ir_graph, block,
3060 new_r_Minus(current_ir_graph, block, x, mode),
3061 new_r_Const_long(current_ir_graph, block, mode_Iu,
3062 get_mode_size_bits(mode) - 1),
3064 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3071 return arch_transform_node_Mux(n);
3075 * Optimize a Psi into some simpler cases.
3077 static ir_node *transform_node_Psi(ir_node *n) {
3079 return transform_node_Mux(n);
3085 * Tries several [inplace] [optimizing] transformations and returns an
3086 * equivalent node. The difference to equivalent_node() is that these
3087 * transformations _do_ generate new nodes, and thus the old node must
3088 * not be freed even if the equivalent node isn't the old one.
3090 static ir_node *transform_node(ir_node *n)
3092 if (n->op->ops.transform_node)
3093 n = n->op->ops.transform_node(n);
3098 * sSets the default transform node operation for an ir_op_ops.
3100 * @param code the opcode for the default operation
3101 * @param ops the operations initialized
3106 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3110 ops->transform_node = transform_node_##a; \
3143 /* **************** Common Subexpression Elimination **************** */
3145 /** The size of the hash table used, should estimate the number of nodes
3147 #define N_IR_NODES 512
3149 /** Compares the attributes of two Const nodes. */
3150 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3152 return (get_Const_tarval(a) != get_Const_tarval(b))
3153 || (get_Const_type(a) != get_Const_type(b));
3156 /** Compares the attributes of two Proj nodes. */
3157 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3159 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3162 /** Compares the attributes of two Filter nodes. */
3163 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3165 return get_Filter_proj(a) != get_Filter_proj(b);
3168 /** Compares the attributes of two Alloc nodes. */
3169 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3171 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3172 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3175 /** Compares the attributes of two Free nodes. */
3176 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3178 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3179 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3182 /** Compares the attributes of two SymConst nodes. */
3183 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3185 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3186 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3187 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3190 /** Compares the attributes of two Call nodes. */
3191 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3193 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3196 /** Compares the attributes of two Sel nodes. */
3197 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3199 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3200 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3201 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3202 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3203 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3206 /** Compares the attributes of two Phi nodes. */
3207 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3209 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3212 /** Compares the attributes of two Cast nodes. */
3213 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3215 return get_Cast_type(a) != get_Cast_type(b);
3218 /** Compares the attributes of two Load nodes. */
3219 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3221 if (get_Load_volatility(a) == volatility_is_volatile ||
3222 get_Load_volatility(b) == volatility_is_volatile)
3223 /* NEVER do CSE on volatile Loads */
3226 return get_Load_mode(a) != get_Load_mode(b);
3229 /** Compares the attributes of two Store nodes. */
3230 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3232 /* NEVER do CSE on volatile Stores */
3233 return (get_Store_volatility(a) == volatility_is_volatile ||
3234 get_Store_volatility(b) == volatility_is_volatile);
3237 /** Compares the attributes of two Confirm nodes. */
3238 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3240 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3244 * Set the default node attribute compare operation for an ir_op_ops.
3246 * @param code the opcode for the default operation
3247 * @param ops the operations initialized
3252 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3256 ops->node_cmp_attr = node_cmp_attr_##a; \
3282 * Compare function for two nodes in the hash table. Gets two
3283 * nodes as parameters. Returns 0 if the nodes are a cse.
3285 int identities_cmp(const void *elt, const void *key)
3293 if (a == b) return 0;
3295 if ((get_irn_op(a) != get_irn_op(b)) ||
3296 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3298 /* compare if a's in and b's in are of equal length */
3299 irn_arity_a = get_irn_intra_arity (a);
3300 if (irn_arity_a != get_irn_intra_arity(b))
3303 /* for block-local cse and op_pin_state_pinned nodes: */
3304 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3305 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3309 /* compare a->in[0..ins] with b->in[0..ins] */
3310 for (i = 0; i < irn_arity_a; i++)
3311 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3315 * here, we already now that the nodes are identical except their
3318 if (a->op->ops.node_cmp_attr)
3319 return a->op->ops.node_cmp_attr(a, b);
3325 * Calculate a hash value of a node.
3328 ir_node_hash (ir_node *node)
3333 if (node->op == op_Const) {
3334 /* special value for const, as they only differ in their tarval. */
3335 h = HASH_PTR(node->attr.con.tv);
3336 h = 9*h + HASH_PTR(get_irn_mode(node));
3337 } else if (node->op == op_SymConst) {
3338 /* special value for const, as they only differ in their symbol. */
3339 h = HASH_PTR(node->attr.i.sym.type_p);
3340 h = 9*h + HASH_PTR(get_irn_mode(node));
3343 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3344 h = irn_arity = get_irn_intra_arity(node);
3346 /* consider all in nodes... except the block if not a control flow. */
3347 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3348 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3352 h = 9*h + HASH_PTR(get_irn_mode(node));
3354 h = 9*h + HASH_PTR(get_irn_op(node));
3361 new_identities(void) {
3362 return new_pset(identities_cmp, N_IR_NODES);
3366 del_identities(pset *value_table) {
3367 del_pset(value_table);
3371 * Return the canonical node computing the same value as n.
3372 * Looks up the node in a hash table.
3374 * For Const nodes this is performed in the constructor, too. Const
3375 * nodes are extremely time critical because of their frequent use in
3376 * constant string arrays.
3378 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3382 if (!value_table) return n;
3384 if (get_opt_reassociation()) {
3385 if (is_op_commutative(get_irn_op(n))) {
3386 ir_node *l = get_binop_left(n);
3387 ir_node *r = get_binop_right(n);
3389 /* for commutative operators perform a OP b == b OP a */
3390 if (get_irn_idx(l) > get_irn_idx(r)) {
3391 set_binop_left(n, r);
3392 set_binop_right(n, l);
3397 o = pset_find(value_table, n, ir_node_hash (n));
3406 * During construction we set the op_pin_state_pinned flag in the graph right when the
3407 * optimization is performed. The flag turning on procedure global cse could
3408 * be changed between two allocations. This way we are safe.
3410 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3413 n = identify(value_table, n);
3414 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3415 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3420 * Return the canonical node computing the same value as n.
3421 * Looks up the node in a hash table, enters it in the table
3422 * if it isn't there yet.
3424 ir_node *identify_remember(pset *value_table, ir_node *n)
3428 if (!value_table) return n;
3430 if (get_opt_reassociation()) {
3431 if (is_op_commutative(get_irn_op(n))) {
3432 ir_node *l = get_binop_left(n);
3433 ir_node *r = get_binop_right(n);
3435 /* for commutative operators perform a OP b == b OP a */
3437 set_binop_left(n, r);
3438 set_binop_right(n, l);
3443 /* lookup or insert in hash table with given hash key. */
3444 o = pset_insert (value_table, n, ir_node_hash (n));
3453 /* Add a node to the identities value table. */
3454 void add_identities(pset *value_table, ir_node *node) {
3455 if (get_opt_cse() && is_no_Block(node))
3456 identify_remember(value_table, node);
3459 /* Visit each node in the value table of a graph. */
3460 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3462 ir_graph *rem = current_ir_graph;
3464 current_ir_graph = irg;
3465 foreach_pset(irg->value_table, node)
3467 current_ir_graph = rem;
3471 * garbage in, garbage out. If a node has a dead input, i.e., the
3472 * Bad node is input to the node, return the Bad node.
3474 static INLINE ir_node *gigo(ir_node *node)
3477 ir_op *op = get_irn_op(node);
3479 /* remove garbage blocks by looking at control flow that leaves the block
3480 and replacing the control flow by Bad. */
3481 if (get_irn_mode(node) == mode_X) {
3482 ir_node *block = get_nodes_block(skip_Proj(node));
3484 /* Don't optimize nodes in immature blocks. */
3485 if (!get_Block_matured(block)) return node;
3486 /* Don't optimize End, may have Bads. */
3487 if (op == op_End) return node;
3489 if (is_Block(block)) {
3490 irn_arity = get_irn_arity(block);
3491 for (i = 0; i < irn_arity; i++) {
3492 if (!is_Bad(get_irn_n(block, i)))
3495 if (i == irn_arity) return new_Bad();
3499 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3500 blocks predecessors is dead. */
3501 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3502 irn_arity = get_irn_arity(node);
3505 * Beware: we can only read the block of a non-floating node.
3507 if (is_irn_pinned_in_irg(node) &&
3508 is_Block_dead(get_nodes_block(node)))
3511 for (i = 0; i < irn_arity; i++) {
3512 ir_node *pred = get_irn_n(node, i);
3517 /* Propagating Unknowns here seems to be a bad idea, because
3518 sometimes we need a node as a input and did not want that
3520 However, i might be useful to move this into a later phase
3521 (it you thing optimizing such code is useful). */
3522 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3523 return new_Unknown(get_irn_mode(node));
3528 /* With this code we violate the agreement that local_optimize
3529 only leaves Bads in Block, Phi and Tuple nodes. */
3530 /* If Block has only Bads as predecessors it's garbage. */
3531 /* If Phi has only Bads as predecessors it's garbage. */
3532 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3533 irn_arity = get_irn_arity(node);
3534 for (i = 0; i < irn_arity; i++) {
3535 if (!is_Bad(get_irn_n(node, i))) break;
3537 if (i == irn_arity) node = new_Bad();
3544 * These optimizations deallocate nodes from the obstack.
3545 * It can only be called if it is guaranteed that no other nodes
3546 * reference this one, i.e., right after construction of a node.
3548 * current_ir_graph must be set to the graph of the node!
3550 ir_node *optimize_node(ir_node *n)
3554 opcode iro = get_irn_opcode(n);
3556 /* Always optimize Phi nodes: part of the construction. */
3557 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3559 /* constant expression evaluation / constant folding */
3560 if (get_opt_constant_folding()) {
3561 /* neither constants nor Tuple values can be evaluated */
3562 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3563 /* try to evaluate */
3564 tv = computed_value(n);
3565 if (tv != tarval_bad) {
3567 ir_type *old_tp = get_irn_type(n);
3568 int i, arity = get_irn_arity(n);
3572 * Try to recover the type of the new expression.
3574 for (i = 0; i < arity && !old_tp; ++i)
3575 old_tp = get_irn_type(get_irn_n(n, i));
3578 * we MUST copy the node here temporary, because it's still needed
3579 * for DBG_OPT_CSTEVAL
3581 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3582 oldn = alloca(node_size);
3584 memcpy(oldn, n, node_size);
3585 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3587 /* ARG, copy the in array, we need it for statistics */
3588 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3590 /* note the inplace edges module */
3591 edges_node_deleted(n, current_ir_graph);
3593 /* evaluation was successful -- replace the node. */
3594 irg_kill_node(current_ir_graph, n);
3595 nw = new_Const(get_tarval_mode (tv), tv);
3597 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3598 set_Const_type(nw, old_tp);
3599 DBG_OPT_CSTEVAL(oldn, nw);
3605 /* remove unnecessary nodes */
3606 if (get_opt_constant_folding() ||
3607 (iro == iro_Phi) || /* always optimize these nodes. */
3609 (iro == iro_Proj) ||
3610 (iro == iro_Block) ) /* Flags tested local. */
3611 n = equivalent_node (n);
3613 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3615 /* Common Subexpression Elimination.
3617 * Checks whether n is already available.
3618 * The block input is used to distinguish different subexpressions. Right
3619 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3620 * subexpressions within a block.
3623 n = identify_cons (current_ir_graph->value_table, n);
3626 edges_node_deleted(oldn, current_ir_graph);
3628 /* We found an existing, better node, so we can deallocate the old node. */
3629 irg_kill_node(current_ir_graph, oldn);
3633 /* Some more constant expression evaluation that does not allow to
3635 iro = get_irn_opcode(n);
3636 if (get_opt_constant_folding() ||
3637 (iro == iro_Cond) ||
3638 (iro == iro_Proj) ||
3639 (iro == iro_Sel)) /* Flags tested local. */
3640 n = transform_node (n);
3642 /* Remove nodes with dead (Bad) input.
3643 Run always for transformation induced Bads. */
3646 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3647 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3648 n = identify_remember (current_ir_graph->value_table, n);
3656 * These optimizations never deallocate nodes (in place). This can cause dead
3657 * nodes lying on the obstack. Remove these by a dead node elimination,
3658 * i.e., a copying garbage collection.
3660 ir_node *optimize_in_place_2(ir_node *n)
3664 opcode iro = get_irn_opcode(n);
3666 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3668 /* constant expression evaluation / constant folding */
3669 if (get_opt_constant_folding()) {
3670 /* neither constants nor Tuple values can be evaluated */
3671 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3672 /* try to evaluate */
3673 tv = computed_value(n);
3674 if (tv != tarval_bad) {
3675 /* evaluation was successful -- replace the node. */
3676 ir_type *old_tp = get_irn_type(n);
3677 int i, arity = get_irn_arity(n);
3680 * Try to recover the type of the new expression.
3682 for (i = 0; i < arity && !old_tp; ++i)
3683 old_tp = get_irn_type(get_irn_n(n, i));
3685 n = new_Const(get_tarval_mode(tv), tv);
3687 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3688 set_Const_type(n, old_tp);
3690 DBG_OPT_CSTEVAL(oldn, n);
3696 /* remove unnecessary nodes */
3697 if (get_opt_constant_folding() ||
3698 (iro == iro_Phi) || /* always optimize these nodes. */
3699 (iro == iro_Id) || /* ... */
3700 (iro == iro_Proj) || /* ... */
3701 (iro == iro_Block) ) /* Flags tested local. */
3702 n = equivalent_node(n);
3704 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3706 /** common subexpression elimination **/
3707 /* Checks whether n is already available. */
3708 /* The block input is used to distinguish different subexpressions. Right
3709 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3710 subexpressions within a block. */
3711 if (get_opt_cse()) {
3712 n = identify(current_ir_graph->value_table, n);
3715 /* Some more constant expression evaluation. */
3716 iro = get_irn_opcode(n);
3717 if (get_opt_constant_folding() ||
3718 (iro == iro_Cond) ||
3719 (iro == iro_Proj) ||
3720 (iro == iro_Sel)) /* Flags tested local. */
3721 n = transform_node(n);
3723 /* Remove nodes with dead (Bad) input.
3724 Run always for transformation induced Bads. */
3727 /* Now we can verify the node, as it has no dead inputs any more. */
3730 /* Now we have a legal, useful node. Enter it in hash table for cse.
3731 Blocks should be unique anyways. (Except the successor of start:
3732 is cse with the start block!) */
3733 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3734 n = identify_remember(current_ir_graph->value_table, n);
3740 * Wrapper for external use, set proper status bits after optimization.
3742 ir_node *optimize_in_place(ir_node *n)
3744 /* Handle graph state */
3745 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3747 if (get_opt_global_cse())
3748 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3749 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3750 set_irg_outs_inconsistent(current_ir_graph);
3752 /* FIXME: Maybe we could also test whether optimizing the node can
3753 change the control graph. */
3754 set_irg_doms_inconsistent(current_ir_graph);
3755 return optimize_in_place_2 (n);
3759 * Sets the default operation for an ir_ops.
3761 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3763 ops = firm_set_default_computed_value(code, ops);
3764 ops = firm_set_default_equivalent_node(code, ops);
3765 ops = firm_set_default_transform_node(code, ops);
3766 ops = firm_set_default_node_cmp_attr(code, ops);
3767 ops = firm_set_default_get_type(code, ops);
3768 ops = firm_set_default_get_type_attr(code, ops);
3769 ops = firm_set_default_get_entity_attr(code, ops);