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);
561 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
565 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
567 static tarval *computed_value_Proj(ir_node *n)
569 ir_node *a = get_Proj_pred(n);
572 switch (get_irn_opcode(a)) {
574 return computed_value_Proj_Cmp(n);
577 /* compute either the Div or the Mod part */
578 proj_nr = get_Proj_proj(n);
579 if (proj_nr == pn_DivMod_res_div)
580 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
581 else if (proj_nr == pn_DivMod_res_mod)
582 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
586 if (get_Proj_proj(n) == pn_Div_res)
587 return computed_value(a);
591 if (get_Proj_proj(n) == pn_Mod_res)
592 return computed_value(a);
602 * calculate the value of a Mux: can be evaluated, if the
603 * sel and the right input are known
605 static tarval *computed_value_Mux(ir_node *n)
607 ir_node *sel = get_Mux_sel(n);
608 tarval *ts = value_of(sel);
610 if (ts == get_tarval_b_true()) {
611 ir_node *v = get_Mux_true(n);
614 else if (ts == get_tarval_b_false()) {
615 ir_node *v = get_Mux_false(n);
622 * Calculate the value of a Psi: can be evaluated, if a condition is true
623 * and all previous conditions are false. If all conditions are false
624 * we evaluate to the default one.
626 static tarval *computed_value_Psi(ir_node *n)
632 * calculate the value of a Confirm: can be evaluated,
633 * if it has the form Confirm(x, '=', Const).
635 static tarval *computed_value_Confirm(ir_node *n)
637 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
638 value_of(get_Confirm_bound(n)) : tarval_bad;
642 * If the parameter n can be computed, return its value, else tarval_bad.
643 * Performs constant folding.
645 * @param n The node this should be evaluated
647 tarval *computed_value(ir_node *n)
649 if (n->op->ops.computed_value)
650 return n->op->ops.computed_value(n);
655 * set the default computed_value evaluator in an ir_op_ops.
657 * @param code the opcode for the default operation
658 * @param ops the operations initialized
663 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
667 ops->computed_value = computed_value_##a; \
705 * Returns a equivalent block for another block.
706 * If the block has only one predecessor, this is
707 * the equivalent one. If the only predecessor of a block is
708 * the block itself, this is a dead block.
710 * If both predecessors of a block are the branches of a binary
711 * Cond, the equivalent block is Cond's block.
713 * If all predecessors of a block are bad or lies in a dead
714 * block, the current block is dead as well.
716 * Note, that blocks are NEVER turned into Bad's, instead
717 * the dead_block flag is set. So, never test for is_Bad(block),
718 * always use is_dead_Block(block).
720 static ir_node *equivalent_node_Block(ir_node *n)
723 int n_preds = get_Block_n_cfgpreds(n);
725 /* The Block constructor does not call optimize, but mature_immBlock
726 calls the optimization. */
727 assert(get_Block_matured(n));
729 /* Straightening: a single entry Block following a single exit Block
730 can be merged, if it is not the Start block. */
731 /* !!! Beware, all Phi-nodes of n must have been optimized away.
732 This should be true, as the block is matured before optimize is called.
733 But what about Phi-cycles with the Phi0/Id that could not be resolved?
734 Remaining Phi nodes are just Ids. */
735 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
736 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
737 if (predblock == oldn) {
738 /* Jmp jumps into the block it is in -- deal self cycle. */
739 n = set_Block_dead(n);
740 DBG_OPT_DEAD_BLOCK(oldn, n);
741 } else if (get_opt_control_flow_straightening()) {
743 DBG_OPT_STG(oldn, n);
746 else if ((n_preds == 1) &&
747 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
748 ir_node *predblock = get_Block_cfgpred_block(n, 0);
749 if (predblock == oldn) {
750 /* Jmp jumps into the block it is in -- deal self cycle. */
751 n = set_Block_dead(n);
752 DBG_OPT_DEAD_BLOCK(oldn, n);
755 else if ((n_preds == 2) &&
756 (get_opt_control_flow_weak_simplification())) {
757 /* Test whether Cond jumps twice to this block
758 * The more general case which more than 2 predecessors is handles
759 * in optimize_cf(), we handle only this special case for speed here.
761 ir_node *a = get_Block_cfgpred(n, 0);
762 ir_node *b = get_Block_cfgpred(n, 1);
764 if ((get_irn_op(a) == op_Proj) &&
765 (get_irn_op(b) == op_Proj) &&
766 (get_Proj_pred(a) == get_Proj_pred(b)) &&
767 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
768 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
769 /* Also a single entry Block following a single exit Block. Phis have
770 twice the same operand and will be optimized away. */
771 n = get_nodes_block(get_Proj_pred(a));
772 DBG_OPT_IFSIM1(oldn, a, b, n);
775 else if (get_opt_unreachable_code() &&
776 (n != current_ir_graph->start_block) &&
777 (n != current_ir_graph->end_block) ) {
780 /* If all inputs are dead, this block is dead too, except if it is
781 the start or end block. This is one step of unreachable code
783 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
784 ir_node *pred = get_Block_cfgpred(n, i);
787 if (is_Bad(pred)) continue;
788 pred_blk = get_nodes_block(skip_Proj(pred));
790 if (is_Block_dead(pred_blk)) continue;
793 /* really found a living input */
798 n = set_Block_dead(n);
799 DBG_OPT_DEAD_BLOCK(oldn, n);
807 * Returns a equivalent node for a Jmp, a Bad :-)
808 * Of course this only happens if the Block of the Jmp is Bad.
810 static ir_node *equivalent_node_Jmp(ir_node *n)
812 /* unreachable code elimination */
813 if (is_Block_dead(get_nodes_block(n)))
819 /* Same for op_Raise */
820 #define equivalent_node_Raise equivalent_node_Jmp
823 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
824 See transform_node_Proj_Cond(). */
827 * optimize operations that are commutative and have neutral 0,
828 * so a op 0 = 0 op a = a.
830 static ir_node *equivalent_node_neutral_zero(ir_node *n)
834 ir_node *a = get_binop_left(n);
835 ir_node *b = get_binop_right(n);
840 /* After running compute_node there is only one constant predecessor.
841 Find this predecessors value and remember the other node: */
842 if ((tv = value_of(a)) != tarval_bad) {
844 } else if ((tv = value_of(b)) != tarval_bad) {
849 /* If this predecessors constant value is zero, the operation is
850 * unnecessary. Remove it.
852 * Beware: If n is a Add, the mode of on and n might be different
853 * which happens in this rare construction: NULL + 3.
854 * Then, a Conv would be needed which we cannot include here.
856 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
857 if (get_irn_mode(on) == get_irn_mode(n)) {
860 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
867 #define equivalent_node_Eor equivalent_node_neutral_zero
870 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
872 * The second one looks strange, but this construct
873 * is used heavily in the LCC sources :-).
875 * Beware: The Mode of an Add may be different than the mode of its
876 * predecessors, so we could not return a predecessors in all cases.
878 static ir_node *equivalent_node_Add(ir_node *n)
881 ir_node *left, *right;
883 n = equivalent_node_neutral_zero(n);
887 left = get_Add_left(n);
888 right = get_Add_right(n);
890 if (get_irn_op(left) == op_Sub) {
891 if (get_Sub_right(left) == right) {
894 n = get_Sub_left(left);
895 if (get_irn_mode(oldn) == get_irn_mode(n)) {
896 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
901 if (get_irn_op(right) == op_Sub) {
902 if (get_Sub_right(right) == left) {
905 n = get_Sub_left(right);
906 if (get_irn_mode(oldn) == get_irn_mode(n)) {
907 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
916 * optimize operations that are not commutative but have neutral 0 on left,
919 static ir_node *equivalent_node_left_zero(ir_node *n)
923 ir_node *a = get_binop_left(n);
924 ir_node *b = get_binop_right(n);
926 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
935 #define equivalent_node_Shl equivalent_node_left_zero
936 #define equivalent_node_Shr equivalent_node_left_zero
937 #define equivalent_node_Shrs equivalent_node_left_zero
938 #define equivalent_node_Rot equivalent_node_left_zero
941 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
943 * The second one looks strange, but this construct
944 * is used heavily in the LCC sources :-).
946 * Beware: The Mode of a Sub may be different than the mode of its
947 * predecessors, so we could not return a predecessors in all cases.
949 static ir_node *equivalent_node_Sub(ir_node *n)
953 ir_node *a = get_Sub_left(n);
954 ir_node *b = get_Sub_right(n);
956 /* Beware: modes might be different */
957 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
958 if (get_irn_mode(n) == get_irn_mode(a)) {
961 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
964 else if (get_irn_op(a) == op_Add) {
965 ir_mode *mode = get_irn_mode(n);
967 if (mode_wrap_around(mode)) {
968 ir_node *left = get_Add_left(a);
969 ir_node *right = get_Add_right(a);
972 if (get_irn_mode(n) == get_irn_mode(right)) {
974 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
977 else if (right == b) {
978 if (get_irn_mode(n) == get_irn_mode(left)) {
980 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
991 * Optimize an "idempotent unary op", ie op(op(n)) = n.
994 * -(-a) == a, but might overflow two times.
995 * We handle it anyway here but the better way would be a
996 * flag. This would be needed for Pascal for instance.
998 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1001 ir_node *pred = get_unop_op(n);
1003 /* optimize symmetric unop */
1004 if (get_irn_op(pred) == get_irn_op(n)) {
1005 n = get_unop_op(pred);
1006 DBG_OPT_ALGSIM2(oldn, pred, n);
1011 /* Not(Not(x)) == x */
1012 #define equivalent_node_Not equivalent_node_idempotent_unop
1014 /* --x == x */ /* ??? Is this possible or can --x raise an
1015 out of bounds exception if min =! max? */
1016 #define equivalent_node_Minus equivalent_node_idempotent_unop
1019 * Optimize a * 1 = 1 * a = a.
1021 static ir_node *equivalent_node_Mul(ir_node *n)
1025 ir_node *a = get_Mul_left(n);
1026 ir_node *b = get_Mul_right(n);
1028 /* Mul is commutative and has again an other neutral element. */
1029 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1031 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1032 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1034 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1040 * Optimize a / 1 = a.
1042 static ir_node *equivalent_node_Div(ir_node *n)
1044 ir_node *a = get_Div_left(n);
1045 ir_node *b = get_Div_right(n);
1047 /* Div is not commutative. */
1048 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1049 /* Turn Div into a tuple (mem, bad, a) */
1050 ir_node *mem = get_Div_mem(n);
1051 turn_into_tuple(n, pn_Div_max);
1052 set_Tuple_pred(n, pn_Div_M, mem);
1053 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1054 set_Tuple_pred(n, pn_Div_res, a);
1060 * Optimize a / 1 = a.
1062 static ir_node *equivalent_node_DivMod(ir_node *n)
1064 ir_node *a = get_DivMod_left(n);
1065 ir_node *b = get_DivMod_right(n);
1067 /* Div is not commutative. */
1068 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1069 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1070 ir_node *mem = get_Div_mem(n);
1071 ir_mode *mode = get_irn_mode(b);
1073 turn_into_tuple(n, pn_DivMod_max);
1074 set_Tuple_pred(n, pn_DivMod_M, mem);
1075 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1076 set_Tuple_pred(n, pn_DivMod_res_div, a);
1077 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1083 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1085 static ir_node *equivalent_node_Or(ir_node *n)
1089 ir_node *a = get_Or_left(n);
1090 ir_node *b = get_Or_right(n);
1093 n = a; /* Or has it's own neutral element */
1094 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1095 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1097 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1098 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1100 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1107 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1109 static ir_node *equivalent_node_And(ir_node *n)
1113 ir_node *a = get_And_left(n);
1114 ir_node *b = get_And_right(n);
1117 n = a; /* And has it's own neutral element */
1118 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1119 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1121 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1122 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1124 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1130 * Try to remove useless Conv's:
1132 static ir_node *equivalent_node_Conv(ir_node *n)
1135 ir_node *a = get_Conv_op(n);
1138 ir_mode *n_mode = get_irn_mode(n);
1139 ir_mode *a_mode = get_irn_mode(a);
1141 if (n_mode == a_mode) { /* No Conv necessary */
1143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1144 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1148 n_mode = get_irn_mode(n);
1149 b_mode = get_irn_mode(b);
1151 if (n_mode == b_mode) {
1152 if (n_mode == mode_b) {
1153 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1154 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1156 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1157 if (smaller_mode(b_mode, a_mode)){
1158 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1159 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1168 * A Cast may be removed if the type of the previous node
1169 * is already the type of the Cast.
1171 static ir_node *equivalent_node_Cast(ir_node *n) {
1173 ir_node *pred = get_Cast_op(n);
1175 if (get_irn_type(pred) == get_Cast_type(n)) {
1177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1183 Several optimizations:
1184 - no Phi in start block.
1185 - remove Id operators that are inputs to Phi
1186 - fold Phi-nodes, iff they have only one predecessor except
1189 static ir_node *equivalent_node_Phi(ir_node *n)
1194 ir_node *block = NULL; /* to shutup gcc */
1195 ir_node *first_val = NULL; /* to shutup gcc */
1197 if (!get_opt_normalize()) return n;
1199 n_preds = get_Phi_n_preds(n);
1201 block = get_nodes_block(n);
1202 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1203 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1204 if ((is_Block_dead(block)) || /* Control dead */
1205 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1206 return new_Bad(); /* in the Start Block. */
1208 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1210 /* If the Block has a Bad pred, we also have one. */
1211 for (i = 0; i < n_preds; ++i)
1212 if (is_Bad(get_Block_cfgpred(block, i)))
1213 set_Phi_pred(n, i, new_Bad());
1215 /* Find first non-self-referencing input */
1216 for (i = 0; i < n_preds; ++i) {
1217 first_val = get_Phi_pred(n, i);
1218 if ( (first_val != n) /* not self pointer */
1220 && (! is_Bad(first_val))
1222 ) { /* value not dead */
1223 break; /* then found first value. */
1228 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1232 /* search for rest of inputs, determine if any of these
1233 are non-self-referencing */
1234 while (++i < n_preds) {
1235 ir_node *scnd_val = get_Phi_pred(n, i);
1236 if ( (scnd_val != n)
1237 && (scnd_val != first_val)
1239 && (! is_Bad(scnd_val))
1247 /* Fold, if no multiple distinct non-self-referencing inputs */
1249 DBG_OPT_PHI(oldn, n);
1255 Several optimizations:
1256 - no Sync in start block.
1257 - fold Sync-nodes, iff they have only one predecessor except
1259 @fixme: are there loop's in Sync's
1261 static ir_node *equivalent_node_Sync(ir_node *n)
1266 ir_node *first_val = NULL; /* to shutup gcc */
1268 if (!get_opt_normalize()) return n;
1270 n_preds = get_Sync_n_preds(n);
1272 /* Find first non-self-referencing input */
1273 for (i = 0; i < n_preds; ++i) {
1274 first_val = get_Sync_pred(n, i);
1275 if ((first_val != n) /* not self pointer */ &&
1276 (! is_Bad(first_val))
1277 ) { /* value not dead */
1278 break; /* then found first value. */
1283 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1286 /* search the rest of inputs, determine if any of these
1287 are non-self-referencing */
1288 while (++i < n_preds) {
1289 ir_node *scnd_val = get_Sync_pred(n, i);
1290 if ((scnd_val != n) &&
1291 (scnd_val != first_val) &&
1292 (! is_Bad(scnd_val))
1298 /* Fold, if no multiple distinct non-self-referencing inputs */
1300 DBG_OPT_SYNC(oldn, n);
1306 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1307 * ProjX(Load) and ProjX(Store)
1309 static ir_node *equivalent_node_Proj(ir_node *n)
1313 ir_node *a = get_Proj_pred(n);
1315 if ( get_irn_op(a) == op_Tuple) {
1316 /* Remove the Tuple/Proj combination. */
1317 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1318 n = get_Tuple_pred(a, get_Proj_proj(n));
1319 DBG_OPT_TUPLE(oldn, a, n);
1321 assert(0); /* This should not happen! */
1325 else if (get_irn_mode(n) == mode_X) {
1326 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1327 /* Remove dead control flow -- early gigo(). */
1330 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1331 ir_op *op = get_irn_op(a);
1333 if (op == op_Load || op == op_Store) {
1334 /* get the load/store address */
1335 ir_node *addr = get_irn_n(a, 1);
1336 if (value_not_null(addr)) {
1337 /* this node may float if it did not depend on a Confirm */
1338 set_irn_pinned(a, op_pin_state_floats);
1352 static ir_node *equivalent_node_Id(ir_node *n)
1358 } while (get_irn_op(n) == op_Id);
1360 DBG_OPT_ID(oldn, n);
1367 static ir_node *equivalent_node_Mux(ir_node *n)
1369 ir_node *oldn = n, *sel = get_Mux_sel(n);
1370 tarval *ts = value_of(sel);
1372 /* Mux(true, f, t) == t */
1373 if (ts == tarval_b_true) {
1374 n = get_Mux_true(n);
1375 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1377 /* Mux(false, f, t) == f */
1378 else if (ts == tarval_b_false) {
1379 n = get_Mux_false(n);
1380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1382 /* Mux(v, x, x) == x */
1383 else if (get_Mux_false(n) == get_Mux_true(n)) {
1384 n = get_Mux_true(n);
1385 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1387 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1388 ir_node *cmp = get_Proj_pred(sel);
1389 long proj_nr = get_Proj_proj(sel);
1390 ir_node *b = get_Mux_false(n);
1391 ir_node *a = get_Mux_true(n);
1394 * Note: normalization puts the constant on the right site,
1395 * so we check only one case.
1397 * Note further that these optimization work even for floating point
1398 * with NaN's because -NaN == NaN.
1399 * However, if +0 and -0 is handled differently, we cannot use the first one.
1401 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1402 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1403 /* Mux(a CMP 0, X, a) */
1404 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1405 /* Mux(a CMP 0, -a, a) */
1406 if (proj_nr == pn_Cmp_Eq) {
1407 /* Mux(a == 0, -a, a) ==> -a */
1409 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1411 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1412 /* Mux(a != 0, -a, a) ==> a */
1414 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1417 else if (classify_Const(b) == CNST_NULL) {
1418 /* Mux(a CMP 0, 0, a) */
1419 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1420 /* Mux(a != 0, 0, a) ==> a */
1422 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1424 else if (proj_nr == pn_Cmp_Eq) {
1425 /* Mux(a == 0, 0, a) ==> 0 */
1427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1437 * Returns a equivalent node of a Psi: if a condition is true
1438 * and all previous conditions are false we know its value.
1439 * If all conditions are false its value is the default one.
1441 static ir_node *equivalent_node_Psi(ir_node *n) {
1446 * Optimize -a CMP -b into b CMP a.
1447 * This works only for for modes where unary Minus
1449 * Note that two-complement integers can Overflow
1450 * so it will NOT work.
1452 static ir_node *equivalent_node_Cmp(ir_node *n)
1454 ir_node *left = get_Cmp_left(n);
1455 ir_node *right = get_Cmp_right(n);
1457 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1458 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1459 left = get_Minus_op(left);
1460 right = get_Minus_op(right);
1461 set_Cmp_left(n, right);
1462 set_Cmp_right(n, left);
1468 * Remove Confirm nodes if setting is on.
1469 * Replace Confirms(x, '=', Constlike) by Constlike.
1471 static ir_node *equivalent_node_Confirm(ir_node *n)
1473 ir_node *pred = get_Confirm_value(n);
1474 pn_Cmp pnc = get_Confirm_cmp(n);
1476 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1478 * rare case: two identical Confirms one after another,
1479 * replace the second one with the first.
1483 if (pnc == pn_Cmp_Eq) {
1484 ir_node *bound = get_Confirm_bound(n);
1487 * Optimize a rare case:
1488 * Confirm(x, '=', Constlike) ==> Constlike
1490 if (is_irn_constlike(bound)) {
1491 DBG_OPT_CONFIRM(n, bound);
1495 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1499 * Optimize CopyB(mem, x, x) into a Nop
1501 static ir_node *equivalent_node_CopyB(ir_node *n)
1503 ir_node *a = get_CopyB_dst(n);
1504 ir_node *b = get_CopyB_src(n);
1507 /* Turn CopyB into a tuple (mem, bad, bad) */
1508 ir_node *mem = get_CopyB_mem(n);
1509 turn_into_tuple(n, pn_CopyB_max);
1510 set_Tuple_pred(n, pn_CopyB_M, mem);
1511 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1512 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1518 * Optimize Bounds(idx, idx, upper) into idx.
1520 static ir_node *equivalent_node_Bound(ir_node *n)
1522 ir_node *idx = get_Bound_index(n);
1523 ir_node *lower = get_Bound_lower(n);
1526 /* By definition lower < upper, so if idx == lower -->
1527 lower <= idx && idx < upper */
1529 /* Turn Bound into a tuple (mem, bad, idx) */
1533 ir_node *pred = skip_Proj(idx);
1535 if (get_irn_op(pred) == op_Bound) {
1537 * idx was Bounds_check previously, it is still valid if
1538 * lower <= pred_lower && pred_upper <= upper.
1540 ir_node *upper = get_Bound_upper(n);
1541 if (get_Bound_lower(pred) == lower &&
1542 get_Bound_upper(pred) == upper) {
1544 * One could expect that we simple return the previous
1545 * Bound here. However, this would be wrong, as we could
1546 * add an exception Proj to a new location than.
1547 * So, we must turn in into a tuple
1554 /* Turn Bound into a tuple (mem, bad, idx) */
1555 ir_node *mem = get_Bound_mem(n);
1556 turn_into_tuple(n, pn_Bound_max);
1557 set_Tuple_pred(n, pn_Bound_M_regular, mem);
1558 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1559 set_Tuple_pred(n, pn_Bound_res, idx);
1560 set_Tuple_pred(n, pn_Bound_M_except, mem);
1566 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1567 * perform no actual computation, as, e.g., the Id nodes. It does not create
1568 * new nodes. It is therefore safe to free n if the node returned is not n.
1569 * If a node returns a Tuple we can not just skip it. If the size of the
1570 * in array fits, we transform n into a tuple (e.g., Div).
1573 equivalent_node(ir_node *n)
1575 if (n->op->ops.equivalent_node)
1576 return n->op->ops.equivalent_node(n);
1581 * sets the default equivalent node operation for an ir_op_ops.
1583 * @param code the opcode for the default operation
1584 * @param ops the operations initialized
1589 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1593 ops->equivalent_node = equivalent_node_##a; \
1635 * Do node specific optimizations of nodes predecessors.
1638 optimize_preds(ir_node *n) {
1639 ir_node *a = NULL, *b = NULL;
1641 /* get the operands we will work on for simple cases. */
1643 a = get_binop_left(n);
1644 b = get_binop_right(n);
1645 } else if (is_unop(n)) {
1649 switch (get_irn_opcode(n)) {
1652 /* We don't want Cast as input to Cmp. */
1653 if (get_irn_op(a) == op_Cast) {
1657 if (get_irn_op(b) == op_Cast) {
1659 set_Cmp_right(n, b);
1668 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1669 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1670 * If possible, remove the Conv's.
1672 static ir_node *transform_node_AddSub(ir_node *n)
1674 ir_mode *mode = get_irn_mode(n);
1676 if (mode_is_reference(mode)) {
1677 ir_node *left = get_binop_left(n);
1678 ir_node *right = get_binop_right(n);
1679 int ref_bits = get_mode_size_bits(mode);
1681 if (get_irn_op(left) == op_Conv) {
1682 ir_mode *mode = get_irn_mode(left);
1683 int bits = get_mode_size_bits(mode);
1685 if (ref_bits == bits &&
1686 mode_is_int(mode) &&
1687 get_mode_arithmetic(mode) == irma_twos_complement) {
1688 ir_node *pre = get_Conv_op(left);
1689 ir_mode *pre_mode = get_irn_mode(pre);
1691 if (mode_is_int(pre_mode) &&
1692 get_mode_size_bits(pre_mode) == bits &&
1693 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1694 /* ok, this conv just changes to sign, moreover the calculation
1695 * is done with same number of bits as our address mode, so
1696 * we can ignore the conv as address calculation can be viewed
1697 * as either signed or unsigned
1699 set_binop_left(n, pre);
1704 if (get_irn_op(right) == op_Conv) {
1705 ir_mode *mode = get_irn_mode(right);
1706 int bits = get_mode_size_bits(mode);
1708 if (ref_bits == bits &&
1709 mode_is_int(mode) &&
1710 get_mode_arithmetic(mode) == irma_twos_complement) {
1711 ir_node *pre = get_Conv_op(right);
1712 ir_mode *pre_mode = get_irn_mode(pre);
1714 if (mode_is_int(pre_mode) &&
1715 get_mode_size_bits(pre_mode) == bits &&
1716 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1717 /* ok, this conv just changes to sign, moreover the calculation
1718 * is done with same number of bits as our address mode, so
1719 * we can ignore the conv as address calculation can be viewed
1720 * as either signed or unsigned
1722 set_binop_right(n, pre);
1731 * Do the AddSub optimization, then Transform
1732 * Add(a,a) -> Mul(a, 2)
1733 * Add(Mul(a, x), a) -> Mul(a, x+1)
1734 * if the mode is integer or float.
1735 * Transform Add(a,-b) into Sub(a,b).
1736 * Reassociation might fold this further.
1738 static ir_node *transform_node_Add(ir_node *n)
1743 n = transform_node_AddSub(n);
1745 mode = get_irn_mode(n);
1746 if (mode_is_num(mode)) {
1747 ir_node *a = get_Add_left(n);
1748 ir_node *b = get_Add_right(n);
1751 ir_node *block = get_irn_n(n, -1);
1754 get_irn_dbg_info(n),
1758 new_r_Const_long(current_ir_graph, block, mode, 2),
1760 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1762 else if (get_irn_op(a) == op_Minus) {
1764 get_irn_dbg_info(n),
1770 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1772 else if (get_irn_op(b) == op_Minus) {
1774 get_irn_dbg_info(n),
1780 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1782 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1783 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1784 ir_node *ma = get_Mul_left(a);
1785 ir_node *mb = get_Mul_right(a);
1788 ir_node *blk = get_irn_n(n, -1);
1790 get_irn_dbg_info(n), current_ir_graph, blk,
1793 get_irn_dbg_info(n), current_ir_graph, blk,
1795 new_r_Const_long(current_ir_graph, blk, mode, 1),
1798 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1801 ir_node *blk = get_irn_n(n, -1);
1803 get_irn_dbg_info(n), current_ir_graph, blk,
1806 get_irn_dbg_info(n), current_ir_graph, blk,
1808 new_r_Const_long(current_ir_graph, blk, mode, 1),
1811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1814 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1815 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1816 ir_node *ma = get_Mul_left(b);
1817 ir_node *mb = get_Mul_right(b);
1820 ir_node *blk = get_irn_n(n, -1);
1822 get_irn_dbg_info(n), current_ir_graph, blk,
1825 get_irn_dbg_info(n), current_ir_graph, blk,
1827 new_r_Const_long(current_ir_graph, blk, mode, 1),
1830 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1833 ir_node *blk = get_irn_n(n, -1);
1835 get_irn_dbg_info(n), current_ir_graph, blk,
1838 get_irn_dbg_info(n), current_ir_graph, blk,
1840 new_r_Const_long(current_ir_graph, blk, mode, 1),
1843 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1851 * Do the AddSub optimization, then Transform
1852 * Sub(0,a) -> Minus(a)
1853 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1855 static ir_node *transform_node_Sub(ir_node *n)
1861 n = transform_node_AddSub(n);
1863 mode = get_irn_mode(n);
1864 a = get_Sub_left(n);
1865 b = get_Sub_right(n);
1866 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1868 get_irn_dbg_info(n),
1873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1875 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1876 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1877 ir_node *ma = get_Mul_left(a);
1878 ir_node *mb = get_Mul_right(a);
1881 ir_node *blk = get_irn_n(n, -1);
1883 get_irn_dbg_info(n),
1884 current_ir_graph, blk,
1887 get_irn_dbg_info(n),
1888 current_ir_graph, blk,
1890 new_r_Const_long(current_ir_graph, blk, mode, 1),
1893 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1896 ir_node *blk = get_irn_n(n, -1);
1898 get_irn_dbg_info(n),
1899 current_ir_graph, blk,
1902 get_irn_dbg_info(n),
1903 current_ir_graph, blk,
1905 new_r_Const_long(current_ir_graph, blk, mode, 1),
1908 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1916 * Transform Mul(a,-1) into -a.
1917 * Do architecture dependent optimizations on Mul nodes
1919 static ir_node *transform_node_Mul(ir_node *n) {
1921 ir_mode *mode = get_irn_mode(n);
1923 if (mode_is_signed(mode)) {
1925 ir_node *a = get_Mul_left(n);
1926 ir_node *b = get_Mul_right(n);
1928 if (value_of(a) == get_mode_minus_one(mode))
1930 else if (value_of(b) == get_mode_minus_one(mode))
1933 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1934 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1938 return arch_dep_replace_mul_with_shifts(n);
1942 * transform a Div Node
1944 static ir_node *transform_node_Div(ir_node *n)
1946 tarval *tv = value_of(n);
1949 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1951 if (tv != tarval_bad) {
1952 value = new_Const(get_tarval_mode(tv), tv);
1954 DBG_OPT_CSTEVAL(n, value);
1956 else /* Try architecture dependent optimization */
1957 value = arch_dep_replace_div_by_const(n);
1960 /* Turn Div into a tuple (mem, bad, value) */
1961 ir_node *mem = get_Div_mem(n);
1963 turn_into_tuple(n, pn_Div_max);
1964 set_Tuple_pred(n, pn_Div_M, mem);
1965 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1966 set_Tuple_pred(n, pn_Div_res, value);
1972 * transform a Mod node
1974 static ir_node *transform_node_Mod(ir_node *n)
1976 tarval *tv = value_of(n);
1979 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1981 if (tv != tarval_bad) {
1982 value = new_Const(get_tarval_mode(tv), tv);
1984 DBG_OPT_CSTEVAL(n, value);
1986 else /* Try architecture dependent optimization */
1987 value = arch_dep_replace_mod_by_const(n);
1990 /* Turn Mod into a tuple (mem, bad, value) */
1991 ir_node *mem = get_Mod_mem(n);
1993 turn_into_tuple(n, pn_Mod_max);
1994 set_Tuple_pred(n, pn_Mod_M, mem);
1995 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1996 set_Tuple_pred(n, pn_Mod_res, value);
2002 * transform a DivMod node
2004 static ir_node *transform_node_DivMod(ir_node *n)
2008 ir_node *a = get_DivMod_left(n);
2009 ir_node *b = get_DivMod_right(n);
2010 ir_mode *mode = get_irn_mode(a);
2011 tarval *ta = value_of(a);
2012 tarval *tb = value_of(b);
2014 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2017 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2019 if (tb != tarval_bad) {
2020 if (tb == get_mode_one(get_tarval_mode(tb))) {
2021 b = new_Const (mode, get_mode_null(mode));
2024 DBG_OPT_CSTEVAL(n, b);
2026 else if (ta != tarval_bad) {
2027 tarval *resa, *resb;
2028 resa = tarval_div (ta, tb);
2029 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2030 Jmp for X result!? */
2031 resb = tarval_mod (ta, tb);
2032 if (resb == tarval_bad) return n; /* Causes exception! */
2033 a = new_Const (mode, resa);
2034 b = new_Const (mode, resb);
2037 DBG_OPT_CSTEVAL(n, a);
2038 DBG_OPT_CSTEVAL(n, b);
2040 else { /* Try architecture dependent optimization */
2041 arch_dep_replace_divmod_by_const(&a, &b, n);
2042 evaluated = a != NULL;
2044 } else if (ta == get_mode_null(mode)) {
2045 /* 0 / non-Const = 0 */
2050 if (evaluated) { /* replace by tuple */
2051 ir_node *mem = get_DivMod_mem(n);
2052 turn_into_tuple(n, pn_DivMod_max);
2053 set_Tuple_pred(n, pn_DivMod_M, mem);
2054 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2055 set_Tuple_pred(n, pn_DivMod_res_div, a);
2056 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2063 * Optimize Abs(x) into x if x is Confirmed >= 0
2064 * Optimize Abs(x) into -x if x is Confirmed <= 0
2066 static ir_node *transform_node_Abs(ir_node *n)
2069 ir_node *a = get_Abs_op(n);
2070 value_classify sign = classify_value_sign(a);
2072 if (sign == VALUE_NEGATIVE) {
2073 ir_mode *mode = get_irn_mode(n);
2076 * We can replace the Abs by -x here.
2077 * We even could add a new Confirm here.
2079 * Note that -x would create a new node, so we could
2080 * not run it in the equivalent_node() context.
2082 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2083 get_irn_n(n, -1), a, mode);
2085 DBG_OPT_CONFIRM(oldn, n);
2087 else if (sign == VALUE_POSITIVE) {
2088 /* n is positive, Abs is not needed */
2091 DBG_OPT_CONFIRM(oldn, n);
2098 * transform a Cond node
2100 static ir_node *transform_node_Cond(ir_node *n)
2102 /* Replace the Cond by a Jmp if it branches on a constant
2105 ir_node *a = get_Cond_selector(n);
2106 tarval *ta = value_of(a);
2108 /* we need block info which is not available in floating irgs */
2109 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2112 if ((ta != tarval_bad) &&
2113 (get_irn_mode(a) == mode_b) &&
2114 (get_opt_unreachable_code())) {
2115 /* It's a boolean Cond, branching on a boolean constant.
2116 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2117 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2118 turn_into_tuple(n, pn_Cond_max);
2119 if (ta == tarval_b_true) {
2120 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2121 set_Tuple_pred(n, pn_Cond_true, jmp);
2123 set_Tuple_pred(n, pn_Cond_false, jmp);
2124 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2126 /* We might generate an endless loop, so keep it alive. */
2127 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2135 static ir_node *transform_node_Eor(ir_node *n)
2138 ir_node *a = get_Eor_left(n);
2139 ir_node *b = get_Eor_right(n);
2140 ir_mode *mode = get_irn_mode(n);
2144 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2145 mode, get_mode_null(mode));
2146 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2148 else if ((mode == mode_b)
2149 && (get_irn_op(a) == op_Proj)
2150 && (get_irn_mode(a) == mode_b)
2151 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2152 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2153 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2154 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2155 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2157 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2159 else if ((mode == mode_b)
2160 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2161 /* The Eor is a Not. Replace it by a Not. */
2162 /* ????!!!Extend to bitfield 1111111. */
2163 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2165 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2172 * Transform a boolean Not.
2174 static ir_node *transform_node_Not(ir_node *n)
2177 ir_node *a = get_Not_op(n);
2179 if ( (get_irn_mode(n) == mode_b)
2180 && (get_irn_op(a) == op_Proj)
2181 && (get_irn_mode(a) == mode_b)
2182 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2183 /* We negate a Cmp. The Cmp has the negated result anyways! */
2184 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2185 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2186 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2193 * Transform a Cast_type(Const) into a new Const_type
2195 static ir_node *transform_node_Cast(ir_node *n) {
2197 ir_node *pred = get_Cast_op(n);
2198 ir_type *tp = get_irn_type(n);
2200 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2201 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2202 get_Const_tarval(pred), tp);
2203 DBG_OPT_CSTEVAL(oldn, n);
2204 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2205 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2206 get_SymConst_kind(pred), tp);
2207 DBG_OPT_CSTEVAL(oldn, n);
2214 * Transform a Proj(Div) with a non-zero value.
2215 * Removes the exceptions and routes the memory to the NoMem node.
2217 static ir_node *transform_node_Proj_Div(ir_node *proj)
2219 ir_node *n = get_Proj_pred(proj);
2220 ir_node *b = get_Div_right(n);
2223 if (value_not_zero(b)) {
2224 /* div(x, y) && y != 0 */
2225 proj_nr = get_Proj_proj(proj);
2227 /* this node may float if it did not depend on a Confirm */
2228 set_irn_pinned(n, op_pin_state_floats);
2230 if (proj_nr == pn_Div_X_except) {
2231 /* we found an exception handler, remove it */
2232 DBG_OPT_EXC_REM(proj);
2235 else if (proj_nr == pn_Div_M) {
2236 ir_node *res = get_Div_mem(n);
2237 /* the memory Proj can only be removed if we divide by a
2238 real constant, but the node never produce a new memory */
2239 if (value_of(b) != tarval_bad) {
2240 /* this is a Div by a const, we can remove the memory edge */
2241 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2250 * Transform a Proj(Mod) with a non-zero value.
2251 * Removes the exceptions and routes the memory to the NoMem node.
2253 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2255 ir_node *n = get_Proj_pred(proj);
2256 ir_node *b = get_Mod_right(n);
2259 if (value_not_zero(b)) {
2260 /* mod(x, y) && y != 0 */
2261 proj_nr = get_Proj_proj(proj);
2263 /* this node may float if it did not depend on a Confirm */
2264 set_irn_pinned(n, op_pin_state_floats);
2266 if (proj_nr == pn_Mod_X_except) {
2267 /* we found an exception handler, remove it */
2268 DBG_OPT_EXC_REM(proj);
2270 } else if (proj_nr == pn_Mod_M) {
2271 ir_node *res = get_Mod_mem(n);
2272 /* the memory Proj can only be removed if we divide by a
2273 real constant, but the node never produce a new memory */
2274 if (value_of(b) != tarval_bad) {
2275 /* this is a Mod by a const, we can remove the memory edge */
2276 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2280 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2281 /* a % a = 0 if a != 0 */
2282 ir_mode *mode = get_irn_mode(proj);
2283 ir_node *res = new_Const(mode, get_mode_null(mode));
2285 DBG_OPT_CSTEVAL(n, res);
2293 * Transform a Proj(DivMod) with a non-zero value.
2294 * Removes the exceptions and routes the memory to the NoMem node.
2296 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2298 ir_node *n = get_Proj_pred(proj);
2299 ir_node *b = get_DivMod_right(n);
2302 if (value_not_zero(b)) {
2303 /* DivMod(x, y) && y != 0 */
2304 proj_nr = get_Proj_proj(proj);
2306 /* this node may float if it did not depend on a Confirm */
2307 set_irn_pinned(n, op_pin_state_floats);
2309 if (proj_nr == pn_DivMod_X_except) {
2310 /* we found an exception handler, remove it */
2311 DBG_OPT_EXC_REM(proj);
2314 else if (proj_nr == pn_DivMod_M) {
2315 ir_node *res = get_DivMod_mem(n);
2316 /* the memory Proj can only be removed if we divide by a
2317 real constant, but the node never produce a new memory */
2318 if (value_of(b) != tarval_bad) {
2319 /* this is a DivMod by a const, we can remove the memory edge */
2320 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2324 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2325 /* a % a = 0 if a != 0 */
2326 ir_mode *mode = get_irn_mode(proj);
2327 ir_node *res = new_Const(mode, get_mode_null(mode));
2329 DBG_OPT_CSTEVAL(n, res);
2337 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2339 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2341 if (get_opt_unreachable_code()) {
2342 ir_node *n = get_Proj_pred(proj);
2343 ir_node *b = get_Cond_selector(n);
2345 if (mode_is_int(get_irn_mode(b))) {
2346 tarval *tb = value_of(b);
2348 if (tb != tarval_bad) {
2349 /* we have a constant switch */
2350 long num = get_Proj_proj(proj);
2352 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2353 if (get_tarval_long(tb) == num) {
2354 /* Do NOT create a jump here, or we will have 2 control flow ops
2355 * in a block. This case is optimized away in optimize_cf(). */
2359 /* this case will NEVER be taken, kill it */
2370 * Normalizes and optimizes Cmp nodes.
2372 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2374 if (get_opt_reassociation()) {
2375 ir_node *n = get_Proj_pred(proj);
2376 ir_node *left = get_Cmp_left(n);
2377 ir_node *right = get_Cmp_right(n);
2381 ir_mode *mode = NULL;
2382 long proj_nr = get_Proj_proj(proj);
2385 * First step: normalize the compare op
2386 * by placing the constant on the right site
2387 * or moving the lower address node to the left.
2388 * We ignore the case that both are constants
2389 * this case should be optimized away.
2391 if (get_irn_op(right) == op_Const)
2393 else if (get_irn_op(left) == op_Const) {
2398 proj_nr = get_inversed_pnc(proj_nr);
2401 else if (left > right) {
2407 proj_nr = get_inversed_pnc(proj_nr);
2412 * Second step: Try to reduce the magnitude
2413 * of a constant. This may help to generate better code
2414 * later and may help to normalize more compares.
2415 * Of course this is only possible for integer values.
2418 mode = get_irn_mode(c);
2419 tv = get_Const_tarval(c);
2421 if (tv != tarval_bad) {
2422 /* the following optimization is possible on modes without Overflow
2423 * on Unary Minus or on == and !=:
2424 * -a CMP c ==> a swap(CMP) -c
2426 * Beware: for two-complement Overflow may occur, so only == and != can
2427 * be optimized, see this:
2428 * -MININT < 0 =/=> MININT > 0 !!!
2430 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2431 (!mode_overflow_on_unary_Minus(mode) ||
2432 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2433 left = get_Minus_op(left);
2434 tv = tarval_sub(get_mode_null(mode), tv);
2436 proj_nr = get_inversed_pnc(proj_nr);
2440 /* for integer modes, we have more */
2441 if (mode_is_int(mode)) {
2442 /* Ne includes Unordered which is not possible on integers.
2443 * However, frontends often use this wrong, so fix it here */
2444 if (proj_nr & pn_Cmp_Uo) {
2445 proj_nr &= ~pn_Cmp_Uo;
2446 set_Proj_proj(proj, proj_nr);
2449 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2450 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2451 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2452 tv = tarval_sub(tv, get_mode_one(mode));
2454 proj_nr ^= pn_Cmp_Eq;
2457 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2458 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2459 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2460 tv = tarval_add(tv, get_mode_one(mode));
2462 proj_nr ^= pn_Cmp_Eq;
2466 /* the following reassociations work only for == and != */
2467 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2469 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2470 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2471 right = get_Sub_right(left);
2472 left = get_Sub_left(left);
2474 tv = value_of(right);
2478 if (tv != tarval_bad) {
2479 ir_op *op = get_irn_op(left);
2481 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2483 ir_node *c1 = get_Sub_right(left);
2484 tarval *tv2 = value_of(c1);
2486 if (tv2 != tarval_bad) {
2487 tv2 = tarval_add(tv, value_of(c1));
2489 if (tv2 != tarval_bad) {
2490 left = get_Sub_left(left);
2496 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2497 else if (op == op_Add) {
2498 ir_node *a_l = get_Add_left(left);
2499 ir_node *a_r = get_Add_right(left);
2503 if (get_irn_op(a_l) == op_Const) {
2505 tv2 = value_of(a_l);
2509 tv2 = value_of(a_r);
2512 if (tv2 != tarval_bad) {
2513 tv2 = tarval_sub(tv, tv2);
2515 if (tv2 != tarval_bad) {
2522 /* -a == c ==> a == -c, -a != c ==> a != -c */
2523 else if (op == op_Minus) {
2524 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2526 if (tv2 != tarval_bad) {
2527 left = get_Minus_op(left);
2534 /* the following reassociations work only for <= */
2535 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2536 if (tv != tarval_bad) {
2537 ir_op *op = get_irn_op(left);
2539 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2547 * optimization for AND:
2549 * And(x, C) == C ==> And(x, C) != 0
2550 * And(x, C) != C ==> And(X, C) == 0
2552 * if C is a single Bit constant.
2554 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2555 (get_irn_op(left) == op_And)) {
2556 if (is_single_bit_tarval(tv)) {
2557 /* check for Constant's match. We have check hare the tarvals,
2558 because our const might be changed */
2559 ir_node *la = get_And_left(left);
2560 ir_node *ra = get_And_right(left);
2561 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2562 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2563 /* fine: do the transformation */
2564 tv = get_mode_null(get_tarval_mode(tv));
2565 proj_nr ^= pn_Cmp_Leg;
2570 } /* tarval != bad */
2574 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2576 if (changed & 2) /* need a new Const */
2577 right = new_Const(mode, tv);
2579 /* create a new compare */
2580 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2583 set_Proj_pred(proj, n);
2584 set_Proj_proj(proj, proj_nr);
2591 * Does all optimizations on nodes that must be done on it's Proj's
2592 * because of creating new nodes.
2594 static ir_node *transform_node_Proj(ir_node *proj)
2596 ir_node *n = get_Proj_pred(proj);
2598 switch (get_irn_opcode(n)) {
2600 return transform_node_Proj_Div(proj);
2603 return transform_node_Proj_Mod(proj);
2606 return transform_node_Proj_DivMod(proj);
2609 return transform_node_Proj_Cond(proj);
2612 return transform_node_Proj_Cmp(proj);
2615 /* should not happen, but if it does will be optimized away */
2616 return equivalent_node_Proj(proj);
2625 * returns the operands of a commutative bin-op, if one operand is
2626 * a const, it is returned as the second one.
2628 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2630 ir_node *op_a = get_binop_left(binop);
2631 ir_node *op_b = get_binop_right(binop);
2633 assert(is_op_commutative(get_irn_op(binop)));
2635 if (get_irn_op(op_a) == op_Const) {
2646 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2647 * Such pattern may arise in bitfield stores.
2649 * value c4 value c4 & c2
2650 * AND c3 AND c1 | c3
2655 static ir_node *transform_node_Or_bf_store(ir_node *or)
2659 ir_node *and_l, *c3;
2660 ir_node *value, *c4;
2661 ir_node *new_and, *new_const, *block;
2662 ir_mode *mode = get_irn_mode(or);
2664 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2666 get_comm_Binop_Ops(or, &and, &c1);
2667 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2670 get_comm_Binop_Ops(and, &or_l, &c2);
2671 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2674 get_comm_Binop_Ops(or_l, &and_l, &c3);
2675 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2678 get_comm_Binop_Ops(and_l, &value, &c4);
2679 if (get_irn_op(c4) != op_Const)
2682 /* ok, found the pattern, check for conditions */
2683 assert(mode == get_irn_mode(and));
2684 assert(mode == get_irn_mode(or_l));
2685 assert(mode == get_irn_mode(and_l));
2687 tv1 = get_Const_tarval(c1);
2688 tv2 = get_Const_tarval(c2);
2689 tv3 = get_Const_tarval(c3);
2690 tv4 = get_Const_tarval(c4);
2692 tv = tarval_or(tv4, tv2);
2693 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2694 /* have at least one 0 at the same bit position */
2698 n_tv4 = tarval_not(tv4);
2699 if (tv3 != tarval_and(tv3, n_tv4)) {
2700 /* bit in the or_mask is outside the and_mask */
2704 n_tv2 = tarval_not(tv2);
2705 if (tv1 != tarval_and(tv1, n_tv2)) {
2706 /* bit in the or_mask is outside the and_mask */
2710 /* ok, all conditions met */
2711 block = get_irn_n(or, -1);
2713 new_and = new_r_And(current_ir_graph, block,
2714 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2716 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2718 set_Or_left(or, new_and);
2719 set_Or_right(or, new_const);
2721 /* check for more */
2722 return transform_node_Or_bf_store(or);
2726 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2728 static ir_node *transform_node_Or_Rot(ir_node *or)
2730 ir_mode *mode = get_irn_mode(or);
2731 ir_node *shl, *shr, *block;
2732 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2735 if (! mode_is_int(mode))
2738 shl = get_binop_left(or);
2739 shr = get_binop_right(or);
2741 if (get_irn_op(shl) == op_Shr) {
2742 if (get_irn_op(shr) != op_Shl)
2749 else if (get_irn_op(shl) != op_Shl)
2751 else if (get_irn_op(shr) != op_Shr)
2754 x = get_Shl_left(shl);
2755 if (x != get_Shr_left(shr))
2758 c1 = get_Shl_right(shl);
2759 c2 = get_Shr_right(shr);
2760 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2761 tv1 = get_Const_tarval(c1);
2762 if (! tarval_is_long(tv1))
2765 tv2 = get_Const_tarval(c2);
2766 if (! tarval_is_long(tv2))
2769 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2770 != get_mode_size_bits(mode))
2773 /* yet, condition met */
2774 block = get_irn_n(or, -1);
2776 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2778 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2781 else if (get_irn_op(c1) == op_Sub) {
2785 if (get_Sub_right(sub) != v)
2788 c1 = get_Sub_left(sub);
2789 if (get_irn_op(c1) != op_Const)
2792 tv1 = get_Const_tarval(c1);
2793 if (! tarval_is_long(tv1))
2796 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2799 /* yet, condition met */
2800 block = get_nodes_block(or);
2802 /* a Rot right is not supported, so use a rot left */
2803 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2805 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2808 else if (get_irn_op(c2) == op_Sub) {
2812 c1 = get_Sub_left(sub);
2813 if (get_irn_op(c1) != op_Const)
2816 tv1 = get_Const_tarval(c1);
2817 if (! tarval_is_long(tv1))
2820 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2823 /* yet, condition met */
2824 block = get_irn_n(or, -1);
2827 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2829 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2839 static ir_node *transform_node_Or(ir_node *or)
2841 or = transform_node_Or_bf_store(or);
2842 or = transform_node_Or_Rot(or);
2848 static ir_node *transform_node(ir_node *n);
2851 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2853 * Should be moved to reassociation?
2855 static ir_node *transform_node_shift(ir_node *n)
2857 ir_node *left, *right;
2858 tarval *tv1, *tv2, *res;
2860 int modulo_shf, flag;
2862 left = get_binop_left(n);
2864 /* different operations */
2865 if (get_irn_op(left) != get_irn_op(n))
2868 right = get_binop_right(n);
2869 tv1 = value_of(right);
2870 if (tv1 == tarval_bad)
2873 tv2 = value_of(get_binop_right(left));
2874 if (tv2 == tarval_bad)
2877 res = tarval_add(tv1, tv2);
2879 /* beware: a simple replacement works only, if res < modulo shift */
2880 mode = get_irn_mode(n);
2884 modulo_shf = get_mode_modulo_shift(mode);
2885 if (modulo_shf > 0) {
2886 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2888 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2895 /* ok, we can replace it */
2896 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2898 in[0] = get_binop_left(left);
2899 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2901 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2903 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2905 return transform_node(irn);
2910 #define transform_node_Shr transform_node_shift
2911 #define transform_node_Shrs transform_node_shift
2912 #define transform_node_Shl transform_node_shift
2915 * Remove dead blocks and nodes in dead blocks
2916 * in keep alive list. We do not generate a new End node.
2918 static ir_node *transform_node_End(ir_node *n) {
2919 int i, n_keepalives = get_End_n_keepalives(n);
2921 for (i = 0; i < n_keepalives; ++i) {
2922 ir_node *ka = get_End_keepalive(n, i);
2924 if (is_Block_dead(ka)) {
2925 set_End_keepalive(n, i, new_Bad());
2928 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2929 set_End_keepalive(n, i, new_Bad());
2935 * Optimize a Mux into some simpler cases.
2937 static ir_node *transform_node_Mux(ir_node *n)
2939 ir_node *oldn = n, *sel = get_Mux_sel(n);
2940 ir_mode *mode = get_irn_mode(n);
2942 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2943 ir_node *cmp = get_Proj_pred(sel);
2944 long proj_nr = get_Proj_proj(sel);
2945 ir_node *f = get_Mux_false(n);
2946 ir_node *t = get_Mux_true(n);
2948 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2949 ir_node *block = get_irn_n(n, -1);
2952 * Note: normalization puts the constant on the right site,
2953 * so we check only one case.
2955 * Note further that these optimization work even for floating point
2956 * with NaN's because -NaN == NaN.
2957 * However, if +0 and -0 is handled differently, we cannot use the first one.
2959 if (get_irn_op(f) == op_Minus &&
2960 get_Minus_op(f) == t &&
2961 get_Cmp_left(cmp) == t) {
2963 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2964 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2965 n = new_rd_Abs(get_irn_dbg_info(n),
2969 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2972 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2973 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2974 n = new_rd_Abs(get_irn_dbg_info(n),
2978 n = new_rd_Minus(get_irn_dbg_info(n),
2983 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2987 else if (get_irn_op(t) == op_Minus &&
2988 get_Minus_op(t) == f &&
2989 get_Cmp_left(cmp) == f) {
2991 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2992 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2993 n = new_rd_Abs(get_irn_dbg_info(n),
2997 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3000 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3001 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3002 n = new_rd_Abs(get_irn_dbg_info(n),
3006 n = new_rd_Minus(get_irn_dbg_info(n),
3011 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3016 if (mode_is_int(mode) && mode_is_signed(mode) &&
3017 get_mode_arithmetic(mode) == irma_twos_complement) {
3018 ir_node *x = get_Cmp_left(cmp);
3020 /* the following optimization works only with signed integer two-complement mode */
3022 if (mode == get_irn_mode(x)) {
3024 * FIXME: this restriction is two rigid, as it would still
3025 * work if mode(x) = Hs and mode == Is, but at least it removes
3028 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3029 classify_Const(t) == CNST_ALL_ONE &&
3030 classify_Const(f) == CNST_NULL) {
3032 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3036 n = new_rd_Shrs(get_irn_dbg_info(n),
3037 current_ir_graph, block, x,
3038 new_r_Const_long(current_ir_graph, block, mode_Iu,
3039 get_mode_size_bits(mode) - 1),
3041 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3044 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3045 classify_Const(t) == CNST_ONE &&
3046 classify_Const(f) == CNST_NULL) {
3048 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3052 n = new_rd_Shr(get_irn_dbg_info(n),
3053 current_ir_graph, block,
3054 new_r_Minus(current_ir_graph, block, x, mode),
3055 new_r_Const_long(current_ir_graph, block, mode_Iu,
3056 get_mode_size_bits(mode) - 1),
3058 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3065 return arch_transform_node_Mux(n);
3069 * Optimize a Psi into some simpler cases.
3071 static ir_node *transform_node_Psi(ir_node *n) {
3073 return transform_node_Mux(n);
3079 * Tries several [inplace] [optimizing] transformations and returns an
3080 * equivalent node. The difference to equivalent_node() is that these
3081 * transformations _do_ generate new nodes, and thus the old node must
3082 * not be freed even if the equivalent node isn't the old one.
3084 static ir_node *transform_node(ir_node *n)
3086 if (n->op->ops.transform_node)
3087 n = n->op->ops.transform_node(n);
3092 * sSets the default transform node operation for an ir_op_ops.
3094 * @param code the opcode for the default operation
3095 * @param ops the operations initialized
3100 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3104 ops->transform_node = transform_node_##a; \
3137 /* **************** Common Subexpression Elimination **************** */
3139 /** The size of the hash table used, should estimate the number of nodes
3141 #define N_IR_NODES 512
3143 /** Compares the attributes of two Const nodes. */
3144 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3146 return (get_Const_tarval(a) != get_Const_tarval(b))
3147 || (get_Const_type(a) != get_Const_type(b));
3150 /** Compares the attributes of two Proj nodes. */
3151 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3153 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3156 /** Compares the attributes of two Filter nodes. */
3157 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3159 return get_Filter_proj(a) != get_Filter_proj(b);
3162 /** Compares the attributes of two Alloc nodes. */
3163 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3165 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3166 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3169 /** Compares the attributes of two Free nodes. */
3170 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3172 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3173 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3176 /** Compares the attributes of two SymConst nodes. */
3177 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3179 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3180 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3181 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3184 /** Compares the attributes of two Call nodes. */
3185 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3187 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3190 /** Compares the attributes of two Sel nodes. */
3191 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3193 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3194 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3195 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3196 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3197 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3200 /** Compares the attributes of two Phi nodes. */
3201 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3203 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3206 /** Compares the attributes of two Cast nodes. */
3207 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3209 return get_Cast_type(a) != get_Cast_type(b);
3212 /** Compares the attributes of two Load nodes. */
3213 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3215 if (get_Load_volatility(a) == volatility_is_volatile ||
3216 get_Load_volatility(b) == volatility_is_volatile)
3217 /* NEVER do CSE on volatile Loads */
3220 return get_Load_mode(a) != get_Load_mode(b);
3223 /** Compares the attributes of two Store nodes. */
3224 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3226 /* NEVER do CSE on volatile Stores */
3227 return (get_Store_volatility(a) == volatility_is_volatile ||
3228 get_Store_volatility(b) == volatility_is_volatile);
3231 /** Compares the attributes of two Confirm nodes. */
3232 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3234 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3238 * Set the default node attribute compare operation for an ir_op_ops.
3240 * @param code the opcode for the default operation
3241 * @param ops the operations initialized
3246 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3250 ops->node_cmp_attr = node_cmp_attr_##a; \
3276 * Compare function for two nodes in the hash table. Gets two
3277 * nodes as parameters. Returns 0 if the nodes are a cse.
3279 int identities_cmp(const void *elt, const void *key)
3287 if (a == b) return 0;
3289 if ((get_irn_op(a) != get_irn_op(b)) ||
3290 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3292 /* compare if a's in and b's in are of equal length */
3293 irn_arity_a = get_irn_intra_arity (a);
3294 if (irn_arity_a != get_irn_intra_arity(b))
3297 /* for block-local cse and op_pin_state_pinned nodes: */
3298 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3299 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3303 /* compare a->in[0..ins] with b->in[0..ins] */
3304 for (i = 0; i < irn_arity_a; i++)
3305 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3309 * here, we already now that the nodes are identical except their
3312 if (a->op->ops.node_cmp_attr)
3313 return a->op->ops.node_cmp_attr(a, b);
3319 * Calculate a hash value of a node.
3322 ir_node_hash (ir_node *node)
3327 if (node->op == op_Const) {
3328 /* special value for const, as they only differ in their tarval. */
3329 h = HASH_PTR(node->attr.con.tv);
3330 h = 9*h + HASH_PTR(get_irn_mode(node));
3331 } else if (node->op == op_SymConst) {
3332 /* special value for const, as they only differ in their symbol. */
3333 h = HASH_PTR(node->attr.i.sym.type_p);
3334 h = 9*h + HASH_PTR(get_irn_mode(node));
3337 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3338 h = irn_arity = get_irn_intra_arity(node);
3340 /* consider all in nodes... except the block if not a control flow. */
3341 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3342 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3346 h = 9*h + HASH_PTR(get_irn_mode(node));
3348 h = 9*h + HASH_PTR(get_irn_op(node));
3355 new_identities(void) {
3356 return new_pset(identities_cmp, N_IR_NODES);
3360 del_identities(pset *value_table) {
3361 del_pset(value_table);
3365 * Return the canonical node computing the same value as n.
3366 * Looks up the node in a hash table.
3368 * For Const nodes this is performed in the constructor, too. Const
3369 * nodes are extremely time critical because of their frequent use in
3370 * constant string arrays.
3372 static INLINE ir_node *
3373 identify (pset *value_table, ir_node *n)
3377 if (!value_table) return n;
3379 if (get_opt_reassociation()) {
3380 if (is_op_commutative(get_irn_op(n))) {
3381 ir_node *l = get_binop_left(n);
3382 ir_node *r = get_binop_right(n);
3384 /* for commutative operators perform a OP b == b OP a */
3386 set_binop_left(n, r);
3387 set_binop_right(n, l);
3392 o = pset_find (value_table, n, ir_node_hash (n));
3401 * During construction we set the op_pin_state_pinned flag in the graph right when the
3402 * optimization is performed. The flag turning on procedure global cse could
3403 * be changed between two allocations. This way we are safe.
3405 static INLINE ir_node *
3406 identify_cons (pset *value_table, ir_node *n) {
3409 n = identify(value_table, n);
3410 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3411 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3416 * Return the canonical node computing the same value as n.
3417 * Looks up the node in a hash table, enters it in the table
3418 * if it isn't there yet.
3421 identify_remember (pset *value_table, ir_node *n)
3425 if (!value_table) return n;
3427 if (get_opt_reassociation()) {
3428 if (is_op_commutative(get_irn_op(n))) {
3429 ir_node *l = get_binop_left(n);
3430 ir_node *r = get_binop_right(n);
3432 /* for commutative operators perform a OP b == b OP a */
3434 set_binop_left(n, r);
3435 set_binop_right(n, l);
3440 /* lookup or insert in hash table with given hash key. */
3441 o = pset_insert (value_table, n, ir_node_hash (n));
3451 add_identities (pset *value_table, ir_node *node) {
3452 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3453 identify_remember (value_table, node);
3457 * garbage in, garbage out. If a node has a dead input, i.e., the
3458 * Bad node is input to the node, return the Bad node.
3460 static INLINE ir_node *
3461 gigo (ir_node *node)
3464 ir_op *op = get_irn_op(node);
3466 /* remove garbage blocks by looking at control flow that leaves the block
3467 and replacing the control flow by Bad. */
3468 if (get_irn_mode(node) == mode_X) {
3469 ir_node *block = get_nodes_block(skip_Proj(node));
3471 /* Don't optimize nodes in immature blocks. */
3472 if (!get_Block_matured(block)) return node;
3473 /* Don't optimize End, may have Bads. */
3474 if (op == op_End) return node;
3476 if (is_Block(block)) {
3477 irn_arity = get_irn_arity(block);
3478 for (i = 0; i < irn_arity; i++) {
3479 if (!is_Bad(get_irn_n(block, i)))
3482 if (i == irn_arity) return new_Bad();
3486 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3487 blocks predecessors is dead. */
3488 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3489 irn_arity = get_irn_arity(node);
3492 * Beware: we can only read the block of a non-floating node.
3494 if (is_irn_pinned_in_irg(node) &&
3495 is_Block_dead(get_nodes_block(node)))
3498 for (i = 0; i < irn_arity; i++) {
3499 ir_node *pred = get_irn_n(node, i);
3504 /* Propagating Unknowns here seems to be a bad idea, because
3505 sometimes we need a node as a input and did not want that
3507 However, i might be useful to move this into a later phase
3508 (it you thing optimizing such code is useful). */
3509 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3510 return new_Unknown(get_irn_mode(node));
3515 /* With this code we violate the agreement that local_optimize
3516 only leaves Bads in Block, Phi and Tuple nodes. */
3517 /* If Block has only Bads as predecessors it's garbage. */
3518 /* If Phi has only Bads as predecessors it's garbage. */
3519 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3520 irn_arity = get_irn_arity(node);
3521 for (i = 0; i < irn_arity; i++) {
3522 if (!is_Bad(get_irn_n(node, i))) break;
3524 if (i == irn_arity) node = new_Bad();
3532 * These optimizations deallocate nodes from the obstack.
3533 * It can only be called if it is guaranteed that no other nodes
3534 * reference this one, i.e., right after construction of a node.
3536 * current_ir_graph must be set to the graph of the node!
3539 optimize_node(ir_node *n)
3543 opcode iro = get_irn_opcode(n);
3545 /* Always optimize Phi nodes: part of the construction. */
3546 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3548 /* constant expression evaluation / constant folding */
3549 if (get_opt_constant_folding()) {
3550 /* neither constants nor Tuple values can be evaluated */
3551 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3552 /* try to evaluate */
3553 tv = computed_value(n);
3554 if (tv != tarval_bad) {
3556 ir_type *old_tp = get_irn_type(n);
3557 int i, arity = get_irn_arity(n);
3561 * Try to recover the type of the new expression.
3563 for (i = 0; i < arity && !old_tp; ++i)
3564 old_tp = get_irn_type(get_irn_n(n, i));
3567 * we MUST copy the node here temporary, because it's still needed
3568 * for DBG_OPT_CSTEVAL
3570 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3571 oldn = alloca(node_size);
3573 memcpy(oldn, n, node_size);
3574 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3576 /* ARG, copy the in array, we need it for statistics */
3577 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3579 /* note the inplace edges module */
3580 edges_node_deleted(n, current_ir_graph);
3582 /* evaluation was successful -- replace the node. */
3583 obstack_free(current_ir_graph->obst, n);
3584 nw = new_Const(get_tarval_mode (tv), tv);
3586 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3587 set_Const_type(nw, old_tp);
3588 DBG_OPT_CSTEVAL(oldn, nw);
3594 /* remove unnecessary nodes */
3595 if (get_opt_constant_folding() ||
3596 (iro == iro_Phi) || /* always optimize these nodes. */
3598 (iro == iro_Proj) ||
3599 (iro == iro_Block) ) /* Flags tested local. */
3600 n = equivalent_node (n);
3602 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3604 /* Common Subexpression Elimination.
3606 * Checks whether n is already available.
3607 * The block input is used to distinguish different subexpressions. Right
3608 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3609 * subexpressions within a block.
3612 n = identify_cons (current_ir_graph->value_table, n);
3615 edges_node_deleted(oldn, current_ir_graph);
3617 /* We found an existing, better node, so we can deallocate the old node. */
3618 obstack_free (current_ir_graph->obst, oldn);
3623 /* Some more constant expression evaluation that does not allow to
3625 iro = get_irn_opcode(n);
3626 if (get_opt_constant_folding() ||
3627 (iro == iro_Cond) ||
3628 (iro == iro_Proj) ||
3629 (iro == iro_Sel)) /* Flags tested local. */
3630 n = transform_node (n);
3632 /* Remove nodes with dead (Bad) input.
3633 Run always for transformation induced Bads. */
3636 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3637 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3638 n = identify_remember (current_ir_graph->value_table, n);
3646 * These optimizations never deallocate nodes (in place). This can cause dead
3647 * nodes lying on the obstack. Remove these by a dead node elimination,
3648 * i.e., a copying garbage collection.
3651 optimize_in_place_2 (ir_node *n)
3655 opcode iro = get_irn_opcode(n);
3657 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3659 /* constant expression evaluation / constant folding */
3660 if (get_opt_constant_folding()) {
3661 /* neither constants nor Tuple values can be evaluated */
3662 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3663 /* try to evaluate */
3664 tv = computed_value(n);
3665 if (tv != tarval_bad) {
3666 /* evaluation was successful -- replace the node. */
3667 ir_type *old_tp = get_irn_type(n);
3668 int i, arity = get_irn_arity(n);
3671 * Try to recover the type of the new expression.
3673 for (i = 0; i < arity && !old_tp; ++i)
3674 old_tp = get_irn_type(get_irn_n(n, i));
3676 n = new_Const(get_tarval_mode(tv), tv);
3678 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3679 set_Const_type(n, old_tp);
3681 DBG_OPT_CSTEVAL(oldn, n);
3687 /* remove unnecessary nodes */
3688 if (get_opt_constant_folding() ||
3689 (iro == iro_Phi) || /* always optimize these nodes. */
3690 (iro == iro_Id) || /* ... */
3691 (iro == iro_Proj) || /* ... */
3692 (iro == iro_Block) ) /* Flags tested local. */
3693 n = equivalent_node(n);
3695 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3697 /** common subexpression elimination **/
3698 /* Checks whether n is already available. */
3699 /* The block input is used to distinguish different subexpressions. Right
3700 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3701 subexpressions within a block. */
3702 if (get_opt_cse()) {
3703 n = identify(current_ir_graph->value_table, n);
3706 /* Some more constant expression evaluation. */
3707 iro = get_irn_opcode(n);
3708 if (get_opt_constant_folding() ||
3709 (iro == iro_Cond) ||
3710 (iro == iro_Proj) ||
3711 (iro == iro_Sel)) /* Flags tested local. */
3712 n = transform_node(n);
3714 /* Remove nodes with dead (Bad) input.
3715 Run always for transformation induced Bads. */
3718 /* Now we can verify the node, as it has no dead inputs any more. */
3721 /* Now we have a legal, useful node. Enter it in hash table for cse.
3722 Blocks should be unique anyways. (Except the successor of start:
3723 is cse with the start block!) */
3724 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3725 n = identify_remember(current_ir_graph->value_table, n);
3731 * Wrapper for external use, set proper status bits after optimization.
3734 optimize_in_place (ir_node *n)
3736 /* Handle graph state */
3737 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3739 if (get_opt_global_cse())
3740 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3741 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3742 set_irg_outs_inconsistent(current_ir_graph);
3744 /* FIXME: Maybe we could also test whether optimizing the node can
3745 change the control graph. */
3746 set_irg_doms_inconsistent(current_ir_graph);
3747 return optimize_in_place_2 (n);
3751 * Sets the default operation for an ir_ops.
3753 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3755 ops = firm_set_default_computed_value(code, ops);
3756 ops = firm_set_default_equivalent_node(code, ops);
3757 ops = firm_set_default_transform_node(code, ops);
3758 ops = firm_set_default_node_cmp_attr(code, ops);
3759 ops = firm_set_default_get_type(code, ops);
3760 ops = firm_set_default_get_type_attr(code, ops);
3761 ops = firm_set_default_get_entity_attr(code, ops);