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)
64 switch (get_SymConst_kind(n)) {
65 case symconst_type_size:
66 type = get_SymConst_type(n);
67 if (get_type_state(type) == layout_fixed)
68 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
70 case symconst_type_align:
71 type = get_SymConst_type(n);
72 if (get_type_state(type) == layout_fixed)
73 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
82 * return the value of an Add
84 static tarval *computed_value_Add(ir_node *n)
86 ir_node *a = get_Add_left(n);
87 ir_node *b = get_Add_right(n);
89 tarval *ta = value_of(a);
90 tarval *tb = value_of(b);
92 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
93 return tarval_add(ta, tb);
99 * return the value of a Sub
100 * Special case: a - a
102 static tarval *computed_value_Sub(ir_node *n)
104 ir_node *a = get_Sub_left(n);
105 ir_node *b = get_Sub_right(n);
110 if (a == b && !is_Bad(a))
111 return get_mode_null(get_irn_mode(n));
116 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
117 return tarval_sub(ta, tb);
123 * return the value of a Carry
124 * Special : a op 0, 0 op b
126 static tarval *computed_value_Carry(ir_node *n)
128 ir_node *a = get_binop_left(n);
129 ir_node *b = get_binop_right(n);
130 ir_mode *m = get_irn_mode(n);
132 tarval *ta = value_of(a);
133 tarval *tb = value_of(b);
135 if ((ta != tarval_bad) && (tb != tarval_bad)) {
137 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
139 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
140 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
141 return get_mode_null(m);
147 * return the value of a Borrow
150 static tarval *computed_value_Borrow(ir_node *n)
152 ir_node *a = get_binop_left(n);
153 ir_node *b = get_binop_right(n);
154 ir_mode *m = get_irn_mode(n);
156 tarval *ta = value_of(a);
157 tarval *tb = value_of(b);
159 if ((ta != tarval_bad) && (tb != tarval_bad)) {
160 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
161 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
162 return get_mode_null(m);
168 * return the value of an unary Minus
170 static tarval *computed_value_Minus(ir_node *n)
172 ir_node *a = get_Minus_op(n);
173 tarval *ta = value_of(a);
175 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
176 return tarval_neg(ta);
182 * return the value of a Mul
184 static tarval *computed_value_Mul(ir_node *n)
186 ir_node *a = get_Mul_left(n);
187 ir_node *b = get_Mul_right(n);
189 tarval *ta = value_of(a);
190 tarval *tb = value_of(b);
192 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
193 return tarval_mul(ta, tb);
195 /* a*0 = 0 or 0*b = 0:
196 calls computed_value recursive and returns the 0 with proper
198 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
200 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
207 * return the value of a floating point Quot
209 static tarval *computed_value_Quot(ir_node *n)
211 ir_node *a = get_Quot_left(n);
212 ir_node *b = get_Quot_right(n);
214 tarval *ta = value_of(a);
215 tarval *tb = value_of(b);
217 /* This was missing in original implementation. Why? */
218 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
219 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
220 return tarval_quo(ta, tb);
226 * calculate the value of an integer Div of two nodes
227 * Special case: 0 / b
229 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
231 tarval *ta = value_of(a);
232 tarval *tb = value_of(b);
234 /* Compute c1 / c2 or 0 / a, a != 0 */
235 if (ta != tarval_bad) {
236 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
237 return tarval_div(ta, tb);
238 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
245 * return the value of an integer Div
247 static tarval *computed_value_Div(ir_node *n)
249 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
253 * calculate the value of an integer Mod of two nodes
254 * Special case: a % 1
256 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
258 tarval *ta = value_of(a);
259 tarval *tb = value_of(b);
261 /* Compute c1 % c2 or a % 1 */
262 if (tb != tarval_bad) {
263 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
264 return tarval_mod(ta, tb);
265 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
266 return get_mode_null(get_irn_mode(a));
273 * return the value of an integer Mod
275 static tarval *computed_value_Mod(ir_node *n)
277 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
281 * return the value of an Abs
283 static tarval *computed_value_Abs(ir_node *n)
285 ir_node *a = get_Abs_op(n);
286 tarval *ta = value_of(a);
288 if (ta != tarval_bad)
289 return tarval_abs(ta);
295 * return the value of an And
296 * Special case: a & 0, 0 & b
298 static tarval *computed_value_And(ir_node *n)
300 ir_node *a = get_And_left(n);
301 ir_node *b = get_And_right(n);
303 tarval *ta = value_of(a);
304 tarval *tb = value_of(b);
306 if ((ta != tarval_bad) && (tb != tarval_bad)) {
307 return tarval_and (ta, tb);
311 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
312 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
320 * return the value of an Or
321 * Special case: a | 1...1, 1...1 | b
323 static tarval *computed_value_Or(ir_node *n)
325 ir_node *a = get_Or_left(n);
326 ir_node *b = get_Or_right(n);
328 tarval *ta = value_of(a);
329 tarval *tb = value_of(b);
331 if ((ta != tarval_bad) && (tb != tarval_bad)) {
332 return tarval_or (ta, tb);
335 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
336 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
344 * return the value of an Eor
346 static tarval *computed_value_Eor(ir_node *n)
348 ir_node *a = get_Eor_left(n);
349 ir_node *b = get_Eor_right(n);
354 return get_mode_null(get_irn_mode(n));
359 if ((ta != tarval_bad) && (tb != tarval_bad)) {
360 return tarval_eor (ta, tb);
366 * return the value of a Not
368 static tarval *computed_value_Not(ir_node *n)
370 ir_node *a = get_Not_op(n);
371 tarval *ta = value_of(a);
373 if (ta != tarval_bad)
374 return tarval_not(ta);
380 * return the value of a Shl
382 static tarval *computed_value_Shl(ir_node *n)
384 ir_node *a = get_Shl_left(n);
385 ir_node *b = get_Shl_right(n);
387 tarval *ta = value_of(a);
388 tarval *tb = value_of(b);
390 if ((ta != tarval_bad) && (tb != tarval_bad)) {
391 return tarval_shl (ta, tb);
397 * return the value of a Shr
399 static tarval *computed_value_Shr(ir_node *n)
401 ir_node *a = get_Shr_left(n);
402 ir_node *b = get_Shr_right(n);
404 tarval *ta = value_of(a);
405 tarval *tb = value_of(b);
407 if ((ta != tarval_bad) && (tb != tarval_bad)) {
408 return tarval_shr (ta, tb);
414 * return the value of a Shrs
416 static tarval *computed_value_Shrs(ir_node *n)
418 ir_node *a = get_Shrs_left(n);
419 ir_node *b = get_Shrs_right(n);
421 tarval *ta = value_of(a);
422 tarval *tb = value_of(b);
424 if ((ta != tarval_bad) && (tb != tarval_bad)) {
425 return tarval_shrs (ta, tb);
431 * return the value of a Rot
433 static tarval *computed_value_Rot(ir_node *n)
435 ir_node *a = get_Rot_left(n);
436 ir_node *b = get_Rot_right(n);
438 tarval *ta = value_of(a);
439 tarval *tb = value_of(b);
441 if ((ta != tarval_bad) && (tb != tarval_bad)) {
442 return tarval_rot (ta, tb);
448 * return the value of a Conv
450 static tarval *computed_value_Conv(ir_node *n)
452 ir_node *a = get_Conv_op(n);
453 tarval *ta = value_of(a);
455 if (ta != tarval_bad)
456 return tarval_convert_to(ta, get_irn_mode(n));
462 * return the value of a Proj(Cmp)
464 * This performs a first step of unreachable code elimination.
465 * Proj can not be computed, but folding a Cmp above the Proj here is
466 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
468 * There are several case where we can evaluate a Cmp node, see later.
470 static tarval *computed_value_Proj_Cmp(ir_node *n)
472 ir_node *a = get_Proj_pred(n);
473 ir_node *aa = get_Cmp_left(a);
474 ir_node *ab = get_Cmp_right(a);
475 long proj_nr = get_Proj_proj(n);
478 * BEWARE: a == a is NOT always True for floating Point values, as
479 * NaN != NaN is defined, so we must check this here.
482 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
485 /* This is a trick with the bits used for encoding the Cmp
486 Proj numbers, the following statement is not the same:
487 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
488 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
491 tarval *taa = value_of(aa);
492 tarval *tab = value_of(ab);
493 ir_mode *mode = get_irn_mode(aa);
496 * The predecessors of Cmp are target values. We can evaluate
499 if ((taa != tarval_bad) && (tab != tarval_bad)) {
500 /* strange checks... */
501 pn_Cmp flags = tarval_cmp(taa, tab);
502 if (flags != pn_Cmp_False) {
503 return new_tarval_from_long (proj_nr & flags, mode_b);
506 /* for integer values, we can check against MIN/MAX */
507 else if (mode_is_int(mode)) {
508 /* MIN <=/> x. This results in true/false. */
509 if (taa == get_mode_min(mode)) {
510 /* a compare with the MIN value */
511 if (proj_nr == pn_Cmp_Le)
512 return get_tarval_b_true();
513 else if (proj_nr == pn_Cmp_Gt)
514 return get_tarval_b_false();
516 /* x >=/< MIN. This results in true/false. */
518 if (tab == get_mode_min(mode)) {
519 /* a compare with the MIN value */
520 if (proj_nr == pn_Cmp_Ge)
521 return get_tarval_b_true();
522 else if (proj_nr == pn_Cmp_Lt)
523 return get_tarval_b_false();
525 /* MAX >=/< x. This results in true/false. */
526 else if (taa == get_mode_max(mode)) {
527 if (proj_nr == pn_Cmp_Ge)
528 return get_tarval_b_true();
529 else if (proj_nr == pn_Cmp_Lt)
530 return get_tarval_b_false();
532 /* x <=/> MAX. This results in true/false. */
533 else if (tab == get_mode_max(mode)) {
534 if (proj_nr == pn_Cmp_Le)
535 return get_tarval_b_true();
536 else if (proj_nr == pn_Cmp_Gt)
537 return get_tarval_b_false();
541 * The predecessors are Allocs or (void*)(0) constants. Allocs never
542 * return NULL, they raise an exception. Therefore we can predict
546 ir_node *aaa = skip_Id(skip_Proj(aa));
547 ir_node *aba = skip_Id(skip_Proj(ab));
549 if ( ( (/* aa is ProjP and aaa is Alloc */
550 (get_irn_op(aa) == op_Proj)
551 && (mode_is_reference(get_irn_mode(aa)))
552 && (get_irn_op(aaa) == op_Alloc))
553 && ( (/* ab is NULL */
554 (get_irn_op(ab) == op_Const)
555 && (mode_is_reference(get_irn_mode(ab)))
556 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
557 || (/* ab is other Alloc */
558 (get_irn_op(ab) == op_Proj)
559 && (mode_is_reference(get_irn_mode(ab)))
560 && (get_irn_op(aba) == op_Alloc)
562 || (/* aa is NULL and aba is Alloc */
563 (get_irn_op(aa) == op_Const)
564 && (mode_is_reference(get_irn_mode(aa)))
565 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
566 && (get_irn_op(ab) == op_Proj)
567 && (mode_is_reference(get_irn_mode(ab)))
568 && (get_irn_op(aba) == op_Alloc)))
570 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
573 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
577 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
579 static tarval *computed_value_Proj(ir_node *n)
581 ir_node *a = get_Proj_pred(n);
584 switch (get_irn_opcode(a)) {
586 return computed_value_Proj_Cmp(n);
589 /* compute either the Div or the Mod part */
590 proj_nr = get_Proj_proj(n);
591 if (proj_nr == pn_DivMod_res_div)
592 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
593 else if (proj_nr == pn_DivMod_res_mod)
594 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
598 if (get_Proj_proj(n) == pn_Div_res)
599 return computed_value(a);
603 if (get_Proj_proj(n) == pn_Mod_res)
604 return computed_value(a);
614 * calculate the value of a Mux: can be evaluated, if the
615 * sel and the right input are known
617 static tarval *computed_value_Mux(ir_node *n)
619 ir_node *sel = get_Mux_sel(n);
620 tarval *ts = value_of(sel);
622 if (ts == get_tarval_b_true()) {
623 ir_node *v = get_Mux_true(n);
626 else if (ts == get_tarval_b_false()) {
627 ir_node *v = get_Mux_false(n);
634 * Calculate the value of a Psi: can be evaluated, if a condition is true
635 * and all previous conditions are false. If all conditions are false
636 * we evaluate to the default one.
638 static tarval *computed_value_Psi(ir_node *n)
641 return computed_value_Mux(n);
646 * calculate the value of a Confirm: can be evaluated,
647 * if it has the form Confirm(x, '=', Const).
649 static tarval *computed_value_Confirm(ir_node *n)
651 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
652 value_of(get_Confirm_bound(n)) : tarval_bad;
656 * If the parameter n can be computed, return its value, else tarval_bad.
657 * Performs constant folding.
659 * @param n The node this should be evaluated
661 tarval *computed_value(ir_node *n)
663 if (n->op->ops.computed_value)
664 return n->op->ops.computed_value(n);
669 * set the default computed_value evaluator in an ir_op_ops.
671 * @param code the opcode for the default operation
672 * @param ops the operations initialized
677 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
681 ops->computed_value = computed_value_##a; \
719 * Returns a equivalent block for another block.
720 * If the block has only one predecessor, this is
721 * the equivalent one. If the only predecessor of a block is
722 * the block itself, this is a dead block.
724 * If both predecessors of a block are the branches of a binary
725 * Cond, the equivalent block is Cond's block.
727 * If all predecessors of a block are bad or lies in a dead
728 * block, the current block is dead as well.
730 * Note, that blocks are NEVER turned into Bad's, instead
731 * the dead_block flag is set. So, never test for is_Bad(block),
732 * always use is_dead_Block(block).
734 static ir_node *equivalent_node_Block(ir_node *n)
737 int n_preds = get_Block_n_cfgpreds(n);
739 /* The Block constructor does not call optimize, but mature_immBlock
740 calls the optimization. */
741 assert(get_Block_matured(n));
743 /* Straightening: a single entry Block following a single exit Block
744 can be merged, if it is not the Start block. */
745 /* !!! Beware, all Phi-nodes of n must have been optimized away.
746 This should be true, as the block is matured before optimize is called.
747 But what about Phi-cycles with the Phi0/Id that could not be resolved?
748 Remaining Phi nodes are just Ids. */
749 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
750 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
751 if (predblock == oldn) {
752 /* Jmp jumps into the block it is in -- deal self cycle. */
753 n = set_Block_dead(n);
754 DBG_OPT_DEAD_BLOCK(oldn, n);
755 } else if (get_opt_control_flow_straightening()) {
757 DBG_OPT_STG(oldn, n);
760 else if ((n_preds == 1) &&
761 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
762 ir_node *predblock = get_Block_cfgpred_block(n, 0);
763 if (predblock == oldn) {
764 /* Jmp jumps into the block it is in -- deal self cycle. */
765 n = set_Block_dead(n);
766 DBG_OPT_DEAD_BLOCK(oldn, n);
769 else if ((n_preds == 2) &&
770 (get_opt_control_flow_weak_simplification())) {
771 /* Test whether Cond jumps twice to this block
772 * The more general case which more than 2 predecessors is handles
773 * in optimize_cf(), we handle only this special case for speed here.
775 ir_node *a = get_Block_cfgpred(n, 0);
776 ir_node *b = get_Block_cfgpred(n, 1);
778 if ((get_irn_op(a) == op_Proj) &&
779 (get_irn_op(b) == op_Proj) &&
780 (get_Proj_pred(a) == get_Proj_pred(b)) &&
781 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
782 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
783 /* Also a single entry Block following a single exit Block. Phis have
784 twice the same operand and will be optimized away. */
785 n = get_nodes_block(get_Proj_pred(a));
786 DBG_OPT_IFSIM1(oldn, a, b, n);
789 else if (get_opt_unreachable_code() &&
790 (n != get_irg_start_block(current_ir_graph)) &&
791 (n != get_irg_end_block(current_ir_graph)) ) {
794 /* If all inputs are dead, this block is dead too, except if it is
795 the start or end block. This is one step of unreachable code
797 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
798 ir_node *pred = get_Block_cfgpred(n, i);
801 if (is_Bad(pred)) continue;
802 pred_blk = get_nodes_block(skip_Proj(pred));
804 if (is_Block_dead(pred_blk)) continue;
807 /* really found a living input */
812 n = set_Block_dead(n);
813 DBG_OPT_DEAD_BLOCK(oldn, n);
821 * Returns a equivalent node for a Jmp, a Bad :-)
822 * Of course this only happens if the Block of the Jmp is dead.
824 static ir_node *equivalent_node_Jmp(ir_node *n)
826 /* unreachable code elimination */
827 if (is_Block_dead(get_nodes_block(n)))
833 /** Raise is handled in the same way as Jmp. */
834 #define equivalent_node_Raise equivalent_node_Jmp
837 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
838 See transform_node_Proj_Cond(). */
841 * optimize operations that are commutative and have neutral 0,
842 * so a op 0 = 0 op a = a.
844 static ir_node *equivalent_node_neutral_zero(ir_node *n)
848 ir_node *a = get_binop_left(n);
849 ir_node *b = get_binop_right(n);
854 /* After running compute_node there is only one constant predecessor.
855 Find this predecessors value and remember the other node: */
856 if ((tv = value_of(a)) != tarval_bad) {
858 } else if ((tv = value_of(b)) != tarval_bad) {
863 /* If this predecessors constant value is zero, the operation is
864 * unnecessary. Remove it.
866 * Beware: If n is a Add, the mode of on and n might be different
867 * which happens in this rare construction: NULL + 3.
868 * Then, a Conv would be needed which we cannot include here.
870 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
871 if (get_irn_mode(on) == get_irn_mode(n)) {
874 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
882 * Eor is commutative and has neutral 0.
884 #define equivalent_node_Eor equivalent_node_neutral_zero
887 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
889 * The second one looks strange, but this construct
890 * is used heavily in the LCC sources :-).
892 * Beware: The Mode of an Add may be different than the mode of its
893 * predecessors, so we could not return a predecessors in all cases.
895 static ir_node *equivalent_node_Add(ir_node *n)
898 ir_node *left, *right;
900 n = equivalent_node_neutral_zero(n);
904 left = get_Add_left(n);
905 right = get_Add_right(n);
907 if (get_irn_op(left) == op_Sub) {
908 if (get_Sub_right(left) == right) {
911 n = get_Sub_left(left);
912 if (get_irn_mode(oldn) == get_irn_mode(n)) {
913 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
918 if (get_irn_op(right) == op_Sub) {
919 if (get_Sub_right(right) == left) {
922 n = get_Sub_left(right);
923 if (get_irn_mode(oldn) == get_irn_mode(n)) {
924 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
933 * optimize operations that are not commutative but have neutral 0 on left,
936 static ir_node *equivalent_node_left_zero(ir_node *n)
940 ir_node *a = get_binop_left(n);
941 ir_node *b = get_binop_right(n);
943 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
946 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
952 #define equivalent_node_Shl equivalent_node_left_zero
953 #define equivalent_node_Shr equivalent_node_left_zero
954 #define equivalent_node_Shrs equivalent_node_left_zero
955 #define equivalent_node_Rot equivalent_node_left_zero
958 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
960 * The second one looks strange, but this construct
961 * is used heavily in the LCC sources :-).
963 * Beware: The Mode of a Sub may be different than the mode of its
964 * predecessors, so we could not return a predecessors in all cases.
966 static ir_node *equivalent_node_Sub(ir_node *n)
970 ir_node *a = get_Sub_left(n);
971 ir_node *b = get_Sub_right(n);
973 /* Beware: modes might be different */
974 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
975 if (get_irn_mode(n) == get_irn_mode(a)) {
978 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
981 else if (get_irn_op(a) == op_Add) {
982 ir_mode *mode = get_irn_mode(n);
984 if (mode_wrap_around(mode)) {
985 ir_node *left = get_Add_left(a);
986 ir_node *right = get_Add_right(a);
989 if (get_irn_mode(n) == get_irn_mode(right)) {
991 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
994 else if (right == b) {
995 if (get_irn_mode(n) == get_irn_mode(left)) {
997 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
1008 * Optimize an "idempotent unary op", ie op(op(n)) = n.
1011 * -(-a) == a, but might overflow two times.
1012 * We handle it anyway here but the better way would be a
1013 * flag. This would be needed for Pascal for instance.
1015 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1018 ir_node *pred = get_unop_op(n);
1020 /* optimize symmetric unop */
1021 if (get_irn_op(pred) == get_irn_op(n)) {
1022 n = get_unop_op(pred);
1023 DBG_OPT_ALGSIM2(oldn, pred, n);
1028 /** Not(Not(x)) == x */
1029 #define equivalent_node_Not equivalent_node_idempotent_unop
1031 /** --x == x ??? Is this possible or can --x raise an
1032 out of bounds exception if min =! max? */
1033 #define equivalent_node_Minus equivalent_node_idempotent_unop
1036 * Optimize a * 1 = 1 * a = a.
1038 static ir_node *equivalent_node_Mul(ir_node *n)
1042 ir_node *a = get_Mul_left(n);
1043 ir_node *b = get_Mul_right(n);
1045 /* Mul is commutative and has again an other neutral element. */
1046 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1048 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1049 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1051 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1057 * Optimize a / 1 = a.
1059 static ir_node *equivalent_node_Div(ir_node *n)
1061 ir_node *a = get_Div_left(n);
1062 ir_node *b = get_Div_right(n);
1064 /* Div is not commutative. */
1065 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1066 /* Turn Div into a tuple (mem, bad, a) */
1067 ir_node *mem = get_Div_mem(n);
1068 turn_into_tuple(n, pn_Div_max);
1069 set_Tuple_pred(n, pn_Div_M, mem);
1070 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1071 set_Tuple_pred(n, pn_Div_res, a);
1077 * Optimize a / 1 = a.
1079 static ir_node *equivalent_node_DivMod(ir_node *n)
1081 ir_node *a = get_DivMod_left(n);
1082 ir_node *b = get_DivMod_right(n);
1084 /* Div is not commutative. */
1085 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1086 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1087 ir_node *mem = get_Div_mem(n);
1088 ir_mode *mode = get_irn_mode(b);
1090 turn_into_tuple(n, pn_DivMod_max);
1091 set_Tuple_pred(n, pn_DivMod_M, mem);
1092 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1093 set_Tuple_pred(n, pn_DivMod_res_div, a);
1094 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1100 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1102 static ir_node *equivalent_node_Or(ir_node *n)
1106 ir_node *a = get_Or_left(n);
1107 ir_node *b = get_Or_right(n);
1110 n = a; /* Or has it's own neutral element */
1111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1112 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1124 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1126 static ir_node *equivalent_node_And(ir_node *n)
1130 ir_node *a = get_And_left(n);
1131 ir_node *b = get_And_right(n);
1134 n = a; /* And has it's own neutral element */
1135 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1136 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1139 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1147 * Try to remove useless Conv's:
1149 static ir_node *equivalent_node_Conv(ir_node *n)
1152 ir_node *a = get_Conv_op(n);
1155 ir_mode *n_mode = get_irn_mode(n);
1156 ir_mode *a_mode = get_irn_mode(a);
1158 if (n_mode == a_mode) { /* No Conv necessary */
1160 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1161 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1165 n_mode = get_irn_mode(n);
1166 b_mode = get_irn_mode(b);
1168 if (n_mode == b_mode) {
1169 if (n_mode == mode_b) {
1170 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1173 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1174 if (smaller_mode(b_mode, a_mode)){
1175 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1185 * A Cast may be removed if the type of the previous node
1186 * is already the type of the Cast.
1188 static ir_node *equivalent_node_Cast(ir_node *n) {
1190 ir_node *pred = get_Cast_op(n);
1192 if (get_irn_type(pred) == get_Cast_type(n)) {
1194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1200 Several optimizations:
1201 - no Phi in start block.
1202 - remove Id operators that are inputs to Phi
1203 - fold Phi-nodes, iff they have only one predecessor except
1206 static ir_node *equivalent_node_Phi(ir_node *n)
1211 ir_node *block = NULL; /* to shutup gcc */
1212 ir_node *first_val = NULL; /* to shutup gcc */
1214 if (!get_opt_normalize()) return n;
1216 n_preds = get_Phi_n_preds(n);
1218 block = get_nodes_block(n);
1219 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1220 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1221 if ((is_Block_dead(block)) || /* Control dead */
1222 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1223 return new_Bad(); /* in the Start Block. */
1225 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1227 /* If the Block has a Bad pred, we also have one. */
1228 for (i = 0; i < n_preds; ++i)
1229 if (is_Bad(get_Block_cfgpred(block, i)))
1230 set_Phi_pred(n, i, new_Bad());
1232 /* Find first non-self-referencing input */
1233 for (i = 0; i < n_preds; ++i) {
1234 first_val = get_Phi_pred(n, i);
1235 if ( (first_val != n) /* not self pointer */
1237 && (! is_Bad(first_val))
1239 ) { /* value not dead */
1240 break; /* then found first value. */
1245 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1249 /* search for rest of inputs, determine if any of these
1250 are non-self-referencing */
1251 while (++i < n_preds) {
1252 ir_node *scnd_val = get_Phi_pred(n, i);
1253 if ( (scnd_val != n)
1254 && (scnd_val != first_val)
1256 && (! is_Bad(scnd_val))
1264 /* Fold, if no multiple distinct non-self-referencing inputs */
1266 DBG_OPT_PHI(oldn, n);
1272 Several optimizations:
1273 - no Sync in start block.
1274 - fold Sync-nodes, iff they have only one predecessor except
1276 @fixme: are there loop's in Sync's
1278 static ir_node *equivalent_node_Sync(ir_node *n)
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_normalize()) return n;
1287 n_preds = get_Sync_n_preds(n);
1289 /* Find first non-self-referencing input */
1290 for (i = 0; i < n_preds; ++i) {
1291 first_val = get_Sync_pred(n, i);
1292 if ((first_val != n) /* not self pointer */ &&
1293 (! is_Bad(first_val))
1294 ) { /* value not dead */
1295 break; /* then found first value. */
1300 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1303 /* search the rest of inputs, determine if any of these
1304 are non-self-referencing */
1305 while (++i < n_preds) {
1306 ir_node *scnd_val = get_Sync_pred(n, i);
1307 if ((scnd_val != n) &&
1308 (scnd_val != first_val) &&
1309 (! is_Bad(scnd_val))
1315 /* Fold, if no multiple distinct non-self-referencing inputs */
1317 DBG_OPT_SYNC(oldn, n);
1323 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324 * ProjX(Load) and ProjX(Store)
1326 static ir_node *equivalent_node_Proj(ir_node *n)
1330 ir_node *a = get_Proj_pred(n);
1332 if ( get_irn_op(a) == op_Tuple) {
1333 /* Remove the Tuple/Proj combination. */
1334 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1335 n = get_Tuple_pred(a, get_Proj_proj(n));
1336 DBG_OPT_TUPLE(oldn, a, n);
1338 assert(0); /* This should not happen! */
1342 else if (get_irn_mode(n) == mode_X) {
1343 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1344 /* Remove dead control flow -- early gigo(). */
1347 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1348 ir_op *op = get_irn_op(a);
1350 if (op == op_Load || op == op_Store) {
1351 /* get the load/store address */
1352 ir_node *addr = get_irn_n(a, 1);
1353 if (value_not_null(addr)) {
1354 /* this node may float if it did not depend on a Confirm */
1355 set_irn_pinned(a, op_pin_state_floats);
1369 static ir_node *equivalent_node_Id(ir_node *n)
1375 } while (get_irn_op(n) == op_Id);
1377 DBG_OPT_ID(oldn, n);
1384 static ir_node *equivalent_node_Mux(ir_node *n)
1386 ir_node *oldn = n, *sel = get_Mux_sel(n);
1387 tarval *ts = value_of(sel);
1389 /* Mux(true, f, t) == t */
1390 if (ts == tarval_b_true) {
1391 n = get_Mux_true(n);
1392 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1394 /* Mux(false, f, t) == f */
1395 else if (ts == tarval_b_false) {
1396 n = get_Mux_false(n);
1397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1399 /* Mux(v, x, x) == x */
1400 else if (get_Mux_false(n) == get_Mux_true(n)) {
1401 n = get_Mux_true(n);
1402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1404 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1405 ir_node *cmp = get_Proj_pred(sel);
1406 long proj_nr = get_Proj_proj(sel);
1407 ir_node *b = get_Mux_false(n);
1408 ir_node *a = get_Mux_true(n);
1411 * Note: normalization puts the constant on the right site,
1412 * so we check only one case.
1414 * Note further that these optimization work even for floating point
1415 * with NaN's because -NaN == NaN.
1416 * However, if +0 and -0 is handled differently, we cannot use the first one.
1418 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1419 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1420 /* Mux(a CMP 0, X, a) */
1421 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1422 /* Mux(a CMP 0, -a, a) */
1423 if (proj_nr == pn_Cmp_Eq) {
1424 /* Mux(a == 0, -a, a) ==> -a */
1426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1428 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1429 /* Mux(a != 0, -a, a) ==> a */
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1434 else if (classify_Const(b) == CNST_NULL) {
1435 /* Mux(a CMP 0, 0, a) */
1436 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1437 /* Mux(a != 0, 0, a) ==> a */
1439 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1441 else if (proj_nr == pn_Cmp_Eq) {
1442 /* Mux(a == 0, 0, a) ==> 0 */
1444 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1454 * Returns a equivalent node of a Psi: if a condition is true
1455 * and all previous conditions are false we know its value.
1456 * If all conditions are false its value is the default one.
1458 static ir_node *equivalent_node_Psi(ir_node *n) {
1460 return equivalent_node_Mux(n);
1465 * Optimize -a CMP -b into b CMP a.
1466 * This works only for for modes where unary Minus
1468 * Note that two-complement integers can Overflow
1469 * so it will NOT work.
1471 static ir_node *equivalent_node_Cmp(ir_node *n)
1473 ir_node *left = get_Cmp_left(n);
1474 ir_node *right = get_Cmp_right(n);
1476 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1477 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1478 left = get_Minus_op(left);
1479 right = get_Minus_op(right);
1480 set_Cmp_left(n, right);
1481 set_Cmp_right(n, left);
1487 * Remove Confirm nodes if setting is on.
1488 * Replace Confirms(x, '=', Constlike) by Constlike.
1490 static ir_node *equivalent_node_Confirm(ir_node *n)
1492 ir_node *pred = get_Confirm_value(n);
1493 pn_Cmp pnc = get_Confirm_cmp(n);
1495 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1497 * rare case: two identical Confirms one after another,
1498 * replace the second one with the first.
1502 if (pnc == pn_Cmp_Eq) {
1503 ir_node *bound = get_Confirm_bound(n);
1506 * Optimize a rare case:
1507 * Confirm(x, '=', Constlike) ==> Constlike
1509 if (is_irn_constlike(bound)) {
1510 DBG_OPT_CONFIRM(n, bound);
1514 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1518 * Optimize CopyB(mem, x, x) into a Nop
1520 static ir_node *equivalent_node_CopyB(ir_node *n)
1522 ir_node *a = get_CopyB_dst(n);
1523 ir_node *b = get_CopyB_src(n);
1526 /* Turn CopyB into a tuple (mem, bad, bad) */
1527 ir_node *mem = get_CopyB_mem(n);
1528 turn_into_tuple(n, pn_CopyB_max);
1529 set_Tuple_pred(n, pn_CopyB_M, mem);
1530 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1531 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1537 * Optimize Bounds(idx, idx, upper) into idx.
1539 static ir_node *equivalent_node_Bound(ir_node *n)
1541 ir_node *idx = get_Bound_index(n);
1542 ir_node *lower = get_Bound_lower(n);
1545 /* By definition lower < upper, so if idx == lower -->
1546 lower <= idx && idx < upper */
1548 /* Turn Bound into a tuple (mem, bad, idx) */
1552 ir_node *pred = skip_Proj(idx);
1554 if (get_irn_op(pred) == op_Bound) {
1556 * idx was Bounds_check previously, it is still valid if
1557 * lower <= pred_lower && pred_upper <= upper.
1559 ir_node *upper = get_Bound_upper(n);
1560 if (get_Bound_lower(pred) == lower &&
1561 get_Bound_upper(pred) == upper) {
1563 * One could expect that we simply return the previous
1564 * Bound here. However, this would be wrong, as we could
1565 * add an exception Proj to a new location than.
1566 * So, we must turn in into a tuple
1573 /* Turn Bound into a tuple (mem, bad, idx) */
1574 ir_node *mem = get_Bound_mem(n);
1575 turn_into_tuple(n, pn_Bound_max);
1576 set_Tuple_pred(n, pn_Bound_M_regular, mem);
1577 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1578 set_Tuple_pred(n, pn_Bound_res, idx);
1579 set_Tuple_pred(n, pn_Bound_M_except, mem);
1585 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1586 * perform no actual computation, as, e.g., the Id nodes. It does not create
1587 * new nodes. It is therefore safe to free n if the node returned is not n.
1588 * If a node returns a Tuple we can not just skip it. If the size of the
1589 * in array fits, we transform n into a tuple (e.g., Div).
1592 equivalent_node(ir_node *n)
1594 if (n->op->ops.equivalent_node)
1595 return n->op->ops.equivalent_node(n);
1600 * sets the default equivalent node operation for an ir_op_ops.
1602 * @param code the opcode for the default operation
1603 * @param ops the operations initialized
1608 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1612 ops->equivalent_node = equivalent_node_##a; \
1654 * Do node specific optimizations of nodes predecessors.
1657 optimize_preds(ir_node *n) {
1658 ir_node *a = NULL, *b = NULL;
1660 /* get the operands we will work on for simple cases. */
1662 a = get_binop_left(n);
1663 b = get_binop_right(n);
1664 } else if (is_unop(n)) {
1668 switch (get_irn_opcode(n)) {
1671 /* We don't want Cast as input to Cmp. */
1672 if (get_irn_op(a) == op_Cast) {
1676 if (get_irn_op(b) == op_Cast) {
1678 set_Cmp_right(n, b);
1687 * Returns non-zero if all Phi predecessors are constants
1689 static int is_const_Phi(ir_node *phi) {
1692 for (i = get_irn_arity(phi) - 1; i >= 0; --i)
1693 if (! is_Const(get_irn_n(phi, i)))
1699 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1700 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1701 * If possible, remove the Conv's.
1703 static ir_node *transform_node_AddSub(ir_node *n)
1705 ir_mode *mode = get_irn_mode(n);
1707 if (mode_is_reference(mode)) {
1708 ir_node *left = get_binop_left(n);
1709 ir_node *right = get_binop_right(n);
1710 int ref_bits = get_mode_size_bits(mode);
1712 if (get_irn_op(left) == op_Conv) {
1713 ir_mode *mode = get_irn_mode(left);
1714 int bits = get_mode_size_bits(mode);
1716 if (ref_bits == bits &&
1717 mode_is_int(mode) &&
1718 get_mode_arithmetic(mode) == irma_twos_complement) {
1719 ir_node *pre = get_Conv_op(left);
1720 ir_mode *pre_mode = get_irn_mode(pre);
1722 if (mode_is_int(pre_mode) &&
1723 get_mode_size_bits(pre_mode) == bits &&
1724 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1725 /* ok, this conv just changes to sign, moreover the calculation
1726 * is done with same number of bits as our address mode, so
1727 * we can ignore the conv as address calculation can be viewed
1728 * as either signed or unsigned
1730 set_binop_left(n, pre);
1735 if (get_irn_op(right) == op_Conv) {
1736 ir_mode *mode = get_irn_mode(right);
1737 int bits = get_mode_size_bits(mode);
1739 if (ref_bits == bits &&
1740 mode_is_int(mode) &&
1741 get_mode_arithmetic(mode) == irma_twos_complement) {
1742 ir_node *pre = get_Conv_op(right);
1743 ir_mode *pre_mode = get_irn_mode(pre);
1745 if (mode_is_int(pre_mode) &&
1746 get_mode_size_bits(pre_mode) == bits &&
1747 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1748 /* ok, this conv just changes to sign, moreover the calculation
1749 * is done with same number of bits as our address mode, so
1750 * we can ignore the conv as address calculation can be viewed
1751 * as either signed or unsigned
1753 set_binop_right(n, pre);
1762 * Do the AddSub optimization, then Transform
1763 * Add(a,a) -> Mul(a, 2)
1764 * Add(Mul(a, x), a) -> Mul(a, x+1)
1765 * if the mode is integer or float.
1766 * Transform Add(a,-b) into Sub(a,b).
1767 * Reassociation might fold this further.
1769 static ir_node *transform_node_Add(ir_node *n)
1774 n = transform_node_AddSub(n);
1776 mode = get_irn_mode(n);
1777 if (mode_is_num(mode)) {
1778 ir_node *a = get_Add_left(n);
1779 ir_node *b = get_Add_right(n);
1782 ir_node *block = get_irn_n(n, -1);
1785 get_irn_dbg_info(n),
1789 new_r_Const_long(current_ir_graph, block, mode, 2),
1791 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1793 else if (get_irn_op(a) == op_Minus) {
1795 get_irn_dbg_info(n),
1801 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1803 else if (get_irn_op(b) == op_Minus) {
1805 get_irn_dbg_info(n),
1811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1813 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1814 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1815 ir_node *ma = get_Mul_left(a);
1816 ir_node *mb = get_Mul_right(a);
1819 ir_node *blk = get_irn_n(n, -1);
1821 get_irn_dbg_info(n), current_ir_graph, blk,
1824 get_irn_dbg_info(n), current_ir_graph, blk,
1826 new_r_Const_long(current_ir_graph, blk, mode, 1),
1829 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1832 ir_node *blk = get_irn_n(n, -1);
1834 get_irn_dbg_info(n), current_ir_graph, blk,
1837 get_irn_dbg_info(n), current_ir_graph, blk,
1839 new_r_Const_long(current_ir_graph, blk, mode, 1),
1842 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1845 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1846 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1847 ir_node *ma = get_Mul_left(b);
1848 ir_node *mb = get_Mul_right(b);
1851 ir_node *blk = get_irn_n(n, -1);
1853 get_irn_dbg_info(n), current_ir_graph, blk,
1856 get_irn_dbg_info(n), current_ir_graph, blk,
1858 new_r_Const_long(current_ir_graph, blk, mode, 1),
1861 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1864 ir_node *blk = get_irn_n(n, -1);
1866 get_irn_dbg_info(n), current_ir_graph, blk,
1869 get_irn_dbg_info(n), current_ir_graph, blk,
1871 new_r_Const_long(current_ir_graph, blk, mode, 1),
1874 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1882 * Do the AddSub optimization, then Transform
1883 * Sub(0,a) -> Minus(a)
1884 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1886 static ir_node *transform_node_Sub(ir_node *n)
1892 n = transform_node_AddSub(n);
1894 mode = get_irn_mode(n);
1895 a = get_Sub_left(n);
1896 b = get_Sub_right(n);
1897 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1899 get_irn_dbg_info(n),
1904 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1906 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1907 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1908 ir_node *ma = get_Mul_left(a);
1909 ir_node *mb = get_Mul_right(a);
1912 ir_node *blk = get_irn_n(n, -1);
1914 get_irn_dbg_info(n),
1915 current_ir_graph, blk,
1918 get_irn_dbg_info(n),
1919 current_ir_graph, blk,
1921 new_r_Const_long(current_ir_graph, blk, mode, 1),
1924 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1927 ir_node *blk = get_irn_n(n, -1);
1929 get_irn_dbg_info(n),
1930 current_ir_graph, blk,
1933 get_irn_dbg_info(n),
1934 current_ir_graph, blk,
1936 new_r_Const_long(current_ir_graph, blk, mode, 1),
1939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1947 * Transform Mul(a,-1) into -a.
1948 * Do architecture dependent optimizations on Mul nodes
1950 static ir_node *transform_node_Mul(ir_node *n) {
1952 ir_mode *mode = get_irn_mode(n);
1954 if (mode_is_signed(mode)) {
1956 ir_node *a = get_Mul_left(n);
1957 ir_node *b = get_Mul_right(n);
1959 if (value_of(a) == get_mode_minus_one(mode))
1961 else if (value_of(b) == get_mode_minus_one(mode))
1964 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1965 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1969 return arch_dep_replace_mul_with_shifts(n);
1973 * transform a Div Node
1975 static ir_node *transform_node_Div(ir_node *n)
1977 tarval *tv = value_of(n);
1980 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1982 if (tv != tarval_bad) {
1983 value = new_Const(get_tarval_mode(tv), tv);
1985 DBG_OPT_CSTEVAL(n, value);
1987 else /* Try architecture dependent optimization */
1988 value = arch_dep_replace_div_by_const(n);
1991 /* Turn Div into a tuple (mem, bad, value) */
1992 ir_node *mem = get_Div_mem(n);
1994 turn_into_tuple(n, pn_Div_max);
1995 set_Tuple_pred(n, pn_Div_M, mem);
1996 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1997 set_Tuple_pred(n, pn_Div_res, value);
2003 * transform a Mod node
2005 static ir_node *transform_node_Mod(ir_node *n)
2007 tarval *tv = value_of(n);
2010 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2012 if (tv != tarval_bad) {
2013 value = new_Const(get_tarval_mode(tv), tv);
2015 DBG_OPT_CSTEVAL(n, value);
2017 else /* Try architecture dependent optimization */
2018 value = arch_dep_replace_mod_by_const(n);
2021 /* Turn Mod into a tuple (mem, bad, value) */
2022 ir_node *mem = get_Mod_mem(n);
2024 turn_into_tuple(n, pn_Mod_max);
2025 set_Tuple_pred(n, pn_Mod_M, mem);
2026 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2027 set_Tuple_pred(n, pn_Mod_res, value);
2033 * transform a DivMod node
2035 static ir_node *transform_node_DivMod(ir_node *n)
2039 ir_node *a = get_DivMod_left(n);
2040 ir_node *b = get_DivMod_right(n);
2041 ir_mode *mode = get_irn_mode(a);
2042 tarval *ta = value_of(a);
2043 tarval *tb = value_of(b);
2045 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2048 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2050 if (tb != tarval_bad) {
2051 if (tb == get_mode_one(get_tarval_mode(tb))) {
2052 b = new_Const (mode, get_mode_null(mode));
2055 DBG_OPT_CSTEVAL(n, b);
2057 else if (ta != tarval_bad) {
2058 tarval *resa, *resb;
2059 resa = tarval_div (ta, tb);
2060 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2061 Jmp for X result!? */
2062 resb = tarval_mod (ta, tb);
2063 if (resb == tarval_bad) return n; /* Causes exception! */
2064 a = new_Const (mode, resa);
2065 b = new_Const (mode, resb);
2068 DBG_OPT_CSTEVAL(n, a);
2069 DBG_OPT_CSTEVAL(n, b);
2071 else { /* Try architecture dependent optimization */
2072 arch_dep_replace_divmod_by_const(&a, &b, n);
2073 evaluated = a != NULL;
2075 } else if (ta == get_mode_null(mode)) {
2076 /* 0 / non-Const = 0 */
2081 if (evaluated) { /* replace by tuple */
2082 ir_node *mem = get_DivMod_mem(n);
2083 turn_into_tuple(n, pn_DivMod_max);
2084 set_Tuple_pred(n, pn_DivMod_M, mem);
2085 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2086 set_Tuple_pred(n, pn_DivMod_res_div, a);
2087 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2094 * Optimize Abs(x) into x if x is Confirmed >= 0
2095 * Optimize Abs(x) into -x if x is Confirmed <= 0
2097 static ir_node *transform_node_Abs(ir_node *n)
2100 ir_node *a = get_Abs_op(n);
2101 value_classify sign = classify_value_sign(a);
2103 if (sign == VALUE_NEGATIVE) {
2104 ir_mode *mode = get_irn_mode(n);
2107 * We can replace the Abs by -x here.
2108 * We even could add a new Confirm here.
2110 * Note that -x would create a new node, so we could
2111 * not run it in the equivalent_node() context.
2113 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2114 get_irn_n(n, -1), a, mode);
2116 DBG_OPT_CONFIRM(oldn, n);
2118 else if (sign == VALUE_POSITIVE) {
2119 /* n is positive, Abs is not needed */
2122 DBG_OPT_CONFIRM(oldn, n);
2129 * transform a Cond node
2131 static ir_node *transform_node_Cond(ir_node *n)
2133 /* Replace the Cond by a Jmp if it branches on a constant
2136 ir_node *a = get_Cond_selector(n);
2137 tarval *ta = value_of(a);
2139 /* we need block info which is not available in floating irgs */
2140 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2143 if ((ta != tarval_bad) &&
2144 (get_irn_mode(a) == mode_b) &&
2145 (get_opt_unreachable_code())) {
2146 /* It's a boolean Cond, branching on a boolean constant.
2147 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2148 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2149 turn_into_tuple(n, pn_Cond_max);
2150 if (ta == tarval_b_true) {
2151 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2152 set_Tuple_pred(n, pn_Cond_true, jmp);
2154 set_Tuple_pred(n, pn_Cond_false, jmp);
2155 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2157 /* We might generate an endless loop, so keep it alive. */
2158 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2166 static ir_node *transform_node_Eor(ir_node *n)
2169 ir_node *a = get_Eor_left(n);
2170 ir_node *b = get_Eor_right(n);
2171 ir_mode *mode = get_irn_mode(n);
2175 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2176 mode, get_mode_null(mode));
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2179 else if ((mode == mode_b)
2180 && (get_irn_op(a) == op_Proj)
2181 && (get_irn_mode(a) == mode_b)
2182 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2183 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2184 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2185 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2186 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2188 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2190 else if ((mode == mode_b)
2191 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2192 /* The Eor is a Not. Replace it by a Not. */
2193 /* ????!!!Extend to bitfield 1111111. */
2194 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2196 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2203 * Transform a boolean Not.
2205 static ir_node *transform_node_Not(ir_node *n)
2208 ir_node *a = get_Not_op(n);
2210 if ( (get_irn_mode(n) == mode_b)
2211 && (get_irn_op(a) == op_Proj)
2212 && (get_irn_mode(a) == mode_b)
2213 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2214 /* We negate a Cmp. The Cmp has the negated result anyways! */
2215 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2216 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2217 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2224 * Transform a Cast_type(Const) into a new Const_type
2226 static ir_node *transform_node_Cast(ir_node *n) {
2228 ir_node *pred = get_Cast_op(n);
2229 ir_type *tp = get_irn_type(n);
2231 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2232 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2233 get_Const_tarval(pred), tp);
2234 DBG_OPT_CSTEVAL(oldn, n);
2235 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2236 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2237 get_SymConst_kind(pred), tp);
2238 DBG_OPT_CSTEVAL(oldn, n);
2245 * Transform a Proj(Div) with a non-zero value.
2246 * Removes the exceptions and routes the memory to the NoMem node.
2248 static ir_node *transform_node_Proj_Div(ir_node *proj)
2250 ir_node *n = get_Proj_pred(proj);
2251 ir_node *b = get_Div_right(n);
2254 if (value_not_zero(b)) {
2255 /* div(x, y) && y != 0 */
2256 proj_nr = get_Proj_proj(proj);
2258 /* this node may float if it did not depend on a Confirm */
2259 set_irn_pinned(n, op_pin_state_floats);
2261 if (proj_nr == pn_Div_X_except) {
2262 /* we found an exception handler, remove it */
2263 DBG_OPT_EXC_REM(proj);
2266 else if (proj_nr == pn_Div_M) {
2267 ir_node *res = get_Div_mem(n);
2268 /* the memory Proj can only be removed if we divide by a
2269 real constant, but the node never produce a new memory */
2270 if (value_of(b) != tarval_bad) {
2271 /* this is a Div by a const, we can remove the memory edge */
2272 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2281 * Transform a Proj(Mod) with a non-zero value.
2282 * Removes the exceptions and routes the memory to the NoMem node.
2284 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2286 ir_node *n = get_Proj_pred(proj);
2287 ir_node *b = get_Mod_right(n);
2290 if (value_not_zero(b)) {
2291 /* mod(x, y) && y != 0 */
2292 proj_nr = get_Proj_proj(proj);
2294 /* this node may float if it did not depend on a Confirm */
2295 set_irn_pinned(n, op_pin_state_floats);
2297 if (proj_nr == pn_Mod_X_except) {
2298 /* we found an exception handler, remove it */
2299 DBG_OPT_EXC_REM(proj);
2301 } else if (proj_nr == pn_Mod_M) {
2302 ir_node *res = get_Mod_mem(n);
2303 /* the memory Proj can only be removed if we divide by a
2304 real constant, but the node never produce a new memory */
2305 if (value_of(b) != tarval_bad) {
2306 /* this is a Mod by a const, we can remove the memory edge */
2307 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2311 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2312 /* a % a = 0 if a != 0 */
2313 ir_mode *mode = get_irn_mode(proj);
2314 ir_node *res = new_Const(mode, get_mode_null(mode));
2316 DBG_OPT_CSTEVAL(n, res);
2324 * Transform a Proj(DivMod) with a non-zero value.
2325 * Removes the exceptions and routes the memory to the NoMem node.
2327 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2329 ir_node *n = get_Proj_pred(proj);
2330 ir_node *b = get_DivMod_right(n);
2333 if (value_not_zero(b)) {
2334 /* DivMod(x, y) && y != 0 */
2335 proj_nr = get_Proj_proj(proj);
2337 /* this node may float if it did not depend on a Confirm */
2338 set_irn_pinned(n, op_pin_state_floats);
2340 if (proj_nr == pn_DivMod_X_except) {
2341 /* we found an exception handler, remove it */
2342 DBG_OPT_EXC_REM(proj);
2345 else if (proj_nr == pn_DivMod_M) {
2346 ir_node *res = get_DivMod_mem(n);
2347 /* the memory Proj can only be removed if we divide by a
2348 real constant, but the node never produce a new memory */
2349 if (value_of(b) != tarval_bad) {
2350 /* this is a DivMod by a const, we can remove the memory edge */
2351 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2355 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2356 /* a % a = 0 if a != 0 */
2357 ir_mode *mode = get_irn_mode(proj);
2358 ir_node *res = new_Const(mode, get_mode_null(mode));
2360 DBG_OPT_CSTEVAL(n, res);
2368 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2370 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2372 if (get_opt_unreachable_code()) {
2373 ir_node *n = get_Proj_pred(proj);
2374 ir_node *b = get_Cond_selector(n);
2376 if (mode_is_int(get_irn_mode(b))) {
2377 tarval *tb = value_of(b);
2379 if (tb != tarval_bad) {
2380 /* we have a constant switch */
2381 long num = get_Proj_proj(proj);
2383 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2384 if (get_tarval_long(tb) == num) {
2385 /* Do NOT create a jump here, or we will have 2 control flow ops
2386 * in a block. This case is optimized away in optimize_cf(). */
2390 /* this case will NEVER be taken, kill it */
2401 * Normalizes and optimizes Cmp nodes.
2403 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2405 if (get_opt_reassociation()) {
2406 ir_node *n = get_Proj_pred(proj);
2407 ir_node *left = get_Cmp_left(n);
2408 ir_node *right = get_Cmp_right(n);
2412 ir_mode *mode = NULL;
2413 long proj_nr = get_Proj_proj(proj);
2416 * First step: normalize the compare op
2417 * by placing the constant on the right site
2418 * or moving the lower address node to the left.
2419 * We ignore the case that both are constants
2420 * this case should be optimized away.
2422 if (get_irn_op(right) == op_Const)
2424 else if (get_irn_op(left) == op_Const) {
2429 proj_nr = get_inversed_pnc(proj_nr);
2432 else if (get_irn_idx(left) > get_irn_idx(right)) {
2438 proj_nr = get_inversed_pnc(proj_nr);
2443 * Second step: Try to reduce the magnitude
2444 * of a constant. This may help to generate better code
2445 * later and may help to normalize more compares.
2446 * Of course this is only possible for integer values.
2449 mode = get_irn_mode(c);
2450 tv = get_Const_tarval(c);
2452 if (tv != tarval_bad) {
2453 /* the following optimization is possible on modes without Overflow
2454 * on Unary Minus or on == and !=:
2455 * -a CMP c ==> a swap(CMP) -c
2457 * Beware: for two-complement Overflow may occur, so only == and != can
2458 * be optimized, see this:
2459 * -MININT < 0 =/=> MININT > 0 !!!
2461 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2462 (!mode_overflow_on_unary_Minus(mode) ||
2463 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2464 left = get_Minus_op(left);
2465 tv = tarval_sub(get_mode_null(mode), tv);
2467 proj_nr = get_inversed_pnc(proj_nr);
2471 /* for integer modes, we have more */
2472 if (mode_is_int(mode)) {
2473 /* Ne includes Unordered which is not possible on integers.
2474 * However, frontends often use this wrong, so fix it here */
2475 if (proj_nr & pn_Cmp_Uo) {
2476 proj_nr &= ~pn_Cmp_Uo;
2477 set_Proj_proj(proj, proj_nr);
2480 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2481 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2482 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2483 tv = tarval_sub(tv, get_mode_one(mode));
2485 proj_nr ^= pn_Cmp_Eq;
2488 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2489 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2490 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2491 tv = tarval_add(tv, get_mode_one(mode));
2493 proj_nr ^= pn_Cmp_Eq;
2497 /* the following reassociations work only for == and != */
2498 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2500 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2501 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2502 right = get_Sub_right(left);
2503 left = get_Sub_left(left);
2505 tv = value_of(right);
2509 if (tv != tarval_bad) {
2510 ir_op *op = get_irn_op(left);
2512 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2514 ir_node *c1 = get_Sub_right(left);
2515 tarval *tv2 = value_of(c1);
2517 if (tv2 != tarval_bad) {
2518 tv2 = tarval_add(tv, value_of(c1));
2520 if (tv2 != tarval_bad) {
2521 left = get_Sub_left(left);
2527 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2528 else if (op == op_Add) {
2529 ir_node *a_l = get_Add_left(left);
2530 ir_node *a_r = get_Add_right(left);
2534 if (get_irn_op(a_l) == op_Const) {
2536 tv2 = value_of(a_l);
2540 tv2 = value_of(a_r);
2543 if (tv2 != tarval_bad) {
2544 tv2 = tarval_sub(tv, tv2);
2546 if (tv2 != tarval_bad) {
2553 /* -a == c ==> a == -c, -a != c ==> a != -c */
2554 else if (op == op_Minus) {
2555 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2557 if (tv2 != tarval_bad) {
2558 left = get_Minus_op(left);
2565 /* the following reassociations work only for <= */
2566 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2567 if (tv != tarval_bad) {
2568 ir_op *op = get_irn_op(left);
2570 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2578 * optimization for AND:
2580 * And(x, C) == C ==> And(x, C) != 0
2581 * And(x, C) != C ==> And(X, C) == 0
2583 * if C is a single Bit constant.
2585 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2586 (get_irn_op(left) == op_And)) {
2587 if (is_single_bit_tarval(tv)) {
2588 /* check for Constant's match. We have check hare the tarvals,
2589 because our const might be changed */
2590 ir_node *la = get_And_left(left);
2591 ir_node *ra = get_And_right(left);
2592 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2593 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2594 /* fine: do the transformation */
2595 tv = get_mode_null(get_tarval_mode(tv));
2596 proj_nr ^= pn_Cmp_Leg;
2601 } /* tarval != bad */
2605 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2607 if (changed & 2) /* need a new Const */
2608 right = new_Const(mode, tv);
2610 /* create a new compare */
2611 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2614 set_Proj_pred(proj, n);
2615 set_Proj_proj(proj, proj_nr);
2622 * Does all optimizations on nodes that must be done on it's Proj's
2623 * because of creating new nodes.
2625 static ir_node *transform_node_Proj(ir_node *proj)
2627 ir_node *n = get_Proj_pred(proj);
2629 switch (get_irn_opcode(n)) {
2631 return transform_node_Proj_Div(proj);
2634 return transform_node_Proj_Mod(proj);
2637 return transform_node_Proj_DivMod(proj);
2640 return transform_node_Proj_Cond(proj);
2643 return transform_node_Proj_Cmp(proj);
2646 /* should not happen, but if it does will be optimized away */
2647 return equivalent_node_Proj(proj);
2656 * Move Confirms down through Phi nodes.
2658 static ir_node *transform_node_Phi(ir_node *phi) {
2660 ir_mode *mode = get_irn_mode(phi);
2662 if (mode_is_reference(mode)) {
2663 n = get_irn_arity(phi);
2665 /* Beware of Phi0 */
2667 ir_node *pred = get_irn_n(phi, 0);
2671 if (! is_Confirm(pred))
2674 bound = get_Confirm_bound(pred);
2675 pnc = get_Confirm_cmp(pred);
2677 for (i = 1; i < n; ++i) {
2678 pred = get_irn_n(phi, i);
2680 if (! is_Confirm(pred) ||
2681 get_Confirm_bound(pred) != bound ||
2682 get_Confirm_cmp(pred) != pnc)
2685 return new_r_Confirm(current_ir_graph, get_irn_n(phi, -1), phi, bound, pnc);
2692 * returns the operands of a commutative bin-op, if one operand is
2693 * a const, it is returned as the second one.
2695 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2697 ir_node *op_a = get_binop_left(binop);
2698 ir_node *op_b = get_binop_right(binop);
2700 assert(is_op_commutative(get_irn_op(binop)));
2702 if (get_irn_op(op_a) == op_Const) {
2713 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2714 * Such pattern may arise in bitfield stores.
2716 * value c4 value c4 & c2
2717 * AND c3 AND c1 | c3
2722 static ir_node *transform_node_Or_bf_store(ir_node *or)
2726 ir_node *and_l, *c3;
2727 ir_node *value, *c4;
2728 ir_node *new_and, *new_const, *block;
2729 ir_mode *mode = get_irn_mode(or);
2731 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2733 get_comm_Binop_Ops(or, &and, &c1);
2734 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2737 get_comm_Binop_Ops(and, &or_l, &c2);
2738 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2741 get_comm_Binop_Ops(or_l, &and_l, &c3);
2742 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2745 get_comm_Binop_Ops(and_l, &value, &c4);
2746 if (get_irn_op(c4) != op_Const)
2749 /* ok, found the pattern, check for conditions */
2750 assert(mode == get_irn_mode(and));
2751 assert(mode == get_irn_mode(or_l));
2752 assert(mode == get_irn_mode(and_l));
2754 tv1 = get_Const_tarval(c1);
2755 tv2 = get_Const_tarval(c2);
2756 tv3 = get_Const_tarval(c3);
2757 tv4 = get_Const_tarval(c4);
2759 tv = tarval_or(tv4, tv2);
2760 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2761 /* have at least one 0 at the same bit position */
2765 n_tv4 = tarval_not(tv4);
2766 if (tv3 != tarval_and(tv3, n_tv4)) {
2767 /* bit in the or_mask is outside the and_mask */
2771 n_tv2 = tarval_not(tv2);
2772 if (tv1 != tarval_and(tv1, n_tv2)) {
2773 /* bit in the or_mask is outside the and_mask */
2777 /* ok, all conditions met */
2778 block = get_irn_n(or, -1);
2780 new_and = new_r_And(current_ir_graph, block,
2781 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2783 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2785 set_Or_left(or, new_and);
2786 set_Or_right(or, new_const);
2788 /* check for more */
2789 return transform_node_Or_bf_store(or);
2793 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2795 static ir_node *transform_node_Or_Rot(ir_node *or)
2797 ir_mode *mode = get_irn_mode(or);
2798 ir_node *shl, *shr, *block;
2799 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2802 if (! mode_is_int(mode))
2805 shl = get_binop_left(or);
2806 shr = get_binop_right(or);
2808 if (get_irn_op(shl) == op_Shr) {
2809 if (get_irn_op(shr) != op_Shl)
2816 else if (get_irn_op(shl) != op_Shl)
2818 else if (get_irn_op(shr) != op_Shr)
2821 x = get_Shl_left(shl);
2822 if (x != get_Shr_left(shr))
2825 c1 = get_Shl_right(shl);
2826 c2 = get_Shr_right(shr);
2827 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2828 tv1 = get_Const_tarval(c1);
2829 if (! tarval_is_long(tv1))
2832 tv2 = get_Const_tarval(c2);
2833 if (! tarval_is_long(tv2))
2836 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2837 != get_mode_size_bits(mode))
2840 /* yet, condition met */
2841 block = get_irn_n(or, -1);
2843 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2845 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2848 else if (get_irn_op(c1) == op_Sub) {
2852 if (get_Sub_right(sub) != v)
2855 c1 = get_Sub_left(sub);
2856 if (get_irn_op(c1) != op_Const)
2859 tv1 = get_Const_tarval(c1);
2860 if (! tarval_is_long(tv1))
2863 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2866 /* yet, condition met */
2867 block = get_nodes_block(or);
2869 /* a Rot right is not supported, so use a rot left */
2870 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2872 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2875 else if (get_irn_op(c2) == op_Sub) {
2879 c1 = get_Sub_left(sub);
2880 if (get_irn_op(c1) != op_Const)
2883 tv1 = get_Const_tarval(c1);
2884 if (! tarval_is_long(tv1))
2887 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2890 /* yet, condition met */
2891 block = get_irn_n(or, -1);
2894 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2896 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2906 static ir_node *transform_node_Or(ir_node *or)
2908 or = transform_node_Or_bf_store(or);
2909 or = transform_node_Or_Rot(or);
2915 static ir_node *transform_node(ir_node *n);
2918 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2920 * Should be moved to reassociation?
2922 static ir_node *transform_node_shift(ir_node *n)
2924 ir_node *left, *right;
2925 tarval *tv1, *tv2, *res;
2927 int modulo_shf, flag;
2929 left = get_binop_left(n);
2931 /* different operations */
2932 if (get_irn_op(left) != get_irn_op(n))
2935 right = get_binop_right(n);
2936 tv1 = value_of(right);
2937 if (tv1 == tarval_bad)
2940 tv2 = value_of(get_binop_right(left));
2941 if (tv2 == tarval_bad)
2944 res = tarval_add(tv1, tv2);
2946 /* beware: a simple replacement works only, if res < modulo shift */
2947 mode = get_irn_mode(n);
2951 modulo_shf = get_mode_modulo_shift(mode);
2952 if (modulo_shf > 0) {
2953 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2955 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2962 /* ok, we can replace it */
2963 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2965 in[0] = get_binop_left(left);
2966 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2968 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2970 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2972 return transform_node(irn);
2977 #define transform_node_Shr transform_node_shift
2978 #define transform_node_Shrs transform_node_shift
2979 #define transform_node_Shl transform_node_shift
2982 * Remove dead blocks and nodes in dead blocks
2983 * in keep alive list. We do not generate a new End node.
2985 static ir_node *transform_node_End(ir_node *n) {
2986 int i, n_keepalives = get_End_n_keepalives(n);
2988 for (i = 0; i < n_keepalives; ++i) {
2989 ir_node *ka = get_End_keepalive(n, i);
2991 if (is_Block_dead(ka)) {
2992 set_End_keepalive(n, i, new_Bad());
2995 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2996 set_End_keepalive(n, i, new_Bad());
3002 * Optimize a Mux into some simpler cases.
3004 static ir_node *transform_node_Mux(ir_node *n)
3006 ir_node *oldn = n, *sel = get_Mux_sel(n);
3007 ir_mode *mode = get_irn_mode(n);
3009 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3010 ir_node *cmp = get_Proj_pred(sel);
3011 long proj_nr = get_Proj_proj(sel);
3012 ir_node *f = get_Mux_false(n);
3013 ir_node *t = get_Mux_true(n);
3015 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3016 ir_node *block = get_irn_n(n, -1);
3019 * Note: normalization puts the constant on the right site,
3020 * so we check only one case.
3022 * Note further that these optimization work even for floating point
3023 * with NaN's because -NaN == NaN.
3024 * However, if +0 and -0 is handled differently, we cannot use the first one.
3026 if (get_irn_op(f) == op_Minus &&
3027 get_Minus_op(f) == t &&
3028 get_Cmp_left(cmp) == t) {
3030 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3031 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3032 n = new_rd_Abs(get_irn_dbg_info(n),
3036 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3039 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3040 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3041 n = new_rd_Abs(get_irn_dbg_info(n),
3045 n = new_rd_Minus(get_irn_dbg_info(n),
3050 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3054 else if (get_irn_op(t) == op_Minus &&
3055 get_Minus_op(t) == f &&
3056 get_Cmp_left(cmp) == f) {
3058 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3059 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3060 n = new_rd_Abs(get_irn_dbg_info(n),
3064 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3067 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3068 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3069 n = new_rd_Abs(get_irn_dbg_info(n),
3073 n = new_rd_Minus(get_irn_dbg_info(n),
3078 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3083 if (mode_is_int(mode) && mode_is_signed(mode) &&
3084 get_mode_arithmetic(mode) == irma_twos_complement) {
3085 ir_node *x = get_Cmp_left(cmp);
3087 /* the following optimization works only with signed integer two-complement mode */
3089 if (mode == get_irn_mode(x)) {
3091 * FIXME: this restriction is two rigid, as it would still
3092 * work if mode(x) = Hs and mode == Is, but at least it removes
3095 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3096 classify_Const(t) == CNST_ALL_ONE &&
3097 classify_Const(f) == CNST_NULL) {
3099 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3103 n = new_rd_Shrs(get_irn_dbg_info(n),
3104 current_ir_graph, block, x,
3105 new_r_Const_long(current_ir_graph, block, mode_Iu,
3106 get_mode_size_bits(mode) - 1),
3108 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3111 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3112 classify_Const(t) == CNST_ONE &&
3113 classify_Const(f) == CNST_NULL) {
3115 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3119 n = new_rd_Shr(get_irn_dbg_info(n),
3120 current_ir_graph, block,
3121 new_r_Minus(current_ir_graph, block, x, mode),
3122 new_r_Const_long(current_ir_graph, block, mode_Iu,
3123 get_mode_size_bits(mode) - 1),
3125 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3132 return arch_transform_node_Mux(n);
3136 * Optimize a Psi into some simpler cases.
3138 static ir_node *transform_node_Psi(ir_node *n) {
3140 return transform_node_Mux(n);
3146 * Tries several [inplace] [optimizing] transformations and returns an
3147 * equivalent node. The difference to equivalent_node() is that these
3148 * transformations _do_ generate new nodes, and thus the old node must
3149 * not be freed even if the equivalent node isn't the old one.
3151 static ir_node *transform_node(ir_node *n)
3153 if (n->op->ops.transform_node)
3154 n = n->op->ops.transform_node(n);
3159 * sSets the default transform node operation for an ir_op_ops.
3161 * @param code the opcode for the default operation
3162 * @param ops the operations initialized
3167 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3171 ops->transform_node = transform_node_##a; \
3205 /* **************** Common Subexpression Elimination **************** */
3207 /** The size of the hash table used, should estimate the number of nodes
3209 #define N_IR_NODES 512
3211 /** Compares the attributes of two Const nodes. */
3212 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3214 return (get_Const_tarval(a) != get_Const_tarval(b))
3215 || (get_Const_type(a) != get_Const_type(b));
3218 /** Compares the attributes of two Proj nodes. */
3219 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3221 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3224 /** Compares the attributes of two Filter nodes. */
3225 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3227 return get_Filter_proj(a) != get_Filter_proj(b);
3230 /** Compares the attributes of two Alloc nodes. */
3231 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3233 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3234 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3237 /** Compares the attributes of two Free nodes. */
3238 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3240 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3241 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3244 /** Compares the attributes of two SymConst nodes. */
3245 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3247 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3248 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3249 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3252 /** Compares the attributes of two Call nodes. */
3253 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3255 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3258 /** Compares the attributes of two Sel nodes. */
3259 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3261 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3262 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3263 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3264 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3265 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3268 /** Compares the attributes of two Phi nodes. */
3269 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3271 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3274 /** Compares the attributes of two Cast nodes. */
3275 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3277 return get_Cast_type(a) != get_Cast_type(b);
3280 /** Compares the attributes of two Load nodes. */
3281 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3283 if (get_Load_volatility(a) == volatility_is_volatile ||
3284 get_Load_volatility(b) == volatility_is_volatile)
3285 /* NEVER do CSE on volatile Loads */
3288 return get_Load_mode(a) != get_Load_mode(b);
3291 /** Compares the attributes of two Store nodes. */
3292 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3294 /* NEVER do CSE on volatile Stores */
3295 return (get_Store_volatility(a) == volatility_is_volatile ||
3296 get_Store_volatility(b) == volatility_is_volatile);
3299 /** Compares the attributes of two Confirm nodes. */
3300 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3302 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3306 * Set the default node attribute compare operation for an ir_op_ops.
3308 * @param code the opcode for the default operation
3309 * @param ops the operations initialized
3314 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3318 ops->node_cmp_attr = node_cmp_attr_##a; \
3344 * Compare function for two nodes in the hash table. Gets two
3345 * nodes as parameters. Returns 0 if the nodes are a cse.
3347 int identities_cmp(const void *elt, const void *key)
3355 if (a == b) return 0;
3357 if ((get_irn_op(a) != get_irn_op(b)) ||
3358 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3360 /* compare if a's in and b's in are of equal length */
3361 irn_arity_a = get_irn_intra_arity (a);
3362 if (irn_arity_a != get_irn_intra_arity(b))
3365 /* for block-local cse and op_pin_state_pinned nodes: */
3366 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3367 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3371 /* compare a->in[0..ins] with b->in[0..ins] */
3372 for (i = 0; i < irn_arity_a; i++)
3373 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3377 * here, we already now that the nodes are identical except their
3380 if (a->op->ops.node_cmp_attr)
3381 return a->op->ops.node_cmp_attr(a, b);
3387 * Calculate a hash value of a node.
3390 ir_node_hash (ir_node *node)
3395 if (node->op == op_Const) {
3396 /* special value for const, as they only differ in their tarval. */
3397 h = HASH_PTR(node->attr.con.tv);
3398 h = 9*h + HASH_PTR(get_irn_mode(node));
3399 } else if (node->op == op_SymConst) {
3400 /* special value for const, as they only differ in their symbol. */
3401 h = HASH_PTR(node->attr.i.sym.type_p);
3402 h = 9*h + HASH_PTR(get_irn_mode(node));
3405 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3406 h = irn_arity = get_irn_intra_arity(node);
3408 /* consider all in nodes... except the block if not a control flow. */
3409 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3410 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3414 h = 9*h + HASH_PTR(get_irn_mode(node));
3416 h = 9*h + HASH_PTR(get_irn_op(node));
3423 new_identities(void) {
3424 return new_pset(identities_cmp, N_IR_NODES);
3428 del_identities(pset *value_table) {
3429 del_pset(value_table);
3433 * Return the canonical node computing the same value as n.
3434 * Looks up the node in a hash table.
3436 * For Const nodes this is performed in the constructor, too. Const
3437 * nodes are extremely time critical because of their frequent use in
3438 * constant string arrays.
3440 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3444 if (!value_table) return n;
3446 if (get_opt_reassociation()) {
3447 if (is_op_commutative(get_irn_op(n))) {
3448 ir_node *l = get_binop_left(n);
3449 ir_node *r = get_binop_right(n);
3451 /* for commutative operators perform a OP b == b OP a */
3452 if (get_irn_idx(l) > get_irn_idx(r)) {
3453 set_binop_left(n, r);
3454 set_binop_right(n, l);
3459 o = pset_find(value_table, n, ir_node_hash (n));
3468 * During construction we set the op_pin_state_pinned flag in the graph right when the
3469 * optimization is performed. The flag turning on procedure global cse could
3470 * be changed between two allocations. This way we are safe.
3472 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3475 n = identify(value_table, n);
3476 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3477 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3482 * Return the canonical node computing the same value as n.
3483 * Looks up the node in a hash table, enters it in the table
3484 * if it isn't there yet.
3486 ir_node *identify_remember(pset *value_table, ir_node *n)
3490 if (!value_table) return n;
3492 if (get_opt_reassociation()) {
3493 if (is_op_commutative(get_irn_op(n))) {
3494 ir_node *l = get_binop_left(n);
3495 ir_node *r = get_binop_right(n);
3497 /* for commutative operators perform a OP b == b OP a */
3499 set_binop_left(n, r);
3500 set_binop_right(n, l);
3505 /* lookup or insert in hash table with given hash key. */
3506 o = pset_insert (value_table, n, ir_node_hash (n));
3515 /* Add a node to the identities value table. */
3516 void add_identities(pset *value_table, ir_node *node) {
3517 if (get_opt_cse() && is_no_Block(node))
3518 identify_remember(value_table, node);
3521 /* Visit each node in the value table of a graph. */
3522 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3524 ir_graph *rem = current_ir_graph;
3526 current_ir_graph = irg;
3527 foreach_pset(irg->value_table, node)
3529 current_ir_graph = rem;
3533 * garbage in, garbage out. If a node has a dead input, i.e., the
3534 * Bad node is input to the node, return the Bad node.
3536 static INLINE ir_node *gigo(ir_node *node)
3539 ir_op *op = get_irn_op(node);
3541 /* remove garbage blocks by looking at control flow that leaves the block
3542 and replacing the control flow by Bad. */
3543 if (get_irn_mode(node) == mode_X) {
3544 ir_node *block = get_nodes_block(skip_Proj(node));
3546 /* Don't optimize nodes in immature blocks. */
3547 if (!get_Block_matured(block)) return node;
3548 /* Don't optimize End, may have Bads. */
3549 if (op == op_End) return node;
3551 if (is_Block(block)) {
3552 irn_arity = get_irn_arity(block);
3553 for (i = 0; i < irn_arity; i++) {
3554 if (!is_Bad(get_irn_n(block, i)))
3557 if (i == irn_arity) return new_Bad();
3561 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3562 blocks predecessors is dead. */
3563 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3564 irn_arity = get_irn_arity(node);
3567 * Beware: we can only read the block of a non-floating node.
3569 if (is_irn_pinned_in_irg(node) &&
3570 is_Block_dead(get_nodes_block(node)))
3573 for (i = 0; i < irn_arity; i++) {
3574 ir_node *pred = get_irn_n(node, i);
3579 /* Propagating Unknowns here seems to be a bad idea, because
3580 sometimes we need a node as a input and did not want that
3582 However, i might be useful to move this into a later phase
3583 (it you thing optimizing such code is useful). */
3584 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3585 return new_Unknown(get_irn_mode(node));
3590 /* With this code we violate the agreement that local_optimize
3591 only leaves Bads in Block, Phi and Tuple nodes. */
3592 /* If Block has only Bads as predecessors it's garbage. */
3593 /* If Phi has only Bads as predecessors it's garbage. */
3594 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3595 irn_arity = get_irn_arity(node);
3596 for (i = 0; i < irn_arity; i++) {
3597 if (!is_Bad(get_irn_n(node, i))) break;
3599 if (i == irn_arity) node = new_Bad();
3606 * These optimizations deallocate nodes from the obstack.
3607 * It can only be called if it is guaranteed that no other nodes
3608 * reference this one, i.e., right after construction of a node.
3610 * current_ir_graph must be set to the graph of the node!
3612 ir_node *optimize_node(ir_node *n)
3616 opcode iro = get_irn_opcode(n);
3618 /* Always optimize Phi nodes: part of the construction. */
3619 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3621 /* constant expression evaluation / constant folding */
3622 if (get_opt_constant_folding()) {
3623 /* neither constants nor Tuple values can be evaluated */
3624 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3625 /* try to evaluate */
3626 tv = computed_value(n);
3627 if (tv != tarval_bad) {
3629 ir_type *old_tp = get_irn_type(n);
3630 int i, arity = get_irn_arity(n);
3634 * Try to recover the type of the new expression.
3636 for (i = 0; i < arity && !old_tp; ++i)
3637 old_tp = get_irn_type(get_irn_n(n, i));
3640 * we MUST copy the node here temporary, because it's still needed
3641 * for DBG_OPT_CSTEVAL
3643 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3644 oldn = alloca(node_size);
3646 memcpy(oldn, n, node_size);
3647 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3649 /* ARG, copy the in array, we need it for statistics */
3650 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3652 /* note the inplace edges module */
3653 edges_node_deleted(n, current_ir_graph);
3655 /* evaluation was successful -- replace the node. */
3656 irg_kill_node(current_ir_graph, n);
3657 nw = new_Const(get_tarval_mode (tv), tv);
3659 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3660 set_Const_type(nw, old_tp);
3661 DBG_OPT_CSTEVAL(oldn, nw);
3667 /* remove unnecessary nodes */
3668 if (get_opt_constant_folding() ||
3669 (iro == iro_Phi) || /* always optimize these nodes. */
3671 (iro == iro_Proj) ||
3672 (iro == iro_Block) ) /* Flags tested local. */
3673 n = equivalent_node (n);
3675 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3677 /* Common Subexpression Elimination.
3679 * Checks whether n is already available.
3680 * The block input is used to distinguish different subexpressions. Right
3681 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3682 * subexpressions within a block.
3685 n = identify_cons (current_ir_graph->value_table, n);
3688 edges_node_deleted(oldn, current_ir_graph);
3690 /* We found an existing, better node, so we can deallocate the old node. */
3691 irg_kill_node(current_ir_graph, oldn);
3695 /* Some more constant expression evaluation that does not allow to
3697 iro = get_irn_opcode(n);
3698 if (get_opt_constant_folding() ||
3699 (iro == iro_Cond) ||
3700 (iro == iro_Proj) ||
3701 (iro == iro_Sel)) /* Flags tested local. */
3702 n = transform_node (n);
3704 /* Remove nodes with dead (Bad) input.
3705 Run always for transformation induced Bads. */
3708 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3709 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3710 n = identify_remember (current_ir_graph->value_table, n);
3718 * These optimizations never deallocate nodes (in place). This can cause dead
3719 * nodes lying on the obstack. Remove these by a dead node elimination,
3720 * i.e., a copying garbage collection.
3722 ir_node *optimize_in_place_2(ir_node *n)
3726 opcode iro = get_irn_opcode(n);
3728 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3730 /* constant expression evaluation / constant folding */
3731 if (get_opt_constant_folding()) {
3732 /* neither constants nor Tuple values can be evaluated */
3733 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3734 /* try to evaluate */
3735 tv = computed_value(n);
3736 if (tv != tarval_bad) {
3737 /* evaluation was successful -- replace the node. */
3738 ir_type *old_tp = get_irn_type(n);
3739 int i, arity = get_irn_arity(n);
3742 * Try to recover the type of the new expression.
3744 for (i = 0; i < arity && !old_tp; ++i)
3745 old_tp = get_irn_type(get_irn_n(n, i));
3747 n = new_Const(get_tarval_mode(tv), tv);
3749 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3750 set_Const_type(n, old_tp);
3752 DBG_OPT_CSTEVAL(oldn, n);
3758 /* remove unnecessary nodes */
3759 if (get_opt_constant_folding() ||
3760 (iro == iro_Phi) || /* always optimize these nodes. */
3761 (iro == iro_Id) || /* ... */
3762 (iro == iro_Proj) || /* ... */
3763 (iro == iro_Block) ) /* Flags tested local. */
3764 n = equivalent_node(n);
3766 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3768 /** common subexpression elimination **/
3769 /* Checks whether n is already available. */
3770 /* The block input is used to distinguish different subexpressions. Right
3771 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3772 subexpressions within a block. */
3773 if (get_opt_cse()) {
3774 n = identify(current_ir_graph->value_table, n);
3777 /* Some more constant expression evaluation. */
3778 iro = get_irn_opcode(n);
3779 if (get_opt_constant_folding() ||
3780 (iro == iro_Cond) ||
3781 (iro == iro_Proj) ||
3782 (iro == iro_Sel)) /* Flags tested local. */
3783 n = transform_node(n);
3785 /* Remove nodes with dead (Bad) input.
3786 Run always for transformation induced Bads. */
3789 /* Now we can verify the node, as it has no dead inputs any more. */
3792 /* Now we have a legal, useful node. Enter it in hash table for cse.
3793 Blocks should be unique anyways. (Except the successor of start:
3794 is cse with the start block!) */
3795 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3796 n = identify_remember(current_ir_graph->value_table, n);
3802 * Wrapper for external use, set proper status bits after optimization.
3804 ir_node *optimize_in_place(ir_node *n)
3806 /* Handle graph state */
3807 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3809 if (get_opt_global_cse())
3810 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3811 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3812 set_irg_outs_inconsistent(current_ir_graph);
3814 /* FIXME: Maybe we could also test whether optimizing the node can
3815 change the control graph. */
3816 set_irg_doms_inconsistent(current_ir_graph);
3817 return optimize_in_place_2 (n);
3821 * Sets the default operation for an ir_ops.
3823 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3825 ops = firm_set_default_computed_value(code, ops);
3826 ops = firm_set_default_equivalent_node(code, ops);
3827 ops = firm_set_default_transform_node(code, ops);
3828 ops = firm_set_default_node_cmp_attr(code, ops);
3829 ops = firm_set_default_get_type(code, ops);
3830 ops = firm_set_default_get_type_attr(code, ops);
3831 ops = firm_set_default_get_entity_attr(code, ops);