2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Return the value of a Constant.
59 static tarval *computed_value_Const(ir_node *n) {
60 return get_Const_tarval(n);
61 } /* computed_value_Const */
64 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
66 static tarval *computed_value_SymConst(ir_node *n) {
70 switch (get_SymConst_kind(n)) {
71 case symconst_type_size:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
76 case symconst_type_align:
77 type = get_SymConst_type(n);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
81 case symconst_ofs_ent:
82 ent = get_SymConst_entity(n);
83 type = get_entity_owner(ent);
84 if (get_type_state(type) == layout_fixed)
85 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
91 } /* computed_value_SymConst */
94 * Return the value of an Add.
96 static tarval *computed_value_Add(ir_node *n) {
97 ir_node *a = get_Add_left(n);
98 ir_node *b = get_Add_right(n);
100 tarval *ta = value_of(a);
101 tarval *tb = value_of(b);
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_add(ta, tb);
107 } /* computed_value_Add */
110 * Return the value of a Sub.
111 * Special case: a - a
113 static tarval *computed_value_Sub(ir_node *n) {
114 ir_node *a = get_Sub_left(n);
115 ir_node *b = get_Sub_right(n);
120 if (a == b && !is_Bad(a))
121 return get_mode_null(get_irn_mode(n));
126 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127 return tarval_sub(ta, tb);
130 } /* computed_value_Sub */
133 * Return the value of a Carry.
134 * Special : a op 0, 0 op b
136 static tarval *computed_value_Carry(ir_node *n) {
137 ir_node *a = get_binop_left(n);
138 ir_node *b = get_binop_right(n);
139 ir_mode *m = get_irn_mode(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad)) {
146 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
148 if (tarval_is_null(ta) || tarval_is_null(tb))
149 return get_mode_null(m);
152 } /* computed_value_Carry */
155 * Return the value of a Borrow.
158 static tarval *computed_value_Borrow(ir_node *n) {
159 ir_node *a = get_binop_left(n);
160 ir_node *b = get_binop_right(n);
161 ir_mode *m = get_irn_mode(n);
163 tarval *ta = value_of(a);
164 tarval *tb = value_of(b);
166 if ((ta != tarval_bad) && (tb != tarval_bad)) {
167 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
168 } else if (tarval_is_null(ta)) {
169 return get_mode_null(m);
172 } /* computed_value_Borrow */
175 * Return the value of an unary Minus.
177 static tarval *computed_value_Minus(ir_node *n) {
178 ir_node *a = get_Minus_op(n);
179 tarval *ta = value_of(a);
181 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
182 return tarval_neg(ta);
185 } /* computed_value_Minus */
188 * Return the value of a Mul.
190 static tarval *computed_value_Mul(ir_node *n) {
191 ir_node *a = get_Mul_left(n);
192 ir_node *b = get_Mul_right(n);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 mode = get_irn_mode(n);
199 if (mode != get_irn_mode(a)) {
200 /* n * n = 2n bit multiplication */
201 ta = tarval_convert_to(ta, mode);
202 tb = tarval_convert_to(tb, mode);
205 if (ta != tarval_bad && tb != tarval_bad) {
206 return tarval_mul(ta, tb);
208 /* a*0 = 0 or 0*b = 0 */
209 if (ta == get_mode_null(mode))
211 if (tb == get_mode_null(mode))
215 } /* computed_value_Mul */
218 * Return the value of a floating point Quot.
220 static tarval *computed_value_Quot(ir_node *n) {
221 ir_node *a = get_Quot_left(n);
222 ir_node *b = get_Quot_right(n);
224 tarval *ta = value_of(a);
225 tarval *tb = value_of(b);
227 /* This was missing in original implementation. Why? */
228 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
229 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
230 return tarval_quo(ta, tb);
233 } /* computed_value_Quot */
236 * Calculate the value of an integer Div of two nodes.
237 * Special case: 0 / b
239 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
240 tarval *ta = value_of(a);
241 tarval *tb = value_of(b);
243 /* Compute c1 / c2 or 0 / a, a != 0 */
244 if (ta != tarval_bad) {
245 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
246 return tarval_div(ta, tb);
247 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
251 } /* do_computed_value_Div */
254 * Return the value of an integer Div.
256 static tarval *computed_value_Div(ir_node *n) {
257 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
258 } /* computed_value_Div */
261 * Calculate the value of an integer Mod of two nodes.
262 * Special case: a % 1
264 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 /* Compute c1 % c2 or a % 1 */
269 if (tb != tarval_bad) {
270 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
271 return tarval_mod(ta, tb);
272 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
273 return get_mode_null(get_irn_mode(a));
276 } /* do_computed_value_Mod */
279 * Return the value of an integer Mod.
281 static tarval *computed_value_Mod(ir_node *n) {
282 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
283 } /* computed_value_Mod */
286 * Return the value of an Abs.
288 static tarval *computed_value_Abs(ir_node *n) {
289 ir_node *a = get_Abs_op(n);
290 tarval *ta = value_of(a);
292 if (ta != tarval_bad)
293 return tarval_abs(ta);
296 } /* computed_value_Abs */
299 * Return the value of an And.
300 * Special case: a & 0, 0 & b
302 static tarval *computed_value_And(ir_node *n) {
303 ir_node *a = get_And_left(n);
304 ir_node *b = get_And_right(n);
306 tarval *ta = value_of(a);
307 tarval *tb = value_of(b);
309 if ((ta != tarval_bad) && (tb != tarval_bad)) {
310 return tarval_and (ta, tb);
312 if (tarval_is_null(ta)) return ta;
313 if (tarval_is_null(tb)) return tb;
316 } /* computed_value_And */
319 * Return the value of an Or.
320 * Special case: a | 1...1, 1...1 | b
322 static tarval *computed_value_Or(ir_node *n) {
323 ir_node *a = get_Or_left(n);
324 ir_node *b = get_Or_right(n);
326 tarval *ta = value_of(a);
327 tarval *tb = value_of(b);
329 if ((ta != tarval_bad) && (tb != tarval_bad)) {
330 return tarval_or (ta, tb);
332 if (tarval_is_all_one(ta)) return ta;
333 if (tarval_is_all_one(tb)) return tb;
336 } /* computed_value_Or */
339 * Return the value of an Eor.
341 static tarval *computed_value_Eor(ir_node *n) {
342 ir_node *a = get_Eor_left(n);
343 ir_node *b = get_Eor_right(n);
348 return get_mode_null(get_irn_mode(n));
353 if ((ta != tarval_bad) && (tb != tarval_bad)) {
354 return tarval_eor (ta, tb);
357 } /* computed_value_Eor */
360 * Return the value of a Not.
362 static tarval *computed_value_Not(ir_node *n) {
363 ir_node *a = get_Not_op(n);
364 tarval *ta = value_of(a);
366 if (ta != tarval_bad)
367 return tarval_not(ta);
370 } /* computed_value_Not */
373 * Return the value of a Shl.
375 static tarval *computed_value_Shl(ir_node *n) {
376 ir_node *a = get_Shl_left(n);
377 ir_node *b = get_Shl_right(n);
379 tarval *ta = value_of(a);
380 tarval *tb = value_of(b);
382 if ((ta != tarval_bad) && (tb != tarval_bad)) {
383 return tarval_shl (ta, tb);
386 } /* computed_value_Shl */
389 * Return the value of a Shr.
391 static tarval *computed_value_Shr(ir_node *n) {
392 ir_node *a = get_Shr_left(n);
393 ir_node *b = get_Shr_right(n);
395 tarval *ta = value_of(a);
396 tarval *tb = value_of(b);
398 if ((ta != tarval_bad) && (tb != tarval_bad)) {
399 return tarval_shr (ta, tb);
402 } /* computed_value_Shr */
405 * Return the value of a Shrs.
407 static tarval *computed_value_Shrs(ir_node *n) {
408 ir_node *a = get_Shrs_left(n);
409 ir_node *b = get_Shrs_right(n);
411 tarval *ta = value_of(a);
412 tarval *tb = value_of(b);
414 if ((ta != tarval_bad) && (tb != tarval_bad)) {
415 return tarval_shrs (ta, tb);
418 } /* computed_value_Shrs */
421 * Return the value of a Rot.
423 static tarval *computed_value_Rot(ir_node *n) {
424 ir_node *a = get_Rot_left(n);
425 ir_node *b = get_Rot_right(n);
427 tarval *ta = value_of(a);
428 tarval *tb = value_of(b);
430 if ((ta != tarval_bad) && (tb != tarval_bad)) {
431 return tarval_rot (ta, tb);
434 } /* computed_value_Rot */
437 * Return the value of a Conv.
439 static tarval *computed_value_Conv(ir_node *n) {
440 ir_node *a = get_Conv_op(n);
441 tarval *ta = value_of(a);
443 if (ta != tarval_bad)
444 return tarval_convert_to(ta, get_irn_mode(n));
447 } /* computed_value_Conv */
450 * Return the value of a Proj(Cmp).
452 * This performs a first step of unreachable code elimination.
453 * Proj can not be computed, but folding a Cmp above the Proj here is
454 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
456 * There are several case where we can evaluate a Cmp node, see later.
458 static tarval *computed_value_Proj_Cmp(ir_node *n) {
459 ir_node *a = get_Proj_pred(n);
460 ir_node *aa = get_Cmp_left(a);
461 ir_node *ab = get_Cmp_right(a);
462 long proj_nr = get_Proj_proj(n);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
469 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
472 /* This is a trick with the bits used for encoding the Cmp
473 Proj numbers, the following statement is not the same:
474 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
478 tarval *taa = value_of(aa);
479 tarval *tab = value_of(ab);
480 ir_mode *mode = get_irn_mode(aa);
483 * The predecessors of Cmp are target values. We can evaluate
486 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487 /* strange checks... */
488 pn_Cmp flags = tarval_cmp(taa, tab);
489 if (flags != pn_Cmp_False) {
490 return new_tarval_from_long (proj_nr & flags, mode_b);
493 /* for integer values, we can check against MIN/MAX */
494 else if (mode_is_int(mode)) {
495 /* MIN <=/> x. This results in true/false. */
496 if (taa == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Le)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Gt)
501 return get_tarval_b_false();
503 /* x >=/< MIN. This results in true/false. */
505 if (tab == get_mode_min(mode)) {
506 /* a compare with the MIN value */
507 if (proj_nr == pn_Cmp_Ge)
508 return get_tarval_b_true();
509 else if (proj_nr == pn_Cmp_Lt)
510 return get_tarval_b_false();
512 /* MAX >=/< x. This results in true/false. */
513 else if (taa == get_mode_max(mode)) {
514 if (proj_nr == pn_Cmp_Ge)
515 return get_tarval_b_true();
516 else if (proj_nr == pn_Cmp_Lt)
517 return get_tarval_b_false();
519 /* x <=/> MAX. This results in true/false. */
520 else if (tab == get_mode_max(mode)) {
521 if (proj_nr == pn_Cmp_Le)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Gt)
524 return get_tarval_b_false();
528 * The predecessors are Allocs or (void*)(0) constants. Allocs never
529 * return NULL, they raise an exception. Therefore we can predict
533 ir_node *aaa = skip_Id(skip_Proj(aa));
534 ir_node *aba = skip_Id(skip_Proj(ab));
536 if ( ( (/* aa is ProjP and aaa is Alloc */
538 && mode_is_reference(get_irn_mode(aa))
540 && ( (/* ab is NULL */
542 && mode_is_reference(get_irn_mode(ab))
543 && is_Const_null(ab))
544 || (/* ab is other Alloc */
546 && mode_is_reference(get_irn_mode(ab))
549 || (/* aa is NULL and aba is Alloc */
551 && mode_is_reference(get_irn_mode(aa))
554 && mode_is_reference(get_irn_mode(ab))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 } /* computed_value_Proj_Cmp */
564 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565 * Proj(DivMod) and Proj(Quot).
567 static tarval *computed_value_Proj(ir_node *n) {
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(a);
595 if (get_Proj_proj(n) == pn_Quot_res)
596 return computed_value(a);
603 } /* computed_value_Proj */
606 * Calculate the value of a Mux: can be evaluated, if the
607 * sel and the right input are known.
609 static tarval *computed_value_Mux(ir_node *n) {
610 ir_node *sel = get_Mux_sel(n);
611 tarval *ts = value_of(sel);
613 if (ts == get_tarval_b_true()) {
614 ir_node *v = get_Mux_true(n);
617 else if (ts == get_tarval_b_false()) {
618 ir_node *v = get_Mux_false(n);
622 } /* computed_value_Mux */
625 * Calculate the value of a Psi: can be evaluated, if a condition is true
626 * and all previous conditions are false. If all conditions are false
627 * we evaluate to the default one.
629 static tarval *computed_value_Psi(ir_node *n) {
631 return computed_value_Mux(n);
633 } /* computed_value_Psi */
636 * Calculate the value of a Confirm: can be evaluated,
637 * if it has the form Confirm(x, '=', Const).
639 static tarval *computed_value_Confirm(ir_node *n) {
640 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641 value_of(get_Confirm_bound(n)) : tarval_bad;
642 } /* computed_value_Confirm */
645 * If the parameter n can be computed, return its value, else tarval_bad.
646 * Performs constant folding.
648 * @param n The node this should be evaluated
650 tarval *computed_value(ir_node *n) {
651 if (n->op->ops.computed_value)
652 return n->op->ops.computed_value(n);
654 } /* computed_value */
657 * Set the default computed_value evaluator in an ir_op_ops.
659 * @param code the opcode for the default operation
660 * @param ops the operations initialized
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
669 ops->computed_value = computed_value_##a; \
704 } /* firm_set_default_computed_value */
707 * Returns a equivalent block for another block.
708 * If the block has only one predecessor, this is
709 * the equivalent one. If the only predecessor of a block is
710 * the block itself, this is a dead block.
712 * If both predecessors of a block are the branches of a binary
713 * Cond, the equivalent block is Cond's block.
715 * If all predecessors of a block are bad or lies in a dead
716 * block, the current block is dead as well.
718 * Note, that blocks are NEVER turned into Bad's, instead
719 * the dead_block flag is set. So, never test for is_Bad(block),
720 * always use is_dead_Block(block).
722 static ir_node *equivalent_node_Block(ir_node *n)
725 int n_preds = get_Block_n_cfgpreds(n);
727 /* The Block constructor does not call optimize, but mature_immBlock
728 calls the optimization. */
729 assert(get_Block_matured(n));
731 /* Straightening: a single entry Block following a single exit Block
732 can be merged, if it is not the Start block. */
733 /* !!! Beware, all Phi-nodes of n must have been optimized away.
734 This should be true, as the block is matured before optimize is called.
735 But what about Phi-cycles with the Phi0/Id that could not be resolved?
736 Remaining Phi nodes are just Ids. */
737 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
738 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
739 if (predblock == oldn) {
740 /* Jmp jumps into the block it is in -- deal self cycle. */
741 n = set_Block_dead(n);
742 DBG_OPT_DEAD_BLOCK(oldn, n);
743 } else if (get_opt_control_flow_straightening()) {
745 DBG_OPT_STG(oldn, n);
747 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
748 ir_node *predblock = get_Block_cfgpred_block(n, 0);
749 if (predblock == oldn) {
750 /* Jmp jumps into the block it is in -- deal self cycle. */
751 n = set_Block_dead(n);
752 DBG_OPT_DEAD_BLOCK(oldn, n);
754 } else if ((n_preds == 2) &&
755 (get_opt_control_flow_weak_simplification())) {
756 /* Test whether Cond jumps twice to this block
757 * The more general case which more than 2 predecessors is handles
758 * in optimize_cf(), we handle only this special case for speed here.
760 ir_node *a = get_Block_cfgpred(n, 0);
761 ir_node *b = get_Block_cfgpred(n, 1);
765 (get_Proj_pred(a) == get_Proj_pred(b)) &&
766 is_Cond(get_Proj_pred(a)) &&
767 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
768 /* Also a single entry Block following a single exit Block. Phis have
769 twice the same operand and will be optimized away. */
770 n = get_nodes_block(get_Proj_pred(a));
771 DBG_OPT_IFSIM1(oldn, a, b, n);
773 } else if (get_opt_unreachable_code() &&
774 (n != get_irg_start_block(current_ir_graph)) &&
775 (n != get_irg_end_block(current_ir_graph)) ) {
778 /* If all inputs are dead, this block is dead too, except if it is
779 the start or end block. This is one step of unreachable code
781 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
782 ir_node *pred = get_Block_cfgpred(n, i);
785 if (is_Bad(pred)) continue;
786 pred_blk = get_nodes_block(skip_Proj(pred));
788 if (is_Block_dead(pred_blk)) continue;
791 /* really found a living input */
796 n = set_Block_dead(n);
797 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } /* equivalent_node_Block */
805 * Returns a equivalent node for a Jmp, a Bad :-)
806 * Of course this only happens if the Block of the Jmp is dead.
808 static ir_node *equivalent_node_Jmp(ir_node *n) {
809 /* unreachable code elimination */
810 if (is_Block_dead(get_nodes_block(n)))
814 } /* equivalent_node_Jmp */
816 /** Raise is handled in the same way as Jmp. */
817 #define equivalent_node_Raise equivalent_node_Jmp
820 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
821 See transform_node_Proj_Cond(). */
824 * Optimize operations that are commutative and have neutral 0,
825 * so a op 0 = 0 op a = a.
827 static ir_node *equivalent_node_neutral_zero(ir_node *n)
831 ir_node *a = get_binop_left(n);
832 ir_node *b = get_binop_right(n);
837 /* After running compute_node there is only one constant predecessor.
838 Find this predecessors value and remember the other node: */
839 if ((tv = value_of(a)) != tarval_bad) {
841 } else if ((tv = value_of(b)) != tarval_bad) {
846 /* If this predecessors constant value is zero, the operation is
847 * unnecessary. Remove it.
849 * Beware: If n is a Add, the mode of on and n might be different
850 * which happens in this rare construction: NULL + 3.
851 * Then, a Conv would be needed which we cannot include here.
853 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
860 } /* equivalent_node_neutral_zero */
863 * Eor is commutative and has neutral 0.
865 #define equivalent_node_Eor equivalent_node_neutral_zero
868 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
870 * The second one looks strange, but this construct
871 * is used heavily in the LCC sources :-).
873 * Beware: The Mode of an Add may be different than the mode of its
874 * predecessors, so we could not return a predecessors in all cases.
876 static ir_node *equivalent_node_Add(ir_node *n) {
878 ir_node *left, *right;
879 ir_mode *mode = get_irn_mode(n);
881 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
882 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
885 n = equivalent_node_neutral_zero(n);
889 left = get_Add_left(n);
890 right = get_Add_right(n);
893 if (get_Sub_right(left) == right) {
896 n = get_Sub_left(left);
897 if (mode == get_irn_mode(n)) {
898 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
904 if (get_Sub_right(right) == left) {
907 n = get_Sub_left(right);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915 } /* equivalent_node_Add */
918 * optimize operations that are not commutative but have neutral 0 on left,
921 static ir_node *equivalent_node_left_zero(ir_node *n) {
924 ir_node *a = get_binop_left(n);
925 ir_node *b = get_binop_right(n);
927 if (is_Const(b) && is_Const_null(b)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
933 } /* equivalent_node_left_zero */
935 #define equivalent_node_Shl equivalent_node_left_zero
936 #define equivalent_node_Shr equivalent_node_left_zero
937 #define equivalent_node_Shrs equivalent_node_left_zero
938 #define equivalent_node_Rot equivalent_node_left_zero
941 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
943 * The second one looks strange, but this construct
944 * is used heavily in the LCC sources :-).
946 * Beware: The Mode of a Sub may be different than the mode of its
947 * predecessors, so we could not return a predecessors in all cases.
949 static ir_node *equivalent_node_Sub(ir_node *n) {
952 ir_mode *mode = get_irn_mode(n);
954 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
955 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
958 b = get_Sub_right(n);
960 /* Beware: modes might be different */
961 if (is_Const(b) && is_Const_null(b)) {
962 ir_node *a = get_Sub_left(n);
963 if (mode == get_irn_mode(a)) {
966 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
970 } /* equivalent_node_Sub */
974 * Optimize an "idempotent unary op", ie op(op(n)) = n.
977 * -(-a) == a, but might overflow two times.
978 * We handle it anyway here but the better way would be a
979 * flag. This would be needed for Pascal for instance.
981 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
983 ir_node *pred = get_unop_op(n);
985 /* optimize symmetric unop */
986 if (get_irn_op(pred) == get_irn_op(n)) {
987 n = get_unop_op(pred);
988 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
991 } /* equivalent_node_idempotent_unop */
993 /** Optimize Not(Not(x)) == x. */
994 #define equivalent_node_Not equivalent_node_idempotent_unop
996 /** -(-x) == x ??? Is this possible or can --x raise an
997 out of bounds exception if min =! max? */
998 #define equivalent_node_Minus equivalent_node_idempotent_unop
1001 * Optimize a * 1 = 1 * a = a.
1003 static ir_node *equivalent_node_Mul(ir_node *n) {
1005 ir_node *a = get_Mul_left(n);
1007 /* we can handle here only the n * n = n bit cases */
1008 if (get_irn_mode(n) == get_irn_mode(a)) {
1009 ir_node *b = get_Mul_right(n);
1011 /* Mul is commutative and has again an other neutral element. */
1012 if (is_Const(a) && is_Const_one(a)) {
1014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1015 } else if (is_Const(b) && is_Const_one(b)) {
1017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1021 } /* equivalent_node_Mul */
1024 * Optimize a / 1 = a.
1026 static ir_node *equivalent_node_Div(ir_node *n) {
1027 ir_node *a = get_Div_left(n);
1028 ir_node *b = get_Div_right(n);
1030 /* Div is not commutative. */
1031 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1032 /* Turn Div into a tuple (mem, bad, a) */
1033 ir_node *mem = get_Div_mem(n);
1034 ir_node *blk = get_irn_n(n, -1);
1035 turn_into_tuple(n, pn_Div_max);
1036 set_Tuple_pred(n, pn_Div_M, mem);
1037 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1038 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1039 set_Tuple_pred(n, pn_Div_res, a);
1042 } /* equivalent_node_Div */
1045 * Optimize a / 1.0 = a.
1047 static ir_node *equivalent_node_Quot(ir_node *n) {
1048 ir_node *a = get_Quot_left(n);
1049 ir_node *b = get_Quot_right(n);
1051 /* Div is not commutative. */
1052 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1053 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1054 ir_node *mem = get_Quot_mem(n);
1055 ir_node *blk = get_irn_n(n, -1);
1056 turn_into_tuple(n, pn_Quot_max);
1057 set_Tuple_pred(n, pn_Quot_M, mem);
1058 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1059 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1060 set_Tuple_pred(n, pn_Quot_res, a);
1063 } /* equivalent_node_Quot */
1066 * Optimize a / 1 = a.
1068 static ir_node *equivalent_node_DivMod(ir_node *n) {
1069 ir_node *b = get_DivMod_right(n);
1071 /* Div is not commutative. */
1072 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1073 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1074 ir_node *a = get_DivMod_left(n);
1075 ir_node *mem = get_Div_mem(n);
1076 ir_node *blk = get_irn_n(n, -1);
1077 ir_mode *mode = get_DivMod_resmode(n);
1079 turn_into_tuple(n, pn_DivMod_max);
1080 set_Tuple_pred(n, pn_DivMod_M, mem);
1081 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1082 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1083 set_Tuple_pred(n, pn_DivMod_res_div, a);
1084 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1087 } /* equivalent_node_DivMod */
1090 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1092 static ir_node *equivalent_node_Or(ir_node *n) {
1095 ir_node *a = get_Or_left(n);
1096 ir_node *b = get_Or_right(n);
1099 n = a; /* Or has it's own neutral element */
1100 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1101 } else if (is_Const(a) && is_Const_null(a)) {
1103 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1104 } else if (is_Const(b) && is_Const_null(b)) {
1106 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } /* equivalent_node_Or */
1113 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1115 static ir_node *equivalent_node_And(ir_node *n) {
1118 ir_node *a = get_And_left(n);
1119 ir_node *b = get_And_right(n);
1122 n = a; /* And has it's own neutral element */
1123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1126 if (is_Const(a) && is_Const_all_one(a)) {
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1131 if (is_Const(b) && is_Const_all_one(b)) {
1133 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1145 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1148 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1154 } /* equivalent_node_And */
1157 * Try to remove useless Conv's:
1159 static ir_node *equivalent_node_Conv(ir_node *n) {
1161 ir_node *a = get_Conv_op(n);
1164 ir_mode *n_mode = get_irn_mode(n);
1165 ir_mode *a_mode = get_irn_mode(a);
1167 if (n_mode == a_mode) { /* No Conv necessary */
1168 if (get_Conv_strict(n)) {
1169 /* special case: the predecessor might be a also a Conv */
1171 if (! get_Conv_strict(a)) {
1172 /* first one is not strict, kick it */
1173 set_Conv_op(n, get_Conv_op(a));
1176 /* else both are strict conv, second is superflous */
1178 /* leave strict floating point Conv's */
1183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1184 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1188 n_mode = get_irn_mode(n);
1189 b_mode = get_irn_mode(b);
1191 if (n_mode == b_mode) {
1192 if (n_mode == mode_b) {
1193 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1195 } else if (mode_is_int(n_mode)) {
1196 if (smaller_mode(b_mode, a_mode)){
1197 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1198 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1204 } /* equivalent_node_Conv */
1207 * A Cast may be removed if the type of the previous node
1208 * is already the type of the Cast.
1210 static ir_node *equivalent_node_Cast(ir_node *n) {
1212 ir_node *pred = get_Cast_op(n);
1214 if (get_irn_type(pred) == get_Cast_type(n)) {
1216 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1219 } /* equivalent_node_Cast */
1222 * Several optimizations:
1223 * - no Phi in start block.
1224 * - remove Id operators that are inputs to Phi
1225 * - fold Phi-nodes, iff they have only one predecessor except
1228 static ir_node *equivalent_node_Phi(ir_node *n) {
1232 ir_node *block = NULL; /* to shutup gcc */
1233 ir_node *first_val = NULL; /* to shutup gcc */
1235 if (!get_opt_normalize()) return n;
1237 n_preds = get_Phi_n_preds(n);
1239 block = get_nodes_block(n);
1240 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1241 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1242 if ((is_Block_dead(block)) || /* Control dead */
1243 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1244 return new_Bad(); /* in the Start Block. */
1246 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1248 /* If the Block has a Bad pred, we also have one. */
1249 for (i = 0; i < n_preds; ++i)
1250 if (is_Bad(get_Block_cfgpred(block, i)))
1251 set_Phi_pred(n, i, new_Bad());
1253 /* Find first non-self-referencing input */
1254 for (i = 0; i < n_preds; ++i) {
1255 first_val = get_Phi_pred(n, i);
1256 if ( (first_val != n) /* not self pointer */
1258 && (! is_Bad(first_val))
1260 ) { /* value not dead */
1261 break; /* then found first value. */
1266 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1270 /* search for rest of inputs, determine if any of these
1271 are non-self-referencing */
1272 while (++i < n_preds) {
1273 ir_node *scnd_val = get_Phi_pred(n, i);
1274 if ( (scnd_val != n)
1275 && (scnd_val != first_val)
1277 && (! is_Bad(scnd_val))
1285 /* Fold, if no multiple distinct non-self-referencing inputs */
1287 DBG_OPT_PHI(oldn, n);
1290 } /* equivalent_node_Phi */
1293 * Several optimizations:
1294 * - no Sync in start block.
1295 * - fold Sync-nodes, iff they have only one predecessor except
1298 static ir_node *equivalent_node_Sync(ir_node *n) {
1302 ir_node *first_val = NULL; /* to shutup gcc */
1304 if (!get_opt_normalize()) return n;
1306 n_preds = get_Sync_n_preds(n);
1308 /* Find first non-self-referencing input */
1309 for (i = 0; i < n_preds; ++i) {
1310 first_val = get_Sync_pred(n, i);
1311 if ((first_val != n) /* not self pointer */ &&
1312 (! is_Bad(first_val))
1313 ) { /* value not dead */
1314 break; /* then found first value. */
1319 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1322 /* search the rest of inputs, determine if any of these
1323 are non-self-referencing */
1324 while (++i < n_preds) {
1325 ir_node *scnd_val = get_Sync_pred(n, i);
1326 if ((scnd_val != n) &&
1327 (scnd_val != first_val) &&
1328 (! is_Bad(scnd_val))
1334 /* Fold, if no multiple distinct non-self-referencing inputs */
1336 DBG_OPT_SYNC(oldn, n);
1339 } /* equivalent_node_Sync */
1342 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1343 * ProjX(Load) and ProjX(Store).
1345 static ir_node *equivalent_node_Proj(ir_node *proj) {
1346 ir_node *oldn = proj;
1347 ir_node *a = get_Proj_pred(proj);
1350 /* Remove the Tuple/Proj combination. */
1351 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1352 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1353 DBG_OPT_TUPLE(oldn, a, proj);
1355 /* This should not happen! */
1356 assert(! "found a Proj with higher number than Tuple predecessors");
1359 } else if (get_irn_mode(proj) == mode_X) {
1360 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1361 /* Remove dead control flow -- early gigo(). */
1363 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1365 /* get the Load address */
1366 ir_node *addr = get_Load_ptr(a);
1367 ir_node *blk = get_irn_n(a, -1);
1370 if (value_not_null(addr, &confirm)) {
1371 if (confirm == NULL) {
1372 /* this node may float if it did not depend on a Confirm */
1373 set_irn_pinned(a, op_pin_state_floats);
1375 if (get_Proj_proj(proj) == pn_Load_X_except) {
1376 DBG_OPT_EXC_REM(proj);
1379 return new_r_Jmp(current_ir_graph, blk);
1381 } else if (is_Store(a)) {
1382 /* get the load/store address */
1383 ir_node *addr = get_Store_ptr(a);
1384 ir_node *blk = get_irn_n(a, -1);
1387 if (value_not_null(addr, &confirm)) {
1388 if (confirm == NULL) {
1389 /* this node may float if it did not depend on a Confirm */
1390 set_irn_pinned(a, op_pin_state_floats);
1392 if (get_Proj_proj(proj) == pn_Store_X_except) {
1393 DBG_OPT_EXC_REM(proj);
1396 return new_r_Jmp(current_ir_graph, blk);
1403 } /* equivalent_node_Proj */
1408 static ir_node *equivalent_node_Id(ir_node *n) {
1413 } while (get_irn_op(n) == op_Id);
1415 DBG_OPT_ID(oldn, n);
1417 } /* equivalent_node_Id */
1422 static ir_node *equivalent_node_Mux(ir_node *n)
1424 ir_node *oldn = n, *sel = get_Mux_sel(n);
1425 tarval *ts = value_of(sel);
1427 /* Mux(true, f, t) == t */
1428 if (ts == tarval_b_true) {
1429 n = get_Mux_true(n);
1430 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1432 /* Mux(false, f, t) == f */
1433 else if (ts == tarval_b_false) {
1434 n = get_Mux_false(n);
1435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1437 /* Mux(v, x, x) == x */
1438 else if (get_Mux_false(n) == get_Mux_true(n)) {
1439 n = get_Mux_true(n);
1440 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1442 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1443 ir_node *cmp = get_Proj_pred(sel);
1444 long proj_nr = get_Proj_proj(sel);
1445 ir_node *b = get_Mux_false(n);
1446 ir_node *a = get_Mux_true(n);
1449 * Note: normalization puts the constant on the right site,
1450 * so we check only one case.
1452 * Note further that these optimization work even for floating point
1453 * with NaN's because -NaN == NaN.
1454 * However, if +0 and -0 is handled differently, we cannot use the first one.
1456 if (is_Cmp(cmp) && get_Cmp_left(cmp) == a) {
1457 ir_node *cmp_r = get_Cmp_right(cmp);
1458 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1459 /* Mux(a CMP 0, X, a) */
1460 if (is_Minus(b) && get_Minus_op(b) == a) {
1461 /* Mux(a CMP 0, -a, a) */
1462 if (proj_nr == pn_Cmp_Eq) {
1463 /* Mux(a == 0, -a, a) ==> -a */
1465 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1466 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1467 /* Mux(a != 0, -a, a) ==> a */
1469 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1471 } else if (is_Const(b) && is_Const_null(b)) {
1472 /* Mux(a CMP 0, 0, a) */
1473 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1474 /* Mux(a != 0, 0, a) ==> a */
1476 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1477 } else if (proj_nr == pn_Cmp_Eq) {
1478 /* Mux(a == 0, 0, a) ==> 0 */
1480 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1487 } /* equivalent_node_Mux */
1490 * Returns a equivalent node of a Psi: if a condition is true
1491 * and all previous conditions are false we know its value.
1492 * If all conditions are false its value is the default one.
1494 static ir_node *equivalent_node_Psi(ir_node *n) {
1496 return equivalent_node_Mux(n);
1498 } /* equivalent_node_Psi */
1501 * Optimize -a CMP -b into b CMP a.
1502 * This works only for for modes where unary Minus
1504 * Note that two-complement integers can Overflow
1505 * so it will NOT work.
1507 * For == and != can be handled in Proj(Cmp)
1509 static ir_node *equivalent_node_Cmp(ir_node *n) {
1510 ir_node *left = get_Cmp_left(n);
1511 ir_node *right = get_Cmp_right(n);
1513 if (is_Minus(left) && is_Minus(right) &&
1514 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1515 left = get_Minus_op(left);
1516 right = get_Minus_op(right);
1517 set_Cmp_left(n, right);
1518 set_Cmp_right(n, left);
1521 } /* equivalent_node_Cmp */
1524 * Remove Confirm nodes if setting is on.
1525 * Replace Confirms(x, '=', Constlike) by Constlike.
1527 static ir_node *equivalent_node_Confirm(ir_node *n) {
1528 ir_node *pred = get_Confirm_value(n);
1529 pn_Cmp pnc = get_Confirm_cmp(n);
1531 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1533 * rare case: two identical Confirms one after another,
1534 * replace the second one with the first.
1538 if (pnc == pn_Cmp_Eq) {
1539 ir_node *bound = get_Confirm_bound(n);
1542 * Optimize a rare case:
1543 * Confirm(x, '=', Constlike) ==> Constlike
1545 if (is_irn_constlike(bound)) {
1546 DBG_OPT_CONFIRM(n, bound);
1550 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1554 * Optimize CopyB(mem, x, x) into a Nop.
1556 static ir_node *equivalent_node_CopyB(ir_node *n) {
1557 ir_node *a = get_CopyB_dst(n);
1558 ir_node *b = get_CopyB_src(n);
1561 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1562 ir_node *mem = get_CopyB_mem(n);
1563 ir_node *blk = get_nodes_block(n);
1564 turn_into_tuple(n, pn_CopyB_max);
1565 set_Tuple_pred(n, pn_CopyB_M, mem);
1566 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1567 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1568 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1571 } /* equivalent_node_CopyB */
1574 * Optimize Bounds(idx, idx, upper) into idx.
1576 static ir_node *equivalent_node_Bound(ir_node *n) {
1577 ir_node *idx = get_Bound_index(n);
1578 ir_node *lower = get_Bound_lower(n);
1581 /* By definition lower < upper, so if idx == lower -->
1582 lower <= idx && idx < upper */
1584 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1587 ir_node *pred = skip_Proj(idx);
1589 if (get_irn_op(pred) == op_Bound) {
1591 * idx was Bounds_check previously, it is still valid if
1592 * lower <= pred_lower && pred_upper <= upper.
1594 ir_node *upper = get_Bound_upper(n);
1595 if (get_Bound_lower(pred) == lower &&
1596 get_Bound_upper(pred) == upper) {
1598 * One could expect that we simply return the previous
1599 * Bound here. However, this would be wrong, as we could
1600 * add an exception Proj to a new location then.
1601 * So, we must turn in into a tuple.
1608 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1609 ir_node *mem = get_Bound_mem(n);
1610 ir_node *blk = get_nodes_block(n);
1611 turn_into_tuple(n, pn_Bound_max);
1612 set_Tuple_pred(n, pn_Bound_M, mem);
1613 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1614 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1615 set_Tuple_pred(n, pn_Bound_res, idx);
1618 } /* equivalent_node_Bound */
1621 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1622 * perform no actual computation, as, e.g., the Id nodes. It does not create
1623 * new nodes. It is therefore safe to free n if the node returned is not n.
1624 * If a node returns a Tuple we can not just skip it. If the size of the
1625 * in array fits, we transform n into a tuple (e.g., Div).
1627 ir_node *equivalent_node(ir_node *n) {
1628 if (n->op->ops.equivalent_node)
1629 return n->op->ops.equivalent_node(n);
1631 } /* equivalent_node */
1634 * Sets the default equivalent node operation for an ir_op_ops.
1636 * @param code the opcode for the default operation
1637 * @param ops the operations initialized
1642 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1646 ops->equivalent_node = equivalent_node_##a; \
1686 } /* firm_set_default_equivalent_node */
1689 * Returns non-zero if a node is a Phi node
1690 * with all predecessors constant.
1692 static int is_const_Phi(ir_node *n) {
1697 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1698 if (! is_Const(get_irn_n(n, i)))
1701 } /* is_const_Phi */
1704 * Apply an evaluator on a binop with a constant operators (and one Phi).
1706 * @param phi the Phi node
1707 * @param other the other operand
1708 * @param eval an evaluator function
1709 * @param left if non-zero, other is the left operand, else the right
1711 * @return a new Phi node if the conversion was successful, NULL else
1713 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1719 int i, n = get_irn_arity(phi);
1721 NEW_ARR_A(void *, res, n);
1723 for (i = 0; i < n; ++i) {
1724 pred = get_irn_n(phi, i);
1725 tv = get_Const_tarval(pred);
1726 tv = eval(other, tv);
1728 if (tv == tarval_bad) {
1729 /* folding failed, bad */
1735 for (i = 0; i < n; ++i) {
1736 pred = get_irn_n(phi, i);
1737 tv = get_Const_tarval(pred);
1738 tv = eval(tv, other);
1740 if (tv == tarval_bad) {
1741 /* folding failed, bad */
1747 mode = get_irn_mode(phi);
1748 irg = current_ir_graph;
1749 for (i = 0; i < n; ++i) {
1750 pred = get_irn_n(phi, i);
1751 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1752 mode, res[i], get_Const_type(pred));
1754 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1755 } /* apply_binop_on_phi */
1758 * Apply an evaluator on a unop with a constant operator (a Phi).
1760 * @param phi the Phi node
1761 * @param eval an evaluator function
1763 * @return a new Phi node if the conversion was successful, NULL else
1765 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1771 int i, n = get_irn_arity(phi);
1773 NEW_ARR_A(void *, res, n);
1774 for (i = 0; i < n; ++i) {
1775 pred = get_irn_n(phi, i);
1776 tv = get_Const_tarval(pred);
1779 if (tv == tarval_bad) {
1780 /* folding failed, bad */
1785 mode = get_irn_mode(phi);
1786 irg = current_ir_graph;
1787 for (i = 0; i < n; ++i) {
1788 pred = get_irn_n(phi, i);
1789 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1790 mode, res[i], get_Const_type(pred));
1792 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1793 } /* apply_unop_on_phi */
1796 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1797 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1798 * If possible, remove the Conv's.
1800 static ir_node *transform_node_AddSub(ir_node *n) {
1801 ir_mode *mode = get_irn_mode(n);
1803 if (mode_is_reference(mode)) {
1804 ir_node *left = get_binop_left(n);
1805 ir_node *right = get_binop_right(n);
1806 int ref_bits = get_mode_size_bits(mode);
1808 if (is_Conv(left)) {
1809 ir_mode *mode = get_irn_mode(left);
1810 int bits = get_mode_size_bits(mode);
1812 if (ref_bits == bits &&
1813 mode_is_int(mode) &&
1814 get_mode_arithmetic(mode) == irma_twos_complement) {
1815 ir_node *pre = get_Conv_op(left);
1816 ir_mode *pre_mode = get_irn_mode(pre);
1818 if (mode_is_int(pre_mode) &&
1819 get_mode_size_bits(pre_mode) == bits &&
1820 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1821 /* ok, this conv just changes to sign, moreover the calculation
1822 * is done with same number of bits as our address mode, so
1823 * we can ignore the conv as address calculation can be viewed
1824 * as either signed or unsigned
1826 set_binop_left(n, pre);
1831 if (is_Conv(right)) {
1832 ir_mode *mode = get_irn_mode(right);
1833 int bits = get_mode_size_bits(mode);
1835 if (ref_bits == bits &&
1836 mode_is_int(mode) &&
1837 get_mode_arithmetic(mode) == irma_twos_complement) {
1838 ir_node *pre = get_Conv_op(right);
1839 ir_mode *pre_mode = get_irn_mode(pre);
1841 if (mode_is_int(pre_mode) &&
1842 get_mode_size_bits(pre_mode) == bits &&
1843 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1844 /* ok, this conv just changes to sign, moreover the calculation
1845 * is done with same number of bits as our address mode, so
1846 * we can ignore the conv as address calculation can be viewed
1847 * as either signed or unsigned
1849 set_binop_right(n, pre);
1855 } /* transform_node_AddSub */
1857 #define HANDLE_BINOP_PHI(op,a,b,c) \
1859 if (is_Const(b) && is_const_Phi(a)) { \
1860 /* check for Op(Phi, Const) */ \
1861 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1863 else if (is_Const(a) && is_const_Phi(b)) { \
1864 /* check for Op(Const, Phi) */ \
1865 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1868 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1872 #define HANDLE_UNOP_PHI(op,a,c) \
1874 if (is_const_Phi(a)) { \
1875 /* check for Op(Phi) */ \
1876 c = apply_unop_on_phi(a, op); \
1878 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1884 * Do the AddSub optimization, then Transform
1885 * Constant folding on Phi
1886 * Add(a,a) -> Mul(a, 2)
1887 * Add(Mul(a, x), a) -> Mul(a, x+1)
1888 * if the mode is integer or float.
1889 * Transform Add(a,-b) into Sub(a,b).
1890 * Reassociation might fold this further.
1892 static ir_node *transform_node_Add(ir_node *n) {
1894 ir_node *a, *b, *c, *oldn = n;
1896 n = transform_node_AddSub(n);
1898 a = get_Add_left(n);
1899 b = get_Add_right(n);
1901 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1903 mode = get_irn_mode(n);
1905 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1906 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1909 if (mode_is_num(mode)) {
1910 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1911 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1912 ir_node *block = get_irn_n(n, -1);
1915 get_irn_dbg_info(n),
1919 new_r_Const_long(current_ir_graph, block, mode, 2),
1921 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1926 get_irn_dbg_info(n),
1932 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1937 get_irn_dbg_info(n),
1943 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1946 if (! get_opt_reassociation()) {
1947 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1949 ir_node *ma = get_Mul_left(a);
1950 ir_node *mb = get_Mul_right(a);
1953 ir_node *blk = get_irn_n(n, -1);
1955 get_irn_dbg_info(n), current_ir_graph, blk,
1958 get_irn_dbg_info(n), current_ir_graph, blk,
1960 new_r_Const_long(current_ir_graph, blk, mode, 1),
1963 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1965 } else if (b == mb) {
1966 ir_node *blk = get_irn_n(n, -1);
1968 get_irn_dbg_info(n), current_ir_graph, blk,
1971 get_irn_dbg_info(n), current_ir_graph, blk,
1973 new_r_Const_long(current_ir_graph, blk, mode, 1),
1976 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1981 ir_node *ma = get_Mul_left(b);
1982 ir_node *mb = get_Mul_right(b);
1985 ir_node *blk = get_irn_n(n, -1);
1987 get_irn_dbg_info(n), current_ir_graph, blk,
1990 get_irn_dbg_info(n), current_ir_graph, blk,
1992 new_r_Const_long(current_ir_graph, blk, mode, 1),
1995 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1999 ir_node *blk = get_irn_n(n, -1);
2001 get_irn_dbg_info(n), current_ir_graph, blk,
2004 get_irn_dbg_info(n), current_ir_graph, blk,
2006 new_r_Const_long(current_ir_graph, blk, mode, 1),
2009 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2014 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2015 /* Here we rely on constants be on the RIGHT side */
2017 ir_node *op = get_Not_op(a);
2019 if (is_Const(b) && is_Const_one(b)) {
2021 ir_node *blk = get_irn_n(n, -1);
2022 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2023 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2028 ir_node *blk = get_irn_n(n, -1);
2029 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2030 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2035 ir_node *op = get_Not_op(b);
2039 ir_node *blk = get_irn_n(n, -1);
2040 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2048 } /* transform_node_Add */
2051 static ir_node *const_negate(ir_node *cnst) {
2052 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2053 dbg_info *dbgi = get_irn_dbg_info(cnst);
2054 ir_graph *irg = get_irn_irg(cnst);
2055 ir_node *block = get_nodes_block(cnst);
2056 ir_mode *mode = get_irn_mode(cnst);
2057 if (tv == tarval_bad) return NULL;
2058 return new_rd_Const(dbgi, irg, block, mode, tv);
2062 * Do the AddSub optimization, then Transform
2063 * Constant folding on Phi
2064 * Sub(0,a) -> Minus(a)
2065 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2066 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2067 * Sub(Add(a, x), x) -> a
2068 * Sub(x, Add(x, a)) -> -a
2069 * Sub(x, Const) -> Add(x, -Const)
2071 static ir_node *transform_node_Sub(ir_node *n) {
2076 n = transform_node_AddSub(n);
2078 a = get_Sub_left(n);
2079 b = get_Sub_right(n);
2081 mode = get_irn_mode(n);
2084 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2086 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2087 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2090 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2091 /* a - C -> a + (-C) */
2092 ir_node *cnst = const_negate(b);
2094 ir_node *block = get_nodes_block(n);
2095 dbg_info *dbgi = get_irn_dbg_info(n);
2096 ir_graph *irg = get_irn_irg(n);
2098 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2099 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2104 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2105 ir_graph *irg = current_ir_graph;
2106 dbg_info *dbg = get_irn_dbg_info(n);
2107 ir_node *block = get_nodes_block(n);
2108 ir_node *left = get_Minus_op(a);
2109 ir_mode *mode = get_irn_mode(n);
2110 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2112 n = new_rd_Minus(dbg, irg, block, add, mode);
2113 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2115 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2116 ir_graph *irg = current_ir_graph;
2117 dbg_info *dbg = get_irn_dbg_info(n);
2118 ir_node *block = get_nodes_block(n);
2119 ir_node *right = get_Minus_op(b);
2120 ir_mode *mode = get_irn_mode(n);
2122 n = new_rd_Add(dbg, irg, block, a, right, mode);
2123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2125 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2126 ir_graph *irg = current_ir_graph;
2127 dbg_info *s_dbg = get_irn_dbg_info(b);
2128 ir_node *s_block = get_nodes_block(b);
2129 ir_node *s_left = get_Sub_right(b);
2130 ir_node *s_right = get_Sub_left(b);
2131 ir_mode *s_mode = get_irn_mode(b);
2132 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2133 dbg_info *a_dbg = get_irn_dbg_info(n);
2134 ir_node *a_block = get_nodes_block(n);
2135 ir_mode *a_mode = get_irn_mode(n);
2137 n = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2138 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2140 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2141 ir_node *m_right = get_Mul_right(b);
2142 if (is_Const(m_right)) {
2143 ir_node *cnst2 = const_negate(m_right);
2144 if (cnst2 != NULL) {
2145 ir_graph *irg = current_ir_graph;
2146 dbg_info *m_dbg = get_irn_dbg_info(b);
2147 ir_node *m_block = get_nodes_block(b);
2148 ir_node *m_left = get_Mul_left(b);
2149 ir_mode *m_mode = get_irn_mode(b);
2150 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2151 dbg_info *a_dbg = get_irn_dbg_info(n);
2152 ir_node *a_block = get_nodes_block(n);
2153 ir_mode *a_mode = get_irn_mode(n);
2155 n = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2156 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2162 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2163 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2165 get_irn_dbg_info(n),
2170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2174 if (mode_wrap_around(mode)) {
2175 ir_node *left = get_Add_left(a);
2176 ir_node *right = get_Add_right(a);
2178 /* FIXME: Does the Conv's work only for two complement or generally? */
2180 if (mode != get_irn_mode(right)) {
2181 /* This Sub is an effective Cast */
2182 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2185 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2187 } else if (right == b) {
2188 if (mode != get_irn_mode(left)) {
2189 /* This Sub is an effective Cast */
2190 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2193 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2199 if (mode_wrap_around(mode)) {
2200 ir_node *left = get_Add_left(b);
2201 ir_node *right = get_Add_right(b);
2203 /* FIXME: Does the Conv's work only for two complement or generally? */
2205 ir_mode *r_mode = get_irn_mode(right);
2207 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2208 if (mode != r_mode) {
2209 /* This Sub is an effective Cast */
2210 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2212 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2214 } else if (right == a) {
2215 ir_mode *l_mode = get_irn_mode(left);
2217 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2218 if (mode != l_mode) {
2219 /* This Sub is an effective Cast */
2220 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2222 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2227 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2228 ir_mode *mode = get_irn_mode(a);
2230 if (mode == get_irn_mode(b)) {
2236 /* check if it's allowed to skip the conv */
2237 ma = get_irn_mode(a);
2238 mb = get_irn_mode(b);
2240 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2241 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2243 set_Sub_right(n, b);
2249 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2250 if (get_opt_reassociation() && is_Mul(a)) {
2251 ir_node *ma = get_Mul_left(a);
2252 ir_node *mb = get_Mul_right(a);
2255 ir_node *blk = get_irn_n(n, -1);
2257 get_irn_dbg_info(n),
2258 current_ir_graph, blk,
2261 get_irn_dbg_info(n),
2262 current_ir_graph, blk,
2264 new_r_Const_long(current_ir_graph, blk, mode, 1),
2267 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2269 } else if (mb == b) {
2270 ir_node *blk = get_irn_n(n, -1);
2272 get_irn_dbg_info(n),
2273 current_ir_graph, blk,
2276 get_irn_dbg_info(n),
2277 current_ir_graph, blk,
2279 new_r_Const_long(current_ir_graph, blk, mode, 1),
2282 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2287 ir_node *x = get_Sub_left(a);
2288 ir_node *y = get_Sub_right(a);
2289 ir_node *blk = get_irn_n(n, -1);
2290 ir_mode *m_b = get_irn_mode(b);
2291 ir_mode *m_y = get_irn_mode(y);
2294 /* Determine the right mode for the Add. */
2297 else if (mode_is_reference(m_b))
2299 else if (mode_is_reference(m_y))
2303 * Both modes are different but none is reference,
2304 * happens for instance in SubP(SubP(P, Iu), Is).
2305 * We have two possibilities here: Cast or ignore.
2306 * Currently we ignore this case.
2311 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2313 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2314 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2318 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2319 if (is_Const(a) && is_Not(b)) {
2320 /* c - ~X = X + (c+1) */
2321 tarval *tv = get_Const_tarval(a);
2323 tv = tarval_add(tv, get_mode_one(mode));
2324 if (tv != tarval_bad) {
2325 ir_node *blk = get_irn_n(n, -1);
2326 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2327 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2328 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2334 } /* transform_node_Sub */
2337 * Several transformation done on n*n=2n bits mul.
2338 * These transformations must be done here because new nodes may be produced.
2340 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2342 ir_node *a = get_Mul_left(n);
2343 ir_node *b = get_Mul_right(n);
2344 tarval *ta = value_of(a);
2345 tarval *tb = value_of(b);
2346 ir_mode *smode = get_irn_mode(a);
2348 if (ta == get_mode_one(smode)) {
2349 ir_node *blk = get_irn_n(n, -1);
2350 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2351 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2354 else if (ta == get_mode_minus_one(smode)) {
2355 ir_node *blk = get_irn_n(n, -1);
2356 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2357 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2358 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2361 if (tb == get_mode_one(smode)) {
2362 ir_node *blk = get_irn_n(a, -1);
2363 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2364 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2367 else if (tb == get_mode_minus_one(smode)) {
2368 ir_node *blk = get_irn_n(n, -1);
2369 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2370 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2371 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2378 * Transform Mul(a,-1) into -a.
2379 * Do constant evaluation of Phi nodes.
2380 * Do architecture dependent optimizations on Mul nodes
2382 static ir_node *transform_node_Mul(ir_node *n) {
2383 ir_node *c, *oldn = n;
2384 ir_mode *mode = get_irn_mode(n);
2385 ir_node *a = get_Mul_left(n);
2386 ir_node *b = get_Mul_right(n);
2388 if (is_Bad(a) || is_Bad(b))
2391 if (mode != get_irn_mode(a))
2392 return transform_node_Mul2n(n, mode);
2394 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2396 if (mode_is_signed(mode)) {
2399 if (value_of(a) == get_mode_minus_one(mode))
2401 else if (value_of(b) == get_mode_minus_one(mode))
2404 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2405 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2410 if (is_Const(b)) { /* (-a) * const -> a * -const */
2411 ir_node *cnst = const_negate(b);
2413 dbg_info *dbgi = get_irn_dbg_info(n);
2414 ir_node *block = get_nodes_block(n);
2415 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2416 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2419 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2420 dbg_info *dbgi = get_irn_dbg_info(n);
2421 ir_node *block = get_nodes_block(n);
2422 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2423 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2425 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2426 ir_node *sub_l = get_Sub_left(b);
2427 ir_node *sub_r = get_Sub_right(b);
2428 dbg_info *dbgi = get_irn_dbg_info(n);
2429 ir_graph *irg = current_ir_graph;
2430 ir_node *block = get_nodes_block(n);
2431 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2432 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2433 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2436 } else if (is_Minus(b)) {
2437 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2438 ir_node *sub_l = get_Sub_left(a);
2439 ir_node *sub_r = get_Sub_right(a);
2440 dbg_info *dbgi = get_irn_dbg_info(n);
2441 ir_graph *irg = current_ir_graph;
2442 ir_node *block = get_nodes_block(n);
2443 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2444 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2449 if (get_mode_arithmetic(mode) == irma_ieee754) {
2451 tarval *tv = get_Const_tarval(a);
2452 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2453 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2454 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2458 else if (is_Const(b)) {
2459 tarval *tv = get_Const_tarval(b);
2460 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2461 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2462 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2467 return arch_dep_replace_mul_with_shifts(n);
2468 } /* transform_node_Mul */
2471 * Transform a Div Node.
2473 static ir_node *transform_node_Div(ir_node *n) {
2474 tarval *tv = value_of(n);
2475 ir_mode *mode = get_Div_resmode(n);
2478 if (tv != tarval_bad) {
2479 value = new_Const(get_tarval_mode(tv), tv);
2481 DBG_OPT_CSTEVAL(n, value);
2484 ir_node *a = get_Div_left(n);
2485 ir_node *b = get_Div_right(n);
2488 if (a == b && value_not_zero(a, &dummy)) {
2489 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2490 value = new_Const(mode, get_mode_one(mode));
2491 DBG_OPT_CSTEVAL(n, value);
2494 if (mode_is_signed(mode) && is_Const(b)) {
2495 tarval *tv = get_Const_tarval(b);
2497 if (tv == get_mode_minus_one(mode)) {
2499 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2500 DBG_OPT_CSTEVAL(n, value);
2504 /* Try architecture dependent optimization */
2505 value = arch_dep_replace_div_by_const(n);
2513 /* Turn Div into a tuple (mem, jmp, bad, value) */
2514 mem = get_Div_mem(n);
2515 blk = get_irn_n(n, -1);
2517 /* skip a potential Pin */
2519 mem = get_Pin_op(mem);
2520 turn_into_tuple(n, pn_Div_max);
2521 set_Tuple_pred(n, pn_Div_M, mem);
2522 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2523 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2524 set_Tuple_pred(n, pn_Div_res, value);
2527 } /* transform_node_Div */
2530 * Transform a Mod node.
2532 static ir_node *transform_node_Mod(ir_node *n) {
2533 tarval *tv = value_of(n);
2534 ir_mode *mode = get_Mod_resmode(n);
2537 if (tv != tarval_bad) {
2538 value = new_Const(get_tarval_mode(tv), tv);
2540 DBG_OPT_CSTEVAL(n, value);
2543 ir_node *a = get_Mod_left(n);
2544 ir_node *b = get_Mod_right(n);
2547 if (a == b && value_not_zero(a, &dummy)) {
2548 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2549 value = new_Const(mode, get_mode_null(mode));
2550 DBG_OPT_CSTEVAL(n, value);
2553 if (mode_is_signed(mode) && is_Const(b)) {
2554 tarval *tv = get_Const_tarval(b);
2556 if (tv == get_mode_minus_one(mode)) {
2558 value = new_Const(mode, get_mode_null(mode));
2559 DBG_OPT_CSTEVAL(n, value);
2563 /* Try architecture dependent optimization */
2564 value = arch_dep_replace_mod_by_const(n);
2572 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2573 mem = get_Mod_mem(n);
2574 blk = get_irn_n(n, -1);
2576 /* skip a potential Pin */
2578 mem = get_Pin_op(mem);
2579 turn_into_tuple(n, pn_Mod_max);
2580 set_Tuple_pred(n, pn_Mod_M, mem);
2581 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2582 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2583 set_Tuple_pred(n, pn_Mod_res, value);
2586 } /* transform_node_Mod */
2589 * Transform a DivMod node.
2591 static ir_node *transform_node_DivMod(ir_node *n) {
2593 ir_node *a = get_DivMod_left(n);
2594 ir_node *b = get_DivMod_right(n);
2595 ir_mode *mode = get_DivMod_resmode(n);
2596 tarval *ta = value_of(a);
2597 tarval *tb = value_of(b);
2600 if (tb != tarval_bad) {
2601 if (tb == get_mode_one(get_tarval_mode(tb))) {
2602 b = new_Const(mode, get_mode_null(mode));
2603 DBG_OPT_CSTEVAL(n, b);
2605 } else if (ta != tarval_bad) {
2606 tarval *resa, *resb;
2607 resa = tarval_div(ta, tb);
2608 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2609 Jmp for X result!? */
2610 resb = tarval_mod(ta, tb);
2611 if (resb == tarval_bad) return n; /* Causes exception! */
2612 a = new_Const(mode, resa);
2613 b = new_Const(mode, resb);
2614 DBG_OPT_CSTEVAL(n, a);
2615 DBG_OPT_CSTEVAL(n, b);
2617 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2618 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2619 b = new_Const(mode, get_mode_null(mode));
2620 DBG_OPT_CSTEVAL(n, a);
2621 DBG_OPT_CSTEVAL(n, b);
2623 } else { /* Try architecture dependent optimization */
2624 arch_dep_replace_divmod_by_const(&a, &b, n);
2625 evaluated = a != NULL;
2627 } else if (a == b) {
2628 if (value_not_zero(a, &dummy)) {
2630 a = new_Const(mode, get_mode_one(mode));
2631 b = new_Const(mode, get_mode_null(mode));
2632 DBG_OPT_CSTEVAL(n, a);
2633 DBG_OPT_CSTEVAL(n, b);
2636 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2639 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2640 /* 0 / non-Const = 0 */
2645 if (evaluated) { /* replace by tuple */
2649 mem = get_DivMod_mem(n);
2650 /* skip a potential Pin */
2652 mem = get_Pin_op(mem);
2654 blk = get_irn_n(n, -1);
2655 turn_into_tuple(n, pn_DivMod_max);
2656 set_Tuple_pred(n, pn_DivMod_M, mem);
2657 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2658 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2659 set_Tuple_pred(n, pn_DivMod_res_div, a);
2660 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2664 } /* transform_node_DivMod */
2667 * Optimize x / c to x * (1/c)
2669 static ir_node *transform_node_Quot(ir_node *n) {
2670 ir_mode *mode = get_Quot_resmode(n);
2673 if (get_mode_arithmetic(mode) == irma_ieee754) {
2674 ir_node *b = get_Quot_right(n);
2677 tarval *tv = get_Const_tarval(b);
2679 tv = tarval_quo(get_mode_one(mode), tv);
2681 /* Do the transformation if the result is either exact or we are not
2682 using strict rules. */
2683 if (tv != tarval_bad &&
2684 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2685 ir_node *blk = get_irn_n(n, -1);
2686 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2687 ir_node *a = get_Quot_left(n);
2688 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2689 ir_node *mem = get_Quot_mem(n);
2691 /* skip a potential Pin */
2693 mem = get_Pin_op(mem);
2694 turn_into_tuple(n, pn_Quot_max);
2695 set_Tuple_pred(n, pn_Quot_M, mem);
2696 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2697 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2698 set_Tuple_pred(n, pn_Quot_res, m);
2699 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2704 } /* transform_node_Quot */
2707 * Optimize Abs(x) into x if x is Confirmed >= 0
2708 * Optimize Abs(x) into -x if x is Confirmed <= 0
2710 static ir_node *transform_node_Abs(ir_node *n) {
2712 ir_node *a = get_Abs_op(n);
2713 value_classify_sign sign = classify_value_sign(a);
2715 if (sign == value_classified_negative) {
2716 ir_mode *mode = get_irn_mode(n);
2719 * We can replace the Abs by -x here.
2720 * We even could add a new Confirm here.
2722 * Note that -x would create a new node, so we could
2723 * not run it in the equivalent_node() context.
2725 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2726 get_irn_n(n, -1), a, mode);
2728 DBG_OPT_CONFIRM(oldn, n);
2729 } else if (sign == value_classified_positive) {
2730 /* n is positive, Abs is not needed */
2733 DBG_OPT_CONFIRM(oldn, n);
2737 } /* transform_node_Abs */
2740 * Transform a Cond node.
2742 * Replace the Cond by a Jmp if it branches on a constant
2745 static ir_node *transform_node_Cond(ir_node *n) {
2748 ir_node *a = get_Cond_selector(n);
2749 tarval *ta = value_of(a);
2751 /* we need block info which is not available in floating irgs */
2752 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2755 if ((ta != tarval_bad) &&
2756 (get_irn_mode(a) == mode_b) &&
2757 (get_opt_unreachable_code())) {
2758 /* It's a boolean Cond, branching on a boolean constant.
2759 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2760 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2761 turn_into_tuple(n, pn_Cond_max);
2762 if (ta == tarval_b_true) {
2763 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2764 set_Tuple_pred(n, pn_Cond_true, jmp);
2766 set_Tuple_pred(n, pn_Cond_false, jmp);
2767 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2769 /* We might generate an endless loop, so keep it alive. */
2770 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2773 } /* transform_node_Cond */
2775 typedef ir_node* (*recursive_transform) (ir_node *n);
2778 * makes use of distributive laws for and, or, eor
2779 * and(a OP c, b OP c) -> and(a, b) OP c
2780 * note, might return a different op than n
2782 static ir_node *transform_bitwise_distributive(ir_node *n,
2783 recursive_transform trans_func)
2786 ir_node *a = get_binop_left(n);
2787 ir_node *b = get_binop_right(n);
2788 ir_op *op = get_irn_op(a);
2789 ir_op *op_root = get_irn_op(n);
2791 if(op != get_irn_op(b))
2794 if (op == op_Conv) {
2795 ir_node *a_op = get_Conv_op(a);
2796 ir_node *b_op = get_Conv_op(b);
2797 ir_mode *a_mode = get_irn_mode(a_op);
2798 ir_mode *b_mode = get_irn_mode(b_op);
2799 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2800 ir_node *blk = get_irn_n(n, -1);
2803 set_binop_left(n, a_op);
2804 set_binop_right(n, b_op);
2805 set_irn_mode(n, a_mode);
2807 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2809 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2815 /* nothing to gain here */
2819 if (op == op_Shrs || op == op_Shr || op == op_Shl
2820 || op == op_And || op == op_Or || op == op_Eor) {
2821 ir_node *a_left = get_binop_left(a);
2822 ir_node *a_right = get_binop_right(a);
2823 ir_node *b_left = get_binop_left(b);
2824 ir_node *b_right = get_binop_right(b);
2828 if (is_op_commutative(op)) {
2829 if (a_left == b_left) {
2833 } else if(a_left == b_right) {
2837 } else if(a_right == b_left) {
2843 if(a_right == b_right) {
2850 /* (a sop c) & (b sop c) => (a & b) sop c */
2851 ir_node *blk = get_irn_n(n, -1);
2853 ir_node *new_n = exact_copy(n);
2854 set_binop_left(new_n, op1);
2855 set_binop_right(new_n, op2);
2856 new_n = trans_func(new_n);
2858 if(op_root == op_Eor && op == op_Or) {
2859 dbg_info *dbgi = get_irn_dbg_info(n);
2860 ir_graph *irg = current_ir_graph;
2861 ir_mode *mode = get_irn_mode(c);
2863 c = new_rd_Not(dbgi, irg, blk, c, mode);
2864 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2867 set_irn_n(n, -1, blk);
2868 set_binop_left(n, new_n);
2869 set_binop_right(n, c);
2872 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2883 static ir_node *transform_node_And(ir_node *n) {
2884 ir_node *c, *oldn = n;
2885 ir_node *a = get_And_left(n);
2886 ir_node *b = get_And_right(n);
2889 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2891 mode = get_irn_mode(n);
2893 /* we can evaluate 2 Projs of the same Cmp */
2894 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2895 ir_node *pred_a = get_Proj_pred(a);
2896 ir_node *pred_b = get_Proj_pred(b);
2897 if (pred_a == pred_b) {
2898 dbg_info *dbgi = get_irn_dbg_info(n);
2899 ir_node *block = get_nodes_block(pred_a);
2900 pn_Cmp pn_a = get_Proj_proj(a);
2901 pn_Cmp pn_b = get_Proj_proj(b);
2902 /* yes, we can simply calculate with pncs */
2903 pn_Cmp new_pnc = pn_a & pn_b;
2905 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2910 ir_node *op = get_Not_op(b);
2912 ir_node *ba = get_And_left(op);
2913 ir_node *bb = get_And_right(op);
2915 /* it's enough to test the following cases due to normalization! */
2916 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2917 /* (a|b) & ~(a&b) = a^b */
2918 ir_node *block = get_nodes_block(n);
2920 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2921 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2929 ir_node *op = get_Not_op(a);
2931 ir_node *aa = get_And_left(op);
2932 ir_node *ab = get_And_right(op);
2934 /* it's enough to test the following cases due to normalization! */
2935 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2936 /* (a|b) & ~(a&b) = a^b */
2937 ir_node *block = get_nodes_block(n);
2939 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2940 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2947 ir_node *al = get_Eor_left(a);
2948 ir_node *ar = get_Eor_right(a);
2951 /* (b ^ a) & b -> ~a & b */
2952 dbg_info *dbg = get_irn_dbg_info(n);
2953 ir_node *block = get_nodes_block(n);
2955 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
2956 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
2957 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2961 /* (a ^ b) & b -> ~a & b */
2962 dbg_info *dbg = get_irn_dbg_info(n);
2963 ir_node *block = get_nodes_block(n);
2965 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
2966 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
2967 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2972 ir_node *bl = get_Eor_left(b);
2973 ir_node *br = get_Eor_right(b);
2976 /* a & (a ^ b) -> a & ~b */
2977 dbg_info *dbg = get_irn_dbg_info(n);
2978 ir_node *block = get_nodes_block(n);
2980 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
2981 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
2982 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2986 /* a & (b ^ a) -> a & ~b */
2987 dbg_info *dbg = get_irn_dbg_info(n);
2988 ir_node *block = get_nodes_block(n);
2990 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
2991 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
2992 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2996 if (is_Not(a) && is_Not(b)) {
2997 /* ~a & ~b = ~(a|b) */
2998 ir_node *block = get_nodes_block(n);
2999 ir_mode *mode = get_irn_mode(n);
3003 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3004 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3005 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3009 n = transform_bitwise_distributive(n, transform_node_And);
3012 } /* transform_node_And */
3017 static ir_node *transform_node_Eor(ir_node *n) {
3018 ir_node *c, *oldn = n;
3019 ir_node *a = get_Eor_left(n);
3020 ir_node *b = get_Eor_right(n);
3021 ir_mode *mode = get_irn_mode(n);
3023 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
3025 /* we can evaluate 2 Projs of the same Cmp */
3026 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3027 ir_node *pred_a = get_Proj_pred(a);
3028 ir_node *pred_b = get_Proj_pred(b);
3029 if(pred_a == pred_b) {
3030 dbg_info *dbgi = get_irn_dbg_info(n);
3031 ir_node *block = get_nodes_block(pred_a);
3032 pn_Cmp pn_a = get_Proj_proj(a);
3033 pn_Cmp pn_b = get_Proj_proj(b);
3034 /* yes, we can simply calculate with pncs */
3035 pn_Cmp new_pnc = pn_a ^ pn_b;
3037 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3044 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3045 mode, get_mode_null(mode));
3046 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3047 } else if (mode == mode_b &&
3049 is_Const(b) && is_Const_one(b) &&
3050 is_Cmp(get_Proj_pred(a))) {
3051 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3052 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3053 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3055 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3056 } else if (mode == mode_b && is_Const(b) && is_Const_one(b)) {
3057 /* The Eor is a Not. Replace it by a Not. */
3058 /* ????!!!Extend to bitfield 1111111. */
3059 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
3061 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3063 n = transform_bitwise_distributive(n, transform_node_Eor);
3067 } /* transform_node_Eor */
3072 static ir_node *transform_node_Not(ir_node *n) {
3073 ir_node *c, *oldn = n;
3074 ir_node *a = get_Not_op(n);
3075 ir_mode *mode = get_irn_mode(n);
3077 HANDLE_UNOP_PHI(tarval_not,a,c);
3079 /* check for a boolean Not */
3080 if (mode == mode_b &&
3082 is_Cmp(get_Proj_pred(a))) {
3083 /* We negate a Cmp. The Cmp has the negated result anyways! */
3084 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3085 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3086 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3089 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3091 ir_node *add_r = get_Add_right(a);
3092 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3093 /* ~(x + -1) = -x */
3094 ir_node *op = get_Add_left(a);
3095 ir_node *blk = get_irn_n(n, -1);
3096 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3097 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3102 } /* transform_node_Not */
3105 * Transform a Minus.
3110 static ir_node *transform_node_Minus(ir_node *n) {
3111 ir_node *c, *oldn = n;
3112 ir_node *a = get_Minus_op(n);
3115 HANDLE_UNOP_PHI(tarval_neg,a,c);
3117 mode = get_irn_mode(a);
3118 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3119 /* the following rules are only to twos-complement */
3122 ir_node *op = get_Not_op(a);
3123 tarval *tv = get_mode_one(mode);
3124 ir_node *blk = get_irn_n(n, -1);
3125 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3126 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3127 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3131 ir_node *c = get_Shr_right(a);
3134 tarval *tv = get_Const_tarval(c);
3136 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3137 /* -(a >>u (size-1)) = a >>s (size-1) */
3138 ir_node *v = get_Shr_left(a);
3140 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3141 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3147 ir_node *c = get_Shrs_right(a);
3150 tarval *tv = get_Const_tarval(c);
3152 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3153 /* -(a >>s (size-1)) = a >>u (size-1) */
3154 ir_node *v = get_Shrs_left(a);
3156 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3157 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3164 /* - (a-b) = b - a */
3165 ir_node *la = get_Sub_left(a);
3166 ir_node *ra = get_Sub_right(a);
3167 ir_node *blk = get_irn_n(n, -1);
3169 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3170 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3175 } /* transform_node_Minus */
3178 * Transform a Cast_type(Const) into a new Const_type
3180 static ir_node *transform_node_Cast(ir_node *n) {
3182 ir_node *pred = get_Cast_op(n);
3183 ir_type *tp = get_irn_type(n);
3185 if (is_Const(pred) && get_Const_type(pred) != tp) {
3186 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3187 get_Const_tarval(pred), tp);
3188 DBG_OPT_CSTEVAL(oldn, n);
3189 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3190 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3191 get_SymConst_kind(pred), tp);
3192 DBG_OPT_CSTEVAL(oldn, n);
3196 } /* transform_node_Cast */
3199 * Transform a Proj(Div) with a non-zero value.
3200 * Removes the exceptions and routes the memory to the NoMem node.
3202 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3203 ir_node *div = get_Proj_pred(proj);
3204 ir_node *b = get_Div_right(div);
3205 ir_node *confirm, *res, *new_mem;
3208 if (value_not_zero(b, &confirm)) {
3209 /* div(x, y) && y != 0 */
3210 proj_nr = get_Proj_proj(proj);
3212 case pn_Div_X_regular:
3213 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3215 case pn_Div_X_except:
3216 /* we found an exception handler, remove it */
3217 DBG_OPT_EXC_REM(proj);
3221 res = get_Div_mem(div);
3222 new_mem = get_irg_no_mem(current_ir_graph);
3225 /* This node can only float up to the Confirm block */
3226 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3228 set_irn_pinned(div, op_pin_state_floats);
3229 /* this is a Div without exception, we can remove the memory edge */
3230 set_Div_mem(div, new_mem);
3235 } /* transform_node_Proj_Div */
3238 * Transform a Proj(Mod) with a non-zero value.
3239 * Removes the exceptions and routes the memory to the NoMem node.
3241 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3242 ir_node *mod = get_Proj_pred(proj);
3243 ir_node *b = get_Mod_right(mod);
3244 ir_node *confirm, *res, *new_mem;
3247 if (value_not_zero(b, &confirm)) {
3248 /* mod(x, y) && y != 0 */
3249 proj_nr = get_Proj_proj(proj);
3253 case pn_Mod_X_regular:
3254 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3256 case pn_Mod_X_except:
3257 /* we found an exception handler, remove it */
3258 DBG_OPT_EXC_REM(proj);
3262 res = get_Mod_mem(mod);
3263 new_mem = get_irg_no_mem(current_ir_graph);
3266 /* This node can only float up to the Confirm block */
3267 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3269 set_irn_pinned(mod, op_pin_state_floats);
3270 /* this is a Mod without exception, we can remove the memory edge */
3271 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3274 if (get_Mod_left(mod) == b) {
3275 /* a % a = 0 if a != 0 */
3276 ir_mode *mode = get_irn_mode(proj);
3277 ir_node *res = new_Const(mode, get_mode_null(mode));
3279 DBG_OPT_CSTEVAL(mod, res);
3285 } /* transform_node_Proj_Mod */
3288 * Transform a Proj(DivMod) with a non-zero value.
3289 * Removes the exceptions and routes the memory to the NoMem node.
3291 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3292 ir_node *divmod = get_Proj_pred(proj);
3293 ir_node *b = get_DivMod_right(divmod);
3294 ir_node *confirm, *res, *new_mem;
3297 if (value_not_zero(b, &confirm)) {
3298 /* DivMod(x, y) && y != 0 */
3299 proj_nr = get_Proj_proj(proj);
3303 case pn_DivMod_X_regular:
3304 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3306 case pn_DivMod_X_except:
3307 /* we found an exception handler, remove it */
3308 DBG_OPT_EXC_REM(proj);
3312 res = get_DivMod_mem(divmod);
3313 new_mem = get_irg_no_mem(current_ir_graph);
3316 /* This node can only float up to the Confirm block */
3317 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3319 set_irn_pinned(divmod, op_pin_state_floats);
3320 /* this is a DivMod without exception, we can remove the memory edge */
3321 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3324 case pn_DivMod_res_mod:
3325 if (get_DivMod_left(divmod) == b) {
3326 /* a % a = 0 if a != 0 */
3327 ir_mode *mode = get_irn_mode(proj);
3328 ir_node *res = new_Const(mode, get_mode_null(mode));
3330 DBG_OPT_CSTEVAL(divmod, res);
3336 } /* transform_node_Proj_DivMod */
3339 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3341 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3342 if (get_opt_unreachable_code()) {
3343 ir_node *n = get_Proj_pred(proj);
3344 ir_node *b = get_Cond_selector(n);
3346 if (mode_is_int(get_irn_mode(b))) {
3347 tarval *tb = value_of(b);
3349 if (tb != tarval_bad) {
3350 /* we have a constant switch */
3351 long num = get_Proj_proj(proj);
3353 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3354 if (get_tarval_long(tb) == num) {
3355 /* Do NOT create a jump here, or we will have 2 control flow ops
3356 * in a block. This case is optimized away in optimize_cf(). */
3359 /* this case will NEVER be taken, kill it */
3367 } /* transform_node_Proj_Cond */
3370 * Normalizes and optimizes Cmp nodes.
3372 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3373 ir_node *n = get_Proj_pred(proj);
3374 ir_node *left = get_Cmp_left(n);
3375 ir_node *right = get_Cmp_right(n);
3379 ir_mode *mode = NULL;
3380 long proj_nr = get_Proj_proj(proj);
3382 /* we can evaluate this direct */
3385 return new_Const(mode_b, get_tarval_b_false());
3387 return new_Const(mode_b, get_tarval_b_true());
3389 if(!mode_is_float(get_irn_mode(left)))
3390 return new_Const(mode_b, get_tarval_b_true());
3396 /* Remove unnecessary conversions */
3397 /* TODO handle constants */
3398 if (is_Conv(left) && is_Conv(right)) {
3399 ir_mode *mode = get_irn_mode(left);
3400 ir_node *op_left = get_Conv_op(left);
3401 ir_node *op_right = get_Conv_op(right);
3402 ir_mode *mode_left = get_irn_mode(op_left);
3403 ir_mode *mode_right = get_irn_mode(op_right);
3405 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3406 ir_graph *irg = current_ir_graph;
3407 ir_node *block = get_nodes_block(n);
3409 if (mode_left == mode_right) {
3413 } else if (smaller_mode(mode_left, mode_right)) {
3414 left = new_r_Conv(irg, block, op_left, mode_right);
3417 } else if (smaller_mode(mode_right, mode_left)) {
3419 right = new_r_Conv(irg, block, op_right, mode_left);
3425 /* TODO extend to arbitrary constants */
3426 if (is_Conv(left) && is_Const(right) && is_Const_null(right)) {
3427 ir_mode* mode = get_irn_mode(left);
3428 ir_node* op = get_Conv_op(left);
3429 ir_mode* op_mode = get_irn_mode(op);
3431 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3432 (mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3433 ir_node *null = new_Const(op_mode, get_mode_null(op_mode));
3434 set_Cmp_left( n, op);
3435 set_Cmp_right(n, null);
3442 left = get_Cast_op(left);
3444 right = get_Cast_op(right);
3446 /* remove operation of both sides if possible */
3447 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3448 ir_opcode lop = get_irn_opcode(left);
3450 if (lop == get_irn_opcode(right)) {
3451 ir_node *ll, *lr, *rl, *rr;
3453 /* same operation on both sides, try to remove */
3457 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3458 left = get_unop_op(left);
3459 right = get_unop_op(right);
3463 ll = get_Add_left(left);
3464 lr = get_Add_right(left);
3465 rl = get_Add_left(right);
3466 rr = get_Add_right(right);
3469 /* X + a CMP X + b ==> a CMP b */
3473 } else if (ll == rr) {
3474 /* X + a CMP b + X ==> a CMP b */
3478 } else if (lr == rl) {
3479 /* a + X CMP X + b ==> a CMP b */
3483 } else if (lr == rr) {
3484 /* a + X CMP b + X ==> a CMP b */
3491 ll = get_Sub_left(left);
3492 lr = get_Sub_right(left);
3493 rl = get_Sub_left(right);
3494 rr = get_Sub_right(right);
3497 /* X - a CMP X - b ==> a CMP b */
3501 } else if (lr == rr) {
3502 /* a - X CMP b - X ==> a CMP b */
3509 if (get_Rot_right(left) == get_Rot_right(right)) {
3510 /* a ROT X CMP b ROT X */
3511 left = get_Rot_left(left);
3512 right = get_Rot_left(right);
3522 if (get_irn_mode(left) == mode_b) {
3523 ir_graph *irg = current_ir_graph;
3524 ir_node *block = get_nodes_block(n);
3527 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3528 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3529 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3530 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3531 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3532 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3536 if (!get_opt_reassociation())
3540 * First step: normalize the compare op
3541 * by placing the constant on the right side
3542 * or moving the lower address node to the left.
3543 * We ignore the case that both are constants
3544 * this case should be optimized away.
3546 if (is_Const(right)) {
3548 } else if (is_Const(left)) {
3553 proj_nr = get_inversed_pnc(proj_nr);
3555 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3561 proj_nr = get_inversed_pnc(proj_nr);
3566 * Second step: Try to reduce the magnitude
3567 * of a constant. This may help to generate better code
3568 * later and may help to normalize more compares.
3569 * Of course this is only possible for integer values.
3572 mode = get_irn_mode(c);
3573 tv = get_Const_tarval(c);
3575 if (tv != tarval_bad) {
3576 /* the following optimization is possible on modes without Overflow
3577 * on Unary Minus or on == and !=:
3578 * -a CMP c ==> a swap(CMP) -c
3580 * Beware: for two-complement Overflow may occur, so only == and != can
3581 * be optimized, see this:
3582 * -MININT < 0 =/=> MININT > 0 !!!
3584 if (is_Minus(left) &&
3585 (!mode_overflow_on_unary_Minus(mode) ||
3586 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3587 tv = tarval_neg(tv);
3589 if (tv != tarval_bad) {
3590 left = get_Minus_op(left);
3591 proj_nr = get_inversed_pnc(proj_nr);
3594 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3595 tv = tarval_not(tv);
3597 if (tv != tarval_bad) {
3598 left = get_Not_op(left);
3603 /* for integer modes, we have more */
3604 if (mode_is_int(mode)) {
3605 /* Ne includes Unordered which is not possible on integers.
3606 * However, frontends often use this wrong, so fix it here */
3607 if (proj_nr & pn_Cmp_Uo) {
3608 proj_nr &= ~pn_Cmp_Uo;
3609 set_Proj_proj(proj, proj_nr);
3612 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3613 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3614 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3615 tv = tarval_sub(tv, get_mode_one(mode));
3617 if (tv != tarval_bad) {
3618 proj_nr ^= pn_Cmp_Eq;
3622 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3623 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3624 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3625 tv = tarval_add(tv, get_mode_one(mode));
3627 if (tv != tarval_bad) {
3628 proj_nr ^= pn_Cmp_Eq;
3633 /* the following reassociations work only for == and != */
3634 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3636 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3637 if (tarval_is_null(tv) && is_Sub(left)) {
3638 right =get_Sub_right(left);
3639 left = get_Sub_left(left);
3641 tv = value_of(right);
3645 if (tv != tarval_bad) {
3646 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3648 ir_node *c1 = get_Sub_right(left);
3649 tarval *tv2 = value_of(c1);
3651 if (tv2 != tarval_bad) {
3652 tv2 = tarval_add(tv, value_of(c1));
3654 if (tv2 != tarval_bad) {
3655 left = get_Sub_left(left);
3661 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3662 else if (is_Add(left)) {
3663 ir_node *a_l = get_Add_left(left);
3664 ir_node *a_r = get_Add_right(left);
3668 if (is_Const(a_l)) {
3670 tv2 = value_of(a_l);
3673 tv2 = value_of(a_r);
3676 if (tv2 != tarval_bad) {
3677 tv2 = tarval_sub(tv, tv2);
3679 if (tv2 != tarval_bad) {
3686 /* -a == c ==> a == -c, -a != c ==> a != -c */
3687 else if (is_Minus(left)) {
3688 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3690 if (tv2 != tarval_bad) {
3691 left = get_Minus_op(left);
3698 /* the following reassociations work only for <= */
3699 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3700 if (tv != tarval_bad) {
3701 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3702 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3709 * optimization for AND:
3711 * And(x, C) == C ==> And(x, C) != 0
3712 * And(x, C) != C ==> And(X, C) == 0
3714 * if C is a single Bit constant.
3716 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3717 if (tarval_is_single_bit(tv)) {
3718 /* check for Constant's match. We have check hare the tarvals,
3719 because our const might be changed */
3720 ir_node *la = get_And_left(left);
3721 ir_node *ra = get_And_right(left);
3722 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3723 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3724 /* fine: do the transformation */
3725 tv = get_mode_null(get_tarval_mode(tv));
3726 proj_nr ^= pn_Cmp_Leg;
3731 } /* tarval != bad */
3735 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3737 if (changed & 2) /* need a new Const */
3738 right = new_Const(mode, tv);
3740 /* create a new compare */
3741 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3743 set_Proj_pred(proj, n);
3744 set_Proj_proj(proj, proj_nr);
3748 } /* transform_node_Proj_Cmp */
3751 * Does all optimizations on nodes that must be done on it's Proj's
3752 * because of creating new nodes.
3754 static ir_node *transform_node_Proj(ir_node *proj) {
3755 ir_node *n = get_Proj_pred(proj);
3757 switch (get_irn_opcode(n)) {
3759 return transform_node_Proj_Div(proj);
3762 return transform_node_Proj_Mod(proj);
3765 return transform_node_Proj_DivMod(proj);
3768 return transform_node_Proj_Cond(proj);
3771 return transform_node_Proj_Cmp(proj);
3774 /* should not happen, but if it does will be optimized away */
3775 return equivalent_node_Proj(proj);
3781 } /* transform_node_Proj */
3784 * Move Confirms down through Phi nodes.
3786 static ir_node *transform_node_Phi(ir_node *phi) {
3788 ir_mode *mode = get_irn_mode(phi);
3790 if (mode_is_reference(mode)) {
3791 n = get_irn_arity(phi);
3793 /* Beware of Phi0 */
3795 ir_node *pred = get_irn_n(phi, 0);
3796 ir_node *bound, *new_Phi, *block, **in;
3799 if (! is_Confirm(pred))
3802 bound = get_Confirm_bound(pred);
3803 pnc = get_Confirm_cmp(pred);
3805 NEW_ARR_A(ir_node *, in, n);
3806 in[0] = get_Confirm_value(pred);
3808 for (i = 1; i < n; ++i) {
3809 pred = get_irn_n(phi, i);
3811 if (! is_Confirm(pred) ||
3812 get_Confirm_bound(pred) != bound ||
3813 get_Confirm_cmp(pred) != pnc)
3815 in[i] = get_Confirm_value(pred);
3817 /* move the Confirm nodes "behind" the Phi */
3818 block = get_irn_n(phi, -1);
3819 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3820 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3824 } /* transform_node_Phi */
3827 * Returns the operands of a commutative bin-op, if one operand is
3828 * a const, it is returned as the second one.
3830 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3831 ir_node *op_a = get_binop_left(binop);
3832 ir_node *op_b = get_binop_right(binop);
3834 assert(is_op_commutative(get_irn_op(binop)));
3836 if (is_Const(op_a)) {
3843 } /* get_comm_Binop_Ops */
3846 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3847 * Such pattern may arise in bitfield stores.
3849 * value c4 value c4 & c2
3850 * AND c3 AND c1 | c3
3857 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3860 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3863 ir_node *and_l, *c3;
3864 ir_node *value, *c4;
3865 ir_node *new_and, *new_const, *block;
3866 ir_mode *mode = get_irn_mode(or);
3868 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3871 get_comm_Binop_Ops(or, &and, &c1);
3872 if (!is_Const(c1) || !is_And(and))
3875 get_comm_Binop_Ops(and, &or_l, &c2);
3879 tv1 = get_Const_tarval(c1);
3880 tv2 = get_Const_tarval(c2);
3882 tv = tarval_or(tv1, tv2);
3883 if (tarval_is_all_one(tv)) {
3884 /* the AND does NOT clear a bit with isn't set by the OR */
3885 set_Or_left(or, or_l);
3886 set_Or_right(or, c1);
3888 /* check for more */
3895 get_comm_Binop_Ops(or_l, &and_l, &c3);
3896 if (!is_Const(c3) || !is_And(and_l))
3899 get_comm_Binop_Ops(and_l, &value, &c4);
3903 /* ok, found the pattern, check for conditions */
3904 assert(mode == get_irn_mode(and));
3905 assert(mode == get_irn_mode(or_l));
3906 assert(mode == get_irn_mode(and_l));
3908 tv3 = get_Const_tarval(c3);
3909 tv4 = get_Const_tarval(c4);
3911 tv = tarval_or(tv4, tv2);
3912 if (!tarval_is_all_one(tv)) {
3913 /* have at least one 0 at the same bit position */
3917 n_tv4 = tarval_not(tv4);
3918 if (tv3 != tarval_and(tv3, n_tv4)) {
3919 /* bit in the or_mask is outside the and_mask */
3923 n_tv2 = tarval_not(tv2);
3924 if (tv1 != tarval_and(tv1, n_tv2)) {
3925 /* bit in the or_mask is outside the and_mask */
3929 /* ok, all conditions met */
3930 block = get_irn_n(or, -1);
3932 new_and = new_r_And(current_ir_graph, block,
3933 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3935 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3937 set_Or_left(or, new_and);
3938 set_Or_right(or, new_const);
3940 /* check for more */
3942 } /* transform_node_Or_bf_store */
3945 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3947 static ir_node *transform_node_Or_Rot(ir_node *or) {
3948 ir_mode *mode = get_irn_mode(or);
3949 ir_node *shl, *shr, *block;
3950 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3953 if (! mode_is_int(mode))
3956 shl = get_binop_left(or);
3957 shr = get_binop_right(or);
3966 } else if (!is_Shl(shl)) {
3968 } else if (!is_Shr(shr)) {
3971 x = get_Shl_left(shl);
3972 if (x != get_Shr_left(shr))
3975 c1 = get_Shl_right(shl);
3976 c2 = get_Shr_right(shr);
3977 if (is_Const(c1) && is_Const(c2)) {
3978 tv1 = get_Const_tarval(c1);
3979 if (! tarval_is_long(tv1))
3982 tv2 = get_Const_tarval(c2);
3983 if (! tarval_is_long(tv2))
3986 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3987 != get_mode_size_bits(mode))
3990 /* yet, condition met */
3991 block = get_irn_n(or, -1);
3993 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3995 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3997 } else if (is_Sub(c1)) {
4001 if (get_Sub_right(sub) != v)
4004 c1 = get_Sub_left(sub);
4008 tv1 = get_Const_tarval(c1);
4009 if (! tarval_is_long(tv1))
4012 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4015 /* yet, condition met */
4016 block = get_nodes_block(or);
4018 /* a Rot right is not supported, so use a rot left */
4019 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4021 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4023 } else if (is_Sub(c2)) {
4027 c1 = get_Sub_left(sub);
4031 tv1 = get_Const_tarval(c1);
4032 if (! tarval_is_long(tv1))
4035 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4038 /* yet, condition met */
4039 block = get_irn_n(or, -1);
4042 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4044 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4049 } /* transform_node_Or_Rot */
4054 static ir_node *transform_node_Or(ir_node *n) {
4055 ir_node *c, *oldn = n;
4056 ir_node *a = get_Or_left(n);
4057 ir_node *b = get_Or_right(n);
4059 if (is_Not(a) && is_Not(b)) {
4060 /* ~a | ~b = ~(a&b) */
4061 ir_node *block = get_nodes_block(n);
4062 ir_mode *mode = get_irn_mode(n);
4066 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4067 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4068 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4072 /* we can evaluate 2 Projs of the same Cmp */
4073 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4074 ir_node *pred_a = get_Proj_pred(a);
4075 ir_node *pred_b = get_Proj_pred(b);
4076 if (pred_a == pred_b) {
4077 dbg_info *dbgi = get_irn_dbg_info(n);
4078 ir_node *block = get_nodes_block(pred_a);
4079 pn_Cmp pn_a = get_Proj_proj(a);
4080 pn_Cmp pn_b = get_Proj_proj(b);
4081 /* yes, we can simply calculate with pncs */
4082 pn_Cmp new_pnc = pn_a | pn_b;
4084 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4089 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4091 n = transform_node_Or_bf_store(n);
4092 n = transform_node_Or_Rot(n);
4096 n = transform_bitwise_distributive(n, transform_node_Or);
4099 } /* transform_node_Or */
4103 static ir_node *transform_node(ir_node *n);
4106 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4108 * Should be moved to reassociation?
4110 static ir_node *transform_node_shift(ir_node *n) {
4111 ir_node *left, *right;
4112 tarval *tv1, *tv2, *res;
4114 int modulo_shf, flag;
4116 left = get_binop_left(n);
4118 /* different operations */
4119 if (get_irn_op(left) != get_irn_op(n))
4122 right = get_binop_right(n);
4123 tv1 = value_of(right);
4124 if (tv1 == tarval_bad)
4127 tv2 = value_of(get_binop_right(left));
4128 if (tv2 == tarval_bad)
4131 res = tarval_add(tv1, tv2);
4133 /* beware: a simple replacement works only, if res < modulo shift */
4134 mode = get_irn_mode(n);
4138 modulo_shf = get_mode_modulo_shift(mode);
4139 if (modulo_shf > 0) {
4140 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4142 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4148 /* ok, we can replace it */
4149 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4151 in[0] = get_binop_left(left);
4152 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4154 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4156 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4158 return transform_node(irn);
4161 } /* transform_node_shift */
4166 static ir_node *transform_node_Shr(ir_node *n) {
4167 ir_node *c, *oldn = n;
4168 ir_node *a = get_Shr_left(n);
4169 ir_node *b = get_Shr_right(n);
4171 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4172 return transform_node_shift(n);
4173 } /* transform_node_Shr */
4178 static ir_node *transform_node_Shrs(ir_node *n) {
4179 ir_node *c, *oldn = n;
4180 ir_node *a = get_Shrs_left(n);
4181 ir_node *b = get_Shrs_right(n);
4183 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4184 return transform_node_shift(n);
4185 } /* transform_node_Shrs */
4190 static ir_node *transform_node_Shl(ir_node *n) {
4191 ir_node *c, *oldn = n;
4192 ir_node *a = get_Shl_left(n);
4193 ir_node *b = get_Shl_right(n);
4195 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4196 return transform_node_shift(n);
4197 } /* transform_node_Shl */
4200 * Remove dead blocks and nodes in dead blocks
4201 * in keep alive list. We do not generate a new End node.
4203 static ir_node *transform_node_End(ir_node *n) {
4204 int i, j, n_keepalives = get_End_n_keepalives(n);
4207 NEW_ARR_A(ir_node *, in, n_keepalives);
4209 for (i = j = 0; i < n_keepalives; ++i) {
4210 ir_node *ka = get_End_keepalive(n, i);
4212 if (! is_Block_dead(ka)) {
4216 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4219 /* FIXME: beabi need to keep a Proj(M) */
4220 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4223 if (j != n_keepalives)
4224 set_End_keepalives(n, j, in);
4226 } /* transform_node_End */
4228 /** returns 1 if a == -b */
4229 static int is_negated_value(ir_node *a, ir_node *b) {
4230 if(is_Minus(a) && get_Minus_op(a) == b)
4232 if(is_Minus(b) && get_Minus_op(b) == a)
4234 if(is_Sub(a) && is_Sub(b)) {
4235 ir_node *a_left = get_Sub_left(a);
4236 ir_node *a_right = get_Sub_right(a);
4237 ir_node *b_left = get_Sub_left(b);
4238 ir_node *b_right = get_Sub_right(b);
4240 if(a_left == b_right && a_right == b_left)
4248 * Optimize a Mux into some simpler cases.
4250 static ir_node *transform_node_Mux(ir_node *n) {
4251 ir_node *oldn = n, *sel = get_Mux_sel(n);
4252 ir_mode *mode = get_irn_mode(n);
4254 if (mode == mode_b) {
4255 ir_node *t = get_Mux_true(n);
4256 ir_node *f = get_Mux_false(n);
4257 dbg_info *dbg = get_irn_dbg_info(n);
4258 ir_node *block = get_irn_n(n, -1);
4259 ir_graph *irg = current_ir_graph;
4262 tarval *tv_t = get_Const_tarval(t);
4263 if (tv_t == tarval_b_true) {
4265 assert(get_Const_tarval(f) == tarval_b_false);
4268 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4271 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4272 assert(tv_t == tarval_b_false);
4274 assert(get_Const_tarval(f) == tarval_b_true);
4277 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4280 } else if (is_Const(f)) {
4281 tarval *tv_f = get_Const_tarval(f);
4282 if (tv_f == tarval_b_true) {
4283 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4284 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4286 assert(tv_f == tarval_b_false);
4287 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4292 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4293 ir_node *cmp = get_Proj_pred(sel);
4294 long pn = get_Proj_proj(sel);
4295 ir_node *f = get_Mux_false(n);
4296 ir_node *t = get_Mux_true(n);
4299 * Note: normalization puts the constant on the right side,
4300 * so we check only one case.
4302 * Note further that these optimization work even for floating point
4303 * with NaN's because -NaN == NaN.
4304 * However, if +0 and -0 is handled differently, we cannot use the first
4308 ir_node *cmp_r = get_Cmp_right(cmp);
4309 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4310 ir_node *block = get_irn_n(n, -1);
4312 if(is_negated_value(f, t)) {
4313 ir_node *cmp_left = get_Cmp_left(cmp);
4315 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4316 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4317 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4319 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4321 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4323 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4324 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4325 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4327 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4329 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4331 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4336 if (mode_is_int(mode) && mode_is_signed(mode) &&
4337 get_mode_arithmetic(mode) == irma_twos_complement) {
4338 ir_node *x = get_Cmp_left(cmp);
4340 /* the following optimization works only with signed integer two-complement mode */
4342 if (mode == get_irn_mode(x)) {
4344 * FIXME: this restriction is two rigid, as it would still
4345 * work if mode(x) = Hs and mode == Is, but at least it removes
4348 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4349 is_Const(t) && is_Const_all_one(t) &&
4350 is_Const(f) && is_Const_null(f)) {
4352 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4356 n = new_rd_Shrs(get_irn_dbg_info(n),
4357 current_ir_graph, block, x,
4358 new_r_Const_long(current_ir_graph, block, mode_Iu,
4359 get_mode_size_bits(mode) - 1),
4361 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4363 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4364 is_Const(t) && is_Const_one(t) &&
4365 is_Const(f) && is_Const_null(f)) {
4367 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4371 n = new_rd_Shr(get_irn_dbg_info(n),
4372 current_ir_graph, block,
4373 new_r_Minus(current_ir_graph, block, x, mode),
4374 new_r_Const_long(current_ir_graph, block, mode_Iu,
4375 get_mode_size_bits(mode) - 1),
4377 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4385 return arch_transform_node_Mux(n);
4386 } /* transform_node_Mux */
4389 * Optimize a Psi into some simpler cases.
4391 static ir_node *transform_node_Psi(ir_node *n) {
4393 return transform_node_Mux(n);
4396 } /* transform_node_Psi */
4399 * Tries several [inplace] [optimizing] transformations and returns an
4400 * equivalent node. The difference to equivalent_node() is that these
4401 * transformations _do_ generate new nodes, and thus the old node must
4402 * not be freed even if the equivalent node isn't the old one.
4404 static ir_node *transform_node(ir_node *n) {
4408 * Transform_node is the only "optimizing transformation" that might
4409 * return a node with a different opcode. We iterate HERE until fixpoint
4410 * to get the final result.
4414 if (n->op->ops.transform_node)
4415 n = n->op->ops.transform_node(n);
4416 } while (oldn != n);
4419 } /* transform_node */
4422 * Sets the default transform node operation for an ir_op_ops.
4424 * @param code the opcode for the default operation
4425 * @param ops the operations initialized
4430 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4434 ops->transform_node = transform_node_##a; \
4468 } /* firm_set_default_transform_node */
4471 /* **************** Common Subexpression Elimination **************** */
4473 /** The size of the hash table used, should estimate the number of nodes
4475 #define N_IR_NODES 512
4477 /** Compares the attributes of two Const nodes. */
4478 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4479 return (get_Const_tarval(a) != get_Const_tarval(b))
4480 || (get_Const_type(a) != get_Const_type(b));
4481 } /* node_cmp_attr_Const */
4483 /** Compares the attributes of two Proj nodes. */
4484 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4485 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4486 } /* node_cmp_attr_Proj */
4488 /** Compares the attributes of two Filter nodes. */
4489 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4490 return get_Filter_proj(a) != get_Filter_proj(b);
4491 } /* node_cmp_attr_Filter */
4493 /** Compares the attributes of two Alloc nodes. */
4494 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4495 const alloc_attr *pa = get_irn_alloc_attr(a);
4496 const alloc_attr *pb = get_irn_alloc_attr(b);
4497 return (pa->where != pb->where) || (pa->type != pb->type);
4498 } /* node_cmp_attr_Alloc */
4500 /** Compares the attributes of two Free nodes. */
4501 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4502 const free_attr *pa = get_irn_free_attr(a);
4503 const free_attr *pb = get_irn_free_attr(b);
4504 return (pa->where != pb->where) || (pa->type != pb->type);
4505 } /* node_cmp_attr_Free */
4507 /** Compares the attributes of two SymConst nodes. */
4508 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4509 const symconst_attr *pa = get_irn_symconst_attr(a);
4510 const symconst_attr *pb = get_irn_symconst_attr(b);
4511 return (pa->num != pb->num)
4512 || (pa->sym.type_p != pb->sym.type_p)
4513 || (pa->tp != pb->tp);
4514 } /* node_cmp_attr_SymConst */
4516 /** Compares the attributes of two Call nodes. */
4517 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4518 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4519 } /* node_cmp_attr_Call */
4521 /** Compares the attributes of two Sel nodes. */
4522 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4523 const ir_entity *a_ent = get_Sel_entity(a);
4524 const ir_entity *b_ent = get_Sel_entity(b);
4526 (a_ent->kind != b_ent->kind) ||
4527 (a_ent->name != b_ent->name) ||
4528 (a_ent->owner != b_ent->owner) ||
4529 (a_ent->ld_name != b_ent->ld_name) ||
4530 (a_ent->type != b_ent->type);
4531 } /* node_cmp_attr_Sel */
4533 /** Compares the attributes of two Phi nodes. */
4534 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4535 /* we can only enter this function if both nodes have the same number of inputs,
4536 hence it is enough to check if one of them is a Phi0 */
4538 /* check the Phi0 attribute */
4539 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4542 } /* node_cmp_attr_Phi */
4544 /** Compares the attributes of two Conv nodes. */
4545 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4546 return get_Conv_strict(a) != get_Conv_strict(b);
4547 } /* node_cmp_attr_Conv */
4549 /** Compares the attributes of two Cast nodes. */
4550 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4551 return get_Cast_type(a) != get_Cast_type(b);
4552 } /* node_cmp_attr_Cast */
4554 /** Compares the attributes of two Load nodes. */
4555 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4556 if (get_Load_volatility(a) == volatility_is_volatile ||
4557 get_Load_volatility(b) == volatility_is_volatile)
4558 /* NEVER do CSE on volatile Loads */
4560 /* do not CSE Loads with different alignment. Be conservative. */
4561 if (get_Load_align(a) != get_Load_align(b))
4564 return get_Load_mode(a) != get_Load_mode(b);
4565 } /* node_cmp_attr_Load */
4567 /** Compares the attributes of two Store nodes. */
4568 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4569 /* do not CSE Stores with different alignment. Be conservative. */
4570 if (get_Store_align(a) != get_Store_align(b))
4573 /* NEVER do CSE on volatile Stores */
4574 return (get_Store_volatility(a) == volatility_is_volatile ||
4575 get_Store_volatility(b) == volatility_is_volatile);
4576 } /* node_cmp_attr_Store */
4578 /** Compares the attributes of two Confirm nodes. */
4579 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4580 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4581 } /* node_cmp_attr_Confirm */
4583 /** Compares the attributes of two ASM nodes. */
4584 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4586 const ir_asm_constraint *ca;
4587 const ir_asm_constraint *cb;
4590 if (get_ASM_text(a) != get_ASM_text(b))
4593 /* Should we really check the constraints here? Should be better, but is strange. */
4594 n = get_ASM_n_input_constraints(a);
4595 if (n != get_ASM_n_input_constraints(b))
4598 ca = get_ASM_input_constraints(a);
4599 cb = get_ASM_input_constraints(b);
4600 for (i = 0; i < n; ++i) {
4601 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4605 n = get_ASM_n_output_constraints(a);
4606 if (n != get_ASM_n_output_constraints(b))
4609 ca = get_ASM_output_constraints(a);
4610 cb = get_ASM_output_constraints(b);
4611 for (i = 0; i < n; ++i) {
4612 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4616 n = get_ASM_n_clobbers(a);
4617 if (n != get_ASM_n_clobbers(b))
4620 cla = get_ASM_clobbers(a);
4621 clb = get_ASM_clobbers(b);
4622 for (i = 0; i < n; ++i) {
4623 if (cla[i] != clb[i])
4627 } /* node_cmp_attr_ASM */
4630 * Set the default node attribute compare operation for an ir_op_ops.
4632 * @param code the opcode for the default operation
4633 * @param ops the operations initialized
4638 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4642 ops->node_cmp_attr = node_cmp_attr_##a; \
4667 } /* firm_set_default_node_cmp_attr */
4670 * Compare function for two nodes in the hash table. Gets two
4671 * nodes as parameters. Returns 0 if the nodes are a cse.
4673 int identities_cmp(const void *elt, const void *key) {
4680 if (a == b) return 0;
4682 if ((get_irn_op(a) != get_irn_op(b)) ||
4683 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4685 /* compare if a's in and b's in are of equal length */
4686 irn_arity_a = get_irn_intra_arity (a);
4687 if (irn_arity_a != get_irn_intra_arity(b))
4690 /* for block-local cse and op_pin_state_pinned nodes: */
4691 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4692 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4696 /* compare a->in[0..ins] with b->in[0..ins] */
4697 for (i = 0; i < irn_arity_a; i++)
4698 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4702 * here, we already now that the nodes are identical except their
4705 if (a->op->ops.node_cmp_attr)
4706 return a->op->ops.node_cmp_attr(a, b);
4709 } /* identities_cmp */
4712 * Calculate a hash value of a node.
4714 unsigned ir_node_hash(ir_node *node) {
4718 if (node->op == op_Const) {
4719 /* special value for const, as they only differ in their tarval. */
4720 h = HASH_PTR(node->attr.con.tv);
4721 h = 9*h + HASH_PTR(get_irn_mode(node));
4722 } else if (node->op == op_SymConst) {
4723 /* special value for const, as they only differ in their symbol. */
4724 h = HASH_PTR(node->attr.symc.sym.type_p);
4725 h = 9*h + HASH_PTR(get_irn_mode(node));
4728 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4729 h = irn_arity = get_irn_intra_arity(node);
4731 /* consider all in nodes... except the block if not a control flow. */
4732 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4733 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4737 h = 9*h + HASH_PTR(get_irn_mode(node));
4739 h = 9*h + HASH_PTR(get_irn_op(node));
4743 } /* ir_node_hash */
4745 pset *new_identities(void) {
4746 return new_pset(identities_cmp, N_IR_NODES);
4747 } /* new_identities */
4749 void del_identities(pset *value_table) {
4750 del_pset(value_table);
4751 } /* del_identities */
4754 * Normalize a node by putting constants (and operands with smaller
4755 * node index) on the right
4757 * @param n The node to normalize
4759 static void normalize_node(ir_node *n) {
4760 if (get_opt_reassociation()) {
4761 if (is_op_commutative(get_irn_op(n))) {
4762 ir_node *l = get_binop_left(n);
4763 ir_node *r = get_binop_right(n);
4764 int l_idx = get_irn_idx(l);
4765 int r_idx = get_irn_idx(r);
4767 /* For commutative operators perform a OP b == b OP a but keep
4768 constants on the RIGHT side. This helps greatly in some optimizations.
4769 Moreover we use the idx number to make the form deterministic. */
4770 if (is_irn_constlike(l))
4772 if (is_irn_constlike(r))
4774 if (l_idx < r_idx) {
4775 set_binop_left(n, r);
4776 set_binop_right(n, l);
4780 } /* normalize_node */
4783 * Return the canonical node computing the same value as n.
4785 * @param value_table The value table
4786 * @param n The node to lookup
4788 * Looks up the node in a hash table.
4790 * For Const nodes this is performed in the constructor, too. Const
4791 * nodes are extremely time critical because of their frequent use in
4792 * constant string arrays.
4794 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4797 if (!value_table) return n;
4801 o = pset_find(value_table, n, ir_node_hash(n));
4810 * During construction we set the op_pin_state_pinned flag in the graph right when the
4811 * optimization is performed. The flag turning on procedure global cse could
4812 * be changed between two allocations. This way we are safe.
4814 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4817 n = identify(value_table, n);
4818 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4819 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4821 } /* identify_cons */
4824 * Return the canonical node computing the same value as n.
4825 * Looks up the node in a hash table, enters it in the table
4826 * if it isn't there yet.
4828 ir_node *identify_remember(pset *value_table, ir_node *n) {
4831 if (!value_table) return n;
4834 /* lookup or insert in hash table with given hash key. */
4835 o = pset_insert(value_table, n, ir_node_hash(n));
4842 } /* identify_remember */
4844 /* Add a node to the identities value table. */
4845 void add_identities(pset *value_table, ir_node *node) {
4846 if (get_opt_cse() && is_no_Block(node))
4847 identify_remember(value_table, node);
4848 } /* add_identities */
4850 /* Visit each node in the value table of a graph. */
4851 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4853 ir_graph *rem = current_ir_graph;
4855 current_ir_graph = irg;
4856 foreach_pset(irg->value_table, node)
4858 current_ir_graph = rem;
4859 } /* visit_all_identities */
4862 * Garbage in, garbage out. If a node has a dead input, i.e., the
4863 * Bad node is input to the node, return the Bad node.
4865 static ir_node *gigo(ir_node *node) {
4867 ir_op *op = get_irn_op(node);
4869 /* remove garbage blocks by looking at control flow that leaves the block
4870 and replacing the control flow by Bad. */
4871 if (get_irn_mode(node) == mode_X) {
4872 ir_node *block = get_nodes_block(skip_Proj(node));
4874 /* Don't optimize nodes in immature blocks. */
4875 if (!get_Block_matured(block)) return node;
4876 /* Don't optimize End, may have Bads. */
4877 if (op == op_End) return node;
4879 if (is_Block(block)) {
4880 irn_arity = get_irn_arity(block);
4881 for (i = 0; i < irn_arity; i++) {
4882 if (!is_Bad(get_irn_n(block, i)))
4885 if (i == irn_arity) {
4886 ir_graph *irg = get_irn_irg(block);
4887 /* the start block is never dead */
4888 if (block != get_irg_start_block(irg)
4889 && block != get_irg_end_block(irg))
4895 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4896 blocks predecessors is dead. */
4897 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4898 irn_arity = get_irn_arity(node);
4901 * Beware: we can only read the block of a non-floating node.
4903 if (is_irn_pinned_in_irg(node) &&
4904 is_Block_dead(get_nodes_block(node)))
4907 for (i = 0; i < irn_arity; i++) {
4908 ir_node *pred = get_irn_n(node, i);
4913 /* Propagating Unknowns here seems to be a bad idea, because
4914 sometimes we need a node as a input and did not want that
4916 However, it might be useful to move this into a later phase
4917 (if you think that optimizing such code is useful). */
4918 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4919 return new_Unknown(get_irn_mode(node));
4924 /* With this code we violate the agreement that local_optimize
4925 only leaves Bads in Block, Phi and Tuple nodes. */
4926 /* If Block has only Bads as predecessors it's garbage. */
4927 /* If Phi has only Bads as predecessors it's garbage. */
4928 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4929 irn_arity = get_irn_arity(node);
4930 for (i = 0; i < irn_arity; i++) {
4931 if (!is_Bad(get_irn_n(node, i))) break;
4933 if (i == irn_arity) node = new_Bad();
4940 * These optimizations deallocate nodes from the obstack.
4941 * It can only be called if it is guaranteed that no other nodes
4942 * reference this one, i.e., right after construction of a node.
4944 * @param n The node to optimize
4946 * current_ir_graph must be set to the graph of the node!
4948 ir_node *optimize_node(ir_node *n) {
4951 ir_opcode iro = get_irn_opcode(n);
4953 /* Always optimize Phi nodes: part of the construction. */
4954 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4956 /* constant expression evaluation / constant folding */
4957 if (get_opt_constant_folding()) {
4958 /* neither constants nor Tuple values can be evaluated */
4959 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4960 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4961 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4962 /* try to evaluate */
4963 tv = computed_value(n);
4964 if (tv != tarval_bad) {
4966 ir_type *old_tp = get_irn_type(n);
4967 int i, arity = get_irn_arity(n);
4971 * Try to recover the type of the new expression.
4973 for (i = 0; i < arity && !old_tp; ++i)
4974 old_tp = get_irn_type(get_irn_n(n, i));
4977 * we MUST copy the node here temporary, because it's still needed
4978 * for DBG_OPT_CSTEVAL
4980 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4981 oldn = alloca(node_size);
4983 memcpy(oldn, n, node_size);
4984 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4986 /* ARG, copy the in array, we need it for statistics */
4987 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4989 /* note the inplace edges module */
4990 edges_node_deleted(n, current_ir_graph);
4992 /* evaluation was successful -- replace the node. */
4993 irg_kill_node(current_ir_graph, n);
4994 nw = new_Const(get_tarval_mode(tv), tv);
4996 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4997 set_Const_type(nw, old_tp);
4998 DBG_OPT_CSTEVAL(oldn, nw);
4999 tarval_enable_fp_ops(old_fp_mode);
5002 tarval_enable_fp_ops(old_fp_mode);
5006 /* remove unnecessary nodes */
5007 if (get_opt_constant_folding() ||
5008 (iro == iro_Phi) || /* always optimize these nodes. */
5010 (iro == iro_Proj) ||
5011 (iro == iro_Block) ) /* Flags tested local. */
5012 n = equivalent_node(n);
5014 /* Common Subexpression Elimination.
5016 * Checks whether n is already available.
5017 * The block input is used to distinguish different subexpressions. Right
5018 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5019 * subexpressions within a block.
5022 n = identify_cons(current_ir_graph->value_table, n);
5025 edges_node_deleted(oldn, current_ir_graph);
5027 /* We found an existing, better node, so we can deallocate the old node. */
5028 irg_kill_node(current_ir_graph, oldn);
5032 /* Some more constant expression evaluation that does not allow to
5034 iro = get_irn_opcode(n);
5035 if (get_opt_constant_folding() ||
5036 (iro == iro_Cond) ||
5037 (iro == iro_Proj)) /* Flags tested local. */
5038 n = transform_node(n);
5040 /* Remove nodes with dead (Bad) input.
5041 Run always for transformation induced Bads. */
5044 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5045 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5046 n = identify_remember(current_ir_graph->value_table, n);
5050 } /* optimize_node */
5054 * These optimizations never deallocate nodes (in place). This can cause dead
5055 * nodes lying on the obstack. Remove these by a dead node elimination,
5056 * i.e., a copying garbage collection.
5058 ir_node *optimize_in_place_2(ir_node *n) {
5061 ir_opcode iro = get_irn_opcode(n);
5063 if (!get_opt_optimize() && !is_Phi(n)) return n;
5065 /* constant expression evaluation / constant folding */
5066 if (get_opt_constant_folding()) {
5067 /* neither constants nor Tuple values can be evaluated */
5068 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5069 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5070 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5071 /* try to evaluate */
5072 tv = computed_value(n);
5073 if (tv != tarval_bad) {
5074 /* evaluation was successful -- replace the node. */
5075 ir_type *old_tp = get_irn_type(n);
5076 int i, arity = get_irn_arity(n);
5079 * Try to recover the type of the new expression.
5081 for (i = 0; i < arity && !old_tp; ++i)
5082 old_tp = get_irn_type(get_irn_n(n, i));
5084 n = new_Const(get_tarval_mode(tv), tv);
5086 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5087 set_Const_type(n, old_tp);
5089 DBG_OPT_CSTEVAL(oldn, n);
5090 tarval_enable_fp_ops(old_fp_mode);
5093 tarval_enable_fp_ops(old_fp_mode);
5097 /* remove unnecessary nodes */
5098 if (get_opt_constant_folding() ||
5099 (iro == iro_Phi) || /* always optimize these nodes. */
5100 (iro == iro_Id) || /* ... */
5101 (iro == iro_Proj) || /* ... */
5102 (iro == iro_Block) ) /* Flags tested local. */
5103 n = equivalent_node(n);
5105 /** common subexpression elimination **/
5106 /* Checks whether n is already available. */
5107 /* The block input is used to distinguish different subexpressions. Right
5108 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5109 subexpressions within a block. */
5110 if (get_opt_cse()) {
5111 n = identify(current_ir_graph->value_table, n);
5114 /* Some more constant expression evaluation. */
5115 iro = get_irn_opcode(n);
5116 if (get_opt_constant_folding() ||
5117 (iro == iro_Cond) ||
5118 (iro == iro_Proj)) /* Flags tested local. */
5119 n = transform_node(n);
5121 /* Remove nodes with dead (Bad) input.
5122 Run always for transformation induced Bads. */
5125 /* Now we can verify the node, as it has no dead inputs any more. */
5128 /* Now we have a legal, useful node. Enter it in hash table for cse.
5129 Blocks should be unique anyways. (Except the successor of start:
5130 is cse with the start block!) */
5131 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5132 n = identify_remember(current_ir_graph->value_table, n);
5135 } /* optimize_in_place_2 */
5138 * Wrapper for external use, set proper status bits after optimization.
5140 ir_node *optimize_in_place(ir_node *n) {
5141 /* Handle graph state */
5142 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5144 if (get_opt_global_cse())
5145 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5146 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5147 set_irg_outs_inconsistent(current_ir_graph);
5149 /* FIXME: Maybe we could also test whether optimizing the node can
5150 change the control graph. */
5151 set_irg_doms_inconsistent(current_ir_graph);
5152 return optimize_in_place_2(n);
5153 } /* optimize_in_place */
5156 * Sets the default operation for an ir_ops.
5158 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5159 ops = firm_set_default_computed_value(code, ops);
5160 ops = firm_set_default_equivalent_node(code, ops);
5161 ops = firm_set_default_transform_node(code, ops);
5162 ops = firm_set_default_node_cmp_attr(code, ops);
5163 ops = firm_set_default_get_type(code, ops);
5164 ops = firm_set_default_get_type_attr(code, ops);
5165 ops = firm_set_default_get_entity_attr(code, ops);
5168 } /* firm_set_default_operations */