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(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(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_nodes_block(get_Block_cfgpred(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(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 @@@ we could do this also with two loops finding two preds from several ones. */
714 ir_node *a = get_Block_cfgpred(n, 0);
715 ir_node *b = get_Block_cfgpred(n, 1);
717 if ((get_irn_op(a) == op_Proj) &&
718 (get_irn_op(b) == op_Proj) &&
719 (get_Proj_pred(a) == get_Proj_pred(b)) &&
720 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
721 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
722 /* Also a single entry Block following a single exit Block. Phis have
723 twice the same operand and will be optimized away. */
724 n = get_nodes_block(a);
725 DBG_OPT_IFSIM(oldn, a, b, n);
727 } else if (get_opt_unreachable_code() &&
728 (n != current_ir_graph->start_block) &&
729 (n != current_ir_graph->end_block) ) {
730 int i, n_cfg = get_Block_n_cfgpreds(n);
732 /* If all inputs are dead, this block is dead too, except if it is
733 the start or end block. This is a step of unreachable code
735 for (i = 0; i < n_cfg; i++) {
736 ir_node *pred = get_Block_cfgpred(n, i);
739 if (is_Bad(pred)) continue;
740 pred_blk = get_nodes_block(pred);
742 if (is_Block_dead(pred_blk)) continue;
745 /* really found a living input */
750 n = set_Block_dead(n);
757 * Returns a equivalent node for a Jmp, a Bad :-)
758 * Of course this only happens if the Block of the Jmp is Bad.
760 static ir_node *equivalent_node_Jmp(ir_node *n)
762 /* GL: Why not same for op_Raise?? */
763 /* unreachable code elimination */
764 if (is_Block_dead(get_nodes_block(n)))
770 static ir_node *equivalent_node_Cond(ir_node *n)
772 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
773 See cases for iro_Cond and iro_Proj in transform_node(). */
778 * optimize operations that are commutative and have neutral 0,
779 * so a op 0 = 0 op a = a.
781 static ir_node *equivalent_node_neutral_zero(ir_node *n)
785 ir_node *a = get_binop_left(n);
786 ir_node *b = get_binop_right(n);
791 /* After running compute_node there is only one constant predecessor.
792 Find this predecessors value and remember the other node: */
793 if ((tv = value_of(a)) != tarval_bad) {
795 } else if ((tv = value_of(b)) != tarval_bad) {
800 /* If this predecessors constant value is zero, the operation is
801 unnecessary. Remove it: */
802 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
805 DBG_OPT_ALGSIM1(oldn, a, b, n);
811 #define equivalent_node_Add equivalent_node_neutral_zero
812 #define equivalent_node_Eor equivalent_node_neutral_zero
815 * optimize operations that are not commutative but have neutral 0 on left,
818 static ir_node *equivalent_node_left_zero(ir_node *n)
822 ir_node *a = get_binop_left(n);
823 ir_node *b = get_binop_right(n);
825 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
828 DBG_OPT_ALGSIM1(oldn, a, b, n);
834 #define equivalent_node_Sub equivalent_node_left_zero
835 #define equivalent_node_Shl equivalent_node_left_zero
836 #define equivalent_node_Shr equivalent_node_left_zero
837 #define equivalent_node_Shrs equivalent_node_left_zero
838 #define equivalent_node_Rot equivalent_node_left_zero
841 * Optimize an "idempotent unary op", ie op(op(n)) = n.
843 * @fixme -(-a) == a, but might overflow two times.
844 * We handle it anyway here but the better way would be a
845 * flag. This would be needed for Pascal for instance.
847 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
850 ir_node *pred = get_unop_op(n);
852 /* optimize symmetric unop */
853 if (get_irn_op(pred) == get_irn_op(n)) {
854 n = get_unop_op(pred);
855 DBG_OPT_ALGSIM2(oldn, pred, n);
860 /* Not(Not(x)) == x */
861 #define equivalent_node_Not equivalent_node_idempotent_unop
863 /* --x == x */ /* ??? Is this possible or can --x raise an
864 out of bounds exception if min =! max? */
865 #define equivalent_node_Minus equivalent_node_idempotent_unop
868 * Optimize a * 1 = 1 * a = a.
870 static ir_node *equivalent_node_Mul(ir_node *n)
874 ir_node *a = get_Mul_left(n);
875 ir_node *b = get_Mul_right(n);
877 /* Mul is commutative and has again an other neutral element. */
878 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
880 DBG_OPT_ALGSIM1(oldn, a, b, n);
881 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
883 DBG_OPT_ALGSIM1(oldn, a, b, n);
889 * Optimize a / 1 = a.
891 static ir_node *equivalent_node_Div(ir_node *n)
893 ir_node *a = get_Div_left(n);
894 ir_node *b = get_Div_right(n);
896 /* Div is not commutative. */
897 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
898 /* Turn Div into a tuple (mem, bad, a) */
899 ir_node *mem = get_Div_mem(n);
900 turn_into_tuple(n, 3);
901 set_Tuple_pred(n, pn_Div_M, mem);
902 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
903 set_Tuple_pred(n, pn_Div_res, a);
909 * Optimize a / 1 = a.
911 static ir_node *equivalent_node_DivMod(ir_node *n)
913 ir_node *a = get_DivMod_left(n);
914 ir_node *b = get_DivMod_right(n);
916 /* Div is not commutative. */
917 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
918 /* Turn DivMod into a tuple (mem, bad, a, 0) */
919 ir_node *mem = get_Div_mem(n);
920 ir_mode *mode = get_irn_mode(b);
922 turn_into_tuple(n, 4);
923 set_Tuple_pred(n, pn_DivMod_M, mem);
924 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
925 set_Tuple_pred(n, pn_DivMod_res_div, a);
926 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
932 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
934 static ir_node *equivalent_node_Or(ir_node *n)
938 ir_node *a = get_Or_left(n);
939 ir_node *b = get_Or_right(n);
942 n = a; /* Or has it's own neutral element */
943 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
945 DBG_OPT_ALGSIM1(oldn, a, b, n);
946 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
948 DBG_OPT_ALGSIM1(oldn, a, b, n);
955 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
957 static ir_node *equivalent_node_And(ir_node *n)
961 ir_node *a = get_And_left(n);
962 ir_node *b = get_And_right(n);
965 n = a; /* And has it's own neutral element */
966 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
968 DBG_OPT_ALGSIM1(oldn, a, b, n);
969 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
971 DBG_OPT_ALGSIM1(oldn, a, b, n);
977 * Try to remove useless Conv's:
979 static ir_node *equivalent_node_Conv(ir_node *n)
982 ir_node *a = get_Conv_op(n);
985 ir_mode *n_mode = get_irn_mode(n);
986 ir_mode *a_mode = get_irn_mode(a);
988 if (n_mode == a_mode) { /* No Conv necessary */
990 DBG_OPT_ALGSIM3(oldn, a, n);
991 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
995 n_mode = get_irn_mode(n);
996 b_mode = get_irn_mode(b);
998 if (n_mode == b_mode) {
999 if (n_mode == mode_b) {
1000 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1001 DBG_OPT_ALGSIM1(oldn, a, b, n);
1003 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1004 if (smaller_mode(b_mode, a_mode)){
1005 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1006 DBG_OPT_ALGSIM1(oldn, a, b, n);
1015 * A Cast may be removed if the type of the previous node
1016 * is already the type of the Cast.
1018 static ir_node *equivalent_node_Cast(ir_node *n) {
1019 ir_node *pred = get_Cast_op(n);
1020 if (get_irn_type(pred) == get_Cast_type(n))
1025 /* Several optimizations:
1026 - no Phi in start block.
1027 - remove Id operators that are inputs to Phi
1028 - fold Phi-nodes, iff they have only one predecessor except
1031 static ir_node *equivalent_node_Phi(ir_node *n)
1036 ir_node *block = NULL; /* to shutup gcc */
1037 ir_node *first_val = NULL; /* to shutup gcc */
1038 ir_node *scnd_val = NULL; /* to shutup gcc */
1040 if (!get_opt_normalize()) return n;
1042 n_preds = get_Phi_n_preds(n);
1044 block = get_nodes_block(n);
1045 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1046 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1047 if ((is_Block_dead(block)) || /* Control dead */
1048 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1049 return new_Bad(); /* in the Start Block. */
1051 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1054 /* first we test for a special case: */
1055 /* Confirm is a special node fixing additional information for a
1056 value that is known at a certain point. This is useful for
1057 dataflow analysis. */
1059 ir_node *a = get_Phi_pred(n, 0);
1060 ir_node *b = get_Phi_pred(n, 1);
1061 if ( (get_irn_op(a) == op_Confirm)
1062 && (get_irn_op(b) == op_Confirm)
1063 && (get_irn_n(a, 0) == get_irn_n(b, 0))
1064 && (get_irn_n(a, 1) == get_irn_n(b, 1))
1065 && (a->data.num == (~b->data.num & irpn_True) )) {
1066 return get_irn_n(a, 0);
1071 /* If the Block has a Bad pred, we also have one. */
1072 for (i = 0; i < n_preds; ++i)
1073 if (is_Bad (get_Block_cfgpred(block, i)))
1074 set_Phi_pred(n, i, new_Bad());
1076 /* Find first non-self-referencing input */
1077 for (i = 0; i < n_preds; ++i) {
1078 first_val = get_Phi_pred(n, i);
1079 if ( (first_val != n) /* not self pointer */
1081 && (get_irn_op(first_val) != op_Bad)
1083 ) { /* value not dead */
1084 break; /* then found first value. */
1088 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1089 if (i >= n_preds) { return new_Bad(); }
1093 /* follow_Id () for rest of inputs, determine if any of these
1094 are non-self-referencing */
1095 while (++i < n_preds) {
1096 scnd_val = get_Phi_pred(n, i);
1097 if ( (scnd_val != n)
1098 && (scnd_val != first_val)
1100 && (get_irn_op(scnd_val) != op_Bad)
1107 /* Fold, if no multiple distinct non-self-referencing inputs */
1110 DBG_OPT_PHI(oldn, first_val, n);
1112 /* skip the remaining Ids (done in get_Phi_pred). */
1113 /* superfluous, since we walk all to propagate Block's Bads.
1114 while (++i < n_preds) get_Phi_pred(n, i); */
1120 * optimize Proj(Tuple) and gigo for ProjX in Bad block
1122 static ir_node *equivalent_node_Proj(ir_node *n)
1126 ir_node *a = get_Proj_pred(n);
1128 if ( get_irn_op(a) == op_Tuple) {
1129 /* Remove the Tuple/Proj combination. */
1130 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1131 n = get_Tuple_pred(a, get_Proj_proj(n));
1132 DBG_OPT_TUPLE(oldn, a, n);
1134 assert(0); /* This should not happen! */
1137 } else if (get_irn_mode(n) == mode_X &&
1138 is_Block_dead(get_nodes_block(n))) {
1139 /* Remove dead control flow -- early gigo. */
1148 static ir_node *equivalent_node_Id(ir_node *n)
1154 } while (get_irn_op(n) == op_Id);
1156 DBG_OPT_ID(oldn, n);
1163 static ir_node *equivalent_node_Mux(ir_node *n)
1165 ir_node *oldn = n, *sel = get_Mux_sel(n);
1166 tarval *ts = value_of(sel);
1168 /* Mux(true, f, t) == t */
1169 if (ts == get_tarval_b_true()) {
1170 n = get_Mux_true(n);
1171 DBG_OPT_ALGSIM0(oldn, n);
1173 /* Mux(false, f, t) == f */
1174 else if (ts == get_tarval_b_false()) {
1175 n = get_Mux_false(n);
1176 DBG_OPT_ALGSIM0(oldn, n);
1178 /* Mux(v, x, x) == x */
1179 else if (get_Mux_false(n) == get_Mux_true(n)) {
1180 n = get_Mux_true(n);
1181 DBG_OPT_ALGSIM0(oldn, n);
1183 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1184 ir_node *cmp = get_Proj_pred(sel);
1185 long proj_nr = get_Proj_proj(sel);
1186 ir_node *b = get_Mux_false(n);
1187 ir_node *a = get_Mux_true(n);
1190 * Note: normalization puts the constant on the right site,
1191 * so we check only one case.
1193 * Note further that these optimization work even for floating point
1194 * with NaN's because -NaN == NaN.
1195 * However, if +0 and -0 is handled differently, we cannot use the first one.
1197 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1198 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1199 /* Mux(a CMP 0, X, a) */
1200 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1201 /* Mux(a CMP 0, -a, a) */
1202 if (proj_nr == pn_Cmp_Eq) {
1203 /* Mux(a == 0, -a, a) ==> -a */
1205 DBG_OPT_ALGSIM0(oldn, n);
1207 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1208 /* Mux(a != 0, -a, a) ==> a */
1210 DBG_OPT_ALGSIM0(oldn, n);
1213 else if (classify_Const(b) == CNST_NULL) {
1214 /* Mux(a CMP 0, 0, a) */
1215 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1216 /* Mux(a != 0, 0, a) ==> a */
1218 DBG_OPT_ALGSIM0(oldn, n);
1220 else if (proj_nr == pn_Cmp_Eq) {
1221 /* Mux(a == 0, 0, a) ==> 0 */
1223 DBG_OPT_ALGSIM0(oldn, n);
1234 * Optimize -a CMP -b into b CMP a.
1235 * This works only for for modes where unary Minus
1237 * Note that two-complement integers can Overflow
1238 * so it will NOT work.
1240 static ir_node *equivalent_node_Cmp(ir_node *n)
1242 ir_node *left = get_Cmp_left(n);
1243 ir_node *right = get_Cmp_right(n);
1245 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1246 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1247 left = get_Minus_op(left);
1248 right = get_Minus_op(right);
1249 set_Cmp_left(n, right);
1250 set_Cmp_right(n, left);
1256 * Remove Confirm nodes if setting is on.
1258 static ir_node *equivalent_node_Confirm(ir_node *n)
1260 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
1261 ir_node *bound = get_Confirm_bound(n);
1262 ir_op *op = get_irn_op(bound);
1265 * Optimize a rare case:
1266 * Confirm(x, '=', Const) ==> Const
1268 if (op == op_Const || op == op_SymConst)
1271 return get_opt_remove_Confirm() ? get_Confirm_value(n) : n;
1275 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1276 * perform no actual computation, as, e.g., the Id nodes. It does not create
1277 * new nodes. It is therefore safe to free n if the node returned is not n.
1278 * If a node returns a Tuple we can not just skip it. If the size of the
1279 * in array fits, we transform n into a tuple (e.g., Div).
1282 equivalent_node(ir_node *n)
1284 if (n->op->equivalent_node)
1285 return n->op->equivalent_node(n);
1290 * set the default equivalent node operation
1292 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1296 op->equivalent_node = equivalent_node_##a; \
1326 op->equivalent_node = NULL;
1334 * Do node specific optimizations of nodes predecessors.
1337 optimize_preds(ir_node *n) {
1338 ir_node *a = NULL, *b = NULL;
1340 /* get the operands we will work on for simple cases. */
1342 a = get_binop_left(n);
1343 b = get_binop_right(n);
1344 } else if (is_unop(n)) {
1348 switch (get_irn_opcode(n)) {
1351 /* We don't want Cast as input to Cmp. */
1352 if (get_irn_op(a) == op_Cast) {
1356 if (get_irn_op(b) == op_Cast) {
1358 set_Cmp_right(n, b);
1367 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1368 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1369 * If possible, remove the Conv's.
1371 static ir_node *transform_node_AddSub(ir_node *n)
1373 ir_mode *mode = get_irn_mode(n);
1375 if (mode_is_reference(mode)) {
1376 ir_node *left = get_binop_left(n);
1377 ir_node *right = get_binop_right(n);
1378 int ref_bits = get_mode_size_bits(mode);
1380 if (get_irn_op(left) == op_Conv) {
1381 ir_mode *mode = get_irn_mode(left);
1382 int bits = get_mode_size_bits(mode);
1384 if (ref_bits == bits &&
1385 mode_is_int(mode) &&
1386 get_mode_arithmetic(mode) == irma_twos_complement) {
1387 ir_node *pre = get_Conv_op(left);
1388 ir_mode *pre_mode = get_irn_mode(pre);
1390 if (mode_is_int(pre_mode) &&
1391 get_mode_size_bits(pre_mode) == bits &&
1392 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1393 /* ok, this conv just changes to sign, moreover the calculation
1394 * is done with same number of bits as our address mode, so
1395 * we can ignore the conv as address calculation can be viewed
1396 * as either signed or unsigned
1398 set_binop_left(n, pre);
1403 if (get_irn_op(right) == op_Conv) {
1404 ir_mode *mode = get_irn_mode(right);
1405 int bits = get_mode_size_bits(mode);
1407 if (ref_bits == bits &&
1408 mode_is_int(mode) &&
1409 get_mode_arithmetic(mode) == irma_twos_complement) {
1410 ir_node *pre = get_Conv_op(right);
1411 ir_mode *pre_mode = get_irn_mode(pre);
1413 if (mode_is_int(pre_mode) &&
1414 get_mode_size_bits(pre_mode) == bits &&
1415 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1416 /* ok, this conv just changes to sign, moreover the calculation
1417 * is done with same number of bits as our address mode, so
1418 * we can ignore the conv as address calculation can be viewed
1419 * as either signed or unsigned
1421 set_binop_right(n, pre);
1430 * Do the AddSub optimization, then Transform Add(a,a) into Mul(a, 2)
1431 * if the mode is integer or float.
1432 * Transform Add(a,-b) into Sub(a,b).
1433 * Reassociation might fold this further.
1435 static ir_node *transform_node_Add(ir_node *n)
1440 n = transform_node_AddSub(n);
1442 mode = get_irn_mode(n);
1443 if (mode_is_num(mode)) {
1444 ir_node *a = get_Add_left(n);
1446 if (a == get_Add_right(n)) {
1447 ir_node *block = get_nodes_block(n);
1450 get_irn_dbg_info(n),
1454 new_r_Const_long(current_ir_graph, block, mode, 2),
1456 DBG_OPT_ALGSIM0(oldn, n);
1459 ir_node *b = get_Add_right(n);
1461 if (get_irn_op(a) == op_Minus) {
1463 get_irn_dbg_info(n),
1469 DBG_OPT_ALGSIM0(oldn, n);
1471 else if (get_irn_op(b) == op_Minus) {
1473 get_irn_dbg_info(n),
1479 DBG_OPT_ALGSIM0(oldn, n);
1487 * Do the AddSub optimization, then Transform Sub(0,a) into Minus(a).
1489 static ir_node *transform_node_Sub(ir_node *n)
1494 n = transform_node_AddSub(n);
1496 mode = get_irn_mode(n);
1497 if (mode_is_num(mode) && (classify_Const(get_Sub_left(n)) == CNST_NULL)) {
1499 get_irn_dbg_info(n),
1504 DBG_OPT_ALGSIM0(oldn, n);
1511 Transform Mul(a,-1) into -a.
1512 * Do architecture dependent optimizations on Mul nodes
1514 static ir_node *transform_node_Mul(ir_node *n) {
1516 ir_mode *mode = get_irn_mode(n);
1518 if (mode_is_signed(mode)) {
1520 ir_node *a = get_Mul_left(n);
1521 ir_node *b = get_Mul_right(n);
1523 if (value_of(a) == get_mode_minus_one(mode))
1525 else if (value_of(b) == get_mode_minus_one(mode))
1528 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
1529 DBG_OPT_ALGSIM1(oldn, a, b, n);
1533 return arch_dep_replace_mul_with_shifts(n);
1537 * transform a Div Node
1539 static ir_node *transform_node_Div(ir_node *n)
1541 tarval *tv = value_of(n);
1544 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1546 if (tv != tarval_bad) {
1547 value = new_Const(get_tarval_mode(tv), tv);
1549 DBG_OPT_CSTEVAL(n, value);
1551 else /* Try architecture dependent optimization */
1552 value = arch_dep_replace_div_by_const(n);
1555 /* Turn Div into a tuple (mem, bad, value) */
1556 ir_node *mem = get_Div_mem(n);
1558 turn_into_tuple(n, 3);
1559 set_Tuple_pred(n, pn_Div_M, mem);
1560 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1561 set_Tuple_pred(n, pn_Div_res, value);
1567 * transform a Mod node
1569 static ir_node *transform_node_Mod(ir_node *n)
1571 tarval *tv = value_of(n);
1574 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1576 if (tv != tarval_bad) {
1577 value = new_Const(get_tarval_mode(tv), tv);
1579 DBG_OPT_CSTEVAL(n, value);
1581 else /* Try architecture dependent optimization */
1582 value = arch_dep_replace_mod_by_const(n);
1585 /* Turn Mod into a tuple (mem, bad, value) */
1586 ir_node *mem = get_Mod_mem(n);
1588 turn_into_tuple(n, 3);
1589 set_Tuple_pred(n, pn_Mod_M, mem);
1590 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1591 set_Tuple_pred(n, pn_Mod_res, value);
1597 * transform a DivMod node
1599 static ir_node *transform_node_DivMod(ir_node *n)
1603 ir_node *a = get_DivMod_left(n);
1604 ir_node *b = get_DivMod_right(n);
1605 ir_mode *mode = get_irn_mode(a);
1606 tarval *ta = value_of(a);
1607 tarval *tb = value_of(b);
1609 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1612 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1614 if (tb != tarval_bad) {
1615 if (tb == get_mode_one(get_tarval_mode(tb))) {
1616 b = new_Const (mode, get_mode_null(mode));
1619 DBG_OPT_CSTEVAL(n, b);
1621 else if (ta != tarval_bad) {
1622 tarval *resa, *resb;
1623 resa = tarval_div (ta, tb);
1624 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1625 Jmp for X result!? */
1626 resb = tarval_mod (ta, tb);
1627 if (resb == tarval_bad) return n; /* Causes exception! */
1628 a = new_Const (mode, resa);
1629 b = new_Const (mode, resb);
1632 DBG_OPT_CSTEVAL(n, a);
1633 DBG_OPT_CSTEVAL(n, b);
1635 else { /* Try architecture dependent optimization */
1636 arch_dep_replace_divmod_by_const(&a, &b, n);
1637 evaluated = a != NULL;
1639 } else if (ta == get_mode_null(mode)) {
1640 /* 0 / non-Const = 0 */
1645 if (evaluated) { /* replace by tuple */
1646 ir_node *mem = get_DivMod_mem(n);
1647 turn_into_tuple(n, 4);
1648 set_Tuple_pred(n, pn_DivMod_M, mem);
1649 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1650 set_Tuple_pred(n, pn_DivMod_res_div, a);
1651 set_Tuple_pred(n, pn_DivMod_res_mod, b);
1652 assert(get_nodes_block(n));
1659 * Optimize Abs(x) into x if x is Confirmed >= 0
1660 * Optimize Abs(x) into -x if x is Confirmed <= 0
1662 static ir_node *transform_node_Abs(ir_node *n)
1665 ir_node *a = get_Abs_op(n);
1666 value_classify sign = classify_value_sign(a);
1668 if (sign == VALUE_NEGATIVE) {
1669 ir_mode *mode = get_irn_mode(n);
1672 * We can replace the Abs by -x here.
1673 * We even could add a new Confirm here.
1675 * Note that -x would create a new node, so we could
1676 * not run it in the equivalent_node() context.
1678 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1679 get_nodes_block(n), a, mode);
1681 DBG_OPT_CONFIRM(oldn, n);
1683 else if (sign == VALUE_POSITIVE) {
1684 /* n is positive, Abs is not needed */
1687 DBG_OPT_CONFIRM(oldn, n);
1694 * transform a Cond node
1696 static ir_node *transform_node_Cond(ir_node *n)
1698 /* Replace the Cond by a Jmp if it branches on a constant
1701 ir_node *a = get_Cond_selector(n);
1702 tarval *ta = value_of(a);
1704 if ((ta != tarval_bad) &&
1705 (get_irn_mode(a) == mode_b) &&
1706 (get_opt_unreachable_code())) {
1707 /* It's a boolean Cond, branching on a boolean constant.
1708 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1709 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1710 turn_into_tuple(n, 2);
1711 if (ta == tarval_b_true) {
1712 set_Tuple_pred(n, pn_Cond_false, new_Bad());
1713 set_Tuple_pred(n, pn_Cond_true, jmp);
1715 set_Tuple_pred(n, pn_Cond_false, jmp);
1716 set_Tuple_pred(n, pn_Cond_true, new_Bad());
1718 /* We might generate an endless loop, so keep it alive. */
1719 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1720 } else if ((ta != tarval_bad) &&
1721 (get_irn_mode(a) == mode_Iu) &&
1722 (get_Cond_kind(n) == dense) &&
1723 (get_opt_unreachable_code())) {
1724 /* I don't want to allow Tuples smaller than the biggest Proj.
1725 Also this tuple might get really big...
1726 I generate the Jmp here, and remember it in link. Link is used
1727 when optimizing Proj. */
1728 set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_block(n)));
1729 /* We might generate an endless loop, so keep it alive. */
1730 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1731 } else if ((get_irn_op(a) == op_Eor)
1732 && (get_irn_mode(a) == mode_b)
1733 && (classify_tarval(value_of(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1734 /* The Eor is a negate. Generate a new Cond without the negate,
1735 simulate the negate by exchanging the results. */
1736 set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1738 } else if ((get_irn_op(a) == op_Not)
1739 && (get_irn_mode(a) == mode_b)) {
1740 /* A Not before the Cond. Generate a new Cond without the Not,
1741 simulate the Not by exchanging the results. */
1742 set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1751 static ir_node *transform_node_Eor(ir_node *n)
1754 ir_node *a = get_Eor_left(n);
1755 ir_node *b = get_Eor_right(n);
1756 ir_mode *mode = get_irn_mode(n);
1760 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
1761 mode, get_mode_null(mode));
1762 DBG_OPT_ALGSIM0(oldn, n);
1764 else if ((mode == mode_b)
1765 && (get_irn_op(a) == op_Proj)
1766 && (get_irn_mode(a) == mode_b)
1767 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1768 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1769 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1770 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1771 mode_b, get_negated_pnc(get_Proj_proj(a)));
1773 DBG_OPT_ALGSIM0(oldn, n);
1775 else if ((mode == mode_b)
1776 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
1777 /* The Eor is a Not. Replace it by a Not. */
1778 /* ????!!!Extend to bitfield 1111111. */
1779 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode_b);
1781 DBG_OPT_ALGSIM0(oldn, n);
1788 * Transform a boolean Not.
1790 static ir_node *transform_node_Not(ir_node *n)
1793 ir_node *a = get_Not_op(n);
1795 if ( (get_irn_mode(n) == mode_b)
1796 && (get_irn_op(a) == op_Proj)
1797 && (get_irn_mode(a) == mode_b)
1798 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1799 /* We negate a Cmp. The Cmp has the negated result anyways! */
1800 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1801 mode_b, get_negated_pnc(get_Proj_proj(a)));
1802 DBG_OPT_ALGSIM0(oldn, n);
1809 * Transform a Cast_type(Const) into a new Const_type
1811 static ir_node *transform_node_Cast(ir_node *n) {
1813 ir_node *pred = get_Cast_op(n);
1814 type *tp = get_irn_type(n);
1816 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1817 n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
1818 get_Const_tarval(pred), tp);
1819 DBG_OPT_CSTEVAL(oldn, n);
1820 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1821 n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
1822 get_SymConst_kind(pred), tp);
1823 DBG_OPT_CSTEVAL(oldn, n);
1830 * Transform a Proj(Div) with a non-zero value.
1831 * Removes the exceptions and routes the memory to the NoMem node.
1833 static ir_node *transform_node_Proj_Div(ir_node *proj)
1835 ir_node *n = get_Proj_pred(proj);
1836 ir_node *b = get_Div_right(n);
1839 if (value_not_zero(b)) {
1840 /* div(x, y) && y != 0 */
1841 proj_nr = get_Proj_proj(proj);
1843 /* this node may float */
1844 set_irn_pinned(n, op_pin_state_floats);
1846 if (proj_nr == pn_Div_X_except) {
1847 /* we found an exception handler, remove it */
1850 /* the memory Proj can be removed */
1851 ir_node *res = get_Div_mem(n);
1852 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1853 if (proj_nr == pn_Div_M)
1861 * Transform a Proj(Mod) with a non-zero value.
1862 * Removes the exceptions and routes the memory to the NoMem node.
1864 static ir_node *transform_node_Proj_Mod(ir_node *proj)
1866 ir_node *n = get_Proj_pred(proj);
1867 ir_node *b = get_Mod_right(n);
1870 if (value_not_zero(b)) {
1871 /* mod(x, y) && y != 0 */
1872 proj_nr = get_Proj_proj(proj);
1874 /* this node may float */
1875 set_irn_pinned(n, op_pin_state_floats);
1877 if (proj_nr == pn_Mod_X_except) {
1878 /* we found an exception handler, remove it */
1881 /* the memory Proj can be removed */
1882 ir_node *res = get_Mod_mem(n);
1883 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1884 if (proj_nr == pn_Mod_M)
1892 * Transform a Proj(DivMod) with a non-zero value.
1893 * Removes the exceptions and routes the memory to the NoMem node.
1895 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
1897 ir_node *n = get_Proj_pred(proj);
1898 ir_node *b = get_DivMod_right(n);
1901 if (value_not_zero(b)) {
1902 /* DivMod(x, y) && y != 0 */
1903 proj_nr = get_Proj_proj(proj);
1905 /* this node may float */
1906 set_irn_pinned(n, op_pin_state_floats);
1908 if (proj_nr == pn_DivMod_X_except) {
1909 /* we found an exception handler, remove it */
1913 /* the memory Proj can be removed */
1914 ir_node *res = get_DivMod_mem(n);
1915 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1916 if (proj_nr == pn_DivMod_M)
1924 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
1926 static ir_node *transform_node_Proj_Cond(ir_node *proj)
1928 if (get_opt_unreachable_code()) {
1929 ir_node *n = get_Proj_pred(proj);
1930 ir_node *b = get_Cond_selector(n);
1931 tarval *tb = value_of(b);
1933 if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
1934 /* we have a constant switch */
1935 long num = get_Proj_proj(proj);
1937 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
1938 if (get_tarval_long(tb) == num) {
1939 /* Do NOT create a jump here, or we will have 2 control flow ops
1940 * in a block. This case is optimized away in optimize_cf(). */
1944 /* this case will NEVER be taken, kill it */
1954 * Normalizes and optimizes Cmp nodes.
1956 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
1958 if (get_opt_reassociation()) {
1959 ir_node *n = get_Proj_pred(proj);
1960 ir_node *left = get_Cmp_left(n);
1961 ir_node *right = get_Cmp_right(n);
1965 ir_mode *mode = NULL;
1966 long proj_nr = get_Proj_proj(proj);
1969 * First step: normalize the compare op
1970 * by placing the constant on the right site
1971 * or moving the lower address node to the left.
1972 * We ignore the case that both are constants
1973 * this case should be optimized away.
1975 if (get_irn_op(right) == op_Const)
1977 else if (get_irn_op(left) == op_Const) {
1982 proj_nr = get_inversed_pnc(proj_nr);
1985 else if (left > right) {
1991 proj_nr = get_inversed_pnc(proj_nr);
1996 * Second step: Try to reduce the magnitude
1997 * of a constant. This may help to generate better code
1998 * later and may help to normalize more compares.
1999 * Of course this is only possible for integer values.
2002 mode = get_irn_mode(c);
2003 tv = get_Const_tarval(c);
2005 if (tv != tarval_bad) {
2006 /* the following optimization is possible on modes without Overflow
2007 * on Unary Minus or on == and !=:
2008 * -a CMP c ==> a swap(CMP) -c
2010 * Beware: for two-complement Overflow may occur, so only == and != can
2011 * be optimized, see this:
2012 * -MININT < 0 =/=> MININT > 0 !!!
2014 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2015 (!mode_overflow_on_unary_Minus(mode) ||
2016 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2017 left = get_Minus_op(left);
2018 tv = tarval_sub(get_mode_null(mode), tv);
2020 proj_nr = get_inversed_pnc(proj_nr);
2024 /* for integer modes, we have more */
2025 if (mode_is_int(mode)) {
2026 /* Ne includes Unordered which is not possible on integers.
2027 * However, frontends often use this wrong, so fix it here */
2028 if (proj_nr == pn_Cmp_Ne) {
2029 proj_nr = pn_Cmp_Lg;
2030 set_Proj_proj(proj, proj_nr);
2033 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2034 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2035 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2036 tv = tarval_sub(tv, get_mode_one(mode));
2038 proj_nr ^= pn_Cmp_Eq;
2041 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2042 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2043 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2044 tv = tarval_add(tv, get_mode_one(mode));
2046 proj_nr ^= pn_Cmp_Eq;
2050 /* the following reassociations work only for == and != */
2051 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2053 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2054 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2055 right = get_Sub_right(left);
2056 left = get_Sub_left(left);
2058 tv = value_of(right);
2062 if (tv != tarval_bad) {
2063 ir_op *op = get_irn_op(left);
2065 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2067 ir_node *c1 = get_Sub_right(left);
2068 tarval *tv2 = value_of(c1);
2070 if (tv2 != tarval_bad) {
2071 tv2 = tarval_add(tv, value_of(c1));
2073 if (tv2 != tarval_bad) {
2074 left = get_Sub_left(left);
2080 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2081 else if (op == op_Add) {
2082 ir_node *a_l = get_Add_left(left);
2083 ir_node *a_r = get_Add_right(left);
2087 if (get_irn_op(a_l) == op_Const) {
2089 tv2 = value_of(a_l);
2093 tv2 = value_of(a_r);
2096 if (tv2 != tarval_bad) {
2097 tv2 = tarval_sub(tv, tv2);
2099 if (tv2 != tarval_bad) {
2106 /* -a == c ==> a == -c, -a != c ==> a != -c */
2107 else if (op == op_Minus) {
2108 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2110 if (tv2 != tarval_bad) {
2111 left = get_Minus_op(left);
2118 /* the following reassociations work only for <= */
2119 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2120 if (tv != tarval_bad) {
2121 ir_op *op = get_irn_op(left);
2123 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2133 ir_node *block = get_nodes_block(n);
2135 if (changed & 2) /* need a new Const */
2136 right = new_Const(mode, tv);
2138 /* create a new compare */
2139 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2142 set_Proj_pred(proj, n);
2143 set_Proj_proj(proj, proj_nr);
2150 * Does all optimizations on nodes that must be done on it's Proj's
2151 * because of creating new nodes.
2153 static ir_node *transform_node_Proj(ir_node *proj)
2155 ir_node *n = get_Proj_pred(proj);
2157 switch (get_irn_opcode(n)) {
2159 return transform_node_Proj_Div(proj);
2162 return transform_node_Proj_Mod(proj);
2165 return transform_node_Proj_DivMod(proj);
2168 return transform_node_Proj_Cond(proj);
2171 return transform_node_Proj_Cmp(proj);
2174 /* should not happen, but if it does will be optimized away */
2175 return equivalent_node_Proj(proj);
2184 * returns the operands of a commutative bin-op, if one operand is
2185 * a const, it is returned as the second one.
2187 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2189 ir_node *op_a = get_binop_left(binop);
2190 ir_node *op_b = get_binop_right(binop);
2192 assert(is_op_commutative(get_irn_op(binop)));
2194 if (get_irn_op(op_a) == op_Const) {
2205 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2206 * Such pattern may arise in bitfield stores.
2208 * value c4 value c4 & c2
2209 * AND c3 AND c1 | c3
2214 static ir_node *transform_node_Or_bf_store(ir_node *or)
2218 ir_node *and_l, *c3;
2219 ir_node *value, *c4;
2220 ir_node *new_and, *new_const, *block;
2221 ir_mode *mode = get_irn_mode(or);
2223 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2225 get_comm_Binop_Ops(or, &and, &c1);
2226 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2229 get_comm_Binop_Ops(and, &or_l, &c2);
2230 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2233 get_comm_Binop_Ops(or_l, &and_l, &c3);
2234 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2237 get_comm_Binop_Ops(and_l, &value, &c4);
2238 if (get_irn_op(c4) != op_Const)
2241 /* ok, found the pattern, check for conditions */
2242 assert(mode == get_irn_mode(and));
2243 assert(mode == get_irn_mode(or_l));
2244 assert(mode == get_irn_mode(and_l));
2246 tv1 = get_Const_tarval(c1);
2247 tv2 = get_Const_tarval(c2);
2248 tv3 = get_Const_tarval(c3);
2249 tv4 = get_Const_tarval(c4);
2251 tv = tarval_or(tv4, tv2);
2252 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2253 /* have at least one 0 at the same bit position */
2257 n_tv4 = tarval_not(tv4);
2258 if (tv3 != tarval_and(tv3, n_tv4)) {
2259 /* bit in the or_mask is outside the and_mask */
2263 n_tv2 = tarval_not(tv2);
2264 if (tv1 != tarval_and(tv1, n_tv2)) {
2265 /* bit in the or_mask is outside the and_mask */
2269 /* ok, all conditions met */
2270 block = get_nodes_block(or);
2272 new_and = new_r_And(current_ir_graph, block,
2273 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2275 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2277 set_Or_left(or, new_and);
2278 set_Or_right(or, new_const);
2280 /* check for more */
2281 return transform_node_Or_bf_store(or);
2285 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2287 static ir_node *transform_node_Or_Rot(ir_node *or)
2289 ir_mode *mode = get_irn_mode(or);
2290 ir_node *shl, *shr, *block;
2291 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2294 if (! mode_is_int(mode))
2297 shl = get_binop_left(or);
2298 shr = get_binop_right(or);
2300 if (get_irn_op(shl) == op_Shr) {
2301 if (get_irn_op(shr) != op_Shl)
2308 else if (get_irn_op(shl) != op_Shl)
2310 else if (get_irn_op(shr) != op_Shr)
2313 x = get_Shl_left(shl);
2314 if (x != get_Shr_left(shr))
2317 c1 = get_Shl_right(shl);
2318 c2 = get_Shr_right(shr);
2319 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2320 tv1 = get_Const_tarval(c1);
2321 if (! tarval_is_long(tv1))
2324 tv2 = get_Const_tarval(c2);
2325 if (! tarval_is_long(tv2))
2328 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2329 != get_mode_size_bits(mode))
2332 /* yet, condition met */
2333 block = get_nodes_block(or);
2335 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2337 DBG_OPT_ALGSIM1(or, shl, shr, n);
2340 else if (get_irn_op(c1) == op_Sub) {
2344 if (get_Sub_right(sub) != v)
2347 c1 = get_Sub_left(sub);
2348 if (get_irn_op(c1) != op_Const)
2351 tv1 = get_Const_tarval(c1);
2352 if (! tarval_is_long(tv1))
2355 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2358 /* yet, condition met */
2359 block = get_nodes_block(or);
2361 /* a Rot right is not supported, so use a rot left */
2362 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2364 DBG_OPT_ALGSIM0(or, n);
2367 else if (get_irn_op(c2) == op_Sub) {
2371 c1 = get_Sub_left(sub);
2372 if (get_irn_op(c1) != op_Const)
2375 tv1 = get_Const_tarval(c1);
2376 if (! tarval_is_long(tv1))
2379 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2382 /* yet, condition met */
2383 block = get_nodes_block(or);
2386 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2388 DBG_OPT_ALGSIM0(or, n);
2398 static ir_node *transform_node_Or(ir_node *or)
2400 or = transform_node_Or_bf_store(or);
2401 or = transform_node_Or_Rot(or);
2407 static ir_node *transform_node(ir_node *n);
2410 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
2412 static ir_node *transform_node_shift(ir_node *n)
2414 ir_node *left, *right;
2415 tarval *tv1, *tv2, *res;
2417 int modulo_shf, flag;
2419 left = get_binop_left(n);
2421 /* different operations */
2422 if (get_irn_op(left) != get_irn_op(n))
2425 right = get_binop_right(n);
2426 tv1 = value_of(right);
2427 if (tv1 == tarval_bad)
2430 tv2 = value_of(get_binop_right(left));
2431 if (tv2 == tarval_bad)
2434 res = tarval_add(tv1, tv2);
2436 /* beware: a simple replacement works only, if res < modulo shift */
2437 mode = get_irn_mode(n);
2441 modulo_shf = get_mode_modulo_shift(mode);
2442 if (modulo_shf > 0) {
2443 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2445 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2452 /* ok, we can replace it */
2453 ir_node *in[2], *irn, *block = get_nodes_block(n);
2455 in[0] = get_binop_left(left);
2456 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2458 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2460 DBG_OPT_ALGSIM0(n, irn);
2462 return transform_node(irn);
2467 #define transform_node_Shr transform_node_shift
2468 #define transform_node_Shrs transform_node_shift
2469 #define transform_node_Shl transform_node_shift
2472 * Remove dead blocks in keep alive list. We do not generate a new End node.
2474 static ir_node *transform_node_End(ir_node *n) {
2475 int i, n_keepalives = get_End_n_keepalives(n);
2477 for (i = 0; i < n_keepalives; ++i) {
2478 ir_node *ka = get_End_keepalive(n, i);
2479 if (is_Block(ka) && is_Block_dead(ka))
2480 set_End_keepalive(n, i, new_Bad());
2486 * Optimize a Mux into some simpler cases.
2488 static ir_node *transform_node_Mux(ir_node *n)
2490 ir_node *oldn = n, *sel = get_Mux_sel(n);
2491 ir_mode *mode = get_irn_mode(n);
2493 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2494 ir_node *cmp = get_Proj_pred(sel);
2495 long proj_nr = get_Proj_proj(sel);
2496 ir_node *f = get_Mux_false(n);
2497 ir_node *t = get_Mux_true(n);
2499 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2500 ir_node *block = get_nodes_block(n);
2503 * Note: normalization puts the constant on the right site,
2504 * so we check only one case.
2506 * Note further that these optimization work even for floating point
2507 * with NaN's because -NaN == NaN.
2508 * However, if +0 and -0 is handled differently, we cannot use the first one.
2510 if (get_irn_op(f) == op_Minus &&
2511 get_Minus_op(f) == t &&
2512 get_Cmp_left(cmp) == t) {
2514 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2515 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2516 n = new_rd_Abs(get_irn_dbg_info(n),
2520 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2523 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2524 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2525 n = new_rd_Abs(get_irn_dbg_info(n),
2529 n = new_rd_Minus(get_irn_dbg_info(n),
2534 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2538 else if (get_irn_op(t) == op_Minus &&
2539 get_Minus_op(t) == f &&
2540 get_Cmp_left(cmp) == f) {
2542 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2543 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2544 n = new_rd_Abs(get_irn_dbg_info(n),
2548 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2551 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2552 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
2553 n = new_rd_Abs(get_irn_dbg_info(n),
2557 n = new_rd_Minus(get_irn_dbg_info(n),
2562 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2567 if (mode_is_int(mode) && mode_is_signed(mode) &&
2568 get_mode_arithmetic(mode) == irma_twos_complement) {
2569 ir_node *x = get_Cmp_left(cmp);
2571 /* the following optimization works only with signed integer two-complement mode */
2573 if (mode == get_irn_mode(x)) {
2575 * FIXME: this restriction is two rigid, as it would still
2576 * work if mode(x) = Hs and mode == Is, but at least it removes
2579 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2580 classify_Const(t) == CNST_ALL_ONE &&
2581 classify_Const(f) == CNST_NULL) {
2583 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2587 n = new_rd_Shrs(get_irn_dbg_info(n),
2588 current_ir_graph, block, x,
2589 new_r_Const_long(current_ir_graph, block, mode_Iu,
2590 get_mode_size_bits(mode) - 1),
2592 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2595 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2596 classify_Const(t) == CNST_ONE &&
2597 classify_Const(f) == CNST_NULL) {
2599 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2603 n = new_rd_Shr(get_irn_dbg_info(n),
2604 current_ir_graph, block,
2605 new_r_Minus(current_ir_graph, block, x, mode),
2606 new_r_Const_long(current_ir_graph, block, mode_Iu,
2607 get_mode_size_bits(mode) - 1),
2609 DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2616 return arch_transform_node_Mux(n);
2620 * Tries several [inplace] [optimizing] transformations and returns an
2621 * equivalent node. The difference to equivalent_node() is that these
2622 * transformations _do_ generate new nodes, and thus the old node must
2623 * not be freed even if the equivalent node isn't the old one.
2625 static ir_node *transform_node(ir_node *n)
2627 if (n->op->transform_node)
2628 n = n->op->transform_node(n);
2633 * set the default transform node operation
2635 static ir_op *firm_set_default_transform_node(ir_op *op)
2639 op->transform_node = transform_node_##a; \
2663 op->transform_node = NULL;
2671 /* **************** Common Subexpression Elimination **************** */
2673 /** The size of the hash table used, should estimate the number of nodes
2675 #define N_IR_NODES 512
2677 /** Compares the attributes of two Const nodes. */
2678 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
2680 return (get_Const_tarval(a) != get_Const_tarval(b))
2681 || (get_Const_type(a) != get_Const_type(b));
2684 /** Compares the attributes of two Proj nodes. */
2685 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
2687 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
2690 /** Compares the attributes of two Filter nodes. */
2691 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
2693 return get_Filter_proj(a) != get_Filter_proj(b);
2696 /** Compares the attributes of two Alloc nodes. */
2697 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
2699 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
2700 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
2703 /** Compares the attributes of two Free nodes. */
2704 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
2706 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
2707 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
2710 /** Compares the attributes of two SymConst nodes. */
2711 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
2713 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
2714 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
2715 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
2718 /** Compares the attributes of two Call nodes. */
2719 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
2721 return (get_irn_call_attr(a) != get_irn_call_attr(b));
2724 /** Compares the attributes of two Sel nodes. */
2725 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
2727 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
2728 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
2729 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
2730 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
2731 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
2734 /** Compares the attributes of two Phi nodes. */
2735 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
2737 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
2740 /** Compares the attributes of two Cast nodes. */
2741 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
2743 return get_Cast_type(a) != get_Cast_type(b);
2746 /** Compares the attributes of two Load nodes. */
2747 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
2749 if (get_Load_volatility(a) == volatility_is_volatile ||
2750 get_Load_volatility(b) == volatility_is_volatile)
2751 /* NEVER do CSE on volatile Loads */
2754 return get_Load_mode(a) != get_Load_mode(b);
2757 /** Compares the attributes of two Store nodes. */
2758 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
2760 /* NEVER do CSE on volatile Stores */
2761 return (get_Store_volatility(a) == volatility_is_volatile ||
2762 get_Store_volatility(b) == volatility_is_volatile);
2765 /** Compares the attributes of two Confirm nodes. */
2766 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
2768 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
2772 * set the default node attribute compare operation
2774 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
2778 op->node_cmp_attr = node_cmp_attr_##a; \
2796 op->node_cmp_attr = NULL;
2804 * Compare function for two nodes in the hash table. Gets two
2805 * nodes as parameters. Returns 0 if the nodes are a cse.
2808 vt_cmp (const void *elt, const void *key)
2816 if (a == b) return 0;
2818 if ((get_irn_op(a) != get_irn_op(b)) ||
2819 (get_irn_mode(a) != get_irn_mode(b))) return 1;
2821 /* compare if a's in and b's in are of equal length */
2822 irn_arity_a = get_irn_intra_arity (a);
2823 if (irn_arity_a != get_irn_intra_arity(b))
2826 /* for block-local cse and op_pin_state_pinned nodes: */
2827 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
2828 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
2832 /* compare a->in[0..ins] with b->in[0..ins] */
2833 for (i = 0; i < irn_arity_a; i++)
2834 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
2838 * here, we already now that the nodes are identical except their
2841 if (a->op->node_cmp_attr)
2842 return a->op->node_cmp_attr(a, b);
2848 * Calculate a hash value of a node.
2851 ir_node_hash (ir_node *node)
2856 if (node->op == op_Const) {
2857 /* special value for const, as they only differ in their tarval. */
2858 h = HASH_PTR(node->attr.con.tv);
2859 h = 9*h + HASH_PTR(get_irn_mode(node));
2860 } else if (node->op == op_SymConst) {
2861 /* special value for const, as they only differ in their symbol. */
2862 h = HASH_PTR(node->attr.i.sym.type_p);
2863 h = 9*h + HASH_PTR(get_irn_mode(node));
2866 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
2867 h = irn_arity = get_irn_intra_arity(node);
2869 /* consider all in nodes... except the block if not a control flow. */
2870 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
2871 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
2875 h = 9*h + HASH_PTR(get_irn_mode(node));
2877 h = 9*h + HASH_PTR(get_irn_op(node));
2884 new_identities(void) {
2885 return new_pset(vt_cmp, N_IR_NODES);
2889 del_identities(pset *value_table) {
2890 del_pset(value_table);
2894 * Return the canonical node computing the same value as n.
2895 * Looks up the node in a hash table.
2897 * For Const nodes this is performed in the constructor, too. Const
2898 * nodes are extremely time critical because of their frequent use in
2899 * constant string arrays.
2901 static INLINE ir_node *
2902 identify (pset *value_table, ir_node *n)
2906 if (!value_table) return n;
2908 if (get_opt_reassociation()) {
2909 if (is_op_commutative(get_irn_op(n))) {
2910 ir_node *l = get_binop_left(n);
2911 ir_node *r = get_binop_right(n);
2913 /* for commutative operators perform a OP b == b OP a */
2915 set_binop_left(n, r);
2916 set_binop_right(n, l);
2921 o = pset_find (value_table, n, ir_node_hash (n));
2930 * During construction we set the op_pin_state_pinned flag in the graph right when the
2931 * optimization is performed. The flag turning on procedure global cse could
2932 * be changed between two allocations. This way we are safe.
2934 static INLINE ir_node *
2935 identify_cons (pset *value_table, ir_node *n) {
2938 n = identify(value_table, n);
2939 if (get_irn_n(old, -1) != get_irn_n(n, -1))
2940 set_irg_pinned(current_ir_graph, op_pin_state_floats);
2945 * Return the canonical node computing the same value as n.
2946 * Looks up the node in a hash table, enters it in the table
2947 * if it isn't there yet.
2950 identify_remember (pset *value_table, ir_node *n)
2954 if (!value_table) return n;
2956 if (get_opt_reassociation()) {
2957 if (is_op_commutative(get_irn_op(n))) {
2958 ir_node *l = get_binop_left(n);
2959 ir_node *r = get_binop_right(n);
2961 /* for commutative operators perform a OP b == b OP a */
2963 set_binop_left(n, r);
2964 set_binop_right(n, l);
2969 /* lookup or insert in hash table with given hash key. */
2970 o = pset_insert (value_table, n, ir_node_hash (n));
2980 add_identities (pset *value_table, ir_node *node) {
2981 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
2982 identify_remember (value_table, node);
2986 * garbage in, garbage out. If a node has a dead input, i.e., the
2987 * Bad node is input to the node, return the Bad node.
2989 static INLINE ir_node *
2990 gigo (ir_node *node)
2993 ir_op* op = get_irn_op(node);
2995 /* remove garbage blocks by looking at control flow that leaves the block
2996 and replacing the control flow by Bad. */
2997 if (get_irn_mode(node) == mode_X) {
2998 ir_node *block = get_nodes_block(node);
2999 if (!get_Block_matured(block)) return node; /* Don't optimize nodes in immature blocks. */
3000 if (op == op_End) return node; /* Don't optimize End, may have Bads. */
3002 if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
3003 irn_arity = get_irn_arity(block);
3004 for (i = 0; i < irn_arity; i++) {
3005 if (!is_Bad(get_irn_n(block, i))) break;
3007 if (i == irn_arity) return new_Bad();
3011 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3012 blocks predecessors is dead. */
3013 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3014 irn_arity = get_irn_arity(node);
3016 if (is_Block_dead(get_nodes_block(node)))
3019 for (i = 0; i < irn_arity; i++) {
3020 if (is_Bad(get_irn_n(node, i))) {
3026 /* With this code we violate the agreement that local_optimize
3027 only leaves Bads in Block, Phi and Tuple nodes. */
3028 /* If Block has only Bads as predecessors it's garbage. */
3029 /* If Phi has only Bads as predecessors it's garbage. */
3030 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3031 irn_arity = get_irn_arity(node);
3032 for (i = 0; i < irn_arity; i++) {
3033 if (!is_Bad(get_irn_n(node, i))) break;
3035 if (i == irn_arity) node = new_Bad();
3043 * These optimizations deallocate nodes from the obstack.
3044 * It can only be called if it is guaranteed that no other nodes
3045 * reference this one, i.e., right after construction of a node.
3048 optimize_node(ir_node *n)
3052 opcode iro = get_irn_opcode(n);
3054 /* Always optimize Phi nodes: part of the construction. */
3055 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3057 /* constant expression evaluation / constant folding */
3058 if (get_opt_constant_folding()) {
3059 /* neither constants nor Tuple values can be evaluated */
3060 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3061 /* try to evaluate */
3062 tv = computed_value(n);
3063 if (tv != tarval_bad) {
3065 type *old_tp = get_irn_type(n);
3066 int i, arity = get_irn_arity(n);
3070 * Try to recover the type of the new expression.
3072 for (i = 0; i < arity && !old_tp; ++i)
3073 old_tp = get_irn_type(get_irn_n(n, i));
3076 * we MUST copy the node here temporary, because it's still needed
3077 * for DBG_OPT_CSTEVAL
3079 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3080 oldn = alloca(node_size);
3082 memcpy(oldn, n, node_size);
3083 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3085 /* ARG, copy the in array, we need it for statistics */
3086 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3088 /* note the inplace edges module */
3089 edges_node_deleted(n, current_ir_graph);
3091 /* evaluation was successful -- replace the node. */
3092 obstack_free(current_ir_graph->obst, n);
3093 nw = new_Const(get_tarval_mode (tv), tv);
3095 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3096 set_Const_type(nw, old_tp);
3097 DBG_OPT_CSTEVAL(oldn, nw);
3103 /* remove unnecessary nodes */
3104 if (get_opt_constant_folding() ||
3105 (iro == iro_Phi) || /* always optimize these nodes. */
3107 (iro == iro_Proj) ||
3108 (iro == iro_Block) ) /* Flags tested local. */
3109 n = equivalent_node (n);
3111 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3113 /* Common Subexpression Elimination.
3115 * Checks whether n is already available.
3116 * The block input is used to distinguish different subexpressions. Right
3117 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3118 * subexpressions within a block.
3121 n = identify_cons (current_ir_graph->value_table, n);
3124 edges_node_deleted(oldn, current_ir_graph);
3126 /* We found an existing, better node, so we can deallocate the old node. */
3127 obstack_free (current_ir_graph->obst, oldn);
3132 /* Some more constant expression evaluation that does not allow to
3134 iro = get_irn_opcode(n);
3135 if (get_opt_constant_folding() ||
3136 (iro == iro_Cond) ||
3137 (iro == iro_Proj) ||
3138 (iro == iro_Sel)) /* Flags tested local. */
3139 n = transform_node (n);
3141 /* Remove nodes with dead (Bad) input.
3142 Run always for transformation induced Bads. */
3145 /* Now we have a legal, useful node. Enter it in hash table for cse */
3146 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3147 n = identify_remember (current_ir_graph->value_table, n);
3155 * These optimizations never deallocate nodes (in place). This can cause dead
3156 * nodes lying on the obstack. Remove these by a dead node elimination,
3157 * i.e., a copying garbage collection.
3160 optimize_in_place_2 (ir_node *n)
3164 opcode iro = get_irn_opcode(n);
3166 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3168 /* constant expression evaluation / constant folding */
3169 if (get_opt_constant_folding()) {
3170 /* neither constants nor Tuple values can be evaluated */
3171 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3172 /* try to evaluate */
3173 tv = computed_value(n);
3174 if (tv != tarval_bad) {
3175 /* evaluation was successful -- replace the node. */
3176 type *old_tp = get_irn_type(n);
3177 int i, arity = get_irn_arity(n);
3180 * Try to recover the type of the new expression.
3182 for (i = 0; i < arity && !old_tp; ++i)
3183 old_tp = get_irn_type(get_irn_n(n, i));
3185 n = new_Const(get_tarval_mode(tv), tv);
3187 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3188 set_Const_type(n, old_tp);
3190 DBG_OPT_CSTEVAL(oldn, n);
3196 /* remove unnecessary nodes */
3197 if (get_opt_constant_folding() ||
3198 (iro == iro_Phi) || /* always optimize these nodes. */
3199 (iro == iro_Id) || /* ... */
3200 (iro == iro_Proj) || /* ... */
3201 (iro == iro_Block) ) /* Flags tested local. */
3202 n = equivalent_node(n);
3204 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3206 /** common subexpression elimination **/
3207 /* Checks whether n is already available. */
3208 /* The block input is used to distinguish different subexpressions. Right
3209 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3210 subexpressions within a block. */
3211 if (get_opt_cse()) {
3212 n = identify(current_ir_graph->value_table, n);
3215 /* Some more constant expression evaluation. */
3216 iro = get_irn_opcode(n);
3217 if (get_opt_constant_folding() ||
3218 (iro == iro_Cond) ||
3219 (iro == iro_Proj) ||
3220 (iro == iro_Sel)) /* Flags tested local. */
3221 n = transform_node(n);
3223 /* Remove nodes with dead (Bad) input.
3224 Run always for transformation induced Bads. */
3227 /* Now we can verify the node, as it has no dead inputs any more. */
3230 /* Now we have a legal, useful node. Enter it in hash table for cse.
3231 Blocks should be unique anyways. (Except the successor of start:
3232 is cse with the start block!) */
3233 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3234 n = identify_remember(current_ir_graph->value_table, n);
3240 * Wrapper for external use, set proper status bits after optimization.
3243 optimize_in_place (ir_node *n)
3245 /* Handle graph state */
3246 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3248 if (get_opt_global_cse())
3249 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3250 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3251 set_irg_outs_inconsistent(current_ir_graph);
3253 /* Maybe we could also test whether optimizing the node can
3254 change the control graph. */
3255 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
3256 set_irg_dom_inconsistent(current_ir_graph);
3257 return optimize_in_place_2 (n);
3261 * set the default ir op operations
3263 ir_op *firm_set_default_operations(ir_op *op)
3265 op = firm_set_default_computed_value(op);
3266 op = firm_set_default_equivalent_node(op);
3267 op = firm_set_default_transform_node(op);
3268 op = firm_set_default_node_cmp_attr(op);
3269 op = firm_set_default_get_type(op);