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)
727 /* don't optimize dead blocks */
728 if (is_Block_dead(n))
731 n_preds = get_Block_n_cfgpreds(n);
733 /* The Block constructor does not call optimize, but mature_immBlock()
734 calls the optimization. */
735 assert(get_Block_matured(n));
737 /* Straightening: a single entry Block following a single exit Block
738 can be merged, if it is not the Start block. */
739 /* !!! Beware, all Phi-nodes of n must have been optimized away.
740 This should be true, as the block is matured before optimize is called.
741 But what about Phi-cycles with the Phi0/Id that could not be resolved?
742 Remaining Phi nodes are just Ids. */
743 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
744 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
745 if (predblock == oldn) {
746 /* Jmp jumps into the block it is in -- deal self cycle. */
747 n = set_Block_dead(n);
748 DBG_OPT_DEAD_BLOCK(oldn, n);
749 } else if (get_opt_control_flow_straightening()) {
751 DBG_OPT_STG(oldn, n);
753 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
754 ir_node *predblock = get_Block_cfgpred_block(n, 0);
755 if (predblock == oldn) {
756 /* Jmp jumps into the block it is in -- deal self cycle. */
757 n = set_Block_dead(n);
758 DBG_OPT_DEAD_BLOCK(oldn, n);
760 } else if ((n_preds == 2) &&
761 (get_opt_control_flow_weak_simplification())) {
762 /* Test whether Cond jumps twice to this block
763 * The more general case which more than 2 predecessors is handles
764 * in optimize_cf(), we handle only this special case for speed here.
766 ir_node *a = get_Block_cfgpred(n, 0);
767 ir_node *b = get_Block_cfgpred(n, 1);
771 (get_Proj_pred(a) == get_Proj_pred(b)) &&
772 is_Cond(get_Proj_pred(a)) &&
773 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
774 /* Also a single entry Block following a single exit Block. Phis have
775 twice the same operand and will be optimized away. */
776 n = get_nodes_block(get_Proj_pred(a));
777 DBG_OPT_IFSIM1(oldn, a, b, n);
779 } else if (get_opt_unreachable_code() &&
780 (n != get_irg_start_block(current_ir_graph)) &&
781 (n != get_irg_end_block(current_ir_graph)) ) {
784 /* If all inputs are dead, this block is dead too, except if it is
785 the start or end block. This is one step of unreachable code
787 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
788 ir_node *pred = get_Block_cfgpred(n, i);
791 if (is_Bad(pred)) continue;
792 pred_blk = get_nodes_block(skip_Proj(pred));
794 if (is_Block_dead(pred_blk)) continue;
797 /* really found a living input */
802 n = set_Block_dead(n);
803 DBG_OPT_DEAD_BLOCK(oldn, n);
808 } /* equivalent_node_Block */
811 * Returns a equivalent node for a Jmp, a Bad :-)
812 * Of course this only happens if the Block of the Jmp is dead.
814 static ir_node *equivalent_node_Jmp(ir_node *n) {
815 /* unreachable code elimination */
816 if (is_Block_dead(get_nodes_block(n)))
820 } /* equivalent_node_Jmp */
822 /** Raise is handled in the same way as Jmp. */
823 #define equivalent_node_Raise equivalent_node_Jmp
826 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
827 See transform_node_Proj_Cond(). */
830 * Optimize operations that are commutative and have neutral 0,
831 * so a op 0 = 0 op a = a.
833 static ir_node *equivalent_node_neutral_zero(ir_node *n)
837 ir_node *a = get_binop_left(n);
838 ir_node *b = get_binop_right(n);
843 /* After running compute_node there is only one constant predecessor.
844 Find this predecessors value and remember the other node: */
845 if ((tv = value_of(a)) != tarval_bad) {
847 } else if ((tv = value_of(b)) != tarval_bad) {
852 /* If this predecessors constant value is zero, the operation is
853 * unnecessary. Remove it.
855 * Beware: If n is a Add, the mode of on and n might be different
856 * which happens in this rare construction: NULL + 3.
857 * Then, a Conv would be needed which we cannot include here.
859 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
866 } /* equivalent_node_neutral_zero */
869 * Eor is commutative and has neutral 0.
871 #define equivalent_node_Eor equivalent_node_neutral_zero
874 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
876 * The second one looks strange, but this construct
877 * is used heavily in the LCC sources :-).
879 * Beware: The Mode of an Add may be different than the mode of its
880 * predecessors, so we could not return a predecessors in all cases.
882 static ir_node *equivalent_node_Add(ir_node *n) {
884 ir_node *left, *right;
885 ir_mode *mode = get_irn_mode(n);
887 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
888 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
891 n = equivalent_node_neutral_zero(n);
895 left = get_Add_left(n);
896 right = get_Add_right(n);
899 if (get_Sub_right(left) == right) {
902 n = get_Sub_left(left);
903 if (mode == get_irn_mode(n)) {
904 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
910 if (get_Sub_right(right) == left) {
913 n = get_Sub_left(right);
914 if (mode == get_irn_mode(n)) {
915 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
921 } /* equivalent_node_Add */
924 * optimize operations that are not commutative but have neutral 0 on left,
927 static ir_node *equivalent_node_left_zero(ir_node *n) {
930 ir_node *a = get_binop_left(n);
931 ir_node *b = get_binop_right(n);
933 if (is_Const(b) && is_Const_null(b)) {
936 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
939 } /* equivalent_node_left_zero */
941 #define equivalent_node_Shl equivalent_node_left_zero
942 #define equivalent_node_Shr equivalent_node_left_zero
943 #define equivalent_node_Shrs equivalent_node_left_zero
944 #define equivalent_node_Rot equivalent_node_left_zero
947 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
949 * The second one looks strange, but this construct
950 * is used heavily in the LCC sources :-).
952 * Beware: The Mode of a Sub may be different than the mode of its
953 * predecessors, so we could not return a predecessors in all cases.
955 static ir_node *equivalent_node_Sub(ir_node *n) {
958 ir_mode *mode = get_irn_mode(n);
960 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
961 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
964 b = get_Sub_right(n);
966 /* Beware: modes might be different */
967 if (is_Const(b) && is_Const_null(b)) {
968 ir_node *a = get_Sub_left(n);
969 if (mode == get_irn_mode(a)) {
972 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
976 } /* equivalent_node_Sub */
980 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
983 * -(-a) == a, but might overflow two times.
984 * We handle it anyway here but the better way would be a
985 * flag. This would be needed for Pascal for instance.
987 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
989 ir_node *pred = get_unop_op(n);
991 /* optimize symmetric unop */
992 if (get_irn_op(pred) == get_irn_op(n)) {
993 n = get_unop_op(pred);
994 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
997 } /* equivalent_node_idempotent_unop */
999 /** Optimize Not(Not(x)) == x. */
1000 #define equivalent_node_Not equivalent_node_idempotent_unop
1002 /** -(-x) == x ??? Is this possible or can --x raise an
1003 out of bounds exception if min =! max? */
1004 #define equivalent_node_Minus equivalent_node_idempotent_unop
1007 * Optimize a * 1 = 1 * a = a.
1009 static ir_node *equivalent_node_Mul(ir_node *n) {
1011 ir_node *a = get_Mul_left(n);
1013 /* we can handle here only the n * n = n bit cases */
1014 if (get_irn_mode(n) == get_irn_mode(a)) {
1015 ir_node *b = get_Mul_right(n);
1017 /* Mul is commutative and has again an other neutral element. */
1018 if (is_Const(a) && is_Const_one(a)) {
1020 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1021 } else if (is_Const(b) && is_Const_one(b)) {
1023 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1027 } /* equivalent_node_Mul */
1030 * Optimize a / 1 = a.
1032 static ir_node *equivalent_node_Div(ir_node *n) {
1033 ir_node *a = get_Div_left(n);
1034 ir_node *b = get_Div_right(n);
1036 /* Div is not commutative. */
1037 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1038 /* Turn Div into a tuple (mem, bad, a) */
1039 ir_node *mem = get_Div_mem(n);
1040 ir_node *blk = get_irn_n(n, -1);
1041 turn_into_tuple(n, pn_Div_max);
1042 set_Tuple_pred(n, pn_Div_M, mem);
1043 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1044 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1045 set_Tuple_pred(n, pn_Div_res, a);
1048 } /* equivalent_node_Div */
1051 * Optimize a / 1.0 = a.
1053 static ir_node *equivalent_node_Quot(ir_node *n) {
1054 ir_node *a = get_Quot_left(n);
1055 ir_node *b = get_Quot_right(n);
1057 /* Div is not commutative. */
1058 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1059 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1060 ir_node *mem = get_Quot_mem(n);
1061 ir_node *blk = get_irn_n(n, -1);
1062 turn_into_tuple(n, pn_Quot_max);
1063 set_Tuple_pred(n, pn_Quot_M, mem);
1064 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1065 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1066 set_Tuple_pred(n, pn_Quot_res, a);
1069 } /* equivalent_node_Quot */
1072 * Optimize a / 1 = a.
1074 static ir_node *equivalent_node_DivMod(ir_node *n) {
1075 ir_node *b = get_DivMod_right(n);
1077 /* Div is not commutative. */
1078 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1079 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1080 ir_node *a = get_DivMod_left(n);
1081 ir_node *mem = get_Div_mem(n);
1082 ir_node *blk = get_irn_n(n, -1);
1083 ir_mode *mode = get_DivMod_resmode(n);
1085 turn_into_tuple(n, pn_DivMod_max);
1086 set_Tuple_pred(n, pn_DivMod_M, mem);
1087 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1088 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1089 set_Tuple_pred(n, pn_DivMod_res_div, a);
1090 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1093 } /* equivalent_node_DivMod */
1096 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1098 static ir_node *equivalent_node_Or(ir_node *n) {
1101 ir_node *a = get_Or_left(n);
1102 ir_node *b = get_Or_right(n);
1105 n = a; /* Or has it's own neutral element */
1106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1107 } else if (is_Const(a) && is_Const_null(a)) {
1109 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } else if (is_Const(b) && is_Const_null(b)) {
1112 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1116 } /* equivalent_node_Or */
1119 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1121 static ir_node *equivalent_node_And(ir_node *n) {
1124 ir_node *a = get_And_left(n);
1125 ir_node *b = get_And_right(n);
1128 n = a; /* And has it's own neutral element */
1129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1132 if (is_Const(a) && is_Const_all_one(a)) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 if (is_Const(b) && is_Const_all_one(b)) {
1139 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1151 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1154 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1160 } /* equivalent_node_And */
1163 * Try to remove useless Conv's:
1165 static ir_node *equivalent_node_Conv(ir_node *n) {
1167 ir_node *a = get_Conv_op(n);
1169 ir_mode *n_mode = get_irn_mode(n);
1170 ir_mode *a_mode = get_irn_mode(a);
1172 if (n_mode == a_mode) { /* No Conv necessary */
1173 if (get_Conv_strict(n)) {
1174 /* special case: the predecessor might be a also a Conv */
1176 if (! get_Conv_strict(a)) {
1177 /* first one is not strict, kick it */
1178 set_Conv_op(n, get_Conv_op(a));
1181 /* else both are strict conv, second is superflous */
1182 } else if(is_Proj(a)) {
1183 ir_node *pred = get_Proj_pred(a);
1185 /* loads always return with the exact precision of n_mode */
1186 assert(get_Load_mode(pred) == n_mode);
1191 /* leave strict floating point Conv's */
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1196 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1197 ir_node *b = get_Conv_op(a);
1198 ir_mode *b_mode = get_irn_mode(b);
1200 if (n_mode == b_mode) {
1201 if (n_mode == mode_b) {
1202 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1203 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1204 } else if (mode_is_int(n_mode)) {
1205 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1206 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1213 } /* equivalent_node_Conv */
1216 * A Cast may be removed if the type of the previous node
1217 * is already the type of the Cast.
1219 static ir_node *equivalent_node_Cast(ir_node *n) {
1221 ir_node *pred = get_Cast_op(n);
1223 if (get_irn_type(pred) == get_Cast_type(n)) {
1225 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1228 } /* equivalent_node_Cast */
1231 * Several optimizations:
1232 * - no Phi in start block.
1233 * - remove Id operators that are inputs to Phi
1234 * - fold Phi-nodes, iff they have only one predecessor except
1237 static ir_node *equivalent_node_Phi(ir_node *n) {
1242 ir_node *first_val = NULL; /* to shutup gcc */
1244 if (!get_opt_normalize()) return n;
1246 n_preds = get_Phi_n_preds(n);
1248 block = get_nodes_block(n);
1249 if ((is_Block_dead(block)) || /* Control dead */
1250 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1251 return new_Bad(); /* in the Start Block. */
1253 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1255 /* If the Block has a Bad pred, we also have one. */
1256 for (i = 0; i < n_preds; ++i)
1257 if (is_Bad(get_Block_cfgpred(block, i)))
1258 set_Phi_pred(n, i, new_Bad());
1260 /* Find first non-self-referencing input */
1261 for (i = 0; i < n_preds; ++i) {
1262 first_val = get_Phi_pred(n, i);
1263 if ( (first_val != n) /* not self pointer */
1265 && (! is_Bad(first_val))
1267 ) { /* value not dead */
1268 break; /* then found first value. */
1273 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1277 /* search for rest of inputs, determine if any of these
1278 are non-self-referencing */
1279 while (++i < n_preds) {
1280 ir_node *scnd_val = get_Phi_pred(n, i);
1281 if ( (scnd_val != n)
1282 && (scnd_val != first_val)
1284 && (! is_Bad(scnd_val))
1292 /* Fold, if no multiple distinct non-self-referencing inputs */
1294 DBG_OPT_PHI(oldn, n);
1297 } /* equivalent_node_Phi */
1300 * Several optimizations:
1301 * - no Sync in start block.
1302 * - fold Sync-nodes, iff they have only one predecessor except
1305 static ir_node *equivalent_node_Sync(ir_node *n) {
1306 int arity = get_Sync_n_preds(n);
1309 for (i = 0; i < arity;) {
1310 ir_node *pred = get_Sync_pred(n, i);
1313 /* Remove Bad predecessors */
1320 /* Remove duplicate predecessors */
1326 if (get_Sync_pred(n, j) == pred) {
1334 if (arity == 0) return new_Bad();
1335 if (arity == 1) return get_Sync_pred(n, 0);
1337 } /* equivalent_node_Sync */
1340 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1341 * ProjX(Load) and ProjX(Store).
1343 static ir_node *equivalent_node_Proj(ir_node *proj) {
1344 ir_node *oldn = proj;
1345 ir_node *a = get_Proj_pred(proj);
1348 /* Remove the Tuple/Proj combination. */
1349 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1350 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1351 DBG_OPT_TUPLE(oldn, a, proj);
1353 /* This should not happen! */
1354 assert(! "found a Proj with higher number than Tuple predecessors");
1357 } else if (get_irn_mode(proj) == mode_X) {
1358 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1359 /* Remove dead control flow -- early gigo(). */
1361 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1363 /* get the Load address */
1364 ir_node *addr = get_Load_ptr(a);
1365 ir_node *blk = get_irn_n(a, -1);
1368 if (value_not_null(addr, &confirm)) {
1369 if (confirm == NULL) {
1370 /* this node may float if it did not depend on a Confirm */
1371 set_irn_pinned(a, op_pin_state_floats);
1373 if (get_Proj_proj(proj) == pn_Load_X_except) {
1374 DBG_OPT_EXC_REM(proj);
1377 return new_r_Jmp(current_ir_graph, blk);
1379 } else if (is_Store(a)) {
1380 /* get the load/store address */
1381 ir_node *addr = get_Store_ptr(a);
1382 ir_node *blk = get_irn_n(a, -1);
1385 if (value_not_null(addr, &confirm)) {
1386 if (confirm == NULL) {
1387 /* this node may float if it did not depend on a Confirm */
1388 set_irn_pinned(a, op_pin_state_floats);
1390 if (get_Proj_proj(proj) == pn_Store_X_except) {
1391 DBG_OPT_EXC_REM(proj);
1394 return new_r_Jmp(current_ir_graph, blk);
1401 } /* equivalent_node_Proj */
1406 static ir_node *equivalent_node_Id(ir_node *n) {
1411 } while (get_irn_op(n) == op_Id);
1413 DBG_OPT_ID(oldn, n);
1415 } /* equivalent_node_Id */
1420 static ir_node *equivalent_node_Mux(ir_node *n)
1422 ir_node *oldn = n, *sel = get_Mux_sel(n);
1423 tarval *ts = value_of(sel);
1425 /* Mux(true, f, t) == t */
1426 if (ts == tarval_b_true) {
1427 n = get_Mux_true(n);
1428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1430 /* Mux(false, f, t) == f */
1431 else if (ts == tarval_b_false) {
1432 n = get_Mux_false(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1435 /* Mux(v, x, x) == x */
1436 else if (get_Mux_false(n) == get_Mux_true(n)) {
1437 n = get_Mux_true(n);
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1440 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1441 ir_node *cmp = get_Proj_pred(sel);
1442 long proj_nr = get_Proj_proj(sel);
1443 ir_node *f = get_Mux_false(n);
1444 ir_node *t = get_Mux_true(n);
1447 * Note further that these optimization work even for floating point
1448 * with NaN's because -NaN == NaN.
1449 * However, if +0 and -0 is handled differently, we cannot use the first one.
1452 ir_node *const cmp_l = get_Cmp_left(cmp);
1453 ir_node *const cmp_r = get_Cmp_right(cmp);
1457 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1458 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1460 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1467 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1468 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1477 * Note: normalization puts the constant on the right side,
1478 * so we check only one case.
1480 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1481 /* Mux(t CMP 0, X, t) */
1482 if (is_Minus(f) && get_Minus_op(f) == t) {
1483 /* Mux(t CMP 0, -t, t) */
1484 if (proj_nr == pn_Cmp_Eq) {
1485 /* Mux(t == 0, -t, t) ==> -t */
1487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1488 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1489 /* Mux(t != 0, -t, t) ==> t */
1491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1498 } /* equivalent_node_Mux */
1501 * Returns a equivalent node of a Psi: if a condition is true
1502 * and all previous conditions are false we know its value.
1503 * If all conditions are false its value is the default one.
1505 static ir_node *equivalent_node_Psi(ir_node *n) {
1507 return equivalent_node_Mux(n);
1509 } /* equivalent_node_Psi */
1512 * Optimize -a CMP -b into b CMP a.
1513 * This works only for for modes where unary Minus
1515 * Note that two-complement integers can Overflow
1516 * so it will NOT work.
1518 * For == and != can be handled in Proj(Cmp)
1520 static ir_node *equivalent_node_Cmp(ir_node *n) {
1521 ir_node *left = get_Cmp_left(n);
1522 ir_node *right = get_Cmp_right(n);
1524 if (is_Minus(left) && is_Minus(right) &&
1525 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1526 left = get_Minus_op(left);
1527 right = get_Minus_op(right);
1528 set_Cmp_left(n, right);
1529 set_Cmp_right(n, left);
1532 } /* equivalent_node_Cmp */
1535 * Remove Confirm nodes if setting is on.
1536 * Replace Confirms(x, '=', Constlike) by Constlike.
1538 static ir_node *equivalent_node_Confirm(ir_node *n) {
1539 ir_node *pred = get_Confirm_value(n);
1540 pn_Cmp pnc = get_Confirm_cmp(n);
1542 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1544 * rare case: two identical Confirms one after another,
1545 * replace the second one with the first.
1549 if (pnc == pn_Cmp_Eq) {
1550 ir_node *bound = get_Confirm_bound(n);
1553 * Optimize a rare case:
1554 * Confirm(x, '=', Constlike) ==> Constlike
1556 if (is_irn_constlike(bound)) {
1557 DBG_OPT_CONFIRM(n, bound);
1561 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1565 * Optimize CopyB(mem, x, x) into a Nop.
1567 static ir_node *equivalent_node_CopyB(ir_node *n) {
1568 ir_node *a = get_CopyB_dst(n);
1569 ir_node *b = get_CopyB_src(n);
1572 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1573 ir_node *mem = get_CopyB_mem(n);
1574 ir_node *blk = get_nodes_block(n);
1575 turn_into_tuple(n, pn_CopyB_max);
1576 set_Tuple_pred(n, pn_CopyB_M, mem);
1577 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1578 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1579 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1582 } /* equivalent_node_CopyB */
1585 * Optimize Bounds(idx, idx, upper) into idx.
1587 static ir_node *equivalent_node_Bound(ir_node *n) {
1588 ir_node *idx = get_Bound_index(n);
1589 ir_node *pred = skip_Proj(idx);
1592 if (is_Bound(pred)) {
1594 * idx was Bounds checked in the same MacroBlock previously,
1595 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1597 ir_node *lower = get_Bound_lower(n);
1598 ir_node *upper = get_Bound_upper(n);
1599 if (get_Bound_lower(pred) == lower &&
1600 get_Bound_upper(pred) == upper &&
1601 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1603 * One could expect that we simply return the previous
1604 * Bound here. However, this would be wrong, as we could
1605 * add an exception Proj to a new location then.
1606 * So, we must turn in into a tuple.
1612 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1613 ir_node *mem = get_Bound_mem(n);
1614 ir_node *blk = get_nodes_block(n);
1615 turn_into_tuple(n, pn_Bound_max);
1616 set_Tuple_pred(n, pn_Bound_M, mem);
1617 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1618 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1619 set_Tuple_pred(n, pn_Bound_res, idx);
1622 } /* equivalent_node_Bound */
1625 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1626 * perform no actual computation, as, e.g., the Id nodes. It does not create
1627 * new nodes. It is therefore safe to free n if the node returned is not n.
1628 * If a node returns a Tuple we can not just skip it. If the size of the
1629 * in array fits, we transform n into a tuple (e.g., Div).
1631 ir_node *equivalent_node(ir_node *n) {
1632 if (n->op->ops.equivalent_node)
1633 return n->op->ops.equivalent_node(n);
1635 } /* equivalent_node */
1638 * Sets the default equivalent node operation for an ir_op_ops.
1640 * @param code the opcode for the default operation
1641 * @param ops the operations initialized
1646 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1650 ops->equivalent_node = equivalent_node_##a; \
1690 } /* firm_set_default_equivalent_node */
1693 * Returns non-zero if a node is a Phi node
1694 * with all predecessors constant.
1696 static int is_const_Phi(ir_node *n) {
1699 if (! is_Phi(n) || get_irn_arity(n) == 0)
1701 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1702 if (! is_Const(get_irn_n(n, i)))
1705 } /* is_const_Phi */
1708 * Apply an evaluator on a binop with a constant operators (and one Phi).
1710 * @param phi the Phi node
1711 * @param other the other operand
1712 * @param eval an evaluator function
1713 * @param mode the mode of the result, may be different from the mode of the Phi!
1714 * @param left if non-zero, other is the left operand, else the right
1716 * @return a new Phi node if the conversion was successful, NULL else
1718 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1723 int i, n = get_irn_arity(phi);
1725 NEW_ARR_A(void *, res, n);
1727 for (i = 0; i < n; ++i) {
1728 pred = get_irn_n(phi, i);
1729 tv = get_Const_tarval(pred);
1730 tv = eval(other, tv);
1732 if (tv == tarval_bad) {
1733 /* folding failed, bad */
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 tv = get_Const_tarval(pred);
1742 tv = eval(tv, other);
1744 if (tv == tarval_bad) {
1745 /* folding failed, bad */
1751 irg = current_ir_graph;
1752 for (i = 0; i < n; ++i) {
1753 pred = get_irn_n(phi, i);
1754 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1755 mode, res[i], get_Const_type(pred));
1757 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1758 } /* apply_binop_on_phi */
1761 * Apply an evaluator on a binop with two constant Phi.
1763 * @param a the left Phi node
1764 * @param b the right Phi node
1765 * @param eval an evaluator function
1766 * @param mode the mode of the result, may be different from the mode of the Phi!
1768 * @return a new Phi node if the conversion was successful, NULL else
1770 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1771 tarval *tv_l, *tv_r, *tv;
1777 if (get_nodes_block(a) != get_nodes_block(b))
1780 n = get_irn_arity(a);
1781 NEW_ARR_A(void *, res, n);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(a, i);
1785 tv_l = get_Const_tarval(pred);
1786 pred = get_irn_n(b, i);
1787 tv_r = get_Const_tarval(pred);
1788 tv = eval(tv_l, tv_r);
1790 if (tv == tarval_bad) {
1791 /* folding failed, bad */
1796 irg = current_ir_graph;
1797 for (i = 0; i < n; ++i) {
1798 pred = get_irn_n(a, i);
1799 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1801 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1802 } /* apply_binop_on_2_phis */
1805 * Apply an evaluator on a unop with a constant operator (a Phi).
1807 * @param phi the Phi node
1808 * @param eval an evaluator function
1810 * @return a new Phi node if the conversion was successful, NULL else
1812 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1818 int i, n = get_irn_arity(phi);
1820 NEW_ARR_A(void *, res, n);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 tv = get_Const_tarval(pred);
1826 if (tv == tarval_bad) {
1827 /* folding failed, bad */
1832 mode = get_irn_mode(phi);
1833 irg = current_ir_graph;
1834 for (i = 0; i < n; ++i) {
1835 pred = get_irn_n(phi, i);
1836 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1837 mode, res[i], get_Const_type(pred));
1839 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1840 } /* apply_unop_on_phi */
1843 * Apply a conversion on a constant operator (a Phi).
1845 * @param phi the Phi node
1847 * @return a new Phi node if the conversion was successful, NULL else
1849 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1854 int i, n = get_irn_arity(phi);
1856 NEW_ARR_A(void *, res, n);
1857 for (i = 0; i < n; ++i) {
1858 pred = get_irn_n(phi, i);
1859 tv = get_Const_tarval(pred);
1860 tv = tarval_convert_to(tv, mode);
1862 if (tv == tarval_bad) {
1863 /* folding failed, bad */
1868 irg = current_ir_graph;
1869 for (i = 0; i < n; ++i) {
1870 pred = get_irn_n(phi, i);
1871 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1872 mode, res[i], get_Const_type(pred));
1874 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1875 } /* apply_conv_on_phi */
1878 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1879 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1880 * If possible, remove the Conv's.
1882 static ir_node *transform_node_AddSub(ir_node *n) {
1883 ir_mode *mode = get_irn_mode(n);
1885 if (mode_is_reference(mode)) {
1886 ir_node *left = get_binop_left(n);
1887 ir_node *right = get_binop_right(n);
1888 unsigned ref_bits = get_mode_size_bits(mode);
1890 if (is_Conv(left)) {
1891 ir_mode *lmode = get_irn_mode(left);
1892 unsigned bits = get_mode_size_bits(lmode);
1894 if (ref_bits == bits &&
1895 mode_is_int(lmode) &&
1896 get_mode_arithmetic(lmode) == irma_twos_complement) {
1897 ir_node *pre = get_Conv_op(left);
1898 ir_mode *pre_mode = get_irn_mode(pre);
1900 if (mode_is_int(pre_mode) &&
1901 get_mode_size_bits(pre_mode) == bits &&
1902 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1903 /* ok, this conv just changes to sign, moreover the calculation
1904 * is done with same number of bits as our address mode, so
1905 * we can ignore the conv as address calculation can be viewed
1906 * as either signed or unsigned
1908 set_binop_left(n, pre);
1913 if (is_Conv(right)) {
1914 ir_mode *rmode = get_irn_mode(right);
1915 unsigned bits = get_mode_size_bits(rmode);
1917 if (ref_bits == bits &&
1918 mode_is_int(rmode) &&
1919 get_mode_arithmetic(rmode) == irma_twos_complement) {
1920 ir_node *pre = get_Conv_op(right);
1921 ir_mode *pre_mode = get_irn_mode(pre);
1923 if (mode_is_int(pre_mode) &&
1924 get_mode_size_bits(pre_mode) == bits &&
1925 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1926 /* ok, this conv just changes to sign, moreover the calculation
1927 * is done with same number of bits as our address mode, so
1928 * we can ignore the conv as address calculation can be viewed
1929 * as either signed or unsigned
1931 set_binop_right(n, pre);
1936 /* let address arithmetic use unsigned modes */
1937 if (is_Const(right)) {
1938 ir_mode *rmode = get_irn_mode(right);
1940 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1941 /* convert a AddP(P, *s) into AddP(P, *u) */
1942 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1944 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
1945 set_binop_right(n, pre);
1950 } /* transform_node_AddSub */
1952 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1954 if (is_Const(b) && is_const_Phi(a)) { \
1955 /* check for Op(Phi, Const) */ \
1956 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1958 else if (is_Const(a) && is_const_Phi(b)) { \
1959 /* check for Op(Const, Phi) */ \
1960 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1962 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1963 /* check for Op(Phi, Phi) */ \
1964 c = apply_binop_on_2_phis(a, b, eval, mode); \
1967 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1971 #define HANDLE_UNOP_PHI(eval, a, c) \
1973 if (is_const_Phi(a)) { \
1974 /* check for Op(Phi) */ \
1975 c = apply_unop_on_phi(a, eval); \
1977 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1983 * Do the AddSub optimization, then Transform
1984 * Constant folding on Phi
1985 * Add(a,a) -> Mul(a, 2)
1986 * Add(Mul(a, x), a) -> Mul(a, x+1)
1987 * if the mode is integer or float.
1988 * Transform Add(a,-b) into Sub(a,b).
1989 * Reassociation might fold this further.
1991 static ir_node *transform_node_Add(ir_node *n) {
1993 ir_node *a, *b, *c, *oldn = n;
1995 n = transform_node_AddSub(n);
1997 a = get_Add_left(n);
1998 b = get_Add_right(n);
2000 mode = get_irn_mode(n);
2001 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2003 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2004 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2007 if (mode_is_num(mode)) {
2008 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2009 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2010 ir_node *block = get_irn_n(n, -1);
2013 get_irn_dbg_info(n),
2017 new_r_Const_long(current_ir_graph, block, mode, 2),
2019 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2024 get_irn_dbg_info(n),
2030 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2035 get_irn_dbg_info(n),
2041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2044 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2045 /* Here we rely on constants be on the RIGHT side */
2047 ir_node *op = get_Not_op(a);
2049 if (is_Const(b) && is_Const_one(b)) {
2051 ir_node *blk = get_irn_n(n, -1);
2052 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2053 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2058 ir_node *blk = get_irn_n(n, -1);
2059 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2060 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2065 ir_node *op = get_Not_op(b);
2069 ir_node *blk = get_irn_n(n, -1);
2070 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2071 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2078 } /* transform_node_Add */
2081 * returns -cnst or NULL if impossible
2083 static ir_node *const_negate(ir_node *cnst) {
2084 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2085 dbg_info *dbgi = get_irn_dbg_info(cnst);
2086 ir_graph *irg = get_irn_irg(cnst);
2087 ir_node *block = get_nodes_block(cnst);
2088 ir_mode *mode = get_irn_mode(cnst);
2089 if (tv == tarval_bad) return NULL;
2090 return new_rd_Const(dbgi, irg, block, mode, tv);
2094 * Do the AddSub optimization, then Transform
2095 * Constant folding on Phi
2096 * Sub(0,a) -> Minus(a)
2097 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2098 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2099 * Sub(Add(a, x), x) -> a
2100 * Sub(x, Add(x, a)) -> -a
2101 * Sub(x, Const) -> Add(x, -Const)
2103 static ir_node *transform_node_Sub(ir_node *n) {
2108 n = transform_node_AddSub(n);
2110 a = get_Sub_left(n);
2111 b = get_Sub_right(n);
2113 mode = get_irn_mode(n);
2116 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2118 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2119 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2122 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2123 /* a - C -> a + (-C) */
2124 ir_node *cnst = const_negate(b);
2126 ir_node *block = get_nodes_block(n);
2127 dbg_info *dbgi = get_irn_dbg_info(n);
2128 ir_graph *irg = get_irn_irg(n);
2130 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2131 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2136 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2137 ir_graph *irg = current_ir_graph;
2138 dbg_info *dbg = get_irn_dbg_info(n);
2139 ir_node *block = get_nodes_block(n);
2140 ir_node *left = get_Minus_op(a);
2141 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2143 n = new_rd_Minus(dbg, irg, block, add, mode);
2144 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2146 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2147 ir_graph *irg = current_ir_graph;
2148 dbg_info *dbg = get_irn_dbg_info(n);
2149 ir_node *block = get_nodes_block(n);
2150 ir_node *right = get_Minus_op(b);
2152 n = new_rd_Add(dbg, irg, block, a, right, mode);
2153 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2155 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2156 ir_graph *irg = current_ir_graph;
2157 dbg_info *s_dbg = get_irn_dbg_info(b);
2158 ir_node *s_block = get_nodes_block(b);
2159 ir_node *s_left = get_Sub_right(b);
2160 ir_node *s_right = get_Sub_left(b);
2161 ir_mode *s_mode = get_irn_mode(b);
2162 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2163 dbg_info *a_dbg = get_irn_dbg_info(n);
2164 ir_node *a_block = get_nodes_block(n);
2166 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2167 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2169 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2170 ir_node *m_right = get_Mul_right(b);
2171 if (is_Const(m_right)) {
2172 ir_node *cnst2 = const_negate(m_right);
2173 if (cnst2 != NULL) {
2174 ir_graph *irg = current_ir_graph;
2175 dbg_info *m_dbg = get_irn_dbg_info(b);
2176 ir_node *m_block = get_nodes_block(b);
2177 ir_node *m_left = get_Mul_left(b);
2178 ir_mode *m_mode = get_irn_mode(b);
2179 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2180 dbg_info *a_dbg = get_irn_dbg_info(n);
2181 ir_node *a_block = get_nodes_block(n);
2183 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2190 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2191 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2193 get_irn_dbg_info(n),
2198 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2202 if (mode_wrap_around(mode)) {
2203 ir_node *left = get_Add_left(a);
2204 ir_node *right = get_Add_right(a);
2206 /* FIXME: Does the Conv's work only for two complement or generally? */
2208 if (mode != get_irn_mode(right)) {
2209 /* This Sub is an effective Cast */
2210 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2215 } else if (right == b) {
2216 if (mode != get_irn_mode(left)) {
2217 /* This Sub is an effective Cast */
2218 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2221 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2227 if (mode_wrap_around(mode)) {
2228 ir_node *left = get_Add_left(b);
2229 ir_node *right = get_Add_right(b);
2231 /* FIXME: Does the Conv's work only for two complement or generally? */
2233 ir_mode *r_mode = get_irn_mode(right);
2235 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2236 if (mode != r_mode) {
2237 /* This Sub is an effective Cast */
2238 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2240 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2242 } else if (right == a) {
2243 ir_mode *l_mode = get_irn_mode(left);
2245 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2246 if (mode != l_mode) {
2247 /* This Sub is an effective Cast */
2248 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2250 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2255 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2256 ir_mode *mode = get_irn_mode(a);
2258 if (mode == get_irn_mode(b)) {
2260 ir_node *op_a = get_Conv_op(a);
2261 ir_node *op_b = get_Conv_op(b);
2263 /* check if it's allowed to skip the conv */
2264 ma = get_irn_mode(op_a);
2265 mb = get_irn_mode(op_b);
2267 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2268 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2271 set_Sub_right(n, b);
2277 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2278 if (!is_reassoc_running() && is_Mul(a)) {
2279 ir_node *ma = get_Mul_left(a);
2280 ir_node *mb = get_Mul_right(a);
2283 ir_node *blk = get_irn_n(n, -1);
2285 get_irn_dbg_info(n),
2286 current_ir_graph, blk,
2289 get_irn_dbg_info(n),
2290 current_ir_graph, blk,
2292 new_r_Const_long(current_ir_graph, blk, mode, 1),
2295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2297 } else if (mb == b) {
2298 ir_node *blk = get_irn_n(n, -1);
2300 get_irn_dbg_info(n),
2301 current_ir_graph, blk,
2304 get_irn_dbg_info(n),
2305 current_ir_graph, blk,
2307 new_r_Const_long(current_ir_graph, blk, mode, 1),
2310 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2314 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2315 ir_node *x = get_Sub_left(a);
2316 ir_node *y = get_Sub_right(a);
2317 ir_node *blk = get_irn_n(n, -1);
2318 ir_mode *m_b = get_irn_mode(b);
2319 ir_mode *m_y = get_irn_mode(y);
2323 /* Determine the right mode for the Add. */
2326 else if (mode_is_reference(m_b))
2328 else if (mode_is_reference(m_y))
2332 * Both modes are different but none is reference,
2333 * happens for instance in SubP(SubP(P, Iu), Is).
2334 * We have two possibilities here: Cast or ignore.
2335 * Currently we ignore this case.
2340 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2342 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2343 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2347 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2348 if (is_Const(a) && is_Not(b)) {
2349 /* c - ~X = X + (c+1) */
2350 tarval *tv = get_Const_tarval(a);
2352 tv = tarval_add(tv, get_mode_one(mode));
2353 if (tv != tarval_bad) {
2354 ir_node *blk = get_irn_n(n, -1);
2355 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2356 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2363 } /* transform_node_Sub */
2366 * Several transformation done on n*n=2n bits mul.
2367 * These transformations must be done here because new nodes may be produced.
2369 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2371 ir_node *a = get_Mul_left(n);
2372 ir_node *b = get_Mul_right(n);
2373 tarval *ta = value_of(a);
2374 tarval *tb = value_of(b);
2375 ir_mode *smode = get_irn_mode(a);
2377 if (ta == get_mode_one(smode)) {
2378 /* (L)1 * (L)b = (L)b */
2379 ir_node *blk = get_irn_n(n, -1);
2380 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2381 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2384 else if (ta == get_mode_minus_one(smode)) {
2385 /* (L)-1 * (L)b = (L)b */
2386 ir_node *blk = get_irn_n(n, -1);
2387 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2388 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2389 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2392 if (tb == get_mode_one(smode)) {
2393 /* (L)a * (L)1 = (L)a */
2394 ir_node *blk = get_irn_n(a, -1);
2395 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2396 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2399 else if (tb == get_mode_minus_one(smode)) {
2400 /* (L)a * (L)-1 = (L)-a */
2401 ir_node *blk = get_irn_n(n, -1);
2402 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2403 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2404 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2411 * Transform Mul(a,-1) into -a.
2412 * Do constant evaluation of Phi nodes.
2413 * Do architecture dependent optimizations on Mul nodes
2415 static ir_node *transform_node_Mul(ir_node *n) {
2416 ir_node *c, *oldn = n;
2417 ir_mode *mode = get_irn_mode(n);
2418 ir_node *a = get_Mul_left(n);
2419 ir_node *b = get_Mul_right(n);
2421 if (is_Bad(a) || is_Bad(b))
2424 if (mode != get_irn_mode(a))
2425 return transform_node_Mul2n(n, mode);
2427 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2429 if (mode_is_signed(mode)) {
2432 if (value_of(a) == get_mode_minus_one(mode))
2434 else if (value_of(b) == get_mode_minus_one(mode))
2437 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2438 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2443 if (is_Const(b)) { /* (-a) * const -> a * -const */
2444 ir_node *cnst = const_negate(b);
2446 dbg_info *dbgi = get_irn_dbg_info(n);
2447 ir_node *block = get_nodes_block(n);
2448 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2449 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2452 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2453 dbg_info *dbgi = get_irn_dbg_info(n);
2454 ir_node *block = get_nodes_block(n);
2455 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2456 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2458 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2459 ir_node *sub_l = get_Sub_left(b);
2460 ir_node *sub_r = get_Sub_right(b);
2461 dbg_info *dbgi = get_irn_dbg_info(n);
2462 ir_graph *irg = current_ir_graph;
2463 ir_node *block = get_nodes_block(n);
2464 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2465 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2466 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2469 } else if (is_Minus(b)) {
2470 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2471 ir_node *sub_l = get_Sub_left(a);
2472 ir_node *sub_r = get_Sub_right(a);
2473 dbg_info *dbgi = get_irn_dbg_info(n);
2474 ir_graph *irg = current_ir_graph;
2475 ir_node *block = get_nodes_block(n);
2476 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2477 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2482 if (get_mode_arithmetic(mode) == irma_ieee754) {
2484 tarval *tv = get_Const_tarval(a);
2485 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2486 /* 2.0 * b = b + b */
2487 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2488 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2492 else if (is_Const(b)) {
2493 tarval *tv = get_Const_tarval(b);
2494 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2495 /* a * 2.0 = a + a */
2496 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2497 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2502 return arch_dep_replace_mul_with_shifts(n);
2503 } /* transform_node_Mul */
2506 * Transform a Div Node.
2508 static ir_node *transform_node_Div(ir_node *n) {
2509 ir_mode *mode = get_Div_resmode(n);
2510 ir_node *a = get_Div_left(n);
2511 ir_node *b = get_Div_right(n);
2515 if (is_Const(b) && is_const_Phi(a)) {
2516 /* check for Div(Phi, Const) */
2517 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2519 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2523 else if (is_Const(a) && is_const_Phi(b)) {
2524 /* check for Div(Const, Phi) */
2525 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2527 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2531 else if (is_const_Phi(a) && is_const_Phi(b)) {
2532 /* check for Div(Phi, Phi) */
2533 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2535 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2542 if (tv != tarval_bad) {
2543 value = new_Const(get_tarval_mode(tv), tv);
2545 DBG_OPT_CSTEVAL(n, value);
2548 ir_node *a = get_Div_left(n);
2549 ir_node *b = get_Div_right(n);
2552 if (a == b && value_not_zero(a, &dummy)) {
2553 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2554 value = new_Const(mode, get_mode_one(mode));
2555 DBG_OPT_CSTEVAL(n, value);
2558 if (mode_is_signed(mode) && is_Const(b)) {
2559 tarval *tv = get_Const_tarval(b);
2561 if (tv == get_mode_minus_one(mode)) {
2563 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2564 DBG_OPT_CSTEVAL(n, value);
2568 /* Try architecture dependent optimization */
2569 value = arch_dep_replace_div_by_const(n);
2577 /* Turn Div into a tuple (mem, jmp, bad, value) */
2578 mem = get_Div_mem(n);
2579 blk = get_irn_n(n, -1);
2581 /* skip a potential Pin */
2583 mem = get_Pin_op(mem);
2584 turn_into_tuple(n, pn_Div_max);
2585 set_Tuple_pred(n, pn_Div_M, mem);
2586 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2587 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2588 set_Tuple_pred(n, pn_Div_res, value);
2591 } /* transform_node_Div */
2594 * Transform a Mod node.
2596 static ir_node *transform_node_Mod(ir_node *n) {
2597 ir_mode *mode = get_Mod_resmode(n);
2598 ir_node *a = get_Mod_left(n);
2599 ir_node *b = get_Mod_right(n);
2603 if (is_Const(b) && is_const_Phi(a)) {
2604 /* check for Div(Phi, Const) */
2605 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2607 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2611 else if (is_Const(a) && is_const_Phi(b)) {
2612 /* check for Div(Const, Phi) */
2613 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2615 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2619 else if (is_const_Phi(a) && is_const_Phi(b)) {
2620 /* check for Div(Phi, Phi) */
2621 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2623 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2630 if (tv != tarval_bad) {
2631 value = new_Const(get_tarval_mode(tv), tv);
2633 DBG_OPT_CSTEVAL(n, value);
2636 ir_node *a = get_Mod_left(n);
2637 ir_node *b = get_Mod_right(n);
2640 if (a == b && value_not_zero(a, &dummy)) {
2641 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2642 value = new_Const(mode, get_mode_null(mode));
2643 DBG_OPT_CSTEVAL(n, value);
2646 if (mode_is_signed(mode) && is_Const(b)) {
2647 tarval *tv = get_Const_tarval(b);
2649 if (tv == get_mode_minus_one(mode)) {
2651 value = new_Const(mode, get_mode_null(mode));
2652 DBG_OPT_CSTEVAL(n, value);
2656 /* Try architecture dependent optimization */
2657 value = arch_dep_replace_mod_by_const(n);
2665 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2666 mem = get_Mod_mem(n);
2667 blk = get_irn_n(n, -1);
2669 /* skip a potential Pin */
2671 mem = get_Pin_op(mem);
2672 turn_into_tuple(n, pn_Mod_max);
2673 set_Tuple_pred(n, pn_Mod_M, mem);
2674 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2675 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2676 set_Tuple_pred(n, pn_Mod_res, value);
2679 } /* transform_node_Mod */
2682 * Transform a DivMod node.
2684 static ir_node *transform_node_DivMod(ir_node *n) {
2686 ir_node *a = get_DivMod_left(n);
2687 ir_node *b = get_DivMod_right(n);
2688 ir_mode *mode = get_DivMod_resmode(n);
2693 if (is_Const(b) && is_const_Phi(a)) {
2694 /* check for Div(Phi, Const) */
2695 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2696 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2698 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2699 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2703 else if (is_Const(a) && is_const_Phi(b)) {
2704 /* check for Div(Const, Phi) */
2705 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2706 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2708 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2709 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2713 else if (is_const_Phi(a) && is_const_Phi(b)) {
2714 /* check for Div(Phi, Phi) */
2715 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2716 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2718 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2719 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2726 if (tb != tarval_bad) {
2727 if (tb == get_mode_one(get_tarval_mode(tb))) {
2729 vb = new_Const(mode, get_mode_null(mode));
2730 DBG_OPT_CSTEVAL(n, vb);
2732 } else if (ta != tarval_bad) {
2733 tarval *resa, *resb;
2734 resa = tarval_div(ta, tb);
2735 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2736 Jmp for X result!? */
2737 resb = tarval_mod(ta, tb);
2738 if (resb == tarval_bad) return n; /* Causes exception! */
2739 va = new_Const(mode, resa);
2740 vb = new_Const(mode, resb);
2741 DBG_OPT_CSTEVAL(n, va);
2742 DBG_OPT_CSTEVAL(n, vb);
2744 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2745 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2746 vb = new_Const(mode, get_mode_null(mode));
2747 DBG_OPT_CSTEVAL(n, va);
2748 DBG_OPT_CSTEVAL(n, vb);
2750 } else { /* Try architecture dependent optimization */
2753 arch_dep_replace_divmod_by_const(&va, &vb, n);
2754 evaluated = va != NULL;
2756 } else if (a == b) {
2757 if (value_not_zero(a, &dummy)) {
2759 va = new_Const(mode, get_mode_one(mode));
2760 vb = new_Const(mode, get_mode_null(mode));
2761 DBG_OPT_CSTEVAL(n, va);
2762 DBG_OPT_CSTEVAL(n, vb);
2765 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2768 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2769 /* 0 / non-Const = 0 */
2774 if (evaluated) { /* replace by tuple */
2778 mem = get_DivMod_mem(n);
2779 /* skip a potential Pin */
2781 mem = get_Pin_op(mem);
2783 blk = get_irn_n(n, -1);
2784 turn_into_tuple(n, pn_DivMod_max);
2785 set_Tuple_pred(n, pn_DivMod_M, mem);
2786 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2787 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2788 set_Tuple_pred(n, pn_DivMod_res_div, va);
2789 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2793 } /* transform_node_DivMod */
2796 * Optimize x / c to x * (1/c)
2798 static ir_node *transform_node_Quot(ir_node *n) {
2799 ir_mode *mode = get_Quot_resmode(n);
2802 if (get_mode_arithmetic(mode) == irma_ieee754) {
2803 ir_node *b = get_Quot_right(n);
2806 tarval *tv = get_Const_tarval(b);
2810 * Floating point constant folding might be disabled here to
2812 * However, as we check for exact result, doing it is safe.
2815 rem = tarval_enable_fp_ops(1);
2816 tv = tarval_quo(get_mode_one(mode), tv);
2817 (void)tarval_enable_fp_ops(rem);
2819 /* Do the transformation if the result is either exact or we are not
2820 using strict rules. */
2821 if (tv != tarval_bad &&
2822 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2823 ir_node *blk = get_irn_n(n, -1);
2824 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2825 ir_node *a = get_Quot_left(n);
2826 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2827 ir_node *mem = get_Quot_mem(n);
2829 /* skip a potential Pin */
2831 mem = get_Pin_op(mem);
2832 turn_into_tuple(n, pn_Quot_max);
2833 set_Tuple_pred(n, pn_Quot_M, mem);
2834 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2835 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2836 set_Tuple_pred(n, pn_Quot_res, m);
2837 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2842 } /* transform_node_Quot */
2845 * Optimize Abs(x) into x if x is Confirmed >= 0
2846 * Optimize Abs(x) into -x if x is Confirmed <= 0
2847 * Optimize Abs(-x) int Abs(x)
2849 static ir_node *transform_node_Abs(ir_node *n) {
2850 ir_node *c, *oldn = n;
2851 ir_node *a = get_Abs_op(n);
2854 HANDLE_UNOP_PHI(tarval_abs, a, c);
2856 switch (classify_value_sign(a)) {
2857 case value_classified_negative:
2858 mode = get_irn_mode(n);
2861 * We can replace the Abs by -x here.
2862 * We even could add a new Confirm here
2863 * (if not twos complement)
2865 * Note that -x would create a new node, so we could
2866 * not run it in the equivalent_node() context.
2868 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2869 get_nodes_block(n), a, mode);
2871 DBG_OPT_CONFIRM(oldn, n);
2873 case value_classified_positive:
2874 /* n is positive, Abs is not needed */
2877 DBG_OPT_CONFIRM(oldn, n);
2883 /* Abs(-x) = Abs(x) */
2884 mode = get_irn_mode(n);
2885 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2886 get_nodes_block(n), get_Minus_op(a), mode);
2887 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2891 } /* transform_node_Abs */
2894 * Transform a Cond node.
2896 * Replace the Cond by a Jmp if it branches on a constant
2899 static ir_node *transform_node_Cond(ir_node *n) {
2902 ir_node *a = get_Cond_selector(n);
2903 tarval *ta = value_of(a);
2905 /* we need block info which is not available in floating irgs */
2906 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2909 if ((ta != tarval_bad) &&
2910 (get_irn_mode(a) == mode_b) &&
2911 (get_opt_unreachable_code())) {
2912 /* It's a boolean Cond, branching on a boolean constant.
2913 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2914 ir_node *blk = get_nodes_block(n);
2915 jmp = new_r_Jmp(current_ir_graph, blk);
2916 turn_into_tuple(n, pn_Cond_max);
2917 if (ta == tarval_b_true) {
2918 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2919 set_Tuple_pred(n, pn_Cond_true, jmp);
2921 set_Tuple_pred(n, pn_Cond_false, jmp);
2922 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2924 /* We might generate an endless loop, so keep it alive. */
2925 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2928 } /* transform_node_Cond */
2931 * Prototype of a recursive transform function
2932 * for bitwise distributive transformations.
2934 typedef ir_node* (*recursive_transform)(ir_node *n);
2937 * makes use of distributive laws for and, or, eor
2938 * and(a OP c, b OP c) -> and(a, b) OP c
2939 * note, might return a different op than n
2941 static ir_node *transform_bitwise_distributive(ir_node *n,
2942 recursive_transform trans_func)
2945 ir_node *a = get_binop_left(n);
2946 ir_node *b = get_binop_right(n);
2947 ir_op *op = get_irn_op(a);
2948 ir_op *op_root = get_irn_op(n);
2950 if(op != get_irn_op(b))
2953 if (op == op_Conv) {
2954 ir_node *a_op = get_Conv_op(a);
2955 ir_node *b_op = get_Conv_op(b);
2956 ir_mode *a_mode = get_irn_mode(a_op);
2957 ir_mode *b_mode = get_irn_mode(b_op);
2958 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2959 ir_node *blk = get_irn_n(n, -1);
2962 set_binop_left(n, a_op);
2963 set_binop_right(n, b_op);
2964 set_irn_mode(n, a_mode);
2966 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2968 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2974 /* nothing to gain here */
2978 if (op == op_Shrs || op == op_Shr || op == op_Shl
2979 || op == op_And || op == op_Or || op == op_Eor) {
2980 ir_node *a_left = get_binop_left(a);
2981 ir_node *a_right = get_binop_right(a);
2982 ir_node *b_left = get_binop_left(b);
2983 ir_node *b_right = get_binop_right(b);
2985 ir_node *op1 = NULL;
2986 ir_node *op2 = NULL;
2988 if (is_op_commutative(op)) {
2989 if (a_left == b_left) {
2993 } else if(a_left == b_right) {
2997 } else if(a_right == b_left) {
3003 if(a_right == b_right) {
3010 /* (a sop c) & (b sop c) => (a & b) sop c */
3011 ir_node *blk = get_irn_n(n, -1);
3013 ir_node *new_n = exact_copy(n);
3014 set_binop_left(new_n, op1);
3015 set_binop_right(new_n, op2);
3016 new_n = trans_func(new_n);
3018 if(op_root == op_Eor && op == op_Or) {
3019 dbg_info *dbgi = get_irn_dbg_info(n);
3020 ir_graph *irg = current_ir_graph;
3021 ir_mode *mode = get_irn_mode(c);
3023 c = new_rd_Not(dbgi, irg, blk, c, mode);
3024 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3027 set_nodes_block(n, blk);
3028 set_binop_left(n, new_n);
3029 set_binop_right(n, c);
3030 add_identities(current_ir_graph->value_table, n);
3033 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3044 static ir_node *transform_node_And(ir_node *n) {
3045 ir_node *c, *oldn = n;
3046 ir_node *a = get_And_left(n);
3047 ir_node *b = get_And_right(n);
3050 mode = get_irn_mode(n);
3051 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3053 /* we can evaluate 2 Projs of the same Cmp */
3054 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3055 ir_node *pred_a = get_Proj_pred(a);
3056 ir_node *pred_b = get_Proj_pred(b);
3057 if (pred_a == pred_b) {
3058 dbg_info *dbgi = get_irn_dbg_info(n);
3059 ir_node *block = get_nodes_block(pred_a);
3060 pn_Cmp pn_a = get_Proj_proj(a);
3061 pn_Cmp pn_b = get_Proj_proj(b);
3062 /* yes, we can simply calculate with pncs */
3063 pn_Cmp new_pnc = pn_a & pn_b;
3065 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3070 ir_node *op = get_Not_op(b);
3072 ir_node *ba = get_And_left(op);
3073 ir_node *bb = get_And_right(op);
3075 /* it's enough to test the following cases due to normalization! */
3076 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3077 /* (a|b) & ~(a&b) = a^b */
3078 ir_node *block = get_nodes_block(n);
3080 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3081 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3089 ir_node *op = get_Not_op(a);
3091 ir_node *aa = get_And_left(op);
3092 ir_node *ab = get_And_right(op);
3094 /* it's enough to test the following cases due to normalization! */
3095 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3096 /* (a|b) & ~(a&b) = a^b */
3097 ir_node *block = get_nodes_block(n);
3099 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3100 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3107 ir_node *al = get_Eor_left(a);
3108 ir_node *ar = get_Eor_right(a);
3111 /* (b ^ a) & b -> ~a & b */
3112 dbg_info *dbg = get_irn_dbg_info(n);
3113 ir_node *block = get_nodes_block(n);
3115 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3116 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3117 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3121 /* (a ^ b) & b -> ~a & b */
3122 dbg_info *dbg = get_irn_dbg_info(n);
3123 ir_node *block = get_nodes_block(n);
3125 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3126 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3127 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3132 ir_node *bl = get_Eor_left(b);
3133 ir_node *br = get_Eor_right(b);
3136 /* a & (a ^ b) -> a & ~b */
3137 dbg_info *dbg = get_irn_dbg_info(n);
3138 ir_node *block = get_nodes_block(n);
3140 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3141 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3146 /* a & (b ^ a) -> a & ~b */
3147 dbg_info *dbg = get_irn_dbg_info(n);
3148 ir_node *block = get_nodes_block(n);
3150 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3151 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3152 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3156 if (is_Not(a) && is_Not(b)) {
3157 /* ~a & ~b = ~(a|b) */
3158 ir_node *block = get_nodes_block(n);
3159 ir_mode *mode = get_irn_mode(n);
3163 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3164 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3165 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3169 n = transform_bitwise_distributive(n, transform_node_And);
3172 } /* transform_node_And */
3177 static ir_node *transform_node_Eor(ir_node *n) {
3178 ir_node *c, *oldn = n;
3179 ir_node *a = get_Eor_left(n);
3180 ir_node *b = get_Eor_right(n);
3181 ir_mode *mode = get_irn_mode(n);
3183 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3185 /* we can evaluate 2 Projs of the same Cmp */
3186 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3187 ir_node *pred_a = get_Proj_pred(a);
3188 ir_node *pred_b = get_Proj_pred(b);
3189 if(pred_a == pred_b) {
3190 dbg_info *dbgi = get_irn_dbg_info(n);
3191 ir_node *block = get_nodes_block(pred_a);
3192 pn_Cmp pn_a = get_Proj_proj(a);
3193 pn_Cmp pn_b = get_Proj_proj(b);
3194 /* yes, we can simply calculate with pncs */
3195 pn_Cmp new_pnc = pn_a ^ pn_b;
3197 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3204 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3205 mode, get_mode_null(mode));
3206 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3207 } else if (mode == mode_b &&
3209 is_Const(b) && is_Const_one(b) &&
3210 is_Cmp(get_Proj_pred(a))) {
3211 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3212 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3213 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3215 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3216 } else if (is_Const(b)) {
3217 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3218 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3219 ir_node *not_op = get_Not_op(a);
3220 dbg_info *dbg = get_irn_dbg_info(n);
3221 ir_graph *irg = current_ir_graph;
3222 ir_node *block = get_nodes_block(n);
3223 ir_mode *mode = get_irn_mode(n);
3224 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3226 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3227 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3228 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3231 n = transform_bitwise_distributive(n, transform_node_Eor);
3235 } /* transform_node_Eor */
3240 static ir_node *transform_node_Not(ir_node *n) {
3241 ir_node *c, *oldn = n;
3242 ir_node *a = get_Not_op(n);
3243 ir_mode *mode = get_irn_mode(n);
3245 HANDLE_UNOP_PHI(tarval_not,a,c);
3247 /* check for a boolean Not */
3248 if (mode == mode_b &&
3250 is_Cmp(get_Proj_pred(a))) {
3251 /* We negate a Cmp. The Cmp has the negated result anyways! */
3252 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3253 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3254 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3258 ir_node *eor_b = get_Eor_right(a);
3259 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3260 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3261 ir_node *eor_a = get_Eor_left(a);
3262 dbg_info *dbg = get_irn_dbg_info(n);
3263 ir_graph *irg = current_ir_graph;
3264 ir_node *block = get_nodes_block(n);
3265 ir_mode *mode = get_irn_mode(n);
3266 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3270 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3271 if (is_Minus(a)) { /* ~-x -> x + -1 */
3272 dbg_info *dbg = get_irn_dbg_info(n);
3273 ir_graph *irg = current_ir_graph;
3274 ir_node *block = get_nodes_block(n);
3275 ir_node *add_l = get_Minus_op(a);
3276 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3277 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3278 } else if (is_Add(a)) {
3279 ir_node *add_r = get_Add_right(a);
3280 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3281 /* ~(x + -1) = -x */
3282 ir_node *op = get_Add_left(a);
3283 ir_node *blk = get_irn_n(n, -1);
3284 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3285 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3290 } /* transform_node_Not */
3293 * Transform a Minus.
3297 * -(a >>u (size-1)) = a >>s (size-1)
3298 * -(a >>s (size-1)) = a >>u (size-1)
3299 * -(a * const) -> a * -const
3301 static ir_node *transform_node_Minus(ir_node *n) {
3302 ir_node *c, *oldn = n;
3303 ir_node *a = get_Minus_op(n);
3306 HANDLE_UNOP_PHI(tarval_neg,a,c);
3308 mode = get_irn_mode(a);
3309 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3310 /* the following rules are only to twos-complement */
3313 ir_node *op = get_Not_op(a);
3314 tarval *tv = get_mode_one(mode);
3315 ir_node *blk = get_irn_n(n, -1);
3316 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3317 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3318 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3322 ir_node *c = get_Shr_right(a);
3325 tarval *tv = get_Const_tarval(c);
3327 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3328 /* -(a >>u (size-1)) = a >>s (size-1) */
3329 ir_node *v = get_Shr_left(a);
3331 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3332 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3338 ir_node *c = get_Shrs_right(a);
3341 tarval *tv = get_Const_tarval(c);
3343 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3344 /* -(a >>s (size-1)) = a >>u (size-1) */
3345 ir_node *v = get_Shrs_left(a);
3347 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3348 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3355 /* - (a-b) = b - a */
3356 ir_node *la = get_Sub_left(a);
3357 ir_node *ra = get_Sub_right(a);
3358 ir_node *blk = get_irn_n(n, -1);
3360 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3361 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3365 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3366 ir_node *mul_l = get_Mul_left(a);
3367 ir_node *mul_r = get_Mul_right(a);
3368 if (is_Const(mul_r)) {
3369 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3370 if(tv != tarval_bad) {
3371 ir_node *cnst = new_Const(mode, tv);
3372 dbg_info *dbg = get_irn_dbg_info(a);
3373 ir_graph *irg = current_ir_graph;
3374 ir_node *block = get_nodes_block(a);
3375 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3376 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3383 } /* transform_node_Minus */
3386 * Transform a Cast_type(Const) into a new Const_type
3388 static ir_node *transform_node_Cast(ir_node *n) {
3390 ir_node *pred = get_Cast_op(n);
3391 ir_type *tp = get_irn_type(n);
3393 if (is_Const(pred) && get_Const_type(pred) != tp) {
3394 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3395 get_Const_tarval(pred), tp);
3396 DBG_OPT_CSTEVAL(oldn, n);
3397 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3398 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3399 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3400 DBG_OPT_CSTEVAL(oldn, n);
3404 } /* transform_node_Cast */
3407 * Transform a Proj(Div) with a non-zero value.
3408 * Removes the exceptions and routes the memory to the NoMem node.
3410 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3411 ir_node *div = get_Proj_pred(proj);
3412 ir_node *b = get_Div_right(div);
3413 ir_node *confirm, *res, *new_mem;
3416 if (value_not_zero(b, &confirm)) {
3417 /* div(x, y) && y != 0 */
3418 if (confirm == NULL) {
3419 /* we are sure we have a Const != 0 */
3420 new_mem = get_Div_mem(div);
3421 if (is_Pin(new_mem))
3422 new_mem = get_Pin_op(new_mem);
3423 set_Div_mem(div, new_mem);
3424 set_irn_pinned(div, op_pin_state_floats);
3427 proj_nr = get_Proj_proj(proj);
3429 case pn_Div_X_regular:
3430 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3432 case pn_Div_X_except:
3433 /* we found an exception handler, remove it */
3434 DBG_OPT_EXC_REM(proj);
3438 res = get_Div_mem(div);
3439 new_mem = get_irg_no_mem(current_ir_graph);
3442 /* This node can only float up to the Confirm block */
3443 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3445 set_irn_pinned(div, op_pin_state_floats);
3446 /* this is a Div without exception, we can remove the memory edge */
3447 set_Div_mem(div, new_mem);
3452 } /* transform_node_Proj_Div */
3455 * Transform a Proj(Mod) with a non-zero value.
3456 * Removes the exceptions and routes the memory to the NoMem node.
3458 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3459 ir_node *mod = get_Proj_pred(proj);
3460 ir_node *b = get_Mod_right(mod);
3461 ir_node *confirm, *res, *new_mem;
3464 if (value_not_zero(b, &confirm)) {
3465 /* mod(x, y) && y != 0 */
3466 proj_nr = get_Proj_proj(proj);
3468 if (confirm == NULL) {
3469 /* we are sure we have a Const != 0 */
3470 new_mem = get_Mod_mem(mod);
3471 if (is_Pin(new_mem))
3472 new_mem = get_Pin_op(new_mem);
3473 set_Mod_mem(mod, new_mem);
3474 set_irn_pinned(mod, op_pin_state_floats);
3479 case pn_Mod_X_regular:
3480 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3482 case pn_Mod_X_except:
3483 /* we found an exception handler, remove it */
3484 DBG_OPT_EXC_REM(proj);
3488 res = get_Mod_mem(mod);
3489 new_mem = get_irg_no_mem(current_ir_graph);
3492 /* This node can only float up to the Confirm block */
3493 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3495 /* this is a Mod without exception, we can remove the memory edge */
3496 set_Mod_mem(mod, new_mem);
3499 if (get_Mod_left(mod) == b) {
3500 /* a % a = 0 if a != 0 */
3501 ir_mode *mode = get_irn_mode(proj);
3502 ir_node *res = new_Const(mode, get_mode_null(mode));
3504 DBG_OPT_CSTEVAL(mod, res);
3510 } /* transform_node_Proj_Mod */
3513 * Transform a Proj(DivMod) with a non-zero value.
3514 * Removes the exceptions and routes the memory to the NoMem node.
3516 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3517 ir_node *divmod = get_Proj_pred(proj);
3518 ir_node *b = get_DivMod_right(divmod);
3519 ir_node *confirm, *res, *new_mem;
3522 if (value_not_zero(b, &confirm)) {
3523 /* DivMod(x, y) && y != 0 */
3524 proj_nr = get_Proj_proj(proj);
3526 if (confirm == NULL) {
3527 /* we are sure we have a Const != 0 */
3528 new_mem = get_DivMod_mem(divmod);
3529 if (is_Pin(new_mem))
3530 new_mem = get_Pin_op(new_mem);
3531 set_DivMod_mem(divmod, new_mem);
3532 set_irn_pinned(divmod, op_pin_state_floats);
3537 case pn_DivMod_X_regular:
3538 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3540 case pn_DivMod_X_except:
3541 /* we found an exception handler, remove it */
3542 DBG_OPT_EXC_REM(proj);
3546 res = get_DivMod_mem(divmod);
3547 new_mem = get_irg_no_mem(current_ir_graph);
3550 /* This node can only float up to the Confirm block */
3551 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3553 /* this is a DivMod without exception, we can remove the memory edge */
3554 set_DivMod_mem(divmod, new_mem);
3557 case pn_DivMod_res_mod:
3558 if (get_DivMod_left(divmod) == b) {
3559 /* a % a = 0 if a != 0 */
3560 ir_mode *mode = get_irn_mode(proj);
3561 ir_node *res = new_Const(mode, get_mode_null(mode));
3563 DBG_OPT_CSTEVAL(divmod, res);
3569 } /* transform_node_Proj_DivMod */
3572 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3574 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3575 if (get_opt_unreachable_code()) {
3576 ir_node *n = get_Proj_pred(proj);
3577 ir_node *b = get_Cond_selector(n);
3579 if (mode_is_int(get_irn_mode(b))) {
3580 tarval *tb = value_of(b);
3582 if (tb != tarval_bad) {
3583 /* we have a constant switch */
3584 long num = get_Proj_proj(proj);
3586 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3587 if (get_tarval_long(tb) == num) {
3588 /* Do NOT create a jump here, or we will have 2 control flow ops
3589 * in a block. This case is optimized away in optimize_cf(). */
3592 /* this case will NEVER be taken, kill it */
3600 } /* transform_node_Proj_Cond */
3603 * Create a 0 constant of given mode.
3605 static ir_node *create_zero_const(ir_mode *mode) {
3606 tarval *tv = get_mode_null(mode);
3607 ir_node *cnst = new_Const(mode, tv);
3612 /* the order of the values is important! */
3613 typedef enum const_class {
3619 static const_class classify_const(const ir_node* n)
3621 if (is_Const(n)) return const_const;
3622 if (is_irn_constlike(n)) return const_like;
3627 * Determines whether r is more constlike or has a larger index (in that order)
3630 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3632 const const_class l_order = classify_const(l);
3633 const const_class r_order = classify_const(r);
3635 l_order > r_order ||
3636 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3640 * Normalizes and optimizes Cmp nodes.
3642 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3643 ir_node *n = get_Proj_pred(proj);
3644 ir_node *left = get_Cmp_left(n);
3645 ir_node *right = get_Cmp_right(n);
3648 ir_mode *mode = NULL;
3649 long proj_nr = get_Proj_proj(proj);
3651 /* we can evaluate some cases directly */
3654 return new_Const(mode_b, get_tarval_b_false());
3656 return new_Const(mode_b, get_tarval_b_true());
3658 if (!mode_is_float(get_irn_mode(left)))
3659 return new_Const(mode_b, get_tarval_b_true());
3665 /* remove Casts of both sides */
3666 left = skip_Cast(left);
3667 right = skip_Cast(right);
3669 /* Remove unnecessary conversions */
3670 /* TODO handle constants */
3671 if (is_Conv(left) && is_Conv(right)) {
3672 ir_mode *mode = get_irn_mode(left);
3673 ir_node *op_left = get_Conv_op(left);
3674 ir_node *op_right = get_Conv_op(right);
3675 ir_mode *mode_left = get_irn_mode(op_left);
3676 ir_mode *mode_right = get_irn_mode(op_right);
3678 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3679 && mode_left != mode_b && mode_right != mode_b) {
3680 ir_graph *irg = current_ir_graph;
3681 ir_node *block = get_nodes_block(n);
3683 if (mode_left == mode_right) {
3687 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3688 } else if (smaller_mode(mode_left, mode_right)) {
3689 left = new_r_Conv(irg, block, op_left, mode_right);
3692 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3693 } else if (smaller_mode(mode_right, mode_left)) {
3695 right = new_r_Conv(irg, block, op_right, mode_left);
3697 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3702 /* remove operation on both sides if possible */
3703 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3705 * The following operations are NOT safe for floating point operations, for instance
3706 * 1.0 + inf == 2.0 + inf, =/=> x == y
3708 if (mode_is_int(get_irn_mode(left))) {
3709 unsigned lop = get_irn_opcode(left);
3711 if (lop == get_irn_opcode(right)) {
3712 ir_node *ll, *lr, *rl, *rr;
3714 /* same operation on both sides, try to remove */
3718 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3719 left = get_unop_op(left);
3720 right = get_unop_op(right);
3722 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3725 ll = get_Add_left(left);
3726 lr = get_Add_right(left);
3727 rl = get_Add_left(right);
3728 rr = get_Add_right(right);
3731 /* X + a CMP X + b ==> a CMP b */
3735 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3736 } else if (ll == rr) {
3737 /* X + a CMP b + X ==> a CMP b */
3741 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3742 } else if (lr == rl) {
3743 /* a + X CMP X + b ==> a CMP b */
3747 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3748 } else if (lr == rr) {
3749 /* a + X CMP b + X ==> a CMP b */
3753 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3757 ll = get_Sub_left(left);
3758 lr = get_Sub_right(left);
3759 rl = get_Sub_left(right);
3760 rr = get_Sub_right(right);
3763 /* X - a CMP X - b ==> a CMP b */
3767 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3768 } else if (lr == rr) {
3769 /* a - X CMP b - X ==> a CMP b */
3773 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3777 if (get_Rot_right(left) == get_Rot_right(right)) {
3778 /* a ROT X CMP b ROT X ==> a CMP b */
3779 left = get_Rot_left(left);
3780 right = get_Rot_left(right);
3782 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3790 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3791 if (is_Add(left) || is_Sub(left)) {
3792 ir_node *ll = get_binop_left(left);
3793 ir_node *lr = get_binop_right(left);
3795 if (lr == right && is_Add(left)) {
3802 right = create_zero_const(get_irn_mode(left));
3804 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3807 if (is_Add(right) || is_Sub(right)) {
3808 ir_node *rl = get_binop_left(right);
3809 ir_node *rr = get_binop_right(right);
3811 if (rr == left && is_Add(right)) {
3818 right = create_zero_const(get_irn_mode(left));
3820 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3823 } /* mode_is_int(...) */
3824 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3826 /* replace mode_b compares with ands/ors */
3827 if (get_irn_mode(left) == mode_b) {
3828 ir_graph *irg = current_ir_graph;
3829 ir_node *block = get_nodes_block(n);
3833 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3834 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3835 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3836 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3837 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3838 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3839 default: bres = NULL;
3842 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3848 * First step: normalize the compare op
3849 * by placing the constant on the right side
3850 * or moving the lower address node to the left.
3852 if (!operands_are_normalized(left, right)) {
3858 proj_nr = get_inversed_pnc(proj_nr);
3863 * Second step: Try to reduce the magnitude
3864 * of a constant. This may help to generate better code
3865 * later and may help to normalize more compares.
3866 * Of course this is only possible for integer values.
3868 if (is_Const(right)) {
3869 mode = get_irn_mode(right);
3870 tv = get_Const_tarval(right);
3872 /* TODO extend to arbitrary constants */
3873 if (is_Conv(left) && tarval_is_null(tv)) {
3874 ir_node *op = get_Conv_op(left);
3875 ir_mode *op_mode = get_irn_mode(op);
3878 * UpConv(x) REL 0 ==> x REL 0
3880 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3881 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3882 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3883 tv = get_mode_null(op_mode);
3887 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3891 if (tv != tarval_bad) {
3892 /* the following optimization is possible on modes without Overflow
3893 * on Unary Minus or on == and !=:
3894 * -a CMP c ==> a swap(CMP) -c
3896 * Beware: for two-complement Overflow may occur, so only == and != can
3897 * be optimized, see this:
3898 * -MININT < 0 =/=> MININT > 0 !!!
3900 if (is_Minus(left) &&
3901 (!mode_overflow_on_unary_Minus(mode) ||
3902 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3903 tv = tarval_neg(tv);
3905 if (tv != tarval_bad) {
3906 left = get_Minus_op(left);
3907 proj_nr = get_inversed_pnc(proj_nr);
3909 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3911 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3912 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3913 tv = tarval_not(tv);
3915 if (tv != tarval_bad) {
3916 left = get_Not_op(left);
3918 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3922 /* for integer modes, we have more */
3923 if (mode_is_int(mode)) {
3924 /* Ne includes Unordered which is not possible on integers.
3925 * However, frontends often use this wrong, so fix it here */
3926 if (proj_nr & pn_Cmp_Uo) {
3927 proj_nr &= ~pn_Cmp_Uo;
3928 set_Proj_proj(proj, proj_nr);
3931 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3932 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3933 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3934 tv = tarval_sub(tv, get_mode_one(mode));
3936 if (tv != tarval_bad) {
3937 proj_nr ^= pn_Cmp_Eq;
3939 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3942 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3943 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3944 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3945 tv = tarval_add(tv, get_mode_one(mode));
3947 if (tv != tarval_bad) {
3948 proj_nr ^= pn_Cmp_Eq;
3950 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3954 /* the following reassociations work only for == and != */
3955 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3957 #if 0 /* Might be not that good in general */
3958 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3959 if (tarval_is_null(tv) && is_Sub(left)) {
3960 right = get_Sub_right(left);
3961 left = get_Sub_left(left);
3963 tv = value_of(right);
3965 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3969 if (tv != tarval_bad) {
3970 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3972 ir_node *c1 = get_Sub_right(left);
3973 tarval *tv2 = value_of(c1);
3975 if (tv2 != tarval_bad) {
3976 tv2 = tarval_add(tv, value_of(c1));
3978 if (tv2 != tarval_bad) {
3979 left = get_Sub_left(left);
3982 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3986 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3987 else if (is_Add(left)) {
3988 ir_node *a_l = get_Add_left(left);
3989 ir_node *a_r = get_Add_right(left);
3993 if (is_Const(a_l)) {
3995 tv2 = value_of(a_l);
3998 tv2 = value_of(a_r);
4001 if (tv2 != tarval_bad) {
4002 tv2 = tarval_sub(tv, tv2);
4004 if (tv2 != tarval_bad) {
4008 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4012 /* -a == c ==> a == -c, -a != c ==> a != -c */
4013 else if (is_Minus(left)) {
4014 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4016 if (tv2 != tarval_bad) {
4017 left = get_Minus_op(left);
4020 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4025 /* the following reassociations work only for <= */
4026 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4027 if (tv != tarval_bad) {
4028 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4029 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4035 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4036 switch (get_irn_opcode(left)) {
4040 c1 = get_And_right(left);
4043 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4044 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4046 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4048 /* TODO: move to constant evaluation */
4049 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4050 c1 = new_Const(mode_b, tv);
4051 DBG_OPT_CSTEVAL(proj, c1);
4055 if (tarval_is_single_bit(tv)) {
4057 * optimization for AND:
4059 * And(x, C) == C ==> And(x, C) != 0
4060 * And(x, C) != C ==> And(X, C) == 0
4062 * if C is a single Bit constant.
4065 /* check for Constant's match. We have check hare the tarvals,
4066 because our const might be changed */
4067 if (get_Const_tarval(c1) == tv) {
4068 /* fine: do the transformation */
4069 tv = get_mode_null(get_tarval_mode(tv));
4070 proj_nr ^= pn_Cmp_Leg;
4072 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4078 c1 = get_Or_right(left);
4079 if (is_Const(c1) && tarval_is_null(tv)) {
4081 * Or(x, C) == 0 && C != 0 ==> FALSE
4082 * Or(x, C) != 0 && C != 0 ==> TRUE
4084 if (! tarval_is_null(get_Const_tarval(c1))) {
4085 /* TODO: move to constant evaluation */
4086 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4087 c1 = new_Const(mode_b, tv);
4088 DBG_OPT_CSTEVAL(proj, c1);
4095 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4097 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4100 c1 = get_Shl_right(left);
4102 tarval *tv1 = get_Const_tarval(c1);
4103 ir_mode *mode = get_irn_mode(left);
4104 tarval *minus1 = get_mode_all_one(mode);
4105 tarval *amask = tarval_shr(minus1, tv1);
4106 tarval *cmask = tarval_shl(minus1, tv1);
4109 if (tarval_and(tv, cmask) != tv) {
4110 /* condition not met */
4111 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4112 c1 = new_Const(mode_b, tv);
4113 DBG_OPT_CSTEVAL(proj, c1);
4116 sl = get_Shl_left(left);
4117 blk = get_nodes_block(n);
4118 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4119 tv = tarval_shr(tv, tv1);
4121 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4126 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4128 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4131 c1 = get_Shr_right(left);
4133 tarval *tv1 = get_Const_tarval(c1);
4134 ir_mode *mode = get_irn_mode(left);
4135 tarval *minus1 = get_mode_all_one(mode);
4136 tarval *amask = tarval_shl(minus1, tv1);
4137 tarval *cmask = tarval_shr(minus1, tv1);
4140 if (tarval_and(tv, cmask) != tv) {
4141 /* condition not met */
4142 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4143 c1 = new_Const(mode_b, tv);
4144 DBG_OPT_CSTEVAL(proj, c1);
4147 sl = get_Shr_left(left);
4148 blk = get_nodes_block(n);
4149 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4150 tv = tarval_shl(tv, tv1);
4152 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4157 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4159 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4162 c1 = get_Shrs_right(left);
4164 tarval *tv1 = get_Const_tarval(c1);
4165 ir_mode *mode = get_irn_mode(left);
4166 tarval *minus1 = get_mode_all_one(mode);
4167 tarval *amask = tarval_shl(minus1, tv1);
4168 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4171 cond = tarval_sub(cond, tv1);
4172 cond = tarval_shrs(tv, cond);
4174 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4175 /* condition not met */
4176 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4177 c1 = new_Const(mode_b, tv);
4178 DBG_OPT_CSTEVAL(proj, c1);
4181 sl = get_Shrs_left(left);
4182 blk = get_nodes_block(n);
4183 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4184 tv = tarval_shl(tv, tv1);
4186 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4191 } /* tarval != bad */
4194 if (changed & 2) /* need a new Const */
4195 right = new_Const(mode, tv);
4197 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4198 ir_node *op = get_Proj_pred(left);
4200 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4201 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4202 ir_node *c = get_binop_right(op);
4205 tarval *tv = get_Const_tarval(c);
4207 if (tarval_is_single_bit(tv)) {
4208 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4209 ir_node *v = get_binop_left(op);
4210 ir_node *blk = get_irn_n(op, -1);
4211 ir_mode *mode = get_irn_mode(v);
4213 tv = tarval_sub(tv, get_mode_one(mode));
4214 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4216 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4223 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4225 /* create a new compare */
4226 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4227 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4231 } /* transform_node_Proj_Cmp */
4234 * Does all optimizations on nodes that must be done on it's Proj's
4235 * because of creating new nodes.
4237 static ir_node *transform_node_Proj(ir_node *proj) {
4238 ir_node *n = get_Proj_pred(proj);
4240 switch (get_irn_opcode(n)) {
4242 return transform_node_Proj_Div(proj);
4245 return transform_node_Proj_Mod(proj);
4248 return transform_node_Proj_DivMod(proj);
4251 return transform_node_Proj_Cond(proj);
4254 return transform_node_Proj_Cmp(proj);
4257 /* should not happen, but if it does will be optimized away */
4258 return equivalent_node_Proj(proj);
4264 } /* transform_node_Proj */
4267 * Move Confirms down through Phi nodes.
4269 static ir_node *transform_node_Phi(ir_node *phi) {
4271 ir_mode *mode = get_irn_mode(phi);
4273 if (mode_is_reference(mode)) {
4274 n = get_irn_arity(phi);
4276 /* Beware of Phi0 */
4278 ir_node *pred = get_irn_n(phi, 0);
4279 ir_node *bound, *new_Phi, *block, **in;
4282 if (! is_Confirm(pred))
4285 bound = get_Confirm_bound(pred);
4286 pnc = get_Confirm_cmp(pred);
4288 NEW_ARR_A(ir_node *, in, n);
4289 in[0] = get_Confirm_value(pred);
4291 for (i = 1; i < n; ++i) {
4292 pred = get_irn_n(phi, i);
4294 if (! is_Confirm(pred) ||
4295 get_Confirm_bound(pred) != bound ||
4296 get_Confirm_cmp(pred) != pnc)
4298 in[i] = get_Confirm_value(pred);
4300 /* move the Confirm nodes "behind" the Phi */
4301 block = get_irn_n(phi, -1);
4302 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4303 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4307 } /* transform_node_Phi */
4310 * Returns the operands of a commutative bin-op, if one operand is
4311 * a const, it is returned as the second one.
4313 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4314 ir_node *op_a = get_binop_left(binop);
4315 ir_node *op_b = get_binop_right(binop);
4317 assert(is_op_commutative(get_irn_op(binop)));
4319 if (is_Const(op_a)) {
4326 } /* get_comm_Binop_Ops */
4329 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4330 * Such pattern may arise in bitfield stores.
4332 * value c4 value c4 & c2
4333 * AND c3 AND c1 | c3
4340 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4343 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4346 ir_node *and_l, *c3;
4347 ir_node *value, *c4;
4348 ir_node *new_and, *new_const, *block;
4349 ir_mode *mode = get_irn_mode(or);
4351 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4354 get_comm_Binop_Ops(or, &and, &c1);
4355 if (!is_Const(c1) || !is_And(and))
4358 get_comm_Binop_Ops(and, &or_l, &c2);
4362 tv1 = get_Const_tarval(c1);
4363 tv2 = get_Const_tarval(c2);
4365 tv = tarval_or(tv1, tv2);
4366 if (tarval_is_all_one(tv)) {
4367 /* the AND does NOT clear a bit with isn't set by the OR */
4368 set_Or_left(or, or_l);
4369 set_Or_right(or, c1);
4371 /* check for more */
4378 get_comm_Binop_Ops(or_l, &and_l, &c3);
4379 if (!is_Const(c3) || !is_And(and_l))
4382 get_comm_Binop_Ops(and_l, &value, &c4);
4386 /* ok, found the pattern, check for conditions */
4387 assert(mode == get_irn_mode(and));
4388 assert(mode == get_irn_mode(or_l));
4389 assert(mode == get_irn_mode(and_l));
4391 tv3 = get_Const_tarval(c3);
4392 tv4 = get_Const_tarval(c4);
4394 tv = tarval_or(tv4, tv2);
4395 if (!tarval_is_all_one(tv)) {
4396 /* have at least one 0 at the same bit position */
4400 n_tv4 = tarval_not(tv4);
4401 if (tv3 != tarval_and(tv3, n_tv4)) {
4402 /* bit in the or_mask is outside the and_mask */
4406 n_tv2 = tarval_not(tv2);
4407 if (tv1 != tarval_and(tv1, n_tv2)) {
4408 /* bit in the or_mask is outside the and_mask */
4412 /* ok, all conditions met */
4413 block = get_irn_n(or, -1);
4415 new_and = new_r_And(current_ir_graph, block,
4416 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4418 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4420 set_Or_left(or, new_and);
4421 set_Or_right(or, new_const);
4423 /* check for more */
4425 } /* transform_node_Or_bf_store */
4428 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4430 static ir_node *transform_node_Or_Rot(ir_node *or) {
4431 ir_mode *mode = get_irn_mode(or);
4432 ir_node *shl, *shr, *block;
4433 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4436 if (! mode_is_int(mode))
4439 shl = get_binop_left(or);
4440 shr = get_binop_right(or);
4449 } else if (!is_Shl(shl)) {
4451 } else if (!is_Shr(shr)) {
4454 x = get_Shl_left(shl);
4455 if (x != get_Shr_left(shr))
4458 c1 = get_Shl_right(shl);
4459 c2 = get_Shr_right(shr);
4460 if (is_Const(c1) && is_Const(c2)) {
4461 tv1 = get_Const_tarval(c1);
4462 if (! tarval_is_long(tv1))
4465 tv2 = get_Const_tarval(c2);
4466 if (! tarval_is_long(tv2))
4469 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4470 != (int) get_mode_size_bits(mode))
4473 /* yet, condition met */
4474 block = get_irn_n(or, -1);
4476 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4478 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4480 } else if (is_Sub(c1)) {
4484 if (get_Sub_right(sub) != v)
4487 c1 = get_Sub_left(sub);
4491 tv1 = get_Const_tarval(c1);
4492 if (! tarval_is_long(tv1))
4495 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4498 /* yet, condition met */
4499 block = get_nodes_block(or);
4501 /* a Rot right is not supported, so use a rot left */
4502 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4504 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4506 } else if (is_Sub(c2)) {
4510 c1 = get_Sub_left(sub);
4514 tv1 = get_Const_tarval(c1);
4515 if (! tarval_is_long(tv1))
4518 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4521 /* yet, condition met */
4522 block = get_irn_n(or, -1);
4525 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4527 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4532 } /* transform_node_Or_Rot */
4537 static ir_node *transform_node_Or(ir_node *n) {
4538 ir_node *c, *oldn = n;
4539 ir_node *a = get_Or_left(n);
4540 ir_node *b = get_Or_right(n);
4543 if (is_Not(a) && is_Not(b)) {
4544 /* ~a | ~b = ~(a&b) */
4545 ir_node *block = get_nodes_block(n);
4547 mode = get_irn_mode(n);
4550 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4551 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4552 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4556 /* we can evaluate 2 Projs of the same Cmp */
4557 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4558 ir_node *pred_a = get_Proj_pred(a);
4559 ir_node *pred_b = get_Proj_pred(b);
4560 if (pred_a == pred_b) {
4561 dbg_info *dbgi = get_irn_dbg_info(n);
4562 ir_node *block = get_nodes_block(pred_a);
4563 pn_Cmp pn_a = get_Proj_proj(a);
4564 pn_Cmp pn_b = get_Proj_proj(b);
4565 /* yes, we can simply calculate with pncs */
4566 pn_Cmp new_pnc = pn_a | pn_b;
4568 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4573 mode = get_irn_mode(n);
4574 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4576 n = transform_node_Or_bf_store(n);
4577 n = transform_node_Or_Rot(n);
4581 n = transform_bitwise_distributive(n, transform_node_Or);
4584 } /* transform_node_Or */
4588 static ir_node *transform_node(ir_node *n);
4591 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4593 * Should be moved to reassociation?
4595 static ir_node *transform_node_shift(ir_node *n) {
4596 ir_node *left, *right;
4597 tarval *tv1, *tv2, *res;
4599 int modulo_shf, flag;
4601 left = get_binop_left(n);
4603 /* different operations */
4604 if (get_irn_op(left) != get_irn_op(n))
4607 right = get_binop_right(n);
4608 tv1 = value_of(right);
4609 if (tv1 == tarval_bad)
4612 tv2 = value_of(get_binop_right(left));
4613 if (tv2 == tarval_bad)
4616 res = tarval_add(tv1, tv2);
4618 /* beware: a simple replacement works only, if res < modulo shift */
4619 mode = get_irn_mode(n);
4623 modulo_shf = get_mode_modulo_shift(mode);
4624 if (modulo_shf > 0) {
4625 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4627 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4633 /* ok, we can replace it */
4634 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4636 in[0] = get_binop_left(left);
4637 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4639 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4641 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4643 return transform_node(irn);
4646 } /* transform_node_shift */
4651 static ir_node *transform_node_Shr(ir_node *n) {
4652 ir_node *c, *oldn = n;
4653 ir_node *a = get_Shr_left(n);
4654 ir_node *b = get_Shr_right(n);
4655 ir_mode *mode = get_irn_mode(n);
4657 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4658 return transform_node_shift(n);
4659 } /* transform_node_Shr */
4664 static ir_node *transform_node_Shrs(ir_node *n) {
4665 ir_node *c, *oldn = n;
4666 ir_node *a = get_Shrs_left(n);
4667 ir_node *b = get_Shrs_right(n);
4668 ir_mode *mode = get_irn_mode(n);
4670 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4671 return transform_node_shift(n);
4672 } /* transform_node_Shrs */
4677 static ir_node *transform_node_Shl(ir_node *n) {
4678 ir_node *c, *oldn = n;
4679 ir_node *a = get_Shl_left(n);
4680 ir_node *b = get_Shl_right(n);
4681 ir_mode *mode = get_irn_mode(n);
4683 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4684 return transform_node_shift(n);
4685 } /* transform_node_Shl */
4690 static ir_node *transform_node_Rot(ir_node *n) {
4691 ir_node *c, *oldn = n;
4692 ir_node *a = get_Rot_left(n);
4693 ir_node *b = get_Rot_right(n);
4694 ir_mode *mode = get_irn_mode(n);
4696 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4697 return transform_node_shift(n);
4698 } /* transform_node_Rot */
4703 static ir_node *transform_node_Conv(ir_node *n) {
4704 ir_node *c, *oldn = n;
4705 ir_node *a = get_Conv_op(n);
4707 if (is_const_Phi(a)) {
4708 c = apply_conv_on_phi(a, get_irn_mode(n));
4710 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4715 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4716 ir_mode *mode = get_irn_mode(n);
4717 return new_r_Unknown(current_ir_graph, mode);
4721 } /* transform_node_Conv */
4724 * Remove dead blocks and nodes in dead blocks
4725 * in keep alive list. We do not generate a new End node.
4727 static ir_node *transform_node_End(ir_node *n) {
4728 int i, j, n_keepalives = get_End_n_keepalives(n);
4731 NEW_ARR_A(ir_node *, in, n_keepalives);
4733 for (i = j = 0; i < n_keepalives; ++i) {
4734 ir_node *ka = get_End_keepalive(n, i);
4736 if (! is_Block_dead(ka)) {
4740 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4743 /* FIXME: beabi need to keep a Proj(M) */
4744 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4747 if (j != n_keepalives)
4748 set_End_keepalives(n, j, in);
4750 } /* transform_node_End */
4752 /** returns 1 if a == -b */
4753 static int is_negated_value(ir_node *a, ir_node *b) {
4754 if (is_Minus(a) && get_Minus_op(a) == b)
4756 if (is_Minus(b) && get_Minus_op(b) == a)
4758 if (is_Sub(a) && is_Sub(b)) {
4759 ir_node *a_left = get_Sub_left(a);
4760 ir_node *a_right = get_Sub_right(a);
4761 ir_node *b_left = get_Sub_left(b);
4762 ir_node *b_right = get_Sub_right(b);
4764 if (a_left == b_right && a_right == b_left)
4772 * Optimize a Mux into some simpler cases.
4774 static ir_node *transform_node_Mux(ir_node *n) {
4775 ir_node *oldn = n, *sel = get_Mux_sel(n);
4776 ir_mode *mode = get_irn_mode(n);
4778 if (mode == mode_b) {
4779 ir_node *t = get_Mux_true(n);
4780 ir_node *f = get_Mux_false(n);
4781 dbg_info *dbg = get_irn_dbg_info(n);
4782 ir_node *block = get_irn_n(n, -1);
4783 ir_graph *irg = current_ir_graph;
4786 tarval *tv_t = get_Const_tarval(t);
4787 if (tv_t == tarval_b_true) {
4789 /* Muxb(sel, true, false) = sel */
4790 assert(get_Const_tarval(f) == tarval_b_false);
4791 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4794 /* Muxb(sel, true, x) = Or(sel, x) */
4795 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4796 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4800 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4801 assert(tv_t == tarval_b_false);
4803 /* Muxb(sel, false, true) = Not(sel) */
4804 assert(get_Const_tarval(f) == tarval_b_true);
4805 DBG_OPT_ALGSIM0(oldn, not_sel, FS_OPT_MUX_NOT_BOOL);
4808 /* Muxb(sel, false, x) = And(Not(sel), x) */
4809 n = new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4810 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ANDNOT_BOOL);
4814 } else if (is_Const(f)) {
4815 tarval *tv_f = get_Const_tarval(f);
4816 if (tv_f == tarval_b_true) {
4817 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4818 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4819 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4820 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4823 /* Muxb(sel, x, false) = And(sel, x) */
4824 assert(tv_f == tarval_b_false);
4825 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4826 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4832 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4833 ir_node *cmp = get_Proj_pred(sel);
4834 long pn = get_Proj_proj(sel);
4835 ir_node *f = get_Mux_false(n);
4836 ir_node *t = get_Mux_true(n);
4839 * Note: normalization puts the constant on the right side,
4840 * so we check only one case.
4842 * Note further that these optimization work even for floating point
4843 * with NaN's because -NaN == NaN.
4844 * However, if +0 and -0 is handled differently, we cannot use the first
4848 ir_node *cmp_r = get_Cmp_right(cmp);
4849 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4850 ir_node *block = get_irn_n(n, -1);
4852 if (is_negated_value(f, t)) {
4853 ir_node *cmp_left = get_Cmp_left(cmp);
4855 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4856 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4857 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4859 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4861 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4863 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4864 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4865 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4867 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4869 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4871 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4878 return arch_transform_node_Mux(n);
4879 } /* transform_node_Mux */
4882 * Optimize a Psi into some simpler cases.
4884 static ir_node *transform_node_Psi(ir_node *n) {
4886 return transform_node_Mux(n);
4889 } /* transform_node_Psi */
4892 * optimize sync nodes that have other syncs as input we simply add the inputs
4893 * of the other sync to our own inputs
4895 static ir_node *transform_node_Sync(ir_node *n) {
4896 int arity = get_Sync_n_preds(n);
4899 for (i = 0; i < arity;) {
4900 ir_node *pred = get_Sync_pred(n, i);
4904 if (!is_Sync(pred)) {
4912 pred_arity = get_Sync_n_preds(pred);
4913 for (j = 0; j < pred_arity; ++j) {
4914 ir_node *pred_pred = get_Sync_pred(pred, j);
4919 add_irn_n(n, pred_pred);
4923 if (get_Sync_pred(n, k) == pred_pred) break;
4928 /* rehash the sync node */
4929 add_identities(current_ir_graph->value_table, n);
4935 * Tries several [inplace] [optimizing] transformations and returns an
4936 * equivalent node. The difference to equivalent_node() is that these
4937 * transformations _do_ generate new nodes, and thus the old node must
4938 * not be freed even if the equivalent node isn't the old one.
4940 static ir_node *transform_node(ir_node *n) {
4944 * Transform_node is the only "optimizing transformation" that might
4945 * return a node with a different opcode. We iterate HERE until fixpoint
4946 * to get the final result.
4950 if (n->op->ops.transform_node)
4951 n = n->op->ops.transform_node(n);
4952 } while (oldn != n);
4955 } /* transform_node */
4958 * Sets the default transform node operation for an ir_op_ops.
4960 * @param code the opcode for the default operation
4961 * @param ops the operations initialized
4966 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4970 ops->transform_node = transform_node_##a; \
5007 } /* firm_set_default_transform_node */
5010 /* **************** Common Subexpression Elimination **************** */
5012 /** The size of the hash table used, should estimate the number of nodes
5014 #define N_IR_NODES 512
5016 /** Compares the attributes of two Const nodes. */
5017 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5018 return (get_Const_tarval(a) != get_Const_tarval(b))
5019 || (get_Const_type(a) != get_Const_type(b));
5020 } /* node_cmp_attr_Const */
5022 /** Compares the attributes of two Proj nodes. */
5023 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5024 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5025 } /* node_cmp_attr_Proj */
5027 /** Compares the attributes of two Filter nodes. */
5028 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5029 return get_Filter_proj(a) != get_Filter_proj(b);
5030 } /* node_cmp_attr_Filter */
5032 /** Compares the attributes of two Alloc nodes. */
5033 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5034 const alloc_attr *pa = get_irn_alloc_attr(a);
5035 const alloc_attr *pb = get_irn_alloc_attr(b);
5036 return (pa->where != pb->where) || (pa->type != pb->type);
5037 } /* node_cmp_attr_Alloc */
5039 /** Compares the attributes of two Free nodes. */
5040 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5041 const free_attr *pa = get_irn_free_attr(a);
5042 const free_attr *pb = get_irn_free_attr(b);
5043 return (pa->where != pb->where) || (pa->type != pb->type);
5044 } /* node_cmp_attr_Free */
5046 /** Compares the attributes of two SymConst nodes. */
5047 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5048 const symconst_attr *pa = get_irn_symconst_attr(a);
5049 const symconst_attr *pb = get_irn_symconst_attr(b);
5050 return (pa->kind != pb->kind)
5051 || (pa->sym.type_p != pb->sym.type_p)
5052 || (pa->tp != pb->tp);
5053 } /* node_cmp_attr_SymConst */
5055 /** Compares the attributes of two Call nodes. */
5056 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5057 return get_irn_call_attr(a) != get_irn_call_attr(b);
5058 } /* node_cmp_attr_Call */
5060 /** Compares the attributes of two Sel nodes. */
5061 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5062 const ir_entity *a_ent = get_Sel_entity(a);
5063 const ir_entity *b_ent = get_Sel_entity(b);
5065 (a_ent->kind != b_ent->kind) ||
5066 (a_ent->name != b_ent->name) ||
5067 (a_ent->owner != b_ent->owner) ||
5068 (a_ent->ld_name != b_ent->ld_name) ||
5069 (a_ent->type != b_ent->type);
5070 } /* node_cmp_attr_Sel */
5072 /** Compares the attributes of two Phi nodes. */
5073 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5074 /* we can only enter this function if both nodes have the same number of inputs,
5075 hence it is enough to check if one of them is a Phi0 */
5077 /* check the Phi0 pos attribute */
5078 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5081 } /* node_cmp_attr_Phi */
5083 /** Compares the attributes of two Conv nodes. */
5084 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5085 return get_Conv_strict(a) != get_Conv_strict(b);
5086 } /* node_cmp_attr_Conv */
5088 /** Compares the attributes of two Cast nodes. */
5089 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5090 return get_Cast_type(a) != get_Cast_type(b);
5091 } /* node_cmp_attr_Cast */
5093 /** Compares the attributes of two Load nodes. */
5094 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5095 if (get_Load_volatility(a) == volatility_is_volatile ||
5096 get_Load_volatility(b) == volatility_is_volatile)
5097 /* NEVER do CSE on volatile Loads */
5099 /* do not CSE Loads with different alignment. Be conservative. */
5100 if (get_Load_align(a) != get_Load_align(b))
5103 return get_Load_mode(a) != get_Load_mode(b);
5104 } /* node_cmp_attr_Load */
5106 /** Compares the attributes of two Store nodes. */
5107 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5108 /* do not CSE Stores with different alignment. Be conservative. */
5109 if (get_Store_align(a) != get_Store_align(b))
5112 /* NEVER do CSE on volatile Stores */
5113 return (get_Store_volatility(a) == volatility_is_volatile ||
5114 get_Store_volatility(b) == volatility_is_volatile);
5115 } /* node_cmp_attr_Store */
5117 /** Compares two exception attributes */
5118 static int node_cmp_exception(ir_node *a, ir_node *b) {
5119 const except_attr *ea = get_irn_except_attr(a);
5120 const except_attr *eb = get_irn_except_attr(b);
5122 return ea->pin_state != eb->pin_state;
5125 #define node_cmp_attr_Bound node_cmp_exception
5127 /** Compares the attributes of two Div nodes. */
5128 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5129 const divmod_attr *ma = get_irn_divmod_attr(a);
5130 const divmod_attr *mb = get_irn_divmod_attr(b);
5131 return ma->exc.pin_state != mb->exc.pin_state ||
5132 ma->res_mode != mb->res_mode ||
5133 ma->no_remainder != mb->no_remainder;
5134 } /* node_cmp_attr_Div */
5136 /** Compares the attributes of two DivMod nodes. */
5137 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5138 const divmod_attr *ma = get_irn_divmod_attr(a);
5139 const divmod_attr *mb = get_irn_divmod_attr(b);
5140 return ma->exc.pin_state != mb->exc.pin_state ||
5141 ma->res_mode != mb->res_mode;
5142 } /* node_cmp_attr_DivMod */
5144 /** Compares the attributes of two Mod nodes. */
5145 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5146 const divmod_attr *ma = get_irn_divmod_attr(a);
5147 const divmod_attr *mb = get_irn_divmod_attr(b);
5148 return ma->exc.pin_state != mb->exc.pin_state ||
5149 ma->res_mode != mb->res_mode;
5150 } /* node_cmp_attr_Mod */
5152 /** Compares the attributes of two Quot nodes. */
5153 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5154 const divmod_attr *ma = get_irn_divmod_attr(a);
5155 const divmod_attr *mb = get_irn_divmod_attr(b);
5156 return ma->exc.pin_state != mb->exc.pin_state ||
5157 ma->res_mode != mb->res_mode;
5158 } /* node_cmp_attr_Quot */
5160 /** Compares the attributes of two Confirm nodes. */
5161 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5162 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5163 } /* node_cmp_attr_Confirm */
5165 /** Compares the attributes of two ASM nodes. */
5166 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5168 const ir_asm_constraint *ca;
5169 const ir_asm_constraint *cb;
5172 if (get_ASM_text(a) != get_ASM_text(b))
5175 /* Should we really check the constraints here? Should be better, but is strange. */
5176 n = get_ASM_n_input_constraints(a);
5177 if (n != get_ASM_n_input_constraints(b))
5180 ca = get_ASM_input_constraints(a);
5181 cb = get_ASM_input_constraints(b);
5182 for (i = 0; i < n; ++i) {
5183 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5187 n = get_ASM_n_output_constraints(a);
5188 if (n != get_ASM_n_output_constraints(b))
5191 ca = get_ASM_output_constraints(a);
5192 cb = get_ASM_output_constraints(b);
5193 for (i = 0; i < n; ++i) {
5194 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5198 n = get_ASM_n_clobbers(a);
5199 if (n != get_ASM_n_clobbers(b))
5202 cla = get_ASM_clobbers(a);
5203 clb = get_ASM_clobbers(b);
5204 for (i = 0; i < n; ++i) {
5205 if (cla[i] != clb[i])
5209 } /* node_cmp_attr_ASM */
5212 * Set the default node attribute compare operation for an ir_op_ops.
5214 * @param code the opcode for the default operation
5215 * @param ops the operations initialized
5220 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5224 ops->node_cmp_attr = node_cmp_attr_##a; \
5255 } /* firm_set_default_node_cmp_attr */
5258 * Compare function for two nodes in the value table. Gets two
5259 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5261 int identities_cmp(const void *elt, const void *key) {
5262 ir_node *a = (ir_node *)elt;
5263 ir_node *b = (ir_node *)key;
5266 if (a == b) return 0;
5268 if ((get_irn_op(a) != get_irn_op(b)) ||
5269 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5271 /* compare if a's in and b's in are of equal length */
5272 irn_arity_a = get_irn_intra_arity(a);
5273 if (irn_arity_a != get_irn_intra_arity(b))
5276 if (get_irn_pinned(a) == op_pin_state_pinned) {
5277 /* for pinned nodes, the block inputs must be equal */
5278 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5280 } else if (! get_opt_global_cse()) {
5281 /* for block-local CSE both nodes must be in the same MacroBlock */
5282 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5286 /* compare a->in[0..ins] with b->in[0..ins] */
5287 for (i = 0; i < irn_arity_a; i++)
5288 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5292 * here, we already now that the nodes are identical except their
5295 if (a->op->ops.node_cmp_attr)
5296 return a->op->ops.node_cmp_attr(a, b);
5299 } /* identities_cmp */
5302 * Calculate a hash value of a node.
5304 unsigned ir_node_hash(ir_node *node) {
5308 if (node->op == op_Const) {
5309 /* special value for const, as they only differ in their tarval. */
5310 h = HASH_PTR(node->attr.con.tv);
5311 h = 9*h + HASH_PTR(get_irn_mode(node));
5312 } else if (node->op == op_SymConst) {
5313 /* special value for const, as they only differ in their symbol. */
5314 h = HASH_PTR(node->attr.symc.sym.type_p);
5315 h = 9*h + HASH_PTR(get_irn_mode(node));
5318 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5319 h = irn_arity = get_irn_intra_arity(node);
5321 /* consider all in nodes... except the block if not a control flow. */
5322 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5323 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5327 h = 9*h + HASH_PTR(get_irn_mode(node));
5329 h = 9*h + HASH_PTR(get_irn_op(node));
5333 } /* ir_node_hash */
5335 pset *new_identities(void) {
5336 return new_pset(identities_cmp, N_IR_NODES);
5337 } /* new_identities */
5339 void del_identities(pset *value_table) {
5340 del_pset(value_table);
5341 } /* del_identities */
5344 * Normalize a node by putting constants (and operands with larger
5345 * node index) on the right (operator side).
5347 * @param n The node to normalize
5349 static void normalize_node(ir_node *n) {
5350 if (is_op_commutative(get_irn_op(n))) {
5351 ir_node *l = get_binop_left(n);
5352 ir_node *r = get_binop_right(n);
5354 /* For commutative operators perform a OP b == b OP a but keep
5355 * constants on the RIGHT side. This helps greatly in some
5356 * optimizations. Moreover we use the idx number to make the form
5358 if (!operands_are_normalized(l, r)) {
5359 set_binop_left(n, r);
5360 set_binop_right(n, l);
5363 } /* normalize_node */
5366 * Update the nodes after a match in the value table. If both nodes have
5367 * the same MacroBlock but different Blocks, we must ensure that the node
5368 * with the dominating Block (the node that is near to the MacroBlock header
5369 * is stored in the table.
5370 * Because a MacroBlock has only one "non-exception" flow, we don't need
5371 * dominance info here: We known, that one block must dominate the other and
5372 * following the only block input will allow to find it.
5374 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5375 ir_node *known_blk, *new_block, *block, *mbh;
5377 if (get_opt_global_cse()) {
5378 /* Block inputs are meaning less */
5381 known_blk = get_irn_n(known_irn, -1);
5382 new_block = get_irn_n(new_ir_node, -1);
5383 if (known_blk == new_block) {
5384 /* already in the same block */
5388 * We expect the typical case when we built the graph. In that case, the
5389 * known_irn is already the upper one, so checking this should be faster.
5392 mbh = get_Block_MacroBlock(new_block);
5394 if (block == known_blk) {
5395 /* ok, we have found it: known_block dominates new_block as expected */
5400 * We have reached the MacroBlock header NOT founding
5401 * the known_block. new_block must dominate known_block.
5404 set_irn_n(known_irn, -1, new_block);
5407 assert(get_Block_n_cfgpreds(block) == 1);
5408 block = get_Block_cfgpred_block(block, 0);
5410 } /* update_value_table */
5413 * Return the canonical node computing the same value as n.
5415 * @param value_table The value table
5416 * @param n The node to lookup
5418 * Looks up the node in a hash table.
5420 * For Const nodes this is performed in the constructor, too. Const
5421 * nodes are extremely time critical because of their frequent use in
5422 * constant string arrays.
5424 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5427 if (!value_table) return n;
5431 o = pset_find(value_table, n, ir_node_hash(n));
5435 update_known_irn(o, n);
5442 * During construction we set the op_pin_state_pinned flag in the graph right when the
5443 * optimization is performed. The flag turning on procedure global cse could
5444 * be changed between two allocations. This way we are safe.
5446 * @param value_table The value table
5447 * @param n The node to lookup
5449 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5452 n = identify(value_table, n);
5453 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5454 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5456 } /* identify_cons */
5459 * Return the canonical node computing the same value as n.
5460 * Looks up the node in a hash table, enters it in the table
5461 * if it isn't there yet.
5463 * @param value_table the HashSet containing all nodes in the
5465 * @param n the node to look up
5467 * @return a node that computes the same value as n or n if no such
5468 * node could be found
5470 ir_node *identify_remember(pset *value_table, ir_node *n) {
5473 if (!value_table) return n;
5476 /* lookup or insert in hash table with given hash key. */
5477 o = pset_insert(value_table, n, ir_node_hash(n));
5480 update_known_irn(o, n);
5485 } /* identify_remember */
5487 /* Add a node to the identities value table. */
5488 void add_identities(pset *value_table, ir_node *node) {
5489 if (get_opt_cse() && is_no_Block(node))
5490 identify_remember(value_table, node);
5491 } /* add_identities */
5493 /* Visit each node in the value table of a graph. */
5494 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5496 ir_graph *rem = current_ir_graph;
5498 current_ir_graph = irg;
5499 foreach_pset(irg->value_table, node)
5501 current_ir_graph = rem;
5502 } /* visit_all_identities */
5505 * Garbage in, garbage out. If a node has a dead input, i.e., the
5506 * Bad node is input to the node, return the Bad node.
5508 static ir_node *gigo(ir_node *node) {
5510 ir_op *op = get_irn_op(node);
5512 /* remove garbage blocks by looking at control flow that leaves the block
5513 and replacing the control flow by Bad. */
5514 if (get_irn_mode(node) == mode_X) {
5515 ir_node *block = get_nodes_block(skip_Proj(node));
5517 /* Don't optimize nodes in immature blocks. */
5518 if (!get_Block_matured(block))
5520 /* Don't optimize End, may have Bads. */
5521 if (op == op_End) return node;
5523 if (is_Block(block)) {
5524 if (is_Block_dead(block)) {
5525 /* control flow from dead block is dead */
5529 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5530 if (!is_Bad(get_irn_n(block, i)))
5534 ir_graph *irg = get_irn_irg(block);
5535 /* the start block is never dead */
5536 if (block != get_irg_start_block(irg)
5537 && block != get_irg_end_block(irg)) {
5539 * Do NOT kill control flow without setting
5540 * the block to dead of bad things can happen:
5541 * We get a Block that is not reachable be irg_block_walk()
5542 * but can be found by irg_walk()!
5544 set_Block_dead(block);
5551 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5552 blocks predecessors is dead. */
5553 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5554 irn_arity = get_irn_arity(node);
5557 * Beware: we can only read the block of a non-floating node.
5559 if (is_irn_pinned_in_irg(node) &&
5560 is_Block_dead(get_nodes_block(skip_Proj(node))))
5563 for (i = 0; i < irn_arity; i++) {
5564 ir_node *pred = get_irn_n(node, i);
5569 /* Propagating Unknowns here seems to be a bad idea, because
5570 sometimes we need a node as a input and did not want that
5572 However, it might be useful to move this into a later phase
5573 (if you think that optimizing such code is useful). */
5574 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5575 return new_Unknown(get_irn_mode(node));
5580 /* With this code we violate the agreement that local_optimize
5581 only leaves Bads in Block, Phi and Tuple nodes. */
5582 /* If Block has only Bads as predecessors it's garbage. */
5583 /* If Phi has only Bads as predecessors it's garbage. */
5584 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5585 irn_arity = get_irn_arity(node);
5586 for (i = 0; i < irn_arity; i++) {
5587 if (!is_Bad(get_irn_n(node, i))) break;
5589 if (i == irn_arity) node = new_Bad();
5596 * These optimizations deallocate nodes from the obstack.
5597 * It can only be called if it is guaranteed that no other nodes
5598 * reference this one, i.e., right after construction of a node.
5600 * @param n The node to optimize
5602 * current_ir_graph must be set to the graph of the node!
5604 ir_node *optimize_node(ir_node *n) {
5607 ir_opcode iro = get_irn_opcode(n);
5609 /* Always optimize Phi nodes: part of the construction. */
5610 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5612 /* constant expression evaluation / constant folding */
5613 if (get_opt_constant_folding()) {
5614 /* neither constants nor Tuple values can be evaluated */
5615 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5616 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5617 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5618 /* try to evaluate */
5619 tv = computed_value(n);
5620 if (tv != tarval_bad) {
5622 ir_type *old_tp = get_irn_type(n);
5623 int i, arity = get_irn_arity(n);
5627 * Try to recover the type of the new expression.
5629 for (i = 0; i < arity && !old_tp; ++i)
5630 old_tp = get_irn_type(get_irn_n(n, i));
5633 * we MUST copy the node here temporary, because it's still needed
5634 * for DBG_OPT_CSTEVAL
5636 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5637 oldn = alloca(node_size);
5639 memcpy(oldn, n, node_size);
5640 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5642 /* ARG, copy the in array, we need it for statistics */
5643 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5645 /* note the inplace edges module */
5646 edges_node_deleted(n, current_ir_graph);
5648 /* evaluation was successful -- replace the node. */
5649 irg_kill_node(current_ir_graph, n);
5650 nw = new_Const(get_tarval_mode(tv), tv);
5652 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5653 set_Const_type(nw, old_tp);
5654 DBG_OPT_CSTEVAL(oldn, nw);
5655 tarval_enable_fp_ops(old_fp_mode);
5658 tarval_enable_fp_ops(old_fp_mode);
5662 /* remove unnecessary nodes */
5663 if (get_opt_constant_folding() ||
5664 (iro == iro_Phi) || /* always optimize these nodes. */
5666 (iro == iro_Proj) ||
5667 (iro == iro_Block) ) /* Flags tested local. */
5668 n = equivalent_node(n);
5670 /* Common Subexpression Elimination.
5672 * Checks whether n is already available.
5673 * The block input is used to distinguish different subexpressions. Right
5674 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5675 * subexpressions within a block.
5678 n = identify_cons(current_ir_graph->value_table, n);
5681 edges_node_deleted(oldn, current_ir_graph);
5683 /* We found an existing, better node, so we can deallocate the old node. */
5684 irg_kill_node(current_ir_graph, oldn);
5688 /* Some more constant expression evaluation that does not allow to
5690 iro = get_irn_opcode(n);
5691 if (get_opt_constant_folding() ||
5692 (iro == iro_Cond) ||
5693 (iro == iro_Proj)) /* Flags tested local. */
5694 n = transform_node(n);
5696 /* Remove nodes with dead (Bad) input.
5697 Run always for transformation induced Bads. */
5700 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5701 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5702 n = identify_remember(current_ir_graph->value_table, n);
5706 } /* optimize_node */
5710 * These optimizations never deallocate nodes (in place). This can cause dead
5711 * nodes lying on the obstack. Remove these by a dead node elimination,
5712 * i.e., a copying garbage collection.
5714 ir_node *optimize_in_place_2(ir_node *n) {
5717 ir_opcode iro = get_irn_opcode(n);
5719 if (!get_opt_optimize() && !is_Phi(n)) return n;
5721 /* constant expression evaluation / constant folding */
5722 if (get_opt_constant_folding()) {
5723 /* neither constants nor Tuple values can be evaluated */
5724 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5725 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5726 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5727 /* try to evaluate */
5728 tv = computed_value(n);
5729 if (tv != tarval_bad) {
5730 /* evaluation was successful -- replace the node. */
5731 ir_type *old_tp = get_irn_type(n);
5732 int i, arity = get_irn_arity(n);
5735 * Try to recover the type of the new expression.
5737 for (i = 0; i < arity && !old_tp; ++i)
5738 old_tp = get_irn_type(get_irn_n(n, i));
5740 n = new_Const(get_tarval_mode(tv), tv);
5742 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5743 set_Const_type(n, old_tp);
5745 DBG_OPT_CSTEVAL(oldn, n);
5746 tarval_enable_fp_ops(old_fp_mode);
5749 tarval_enable_fp_ops(old_fp_mode);
5753 /* remove unnecessary nodes */
5754 if (get_opt_constant_folding() ||
5755 (iro == iro_Phi) || /* always optimize these nodes. */
5756 (iro == iro_Id) || /* ... */
5757 (iro == iro_Proj) || /* ... */
5758 (iro == iro_Block) ) /* Flags tested local. */
5759 n = equivalent_node(n);
5761 /** common subexpression elimination **/
5762 /* Checks whether n is already available. */
5763 /* The block input is used to distinguish different subexpressions. Right
5764 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5765 subexpressions within a block. */
5766 if (get_opt_cse()) {
5767 n = identify(current_ir_graph->value_table, n);
5770 /* Some more constant expression evaluation. */
5771 iro = get_irn_opcode(n);
5772 if (get_opt_constant_folding() ||
5773 (iro == iro_Cond) ||
5774 (iro == iro_Proj)) /* Flags tested local. */
5775 n = transform_node(n);
5777 /* Remove nodes with dead (Bad) input.
5778 Run always for transformation induced Bads. */
5781 /* Now we can verify the node, as it has no dead inputs any more. */
5784 /* Now we have a legal, useful node. Enter it in hash table for cse.
5785 Blocks should be unique anyways. (Except the successor of start:
5786 is cse with the start block!) */
5787 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5788 n = identify_remember(current_ir_graph->value_table, n);
5791 } /* optimize_in_place_2 */
5794 * Wrapper for external use, set proper status bits after optimization.
5796 ir_node *optimize_in_place(ir_node *n) {
5797 /* Handle graph state */
5798 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5800 if (get_opt_global_cse())
5801 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5802 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5803 set_irg_outs_inconsistent(current_ir_graph);
5805 /* FIXME: Maybe we could also test whether optimizing the node can
5806 change the control graph. */
5807 set_irg_doms_inconsistent(current_ir_graph);
5808 return optimize_in_place_2(n);
5809 } /* optimize_in_place */
5812 * Sets the default operation for an ir_ops.
5814 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5815 ops = firm_set_default_computed_value(code, ops);
5816 ops = firm_set_default_equivalent_node(code, ops);
5817 ops = firm_set_default_transform_node(code, ops);
5818 ops = firm_set_default_node_cmp_attr(code, ops);
5819 ops = firm_set_default_get_type(code, ops);
5820 ops = firm_set_default_get_type_attr(code, ops);
5821 ops = firm_set_default_get_entity_attr(code, ops);
5824 } /* firm_set_default_operations */