2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Returns the tarval of a Const node or tarval_bad for all other nodes.
59 static tarval *default_value_of(const ir_node *n) {
61 return get_Const_tarval(n); /* might return tarval_bad */
66 value_of_func value_of_ptr = default_value_of;
68 void set_value_of_func(value_of_func func) {
72 value_of_ptr = default_value_of;
76 * Return the value of a Constant.
78 static tarval *computed_value_Const(ir_node *n) {
79 return get_Const_tarval(n);
80 } /* computed_value_Const */
83 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
85 static tarval *computed_value_SymConst(ir_node *n) {
89 switch (get_SymConst_kind(n)) {
90 case symconst_type_size:
91 type = get_SymConst_type(n);
92 if (get_type_state(type) == layout_fixed)
93 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
95 case symconst_type_align:
96 type = get_SymConst_type(n);
97 if (get_type_state(type) == layout_fixed)
98 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
100 case symconst_ofs_ent:
101 ent = get_SymConst_entity(n);
102 type = get_entity_owner(ent);
103 if (get_type_state(type) == layout_fixed)
104 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
110 } /* computed_value_SymConst */
113 * Return the value of an Add.
115 static tarval *computed_value_Add(ir_node *n) {
116 ir_node *a = get_Add_left(n);
117 ir_node *b = get_Add_right(n);
119 tarval *ta = value_of(a);
120 tarval *tb = value_of(b);
122 if ((ta != tarval_bad) && (tb != tarval_bad))
123 return tarval_add(ta, tb);
126 } /* computed_value_Add */
129 * Return the value of a Sub.
130 * Special case: a - a
132 static tarval *computed_value_Sub(ir_node *n) {
133 ir_mode *mode = get_irn_mode(n);
134 ir_node *a = get_Sub_left(n);
135 ir_node *b = get_Sub_right(n);
140 if (a == b && !is_Bad(a))
141 return get_mode_null(mode);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_sub(ta, tb, mode);
150 } /* computed_value_Sub */
153 * Return the value of a Carry.
154 * Special : a op 0, 0 op b
156 static tarval *computed_value_Carry(ir_node *n) {
157 ir_node *a = get_binop_left(n);
158 ir_node *b = get_binop_right(n);
159 ir_mode *m = get_irn_mode(n);
161 tarval *ta = value_of(a);
162 tarval *tb = value_of(b);
164 if ((ta != tarval_bad) && (tb != tarval_bad)) {
166 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
168 if (tarval_is_null(ta) || tarval_is_null(tb))
169 return get_mode_null(m);
172 } /* computed_value_Carry */
175 * Return the value of a Borrow.
178 static tarval *computed_value_Borrow(ir_node *n) {
179 ir_node *a = get_binop_left(n);
180 ir_node *b = get_binop_right(n);
181 ir_mode *m = get_irn_mode(n);
183 tarval *ta = value_of(a);
184 tarval *tb = value_of(b);
186 if ((ta != tarval_bad) && (tb != tarval_bad)) {
187 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
188 } else if (tarval_is_null(ta)) {
189 return get_mode_null(m);
192 } /* computed_value_Borrow */
195 * Return the value of an unary Minus.
197 static tarval *computed_value_Minus(ir_node *n) {
198 ir_node *a = get_Minus_op(n);
199 tarval *ta = value_of(a);
201 if (ta != tarval_bad)
202 return tarval_neg(ta);
205 } /* computed_value_Minus */
208 * Return the value of a Mul.
210 static tarval *computed_value_Mul(ir_node *n) {
211 ir_node *a = get_Mul_left(n);
212 ir_node *b = get_Mul_right(n);
215 tarval *ta = value_of(a);
216 tarval *tb = value_of(b);
218 mode = get_irn_mode(n);
219 if (mode != get_irn_mode(a)) {
220 /* n * n = 2n bit multiplication */
221 ta = tarval_convert_to(ta, mode);
222 tb = tarval_convert_to(tb, mode);
225 if (ta != tarval_bad && tb != tarval_bad) {
226 return tarval_mul(ta, tb);
228 /* a*0 = 0 or 0*b = 0 */
229 if (ta == get_mode_null(mode))
231 if (tb == get_mode_null(mode))
235 } /* computed_value_Mul */
238 * Return the value of an Abs.
240 static tarval *computed_value_Abs(ir_node *n) {
241 ir_node *a = get_Abs_op(n);
242 tarval *ta = value_of(a);
244 if (ta != tarval_bad)
245 return tarval_abs(ta);
248 } /* computed_value_Abs */
251 * Return the value of an And.
252 * Special case: a & 0, 0 & b
254 static tarval *computed_value_And(ir_node *n) {
255 ir_node *a = get_And_left(n);
256 ir_node *b = get_And_right(n);
258 tarval *ta = value_of(a);
259 tarval *tb = value_of(b);
261 if ((ta != tarval_bad) && (tb != tarval_bad)) {
262 return tarval_and (ta, tb);
264 if (tarval_is_null(ta)) return ta;
265 if (tarval_is_null(tb)) return tb;
268 } /* computed_value_And */
271 * Return the value of an Or.
272 * Special case: a | 1...1, 1...1 | b
274 static tarval *computed_value_Or(ir_node *n) {
275 ir_node *a = get_Or_left(n);
276 ir_node *b = get_Or_right(n);
278 tarval *ta = value_of(a);
279 tarval *tb = value_of(b);
281 if ((ta != tarval_bad) && (tb != tarval_bad)) {
282 return tarval_or (ta, tb);
284 if (tarval_is_all_one(ta)) return ta;
285 if (tarval_is_all_one(tb)) return tb;
288 } /* computed_value_Or */
291 * Return the value of an Eor.
293 static tarval *computed_value_Eor(ir_node *n) {
294 ir_node *a = get_Eor_left(n);
295 ir_node *b = get_Eor_right(n);
300 return get_mode_null(get_irn_mode(n));
305 if ((ta != tarval_bad) && (tb != tarval_bad)) {
306 return tarval_eor (ta, tb);
309 } /* computed_value_Eor */
312 * Return the value of a Not.
314 static tarval *computed_value_Not(ir_node *n) {
315 ir_node *a = get_Not_op(n);
316 tarval *ta = value_of(a);
318 if (ta != tarval_bad)
319 return tarval_not(ta);
322 } /* computed_value_Not */
325 * Return the value of a Shl.
327 static tarval *computed_value_Shl(ir_node *n) {
328 ir_node *a = get_Shl_left(n);
329 ir_node *b = get_Shl_right(n);
331 tarval *ta = value_of(a);
332 tarval *tb = value_of(b);
334 if ((ta != tarval_bad) && (tb != tarval_bad)) {
335 return tarval_shl (ta, tb);
338 } /* computed_value_Shl */
341 * Return the value of a Shr.
343 static tarval *computed_value_Shr(ir_node *n) {
344 ir_node *a = get_Shr_left(n);
345 ir_node *b = get_Shr_right(n);
347 tarval *ta = value_of(a);
348 tarval *tb = value_of(b);
350 if ((ta != tarval_bad) && (tb != tarval_bad)) {
351 return tarval_shr (ta, tb);
354 } /* computed_value_Shr */
357 * Return the value of a Shrs.
359 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);
370 } /* computed_value_Shrs */
373 * Return the value of a Rotl.
375 static tarval *computed_value_Rotl(ir_node *n) {
376 ir_node *a = get_Rotl_left(n);
377 ir_node *b = get_Rotl_right(n);
379 tarval *ta = value_of(a);
380 tarval *tb = value_of(b);
382 if ((ta != tarval_bad) && (tb != tarval_bad)) {
383 return tarval_rotl(ta, tb);
386 } /* computed_value_Rotl */
389 * Return the value of a Conv.
391 static tarval *computed_value_Conv(ir_node *n) {
392 ir_node *a = get_Conv_op(n);
393 tarval *ta = value_of(a);
395 if (ta != tarval_bad)
396 return tarval_convert_to(ta, get_irn_mode(n));
399 } /* computed_value_Conv */
402 * Return the value of a Proj(Cmp).
404 * This performs a first step of unreachable code elimination.
405 * Proj can not be computed, but folding a Cmp above the Proj here is
406 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
408 * There are several case where we can evaluate a Cmp node, see later.
410 static tarval *computed_value_Proj_Cmp(ir_node *n) {
411 ir_node *a = get_Proj_pred(n);
412 ir_node *aa = get_Cmp_left(a);
413 ir_node *ab = get_Cmp_right(a);
414 long proj_nr = get_Proj_proj(n);
417 * BEWARE: a == a is NOT always True for floating Point values, as
418 * NaN != NaN is defined, so we must check this here.
421 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
424 /* This is a trick with the bits used for encoding the Cmp
425 Proj numbers, the following statement is not the same:
426 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
427 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
430 tarval *taa = value_of(aa);
431 tarval *tab = value_of(ab);
432 ir_mode *mode = get_irn_mode(aa);
435 * The predecessors of Cmp are target values. We can evaluate
438 if ((taa != tarval_bad) && (tab != tarval_bad)) {
439 /* strange checks... */
440 pn_Cmp flags = tarval_cmp(taa, tab);
441 if (flags != pn_Cmp_False) {
442 return new_tarval_from_long (proj_nr & flags, mode_b);
445 /* for integer values, we can check against MIN/MAX */
446 else if (mode_is_int(mode)) {
447 /* MIN <=/> x. This results in true/false. */
448 if (taa == get_mode_min(mode)) {
449 /* a compare with the MIN value */
450 if (proj_nr == pn_Cmp_Le)
451 return get_tarval_b_true();
452 else if (proj_nr == pn_Cmp_Gt)
453 return get_tarval_b_false();
455 /* x >=/< MIN. This results in true/false. */
457 if (tab == get_mode_min(mode)) {
458 /* a compare with the MIN value */
459 if (proj_nr == pn_Cmp_Ge)
460 return get_tarval_b_true();
461 else if (proj_nr == pn_Cmp_Lt)
462 return get_tarval_b_false();
464 /* MAX >=/< x. This results in true/false. */
465 else if (taa == get_mode_max(mode)) {
466 if (proj_nr == pn_Cmp_Ge)
467 return get_tarval_b_true();
468 else if (proj_nr == pn_Cmp_Lt)
469 return get_tarval_b_false();
471 /* x <=/> MAX. This results in true/false. */
472 else if (tab == get_mode_max(mode)) {
473 if (proj_nr == pn_Cmp_Le)
474 return get_tarval_b_true();
475 else if (proj_nr == pn_Cmp_Gt)
476 return get_tarval_b_false();
480 * The predecessors are Allocs or (void*)(0) constants. Allocs never
481 * return NULL, they raise an exception. Therefore we can predict
485 ir_node *aaa = skip_Id(skip_Proj(aa));
486 ir_node *aba = skip_Id(skip_Proj(ab));
488 if ( ( (/* aa is ProjP and aaa is Alloc */
490 && mode_is_reference(get_irn_mode(aa))
492 && ( (/* ab is NULL */
494 && mode_is_reference(get_irn_mode(ab))
495 && is_Const_null(ab))
496 || (/* ab is other Alloc */
498 && mode_is_reference(get_irn_mode(ab))
501 || (/* aa is NULL and aba is Alloc */
503 && mode_is_reference(get_irn_mode(aa))
506 && mode_is_reference(get_irn_mode(ab))
509 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
512 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
513 } /* computed_value_Proj_Cmp */
516 * Return the value of a floating point Quot.
518 static tarval *do_computed_value_Quot(ir_node *a, ir_node *b) {
519 tarval *ta = value_of(a);
520 tarval *tb = value_of(b);
522 /* cannot optimize 0 / b = 0 because of NaN */
523 if (ta != tarval_bad && tb != tarval_bad)
524 return tarval_quo(ta, tb);
526 } /* do_computed_value_Quot */
529 * Calculate the value of an integer Div of two nodes.
530 * Special case: 0 / b
532 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
533 tarval *ta = value_of(a);
537 /* Compute c1 / c2 or 0 / a, a != 0 */
538 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
539 return ta; /* 0 / b == 0 */
541 if (ta != tarval_bad && tb != tarval_bad)
542 return tarval_div(ta, tb);
544 } /* do_computed_value_Div */
547 * Calculate the value of an integer Mod of two nodes.
548 * Special case: a % 1
550 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
551 tarval *ta = value_of(a);
552 tarval *tb = value_of(b);
554 /* Compute a % 1 or c1 % c2 */
555 if (tarval_is_one(tb))
556 return get_mode_null(get_irn_mode(a));
557 if (ta != tarval_bad && tb != tarval_bad)
558 return tarval_mod(ta, tb);
560 } /* do_computed_value_Mod */
564 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565 * Proj(DivMod) and Proj(Quot).
567 static tarval *computed_value_Proj(ir_node *n) {
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
595 if (get_Proj_proj(n) == pn_Quot_res)
596 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
603 } /* computed_value_Proj */
606 * Calculate the value of a Mux: can be evaluated, if the
607 * sel and the right input are known.
609 static tarval *computed_value_Mux(ir_node *n) {
610 ir_node *sel = get_Mux_sel(n);
611 tarval *ts = value_of(sel);
613 if (ts == get_tarval_b_true()) {
614 ir_node *v = get_Mux_true(n);
617 else if (ts == get_tarval_b_false()) {
618 ir_node *v = get_Mux_false(n);
622 } /* computed_value_Mux */
625 * Calculate the value of a Psi: can be evaluated, if a condition is true
626 * and all previous conditions are false. If all conditions are false
627 * we evaluate to the default one.
629 static tarval *computed_value_Psi(ir_node *n) {
631 return computed_value_Mux(n);
633 } /* computed_value_Psi */
636 * Calculate the value of a Confirm: can be evaluated,
637 * if it has the form Confirm(x, '=', Const).
639 static tarval *computed_value_Confirm(ir_node *n) {
641 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
642 * Do NOT optimize them away (CondEval wants them), so wait until
643 * remove_confirm is activated.
645 if (get_opt_remove_confirm()) {
646 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
647 tarval *tv = value_of(get_Confirm_bound(n));
648 if (tv != tarval_bad)
652 return value_of(get_Confirm_value(n));
653 } /* computed_value_Confirm */
656 * If the parameter n can be computed, return its value, else tarval_bad.
657 * Performs constant folding.
659 * @param n The node this should be evaluated
661 tarval *computed_value(ir_node *n) {
662 if (n->op->ops.computed_value)
663 return n->op->ops.computed_value(n);
665 } /* computed_value */
668 * Set the default computed_value evaluator in an ir_op_ops.
670 * @param code the opcode for the default operation
671 * @param ops the operations initialized
676 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
680 ops->computed_value = computed_value_##a; \
712 } /* firm_set_default_computed_value */
715 * Returns a equivalent block for another block.
716 * If the block has only one predecessor, this is
717 * the equivalent one. If the only predecessor of a block is
718 * the block itself, this is a dead block.
720 * If both predecessors of a block are the branches of a binary
721 * Cond, the equivalent block is Cond's block.
723 * If all predecessors of a block are bad or lies in a dead
724 * block, the current block is dead as well.
726 * Note, that blocks are NEVER turned into Bad's, instead
727 * the dead_block flag is set. So, never test for is_Bad(block),
728 * always use is_dead_Block(block).
730 static ir_node *equivalent_node_Block(ir_node *n)
735 /* don't optimize dead blocks */
736 if (is_Block_dead(n))
739 n_preds = get_Block_n_cfgpreds(n);
741 /* The Block constructor does not call optimize, but mature_immBlock()
742 calls the optimization. */
743 assert(get_Block_matured(n));
745 /* Straightening: a single entry Block following a single exit Block
746 can be merged, if it is not the Start block. */
747 /* !!! Beware, all Phi-nodes of n must have been optimized away.
748 This should be true, as the block is matured before optimize is called.
749 But what about Phi-cycles with the Phi0/Id that could not be resolved?
750 Remaining Phi nodes are just Ids. */
751 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
752 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
753 if (predblock == oldn) {
754 /* Jmp jumps into the block it is in -- deal self cycle. */
755 n = set_Block_dead(n);
756 DBG_OPT_DEAD_BLOCK(oldn, n);
757 } else if (get_opt_control_flow_straightening()) {
759 DBG_OPT_STG(oldn, n);
761 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
762 ir_node *predblock = get_Block_cfgpred_block(n, 0);
763 if (predblock == oldn) {
764 /* Jmp jumps into the block it is in -- deal self cycle. */
765 n = set_Block_dead(n);
766 DBG_OPT_DEAD_BLOCK(oldn, n);
768 } else if ((n_preds == 2) &&
769 (get_opt_control_flow_weak_simplification())) {
770 /* Test whether Cond jumps twice to this block
771 * The more general case which more than 2 predecessors is handles
772 * in optimize_cf(), we handle only this special case for speed here.
774 ir_node *a = get_Block_cfgpred(n, 0);
775 ir_node *b = get_Block_cfgpred(n, 1);
779 (get_Proj_pred(a) == get_Proj_pred(b)) &&
780 is_Cond(get_Proj_pred(a)) &&
781 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
782 /* Also a single entry Block following a single exit Block. Phis have
783 twice the same operand and will be optimized away. */
784 n = get_nodes_block(get_Proj_pred(a));
785 DBG_OPT_IFSIM1(oldn, a, b, n);
787 } else if (get_opt_unreachable_code() &&
788 (n != get_irg_start_block(current_ir_graph)) &&
789 (n != get_irg_end_block(current_ir_graph)) ) {
792 /* If all inputs are dead, this block is dead too, except if it is
793 the start or end block. This is one step of unreachable code
795 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
796 ir_node *pred = get_Block_cfgpred(n, i);
799 if (is_Bad(pred)) continue;
800 pred_blk = get_nodes_block(skip_Proj(pred));
802 if (is_Block_dead(pred_blk)) continue;
805 /* really found a living input */
810 n = set_Block_dead(n);
811 DBG_OPT_DEAD_BLOCK(oldn, n);
816 } /* equivalent_node_Block */
819 * Returns a equivalent node for a Jmp, a Bad :-)
820 * Of course this only happens if the Block of the Jmp is dead.
822 static ir_node *equivalent_node_Jmp(ir_node *n) {
823 /* unreachable code elimination */
824 if (is_Block_dead(get_nodes_block(n)))
828 } /* equivalent_node_Jmp */
830 /** Raise is handled in the same way as Jmp. */
831 #define equivalent_node_Raise equivalent_node_Jmp
834 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
835 See transform_node_Proj_Cond(). */
838 * Optimize operations that are commutative and have neutral 0,
839 * so a op 0 = 0 op a = a.
841 static ir_node *equivalent_node_neutral_zero(ir_node *n)
845 ir_node *a = get_binop_left(n);
846 ir_node *b = get_binop_right(n);
851 /* After running compute_node there is only one constant predecessor.
852 Find this predecessors value and remember the other node: */
853 if ((tv = value_of(a)) != tarval_bad) {
855 } else if ((tv = value_of(b)) != tarval_bad) {
860 /* If this predecessors constant value is zero, the operation is
861 * unnecessary. Remove it.
863 * Beware: If n is a Add, the mode of on and n might be different
864 * which happens in this rare construction: NULL + 3.
865 * Then, a Conv would be needed which we cannot include here.
867 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
870 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
874 } /* equivalent_node_neutral_zero */
877 * Eor is commutative and has neutral 0.
879 static ir_node *equivalent_node_Eor(ir_node *n)
885 n = equivalent_node_neutral_zero(n);
886 if (n != oldn) return n;
889 b = get_Eor_right(n);
892 ir_node *aa = get_Eor_left(a);
893 ir_node *ab = get_Eor_right(a);
896 /* (a ^ b) ^ a -> b */
898 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
900 } else if (ab == b) {
901 /* (a ^ b) ^ b -> a */
903 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
908 ir_node *ba = get_Eor_left(b);
909 ir_node *bb = get_Eor_right(b);
912 /* a ^ (a ^ b) -> b */
914 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
916 } else if (bb == a) {
917 /* a ^ (b ^ a) -> b */
919 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
928 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
930 * The second one looks strange, but this construct
931 * is used heavily in the LCC sources :-).
933 * Beware: The Mode of an Add may be different than the mode of its
934 * predecessors, so we could not return a predecessors in all cases.
936 static ir_node *equivalent_node_Add(ir_node *n) {
938 ir_node *left, *right;
939 ir_mode *mode = get_irn_mode(n);
941 n = equivalent_node_neutral_zero(n);
945 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
946 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
949 left = get_Add_left(n);
950 right = get_Add_right(n);
953 if (get_Sub_right(left) == right) {
956 n = get_Sub_left(left);
957 if (mode == get_irn_mode(n)) {
958 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
964 if (get_Sub_right(right) == left) {
967 n = get_Sub_left(right);
968 if (mode == get_irn_mode(n)) {
969 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
975 } /* equivalent_node_Add */
978 * optimize operations that are not commutative but have neutral 0 on left,
981 static ir_node *equivalent_node_left_zero(ir_node *n) {
984 ir_node *a = get_binop_left(n);
985 ir_node *b = get_binop_right(n);
986 tarval *tb = value_of(b);
988 if (tarval_is_null(tb)) {
991 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
994 } /* equivalent_node_left_zero */
996 #define equivalent_node_Shl equivalent_node_left_zero
997 #define equivalent_node_Shr equivalent_node_left_zero
998 #define equivalent_node_Shrs equivalent_node_left_zero
999 #define equivalent_node_Rotl equivalent_node_left_zero
1002 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1004 * The second one looks strange, but this construct
1005 * is used heavily in the LCC sources :-).
1007 * Beware: The Mode of a Sub may be different than the mode of its
1008 * predecessors, so we could not return a predecessors in all cases.
1010 static ir_node *equivalent_node_Sub(ir_node *n) {
1013 ir_mode *mode = get_irn_mode(n);
1016 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1017 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1020 b = get_Sub_right(n);
1023 /* Beware: modes might be different */
1024 if (tarval_is_null(tb)) {
1025 ir_node *a = get_Sub_left(n);
1026 if (mode == get_irn_mode(a)) {
1029 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1033 } /* equivalent_node_Sub */
1037 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1040 * -(-a) == a, but might overflow two times.
1041 * We handle it anyway here but the better way would be a
1042 * flag. This would be needed for Pascal for instance.
1044 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1046 ir_node *pred = get_unop_op(n);
1048 /* optimize symmetric unop */
1049 if (get_irn_op(pred) == get_irn_op(n)) {
1050 n = get_unop_op(pred);
1051 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1054 } /* equivalent_node_idempotent_unop */
1056 /** Optimize Not(Not(x)) == x. */
1057 #define equivalent_node_Not equivalent_node_idempotent_unop
1059 /** -(-x) == x ??? Is this possible or can --x raise an
1060 out of bounds exception if min =! max? */
1061 #define equivalent_node_Minus equivalent_node_idempotent_unop
1064 * Optimize a * 1 = 1 * a = a.
1066 static ir_node *equivalent_node_Mul(ir_node *n) {
1068 ir_node *a = get_Mul_left(n);
1070 /* we can handle here only the n * n = n bit cases */
1071 if (get_irn_mode(n) == get_irn_mode(a)) {
1072 ir_node *b = get_Mul_right(n);
1076 * Mul is commutative and has again an other neutral element.
1077 * Constants are place right, so check this case first.
1080 if (tarval_is_one(tv)) {
1082 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1085 if (tarval_is_one(tv)) {
1087 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1092 } /* equivalent_node_Mul */
1095 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1097 static ir_node *equivalent_node_Or(ir_node *n) {
1100 ir_node *a = get_Or_left(n);
1101 ir_node *b = get_Or_right(n);
1104 n = a; /* Or has it's own neutral element */
1105 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1106 } else if (is_Const(a) && is_Const_null(a)) {
1108 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1109 } else if (is_Const(b) && is_Const_null(b)) {
1111 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 } /* equivalent_node_Or */
1118 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1120 static ir_node *equivalent_node_And(ir_node *n) {
1123 ir_node *a = get_And_left(n);
1124 ir_node *b = get_And_right(n);
1127 n = a; /* And has it's own neutral element */
1128 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1131 if (is_Const(a) && is_Const_all_one(a)) {
1133 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1136 if (is_Const(b) && is_Const_all_one(b)) {
1138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1142 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1145 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1150 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1153 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1159 } /* equivalent_node_And */
1162 * Try to remove useless Conv's:
1164 static ir_node *equivalent_node_Conv(ir_node *n) {
1166 ir_node *a = get_Conv_op(n);
1168 ir_mode *n_mode = get_irn_mode(n);
1169 ir_mode *a_mode = get_irn_mode(a);
1172 if (n_mode == a_mode) { /* No Conv necessary */
1173 if (get_Conv_strict(n)) {
1174 /* special case: the predecessor might be a also a Conv */
1176 if (! get_Conv_strict(a)) {
1177 /* first one is not strict, kick it */
1179 a_mode = get_irn_mode(a);
1183 /* else both are strict conv, second is superfluous */
1184 } else if (is_Proj(a)) {
1185 ir_node *pred = get_Proj_pred(a);
1186 if (is_Load(pred)) {
1187 /* loads always return with the exact precision of n_mode */
1188 assert(get_Load_mode(pred) == n_mode);
1193 /* leave strict floating point Conv's */
1197 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1198 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1199 ir_node *b = get_Conv_op(a);
1200 ir_mode *b_mode = get_irn_mode(b);
1202 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1203 /* both are strict conv */
1204 if (smaller_mode(a_mode, n_mode)) {
1205 /* both are strict, but the first is smaller, so
1206 the second cannot remove more precision, remove the
1208 set_Conv_strict(n, 0);
1211 if (n_mode == b_mode) {
1212 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1213 if (n_mode == mode_b) {
1214 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1216 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1217 if (smaller_mode(b_mode, a_mode)) {
1218 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1219 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1223 if (mode_is_int(n_mode) && mode_is_float(a_mode)) {
1224 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1225 size_t int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1226 size_t float_mantissa;
1227 /* FIXME There is no way to get the mantissa size of a mode */
1228 switch (get_mode_size_bits(a_mode)) {
1229 case 32: float_mantissa = 23 + 1; break; // + 1 for implicit 1
1230 case 64: float_mantissa = 52 + 1; break;
1231 case 80: float_mantissa = 64 + 1; break;
1232 default: float_mantissa = 0; break;
1234 if (float_mantissa != 0 && float_mantissa >= int_mantissa) {
1236 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1241 if (smaller_mode(b_mode, a_mode)) {
1242 if (get_Conv_strict(n))
1243 set_Conv_strict(b, 1);
1244 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1245 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1251 } /* equivalent_node_Conv */
1254 * A Cast may be removed if the type of the previous node
1255 * is already the type of the Cast.
1257 static ir_node *equivalent_node_Cast(ir_node *n) {
1259 ir_node *pred = get_Cast_op(n);
1261 if (get_irn_type(pred) == get_Cast_type(n)) {
1263 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1266 } /* equivalent_node_Cast */
1269 * Several optimizations:
1270 * - no Phi in start block.
1271 * - remove Id operators that are inputs to Phi
1272 * - fold Phi-nodes, iff they have only one predecessor except
1275 static ir_node *equivalent_node_Phi(ir_node *n) {
1280 ir_node *first_val = NULL; /* to shutup gcc */
1282 if (!get_opt_normalize()) return n;
1284 n_preds = get_Phi_n_preds(n);
1286 block = get_nodes_block(n);
1287 if ((is_Block_dead(block)) || /* Control dead */
1288 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1289 return new_Bad(); /* in the Start Block. */
1291 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1293 /* If the Block has a Bad pred, we also have one. */
1294 for (i = 0; i < n_preds; ++i)
1295 if (is_Bad(get_Block_cfgpred(block, i)))
1296 set_Phi_pred(n, i, new_Bad());
1298 /* Find first non-self-referencing input */
1299 for (i = 0; i < n_preds; ++i) {
1300 first_val = get_Phi_pred(n, i);
1301 if ( (first_val != n) /* not self pointer */
1303 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1304 * predecessors. Then, Phi nodes in dead code might be removed, causing
1305 * nodes pointing to themself (Add's for instance).
1306 * This is really bad and causes endless recursions in several
1307 * code pathes, so we do NOT optimize such a code.
1308 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1309 * (and bad Phi predecessors), so live code is optimized later.
1311 && (! is_Bad(first_val))
1313 ) { /* value not dead */
1314 break; /* then found first value. */
1319 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1323 /* search for rest of inputs, determine if any of these
1324 are non-self-referencing */
1325 while (++i < n_preds) {
1326 ir_node *scnd_val = get_Phi_pred(n, i);
1327 if ( (scnd_val != n)
1328 && (scnd_val != first_val)
1331 && (! is_Bad(scnd_val))
1339 /* Fold, if no multiple distinct non-self-referencing inputs */
1341 DBG_OPT_PHI(oldn, n);
1344 } /* equivalent_node_Phi */
1347 * Several optimizations:
1348 * - no Sync in start block.
1349 * - fold Sync-nodes, iff they have only one predecessor except
1352 static ir_node *equivalent_node_Sync(ir_node *n) {
1353 int arity = get_Sync_n_preds(n);
1356 for (i = 0; i < arity;) {
1357 ir_node *pred = get_Sync_pred(n, i);
1360 /* Remove Bad predecessors */
1367 /* Remove duplicate predecessors */
1373 if (get_Sync_pred(n, j) == pred) {
1381 if (arity == 0) return new_Bad();
1382 if (arity == 1) return get_Sync_pred(n, 0);
1384 } /* equivalent_node_Sync */
1387 * Optimize Proj(Tuple).
1389 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1390 ir_node *oldn = proj;
1391 ir_node *tuple = get_Proj_pred(proj);
1393 /* Remove the Tuple/Proj combination. */
1394 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1395 DBG_OPT_TUPLE(oldn, tuple, proj);
1398 } /* equivalent_node_Proj_Tuple */
1401 * Optimize a / 1 = a.
1403 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1404 ir_node *oldn = proj;
1405 ir_node *div = get_Proj_pred(proj);
1406 ir_node *b = get_Div_right(div);
1407 tarval *tb = value_of(b);
1409 /* Div is not commutative. */
1410 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1411 switch (get_Proj_proj(proj)) {
1413 proj = get_Div_mem(div);
1414 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1418 proj = get_Div_left(div);
1419 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1423 /* we cannot replace the exception Proj's here, this is done in
1424 transform_node_Proj_Div() */
1429 } /* equivalent_node_Proj_Div */
1432 * Optimize a / 1.0 = a.
1434 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1435 ir_node *oldn = proj;
1436 ir_node *quot = get_Proj_pred(proj);
1437 ir_node *b = get_Quot_right(quot);
1438 tarval *tb = value_of(b);
1440 /* Div is not commutative. */
1441 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1442 switch (get_Proj_proj(proj)) {
1444 proj = get_Quot_mem(quot);
1445 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1449 proj = get_Quot_left(quot);
1450 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1454 /* we cannot replace the exception Proj's here, this is done in
1455 transform_node_Proj_Quot() */
1460 } /* equivalent_node_Proj_Quot */
1463 * Optimize a / 1 = a.
1465 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1466 ir_node *oldn = proj;
1467 ir_node *divmod = get_Proj_pred(proj);
1468 ir_node *b = get_DivMod_right(divmod);
1469 tarval *tb = value_of(b);
1471 /* Div is not commutative. */
1472 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1473 switch (get_Proj_proj(proj)) {
1475 proj = get_DivMod_mem(divmod);
1476 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1479 case pn_DivMod_res_div:
1480 proj = get_DivMod_left(divmod);
1481 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1485 /* we cannot replace the exception Proj's here, this is done in
1486 transform_node_Proj_DivMod().
1487 Note further that the pn_DivMod_res_div case is handled in
1488 computed_value_Proj(). */
1493 } /* equivalent_node_Proj_DivMod */
1496 * Optimize CopyB(mem, x, x) into a Nop.
1498 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1499 ir_node *oldn = proj;
1500 ir_node *copyb = get_Proj_pred(proj);
1501 ir_node *a = get_CopyB_dst(copyb);
1502 ir_node *b = get_CopyB_src(copyb);
1505 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1506 switch (get_Proj_proj(proj)) {
1507 case pn_CopyB_M_regular:
1508 proj = get_CopyB_mem(copyb);
1509 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1512 case pn_CopyB_M_except:
1513 case pn_CopyB_X_except:
1514 DBG_OPT_EXC_REM(proj);
1515 proj = get_irg_bad(current_ir_graph);
1520 } /* equivalent_node_Proj_CopyB */
1523 * Optimize Bounds(idx, idx, upper) into idx.
1525 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1526 ir_node *oldn = proj;
1527 ir_node *bound = get_Proj_pred(proj);
1528 ir_node *idx = get_Bound_index(bound);
1529 ir_node *pred = skip_Proj(idx);
1532 if (idx == get_Bound_lower(bound))
1534 else if (is_Bound(pred)) {
1536 * idx was Bounds checked in the same MacroBlock previously,
1537 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1539 ir_node *lower = get_Bound_lower(bound);
1540 ir_node *upper = get_Bound_upper(bound);
1541 if (get_Bound_lower(pred) == lower &&
1542 get_Bound_upper(pred) == upper &&
1543 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1545 * One could expect that we simply return the previous
1546 * Bound here. However, this would be wrong, as we could
1547 * add an exception Proj to a new location then.
1548 * So, we must turn in into a tuple.
1554 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1555 switch (get_Proj_proj(proj)) {
1557 DBG_OPT_EXC_REM(proj);
1558 proj = get_Bound_mem(bound);
1560 case pn_Bound_X_except:
1561 DBG_OPT_EXC_REM(proj);
1562 proj = get_irg_bad(current_ir_graph);
1566 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1569 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1574 } /* equivalent_node_Proj_Bound */
1577 * Optimize an Exception Proj(Load) with a non-null address.
1579 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1580 if (get_opt_ldst_only_null_ptr_exceptions()) {
1581 if (get_irn_mode(proj) == mode_X) {
1582 ir_node *load = get_Proj_pred(proj);
1584 /* get the Load address */
1585 ir_node *addr = get_Load_ptr(load);
1588 if (value_not_null(addr, &confirm)) {
1589 if (get_Proj_proj(proj) == pn_Load_X_except) {
1590 DBG_OPT_EXC_REM(proj);
1591 return get_irg_bad(current_ir_graph);
1597 } /* equivalent_node_Proj_Load */
1600 * Optimize an Exception Proj(Store) with a non-null address.
1602 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1603 if (get_opt_ldst_only_null_ptr_exceptions()) {
1604 if (get_irn_mode(proj) == mode_X) {
1605 ir_node *store = get_Proj_pred(proj);
1607 /* get the load/store address */
1608 ir_node *addr = get_Store_ptr(store);
1611 if (value_not_null(addr, &confirm)) {
1612 if (get_Proj_proj(proj) == pn_Store_X_except) {
1613 DBG_OPT_EXC_REM(proj);
1614 return get_irg_bad(current_ir_graph);
1620 } /* equivalent_node_Proj_Store */
1623 * Does all optimizations on nodes that must be done on it's Proj's
1624 * because of creating new nodes.
1626 static ir_node *equivalent_node_Proj(ir_node *proj) {
1627 ir_node *n = get_Proj_pred(proj);
1629 switch (get_irn_opcode(n)) {
1631 return equivalent_node_Proj_Div(proj);
1634 return equivalent_node_Proj_DivMod(proj);
1637 return equivalent_node_Proj_Quot(proj);
1640 return equivalent_node_Proj_Tuple(proj);
1643 return equivalent_node_Proj_CopyB(proj);
1646 return equivalent_node_Proj_Bound(proj);
1649 return equivalent_node_Proj_Load(proj);
1652 return equivalent_node_Proj_Store(proj);
1655 if (get_irn_mode(proj) == mode_X) {
1656 if (is_Block_dead(get_nodes_block(n))) {
1657 /* Remove dead control flow -- early gigo(). */
1658 proj = get_irg_bad(current_ir_graph);
1663 } /* equivalent_node_Proj */
1668 static ir_node *equivalent_node_Id(ir_node *n) {
1675 DBG_OPT_ID(oldn, n);
1677 } /* equivalent_node_Id */
1682 static ir_node *equivalent_node_Mux(ir_node *n)
1684 ir_node *oldn = n, *sel = get_Mux_sel(n);
1685 tarval *ts = value_of(sel);
1687 /* Mux(true, f, t) == t */
1688 if (ts == tarval_b_true) {
1689 n = get_Mux_true(n);
1690 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1692 /* Mux(false, f, t) == f */
1693 else if (ts == tarval_b_false) {
1694 n = get_Mux_false(n);
1695 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1697 /* Mux(v, x, x) == x */
1698 else if (get_Mux_false(n) == get_Mux_true(n)) {
1699 n = get_Mux_true(n);
1700 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1702 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1703 ir_node *cmp = get_Proj_pred(sel);
1704 long proj_nr = get_Proj_proj(sel);
1705 ir_node *f = get_Mux_false(n);
1706 ir_node *t = get_Mux_true(n);
1709 * Note further that these optimization work even for floating point
1710 * with NaN's because -NaN == NaN.
1711 * However, if +0 and -0 is handled differently, we cannot use the first one.
1714 ir_node *const cmp_l = get_Cmp_left(cmp);
1715 ir_node *const cmp_r = get_Cmp_right(cmp);
1719 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1720 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1722 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1729 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1730 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1732 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1739 * Note: normalization puts the constant on the right side,
1740 * so we check only one case.
1742 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1743 /* Mux(t CMP 0, X, t) */
1744 if (is_Minus(f) && get_Minus_op(f) == t) {
1745 /* Mux(t CMP 0, -t, t) */
1746 if (proj_nr == pn_Cmp_Eq) {
1747 /* Mux(t == 0, -t, t) ==> -t */
1749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1750 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1751 /* Mux(t != 0, -t, t) ==> t */
1753 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1760 } /* equivalent_node_Mux */
1763 * Returns a equivalent node of a Psi: if a condition is true
1764 * and all previous conditions are false we know its value.
1765 * If all conditions are false its value is the default one.
1767 static ir_node *equivalent_node_Psi(ir_node *n) {
1769 return equivalent_node_Mux(n);
1771 } /* equivalent_node_Psi */
1774 * Remove Confirm nodes if setting is on.
1775 * Replace Confirms(x, '=', Constlike) by Constlike.
1777 static ir_node *equivalent_node_Confirm(ir_node *n) {
1778 ir_node *pred = get_Confirm_value(n);
1779 pn_Cmp pnc = get_Confirm_cmp(n);
1781 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1783 * rare case: two identical Confirms one after another,
1784 * replace the second one with the first.
1787 pred = get_Confirm_value(n);
1788 pnc = get_Confirm_cmp(n);
1790 if (get_opt_remove_confirm())
1791 return get_Confirm_value(n);
1796 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1797 * perform no actual computation, as, e.g., the Id nodes. It does not create
1798 * new nodes. It is therefore safe to free n if the node returned is not n.
1799 * If a node returns a Tuple we can not just skip it. If the size of the
1800 * in array fits, we transform n into a tuple (e.g., Div).
1802 ir_node *equivalent_node(ir_node *n) {
1803 if (n->op->ops.equivalent_node)
1804 return n->op->ops.equivalent_node(n);
1806 } /* equivalent_node */
1809 * Sets the default equivalent node operation for an ir_op_ops.
1811 * @param code the opcode for the default operation
1812 * @param ops the operations initialized
1817 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1821 ops->equivalent_node = equivalent_node_##a; \
1855 } /* firm_set_default_equivalent_node */
1858 * Returns non-zero if a node is a Phi node
1859 * with all predecessors constant.
1861 static int is_const_Phi(ir_node *n) {
1864 if (! is_Phi(n) || get_irn_arity(n) == 0)
1866 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1867 if (! is_Const(get_irn_n(n, i)))
1870 } /* is_const_Phi */
1872 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1873 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1876 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1878 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1879 if (eval == tarval_sub) {
1880 tarval_sub_type func = (tarval_sub_type)eval;
1882 return func(a, b, mode);
1884 tarval_binop_type func = (tarval_binop_type)eval;
1891 * Apply an evaluator on a binop with a constant operators (and one Phi).
1893 * @param phi the Phi node
1894 * @param other the other operand
1895 * @param eval an evaluator function
1896 * @param mode the mode of the result, may be different from the mode of the Phi!
1897 * @param left if non-zero, other is the left operand, else the right
1899 * @return a new Phi node if the conversion was successful, NULL else
1901 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1906 int i, n = get_irn_arity(phi);
1908 NEW_ARR_A(void *, res, n);
1910 for (i = 0; i < n; ++i) {
1911 pred = get_irn_n(phi, i);
1912 tv = get_Const_tarval(pred);
1913 tv = do_eval(eval, other, tv, mode);
1915 if (tv == tarval_bad) {
1916 /* folding failed, bad */
1922 for (i = 0; i < n; ++i) {
1923 pred = get_irn_n(phi, i);
1924 tv = get_Const_tarval(pred);
1925 tv = do_eval(eval, tv, other, mode);
1927 if (tv == tarval_bad) {
1928 /* folding failed, bad */
1934 irg = current_ir_graph;
1935 for (i = 0; i < n; ++i) {
1936 pred = get_irn_n(phi, i);
1937 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1938 mode, res[i], get_Const_type(pred));
1940 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1941 } /* apply_binop_on_phi */
1944 * Apply an evaluator on a binop with two constant Phi.
1946 * @param a the left Phi node
1947 * @param b the right Phi node
1948 * @param eval an evaluator function
1949 * @param mode the mode of the result, may be different from the mode of the Phi!
1951 * @return a new Phi node if the conversion was successful, NULL else
1953 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1954 tarval *tv_l, *tv_r, *tv;
1960 if (get_nodes_block(a) != get_nodes_block(b))
1963 n = get_irn_arity(a);
1964 NEW_ARR_A(void *, res, n);
1966 for (i = 0; i < n; ++i) {
1967 pred = get_irn_n(a, i);
1968 tv_l = get_Const_tarval(pred);
1969 pred = get_irn_n(b, i);
1970 tv_r = get_Const_tarval(pred);
1971 tv = do_eval(eval, tv_l, tv_r, mode);
1973 if (tv == tarval_bad) {
1974 /* folding failed, bad */
1979 irg = current_ir_graph;
1980 for (i = 0; i < n; ++i) {
1981 pred = get_irn_n(a, i);
1982 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1984 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1985 } /* apply_binop_on_2_phis */
1988 * Apply an evaluator on a unop with a constant operator (a Phi).
1990 * @param phi the Phi node
1991 * @param eval an evaluator function
1993 * @return a new Phi node if the conversion was successful, NULL else
1995 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2001 int i, n = get_irn_arity(phi);
2003 NEW_ARR_A(void *, res, n);
2004 for (i = 0; i < n; ++i) {
2005 pred = get_irn_n(phi, i);
2006 tv = get_Const_tarval(pred);
2009 if (tv == tarval_bad) {
2010 /* folding failed, bad */
2015 mode = get_irn_mode(phi);
2016 irg = current_ir_graph;
2017 for (i = 0; i < n; ++i) {
2018 pred = get_irn_n(phi, i);
2019 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2020 mode, res[i], get_Const_type(pred));
2022 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2023 } /* apply_unop_on_phi */
2026 * Apply a conversion on a constant operator (a Phi).
2028 * @param phi the Phi node
2030 * @return a new Phi node if the conversion was successful, NULL else
2032 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2037 int i, n = get_irn_arity(phi);
2039 NEW_ARR_A(void *, res, n);
2040 for (i = 0; i < n; ++i) {
2041 pred = get_irn_n(phi, i);
2042 tv = get_Const_tarval(pred);
2043 tv = tarval_convert_to(tv, mode);
2045 if (tv == tarval_bad) {
2046 /* folding failed, bad */
2051 irg = current_ir_graph;
2052 for (i = 0; i < n; ++i) {
2053 pred = get_irn_n(phi, i);
2054 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2055 mode, res[i], get_Const_type(pred));
2057 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2058 } /* apply_conv_on_phi */
2061 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2062 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2063 * If possible, remove the Conv's.
2065 static ir_node *transform_node_AddSub(ir_node *n) {
2066 ir_mode *mode = get_irn_mode(n);
2068 if (mode_is_reference(mode)) {
2069 ir_node *left = get_binop_left(n);
2070 ir_node *right = get_binop_right(n);
2071 unsigned ref_bits = get_mode_size_bits(mode);
2073 if (is_Conv(left)) {
2074 ir_mode *lmode = get_irn_mode(left);
2075 unsigned bits = get_mode_size_bits(lmode);
2077 if (ref_bits == bits &&
2078 mode_is_int(lmode) &&
2079 get_mode_arithmetic(lmode) == irma_twos_complement) {
2080 ir_node *pre = get_Conv_op(left);
2081 ir_mode *pre_mode = get_irn_mode(pre);
2083 if (mode_is_int(pre_mode) &&
2084 get_mode_size_bits(pre_mode) == bits &&
2085 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2086 /* ok, this conv just changes to sign, moreover the calculation
2087 * is done with same number of bits as our address mode, so
2088 * we can ignore the conv as address calculation can be viewed
2089 * as either signed or unsigned
2091 set_binop_left(n, pre);
2096 if (is_Conv(right)) {
2097 ir_mode *rmode = get_irn_mode(right);
2098 unsigned bits = get_mode_size_bits(rmode);
2100 if (ref_bits == bits &&
2101 mode_is_int(rmode) &&
2102 get_mode_arithmetic(rmode) == irma_twos_complement) {
2103 ir_node *pre = get_Conv_op(right);
2104 ir_mode *pre_mode = get_irn_mode(pre);
2106 if (mode_is_int(pre_mode) &&
2107 get_mode_size_bits(pre_mode) == bits &&
2108 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2109 /* ok, this conv just changes to sign, moreover the calculation
2110 * is done with same number of bits as our address mode, so
2111 * we can ignore the conv as address calculation can be viewed
2112 * as either signed or unsigned
2114 set_binop_right(n, pre);
2119 /* let address arithmetic use unsigned modes */
2120 if (is_Const(right)) {
2121 ir_mode *rmode = get_irn_mode(right);
2123 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2124 /* convert a AddP(P, *s) into AddP(P, *u) */
2125 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2127 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2128 set_binop_right(n, pre);
2134 } /* transform_node_AddSub */
2136 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2138 if (is_Const(b) && is_const_Phi(a)) { \
2139 /* check for Op(Phi, Const) */ \
2140 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2142 else if (is_Const(a) && is_const_Phi(b)) { \
2143 /* check for Op(Const, Phi) */ \
2144 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2146 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2147 /* check for Op(Phi, Phi) */ \
2148 c = apply_binop_on_2_phis(a, b, eval, mode); \
2151 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2155 #define HANDLE_UNOP_PHI(eval, a, c) \
2157 if (is_const_Phi(a)) { \
2158 /* check for Op(Phi) */ \
2159 c = apply_unop_on_phi(a, eval); \
2161 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2167 * Do the AddSub optimization, then Transform
2168 * Constant folding on Phi
2169 * Add(a,a) -> Mul(a, 2)
2170 * Add(Mul(a, x), a) -> Mul(a, x+1)
2171 * if the mode is integer or float.
2172 * Transform Add(a,-b) into Sub(a,b).
2173 * Reassociation might fold this further.
2175 static ir_node *transform_node_Add(ir_node *n) {
2177 ir_node *a, *b, *c, *oldn = n;
2179 n = transform_node_AddSub(n);
2181 a = get_Add_left(n);
2182 b = get_Add_right(n);
2184 mode = get_irn_mode(n);
2186 if (mode_is_reference(mode)) {
2187 ir_mode *lmode = get_irn_mode(a);
2189 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2190 /* an Add(a, NULL) is a hidden Conv */
2191 dbg_info *dbg = get_irn_dbg_info(n);
2192 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2196 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2198 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2199 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2202 if (mode_is_num(mode)) {
2203 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2204 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2205 ir_node *block = get_nodes_block(n);
2208 get_irn_dbg_info(n),
2212 new_r_Const_long(current_ir_graph, block, mode, 2),
2214 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2219 get_irn_dbg_info(n),
2225 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2230 get_irn_dbg_info(n),
2236 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2239 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2240 /* Here we rely on constants be on the RIGHT side */
2242 ir_node *op = get_Not_op(a);
2244 if (is_Const(b) && is_Const_one(b)) {
2246 ir_node *blk = get_irn_n(n, -1);
2247 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2248 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2253 ir_node *blk = get_irn_n(n, -1);
2254 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2255 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2260 ir_node *op = get_Not_op(b);
2264 ir_node *blk = get_irn_n(n, -1);
2265 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2266 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2273 } /* transform_node_Add */
2276 * returns -cnst or NULL if impossible
2278 static ir_node *const_negate(ir_node *cnst) {
2279 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2280 dbg_info *dbgi = get_irn_dbg_info(cnst);
2281 ir_graph *irg = get_irn_irg(cnst);
2282 ir_node *block = get_nodes_block(cnst);
2283 ir_mode *mode = get_irn_mode(cnst);
2284 if (tv == tarval_bad) return NULL;
2285 return new_rd_Const(dbgi, irg, block, mode, tv);
2289 * Do the AddSub optimization, then Transform
2290 * Constant folding on Phi
2291 * Sub(0,a) -> Minus(a)
2292 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2293 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2294 * Sub(Add(a, x), x) -> a
2295 * Sub(x, Add(x, a)) -> -a
2296 * Sub(x, Const) -> Add(x, -Const)
2298 static ir_node *transform_node_Sub(ir_node *n) {
2303 n = transform_node_AddSub(n);
2305 a = get_Sub_left(n);
2306 b = get_Sub_right(n);
2308 mode = get_irn_mode(n);
2310 if (mode_is_int(mode)) {
2311 ir_mode *lmode = get_irn_mode(a);
2313 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2314 /* a Sub(a, NULL) is a hidden Conv */
2315 dbg_info *dbg = get_irn_dbg_info(n);
2316 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2321 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2323 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2324 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2327 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2328 /* a - C -> a + (-C) */
2329 ir_node *cnst = const_negate(b);
2331 ir_node *block = get_nodes_block(n);
2332 dbg_info *dbgi = get_irn_dbg_info(n);
2333 ir_graph *irg = get_irn_irg(n);
2335 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2336 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2341 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2342 ir_graph *irg = current_ir_graph;
2343 dbg_info *dbg = get_irn_dbg_info(n);
2344 ir_node *block = get_nodes_block(n);
2345 ir_node *left = get_Minus_op(a);
2346 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2348 n = new_rd_Minus(dbg, irg, block, add, mode);
2349 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2351 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2352 ir_graph *irg = current_ir_graph;
2353 dbg_info *dbg = get_irn_dbg_info(n);
2354 ir_node *block = get_nodes_block(n);
2355 ir_node *right = get_Minus_op(b);
2357 n = new_rd_Add(dbg, irg, block, a, right, mode);
2358 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2360 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2361 ir_graph *irg = current_ir_graph;
2362 dbg_info *s_dbg = get_irn_dbg_info(b);
2363 ir_node *s_block = get_nodes_block(b);
2364 ir_node *s_left = get_Sub_right(b);
2365 ir_node *s_right = get_Sub_left(b);
2366 ir_mode *s_mode = get_irn_mode(b);
2367 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2368 dbg_info *a_dbg = get_irn_dbg_info(n);
2369 ir_node *a_block = get_nodes_block(n);
2371 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2372 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2374 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2375 ir_node *m_right = get_Mul_right(b);
2376 if (is_Const(m_right)) {
2377 ir_node *cnst2 = const_negate(m_right);
2378 if (cnst2 != NULL) {
2379 ir_graph *irg = current_ir_graph;
2380 dbg_info *m_dbg = get_irn_dbg_info(b);
2381 ir_node *m_block = get_nodes_block(b);
2382 ir_node *m_left = get_Mul_left(b);
2383 ir_mode *m_mode = get_irn_mode(b);
2384 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2385 dbg_info *a_dbg = get_irn_dbg_info(n);
2386 ir_node *a_block = get_nodes_block(n);
2388 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2395 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2396 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2398 get_irn_dbg_info(n),
2403 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2407 if (mode_wrap_around(mode)) {
2408 ir_node *left = get_Add_left(a);
2409 ir_node *right = get_Add_right(a);
2411 /* FIXME: Does the Conv's work only for two complement or generally? */
2413 if (mode != get_irn_mode(right)) {
2414 /* This Sub is an effective Cast */
2415 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2418 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2420 } else if (right == b) {
2421 if (mode != get_irn_mode(left)) {
2422 /* This Sub is an effective Cast */
2423 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2432 if (mode_wrap_around(mode)) {
2433 ir_node *left = get_Add_left(b);
2434 ir_node *right = get_Add_right(b);
2436 /* FIXME: Does the Conv's work only for two complement or generally? */
2438 ir_mode *r_mode = get_irn_mode(right);
2440 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2441 if (mode != r_mode) {
2442 /* This Sub is an effective Cast */
2443 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2447 } else if (right == a) {
2448 ir_mode *l_mode = get_irn_mode(left);
2450 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2451 if (mode != l_mode) {
2452 /* This Sub is an effective Cast */
2453 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2455 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2460 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2461 ir_mode *mode = get_irn_mode(a);
2463 if (mode == get_irn_mode(b)) {
2465 ir_node *op_a = get_Conv_op(a);
2466 ir_node *op_b = get_Conv_op(b);
2468 /* check if it's allowed to skip the conv */
2469 ma = get_irn_mode(op_a);
2470 mb = get_irn_mode(op_b);
2472 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2473 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2476 set_Sub_right(n, b);
2482 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2483 if (!is_reassoc_running() && is_Mul(a)) {
2484 ir_node *ma = get_Mul_left(a);
2485 ir_node *mb = get_Mul_right(a);
2488 ir_node *blk = get_irn_n(n, -1);
2490 get_irn_dbg_info(n),
2491 current_ir_graph, blk,
2494 get_irn_dbg_info(n),
2495 current_ir_graph, blk,
2497 new_r_Const_long(current_ir_graph, blk, mode, 1),
2500 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2502 } else if (mb == b) {
2503 ir_node *blk = get_irn_n(n, -1);
2505 get_irn_dbg_info(n),
2506 current_ir_graph, blk,
2509 get_irn_dbg_info(n),
2510 current_ir_graph, blk,
2512 new_r_Const_long(current_ir_graph, blk, mode, 1),
2515 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2519 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2520 ir_node *x = get_Sub_left(a);
2521 ir_node *y = get_Sub_right(a);
2522 ir_node *blk = get_irn_n(n, -1);
2523 ir_mode *m_b = get_irn_mode(b);
2524 ir_mode *m_y = get_irn_mode(y);
2528 /* Determine the right mode for the Add. */
2531 else if (mode_is_reference(m_b))
2533 else if (mode_is_reference(m_y))
2537 * Both modes are different but none is reference,
2538 * happens for instance in SubP(SubP(P, Iu), Is).
2539 * We have two possibilities here: Cast or ignore.
2540 * Currently we ignore this case.
2545 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2547 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2548 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2552 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2553 if (is_Const(a) && is_Not(b)) {
2554 /* c - ~X = X + (c+1) */
2555 tarval *tv = get_Const_tarval(a);
2557 tv = tarval_add(tv, get_mode_one(mode));
2558 if (tv != tarval_bad) {
2559 ir_node *blk = get_irn_n(n, -1);
2560 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2561 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2562 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2568 } /* transform_node_Sub */
2571 * Several transformation done on n*n=2n bits mul.
2572 * These transformations must be done here because new nodes may be produced.
2574 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2576 ir_node *a = get_Mul_left(n);
2577 ir_node *b = get_Mul_right(n);
2578 tarval *ta = value_of(a);
2579 tarval *tb = value_of(b);
2580 ir_mode *smode = get_irn_mode(a);
2582 if (ta == get_mode_one(smode)) {
2583 /* (L)1 * (L)b = (L)b */
2584 ir_node *blk = get_irn_n(n, -1);
2585 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2586 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2589 else if (ta == get_mode_minus_one(smode)) {
2590 /* (L)-1 * (L)b = (L)b */
2591 ir_node *blk = get_irn_n(n, -1);
2592 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2593 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2594 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2597 if (tb == get_mode_one(smode)) {
2598 /* (L)a * (L)1 = (L)a */
2599 ir_node *blk = get_irn_n(a, -1);
2600 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2601 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2604 else if (tb == get_mode_minus_one(smode)) {
2605 /* (L)a * (L)-1 = (L)-a */
2606 ir_node *blk = get_irn_n(n, -1);
2607 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2608 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2609 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2616 * Transform Mul(a,-1) into -a.
2617 * Do constant evaluation of Phi nodes.
2618 * Do architecture dependent optimizations on Mul nodes
2620 static ir_node *transform_node_Mul(ir_node *n) {
2621 ir_node *c, *oldn = n;
2622 ir_mode *mode = get_irn_mode(n);
2623 ir_node *a = get_Mul_left(n);
2624 ir_node *b = get_Mul_right(n);
2626 if (is_Bad(a) || is_Bad(b))
2629 if (mode != get_irn_mode(a))
2630 return transform_node_Mul2n(n, mode);
2632 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2634 if (mode_is_signed(mode)) {
2637 if (value_of(a) == get_mode_minus_one(mode))
2639 else if (value_of(b) == get_mode_minus_one(mode))
2642 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2643 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2648 if (is_Const(b)) { /* (-a) * const -> a * -const */
2649 ir_node *cnst = const_negate(b);
2651 dbg_info *dbgi = get_irn_dbg_info(n);
2652 ir_node *block = get_nodes_block(n);
2653 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2654 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2657 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2658 dbg_info *dbgi = get_irn_dbg_info(n);
2659 ir_node *block = get_nodes_block(n);
2660 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2661 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2663 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2664 ir_node *sub_l = get_Sub_left(b);
2665 ir_node *sub_r = get_Sub_right(b);
2666 dbg_info *dbgi = get_irn_dbg_info(n);
2667 ir_graph *irg = current_ir_graph;
2668 ir_node *block = get_nodes_block(n);
2669 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2670 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2671 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2674 } else if (is_Minus(b)) {
2675 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2676 ir_node *sub_l = get_Sub_left(a);
2677 ir_node *sub_r = get_Sub_right(a);
2678 dbg_info *dbgi = get_irn_dbg_info(n);
2679 ir_graph *irg = current_ir_graph;
2680 ir_node *block = get_nodes_block(n);
2681 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2682 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2683 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2687 if (get_mode_arithmetic(mode) == irma_ieee754) {
2689 tarval *tv = get_Const_tarval(a);
2690 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2691 /* 2.0 * b = b + b */
2692 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2693 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2697 else if (is_Const(b)) {
2698 tarval *tv = get_Const_tarval(b);
2699 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2700 /* a * 2.0 = a + a */
2701 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2702 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2707 return arch_dep_replace_mul_with_shifts(n);
2708 } /* transform_node_Mul */
2711 * Transform a Div Node.
2713 static ir_node *transform_node_Div(ir_node *n) {
2714 ir_mode *mode = get_Div_resmode(n);
2715 ir_node *a = get_Div_left(n);
2716 ir_node *b = get_Div_right(n);
2720 if (is_Const(b) && is_const_Phi(a)) {
2721 /* check for Div(Phi, Const) */
2722 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2724 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2728 else if (is_Const(a) && is_const_Phi(b)) {
2729 /* check for Div(Const, Phi) */
2730 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2732 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2736 else if (is_const_Phi(a) && is_const_Phi(b)) {
2737 /* check for Div(Phi, Phi) */
2738 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2740 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2747 if (tv != tarval_bad) {
2748 value = new_Const(get_tarval_mode(tv), tv);
2750 DBG_OPT_CSTEVAL(n, value);
2753 ir_node *a = get_Div_left(n);
2754 ir_node *b = get_Div_right(n);
2757 if (a == b && value_not_zero(a, &dummy)) {
2758 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2759 value = new_Const(mode, get_mode_one(mode));
2760 DBG_OPT_CSTEVAL(n, value);
2763 if (mode_is_signed(mode) && is_Const(b)) {
2764 tarval *tv = get_Const_tarval(b);
2766 if (tv == get_mode_minus_one(mode)) {
2768 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2769 DBG_OPT_CSTEVAL(n, value);
2773 /* Try architecture dependent optimization */
2774 value = arch_dep_replace_div_by_const(n);
2782 /* Turn Div into a tuple (mem, jmp, bad, value) */
2783 mem = get_Div_mem(n);
2784 blk = get_irn_n(n, -1);
2786 /* skip a potential Pin */
2788 mem = get_Pin_op(mem);
2789 turn_into_tuple(n, pn_Div_max);
2790 set_Tuple_pred(n, pn_Div_M, mem);
2791 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2792 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2793 set_Tuple_pred(n, pn_Div_res, value);
2796 } /* transform_node_Div */
2799 * Transform a Mod node.
2801 static ir_node *transform_node_Mod(ir_node *n) {
2802 ir_mode *mode = get_Mod_resmode(n);
2803 ir_node *a = get_Mod_left(n);
2804 ir_node *b = get_Mod_right(n);
2808 if (is_Const(b) && is_const_Phi(a)) {
2809 /* check for Div(Phi, Const) */
2810 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2812 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2816 else if (is_Const(a) && is_const_Phi(b)) {
2817 /* check for Div(Const, Phi) */
2818 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2820 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2824 else if (is_const_Phi(a) && is_const_Phi(b)) {
2825 /* check for Div(Phi, Phi) */
2826 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2828 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2835 if (tv != tarval_bad) {
2836 value = new_Const(get_tarval_mode(tv), tv);
2838 DBG_OPT_CSTEVAL(n, value);
2841 ir_node *a = get_Mod_left(n);
2842 ir_node *b = get_Mod_right(n);
2845 if (a == b && value_not_zero(a, &dummy)) {
2846 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2847 value = new_Const(mode, get_mode_null(mode));
2848 DBG_OPT_CSTEVAL(n, value);
2851 if (mode_is_signed(mode) && is_Const(b)) {
2852 tarval *tv = get_Const_tarval(b);
2854 if (tv == get_mode_minus_one(mode)) {
2856 value = new_Const(mode, get_mode_null(mode));
2857 DBG_OPT_CSTEVAL(n, value);
2861 /* Try architecture dependent optimization */
2862 value = arch_dep_replace_mod_by_const(n);
2870 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2871 mem = get_Mod_mem(n);
2872 blk = get_irn_n(n, -1);
2874 /* skip a potential Pin */
2876 mem = get_Pin_op(mem);
2877 turn_into_tuple(n, pn_Mod_max);
2878 set_Tuple_pred(n, pn_Mod_M, mem);
2879 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2880 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2881 set_Tuple_pred(n, pn_Mod_res, value);
2884 } /* transform_node_Mod */
2887 * Transform a DivMod node.
2889 static ir_node *transform_node_DivMod(ir_node *n) {
2891 ir_node *a = get_DivMod_left(n);
2892 ir_node *b = get_DivMod_right(n);
2893 ir_mode *mode = get_DivMod_resmode(n);
2898 if (is_Const(b) && is_const_Phi(a)) {
2899 /* check for Div(Phi, Const) */
2900 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2901 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2903 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2904 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2908 else if (is_Const(a) && is_const_Phi(b)) {
2909 /* check for Div(Const, Phi) */
2910 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2911 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2913 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2914 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2918 else if (is_const_Phi(a) && is_const_Phi(b)) {
2919 /* check for Div(Phi, Phi) */
2920 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2921 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2923 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2924 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2931 if (tb != tarval_bad) {
2932 if (tb == get_mode_one(get_tarval_mode(tb))) {
2934 vb = new_Const(mode, get_mode_null(mode));
2935 DBG_OPT_CSTEVAL(n, vb);
2937 } else if (ta != tarval_bad) {
2938 tarval *resa, *resb;
2939 resa = tarval_div(ta, tb);
2940 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2941 Jmp for X result!? */
2942 resb = tarval_mod(ta, tb);
2943 if (resb == tarval_bad) return n; /* Causes exception! */
2944 va = new_Const(mode, resa);
2945 vb = new_Const(mode, resb);
2946 DBG_OPT_CSTEVAL(n, va);
2947 DBG_OPT_CSTEVAL(n, vb);
2949 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2950 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2951 vb = new_Const(mode, get_mode_null(mode));
2952 DBG_OPT_CSTEVAL(n, va);
2953 DBG_OPT_CSTEVAL(n, vb);
2955 } else { /* Try architecture dependent optimization */
2958 arch_dep_replace_divmod_by_const(&va, &vb, n);
2959 evaluated = va != NULL;
2961 } else if (a == b) {
2962 if (value_not_zero(a, &dummy)) {
2964 va = new_Const(mode, get_mode_one(mode));
2965 vb = new_Const(mode, get_mode_null(mode));
2966 DBG_OPT_CSTEVAL(n, va);
2967 DBG_OPT_CSTEVAL(n, vb);
2970 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2973 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2974 /* 0 / non-Const = 0 */
2979 if (evaluated) { /* replace by tuple */
2983 mem = get_DivMod_mem(n);
2984 /* skip a potential Pin */
2986 mem = get_Pin_op(mem);
2988 blk = get_irn_n(n, -1);
2989 turn_into_tuple(n, pn_DivMod_max);
2990 set_Tuple_pred(n, pn_DivMod_M, mem);
2991 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2992 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2993 set_Tuple_pred(n, pn_DivMod_res_div, va);
2994 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2998 } /* transform_node_DivMod */
3001 * Optimize x / c to x * (1/c)
3003 static ir_node *transform_node_Quot(ir_node *n) {
3004 ir_mode *mode = get_Quot_resmode(n);
3007 if (get_mode_arithmetic(mode) == irma_ieee754) {
3008 ir_node *b = get_Quot_right(n);
3011 tarval *tv = get_Const_tarval(b);
3015 * Floating point constant folding might be disabled here to
3017 * However, as we check for exact result, doing it is safe.
3020 rem = tarval_enable_fp_ops(1);
3021 tv = tarval_quo(get_mode_one(mode), tv);
3022 (void)tarval_enable_fp_ops(rem);
3024 /* Do the transformation if the result is either exact or we are not
3025 using strict rules. */
3026 if (tv != tarval_bad &&
3027 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3028 ir_node *blk = get_irn_n(n, -1);
3029 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3030 ir_node *a = get_Quot_left(n);
3031 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3032 ir_node *mem = get_Quot_mem(n);
3034 /* skip a potential Pin */
3036 mem = get_Pin_op(mem);
3037 turn_into_tuple(n, pn_Quot_max);
3038 set_Tuple_pred(n, pn_Quot_M, mem);
3039 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3040 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3041 set_Tuple_pred(n, pn_Quot_res, m);
3042 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3047 } /* transform_node_Quot */
3050 * Optimize Abs(x) into x if x is Confirmed >= 0
3051 * Optimize Abs(x) into -x if x is Confirmed <= 0
3052 * Optimize Abs(-x) int Abs(x)
3054 static ir_node *transform_node_Abs(ir_node *n) {
3055 ir_node *c, *oldn = n;
3056 ir_node *a = get_Abs_op(n);
3059 HANDLE_UNOP_PHI(tarval_abs, a, c);
3061 switch (classify_value_sign(a)) {
3062 case value_classified_negative:
3063 mode = get_irn_mode(n);
3066 * We can replace the Abs by -x here.
3067 * We even could add a new Confirm here
3068 * (if not twos complement)
3070 * Note that -x would create a new node, so we could
3071 * not run it in the equivalent_node() context.
3073 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3074 get_nodes_block(n), a, mode);
3076 DBG_OPT_CONFIRM(oldn, n);
3078 case value_classified_positive:
3079 /* n is positive, Abs is not needed */
3082 DBG_OPT_CONFIRM(oldn, n);
3088 /* Abs(-x) = Abs(x) */
3089 mode = get_irn_mode(n);
3090 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3091 get_nodes_block(n), get_Minus_op(a), mode);
3092 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3096 } /* transform_node_Abs */
3099 * Optimize -a CMP -b into b CMP a.
3100 * This works only for for modes where unary Minus
3102 * Note that two-complement integers can Overflow
3103 * so it will NOT work.
3105 * For == and != can be handled in Proj(Cmp)
3107 static ir_node *transform_node_Cmp(ir_node *n) {
3109 ir_node *left = get_Cmp_left(n);
3110 ir_node *right = get_Cmp_right(n);
3112 if (is_Minus(left) && is_Minus(right) &&
3113 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3114 left = get_Minus_op(left);
3115 right = get_Minus_op(right);
3116 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3117 get_nodes_block(n), left, right);
3118 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3121 } /* transform_node_Cmp */
3125 * Transform a Cond node.
3127 * Replace the Cond by a Jmp if it branches on a constant
3130 static ir_node *transform_node_Cond(ir_node *n) {
3133 ir_node *a = get_Cond_selector(n);
3134 tarval *ta = value_of(a);
3136 /* we need block info which is not available in floating irgs */
3137 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3140 if ((ta != tarval_bad) &&
3141 (get_irn_mode(a) == mode_b) &&
3142 (get_opt_unreachable_code())) {
3143 /* It's a boolean Cond, branching on a boolean constant.
3144 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3145 ir_node *blk = get_nodes_block(n);
3146 jmp = new_r_Jmp(current_ir_graph, blk);
3147 turn_into_tuple(n, pn_Cond_max);
3148 if (ta == tarval_b_true) {
3149 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3150 set_Tuple_pred(n, pn_Cond_true, jmp);
3152 set_Tuple_pred(n, pn_Cond_false, jmp);
3153 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3155 /* We might generate an endless loop, so keep it alive. */
3156 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3159 } /* transform_node_Cond */
3162 * Prototype of a recursive transform function
3163 * for bitwise distributive transformations.
3165 typedef ir_node* (*recursive_transform)(ir_node *n);
3168 * makes use of distributive laws for and, or, eor
3169 * and(a OP c, b OP c) -> and(a, b) OP c
3170 * note, might return a different op than n
3172 static ir_node *transform_bitwise_distributive(ir_node *n,
3173 recursive_transform trans_func)
3176 ir_node *a = get_binop_left(n);
3177 ir_node *b = get_binop_right(n);
3178 ir_op *op = get_irn_op(a);
3179 ir_op *op_root = get_irn_op(n);
3181 if(op != get_irn_op(b))
3184 if (op == op_Conv) {
3185 ir_node *a_op = get_Conv_op(a);
3186 ir_node *b_op = get_Conv_op(b);
3187 ir_mode *a_mode = get_irn_mode(a_op);
3188 ir_mode *b_mode = get_irn_mode(b_op);
3189 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3190 ir_node *blk = get_irn_n(n, -1);
3193 set_binop_left(n, a_op);
3194 set_binop_right(n, b_op);
3195 set_irn_mode(n, a_mode);
3197 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3199 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3205 /* nothing to gain here */
3209 if (op == op_Shrs || op == op_Shr || op == op_Shl
3210 || op == op_And || op == op_Or || op == op_Eor) {
3211 ir_node *a_left = get_binop_left(a);
3212 ir_node *a_right = get_binop_right(a);
3213 ir_node *b_left = get_binop_left(b);
3214 ir_node *b_right = get_binop_right(b);
3216 ir_node *op1 = NULL;
3217 ir_node *op2 = NULL;
3219 if (is_op_commutative(op)) {
3220 if (a_left == b_left) {
3224 } else if(a_left == b_right) {
3228 } else if(a_right == b_left) {
3234 if(a_right == b_right) {
3241 /* (a sop c) & (b sop c) => (a & b) sop c */
3242 ir_node *blk = get_irn_n(n, -1);
3244 ir_node *new_n = exact_copy(n);
3245 set_binop_left(new_n, op1);
3246 set_binop_right(new_n, op2);
3247 new_n = trans_func(new_n);
3249 if(op_root == op_Eor && op == op_Or) {
3250 dbg_info *dbgi = get_irn_dbg_info(n);
3251 ir_graph *irg = current_ir_graph;
3252 ir_mode *mode = get_irn_mode(c);
3254 c = new_rd_Not(dbgi, irg, blk, c, mode);
3255 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3258 set_nodes_block(n, blk);
3259 set_binop_left(n, new_n);
3260 set_binop_right(n, c);
3261 add_identities(current_ir_graph->value_table, n);
3264 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3275 static ir_node *transform_node_And(ir_node *n) {
3276 ir_node *c, *oldn = n;
3277 ir_node *a = get_And_left(n);
3278 ir_node *b = get_And_right(n);
3281 mode = get_irn_mode(n);
3282 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3284 /* we can evaluate 2 Projs of the same Cmp */
3285 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3286 ir_node *pred_a = get_Proj_pred(a);
3287 ir_node *pred_b = get_Proj_pred(b);
3288 if (pred_a == pred_b) {
3289 dbg_info *dbgi = get_irn_dbg_info(n);
3290 ir_node *block = get_nodes_block(pred_a);
3291 pn_Cmp pn_a = get_Proj_proj(a);
3292 pn_Cmp pn_b = get_Proj_proj(b);
3293 /* yes, we can simply calculate with pncs */
3294 pn_Cmp new_pnc = pn_a & pn_b;
3296 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3301 ir_node *op = get_Not_op(b);
3303 ir_node *ba = get_And_left(op);
3304 ir_node *bb = get_And_right(op);
3306 /* it's enough to test the following cases due to normalization! */
3307 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3308 /* (a|b) & ~(a&b) = a^b */
3309 ir_node *block = get_nodes_block(n);
3311 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3312 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3320 ir_node *op = get_Not_op(a);
3322 ir_node *aa = get_And_left(op);
3323 ir_node *ab = get_And_right(op);
3325 /* it's enough to test the following cases due to normalization! */
3326 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3327 /* (a|b) & ~(a&b) = a^b */
3328 ir_node *block = get_nodes_block(n);
3330 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3331 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3338 ir_node *al = get_Eor_left(a);
3339 ir_node *ar = get_Eor_right(a);
3342 /* (b ^ a) & b -> ~a & b */
3343 dbg_info *dbg = get_irn_dbg_info(n);
3344 ir_node *block = get_nodes_block(n);
3346 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3347 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3348 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3352 /* (a ^ b) & b -> ~a & b */
3353 dbg_info *dbg = get_irn_dbg_info(n);
3354 ir_node *block = get_nodes_block(n);
3356 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3357 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3358 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3363 ir_node *bl = get_Eor_left(b);
3364 ir_node *br = get_Eor_right(b);
3367 /* a & (a ^ b) -> a & ~b */
3368 dbg_info *dbg = get_irn_dbg_info(n);
3369 ir_node *block = get_nodes_block(n);
3371 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3372 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3377 /* a & (b ^ a) -> a & ~b */
3378 dbg_info *dbg = get_irn_dbg_info(n);
3379 ir_node *block = get_nodes_block(n);
3381 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3382 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3387 if (is_Not(a) && is_Not(b)) {
3388 /* ~a & ~b = ~(a|b) */
3389 ir_node *block = get_nodes_block(n);
3390 ir_mode *mode = get_irn_mode(n);
3394 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3395 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3396 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3400 n = transform_bitwise_distributive(n, transform_node_And);
3403 } /* transform_node_And */
3408 static ir_node *transform_node_Eor(ir_node *n) {
3409 ir_node *c, *oldn = n;
3410 ir_node *a = get_Eor_left(n);
3411 ir_node *b = get_Eor_right(n);
3412 ir_mode *mode = get_irn_mode(n);
3414 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3416 /* we can evaluate 2 Projs of the same Cmp */
3417 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3418 ir_node *pred_a = get_Proj_pred(a);
3419 ir_node *pred_b = get_Proj_pred(b);
3420 if(pred_a == pred_b) {
3421 dbg_info *dbgi = get_irn_dbg_info(n);
3422 ir_node *block = get_nodes_block(pred_a);
3423 pn_Cmp pn_a = get_Proj_proj(a);
3424 pn_Cmp pn_b = get_Proj_proj(b);
3425 /* yes, we can simply calculate with pncs */
3426 pn_Cmp new_pnc = pn_a ^ pn_b;
3428 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3435 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3436 mode, get_mode_null(mode));
3437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3438 } else if (mode == mode_b &&
3440 is_Const(b) && is_Const_one(b) &&
3441 is_Cmp(get_Proj_pred(a))) {
3442 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3443 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3444 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3447 } else if (is_Const(b)) {
3448 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3449 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3450 ir_node *not_op = get_Not_op(a);
3451 dbg_info *dbg = get_irn_dbg_info(n);
3452 ir_graph *irg = current_ir_graph;
3453 ir_node *block = get_nodes_block(n);
3454 ir_mode *mode = get_irn_mode(n);
3455 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3457 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3458 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3459 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3462 n = transform_bitwise_distributive(n, transform_node_Eor);
3466 } /* transform_node_Eor */
3471 static ir_node *transform_node_Not(ir_node *n) {
3472 ir_node *c, *oldn = n;
3473 ir_node *a = get_Not_op(n);
3474 ir_mode *mode = get_irn_mode(n);
3476 HANDLE_UNOP_PHI(tarval_not,a,c);
3478 /* check for a boolean Not */
3479 if (mode == mode_b &&
3481 is_Cmp(get_Proj_pred(a))) {
3482 /* We negate a Cmp. The Cmp has the negated result anyways! */
3483 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3484 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3485 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3489 ir_node *eor_b = get_Eor_right(a);
3490 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3491 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3492 ir_node *eor_a = get_Eor_left(a);
3493 dbg_info *dbg = get_irn_dbg_info(n);
3494 ir_graph *irg = current_ir_graph;
3495 ir_node *block = get_nodes_block(n);
3496 ir_mode *mode = get_irn_mode(n);
3497 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3501 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3502 if (is_Minus(a)) { /* ~-x -> x + -1 */
3503 dbg_info *dbg = get_irn_dbg_info(n);
3504 ir_graph *irg = current_ir_graph;
3505 ir_node *block = get_nodes_block(n);
3506 ir_node *add_l = get_Minus_op(a);
3507 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3508 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3509 } else if (is_Add(a)) {
3510 ir_node *add_r = get_Add_right(a);
3511 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3512 /* ~(x + -1) = -x */
3513 ir_node *op = get_Add_left(a);
3514 ir_node *blk = get_irn_n(n, -1);
3515 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3516 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3521 } /* transform_node_Not */
3524 * Transform a Minus.
3528 * -(a >>u (size-1)) = a >>s (size-1)
3529 * -(a >>s (size-1)) = a >>u (size-1)
3530 * -(a * const) -> a * -const
3532 static ir_node *transform_node_Minus(ir_node *n) {
3533 ir_node *c, *oldn = n;
3534 ir_node *a = get_Minus_op(n);
3537 HANDLE_UNOP_PHI(tarval_neg,a,c);
3539 mode = get_irn_mode(a);
3540 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3541 /* the following rules are only to twos-complement */
3544 ir_node *op = get_Not_op(a);
3545 tarval *tv = get_mode_one(mode);
3546 ir_node *blk = get_irn_n(n, -1);
3547 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3548 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3549 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3553 ir_node *c = get_Shr_right(a);
3556 tarval *tv = get_Const_tarval(c);
3558 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3559 /* -(a >>u (size-1)) = a >>s (size-1) */
3560 ir_node *v = get_Shr_left(a);
3562 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3563 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3569 ir_node *c = get_Shrs_right(a);
3572 tarval *tv = get_Const_tarval(c);
3574 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3575 /* -(a >>s (size-1)) = a >>u (size-1) */
3576 ir_node *v = get_Shrs_left(a);
3578 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3579 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3586 /* - (a-b) = b - a */
3587 ir_node *la = get_Sub_left(a);
3588 ir_node *ra = get_Sub_right(a);
3589 ir_node *blk = get_irn_n(n, -1);
3591 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3592 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3596 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3597 ir_node *mul_l = get_Mul_left(a);
3598 ir_node *mul_r = get_Mul_right(a);
3599 if (is_Const(mul_r)) {
3600 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3601 if(tv != tarval_bad) {
3602 ir_node *cnst = new_Const(mode, tv);
3603 dbg_info *dbg = get_irn_dbg_info(a);
3604 ir_graph *irg = current_ir_graph;
3605 ir_node *block = get_nodes_block(a);
3606 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3607 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3614 } /* transform_node_Minus */
3617 * Transform a Cast_type(Const) into a new Const_type
3619 static ir_node *transform_node_Cast(ir_node *n) {
3621 ir_node *pred = get_Cast_op(n);
3622 ir_type *tp = get_irn_type(n);
3624 if (is_Const(pred) && get_Const_type(pred) != tp) {
3625 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3626 get_Const_tarval(pred), tp);
3627 DBG_OPT_CSTEVAL(oldn, n);
3628 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3629 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3630 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3631 DBG_OPT_CSTEVAL(oldn, n);
3635 } /* transform_node_Cast */
3638 * Transform a Proj(Load) with a non-null address.
3640 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3641 if (get_opt_ldst_only_null_ptr_exceptions()) {
3642 if (get_irn_mode(proj) == mode_X) {
3643 ir_node *load = get_Proj_pred(proj);
3645 /* get the Load address */
3646 ir_node *addr = get_Load_ptr(load);
3647 ir_node *blk = get_nodes_block(load);
3650 if (value_not_null(addr, &confirm)) {
3651 if (confirm == NULL) {
3652 /* this node may float if it did not depend on a Confirm */
3653 set_irn_pinned(load, op_pin_state_floats);
3655 if (get_Proj_proj(proj) == pn_Load_X_except) {
3656 DBG_OPT_EXC_REM(proj);
3657 return get_irg_bad(current_ir_graph);
3659 return new_r_Jmp(current_ir_graph, blk);
3665 } /* transform_node_Proj_Load */
3668 * Transform a Proj(Store) with a non-null address.
3670 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3671 if (get_opt_ldst_only_null_ptr_exceptions()) {
3672 if (get_irn_mode(proj) == mode_X) {
3673 ir_node *store = get_Proj_pred(proj);
3675 /* get the load/store address */
3676 ir_node *addr = get_Store_ptr(store);
3677 ir_node *blk = get_nodes_block(store);
3680 if (value_not_null(addr, &confirm)) {
3681 if (confirm == NULL) {
3682 /* this node may float if it did not depend on a Confirm */
3683 set_irn_pinned(store, op_pin_state_floats);
3685 if (get_Proj_proj(proj) == pn_Store_X_except) {
3686 DBG_OPT_EXC_REM(proj);
3687 return get_irg_bad(current_ir_graph);
3689 return new_r_Jmp(current_ir_graph, blk);
3694 } /* transform_node_Proj_Store */
3697 * Transform a Proj(Div) with a non-zero value.
3698 * Removes the exceptions and routes the memory to the NoMem node.
3700 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3701 ir_node *div = get_Proj_pred(proj);
3702 ir_node *b = get_Div_right(div);
3703 ir_node *confirm, *res, *new_mem;
3706 if (value_not_zero(b, &confirm)) {
3707 /* div(x, y) && y != 0 */
3708 if (confirm == NULL) {
3709 /* we are sure we have a Const != 0 */
3710 new_mem = get_Div_mem(div);
3711 if (is_Pin(new_mem))
3712 new_mem = get_Pin_op(new_mem);
3713 set_Div_mem(div, new_mem);
3714 set_irn_pinned(div, op_pin_state_floats);
3717 proj_nr = get_Proj_proj(proj);
3719 case pn_Div_X_regular:
3720 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3722 case pn_Div_X_except:
3723 /* we found an exception handler, remove it */
3724 DBG_OPT_EXC_REM(proj);
3728 res = get_Div_mem(div);
3729 new_mem = get_irg_no_mem(current_ir_graph);
3732 /* This node can only float up to the Confirm block */
3733 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3735 set_irn_pinned(div, op_pin_state_floats);
3736 /* this is a Div without exception, we can remove the memory edge */
3737 set_Div_mem(div, new_mem);
3742 } /* transform_node_Proj_Div */
3745 * Transform a Proj(Mod) with a non-zero value.
3746 * Removes the exceptions and routes the memory to the NoMem node.
3748 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3749 ir_node *mod = get_Proj_pred(proj);
3750 ir_node *b = get_Mod_right(mod);
3751 ir_node *confirm, *res, *new_mem;
3754 if (value_not_zero(b, &confirm)) {
3755 /* mod(x, y) && y != 0 */
3756 proj_nr = get_Proj_proj(proj);
3758 if (confirm == NULL) {
3759 /* we are sure we have a Const != 0 */
3760 new_mem = get_Mod_mem(mod);
3761 if (is_Pin(new_mem))
3762 new_mem = get_Pin_op(new_mem);
3763 set_Mod_mem(mod, new_mem);
3764 set_irn_pinned(mod, op_pin_state_floats);
3769 case pn_Mod_X_regular:
3770 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3772 case pn_Mod_X_except:
3773 /* we found an exception handler, remove it */
3774 DBG_OPT_EXC_REM(proj);
3778 res = get_Mod_mem(mod);
3779 new_mem = get_irg_no_mem(current_ir_graph);
3782 /* This node can only float up to the Confirm block */
3783 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3785 /* this is a Mod without exception, we can remove the memory edge */
3786 set_Mod_mem(mod, new_mem);
3789 if (get_Mod_left(mod) == b) {
3790 /* a % a = 0 if a != 0 */
3791 ir_mode *mode = get_irn_mode(proj);
3792 ir_node *res = new_Const(mode, get_mode_null(mode));
3794 DBG_OPT_CSTEVAL(mod, res);
3800 } /* transform_node_Proj_Mod */
3803 * Transform a Proj(DivMod) with a non-zero value.
3804 * Removes the exceptions and routes the memory to the NoMem node.
3806 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3807 ir_node *divmod = get_Proj_pred(proj);
3808 ir_node *b = get_DivMod_right(divmod);
3809 ir_node *confirm, *res, *new_mem;
3812 if (value_not_zero(b, &confirm)) {
3813 /* DivMod(x, y) && y != 0 */
3814 proj_nr = get_Proj_proj(proj);
3816 if (confirm == NULL) {
3817 /* we are sure we have a Const != 0 */
3818 new_mem = get_DivMod_mem(divmod);
3819 if (is_Pin(new_mem))
3820 new_mem = get_Pin_op(new_mem);
3821 set_DivMod_mem(divmod, new_mem);
3822 set_irn_pinned(divmod, op_pin_state_floats);
3827 case pn_DivMod_X_regular:
3828 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3830 case pn_DivMod_X_except:
3831 /* we found an exception handler, remove it */
3832 DBG_OPT_EXC_REM(proj);
3836 res = get_DivMod_mem(divmod);
3837 new_mem = get_irg_no_mem(current_ir_graph);
3840 /* This node can only float up to the Confirm block */
3841 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3843 /* this is a DivMod without exception, we can remove the memory edge */
3844 set_DivMod_mem(divmod, new_mem);
3847 case pn_DivMod_res_mod:
3848 if (get_DivMod_left(divmod) == b) {
3849 /* a % a = 0 if a != 0 */
3850 ir_mode *mode = get_irn_mode(proj);
3851 ir_node *res = new_Const(mode, get_mode_null(mode));
3853 DBG_OPT_CSTEVAL(divmod, res);
3859 } /* transform_node_Proj_DivMod */
3862 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3864 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3865 if (get_opt_unreachable_code()) {
3866 ir_node *n = get_Proj_pred(proj);
3867 ir_node *b = get_Cond_selector(n);
3869 if (mode_is_int(get_irn_mode(b))) {
3870 tarval *tb = value_of(b);
3872 if (tb != tarval_bad) {
3873 /* we have a constant switch */
3874 long num = get_Proj_proj(proj);
3876 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3877 if (get_tarval_long(tb) == num) {
3878 /* Do NOT create a jump here, or we will have 2 control flow ops
3879 * in a block. This case is optimized away in optimize_cf(). */
3882 /* this case will NEVER be taken, kill it */
3890 } /* transform_node_Proj_Cond */
3893 * Create a 0 constant of given mode.
3895 static ir_node *create_zero_const(ir_mode *mode) {
3896 tarval *tv = get_mode_null(mode);
3897 ir_node *cnst = new_Const(mode, tv);
3902 /* the order of the values is important! */
3903 typedef enum const_class {
3909 static const_class classify_const(const ir_node* n)
3911 if (is_Const(n)) return const_const;
3912 if (is_irn_constlike(n)) return const_like;
3917 * Determines whether r is more constlike or has a larger index (in that order)
3920 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3922 const const_class l_order = classify_const(l);
3923 const const_class r_order = classify_const(r);
3925 l_order > r_order ||
3926 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3930 * Normalizes and optimizes Cmp nodes.
3932 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3933 ir_node *n = get_Proj_pred(proj);
3934 ir_node *left = get_Cmp_left(n);
3935 ir_node *right = get_Cmp_right(n);
3938 ir_mode *mode = NULL;
3939 long proj_nr = get_Proj_proj(proj);
3941 /* we can evaluate some cases directly */
3944 return new_Const(mode_b, get_tarval_b_false());
3946 return new_Const(mode_b, get_tarval_b_true());
3948 if (!mode_is_float(get_irn_mode(left)))
3949 return new_Const(mode_b, get_tarval_b_true());
3955 /* remove Casts of both sides */
3956 left = skip_Cast(left);
3957 right = skip_Cast(right);
3959 /* Remove unnecessary conversions */
3960 /* TODO handle constants */
3961 if (is_Conv(left) && is_Conv(right)) {
3962 ir_mode *mode = get_irn_mode(left);
3963 ir_node *op_left = get_Conv_op(left);
3964 ir_node *op_right = get_Conv_op(right);
3965 ir_mode *mode_left = get_irn_mode(op_left);
3966 ir_mode *mode_right = get_irn_mode(op_right);
3968 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3969 && mode_left != mode_b && mode_right != mode_b) {
3970 ir_graph *irg = current_ir_graph;
3971 ir_node *block = get_nodes_block(n);
3973 if (mode_left == mode_right) {
3977 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3978 } else if (smaller_mode(mode_left, mode_right)) {
3979 left = new_r_Conv(irg, block, op_left, mode_right);
3982 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3983 } else if (smaller_mode(mode_right, mode_left)) {
3985 right = new_r_Conv(irg, block, op_right, mode_left);
3987 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3992 /* remove operation on both sides if possible */
3993 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3995 * The following operations are NOT safe for floating point operations, for instance
3996 * 1.0 + inf == 2.0 + inf, =/=> x == y
3998 if (mode_is_int(get_irn_mode(left))) {
3999 unsigned lop = get_irn_opcode(left);
4001 if (lop == get_irn_opcode(right)) {
4002 ir_node *ll, *lr, *rl, *rr;
4004 /* same operation on both sides, try to remove */
4008 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4009 left = get_unop_op(left);
4010 right = get_unop_op(right);
4012 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4015 ll = get_Add_left(left);
4016 lr = get_Add_right(left);
4017 rl = get_Add_left(right);
4018 rr = get_Add_right(right);
4021 /* X + a CMP X + b ==> a CMP b */
4025 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4026 } else if (ll == rr) {
4027 /* X + a CMP b + X ==> a CMP b */
4031 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4032 } else if (lr == rl) {
4033 /* a + X CMP X + b ==> a CMP b */
4037 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4038 } else if (lr == rr) {
4039 /* a + X CMP b + X ==> a CMP b */
4043 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4047 ll = get_Sub_left(left);
4048 lr = get_Sub_right(left);
4049 rl = get_Sub_left(right);
4050 rr = get_Sub_right(right);
4053 /* X - a CMP X - b ==> a CMP b */
4057 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4058 } else if (lr == rr) {
4059 /* a - X CMP b - X ==> a CMP b */
4063 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4067 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4068 /* a ROTL X CMP b ROTL X ==> a CMP b */
4069 left = get_Rotl_left(left);
4070 right = get_Rotl_left(right);
4072 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4080 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4081 if (is_Add(left) || is_Sub(left)) {
4082 ir_node *ll = get_binop_left(left);
4083 ir_node *lr = get_binop_right(left);
4085 if (lr == right && is_Add(left)) {
4092 right = create_zero_const(get_irn_mode(left));
4094 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4097 if (is_Add(right) || is_Sub(right)) {
4098 ir_node *rl = get_binop_left(right);
4099 ir_node *rr = get_binop_right(right);
4101 if (rr == left && is_Add(right)) {
4108 right = create_zero_const(get_irn_mode(left));
4110 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4113 } /* mode_is_int(...) */
4114 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4116 /* replace mode_b compares with ands/ors */
4117 if (get_irn_mode(left) == mode_b) {
4118 ir_graph *irg = current_ir_graph;
4119 ir_node *block = get_nodes_block(n);
4123 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4124 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4125 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4126 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4127 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4128 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4129 default: bres = NULL;
4132 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4138 * First step: normalize the compare op
4139 * by placing the constant on the right side
4140 * or moving the lower address node to the left.
4142 if (!operands_are_normalized(left, right)) {
4148 proj_nr = get_inversed_pnc(proj_nr);
4153 * Second step: Try to reduce the magnitude
4154 * of a constant. This may help to generate better code
4155 * later and may help to normalize more compares.
4156 * Of course this is only possible for integer values.
4158 if (is_Const(right)) {
4159 mode = get_irn_mode(right);
4160 tv = get_Const_tarval(right);
4162 /* TODO extend to arbitrary constants */
4163 if (is_Conv(left) && tarval_is_null(tv)) {
4164 ir_node *op = get_Conv_op(left);
4165 ir_mode *op_mode = get_irn_mode(op);
4168 * UpConv(x) REL 0 ==> x REL 0
4170 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4171 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4172 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4173 tv = get_mode_null(op_mode);
4177 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4181 if (tv != tarval_bad) {
4182 /* the following optimization is possible on modes without Overflow
4183 * on Unary Minus or on == and !=:
4184 * -a CMP c ==> a swap(CMP) -c
4186 * Beware: for two-complement Overflow may occur, so only == and != can
4187 * be optimized, see this:
4188 * -MININT < 0 =/=> MININT > 0 !!!
4190 if (is_Minus(left) &&
4191 (!mode_overflow_on_unary_Minus(mode) ||
4192 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4193 tv = tarval_neg(tv);
4195 if (tv != tarval_bad) {
4196 left = get_Minus_op(left);
4197 proj_nr = get_inversed_pnc(proj_nr);
4199 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4201 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4202 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4203 tv = tarval_not(tv);
4205 if (tv != tarval_bad) {
4206 left = get_Not_op(left);
4208 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4212 /* for integer modes, we have more */
4213 if (mode_is_int(mode)) {
4214 /* Ne includes Unordered which is not possible on integers.
4215 * However, frontends often use this wrong, so fix it here */
4216 if (proj_nr & pn_Cmp_Uo) {
4217 proj_nr &= ~pn_Cmp_Uo;
4218 set_Proj_proj(proj, proj_nr);
4221 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4222 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4223 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4224 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4226 if (tv != tarval_bad) {
4227 proj_nr ^= pn_Cmp_Eq;
4229 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4232 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4233 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4234 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4235 tv = tarval_add(tv, get_mode_one(mode));
4237 if (tv != tarval_bad) {
4238 proj_nr ^= pn_Cmp_Eq;
4240 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4244 /* the following reassociations work only for == and != */
4245 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4247 #if 0 /* Might be not that good in general */
4248 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4249 if (tarval_is_null(tv) && is_Sub(left)) {
4250 right = get_Sub_right(left);
4251 left = get_Sub_left(left);
4253 tv = value_of(right);
4255 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4259 if (tv != tarval_bad) {
4260 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4262 ir_node *c1 = get_Sub_right(left);
4263 tarval *tv2 = value_of(c1);
4265 if (tv2 != tarval_bad) {
4266 tv2 = tarval_add(tv, value_of(c1));
4268 if (tv2 != tarval_bad) {
4269 left = get_Sub_left(left);
4272 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4276 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4277 else if (is_Add(left)) {
4278 ir_node *a_l = get_Add_left(left);
4279 ir_node *a_r = get_Add_right(left);
4283 if (is_Const(a_l)) {
4285 tv2 = value_of(a_l);
4288 tv2 = value_of(a_r);
4291 if (tv2 != tarval_bad) {
4292 tv2 = tarval_sub(tv, tv2, NULL);
4294 if (tv2 != tarval_bad) {
4298 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4302 /* -a == c ==> a == -c, -a != c ==> a != -c */
4303 else if (is_Minus(left)) {
4304 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4306 if (tv2 != tarval_bad) {
4307 left = get_Minus_op(left);
4310 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4315 /* the following reassociations work only for <= */
4316 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4317 if (tv != tarval_bad) {
4318 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4319 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4325 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4326 switch (get_irn_opcode(left)) {
4330 c1 = get_And_right(left);
4333 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4334 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4336 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4338 /* TODO: move to constant evaluation */
4339 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4340 c1 = new_Const(mode_b, tv);
4341 DBG_OPT_CSTEVAL(proj, c1);
4345 if (tarval_is_single_bit(tv)) {
4347 * optimization for AND:
4349 * And(x, C) == C ==> And(x, C) != 0
4350 * And(x, C) != C ==> And(X, C) == 0
4352 * if C is a single Bit constant.
4355 /* check for Constant's match. We have check hare the tarvals,
4356 because our const might be changed */
4357 if (get_Const_tarval(c1) == tv) {
4358 /* fine: do the transformation */
4359 tv = get_mode_null(get_tarval_mode(tv));
4360 proj_nr ^= pn_Cmp_Leg;
4362 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4368 c1 = get_Or_right(left);
4369 if (is_Const(c1) && tarval_is_null(tv)) {
4371 * Or(x, C) == 0 && C != 0 ==> FALSE
4372 * Or(x, C) != 0 && C != 0 ==> TRUE
4374 if (! tarval_is_null(get_Const_tarval(c1))) {
4375 /* TODO: move to constant evaluation */
4376 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4377 c1 = new_Const(mode_b, tv);
4378 DBG_OPT_CSTEVAL(proj, c1);
4385 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4387 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4390 c1 = get_Shl_right(left);
4392 tarval *tv1 = get_Const_tarval(c1);
4393 ir_mode *mode = get_irn_mode(left);
4394 tarval *minus1 = get_mode_all_one(mode);
4395 tarval *amask = tarval_shr(minus1, tv1);
4396 tarval *cmask = tarval_shl(minus1, tv1);
4399 if (tarval_and(tv, cmask) != tv) {
4400 /* condition not met */
4401 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4402 c1 = new_Const(mode_b, tv);
4403 DBG_OPT_CSTEVAL(proj, c1);
4406 sl = get_Shl_left(left);
4407 blk = get_nodes_block(n);
4408 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4409 tv = tarval_shr(tv, tv1);
4411 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4416 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4418 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4421 c1 = get_Shr_right(left);
4423 tarval *tv1 = get_Const_tarval(c1);
4424 ir_mode *mode = get_irn_mode(left);
4425 tarval *minus1 = get_mode_all_one(mode);
4426 tarval *amask = tarval_shl(minus1, tv1);
4427 tarval *cmask = tarval_shr(minus1, tv1);
4430 if (tarval_and(tv, cmask) != tv) {
4431 /* condition not met */
4432 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4433 c1 = new_Const(mode_b, tv);
4434 DBG_OPT_CSTEVAL(proj, c1);
4437 sl = get_Shr_left(left);
4438 blk = get_nodes_block(n);
4439 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4440 tv = tarval_shl(tv, tv1);
4442 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4447 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4449 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4452 c1 = get_Shrs_right(left);
4454 tarval *tv1 = get_Const_tarval(c1);
4455 ir_mode *mode = get_irn_mode(left);
4456 tarval *minus1 = get_mode_all_one(mode);
4457 tarval *amask = tarval_shl(minus1, tv1);
4458 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4461 cond = tarval_sub(cond, tv1, NULL);
4462 cond = tarval_shrs(tv, cond);
4464 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4465 /* condition not met */
4466 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4467 c1 = new_Const(mode_b, tv);
4468 DBG_OPT_CSTEVAL(proj, c1);
4471 sl = get_Shrs_left(left);
4472 blk = get_nodes_block(n);
4473 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4474 tv = tarval_shl(tv, tv1);
4476 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4481 } /* tarval != bad */
4484 if (changed & 2) /* need a new Const */
4485 right = new_Const(mode, tv);
4487 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4488 ir_node *op = get_Proj_pred(left);
4490 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4491 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4492 ir_node *c = get_binop_right(op);
4495 tarval *tv = get_Const_tarval(c);
4497 if (tarval_is_single_bit(tv)) {
4498 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4499 ir_node *v = get_binop_left(op);
4500 ir_node *blk = get_irn_n(op, -1);
4501 ir_mode *mode = get_irn_mode(v);
4503 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4504 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4506 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4513 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4515 /* create a new compare */
4516 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4517 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4521 } /* transform_node_Proj_Cmp */
4524 * Optimize CopyB(mem, x, x) into a Nop.
4526 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4527 ir_node *copyb = get_Proj_pred(proj);
4528 ir_node *a = get_CopyB_dst(copyb);
4529 ir_node *b = get_CopyB_src(copyb);
4532 switch (get_Proj_proj(proj)) {
4533 case pn_CopyB_X_regular:
4534 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4535 DBG_OPT_EXC_REM(proj);
4536 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4538 case pn_CopyB_M_except:
4539 case pn_CopyB_X_except:
4540 DBG_OPT_EXC_REM(proj);
4541 proj = get_irg_bad(current_ir_graph);
4548 } /* transform_node_Proj_CopyB */
4551 * Optimize Bounds(idx, idx, upper) into idx.
4553 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4554 ir_node *oldn = proj;
4555 ir_node *bound = get_Proj_pred(proj);
4556 ir_node *idx = get_Bound_index(bound);
4557 ir_node *pred = skip_Proj(idx);
4560 if (idx == get_Bound_lower(bound))
4562 else if (is_Bound(pred)) {
4564 * idx was Bounds checked in the same MacroBlock previously,
4565 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4567 ir_node *lower = get_Bound_lower(bound);
4568 ir_node *upper = get_Bound_upper(bound);
4569 if (get_Bound_lower(pred) == lower &&
4570 get_Bound_upper(pred) == upper &&
4571 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4573 * One could expect that we simply return the previous
4574 * Bound here. However, this would be wrong, as we could
4575 * add an exception Proj to a new location then.
4576 * So, we must turn in into a tuple.
4582 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4583 switch (get_Proj_proj(proj)) {
4585 DBG_OPT_EXC_REM(proj);
4586 proj = get_Bound_mem(bound);
4588 case pn_Bound_X_except:
4589 DBG_OPT_EXC_REM(proj);
4590 proj = get_irg_bad(current_ir_graph);
4594 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4596 case pn_Bound_X_regular:
4597 DBG_OPT_EXC_REM(proj);
4598 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4605 } /* transform_node_Proj_Bound */
4608 * Does all optimizations on nodes that must be done on it's Proj's
4609 * because of creating new nodes.
4611 static ir_node *transform_node_Proj(ir_node *proj) {
4612 ir_node *n = get_Proj_pred(proj);
4614 switch (get_irn_opcode(n)) {
4616 return transform_node_Proj_Load(proj);
4619 return transform_node_Proj_Store(proj);
4622 return transform_node_Proj_Div(proj);
4625 return transform_node_Proj_Mod(proj);
4628 return transform_node_Proj_DivMod(proj);
4631 return transform_node_Proj_Cond(proj);
4634 return transform_node_Proj_Cmp(proj);
4637 /* should not happen, but if it does will be optimized away */
4638 return equivalent_node_Proj(proj);
4641 return transform_node_Proj_CopyB(proj);
4644 return transform_node_Proj_Bound(proj);
4650 } /* transform_node_Proj */
4653 * Move Confirms down through Phi nodes.
4655 static ir_node *transform_node_Phi(ir_node *phi) {
4657 ir_mode *mode = get_irn_mode(phi);
4659 if (mode_is_reference(mode)) {
4660 n = get_irn_arity(phi);
4662 /* Beware of Phi0 */
4664 ir_node *pred = get_irn_n(phi, 0);
4665 ir_node *bound, *new_Phi, *block, **in;
4668 if (! is_Confirm(pred))
4671 bound = get_Confirm_bound(pred);
4672 pnc = get_Confirm_cmp(pred);
4674 NEW_ARR_A(ir_node *, in, n);
4675 in[0] = get_Confirm_value(pred);
4677 for (i = 1; i < n; ++i) {
4678 pred = get_irn_n(phi, i);
4680 if (! is_Confirm(pred) ||
4681 get_Confirm_bound(pred) != bound ||
4682 get_Confirm_cmp(pred) != pnc)
4684 in[i] = get_Confirm_value(pred);
4686 /* move the Confirm nodes "behind" the Phi */
4687 block = get_irn_n(phi, -1);
4688 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4689 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4693 } /* transform_node_Phi */
4696 * Returns the operands of a commutative bin-op, if one operand is
4697 * a const, it is returned as the second one.
4699 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4700 ir_node *op_a = get_binop_left(binop);
4701 ir_node *op_b = get_binop_right(binop);
4703 assert(is_op_commutative(get_irn_op(binop)));
4705 if (is_Const(op_a)) {
4712 } /* get_comm_Binop_Ops */
4715 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4716 * Such pattern may arise in bitfield stores.
4718 * value c4 value c4 & c2
4719 * AND c3 AND c1 | c3
4726 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4729 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4732 ir_node *and_l, *c3;
4733 ir_node *value, *c4;
4734 ir_node *new_and, *new_const, *block;
4735 ir_mode *mode = get_irn_mode(or);
4737 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4740 get_comm_Binop_Ops(or, &and, &c1);
4741 if (!is_Const(c1) || !is_And(and))
4744 get_comm_Binop_Ops(and, &or_l, &c2);
4748 tv1 = get_Const_tarval(c1);
4749 tv2 = get_Const_tarval(c2);
4751 tv = tarval_or(tv1, tv2);
4752 if (tarval_is_all_one(tv)) {
4753 /* the AND does NOT clear a bit with isn't set by the OR */
4754 set_Or_left(or, or_l);
4755 set_Or_right(or, c1);
4757 /* check for more */
4764 get_comm_Binop_Ops(or_l, &and_l, &c3);
4765 if (!is_Const(c3) || !is_And(and_l))
4768 get_comm_Binop_Ops(and_l, &value, &c4);
4772 /* ok, found the pattern, check for conditions */
4773 assert(mode == get_irn_mode(and));
4774 assert(mode == get_irn_mode(or_l));
4775 assert(mode == get_irn_mode(and_l));
4777 tv3 = get_Const_tarval(c3);
4778 tv4 = get_Const_tarval(c4);
4780 tv = tarval_or(tv4, tv2);
4781 if (!tarval_is_all_one(tv)) {
4782 /* have at least one 0 at the same bit position */
4786 n_tv4 = tarval_not(tv4);
4787 if (tv3 != tarval_and(tv3, n_tv4)) {
4788 /* bit in the or_mask is outside the and_mask */
4792 n_tv2 = tarval_not(tv2);
4793 if (tv1 != tarval_and(tv1, n_tv2)) {
4794 /* bit in the or_mask is outside the and_mask */
4798 /* ok, all conditions met */
4799 block = get_irn_n(or, -1);
4801 new_and = new_r_And(current_ir_graph, block,
4802 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4804 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4806 set_Or_left(or, new_and);
4807 set_Or_right(or, new_const);
4809 /* check for more */
4811 } /* transform_node_Or_bf_store */
4814 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4816 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4817 ir_mode *mode = get_irn_mode(or);
4818 ir_node *shl, *shr, *block;
4819 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4822 if (! mode_is_int(mode))
4825 shl = get_binop_left(or);
4826 shr = get_binop_right(or);
4835 } else if (!is_Shl(shl)) {
4837 } else if (!is_Shr(shr)) {
4840 x = get_Shl_left(shl);
4841 if (x != get_Shr_left(shr))
4844 c1 = get_Shl_right(shl);
4845 c2 = get_Shr_right(shr);
4846 if (is_Const(c1) && is_Const(c2)) {
4847 tv1 = get_Const_tarval(c1);
4848 if (! tarval_is_long(tv1))
4851 tv2 = get_Const_tarval(c2);
4852 if (! tarval_is_long(tv2))
4855 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4856 != (int) get_mode_size_bits(mode))
4859 /* yet, condition met */
4860 block = get_nodes_block(or);
4862 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4864 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4871 rotval = sub; /* a Rot right is not supported, so use a rot left */
4872 } else if (is_Sub(c2)) {
4878 if (get_Sub_right(sub) != v)
4881 c1 = get_Sub_left(sub);
4885 tv1 = get_Const_tarval(c1);
4886 if (! tarval_is_long(tv1))
4889 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4892 /* yet, condition met */
4893 block = get_nodes_block(or);
4895 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4897 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4899 } /* transform_node_Or_Rotl */
4904 static ir_node *transform_node_Or(ir_node *n) {
4905 ir_node *c, *oldn = n;
4906 ir_node *a = get_Or_left(n);
4907 ir_node *b = get_Or_right(n);
4910 if (is_Not(a) && is_Not(b)) {
4911 /* ~a | ~b = ~(a&b) */
4912 ir_node *block = get_nodes_block(n);
4914 mode = get_irn_mode(n);
4917 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4918 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4919 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4923 /* we can evaluate 2 Projs of the same Cmp */
4924 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4925 ir_node *pred_a = get_Proj_pred(a);
4926 ir_node *pred_b = get_Proj_pred(b);
4927 if (pred_a == pred_b) {
4928 dbg_info *dbgi = get_irn_dbg_info(n);
4929 ir_node *block = get_nodes_block(pred_a);
4930 pn_Cmp pn_a = get_Proj_proj(a);
4931 pn_Cmp pn_b = get_Proj_proj(b);
4932 /* yes, we can simply calculate with pncs */
4933 pn_Cmp new_pnc = pn_a | pn_b;
4935 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4940 mode = get_irn_mode(n);
4941 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4943 n = transform_node_Or_bf_store(n);
4944 n = transform_node_Or_Rotl(n);
4948 n = transform_bitwise_distributive(n, transform_node_Or);
4951 } /* transform_node_Or */
4955 static ir_node *transform_node(ir_node *n);
4958 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4960 * Should be moved to reassociation?
4962 static ir_node *transform_node_shift(ir_node *n) {
4963 ir_node *left, *right;
4965 tarval *tv1, *tv2, *res;
4966 ir_node *in[2], *irn, *block;
4968 left = get_binop_left(n);
4970 /* different operations */
4971 if (get_irn_op(left) != get_irn_op(n))
4974 right = get_binop_right(n);
4975 tv1 = value_of(right);
4976 if (tv1 == tarval_bad)
4979 tv2 = value_of(get_binop_right(left));
4980 if (tv2 == tarval_bad)
4983 res = tarval_add(tv1, tv2);
4984 mode = get_irn_mode(n);
4986 /* beware: a simple replacement works only, if res < modulo shift */
4988 int modulo_shf = get_mode_modulo_shift(mode);
4989 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4990 if (modulo_shf > 0) {
4991 tarval *modulo = new_tarval_from_long(modulo_shf,
4992 get_tarval_mode(res));
4994 /* shifting too much */
4995 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4997 ir_graph *irg = get_irn_irg(n);
4998 ir_node *block = get_nodes_block(n);
4999 dbg_info *dbgi = get_irn_dbg_info(n);
5000 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
5001 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5005 return new_Const(mode, get_mode_null(mode));
5009 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5012 /* ok, we can replace it */
5013 block = get_nodes_block(n);
5015 in[0] = get_binop_left(left);
5016 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
5018 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5020 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5022 return transform_node(irn);
5023 } /* transform_node_shift */
5026 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5028 * - and, or, xor instead of &
5029 * - Shl, Shr, Shrs, rotl instead of >>
5030 * (with a special case for Or/Xor + Shrs)
5032 static ir_node *transform_node_bitop_shift(ir_node *n) {
5034 ir_node *right = get_binop_right(n);
5035 ir_mode *mode = get_irn_mode(n);
5036 ir_node *bitop_left;
5037 ir_node *bitop_right;
5049 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5051 if (!is_Const(right))
5054 left = get_binop_left(n);
5055 op_left = get_irn_op(left);
5056 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5059 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5060 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5061 /* TODO: test if sign bit is affectes */
5065 bitop_right = get_binop_right(left);
5066 if (!is_Const(bitop_right))
5069 bitop_left = get_binop_left(left);
5071 irg = get_irn_irg(n);
5072 block = get_nodes_block(n);
5073 dbgi = get_irn_dbg_info(n);
5074 tv1 = get_Const_tarval(bitop_right);
5075 tv2 = get_Const_tarval(right);
5077 assert(get_tarval_mode(tv1) == mode);
5080 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5081 tv_shift = tarval_shl(tv1, tv2);
5082 } else if(is_Shr(n)) {
5083 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5084 tv_shift = tarval_shr(tv1, tv2);
5085 } else if(is_Shrs(n)) {
5086 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5087 tv_shift = tarval_shrs(tv1, tv2);
5090 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5091 tv_shift = tarval_rotl(tv1, tv2);
5094 assert(get_tarval_mode(tv_shift) == mode);
5095 new_const = new_Const(mode, tv_shift);
5097 if (op_left == op_And) {
5098 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5099 } else if(op_left == op_Or) {
5100 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5102 assert(op_left == op_Eor);
5103 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5111 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5113 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5114 * (also with x >>s c1 when c1>=c2)
5116 static ir_node *transform_node_shl_shr(ir_node *n) {
5118 ir_node *right = get_binop_right(n);
5134 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5136 if (!is_Const(right))
5139 left = get_binop_left(n);
5140 mode = get_irn_mode(n);
5141 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5142 ir_node *shr_right = get_binop_right(left);
5144 if (!is_Const(shr_right))
5147 x = get_binop_left(left);
5148 tv_shr = get_Const_tarval(shr_right);
5149 tv_shl = get_Const_tarval(right);
5151 if (is_Shrs(left)) {
5152 /* shrs variant only allowed if c1 >= c2 */
5153 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5156 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5159 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5161 tv_mask = tarval_shl(tv_mask, tv_shl);
5162 } else if(is_Shr(n) && is_Shl(left)) {
5163 ir_node *shl_right = get_Shl_right(left);
5165 if (!is_Const(shl_right))
5168 x = get_Shl_left(left);
5169 tv_shr = get_Const_tarval(right);
5170 tv_shl = get_Const_tarval(shl_right);
5172 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5173 tv_mask = tarval_shr(tv_mask, tv_shr);
5178 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5179 assert(tv_mask != tarval_bad);
5180 assert(get_tarval_mode(tv_mask) == mode);
5182 irg = get_irn_irg(n);
5183 block = get_nodes_block(n);
5184 dbgi = get_irn_dbg_info(n);
5186 pnc = tarval_cmp(tv_shl, tv_shr);
5187 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5188 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5189 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5191 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5193 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5196 assert(pnc == pn_Cmp_Gt);
5197 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5198 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5199 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5202 new_const = new_Const(mode, tv_mask);
5203 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5211 static ir_node *transform_node_Shr(ir_node *n) {
5212 ir_node *c, *oldn = n;
5213 ir_node *left = get_Shr_left(n);
5214 ir_node *right = get_Shr_right(n);
5215 ir_mode *mode = get_irn_mode(n);
5217 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5218 n = transform_node_shift(n);
5221 n = transform_node_shl_shr(n);
5223 n = transform_node_bitop_shift(n);
5226 } /* transform_node_Shr */
5231 static ir_node *transform_node_Shrs(ir_node *n) {
5232 ir_node *c, *oldn = n;
5233 ir_node *a = get_Shrs_left(n);
5234 ir_node *b = get_Shrs_right(n);
5235 ir_mode *mode = get_irn_mode(n);
5237 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5238 n = transform_node_shift(n);
5241 n = transform_node_bitop_shift(n);
5244 } /* transform_node_Shrs */
5249 static ir_node *transform_node_Shl(ir_node *n) {
5250 ir_node *c, *oldn = n;
5251 ir_node *a = get_Shl_left(n);
5252 ir_node *b = get_Shl_right(n);
5253 ir_mode *mode = get_irn_mode(n);
5255 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5256 n = transform_node_shift(n);
5259 n = transform_node_shl_shr(n);
5261 n = transform_node_bitop_shift(n);
5264 } /* transform_node_Shl */
5269 static ir_node *transform_node_Rotl(ir_node *n) {
5270 ir_node *c, *oldn = n;
5271 ir_node *a = get_Rotl_left(n);
5272 ir_node *b = get_Rotl_right(n);
5273 ir_mode *mode = get_irn_mode(n);
5275 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5276 n = transform_node_shift(n);
5279 n = transform_node_bitop_shift(n);
5282 } /* transform_node_Rotl */
5287 static ir_node *transform_node_Conv(ir_node *n) {
5288 ir_node *c, *oldn = n;
5289 ir_node *a = get_Conv_op(n);
5291 if (is_const_Phi(a)) {
5292 c = apply_conv_on_phi(a, get_irn_mode(n));
5294 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5299 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5300 ir_mode *mode = get_irn_mode(n);
5301 return new_r_Unknown(current_ir_graph, mode);
5305 } /* transform_node_Conv */
5308 * Remove dead blocks and nodes in dead blocks
5309 * in keep alive list. We do not generate a new End node.
5311 static ir_node *transform_node_End(ir_node *n) {
5312 int i, j, n_keepalives = get_End_n_keepalives(n);
5315 NEW_ARR_A(ir_node *, in, n_keepalives);
5317 for (i = j = 0; i < n_keepalives; ++i) {
5318 ir_node *ka = get_End_keepalive(n, i);
5320 if (! is_Block_dead(ka)) {
5324 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5327 /* FIXME: beabi need to keep a Proj(M) */
5328 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5331 if (j != n_keepalives)
5332 set_End_keepalives(n, j, in);
5334 } /* transform_node_End */
5336 /** returns 1 if a == -b */
5337 static int is_negated_value(ir_node *a, ir_node *b) {
5338 if (is_Minus(a) && get_Minus_op(a) == b)
5340 if (is_Minus(b) && get_Minus_op(b) == a)
5342 if (is_Sub(a) && is_Sub(b)) {
5343 ir_node *a_left = get_Sub_left(a);
5344 ir_node *a_right = get_Sub_right(a);
5345 ir_node *b_left = get_Sub_left(b);
5346 ir_node *b_right = get_Sub_right(b);
5348 if (a_left == b_right && a_right == b_left)
5356 * Optimize a Mux into some simpler cases.
5358 static ir_node *transform_node_Mux(ir_node *n) {
5359 ir_node *oldn = n, *sel = get_Mux_sel(n);
5360 ir_mode *mode = get_irn_mode(n);
5361 ir_node *t = get_Mux_true(n);
5362 ir_node *f = get_Mux_false(n);
5363 ir_graph *irg = current_ir_graph;
5364 ir_node *conds[1], *vals[2];
5366 /* first normalization step: move a possible zero to the false case */
5368 ir_node *cmp = get_Proj_pred(sel);
5371 if (is_Const(t) && is_Const_null(t)) {
5372 /* Psi(x, 0, y) => Psi(x, y, 0) */
5373 pn_Cmp pnc = get_Proj_proj(sel);
5374 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5375 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5379 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
5386 /* note: after normalization, false can only happen on default */
5387 if (mode == mode_b) {
5388 dbg_info *dbg = get_irn_dbg_info(n);
5389 ir_node *block = get_nodes_block(n);
5390 ir_graph *irg = current_ir_graph;
5393 tarval *tv_t = get_Const_tarval(t);
5394 if (tv_t == tarval_b_true) {
5396 /* Muxb(sel, true, false) = sel */
5397 assert(get_Const_tarval(f) == tarval_b_false);
5398 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5401 /* Muxb(sel, true, x) = Or(sel, x) */
5402 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5403 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5407 } else if (is_Const(f)) {
5408 tarval *tv_f = get_Const_tarval(f);
5409 if (tv_f == tarval_b_true) {
5410 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5411 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5413 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5416 /* Muxb(sel, x, false) = And(sel, x) */
5417 assert(tv_f == tarval_b_false);
5418 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5419 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5425 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5426 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5427 tarval *a = get_Const_tarval(t);
5428 tarval *b = get_Const_tarval(f);
5429 tarval *null = get_tarval_null(mode);
5432 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5433 diff = tarval_sub(a, b, NULL);
5436 diff = tarval_sub(b, a, NULL);
5440 if (diff == get_tarval_one(mode) && min != null) {
5441 dbg_info *dbg = get_irn_dbg_info(n);
5442 ir_node *block = get_nodes_block(n);
5443 ir_graph *irg = current_ir_graph;
5447 vals[0] = new_Const(mode, tarval_sub(a, min, NULL));
5448 vals[1] = new_Const(mode, tarval_sub(b, min, NULL));
5449 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
5450 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5456 ir_node *cmp = get_Proj_pred(sel);
5457 long pn = get_Proj_proj(sel);
5460 * Note: normalization puts the constant on the right side,
5461 * so we check only one case.
5463 * Note further that these optimization work even for floating point
5464 * with NaN's because -NaN == NaN.
5465 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5469 ir_node *cmp_r = get_Cmp_right(cmp);
5470 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5471 ir_node *block = get_nodes_block(n);
5472 ir_node *cmp_l = get_Cmp_left(cmp);
5474 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5477 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5478 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5480 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
5481 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5483 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5485 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5486 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5488 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5489 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5491 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5493 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5498 if (mode_is_int(mode)) {
5500 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5501 /* Psi((a & b) != 0, c, 0) */
5502 ir_node *and_r = get_And_right(cmp_l);
5505 if (and_r == t && f == cmp_r) {
5506 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5507 if (pn == pn_Cmp_Lg) {
5508 /* Psi((a & 2^C) != 0, 2^C, 0) */
5511 /* Psi((a & 2^C) == 0, 2^C, 0) */
5512 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5513 block, cmp_l, t, mode);
5518 if (is_Shl(and_r)) {
5519 ir_node *shl_l = get_Shl_left(and_r);
5520 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5521 if (and_r == t && f == cmp_r) {
5522 if (pn == pn_Cmp_Lg) {
5523 /* (a & (1 << n)) != 0, (1 << n), 0) */
5526 /* (a & (1 << n)) == 0, (1 << n), 0) */
5527 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5528 block, cmp_l, t, mode);
5534 and_l = get_And_left(cmp_l);
5535 if (is_Shl(and_l)) {
5536 ir_node *shl_l = get_Shl_left(and_l);
5537 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5538 if (and_l == t && f == cmp_r) {
5539 if (pn == pn_Cmp_Lg) {
5540 /* ((1 << n) & a) != 0, (1 << n), 0) */
5543 /* ((1 << n) & a) == 0, (1 << n), 0) */
5544 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5545 block, cmp_l, t, mode);
5556 return arch_transform_node_Mux(n);
5557 } /* transform_node_Mux */
5560 * Optimize a Psi into some simpler cases.
5562 static ir_node *transform_node_Psi(ir_node *n) {
5564 return transform_node_Mux(n);
5567 } /* transform_node_Psi */
5570 * optimize sync nodes that have other syncs as input we simply add the inputs
5571 * of the other sync to our own inputs
5573 static ir_node *transform_node_Sync(ir_node *n) {
5574 int arity = get_Sync_n_preds(n);
5577 for (i = 0; i < arity;) {
5578 ir_node *pred = get_Sync_pred(n, i);
5582 if (!is_Sync(pred)) {
5590 pred_arity = get_Sync_n_preds(pred);
5591 for (j = 0; j < pred_arity; ++j) {
5592 ir_node *pred_pred = get_Sync_pred(pred, j);
5597 add_irn_n(n, pred_pred);
5601 if (get_Sync_pred(n, k) == pred_pred) break;
5606 /* rehash the sync node */
5607 add_identities(current_ir_graph->value_table, n);
5613 * Tries several [inplace] [optimizing] transformations and returns an
5614 * equivalent node. The difference to equivalent_node() is that these
5615 * transformations _do_ generate new nodes, and thus the old node must
5616 * not be freed even if the equivalent node isn't the old one.
5618 static ir_node *transform_node(ir_node *n) {
5622 * Transform_node is the only "optimizing transformation" that might
5623 * return a node with a different opcode. We iterate HERE until fixpoint
5624 * to get the final result.
5628 if (n->op->ops.transform_node)
5629 n = n->op->ops.transform_node(n);
5630 } while (oldn != n);
5633 } /* transform_node */
5636 * Sets the default transform node operation for an ir_op_ops.
5638 * @param code the opcode for the default operation
5639 * @param ops the operations initialized
5644 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5648 ops->transform_node = transform_node_##a; \
5686 } /* firm_set_default_transform_node */
5689 /* **************** Common Subexpression Elimination **************** */
5691 /** The size of the hash table used, should estimate the number of nodes
5693 #define N_IR_NODES 512
5695 /** Compares the attributes of two Const nodes. */
5696 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5697 return (get_Const_tarval(a) != get_Const_tarval(b))
5698 || (get_Const_type(a) != get_Const_type(b));
5699 } /* node_cmp_attr_Const */
5701 /** Compares the attributes of two Proj nodes. */
5702 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5703 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5704 } /* node_cmp_attr_Proj */
5706 /** Compares the attributes of two Filter nodes. */
5707 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5708 return get_Filter_proj(a) != get_Filter_proj(b);
5709 } /* node_cmp_attr_Filter */
5711 /** Compares the attributes of two Alloc nodes. */
5712 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5713 const alloc_attr *pa = get_irn_alloc_attr(a);
5714 const alloc_attr *pb = get_irn_alloc_attr(b);
5715 return (pa->where != pb->where) || (pa->type != pb->type);
5716 } /* node_cmp_attr_Alloc */
5718 /** Compares the attributes of two Free nodes. */
5719 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5720 const free_attr *pa = get_irn_free_attr(a);
5721 const free_attr *pb = get_irn_free_attr(b);
5722 return (pa->where != pb->where) || (pa->type != pb->type);
5723 } /* node_cmp_attr_Free */
5725 /** Compares the attributes of two SymConst nodes. */
5726 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5727 const symconst_attr *pa = get_irn_symconst_attr(a);
5728 const symconst_attr *pb = get_irn_symconst_attr(b);
5729 return (pa->kind != pb->kind)
5730 || (pa->sym.type_p != pb->sym.type_p)
5731 || (pa->tp != pb->tp);
5732 } /* node_cmp_attr_SymConst */
5734 /** Compares the attributes of two Call nodes. */
5735 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5736 return get_irn_call_attr(a) != get_irn_call_attr(b);
5737 } /* node_cmp_attr_Call */
5739 /** Compares the attributes of two Sel nodes. */
5740 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5741 const ir_entity *a_ent = get_Sel_entity(a);
5742 const ir_entity *b_ent = get_Sel_entity(b);
5744 (a_ent->kind != b_ent->kind) ||
5745 (a_ent->name != b_ent->name) ||
5746 (a_ent->owner != b_ent->owner) ||
5747 (a_ent->ld_name != b_ent->ld_name) ||
5748 (a_ent->type != b_ent->type);
5749 } /* node_cmp_attr_Sel */
5751 /** Compares the attributes of two Phi nodes. */
5752 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5753 /* we can only enter this function if both nodes have the same number of inputs,
5754 hence it is enough to check if one of them is a Phi0 */
5756 /* check the Phi0 pos attribute */
5757 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5760 } /* node_cmp_attr_Phi */
5762 /** Compares the attributes of two Conv nodes. */
5763 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5764 return get_Conv_strict(a) != get_Conv_strict(b);
5765 } /* node_cmp_attr_Conv */
5767 /** Compares the attributes of two Cast nodes. */
5768 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5769 return get_Cast_type(a) != get_Cast_type(b);
5770 } /* node_cmp_attr_Cast */
5772 /** Compares the attributes of two Load nodes. */
5773 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5774 if (get_Load_volatility(a) == volatility_is_volatile ||
5775 get_Load_volatility(b) == volatility_is_volatile)
5776 /* NEVER do CSE on volatile Loads */
5778 /* do not CSE Loads with different alignment. Be conservative. */
5779 if (get_Load_align(a) != get_Load_align(b))
5782 return get_Load_mode(a) != get_Load_mode(b);
5783 } /* node_cmp_attr_Load */
5785 /** Compares the attributes of two Store nodes. */
5786 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5787 /* do not CSE Stores with different alignment. Be conservative. */
5788 if (get_Store_align(a) != get_Store_align(b))
5791 /* NEVER do CSE on volatile Stores */
5792 return (get_Store_volatility(a) == volatility_is_volatile ||
5793 get_Store_volatility(b) == volatility_is_volatile);
5794 } /* node_cmp_attr_Store */
5796 /** Compares two exception attributes */
5797 static int node_cmp_exception(ir_node *a, ir_node *b) {
5798 const except_attr *ea = get_irn_except_attr(a);
5799 const except_attr *eb = get_irn_except_attr(b);
5801 return ea->pin_state != eb->pin_state;
5804 #define node_cmp_attr_Bound node_cmp_exception
5806 /** Compares the attributes of two Div nodes. */
5807 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5808 const divmod_attr *ma = get_irn_divmod_attr(a);
5809 const divmod_attr *mb = get_irn_divmod_attr(b);
5810 return ma->exc.pin_state != mb->exc.pin_state ||
5811 ma->res_mode != mb->res_mode ||
5812 ma->no_remainder != mb->no_remainder;
5813 } /* node_cmp_attr_Div */
5815 /** Compares the attributes of two DivMod nodes. */
5816 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5817 const divmod_attr *ma = get_irn_divmod_attr(a);
5818 const divmod_attr *mb = get_irn_divmod_attr(b);
5819 return ma->exc.pin_state != mb->exc.pin_state ||
5820 ma->res_mode != mb->res_mode;
5821 } /* node_cmp_attr_DivMod */
5823 /** Compares the attributes of two Mod nodes. */
5824 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5825 const divmod_attr *ma = get_irn_divmod_attr(a);
5826 const divmod_attr *mb = get_irn_divmod_attr(b);
5827 return ma->exc.pin_state != mb->exc.pin_state ||
5828 ma->res_mode != mb->res_mode;
5829 } /* node_cmp_attr_Mod */
5831 /** Compares the attributes of two Quot nodes. */
5832 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5833 const divmod_attr *ma = get_irn_divmod_attr(a);
5834 const divmod_attr *mb = get_irn_divmod_attr(b);
5835 return ma->exc.pin_state != mb->exc.pin_state ||
5836 ma->res_mode != mb->res_mode;
5837 } /* node_cmp_attr_Quot */
5839 /** Compares the attributes of two Confirm nodes. */
5840 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5841 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5842 } /* node_cmp_attr_Confirm */
5844 /** Compares the attributes of two ASM nodes. */
5845 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5847 const ir_asm_constraint *ca;
5848 const ir_asm_constraint *cb;
5851 if (get_ASM_text(a) != get_ASM_text(b))
5854 /* Should we really check the constraints here? Should be better, but is strange. */
5855 n = get_ASM_n_input_constraints(a);
5856 if (n != get_ASM_n_input_constraints(b))
5859 ca = get_ASM_input_constraints(a);
5860 cb = get_ASM_input_constraints(b);
5861 for (i = 0; i < n; ++i) {
5862 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5866 n = get_ASM_n_output_constraints(a);
5867 if (n != get_ASM_n_output_constraints(b))
5870 ca = get_ASM_output_constraints(a);
5871 cb = get_ASM_output_constraints(b);
5872 for (i = 0; i < n; ++i) {
5873 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5877 n = get_ASM_n_clobbers(a);
5878 if (n != get_ASM_n_clobbers(b))
5881 cla = get_ASM_clobbers(a);
5882 clb = get_ASM_clobbers(b);
5883 for (i = 0; i < n; ++i) {
5884 if (cla[i] != clb[i])
5888 } /* node_cmp_attr_ASM */
5891 * Set the default node attribute compare operation for an ir_op_ops.
5893 * @param code the opcode for the default operation
5894 * @param ops the operations initialized
5899 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5903 ops->node_cmp_attr = node_cmp_attr_##a; \
5934 } /* firm_set_default_node_cmp_attr */
5937 * Compare function for two nodes in the value table. Gets two
5938 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5940 int identities_cmp(const void *elt, const void *key) {
5941 ir_node *a = (ir_node *)elt;
5942 ir_node *b = (ir_node *)key;
5945 if (a == b) return 0;
5947 if ((get_irn_op(a) != get_irn_op(b)) ||
5948 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5950 /* compare if a's in and b's in are of equal length */
5951 irn_arity_a = get_irn_intra_arity(a);
5952 if (irn_arity_a != get_irn_intra_arity(b))
5955 if (get_irn_pinned(a) == op_pin_state_pinned) {
5956 /* for pinned nodes, the block inputs must be equal */
5957 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5959 } else if (! get_opt_global_cse()) {
5960 /* for block-local CSE both nodes must be in the same MacroBlock */
5961 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5965 /* compare a->in[0..ins] with b->in[0..ins] */
5966 for (i = 0; i < irn_arity_a; i++)
5967 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5971 * here, we already now that the nodes are identical except their
5974 if (a->op->ops.node_cmp_attr)
5975 return a->op->ops.node_cmp_attr(a, b);
5978 } /* identities_cmp */
5981 * Calculate a hash value of a node.
5983 * @param node The IR-node
5985 unsigned ir_node_hash(const ir_node *node) {
5986 return node->op->ops.hash(node);
5987 } /* ir_node_hash */
5990 pset *new_identities(void) {
5991 return new_pset(identities_cmp, N_IR_NODES);
5992 } /* new_identities */
5994 void del_identities(pset *value_table) {
5995 del_pset(value_table);
5996 } /* del_identities */
5999 * Normalize a node by putting constants (and operands with larger
6000 * node index) on the right (operator side).
6002 * @param n The node to normalize
6004 static void normalize_node(ir_node *n) {
6005 if (is_op_commutative(get_irn_op(n))) {
6006 ir_node *l = get_binop_left(n);
6007 ir_node *r = get_binop_right(n);
6009 /* For commutative operators perform a OP b == b OP a but keep
6010 * constants on the RIGHT side. This helps greatly in some
6011 * optimizations. Moreover we use the idx number to make the form
6013 if (!operands_are_normalized(l, r)) {
6014 set_binop_left(n, r);
6015 set_binop_right(n, l);
6018 } /* normalize_node */
6021 * Update the nodes after a match in the value table. If both nodes have
6022 * the same MacroBlock but different Blocks, we must ensure that the node
6023 * with the dominating Block (the node that is near to the MacroBlock header
6024 * is stored in the table.
6025 * Because a MacroBlock has only one "non-exception" flow, we don't need
6026 * dominance info here: We known, that one block must dominate the other and
6027 * following the only block input will allow to find it.
6029 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6030 ir_node *known_blk, *new_block, *block, *mbh;
6032 if (get_opt_global_cse()) {
6033 /* Block inputs are meaning less */
6036 known_blk = get_irn_n(known_irn, -1);
6037 new_block = get_irn_n(new_ir_node, -1);
6038 if (known_blk == new_block) {
6039 /* already in the same block */
6043 * We expect the typical case when we built the graph. In that case, the
6044 * known_irn is already the upper one, so checking this should be faster.
6047 mbh = get_Block_MacroBlock(new_block);
6049 if (block == known_blk) {
6050 /* ok, we have found it: known_block dominates new_block as expected */
6055 * We have reached the MacroBlock header NOT founding
6056 * the known_block. new_block must dominate known_block.
6059 set_irn_n(known_irn, -1, new_block);
6062 assert(get_Block_n_cfgpreds(block) == 1);
6063 block = get_Block_cfgpred_block(block, 0);
6065 } /* update_value_table */
6068 * Return the canonical node computing the same value as n.
6069 * Looks up the node in a hash table, enters it in the table
6070 * if it isn't there yet.
6072 * @param value_table the HashSet containing all nodes in the
6074 * @param n the node to look up
6076 * @return a node that computes the same value as n or n if no such
6077 * node could be found
6079 ir_node *identify_remember(pset *value_table, ir_node *n) {
6082 if (!value_table) return n;
6085 /* lookup or insert in hash table with given hash key. */
6086 o = pset_insert(value_table, n, ir_node_hash(n));
6089 update_known_irn(o, n);
6094 } /* identify_remember */
6097 * During construction we set the op_pin_state_pinned flag in the graph right when the
6098 * optimization is performed. The flag turning on procedure global cse could
6099 * be changed between two allocations. This way we are safe.
6101 * @param value_table The value table
6102 * @param n The node to lookup
6104 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
6107 n = identify_remember(value_table, n);
6108 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6109 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6111 } /* identify_cons */
6113 /* Add a node to the identities value table. */
6114 void add_identities(pset *value_table, ir_node *node) {
6115 if (get_opt_cse() && is_no_Block(node))
6116 identify_remember(value_table, node);
6117 } /* add_identities */
6119 /* Visit each node in the value table of a graph. */
6120 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6122 ir_graph *rem = current_ir_graph;
6124 current_ir_graph = irg;
6125 foreach_pset(irg->value_table, node)
6127 current_ir_graph = rem;
6128 } /* visit_all_identities */
6131 * Garbage in, garbage out. If a node has a dead input, i.e., the
6132 * Bad node is input to the node, return the Bad node.
6134 static ir_node *gigo(ir_node *node) {
6136 ir_op *op = get_irn_op(node);
6138 /* remove garbage blocks by looking at control flow that leaves the block
6139 and replacing the control flow by Bad. */
6140 if (get_irn_mode(node) == mode_X) {
6141 ir_node *block = get_nodes_block(skip_Proj(node));
6143 /* Don't optimize nodes in immature blocks. */
6144 if (!get_Block_matured(block))
6146 /* Don't optimize End, may have Bads. */
6147 if (op == op_End) return node;
6149 if (is_Block(block)) {
6150 if (is_Block_dead(block)) {
6151 /* control flow from dead block is dead */
6155 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6156 if (!is_Bad(get_irn_n(block, i)))
6160 ir_graph *irg = get_irn_irg(block);
6161 /* the start block is never dead */
6162 if (block != get_irg_start_block(irg)
6163 && block != get_irg_end_block(irg)) {
6165 * Do NOT kill control flow without setting
6166 * the block to dead of bad things can happen:
6167 * We get a Block that is not reachable be irg_block_walk()
6168 * but can be found by irg_walk()!
6170 set_Block_dead(block);
6177 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6178 blocks predecessors is dead. */
6179 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6180 irn_arity = get_irn_arity(node);
6183 * Beware: we can only read the block of a non-floating node.
6185 if (is_irn_pinned_in_irg(node) &&
6186 is_Block_dead(get_nodes_block(skip_Proj(node))))
6189 for (i = 0; i < irn_arity; i++) {
6190 ir_node *pred = get_irn_n(node, i);
6195 /* Propagating Unknowns here seems to be a bad idea, because
6196 sometimes we need a node as a input and did not want that
6198 However, it might be useful to move this into a later phase
6199 (if you think that optimizing such code is useful). */
6200 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6201 return new_Unknown(get_irn_mode(node));
6206 /* With this code we violate the agreement that local_optimize
6207 only leaves Bads in Block, Phi and Tuple nodes. */
6208 /* If Block has only Bads as predecessors it's garbage. */
6209 /* If Phi has only Bads as predecessors it's garbage. */
6210 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6211 irn_arity = get_irn_arity(node);
6212 for (i = 0; i < irn_arity; i++) {
6213 if (!is_Bad(get_irn_n(node, i))) break;
6215 if (i == irn_arity) node = new_Bad();
6222 * These optimizations deallocate nodes from the obstack.
6223 * It can only be called if it is guaranteed that no other nodes
6224 * reference this one, i.e., right after construction of a node.
6226 * @param n The node to optimize
6228 * current_ir_graph must be set to the graph of the node!
6230 ir_node *optimize_node(ir_node *n) {
6233 ir_opcode iro = get_irn_opcode(n);
6235 /* Always optimize Phi nodes: part of the construction. */
6236 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6238 /* constant expression evaluation / constant folding */
6239 if (get_opt_constant_folding()) {
6240 /* neither constants nor Tuple values can be evaluated */
6241 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6242 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6243 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6244 /* try to evaluate */
6245 tv = computed_value(n);
6246 if (tv != tarval_bad) {
6248 ir_type *old_tp = get_irn_type(n);
6249 int i, arity = get_irn_arity(n);
6253 * Try to recover the type of the new expression.
6255 for (i = 0; i < arity && !old_tp; ++i)
6256 old_tp = get_irn_type(get_irn_n(n, i));
6259 * we MUST copy the node here temporary, because it's still needed
6260 * for DBG_OPT_CSTEVAL
6262 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6263 oldn = alloca(node_size);
6265 memcpy(oldn, n, node_size);
6266 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6268 /* ARG, copy the in array, we need it for statistics */
6269 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6271 /* note the inplace edges module */
6272 edges_node_deleted(n, current_ir_graph);
6274 /* evaluation was successful -- replace the node. */
6275 irg_kill_node(current_ir_graph, n);
6276 nw = new_Const(get_tarval_mode(tv), tv);
6278 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6279 set_Const_type(nw, old_tp);
6280 DBG_OPT_CSTEVAL(oldn, nw);
6281 tarval_enable_fp_ops(old_fp_mode);
6284 tarval_enable_fp_ops(old_fp_mode);
6288 /* remove unnecessary nodes */
6289 if (get_opt_algebraic_simplification() ||
6290 (iro == iro_Phi) || /* always optimize these nodes. */
6292 (iro == iro_Proj) ||
6293 (iro == iro_Block) ) /* Flags tested local. */
6294 n = equivalent_node(n);
6296 /* Common Subexpression Elimination.
6298 * Checks whether n is already available.
6299 * The block input is used to distinguish different subexpressions. Right
6300 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6301 * subexpressions within a block.
6304 n = identify_cons(current_ir_graph->value_table, n);
6307 edges_node_deleted(oldn, current_ir_graph);
6309 /* We found an existing, better node, so we can deallocate the old node. */
6310 irg_kill_node(current_ir_graph, oldn);
6314 /* Some more constant expression evaluation that does not allow to
6316 iro = get_irn_opcode(n);
6317 if (get_opt_algebraic_simplification() ||
6318 (iro == iro_Cond) ||
6319 (iro == iro_Proj)) /* Flags tested local. */
6320 n = transform_node(n);
6322 /* Remove nodes with dead (Bad) input.
6323 Run always for transformation induced Bads. */
6326 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6327 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6328 n = identify_remember(current_ir_graph->value_table, n);
6332 } /* optimize_node */
6336 * These optimizations never deallocate nodes (in place). This can cause dead
6337 * nodes lying on the obstack. Remove these by a dead node elimination,
6338 * i.e., a copying garbage collection.
6340 ir_node *optimize_in_place_2(ir_node *n) {
6343 ir_opcode iro = get_irn_opcode(n);
6345 if (!get_opt_optimize() && !is_Phi(n)) return n;
6347 /* constant expression evaluation / constant folding */
6348 if (get_opt_constant_folding()) {
6349 /* neither constants nor Tuple values can be evaluated */
6350 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6351 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6352 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6353 /* try to evaluate */
6354 tv = computed_value(n);
6355 if (tv != tarval_bad) {
6356 /* evaluation was successful -- replace the node. */
6357 ir_type *old_tp = get_irn_type(n);
6358 int i, arity = get_irn_arity(n);
6361 * Try to recover the type of the new expression.
6363 for (i = 0; i < arity && !old_tp; ++i)
6364 old_tp = get_irn_type(get_irn_n(n, i));
6366 n = new_Const(get_tarval_mode(tv), tv);
6368 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6369 set_Const_type(n, old_tp);
6371 DBG_OPT_CSTEVAL(oldn, n);
6372 tarval_enable_fp_ops(old_fp_mode);
6375 tarval_enable_fp_ops(old_fp_mode);
6379 /* remove unnecessary nodes */
6380 if (get_opt_constant_folding() ||
6381 (iro == iro_Phi) || /* always optimize these nodes. */
6382 (iro == iro_Id) || /* ... */
6383 (iro == iro_Proj) || /* ... */
6384 (iro == iro_Block) ) /* Flags tested local. */
6385 n = equivalent_node(n);
6387 /** common subexpression elimination **/
6388 /* Checks whether n is already available. */
6389 /* The block input is used to distinguish different subexpressions. Right
6390 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6391 subexpressions within a block. */
6392 if (get_opt_cse()) {
6393 n = identify_remember(current_ir_graph->value_table, n);
6396 /* Some more constant expression evaluation. */
6397 iro = get_irn_opcode(n);
6398 if (get_opt_constant_folding() ||
6399 (iro == iro_Cond) ||
6400 (iro == iro_Proj)) /* Flags tested local. */
6401 n = transform_node(n);
6403 /* Remove nodes with dead (Bad) input.
6404 Run always for transformation induced Bads. */
6407 /* Now we can verify the node, as it has no dead inputs any more. */
6410 /* Now we have a legal, useful node. Enter it in hash table for cse.
6411 Blocks should be unique anyways. (Except the successor of start:
6412 is cse with the start block!) */
6413 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6414 n = identify_remember(current_ir_graph->value_table, n);
6417 } /* optimize_in_place_2 */
6420 * Wrapper for external use, set proper status bits after optimization.
6422 ir_node *optimize_in_place(ir_node *n) {
6423 /* Handle graph state */
6424 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6426 if (get_opt_global_cse())
6427 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6428 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6429 set_irg_outs_inconsistent(current_ir_graph);
6431 /* FIXME: Maybe we could also test whether optimizing the node can
6432 change the control graph. */
6433 set_irg_doms_inconsistent(current_ir_graph);
6434 return optimize_in_place_2(n);
6435 } /* optimize_in_place */
6438 * Calculate a hash value of a Const node.
6440 static unsigned hash_Const(const ir_node *node) {
6443 /* special value for const, as they only differ in their tarval. */
6444 h = HASH_PTR(node->attr.con.tv);
6445 h = 9*h + HASH_PTR(get_irn_mode(node));
6451 * Calculate a hash value of a SymConst node.
6453 static unsigned hash_SymConst(const ir_node *node) {
6456 /* special value for const, as they only differ in their symbol. */
6457 h = HASH_PTR(node->attr.symc.sym.type_p);
6458 h = 9*h + HASH_PTR(get_irn_mode(node));
6461 } /* hash_SymConst */
6464 * Set the default hash operation in an ir_op_ops.
6466 * @param code the opcode for the default operation
6467 * @param ops the operations initialized
6472 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6476 ops->hash = hash_##a; \
6479 /* hash function already set */
6480 if (ops->hash != NULL)
6487 /* use input/mode default hash if no function was given */
6488 ops->hash = firm_default_hash;
6496 * Sets the default operation for an ir_ops.
6498 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6499 ops = firm_set_default_hash(code, ops);
6500 ops = firm_set_default_computed_value(code, ops);
6501 ops = firm_set_default_equivalent_node(code, ops);
6502 ops = firm_set_default_transform_node(code, ops);
6503 ops = firm_set_default_node_cmp_attr(code, ops);
6504 ops = firm_set_default_get_type(code, ops);
6505 ops = firm_set_default_get_type_attr(code, ops);
6506 ops = firm_set_default_get_entity_attr(code, ops);
6509 } /* firm_set_default_operations */