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"
47 /* Make types visible to allow most efficient access */
51 * Return the value of a Constant.
53 static tarval *computed_value_Const(ir_node *n) {
54 return get_Const_tarval(n);
55 } /* computed_value_Const */
58 * Return the value of a 'sizeof' or 'alignof' SymConst.
60 static tarval *computed_value_SymConst(ir_node *n) {
63 switch (get_SymConst_kind(n)) {
64 case symconst_type_size:
65 type = get_SymConst_type(n);
66 if (get_type_state(type) == layout_fixed)
67 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
69 case symconst_type_align:
70 type = get_SymConst_type(n);
71 if (get_type_state(type) == layout_fixed)
72 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
78 } /* computed_value_SymConst */
81 * Return the value of an Add.
83 static tarval *computed_value_Add(ir_node *n) {
84 ir_node *a = get_Add_left(n);
85 ir_node *b = get_Add_right(n);
87 tarval *ta = value_of(a);
88 tarval *tb = value_of(b);
90 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
91 return tarval_add(ta, tb);
94 } /* computed_value_Add */
97 * Return the value of a Sub.
100 static tarval *computed_value_Sub(ir_node *n) {
101 ir_node *a = get_Sub_left(n);
102 ir_node *b = get_Sub_right(n);
107 if (a == b && !is_Bad(a))
108 return get_mode_null(get_irn_mode(n));
113 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
114 return tarval_sub(ta, tb);
117 } /* computed_value_Sub */
120 * Return the value of a Carry.
121 * Special : a op 0, 0 op b
123 static tarval *computed_value_Carry(ir_node *n) {
124 ir_node *a = get_binop_left(n);
125 ir_node *b = get_binop_right(n);
126 ir_mode *m = get_irn_mode(n);
128 tarval *ta = value_of(a);
129 tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad)) {
133 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
135 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
136 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
137 return get_mode_null(m);
140 } /* computed_value_Carry */
143 * Return the value of a Borrow.
146 static tarval *computed_value_Borrow(ir_node *n) {
147 ir_node *a = get_binop_left(n);
148 ir_node *b = get_binop_right(n);
149 ir_mode *m = get_irn_mode(n);
151 tarval *ta = value_of(a);
152 tarval *tb = value_of(b);
154 if ((ta != tarval_bad) && (tb != tarval_bad)) {
155 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
156 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
157 return get_mode_null(m);
160 } /* computed_value_Borrow */
163 * Return the value of an unary Minus.
165 static tarval *computed_value_Minus(ir_node *n) {
166 ir_node *a = get_Minus_op(n);
167 tarval *ta = value_of(a);
169 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
170 return tarval_neg(ta);
173 } /* computed_value_Minus */
176 * Return the value of a Mul.
178 static tarval *computed_value_Mul(ir_node *n) {
179 ir_node *a = get_Mul_left(n);
180 ir_node *b = get_Mul_right(n);
182 tarval *ta = value_of(a);
183 tarval *tb = value_of(b);
185 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
186 return tarval_mul(ta, tb);
188 /* a*0 = 0 or 0*b = 0:
189 calls computed_value recursive and returns the 0 with proper
191 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
193 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
197 } /* computed_value_Mul */
200 * Return the value of a floating point Quot.
202 static tarval *computed_value_Quot(ir_node *n) {
203 ir_node *a = get_Quot_left(n);
204 ir_node *b = get_Quot_right(n);
206 tarval *ta = value_of(a);
207 tarval *tb = value_of(b);
209 /* This was missing in original implementation. Why? */
210 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
211 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
212 return tarval_quo(ta, tb);
215 } /* computed_value_Quot */
218 * Calculate the value of an integer Div of two nodes.
219 * Special case: 0 / b
221 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
222 tarval *ta = value_of(a);
223 tarval *tb = value_of(b);
225 /* Compute c1 / c2 or 0 / a, a != 0 */
226 if (ta != tarval_bad) {
227 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
228 return tarval_div(ta, tb);
229 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
233 } /* do_computed_value_Div */
236 * Return the value of an integer Div.
238 static tarval *computed_value_Div(ir_node *n) {
239 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
240 } /* computed_value_Div */
243 * Calculate the value of an integer Mod of two nodes.
244 * Special case: a % 1
246 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
247 tarval *ta = value_of(a);
248 tarval *tb = value_of(b);
250 /* Compute c1 % c2 or a % 1 */
251 if (tb != tarval_bad) {
252 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
253 return tarval_mod(ta, tb);
254 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
255 return get_mode_null(get_irn_mode(a));
258 } /* do_computed_value_Mod */
261 * Return the value of an integer Mod.
263 static tarval *computed_value_Mod(ir_node *n) {
264 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
265 } /* computed_value_Mod */
268 * Return the value of an Abs.
270 static tarval *computed_value_Abs(ir_node *n) {
271 ir_node *a = get_Abs_op(n);
272 tarval *ta = value_of(a);
274 if (ta != tarval_bad)
275 return tarval_abs(ta);
278 } /* computed_value_Abs */
281 * Return the value of an And.
282 * Special case: a & 0, 0 & b
284 static tarval *computed_value_And(ir_node *n) {
285 ir_node *a = get_And_left(n);
286 ir_node *b = get_And_right(n);
288 tarval *ta = value_of(a);
289 tarval *tb = value_of(b);
291 if ((ta != tarval_bad) && (tb != tarval_bad)) {
292 return tarval_and (ta, tb);
296 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
297 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
302 } /* computed_value_And */
305 * Return the value of an Or.
306 * Special case: a | 1...1, 1...1 | b
308 static tarval *computed_value_Or(ir_node *n) {
309 ir_node *a = get_Or_left(n);
310 ir_node *b = get_Or_right(n);
312 tarval *ta = value_of(a);
313 tarval *tb = value_of(b);
315 if ((ta != tarval_bad) && (tb != tarval_bad)) {
316 return tarval_or (ta, tb);
319 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
320 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
325 } /* computed_value_Or */
328 * Return the value of an Eor.
330 static tarval *computed_value_Eor(ir_node *n) {
331 ir_node *a = get_Eor_left(n);
332 ir_node *b = get_Eor_right(n);
337 return get_mode_null(get_irn_mode(n));
342 if ((ta != tarval_bad) && (tb != tarval_bad)) {
343 return tarval_eor (ta, tb);
346 } /* computed_value_Eor */
349 * Return the value of a Not.
351 static tarval *computed_value_Not(ir_node *n) {
352 ir_node *a = get_Not_op(n);
353 tarval *ta = value_of(a);
355 if (ta != tarval_bad)
356 return tarval_not(ta);
359 } /* computed_value_Not */
362 * Return the value of a Shl.
364 static tarval *computed_value_Shl(ir_node *n) {
365 ir_node *a = get_Shl_left(n);
366 ir_node *b = get_Shl_right(n);
368 tarval *ta = value_of(a);
369 tarval *tb = value_of(b);
371 if ((ta != tarval_bad) && (tb != tarval_bad)) {
372 return tarval_shl (ta, tb);
375 } /* computed_value_Shl */
378 * Return the value of a Shr.
380 static tarval *computed_value_Shr(ir_node *n) {
381 ir_node *a = get_Shr_left(n);
382 ir_node *b = get_Shr_right(n);
384 tarval *ta = value_of(a);
385 tarval *tb = value_of(b);
387 if ((ta != tarval_bad) && (tb != tarval_bad)) {
388 return tarval_shr (ta, tb);
391 } /* computed_value_Shr */
394 * Return the value of a Shrs.
396 static tarval *computed_value_Shrs(ir_node *n) {
397 ir_node *a = get_Shrs_left(n);
398 ir_node *b = get_Shrs_right(n);
400 tarval *ta = value_of(a);
401 tarval *tb = value_of(b);
403 if ((ta != tarval_bad) && (tb != tarval_bad)) {
404 return tarval_shrs (ta, tb);
407 } /* computed_value_Shrs */
410 * Return the value of a Rot.
412 static tarval *computed_value_Rot(ir_node *n)
414 ir_node *a = get_Rot_left(n);
415 ir_node *b = get_Rot_right(n);
417 tarval *ta = value_of(a);
418 tarval *tb = value_of(b);
420 if ((ta != tarval_bad) && (tb != tarval_bad)) {
421 return tarval_rot (ta, tb);
424 } /* computed_value_Rot */
427 * Return the value of a Conv.
429 static tarval *computed_value_Conv(ir_node *n)
431 ir_node *a = get_Conv_op(n);
432 tarval *ta = value_of(a);
434 if (ta != tarval_bad)
435 return tarval_convert_to(ta, get_irn_mode(n));
438 } /* computed_value_Conv */
441 * Return the value of a Proj(Cmp).
443 * This performs a first step of unreachable code elimination.
444 * Proj can not be computed, but folding a Cmp above the Proj here is
445 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
447 * There are several case where we can evaluate a Cmp node, see later.
449 static tarval *computed_value_Proj_Cmp(ir_node *n)
451 ir_node *a = get_Proj_pred(n);
452 ir_node *aa = get_Cmp_left(a);
453 ir_node *ab = get_Cmp_right(a);
454 long proj_nr = get_Proj_proj(n);
457 * BEWARE: a == a is NOT always True for floating Point values, as
458 * NaN != NaN is defined, so we must check this here.
461 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
464 /* This is a trick with the bits used for encoding the Cmp
465 Proj numbers, the following statement is not the same:
466 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
467 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
470 tarval *taa = value_of(aa);
471 tarval *tab = value_of(ab);
472 ir_mode *mode = get_irn_mode(aa);
475 * The predecessors of Cmp are target values. We can evaluate
478 if ((taa != tarval_bad) && (tab != tarval_bad)) {
479 /* strange checks... */
480 pn_Cmp flags = tarval_cmp(taa, tab);
481 if (flags != pn_Cmp_False) {
482 return new_tarval_from_long (proj_nr & flags, mode_b);
485 /* for integer values, we can check against MIN/MAX */
486 else if (mode_is_int(mode)) {
487 /* MIN <=/> x. This results in true/false. */
488 if (taa == get_mode_min(mode)) {
489 /* a compare with the MIN value */
490 if (proj_nr == pn_Cmp_Le)
491 return get_tarval_b_true();
492 else if (proj_nr == pn_Cmp_Gt)
493 return get_tarval_b_false();
495 /* x >=/< MIN. This results in true/false. */
497 if (tab == get_mode_min(mode)) {
498 /* a compare with the MIN value */
499 if (proj_nr == pn_Cmp_Ge)
500 return get_tarval_b_true();
501 else if (proj_nr == pn_Cmp_Lt)
502 return get_tarval_b_false();
504 /* MAX >=/< x. This results in true/false. */
505 else if (taa == get_mode_max(mode)) {
506 if (proj_nr == pn_Cmp_Ge)
507 return get_tarval_b_true();
508 else if (proj_nr == pn_Cmp_Lt)
509 return get_tarval_b_false();
511 /* x <=/> MAX. This results in true/false. */
512 else if (tab == get_mode_max(mode)) {
513 if (proj_nr == pn_Cmp_Le)
514 return get_tarval_b_true();
515 else if (proj_nr == pn_Cmp_Gt)
516 return get_tarval_b_false();
520 * The predecessors are Allocs or (void*)(0) constants. Allocs never
521 * return NULL, they raise an exception. Therefore we can predict
525 ir_node *aaa = skip_Id(skip_Proj(aa));
526 ir_node *aba = skip_Id(skip_Proj(ab));
528 if ( ( (/* aa is ProjP and aaa is Alloc */
529 (get_irn_op(aa) == op_Proj)
530 && (mode_is_reference(get_irn_mode(aa)))
531 && (get_irn_op(aaa) == op_Alloc))
532 && ( (/* ab is NULL */
533 (get_irn_op(ab) == op_Const)
534 && (mode_is_reference(get_irn_mode(ab)))
535 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
536 || (/* ab is other Alloc */
537 (get_irn_op(ab) == op_Proj)
538 && (mode_is_reference(get_irn_mode(ab)))
539 && (get_irn_op(aba) == op_Alloc)
541 || (/* aa is NULL and aba is Alloc */
542 (get_irn_op(aa) == op_Const)
543 && (mode_is_reference(get_irn_mode(aa)))
544 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
545 && (get_irn_op(ab) == op_Proj)
546 && (mode_is_reference(get_irn_mode(ab)))
547 && (get_irn_op(aba) == op_Alloc)))
549 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
552 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
553 } /* computed_value_Proj_Cmp */
556 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod).
558 static tarval *computed_value_Proj(ir_node *n) {
559 ir_node *a = get_Proj_pred(n);
562 switch (get_irn_opcode(a)) {
564 return computed_value_Proj_Cmp(n);
567 /* compute either the Div or the Mod part */
568 proj_nr = get_Proj_proj(n);
569 if (proj_nr == pn_DivMod_res_div)
570 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
571 else if (proj_nr == pn_DivMod_res_mod)
572 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
576 if (get_Proj_proj(n) == pn_Div_res)
577 return computed_value(a);
581 if (get_Proj_proj(n) == pn_Mod_res)
582 return computed_value(a);
589 } /* computed_value_Proj */
592 * Calculate the value of a Mux: can be evaluated, if the
593 * sel and the right input are known.
595 static tarval *computed_value_Mux(ir_node *n) {
596 ir_node *sel = get_Mux_sel(n);
597 tarval *ts = value_of(sel);
599 if (ts == get_tarval_b_true()) {
600 ir_node *v = get_Mux_true(n);
603 else if (ts == get_tarval_b_false()) {
604 ir_node *v = get_Mux_false(n);
608 } /* computed_value_Mux */
611 * Calculate the value of a Psi: can be evaluated, if a condition is true
612 * and all previous conditions are false. If all conditions are false
613 * we evaluate to the default one.
615 static tarval *computed_value_Psi(ir_node *n) {
617 return computed_value_Mux(n);
619 } /* computed_value_Psi */
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) {
626 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
627 value_of(get_Confirm_bound(n)) : tarval_bad;
628 } /* computed_value_Confirm */
631 * If the parameter n can be computed, return its value, else tarval_bad.
632 * Performs constant folding.
634 * @param n The node this should be evaluated
636 tarval *computed_value(ir_node *n) {
637 if (n->op->ops.computed_value)
638 return n->op->ops.computed_value(n);
640 } /* computed_value */
643 * Set the default computed_value evaluator in an ir_op_ops.
645 * @param code the opcode for the default operation
646 * @param ops the operations initialized
651 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
655 ops->computed_value = computed_value_##a; \
690 } /* firm_set_default_computed_value */
693 * Returns a equivalent block for another block.
694 * If the block has only one predecessor, this is
695 * the equivalent one. If the only predecessor of a block is
696 * the block itself, this is a dead block.
698 * If both predecessors of a block are the branches of a binary
699 * Cond, the equivalent block is Cond's block.
701 * If all predecessors of a block are bad or lies in a dead
702 * block, the current block is dead as well.
704 * Note, that blocks are NEVER turned into Bad's, instead
705 * the dead_block flag is set. So, never test for is_Bad(block),
706 * always use is_dead_Block(block).
708 static ir_node *equivalent_node_Block(ir_node *n)
711 int n_preds = get_Block_n_cfgpreds(n);
713 /* The Block constructor does not call optimize, but mature_immBlock
714 calls the optimization. */
715 assert(get_Block_matured(n));
717 /* Straightening: a single entry Block following a single exit Block
718 can be merged, if it is not the Start block. */
719 /* !!! Beware, all Phi-nodes of n must have been optimized away.
720 This should be true, as the block is matured before optimize is called.
721 But what about Phi-cycles with the Phi0/Id that could not be resolved?
722 Remaining Phi nodes are just Ids. */
723 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
724 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
725 if (predblock == oldn) {
726 /* Jmp jumps into the block it is in -- deal self cycle. */
727 n = set_Block_dead(n);
728 DBG_OPT_DEAD_BLOCK(oldn, n);
729 } else if (get_opt_control_flow_straightening()) {
731 DBG_OPT_STG(oldn, n);
734 else if ((n_preds == 1) &&
735 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
736 ir_node *predblock = get_Block_cfgpred_block(n, 0);
737 if (predblock == oldn) {
738 /* Jmp jumps into the block it is in -- deal self cycle. */
739 n = set_Block_dead(n);
740 DBG_OPT_DEAD_BLOCK(oldn, n);
743 else if ((n_preds == 2) &&
744 (get_opt_control_flow_weak_simplification())) {
745 /* Test whether Cond jumps twice to this block
746 * The more general case which more than 2 predecessors is handles
747 * in optimize_cf(), we handle only this special case for speed here.
749 ir_node *a = get_Block_cfgpred(n, 0);
750 ir_node *b = get_Block_cfgpred(n, 1);
752 if ((get_irn_op(a) == op_Proj) &&
753 (get_irn_op(b) == op_Proj) &&
754 (get_Proj_pred(a) == get_Proj_pred(b)) &&
755 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
756 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
757 /* Also a single entry Block following a single exit Block. Phis have
758 twice the same operand and will be optimized away. */
759 n = get_nodes_block(get_Proj_pred(a));
760 DBG_OPT_IFSIM1(oldn, a, b, n);
763 else if (get_opt_unreachable_code() &&
764 (n != get_irg_start_block(current_ir_graph)) &&
765 (n != get_irg_end_block(current_ir_graph)) ) {
768 /* If all inputs are dead, this block is dead too, except if it is
769 the start or end block. This is one step of unreachable code
771 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
772 ir_node *pred = get_Block_cfgpred(n, i);
775 if (is_Bad(pred)) continue;
776 pred_blk = get_nodes_block(skip_Proj(pred));
778 if (is_Block_dead(pred_blk)) continue;
781 /* really found a living input */
786 n = set_Block_dead(n);
787 DBG_OPT_DEAD_BLOCK(oldn, n);
792 } /* equivalent_node_Block */
795 * Returns a equivalent node for a Jmp, a Bad :-)
796 * Of course this only happens if the Block of the Jmp is dead.
798 static ir_node *equivalent_node_Jmp(ir_node *n) {
799 /* unreachable code elimination */
800 if (is_Block_dead(get_nodes_block(n)))
804 } /* equivalent_node_Jmp */
806 /** Raise is handled in the same way as Jmp. */
807 #define equivalent_node_Raise equivalent_node_Jmp
810 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
811 See transform_node_Proj_Cond(). */
814 * Optimize operations that are commutative and have neutral 0,
815 * so a op 0 = 0 op a = a.
817 static ir_node *equivalent_node_neutral_zero(ir_node *n)
821 ir_node *a = get_binop_left(n);
822 ir_node *b = get_binop_right(n);
827 /* After running compute_node there is only one constant predecessor.
828 Find this predecessors value and remember the other node: */
829 if ((tv = value_of(a)) != tarval_bad) {
831 } else if ((tv = value_of(b)) != tarval_bad) {
836 /* If this predecessors constant value is zero, the operation is
837 * unnecessary. Remove it.
839 * Beware: If n is a Add, the mode of on and n might be different
840 * which happens in this rare construction: NULL + 3.
841 * Then, a Conv would be needed which we cannot include here.
843 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
844 if (get_irn_mode(on) == get_irn_mode(n)) {
847 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
852 } /* equivalent_node_neutral_zero */
855 * Eor is commutative and has neutral 0.
857 #define equivalent_node_Eor equivalent_node_neutral_zero
860 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
862 * The second one looks strange, but this construct
863 * is used heavily in the LCC sources :-).
865 * Beware: The Mode of an Add may be different than the mode of its
866 * predecessors, so we could not return a predecessors in all cases.
868 static ir_node *equivalent_node_Add(ir_node *n)
871 ir_node *left, *right;
872 ir_mode *mode = get_irn_mode(n);
874 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
875 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
878 n = equivalent_node_neutral_zero(n);
882 left = get_Add_left(n);
883 right = get_Add_right(n);
885 if (get_irn_op(left) == op_Sub) {
886 if (get_Sub_right(left) == right) {
889 n = get_Sub_left(left);
890 if (mode == get_irn_mode(n)) {
891 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
896 if (get_irn_op(right) == op_Sub) {
897 if (get_Sub_right(right) == left) {
900 n = get_Sub_left(right);
901 if (mode == get_irn_mode(n)) {
902 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
908 } /* equivalent_node_Add */
911 * optimize operations that are not commutative but have neutral 0 on left,
914 static ir_node *equivalent_node_left_zero(ir_node *n) {
917 ir_node *a = get_binop_left(n);
918 ir_node *b = get_binop_right(n);
920 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
923 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
926 } /* equivalent_node_left_zero */
928 #define equivalent_node_Shl equivalent_node_left_zero
929 #define equivalent_node_Shr equivalent_node_left_zero
930 #define equivalent_node_Shrs equivalent_node_left_zero
931 #define equivalent_node_Rot equivalent_node_left_zero
934 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
936 * The second one looks strange, but this construct
937 * is used heavily in the LCC sources :-).
939 * Beware: The Mode of a Sub may be different than the mode of its
940 * predecessors, so we could not return a predecessors in all cases.
942 static ir_node *equivalent_node_Sub(ir_node *n)
946 ir_mode *mode = get_irn_mode(n);
948 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
949 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
953 b = get_Sub_right(n);
955 /* Beware: modes might be different */
956 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
957 if (mode == get_irn_mode(a)) {
960 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
963 else if (get_irn_op(a) == op_Add) {
964 if (mode_wrap_around(mode)) {
965 ir_node *left = get_Add_left(a);
966 ir_node *right = get_Add_right(a);
969 if (mode == get_irn_mode(right)) {
971 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
974 else if (right == b) {
975 if (mode == get_irn_mode(left)) {
977 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
983 } /* equivalent_node_Sub */
987 * Optimize an "idempotent unary op", ie op(op(n)) = n.
990 * -(-a) == a, but might overflow two times.
991 * We handle it anyway here but the better way would be a
992 * flag. This would be needed for Pascal for instance.
994 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
997 ir_node *pred = get_unop_op(n);
999 /* optimize symmetric unop */
1000 if (get_irn_op(pred) == get_irn_op(n)) {
1001 n = get_unop_op(pred);
1002 DBG_OPT_ALGSIM2(oldn, pred, n);
1005 } /* equivalent_node_idempotent_unop */
1007 /** Optimize Not(Not(x)) == x. */
1008 #define equivalent_node_Not equivalent_node_idempotent_unop
1010 /** --x == x ??? Is this possible or can --x raise an
1011 out of bounds exception if min =! max? */
1012 #define equivalent_node_Minus equivalent_node_idempotent_unop
1015 * Optimize a * 1 = 1 * a = a.
1017 static ir_node *equivalent_node_Mul(ir_node *n)
1020 ir_node *a = get_Mul_left(n);
1021 ir_node *b = get_Mul_right(n);
1023 /* Mul is commutative and has again an other neutral element. */
1024 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1026 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1027 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1029 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1032 } /* equivalent_node_Mul */
1035 * Optimize a / 1 = a.
1037 static ir_node *equivalent_node_Div(ir_node *n) {
1038 ir_node *a = get_Div_left(n);
1039 ir_node *b = get_Div_right(n);
1041 /* Div is not commutative. */
1042 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1043 /* Turn Div into a tuple (mem, bad, a) */
1044 ir_node *mem = get_Div_mem(n);
1045 turn_into_tuple(n, pn_Div_max);
1046 set_Tuple_pred(n, pn_Div_M, mem);
1047 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1048 set_Tuple_pred(n, pn_Div_res, a);
1051 } /* equivalent_node_Div */
1054 * Optimize a / 1.0 = a.
1056 static ir_node *equivalent_node_Quot(ir_node *n) {
1057 ir_node *a = get_Quot_left(n);
1058 ir_node *b = get_Quot_right(n);
1060 /* Div is not commutative. */
1061 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1062 /* Turn Quot into a tuple (mem, bad, a) */
1063 ir_node *mem = get_Quot_mem(n);
1064 turn_into_tuple(n, pn_Quot_max);
1065 set_Tuple_pred(n, pn_Quot_M, mem);
1066 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1067 set_Tuple_pred(n, pn_Quot_res, a);
1070 } /* equivalent_node_Quot */
1073 * Optimize a / 1 = a.
1075 static ir_node *equivalent_node_DivMod(ir_node *n) {
1076 ir_node *a = get_DivMod_left(n);
1077 ir_node *b = get_DivMod_right(n);
1079 /* Div is not commutative. */
1080 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1081 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1082 ir_node *mem = get_Div_mem(n);
1083 ir_mode *mode = get_irn_mode(b);
1085 turn_into_tuple(n, pn_DivMod_max);
1086 set_Tuple_pred(n, pn_DivMod_M, mem);
1087 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1088 set_Tuple_pred(n, pn_DivMod_res_div, a);
1089 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1092 } /* equivalent_node_DivMod */
1095 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1097 static ir_node *equivalent_node_Or(ir_node *n) {
1100 ir_node *a = get_Or_left(n);
1101 ir_node *b = get_Or_right(n);
1104 n = a; /* Or has it's own neutral element */
1105 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1106 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1109 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1111 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 } /* equivalent_node_Or */
1118 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1120 static ir_node *equivalent_node_And(ir_node *n) {
1123 ir_node *a = get_And_left(n);
1124 ir_node *b = get_And_right(n);
1127 n = a; /* And has it's own neutral element */
1128 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1129 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1131 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1132 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 } /* equivalent_node_And */
1140 * Try to remove useless Conv's:
1142 static ir_node *equivalent_node_Conv(ir_node *n) {
1144 ir_node *a = get_Conv_op(n);
1147 ir_mode *n_mode = get_irn_mode(n);
1148 ir_mode *a_mode = get_irn_mode(a);
1150 if (n_mode == a_mode) { /* No Conv necessary */
1151 /* leave strict floating point Conv's */
1152 if (get_Conv_strict(n))
1155 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1156 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1160 n_mode = get_irn_mode(n);
1161 b_mode = get_irn_mode(b);
1163 if (n_mode == b_mode) {
1164 if (n_mode == mode_b) {
1165 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1166 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1168 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1169 if (smaller_mode(b_mode, a_mode)){
1170 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1177 } /* equivalent_node_Conv */
1180 * A Cast may be removed if the type of the previous node
1181 * is already the type of the Cast.
1183 static ir_node *equivalent_node_Cast(ir_node *n) {
1185 ir_node *pred = get_Cast_op(n);
1187 if (get_irn_type(pred) == get_Cast_type(n)) {
1189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1192 } /* equivalent_node_Cast */
1195 * Several optimizations:
1196 * - no Phi in start block.
1197 * - remove Id operators that are inputs to Phi
1198 * - fold Phi-nodes, iff they have only one predecessor except
1201 static ir_node *equivalent_node_Phi(ir_node *n)
1206 ir_node *block = NULL; /* to shutup gcc */
1207 ir_node *first_val = NULL; /* to shutup gcc */
1209 if (!get_opt_normalize()) return n;
1211 n_preds = get_Phi_n_preds(n);
1213 block = get_nodes_block(n);
1214 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1215 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1216 if ((is_Block_dead(block)) || /* Control dead */
1217 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1218 return new_Bad(); /* in the Start Block. */
1220 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1222 /* If the Block has a Bad pred, we also have one. */
1223 for (i = 0; i < n_preds; ++i)
1224 if (is_Bad(get_Block_cfgpred(block, i)))
1225 set_Phi_pred(n, i, new_Bad());
1227 /* Find first non-self-referencing input */
1228 for (i = 0; i < n_preds; ++i) {
1229 first_val = get_Phi_pred(n, i);
1230 if ( (first_val != n) /* not self pointer */
1232 && (! is_Bad(first_val))
1234 ) { /* value not dead */
1235 break; /* then found first value. */
1240 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1244 /* search for rest of inputs, determine if any of these
1245 are non-self-referencing */
1246 while (++i < n_preds) {
1247 ir_node *scnd_val = get_Phi_pred(n, i);
1248 if ( (scnd_val != n)
1249 && (scnd_val != first_val)
1251 && (! is_Bad(scnd_val))
1259 /* Fold, if no multiple distinct non-self-referencing inputs */
1261 DBG_OPT_PHI(oldn, n);
1264 } /* equivalent_node_Phi */
1267 * Several optimizations:
1268 * - no Sync in start block.
1269 * - fold Sync-nodes, iff they have only one predecessor except
1272 static ir_node *equivalent_node_Sync(ir_node *n)
1277 ir_node *first_val = NULL; /* to shutup gcc */
1279 if (!get_opt_normalize()) return n;
1281 n_preds = get_Sync_n_preds(n);
1283 /* Find first non-self-referencing input */
1284 for (i = 0; i < n_preds; ++i) {
1285 first_val = get_Sync_pred(n, i);
1286 if ((first_val != n) /* not self pointer */ &&
1287 (! is_Bad(first_val))
1288 ) { /* value not dead */
1289 break; /* then found first value. */
1294 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1297 /* search the rest of inputs, determine if any of these
1298 are non-self-referencing */
1299 while (++i < n_preds) {
1300 ir_node *scnd_val = get_Sync_pred(n, i);
1301 if ((scnd_val != n) &&
1302 (scnd_val != first_val) &&
1303 (! is_Bad(scnd_val))
1309 /* Fold, if no multiple distinct non-self-referencing inputs */
1311 DBG_OPT_SYNC(oldn, n);
1314 } /* equivalent_node_Sync */
1317 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1318 * ProjX(Load) and ProjX(Store).
1320 static ir_node *equivalent_node_Proj(ir_node *n)
1324 ir_node *a = get_Proj_pred(n);
1326 if ( get_irn_op(a) == op_Tuple) {
1327 /* Remove the Tuple/Proj combination. */
1328 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1329 n = get_Tuple_pred(a, get_Proj_proj(n));
1330 DBG_OPT_TUPLE(oldn, a, n);
1332 assert(0); /* This should not happen! */
1336 else if (get_irn_mode(n) == mode_X) {
1337 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1338 /* Remove dead control flow -- early gigo(). */
1341 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1342 ir_op *op = get_irn_op(a);
1344 if (op == op_Load || op == op_Store) {
1345 /* get the load/store address */
1346 ir_node *addr = get_irn_n(a, 1);
1349 if (value_not_null(addr, &confirm)) {
1350 if (confirm == NULL) {
1351 /* this node may float if it did not depend on a Confirm */
1352 set_irn_pinned(a, op_pin_state_floats);
1362 } /* equivalent_node_Proj */
1367 static ir_node *equivalent_node_Id(ir_node *n) {
1372 } while (get_irn_op(n) == op_Id);
1374 DBG_OPT_ID(oldn, n);
1376 } /* equivalent_node_Id */
1381 static ir_node *equivalent_node_Mux(ir_node *n)
1383 ir_node *oldn = n, *sel = get_Mux_sel(n);
1384 tarval *ts = value_of(sel);
1386 /* Mux(true, f, t) == t */
1387 if (ts == tarval_b_true) {
1388 n = get_Mux_true(n);
1389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1391 /* Mux(false, f, t) == f */
1392 else if (ts == tarval_b_false) {
1393 n = get_Mux_false(n);
1394 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1396 /* Mux(v, x, x) == x */
1397 else if (get_Mux_false(n) == get_Mux_true(n)) {
1398 n = get_Mux_true(n);
1399 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1401 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1402 ir_node *cmp = get_Proj_pred(sel);
1403 long proj_nr = get_Proj_proj(sel);
1404 ir_node *b = get_Mux_false(n);
1405 ir_node *a = get_Mux_true(n);
1408 * Note: normalization puts the constant on the right site,
1409 * so we check only one case.
1411 * Note further that these optimization work even for floating point
1412 * with NaN's because -NaN == NaN.
1413 * However, if +0 and -0 is handled differently, we cannot use the first one.
1415 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1416 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1417 /* Mux(a CMP 0, X, a) */
1418 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1419 /* Mux(a CMP 0, -a, a) */
1420 if (proj_nr == pn_Cmp_Eq) {
1421 /* Mux(a == 0, -a, a) ==> -a */
1423 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1425 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1426 /* Mux(a != 0, -a, a) ==> a */
1428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1431 else if (classify_Const(b) == CNST_NULL) {
1432 /* Mux(a CMP 0, 0, a) */
1433 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1434 /* Mux(a != 0, 0, a) ==> a */
1436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1438 else if (proj_nr == pn_Cmp_Eq) {
1439 /* Mux(a == 0, 0, a) ==> 0 */
1441 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1448 } /* equivalent_node_Mux */
1451 * Returns a equivalent node of a Psi: if a condition is true
1452 * and all previous conditions are false we know its value.
1453 * If all conditions are false its value is the default one.
1455 static ir_node *equivalent_node_Psi(ir_node *n) {
1457 return equivalent_node_Mux(n);
1459 } /* equivalent_node_Psi */
1462 * Optimize -a CMP -b into b CMP a.
1463 * This works only for for modes where unary Minus
1465 * Note that two-complement integers can Overflow
1466 * so it will NOT work.
1468 static ir_node *equivalent_node_Cmp(ir_node *n)
1470 ir_node *left = get_Cmp_left(n);
1471 ir_node *right = get_Cmp_right(n);
1473 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1474 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1475 left = get_Minus_op(left);
1476 right = get_Minus_op(right);
1477 set_Cmp_left(n, right);
1478 set_Cmp_right(n, left);
1481 } /* equivalent_node_Cmp */
1484 * Remove Confirm nodes if setting is on.
1485 * Replace Confirms(x, '=', Constlike) by Constlike.
1487 static ir_node *equivalent_node_Confirm(ir_node *n) {
1488 ir_node *pred = get_Confirm_value(n);
1489 pn_Cmp pnc = get_Confirm_cmp(n);
1491 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1493 * rare case: two identical Confirms one after another,
1494 * replace the second one with the first.
1498 if (pnc == pn_Cmp_Eq) {
1499 ir_node *bound = get_Confirm_bound(n);
1502 * Optimize a rare case:
1503 * Confirm(x, '=', Constlike) ==> Constlike
1505 if (is_irn_constlike(bound)) {
1506 DBG_OPT_CONFIRM(n, bound);
1510 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1514 * Optimize CopyB(mem, x, x) into a Nop.
1516 static ir_node *equivalent_node_CopyB(ir_node *n) {
1517 ir_node *a = get_CopyB_dst(n);
1518 ir_node *b = get_CopyB_src(n);
1521 /* Turn CopyB into a tuple (mem, bad, bad) */
1522 ir_node *mem = get_CopyB_mem(n);
1523 turn_into_tuple(n, pn_CopyB_max);
1524 set_Tuple_pred(n, pn_CopyB_M, mem);
1525 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1526 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1529 } /* equivalent_node_CopyB */
1532 * Optimize Bounds(idx, idx, upper) into idx.
1534 static ir_node *equivalent_node_Bound(ir_node *n) {
1535 ir_node *idx = get_Bound_index(n);
1536 ir_node *lower = get_Bound_lower(n);
1539 /* By definition lower < upper, so if idx == lower -->
1540 lower <= idx && idx < upper */
1542 /* Turn Bound into a tuple (mem, bad, idx) */
1546 ir_node *pred = skip_Proj(idx);
1548 if (get_irn_op(pred) == op_Bound) {
1550 * idx was Bounds_check previously, it is still valid if
1551 * lower <= pred_lower && pred_upper <= upper.
1553 ir_node *upper = get_Bound_upper(n);
1554 if (get_Bound_lower(pred) == lower &&
1555 get_Bound_upper(pred) == upper) {
1557 * One could expect that we simply return the previous
1558 * Bound here. However, this would be wrong, as we could
1559 * add an exception Proj to a new location than.
1560 * So, we must turn in into a tuple
1567 /* Turn Bound into a tuple (mem, bad, idx) */
1568 ir_node *mem = get_Bound_mem(n);
1569 turn_into_tuple(n, pn_Bound_max);
1570 set_Tuple_pred(n, pn_Bound_M, mem);
1571 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1572 set_Tuple_pred(n, pn_Bound_res, idx);
1575 } /* equivalent_node_Bound */
1578 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1579 * perform no actual computation, as, e.g., the Id nodes. It does not create
1580 * new nodes. It is therefore safe to free n if the node returned is not n.
1581 * If a node returns a Tuple we can not just skip it. If the size of the
1582 * in array fits, we transform n into a tuple (e.g., Div).
1584 ir_node *equivalent_node(ir_node *n) {
1585 if (n->op->ops.equivalent_node)
1586 return n->op->ops.equivalent_node(n);
1588 } /* equivalent_node */
1591 * Sets the default equivalent node operation for an ir_op_ops.
1593 * @param code the opcode for the default operation
1594 * @param ops the operations initialized
1599 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1603 ops->equivalent_node = equivalent_node_##a; \
1643 } /* firm_set_default_equivalent_node */
1646 * Do node specific optimizations of nodes predecessors.
1648 static void optimize_preds(ir_node *n) {
1649 ir_node *a = NULL, *b = NULL;
1651 /* get the operands we will work on for simple cases. */
1653 a = get_binop_left(n);
1654 b = get_binop_right(n);
1655 } else if (is_unop(n)) {
1659 switch (get_irn_opcode(n)) {
1662 /* We don't want Cast as input to Cmp. */
1663 if (get_irn_op(a) == op_Cast) {
1667 if (get_irn_op(b) == op_Cast) {
1669 set_Cmp_right(n, b);
1675 } /* optimize_preds */
1678 * Returns non-zero if a node is a Phi node
1679 * with all predecessors constant.
1681 static int is_const_Phi(ir_node *n) {
1686 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1687 if (! is_Const(get_irn_n(n, i)))
1690 } /* is_const_Phi */
1693 * Apply an evaluator on a binop with a constant operators (and one Phi).
1695 * @param phi the Phi node
1696 * @param other the other operand
1697 * @param eval an evaluator function
1698 * @param left if non-zero, other is the left operand, else the right
1700 * @return a new Phi node if the conversion was successful, NULL else
1702 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1708 int i, n = get_irn_arity(phi);
1710 NEW_ARR_A(void *, res, n);
1712 for (i = 0; i < n; ++i) {
1713 pred = get_irn_n(phi, i);
1714 tv = get_Const_tarval(pred);
1715 tv = eval(other, tv);
1717 if (tv == tarval_bad) {
1718 /* folding failed, bad */
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = eval(tv, other);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1737 mode = get_irn_mode(phi);
1738 irg = current_ir_graph;
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1742 mode, res[i], get_Const_type(pred));
1744 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1745 } /* apply_binop_on_phi */
1748 * Apply an evaluator on a unop with a constant operator (a Phi).
1750 * @param phi the Phi node
1751 * @param eval an evaluator function
1753 * @return a new Phi node if the conversion was successful, NULL else
1755 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1761 int i, n = get_irn_arity(phi);
1763 NEW_ARR_A(void *, res, n);
1764 for (i = 0; i < n; ++i) {
1765 pred = get_irn_n(phi, i);
1766 tv = get_Const_tarval(pred);
1769 if (tv == tarval_bad) {
1770 /* folding failed, bad */
1775 mode = get_irn_mode(phi);
1776 irg = current_ir_graph;
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(phi, i);
1779 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1780 mode, res[i], get_Const_type(pred));
1782 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1783 } /* apply_unop_on_phi */
1786 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1787 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1788 * If possible, remove the Conv's.
1790 static ir_node *transform_node_AddSub(ir_node *n)
1792 ir_mode *mode = get_irn_mode(n);
1794 if (mode_is_reference(mode)) {
1795 ir_node *left = get_binop_left(n);
1796 ir_node *right = get_binop_right(n);
1797 int ref_bits = get_mode_size_bits(mode);
1799 if (get_irn_op(left) == op_Conv) {
1800 ir_mode *mode = get_irn_mode(left);
1801 int bits = get_mode_size_bits(mode);
1803 if (ref_bits == bits &&
1804 mode_is_int(mode) &&
1805 get_mode_arithmetic(mode) == irma_twos_complement) {
1806 ir_node *pre = get_Conv_op(left);
1807 ir_mode *pre_mode = get_irn_mode(pre);
1809 if (mode_is_int(pre_mode) &&
1810 get_mode_size_bits(pre_mode) == bits &&
1811 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1812 /* ok, this conv just changes to sign, moreover the calculation
1813 * is done with same number of bits as our address mode, so
1814 * we can ignore the conv as address calculation can be viewed
1815 * as either signed or unsigned
1817 set_binop_left(n, pre);
1822 if (get_irn_op(right) == op_Conv) {
1823 ir_mode *mode = get_irn_mode(right);
1824 int bits = get_mode_size_bits(mode);
1826 if (ref_bits == bits &&
1827 mode_is_int(mode) &&
1828 get_mode_arithmetic(mode) == irma_twos_complement) {
1829 ir_node *pre = get_Conv_op(right);
1830 ir_mode *pre_mode = get_irn_mode(pre);
1832 if (mode_is_int(pre_mode) &&
1833 get_mode_size_bits(pre_mode) == bits &&
1834 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1835 /* ok, this conv just changes to sign, moreover the calculation
1836 * is done with same number of bits as our address mode, so
1837 * we can ignore the conv as address calculation can be viewed
1838 * as either signed or unsigned
1840 set_binop_right(n, pre);
1846 } /* transform_node_AddSub */
1848 #define HANDLE_BINOP_PHI(op,a,b,c) \
1850 if (is_Const(b) && is_const_Phi(a)) { \
1851 /* check for Op(Phi, Const) */ \
1852 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1854 else if (is_Const(a) && is_const_Phi(b)) { \
1855 /* check for Op(Const, Phi) */ \
1856 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1859 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1863 #define HANDLE_UNOP_PHI(op,a,c) \
1865 if (is_const_Phi(a)) { \
1866 /* check for Op(Phi) */ \
1867 c = apply_unop_on_phi(a, op); \
1870 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1876 * Do the AddSub optimization, then Transform
1877 * Constant folding on Phi
1878 * Add(a,a) -> Mul(a, 2)
1879 * Add(Mul(a, x), a) -> Mul(a, x+1)
1880 * if the mode is integer or float.
1881 * Transform Add(a,-b) into Sub(a,b).
1882 * Reassociation might fold this further.
1884 static ir_node *transform_node_Add(ir_node *n)
1887 ir_node *a, *b, *c, *oldn = n;
1889 n = transform_node_AddSub(n);
1891 a = get_Add_left(n);
1892 b = get_Add_right(n);
1894 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1896 mode = get_irn_mode(n);
1898 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1899 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1902 if (mode_is_num(mode)) {
1904 ir_node *block = get_irn_n(n, -1);
1907 get_irn_dbg_info(n),
1911 new_r_Const_long(current_ir_graph, block, mode, 2),
1913 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1915 else if (get_irn_op(a) == op_Minus) {
1917 get_irn_dbg_info(n),
1923 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1925 else if (get_irn_op(b) == op_Minus) {
1927 get_irn_dbg_info(n),
1933 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1935 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1936 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1937 ir_node *ma = get_Mul_left(a);
1938 ir_node *mb = get_Mul_right(a);
1941 ir_node *blk = get_irn_n(n, -1);
1943 get_irn_dbg_info(n), current_ir_graph, blk,
1946 get_irn_dbg_info(n), current_ir_graph, blk,
1948 new_r_Const_long(current_ir_graph, blk, mode, 1),
1951 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1954 ir_node *blk = get_irn_n(n, -1);
1956 get_irn_dbg_info(n), current_ir_graph, blk,
1959 get_irn_dbg_info(n), current_ir_graph, blk,
1961 new_r_Const_long(current_ir_graph, blk, mode, 1),
1964 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1967 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1968 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1969 ir_node *ma = get_Mul_left(b);
1970 ir_node *mb = get_Mul_right(b);
1973 ir_node *blk = get_irn_n(n, -1);
1975 get_irn_dbg_info(n), current_ir_graph, blk,
1978 get_irn_dbg_info(n), current_ir_graph, blk,
1980 new_r_Const_long(current_ir_graph, blk, mode, 1),
1983 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1986 ir_node *blk = get_irn_n(n, -1);
1988 get_irn_dbg_info(n), current_ir_graph, blk,
1991 get_irn_dbg_info(n), current_ir_graph, blk,
1993 new_r_Const_long(current_ir_graph, blk, mode, 1),
1996 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2001 } /* transform_node_Add */
2004 * Do the AddSub optimization, then Transform
2005 * Constant folding on Phi
2006 * Sub(0,a) -> Minus(a)
2007 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2008 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2010 static ir_node *transform_node_Sub(ir_node *n)
2016 n = transform_node_AddSub(n);
2018 a = get_Sub_left(n);
2019 b = get_Sub_right(n);
2021 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2023 mode = get_irn_mode(n);
2025 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2026 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2029 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
2031 get_irn_dbg_info(n),
2036 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2038 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2039 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2040 ir_node *ma = get_Mul_left(a);
2041 ir_node *mb = get_Mul_right(a);
2044 ir_node *blk = get_irn_n(n, -1);
2046 get_irn_dbg_info(n),
2047 current_ir_graph, blk,
2050 get_irn_dbg_info(n),
2051 current_ir_graph, blk,
2053 new_r_Const_long(current_ir_graph, blk, mode, 1),
2056 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2059 ir_node *blk = get_irn_n(n, -1);
2061 get_irn_dbg_info(n),
2062 current_ir_graph, blk,
2065 get_irn_dbg_info(n),
2066 current_ir_graph, blk,
2068 new_r_Const_long(current_ir_graph, blk, mode, 1),
2071 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2074 else if (get_irn_op(a) == op_Sub) {
2075 ir_node *x = get_Sub_left(a);
2076 ir_node *y = get_Sub_right(a);
2077 ir_node *blk = get_irn_n(n, -1);
2078 ir_mode *m_b = get_irn_mode(b);
2079 ir_mode *m_y = get_irn_mode(y);
2082 /* Determine the right mode for the Add. */
2085 else if (mode_is_reference(m_b))
2087 else if (mode_is_reference(m_y))
2091 * Both modes are different but none is reference,
2092 * happens for instance in SubP(SubP(P, Iu), Is).
2093 * We have two possibilities here: Cast or ignore.
2094 * Currently we ignore this case.
2099 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2102 set_Sub_right(n, add);
2103 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2107 } /* transform_node_Sub */
2110 * Transform Mul(a,-1) into -a.
2111 * Do constant evaluation of Phi nodes.
2112 * Do architecture dependent optimizations on Mul nodes
2114 static ir_node *transform_node_Mul(ir_node *n) {
2115 ir_node *c, *oldn = n;
2116 ir_node *a = get_Mul_left(n);
2117 ir_node *b = get_Mul_right(n);
2120 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2122 mode = get_irn_mode(n);
2123 if (mode_is_signed(mode)) {
2126 if (value_of(a) == get_mode_minus_one(mode))
2128 else if (value_of(b) == get_mode_minus_one(mode))
2131 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2132 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2136 return arch_dep_replace_mul_with_shifts(n);
2137 } /* transform_node_Mul */
2140 * Transform a Div Node.
2142 static ir_node *transform_node_Div(ir_node *n)
2144 tarval *tv = value_of(n);
2147 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2149 if (tv != tarval_bad) {
2150 value = new_Const(get_tarval_mode(tv), tv);
2152 DBG_OPT_CSTEVAL(n, value);
2154 else /* Try architecture dependent optimization */
2155 value = arch_dep_replace_div_by_const(n);
2158 /* Turn Div into a tuple (mem, bad, value) */
2159 ir_node *mem = get_Div_mem(n);
2161 turn_into_tuple(n, pn_Div_max);
2162 set_Tuple_pred(n, pn_Div_M, mem);
2163 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2164 set_Tuple_pred(n, pn_Div_res, value);
2167 } /* transform_node_Div */
2170 * Transform a Mod node.
2172 static ir_node *transform_node_Mod(ir_node *n)
2174 tarval *tv = value_of(n);
2177 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2179 if (tv != tarval_bad) {
2180 value = new_Const(get_tarval_mode(tv), tv);
2182 DBG_OPT_CSTEVAL(n, value);
2184 else /* Try architecture dependent optimization */
2185 value = arch_dep_replace_mod_by_const(n);
2188 /* Turn Mod into a tuple (mem, bad, value) */
2189 ir_node *mem = get_Mod_mem(n);
2191 turn_into_tuple(n, pn_Mod_max);
2192 set_Tuple_pred(n, pn_Mod_M, mem);
2193 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2194 set_Tuple_pred(n, pn_Mod_res, value);
2197 } /* transform_node_Mod */
2200 * Transform a DivMod node.
2202 static ir_node *transform_node_DivMod(ir_node *n)
2206 ir_node *a = get_DivMod_left(n);
2207 ir_node *b = get_DivMod_right(n);
2208 ir_mode *mode = get_irn_mode(a);
2209 tarval *ta = value_of(a);
2210 tarval *tb = value_of(b);
2212 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2215 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2217 if (tb != tarval_bad) {
2218 if (tb == get_mode_one(get_tarval_mode(tb))) {
2219 b = new_Const (mode, get_mode_null(mode));
2222 DBG_OPT_CSTEVAL(n, b);
2224 else if (ta != tarval_bad) {
2225 tarval *resa, *resb;
2226 resa = tarval_div (ta, tb);
2227 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2228 Jmp for X result!? */
2229 resb = tarval_mod (ta, tb);
2230 if (resb == tarval_bad) return n; /* Causes exception! */
2231 a = new_Const (mode, resa);
2232 b = new_Const (mode, resb);
2235 DBG_OPT_CSTEVAL(n, a);
2236 DBG_OPT_CSTEVAL(n, b);
2238 else { /* Try architecture dependent optimization */
2239 arch_dep_replace_divmod_by_const(&a, &b, n);
2240 evaluated = a != NULL;
2242 } else if (ta == get_mode_null(mode)) {
2243 /* 0 / non-Const = 0 */
2248 if (evaluated) { /* replace by tuple */
2249 ir_node *mem = get_DivMod_mem(n);
2250 turn_into_tuple(n, pn_DivMod_max);
2251 set_Tuple_pred(n, pn_DivMod_M, mem);
2252 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2253 set_Tuple_pred(n, pn_DivMod_res_div, a);
2254 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2258 } /* transform_node_DivMod */
2261 * Optimize Abs(x) into x if x is Confirmed >= 0
2262 * Optimize Abs(x) into -x if x is Confirmed <= 0
2264 static ir_node *transform_node_Abs(ir_node *n)
2267 ir_node *a = get_Abs_op(n);
2268 value_classify_sign sign = classify_value_sign(a);
2270 if (sign == value_classified_negative) {
2271 ir_mode *mode = get_irn_mode(n);
2274 * We can replace the Abs by -x here.
2275 * We even could add a new Confirm here.
2277 * Note that -x would create a new node, so we could
2278 * not run it in the equivalent_node() context.
2280 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2281 get_irn_n(n, -1), a, mode);
2283 DBG_OPT_CONFIRM(oldn, n);
2285 else if (sign == value_classified_positive) {
2286 /* n is positive, Abs is not needed */
2289 DBG_OPT_CONFIRM(oldn, n);
2293 } /* transform_node_Abs */
2296 * Transform a Cond node.
2298 static ir_node *transform_node_Cond(ir_node *n)
2300 /* Replace the Cond by a Jmp if it branches on a constant
2303 ir_node *a = get_Cond_selector(n);
2304 tarval *ta = value_of(a);
2306 /* we need block info which is not available in floating irgs */
2307 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2310 if ((ta != tarval_bad) &&
2311 (get_irn_mode(a) == mode_b) &&
2312 (get_opt_unreachable_code())) {
2313 /* It's a boolean Cond, branching on a boolean constant.
2314 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2315 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2316 turn_into_tuple(n, pn_Cond_max);
2317 if (ta == tarval_b_true) {
2318 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2319 set_Tuple_pred(n, pn_Cond_true, jmp);
2321 set_Tuple_pred(n, pn_Cond_false, jmp);
2322 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2324 /* We might generate an endless loop, so keep it alive. */
2325 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2328 } /* transform_node_Cond */
2333 static ir_node *transform_node_And(ir_node *n)
2335 ir_node *c, *oldn = n;
2336 ir_node *a = get_And_left(n);
2337 ir_node *b = get_And_right(n);
2339 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2341 } /* transform_node_And */
2346 static ir_node *transform_node_Eor(ir_node *n)
2348 ir_node *c, *oldn = n;
2349 ir_node *a = get_Eor_left(n);
2350 ir_node *b = get_Eor_right(n);
2351 ir_mode *mode = get_irn_mode(n);
2353 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2357 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2358 mode, get_mode_null(mode));
2359 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2361 else if ((mode == mode_b)
2362 && (get_irn_op(a) == op_Proj)
2363 && (get_irn_mode(a) == mode_b)
2364 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2365 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2366 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2367 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2368 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2370 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2372 else if ((mode == mode_b)
2373 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2374 /* The Eor is a Not. Replace it by a Not. */
2375 /* ????!!!Extend to bitfield 1111111. */
2376 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2378 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2382 } /* transform_node_Eor */
2387 static ir_node *transform_node_Not(ir_node *n)
2389 ir_node *c, *oldn = n;
2390 ir_node *a = get_Not_op(n);
2392 HANDLE_UNOP_PHI(tarval_not,a,c);
2394 /* check for a boolean Not */
2395 if ( (get_irn_mode(n) == mode_b)
2396 && (get_irn_op(a) == op_Proj)
2397 && (get_irn_mode(a) == mode_b)
2398 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2399 /* We negate a Cmp. The Cmp has the negated result anyways! */
2400 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2401 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2405 } /* transform_node_Not */
2408 * Transform a Minus.
2410 static ir_node *transform_node_Minus(ir_node *n)
2412 ir_node *c, *oldn = n;
2413 ir_node *a = get_Minus_op(n);
2415 HANDLE_UNOP_PHI(tarval_neg,a,c);
2417 } /* transform_node_Minus */
2420 * Transform a Cast_type(Const) into a new Const_type
2422 static ir_node *transform_node_Cast(ir_node *n) {
2424 ir_node *pred = get_Cast_op(n);
2425 ir_type *tp = get_irn_type(n);
2427 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2428 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2429 get_Const_tarval(pred), tp);
2430 DBG_OPT_CSTEVAL(oldn, n);
2431 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2432 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2433 get_SymConst_kind(pred), tp);
2434 DBG_OPT_CSTEVAL(oldn, n);
2438 } /* transform_node_Cast */
2441 * Transform a Proj(Div) with a non-zero value.
2442 * Removes the exceptions and routes the memory to the NoMem node.
2444 static ir_node *transform_node_Proj_Div(ir_node *proj)
2446 ir_node *n = get_Proj_pred(proj);
2447 ir_node *b = get_Div_right(n);
2451 if (value_not_zero(b, &confirm)) {
2452 /* div(x, y) && y != 0 */
2453 proj_nr = get_Proj_proj(proj);
2454 if (proj_nr == pn_Div_X_except) {
2455 /* we found an exception handler, remove it */
2456 DBG_OPT_EXC_REM(proj);
2459 else if (proj_nr == pn_Div_M) {
2460 ir_node *res = get_Div_mem(n);
2461 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2464 /* This node can only float up to the Confirm block */
2465 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2467 set_irn_pinned(n, op_pin_state_floats);
2468 /* this is a Div without exception, we can remove the memory edge */
2469 set_Div_mem(n, new_mem);
2474 } /* transform_node_Proj_Div */
2477 * Transform a Proj(Mod) with a non-zero value.
2478 * Removes the exceptions and routes the memory to the NoMem node.
2480 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2482 ir_node *n = get_Proj_pred(proj);
2483 ir_node *b = get_Mod_right(n);
2487 if (value_not_zero(b, &confirm)) {
2488 /* mod(x, y) && y != 0 */
2489 proj_nr = get_Proj_proj(proj);
2491 if (proj_nr == pn_Mod_X_except) {
2492 /* we found an exception handler, remove it */
2493 DBG_OPT_EXC_REM(proj);
2495 } else if (proj_nr == pn_Mod_M) {
2496 ir_node *res = get_Mod_mem(n);
2497 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2500 /* This node can only float up to the Confirm block */
2501 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2503 set_irn_pinned(n, op_pin_state_floats);
2504 /* this is a Mod without exception, we can remove the memory edge */
2505 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2508 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2509 /* a % a = 0 if a != 0 */
2510 ir_mode *mode = get_irn_mode(proj);
2511 ir_node *res = new_Const(mode, get_mode_null(mode));
2513 DBG_OPT_CSTEVAL(n, res);
2518 } /* transform_node_Proj_Mod */
2521 * Transform a Proj(DivMod) with a non-zero value.
2522 * Removes the exceptions and routes the memory to the NoMem node.
2524 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2526 ir_node *n = get_Proj_pred(proj);
2527 ir_node *b = get_DivMod_right(n);
2531 if (value_not_zero(b, &confirm)) {
2532 /* DivMod(x, y) && y != 0 */
2533 proj_nr = get_Proj_proj(proj);
2535 if (proj_nr == pn_DivMod_X_except) {
2536 /* we found an exception handler, remove it */
2537 DBG_OPT_EXC_REM(proj);
2540 else if (proj_nr == pn_DivMod_M) {
2541 ir_node *res = get_DivMod_mem(n);
2542 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2545 /* This node can only float up to the Confirm block */
2546 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2548 set_irn_pinned(n, op_pin_state_floats);
2549 /* this is a DivMod without exception, we can remove the memory edge */
2550 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2553 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2554 /* a % a = 0 if a != 0 */
2555 ir_mode *mode = get_irn_mode(proj);
2556 ir_node *res = new_Const(mode, get_mode_null(mode));
2558 DBG_OPT_CSTEVAL(n, res);
2563 } /* transform_node_Proj_DivMod */
2566 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2568 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2570 if (get_opt_unreachable_code()) {
2571 ir_node *n = get_Proj_pred(proj);
2572 ir_node *b = get_Cond_selector(n);
2574 if (mode_is_int(get_irn_mode(b))) {
2575 tarval *tb = value_of(b);
2577 if (tb != tarval_bad) {
2578 /* we have a constant switch */
2579 long num = get_Proj_proj(proj);
2581 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2582 if (get_tarval_long(tb) == num) {
2583 /* Do NOT create a jump here, or we will have 2 control flow ops
2584 * in a block. This case is optimized away in optimize_cf(). */
2588 /* this case will NEVER be taken, kill it */
2596 } /* transform_node_Proj_Cond */
2599 * Normalizes and optimizes Cmp nodes.
2601 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2603 if (get_opt_reassociation()) {
2604 ir_node *n = get_Proj_pred(proj);
2605 ir_node *left = get_Cmp_left(n);
2606 ir_node *right = get_Cmp_right(n);
2610 ir_mode *mode = NULL;
2611 long proj_nr = get_Proj_proj(proj);
2614 * First step: normalize the compare op
2615 * by placing the constant on the right site
2616 * or moving the lower address node to the left.
2617 * We ignore the case that both are constants
2618 * this case should be optimized away.
2620 if (get_irn_op(right) == op_Const)
2622 else if (get_irn_op(left) == op_Const) {
2627 proj_nr = get_inversed_pnc(proj_nr);
2630 else if (get_irn_idx(left) > get_irn_idx(right)) {
2636 proj_nr = get_inversed_pnc(proj_nr);
2641 * Second step: Try to reduce the magnitude
2642 * of a constant. This may help to generate better code
2643 * later and may help to normalize more compares.
2644 * Of course this is only possible for integer values.
2647 mode = get_irn_mode(c);
2648 tv = get_Const_tarval(c);
2650 if (tv != tarval_bad) {
2651 /* the following optimization is possible on modes without Overflow
2652 * on Unary Minus or on == and !=:
2653 * -a CMP c ==> a swap(CMP) -c
2655 * Beware: for two-complement Overflow may occur, so only == and != can
2656 * be optimized, see this:
2657 * -MININT < 0 =/=> MININT > 0 !!!
2659 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2660 (!mode_overflow_on_unary_Minus(mode) ||
2661 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2662 left = get_Minus_op(left);
2663 tv = tarval_sub(get_mode_null(mode), tv);
2665 proj_nr = get_inversed_pnc(proj_nr);
2669 /* for integer modes, we have more */
2670 if (mode_is_int(mode)) {
2671 /* Ne includes Unordered which is not possible on integers.
2672 * However, frontends often use this wrong, so fix it here */
2673 if (proj_nr & pn_Cmp_Uo) {
2674 proj_nr &= ~pn_Cmp_Uo;
2675 set_Proj_proj(proj, proj_nr);
2678 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2679 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2680 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2681 tv = tarval_sub(tv, get_mode_one(mode));
2683 proj_nr ^= pn_Cmp_Eq;
2686 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2687 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2688 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2689 tv = tarval_add(tv, get_mode_one(mode));
2691 proj_nr ^= pn_Cmp_Eq;
2695 /* the following reassociations work only for == and != */
2696 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2698 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2699 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2700 right = get_Sub_right(left);
2701 left = get_Sub_left(left);
2703 tv = value_of(right);
2707 if (tv != tarval_bad) {
2708 ir_op *op = get_irn_op(left);
2710 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2712 ir_node *c1 = get_Sub_right(left);
2713 tarval *tv2 = value_of(c1);
2715 if (tv2 != tarval_bad) {
2716 tv2 = tarval_add(tv, value_of(c1));
2718 if (tv2 != tarval_bad) {
2719 left = get_Sub_left(left);
2725 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2726 else if (op == op_Add) {
2727 ir_node *a_l = get_Add_left(left);
2728 ir_node *a_r = get_Add_right(left);
2732 if (get_irn_op(a_l) == op_Const) {
2734 tv2 = value_of(a_l);
2738 tv2 = value_of(a_r);
2741 if (tv2 != tarval_bad) {
2742 tv2 = tarval_sub(tv, tv2);
2744 if (tv2 != tarval_bad) {
2751 /* -a == c ==> a == -c, -a != c ==> a != -c */
2752 else if (op == op_Minus) {
2753 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2755 if (tv2 != tarval_bad) {
2756 left = get_Minus_op(left);
2763 /* the following reassociations work only for <= */
2764 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2765 if (tv != tarval_bad) {
2766 ir_op *op = get_irn_op(left);
2768 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2776 * optimization for AND:
2778 * And(x, C) == C ==> And(x, C) != 0
2779 * And(x, C) != C ==> And(X, C) == 0
2781 * if C is a single Bit constant.
2783 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2784 (get_irn_op(left) == op_And)) {
2785 if (is_single_bit_tarval(tv)) {
2786 /* check for Constant's match. We have check hare the tarvals,
2787 because our const might be changed */
2788 ir_node *la = get_And_left(left);
2789 ir_node *ra = get_And_right(left);
2790 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2791 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2792 /* fine: do the transformation */
2793 tv = get_mode_null(get_tarval_mode(tv));
2794 proj_nr ^= pn_Cmp_Leg;
2799 } /* tarval != bad */
2803 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2805 if (changed & 2) /* need a new Const */
2806 right = new_Const(mode, tv);
2808 /* create a new compare */
2809 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2812 set_Proj_pred(proj, n);
2813 set_Proj_proj(proj, proj_nr);
2817 } /* transform_node_Proj_Cmp */
2820 * Does all optimizations on nodes that must be done on it's Proj's
2821 * because of creating new nodes.
2823 static ir_node *transform_node_Proj(ir_node *proj)
2825 ir_node *n = get_Proj_pred(proj);
2827 switch (get_irn_opcode(n)) {
2829 return transform_node_Proj_Div(proj);
2832 return transform_node_Proj_Mod(proj);
2835 return transform_node_Proj_DivMod(proj);
2838 return transform_node_Proj_Cond(proj);
2841 return transform_node_Proj_Cmp(proj);
2844 /* should not happen, but if it does will be optimized away */
2845 return equivalent_node_Proj(proj);
2851 } /* transform_node_Proj */
2854 * Move Confirms down through Phi nodes.
2856 static ir_node *transform_node_Phi(ir_node *phi) {
2858 ir_mode *mode = get_irn_mode(phi);
2860 if (mode_is_reference(mode)) {
2861 n = get_irn_arity(phi);
2863 /* Beware of Phi0 */
2865 ir_node *pred = get_irn_n(phi, 0);
2866 ir_node *bound, *new_Phi, *block, **in;
2869 if (! is_Confirm(pred))
2872 bound = get_Confirm_bound(pred);
2873 pnc = get_Confirm_cmp(pred);
2875 NEW_ARR_A(ir_node *, in, n);
2876 in[0] = get_Confirm_value(pred);
2878 for (i = 1; i < n; ++i) {
2879 pred = get_irn_n(phi, i);
2881 if (! is_Confirm(pred) ||
2882 get_Confirm_bound(pred) != bound ||
2883 get_Confirm_cmp(pred) != pnc)
2885 in[i] = get_Confirm_value(pred);
2887 /* move the Confirm nodes "behind" the Phi */
2888 block = get_irn_n(phi, -1);
2889 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2890 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2894 } /* transform_node_Phi */
2897 * Returns the operands of a commutative bin-op, if one operand is
2898 * a const, it is returned as the second one.
2900 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2902 ir_node *op_a = get_binop_left(binop);
2903 ir_node *op_b = get_binop_right(binop);
2905 assert(is_op_commutative(get_irn_op(binop)));
2907 if (get_irn_op(op_a) == op_Const) {
2915 } /* get_comm_Binop_Ops */
2918 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2919 * Such pattern may arise in bitfield stores.
2921 * value c4 value c4 & c2
2922 * AND c3 AND c1 | c3
2927 static ir_node *transform_node_Or_bf_store(ir_node *or)
2931 ir_node *and_l, *c3;
2932 ir_node *value, *c4;
2933 ir_node *new_and, *new_const, *block;
2934 ir_mode *mode = get_irn_mode(or);
2936 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2938 get_comm_Binop_Ops(or, &and, &c1);
2939 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2942 get_comm_Binop_Ops(and, &or_l, &c2);
2943 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2946 get_comm_Binop_Ops(or_l, &and_l, &c3);
2947 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2950 get_comm_Binop_Ops(and_l, &value, &c4);
2951 if (get_irn_op(c4) != op_Const)
2954 /* ok, found the pattern, check for conditions */
2955 assert(mode == get_irn_mode(and));
2956 assert(mode == get_irn_mode(or_l));
2957 assert(mode == get_irn_mode(and_l));
2959 tv1 = get_Const_tarval(c1);
2960 tv2 = get_Const_tarval(c2);
2961 tv3 = get_Const_tarval(c3);
2962 tv4 = get_Const_tarval(c4);
2964 tv = tarval_or(tv4, tv2);
2965 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2966 /* have at least one 0 at the same bit position */
2970 n_tv4 = tarval_not(tv4);
2971 if (tv3 != tarval_and(tv3, n_tv4)) {
2972 /* bit in the or_mask is outside the and_mask */
2976 n_tv2 = tarval_not(tv2);
2977 if (tv1 != tarval_and(tv1, n_tv2)) {
2978 /* bit in the or_mask is outside the and_mask */
2982 /* ok, all conditions met */
2983 block = get_irn_n(or, -1);
2985 new_and = new_r_And(current_ir_graph, block,
2986 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2988 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2990 set_Or_left(or, new_and);
2991 set_Or_right(or, new_const);
2993 /* check for more */
2994 return transform_node_Or_bf_store(or);
2995 } /* transform_node_Or_bf_store */
2998 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3000 static ir_node *transform_node_Or_Rot(ir_node *or)
3002 ir_mode *mode = get_irn_mode(or);
3003 ir_node *shl, *shr, *block;
3004 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3007 if (! mode_is_int(mode))
3010 shl = get_binop_left(or);
3011 shr = get_binop_right(or);
3013 if (get_irn_op(shl) == op_Shr) {
3014 if (get_irn_op(shr) != op_Shl)
3021 else if (get_irn_op(shl) != op_Shl)
3023 else if (get_irn_op(shr) != op_Shr)
3026 x = get_Shl_left(shl);
3027 if (x != get_Shr_left(shr))
3030 c1 = get_Shl_right(shl);
3031 c2 = get_Shr_right(shr);
3032 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3033 tv1 = get_Const_tarval(c1);
3034 if (! tarval_is_long(tv1))
3037 tv2 = get_Const_tarval(c2);
3038 if (! tarval_is_long(tv2))
3041 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3042 != get_mode_size_bits(mode))
3045 /* yet, condition met */
3046 block = get_irn_n(or, -1);
3048 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3050 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3053 else if (get_irn_op(c1) == op_Sub) {
3057 if (get_Sub_right(sub) != v)
3060 c1 = get_Sub_left(sub);
3061 if (get_irn_op(c1) != op_Const)
3064 tv1 = get_Const_tarval(c1);
3065 if (! tarval_is_long(tv1))
3068 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3071 /* yet, condition met */
3072 block = get_nodes_block(or);
3074 /* a Rot right is not supported, so use a rot left */
3075 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3077 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3080 else if (get_irn_op(c2) == op_Sub) {
3084 c1 = get_Sub_left(sub);
3085 if (get_irn_op(c1) != op_Const)
3088 tv1 = get_Const_tarval(c1);
3089 if (! tarval_is_long(tv1))
3092 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3095 /* yet, condition met */
3096 block = get_irn_n(or, -1);
3099 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3101 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3106 } /* transform_node_Or_Rot */
3111 static ir_node *transform_node_Or(ir_node *n)
3113 ir_node *c, *oldn = n;
3114 ir_node *a = get_Or_left(n);
3115 ir_node *b = get_Or_right(n);
3117 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3119 n = transform_node_Or_bf_store(n);
3120 n = transform_node_Or_Rot(n);
3123 } /* transform_node_Or */
3127 static ir_node *transform_node(ir_node *n);
3130 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3132 * Should be moved to reassociation?
3134 static ir_node *transform_node_shift(ir_node *n)
3136 ir_node *left, *right;
3137 tarval *tv1, *tv2, *res;
3139 int modulo_shf, flag;
3141 left = get_binop_left(n);
3143 /* different operations */
3144 if (get_irn_op(left) != get_irn_op(n))
3147 right = get_binop_right(n);
3148 tv1 = value_of(right);
3149 if (tv1 == tarval_bad)
3152 tv2 = value_of(get_binop_right(left));
3153 if (tv2 == tarval_bad)
3156 res = tarval_add(tv1, tv2);
3158 /* beware: a simple replacement works only, if res < modulo shift */
3159 mode = get_irn_mode(n);
3163 modulo_shf = get_mode_modulo_shift(mode);
3164 if (modulo_shf > 0) {
3165 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3167 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3174 /* ok, we can replace it */
3175 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3177 in[0] = get_binop_left(left);
3178 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3180 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3182 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3184 return transform_node(irn);
3187 } /* transform_node_shift */
3192 static ir_node *transform_node_Shr(ir_node *n)
3194 ir_node *c, *oldn = n;
3195 ir_node *a = get_Shr_left(n);
3196 ir_node *b = get_Shr_right(n);
3198 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3199 return transform_node_shift(n);
3200 } /* transform_node_Shr */
3205 static ir_node *transform_node_Shrs(ir_node *n)
3207 ir_node *c, *oldn = n;
3208 ir_node *a = get_Shrs_left(n);
3209 ir_node *b = get_Shrs_right(n);
3211 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3212 return transform_node_shift(n);
3213 } /* transform_node_Shrs */
3218 static ir_node *transform_node_Shl(ir_node *n)
3220 ir_node *c, *oldn = n;
3221 ir_node *a = get_Shl_left(n);
3222 ir_node *b = get_Shl_right(n);
3224 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3225 return transform_node_shift(n);
3226 } /* transform_node_Shl */
3229 * Remove dead blocks and nodes in dead blocks
3230 * in keep alive list. We do not generate a new End node.
3232 static ir_node *transform_node_End(ir_node *n) {
3233 int i, n_keepalives = get_End_n_keepalives(n);
3235 for (i = 0; i < n_keepalives; ++i) {
3236 ir_node *ka = get_End_keepalive(n, i);
3238 if (is_Block_dead(ka)) {
3239 set_End_keepalive(n, i, new_Bad());
3242 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3243 set_End_keepalive(n, i, new_Bad());
3246 } /* transform_node_End */
3249 * Optimize a Mux into some simpler cases.
3251 static ir_node *transform_node_Mux(ir_node *n)
3253 ir_node *oldn = n, *sel = get_Mux_sel(n);
3254 ir_mode *mode = get_irn_mode(n);
3256 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3257 ir_node *cmp = get_Proj_pred(sel);
3258 long proj_nr = get_Proj_proj(sel);
3259 ir_node *f = get_Mux_false(n);
3260 ir_node *t = get_Mux_true(n);
3262 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3263 ir_node *block = get_irn_n(n, -1);
3266 * Note: normalization puts the constant on the right site,
3267 * so we check only one case.
3269 * Note further that these optimization work even for floating point
3270 * with NaN's because -NaN == NaN.
3271 * However, if +0 and -0 is handled differently, we cannot use the first one.
3273 if (get_irn_op(f) == op_Minus &&
3274 get_Minus_op(f) == t &&
3275 get_Cmp_left(cmp) == t) {
3277 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3278 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3279 n = new_rd_Abs(get_irn_dbg_info(n),
3283 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3286 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3287 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3288 n = new_rd_Abs(get_irn_dbg_info(n),
3292 n = new_rd_Minus(get_irn_dbg_info(n),
3297 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3301 else if (get_irn_op(t) == op_Minus &&
3302 get_Minus_op(t) == f &&
3303 get_Cmp_left(cmp) == f) {
3305 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3306 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3307 n = new_rd_Abs(get_irn_dbg_info(n),
3311 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3314 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3315 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3316 n = new_rd_Abs(get_irn_dbg_info(n),
3320 n = new_rd_Minus(get_irn_dbg_info(n),
3325 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3330 if (mode_is_int(mode) && mode_is_signed(mode) &&
3331 get_mode_arithmetic(mode) == irma_twos_complement) {
3332 ir_node *x = get_Cmp_left(cmp);
3334 /* the following optimization works only with signed integer two-complement mode */
3336 if (mode == get_irn_mode(x)) {
3338 * FIXME: this restriction is two rigid, as it would still
3339 * work if mode(x) = Hs and mode == Is, but at least it removes
3342 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3343 classify_Const(t) == CNST_ALL_ONE &&
3344 classify_Const(f) == CNST_NULL) {
3346 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3350 n = new_rd_Shrs(get_irn_dbg_info(n),
3351 current_ir_graph, block, x,
3352 new_r_Const_long(current_ir_graph, block, mode_Iu,
3353 get_mode_size_bits(mode) - 1),
3355 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3358 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3359 classify_Const(t) == CNST_ONE &&
3360 classify_Const(f) == CNST_NULL) {
3362 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3366 n = new_rd_Shr(get_irn_dbg_info(n),
3367 current_ir_graph, block,
3368 new_r_Minus(current_ir_graph, block, x, mode),
3369 new_r_Const_long(current_ir_graph, block, mode_Iu,
3370 get_mode_size_bits(mode) - 1),
3372 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3379 return arch_transform_node_Mux(n);
3380 } /* transform_node_Mux */
3383 * Optimize a Psi into some simpler cases.
3385 static ir_node *transform_node_Psi(ir_node *n) {
3387 return transform_node_Mux(n);
3390 } /* transform_node_Psi */
3393 * Tries several [inplace] [optimizing] transformations and returns an
3394 * equivalent node. The difference to equivalent_node() is that these
3395 * transformations _do_ generate new nodes, and thus the old node must
3396 * not be freed even if the equivalent node isn't the old one.
3398 static ir_node *transform_node(ir_node *n)
3400 if (n->op->ops.transform_node)
3401 n = n->op->ops.transform_node(n);
3403 } /* transform_node */
3406 * sSets the default transform node operation for an ir_op_ops.
3408 * @param code the opcode for the default operation
3409 * @param ops the operations initialized
3414 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3418 ops->transform_node = transform_node_##a; \
3451 } /* firm_set_default_transform_node */
3454 /* **************** Common Subexpression Elimination **************** */
3456 /** The size of the hash table used, should estimate the number of nodes
3458 #define N_IR_NODES 512
3460 /** Compares the attributes of two Const nodes. */
3461 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3462 return (get_Const_tarval(a) != get_Const_tarval(b))
3463 || (get_Const_type(a) != get_Const_type(b));
3464 } /* node_cmp_attr_Const */
3466 /** Compares the attributes of two Proj nodes. */
3467 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3468 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3469 } /* node_cmp_attr_Proj */
3471 /** Compares the attributes of two Filter nodes. */
3472 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3473 return get_Filter_proj(a) != get_Filter_proj(b);
3474 } /* node_cmp_attr_Filter */
3476 /** Compares the attributes of two Alloc nodes. */
3477 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3478 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3479 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3480 } /* node_cmp_attr_Alloc */
3482 /** Compares the attributes of two Free nodes. */
3483 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3484 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3485 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3486 } /* node_cmp_attr_Free */
3488 /** Compares the attributes of two SymConst nodes. */
3489 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3490 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3491 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3492 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3493 } /* node_cmp_attr_SymConst */
3495 /** Compares the attributes of two Call nodes. */
3496 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3497 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3498 } /* node_cmp_attr_Call */
3500 /** Compares the attributes of two Sel nodes. */
3501 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3502 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3503 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3504 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3505 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3506 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3507 } /* node_cmp_attr_Sel */
3509 /** Compares the attributes of two Phi nodes. */
3510 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3511 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3512 } /* node_cmp_attr_Phi */
3514 /** Compares the attributes of two Conv nodes. */
3515 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3516 return get_Conv_strict(a) != get_Conv_strict(b);
3517 } /* node_cmp_attr_Conv */
3519 /** Compares the attributes of two Cast nodes. */
3520 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3521 return get_Cast_type(a) != get_Cast_type(b);
3522 } /* node_cmp_attr_Cast */
3524 /** Compares the attributes of two Load nodes. */
3525 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3526 if (get_Load_volatility(a) == volatility_is_volatile ||
3527 get_Load_volatility(b) == volatility_is_volatile)
3528 /* NEVER do CSE on volatile Loads */
3531 return get_Load_mode(a) != get_Load_mode(b);
3532 } /* node_cmp_attr_Load */
3534 /** Compares the attributes of two Store nodes. */
3535 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3536 /* NEVER do CSE on volatile Stores */
3537 return (get_Store_volatility(a) == volatility_is_volatile ||
3538 get_Store_volatility(b) == volatility_is_volatile);
3539 } /* node_cmp_attr_Store */
3541 /** Compares the attributes of two Confirm nodes. */
3542 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3543 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3544 } /* node_cmp_attr_Confirm */
3547 * Set the default node attribute compare operation for an ir_op_ops.
3549 * @param code the opcode for the default operation
3550 * @param ops the operations initialized
3555 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3559 ops->node_cmp_attr = node_cmp_attr_##a; \
3583 } /* firm_set_default_node_cmp_attr */
3586 * Compare function for two nodes in the hash table. Gets two
3587 * nodes as parameters. Returns 0 if the nodes are a cse.
3589 int identities_cmp(const void *elt, const void *key)
3597 if (a == b) return 0;
3599 if ((get_irn_op(a) != get_irn_op(b)) ||
3600 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3602 /* compare if a's in and b's in are of equal length */
3603 irn_arity_a = get_irn_intra_arity (a);
3604 if (irn_arity_a != get_irn_intra_arity(b))
3607 /* for block-local cse and op_pin_state_pinned nodes: */
3608 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3609 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3613 /* compare a->in[0..ins] with b->in[0..ins] */
3614 for (i = 0; i < irn_arity_a; i++)
3615 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3619 * here, we already now that the nodes are identical except their
3622 if (a->op->ops.node_cmp_attr)
3623 return a->op->ops.node_cmp_attr(a, b);
3626 } /* identities_cmp */
3629 * Calculate a hash value of a node.
3631 unsigned ir_node_hash(ir_node *node)
3636 if (node->op == op_Const) {
3637 /* special value for const, as they only differ in their tarval. */
3638 h = HASH_PTR(node->attr.con.tv);
3639 h = 9*h + HASH_PTR(get_irn_mode(node));
3640 } else if (node->op == op_SymConst) {
3641 /* special value for const, as they only differ in their symbol. */
3642 h = HASH_PTR(node->attr.symc.sym.type_p);
3643 h = 9*h + HASH_PTR(get_irn_mode(node));
3646 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3647 h = irn_arity = get_irn_intra_arity(node);
3649 /* consider all in nodes... except the block if not a control flow. */
3650 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3651 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3655 h = 9*h + HASH_PTR(get_irn_mode(node));
3657 h = 9*h + HASH_PTR(get_irn_op(node));
3661 } /* ir_node_hash */
3663 pset *new_identities(void) {
3664 return new_pset(identities_cmp, N_IR_NODES);
3665 } /* new_identities */
3667 void del_identities(pset *value_table) {
3668 del_pset(value_table);
3669 } /* del_identities */
3672 * Return the canonical node computing the same value as n.
3673 * Looks up the node in a hash table.
3675 * For Const nodes this is performed in the constructor, too. Const
3676 * nodes are extremely time critical because of their frequent use in
3677 * constant string arrays.
3679 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3683 if (!value_table) return n;
3685 if (get_opt_reassociation()) {
3686 if (is_op_commutative(get_irn_op(n))) {
3687 ir_node *l = get_binop_left(n);
3688 ir_node *r = get_binop_right(n);
3690 /* for commutative operators perform a OP b == b OP a */
3691 if (get_irn_idx(l) > get_irn_idx(r)) {
3692 set_binop_left(n, r);
3693 set_binop_right(n, l);
3698 o = pset_find(value_table, n, ir_node_hash(n));
3707 * During construction we set the op_pin_state_pinned flag in the graph right when the
3708 * optimization is performed. The flag turning on procedure global cse could
3709 * be changed between two allocations. This way we are safe.
3711 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3714 n = identify(value_table, n);
3715 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3716 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3718 } /* identify_cons */
3721 * Return the canonical node computing the same value as n.
3722 * Looks up the node in a hash table, enters it in the table
3723 * if it isn't there yet.
3725 ir_node *identify_remember(pset *value_table, ir_node *n)
3729 if (!value_table) return n;
3731 if (get_opt_reassociation()) {
3732 if (is_op_commutative(get_irn_op(n))) {
3733 ir_node *l = get_binop_left(n);
3734 ir_node *r = get_binop_right(n);
3736 /* for commutative operators perform a OP b == b OP a */
3738 set_binop_left(n, r);
3739 set_binop_right(n, l);
3744 /* lookup or insert in hash table with given hash key. */
3745 o = pset_insert (value_table, n, ir_node_hash (n));
3752 } /* identify_remember */
3754 /* Add a node to the identities value table. */
3755 void add_identities(pset *value_table, ir_node *node) {
3756 if (get_opt_cse() && is_no_Block(node))
3757 identify_remember(value_table, node);
3758 } /* add_identities */
3760 /* Visit each node in the value table of a graph. */
3761 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3763 ir_graph *rem = current_ir_graph;
3765 current_ir_graph = irg;
3766 foreach_pset(irg->value_table, node)
3768 current_ir_graph = rem;
3769 } /* visit_all_identities */
3772 * Garbage in, garbage out. If a node has a dead input, i.e., the
3773 * Bad node is input to the node, return the Bad node.
3775 static INLINE ir_node *gigo(ir_node *node)
3778 ir_op *op = get_irn_op(node);
3780 /* remove garbage blocks by looking at control flow that leaves the block
3781 and replacing the control flow by Bad. */
3782 if (get_irn_mode(node) == mode_X) {
3783 ir_node *block = get_nodes_block(skip_Proj(node));
3785 /* Don't optimize nodes in immature blocks. */
3786 if (!get_Block_matured(block)) return node;
3787 /* Don't optimize End, may have Bads. */
3788 if (op == op_End) return node;
3790 if (is_Block(block)) {
3791 irn_arity = get_irn_arity(block);
3792 for (i = 0; i < irn_arity; i++) {
3793 if (!is_Bad(get_irn_n(block, i)))
3796 if (i == irn_arity) return new_Bad();
3800 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3801 blocks predecessors is dead. */
3802 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3803 irn_arity = get_irn_arity(node);
3806 * Beware: we can only read the block of a non-floating node.
3808 if (is_irn_pinned_in_irg(node) &&
3809 is_Block_dead(get_nodes_block(node)))
3812 for (i = 0; i < irn_arity; i++) {
3813 ir_node *pred = get_irn_n(node, i);
3818 /* Propagating Unknowns here seems to be a bad idea, because
3819 sometimes we need a node as a input and did not want that
3821 However, it might be useful to move this into a later phase
3822 (if you think that optimizing such code is useful). */
3823 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3824 return new_Unknown(get_irn_mode(node));
3829 /* With this code we violate the agreement that local_optimize
3830 only leaves Bads in Block, Phi and Tuple nodes. */
3831 /* If Block has only Bads as predecessors it's garbage. */
3832 /* If Phi has only Bads as predecessors it's garbage. */
3833 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3834 irn_arity = get_irn_arity(node);
3835 for (i = 0; i < irn_arity; i++) {
3836 if (!is_Bad(get_irn_n(node, i))) break;
3838 if (i == irn_arity) node = new_Bad();
3845 * These optimizations deallocate nodes from the obstack.
3846 * It can only be called if it is guaranteed that no other nodes
3847 * reference this one, i.e., right after construction of a node.
3849 * current_ir_graph must be set to the graph of the node!
3851 ir_node *optimize_node(ir_node *n)
3855 opcode iro = get_irn_opcode(n);
3857 /* Always optimize Phi nodes: part of the construction. */
3858 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3860 /* constant expression evaluation / constant folding */
3861 if (get_opt_constant_folding()) {
3862 /* neither constants nor Tuple values can be evaluated */
3863 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3864 /* try to evaluate */
3865 tv = computed_value(n);
3866 if (tv != tarval_bad) {
3868 ir_type *old_tp = get_irn_type(n);
3869 int i, arity = get_irn_arity(n);
3873 * Try to recover the type of the new expression.
3875 for (i = 0; i < arity && !old_tp; ++i)
3876 old_tp = get_irn_type(get_irn_n(n, i));
3879 * we MUST copy the node here temporary, because it's still needed
3880 * for DBG_OPT_CSTEVAL
3882 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3883 oldn = alloca(node_size);
3885 memcpy(oldn, n, node_size);
3886 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3888 /* ARG, copy the in array, we need it for statistics */
3889 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3891 /* note the inplace edges module */
3892 edges_node_deleted(n, current_ir_graph);
3894 /* evaluation was successful -- replace the node. */
3895 irg_kill_node(current_ir_graph, n);
3896 nw = new_Const(get_tarval_mode (tv), tv);
3898 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3899 set_Const_type(nw, old_tp);
3900 DBG_OPT_CSTEVAL(oldn, nw);
3906 /* remove unnecessary nodes */
3907 if (get_opt_constant_folding() ||
3908 (iro == iro_Phi) || /* always optimize these nodes. */
3910 (iro == iro_Proj) ||
3911 (iro == iro_Block) ) /* Flags tested local. */
3912 n = equivalent_node(n);
3914 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3916 /* Common Subexpression Elimination.
3918 * Checks whether n is already available.
3919 * The block input is used to distinguish different subexpressions. Right
3920 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3921 * subexpressions within a block.
3924 n = identify_cons(current_ir_graph->value_table, n);
3927 edges_node_deleted(oldn, current_ir_graph);
3929 /* We found an existing, better node, so we can deallocate the old node. */
3930 irg_kill_node(current_ir_graph, oldn);
3934 /* Some more constant expression evaluation that does not allow to
3936 iro = get_irn_opcode(n);
3937 if (get_opt_constant_folding() ||
3938 (iro == iro_Cond) ||
3939 (iro == iro_Proj)) /* Flags tested local. */
3940 n = transform_node(n);
3942 /* Remove nodes with dead (Bad) input.
3943 Run always for transformation induced Bads. */
3946 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3947 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3948 n = identify_remember(current_ir_graph->value_table, n);
3952 } /* optimize_node */
3956 * These optimizations never deallocate nodes (in place). This can cause dead
3957 * nodes lying on the obstack. Remove these by a dead node elimination,
3958 * i.e., a copying garbage collection.
3960 ir_node *optimize_in_place_2(ir_node *n)
3964 opcode iro = get_irn_opcode(n);
3966 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3968 /* constant expression evaluation / constant folding */
3969 if (get_opt_constant_folding()) {
3970 /* neither constants nor Tuple values can be evaluated */
3971 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3972 /* try to evaluate */
3973 tv = computed_value(n);
3974 if (tv != tarval_bad) {
3975 /* evaluation was successful -- replace the node. */
3976 ir_type *old_tp = get_irn_type(n);
3977 int i, arity = get_irn_arity(n);
3980 * Try to recover the type of the new expression.
3982 for (i = 0; i < arity && !old_tp; ++i)
3983 old_tp = get_irn_type(get_irn_n(n, i));
3985 n = new_Const(get_tarval_mode(tv), tv);
3987 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3988 set_Const_type(n, old_tp);
3990 DBG_OPT_CSTEVAL(oldn, n);
3996 /* remove unnecessary nodes */
3997 if (get_opt_constant_folding() ||
3998 (iro == iro_Phi) || /* always optimize these nodes. */
3999 (iro == iro_Id) || /* ... */
4000 (iro == iro_Proj) || /* ... */
4001 (iro == iro_Block) ) /* Flags tested local. */
4002 n = equivalent_node(n);
4004 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4006 /** common subexpression elimination **/
4007 /* Checks whether n is already available. */
4008 /* The block input is used to distinguish different subexpressions. Right
4009 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4010 subexpressions within a block. */
4011 if (get_opt_cse()) {
4012 n = identify(current_ir_graph->value_table, n);
4015 /* Some more constant expression evaluation. */
4016 iro = get_irn_opcode(n);
4017 if (get_opt_constant_folding() ||
4018 (iro == iro_Cond) ||
4019 (iro == iro_Proj)) /* Flags tested local. */
4020 n = transform_node(n);
4022 /* Remove nodes with dead (Bad) input.
4023 Run always for transformation induced Bads. */
4026 /* Now we can verify the node, as it has no dead inputs any more. */
4029 /* Now we have a legal, useful node. Enter it in hash table for cse.
4030 Blocks should be unique anyways. (Except the successor of start:
4031 is cse with the start block!) */
4032 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4033 n = identify_remember(current_ir_graph->value_table, n);
4036 } /* optimize_in_place_2 */
4039 * Wrapper for external use, set proper status bits after optimization.
4041 ir_node *optimize_in_place(ir_node *n)
4043 /* Handle graph state */
4044 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4046 if (get_opt_global_cse())
4047 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4048 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4049 set_irg_outs_inconsistent(current_ir_graph);
4051 /* FIXME: Maybe we could also test whether optimizing the node can
4052 change the control graph. */
4053 set_irg_doms_inconsistent(current_ir_graph);
4054 return optimize_in_place_2 (n);
4055 } /* optimize_in_place */
4058 * Sets the default operation for an ir_ops.
4060 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
4062 ops = firm_set_default_computed_value(code, ops);
4063 ops = firm_set_default_equivalent_node(code, ops);
4064 ops = firm_set_default_transform_node(code, ops);
4065 ops = firm_set_default_node_cmp_attr(code, ops);
4066 ops = firm_set_default_get_type(code, ops);
4067 ops = firm_set_default_get_type_attr(code, ops);
4068 ops = firm_set_default_get_entity_attr(code, ops);
4071 } /* firm_set_default_operations */