2 * Copyright (C) 1995-2007 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) && mode_is_signed(get_irn_mode(a)))
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 Rot.
423 static tarval *computed_value_Rot(ir_node *n) {
424 ir_node *a = get_Rot_left(n);
425 ir_node *b = get_Rot_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_rot (ta, tb);
434 } /* computed_value_Rot */
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) {
640 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641 value_of(get_Confirm_bound(n)) : tarval_bad;
642 } /* computed_value_Confirm */
645 * If the parameter n can be computed, return its value, else tarval_bad.
646 * Performs constant folding.
648 * @param n The node this should be evaluated
650 tarval *computed_value(ir_node *n) {
651 if (n->op->ops.computed_value)
652 return n->op->ops.computed_value(n);
654 } /* computed_value */
657 * Set the default computed_value evaluator in an ir_op_ops.
659 * @param code the opcode for the default operation
660 * @param ops the operations initialized
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
669 ops->computed_value = computed_value_##a; \
704 } /* firm_set_default_computed_value */
707 * Returns a equivalent block for another block.
708 * If the block has only one predecessor, this is
709 * the equivalent one. If the only predecessor of a block is
710 * the block itself, this is a dead block.
712 * If both predecessors of a block are the branches of a binary
713 * Cond, the equivalent block is Cond's block.
715 * If all predecessors of a block are bad or lies in a dead
716 * block, the current block is dead as well.
718 * Note, that blocks are NEVER turned into Bad's, instead
719 * the dead_block flag is set. So, never test for is_Bad(block),
720 * always use is_dead_Block(block).
722 static ir_node *equivalent_node_Block(ir_node *n)
725 int n_preds = get_Block_n_cfgpreds(n);
727 /* The Block constructor does not call optimize, but mature_immBlock
728 calls the optimization. */
729 assert(get_Block_matured(n));
731 /* Straightening: a single entry Block following a single exit Block
732 can be merged, if it is not the Start block. */
733 /* !!! Beware, all Phi-nodes of n must have been optimized away.
734 This should be true, as the block is matured before optimize is called.
735 But what about Phi-cycles with the Phi0/Id that could not be resolved?
736 Remaining Phi nodes are just Ids. */
737 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
738 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
739 if (predblock == oldn) {
740 /* Jmp jumps into the block it is in -- deal self cycle. */
741 n = set_Block_dead(n);
742 DBG_OPT_DEAD_BLOCK(oldn, n);
743 } else if (get_opt_control_flow_straightening()) {
745 DBG_OPT_STG(oldn, n);
747 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
748 ir_node *predblock = get_Block_cfgpred_block(n, 0);
749 if (predblock == oldn) {
750 /* Jmp jumps into the block it is in -- deal self cycle. */
751 n = set_Block_dead(n);
752 DBG_OPT_DEAD_BLOCK(oldn, n);
754 } else if ((n_preds == 2) &&
755 (get_opt_control_flow_weak_simplification())) {
756 /* Test whether Cond jumps twice to this block
757 * The more general case which more than 2 predecessors is handles
758 * in optimize_cf(), we handle only this special case for speed here.
760 ir_node *a = get_Block_cfgpred(n, 0);
761 ir_node *b = get_Block_cfgpred(n, 1);
765 (get_Proj_pred(a) == get_Proj_pred(b)) &&
766 is_Cond(get_Proj_pred(a)) &&
767 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
768 /* Also a single entry Block following a single exit Block. Phis have
769 twice the same operand and will be optimized away. */
770 n = get_nodes_block(get_Proj_pred(a));
771 DBG_OPT_IFSIM1(oldn, a, b, n);
773 } else if (get_opt_unreachable_code() &&
774 (n != get_irg_start_block(current_ir_graph)) &&
775 (n != get_irg_end_block(current_ir_graph)) ) {
778 /* If all inputs are dead, this block is dead too, except if it is
779 the start or end block. This is one step of unreachable code
781 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
782 ir_node *pred = get_Block_cfgpred(n, i);
785 if (is_Bad(pred)) continue;
786 pred_blk = get_nodes_block(skip_Proj(pred));
788 if (is_Block_dead(pred_blk)) continue;
791 /* really found a living input */
796 n = set_Block_dead(n);
797 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } /* equivalent_node_Block */
805 * Returns a equivalent node for a Jmp, a Bad :-)
806 * Of course this only happens if the Block of the Jmp is dead.
808 static ir_node *equivalent_node_Jmp(ir_node *n) {
809 /* unreachable code elimination */
810 if (is_Block_dead(get_nodes_block(n)))
814 } /* equivalent_node_Jmp */
816 /** Raise is handled in the same way as Jmp. */
817 #define equivalent_node_Raise equivalent_node_Jmp
820 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
821 See transform_node_Proj_Cond(). */
824 * Optimize operations that are commutative and have neutral 0,
825 * so a op 0 = 0 op a = a.
827 static ir_node *equivalent_node_neutral_zero(ir_node *n)
831 ir_node *a = get_binop_left(n);
832 ir_node *b = get_binop_right(n);
837 /* After running compute_node there is only one constant predecessor.
838 Find this predecessors value and remember the other node: */
839 if ((tv = value_of(a)) != tarval_bad) {
841 } else if ((tv = value_of(b)) != tarval_bad) {
846 /* If this predecessors constant value is zero, the operation is
847 * unnecessary. Remove it.
849 * Beware: If n is a Add, the mode of on and n might be different
850 * which happens in this rare construction: NULL + 3.
851 * Then, a Conv would be needed which we cannot include here.
853 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
860 } /* equivalent_node_neutral_zero */
863 * Eor is commutative and has neutral 0.
865 #define equivalent_node_Eor equivalent_node_neutral_zero
868 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
870 * The second one looks strange, but this construct
871 * is used heavily in the LCC sources :-).
873 * Beware: The Mode of an Add may be different than the mode of its
874 * predecessors, so we could not return a predecessors in all cases.
876 static ir_node *equivalent_node_Add(ir_node *n) {
878 ir_node *left, *right;
879 ir_mode *mode = get_irn_mode(n);
881 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
882 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
885 n = equivalent_node_neutral_zero(n);
889 left = get_Add_left(n);
890 right = get_Add_right(n);
893 if (get_Sub_right(left) == right) {
896 n = get_Sub_left(left);
897 if (mode == get_irn_mode(n)) {
898 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
904 if (get_Sub_right(right) == left) {
907 n = get_Sub_left(right);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915 } /* equivalent_node_Add */
918 * optimize operations that are not commutative but have neutral 0 on left,
921 static ir_node *equivalent_node_left_zero(ir_node *n) {
924 ir_node *a = get_binop_left(n);
925 ir_node *b = get_binop_right(n);
927 if (is_Const(b) && is_Const_null(b)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
933 } /* equivalent_node_left_zero */
935 #define equivalent_node_Shl equivalent_node_left_zero
936 #define equivalent_node_Shr equivalent_node_left_zero
937 #define equivalent_node_Shrs equivalent_node_left_zero
938 #define equivalent_node_Rot equivalent_node_left_zero
941 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
943 * The second one looks strange, but this construct
944 * is used heavily in the LCC sources :-).
946 * Beware: The Mode of a Sub may be different than the mode of its
947 * predecessors, so we could not return a predecessors in all cases.
949 static ir_node *equivalent_node_Sub(ir_node *n) {
952 ir_mode *mode = get_irn_mode(n);
954 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
955 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
958 b = get_Sub_right(n);
960 /* Beware: modes might be different */
961 if (is_Const(b) && is_Const_null(b)) {
962 ir_node *a = get_Sub_left(n);
963 if (mode == get_irn_mode(a)) {
966 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
970 } /* equivalent_node_Sub */
974 * Optimize an "idempotent unary op", ie op(op(n)) = n.
977 * -(-a) == a, but might overflow two times.
978 * We handle it anyway here but the better way would be a
979 * flag. This would be needed for Pascal for instance.
981 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
983 ir_node *pred = get_unop_op(n);
985 /* optimize symmetric unop */
986 if (get_irn_op(pred) == get_irn_op(n)) {
987 n = get_unop_op(pred);
988 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
991 } /* equivalent_node_idempotent_unop */
993 /** Optimize Not(Not(x)) == x. */
994 #define equivalent_node_Not equivalent_node_idempotent_unop
996 /** -(-x) == x ??? Is this possible or can --x raise an
997 out of bounds exception if min =! max? */
998 #define equivalent_node_Minus equivalent_node_idempotent_unop
1001 * Optimize a * 1 = 1 * a = a.
1003 static ir_node *equivalent_node_Mul(ir_node *n) {
1005 ir_node *a = get_Mul_left(n);
1007 /* we can handle here only the n * n = n bit cases */
1008 if (get_irn_mode(n) == get_irn_mode(a)) {
1009 ir_node *b = get_Mul_right(n);
1011 /* Mul is commutative and has again an other neutral element. */
1012 if (is_Const(a) && is_Const_one(a)) {
1014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1015 } else if (is_Const(b) && is_Const_one(b)) {
1017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1021 } /* equivalent_node_Mul */
1024 * Optimize a / 1 = a.
1026 static ir_node *equivalent_node_Div(ir_node *n) {
1027 ir_node *a = get_Div_left(n);
1028 ir_node *b = get_Div_right(n);
1030 /* Div is not commutative. */
1031 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1032 /* Turn Div into a tuple (mem, bad, a) */
1033 ir_node *mem = get_Div_mem(n);
1034 ir_node *blk = get_irn_n(n, -1);
1035 turn_into_tuple(n, pn_Div_max);
1036 set_Tuple_pred(n, pn_Div_M, mem);
1037 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1038 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1039 set_Tuple_pred(n, pn_Div_res, a);
1042 } /* equivalent_node_Div */
1045 * Optimize a / 1.0 = a.
1047 static ir_node *equivalent_node_Quot(ir_node *n) {
1048 ir_node *a = get_Quot_left(n);
1049 ir_node *b = get_Quot_right(n);
1051 /* Div is not commutative. */
1052 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1053 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1054 ir_node *mem = get_Quot_mem(n);
1055 ir_node *blk = get_irn_n(n, -1);
1056 turn_into_tuple(n, pn_Quot_max);
1057 set_Tuple_pred(n, pn_Quot_M, mem);
1058 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1059 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1060 set_Tuple_pred(n, pn_Quot_res, a);
1063 } /* equivalent_node_Quot */
1066 * Optimize a / 1 = a.
1068 static ir_node *equivalent_node_DivMod(ir_node *n) {
1069 ir_node *b = get_DivMod_right(n);
1071 /* Div is not commutative. */
1072 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1073 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1074 ir_node *a = get_DivMod_left(n);
1075 ir_node *mem = get_Div_mem(n);
1076 ir_node *blk = get_irn_n(n, -1);
1077 ir_mode *mode = get_DivMod_resmode(n);
1079 turn_into_tuple(n, pn_DivMod_max);
1080 set_Tuple_pred(n, pn_DivMod_M, mem);
1081 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1082 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1083 set_Tuple_pred(n, pn_DivMod_res_div, a);
1084 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1087 } /* equivalent_node_DivMod */
1090 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1092 static ir_node *equivalent_node_Or(ir_node *n) {
1095 ir_node *a = get_Or_left(n);
1096 ir_node *b = get_Or_right(n);
1099 n = a; /* Or has it's own neutral element */
1100 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1101 } else if (is_Const(a) && is_Const_null(a)) {
1103 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1104 } else if (is_Const(b) && is_Const_null(b)) {
1106 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } /* equivalent_node_Or */
1113 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1115 static ir_node *equivalent_node_And(ir_node *n) {
1118 ir_node *a = get_And_left(n);
1119 ir_node *b = get_And_right(n);
1122 n = a; /* And has it's own neutral element */
1123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1126 if (is_Const(a) && is_Const_all_one(a)) {
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1131 if (is_Const(b) && is_Const_all_one(b)) {
1133 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1145 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1148 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1154 } /* equivalent_node_And */
1157 * Try to remove useless Conv's:
1159 static ir_node *equivalent_node_Conv(ir_node *n) {
1161 ir_node *a = get_Conv_op(n);
1164 ir_mode *n_mode = get_irn_mode(n);
1165 ir_mode *a_mode = get_irn_mode(a);
1167 if (n_mode == a_mode) { /* No Conv necessary */
1168 if (get_Conv_strict(n)) {
1169 /* special case: the predecessor might be a also a Conv */
1171 if (! get_Conv_strict(a)) {
1172 /* first one is not strict, kick it */
1173 set_Conv_op(n, get_Conv_op(a));
1176 /* else both are strict conv, second is superflous */
1177 } else if(is_Proj(a)) {
1178 ir_node *pred = get_Proj_pred(a);
1180 /* loads always return with the exact precision of n_mode */
1181 assert(get_Load_mode(pred) == n_mode);
1186 /* leave strict floating point Conv's */
1190 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1191 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1195 n_mode = get_irn_mode(n);
1196 b_mode = get_irn_mode(b);
1198 if (n_mode == b_mode) {
1199 if (n_mode == mode_b) {
1200 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1201 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1202 } else if (mode_is_int(n_mode)) {
1203 if (smaller_mode(b_mode, a_mode)){
1204 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1205 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1211 } /* equivalent_node_Conv */
1214 * A Cast may be removed if the type of the previous node
1215 * is already the type of the Cast.
1217 static ir_node *equivalent_node_Cast(ir_node *n) {
1219 ir_node *pred = get_Cast_op(n);
1221 if (get_irn_type(pred) == get_Cast_type(n)) {
1223 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1226 } /* equivalent_node_Cast */
1229 * Several optimizations:
1230 * - no Phi in start block.
1231 * - remove Id operators that are inputs to Phi
1232 * - fold Phi-nodes, iff they have only one predecessor except
1235 static ir_node *equivalent_node_Phi(ir_node *n) {
1239 ir_node *block = NULL; /* to shutup gcc */
1240 ir_node *first_val = NULL; /* to shutup gcc */
1242 if (!get_opt_normalize()) return n;
1244 n_preds = get_Phi_n_preds(n);
1246 block = get_nodes_block(n);
1247 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1248 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1249 if ((is_Block_dead(block)) || /* Control dead */
1250 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1251 return new_Bad(); /* in the Start Block. */
1253 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1255 /* If the Block has a Bad pred, we also have one. */
1256 for (i = 0; i < n_preds; ++i)
1257 if (is_Bad(get_Block_cfgpred(block, i)))
1258 set_Phi_pred(n, i, new_Bad());
1260 /* Find first non-self-referencing input */
1261 for (i = 0; i < n_preds; ++i) {
1262 first_val = get_Phi_pred(n, i);
1263 if ( (first_val != n) /* not self pointer */
1265 && (! is_Bad(first_val))
1267 ) { /* value not dead */
1268 break; /* then found first value. */
1273 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1277 /* search for rest of inputs, determine if any of these
1278 are non-self-referencing */
1279 while (++i < n_preds) {
1280 ir_node *scnd_val = get_Phi_pred(n, i);
1281 if ( (scnd_val != n)
1282 && (scnd_val != first_val)
1284 && (! is_Bad(scnd_val))
1292 /* Fold, if no multiple distinct non-self-referencing inputs */
1294 DBG_OPT_PHI(oldn, n);
1297 } /* equivalent_node_Phi */
1300 * Several optimizations:
1301 * - no Sync in start block.
1302 * - fold Sync-nodes, iff they have only one predecessor except
1305 static ir_node *equivalent_node_Sync(ir_node *n) {
1309 ir_node *first_val = NULL; /* to shutup gcc */
1311 if (!get_opt_normalize()) return n;
1313 n_preds = get_Sync_n_preds(n);
1315 /* Find first non-self-referencing input */
1316 for (i = 0; i < n_preds; ++i) {
1317 first_val = get_Sync_pred(n, i);
1318 if ((first_val != n) /* not self pointer */ &&
1319 (! is_Bad(first_val))
1320 ) { /* value not dead */
1321 break; /* then found first value. */
1326 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1329 /* search the rest of inputs, determine if any of these
1330 are non-self-referencing */
1331 while (++i < n_preds) {
1332 ir_node *scnd_val = get_Sync_pred(n, i);
1333 if ((scnd_val != n) &&
1334 (scnd_val != first_val) &&
1335 (! is_Bad(scnd_val))
1341 /* Fold, if no multiple distinct non-self-referencing inputs */
1343 DBG_OPT_SYNC(oldn, n);
1346 } /* equivalent_node_Sync */
1349 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1350 * ProjX(Load) and ProjX(Store).
1352 static ir_node *equivalent_node_Proj(ir_node *proj) {
1353 ir_node *oldn = proj;
1354 ir_node *a = get_Proj_pred(proj);
1357 /* Remove the Tuple/Proj combination. */
1358 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1359 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1360 DBG_OPT_TUPLE(oldn, a, proj);
1362 /* This should not happen! */
1363 assert(! "found a Proj with higher number than Tuple predecessors");
1366 } else if (get_irn_mode(proj) == mode_X) {
1367 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1368 /* Remove dead control flow -- early gigo(). */
1370 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1372 /* get the Load address */
1373 ir_node *addr = get_Load_ptr(a);
1374 ir_node *blk = get_irn_n(a, -1);
1377 if (value_not_null(addr, &confirm)) {
1378 if (confirm == NULL) {
1379 /* this node may float if it did not depend on a Confirm */
1380 set_irn_pinned(a, op_pin_state_floats);
1382 if (get_Proj_proj(proj) == pn_Load_X_except) {
1383 DBG_OPT_EXC_REM(proj);
1386 return new_r_Jmp(current_ir_graph, blk);
1388 } else if (is_Store(a)) {
1389 /* get the load/store address */
1390 ir_node *addr = get_Store_ptr(a);
1391 ir_node *blk = get_irn_n(a, -1);
1394 if (value_not_null(addr, &confirm)) {
1395 if (confirm == NULL) {
1396 /* this node may float if it did not depend on a Confirm */
1397 set_irn_pinned(a, op_pin_state_floats);
1399 if (get_Proj_proj(proj) == pn_Store_X_except) {
1400 DBG_OPT_EXC_REM(proj);
1403 return new_r_Jmp(current_ir_graph, blk);
1410 } /* equivalent_node_Proj */
1415 static ir_node *equivalent_node_Id(ir_node *n) {
1420 } while (get_irn_op(n) == op_Id);
1422 DBG_OPT_ID(oldn, n);
1424 } /* equivalent_node_Id */
1429 static ir_node *equivalent_node_Mux(ir_node *n)
1431 ir_node *oldn = n, *sel = get_Mux_sel(n);
1432 tarval *ts = value_of(sel);
1434 /* Mux(true, f, t) == t */
1435 if (ts == tarval_b_true) {
1436 n = get_Mux_true(n);
1437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1439 /* Mux(false, f, t) == f */
1440 else if (ts == tarval_b_false) {
1441 n = get_Mux_false(n);
1442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1444 /* Mux(v, x, x) == x */
1445 else if (get_Mux_false(n) == get_Mux_true(n)) {
1446 n = get_Mux_true(n);
1447 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1449 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1450 ir_node *cmp = get_Proj_pred(sel);
1451 long proj_nr = get_Proj_proj(sel);
1452 ir_node *b = get_Mux_false(n);
1453 ir_node *a = get_Mux_true(n);
1456 * Note: normalization puts the constant on the right site,
1457 * so we check only one case.
1459 * Note further that these optimization work even for floating point
1460 * with NaN's because -NaN == NaN.
1461 * However, if +0 and -0 is handled differently, we cannot use the first one.
1463 if (is_Cmp(cmp) && get_Cmp_left(cmp) == a) {
1464 ir_node *cmp_r = get_Cmp_right(cmp);
1465 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1466 /* Mux(a CMP 0, X, a) */
1467 if (is_Minus(b) && get_Minus_op(b) == a) {
1468 /* Mux(a CMP 0, -a, a) */
1469 if (proj_nr == pn_Cmp_Eq) {
1470 /* Mux(a == 0, -a, a) ==> -a */
1472 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1473 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1474 /* Mux(a != 0, -a, a) ==> a */
1476 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1478 } else if (is_Const(b) && is_Const_null(b)) {
1479 /* Mux(a CMP 0, 0, a) */
1480 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1481 /* Mux(a != 0, 0, a) ==> a */
1483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1484 } else if (proj_nr == pn_Cmp_Eq) {
1485 /* Mux(a == 0, 0, a) ==> 0 */
1487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1494 } /* equivalent_node_Mux */
1497 * Returns a equivalent node of a Psi: if a condition is true
1498 * and all previous conditions are false we know its value.
1499 * If all conditions are false its value is the default one.
1501 static ir_node *equivalent_node_Psi(ir_node *n) {
1503 return equivalent_node_Mux(n);
1505 } /* equivalent_node_Psi */
1508 * Optimize -a CMP -b into b CMP a.
1509 * This works only for for modes where unary Minus
1511 * Note that two-complement integers can Overflow
1512 * so it will NOT work.
1514 * For == and != can be handled in Proj(Cmp)
1516 static ir_node *equivalent_node_Cmp(ir_node *n) {
1517 ir_node *left = get_Cmp_left(n);
1518 ir_node *right = get_Cmp_right(n);
1520 if (is_Minus(left) && is_Minus(right) &&
1521 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1522 left = get_Minus_op(left);
1523 right = get_Minus_op(right);
1524 set_Cmp_left(n, right);
1525 set_Cmp_right(n, left);
1528 } /* equivalent_node_Cmp */
1531 * Remove Confirm nodes if setting is on.
1532 * Replace Confirms(x, '=', Constlike) by Constlike.
1534 static ir_node *equivalent_node_Confirm(ir_node *n) {
1535 ir_node *pred = get_Confirm_value(n);
1536 pn_Cmp pnc = get_Confirm_cmp(n);
1538 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1540 * rare case: two identical Confirms one after another,
1541 * replace the second one with the first.
1545 if (pnc == pn_Cmp_Eq) {
1546 ir_node *bound = get_Confirm_bound(n);
1549 * Optimize a rare case:
1550 * Confirm(x, '=', Constlike) ==> Constlike
1552 if (is_irn_constlike(bound)) {
1553 DBG_OPT_CONFIRM(n, bound);
1557 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1561 * Optimize CopyB(mem, x, x) into a Nop.
1563 static ir_node *equivalent_node_CopyB(ir_node *n) {
1564 ir_node *a = get_CopyB_dst(n);
1565 ir_node *b = get_CopyB_src(n);
1568 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1569 ir_node *mem = get_CopyB_mem(n);
1570 ir_node *blk = get_nodes_block(n);
1571 turn_into_tuple(n, pn_CopyB_max);
1572 set_Tuple_pred(n, pn_CopyB_M, mem);
1573 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1574 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1575 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1578 } /* equivalent_node_CopyB */
1581 * Optimize Bounds(idx, idx, upper) into idx.
1583 static ir_node *equivalent_node_Bound(ir_node *n) {
1584 ir_node *idx = get_Bound_index(n);
1585 ir_node *lower = get_Bound_lower(n);
1588 /* By definition lower < upper, so if idx == lower -->
1589 lower <= idx && idx < upper */
1591 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1594 ir_node *pred = skip_Proj(idx);
1596 if (get_irn_op(pred) == op_Bound) {
1598 * idx was Bounds_check previously, it is still valid if
1599 * lower <= pred_lower && pred_upper <= upper.
1601 ir_node *upper = get_Bound_upper(n);
1602 if (get_Bound_lower(pred) == lower &&
1603 get_Bound_upper(pred) == upper) {
1605 * One could expect that we simply return the previous
1606 * Bound here. However, this would be wrong, as we could
1607 * add an exception Proj to a new location then.
1608 * So, we must turn in into a tuple.
1615 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1616 ir_node *mem = get_Bound_mem(n);
1617 ir_node *blk = get_nodes_block(n);
1618 turn_into_tuple(n, pn_Bound_max);
1619 set_Tuple_pred(n, pn_Bound_M, mem);
1620 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1621 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1622 set_Tuple_pred(n, pn_Bound_res, idx);
1625 } /* equivalent_node_Bound */
1628 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1629 * perform no actual computation, as, e.g., the Id nodes. It does not create
1630 * new nodes. It is therefore safe to free n if the node returned is not n.
1631 * If a node returns a Tuple we can not just skip it. If the size of the
1632 * in array fits, we transform n into a tuple (e.g., Div).
1634 ir_node *equivalent_node(ir_node *n) {
1635 if (n->op->ops.equivalent_node)
1636 return n->op->ops.equivalent_node(n);
1638 } /* equivalent_node */
1641 * Sets the default equivalent node operation for an ir_op_ops.
1643 * @param code the opcode for the default operation
1644 * @param ops the operations initialized
1649 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1653 ops->equivalent_node = equivalent_node_##a; \
1693 } /* firm_set_default_equivalent_node */
1696 * Returns non-zero if a node is a Phi node
1697 * with all predecessors constant.
1699 static int is_const_Phi(ir_node *n) {
1704 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1705 if (! is_Const(get_irn_n(n, i)))
1708 } /* is_const_Phi */
1711 * Apply an evaluator on a binop with a constant operators (and one Phi).
1713 * @param phi the Phi node
1714 * @param other the other operand
1715 * @param eval an evaluator function
1716 * @param mode the mode of the result, may be different from the mode of the Phi!
1717 * @param left if non-zero, other is the left operand, else the right
1719 * @return a new Phi node if the conversion was successful, NULL else
1721 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1726 int i, n = get_irn_arity(phi);
1728 NEW_ARR_A(void *, res, n);
1730 for (i = 0; i < n; ++i) {
1731 pred = get_irn_n(phi, i);
1732 tv = get_Const_tarval(pred);
1733 tv = eval(other, tv);
1735 if (tv == tarval_bad) {
1736 /* folding failed, bad */
1742 for (i = 0; i < n; ++i) {
1743 pred = get_irn_n(phi, i);
1744 tv = get_Const_tarval(pred);
1745 tv = eval(tv, other);
1747 if (tv == tarval_bad) {
1748 /* folding failed, bad */
1754 irg = current_ir_graph;
1755 for (i = 0; i < n; ++i) {
1756 pred = get_irn_n(phi, i);
1757 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1758 mode, res[i], get_Const_type(pred));
1760 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1761 } /* apply_binop_on_phi */
1764 * Apply an evaluator on a unop with a constant operator (a Phi).
1766 * @param phi the Phi node
1767 * @param eval an evaluator function
1769 * @return a new Phi node if the conversion was successful, NULL else
1771 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1777 int i, n = get_irn_arity(phi);
1779 NEW_ARR_A(void *, res, n);
1780 for (i = 0; i < n; ++i) {
1781 pred = get_irn_n(phi, i);
1782 tv = get_Const_tarval(pred);
1785 if (tv == tarval_bad) {
1786 /* folding failed, bad */
1791 mode = get_irn_mode(phi);
1792 irg = current_ir_graph;
1793 for (i = 0; i < n; ++i) {
1794 pred = get_irn_n(phi, i);
1795 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1796 mode, res[i], get_Const_type(pred));
1798 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1799 } /* apply_unop_on_phi */
1802 * Apply a conversion on a constant operator (a Phi).
1804 * @param phi the Phi node
1806 * @return a new Phi node if the conversion was successful, NULL else
1808 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1813 int i, n = get_irn_arity(phi);
1815 NEW_ARR_A(void *, res, n);
1816 for (i = 0; i < n; ++i) {
1817 pred = get_irn_n(phi, i);
1818 tv = get_Const_tarval(pred);
1819 tv = tarval_convert_to(tv, mode);
1821 if (tv == tarval_bad) {
1822 /* folding failed, bad */
1827 irg = current_ir_graph;
1828 for (i = 0; i < n; ++i) {
1829 pred = get_irn_n(phi, i);
1830 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1831 mode, res[i], get_Const_type(pred));
1833 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1834 } /* apply_conv_on_phi */
1837 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1838 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1839 * If possible, remove the Conv's.
1841 static ir_node *transform_node_AddSub(ir_node *n) {
1842 ir_mode *mode = get_irn_mode(n);
1844 if (mode_is_reference(mode)) {
1845 ir_node *left = get_binop_left(n);
1846 ir_node *right = get_binop_right(n);
1847 int ref_bits = get_mode_size_bits(mode);
1849 if (is_Conv(left)) {
1850 ir_mode *mode = get_irn_mode(left);
1851 int bits = get_mode_size_bits(mode);
1853 if (ref_bits == bits &&
1854 mode_is_int(mode) &&
1855 get_mode_arithmetic(mode) == irma_twos_complement) {
1856 ir_node *pre = get_Conv_op(left);
1857 ir_mode *pre_mode = get_irn_mode(pre);
1859 if (mode_is_int(pre_mode) &&
1860 get_mode_size_bits(pre_mode) == bits &&
1861 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1862 /* ok, this conv just changes to sign, moreover the calculation
1863 * is done with same number of bits as our address mode, so
1864 * we can ignore the conv as address calculation can be viewed
1865 * as either signed or unsigned
1867 set_binop_left(n, pre);
1872 if (is_Conv(right)) {
1873 ir_mode *mode = get_irn_mode(right);
1874 int bits = get_mode_size_bits(mode);
1876 if (ref_bits == bits &&
1877 mode_is_int(mode) &&
1878 get_mode_arithmetic(mode) == irma_twos_complement) {
1879 ir_node *pre = get_Conv_op(right);
1880 ir_mode *pre_mode = get_irn_mode(pre);
1882 if (mode_is_int(pre_mode) &&
1883 get_mode_size_bits(pre_mode) == bits &&
1884 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1885 /* ok, this conv just changes to sign, moreover the calculation
1886 * is done with same number of bits as our address mode, so
1887 * we can ignore the conv as address calculation can be viewed
1888 * as either signed or unsigned
1890 set_binop_right(n, pre);
1896 } /* transform_node_AddSub */
1898 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1900 if (is_Const(b) && is_const_Phi(a)) { \
1901 /* check for Op(Phi, Const) */ \
1902 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1904 else if (is_Const(a) && is_const_Phi(b)) { \
1905 /* check for Op(Const, Phi) */ \
1906 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1909 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1913 #define HANDLE_UNOP_PHI(eval, a, c) \
1915 if (is_const_Phi(a)) { \
1916 /* check for Op(Phi) */ \
1917 c = apply_unop_on_phi(a, eval); \
1919 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1925 * Do the AddSub optimization, then Transform
1926 * Constant folding on Phi
1927 * Add(a,a) -> Mul(a, 2)
1928 * Add(Mul(a, x), a) -> Mul(a, x+1)
1929 * if the mode is integer or float.
1930 * Transform Add(a,-b) into Sub(a,b).
1931 * Reassociation might fold this further.
1933 static ir_node *transform_node_Add(ir_node *n) {
1935 ir_node *a, *b, *c, *oldn = n;
1937 n = transform_node_AddSub(n);
1939 a = get_Add_left(n);
1940 b = get_Add_right(n);
1942 mode = get_irn_mode(n);
1943 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
1945 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1946 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1949 if (mode_is_num(mode)) {
1950 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1951 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1952 ir_node *block = get_irn_n(n, -1);
1955 get_irn_dbg_info(n),
1959 new_r_Const_long(current_ir_graph, block, mode, 2),
1961 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1966 get_irn_dbg_info(n),
1972 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1977 get_irn_dbg_info(n),
1983 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1986 if (! is_reassoc_running()) {
1987 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1989 ir_node *ma = get_Mul_left(a);
1990 ir_node *mb = get_Mul_right(a);
1993 ir_node *blk = get_irn_n(n, -1);
1995 get_irn_dbg_info(n), current_ir_graph, blk,
1998 get_irn_dbg_info(n), current_ir_graph, blk,
2000 new_r_Const_long(current_ir_graph, blk, mode, 1),
2003 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2005 } else if (b == mb) {
2006 ir_node *blk = get_irn_n(n, -1);
2008 get_irn_dbg_info(n), current_ir_graph, blk,
2011 get_irn_dbg_info(n), current_ir_graph, blk,
2013 new_r_Const_long(current_ir_graph, blk, mode, 1),
2016 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2021 ir_node *ma = get_Mul_left(b);
2022 ir_node *mb = get_Mul_right(b);
2025 ir_node *blk = get_irn_n(n, -1);
2027 get_irn_dbg_info(n), current_ir_graph, blk,
2030 get_irn_dbg_info(n), current_ir_graph, blk,
2032 new_r_Const_long(current_ir_graph, blk, mode, 1),
2035 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2039 ir_node *blk = get_irn_n(n, -1);
2041 get_irn_dbg_info(n), current_ir_graph, blk,
2044 get_irn_dbg_info(n), current_ir_graph, blk,
2046 new_r_Const_long(current_ir_graph, blk, mode, 1),
2049 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2054 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2055 /* Here we rely on constants be on the RIGHT side */
2057 ir_node *op = get_Not_op(a);
2059 if (is_Const(b) && is_Const_one(b)) {
2061 ir_node *blk = get_irn_n(n, -1);
2062 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2063 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2068 ir_node *blk = get_irn_n(n, -1);
2069 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2070 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2075 ir_node *op = get_Not_op(b);
2079 ir_node *blk = get_irn_n(n, -1);
2080 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2081 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2088 } /* transform_node_Add */
2091 * returns -cnst or NULL if impossible
2093 static ir_node *const_negate(ir_node *cnst) {
2094 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2095 dbg_info *dbgi = get_irn_dbg_info(cnst);
2096 ir_graph *irg = get_irn_irg(cnst);
2097 ir_node *block = get_nodes_block(cnst);
2098 ir_mode *mode = get_irn_mode(cnst);
2099 if (tv == tarval_bad) return NULL;
2100 return new_rd_Const(dbgi, irg, block, mode, tv);
2104 * Do the AddSub optimization, then Transform
2105 * Constant folding on Phi
2106 * Sub(0,a) -> Minus(a)
2107 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2108 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2109 * Sub(Add(a, x), x) -> a
2110 * Sub(x, Add(x, a)) -> -a
2111 * Sub(x, Const) -> Add(x, -Const)
2113 static ir_node *transform_node_Sub(ir_node *n) {
2118 n = transform_node_AddSub(n);
2120 a = get_Sub_left(n);
2121 b = get_Sub_right(n);
2123 mode = get_irn_mode(n);
2126 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2128 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2129 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2132 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2133 /* a - C -> a + (-C) */
2134 ir_node *cnst = const_negate(b);
2136 ir_node *block = get_nodes_block(n);
2137 dbg_info *dbgi = get_irn_dbg_info(n);
2138 ir_graph *irg = get_irn_irg(n);
2140 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2146 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2147 ir_graph *irg = current_ir_graph;
2148 dbg_info *dbg = get_irn_dbg_info(n);
2149 ir_node *block = get_nodes_block(n);
2150 ir_node *left = get_Minus_op(a);
2151 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2153 n = new_rd_Minus(dbg, irg, block, add, mode);
2154 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2156 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2157 ir_graph *irg = current_ir_graph;
2158 dbg_info *dbg = get_irn_dbg_info(n);
2159 ir_node *block = get_nodes_block(n);
2160 ir_node *right = get_Minus_op(b);
2162 n = new_rd_Add(dbg, irg, block, a, right, mode);
2163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2165 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2166 ir_graph *irg = current_ir_graph;
2167 dbg_info *s_dbg = get_irn_dbg_info(b);
2168 ir_node *s_block = get_nodes_block(b);
2169 ir_node *s_left = get_Sub_right(b);
2170 ir_node *s_right = get_Sub_left(b);
2171 ir_mode *s_mode = get_irn_mode(b);
2172 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2173 dbg_info *a_dbg = get_irn_dbg_info(n);
2174 ir_node *a_block = get_nodes_block(n);
2176 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2179 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2180 ir_node *m_right = get_Mul_right(b);
2181 if (is_Const(m_right)) {
2182 ir_node *cnst2 = const_negate(m_right);
2183 if (cnst2 != NULL) {
2184 ir_graph *irg = current_ir_graph;
2185 dbg_info *m_dbg = get_irn_dbg_info(b);
2186 ir_node *m_block = get_nodes_block(b);
2187 ir_node *m_left = get_Mul_left(b);
2188 ir_mode *m_mode = get_irn_mode(b);
2189 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2190 dbg_info *a_dbg = get_irn_dbg_info(n);
2191 ir_node *a_block = get_nodes_block(n);
2193 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2200 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2201 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2203 get_irn_dbg_info(n),
2208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2212 if (mode_wrap_around(mode)) {
2213 ir_node *left = get_Add_left(a);
2214 ir_node *right = get_Add_right(a);
2216 /* FIXME: Does the Conv's work only for two complement or generally? */
2218 if (mode != get_irn_mode(right)) {
2219 /* This Sub is an effective Cast */
2220 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2223 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2225 } else if (right == b) {
2226 if (mode != get_irn_mode(left)) {
2227 /* This Sub is an effective Cast */
2228 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2231 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2237 if (mode_wrap_around(mode)) {
2238 ir_node *left = get_Add_left(b);
2239 ir_node *right = get_Add_right(b);
2241 /* FIXME: Does the Conv's work only for two complement or generally? */
2243 ir_mode *r_mode = get_irn_mode(right);
2245 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2246 if (mode != r_mode) {
2247 /* This Sub is an effective Cast */
2248 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2250 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2252 } else if (right == a) {
2253 ir_mode *l_mode = get_irn_mode(left);
2255 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2256 if (mode != l_mode) {
2257 /* This Sub is an effective Cast */
2258 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2260 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2265 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2266 ir_mode *mode = get_irn_mode(a);
2268 if (mode == get_irn_mode(b)) {
2274 /* check if it's allowed to skip the conv */
2275 ma = get_irn_mode(a);
2276 mb = get_irn_mode(b);
2278 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2279 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2281 set_Sub_right(n, b);
2287 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2288 if (!is_reassoc_running() && is_Mul(a)) {
2289 ir_node *ma = get_Mul_left(a);
2290 ir_node *mb = get_Mul_right(a);
2293 ir_node *blk = get_irn_n(n, -1);
2295 get_irn_dbg_info(n),
2296 current_ir_graph, blk,
2299 get_irn_dbg_info(n),
2300 current_ir_graph, blk,
2302 new_r_Const_long(current_ir_graph, blk, mode, 1),
2305 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2307 } else if (mb == b) {
2308 ir_node *blk = get_irn_n(n, -1);
2310 get_irn_dbg_info(n),
2311 current_ir_graph, blk,
2314 get_irn_dbg_info(n),
2315 current_ir_graph, blk,
2317 new_r_Const_long(current_ir_graph, blk, mode, 1),
2320 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2324 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2325 ir_node *x = get_Sub_left(a);
2326 ir_node *y = get_Sub_right(a);
2327 ir_node *blk = get_irn_n(n, -1);
2328 ir_mode *m_b = get_irn_mode(b);
2329 ir_mode *m_y = get_irn_mode(y);
2333 /* Determine the right mode for the Add. */
2336 else if (mode_is_reference(m_b))
2338 else if (mode_is_reference(m_y))
2342 * Both modes are different but none is reference,
2343 * happens for instance in SubP(SubP(P, Iu), Is).
2344 * We have two possibilities here: Cast or ignore.
2345 * Currently we ignore this case.
2350 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2352 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2353 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2357 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2358 if (is_Const(a) && is_Not(b)) {
2359 /* c - ~X = X + (c+1) */
2360 tarval *tv = get_Const_tarval(a);
2362 tv = tarval_add(tv, get_mode_one(mode));
2363 if (tv != tarval_bad) {
2364 ir_node *blk = get_irn_n(n, -1);
2365 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2366 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2367 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2373 } /* transform_node_Sub */
2376 * Several transformation done on n*n=2n bits mul.
2377 * These transformations must be done here because new nodes may be produced.
2379 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2381 ir_node *a = get_Mul_left(n);
2382 ir_node *b = get_Mul_right(n);
2383 tarval *ta = value_of(a);
2384 tarval *tb = value_of(b);
2385 ir_mode *smode = get_irn_mode(a);
2387 if (ta == get_mode_one(smode)) {
2388 ir_node *blk = get_irn_n(n, -1);
2389 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2390 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2393 else if (ta == get_mode_minus_one(smode)) {
2394 ir_node *blk = get_irn_n(n, -1);
2395 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2396 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2397 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2400 if (tb == get_mode_one(smode)) {
2401 ir_node *blk = get_irn_n(a, -1);
2402 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2403 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2406 else if (tb == get_mode_minus_one(smode)) {
2407 ir_node *blk = get_irn_n(n, -1);
2408 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2409 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2410 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2417 * Transform Mul(a,-1) into -a.
2418 * Do constant evaluation of Phi nodes.
2419 * Do architecture dependent optimizations on Mul nodes
2421 static ir_node *transform_node_Mul(ir_node *n) {
2422 ir_node *c, *oldn = n;
2423 ir_mode *mode = get_irn_mode(n);
2424 ir_node *a = get_Mul_left(n);
2425 ir_node *b = get_Mul_right(n);
2427 if (is_Bad(a) || is_Bad(b))
2430 if (mode != get_irn_mode(a))
2431 return transform_node_Mul2n(n, mode);
2433 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2435 if (mode_is_signed(mode)) {
2438 if (value_of(a) == get_mode_minus_one(mode))
2440 else if (value_of(b) == get_mode_minus_one(mode))
2443 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2444 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2449 if (is_Const(b)) { /* (-a) * const -> a * -const */
2450 ir_node *cnst = const_negate(b);
2452 dbg_info *dbgi = get_irn_dbg_info(n);
2453 ir_node *block = get_nodes_block(n);
2454 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2455 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2458 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2459 dbg_info *dbgi = get_irn_dbg_info(n);
2460 ir_node *block = get_nodes_block(n);
2461 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2462 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2464 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2465 ir_node *sub_l = get_Sub_left(b);
2466 ir_node *sub_r = get_Sub_right(b);
2467 dbg_info *dbgi = get_irn_dbg_info(n);
2468 ir_graph *irg = current_ir_graph;
2469 ir_node *block = get_nodes_block(n);
2470 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2471 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2472 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2475 } else if (is_Minus(b)) {
2476 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2477 ir_node *sub_l = get_Sub_left(a);
2478 ir_node *sub_r = get_Sub_right(a);
2479 dbg_info *dbgi = get_irn_dbg_info(n);
2480 ir_graph *irg = current_ir_graph;
2481 ir_node *block = get_nodes_block(n);
2482 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2483 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2484 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2488 if (get_mode_arithmetic(mode) == irma_ieee754) {
2490 tarval *tv = get_Const_tarval(a);
2491 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2492 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2493 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2497 else if (is_Const(b)) {
2498 tarval *tv = get_Const_tarval(b);
2499 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2500 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2501 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2506 return arch_dep_replace_mul_with_shifts(n);
2507 } /* transform_node_Mul */
2510 * Transform a Div Node.
2512 static ir_node *transform_node_Div(ir_node *n) {
2513 tarval *tv = value_of(n);
2514 ir_mode *mode = get_Div_resmode(n);
2517 if (tv != tarval_bad) {
2518 value = new_Const(get_tarval_mode(tv), tv);
2520 DBG_OPT_CSTEVAL(n, value);
2523 ir_node *a = get_Div_left(n);
2524 ir_node *b = get_Div_right(n);
2527 if (a == b && value_not_zero(a, &dummy)) {
2528 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2529 value = new_Const(mode, get_mode_one(mode));
2530 DBG_OPT_CSTEVAL(n, value);
2533 if (mode_is_signed(mode) && is_Const(b)) {
2534 tarval *tv = get_Const_tarval(b);
2536 if (tv == get_mode_minus_one(mode)) {
2538 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2539 DBG_OPT_CSTEVAL(n, value);
2543 /* Try architecture dependent optimization */
2544 value = arch_dep_replace_div_by_const(n);
2552 /* Turn Div into a tuple (mem, jmp, bad, value) */
2553 mem = get_Div_mem(n);
2554 blk = get_irn_n(n, -1);
2556 /* skip a potential Pin */
2558 mem = get_Pin_op(mem);
2559 turn_into_tuple(n, pn_Div_max);
2560 set_Tuple_pred(n, pn_Div_M, mem);
2561 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2562 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2563 set_Tuple_pred(n, pn_Div_res, value);
2566 } /* transform_node_Div */
2569 * Transform a Mod node.
2571 static ir_node *transform_node_Mod(ir_node *n) {
2572 tarval *tv = value_of(n);
2573 ir_mode *mode = get_Mod_resmode(n);
2576 if (tv != tarval_bad) {
2577 value = new_Const(get_tarval_mode(tv), tv);
2579 DBG_OPT_CSTEVAL(n, value);
2582 ir_node *a = get_Mod_left(n);
2583 ir_node *b = get_Mod_right(n);
2586 if (a == b && value_not_zero(a, &dummy)) {
2587 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2588 value = new_Const(mode, get_mode_null(mode));
2589 DBG_OPT_CSTEVAL(n, value);
2592 if (mode_is_signed(mode) && is_Const(b)) {
2593 tarval *tv = get_Const_tarval(b);
2595 if (tv == get_mode_minus_one(mode)) {
2597 value = new_Const(mode, get_mode_null(mode));
2598 DBG_OPT_CSTEVAL(n, value);
2602 /* Try architecture dependent optimization */
2603 value = arch_dep_replace_mod_by_const(n);
2611 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2612 mem = get_Mod_mem(n);
2613 blk = get_irn_n(n, -1);
2615 /* skip a potential Pin */
2617 mem = get_Pin_op(mem);
2618 turn_into_tuple(n, pn_Mod_max);
2619 set_Tuple_pred(n, pn_Mod_M, mem);
2620 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2621 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2622 set_Tuple_pred(n, pn_Mod_res, value);
2625 } /* transform_node_Mod */
2628 * Transform a DivMod node.
2630 static ir_node *transform_node_DivMod(ir_node *n) {
2632 ir_node *a = get_DivMod_left(n);
2633 ir_node *b = get_DivMod_right(n);
2634 ir_mode *mode = get_DivMod_resmode(n);
2635 tarval *ta = value_of(a);
2636 tarval *tb = value_of(b);
2639 if (tb != tarval_bad) {
2640 if (tb == get_mode_one(get_tarval_mode(tb))) {
2641 b = new_Const(mode, get_mode_null(mode));
2642 DBG_OPT_CSTEVAL(n, b);
2644 } else if (ta != tarval_bad) {
2645 tarval *resa, *resb;
2646 resa = tarval_div(ta, tb);
2647 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2648 Jmp for X result!? */
2649 resb = tarval_mod(ta, tb);
2650 if (resb == tarval_bad) return n; /* Causes exception! */
2651 a = new_Const(mode, resa);
2652 b = new_Const(mode, resb);
2653 DBG_OPT_CSTEVAL(n, a);
2654 DBG_OPT_CSTEVAL(n, b);
2656 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2657 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2658 b = new_Const(mode, get_mode_null(mode));
2659 DBG_OPT_CSTEVAL(n, a);
2660 DBG_OPT_CSTEVAL(n, b);
2662 } else { /* Try architecture dependent optimization */
2663 arch_dep_replace_divmod_by_const(&a, &b, n);
2664 evaluated = a != NULL;
2666 } else if (a == b) {
2667 if (value_not_zero(a, &dummy)) {
2669 a = new_Const(mode, get_mode_one(mode));
2670 b = new_Const(mode, get_mode_null(mode));
2671 DBG_OPT_CSTEVAL(n, a);
2672 DBG_OPT_CSTEVAL(n, b);
2675 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2678 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2679 /* 0 / non-Const = 0 */
2684 if (evaluated) { /* replace by tuple */
2688 mem = get_DivMod_mem(n);
2689 /* skip a potential Pin */
2691 mem = get_Pin_op(mem);
2693 blk = get_irn_n(n, -1);
2694 turn_into_tuple(n, pn_DivMod_max);
2695 set_Tuple_pred(n, pn_DivMod_M, mem);
2696 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2697 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2698 set_Tuple_pred(n, pn_DivMod_res_div, a);
2699 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2703 } /* transform_node_DivMod */
2706 * Optimize x / c to x * (1/c)
2708 static ir_node *transform_node_Quot(ir_node *n) {
2709 ir_mode *mode = get_Quot_resmode(n);
2712 if (get_mode_arithmetic(mode) == irma_ieee754) {
2713 ir_node *b = get_Quot_right(n);
2716 tarval *tv = get_Const_tarval(b);
2718 tv = tarval_quo(get_mode_one(mode), tv);
2720 /* Do the transformation if the result is either exact or we are not
2721 using strict rules. */
2722 if (tv != tarval_bad &&
2723 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2724 ir_node *blk = get_irn_n(n, -1);
2725 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2726 ir_node *a = get_Quot_left(n);
2727 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2728 ir_node *mem = get_Quot_mem(n);
2730 /* skip a potential Pin */
2732 mem = get_Pin_op(mem);
2733 turn_into_tuple(n, pn_Quot_max);
2734 set_Tuple_pred(n, pn_Quot_M, mem);
2735 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2736 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2737 set_Tuple_pred(n, pn_Quot_res, m);
2738 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2743 } /* transform_node_Quot */
2746 * Optimize Abs(x) into x if x is Confirmed >= 0
2747 * Optimize Abs(x) into -x if x is Confirmed <= 0
2749 static ir_node *transform_node_Abs(ir_node *n) {
2750 ir_node *c, *oldn = n;
2751 ir_node *a = get_Abs_op(n);
2754 HANDLE_UNOP_PHI(tarval_abs, a, c);
2756 switch (classify_value_sign(a)) {
2757 case value_classified_negative:
2758 mode = get_irn_mode(n);
2761 * We can replace the Abs by -x here.
2762 * We even could add a new Confirm here.
2764 * Note that -x would create a new node, so we could
2765 * not run it in the equivalent_node() context.
2767 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2768 get_irn_n(n, -1), a, mode);
2770 DBG_OPT_CONFIRM(oldn, n);
2772 case value_classified_positive:
2773 /* n is positive, Abs is not needed */
2776 DBG_OPT_CONFIRM(oldn, n);
2781 } /* transform_node_Abs */
2784 * Transform a Cond node.
2786 * Replace the Cond by a Jmp if it branches on a constant
2789 static ir_node *transform_node_Cond(ir_node *n) {
2792 ir_node *a = get_Cond_selector(n);
2793 tarval *ta = value_of(a);
2795 /* we need block info which is not available in floating irgs */
2796 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2799 if ((ta != tarval_bad) &&
2800 (get_irn_mode(a) == mode_b) &&
2801 (get_opt_unreachable_code())) {
2802 /* It's a boolean Cond, branching on a boolean constant.
2803 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2804 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2805 turn_into_tuple(n, pn_Cond_max);
2806 if (ta == tarval_b_true) {
2807 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2808 set_Tuple_pred(n, pn_Cond_true, jmp);
2810 set_Tuple_pred(n, pn_Cond_false, jmp);
2811 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2813 /* We might generate an endless loop, so keep it alive. */
2814 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2817 } /* transform_node_Cond */
2819 typedef ir_node* (*recursive_transform) (ir_node *n);
2822 * makes use of distributive laws for and, or, eor
2823 * and(a OP c, b OP c) -> and(a, b) OP c
2824 * note, might return a different op than n
2826 static ir_node *transform_bitwise_distributive(ir_node *n,
2827 recursive_transform trans_func)
2830 ir_node *a = get_binop_left(n);
2831 ir_node *b = get_binop_right(n);
2832 ir_op *op = get_irn_op(a);
2833 ir_op *op_root = get_irn_op(n);
2835 if(op != get_irn_op(b))
2838 if (op == op_Conv) {
2839 ir_node *a_op = get_Conv_op(a);
2840 ir_node *b_op = get_Conv_op(b);
2841 ir_mode *a_mode = get_irn_mode(a_op);
2842 ir_mode *b_mode = get_irn_mode(b_op);
2843 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2844 ir_node *blk = get_irn_n(n, -1);
2847 set_binop_left(n, a_op);
2848 set_binop_right(n, b_op);
2849 set_irn_mode(n, a_mode);
2851 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2853 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2859 /* nothing to gain here */
2863 if (op == op_Shrs || op == op_Shr || op == op_Shl
2864 || op == op_And || op == op_Or || op == op_Eor) {
2865 ir_node *a_left = get_binop_left(a);
2866 ir_node *a_right = get_binop_right(a);
2867 ir_node *b_left = get_binop_left(b);
2868 ir_node *b_right = get_binop_right(b);
2872 if (is_op_commutative(op)) {
2873 if (a_left == b_left) {
2877 } else if(a_left == b_right) {
2881 } else if(a_right == b_left) {
2887 if(a_right == b_right) {
2894 /* (a sop c) & (b sop c) => (a & b) sop c */
2895 ir_node *blk = get_irn_n(n, -1);
2897 ir_node *new_n = exact_copy(n);
2898 set_binop_left(new_n, op1);
2899 set_binop_right(new_n, op2);
2900 new_n = trans_func(new_n);
2902 if(op_root == op_Eor && op == op_Or) {
2903 dbg_info *dbgi = get_irn_dbg_info(n);
2904 ir_graph *irg = current_ir_graph;
2905 ir_mode *mode = get_irn_mode(c);
2907 c = new_rd_Not(dbgi, irg, blk, c, mode);
2908 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2911 set_irn_n(n, -1, blk);
2912 set_binop_left(n, new_n);
2913 set_binop_right(n, c);
2916 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2927 static ir_node *transform_node_And(ir_node *n) {
2928 ir_node *c, *oldn = n;
2929 ir_node *a = get_And_left(n);
2930 ir_node *b = get_And_right(n);
2933 mode = get_irn_mode(n);
2934 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
2936 /* we can evaluate 2 Projs of the same Cmp */
2937 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2938 ir_node *pred_a = get_Proj_pred(a);
2939 ir_node *pred_b = get_Proj_pred(b);
2940 if (pred_a == pred_b) {
2941 dbg_info *dbgi = get_irn_dbg_info(n);
2942 ir_node *block = get_nodes_block(pred_a);
2943 pn_Cmp pn_a = get_Proj_proj(a);
2944 pn_Cmp pn_b = get_Proj_proj(b);
2945 /* yes, we can simply calculate with pncs */
2946 pn_Cmp new_pnc = pn_a & pn_b;
2948 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2953 ir_node *op = get_Not_op(b);
2955 ir_node *ba = get_And_left(op);
2956 ir_node *bb = get_And_right(op);
2958 /* it's enough to test the following cases due to normalization! */
2959 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2960 /* (a|b) & ~(a&b) = a^b */
2961 ir_node *block = get_nodes_block(n);
2963 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2964 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2972 ir_node *op = get_Not_op(a);
2974 ir_node *aa = get_And_left(op);
2975 ir_node *ab = get_And_right(op);
2977 /* it's enough to test the following cases due to normalization! */
2978 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2979 /* (a|b) & ~(a&b) = a^b */
2980 ir_node *block = get_nodes_block(n);
2982 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2990 ir_node *al = get_Eor_left(a);
2991 ir_node *ar = get_Eor_right(a);
2994 /* (b ^ a) & b -> ~a & b */
2995 dbg_info *dbg = get_irn_dbg_info(n);
2996 ir_node *block = get_nodes_block(n);
2998 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
2999 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3000 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3004 /* (a ^ b) & b -> ~a & b */
3005 dbg_info *dbg = get_irn_dbg_info(n);
3006 ir_node *block = get_nodes_block(n);
3008 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3009 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3010 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3015 ir_node *bl = get_Eor_left(b);
3016 ir_node *br = get_Eor_right(b);
3019 /* a & (a ^ b) -> a & ~b */
3020 dbg_info *dbg = get_irn_dbg_info(n);
3021 ir_node *block = get_nodes_block(n);
3023 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3024 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3025 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3029 /* a & (b ^ a) -> a & ~b */
3030 dbg_info *dbg = get_irn_dbg_info(n);
3031 ir_node *block = get_nodes_block(n);
3033 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3034 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3035 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3039 if (is_Not(a) && is_Not(b)) {
3040 /* ~a & ~b = ~(a|b) */
3041 ir_node *block = get_nodes_block(n);
3042 ir_mode *mode = get_irn_mode(n);
3046 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3047 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3048 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3052 n = transform_bitwise_distributive(n, transform_node_And);
3055 } /* transform_node_And */
3060 static ir_node *transform_node_Eor(ir_node *n) {
3061 ir_node *c, *oldn = n;
3062 ir_node *a = get_Eor_left(n);
3063 ir_node *b = get_Eor_right(n);
3064 ir_mode *mode = get_irn_mode(n);
3066 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3068 /* we can evaluate 2 Projs of the same Cmp */
3069 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3070 ir_node *pred_a = get_Proj_pred(a);
3071 ir_node *pred_b = get_Proj_pred(b);
3072 if(pred_a == pred_b) {
3073 dbg_info *dbgi = get_irn_dbg_info(n);
3074 ir_node *block = get_nodes_block(pred_a);
3075 pn_Cmp pn_a = get_Proj_proj(a);
3076 pn_Cmp pn_b = get_Proj_proj(b);
3077 /* yes, we can simply calculate with pncs */
3078 pn_Cmp new_pnc = pn_a ^ pn_b;
3080 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3087 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3088 mode, get_mode_null(mode));
3089 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3090 } else if (mode == mode_b &&
3092 is_Const(b) && is_Const_one(b) &&
3093 is_Cmp(get_Proj_pred(a))) {
3094 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3095 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3096 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3099 } else if (is_Const(b)) {
3100 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3101 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3102 ir_node *not_op = get_Not_op(a);
3103 dbg_info *dbg = get_irn_dbg_info(n);
3104 ir_graph *irg = current_ir_graph;
3105 ir_node *block = get_nodes_block(n);
3106 ir_mode *mode = get_irn_mode(n);
3107 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3109 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3110 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3114 n = transform_bitwise_distributive(n, transform_node_Eor);
3118 } /* transform_node_Eor */
3123 static ir_node *transform_node_Not(ir_node *n) {
3124 ir_node *c, *oldn = n;
3125 ir_node *a = get_Not_op(n);
3126 ir_mode *mode = get_irn_mode(n);
3128 HANDLE_UNOP_PHI(tarval_not,a,c);
3130 /* check for a boolean Not */
3131 if (mode == mode_b &&
3133 is_Cmp(get_Proj_pred(a))) {
3134 /* We negate a Cmp. The Cmp has the negated result anyways! */
3135 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3136 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3137 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3141 ir_node *eor_b = get_Eor_right(a);
3142 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3143 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3144 ir_node *eor_a = get_Eor_left(a);
3145 dbg_info *dbg = get_irn_dbg_info(n);
3146 ir_graph *irg = current_ir_graph;
3147 ir_node *block = get_nodes_block(n);
3148 ir_mode *mode = get_irn_mode(n);
3149 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3153 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3154 if (is_Minus(a)) { /* ~-x -> x + -1 */
3155 dbg_info *dbg = get_irn_dbg_info(n);
3156 ir_graph *irg = current_ir_graph;
3157 ir_node *block = get_nodes_block(n);
3158 ir_node *add_l = get_Minus_op(a);
3159 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3160 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3161 } else if (is_Add(a)) {
3162 ir_node *add_r = get_Add_right(a);
3163 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3164 /* ~(x + -1) = -x */
3165 ir_node *op = get_Add_left(a);
3166 ir_node *blk = get_irn_n(n, -1);
3167 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3173 } /* transform_node_Not */
3176 * Transform a Minus.
3181 static ir_node *transform_node_Minus(ir_node *n) {
3182 ir_node *c, *oldn = n;
3183 ir_node *a = get_Minus_op(n);
3186 HANDLE_UNOP_PHI(tarval_neg,a,c);
3188 mode = get_irn_mode(a);
3189 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3190 /* the following rules are only to twos-complement */
3193 ir_node *op = get_Not_op(a);
3194 tarval *tv = get_mode_one(mode);
3195 ir_node *blk = get_irn_n(n, -1);
3196 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3197 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3198 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3202 ir_node *c = get_Shr_right(a);
3205 tarval *tv = get_Const_tarval(c);
3207 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3208 /* -(a >>u (size-1)) = a >>s (size-1) */
3209 ir_node *v = get_Shr_left(a);
3211 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3212 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3218 ir_node *c = get_Shrs_right(a);
3221 tarval *tv = get_Const_tarval(c);
3223 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3224 /* -(a >>s (size-1)) = a >>u (size-1) */
3225 ir_node *v = get_Shrs_left(a);
3227 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3228 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3235 /* - (a-b) = b - a */
3236 ir_node *la = get_Sub_left(a);
3237 ir_node *ra = get_Sub_right(a);
3238 ir_node *blk = get_irn_n(n, -1);
3240 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3241 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3245 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3246 ir_node *mul_l = get_Mul_left(a);
3247 ir_node *mul_r = get_Mul_right(a);
3248 if (is_Const(mul_r)) {
3249 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3250 ir_node *cnst = new_Const(mode, tv);
3251 dbg_info *dbg = get_irn_dbg_info(a);
3252 ir_graph *irg = current_ir_graph;
3253 ir_node *block = get_nodes_block(a);
3254 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3260 } /* transform_node_Minus */
3263 * Transform a Cast_type(Const) into a new Const_type
3265 static ir_node *transform_node_Cast(ir_node *n) {
3267 ir_node *pred = get_Cast_op(n);
3268 ir_type *tp = get_irn_type(n);
3270 if (is_Const(pred) && get_Const_type(pred) != tp) {
3271 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3272 get_Const_tarval(pred), tp);
3273 DBG_OPT_CSTEVAL(oldn, n);
3274 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3275 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3276 get_SymConst_kind(pred), tp);
3277 DBG_OPT_CSTEVAL(oldn, n);
3281 } /* transform_node_Cast */
3284 * Transform a Proj(Div) with a non-zero value.
3285 * Removes the exceptions and routes the memory to the NoMem node.
3287 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3288 ir_node *div = get_Proj_pred(proj);
3289 ir_node *b = get_Div_right(div);
3290 ir_node *confirm, *res, *new_mem;
3293 if (value_not_zero(b, &confirm)) {
3294 /* div(x, y) && y != 0 */
3295 if (confirm == NULL) {
3296 /* we are sure we have a Const != 0 */
3297 new_mem = get_Div_mem(div);
3298 if (is_Pin(new_mem))
3299 new_mem = get_Pin_op(new_mem);
3300 set_Div_mem(div, new_mem);
3301 set_irn_pinned(div, op_pin_state_floats);
3304 proj_nr = get_Proj_proj(proj);
3306 case pn_Div_X_regular:
3307 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3309 case pn_Div_X_except:
3310 /* we found an exception handler, remove it */
3311 DBG_OPT_EXC_REM(proj);
3315 res = get_Div_mem(div);
3316 new_mem = get_irg_no_mem(current_ir_graph);
3319 /* This node can only float up to the Confirm block */
3320 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3322 set_irn_pinned(div, op_pin_state_floats);
3323 /* this is a Div without exception, we can remove the memory edge */
3324 set_Div_mem(div, new_mem);
3329 } /* transform_node_Proj_Div */
3332 * Transform a Proj(Mod) with a non-zero value.
3333 * Removes the exceptions and routes the memory to the NoMem node.
3335 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3336 ir_node *mod = get_Proj_pred(proj);
3337 ir_node *b = get_Mod_right(mod);
3338 ir_node *confirm, *res, *new_mem;
3341 if (value_not_zero(b, &confirm)) {
3342 /* mod(x, y) && y != 0 */
3343 proj_nr = get_Proj_proj(proj);
3345 if (confirm == NULL) {
3346 /* we are sure we have a Const != 0 */
3347 new_mem = get_Mod_mem(mod);
3348 if (is_Pin(new_mem))
3349 new_mem = get_Pin_op(new_mem);
3350 set_Mod_mem(mod, new_mem);
3351 set_irn_pinned(mod, op_pin_state_floats);
3356 case pn_Mod_X_regular:
3357 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3359 case pn_Mod_X_except:
3360 /* we found an exception handler, remove it */
3361 DBG_OPT_EXC_REM(proj);
3365 res = get_Mod_mem(mod);
3366 new_mem = get_irg_no_mem(current_ir_graph);
3369 /* This node can only float up to the Confirm block */
3370 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3372 /* this is a Mod without exception, we can remove the memory edge */
3373 set_Mod_mem(mod, new_mem);
3376 if (get_Mod_left(mod) == b) {
3377 /* a % a = 0 if a != 0 */
3378 ir_mode *mode = get_irn_mode(proj);
3379 ir_node *res = new_Const(mode, get_mode_null(mode));
3381 DBG_OPT_CSTEVAL(mod, res);
3387 } /* transform_node_Proj_Mod */
3390 * Transform a Proj(DivMod) with a non-zero value.
3391 * Removes the exceptions and routes the memory to the NoMem node.
3393 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3394 ir_node *divmod = get_Proj_pred(proj);
3395 ir_node *b = get_DivMod_right(divmod);
3396 ir_node *confirm, *res, *new_mem;
3399 if (value_not_zero(b, &confirm)) {
3400 /* DivMod(x, y) && y != 0 */
3401 proj_nr = get_Proj_proj(proj);
3403 if (confirm == NULL) {
3404 /* we are sure we have a Const != 0 */
3405 new_mem = get_DivMod_mem(divmod);
3406 if (is_Pin(new_mem))
3407 new_mem = get_Pin_op(new_mem);
3408 set_DivMod_mem(divmod, new_mem);
3409 set_irn_pinned(divmod, op_pin_state_floats);
3414 case pn_DivMod_X_regular:
3415 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3417 case pn_DivMod_X_except:
3418 /* we found an exception handler, remove it */
3419 DBG_OPT_EXC_REM(proj);
3423 res = get_DivMod_mem(divmod);
3424 new_mem = get_irg_no_mem(current_ir_graph);
3427 /* This node can only float up to the Confirm block */
3428 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3430 /* this is a DivMod without exception, we can remove the memory edge */
3431 set_DivMod_mem(divmod, new_mem);
3434 case pn_DivMod_res_mod:
3435 if (get_DivMod_left(divmod) == b) {
3436 /* a % a = 0 if a != 0 */
3437 ir_mode *mode = get_irn_mode(proj);
3438 ir_node *res = new_Const(mode, get_mode_null(mode));
3440 DBG_OPT_CSTEVAL(divmod, res);
3446 } /* transform_node_Proj_DivMod */
3449 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3451 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3452 if (get_opt_unreachable_code()) {
3453 ir_node *n = get_Proj_pred(proj);
3454 ir_node *b = get_Cond_selector(n);
3456 if (mode_is_int(get_irn_mode(b))) {
3457 tarval *tb = value_of(b);
3459 if (tb != tarval_bad) {
3460 /* we have a constant switch */
3461 long num = get_Proj_proj(proj);
3463 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3464 if (get_tarval_long(tb) == num) {
3465 /* Do NOT create a jump here, or we will have 2 control flow ops
3466 * in a block. This case is optimized away in optimize_cf(). */
3469 /* this case will NEVER be taken, kill it */
3477 } /* transform_node_Proj_Cond */
3480 * Normalizes and optimizes Cmp nodes.
3482 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3483 ir_node *n = get_Proj_pred(proj);
3484 ir_node *left = get_Cmp_left(n);
3485 ir_node *right = get_Cmp_right(n);
3489 ir_mode *mode = NULL;
3490 long proj_nr = get_Proj_proj(proj);
3492 /* we can evaluate this direct */
3495 return new_Const(mode_b, get_tarval_b_false());
3497 return new_Const(mode_b, get_tarval_b_true());
3499 if(!mode_is_float(get_irn_mode(left)))
3500 return new_Const(mode_b, get_tarval_b_true());
3508 left = get_Cast_op(left);
3510 right = get_Cast_op(right);
3512 /* Remove unnecessary conversions */
3513 /* TODO handle constants */
3514 if (is_Conv(left) && is_Conv(right)) {
3515 ir_mode *mode = get_irn_mode(left);
3516 ir_node *op_left = get_Conv_op(left);
3517 ir_node *op_right = get_Conv_op(right);
3518 ir_mode *mode_left = get_irn_mode(op_left);
3519 ir_mode *mode_right = get_irn_mode(op_right);
3521 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3522 ir_graph *irg = current_ir_graph;
3523 ir_node *block = get_nodes_block(n);
3525 if (mode_left == mode_right) {
3529 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3530 } else if (smaller_mode(mode_left, mode_right)) {
3531 left = new_r_Conv(irg, block, op_left, mode_right);
3534 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3535 } else if (smaller_mode(mode_right, mode_left)) {
3537 right = new_r_Conv(irg, block, op_right, mode_left);
3539 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3544 /* remove operation of both sides if possible */
3545 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3546 ir_opcode lop = get_irn_opcode(left);
3548 if (lop == get_irn_opcode(right)) {
3549 ir_node *ll, *lr, *rl, *rr;
3551 /* same operation on both sides, try to remove */
3555 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3556 left = get_unop_op(left);
3557 right = get_unop_op(right);
3559 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3562 ll = get_Add_left(left);
3563 lr = get_Add_right(left);
3564 rl = get_Add_left(right);
3565 rr = get_Add_right(right);
3568 /* X + a CMP X + b ==> a CMP b */
3572 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3573 } else if (ll == rr) {
3574 /* X + a CMP b + X ==> a CMP b */
3578 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3579 } else if (lr == rl) {
3580 /* a + X CMP X + b ==> a CMP b */
3584 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3585 } else if (lr == rr) {
3586 /* a + X CMP b + X ==> a CMP b */
3590 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3594 ll = get_Sub_left(left);
3595 lr = get_Sub_right(left);
3596 rl = get_Sub_left(right);
3597 rr = get_Sub_right(right);
3600 /* X - a CMP X - b ==> a CMP b */
3604 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3605 } else if (lr == rr) {
3606 /* a - X CMP b - X ==> a CMP b */
3610 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3614 if (get_Rot_right(left) == get_Rot_right(right)) {
3615 /* a ROT X CMP b ROT X */
3616 left = get_Rot_left(left);
3617 right = get_Rot_left(right);
3619 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3628 if (get_irn_mode(left) == mode_b) {
3629 ir_graph *irg = current_ir_graph;
3630 ir_node *block = get_nodes_block(n);
3634 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3635 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3636 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3637 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3638 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3639 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3640 default: bres = NULL;
3643 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3648 if (!get_opt_reassociation())
3652 * First step: normalize the compare op
3653 * by placing the constant on the right side
3654 * or moving the lower address node to the left.
3655 * We ignore the case that both are constants
3656 * this case should be optimized away.
3658 if (is_Const(right)) {
3660 } else if (is_Const(left)) {
3665 proj_nr = get_inversed_pnc(proj_nr);
3667 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3673 proj_nr = get_inversed_pnc(proj_nr);
3678 * Second step: Try to reduce the magnitude
3679 * of a constant. This may help to generate better code
3680 * later and may help to normalize more compares.
3681 * Of course this is only possible for integer values.
3684 mode = get_irn_mode(c);
3685 tv = get_Const_tarval(c);
3687 /* TODO extend to arbitrary constants */
3688 if (is_Conv(left) && tarval_is_null(tv)) {
3689 ir_node *op = get_Conv_op(left);
3690 ir_mode *op_mode = get_irn_mode(op);
3693 * UpConv(x) REL 0 ==> x REL 0
3695 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3696 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3697 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3698 tv = get_mode_null(op_mode);
3702 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3706 if (tv != tarval_bad) {
3707 /* the following optimization is possible on modes without Overflow
3708 * on Unary Minus or on == and !=:
3709 * -a CMP c ==> a swap(CMP) -c
3711 * Beware: for two-complement Overflow may occur, so only == and != can
3712 * be optimized, see this:
3713 * -MININT < 0 =/=> MININT > 0 !!!
3715 if (is_Minus(left) &&
3716 (!mode_overflow_on_unary_Minus(mode) ||
3717 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3718 tv = tarval_neg(tv);
3720 if (tv != tarval_bad) {
3721 left = get_Minus_op(left);
3722 proj_nr = get_inversed_pnc(proj_nr);
3724 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3726 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3727 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3728 tv = tarval_not(tv);
3730 if (tv != tarval_bad) {
3731 left = get_Not_op(left);
3733 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3737 /* for integer modes, we have more */
3738 if (mode_is_int(mode)) {
3739 /* Ne includes Unordered which is not possible on integers.
3740 * However, frontends often use this wrong, so fix it here */
3741 if (proj_nr & pn_Cmp_Uo) {
3742 proj_nr &= ~pn_Cmp_Uo;
3743 set_Proj_proj(proj, proj_nr);
3746 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3747 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3748 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3749 tv = tarval_sub(tv, get_mode_one(mode));
3751 if (tv != tarval_bad) {
3752 proj_nr ^= pn_Cmp_Eq;
3754 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3757 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3758 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3759 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3760 tv = tarval_add(tv, get_mode_one(mode));
3762 if (tv != tarval_bad) {
3763 proj_nr ^= pn_Cmp_Eq;
3765 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3769 /* the following reassociations work only for == and != */
3770 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3772 #if 0 /* Might be not that good in general */
3773 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3774 if (tarval_is_null(tv) && is_Sub(left)) {
3775 right = get_Sub_right(left);
3776 left = get_Sub_left(left);
3778 tv = value_of(right);
3780 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3784 if (tv != tarval_bad) {
3785 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3787 ir_node *c1 = get_Sub_right(left);
3788 tarval *tv2 = value_of(c1);
3790 if (tv2 != tarval_bad) {
3791 tv2 = tarval_add(tv, value_of(c1));
3793 if (tv2 != tarval_bad) {
3794 left = get_Sub_left(left);
3797 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3801 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3802 else if (is_Add(left)) {
3803 ir_node *a_l = get_Add_left(left);
3804 ir_node *a_r = get_Add_right(left);
3808 if (is_Const(a_l)) {
3810 tv2 = value_of(a_l);
3813 tv2 = value_of(a_r);
3816 if (tv2 != tarval_bad) {
3817 tv2 = tarval_sub(tv, tv2);
3819 if (tv2 != tarval_bad) {
3823 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3827 /* -a == c ==> a == -c, -a != c ==> a != -c */
3828 else if (is_Minus(left)) {
3829 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3831 if (tv2 != tarval_bad) {
3832 left = get_Minus_op(left);
3835 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3840 /* the following reassociations work only for <= */
3841 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3842 if (tv != tarval_bad) {
3843 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3844 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3850 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3851 switch (get_irn_opcode(left)) {
3855 c1 = get_And_right(left);
3858 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
3859 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
3861 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
3863 /* TODO: move to constant evaluation */
3864 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3865 c1 = new_Const(mode_b, tv);
3866 DBG_OPT_CSTEVAL(proj, c1);
3870 if (tarval_is_single_bit(tv)) {
3872 * optimization for AND:
3874 * And(x, C) == C ==> And(x, C) != 0
3875 * And(x, C) != C ==> And(X, C) == 0
3877 * if C is a single Bit constant.
3880 /* check for Constant's match. We have check hare the tarvals,
3881 because our const might be changed */
3882 if (get_Const_tarval(c1) == tv) {
3883 /* fine: do the transformation */
3884 tv = get_mode_null(get_tarval_mode(tv));
3885 proj_nr ^= pn_Cmp_Leg;
3887 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3893 c1 = get_Or_right(left);
3894 if (is_Const(c1) && tarval_is_null(tv)) {
3896 * Or(x, C) == 0 && C != 0 ==> FALSE
3897 * Or(x, C) != 0 && C != 0 ==> TRUE
3899 if (! tarval_is_null(get_Const_tarval(c1))) {
3900 /* TODO: move to constant evaluation */
3901 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3902 c1 = new_Const(mode_b, tv);
3903 DBG_OPT_CSTEVAL(proj, c1);
3910 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
3912 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
3915 c1 = get_Shl_right(left);
3917 tarval *tv1 = get_Const_tarval(c1);
3918 ir_mode *mode = get_irn_mode(left);
3919 tarval *minus1 = get_mode_all_one(mode);
3920 tarval *amask = tarval_shr(minus1, tv1);
3921 tarval *cmask = tarval_shl(minus1, tv1);
3924 if (tarval_and(tv, cmask) != tv) {
3925 /* condition not met */
3926 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3927 c1 = new_Const(mode_b, tv);
3928 DBG_OPT_CSTEVAL(proj, c1);
3931 sl = get_Shl_left(left);
3932 blk = get_nodes_block(n);
3933 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3934 tv = tarval_shr(tv, tv1);
3936 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3941 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
3943 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
3946 c1 = get_Shr_right(left);
3948 tarval *tv1 = get_Const_tarval(c1);
3949 ir_mode *mode = get_irn_mode(left);
3950 tarval *minus1 = get_mode_all_one(mode);
3951 tarval *amask = tarval_shl(minus1, tv1);
3952 tarval *cmask = tarval_shr(minus1, tv1);
3955 if (tarval_and(tv, cmask) != tv) {
3956 /* condition not met */
3957 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3958 c1 = new_Const(mode_b, tv);
3959 DBG_OPT_CSTEVAL(proj, c1);
3962 sl = get_Shr_left(left);
3963 blk = get_nodes_block(n);
3964 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3965 tv = tarval_shl(tv, tv1);
3967 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3972 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
3974 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
3977 c1 = get_Shrs_right(left);
3979 tarval *tv1 = get_Const_tarval(c1);
3980 ir_mode *mode = get_irn_mode(left);
3981 tarval *minus1 = get_mode_all_one(mode);
3982 tarval *amask = tarval_shl(minus1, tv1);
3983 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
3986 cond = tarval_sub(cond, tv1);
3987 cond = tarval_shrs(tv, cond);
3989 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
3990 /* condition not met */
3991 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3992 c1 = new_Const(mode_b, tv);
3993 DBG_OPT_CSTEVAL(proj, c1);
3996 sl = get_Shrs_left(left);
3997 blk = get_nodes_block(n);
3998 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3999 tv = tarval_shl(tv, tv1);
4001 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4006 } /* tarval != bad */
4009 if (changed & 2) /* need a new Const */
4010 right = new_Const(mode, tv);
4012 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4013 ir_node *op = get_Proj_pred(left);
4015 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4016 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4017 ir_node *c = get_binop_right(op);
4020 tarval *tv = get_Const_tarval(c);
4022 if (tarval_is_single_bit(tv)) {
4023 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4024 ir_node *v = get_binop_left(op);
4025 ir_node *blk = get_irn_n(op, -1);
4026 ir_mode *mode = get_irn_mode(v);
4028 tv = tarval_sub(tv, get_mode_one(mode));
4029 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4031 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4038 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4040 /* create a new compare */
4041 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4043 set_Proj_pred(proj, n);
4044 set_Proj_proj(proj, proj_nr);
4048 } /* transform_node_Proj_Cmp */
4051 * Does all optimizations on nodes that must be done on it's Proj's
4052 * because of creating new nodes.
4054 static ir_node *transform_node_Proj(ir_node *proj) {
4055 ir_node *n = get_Proj_pred(proj);
4057 switch (get_irn_opcode(n)) {
4059 return transform_node_Proj_Div(proj);
4062 return transform_node_Proj_Mod(proj);
4065 return transform_node_Proj_DivMod(proj);
4068 return transform_node_Proj_Cond(proj);
4071 return transform_node_Proj_Cmp(proj);
4074 /* should not happen, but if it does will be optimized away */
4075 return equivalent_node_Proj(proj);
4081 } /* transform_node_Proj */
4084 * Move Confirms down through Phi nodes.
4086 static ir_node *transform_node_Phi(ir_node *phi) {
4088 ir_mode *mode = get_irn_mode(phi);
4090 if (mode_is_reference(mode)) {
4091 n = get_irn_arity(phi);
4093 /* Beware of Phi0 */
4095 ir_node *pred = get_irn_n(phi, 0);
4096 ir_node *bound, *new_Phi, *block, **in;
4099 if (! is_Confirm(pred))
4102 bound = get_Confirm_bound(pred);
4103 pnc = get_Confirm_cmp(pred);
4105 NEW_ARR_A(ir_node *, in, n);
4106 in[0] = get_Confirm_value(pred);
4108 for (i = 1; i < n; ++i) {
4109 pred = get_irn_n(phi, i);
4111 if (! is_Confirm(pred) ||
4112 get_Confirm_bound(pred) != bound ||
4113 get_Confirm_cmp(pred) != pnc)
4115 in[i] = get_Confirm_value(pred);
4117 /* move the Confirm nodes "behind" the Phi */
4118 block = get_irn_n(phi, -1);
4119 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4120 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4124 } /* transform_node_Phi */
4127 * Returns the operands of a commutative bin-op, if one operand is
4128 * a const, it is returned as the second one.
4130 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4131 ir_node *op_a = get_binop_left(binop);
4132 ir_node *op_b = get_binop_right(binop);
4134 assert(is_op_commutative(get_irn_op(binop)));
4136 if (is_Const(op_a)) {
4143 } /* get_comm_Binop_Ops */
4146 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4147 * Such pattern may arise in bitfield stores.
4149 * value c4 value c4 & c2
4150 * AND c3 AND c1 | c3
4157 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4160 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4163 ir_node *and_l, *c3;
4164 ir_node *value, *c4;
4165 ir_node *new_and, *new_const, *block;
4166 ir_mode *mode = get_irn_mode(or);
4168 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4171 get_comm_Binop_Ops(or, &and, &c1);
4172 if (!is_Const(c1) || !is_And(and))
4175 get_comm_Binop_Ops(and, &or_l, &c2);
4179 tv1 = get_Const_tarval(c1);
4180 tv2 = get_Const_tarval(c2);
4182 tv = tarval_or(tv1, tv2);
4183 if (tarval_is_all_one(tv)) {
4184 /* the AND does NOT clear a bit with isn't set by the OR */
4185 set_Or_left(or, or_l);
4186 set_Or_right(or, c1);
4188 /* check for more */
4195 get_comm_Binop_Ops(or_l, &and_l, &c3);
4196 if (!is_Const(c3) || !is_And(and_l))
4199 get_comm_Binop_Ops(and_l, &value, &c4);
4203 /* ok, found the pattern, check for conditions */
4204 assert(mode == get_irn_mode(and));
4205 assert(mode == get_irn_mode(or_l));
4206 assert(mode == get_irn_mode(and_l));
4208 tv3 = get_Const_tarval(c3);
4209 tv4 = get_Const_tarval(c4);
4211 tv = tarval_or(tv4, tv2);
4212 if (!tarval_is_all_one(tv)) {
4213 /* have at least one 0 at the same bit position */
4217 n_tv4 = tarval_not(tv4);
4218 if (tv3 != tarval_and(tv3, n_tv4)) {
4219 /* bit in the or_mask is outside the and_mask */
4223 n_tv2 = tarval_not(tv2);
4224 if (tv1 != tarval_and(tv1, n_tv2)) {
4225 /* bit in the or_mask is outside the and_mask */
4229 /* ok, all conditions met */
4230 block = get_irn_n(or, -1);
4232 new_and = new_r_And(current_ir_graph, block,
4233 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4235 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4237 set_Or_left(or, new_and);
4238 set_Or_right(or, new_const);
4240 /* check for more */
4242 } /* transform_node_Or_bf_store */
4245 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4247 static ir_node *transform_node_Or_Rot(ir_node *or) {
4248 ir_mode *mode = get_irn_mode(or);
4249 ir_node *shl, *shr, *block;
4250 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4253 if (! mode_is_int(mode))
4256 shl = get_binop_left(or);
4257 shr = get_binop_right(or);
4266 } else if (!is_Shl(shl)) {
4268 } else if (!is_Shr(shr)) {
4271 x = get_Shl_left(shl);
4272 if (x != get_Shr_left(shr))
4275 c1 = get_Shl_right(shl);
4276 c2 = get_Shr_right(shr);
4277 if (is_Const(c1) && is_Const(c2)) {
4278 tv1 = get_Const_tarval(c1);
4279 if (! tarval_is_long(tv1))
4282 tv2 = get_Const_tarval(c2);
4283 if (! tarval_is_long(tv2))
4286 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4287 != get_mode_size_bits(mode))
4290 /* yet, condition met */
4291 block = get_irn_n(or, -1);
4293 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4295 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4297 } else if (is_Sub(c1)) {
4301 if (get_Sub_right(sub) != v)
4304 c1 = get_Sub_left(sub);
4308 tv1 = get_Const_tarval(c1);
4309 if (! tarval_is_long(tv1))
4312 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4315 /* yet, condition met */
4316 block = get_nodes_block(or);
4318 /* a Rot right is not supported, so use a rot left */
4319 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4321 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4323 } else if (is_Sub(c2)) {
4327 c1 = get_Sub_left(sub);
4331 tv1 = get_Const_tarval(c1);
4332 if (! tarval_is_long(tv1))
4335 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4338 /* yet, condition met */
4339 block = get_irn_n(or, -1);
4342 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4344 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4349 } /* transform_node_Or_Rot */
4354 static ir_node *transform_node_Or(ir_node *n) {
4355 ir_node *c, *oldn = n;
4356 ir_node *a = get_Or_left(n);
4357 ir_node *b = get_Or_right(n);
4360 if (is_Not(a) && is_Not(b)) {
4361 /* ~a | ~b = ~(a&b) */
4362 ir_node *block = get_nodes_block(n);
4364 mode = get_irn_mode(n);
4367 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4368 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4369 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4373 /* we can evaluate 2 Projs of the same Cmp */
4374 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4375 ir_node *pred_a = get_Proj_pred(a);
4376 ir_node *pred_b = get_Proj_pred(b);
4377 if (pred_a == pred_b) {
4378 dbg_info *dbgi = get_irn_dbg_info(n);
4379 ir_node *block = get_nodes_block(pred_a);
4380 pn_Cmp pn_a = get_Proj_proj(a);
4381 pn_Cmp pn_b = get_Proj_proj(b);
4382 /* yes, we can simply calculate with pncs */
4383 pn_Cmp new_pnc = pn_a | pn_b;
4385 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4390 mode = get_irn_mode(n);
4391 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4393 n = transform_node_Or_bf_store(n);
4394 n = transform_node_Or_Rot(n);
4398 n = transform_bitwise_distributive(n, transform_node_Or);
4401 } /* transform_node_Or */
4405 static ir_node *transform_node(ir_node *n);
4408 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4410 * Should be moved to reassociation?
4412 static ir_node *transform_node_shift(ir_node *n) {
4413 ir_node *left, *right;
4414 tarval *tv1, *tv2, *res;
4416 int modulo_shf, flag;
4418 left = get_binop_left(n);
4420 /* different operations */
4421 if (get_irn_op(left) != get_irn_op(n))
4424 right = get_binop_right(n);
4425 tv1 = value_of(right);
4426 if (tv1 == tarval_bad)
4429 tv2 = value_of(get_binop_right(left));
4430 if (tv2 == tarval_bad)
4433 res = tarval_add(tv1, tv2);
4435 /* beware: a simple replacement works only, if res < modulo shift */
4436 mode = get_irn_mode(n);
4440 modulo_shf = get_mode_modulo_shift(mode);
4441 if (modulo_shf > 0) {
4442 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4444 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4450 /* ok, we can replace it */
4451 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4453 in[0] = get_binop_left(left);
4454 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4456 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4458 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4460 return transform_node(irn);
4463 } /* transform_node_shift */
4468 static ir_node *transform_node_Shr(ir_node *n) {
4469 ir_node *c, *oldn = n;
4470 ir_node *a = get_Shr_left(n);
4471 ir_node *b = get_Shr_right(n);
4472 ir_mode *mode = get_irn_mode(n);
4474 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4475 return transform_node_shift(n);
4476 } /* transform_node_Shr */
4481 static ir_node *transform_node_Shrs(ir_node *n) {
4482 ir_node *c, *oldn = n;
4483 ir_node *a = get_Shrs_left(n);
4484 ir_node *b = get_Shrs_right(n);
4485 ir_mode *mode = get_irn_mode(n);
4487 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4488 return transform_node_shift(n);
4489 } /* transform_node_Shrs */
4494 static ir_node *transform_node_Shl(ir_node *n) {
4495 ir_node *c, *oldn = n;
4496 ir_node *a = get_Shl_left(n);
4497 ir_node *b = get_Shl_right(n);
4498 ir_mode *mode = get_irn_mode(n);
4500 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4501 return transform_node_shift(n);
4502 } /* transform_node_Shl */
4507 static ir_node *transform_node_Rot(ir_node *n) {
4508 ir_node *c, *oldn = n;
4509 ir_node *a = get_Rot_left(n);
4510 ir_node *b = get_Rot_right(n);
4511 ir_mode *mode = get_irn_mode(n);
4513 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4514 return transform_node_shift(n);
4515 } /* transform_node_Rot */
4520 static ir_node *transform_node_Conv(ir_node *n) {
4521 ir_node *c, *oldn = n;
4522 ir_node *a = get_Conv_op(n);
4524 if (is_const_Phi(a)) {
4525 c = apply_conv_on_phi(a, get_irn_mode(n));
4527 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4532 } /* transform_node_Conv */
4535 * Remove dead blocks and nodes in dead blocks
4536 * in keep alive list. We do not generate a new End node.
4538 static ir_node *transform_node_End(ir_node *n) {
4539 int i, j, n_keepalives = get_End_n_keepalives(n);
4542 NEW_ARR_A(ir_node *, in, n_keepalives);
4544 for (i = j = 0; i < n_keepalives; ++i) {
4545 ir_node *ka = get_End_keepalive(n, i);
4547 if (! is_Block_dead(ka)) {
4551 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4554 /* FIXME: beabi need to keep a Proj(M) */
4555 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4558 if (j != n_keepalives)
4559 set_End_keepalives(n, j, in);
4561 } /* transform_node_End */
4563 /** returns 1 if a == -b */
4564 static int is_negated_value(ir_node *a, ir_node *b) {
4565 if(is_Minus(a) && get_Minus_op(a) == b)
4567 if(is_Minus(b) && get_Minus_op(b) == a)
4569 if(is_Sub(a) && is_Sub(b)) {
4570 ir_node *a_left = get_Sub_left(a);
4571 ir_node *a_right = get_Sub_right(a);
4572 ir_node *b_left = get_Sub_left(b);
4573 ir_node *b_right = get_Sub_right(b);
4575 if(a_left == b_right && a_right == b_left)
4583 * Optimize a Mux into some simpler cases.
4585 static ir_node *transform_node_Mux(ir_node *n) {
4586 ir_node *oldn = n, *sel = get_Mux_sel(n);
4587 ir_mode *mode = get_irn_mode(n);
4589 if (mode == mode_b) {
4590 ir_node *t = get_Mux_true(n);
4591 ir_node *f = get_Mux_false(n);
4592 dbg_info *dbg = get_irn_dbg_info(n);
4593 ir_node *block = get_irn_n(n, -1);
4594 ir_graph *irg = current_ir_graph;
4597 tarval *tv_t = get_Const_tarval(t);
4598 if (tv_t == tarval_b_true) {
4600 assert(get_Const_tarval(f) == tarval_b_false);
4603 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4606 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4607 assert(tv_t == tarval_b_false);
4609 assert(get_Const_tarval(f) == tarval_b_true);
4612 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4615 } else if (is_Const(f)) {
4616 tarval *tv_f = get_Const_tarval(f);
4617 if (tv_f == tarval_b_true) {
4618 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4619 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4621 assert(tv_f == tarval_b_false);
4622 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4627 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4628 ir_node *cmp = get_Proj_pred(sel);
4629 long pn = get_Proj_proj(sel);
4630 ir_node *f = get_Mux_false(n);
4631 ir_node *t = get_Mux_true(n);
4634 * Note: normalization puts the constant on the right side,
4635 * so we check only one case.
4637 * Note further that these optimization work even for floating point
4638 * with NaN's because -NaN == NaN.
4639 * However, if +0 and -0 is handled differently, we cannot use the first
4643 ir_node *cmp_r = get_Cmp_right(cmp);
4644 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4645 ir_node *block = get_irn_n(n, -1);
4647 if(is_negated_value(f, t)) {
4648 ir_node *cmp_left = get_Cmp_left(cmp);
4650 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4651 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4652 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4654 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4656 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4658 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4659 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4660 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4662 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4664 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4666 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4674 return arch_transform_node_Mux(n);
4675 } /* transform_node_Mux */
4678 * Optimize a Psi into some simpler cases.
4680 static ir_node *transform_node_Psi(ir_node *n) {
4682 return transform_node_Mux(n);
4685 } /* transform_node_Psi */
4688 * optimize sync nodes that have other syncs as input we simply add the inputs
4689 * of the other sync to our own inputs
4691 static ir_node *transform_node_Sync(ir_node *n) {
4694 arity = get_irn_arity(n);
4695 for(i = 0; i < get_irn_arity(n); /*empty*/) {
4697 ir_node *in = get_irn_n(n, i);
4703 /* set sync input 0 instead of the sync */
4704 set_irn_n(n, i, get_irn_n(in, 0));
4705 /* so we check this input again for syncs */
4707 /* append all other inputs of the sync to our sync */
4708 arity2 = get_irn_arity(in);
4709 for(i2 = 1; i2 < arity2; ++i2) {
4710 ir_node *in_in = get_irn_n(in, i2);
4711 add_irn_n(n, in_in);
4712 /* increase arity so we also check the new inputs for syncs */
4717 /* rehash the sync node */
4718 add_identities(current_ir_graph->value_table, n);
4724 * Tries several [inplace] [optimizing] transformations and returns an
4725 * equivalent node. The difference to equivalent_node() is that these
4726 * transformations _do_ generate new nodes, and thus the old node must
4727 * not be freed even if the equivalent node isn't the old one.
4729 static ir_node *transform_node(ir_node *n) {
4733 * Transform_node is the only "optimizing transformation" that might
4734 * return a node with a different opcode. We iterate HERE until fixpoint
4735 * to get the final result.
4739 if (n->op->ops.transform_node)
4740 n = n->op->ops.transform_node(n);
4741 } while (oldn != n);
4744 } /* transform_node */
4747 * Sets the default transform node operation for an ir_op_ops.
4749 * @param code the opcode for the default operation
4750 * @param ops the operations initialized
4755 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4759 ops->transform_node = transform_node_##a; \
4796 } /* firm_set_default_transform_node */
4799 /* **************** Common Subexpression Elimination **************** */
4801 /** The size of the hash table used, should estimate the number of nodes
4803 #define N_IR_NODES 512
4805 /** Compares the attributes of two Const nodes. */
4806 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4807 return (get_Const_tarval(a) != get_Const_tarval(b))
4808 || (get_Const_type(a) != get_Const_type(b));
4809 } /* node_cmp_attr_Const */
4811 /** Compares the attributes of two Proj nodes. */
4812 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4813 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4814 } /* node_cmp_attr_Proj */
4816 /** Compares the attributes of two Filter nodes. */
4817 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4818 return get_Filter_proj(a) != get_Filter_proj(b);
4819 } /* node_cmp_attr_Filter */
4821 /** Compares the attributes of two Alloc nodes. */
4822 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4823 const alloc_attr *pa = get_irn_alloc_attr(a);
4824 const alloc_attr *pb = get_irn_alloc_attr(b);
4825 return (pa->where != pb->where) || (pa->type != pb->type);
4826 } /* node_cmp_attr_Alloc */
4828 /** Compares the attributes of two Free nodes. */
4829 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4830 const free_attr *pa = get_irn_free_attr(a);
4831 const free_attr *pb = get_irn_free_attr(b);
4832 return (pa->where != pb->where) || (pa->type != pb->type);
4833 } /* node_cmp_attr_Free */
4835 /** Compares the attributes of two SymConst nodes. */
4836 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4837 const symconst_attr *pa = get_irn_symconst_attr(a);
4838 const symconst_attr *pb = get_irn_symconst_attr(b);
4839 return (pa->num != pb->num)
4840 || (pa->sym.type_p != pb->sym.type_p)
4841 || (pa->tp != pb->tp);
4842 } /* node_cmp_attr_SymConst */
4844 /** Compares the attributes of two Call nodes. */
4845 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4846 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4847 } /* node_cmp_attr_Call */
4849 /** Compares the attributes of two Sel nodes. */
4850 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4851 const ir_entity *a_ent = get_Sel_entity(a);
4852 const ir_entity *b_ent = get_Sel_entity(b);
4854 (a_ent->kind != b_ent->kind) ||
4855 (a_ent->name != b_ent->name) ||
4856 (a_ent->owner != b_ent->owner) ||
4857 (a_ent->ld_name != b_ent->ld_name) ||
4858 (a_ent->type != b_ent->type);
4859 } /* node_cmp_attr_Sel */
4861 /** Compares the attributes of two Phi nodes. */
4862 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4863 /* we can only enter this function if both nodes have the same number of inputs,
4864 hence it is enough to check if one of them is a Phi0 */
4866 /* check the Phi0 attribute */
4867 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4870 } /* node_cmp_attr_Phi */
4872 /** Compares the attributes of two Conv nodes. */
4873 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4874 return get_Conv_strict(a) != get_Conv_strict(b);
4875 } /* node_cmp_attr_Conv */
4877 /** Compares the attributes of two Cast nodes. */
4878 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4879 return get_Cast_type(a) != get_Cast_type(b);
4880 } /* node_cmp_attr_Cast */
4882 /** Compares the attributes of two Load nodes. */
4883 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4884 if (get_Load_volatility(a) == volatility_is_volatile ||
4885 get_Load_volatility(b) == volatility_is_volatile)
4886 /* NEVER do CSE on volatile Loads */
4888 /* do not CSE Loads with different alignment. Be conservative. */
4889 if (get_Load_align(a) != get_Load_align(b))
4892 return get_Load_mode(a) != get_Load_mode(b);
4893 } /* node_cmp_attr_Load */
4895 /** Compares the attributes of two Store nodes. */
4896 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4897 /* do not CSE Stores with different alignment. Be conservative. */
4898 if (get_Store_align(a) != get_Store_align(b))
4901 /* NEVER do CSE on volatile Stores */
4902 return (get_Store_volatility(a) == volatility_is_volatile ||
4903 get_Store_volatility(b) == volatility_is_volatile);
4904 } /* node_cmp_attr_Store */
4906 /** Compares the attributes of two Confirm nodes. */
4907 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4908 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4909 } /* node_cmp_attr_Confirm */
4911 /** Compares the attributes of two ASM nodes. */
4912 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4914 const ir_asm_constraint *ca;
4915 const ir_asm_constraint *cb;
4918 if (get_ASM_text(a) != get_ASM_text(b))
4921 /* Should we really check the constraints here? Should be better, but is strange. */
4922 n = get_ASM_n_input_constraints(a);
4923 if (n != get_ASM_n_input_constraints(b))
4926 ca = get_ASM_input_constraints(a);
4927 cb = get_ASM_input_constraints(b);
4928 for (i = 0; i < n; ++i) {
4929 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4933 n = get_ASM_n_output_constraints(a);
4934 if (n != get_ASM_n_output_constraints(b))
4937 ca = get_ASM_output_constraints(a);
4938 cb = get_ASM_output_constraints(b);
4939 for (i = 0; i < n; ++i) {
4940 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4944 n = get_ASM_n_clobbers(a);
4945 if (n != get_ASM_n_clobbers(b))
4948 cla = get_ASM_clobbers(a);
4949 clb = get_ASM_clobbers(b);
4950 for (i = 0; i < n; ++i) {
4951 if (cla[i] != clb[i])
4955 } /* node_cmp_attr_ASM */
4958 * Set the default node attribute compare operation for an ir_op_ops.
4960 * @param code the opcode for the default operation
4961 * @param ops the operations initialized
4966 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4970 ops->node_cmp_attr = node_cmp_attr_##a; \
4995 } /* firm_set_default_node_cmp_attr */
4998 * Compare function for two nodes in the hash table. Gets two
4999 * nodes as parameters. Returns 0 if the nodes are a cse.
5001 int identities_cmp(const void *elt, const void *key) {
5008 if (a == b) return 0;
5010 if ((get_irn_op(a) != get_irn_op(b)) ||
5011 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5013 /* compare if a's in and b's in are of equal length */
5014 irn_arity_a = get_irn_intra_arity (a);
5015 if (irn_arity_a != get_irn_intra_arity(b))
5018 /* for block-local cse and op_pin_state_pinned nodes: */
5019 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
5020 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5024 /* compare a->in[0..ins] with b->in[0..ins] */
5025 for (i = 0; i < irn_arity_a; i++)
5026 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5030 * here, we already now that the nodes are identical except their
5033 if (a->op->ops.node_cmp_attr)
5034 return a->op->ops.node_cmp_attr(a, b);
5037 } /* identities_cmp */
5040 * Calculate a hash value of a node.
5042 unsigned ir_node_hash(ir_node *node) {
5046 if (node->op == op_Const) {
5047 /* special value for const, as they only differ in their tarval. */
5048 h = HASH_PTR(node->attr.con.tv);
5049 h = 9*h + HASH_PTR(get_irn_mode(node));
5050 } else if (node->op == op_SymConst) {
5051 /* special value for const, as they only differ in their symbol. */
5052 h = HASH_PTR(node->attr.symc.sym.type_p);
5053 h = 9*h + HASH_PTR(get_irn_mode(node));
5056 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5057 h = irn_arity = get_irn_intra_arity(node);
5059 /* consider all in nodes... except the block if not a control flow. */
5060 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5061 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5065 h = 9*h + HASH_PTR(get_irn_mode(node));
5067 h = 9*h + HASH_PTR(get_irn_op(node));
5071 } /* ir_node_hash */
5073 pset *new_identities(void) {
5074 return new_pset(identities_cmp, N_IR_NODES);
5075 } /* new_identities */
5077 void del_identities(pset *value_table) {
5078 del_pset(value_table);
5079 } /* del_identities */
5082 * Normalize a node by putting constants (and operands with smaller
5083 * node index) on the right
5085 * @param n The node to normalize
5087 static void normalize_node(ir_node *n) {
5088 if (get_opt_reassociation()) {
5089 if (is_op_commutative(get_irn_op(n))) {
5090 ir_node *l = get_binop_left(n);
5091 ir_node *r = get_binop_right(n);
5092 int l_idx = get_irn_idx(l);
5093 int r_idx = get_irn_idx(r);
5095 /* For commutative operators perform a OP b == b OP a but keep
5096 constants on the RIGHT side. This helps greatly in some optimizations.
5097 Moreover we use the idx number to make the form deterministic. */
5098 if (is_irn_constlike(l))
5100 if (is_irn_constlike(r))
5102 if (l_idx < r_idx) {
5103 set_binop_left(n, r);
5104 set_binop_right(n, l);
5108 } /* normalize_node */
5111 * Return the canonical node computing the same value as n.
5113 * @param value_table The value table
5114 * @param n The node to lookup
5116 * Looks up the node in a hash table.
5118 * For Const nodes this is performed in the constructor, too. Const
5119 * nodes are extremely time critical because of their frequent use in
5120 * constant string arrays.
5122 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5125 if (!value_table) return n;
5129 o = pset_find(value_table, n, ir_node_hash(n));
5138 * During construction we set the op_pin_state_pinned flag in the graph right when the
5139 * optimization is performed. The flag turning on procedure global cse could
5140 * be changed between two allocations. This way we are safe.
5142 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5145 n = identify(value_table, n);
5146 if (get_irn_n(old, -1) != get_irn_n(n, -1))
5147 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5149 } /* identify_cons */
5152 * Return the canonical node computing the same value as n.
5153 * Looks up the node in a hash table, enters it in the table
5154 * if it isn't there yet.
5156 ir_node *identify_remember(pset *value_table, ir_node *n) {
5159 if (!value_table) return n;
5162 /* lookup or insert in hash table with given hash key. */
5163 o = pset_insert(value_table, n, ir_node_hash(n));
5170 } /* identify_remember */
5172 /* Add a node to the identities value table. */
5173 void add_identities(pset *value_table, ir_node *node) {
5174 if (get_opt_cse() && is_no_Block(node))
5175 identify_remember(value_table, node);
5176 } /* add_identities */
5178 /* Visit each node in the value table of a graph. */
5179 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5181 ir_graph *rem = current_ir_graph;
5183 current_ir_graph = irg;
5184 foreach_pset(irg->value_table, node)
5186 current_ir_graph = rem;
5187 } /* visit_all_identities */
5190 * Garbage in, garbage out. If a node has a dead input, i.e., the
5191 * Bad node is input to the node, return the Bad node.
5193 static ir_node *gigo(ir_node *node) {
5195 ir_op *op = get_irn_op(node);
5197 /* remove garbage blocks by looking at control flow that leaves the block
5198 and replacing the control flow by Bad. */
5199 if (get_irn_mode(node) == mode_X) {
5200 ir_node *block = get_nodes_block(skip_Proj(node));
5202 /* Don't optimize nodes in immature blocks. */
5203 if (!get_Block_matured(block)) return node;
5204 /* Don't optimize End, may have Bads. */
5205 if (op == op_End) return node;
5207 if (is_Block(block)) {
5208 irn_arity = get_irn_arity(block);
5209 for (i = 0; i < irn_arity; i++) {
5210 if (!is_Bad(get_irn_n(block, i)))
5213 if (i == irn_arity) {
5214 ir_graph *irg = get_irn_irg(block);
5215 /* the start block is never dead */
5216 if (block != get_irg_start_block(irg)
5217 && block != get_irg_end_block(irg))
5223 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5224 blocks predecessors is dead. */
5225 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5226 irn_arity = get_irn_arity(node);
5229 * Beware: we can only read the block of a non-floating node.
5231 if (is_irn_pinned_in_irg(node) &&
5232 is_Block_dead(get_nodes_block(node)))
5235 for (i = 0; i < irn_arity; i++) {
5236 ir_node *pred = get_irn_n(node, i);
5241 /* Propagating Unknowns here seems to be a bad idea, because
5242 sometimes we need a node as a input and did not want that
5244 However, it might be useful to move this into a later phase
5245 (if you think that optimizing such code is useful). */
5246 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5247 return new_Unknown(get_irn_mode(node));
5252 /* With this code we violate the agreement that local_optimize
5253 only leaves Bads in Block, Phi and Tuple nodes. */
5254 /* If Block has only Bads as predecessors it's garbage. */
5255 /* If Phi has only Bads as predecessors it's garbage. */
5256 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5257 irn_arity = get_irn_arity(node);
5258 for (i = 0; i < irn_arity; i++) {
5259 if (!is_Bad(get_irn_n(node, i))) break;
5261 if (i == irn_arity) node = new_Bad();
5268 * These optimizations deallocate nodes from the obstack.
5269 * It can only be called if it is guaranteed that no other nodes
5270 * reference this one, i.e., right after construction of a node.
5272 * @param n The node to optimize
5274 * current_ir_graph must be set to the graph of the node!
5276 ir_node *optimize_node(ir_node *n) {
5279 ir_opcode iro = get_irn_opcode(n);
5281 /* Always optimize Phi nodes: part of the construction. */
5282 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5284 /* constant expression evaluation / constant folding */
5285 if (get_opt_constant_folding()) {
5286 /* neither constants nor Tuple values can be evaluated */
5287 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5288 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5289 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5290 /* try to evaluate */
5291 tv = computed_value(n);
5292 if (tv != tarval_bad) {
5294 ir_type *old_tp = get_irn_type(n);
5295 int i, arity = get_irn_arity(n);
5299 * Try to recover the type of the new expression.
5301 for (i = 0; i < arity && !old_tp; ++i)
5302 old_tp = get_irn_type(get_irn_n(n, i));
5305 * we MUST copy the node here temporary, because it's still needed
5306 * for DBG_OPT_CSTEVAL
5308 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5309 oldn = alloca(node_size);
5311 memcpy(oldn, n, node_size);
5312 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5314 /* ARG, copy the in array, we need it for statistics */
5315 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5317 /* note the inplace edges module */
5318 edges_node_deleted(n, current_ir_graph);
5320 /* evaluation was successful -- replace the node. */
5321 irg_kill_node(current_ir_graph, n);
5322 nw = new_Const(get_tarval_mode(tv), tv);
5324 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5325 set_Const_type(nw, old_tp);
5326 DBG_OPT_CSTEVAL(oldn, nw);
5327 tarval_enable_fp_ops(old_fp_mode);
5330 tarval_enable_fp_ops(old_fp_mode);
5334 /* remove unnecessary nodes */
5335 if (get_opt_constant_folding() ||
5336 (iro == iro_Phi) || /* always optimize these nodes. */
5338 (iro == iro_Proj) ||
5339 (iro == iro_Block) ) /* Flags tested local. */
5340 n = equivalent_node(n);
5342 /* Common Subexpression Elimination.
5344 * Checks whether n is already available.
5345 * The block input is used to distinguish different subexpressions. Right
5346 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5347 * subexpressions within a block.
5350 n = identify_cons(current_ir_graph->value_table, n);
5353 edges_node_deleted(oldn, current_ir_graph);
5355 /* We found an existing, better node, so we can deallocate the old node. */
5356 irg_kill_node(current_ir_graph, oldn);
5360 /* Some more constant expression evaluation that does not allow to
5362 iro = get_irn_opcode(n);
5363 if (get_opt_constant_folding() ||
5364 (iro == iro_Cond) ||
5365 (iro == iro_Proj)) /* Flags tested local. */
5366 n = transform_node(n);
5368 /* Remove nodes with dead (Bad) input.
5369 Run always for transformation induced Bads. */
5372 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5373 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5374 n = identify_remember(current_ir_graph->value_table, n);
5378 } /* optimize_node */
5382 * These optimizations never deallocate nodes (in place). This can cause dead
5383 * nodes lying on the obstack. Remove these by a dead node elimination,
5384 * i.e., a copying garbage collection.
5386 ir_node *optimize_in_place_2(ir_node *n) {
5389 ir_opcode iro = get_irn_opcode(n);
5391 if (!get_opt_optimize() && !is_Phi(n)) return n;
5393 /* constant expression evaluation / constant folding */
5394 if (get_opt_constant_folding()) {
5395 /* neither constants nor Tuple values can be evaluated */
5396 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5397 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5398 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5399 /* try to evaluate */
5400 tv = computed_value(n);
5401 if (tv != tarval_bad) {
5402 /* evaluation was successful -- replace the node. */
5403 ir_type *old_tp = get_irn_type(n);
5404 int i, arity = get_irn_arity(n);
5407 * Try to recover the type of the new expression.
5409 for (i = 0; i < arity && !old_tp; ++i)
5410 old_tp = get_irn_type(get_irn_n(n, i));
5412 n = new_Const(get_tarval_mode(tv), tv);
5414 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5415 set_Const_type(n, old_tp);
5417 DBG_OPT_CSTEVAL(oldn, n);
5418 tarval_enable_fp_ops(old_fp_mode);
5421 tarval_enable_fp_ops(old_fp_mode);
5425 /* remove unnecessary nodes */
5426 if (get_opt_constant_folding() ||
5427 (iro == iro_Phi) || /* always optimize these nodes. */
5428 (iro == iro_Id) || /* ... */
5429 (iro == iro_Proj) || /* ... */
5430 (iro == iro_Block) ) /* Flags tested local. */
5431 n = equivalent_node(n);
5433 /** common subexpression elimination **/
5434 /* Checks whether n is already available. */
5435 /* The block input is used to distinguish different subexpressions. Right
5436 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5437 subexpressions within a block. */
5438 if (get_opt_cse()) {
5439 n = identify(current_ir_graph->value_table, n);
5442 /* Some more constant expression evaluation. */
5443 iro = get_irn_opcode(n);
5444 if (get_opt_constant_folding() ||
5445 (iro == iro_Cond) ||
5446 (iro == iro_Proj)) /* Flags tested local. */
5447 n = transform_node(n);
5449 /* Remove nodes with dead (Bad) input.
5450 Run always for transformation induced Bads. */
5453 /* Now we can verify the node, as it has no dead inputs any more. */
5456 /* Now we have a legal, useful node. Enter it in hash table for cse.
5457 Blocks should be unique anyways. (Except the successor of start:
5458 is cse with the start block!) */
5459 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5460 n = identify_remember(current_ir_graph->value_table, n);
5463 } /* optimize_in_place_2 */
5466 * Wrapper for external use, set proper status bits after optimization.
5468 ir_node *optimize_in_place(ir_node *n) {
5469 /* Handle graph state */
5470 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5472 if (get_opt_global_cse())
5473 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5474 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5475 set_irg_outs_inconsistent(current_ir_graph);
5477 /* FIXME: Maybe we could also test whether optimizing the node can
5478 change the control graph. */
5479 set_irg_doms_inconsistent(current_ir_graph);
5480 return optimize_in_place_2(n);
5481 } /* optimize_in_place */
5484 * Sets the default operation for an ir_ops.
5486 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5487 ops = firm_set_default_computed_value(code, ops);
5488 ops = firm_set_default_equivalent_node(code, ops);
5489 ops = firm_set_default_transform_node(code, ops);
5490 ops = firm_set_default_node_cmp_attr(code, ops);
5491 ops = firm_set_default_get_type(code, ops);
5492 ops = firm_set_default_get_type_attr(code, ops);
5493 ops = firm_set_default_get_entity_attr(code, ops);
5496 } /* firm_set_default_operations */