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 (tb != 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 computed_value(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 *oldn = proj;
1583 ir_node *load = get_Proj_pred(proj);
1585 /* get the Load address */
1586 ir_node *addr = get_Load_ptr(load);
1587 ir_node *blk = get_nodes_block(load);
1590 if (value_not_null(addr, &confirm)) {
1591 if (get_Proj_proj(proj) == pn_Load_X_except) {
1592 DBG_OPT_EXC_REM(proj);
1593 return get_irg_bad(current_ir_graph);
1599 } /* equivalent_node_Proj_Load */
1602 * Optimize an Exception Proj(Store) with a non-null address.
1604 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1605 if (get_opt_ldst_only_null_ptr_exceptions()) {
1606 if (get_irn_mode(proj) == mode_X) {
1607 ir_node *oldn = proj;
1608 ir_node *store = get_Proj_pred(proj);
1610 /* get the load/store address */
1611 ir_node *addr = get_Store_ptr(store);
1612 ir_node *blk = get_nodes_block(store);
1615 if (value_not_null(addr, &confirm)) {
1616 if (get_Proj_proj(proj) == pn_Store_X_except) {
1617 DBG_OPT_EXC_REM(proj);
1618 return get_irg_bad(current_ir_graph);
1624 } /* equivalent_node_Proj_Store */
1627 * Does all optimizations on nodes that must be done on it's Proj's
1628 * because of creating new nodes.
1630 static ir_node *equivalent_node_Proj(ir_node *proj) {
1631 ir_node *n = get_Proj_pred(proj);
1633 switch (get_irn_opcode(n)) {
1635 return equivalent_node_Proj_Div(proj);
1638 return equivalent_node_Proj_DivMod(proj);
1641 return equivalent_node_Proj_DivMod(proj);
1644 return equivalent_node_Proj_Tuple(proj);
1647 return equivalent_node_Proj_CopyB(proj);
1650 return equivalent_node_Proj_Bound(proj);
1653 return equivalent_node_Proj_Load(proj);
1656 return equivalent_node_Proj_Store(proj);
1659 if (get_irn_mode(proj) == mode_X) {
1660 if (is_Block_dead(get_nodes_block(n))) {
1661 /* Remove dead control flow -- early gigo(). */
1662 proj = get_irg_bad(current_ir_graph);
1667 } /* equivalent_node_Proj */
1672 static ir_node *equivalent_node_Id(ir_node *n) {
1679 DBG_OPT_ID(oldn, n);
1681 } /* equivalent_node_Id */
1686 static ir_node *equivalent_node_Mux(ir_node *n)
1688 ir_node *oldn = n, *sel = get_Mux_sel(n);
1689 tarval *ts = value_of(sel);
1691 /* Mux(true, f, t) == t */
1692 if (ts == tarval_b_true) {
1693 n = get_Mux_true(n);
1694 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1696 /* Mux(false, f, t) == f */
1697 else if (ts == tarval_b_false) {
1698 n = get_Mux_false(n);
1699 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1701 /* Mux(v, x, x) == x */
1702 else if (get_Mux_false(n) == get_Mux_true(n)) {
1703 n = get_Mux_true(n);
1704 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1706 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1707 ir_node *cmp = get_Proj_pred(sel);
1708 long proj_nr = get_Proj_proj(sel);
1709 ir_node *f = get_Mux_false(n);
1710 ir_node *t = get_Mux_true(n);
1713 * Note further that these optimization work even for floating point
1714 * with NaN's because -NaN == NaN.
1715 * However, if +0 and -0 is handled differently, we cannot use the first one.
1718 ir_node *const cmp_l = get_Cmp_left(cmp);
1719 ir_node *const cmp_r = get_Cmp_right(cmp);
1723 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1724 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1726 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1733 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1734 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1736 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1743 * Note: normalization puts the constant on the right side,
1744 * so we check only one case.
1746 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1747 /* Mux(t CMP 0, X, t) */
1748 if (is_Minus(f) && get_Minus_op(f) == t) {
1749 /* Mux(t CMP 0, -t, t) */
1750 if (proj_nr == pn_Cmp_Eq) {
1751 /* Mux(t == 0, -t, t) ==> -t */
1753 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1754 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1755 /* Mux(t != 0, -t, t) ==> t */
1757 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1764 } /* equivalent_node_Mux */
1767 * Returns a equivalent node of a Psi: if a condition is true
1768 * and all previous conditions are false we know its value.
1769 * If all conditions are false its value is the default one.
1771 static ir_node *equivalent_node_Psi(ir_node *n) {
1773 return equivalent_node_Mux(n);
1775 } /* equivalent_node_Psi */
1778 * Remove Confirm nodes if setting is on.
1779 * Replace Confirms(x, '=', Constlike) by Constlike.
1781 static ir_node *equivalent_node_Confirm(ir_node *n) {
1782 ir_node *pred = get_Confirm_value(n);
1783 pn_Cmp pnc = get_Confirm_cmp(n);
1785 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1787 * rare case: two identical Confirms one after another,
1788 * replace the second one with the first.
1791 pred = get_Confirm_value(n);
1792 pnc = get_Confirm_cmp(n);
1794 if (get_opt_remove_confirm())
1795 return get_Confirm_value(n);
1800 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1801 * perform no actual computation, as, e.g., the Id nodes. It does not create
1802 * new nodes. It is therefore safe to free n if the node returned is not n.
1803 * If a node returns a Tuple we can not just skip it. If the size of the
1804 * in array fits, we transform n into a tuple (e.g., Div).
1806 ir_node *equivalent_node(ir_node *n) {
1807 if (n->op->ops.equivalent_node)
1808 return n->op->ops.equivalent_node(n);
1810 } /* equivalent_node */
1813 * Sets the default equivalent node operation for an ir_op_ops.
1815 * @param code the opcode for the default operation
1816 * @param ops the operations initialized
1821 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1825 ops->equivalent_node = equivalent_node_##a; \
1859 } /* firm_set_default_equivalent_node */
1862 * Returns non-zero if a node is a Phi node
1863 * with all predecessors constant.
1865 static int is_const_Phi(ir_node *n) {
1868 if (! is_Phi(n) || get_irn_arity(n) == 0)
1870 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1871 if (! is_Const(get_irn_n(n, i)))
1874 } /* is_const_Phi */
1876 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1877 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1880 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1882 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1883 if (eval == tarval_sub) {
1884 tarval_sub_type func = (tarval_sub_type)eval;
1886 return func(a, b, mode);
1888 tarval_binop_type func = (tarval_binop_type)eval;
1895 * Apply an evaluator on a binop with a constant operators (and one Phi).
1897 * @param phi the Phi node
1898 * @param other the other operand
1899 * @param eval an evaluator function
1900 * @param mode the mode of the result, may be different from the mode of the Phi!
1901 * @param left if non-zero, other is the left operand, else the right
1903 * @return a new Phi node if the conversion was successful, NULL else
1905 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1910 int i, n = get_irn_arity(phi);
1912 NEW_ARR_A(void *, res, n);
1914 for (i = 0; i < n; ++i) {
1915 pred = get_irn_n(phi, i);
1916 tv = get_Const_tarval(pred);
1917 tv = do_eval(eval, other, tv, mode);
1919 if (tv == tarval_bad) {
1920 /* folding failed, bad */
1926 for (i = 0; i < n; ++i) {
1927 pred = get_irn_n(phi, i);
1928 tv = get_Const_tarval(pred);
1929 tv = do_eval(eval, tv, other, mode);
1931 if (tv == tarval_bad) {
1932 /* folding failed, bad */
1938 irg = current_ir_graph;
1939 for (i = 0; i < n; ++i) {
1940 pred = get_irn_n(phi, i);
1941 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1942 mode, res[i], get_Const_type(pred));
1944 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1945 } /* apply_binop_on_phi */
1948 * Apply an evaluator on a binop with two constant Phi.
1950 * @param a the left Phi node
1951 * @param b the right Phi node
1952 * @param eval an evaluator function
1953 * @param mode the mode of the result, may be different from the mode of the Phi!
1955 * @return a new Phi node if the conversion was successful, NULL else
1957 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1958 tarval *tv_l, *tv_r, *tv;
1964 if (get_nodes_block(a) != get_nodes_block(b))
1967 n = get_irn_arity(a);
1968 NEW_ARR_A(void *, res, n);
1970 for (i = 0; i < n; ++i) {
1971 pred = get_irn_n(a, i);
1972 tv_l = get_Const_tarval(pred);
1973 pred = get_irn_n(b, i);
1974 tv_r = get_Const_tarval(pred);
1975 tv = do_eval(eval, tv_l, tv_r, mode);
1977 if (tv == tarval_bad) {
1978 /* folding failed, bad */
1983 irg = current_ir_graph;
1984 for (i = 0; i < n; ++i) {
1985 pred = get_irn_n(a, i);
1986 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1988 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1989 } /* apply_binop_on_2_phis */
1992 * Apply an evaluator on a unop with a constant operator (a Phi).
1994 * @param phi the Phi node
1995 * @param eval an evaluator function
1997 * @return a new Phi node if the conversion was successful, NULL else
1999 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2005 int i, n = get_irn_arity(phi);
2007 NEW_ARR_A(void *, res, n);
2008 for (i = 0; i < n; ++i) {
2009 pred = get_irn_n(phi, i);
2010 tv = get_Const_tarval(pred);
2013 if (tv == tarval_bad) {
2014 /* folding failed, bad */
2019 mode = get_irn_mode(phi);
2020 irg = current_ir_graph;
2021 for (i = 0; i < n; ++i) {
2022 pred = get_irn_n(phi, i);
2023 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2024 mode, res[i], get_Const_type(pred));
2026 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2027 } /* apply_unop_on_phi */
2030 * Apply a conversion on a constant operator (a Phi).
2032 * @param phi the Phi node
2034 * @return a new Phi node if the conversion was successful, NULL else
2036 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2041 int i, n = get_irn_arity(phi);
2043 NEW_ARR_A(void *, res, n);
2044 for (i = 0; i < n; ++i) {
2045 pred = get_irn_n(phi, i);
2046 tv = get_Const_tarval(pred);
2047 tv = tarval_convert_to(tv, mode);
2049 if (tv == tarval_bad) {
2050 /* folding failed, bad */
2055 irg = current_ir_graph;
2056 for (i = 0; i < n; ++i) {
2057 pred = get_irn_n(phi, i);
2058 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2059 mode, res[i], get_Const_type(pred));
2061 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2062 } /* apply_conv_on_phi */
2065 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2066 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2067 * If possible, remove the Conv's.
2069 static ir_node *transform_node_AddSub(ir_node *n) {
2070 ir_mode *mode = get_irn_mode(n);
2072 if (mode_is_reference(mode)) {
2073 ir_node *left = get_binop_left(n);
2074 ir_node *right = get_binop_right(n);
2075 unsigned ref_bits = get_mode_size_bits(mode);
2077 if (is_Conv(left)) {
2078 ir_mode *lmode = get_irn_mode(left);
2079 unsigned bits = get_mode_size_bits(lmode);
2081 if (ref_bits == bits &&
2082 mode_is_int(lmode) &&
2083 get_mode_arithmetic(lmode) == irma_twos_complement) {
2084 ir_node *pre = get_Conv_op(left);
2085 ir_mode *pre_mode = get_irn_mode(pre);
2087 if (mode_is_int(pre_mode) &&
2088 get_mode_size_bits(pre_mode) == bits &&
2089 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2090 /* ok, this conv just changes to sign, moreover the calculation
2091 * is done with same number of bits as our address mode, so
2092 * we can ignore the conv as address calculation can be viewed
2093 * as either signed or unsigned
2095 set_binop_left(n, pre);
2100 if (is_Conv(right)) {
2101 ir_mode *rmode = get_irn_mode(right);
2102 unsigned bits = get_mode_size_bits(rmode);
2104 if (ref_bits == bits &&
2105 mode_is_int(rmode) &&
2106 get_mode_arithmetic(rmode) == irma_twos_complement) {
2107 ir_node *pre = get_Conv_op(right);
2108 ir_mode *pre_mode = get_irn_mode(pre);
2110 if (mode_is_int(pre_mode) &&
2111 get_mode_size_bits(pre_mode) == bits &&
2112 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2113 /* ok, this conv just changes to sign, moreover the calculation
2114 * is done with same number of bits as our address mode, so
2115 * we can ignore the conv as address calculation can be viewed
2116 * as either signed or unsigned
2118 set_binop_right(n, pre);
2123 /* let address arithmetic use unsigned modes */
2124 if (is_Const(right)) {
2125 ir_mode *rmode = get_irn_mode(right);
2127 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2128 /* convert a AddP(P, *s) into AddP(P, *u) */
2129 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2131 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2132 set_binop_right(n, pre);
2138 } /* transform_node_AddSub */
2140 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2142 if (is_Const(b) && is_const_Phi(a)) { \
2143 /* check for Op(Phi, Const) */ \
2144 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2146 else if (is_Const(a) && is_const_Phi(b)) { \
2147 /* check for Op(Const, Phi) */ \
2148 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2150 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2151 /* check for Op(Phi, Phi) */ \
2152 c = apply_binop_on_2_phis(a, b, eval, mode); \
2155 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2159 #define HANDLE_UNOP_PHI(eval, a, c) \
2161 if (is_const_Phi(a)) { \
2162 /* check for Op(Phi) */ \
2163 c = apply_unop_on_phi(a, eval); \
2165 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2171 * Do the AddSub optimization, then Transform
2172 * Constant folding on Phi
2173 * Add(a,a) -> Mul(a, 2)
2174 * Add(Mul(a, x), a) -> Mul(a, x+1)
2175 * if the mode is integer or float.
2176 * Transform Add(a,-b) into Sub(a,b).
2177 * Reassociation might fold this further.
2179 static ir_node *transform_node_Add(ir_node *n) {
2181 ir_node *a, *b, *c, *oldn = n;
2183 n = transform_node_AddSub(n);
2185 a = get_Add_left(n);
2186 b = get_Add_right(n);
2188 mode = get_irn_mode(n);
2190 if (mode_is_reference(mode)) {
2191 ir_mode *lmode = get_irn_mode(a);
2193 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2194 /* an Add(a, NULL) is a hidden Conv */
2195 dbg_info *dbg = get_irn_dbg_info(n);
2196 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2200 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2202 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2203 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2206 if (mode_is_num(mode)) {
2207 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2208 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2209 ir_node *block = get_nodes_block(n);
2212 get_irn_dbg_info(n),
2216 new_r_Const_long(current_ir_graph, block, mode, 2),
2218 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2223 get_irn_dbg_info(n),
2229 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2234 get_irn_dbg_info(n),
2240 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2243 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2244 /* Here we rely on constants be on the RIGHT side */
2246 ir_node *op = get_Not_op(a);
2248 if (is_Const(b) && is_Const_one(b)) {
2250 ir_node *blk = get_irn_n(n, -1);
2251 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2252 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2257 ir_node *blk = get_irn_n(n, -1);
2258 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2259 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2264 ir_node *op = get_Not_op(b);
2268 ir_node *blk = get_irn_n(n, -1);
2269 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2270 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2277 } /* transform_node_Add */
2280 * returns -cnst or NULL if impossible
2282 static ir_node *const_negate(ir_node *cnst) {
2283 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2284 dbg_info *dbgi = get_irn_dbg_info(cnst);
2285 ir_graph *irg = get_irn_irg(cnst);
2286 ir_node *block = get_nodes_block(cnst);
2287 ir_mode *mode = get_irn_mode(cnst);
2288 if (tv == tarval_bad) return NULL;
2289 return new_rd_Const(dbgi, irg, block, mode, tv);
2293 * Do the AddSub optimization, then Transform
2294 * Constant folding on Phi
2295 * Sub(0,a) -> Minus(a)
2296 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2297 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2298 * Sub(Add(a, x), x) -> a
2299 * Sub(x, Add(x, a)) -> -a
2300 * Sub(x, Const) -> Add(x, -Const)
2302 static ir_node *transform_node_Sub(ir_node *n) {
2307 n = transform_node_AddSub(n);
2309 a = get_Sub_left(n);
2310 b = get_Sub_right(n);
2312 mode = get_irn_mode(n);
2314 if (mode_is_int(mode)) {
2315 ir_mode *lmode = get_irn_mode(a);
2317 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2318 /* a Sub(a, NULL) is a hidden Conv */
2319 dbg_info *dbg = get_irn_dbg_info(n);
2320 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2325 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2327 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2328 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2331 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2332 /* a - C -> a + (-C) */
2333 ir_node *cnst = const_negate(b);
2335 ir_node *block = get_nodes_block(n);
2336 dbg_info *dbgi = get_irn_dbg_info(n);
2337 ir_graph *irg = get_irn_irg(n);
2339 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2340 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2345 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2346 ir_graph *irg = current_ir_graph;
2347 dbg_info *dbg = get_irn_dbg_info(n);
2348 ir_node *block = get_nodes_block(n);
2349 ir_node *left = get_Minus_op(a);
2350 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2352 n = new_rd_Minus(dbg, irg, block, add, mode);
2353 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2355 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2356 ir_graph *irg = current_ir_graph;
2357 dbg_info *dbg = get_irn_dbg_info(n);
2358 ir_node *block = get_nodes_block(n);
2359 ir_node *right = get_Minus_op(b);
2361 n = new_rd_Add(dbg, irg, block, a, right, mode);
2362 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2364 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2365 ir_graph *irg = current_ir_graph;
2366 dbg_info *s_dbg = get_irn_dbg_info(b);
2367 ir_node *s_block = get_nodes_block(b);
2368 ir_node *s_left = get_Sub_right(b);
2369 ir_node *s_right = get_Sub_left(b);
2370 ir_mode *s_mode = get_irn_mode(b);
2371 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2372 dbg_info *a_dbg = get_irn_dbg_info(n);
2373 ir_node *a_block = get_nodes_block(n);
2375 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2376 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2378 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2379 ir_node *m_right = get_Mul_right(b);
2380 if (is_Const(m_right)) {
2381 ir_node *cnst2 = const_negate(m_right);
2382 if (cnst2 != NULL) {
2383 ir_graph *irg = current_ir_graph;
2384 dbg_info *m_dbg = get_irn_dbg_info(b);
2385 ir_node *m_block = get_nodes_block(b);
2386 ir_node *m_left = get_Mul_left(b);
2387 ir_mode *m_mode = get_irn_mode(b);
2388 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2389 dbg_info *a_dbg = get_irn_dbg_info(n);
2390 ir_node *a_block = get_nodes_block(n);
2392 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2399 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2400 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2402 get_irn_dbg_info(n),
2407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2411 if (mode_wrap_around(mode)) {
2412 ir_node *left = get_Add_left(a);
2413 ir_node *right = get_Add_right(a);
2415 /* FIXME: Does the Conv's work only for two complement or generally? */
2417 if (mode != get_irn_mode(right)) {
2418 /* This Sub is an effective Cast */
2419 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2422 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2424 } else if (right == b) {
2425 if (mode != get_irn_mode(left)) {
2426 /* This Sub is an effective Cast */
2427 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2430 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2436 if (mode_wrap_around(mode)) {
2437 ir_node *left = get_Add_left(b);
2438 ir_node *right = get_Add_right(b);
2440 /* FIXME: Does the Conv's work only for two complement or generally? */
2442 ir_mode *r_mode = get_irn_mode(right);
2444 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2445 if (mode != r_mode) {
2446 /* This Sub is an effective Cast */
2447 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2449 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2451 } else if (right == a) {
2452 ir_mode *l_mode = get_irn_mode(left);
2454 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2455 if (mode != l_mode) {
2456 /* This Sub is an effective Cast */
2457 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2459 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2464 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2465 ir_mode *mode = get_irn_mode(a);
2467 if (mode == get_irn_mode(b)) {
2469 ir_node *op_a = get_Conv_op(a);
2470 ir_node *op_b = get_Conv_op(b);
2472 /* check if it's allowed to skip the conv */
2473 ma = get_irn_mode(op_a);
2474 mb = get_irn_mode(op_b);
2476 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2477 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2480 set_Sub_right(n, b);
2486 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2487 if (!is_reassoc_running() && is_Mul(a)) {
2488 ir_node *ma = get_Mul_left(a);
2489 ir_node *mb = get_Mul_right(a);
2492 ir_node *blk = get_irn_n(n, -1);
2494 get_irn_dbg_info(n),
2495 current_ir_graph, blk,
2498 get_irn_dbg_info(n),
2499 current_ir_graph, blk,
2501 new_r_Const_long(current_ir_graph, blk, mode, 1),
2504 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2506 } else if (mb == b) {
2507 ir_node *blk = get_irn_n(n, -1);
2509 get_irn_dbg_info(n),
2510 current_ir_graph, blk,
2513 get_irn_dbg_info(n),
2514 current_ir_graph, blk,
2516 new_r_Const_long(current_ir_graph, blk, mode, 1),
2519 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2523 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2524 ir_node *x = get_Sub_left(a);
2525 ir_node *y = get_Sub_right(a);
2526 ir_node *blk = get_irn_n(n, -1);
2527 ir_mode *m_b = get_irn_mode(b);
2528 ir_mode *m_y = get_irn_mode(y);
2532 /* Determine the right mode for the Add. */
2535 else if (mode_is_reference(m_b))
2537 else if (mode_is_reference(m_y))
2541 * Both modes are different but none is reference,
2542 * happens for instance in SubP(SubP(P, Iu), Is).
2543 * We have two possibilities here: Cast or ignore.
2544 * Currently we ignore this case.
2549 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2551 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2552 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2556 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2557 if (is_Const(a) && is_Not(b)) {
2558 /* c - ~X = X + (c+1) */
2559 tarval *tv = get_Const_tarval(a);
2561 tv = tarval_add(tv, get_mode_one(mode));
2562 if (tv != tarval_bad) {
2563 ir_node *blk = get_irn_n(n, -1);
2564 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2565 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2566 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2572 } /* transform_node_Sub */
2575 * Several transformation done on n*n=2n bits mul.
2576 * These transformations must be done here because new nodes may be produced.
2578 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2580 ir_node *a = get_Mul_left(n);
2581 ir_node *b = get_Mul_right(n);
2582 tarval *ta = value_of(a);
2583 tarval *tb = value_of(b);
2584 ir_mode *smode = get_irn_mode(a);
2586 if (ta == get_mode_one(smode)) {
2587 /* (L)1 * (L)b = (L)b */
2588 ir_node *blk = get_irn_n(n, -1);
2589 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2590 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2593 else if (ta == get_mode_minus_one(smode)) {
2594 /* (L)-1 * (L)b = (L)b */
2595 ir_node *blk = get_irn_n(n, -1);
2596 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2597 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2598 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2601 if (tb == get_mode_one(smode)) {
2602 /* (L)a * (L)1 = (L)a */
2603 ir_node *blk = get_irn_n(a, -1);
2604 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2605 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2608 else if (tb == get_mode_minus_one(smode)) {
2609 /* (L)a * (L)-1 = (L)-a */
2610 ir_node *blk = get_irn_n(n, -1);
2611 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2612 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2613 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2620 * Transform Mul(a,-1) into -a.
2621 * Do constant evaluation of Phi nodes.
2622 * Do architecture dependent optimizations on Mul nodes
2624 static ir_node *transform_node_Mul(ir_node *n) {
2625 ir_node *c, *oldn = n;
2626 ir_mode *mode = get_irn_mode(n);
2627 ir_node *a = get_Mul_left(n);
2628 ir_node *b = get_Mul_right(n);
2630 if (is_Bad(a) || is_Bad(b))
2633 if (mode != get_irn_mode(a))
2634 return transform_node_Mul2n(n, mode);
2636 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2638 if (mode_is_signed(mode)) {
2641 if (value_of(a) == get_mode_minus_one(mode))
2643 else if (value_of(b) == get_mode_minus_one(mode))
2646 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2647 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2652 if (is_Const(b)) { /* (-a) * const -> a * -const */
2653 ir_node *cnst = const_negate(b);
2655 dbg_info *dbgi = get_irn_dbg_info(n);
2656 ir_node *block = get_nodes_block(n);
2657 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2658 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2661 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2662 dbg_info *dbgi = get_irn_dbg_info(n);
2663 ir_node *block = get_nodes_block(n);
2664 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2665 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2667 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2668 ir_node *sub_l = get_Sub_left(b);
2669 ir_node *sub_r = get_Sub_right(b);
2670 dbg_info *dbgi = get_irn_dbg_info(n);
2671 ir_graph *irg = current_ir_graph;
2672 ir_node *block = get_nodes_block(n);
2673 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2674 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2675 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2678 } else if (is_Minus(b)) {
2679 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2680 ir_node *sub_l = get_Sub_left(a);
2681 ir_node *sub_r = get_Sub_right(a);
2682 dbg_info *dbgi = get_irn_dbg_info(n);
2683 ir_graph *irg = current_ir_graph;
2684 ir_node *block = get_nodes_block(n);
2685 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2686 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2687 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2691 if (get_mode_arithmetic(mode) == irma_ieee754) {
2693 tarval *tv = get_Const_tarval(a);
2694 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2695 /* 2.0 * b = b + b */
2696 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2697 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2701 else if (is_Const(b)) {
2702 tarval *tv = get_Const_tarval(b);
2703 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2704 /* a * 2.0 = a + a */
2705 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2706 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2711 return arch_dep_replace_mul_with_shifts(n);
2712 } /* transform_node_Mul */
2715 * Transform a Div Node.
2717 static ir_node *transform_node_Div(ir_node *n) {
2718 ir_mode *mode = get_Div_resmode(n);
2719 ir_node *a = get_Div_left(n);
2720 ir_node *b = get_Div_right(n);
2724 if (is_Const(b) && is_const_Phi(a)) {
2725 /* check for Div(Phi, Const) */
2726 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2728 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2732 else if (is_Const(a) && is_const_Phi(b)) {
2733 /* check for Div(Const, Phi) */
2734 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2736 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2740 else if (is_const_Phi(a) && is_const_Phi(b)) {
2741 /* check for Div(Phi, Phi) */
2742 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2744 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2751 if (tv != tarval_bad) {
2752 value = new_Const(get_tarval_mode(tv), tv);
2754 DBG_OPT_CSTEVAL(n, value);
2757 ir_node *a = get_Div_left(n);
2758 ir_node *b = get_Div_right(n);
2761 if (a == b && value_not_zero(a, &dummy)) {
2762 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2763 value = new_Const(mode, get_mode_one(mode));
2764 DBG_OPT_CSTEVAL(n, value);
2767 if (mode_is_signed(mode) && is_Const(b)) {
2768 tarval *tv = get_Const_tarval(b);
2770 if (tv == get_mode_minus_one(mode)) {
2772 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2773 DBG_OPT_CSTEVAL(n, value);
2777 /* Try architecture dependent optimization */
2778 value = arch_dep_replace_div_by_const(n);
2786 /* Turn Div into a tuple (mem, jmp, bad, value) */
2787 mem = get_Div_mem(n);
2788 blk = get_irn_n(n, -1);
2790 /* skip a potential Pin */
2792 mem = get_Pin_op(mem);
2793 turn_into_tuple(n, pn_Div_max);
2794 set_Tuple_pred(n, pn_Div_M, mem);
2795 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2796 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2797 set_Tuple_pred(n, pn_Div_res, value);
2800 } /* transform_node_Div */
2803 * Transform a Mod node.
2805 static ir_node *transform_node_Mod(ir_node *n) {
2806 ir_mode *mode = get_Mod_resmode(n);
2807 ir_node *a = get_Mod_left(n);
2808 ir_node *b = get_Mod_right(n);
2812 if (is_Const(b) && is_const_Phi(a)) {
2813 /* check for Div(Phi, Const) */
2814 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2816 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2820 else if (is_Const(a) && is_const_Phi(b)) {
2821 /* check for Div(Const, Phi) */
2822 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2824 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2828 else if (is_const_Phi(a) && is_const_Phi(b)) {
2829 /* check for Div(Phi, Phi) */
2830 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2832 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2839 if (tv != tarval_bad) {
2840 value = new_Const(get_tarval_mode(tv), tv);
2842 DBG_OPT_CSTEVAL(n, value);
2845 ir_node *a = get_Mod_left(n);
2846 ir_node *b = get_Mod_right(n);
2849 if (a == b && value_not_zero(a, &dummy)) {
2850 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2851 value = new_Const(mode, get_mode_null(mode));
2852 DBG_OPT_CSTEVAL(n, value);
2855 if (mode_is_signed(mode) && is_Const(b)) {
2856 tarval *tv = get_Const_tarval(b);
2858 if (tv == get_mode_minus_one(mode)) {
2860 value = new_Const(mode, get_mode_null(mode));
2861 DBG_OPT_CSTEVAL(n, value);
2865 /* Try architecture dependent optimization */
2866 value = arch_dep_replace_mod_by_const(n);
2874 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2875 mem = get_Mod_mem(n);
2876 blk = get_irn_n(n, -1);
2878 /* skip a potential Pin */
2880 mem = get_Pin_op(mem);
2881 turn_into_tuple(n, pn_Mod_max);
2882 set_Tuple_pred(n, pn_Mod_M, mem);
2883 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2884 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2885 set_Tuple_pred(n, pn_Mod_res, value);
2888 } /* transform_node_Mod */
2891 * Transform a DivMod node.
2893 static ir_node *transform_node_DivMod(ir_node *n) {
2895 ir_node *a = get_DivMod_left(n);
2896 ir_node *b = get_DivMod_right(n);
2897 ir_mode *mode = get_DivMod_resmode(n);
2902 if (is_Const(b) && is_const_Phi(a)) {
2903 /* check for Div(Phi, Const) */
2904 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2905 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2907 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2908 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2912 else if (is_Const(a) && is_const_Phi(b)) {
2913 /* check for Div(Const, Phi) */
2914 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2915 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2917 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2918 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2922 else if (is_const_Phi(a) && is_const_Phi(b)) {
2923 /* check for Div(Phi, Phi) */
2924 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2925 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2927 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2928 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2935 if (tb != tarval_bad) {
2936 if (tb == get_mode_one(get_tarval_mode(tb))) {
2938 vb = new_Const(mode, get_mode_null(mode));
2939 DBG_OPT_CSTEVAL(n, vb);
2941 } else if (ta != tarval_bad) {
2942 tarval *resa, *resb;
2943 resa = tarval_div(ta, tb);
2944 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2945 Jmp for X result!? */
2946 resb = tarval_mod(ta, tb);
2947 if (resb == tarval_bad) return n; /* Causes exception! */
2948 va = new_Const(mode, resa);
2949 vb = new_Const(mode, resb);
2950 DBG_OPT_CSTEVAL(n, va);
2951 DBG_OPT_CSTEVAL(n, vb);
2953 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2954 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2955 vb = new_Const(mode, get_mode_null(mode));
2956 DBG_OPT_CSTEVAL(n, va);
2957 DBG_OPT_CSTEVAL(n, vb);
2959 } else { /* Try architecture dependent optimization */
2962 arch_dep_replace_divmod_by_const(&va, &vb, n);
2963 evaluated = va != NULL;
2965 } else if (a == b) {
2966 if (value_not_zero(a, &dummy)) {
2968 va = new_Const(mode, get_mode_one(mode));
2969 vb = new_Const(mode, get_mode_null(mode));
2970 DBG_OPT_CSTEVAL(n, va);
2971 DBG_OPT_CSTEVAL(n, vb);
2974 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2977 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2978 /* 0 / non-Const = 0 */
2983 if (evaluated) { /* replace by tuple */
2987 mem = get_DivMod_mem(n);
2988 /* skip a potential Pin */
2990 mem = get_Pin_op(mem);
2992 blk = get_irn_n(n, -1);
2993 turn_into_tuple(n, pn_DivMod_max);
2994 set_Tuple_pred(n, pn_DivMod_M, mem);
2995 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2996 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2997 set_Tuple_pred(n, pn_DivMod_res_div, va);
2998 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3002 } /* transform_node_DivMod */
3005 * Optimize x / c to x * (1/c)
3007 static ir_node *transform_node_Quot(ir_node *n) {
3008 ir_mode *mode = get_Quot_resmode(n);
3011 if (get_mode_arithmetic(mode) == irma_ieee754) {
3012 ir_node *b = get_Quot_right(n);
3015 tarval *tv = get_Const_tarval(b);
3019 * Floating point constant folding might be disabled here to
3021 * However, as we check for exact result, doing it is safe.
3024 rem = tarval_enable_fp_ops(1);
3025 tv = tarval_quo(get_mode_one(mode), tv);
3026 (void)tarval_enable_fp_ops(rem);
3028 /* Do the transformation if the result is either exact or we are not
3029 using strict rules. */
3030 if (tv != tarval_bad &&
3031 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3032 ir_node *blk = get_irn_n(n, -1);
3033 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3034 ir_node *a = get_Quot_left(n);
3035 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3036 ir_node *mem = get_Quot_mem(n);
3038 /* skip a potential Pin */
3040 mem = get_Pin_op(mem);
3041 turn_into_tuple(n, pn_Quot_max);
3042 set_Tuple_pred(n, pn_Quot_M, mem);
3043 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3044 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3045 set_Tuple_pred(n, pn_Quot_res, m);
3046 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3051 } /* transform_node_Quot */
3054 * Optimize Abs(x) into x if x is Confirmed >= 0
3055 * Optimize Abs(x) into -x if x is Confirmed <= 0
3056 * Optimize Abs(-x) int Abs(x)
3058 static ir_node *transform_node_Abs(ir_node *n) {
3059 ir_node *c, *oldn = n;
3060 ir_node *a = get_Abs_op(n);
3063 HANDLE_UNOP_PHI(tarval_abs, a, c);
3065 switch (classify_value_sign(a)) {
3066 case value_classified_negative:
3067 mode = get_irn_mode(n);
3070 * We can replace the Abs by -x here.
3071 * We even could add a new Confirm here
3072 * (if not twos complement)
3074 * Note that -x would create a new node, so we could
3075 * not run it in the equivalent_node() context.
3077 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3078 get_nodes_block(n), a, mode);
3080 DBG_OPT_CONFIRM(oldn, n);
3082 case value_classified_positive:
3083 /* n is positive, Abs is not needed */
3086 DBG_OPT_CONFIRM(oldn, n);
3092 /* Abs(-x) = Abs(x) */
3093 mode = get_irn_mode(n);
3094 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3095 get_nodes_block(n), get_Minus_op(a), mode);
3096 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3100 } /* transform_node_Abs */
3103 * Optimize -a CMP -b into b CMP a.
3104 * This works only for for modes where unary Minus
3106 * Note that two-complement integers can Overflow
3107 * so it will NOT work.
3109 * For == and != can be handled in Proj(Cmp)
3111 static ir_node *transform_node_Cmp(ir_node *n) {
3113 ir_node *left = get_Cmp_left(n);
3114 ir_node *right = get_Cmp_right(n);
3116 if (is_Minus(left) && is_Minus(right) &&
3117 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3118 left = get_Minus_op(left);
3119 right = get_Minus_op(right);
3120 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3121 get_nodes_block(n), left, right);
3122 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3125 } /* transform_node_Cmp */
3129 * Transform a Cond node.
3131 * Replace the Cond by a Jmp if it branches on a constant
3134 static ir_node *transform_node_Cond(ir_node *n) {
3137 ir_node *a = get_Cond_selector(n);
3138 tarval *ta = value_of(a);
3140 /* we need block info which is not available in floating irgs */
3141 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3144 if ((ta != tarval_bad) &&
3145 (get_irn_mode(a) == mode_b) &&
3146 (get_opt_unreachable_code())) {
3147 /* It's a boolean Cond, branching on a boolean constant.
3148 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3149 ir_node *blk = get_nodes_block(n);
3150 jmp = new_r_Jmp(current_ir_graph, blk);
3151 turn_into_tuple(n, pn_Cond_max);
3152 if (ta == tarval_b_true) {
3153 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3154 set_Tuple_pred(n, pn_Cond_true, jmp);
3156 set_Tuple_pred(n, pn_Cond_false, jmp);
3157 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3159 /* We might generate an endless loop, so keep it alive. */
3160 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3163 } /* transform_node_Cond */
3166 * Prototype of a recursive transform function
3167 * for bitwise distributive transformations.
3169 typedef ir_node* (*recursive_transform)(ir_node *n);
3172 * makes use of distributive laws for and, or, eor
3173 * and(a OP c, b OP c) -> and(a, b) OP c
3174 * note, might return a different op than n
3176 static ir_node *transform_bitwise_distributive(ir_node *n,
3177 recursive_transform trans_func)
3180 ir_node *a = get_binop_left(n);
3181 ir_node *b = get_binop_right(n);
3182 ir_op *op = get_irn_op(a);
3183 ir_op *op_root = get_irn_op(n);
3185 if(op != get_irn_op(b))
3188 if (op == op_Conv) {
3189 ir_node *a_op = get_Conv_op(a);
3190 ir_node *b_op = get_Conv_op(b);
3191 ir_mode *a_mode = get_irn_mode(a_op);
3192 ir_mode *b_mode = get_irn_mode(b_op);
3193 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3194 ir_node *blk = get_irn_n(n, -1);
3197 set_binop_left(n, a_op);
3198 set_binop_right(n, b_op);
3199 set_irn_mode(n, a_mode);
3201 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3203 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3209 /* nothing to gain here */
3213 if (op == op_Shrs || op == op_Shr || op == op_Shl
3214 || op == op_And || op == op_Or || op == op_Eor) {
3215 ir_node *a_left = get_binop_left(a);
3216 ir_node *a_right = get_binop_right(a);
3217 ir_node *b_left = get_binop_left(b);
3218 ir_node *b_right = get_binop_right(b);
3220 ir_node *op1 = NULL;
3221 ir_node *op2 = NULL;
3223 if (is_op_commutative(op)) {
3224 if (a_left == b_left) {
3228 } else if(a_left == b_right) {
3232 } else if(a_right == b_left) {
3238 if(a_right == b_right) {
3245 /* (a sop c) & (b sop c) => (a & b) sop c */
3246 ir_node *blk = get_irn_n(n, -1);
3248 ir_node *new_n = exact_copy(n);
3249 set_binop_left(new_n, op1);
3250 set_binop_right(new_n, op2);
3251 new_n = trans_func(new_n);
3253 if(op_root == op_Eor && op == op_Or) {
3254 dbg_info *dbgi = get_irn_dbg_info(n);
3255 ir_graph *irg = current_ir_graph;
3256 ir_mode *mode = get_irn_mode(c);
3258 c = new_rd_Not(dbgi, irg, blk, c, mode);
3259 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3262 set_nodes_block(n, blk);
3263 set_binop_left(n, new_n);
3264 set_binop_right(n, c);
3265 add_identities(current_ir_graph->value_table, n);
3268 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3279 static ir_node *transform_node_And(ir_node *n) {
3280 ir_node *c, *oldn = n;
3281 ir_node *a = get_And_left(n);
3282 ir_node *b = get_And_right(n);
3285 mode = get_irn_mode(n);
3286 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3288 /* we can evaluate 2 Projs of the same Cmp */
3289 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3290 ir_node *pred_a = get_Proj_pred(a);
3291 ir_node *pred_b = get_Proj_pred(b);
3292 if (pred_a == pred_b) {
3293 dbg_info *dbgi = get_irn_dbg_info(n);
3294 ir_node *block = get_nodes_block(pred_a);
3295 pn_Cmp pn_a = get_Proj_proj(a);
3296 pn_Cmp pn_b = get_Proj_proj(b);
3297 /* yes, we can simply calculate with pncs */
3298 pn_Cmp new_pnc = pn_a & pn_b;
3300 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3305 ir_node *op = get_Not_op(b);
3307 ir_node *ba = get_And_left(op);
3308 ir_node *bb = get_And_right(op);
3310 /* it's enough to test the following cases due to normalization! */
3311 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3312 /* (a|b) & ~(a&b) = a^b */
3313 ir_node *block = get_nodes_block(n);
3315 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3316 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3324 ir_node *op = get_Not_op(a);
3326 ir_node *aa = get_And_left(op);
3327 ir_node *ab = get_And_right(op);
3329 /* it's enough to test the following cases due to normalization! */
3330 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3331 /* (a|b) & ~(a&b) = a^b */
3332 ir_node *block = get_nodes_block(n);
3334 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3335 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3342 ir_node *al = get_Eor_left(a);
3343 ir_node *ar = get_Eor_right(a);
3346 /* (b ^ a) & b -> ~a & b */
3347 dbg_info *dbg = get_irn_dbg_info(n);
3348 ir_node *block = get_nodes_block(n);
3350 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3351 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3352 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3356 /* (a ^ b) & b -> ~a & b */
3357 dbg_info *dbg = get_irn_dbg_info(n);
3358 ir_node *block = get_nodes_block(n);
3360 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3361 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3362 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3367 ir_node *bl = get_Eor_left(b);
3368 ir_node *br = get_Eor_right(b);
3371 /* a & (a ^ b) -> a & ~b */
3372 dbg_info *dbg = get_irn_dbg_info(n);
3373 ir_node *block = get_nodes_block(n);
3375 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3376 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3377 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3381 /* a & (b ^ a) -> a & ~b */
3382 dbg_info *dbg = get_irn_dbg_info(n);
3383 ir_node *block = get_nodes_block(n);
3385 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3386 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3387 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3391 if (is_Not(a) && is_Not(b)) {
3392 /* ~a & ~b = ~(a|b) */
3393 ir_node *block = get_nodes_block(n);
3394 ir_mode *mode = get_irn_mode(n);
3398 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3399 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3400 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3404 n = transform_bitwise_distributive(n, transform_node_And);
3407 } /* transform_node_And */
3412 static ir_node *transform_node_Eor(ir_node *n) {
3413 ir_node *c, *oldn = n;
3414 ir_node *a = get_Eor_left(n);
3415 ir_node *b = get_Eor_right(n);
3416 ir_mode *mode = get_irn_mode(n);
3418 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3420 /* we can evaluate 2 Projs of the same Cmp */
3421 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3422 ir_node *pred_a = get_Proj_pred(a);
3423 ir_node *pred_b = get_Proj_pred(b);
3424 if(pred_a == pred_b) {
3425 dbg_info *dbgi = get_irn_dbg_info(n);
3426 ir_node *block = get_nodes_block(pred_a);
3427 pn_Cmp pn_a = get_Proj_proj(a);
3428 pn_Cmp pn_b = get_Proj_proj(b);
3429 /* yes, we can simply calculate with pncs */
3430 pn_Cmp new_pnc = pn_a ^ pn_b;
3432 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3439 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3440 mode, get_mode_null(mode));
3441 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3442 } else if (mode == mode_b &&
3444 is_Const(b) && is_Const_one(b) &&
3445 is_Cmp(get_Proj_pred(a))) {
3446 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3447 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3448 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3450 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3451 } else if (is_Const(b)) {
3452 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3453 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3454 ir_node *not_op = get_Not_op(a);
3455 dbg_info *dbg = get_irn_dbg_info(n);
3456 ir_graph *irg = current_ir_graph;
3457 ir_node *block = get_nodes_block(n);
3458 ir_mode *mode = get_irn_mode(n);
3459 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3461 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3462 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3463 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3466 n = transform_bitwise_distributive(n, transform_node_Eor);
3470 } /* transform_node_Eor */
3475 static ir_node *transform_node_Not(ir_node *n) {
3476 ir_node *c, *oldn = n;
3477 ir_node *a = get_Not_op(n);
3478 ir_mode *mode = get_irn_mode(n);
3480 HANDLE_UNOP_PHI(tarval_not,a,c);
3482 /* check for a boolean Not */
3483 if (mode == mode_b &&
3485 is_Cmp(get_Proj_pred(a))) {
3486 /* We negate a Cmp. The Cmp has the negated result anyways! */
3487 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3488 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3489 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3493 ir_node *eor_b = get_Eor_right(a);
3494 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3495 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3496 ir_node *eor_a = get_Eor_left(a);
3497 dbg_info *dbg = get_irn_dbg_info(n);
3498 ir_graph *irg = current_ir_graph;
3499 ir_node *block = get_nodes_block(n);
3500 ir_mode *mode = get_irn_mode(n);
3501 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3505 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3506 if (is_Minus(a)) { /* ~-x -> x + -1 */
3507 dbg_info *dbg = get_irn_dbg_info(n);
3508 ir_graph *irg = current_ir_graph;
3509 ir_node *block = get_nodes_block(n);
3510 ir_node *add_l = get_Minus_op(a);
3511 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3512 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3513 } else if (is_Add(a)) {
3514 ir_node *add_r = get_Add_right(a);
3515 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3516 /* ~(x + -1) = -x */
3517 ir_node *op = get_Add_left(a);
3518 ir_node *blk = get_irn_n(n, -1);
3519 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3525 } /* transform_node_Not */
3528 * Transform a Minus.
3532 * -(a >>u (size-1)) = a >>s (size-1)
3533 * -(a >>s (size-1)) = a >>u (size-1)
3534 * -(a * const) -> a * -const
3536 static ir_node *transform_node_Minus(ir_node *n) {
3537 ir_node *c, *oldn = n;
3538 ir_node *a = get_Minus_op(n);
3541 HANDLE_UNOP_PHI(tarval_neg,a,c);
3543 mode = get_irn_mode(a);
3544 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3545 /* the following rules are only to twos-complement */
3548 ir_node *op = get_Not_op(a);
3549 tarval *tv = get_mode_one(mode);
3550 ir_node *blk = get_irn_n(n, -1);
3551 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3552 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3553 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3557 ir_node *c = get_Shr_right(a);
3560 tarval *tv = get_Const_tarval(c);
3562 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3563 /* -(a >>u (size-1)) = a >>s (size-1) */
3564 ir_node *v = get_Shr_left(a);
3566 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3567 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3573 ir_node *c = get_Shrs_right(a);
3576 tarval *tv = get_Const_tarval(c);
3578 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3579 /* -(a >>s (size-1)) = a >>u (size-1) */
3580 ir_node *v = get_Shrs_left(a);
3582 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3583 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3590 /* - (a-b) = b - a */
3591 ir_node *la = get_Sub_left(a);
3592 ir_node *ra = get_Sub_right(a);
3593 ir_node *blk = get_irn_n(n, -1);
3595 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3596 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3600 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3601 ir_node *mul_l = get_Mul_left(a);
3602 ir_node *mul_r = get_Mul_right(a);
3603 if (is_Const(mul_r)) {
3604 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3605 if(tv != tarval_bad) {
3606 ir_node *cnst = new_Const(mode, tv);
3607 dbg_info *dbg = get_irn_dbg_info(a);
3608 ir_graph *irg = current_ir_graph;
3609 ir_node *block = get_nodes_block(a);
3610 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3611 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3618 } /* transform_node_Minus */
3621 * Transform a Cast_type(Const) into a new Const_type
3623 static ir_node *transform_node_Cast(ir_node *n) {
3625 ir_node *pred = get_Cast_op(n);
3626 ir_type *tp = get_irn_type(n);
3628 if (is_Const(pred) && get_Const_type(pred) != tp) {
3629 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3630 get_Const_tarval(pred), tp);
3631 DBG_OPT_CSTEVAL(oldn, n);
3632 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3633 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3634 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3635 DBG_OPT_CSTEVAL(oldn, n);
3639 } /* transform_node_Cast */
3642 * Transform a Proj(Load) with a non-null address.
3644 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3645 if (get_opt_ldst_only_null_ptr_exceptions()) {
3646 if (get_irn_mode(proj) == mode_X) {
3647 ir_node *oldn = proj;
3648 ir_node *load = get_Proj_pred(proj);
3650 /* get the Load address */
3651 ir_node *addr = get_Load_ptr(load);
3652 ir_node *blk = get_nodes_block(load);
3655 if (value_not_null(addr, &confirm)) {
3656 if (confirm == NULL) {
3657 /* this node may float if it did not depend on a Confirm */
3658 set_irn_pinned(load, op_pin_state_floats);
3660 if (get_Proj_proj(proj) == pn_Load_X_except) {
3661 DBG_OPT_EXC_REM(proj);
3662 return get_irg_bad(current_ir_graph);
3664 return new_r_Jmp(current_ir_graph, blk);
3670 } /* transform_node_Proj_Load */
3673 * Transform a Proj(Store) with a non-null address.
3675 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3676 if (get_opt_ldst_only_null_ptr_exceptions()) {
3677 if (get_irn_mode(proj) == mode_X) {
3678 ir_node *oldn = proj;
3679 ir_node *store = get_Proj_pred(proj);
3681 /* get the load/store address */
3682 ir_node *addr = get_Store_ptr(store);
3683 ir_node *blk = get_nodes_block(store);
3686 if (value_not_null(addr, &confirm)) {
3687 if (confirm == NULL) {
3688 /* this node may float if it did not depend on a Confirm */
3689 set_irn_pinned(store, op_pin_state_floats);
3691 if (get_Proj_proj(proj) == pn_Store_X_except) {
3692 DBG_OPT_EXC_REM(proj);
3693 return get_irg_bad(current_ir_graph);
3695 return new_r_Jmp(current_ir_graph, blk);
3700 } /* transform_node_Proj_Store */
3703 * Transform a Proj(Div) with a non-zero value.
3704 * Removes the exceptions and routes the memory to the NoMem node.
3706 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3707 ir_node *div = get_Proj_pred(proj);
3708 ir_node *b = get_Div_right(div);
3709 ir_node *confirm, *res, *new_mem;
3712 if (value_not_zero(b, &confirm)) {
3713 /* div(x, y) && y != 0 */
3714 if (confirm == NULL) {
3715 /* we are sure we have a Const != 0 */
3716 new_mem = get_Div_mem(div);
3717 if (is_Pin(new_mem))
3718 new_mem = get_Pin_op(new_mem);
3719 set_Div_mem(div, new_mem);
3720 set_irn_pinned(div, op_pin_state_floats);
3723 proj_nr = get_Proj_proj(proj);
3725 case pn_Div_X_regular:
3726 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3728 case pn_Div_X_except:
3729 /* we found an exception handler, remove it */
3730 DBG_OPT_EXC_REM(proj);
3734 res = get_Div_mem(div);
3735 new_mem = get_irg_no_mem(current_ir_graph);
3738 /* This node can only float up to the Confirm block */
3739 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3741 set_irn_pinned(div, op_pin_state_floats);
3742 /* this is a Div without exception, we can remove the memory edge */
3743 set_Div_mem(div, new_mem);
3748 } /* transform_node_Proj_Div */
3751 * Transform a Proj(Mod) with a non-zero value.
3752 * Removes the exceptions and routes the memory to the NoMem node.
3754 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3755 ir_node *mod = get_Proj_pred(proj);
3756 ir_node *b = get_Mod_right(mod);
3757 ir_node *confirm, *res, *new_mem;
3760 if (value_not_zero(b, &confirm)) {
3761 /* mod(x, y) && y != 0 */
3762 proj_nr = get_Proj_proj(proj);
3764 if (confirm == NULL) {
3765 /* we are sure we have a Const != 0 */
3766 new_mem = get_Mod_mem(mod);
3767 if (is_Pin(new_mem))
3768 new_mem = get_Pin_op(new_mem);
3769 set_Mod_mem(mod, new_mem);
3770 set_irn_pinned(mod, op_pin_state_floats);
3775 case pn_Mod_X_regular:
3776 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3778 case pn_Mod_X_except:
3779 /* we found an exception handler, remove it */
3780 DBG_OPT_EXC_REM(proj);
3784 res = get_Mod_mem(mod);
3785 new_mem = get_irg_no_mem(current_ir_graph);
3788 /* This node can only float up to the Confirm block */
3789 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3791 /* this is a Mod without exception, we can remove the memory edge */
3792 set_Mod_mem(mod, new_mem);
3795 if (get_Mod_left(mod) == b) {
3796 /* a % a = 0 if a != 0 */
3797 ir_mode *mode = get_irn_mode(proj);
3798 ir_node *res = new_Const(mode, get_mode_null(mode));
3800 DBG_OPT_CSTEVAL(mod, res);
3806 } /* transform_node_Proj_Mod */
3809 * Transform a Proj(DivMod) with a non-zero value.
3810 * Removes the exceptions and routes the memory to the NoMem node.
3812 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3813 ir_node *divmod = get_Proj_pred(proj);
3814 ir_node *b = get_DivMod_right(divmod);
3815 ir_node *confirm, *res, *new_mem;
3818 if (value_not_zero(b, &confirm)) {
3819 /* DivMod(x, y) && y != 0 */
3820 proj_nr = get_Proj_proj(proj);
3822 if (confirm == NULL) {
3823 /* we are sure we have a Const != 0 */
3824 new_mem = get_DivMod_mem(divmod);
3825 if (is_Pin(new_mem))
3826 new_mem = get_Pin_op(new_mem);
3827 set_DivMod_mem(divmod, new_mem);
3828 set_irn_pinned(divmod, op_pin_state_floats);
3833 case pn_DivMod_X_regular:
3834 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3836 case pn_DivMod_X_except:
3837 /* we found an exception handler, remove it */
3838 DBG_OPT_EXC_REM(proj);
3842 res = get_DivMod_mem(divmod);
3843 new_mem = get_irg_no_mem(current_ir_graph);
3846 /* This node can only float up to the Confirm block */
3847 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3849 /* this is a DivMod without exception, we can remove the memory edge */
3850 set_DivMod_mem(divmod, new_mem);
3853 case pn_DivMod_res_mod:
3854 if (get_DivMod_left(divmod) == b) {
3855 /* a % a = 0 if a != 0 */
3856 ir_mode *mode = get_irn_mode(proj);
3857 ir_node *res = new_Const(mode, get_mode_null(mode));
3859 DBG_OPT_CSTEVAL(divmod, res);
3865 } /* transform_node_Proj_DivMod */
3868 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3870 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3871 if (get_opt_unreachable_code()) {
3872 ir_node *n = get_Proj_pred(proj);
3873 ir_node *b = get_Cond_selector(n);
3875 if (mode_is_int(get_irn_mode(b))) {
3876 tarval *tb = value_of(b);
3878 if (tb != tarval_bad) {
3879 /* we have a constant switch */
3880 long num = get_Proj_proj(proj);
3882 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3883 if (get_tarval_long(tb) == num) {
3884 /* Do NOT create a jump here, or we will have 2 control flow ops
3885 * in a block. This case is optimized away in optimize_cf(). */
3888 /* this case will NEVER be taken, kill it */
3896 } /* transform_node_Proj_Cond */
3899 * Create a 0 constant of given mode.
3901 static ir_node *create_zero_const(ir_mode *mode) {
3902 tarval *tv = get_mode_null(mode);
3903 ir_node *cnst = new_Const(mode, tv);
3908 /* the order of the values is important! */
3909 typedef enum const_class {
3915 static const_class classify_const(const ir_node* n)
3917 if (is_Const(n)) return const_const;
3918 if (is_irn_constlike(n)) return const_like;
3923 * Determines whether r is more constlike or has a larger index (in that order)
3926 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3928 const const_class l_order = classify_const(l);
3929 const const_class r_order = classify_const(r);
3931 l_order > r_order ||
3932 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3936 * Normalizes and optimizes Cmp nodes.
3938 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3939 ir_node *n = get_Proj_pred(proj);
3940 ir_node *left = get_Cmp_left(n);
3941 ir_node *right = get_Cmp_right(n);
3944 ir_mode *mode = NULL;
3945 long proj_nr = get_Proj_proj(proj);
3947 /* we can evaluate some cases directly */
3950 return new_Const(mode_b, get_tarval_b_false());
3952 return new_Const(mode_b, get_tarval_b_true());
3954 if (!mode_is_float(get_irn_mode(left)))
3955 return new_Const(mode_b, get_tarval_b_true());
3961 /* remove Casts of both sides */
3962 left = skip_Cast(left);
3963 right = skip_Cast(right);
3965 /* Remove unnecessary conversions */
3966 /* TODO handle constants */
3967 if (is_Conv(left) && is_Conv(right)) {
3968 ir_mode *mode = get_irn_mode(left);
3969 ir_node *op_left = get_Conv_op(left);
3970 ir_node *op_right = get_Conv_op(right);
3971 ir_mode *mode_left = get_irn_mode(op_left);
3972 ir_mode *mode_right = get_irn_mode(op_right);
3974 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3975 && mode_left != mode_b && mode_right != mode_b) {
3976 ir_graph *irg = current_ir_graph;
3977 ir_node *block = get_nodes_block(n);
3979 if (mode_left == mode_right) {
3983 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3984 } else if (smaller_mode(mode_left, mode_right)) {
3985 left = new_r_Conv(irg, block, op_left, mode_right);
3988 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3989 } else if (smaller_mode(mode_right, mode_left)) {
3991 right = new_r_Conv(irg, block, op_right, mode_left);
3993 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3998 /* remove operation on both sides if possible */
3999 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4001 * The following operations are NOT safe for floating point operations, for instance
4002 * 1.0 + inf == 2.0 + inf, =/=> x == y
4004 if (mode_is_int(get_irn_mode(left))) {
4005 unsigned lop = get_irn_opcode(left);
4007 if (lop == get_irn_opcode(right)) {
4008 ir_node *ll, *lr, *rl, *rr;
4010 /* same operation on both sides, try to remove */
4014 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4015 left = get_unop_op(left);
4016 right = get_unop_op(right);
4018 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4021 ll = get_Add_left(left);
4022 lr = get_Add_right(left);
4023 rl = get_Add_left(right);
4024 rr = get_Add_right(right);
4027 /* X + a CMP X + b ==> a CMP b */
4031 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4032 } else if (ll == rr) {
4033 /* X + a CMP b + X ==> a CMP b */
4037 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4038 } else if (lr == rl) {
4039 /* a + X CMP X + b ==> a CMP b */
4043 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4044 } else if (lr == rr) {
4045 /* a + X CMP b + X ==> a CMP b */
4049 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4053 ll = get_Sub_left(left);
4054 lr = get_Sub_right(left);
4055 rl = get_Sub_left(right);
4056 rr = get_Sub_right(right);
4059 /* X - a CMP X - b ==> a CMP b */
4063 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4064 } else if (lr == rr) {
4065 /* a - X CMP b - X ==> a CMP b */
4069 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4073 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4074 /* a ROTL X CMP b ROTL X ==> a CMP b */
4075 left = get_Rotl_left(left);
4076 right = get_Rotl_left(right);
4078 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4086 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4087 if (is_Add(left) || is_Sub(left)) {
4088 ir_node *ll = get_binop_left(left);
4089 ir_node *lr = get_binop_right(left);
4091 if (lr == right && is_Add(left)) {
4098 right = create_zero_const(get_irn_mode(left));
4100 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4103 if (is_Add(right) || is_Sub(right)) {
4104 ir_node *rl = get_binop_left(right);
4105 ir_node *rr = get_binop_right(right);
4107 if (rr == left && is_Add(right)) {
4114 right = create_zero_const(get_irn_mode(left));
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4119 } /* mode_is_int(...) */
4120 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4122 /* replace mode_b compares with ands/ors */
4123 if (get_irn_mode(left) == mode_b) {
4124 ir_graph *irg = current_ir_graph;
4125 ir_node *block = get_nodes_block(n);
4129 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4130 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4131 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4132 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4133 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4134 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4135 default: bres = NULL;
4138 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4144 * First step: normalize the compare op
4145 * by placing the constant on the right side
4146 * or moving the lower address node to the left.
4148 if (!operands_are_normalized(left, right)) {
4154 proj_nr = get_inversed_pnc(proj_nr);
4159 * Second step: Try to reduce the magnitude
4160 * of a constant. This may help to generate better code
4161 * later and may help to normalize more compares.
4162 * Of course this is only possible for integer values.
4164 if (is_Const(right)) {
4165 mode = get_irn_mode(right);
4166 tv = get_Const_tarval(right);
4168 /* TODO extend to arbitrary constants */
4169 if (is_Conv(left) && tarval_is_null(tv)) {
4170 ir_node *op = get_Conv_op(left);
4171 ir_mode *op_mode = get_irn_mode(op);
4174 * UpConv(x) REL 0 ==> x REL 0
4176 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4177 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4178 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4179 tv = get_mode_null(op_mode);
4183 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4187 if (tv != tarval_bad) {
4188 /* the following optimization is possible on modes without Overflow
4189 * on Unary Minus or on == and !=:
4190 * -a CMP c ==> a swap(CMP) -c
4192 * Beware: for two-complement Overflow may occur, so only == and != can
4193 * be optimized, see this:
4194 * -MININT < 0 =/=> MININT > 0 !!!
4196 if (is_Minus(left) &&
4197 (!mode_overflow_on_unary_Minus(mode) ||
4198 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4199 tv = tarval_neg(tv);
4201 if (tv != tarval_bad) {
4202 left = get_Minus_op(left);
4203 proj_nr = get_inversed_pnc(proj_nr);
4205 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4207 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4208 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4209 tv = tarval_not(tv);
4211 if (tv != tarval_bad) {
4212 left = get_Not_op(left);
4214 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4218 /* for integer modes, we have more */
4219 if (mode_is_int(mode)) {
4220 /* Ne includes Unordered which is not possible on integers.
4221 * However, frontends often use this wrong, so fix it here */
4222 if (proj_nr & pn_Cmp_Uo) {
4223 proj_nr &= ~pn_Cmp_Uo;
4224 set_Proj_proj(proj, proj_nr);
4227 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4228 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4229 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4230 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4232 if (tv != tarval_bad) {
4233 proj_nr ^= pn_Cmp_Eq;
4235 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4238 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4239 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4240 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4241 tv = tarval_add(tv, get_mode_one(mode));
4243 if (tv != tarval_bad) {
4244 proj_nr ^= pn_Cmp_Eq;
4246 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4250 /* the following reassociations work only for == and != */
4251 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4253 #if 0 /* Might be not that good in general */
4254 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4255 if (tarval_is_null(tv) && is_Sub(left)) {
4256 right = get_Sub_right(left);
4257 left = get_Sub_left(left);
4259 tv = value_of(right);
4261 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4265 if (tv != tarval_bad) {
4266 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4268 ir_node *c1 = get_Sub_right(left);
4269 tarval *tv2 = value_of(c1);
4271 if (tv2 != tarval_bad) {
4272 tv2 = tarval_add(tv, value_of(c1));
4274 if (tv2 != tarval_bad) {
4275 left = get_Sub_left(left);
4278 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4282 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4283 else if (is_Add(left)) {
4284 ir_node *a_l = get_Add_left(left);
4285 ir_node *a_r = get_Add_right(left);
4289 if (is_Const(a_l)) {
4291 tv2 = value_of(a_l);
4294 tv2 = value_of(a_r);
4297 if (tv2 != tarval_bad) {
4298 tv2 = tarval_sub(tv, tv2, NULL);
4300 if (tv2 != tarval_bad) {
4304 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4308 /* -a == c ==> a == -c, -a != c ==> a != -c */
4309 else if (is_Minus(left)) {
4310 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4312 if (tv2 != tarval_bad) {
4313 left = get_Minus_op(left);
4316 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4321 /* the following reassociations work only for <= */
4322 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4323 if (tv != tarval_bad) {
4324 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4325 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4331 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4332 switch (get_irn_opcode(left)) {
4336 c1 = get_And_right(left);
4339 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4340 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4342 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4344 /* TODO: move to constant evaluation */
4345 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4346 c1 = new_Const(mode_b, tv);
4347 DBG_OPT_CSTEVAL(proj, c1);
4351 if (tarval_is_single_bit(tv)) {
4353 * optimization for AND:
4355 * And(x, C) == C ==> And(x, C) != 0
4356 * And(x, C) != C ==> And(X, C) == 0
4358 * if C is a single Bit constant.
4361 /* check for Constant's match. We have check hare the tarvals,
4362 because our const might be changed */
4363 if (get_Const_tarval(c1) == tv) {
4364 /* fine: do the transformation */
4365 tv = get_mode_null(get_tarval_mode(tv));
4366 proj_nr ^= pn_Cmp_Leg;
4368 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4374 c1 = get_Or_right(left);
4375 if (is_Const(c1) && tarval_is_null(tv)) {
4377 * Or(x, C) == 0 && C != 0 ==> FALSE
4378 * Or(x, C) != 0 && C != 0 ==> TRUE
4380 if (! tarval_is_null(get_Const_tarval(c1))) {
4381 /* TODO: move to constant evaluation */
4382 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4383 c1 = new_Const(mode_b, tv);
4384 DBG_OPT_CSTEVAL(proj, c1);
4391 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4393 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4396 c1 = get_Shl_right(left);
4398 tarval *tv1 = get_Const_tarval(c1);
4399 ir_mode *mode = get_irn_mode(left);
4400 tarval *minus1 = get_mode_all_one(mode);
4401 tarval *amask = tarval_shr(minus1, tv1);
4402 tarval *cmask = tarval_shl(minus1, tv1);
4405 if (tarval_and(tv, cmask) != tv) {
4406 /* condition not met */
4407 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4408 c1 = new_Const(mode_b, tv);
4409 DBG_OPT_CSTEVAL(proj, c1);
4412 sl = get_Shl_left(left);
4413 blk = get_nodes_block(n);
4414 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4415 tv = tarval_shr(tv, tv1);
4417 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4422 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4424 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4427 c1 = get_Shr_right(left);
4429 tarval *tv1 = get_Const_tarval(c1);
4430 ir_mode *mode = get_irn_mode(left);
4431 tarval *minus1 = get_mode_all_one(mode);
4432 tarval *amask = tarval_shl(minus1, tv1);
4433 tarval *cmask = tarval_shr(minus1, tv1);
4436 if (tarval_and(tv, cmask) != tv) {
4437 /* condition not met */
4438 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4439 c1 = new_Const(mode_b, tv);
4440 DBG_OPT_CSTEVAL(proj, c1);
4443 sl = get_Shr_left(left);
4444 blk = get_nodes_block(n);
4445 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4446 tv = tarval_shl(tv, tv1);
4448 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4453 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4455 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4458 c1 = get_Shrs_right(left);
4460 tarval *tv1 = get_Const_tarval(c1);
4461 ir_mode *mode = get_irn_mode(left);
4462 tarval *minus1 = get_mode_all_one(mode);
4463 tarval *amask = tarval_shl(minus1, tv1);
4464 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4467 cond = tarval_sub(cond, tv1, NULL);
4468 cond = tarval_shrs(tv, cond);
4470 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4471 /* condition not met */
4472 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4473 c1 = new_Const(mode_b, tv);
4474 DBG_OPT_CSTEVAL(proj, c1);
4477 sl = get_Shrs_left(left);
4478 blk = get_nodes_block(n);
4479 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4480 tv = tarval_shl(tv, tv1);
4482 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4487 } /* tarval != bad */
4490 if (changed & 2) /* need a new Const */
4491 right = new_Const(mode, tv);
4493 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4494 ir_node *op = get_Proj_pred(left);
4496 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4497 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4498 ir_node *c = get_binop_right(op);
4501 tarval *tv = get_Const_tarval(c);
4503 if (tarval_is_single_bit(tv)) {
4504 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4505 ir_node *v = get_binop_left(op);
4506 ir_node *blk = get_irn_n(op, -1);
4507 ir_mode *mode = get_irn_mode(v);
4509 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4510 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4512 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4519 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4521 /* create a new compare */
4522 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4523 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4527 } /* transform_node_Proj_Cmp */
4530 * Optimize CopyB(mem, x, x) into a Nop.
4532 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4533 ir_node *oldn = proj;
4534 ir_node *copyb = get_Proj_pred(proj);
4535 ir_node *a = get_CopyB_dst(copyb);
4536 ir_node *b = get_CopyB_src(copyb);
4539 switch (get_Proj_proj(proj)) {
4540 case pn_CopyB_X_regular:
4541 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4542 DBG_OPT_EXC_REM(proj);
4543 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4545 case pn_CopyB_M_except:
4546 case pn_CopyB_X_except:
4547 DBG_OPT_EXC_REM(proj);
4548 proj = get_irg_bad(current_ir_graph);
4555 } /* transform_node_Proj_CopyB */
4558 * Optimize Bounds(idx, idx, upper) into idx.
4560 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4561 ir_node *oldn = proj;
4562 ir_node *bound = get_Proj_pred(proj);
4563 ir_node *idx = get_Bound_index(bound);
4564 ir_node *pred = skip_Proj(idx);
4567 if (idx == get_Bound_lower(bound))
4569 else if (is_Bound(pred)) {
4571 * idx was Bounds checked in the same MacroBlock previously,
4572 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4574 ir_node *lower = get_Bound_lower(bound);
4575 ir_node *upper = get_Bound_upper(bound);
4576 if (get_Bound_lower(pred) == lower &&
4577 get_Bound_upper(pred) == upper &&
4578 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4580 * One could expect that we simply return the previous
4581 * Bound here. However, this would be wrong, as we could
4582 * add an exception Proj to a new location then.
4583 * So, we must turn in into a tuple.
4589 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4590 switch (get_Proj_proj(proj)) {
4592 DBG_OPT_EXC_REM(proj);
4593 proj = get_Bound_mem(bound);
4595 case pn_Bound_X_except:
4596 DBG_OPT_EXC_REM(proj);
4597 proj = get_irg_bad(current_ir_graph);
4601 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4603 case pn_Bound_X_regular:
4604 DBG_OPT_EXC_REM(proj);
4605 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4612 } /* transform_node_Proj_Bound */
4615 * Does all optimizations on nodes that must be done on it's Proj's
4616 * because of creating new nodes.
4618 static ir_node *transform_node_Proj(ir_node *proj) {
4619 ir_node *n = get_Proj_pred(proj);
4621 switch (get_irn_opcode(n)) {
4623 return transform_node_Proj_Load(proj);
4626 return transform_node_Proj_Store(proj);
4629 return transform_node_Proj_Div(proj);
4632 return transform_node_Proj_Mod(proj);
4635 return transform_node_Proj_DivMod(proj);
4638 return transform_node_Proj_Cond(proj);
4641 return transform_node_Proj_Cmp(proj);
4644 /* should not happen, but if it does will be optimized away */
4645 return equivalent_node_Proj(proj);
4648 return transform_node_Proj_CopyB(proj);
4651 return transform_node_Proj_CopyB(proj);
4657 } /* transform_node_Proj */
4660 * Move Confirms down through Phi nodes.
4662 static ir_node *transform_node_Phi(ir_node *phi) {
4664 ir_mode *mode = get_irn_mode(phi);
4666 if (mode_is_reference(mode)) {
4667 n = get_irn_arity(phi);
4669 /* Beware of Phi0 */
4671 ir_node *pred = get_irn_n(phi, 0);
4672 ir_node *bound, *new_Phi, *block, **in;
4675 if (! is_Confirm(pred))
4678 bound = get_Confirm_bound(pred);
4679 pnc = get_Confirm_cmp(pred);
4681 NEW_ARR_A(ir_node *, in, n);
4682 in[0] = get_Confirm_value(pred);
4684 for (i = 1; i < n; ++i) {
4685 pred = get_irn_n(phi, i);
4687 if (! is_Confirm(pred) ||
4688 get_Confirm_bound(pred) != bound ||
4689 get_Confirm_cmp(pred) != pnc)
4691 in[i] = get_Confirm_value(pred);
4693 /* move the Confirm nodes "behind" the Phi */
4694 block = get_irn_n(phi, -1);
4695 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4696 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4700 } /* transform_node_Phi */
4703 * Returns the operands of a commutative bin-op, if one operand is
4704 * a const, it is returned as the second one.
4706 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4707 ir_node *op_a = get_binop_left(binop);
4708 ir_node *op_b = get_binop_right(binop);
4710 assert(is_op_commutative(get_irn_op(binop)));
4712 if (is_Const(op_a)) {
4719 } /* get_comm_Binop_Ops */
4722 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4723 * Such pattern may arise in bitfield stores.
4725 * value c4 value c4 & c2
4726 * AND c3 AND c1 | c3
4733 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4736 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4739 ir_node *and_l, *c3;
4740 ir_node *value, *c4;
4741 ir_node *new_and, *new_const, *block;
4742 ir_mode *mode = get_irn_mode(or);
4744 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4747 get_comm_Binop_Ops(or, &and, &c1);
4748 if (!is_Const(c1) || !is_And(and))
4751 get_comm_Binop_Ops(and, &or_l, &c2);
4755 tv1 = get_Const_tarval(c1);
4756 tv2 = get_Const_tarval(c2);
4758 tv = tarval_or(tv1, tv2);
4759 if (tarval_is_all_one(tv)) {
4760 /* the AND does NOT clear a bit with isn't set by the OR */
4761 set_Or_left(or, or_l);
4762 set_Or_right(or, c1);
4764 /* check for more */
4771 get_comm_Binop_Ops(or_l, &and_l, &c3);
4772 if (!is_Const(c3) || !is_And(and_l))
4775 get_comm_Binop_Ops(and_l, &value, &c4);
4779 /* ok, found the pattern, check for conditions */
4780 assert(mode == get_irn_mode(and));
4781 assert(mode == get_irn_mode(or_l));
4782 assert(mode == get_irn_mode(and_l));
4784 tv3 = get_Const_tarval(c3);
4785 tv4 = get_Const_tarval(c4);
4787 tv = tarval_or(tv4, tv2);
4788 if (!tarval_is_all_one(tv)) {
4789 /* have at least one 0 at the same bit position */
4793 n_tv4 = tarval_not(tv4);
4794 if (tv3 != tarval_and(tv3, n_tv4)) {
4795 /* bit in the or_mask is outside the and_mask */
4799 n_tv2 = tarval_not(tv2);
4800 if (tv1 != tarval_and(tv1, n_tv2)) {
4801 /* bit in the or_mask is outside the and_mask */
4805 /* ok, all conditions met */
4806 block = get_irn_n(or, -1);
4808 new_and = new_r_And(current_ir_graph, block,
4809 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4811 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4813 set_Or_left(or, new_and);
4814 set_Or_right(or, new_const);
4816 /* check for more */
4818 } /* transform_node_Or_bf_store */
4821 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4823 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4824 ir_mode *mode = get_irn_mode(or);
4825 ir_node *shl, *shr, *block;
4826 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4829 if (! mode_is_int(mode))
4832 shl = get_binop_left(or);
4833 shr = get_binop_right(or);
4842 } else if (!is_Shl(shl)) {
4844 } else if (!is_Shr(shr)) {
4847 x = get_Shl_left(shl);
4848 if (x != get_Shr_left(shr))
4851 c1 = get_Shl_right(shl);
4852 c2 = get_Shr_right(shr);
4853 if (is_Const(c1) && is_Const(c2)) {
4854 tv1 = get_Const_tarval(c1);
4855 if (! tarval_is_long(tv1))
4858 tv2 = get_Const_tarval(c2);
4859 if (! tarval_is_long(tv2))
4862 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4863 != (int) get_mode_size_bits(mode))
4866 /* yet, condition met */
4867 block = get_nodes_block(or);
4869 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4871 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4878 rotval = sub; /* a Rot right is not supported, so use a rot left */
4879 } else if (is_Sub(c2)) {
4885 if (get_Sub_right(sub) != v)
4888 c1 = get_Sub_left(sub);
4892 tv1 = get_Const_tarval(c1);
4893 if (! tarval_is_long(tv1))
4896 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4899 /* yet, condition met */
4900 block = get_nodes_block(or);
4902 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4904 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4906 } /* transform_node_Or_Rotl */
4911 static ir_node *transform_node_Or(ir_node *n) {
4912 ir_node *c, *oldn = n;
4913 ir_node *a = get_Or_left(n);
4914 ir_node *b = get_Or_right(n);
4917 if (is_Not(a) && is_Not(b)) {
4918 /* ~a | ~b = ~(a&b) */
4919 ir_node *block = get_nodes_block(n);
4921 mode = get_irn_mode(n);
4924 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4925 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4926 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4930 /* we can evaluate 2 Projs of the same Cmp */
4931 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4932 ir_node *pred_a = get_Proj_pred(a);
4933 ir_node *pred_b = get_Proj_pred(b);
4934 if (pred_a == pred_b) {
4935 dbg_info *dbgi = get_irn_dbg_info(n);
4936 ir_node *block = get_nodes_block(pred_a);
4937 pn_Cmp pn_a = get_Proj_proj(a);
4938 pn_Cmp pn_b = get_Proj_proj(b);
4939 /* yes, we can simply calculate with pncs */
4940 pn_Cmp new_pnc = pn_a | pn_b;
4942 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4947 mode = get_irn_mode(n);
4948 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4950 n = transform_node_Or_bf_store(n);
4951 n = transform_node_Or_Rotl(n);
4955 n = transform_bitwise_distributive(n, transform_node_Or);
4958 } /* transform_node_Or */
4962 static ir_node *transform_node(ir_node *n);
4965 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4967 * Should be moved to reassociation?
4969 static ir_node *transform_node_shift(ir_node *n) {
4970 ir_node *left, *right;
4972 tarval *tv1, *tv2, *res;
4973 ir_node *in[2], *irn, *block;
4975 left = get_binop_left(n);
4977 /* different operations */
4978 if (get_irn_op(left) != get_irn_op(n))
4981 right = get_binop_right(n);
4982 tv1 = value_of(right);
4983 if (tv1 == tarval_bad)
4986 tv2 = value_of(get_binop_right(left));
4987 if (tv2 == tarval_bad)
4990 res = tarval_add(tv1, tv2);
4991 mode = get_irn_mode(n);
4993 /* beware: a simple replacement works only, if res < modulo shift */
4995 int modulo_shf = get_mode_modulo_shift(mode);
4996 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4997 if (modulo_shf > 0) {
4998 tarval *modulo = new_tarval_from_long(modulo_shf,
4999 get_tarval_mode(res));
5001 /* shifting too much */
5002 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5004 ir_graph *irg = get_irn_irg(n);
5005 ir_node *block = get_nodes_block(n);
5006 dbg_info *dbgi = get_irn_dbg_info(n);
5007 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
5008 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5012 return new_Const(mode, get_mode_null(mode));
5016 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5019 /* ok, we can replace it */
5020 block = get_nodes_block(n);
5022 in[0] = get_binop_left(left);
5023 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
5025 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5027 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5029 return transform_node(irn);
5030 } /* transform_node_shift */
5033 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5035 * - and, or, xor instead of &
5036 * - Shl, Shr, Shrs, rotl instead of >>
5037 * (with a special case for Or/Xor + Shrs)
5039 static ir_node *transform_node_bitop_shift(ir_node *n) {
5041 ir_node *right = get_binop_right(n);
5042 ir_mode *mode = get_irn_mode(n);
5043 ir_node *bitop_left;
5044 ir_node *bitop_right;
5056 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5058 if (!is_Const(right))
5061 left = get_binop_left(n);
5062 op_left = get_irn_op(left);
5063 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5066 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5067 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5068 /* TODO: test if sign bit is affectes */
5072 bitop_right = get_binop_right(left);
5073 if (!is_Const(bitop_right))
5076 bitop_left = get_binop_left(left);
5078 irg = get_irn_irg(n);
5079 block = get_nodes_block(n);
5080 dbgi = get_irn_dbg_info(n);
5081 tv1 = get_Const_tarval(bitop_right);
5082 tv2 = get_Const_tarval(right);
5084 assert(get_tarval_mode(tv1) == mode);
5087 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5088 tv_shift = tarval_shl(tv1, tv2);
5089 } else if(is_Shr(n)) {
5090 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5091 tv_shift = tarval_shr(tv1, tv2);
5092 } else if(is_Shrs(n)) {
5093 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5094 tv_shift = tarval_shrs(tv1, tv2);
5097 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5098 tv_shift = tarval_rotl(tv1, tv2);
5101 assert(get_tarval_mode(tv_shift) == mode);
5102 new_const = new_Const(mode, tv_shift);
5104 if (op_left == op_And) {
5105 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5106 } else if(op_left == op_Or) {
5107 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5109 assert(op_left == op_Eor);
5110 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5118 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5120 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5121 * (also with x >>s c1 when c1>=c2)
5123 static ir_node *transform_node_shl_shr(ir_node *n) {
5125 ir_node *right = get_binop_right(n);
5141 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5143 if (!is_Const(right))
5146 left = get_binop_left(n);
5147 mode = get_irn_mode(n);
5148 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5149 ir_node *shr_right = get_binop_right(left);
5151 if (!is_Const(shr_right))
5154 x = get_binop_left(left);
5155 tv_shr = get_Const_tarval(shr_right);
5156 tv_shl = get_Const_tarval(right);
5158 if (is_Shrs(left)) {
5159 /* shrs variant only allowed if c1 >= c2 */
5160 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5163 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5166 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5168 tv_mask = tarval_shl(tv_mask, tv_shl);
5169 } else if(is_Shr(n) && is_Shl(left)) {
5170 ir_node *shl_right = get_Shl_right(left);
5172 if (!is_Const(shl_right))
5175 x = get_Shl_left(left);
5176 tv_shr = get_Const_tarval(right);
5177 tv_shl = get_Const_tarval(shl_right);
5179 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5180 tv_mask = tarval_shr(tv_mask, tv_shr);
5185 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5186 assert(tv_mask != tarval_bad);
5187 assert(get_tarval_mode(tv_mask) == mode);
5189 irg = get_irn_irg(n);
5190 block = get_nodes_block(n);
5191 dbgi = get_irn_dbg_info(n);
5193 pnc = tarval_cmp(tv_shl, tv_shr);
5194 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5195 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5196 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5198 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5200 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5203 assert(pnc == pn_Cmp_Gt);
5204 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5205 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5206 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5209 new_const = new_Const(mode, tv_mask);
5210 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5218 static ir_node *transform_node_Shr(ir_node *n) {
5219 ir_node *c, *oldn = n;
5220 ir_node *left = get_Shr_left(n);
5221 ir_node *right = get_Shr_right(n);
5222 ir_mode *mode = get_irn_mode(n);
5224 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5225 n = transform_node_shift(n);
5228 n = transform_node_shl_shr(n);
5230 n = transform_node_bitop_shift(n);
5233 } /* transform_node_Shr */
5238 static ir_node *transform_node_Shrs(ir_node *n) {
5239 ir_node *c, *oldn = n;
5240 ir_node *a = get_Shrs_left(n);
5241 ir_node *b = get_Shrs_right(n);
5242 ir_mode *mode = get_irn_mode(n);
5244 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5245 n = transform_node_shift(n);
5248 n = transform_node_bitop_shift(n);
5251 } /* transform_node_Shrs */
5256 static ir_node *transform_node_Shl(ir_node *n) {
5257 ir_node *c, *oldn = n;
5258 ir_node *a = get_Shl_left(n);
5259 ir_node *b = get_Shl_right(n);
5260 ir_mode *mode = get_irn_mode(n);
5262 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5263 n = transform_node_shift(n);
5266 n = transform_node_shl_shr(n);
5268 n = transform_node_bitop_shift(n);
5271 } /* transform_node_Shl */
5276 static ir_node *transform_node_Rotl(ir_node *n) {
5277 ir_node *c, *oldn = n;
5278 ir_node *a = get_Rotl_left(n);
5279 ir_node *b = get_Rotl_right(n);
5280 ir_mode *mode = get_irn_mode(n);
5282 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5283 n = transform_node_shift(n);
5286 n = transform_node_bitop_shift(n);
5289 } /* transform_node_Rotl */
5294 static ir_node *transform_node_Conv(ir_node *n) {
5295 ir_node *c, *oldn = n;
5296 ir_node *a = get_Conv_op(n);
5298 if (is_const_Phi(a)) {
5299 c = apply_conv_on_phi(a, get_irn_mode(n));
5301 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5306 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5307 ir_mode *mode = get_irn_mode(n);
5308 return new_r_Unknown(current_ir_graph, mode);
5312 } /* transform_node_Conv */
5315 * Remove dead blocks and nodes in dead blocks
5316 * in keep alive list. We do not generate a new End node.
5318 static ir_node *transform_node_End(ir_node *n) {
5319 int i, j, n_keepalives = get_End_n_keepalives(n);
5322 NEW_ARR_A(ir_node *, in, n_keepalives);
5324 for (i = j = 0; i < n_keepalives; ++i) {
5325 ir_node *ka = get_End_keepalive(n, i);
5327 if (! is_Block_dead(ka)) {
5331 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5334 /* FIXME: beabi need to keep a Proj(M) */
5335 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5338 if (j != n_keepalives)
5339 set_End_keepalives(n, j, in);
5341 } /* transform_node_End */
5343 /** returns 1 if a == -b */
5344 static int is_negated_value(ir_node *a, ir_node *b) {
5345 if (is_Minus(a) && get_Minus_op(a) == b)
5347 if (is_Minus(b) && get_Minus_op(b) == a)
5349 if (is_Sub(a) && is_Sub(b)) {
5350 ir_node *a_left = get_Sub_left(a);
5351 ir_node *a_right = get_Sub_right(a);
5352 ir_node *b_left = get_Sub_left(b);
5353 ir_node *b_right = get_Sub_right(b);
5355 if (a_left == b_right && a_right == b_left)
5363 * Optimize a Mux into some simpler cases.
5365 static ir_node *transform_node_Mux(ir_node *n) {
5366 ir_node *oldn = n, *sel = get_Mux_sel(n);
5367 ir_mode *mode = get_irn_mode(n);
5368 ir_node *t = get_Mux_true(n);
5369 ir_node *f = get_Mux_false(n);
5370 ir_graph *irg = current_ir_graph;
5371 ir_node *conds[1], *vals[2];
5373 /* first normalization step: move a possible zero to the false case */
5375 ir_node *cmp = get_Proj_pred(sel);
5378 if (is_Const(t) && is_Const_null(t)) {
5379 /* Psi(x, 0, y) => Psi(x, y, 0) */
5380 pn_Cmp pnc = get_Proj_proj(sel);
5381 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5382 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5386 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
5393 /* note: after normalization, false can only happen on default */
5394 if (mode == mode_b) {
5395 dbg_info *dbg = get_irn_dbg_info(n);
5396 ir_node *block = get_nodes_block(n);
5397 ir_graph *irg = current_ir_graph;
5400 tarval *tv_t = get_Const_tarval(t);
5401 if (tv_t == tarval_b_true) {
5403 /* Muxb(sel, true, false) = sel */
5404 assert(get_Const_tarval(f) == tarval_b_false);
5405 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5408 /* Muxb(sel, true, x) = Or(sel, x) */
5409 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5410 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5414 } else if (is_Const(f)) {
5415 tarval *tv_f = get_Const_tarval(f);
5416 if (tv_f == tarval_b_true) {
5417 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5418 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5419 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5420 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5423 /* Muxb(sel, x, false) = And(sel, x) */
5424 assert(tv_f == tarval_b_false);
5425 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5432 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5433 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5434 tarval *a = get_Const_tarval(t);
5435 tarval *b = get_Const_tarval(f);
5436 tarval *null = get_tarval_null(mode);
5439 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5440 diff = tarval_sub(a, b, NULL);
5443 diff = tarval_sub(b, a, NULL);
5447 if (diff == get_tarval_one(mode) && min != null) {
5448 dbg_info *dbg = get_irn_dbg_info(n);
5449 ir_node *block = get_nodes_block(n);
5450 ir_graph *irg = current_ir_graph;
5454 vals[0] = new_Const(mode, tarval_sub(a, min, NULL));
5455 vals[1] = new_Const(mode, tarval_sub(b, min, NULL));
5456 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
5457 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5463 ir_node *cmp = get_Proj_pred(sel);
5464 long pn = get_Proj_proj(sel);
5467 * Note: normalization puts the constant on the right side,
5468 * so we check only one case.
5470 * Note further that these optimization work even for floating point
5471 * with NaN's because -NaN == NaN.
5472 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5476 ir_node *cmp_r = get_Cmp_right(cmp);
5477 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5478 ir_node *block = get_nodes_block(n);
5479 ir_node *cmp_l = get_Cmp_left(cmp);
5481 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5484 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5485 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5487 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
5488 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5490 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5492 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5493 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5495 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5496 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5498 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5500 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5505 if (mode_is_int(mode)) {
5507 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5508 /* Psi((a & b) != 0, c, 0) */
5509 ir_node *and_r = get_And_right(cmp_l);
5512 if (and_r == t && f == cmp_r) {
5513 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5514 if (pn == pn_Cmp_Lg) {
5515 /* Psi((a & 2^C) != 0, 2^C, 0) */
5518 /* Psi((a & 2^C) == 0, 2^C, 0) */
5519 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5520 block, cmp_l, t, mode);
5525 if (is_Shl(and_r)) {
5526 ir_node *shl_l = get_Shl_left(and_r);
5527 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5528 if (and_r == t && f == cmp_r) {
5529 if (pn == pn_Cmp_Lg) {
5530 /* (a & (1 << n)) != 0, (1 << n), 0) */
5533 /* (a & (1 << n)) == 0, (1 << n), 0) */
5534 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5535 block, cmp_l, t, mode);
5541 and_l = get_And_left(cmp_l);
5542 if (is_Shl(and_l)) {
5543 ir_node *shl_l = get_Shl_left(and_l);
5544 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5545 if (and_l == t && f == cmp_r) {
5546 if (pn == pn_Cmp_Lg) {
5547 /* ((1 << n) & a) != 0, (1 << n), 0) */
5550 /* ((1 << n) & a) == 0, (1 << n), 0) */
5551 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5552 block, cmp_l, t, mode);
5563 return arch_transform_node_Mux(n);
5564 } /* transform_node_Mux */
5567 * Optimize a Psi into some simpler cases.
5569 static ir_node *transform_node_Psi(ir_node *n) {
5571 return transform_node_Mux(n);
5574 } /* transform_node_Psi */
5577 * optimize sync nodes that have other syncs as input we simply add the inputs
5578 * of the other sync to our own inputs
5580 static ir_node *transform_node_Sync(ir_node *n) {
5581 int arity = get_Sync_n_preds(n);
5584 for (i = 0; i < arity;) {
5585 ir_node *pred = get_Sync_pred(n, i);
5589 if (!is_Sync(pred)) {
5597 pred_arity = get_Sync_n_preds(pred);
5598 for (j = 0; j < pred_arity; ++j) {
5599 ir_node *pred_pred = get_Sync_pred(pred, j);
5604 add_irn_n(n, pred_pred);
5608 if (get_Sync_pred(n, k) == pred_pred) break;
5613 /* rehash the sync node */
5614 add_identities(current_ir_graph->value_table, n);
5620 * Tries several [inplace] [optimizing] transformations and returns an
5621 * equivalent node. The difference to equivalent_node() is that these
5622 * transformations _do_ generate new nodes, and thus the old node must
5623 * not be freed even if the equivalent node isn't the old one.
5625 static ir_node *transform_node(ir_node *n) {
5629 * Transform_node is the only "optimizing transformation" that might
5630 * return a node with a different opcode. We iterate HERE until fixpoint
5631 * to get the final result.
5635 if (n->op->ops.transform_node)
5636 n = n->op->ops.transform_node(n);
5637 } while (oldn != n);
5640 } /* transform_node */
5643 * Sets the default transform node operation for an ir_op_ops.
5645 * @param code the opcode for the default operation
5646 * @param ops the operations initialized
5651 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5655 ops->transform_node = transform_node_##a; \
5693 } /* firm_set_default_transform_node */
5696 /* **************** Common Subexpression Elimination **************** */
5698 /** The size of the hash table used, should estimate the number of nodes
5700 #define N_IR_NODES 512
5702 /** Compares the attributes of two Const nodes. */
5703 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5704 return (get_Const_tarval(a) != get_Const_tarval(b))
5705 || (get_Const_type(a) != get_Const_type(b));
5706 } /* node_cmp_attr_Const */
5708 /** Compares the attributes of two Proj nodes. */
5709 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5710 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5711 } /* node_cmp_attr_Proj */
5713 /** Compares the attributes of two Filter nodes. */
5714 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5715 return get_Filter_proj(a) != get_Filter_proj(b);
5716 } /* node_cmp_attr_Filter */
5718 /** Compares the attributes of two Alloc nodes. */
5719 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5720 const alloc_attr *pa = get_irn_alloc_attr(a);
5721 const alloc_attr *pb = get_irn_alloc_attr(b);
5722 return (pa->where != pb->where) || (pa->type != pb->type);
5723 } /* node_cmp_attr_Alloc */
5725 /** Compares the attributes of two Free nodes. */
5726 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5727 const free_attr *pa = get_irn_free_attr(a);
5728 const free_attr *pb = get_irn_free_attr(b);
5729 return (pa->where != pb->where) || (pa->type != pb->type);
5730 } /* node_cmp_attr_Free */
5732 /** Compares the attributes of two SymConst nodes. */
5733 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5734 const symconst_attr *pa = get_irn_symconst_attr(a);
5735 const symconst_attr *pb = get_irn_symconst_attr(b);
5736 return (pa->kind != pb->kind)
5737 || (pa->sym.type_p != pb->sym.type_p)
5738 || (pa->tp != pb->tp);
5739 } /* node_cmp_attr_SymConst */
5741 /** Compares the attributes of two Call nodes. */
5742 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5743 return get_irn_call_attr(a) != get_irn_call_attr(b);
5744 } /* node_cmp_attr_Call */
5746 /** Compares the attributes of two Sel nodes. */
5747 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5748 const ir_entity *a_ent = get_Sel_entity(a);
5749 const ir_entity *b_ent = get_Sel_entity(b);
5751 (a_ent->kind != b_ent->kind) ||
5752 (a_ent->name != b_ent->name) ||
5753 (a_ent->owner != b_ent->owner) ||
5754 (a_ent->ld_name != b_ent->ld_name) ||
5755 (a_ent->type != b_ent->type);
5756 } /* node_cmp_attr_Sel */
5758 /** Compares the attributes of two Phi nodes. */
5759 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5760 /* we can only enter this function if both nodes have the same number of inputs,
5761 hence it is enough to check if one of them is a Phi0 */
5763 /* check the Phi0 pos attribute */
5764 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5767 } /* node_cmp_attr_Phi */
5769 /** Compares the attributes of two Conv nodes. */
5770 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5771 return get_Conv_strict(a) != get_Conv_strict(b);
5772 } /* node_cmp_attr_Conv */
5774 /** Compares the attributes of two Cast nodes. */
5775 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5776 return get_Cast_type(a) != get_Cast_type(b);
5777 } /* node_cmp_attr_Cast */
5779 /** Compares the attributes of two Load nodes. */
5780 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5781 if (get_Load_volatility(a) == volatility_is_volatile ||
5782 get_Load_volatility(b) == volatility_is_volatile)
5783 /* NEVER do CSE on volatile Loads */
5785 /* do not CSE Loads with different alignment. Be conservative. */
5786 if (get_Load_align(a) != get_Load_align(b))
5789 return get_Load_mode(a) != get_Load_mode(b);
5790 } /* node_cmp_attr_Load */
5792 /** Compares the attributes of two Store nodes. */
5793 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5794 /* do not CSE Stores with different alignment. Be conservative. */
5795 if (get_Store_align(a) != get_Store_align(b))
5798 /* NEVER do CSE on volatile Stores */
5799 return (get_Store_volatility(a) == volatility_is_volatile ||
5800 get_Store_volatility(b) == volatility_is_volatile);
5801 } /* node_cmp_attr_Store */
5803 /** Compares two exception attributes */
5804 static int node_cmp_exception(ir_node *a, ir_node *b) {
5805 const except_attr *ea = get_irn_except_attr(a);
5806 const except_attr *eb = get_irn_except_attr(b);
5808 return ea->pin_state != eb->pin_state;
5811 #define node_cmp_attr_Bound node_cmp_exception
5813 /** Compares the attributes of two Div nodes. */
5814 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5815 const divmod_attr *ma = get_irn_divmod_attr(a);
5816 const divmod_attr *mb = get_irn_divmod_attr(b);
5817 return ma->exc.pin_state != mb->exc.pin_state ||
5818 ma->res_mode != mb->res_mode ||
5819 ma->no_remainder != mb->no_remainder;
5820 } /* node_cmp_attr_Div */
5822 /** Compares the attributes of two DivMod nodes. */
5823 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5824 const divmod_attr *ma = get_irn_divmod_attr(a);
5825 const divmod_attr *mb = get_irn_divmod_attr(b);
5826 return ma->exc.pin_state != mb->exc.pin_state ||
5827 ma->res_mode != mb->res_mode;
5828 } /* node_cmp_attr_DivMod */
5830 /** Compares the attributes of two Mod nodes. */
5831 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5832 const divmod_attr *ma = get_irn_divmod_attr(a);
5833 const divmod_attr *mb = get_irn_divmod_attr(b);
5834 return ma->exc.pin_state != mb->exc.pin_state ||
5835 ma->res_mode != mb->res_mode;
5836 } /* node_cmp_attr_Mod */
5838 /** Compares the attributes of two Quot nodes. */
5839 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5840 const divmod_attr *ma = get_irn_divmod_attr(a);
5841 const divmod_attr *mb = get_irn_divmod_attr(b);
5842 return ma->exc.pin_state != mb->exc.pin_state ||
5843 ma->res_mode != mb->res_mode;
5844 } /* node_cmp_attr_Quot */
5846 /** Compares the attributes of two Confirm nodes. */
5847 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5848 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5849 } /* node_cmp_attr_Confirm */
5851 /** Compares the attributes of two ASM nodes. */
5852 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5854 const ir_asm_constraint *ca;
5855 const ir_asm_constraint *cb;
5858 if (get_ASM_text(a) != get_ASM_text(b))
5861 /* Should we really check the constraints here? Should be better, but is strange. */
5862 n = get_ASM_n_input_constraints(a);
5863 if (n != get_ASM_n_input_constraints(b))
5866 ca = get_ASM_input_constraints(a);
5867 cb = get_ASM_input_constraints(b);
5868 for (i = 0; i < n; ++i) {
5869 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5873 n = get_ASM_n_output_constraints(a);
5874 if (n != get_ASM_n_output_constraints(b))
5877 ca = get_ASM_output_constraints(a);
5878 cb = get_ASM_output_constraints(b);
5879 for (i = 0; i < n; ++i) {
5880 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5884 n = get_ASM_n_clobbers(a);
5885 if (n != get_ASM_n_clobbers(b))
5888 cla = get_ASM_clobbers(a);
5889 clb = get_ASM_clobbers(b);
5890 for (i = 0; i < n; ++i) {
5891 if (cla[i] != clb[i])
5895 } /* node_cmp_attr_ASM */
5898 * Set the default node attribute compare operation for an ir_op_ops.
5900 * @param code the opcode for the default operation
5901 * @param ops the operations initialized
5906 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5910 ops->node_cmp_attr = node_cmp_attr_##a; \
5941 } /* firm_set_default_node_cmp_attr */
5944 * Compare function for two nodes in the value table. Gets two
5945 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5947 int identities_cmp(const void *elt, const void *key) {
5948 ir_node *a = (ir_node *)elt;
5949 ir_node *b = (ir_node *)key;
5952 if (a == b) return 0;
5954 if ((get_irn_op(a) != get_irn_op(b)) ||
5955 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5957 /* compare if a's in and b's in are of equal length */
5958 irn_arity_a = get_irn_intra_arity(a);
5959 if (irn_arity_a != get_irn_intra_arity(b))
5962 if (get_irn_pinned(a) == op_pin_state_pinned) {
5963 /* for pinned nodes, the block inputs must be equal */
5964 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5966 } else if (! get_opt_global_cse()) {
5967 /* for block-local CSE both nodes must be in the same MacroBlock */
5968 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5972 /* compare a->in[0..ins] with b->in[0..ins] */
5973 for (i = 0; i < irn_arity_a; i++)
5974 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5978 * here, we already now that the nodes are identical except their
5981 if (a->op->ops.node_cmp_attr)
5982 return a->op->ops.node_cmp_attr(a, b);
5985 } /* identities_cmp */
5988 * Calculate a hash value of a node.
5990 * @param node The IR-node
5992 unsigned ir_node_hash(const ir_node *node) {
5993 return node->op->ops.hash(node);
5994 } /* ir_node_hash */
5997 pset *new_identities(void) {
5998 return new_pset(identities_cmp, N_IR_NODES);
5999 } /* new_identities */
6001 void del_identities(pset *value_table) {
6002 del_pset(value_table);
6003 } /* del_identities */
6006 * Normalize a node by putting constants (and operands with larger
6007 * node index) on the right (operator side).
6009 * @param n The node to normalize
6011 static void normalize_node(ir_node *n) {
6012 if (is_op_commutative(get_irn_op(n))) {
6013 ir_node *l = get_binop_left(n);
6014 ir_node *r = get_binop_right(n);
6016 /* For commutative operators perform a OP b == b OP a but keep
6017 * constants on the RIGHT side. This helps greatly in some
6018 * optimizations. Moreover we use the idx number to make the form
6020 if (!operands_are_normalized(l, r)) {
6021 set_binop_left(n, r);
6022 set_binop_right(n, l);
6025 } /* normalize_node */
6028 * Update the nodes after a match in the value table. If both nodes have
6029 * the same MacroBlock but different Blocks, we must ensure that the node
6030 * with the dominating Block (the node that is near to the MacroBlock header
6031 * is stored in the table.
6032 * Because a MacroBlock has only one "non-exception" flow, we don't need
6033 * dominance info here: We known, that one block must dominate the other and
6034 * following the only block input will allow to find it.
6036 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6037 ir_node *known_blk, *new_block, *block, *mbh;
6039 if (get_opt_global_cse()) {
6040 /* Block inputs are meaning less */
6043 known_blk = get_irn_n(known_irn, -1);
6044 new_block = get_irn_n(new_ir_node, -1);
6045 if (known_blk == new_block) {
6046 /* already in the same block */
6050 * We expect the typical case when we built the graph. In that case, the
6051 * known_irn is already the upper one, so checking this should be faster.
6054 mbh = get_Block_MacroBlock(new_block);
6056 if (block == known_blk) {
6057 /* ok, we have found it: known_block dominates new_block as expected */
6062 * We have reached the MacroBlock header NOT founding
6063 * the known_block. new_block must dominate known_block.
6066 set_irn_n(known_irn, -1, new_block);
6069 assert(get_Block_n_cfgpreds(block) == 1);
6070 block = get_Block_cfgpred_block(block, 0);
6072 } /* update_value_table */
6075 * Return the canonical node computing the same value as n.
6076 * Looks up the node in a hash table, enters it in the table
6077 * if it isn't there yet.
6079 * @param value_table the HashSet containing all nodes in the
6081 * @param n the node to look up
6083 * @return a node that computes the same value as n or n if no such
6084 * node could be found
6086 ir_node *identify_remember(pset *value_table, ir_node *n) {
6089 if (!value_table) return n;
6092 /* lookup or insert in hash table with given hash key. */
6093 o = pset_insert(value_table, n, ir_node_hash(n));
6096 update_known_irn(o, n);
6101 } /* identify_remember */
6104 * During construction we set the op_pin_state_pinned flag in the graph right when the
6105 * optimization is performed. The flag turning on procedure global cse could
6106 * be changed between two allocations. This way we are safe.
6108 * @param value_table The value table
6109 * @param n The node to lookup
6111 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
6114 n = identify_remember(value_table, n);
6115 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6116 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6118 } /* identify_cons */
6120 /* Add a node to the identities value table. */
6121 void add_identities(pset *value_table, ir_node *node) {
6122 if (get_opt_cse() && is_no_Block(node))
6123 identify_remember(value_table, node);
6124 } /* add_identities */
6126 /* Visit each node in the value table of a graph. */
6127 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6129 ir_graph *rem = current_ir_graph;
6131 current_ir_graph = irg;
6132 foreach_pset(irg->value_table, node)
6134 current_ir_graph = rem;
6135 } /* visit_all_identities */
6138 * Garbage in, garbage out. If a node has a dead input, i.e., the
6139 * Bad node is input to the node, return the Bad node.
6141 static ir_node *gigo(ir_node *node) {
6143 ir_op *op = get_irn_op(node);
6145 /* remove garbage blocks by looking at control flow that leaves the block
6146 and replacing the control flow by Bad. */
6147 if (get_irn_mode(node) == mode_X) {
6148 ir_node *block = get_nodes_block(skip_Proj(node));
6150 /* Don't optimize nodes in immature blocks. */
6151 if (!get_Block_matured(block))
6153 /* Don't optimize End, may have Bads. */
6154 if (op == op_End) return node;
6156 if (is_Block(block)) {
6157 if (is_Block_dead(block)) {
6158 /* control flow from dead block is dead */
6162 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6163 if (!is_Bad(get_irn_n(block, i)))
6167 ir_graph *irg = get_irn_irg(block);
6168 /* the start block is never dead */
6169 if (block != get_irg_start_block(irg)
6170 && block != get_irg_end_block(irg)) {
6172 * Do NOT kill control flow without setting
6173 * the block to dead of bad things can happen:
6174 * We get a Block that is not reachable be irg_block_walk()
6175 * but can be found by irg_walk()!
6177 set_Block_dead(block);
6184 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6185 blocks predecessors is dead. */
6186 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6187 irn_arity = get_irn_arity(node);
6190 * Beware: we can only read the block of a non-floating node.
6192 if (is_irn_pinned_in_irg(node) &&
6193 is_Block_dead(get_nodes_block(skip_Proj(node))))
6196 for (i = 0; i < irn_arity; i++) {
6197 ir_node *pred = get_irn_n(node, i);
6202 /* Propagating Unknowns here seems to be a bad idea, because
6203 sometimes we need a node as a input and did not want that
6205 However, it might be useful to move this into a later phase
6206 (if you think that optimizing such code is useful). */
6207 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6208 return new_Unknown(get_irn_mode(node));
6213 /* With this code we violate the agreement that local_optimize
6214 only leaves Bads in Block, Phi and Tuple nodes. */
6215 /* If Block has only Bads as predecessors it's garbage. */
6216 /* If Phi has only Bads as predecessors it's garbage. */
6217 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6218 irn_arity = get_irn_arity(node);
6219 for (i = 0; i < irn_arity; i++) {
6220 if (!is_Bad(get_irn_n(node, i))) break;
6222 if (i == irn_arity) node = new_Bad();
6229 * These optimizations deallocate nodes from the obstack.
6230 * It can only be called if it is guaranteed that no other nodes
6231 * reference this one, i.e., right after construction of a node.
6233 * @param n The node to optimize
6235 * current_ir_graph must be set to the graph of the node!
6237 ir_node *optimize_node(ir_node *n) {
6240 ir_opcode iro = get_irn_opcode(n);
6242 /* Always optimize Phi nodes: part of the construction. */
6243 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6245 /* constant expression evaluation / constant folding */
6246 if (get_opt_constant_folding()) {
6247 /* neither constants nor Tuple values can be evaluated */
6248 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6249 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6250 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6251 /* try to evaluate */
6252 tv = computed_value(n);
6253 if (tv != tarval_bad) {
6255 ir_type *old_tp = get_irn_type(n);
6256 int i, arity = get_irn_arity(n);
6260 * Try to recover the type of the new expression.
6262 for (i = 0; i < arity && !old_tp; ++i)
6263 old_tp = get_irn_type(get_irn_n(n, i));
6266 * we MUST copy the node here temporary, because it's still needed
6267 * for DBG_OPT_CSTEVAL
6269 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6270 oldn = alloca(node_size);
6272 memcpy(oldn, n, node_size);
6273 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6275 /* ARG, copy the in array, we need it for statistics */
6276 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6278 /* note the inplace edges module */
6279 edges_node_deleted(n, current_ir_graph);
6281 /* evaluation was successful -- replace the node. */
6282 irg_kill_node(current_ir_graph, n);
6283 nw = new_Const(get_tarval_mode(tv), tv);
6285 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6286 set_Const_type(nw, old_tp);
6287 DBG_OPT_CSTEVAL(oldn, nw);
6288 tarval_enable_fp_ops(old_fp_mode);
6291 tarval_enable_fp_ops(old_fp_mode);
6295 /* remove unnecessary nodes */
6296 if (get_opt_algebraic_simplification() ||
6297 (iro == iro_Phi) || /* always optimize these nodes. */
6299 (iro == iro_Proj) ||
6300 (iro == iro_Block) ) /* Flags tested local. */
6301 n = equivalent_node(n);
6303 /* Common Subexpression Elimination.
6305 * Checks whether n is already available.
6306 * The block input is used to distinguish different subexpressions. Right
6307 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6308 * subexpressions within a block.
6311 n = identify_cons(current_ir_graph->value_table, n);
6314 edges_node_deleted(oldn, current_ir_graph);
6316 /* We found an existing, better node, so we can deallocate the old node. */
6317 irg_kill_node(current_ir_graph, oldn);
6321 /* Some more constant expression evaluation that does not allow to
6323 iro = get_irn_opcode(n);
6324 if (get_opt_algebraic_simplification() ||
6325 (iro == iro_Cond) ||
6326 (iro == iro_Proj)) /* Flags tested local. */
6327 n = transform_node(n);
6329 /* Remove nodes with dead (Bad) input.
6330 Run always for transformation induced Bads. */
6333 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6334 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6335 n = identify_remember(current_ir_graph->value_table, n);
6339 } /* optimize_node */
6343 * These optimizations never deallocate nodes (in place). This can cause dead
6344 * nodes lying on the obstack. Remove these by a dead node elimination,
6345 * i.e., a copying garbage collection.
6347 ir_node *optimize_in_place_2(ir_node *n) {
6350 ir_opcode iro = get_irn_opcode(n);
6352 if (!get_opt_optimize() && !is_Phi(n)) return n;
6354 /* constant expression evaluation / constant folding */
6355 if (get_opt_constant_folding()) {
6356 /* neither constants nor Tuple values can be evaluated */
6357 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6358 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6359 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6360 /* try to evaluate */
6361 tv = computed_value(n);
6362 if (tv != tarval_bad) {
6363 /* evaluation was successful -- replace the node. */
6364 ir_type *old_tp = get_irn_type(n);
6365 int i, arity = get_irn_arity(n);
6368 * Try to recover the type of the new expression.
6370 for (i = 0; i < arity && !old_tp; ++i)
6371 old_tp = get_irn_type(get_irn_n(n, i));
6373 n = new_Const(get_tarval_mode(tv), tv);
6375 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6376 set_Const_type(n, old_tp);
6378 DBG_OPT_CSTEVAL(oldn, n);
6379 tarval_enable_fp_ops(old_fp_mode);
6382 tarval_enable_fp_ops(old_fp_mode);
6386 /* remove unnecessary nodes */
6387 if (get_opt_constant_folding() ||
6388 (iro == iro_Phi) || /* always optimize these nodes. */
6389 (iro == iro_Id) || /* ... */
6390 (iro == iro_Proj) || /* ... */
6391 (iro == iro_Block) ) /* Flags tested local. */
6392 n = equivalent_node(n);
6394 /** common subexpression elimination **/
6395 /* Checks whether n is already available. */
6396 /* The block input is used to distinguish different subexpressions. Right
6397 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6398 subexpressions within a block. */
6399 if (get_opt_cse()) {
6400 n = identify_remember(current_ir_graph->value_table, n);
6403 /* Some more constant expression evaluation. */
6404 iro = get_irn_opcode(n);
6405 if (get_opt_constant_folding() ||
6406 (iro == iro_Cond) ||
6407 (iro == iro_Proj)) /* Flags tested local. */
6408 n = transform_node(n);
6410 /* Remove nodes with dead (Bad) input.
6411 Run always for transformation induced Bads. */
6414 /* Now we can verify the node, as it has no dead inputs any more. */
6417 /* Now we have a legal, useful node. Enter it in hash table for cse.
6418 Blocks should be unique anyways. (Except the successor of start:
6419 is cse with the start block!) */
6420 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6421 n = identify_remember(current_ir_graph->value_table, n);
6424 } /* optimize_in_place_2 */
6427 * Wrapper for external use, set proper status bits after optimization.
6429 ir_node *optimize_in_place(ir_node *n) {
6430 /* Handle graph state */
6431 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6433 if (get_opt_global_cse())
6434 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6435 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6436 set_irg_outs_inconsistent(current_ir_graph);
6438 /* FIXME: Maybe we could also test whether optimizing the node can
6439 change the control graph. */
6440 set_irg_doms_inconsistent(current_ir_graph);
6441 return optimize_in_place_2(n);
6442 } /* optimize_in_place */
6445 * Calculate a hash value of a Const node.
6447 static unsigned hash_Const(const ir_node *node) {
6450 /* special value for const, as they only differ in their tarval. */
6451 h = HASH_PTR(node->attr.con.tv);
6452 h = 9*h + HASH_PTR(get_irn_mode(node));
6458 * Calculate a hash value of a SymConst node.
6460 static unsigned hash_SymConst(const ir_node *node) {
6463 /* special value for const, as they only differ in their symbol. */
6464 h = HASH_PTR(node->attr.symc.sym.type_p);
6465 h = 9*h + HASH_PTR(get_irn_mode(node));
6468 } /* hash_SymConst */
6471 * Set the default hash operation in an ir_op_ops.
6473 * @param code the opcode for the default operation
6474 * @param ops the operations initialized
6479 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6483 ops->hash = hash_##a; \
6486 /* hash function already set */
6487 if (ops->hash != NULL)
6494 /* use input/mode default hash if no function was given */
6495 ops->hash = firm_default_hash;
6503 * Sets the default operation for an ir_ops.
6505 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6506 ops = firm_set_default_hash(code, ops);
6507 ops = firm_set_default_computed_value(code, ops);
6508 ops = firm_set_default_equivalent_node(code, ops);
6509 ops = firm_set_default_transform_node(code, ops);
6510 ops = firm_set_default_node_cmp_attr(code, ops);
6511 ops = firm_set_default_get_type(code, ops);
6512 ops = firm_set_default_get_type_attr(code, ops);
6513 ops = firm_set_default_get_entity_attr(code, ops);
6516 } /* firm_set_default_operations */