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 * Return the value of a Constant.
59 static tarval *computed_value_Const(ir_node *n) {
60 return get_Const_tarval(n);
61 } /* computed_value_Const */
64 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
66 static tarval *computed_value_SymConst(ir_node *n) {
70 switch (get_SymConst_kind(n)) {
71 case symconst_type_size:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
76 case symconst_type_align:
77 type = get_SymConst_type(n);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
81 case symconst_ofs_ent:
82 ent = get_SymConst_entity(n);
83 type = get_entity_owner(ent);
84 if (get_type_state(type) == layout_fixed)
85 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
91 } /* computed_value_SymConst */
94 * Return the value of an Add.
96 static tarval *computed_value_Add(ir_node *n) {
97 ir_node *a = get_Add_left(n);
98 ir_node *b = get_Add_right(n);
100 tarval *ta = value_of(a);
101 tarval *tb = value_of(b);
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_add(ta, tb);
107 } /* computed_value_Add */
110 * Return the value of a Sub.
111 * Special case: a - a
113 static tarval *computed_value_Sub(ir_node *n) {
114 ir_node *a = get_Sub_left(n);
115 ir_node *b = get_Sub_right(n);
120 if (a == b && !is_Bad(a))
121 return get_mode_null(get_irn_mode(n));
126 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127 return tarval_sub(ta, tb);
130 } /* computed_value_Sub */
133 * Return the value of a Carry.
134 * Special : a op 0, 0 op b
136 static tarval *computed_value_Carry(ir_node *n) {
137 ir_node *a = get_binop_left(n);
138 ir_node *b = get_binop_right(n);
139 ir_mode *m = get_irn_mode(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad)) {
146 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
148 if (tarval_is_null(ta) || tarval_is_null(tb))
149 return get_mode_null(m);
152 } /* computed_value_Carry */
155 * Return the value of a Borrow.
158 static tarval *computed_value_Borrow(ir_node *n) {
159 ir_node *a = get_binop_left(n);
160 ir_node *b = get_binop_right(n);
161 ir_mode *m = get_irn_mode(n);
163 tarval *ta = value_of(a);
164 tarval *tb = value_of(b);
166 if ((ta != tarval_bad) && (tb != tarval_bad)) {
167 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
168 } else if (tarval_is_null(ta)) {
169 return get_mode_null(m);
172 } /* computed_value_Borrow */
175 * Return the value of an unary Minus.
177 static tarval *computed_value_Minus(ir_node *n) {
178 ir_node *a = get_Minus_op(n);
179 tarval *ta = value_of(a);
181 if (ta != tarval_bad)
182 return tarval_neg(ta);
185 } /* computed_value_Minus */
188 * Return the value of a Mul.
190 static tarval *computed_value_Mul(ir_node *n) {
191 ir_node *a = get_Mul_left(n);
192 ir_node *b = get_Mul_right(n);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 mode = get_irn_mode(n);
199 if (mode != get_irn_mode(a)) {
200 /* n * n = 2n bit multiplication */
201 ta = tarval_convert_to(ta, mode);
202 tb = tarval_convert_to(tb, mode);
205 if (ta != tarval_bad && tb != tarval_bad) {
206 return tarval_mul(ta, tb);
208 /* a*0 = 0 or 0*b = 0 */
209 if (ta == get_mode_null(mode))
211 if (tb == get_mode_null(mode))
215 } /* computed_value_Mul */
218 * Return the value of a floating point Quot.
220 static tarval *computed_value_Quot(ir_node *n) {
221 ir_node *a = get_Quot_left(n);
222 ir_node *b = get_Quot_right(n);
224 tarval *ta = value_of(a);
225 tarval *tb = value_of(b);
227 /* This was missing in original implementation. Why? */
228 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
229 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
230 return tarval_quo(ta, tb);
233 } /* computed_value_Quot */
236 * Calculate the value of an integer Div of two nodes.
237 * Special case: 0 / b
239 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
240 tarval *ta = value_of(a);
241 tarval *tb = value_of(b);
243 /* Compute c1 / c2 or 0 / a, a != 0 */
244 if (ta != tarval_bad) {
245 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
246 return tarval_div(ta, tb);
247 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
251 } /* do_computed_value_Div */
254 * Return the value of an integer Div.
256 static tarval *computed_value_Div(ir_node *n) {
257 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
258 } /* computed_value_Div */
261 * Calculate the value of an integer Mod of two nodes.
262 * Special case: a % 1
264 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 /* Compute c1 % c2 or a % 1 */
269 if (tb != tarval_bad) {
270 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
271 return tarval_mod(ta, tb);
272 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
273 return get_mode_null(get_irn_mode(a));
276 } /* do_computed_value_Mod */
279 * Return the value of an integer Mod.
281 static tarval *computed_value_Mod(ir_node *n) {
282 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
283 } /* computed_value_Mod */
286 * Return the value of an Abs.
288 static tarval *computed_value_Abs(ir_node *n) {
289 ir_node *a = get_Abs_op(n);
290 tarval *ta = value_of(a);
292 if (ta != tarval_bad)
293 return tarval_abs(ta);
296 } /* computed_value_Abs */
299 * Return the value of an And.
300 * Special case: a & 0, 0 & b
302 static tarval *computed_value_And(ir_node *n) {
303 ir_node *a = get_And_left(n);
304 ir_node *b = get_And_right(n);
306 tarval *ta = value_of(a);
307 tarval *tb = value_of(b);
309 if ((ta != tarval_bad) && (tb != tarval_bad)) {
310 return tarval_and (ta, tb);
312 if (tarval_is_null(ta)) return ta;
313 if (tarval_is_null(tb)) return tb;
316 } /* computed_value_And */
319 * Return the value of an Or.
320 * Special case: a | 1...1, 1...1 | b
322 static tarval *computed_value_Or(ir_node *n) {
323 ir_node *a = get_Or_left(n);
324 ir_node *b = get_Or_right(n);
326 tarval *ta = value_of(a);
327 tarval *tb = value_of(b);
329 if ((ta != tarval_bad) && (tb != tarval_bad)) {
330 return tarval_or (ta, tb);
332 if (tarval_is_all_one(ta)) return ta;
333 if (tarval_is_all_one(tb)) return tb;
336 } /* computed_value_Or */
339 * Return the value of an Eor.
341 static tarval *computed_value_Eor(ir_node *n) {
342 ir_node *a = get_Eor_left(n);
343 ir_node *b = get_Eor_right(n);
348 return get_mode_null(get_irn_mode(n));
353 if ((ta != tarval_bad) && (tb != tarval_bad)) {
354 return tarval_eor (ta, tb);
357 } /* computed_value_Eor */
360 * Return the value of a Not.
362 static tarval *computed_value_Not(ir_node *n) {
363 ir_node *a = get_Not_op(n);
364 tarval *ta = value_of(a);
366 if (ta != tarval_bad)
367 return tarval_not(ta);
370 } /* computed_value_Not */
373 * Return the value of a Shl.
375 static tarval *computed_value_Shl(ir_node *n) {
376 ir_node *a = get_Shl_left(n);
377 ir_node *b = get_Shl_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_shl (ta, tb);
386 } /* computed_value_Shl */
389 * Return the value of a Shr.
391 static tarval *computed_value_Shr(ir_node *n) {
392 ir_node *a = get_Shr_left(n);
393 ir_node *b = get_Shr_right(n);
395 tarval *ta = value_of(a);
396 tarval *tb = value_of(b);
398 if ((ta != tarval_bad) && (tb != tarval_bad)) {
399 return tarval_shr (ta, tb);
402 } /* computed_value_Shr */
405 * Return the value of a Shrs.
407 static tarval *computed_value_Shrs(ir_node *n) {
408 ir_node *a = get_Shrs_left(n);
409 ir_node *b = get_Shrs_right(n);
411 tarval *ta = value_of(a);
412 tarval *tb = value_of(b);
414 if ((ta != tarval_bad) && (tb != tarval_bad)) {
415 return tarval_shrs (ta, tb);
418 } /* computed_value_Shrs */
421 * Return the value of a Rotl.
423 static tarval *computed_value_Rotl(ir_node *n) {
424 ir_node *a = get_Rotl_left(n);
425 ir_node *b = get_Rotl_right(n);
427 tarval *ta = value_of(a);
428 tarval *tb = value_of(b);
430 if ((ta != tarval_bad) && (tb != tarval_bad)) {
431 return tarval_rotl(ta, tb);
434 } /* computed_value_Rotl */
437 * Return the value of a Conv.
439 static tarval *computed_value_Conv(ir_node *n) {
440 ir_node *a = get_Conv_op(n);
441 tarval *ta = value_of(a);
443 if (ta != tarval_bad)
444 return tarval_convert_to(ta, get_irn_mode(n));
447 } /* computed_value_Conv */
450 * Return the value of a Proj(Cmp).
452 * This performs a first step of unreachable code elimination.
453 * Proj can not be computed, but folding a Cmp above the Proj here is
454 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
456 * There are several case where we can evaluate a Cmp node, see later.
458 static tarval *computed_value_Proj_Cmp(ir_node *n) {
459 ir_node *a = get_Proj_pred(n);
460 ir_node *aa = get_Cmp_left(a);
461 ir_node *ab = get_Cmp_right(a);
462 long proj_nr = get_Proj_proj(n);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
469 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
472 /* This is a trick with the bits used for encoding the Cmp
473 Proj numbers, the following statement is not the same:
474 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
478 tarval *taa = value_of(aa);
479 tarval *tab = value_of(ab);
480 ir_mode *mode = get_irn_mode(aa);
483 * The predecessors of Cmp are target values. We can evaluate
486 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487 /* strange checks... */
488 pn_Cmp flags = tarval_cmp(taa, tab);
489 if (flags != pn_Cmp_False) {
490 return new_tarval_from_long (proj_nr & flags, mode_b);
493 /* for integer values, we can check against MIN/MAX */
494 else if (mode_is_int(mode)) {
495 /* MIN <=/> x. This results in true/false. */
496 if (taa == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Le)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Gt)
501 return get_tarval_b_false();
503 /* x >=/< MIN. This results in true/false. */
505 if (tab == get_mode_min(mode)) {
506 /* a compare with the MIN value */
507 if (proj_nr == pn_Cmp_Ge)
508 return get_tarval_b_true();
509 else if (proj_nr == pn_Cmp_Lt)
510 return get_tarval_b_false();
512 /* MAX >=/< x. This results in true/false. */
513 else if (taa == get_mode_max(mode)) {
514 if (proj_nr == pn_Cmp_Ge)
515 return get_tarval_b_true();
516 else if (proj_nr == pn_Cmp_Lt)
517 return get_tarval_b_false();
519 /* x <=/> MAX. This results in true/false. */
520 else if (tab == get_mode_max(mode)) {
521 if (proj_nr == pn_Cmp_Le)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Gt)
524 return get_tarval_b_false();
528 * The predecessors are Allocs or (void*)(0) constants. Allocs never
529 * return NULL, they raise an exception. Therefore we can predict
533 ir_node *aaa = skip_Id(skip_Proj(aa));
534 ir_node *aba = skip_Id(skip_Proj(ab));
536 if ( ( (/* aa is ProjP and aaa is Alloc */
538 && mode_is_reference(get_irn_mode(aa))
540 && ( (/* ab is NULL */
542 && mode_is_reference(get_irn_mode(ab))
543 && is_Const_null(ab))
544 || (/* ab is other Alloc */
546 && mode_is_reference(get_irn_mode(ab))
549 || (/* aa is NULL and aba is Alloc */
551 && mode_is_reference(get_irn_mode(aa))
554 && mode_is_reference(get_irn_mode(ab))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 } /* computed_value_Proj_Cmp */
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 computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(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 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
647 value_of(get_Confirm_bound(n)) : tarval_bad;
650 } /* computed_value_Confirm */
653 * If the parameter n can be computed, return its value, else tarval_bad.
654 * Performs constant folding.
656 * @param n The node this should be evaluated
658 tarval *computed_value(ir_node *n) {
659 if (n->op->ops.computed_value)
660 return n->op->ops.computed_value(n);
662 } /* computed_value */
665 * Set the default computed_value evaluator in an ir_op_ops.
667 * @param code the opcode for the default operation
668 * @param ops the operations initialized
673 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
677 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);
987 if (is_Const(b) && is_Const_null(b)) {
990 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
993 } /* equivalent_node_left_zero */
995 #define equivalent_node_Shl equivalent_node_left_zero
996 #define equivalent_node_Shr equivalent_node_left_zero
997 #define equivalent_node_Shrs equivalent_node_left_zero
998 #define equivalent_node_Rotl equivalent_node_left_zero
1001 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1003 * The second one looks strange, but this construct
1004 * is used heavily in the LCC sources :-).
1006 * Beware: The Mode of a Sub may be different than the mode of its
1007 * predecessors, so we could not return a predecessors in all cases.
1009 static ir_node *equivalent_node_Sub(ir_node *n) {
1012 ir_mode *mode = get_irn_mode(n);
1014 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1015 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1018 b = get_Sub_right(n);
1020 /* Beware: modes might be different */
1021 if (is_Const(b) && is_Const_null(b)) {
1022 ir_node *a = get_Sub_left(n);
1023 if (mode == get_irn_mode(a)) {
1026 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1030 } /* equivalent_node_Sub */
1034 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1037 * -(-a) == a, but might overflow two times.
1038 * We handle it anyway here but the better way would be a
1039 * flag. This would be needed for Pascal for instance.
1041 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1043 ir_node *pred = get_unop_op(n);
1045 /* optimize symmetric unop */
1046 if (get_irn_op(pred) == get_irn_op(n)) {
1047 n = get_unop_op(pred);
1048 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1051 } /* equivalent_node_idempotent_unop */
1053 /** Optimize Not(Not(x)) == x. */
1054 #define equivalent_node_Not equivalent_node_idempotent_unop
1056 /** -(-x) == x ??? Is this possible or can --x raise an
1057 out of bounds exception if min =! max? */
1058 #define equivalent_node_Minus equivalent_node_idempotent_unop
1061 * Optimize a * 1 = 1 * a = a.
1063 static ir_node *equivalent_node_Mul(ir_node *n) {
1065 ir_node *a = get_Mul_left(n);
1067 /* we can handle here only the n * n = n bit cases */
1068 if (get_irn_mode(n) == get_irn_mode(a)) {
1069 ir_node *b = get_Mul_right(n);
1071 /* Mul is commutative and has again an other neutral element. */
1072 if (is_Const(a) && is_Const_one(a)) {
1074 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1075 } else if (is_Const(b) && is_Const_one(b)) {
1077 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1081 } /* equivalent_node_Mul */
1084 * Optimize a / 1 = a.
1086 static ir_node *equivalent_node_Div(ir_node *n) {
1087 ir_node *a = get_Div_left(n);
1088 ir_node *b = get_Div_right(n);
1090 /* Div is not commutative. */
1091 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1092 /* Turn Div into a tuple (mem, bad, a) */
1093 ir_node *mem = get_Div_mem(n);
1094 ir_node *blk = get_irn_n(n, -1);
1095 turn_into_tuple(n, pn_Div_max);
1096 set_Tuple_pred(n, pn_Div_M, mem);
1097 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1098 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1099 set_Tuple_pred(n, pn_Div_res, a);
1102 } /* equivalent_node_Div */
1105 * Optimize a / 1.0 = a.
1107 static ir_node *equivalent_node_Quot(ir_node *n) {
1108 ir_node *a = get_Quot_left(n);
1109 ir_node *b = get_Quot_right(n);
1111 /* Div is not commutative. */
1112 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1113 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1114 ir_node *mem = get_Quot_mem(n);
1115 ir_node *blk = get_irn_n(n, -1);
1116 turn_into_tuple(n, pn_Quot_max);
1117 set_Tuple_pred(n, pn_Quot_M, mem);
1118 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1119 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1120 set_Tuple_pred(n, pn_Quot_res, a);
1123 } /* equivalent_node_Quot */
1126 * Optimize a / 1 = a.
1128 static ir_node *equivalent_node_DivMod(ir_node *n) {
1129 ir_node *b = get_DivMod_right(n);
1131 /* Div is not commutative. */
1132 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1133 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1134 ir_node *a = get_DivMod_left(n);
1135 ir_node *mem = get_Div_mem(n);
1136 ir_node *blk = get_irn_n(n, -1);
1137 ir_mode *mode = get_DivMod_resmode(n);
1139 turn_into_tuple(n, pn_DivMod_max);
1140 set_Tuple_pred(n, pn_DivMod_M, mem);
1141 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1142 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1143 set_Tuple_pred(n, pn_DivMod_res_div, a);
1144 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1147 } /* equivalent_node_DivMod */
1150 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1152 static ir_node *equivalent_node_Or(ir_node *n) {
1155 ir_node *a = get_Or_left(n);
1156 ir_node *b = get_Or_right(n);
1159 n = a; /* Or has it's own neutral element */
1160 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1161 } else if (is_Const(a) && is_Const_null(a)) {
1163 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1164 } else if (is_Const(b) && is_Const_null(b)) {
1166 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1170 } /* equivalent_node_Or */
1173 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1175 static ir_node *equivalent_node_And(ir_node *n) {
1178 ir_node *a = get_And_left(n);
1179 ir_node *b = get_And_right(n);
1182 n = a; /* And has it's own neutral element */
1183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1186 if (is_Const(a) && is_Const_all_one(a)) {
1188 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1191 if (is_Const(b) && is_Const_all_one(b)) {
1193 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1197 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1200 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1205 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1208 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1214 } /* equivalent_node_And */
1217 * Try to remove useless Conv's:
1219 static ir_node *equivalent_node_Conv(ir_node *n) {
1221 ir_node *a = get_Conv_op(n);
1223 ir_mode *n_mode = get_irn_mode(n);
1224 ir_mode *a_mode = get_irn_mode(a);
1227 if (n_mode == a_mode) { /* No Conv necessary */
1228 if (get_Conv_strict(n)) {
1229 /* special case: the predecessor might be a also a Conv */
1231 if (! get_Conv_strict(a)) {
1232 /* first one is not strict, kick it */
1234 a_mode = get_irn_mode(a);
1238 /* else both are strict conv, second is superfluous */
1239 } else if (is_Proj(a)) {
1240 ir_node *pred = get_Proj_pred(a);
1241 if (is_Load(pred)) {
1242 /* loads always return with the exact precision of n_mode */
1243 assert(get_Load_mode(pred) == n_mode);
1248 /* leave strict floating point Conv's */
1252 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1253 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1254 ir_node *b = get_Conv_op(a);
1255 ir_mode *b_mode = get_irn_mode(b);
1257 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1258 /* both are strict conv */
1259 if (smaller_mode(a_mode, n_mode)) {
1260 /* both are strict, but the first is smaller, so
1261 the second cannot remove more precision, remove the
1263 set_Conv_strict(n, 0);
1266 if (n_mode == b_mode) {
1267 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1268 if (n_mode == mode_b) {
1269 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1271 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1272 if (smaller_mode(b_mode, a_mode)) {
1273 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1274 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1279 if (smaller_mode(b_mode, a_mode)) {
1280 if (get_Conv_strict(n))
1281 set_Conv_strict(b, 1);
1282 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1283 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1289 } /* equivalent_node_Conv */
1292 * A Cast may be removed if the type of the previous node
1293 * is already the type of the Cast.
1295 static ir_node *equivalent_node_Cast(ir_node *n) {
1297 ir_node *pred = get_Cast_op(n);
1299 if (get_irn_type(pred) == get_Cast_type(n)) {
1301 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1304 } /* equivalent_node_Cast */
1307 * Several optimizations:
1308 * - no Phi in start block.
1309 * - remove Id operators that are inputs to Phi
1310 * - fold Phi-nodes, iff they have only one predecessor except
1313 static ir_node *equivalent_node_Phi(ir_node *n) {
1318 ir_node *first_val = NULL; /* to shutup gcc */
1320 if (!get_opt_normalize()) return n;
1322 n_preds = get_Phi_n_preds(n);
1324 block = get_nodes_block(n);
1325 if ((is_Block_dead(block)) || /* Control dead */
1326 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1327 return new_Bad(); /* in the Start Block. */
1329 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1331 /* If the Block has a Bad pred, we also have one. */
1332 for (i = 0; i < n_preds; ++i)
1333 if (is_Bad(get_Block_cfgpred(block, i)))
1334 set_Phi_pred(n, i, new_Bad());
1336 /* Find first non-self-referencing input */
1337 for (i = 0; i < n_preds; ++i) {
1338 first_val = get_Phi_pred(n, i);
1339 if ( (first_val != n) /* not self pointer */
1341 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1342 * predecessors. Then, Phi nodes in dead code might be removed, causing
1343 * nodes pointing to themself (Add's for instance).
1344 * This is really bad and causes endless recursions in several
1345 * code pathes, so we do NOT optimize such a code.
1346 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1347 * (and bad Phi predecessors), so live code is optimized later.
1349 && (! is_Bad(first_val))
1351 ) { /* value not dead */
1352 break; /* then found first value. */
1357 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1361 /* search for rest of inputs, determine if any of these
1362 are non-self-referencing */
1363 while (++i < n_preds) {
1364 ir_node *scnd_val = get_Phi_pred(n, i);
1365 if ( (scnd_val != n)
1366 && (scnd_val != first_val)
1369 && (! is_Bad(scnd_val))
1377 /* Fold, if no multiple distinct non-self-referencing inputs */
1379 DBG_OPT_PHI(oldn, n);
1382 } /* equivalent_node_Phi */
1385 * Several optimizations:
1386 * - no Sync in start block.
1387 * - fold Sync-nodes, iff they have only one predecessor except
1390 static ir_node *equivalent_node_Sync(ir_node *n) {
1391 int arity = get_Sync_n_preds(n);
1394 for (i = 0; i < arity;) {
1395 ir_node *pred = get_Sync_pred(n, i);
1398 /* Remove Bad predecessors */
1405 /* Remove duplicate predecessors */
1411 if (get_Sync_pred(n, j) == pred) {
1419 if (arity == 0) return new_Bad();
1420 if (arity == 1) return get_Sync_pred(n, 0);
1422 } /* equivalent_node_Sync */
1425 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1426 * ProjX(Load) and ProjX(Store).
1428 static ir_node *equivalent_node_Proj(ir_node *proj) {
1429 ir_node *oldn = proj;
1430 ir_node *a = get_Proj_pred(proj);
1433 /* Remove the Tuple/Proj combination. */
1434 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1435 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1436 DBG_OPT_TUPLE(oldn, a, proj);
1438 /* This should not happen! */
1439 assert(! "found a Proj with higher number than Tuple predecessors");
1442 } else if (get_irn_mode(proj) == mode_X) {
1443 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1444 /* Remove dead control flow -- early gigo(). */
1446 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1448 /* get the Load address */
1449 ir_node *addr = get_Load_ptr(a);
1450 ir_node *blk = get_irn_n(a, -1);
1453 if (value_not_null(addr, &confirm)) {
1454 if (confirm == NULL) {
1455 /* this node may float if it did not depend on a Confirm */
1456 set_irn_pinned(a, op_pin_state_floats);
1458 if (get_Proj_proj(proj) == pn_Load_X_except) {
1459 DBG_OPT_EXC_REM(proj);
1462 return new_r_Jmp(current_ir_graph, blk);
1464 } else if (is_Store(a)) {
1465 /* get the load/store address */
1466 ir_node *addr = get_Store_ptr(a);
1467 ir_node *blk = get_irn_n(a, -1);
1470 if (value_not_null(addr, &confirm)) {
1471 if (confirm == NULL) {
1472 /* this node may float if it did not depend on a Confirm */
1473 set_irn_pinned(a, op_pin_state_floats);
1475 if (get_Proj_proj(proj) == pn_Store_X_except) {
1476 DBG_OPT_EXC_REM(proj);
1479 return new_r_Jmp(current_ir_graph, blk);
1486 } /* equivalent_node_Proj */
1491 static ir_node *equivalent_node_Id(ir_node *n) {
1496 } while (get_irn_op(n) == op_Id);
1498 DBG_OPT_ID(oldn, n);
1500 } /* equivalent_node_Id */
1505 static ir_node *equivalent_node_Mux(ir_node *n)
1507 ir_node *oldn = n, *sel = get_Mux_sel(n);
1508 tarval *ts = value_of(sel);
1510 /* Mux(true, f, t) == t */
1511 if (ts == tarval_b_true) {
1512 n = get_Mux_true(n);
1513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1515 /* Mux(false, f, t) == f */
1516 else if (ts == tarval_b_false) {
1517 n = get_Mux_false(n);
1518 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1520 /* Mux(v, x, x) == x */
1521 else if (get_Mux_false(n) == get_Mux_true(n)) {
1522 n = get_Mux_true(n);
1523 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1525 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1526 ir_node *cmp = get_Proj_pred(sel);
1527 long proj_nr = get_Proj_proj(sel);
1528 ir_node *f = get_Mux_false(n);
1529 ir_node *t = get_Mux_true(n);
1532 * Note further that these optimization work even for floating point
1533 * with NaN's because -NaN == NaN.
1534 * However, if +0 and -0 is handled differently, we cannot use the first one.
1537 ir_node *const cmp_l = get_Cmp_left(cmp);
1538 ir_node *const cmp_r = get_Cmp_right(cmp);
1542 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1543 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1545 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1552 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1553 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1555 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1562 * Note: normalization puts the constant on the right side,
1563 * so we check only one case.
1565 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1566 /* Mux(t CMP 0, X, t) */
1567 if (is_Minus(f) && get_Minus_op(f) == t) {
1568 /* Mux(t CMP 0, -t, t) */
1569 if (proj_nr == pn_Cmp_Eq) {
1570 /* Mux(t == 0, -t, t) ==> -t */
1572 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1573 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1574 /* Mux(t != 0, -t, t) ==> t */
1576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1583 } /* equivalent_node_Mux */
1586 * Returns a equivalent node of a Psi: if a condition is true
1587 * and all previous conditions are false we know its value.
1588 * If all conditions are false its value is the default one.
1590 static ir_node *equivalent_node_Psi(ir_node *n) {
1592 return equivalent_node_Mux(n);
1594 } /* equivalent_node_Psi */
1597 * Optimize -a CMP -b into b CMP a.
1598 * This works only for for modes where unary Minus
1600 * Note that two-complement integers can Overflow
1601 * so it will NOT work.
1603 * For == and != can be handled in Proj(Cmp)
1605 static ir_node *equivalent_node_Cmp(ir_node *n) {
1606 ir_node *left = get_Cmp_left(n);
1607 ir_node *right = get_Cmp_right(n);
1609 if (is_Minus(left) && is_Minus(right) &&
1610 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1611 left = get_Minus_op(left);
1612 right = get_Minus_op(right);
1613 set_Cmp_left(n, right);
1614 set_Cmp_right(n, left);
1617 } /* equivalent_node_Cmp */
1620 * Remove Confirm nodes if setting is on.
1621 * Replace Confirms(x, '=', Constlike) by Constlike.
1623 static ir_node *equivalent_node_Confirm(ir_node *n) {
1624 ir_node *pred = get_Confirm_value(n);
1625 pn_Cmp pnc = get_Confirm_cmp(n);
1627 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1629 * rare case: two identical Confirms one after another,
1630 * replace the second one with the first.
1634 if (get_opt_remove_confirm())
1635 return get_Confirm_value(n);
1640 * Optimize CopyB(mem, x, x) into a Nop.
1642 static ir_node *equivalent_node_CopyB(ir_node *n) {
1643 ir_node *a = get_CopyB_dst(n);
1644 ir_node *b = get_CopyB_src(n);
1647 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1648 ir_node *mem = get_CopyB_mem(n);
1649 ir_node *blk = get_nodes_block(n);
1650 turn_into_tuple(n, pn_CopyB_max);
1651 set_Tuple_pred(n, pn_CopyB_M, mem);
1652 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1653 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1654 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1657 } /* equivalent_node_CopyB */
1660 * Optimize Bounds(idx, idx, upper) into idx.
1662 static ir_node *equivalent_node_Bound(ir_node *n) {
1663 ir_node *idx = get_Bound_index(n);
1664 ir_node *pred = skip_Proj(idx);
1667 if (is_Bound(pred)) {
1669 * idx was Bounds checked in the same MacroBlock previously,
1670 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1672 ir_node *lower = get_Bound_lower(n);
1673 ir_node *upper = get_Bound_upper(n);
1674 if (get_Bound_lower(pred) == lower &&
1675 get_Bound_upper(pred) == upper &&
1676 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1678 * One could expect that we simply return the previous
1679 * Bound here. However, this would be wrong, as we could
1680 * add an exception Proj to a new location then.
1681 * So, we must turn in into a tuple.
1687 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1688 ir_node *mem = get_Bound_mem(n);
1689 ir_node *blk = get_nodes_block(n);
1690 turn_into_tuple(n, pn_Bound_max);
1691 set_Tuple_pred(n, pn_Bound_M, mem);
1692 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1693 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1694 set_Tuple_pred(n, pn_Bound_res, idx);
1697 } /* equivalent_node_Bound */
1700 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1701 * perform no actual computation, as, e.g., the Id nodes. It does not create
1702 * new nodes. It is therefore safe to free n if the node returned is not n.
1703 * If a node returns a Tuple we can not just skip it. If the size of the
1704 * in array fits, we transform n into a tuple (e.g., Div).
1706 ir_node *equivalent_node(ir_node *n) {
1707 if (n->op->ops.equivalent_node)
1708 return n->op->ops.equivalent_node(n);
1710 } /* equivalent_node */
1713 * Sets the default equivalent node operation for an ir_op_ops.
1715 * @param code the opcode for the default operation
1716 * @param ops the operations initialized
1721 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1725 ops->equivalent_node = equivalent_node_##a; \
1765 } /* firm_set_default_equivalent_node */
1768 * Returns non-zero if a node is a Phi node
1769 * with all predecessors constant.
1771 static int is_const_Phi(ir_node *n) {
1774 if (! is_Phi(n) || get_irn_arity(n) == 0)
1776 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1777 if (! is_Const(get_irn_n(n, i)))
1780 } /* is_const_Phi */
1783 * Apply an evaluator on a binop with a constant operators (and one Phi).
1785 * @param phi the Phi node
1786 * @param other the other operand
1787 * @param eval an evaluator function
1788 * @param mode the mode of the result, may be different from the mode of the Phi!
1789 * @param left if non-zero, other is the left operand, else the right
1791 * @return a new Phi node if the conversion was successful, NULL else
1793 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1798 int i, n = get_irn_arity(phi);
1800 NEW_ARR_A(void *, res, n);
1802 for (i = 0; i < n; ++i) {
1803 pred = get_irn_n(phi, i);
1804 tv = get_Const_tarval(pred);
1805 tv = eval(other, tv);
1807 if (tv == tarval_bad) {
1808 /* folding failed, bad */
1814 for (i = 0; i < n; ++i) {
1815 pred = get_irn_n(phi, i);
1816 tv = get_Const_tarval(pred);
1817 tv = eval(tv, other);
1819 if (tv == tarval_bad) {
1820 /* folding failed, bad */
1826 irg = current_ir_graph;
1827 for (i = 0; i < n; ++i) {
1828 pred = get_irn_n(phi, i);
1829 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1830 mode, res[i], get_Const_type(pred));
1832 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1833 } /* apply_binop_on_phi */
1836 * Apply an evaluator on a binop with two constant Phi.
1838 * @param a the left Phi node
1839 * @param b the right Phi node
1840 * @param eval an evaluator function
1841 * @param mode the mode of the result, may be different from the mode of the Phi!
1843 * @return a new Phi node if the conversion was successful, NULL else
1845 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1846 tarval *tv_l, *tv_r, *tv;
1852 if (get_nodes_block(a) != get_nodes_block(b))
1855 n = get_irn_arity(a);
1856 NEW_ARR_A(void *, res, n);
1858 for (i = 0; i < n; ++i) {
1859 pred = get_irn_n(a, i);
1860 tv_l = get_Const_tarval(pred);
1861 pred = get_irn_n(b, i);
1862 tv_r = get_Const_tarval(pred);
1863 tv = eval(tv_l, tv_r);
1865 if (tv == tarval_bad) {
1866 /* folding failed, bad */
1871 irg = current_ir_graph;
1872 for (i = 0; i < n; ++i) {
1873 pred = get_irn_n(a, i);
1874 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1876 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1877 } /* apply_binop_on_2_phis */
1880 * Apply an evaluator on a unop with a constant operator (a Phi).
1882 * @param phi the Phi node
1883 * @param eval an evaluator function
1885 * @return a new Phi node if the conversion was successful, NULL else
1887 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1893 int i, n = get_irn_arity(phi);
1895 NEW_ARR_A(void *, res, n);
1896 for (i = 0; i < n; ++i) {
1897 pred = get_irn_n(phi, i);
1898 tv = get_Const_tarval(pred);
1901 if (tv == tarval_bad) {
1902 /* folding failed, bad */
1907 mode = get_irn_mode(phi);
1908 irg = current_ir_graph;
1909 for (i = 0; i < n; ++i) {
1910 pred = get_irn_n(phi, i);
1911 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1912 mode, res[i], get_Const_type(pred));
1914 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1915 } /* apply_unop_on_phi */
1918 * Apply a conversion on a constant operator (a Phi).
1920 * @param phi the Phi node
1922 * @return a new Phi node if the conversion was successful, NULL else
1924 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1929 int i, n = get_irn_arity(phi);
1931 NEW_ARR_A(void *, res, n);
1932 for (i = 0; i < n; ++i) {
1933 pred = get_irn_n(phi, i);
1934 tv = get_Const_tarval(pred);
1935 tv = tarval_convert_to(tv, mode);
1937 if (tv == tarval_bad) {
1938 /* folding failed, bad */
1943 irg = current_ir_graph;
1944 for (i = 0; i < n; ++i) {
1945 pred = get_irn_n(phi, i);
1946 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1947 mode, res[i], get_Const_type(pred));
1949 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1950 } /* apply_conv_on_phi */
1953 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1954 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1955 * If possible, remove the Conv's.
1957 static ir_node *transform_node_AddSub(ir_node *n) {
1958 ir_mode *mode = get_irn_mode(n);
1960 if (mode_is_reference(mode)) {
1961 ir_node *left = get_binop_left(n);
1962 ir_node *right = get_binop_right(n);
1963 unsigned ref_bits = get_mode_size_bits(mode);
1965 if (is_Conv(left)) {
1966 ir_mode *lmode = get_irn_mode(left);
1967 unsigned bits = get_mode_size_bits(lmode);
1969 if (ref_bits == bits &&
1970 mode_is_int(lmode) &&
1971 get_mode_arithmetic(lmode) == irma_twos_complement) {
1972 ir_node *pre = get_Conv_op(left);
1973 ir_mode *pre_mode = get_irn_mode(pre);
1975 if (mode_is_int(pre_mode) &&
1976 get_mode_size_bits(pre_mode) == bits &&
1977 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1978 /* ok, this conv just changes to sign, moreover the calculation
1979 * is done with same number of bits as our address mode, so
1980 * we can ignore the conv as address calculation can be viewed
1981 * as either signed or unsigned
1983 set_binop_left(n, pre);
1988 if (is_Conv(right)) {
1989 ir_mode *rmode = get_irn_mode(right);
1990 unsigned bits = get_mode_size_bits(rmode);
1992 if (ref_bits == bits &&
1993 mode_is_int(rmode) &&
1994 get_mode_arithmetic(rmode) == irma_twos_complement) {
1995 ir_node *pre = get_Conv_op(right);
1996 ir_mode *pre_mode = get_irn_mode(pre);
1998 if (mode_is_int(pre_mode) &&
1999 get_mode_size_bits(pre_mode) == bits &&
2000 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2001 /* ok, this conv just changes to sign, moreover the calculation
2002 * is done with same number of bits as our address mode, so
2003 * we can ignore the conv as address calculation can be viewed
2004 * as either signed or unsigned
2006 set_binop_right(n, pre);
2011 /* let address arithmetic use unsigned modes */
2012 if (is_Const(right)) {
2013 ir_mode *rmode = get_irn_mode(right);
2015 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2016 /* convert a AddP(P, *s) into AddP(P, *u) */
2017 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2019 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2020 set_binop_right(n, pre);
2026 } /* transform_node_AddSub */
2028 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2030 if (is_Const(b) && is_const_Phi(a)) { \
2031 /* check for Op(Phi, Const) */ \
2032 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2034 else if (is_Const(a) && is_const_Phi(b)) { \
2035 /* check for Op(Const, Phi) */ \
2036 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2038 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2039 /* check for Op(Phi, Phi) */ \
2040 c = apply_binop_on_2_phis(a, b, eval, mode); \
2043 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2047 #define HANDLE_UNOP_PHI(eval, a, c) \
2049 if (is_const_Phi(a)) { \
2050 /* check for Op(Phi) */ \
2051 c = apply_unop_on_phi(a, eval); \
2053 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2059 * Do the AddSub optimization, then Transform
2060 * Constant folding on Phi
2061 * Add(a,a) -> Mul(a, 2)
2062 * Add(Mul(a, x), a) -> Mul(a, x+1)
2063 * if the mode is integer or float.
2064 * Transform Add(a,-b) into Sub(a,b).
2065 * Reassociation might fold this further.
2067 static ir_node *transform_node_Add(ir_node *n) {
2069 ir_node *a, *b, *c, *oldn = n;
2071 n = transform_node_AddSub(n);
2073 a = get_Add_left(n);
2074 b = get_Add_right(n);
2076 mode = get_irn_mode(n);
2078 if (mode_is_reference(mode)) {
2079 ir_mode *lmode = get_irn_mode(a);
2081 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2082 /* an Add(a, NULL) is a hidden Conv */
2083 dbg_info *dbg = get_irn_dbg_info(n);
2084 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2088 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2090 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2091 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2094 if (mode_is_num(mode)) {
2095 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2096 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2097 ir_node *block = get_nodes_block(n);
2100 get_irn_dbg_info(n),
2104 new_r_Const_long(current_ir_graph, block, mode, 2),
2106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2111 get_irn_dbg_info(n),
2117 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2122 get_irn_dbg_info(n),
2128 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2131 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2132 /* Here we rely on constants be on the RIGHT side */
2134 ir_node *op = get_Not_op(a);
2136 if (is_Const(b) && is_Const_one(b)) {
2138 ir_node *blk = get_irn_n(n, -1);
2139 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2140 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2145 ir_node *blk = get_irn_n(n, -1);
2146 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2147 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2152 ir_node *op = get_Not_op(b);
2156 ir_node *blk = get_irn_n(n, -1);
2157 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2158 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2165 } /* transform_node_Add */
2168 * returns -cnst or NULL if impossible
2170 static ir_node *const_negate(ir_node *cnst) {
2171 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2172 dbg_info *dbgi = get_irn_dbg_info(cnst);
2173 ir_graph *irg = get_irn_irg(cnst);
2174 ir_node *block = get_nodes_block(cnst);
2175 ir_mode *mode = get_irn_mode(cnst);
2176 if (tv == tarval_bad) return NULL;
2177 return new_rd_Const(dbgi, irg, block, mode, tv);
2181 * Do the AddSub optimization, then Transform
2182 * Constant folding on Phi
2183 * Sub(0,a) -> Minus(a)
2184 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2185 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2186 * Sub(Add(a, x), x) -> a
2187 * Sub(x, Add(x, a)) -> -a
2188 * Sub(x, Const) -> Add(x, -Const)
2190 static ir_node *transform_node_Sub(ir_node *n) {
2195 n = transform_node_AddSub(n);
2197 a = get_Sub_left(n);
2198 b = get_Sub_right(n);
2200 mode = get_irn_mode(n);
2202 if (mode_is_int(mode)) {
2203 ir_mode *lmode = get_irn_mode(a);
2205 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2206 /* a Sub(a, NULL) is a hidden Conv */
2207 dbg_info *dbg = get_irn_dbg_info(n);
2208 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2213 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2215 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2216 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2219 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2220 /* a - C -> a + (-C) */
2221 ir_node *cnst = const_negate(b);
2223 ir_node *block = get_nodes_block(n);
2224 dbg_info *dbgi = get_irn_dbg_info(n);
2225 ir_graph *irg = get_irn_irg(n);
2227 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2228 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2233 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2234 ir_graph *irg = current_ir_graph;
2235 dbg_info *dbg = get_irn_dbg_info(n);
2236 ir_node *block = get_nodes_block(n);
2237 ir_node *left = get_Minus_op(a);
2238 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2240 n = new_rd_Minus(dbg, irg, block, add, mode);
2241 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2243 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2244 ir_graph *irg = current_ir_graph;
2245 dbg_info *dbg = get_irn_dbg_info(n);
2246 ir_node *block = get_nodes_block(n);
2247 ir_node *right = get_Minus_op(b);
2249 n = new_rd_Add(dbg, irg, block, a, right, mode);
2250 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2252 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2253 ir_graph *irg = current_ir_graph;
2254 dbg_info *s_dbg = get_irn_dbg_info(b);
2255 ir_node *s_block = get_nodes_block(b);
2256 ir_node *s_left = get_Sub_right(b);
2257 ir_node *s_right = get_Sub_left(b);
2258 ir_mode *s_mode = get_irn_mode(b);
2259 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2260 dbg_info *a_dbg = get_irn_dbg_info(n);
2261 ir_node *a_block = get_nodes_block(n);
2263 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2264 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2266 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2267 ir_node *m_right = get_Mul_right(b);
2268 if (is_Const(m_right)) {
2269 ir_node *cnst2 = const_negate(m_right);
2270 if (cnst2 != NULL) {
2271 ir_graph *irg = current_ir_graph;
2272 dbg_info *m_dbg = get_irn_dbg_info(b);
2273 ir_node *m_block = get_nodes_block(b);
2274 ir_node *m_left = get_Mul_left(b);
2275 ir_mode *m_mode = get_irn_mode(b);
2276 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2277 dbg_info *a_dbg = get_irn_dbg_info(n);
2278 ir_node *a_block = get_nodes_block(n);
2280 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2287 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2288 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2290 get_irn_dbg_info(n),
2295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2299 if (mode_wrap_around(mode)) {
2300 ir_node *left = get_Add_left(a);
2301 ir_node *right = get_Add_right(a);
2303 /* FIXME: Does the Conv's work only for two complement or generally? */
2305 if (mode != get_irn_mode(right)) {
2306 /* This Sub is an effective Cast */
2307 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2310 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2312 } else if (right == b) {
2313 if (mode != get_irn_mode(left)) {
2314 /* This Sub is an effective Cast */
2315 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2318 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2324 if (mode_wrap_around(mode)) {
2325 ir_node *left = get_Add_left(b);
2326 ir_node *right = get_Add_right(b);
2328 /* FIXME: Does the Conv's work only for two complement or generally? */
2330 ir_mode *r_mode = get_irn_mode(right);
2332 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2333 if (mode != r_mode) {
2334 /* This Sub is an effective Cast */
2335 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2339 } else if (right == a) {
2340 ir_mode *l_mode = get_irn_mode(left);
2342 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2343 if (mode != l_mode) {
2344 /* This Sub is an effective Cast */
2345 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2347 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2352 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2353 ir_mode *mode = get_irn_mode(a);
2355 if (mode == get_irn_mode(b)) {
2357 ir_node *op_a = get_Conv_op(a);
2358 ir_node *op_b = get_Conv_op(b);
2360 /* check if it's allowed to skip the conv */
2361 ma = get_irn_mode(op_a);
2362 mb = get_irn_mode(op_b);
2364 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2365 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2368 set_Sub_right(n, b);
2374 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2375 if (!is_reassoc_running() && is_Mul(a)) {
2376 ir_node *ma = get_Mul_left(a);
2377 ir_node *mb = get_Mul_right(a);
2380 ir_node *blk = get_irn_n(n, -1);
2382 get_irn_dbg_info(n),
2383 current_ir_graph, blk,
2386 get_irn_dbg_info(n),
2387 current_ir_graph, blk,
2389 new_r_Const_long(current_ir_graph, blk, mode, 1),
2392 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2394 } else if (mb == b) {
2395 ir_node *blk = get_irn_n(n, -1);
2397 get_irn_dbg_info(n),
2398 current_ir_graph, blk,
2401 get_irn_dbg_info(n),
2402 current_ir_graph, blk,
2404 new_r_Const_long(current_ir_graph, blk, mode, 1),
2407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2411 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2412 ir_node *x = get_Sub_left(a);
2413 ir_node *y = get_Sub_right(a);
2414 ir_node *blk = get_irn_n(n, -1);
2415 ir_mode *m_b = get_irn_mode(b);
2416 ir_mode *m_y = get_irn_mode(y);
2420 /* Determine the right mode for the Add. */
2423 else if (mode_is_reference(m_b))
2425 else if (mode_is_reference(m_y))
2429 * Both modes are different but none is reference,
2430 * happens for instance in SubP(SubP(P, Iu), Is).
2431 * We have two possibilities here: Cast or ignore.
2432 * Currently we ignore this case.
2437 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2439 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2440 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2444 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2445 if (is_Const(a) && is_Not(b)) {
2446 /* c - ~X = X + (c+1) */
2447 tarval *tv = get_Const_tarval(a);
2449 tv = tarval_add(tv, get_mode_one(mode));
2450 if (tv != tarval_bad) {
2451 ir_node *blk = get_irn_n(n, -1);
2452 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2453 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2454 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2460 } /* transform_node_Sub */
2463 * Several transformation done on n*n=2n bits mul.
2464 * These transformations must be done here because new nodes may be produced.
2466 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2468 ir_node *a = get_Mul_left(n);
2469 ir_node *b = get_Mul_right(n);
2470 tarval *ta = value_of(a);
2471 tarval *tb = value_of(b);
2472 ir_mode *smode = get_irn_mode(a);
2474 if (ta == get_mode_one(smode)) {
2475 /* (L)1 * (L)b = (L)b */
2476 ir_node *blk = get_irn_n(n, -1);
2477 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2481 else if (ta == get_mode_minus_one(smode)) {
2482 /* (L)-1 * (L)b = (L)b */
2483 ir_node *blk = get_irn_n(n, -1);
2484 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2485 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2486 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2489 if (tb == get_mode_one(smode)) {
2490 /* (L)a * (L)1 = (L)a */
2491 ir_node *blk = get_irn_n(a, -1);
2492 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2493 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2496 else if (tb == get_mode_minus_one(smode)) {
2497 /* (L)a * (L)-1 = (L)-a */
2498 ir_node *blk = get_irn_n(n, -1);
2499 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2500 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2501 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2508 * Transform Mul(a,-1) into -a.
2509 * Do constant evaluation of Phi nodes.
2510 * Do architecture dependent optimizations on Mul nodes
2512 static ir_node *transform_node_Mul(ir_node *n) {
2513 ir_node *c, *oldn = n;
2514 ir_mode *mode = get_irn_mode(n);
2515 ir_node *a = get_Mul_left(n);
2516 ir_node *b = get_Mul_right(n);
2518 if (is_Bad(a) || is_Bad(b))
2521 if (mode != get_irn_mode(a))
2522 return transform_node_Mul2n(n, mode);
2524 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2526 if (mode_is_signed(mode)) {
2529 if (value_of(a) == get_mode_minus_one(mode))
2531 else if (value_of(b) == get_mode_minus_one(mode))
2534 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2535 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2540 if (is_Const(b)) { /* (-a) * const -> a * -const */
2541 ir_node *cnst = const_negate(b);
2543 dbg_info *dbgi = get_irn_dbg_info(n);
2544 ir_node *block = get_nodes_block(n);
2545 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2546 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2549 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2550 dbg_info *dbgi = get_irn_dbg_info(n);
2551 ir_node *block = get_nodes_block(n);
2552 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2553 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2555 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2556 ir_node *sub_l = get_Sub_left(b);
2557 ir_node *sub_r = get_Sub_right(b);
2558 dbg_info *dbgi = get_irn_dbg_info(n);
2559 ir_graph *irg = current_ir_graph;
2560 ir_node *block = get_nodes_block(n);
2561 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2562 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2563 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2566 } else if (is_Minus(b)) {
2567 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2568 ir_node *sub_l = get_Sub_left(a);
2569 ir_node *sub_r = get_Sub_right(a);
2570 dbg_info *dbgi = get_irn_dbg_info(n);
2571 ir_graph *irg = current_ir_graph;
2572 ir_node *block = get_nodes_block(n);
2573 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2574 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2575 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2579 if (get_mode_arithmetic(mode) == irma_ieee754) {
2581 tarval *tv = get_Const_tarval(a);
2582 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2583 /* 2.0 * b = b + b */
2584 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2585 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2589 else if (is_Const(b)) {
2590 tarval *tv = get_Const_tarval(b);
2591 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2592 /* a * 2.0 = a + a */
2593 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2594 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2599 return arch_dep_replace_mul_with_shifts(n);
2600 } /* transform_node_Mul */
2603 * Transform a Div Node.
2605 static ir_node *transform_node_Div(ir_node *n) {
2606 ir_mode *mode = get_Div_resmode(n);
2607 ir_node *a = get_Div_left(n);
2608 ir_node *b = get_Div_right(n);
2612 if (is_Const(b) && is_const_Phi(a)) {
2613 /* check for Div(Phi, Const) */
2614 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2616 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2620 else if (is_Const(a) && is_const_Phi(b)) {
2621 /* check for Div(Const, Phi) */
2622 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2624 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2628 else if (is_const_Phi(a) && is_const_Phi(b)) {
2629 /* check for Div(Phi, Phi) */
2630 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2632 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2639 if (tv != tarval_bad) {
2640 value = new_Const(get_tarval_mode(tv), tv);
2642 DBG_OPT_CSTEVAL(n, value);
2645 ir_node *a = get_Div_left(n);
2646 ir_node *b = get_Div_right(n);
2649 if (a == b && value_not_zero(a, &dummy)) {
2650 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2651 value = new_Const(mode, get_mode_one(mode));
2652 DBG_OPT_CSTEVAL(n, value);
2655 if (mode_is_signed(mode) && is_Const(b)) {
2656 tarval *tv = get_Const_tarval(b);
2658 if (tv == get_mode_minus_one(mode)) {
2660 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2661 DBG_OPT_CSTEVAL(n, value);
2665 /* Try architecture dependent optimization */
2666 value = arch_dep_replace_div_by_const(n);
2674 /* Turn Div into a tuple (mem, jmp, bad, value) */
2675 mem = get_Div_mem(n);
2676 blk = get_irn_n(n, -1);
2678 /* skip a potential Pin */
2680 mem = get_Pin_op(mem);
2681 turn_into_tuple(n, pn_Div_max);
2682 set_Tuple_pred(n, pn_Div_M, mem);
2683 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2684 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2685 set_Tuple_pred(n, pn_Div_res, value);
2688 } /* transform_node_Div */
2691 * Transform a Mod node.
2693 static ir_node *transform_node_Mod(ir_node *n) {
2694 ir_mode *mode = get_Mod_resmode(n);
2695 ir_node *a = get_Mod_left(n);
2696 ir_node *b = get_Mod_right(n);
2700 if (is_Const(b) && is_const_Phi(a)) {
2701 /* check for Div(Phi, Const) */
2702 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2704 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2708 else if (is_Const(a) && is_const_Phi(b)) {
2709 /* check for Div(Const, Phi) */
2710 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2712 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2716 else if (is_const_Phi(a) && is_const_Phi(b)) {
2717 /* check for Div(Phi, Phi) */
2718 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2720 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2727 if (tv != tarval_bad) {
2728 value = new_Const(get_tarval_mode(tv), tv);
2730 DBG_OPT_CSTEVAL(n, value);
2733 ir_node *a = get_Mod_left(n);
2734 ir_node *b = get_Mod_right(n);
2737 if (a == b && value_not_zero(a, &dummy)) {
2738 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2739 value = new_Const(mode, get_mode_null(mode));
2740 DBG_OPT_CSTEVAL(n, value);
2743 if (mode_is_signed(mode) && is_Const(b)) {
2744 tarval *tv = get_Const_tarval(b);
2746 if (tv == get_mode_minus_one(mode)) {
2748 value = new_Const(mode, get_mode_null(mode));
2749 DBG_OPT_CSTEVAL(n, value);
2753 /* Try architecture dependent optimization */
2754 value = arch_dep_replace_mod_by_const(n);
2762 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2763 mem = get_Mod_mem(n);
2764 blk = get_irn_n(n, -1);
2766 /* skip a potential Pin */
2768 mem = get_Pin_op(mem);
2769 turn_into_tuple(n, pn_Mod_max);
2770 set_Tuple_pred(n, pn_Mod_M, mem);
2771 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2772 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2773 set_Tuple_pred(n, pn_Mod_res, value);
2776 } /* transform_node_Mod */
2779 * Transform a DivMod node.
2781 static ir_node *transform_node_DivMod(ir_node *n) {
2783 ir_node *a = get_DivMod_left(n);
2784 ir_node *b = get_DivMod_right(n);
2785 ir_mode *mode = get_DivMod_resmode(n);
2790 if (is_Const(b) && is_const_Phi(a)) {
2791 /* check for Div(Phi, Const) */
2792 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2793 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2795 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2796 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2800 else if (is_Const(a) && is_const_Phi(b)) {
2801 /* check for Div(Const, Phi) */
2802 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2803 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2805 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2806 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2810 else if (is_const_Phi(a) && is_const_Phi(b)) {
2811 /* check for Div(Phi, Phi) */
2812 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2813 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2815 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2816 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2823 if (tb != tarval_bad) {
2824 if (tb == get_mode_one(get_tarval_mode(tb))) {
2826 vb = new_Const(mode, get_mode_null(mode));
2827 DBG_OPT_CSTEVAL(n, vb);
2829 } else if (ta != tarval_bad) {
2830 tarval *resa, *resb;
2831 resa = tarval_div(ta, tb);
2832 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2833 Jmp for X result!? */
2834 resb = tarval_mod(ta, tb);
2835 if (resb == tarval_bad) return n; /* Causes exception! */
2836 va = new_Const(mode, resa);
2837 vb = new_Const(mode, resb);
2838 DBG_OPT_CSTEVAL(n, va);
2839 DBG_OPT_CSTEVAL(n, vb);
2841 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2842 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2843 vb = new_Const(mode, get_mode_null(mode));
2844 DBG_OPT_CSTEVAL(n, va);
2845 DBG_OPT_CSTEVAL(n, vb);
2847 } else { /* Try architecture dependent optimization */
2850 arch_dep_replace_divmod_by_const(&va, &vb, n);
2851 evaluated = va != NULL;
2853 } else if (a == b) {
2854 if (value_not_zero(a, &dummy)) {
2856 va = new_Const(mode, get_mode_one(mode));
2857 vb = new_Const(mode, get_mode_null(mode));
2858 DBG_OPT_CSTEVAL(n, va);
2859 DBG_OPT_CSTEVAL(n, vb);
2862 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2865 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2866 /* 0 / non-Const = 0 */
2871 if (evaluated) { /* replace by tuple */
2875 mem = get_DivMod_mem(n);
2876 /* skip a potential Pin */
2878 mem = get_Pin_op(mem);
2880 blk = get_irn_n(n, -1);
2881 turn_into_tuple(n, pn_DivMod_max);
2882 set_Tuple_pred(n, pn_DivMod_M, mem);
2883 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2884 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2885 set_Tuple_pred(n, pn_DivMod_res_div, va);
2886 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2890 } /* transform_node_DivMod */
2893 * Optimize x / c to x * (1/c)
2895 static ir_node *transform_node_Quot(ir_node *n) {
2896 ir_mode *mode = get_Quot_resmode(n);
2899 if (get_mode_arithmetic(mode) == irma_ieee754) {
2900 ir_node *b = get_Quot_right(n);
2903 tarval *tv = get_Const_tarval(b);
2907 * Floating point constant folding might be disabled here to
2909 * However, as we check for exact result, doing it is safe.
2912 rem = tarval_enable_fp_ops(1);
2913 tv = tarval_quo(get_mode_one(mode), tv);
2914 (void)tarval_enable_fp_ops(rem);
2916 /* Do the transformation if the result is either exact or we are not
2917 using strict rules. */
2918 if (tv != tarval_bad &&
2919 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2920 ir_node *blk = get_irn_n(n, -1);
2921 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2922 ir_node *a = get_Quot_left(n);
2923 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2924 ir_node *mem = get_Quot_mem(n);
2926 /* skip a potential Pin */
2928 mem = get_Pin_op(mem);
2929 turn_into_tuple(n, pn_Quot_max);
2930 set_Tuple_pred(n, pn_Quot_M, mem);
2931 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2932 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2933 set_Tuple_pred(n, pn_Quot_res, m);
2934 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2939 } /* transform_node_Quot */
2942 * Optimize Abs(x) into x if x is Confirmed >= 0
2943 * Optimize Abs(x) into -x if x is Confirmed <= 0
2944 * Optimize Abs(-x) int Abs(x)
2946 static ir_node *transform_node_Abs(ir_node *n) {
2947 ir_node *c, *oldn = n;
2948 ir_node *a = get_Abs_op(n);
2951 HANDLE_UNOP_PHI(tarval_abs, a, c);
2953 switch (classify_value_sign(a)) {
2954 case value_classified_negative:
2955 mode = get_irn_mode(n);
2958 * We can replace the Abs by -x here.
2959 * We even could add a new Confirm here
2960 * (if not twos complement)
2962 * Note that -x would create a new node, so we could
2963 * not run it in the equivalent_node() context.
2965 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2966 get_nodes_block(n), a, mode);
2968 DBG_OPT_CONFIRM(oldn, n);
2970 case value_classified_positive:
2971 /* n is positive, Abs is not needed */
2974 DBG_OPT_CONFIRM(oldn, n);
2980 /* Abs(-x) = Abs(x) */
2981 mode = get_irn_mode(n);
2982 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2983 get_nodes_block(n), get_Minus_op(a), mode);
2984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2988 } /* transform_node_Abs */
2991 * Transform a Cond node.
2993 * Replace the Cond by a Jmp if it branches on a constant
2996 static ir_node *transform_node_Cond(ir_node *n) {
2999 ir_node *a = get_Cond_selector(n);
3000 tarval *ta = value_of(a);
3002 /* we need block info which is not available in floating irgs */
3003 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3006 if ((ta != tarval_bad) &&
3007 (get_irn_mode(a) == mode_b) &&
3008 (get_opt_unreachable_code())) {
3009 /* It's a boolean Cond, branching on a boolean constant.
3010 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3011 ir_node *blk = get_nodes_block(n);
3012 jmp = new_r_Jmp(current_ir_graph, blk);
3013 turn_into_tuple(n, pn_Cond_max);
3014 if (ta == tarval_b_true) {
3015 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3016 set_Tuple_pred(n, pn_Cond_true, jmp);
3018 set_Tuple_pred(n, pn_Cond_false, jmp);
3019 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3021 /* We might generate an endless loop, so keep it alive. */
3022 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3025 } /* transform_node_Cond */
3028 * Prototype of a recursive transform function
3029 * for bitwise distributive transformations.
3031 typedef ir_node* (*recursive_transform)(ir_node *n);
3034 * makes use of distributive laws for and, or, eor
3035 * and(a OP c, b OP c) -> and(a, b) OP c
3036 * note, might return a different op than n
3038 static ir_node *transform_bitwise_distributive(ir_node *n,
3039 recursive_transform trans_func)
3042 ir_node *a = get_binop_left(n);
3043 ir_node *b = get_binop_right(n);
3044 ir_op *op = get_irn_op(a);
3045 ir_op *op_root = get_irn_op(n);
3047 if(op != get_irn_op(b))
3050 if (op == op_Conv) {
3051 ir_node *a_op = get_Conv_op(a);
3052 ir_node *b_op = get_Conv_op(b);
3053 ir_mode *a_mode = get_irn_mode(a_op);
3054 ir_mode *b_mode = get_irn_mode(b_op);
3055 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3056 ir_node *blk = get_irn_n(n, -1);
3059 set_binop_left(n, a_op);
3060 set_binop_right(n, b_op);
3061 set_irn_mode(n, a_mode);
3063 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3065 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3071 /* nothing to gain here */
3075 if (op == op_Shrs || op == op_Shr || op == op_Shl
3076 || op == op_And || op == op_Or || op == op_Eor) {
3077 ir_node *a_left = get_binop_left(a);
3078 ir_node *a_right = get_binop_right(a);
3079 ir_node *b_left = get_binop_left(b);
3080 ir_node *b_right = get_binop_right(b);
3082 ir_node *op1 = NULL;
3083 ir_node *op2 = NULL;
3085 if (is_op_commutative(op)) {
3086 if (a_left == b_left) {
3090 } else if(a_left == b_right) {
3094 } else if(a_right == b_left) {
3100 if(a_right == b_right) {
3107 /* (a sop c) & (b sop c) => (a & b) sop c */
3108 ir_node *blk = get_irn_n(n, -1);
3110 ir_node *new_n = exact_copy(n);
3111 set_binop_left(new_n, op1);
3112 set_binop_right(new_n, op2);
3113 new_n = trans_func(new_n);
3115 if(op_root == op_Eor && op == op_Or) {
3116 dbg_info *dbgi = get_irn_dbg_info(n);
3117 ir_graph *irg = current_ir_graph;
3118 ir_mode *mode = get_irn_mode(c);
3120 c = new_rd_Not(dbgi, irg, blk, c, mode);
3121 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3124 set_nodes_block(n, blk);
3125 set_binop_left(n, new_n);
3126 set_binop_right(n, c);
3127 add_identities(current_ir_graph->value_table, n);
3130 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3141 static ir_node *transform_node_And(ir_node *n) {
3142 ir_node *c, *oldn = n;
3143 ir_node *a = get_And_left(n);
3144 ir_node *b = get_And_right(n);
3147 mode = get_irn_mode(n);
3148 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3150 /* we can evaluate 2 Projs of the same Cmp */
3151 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3152 ir_node *pred_a = get_Proj_pred(a);
3153 ir_node *pred_b = get_Proj_pred(b);
3154 if (pred_a == pred_b) {
3155 dbg_info *dbgi = get_irn_dbg_info(n);
3156 ir_node *block = get_nodes_block(pred_a);
3157 pn_Cmp pn_a = get_Proj_proj(a);
3158 pn_Cmp pn_b = get_Proj_proj(b);
3159 /* yes, we can simply calculate with pncs */
3160 pn_Cmp new_pnc = pn_a & pn_b;
3162 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3167 ir_node *op = get_Not_op(b);
3169 ir_node *ba = get_And_left(op);
3170 ir_node *bb = get_And_right(op);
3172 /* it's enough to test the following cases due to normalization! */
3173 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3174 /* (a|b) & ~(a&b) = a^b */
3175 ir_node *block = get_nodes_block(n);
3177 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3178 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3186 ir_node *op = get_Not_op(a);
3188 ir_node *aa = get_And_left(op);
3189 ir_node *ab = get_And_right(op);
3191 /* it's enough to test the following cases due to normalization! */
3192 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3193 /* (a|b) & ~(a&b) = a^b */
3194 ir_node *block = get_nodes_block(n);
3196 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3197 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3204 ir_node *al = get_Eor_left(a);
3205 ir_node *ar = get_Eor_right(a);
3208 /* (b ^ a) & b -> ~a & b */
3209 dbg_info *dbg = get_irn_dbg_info(n);
3210 ir_node *block = get_nodes_block(n);
3212 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3213 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3214 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3218 /* (a ^ b) & b -> ~a & b */
3219 dbg_info *dbg = get_irn_dbg_info(n);
3220 ir_node *block = get_nodes_block(n);
3222 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3223 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3224 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3229 ir_node *bl = get_Eor_left(b);
3230 ir_node *br = get_Eor_right(b);
3233 /* a & (a ^ b) -> a & ~b */
3234 dbg_info *dbg = get_irn_dbg_info(n);
3235 ir_node *block = get_nodes_block(n);
3237 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3238 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3239 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3243 /* a & (b ^ a) -> a & ~b */
3244 dbg_info *dbg = get_irn_dbg_info(n);
3245 ir_node *block = get_nodes_block(n);
3247 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3248 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3249 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3253 if (is_Not(a) && is_Not(b)) {
3254 /* ~a & ~b = ~(a|b) */
3255 ir_node *block = get_nodes_block(n);
3256 ir_mode *mode = get_irn_mode(n);
3260 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3261 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3266 n = transform_bitwise_distributive(n, transform_node_And);
3269 } /* transform_node_And */
3274 static ir_node *transform_node_Eor(ir_node *n) {
3275 ir_node *c, *oldn = n;
3276 ir_node *a = get_Eor_left(n);
3277 ir_node *b = get_Eor_right(n);
3278 ir_mode *mode = get_irn_mode(n);
3280 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3282 /* we can evaluate 2 Projs of the same Cmp */
3283 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3284 ir_node *pred_a = get_Proj_pred(a);
3285 ir_node *pred_b = get_Proj_pred(b);
3286 if(pred_a == pred_b) {
3287 dbg_info *dbgi = get_irn_dbg_info(n);
3288 ir_node *block = get_nodes_block(pred_a);
3289 pn_Cmp pn_a = get_Proj_proj(a);
3290 pn_Cmp pn_b = get_Proj_proj(b);
3291 /* yes, we can simply calculate with pncs */
3292 pn_Cmp new_pnc = pn_a ^ pn_b;
3294 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3301 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3302 mode, get_mode_null(mode));
3303 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3304 } else if (mode == mode_b &&
3306 is_Const(b) && is_Const_one(b) &&
3307 is_Cmp(get_Proj_pred(a))) {
3308 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3309 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3310 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3312 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3313 } else if (is_Const(b)) {
3314 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3315 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3316 ir_node *not_op = get_Not_op(a);
3317 dbg_info *dbg = get_irn_dbg_info(n);
3318 ir_graph *irg = current_ir_graph;
3319 ir_node *block = get_nodes_block(n);
3320 ir_mode *mode = get_irn_mode(n);
3321 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3323 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3324 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3325 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3328 n = transform_bitwise_distributive(n, transform_node_Eor);
3332 } /* transform_node_Eor */
3337 static ir_node *transform_node_Not(ir_node *n) {
3338 ir_node *c, *oldn = n;
3339 ir_node *a = get_Not_op(n);
3340 ir_mode *mode = get_irn_mode(n);
3342 HANDLE_UNOP_PHI(tarval_not,a,c);
3344 /* check for a boolean Not */
3345 if (mode == mode_b &&
3347 is_Cmp(get_Proj_pred(a))) {
3348 /* We negate a Cmp. The Cmp has the negated result anyways! */
3349 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3350 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3351 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3355 ir_node *eor_b = get_Eor_right(a);
3356 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3357 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3358 ir_node *eor_a = get_Eor_left(a);
3359 dbg_info *dbg = get_irn_dbg_info(n);
3360 ir_graph *irg = current_ir_graph;
3361 ir_node *block = get_nodes_block(n);
3362 ir_mode *mode = get_irn_mode(n);
3363 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3367 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3368 if (is_Minus(a)) { /* ~-x -> x + -1 */
3369 dbg_info *dbg = get_irn_dbg_info(n);
3370 ir_graph *irg = current_ir_graph;
3371 ir_node *block = get_nodes_block(n);
3372 ir_node *add_l = get_Minus_op(a);
3373 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3374 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3375 } else if (is_Add(a)) {
3376 ir_node *add_r = get_Add_right(a);
3377 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3378 /* ~(x + -1) = -x */
3379 ir_node *op = get_Add_left(a);
3380 ir_node *blk = get_irn_n(n, -1);
3381 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3382 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3387 } /* transform_node_Not */
3390 * Transform a Minus.
3394 * -(a >>u (size-1)) = a >>s (size-1)
3395 * -(a >>s (size-1)) = a >>u (size-1)
3396 * -(a * const) -> a * -const
3398 static ir_node *transform_node_Minus(ir_node *n) {
3399 ir_node *c, *oldn = n;
3400 ir_node *a = get_Minus_op(n);
3403 HANDLE_UNOP_PHI(tarval_neg,a,c);
3405 mode = get_irn_mode(a);
3406 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3407 /* the following rules are only to twos-complement */
3410 ir_node *op = get_Not_op(a);
3411 tarval *tv = get_mode_one(mode);
3412 ir_node *blk = get_irn_n(n, -1);
3413 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3414 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3415 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3419 ir_node *c = get_Shr_right(a);
3422 tarval *tv = get_Const_tarval(c);
3424 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3425 /* -(a >>u (size-1)) = a >>s (size-1) */
3426 ir_node *v = get_Shr_left(a);
3428 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3429 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3435 ir_node *c = get_Shrs_right(a);
3438 tarval *tv = get_Const_tarval(c);
3440 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3441 /* -(a >>s (size-1)) = a >>u (size-1) */
3442 ir_node *v = get_Shrs_left(a);
3444 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3445 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3452 /* - (a-b) = b - a */
3453 ir_node *la = get_Sub_left(a);
3454 ir_node *ra = get_Sub_right(a);
3455 ir_node *blk = get_irn_n(n, -1);
3457 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3458 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3462 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3463 ir_node *mul_l = get_Mul_left(a);
3464 ir_node *mul_r = get_Mul_right(a);
3465 if (is_Const(mul_r)) {
3466 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3467 if(tv != tarval_bad) {
3468 ir_node *cnst = new_Const(mode, tv);
3469 dbg_info *dbg = get_irn_dbg_info(a);
3470 ir_graph *irg = current_ir_graph;
3471 ir_node *block = get_nodes_block(a);
3472 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3473 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3480 } /* transform_node_Minus */
3483 * Transform a Cast_type(Const) into a new Const_type
3485 static ir_node *transform_node_Cast(ir_node *n) {
3487 ir_node *pred = get_Cast_op(n);
3488 ir_type *tp = get_irn_type(n);
3490 if (is_Const(pred) && get_Const_type(pred) != tp) {
3491 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3492 get_Const_tarval(pred), tp);
3493 DBG_OPT_CSTEVAL(oldn, n);
3494 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3495 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3496 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3497 DBG_OPT_CSTEVAL(oldn, n);
3501 } /* transform_node_Cast */
3504 * Transform a Proj(Div) with a non-zero value.
3505 * Removes the exceptions and routes the memory to the NoMem node.
3507 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3508 ir_node *div = get_Proj_pred(proj);
3509 ir_node *b = get_Div_right(div);
3510 ir_node *confirm, *res, *new_mem;
3513 if (value_not_zero(b, &confirm)) {
3514 /* div(x, y) && y != 0 */
3515 if (confirm == NULL) {
3516 /* we are sure we have a Const != 0 */
3517 new_mem = get_Div_mem(div);
3518 if (is_Pin(new_mem))
3519 new_mem = get_Pin_op(new_mem);
3520 set_Div_mem(div, new_mem);
3521 set_irn_pinned(div, op_pin_state_floats);
3524 proj_nr = get_Proj_proj(proj);
3526 case pn_Div_X_regular:
3527 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3529 case pn_Div_X_except:
3530 /* we found an exception handler, remove it */
3531 DBG_OPT_EXC_REM(proj);
3535 res = get_Div_mem(div);
3536 new_mem = get_irg_no_mem(current_ir_graph);
3539 /* This node can only float up to the Confirm block */
3540 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3542 set_irn_pinned(div, op_pin_state_floats);
3543 /* this is a Div without exception, we can remove the memory edge */
3544 set_Div_mem(div, new_mem);
3549 } /* transform_node_Proj_Div */
3552 * Transform a Proj(Mod) with a non-zero value.
3553 * Removes the exceptions and routes the memory to the NoMem node.
3555 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3556 ir_node *mod = get_Proj_pred(proj);
3557 ir_node *b = get_Mod_right(mod);
3558 ir_node *confirm, *res, *new_mem;
3561 if (value_not_zero(b, &confirm)) {
3562 /* mod(x, y) && y != 0 */
3563 proj_nr = get_Proj_proj(proj);
3565 if (confirm == NULL) {
3566 /* we are sure we have a Const != 0 */
3567 new_mem = get_Mod_mem(mod);
3568 if (is_Pin(new_mem))
3569 new_mem = get_Pin_op(new_mem);
3570 set_Mod_mem(mod, new_mem);
3571 set_irn_pinned(mod, op_pin_state_floats);
3576 case pn_Mod_X_regular:
3577 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3579 case pn_Mod_X_except:
3580 /* we found an exception handler, remove it */
3581 DBG_OPT_EXC_REM(proj);
3585 res = get_Mod_mem(mod);
3586 new_mem = get_irg_no_mem(current_ir_graph);
3589 /* This node can only float up to the Confirm block */
3590 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3592 /* this is a Mod without exception, we can remove the memory edge */
3593 set_Mod_mem(mod, new_mem);
3596 if (get_Mod_left(mod) == b) {
3597 /* a % a = 0 if a != 0 */
3598 ir_mode *mode = get_irn_mode(proj);
3599 ir_node *res = new_Const(mode, get_mode_null(mode));
3601 DBG_OPT_CSTEVAL(mod, res);
3607 } /* transform_node_Proj_Mod */
3610 * Transform a Proj(DivMod) with a non-zero value.
3611 * Removes the exceptions and routes the memory to the NoMem node.
3613 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3614 ir_node *divmod = get_Proj_pred(proj);
3615 ir_node *b = get_DivMod_right(divmod);
3616 ir_node *confirm, *res, *new_mem;
3619 if (value_not_zero(b, &confirm)) {
3620 /* DivMod(x, y) && y != 0 */
3621 proj_nr = get_Proj_proj(proj);
3623 if (confirm == NULL) {
3624 /* we are sure we have a Const != 0 */
3625 new_mem = get_DivMod_mem(divmod);
3626 if (is_Pin(new_mem))
3627 new_mem = get_Pin_op(new_mem);
3628 set_DivMod_mem(divmod, new_mem);
3629 set_irn_pinned(divmod, op_pin_state_floats);
3634 case pn_DivMod_X_regular:
3635 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3637 case pn_DivMod_X_except:
3638 /* we found an exception handler, remove it */
3639 DBG_OPT_EXC_REM(proj);
3643 res = get_DivMod_mem(divmod);
3644 new_mem = get_irg_no_mem(current_ir_graph);
3647 /* This node can only float up to the Confirm block */
3648 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3650 /* this is a DivMod without exception, we can remove the memory edge */
3651 set_DivMod_mem(divmod, new_mem);
3654 case pn_DivMod_res_mod:
3655 if (get_DivMod_left(divmod) == b) {
3656 /* a % a = 0 if a != 0 */
3657 ir_mode *mode = get_irn_mode(proj);
3658 ir_node *res = new_Const(mode, get_mode_null(mode));
3660 DBG_OPT_CSTEVAL(divmod, res);
3666 } /* transform_node_Proj_DivMod */
3669 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3671 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3672 if (get_opt_unreachable_code()) {
3673 ir_node *n = get_Proj_pred(proj);
3674 ir_node *b = get_Cond_selector(n);
3676 if (mode_is_int(get_irn_mode(b))) {
3677 tarval *tb = value_of(b);
3679 if (tb != tarval_bad) {
3680 /* we have a constant switch */
3681 long num = get_Proj_proj(proj);
3683 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3684 if (get_tarval_long(tb) == num) {
3685 /* Do NOT create a jump here, or we will have 2 control flow ops
3686 * in a block. This case is optimized away in optimize_cf(). */
3689 /* this case will NEVER be taken, kill it */
3697 } /* transform_node_Proj_Cond */
3700 * Create a 0 constant of given mode.
3702 static ir_node *create_zero_const(ir_mode *mode) {
3703 tarval *tv = get_mode_null(mode);
3704 ir_node *cnst = new_Const(mode, tv);
3709 /* the order of the values is important! */
3710 typedef enum const_class {
3716 static const_class classify_const(const ir_node* n)
3718 if (is_Const(n)) return const_const;
3719 if (is_irn_constlike(n)) return const_like;
3724 * Determines whether r is more constlike or has a larger index (in that order)
3727 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3729 const const_class l_order = classify_const(l);
3730 const const_class r_order = classify_const(r);
3732 l_order > r_order ||
3733 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3737 * Normalizes and optimizes Cmp nodes.
3739 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3740 ir_node *n = get_Proj_pred(proj);
3741 ir_node *left = get_Cmp_left(n);
3742 ir_node *right = get_Cmp_right(n);
3745 ir_mode *mode = NULL;
3746 long proj_nr = get_Proj_proj(proj);
3748 /* we can evaluate some cases directly */
3751 return new_Const(mode_b, get_tarval_b_false());
3753 return new_Const(mode_b, get_tarval_b_true());
3755 if (!mode_is_float(get_irn_mode(left)))
3756 return new_Const(mode_b, get_tarval_b_true());
3762 /* remove Casts of both sides */
3763 left = skip_Cast(left);
3764 right = skip_Cast(right);
3766 /* Remove unnecessary conversions */
3767 /* TODO handle constants */
3768 if (is_Conv(left) && is_Conv(right)) {
3769 ir_mode *mode = get_irn_mode(left);
3770 ir_node *op_left = get_Conv_op(left);
3771 ir_node *op_right = get_Conv_op(right);
3772 ir_mode *mode_left = get_irn_mode(op_left);
3773 ir_mode *mode_right = get_irn_mode(op_right);
3775 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3776 && mode_left != mode_b && mode_right != mode_b) {
3777 ir_graph *irg = current_ir_graph;
3778 ir_node *block = get_nodes_block(n);
3780 if (mode_left == mode_right) {
3784 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3785 } else if (smaller_mode(mode_left, mode_right)) {
3786 left = new_r_Conv(irg, block, op_left, mode_right);
3789 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3790 } else if (smaller_mode(mode_right, mode_left)) {
3792 right = new_r_Conv(irg, block, op_right, mode_left);
3794 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3799 /* remove operation on both sides if possible */
3800 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3802 * The following operations are NOT safe for floating point operations, for instance
3803 * 1.0 + inf == 2.0 + inf, =/=> x == y
3805 if (mode_is_int(get_irn_mode(left))) {
3806 unsigned lop = get_irn_opcode(left);
3808 if (lop == get_irn_opcode(right)) {
3809 ir_node *ll, *lr, *rl, *rr;
3811 /* same operation on both sides, try to remove */
3815 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3816 left = get_unop_op(left);
3817 right = get_unop_op(right);
3819 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3822 ll = get_Add_left(left);
3823 lr = get_Add_right(left);
3824 rl = get_Add_left(right);
3825 rr = get_Add_right(right);
3828 /* X + a CMP X + b ==> a CMP b */
3832 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3833 } else if (ll == rr) {
3834 /* X + a CMP b + X ==> a CMP b */
3838 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3839 } else if (lr == rl) {
3840 /* a + X CMP X + b ==> a CMP b */
3844 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3845 } else if (lr == rr) {
3846 /* a + X CMP b + X ==> a CMP b */
3850 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3854 ll = get_Sub_left(left);
3855 lr = get_Sub_right(left);
3856 rl = get_Sub_left(right);
3857 rr = get_Sub_right(right);
3860 /* X - a CMP X - b ==> a CMP b */
3864 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3865 } else if (lr == rr) {
3866 /* a - X CMP b - X ==> a CMP b */
3870 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3874 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3875 /* a ROTL X CMP b ROTL X ==> a CMP b */
3876 left = get_Rotl_left(left);
3877 right = get_Rotl_left(right);
3879 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3887 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3888 if (is_Add(left) || is_Sub(left)) {
3889 ir_node *ll = get_binop_left(left);
3890 ir_node *lr = get_binop_right(left);
3892 if (lr == right && is_Add(left)) {
3899 right = create_zero_const(get_irn_mode(left));
3901 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3904 if (is_Add(right) || is_Sub(right)) {
3905 ir_node *rl = get_binop_left(right);
3906 ir_node *rr = get_binop_right(right);
3908 if (rr == left && is_Add(right)) {
3915 right = create_zero_const(get_irn_mode(left));
3917 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3920 } /* mode_is_int(...) */
3921 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3923 /* replace mode_b compares with ands/ors */
3924 if (get_irn_mode(left) == mode_b) {
3925 ir_graph *irg = current_ir_graph;
3926 ir_node *block = get_nodes_block(n);
3930 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3931 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3932 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3933 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3934 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3935 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3936 default: bres = NULL;
3939 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3945 * First step: normalize the compare op
3946 * by placing the constant on the right side
3947 * or moving the lower address node to the left.
3949 if (!operands_are_normalized(left, right)) {
3955 proj_nr = get_inversed_pnc(proj_nr);
3960 * Second step: Try to reduce the magnitude
3961 * of a constant. This may help to generate better code
3962 * later and may help to normalize more compares.
3963 * Of course this is only possible for integer values.
3965 if (is_Const(right)) {
3966 mode = get_irn_mode(right);
3967 tv = get_Const_tarval(right);
3969 /* TODO extend to arbitrary constants */
3970 if (is_Conv(left) && tarval_is_null(tv)) {
3971 ir_node *op = get_Conv_op(left);
3972 ir_mode *op_mode = get_irn_mode(op);
3975 * UpConv(x) REL 0 ==> x REL 0
3977 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3978 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3979 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3980 tv = get_mode_null(op_mode);
3984 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3988 if (tv != tarval_bad) {
3989 /* the following optimization is possible on modes without Overflow
3990 * on Unary Minus or on == and !=:
3991 * -a CMP c ==> a swap(CMP) -c
3993 * Beware: for two-complement Overflow may occur, so only == and != can
3994 * be optimized, see this:
3995 * -MININT < 0 =/=> MININT > 0 !!!
3997 if (is_Minus(left) &&
3998 (!mode_overflow_on_unary_Minus(mode) ||
3999 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4000 tv = tarval_neg(tv);
4002 if (tv != tarval_bad) {
4003 left = get_Minus_op(left);
4004 proj_nr = get_inversed_pnc(proj_nr);
4006 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4008 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4009 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4010 tv = tarval_not(tv);
4012 if (tv != tarval_bad) {
4013 left = get_Not_op(left);
4015 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4019 /* for integer modes, we have more */
4020 if (mode_is_int(mode)) {
4021 /* Ne includes Unordered which is not possible on integers.
4022 * However, frontends often use this wrong, so fix it here */
4023 if (proj_nr & pn_Cmp_Uo) {
4024 proj_nr &= ~pn_Cmp_Uo;
4025 set_Proj_proj(proj, proj_nr);
4028 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4029 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4030 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4031 tv = tarval_sub(tv, get_mode_one(mode));
4033 if (tv != tarval_bad) {
4034 proj_nr ^= pn_Cmp_Eq;
4036 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4039 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4040 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4041 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4042 tv = tarval_add(tv, get_mode_one(mode));
4044 if (tv != tarval_bad) {
4045 proj_nr ^= pn_Cmp_Eq;
4047 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4051 /* the following reassociations work only for == and != */
4052 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4054 #if 0 /* Might be not that good in general */
4055 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4056 if (tarval_is_null(tv) && is_Sub(left)) {
4057 right = get_Sub_right(left);
4058 left = get_Sub_left(left);
4060 tv = value_of(right);
4062 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4066 if (tv != tarval_bad) {
4067 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4069 ir_node *c1 = get_Sub_right(left);
4070 tarval *tv2 = value_of(c1);
4072 if (tv2 != tarval_bad) {
4073 tv2 = tarval_add(tv, value_of(c1));
4075 if (tv2 != tarval_bad) {
4076 left = get_Sub_left(left);
4079 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4083 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4084 else if (is_Add(left)) {
4085 ir_node *a_l = get_Add_left(left);
4086 ir_node *a_r = get_Add_right(left);
4090 if (is_Const(a_l)) {
4092 tv2 = value_of(a_l);
4095 tv2 = value_of(a_r);
4098 if (tv2 != tarval_bad) {
4099 tv2 = tarval_sub(tv, tv2);
4101 if (tv2 != tarval_bad) {
4105 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4109 /* -a == c ==> a == -c, -a != c ==> a != -c */
4110 else if (is_Minus(left)) {
4111 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4113 if (tv2 != tarval_bad) {
4114 left = get_Minus_op(left);
4117 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4122 /* the following reassociations work only for <= */
4123 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4124 if (tv != tarval_bad) {
4125 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4126 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4132 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4133 switch (get_irn_opcode(left)) {
4137 c1 = get_And_right(left);
4140 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4141 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4143 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4145 /* TODO: move to constant evaluation */
4146 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4147 c1 = new_Const(mode_b, tv);
4148 DBG_OPT_CSTEVAL(proj, c1);
4152 if (tarval_is_single_bit(tv)) {
4154 * optimization for AND:
4156 * And(x, C) == C ==> And(x, C) != 0
4157 * And(x, C) != C ==> And(X, C) == 0
4159 * if C is a single Bit constant.
4162 /* check for Constant's match. We have check hare the tarvals,
4163 because our const might be changed */
4164 if (get_Const_tarval(c1) == tv) {
4165 /* fine: do the transformation */
4166 tv = get_mode_null(get_tarval_mode(tv));
4167 proj_nr ^= pn_Cmp_Leg;
4169 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4175 c1 = get_Or_right(left);
4176 if (is_Const(c1) && tarval_is_null(tv)) {
4178 * Or(x, C) == 0 && C != 0 ==> FALSE
4179 * Or(x, C) != 0 && C != 0 ==> TRUE
4181 if (! tarval_is_null(get_Const_tarval(c1))) {
4182 /* TODO: move to constant evaluation */
4183 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4184 c1 = new_Const(mode_b, tv);
4185 DBG_OPT_CSTEVAL(proj, c1);
4192 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4194 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4197 c1 = get_Shl_right(left);
4199 tarval *tv1 = get_Const_tarval(c1);
4200 ir_mode *mode = get_irn_mode(left);
4201 tarval *minus1 = get_mode_all_one(mode);
4202 tarval *amask = tarval_shr(minus1, tv1);
4203 tarval *cmask = tarval_shl(minus1, tv1);
4206 if (tarval_and(tv, cmask) != tv) {
4207 /* condition not met */
4208 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4209 c1 = new_Const(mode_b, tv);
4210 DBG_OPT_CSTEVAL(proj, c1);
4213 sl = get_Shl_left(left);
4214 blk = get_nodes_block(n);
4215 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4216 tv = tarval_shr(tv, tv1);
4218 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4223 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4225 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4228 c1 = get_Shr_right(left);
4230 tarval *tv1 = get_Const_tarval(c1);
4231 ir_mode *mode = get_irn_mode(left);
4232 tarval *minus1 = get_mode_all_one(mode);
4233 tarval *amask = tarval_shl(minus1, tv1);
4234 tarval *cmask = tarval_shr(minus1, tv1);
4237 if (tarval_and(tv, cmask) != tv) {
4238 /* condition not met */
4239 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4240 c1 = new_Const(mode_b, tv);
4241 DBG_OPT_CSTEVAL(proj, c1);
4244 sl = get_Shr_left(left);
4245 blk = get_nodes_block(n);
4246 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4247 tv = tarval_shl(tv, tv1);
4249 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4254 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4256 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4259 c1 = get_Shrs_right(left);
4261 tarval *tv1 = get_Const_tarval(c1);
4262 ir_mode *mode = get_irn_mode(left);
4263 tarval *minus1 = get_mode_all_one(mode);
4264 tarval *amask = tarval_shl(minus1, tv1);
4265 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4268 cond = tarval_sub(cond, tv1);
4269 cond = tarval_shrs(tv, cond);
4271 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4272 /* condition not met */
4273 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4274 c1 = new_Const(mode_b, tv);
4275 DBG_OPT_CSTEVAL(proj, c1);
4278 sl = get_Shrs_left(left);
4279 blk = get_nodes_block(n);
4280 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4281 tv = tarval_shl(tv, tv1);
4283 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4288 } /* tarval != bad */
4291 if (changed & 2) /* need a new Const */
4292 right = new_Const(mode, tv);
4294 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4295 ir_node *op = get_Proj_pred(left);
4297 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4298 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4299 ir_node *c = get_binop_right(op);
4302 tarval *tv = get_Const_tarval(c);
4304 if (tarval_is_single_bit(tv)) {
4305 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4306 ir_node *v = get_binop_left(op);
4307 ir_node *blk = get_irn_n(op, -1);
4308 ir_mode *mode = get_irn_mode(v);
4310 tv = tarval_sub(tv, get_mode_one(mode));
4311 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4313 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4320 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4322 /* create a new compare */
4323 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4324 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4328 } /* transform_node_Proj_Cmp */
4331 * Does all optimizations on nodes that must be done on it's Proj's
4332 * because of creating new nodes.
4334 static ir_node *transform_node_Proj(ir_node *proj) {
4335 ir_node *n = get_Proj_pred(proj);
4337 switch (get_irn_opcode(n)) {
4339 return transform_node_Proj_Div(proj);
4342 return transform_node_Proj_Mod(proj);
4345 return transform_node_Proj_DivMod(proj);
4348 return transform_node_Proj_Cond(proj);
4351 return transform_node_Proj_Cmp(proj);
4354 /* should not happen, but if it does will be optimized away */
4355 return equivalent_node_Proj(proj);
4361 } /* transform_node_Proj */
4364 * Move Confirms down through Phi nodes.
4366 static ir_node *transform_node_Phi(ir_node *phi) {
4368 ir_mode *mode = get_irn_mode(phi);
4370 if (mode_is_reference(mode)) {
4371 n = get_irn_arity(phi);
4373 /* Beware of Phi0 */
4375 ir_node *pred = get_irn_n(phi, 0);
4376 ir_node *bound, *new_Phi, *block, **in;
4379 if (! is_Confirm(pred))
4382 bound = get_Confirm_bound(pred);
4383 pnc = get_Confirm_cmp(pred);
4385 NEW_ARR_A(ir_node *, in, n);
4386 in[0] = get_Confirm_value(pred);
4388 for (i = 1; i < n; ++i) {
4389 pred = get_irn_n(phi, i);
4391 if (! is_Confirm(pred) ||
4392 get_Confirm_bound(pred) != bound ||
4393 get_Confirm_cmp(pred) != pnc)
4395 in[i] = get_Confirm_value(pred);
4397 /* move the Confirm nodes "behind" the Phi */
4398 block = get_irn_n(phi, -1);
4399 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4400 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4404 } /* transform_node_Phi */
4407 * Returns the operands of a commutative bin-op, if one operand is
4408 * a const, it is returned as the second one.
4410 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4411 ir_node *op_a = get_binop_left(binop);
4412 ir_node *op_b = get_binop_right(binop);
4414 assert(is_op_commutative(get_irn_op(binop)));
4416 if (is_Const(op_a)) {
4423 } /* get_comm_Binop_Ops */
4426 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4427 * Such pattern may arise in bitfield stores.
4429 * value c4 value c4 & c2
4430 * AND c3 AND c1 | c3
4437 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4440 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4443 ir_node *and_l, *c3;
4444 ir_node *value, *c4;
4445 ir_node *new_and, *new_const, *block;
4446 ir_mode *mode = get_irn_mode(or);
4448 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4451 get_comm_Binop_Ops(or, &and, &c1);
4452 if (!is_Const(c1) || !is_And(and))
4455 get_comm_Binop_Ops(and, &or_l, &c2);
4459 tv1 = get_Const_tarval(c1);
4460 tv2 = get_Const_tarval(c2);
4462 tv = tarval_or(tv1, tv2);
4463 if (tarval_is_all_one(tv)) {
4464 /* the AND does NOT clear a bit with isn't set by the OR */
4465 set_Or_left(or, or_l);
4466 set_Or_right(or, c1);
4468 /* check for more */
4475 get_comm_Binop_Ops(or_l, &and_l, &c3);
4476 if (!is_Const(c3) || !is_And(and_l))
4479 get_comm_Binop_Ops(and_l, &value, &c4);
4483 /* ok, found the pattern, check for conditions */
4484 assert(mode == get_irn_mode(and));
4485 assert(mode == get_irn_mode(or_l));
4486 assert(mode == get_irn_mode(and_l));
4488 tv3 = get_Const_tarval(c3);
4489 tv4 = get_Const_tarval(c4);
4491 tv = tarval_or(tv4, tv2);
4492 if (!tarval_is_all_one(tv)) {
4493 /* have at least one 0 at the same bit position */
4497 n_tv4 = tarval_not(tv4);
4498 if (tv3 != tarval_and(tv3, n_tv4)) {
4499 /* bit in the or_mask is outside the and_mask */
4503 n_tv2 = tarval_not(tv2);
4504 if (tv1 != tarval_and(tv1, n_tv2)) {
4505 /* bit in the or_mask is outside the and_mask */
4509 /* ok, all conditions met */
4510 block = get_irn_n(or, -1);
4512 new_and = new_r_And(current_ir_graph, block,
4513 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4515 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4517 set_Or_left(or, new_and);
4518 set_Or_right(or, new_const);
4520 /* check for more */
4522 } /* transform_node_Or_bf_store */
4525 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4527 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4528 ir_mode *mode = get_irn_mode(or);
4529 ir_node *shl, *shr, *block;
4530 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4533 if (! mode_is_int(mode))
4536 shl = get_binop_left(or);
4537 shr = get_binop_right(or);
4546 } else if (!is_Shl(shl)) {
4548 } else if (!is_Shr(shr)) {
4551 x = get_Shl_left(shl);
4552 if (x != get_Shr_left(shr))
4555 c1 = get_Shl_right(shl);
4556 c2 = get_Shr_right(shr);
4557 if (is_Const(c1) && is_Const(c2)) {
4558 tv1 = get_Const_tarval(c1);
4559 if (! tarval_is_long(tv1))
4562 tv2 = get_Const_tarval(c2);
4563 if (! tarval_is_long(tv2))
4566 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4567 != (int) get_mode_size_bits(mode))
4570 /* yet, condition met */
4571 block = get_irn_n(or, -1);
4573 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4575 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4577 } else if (is_Sub(c1)) {
4581 if (get_Sub_right(sub) != v)
4584 c1 = get_Sub_left(sub);
4588 tv1 = get_Const_tarval(c1);
4589 if (! tarval_is_long(tv1))
4592 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4595 /* yet, condition met */
4596 block = get_nodes_block(or);
4598 /* a Rot right is not supported, so use a rot left */
4599 n = new_r_Rotl(current_ir_graph, block, x, sub, mode);
4601 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4603 } else if (is_Sub(c2)) {
4607 c1 = get_Sub_left(sub);
4611 tv1 = get_Const_tarval(c1);
4612 if (! tarval_is_long(tv1))
4615 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4618 /* yet, condition met */
4619 block = get_irn_n(or, -1);
4622 n = new_r_Rotl(current_ir_graph, block, x, v, mode);
4624 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4629 } /* transform_node_Or_Rotl */
4634 static ir_node *transform_node_Or(ir_node *n) {
4635 ir_node *c, *oldn = n;
4636 ir_node *a = get_Or_left(n);
4637 ir_node *b = get_Or_right(n);
4640 if (is_Not(a) && is_Not(b)) {
4641 /* ~a | ~b = ~(a&b) */
4642 ir_node *block = get_nodes_block(n);
4644 mode = get_irn_mode(n);
4647 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4648 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4649 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4653 /* we can evaluate 2 Projs of the same Cmp */
4654 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4655 ir_node *pred_a = get_Proj_pred(a);
4656 ir_node *pred_b = get_Proj_pred(b);
4657 if (pred_a == pred_b) {
4658 dbg_info *dbgi = get_irn_dbg_info(n);
4659 ir_node *block = get_nodes_block(pred_a);
4660 pn_Cmp pn_a = get_Proj_proj(a);
4661 pn_Cmp pn_b = get_Proj_proj(b);
4662 /* yes, we can simply calculate with pncs */
4663 pn_Cmp new_pnc = pn_a | pn_b;
4665 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4670 mode = get_irn_mode(n);
4671 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4673 n = transform_node_Or_bf_store(n);
4674 n = transform_node_Or_Rotl(n);
4678 n = transform_bitwise_distributive(n, transform_node_Or);
4681 } /* transform_node_Or */
4685 static ir_node *transform_node(ir_node *n);
4688 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4690 * Should be moved to reassociation?
4692 static ir_node *transform_node_shift(ir_node *n) {
4693 ir_node *left, *right;
4694 tarval *tv1, *tv2, *res;
4696 int modulo_shf, flag;
4698 left = get_binop_left(n);
4700 /* different operations */
4701 if (get_irn_op(left) != get_irn_op(n))
4704 right = get_binop_right(n);
4705 tv1 = value_of(right);
4706 if (tv1 == tarval_bad)
4709 tv2 = value_of(get_binop_right(left));
4710 if (tv2 == tarval_bad)
4713 res = tarval_add(tv1, tv2);
4715 /* beware: a simple replacement works only, if res < modulo shift */
4716 mode = get_irn_mode(n);
4720 modulo_shf = get_mode_modulo_shift(mode);
4721 if (modulo_shf > 0) {
4722 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4724 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4730 /* ok, we can replace it */
4731 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4733 in[0] = get_binop_left(left);
4734 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4736 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4738 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4740 return transform_node(irn);
4743 } /* transform_node_shift */
4748 static ir_node *transform_node_Shr(ir_node *n) {
4749 ir_node *c, *oldn = n;
4750 ir_node *a = get_Shr_left(n);
4751 ir_node *b = get_Shr_right(n);
4752 ir_mode *mode = get_irn_mode(n);
4754 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4755 return transform_node_shift(n);
4756 } /* transform_node_Shr */
4761 static ir_node *transform_node_Shrs(ir_node *n) {
4762 ir_node *c, *oldn = n;
4763 ir_node *a = get_Shrs_left(n);
4764 ir_node *b = get_Shrs_right(n);
4765 ir_mode *mode = get_irn_mode(n);
4767 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4768 return transform_node_shift(n);
4769 } /* transform_node_Shrs */
4774 static ir_node *transform_node_Shl(ir_node *n) {
4775 ir_node *c, *oldn = n;
4776 ir_node *a = get_Shl_left(n);
4777 ir_node *b = get_Shl_right(n);
4778 ir_mode *mode = get_irn_mode(n);
4780 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4781 return transform_node_shift(n);
4782 } /* transform_node_Shl */
4787 static ir_node *transform_node_Rotl(ir_node *n) {
4788 ir_node *c, *oldn = n;
4789 ir_node *a = get_Rotl_left(n);
4790 ir_node *b = get_Rotl_right(n);
4791 ir_mode *mode = get_irn_mode(n);
4793 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
4794 return transform_node_shift(n);
4795 } /* transform_node_Rotl */
4800 static ir_node *transform_node_Conv(ir_node *n) {
4801 ir_node *c, *oldn = n;
4802 ir_node *a = get_Conv_op(n);
4804 if (is_const_Phi(a)) {
4805 c = apply_conv_on_phi(a, get_irn_mode(n));
4807 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4812 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4813 ir_mode *mode = get_irn_mode(n);
4814 return new_r_Unknown(current_ir_graph, mode);
4818 } /* transform_node_Conv */
4821 * Remove dead blocks and nodes in dead blocks
4822 * in keep alive list. We do not generate a new End node.
4824 static ir_node *transform_node_End(ir_node *n) {
4825 int i, j, n_keepalives = get_End_n_keepalives(n);
4828 NEW_ARR_A(ir_node *, in, n_keepalives);
4830 for (i = j = 0; i < n_keepalives; ++i) {
4831 ir_node *ka = get_End_keepalive(n, i);
4833 if (! is_Block_dead(ka)) {
4837 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4840 /* FIXME: beabi need to keep a Proj(M) */
4841 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4844 if (j != n_keepalives)
4845 set_End_keepalives(n, j, in);
4847 } /* transform_node_End */
4849 /** returns 1 if a == -b */
4850 static int is_negated_value(ir_node *a, ir_node *b) {
4851 if (is_Minus(a) && get_Minus_op(a) == b)
4853 if (is_Minus(b) && get_Minus_op(b) == a)
4855 if (is_Sub(a) && is_Sub(b)) {
4856 ir_node *a_left = get_Sub_left(a);
4857 ir_node *a_right = get_Sub_right(a);
4858 ir_node *b_left = get_Sub_left(b);
4859 ir_node *b_right = get_Sub_right(b);
4861 if (a_left == b_right && a_right == b_left)
4869 * Optimize a Mux into some simpler cases.
4871 static ir_node *transform_node_Mux(ir_node *n) {
4872 ir_node *oldn = n, *sel = get_Mux_sel(n);
4873 ir_mode *mode = get_irn_mode(n);
4874 ir_node *t = get_Mux_true(n);
4875 ir_node *f = get_Mux_false(n);
4876 ir_graph *irg = current_ir_graph;
4877 ir_node *conds[1], *vals[2];
4879 /* first normalization step: move a possible zero to the false case */
4881 ir_node *cmp = get_Proj_pred(sel);
4884 if (is_Const(t) && is_Const_null(t)) {
4885 /* Psi(x, 0, y) => Psi(x, y, 0) */
4886 pn_Cmp pnc = get_Proj_proj(sel);
4887 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
4888 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
4892 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
4899 /* note: after normalization, false can only happen on default */
4900 if (mode == mode_b) {
4901 dbg_info *dbg = get_irn_dbg_info(n);
4902 ir_node *block = get_nodes_block(n);
4903 ir_graph *irg = current_ir_graph;
4906 tarval *tv_t = get_Const_tarval(t);
4907 if (tv_t == tarval_b_true) {
4909 /* Muxb(sel, true, false) = sel */
4910 assert(get_Const_tarval(f) == tarval_b_false);
4911 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4914 /* Muxb(sel, true, x) = Or(sel, x) */
4915 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4916 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4920 } else if (is_Const(f)) {
4921 tarval *tv_f = get_Const_tarval(f);
4922 if (tv_f == tarval_b_true) {
4923 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4924 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4925 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4926 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4929 /* Muxb(sel, x, false) = And(sel, x) */
4930 assert(tv_f == tarval_b_false);
4931 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4932 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4938 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
4939 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
4940 tarval *a = get_Const_tarval(t);
4941 tarval *b = get_Const_tarval(f);
4942 tarval *null = get_tarval_null(mode);
4945 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
4946 diff = tarval_sub(a, b);
4949 diff = tarval_sub(b, a);
4953 if (diff == get_tarval_one(mode) && min != null) {
4954 dbg_info *dbg = get_irn_dbg_info(n);
4955 ir_node *block = get_nodes_block(n);
4956 ir_graph *irg = current_ir_graph;
4960 vals[0] = new_Const(mode, tarval_sub(a, min));
4961 vals[1] = new_Const(mode, tarval_sub(b, min));
4962 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
4963 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
4969 ir_node *cmp = get_Proj_pred(sel);
4970 long pn = get_Proj_proj(sel);
4973 * Note: normalization puts the constant on the right side,
4974 * so we check only one case.
4976 * Note further that these optimization work even for floating point
4977 * with NaN's because -NaN == NaN.
4978 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
4982 ir_node *cmp_r = get_Cmp_right(cmp);
4983 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4984 ir_node *block = get_nodes_block(n);
4985 ir_node *cmp_l = get_Cmp_left(cmp);
4987 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
4990 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4991 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4993 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
4994 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4996 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4998 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4999 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5001 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5002 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5004 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5006 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5011 if (mode_is_int(mode)) {
5013 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5014 /* Psi((a & b) != 0, c, 0) */
5015 ir_node *and_r = get_And_right(cmp_l);
5018 if (and_r == t && f == cmp_r) {
5019 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5020 if (pn == pn_Cmp_Lg) {
5021 /* Psi((a & 2^C) != 0, 2^C, 0) */
5024 /* Psi((a & 2^C) == 0, 2^C, 0) */
5025 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5026 block, cmp_l, t, mode);
5031 if (is_Shl(and_r)) {
5032 ir_node *shl_l = get_Shl_left(and_r);
5033 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5034 if (and_r == t && f == cmp_r) {
5035 if (pn == pn_Cmp_Lg) {
5036 /* (a & (1 << n)) != 0, (1 << n), 0) */
5039 /* (a & (1 << n)) == 0, (1 << n), 0) */
5040 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5041 block, cmp_l, t, mode);
5047 and_l = get_And_left(cmp_l);
5048 if (is_Shl(and_l)) {
5049 ir_node *shl_l = get_Shl_left(and_l);
5050 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5051 if (and_l == t && f == cmp_r) {
5052 if (pn == pn_Cmp_Lg) {
5053 /* ((1 << n) & a) != 0, (1 << n), 0) */
5056 /* ((1 << n) & a) == 0, (1 << n), 0) */
5057 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5058 block, cmp_l, t, mode);
5069 return arch_transform_node_Mux(n);
5070 } /* transform_node_Mux */
5073 * Optimize a Psi into some simpler cases.
5075 static ir_node *transform_node_Psi(ir_node *n) {
5077 return transform_node_Mux(n);
5080 } /* transform_node_Psi */
5083 * optimize sync nodes that have other syncs as input we simply add the inputs
5084 * of the other sync to our own inputs
5086 static ir_node *transform_node_Sync(ir_node *n) {
5087 int arity = get_Sync_n_preds(n);
5090 for (i = 0; i < arity;) {
5091 ir_node *pred = get_Sync_pred(n, i);
5095 if (!is_Sync(pred)) {
5103 pred_arity = get_Sync_n_preds(pred);
5104 for (j = 0; j < pred_arity; ++j) {
5105 ir_node *pred_pred = get_Sync_pred(pred, j);
5110 add_irn_n(n, pred_pred);
5114 if (get_Sync_pred(n, k) == pred_pred) break;
5119 /* rehash the sync node */
5120 add_identities(current_ir_graph->value_table, n);
5126 * Tries several [inplace] [optimizing] transformations and returns an
5127 * equivalent node. The difference to equivalent_node() is that these
5128 * transformations _do_ generate new nodes, and thus the old node must
5129 * not be freed even if the equivalent node isn't the old one.
5131 static ir_node *transform_node(ir_node *n) {
5135 * Transform_node is the only "optimizing transformation" that might
5136 * return a node with a different opcode. We iterate HERE until fixpoint
5137 * to get the final result.
5141 if (n->op->ops.transform_node)
5142 n = n->op->ops.transform_node(n);
5143 } while (oldn != n);
5146 } /* transform_node */
5149 * Sets the default transform node operation for an ir_op_ops.
5151 * @param code the opcode for the default operation
5152 * @param ops the operations initialized
5157 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5161 ops->transform_node = transform_node_##a; \
5198 } /* firm_set_default_transform_node */
5201 /* **************** Common Subexpression Elimination **************** */
5203 /** The size of the hash table used, should estimate the number of nodes
5205 #define N_IR_NODES 512
5207 /** Compares the attributes of two Const nodes. */
5208 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5209 return (get_Const_tarval(a) != get_Const_tarval(b))
5210 || (get_Const_type(a) != get_Const_type(b));
5211 } /* node_cmp_attr_Const */
5213 /** Compares the attributes of two Proj nodes. */
5214 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5215 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5216 } /* node_cmp_attr_Proj */
5218 /** Compares the attributes of two Filter nodes. */
5219 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5220 return get_Filter_proj(a) != get_Filter_proj(b);
5221 } /* node_cmp_attr_Filter */
5223 /** Compares the attributes of two Alloc nodes. */
5224 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5225 const alloc_attr *pa = get_irn_alloc_attr(a);
5226 const alloc_attr *pb = get_irn_alloc_attr(b);
5227 return (pa->where != pb->where) || (pa->type != pb->type);
5228 } /* node_cmp_attr_Alloc */
5230 /** Compares the attributes of two Free nodes. */
5231 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5232 const free_attr *pa = get_irn_free_attr(a);
5233 const free_attr *pb = get_irn_free_attr(b);
5234 return (pa->where != pb->where) || (pa->type != pb->type);
5235 } /* node_cmp_attr_Free */
5237 /** Compares the attributes of two SymConst nodes. */
5238 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5239 const symconst_attr *pa = get_irn_symconst_attr(a);
5240 const symconst_attr *pb = get_irn_symconst_attr(b);
5241 return (pa->kind != pb->kind)
5242 || (pa->sym.type_p != pb->sym.type_p)
5243 || (pa->tp != pb->tp);
5244 } /* node_cmp_attr_SymConst */
5246 /** Compares the attributes of two Call nodes. */
5247 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5248 return get_irn_call_attr(a) != get_irn_call_attr(b);
5249 } /* node_cmp_attr_Call */
5251 /** Compares the attributes of two Sel nodes. */
5252 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5253 const ir_entity *a_ent = get_Sel_entity(a);
5254 const ir_entity *b_ent = get_Sel_entity(b);
5256 (a_ent->kind != b_ent->kind) ||
5257 (a_ent->name != b_ent->name) ||
5258 (a_ent->owner != b_ent->owner) ||
5259 (a_ent->ld_name != b_ent->ld_name) ||
5260 (a_ent->type != b_ent->type);
5261 } /* node_cmp_attr_Sel */
5263 /** Compares the attributes of two Phi nodes. */
5264 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5265 /* we can only enter this function if both nodes have the same number of inputs,
5266 hence it is enough to check if one of them is a Phi0 */
5268 /* check the Phi0 pos attribute */
5269 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5272 } /* node_cmp_attr_Phi */
5274 /** Compares the attributes of two Conv nodes. */
5275 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5276 return get_Conv_strict(a) != get_Conv_strict(b);
5277 } /* node_cmp_attr_Conv */
5279 /** Compares the attributes of two Cast nodes. */
5280 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5281 return get_Cast_type(a) != get_Cast_type(b);
5282 } /* node_cmp_attr_Cast */
5284 /** Compares the attributes of two Load nodes. */
5285 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5286 if (get_Load_volatility(a) == volatility_is_volatile ||
5287 get_Load_volatility(b) == volatility_is_volatile)
5288 /* NEVER do CSE on volatile Loads */
5290 /* do not CSE Loads with different alignment. Be conservative. */
5291 if (get_Load_align(a) != get_Load_align(b))
5294 return get_Load_mode(a) != get_Load_mode(b);
5295 } /* node_cmp_attr_Load */
5297 /** Compares the attributes of two Store nodes. */
5298 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5299 /* do not CSE Stores with different alignment. Be conservative. */
5300 if (get_Store_align(a) != get_Store_align(b))
5303 /* NEVER do CSE on volatile Stores */
5304 return (get_Store_volatility(a) == volatility_is_volatile ||
5305 get_Store_volatility(b) == volatility_is_volatile);
5306 } /* node_cmp_attr_Store */
5308 /** Compares two exception attributes */
5309 static int node_cmp_exception(ir_node *a, ir_node *b) {
5310 const except_attr *ea = get_irn_except_attr(a);
5311 const except_attr *eb = get_irn_except_attr(b);
5313 return ea->pin_state != eb->pin_state;
5316 #define node_cmp_attr_Bound node_cmp_exception
5318 /** Compares the attributes of two Div nodes. */
5319 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5320 const divmod_attr *ma = get_irn_divmod_attr(a);
5321 const divmod_attr *mb = get_irn_divmod_attr(b);
5322 return ma->exc.pin_state != mb->exc.pin_state ||
5323 ma->res_mode != mb->res_mode ||
5324 ma->no_remainder != mb->no_remainder;
5325 } /* node_cmp_attr_Div */
5327 /** Compares the attributes of two DivMod nodes. */
5328 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5329 const divmod_attr *ma = get_irn_divmod_attr(a);
5330 const divmod_attr *mb = get_irn_divmod_attr(b);
5331 return ma->exc.pin_state != mb->exc.pin_state ||
5332 ma->res_mode != mb->res_mode;
5333 } /* node_cmp_attr_DivMod */
5335 /** Compares the attributes of two Mod nodes. */
5336 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5337 const divmod_attr *ma = get_irn_divmod_attr(a);
5338 const divmod_attr *mb = get_irn_divmod_attr(b);
5339 return ma->exc.pin_state != mb->exc.pin_state ||
5340 ma->res_mode != mb->res_mode;
5341 } /* node_cmp_attr_Mod */
5343 /** Compares the attributes of two Quot nodes. */
5344 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5345 const divmod_attr *ma = get_irn_divmod_attr(a);
5346 const divmod_attr *mb = get_irn_divmod_attr(b);
5347 return ma->exc.pin_state != mb->exc.pin_state ||
5348 ma->res_mode != mb->res_mode;
5349 } /* node_cmp_attr_Quot */
5351 /** Compares the attributes of two Confirm nodes. */
5352 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5353 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5354 } /* node_cmp_attr_Confirm */
5356 /** Compares the attributes of two ASM nodes. */
5357 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5359 const ir_asm_constraint *ca;
5360 const ir_asm_constraint *cb;
5363 if (get_ASM_text(a) != get_ASM_text(b))
5366 /* Should we really check the constraints here? Should be better, but is strange. */
5367 n = get_ASM_n_input_constraints(a);
5368 if (n != get_ASM_n_input_constraints(b))
5371 ca = get_ASM_input_constraints(a);
5372 cb = get_ASM_input_constraints(b);
5373 for (i = 0; i < n; ++i) {
5374 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5378 n = get_ASM_n_output_constraints(a);
5379 if (n != get_ASM_n_output_constraints(b))
5382 ca = get_ASM_output_constraints(a);
5383 cb = get_ASM_output_constraints(b);
5384 for (i = 0; i < n; ++i) {
5385 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5389 n = get_ASM_n_clobbers(a);
5390 if (n != get_ASM_n_clobbers(b))
5393 cla = get_ASM_clobbers(a);
5394 clb = get_ASM_clobbers(b);
5395 for (i = 0; i < n; ++i) {
5396 if (cla[i] != clb[i])
5400 } /* node_cmp_attr_ASM */
5403 * Set the default node attribute compare operation for an ir_op_ops.
5405 * @param code the opcode for the default operation
5406 * @param ops the operations initialized
5411 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5415 ops->node_cmp_attr = node_cmp_attr_##a; \
5446 } /* firm_set_default_node_cmp_attr */
5449 * Compare function for two nodes in the value table. Gets two
5450 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5452 int identities_cmp(const void *elt, const void *key) {
5453 ir_node *a = (ir_node *)elt;
5454 ir_node *b = (ir_node *)key;
5457 if (a == b) return 0;
5459 if ((get_irn_op(a) != get_irn_op(b)) ||
5460 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5462 /* compare if a's in and b's in are of equal length */
5463 irn_arity_a = get_irn_intra_arity(a);
5464 if (irn_arity_a != get_irn_intra_arity(b))
5467 if (get_irn_pinned(a) == op_pin_state_pinned) {
5468 /* for pinned nodes, the block inputs must be equal */
5469 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5471 } else if (! get_opt_global_cse()) {
5472 /* for block-local CSE both nodes must be in the same MacroBlock */
5473 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5477 /* compare a->in[0..ins] with b->in[0..ins] */
5478 for (i = 0; i < irn_arity_a; i++)
5479 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5483 * here, we already now that the nodes are identical except their
5486 if (a->op->ops.node_cmp_attr)
5487 return a->op->ops.node_cmp_attr(a, b);
5490 } /* identities_cmp */
5493 * Calculate a hash value of a node.
5495 unsigned ir_node_hash(const ir_node *node) {
5499 if (node->op == op_Const) {
5500 /* special value for const, as they only differ in their tarval. */
5501 h = HASH_PTR(node->attr.con.tv);
5502 h = 9*h + HASH_PTR(get_irn_mode(node));
5503 } else if (node->op == op_SymConst) {
5504 /* special value for const, as they only differ in their symbol. */
5505 h = HASH_PTR(node->attr.symc.sym.type_p);
5506 h = 9*h + HASH_PTR(get_irn_mode(node));
5509 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5510 h = irn_arity = get_irn_intra_arity(node);
5512 /* consider all in nodes... except the block if not a control flow. */
5513 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5514 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5518 h = 9*h + HASH_PTR(get_irn_mode(node));
5520 h = 9*h + HASH_PTR(get_irn_op(node));
5524 } /* ir_node_hash */
5526 pset *new_identities(void) {
5527 return new_pset(identities_cmp, N_IR_NODES);
5528 } /* new_identities */
5530 void del_identities(pset *value_table) {
5531 del_pset(value_table);
5532 } /* del_identities */
5535 * Normalize a node by putting constants (and operands with larger
5536 * node index) on the right (operator side).
5538 * @param n The node to normalize
5540 static void normalize_node(ir_node *n) {
5541 if (is_op_commutative(get_irn_op(n))) {
5542 ir_node *l = get_binop_left(n);
5543 ir_node *r = get_binop_right(n);
5545 /* For commutative operators perform a OP b == b OP a but keep
5546 * constants on the RIGHT side. This helps greatly in some
5547 * optimizations. Moreover we use the idx number to make the form
5549 if (!operands_are_normalized(l, r)) {
5550 set_binop_left(n, r);
5551 set_binop_right(n, l);
5554 } /* normalize_node */
5557 * Update the nodes after a match in the value table. If both nodes have
5558 * the same MacroBlock but different Blocks, we must ensure that the node
5559 * with the dominating Block (the node that is near to the MacroBlock header
5560 * is stored in the table.
5561 * Because a MacroBlock has only one "non-exception" flow, we don't need
5562 * dominance info here: We known, that one block must dominate the other and
5563 * following the only block input will allow to find it.
5565 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5566 ir_node *known_blk, *new_block, *block, *mbh;
5568 if (get_opt_global_cse()) {
5569 /* Block inputs are meaning less */
5572 known_blk = get_irn_n(known_irn, -1);
5573 new_block = get_irn_n(new_ir_node, -1);
5574 if (known_blk == new_block) {
5575 /* already in the same block */
5579 * We expect the typical case when we built the graph. In that case, the
5580 * known_irn is already the upper one, so checking this should be faster.
5583 mbh = get_Block_MacroBlock(new_block);
5585 if (block == known_blk) {
5586 /* ok, we have found it: known_block dominates new_block as expected */
5591 * We have reached the MacroBlock header NOT founding
5592 * the known_block. new_block must dominate known_block.
5595 set_irn_n(known_irn, -1, new_block);
5598 assert(get_Block_n_cfgpreds(block) == 1);
5599 block = get_Block_cfgpred_block(block, 0);
5601 } /* update_value_table */
5604 * Return the canonical node computing the same value as n.
5605 * Looks up the node in a hash table, enters it in the table
5606 * if it isn't there yet.
5608 * @param value_table the HashSet containing all nodes in the
5610 * @param n the node to look up
5612 * @return a node that computes the same value as n or n if no such
5613 * node could be found
5615 ir_node *identify_remember(pset *value_table, ir_node *n) {
5618 if (!value_table) return n;
5621 /* lookup or insert in hash table with given hash key. */
5622 o = pset_insert(value_table, n, ir_node_hash(n));
5625 update_known_irn(o, n);
5630 } /* identify_remember */
5633 * During construction we set the op_pin_state_pinned flag in the graph right when the
5634 * optimization is performed. The flag turning on procedure global cse could
5635 * be changed between two allocations. This way we are safe.
5637 * @param value_table The value table
5638 * @param n The node to lookup
5640 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5643 n = identify_remember(value_table, n);
5644 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5645 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5647 } /* identify_cons */
5649 /* Add a node to the identities value table. */
5650 void add_identities(pset *value_table, ir_node *node) {
5651 if (get_opt_cse() && is_no_Block(node))
5652 identify_remember(value_table, node);
5653 } /* add_identities */
5655 /* Visit each node in the value table of a graph. */
5656 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5658 ir_graph *rem = current_ir_graph;
5660 current_ir_graph = irg;
5661 foreach_pset(irg->value_table, node)
5663 current_ir_graph = rem;
5664 } /* visit_all_identities */
5667 * Garbage in, garbage out. If a node has a dead input, i.e., the
5668 * Bad node is input to the node, return the Bad node.
5670 static ir_node *gigo(ir_node *node) {
5672 ir_op *op = get_irn_op(node);
5674 /* remove garbage blocks by looking at control flow that leaves the block
5675 and replacing the control flow by Bad. */
5676 if (get_irn_mode(node) == mode_X) {
5677 ir_node *block = get_nodes_block(skip_Proj(node));
5679 /* Don't optimize nodes in immature blocks. */
5680 if (!get_Block_matured(block))
5682 /* Don't optimize End, may have Bads. */
5683 if (op == op_End) return node;
5685 if (is_Block(block)) {
5686 if (is_Block_dead(block)) {
5687 /* control flow from dead block is dead */
5691 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5692 if (!is_Bad(get_irn_n(block, i)))
5696 ir_graph *irg = get_irn_irg(block);
5697 /* the start block is never dead */
5698 if (block != get_irg_start_block(irg)
5699 && block != get_irg_end_block(irg)) {
5701 * Do NOT kill control flow without setting
5702 * the block to dead of bad things can happen:
5703 * We get a Block that is not reachable be irg_block_walk()
5704 * but can be found by irg_walk()!
5706 set_Block_dead(block);
5713 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5714 blocks predecessors is dead. */
5715 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5716 irn_arity = get_irn_arity(node);
5719 * Beware: we can only read the block of a non-floating node.
5721 if (is_irn_pinned_in_irg(node) &&
5722 is_Block_dead(get_nodes_block(skip_Proj(node))))
5725 for (i = 0; i < irn_arity; i++) {
5726 ir_node *pred = get_irn_n(node, i);
5731 /* Propagating Unknowns here seems to be a bad idea, because
5732 sometimes we need a node as a input and did not want that
5734 However, it might be useful to move this into a later phase
5735 (if you think that optimizing such code is useful). */
5736 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5737 return new_Unknown(get_irn_mode(node));
5742 /* With this code we violate the agreement that local_optimize
5743 only leaves Bads in Block, Phi and Tuple nodes. */
5744 /* If Block has only Bads as predecessors it's garbage. */
5745 /* If Phi has only Bads as predecessors it's garbage. */
5746 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5747 irn_arity = get_irn_arity(node);
5748 for (i = 0; i < irn_arity; i++) {
5749 if (!is_Bad(get_irn_n(node, i))) break;
5751 if (i == irn_arity) node = new_Bad();
5758 * These optimizations deallocate nodes from the obstack.
5759 * It can only be called if it is guaranteed that no other nodes
5760 * reference this one, i.e., right after construction of a node.
5762 * @param n The node to optimize
5764 * current_ir_graph must be set to the graph of the node!
5766 ir_node *optimize_node(ir_node *n) {
5769 ir_opcode iro = get_irn_opcode(n);
5771 /* Always optimize Phi nodes: part of the construction. */
5772 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5774 /* constant expression evaluation / constant folding */
5775 if (get_opt_constant_folding()) {
5776 /* neither constants nor Tuple values can be evaluated */
5777 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5778 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5779 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5780 /* try to evaluate */
5781 tv = computed_value(n);
5782 if (tv != tarval_bad) {
5784 ir_type *old_tp = get_irn_type(n);
5785 int i, arity = get_irn_arity(n);
5789 * Try to recover the type of the new expression.
5791 for (i = 0; i < arity && !old_tp; ++i)
5792 old_tp = get_irn_type(get_irn_n(n, i));
5795 * we MUST copy the node here temporary, because it's still needed
5796 * for DBG_OPT_CSTEVAL
5798 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5799 oldn = alloca(node_size);
5801 memcpy(oldn, n, node_size);
5802 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5804 /* ARG, copy the in array, we need it for statistics */
5805 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5807 /* note the inplace edges module */
5808 edges_node_deleted(n, current_ir_graph);
5810 /* evaluation was successful -- replace the node. */
5811 irg_kill_node(current_ir_graph, n);
5812 nw = new_Const(get_tarval_mode(tv), tv);
5814 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5815 set_Const_type(nw, old_tp);
5816 DBG_OPT_CSTEVAL(oldn, nw);
5817 tarval_enable_fp_ops(old_fp_mode);
5820 tarval_enable_fp_ops(old_fp_mode);
5824 /* remove unnecessary nodes */
5825 if (get_opt_algebraic_simplification() ||
5826 (iro == iro_Phi) || /* always optimize these nodes. */
5828 (iro == iro_Proj) ||
5829 (iro == iro_Block) ) /* Flags tested local. */
5830 n = equivalent_node(n);
5832 /* Common Subexpression Elimination.
5834 * Checks whether n is already available.
5835 * The block input is used to distinguish different subexpressions. Right
5836 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5837 * subexpressions within a block.
5840 n = identify_cons(current_ir_graph->value_table, n);
5843 edges_node_deleted(oldn, current_ir_graph);
5845 /* We found an existing, better node, so we can deallocate the old node. */
5846 irg_kill_node(current_ir_graph, oldn);
5850 /* Some more constant expression evaluation that does not allow to
5852 iro = get_irn_opcode(n);
5853 if (get_opt_algebraic_simplification() ||
5854 (iro == iro_Cond) ||
5855 (iro == iro_Proj)) /* Flags tested local. */
5856 n = transform_node(n);
5858 /* Remove nodes with dead (Bad) input.
5859 Run always for transformation induced Bads. */
5862 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5863 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5864 n = identify_remember(current_ir_graph->value_table, n);
5868 } /* optimize_node */
5872 * These optimizations never deallocate nodes (in place). This can cause dead
5873 * nodes lying on the obstack. Remove these by a dead node elimination,
5874 * i.e., a copying garbage collection.
5876 ir_node *optimize_in_place_2(ir_node *n) {
5879 ir_opcode iro = get_irn_opcode(n);
5881 if (!get_opt_optimize() && !is_Phi(n)) return n;
5883 /* constant expression evaluation / constant folding */
5884 if (get_opt_constant_folding()) {
5885 /* neither constants nor Tuple values can be evaluated */
5886 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5887 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5888 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5889 /* try to evaluate */
5890 tv = computed_value(n);
5891 if (tv != tarval_bad) {
5892 /* evaluation was successful -- replace the node. */
5893 ir_type *old_tp = get_irn_type(n);
5894 int i, arity = get_irn_arity(n);
5897 * Try to recover the type of the new expression.
5899 for (i = 0; i < arity && !old_tp; ++i)
5900 old_tp = get_irn_type(get_irn_n(n, i));
5902 n = new_Const(get_tarval_mode(tv), tv);
5904 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5905 set_Const_type(n, old_tp);
5907 DBG_OPT_CSTEVAL(oldn, n);
5908 tarval_enable_fp_ops(old_fp_mode);
5911 tarval_enable_fp_ops(old_fp_mode);
5915 /* remove unnecessary nodes */
5916 if (get_opt_constant_folding() ||
5917 (iro == iro_Phi) || /* always optimize these nodes. */
5918 (iro == iro_Id) || /* ... */
5919 (iro == iro_Proj) || /* ... */
5920 (iro == iro_Block) ) /* Flags tested local. */
5921 n = equivalent_node(n);
5923 /** common subexpression elimination **/
5924 /* Checks whether n is already available. */
5925 /* The block input is used to distinguish different subexpressions. Right
5926 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5927 subexpressions within a block. */
5928 if (get_opt_cse()) {
5929 n = identify_remember(current_ir_graph->value_table, n);
5932 /* Some more constant expression evaluation. */
5933 iro = get_irn_opcode(n);
5934 if (get_opt_constant_folding() ||
5935 (iro == iro_Cond) ||
5936 (iro == iro_Proj)) /* Flags tested local. */
5937 n = transform_node(n);
5939 /* Remove nodes with dead (Bad) input.
5940 Run always for transformation induced Bads. */
5943 /* Now we can verify the node, as it has no dead inputs any more. */
5946 /* Now we have a legal, useful node. Enter it in hash table for cse.
5947 Blocks should be unique anyways. (Except the successor of start:
5948 is cse with the start block!) */
5949 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5950 n = identify_remember(current_ir_graph->value_table, n);
5953 } /* optimize_in_place_2 */
5956 * Wrapper for external use, set proper status bits after optimization.
5958 ir_node *optimize_in_place(ir_node *n) {
5959 /* Handle graph state */
5960 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5962 if (get_opt_global_cse())
5963 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5964 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5965 set_irg_outs_inconsistent(current_ir_graph);
5967 /* FIXME: Maybe we could also test whether optimizing the node can
5968 change the control graph. */
5969 set_irg_doms_inconsistent(current_ir_graph);
5970 return optimize_in_place_2(n);
5971 } /* optimize_in_place */
5974 * Sets the default operation for an ir_ops.
5976 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5977 ops = firm_set_default_computed_value(code, ops);
5978 ops = firm_set_default_equivalent_node(code, ops);
5979 ops = firm_set_default_transform_node(code, ops);
5980 ops = firm_set_default_node_cmp_attr(code, ops);
5981 ops = firm_set_default_get_type(code, ops);
5982 ops = firm_set_default_get_type_attr(code, ops);
5983 ops = firm_set_default_get_entity_attr(code, ops);
5986 } /* firm_set_default_operations */