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 Confirm: can be evaluated,
623 * if it has the form Confirm(x, '=', Const).
625 static tarval *computed_value_Confirm(ir_node *n)
627 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
628 value_of(get_Confirm_bound(n)) : tarval_bad;
632 * If the parameter n can be computed, return its value, else tarval_bad.
633 * Performs constant folding.
635 * @param n The node this should be evaluated
637 tarval *computed_value(ir_node *n)
639 if (n->op->ops.computed_value)
640 return n->op->ops.computed_value(n);
645 * set the default computed_value evaluator in an ir_op_ops.
647 * @param code the opcode for the default operation
648 * @param ops the operations initialized
653 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
657 ops->computed_value = computed_value_##a; \
694 /* returns 1 if the a and b are pointers to different locations. */
696 different_identity (ir_node *a, ir_node *b)
698 assert (mode_is_reference(get_irn_mode (a))
699 && mode_is_reference(get_irn_mode (b)));
701 if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
702 ir_node *a1 = get_Proj_pred (a);
703 ir_node *b1 = get_Proj_pred (b);
704 if (a1 != b1 && get_irn_op (a1) == op_Alloc
705 && get_irn_op (b1) == op_Alloc)
713 * Returns a equivalent block for another block.
714 * If the block has only one predecessor, this is
715 * the equivalent one. If the only predecessor of a block is
716 * the block itself, this is a dead block.
718 * If both predecessors of a block are the branches of a binary
719 * Cond, the equivalent block is Cond's block.
721 * If all predecessors of a block are bad or lies in a dead
722 * block, the current block is dead as well.
724 * Note, that blocks are NEVER turned into Bad's, instead
725 * the dead_block flag is set. So, never test for is_Bad(block),
726 * always use is_dead_Block(block).
728 static ir_node *equivalent_node_Block(ir_node *n)
731 int n_preds = get_Block_n_cfgpreds(n);
733 /* The Block constructor does not call optimize, but mature_immBlock
734 calls the optimization. */
735 assert(get_Block_matured(n));
737 /* Straightening: a single entry Block following a single exit Block
738 can be merged, if it is not the Start block. */
739 /* !!! Beware, all Phi-nodes of n must have been optimized away.
740 This should be true, as the block is matured before optimize is called.
741 But what about Phi-cycles with the Phi0/Id that could not be resolved?
742 Remaining Phi nodes are just Ids. */
743 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
744 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
745 if (predblock == oldn) {
746 /* Jmp jumps into the block it is in -- deal self cycle. */
747 n = set_Block_dead(n);
748 DBG_OPT_DEAD_BLOCK(oldn, n);
749 } else if (get_opt_control_flow_straightening()) {
751 DBG_OPT_STG(oldn, n);
754 else if ((n_preds == 1) &&
755 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
756 ir_node *predblock = get_Block_cfgpred_block(n, 0);
757 if (predblock == oldn) {
758 /* Jmp jumps into the block it is in -- deal self cycle. */
759 n = set_Block_dead(n);
760 DBG_OPT_DEAD_BLOCK(oldn, n);
763 else if ((n_preds == 2) &&
764 (get_opt_control_flow_weak_simplification())) {
765 /* Test whether Cond jumps twice to this block
766 * The more general case which more than 2 predecessors is handles
767 * in optimize_cf(), we handle only this special case for speed here.
769 ir_node *a = get_Block_cfgpred(n, 0);
770 ir_node *b = get_Block_cfgpred(n, 1);
772 if ((get_irn_op(a) == op_Proj) &&
773 (get_irn_op(b) == op_Proj) &&
774 (get_Proj_pred(a) == get_Proj_pred(b)) &&
775 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
776 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
777 /* Also a single entry Block following a single exit Block. Phis have
778 twice the same operand and will be optimized away. */
779 n = get_nodes_block(get_Proj_pred(a));
780 DBG_OPT_IFSIM1(oldn, a, b, n);
783 else if (get_opt_unreachable_code() &&
784 (n != current_ir_graph->start_block) &&
785 (n != current_ir_graph->end_block) ) {
788 /* If all inputs are dead, this block is dead too, except if it is
789 the start or end block. This is one step of unreachable code
791 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
792 ir_node *pred = get_Block_cfgpred(n, i);
795 if (is_Bad(pred)) continue;
796 pred_blk = get_nodes_block(skip_Proj(pred));
798 if (is_Block_dead(pred_blk)) continue;
801 /* really found a living input */
806 n = set_Block_dead(n);
807 DBG_OPT_DEAD_BLOCK(oldn, n);
815 * Returns a equivalent node for a Jmp, a Bad :-)
816 * Of course this only happens if the Block of the Jmp is Bad.
818 static ir_node *equivalent_node_Jmp(ir_node *n)
820 /* unreachable code elimination */
821 if (is_Block_dead(get_nodes_block(n)))
827 /* Same for op_Raise */
828 #define equivalent_node_Raise equivalent_node_Jmp
831 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
832 See transform_node_Proj_Cond(). */
835 * optimize operations that are commutative and have neutral 0,
836 * so a op 0 = 0 op a = a.
838 static ir_node *equivalent_node_neutral_zero(ir_node *n)
842 ir_node *a = get_binop_left(n);
843 ir_node *b = get_binop_right(n);
848 /* After running compute_node there is only one constant predecessor.
849 Find this predecessors value and remember the other node: */
850 if ((tv = value_of(a)) != tarval_bad) {
852 } else if ((tv = value_of(b)) != tarval_bad) {
857 /* If this predecessors constant value is zero, the operation is
858 * unnecessary. Remove it.
860 * Beware: If n is a Add, the mode of on and n might be different
861 * which happens in this rare construction: NULL + 3.
862 * Then, a Conv would be needed which we cannot include here.
864 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
865 if (get_irn_mode(on) == get_irn_mode(n)) {
868 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
875 #define equivalent_node_Eor equivalent_node_neutral_zero
878 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
880 * The second one looks strange, but this construct
881 * is used heavily in the LCC sources :-).
883 * Beware: The Mode of an Add may be different than the mode of its
884 * predecessors, so we could not return a predecessors in all cases.
886 static ir_node *equivalent_node_Add(ir_node *n)
889 ir_node *left, *right;
891 n = equivalent_node_neutral_zero(n);
895 left = get_Add_left(n);
896 right = get_Add_right(n);
898 if (get_irn_op(left) == op_Sub) {
899 if (get_Sub_right(left) == right) {
902 n = get_Sub_left(left);
903 if (get_irn_mode(oldn) == get_irn_mode(n)) {
904 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
909 if (get_irn_op(right) == op_Sub) {
910 if (get_Sub_right(right) == left) {
913 n = get_Sub_left(right);
914 if (get_irn_mode(oldn) == get_irn_mode(n)) {
915 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
924 * optimize operations that are not commutative but have neutral 0 on left,
927 static ir_node *equivalent_node_left_zero(ir_node *n)
931 ir_node *a = get_binop_left(n);
932 ir_node *b = get_binop_right(n);
934 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
937 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
943 #define equivalent_node_Shl equivalent_node_left_zero
944 #define equivalent_node_Shr equivalent_node_left_zero
945 #define equivalent_node_Shrs equivalent_node_left_zero
946 #define equivalent_node_Rot equivalent_node_left_zero
949 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
951 * The second one looks strange, but this construct
952 * is used heavily in the LCC sources :-).
954 * Beware: The Mode of a Sub may be different than the mode of its
955 * predecessors, so we could not return a predecessors in all cases.
957 static ir_node *equivalent_node_Sub(ir_node *n)
961 ir_node *a = get_Sub_left(n);
962 ir_node *b = get_Sub_right(n);
964 /* Beware: modes might be different */
965 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
966 if (get_irn_mode(n) == get_irn_mode(a)) {
969 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
972 else if (get_irn_op(a) == op_Add) {
973 ir_mode *mode = get_irn_mode(n);
975 if (mode_wrap_around(mode)) {
976 ir_node *left = get_Add_left(a);
977 ir_node *right = get_Add_right(a);
980 if (get_irn_mode(n) == get_irn_mode(right)) {
982 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
985 else if (right == b) {
986 if (get_irn_mode(n) == get_irn_mode(left)) {
988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
999 * Optimize an "idempotent unary op", ie op(op(n)) = n.
1002 * -(-a) == a, but might overflow two times.
1003 * We handle it anyway here but the better way would be a
1004 * flag. This would be needed for Pascal for instance.
1006 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1009 ir_node *pred = get_unop_op(n);
1011 /* optimize symmetric unop */
1012 if (get_irn_op(pred) == get_irn_op(n)) {
1013 n = get_unop_op(pred);
1014 DBG_OPT_ALGSIM2(oldn, pred, n);
1019 /* Not(Not(x)) == x */
1020 #define equivalent_node_Not equivalent_node_idempotent_unop
1022 /* --x == x */ /* ??? Is this possible or can --x raise an
1023 out of bounds exception if min =! max? */
1024 #define equivalent_node_Minus equivalent_node_idempotent_unop
1027 * Optimize a * 1 = 1 * a = a.
1029 static ir_node *equivalent_node_Mul(ir_node *n)
1033 ir_node *a = get_Mul_left(n);
1034 ir_node *b = get_Mul_right(n);
1036 /* Mul is commutative and has again an other neutral element. */
1037 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1039 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1040 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1042 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1048 * Optimize a / 1 = a.
1050 static ir_node *equivalent_node_Div(ir_node *n)
1052 ir_node *a = get_Div_left(n);
1053 ir_node *b = get_Div_right(n);
1055 /* Div is not commutative. */
1056 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1057 /* Turn Div into a tuple (mem, bad, a) */
1058 ir_node *mem = get_Div_mem(n);
1059 turn_into_tuple(n, pn_Div_max);
1060 set_Tuple_pred(n, pn_Div_M, mem);
1061 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1062 set_Tuple_pred(n, pn_Div_res, a);
1068 * Optimize a / 1 = a.
1070 static ir_node *equivalent_node_DivMod(ir_node *n)
1072 ir_node *a = get_DivMod_left(n);
1073 ir_node *b = get_DivMod_right(n);
1075 /* Div is not commutative. */
1076 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1077 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1078 ir_node *mem = get_Div_mem(n);
1079 ir_mode *mode = get_irn_mode(b);
1081 turn_into_tuple(n, pn_DivMod_max);
1082 set_Tuple_pred(n, pn_DivMod_M, mem);
1083 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1084 set_Tuple_pred(n, pn_DivMod_res_div, a);
1085 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1091 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1093 static ir_node *equivalent_node_Or(ir_node *n)
1097 ir_node *a = get_Or_left(n);
1098 ir_node *b = get_Or_right(n);
1101 n = a; /* Or has it's own neutral element */
1102 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1103 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1105 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1106 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1117 static ir_node *equivalent_node_And(ir_node *n)
1121 ir_node *a = get_And_left(n);
1122 ir_node *b = get_And_right(n);
1125 n = a; /* And has it's own neutral element */
1126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1127 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1129 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1130 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1132 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 * Try to remove useless Conv's:
1140 static ir_node *equivalent_node_Conv(ir_node *n)
1143 ir_node *a = get_Conv_op(n);
1146 ir_mode *n_mode = get_irn_mode(n);
1147 ir_mode *a_mode = get_irn_mode(a);
1149 if (n_mode == a_mode) { /* No Conv necessary */
1151 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1152 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1156 n_mode = get_irn_mode(n);
1157 b_mode = get_irn_mode(b);
1159 if (n_mode == b_mode) {
1160 if (n_mode == mode_b) {
1161 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1162 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1164 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1165 if (smaller_mode(b_mode, a_mode)){
1166 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1167 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1176 * A Cast may be removed if the type of the previous node
1177 * is already the type of the Cast.
1179 static ir_node *equivalent_node_Cast(ir_node *n) {
1181 ir_node *pred = get_Cast_op(n);
1183 if (get_irn_type(pred) == get_Cast_type(n)) {
1185 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1191 Several optimizations:
1192 - no Phi in start block.
1193 - remove Id operators that are inputs to Phi
1194 - fold Phi-nodes, iff they have only one predecessor except
1197 static ir_node *equivalent_node_Phi(ir_node *n)
1202 ir_node *block = NULL; /* to shutup gcc */
1203 ir_node *first_val = NULL; /* to shutup gcc */
1205 if (!get_opt_normalize()) return n;
1207 n_preds = get_Phi_n_preds(n);
1209 block = get_nodes_block(n);
1210 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1211 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1212 if ((is_Block_dead(block)) || /* Control dead */
1213 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1214 return new_Bad(); /* in the Start Block. */
1216 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1218 /* If the Block has a Bad pred, we also have one. */
1219 for (i = 0; i < n_preds; ++i)
1220 if (is_Bad(get_Block_cfgpred(block, i)))
1221 set_Phi_pred(n, i, new_Bad());
1223 /* Find first non-self-referencing input */
1224 for (i = 0; i < n_preds; ++i) {
1225 first_val = get_Phi_pred(n, i);
1226 if ( (first_val != n) /* not self pointer */
1228 && (! is_Bad(first_val))
1230 ) { /* value not dead */
1231 break; /* then found first value. */
1236 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1240 /* search for rest of inputs, determine if any of these
1241 are non-self-referencing */
1242 while (++i < n_preds) {
1243 ir_node *scnd_val = get_Phi_pred(n, i);
1244 if ( (scnd_val != n)
1245 && (scnd_val != first_val)
1247 && (! is_Bad(scnd_val))
1255 /* Fold, if no multiple distinct non-self-referencing inputs */
1257 DBG_OPT_PHI(oldn, n);
1263 Several optimizations:
1264 - no Sync in start block.
1265 - fold Sync-nodes, iff they have only one predecessor except
1267 @fixme: are there loop's in Sync's
1269 static ir_node *equivalent_node_Sync(ir_node *n)
1274 ir_node *first_val = NULL; /* to shutup gcc */
1276 if (!get_opt_normalize()) return n;
1278 n_preds = get_Sync_n_preds(n);
1280 /* Find first non-self-referencing input */
1281 for (i = 0; i < n_preds; ++i) {
1282 first_val = get_Sync_pred(n, i);
1283 if ((first_val != n) /* not self pointer */ &&
1284 (! is_Bad(first_val))
1285 ) { /* value not dead */
1286 break; /* then found first value. */
1291 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1294 /* search the rest of inputs, determine if any of these
1295 are non-self-referencing */
1296 while (++i < n_preds) {
1297 ir_node *scnd_val = get_Sync_pred(n, i);
1298 if ((scnd_val != n) &&
1299 (scnd_val != first_val) &&
1300 (! is_Bad(scnd_val))
1306 /* Fold, if no multiple distinct non-self-referencing inputs */
1308 DBG_OPT_SYNC(oldn, n);
1314 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1315 * ProjX(Load) and ProjX(Store)
1317 static ir_node *equivalent_node_Proj(ir_node *n)
1321 ir_node *a = get_Proj_pred(n);
1323 if ( get_irn_op(a) == op_Tuple) {
1324 /* Remove the Tuple/Proj combination. */
1325 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1326 n = get_Tuple_pred(a, get_Proj_proj(n));
1327 DBG_OPT_TUPLE(oldn, a, n);
1329 assert(0); /* This should not happen! */
1333 else if (get_irn_mode(n) == mode_X) {
1334 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1335 /* Remove dead control flow -- early gigo(). */
1338 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1339 ir_op *op = get_irn_op(a);
1341 if (op == op_Load || op == op_Store) {
1342 /* get the load/store address */
1343 ir_node *addr = get_irn_n(a, 1);
1344 if (value_not_null(addr)) {
1345 /* this node may float */
1346 set_irn_pinned(a, op_pin_state_floats);
1360 static ir_node *equivalent_node_Id(ir_node *n)
1366 } while (get_irn_op(n) == op_Id);
1368 DBG_OPT_ID(oldn, n);
1375 static ir_node *equivalent_node_Mux(ir_node *n)
1377 ir_node *oldn = n, *sel = get_Mux_sel(n);
1378 tarval *ts = value_of(sel);
1380 /* Mux(true, f, t) == t */
1381 if (ts == tarval_b_true) {
1382 n = get_Mux_true(n);
1383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1385 /* Mux(false, f, t) == f */
1386 else if (ts == tarval_b_false) {
1387 n = get_Mux_false(n);
1388 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1390 /* Mux(v, x, x) == x */
1391 else if (get_Mux_false(n) == get_Mux_true(n)) {
1392 n = get_Mux_true(n);
1393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1395 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1396 ir_node *cmp = get_Proj_pred(sel);
1397 long proj_nr = get_Proj_proj(sel);
1398 ir_node *b = get_Mux_false(n);
1399 ir_node *a = get_Mux_true(n);
1402 * Note: normalization puts the constant on the right site,
1403 * so we check only one case.
1405 * Note further that these optimization work even for floating point
1406 * with NaN's because -NaN == NaN.
1407 * However, if +0 and -0 is handled differently, we cannot use the first one.
1409 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1410 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1411 /* Mux(a CMP 0, X, a) */
1412 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1413 /* Mux(a CMP 0, -a, a) */
1414 if (proj_nr == pn_Cmp_Eq) {
1415 /* Mux(a == 0, -a, a) ==> -a */
1417 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1419 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1420 /* Mux(a != 0, -a, a) ==> a */
1422 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1425 else if (classify_Const(b) == CNST_NULL) {
1426 /* Mux(a CMP 0, 0, a) */
1427 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1428 /* Mux(a != 0, 0, a) ==> a */
1430 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1432 else if (proj_nr == pn_Cmp_Eq) {
1433 /* Mux(a == 0, 0, a) ==> 0 */
1435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1445 * Optimize -a CMP -b into b CMP a.
1446 * This works only for for modes where unary Minus
1448 * Note that two-complement integers can Overflow
1449 * so it will NOT work.
1451 static ir_node *equivalent_node_Cmp(ir_node *n)
1453 ir_node *left = get_Cmp_left(n);
1454 ir_node *right = get_Cmp_right(n);
1456 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1457 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1458 left = get_Minus_op(left);
1459 right = get_Minus_op(right);
1460 set_Cmp_left(n, right);
1461 set_Cmp_right(n, left);
1467 * Remove Confirm nodes if setting is on.
1468 * Replace Confirms(x, '=', Constlike) by Constlike.
1470 static ir_node *equivalent_node_Confirm(ir_node *n)
1472 ir_node *pred = get_Confirm_value(n);
1473 pn_Cmp pnc = get_Confirm_cmp(n);
1475 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1477 * rare case: two identical Confirms one after another,
1478 * replace the second one with the first.
1482 if (pnc == pn_Cmp_Eq) {
1483 ir_node *bound = get_Confirm_bound(n);
1486 * Optimize a rare case:
1487 * Confirm(x, '=', Constlike) ==> Constlike
1489 if (is_irn_constlike(bound)) {
1490 DBG_OPT_CONFIRM(n, bound);
1494 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1498 * Optimize CopyB(mem, x, x) into a Nop
1500 static ir_node *equivalent_node_CopyB(ir_node *n)
1502 ir_node *a = get_CopyB_dst(n);
1503 ir_node *b = get_CopyB_src(n);
1506 /* Turn CopyB into a tuple (mem, bad, bad) */
1507 ir_node *mem = get_CopyB_mem(n);
1508 turn_into_tuple(n, pn_CopyB_max);
1509 set_Tuple_pred(n, pn_CopyB_M, mem);
1510 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1511 set_Tuple_pred(n, pn_Call_M_except, new_Bad());
1517 * Optimize Bounds(idx, idx, upper) into idx.
1519 static ir_node *equivalent_node_Bound(ir_node *n)
1521 ir_node *idx = get_Bound_index(n);
1522 ir_node *lower = get_Bound_lower(n);
1525 /* By definition lower < upper, so if idx == lower -->
1526 lower <= idx && idx < upper */
1528 /* Turn Bound into a tuple (mem, bad, idx) */
1532 ir_node *pred = skip_Proj(idx);
1534 if (get_irn_op(pred) == op_Bound) {
1536 * idx was Bounds_check previously, it is still valid if
1537 * lower <= pred_lower && pred_upper <= upper.
1539 ir_node *upper = get_Bound_upper(n);
1540 if (get_Bound_lower(pred) == lower &&
1541 get_Bound_upper(pred) == upper) {
1543 * One could expect that we simple return the previous
1544 * Bound here. However, this would be wrong, as we could
1545 * add an exception Proj to a new location than.
1546 * So, we must turn in into a tuple
1553 /* Turn Bound into a tuple (mem, bad, idx) */
1554 ir_node *mem = get_Bound_mem(n);
1555 turn_into_tuple(n, pn_Bound_max);
1556 set_Tuple_pred(n, pn_Bound_M_regular, mem);
1557 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1558 set_Tuple_pred(n, pn_Bound_res, idx);
1559 set_Tuple_pred(n, pn_Bound_M_except, mem);
1565 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1566 * perform no actual computation, as, e.g., the Id nodes. It does not create
1567 * new nodes. It is therefore safe to free n if the node returned is not n.
1568 * If a node returns a Tuple we can not just skip it. If the size of the
1569 * in array fits, we transform n into a tuple (e.g., Div).
1572 equivalent_node(ir_node *n)
1574 if (n->op->ops.equivalent_node)
1575 return n->op->ops.equivalent_node(n);
1580 * sets the default equivalent node operation for an ir_op_ops.
1582 * @param code the opcode for the default operation
1583 * @param ops the operations initialized
1588 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1592 ops->equivalent_node = equivalent_node_##a; \
1633 * Do node specific optimizations of nodes predecessors.
1636 optimize_preds(ir_node *n) {
1637 ir_node *a = NULL, *b = NULL;
1639 /* get the operands we will work on for simple cases. */
1641 a = get_binop_left(n);
1642 b = get_binop_right(n);
1643 } else if (is_unop(n)) {
1647 switch (get_irn_opcode(n)) {
1650 /* We don't want Cast as input to Cmp. */
1651 if (get_irn_op(a) == op_Cast) {
1655 if (get_irn_op(b) == op_Cast) {
1657 set_Cmp_right(n, b);
1666 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1667 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1668 * If possible, remove the Conv's.
1670 static ir_node *transform_node_AddSub(ir_node *n)
1672 ir_mode *mode = get_irn_mode(n);
1674 if (mode_is_reference(mode)) {
1675 ir_node *left = get_binop_left(n);
1676 ir_node *right = get_binop_right(n);
1677 int ref_bits = get_mode_size_bits(mode);
1679 if (get_irn_op(left) == op_Conv) {
1680 ir_mode *mode = get_irn_mode(left);
1681 int bits = get_mode_size_bits(mode);
1683 if (ref_bits == bits &&
1684 mode_is_int(mode) &&
1685 get_mode_arithmetic(mode) == irma_twos_complement) {
1686 ir_node *pre = get_Conv_op(left);
1687 ir_mode *pre_mode = get_irn_mode(pre);
1689 if (mode_is_int(pre_mode) &&
1690 get_mode_size_bits(pre_mode) == bits &&
1691 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1692 /* ok, this conv just changes to sign, moreover the calculation
1693 * is done with same number of bits as our address mode, so
1694 * we can ignore the conv as address calculation can be viewed
1695 * as either signed or unsigned
1697 set_binop_left(n, pre);
1702 if (get_irn_op(right) == op_Conv) {
1703 ir_mode *mode = get_irn_mode(right);
1704 int bits = get_mode_size_bits(mode);
1706 if (ref_bits == bits &&
1707 mode_is_int(mode) &&
1708 get_mode_arithmetic(mode) == irma_twos_complement) {
1709 ir_node *pre = get_Conv_op(right);
1710 ir_mode *pre_mode = get_irn_mode(pre);
1712 if (mode_is_int(pre_mode) &&
1713 get_mode_size_bits(pre_mode) == bits &&
1714 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1715 /* ok, this conv just changes to sign, moreover the calculation
1716 * is done with same number of bits as our address mode, so
1717 * we can ignore the conv as address calculation can be viewed
1718 * as either signed or unsigned
1720 set_binop_right(n, pre);
1729 * Do the AddSub optimization, then Transform
1730 * Add(a,a) -> Mul(a, 2)
1731 * Add(Mul(a, x), a) -> Mul(a, x+1)
1732 * if the mode is integer or float.
1733 * Transform Add(a,-b) into Sub(a,b).
1734 * Reassociation might fold this further.
1736 static ir_node *transform_node_Add(ir_node *n)
1741 n = transform_node_AddSub(n);
1743 mode = get_irn_mode(n);
1744 if (mode_is_num(mode)) {
1745 ir_node *a = get_Add_left(n);
1746 ir_node *b = get_Add_right(n);
1749 ir_node *block = get_irn_n(n, -1);
1752 get_irn_dbg_info(n),
1756 new_r_Const_long(current_ir_graph, block, mode, 2),
1758 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1760 else if (get_irn_op(a) == op_Minus) {
1762 get_irn_dbg_info(n),
1768 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1770 else if (get_irn_op(b) == op_Minus) {
1772 get_irn_dbg_info(n),
1778 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1780 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1781 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1782 ir_node *ma = get_Mul_left(a);
1783 ir_node *mb = get_Mul_right(a);
1786 ir_node *blk = get_irn_n(n, -1);
1788 get_irn_dbg_info(n), current_ir_graph, blk,
1791 get_irn_dbg_info(n), current_ir_graph, blk,
1793 new_r_Const_long(current_ir_graph, blk, mode, 1),
1796 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1799 ir_node *blk = get_irn_n(n, -1);
1801 get_irn_dbg_info(n), current_ir_graph, blk,
1804 get_irn_dbg_info(n), current_ir_graph, blk,
1806 new_r_Const_long(current_ir_graph, blk, mode, 1),
1809 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1812 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1813 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1814 ir_node *ma = get_Mul_left(b);
1815 ir_node *mb = get_Mul_right(b);
1818 ir_node *blk = get_irn_n(n, -1);
1820 get_irn_dbg_info(n), current_ir_graph, blk,
1823 get_irn_dbg_info(n), current_ir_graph, blk,
1825 new_r_Const_long(current_ir_graph, blk, mode, 1),
1828 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1831 ir_node *blk = get_irn_n(n, -1);
1833 get_irn_dbg_info(n), current_ir_graph, blk,
1836 get_irn_dbg_info(n), current_ir_graph, blk,
1838 new_r_Const_long(current_ir_graph, blk, mode, 1),
1841 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1849 * Do the AddSub optimization, then Transform
1850 * Sub(0,a) -> Minus(a)
1851 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1853 static ir_node *transform_node_Sub(ir_node *n)
1859 n = transform_node_AddSub(n);
1861 mode = get_irn_mode(n);
1862 a = get_Sub_left(n);
1863 b = get_Sub_right(n);
1864 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1866 get_irn_dbg_info(n),
1871 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1873 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1874 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1875 ir_node *ma = get_Mul_left(a);
1876 ir_node *mb = get_Mul_right(a);
1879 ir_node *blk = get_irn_n(n, -1);
1881 get_irn_dbg_info(n),
1882 current_ir_graph, blk,
1885 get_irn_dbg_info(n),
1886 current_ir_graph, blk,
1888 new_r_Const_long(current_ir_graph, blk, mode, 1),
1891 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1894 ir_node *blk = get_irn_n(n, -1);
1896 get_irn_dbg_info(n),
1897 current_ir_graph, blk,
1900 get_irn_dbg_info(n),
1901 current_ir_graph, blk,
1903 new_r_Const_long(current_ir_graph, blk, mode, 1),
1906 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1914 * Transform Mul(a,-1) into -a.
1915 * Do architecture dependent optimizations on Mul nodes
1917 static ir_node *transform_node_Mul(ir_node *n) {
1919 ir_mode *mode = get_irn_mode(n);
1921 if (mode_is_signed(mode)) {
1923 ir_node *a = get_Mul_left(n);
1924 ir_node *b = get_Mul_right(n);
1926 if (value_of(a) == get_mode_minus_one(mode))
1928 else if (value_of(b) == get_mode_minus_one(mode))
1931 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1932 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1936 return arch_dep_replace_mul_with_shifts(n);
1940 * transform a Div Node
1942 static ir_node *transform_node_Div(ir_node *n)
1944 tarval *tv = value_of(n);
1947 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1949 if (tv != tarval_bad) {
1950 value = new_Const(get_tarval_mode(tv), tv);
1952 DBG_OPT_CSTEVAL(n, value);
1954 else /* Try architecture dependent optimization */
1955 value = arch_dep_replace_div_by_const(n);
1958 /* Turn Div into a tuple (mem, bad, value) */
1959 ir_node *mem = get_Div_mem(n);
1961 turn_into_tuple(n, pn_Div_max);
1962 set_Tuple_pred(n, pn_Div_M, mem);
1963 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1964 set_Tuple_pred(n, pn_Div_res, value);
1970 * transform a Mod node
1972 static ir_node *transform_node_Mod(ir_node *n)
1974 tarval *tv = value_of(n);
1977 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1979 if (tv != tarval_bad) {
1980 value = new_Const(get_tarval_mode(tv), tv);
1982 DBG_OPT_CSTEVAL(n, value);
1984 else /* Try architecture dependent optimization */
1985 value = arch_dep_replace_mod_by_const(n);
1988 /* Turn Mod into a tuple (mem, bad, value) */
1989 ir_node *mem = get_Mod_mem(n);
1991 turn_into_tuple(n, pn_Mod_max);
1992 set_Tuple_pred(n, pn_Mod_M, mem);
1993 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1994 set_Tuple_pred(n, pn_Mod_res, value);
2000 * transform a DivMod node
2002 static ir_node *transform_node_DivMod(ir_node *n)
2006 ir_node *a = get_DivMod_left(n);
2007 ir_node *b = get_DivMod_right(n);
2008 ir_mode *mode = get_irn_mode(a);
2009 tarval *ta = value_of(a);
2010 tarval *tb = value_of(b);
2012 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2015 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2017 if (tb != tarval_bad) {
2018 if (tb == get_mode_one(get_tarval_mode(tb))) {
2019 b = new_Const (mode, get_mode_null(mode));
2022 DBG_OPT_CSTEVAL(n, b);
2024 else if (ta != tarval_bad) {
2025 tarval *resa, *resb;
2026 resa = tarval_div (ta, tb);
2027 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2028 Jmp for X result!? */
2029 resb = tarval_mod (ta, tb);
2030 if (resb == tarval_bad) return n; /* Causes exception! */
2031 a = new_Const (mode, resa);
2032 b = new_Const (mode, resb);
2035 DBG_OPT_CSTEVAL(n, a);
2036 DBG_OPT_CSTEVAL(n, b);
2038 else { /* Try architecture dependent optimization */
2039 arch_dep_replace_divmod_by_const(&a, &b, n);
2040 evaluated = a != NULL;
2042 } else if (ta == get_mode_null(mode)) {
2043 /* 0 / non-Const = 0 */
2048 if (evaluated) { /* replace by tuple */
2049 ir_node *mem = get_DivMod_mem(n);
2050 turn_into_tuple(n, pn_DivMod_max);
2051 set_Tuple_pred(n, pn_DivMod_M, mem);
2052 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2053 set_Tuple_pred(n, pn_DivMod_res_div, a);
2054 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2061 * Optimize Abs(x) into x if x is Confirmed >= 0
2062 * Optimize Abs(x) into -x if x is Confirmed <= 0
2064 static ir_node *transform_node_Abs(ir_node *n)
2067 ir_node *a = get_Abs_op(n);
2068 value_classify sign = classify_value_sign(a);
2070 if (sign == VALUE_NEGATIVE) {
2071 ir_mode *mode = get_irn_mode(n);
2074 * We can replace the Abs by -x here.
2075 * We even could add a new Confirm here.
2077 * Note that -x would create a new node, so we could
2078 * not run it in the equivalent_node() context.
2080 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2081 get_irn_n(n, -1), a, mode);
2083 DBG_OPT_CONFIRM(oldn, n);
2085 else if (sign == VALUE_POSITIVE) {
2086 /* n is positive, Abs is not needed */
2089 DBG_OPT_CONFIRM(oldn, n);
2096 * transform a Cond node
2098 static ir_node *transform_node_Cond(ir_node *n)
2100 /* Replace the Cond by a Jmp if it branches on a constant
2103 ir_node *a = get_Cond_selector(n);
2104 tarval *ta = value_of(a);
2106 /* we need block info which is not available in floating irgs */
2107 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2110 if ((ta != tarval_bad) &&
2111 (get_irn_mode(a) == mode_b) &&
2112 (get_opt_unreachable_code())) {
2113 /* It's a boolean Cond, branching on a boolean constant.
2114 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2115 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2116 turn_into_tuple(n, pn_Cond_max);
2117 if (ta == tarval_b_true) {
2118 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2119 set_Tuple_pred(n, pn_Cond_true, jmp);
2121 set_Tuple_pred(n, pn_Cond_false, jmp);
2122 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2124 /* We might generate an endless loop, so keep it alive. */
2125 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2133 static ir_node *transform_node_Eor(ir_node *n)
2136 ir_node *a = get_Eor_left(n);
2137 ir_node *b = get_Eor_right(n);
2138 ir_mode *mode = get_irn_mode(n);
2142 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2143 mode, get_mode_null(mode));
2144 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2146 else if ((mode == mode_b)
2147 && (get_irn_op(a) == op_Proj)
2148 && (get_irn_mode(a) == mode_b)
2149 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2150 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2151 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2152 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2153 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2155 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2157 else if ((mode == mode_b)
2158 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2159 /* The Eor is a Not. Replace it by a Not. */
2160 /* ????!!!Extend to bitfield 1111111. */
2161 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2170 * Transform a boolean Not.
2172 static ir_node *transform_node_Not(ir_node *n)
2175 ir_node *a = get_Not_op(n);
2177 if ( (get_irn_mode(n) == mode_b)
2178 && (get_irn_op(a) == op_Proj)
2179 && (get_irn_mode(a) == mode_b)
2180 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2181 /* We negate a Cmp. The Cmp has the negated result anyways! */
2182 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2183 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2191 * Transform a Cast_type(Const) into a new Const_type
2193 static ir_node *transform_node_Cast(ir_node *n) {
2195 ir_node *pred = get_Cast_op(n);
2196 ir_type *tp = get_irn_type(n);
2198 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2199 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2200 get_Const_tarval(pred), tp);
2201 DBG_OPT_CSTEVAL(oldn, n);
2202 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2203 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2204 get_SymConst_kind(pred), tp);
2205 DBG_OPT_CSTEVAL(oldn, n);
2212 * Transform a Proj(Div) with a non-zero value.
2213 * Removes the exceptions and routes the memory to the NoMem node.
2215 static ir_node *transform_node_Proj_Div(ir_node *proj)
2217 ir_node *n = get_Proj_pred(proj);
2218 ir_node *b = get_Div_right(n);
2221 if (value_not_zero(b)) {
2222 /* div(x, y) && y != 0 */
2223 proj_nr = get_Proj_proj(proj);
2225 /* this node may float */
2226 set_irn_pinned(n, op_pin_state_floats);
2228 if (proj_nr == pn_Div_X_except) {
2229 /* we found an exception handler, remove it */
2230 DBG_OPT_EXC_REM(proj);
2232 } else if (proj_nr == pn_Div_M) {
2233 /* the memory Proj can be removed */
2234 ir_node *res = get_Div_mem(n);
2235 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2244 * Transform a Proj(Mod) with a non-zero value.
2245 * Removes the exceptions and routes the memory to the NoMem node.
2247 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2249 ir_node *n = get_Proj_pred(proj);
2250 ir_node *b = get_Mod_right(n);
2253 if (value_not_zero(b)) {
2254 /* mod(x, y) && y != 0 */
2255 proj_nr = get_Proj_proj(proj);
2257 /* this node may float */
2258 set_irn_pinned(n, op_pin_state_floats);
2260 if (proj_nr == pn_Mod_X_except) {
2261 /* we found an exception handler, remove it */
2262 DBG_OPT_EXC_REM(proj);
2264 } else if (proj_nr == pn_Mod_M) {
2265 /* the memory Proj can be removed */
2266 ir_node *res = get_Mod_mem(n);
2267 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2271 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2272 /* a % a = 0 if a != 0 */
2273 ir_mode *mode = get_irn_mode(proj);
2274 ir_node *res = new_Const(mode, get_mode_null(mode));
2276 DBG_OPT_CSTEVAL(n, res);
2284 * Transform a Proj(DivMod) with a non-zero value.
2285 * Removes the exceptions and routes the memory to the NoMem node.
2287 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2289 ir_node *n = get_Proj_pred(proj);
2290 ir_node *b = get_DivMod_right(n);
2293 if (value_not_zero(b)) {
2294 /* DivMod(x, y) && y != 0 */
2295 proj_nr = get_Proj_proj(proj);
2297 /* this node may float */
2298 set_irn_pinned(n, op_pin_state_floats);
2300 if (proj_nr == pn_DivMod_X_except) {
2301 /* we found an exception handler, remove it */
2302 DBG_OPT_EXC_REM(proj);
2305 else if (proj_nr == pn_DivMod_M) {
2306 /* the memory Proj can be removed */
2307 ir_node *res = get_DivMod_mem(n);
2308 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2312 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2313 /* a % a = 0 if a != 0 */
2314 ir_mode *mode = get_irn_mode(proj);
2315 ir_node *res = new_Const(mode, get_mode_null(mode));
2317 DBG_OPT_CSTEVAL(n, res);
2325 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2327 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2329 if (get_opt_unreachable_code()) {
2330 ir_node *n = get_Proj_pred(proj);
2331 ir_node *b = get_Cond_selector(n);
2333 if (mode_is_int(get_irn_mode(b))) {
2334 tarval *tb = value_of(b);
2336 if (tb != tarval_bad) {
2337 /* we have a constant switch */
2338 long num = get_Proj_proj(proj);
2340 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2341 if (get_tarval_long(tb) == num) {
2342 /* Do NOT create a jump here, or we will have 2 control flow ops
2343 * in a block. This case is optimized away in optimize_cf(). */
2347 /* this case will NEVER be taken, kill it */
2358 * Normalizes and optimizes Cmp nodes.
2360 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2362 if (get_opt_reassociation()) {
2363 ir_node *n = get_Proj_pred(proj);
2364 ir_node *left = get_Cmp_left(n);
2365 ir_node *right = get_Cmp_right(n);
2369 ir_mode *mode = NULL;
2370 long proj_nr = get_Proj_proj(proj);
2373 * First step: normalize the compare op
2374 * by placing the constant on the right site
2375 * or moving the lower address node to the left.
2376 * We ignore the case that both are constants
2377 * this case should be optimized away.
2379 if (get_irn_op(right) == op_Const)
2381 else if (get_irn_op(left) == op_Const) {
2386 proj_nr = get_inversed_pnc(proj_nr);
2389 else if (left > right) {
2395 proj_nr = get_inversed_pnc(proj_nr);
2400 * Second step: Try to reduce the magnitude
2401 * of a constant. This may help to generate better code
2402 * later and may help to normalize more compares.
2403 * Of course this is only possible for integer values.
2406 mode = get_irn_mode(c);
2407 tv = get_Const_tarval(c);
2409 if (tv != tarval_bad) {
2410 /* the following optimization is possible on modes without Overflow
2411 * on Unary Minus or on == and !=:
2412 * -a CMP c ==> a swap(CMP) -c
2414 * Beware: for two-complement Overflow may occur, so only == and != can
2415 * be optimized, see this:
2416 * -MININT < 0 =/=> MININT > 0 !!!
2418 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2419 (!mode_overflow_on_unary_Minus(mode) ||
2420 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2421 left = get_Minus_op(left);
2422 tv = tarval_sub(get_mode_null(mode), tv);
2424 proj_nr = get_inversed_pnc(proj_nr);
2428 /* for integer modes, we have more */
2429 if (mode_is_int(mode)) {
2430 /* Ne includes Unordered which is not possible on integers.
2431 * However, frontends often use this wrong, so fix it here */
2432 if (proj_nr & pn_Cmp_Uo) {
2433 proj_nr &= ~pn_Cmp_Uo;
2434 set_Proj_proj(proj, proj_nr);
2437 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2438 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2439 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2440 tv = tarval_sub(tv, get_mode_one(mode));
2442 proj_nr ^= pn_Cmp_Eq;
2445 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2446 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2447 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2448 tv = tarval_add(tv, get_mode_one(mode));
2450 proj_nr ^= pn_Cmp_Eq;
2454 /* the following reassociations work only for == and != */
2455 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2457 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2458 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2459 right = get_Sub_right(left);
2460 left = get_Sub_left(left);
2462 tv = value_of(right);
2466 if (tv != tarval_bad) {
2467 ir_op *op = get_irn_op(left);
2469 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2471 ir_node *c1 = get_Sub_right(left);
2472 tarval *tv2 = value_of(c1);
2474 if (tv2 != tarval_bad) {
2475 tv2 = tarval_add(tv, value_of(c1));
2477 if (tv2 != tarval_bad) {
2478 left = get_Sub_left(left);
2484 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2485 else if (op == op_Add) {
2486 ir_node *a_l = get_Add_left(left);
2487 ir_node *a_r = get_Add_right(left);
2491 if (get_irn_op(a_l) == op_Const) {
2493 tv2 = value_of(a_l);
2497 tv2 = value_of(a_r);
2500 if (tv2 != tarval_bad) {
2501 tv2 = tarval_sub(tv, tv2);
2503 if (tv2 != tarval_bad) {
2510 /* -a == c ==> a == -c, -a != c ==> a != -c */
2511 else if (op == op_Minus) {
2512 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2514 if (tv2 != tarval_bad) {
2515 left = get_Minus_op(left);
2522 /* the following reassociations work only for <= */
2523 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2524 if (tv != tarval_bad) {
2525 ir_op *op = get_irn_op(left);
2527 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2537 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2539 if (changed & 2) /* need a new Const */
2540 right = new_Const(mode, tv);
2542 /* create a new compare */
2543 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2546 set_Proj_pred(proj, n);
2547 set_Proj_proj(proj, proj_nr);
2554 * Does all optimizations on nodes that must be done on it's Proj's
2555 * because of creating new nodes.
2557 static ir_node *transform_node_Proj(ir_node *proj)
2559 ir_node *n = get_Proj_pred(proj);
2561 switch (get_irn_opcode(n)) {
2563 return transform_node_Proj_Div(proj);
2566 return transform_node_Proj_Mod(proj);
2569 return transform_node_Proj_DivMod(proj);
2572 return transform_node_Proj_Cond(proj);
2575 return transform_node_Proj_Cmp(proj);
2578 /* should not happen, but if it does will be optimized away */
2579 return equivalent_node_Proj(proj);
2588 * returns the operands of a commutative bin-op, if one operand is
2589 * a const, it is returned as the second one.
2591 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2593 ir_node *op_a = get_binop_left(binop);
2594 ir_node *op_b = get_binop_right(binop);
2596 assert(is_op_commutative(get_irn_op(binop)));
2598 if (get_irn_op(op_a) == op_Const) {
2609 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2610 * Such pattern may arise in bitfield stores.
2612 * value c4 value c4 & c2
2613 * AND c3 AND c1 | c3
2618 static ir_node *transform_node_Or_bf_store(ir_node *or)
2622 ir_node *and_l, *c3;
2623 ir_node *value, *c4;
2624 ir_node *new_and, *new_const, *block;
2625 ir_mode *mode = get_irn_mode(or);
2627 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2629 get_comm_Binop_Ops(or, &and, &c1);
2630 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2633 get_comm_Binop_Ops(and, &or_l, &c2);
2634 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2637 get_comm_Binop_Ops(or_l, &and_l, &c3);
2638 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2641 get_comm_Binop_Ops(and_l, &value, &c4);
2642 if (get_irn_op(c4) != op_Const)
2645 /* ok, found the pattern, check for conditions */
2646 assert(mode == get_irn_mode(and));
2647 assert(mode == get_irn_mode(or_l));
2648 assert(mode == get_irn_mode(and_l));
2650 tv1 = get_Const_tarval(c1);
2651 tv2 = get_Const_tarval(c2);
2652 tv3 = get_Const_tarval(c3);
2653 tv4 = get_Const_tarval(c4);
2655 tv = tarval_or(tv4, tv2);
2656 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2657 /* have at least one 0 at the same bit position */
2661 n_tv4 = tarval_not(tv4);
2662 if (tv3 != tarval_and(tv3, n_tv4)) {
2663 /* bit in the or_mask is outside the and_mask */
2667 n_tv2 = tarval_not(tv2);
2668 if (tv1 != tarval_and(tv1, n_tv2)) {
2669 /* bit in the or_mask is outside the and_mask */
2673 /* ok, all conditions met */
2674 block = get_irn_n(or, -1);
2676 new_and = new_r_And(current_ir_graph, block,
2677 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2679 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2681 set_Or_left(or, new_and);
2682 set_Or_right(or, new_const);
2684 /* check for more */
2685 return transform_node_Or_bf_store(or);
2689 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2691 static ir_node *transform_node_Or_Rot(ir_node *or)
2693 ir_mode *mode = get_irn_mode(or);
2694 ir_node *shl, *shr, *block;
2695 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2698 if (! mode_is_int(mode))
2701 shl = get_binop_left(or);
2702 shr = get_binop_right(or);
2704 if (get_irn_op(shl) == op_Shr) {
2705 if (get_irn_op(shr) != op_Shl)
2712 else if (get_irn_op(shl) != op_Shl)
2714 else if (get_irn_op(shr) != op_Shr)
2717 x = get_Shl_left(shl);
2718 if (x != get_Shr_left(shr))
2721 c1 = get_Shl_right(shl);
2722 c2 = get_Shr_right(shr);
2723 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2724 tv1 = get_Const_tarval(c1);
2725 if (! tarval_is_long(tv1))
2728 tv2 = get_Const_tarval(c2);
2729 if (! tarval_is_long(tv2))
2732 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2733 != get_mode_size_bits(mode))
2736 /* yet, condition met */
2737 block = get_irn_n(or, -1);
2739 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2741 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2744 else if (get_irn_op(c1) == op_Sub) {
2748 if (get_Sub_right(sub) != v)
2751 c1 = get_Sub_left(sub);
2752 if (get_irn_op(c1) != op_Const)
2755 tv1 = get_Const_tarval(c1);
2756 if (! tarval_is_long(tv1))
2759 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2762 /* yet, condition met */
2763 block = get_nodes_block(or);
2765 /* a Rot right is not supported, so use a rot left */
2766 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2768 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2771 else if (get_irn_op(c2) == op_Sub) {
2775 c1 = get_Sub_left(sub);
2776 if (get_irn_op(c1) != op_Const)
2779 tv1 = get_Const_tarval(c1);
2780 if (! tarval_is_long(tv1))
2783 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2786 /* yet, condition met */
2787 block = get_irn_n(or, -1);
2790 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2792 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2802 static ir_node *transform_node_Or(ir_node *or)
2804 or = transform_node_Or_bf_store(or);
2805 or = transform_node_Or_Rot(or);
2811 static ir_node *transform_node(ir_node *n);
2814 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2816 * Should be moved to reassociation?
2818 static ir_node *transform_node_shift(ir_node *n)
2820 ir_node *left, *right;
2821 tarval *tv1, *tv2, *res;
2823 int modulo_shf, flag;
2825 left = get_binop_left(n);
2827 /* different operations */
2828 if (get_irn_op(left) != get_irn_op(n))
2831 right = get_binop_right(n);
2832 tv1 = value_of(right);
2833 if (tv1 == tarval_bad)
2836 tv2 = value_of(get_binop_right(left));
2837 if (tv2 == tarval_bad)
2840 res = tarval_add(tv1, tv2);
2842 /* beware: a simple replacement works only, if res < modulo shift */
2843 mode = get_irn_mode(n);
2847 modulo_shf = get_mode_modulo_shift(mode);
2848 if (modulo_shf > 0) {
2849 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2851 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2858 /* ok, we can replace it */
2859 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2861 in[0] = get_binop_left(left);
2862 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2864 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2866 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2868 return transform_node(irn);
2873 #define transform_node_Shr transform_node_shift
2874 #define transform_node_Shrs transform_node_shift
2875 #define transform_node_Shl transform_node_shift
2878 * Remove dead blocks and nodes in dead blocks
2879 * in keep alive list. We do not generate a new End node.
2881 static ir_node *transform_node_End(ir_node *n) {
2882 int i, n_keepalives = get_End_n_keepalives(n);
2884 for (i = 0; i < n_keepalives; ++i) {
2885 ir_node *ka = get_End_keepalive(n, i);
2887 if (is_Block_dead(ka)) {
2888 set_End_keepalive(n, i, new_Bad());
2891 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2892 set_End_keepalive(n, i, new_Bad());
2898 * Optimize a Mux into some simpler cases.
2900 static ir_node *transform_node_Mux(ir_node *n)
2902 ir_node *oldn = n, *sel = get_Mux_sel(n);
2903 ir_mode *mode = get_irn_mode(n);
2905 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2906 ir_node *cmp = get_Proj_pred(sel);
2907 long proj_nr = get_Proj_proj(sel);
2908 ir_node *f = get_Mux_false(n);
2909 ir_node *t = get_Mux_true(n);
2911 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2912 ir_node *block = get_irn_n(n, -1);
2915 * Note: normalization puts the constant on the right site,
2916 * so we check only one case.
2918 * Note further that these optimization work even for floating point
2919 * with NaN's because -NaN == NaN.
2920 * However, if +0 and -0 is handled differently, we cannot use the first one.
2922 if (get_irn_op(f) == op_Minus &&
2923 get_Minus_op(f) == t &&
2924 get_Cmp_left(cmp) == t) {
2926 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2927 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2928 n = new_rd_Abs(get_irn_dbg_info(n),
2932 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2935 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2936 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2937 n = new_rd_Abs(get_irn_dbg_info(n),
2941 n = new_rd_Minus(get_irn_dbg_info(n),
2946 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2950 else if (get_irn_op(t) == op_Minus &&
2951 get_Minus_op(t) == f &&
2952 get_Cmp_left(cmp) == f) {
2954 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2955 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2956 n = new_rd_Abs(get_irn_dbg_info(n),
2960 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2963 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2964 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
2965 n = new_rd_Abs(get_irn_dbg_info(n),
2969 n = new_rd_Minus(get_irn_dbg_info(n),
2974 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2979 if (mode_is_int(mode) && mode_is_signed(mode) &&
2980 get_mode_arithmetic(mode) == irma_twos_complement) {
2981 ir_node *x = get_Cmp_left(cmp);
2983 /* the following optimization works only with signed integer two-complement mode */
2985 if (mode == get_irn_mode(x)) {
2987 * FIXME: this restriction is two rigid, as it would still
2988 * work if mode(x) = Hs and mode == Is, but at least it removes
2991 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2992 classify_Const(t) == CNST_ALL_ONE &&
2993 classify_Const(f) == CNST_NULL) {
2995 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2999 n = new_rd_Shrs(get_irn_dbg_info(n),
3000 current_ir_graph, block, x,
3001 new_r_Const_long(current_ir_graph, block, mode_Iu,
3002 get_mode_size_bits(mode) - 1),
3004 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3007 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3008 classify_Const(t) == CNST_ONE &&
3009 classify_Const(f) == CNST_NULL) {
3011 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3015 n = new_rd_Shr(get_irn_dbg_info(n),
3016 current_ir_graph, block,
3017 new_r_Minus(current_ir_graph, block, x, mode),
3018 new_r_Const_long(current_ir_graph, block, mode_Iu,
3019 get_mode_size_bits(mode) - 1),
3021 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3028 return arch_transform_node_Mux(n);
3032 * Tries several [inplace] [optimizing] transformations and returns an
3033 * equivalent node. The difference to equivalent_node() is that these
3034 * transformations _do_ generate new nodes, and thus the old node must
3035 * not be freed even if the equivalent node isn't the old one.
3037 static ir_node *transform_node(ir_node *n)
3039 if (n->op->ops.transform_node)
3040 n = n->op->ops.transform_node(n);
3045 * sSets the default transform node operation for an ir_op_ops.
3047 * @param code the opcode for the default operation
3048 * @param ops the operations initialized
3053 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3057 ops->transform_node = transform_node_##a; \
3089 /* **************** Common Subexpression Elimination **************** */
3091 /** The size of the hash table used, should estimate the number of nodes
3093 #define N_IR_NODES 512
3095 /** Compares the attributes of two Const nodes. */
3096 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3098 return (get_Const_tarval(a) != get_Const_tarval(b))
3099 || (get_Const_type(a) != get_Const_type(b));
3102 /** Compares the attributes of two Proj nodes. */
3103 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3105 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3108 /** Compares the attributes of two Filter nodes. */
3109 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3111 return get_Filter_proj(a) != get_Filter_proj(b);
3114 /** Compares the attributes of two Alloc nodes. */
3115 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3117 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3118 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3121 /** Compares the attributes of two Free nodes. */
3122 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3124 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3125 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3128 /** Compares the attributes of two SymConst nodes. */
3129 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3131 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3132 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3133 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3136 /** Compares the attributes of two Call nodes. */
3137 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3139 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3142 /** Compares the attributes of two Sel nodes. */
3143 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3145 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3146 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3147 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3148 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3149 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3152 /** Compares the attributes of two Phi nodes. */
3153 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3155 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3158 /** Compares the attributes of two Cast nodes. */
3159 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3161 return get_Cast_type(a) != get_Cast_type(b);
3164 /** Compares the attributes of two Load nodes. */
3165 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3167 if (get_Load_volatility(a) == volatility_is_volatile ||
3168 get_Load_volatility(b) == volatility_is_volatile)
3169 /* NEVER do CSE on volatile Loads */
3172 return get_Load_mode(a) != get_Load_mode(b);
3175 /** Compares the attributes of two Store nodes. */
3176 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3178 /* NEVER do CSE on volatile Stores */
3179 return (get_Store_volatility(a) == volatility_is_volatile ||
3180 get_Store_volatility(b) == volatility_is_volatile);
3183 /** Compares the attributes of two Confirm nodes. */
3184 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3186 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3190 * Set the default node attribute compare operation for an ir_op_ops.
3192 * @param code the opcode for the default operation
3193 * @param ops the operations initialized
3198 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3202 ops->node_cmp_attr = node_cmp_attr_##a; \
3228 * Compare function for two nodes in the hash table. Gets two
3229 * nodes as parameters. Returns 0 if the nodes are a cse.
3231 int identities_cmp(const void *elt, const void *key)
3239 if (a == b) return 0;
3241 if ((get_irn_op(a) != get_irn_op(b)) ||
3242 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3244 /* compare if a's in and b's in are of equal length */
3245 irn_arity_a = get_irn_intra_arity (a);
3246 if (irn_arity_a != get_irn_intra_arity(b))
3249 /* for block-local cse and op_pin_state_pinned nodes: */
3250 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3251 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3255 /* compare a->in[0..ins] with b->in[0..ins] */
3256 for (i = 0; i < irn_arity_a; i++)
3257 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3261 * here, we already now that the nodes are identical except their
3264 if (a->op->ops.node_cmp_attr)
3265 return a->op->ops.node_cmp_attr(a, b);
3271 * Calculate a hash value of a node.
3274 ir_node_hash (ir_node *node)
3279 if (node->op == op_Const) {
3280 /* special value for const, as they only differ in their tarval. */
3281 h = HASH_PTR(node->attr.con.tv);
3282 h = 9*h + HASH_PTR(get_irn_mode(node));
3283 } else if (node->op == op_SymConst) {
3284 /* special value for const, as they only differ in their symbol. */
3285 h = HASH_PTR(node->attr.i.sym.type_p);
3286 h = 9*h + HASH_PTR(get_irn_mode(node));
3289 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3290 h = irn_arity = get_irn_intra_arity(node);
3292 /* consider all in nodes... except the block if not a control flow. */
3293 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3294 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3298 h = 9*h + HASH_PTR(get_irn_mode(node));
3300 h = 9*h + HASH_PTR(get_irn_op(node));
3307 new_identities(void) {
3308 return new_pset(identities_cmp, N_IR_NODES);
3312 del_identities(pset *value_table) {
3313 del_pset(value_table);
3317 * Return the canonical node computing the same value as n.
3318 * Looks up the node in a hash table.
3320 * For Const nodes this is performed in the constructor, too. Const
3321 * nodes are extremely time critical because of their frequent use in
3322 * constant string arrays.
3324 static INLINE ir_node *
3325 identify (pset *value_table, ir_node *n)
3329 if (!value_table) return n;
3331 if (get_opt_reassociation()) {
3332 if (is_op_commutative(get_irn_op(n))) {
3333 ir_node *l = get_binop_left(n);
3334 ir_node *r = get_binop_right(n);
3336 /* for commutative operators perform a OP b == b OP a */
3338 set_binop_left(n, r);
3339 set_binop_right(n, l);
3344 o = pset_find (value_table, n, ir_node_hash (n));
3353 * During construction we set the op_pin_state_pinned flag in the graph right when the
3354 * optimization is performed. The flag turning on procedure global cse could
3355 * be changed between two allocations. This way we are safe.
3357 static INLINE ir_node *
3358 identify_cons (pset *value_table, ir_node *n) {
3361 n = identify(value_table, n);
3362 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3363 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3368 * Return the canonical node computing the same value as n.
3369 * Looks up the node in a hash table, enters it in the table
3370 * if it isn't there yet.
3373 identify_remember (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 /* lookup or insert in hash table with given hash key. */
3393 o = pset_insert (value_table, n, ir_node_hash (n));
3403 add_identities (pset *value_table, ir_node *node) {
3404 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3405 identify_remember (value_table, node);
3409 * garbage in, garbage out. If a node has a dead input, i.e., the
3410 * Bad node is input to the node, return the Bad node.
3412 static INLINE ir_node *
3413 gigo (ir_node *node)
3416 ir_op *op = get_irn_op(node);
3418 /* remove garbage blocks by looking at control flow that leaves the block
3419 and replacing the control flow by Bad. */
3420 if (get_irn_mode(node) == mode_X) {
3421 ir_node *block = get_nodes_block(skip_Proj(node));
3423 /* Don't optimize nodes in immature blocks. */
3424 if (!get_Block_matured(block)) return node;
3425 /* Don't optimize End, may have Bads. */
3426 if (op == op_End) return node;
3428 if (is_Block(block)) {
3429 irn_arity = get_irn_arity(block);
3430 for (i = 0; i < irn_arity; i++) {
3431 if (!is_Bad(get_irn_n(block, i)))
3434 if (i == irn_arity) return new_Bad();
3438 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3439 blocks predecessors is dead. */
3440 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3441 irn_arity = get_irn_arity(node);
3444 * Beware: we can only read the block of a non-floating node.
3446 if (is_irn_pinned_in_irg(node) &&
3447 is_Block_dead(get_nodes_block(node)))
3450 for (i = 0; i < irn_arity; i++) {
3451 ir_node *pred = get_irn_n(node, i);
3455 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3456 return new_Unknown(get_irn_mode(node));
3460 /* With this code we violate the agreement that local_optimize
3461 only leaves Bads in Block, Phi and Tuple nodes. */
3462 /* If Block has only Bads as predecessors it's garbage. */
3463 /* If Phi has only Bads as predecessors it's garbage. */
3464 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3465 irn_arity = get_irn_arity(node);
3466 for (i = 0; i < irn_arity; i++) {
3467 if (!is_Bad(get_irn_n(node, i))) break;
3469 if (i == irn_arity) node = new_Bad();
3477 * These optimizations deallocate nodes from the obstack.
3478 * It can only be called if it is guaranteed that no other nodes
3479 * reference this one, i.e., right after construction of a node.
3481 * current_ir_graph must be set to the graph of the node!
3484 optimize_node(ir_node *n)
3488 opcode iro = get_irn_opcode(n);
3490 /* Always optimize Phi nodes: part of the construction. */
3491 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3493 /* constant expression evaluation / constant folding */
3494 if (get_opt_constant_folding()) {
3495 /* neither constants nor Tuple values can be evaluated */
3496 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3497 /* try to evaluate */
3498 tv = computed_value(n);
3499 if (tv != tarval_bad) {
3501 ir_type *old_tp = get_irn_type(n);
3502 int i, arity = get_irn_arity(n);
3506 * Try to recover the type of the new expression.
3508 for (i = 0; i < arity && !old_tp; ++i)
3509 old_tp = get_irn_type(get_irn_n(n, i));
3512 * we MUST copy the node here temporary, because it's still needed
3513 * for DBG_OPT_CSTEVAL
3515 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3516 oldn = alloca(node_size);
3518 memcpy(oldn, n, node_size);
3519 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3521 /* ARG, copy the in array, we need it for statistics */
3522 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3524 /* note the inplace edges module */
3525 edges_node_deleted(n, current_ir_graph);
3527 /* evaluation was successful -- replace the node. */
3528 obstack_free(current_ir_graph->obst, n);
3529 nw = new_Const(get_tarval_mode (tv), tv);
3531 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3532 set_Const_type(nw, old_tp);
3533 DBG_OPT_CSTEVAL(oldn, nw);
3539 /* remove unnecessary nodes */
3540 if (get_opt_constant_folding() ||
3541 (iro == iro_Phi) || /* always optimize these nodes. */
3543 (iro == iro_Proj) ||
3544 (iro == iro_Block) ) /* Flags tested local. */
3545 n = equivalent_node (n);
3547 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3549 /* Common Subexpression Elimination.
3551 * Checks whether n is already available.
3552 * The block input is used to distinguish different subexpressions. Right
3553 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3554 * subexpressions within a block.
3557 n = identify_cons (current_ir_graph->value_table, n);
3560 edges_node_deleted(oldn, current_ir_graph);
3562 /* We found an existing, better node, so we can deallocate the old node. */
3563 obstack_free (current_ir_graph->obst, oldn);
3568 /* Some more constant expression evaluation that does not allow to
3570 iro = get_irn_opcode(n);
3571 if (get_opt_constant_folding() ||
3572 (iro == iro_Cond) ||
3573 (iro == iro_Proj) ||
3574 (iro == iro_Sel)) /* Flags tested local. */
3575 n = transform_node (n);
3577 /* Remove nodes with dead (Bad) input.
3578 Run always for transformation induced Bads. */
3581 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3582 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3583 n = identify_remember (current_ir_graph->value_table, n);
3591 * These optimizations never deallocate nodes (in place). This can cause dead
3592 * nodes lying on the obstack. Remove these by a dead node elimination,
3593 * i.e., a copying garbage collection.
3596 optimize_in_place_2 (ir_node *n)
3600 opcode iro = get_irn_opcode(n);
3602 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3604 /* constant expression evaluation / constant folding */
3605 if (get_opt_constant_folding()) {
3606 /* neither constants nor Tuple values can be evaluated */
3607 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3608 /* try to evaluate */
3609 tv = computed_value(n);
3610 if (tv != tarval_bad) {
3611 /* evaluation was successful -- replace the node. */
3612 ir_type *old_tp = get_irn_type(n);
3613 int i, arity = get_irn_arity(n);
3616 * Try to recover the type of the new expression.
3618 for (i = 0; i < arity && !old_tp; ++i)
3619 old_tp = get_irn_type(get_irn_n(n, i));
3621 n = new_Const(get_tarval_mode(tv), tv);
3623 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3624 set_Const_type(n, old_tp);
3626 DBG_OPT_CSTEVAL(oldn, n);
3632 /* remove unnecessary nodes */
3633 if (get_opt_constant_folding() ||
3634 (iro == iro_Phi) || /* always optimize these nodes. */
3635 (iro == iro_Id) || /* ... */
3636 (iro == iro_Proj) || /* ... */
3637 (iro == iro_Block) ) /* Flags tested local. */
3638 n = equivalent_node(n);
3640 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3642 /** common subexpression elimination **/
3643 /* Checks whether n is already available. */
3644 /* The block input is used to distinguish different subexpressions. Right
3645 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3646 subexpressions within a block. */
3647 if (get_opt_cse()) {
3648 n = identify(current_ir_graph->value_table, n);
3651 /* Some more constant expression evaluation. */
3652 iro = get_irn_opcode(n);
3653 if (get_opt_constant_folding() ||
3654 (iro == iro_Cond) ||
3655 (iro == iro_Proj) ||
3656 (iro == iro_Sel)) /* Flags tested local. */
3657 n = transform_node(n);
3659 /* Remove nodes with dead (Bad) input.
3660 Run always for transformation induced Bads. */
3663 /* Now we can verify the node, as it has no dead inputs any more. */
3666 /* Now we have a legal, useful node. Enter it in hash table for cse.
3667 Blocks should be unique anyways. (Except the successor of start:
3668 is cse with the start block!) */
3669 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3670 n = identify_remember(current_ir_graph->value_table, n);
3676 * Wrapper for external use, set proper status bits after optimization.
3679 optimize_in_place (ir_node *n)
3681 /* Handle graph state */
3682 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3684 if (get_opt_global_cse())
3685 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3686 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3687 set_irg_outs_inconsistent(current_ir_graph);
3689 /* FIXME: Maybe we could also test whether optimizing the node can
3690 change the control graph. */
3691 set_irg_doms_inconsistent(current_ir_graph);
3692 return optimize_in_place_2 (n);
3696 * Sets the default operation for an ir_ops.
3698 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3700 ops = firm_set_default_computed_value(code, ops);
3701 ops = firm_set_default_equivalent_node(code, ops);
3702 ops = firm_set_default_transform_node(code, ops);
3703 ops = firm_set_default_node_cmp_attr(code, ops);
3704 ops = firm_set_default_get_type(code, ops);
3705 ops = firm_set_default_get_type_attr(code, ops);
3706 ops = firm_set_default_get_entity_attr(code, ops);