2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Return the value of a Constant.
59 static tarval *computed_value_Const(ir_node *n) {
60 return get_Const_tarval(n);
61 } /* computed_value_Const */
64 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
66 static tarval *computed_value_SymConst(ir_node *n) {
70 switch (get_SymConst_kind(n)) {
71 case symconst_type_size:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
76 case symconst_type_align:
77 type = get_SymConst_type(n);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
81 case symconst_ofs_ent:
82 ent = get_SymConst_entity(n);
83 type = get_entity_owner(ent);
84 if (get_type_state(type) == layout_fixed)
85 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
91 } /* computed_value_SymConst */
94 * Return the value of an Add.
96 static tarval *computed_value_Add(ir_node *n) {
97 ir_node *a = get_Add_left(n);
98 ir_node *b = get_Add_right(n);
100 tarval *ta = value_of(a);
101 tarval *tb = value_of(b);
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_add(ta, tb);
107 } /* computed_value_Add */
110 * Return the value of a Sub.
111 * Special case: a - a
113 static tarval *computed_value_Sub(ir_node *n) {
114 ir_node *a = get_Sub_left(n);
115 ir_node *b = get_Sub_right(n);
120 if (a == b && !is_Bad(a))
121 return get_mode_null(get_irn_mode(n));
126 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127 return tarval_sub(ta, tb);
130 } /* computed_value_Sub */
133 * Return the value of a Carry.
134 * Special : a op 0, 0 op b
136 static tarval *computed_value_Carry(ir_node *n) {
137 ir_node *a = get_binop_left(n);
138 ir_node *b = get_binop_right(n);
139 ir_mode *m = get_irn_mode(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad)) {
146 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
148 if (tarval_is_null(ta) || tarval_is_null(tb))
149 return get_mode_null(m);
152 } /* computed_value_Carry */
155 * Return the value of a Borrow.
158 static tarval *computed_value_Borrow(ir_node *n) {
159 ir_node *a = get_binop_left(n);
160 ir_node *b = get_binop_right(n);
161 ir_mode *m = get_irn_mode(n);
163 tarval *ta = value_of(a);
164 tarval *tb = value_of(b);
166 if ((ta != tarval_bad) && (tb != tarval_bad)) {
167 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
168 } else if (tarval_is_null(ta)) {
169 return get_mode_null(m);
172 } /* computed_value_Borrow */
175 * Return the value of an unary Minus.
177 static tarval *computed_value_Minus(ir_node *n) {
178 ir_node *a = get_Minus_op(n);
179 tarval *ta = value_of(a);
181 if (ta != tarval_bad)
182 return tarval_neg(ta);
185 } /* computed_value_Minus */
188 * Return the value of a Mul.
190 static tarval *computed_value_Mul(ir_node *n) {
191 ir_node *a = get_Mul_left(n);
192 ir_node *b = get_Mul_right(n);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 mode = get_irn_mode(n);
199 if (mode != get_irn_mode(a)) {
200 /* n * n = 2n bit multiplication */
201 ta = tarval_convert_to(ta, mode);
202 tb = tarval_convert_to(tb, mode);
205 if (ta != tarval_bad && tb != tarval_bad) {
206 return tarval_mul(ta, tb);
208 /* a*0 = 0 or 0*b = 0 */
209 if (ta == get_mode_null(mode))
211 if (tb == get_mode_null(mode))
215 } /* computed_value_Mul */
218 * Return the value of a floating point Quot.
220 static tarval *computed_value_Quot(ir_node *n) {
221 ir_node *a = get_Quot_left(n);
222 ir_node *b = get_Quot_right(n);
224 tarval *ta = value_of(a);
225 tarval *tb = value_of(b);
227 /* This was missing in original implementation. Why? */
228 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
229 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
230 return tarval_quo(ta, tb);
233 } /* computed_value_Quot */
236 * Calculate the value of an integer Div of two nodes.
237 * Special case: 0 / b
239 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
240 tarval *ta = value_of(a);
241 tarval *tb = value_of(b);
243 /* Compute c1 / c2 or 0 / a, a != 0 */
244 if (ta != tarval_bad) {
245 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
246 return tarval_div(ta, tb);
247 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
251 } /* do_computed_value_Div */
254 * Return the value of an integer Div.
256 static tarval *computed_value_Div(ir_node *n) {
257 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
258 } /* computed_value_Div */
261 * Calculate the value of an integer Mod of two nodes.
262 * Special case: a % 1
264 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 /* Compute c1 % c2 or a % 1 */
269 if (tb != tarval_bad) {
270 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
271 return tarval_mod(ta, tb);
272 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
273 return get_mode_null(get_irn_mode(a));
276 } /* do_computed_value_Mod */
279 * Return the value of an integer Mod.
281 static tarval *computed_value_Mod(ir_node *n) {
282 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
283 } /* computed_value_Mod */
286 * Return the value of an Abs.
288 static tarval *computed_value_Abs(ir_node *n) {
289 ir_node *a = get_Abs_op(n);
290 tarval *ta = value_of(a);
292 if (ta != tarval_bad)
293 return tarval_abs(ta);
296 } /* computed_value_Abs */
299 * Return the value of an And.
300 * Special case: a & 0, 0 & b
302 static tarval *computed_value_And(ir_node *n) {
303 ir_node *a = get_And_left(n);
304 ir_node *b = get_And_right(n);
306 tarval *ta = value_of(a);
307 tarval *tb = value_of(b);
309 if ((ta != tarval_bad) && (tb != tarval_bad)) {
310 return tarval_and (ta, tb);
312 if (tarval_is_null(ta)) return ta;
313 if (tarval_is_null(tb)) return tb;
316 } /* computed_value_And */
319 * Return the value of an Or.
320 * Special case: a | 1...1, 1...1 | b
322 static tarval *computed_value_Or(ir_node *n) {
323 ir_node *a = get_Or_left(n);
324 ir_node *b = get_Or_right(n);
326 tarval *ta = value_of(a);
327 tarval *tb = value_of(b);
329 if ((ta != tarval_bad) && (tb != tarval_bad)) {
330 return tarval_or (ta, tb);
332 if (tarval_is_all_one(ta)) return ta;
333 if (tarval_is_all_one(tb)) return tb;
336 } /* computed_value_Or */
339 * Return the value of an Eor.
341 static tarval *computed_value_Eor(ir_node *n) {
342 ir_node *a = get_Eor_left(n);
343 ir_node *b = get_Eor_right(n);
348 return get_mode_null(get_irn_mode(n));
353 if ((ta != tarval_bad) && (tb != tarval_bad)) {
354 return tarval_eor (ta, tb);
357 } /* computed_value_Eor */
360 * Return the value of a Not.
362 static tarval *computed_value_Not(ir_node *n) {
363 ir_node *a = get_Not_op(n);
364 tarval *ta = value_of(a);
366 if (ta != tarval_bad)
367 return tarval_not(ta);
370 } /* computed_value_Not */
373 * Return the value of a Shl.
375 static tarval *computed_value_Shl(ir_node *n) {
376 ir_node *a = get_Shl_left(n);
377 ir_node *b = get_Shl_right(n);
379 tarval *ta = value_of(a);
380 tarval *tb = value_of(b);
382 if ((ta != tarval_bad) && (tb != tarval_bad)) {
383 return tarval_shl (ta, tb);
386 } /* computed_value_Shl */
389 * Return the value of a Shr.
391 static tarval *computed_value_Shr(ir_node *n) {
392 ir_node *a = get_Shr_left(n);
393 ir_node *b = get_Shr_right(n);
395 tarval *ta = value_of(a);
396 tarval *tb = value_of(b);
398 if ((ta != tarval_bad) && (tb != tarval_bad)) {
399 return tarval_shr (ta, tb);
402 } /* computed_value_Shr */
405 * Return the value of a Shrs.
407 static tarval *computed_value_Shrs(ir_node *n) {
408 ir_node *a = get_Shrs_left(n);
409 ir_node *b = get_Shrs_right(n);
411 tarval *ta = value_of(a);
412 tarval *tb = value_of(b);
414 if ((ta != tarval_bad) && (tb != tarval_bad)) {
415 return tarval_shrs (ta, tb);
418 } /* computed_value_Shrs */
421 * Return the value of a Rot.
423 static tarval *computed_value_Rot(ir_node *n) {
424 ir_node *a = get_Rot_left(n);
425 ir_node *b = get_Rot_right(n);
427 tarval *ta = value_of(a);
428 tarval *tb = value_of(b);
430 if ((ta != tarval_bad) && (tb != tarval_bad)) {
431 return tarval_rot (ta, tb);
434 } /* computed_value_Rot */
437 * Return the value of a Conv.
439 static tarval *computed_value_Conv(ir_node *n) {
440 ir_node *a = get_Conv_op(n);
441 tarval *ta = value_of(a);
443 if (ta != tarval_bad)
444 return tarval_convert_to(ta, get_irn_mode(n));
447 } /* computed_value_Conv */
450 * Return the value of a Proj(Cmp).
452 * This performs a first step of unreachable code elimination.
453 * Proj can not be computed, but folding a Cmp above the Proj here is
454 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
456 * There are several case where we can evaluate a Cmp node, see later.
458 static tarval *computed_value_Proj_Cmp(ir_node *n) {
459 ir_node *a = get_Proj_pred(n);
460 ir_node *aa = get_Cmp_left(a);
461 ir_node *ab = get_Cmp_right(a);
462 long proj_nr = get_Proj_proj(n);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
469 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
472 /* This is a trick with the bits used for encoding the Cmp
473 Proj numbers, the following statement is not the same:
474 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
478 tarval *taa = value_of(aa);
479 tarval *tab = value_of(ab);
480 ir_mode *mode = get_irn_mode(aa);
483 * The predecessors of Cmp are target values. We can evaluate
486 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487 /* strange checks... */
488 pn_Cmp flags = tarval_cmp(taa, tab);
489 if (flags != pn_Cmp_False) {
490 return new_tarval_from_long (proj_nr & flags, mode_b);
493 /* for integer values, we can check against MIN/MAX */
494 else if (mode_is_int(mode)) {
495 /* MIN <=/> x. This results in true/false. */
496 if (taa == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Le)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Gt)
501 return get_tarval_b_false();
503 /* x >=/< MIN. This results in true/false. */
505 if (tab == get_mode_min(mode)) {
506 /* a compare with the MIN value */
507 if (proj_nr == pn_Cmp_Ge)
508 return get_tarval_b_true();
509 else if (proj_nr == pn_Cmp_Lt)
510 return get_tarval_b_false();
512 /* MAX >=/< x. This results in true/false. */
513 else if (taa == get_mode_max(mode)) {
514 if (proj_nr == pn_Cmp_Ge)
515 return get_tarval_b_true();
516 else if (proj_nr == pn_Cmp_Lt)
517 return get_tarval_b_false();
519 /* x <=/> MAX. This results in true/false. */
520 else if (tab == get_mode_max(mode)) {
521 if (proj_nr == pn_Cmp_Le)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Gt)
524 return get_tarval_b_false();
528 * The predecessors are Allocs or (void*)(0) constants. Allocs never
529 * return NULL, they raise an exception. Therefore we can predict
533 ir_node *aaa = skip_Id(skip_Proj(aa));
534 ir_node *aba = skip_Id(skip_Proj(ab));
536 if ( ( (/* aa is ProjP and aaa is Alloc */
538 && mode_is_reference(get_irn_mode(aa))
540 && ( (/* ab is NULL */
542 && mode_is_reference(get_irn_mode(ab))
543 && is_Const_null(ab))
544 || (/* ab is other Alloc */
546 && mode_is_reference(get_irn_mode(ab))
549 || (/* aa is NULL and aba is Alloc */
551 && mode_is_reference(get_irn_mode(aa))
554 && mode_is_reference(get_irn_mode(ab))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 } /* computed_value_Proj_Cmp */
564 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565 * Proj(DivMod) and Proj(Quot).
567 static tarval *computed_value_Proj(ir_node *n) {
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(a);
595 if (get_Proj_proj(n) == pn_Quot_res)
596 return computed_value(a);
603 } /* computed_value_Proj */
606 * Calculate the value of a Mux: can be evaluated, if the
607 * sel and the right input are known.
609 static tarval *computed_value_Mux(ir_node *n) {
610 ir_node *sel = get_Mux_sel(n);
611 tarval *ts = value_of(sel);
613 if (ts == get_tarval_b_true()) {
614 ir_node *v = get_Mux_true(n);
617 else if (ts == get_tarval_b_false()) {
618 ir_node *v = get_Mux_false(n);
622 } /* computed_value_Mux */
625 * Calculate the value of a Psi: can be evaluated, if a condition is true
626 * and all previous conditions are false. If all conditions are false
627 * we evaluate to the default one.
629 static tarval *computed_value_Psi(ir_node *n) {
631 return computed_value_Mux(n);
633 } /* computed_value_Psi */
636 * Calculate the value of a Confirm: can be evaluated,
637 * if it has the form Confirm(x, '=', Const).
639 static tarval *computed_value_Confirm(ir_node *n) {
640 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641 value_of(get_Confirm_bound(n)) : tarval_bad;
642 } /* computed_value_Confirm */
645 * If the parameter n can be computed, return its value, else tarval_bad.
646 * Performs constant folding.
648 * @param n The node this should be evaluated
650 tarval *computed_value(ir_node *n) {
651 if (n->op->ops.computed_value)
652 return n->op->ops.computed_value(n);
654 } /* computed_value */
657 * Set the default computed_value evaluator in an ir_op_ops.
659 * @param code the opcode for the default operation
660 * @param ops the operations initialized
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
669 ops->computed_value = computed_value_##a; \
704 } /* firm_set_default_computed_value */
707 * Returns a equivalent block for another block.
708 * If the block has only one predecessor, this is
709 * the equivalent one. If the only predecessor of a block is
710 * the block itself, this is a dead block.
712 * If both predecessors of a block are the branches of a binary
713 * Cond, the equivalent block is Cond's block.
715 * If all predecessors of a block are bad or lies in a dead
716 * block, the current block is dead as well.
718 * Note, that blocks are NEVER turned into Bad's, instead
719 * the dead_block flag is set. So, never test for is_Bad(block),
720 * always use is_dead_Block(block).
722 static ir_node *equivalent_node_Block(ir_node *n)
725 int n_preds = get_Block_n_cfgpreds(n);
727 /* The Block constructor does not call optimize, but mature_immBlock
728 calls the optimization. */
729 assert(get_Block_matured(n));
731 /* Straightening: a single entry Block following a single exit Block
732 can be merged, if it is not the Start block. */
733 /* !!! Beware, all Phi-nodes of n must have been optimized away.
734 This should be true, as the block is matured before optimize is called.
735 But what about Phi-cycles with the Phi0/Id that could not be resolved?
736 Remaining Phi nodes are just Ids. */
737 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
738 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
739 if (predblock == oldn) {
740 /* Jmp jumps into the block it is in -- deal self cycle. */
741 n = set_Block_dead(n);
742 DBG_OPT_DEAD_BLOCK(oldn, n);
743 } else if (get_opt_control_flow_straightening()) {
745 DBG_OPT_STG(oldn, n);
747 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
748 ir_node *predblock = get_Block_cfgpred_block(n, 0);
749 if (predblock == oldn) {
750 /* Jmp jumps into the block it is in -- deal self cycle. */
751 n = set_Block_dead(n);
752 DBG_OPT_DEAD_BLOCK(oldn, n);
754 } else if ((n_preds == 2) &&
755 (get_opt_control_flow_weak_simplification())) {
756 /* Test whether Cond jumps twice to this block
757 * The more general case which more than 2 predecessors is handles
758 * in optimize_cf(), we handle only this special case for speed here.
760 ir_node *a = get_Block_cfgpred(n, 0);
761 ir_node *b = get_Block_cfgpred(n, 1);
765 (get_Proj_pred(a) == get_Proj_pred(b)) &&
766 is_Cond(get_Proj_pred(a)) &&
767 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
768 /* Also a single entry Block following a single exit Block. Phis have
769 twice the same operand and will be optimized away. */
770 n = get_nodes_block(get_Proj_pred(a));
771 DBG_OPT_IFSIM1(oldn, a, b, n);
773 } else if (get_opt_unreachable_code() &&
774 (n != get_irg_start_block(current_ir_graph)) &&
775 (n != get_irg_end_block(current_ir_graph)) ) {
778 /* If all inputs are dead, this block is dead too, except if it is
779 the start or end block. This is one step of unreachable code
781 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
782 ir_node *pred = get_Block_cfgpred(n, i);
785 if (is_Bad(pred)) continue;
786 pred_blk = get_nodes_block(skip_Proj(pred));
788 if (is_Block_dead(pred_blk)) continue;
791 /* really found a living input */
796 n = set_Block_dead(n);
797 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } /* equivalent_node_Block */
805 * Returns a equivalent node for a Jmp, a Bad :-)
806 * Of course this only happens if the Block of the Jmp is dead.
808 static ir_node *equivalent_node_Jmp(ir_node *n) {
809 /* unreachable code elimination */
810 if (is_Block_dead(get_nodes_block(n)))
814 } /* equivalent_node_Jmp */
816 /** Raise is handled in the same way as Jmp. */
817 #define equivalent_node_Raise equivalent_node_Jmp
820 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
821 See transform_node_Proj_Cond(). */
824 * Optimize operations that are commutative and have neutral 0,
825 * so a op 0 = 0 op a = a.
827 static ir_node *equivalent_node_neutral_zero(ir_node *n)
831 ir_node *a = get_binop_left(n);
832 ir_node *b = get_binop_right(n);
837 /* After running compute_node there is only one constant predecessor.
838 Find this predecessors value and remember the other node: */
839 if ((tv = value_of(a)) != tarval_bad) {
841 } else if ((tv = value_of(b)) != tarval_bad) {
846 /* If this predecessors constant value is zero, the operation is
847 * unnecessary. Remove it.
849 * Beware: If n is a Add, the mode of on and n might be different
850 * which happens in this rare construction: NULL + 3.
851 * Then, a Conv would be needed which we cannot include here.
853 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
860 } /* equivalent_node_neutral_zero */
863 * Eor is commutative and has neutral 0.
865 #define equivalent_node_Eor equivalent_node_neutral_zero
868 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
870 * The second one looks strange, but this construct
871 * is used heavily in the LCC sources :-).
873 * Beware: The Mode of an Add may be different than the mode of its
874 * predecessors, so we could not return a predecessors in all cases.
876 static ir_node *equivalent_node_Add(ir_node *n) {
878 ir_node *left, *right;
879 ir_mode *mode = get_irn_mode(n);
881 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
882 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
885 n = equivalent_node_neutral_zero(n);
889 left = get_Add_left(n);
890 right = get_Add_right(n);
893 if (get_Sub_right(left) == right) {
896 n = get_Sub_left(left);
897 if (mode == get_irn_mode(n)) {
898 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
904 if (get_Sub_right(right) == left) {
907 n = get_Sub_left(right);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915 } /* equivalent_node_Add */
918 * optimize operations that are not commutative but have neutral 0 on left,
921 static ir_node *equivalent_node_left_zero(ir_node *n) {
924 ir_node *a = get_binop_left(n);
925 ir_node *b = get_binop_right(n);
927 if (is_Const(b) && is_Const_null(b)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
933 } /* equivalent_node_left_zero */
935 #define equivalent_node_Shl equivalent_node_left_zero
936 #define equivalent_node_Shr equivalent_node_left_zero
937 #define equivalent_node_Shrs equivalent_node_left_zero
938 #define equivalent_node_Rot equivalent_node_left_zero
941 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
943 * The second one looks strange, but this construct
944 * is used heavily in the LCC sources :-).
946 * Beware: The Mode of a Sub may be different than the mode of its
947 * predecessors, so we could not return a predecessors in all cases.
949 static ir_node *equivalent_node_Sub(ir_node *n) {
952 ir_mode *mode = get_irn_mode(n);
954 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
955 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
958 b = get_Sub_right(n);
960 /* Beware: modes might be different */
961 if (is_Const(b) && is_Const_null(b)) {
962 ir_node *a = get_Sub_left(n);
963 if (mode == get_irn_mode(a)) {
966 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
970 } /* equivalent_node_Sub */
974 * Optimize an "idempotent unary op", ie op(op(n)) = n.
977 * -(-a) == a, but might overflow two times.
978 * We handle it anyway here but the better way would be a
979 * flag. This would be needed for Pascal for instance.
981 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
983 ir_node *pred = get_unop_op(n);
985 /* optimize symmetric unop */
986 if (get_irn_op(pred) == get_irn_op(n)) {
987 n = get_unop_op(pred);
988 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
991 } /* equivalent_node_idempotent_unop */
993 /** Optimize Not(Not(x)) == x. */
994 #define equivalent_node_Not equivalent_node_idempotent_unop
996 /** -(-x) == x ??? Is this possible or can --x raise an
997 out of bounds exception if min =! max? */
998 #define equivalent_node_Minus equivalent_node_idempotent_unop
1001 * Optimize a * 1 = 1 * a = a.
1003 static ir_node *equivalent_node_Mul(ir_node *n) {
1005 ir_node *a = get_Mul_left(n);
1007 /* we can handle here only the n * n = n bit cases */
1008 if (get_irn_mode(n) == get_irn_mode(a)) {
1009 ir_node *b = get_Mul_right(n);
1011 /* Mul is commutative and has again an other neutral element. */
1012 if (is_Const(a) && is_Const_one(a)) {
1014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1015 } else if (is_Const(b) && is_Const_one(b)) {
1017 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1021 } /* equivalent_node_Mul */
1024 * Optimize a / 1 = a.
1026 static ir_node *equivalent_node_Div(ir_node *n) {
1027 ir_node *a = get_Div_left(n);
1028 ir_node *b = get_Div_right(n);
1030 /* Div is not commutative. */
1031 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1032 /* Turn Div into a tuple (mem, bad, a) */
1033 ir_node *mem = get_Div_mem(n);
1034 ir_node *blk = get_irn_n(n, -1);
1035 turn_into_tuple(n, pn_Div_max);
1036 set_Tuple_pred(n, pn_Div_M, mem);
1037 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1038 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1039 set_Tuple_pred(n, pn_Div_res, a);
1042 } /* equivalent_node_Div */
1045 * Optimize a / 1.0 = a.
1047 static ir_node *equivalent_node_Quot(ir_node *n) {
1048 ir_node *a = get_Quot_left(n);
1049 ir_node *b = get_Quot_right(n);
1051 /* Div is not commutative. */
1052 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1053 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1054 ir_node *mem = get_Quot_mem(n);
1055 ir_node *blk = get_irn_n(n, -1);
1056 turn_into_tuple(n, pn_Quot_max);
1057 set_Tuple_pred(n, pn_Quot_M, mem);
1058 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1059 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1060 set_Tuple_pred(n, pn_Quot_res, a);
1063 } /* equivalent_node_Quot */
1066 * Optimize a / 1 = a.
1068 static ir_node *equivalent_node_DivMod(ir_node *n) {
1069 ir_node *b = get_DivMod_right(n);
1071 /* Div is not commutative. */
1072 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1073 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1074 ir_node *a = get_DivMod_left(n);
1075 ir_node *mem = get_Div_mem(n);
1076 ir_node *blk = get_irn_n(n, -1);
1077 ir_mode *mode = get_DivMod_resmode(n);
1079 turn_into_tuple(n, pn_DivMod_max);
1080 set_Tuple_pred(n, pn_DivMod_M, mem);
1081 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1082 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1083 set_Tuple_pred(n, pn_DivMod_res_div, a);
1084 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1087 } /* equivalent_node_DivMod */
1090 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1092 static ir_node *equivalent_node_Or(ir_node *n) {
1095 ir_node *a = get_Or_left(n);
1096 ir_node *b = get_Or_right(n);
1099 n = a; /* Or has it's own neutral element */
1100 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1101 } else if (is_Const(a) && is_Const_null(a)) {
1103 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1104 } else if (is_Const(b) && is_Const_null(b)) {
1106 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } /* equivalent_node_Or */
1113 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1115 static ir_node *equivalent_node_And(ir_node *n) {
1118 ir_node *a = get_And_left(n);
1119 ir_node *b = get_And_right(n);
1122 n = a; /* And has it's own neutral element */
1123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1126 if (is_Const(a) && is_Const_all_one(a)) {
1128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1131 if (is_Const(b) && is_Const_all_one(b)) {
1133 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1145 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1148 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1154 } /* equivalent_node_And */
1157 * Try to remove useless Conv's:
1159 static ir_node *equivalent_node_Conv(ir_node *n) {
1161 ir_node *a = get_Conv_op(n);
1163 ir_mode *n_mode = get_irn_mode(n);
1164 ir_mode *a_mode = get_irn_mode(a);
1166 if (n_mode == a_mode) { /* No Conv necessary */
1167 if (get_Conv_strict(n)) {
1168 /* special case: the predecessor might be a also a Conv */
1170 if (! get_Conv_strict(a)) {
1171 /* first one is not strict, kick it */
1172 set_Conv_op(n, get_Conv_op(a));
1175 /* else both are strict conv, second is superflous */
1176 } else if(is_Proj(a)) {
1177 ir_node *pred = get_Proj_pred(a);
1179 /* loads always return with the exact precision of n_mode */
1180 assert(get_Load_mode(pred) == n_mode);
1185 /* leave strict floating point Conv's */
1189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1190 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1191 ir_node *b = get_Conv_op(a);
1192 ir_mode *b_mode = get_irn_mode(b);
1194 if (n_mode == b_mode) {
1195 if (n_mode == mode_b) {
1196 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1197 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1198 } else if (mode_is_int(n_mode)) {
1199 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1200 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1201 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1207 } /* equivalent_node_Conv */
1210 * A Cast may be removed if the type of the previous node
1211 * is already the type of the Cast.
1213 static ir_node *equivalent_node_Cast(ir_node *n) {
1215 ir_node *pred = get_Cast_op(n);
1217 if (get_irn_type(pred) == get_Cast_type(n)) {
1219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1222 } /* equivalent_node_Cast */
1225 * Several optimizations:
1226 * - no Phi in start block.
1227 * - remove Id operators that are inputs to Phi
1228 * - fold Phi-nodes, iff they have only one predecessor except
1231 static ir_node *equivalent_node_Phi(ir_node *n) {
1235 ir_node *block = NULL; /* to shutup gcc */
1236 ir_node *first_val = NULL; /* to shutup gcc */
1238 if (!get_opt_normalize()) return n;
1240 n_preds = get_Phi_n_preds(n);
1242 block = get_nodes_block(n);
1243 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1244 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1245 if ((is_Block_dead(block)) || /* Control dead */
1246 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1247 return new_Bad(); /* in the Start Block. */
1249 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1251 /* If the Block has a Bad pred, we also have one. */
1252 for (i = 0; i < n_preds; ++i)
1253 if (is_Bad(get_Block_cfgpred(block, i)))
1254 set_Phi_pred(n, i, new_Bad());
1256 /* Find first non-self-referencing input */
1257 for (i = 0; i < n_preds; ++i) {
1258 first_val = get_Phi_pred(n, i);
1259 if ( (first_val != n) /* not self pointer */
1261 && (! is_Bad(first_val))
1263 ) { /* value not dead */
1264 break; /* then found first value. */
1269 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1273 /* search for rest of inputs, determine if any of these
1274 are non-self-referencing */
1275 while (++i < n_preds) {
1276 ir_node *scnd_val = get_Phi_pred(n, i);
1277 if ( (scnd_val != n)
1278 && (scnd_val != first_val)
1280 && (! is_Bad(scnd_val))
1288 /* Fold, if no multiple distinct non-self-referencing inputs */
1290 DBG_OPT_PHI(oldn, n);
1293 } /* equivalent_node_Phi */
1296 * Several optimizations:
1297 * - no Sync in start block.
1298 * - fold Sync-nodes, iff they have only one predecessor except
1301 static ir_node *equivalent_node_Sync(ir_node *n) {
1305 ir_node *first_val = NULL; /* to shutup gcc */
1307 if (!get_opt_normalize()) return n;
1309 n_preds = get_Sync_n_preds(n);
1311 /* Find first non-self-referencing input */
1312 for (i = 0; i < n_preds; ++i) {
1313 first_val = get_Sync_pred(n, i);
1314 if ((first_val != n) /* not self pointer */ &&
1315 (! is_Bad(first_val))
1316 ) { /* value not dead */
1317 break; /* then found first value. */
1322 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1325 /* search the rest of inputs, determine if any of these
1326 are non-self-referencing */
1327 while (++i < n_preds) {
1328 ir_node *scnd_val = get_Sync_pred(n, i);
1329 if ((scnd_val != n) &&
1330 (scnd_val != first_val) &&
1331 (! is_Bad(scnd_val))
1337 /* Fold, if no multiple distinct non-self-referencing inputs */
1339 DBG_OPT_SYNC(oldn, n);
1342 } /* equivalent_node_Sync */
1345 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1346 * ProjX(Load) and ProjX(Store).
1348 static ir_node *equivalent_node_Proj(ir_node *proj) {
1349 ir_node *oldn = proj;
1350 ir_node *a = get_Proj_pred(proj);
1353 /* Remove the Tuple/Proj combination. */
1354 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1355 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1356 DBG_OPT_TUPLE(oldn, a, proj);
1358 /* This should not happen! */
1359 assert(! "found a Proj with higher number than Tuple predecessors");
1362 } else if (get_irn_mode(proj) == mode_X) {
1363 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1364 /* Remove dead control flow -- early gigo(). */
1366 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1368 /* get the Load address */
1369 ir_node *addr = get_Load_ptr(a);
1370 ir_node *blk = get_irn_n(a, -1);
1373 if (value_not_null(addr, &confirm)) {
1374 if (confirm == NULL) {
1375 /* this node may float if it did not depend on a Confirm */
1376 set_irn_pinned(a, op_pin_state_floats);
1378 if (get_Proj_proj(proj) == pn_Load_X_except) {
1379 DBG_OPT_EXC_REM(proj);
1382 return new_r_Jmp(current_ir_graph, blk);
1384 } else if (is_Store(a)) {
1385 /* get the load/store address */
1386 ir_node *addr = get_Store_ptr(a);
1387 ir_node *blk = get_irn_n(a, -1);
1390 if (value_not_null(addr, &confirm)) {
1391 if (confirm == NULL) {
1392 /* this node may float if it did not depend on a Confirm */
1393 set_irn_pinned(a, op_pin_state_floats);
1395 if (get_Proj_proj(proj) == pn_Store_X_except) {
1396 DBG_OPT_EXC_REM(proj);
1399 return new_r_Jmp(current_ir_graph, blk);
1406 } /* equivalent_node_Proj */
1411 static ir_node *equivalent_node_Id(ir_node *n) {
1416 } while (get_irn_op(n) == op_Id);
1418 DBG_OPT_ID(oldn, n);
1420 } /* equivalent_node_Id */
1425 static ir_node *equivalent_node_Mux(ir_node *n)
1427 ir_node *oldn = n, *sel = get_Mux_sel(n);
1428 tarval *ts = value_of(sel);
1430 /* Mux(true, f, t) == t */
1431 if (ts == tarval_b_true) {
1432 n = get_Mux_true(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1435 /* Mux(false, f, t) == f */
1436 else if (ts == tarval_b_false) {
1437 n = get_Mux_false(n);
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1440 /* Mux(v, x, x) == x */
1441 else if (get_Mux_false(n) == get_Mux_true(n)) {
1442 n = get_Mux_true(n);
1443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1445 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1446 ir_node *cmp = get_Proj_pred(sel);
1447 long proj_nr = get_Proj_proj(sel);
1448 ir_node *b = get_Mux_false(n);
1449 ir_node *a = get_Mux_true(n);
1452 * Note: normalization puts the constant on the right site,
1453 * so we check only one case.
1455 * Note further that these optimization work even for floating point
1456 * with NaN's because -NaN == NaN.
1457 * However, if +0 and -0 is handled differently, we cannot use the first one.
1459 if (is_Cmp(cmp) && get_Cmp_left(cmp) == a) {
1460 ir_node *cmp_r = get_Cmp_right(cmp);
1461 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1462 /* Mux(a CMP 0, X, a) */
1463 if (is_Minus(b) && get_Minus_op(b) == a) {
1464 /* Mux(a CMP 0, -a, a) */
1465 if (proj_nr == pn_Cmp_Eq) {
1466 /* Mux(a == 0, -a, a) ==> -a */
1468 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1469 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1470 /* Mux(a != 0, -a, a) ==> a */
1472 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1474 } else if (is_Const(b) && is_Const_null(b)) {
1475 /* Mux(a CMP 0, 0, a) */
1476 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1477 /* Mux(a != 0, 0, a) ==> a */
1479 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1480 } else if (proj_nr == pn_Cmp_Eq) {
1481 /* Mux(a == 0, 0, a) ==> 0 */
1483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1490 } /* equivalent_node_Mux */
1493 * Returns a equivalent node of a Psi: if a condition is true
1494 * and all previous conditions are false we know its value.
1495 * If all conditions are false its value is the default one.
1497 static ir_node *equivalent_node_Psi(ir_node *n) {
1499 return equivalent_node_Mux(n);
1501 } /* equivalent_node_Psi */
1504 * Optimize -a CMP -b into b CMP a.
1505 * This works only for for modes where unary Minus
1507 * Note that two-complement integers can Overflow
1508 * so it will NOT work.
1510 * For == and != can be handled in Proj(Cmp)
1512 static ir_node *equivalent_node_Cmp(ir_node *n) {
1513 ir_node *left = get_Cmp_left(n);
1514 ir_node *right = get_Cmp_right(n);
1516 if (is_Minus(left) && is_Minus(right) &&
1517 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1518 left = get_Minus_op(left);
1519 right = get_Minus_op(right);
1520 set_Cmp_left(n, right);
1521 set_Cmp_right(n, left);
1524 } /* equivalent_node_Cmp */
1527 * Remove Confirm nodes if setting is on.
1528 * Replace Confirms(x, '=', Constlike) by Constlike.
1530 static ir_node *equivalent_node_Confirm(ir_node *n) {
1531 ir_node *pred = get_Confirm_value(n);
1532 pn_Cmp pnc = get_Confirm_cmp(n);
1534 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1536 * rare case: two identical Confirms one after another,
1537 * replace the second one with the first.
1541 if (pnc == pn_Cmp_Eq) {
1542 ir_node *bound = get_Confirm_bound(n);
1545 * Optimize a rare case:
1546 * Confirm(x, '=', Constlike) ==> Constlike
1548 if (is_irn_constlike(bound)) {
1549 DBG_OPT_CONFIRM(n, bound);
1553 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1557 * Optimize CopyB(mem, x, x) into a Nop.
1559 static ir_node *equivalent_node_CopyB(ir_node *n) {
1560 ir_node *a = get_CopyB_dst(n);
1561 ir_node *b = get_CopyB_src(n);
1564 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1565 ir_node *mem = get_CopyB_mem(n);
1566 ir_node *blk = get_nodes_block(n);
1567 turn_into_tuple(n, pn_CopyB_max);
1568 set_Tuple_pred(n, pn_CopyB_M, mem);
1569 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1570 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1571 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1574 } /* equivalent_node_CopyB */
1577 * Optimize Bounds(idx, idx, upper) into idx.
1579 static ir_node *equivalent_node_Bound(ir_node *n) {
1580 ir_node *idx = get_Bound_index(n);
1581 ir_node *pred = skip_Proj(idx);
1584 if (is_Bound(pred)) {
1586 * idx was Bounds checked in the same MacroBlock previously,
1587 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1589 ir_node *lower = get_Bound_lower(n);
1590 ir_node *upper = get_Bound_upper(n);
1591 if (get_Bound_lower(pred) == lower &&
1592 get_Bound_upper(pred) == upper &&
1593 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1595 * One could expect that we simply return the previous
1596 * Bound here. However, this would be wrong, as we could
1597 * add an exception Proj to a new location then.
1598 * So, we must turn in into a tuple.
1604 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1605 ir_node *mem = get_Bound_mem(n);
1606 ir_node *blk = get_nodes_block(n);
1607 turn_into_tuple(n, pn_Bound_max);
1608 set_Tuple_pred(n, pn_Bound_M, mem);
1609 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1610 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1611 set_Tuple_pred(n, pn_Bound_res, idx);
1614 } /* equivalent_node_Bound */
1617 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1618 * perform no actual computation, as, e.g., the Id nodes. It does not create
1619 * new nodes. It is therefore safe to free n if the node returned is not n.
1620 * If a node returns a Tuple we can not just skip it. If the size of the
1621 * in array fits, we transform n into a tuple (e.g., Div).
1623 ir_node *equivalent_node(ir_node *n) {
1624 if (n->op->ops.equivalent_node)
1625 return n->op->ops.equivalent_node(n);
1627 } /* equivalent_node */
1630 * Sets the default equivalent node operation for an ir_op_ops.
1632 * @param code the opcode for the default operation
1633 * @param ops the operations initialized
1638 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1642 ops->equivalent_node = equivalent_node_##a; \
1682 } /* firm_set_default_equivalent_node */
1685 * Returns non-zero if a node is a Phi node
1686 * with all predecessors constant.
1688 static int is_const_Phi(ir_node *n) {
1691 if (! is_Phi(n) || get_irn_arity(n) == 0)
1693 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1694 if (! is_Const(get_irn_n(n, i)))
1697 } /* is_const_Phi */
1700 * Apply an evaluator on a binop with a constant operators (and one Phi).
1702 * @param phi the Phi node
1703 * @param other the other operand
1704 * @param eval an evaluator function
1705 * @param mode the mode of the result, may be different from the mode of the Phi!
1706 * @param left if non-zero, other is the left operand, else the right
1708 * @return a new Phi node if the conversion was successful, NULL else
1710 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1715 int i, n = get_irn_arity(phi);
1717 NEW_ARR_A(void *, res, n);
1719 for (i = 0; i < n; ++i) {
1720 pred = get_irn_n(phi, i);
1721 tv = get_Const_tarval(pred);
1722 tv = eval(other, tv);
1724 if (tv == tarval_bad) {
1725 /* folding failed, bad */
1731 for (i = 0; i < n; ++i) {
1732 pred = get_irn_n(phi, i);
1733 tv = get_Const_tarval(pred);
1734 tv = eval(tv, other);
1736 if (tv == tarval_bad) {
1737 /* folding failed, bad */
1743 irg = current_ir_graph;
1744 for (i = 0; i < n; ++i) {
1745 pred = get_irn_n(phi, i);
1746 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1747 mode, res[i], get_Const_type(pred));
1749 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1750 } /* apply_binop_on_phi */
1753 * Apply an evaluator on a binop with two constant Phi.
1755 * @param a the left Phi node
1756 * @param b the right Phi node
1757 * @param eval an evaluator function
1758 * @param mode the mode of the result, may be different from the mode of the Phi!
1760 * @return a new Phi node if the conversion was successful, NULL else
1762 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1763 tarval *tv_l, *tv_r, *tv;
1769 if (get_nodes_block(a) != get_nodes_block(b))
1772 n = get_irn_arity(a);
1773 NEW_ARR_A(void *, res, n);
1775 for (i = 0; i < n; ++i) {
1776 pred = get_irn_n(a, i);
1777 tv_l = get_Const_tarval(pred);
1778 pred = get_irn_n(b, i);
1779 tv_r = get_Const_tarval(pred);
1780 tv = eval(tv_l, tv_r);
1782 if (tv == tarval_bad) {
1783 /* folding failed, bad */
1788 irg = current_ir_graph;
1789 for (i = 0; i < n; ++i) {
1790 pred = get_irn_n(a, i);
1791 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1793 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1794 } /* apply_binop_on_2_phis */
1797 * Apply an evaluator on a unop with a constant operator (a Phi).
1799 * @param phi the Phi node
1800 * @param eval an evaluator function
1802 * @return a new Phi node if the conversion was successful, NULL else
1804 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1810 int i, n = get_irn_arity(phi);
1812 NEW_ARR_A(void *, res, n);
1813 for (i = 0; i < n; ++i) {
1814 pred = get_irn_n(phi, i);
1815 tv = get_Const_tarval(pred);
1818 if (tv == tarval_bad) {
1819 /* folding failed, bad */
1824 mode = get_irn_mode(phi);
1825 irg = current_ir_graph;
1826 for (i = 0; i < n; ++i) {
1827 pred = get_irn_n(phi, i);
1828 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1829 mode, res[i], get_Const_type(pred));
1831 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1832 } /* apply_unop_on_phi */
1835 * Apply a conversion on a constant operator (a Phi).
1837 * @param phi the Phi node
1839 * @return a new Phi node if the conversion was successful, NULL else
1841 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1846 int i, n = get_irn_arity(phi);
1848 NEW_ARR_A(void *, res, n);
1849 for (i = 0; i < n; ++i) {
1850 pred = get_irn_n(phi, i);
1851 tv = get_Const_tarval(pred);
1852 tv = tarval_convert_to(tv, mode);
1854 if (tv == tarval_bad) {
1855 /* folding failed, bad */
1860 irg = current_ir_graph;
1861 for (i = 0; i < n; ++i) {
1862 pred = get_irn_n(phi, i);
1863 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1864 mode, res[i], get_Const_type(pred));
1866 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1867 } /* apply_conv_on_phi */
1870 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1871 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1872 * If possible, remove the Conv's.
1874 static ir_node *transform_node_AddSub(ir_node *n) {
1875 ir_mode *mode = get_irn_mode(n);
1877 if (mode_is_reference(mode)) {
1878 ir_node *left = get_binop_left(n);
1879 ir_node *right = get_binop_right(n);
1880 unsigned ref_bits = get_mode_size_bits(mode);
1882 if (is_Conv(left)) {
1883 ir_mode *mode = get_irn_mode(left);
1884 unsigned bits = get_mode_size_bits(mode);
1886 if (ref_bits == bits &&
1887 mode_is_int(mode) &&
1888 get_mode_arithmetic(mode) == irma_twos_complement) {
1889 ir_node *pre = get_Conv_op(left);
1890 ir_mode *pre_mode = get_irn_mode(pre);
1892 if (mode_is_int(pre_mode) &&
1893 get_mode_size_bits(pre_mode) == bits &&
1894 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1895 /* ok, this conv just changes to sign, moreover the calculation
1896 * is done with same number of bits as our address mode, so
1897 * we can ignore the conv as address calculation can be viewed
1898 * as either signed or unsigned
1900 set_binop_left(n, pre);
1905 if (is_Conv(right)) {
1906 ir_mode *mode = get_irn_mode(right);
1907 unsigned bits = get_mode_size_bits(mode);
1909 if (ref_bits == bits &&
1910 mode_is_int(mode) &&
1911 get_mode_arithmetic(mode) == irma_twos_complement) {
1912 ir_node *pre = get_Conv_op(right);
1913 ir_mode *pre_mode = get_irn_mode(pre);
1915 if (mode_is_int(pre_mode) &&
1916 get_mode_size_bits(pre_mode) == bits &&
1917 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1918 /* ok, this conv just changes to sign, moreover the calculation
1919 * is done with same number of bits as our address mode, so
1920 * we can ignore the conv as address calculation can be viewed
1921 * as either signed or unsigned
1923 set_binop_right(n, pre);
1929 } /* transform_node_AddSub */
1931 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1933 if (is_Const(b) && is_const_Phi(a)) { \
1934 /* check for Op(Phi, Const) */ \
1935 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1937 else if (is_Const(a) && is_const_Phi(b)) { \
1938 /* check for Op(Const, Phi) */ \
1939 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1941 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1942 /* check for Op(Phi, Phi) */ \
1943 c = apply_binop_on_2_phis(a, b, eval, mode); \
1946 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1950 #define HANDLE_UNOP_PHI(eval, a, c) \
1952 if (is_const_Phi(a)) { \
1953 /* check for Op(Phi) */ \
1954 c = apply_unop_on_phi(a, eval); \
1956 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1962 * Do the AddSub optimization, then Transform
1963 * Constant folding on Phi
1964 * Add(a,a) -> Mul(a, 2)
1965 * Add(Mul(a, x), a) -> Mul(a, x+1)
1966 * if the mode is integer or float.
1967 * Transform Add(a,-b) into Sub(a,b).
1968 * Reassociation might fold this further.
1970 static ir_node *transform_node_Add(ir_node *n) {
1972 ir_node *a, *b, *c, *oldn = n;
1974 n = transform_node_AddSub(n);
1976 a = get_Add_left(n);
1977 b = get_Add_right(n);
1979 mode = get_irn_mode(n);
1980 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
1982 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1983 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1986 if (mode_is_num(mode)) {
1987 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1988 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1989 ir_node *block = get_irn_n(n, -1);
1992 get_irn_dbg_info(n),
1996 new_r_Const_long(current_ir_graph, block, mode, 2),
1998 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2003 get_irn_dbg_info(n),
2009 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2014 get_irn_dbg_info(n),
2020 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2023 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2024 /* Here we rely on constants be on the RIGHT side */
2026 ir_node *op = get_Not_op(a);
2028 if (is_Const(b) && is_Const_one(b)) {
2030 ir_node *blk = get_irn_n(n, -1);
2031 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2032 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2037 ir_node *blk = get_irn_n(n, -1);
2038 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2039 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2044 ir_node *op = get_Not_op(b);
2048 ir_node *blk = get_irn_n(n, -1);
2049 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2050 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2057 } /* transform_node_Add */
2060 * returns -cnst or NULL if impossible
2062 static ir_node *const_negate(ir_node *cnst) {
2063 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2064 dbg_info *dbgi = get_irn_dbg_info(cnst);
2065 ir_graph *irg = get_irn_irg(cnst);
2066 ir_node *block = get_nodes_block(cnst);
2067 ir_mode *mode = get_irn_mode(cnst);
2068 if (tv == tarval_bad) return NULL;
2069 return new_rd_Const(dbgi, irg, block, mode, tv);
2073 * Do the AddSub optimization, then Transform
2074 * Constant folding on Phi
2075 * Sub(0,a) -> Minus(a)
2076 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2077 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2078 * Sub(Add(a, x), x) -> a
2079 * Sub(x, Add(x, a)) -> -a
2080 * Sub(x, Const) -> Add(x, -Const)
2082 static ir_node *transform_node_Sub(ir_node *n) {
2087 n = transform_node_AddSub(n);
2089 a = get_Sub_left(n);
2090 b = get_Sub_right(n);
2092 mode = get_irn_mode(n);
2095 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2097 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2098 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2102 ir_mode *b_mode = get_irn_mode(b);
2105 if (get_opt_overflow_unsafe_transform())
2106 allow = b_mode != mode_P;
2108 allow = mode_is_signed(b_mode);
2111 /* a - C -> a + (-C) */
2112 ir_node *cnst = const_negate(b);
2114 ir_node *block = get_nodes_block(n);
2115 dbg_info *dbgi = get_irn_dbg_info(n);
2116 ir_graph *irg = get_irn_irg(n);
2118 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2119 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2125 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2126 ir_graph *irg = current_ir_graph;
2127 dbg_info *dbg = get_irn_dbg_info(n);
2128 ir_node *block = get_nodes_block(n);
2129 ir_node *left = get_Minus_op(a);
2130 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2132 n = new_rd_Minus(dbg, irg, block, add, mode);
2133 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2135 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2136 ir_graph *irg = current_ir_graph;
2137 dbg_info *dbg = get_irn_dbg_info(n);
2138 ir_node *block = get_nodes_block(n);
2139 ir_node *right = get_Minus_op(b);
2141 n = new_rd_Add(dbg, irg, block, a, right, mode);
2142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2144 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2145 ir_graph *irg = current_ir_graph;
2146 dbg_info *s_dbg = get_irn_dbg_info(b);
2147 ir_node *s_block = get_nodes_block(b);
2148 ir_node *s_left = get_Sub_right(b);
2149 ir_node *s_right = get_Sub_left(b);
2150 ir_mode *s_mode = get_irn_mode(b);
2151 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2152 dbg_info *a_dbg = get_irn_dbg_info(n);
2153 ir_node *a_block = get_nodes_block(n);
2155 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2156 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2158 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2159 ir_node *m_right = get_Mul_right(b);
2160 if (is_Const(m_right)) {
2161 ir_node *cnst2 = const_negate(m_right);
2162 if (cnst2 != NULL) {
2163 ir_graph *irg = current_ir_graph;
2164 dbg_info *m_dbg = get_irn_dbg_info(b);
2165 ir_node *m_block = get_nodes_block(b);
2166 ir_node *m_left = get_Mul_left(b);
2167 ir_mode *m_mode = get_irn_mode(b);
2168 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2169 dbg_info *a_dbg = get_irn_dbg_info(n);
2170 ir_node *a_block = get_nodes_block(n);
2172 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2173 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2179 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2180 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2182 get_irn_dbg_info(n),
2187 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2191 if (mode_wrap_around(mode)) {
2192 ir_node *left = get_Add_left(a);
2193 ir_node *right = get_Add_right(a);
2195 /* FIXME: Does the Conv's work only for two complement or generally? */
2197 if (mode != get_irn_mode(right)) {
2198 /* This Sub is an effective Cast */
2199 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2202 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2204 } else if (right == b) {
2205 if (mode != get_irn_mode(left)) {
2206 /* This Sub is an effective Cast */
2207 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2210 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2216 if (mode_wrap_around(mode)) {
2217 ir_node *left = get_Add_left(b);
2218 ir_node *right = get_Add_right(b);
2220 /* FIXME: Does the Conv's work only for two complement or generally? */
2222 ir_mode *r_mode = get_irn_mode(right);
2224 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2225 if (mode != r_mode) {
2226 /* This Sub is an effective Cast */
2227 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2229 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2231 } else if (right == a) {
2232 ir_mode *l_mode = get_irn_mode(left);
2234 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2235 if (mode != l_mode) {
2236 /* This Sub is an effective Cast */
2237 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2239 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2244 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2245 ir_mode *mode = get_irn_mode(a);
2247 if (mode == get_irn_mode(b)) {
2249 ir_node *op_a = get_Conv_op(a);
2250 ir_node *op_b = get_Conv_op(b);
2252 /* check if it's allowed to skip the conv */
2253 ma = get_irn_mode(op_a);
2254 mb = get_irn_mode(op_b);
2256 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2257 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2260 set_Sub_right(n, b);
2266 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2267 if (!is_reassoc_running() && is_Mul(a)) {
2268 ir_node *ma = get_Mul_left(a);
2269 ir_node *mb = get_Mul_right(a);
2272 ir_node *blk = get_irn_n(n, -1);
2274 get_irn_dbg_info(n),
2275 current_ir_graph, blk,
2278 get_irn_dbg_info(n),
2279 current_ir_graph, blk,
2281 new_r_Const_long(current_ir_graph, blk, mode, 1),
2284 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2286 } else if (mb == b) {
2287 ir_node *blk = get_irn_n(n, -1);
2289 get_irn_dbg_info(n),
2290 current_ir_graph, blk,
2293 get_irn_dbg_info(n),
2294 current_ir_graph, blk,
2296 new_r_Const_long(current_ir_graph, blk, mode, 1),
2299 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2303 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2304 ir_node *x = get_Sub_left(a);
2305 ir_node *y = get_Sub_right(a);
2306 ir_node *blk = get_irn_n(n, -1);
2307 ir_mode *m_b = get_irn_mode(b);
2308 ir_mode *m_y = get_irn_mode(y);
2312 /* Determine the right mode for the Add. */
2315 else if (mode_is_reference(m_b))
2317 else if (mode_is_reference(m_y))
2321 * Both modes are different but none is reference,
2322 * happens for instance in SubP(SubP(P, Iu), Is).
2323 * We have two possibilities here: Cast or ignore.
2324 * Currently we ignore this case.
2329 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2331 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2336 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2337 if (is_Const(a) && is_Not(b)) {
2338 /* c - ~X = X + (c+1) */
2339 tarval *tv = get_Const_tarval(a);
2341 tv = tarval_add(tv, get_mode_one(mode));
2342 if (tv != tarval_bad) {
2343 ir_node *blk = get_irn_n(n, -1);
2344 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2345 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2346 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2352 } /* transform_node_Sub */
2355 * Several transformation done on n*n=2n bits mul.
2356 * These transformations must be done here because new nodes may be produced.
2358 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2360 ir_node *a = get_Mul_left(n);
2361 ir_node *b = get_Mul_right(n);
2362 tarval *ta = value_of(a);
2363 tarval *tb = value_of(b);
2364 ir_mode *smode = get_irn_mode(a);
2366 if (ta == get_mode_one(smode)) {
2367 /* (L)1 * (L)b = (L)b */
2368 ir_node *blk = get_irn_n(n, -1);
2369 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2370 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2373 else if (ta == get_mode_minus_one(smode)) {
2374 /* (L)-1 * (L)b = (L)b */
2375 ir_node *blk = get_irn_n(n, -1);
2376 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2377 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2378 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2381 if (tb == get_mode_one(smode)) {
2382 /* (L)a * (L)1 = (L)a */
2383 ir_node *blk = get_irn_n(a, -1);
2384 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2385 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2388 else if (tb == get_mode_minus_one(smode)) {
2389 /* (L)a * (L)-1 = (L)-a */
2390 ir_node *blk = get_irn_n(n, -1);
2391 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2392 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2393 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2400 * Transform Mul(a,-1) into -a.
2401 * Do constant evaluation of Phi nodes.
2402 * Do architecture dependent optimizations on Mul nodes
2404 static ir_node *transform_node_Mul(ir_node *n) {
2405 ir_node *c, *oldn = n;
2406 ir_mode *mode = get_irn_mode(n);
2407 ir_node *a = get_Mul_left(n);
2408 ir_node *b = get_Mul_right(n);
2410 if (is_Bad(a) || is_Bad(b))
2413 if (mode != get_irn_mode(a))
2414 return transform_node_Mul2n(n, mode);
2416 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2418 if (mode_is_signed(mode)) {
2421 if (value_of(a) == get_mode_minus_one(mode))
2423 else if (value_of(b) == get_mode_minus_one(mode))
2426 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2427 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2432 if (is_Const(b)) { /* (-a) * const -> a * -const */
2433 ir_node *cnst = const_negate(b);
2435 dbg_info *dbgi = get_irn_dbg_info(n);
2436 ir_node *block = get_nodes_block(n);
2437 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2438 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2441 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2442 dbg_info *dbgi = get_irn_dbg_info(n);
2443 ir_node *block = get_nodes_block(n);
2444 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2447 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2448 ir_node *sub_l = get_Sub_left(b);
2449 ir_node *sub_r = get_Sub_right(b);
2450 dbg_info *dbgi = get_irn_dbg_info(n);
2451 ir_graph *irg = current_ir_graph;
2452 ir_node *block = get_nodes_block(n);
2453 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2454 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2455 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2458 } else if (is_Minus(b)) {
2459 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2460 ir_node *sub_l = get_Sub_left(a);
2461 ir_node *sub_r = get_Sub_right(a);
2462 dbg_info *dbgi = get_irn_dbg_info(n);
2463 ir_graph *irg = current_ir_graph;
2464 ir_node *block = get_nodes_block(n);
2465 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2466 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2467 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2471 if (get_mode_arithmetic(mode) == irma_ieee754) {
2473 tarval *tv = get_Const_tarval(a);
2474 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2475 /* 2.0 * b = b + b */
2476 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2477 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2481 else if (is_Const(b)) {
2482 tarval *tv = get_Const_tarval(b);
2483 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2484 /* a * 2.0 = a + a */
2485 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2486 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2491 return arch_dep_replace_mul_with_shifts(n);
2492 } /* transform_node_Mul */
2495 * Transform a Div Node.
2497 static ir_node *transform_node_Div(ir_node *n) {
2498 ir_mode *mode = get_Div_resmode(n);
2499 ir_node *a = get_Div_left(n);
2500 ir_node *b = get_Div_right(n);
2504 if (is_Const(b) && is_const_Phi(a)) {
2505 /* check for Div(Phi, Const) */
2506 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2508 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2512 else if (is_Const(a) && is_const_Phi(b)) {
2513 /* check for Div(Const, Phi) */
2514 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2516 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2520 else if (is_const_Phi(a) && is_const_Phi(b)) {
2521 /* check for Div(Phi, Phi) */
2522 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2524 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2531 if (tv != tarval_bad) {
2532 value = new_Const(get_tarval_mode(tv), tv);
2534 DBG_OPT_CSTEVAL(n, value);
2537 ir_node *a = get_Div_left(n);
2538 ir_node *b = get_Div_right(n);
2541 if (a == b && value_not_zero(a, &dummy)) {
2542 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2543 value = new_Const(mode, get_mode_one(mode));
2544 DBG_OPT_CSTEVAL(n, value);
2547 if (mode_is_signed(mode) && is_Const(b)) {
2548 tarval *tv = get_Const_tarval(b);
2550 if (tv == get_mode_minus_one(mode)) {
2552 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2553 DBG_OPT_CSTEVAL(n, value);
2557 /* Try architecture dependent optimization */
2558 value = arch_dep_replace_div_by_const(n);
2566 /* Turn Div into a tuple (mem, jmp, bad, value) */
2567 mem = get_Div_mem(n);
2568 blk = get_irn_n(n, -1);
2570 /* skip a potential Pin */
2572 mem = get_Pin_op(mem);
2573 turn_into_tuple(n, pn_Div_max);
2574 set_Tuple_pred(n, pn_Div_M, mem);
2575 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2576 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2577 set_Tuple_pred(n, pn_Div_res, value);
2580 } /* transform_node_Div */
2583 * Transform a Mod node.
2585 static ir_node *transform_node_Mod(ir_node *n) {
2586 ir_mode *mode = get_Mod_resmode(n);
2587 ir_node *a = get_Mod_left(n);
2588 ir_node *b = get_Mod_right(n);
2592 if (is_Const(b) && is_const_Phi(a)) {
2593 /* check for Div(Phi, Const) */
2594 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2596 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2600 else if (is_Const(a) && is_const_Phi(b)) {
2601 /* check for Div(Const, Phi) */
2602 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2604 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2608 else if (is_const_Phi(a) && is_const_Phi(b)) {
2609 /* check for Div(Phi, Phi) */
2610 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2612 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2619 if (tv != tarval_bad) {
2620 value = new_Const(get_tarval_mode(tv), tv);
2622 DBG_OPT_CSTEVAL(n, value);
2625 ir_node *a = get_Mod_left(n);
2626 ir_node *b = get_Mod_right(n);
2629 if (a == b && value_not_zero(a, &dummy)) {
2630 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2631 value = new_Const(mode, get_mode_null(mode));
2632 DBG_OPT_CSTEVAL(n, value);
2635 if (mode_is_signed(mode) && is_Const(b)) {
2636 tarval *tv = get_Const_tarval(b);
2638 if (tv == get_mode_minus_one(mode)) {
2640 value = new_Const(mode, get_mode_null(mode));
2641 DBG_OPT_CSTEVAL(n, value);
2645 /* Try architecture dependent optimization */
2646 value = arch_dep_replace_mod_by_const(n);
2654 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2655 mem = get_Mod_mem(n);
2656 blk = get_irn_n(n, -1);
2658 /* skip a potential Pin */
2660 mem = get_Pin_op(mem);
2661 turn_into_tuple(n, pn_Mod_max);
2662 set_Tuple_pred(n, pn_Mod_M, mem);
2663 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2664 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2665 set_Tuple_pred(n, pn_Mod_res, value);
2668 } /* transform_node_Mod */
2671 * Transform a DivMod node.
2673 static ir_node *transform_node_DivMod(ir_node *n) {
2675 ir_node *a = get_DivMod_left(n);
2676 ir_node *b = get_DivMod_right(n);
2677 ir_mode *mode = get_DivMod_resmode(n);
2682 if (is_Const(b) && is_const_Phi(a)) {
2683 /* check for Div(Phi, Const) */
2684 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2685 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2687 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2688 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2692 else if (is_Const(a) && is_const_Phi(b)) {
2693 /* check for Div(Const, Phi) */
2694 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2695 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2697 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2698 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2702 else if (is_const_Phi(a) && is_const_Phi(b)) {
2703 /* check for Div(Phi, Phi) */
2704 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2705 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2707 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2708 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2715 if (tb != tarval_bad) {
2716 if (tb == get_mode_one(get_tarval_mode(tb))) {
2718 vb = new_Const(mode, get_mode_null(mode));
2719 DBG_OPT_CSTEVAL(n, vb);
2721 } else if (ta != tarval_bad) {
2722 tarval *resa, *resb;
2723 resa = tarval_div(ta, tb);
2724 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2725 Jmp for X result!? */
2726 resb = tarval_mod(ta, tb);
2727 if (resb == tarval_bad) return n; /* Causes exception! */
2728 va = new_Const(mode, resa);
2729 vb = new_Const(mode, resb);
2730 DBG_OPT_CSTEVAL(n, va);
2731 DBG_OPT_CSTEVAL(n, vb);
2733 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2734 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2735 vb = new_Const(mode, get_mode_null(mode));
2736 DBG_OPT_CSTEVAL(n, va);
2737 DBG_OPT_CSTEVAL(n, vb);
2739 } else { /* Try architecture dependent optimization */
2742 arch_dep_replace_divmod_by_const(&va, &vb, n);
2743 evaluated = va != NULL;
2745 } else if (a == b) {
2746 if (value_not_zero(a, &dummy)) {
2748 va = new_Const(mode, get_mode_one(mode));
2749 vb = new_Const(mode, get_mode_null(mode));
2750 DBG_OPT_CSTEVAL(n, va);
2751 DBG_OPT_CSTEVAL(n, vb);
2754 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2757 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2758 /* 0 / non-Const = 0 */
2763 if (evaluated) { /* replace by tuple */
2767 mem = get_DivMod_mem(n);
2768 /* skip a potential Pin */
2770 mem = get_Pin_op(mem);
2772 blk = get_irn_n(n, -1);
2773 turn_into_tuple(n, pn_DivMod_max);
2774 set_Tuple_pred(n, pn_DivMod_M, mem);
2775 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2776 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2777 set_Tuple_pred(n, pn_DivMod_res_div, va);
2778 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2782 } /* transform_node_DivMod */
2785 * Optimize x / c to x * (1/c)
2787 static ir_node *transform_node_Quot(ir_node *n) {
2788 ir_mode *mode = get_Quot_resmode(n);
2791 if (get_mode_arithmetic(mode) == irma_ieee754) {
2792 ir_node *b = get_Quot_right(n);
2795 tarval *tv = get_Const_tarval(b);
2797 tv = tarval_quo(get_mode_one(mode), tv);
2799 /* Do the transformation if the result is either exact or we are not
2800 using strict rules. */
2801 if (tv != tarval_bad &&
2802 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2803 ir_node *blk = get_irn_n(n, -1);
2804 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2805 ir_node *a = get_Quot_left(n);
2806 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2807 ir_node *mem = get_Quot_mem(n);
2809 /* skip a potential Pin */
2811 mem = get_Pin_op(mem);
2812 turn_into_tuple(n, pn_Quot_max);
2813 set_Tuple_pred(n, pn_Quot_M, mem);
2814 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2815 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2816 set_Tuple_pred(n, pn_Quot_res, m);
2817 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2822 } /* transform_node_Quot */
2825 * Optimize Abs(x) into x if x is Confirmed >= 0
2826 * Optimize Abs(x) into -x if x is Confirmed <= 0
2827 * Optimize Abs(-x) int Abs(x)
2829 static ir_node *transform_node_Abs(ir_node *n) {
2830 ir_node *c, *oldn = n;
2831 ir_node *a = get_Abs_op(n);
2834 HANDLE_UNOP_PHI(tarval_abs, a, c);
2836 switch (classify_value_sign(a)) {
2837 case value_classified_negative:
2838 mode = get_irn_mode(n);
2841 * We can replace the Abs by -x here.
2842 * We even could add a new Confirm here.
2844 * Note that -x would create a new node, so we could
2845 * not run it in the equivalent_node() context.
2847 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2848 get_nodes_block(n), a, mode);
2850 DBG_OPT_CONFIRM(oldn, n);
2852 case value_classified_positive:
2853 /* n is positive, Abs is not needed */
2856 DBG_OPT_CONFIRM(oldn, n);
2862 /* Abs(-x) = Abs(x) */
2863 mode = get_irn_mode(n);
2864 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2865 get_nodes_block(n), get_Minus_op(a), mode);
2866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2870 } /* transform_node_Abs */
2873 * Transform a Cond node.
2875 * Replace the Cond by a Jmp if it branches on a constant
2878 static ir_node *transform_node_Cond(ir_node *n) {
2881 ir_node *a = get_Cond_selector(n);
2882 tarval *ta = value_of(a);
2884 /* we need block info which is not available in floating irgs */
2885 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2888 if ((ta != tarval_bad) &&
2889 (get_irn_mode(a) == mode_b) &&
2890 (get_opt_unreachable_code())) {
2891 /* It's a boolean Cond, branching on a boolean constant.
2892 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2893 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2894 turn_into_tuple(n, pn_Cond_max);
2895 if (ta == tarval_b_true) {
2896 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2897 set_Tuple_pred(n, pn_Cond_true, jmp);
2899 set_Tuple_pred(n, pn_Cond_false, jmp);
2900 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2902 /* We might generate an endless loop, so keep it alive. */
2903 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2906 } /* transform_node_Cond */
2908 typedef ir_node* (*recursive_transform) (ir_node *n);
2911 * makes use of distributive laws for and, or, eor
2912 * and(a OP c, b OP c) -> and(a, b) OP c
2913 * note, might return a different op than n
2915 static ir_node *transform_bitwise_distributive(ir_node *n,
2916 recursive_transform trans_func)
2919 ir_node *a = get_binop_left(n);
2920 ir_node *b = get_binop_right(n);
2921 ir_op *op = get_irn_op(a);
2922 ir_op *op_root = get_irn_op(n);
2924 if(op != get_irn_op(b))
2927 if (op == op_Conv) {
2928 ir_node *a_op = get_Conv_op(a);
2929 ir_node *b_op = get_Conv_op(b);
2930 ir_mode *a_mode = get_irn_mode(a_op);
2931 ir_mode *b_mode = get_irn_mode(b_op);
2932 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2933 ir_node *blk = get_irn_n(n, -1);
2936 set_binop_left(n, a_op);
2937 set_binop_right(n, b_op);
2938 set_irn_mode(n, a_mode);
2940 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2942 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2948 /* nothing to gain here */
2952 if (op == op_Shrs || op == op_Shr || op == op_Shl
2953 || op == op_And || op == op_Or || op == op_Eor) {
2954 ir_node *a_left = get_binop_left(a);
2955 ir_node *a_right = get_binop_right(a);
2956 ir_node *b_left = get_binop_left(b);
2957 ir_node *b_right = get_binop_right(b);
2959 ir_node *op1 = NULL;
2960 ir_node *op2 = NULL;
2962 if (is_op_commutative(op)) {
2963 if (a_left == b_left) {
2967 } else if(a_left == b_right) {
2971 } else if(a_right == b_left) {
2977 if(a_right == b_right) {
2984 /* (a sop c) & (b sop c) => (a & b) sop c */
2985 ir_node *blk = get_irn_n(n, -1);
2987 ir_node *new_n = exact_copy(n);
2988 set_binop_left(new_n, op1);
2989 set_binop_right(new_n, op2);
2990 new_n = trans_func(new_n);
2992 if(op_root == op_Eor && op == op_Or) {
2993 dbg_info *dbgi = get_irn_dbg_info(n);
2994 ir_graph *irg = current_ir_graph;
2995 ir_mode *mode = get_irn_mode(c);
2997 c = new_rd_Not(dbgi, irg, blk, c, mode);
2998 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3001 set_irn_n(n, -1, blk);
3002 set_binop_left(n, new_n);
3003 set_binop_right(n, c);
3004 add_identities(current_ir_graph->value_table, n);
3007 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3018 static ir_node *transform_node_And(ir_node *n) {
3019 ir_node *c, *oldn = n;
3020 ir_node *a = get_And_left(n);
3021 ir_node *b = get_And_right(n);
3024 mode = get_irn_mode(n);
3025 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3027 /* we can evaluate 2 Projs of the same Cmp */
3028 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3029 ir_node *pred_a = get_Proj_pred(a);
3030 ir_node *pred_b = get_Proj_pred(b);
3031 if (pred_a == pred_b) {
3032 dbg_info *dbgi = get_irn_dbg_info(n);
3033 ir_node *block = get_nodes_block(pred_a);
3034 pn_Cmp pn_a = get_Proj_proj(a);
3035 pn_Cmp pn_b = get_Proj_proj(b);
3036 /* yes, we can simply calculate with pncs */
3037 pn_Cmp new_pnc = pn_a & pn_b;
3039 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3044 ir_node *op = get_Not_op(b);
3046 ir_node *ba = get_And_left(op);
3047 ir_node *bb = get_And_right(op);
3049 /* it's enough to test the following cases due to normalization! */
3050 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3051 /* (a|b) & ~(a&b) = a^b */
3052 ir_node *block = get_nodes_block(n);
3054 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3055 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3063 ir_node *op = get_Not_op(a);
3065 ir_node *aa = get_And_left(op);
3066 ir_node *ab = get_And_right(op);
3068 /* it's enough to test the following cases due to normalization! */
3069 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3070 /* (a|b) & ~(a&b) = a^b */
3071 ir_node *block = get_nodes_block(n);
3073 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3074 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3081 ir_node *al = get_Eor_left(a);
3082 ir_node *ar = get_Eor_right(a);
3085 /* (b ^ a) & b -> ~a & b */
3086 dbg_info *dbg = get_irn_dbg_info(n);
3087 ir_node *block = get_nodes_block(n);
3089 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3090 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3091 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3095 /* (a ^ b) & b -> ~a & b */
3096 dbg_info *dbg = get_irn_dbg_info(n);
3097 ir_node *block = get_nodes_block(n);
3099 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3100 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3106 ir_node *bl = get_Eor_left(b);
3107 ir_node *br = get_Eor_right(b);
3110 /* a & (a ^ b) -> a & ~b */
3111 dbg_info *dbg = get_irn_dbg_info(n);
3112 ir_node *block = get_nodes_block(n);
3114 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3115 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3116 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3120 /* a & (b ^ a) -> a & ~b */
3121 dbg_info *dbg = get_irn_dbg_info(n);
3122 ir_node *block = get_nodes_block(n);
3124 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3125 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3130 if (is_Not(a) && is_Not(b)) {
3131 /* ~a & ~b = ~(a|b) */
3132 ir_node *block = get_nodes_block(n);
3133 ir_mode *mode = get_irn_mode(n);
3137 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3138 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3139 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3143 n = transform_bitwise_distributive(n, transform_node_And);
3146 } /* transform_node_And */
3151 static ir_node *transform_node_Eor(ir_node *n) {
3152 ir_node *c, *oldn = n;
3153 ir_node *a = get_Eor_left(n);
3154 ir_node *b = get_Eor_right(n);
3155 ir_mode *mode = get_irn_mode(n);
3157 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3159 /* we can evaluate 2 Projs of the same Cmp */
3160 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3161 ir_node *pred_a = get_Proj_pred(a);
3162 ir_node *pred_b = get_Proj_pred(b);
3163 if(pred_a == pred_b) {
3164 dbg_info *dbgi = get_irn_dbg_info(n);
3165 ir_node *block = get_nodes_block(pred_a);
3166 pn_Cmp pn_a = get_Proj_proj(a);
3167 pn_Cmp pn_b = get_Proj_proj(b);
3168 /* yes, we can simply calculate with pncs */
3169 pn_Cmp new_pnc = pn_a ^ pn_b;
3171 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3178 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3179 mode, get_mode_null(mode));
3180 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3181 } else if (mode == mode_b &&
3183 is_Const(b) && is_Const_one(b) &&
3184 is_Cmp(get_Proj_pred(a))) {
3185 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3186 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3187 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3190 } else if (is_Const(b)) {
3191 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3192 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3193 ir_node *not_op = get_Not_op(a);
3194 dbg_info *dbg = get_irn_dbg_info(n);
3195 ir_graph *irg = current_ir_graph;
3196 ir_node *block = get_nodes_block(n);
3197 ir_mode *mode = get_irn_mode(n);
3198 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3200 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3201 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3205 n = transform_bitwise_distributive(n, transform_node_Eor);
3209 } /* transform_node_Eor */
3214 static ir_node *transform_node_Not(ir_node *n) {
3215 ir_node *c, *oldn = n;
3216 ir_node *a = get_Not_op(n);
3217 ir_mode *mode = get_irn_mode(n);
3219 HANDLE_UNOP_PHI(tarval_not,a,c);
3221 /* check for a boolean Not */
3222 if (mode == mode_b &&
3224 is_Cmp(get_Proj_pred(a))) {
3225 /* We negate a Cmp. The Cmp has the negated result anyways! */
3226 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3227 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3228 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3232 ir_node *eor_b = get_Eor_right(a);
3233 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3234 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3235 ir_node *eor_a = get_Eor_left(a);
3236 dbg_info *dbg = get_irn_dbg_info(n);
3237 ir_graph *irg = current_ir_graph;
3238 ir_node *block = get_nodes_block(n);
3239 ir_mode *mode = get_irn_mode(n);
3240 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3244 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3245 if (is_Minus(a)) { /* ~-x -> x + -1 */
3246 dbg_info *dbg = get_irn_dbg_info(n);
3247 ir_graph *irg = current_ir_graph;
3248 ir_node *block = get_nodes_block(n);
3249 ir_node *add_l = get_Minus_op(a);
3250 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3251 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3252 } else if (is_Add(a)) {
3253 ir_node *add_r = get_Add_right(a);
3254 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3255 /* ~(x + -1) = -x */
3256 ir_node *op = get_Add_left(a);
3257 ir_node *blk = get_irn_n(n, -1);
3258 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3259 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3264 } /* transform_node_Not */
3267 * Transform a Minus.
3271 * -(a >>u (size-1)) = a >>s (size-1)
3272 * -(a >>s (size-1)) = a >>u (size-1)
3273 * -(a * const) -> a * -const
3275 static ir_node *transform_node_Minus(ir_node *n) {
3276 ir_node *c, *oldn = n;
3277 ir_node *a = get_Minus_op(n);
3280 HANDLE_UNOP_PHI(tarval_neg,a,c);
3282 mode = get_irn_mode(a);
3283 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3284 /* the following rules are only to twos-complement */
3287 ir_node *op = get_Not_op(a);
3288 tarval *tv = get_mode_one(mode);
3289 ir_node *blk = get_irn_n(n, -1);
3290 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3291 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3292 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3296 ir_node *c = get_Shr_right(a);
3299 tarval *tv = get_Const_tarval(c);
3301 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3302 /* -(a >>u (size-1)) = a >>s (size-1) */
3303 ir_node *v = get_Shr_left(a);
3305 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3306 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3312 ir_node *c = get_Shrs_right(a);
3315 tarval *tv = get_Const_tarval(c);
3317 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3318 /* -(a >>s (size-1)) = a >>u (size-1) */
3319 ir_node *v = get_Shrs_left(a);
3321 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3322 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3329 /* - (a-b) = b - a */
3330 ir_node *la = get_Sub_left(a);
3331 ir_node *ra = get_Sub_right(a);
3332 ir_node *blk = get_irn_n(n, -1);
3334 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3335 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3339 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3340 ir_node *mul_l = get_Mul_left(a);
3341 ir_node *mul_r = get_Mul_right(a);
3342 if (is_Const(mul_r)) {
3343 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3344 if(tv != tarval_bad) {
3345 ir_node *cnst = new_Const(mode, tv);
3346 dbg_info *dbg = get_irn_dbg_info(a);
3347 ir_graph *irg = current_ir_graph;
3348 ir_node *block = get_nodes_block(a);
3349 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3350 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3357 } /* transform_node_Minus */
3360 * Transform a Cast_type(Const) into a new Const_type
3362 static ir_node *transform_node_Cast(ir_node *n) {
3364 ir_node *pred = get_Cast_op(n);
3365 ir_type *tp = get_irn_type(n);
3367 if (is_Const(pred) && get_Const_type(pred) != tp) {
3368 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3369 get_Const_tarval(pred), tp);
3370 DBG_OPT_CSTEVAL(oldn, n);
3371 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3372 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3373 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3374 DBG_OPT_CSTEVAL(oldn, n);
3378 } /* transform_node_Cast */
3381 * Transform a Proj(Div) with a non-zero value.
3382 * Removes the exceptions and routes the memory to the NoMem node.
3384 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3385 ir_node *div = get_Proj_pred(proj);
3386 ir_node *b = get_Div_right(div);
3387 ir_node *confirm, *res, *new_mem;
3390 if (value_not_zero(b, &confirm)) {
3391 /* div(x, y) && y != 0 */
3392 if (confirm == NULL) {
3393 /* we are sure we have a Const != 0 */
3394 new_mem = get_Div_mem(div);
3395 if (is_Pin(new_mem))
3396 new_mem = get_Pin_op(new_mem);
3397 set_Div_mem(div, new_mem);
3398 set_irn_pinned(div, op_pin_state_floats);
3401 proj_nr = get_Proj_proj(proj);
3403 case pn_Div_X_regular:
3404 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3406 case pn_Div_X_except:
3407 /* we found an exception handler, remove it */
3408 DBG_OPT_EXC_REM(proj);
3412 res = get_Div_mem(div);
3413 new_mem = get_irg_no_mem(current_ir_graph);
3416 /* This node can only float up to the Confirm block */
3417 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3419 set_irn_pinned(div, op_pin_state_floats);
3420 /* this is a Div without exception, we can remove the memory edge */
3421 set_Div_mem(div, new_mem);
3426 } /* transform_node_Proj_Div */
3429 * Transform a Proj(Mod) with a non-zero value.
3430 * Removes the exceptions and routes the memory to the NoMem node.
3432 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3433 ir_node *mod = get_Proj_pred(proj);
3434 ir_node *b = get_Mod_right(mod);
3435 ir_node *confirm, *res, *new_mem;
3438 if (value_not_zero(b, &confirm)) {
3439 /* mod(x, y) && y != 0 */
3440 proj_nr = get_Proj_proj(proj);
3442 if (confirm == NULL) {
3443 /* we are sure we have a Const != 0 */
3444 new_mem = get_Mod_mem(mod);
3445 if (is_Pin(new_mem))
3446 new_mem = get_Pin_op(new_mem);
3447 set_Mod_mem(mod, new_mem);
3448 set_irn_pinned(mod, op_pin_state_floats);
3453 case pn_Mod_X_regular:
3454 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3456 case pn_Mod_X_except:
3457 /* we found an exception handler, remove it */
3458 DBG_OPT_EXC_REM(proj);
3462 res = get_Mod_mem(mod);
3463 new_mem = get_irg_no_mem(current_ir_graph);
3466 /* This node can only float up to the Confirm block */
3467 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3469 /* this is a Mod without exception, we can remove the memory edge */
3470 set_Mod_mem(mod, new_mem);
3473 if (get_Mod_left(mod) == b) {
3474 /* a % a = 0 if a != 0 */
3475 ir_mode *mode = get_irn_mode(proj);
3476 ir_node *res = new_Const(mode, get_mode_null(mode));
3478 DBG_OPT_CSTEVAL(mod, res);
3484 } /* transform_node_Proj_Mod */
3487 * Transform a Proj(DivMod) with a non-zero value.
3488 * Removes the exceptions and routes the memory to the NoMem node.
3490 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3491 ir_node *divmod = get_Proj_pred(proj);
3492 ir_node *b = get_DivMod_right(divmod);
3493 ir_node *confirm, *res, *new_mem;
3496 if (value_not_zero(b, &confirm)) {
3497 /* DivMod(x, y) && y != 0 */
3498 proj_nr = get_Proj_proj(proj);
3500 if (confirm == NULL) {
3501 /* we are sure we have a Const != 0 */
3502 new_mem = get_DivMod_mem(divmod);
3503 if (is_Pin(new_mem))
3504 new_mem = get_Pin_op(new_mem);
3505 set_DivMod_mem(divmod, new_mem);
3506 set_irn_pinned(divmod, op_pin_state_floats);
3511 case pn_DivMod_X_regular:
3512 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3514 case pn_DivMod_X_except:
3515 /* we found an exception handler, remove it */
3516 DBG_OPT_EXC_REM(proj);
3520 res = get_DivMod_mem(divmod);
3521 new_mem = get_irg_no_mem(current_ir_graph);
3524 /* This node can only float up to the Confirm block */
3525 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3527 /* this is a DivMod without exception, we can remove the memory edge */
3528 set_DivMod_mem(divmod, new_mem);
3531 case pn_DivMod_res_mod:
3532 if (get_DivMod_left(divmod) == b) {
3533 /* a % a = 0 if a != 0 */
3534 ir_mode *mode = get_irn_mode(proj);
3535 ir_node *res = new_Const(mode, get_mode_null(mode));
3537 DBG_OPT_CSTEVAL(divmod, res);
3543 } /* transform_node_Proj_DivMod */
3546 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3548 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3549 if (get_opt_unreachable_code()) {
3550 ir_node *n = get_Proj_pred(proj);
3551 ir_node *b = get_Cond_selector(n);
3553 if (mode_is_int(get_irn_mode(b))) {
3554 tarval *tb = value_of(b);
3556 if (tb != tarval_bad) {
3557 /* we have a constant switch */
3558 long num = get_Proj_proj(proj);
3560 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3561 if (get_tarval_long(tb) == num) {
3562 /* Do NOT create a jump here, or we will have 2 control flow ops
3563 * in a block. This case is optimized away in optimize_cf(). */
3566 /* this case will NEVER be taken, kill it */
3574 } /* transform_node_Proj_Cond */
3577 * Create a 0 constant of given mode.
3579 static ir_node *create_zero_const(ir_mode *mode) {
3580 tarval *tv = get_mode_null(mode);
3581 ir_node *cnst = new_Const(mode, tv);
3586 /* the order of the values is important! */
3587 typedef enum const_class {
3593 static const_class classify_const(const ir_node* n)
3595 if (is_Const(n)) return const_const;
3596 if (is_irn_constlike(n)) return const_like;
3601 * Determines whether r is more constlike or has a larger index (in that order)
3604 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3606 const const_class l_order = classify_const(l);
3607 const const_class r_order = classify_const(r);
3609 l_order > r_order ||
3610 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3614 * Normalizes and optimizes Cmp nodes.
3616 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3617 ir_node *n = get_Proj_pred(proj);
3618 ir_node *left = get_Cmp_left(n);
3619 ir_node *right = get_Cmp_right(n);
3622 ir_mode *mode = NULL;
3623 long proj_nr = get_Proj_proj(proj);
3625 /* we can evaluate some cases directly */
3628 return new_Const(mode_b, get_tarval_b_false());
3630 return new_Const(mode_b, get_tarval_b_true());
3632 if (!mode_is_float(get_irn_mode(left)))
3633 return new_Const(mode_b, get_tarval_b_true());
3641 left = get_Cast_op(left);
3643 right = get_Cast_op(right);
3645 /* Remove unnecessary conversions */
3646 /* TODO handle constants */
3647 if (is_Conv(left) && is_Conv(right)) {
3648 ir_mode *mode = get_irn_mode(left);
3649 ir_node *op_left = get_Conv_op(left);
3650 ir_node *op_right = get_Conv_op(right);
3651 ir_mode *mode_left = get_irn_mode(op_left);
3652 ir_mode *mode_right = get_irn_mode(op_right);
3654 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3655 && mode_left != mode_b && mode_right != mode_b) {
3656 ir_graph *irg = current_ir_graph;
3657 ir_node *block = get_nodes_block(n);
3659 if (mode_left == mode_right) {
3663 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3664 } else if (smaller_mode(mode_left, mode_right)) {
3665 left = new_r_Conv(irg, block, op_left, mode_right);
3668 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3669 } else if (smaller_mode(mode_right, mode_left)) {
3671 right = new_r_Conv(irg, block, op_right, mode_left);
3673 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3678 /* remove operation of both sides if possible */
3679 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3681 * The following operations are NOT safe for floating point operations, for instance
3682 * 1.0 + inf == 2.0 + inf, =/=> x == y
3684 if (mode_is_int(get_irn_mode(left))) {
3685 unsigned lop = get_irn_opcode(left);
3687 if (lop == get_irn_opcode(right)) {
3688 ir_node *ll, *lr, *rl, *rr;
3690 /* same operation on both sides, try to remove */
3694 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3695 left = get_unop_op(left);
3696 right = get_unop_op(right);
3698 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3701 ll = get_Add_left(left);
3702 lr = get_Add_right(left);
3703 rl = get_Add_left(right);
3704 rr = get_Add_right(right);
3707 /* X + a CMP X + b ==> a CMP b */
3711 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3712 } else if (ll == rr) {
3713 /* X + a CMP b + X ==> a CMP b */
3717 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3718 } else if (lr == rl) {
3719 /* a + X CMP X + b ==> a CMP b */
3723 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3724 } else if (lr == rr) {
3725 /* a + X CMP b + X ==> a CMP b */
3729 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3733 ll = get_Sub_left(left);
3734 lr = get_Sub_right(left);
3735 rl = get_Sub_left(right);
3736 rr = get_Sub_right(right);
3739 /* X - a CMP X - b ==> a CMP b */
3743 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3744 } else if (lr == rr) {
3745 /* a - X CMP b - X ==> a CMP b */
3749 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3753 if (get_Rot_right(left) == get_Rot_right(right)) {
3754 /* a ROT X CMP b ROT X ==> a CMP b */
3755 left = get_Rot_left(left);
3756 right = get_Rot_left(right);
3758 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3766 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3767 if (is_Add(left) || is_Sub(left)) {
3768 ir_node *ll = get_binop_left(left);
3769 ir_node *lr = get_binop_right(left);
3771 if (lr == right && is_Add(left)) {
3778 right = create_zero_const(get_irn_mode(left));
3780 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3783 if (is_Add(right) || is_Sub(right)) {
3784 ir_node *rl = get_binop_left(right);
3785 ir_node *rr = get_binop_right(right);
3787 if (rr == left && is_Add(right)) {
3794 right = create_zero_const(get_irn_mode(left));
3796 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3799 } /* mode_is_int(...) */
3800 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3802 /* replace mode_b compares with ands/ors */
3803 if (get_irn_mode(left) == mode_b) {
3804 ir_graph *irg = current_ir_graph;
3805 ir_node *block = get_nodes_block(n);
3809 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3810 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3811 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3812 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3813 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3814 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3815 default: bres = NULL;
3818 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3824 * First step: normalize the compare op
3825 * by placing the constant on the right side
3826 * or moving the lower address node to the left.
3828 if (!operands_are_normalized(left, right)) {
3834 proj_nr = get_inversed_pnc(proj_nr);
3839 * Second step: Try to reduce the magnitude
3840 * of a constant. This may help to generate better code
3841 * later and may help to normalize more compares.
3842 * Of course this is only possible for integer values.
3844 if (is_Const(right)) {
3845 mode = get_irn_mode(right);
3846 tv = get_Const_tarval(right);
3848 /* TODO extend to arbitrary constants */
3849 if (is_Conv(left) && tarval_is_null(tv)) {
3850 ir_node *op = get_Conv_op(left);
3851 ir_mode *op_mode = get_irn_mode(op);
3854 * UpConv(x) REL 0 ==> x REL 0
3856 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3857 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3858 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3859 tv = get_mode_null(op_mode);
3863 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3867 if (tv != tarval_bad) {
3868 /* the following optimization is possible on modes without Overflow
3869 * on Unary Minus or on == and !=:
3870 * -a CMP c ==> a swap(CMP) -c
3872 * Beware: for two-complement Overflow may occur, so only == and != can
3873 * be optimized, see this:
3874 * -MININT < 0 =/=> MININT > 0 !!!
3876 if (is_Minus(left) &&
3877 (!mode_overflow_on_unary_Minus(mode) ||
3878 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3879 tv = tarval_neg(tv);
3881 if (tv != tarval_bad) {
3882 left = get_Minus_op(left);
3883 proj_nr = get_inversed_pnc(proj_nr);
3885 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3887 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3888 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3889 tv = tarval_not(tv);
3891 if (tv != tarval_bad) {
3892 left = get_Not_op(left);
3894 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3898 /* for integer modes, we have more */
3899 if (mode_is_int(mode)) {
3900 /* Ne includes Unordered which is not possible on integers.
3901 * However, frontends often use this wrong, so fix it here */
3902 if (proj_nr & pn_Cmp_Uo) {
3903 proj_nr &= ~pn_Cmp_Uo;
3904 set_Proj_proj(proj, proj_nr);
3907 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3908 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3909 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3910 tv = tarval_sub(tv, get_mode_one(mode));
3912 if (tv != tarval_bad) {
3913 proj_nr ^= pn_Cmp_Eq;
3915 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3918 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3919 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3920 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3921 tv = tarval_add(tv, get_mode_one(mode));
3923 if (tv != tarval_bad) {
3924 proj_nr ^= pn_Cmp_Eq;
3926 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3930 /* the following reassociations work only for == and != */
3931 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3933 #if 0 /* Might be not that good in general */
3934 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3935 if (tarval_is_null(tv) && is_Sub(left)) {
3936 right = get_Sub_right(left);
3937 left = get_Sub_left(left);
3939 tv = value_of(right);
3941 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3945 if (tv != tarval_bad) {
3946 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3948 ir_node *c1 = get_Sub_right(left);
3949 tarval *tv2 = value_of(c1);
3951 if (tv2 != tarval_bad) {
3952 tv2 = tarval_add(tv, value_of(c1));
3954 if (tv2 != tarval_bad) {
3955 left = get_Sub_left(left);
3958 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3962 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3963 else if (is_Add(left)) {
3964 ir_node *a_l = get_Add_left(left);
3965 ir_node *a_r = get_Add_right(left);
3969 if (is_Const(a_l)) {
3971 tv2 = value_of(a_l);
3974 tv2 = value_of(a_r);
3977 if (tv2 != tarval_bad) {
3978 tv2 = tarval_sub(tv, tv2);
3980 if (tv2 != tarval_bad) {
3984 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3988 /* -a == c ==> a == -c, -a != c ==> a != -c */
3989 else if (is_Minus(left)) {
3990 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3992 if (tv2 != tarval_bad) {
3993 left = get_Minus_op(left);
3996 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4001 /* the following reassociations work only for <= */
4002 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4003 if (tv != tarval_bad) {
4004 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4005 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4011 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4012 switch (get_irn_opcode(left)) {
4016 c1 = get_And_right(left);
4019 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4020 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4022 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4024 /* TODO: move to constant evaluation */
4025 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4026 c1 = new_Const(mode_b, tv);
4027 DBG_OPT_CSTEVAL(proj, c1);
4031 if (tarval_is_single_bit(tv)) {
4033 * optimization for AND:
4035 * And(x, C) == C ==> And(x, C) != 0
4036 * And(x, C) != C ==> And(X, C) == 0
4038 * if C is a single Bit constant.
4041 /* check for Constant's match. We have check hare the tarvals,
4042 because our const might be changed */
4043 if (get_Const_tarval(c1) == tv) {
4044 /* fine: do the transformation */
4045 tv = get_mode_null(get_tarval_mode(tv));
4046 proj_nr ^= pn_Cmp_Leg;
4048 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4054 c1 = get_Or_right(left);
4055 if (is_Const(c1) && tarval_is_null(tv)) {
4057 * Or(x, C) == 0 && C != 0 ==> FALSE
4058 * Or(x, C) != 0 && C != 0 ==> TRUE
4060 if (! tarval_is_null(get_Const_tarval(c1))) {
4061 /* TODO: move to constant evaluation */
4062 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4063 c1 = new_Const(mode_b, tv);
4064 DBG_OPT_CSTEVAL(proj, c1);
4071 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4073 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4076 c1 = get_Shl_right(left);
4078 tarval *tv1 = get_Const_tarval(c1);
4079 ir_mode *mode = get_irn_mode(left);
4080 tarval *minus1 = get_mode_all_one(mode);
4081 tarval *amask = tarval_shr(minus1, tv1);
4082 tarval *cmask = tarval_shl(minus1, tv1);
4085 if (tarval_and(tv, cmask) != tv) {
4086 /* condition not met */
4087 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4088 c1 = new_Const(mode_b, tv);
4089 DBG_OPT_CSTEVAL(proj, c1);
4092 sl = get_Shl_left(left);
4093 blk = get_nodes_block(n);
4094 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4095 tv = tarval_shr(tv, tv1);
4097 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4102 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4104 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4107 c1 = get_Shr_right(left);
4109 tarval *tv1 = get_Const_tarval(c1);
4110 ir_mode *mode = get_irn_mode(left);
4111 tarval *minus1 = get_mode_all_one(mode);
4112 tarval *amask = tarval_shl(minus1, tv1);
4113 tarval *cmask = tarval_shr(minus1, tv1);
4116 if (tarval_and(tv, cmask) != tv) {
4117 /* condition not met */
4118 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4119 c1 = new_Const(mode_b, tv);
4120 DBG_OPT_CSTEVAL(proj, c1);
4123 sl = get_Shr_left(left);
4124 blk = get_nodes_block(n);
4125 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4126 tv = tarval_shl(tv, tv1);
4128 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4133 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4135 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4138 c1 = get_Shrs_right(left);
4140 tarval *tv1 = get_Const_tarval(c1);
4141 ir_mode *mode = get_irn_mode(left);
4142 tarval *minus1 = get_mode_all_one(mode);
4143 tarval *amask = tarval_shl(minus1, tv1);
4144 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4147 cond = tarval_sub(cond, tv1);
4148 cond = tarval_shrs(tv, cond);
4150 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4151 /* condition not met */
4152 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4153 c1 = new_Const(mode_b, tv);
4154 DBG_OPT_CSTEVAL(proj, c1);
4157 sl = get_Shrs_left(left);
4158 blk = get_nodes_block(n);
4159 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4160 tv = tarval_shl(tv, tv1);
4162 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4167 } /* tarval != bad */
4170 if (changed & 2) /* need a new Const */
4171 right = new_Const(mode, tv);
4173 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4174 ir_node *op = get_Proj_pred(left);
4176 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4177 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4178 ir_node *c = get_binop_right(op);
4181 tarval *tv = get_Const_tarval(c);
4183 if (tarval_is_single_bit(tv)) {
4184 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4185 ir_node *v = get_binop_left(op);
4186 ir_node *blk = get_irn_n(op, -1);
4187 ir_mode *mode = get_irn_mode(v);
4189 tv = tarval_sub(tv, get_mode_one(mode));
4190 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4192 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4199 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4201 /* create a new compare */
4202 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4203 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4207 } /* transform_node_Proj_Cmp */
4210 * Does all optimizations on nodes that must be done on it's Proj's
4211 * because of creating new nodes.
4213 static ir_node *transform_node_Proj(ir_node *proj) {
4214 ir_node *n = get_Proj_pred(proj);
4216 switch (get_irn_opcode(n)) {
4218 return transform_node_Proj_Div(proj);
4221 return transform_node_Proj_Mod(proj);
4224 return transform_node_Proj_DivMod(proj);
4227 return transform_node_Proj_Cond(proj);
4230 return transform_node_Proj_Cmp(proj);
4233 /* should not happen, but if it does will be optimized away */
4234 return equivalent_node_Proj(proj);
4240 } /* transform_node_Proj */
4243 * Move Confirms down through Phi nodes.
4245 static ir_node *transform_node_Phi(ir_node *phi) {
4247 ir_mode *mode = get_irn_mode(phi);
4249 if (mode_is_reference(mode)) {
4250 n = get_irn_arity(phi);
4252 /* Beware of Phi0 */
4254 ir_node *pred = get_irn_n(phi, 0);
4255 ir_node *bound, *new_Phi, *block, **in;
4258 if (! is_Confirm(pred))
4261 bound = get_Confirm_bound(pred);
4262 pnc = get_Confirm_cmp(pred);
4264 NEW_ARR_A(ir_node *, in, n);
4265 in[0] = get_Confirm_value(pred);
4267 for (i = 1; i < n; ++i) {
4268 pred = get_irn_n(phi, i);
4270 if (! is_Confirm(pred) ||
4271 get_Confirm_bound(pred) != bound ||
4272 get_Confirm_cmp(pred) != pnc)
4274 in[i] = get_Confirm_value(pred);
4276 /* move the Confirm nodes "behind" the Phi */
4277 block = get_irn_n(phi, -1);
4278 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4279 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4283 } /* transform_node_Phi */
4286 * Returns the operands of a commutative bin-op, if one operand is
4287 * a const, it is returned as the second one.
4289 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4290 ir_node *op_a = get_binop_left(binop);
4291 ir_node *op_b = get_binop_right(binop);
4293 assert(is_op_commutative(get_irn_op(binop)));
4295 if (is_Const(op_a)) {
4302 } /* get_comm_Binop_Ops */
4305 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4306 * Such pattern may arise in bitfield stores.
4308 * value c4 value c4 & c2
4309 * AND c3 AND c1 | c3
4316 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4319 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4322 ir_node *and_l, *c3;
4323 ir_node *value, *c4;
4324 ir_node *new_and, *new_const, *block;
4325 ir_mode *mode = get_irn_mode(or);
4327 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4330 get_comm_Binop_Ops(or, &and, &c1);
4331 if (!is_Const(c1) || !is_And(and))
4334 get_comm_Binop_Ops(and, &or_l, &c2);
4338 tv1 = get_Const_tarval(c1);
4339 tv2 = get_Const_tarval(c2);
4341 tv = tarval_or(tv1, tv2);
4342 if (tarval_is_all_one(tv)) {
4343 /* the AND does NOT clear a bit with isn't set by the OR */
4344 set_Or_left(or, or_l);
4345 set_Or_right(or, c1);
4347 /* check for more */
4354 get_comm_Binop_Ops(or_l, &and_l, &c3);
4355 if (!is_Const(c3) || !is_And(and_l))
4358 get_comm_Binop_Ops(and_l, &value, &c4);
4362 /* ok, found the pattern, check for conditions */
4363 assert(mode == get_irn_mode(and));
4364 assert(mode == get_irn_mode(or_l));
4365 assert(mode == get_irn_mode(and_l));
4367 tv3 = get_Const_tarval(c3);
4368 tv4 = get_Const_tarval(c4);
4370 tv = tarval_or(tv4, tv2);
4371 if (!tarval_is_all_one(tv)) {
4372 /* have at least one 0 at the same bit position */
4376 n_tv4 = tarval_not(tv4);
4377 if (tv3 != tarval_and(tv3, n_tv4)) {
4378 /* bit in the or_mask is outside the and_mask */
4382 n_tv2 = tarval_not(tv2);
4383 if (tv1 != tarval_and(tv1, n_tv2)) {
4384 /* bit in the or_mask is outside the and_mask */
4388 /* ok, all conditions met */
4389 block = get_irn_n(or, -1);
4391 new_and = new_r_And(current_ir_graph, block,
4392 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4394 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4396 set_Or_left(or, new_and);
4397 set_Or_right(or, new_const);
4399 /* check for more */
4401 } /* transform_node_Or_bf_store */
4404 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4406 static ir_node *transform_node_Or_Rot(ir_node *or) {
4407 ir_mode *mode = get_irn_mode(or);
4408 ir_node *shl, *shr, *block;
4409 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4412 if (! mode_is_int(mode))
4415 shl = get_binop_left(or);
4416 shr = get_binop_right(or);
4425 } else if (!is_Shl(shl)) {
4427 } else if (!is_Shr(shr)) {
4430 x = get_Shl_left(shl);
4431 if (x != get_Shr_left(shr))
4434 c1 = get_Shl_right(shl);
4435 c2 = get_Shr_right(shr);
4436 if (is_Const(c1) && is_Const(c2)) {
4437 tv1 = get_Const_tarval(c1);
4438 if (! tarval_is_long(tv1))
4441 tv2 = get_Const_tarval(c2);
4442 if (! tarval_is_long(tv2))
4445 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4446 != (int) get_mode_size_bits(mode))
4449 /* yet, condition met */
4450 block = get_irn_n(or, -1);
4452 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4454 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4456 } else if (is_Sub(c1)) {
4460 if (get_Sub_right(sub) != v)
4463 c1 = get_Sub_left(sub);
4467 tv1 = get_Const_tarval(c1);
4468 if (! tarval_is_long(tv1))
4471 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4474 /* yet, condition met */
4475 block = get_nodes_block(or);
4477 /* a Rot right is not supported, so use a rot left */
4478 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4480 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4482 } else if (is_Sub(c2)) {
4486 c1 = get_Sub_left(sub);
4490 tv1 = get_Const_tarval(c1);
4491 if (! tarval_is_long(tv1))
4494 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4497 /* yet, condition met */
4498 block = get_irn_n(or, -1);
4501 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4503 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4508 } /* transform_node_Or_Rot */
4513 static ir_node *transform_node_Or(ir_node *n) {
4514 ir_node *c, *oldn = n;
4515 ir_node *a = get_Or_left(n);
4516 ir_node *b = get_Or_right(n);
4519 if (is_Not(a) && is_Not(b)) {
4520 /* ~a | ~b = ~(a&b) */
4521 ir_node *block = get_nodes_block(n);
4523 mode = get_irn_mode(n);
4526 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4527 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4532 /* we can evaluate 2 Projs of the same Cmp */
4533 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4534 ir_node *pred_a = get_Proj_pred(a);
4535 ir_node *pred_b = get_Proj_pred(b);
4536 if (pred_a == pred_b) {
4537 dbg_info *dbgi = get_irn_dbg_info(n);
4538 ir_node *block = get_nodes_block(pred_a);
4539 pn_Cmp pn_a = get_Proj_proj(a);
4540 pn_Cmp pn_b = get_Proj_proj(b);
4541 /* yes, we can simply calculate with pncs */
4542 pn_Cmp new_pnc = pn_a | pn_b;
4544 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4549 mode = get_irn_mode(n);
4550 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4552 n = transform_node_Or_bf_store(n);
4553 n = transform_node_Or_Rot(n);
4557 n = transform_bitwise_distributive(n, transform_node_Or);
4560 } /* transform_node_Or */
4564 static ir_node *transform_node(ir_node *n);
4567 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4569 * Should be moved to reassociation?
4571 static ir_node *transform_node_shift(ir_node *n) {
4572 ir_node *left, *right;
4573 tarval *tv1, *tv2, *res;
4575 int modulo_shf, flag;
4577 left = get_binop_left(n);
4579 /* different operations */
4580 if (get_irn_op(left) != get_irn_op(n))
4583 right = get_binop_right(n);
4584 tv1 = value_of(right);
4585 if (tv1 == tarval_bad)
4588 tv2 = value_of(get_binop_right(left));
4589 if (tv2 == tarval_bad)
4592 res = tarval_add(tv1, tv2);
4594 /* beware: a simple replacement works only, if res < modulo shift */
4595 mode = get_irn_mode(n);
4599 modulo_shf = get_mode_modulo_shift(mode);
4600 if (modulo_shf > 0) {
4601 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4603 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4609 /* ok, we can replace it */
4610 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4612 in[0] = get_binop_left(left);
4613 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4615 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4617 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4619 return transform_node(irn);
4622 } /* transform_node_shift */
4627 static ir_node *transform_node_Shr(ir_node *n) {
4628 ir_node *c, *oldn = n;
4629 ir_node *a = get_Shr_left(n);
4630 ir_node *b = get_Shr_right(n);
4631 ir_mode *mode = get_irn_mode(n);
4633 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4634 return transform_node_shift(n);
4635 } /* transform_node_Shr */
4640 static ir_node *transform_node_Shrs(ir_node *n) {
4641 ir_node *c, *oldn = n;
4642 ir_node *a = get_Shrs_left(n);
4643 ir_node *b = get_Shrs_right(n);
4644 ir_mode *mode = get_irn_mode(n);
4646 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4647 return transform_node_shift(n);
4648 } /* transform_node_Shrs */
4653 static ir_node *transform_node_Shl(ir_node *n) {
4654 ir_node *c, *oldn = n;
4655 ir_node *a = get_Shl_left(n);
4656 ir_node *b = get_Shl_right(n);
4657 ir_mode *mode = get_irn_mode(n);
4659 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4660 return transform_node_shift(n);
4661 } /* transform_node_Shl */
4666 static ir_node *transform_node_Rot(ir_node *n) {
4667 ir_node *c, *oldn = n;
4668 ir_node *a = get_Rot_left(n);
4669 ir_node *b = get_Rot_right(n);
4670 ir_mode *mode = get_irn_mode(n);
4672 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4673 return transform_node_shift(n);
4674 } /* transform_node_Rot */
4679 static ir_node *transform_node_Conv(ir_node *n) {
4680 ir_node *c, *oldn = n;
4681 ir_node *a = get_Conv_op(n);
4683 if (is_const_Phi(a)) {
4684 c = apply_conv_on_phi(a, get_irn_mode(n));
4686 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4691 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4692 ir_mode *mode = get_irn_mode(n);
4693 return new_r_Unknown(current_ir_graph, mode);
4697 } /* transform_node_Conv */
4700 * Remove dead blocks and nodes in dead blocks
4701 * in keep alive list. We do not generate a new End node.
4703 static ir_node *transform_node_End(ir_node *n) {
4704 int i, j, n_keepalives = get_End_n_keepalives(n);
4707 NEW_ARR_A(ir_node *, in, n_keepalives);
4709 for (i = j = 0; i < n_keepalives; ++i) {
4710 ir_node *ka = get_End_keepalive(n, i);
4712 if (! is_Block_dead(ka)) {
4716 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4719 /* FIXME: beabi need to keep a Proj(M) */
4720 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4723 if (j != n_keepalives)
4724 set_End_keepalives(n, j, in);
4726 } /* transform_node_End */
4728 /** returns 1 if a == -b */
4729 static int is_negated_value(ir_node *a, ir_node *b) {
4730 if(is_Minus(a) && get_Minus_op(a) == b)
4732 if(is_Minus(b) && get_Minus_op(b) == a)
4734 if(is_Sub(a) && is_Sub(b)) {
4735 ir_node *a_left = get_Sub_left(a);
4736 ir_node *a_right = get_Sub_right(a);
4737 ir_node *b_left = get_Sub_left(b);
4738 ir_node *b_right = get_Sub_right(b);
4740 if(a_left == b_right && a_right == b_left)
4748 * Optimize a Mux into some simpler cases.
4750 static ir_node *transform_node_Mux(ir_node *n) {
4751 ir_node *oldn = n, *sel = get_Mux_sel(n);
4752 ir_mode *mode = get_irn_mode(n);
4754 if (mode == mode_b) {
4755 ir_node *t = get_Mux_true(n);
4756 ir_node *f = get_Mux_false(n);
4757 dbg_info *dbg = get_irn_dbg_info(n);
4758 ir_node *block = get_irn_n(n, -1);
4759 ir_graph *irg = current_ir_graph;
4762 tarval *tv_t = get_Const_tarval(t);
4763 if (tv_t == tarval_b_true) {
4765 assert(get_Const_tarval(f) == tarval_b_false);
4768 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4771 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4772 assert(tv_t == tarval_b_false);
4774 assert(get_Const_tarval(f) == tarval_b_true);
4777 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4780 } else if (is_Const(f)) {
4781 tarval *tv_f = get_Const_tarval(f);
4782 if (tv_f == tarval_b_true) {
4783 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4784 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4786 assert(tv_f == tarval_b_false);
4787 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4792 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4793 ir_node *cmp = get_Proj_pred(sel);
4794 long pn = get_Proj_proj(sel);
4795 ir_node *f = get_Mux_false(n);
4796 ir_node *t = get_Mux_true(n);
4799 * Note: normalization puts the constant on the right side,
4800 * so we check only one case.
4802 * Note further that these optimization work even for floating point
4803 * with NaN's because -NaN == NaN.
4804 * However, if +0 and -0 is handled differently, we cannot use the first
4808 ir_node *cmp_r = get_Cmp_right(cmp);
4809 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4810 ir_node *block = get_irn_n(n, -1);
4812 if(is_negated_value(f, t)) {
4813 ir_node *cmp_left = get_Cmp_left(cmp);
4815 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4816 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4817 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4819 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4821 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4823 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4824 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4825 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4827 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4829 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4831 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4838 return arch_transform_node_Mux(n);
4839 } /* transform_node_Mux */
4842 * Optimize a Psi into some simpler cases.
4844 static ir_node *transform_node_Psi(ir_node *n) {
4846 return transform_node_Mux(n);
4849 } /* transform_node_Psi */
4852 * optimize sync nodes that have other syncs as input we simply add the inputs
4853 * of the other sync to our own inputs
4855 static ir_node *transform_node_Sync(ir_node *n) {
4858 arity = get_irn_arity(n);
4859 for(i = 0; i < get_irn_arity(n); /*empty*/) {
4861 ir_node *in = get_irn_n(n, i);
4867 /* set sync input 0 instead of the sync */
4868 set_irn_n(n, i, get_irn_n(in, 0));
4869 /* so we check this input again for syncs */
4871 /* append all other inputs of the sync to our sync */
4872 arity2 = get_irn_arity(in);
4873 for(i2 = 1; i2 < arity2; ++i2) {
4874 ir_node *in_in = get_irn_n(in, i2);
4875 add_irn_n(n, in_in);
4876 /* increase arity so we also check the new inputs for syncs */
4881 /* rehash the sync node */
4882 add_identities(current_ir_graph->value_table, n);
4888 * Tries several [inplace] [optimizing] transformations and returns an
4889 * equivalent node. The difference to equivalent_node() is that these
4890 * transformations _do_ generate new nodes, and thus the old node must
4891 * not be freed even if the equivalent node isn't the old one.
4893 static ir_node *transform_node(ir_node *n) {
4897 * Transform_node is the only "optimizing transformation" that might
4898 * return a node with a different opcode. We iterate HERE until fixpoint
4899 * to get the final result.
4903 if (n->op->ops.transform_node)
4904 n = n->op->ops.transform_node(n);
4905 } while (oldn != n);
4908 } /* transform_node */
4911 * Sets the default transform node operation for an ir_op_ops.
4913 * @param code the opcode for the default operation
4914 * @param ops the operations initialized
4919 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4923 ops->transform_node = transform_node_##a; \
4960 } /* firm_set_default_transform_node */
4963 /* **************** Common Subexpression Elimination **************** */
4965 /** The size of the hash table used, should estimate the number of nodes
4967 #define N_IR_NODES 512
4969 /** Compares the attributes of two Const nodes. */
4970 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4971 return (get_Const_tarval(a) != get_Const_tarval(b))
4972 || (get_Const_type(a) != get_Const_type(b));
4973 } /* node_cmp_attr_Const */
4975 /** Compares the attributes of two Proj nodes. */
4976 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4977 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4978 } /* node_cmp_attr_Proj */
4980 /** Compares the attributes of two Filter nodes. */
4981 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4982 return get_Filter_proj(a) != get_Filter_proj(b);
4983 } /* node_cmp_attr_Filter */
4985 /** Compares the attributes of two Alloc nodes. */
4986 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4987 const alloc_attr *pa = get_irn_alloc_attr(a);
4988 const alloc_attr *pb = get_irn_alloc_attr(b);
4989 return (pa->where != pb->where) || (pa->type != pb->type);
4990 } /* node_cmp_attr_Alloc */
4992 /** Compares the attributes of two Free nodes. */
4993 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4994 const free_attr *pa = get_irn_free_attr(a);
4995 const free_attr *pb = get_irn_free_attr(b);
4996 return (pa->where != pb->where) || (pa->type != pb->type);
4997 } /* node_cmp_attr_Free */
4999 /** Compares the attributes of two SymConst nodes. */
5000 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5001 const symconst_attr *pa = get_irn_symconst_attr(a);
5002 const symconst_attr *pb = get_irn_symconst_attr(b);
5003 return (pa->num != pb->num)
5004 || (pa->sym.type_p != pb->sym.type_p)
5005 || (pa->tp != pb->tp);
5006 } /* node_cmp_attr_SymConst */
5008 /** Compares the attributes of two Call nodes. */
5009 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5010 return (get_irn_call_attr(a) != get_irn_call_attr(b));
5011 } /* node_cmp_attr_Call */
5013 /** Compares the attributes of two Sel nodes. */
5014 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5015 const ir_entity *a_ent = get_Sel_entity(a);
5016 const ir_entity *b_ent = get_Sel_entity(b);
5018 (a_ent->kind != b_ent->kind) ||
5019 (a_ent->name != b_ent->name) ||
5020 (a_ent->owner != b_ent->owner) ||
5021 (a_ent->ld_name != b_ent->ld_name) ||
5022 (a_ent->type != b_ent->type);
5023 } /* node_cmp_attr_Sel */
5025 /** Compares the attributes of two Phi nodes. */
5026 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5027 /* we can only enter this function if both nodes have the same number of inputs,
5028 hence it is enough to check if one of them is a Phi0 */
5030 /* check the Phi0 pos attribute */
5031 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5034 } /* node_cmp_attr_Phi */
5036 /** Compares the attributes of two Conv nodes. */
5037 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5038 return get_Conv_strict(a) != get_Conv_strict(b);
5039 } /* node_cmp_attr_Conv */
5041 /** Compares the attributes of two Cast nodes. */
5042 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5043 return get_Cast_type(a) != get_Cast_type(b);
5044 } /* node_cmp_attr_Cast */
5046 /** Compares the attributes of two Load nodes. */
5047 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5048 if (get_Load_volatility(a) == volatility_is_volatile ||
5049 get_Load_volatility(b) == volatility_is_volatile)
5050 /* NEVER do CSE on volatile Loads */
5052 /* do not CSE Loads with different alignment. Be conservative. */
5053 if (get_Load_align(a) != get_Load_align(b))
5056 return get_Load_mode(a) != get_Load_mode(b);
5057 } /* node_cmp_attr_Load */
5059 /** Compares the attributes of two Store nodes. */
5060 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5061 /* do not CSE Stores with different alignment. Be conservative. */
5062 if (get_Store_align(a) != get_Store_align(b))
5065 /* NEVER do CSE on volatile Stores */
5066 return (get_Store_volatility(a) == volatility_is_volatile ||
5067 get_Store_volatility(b) == volatility_is_volatile);
5068 } /* node_cmp_attr_Store */
5070 /** Compares the attributes of two Confirm nodes. */
5071 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5072 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5073 } /* node_cmp_attr_Confirm */
5075 /** Compares the attributes of two ASM nodes. */
5076 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5078 const ir_asm_constraint *ca;
5079 const ir_asm_constraint *cb;
5082 if (get_ASM_text(a) != get_ASM_text(b))
5085 /* Should we really check the constraints here? Should be better, but is strange. */
5086 n = get_ASM_n_input_constraints(a);
5087 if (n != get_ASM_n_input_constraints(b))
5090 ca = get_ASM_input_constraints(a);
5091 cb = get_ASM_input_constraints(b);
5092 for (i = 0; i < n; ++i) {
5093 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5097 n = get_ASM_n_output_constraints(a);
5098 if (n != get_ASM_n_output_constraints(b))
5101 ca = get_ASM_output_constraints(a);
5102 cb = get_ASM_output_constraints(b);
5103 for (i = 0; i < n; ++i) {
5104 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5108 n = get_ASM_n_clobbers(a);
5109 if (n != get_ASM_n_clobbers(b))
5112 cla = get_ASM_clobbers(a);
5113 clb = get_ASM_clobbers(b);
5114 for (i = 0; i < n; ++i) {
5115 if (cla[i] != clb[i])
5119 } /* node_cmp_attr_ASM */
5122 * Set the default node attribute compare operation for an ir_op_ops.
5124 * @param code the opcode for the default operation
5125 * @param ops the operations initialized
5130 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5134 ops->node_cmp_attr = node_cmp_attr_##a; \
5159 } /* firm_set_default_node_cmp_attr */
5162 * Compare function for two nodes in the value table. Gets two
5163 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5165 int identities_cmp(const void *elt, const void *key) {
5166 ir_node *a = (ir_node *)elt;
5167 ir_node *b = (ir_node *)key;
5170 if (a == b) return 0;
5172 if ((get_irn_op(a) != get_irn_op(b)) ||
5173 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5175 /* compare if a's in and b's in are of equal length */
5176 irn_arity_a = get_irn_intra_arity(a);
5177 if (irn_arity_a != get_irn_intra_arity(b))
5180 if (get_irn_pinned(a) == op_pin_state_pinned) {
5181 /* for pinned nodes, the block inputs must be equal */
5182 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5184 } else if (! get_opt_global_cse()) {
5185 /* for block-local CSE both nodes must be in the same MacroBlock */
5186 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5190 /* compare a->in[0..ins] with b->in[0..ins] */
5191 for (i = 0; i < irn_arity_a; i++)
5192 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5196 * here, we already now that the nodes are identical except their
5199 if (a->op->ops.node_cmp_attr)
5200 return a->op->ops.node_cmp_attr(a, b);
5203 } /* identities_cmp */
5206 * Calculate a hash value of a node.
5208 unsigned ir_node_hash(ir_node *node) {
5212 if (node->op == op_Const) {
5213 /* special value for const, as they only differ in their tarval. */
5214 h = HASH_PTR(node->attr.con.tv);
5215 h = 9*h + HASH_PTR(get_irn_mode(node));
5216 } else if (node->op == op_SymConst) {
5217 /* special value for const, as they only differ in their symbol. */
5218 h = HASH_PTR(node->attr.symc.sym.type_p);
5219 h = 9*h + HASH_PTR(get_irn_mode(node));
5222 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5223 h = irn_arity = get_irn_intra_arity(node);
5225 /* consider all in nodes... except the block if not a control flow. */
5226 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5227 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5231 h = 9*h + HASH_PTR(get_irn_mode(node));
5233 h = 9*h + HASH_PTR(get_irn_op(node));
5237 } /* ir_node_hash */
5239 pset *new_identities(void) {
5240 return new_pset(identities_cmp, N_IR_NODES);
5241 } /* new_identities */
5243 void del_identities(pset *value_table) {
5244 del_pset(value_table);
5245 } /* del_identities */
5248 * Normalize a node by putting constants (and operands with larger
5249 * node index) on the right (operator side).
5251 * @param n The node to normalize
5253 static void normalize_node(ir_node *n) {
5254 if (is_op_commutative(get_irn_op(n))) {
5255 ir_node *l = get_binop_left(n);
5256 ir_node *r = get_binop_right(n);
5258 /* For commutative operators perform a OP b == b OP a but keep
5259 * constants on the RIGHT side. This helps greatly in some
5260 * optimizations. Moreover we use the idx number to make the form
5262 if (!operands_are_normalized(l, r)) {
5263 set_binop_left(n, r);
5264 set_binop_right(n, l);
5267 } /* normalize_node */
5270 * Update the nodes after a match in the value table. If both nodes have
5271 * the same MacroBlock but different Blocks, we must ensure that the node
5272 * with the dominating Block (the node that is near to the MacroBlock header
5273 * is stored in the table.
5274 * Because a MacroBlock has only one "non-exception" flow, we don't need
5275 * dominance info here: We known, that one block must dominate the other and
5276 * following the only block input will allow to find it.
5278 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5279 ir_node *known_blk, *new_block, *block, *mbh;
5281 if (get_opt_global_cse()) {
5282 /* Block inputs are meaning less */
5285 known_blk = get_irn_n(known_irn, -1);
5286 new_block = get_irn_n(new_ir_node, -1);
5287 if (known_blk == new_block) {
5288 /* already in the same block */
5292 * We expect the typical case when we built the graph. In that case, the
5293 * known_irn is already the upper one, so checking this should be faster.
5296 mbh = get_Block_MacroBlock(new_block);
5298 if (block == known_blk) {
5299 /* ok, we have found it: known_block dominates new_block as expected */
5304 * We have reached the MacroBlock header NOT founding
5305 * the known_block. new_block must dominate known_block.
5308 set_irn_n(known_irn, -1, new_block);
5311 assert(get_Block_n_cfgpreds(block) == 1);
5312 block = get_Block_cfgpred_block(block, 0);
5314 } /* update_value_table */
5317 * Return the canonical node computing the same value as n.
5319 * @param value_table The value table
5320 * @param n The node to lookup
5322 * Looks up the node in a hash table.
5324 * For Const nodes this is performed in the constructor, too. Const
5325 * nodes are extremely time critical because of their frequent use in
5326 * constant string arrays.
5328 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5331 if (!value_table) return n;
5335 o = pset_find(value_table, n, ir_node_hash(n));
5339 update_known_irn(o, n);
5346 * During construction we set the op_pin_state_pinned flag in the graph right when the
5347 * optimization is performed. The flag turning on procedure global cse could
5348 * be changed between two allocations. This way we are safe.
5350 * @param value_table The value table
5351 * @param n The node to lookup
5353 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5356 n = identify(value_table, n);
5357 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5358 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5360 } /* identify_cons */
5363 * Return the canonical node computing the same value as n.
5364 * Looks up the node in a hash table, enters it in the table
5365 * if it isn't there yet.
5367 * @param value_table the HashSet containing all nodes in the
5369 * @param n the node to look up
5371 * @return a node that computes the same value as n or n if no such
5372 * node could be found
5374 ir_node *identify_remember(pset *value_table, ir_node *n) {
5377 if (!value_table) return n;
5380 /* lookup or insert in hash table with given hash key. */
5381 o = pset_insert(value_table, n, ir_node_hash(n));
5384 update_known_irn(o, n);
5389 } /* identify_remember */
5391 /* Add a node to the identities value table. */
5392 void add_identities(pset *value_table, ir_node *node) {
5393 if (get_opt_cse() && is_no_Block(node))
5394 identify_remember(value_table, node);
5395 } /* add_identities */
5397 /* Visit each node in the value table of a graph. */
5398 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5400 ir_graph *rem = current_ir_graph;
5402 current_ir_graph = irg;
5403 foreach_pset(irg->value_table, node)
5405 current_ir_graph = rem;
5406 } /* visit_all_identities */
5409 * Garbage in, garbage out. If a node has a dead input, i.e., the
5410 * Bad node is input to the node, return the Bad node.
5412 static ir_node *gigo(ir_node *node) {
5414 ir_op *op = get_irn_op(node);
5416 /* remove garbage blocks by looking at control flow that leaves the block
5417 and replacing the control flow by Bad. */
5418 if (get_irn_mode(node) == mode_X) {
5419 ir_node *block = get_nodes_block(skip_Proj(node));
5421 /* Don't optimize nodes in immature blocks. */
5422 if (!get_Block_matured(block))
5424 /* Don't optimize End, may have Bads. */
5425 if (op == op_End) return node;
5427 if (is_Block(block)) {
5428 if (is_Block_dead(block)) {
5429 /* control flow from dead block is dead */
5433 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5434 if (!is_Bad(get_irn_n(block, i)))
5438 ir_graph *irg = get_irn_irg(block);
5439 /* the start block is never dead */
5440 if (block != get_irg_start_block(irg)
5441 && block != get_irg_end_block(irg)) {
5443 * Do NOT kill control flow without setting
5444 * the block to dead of bad things can happen:
5445 * We get a Block that is not reachable be irg_block_walk()
5446 * but can be found by irg_walk()!
5448 set_Block_dead(block);
5455 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5456 blocks predecessors is dead. */
5457 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5458 irn_arity = get_irn_arity(node);
5461 * Beware: we can only read the block of a non-floating node.
5463 if (is_irn_pinned_in_irg(node) &&
5464 is_Block_dead(get_nodes_block(skip_Proj(node))))
5467 for (i = 0; i < irn_arity; i++) {
5468 ir_node *pred = get_irn_n(node, i);
5473 /* Propagating Unknowns here seems to be a bad idea, because
5474 sometimes we need a node as a input and did not want that
5476 However, it might be useful to move this into a later phase
5477 (if you think that optimizing such code is useful). */
5478 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5479 return new_Unknown(get_irn_mode(node));
5484 /* With this code we violate the agreement that local_optimize
5485 only leaves Bads in Block, Phi and Tuple nodes. */
5486 /* If Block has only Bads as predecessors it's garbage. */
5487 /* If Phi has only Bads as predecessors it's garbage. */
5488 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5489 irn_arity = get_irn_arity(node);
5490 for (i = 0; i < irn_arity; i++) {
5491 if (!is_Bad(get_irn_n(node, i))) break;
5493 if (i == irn_arity) node = new_Bad();
5500 * These optimizations deallocate nodes from the obstack.
5501 * It can only be called if it is guaranteed that no other nodes
5502 * reference this one, i.e., right after construction of a node.
5504 * @param n The node to optimize
5506 * current_ir_graph must be set to the graph of the node!
5508 ir_node *optimize_node(ir_node *n) {
5511 ir_opcode iro = get_irn_opcode(n);
5513 /* Always optimize Phi nodes: part of the construction. */
5514 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5516 /* constant expression evaluation / constant folding */
5517 if (get_opt_constant_folding()) {
5518 /* neither constants nor Tuple values can be evaluated */
5519 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5520 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5521 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5522 /* try to evaluate */
5523 tv = computed_value(n);
5524 if (tv != tarval_bad) {
5526 ir_type *old_tp = get_irn_type(n);
5527 int i, arity = get_irn_arity(n);
5531 * Try to recover the type of the new expression.
5533 for (i = 0; i < arity && !old_tp; ++i)
5534 old_tp = get_irn_type(get_irn_n(n, i));
5537 * we MUST copy the node here temporary, because it's still needed
5538 * for DBG_OPT_CSTEVAL
5540 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5541 oldn = alloca(node_size);
5543 memcpy(oldn, n, node_size);
5544 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5546 /* ARG, copy the in array, we need it for statistics */
5547 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5549 /* note the inplace edges module */
5550 edges_node_deleted(n, current_ir_graph);
5552 /* evaluation was successful -- replace the node. */
5553 irg_kill_node(current_ir_graph, n);
5554 nw = new_Const(get_tarval_mode(tv), tv);
5556 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5557 set_Const_type(nw, old_tp);
5558 DBG_OPT_CSTEVAL(oldn, nw);
5559 tarval_enable_fp_ops(old_fp_mode);
5562 tarval_enable_fp_ops(old_fp_mode);
5566 /* remove unnecessary nodes */
5567 if (get_opt_constant_folding() ||
5568 (iro == iro_Phi) || /* always optimize these nodes. */
5570 (iro == iro_Proj) ||
5571 (iro == iro_Block) ) /* Flags tested local. */
5572 n = equivalent_node(n);
5574 /* Common Subexpression Elimination.
5576 * Checks whether n is already available.
5577 * The block input is used to distinguish different subexpressions. Right
5578 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5579 * subexpressions within a block.
5582 n = identify_cons(current_ir_graph->value_table, n);
5585 edges_node_deleted(oldn, current_ir_graph);
5587 /* We found an existing, better node, so we can deallocate the old node. */
5588 irg_kill_node(current_ir_graph, oldn);
5592 /* Some more constant expression evaluation that does not allow to
5594 iro = get_irn_opcode(n);
5595 if (get_opt_constant_folding() ||
5596 (iro == iro_Cond) ||
5597 (iro == iro_Proj)) /* Flags tested local. */
5598 n = transform_node(n);
5600 /* Remove nodes with dead (Bad) input.
5601 Run always for transformation induced Bads. */
5604 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5605 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5606 n = identify_remember(current_ir_graph->value_table, n);
5610 } /* optimize_node */
5614 * These optimizations never deallocate nodes (in place). This can cause dead
5615 * nodes lying on the obstack. Remove these by a dead node elimination,
5616 * i.e., a copying garbage collection.
5618 ir_node *optimize_in_place_2(ir_node *n) {
5621 ir_opcode iro = get_irn_opcode(n);
5623 if (!get_opt_optimize() && !is_Phi(n)) return n;
5625 /* constant expression evaluation / constant folding */
5626 if (get_opt_constant_folding()) {
5627 /* neither constants nor Tuple values can be evaluated */
5628 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5629 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5630 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5631 /* try to evaluate */
5632 tv = computed_value(n);
5633 if (tv != tarval_bad) {
5634 /* evaluation was successful -- replace the node. */
5635 ir_type *old_tp = get_irn_type(n);
5636 int i, arity = get_irn_arity(n);
5639 * Try to recover the type of the new expression.
5641 for (i = 0; i < arity && !old_tp; ++i)
5642 old_tp = get_irn_type(get_irn_n(n, i));
5644 n = new_Const(get_tarval_mode(tv), tv);
5646 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5647 set_Const_type(n, old_tp);
5649 DBG_OPT_CSTEVAL(oldn, n);
5650 tarval_enable_fp_ops(old_fp_mode);
5653 tarval_enable_fp_ops(old_fp_mode);
5657 /* remove unnecessary nodes */
5658 if (get_opt_constant_folding() ||
5659 (iro == iro_Phi) || /* always optimize these nodes. */
5660 (iro == iro_Id) || /* ... */
5661 (iro == iro_Proj) || /* ... */
5662 (iro == iro_Block) ) /* Flags tested local. */
5663 n = equivalent_node(n);
5665 /** common subexpression elimination **/
5666 /* Checks whether n is already available. */
5667 /* The block input is used to distinguish different subexpressions. Right
5668 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5669 subexpressions within a block. */
5670 if (get_opt_cse()) {
5671 n = identify(current_ir_graph->value_table, n);
5674 /* Some more constant expression evaluation. */
5675 iro = get_irn_opcode(n);
5676 if (get_opt_constant_folding() ||
5677 (iro == iro_Cond) ||
5678 (iro == iro_Proj)) /* Flags tested local. */
5679 n = transform_node(n);
5681 /* Remove nodes with dead (Bad) input.
5682 Run always for transformation induced Bads. */
5685 /* Now we can verify the node, as it has no dead inputs any more. */
5688 /* Now we have a legal, useful node. Enter it in hash table for cse.
5689 Blocks should be unique anyways. (Except the successor of start:
5690 is cse with the start block!) */
5691 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5692 n = identify_remember(current_ir_graph->value_table, n);
5695 } /* optimize_in_place_2 */
5698 * Wrapper for external use, set proper status bits after optimization.
5700 ir_node *optimize_in_place(ir_node *n) {
5701 /* Handle graph state */
5702 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5704 if (get_opt_global_cse())
5705 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5706 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5707 set_irg_outs_inconsistent(current_ir_graph);
5709 /* FIXME: Maybe we could also test whether optimizing the node can
5710 change the control graph. */
5711 set_irg_doms_inconsistent(current_ir_graph);
5712 return optimize_in_place_2(n);
5713 } /* optimize_in_place */
5716 * Sets the default operation for an ir_ops.
5718 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5719 ops = firm_set_default_computed_value(code, ops);
5720 ops = firm_set_default_equivalent_node(code, ops);
5721 ops = firm_set_default_transform_node(code, ops);
5722 ops = firm_set_default_node_cmp_attr(code, ops);
5723 ops = firm_set_default_get_type(code, ops);
5724 ops = firm_set_default_get_type_attr(code, ops);
5725 ops = firm_set_default_get_entity_attr(code, ops);
5728 } /* firm_set_default_operations */