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.
27 # include "irnode_t.h"
28 # include "irgraph_t.h"
29 # include "iredges_t.h"
30 # include "irmode_t.h"
32 # include "ircons_t.h"
36 # include "dbginfo_t.h"
37 # include "iropt_dbg.h"
38 # include "irflag_t.h"
43 # include "opt_polymorphy.h"
44 # include "opt_confirms.h"
46 /* Make types visible to allow most efficient access */
47 # include "entity_t.h"
50 * return the value of a Constant
52 static tarval *computed_value_Const(ir_node *n)
54 return get_Const_tarval(n);
58 * return the value of a 'sizeof' SymConst
60 static tarval *computed_value_SymConst(ir_node *n)
62 if ((get_SymConst_kind(n) == symconst_size) &&
63 (get_type_state(get_SymConst_type(n))) == layout_fixed)
64 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
69 * return the value of an Add
71 static tarval *computed_value_Add(ir_node *n)
73 ir_node *a = get_Add_left(n);
74 ir_node *b = get_Add_right(n);
76 tarval *ta = value_of(a);
77 tarval *tb = value_of(b);
79 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
80 return tarval_add(ta, tb);
86 * return the value of a Sub
89 static tarval *computed_value_Sub(ir_node *n)
91 ir_node *a = get_Sub_left(n);
92 ir_node *b = get_Sub_right(n);
97 if (a == b && !is_Bad(a))
98 return get_mode_null(get_irn_mode(n));
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_sub(ta, tb);
110 * return the value of an unary Minus
112 static tarval *computed_value_Minus(ir_node *n)
114 ir_node *a = get_Minus_op(n);
115 tarval *ta = value_of(a);
117 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
118 return tarval_neg(ta);
124 * return the value of a Mul
126 static tarval *computed_value_Mul(ir_node *n)
128 ir_node *a = get_Mul_left(n);
129 ir_node *b = get_Mul_right(n);
131 tarval *ta = value_of(a);
132 tarval *tb = value_of(b);
134 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
135 return tarval_mul(ta, tb);
137 /* a*0 = 0 or 0*b = 0:
138 calls computed_value recursive and returns the 0 with proper
140 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
142 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
149 * return the value of a floating point Quot
151 static tarval *computed_value_Quot(ir_node *n)
153 ir_node *a = get_Quot_left(n);
154 ir_node *b = get_Quot_right(n);
156 tarval *ta = value_of(a);
157 tarval *tb = value_of(b);
159 /* This was missing in original implementation. Why? */
160 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
161 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
162 return tarval_quo(ta, tb);
168 * calculate the value of an integer Div of two nodes
169 * Special case: 0 / b
171 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
173 tarval *ta = value_of(a);
174 tarval *tb = value_of(b);
176 /* Compute c1 / c2 or 0 / a, a != 0 */
177 if (ta != tarval_bad) {
178 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
179 return tarval_div(ta, tb);
180 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
187 * return the value of an integer Div
189 static tarval *computed_value_Div(ir_node *n)
191 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
195 * calculate the value of an integer Mod of two nodes
196 * Special case: a % 1
198 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
200 tarval *ta = value_of(a);
201 tarval *tb = value_of(b);
203 /* Compute c1 % c2 or a % 1 */
204 if (tb != tarval_bad) {
205 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
206 return tarval_mod(ta, tb);
207 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
208 return get_mode_null(get_irn_mode(a));
215 * return the value of an integer Mod
217 static tarval *computed_value_Mod(ir_node *n)
219 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
223 * return the value of an Abs
225 static tarval *computed_value_Abs(ir_node *n)
227 ir_node *a = get_Abs_op(n);
228 tarval *ta = value_of(a);
230 if (ta != tarval_bad)
231 return tarval_abs(ta);
237 * return the value of an And
238 * Special case: a & 0, 0 & b
240 static tarval *computed_value_And(ir_node *n)
242 ir_node *a = get_And_left(n);
243 ir_node *b = get_And_right(n);
245 tarval *ta = value_of(a);
246 tarval *tb = value_of(b);
248 if ((ta != tarval_bad) && (tb != tarval_bad)) {
249 return tarval_and (ta, tb);
253 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
254 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
262 * return the value of an Or
263 * Special case: a | 1...1, 1...1 | b
265 static tarval *computed_value_Or(ir_node *n)
267 ir_node *a = get_Or_left(n);
268 ir_node *b = get_Or_right(n);
270 tarval *ta = value_of(a);
271 tarval *tb = value_of(b);
273 if ((ta != tarval_bad) && (tb != tarval_bad)) {
274 return tarval_or (ta, tb);
277 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
278 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
286 * return the value of an Eor
288 static tarval *computed_value_Eor(ir_node *n)
290 ir_node *a = get_Eor_left(n);
291 ir_node *b = get_Eor_right(n);
296 return get_mode_null(get_irn_mode(n));
301 if ((ta != tarval_bad) && (tb != tarval_bad)) {
302 return tarval_eor (ta, tb);
308 * return the value of a Not
310 static tarval *computed_value_Not(ir_node *n)
312 ir_node *a = get_Not_op(n);
313 tarval *ta = value_of(a);
315 if (ta != tarval_bad)
316 return tarval_not(ta);
322 * return the value of a Shl
324 static tarval *computed_value_Shl(ir_node *n)
326 ir_node *a = get_Shl_left(n);
327 ir_node *b = get_Shl_right(n);
329 tarval *ta = value_of(a);
330 tarval *tb = value_of(b);
332 if ((ta != tarval_bad) && (tb != tarval_bad)) {
333 return tarval_shl (ta, tb);
339 * return the value of a Shr
341 static tarval *computed_value_Shr(ir_node *n)
343 ir_node *a = get_Shr_left(n);
344 ir_node *b = get_Shr_right(n);
346 tarval *ta = value_of(a);
347 tarval *tb = value_of(b);
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_shr (ta, tb);
356 * return the value of a Shrs
358 static tarval *computed_value_Shrs(ir_node *n)
360 ir_node *a = get_Shrs_left(n);
361 ir_node *b = get_Shrs_right(n);
363 tarval *ta = value_of(a);
364 tarval *tb = value_of(b);
366 if ((ta != tarval_bad) && (tb != tarval_bad)) {
367 return tarval_shrs (ta, tb);
373 * return the value of a Rot
375 static tarval *computed_value_Rot(ir_node *n)
377 ir_node *a = get_Rot_left(n);
378 ir_node *b = get_Rot_right(n);
380 tarval *ta = value_of(a);
381 tarval *tb = value_of(b);
383 if ((ta != tarval_bad) && (tb != tarval_bad)) {
384 return tarval_rot (ta, tb);
390 * return the value of a Conv
392 static tarval *computed_value_Conv(ir_node *n)
394 ir_node *a = get_Conv_op(n);
395 tarval *ta = value_of(a);
397 if (ta != tarval_bad)
398 return tarval_convert_to(ta, get_irn_mode(n));
404 * return the value of a Proj(Cmp)
406 * This performs a first step of unreachable code elimination.
407 * Proj can not be computed, but folding a Cmp above the Proj here is
408 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
410 * There are several case where we can evaluate a Cmp node, see later.
412 static tarval *computed_value_Proj_Cmp(ir_node *n)
414 ir_node *a = get_Proj_pred(n);
415 ir_node *aa = get_Cmp_left(a);
416 ir_node *ab = get_Cmp_right(a);
417 long proj_nr = get_Proj_proj(n);
420 * BEWARE: a == a is NOT always True for floating Point values, as
421 * NaN != NaN is defined, so we must check this here.
424 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
427 /* This is a trick with the bits used for encoding the Cmp
428 Proj numbers, the following statement is not the same:
429 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
430 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
433 tarval *taa = value_of(aa);
434 tarval *tab = value_of(ab);
435 ir_mode *mode = get_irn_mode(aa);
438 * The predecessors of Cmp are target values. We can evaluate
441 if ((taa != tarval_bad) && (tab != tarval_bad)) {
442 /* strange checks... */
443 pn_Cmp flags = tarval_cmp(taa, tab);
444 if (flags != pn_Cmp_False) {
445 return new_tarval_from_long (proj_nr & flags, mode_b);
448 /* for integer values, we can check against MIN/MAX */
449 else if (mode_is_int(mode)) {
450 /* MIN <=/> x. This results in true/false. */
451 if (taa == get_mode_min(mode)) {
452 /* a compare with the MIN value */
453 if (proj_nr == pn_Cmp_Le)
454 return get_tarval_b_true();
455 else if (proj_nr == pn_Cmp_Gt)
456 return get_tarval_b_false();
458 /* x >=/< MIN. This results in true/false. */
460 if (tab == get_mode_min(mode)) {
461 /* a compare with the MIN value */
462 if (proj_nr == pn_Cmp_Ge)
463 return get_tarval_b_true();
464 else if (proj_nr == pn_Cmp_Lt)
465 return get_tarval_b_false();
467 /* MAX >=/< x. This results in true/false. */
468 else if (taa == get_mode_max(mode)) {
469 if (proj_nr == pn_Cmp_Ge)
470 return get_tarval_b_true();
471 else if (proj_nr == pn_Cmp_Lt)
472 return get_tarval_b_false();
474 /* x <=/> MAX. This results in true/false. */
475 else if (tab == get_mode_max(mode)) {
476 if (proj_nr == pn_Cmp_Le)
477 return get_tarval_b_true();
478 else if (proj_nr == pn_Cmp_Gt)
479 return get_tarval_b_false();
483 * The predecessors are Allocs or (void*)(0) constants. Allocs never
484 * return NULL, they raise an exception. Therefore we can predict
488 ir_node *aaa = skip_Id(skip_Proj(aa));
489 ir_node *aba = skip_Id(skip_Proj(ab));
491 if ( ( (/* aa is ProjP and aaa is Alloc */
492 (get_irn_op(aa) == op_Proj)
493 && (mode_is_reference(get_irn_mode(aa)))
494 && (get_irn_op(aaa) == op_Alloc))
495 && ( (/* ab is NULL */
496 (get_irn_op(ab) == op_Const)
497 && (mode_is_reference(get_irn_mode(ab)))
498 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
499 || (/* ab is other Alloc */
500 (get_irn_op(ab) == op_Proj)
501 && (mode_is_reference(get_irn_mode(ab)))
502 && (get_irn_op(aba) == op_Alloc)
504 || (/* aa is NULL and aba is Alloc */
505 (get_irn_op(aa) == op_Const)
506 && (mode_is_reference(get_irn_mode(aa)))
507 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
508 && (get_irn_op(ab) == op_Proj)
509 && (mode_is_reference(get_irn_mode(ab)))
510 && (get_irn_op(aba) == op_Alloc)))
512 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
516 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
520 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
522 static tarval *computed_value_Proj(ir_node *n)
524 ir_node *a = get_Proj_pred(n);
527 switch (get_irn_opcode(a)) {
529 return computed_value_Proj_Cmp(n);
532 /* compute either the Div or the Mod part */
533 proj_nr = get_Proj_proj(n);
534 if (proj_nr == pn_DivMod_res_div)
535 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
536 else if (proj_nr == pn_DivMod_res_mod)
537 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
541 if (get_Proj_proj(n) == pn_Div_res)
542 return computed_value(a);
546 if (get_Proj_proj(n) == pn_Mod_res)
547 return computed_value(a);
557 * calculate the value of a Mux: can be evaluated, if the
558 * sel and the right input are known
560 static tarval *computed_value_Mux(ir_node *n)
562 ir_node *sel = get_Mux_sel(n);
563 tarval *ts = value_of(sel);
565 if (ts == get_tarval_b_true()) {
566 ir_node *v = get_Mux_true(n);
569 else if (ts == get_tarval_b_false()) {
570 ir_node *v = get_Mux_false(n);
577 * calculate the value of a Confirm: can be evaluated,
578 * if it has the form Confirm(x, '=', Const).
580 static tarval *computed_value_Confirm(ir_node *n)
582 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
583 value_of(get_Confirm_bound(n)) : tarval_bad;
587 * If the parameter n can be computed, return its value, else tarval_bad.
588 * Performs constant folding.
590 * @param n The node this should be evaluated
592 tarval *computed_value(ir_node *n)
594 if (n->op->computed_value)
595 return n->op->computed_value(n);
600 * set the default computed_value evaluator
602 static ir_op *firm_set_default_computed_value(ir_op *op)
606 op->computed_value = computed_value_##a; \
633 op->computed_value = NULL;
641 /* returns 1 if the a and b are pointers to different locations. */
643 different_identity (ir_node *a, ir_node *b)
645 assert (mode_is_reference(get_irn_mode (a))
646 && mode_is_reference(get_irn_mode (b)));
648 if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
649 ir_node *a1 = get_Proj_pred (a);
650 ir_node *b1 = get_Proj_pred (b);
651 if (a1 != b1 && get_irn_op (a1) == op_Alloc
652 && get_irn_op (b1) == op_Alloc)
660 * Returns a equivalent block for another block.
661 * If the block has only one predecessor, this is
662 * the equivalent one. If the only predecessor of a block is
663 * the block itself, this is a dead block.
665 * If both predecessors of a block are the branches of a binary
666 * Cond, the equivalent block is Cond's block.
668 * If all predecessors of a block are bad or lies in a dead
669 * block, the current block is dead as well.
671 * Note, that blocks are NEVER turned into Bad's, instead
672 * the dead_block flag is set. So, never test for is_Bad(block),
673 * always use is_dead_Block(block).
675 static ir_node *equivalent_node_Block(ir_node *n)
678 int n_preds = get_Block_n_cfgpreds(n);
680 /* The Block constructor does not call optimize, but mature_immBlock
681 calls the optimization. */
682 assert(get_Block_matured(n));
684 /* Straightening: a single entry Block following a single exit Block
685 can be merged, if it is not the Start block. */
686 /* !!! Beware, all Phi-nodes of n must have been optimized away.
687 This should be true, as the block is matured before optimize is called.
688 But what about Phi-cycles with the Phi0/Id that could not be resolved?
689 Remaining Phi nodes are just Ids. */
690 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
691 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
692 if (predblock == oldn) {
693 /* Jmp jumps into the block it is in -- deal self cycle. */
694 n = set_Block_dead(n);
695 DBG_OPT_DEAD_BLOCK(oldn, n);
696 } else if (get_opt_control_flow_straightening()) {
698 DBG_OPT_STG(oldn, n);
701 else if ((n_preds == 1) &&
702 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
703 ir_node *predblock = get_Block_cfgpred_block(n, 0);
704 if (predblock == oldn) {
705 /* Jmp jumps into the block it is in -- deal self cycle. */
706 n = set_Block_dead(n);
707 DBG_OPT_DEAD_BLOCK(oldn, n);
710 else if ((n_preds == 2) &&
711 (get_opt_control_flow_weak_simplification())) {
712 /* Test whether Cond jumps twice to this block
713 * The more general case which more than 2 predecessors is handles
714 * in optimize_cf(), we handle only this special case for speed here.
716 ir_node *a = get_Block_cfgpred(n, 0);
717 ir_node *b = get_Block_cfgpred(n, 1);
719 if ((get_irn_op(a) == op_Proj) &&
720 (get_irn_op(b) == op_Proj) &&
721 (get_Proj_pred(a) == get_Proj_pred(b)) &&
722 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
723 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
724 /* Also a single entry Block following a single exit Block. Phis have
725 twice the same operand and will be optimized away. */
726 n = get_nodes_block(a);
727 DBG_OPT_IFSIM1(oldn, a, b, n);
730 else if (get_opt_unreachable_code() &&
731 (n != current_ir_graph->start_block) &&
732 (n != current_ir_graph->end_block) ) {
733 int i, n_cfg = get_Block_n_cfgpreds(n);
735 /* If all inputs are dead, this block is dead too, except if it is
736 the start or end block. This is a step of unreachable code
738 for (i = 0; i < n_cfg; i++) {
739 ir_node *pred = get_Block_cfgpred(n, i);
742 if (is_Bad(pred)) continue;
743 pred_blk = get_nodes_block(skip_Proj(pred));
745 if (is_Block_dead(pred_blk)) continue;
748 /* really found a living input */
753 n = set_Block_dead(n);
760 * Returns a equivalent node for a Jmp, a Bad :-)
761 * Of course this only happens if the Block of the Jmp is Bad.
763 static ir_node *equivalent_node_Jmp(ir_node *n)
765 /* GL: Why not same for op_Raise?? */
766 /* unreachable code elimination */
767 if (is_Block_dead(get_nodes_block(n)))
773 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
774 See transform_node_Proj_Cond(). */
777 * optimize operations that are commutative and have neutral 0,
778 * so a op 0 = 0 op a = a.
780 static ir_node *equivalent_node_neutral_zero(ir_node *n)
784 ir_node *a = get_binop_left(n);
785 ir_node *b = get_binop_right(n);
790 /* After running compute_node there is only one constant predecessor.
791 Find this predecessors value and remember the other node: */
792 if ((tv = value_of(a)) != tarval_bad) {
794 } else if ((tv = value_of(b)) != tarval_bad) {
799 /* If this predecessors constant value is zero, the operation is
800 * unnecessary. Remove it.
802 * Beware: If n is a Add, the mode of on and n might be different
803 * which happens in this rare construction: NULL + 3.
804 * Then, a Conv would be needed which we cannot include here.
806 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
807 if (get_irn_mode(on) == get_irn_mode(n)) {
810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
817 #define equivalent_node_Eor equivalent_node_neutral_zero
820 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
822 * The second one looks strange, but this construct
823 * is used heavily in the LCC sources :-).
825 * Beware: The Mode of an Add may be different than the mode of its
826 * predecessors, so we could not return a predecessors in all cases.
828 static ir_node *equivalent_node_Add(ir_node *n)
831 ir_node *left, *right;
833 n = equivalent_node_neutral_zero(n);
837 left = get_Add_left(n);
838 right = get_Add_right(n);
840 if (get_irn_op(left) == op_Sub) {
841 if (get_Sub_right(left) == right) {
844 n = get_Sub_left(left);
845 if (get_irn_mode(oldn) == get_irn_mode(n)) {
846 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
851 if (get_irn_op(right) == op_Sub) {
852 if (get_Sub_right(right) == left) {
855 n = get_Sub_left(right);
856 if (get_irn_mode(oldn) == get_irn_mode(n)) {
857 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
866 * optimize operations that are not commutative but have neutral 0 on left,
869 static ir_node *equivalent_node_left_zero(ir_node *n)
873 ir_node *a = get_binop_left(n);
874 ir_node *b = get_binop_right(n);
876 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
879 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
885 #define equivalent_node_Shl equivalent_node_left_zero
886 #define equivalent_node_Shr equivalent_node_left_zero
887 #define equivalent_node_Shrs equivalent_node_left_zero
888 #define equivalent_node_Rot equivalent_node_left_zero
891 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
893 * The second one looks strange, but this construct
894 * is used heavily in the LCC sources :-).
896 * Beware: The Mode of a Sub may be different than the mode of its
897 * predecessors, so we could not return a predecessors in all cases.
899 static ir_node *equivalent_node_Sub(ir_node *n)
903 ir_node *a = get_Sub_left(n);
904 ir_node *b = get_Sub_right(n);
906 /* Beware: modes might be different */
907 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
908 if (get_irn_mode(n) == get_irn_mode(a)) {
911 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
914 else if (get_irn_op(a) == op_Add) {
915 ir_mode *mode = get_irn_mode(n);
917 if (mode_wrap_around(mode)) {
918 ir_node *left = get_Add_left(a);
919 ir_node *right = get_Add_right(a);
922 if (get_irn_mode(n) == get_irn_mode(right)) {
924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
927 else if (right == b) {
928 if (get_irn_mode(n) == get_irn_mode(left)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
941 * Optimize an "idempotent unary op", ie op(op(n)) = n.
943 * @fixme -(-a) == a, but might overflow two times.
944 * We handle it anyway here but the better way would be a
945 * flag. This would be needed for Pascal for instance.
947 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
950 ir_node *pred = get_unop_op(n);
952 /* optimize symmetric unop */
953 if (get_irn_op(pred) == get_irn_op(n)) {
954 n = get_unop_op(pred);
955 DBG_OPT_ALGSIM2(oldn, pred, n);
960 /* Not(Not(x)) == x */
961 #define equivalent_node_Not equivalent_node_idempotent_unop
963 /* --x == x */ /* ??? Is this possible or can --x raise an
964 out of bounds exception if min =! max? */
965 #define equivalent_node_Minus equivalent_node_idempotent_unop
968 * Optimize a * 1 = 1 * a = a.
970 static ir_node *equivalent_node_Mul(ir_node *n)
974 ir_node *a = get_Mul_left(n);
975 ir_node *b = get_Mul_right(n);
977 /* Mul is commutative and has again an other neutral element. */
978 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
980 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
981 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
989 * Optimize a / 1 = a.
991 static ir_node *equivalent_node_Div(ir_node *n)
993 ir_node *a = get_Div_left(n);
994 ir_node *b = get_Div_right(n);
996 /* Div is not commutative. */
997 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
998 /* Turn Div into a tuple (mem, bad, a) */
999 ir_node *mem = get_Div_mem(n);
1000 turn_into_tuple(n, 3);
1001 set_Tuple_pred(n, pn_Div_M, mem);
1002 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1003 set_Tuple_pred(n, pn_Div_res, a);
1009 * Optimize a / 1 = a.
1011 static ir_node *equivalent_node_DivMod(ir_node *n)
1013 ir_node *a = get_DivMod_left(n);
1014 ir_node *b = get_DivMod_right(n);
1016 /* Div is not commutative. */
1017 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1018 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1019 ir_node *mem = get_Div_mem(n);
1020 ir_mode *mode = get_irn_mode(b);
1022 turn_into_tuple(n, 4);
1023 set_Tuple_pred(n, pn_DivMod_M, mem);
1024 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1025 set_Tuple_pred(n, pn_DivMod_res_div, a);
1026 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1032 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1034 static ir_node *equivalent_node_Or(ir_node *n)
1038 ir_node *a = get_Or_left(n);
1039 ir_node *b = get_Or_right(n);
1042 n = a; /* Or has it's own neutral element */
1043 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1044 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1046 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1047 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1049 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1056 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1058 static ir_node *equivalent_node_And(ir_node *n)
1062 ir_node *a = get_And_left(n);
1063 ir_node *b = get_And_right(n);
1066 n = a; /* And has it's own neutral element */
1067 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1068 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1070 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1071 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1073 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1079 * Try to remove useless Conv's:
1081 static ir_node *equivalent_node_Conv(ir_node *n)
1084 ir_node *a = get_Conv_op(n);
1087 ir_mode *n_mode = get_irn_mode(n);
1088 ir_mode *a_mode = get_irn_mode(a);
1090 if (n_mode == a_mode) { /* No Conv necessary */
1092 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1093 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1097 n_mode = get_irn_mode(n);
1098 b_mode = get_irn_mode(b);
1100 if (n_mode == b_mode) {
1101 if (n_mode == mode_b) {
1102 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1103 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1105 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1106 if (smaller_mode(b_mode, a_mode)){
1107 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1117 * A Cast may be removed if the type of the previous node
1118 * is already the type of the Cast.
1120 static ir_node *equivalent_node_Cast(ir_node *n) {
1122 ir_node *pred = get_Cast_op(n);
1124 if (get_irn_type(pred) == get_Cast_type(n)) {
1126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1131 /* Several optimizations:
1132 - no Phi in start block.
1133 - remove Id operators that are inputs to Phi
1134 - fold Phi-nodes, iff they have only one predecessor except
1137 static ir_node *equivalent_node_Phi(ir_node *n)
1142 ir_node *block = NULL; /* to shutup gcc */
1143 ir_node *first_val = NULL; /* to shutup gcc */
1144 ir_node *scnd_val = NULL; /* to shutup gcc */
1146 if (!get_opt_normalize()) return n;
1148 n_preds = get_Phi_n_preds(n);
1150 block = get_nodes_block(n);
1151 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1152 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1153 if ((is_Block_dead(block)) || /* Control dead */
1154 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1155 return new_Bad(); /* in the Start Block. */
1157 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1159 /* If the Block has a Bad pred, we also have one. */
1160 for (i = 0; i < n_preds; ++i)
1161 if (is_Bad(get_Block_cfgpred(block, i)))
1162 set_Phi_pred(n, i, new_Bad());
1164 /* Find first non-self-referencing input */
1165 for (i = 0; i < n_preds; ++i) {
1166 first_val = get_Phi_pred(n, i);
1167 if ( (first_val != n) /* not self pointer */
1169 && (! is_Bad(first_val))
1171 ) { /* value not dead */
1172 break; /* then found first value. */
1177 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1183 /* follow_Id () for rest of inputs, determine if any of these
1184 are non-self-referencing */
1185 while (++i < n_preds) {
1186 scnd_val = get_Phi_pred(n, i);
1187 if ( (scnd_val != n)
1188 && (scnd_val != first_val)
1190 && (! is_Bad(scnd_val))
1198 /* Fold, if no multiple distinct non-self-referencing inputs */
1200 DBG_OPT_PHI(oldn, n);
1202 /* skip the remaining Ids (done in get_Phi_pred). */
1203 /* superfluous, since we walk all to propagate Block's Bads.
1204 while (++i < n_preds) get_Phi_pred(n, i); */
1210 * optimize Proj(Tuple) and gigo() for ProjX in Bad block
1212 static ir_node *equivalent_node_Proj(ir_node *n)
1216 ir_node *a = get_Proj_pred(n);
1218 if ( get_irn_op(a) == op_Tuple) {
1219 /* Remove the Tuple/Proj combination. */
1220 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1221 n = get_Tuple_pred(a, get_Proj_proj(n));
1222 DBG_OPT_TUPLE(oldn, a, n);
1224 assert(0); /* This should not happen! */
1227 } else if (get_irn_mode(n) == mode_X) {
1228 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1229 /* Remove dead control flow -- early gigo(). */
1239 static ir_node *equivalent_node_Id(ir_node *n)
1245 } while (get_irn_op(n) == op_Id);
1247 DBG_OPT_ID(oldn, n);
1254 static ir_node *equivalent_node_Mux(ir_node *n)
1256 ir_node *oldn = n, *sel = get_Mux_sel(n);
1257 tarval *ts = value_of(sel);
1259 /* Mux(true, f, t) == t */
1260 if (ts == tarval_b_true) {
1261 n = get_Mux_true(n);
1262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1264 /* Mux(false, f, t) == f */
1265 else if (ts == tarval_b_false) {
1266 n = get_Mux_false(n);
1267 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1269 /* Mux(v, x, x) == x */
1270 else if (get_Mux_false(n) == get_Mux_true(n)) {
1271 n = get_Mux_true(n);
1272 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1274 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1275 ir_node *cmp = get_Proj_pred(sel);
1276 long proj_nr = get_Proj_proj(sel);
1277 ir_node *b = get_Mux_false(n);
1278 ir_node *a = get_Mux_true(n);
1281 * Note: normalization puts the constant on the right site,
1282 * so we check only one case.
1284 * Note further that these optimization work even for floating point
1285 * with NaN's because -NaN == NaN.
1286 * However, if +0 and -0 is handled differently, we cannot use the first one.
1288 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1289 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1290 /* Mux(a CMP 0, X, a) */
1291 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1292 /* Mux(a CMP 0, -a, a) */
1293 if (proj_nr == pn_Cmp_Eq) {
1294 /* Mux(a == 0, -a, a) ==> -a */
1296 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1298 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1299 /* Mux(a != 0, -a, a) ==> a */
1301 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1304 else if (classify_Const(b) == CNST_NULL) {
1305 /* Mux(a CMP 0, 0, a) */
1306 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1307 /* Mux(a != 0, 0, a) ==> a */
1309 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1311 else if (proj_nr == pn_Cmp_Eq) {
1312 /* Mux(a == 0, 0, a) ==> 0 */
1314 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1324 * Optimize -a CMP -b into b CMP a.
1325 * This works only for for modes where unary Minus
1327 * Note that two-complement integers can Overflow
1328 * so it will NOT work.
1330 static ir_node *equivalent_node_Cmp(ir_node *n)
1332 ir_node *left = get_Cmp_left(n);
1333 ir_node *right = get_Cmp_right(n);
1335 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1336 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1337 left = get_Minus_op(left);
1338 right = get_Minus_op(right);
1339 set_Cmp_left(n, right);
1340 set_Cmp_right(n, left);
1346 * Remove Confirm nodes if setting is on.
1348 static ir_node *equivalent_node_Confirm(ir_node *n)
1350 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
1351 ir_node *bound = get_Confirm_bound(n);
1352 ir_op *op = get_irn_op(bound);
1355 * Optimize a rare case:
1356 * Confirm(x, '=', Const) ==> Const
1358 if (op == op_Const || op == op_SymConst)
1361 return get_opt_remove_Confirm() ? get_Confirm_value(n) : n;
1365 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1366 * perform no actual computation, as, e.g., the Id nodes. It does not create
1367 * new nodes. It is therefore safe to free n if the node returned is not n.
1368 * If a node returns a Tuple we can not just skip it. If the size of the
1369 * in array fits, we transform n into a tuple (e.g., Div).
1372 equivalent_node(ir_node *n)
1374 if (n->op->equivalent_node)
1375 return n->op->equivalent_node(n);
1380 * set the default equivalent node operation
1382 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1386 op->equivalent_node = equivalent_node_##a; \
1415 op->equivalent_node = NULL;
1423 * Do node specific optimizations of nodes predecessors.
1426 optimize_preds(ir_node *n) {
1427 ir_node *a = NULL, *b = NULL;
1429 /* get the operands we will work on for simple cases. */
1431 a = get_binop_left(n);
1432 b = get_binop_right(n);
1433 } else if (is_unop(n)) {
1437 switch (get_irn_opcode(n)) {
1440 /* We don't want Cast as input to Cmp. */
1441 if (get_irn_op(a) == op_Cast) {
1445 if (get_irn_op(b) == op_Cast) {
1447 set_Cmp_right(n, b);
1456 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1457 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1458 * If possible, remove the Conv's.
1460 static ir_node *transform_node_AddSub(ir_node *n)
1462 ir_mode *mode = get_irn_mode(n);
1464 if (mode_is_reference(mode)) {
1465 ir_node *left = get_binop_left(n);
1466 ir_node *right = get_binop_right(n);
1467 int ref_bits = get_mode_size_bits(mode);
1469 if (get_irn_op(left) == op_Conv) {
1470 ir_mode *mode = get_irn_mode(left);
1471 int bits = get_mode_size_bits(mode);
1473 if (ref_bits == bits &&
1474 mode_is_int(mode) &&
1475 get_mode_arithmetic(mode) == irma_twos_complement) {
1476 ir_node *pre = get_Conv_op(left);
1477 ir_mode *pre_mode = get_irn_mode(pre);
1479 if (mode_is_int(pre_mode) &&
1480 get_mode_size_bits(pre_mode) == bits &&
1481 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1482 /* ok, this conv just changes to sign, moreover the calculation
1483 * is done with same number of bits as our address mode, so
1484 * we can ignore the conv as address calculation can be viewed
1485 * as either signed or unsigned
1487 set_binop_left(n, pre);
1492 if (get_irn_op(right) == op_Conv) {
1493 ir_mode *mode = get_irn_mode(right);
1494 int bits = get_mode_size_bits(mode);
1496 if (ref_bits == bits &&
1497 mode_is_int(mode) &&
1498 get_mode_arithmetic(mode) == irma_twos_complement) {
1499 ir_node *pre = get_Conv_op(right);
1500 ir_mode *pre_mode = get_irn_mode(pre);
1502 if (mode_is_int(pre_mode) &&
1503 get_mode_size_bits(pre_mode) == bits &&
1504 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1505 /* ok, this conv just changes to sign, moreover the calculation
1506 * is done with same number of bits as our address mode, so
1507 * we can ignore the conv as address calculation can be viewed
1508 * as either signed or unsigned
1510 set_binop_right(n, pre);
1519 * Do the AddSub optimization, then Transform Add(a,a) into Mul(a, 2)
1520 * if the mode is integer or float.
1521 * Transform Add(a,-b) into Sub(a,b).
1522 * Reassociation might fold this further.
1524 static ir_node *transform_node_Add(ir_node *n)
1529 n = transform_node_AddSub(n);
1531 mode = get_irn_mode(n);
1532 if (mode_is_num(mode)) {
1533 ir_node *a = get_Add_left(n);
1535 if (a == get_Add_right(n)) {
1536 ir_node *block = get_irn_n(n, -1);
1539 get_irn_dbg_info(n),
1543 new_r_Const_long(current_ir_graph, block, mode, 2),
1545 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1548 ir_node *b = get_Add_right(n);
1550 if (get_irn_op(a) == op_Minus) {
1552 get_irn_dbg_info(n),
1558 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1560 else if (get_irn_op(b) == op_Minus) {
1562 get_irn_dbg_info(n),
1568 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1576 * Do the AddSub optimization, then Transform Sub(0,a) into Minus(a).
1578 static ir_node *transform_node_Sub(ir_node *n)
1583 n = transform_node_AddSub(n);
1585 mode = get_irn_mode(n);
1586 if (mode_is_num(mode) && (classify_Const(get_Sub_left(n)) == CNST_NULL)) {
1588 get_irn_dbg_info(n),
1593 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1600 * Transform Mul(a,-1) into -a.
1601 * Do architecture dependent optimizations on Mul nodes
1603 static ir_node *transform_node_Mul(ir_node *n) {
1605 ir_mode *mode = get_irn_mode(n);
1607 if (mode_is_signed(mode)) {
1609 ir_node *a = get_Mul_left(n);
1610 ir_node *b = get_Mul_right(n);
1612 if (value_of(a) == get_mode_minus_one(mode))
1614 else if (value_of(b) == get_mode_minus_one(mode))
1617 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1618 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1622 return arch_dep_replace_mul_with_shifts(n);
1626 * transform a Div Node
1628 static ir_node *transform_node_Div(ir_node *n)
1630 tarval *tv = value_of(n);
1633 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1635 if (tv != tarval_bad) {
1636 value = new_Const(get_tarval_mode(tv), tv);
1638 DBG_OPT_CSTEVAL(n, value);
1640 else /* Try architecture dependent optimization */
1641 value = arch_dep_replace_div_by_const(n);
1644 /* Turn Div into a tuple (mem, bad, value) */
1645 ir_node *mem = get_Div_mem(n);
1647 turn_into_tuple(n, 3);
1648 set_Tuple_pred(n, pn_Div_M, mem);
1649 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1650 set_Tuple_pred(n, pn_Div_res, value);
1656 * transform a Mod node
1658 static ir_node *transform_node_Mod(ir_node *n)
1660 tarval *tv = value_of(n);
1663 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1665 if (tv != tarval_bad) {
1666 value = new_Const(get_tarval_mode(tv), tv);
1668 DBG_OPT_CSTEVAL(n, value);
1670 else /* Try architecture dependent optimization */
1671 value = arch_dep_replace_mod_by_const(n);
1674 /* Turn Mod into a tuple (mem, bad, value) */
1675 ir_node *mem = get_Mod_mem(n);
1677 turn_into_tuple(n, 3);
1678 set_Tuple_pred(n, pn_Mod_M, mem);
1679 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1680 set_Tuple_pred(n, pn_Mod_res, value);
1686 * transform a DivMod node
1688 static ir_node *transform_node_DivMod(ir_node *n)
1692 ir_node *a = get_DivMod_left(n);
1693 ir_node *b = get_DivMod_right(n);
1694 ir_mode *mode = get_irn_mode(a);
1695 tarval *ta = value_of(a);
1696 tarval *tb = value_of(b);
1698 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1701 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1703 if (tb != tarval_bad) {
1704 if (tb == get_mode_one(get_tarval_mode(tb))) {
1705 b = new_Const (mode, get_mode_null(mode));
1708 DBG_OPT_CSTEVAL(n, b);
1710 else if (ta != tarval_bad) {
1711 tarval *resa, *resb;
1712 resa = tarval_div (ta, tb);
1713 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1714 Jmp for X result!? */
1715 resb = tarval_mod (ta, tb);
1716 if (resb == tarval_bad) return n; /* Causes exception! */
1717 a = new_Const (mode, resa);
1718 b = new_Const (mode, resb);
1721 DBG_OPT_CSTEVAL(n, a);
1722 DBG_OPT_CSTEVAL(n, b);
1724 else { /* Try architecture dependent optimization */
1725 arch_dep_replace_divmod_by_const(&a, &b, n);
1726 evaluated = a != NULL;
1728 } else if (ta == get_mode_null(mode)) {
1729 /* 0 / non-Const = 0 */
1734 if (evaluated) { /* replace by tuple */
1735 ir_node *mem = get_DivMod_mem(n);
1736 turn_into_tuple(n, 4);
1737 set_Tuple_pred(n, pn_DivMod_M, mem);
1738 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1739 set_Tuple_pred(n, pn_DivMod_res_div, a);
1740 set_Tuple_pred(n, pn_DivMod_res_mod, b);
1747 * Optimize Abs(x) into x if x is Confirmed >= 0
1748 * Optimize Abs(x) into -x if x is Confirmed <= 0
1750 static ir_node *transform_node_Abs(ir_node *n)
1753 ir_node *a = get_Abs_op(n);
1754 value_classify sign = classify_value_sign(a);
1756 if (sign == VALUE_NEGATIVE) {
1757 ir_mode *mode = get_irn_mode(n);
1760 * We can replace the Abs by -x here.
1761 * We even could add a new Confirm here.
1763 * Note that -x would create a new node, so we could
1764 * not run it in the equivalent_node() context.
1766 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1767 get_irn_n(n, -1), a, mode);
1769 DBG_OPT_CONFIRM(oldn, n);
1771 else if (sign == VALUE_POSITIVE) {
1772 /* n is positive, Abs is not needed */
1775 DBG_OPT_CONFIRM(oldn, n);
1782 * transform a Cond node
1784 static ir_node *transform_node_Cond(ir_node *n)
1786 /* Replace the Cond by a Jmp if it branches on a constant
1789 ir_node *a = get_Cond_selector(n);
1790 tarval *ta = value_of(a);
1792 /* we need block info which is not available in floating irgs */
1793 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
1796 if ((ta != tarval_bad) &&
1797 (get_irn_mode(a) == mode_b) &&
1798 (get_opt_unreachable_code())) {
1799 /* It's a boolean Cond, branching on a boolean constant.
1800 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1801 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1802 turn_into_tuple(n, 2);
1803 if (ta == tarval_b_true) {
1804 set_Tuple_pred(n, pn_Cond_false, new_Bad());
1805 set_Tuple_pred(n, pn_Cond_true, jmp);
1807 set_Tuple_pred(n, pn_Cond_false, jmp);
1808 set_Tuple_pred(n, pn_Cond_true, new_Bad());
1810 /* We might generate an endless loop, so keep it alive. */
1811 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1819 static ir_node *transform_node_Eor(ir_node *n)
1822 ir_node *a = get_Eor_left(n);
1823 ir_node *b = get_Eor_right(n);
1824 ir_mode *mode = get_irn_mode(n);
1828 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
1829 mode, get_mode_null(mode));
1830 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
1832 else if ((mode == mode_b)
1833 && (get_irn_op(a) == op_Proj)
1834 && (get_irn_mode(a) == mode_b)
1835 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1836 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1837 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1838 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
1839 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
1841 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
1843 else if ((mode == mode_b)
1844 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
1845 /* The Eor is a Not. Replace it by a Not. */
1846 /* ????!!!Extend to bitfield 1111111. */
1847 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
1849 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
1856 * Transform a boolean Not.
1858 static ir_node *transform_node_Not(ir_node *n)
1861 ir_node *a = get_Not_op(n);
1863 if ( (get_irn_mode(n) == mode_b)
1864 && (get_irn_op(a) == op_Proj)
1865 && (get_irn_mode(a) == mode_b)
1866 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1867 /* We negate a Cmp. The Cmp has the negated result anyways! */
1868 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
1869 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
1870 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
1877 * Transform a Cast_type(Const) into a new Const_type
1879 static ir_node *transform_node_Cast(ir_node *n) {
1881 ir_node *pred = get_Cast_op(n);
1882 type *tp = get_irn_type(n);
1884 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1885 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
1886 get_Const_tarval(pred), tp);
1887 DBG_OPT_CSTEVAL(oldn, n);
1888 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1889 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
1890 get_SymConst_kind(pred), tp);
1891 DBG_OPT_CSTEVAL(oldn, n);
1898 * Transform a Proj(Div) with a non-zero value.
1899 * Removes the exceptions and routes the memory to the NoMem node.
1901 static ir_node *transform_node_Proj_Div(ir_node *proj)
1903 ir_node *n = get_Proj_pred(proj);
1904 ir_node *b = get_Div_right(n);
1907 if (value_not_zero(b)) {
1908 /* div(x, y) && y != 0 */
1909 proj_nr = get_Proj_proj(proj);
1911 /* this node may float */
1912 set_irn_pinned(n, op_pin_state_floats);
1914 if (proj_nr == pn_Div_X_except) {
1915 /* we found an exception handler, remove it */
1917 } else if (proj_nr == pn_Div_M) {
1918 /* the memory Proj can be removed */
1919 ir_node *res = get_Div_mem(n);
1920 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1929 * Transform a Proj(Mod) with a non-zero value.
1930 * Removes the exceptions and routes the memory to the NoMem node.
1932 static ir_node *transform_node_Proj_Mod(ir_node *proj)
1934 ir_node *n = get_Proj_pred(proj);
1935 ir_node *b = get_Mod_right(n);
1938 if (value_not_zero(b)) {
1939 /* mod(x, y) && y != 0 */
1940 proj_nr = get_Proj_proj(proj);
1942 /* this node may float */
1943 set_irn_pinned(n, op_pin_state_floats);
1945 if (proj_nr == pn_Mod_X_except) {
1946 /* we found an exception handler, remove it */
1948 } else if (proj_nr == pn_Mod_M) {
1949 /* the memory Proj can be removed */
1950 ir_node *res = get_Mod_mem(n);
1951 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1955 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
1956 /* a % a = 0 if a != 0 */
1957 ir_mode *mode = get_irn_mode(proj);
1958 ir_node *res = new_Const(mode, get_mode_null(mode));
1960 DBG_OPT_CSTEVAL(n, res);
1968 * Transform a Proj(DivMod) with a non-zero value.
1969 * Removes the exceptions and routes the memory to the NoMem node.
1971 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
1973 ir_node *n = get_Proj_pred(proj);
1974 ir_node *b = get_DivMod_right(n);
1977 if (value_not_zero(b)) {
1978 /* DivMod(x, y) && y != 0 */
1979 proj_nr = get_Proj_proj(proj);
1981 /* this node may float */
1982 set_irn_pinned(n, op_pin_state_floats);
1984 if (proj_nr == pn_DivMod_X_except) {
1985 /* we found an exception handler, remove it */
1988 else if (proj_nr == pn_DivMod_M) {
1989 /* the memory Proj can be removed */
1990 ir_node *res = get_DivMod_mem(n);
1991 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1995 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
1996 /* a % a = 0 if a != 0 */
1997 ir_mode *mode = get_irn_mode(proj);
1998 ir_node *res = new_Const(mode, get_mode_null(mode));
2000 DBG_OPT_CSTEVAL(n, res);
2008 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2010 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2012 if (get_opt_unreachable_code()) {
2013 ir_node *n = get_Proj_pred(proj);
2014 ir_node *b = get_Cond_selector(n);
2015 tarval *tb = value_of(b);
2017 if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
2018 /* we have a constant switch */
2019 long num = get_Proj_proj(proj);
2021 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2022 if (get_tarval_long(tb) == num) {
2023 /* Do NOT create a jump here, or we will have 2 control flow ops
2024 * in a block. This case is optimized away in optimize_cf(). */
2028 /* this case will NEVER be taken, kill it */
2038 * Normalizes and optimizes Cmp nodes.
2040 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2042 if (get_opt_reassociation()) {
2043 ir_node *n = get_Proj_pred(proj);
2044 ir_node *left = get_Cmp_left(n);
2045 ir_node *right = get_Cmp_right(n);
2049 ir_mode *mode = NULL;
2050 long proj_nr = get_Proj_proj(proj);
2053 * First step: normalize the compare op
2054 * by placing the constant on the right site
2055 * or moving the lower address node to the left.
2056 * We ignore the case that both are constants
2057 * this case should be optimized away.
2059 if (get_irn_op(right) == op_Const)
2061 else if (get_irn_op(left) == op_Const) {
2066 proj_nr = get_inversed_pnc(proj_nr);
2069 else if (left > right) {
2075 proj_nr = get_inversed_pnc(proj_nr);
2080 * Second step: Try to reduce the magnitude
2081 * of a constant. This may help to generate better code
2082 * later and may help to normalize more compares.
2083 * Of course this is only possible for integer values.
2086 mode = get_irn_mode(c);
2087 tv = get_Const_tarval(c);
2089 if (tv != tarval_bad) {
2090 /* the following optimization is possible on modes without Overflow
2091 * on Unary Minus or on == and !=:
2092 * -a CMP c ==> a swap(CMP) -c
2094 * Beware: for two-complement Overflow may occur, so only == and != can
2095 * be optimized, see this:
2096 * -MININT < 0 =/=> MININT > 0 !!!
2098 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2099 (!mode_overflow_on_unary_Minus(mode) ||
2100 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2101 left = get_Minus_op(left);
2102 tv = tarval_sub(get_mode_null(mode), tv);
2104 proj_nr = get_inversed_pnc(proj_nr);
2108 /* for integer modes, we have more */
2109 if (mode_is_int(mode)) {
2110 /* Ne includes Unordered which is not possible on integers.
2111 * However, frontends often use this wrong, so fix it here */
2112 if (proj_nr & pn_Cmp_Uo) {
2113 proj_nr &= ~pn_Cmp_Uo;
2114 set_Proj_proj(proj, proj_nr);
2117 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2118 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2119 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2120 tv = tarval_sub(tv, get_mode_one(mode));
2122 proj_nr ^= pn_Cmp_Eq;
2125 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2126 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2127 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2128 tv = tarval_add(tv, get_mode_one(mode));
2130 proj_nr ^= pn_Cmp_Eq;
2134 /* the following reassociations work only for == and != */
2135 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2137 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2138 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2139 right = get_Sub_right(left);
2140 left = get_Sub_left(left);
2142 tv = value_of(right);
2146 if (tv != tarval_bad) {
2147 ir_op *op = get_irn_op(left);
2149 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2151 ir_node *c1 = get_Sub_right(left);
2152 tarval *tv2 = value_of(c1);
2154 if (tv2 != tarval_bad) {
2155 tv2 = tarval_add(tv, value_of(c1));
2157 if (tv2 != tarval_bad) {
2158 left = get_Sub_left(left);
2164 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2165 else if (op == op_Add) {
2166 ir_node *a_l = get_Add_left(left);
2167 ir_node *a_r = get_Add_right(left);
2171 if (get_irn_op(a_l) == op_Const) {
2173 tv2 = value_of(a_l);
2177 tv2 = value_of(a_r);
2180 if (tv2 != tarval_bad) {
2181 tv2 = tarval_sub(tv, tv2);
2183 if (tv2 != tarval_bad) {
2190 /* -a == c ==> a == -c, -a != c ==> a != -c */
2191 else if (op == op_Minus) {
2192 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2194 if (tv2 != tarval_bad) {
2195 left = get_Minus_op(left);
2202 /* the following reassociations work only for <= */
2203 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2204 if (tv != tarval_bad) {
2205 ir_op *op = get_irn_op(left);
2207 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2217 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2219 if (changed & 2) /* need a new Const */
2220 right = new_Const(mode, tv);
2222 /* create a new compare */
2223 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2226 set_Proj_pred(proj, n);
2227 set_Proj_proj(proj, proj_nr);
2234 * Does all optimizations on nodes that must be done on it's Proj's
2235 * because of creating new nodes.
2237 static ir_node *transform_node_Proj(ir_node *proj)
2239 ir_node *n = get_Proj_pred(proj);
2241 switch (get_irn_opcode(n)) {
2243 return transform_node_Proj_Div(proj);
2246 return transform_node_Proj_Mod(proj);
2249 return transform_node_Proj_DivMod(proj);
2252 return transform_node_Proj_Cond(proj);
2255 return transform_node_Proj_Cmp(proj);
2258 /* should not happen, but if it does will be optimized away */
2259 return equivalent_node_Proj(proj);
2268 * returns the operands of a commutative bin-op, if one operand is
2269 * a const, it is returned as the second one.
2271 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2273 ir_node *op_a = get_binop_left(binop);
2274 ir_node *op_b = get_binop_right(binop);
2276 assert(is_op_commutative(get_irn_op(binop)));
2278 if (get_irn_op(op_a) == op_Const) {
2289 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2290 * Such pattern may arise in bitfield stores.
2292 * value c4 value c4 & c2
2293 * AND c3 AND c1 | c3
2298 static ir_node *transform_node_Or_bf_store(ir_node *or)
2302 ir_node *and_l, *c3;
2303 ir_node *value, *c4;
2304 ir_node *new_and, *new_const, *block;
2305 ir_mode *mode = get_irn_mode(or);
2307 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2309 get_comm_Binop_Ops(or, &and, &c1);
2310 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2313 get_comm_Binop_Ops(and, &or_l, &c2);
2314 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2317 get_comm_Binop_Ops(or_l, &and_l, &c3);
2318 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2321 get_comm_Binop_Ops(and_l, &value, &c4);
2322 if (get_irn_op(c4) != op_Const)
2325 /* ok, found the pattern, check for conditions */
2326 assert(mode == get_irn_mode(and));
2327 assert(mode == get_irn_mode(or_l));
2328 assert(mode == get_irn_mode(and_l));
2330 tv1 = get_Const_tarval(c1);
2331 tv2 = get_Const_tarval(c2);
2332 tv3 = get_Const_tarval(c3);
2333 tv4 = get_Const_tarval(c4);
2335 tv = tarval_or(tv4, tv2);
2336 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2337 /* have at least one 0 at the same bit position */
2341 n_tv4 = tarval_not(tv4);
2342 if (tv3 != tarval_and(tv3, n_tv4)) {
2343 /* bit in the or_mask is outside the and_mask */
2347 n_tv2 = tarval_not(tv2);
2348 if (tv1 != tarval_and(tv1, n_tv2)) {
2349 /* bit in the or_mask is outside the and_mask */
2353 /* ok, all conditions met */
2354 block = get_irn_n(or, -1);
2356 new_and = new_r_And(current_ir_graph, block,
2357 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2359 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2361 set_Or_left(or, new_and);
2362 set_Or_right(or, new_const);
2364 /* check for more */
2365 return transform_node_Or_bf_store(or);
2369 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2371 static ir_node *transform_node_Or_Rot(ir_node *or)
2373 ir_mode *mode = get_irn_mode(or);
2374 ir_node *shl, *shr, *block;
2375 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2378 if (! mode_is_int(mode))
2381 shl = get_binop_left(or);
2382 shr = get_binop_right(or);
2384 if (get_irn_op(shl) == op_Shr) {
2385 if (get_irn_op(shr) != op_Shl)
2392 else if (get_irn_op(shl) != op_Shl)
2394 else if (get_irn_op(shr) != op_Shr)
2397 x = get_Shl_left(shl);
2398 if (x != get_Shr_left(shr))
2401 c1 = get_Shl_right(shl);
2402 c2 = get_Shr_right(shr);
2403 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2404 tv1 = get_Const_tarval(c1);
2405 if (! tarval_is_long(tv1))
2408 tv2 = get_Const_tarval(c2);
2409 if (! tarval_is_long(tv2))
2412 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2413 != get_mode_size_bits(mode))
2416 /* yet, condition met */
2417 block = get_irn_n(or, -1);
2419 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2421 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2424 else if (get_irn_op(c1) == op_Sub) {
2428 if (get_Sub_right(sub) != v)
2431 c1 = get_Sub_left(sub);
2432 if (get_irn_op(c1) != op_Const)
2435 tv1 = get_Const_tarval(c1);
2436 if (! tarval_is_long(tv1))
2439 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2442 /* yet, condition met */
2443 block = get_nodes_block(or);
2445 /* a Rot right is not supported, so use a rot left */
2446 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2448 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2451 else if (get_irn_op(c2) == op_Sub) {
2455 c1 = get_Sub_left(sub);
2456 if (get_irn_op(c1) != op_Const)
2459 tv1 = get_Const_tarval(c1);
2460 if (! tarval_is_long(tv1))
2463 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2466 /* yet, condition met */
2467 block = get_irn_n(or, -1);
2470 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2472 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2482 static ir_node *transform_node_Or(ir_node *or)
2484 or = transform_node_Or_bf_store(or);
2485 or = transform_node_Or_Rot(or);
2491 static ir_node *transform_node(ir_node *n);
2494 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2496 * Should be moved to reassociation?
2498 static ir_node *transform_node_shift(ir_node *n)
2500 ir_node *left, *right;
2501 tarval *tv1, *tv2, *res;
2503 int modulo_shf, flag;
2505 left = get_binop_left(n);
2507 /* different operations */
2508 if (get_irn_op(left) != get_irn_op(n))
2511 right = get_binop_right(n);
2512 tv1 = value_of(right);
2513 if (tv1 == tarval_bad)
2516 tv2 = value_of(get_binop_right(left));
2517 if (tv2 == tarval_bad)
2520 res = tarval_add(tv1, tv2);
2522 /* beware: a simple replacement works only, if res < modulo shift */
2523 mode = get_irn_mode(n);
2527 modulo_shf = get_mode_modulo_shift(mode);
2528 if (modulo_shf > 0) {
2529 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2531 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2538 /* ok, we can replace it */
2539 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2541 in[0] = get_binop_left(left);
2542 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2544 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2546 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2548 return transform_node(irn);
2553 #define transform_node_Shr transform_node_shift
2554 #define transform_node_Shrs transform_node_shift
2555 #define transform_node_Shl transform_node_shift
2558 * Remove dead blocks and nodes in dead blocks
2559 * in keep alive list. We do not generate a new End node.
2561 static ir_node *transform_node_End(ir_node *n) {
2562 int i, n_keepalives = get_End_n_keepalives(n);
2564 for (i = 0; i < n_keepalives; ++i) {
2565 ir_node *ka = get_End_keepalive(n, i);
2567 if (is_Block_dead(ka)) {
2568 set_End_keepalive(n, i, new_Bad());
2571 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2572 set_End_keepalive(n, i, new_Bad());
2578 * Optimize a Mux into some simpler cases.
2580 static ir_node *transform_node_Mux(ir_node *n)
2582 ir_node *oldn = n, *sel = get_Mux_sel(n);
2583 ir_mode *mode = get_irn_mode(n);
2585 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2586 ir_node *cmp = get_Proj_pred(sel);
2587 long proj_nr = get_Proj_proj(sel);
2588 ir_node *f = get_Mux_false(n);
2589 ir_node *t = get_Mux_true(n);
2591 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2592 ir_node *block = get_irn_n(n, -1);
2595 * Note: normalization puts the constant on the right site,
2596 * so we check only one case.
2598 * Note further that these optimization work even for floating point
2599 * with NaN's because -NaN == NaN.
2600 * However, if +0 and -0 is handled differently, we cannot use the first one.
2602 if (get_irn_op(f) == op_Minus &&
2603 get_Minus_op(f) == t &&
2604 get_Cmp_left(cmp) == t) {
2606 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2607 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2608 n = new_rd_Abs(get_irn_dbg_info(n),
2612 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2615 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2616 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2617 n = new_rd_Abs(get_irn_dbg_info(n),
2621 n = new_rd_Minus(get_irn_dbg_info(n),
2626 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2630 else if (get_irn_op(t) == op_Minus &&
2631 get_Minus_op(t) == f &&
2632 get_Cmp_left(cmp) == f) {
2634 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2635 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2636 n = new_rd_Abs(get_irn_dbg_info(n),
2640 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2643 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2644 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
2645 n = new_rd_Abs(get_irn_dbg_info(n),
2649 n = new_rd_Minus(get_irn_dbg_info(n),
2654 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2659 if (mode_is_int(mode) && mode_is_signed(mode) &&
2660 get_mode_arithmetic(mode) == irma_twos_complement) {
2661 ir_node *x = get_Cmp_left(cmp);
2663 /* the following optimization works only with signed integer two-complement mode */
2665 if (mode == get_irn_mode(x)) {
2667 * FIXME: this restriction is two rigid, as it would still
2668 * work if mode(x) = Hs and mode == Is, but at least it removes
2671 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2672 classify_Const(t) == CNST_ALL_ONE &&
2673 classify_Const(f) == CNST_NULL) {
2675 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2679 n = new_rd_Shrs(get_irn_dbg_info(n),
2680 current_ir_graph, block, x,
2681 new_r_Const_long(current_ir_graph, block, mode_Iu,
2682 get_mode_size_bits(mode) - 1),
2684 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2687 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2688 classify_Const(t) == CNST_ONE &&
2689 classify_Const(f) == CNST_NULL) {
2691 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2695 n = new_rd_Shr(get_irn_dbg_info(n),
2696 current_ir_graph, block,
2697 new_r_Minus(current_ir_graph, block, x, mode),
2698 new_r_Const_long(current_ir_graph, block, mode_Iu,
2699 get_mode_size_bits(mode) - 1),
2701 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2708 return arch_transform_node_Mux(n);
2712 * Tries several [inplace] [optimizing] transformations and returns an
2713 * equivalent node. The difference to equivalent_node() is that these
2714 * transformations _do_ generate new nodes, and thus the old node must
2715 * not be freed even if the equivalent node isn't the old one.
2717 static ir_node *transform_node(ir_node *n)
2719 if (n->op->transform_node)
2720 n = n->op->transform_node(n);
2725 * set the default transform node operation
2727 static ir_op *firm_set_default_transform_node(ir_op *op)
2731 op->transform_node = transform_node_##a; \
2755 op->transform_node = NULL;
2763 /* **************** Common Subexpression Elimination **************** */
2765 /** The size of the hash table used, should estimate the number of nodes
2767 #define N_IR_NODES 512
2769 /** Compares the attributes of two Const nodes. */
2770 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
2772 return (get_Const_tarval(a) != get_Const_tarval(b))
2773 || (get_Const_type(a) != get_Const_type(b));
2776 /** Compares the attributes of two Proj nodes. */
2777 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
2779 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
2782 /** Compares the attributes of two Filter nodes. */
2783 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
2785 return get_Filter_proj(a) != get_Filter_proj(b);
2788 /** Compares the attributes of two Alloc nodes. */
2789 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
2791 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
2792 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
2795 /** Compares the attributes of two Free nodes. */
2796 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
2798 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
2799 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
2802 /** Compares the attributes of two SymConst nodes. */
2803 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
2805 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
2806 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
2807 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
2810 /** Compares the attributes of two Call nodes. */
2811 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
2813 return (get_irn_call_attr(a) != get_irn_call_attr(b));
2816 /** Compares the attributes of two Sel nodes. */
2817 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
2819 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
2820 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
2821 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
2822 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
2823 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
2826 /** Compares the attributes of two Phi nodes. */
2827 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
2829 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
2832 /** Compares the attributes of two Cast nodes. */
2833 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
2835 return get_Cast_type(a) != get_Cast_type(b);
2838 /** Compares the attributes of two Load nodes. */
2839 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
2841 if (get_Load_volatility(a) == volatility_is_volatile ||
2842 get_Load_volatility(b) == volatility_is_volatile)
2843 /* NEVER do CSE on volatile Loads */
2846 return get_Load_mode(a) != get_Load_mode(b);
2849 /** Compares the attributes of two Store nodes. */
2850 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
2852 /* NEVER do CSE on volatile Stores */
2853 return (get_Store_volatility(a) == volatility_is_volatile ||
2854 get_Store_volatility(b) == volatility_is_volatile);
2857 /** Compares the attributes of two Confirm nodes. */
2858 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
2860 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
2864 * set the default node attribute compare operation
2866 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
2870 op->node_cmp_attr = node_cmp_attr_##a; \
2888 op->node_cmp_attr = NULL;
2896 * Compare function for two nodes in the hash table. Gets two
2897 * nodes as parameters. Returns 0 if the nodes are a cse.
2900 vt_cmp (const void *elt, const void *key)
2908 if (a == b) return 0;
2910 if ((get_irn_op(a) != get_irn_op(b)) ||
2911 (get_irn_mode(a) != get_irn_mode(b))) return 1;
2913 /* compare if a's in and b's in are of equal length */
2914 irn_arity_a = get_irn_intra_arity (a);
2915 if (irn_arity_a != get_irn_intra_arity(b))
2918 /* for block-local cse and op_pin_state_pinned nodes: */
2919 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
2920 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
2924 /* compare a->in[0..ins] with b->in[0..ins] */
2925 for (i = 0; i < irn_arity_a; i++)
2926 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
2930 * here, we already now that the nodes are identical except their
2933 if (a->op->node_cmp_attr)
2934 return a->op->node_cmp_attr(a, b);
2940 * Calculate a hash value of a node.
2943 ir_node_hash (ir_node *node)
2948 if (node->op == op_Const) {
2949 /* special value for const, as they only differ in their tarval. */
2950 h = HASH_PTR(node->attr.con.tv);
2951 h = 9*h + HASH_PTR(get_irn_mode(node));
2952 } else if (node->op == op_SymConst) {
2953 /* special value for const, as they only differ in their symbol. */
2954 h = HASH_PTR(node->attr.i.sym.type_p);
2955 h = 9*h + HASH_PTR(get_irn_mode(node));
2958 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
2959 h = irn_arity = get_irn_intra_arity(node);
2961 /* consider all in nodes... except the block if not a control flow. */
2962 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
2963 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
2967 h = 9*h + HASH_PTR(get_irn_mode(node));
2969 h = 9*h + HASH_PTR(get_irn_op(node));
2976 new_identities(void) {
2977 return new_pset(vt_cmp, N_IR_NODES);
2981 del_identities(pset *value_table) {
2982 del_pset(value_table);
2986 * Return the canonical node computing the same value as n.
2987 * Looks up the node in a hash table.
2989 * For Const nodes this is performed in the constructor, too. Const
2990 * nodes are extremely time critical because of their frequent use in
2991 * constant string arrays.
2993 static INLINE ir_node *
2994 identify (pset *value_table, ir_node *n)
2998 if (!value_table) return n;
3000 if (get_opt_reassociation()) {
3001 if (is_op_commutative(get_irn_op(n))) {
3002 ir_node *l = get_binop_left(n);
3003 ir_node *r = get_binop_right(n);
3005 /* for commutative operators perform a OP b == b OP a */
3007 set_binop_left(n, r);
3008 set_binop_right(n, l);
3013 o = pset_find (value_table, n, ir_node_hash (n));
3022 * During construction we set the op_pin_state_pinned flag in the graph right when the
3023 * optimization is performed. The flag turning on procedure global cse could
3024 * be changed between two allocations. This way we are safe.
3026 static INLINE ir_node *
3027 identify_cons (pset *value_table, ir_node *n) {
3030 n = identify(value_table, n);
3031 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3032 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3037 * Return the canonical node computing the same value as n.
3038 * Looks up the node in a hash table, enters it in the table
3039 * if it isn't there yet.
3042 identify_remember (pset *value_table, ir_node *n)
3046 if (!value_table) return n;
3048 if (get_opt_reassociation()) {
3049 if (is_op_commutative(get_irn_op(n))) {
3050 ir_node *l = get_binop_left(n);
3051 ir_node *r = get_binop_right(n);
3053 /* for commutative operators perform a OP b == b OP a */
3055 set_binop_left(n, r);
3056 set_binop_right(n, l);
3061 /* lookup or insert in hash table with given hash key. */
3062 o = pset_insert (value_table, n, ir_node_hash (n));
3072 add_identities (pset *value_table, ir_node *node) {
3073 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3074 identify_remember (value_table, node);
3078 * garbage in, garbage out. If a node has a dead input, i.e., the
3079 * Bad node is input to the node, return the Bad node.
3081 static INLINE ir_node *
3082 gigo (ir_node *node)
3085 ir_op *op = get_irn_op(node);
3087 /* remove garbage blocks by looking at control flow that leaves the block
3088 and replacing the control flow by Bad. */
3089 if (get_irn_mode(node) == mode_X) {
3090 ir_node *block = get_nodes_block(skip_Proj(node));
3092 /* Don't optimize nodes in immature blocks. */
3093 if (!get_Block_matured(block)) return node;
3094 /* Don't optimize End, may have Bads. */
3095 if (op == op_End) return node;
3097 if (is_Block(block)) {
3098 irn_arity = get_irn_arity(block);
3099 for (i = 0; i < irn_arity; i++) {
3100 if (!is_Bad(get_irn_n(block, i))) break;
3102 if (i == irn_arity) return new_Bad();
3106 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3107 blocks predecessors is dead. */
3108 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3109 irn_arity = get_irn_arity(node);
3112 * Beware: we can only read the block of a non-floating node.
3114 if (is_irn_pinned_in_irg(node) &&
3115 is_Block_dead(get_nodes_block(node)))
3118 for (i = 0; i < irn_arity; i++) {
3119 if (is_Bad(get_irn_n(node, i))) {
3125 /* With this code we violate the agreement that local_optimize
3126 only leaves Bads in Block, Phi and Tuple nodes. */
3127 /* If Block has only Bads as predecessors it's garbage. */
3128 /* If Phi has only Bads as predecessors it's garbage. */
3129 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3130 irn_arity = get_irn_arity(node);
3131 for (i = 0; i < irn_arity; i++) {
3132 if (!is_Bad(get_irn_n(node, i))) break;
3134 if (i == irn_arity) node = new_Bad();
3142 * These optimizations deallocate nodes from the obstack.
3143 * It can only be called if it is guaranteed that no other nodes
3144 * reference this one, i.e., right after construction of a node.
3146 * current_ir_graph must be set to the graph of the node!
3149 optimize_node(ir_node *n)
3153 opcode iro = get_irn_opcode(n);
3155 /* Always optimize Phi nodes: part of the construction. */
3156 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3158 /* constant expression evaluation / constant folding */
3159 if (get_opt_constant_folding()) {
3160 /* neither constants nor Tuple values can be evaluated */
3161 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3162 /* try to evaluate */
3163 tv = computed_value(n);
3164 if (tv != tarval_bad) {
3166 type *old_tp = get_irn_type(n);
3167 int i, arity = get_irn_arity(n);
3171 * Try to recover the type of the new expression.
3173 for (i = 0; i < arity && !old_tp; ++i)
3174 old_tp = get_irn_type(get_irn_n(n, i));
3177 * we MUST copy the node here temporary, because it's still needed
3178 * for DBG_OPT_CSTEVAL
3180 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3181 oldn = alloca(node_size);
3183 memcpy(oldn, n, node_size);
3184 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3186 /* ARG, copy the in array, we need it for statistics */
3187 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3189 /* note the inplace edges module */
3190 edges_node_deleted(n, current_ir_graph);
3192 /* evaluation was successful -- replace the node. */
3193 obstack_free(current_ir_graph->obst, n);
3194 nw = new_Const(get_tarval_mode (tv), tv);
3196 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3197 set_Const_type(nw, old_tp);
3198 DBG_OPT_CSTEVAL(oldn, nw);
3204 /* remove unnecessary nodes */
3205 if (get_opt_constant_folding() ||
3206 (iro == iro_Phi) || /* always optimize these nodes. */
3208 (iro == iro_Proj) ||
3209 (iro == iro_Block) ) /* Flags tested local. */
3210 n = equivalent_node (n);
3212 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3214 /* Common Subexpression Elimination.
3216 * Checks whether n is already available.
3217 * The block input is used to distinguish different subexpressions. Right
3218 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3219 * subexpressions within a block.
3222 n = identify_cons (current_ir_graph->value_table, n);
3225 edges_node_deleted(oldn, current_ir_graph);
3227 /* We found an existing, better node, so we can deallocate the old node. */
3228 obstack_free (current_ir_graph->obst, oldn);
3233 /* Some more constant expression evaluation that does not allow to
3235 iro = get_irn_opcode(n);
3236 if (get_opt_constant_folding() ||
3237 (iro == iro_Cond) ||
3238 (iro == iro_Proj) ||
3239 (iro == iro_Sel)) /* Flags tested local. */
3240 n = transform_node (n);
3242 /* Remove nodes with dead (Bad) input.
3243 Run always for transformation induced Bads. */
3246 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3247 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3248 n = identify_remember (current_ir_graph->value_table, n);
3256 * These optimizations never deallocate nodes (in place). This can cause dead
3257 * nodes lying on the obstack. Remove these by a dead node elimination,
3258 * i.e., a copying garbage collection.
3261 optimize_in_place_2 (ir_node *n)
3265 opcode iro = get_irn_opcode(n);
3267 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3269 /* constant expression evaluation / constant folding */
3270 if (get_opt_constant_folding()) {
3271 /* neither constants nor Tuple values can be evaluated */
3272 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3273 /* try to evaluate */
3274 tv = computed_value(n);
3275 if (tv != tarval_bad) {
3276 /* evaluation was successful -- replace the node. */
3277 type *old_tp = get_irn_type(n);
3278 int i, arity = get_irn_arity(n);
3281 * Try to recover the type of the new expression.
3283 for (i = 0; i < arity && !old_tp; ++i)
3284 old_tp = get_irn_type(get_irn_n(n, i));
3286 n = new_Const(get_tarval_mode(tv), tv);
3288 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3289 set_Const_type(n, old_tp);
3291 DBG_OPT_CSTEVAL(oldn, n);
3297 /* remove unnecessary nodes */
3298 if (get_opt_constant_folding() ||
3299 (iro == iro_Phi) || /* always optimize these nodes. */
3300 (iro == iro_Id) || /* ... */
3301 (iro == iro_Proj) || /* ... */
3302 (iro == iro_Block) ) /* Flags tested local. */
3303 n = equivalent_node(n);
3305 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3307 /** common subexpression elimination **/
3308 /* Checks whether n is already available. */
3309 /* The block input is used to distinguish different subexpressions. Right
3310 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3311 subexpressions within a block. */
3312 if (get_opt_cse()) {
3313 n = identify(current_ir_graph->value_table, n);
3316 /* Some more constant expression evaluation. */
3317 iro = get_irn_opcode(n);
3318 if (get_opt_constant_folding() ||
3319 (iro == iro_Cond) ||
3320 (iro == iro_Proj) ||
3321 (iro == iro_Sel)) /* Flags tested local. */
3322 n = transform_node(n);
3324 /* Remove nodes with dead (Bad) input.
3325 Run always for transformation induced Bads. */
3328 /* Now we can verify the node, as it has no dead inputs any more. */
3331 /* Now we have a legal, useful node. Enter it in hash table for cse.
3332 Blocks should be unique anyways. (Except the successor of start:
3333 is cse with the start block!) */
3334 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3335 n = identify_remember(current_ir_graph->value_table, n);
3341 * Wrapper for external use, set proper status bits after optimization.
3344 optimize_in_place (ir_node *n)
3346 /* Handle graph state */
3347 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3349 if (get_opt_global_cse())
3350 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3351 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3352 set_irg_outs_inconsistent(current_ir_graph);
3354 /* Maybe we could also test whether optimizing the node can
3355 change the control graph. */
3356 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
3357 set_irg_dom_inconsistent(current_ir_graph);
3358 return optimize_in_place_2 (n);
3362 * set the default ir op operations
3364 ir_op *firm_set_default_operations(ir_op *op)
3366 op = firm_set_default_computed_value(op);
3367 op = firm_set_default_equivalent_node(op);
3368 op = firm_set_default_transform_node(op);
3369 op = firm_set_default_node_cmp_attr(op);
3370 op = firm_set_default_get_type(op);