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)
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);
1163 ir_mode *n_mode = get_irn_mode(n);
1164 ir_mode *a_mode = get_irn_mode(a);
1166 if (n_mode == a_mode) { /* No Conv necessary */
1167 if (get_Conv_strict(n)) {
1168 /* special case: the predecessor might be a also a Conv */
1170 if (! get_Conv_strict(a)) {
1171 /* first one is not strict, kick it */
1172 set_Conv_op(n, get_Conv_op(a));
1175 /* else both are strict conv, second is superflous */
1176 } else if(is_Proj(a)) {
1177 ir_node *pred = get_Proj_pred(a);
1179 /* loads always return with the exact precision of n_mode */
1180 assert(get_Load_mode(pred) == n_mode);
1185 /* leave strict floating point Conv's */
1189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1190 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1191 ir_node *b = get_Conv_op(a);
1192 ir_mode *b_mode = get_irn_mode(b);
1194 if (n_mode == b_mode) {
1195 if (n_mode == mode_b) {
1196 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1197 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1198 } else if (mode_is_int(n_mode)) {
1199 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1200 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1201 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1207 } /* equivalent_node_Conv */
1210 * A Cast may be removed if the type of the previous node
1211 * is already the type of the Cast.
1213 static ir_node *equivalent_node_Cast(ir_node *n) {
1215 ir_node *pred = get_Cast_op(n);
1217 if (get_irn_type(pred) == get_Cast_type(n)) {
1219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1222 } /* equivalent_node_Cast */
1225 * Several optimizations:
1226 * - no Phi in start block.
1227 * - remove Id operators that are inputs to Phi
1228 * - fold Phi-nodes, iff they have only one predecessor except
1231 static ir_node *equivalent_node_Phi(ir_node *n) {
1235 ir_node *block = NULL; /* to shutup gcc */
1236 ir_node *first_val = NULL; /* to shutup gcc */
1238 if (!get_opt_normalize()) return n;
1240 n_preds = get_Phi_n_preds(n);
1242 block = get_nodes_block(n);
1243 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1244 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1245 if ((is_Block_dead(block)) || /* Control dead */
1246 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1247 return new_Bad(); /* in the Start Block. */
1249 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1251 /* If the Block has a Bad pred, we also have one. */
1252 for (i = 0; i < n_preds; ++i)
1253 if (is_Bad(get_Block_cfgpred(block, i)))
1254 set_Phi_pred(n, i, new_Bad());
1256 /* Find first non-self-referencing input */
1257 for (i = 0; i < n_preds; ++i) {
1258 first_val = get_Phi_pred(n, i);
1259 if ( (first_val != n) /* not self pointer */
1261 && (! is_Bad(first_val))
1263 ) { /* value not dead */
1264 break; /* then found first value. */
1269 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1273 /* search for rest of inputs, determine if any of these
1274 are non-self-referencing */
1275 while (++i < n_preds) {
1276 ir_node *scnd_val = get_Phi_pred(n, i);
1277 if ( (scnd_val != n)
1278 && (scnd_val != first_val)
1280 && (! is_Bad(scnd_val))
1288 /* Fold, if no multiple distinct non-self-referencing inputs */
1290 DBG_OPT_PHI(oldn, n);
1293 } /* equivalent_node_Phi */
1296 * Several optimizations:
1297 * - no Sync in start block.
1298 * - fold Sync-nodes, iff they have only one predecessor except
1301 static ir_node *equivalent_node_Sync(ir_node *n) {
1305 ir_node *first_val = NULL; /* to shutup gcc */
1307 if (!get_opt_normalize()) return n;
1309 n_preds = get_Sync_n_preds(n);
1311 /* Find first non-self-referencing input */
1312 for (i = 0; i < n_preds; ++i) {
1313 first_val = get_Sync_pred(n, i);
1314 if ((first_val != n) /* not self pointer */ &&
1315 (! is_Bad(first_val))
1316 ) { /* value not dead */
1317 break; /* then found first value. */
1322 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1325 /* search the rest of inputs, determine if any of these
1326 are non-self-referencing */
1327 while (++i < n_preds) {
1328 ir_node *scnd_val = get_Sync_pred(n, i);
1329 if ((scnd_val != n) &&
1330 (scnd_val != first_val) &&
1331 (! is_Bad(scnd_val))
1337 /* Fold, if no multiple distinct non-self-referencing inputs */
1339 DBG_OPT_SYNC(oldn, n);
1342 } /* equivalent_node_Sync */
1345 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1346 * ProjX(Load) and ProjX(Store).
1348 static ir_node *equivalent_node_Proj(ir_node *proj) {
1349 ir_node *oldn = proj;
1350 ir_node *a = get_Proj_pred(proj);
1353 /* Remove the Tuple/Proj combination. */
1354 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1355 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1356 DBG_OPT_TUPLE(oldn, a, proj);
1358 /* This should not happen! */
1359 assert(! "found a Proj with higher number than Tuple predecessors");
1362 } else if (get_irn_mode(proj) == mode_X) {
1363 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1364 /* Remove dead control flow -- early gigo(). */
1366 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1368 /* get the Load address */
1369 ir_node *addr = get_Load_ptr(a);
1370 ir_node *blk = get_irn_n(a, -1);
1373 if (value_not_null(addr, &confirm)) {
1374 if (confirm == NULL) {
1375 /* this node may float if it did not depend on a Confirm */
1376 set_irn_pinned(a, op_pin_state_floats);
1378 if (get_Proj_proj(proj) == pn_Load_X_except) {
1379 DBG_OPT_EXC_REM(proj);
1382 return new_r_Jmp(current_ir_graph, blk);
1384 } else if (is_Store(a)) {
1385 /* get the load/store address */
1386 ir_node *addr = get_Store_ptr(a);
1387 ir_node *blk = get_irn_n(a, -1);
1390 if (value_not_null(addr, &confirm)) {
1391 if (confirm == NULL) {
1392 /* this node may float if it did not depend on a Confirm */
1393 set_irn_pinned(a, op_pin_state_floats);
1395 if (get_Proj_proj(proj) == pn_Store_X_except) {
1396 DBG_OPT_EXC_REM(proj);
1399 return new_r_Jmp(current_ir_graph, blk);
1406 } /* equivalent_node_Proj */
1411 static ir_node *equivalent_node_Id(ir_node *n) {
1416 } while (get_irn_op(n) == op_Id);
1418 DBG_OPT_ID(oldn, n);
1420 } /* equivalent_node_Id */
1425 static ir_node *equivalent_node_Mux(ir_node *n)
1427 ir_node *oldn = n, *sel = get_Mux_sel(n);
1428 tarval *ts = value_of(sel);
1430 /* Mux(true, f, t) == t */
1431 if (ts == tarval_b_true) {
1432 n = get_Mux_true(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1435 /* Mux(false, f, t) == f */
1436 else if (ts == tarval_b_false) {
1437 n = get_Mux_false(n);
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1440 /* Mux(v, x, x) == x */
1441 else if (get_Mux_false(n) == get_Mux_true(n)) {
1442 n = get_Mux_true(n);
1443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1445 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1446 ir_node *cmp = get_Proj_pred(sel);
1447 long proj_nr = get_Proj_proj(sel);
1448 ir_node *b = get_Mux_false(n);
1449 ir_node *a = get_Mux_true(n);
1452 * Note: normalization puts the constant on the right site,
1453 * so we check only one case.
1455 * Note further that these optimization work even for floating point
1456 * with NaN's because -NaN == NaN.
1457 * However, if +0 and -0 is handled differently, we cannot use the first one.
1459 if (is_Cmp(cmp) && get_Cmp_left(cmp) == a) {
1460 ir_node *cmp_r = get_Cmp_right(cmp);
1461 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1462 /* Mux(a CMP 0, X, a) */
1463 if (is_Minus(b) && get_Minus_op(b) == a) {
1464 /* Mux(a CMP 0, -a, a) */
1465 if (proj_nr == pn_Cmp_Eq) {
1466 /* Mux(a == 0, -a, a) ==> -a */
1468 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1469 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1470 /* Mux(a != 0, -a, a) ==> a */
1472 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1474 } else if (is_Const(b) && is_Const_null(b)) {
1475 /* Mux(a CMP 0, 0, a) */
1476 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1477 /* Mux(a != 0, 0, a) ==> a */
1479 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1480 } else if (proj_nr == pn_Cmp_Eq) {
1481 /* Mux(a == 0, 0, a) ==> 0 */
1483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1490 } /* equivalent_node_Mux */
1493 * Returns a equivalent node of a Psi: if a condition is true
1494 * and all previous conditions are false we know its value.
1495 * If all conditions are false its value is the default one.
1497 static ir_node *equivalent_node_Psi(ir_node *n) {
1499 return equivalent_node_Mux(n);
1501 } /* equivalent_node_Psi */
1504 * Optimize -a CMP -b into b CMP a.
1505 * This works only for for modes where unary Minus
1507 * Note that two-complement integers can Overflow
1508 * so it will NOT work.
1510 * For == and != can be handled in Proj(Cmp)
1512 static ir_node *equivalent_node_Cmp(ir_node *n) {
1513 ir_node *left = get_Cmp_left(n);
1514 ir_node *right = get_Cmp_right(n);
1516 if (is_Minus(left) && is_Minus(right) &&
1517 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1518 left = get_Minus_op(left);
1519 right = get_Minus_op(right);
1520 set_Cmp_left(n, right);
1521 set_Cmp_right(n, left);
1524 } /* equivalent_node_Cmp */
1527 * Remove Confirm nodes if setting is on.
1528 * Replace Confirms(x, '=', Constlike) by Constlike.
1530 static ir_node *equivalent_node_Confirm(ir_node *n) {
1531 ir_node *pred = get_Confirm_value(n);
1532 pn_Cmp pnc = get_Confirm_cmp(n);
1534 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1536 * rare case: two identical Confirms one after another,
1537 * replace the second one with the first.
1541 if (pnc == pn_Cmp_Eq) {
1542 ir_node *bound = get_Confirm_bound(n);
1545 * Optimize a rare case:
1546 * Confirm(x, '=', Constlike) ==> Constlike
1548 if (is_irn_constlike(bound)) {
1549 DBG_OPT_CONFIRM(n, bound);
1553 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1557 * Optimize CopyB(mem, x, x) into a Nop.
1559 static ir_node *equivalent_node_CopyB(ir_node *n) {
1560 ir_node *a = get_CopyB_dst(n);
1561 ir_node *b = get_CopyB_src(n);
1564 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1565 ir_node *mem = get_CopyB_mem(n);
1566 ir_node *blk = get_nodes_block(n);
1567 turn_into_tuple(n, pn_CopyB_max);
1568 set_Tuple_pred(n, pn_CopyB_M, mem);
1569 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1570 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1571 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1574 } /* equivalent_node_CopyB */
1577 * Optimize Bounds(idx, idx, upper) into idx.
1579 static ir_node *equivalent_node_Bound(ir_node *n) {
1580 ir_node *idx = get_Bound_index(n);
1581 ir_node *lower = get_Bound_lower(n);
1584 /* By definition lower < upper, so if idx == lower -->
1585 lower <= idx && idx < upper */
1587 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1590 ir_node *pred = skip_Proj(idx);
1592 if (get_irn_op(pred) == op_Bound) {
1594 * idx was Bounds_check previously, it is still valid if
1595 * lower <= pred_lower && pred_upper <= upper.
1597 ir_node *upper = get_Bound_upper(n);
1598 if (get_Bound_lower(pred) == lower &&
1599 get_Bound_upper(pred) == upper) {
1601 * One could expect that we simply return the previous
1602 * Bound here. However, this would be wrong, as we could
1603 * add an exception Proj to a new location then.
1604 * So, we must turn in into a tuple.
1611 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1612 ir_node *mem = get_Bound_mem(n);
1613 ir_node *blk = get_nodes_block(n);
1614 turn_into_tuple(n, pn_Bound_max);
1615 set_Tuple_pred(n, pn_Bound_M, mem);
1616 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1617 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1618 set_Tuple_pred(n, pn_Bound_res, idx);
1621 } /* equivalent_node_Bound */
1624 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1625 * perform no actual computation, as, e.g., the Id nodes. It does not create
1626 * new nodes. It is therefore safe to free n if the node returned is not n.
1627 * If a node returns a Tuple we can not just skip it. If the size of the
1628 * in array fits, we transform n into a tuple (e.g., Div).
1630 ir_node *equivalent_node(ir_node *n) {
1631 if (n->op->ops.equivalent_node)
1632 return n->op->ops.equivalent_node(n);
1634 } /* equivalent_node */
1637 * Sets the default equivalent node operation for an ir_op_ops.
1639 * @param code the opcode for the default operation
1640 * @param ops the operations initialized
1645 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1649 ops->equivalent_node = equivalent_node_##a; \
1689 } /* firm_set_default_equivalent_node */
1692 * Returns non-zero if a node is a Phi node
1693 * with all predecessors constant.
1695 static int is_const_Phi(ir_node *n) {
1698 if (! is_Phi(n) || get_irn_arity(n) == 0)
1700 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1701 if (! is_Const(get_irn_n(n, i)))
1704 } /* is_const_Phi */
1707 * Apply an evaluator on a binop with a constant operators (and one Phi).
1709 * @param phi the Phi node
1710 * @param other the other operand
1711 * @param eval an evaluator function
1712 * @param mode the mode of the result, may be different from the mode of the Phi!
1713 * @param left if non-zero, other is the left operand, else the right
1715 * @return a new Phi node if the conversion was successful, NULL else
1717 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1722 int i, n = get_irn_arity(phi);
1724 NEW_ARR_A(void *, res, n);
1726 for (i = 0; i < n; ++i) {
1727 pred = get_irn_n(phi, i);
1728 tv = get_Const_tarval(pred);
1729 tv = eval(other, tv);
1731 if (tv == tarval_bad) {
1732 /* folding failed, bad */
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 tv = get_Const_tarval(pred);
1741 tv = eval(tv, other);
1743 if (tv == tarval_bad) {
1744 /* folding failed, bad */
1750 irg = current_ir_graph;
1751 for (i = 0; i < n; ++i) {
1752 pred = get_irn_n(phi, i);
1753 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1754 mode, res[i], get_Const_type(pred));
1756 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1757 } /* apply_binop_on_phi */
1760 * Apply an evaluator on a binop with two constant Phi.
1762 * @param a the left Phi node
1763 * @param b the right Phi node
1764 * @param eval an evaluator function
1765 * @param mode the mode of the result, may be different from the mode of the Phi!
1767 * @return a new Phi node if the conversion was successful, NULL else
1769 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1770 tarval *tv_l, *tv_r, *tv;
1776 if (get_nodes_block(a) != get_nodes_block(b))
1779 n = get_irn_arity(a);
1780 NEW_ARR_A(void *, res, n);
1782 for (i = 0; i < n; ++i) {
1783 pred = get_irn_n(a, i);
1784 tv_l = get_Const_tarval(pred);
1785 pred = get_irn_n(b, i);
1786 tv_r = get_Const_tarval(pred);
1787 tv = eval(tv_l, tv_r);
1789 if (tv == tarval_bad) {
1790 /* folding failed, bad */
1795 irg = current_ir_graph;
1796 for (i = 0; i < n; ++i) {
1797 pred = get_irn_n(a, i);
1798 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1800 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1801 } /* apply_binop_on_2_phis */
1804 * Apply an evaluator on a unop with a constant operator (a Phi).
1806 * @param phi the Phi node
1807 * @param eval an evaluator function
1809 * @return a new Phi node if the conversion was successful, NULL else
1811 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1817 int i, n = get_irn_arity(phi);
1819 NEW_ARR_A(void *, res, n);
1820 for (i = 0; i < n; ++i) {
1821 pred = get_irn_n(phi, i);
1822 tv = get_Const_tarval(pred);
1825 if (tv == tarval_bad) {
1826 /* folding failed, bad */
1831 mode = get_irn_mode(phi);
1832 irg = current_ir_graph;
1833 for (i = 0; i < n; ++i) {
1834 pred = get_irn_n(phi, i);
1835 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1836 mode, res[i], get_Const_type(pred));
1838 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1839 } /* apply_unop_on_phi */
1842 * Apply a conversion on a constant operator (a Phi).
1844 * @param phi the Phi node
1846 * @return a new Phi node if the conversion was successful, NULL else
1848 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1853 int i, n = get_irn_arity(phi);
1855 NEW_ARR_A(void *, res, n);
1856 for (i = 0; i < n; ++i) {
1857 pred = get_irn_n(phi, i);
1858 tv = get_Const_tarval(pred);
1859 tv = tarval_convert_to(tv, mode);
1861 if (tv == tarval_bad) {
1862 /* folding failed, bad */
1867 irg = current_ir_graph;
1868 for (i = 0; i < n; ++i) {
1869 pred = get_irn_n(phi, i);
1870 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1871 mode, res[i], get_Const_type(pred));
1873 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1874 } /* apply_conv_on_phi */
1877 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1878 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1879 * If possible, remove the Conv's.
1881 static ir_node *transform_node_AddSub(ir_node *n) {
1882 ir_mode *mode = get_irn_mode(n);
1884 if (mode_is_reference(mode)) {
1885 ir_node *left = get_binop_left(n);
1886 ir_node *right = get_binop_right(n);
1887 int ref_bits = get_mode_size_bits(mode);
1889 if (is_Conv(left)) {
1890 ir_mode *mode = get_irn_mode(left);
1891 int bits = get_mode_size_bits(mode);
1893 if (ref_bits == bits &&
1894 mode_is_int(mode) &&
1895 get_mode_arithmetic(mode) == irma_twos_complement) {
1896 ir_node *pre = get_Conv_op(left);
1897 ir_mode *pre_mode = get_irn_mode(pre);
1899 if (mode_is_int(pre_mode) &&
1900 get_mode_size_bits(pre_mode) == bits &&
1901 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1902 /* ok, this conv just changes to sign, moreover the calculation
1903 * is done with same number of bits as our address mode, so
1904 * we can ignore the conv as address calculation can be viewed
1905 * as either signed or unsigned
1907 set_binop_left(n, pre);
1912 if (is_Conv(right)) {
1913 ir_mode *mode = get_irn_mode(right);
1914 int bits = get_mode_size_bits(mode);
1916 if (ref_bits == bits &&
1917 mode_is_int(mode) &&
1918 get_mode_arithmetic(mode) == irma_twos_complement) {
1919 ir_node *pre = get_Conv_op(right);
1920 ir_mode *pre_mode = get_irn_mode(pre);
1922 if (mode_is_int(pre_mode) &&
1923 get_mode_size_bits(pre_mode) == bits &&
1924 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1925 /* ok, this conv just changes to sign, moreover the calculation
1926 * is done with same number of bits as our address mode, so
1927 * we can ignore the conv as address calculation can be viewed
1928 * as either signed or unsigned
1930 set_binop_right(n, pre);
1936 } /* transform_node_AddSub */
1938 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1940 if (is_Const(b) && is_const_Phi(a)) { \
1941 /* check for Op(Phi, Const) */ \
1942 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1944 else if (is_Const(a) && is_const_Phi(b)) { \
1945 /* check for Op(Const, Phi) */ \
1946 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1948 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1949 /* check for Op(Phi, Phi) */ \
1950 c = apply_binop_on_2_phis(a, b, eval, mode); \
1953 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1957 #define HANDLE_UNOP_PHI(eval, a, c) \
1959 if (is_const_Phi(a)) { \
1960 /* check for Op(Phi) */ \
1961 c = apply_unop_on_phi(a, eval); \
1963 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1969 * Do the AddSub optimization, then Transform
1970 * Constant folding on Phi
1971 * Add(a,a) -> Mul(a, 2)
1972 * Add(Mul(a, x), a) -> Mul(a, x+1)
1973 * if the mode is integer or float.
1974 * Transform Add(a,-b) into Sub(a,b).
1975 * Reassociation might fold this further.
1977 static ir_node *transform_node_Add(ir_node *n) {
1979 ir_node *a, *b, *c, *oldn = n;
1981 n = transform_node_AddSub(n);
1983 a = get_Add_left(n);
1984 b = get_Add_right(n);
1986 mode = get_irn_mode(n);
1987 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
1989 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1990 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1993 if (mode_is_num(mode)) {
1994 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1995 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1996 ir_node *block = get_irn_n(n, -1);
1999 get_irn_dbg_info(n),
2003 new_r_Const_long(current_ir_graph, block, mode, 2),
2005 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2010 get_irn_dbg_info(n),
2016 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2021 get_irn_dbg_info(n),
2027 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2030 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2031 /* Here we rely on constants be on the RIGHT side */
2033 ir_node *op = get_Not_op(a);
2035 if (is_Const(b) && is_Const_one(b)) {
2037 ir_node *blk = get_irn_n(n, -1);
2038 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2039 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2044 ir_node *blk = get_irn_n(n, -1);
2045 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2046 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2051 ir_node *op = get_Not_op(b);
2055 ir_node *blk = get_irn_n(n, -1);
2056 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2057 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2064 } /* transform_node_Add */
2067 * returns -cnst or NULL if impossible
2069 static ir_node *const_negate(ir_node *cnst) {
2070 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2071 dbg_info *dbgi = get_irn_dbg_info(cnst);
2072 ir_graph *irg = get_irn_irg(cnst);
2073 ir_node *block = get_nodes_block(cnst);
2074 ir_mode *mode = get_irn_mode(cnst);
2075 if (tv == tarval_bad) return NULL;
2076 return new_rd_Const(dbgi, irg, block, mode, tv);
2080 * Do the AddSub optimization, then Transform
2081 * Constant folding on Phi
2082 * Sub(0,a) -> Minus(a)
2083 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2084 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2085 * Sub(Add(a, x), x) -> a
2086 * Sub(x, Add(x, a)) -> -a
2087 * Sub(x, Const) -> Add(x, -Const)
2089 static ir_node *transform_node_Sub(ir_node *n) {
2094 n = transform_node_AddSub(n);
2096 a = get_Sub_left(n);
2097 b = get_Sub_right(n);
2099 mode = get_irn_mode(n);
2102 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2104 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2105 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2108 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2109 /* a - C -> a + (-C) */
2110 ir_node *cnst = const_negate(b);
2112 ir_node *block = get_nodes_block(n);
2113 dbg_info *dbgi = get_irn_dbg_info(n);
2114 ir_graph *irg = get_irn_irg(n);
2116 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2117 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2122 if (is_Minus(a)) { /* (-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 *left = get_Minus_op(a);
2127 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2129 n = new_rd_Minus(dbg, irg, block, add, mode);
2130 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2132 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2133 ir_graph *irg = current_ir_graph;
2134 dbg_info *dbg = get_irn_dbg_info(n);
2135 ir_node *block = get_nodes_block(n);
2136 ir_node *right = get_Minus_op(b);
2138 n = new_rd_Add(dbg, irg, block, a, right, mode);
2139 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2141 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2142 ir_graph *irg = current_ir_graph;
2143 dbg_info *s_dbg = get_irn_dbg_info(b);
2144 ir_node *s_block = get_nodes_block(b);
2145 ir_node *s_left = get_Sub_right(b);
2146 ir_node *s_right = get_Sub_left(b);
2147 ir_mode *s_mode = get_irn_mode(b);
2148 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2149 dbg_info *a_dbg = get_irn_dbg_info(n);
2150 ir_node *a_block = get_nodes_block(n);
2152 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2153 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2155 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2156 ir_node *m_right = get_Mul_right(b);
2157 if (is_Const(m_right)) {
2158 ir_node *cnst2 = const_negate(m_right);
2159 if (cnst2 != NULL) {
2160 ir_graph *irg = current_ir_graph;
2161 dbg_info *m_dbg = get_irn_dbg_info(b);
2162 ir_node *m_block = get_nodes_block(b);
2163 ir_node *m_left = get_Mul_left(b);
2164 ir_mode *m_mode = get_irn_mode(b);
2165 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2166 dbg_info *a_dbg = get_irn_dbg_info(n);
2167 ir_node *a_block = get_nodes_block(n);
2169 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2176 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2177 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2179 get_irn_dbg_info(n),
2184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2188 if (mode_wrap_around(mode)) {
2189 ir_node *left = get_Add_left(a);
2190 ir_node *right = get_Add_right(a);
2192 /* FIXME: Does the Conv's work only for two complement or generally? */
2194 if (mode != get_irn_mode(right)) {
2195 /* This Sub is an effective Cast */
2196 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2199 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2201 } else if (right == b) {
2202 if (mode != get_irn_mode(left)) {
2203 /* This Sub is an effective Cast */
2204 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2213 if (mode_wrap_around(mode)) {
2214 ir_node *left = get_Add_left(b);
2215 ir_node *right = get_Add_right(b);
2217 /* FIXME: Does the Conv's work only for two complement or generally? */
2219 ir_mode *r_mode = get_irn_mode(right);
2221 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2222 if (mode != r_mode) {
2223 /* This Sub is an effective Cast */
2224 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2226 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2228 } else if (right == a) {
2229 ir_mode *l_mode = get_irn_mode(left);
2231 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2232 if (mode != l_mode) {
2233 /* This Sub is an effective Cast */
2234 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2236 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2241 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2242 ir_mode *mode = get_irn_mode(a);
2244 if (mode == get_irn_mode(b)) {
2246 ir_node *op_a = get_Conv_op(a);
2247 ir_node *op_b = get_Conv_op(b);
2249 /* check if it's allowed to skip the conv */
2250 ma = get_irn_mode(op_a);
2251 mb = get_irn_mode(op_b);
2253 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2254 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2257 set_Sub_right(n, b);
2263 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2264 if (!is_reassoc_running() && is_Mul(a)) {
2265 ir_node *ma = get_Mul_left(a);
2266 ir_node *mb = get_Mul_right(a);
2269 ir_node *blk = get_irn_n(n, -1);
2271 get_irn_dbg_info(n),
2272 current_ir_graph, blk,
2275 get_irn_dbg_info(n),
2276 current_ir_graph, blk,
2278 new_r_Const_long(current_ir_graph, blk, mode, 1),
2281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2283 } else if (mb == b) {
2284 ir_node *blk = get_irn_n(n, -1);
2286 get_irn_dbg_info(n),
2287 current_ir_graph, blk,
2290 get_irn_dbg_info(n),
2291 current_ir_graph, blk,
2293 new_r_Const_long(current_ir_graph, blk, mode, 1),
2296 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2300 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2301 ir_node *x = get_Sub_left(a);
2302 ir_node *y = get_Sub_right(a);
2303 ir_node *blk = get_irn_n(n, -1);
2304 ir_mode *m_b = get_irn_mode(b);
2305 ir_mode *m_y = get_irn_mode(y);
2309 /* Determine the right mode for the Add. */
2312 else if (mode_is_reference(m_b))
2314 else if (mode_is_reference(m_y))
2318 * Both modes are different but none is reference,
2319 * happens for instance in SubP(SubP(P, Iu), Is).
2320 * We have two possibilities here: Cast or ignore.
2321 * Currently we ignore this case.
2326 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2328 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2329 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2333 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2334 if (is_Const(a) && is_Not(b)) {
2335 /* c - ~X = X + (c+1) */
2336 tarval *tv = get_Const_tarval(a);
2338 tv = tarval_add(tv, get_mode_one(mode));
2339 if (tv != tarval_bad) {
2340 ir_node *blk = get_irn_n(n, -1);
2341 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2342 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2343 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2349 } /* transform_node_Sub */
2352 * Several transformation done on n*n=2n bits mul.
2353 * These transformations must be done here because new nodes may be produced.
2355 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2357 ir_node *a = get_Mul_left(n);
2358 ir_node *b = get_Mul_right(n);
2359 tarval *ta = value_of(a);
2360 tarval *tb = value_of(b);
2361 ir_mode *smode = get_irn_mode(a);
2363 if (ta == get_mode_one(smode)) {
2364 ir_node *blk = get_irn_n(n, -1);
2365 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2369 else if (ta == get_mode_minus_one(smode)) {
2370 ir_node *blk = get_irn_n(n, -1);
2371 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2372 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2373 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2376 if (tb == get_mode_one(smode)) {
2377 ir_node *blk = get_irn_n(a, -1);
2378 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2379 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2382 else if (tb == get_mode_minus_one(smode)) {
2383 ir_node *blk = get_irn_n(n, -1);
2384 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2385 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2386 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2393 * Transform Mul(a,-1) into -a.
2394 * Do constant evaluation of Phi nodes.
2395 * Do architecture dependent optimizations on Mul nodes
2397 static ir_node *transform_node_Mul(ir_node *n) {
2398 ir_node *c, *oldn = n;
2399 ir_mode *mode = get_irn_mode(n);
2400 ir_node *a = get_Mul_left(n);
2401 ir_node *b = get_Mul_right(n);
2403 if (is_Bad(a) || is_Bad(b))
2406 if (mode != get_irn_mode(a))
2407 return transform_node_Mul2n(n, mode);
2409 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2411 if (mode_is_signed(mode)) {
2414 if (value_of(a) == get_mode_minus_one(mode))
2416 else if (value_of(b) == get_mode_minus_one(mode))
2419 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2420 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2425 if (is_Const(b)) { /* (-a) * const -> a * -const */
2426 ir_node *cnst = const_negate(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), cnst, mode);
2431 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2434 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2435 dbg_info *dbgi = get_irn_dbg_info(n);
2436 ir_node *block = get_nodes_block(n);
2437 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2438 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2440 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2441 ir_node *sub_l = get_Sub_left(b);
2442 ir_node *sub_r = get_Sub_right(b);
2443 dbg_info *dbgi = get_irn_dbg_info(n);
2444 ir_graph *irg = current_ir_graph;
2445 ir_node *block = get_nodes_block(n);
2446 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2447 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2448 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2451 } else if (is_Minus(b)) {
2452 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2453 ir_node *sub_l = get_Sub_left(a);
2454 ir_node *sub_r = get_Sub_right(a);
2455 dbg_info *dbgi = get_irn_dbg_info(n);
2456 ir_graph *irg = current_ir_graph;
2457 ir_node *block = get_nodes_block(n);
2458 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2459 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2460 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2464 if (get_mode_arithmetic(mode) == irma_ieee754) {
2466 tarval *tv = get_Const_tarval(a);
2467 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2468 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2469 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2473 else if (is_Const(b)) {
2474 tarval *tv = get_Const_tarval(b);
2475 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2476 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2477 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2482 return arch_dep_replace_mul_with_shifts(n);
2483 } /* transform_node_Mul */
2486 * Transform a Div Node.
2488 static ir_node *transform_node_Div(ir_node *n) {
2489 ir_mode *mode = get_Div_resmode(n);
2490 ir_node *a = get_Div_left(n);
2491 ir_node *b = get_Div_right(n);
2495 if (is_Const(b) && is_const_Phi(a)) {
2496 /* check for Div(Phi, Const) */
2497 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2499 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2503 else if (is_Const(a) && is_const_Phi(b)) {
2504 /* check for Div(Const, Phi) */
2505 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2507 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2511 else if (is_const_Phi(a) && is_const_Phi(b)) {
2512 /* check for Div(Phi, Phi) */
2513 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2515 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2522 if (tv != tarval_bad) {
2523 value = new_Const(get_tarval_mode(tv), tv);
2525 DBG_OPT_CSTEVAL(n, value);
2528 ir_node *a = get_Div_left(n);
2529 ir_node *b = get_Div_right(n);
2532 if (a == b && value_not_zero(a, &dummy)) {
2533 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2534 value = new_Const(mode, get_mode_one(mode));
2535 DBG_OPT_CSTEVAL(n, value);
2538 if (mode_is_signed(mode) && is_Const(b)) {
2539 tarval *tv = get_Const_tarval(b);
2541 if (tv == get_mode_minus_one(mode)) {
2543 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2544 DBG_OPT_CSTEVAL(n, value);
2548 /* Try architecture dependent optimization */
2549 value = arch_dep_replace_div_by_const(n);
2557 /* Turn Div into a tuple (mem, jmp, bad, value) */
2558 mem = get_Div_mem(n);
2559 blk = get_irn_n(n, -1);
2561 /* skip a potential Pin */
2563 mem = get_Pin_op(mem);
2564 turn_into_tuple(n, pn_Div_max);
2565 set_Tuple_pred(n, pn_Div_M, mem);
2566 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2567 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2568 set_Tuple_pred(n, pn_Div_res, value);
2571 } /* transform_node_Div */
2574 * Transform a Mod node.
2576 static ir_node *transform_node_Mod(ir_node *n) {
2577 ir_mode *mode = get_Mod_resmode(n);
2578 ir_node *a = get_Mod_left(n);
2579 ir_node *b = get_Mod_right(n);
2583 if (is_Const(b) && is_const_Phi(a)) {
2584 /* check for Div(Phi, Const) */
2585 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2587 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2591 else if (is_Const(a) && is_const_Phi(b)) {
2592 /* check for Div(Const, Phi) */
2593 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2595 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2599 else if (is_const_Phi(a) && is_const_Phi(b)) {
2600 /* check for Div(Phi, Phi) */
2601 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2603 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2610 if (tv != tarval_bad) {
2611 value = new_Const(get_tarval_mode(tv), tv);
2613 DBG_OPT_CSTEVAL(n, value);
2616 ir_node *a = get_Mod_left(n);
2617 ir_node *b = get_Mod_right(n);
2620 if (a == b && value_not_zero(a, &dummy)) {
2621 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2622 value = new_Const(mode, get_mode_null(mode));
2623 DBG_OPT_CSTEVAL(n, value);
2626 if (mode_is_signed(mode) && is_Const(b)) {
2627 tarval *tv = get_Const_tarval(b);
2629 if (tv == get_mode_minus_one(mode)) {
2631 value = new_Const(mode, get_mode_null(mode));
2632 DBG_OPT_CSTEVAL(n, value);
2636 /* Try architecture dependent optimization */
2637 value = arch_dep_replace_mod_by_const(n);
2645 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2646 mem = get_Mod_mem(n);
2647 blk = get_irn_n(n, -1);
2649 /* skip a potential Pin */
2651 mem = get_Pin_op(mem);
2652 turn_into_tuple(n, pn_Mod_max);
2653 set_Tuple_pred(n, pn_Mod_M, mem);
2654 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2655 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2656 set_Tuple_pred(n, pn_Mod_res, value);
2659 } /* transform_node_Mod */
2662 * Transform a DivMod node.
2664 static ir_node *transform_node_DivMod(ir_node *n) {
2666 ir_node *a = get_DivMod_left(n);
2667 ir_node *b = get_DivMod_right(n);
2668 ir_mode *mode = get_DivMod_resmode(n);
2673 if (is_Const(b) && is_const_Phi(a)) {
2674 /* check for Div(Phi, Const) */
2675 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2676 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2678 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2679 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2683 else if (is_Const(a) && is_const_Phi(b)) {
2684 /* check for Div(Const, Phi) */
2685 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2686 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2688 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2689 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2693 else if (is_const_Phi(a) && is_const_Phi(b)) {
2694 /* check for Div(Phi, Phi) */
2695 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2696 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2698 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2699 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2706 if (tb != tarval_bad) {
2707 if (tb == get_mode_one(get_tarval_mode(tb))) {
2709 vb = new_Const(mode, get_mode_null(mode));
2710 DBG_OPT_CSTEVAL(n, vb);
2712 } else if (ta != tarval_bad) {
2713 tarval *resa, *resb;
2714 resa = tarval_div(ta, tb);
2715 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2716 Jmp for X result!? */
2717 resb = tarval_mod(ta, tb);
2718 if (resb == tarval_bad) return n; /* Causes exception! */
2719 va = new_Const(mode, resa);
2720 vb = new_Const(mode, resb);
2721 DBG_OPT_CSTEVAL(n, va);
2722 DBG_OPT_CSTEVAL(n, vb);
2724 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2725 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2726 vb = new_Const(mode, get_mode_null(mode));
2727 DBG_OPT_CSTEVAL(n, va);
2728 DBG_OPT_CSTEVAL(n, vb);
2730 } else { /* Try architecture dependent optimization */
2733 arch_dep_replace_divmod_by_const(&va, &vb, n);
2734 evaluated = va != NULL;
2736 } else if (a == b) {
2737 if (value_not_zero(a, &dummy)) {
2739 va = new_Const(mode, get_mode_one(mode));
2740 vb = new_Const(mode, get_mode_null(mode));
2741 DBG_OPT_CSTEVAL(n, va);
2742 DBG_OPT_CSTEVAL(n, vb);
2745 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2748 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2749 /* 0 / non-Const = 0 */
2754 if (evaluated) { /* replace by tuple */
2758 mem = get_DivMod_mem(n);
2759 /* skip a potential Pin */
2761 mem = get_Pin_op(mem);
2763 blk = get_irn_n(n, -1);
2764 turn_into_tuple(n, pn_DivMod_max);
2765 set_Tuple_pred(n, pn_DivMod_M, mem);
2766 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2767 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2768 set_Tuple_pred(n, pn_DivMod_res_div, va);
2769 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2773 } /* transform_node_DivMod */
2776 * Optimize x / c to x * (1/c)
2778 static ir_node *transform_node_Quot(ir_node *n) {
2779 ir_mode *mode = get_Quot_resmode(n);
2782 if (get_mode_arithmetic(mode) == irma_ieee754) {
2783 ir_node *b = get_Quot_right(n);
2786 tarval *tv = get_Const_tarval(b);
2788 tv = tarval_quo(get_mode_one(mode), tv);
2790 /* Do the transformation if the result is either exact or we are not
2791 using strict rules. */
2792 if (tv != tarval_bad &&
2793 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2794 ir_node *blk = get_irn_n(n, -1);
2795 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2796 ir_node *a = get_Quot_left(n);
2797 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2798 ir_node *mem = get_Quot_mem(n);
2800 /* skip a potential Pin */
2802 mem = get_Pin_op(mem);
2803 turn_into_tuple(n, pn_Quot_max);
2804 set_Tuple_pred(n, pn_Quot_M, mem);
2805 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2806 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2807 set_Tuple_pred(n, pn_Quot_res, m);
2808 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2813 } /* transform_node_Quot */
2816 * Optimize Abs(x) into x if x is Confirmed >= 0
2817 * Optimize Abs(x) into -x if x is Confirmed <= 0
2819 static ir_node *transform_node_Abs(ir_node *n) {
2820 ir_node *c, *oldn = n;
2821 ir_node *a = get_Abs_op(n);
2824 HANDLE_UNOP_PHI(tarval_abs, a, c);
2826 switch (classify_value_sign(a)) {
2827 case value_classified_negative:
2828 mode = get_irn_mode(n);
2831 * We can replace the Abs by -x here.
2832 * We even could add a new Confirm here.
2834 * Note that -x would create a new node, so we could
2835 * not run it in the equivalent_node() context.
2837 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2838 get_irn_n(n, -1), a, mode);
2840 DBG_OPT_CONFIRM(oldn, n);
2842 case value_classified_positive:
2843 /* n is positive, Abs is not needed */
2846 DBG_OPT_CONFIRM(oldn, n);
2851 } /* transform_node_Abs */
2854 * Transform a Cond node.
2856 * Replace the Cond by a Jmp if it branches on a constant
2859 static ir_node *transform_node_Cond(ir_node *n) {
2862 ir_node *a = get_Cond_selector(n);
2863 tarval *ta = value_of(a);
2865 /* we need block info which is not available in floating irgs */
2866 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2869 if ((ta != tarval_bad) &&
2870 (get_irn_mode(a) == mode_b) &&
2871 (get_opt_unreachable_code())) {
2872 /* It's a boolean Cond, branching on a boolean constant.
2873 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2874 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2875 turn_into_tuple(n, pn_Cond_max);
2876 if (ta == tarval_b_true) {
2877 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2878 set_Tuple_pred(n, pn_Cond_true, jmp);
2880 set_Tuple_pred(n, pn_Cond_false, jmp);
2881 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2883 /* We might generate an endless loop, so keep it alive. */
2884 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2887 } /* transform_node_Cond */
2889 typedef ir_node* (*recursive_transform) (ir_node *n);
2892 * makes use of distributive laws for and, or, eor
2893 * and(a OP c, b OP c) -> and(a, b) OP c
2894 * note, might return a different op than n
2896 static ir_node *transform_bitwise_distributive(ir_node *n,
2897 recursive_transform trans_func)
2900 ir_node *a = get_binop_left(n);
2901 ir_node *b = get_binop_right(n);
2902 ir_op *op = get_irn_op(a);
2903 ir_op *op_root = get_irn_op(n);
2905 if(op != get_irn_op(b))
2908 if (op == op_Conv) {
2909 ir_node *a_op = get_Conv_op(a);
2910 ir_node *b_op = get_Conv_op(b);
2911 ir_mode *a_mode = get_irn_mode(a_op);
2912 ir_mode *b_mode = get_irn_mode(b_op);
2913 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2914 ir_node *blk = get_irn_n(n, -1);
2917 set_binop_left(n, a_op);
2918 set_binop_right(n, b_op);
2919 set_irn_mode(n, a_mode);
2921 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2923 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2929 /* nothing to gain here */
2933 if (op == op_Shrs || op == op_Shr || op == op_Shl
2934 || op == op_And || op == op_Or || op == op_Eor) {
2935 ir_node *a_left = get_binop_left(a);
2936 ir_node *a_right = get_binop_right(a);
2937 ir_node *b_left = get_binop_left(b);
2938 ir_node *b_right = get_binop_right(b);
2942 if (is_op_commutative(op)) {
2943 if (a_left == b_left) {
2947 } else if(a_left == b_right) {
2951 } else if(a_right == b_left) {
2957 if(a_right == b_right) {
2964 /* (a sop c) & (b sop c) => (a & b) sop c */
2965 ir_node *blk = get_irn_n(n, -1);
2967 ir_node *new_n = exact_copy(n);
2968 set_binop_left(new_n, op1);
2969 set_binop_right(new_n, op2);
2970 new_n = trans_func(new_n);
2972 if(op_root == op_Eor && op == op_Or) {
2973 dbg_info *dbgi = get_irn_dbg_info(n);
2974 ir_graph *irg = current_ir_graph;
2975 ir_mode *mode = get_irn_mode(c);
2977 c = new_rd_Not(dbgi, irg, blk, c, mode);
2978 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2981 set_irn_n(n, -1, blk);
2982 set_binop_left(n, new_n);
2983 set_binop_right(n, c);
2984 add_identities(current_ir_graph->value_table, n);
2987 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2998 static ir_node *transform_node_And(ir_node *n) {
2999 ir_node *c, *oldn = n;
3000 ir_node *a = get_And_left(n);
3001 ir_node *b = get_And_right(n);
3004 mode = get_irn_mode(n);
3005 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3007 /* we can evaluate 2 Projs of the same Cmp */
3008 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3009 ir_node *pred_a = get_Proj_pred(a);
3010 ir_node *pred_b = get_Proj_pred(b);
3011 if (pred_a == pred_b) {
3012 dbg_info *dbgi = get_irn_dbg_info(n);
3013 ir_node *block = get_nodes_block(pred_a);
3014 pn_Cmp pn_a = get_Proj_proj(a);
3015 pn_Cmp pn_b = get_Proj_proj(b);
3016 /* yes, we can simply calculate with pncs */
3017 pn_Cmp new_pnc = pn_a & pn_b;
3019 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3024 ir_node *op = get_Not_op(b);
3026 ir_node *ba = get_And_left(op);
3027 ir_node *bb = get_And_right(op);
3029 /* it's enough to test the following cases due to normalization! */
3030 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3031 /* (a|b) & ~(a&b) = a^b */
3032 ir_node *block = get_nodes_block(n);
3034 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3035 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3043 ir_node *op = get_Not_op(a);
3045 ir_node *aa = get_And_left(op);
3046 ir_node *ab = get_And_right(op);
3048 /* it's enough to test the following cases due to normalization! */
3049 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3050 /* (a|b) & ~(a&b) = a^b */
3051 ir_node *block = get_nodes_block(n);
3053 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3054 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3061 ir_node *al = get_Eor_left(a);
3062 ir_node *ar = get_Eor_right(a);
3065 /* (b ^ a) & b -> ~a & b */
3066 dbg_info *dbg = get_irn_dbg_info(n);
3067 ir_node *block = get_nodes_block(n);
3069 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3070 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3071 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3075 /* (a ^ b) & b -> ~a & b */
3076 dbg_info *dbg = get_irn_dbg_info(n);
3077 ir_node *block = get_nodes_block(n);
3079 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3080 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3081 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3086 ir_node *bl = get_Eor_left(b);
3087 ir_node *br = get_Eor_right(b);
3090 /* a & (a ^ b) -> a & ~b */
3091 dbg_info *dbg = get_irn_dbg_info(n);
3092 ir_node *block = get_nodes_block(n);
3094 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3095 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3096 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3100 /* a & (b ^ a) -> a & ~b */
3101 dbg_info *dbg = get_irn_dbg_info(n);
3102 ir_node *block = get_nodes_block(n);
3104 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3105 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3110 if (is_Not(a) && is_Not(b)) {
3111 /* ~a & ~b = ~(a|b) */
3112 ir_node *block = get_nodes_block(n);
3113 ir_mode *mode = get_irn_mode(n);
3117 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3118 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3119 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3123 n = transform_bitwise_distributive(n, transform_node_And);
3126 } /* transform_node_And */
3131 static ir_node *transform_node_Eor(ir_node *n) {
3132 ir_node *c, *oldn = n;
3133 ir_node *a = get_Eor_left(n);
3134 ir_node *b = get_Eor_right(n);
3135 ir_mode *mode = get_irn_mode(n);
3137 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3139 /* we can evaluate 2 Projs of the same Cmp */
3140 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3141 ir_node *pred_a = get_Proj_pred(a);
3142 ir_node *pred_b = get_Proj_pred(b);
3143 if(pred_a == pred_b) {
3144 dbg_info *dbgi = get_irn_dbg_info(n);
3145 ir_node *block = get_nodes_block(pred_a);
3146 pn_Cmp pn_a = get_Proj_proj(a);
3147 pn_Cmp pn_b = get_Proj_proj(b);
3148 /* yes, we can simply calculate with pncs */
3149 pn_Cmp new_pnc = pn_a ^ pn_b;
3151 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3158 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3159 mode, get_mode_null(mode));
3160 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3161 } else if (mode == mode_b &&
3163 is_Const(b) && is_Const_one(b) &&
3164 is_Cmp(get_Proj_pred(a))) {
3165 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3166 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3167 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3170 } else if (is_Const(b)) {
3171 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3172 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3173 ir_node *not_op = get_Not_op(a);
3174 dbg_info *dbg = get_irn_dbg_info(n);
3175 ir_graph *irg = current_ir_graph;
3176 ir_node *block = get_nodes_block(n);
3177 ir_mode *mode = get_irn_mode(n);
3178 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3180 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3181 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3182 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3185 n = transform_bitwise_distributive(n, transform_node_Eor);
3189 } /* transform_node_Eor */
3194 static ir_node *transform_node_Not(ir_node *n) {
3195 ir_node *c, *oldn = n;
3196 ir_node *a = get_Not_op(n);
3197 ir_mode *mode = get_irn_mode(n);
3199 HANDLE_UNOP_PHI(tarval_not,a,c);
3201 /* check for a boolean Not */
3202 if (mode == mode_b &&
3204 is_Cmp(get_Proj_pred(a))) {
3205 /* We negate a Cmp. The Cmp has the negated result anyways! */
3206 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3207 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3212 ir_node *eor_b = get_Eor_right(a);
3213 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3214 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3215 ir_node *eor_a = get_Eor_left(a);
3216 dbg_info *dbg = get_irn_dbg_info(n);
3217 ir_graph *irg = current_ir_graph;
3218 ir_node *block = get_nodes_block(n);
3219 ir_mode *mode = get_irn_mode(n);
3220 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3224 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3225 if (is_Minus(a)) { /* ~-x -> x + -1 */
3226 dbg_info *dbg = get_irn_dbg_info(n);
3227 ir_graph *irg = current_ir_graph;
3228 ir_node *block = get_nodes_block(n);
3229 ir_node *add_l = get_Minus_op(a);
3230 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3231 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3232 } else if (is_Add(a)) {
3233 ir_node *add_r = get_Add_right(a);
3234 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3235 /* ~(x + -1) = -x */
3236 ir_node *op = get_Add_left(a);
3237 ir_node *blk = get_irn_n(n, -1);
3238 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3239 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3244 } /* transform_node_Not */
3247 * Transform a Minus.
3251 * -(a >>u (size-1)) = a >>s (size-1)
3252 * -(a >>s (size-1)) = a >>u (size-1)
3253 * -(a * const) -> a * -const
3255 static ir_node *transform_node_Minus(ir_node *n) {
3256 ir_node *c, *oldn = n;
3257 ir_node *a = get_Minus_op(n);
3260 HANDLE_UNOP_PHI(tarval_neg,a,c);
3262 mode = get_irn_mode(a);
3263 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3264 /* the following rules are only to twos-complement */
3267 ir_node *op = get_Not_op(a);
3268 tarval *tv = get_mode_one(mode);
3269 ir_node *blk = get_irn_n(n, -1);
3270 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3271 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3272 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3276 ir_node *c = get_Shr_right(a);
3279 tarval *tv = get_Const_tarval(c);
3281 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3282 /* -(a >>u (size-1)) = a >>s (size-1) */
3283 ir_node *v = get_Shr_left(a);
3285 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3286 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3292 ir_node *c = get_Shrs_right(a);
3295 tarval *tv = get_Const_tarval(c);
3297 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3298 /* -(a >>s (size-1)) = a >>u (size-1) */
3299 ir_node *v = get_Shrs_left(a);
3301 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3302 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3309 /* - (a-b) = b - a */
3310 ir_node *la = get_Sub_left(a);
3311 ir_node *ra = get_Sub_right(a);
3312 ir_node *blk = get_irn_n(n, -1);
3314 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3315 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3319 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3320 ir_node *mul_l = get_Mul_left(a);
3321 ir_node *mul_r = get_Mul_right(a);
3322 if (is_Const(mul_r)) {
3323 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3324 ir_node *cnst = new_Const(mode, tv);
3325 dbg_info *dbg = get_irn_dbg_info(a);
3326 ir_graph *irg = current_ir_graph;
3327 ir_node *block = get_nodes_block(a);
3328 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3329 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3335 } /* transform_node_Minus */
3338 * Transform a Cast_type(Const) into a new Const_type
3340 static ir_node *transform_node_Cast(ir_node *n) {
3342 ir_node *pred = get_Cast_op(n);
3343 ir_type *tp = get_irn_type(n);
3345 if (is_Const(pred) && get_Const_type(pred) != tp) {
3346 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3347 get_Const_tarval(pred), tp);
3348 DBG_OPT_CSTEVAL(oldn, n);
3349 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3350 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3351 get_SymConst_kind(pred), tp);
3352 DBG_OPT_CSTEVAL(oldn, n);
3356 } /* transform_node_Cast */
3359 * Transform a Proj(Div) with a non-zero value.
3360 * Removes the exceptions and routes the memory to the NoMem node.
3362 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3363 ir_node *div = get_Proj_pred(proj);
3364 ir_node *b = get_Div_right(div);
3365 ir_node *confirm, *res, *new_mem;
3368 if (value_not_zero(b, &confirm)) {
3369 /* div(x, y) && y != 0 */
3370 if (confirm == NULL) {
3371 /* we are sure we have a Const != 0 */
3372 new_mem = get_Div_mem(div);
3373 if (is_Pin(new_mem))
3374 new_mem = get_Pin_op(new_mem);
3375 set_Div_mem(div, new_mem);
3376 set_irn_pinned(div, op_pin_state_floats);
3379 proj_nr = get_Proj_proj(proj);
3381 case pn_Div_X_regular:
3382 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3384 case pn_Div_X_except:
3385 /* we found an exception handler, remove it */
3386 DBG_OPT_EXC_REM(proj);
3390 res = get_Div_mem(div);
3391 new_mem = get_irg_no_mem(current_ir_graph);
3394 /* This node can only float up to the Confirm block */
3395 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3397 set_irn_pinned(div, op_pin_state_floats);
3398 /* this is a Div without exception, we can remove the memory edge */
3399 set_Div_mem(div, new_mem);
3404 } /* transform_node_Proj_Div */
3407 * Transform a Proj(Mod) with a non-zero value.
3408 * Removes the exceptions and routes the memory to the NoMem node.
3410 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3411 ir_node *mod = get_Proj_pred(proj);
3412 ir_node *b = get_Mod_right(mod);
3413 ir_node *confirm, *res, *new_mem;
3416 if (value_not_zero(b, &confirm)) {
3417 /* mod(x, y) && y != 0 */
3418 proj_nr = get_Proj_proj(proj);
3420 if (confirm == NULL) {
3421 /* we are sure we have a Const != 0 */
3422 new_mem = get_Mod_mem(mod);
3423 if (is_Pin(new_mem))
3424 new_mem = get_Pin_op(new_mem);
3425 set_Mod_mem(mod, new_mem);
3426 set_irn_pinned(mod, op_pin_state_floats);
3431 case pn_Mod_X_regular:
3432 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3434 case pn_Mod_X_except:
3435 /* we found an exception handler, remove it */
3436 DBG_OPT_EXC_REM(proj);
3440 res = get_Mod_mem(mod);
3441 new_mem = get_irg_no_mem(current_ir_graph);
3444 /* This node can only float up to the Confirm block */
3445 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3447 /* this is a Mod without exception, we can remove the memory edge */
3448 set_Mod_mem(mod, new_mem);
3451 if (get_Mod_left(mod) == b) {
3452 /* a % a = 0 if a != 0 */
3453 ir_mode *mode = get_irn_mode(proj);
3454 ir_node *res = new_Const(mode, get_mode_null(mode));
3456 DBG_OPT_CSTEVAL(mod, res);
3462 } /* transform_node_Proj_Mod */
3465 * Transform a Proj(DivMod) with a non-zero value.
3466 * Removes the exceptions and routes the memory to the NoMem node.
3468 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3469 ir_node *divmod = get_Proj_pred(proj);
3470 ir_node *b = get_DivMod_right(divmod);
3471 ir_node *confirm, *res, *new_mem;
3474 if (value_not_zero(b, &confirm)) {
3475 /* DivMod(x, y) && y != 0 */
3476 proj_nr = get_Proj_proj(proj);
3478 if (confirm == NULL) {
3479 /* we are sure we have a Const != 0 */
3480 new_mem = get_DivMod_mem(divmod);
3481 if (is_Pin(new_mem))
3482 new_mem = get_Pin_op(new_mem);
3483 set_DivMod_mem(divmod, new_mem);
3484 set_irn_pinned(divmod, op_pin_state_floats);
3489 case pn_DivMod_X_regular:
3490 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3492 case pn_DivMod_X_except:
3493 /* we found an exception handler, remove it */
3494 DBG_OPT_EXC_REM(proj);
3498 res = get_DivMod_mem(divmod);
3499 new_mem = get_irg_no_mem(current_ir_graph);
3502 /* This node can only float up to the Confirm block */
3503 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3505 /* this is a DivMod without exception, we can remove the memory edge */
3506 set_DivMod_mem(divmod, new_mem);
3509 case pn_DivMod_res_mod:
3510 if (get_DivMod_left(divmod) == b) {
3511 /* a % a = 0 if a != 0 */
3512 ir_mode *mode = get_irn_mode(proj);
3513 ir_node *res = new_Const(mode, get_mode_null(mode));
3515 DBG_OPT_CSTEVAL(divmod, res);
3521 } /* transform_node_Proj_DivMod */
3524 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3526 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3527 if (get_opt_unreachable_code()) {
3528 ir_node *n = get_Proj_pred(proj);
3529 ir_node *b = get_Cond_selector(n);
3531 if (mode_is_int(get_irn_mode(b))) {
3532 tarval *tb = value_of(b);
3534 if (tb != tarval_bad) {
3535 /* we have a constant switch */
3536 long num = get_Proj_proj(proj);
3538 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3539 if (get_tarval_long(tb) == num) {
3540 /* Do NOT create a jump here, or we will have 2 control flow ops
3541 * in a block. This case is optimized away in optimize_cf(). */
3544 /* this case will NEVER be taken, kill it */
3552 } /* transform_node_Proj_Cond */
3555 * Create a 0 constant of given mode.
3557 static ir_node *create_zero_const(ir_mode *mode) {
3558 tarval *tv = get_mode_null(mode);
3559 ir_node *cnst = new_Const(mode, tv);
3564 /* the order of the values is important! */
3565 typedef enum const_class {
3571 static const_class classify_const(const ir_node* n)
3573 if (is_Const(n)) return const_const;
3574 if (is_irn_constlike(n)) return const_like;
3579 * Determines whether r is more constlike or has a larger index (in that order)
3582 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3584 const const_class l_order = classify_const(l);
3585 const const_class r_order = classify_const(r);
3587 l_order > r_order ||
3588 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3592 * Normalizes and optimizes Cmp nodes.
3594 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3595 ir_node *n = get_Proj_pred(proj);
3596 ir_node *left = get_Cmp_left(n);
3597 ir_node *right = get_Cmp_right(n);
3600 ir_mode *mode = NULL;
3601 long proj_nr = get_Proj_proj(proj);
3603 /* we can evaluate some cases directly */
3606 return new_Const(mode_b, get_tarval_b_false());
3608 return new_Const(mode_b, get_tarval_b_true());
3610 if (!mode_is_float(get_irn_mode(left)))
3611 return new_Const(mode_b, get_tarval_b_true());
3619 left = get_Cast_op(left);
3621 right = get_Cast_op(right);
3623 /* Remove unnecessary conversions */
3624 /* TODO handle constants */
3625 if (is_Conv(left) && is_Conv(right)) {
3626 ir_mode *mode = get_irn_mode(left);
3627 ir_node *op_left = get_Conv_op(left);
3628 ir_node *op_right = get_Conv_op(right);
3629 ir_mode *mode_left = get_irn_mode(op_left);
3630 ir_mode *mode_right = get_irn_mode(op_right);
3632 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3633 ir_graph *irg = current_ir_graph;
3634 ir_node *block = get_nodes_block(n);
3636 if (mode_left == mode_right) {
3640 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3641 } else if (smaller_mode(mode_left, mode_right)) {
3642 left = new_r_Conv(irg, block, op_left, mode_right);
3645 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3646 } else if (smaller_mode(mode_right, mode_left)) {
3648 right = new_r_Conv(irg, block, op_right, mode_left);
3650 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3655 /* remove operation of both sides if possible */
3656 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3658 * The following operations are NOT safe for floating point operations, for instance
3659 * 1.0 + inf == 2.0 + inf, =/=> x == y
3661 if (mode_is_int(get_irn_mode(left))) {
3662 unsigned lop = get_irn_opcode(left);
3664 if (lop == get_irn_opcode(right)) {
3665 ir_node *ll, *lr, *rl, *rr;
3667 /* same operation on both sides, try to remove */
3671 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3672 left = get_unop_op(left);
3673 right = get_unop_op(right);
3675 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3678 ll = get_Add_left(left);
3679 lr = get_Add_right(left);
3680 rl = get_Add_left(right);
3681 rr = get_Add_right(right);
3684 /* X + a CMP X + b ==> a CMP b */
3688 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3689 } else if (ll == rr) {
3690 /* X + a CMP b + X ==> a CMP b */
3694 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3695 } else if (lr == rl) {
3696 /* a + X CMP X + b ==> a CMP b */
3700 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3701 } else if (lr == rr) {
3702 /* a + X CMP b + X ==> a CMP b */
3706 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3710 ll = get_Sub_left(left);
3711 lr = get_Sub_right(left);
3712 rl = get_Sub_left(right);
3713 rr = get_Sub_right(right);
3716 /* X - a CMP X - b ==> a CMP b */
3720 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3721 } else if (lr == rr) {
3722 /* a - X CMP b - X ==> a CMP b */
3726 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3730 if (get_Rot_right(left) == get_Rot_right(right)) {
3731 /* a ROT X CMP b ROT X ==> a CMP b */
3732 left = get_Rot_left(left);
3733 right = get_Rot_left(right);
3735 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3743 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3744 if (is_Add(left) || is_Sub(left)) {
3745 ir_node *ll = get_binop_left(left);
3746 ir_node *lr = get_binop_right(left);
3748 if (lr == right && is_Add(left)) {
3755 right = create_zero_const(get_irn_mode(left));
3757 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3760 if (is_Add(right) || is_Sub(right)) {
3761 ir_node *rl = get_binop_left(right);
3762 ir_node *rr = get_binop_right(right);
3764 if (rr == left && is_Add(right)) {
3771 right = create_zero_const(get_irn_mode(left));
3773 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3776 } /* mode_is_int(...) */
3777 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3779 /* replace mode_b compares with ands/ors */
3780 if (get_irn_mode(left) == mode_b) {
3781 ir_graph *irg = current_ir_graph;
3782 ir_node *block = get_nodes_block(n);
3786 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3787 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3788 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3789 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3790 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3791 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3792 default: bres = NULL;
3795 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3800 if (!get_opt_reassociation())
3804 * First step: normalize the compare op
3805 * by placing the constant on the right side
3806 * or moving the lower address node to the left.
3808 if (!operands_are_normalized(left, right)) {
3814 proj_nr = get_inversed_pnc(proj_nr);
3819 * Second step: Try to reduce the magnitude
3820 * of a constant. This may help to generate better code
3821 * later and may help to normalize more compares.
3822 * Of course this is only possible for integer values.
3824 if (is_Const(right)) {
3825 mode = get_irn_mode(right);
3826 tv = get_Const_tarval(right);
3828 /* TODO extend to arbitrary constants */
3829 if (is_Conv(left) && tarval_is_null(tv)) {
3830 ir_node *op = get_Conv_op(left);
3831 ir_mode *op_mode = get_irn_mode(op);
3834 * UpConv(x) REL 0 ==> x REL 0
3836 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3837 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3838 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3839 tv = get_mode_null(op_mode);
3843 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3847 if (tv != tarval_bad) {
3848 /* the following optimization is possible on modes without Overflow
3849 * on Unary Minus or on == and !=:
3850 * -a CMP c ==> a swap(CMP) -c
3852 * Beware: for two-complement Overflow may occur, so only == and != can
3853 * be optimized, see this:
3854 * -MININT < 0 =/=> MININT > 0 !!!
3856 if (is_Minus(left) &&
3857 (!mode_overflow_on_unary_Minus(mode) ||
3858 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3859 tv = tarval_neg(tv);
3861 if (tv != tarval_bad) {
3862 left = get_Minus_op(left);
3863 proj_nr = get_inversed_pnc(proj_nr);
3865 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3867 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3868 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3869 tv = tarval_not(tv);
3871 if (tv != tarval_bad) {
3872 left = get_Not_op(left);
3874 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3878 /* for integer modes, we have more */
3879 if (mode_is_int(mode)) {
3880 /* Ne includes Unordered which is not possible on integers.
3881 * However, frontends often use this wrong, so fix it here */
3882 if (proj_nr & pn_Cmp_Uo) {
3883 proj_nr &= ~pn_Cmp_Uo;
3884 set_Proj_proj(proj, proj_nr);
3887 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3888 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3889 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3890 tv = tarval_sub(tv, get_mode_one(mode));
3892 if (tv != tarval_bad) {
3893 proj_nr ^= pn_Cmp_Eq;
3895 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3898 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3899 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3900 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3901 tv = tarval_add(tv, get_mode_one(mode));
3903 if (tv != tarval_bad) {
3904 proj_nr ^= pn_Cmp_Eq;
3906 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3910 /* the following reassociations work only for == and != */
3911 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3913 #if 0 /* Might be not that good in general */
3914 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3915 if (tarval_is_null(tv) && is_Sub(left)) {
3916 right = get_Sub_right(left);
3917 left = get_Sub_left(left);
3919 tv = value_of(right);
3921 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3925 if (tv != tarval_bad) {
3926 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3928 ir_node *c1 = get_Sub_right(left);
3929 tarval *tv2 = value_of(c1);
3931 if (tv2 != tarval_bad) {
3932 tv2 = tarval_add(tv, value_of(c1));
3934 if (tv2 != tarval_bad) {
3935 left = get_Sub_left(left);
3938 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3942 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3943 else if (is_Add(left)) {
3944 ir_node *a_l = get_Add_left(left);
3945 ir_node *a_r = get_Add_right(left);
3949 if (is_Const(a_l)) {
3951 tv2 = value_of(a_l);
3954 tv2 = value_of(a_r);
3957 if (tv2 != tarval_bad) {
3958 tv2 = tarval_sub(tv, tv2);
3960 if (tv2 != tarval_bad) {
3964 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3968 /* -a == c ==> a == -c, -a != c ==> a != -c */
3969 else if (is_Minus(left)) {
3970 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3972 if (tv2 != tarval_bad) {
3973 left = get_Minus_op(left);
3976 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3981 /* the following reassociations work only for <= */
3982 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3983 if (tv != tarval_bad) {
3984 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3985 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3991 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3992 switch (get_irn_opcode(left)) {
3996 c1 = get_And_right(left);
3999 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4000 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4002 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4004 /* TODO: move to constant evaluation */
4005 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4006 c1 = new_Const(mode_b, tv);
4007 DBG_OPT_CSTEVAL(proj, c1);
4011 if (tarval_is_single_bit(tv)) {
4013 * optimization for AND:
4015 * And(x, C) == C ==> And(x, C) != 0
4016 * And(x, C) != C ==> And(X, C) == 0
4018 * if C is a single Bit constant.
4021 /* check for Constant's match. We have check hare the tarvals,
4022 because our const might be changed */
4023 if (get_Const_tarval(c1) == tv) {
4024 /* fine: do the transformation */
4025 tv = get_mode_null(get_tarval_mode(tv));
4026 proj_nr ^= pn_Cmp_Leg;
4028 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4034 c1 = get_Or_right(left);
4035 if (is_Const(c1) && tarval_is_null(tv)) {
4037 * Or(x, C) == 0 && C != 0 ==> FALSE
4038 * Or(x, C) != 0 && C != 0 ==> TRUE
4040 if (! tarval_is_null(get_Const_tarval(c1))) {
4041 /* TODO: move to constant evaluation */
4042 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4043 c1 = new_Const(mode_b, tv);
4044 DBG_OPT_CSTEVAL(proj, c1);
4051 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4053 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4056 c1 = get_Shl_right(left);
4058 tarval *tv1 = get_Const_tarval(c1);
4059 ir_mode *mode = get_irn_mode(left);
4060 tarval *minus1 = get_mode_all_one(mode);
4061 tarval *amask = tarval_shr(minus1, tv1);
4062 tarval *cmask = tarval_shl(minus1, tv1);
4065 if (tarval_and(tv, cmask) != tv) {
4066 /* condition not met */
4067 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4068 c1 = new_Const(mode_b, tv);
4069 DBG_OPT_CSTEVAL(proj, c1);
4072 sl = get_Shl_left(left);
4073 blk = get_nodes_block(n);
4074 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4075 tv = tarval_shr(tv, tv1);
4077 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4082 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4084 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4087 c1 = get_Shr_right(left);
4089 tarval *tv1 = get_Const_tarval(c1);
4090 ir_mode *mode = get_irn_mode(left);
4091 tarval *minus1 = get_mode_all_one(mode);
4092 tarval *amask = tarval_shl(minus1, tv1);
4093 tarval *cmask = tarval_shr(minus1, tv1);
4096 if (tarval_and(tv, cmask) != tv) {
4097 /* condition not met */
4098 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4099 c1 = new_Const(mode_b, tv);
4100 DBG_OPT_CSTEVAL(proj, c1);
4103 sl = get_Shr_left(left);
4104 blk = get_nodes_block(n);
4105 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4106 tv = tarval_shl(tv, tv1);
4108 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4113 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4115 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4118 c1 = get_Shrs_right(left);
4120 tarval *tv1 = get_Const_tarval(c1);
4121 ir_mode *mode = get_irn_mode(left);
4122 tarval *minus1 = get_mode_all_one(mode);
4123 tarval *amask = tarval_shl(minus1, tv1);
4124 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4127 cond = tarval_sub(cond, tv1);
4128 cond = tarval_shrs(tv, cond);
4130 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4131 /* condition not met */
4132 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4133 c1 = new_Const(mode_b, tv);
4134 DBG_OPT_CSTEVAL(proj, c1);
4137 sl = get_Shrs_left(left);
4138 blk = get_nodes_block(n);
4139 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4140 tv = tarval_shl(tv, tv1);
4142 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4147 } /* tarval != bad */
4150 if (changed & 2) /* need a new Const */
4151 right = new_Const(mode, tv);
4153 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4154 ir_node *op = get_Proj_pred(left);
4156 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4157 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4158 ir_node *c = get_binop_right(op);
4161 tarval *tv = get_Const_tarval(c);
4163 if (tarval_is_single_bit(tv)) {
4164 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4165 ir_node *v = get_binop_left(op);
4166 ir_node *blk = get_irn_n(op, -1);
4167 ir_mode *mode = get_irn_mode(v);
4169 tv = tarval_sub(tv, get_mode_one(mode));
4170 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4172 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4179 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4181 /* create a new compare */
4182 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4183 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4187 } /* transform_node_Proj_Cmp */
4190 * Does all optimizations on nodes that must be done on it's Proj's
4191 * because of creating new nodes.
4193 static ir_node *transform_node_Proj(ir_node *proj) {
4194 ir_node *n = get_Proj_pred(proj);
4196 switch (get_irn_opcode(n)) {
4198 return transform_node_Proj_Div(proj);
4201 return transform_node_Proj_Mod(proj);
4204 return transform_node_Proj_DivMod(proj);
4207 return transform_node_Proj_Cond(proj);
4210 return transform_node_Proj_Cmp(proj);
4213 /* should not happen, but if it does will be optimized away */
4214 return equivalent_node_Proj(proj);
4220 } /* transform_node_Proj */
4223 * Move Confirms down through Phi nodes.
4225 static ir_node *transform_node_Phi(ir_node *phi) {
4227 ir_mode *mode = get_irn_mode(phi);
4229 if (mode_is_reference(mode)) {
4230 n = get_irn_arity(phi);
4232 /* Beware of Phi0 */
4234 ir_node *pred = get_irn_n(phi, 0);
4235 ir_node *bound, *new_Phi, *block, **in;
4238 if (! is_Confirm(pred))
4241 bound = get_Confirm_bound(pred);
4242 pnc = get_Confirm_cmp(pred);
4244 NEW_ARR_A(ir_node *, in, n);
4245 in[0] = get_Confirm_value(pred);
4247 for (i = 1; i < n; ++i) {
4248 pred = get_irn_n(phi, i);
4250 if (! is_Confirm(pred) ||
4251 get_Confirm_bound(pred) != bound ||
4252 get_Confirm_cmp(pred) != pnc)
4254 in[i] = get_Confirm_value(pred);
4256 /* move the Confirm nodes "behind" the Phi */
4257 block = get_irn_n(phi, -1);
4258 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4259 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4263 } /* transform_node_Phi */
4266 * Returns the operands of a commutative bin-op, if one operand is
4267 * a const, it is returned as the second one.
4269 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4270 ir_node *op_a = get_binop_left(binop);
4271 ir_node *op_b = get_binop_right(binop);
4273 assert(is_op_commutative(get_irn_op(binop)));
4275 if (is_Const(op_a)) {
4282 } /* get_comm_Binop_Ops */
4285 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4286 * Such pattern may arise in bitfield stores.
4288 * value c4 value c4 & c2
4289 * AND c3 AND c1 | c3
4296 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4299 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4302 ir_node *and_l, *c3;
4303 ir_node *value, *c4;
4304 ir_node *new_and, *new_const, *block;
4305 ir_mode *mode = get_irn_mode(or);
4307 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4310 get_comm_Binop_Ops(or, &and, &c1);
4311 if (!is_Const(c1) || !is_And(and))
4314 get_comm_Binop_Ops(and, &or_l, &c2);
4318 tv1 = get_Const_tarval(c1);
4319 tv2 = get_Const_tarval(c2);
4321 tv = tarval_or(tv1, tv2);
4322 if (tarval_is_all_one(tv)) {
4323 /* the AND does NOT clear a bit with isn't set by the OR */
4324 set_Or_left(or, or_l);
4325 set_Or_right(or, c1);
4327 /* check for more */
4334 get_comm_Binop_Ops(or_l, &and_l, &c3);
4335 if (!is_Const(c3) || !is_And(and_l))
4338 get_comm_Binop_Ops(and_l, &value, &c4);
4342 /* ok, found the pattern, check for conditions */
4343 assert(mode == get_irn_mode(and));
4344 assert(mode == get_irn_mode(or_l));
4345 assert(mode == get_irn_mode(and_l));
4347 tv3 = get_Const_tarval(c3);
4348 tv4 = get_Const_tarval(c4);
4350 tv = tarval_or(tv4, tv2);
4351 if (!tarval_is_all_one(tv)) {
4352 /* have at least one 0 at the same bit position */
4356 n_tv4 = tarval_not(tv4);
4357 if (tv3 != tarval_and(tv3, n_tv4)) {
4358 /* bit in the or_mask is outside the and_mask */
4362 n_tv2 = tarval_not(tv2);
4363 if (tv1 != tarval_and(tv1, n_tv2)) {
4364 /* bit in the or_mask is outside the and_mask */
4368 /* ok, all conditions met */
4369 block = get_irn_n(or, -1);
4371 new_and = new_r_And(current_ir_graph, block,
4372 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4374 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4376 set_Or_left(or, new_and);
4377 set_Or_right(or, new_const);
4379 /* check for more */
4381 } /* transform_node_Or_bf_store */
4384 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4386 static ir_node *transform_node_Or_Rot(ir_node *or) {
4387 ir_mode *mode = get_irn_mode(or);
4388 ir_node *shl, *shr, *block;
4389 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4392 if (! mode_is_int(mode))
4395 shl = get_binop_left(or);
4396 shr = get_binop_right(or);
4405 } else if (!is_Shl(shl)) {
4407 } else if (!is_Shr(shr)) {
4410 x = get_Shl_left(shl);
4411 if (x != get_Shr_left(shr))
4414 c1 = get_Shl_right(shl);
4415 c2 = get_Shr_right(shr);
4416 if (is_Const(c1) && is_Const(c2)) {
4417 tv1 = get_Const_tarval(c1);
4418 if (! tarval_is_long(tv1))
4421 tv2 = get_Const_tarval(c2);
4422 if (! tarval_is_long(tv2))
4425 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4426 != get_mode_size_bits(mode))
4429 /* yet, condition met */
4430 block = get_irn_n(or, -1);
4432 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4434 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4436 } else if (is_Sub(c1)) {
4440 if (get_Sub_right(sub) != v)
4443 c1 = get_Sub_left(sub);
4447 tv1 = get_Const_tarval(c1);
4448 if (! tarval_is_long(tv1))
4451 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4454 /* yet, condition met */
4455 block = get_nodes_block(or);
4457 /* a Rot right is not supported, so use a rot left */
4458 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4460 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4462 } else if (is_Sub(c2)) {
4466 c1 = get_Sub_left(sub);
4470 tv1 = get_Const_tarval(c1);
4471 if (! tarval_is_long(tv1))
4474 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4477 /* yet, condition met */
4478 block = get_irn_n(or, -1);
4481 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4483 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4488 } /* transform_node_Or_Rot */
4493 static ir_node *transform_node_Or(ir_node *n) {
4494 ir_node *c, *oldn = n;
4495 ir_node *a = get_Or_left(n);
4496 ir_node *b = get_Or_right(n);
4499 if (is_Not(a) && is_Not(b)) {
4500 /* ~a | ~b = ~(a&b) */
4501 ir_node *block = get_nodes_block(n);
4503 mode = get_irn_mode(n);
4506 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4507 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4512 /* we can evaluate 2 Projs of the same Cmp */
4513 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4514 ir_node *pred_a = get_Proj_pred(a);
4515 ir_node *pred_b = get_Proj_pred(b);
4516 if (pred_a == pred_b) {
4517 dbg_info *dbgi = get_irn_dbg_info(n);
4518 ir_node *block = get_nodes_block(pred_a);
4519 pn_Cmp pn_a = get_Proj_proj(a);
4520 pn_Cmp pn_b = get_Proj_proj(b);
4521 /* yes, we can simply calculate with pncs */
4522 pn_Cmp new_pnc = pn_a | pn_b;
4524 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4529 mode = get_irn_mode(n);
4530 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4532 n = transform_node_Or_bf_store(n);
4533 n = transform_node_Or_Rot(n);
4537 n = transform_bitwise_distributive(n, transform_node_Or);
4540 } /* transform_node_Or */
4544 static ir_node *transform_node(ir_node *n);
4547 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4549 * Should be moved to reassociation?
4551 static ir_node *transform_node_shift(ir_node *n) {
4552 ir_node *left, *right;
4553 tarval *tv1, *tv2, *res;
4555 int modulo_shf, flag;
4557 left = get_binop_left(n);
4559 /* different operations */
4560 if (get_irn_op(left) != get_irn_op(n))
4563 right = get_binop_right(n);
4564 tv1 = value_of(right);
4565 if (tv1 == tarval_bad)
4568 tv2 = value_of(get_binop_right(left));
4569 if (tv2 == tarval_bad)
4572 res = tarval_add(tv1, tv2);
4574 /* beware: a simple replacement works only, if res < modulo shift */
4575 mode = get_irn_mode(n);
4579 modulo_shf = get_mode_modulo_shift(mode);
4580 if (modulo_shf > 0) {
4581 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4583 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4589 /* ok, we can replace it */
4590 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4592 in[0] = get_binop_left(left);
4593 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4595 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4597 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4599 return transform_node(irn);
4602 } /* transform_node_shift */
4607 static ir_node *transform_node_Shr(ir_node *n) {
4608 ir_node *c, *oldn = n;
4609 ir_node *a = get_Shr_left(n);
4610 ir_node *b = get_Shr_right(n);
4611 ir_mode *mode = get_irn_mode(n);
4613 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4614 return transform_node_shift(n);
4615 } /* transform_node_Shr */
4620 static ir_node *transform_node_Shrs(ir_node *n) {
4621 ir_node *c, *oldn = n;
4622 ir_node *a = get_Shrs_left(n);
4623 ir_node *b = get_Shrs_right(n);
4624 ir_mode *mode = get_irn_mode(n);
4626 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4627 return transform_node_shift(n);
4628 } /* transform_node_Shrs */
4633 static ir_node *transform_node_Shl(ir_node *n) {
4634 ir_node *c, *oldn = n;
4635 ir_node *a = get_Shl_left(n);
4636 ir_node *b = get_Shl_right(n);
4637 ir_mode *mode = get_irn_mode(n);
4639 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4640 return transform_node_shift(n);
4641 } /* transform_node_Shl */
4646 static ir_node *transform_node_Rot(ir_node *n) {
4647 ir_node *c, *oldn = n;
4648 ir_node *a = get_Rot_left(n);
4649 ir_node *b = get_Rot_right(n);
4650 ir_mode *mode = get_irn_mode(n);
4652 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4653 return transform_node_shift(n);
4654 } /* transform_node_Rot */
4659 static ir_node *transform_node_Conv(ir_node *n) {
4660 ir_node *c, *oldn = n;
4661 ir_node *a = get_Conv_op(n);
4663 if (is_const_Phi(a)) {
4664 c = apply_conv_on_phi(a, get_irn_mode(n));
4666 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4671 } /* transform_node_Conv */
4674 * Remove dead blocks and nodes in dead blocks
4675 * in keep alive list. We do not generate a new End node.
4677 static ir_node *transform_node_End(ir_node *n) {
4678 int i, j, n_keepalives = get_End_n_keepalives(n);
4681 NEW_ARR_A(ir_node *, in, n_keepalives);
4683 for (i = j = 0; i < n_keepalives; ++i) {
4684 ir_node *ka = get_End_keepalive(n, i);
4686 if (! is_Block_dead(ka)) {
4690 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4693 /* FIXME: beabi need to keep a Proj(M) */
4694 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4697 if (j != n_keepalives)
4698 set_End_keepalives(n, j, in);
4700 } /* transform_node_End */
4702 /** returns 1 if a == -b */
4703 static int is_negated_value(ir_node *a, ir_node *b) {
4704 if(is_Minus(a) && get_Minus_op(a) == b)
4706 if(is_Minus(b) && get_Minus_op(b) == a)
4708 if(is_Sub(a) && is_Sub(b)) {
4709 ir_node *a_left = get_Sub_left(a);
4710 ir_node *a_right = get_Sub_right(a);
4711 ir_node *b_left = get_Sub_left(b);
4712 ir_node *b_right = get_Sub_right(b);
4714 if(a_left == b_right && a_right == b_left)
4722 * Optimize a Mux into some simpler cases.
4724 static ir_node *transform_node_Mux(ir_node *n) {
4725 ir_node *oldn = n, *sel = get_Mux_sel(n);
4726 ir_mode *mode = get_irn_mode(n);
4728 if (mode == mode_b) {
4729 ir_node *t = get_Mux_true(n);
4730 ir_node *f = get_Mux_false(n);
4731 dbg_info *dbg = get_irn_dbg_info(n);
4732 ir_node *block = get_irn_n(n, -1);
4733 ir_graph *irg = current_ir_graph;
4736 tarval *tv_t = get_Const_tarval(t);
4737 if (tv_t == tarval_b_true) {
4739 assert(get_Const_tarval(f) == tarval_b_false);
4742 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4745 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4746 assert(tv_t == tarval_b_false);
4748 assert(get_Const_tarval(f) == tarval_b_true);
4751 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4754 } else if (is_Const(f)) {
4755 tarval *tv_f = get_Const_tarval(f);
4756 if (tv_f == tarval_b_true) {
4757 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4758 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4760 assert(tv_f == tarval_b_false);
4761 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4766 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4767 ir_node *cmp = get_Proj_pred(sel);
4768 long pn = get_Proj_proj(sel);
4769 ir_node *f = get_Mux_false(n);
4770 ir_node *t = get_Mux_true(n);
4773 * Note: normalization puts the constant on the right side,
4774 * so we check only one case.
4776 * Note further that these optimization work even for floating point
4777 * with NaN's because -NaN == NaN.
4778 * However, if +0 and -0 is handled differently, we cannot use the first
4782 ir_node *cmp_r = get_Cmp_right(cmp);
4783 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4784 ir_node *block = get_irn_n(n, -1);
4786 if(is_negated_value(f, t)) {
4787 ir_node *cmp_left = get_Cmp_left(cmp);
4789 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4790 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4791 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4793 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4795 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4797 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4798 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4799 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4801 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4803 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4805 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4812 return arch_transform_node_Mux(n);
4813 } /* transform_node_Mux */
4816 * Optimize a Psi into some simpler cases.
4818 static ir_node *transform_node_Psi(ir_node *n) {
4820 return transform_node_Mux(n);
4823 } /* transform_node_Psi */
4826 * optimize sync nodes that have other syncs as input we simply add the inputs
4827 * of the other sync to our own inputs
4829 static ir_node *transform_node_Sync(ir_node *n) {
4832 arity = get_irn_arity(n);
4833 for(i = 0; i < get_irn_arity(n); /*empty*/) {
4835 ir_node *in = get_irn_n(n, i);
4841 /* set sync input 0 instead of the sync */
4842 set_irn_n(n, i, get_irn_n(in, 0));
4843 /* so we check this input again for syncs */
4845 /* append all other inputs of the sync to our sync */
4846 arity2 = get_irn_arity(in);
4847 for(i2 = 1; i2 < arity2; ++i2) {
4848 ir_node *in_in = get_irn_n(in, i2);
4849 add_irn_n(n, in_in);
4850 /* increase arity so we also check the new inputs for syncs */
4855 /* rehash the sync node */
4856 add_identities(current_ir_graph->value_table, n);
4862 * Tries several [inplace] [optimizing] transformations and returns an
4863 * equivalent node. The difference to equivalent_node() is that these
4864 * transformations _do_ generate new nodes, and thus the old node must
4865 * not be freed even if the equivalent node isn't the old one.
4867 static ir_node *transform_node(ir_node *n) {
4871 * Transform_node is the only "optimizing transformation" that might
4872 * return a node with a different opcode. We iterate HERE until fixpoint
4873 * to get the final result.
4877 if (n->op->ops.transform_node)
4878 n = n->op->ops.transform_node(n);
4879 } while (oldn != n);
4882 } /* transform_node */
4885 * Sets the default transform node operation for an ir_op_ops.
4887 * @param code the opcode for the default operation
4888 * @param ops the operations initialized
4893 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4897 ops->transform_node = transform_node_##a; \
4934 } /* firm_set_default_transform_node */
4937 /* **************** Common Subexpression Elimination **************** */
4939 /** The size of the hash table used, should estimate the number of nodes
4941 #define N_IR_NODES 512
4943 /** Compares the attributes of two Const nodes. */
4944 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4945 return (get_Const_tarval(a) != get_Const_tarval(b))
4946 || (get_Const_type(a) != get_Const_type(b));
4947 } /* node_cmp_attr_Const */
4949 /** Compares the attributes of two Proj nodes. */
4950 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4951 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4952 } /* node_cmp_attr_Proj */
4954 /** Compares the attributes of two Filter nodes. */
4955 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4956 return get_Filter_proj(a) != get_Filter_proj(b);
4957 } /* node_cmp_attr_Filter */
4959 /** Compares the attributes of two Alloc nodes. */
4960 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4961 const alloc_attr *pa = get_irn_alloc_attr(a);
4962 const alloc_attr *pb = get_irn_alloc_attr(b);
4963 return (pa->where != pb->where) || (pa->type != pb->type);
4964 } /* node_cmp_attr_Alloc */
4966 /** Compares the attributes of two Free nodes. */
4967 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4968 const free_attr *pa = get_irn_free_attr(a);
4969 const free_attr *pb = get_irn_free_attr(b);
4970 return (pa->where != pb->where) || (pa->type != pb->type);
4971 } /* node_cmp_attr_Free */
4973 /** Compares the attributes of two SymConst nodes. */
4974 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4975 const symconst_attr *pa = get_irn_symconst_attr(a);
4976 const symconst_attr *pb = get_irn_symconst_attr(b);
4977 return (pa->num != pb->num)
4978 || (pa->sym.type_p != pb->sym.type_p)
4979 || (pa->tp != pb->tp);
4980 } /* node_cmp_attr_SymConst */
4982 /** Compares the attributes of two Call nodes. */
4983 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4984 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4985 } /* node_cmp_attr_Call */
4987 /** Compares the attributes of two Sel nodes. */
4988 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4989 const ir_entity *a_ent = get_Sel_entity(a);
4990 const ir_entity *b_ent = get_Sel_entity(b);
4992 (a_ent->kind != b_ent->kind) ||
4993 (a_ent->name != b_ent->name) ||
4994 (a_ent->owner != b_ent->owner) ||
4995 (a_ent->ld_name != b_ent->ld_name) ||
4996 (a_ent->type != b_ent->type);
4997 } /* node_cmp_attr_Sel */
4999 /** Compares the attributes of two Phi nodes. */
5000 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5001 /* we can only enter this function if both nodes have the same number of inputs,
5002 hence it is enough to check if one of them is a Phi0 */
5004 /* check the Phi0 attribute */
5005 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
5008 } /* node_cmp_attr_Phi */
5010 /** Compares the attributes of two Conv nodes. */
5011 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5012 return get_Conv_strict(a) != get_Conv_strict(b);
5013 } /* node_cmp_attr_Conv */
5015 /** Compares the attributes of two Cast nodes. */
5016 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5017 return get_Cast_type(a) != get_Cast_type(b);
5018 } /* node_cmp_attr_Cast */
5020 /** Compares the attributes of two Load nodes. */
5021 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5022 if (get_Load_volatility(a) == volatility_is_volatile ||
5023 get_Load_volatility(b) == volatility_is_volatile)
5024 /* NEVER do CSE on volatile Loads */
5026 /* do not CSE Loads with different alignment. Be conservative. */
5027 if (get_Load_align(a) != get_Load_align(b))
5030 return get_Load_mode(a) != get_Load_mode(b);
5031 } /* node_cmp_attr_Load */
5033 /** Compares the attributes of two Store nodes. */
5034 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5035 /* do not CSE Stores with different alignment. Be conservative. */
5036 if (get_Store_align(a) != get_Store_align(b))
5039 /* NEVER do CSE on volatile Stores */
5040 return (get_Store_volatility(a) == volatility_is_volatile ||
5041 get_Store_volatility(b) == volatility_is_volatile);
5042 } /* node_cmp_attr_Store */
5044 /** Compares the attributes of two Confirm nodes. */
5045 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5046 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5047 } /* node_cmp_attr_Confirm */
5049 /** Compares the attributes of two ASM nodes. */
5050 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5052 const ir_asm_constraint *ca;
5053 const ir_asm_constraint *cb;
5056 if (get_ASM_text(a) != get_ASM_text(b))
5059 /* Should we really check the constraints here? Should be better, but is strange. */
5060 n = get_ASM_n_input_constraints(a);
5061 if (n != get_ASM_n_input_constraints(b))
5064 ca = get_ASM_input_constraints(a);
5065 cb = get_ASM_input_constraints(b);
5066 for (i = 0; i < n; ++i) {
5067 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5071 n = get_ASM_n_output_constraints(a);
5072 if (n != get_ASM_n_output_constraints(b))
5075 ca = get_ASM_output_constraints(a);
5076 cb = get_ASM_output_constraints(b);
5077 for (i = 0; i < n; ++i) {
5078 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5082 n = get_ASM_n_clobbers(a);
5083 if (n != get_ASM_n_clobbers(b))
5086 cla = get_ASM_clobbers(a);
5087 clb = get_ASM_clobbers(b);
5088 for (i = 0; i < n; ++i) {
5089 if (cla[i] != clb[i])
5093 } /* node_cmp_attr_ASM */
5096 * Set the default node attribute compare operation for an ir_op_ops.
5098 * @param code the opcode for the default operation
5099 * @param ops the operations initialized
5104 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5108 ops->node_cmp_attr = node_cmp_attr_##a; \
5133 } /* firm_set_default_node_cmp_attr */
5136 * Compare function for two nodes in the hash table. Gets two
5137 * nodes as parameters. Returns 0 if the nodes are a cse.
5139 int identities_cmp(const void *elt, const void *key) {
5146 if (a == b) return 0;
5148 if ((get_irn_op(a) != get_irn_op(b)) ||
5149 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5151 /* compare if a's in and b's in are of equal length */
5152 irn_arity_a = get_irn_intra_arity (a);
5153 if (irn_arity_a != get_irn_intra_arity(b))
5156 /* for block-local cse and op_pin_state_pinned nodes: */
5157 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
5158 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5162 /* compare a->in[0..ins] with b->in[0..ins] */
5163 for (i = 0; i < irn_arity_a; i++)
5164 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5168 * here, we already now that the nodes are identical except their
5171 if (a->op->ops.node_cmp_attr)
5172 return a->op->ops.node_cmp_attr(a, b);
5175 } /* identities_cmp */
5178 * Calculate a hash value of a node.
5180 unsigned ir_node_hash(ir_node *node) {
5184 if (node->op == op_Const) {
5185 /* special value for const, as they only differ in their tarval. */
5186 h = HASH_PTR(node->attr.con.tv);
5187 h = 9*h + HASH_PTR(get_irn_mode(node));
5188 } else if (node->op == op_SymConst) {
5189 /* special value for const, as they only differ in their symbol. */
5190 h = HASH_PTR(node->attr.symc.sym.type_p);
5191 h = 9*h + HASH_PTR(get_irn_mode(node));
5194 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5195 h = irn_arity = get_irn_intra_arity(node);
5197 /* consider all in nodes... except the block if not a control flow. */
5198 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5199 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5203 h = 9*h + HASH_PTR(get_irn_mode(node));
5205 h = 9*h + HASH_PTR(get_irn_op(node));
5209 } /* ir_node_hash */
5211 pset *new_identities(void) {
5212 return new_pset(identities_cmp, N_IR_NODES);
5213 } /* new_identities */
5215 void del_identities(pset *value_table) {
5216 del_pset(value_table);
5217 } /* del_identities */
5220 * Normalize a node by putting constants (and operands with larger
5221 * node index) on the right
5223 * @param n The node to normalize
5225 static void normalize_node(ir_node *n) {
5226 if (get_opt_reassociation()) {
5227 if (is_op_commutative(get_irn_op(n))) {
5228 ir_node *l = get_binop_left(n);
5229 ir_node *r = get_binop_right(n);
5231 /* For commutative operators perform a OP b == b OP a but keep
5232 * constants on the RIGHT side. This helps greatly in some
5233 * optimizations. Moreover we use the idx number to make the form
5235 if (!operands_are_normalized(l, r)) {
5236 set_binop_left(n, r);
5237 set_binop_right(n, l);
5241 } /* normalize_node */
5244 * Return the canonical node computing the same value as n.
5246 * @param value_table The value table
5247 * @param n The node to lookup
5249 * Looks up the node in a hash table.
5251 * For Const nodes this is performed in the constructor, too. Const
5252 * nodes are extremely time critical because of their frequent use in
5253 * constant string arrays.
5255 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5258 if (!value_table) return n;
5262 o = pset_find(value_table, n, ir_node_hash(n));
5271 * During construction we set the op_pin_state_pinned flag in the graph right when the
5272 * optimization is performed. The flag turning on procedure global cse could
5273 * be changed between two allocations. This way we are safe.
5275 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5278 n = identify(value_table, n);
5279 if (get_irn_n(old, -1) != get_irn_n(n, -1))
5280 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5282 } /* identify_cons */
5285 * Return the canonical node computing the same value as n.
5286 * Looks up the node in a hash table, enters it in the table
5287 * if it isn't there yet.
5289 ir_node *identify_remember(pset *value_table, ir_node *n) {
5292 if (!value_table) return n;
5295 /* lookup or insert in hash table with given hash key. */
5296 o = pset_insert(value_table, n, ir_node_hash(n));
5303 } /* identify_remember */
5305 /* Add a node to the identities value table. */
5306 void add_identities(pset *value_table, ir_node *node) {
5307 if (get_opt_cse() && is_no_Block(node))
5308 identify_remember(value_table, node);
5309 } /* add_identities */
5311 /* Visit each node in the value table of a graph. */
5312 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5314 ir_graph *rem = current_ir_graph;
5316 current_ir_graph = irg;
5317 foreach_pset(irg->value_table, node)
5319 current_ir_graph = rem;
5320 } /* visit_all_identities */
5323 * Garbage in, garbage out. If a node has a dead input, i.e., the
5324 * Bad node is input to the node, return the Bad node.
5326 static ir_node *gigo(ir_node *node) {
5328 ir_op *op = get_irn_op(node);
5330 /* remove garbage blocks by looking at control flow that leaves the block
5331 and replacing the control flow by Bad. */
5332 if (get_irn_mode(node) == mode_X) {
5333 ir_node *block = get_nodes_block(skip_Proj(node));
5335 /* Don't optimize nodes in immature blocks. */
5336 if (!get_Block_matured(block)) return node;
5337 /* Don't optimize End, may have Bads. */
5338 if (op == op_End) return node;
5340 if (is_Block(block)) {
5341 irn_arity = get_irn_arity(block);
5342 for (i = 0; i < irn_arity; i++) {
5343 if (!is_Bad(get_irn_n(block, i)))
5346 if (i == irn_arity) {
5347 ir_graph *irg = get_irn_irg(block);
5348 /* the start block is never dead */
5349 if (block != get_irg_start_block(irg)
5350 && block != get_irg_end_block(irg))
5356 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5357 blocks predecessors is dead. */
5358 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5359 irn_arity = get_irn_arity(node);
5362 * Beware: we can only read the block of a non-floating node.
5364 if (is_irn_pinned_in_irg(node) &&
5365 is_Block_dead(get_nodes_block(node)))
5368 for (i = 0; i < irn_arity; i++) {
5369 ir_node *pred = get_irn_n(node, i);
5374 /* Propagating Unknowns here seems to be a bad idea, because
5375 sometimes we need a node as a input and did not want that
5377 However, it might be useful to move this into a later phase
5378 (if you think that optimizing such code is useful). */
5379 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5380 return new_Unknown(get_irn_mode(node));
5385 /* With this code we violate the agreement that local_optimize
5386 only leaves Bads in Block, Phi and Tuple nodes. */
5387 /* If Block has only Bads as predecessors it's garbage. */
5388 /* If Phi has only Bads as predecessors it's garbage. */
5389 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5390 irn_arity = get_irn_arity(node);
5391 for (i = 0; i < irn_arity; i++) {
5392 if (!is_Bad(get_irn_n(node, i))) break;
5394 if (i == irn_arity) node = new_Bad();
5401 * These optimizations deallocate nodes from the obstack.
5402 * It can only be called if it is guaranteed that no other nodes
5403 * reference this one, i.e., right after construction of a node.
5405 * @param n The node to optimize
5407 * current_ir_graph must be set to the graph of the node!
5409 ir_node *optimize_node(ir_node *n) {
5412 ir_opcode iro = get_irn_opcode(n);
5414 /* Always optimize Phi nodes: part of the construction. */
5415 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5417 /* constant expression evaluation / constant folding */
5418 if (get_opt_constant_folding()) {
5419 /* neither constants nor Tuple values can be evaluated */
5420 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5421 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5422 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5423 /* try to evaluate */
5424 tv = computed_value(n);
5425 if (tv != tarval_bad) {
5427 ir_type *old_tp = get_irn_type(n);
5428 int i, arity = get_irn_arity(n);
5432 * Try to recover the type of the new expression.
5434 for (i = 0; i < arity && !old_tp; ++i)
5435 old_tp = get_irn_type(get_irn_n(n, i));
5438 * we MUST copy the node here temporary, because it's still needed
5439 * for DBG_OPT_CSTEVAL
5441 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5442 oldn = alloca(node_size);
5444 memcpy(oldn, n, node_size);
5445 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5447 /* ARG, copy the in array, we need it for statistics */
5448 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5450 /* note the inplace edges module */
5451 edges_node_deleted(n, current_ir_graph);
5453 /* evaluation was successful -- replace the node. */
5454 irg_kill_node(current_ir_graph, n);
5455 nw = new_Const(get_tarval_mode(tv), tv);
5457 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5458 set_Const_type(nw, old_tp);
5459 DBG_OPT_CSTEVAL(oldn, nw);
5460 tarval_enable_fp_ops(old_fp_mode);
5463 tarval_enable_fp_ops(old_fp_mode);
5467 /* remove unnecessary nodes */
5468 if (get_opt_constant_folding() ||
5469 (iro == iro_Phi) || /* always optimize these nodes. */
5471 (iro == iro_Proj) ||
5472 (iro == iro_Block) ) /* Flags tested local. */
5473 n = equivalent_node(n);
5475 /* Common Subexpression Elimination.
5477 * Checks whether n is already available.
5478 * The block input is used to distinguish different subexpressions. Right
5479 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5480 * subexpressions within a block.
5483 n = identify_cons(current_ir_graph->value_table, n);
5486 edges_node_deleted(oldn, current_ir_graph);
5488 /* We found an existing, better node, so we can deallocate the old node. */
5489 irg_kill_node(current_ir_graph, oldn);
5493 /* Some more constant expression evaluation that does not allow to
5495 iro = get_irn_opcode(n);
5496 if (get_opt_constant_folding() ||
5497 (iro == iro_Cond) ||
5498 (iro == iro_Proj)) /* Flags tested local. */
5499 n = transform_node(n);
5501 /* Remove nodes with dead (Bad) input.
5502 Run always for transformation induced Bads. */
5505 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5506 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5507 n = identify_remember(current_ir_graph->value_table, n);
5511 } /* optimize_node */
5515 * These optimizations never deallocate nodes (in place). This can cause dead
5516 * nodes lying on the obstack. Remove these by a dead node elimination,
5517 * i.e., a copying garbage collection.
5519 ir_node *optimize_in_place_2(ir_node *n) {
5522 ir_opcode iro = get_irn_opcode(n);
5524 if (!get_opt_optimize() && !is_Phi(n)) return n;
5526 /* constant expression evaluation / constant folding */
5527 if (get_opt_constant_folding()) {
5528 /* neither constants nor Tuple values can be evaluated */
5529 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5530 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5531 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5532 /* try to evaluate */
5533 tv = computed_value(n);
5534 if (tv != tarval_bad) {
5535 /* evaluation was successful -- replace the node. */
5536 ir_type *old_tp = get_irn_type(n);
5537 int i, arity = get_irn_arity(n);
5540 * Try to recover the type of the new expression.
5542 for (i = 0; i < arity && !old_tp; ++i)
5543 old_tp = get_irn_type(get_irn_n(n, i));
5545 n = new_Const(get_tarval_mode(tv), tv);
5547 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5548 set_Const_type(n, old_tp);
5550 DBG_OPT_CSTEVAL(oldn, n);
5551 tarval_enable_fp_ops(old_fp_mode);
5554 tarval_enable_fp_ops(old_fp_mode);
5558 /* remove unnecessary nodes */
5559 if (get_opt_constant_folding() ||
5560 (iro == iro_Phi) || /* always optimize these nodes. */
5561 (iro == iro_Id) || /* ... */
5562 (iro == iro_Proj) || /* ... */
5563 (iro == iro_Block) ) /* Flags tested local. */
5564 n = equivalent_node(n);
5566 /** common subexpression elimination **/
5567 /* Checks whether n is already available. */
5568 /* The block input is used to distinguish different subexpressions. Right
5569 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5570 subexpressions within a block. */
5571 if (get_opt_cse()) {
5572 n = identify(current_ir_graph->value_table, n);
5575 /* Some more constant expression evaluation. */
5576 iro = get_irn_opcode(n);
5577 if (get_opt_constant_folding() ||
5578 (iro == iro_Cond) ||
5579 (iro == iro_Proj)) /* Flags tested local. */
5580 n = transform_node(n);
5582 /* Remove nodes with dead (Bad) input.
5583 Run always for transformation induced Bads. */
5586 /* Now we can verify the node, as it has no dead inputs any more. */
5589 /* Now we have a legal, useful node. Enter it in hash table for cse.
5590 Blocks should be unique anyways. (Except the successor of start:
5591 is cse with the start block!) */
5592 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5593 n = identify_remember(current_ir_graph->value_table, n);
5596 } /* optimize_in_place_2 */
5599 * Wrapper for external use, set proper status bits after optimization.
5601 ir_node *optimize_in_place(ir_node *n) {
5602 /* Handle graph state */
5603 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5605 if (get_opt_global_cse())
5606 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5607 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5608 set_irg_outs_inconsistent(current_ir_graph);
5610 /* FIXME: Maybe we could also test whether optimizing the node can
5611 change the control graph. */
5612 set_irg_doms_inconsistent(current_ir_graph);
5613 return optimize_in_place_2(n);
5614 } /* optimize_in_place */
5617 * Sets the default operation for an ir_ops.
5619 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5620 ops = firm_set_default_computed_value(code, ops);
5621 ops = firm_set_default_equivalent_node(code, ops);
5622 ops = firm_set_default_transform_node(code, ops);
5623 ops = firm_set_default_node_cmp_attr(code, ops);
5624 ops = firm_set_default_get_type(code, ops);
5625 ops = firm_set_default_get_type_attr(code, ops);
5626 ops = firm_set_default_get_entity_attr(code, ops);
5629 } /* firm_set_default_operations */