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 left if non-zero, other is the left operand, else the right
1718 * @return a new Phi node if the conversion was successful, NULL else
1720 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), 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 mode = get_irn_mode(phi);
1755 irg = current_ir_graph;
1756 for (i = 0; i < n; ++i) {
1757 pred = get_irn_n(phi, i);
1758 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1759 mode, res[i], get_Const_type(pred));
1761 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1762 } /* apply_binop_on_phi */
1765 * Apply an evaluator on a unop with a constant operator (a Phi).
1767 * @param phi the Phi node
1768 * @param eval an evaluator function
1770 * @return a new Phi node if the conversion was successful, NULL else
1772 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1778 int i, n = get_irn_arity(phi);
1780 NEW_ARR_A(void *, res, n);
1781 for (i = 0; i < n; ++i) {
1782 pred = get_irn_n(phi, i);
1783 tv = get_Const_tarval(pred);
1786 if (tv == tarval_bad) {
1787 /* folding failed, bad */
1792 mode = get_irn_mode(phi);
1793 irg = current_ir_graph;
1794 for (i = 0; i < n; ++i) {
1795 pred = get_irn_n(phi, i);
1796 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1797 mode, res[i], get_Const_type(pred));
1799 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1800 } /* apply_unop_on_phi */
1803 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1804 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1805 * If possible, remove the Conv's.
1807 static ir_node *transform_node_AddSub(ir_node *n) {
1808 ir_mode *mode = get_irn_mode(n);
1810 if (mode_is_reference(mode)) {
1811 ir_node *left = get_binop_left(n);
1812 ir_node *right = get_binop_right(n);
1813 int ref_bits = get_mode_size_bits(mode);
1815 if (is_Conv(left)) {
1816 ir_mode *mode = get_irn_mode(left);
1817 int bits = get_mode_size_bits(mode);
1819 if (ref_bits == bits &&
1820 mode_is_int(mode) &&
1821 get_mode_arithmetic(mode) == irma_twos_complement) {
1822 ir_node *pre = get_Conv_op(left);
1823 ir_mode *pre_mode = get_irn_mode(pre);
1825 if (mode_is_int(pre_mode) &&
1826 get_mode_size_bits(pre_mode) == bits &&
1827 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1828 /* ok, this conv just changes to sign, moreover the calculation
1829 * is done with same number of bits as our address mode, so
1830 * we can ignore the conv as address calculation can be viewed
1831 * as either signed or unsigned
1833 set_binop_left(n, pre);
1838 if (is_Conv(right)) {
1839 ir_mode *mode = get_irn_mode(right);
1840 int bits = get_mode_size_bits(mode);
1842 if (ref_bits == bits &&
1843 mode_is_int(mode) &&
1844 get_mode_arithmetic(mode) == irma_twos_complement) {
1845 ir_node *pre = get_Conv_op(right);
1846 ir_mode *pre_mode = get_irn_mode(pre);
1848 if (mode_is_int(pre_mode) &&
1849 get_mode_size_bits(pre_mode) == bits &&
1850 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1851 /* ok, this conv just changes to sign, moreover the calculation
1852 * is done with same number of bits as our address mode, so
1853 * we can ignore the conv as address calculation can be viewed
1854 * as either signed or unsigned
1856 set_binop_right(n, pre);
1862 } /* transform_node_AddSub */
1864 #define HANDLE_BINOP_PHI(op,a,b,c) \
1866 if (is_Const(b) && is_const_Phi(a)) { \
1867 /* check for Op(Phi, Const) */ \
1868 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1870 else if (is_Const(a) && is_const_Phi(b)) { \
1871 /* check for Op(Const, Phi) */ \
1872 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1875 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1879 #define HANDLE_UNOP_PHI(op,a,c) \
1881 if (is_const_Phi(a)) { \
1882 /* check for Op(Phi) */ \
1883 c = apply_unop_on_phi(a, op); \
1885 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1891 * Do the AddSub optimization, then Transform
1892 * Constant folding on Phi
1893 * Add(a,a) -> Mul(a, 2)
1894 * Add(Mul(a, x), a) -> Mul(a, x+1)
1895 * if the mode is integer or float.
1896 * Transform Add(a,-b) into Sub(a,b).
1897 * Reassociation might fold this further.
1899 static ir_node *transform_node_Add(ir_node *n) {
1901 ir_node *a, *b, *c, *oldn = n;
1903 n = transform_node_AddSub(n);
1905 a = get_Add_left(n);
1906 b = get_Add_right(n);
1908 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1910 mode = get_irn_mode(n);
1912 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1913 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1916 if (mode_is_num(mode)) {
1917 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1918 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1919 ir_node *block = get_irn_n(n, -1);
1922 get_irn_dbg_info(n),
1926 new_r_Const_long(current_ir_graph, block, mode, 2),
1928 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1933 get_irn_dbg_info(n),
1939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1944 get_irn_dbg_info(n),
1950 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1953 if (! is_reassoc_running()) {
1954 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1956 ir_node *ma = get_Mul_left(a);
1957 ir_node *mb = get_Mul_right(a);
1960 ir_node *blk = get_irn_n(n, -1);
1962 get_irn_dbg_info(n), current_ir_graph, blk,
1965 get_irn_dbg_info(n), current_ir_graph, blk,
1967 new_r_Const_long(current_ir_graph, blk, mode, 1),
1970 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1972 } else if (b == mb) {
1973 ir_node *blk = get_irn_n(n, -1);
1975 get_irn_dbg_info(n), current_ir_graph, blk,
1978 get_irn_dbg_info(n), current_ir_graph, blk,
1980 new_r_Const_long(current_ir_graph, blk, mode, 1),
1983 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1988 ir_node *ma = get_Mul_left(b);
1989 ir_node *mb = get_Mul_right(b);
1992 ir_node *blk = get_irn_n(n, -1);
1994 get_irn_dbg_info(n), current_ir_graph, blk,
1997 get_irn_dbg_info(n), current_ir_graph, blk,
1999 new_r_Const_long(current_ir_graph, blk, mode, 1),
2002 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
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 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2022 /* Here we rely on constants be on the RIGHT side */
2024 ir_node *op = get_Not_op(a);
2026 if (is_Const(b) && is_Const_one(b)) {
2028 ir_node *blk = get_irn_n(n, -1);
2029 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2030 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2035 ir_node *blk = get_irn_n(n, -1);
2036 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2037 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2042 ir_node *op = get_Not_op(b);
2046 ir_node *blk = get_irn_n(n, -1);
2047 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2048 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2055 } /* transform_node_Add */
2058 static ir_node *const_negate(ir_node *cnst) {
2059 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2060 dbg_info *dbgi = get_irn_dbg_info(cnst);
2061 ir_graph *irg = get_irn_irg(cnst);
2062 ir_node *block = get_nodes_block(cnst);
2063 ir_mode *mode = get_irn_mode(cnst);
2064 if (tv == tarval_bad) return NULL;
2065 return new_rd_Const(dbgi, irg, block, mode, tv);
2069 * Do the AddSub optimization, then Transform
2070 * Constant folding on Phi
2071 * Sub(0,a) -> Minus(a)
2072 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2073 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2074 * Sub(Add(a, x), x) -> a
2075 * Sub(x, Add(x, a)) -> -a
2076 * Sub(x, Const) -> Add(x, -Const)
2078 static ir_node *transform_node_Sub(ir_node *n) {
2083 n = transform_node_AddSub(n);
2085 a = get_Sub_left(n);
2086 b = get_Sub_right(n);
2088 mode = get_irn_mode(n);
2091 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2093 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2094 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2097 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2098 /* a - C -> a + (-C) */
2099 ir_node *cnst = const_negate(b);
2101 ir_node *block = get_nodes_block(n);
2102 dbg_info *dbgi = get_irn_dbg_info(n);
2103 ir_graph *irg = get_irn_irg(n);
2105 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2111 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2112 ir_graph *irg = current_ir_graph;
2113 dbg_info *dbg = get_irn_dbg_info(n);
2114 ir_node *block = get_nodes_block(n);
2115 ir_node *left = get_Minus_op(a);
2116 ir_mode *mode = get_irn_mode(n);
2117 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2119 n = new_rd_Minus(dbg, irg, block, add, mode);
2120 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2122 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2123 ir_graph *irg = current_ir_graph;
2124 dbg_info *dbg = get_irn_dbg_info(n);
2125 ir_node *block = get_nodes_block(n);
2126 ir_node *right = get_Minus_op(b);
2127 ir_mode *mode = get_irn_mode(n);
2129 n = new_rd_Add(dbg, irg, block, a, right, mode);
2130 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2132 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2133 ir_graph *irg = current_ir_graph;
2134 dbg_info *s_dbg = get_irn_dbg_info(b);
2135 ir_node *s_block = get_nodes_block(b);
2136 ir_node *s_left = get_Sub_right(b);
2137 ir_node *s_right = get_Sub_left(b);
2138 ir_mode *s_mode = get_irn_mode(b);
2139 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2140 dbg_info *a_dbg = get_irn_dbg_info(n);
2141 ir_node *a_block = get_nodes_block(n);
2142 ir_mode *a_mode = get_irn_mode(n);
2144 n = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2145 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2147 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2148 ir_node *m_right = get_Mul_right(b);
2149 if (is_Const(m_right)) {
2150 ir_node *cnst2 = const_negate(m_right);
2151 if (cnst2 != NULL) {
2152 ir_graph *irg = current_ir_graph;
2153 dbg_info *m_dbg = get_irn_dbg_info(b);
2154 ir_node *m_block = get_nodes_block(b);
2155 ir_node *m_left = get_Mul_left(b);
2156 ir_mode *m_mode = get_irn_mode(b);
2157 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2158 dbg_info *a_dbg = get_irn_dbg_info(n);
2159 ir_node *a_block = get_nodes_block(n);
2160 ir_mode *a_mode = get_irn_mode(n);
2162 n = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2169 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2170 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2172 get_irn_dbg_info(n),
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2181 if (mode_wrap_around(mode)) {
2182 ir_node *left = get_Add_left(a);
2183 ir_node *right = get_Add_right(a);
2185 /* FIXME: Does the Conv's work only for two complement or generally? */
2187 if (mode != get_irn_mode(right)) {
2188 /* This Sub is an effective Cast */
2189 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2192 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2194 } else if (right == b) {
2195 if (mode != get_irn_mode(left)) {
2196 /* This Sub is an effective Cast */
2197 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2200 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2206 if (mode_wrap_around(mode)) {
2207 ir_node *left = get_Add_left(b);
2208 ir_node *right = get_Add_right(b);
2210 /* FIXME: Does the Conv's work only for two complement or generally? */
2212 ir_mode *r_mode = get_irn_mode(right);
2214 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2215 if (mode != r_mode) {
2216 /* This Sub is an effective Cast */
2217 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2219 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2221 } else if (right == a) {
2222 ir_mode *l_mode = get_irn_mode(left);
2224 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2225 if (mode != l_mode) {
2226 /* This Sub is an effective Cast */
2227 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2229 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2234 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2235 ir_mode *mode = get_irn_mode(a);
2237 if (mode == get_irn_mode(b)) {
2243 /* check if it's allowed to skip the conv */
2244 ma = get_irn_mode(a);
2245 mb = get_irn_mode(b);
2247 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2248 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2250 set_Sub_right(n, b);
2256 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2257 if (!is_reassoc_running() && is_Mul(a)) {
2258 ir_node *ma = get_Mul_left(a);
2259 ir_node *mb = get_Mul_right(a);
2262 ir_node *blk = get_irn_n(n, -1);
2264 get_irn_dbg_info(n),
2265 current_ir_graph, blk,
2268 get_irn_dbg_info(n),
2269 current_ir_graph, blk,
2271 new_r_Const_long(current_ir_graph, blk, mode, 1),
2274 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2276 } else if (mb == b) {
2277 ir_node *blk = get_irn_n(n, -1);
2279 get_irn_dbg_info(n),
2280 current_ir_graph, blk,
2283 get_irn_dbg_info(n),
2284 current_ir_graph, blk,
2286 new_r_Const_long(current_ir_graph, blk, mode, 1),
2289 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2293 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2294 ir_node *x = get_Sub_left(a);
2295 ir_node *y = get_Sub_right(a);
2296 ir_node *blk = get_irn_n(n, -1);
2297 ir_mode *m_b = get_irn_mode(b);
2298 ir_mode *m_y = get_irn_mode(y);
2302 /* Determine the right mode for the Add. */
2305 else if (mode_is_reference(m_b))
2307 else if (mode_is_reference(m_y))
2311 * Both modes are different but none is reference,
2312 * happens for instance in SubP(SubP(P, Iu), Is).
2313 * We have two possibilities here: Cast or ignore.
2314 * Currently we ignore this case.
2319 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2321 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2322 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2326 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2327 if (is_Const(a) && is_Not(b)) {
2328 /* c - ~X = X + (c+1) */
2329 tarval *tv = get_Const_tarval(a);
2331 tv = tarval_add(tv, get_mode_one(mode));
2332 if (tv != tarval_bad) {
2333 ir_node *blk = get_irn_n(n, -1);
2334 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2335 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2336 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2342 } /* transform_node_Sub */
2345 * Several transformation done on n*n=2n bits mul.
2346 * These transformations must be done here because new nodes may be produced.
2348 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2350 ir_node *a = get_Mul_left(n);
2351 ir_node *b = get_Mul_right(n);
2352 tarval *ta = value_of(a);
2353 tarval *tb = value_of(b);
2354 ir_mode *smode = get_irn_mode(a);
2356 if (ta == get_mode_one(smode)) {
2357 ir_node *blk = get_irn_n(n, -1);
2358 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2359 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2362 else if (ta == get_mode_minus_one(smode)) {
2363 ir_node *blk = get_irn_n(n, -1);
2364 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2365 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2369 if (tb == get_mode_one(smode)) {
2370 ir_node *blk = get_irn_n(a, -1);
2371 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2372 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2375 else if (tb == get_mode_minus_one(smode)) {
2376 ir_node *blk = get_irn_n(n, -1);
2377 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2378 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2379 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2386 * Transform Mul(a,-1) into -a.
2387 * Do constant evaluation of Phi nodes.
2388 * Do architecture dependent optimizations on Mul nodes
2390 static ir_node *transform_node_Mul(ir_node *n) {
2391 ir_node *c, *oldn = n;
2392 ir_mode *mode = get_irn_mode(n);
2393 ir_node *a = get_Mul_left(n);
2394 ir_node *b = get_Mul_right(n);
2396 if (is_Bad(a) || is_Bad(b))
2399 if (mode != get_irn_mode(a))
2400 return transform_node_Mul2n(n, mode);
2402 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2404 if (mode_is_signed(mode)) {
2407 if (value_of(a) == get_mode_minus_one(mode))
2409 else if (value_of(b) == get_mode_minus_one(mode))
2412 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2413 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2418 if (is_Const(b)) { /* (-a) * const -> a * -const */
2419 ir_node *cnst = const_negate(b);
2421 dbg_info *dbgi = get_irn_dbg_info(n);
2422 ir_node *block = get_nodes_block(n);
2423 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2424 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2427 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2428 dbg_info *dbgi = get_irn_dbg_info(n);
2429 ir_node *block = get_nodes_block(n);
2430 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2431 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2433 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2434 ir_node *sub_l = get_Sub_left(b);
2435 ir_node *sub_r = get_Sub_right(b);
2436 dbg_info *dbgi = get_irn_dbg_info(n);
2437 ir_graph *irg = current_ir_graph;
2438 ir_node *block = get_nodes_block(n);
2439 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2440 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2441 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2444 } else if (is_Minus(b)) {
2445 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2446 ir_node *sub_l = get_Sub_left(a);
2447 ir_node *sub_r = get_Sub_right(a);
2448 dbg_info *dbgi = get_irn_dbg_info(n);
2449 ir_graph *irg = current_ir_graph;
2450 ir_node *block = get_nodes_block(n);
2451 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2452 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2453 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2457 if (get_mode_arithmetic(mode) == irma_ieee754) {
2459 tarval *tv = get_Const_tarval(a);
2460 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2461 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2462 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2466 else if (is_Const(b)) {
2467 tarval *tv = get_Const_tarval(b);
2468 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2469 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2470 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2475 return arch_dep_replace_mul_with_shifts(n);
2476 } /* transform_node_Mul */
2479 * Transform a Div Node.
2481 static ir_node *transform_node_Div(ir_node *n) {
2482 tarval *tv = value_of(n);
2483 ir_mode *mode = get_Div_resmode(n);
2486 if (tv != tarval_bad) {
2487 value = new_Const(get_tarval_mode(tv), tv);
2489 DBG_OPT_CSTEVAL(n, value);
2492 ir_node *a = get_Div_left(n);
2493 ir_node *b = get_Div_right(n);
2496 if (a == b && value_not_zero(a, &dummy)) {
2497 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2498 value = new_Const(mode, get_mode_one(mode));
2499 DBG_OPT_CSTEVAL(n, value);
2502 if (mode_is_signed(mode) && is_Const(b)) {
2503 tarval *tv = get_Const_tarval(b);
2505 if (tv == get_mode_minus_one(mode)) {
2507 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2508 DBG_OPT_CSTEVAL(n, value);
2512 /* Try architecture dependent optimization */
2513 value = arch_dep_replace_div_by_const(n);
2521 /* Turn Div into a tuple (mem, jmp, bad, value) */
2522 mem = get_Div_mem(n);
2523 blk = get_irn_n(n, -1);
2525 /* skip a potential Pin */
2527 mem = get_Pin_op(mem);
2528 turn_into_tuple(n, pn_Div_max);
2529 set_Tuple_pred(n, pn_Div_M, mem);
2530 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2531 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2532 set_Tuple_pred(n, pn_Div_res, value);
2535 } /* transform_node_Div */
2538 * Transform a Mod node.
2540 static ir_node *transform_node_Mod(ir_node *n) {
2541 tarval *tv = value_of(n);
2542 ir_mode *mode = get_Mod_resmode(n);
2545 if (tv != tarval_bad) {
2546 value = new_Const(get_tarval_mode(tv), tv);
2548 DBG_OPT_CSTEVAL(n, value);
2551 ir_node *a = get_Mod_left(n);
2552 ir_node *b = get_Mod_right(n);
2555 if (a == b && value_not_zero(a, &dummy)) {
2556 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2557 value = new_Const(mode, get_mode_null(mode));
2558 DBG_OPT_CSTEVAL(n, value);
2561 if (mode_is_signed(mode) && is_Const(b)) {
2562 tarval *tv = get_Const_tarval(b);
2564 if (tv == get_mode_minus_one(mode)) {
2566 value = new_Const(mode, get_mode_null(mode));
2567 DBG_OPT_CSTEVAL(n, value);
2571 /* Try architecture dependent optimization */
2572 value = arch_dep_replace_mod_by_const(n);
2580 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2581 mem = get_Mod_mem(n);
2582 blk = get_irn_n(n, -1);
2584 /* skip a potential Pin */
2586 mem = get_Pin_op(mem);
2587 turn_into_tuple(n, pn_Mod_max);
2588 set_Tuple_pred(n, pn_Mod_M, mem);
2589 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2590 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2591 set_Tuple_pred(n, pn_Mod_res, value);
2594 } /* transform_node_Mod */
2597 * Transform a DivMod node.
2599 static ir_node *transform_node_DivMod(ir_node *n) {
2601 ir_node *a = get_DivMod_left(n);
2602 ir_node *b = get_DivMod_right(n);
2603 ir_mode *mode = get_DivMod_resmode(n);
2604 tarval *ta = value_of(a);
2605 tarval *tb = value_of(b);
2608 if (tb != tarval_bad) {
2609 if (tb == get_mode_one(get_tarval_mode(tb))) {
2610 b = new_Const(mode, get_mode_null(mode));
2611 DBG_OPT_CSTEVAL(n, b);
2613 } else if (ta != tarval_bad) {
2614 tarval *resa, *resb;
2615 resa = tarval_div(ta, tb);
2616 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2617 Jmp for X result!? */
2618 resb = tarval_mod(ta, tb);
2619 if (resb == tarval_bad) return n; /* Causes exception! */
2620 a = new_Const(mode, resa);
2621 b = new_Const(mode, resb);
2622 DBG_OPT_CSTEVAL(n, a);
2623 DBG_OPT_CSTEVAL(n, b);
2625 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2626 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2627 b = new_Const(mode, get_mode_null(mode));
2628 DBG_OPT_CSTEVAL(n, a);
2629 DBG_OPT_CSTEVAL(n, b);
2631 } else { /* Try architecture dependent optimization */
2632 arch_dep_replace_divmod_by_const(&a, &b, n);
2633 evaluated = a != NULL;
2635 } else if (a == b) {
2636 if (value_not_zero(a, &dummy)) {
2638 a = new_Const(mode, get_mode_one(mode));
2639 b = new_Const(mode, get_mode_null(mode));
2640 DBG_OPT_CSTEVAL(n, a);
2641 DBG_OPT_CSTEVAL(n, b);
2644 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2647 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2648 /* 0 / non-Const = 0 */
2653 if (evaluated) { /* replace by tuple */
2657 mem = get_DivMod_mem(n);
2658 /* skip a potential Pin */
2660 mem = get_Pin_op(mem);
2662 blk = get_irn_n(n, -1);
2663 turn_into_tuple(n, pn_DivMod_max);
2664 set_Tuple_pred(n, pn_DivMod_M, mem);
2665 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2666 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2667 set_Tuple_pred(n, pn_DivMod_res_div, a);
2668 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2672 } /* transform_node_DivMod */
2675 * Optimize x / c to x * (1/c)
2677 static ir_node *transform_node_Quot(ir_node *n) {
2678 ir_mode *mode = get_Quot_resmode(n);
2681 if (get_mode_arithmetic(mode) == irma_ieee754) {
2682 ir_node *b = get_Quot_right(n);
2685 tarval *tv = get_Const_tarval(b);
2687 tv = tarval_quo(get_mode_one(mode), tv);
2689 /* Do the transformation if the result is either exact or we are not
2690 using strict rules. */
2691 if (tv != tarval_bad &&
2692 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2693 ir_node *blk = get_irn_n(n, -1);
2694 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2695 ir_node *a = get_Quot_left(n);
2696 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2697 ir_node *mem = get_Quot_mem(n);
2699 /* skip a potential Pin */
2701 mem = get_Pin_op(mem);
2702 turn_into_tuple(n, pn_Quot_max);
2703 set_Tuple_pred(n, pn_Quot_M, mem);
2704 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2705 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2706 set_Tuple_pred(n, pn_Quot_res, m);
2707 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2712 } /* transform_node_Quot */
2715 * Optimize Abs(x) into x if x is Confirmed >= 0
2716 * Optimize Abs(x) into -x if x is Confirmed <= 0
2718 static ir_node *transform_node_Abs(ir_node *n) {
2720 ir_node *a = get_Abs_op(n);
2721 value_classify_sign sign = classify_value_sign(a);
2723 if (sign == value_classified_negative) {
2724 ir_mode *mode = get_irn_mode(n);
2727 * We can replace the Abs by -x here.
2728 * We even could add a new Confirm here.
2730 * Note that -x would create a new node, so we could
2731 * not run it in the equivalent_node() context.
2733 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2734 get_irn_n(n, -1), a, mode);
2736 DBG_OPT_CONFIRM(oldn, n);
2737 } else if (sign == value_classified_positive) {
2738 /* n is positive, Abs is not needed */
2741 DBG_OPT_CONFIRM(oldn, n);
2745 } /* transform_node_Abs */
2748 * Transform a Cond node.
2750 * Replace the Cond by a Jmp if it branches on a constant
2753 static ir_node *transform_node_Cond(ir_node *n) {
2756 ir_node *a = get_Cond_selector(n);
2757 tarval *ta = value_of(a);
2759 /* we need block info which is not available in floating irgs */
2760 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2763 if ((ta != tarval_bad) &&
2764 (get_irn_mode(a) == mode_b) &&
2765 (get_opt_unreachable_code())) {
2766 /* It's a boolean Cond, branching on a boolean constant.
2767 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2768 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2769 turn_into_tuple(n, pn_Cond_max);
2770 if (ta == tarval_b_true) {
2771 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2772 set_Tuple_pred(n, pn_Cond_true, jmp);
2774 set_Tuple_pred(n, pn_Cond_false, jmp);
2775 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2777 /* We might generate an endless loop, so keep it alive. */
2778 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2781 } /* transform_node_Cond */
2783 typedef ir_node* (*recursive_transform) (ir_node *n);
2786 * makes use of distributive laws for and, or, eor
2787 * and(a OP c, b OP c) -> and(a, b) OP c
2788 * note, might return a different op than n
2790 static ir_node *transform_bitwise_distributive(ir_node *n,
2791 recursive_transform trans_func)
2794 ir_node *a = get_binop_left(n);
2795 ir_node *b = get_binop_right(n);
2796 ir_op *op = get_irn_op(a);
2797 ir_op *op_root = get_irn_op(n);
2799 if(op != get_irn_op(b))
2802 if (op == op_Conv) {
2803 ir_node *a_op = get_Conv_op(a);
2804 ir_node *b_op = get_Conv_op(b);
2805 ir_mode *a_mode = get_irn_mode(a_op);
2806 ir_mode *b_mode = get_irn_mode(b_op);
2807 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2808 ir_node *blk = get_irn_n(n, -1);
2811 set_binop_left(n, a_op);
2812 set_binop_right(n, b_op);
2813 set_irn_mode(n, a_mode);
2815 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2817 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2823 /* nothing to gain here */
2827 if (op == op_Shrs || op == op_Shr || op == op_Shl
2828 || op == op_And || op == op_Or || op == op_Eor) {
2829 ir_node *a_left = get_binop_left(a);
2830 ir_node *a_right = get_binop_right(a);
2831 ir_node *b_left = get_binop_left(b);
2832 ir_node *b_right = get_binop_right(b);
2836 if (is_op_commutative(op)) {
2837 if (a_left == b_left) {
2841 } else if(a_left == b_right) {
2845 } else if(a_right == b_left) {
2851 if(a_right == b_right) {
2858 /* (a sop c) & (b sop c) => (a & b) sop c */
2859 ir_node *blk = get_irn_n(n, -1);
2861 ir_node *new_n = exact_copy(n);
2862 set_binop_left(new_n, op1);
2863 set_binop_right(new_n, op2);
2864 new_n = trans_func(new_n);
2866 if(op_root == op_Eor && op == op_Or) {
2867 dbg_info *dbgi = get_irn_dbg_info(n);
2868 ir_graph *irg = current_ir_graph;
2869 ir_mode *mode = get_irn_mode(c);
2871 c = new_rd_Not(dbgi, irg, blk, c, mode);
2872 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2875 set_irn_n(n, -1, blk);
2876 set_binop_left(n, new_n);
2877 set_binop_right(n, c);
2880 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2891 static ir_node *transform_node_And(ir_node *n) {
2892 ir_node *c, *oldn = n;
2893 ir_node *a = get_And_left(n);
2894 ir_node *b = get_And_right(n);
2897 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2899 mode = get_irn_mode(n);
2901 /* we can evaluate 2 Projs of the same Cmp */
2902 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2903 ir_node *pred_a = get_Proj_pred(a);
2904 ir_node *pred_b = get_Proj_pred(b);
2905 if (pred_a == pred_b) {
2906 dbg_info *dbgi = get_irn_dbg_info(n);
2907 ir_node *block = get_nodes_block(pred_a);
2908 pn_Cmp pn_a = get_Proj_proj(a);
2909 pn_Cmp pn_b = get_Proj_proj(b);
2910 /* yes, we can simply calculate with pncs */
2911 pn_Cmp new_pnc = pn_a & pn_b;
2913 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2918 ir_node *op = get_Not_op(b);
2920 ir_node *ba = get_And_left(op);
2921 ir_node *bb = get_And_right(op);
2923 /* it's enough to test the following cases due to normalization! */
2924 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2925 /* (a|b) & ~(a&b) = a^b */
2926 ir_node *block = get_nodes_block(n);
2928 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2937 ir_node *op = get_Not_op(a);
2939 ir_node *aa = get_And_left(op);
2940 ir_node *ab = get_And_right(op);
2942 /* it's enough to test the following cases due to normalization! */
2943 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2944 /* (a|b) & ~(a&b) = a^b */
2945 ir_node *block = get_nodes_block(n);
2947 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2948 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2955 ir_node *al = get_Eor_left(a);
2956 ir_node *ar = get_Eor_right(a);
2959 /* (b ^ a) & b -> ~a & b */
2960 dbg_info *dbg = get_irn_dbg_info(n);
2961 ir_node *block = get_nodes_block(n);
2963 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
2964 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
2965 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2969 /* (a ^ b) & b -> ~a & b */
2970 dbg_info *dbg = get_irn_dbg_info(n);
2971 ir_node *block = get_nodes_block(n);
2973 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
2974 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
2975 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2980 ir_node *bl = get_Eor_left(b);
2981 ir_node *br = get_Eor_right(b);
2984 /* a & (a ^ b) -> a & ~b */
2985 dbg_info *dbg = get_irn_dbg_info(n);
2986 ir_node *block = get_nodes_block(n);
2988 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
2989 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
2990 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2994 /* a & (b ^ a) -> a & ~b */
2995 dbg_info *dbg = get_irn_dbg_info(n);
2996 ir_node *block = get_nodes_block(n);
2998 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
2999 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3000 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3004 if (is_Not(a) && is_Not(b)) {
3005 /* ~a & ~b = ~(a|b) */
3006 ir_node *block = get_nodes_block(n);
3007 ir_mode *mode = get_irn_mode(n);
3011 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3012 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3013 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3017 n = transform_bitwise_distributive(n, transform_node_And);
3020 } /* transform_node_And */
3025 static ir_node *transform_node_Eor(ir_node *n) {
3026 ir_node *c, *oldn = n;
3027 ir_node *a = get_Eor_left(n);
3028 ir_node *b = get_Eor_right(n);
3029 ir_mode *mode = get_irn_mode(n);
3031 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
3033 /* we can evaluate 2 Projs of the same Cmp */
3034 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3035 ir_node *pred_a = get_Proj_pred(a);
3036 ir_node *pred_b = get_Proj_pred(b);
3037 if(pred_a == pred_b) {
3038 dbg_info *dbgi = get_irn_dbg_info(n);
3039 ir_node *block = get_nodes_block(pred_a);
3040 pn_Cmp pn_a = get_Proj_proj(a);
3041 pn_Cmp pn_b = get_Proj_proj(b);
3042 /* yes, we can simply calculate with pncs */
3043 pn_Cmp new_pnc = pn_a ^ pn_b;
3045 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3052 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3053 mode, get_mode_null(mode));
3054 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3055 } else if (mode == mode_b &&
3057 is_Const(b) && is_Const_one(b) &&
3058 is_Cmp(get_Proj_pred(a))) {
3059 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3060 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3061 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3063 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3064 } else if (is_Const(b)) {
3065 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3066 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3067 ir_node *not_op = get_Not_op(a);
3068 dbg_info *dbg = get_irn_dbg_info(n);
3069 ir_graph *irg = current_ir_graph;
3070 ir_node *block = get_nodes_block(n);
3071 ir_mode *mode = get_irn_mode(n);
3072 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3074 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3075 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3076 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3079 n = transform_bitwise_distributive(n, transform_node_Eor);
3083 } /* transform_node_Eor */
3088 static ir_node *transform_node_Not(ir_node *n) {
3089 ir_node *c, *oldn = n;
3090 ir_node *a = get_Not_op(n);
3091 ir_mode *mode = get_irn_mode(n);
3093 HANDLE_UNOP_PHI(tarval_not,a,c);
3095 /* check for a boolean Not */
3096 if (mode == mode_b &&
3098 is_Cmp(get_Proj_pred(a))) {
3099 /* We negate a Cmp. The Cmp has the negated result anyways! */
3100 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3101 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3102 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3106 ir_node *eor_b = get_Eor_right(a);
3107 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3108 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3109 ir_node *eor_a = get_Eor_left(a);
3110 dbg_info *dbg = get_irn_dbg_info(n);
3111 ir_graph *irg = current_ir_graph;
3112 ir_node *block = get_nodes_block(n);
3113 ir_mode *mode = get_irn_mode(n);
3114 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3118 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3119 if (is_Minus(a)) { /* ~-x -> x + -1 */
3120 dbg_info *dbg = get_irn_dbg_info(n);
3121 ir_graph *irg = current_ir_graph;
3122 ir_node *block = get_nodes_block(n);
3123 ir_node *add_l = get_Minus_op(a);
3124 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3125 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3126 } else if (is_Add(a)) {
3127 ir_node *add_r = get_Add_right(a);
3128 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3129 /* ~(x + -1) = -x */
3130 ir_node *op = get_Add_left(a);
3131 ir_node *blk = get_irn_n(n, -1);
3132 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3133 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3138 } /* transform_node_Not */
3141 * Transform a Minus.
3146 static ir_node *transform_node_Minus(ir_node *n) {
3147 ir_node *c, *oldn = n;
3148 ir_node *a = get_Minus_op(n);
3151 HANDLE_UNOP_PHI(tarval_neg,a,c);
3153 mode = get_irn_mode(a);
3154 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3155 /* the following rules are only to twos-complement */
3158 ir_node *op = get_Not_op(a);
3159 tarval *tv = get_mode_one(mode);
3160 ir_node *blk = get_irn_n(n, -1);
3161 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3162 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3163 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3167 ir_node *c = get_Shr_right(a);
3170 tarval *tv = get_Const_tarval(c);
3172 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3173 /* -(a >>u (size-1)) = a >>s (size-1) */
3174 ir_node *v = get_Shr_left(a);
3176 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3177 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3183 ir_node *c = get_Shrs_right(a);
3186 tarval *tv = get_Const_tarval(c);
3188 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3189 /* -(a >>s (size-1)) = a >>u (size-1) */
3190 ir_node *v = get_Shrs_left(a);
3192 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3193 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3200 /* - (a-b) = b - a */
3201 ir_node *la = get_Sub_left(a);
3202 ir_node *ra = get_Sub_right(a);
3203 ir_node *blk = get_irn_n(n, -1);
3205 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3206 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3210 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3211 ir_node *mul_l = get_Mul_left(a);
3212 ir_node *mul_r = get_Mul_right(a);
3213 if (is_Const(mul_r)) {
3214 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3215 ir_node *cnst = new_Const(mode, tv);
3216 dbg_info *dbg = get_irn_dbg_info(a);
3217 ir_graph *irg = current_ir_graph;
3218 ir_node *block = get_nodes_block(a);
3219 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3225 } /* transform_node_Minus */
3228 * Transform a Cast_type(Const) into a new Const_type
3230 static ir_node *transform_node_Cast(ir_node *n) {
3232 ir_node *pred = get_Cast_op(n);
3233 ir_type *tp = get_irn_type(n);
3235 if (is_Const(pred) && get_Const_type(pred) != tp) {
3236 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3237 get_Const_tarval(pred), tp);
3238 DBG_OPT_CSTEVAL(oldn, n);
3239 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3240 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3241 get_SymConst_kind(pred), tp);
3242 DBG_OPT_CSTEVAL(oldn, n);
3246 } /* transform_node_Cast */
3249 * Transform a Proj(Div) with a non-zero value.
3250 * Removes the exceptions and routes the memory to the NoMem node.
3252 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3253 ir_node *div = get_Proj_pred(proj);
3254 ir_node *b = get_Div_right(div);
3255 ir_node *confirm, *res, *new_mem;
3258 if (value_not_zero(b, &confirm)) {
3259 /* div(x, y) && y != 0 */
3260 if (confirm == NULL) {
3261 /* we are sure we have a Const != 0 */
3262 new_mem = get_Div_mem(div);
3263 if (is_Pin(new_mem))
3264 new_mem = get_Pin_op(new_mem);
3265 set_Div_mem(div, new_mem);
3266 set_irn_pinned(div, op_pin_state_floats);
3269 proj_nr = get_Proj_proj(proj);
3271 case pn_Div_X_regular:
3272 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3274 case pn_Div_X_except:
3275 /* we found an exception handler, remove it */
3276 DBG_OPT_EXC_REM(proj);
3280 res = get_Div_mem(div);
3281 new_mem = get_irg_no_mem(current_ir_graph);
3284 /* This node can only float up to the Confirm block */
3285 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3287 set_irn_pinned(div, op_pin_state_floats);
3288 /* this is a Div without exception, we can remove the memory edge */
3289 set_Div_mem(div, new_mem);
3294 } /* transform_node_Proj_Div */
3297 * Transform a Proj(Mod) with a non-zero value.
3298 * Removes the exceptions and routes the memory to the NoMem node.
3300 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3301 ir_node *mod = get_Proj_pred(proj);
3302 ir_node *b = get_Mod_right(mod);
3303 ir_node *confirm, *res, *new_mem;
3306 if (value_not_zero(b, &confirm)) {
3307 /* mod(x, y) && y != 0 */
3308 proj_nr = get_Proj_proj(proj);
3310 if (confirm == NULL) {
3311 /* we are sure we have a Const != 0 */
3312 new_mem = get_Mod_mem(mod);
3313 if (is_Pin(new_mem))
3314 new_mem = get_Pin_op(new_mem);
3315 set_Mod_mem(mod, new_mem);
3316 set_irn_pinned(mod, op_pin_state_floats);
3321 case pn_Mod_X_regular:
3322 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3324 case pn_Mod_X_except:
3325 /* we found an exception handler, remove it */
3326 DBG_OPT_EXC_REM(proj);
3330 res = get_Mod_mem(mod);
3331 new_mem = get_irg_no_mem(current_ir_graph);
3334 /* This node can only float up to the Confirm block */
3335 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3337 /* this is a Mod without exception, we can remove the memory edge */
3338 set_Mod_mem(mod, new_mem);
3341 if (get_Mod_left(mod) == b) {
3342 /* a % a = 0 if a != 0 */
3343 ir_mode *mode = get_irn_mode(proj);
3344 ir_node *res = new_Const(mode, get_mode_null(mode));
3346 DBG_OPT_CSTEVAL(mod, res);
3352 } /* transform_node_Proj_Mod */
3355 * Transform a Proj(DivMod) with a non-zero value.
3356 * Removes the exceptions and routes the memory to the NoMem node.
3358 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3359 ir_node *divmod = get_Proj_pred(proj);
3360 ir_node *b = get_DivMod_right(divmod);
3361 ir_node *confirm, *res, *new_mem;
3364 if (value_not_zero(b, &confirm)) {
3365 /* DivMod(x, y) && y != 0 */
3366 proj_nr = get_Proj_proj(proj);
3368 if (confirm == NULL) {
3369 /* we are sure we have a Const != 0 */
3370 new_mem = get_DivMod_mem(divmod);
3371 if (is_Pin(new_mem))
3372 new_mem = get_Pin_op(new_mem);
3373 set_DivMod_mem(divmod, new_mem);
3374 set_irn_pinned(divmod, op_pin_state_floats);
3379 case pn_DivMod_X_regular:
3380 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3382 case pn_DivMod_X_except:
3383 /* we found an exception handler, remove it */
3384 DBG_OPT_EXC_REM(proj);
3388 res = get_DivMod_mem(divmod);
3389 new_mem = get_irg_no_mem(current_ir_graph);
3392 /* This node can only float up to the Confirm block */
3393 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3395 /* this is a DivMod without exception, we can remove the memory edge */
3396 set_DivMod_mem(divmod, new_mem);
3399 case pn_DivMod_res_mod:
3400 if (get_DivMod_left(divmod) == b) {
3401 /* a % a = 0 if a != 0 */
3402 ir_mode *mode = get_irn_mode(proj);
3403 ir_node *res = new_Const(mode, get_mode_null(mode));
3405 DBG_OPT_CSTEVAL(divmod, res);
3411 } /* transform_node_Proj_DivMod */
3414 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3416 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3417 if (get_opt_unreachable_code()) {
3418 ir_node *n = get_Proj_pred(proj);
3419 ir_node *b = get_Cond_selector(n);
3421 if (mode_is_int(get_irn_mode(b))) {
3422 tarval *tb = value_of(b);
3424 if (tb != tarval_bad) {
3425 /* we have a constant switch */
3426 long num = get_Proj_proj(proj);
3428 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3429 if (get_tarval_long(tb) == num) {
3430 /* Do NOT create a jump here, or we will have 2 control flow ops
3431 * in a block. This case is optimized away in optimize_cf(). */
3434 /* this case will NEVER be taken, kill it */
3442 } /* transform_node_Proj_Cond */
3445 * Normalizes and optimizes Cmp nodes.
3447 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3448 ir_node *n = get_Proj_pred(proj);
3449 ir_node *left = get_Cmp_left(n);
3450 ir_node *right = get_Cmp_right(n);
3454 ir_mode *mode = NULL;
3455 long proj_nr = get_Proj_proj(proj);
3457 /* we can evaluate this direct */
3460 return new_Const(mode_b, get_tarval_b_false());
3462 return new_Const(mode_b, get_tarval_b_true());
3464 if(!mode_is_float(get_irn_mode(left)))
3465 return new_Const(mode_b, get_tarval_b_true());
3471 /* Remove unnecessary conversions */
3472 /* TODO handle constants */
3473 if (is_Conv(left) && is_Conv(right)) {
3474 ir_mode *mode = get_irn_mode(left);
3475 ir_node *op_left = get_Conv_op(left);
3476 ir_node *op_right = get_Conv_op(right);
3477 ir_mode *mode_left = get_irn_mode(op_left);
3478 ir_mode *mode_right = get_irn_mode(op_right);
3480 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3481 ir_graph *irg = current_ir_graph;
3482 ir_node *block = get_nodes_block(n);
3484 if (mode_left == mode_right) {
3488 } else if (smaller_mode(mode_left, mode_right)) {
3489 left = new_r_Conv(irg, block, op_left, mode_right);
3492 } else if (smaller_mode(mode_right, mode_left)) {
3494 right = new_r_Conv(irg, block, op_right, mode_left);
3500 /* TODO extend to arbitrary constants */
3501 if (is_Conv(left) && is_Const(right) && is_Const_null(right)) {
3502 ir_mode* mode = get_irn_mode(left);
3503 ir_node* op = get_Conv_op(left);
3504 ir_mode* op_mode = get_irn_mode(op);
3506 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3507 (mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3508 ir_node *null = new_Const(op_mode, get_mode_null(op_mode));
3509 set_Cmp_left( n, op);
3510 set_Cmp_right(n, null);
3517 left = get_Cast_op(left);
3519 right = get_Cast_op(right);
3521 /* remove operation of both sides if possible */
3522 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3523 ir_opcode lop = get_irn_opcode(left);
3525 if (lop == get_irn_opcode(right)) {
3526 ir_node *ll, *lr, *rl, *rr;
3528 /* same operation on both sides, try to remove */
3532 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3533 left = get_unop_op(left);
3534 right = get_unop_op(right);
3538 ll = get_Add_left(left);
3539 lr = get_Add_right(left);
3540 rl = get_Add_left(right);
3541 rr = get_Add_right(right);
3544 /* X + a CMP X + b ==> a CMP b */
3548 } else if (ll == rr) {
3549 /* X + a CMP b + X ==> a CMP b */
3553 } else if (lr == rl) {
3554 /* a + X CMP X + b ==> a CMP b */
3558 } else if (lr == rr) {
3559 /* a + X CMP b + X ==> a CMP b */
3566 ll = get_Sub_left(left);
3567 lr = get_Sub_right(left);
3568 rl = get_Sub_left(right);
3569 rr = get_Sub_right(right);
3572 /* X - a CMP X - b ==> a CMP b */
3576 } else if (lr == rr) {
3577 /* a - X CMP b - X ==> a CMP b */
3584 if (get_Rot_right(left) == get_Rot_right(right)) {
3585 /* a ROT X CMP b ROT X */
3586 left = get_Rot_left(left);
3587 right = get_Rot_left(right);
3597 if (get_irn_mode(left) == mode_b) {
3598 ir_graph *irg = current_ir_graph;
3599 ir_node *block = get_nodes_block(n);
3602 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3603 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3604 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3605 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3606 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3607 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3611 if (!get_opt_reassociation())
3615 * First step: normalize the compare op
3616 * by placing the constant on the right side
3617 * or moving the lower address node to the left.
3618 * We ignore the case that both are constants
3619 * this case should be optimized away.
3621 if (is_Const(right)) {
3623 } else if (is_Const(left)) {
3628 proj_nr = get_inversed_pnc(proj_nr);
3630 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3636 proj_nr = get_inversed_pnc(proj_nr);
3641 * Second step: Try to reduce the magnitude
3642 * of a constant. This may help to generate better code
3643 * later and may help to normalize more compares.
3644 * Of course this is only possible for integer values.
3647 mode = get_irn_mode(c);
3648 tv = get_Const_tarval(c);
3650 if (tv != tarval_bad) {
3651 /* the following optimization is possible on modes without Overflow
3652 * on Unary Minus or on == and !=:
3653 * -a CMP c ==> a swap(CMP) -c
3655 * Beware: for two-complement Overflow may occur, so only == and != can
3656 * be optimized, see this:
3657 * -MININT < 0 =/=> MININT > 0 !!!
3659 if (is_Minus(left) &&
3660 (!mode_overflow_on_unary_Minus(mode) ||
3661 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3662 tv = tarval_neg(tv);
3664 if (tv != tarval_bad) {
3665 left = get_Minus_op(left);
3666 proj_nr = get_inversed_pnc(proj_nr);
3669 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3670 tv = tarval_not(tv);
3672 if (tv != tarval_bad) {
3673 left = get_Not_op(left);
3678 /* for integer modes, we have more */
3679 if (mode_is_int(mode)) {
3680 /* Ne includes Unordered which is not possible on integers.
3681 * However, frontends often use this wrong, so fix it here */
3682 if (proj_nr & pn_Cmp_Uo) {
3683 proj_nr &= ~pn_Cmp_Uo;
3684 set_Proj_proj(proj, proj_nr);
3687 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3688 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3689 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3690 tv = tarval_sub(tv, get_mode_one(mode));
3692 if (tv != tarval_bad) {
3693 proj_nr ^= pn_Cmp_Eq;
3697 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3698 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3699 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3700 tv = tarval_add(tv, get_mode_one(mode));
3702 if (tv != tarval_bad) {
3703 proj_nr ^= pn_Cmp_Eq;
3708 /* the following reassociations work only for == and != */
3709 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3711 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3712 if (tarval_is_null(tv) && is_Sub(left)) {
3713 right = get_Sub_right(left);
3714 left = get_Sub_left(left);
3716 tv = value_of(right);
3720 if (tv != tarval_bad) {
3721 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3723 ir_node *c1 = get_Sub_right(left);
3724 tarval *tv2 = value_of(c1);
3726 if (tv2 != tarval_bad) {
3727 tv2 = tarval_add(tv, value_of(c1));
3729 if (tv2 != tarval_bad) {
3730 left = get_Sub_left(left);
3736 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3737 else if (is_Add(left)) {
3738 ir_node *a_l = get_Add_left(left);
3739 ir_node *a_r = get_Add_right(left);
3743 if (is_Const(a_l)) {
3745 tv2 = value_of(a_l);
3748 tv2 = value_of(a_r);
3751 if (tv2 != tarval_bad) {
3752 tv2 = tarval_sub(tv, tv2);
3754 if (tv2 != tarval_bad) {
3761 /* -a == c ==> a == -c, -a != c ==> a != -c */
3762 else if (is_Minus(left)) {
3763 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3765 if (tv2 != tarval_bad) {
3766 left = get_Minus_op(left);
3773 /* the following reassociations work only for <= */
3774 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3775 if (tv != tarval_bad) {
3776 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3777 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3784 * optimization for AND:
3786 * And(x, C) == C ==> And(x, C) != 0
3787 * And(x, C) != C ==> And(X, C) == 0
3789 * if C is a single Bit constant.
3791 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3792 if (tarval_is_single_bit(tv)) {
3793 /* check for Constant's match. We have check hare the tarvals,
3794 because our const might be changed */
3795 ir_node *la = get_And_left(left);
3796 ir_node *ra = get_And_right(left);
3797 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3798 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3799 /* fine: do the transformation */
3800 tv = get_mode_null(get_tarval_mode(tv));
3801 proj_nr ^= pn_Cmp_Leg;
3806 } /* tarval != bad */
3809 if (changed & 2) /* need a new Const */
3810 right = new_Const(mode, tv);
3812 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
3813 ir_node *op = get_Proj_pred(left);
3815 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
3816 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
3817 ir_node *c = get_binop_right(op);
3820 tarval *tv = get_Const_tarval(c);
3822 if (tarval_is_single_bit(tv)) {
3823 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
3824 ir_node *v = get_binop_left(op);
3825 ir_node *blk = get_irn_n(op, -1);
3826 ir_mode *mode = get_irn_mode(v);
3828 tv = tarval_sub(tv, get_mode_one(mode));
3829 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
3837 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3839 /* create a new compare */
3840 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3842 set_Proj_pred(proj, n);
3843 set_Proj_proj(proj, proj_nr);
3847 } /* transform_node_Proj_Cmp */
3850 * Does all optimizations on nodes that must be done on it's Proj's
3851 * because of creating new nodes.
3853 static ir_node *transform_node_Proj(ir_node *proj) {
3854 ir_node *n = get_Proj_pred(proj);
3856 switch (get_irn_opcode(n)) {
3858 return transform_node_Proj_Div(proj);
3861 return transform_node_Proj_Mod(proj);
3864 return transform_node_Proj_DivMod(proj);
3867 return transform_node_Proj_Cond(proj);
3870 return transform_node_Proj_Cmp(proj);
3873 /* should not happen, but if it does will be optimized away */
3874 return equivalent_node_Proj(proj);
3880 } /* transform_node_Proj */
3883 * Move Confirms down through Phi nodes.
3885 static ir_node *transform_node_Phi(ir_node *phi) {
3887 ir_mode *mode = get_irn_mode(phi);
3889 if (mode_is_reference(mode)) {
3890 n = get_irn_arity(phi);
3892 /* Beware of Phi0 */
3894 ir_node *pred = get_irn_n(phi, 0);
3895 ir_node *bound, *new_Phi, *block, **in;
3898 if (! is_Confirm(pred))
3901 bound = get_Confirm_bound(pred);
3902 pnc = get_Confirm_cmp(pred);
3904 NEW_ARR_A(ir_node *, in, n);
3905 in[0] = get_Confirm_value(pred);
3907 for (i = 1; i < n; ++i) {
3908 pred = get_irn_n(phi, i);
3910 if (! is_Confirm(pred) ||
3911 get_Confirm_bound(pred) != bound ||
3912 get_Confirm_cmp(pred) != pnc)
3914 in[i] = get_Confirm_value(pred);
3916 /* move the Confirm nodes "behind" the Phi */
3917 block = get_irn_n(phi, -1);
3918 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3919 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3923 } /* transform_node_Phi */
3926 * Returns the operands of a commutative bin-op, if one operand is
3927 * a const, it is returned as the second one.
3929 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3930 ir_node *op_a = get_binop_left(binop);
3931 ir_node *op_b = get_binop_right(binop);
3933 assert(is_op_commutative(get_irn_op(binop)));
3935 if (is_Const(op_a)) {
3942 } /* get_comm_Binop_Ops */
3945 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3946 * Such pattern may arise in bitfield stores.
3948 * value c4 value c4 & c2
3949 * AND c3 AND c1 | c3
3956 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3959 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3962 ir_node *and_l, *c3;
3963 ir_node *value, *c4;
3964 ir_node *new_and, *new_const, *block;
3965 ir_mode *mode = get_irn_mode(or);
3967 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3970 get_comm_Binop_Ops(or, &and, &c1);
3971 if (!is_Const(c1) || !is_And(and))
3974 get_comm_Binop_Ops(and, &or_l, &c2);
3978 tv1 = get_Const_tarval(c1);
3979 tv2 = get_Const_tarval(c2);
3981 tv = tarval_or(tv1, tv2);
3982 if (tarval_is_all_one(tv)) {
3983 /* the AND does NOT clear a bit with isn't set by the OR */
3984 set_Or_left(or, or_l);
3985 set_Or_right(or, c1);
3987 /* check for more */
3994 get_comm_Binop_Ops(or_l, &and_l, &c3);
3995 if (!is_Const(c3) || !is_And(and_l))
3998 get_comm_Binop_Ops(and_l, &value, &c4);
4002 /* ok, found the pattern, check for conditions */
4003 assert(mode == get_irn_mode(and));
4004 assert(mode == get_irn_mode(or_l));
4005 assert(mode == get_irn_mode(and_l));
4007 tv3 = get_Const_tarval(c3);
4008 tv4 = get_Const_tarval(c4);
4010 tv = tarval_or(tv4, tv2);
4011 if (!tarval_is_all_one(tv)) {
4012 /* have at least one 0 at the same bit position */
4016 n_tv4 = tarval_not(tv4);
4017 if (tv3 != tarval_and(tv3, n_tv4)) {
4018 /* bit in the or_mask is outside the and_mask */
4022 n_tv2 = tarval_not(tv2);
4023 if (tv1 != tarval_and(tv1, n_tv2)) {
4024 /* bit in the or_mask is outside the and_mask */
4028 /* ok, all conditions met */
4029 block = get_irn_n(or, -1);
4031 new_and = new_r_And(current_ir_graph, block,
4032 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4034 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4036 set_Or_left(or, new_and);
4037 set_Or_right(or, new_const);
4039 /* check for more */
4041 } /* transform_node_Or_bf_store */
4044 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4046 static ir_node *transform_node_Or_Rot(ir_node *or) {
4047 ir_mode *mode = get_irn_mode(or);
4048 ir_node *shl, *shr, *block;
4049 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4052 if (! mode_is_int(mode))
4055 shl = get_binop_left(or);
4056 shr = get_binop_right(or);
4065 } else if (!is_Shl(shl)) {
4067 } else if (!is_Shr(shr)) {
4070 x = get_Shl_left(shl);
4071 if (x != get_Shr_left(shr))
4074 c1 = get_Shl_right(shl);
4075 c2 = get_Shr_right(shr);
4076 if (is_Const(c1) && is_Const(c2)) {
4077 tv1 = get_Const_tarval(c1);
4078 if (! tarval_is_long(tv1))
4081 tv2 = get_Const_tarval(c2);
4082 if (! tarval_is_long(tv2))
4085 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4086 != get_mode_size_bits(mode))
4089 /* yet, condition met */
4090 block = get_irn_n(or, -1);
4092 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4094 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4096 } else if (is_Sub(c1)) {
4100 if (get_Sub_right(sub) != v)
4103 c1 = get_Sub_left(sub);
4107 tv1 = get_Const_tarval(c1);
4108 if (! tarval_is_long(tv1))
4111 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4114 /* yet, condition met */
4115 block = get_nodes_block(or);
4117 /* a Rot right is not supported, so use a rot left */
4118 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4120 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4122 } else if (is_Sub(c2)) {
4126 c1 = get_Sub_left(sub);
4130 tv1 = get_Const_tarval(c1);
4131 if (! tarval_is_long(tv1))
4134 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4137 /* yet, condition met */
4138 block = get_irn_n(or, -1);
4141 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4143 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4148 } /* transform_node_Or_Rot */
4153 static ir_node *transform_node_Or(ir_node *n) {
4154 ir_node *c, *oldn = n;
4155 ir_node *a = get_Or_left(n);
4156 ir_node *b = get_Or_right(n);
4158 if (is_Not(a) && is_Not(b)) {
4159 /* ~a | ~b = ~(a&b) */
4160 ir_node *block = get_nodes_block(n);
4161 ir_mode *mode = get_irn_mode(n);
4165 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4166 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4167 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4171 /* we can evaluate 2 Projs of the same Cmp */
4172 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4173 ir_node *pred_a = get_Proj_pred(a);
4174 ir_node *pred_b = get_Proj_pred(b);
4175 if (pred_a == pred_b) {
4176 dbg_info *dbgi = get_irn_dbg_info(n);
4177 ir_node *block = get_nodes_block(pred_a);
4178 pn_Cmp pn_a = get_Proj_proj(a);
4179 pn_Cmp pn_b = get_Proj_proj(b);
4180 /* yes, we can simply calculate with pncs */
4181 pn_Cmp new_pnc = pn_a | pn_b;
4183 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4188 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4190 n = transform_node_Or_bf_store(n);
4191 n = transform_node_Or_Rot(n);
4195 n = transform_bitwise_distributive(n, transform_node_Or);
4198 } /* transform_node_Or */
4202 static ir_node *transform_node(ir_node *n);
4205 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4207 * Should be moved to reassociation?
4209 static ir_node *transform_node_shift(ir_node *n) {
4210 ir_node *left, *right;
4211 tarval *tv1, *tv2, *res;
4213 int modulo_shf, flag;
4215 left = get_binop_left(n);
4217 /* different operations */
4218 if (get_irn_op(left) != get_irn_op(n))
4221 right = get_binop_right(n);
4222 tv1 = value_of(right);
4223 if (tv1 == tarval_bad)
4226 tv2 = value_of(get_binop_right(left));
4227 if (tv2 == tarval_bad)
4230 res = tarval_add(tv1, tv2);
4232 /* beware: a simple replacement works only, if res < modulo shift */
4233 mode = get_irn_mode(n);
4237 modulo_shf = get_mode_modulo_shift(mode);
4238 if (modulo_shf > 0) {
4239 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4241 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4247 /* ok, we can replace it */
4248 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4250 in[0] = get_binop_left(left);
4251 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4253 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4255 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4257 return transform_node(irn);
4260 } /* transform_node_shift */
4265 static ir_node *transform_node_Shr(ir_node *n) {
4266 ir_node *c, *oldn = n;
4267 ir_node *a = get_Shr_left(n);
4268 ir_node *b = get_Shr_right(n);
4270 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4271 return transform_node_shift(n);
4272 } /* transform_node_Shr */
4277 static ir_node *transform_node_Shrs(ir_node *n) {
4278 ir_node *c, *oldn = n;
4279 ir_node *a = get_Shrs_left(n);
4280 ir_node *b = get_Shrs_right(n);
4282 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4283 return transform_node_shift(n);
4284 } /* transform_node_Shrs */
4289 static ir_node *transform_node_Shl(ir_node *n) {
4290 ir_node *c, *oldn = n;
4291 ir_node *a = get_Shl_left(n);
4292 ir_node *b = get_Shl_right(n);
4294 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4295 return transform_node_shift(n);
4296 } /* transform_node_Shl */
4299 * Remove dead blocks and nodes in dead blocks
4300 * in keep alive list. We do not generate a new End node.
4302 static ir_node *transform_node_End(ir_node *n) {
4303 int i, j, n_keepalives = get_End_n_keepalives(n);
4306 NEW_ARR_A(ir_node *, in, n_keepalives);
4308 for (i = j = 0; i < n_keepalives; ++i) {
4309 ir_node *ka = get_End_keepalive(n, i);
4311 if (! is_Block_dead(ka)) {
4315 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4318 /* FIXME: beabi need to keep a Proj(M) */
4319 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4322 if (j != n_keepalives)
4323 set_End_keepalives(n, j, in);
4325 } /* transform_node_End */
4327 /** returns 1 if a == -b */
4328 static int is_negated_value(ir_node *a, ir_node *b) {
4329 if(is_Minus(a) && get_Minus_op(a) == b)
4331 if(is_Minus(b) && get_Minus_op(b) == a)
4333 if(is_Sub(a) && is_Sub(b)) {
4334 ir_node *a_left = get_Sub_left(a);
4335 ir_node *a_right = get_Sub_right(a);
4336 ir_node *b_left = get_Sub_left(b);
4337 ir_node *b_right = get_Sub_right(b);
4339 if(a_left == b_right && a_right == b_left)
4347 * Optimize a Mux into some simpler cases.
4349 static ir_node *transform_node_Mux(ir_node *n) {
4350 ir_node *oldn = n, *sel = get_Mux_sel(n);
4351 ir_mode *mode = get_irn_mode(n);
4353 if (mode == mode_b) {
4354 ir_node *t = get_Mux_true(n);
4355 ir_node *f = get_Mux_false(n);
4356 dbg_info *dbg = get_irn_dbg_info(n);
4357 ir_node *block = get_irn_n(n, -1);
4358 ir_graph *irg = current_ir_graph;
4361 tarval *tv_t = get_Const_tarval(t);
4362 if (tv_t == tarval_b_true) {
4364 assert(get_Const_tarval(f) == tarval_b_false);
4367 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4370 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4371 assert(tv_t == tarval_b_false);
4373 assert(get_Const_tarval(f) == tarval_b_true);
4376 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4379 } else if (is_Const(f)) {
4380 tarval *tv_f = get_Const_tarval(f);
4381 if (tv_f == tarval_b_true) {
4382 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4383 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4385 assert(tv_f == tarval_b_false);
4386 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4391 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4392 ir_node *cmp = get_Proj_pred(sel);
4393 long pn = get_Proj_proj(sel);
4394 ir_node *f = get_Mux_false(n);
4395 ir_node *t = get_Mux_true(n);
4398 * Note: normalization puts the constant on the right side,
4399 * so we check only one case.
4401 * Note further that these optimization work even for floating point
4402 * with NaN's because -NaN == NaN.
4403 * However, if +0 and -0 is handled differently, we cannot use the first
4407 ir_node *cmp_r = get_Cmp_right(cmp);
4408 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4409 ir_node *block = get_irn_n(n, -1);
4411 if(is_negated_value(f, t)) {
4412 ir_node *cmp_left = get_Cmp_left(cmp);
4414 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4415 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4416 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4418 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4420 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4422 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4423 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4424 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4426 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4428 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4430 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4438 return arch_transform_node_Mux(n);
4439 } /* transform_node_Mux */
4442 * Optimize a Psi into some simpler cases.
4444 static ir_node *transform_node_Psi(ir_node *n) {
4446 return transform_node_Mux(n);
4449 } /* transform_node_Psi */
4452 * optimize sync nodes that have other syncs as input we simply add the inputs
4453 * of the other sync to our own inputs
4455 static ir_node *transform_node_Sync(ir_node *n) {
4458 arity = get_irn_arity(n);
4459 for(i = 0; i < get_irn_arity(n); /*empty*/) {
4461 ir_node *in = get_irn_n(n, i);
4467 /* set sync input 0 instead of the sync */
4468 set_irn_n(n, i, get_irn_n(in, 0));
4469 /* so we check this input again for syncs */
4471 /* append all other inputs of the sync to our sync */
4472 arity2 = get_irn_arity(in);
4473 for(i2 = 1; i2 < arity2; ++i2) {
4474 ir_node *in_in = get_irn_n(in, i2);
4475 add_irn_n(n, in_in);
4476 /* increase arity so we also check the new inputs for syncs */
4481 /* rehash the sync node */
4482 add_identities(current_ir_graph->value_table, n);
4488 * Tries several [inplace] [optimizing] transformations and returns an
4489 * equivalent node. The difference to equivalent_node() is that these
4490 * transformations _do_ generate new nodes, and thus the old node must
4491 * not be freed even if the equivalent node isn't the old one.
4493 static ir_node *transform_node(ir_node *n) {
4497 * Transform_node is the only "optimizing transformation" that might
4498 * return a node with a different opcode. We iterate HERE until fixpoint
4499 * to get the final result.
4503 if (n->op->ops.transform_node)
4504 n = n->op->ops.transform_node(n);
4505 } while (oldn != n);
4508 } /* transform_node */
4511 * Sets the default transform node operation for an ir_op_ops.
4513 * @param code the opcode for the default operation
4514 * @param ops the operations initialized
4519 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4523 ops->transform_node = transform_node_##a; \
4558 } /* firm_set_default_transform_node */
4561 /* **************** Common Subexpression Elimination **************** */
4563 /** The size of the hash table used, should estimate the number of nodes
4565 #define N_IR_NODES 512
4567 /** Compares the attributes of two Const nodes. */
4568 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4569 return (get_Const_tarval(a) != get_Const_tarval(b))
4570 || (get_Const_type(a) != get_Const_type(b));
4571 } /* node_cmp_attr_Const */
4573 /** Compares the attributes of two Proj nodes. */
4574 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4575 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4576 } /* node_cmp_attr_Proj */
4578 /** Compares the attributes of two Filter nodes. */
4579 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4580 return get_Filter_proj(a) != get_Filter_proj(b);
4581 } /* node_cmp_attr_Filter */
4583 /** Compares the attributes of two Alloc nodes. */
4584 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4585 const alloc_attr *pa = get_irn_alloc_attr(a);
4586 const alloc_attr *pb = get_irn_alloc_attr(b);
4587 return (pa->where != pb->where) || (pa->type != pb->type);
4588 } /* node_cmp_attr_Alloc */
4590 /** Compares the attributes of two Free nodes. */
4591 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4592 const free_attr *pa = get_irn_free_attr(a);
4593 const free_attr *pb = get_irn_free_attr(b);
4594 return (pa->where != pb->where) || (pa->type != pb->type);
4595 } /* node_cmp_attr_Free */
4597 /** Compares the attributes of two SymConst nodes. */
4598 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4599 const symconst_attr *pa = get_irn_symconst_attr(a);
4600 const symconst_attr *pb = get_irn_symconst_attr(b);
4601 return (pa->num != pb->num)
4602 || (pa->sym.type_p != pb->sym.type_p)
4603 || (pa->tp != pb->tp);
4604 } /* node_cmp_attr_SymConst */
4606 /** Compares the attributes of two Call nodes. */
4607 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4608 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4609 } /* node_cmp_attr_Call */
4611 /** Compares the attributes of two Sel nodes. */
4612 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4613 const ir_entity *a_ent = get_Sel_entity(a);
4614 const ir_entity *b_ent = get_Sel_entity(b);
4616 (a_ent->kind != b_ent->kind) ||
4617 (a_ent->name != b_ent->name) ||
4618 (a_ent->owner != b_ent->owner) ||
4619 (a_ent->ld_name != b_ent->ld_name) ||
4620 (a_ent->type != b_ent->type);
4621 } /* node_cmp_attr_Sel */
4623 /** Compares the attributes of two Phi nodes. */
4624 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4625 /* we can only enter this function if both nodes have the same number of inputs,
4626 hence it is enough to check if one of them is a Phi0 */
4628 /* check the Phi0 attribute */
4629 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4632 } /* node_cmp_attr_Phi */
4634 /** Compares the attributes of two Conv nodes. */
4635 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4636 return get_Conv_strict(a) != get_Conv_strict(b);
4637 } /* node_cmp_attr_Conv */
4639 /** Compares the attributes of two Cast nodes. */
4640 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4641 return get_Cast_type(a) != get_Cast_type(b);
4642 } /* node_cmp_attr_Cast */
4644 /** Compares the attributes of two Load nodes. */
4645 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4646 if (get_Load_volatility(a) == volatility_is_volatile ||
4647 get_Load_volatility(b) == volatility_is_volatile)
4648 /* NEVER do CSE on volatile Loads */
4650 /* do not CSE Loads with different alignment. Be conservative. */
4651 if (get_Load_align(a) != get_Load_align(b))
4654 return get_Load_mode(a) != get_Load_mode(b);
4655 } /* node_cmp_attr_Load */
4657 /** Compares the attributes of two Store nodes. */
4658 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4659 /* do not CSE Stores with different alignment. Be conservative. */
4660 if (get_Store_align(a) != get_Store_align(b))
4663 /* NEVER do CSE on volatile Stores */
4664 return (get_Store_volatility(a) == volatility_is_volatile ||
4665 get_Store_volatility(b) == volatility_is_volatile);
4666 } /* node_cmp_attr_Store */
4668 /** Compares the attributes of two Confirm nodes. */
4669 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4670 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4671 } /* node_cmp_attr_Confirm */
4673 /** Compares the attributes of two ASM nodes. */
4674 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4676 const ir_asm_constraint *ca;
4677 const ir_asm_constraint *cb;
4680 if (get_ASM_text(a) != get_ASM_text(b))
4683 /* Should we really check the constraints here? Should be better, but is strange. */
4684 n = get_ASM_n_input_constraints(a);
4685 if (n != get_ASM_n_input_constraints(b))
4688 ca = get_ASM_input_constraints(a);
4689 cb = get_ASM_input_constraints(b);
4690 for (i = 0; i < n; ++i) {
4691 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4695 n = get_ASM_n_output_constraints(a);
4696 if (n != get_ASM_n_output_constraints(b))
4699 ca = get_ASM_output_constraints(a);
4700 cb = get_ASM_output_constraints(b);
4701 for (i = 0; i < n; ++i) {
4702 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4706 n = get_ASM_n_clobbers(a);
4707 if (n != get_ASM_n_clobbers(b))
4710 cla = get_ASM_clobbers(a);
4711 clb = get_ASM_clobbers(b);
4712 for (i = 0; i < n; ++i) {
4713 if (cla[i] != clb[i])
4717 } /* node_cmp_attr_ASM */
4720 * Set the default node attribute compare operation for an ir_op_ops.
4722 * @param code the opcode for the default operation
4723 * @param ops the operations initialized
4728 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4732 ops->node_cmp_attr = node_cmp_attr_##a; \
4757 } /* firm_set_default_node_cmp_attr */
4760 * Compare function for two nodes in the hash table. Gets two
4761 * nodes as parameters. Returns 0 if the nodes are a cse.
4763 int identities_cmp(const void *elt, const void *key) {
4770 if (a == b) return 0;
4772 if ((get_irn_op(a) != get_irn_op(b)) ||
4773 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4775 /* compare if a's in and b's in are of equal length */
4776 irn_arity_a = get_irn_intra_arity (a);
4777 if (irn_arity_a != get_irn_intra_arity(b))
4780 /* for block-local cse and op_pin_state_pinned nodes: */
4781 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4782 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4786 /* compare a->in[0..ins] with b->in[0..ins] */
4787 for (i = 0; i < irn_arity_a; i++)
4788 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4792 * here, we already now that the nodes are identical except their
4795 if (a->op->ops.node_cmp_attr)
4796 return a->op->ops.node_cmp_attr(a, b);
4799 } /* identities_cmp */
4802 * Calculate a hash value of a node.
4804 unsigned ir_node_hash(ir_node *node) {
4808 if (node->op == op_Const) {
4809 /* special value for const, as they only differ in their tarval. */
4810 h = HASH_PTR(node->attr.con.tv);
4811 h = 9*h + HASH_PTR(get_irn_mode(node));
4812 } else if (node->op == op_SymConst) {
4813 /* special value for const, as they only differ in their symbol. */
4814 h = HASH_PTR(node->attr.symc.sym.type_p);
4815 h = 9*h + HASH_PTR(get_irn_mode(node));
4818 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4819 h = irn_arity = get_irn_intra_arity(node);
4821 /* consider all in nodes... except the block if not a control flow. */
4822 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4823 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4827 h = 9*h + HASH_PTR(get_irn_mode(node));
4829 h = 9*h + HASH_PTR(get_irn_op(node));
4833 } /* ir_node_hash */
4835 pset *new_identities(void) {
4836 return new_pset(identities_cmp, N_IR_NODES);
4837 } /* new_identities */
4839 void del_identities(pset *value_table) {
4840 del_pset(value_table);
4841 } /* del_identities */
4844 * Normalize a node by putting constants (and operands with smaller
4845 * node index) on the right
4847 * @param n The node to normalize
4849 static void normalize_node(ir_node *n) {
4850 if (get_opt_reassociation()) {
4851 if (is_op_commutative(get_irn_op(n))) {
4852 ir_node *l = get_binop_left(n);
4853 ir_node *r = get_binop_right(n);
4854 int l_idx = get_irn_idx(l);
4855 int r_idx = get_irn_idx(r);
4857 /* For commutative operators perform a OP b == b OP a but keep
4858 constants on the RIGHT side. This helps greatly in some optimizations.
4859 Moreover we use the idx number to make the form deterministic. */
4860 if (is_irn_constlike(l))
4862 if (is_irn_constlike(r))
4864 if (l_idx < r_idx) {
4865 set_binop_left(n, r);
4866 set_binop_right(n, l);
4870 } /* normalize_node */
4873 * Return the canonical node computing the same value as n.
4875 * @param value_table The value table
4876 * @param n The node to lookup
4878 * Looks up the node in a hash table.
4880 * For Const nodes this is performed in the constructor, too. Const
4881 * nodes are extremely time critical because of their frequent use in
4882 * constant string arrays.
4884 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4887 if (!value_table) return n;
4891 o = pset_find(value_table, n, ir_node_hash(n));
4900 * During construction we set the op_pin_state_pinned flag in the graph right when the
4901 * optimization is performed. The flag turning on procedure global cse could
4902 * be changed between two allocations. This way we are safe.
4904 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4907 n = identify(value_table, n);
4908 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4909 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4911 } /* identify_cons */
4914 * Return the canonical node computing the same value as n.
4915 * Looks up the node in a hash table, enters it in the table
4916 * if it isn't there yet.
4918 ir_node *identify_remember(pset *value_table, ir_node *n) {
4921 if (!value_table) return n;
4924 /* lookup or insert in hash table with given hash key. */
4925 o = pset_insert(value_table, n, ir_node_hash(n));
4932 } /* identify_remember */
4934 /* Add a node to the identities value table. */
4935 void add_identities(pset *value_table, ir_node *node) {
4936 if (get_opt_cse() && is_no_Block(node))
4937 identify_remember(value_table, node);
4938 } /* add_identities */
4940 /* Visit each node in the value table of a graph. */
4941 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4943 ir_graph *rem = current_ir_graph;
4945 current_ir_graph = irg;
4946 foreach_pset(irg->value_table, node)
4948 current_ir_graph = rem;
4949 } /* visit_all_identities */
4952 * Garbage in, garbage out. If a node has a dead input, i.e., the
4953 * Bad node is input to the node, return the Bad node.
4955 static ir_node *gigo(ir_node *node) {
4957 ir_op *op = get_irn_op(node);
4959 /* remove garbage blocks by looking at control flow that leaves the block
4960 and replacing the control flow by Bad. */
4961 if (get_irn_mode(node) == mode_X) {
4962 ir_node *block = get_nodes_block(skip_Proj(node));
4964 /* Don't optimize nodes in immature blocks. */
4965 if (!get_Block_matured(block)) return node;
4966 /* Don't optimize End, may have Bads. */
4967 if (op == op_End) return node;
4969 if (is_Block(block)) {
4970 irn_arity = get_irn_arity(block);
4971 for (i = 0; i < irn_arity; i++) {
4972 if (!is_Bad(get_irn_n(block, i)))
4975 if (i == irn_arity) {
4976 ir_graph *irg = get_irn_irg(block);
4977 /* the start block is never dead */
4978 if (block != get_irg_start_block(irg)
4979 && block != get_irg_end_block(irg))
4985 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4986 blocks predecessors is dead. */
4987 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4988 irn_arity = get_irn_arity(node);
4991 * Beware: we can only read the block of a non-floating node.
4993 if (is_irn_pinned_in_irg(node) &&
4994 is_Block_dead(get_nodes_block(node)))
4997 for (i = 0; i < irn_arity; i++) {
4998 ir_node *pred = get_irn_n(node, i);
5003 /* Propagating Unknowns here seems to be a bad idea, because
5004 sometimes we need a node as a input and did not want that
5006 However, it might be useful to move this into a later phase
5007 (if you think that optimizing such code is useful). */
5008 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5009 return new_Unknown(get_irn_mode(node));
5014 /* With this code we violate the agreement that local_optimize
5015 only leaves Bads in Block, Phi and Tuple nodes. */
5016 /* If Block has only Bads as predecessors it's garbage. */
5017 /* If Phi has only Bads as predecessors it's garbage. */
5018 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5019 irn_arity = get_irn_arity(node);
5020 for (i = 0; i < irn_arity; i++) {
5021 if (!is_Bad(get_irn_n(node, i))) break;
5023 if (i == irn_arity) node = new_Bad();
5030 * These optimizations deallocate nodes from the obstack.
5031 * It can only be called if it is guaranteed that no other nodes
5032 * reference this one, i.e., right after construction of a node.
5034 * @param n The node to optimize
5036 * current_ir_graph must be set to the graph of the node!
5038 ir_node *optimize_node(ir_node *n) {
5041 ir_opcode iro = get_irn_opcode(n);
5043 /* Always optimize Phi nodes: part of the construction. */
5044 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5046 /* constant expression evaluation / constant folding */
5047 if (get_opt_constant_folding()) {
5048 /* neither constants nor Tuple values can be evaluated */
5049 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5050 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5051 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5052 /* try to evaluate */
5053 tv = computed_value(n);
5054 if (tv != tarval_bad) {
5056 ir_type *old_tp = get_irn_type(n);
5057 int i, arity = get_irn_arity(n);
5061 * Try to recover the type of the new expression.
5063 for (i = 0; i < arity && !old_tp; ++i)
5064 old_tp = get_irn_type(get_irn_n(n, i));
5067 * we MUST copy the node here temporary, because it's still needed
5068 * for DBG_OPT_CSTEVAL
5070 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5071 oldn = alloca(node_size);
5073 memcpy(oldn, n, node_size);
5074 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5076 /* ARG, copy the in array, we need it for statistics */
5077 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5079 /* note the inplace edges module */
5080 edges_node_deleted(n, current_ir_graph);
5082 /* evaluation was successful -- replace the node. */
5083 irg_kill_node(current_ir_graph, n);
5084 nw = new_Const(get_tarval_mode(tv), tv);
5086 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5087 set_Const_type(nw, old_tp);
5088 DBG_OPT_CSTEVAL(oldn, nw);
5089 tarval_enable_fp_ops(old_fp_mode);
5092 tarval_enable_fp_ops(old_fp_mode);
5096 /* remove unnecessary nodes */
5097 if (get_opt_constant_folding() ||
5098 (iro == iro_Phi) || /* always optimize these nodes. */
5100 (iro == iro_Proj) ||
5101 (iro == iro_Block) ) /* Flags tested local. */
5102 n = equivalent_node(n);
5104 /* Common Subexpression Elimination.
5106 * Checks whether n is already available.
5107 * The block input is used to distinguish different subexpressions. Right
5108 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5109 * subexpressions within a block.
5112 n = identify_cons(current_ir_graph->value_table, n);
5115 edges_node_deleted(oldn, current_ir_graph);
5117 /* We found an existing, better node, so we can deallocate the old node. */
5118 irg_kill_node(current_ir_graph, oldn);
5122 /* Some more constant expression evaluation that does not allow to
5124 iro = get_irn_opcode(n);
5125 if (get_opt_constant_folding() ||
5126 (iro == iro_Cond) ||
5127 (iro == iro_Proj)) /* Flags tested local. */
5128 n = transform_node(n);
5130 /* Remove nodes with dead (Bad) input.
5131 Run always for transformation induced Bads. */
5134 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5135 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5136 n = identify_remember(current_ir_graph->value_table, n);
5140 } /* optimize_node */
5144 * These optimizations never deallocate nodes (in place). This can cause dead
5145 * nodes lying on the obstack. Remove these by a dead node elimination,
5146 * i.e., a copying garbage collection.
5148 ir_node *optimize_in_place_2(ir_node *n) {
5151 ir_opcode iro = get_irn_opcode(n);
5153 if (!get_opt_optimize() && !is_Phi(n)) return n;
5155 /* constant expression evaluation / constant folding */
5156 if (get_opt_constant_folding()) {
5157 /* neither constants nor Tuple values can be evaluated */
5158 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5159 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5160 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5161 /* try to evaluate */
5162 tv = computed_value(n);
5163 if (tv != tarval_bad) {
5164 /* evaluation was successful -- replace the node. */
5165 ir_type *old_tp = get_irn_type(n);
5166 int i, arity = get_irn_arity(n);
5169 * Try to recover the type of the new expression.
5171 for (i = 0; i < arity && !old_tp; ++i)
5172 old_tp = get_irn_type(get_irn_n(n, i));
5174 n = new_Const(get_tarval_mode(tv), tv);
5176 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5177 set_Const_type(n, old_tp);
5179 DBG_OPT_CSTEVAL(oldn, n);
5180 tarval_enable_fp_ops(old_fp_mode);
5183 tarval_enable_fp_ops(old_fp_mode);
5187 /* remove unnecessary nodes */
5188 if (get_opt_constant_folding() ||
5189 (iro == iro_Phi) || /* always optimize these nodes. */
5190 (iro == iro_Id) || /* ... */
5191 (iro == iro_Proj) || /* ... */
5192 (iro == iro_Block) ) /* Flags tested local. */
5193 n = equivalent_node(n);
5195 /** common subexpression elimination **/
5196 /* Checks whether n is already available. */
5197 /* The block input is used to distinguish different subexpressions. Right
5198 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5199 subexpressions within a block. */
5200 if (get_opt_cse()) {
5201 n = identify(current_ir_graph->value_table, n);
5204 /* Some more constant expression evaluation. */
5205 iro = get_irn_opcode(n);
5206 if (get_opt_constant_folding() ||
5207 (iro == iro_Cond) ||
5208 (iro == iro_Proj)) /* Flags tested local. */
5209 n = transform_node(n);
5211 /* Remove nodes with dead (Bad) input.
5212 Run always for transformation induced Bads. */
5215 /* Now we can verify the node, as it has no dead inputs any more. */
5218 /* Now we have a legal, useful node. Enter it in hash table for cse.
5219 Blocks should be unique anyways. (Except the successor of start:
5220 is cse with the start block!) */
5221 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5222 n = identify_remember(current_ir_graph->value_table, n);
5225 } /* optimize_in_place_2 */
5228 * Wrapper for external use, set proper status bits after optimization.
5230 ir_node *optimize_in_place(ir_node *n) {
5231 /* Handle graph state */
5232 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5234 if (get_opt_global_cse())
5235 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5236 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5237 set_irg_outs_inconsistent(current_ir_graph);
5239 /* FIXME: Maybe we could also test whether optimizing the node can
5240 change the control graph. */
5241 set_irg_doms_inconsistent(current_ir_graph);
5242 return optimize_in_place_2(n);
5243 } /* optimize_in_place */
5246 * Sets the default operation for an ir_ops.
5248 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5249 ops = firm_set_default_computed_value(code, ops);
5250 ops = firm_set_default_equivalent_node(code, ops);
5251 ops = firm_set_default_transform_node(code, ops);
5252 ops = firm_set_default_node_cmp_attr(code, ops);
5253 ops = firm_set_default_get_type(code, ops);
5254 ops = firm_set_default_get_type_attr(code, ops);
5255 ops = firm_set_default_get_entity_attr(code, ops);
5258 } /* firm_set_default_operations */