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 * Returns non-zero if all Phi predecessors are constants
1676 static int is_const_Phi(ir_node *phi) {
1679 for (i = get_irn_arity(phi) - 1; i >= 0; --i)
1680 if (! is_Const(get_irn_n(phi, i)))
1686 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1687 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1688 * If possible, remove the Conv's.
1690 static ir_node *transform_node_AddSub(ir_node *n)
1692 ir_mode *mode = get_irn_mode(n);
1694 if (mode_is_reference(mode)) {
1695 ir_node *left = get_binop_left(n);
1696 ir_node *right = get_binop_right(n);
1697 int ref_bits = get_mode_size_bits(mode);
1699 if (get_irn_op(left) == op_Conv) {
1700 ir_mode *mode = get_irn_mode(left);
1701 int bits = get_mode_size_bits(mode);
1703 if (ref_bits == bits &&
1704 mode_is_int(mode) &&
1705 get_mode_arithmetic(mode) == irma_twos_complement) {
1706 ir_node *pre = get_Conv_op(left);
1707 ir_mode *pre_mode = get_irn_mode(pre);
1709 if (mode_is_int(pre_mode) &&
1710 get_mode_size_bits(pre_mode) == bits &&
1711 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1712 /* ok, this conv just changes to sign, moreover the calculation
1713 * is done with same number of bits as our address mode, so
1714 * we can ignore the conv as address calculation can be viewed
1715 * as either signed or unsigned
1717 set_binop_left(n, pre);
1722 if (get_irn_op(right) == op_Conv) {
1723 ir_mode *mode = get_irn_mode(right);
1724 int bits = get_mode_size_bits(mode);
1726 if (ref_bits == bits &&
1727 mode_is_int(mode) &&
1728 get_mode_arithmetic(mode) == irma_twos_complement) {
1729 ir_node *pre = get_Conv_op(right);
1730 ir_mode *pre_mode = get_irn_mode(pre);
1732 if (mode_is_int(pre_mode) &&
1733 get_mode_size_bits(pre_mode) == bits &&
1734 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1735 /* ok, this conv just changes to sign, moreover the calculation
1736 * is done with same number of bits as our address mode, so
1737 * we can ignore the conv as address calculation can be viewed
1738 * as either signed or unsigned
1740 set_binop_right(n, pre);
1749 * Do the AddSub optimization, then Transform
1750 * Add(a,a) -> Mul(a, 2)
1751 * Add(Mul(a, x), a) -> Mul(a, x+1)
1752 * if the mode is integer or float.
1753 * Transform Add(a,-b) into Sub(a,b).
1754 * Reassociation might fold this further.
1756 static ir_node *transform_node_Add(ir_node *n)
1761 n = transform_node_AddSub(n);
1763 mode = get_irn_mode(n);
1764 if (mode_is_num(mode)) {
1765 ir_node *a = get_Add_left(n);
1766 ir_node *b = get_Add_right(n);
1769 ir_node *block = get_irn_n(n, -1);
1772 get_irn_dbg_info(n),
1776 new_r_Const_long(current_ir_graph, block, mode, 2),
1778 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1780 else if (get_irn_op(a) == op_Minus) {
1782 get_irn_dbg_info(n),
1788 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1790 else if (get_irn_op(b) == op_Minus) {
1792 get_irn_dbg_info(n),
1798 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1800 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1801 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1802 ir_node *ma = get_Mul_left(a);
1803 ir_node *mb = get_Mul_right(a);
1806 ir_node *blk = get_irn_n(n, -1);
1808 get_irn_dbg_info(n), current_ir_graph, blk,
1811 get_irn_dbg_info(n), current_ir_graph, blk,
1813 new_r_Const_long(current_ir_graph, blk, mode, 1),
1816 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1819 ir_node *blk = get_irn_n(n, -1);
1821 get_irn_dbg_info(n), current_ir_graph, blk,
1824 get_irn_dbg_info(n), current_ir_graph, blk,
1826 new_r_Const_long(current_ir_graph, blk, mode, 1),
1829 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1832 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1833 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1834 ir_node *ma = get_Mul_left(b);
1835 ir_node *mb = get_Mul_right(b);
1838 ir_node *blk = get_irn_n(n, -1);
1840 get_irn_dbg_info(n), current_ir_graph, blk,
1843 get_irn_dbg_info(n), current_ir_graph, blk,
1845 new_r_Const_long(current_ir_graph, blk, mode, 1),
1848 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1851 ir_node *blk = get_irn_n(n, -1);
1853 get_irn_dbg_info(n), current_ir_graph, blk,
1856 get_irn_dbg_info(n), current_ir_graph, blk,
1858 new_r_Const_long(current_ir_graph, blk, mode, 1),
1861 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1869 * Do the AddSub optimization, then Transform
1870 * Sub(0,a) -> Minus(a)
1871 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1873 static ir_node *transform_node_Sub(ir_node *n)
1879 n = transform_node_AddSub(n);
1881 mode = get_irn_mode(n);
1882 a = get_Sub_left(n);
1883 b = get_Sub_right(n);
1884 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1886 get_irn_dbg_info(n),
1891 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1893 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1894 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1895 ir_node *ma = get_Mul_left(a);
1896 ir_node *mb = get_Mul_right(a);
1899 ir_node *blk = get_irn_n(n, -1);
1901 get_irn_dbg_info(n),
1902 current_ir_graph, blk,
1905 get_irn_dbg_info(n),
1906 current_ir_graph, blk,
1908 new_r_Const_long(current_ir_graph, blk, mode, 1),
1911 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1914 ir_node *blk = get_irn_n(n, -1);
1916 get_irn_dbg_info(n),
1917 current_ir_graph, blk,
1920 get_irn_dbg_info(n),
1921 current_ir_graph, blk,
1923 new_r_Const_long(current_ir_graph, blk, mode, 1),
1926 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1934 * Transform Mul(a,-1) into -a.
1935 * Do architecture dependent optimizations on Mul nodes
1937 static ir_node *transform_node_Mul(ir_node *n) {
1939 ir_mode *mode = get_irn_mode(n);
1941 if (mode_is_signed(mode)) {
1943 ir_node *a = get_Mul_left(n);
1944 ir_node *b = get_Mul_right(n);
1946 if (value_of(a) == get_mode_minus_one(mode))
1948 else if (value_of(b) == get_mode_minus_one(mode))
1951 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1952 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1956 return arch_dep_replace_mul_with_shifts(n);
1960 * transform a Div Node
1962 static ir_node *transform_node_Div(ir_node *n)
1964 tarval *tv = value_of(n);
1967 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1969 if (tv != tarval_bad) {
1970 value = new_Const(get_tarval_mode(tv), tv);
1972 DBG_OPT_CSTEVAL(n, value);
1974 else /* Try architecture dependent optimization */
1975 value = arch_dep_replace_div_by_const(n);
1978 /* Turn Div into a tuple (mem, bad, value) */
1979 ir_node *mem = get_Div_mem(n);
1981 turn_into_tuple(n, pn_Div_max);
1982 set_Tuple_pred(n, pn_Div_M, mem);
1983 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1984 set_Tuple_pred(n, pn_Div_res, value);
1990 * transform a Mod node
1992 static ir_node *transform_node_Mod(ir_node *n)
1994 tarval *tv = value_of(n);
1997 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1999 if (tv != tarval_bad) {
2000 value = new_Const(get_tarval_mode(tv), tv);
2002 DBG_OPT_CSTEVAL(n, value);
2004 else /* Try architecture dependent optimization */
2005 value = arch_dep_replace_mod_by_const(n);
2008 /* Turn Mod into a tuple (mem, bad, value) */
2009 ir_node *mem = get_Mod_mem(n);
2011 turn_into_tuple(n, pn_Mod_max);
2012 set_Tuple_pred(n, pn_Mod_M, mem);
2013 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2014 set_Tuple_pred(n, pn_Mod_res, value);
2020 * transform a DivMod node
2022 static ir_node *transform_node_DivMod(ir_node *n)
2026 ir_node *a = get_DivMod_left(n);
2027 ir_node *b = get_DivMod_right(n);
2028 ir_mode *mode = get_irn_mode(a);
2029 tarval *ta = value_of(a);
2030 tarval *tb = value_of(b);
2032 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2035 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2037 if (tb != tarval_bad) {
2038 if (tb == get_mode_one(get_tarval_mode(tb))) {
2039 b = new_Const (mode, get_mode_null(mode));
2042 DBG_OPT_CSTEVAL(n, b);
2044 else if (ta != tarval_bad) {
2045 tarval *resa, *resb;
2046 resa = tarval_div (ta, tb);
2047 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2048 Jmp for X result!? */
2049 resb = tarval_mod (ta, tb);
2050 if (resb == tarval_bad) return n; /* Causes exception! */
2051 a = new_Const (mode, resa);
2052 b = new_Const (mode, resb);
2055 DBG_OPT_CSTEVAL(n, a);
2056 DBG_OPT_CSTEVAL(n, b);
2058 else { /* Try architecture dependent optimization */
2059 arch_dep_replace_divmod_by_const(&a, &b, n);
2060 evaluated = a != NULL;
2062 } else if (ta == get_mode_null(mode)) {
2063 /* 0 / non-Const = 0 */
2068 if (evaluated) { /* replace by tuple */
2069 ir_node *mem = get_DivMod_mem(n);
2070 turn_into_tuple(n, pn_DivMod_max);
2071 set_Tuple_pred(n, pn_DivMod_M, mem);
2072 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2073 set_Tuple_pred(n, pn_DivMod_res_div, a);
2074 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2081 * Optimize Abs(x) into x if x is Confirmed >= 0
2082 * Optimize Abs(x) into -x if x is Confirmed <= 0
2084 static ir_node *transform_node_Abs(ir_node *n)
2087 ir_node *a = get_Abs_op(n);
2088 value_classify sign = classify_value_sign(a);
2090 if (sign == VALUE_NEGATIVE) {
2091 ir_mode *mode = get_irn_mode(n);
2094 * We can replace the Abs by -x here.
2095 * We even could add a new Confirm here.
2097 * Note that -x would create a new node, so we could
2098 * not run it in the equivalent_node() context.
2100 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2101 get_irn_n(n, -1), a, mode);
2103 DBG_OPT_CONFIRM(oldn, n);
2105 else if (sign == VALUE_POSITIVE) {
2106 /* n is positive, Abs is not needed */
2109 DBG_OPT_CONFIRM(oldn, n);
2116 * transform a Cond node
2118 static ir_node *transform_node_Cond(ir_node *n)
2120 /* Replace the Cond by a Jmp if it branches on a constant
2123 ir_node *a = get_Cond_selector(n);
2124 tarval *ta = value_of(a);
2126 /* we need block info which is not available in floating irgs */
2127 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2130 if ((ta != tarval_bad) &&
2131 (get_irn_mode(a) == mode_b) &&
2132 (get_opt_unreachable_code())) {
2133 /* It's a boolean Cond, branching on a boolean constant.
2134 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2135 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2136 turn_into_tuple(n, pn_Cond_max);
2137 if (ta == tarval_b_true) {
2138 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2139 set_Tuple_pred(n, pn_Cond_true, jmp);
2141 set_Tuple_pred(n, pn_Cond_false, jmp);
2142 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2144 /* We might generate an endless loop, so keep it alive. */
2145 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2153 static ir_node *transform_node_Eor(ir_node *n)
2156 ir_node *a = get_Eor_left(n);
2157 ir_node *b = get_Eor_right(n);
2158 ir_mode *mode = get_irn_mode(n);
2162 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2163 mode, get_mode_null(mode));
2164 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2166 else if ((mode == mode_b)
2167 && (get_irn_op(a) == op_Proj)
2168 && (get_irn_mode(a) == mode_b)
2169 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2170 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2171 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2172 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2173 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2175 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2177 else if ((mode == mode_b)
2178 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2179 /* The Eor is a Not. Replace it by a Not. */
2180 /* ????!!!Extend to bitfield 1111111. */
2181 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2190 * Transform a boolean Not.
2192 static ir_node *transform_node_Not(ir_node *n)
2195 ir_node *a = get_Not_op(n);
2197 if ( (get_irn_mode(n) == mode_b)
2198 && (get_irn_op(a) == op_Proj)
2199 && (get_irn_mode(a) == mode_b)
2200 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2201 /* We negate a Cmp. The Cmp has the negated result anyways! */
2202 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2203 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2204 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2211 * Transform a Cast_type(Const) into a new Const_type
2213 static ir_node *transform_node_Cast(ir_node *n) {
2215 ir_node *pred = get_Cast_op(n);
2216 ir_type *tp = get_irn_type(n);
2218 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2219 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2220 get_Const_tarval(pred), tp);
2221 DBG_OPT_CSTEVAL(oldn, n);
2222 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2223 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2224 get_SymConst_kind(pred), tp);
2225 DBG_OPT_CSTEVAL(oldn, n);
2232 * Transform a Proj(Div) with a non-zero value.
2233 * Removes the exceptions and routes the memory to the NoMem node.
2235 static ir_node *transform_node_Proj_Div(ir_node *proj)
2237 ir_node *n = get_Proj_pred(proj);
2238 ir_node *b = get_Div_right(n);
2241 if (value_not_zero(b)) {
2242 /* div(x, y) && y != 0 */
2243 proj_nr = get_Proj_proj(proj);
2245 /* this node may float if it did not depend on a Confirm */
2246 set_irn_pinned(n, op_pin_state_floats);
2248 if (proj_nr == pn_Div_X_except) {
2249 /* we found an exception handler, remove it */
2250 DBG_OPT_EXC_REM(proj);
2253 else if (proj_nr == pn_Div_M) {
2254 ir_node *res = get_Div_mem(n);
2255 /* the memory Proj can only be removed if we divide by a
2256 real constant, but the node never produce a new memory */
2257 if (value_of(b) != tarval_bad) {
2258 /* this is a Div by a const, we can remove the memory edge */
2259 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2268 * Transform a Proj(Mod) with a non-zero value.
2269 * Removes the exceptions and routes the memory to the NoMem node.
2271 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2273 ir_node *n = get_Proj_pred(proj);
2274 ir_node *b = get_Mod_right(n);
2277 if (value_not_zero(b)) {
2278 /* mod(x, y) && y != 0 */
2279 proj_nr = get_Proj_proj(proj);
2281 /* this node may float if it did not depend on a Confirm */
2282 set_irn_pinned(n, op_pin_state_floats);
2284 if (proj_nr == pn_Mod_X_except) {
2285 /* we found an exception handler, remove it */
2286 DBG_OPT_EXC_REM(proj);
2288 } else if (proj_nr == pn_Mod_M) {
2289 ir_node *res = get_Mod_mem(n);
2290 /* the memory Proj can only be removed if we divide by a
2291 real constant, but the node never produce a new memory */
2292 if (value_of(b) != tarval_bad) {
2293 /* this is a Mod by a const, we can remove the memory edge */
2294 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2298 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2299 /* a % a = 0 if a != 0 */
2300 ir_mode *mode = get_irn_mode(proj);
2301 ir_node *res = new_Const(mode, get_mode_null(mode));
2303 DBG_OPT_CSTEVAL(n, res);
2311 * Transform a Proj(DivMod) with a non-zero value.
2312 * Removes the exceptions and routes the memory to the NoMem node.
2314 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2316 ir_node *n = get_Proj_pred(proj);
2317 ir_node *b = get_DivMod_right(n);
2320 if (value_not_zero(b)) {
2321 /* DivMod(x, y) && y != 0 */
2322 proj_nr = get_Proj_proj(proj);
2324 /* this node may float if it did not depend on a Confirm */
2325 set_irn_pinned(n, op_pin_state_floats);
2327 if (proj_nr == pn_DivMod_X_except) {
2328 /* we found an exception handler, remove it */
2329 DBG_OPT_EXC_REM(proj);
2332 else if (proj_nr == pn_DivMod_M) {
2333 ir_node *res = get_DivMod_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 DivMod by a const, we can remove the memory edge */
2338 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2342 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_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 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2357 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2359 if (get_opt_unreachable_code()) {
2360 ir_node *n = get_Proj_pred(proj);
2361 ir_node *b = get_Cond_selector(n);
2363 if (mode_is_int(get_irn_mode(b))) {
2364 tarval *tb = value_of(b);
2366 if (tb != tarval_bad) {
2367 /* we have a constant switch */
2368 long num = get_Proj_proj(proj);
2370 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2371 if (get_tarval_long(tb) == num) {
2372 /* Do NOT create a jump here, or we will have 2 control flow ops
2373 * in a block. This case is optimized away in optimize_cf(). */
2377 /* this case will NEVER be taken, kill it */
2388 * Normalizes and optimizes Cmp nodes.
2390 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2392 if (get_opt_reassociation()) {
2393 ir_node *n = get_Proj_pred(proj);
2394 ir_node *left = get_Cmp_left(n);
2395 ir_node *right = get_Cmp_right(n);
2399 ir_mode *mode = NULL;
2400 long proj_nr = get_Proj_proj(proj);
2403 * First step: normalize the compare op
2404 * by placing the constant on the right site
2405 * or moving the lower address node to the left.
2406 * We ignore the case that both are constants
2407 * this case should be optimized away.
2409 if (get_irn_op(right) == op_Const)
2411 else if (get_irn_op(left) == op_Const) {
2416 proj_nr = get_inversed_pnc(proj_nr);
2419 else if (get_irn_idx(left) > get_irn_idx(right)) {
2425 proj_nr = get_inversed_pnc(proj_nr);
2430 * Second step: Try to reduce the magnitude
2431 * of a constant. This may help to generate better code
2432 * later and may help to normalize more compares.
2433 * Of course this is only possible for integer values.
2436 mode = get_irn_mode(c);
2437 tv = get_Const_tarval(c);
2439 if (tv != tarval_bad) {
2440 /* the following optimization is possible on modes without Overflow
2441 * on Unary Minus or on == and !=:
2442 * -a CMP c ==> a swap(CMP) -c
2444 * Beware: for two-complement Overflow may occur, so only == and != can
2445 * be optimized, see this:
2446 * -MININT < 0 =/=> MININT > 0 !!!
2448 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2449 (!mode_overflow_on_unary_Minus(mode) ||
2450 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2451 left = get_Minus_op(left);
2452 tv = tarval_sub(get_mode_null(mode), tv);
2454 proj_nr = get_inversed_pnc(proj_nr);
2458 /* for integer modes, we have more */
2459 if (mode_is_int(mode)) {
2460 /* Ne includes Unordered which is not possible on integers.
2461 * However, frontends often use this wrong, so fix it here */
2462 if (proj_nr & pn_Cmp_Uo) {
2463 proj_nr &= ~pn_Cmp_Uo;
2464 set_Proj_proj(proj, proj_nr);
2467 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2468 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2469 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2470 tv = tarval_sub(tv, get_mode_one(mode));
2472 proj_nr ^= pn_Cmp_Eq;
2475 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2476 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2477 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2478 tv = tarval_add(tv, get_mode_one(mode));
2480 proj_nr ^= pn_Cmp_Eq;
2484 /* the following reassociations work only for == and != */
2485 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2487 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2488 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2489 right = get_Sub_right(left);
2490 left = get_Sub_left(left);
2492 tv = value_of(right);
2496 if (tv != tarval_bad) {
2497 ir_op *op = get_irn_op(left);
2499 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2501 ir_node *c1 = get_Sub_right(left);
2502 tarval *tv2 = value_of(c1);
2504 if (tv2 != tarval_bad) {
2505 tv2 = tarval_add(tv, value_of(c1));
2507 if (tv2 != tarval_bad) {
2508 left = get_Sub_left(left);
2514 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2515 else if (op == op_Add) {
2516 ir_node *a_l = get_Add_left(left);
2517 ir_node *a_r = get_Add_right(left);
2521 if (get_irn_op(a_l) == op_Const) {
2523 tv2 = value_of(a_l);
2527 tv2 = value_of(a_r);
2530 if (tv2 != tarval_bad) {
2531 tv2 = tarval_sub(tv, tv2);
2533 if (tv2 != tarval_bad) {
2540 /* -a == c ==> a == -c, -a != c ==> a != -c */
2541 else if (op == op_Minus) {
2542 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2544 if (tv2 != tarval_bad) {
2545 left = get_Minus_op(left);
2552 /* the following reassociations work only for <= */
2553 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2554 if (tv != tarval_bad) {
2555 ir_op *op = get_irn_op(left);
2557 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2565 * optimization for AND:
2567 * And(x, C) == C ==> And(x, C) != 0
2568 * And(x, C) != C ==> And(X, C) == 0
2570 * if C is a single Bit constant.
2572 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2573 (get_irn_op(left) == op_And)) {
2574 if (is_single_bit_tarval(tv)) {
2575 /* check for Constant's match. We have check hare the tarvals,
2576 because our const might be changed */
2577 ir_node *la = get_And_left(left);
2578 ir_node *ra = get_And_right(left);
2579 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2580 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2581 /* fine: do the transformation */
2582 tv = get_mode_null(get_tarval_mode(tv));
2583 proj_nr ^= pn_Cmp_Leg;
2588 } /* tarval != bad */
2592 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2594 if (changed & 2) /* need a new Const */
2595 right = new_Const(mode, tv);
2597 /* create a new compare */
2598 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2601 set_Proj_pred(proj, n);
2602 set_Proj_proj(proj, proj_nr);
2609 * Does all optimizations on nodes that must be done on it's Proj's
2610 * because of creating new nodes.
2612 static ir_node *transform_node_Proj(ir_node *proj)
2614 ir_node *n = get_Proj_pred(proj);
2616 switch (get_irn_opcode(n)) {
2618 return transform_node_Proj_Div(proj);
2621 return transform_node_Proj_Mod(proj);
2624 return transform_node_Proj_DivMod(proj);
2627 return transform_node_Proj_Cond(proj);
2630 return transform_node_Proj_Cmp(proj);
2633 /* should not happen, but if it does will be optimized away */
2634 return equivalent_node_Proj(proj);
2643 * Move Confirms down through Phi nodes.
2645 static ir_node *transform_node_Phi(ir_node *phi) {
2647 ir_mode *mode = get_irn_mode(phi);
2649 if (mode_is_reference(mode)) {
2650 n = get_irn_arity(phi);
2652 /* Beware of Phi0 */
2654 ir_node *pred = get_irn_n(phi, 0);
2658 if (! is_Confirm(pred))
2661 bound = get_Confirm_bound(pred);
2662 pnc = get_Confirm_cmp(pred);
2664 for (i = 1; i < n; ++i) {
2665 pred = get_irn_n(phi, i);
2667 if (! is_Confirm(pred) ||
2668 get_Confirm_bound(pred) != bound ||
2669 get_Confirm_cmp(pred) != pnc)
2672 return new_r_Confirm(current_ir_graph, get_irn_n(phi, -1), phi, bound, pnc);
2679 * returns the operands of a commutative bin-op, if one operand is
2680 * a const, it is returned as the second one.
2682 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2684 ir_node *op_a = get_binop_left(binop);
2685 ir_node *op_b = get_binop_right(binop);
2687 assert(is_op_commutative(get_irn_op(binop)));
2689 if (get_irn_op(op_a) == op_Const) {
2700 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2701 * Such pattern may arise in bitfield stores.
2703 * value c4 value c4 & c2
2704 * AND c3 AND c1 | c3
2709 static ir_node *transform_node_Or_bf_store(ir_node *or)
2713 ir_node *and_l, *c3;
2714 ir_node *value, *c4;
2715 ir_node *new_and, *new_const, *block;
2716 ir_mode *mode = get_irn_mode(or);
2718 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2720 get_comm_Binop_Ops(or, &and, &c1);
2721 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2724 get_comm_Binop_Ops(and, &or_l, &c2);
2725 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2728 get_comm_Binop_Ops(or_l, &and_l, &c3);
2729 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2732 get_comm_Binop_Ops(and_l, &value, &c4);
2733 if (get_irn_op(c4) != op_Const)
2736 /* ok, found the pattern, check for conditions */
2737 assert(mode == get_irn_mode(and));
2738 assert(mode == get_irn_mode(or_l));
2739 assert(mode == get_irn_mode(and_l));
2741 tv1 = get_Const_tarval(c1);
2742 tv2 = get_Const_tarval(c2);
2743 tv3 = get_Const_tarval(c3);
2744 tv4 = get_Const_tarval(c4);
2746 tv = tarval_or(tv4, tv2);
2747 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2748 /* have at least one 0 at the same bit position */
2752 n_tv4 = tarval_not(tv4);
2753 if (tv3 != tarval_and(tv3, n_tv4)) {
2754 /* bit in the or_mask is outside the and_mask */
2758 n_tv2 = tarval_not(tv2);
2759 if (tv1 != tarval_and(tv1, n_tv2)) {
2760 /* bit in the or_mask is outside the and_mask */
2764 /* ok, all conditions met */
2765 block = get_irn_n(or, -1);
2767 new_and = new_r_And(current_ir_graph, block,
2768 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2770 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2772 set_Or_left(or, new_and);
2773 set_Or_right(or, new_const);
2775 /* check for more */
2776 return transform_node_Or_bf_store(or);
2780 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2782 static ir_node *transform_node_Or_Rot(ir_node *or)
2784 ir_mode *mode = get_irn_mode(or);
2785 ir_node *shl, *shr, *block;
2786 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2789 if (! mode_is_int(mode))
2792 shl = get_binop_left(or);
2793 shr = get_binop_right(or);
2795 if (get_irn_op(shl) == op_Shr) {
2796 if (get_irn_op(shr) != op_Shl)
2803 else if (get_irn_op(shl) != op_Shl)
2805 else if (get_irn_op(shr) != op_Shr)
2808 x = get_Shl_left(shl);
2809 if (x != get_Shr_left(shr))
2812 c1 = get_Shl_right(shl);
2813 c2 = get_Shr_right(shr);
2814 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2815 tv1 = get_Const_tarval(c1);
2816 if (! tarval_is_long(tv1))
2819 tv2 = get_Const_tarval(c2);
2820 if (! tarval_is_long(tv2))
2823 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2824 != get_mode_size_bits(mode))
2827 /* yet, condition met */
2828 block = get_irn_n(or, -1);
2830 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2832 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2835 else if (get_irn_op(c1) == op_Sub) {
2839 if (get_Sub_right(sub) != v)
2842 c1 = get_Sub_left(sub);
2843 if (get_irn_op(c1) != op_Const)
2846 tv1 = get_Const_tarval(c1);
2847 if (! tarval_is_long(tv1))
2850 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2853 /* yet, condition met */
2854 block = get_nodes_block(or);
2856 /* a Rot right is not supported, so use a rot left */
2857 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2859 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2862 else if (get_irn_op(c2) == op_Sub) {
2866 c1 = get_Sub_left(sub);
2867 if (get_irn_op(c1) != op_Const)
2870 tv1 = get_Const_tarval(c1);
2871 if (! tarval_is_long(tv1))
2874 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2877 /* yet, condition met */
2878 block = get_irn_n(or, -1);
2881 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2883 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2893 static ir_node *transform_node_Or(ir_node *or)
2895 or = transform_node_Or_bf_store(or);
2896 or = transform_node_Or_Rot(or);
2902 static ir_node *transform_node(ir_node *n);
2905 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2907 * Should be moved to reassociation?
2909 static ir_node *transform_node_shift(ir_node *n)
2911 ir_node *left, *right;
2912 tarval *tv1, *tv2, *res;
2914 int modulo_shf, flag;
2916 left = get_binop_left(n);
2918 /* different operations */
2919 if (get_irn_op(left) != get_irn_op(n))
2922 right = get_binop_right(n);
2923 tv1 = value_of(right);
2924 if (tv1 == tarval_bad)
2927 tv2 = value_of(get_binop_right(left));
2928 if (tv2 == tarval_bad)
2931 res = tarval_add(tv1, tv2);
2933 /* beware: a simple replacement works only, if res < modulo shift */
2934 mode = get_irn_mode(n);
2938 modulo_shf = get_mode_modulo_shift(mode);
2939 if (modulo_shf > 0) {
2940 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2942 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2949 /* ok, we can replace it */
2950 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2952 in[0] = get_binop_left(left);
2953 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2955 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2957 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2959 return transform_node(irn);
2964 #define transform_node_Shr transform_node_shift
2965 #define transform_node_Shrs transform_node_shift
2966 #define transform_node_Shl transform_node_shift
2969 * Remove dead blocks and nodes in dead blocks
2970 * in keep alive list. We do not generate a new End node.
2972 static ir_node *transform_node_End(ir_node *n) {
2973 int i, n_keepalives = get_End_n_keepalives(n);
2975 for (i = 0; i < n_keepalives; ++i) {
2976 ir_node *ka = get_End_keepalive(n, i);
2978 if (is_Block_dead(ka)) {
2979 set_End_keepalive(n, i, new_Bad());
2982 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2983 set_End_keepalive(n, i, new_Bad());
2989 * Optimize a Mux into some simpler cases.
2991 static ir_node *transform_node_Mux(ir_node *n)
2993 ir_node *oldn = n, *sel = get_Mux_sel(n);
2994 ir_mode *mode = get_irn_mode(n);
2996 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2997 ir_node *cmp = get_Proj_pred(sel);
2998 long proj_nr = get_Proj_proj(sel);
2999 ir_node *f = get_Mux_false(n);
3000 ir_node *t = get_Mux_true(n);
3002 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3003 ir_node *block = get_irn_n(n, -1);
3006 * Note: normalization puts the constant on the right site,
3007 * so we check only one case.
3009 * Note further that these optimization work even for floating point
3010 * with NaN's because -NaN == NaN.
3011 * However, if +0 and -0 is handled differently, we cannot use the first one.
3013 if (get_irn_op(f) == op_Minus &&
3014 get_Minus_op(f) == t &&
3015 get_Cmp_left(cmp) == t) {
3017 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3018 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3019 n = new_rd_Abs(get_irn_dbg_info(n),
3023 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3026 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3027 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3028 n = new_rd_Abs(get_irn_dbg_info(n),
3032 n = new_rd_Minus(get_irn_dbg_info(n),
3037 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3041 else if (get_irn_op(t) == op_Minus &&
3042 get_Minus_op(t) == f &&
3043 get_Cmp_left(cmp) == f) {
3045 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3046 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3047 n = new_rd_Abs(get_irn_dbg_info(n),
3051 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3054 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3055 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3056 n = new_rd_Abs(get_irn_dbg_info(n),
3060 n = new_rd_Minus(get_irn_dbg_info(n),
3065 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3070 if (mode_is_int(mode) && mode_is_signed(mode) &&
3071 get_mode_arithmetic(mode) == irma_twos_complement) {
3072 ir_node *x = get_Cmp_left(cmp);
3074 /* the following optimization works only with signed integer two-complement mode */
3076 if (mode == get_irn_mode(x)) {
3078 * FIXME: this restriction is two rigid, as it would still
3079 * work if mode(x) = Hs and mode == Is, but at least it removes
3082 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3083 classify_Const(t) == CNST_ALL_ONE &&
3084 classify_Const(f) == CNST_NULL) {
3086 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3090 n = new_rd_Shrs(get_irn_dbg_info(n),
3091 current_ir_graph, block, x,
3092 new_r_Const_long(current_ir_graph, block, mode_Iu,
3093 get_mode_size_bits(mode) - 1),
3095 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3098 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3099 classify_Const(t) == CNST_ONE &&
3100 classify_Const(f) == CNST_NULL) {
3102 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3106 n = new_rd_Shr(get_irn_dbg_info(n),
3107 current_ir_graph, block,
3108 new_r_Minus(current_ir_graph, block, x, mode),
3109 new_r_Const_long(current_ir_graph, block, mode_Iu,
3110 get_mode_size_bits(mode) - 1),
3112 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3119 return arch_transform_node_Mux(n);
3123 * Optimize a Psi into some simpler cases.
3125 static ir_node *transform_node_Psi(ir_node *n) {
3127 return transform_node_Mux(n);
3133 * Tries several [inplace] [optimizing] transformations and returns an
3134 * equivalent node. The difference to equivalent_node() is that these
3135 * transformations _do_ generate new nodes, and thus the old node must
3136 * not be freed even if the equivalent node isn't the old one.
3138 static ir_node *transform_node(ir_node *n)
3140 if (n->op->ops.transform_node)
3141 n = n->op->ops.transform_node(n);
3146 * sSets the default transform node operation for an ir_op_ops.
3148 * @param code the opcode for the default operation
3149 * @param ops the operations initialized
3154 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3158 ops->transform_node = transform_node_##a; \
3192 /* **************** Common Subexpression Elimination **************** */
3194 /** The size of the hash table used, should estimate the number of nodes
3196 #define N_IR_NODES 512
3198 /** Compares the attributes of two Const nodes. */
3199 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3201 return (get_Const_tarval(a) != get_Const_tarval(b))
3202 || (get_Const_type(a) != get_Const_type(b));
3205 /** Compares the attributes of two Proj nodes. */
3206 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3208 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3211 /** Compares the attributes of two Filter nodes. */
3212 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3214 return get_Filter_proj(a) != get_Filter_proj(b);
3217 /** Compares the attributes of two Alloc nodes. */
3218 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3220 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3221 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3224 /** Compares the attributes of two Free nodes. */
3225 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3227 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3228 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3231 /** Compares the attributes of two SymConst nodes. */
3232 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3234 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3235 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3236 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3239 /** Compares the attributes of two Call nodes. */
3240 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3242 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3245 /** Compares the attributes of two Sel nodes. */
3246 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3248 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3249 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3250 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3251 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3252 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3255 /** Compares the attributes of two Phi nodes. */
3256 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3258 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3261 /** Compares the attributes of two Cast nodes. */
3262 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3264 return get_Cast_type(a) != get_Cast_type(b);
3267 /** Compares the attributes of two Load nodes. */
3268 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3270 if (get_Load_volatility(a) == volatility_is_volatile ||
3271 get_Load_volatility(b) == volatility_is_volatile)
3272 /* NEVER do CSE on volatile Loads */
3275 return get_Load_mode(a) != get_Load_mode(b);
3278 /** Compares the attributes of two Store nodes. */
3279 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3281 /* NEVER do CSE on volatile Stores */
3282 return (get_Store_volatility(a) == volatility_is_volatile ||
3283 get_Store_volatility(b) == volatility_is_volatile);
3286 /** Compares the attributes of two Confirm nodes. */
3287 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3289 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3293 * Set the default node attribute compare operation for an ir_op_ops.
3295 * @param code the opcode for the default operation
3296 * @param ops the operations initialized
3301 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3305 ops->node_cmp_attr = node_cmp_attr_##a; \
3331 * Compare function for two nodes in the hash table. Gets two
3332 * nodes as parameters. Returns 0 if the nodes are a cse.
3334 int identities_cmp(const void *elt, const void *key)
3342 if (a == b) return 0;
3344 if ((get_irn_op(a) != get_irn_op(b)) ||
3345 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3347 /* compare if a's in and b's in are of equal length */
3348 irn_arity_a = get_irn_intra_arity (a);
3349 if (irn_arity_a != get_irn_intra_arity(b))
3352 /* for block-local cse and op_pin_state_pinned nodes: */
3353 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3354 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3358 /* compare a->in[0..ins] with b->in[0..ins] */
3359 for (i = 0; i < irn_arity_a; i++)
3360 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3364 * here, we already now that the nodes are identical except their
3367 if (a->op->ops.node_cmp_attr)
3368 return a->op->ops.node_cmp_attr(a, b);
3374 * Calculate a hash value of a node.
3377 ir_node_hash (ir_node *node)
3382 if (node->op == op_Const) {
3383 /* special value for const, as they only differ in their tarval. */
3384 h = HASH_PTR(node->attr.con.tv);
3385 h = 9*h + HASH_PTR(get_irn_mode(node));
3386 } else if (node->op == op_SymConst) {
3387 /* special value for const, as they only differ in their symbol. */
3388 h = HASH_PTR(node->attr.i.sym.type_p);
3389 h = 9*h + HASH_PTR(get_irn_mode(node));
3392 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3393 h = irn_arity = get_irn_intra_arity(node);
3395 /* consider all in nodes... except the block if not a control flow. */
3396 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3397 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3401 h = 9*h + HASH_PTR(get_irn_mode(node));
3403 h = 9*h + HASH_PTR(get_irn_op(node));
3410 new_identities(void) {
3411 return new_pset(identities_cmp, N_IR_NODES);
3415 del_identities(pset *value_table) {
3416 del_pset(value_table);
3420 * Return the canonical node computing the same value as n.
3421 * Looks up the node in a hash table.
3423 * For Const nodes this is performed in the constructor, too. Const
3424 * nodes are extremely time critical because of their frequent use in
3425 * constant string arrays.
3427 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3431 if (!value_table) return n;
3433 if (get_opt_reassociation()) {
3434 if (is_op_commutative(get_irn_op(n))) {
3435 ir_node *l = get_binop_left(n);
3436 ir_node *r = get_binop_right(n);
3438 /* for commutative operators perform a OP b == b OP a */
3439 if (get_irn_idx(l) > get_irn_idx(r)) {
3440 set_binop_left(n, r);
3441 set_binop_right(n, l);
3446 o = pset_find(value_table, n, ir_node_hash (n));
3455 * During construction we set the op_pin_state_pinned flag in the graph right when the
3456 * optimization is performed. The flag turning on procedure global cse could
3457 * be changed between two allocations. This way we are safe.
3459 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3462 n = identify(value_table, n);
3463 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3464 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3469 * Return the canonical node computing the same value as n.
3470 * Looks up the node in a hash table, enters it in the table
3471 * if it isn't there yet.
3473 ir_node *identify_remember(pset *value_table, ir_node *n)
3477 if (!value_table) return n;
3479 if (get_opt_reassociation()) {
3480 if (is_op_commutative(get_irn_op(n))) {
3481 ir_node *l = get_binop_left(n);
3482 ir_node *r = get_binop_right(n);
3484 /* for commutative operators perform a OP b == b OP a */
3486 set_binop_left(n, r);
3487 set_binop_right(n, l);
3492 /* lookup or insert in hash table with given hash key. */
3493 o = pset_insert (value_table, n, ir_node_hash (n));
3502 /* Add a node to the identities value table. */
3503 void add_identities(pset *value_table, ir_node *node) {
3504 if (get_opt_cse() && is_no_Block(node))
3505 identify_remember(value_table, node);
3508 /* Visit each node in the value table of a graph. */
3509 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3511 ir_graph *rem = current_ir_graph;
3513 current_ir_graph = irg;
3514 foreach_pset(irg->value_table, node)
3516 current_ir_graph = rem;
3520 * garbage in, garbage out. If a node has a dead input, i.e., the
3521 * Bad node is input to the node, return the Bad node.
3523 static INLINE ir_node *gigo(ir_node *node)
3526 ir_op *op = get_irn_op(node);
3528 /* remove garbage blocks by looking at control flow that leaves the block
3529 and replacing the control flow by Bad. */
3530 if (get_irn_mode(node) == mode_X) {
3531 ir_node *block = get_nodes_block(skip_Proj(node));
3533 /* Don't optimize nodes in immature blocks. */
3534 if (!get_Block_matured(block)) return node;
3535 /* Don't optimize End, may have Bads. */
3536 if (op == op_End) return node;
3538 if (is_Block(block)) {
3539 irn_arity = get_irn_arity(block);
3540 for (i = 0; i < irn_arity; i++) {
3541 if (!is_Bad(get_irn_n(block, i)))
3544 if (i == irn_arity) return new_Bad();
3548 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3549 blocks predecessors is dead. */
3550 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3551 irn_arity = get_irn_arity(node);
3554 * Beware: we can only read the block of a non-floating node.
3556 if (is_irn_pinned_in_irg(node) &&
3557 is_Block_dead(get_nodes_block(node)))
3560 for (i = 0; i < irn_arity; i++) {
3561 ir_node *pred = get_irn_n(node, i);
3566 /* Propagating Unknowns here seems to be a bad idea, because
3567 sometimes we need a node as a input and did not want that
3569 However, i might be useful to move this into a later phase
3570 (it you thing optimizing such code is useful). */
3571 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3572 return new_Unknown(get_irn_mode(node));
3577 /* With this code we violate the agreement that local_optimize
3578 only leaves Bads in Block, Phi and Tuple nodes. */
3579 /* If Block has only Bads as predecessors it's garbage. */
3580 /* If Phi has only Bads as predecessors it's garbage. */
3581 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3582 irn_arity = get_irn_arity(node);
3583 for (i = 0; i < irn_arity; i++) {
3584 if (!is_Bad(get_irn_n(node, i))) break;
3586 if (i == irn_arity) node = new_Bad();
3593 * These optimizations deallocate nodes from the obstack.
3594 * It can only be called if it is guaranteed that no other nodes
3595 * reference this one, i.e., right after construction of a node.
3597 * current_ir_graph must be set to the graph of the node!
3599 ir_node *optimize_node(ir_node *n)
3603 opcode iro = get_irn_opcode(n);
3605 /* Always optimize Phi nodes: part of the construction. */
3606 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3608 /* constant expression evaluation / constant folding */
3609 if (get_opt_constant_folding()) {
3610 /* neither constants nor Tuple values can be evaluated */
3611 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3612 /* try to evaluate */
3613 tv = computed_value(n);
3614 if (tv != tarval_bad) {
3616 ir_type *old_tp = get_irn_type(n);
3617 int i, arity = get_irn_arity(n);
3621 * Try to recover the type of the new expression.
3623 for (i = 0; i < arity && !old_tp; ++i)
3624 old_tp = get_irn_type(get_irn_n(n, i));
3627 * we MUST copy the node here temporary, because it's still needed
3628 * for DBG_OPT_CSTEVAL
3630 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3631 oldn = alloca(node_size);
3633 memcpy(oldn, n, node_size);
3634 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3636 /* ARG, copy the in array, we need it for statistics */
3637 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3639 /* note the inplace edges module */
3640 edges_node_deleted(n, current_ir_graph);
3642 /* evaluation was successful -- replace the node. */
3643 irg_kill_node(current_ir_graph, n);
3644 nw = new_Const(get_tarval_mode (tv), tv);
3646 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3647 set_Const_type(nw, old_tp);
3648 DBG_OPT_CSTEVAL(oldn, nw);
3654 /* remove unnecessary nodes */
3655 if (get_opt_constant_folding() ||
3656 (iro == iro_Phi) || /* always optimize these nodes. */
3658 (iro == iro_Proj) ||
3659 (iro == iro_Block) ) /* Flags tested local. */
3660 n = equivalent_node (n);
3662 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3664 /* Common Subexpression Elimination.
3666 * Checks whether n is already available.
3667 * The block input is used to distinguish different subexpressions. Right
3668 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3669 * subexpressions within a block.
3672 n = identify_cons (current_ir_graph->value_table, n);
3675 edges_node_deleted(oldn, current_ir_graph);
3677 /* We found an existing, better node, so we can deallocate the old node. */
3678 irg_kill_node(current_ir_graph, oldn);
3682 /* Some more constant expression evaluation that does not allow to
3684 iro = get_irn_opcode(n);
3685 if (get_opt_constant_folding() ||
3686 (iro == iro_Cond) ||
3687 (iro == iro_Proj) ||
3688 (iro == iro_Sel)) /* Flags tested local. */
3689 n = transform_node (n);
3691 /* Remove nodes with dead (Bad) input.
3692 Run always for transformation induced Bads. */
3695 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3696 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3697 n = identify_remember (current_ir_graph->value_table, n);
3705 * These optimizations never deallocate nodes (in place). This can cause dead
3706 * nodes lying on the obstack. Remove these by a dead node elimination,
3707 * i.e., a copying garbage collection.
3709 ir_node *optimize_in_place_2(ir_node *n)
3713 opcode iro = get_irn_opcode(n);
3715 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3717 /* constant expression evaluation / constant folding */
3718 if (get_opt_constant_folding()) {
3719 /* neither constants nor Tuple values can be evaluated */
3720 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3721 /* try to evaluate */
3722 tv = computed_value(n);
3723 if (tv != tarval_bad) {
3724 /* evaluation was successful -- replace the node. */
3725 ir_type *old_tp = get_irn_type(n);
3726 int i, arity = get_irn_arity(n);
3729 * Try to recover the type of the new expression.
3731 for (i = 0; i < arity && !old_tp; ++i)
3732 old_tp = get_irn_type(get_irn_n(n, i));
3734 n = new_Const(get_tarval_mode(tv), tv);
3736 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3737 set_Const_type(n, old_tp);
3739 DBG_OPT_CSTEVAL(oldn, n);
3745 /* remove unnecessary nodes */
3746 if (get_opt_constant_folding() ||
3747 (iro == iro_Phi) || /* always optimize these nodes. */
3748 (iro == iro_Id) || /* ... */
3749 (iro == iro_Proj) || /* ... */
3750 (iro == iro_Block) ) /* Flags tested local. */
3751 n = equivalent_node(n);
3753 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3755 /** common subexpression elimination **/
3756 /* Checks whether n is already available. */
3757 /* The block input is used to distinguish different subexpressions. Right
3758 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3759 subexpressions within a block. */
3760 if (get_opt_cse()) {
3761 n = identify(current_ir_graph->value_table, n);
3764 /* Some more constant expression evaluation. */
3765 iro = get_irn_opcode(n);
3766 if (get_opt_constant_folding() ||
3767 (iro == iro_Cond) ||
3768 (iro == iro_Proj) ||
3769 (iro == iro_Sel)) /* Flags tested local. */
3770 n = transform_node(n);
3772 /* Remove nodes with dead (Bad) input.
3773 Run always for transformation induced Bads. */
3776 /* Now we can verify the node, as it has no dead inputs any more. */
3779 /* Now we have a legal, useful node. Enter it in hash table for cse.
3780 Blocks should be unique anyways. (Except the successor of start:
3781 is cse with the start block!) */
3782 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3783 n = identify_remember(current_ir_graph->value_table, n);
3789 * Wrapper for external use, set proper status bits after optimization.
3791 ir_node *optimize_in_place(ir_node *n)
3793 /* Handle graph state */
3794 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3796 if (get_opt_global_cse())
3797 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3798 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3799 set_irg_outs_inconsistent(current_ir_graph);
3801 /* FIXME: Maybe we could also test whether optimizing the node can
3802 change the control graph. */
3803 set_irg_doms_inconsistent(current_ir_graph);
3804 return optimize_in_place_2 (n);
3808 * Sets the default operation for an ir_ops.
3810 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3812 ops = firm_set_default_computed_value(code, ops);
3813 ops = firm_set_default_equivalent_node(code, ops);
3814 ops = firm_set_default_transform_node(code, ops);
3815 ops = firm_set_default_node_cmp_attr(code, ops);
3816 ops = firm_set_default_get_type(code, ops);
3817 ops = firm_set_default_get_type_attr(code, ops);
3818 ops = firm_set_default_get_entity_attr(code, ops);