3 * File name: ir/ir/iropt.c
4 * Purpose: iropt --- optimizations intertwined with IR construction.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier
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"
45 /* Make types visible to allow most efficient access */
46 # include "entity_t.h"
49 * Trivial INLINEable routine for copy propagation.
50 * Does follow Ids, needed to optimize INLINEd code.
52 static INLINE ir_node *
53 follow_Id (ir_node *n)
55 while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
60 * return the value of a Constant
62 static tarval *computed_value_Const(ir_node *n)
64 return get_Const_tarval(n);
68 * return the value of a 'sizeof' SymConst
70 static tarval *computed_value_SymConst(ir_node *n)
72 if ((get_SymConst_kind(n) == symconst_size) &&
73 (get_type_state(get_SymConst_type(n))) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
79 * return the value of an Add
81 static tarval *computed_value_Add(ir_node *n)
83 ir_node *a = get_Add_left(n);
84 ir_node *b = get_Add_right(n);
86 tarval *ta = value_of(a);
87 tarval *tb = value_of(b);
89 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
90 return tarval_add(ta, tb);
96 * return the value of a Sub
99 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);
120 * return the value of an unary Minus
122 static tarval *computed_value_Minus(ir_node *n)
124 ir_node *a = get_Minus_op(n);
125 tarval *ta = value_of(a);
127 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
128 return tarval_neg(ta);
134 * return the value of a Mul
136 static tarval *computed_value_Mul(ir_node *n)
138 ir_node *a = get_Mul_left(n);
139 ir_node *b = get_Mul_right(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
145 return tarval_mul(ta, tb);
147 /* a*0 = 0 or 0*b = 0:
148 calls computed_value recursive and returns the 0 with proper
150 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
152 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
159 * return the value of a floating point Quot
161 static tarval *computed_value_Quot(ir_node *n)
163 ir_node *a = get_Quot_left(n);
164 ir_node *b = get_Quot_right(n);
166 tarval *ta = value_of(a);
167 tarval *tb = value_of(b);
169 /* This was missing in original implementation. Why? */
170 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
171 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
172 return tarval_quo(ta, tb);
178 * calculate the value of an integer Div of two nodes
179 * Special case: 0 / b
181 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
183 tarval *ta = value_of(a);
184 tarval *tb = value_of(b);
186 /* Compute c1 / c2 or 0 / a, a != 0 */
187 if (ta != tarval_bad) {
188 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
189 return tarval_div(ta, tb);
190 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
197 * return the value of an integer Div
199 static tarval *computed_value_Div(ir_node *n)
201 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
205 * calculate the value of an integer Mod of two nodes
206 * Special case: a % 1
208 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
210 tarval *ta = value_of(a);
211 tarval *tb = value_of(b);
213 /* Compute c1 % c2 or a % 1 */
214 if (tb != tarval_bad) {
215 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
216 return tarval_mod(ta, tb);
217 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
218 return get_mode_null(get_irn_mode(a));
225 * return the value of an integer Mod
227 static tarval *computed_value_Mod(ir_node *n)
229 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
233 * return the value of an Abs
235 static tarval *computed_value_Abs(ir_node *n)
237 ir_node *a = get_Abs_op(n);
238 tarval *ta = value_of(a);
240 if (ta != tarval_bad)
241 return tarval_abs(ta);
247 * return the value of an And
248 * Special case: a & 0, 0 & b
250 static tarval *computed_value_And(ir_node *n)
252 ir_node *a = get_And_left(n);
253 ir_node *b = get_And_right(n);
255 tarval *ta = value_of(a);
256 tarval *tb = value_of(b);
258 if ((ta != tarval_bad) && (tb != tarval_bad)) {
259 return tarval_and (ta, tb);
263 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
264 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
272 * return the value of an Or
273 * Special case: a | 1...1, 1...1 | b
275 static tarval *computed_value_Or(ir_node *n)
277 ir_node *a = get_Or_left(n);
278 ir_node *b = get_Or_right(n);
280 tarval *ta = value_of(a);
281 tarval *tb = value_of(b);
283 if ((ta != tarval_bad) && (tb != tarval_bad)) {
284 return tarval_or (ta, tb);
287 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
288 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
296 * return the value of an Eor
298 static tarval *computed_value_Eor(ir_node *n)
300 ir_node *a = get_Eor_left(n);
301 ir_node *b = get_Eor_right(n);
306 return get_mode_null(get_irn_mode(n));
311 if ((ta != tarval_bad) && (tb != tarval_bad)) {
312 return tarval_eor (ta, tb);
318 * return the value of a Not
320 static tarval *computed_value_Not(ir_node *n)
322 ir_node *a = get_Not_op(n);
323 tarval *ta = value_of(a);
325 if (ta != tarval_bad)
326 return tarval_not(ta);
332 * return the value of a Shl
334 static tarval *computed_value_Shl(ir_node *n)
336 ir_node *a = get_Shl_left(n);
337 ir_node *b = get_Shl_right(n);
339 tarval *ta = value_of(a);
340 tarval *tb = value_of(b);
342 if ((ta != tarval_bad) && (tb != tarval_bad)) {
343 return tarval_shl (ta, tb);
349 * return the value of a Shr
351 static tarval *computed_value_Shr(ir_node *n)
353 ir_node *a = get_Shr_left(n);
354 ir_node *b = get_Shr_right(n);
356 tarval *ta = value_of(a);
357 tarval *tb = value_of(b);
359 if ((ta != tarval_bad) && (tb != tarval_bad)) {
360 return tarval_shr (ta, tb);
366 * return the value of a Shrs
368 static tarval *computed_value_Shrs(ir_node *n)
370 ir_node *a = get_Shrs_left(n);
371 ir_node *b = get_Shrs_right(n);
373 tarval *ta = value_of(a);
374 tarval *tb = value_of(b);
376 if ((ta != tarval_bad) && (tb != tarval_bad)) {
377 return tarval_shrs (ta, tb);
383 * return the value of a Rot
385 static tarval *computed_value_Rot(ir_node *n)
387 ir_node *a = get_Rot_left(n);
388 ir_node *b = get_Rot_right(n);
390 tarval *ta = value_of(a);
391 tarval *tb = value_of(b);
393 if ((ta != tarval_bad) && (tb != tarval_bad)) {
394 return tarval_rot (ta, tb);
400 * return the value of a Conv
402 static tarval *computed_value_Conv(ir_node *n)
404 ir_node *a = get_Conv_op(n);
405 tarval *ta = value_of(a);
407 if (ta != tarval_bad)
408 return tarval_convert_to(ta, get_irn_mode(n));
414 * return the value of a Proj(Cmp)
416 * This performs a first step of unreachable code elimination.
417 * Proj can not be computed, but folding a Cmp above the Proj here is
418 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
420 * There are several case where we can evaluate a Cmp node, see later.
422 static tarval *computed_value_Proj_Cmp(ir_node *n)
424 ir_node *a = get_Proj_pred(n);
425 ir_node *aa = get_Cmp_left(a);
426 ir_node *ab = get_Cmp_right(a);
427 long proj_nr = get_Proj_proj(n);
430 * BEWARE: a == a is NOT always True for floating Point values, as
431 * NaN != NaN is defined, so we must check this here.
434 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
437 /* This is a trick with the bits used for encoding the Cmp
438 Proj numbers, the following statement is not the same:
439 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
440 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
443 tarval *taa = value_of(aa);
444 tarval *tab = value_of(ab);
445 ir_mode *mode = get_irn_mode(aa);
448 * The predecessors of Cmp are target values. We can evaluate
451 if ((taa != tarval_bad) && (tab != tarval_bad)) {
452 /* strange checks... */
453 pn_Cmp flags = tarval_cmp(taa, tab);
454 if (flags != pn_Cmp_False) {
455 return new_tarval_from_long (proj_nr & flags, mode_b);
458 /* for integer values, we can check against MIN/MAX */
459 else if (mode_is_int(mode)) {
460 /* MIN <=/> x. This results in true/false. */
461 if (taa == get_mode_min(mode)) {
462 /* a compare with the MIN value */
463 if (proj_nr == pn_Cmp_Le)
464 return get_tarval_b_true();
465 else if (proj_nr == pn_Cmp_Gt)
466 return get_tarval_b_false();
468 /* x >=/< MIN. This results in true/false. */
470 if (tab == get_mode_min(mode)) {
471 /* a compare with the MIN value */
472 if (proj_nr == pn_Cmp_Ge)
473 return get_tarval_b_true();
474 else if (proj_nr == pn_Cmp_Lt)
475 return get_tarval_b_false();
477 /* MAX >=/< x. This results in true/false. */
478 else if (taa == get_mode_max(mode)) {
479 if (proj_nr == pn_Cmp_Ge)
480 return get_tarval_b_true();
481 else if (proj_nr == pn_Cmp_Lt)
482 return get_tarval_b_false();
484 /* x <=/> MAX. This results in true/false. */
485 else if (tab == get_mode_max(mode)) {
486 if (proj_nr == pn_Cmp_Le)
487 return get_tarval_b_true();
488 else if (proj_nr == pn_Cmp_Gt)
489 return get_tarval_b_false();
493 * The predecessors are Allocs or (void*)(0) constants. Allocs never
494 * return NULL, they raise an exception. Therefore we can predict
498 ir_node *aaa = skip_Id(skip_Proj(aa));
499 ir_node *aba = skip_Id(skip_Proj(ab));
501 if ( ( (/* aa is ProjP and aaa is Alloc */
502 (get_irn_op(aa) == op_Proj)
503 && (mode_is_reference(get_irn_mode(aa)))
504 && (get_irn_op(aaa) == op_Alloc))
505 && ( (/* ab is NULL */
506 (get_irn_op(ab) == op_Const)
507 && (mode_is_reference(get_irn_mode(ab)))
508 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
509 || (/* ab is other Alloc */
510 (get_irn_op(ab) == op_Proj)
511 && (mode_is_reference(get_irn_mode(ab)))
512 && (get_irn_op(aba) == op_Alloc)
514 || (/* aa is NULL and aba is Alloc */
515 (get_irn_op(aa) == op_Const)
516 && (mode_is_reference(get_irn_mode(aa)))
517 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
518 && (get_irn_op(ab) == op_Proj)
519 && (mode_is_reference(get_irn_mode(ab)))
520 && (get_irn_op(aba) == op_Alloc)))
522 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
529 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
531 static tarval *computed_value_Proj(ir_node *n)
533 ir_node *a = get_Proj_pred(n);
536 switch (get_irn_opcode(a)) {
538 return computed_value_Proj_Cmp(n);
541 /* compute either the Div or the Mod part */
542 proj_nr = get_Proj_proj(n);
543 if (proj_nr == pn_DivMod_res_div)
544 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
545 else if (proj_nr == pn_DivMod_res_mod)
546 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
550 if (get_Proj_proj(n) == pn_Div_res)
551 return computed_value(a);
555 if (get_Proj_proj(n) == pn_Mod_res)
556 return computed_value(a);
566 * calculate the value of a Mux: can be evaluated, if the
567 * sel and the right input are known
569 static tarval *computed_value_Mux(ir_node *n)
571 ir_node *sel = get_Mux_sel(n);
572 tarval *ts = value_of(sel);
574 if (ts == get_tarval_b_true()) {
575 ir_node *v = get_Mux_true(n);
578 else if (ts == get_tarval_b_false()) {
579 ir_node *v = get_Mux_false(n);
586 * If the parameter n can be computed, return its value, else tarval_bad.
587 * Performs constant folding.
589 * @param n The node this should be evaluated
591 tarval *computed_value(ir_node *n)
593 if (n->op->computed_value)
594 return n->op->computed_value(n);
599 * set the default computed_value evaluator
601 static ir_op *firm_set_default_computed_value(ir_op *op)
605 op->computed_value = computed_value_##a; \
631 op->computed_value = NULL;
639 /* returns 1 if the a and b are pointers to different locations. */
641 different_identity (ir_node *a, ir_node *b)
643 assert (mode_is_reference(get_irn_mode (a))
644 && mode_is_reference(get_irn_mode (b)));
646 if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
647 ir_node *a1 = get_Proj_pred (a);
648 ir_node *b1 = get_Proj_pred (b);
649 if (a1 != b1 && get_irn_op (a1) == op_Alloc
650 && get_irn_op (b1) == op_Alloc)
658 * Returns a equivalent block for another block.
659 * If the block has only one predecessor, this is
660 * the equivalent one. If the only predecessor of a block is
661 * the block itself, this is a dead block.
663 * If both predecessors of a block are the branches of a binary
664 * Cond, the equivalent block is Cond's block.
666 * If all predecessors of a block are bad or lies in a dead
667 * block, the current block is dead as well.
669 * Note, that blocks are NEVER turned into Bad's, instead
670 * the dead_block flag is set. So, never test for is_Bad(block),
671 * always use is_dead_Block(block).
673 static ir_node *equivalent_node_Block(ir_node *n)
676 int n_preds = get_Block_n_cfgpreds(n);
678 /* The Block constructor does not call optimize, but mature_immBlock
679 calls the optimization. */
680 assert(get_Block_matured(n));
682 /* Straightening: a single entry Block following a single exit Block
683 can be merged, if it is not the Start block. */
684 /* !!! Beware, all Phi-nodes of n must have been optimized away.
685 This should be true, as the block is matured before optimize is called.
686 But what about Phi-cycles with the Phi0/Id that could not be resolved?
687 Remaining Phi nodes are just Ids. */
688 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
689 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
690 if (predblock == oldn) {
691 /* Jmp jumps into the block it is in -- deal self cycle. */
692 n = set_Block_dead(n);
693 DBG_OPT_DEAD(oldn, n);
694 } else if (get_opt_control_flow_straightening()) {
696 DBG_OPT_STG(oldn, n);
699 else if ((n_preds == 1) &&
700 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
701 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
702 if (predblock == oldn) {
703 /* Jmp jumps into the block it is in -- deal self cycle. */
704 n = set_Block_dead(n);
705 DBG_OPT_DEAD(oldn, n);
708 else if ((n_preds == 2) &&
709 (get_opt_control_flow_weak_simplification())) {
710 /* Test whether Cond jumps twice to this block
711 @@@ we could do this also with two loops finding two preds from several ones. */
712 ir_node *a = get_Block_cfgpred(n, 0);
713 ir_node *b = get_Block_cfgpred(n, 1);
715 if ((get_irn_op(a) == op_Proj) &&
716 (get_irn_op(b) == op_Proj) &&
717 (get_Proj_pred(a) == get_Proj_pred(b)) &&
718 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
719 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
720 /* Also a single entry Block following a single exit Block. Phis have
721 twice the same operand and will be optimized away. */
722 n = get_nodes_block(a);
723 DBG_OPT_IFSIM(oldn, a, b, n);
725 } else if (get_opt_unreachable_code() &&
726 (n != current_ir_graph->start_block) &&
727 (n != current_ir_graph->end_block) ) {
728 int i, n_cfg = get_Block_n_cfgpreds(n);
730 /* If all inputs are dead, this block is dead too, except if it is
731 the start or end block. This is a step of unreachable code
733 for (i = 0; i < n_cfg; i++) {
734 ir_node *pred = get_Block_cfgpred(n, i);
737 if (is_Bad(pred)) continue;
738 pred_blk = get_nodes_block(pred);
740 if (is_Block_dead(pred_blk)) continue;
743 /* really found a living input */
748 n = set_Block_dead(n);
755 * Returns a equivalent node for a Jmp, a Bad :-)
756 * Of course this only happens if the Block of the Jmp is Bad.
758 static ir_node *equivalent_node_Jmp(ir_node *n)
760 /* GL: Why not same for op_Raise?? */
761 /* unreachable code elimination */
762 if (is_Block_dead(get_nodes_block(n)))
768 static ir_node *equivalent_node_Cond(ir_node *n)
770 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
771 See cases for iro_Cond and iro_Proj in transform_node(). */
776 * optimize operations that are commutative and have neutral 0,
777 * so a op 0 = 0 op a = a.
779 static ir_node *equivalent_node_neutral_zero(ir_node *n)
783 ir_node *a = get_binop_left(n);
784 ir_node *b = get_binop_right(n);
789 /* After running compute_node there is only one constant predecessor.
790 Find this predecessors value and remember the other node: */
791 if ((tv = value_of(a)) != tarval_bad) {
793 } else if ((tv = value_of(b)) != tarval_bad) {
798 /* If this predecessors constant value is zero, the operation is
799 unnecessary. Remove it: */
800 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
803 DBG_OPT_ALGSIM1(oldn, a, b, n);
809 #define equivalent_node_Add equivalent_node_neutral_zero
810 #define equivalent_node_Eor equivalent_node_neutral_zero
813 * optimize operations that are not commutative but have neutral 0 on left,
816 static ir_node *equivalent_node_left_zero(ir_node *n)
820 ir_node *a = get_binop_left(n);
821 ir_node *b = get_binop_right(n);
823 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
826 DBG_OPT_ALGSIM1(oldn, a, b, n);
832 #define equivalent_node_Sub equivalent_node_left_zero
833 #define equivalent_node_Shl equivalent_node_left_zero
834 #define equivalent_node_Shr equivalent_node_left_zero
835 #define equivalent_node_Shrs equivalent_node_left_zero
836 #define equivalent_node_Rot equivalent_node_left_zero
839 * Optimize an "idempotent unary op", ie op(op(n)) = n.
841 * @fixme -(-a) == a, but might overflow two times.
842 * We handle it anyway here but the better way would be a
843 * flag. This would be needed for Pascal for instance.
845 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
848 ir_node *pred = get_unop_op(n);
850 /* optimize symmetric unop */
851 if (get_irn_op(pred) == get_irn_op(n)) {
852 n = get_unop_op(pred);
853 DBG_OPT_ALGSIM2(oldn, pred, n);
858 /* Not(Not(x)) == x */
859 #define equivalent_node_Not equivalent_node_idempotent_unop
861 /* --x == x */ /* ??? Is this possible or can --x raise an
862 out of bounds exception if min =! max? */
863 #define equivalent_node_Minus equivalent_node_idempotent_unop
866 * Optimize a * 1 = 1 * a = a.
868 static ir_node *equivalent_node_Mul(ir_node *n)
872 ir_node *a = get_Mul_left(n);
873 ir_node *b = get_Mul_right(n);
875 /* Mul is commutative and has again an other neutral element. */
876 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
878 DBG_OPT_ALGSIM1(oldn, a, b, n);
879 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
881 DBG_OPT_ALGSIM1(oldn, a, b, n);
887 * Optimize a / 1 = a.
889 static ir_node *equivalent_node_Div(ir_node *n)
891 ir_node *a = get_Div_left(n);
892 ir_node *b = get_Div_right(n);
894 /* Div is not commutative. */
895 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
896 /* Turn Div into a tuple (mem, bad, a) */
897 ir_node *mem = get_Div_mem(n);
898 turn_into_tuple(n, 3);
899 set_Tuple_pred(n, pn_Div_M, mem);
900 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
901 set_Tuple_pred(n, pn_Div_res, a);
907 * Optimize a / 1 = a.
909 static ir_node *equivalent_node_DivMod(ir_node *n)
911 ir_node *a = get_DivMod_left(n);
912 ir_node *b = get_DivMod_right(n);
914 /* Div is not commutative. */
915 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
916 /* Turn DivMod into a tuple (mem, bad, a, 0) */
917 ir_node *mem = get_Div_mem(n);
918 ir_mode *mode = get_irn_mode(b);
920 turn_into_tuple(n, 4);
921 set_Tuple_pred(n, pn_DivMod_M, mem);
922 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
923 set_Tuple_pred(n, pn_DivMod_res_div, a);
924 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
930 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
932 static ir_node *equivalent_node_Or(ir_node *n)
936 ir_node *a = get_Or_left(n);
937 ir_node *b = get_Or_right(n);
940 n = a; /* Or has it's own neutral element */
941 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
943 DBG_OPT_ALGSIM1(oldn, a, b, n);
944 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
946 DBG_OPT_ALGSIM1(oldn, a, b, n);
953 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
955 static ir_node *equivalent_node_And(ir_node *n)
959 ir_node *a = get_And_left(n);
960 ir_node *b = get_And_right(n);
963 n = a; /* And has it's own neutral element */
964 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
966 DBG_OPT_ALGSIM1(oldn, a, b, n);
967 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
969 DBG_OPT_ALGSIM1(oldn, a, b, n);
975 * Try to remove useless Conv's:
977 static ir_node *equivalent_node_Conv(ir_node *n)
980 ir_node *a = get_Conv_op(n);
983 ir_mode *n_mode = get_irn_mode(n);
984 ir_mode *a_mode = get_irn_mode(a);
986 if (n_mode == a_mode) { /* No Conv necessary */
988 DBG_OPT_ALGSIM3(oldn, a, n);
989 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
993 n_mode = get_irn_mode(n);
994 b_mode = get_irn_mode(b);
996 if (n_mode == b_mode) {
997 if (n_mode == mode_b) {
998 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
999 DBG_OPT_ALGSIM1(oldn, a, b, n);
1001 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1002 if (smaller_mode(b_mode, a_mode)){
1003 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1004 DBG_OPT_ALGSIM1(oldn, a, b, n);
1013 * A Cast may be removed if the type of the previous node
1014 * is already the type of the Cast.
1016 static ir_node *equivalent_node_Cast(ir_node *n) {
1017 ir_node *pred = get_Cast_op(n);
1018 if (get_irn_type(pred) == get_Cast_type(n))
1023 /* Several optimizations:
1024 - no Phi in start block.
1025 - remove Id operators that are inputs to Phi
1026 - fold Phi-nodes, iff they have only one predecessor except
1029 static ir_node *equivalent_node_Phi(ir_node *n)
1034 ir_node *block = NULL; /* to shutup gcc */
1035 ir_node *first_val = NULL; /* to shutup gcc */
1036 ir_node *scnd_val = NULL; /* to shutup gcc */
1038 if (!get_opt_normalize()) return n;
1040 n_preds = get_Phi_n_preds(n);
1042 block = get_nodes_block(n);
1043 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1044 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1045 if ((is_Block_dead(block)) || /* Control dead */
1046 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1047 return new_Bad(); /* in the Start Block. */
1049 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1052 /* first we test for a special case: */
1053 /* Confirm is a special node fixing additional information for a
1054 value that is known at a certain point. This is useful for
1055 dataflow analysis. */
1057 ir_node *a = get_Phi_pred(n, 0);
1058 ir_node *b = get_Phi_pred(n, 1);
1059 if ( (get_irn_op(a) == op_Confirm)
1060 && (get_irn_op(b) == op_Confirm)
1061 && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
1062 && (get_irn_n(a, 1) == get_irn_n (b, 1))
1063 && (a->data.num == (~b->data.num & irpn_True) )) {
1064 return get_irn_n(a, 0);
1069 /* If the Block has a Bad pred, we also have one. */
1070 for (i = 0; i < n_preds; ++i)
1071 if (is_Bad (get_Block_cfgpred(block, i)))
1072 set_Phi_pred(n, i, new_Bad());
1074 /* Find first non-self-referencing input */
1075 for (i = 0; i < n_preds; ++i) {
1076 first_val = get_Phi_pred(n, i);
1077 if ( (first_val != n) /* not self pointer */
1079 && (get_irn_op(first_val) != op_Bad)
1081 ) { /* value not dead */
1082 break; /* then found first value. */
1086 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1087 if (i >= n_preds) { return new_Bad(); }
1091 /* follow_Id () for rest of inputs, determine if any of these
1092 are non-self-referencing */
1093 while (++i < n_preds) {
1094 scnd_val = get_Phi_pred(n, i);
1095 if ( (scnd_val != n)
1096 && (scnd_val != first_val)
1098 && (get_irn_op(scnd_val) != op_Bad)
1105 /* Fold, if no multiple distinct non-self-referencing inputs */
1108 DBG_OPT_PHI(oldn, first_val, n);
1110 /* skip the remaining Ids (done in get_Phi_pred). */
1111 /* superfluous, since we walk all to propagate Block's Bads.
1112 while (++i < n_preds) get_Phi_pred(n, i); */
1118 * optimize Proj(Tuple) and gigo for ProjX in Bad block
1120 static ir_node *equivalent_node_Proj(ir_node *n)
1124 ir_node *a = get_Proj_pred(n);
1126 if ( get_irn_op(a) == op_Tuple) {
1127 /* Remove the Tuple/Proj combination. */
1128 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1129 n = get_Tuple_pred(a, get_Proj_proj(n));
1130 DBG_OPT_TUPLE(oldn, a, n);
1132 assert(0); /* This should not happen! */
1135 } else if (get_irn_mode(n) == mode_X &&
1136 is_Block_dead(get_nodes_block(n))) {
1137 /* Remove dead control flow -- early gigo. */
1146 static ir_node *equivalent_node_Id(ir_node *n)
1151 DBG_OPT_ID(oldn, n);
1158 static ir_node *equivalent_node_Mux(ir_node *n)
1160 ir_node *oldn = n, *sel = get_Mux_sel(n);
1161 tarval *ts = value_of(sel);
1163 /* Mux(true, f, t) == t */
1164 if (ts == get_tarval_b_true()) {
1165 n = get_Mux_true(n);
1166 DBG_OPT_ALGSIM0(oldn, n);
1168 /* Mux(false, f, t) == f */
1169 else if (ts == get_tarval_b_false()) {
1170 n = get_Mux_false(n);
1171 DBG_OPT_ALGSIM0(oldn, n);
1173 /* Mux(v, x, x) == x */
1174 else if (get_Mux_false(n) == get_Mux_true(n)) {
1175 n = get_Mux_true(n);
1176 DBG_OPT_ALGSIM0(oldn, n);
1178 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1179 ir_node *cmp = get_Proj_pred(sel);
1180 long proj_nr = get_Proj_proj(sel);
1181 ir_node *b = get_Mux_false(n);
1182 ir_node *a = get_Mux_true(n);
1185 * Note: normalization puts the constant on the right site,
1186 * so we check only one case.
1188 * Note further that these optimization work even for floating point
1189 * with NaN's because -NaN == NaN.
1190 * However, if +0 and -0 is handled differently, we cannot use the first one.
1192 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1193 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1194 /* Mux(a CMP 0, X, a) */
1195 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1196 /* Mux(a CMP 0, -a, a) */
1197 if (proj_nr == pn_Cmp_Eq) {
1198 /* Mux(a == 0, -a, a) ==> -a */
1200 DBG_OPT_ALGSIM0(oldn, n);
1202 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1203 /* Mux(a != 0, -a, a) ==> a */
1205 DBG_OPT_ALGSIM0(oldn, n);
1208 else if (classify_Const(b) == CNST_NULL) {
1209 /* Mux(a CMP 0, 0, a) */
1210 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1211 /* Mux(a != 0, 0, a) ==> a */
1213 DBG_OPT_ALGSIM0(oldn, n);
1215 else if (proj_nr == pn_Cmp_Eq) {
1216 /* Mux(a == 0, 0, a) ==> 0 */
1218 DBG_OPT_ALGSIM0(oldn, n);
1229 * Optimize -a CMP -b into b CMP a.
1230 * This works only for for modes where unary Minus
1232 * Note that two-complement integers can Overflow
1233 * so it will NOT work.
1235 static ir_node *equivalent_node_Cmp(ir_node *n)
1237 ir_node *left = get_Cmp_left(n);
1238 ir_node *right = get_Cmp_right(n);
1240 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1241 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1242 left = get_Minus_op(left);
1243 right = get_Minus_op(right);
1244 set_Cmp_left(n, right);
1245 set_Cmp_right(n, left);
1251 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1252 * perform no actual computation, as, e.g., the Id nodes. It does not create
1253 * new nodes. It is therefore safe to free n if the node returned is not n.
1254 * If a node returns a Tuple we can not just skip it. If the size of the
1255 * in array fits, we transform n into a tuple (e.g., Div).
1258 equivalent_node(ir_node *n)
1260 if (n->op->equivalent_node)
1261 return n->op->equivalent_node(n);
1266 * set the default equivalent node operation
1268 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1272 op->equivalent_node = equivalent_node_##a; \
1301 op->equivalent_node = NULL;
1309 * Do node specific optimizations of nodes predecessors.
1312 optimize_preds(ir_node *n) {
1313 ir_node *a = NULL, *b = NULL;
1315 /* get the operands we will work on for simple cases. */
1317 a = get_binop_left(n);
1318 b = get_binop_right(n);
1319 } else if (is_unop(n)) {
1323 switch (get_irn_opcode(n)) {
1326 /* We don't want Cast as input to Cmp. */
1327 if (get_irn_op(a) == op_Cast) {
1331 if (get_irn_op(b) == op_Cast) {
1333 set_Cmp_right(n, b);
1342 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1343 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1344 * If possible, remove the Conv's.
1346 static ir_node *transform_node_AddSub(ir_node *n)
1348 ir_mode *mode = get_irn_mode(n);
1350 if (mode_is_reference(mode)) {
1351 ir_node *left = get_binop_left(n);
1352 ir_node *right = get_binop_right(n);
1353 int ref_bits = get_mode_size_bits(mode);
1355 if (get_irn_op(left) == op_Conv) {
1356 ir_mode *mode = get_irn_mode(left);
1357 int bits = get_mode_size_bits(mode);
1359 if (ref_bits == bits &&
1360 mode_is_int(mode) &&
1361 get_mode_arithmetic(mode) == irma_twos_complement) {
1362 ir_node *pre = get_Conv_op(left);
1363 ir_mode *pre_mode = get_irn_mode(pre);
1365 if (mode_is_int(pre_mode) &&
1366 get_mode_size_bits(pre_mode) == bits &&
1367 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1368 /* ok, this conv just changes to sign, moreover the calculation
1369 * is done with same number of bits as our address mode, so
1370 * we can ignore the conv as address calculation can be viewed
1371 * as either signed or unsigned
1373 set_binop_left(n, pre);
1378 if (get_irn_op(right) == op_Conv) {
1379 ir_mode *mode = get_irn_mode(right);
1380 int bits = get_mode_size_bits(mode);
1382 if (ref_bits == bits &&
1383 mode_is_int(mode) &&
1384 get_mode_arithmetic(mode) == irma_twos_complement) {
1385 ir_node *pre = get_Conv_op(right);
1386 ir_mode *pre_mode = get_irn_mode(pre);
1388 if (mode_is_int(pre_mode) &&
1389 get_mode_size_bits(pre_mode) == bits &&
1390 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1391 /* ok, this conv just changes to sign, moreover the calculation
1392 * is done with same number of bits as our address mode, so
1393 * we can ignore the conv as address calculation can be viewed
1394 * as either signed or unsigned
1396 set_binop_right(n, pre);
1405 * Do the AddSub optimization, then Transform Add(a,a) into Mul(a, 2)
1406 * if the mode is integer or float.
1407 * Transform Add(a,-b) into Sub(a,b).
1408 * Reassociation might fold this further.
1410 static ir_node *transform_node_Add(ir_node *n)
1415 n = transform_node_AddSub(n);
1417 mode = get_irn_mode(n);
1418 if (mode_is_num(mode)) {
1419 ir_node *a = get_Add_left(n);
1421 if (a == get_Add_right(n)) {
1422 ir_node *block = get_nodes_block(n);
1425 get_irn_dbg_info(n),
1429 new_r_Const_long(current_ir_graph, block, mode, 2),
1431 DBG_OPT_ALGSIM0(oldn, n);
1434 ir_node *b = get_Add_right(n);
1436 if (get_irn_op(a) == op_Minus) {
1438 get_irn_dbg_info(n),
1444 DBG_OPT_ALGSIM0(oldn, n);
1446 else if (get_irn_op(b) == op_Minus) {
1448 get_irn_dbg_info(n),
1454 DBG_OPT_ALGSIM0(oldn, n);
1462 * Do the AddSub optimization, then Transform Sub(0,a) into Minus(a).
1464 static ir_node *transform_node_Sub(ir_node *n)
1469 n = transform_node_AddSub(n);
1471 mode = get_irn_mode(n);
1472 if (mode_is_num(mode) && (classify_Const(get_Sub_left(n)) == CNST_NULL)) {
1474 get_irn_dbg_info(n),
1479 DBG_OPT_ALGSIM0(oldn, n);
1486 Transform Mul(a,-1) into -a.
1487 * Do architecture dependend optimizations on Mul nodes
1489 static ir_node *transform_node_Mul(ir_node *n) {
1491 ir_mode *mode = get_irn_mode(n);
1493 if (mode_is_signed(mode)) {
1495 ir_node *a = get_Mul_left(n);
1496 ir_node *b = get_Mul_right(n);
1498 if (value_of(a) == get_mode_minus_one(mode))
1500 else if (value_of(b) == get_mode_minus_one(mode))
1503 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
1504 DBG_OPT_ALGSIM1(oldn, a, b, n);
1508 return arch_dep_replace_mul_with_shifts(n);
1512 * transform a Div Node
1514 static ir_node *transform_node_Div(ir_node *n)
1516 tarval *tv = value_of(n);
1519 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1521 if (tv != tarval_bad) {
1522 value = new_Const(get_tarval_mode(tv), tv);
1524 DBG_OPT_CSTEVAL(n, value);
1526 else /* Try architecture dependend optimization */
1527 value = arch_dep_replace_div_by_const(n);
1530 /* Turn Div into a tuple (mem, bad, value) */
1531 ir_node *mem = get_Div_mem(n);
1533 turn_into_tuple(n, 3);
1534 set_Tuple_pred(n, pn_Div_M, mem);
1535 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1536 set_Tuple_pred(n, pn_Div_res, value);
1542 * transform a Mod node
1544 static ir_node *transform_node_Mod(ir_node *n)
1546 tarval *tv = value_of(n);
1549 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1551 if (tv != tarval_bad) {
1552 value = new_Const(get_tarval_mode(tv), tv);
1554 DBG_OPT_CSTEVAL(n, value);
1556 else /* Try architecture dependend optimization */
1557 value = arch_dep_replace_mod_by_const(n);
1560 /* Turn Mod into a tuple (mem, bad, value) */
1561 ir_node *mem = get_Mod_mem(n);
1563 turn_into_tuple(n, 3);
1564 set_Tuple_pred(n, pn_Mod_M, mem);
1565 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1566 set_Tuple_pred(n, pn_Mod_res, value);
1572 * transform a DivMod node
1574 static ir_node *transform_node_DivMod(ir_node *n)
1578 ir_node *a = get_DivMod_left(n);
1579 ir_node *b = get_DivMod_right(n);
1580 ir_mode *mode = get_irn_mode(a);
1581 tarval *ta = value_of(a);
1582 tarval *tb = value_of(b);
1584 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1587 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1589 if (tb != tarval_bad) {
1590 if (tb == get_mode_one(get_tarval_mode(tb))) {
1591 b = new_Const (mode, get_mode_null(mode));
1594 DBG_OPT_CSTEVAL(n, b);
1596 else if (ta != tarval_bad) {
1597 tarval *resa, *resb;
1598 resa = tarval_div (ta, tb);
1599 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1600 Jmp for X result!? */
1601 resb = tarval_mod (ta, tb);
1602 if (resb == tarval_bad) return n; /* Causes exception! */
1603 a = new_Const (mode, resa);
1604 b = new_Const (mode, resb);
1607 DBG_OPT_CSTEVAL(n, a);
1608 DBG_OPT_CSTEVAL(n, b);
1610 else { /* Try architecture dependend optimization */
1611 arch_dep_replace_divmod_by_const(&a, &b, n);
1612 evaluated = a != NULL;
1614 } else if (ta == get_mode_null(mode)) {
1615 /* 0 / non-Const = 0 */
1620 if (evaluated) { /* replace by tuple */
1621 ir_node *mem = get_DivMod_mem(n);
1622 turn_into_tuple(n, 4);
1623 set_Tuple_pred(n, pn_DivMod_M, mem);
1624 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1625 set_Tuple_pred(n, pn_DivMod_res_div, a);
1626 set_Tuple_pred(n, pn_DivMod_res_mod, b);
1627 assert(get_nodes_block(n));
1635 * Optimize Abs(x) => x if x is Confirmed >= 0
1636 * Optimize Abs(x) => -x if x is Confirmed <= 0
1638 static ir_node *transform_node_Abs(ir_node *n)
1641 ir_node *a = get_Abs_op(n);
1647 if (get_irn_op(a) != op_Confirm)
1650 tv = value_of(get_Confirm_bound(n));
1651 if (tv == tarval_bad)
1654 mode = get_irn_mode(n);
1657 * We can handle only >=, >, <, <= cases.
1658 * We could handle == too, but this should not happen.
1660 * Note that for integer modes we have a slightly better
1661 * optimization possibilities, so we handle this
1664 cmp = get_Confirm_cmp(n);
1669 * must be x < c <= 1 to be useful if integer mode and -0 = 0
1670 * x < c <= 0 to be useful else
1674 * must be x <= c < 1 to be useful if integer mode and -0 = 0
1675 * x <= c < 0 to be useful else
1677 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
1678 get_mode_one(mode) : get_mode_null(mode);
1680 ncmp = tarval_cmp(tv, c) ^ pn_Cmp_Eq;
1684 /* yep, we can replace by -x */
1690 * must be x >= c > -1 to be useful if integer mode
1691 * x >= c >= 0 to be useful else
1695 * must be x > c >= -1 to be useful if integer mode
1696 * x > c >= 0 to be useful else
1698 if (mode_is_int(mode)) {
1699 c = get_mode_minus_one(mode);
1701 ncmp = tarval_cmp(tv, c) ^ pn_Cmp_Eq;
1706 c = get_mode_minus_one(mode);
1708 ncmp = tarval_cmp(tv, c);
1710 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
1714 /* yep, we can replace by x */
1723 * Ok, when we get here, we can replace the Abs
1724 * by either x or -x. In the second case we eve could
1725 * add a new Confirm.
1727 * Note that -x would create a new node, so we could
1728 * not run it in the equivalent_node() context.
1733 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1734 get_nodes_block(n), a, mode);
1736 c = new_Const(mode, tarval_neg(tv));
1737 n = new_r_Confirm(current_ir_graph, get_nodes_block(n), n, c, get_inversed_pnc(cmp));
1747 * transform a Cond node
1749 static ir_node *transform_node_Cond(ir_node *n)
1751 /* Replace the Cond by a Jmp if it branches on a constant
1754 ir_node *a = get_Cond_selector(n);
1755 tarval *ta = value_of(a);
1757 if ((ta != tarval_bad) &&
1758 (get_irn_mode(a) == mode_b) &&
1759 (get_opt_unreachable_code())) {
1760 /* It's a boolean Cond, branching on a boolean constant.
1761 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1762 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1763 turn_into_tuple(n, 2);
1764 if (ta == tarval_b_true) {
1765 set_Tuple_pred(n, pn_Cond_false, new_Bad());
1766 set_Tuple_pred(n, pn_Cond_true, jmp);
1768 set_Tuple_pred(n, pn_Cond_false, jmp);
1769 set_Tuple_pred(n, pn_Cond_true, new_Bad());
1771 /* We might generate an endless loop, so keep it alive. */
1772 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1773 } else if ((ta != tarval_bad) &&
1774 (get_irn_mode(a) == mode_Iu) &&
1775 (get_Cond_kind(n) == dense) &&
1776 (get_opt_unreachable_code())) {
1777 /* I don't want to allow Tuples smaller than the biggest Proj.
1778 Also this tuple might get really big...
1779 I generate the Jmp here, and remember it in link. Link is used
1780 when optimizing Proj. */
1781 set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_block(n)));
1782 /* We might generate an endless loop, so keep it alive. */
1783 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1784 } else if ((get_irn_op(a) == op_Eor)
1785 && (get_irn_mode(a) == mode_b)
1786 && (classify_tarval(value_of(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1787 /* The Eor is a negate. Generate a new Cond without the negate,
1788 simulate the negate by exchanging the results. */
1789 set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1791 } else if ((get_irn_op(a) == op_Not)
1792 && (get_irn_mode(a) == mode_b)) {
1793 /* A Not before the Cond. Generate a new Cond without the Not,
1794 simulate the Not by exchanging the results. */
1795 set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1804 static ir_node *transform_node_Eor(ir_node *n)
1807 ir_node *a = get_Eor_left(n);
1808 ir_node *b = get_Eor_right(n);
1809 ir_mode *mode = get_irn_mode(n);
1813 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
1814 mode, get_mode_null(mode));
1815 DBG_OPT_ALGSIM0(oldn, n);
1817 else if ((mode == mode_b)
1818 && (get_irn_op(a) == op_Proj)
1819 && (get_irn_mode(a) == mode_b)
1820 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1821 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1822 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1823 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1824 mode_b, get_negated_pnc(get_Proj_proj(a)));
1826 DBG_OPT_ALGSIM0(oldn, n);
1828 else if ((mode == mode_b)
1829 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
1830 /* The Eor is a Not. Replace it by a Not. */
1831 /* ????!!!Extend to bitfield 1111111. */
1832 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode_b);
1834 DBG_OPT_ALGSIM0(oldn, n);
1841 * Transform a boolean Not.
1843 static ir_node *transform_node_Not(ir_node *n)
1846 ir_node *a = get_Not_op(n);
1848 if ( (get_irn_mode(n) == mode_b)
1849 && (get_irn_op(a) == op_Proj)
1850 && (get_irn_mode(a) == mode_b)
1851 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1852 /* We negate a Cmp. The Cmp has the negated result anyways! */
1853 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1854 mode_b, get_negated_pnc(get_Proj_proj(a)));
1855 DBG_OPT_ALGSIM0(oldn, n);
1862 * Transform a Cast of a Const into a new Const
1864 static ir_node *transform_node_Cast(ir_node *n) {
1866 ir_node *pred = get_Cast_op(n);
1867 type *tp = get_irn_type(n);
1869 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1870 n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
1871 get_Const_tarval(pred), tp);
1872 DBG_OPT_CSTEVAL(oldn, n);
1873 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1874 n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
1875 get_SymConst_kind(pred), tp);
1876 DBG_OPT_CSTEVAL(oldn, n);
1883 * Transform a Proj(Div) with a non-zero constant.
1884 * Removes the exceptions and routes the memory to the NoMem node.
1886 static ir_node *transform_node_Proj_Div(ir_node *proj)
1888 ir_node *n = get_Proj_pred(proj);
1893 b = get_Div_right(n);
1896 if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1897 /* div(x, c) && c != 0 */
1898 proj_nr = get_Proj_proj(proj);
1900 /* this node may float */
1901 set_irn_pinned(n, op_pin_state_floats);
1903 if (proj_nr == pn_Div_X_except) {
1904 /* we found an exception handler, remove it */
1907 /* the memory Proj can be removed */
1908 ir_node *res = get_Div_mem(n);
1909 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1910 if (proj_nr == pn_Div_M)
1918 * Transform a Proj(Mod) with a non-zero constant.
1919 * Removes the exceptions and routes the memory to the NoMem node.
1921 static ir_node *transform_node_Proj_Mod(ir_node *proj)
1923 ir_node *n = get_Proj_pred(proj);
1928 b = get_Mod_right(n);
1931 if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1932 /* mod(x, c) && c != 0 */
1933 proj_nr = get_Proj_proj(proj);
1935 /* this node may float */
1936 set_irn_pinned(n, op_pin_state_floats);
1938 if (proj_nr == pn_Mod_X_except) {
1939 /* we found an exception handler, remove it */
1942 /* the memory Proj can be removed */
1943 ir_node *res = get_Mod_mem(n);
1944 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1945 if (proj_nr == pn_Mod_M)
1953 * Transform a Proj(DivMod) with a non-zero constant.
1954 * Removes the exceptions and routes the memory to the NoMem node.
1956 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
1958 ir_node *n = get_Proj_pred(proj);
1963 b = get_DivMod_right(n);
1966 if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1967 /* DivMod(x, c) && c != 0 */
1968 proj_nr = get_Proj_proj(proj);
1970 /* this node may float */
1971 set_irn_pinned(n, op_pin_state_floats);
1973 if (proj_nr == pn_DivMod_X_except) {
1974 /* we found an exception handler, remove it */
1978 /* the memory Proj can be removed */
1979 ir_node *res = get_DivMod_mem(n);
1980 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1981 if (proj_nr == pn_DivMod_M)
1989 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
1991 static ir_node *transform_node_Proj_Cond(ir_node *proj)
1993 if (get_opt_unreachable_code()) {
1994 ir_node *n = get_Proj_pred(proj);
1995 ir_node *b = get_Cond_selector(n);
1996 tarval *tb = value_of(b);
1998 if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
1999 /* we have a constant switch */
2000 long num = get_Proj_proj(proj);
2002 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2003 if (get_tarval_long(tb) == num) {
2004 /* Do NOT create a jump here, or we will have 2 control flow ops
2005 * in a block. This case is optimized away in optimize_cf(). */
2009 /* this case will NEVER be taken, kill it */
2019 * Normalizes and optimizes Cmp nodes.
2021 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2023 if (get_opt_reassociation()) {
2024 ir_node *n = get_Proj_pred(proj);
2025 ir_node *left = get_Cmp_left(n);
2026 ir_node *right = get_Cmp_right(n);
2030 ir_mode *mode = NULL;
2031 long proj_nr = get_Proj_proj(proj);
2034 * First step: normalize the compare op
2035 * by placing the constant on the right site
2036 * or moving the lower address node to the left.
2037 * We ignore the case that both are constants
2038 * this case should be optimized away.
2040 if (get_irn_op(right) == op_Const)
2042 else if (get_irn_op(left) == op_Const) {
2047 proj_nr = get_inversed_pnc(proj_nr);
2050 else if (left > right) {
2056 proj_nr = get_inversed_pnc(proj_nr);
2061 * Second step: Try to reduce the magnitude
2062 * of a constant. This may help to generate better code
2063 * later and may help to normalize more compares.
2064 * Of course this is only possible for integer values.
2067 mode = get_irn_mode(c);
2068 tv = get_Const_tarval(c);
2070 if (tv != tarval_bad) {
2071 /* the following optimization is possible on modes without Overflow
2072 * on Unary Minus or on == and !=:
2073 * -a CMP c ==> a swap(CMP) -c
2075 * Beware: for two-complement Overflow may occur, so only == and != can
2076 * be optimized, see this:
2077 * -MININT < 0 =/=> MININT > 0 !!!
2079 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2080 (!mode_overflow_on_unary_Minus(mode) ||
2081 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2082 left = get_Minus_op(left);
2083 tv = tarval_sub(get_mode_null(mode), tv);
2085 proj_nr = get_inversed_pnc(proj_nr);
2089 /* for integer modes, we have more */
2090 if (mode_is_int(mode)) {
2091 /* Ne includes Unordered which is not possible on integers.
2092 * However, frontends often use this wrong, so fix it here */
2093 if (proj_nr == pn_Cmp_Ne) {
2094 proj_nr = pn_Cmp_Lg;
2095 set_Proj_proj(proj, proj_nr);
2098 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2099 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2100 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2101 tv = tarval_sub(tv, get_mode_one(mode));
2103 proj_nr ^= pn_Cmp_Eq;
2106 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2107 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2108 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2109 tv = tarval_add(tv, get_mode_one(mode));
2111 proj_nr ^= pn_Cmp_Eq;
2115 /* the following reassociations work only for == and != */
2116 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2118 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2119 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2120 right = get_Sub_right(left);
2121 left = get_Sub_left(left);
2123 tv = value_of(right);
2127 if (tv != tarval_bad) {
2128 ir_op *op = get_irn_op(left);
2130 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2132 ir_node *c1 = get_Sub_right(left);
2133 tarval *tv2 = value_of(c1);
2135 if (tv2 != tarval_bad) {
2136 tv2 = tarval_add(tv, value_of(c1));
2138 if (tv2 != tarval_bad) {
2139 left = get_Sub_left(left);
2145 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2146 else if (op == op_Add) {
2147 ir_node *a_l = get_Add_left(left);
2148 ir_node *a_r = get_Add_right(left);
2152 if (get_irn_op(a_l) == op_Const) {
2154 tv2 = value_of(a_l);
2158 tv2 = value_of(a_r);
2161 if (tv2 != tarval_bad) {
2162 tv2 = tarval_sub(tv, tv2);
2164 if (tv2 != tarval_bad) {
2171 /* -a == c ==> a == -c, -a != c ==> a != -c */
2172 else if (op == op_Minus) {
2173 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2175 if (tv2 != tarval_bad) {
2176 left = get_Minus_op(left);
2183 /* the following reassociations work only for <= */
2184 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2185 if (tv != tarval_bad) {
2186 ir_op *op = get_irn_op(left);
2188 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2198 ir_node *block = get_nodes_block(n);
2200 if (changed & 2) /* need a new Const */
2201 right = new_Const(mode, tv);
2203 /* create a new compare */
2204 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2207 set_Proj_pred(proj, n);
2208 set_Proj_proj(proj, proj_nr);
2215 * Does all optimizations on nodes that must be done on it's Proj's
2216 * because of creating new nodes.
2218 static ir_node *transform_node_Proj(ir_node *proj)
2220 ir_node *n = get_Proj_pred(proj);
2222 switch (get_irn_opcode(n)) {
2224 return transform_node_Proj_Div(proj);
2227 return transform_node_Proj_Mod(proj);
2230 return transform_node_Proj_DivMod(proj);
2233 return transform_node_Proj_Cond(proj);
2236 return transform_node_Proj_Cmp(proj);
2239 /* should not happen, but if it does will be optimized away */
2240 return equivalent_node_Proj(proj);
2249 * returns the operands of a commutative bin-op, if one operand is
2250 * a const, it is returned as the second one.
2252 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2254 ir_node *op_a = get_binop_left(binop);
2255 ir_node *op_b = get_binop_right(binop);
2257 assert(is_op_commutative(get_irn_op(binop)));
2259 if (get_irn_op(op_a) == op_Const) {
2270 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2271 * Such pattern may arise in bitfield stores.
2273 * value c4 value c4 & c2
2274 * AND c3 AND c1 | c3
2279 static ir_node *transform_node_Or_bf_store(ir_node *or)
2283 ir_node *and_l, *c3;
2284 ir_node *value, *c4;
2285 ir_node *new_and, *new_const, *block;
2286 ir_mode *mode = get_irn_mode(or);
2288 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2290 get_comm_Binop_Ops(or, &and, &c1);
2291 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2294 get_comm_Binop_Ops(and, &or_l, &c2);
2295 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2298 get_comm_Binop_Ops(or_l, &and_l, &c3);
2299 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2302 get_comm_Binop_Ops(and_l, &value, &c4);
2303 if (get_irn_op(c4) != op_Const)
2306 /* ok, found the pattern, check for conditions */
2307 assert(mode == get_irn_mode(and));
2308 assert(mode == get_irn_mode(or_l));
2309 assert(mode == get_irn_mode(and_l));
2311 tv1 = get_Const_tarval(c1);
2312 tv2 = get_Const_tarval(c2);
2313 tv3 = get_Const_tarval(c3);
2314 tv4 = get_Const_tarval(c4);
2316 tv = tarval_or(tv4, tv2);
2317 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2318 /* have at least one 0 at the same bit position */
2322 n_tv4 = tarval_not(tv4);
2323 if (tv3 != tarval_and(tv3, n_tv4)) {
2324 /* bit in the or_mask is outside the and_mask */
2328 n_tv2 = tarval_not(tv2);
2329 if (tv1 != tarval_and(tv1, n_tv2)) {
2330 /* bit in the or_mask is outside the and_mask */
2334 /* ok, all conditions met */
2335 block = get_nodes_block(or);
2337 new_and = new_r_And(current_ir_graph, block,
2338 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2340 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2342 set_Or_left(or, new_and);
2343 set_Or_right(or, new_const);
2345 /* check for more */
2346 return transform_node_Or_bf_store(or);
2350 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2352 static ir_node *transform_node_Or_Rot(ir_node *or)
2354 ir_mode *mode = get_irn_mode(or);
2355 ir_node *shl, *shr, *block;
2356 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2359 if (! mode_is_int(mode))
2362 shl = get_binop_left(or);
2363 shr = get_binop_right(or);
2365 if (get_irn_op(shl) == op_Shr) {
2366 if (get_irn_op(shr) != op_Shl)
2373 else if (get_irn_op(shl) != op_Shl)
2375 else if (get_irn_op(shr) != op_Shr)
2378 x = get_Shl_left(shl);
2379 if (x != get_Shr_left(shr))
2382 c1 = get_Shl_right(shl);
2383 c2 = get_Shr_right(shr);
2384 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2385 tv1 = get_Const_tarval(c1);
2386 if (! tarval_is_long(tv1))
2389 tv2 = get_Const_tarval(c2);
2390 if (! tarval_is_long(tv2))
2393 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2394 != get_mode_size_bits(mode))
2397 /* yet, condition met */
2398 block = get_nodes_block(or);
2400 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2402 DBG_OPT_ALGSIM1(or, shl, shr, n);
2405 else if (get_irn_op(c1) == op_Sub) {
2409 if (get_Sub_right(sub) != v)
2412 c1 = get_Sub_left(sub);
2413 if (get_irn_op(c1) != op_Const)
2416 tv1 = get_Const_tarval(c1);
2417 if (! tarval_is_long(tv1))
2420 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2423 /* yet, condition met */
2424 block = get_nodes_block(or);
2426 /* a Rot right is not supported, so use a rot left */
2427 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2429 DBG_OPT_ALGSIM0(or, n);
2432 else if (get_irn_op(c2) == op_Sub) {
2436 c1 = get_Sub_left(sub);
2437 if (get_irn_op(c1) != op_Const)
2440 tv1 = get_Const_tarval(c1);
2441 if (! tarval_is_long(tv1))
2444 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2447 /* yet, condition met */
2448 block = get_nodes_block(or);
2451 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2453 DBG_OPT_ALGSIM0(or, n);
2463 static ir_node *transform_node_Or(ir_node *or)
2465 or = transform_node_Or_bf_store(or);
2466 or = transform_node_Or_Rot(or);
2472 static ir_node *transform_node(ir_node *n);
2475 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
2477 static ir_node *transform_node_shift(ir_node *n)
2479 ir_node *left, *right;
2480 tarval *tv1, *tv2, *res;
2482 int modulo_shf, flag;
2484 left = get_binop_left(n);
2486 /* different operations */
2487 if (get_irn_op(left) != get_irn_op(n))
2490 right = get_binop_right(n);
2491 tv1 = value_of(right);
2492 if (tv1 == tarval_bad)
2495 tv2 = value_of(get_binop_right(left));
2496 if (tv2 == tarval_bad)
2499 res = tarval_add(tv1, tv2);
2501 /* beware: a simple replacement works only, if res < modulo shift */
2502 mode = get_irn_mode(n);
2506 modulo_shf = get_mode_modulo_shift(mode);
2507 if (modulo_shf > 0) {
2508 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2510 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2517 /* ok, we can replace it */
2518 ir_node *in[2], *irn, *block = get_nodes_block(n);
2520 in[0] = get_binop_left(left);
2521 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2523 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2525 DBG_OPT_ALGSIM0(n, irn);
2527 return transform_node(irn);
2532 #define transform_node_Shr transform_node_shift
2533 #define transform_node_Shrs transform_node_shift
2534 #define transform_node_Shl transform_node_shift
2537 * Remove dead blocks in keepalive list. We do not generate a new End node.
2539 static ir_node *transform_node_End(ir_node *n) {
2540 int i, n_keepalives = get_End_n_keepalives(n);
2542 for (i = 0; i < n_keepalives; ++i) {
2543 ir_node *ka = get_End_keepalive(n, i);
2544 if (is_Block(ka) && is_Block_dead(ka))
2545 set_End_keepalive(n, i, new_Bad());
2551 * Optimize a Mux into some simplier cases.
2553 static ir_node *transform_node_Mux(ir_node *n)
2555 ir_node *oldn = n, *sel = get_Mux_sel(n);
2556 ir_mode *mode = get_irn_mode(n);
2558 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2559 ir_node *cmp = get_Proj_pred(sel);
2560 long proj_nr = get_Proj_proj(sel);
2561 ir_node *f = get_Mux_false(n);
2562 ir_node *t = get_Mux_true(n);
2564 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2565 ir_node *block = get_nodes_block(n);
2568 * Note: normalization puts the constant on the right site,
2569 * so we check only one case.
2571 * Note further that these optimization work even for floating point
2572 * with NaN's because -NaN == NaN.
2573 * However, if +0 and -0 is handled differently, we cannot use the first one.
2575 if (get_irn_op(f) == op_Minus &&
2576 get_Minus_op(f) == t &&
2577 get_Cmp_left(cmp) == t) {
2579 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2580 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2581 n = new_rd_Abs(get_irn_dbg_info(n),
2585 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2588 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2589 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2590 n = new_rd_Abs(get_irn_dbg_info(n),
2594 n = new_rd_Minus(get_irn_dbg_info(n),
2599 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2603 else if (get_irn_op(t) == op_Minus &&
2604 get_Minus_op(t) == f &&
2605 get_Cmp_left(cmp) == f) {
2607 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2608 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2609 n = new_rd_Abs(get_irn_dbg_info(n),
2613 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2616 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2617 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
2618 n = new_rd_Abs(get_irn_dbg_info(n),
2622 n = new_rd_Minus(get_irn_dbg_info(n),
2627 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2632 if (mode_is_int(mode) && mode_is_signed(mode) &&
2633 get_mode_arithmetic(mode) == irma_twos_complement) {
2634 ir_node *x = get_Cmp_left(cmp);
2636 /* the following optimization works only with signed integer two-complement mode */
2638 if (mode == get_irn_mode(x)) {
2640 * FIXME: this restriction is two rigid, as it would still
2641 * work if mode(x) = Hs and mode == Is, but at least it removes
2644 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2645 classify_Const(t) == CNST_ALL_ONE &&
2646 classify_Const(f) == CNST_NULL) {
2648 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2652 n = new_rd_Shrs(get_irn_dbg_info(n),
2653 current_ir_graph, block, x,
2654 new_r_Const_long(current_ir_graph, block, mode_Iu,
2655 get_mode_size_bits(mode) - 1),
2657 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2660 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2661 classify_Const(t) == CNST_ONE &&
2662 classify_Const(f) == CNST_NULL) {
2664 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2668 n = new_rd_Shr(get_irn_dbg_info(n),
2669 current_ir_graph, block,
2670 new_r_Minus(current_ir_graph, block, x, mode),
2671 new_r_Const_long(current_ir_graph, block, mode_Iu,
2672 get_mode_size_bits(mode) - 1),
2674 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2681 return arch_transform_node_Mux(n);
2685 * Tries several [inplace] [optimizing] transformations and returns an
2686 * equivalent node. The difference to equivalent_node() is that these
2687 * transformations _do_ generate new nodes, and thus the old node must
2688 * not be freed even if the equivalent node isn't the old one.
2690 static ir_node *transform_node(ir_node *n)
2692 if (n->op->transform_node)
2693 n = n->op->transform_node(n);
2698 * set the default transform node operation
2700 static ir_op *firm_set_default_transform_node(ir_op *op)
2704 op->transform_node = transform_node_##a; \
2728 op->transform_node = NULL;
2736 /* **************** Common Subexpression Elimination **************** */
2738 /** The size of the hash table used, should estimate the number of nodes
2740 #define N_IR_NODES 512
2742 /** Compares the attributes of two Const nodes. */
2743 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
2745 return (get_Const_tarval(a) != get_Const_tarval(b))
2746 || (get_Const_type(a) != get_Const_type(b));
2749 /** Compares the attributes of two Proj nodes. */
2750 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
2752 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
2755 /** Compares the attributes of two Filter nodes. */
2756 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
2758 return get_Filter_proj(a) != get_Filter_proj(b);
2761 /** Compares the attributes of two Alloc nodes. */
2762 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
2764 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
2765 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
2768 /** Compares the attributes of two Free nodes. */
2769 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
2771 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
2772 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
2775 /** Compares the attributes of two SymConst nodes. */
2776 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
2778 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
2779 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
2780 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
2783 /** Compares the attributes of two Call nodes. */
2784 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
2786 return (get_irn_call_attr(a) != get_irn_call_attr(b));
2789 /** Compares the attributes of two Sel nodes. */
2790 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
2792 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
2793 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
2794 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
2795 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
2796 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
2799 /** Compares the attributes of two Phi nodes. */
2800 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
2802 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
2805 /** Compares the attributes of two Cast nodes. */
2806 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
2808 return get_Cast_type(a) != get_Cast_type(b);
2811 /** Compares the attributes of two Load nodes. */
2812 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
2814 if (get_Load_volatility(a) == volatility_is_volatile ||
2815 get_Load_volatility(b) == volatility_is_volatile)
2816 /* NEVER do CSE on volatile Loads */
2819 return get_Load_mode(a) != get_Load_mode(b);
2822 /** Compares the attributes of two Store nodes. */
2823 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
2825 /* NEVER do CSE on volatile Stores */
2826 return (get_Store_volatility(a) == volatility_is_volatile ||
2827 get_Store_volatility(b) == volatility_is_volatile);
2831 * set the default node attribute compare operation
2833 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
2837 op->node_cmp_attr = node_cmp_attr_##a; \
2854 op->node_cmp_attr = NULL;
2862 * Compare function for two nodes in the hash table. Gets two
2863 * nodes as parameters. Returns 0 if the nodes are a cse.
2866 vt_cmp (const void *elt, const void *key)
2874 if (a == b) return 0;
2876 if ((get_irn_op(a) != get_irn_op(b)) ||
2877 (get_irn_mode(a) != get_irn_mode(b))) return 1;
2879 /* compare if a's in and b's in are of equal length */
2880 irn_arity_a = get_irn_intra_arity (a);
2881 if (irn_arity_a != get_irn_intra_arity(b))
2884 /* for block-local cse and op_pin_state_pinned nodes: */
2885 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
2886 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
2890 /* compare a->in[0..ins] with b->in[0..ins] */
2891 for (i = 0; i < irn_arity_a; i++)
2892 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
2896 * here, we already now that the nodes are identical except their
2899 if (a->op->node_cmp_attr)
2900 return a->op->node_cmp_attr(a, b);
2906 * Calculate a hash value of a node.
2909 ir_node_hash (ir_node *node)
2914 if (node->op == op_Const) {
2915 /* special value for const, as they only differ in their tarval. */
2916 h = HASH_PTR(node->attr.con.tv);
2917 h = 9*h + HASH_PTR(get_irn_mode(node));
2918 } else if (node->op == op_SymConst) {
2919 /* special value for const, as they only differ in their symbol. */
2920 h = HASH_PTR(node->attr.i.sym.type_p);
2921 h = 9*h + HASH_PTR(get_irn_mode(node));
2924 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
2925 h = irn_arity = get_irn_intra_arity(node);
2927 /* consider all in nodes... except the block if not a control flow. */
2928 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
2929 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
2933 h = 9*h + HASH_PTR(get_irn_mode(node));
2935 h = 9*h + HASH_PTR(get_irn_op(node));
2942 new_identities(void) {
2943 return new_pset(vt_cmp, N_IR_NODES);
2947 del_identities(pset *value_table) {
2948 del_pset(value_table);
2952 * Return the canonical node computing the same value as n.
2953 * Looks up the node in a hash table.
2955 * For Const nodes this is performed in the constructor, too. Const
2956 * nodes are extremely time critical because of their frequent use in
2957 * constant string arrays.
2959 static INLINE ir_node *
2960 identify (pset *value_table, ir_node *n)
2964 if (!value_table) return n;
2966 if (get_opt_reassociation()) {
2967 if (is_op_commutative(get_irn_op(n))) {
2968 ir_node *l = get_binop_left(n);
2969 ir_node *r = get_binop_right(n);
2971 /* for commutative operators perform a OP b == b OP a */
2973 set_binop_left(n, r);
2974 set_binop_right(n, l);
2979 o = pset_find (value_table, n, ir_node_hash (n));
2988 * During construction we set the op_pin_state_pinned flag in the graph right when the
2989 * optimization is performed. The flag turning on procedure global cse could
2990 * be changed between two allocations. This way we are safe.
2992 static INLINE ir_node *
2993 identify_cons (pset *value_table, ir_node *n) {
2996 n = identify(value_table, n);
2997 if (get_irn_n(old, -1) != get_irn_n(n, -1))
2998 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3003 * Return the canonical node computing the same value as n.
3004 * Looks up the node in a hash table, enters it in the table
3005 * if it isn't there yet.
3008 identify_remember (pset *value_table, ir_node *n)
3012 if (!value_table) return n;
3014 if (get_opt_reassociation()) {
3015 if (is_op_commutative(get_irn_op(n))) {
3016 ir_node *l = get_binop_left(n);
3017 ir_node *r = get_binop_right(n);
3019 /* for commutative operators perform a OP b == b OP a */
3021 set_binop_left(n, r);
3022 set_binop_right(n, l);
3027 /* lookup or insert in hash table with given hash key. */
3028 o = pset_insert (value_table, n, ir_node_hash (n));
3038 add_identities (pset *value_table, ir_node *node) {
3039 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3040 identify_remember (value_table, node);
3044 * garbage in, garbage out. If a node has a dead input, i.e., the
3045 * Bad node is input to the node, return the Bad node.
3047 static INLINE ir_node *
3048 gigo (ir_node *node)
3051 ir_op* op = get_irn_op(node);
3053 /* remove garbage blocks by looking at control flow that leaves the block
3054 and replacing the control flow by Bad. */
3055 if (get_irn_mode(node) == mode_X) {
3056 ir_node *block = get_nodes_block(node);
3057 if (!get_Block_matured(block)) return node; /* Don't optimize nodes in immature blocks. */
3058 if (op == op_End) return node; /* Don't optimize End, may have Bads. */
3060 if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
3061 irn_arity = get_irn_arity(block);
3062 for (i = 0; i < irn_arity; i++) {
3063 if (!is_Bad(get_irn_n(block, i))) break;
3065 if (i == irn_arity) return new_Bad();
3069 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3070 blocks predecessors is dead. */
3071 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3072 irn_arity = get_irn_arity(node);
3074 if (is_Block_dead(get_nodes_block(node)))
3077 for (i = 0; i < irn_arity; i++) {
3078 if (is_Bad(get_irn_n(node, i))) {
3084 /* With this code we violate the agreement that local_optimize
3085 only leaves Bads in Block, Phi and Tuple nodes. */
3086 /* If Block has only Bads as predecessors it's garbage. */
3087 /* If Phi has only Bads as predecessors it's garbage. */
3088 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3089 irn_arity = get_irn_arity(node);
3090 for (i = 0; i < irn_arity; i++) {
3091 if (!is_Bad(get_irn_n(node, i))) break;
3093 if (i == irn_arity) node = new_Bad();
3101 * These optimizations deallocate nodes from the obstack.
3102 * It can only be called if it is guaranteed that no other nodes
3103 * reference this one, i.e., right after construction of a node.
3106 optimize_node (ir_node *n)
3110 opcode iro = get_irn_opcode(n);
3112 type *old_tp = get_irn_type(n);
3114 int i, arity = get_irn_arity(n);
3115 for (i = 0; i < arity && !old_tp; ++i)
3116 old_tp = get_irn_type(get_irn_n(n, i));
3119 /* Always optimize Phi nodes: part of the construction. */
3120 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3122 /* constant expression evaluation / constant folding */
3123 if (get_opt_constant_folding()) {
3124 /* constants can not be evaluated */
3125 if (iro != iro_Const) {
3126 /* try to evaluate */
3127 tv = computed_value(n);
3128 if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
3132 * we MUST copy the node here temporary, because it's still needed
3133 * for DBG_OPT_CSTEVAL
3135 int node_size = offsetof(ir_node, attr) + n->op->attr_size;
3136 oldn = alloca(node_size);
3138 memcpy(oldn, n, node_size);
3139 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3141 /* ARG, copy the in array, we need it for statistics */
3142 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3145 edges_node_deleted(n, current_ir_graph);
3147 /* evaluation was successful -- replace the node. */
3148 obstack_free (current_ir_graph->obst, n);
3149 nw = new_Const (get_tarval_mode (tv), tv);
3151 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3152 set_Const_type(nw, old_tp);
3153 DBG_OPT_CSTEVAL(oldn, nw);
3159 /* remove unnecessary nodes */
3160 if (get_opt_constant_folding() ||
3161 (iro == iro_Phi) || /* always optimize these nodes. */
3163 (iro == iro_Proj) ||
3164 (iro == iro_Block) ) /* Flags tested local. */
3165 n = equivalent_node (n);
3167 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3169 /** common subexpression elimination **/
3170 /* Checks whether n is already available. */
3171 /* The block input is used to distinguish different subexpressions. Right
3172 now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3173 subexpressions within a block. */
3175 n = identify_cons (current_ir_graph->value_table, n);
3178 edges_node_deleted(oldn, current_ir_graph);
3180 /* We found an existing, better node, so we can deallocate the old node. */
3181 obstack_free (current_ir_graph->obst, oldn);
3186 /* Some more constant expression evaluation that does not allow to
3188 iro = get_irn_opcode(n);
3189 if (get_opt_constant_folding() ||
3190 (iro == iro_Cond) ||
3191 (iro == iro_Proj) ||
3192 (iro == iro_Sel)) /* Flags tested local. */
3193 n = transform_node (n);
3195 /* Remove nodes with dead (Bad) input.
3196 Run always for transformation induced Bads. */
3199 /* Now we have a legal, useful node. Enter it in hash table for cse */
3200 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3201 n = identify_remember (current_ir_graph->value_table, n);
3209 * These optimizations never deallocate nodes (in place). This can cause dead
3210 * nodes lying on the obstack. Remove these by a dead node elimination,
3211 * i.e., a copying garbage collection.
3214 optimize_in_place_2 (ir_node *n)
3218 opcode iro = get_irn_opcode(n);
3220 type *old_tp = get_irn_type(n);
3222 int i, arity = get_irn_arity(n);
3223 for (i = 0; i < arity && !old_tp; ++i)
3224 old_tp = get_irn_type(get_irn_n(n, i));
3227 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3229 /* if not optimize return n */
3232 /* Here this is possible. Why? */
3236 /* constant expression evaluation / constant folding */
3237 if (get_opt_constant_folding()) {
3238 /* constants can not be evaluated */
3239 if (iro != iro_Const) {
3240 /* try to evaluate */
3241 tv = computed_value(n);
3242 if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
3243 /* evaluation was successful -- replace the node. */
3244 n = new_Const (get_tarval_mode (tv), tv);
3246 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3247 set_Const_type(n, old_tp);
3249 DBG_OPT_CSTEVAL(oldn, n);
3255 /* remove unnecessary nodes */
3256 if (get_opt_constant_folding() ||
3257 (iro == iro_Phi) || /* always optimize these nodes. */
3258 (iro == iro_Id) || /* ... */
3259 (iro == iro_Proj) || /* ... */
3260 (iro == iro_Block) ) /* Flags tested local. */
3261 n = equivalent_node (n);
3263 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3265 /** common subexpression elimination **/
3266 /* Checks whether n is already available. */
3267 /* The block input is used to distinguish different subexpressions. Right
3268 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3269 subexpressions within a block. */
3270 if (get_opt_cse()) {
3271 n = identify (current_ir_graph->value_table, n);
3274 /* Some more constant expression evaluation. */
3275 iro = get_irn_opcode(n);
3276 if (get_opt_constant_folding() ||
3277 (iro == iro_Cond) ||
3278 (iro == iro_Proj) ||
3279 (iro == iro_Sel)) /* Flags tested local. */
3280 n = transform_node (n);
3282 /* Remove nodes with dead (Bad) input.
3283 Run always for transformation induced Bads. */
3286 /* Now we can verify the node, as it has no dead inputs any more. */
3289 /* Now we have a legal, useful node. Enter it in hash table for cse.
3290 Blocks should be unique anyways. (Except the successor of start:
3291 is cse with the start block!) */
3292 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3293 n = identify_remember (current_ir_graph->value_table, n);
3299 * Wrapper for external use, set proper status bits after optimization.
3302 optimize_in_place (ir_node *n)
3304 /* Handle graph state */
3305 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3307 if (get_opt_global_cse())
3308 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3309 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3310 set_irg_outs_inconsistent(current_ir_graph);
3312 /* Maybe we could also test whether optimizing the node can
3313 change the control graph. */
3314 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
3315 set_irg_dom_inconsistent(current_ir_graph);
3316 return optimize_in_place_2 (n);
3320 * set the default ir op operations
3322 ir_op *firm_set_default_operations(ir_op *op)
3324 op = firm_set_default_computed_value(op);
3325 op = firm_set_default_equivalent_node(op);
3326 op = firm_set_default_transform_node(op);
3327 op = firm_set_default_node_cmp_attr(op);
3328 op = firm_set_default_get_type(op);