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 static ir_node *equivalent_node_Eor(ir_node *n)
877 n = equivalent_node_neutral_zero(n);
878 if (n != oldn) return n;
881 b = get_Eor_right(n);
884 ir_node *aa = get_Eor_left(a);
885 ir_node *ab = get_Eor_right(a);
888 /* (a ^ b) ^ a -> b */
890 } else if (ab == b) {
891 /* (a ^ b) ^ b -> a */
894 } else if (is_Eor(b)) {
895 ir_node *ba = get_Eor_left(b);
896 ir_node *bb = get_Eor_right(b);
899 /* a ^ (a ^ b) -> b */
901 } else if (bb == a) {
902 /* a ^ (b ^ a) -> b */
911 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
913 * The second one looks strange, but this construct
914 * is used heavily in the LCC sources :-).
916 * Beware: The Mode of an Add may be different than the mode of its
917 * predecessors, so we could not return a predecessors in all cases.
919 static ir_node *equivalent_node_Add(ir_node *n) {
921 ir_node *left, *right;
922 ir_mode *mode = get_irn_mode(n);
924 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
925 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
928 n = equivalent_node_neutral_zero(n);
932 left = get_Add_left(n);
933 right = get_Add_right(n);
936 if (get_Sub_right(left) == right) {
939 n = get_Sub_left(left);
940 if (mode == get_irn_mode(n)) {
941 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
947 if (get_Sub_right(right) == left) {
950 n = get_Sub_left(right);
951 if (mode == get_irn_mode(n)) {
952 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
958 } /* equivalent_node_Add */
961 * optimize operations that are not commutative but have neutral 0 on left,
964 static ir_node *equivalent_node_left_zero(ir_node *n) {
967 ir_node *a = get_binop_left(n);
968 ir_node *b = get_binop_right(n);
970 if (is_Const(b) && is_Const_null(b)) {
973 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
976 } /* equivalent_node_left_zero */
978 #define equivalent_node_Shl equivalent_node_left_zero
979 #define equivalent_node_Shr equivalent_node_left_zero
980 #define equivalent_node_Shrs equivalent_node_left_zero
981 #define equivalent_node_Rot equivalent_node_left_zero
984 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
986 * The second one looks strange, but this construct
987 * is used heavily in the LCC sources :-).
989 * Beware: The Mode of a Sub may be different than the mode of its
990 * predecessors, so we could not return a predecessors in all cases.
992 static ir_node *equivalent_node_Sub(ir_node *n) {
995 ir_mode *mode = get_irn_mode(n);
997 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
998 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1001 b = get_Sub_right(n);
1003 /* Beware: modes might be different */
1004 if (is_Const(b) && is_Const_null(b)) {
1005 ir_node *a = get_Sub_left(n);
1006 if (mode == get_irn_mode(a)) {
1009 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1013 } /* equivalent_node_Sub */
1017 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1020 * -(-a) == a, but might overflow two times.
1021 * We handle it anyway here but the better way would be a
1022 * flag. This would be needed for Pascal for instance.
1024 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1026 ir_node *pred = get_unop_op(n);
1028 /* optimize symmetric unop */
1029 if (get_irn_op(pred) == get_irn_op(n)) {
1030 n = get_unop_op(pred);
1031 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1034 } /* equivalent_node_idempotent_unop */
1036 /** Optimize Not(Not(x)) == x. */
1037 #define equivalent_node_Not equivalent_node_idempotent_unop
1039 /** -(-x) == x ??? Is this possible or can --x raise an
1040 out of bounds exception if min =! max? */
1041 #define equivalent_node_Minus equivalent_node_idempotent_unop
1044 * Optimize a * 1 = 1 * a = a.
1046 static ir_node *equivalent_node_Mul(ir_node *n) {
1048 ir_node *a = get_Mul_left(n);
1050 /* we can handle here only the n * n = n bit cases */
1051 if (get_irn_mode(n) == get_irn_mode(a)) {
1052 ir_node *b = get_Mul_right(n);
1054 /* Mul is commutative and has again an other neutral element. */
1055 if (is_Const(a) && is_Const_one(a)) {
1057 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1058 } else if (is_Const(b) && is_Const_one(b)) {
1060 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1064 } /* equivalent_node_Mul */
1067 * Optimize a / 1 = a.
1069 static ir_node *equivalent_node_Div(ir_node *n) {
1070 ir_node *a = get_Div_left(n);
1071 ir_node *b = get_Div_right(n);
1073 /* Div is not commutative. */
1074 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1075 /* Turn Div into a tuple (mem, bad, a) */
1076 ir_node *mem = get_Div_mem(n);
1077 ir_node *blk = get_irn_n(n, -1);
1078 turn_into_tuple(n, pn_Div_max);
1079 set_Tuple_pred(n, pn_Div_M, mem);
1080 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1081 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1082 set_Tuple_pred(n, pn_Div_res, a);
1085 } /* equivalent_node_Div */
1088 * Optimize a / 1.0 = a.
1090 static ir_node *equivalent_node_Quot(ir_node *n) {
1091 ir_node *a = get_Quot_left(n);
1092 ir_node *b = get_Quot_right(n);
1094 /* Div is not commutative. */
1095 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1096 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1097 ir_node *mem = get_Quot_mem(n);
1098 ir_node *blk = get_irn_n(n, -1);
1099 turn_into_tuple(n, pn_Quot_max);
1100 set_Tuple_pred(n, pn_Quot_M, mem);
1101 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1102 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1103 set_Tuple_pred(n, pn_Quot_res, a);
1106 } /* equivalent_node_Quot */
1109 * Optimize a / 1 = a.
1111 static ir_node *equivalent_node_DivMod(ir_node *n) {
1112 ir_node *b = get_DivMod_right(n);
1114 /* Div is not commutative. */
1115 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1116 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1117 ir_node *a = get_DivMod_left(n);
1118 ir_node *mem = get_Div_mem(n);
1119 ir_node *blk = get_irn_n(n, -1);
1120 ir_mode *mode = get_DivMod_resmode(n);
1122 turn_into_tuple(n, pn_DivMod_max);
1123 set_Tuple_pred(n, pn_DivMod_M, mem);
1124 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1125 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1126 set_Tuple_pred(n, pn_DivMod_res_div, a);
1127 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1130 } /* equivalent_node_DivMod */
1133 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1135 static ir_node *equivalent_node_Or(ir_node *n) {
1138 ir_node *a = get_Or_left(n);
1139 ir_node *b = get_Or_right(n);
1142 n = a; /* Or has it's own neutral element */
1143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1144 } else if (is_Const(a) && is_Const_null(a)) {
1146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1147 } else if (is_Const(b) && is_Const_null(b)) {
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1153 } /* equivalent_node_Or */
1156 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1158 static ir_node *equivalent_node_And(ir_node *n) {
1161 ir_node *a = get_And_left(n);
1162 ir_node *b = get_And_right(n);
1165 n = a; /* And has it's own neutral element */
1166 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1169 if (is_Const(a) && is_Const_all_one(a)) {
1171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1174 if (is_Const(b) && is_Const_all_one(b)) {
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1180 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1183 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1188 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1191 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1197 } /* equivalent_node_And */
1200 * Try to remove useless Conv's:
1202 static ir_node *equivalent_node_Conv(ir_node *n) {
1204 ir_node *a = get_Conv_op(n);
1206 ir_mode *n_mode = get_irn_mode(n);
1207 ir_mode *a_mode = get_irn_mode(a);
1209 if (n_mode == a_mode) { /* No Conv necessary */
1210 if (get_Conv_strict(n)) {
1211 /* special case: the predecessor might be a also a Conv */
1213 if (! get_Conv_strict(a)) {
1214 /* first one is not strict, kick it */
1215 set_Conv_op(n, get_Conv_op(a));
1218 /* else both are strict conv, second is superflous */
1219 } else if(is_Proj(a)) {
1220 ir_node *pred = get_Proj_pred(a);
1222 /* loads always return with the exact precision of n_mode */
1223 assert(get_Load_mode(pred) == n_mode);
1228 /* leave strict floating point Conv's */
1232 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1233 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1234 ir_node *b = get_Conv_op(a);
1235 ir_mode *b_mode = get_irn_mode(b);
1237 if (n_mode == b_mode) {
1238 if (n_mode == mode_b) {
1239 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1240 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1241 } else if (mode_is_int(n_mode)) {
1242 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1243 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1250 } /* equivalent_node_Conv */
1253 * A Cast may be removed if the type of the previous node
1254 * is already the type of the Cast.
1256 static ir_node *equivalent_node_Cast(ir_node *n) {
1258 ir_node *pred = get_Cast_op(n);
1260 if (get_irn_type(pred) == get_Cast_type(n)) {
1262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1265 } /* equivalent_node_Cast */
1268 * Several optimizations:
1269 * - no Phi in start block.
1270 * - remove Id operators that are inputs to Phi
1271 * - fold Phi-nodes, iff they have only one predecessor except
1274 static ir_node *equivalent_node_Phi(ir_node *n) {
1279 ir_node *first_val = NULL; /* to shutup gcc */
1281 if (!get_opt_normalize()) return n;
1283 n_preds = get_Phi_n_preds(n);
1285 block = get_nodes_block(n);
1286 if ((is_Block_dead(block)) || /* Control dead */
1287 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1288 return new_Bad(); /* in the Start Block. */
1290 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1292 /* If the Block has a Bad pred, we also have one. */
1293 for (i = 0; i < n_preds; ++i)
1294 if (is_Bad(get_Block_cfgpred(block, i)))
1295 set_Phi_pred(n, i, new_Bad());
1297 /* Find first non-self-referencing input */
1298 for (i = 0; i < n_preds; ++i) {
1299 first_val = get_Phi_pred(n, i);
1300 if ( (first_val != n) /* not self pointer */
1302 && (! is_Bad(first_val))
1304 ) { /* value not dead */
1305 break; /* then found first value. */
1310 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1314 /* search for rest of inputs, determine if any of these
1315 are non-self-referencing */
1316 while (++i < n_preds) {
1317 ir_node *scnd_val = get_Phi_pred(n, i);
1318 if ( (scnd_val != n)
1319 && (scnd_val != first_val)
1321 && (! is_Bad(scnd_val))
1329 /* Fold, if no multiple distinct non-self-referencing inputs */
1331 DBG_OPT_PHI(oldn, n);
1334 } /* equivalent_node_Phi */
1337 * Several optimizations:
1338 * - no Sync in start block.
1339 * - fold Sync-nodes, iff they have only one predecessor except
1342 static ir_node *equivalent_node_Sync(ir_node *n) {
1343 int arity = get_Sync_n_preds(n);
1346 for (i = 0; i < arity;) {
1347 ir_node *pred = get_Sync_pred(n, i);
1350 /* Remove Bad predecessors */
1357 /* Remove duplicate predecessors */
1363 if (get_Sync_pred(n, j) == pred) {
1371 if (arity == 0) return new_Bad();
1372 if (arity == 1) return get_Sync_pred(n, 0);
1374 } /* equivalent_node_Sync */
1377 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1378 * ProjX(Load) and ProjX(Store).
1380 static ir_node *equivalent_node_Proj(ir_node *proj) {
1381 ir_node *oldn = proj;
1382 ir_node *a = get_Proj_pred(proj);
1385 /* Remove the Tuple/Proj combination. */
1386 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1387 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1388 DBG_OPT_TUPLE(oldn, a, proj);
1390 /* This should not happen! */
1391 assert(! "found a Proj with higher number than Tuple predecessors");
1394 } else if (get_irn_mode(proj) == mode_X) {
1395 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1396 /* Remove dead control flow -- early gigo(). */
1398 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1400 /* get the Load address */
1401 ir_node *addr = get_Load_ptr(a);
1402 ir_node *blk = get_irn_n(a, -1);
1405 if (value_not_null(addr, &confirm)) {
1406 if (confirm == NULL) {
1407 /* this node may float if it did not depend on a Confirm */
1408 set_irn_pinned(a, op_pin_state_floats);
1410 if (get_Proj_proj(proj) == pn_Load_X_except) {
1411 DBG_OPT_EXC_REM(proj);
1414 return new_r_Jmp(current_ir_graph, blk);
1416 } else if (is_Store(a)) {
1417 /* get the load/store address */
1418 ir_node *addr = get_Store_ptr(a);
1419 ir_node *blk = get_irn_n(a, -1);
1422 if (value_not_null(addr, &confirm)) {
1423 if (confirm == NULL) {
1424 /* this node may float if it did not depend on a Confirm */
1425 set_irn_pinned(a, op_pin_state_floats);
1427 if (get_Proj_proj(proj) == pn_Store_X_except) {
1428 DBG_OPT_EXC_REM(proj);
1431 return new_r_Jmp(current_ir_graph, blk);
1438 } /* equivalent_node_Proj */
1443 static ir_node *equivalent_node_Id(ir_node *n) {
1448 } while (get_irn_op(n) == op_Id);
1450 DBG_OPT_ID(oldn, n);
1452 } /* equivalent_node_Id */
1457 static ir_node *equivalent_node_Mux(ir_node *n)
1459 ir_node *oldn = n, *sel = get_Mux_sel(n);
1460 tarval *ts = value_of(sel);
1462 /* Mux(true, f, t) == t */
1463 if (ts == tarval_b_true) {
1464 n = get_Mux_true(n);
1465 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1467 /* Mux(false, f, t) == f */
1468 else if (ts == tarval_b_false) {
1469 n = get_Mux_false(n);
1470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1472 /* Mux(v, x, x) == x */
1473 else if (get_Mux_false(n) == get_Mux_true(n)) {
1474 n = get_Mux_true(n);
1475 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1477 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1478 ir_node *cmp = get_Proj_pred(sel);
1479 long proj_nr = get_Proj_proj(sel);
1480 ir_node *f = get_Mux_false(n);
1481 ir_node *t = get_Mux_true(n);
1484 * Note further that these optimization work even for floating point
1485 * with NaN's because -NaN == NaN.
1486 * However, if +0 and -0 is handled differently, we cannot use the first one.
1489 ir_node *const cmp_l = get_Cmp_left(cmp);
1490 ir_node *const cmp_r = get_Cmp_right(cmp);
1494 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1495 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1497 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1504 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1505 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1507 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1514 * Note: normalization puts the constant on the right side,
1515 * so we check only one case.
1517 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1518 /* Mux(t CMP 0, X, t) */
1519 if (is_Minus(f) && get_Minus_op(f) == t) {
1520 /* Mux(t CMP 0, -t, t) */
1521 if (proj_nr == pn_Cmp_Eq) {
1522 /* Mux(t == 0, -t, t) ==> -t */
1524 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1525 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1526 /* Mux(t != 0, -t, t) ==> t */
1528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1535 } /* equivalent_node_Mux */
1538 * Returns a equivalent node of a Psi: if a condition is true
1539 * and all previous conditions are false we know its value.
1540 * If all conditions are false its value is the default one.
1542 static ir_node *equivalent_node_Psi(ir_node *n) {
1544 return equivalent_node_Mux(n);
1546 } /* equivalent_node_Psi */
1549 * Optimize -a CMP -b into b CMP a.
1550 * This works only for for modes where unary Minus
1552 * Note that two-complement integers can Overflow
1553 * so it will NOT work.
1555 * For == and != can be handled in Proj(Cmp)
1557 static ir_node *equivalent_node_Cmp(ir_node *n) {
1558 ir_node *left = get_Cmp_left(n);
1559 ir_node *right = get_Cmp_right(n);
1561 if (is_Minus(left) && is_Minus(right) &&
1562 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1563 left = get_Minus_op(left);
1564 right = get_Minus_op(right);
1565 set_Cmp_left(n, right);
1566 set_Cmp_right(n, left);
1569 } /* equivalent_node_Cmp */
1572 * Remove Confirm nodes if setting is on.
1573 * Replace Confirms(x, '=', Constlike) by Constlike.
1575 static ir_node *equivalent_node_Confirm(ir_node *n) {
1576 ir_node *pred = get_Confirm_value(n);
1577 pn_Cmp pnc = get_Confirm_cmp(n);
1579 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1581 * rare case: two identical Confirms one after another,
1582 * replace the second one with the first.
1586 if (pnc == pn_Cmp_Eq) {
1587 ir_node *bound = get_Confirm_bound(n);
1590 * Optimize a rare case:
1591 * Confirm(x, '=', Constlike) ==> Constlike
1593 if (is_irn_constlike(bound)) {
1594 DBG_OPT_CONFIRM(n, bound);
1598 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1602 * Optimize CopyB(mem, x, x) into a Nop.
1604 static ir_node *equivalent_node_CopyB(ir_node *n) {
1605 ir_node *a = get_CopyB_dst(n);
1606 ir_node *b = get_CopyB_src(n);
1609 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1610 ir_node *mem = get_CopyB_mem(n);
1611 ir_node *blk = get_nodes_block(n);
1612 turn_into_tuple(n, pn_CopyB_max);
1613 set_Tuple_pred(n, pn_CopyB_M, mem);
1614 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1615 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1616 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1619 } /* equivalent_node_CopyB */
1622 * Optimize Bounds(idx, idx, upper) into idx.
1624 static ir_node *equivalent_node_Bound(ir_node *n) {
1625 ir_node *idx = get_Bound_index(n);
1626 ir_node *pred = skip_Proj(idx);
1629 if (is_Bound(pred)) {
1631 * idx was Bounds checked in the same MacroBlock previously,
1632 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1634 ir_node *lower = get_Bound_lower(n);
1635 ir_node *upper = get_Bound_upper(n);
1636 if (get_Bound_lower(pred) == lower &&
1637 get_Bound_upper(pred) == upper &&
1638 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1640 * One could expect that we simply return the previous
1641 * Bound here. However, this would be wrong, as we could
1642 * add an exception Proj to a new location then.
1643 * So, we must turn in into a tuple.
1649 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1650 ir_node *mem = get_Bound_mem(n);
1651 ir_node *blk = get_nodes_block(n);
1652 turn_into_tuple(n, pn_Bound_max);
1653 set_Tuple_pred(n, pn_Bound_M, mem);
1654 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1655 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1656 set_Tuple_pred(n, pn_Bound_res, idx);
1659 } /* equivalent_node_Bound */
1662 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1663 * perform no actual computation, as, e.g., the Id nodes. It does not create
1664 * new nodes. It is therefore safe to free n if the node returned is not n.
1665 * If a node returns a Tuple we can not just skip it. If the size of the
1666 * in array fits, we transform n into a tuple (e.g., Div).
1668 ir_node *equivalent_node(ir_node *n) {
1669 if (n->op->ops.equivalent_node)
1670 return n->op->ops.equivalent_node(n);
1672 } /* equivalent_node */
1675 * Sets the default equivalent node operation for an ir_op_ops.
1677 * @param code the opcode for the default operation
1678 * @param ops the operations initialized
1683 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1687 ops->equivalent_node = equivalent_node_##a; \
1727 } /* firm_set_default_equivalent_node */
1730 * Returns non-zero if a node is a Phi node
1731 * with all predecessors constant.
1733 static int is_const_Phi(ir_node *n) {
1736 if (! is_Phi(n) || get_irn_arity(n) == 0)
1738 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1739 if (! is_Const(get_irn_n(n, i)))
1742 } /* is_const_Phi */
1745 * Apply an evaluator on a binop with a constant operators (and one Phi).
1747 * @param phi the Phi node
1748 * @param other the other operand
1749 * @param eval an evaluator function
1750 * @param mode the mode of the result, may be different from the mode of the Phi!
1751 * @param left if non-zero, other is the left operand, else the right
1753 * @return a new Phi node if the conversion was successful, NULL else
1755 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1760 int i, n = get_irn_arity(phi);
1762 NEW_ARR_A(void *, res, n);
1764 for (i = 0; i < n; ++i) {
1765 pred = get_irn_n(phi, i);
1766 tv = get_Const_tarval(pred);
1767 tv = eval(other, tv);
1769 if (tv == tarval_bad) {
1770 /* folding failed, bad */
1776 for (i = 0; i < n; ++i) {
1777 pred = get_irn_n(phi, i);
1778 tv = get_Const_tarval(pred);
1779 tv = eval(tv, other);
1781 if (tv == tarval_bad) {
1782 /* folding failed, bad */
1788 irg = current_ir_graph;
1789 for (i = 0; i < n; ++i) {
1790 pred = get_irn_n(phi, i);
1791 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1792 mode, res[i], get_Const_type(pred));
1794 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1795 } /* apply_binop_on_phi */
1798 * Apply an evaluator on a binop with two constant Phi.
1800 * @param a the left Phi node
1801 * @param b the right Phi node
1802 * @param eval an evaluator function
1803 * @param mode the mode of the result, may be different from the mode of the Phi!
1805 * @return a new Phi node if the conversion was successful, NULL else
1807 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1808 tarval *tv_l, *tv_r, *tv;
1814 if (get_nodes_block(a) != get_nodes_block(b))
1817 n = get_irn_arity(a);
1818 NEW_ARR_A(void *, res, n);
1820 for (i = 0; i < n; ++i) {
1821 pred = get_irn_n(a, i);
1822 tv_l = get_Const_tarval(pred);
1823 pred = get_irn_n(b, i);
1824 tv_r = get_Const_tarval(pred);
1825 tv = eval(tv_l, tv_r);
1827 if (tv == tarval_bad) {
1828 /* folding failed, bad */
1833 irg = current_ir_graph;
1834 for (i = 0; i < n; ++i) {
1835 pred = get_irn_n(a, i);
1836 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1838 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1839 } /* apply_binop_on_2_phis */
1842 * Apply an evaluator on a unop with a constant operator (a Phi).
1844 * @param phi the Phi node
1845 * @param eval an evaluator function
1847 * @return a new Phi node if the conversion was successful, NULL else
1849 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1855 int i, n = get_irn_arity(phi);
1857 NEW_ARR_A(void *, res, n);
1858 for (i = 0; i < n; ++i) {
1859 pred = get_irn_n(phi, i);
1860 tv = get_Const_tarval(pred);
1863 if (tv == tarval_bad) {
1864 /* folding failed, bad */
1869 mode = get_irn_mode(phi);
1870 irg = current_ir_graph;
1871 for (i = 0; i < n; ++i) {
1872 pred = get_irn_n(phi, i);
1873 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1874 mode, res[i], get_Const_type(pred));
1876 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1877 } /* apply_unop_on_phi */
1880 * Apply a conversion on a constant operator (a Phi).
1882 * @param phi the Phi node
1884 * @return a new Phi node if the conversion was successful, NULL else
1886 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1891 int i, n = get_irn_arity(phi);
1893 NEW_ARR_A(void *, res, n);
1894 for (i = 0; i < n; ++i) {
1895 pred = get_irn_n(phi, i);
1896 tv = get_Const_tarval(pred);
1897 tv = tarval_convert_to(tv, mode);
1899 if (tv == tarval_bad) {
1900 /* folding failed, bad */
1905 irg = current_ir_graph;
1906 for (i = 0; i < n; ++i) {
1907 pred = get_irn_n(phi, i);
1908 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1909 mode, res[i], get_Const_type(pred));
1911 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1912 } /* apply_conv_on_phi */
1915 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1916 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1917 * If possible, remove the Conv's.
1919 static ir_node *transform_node_AddSub(ir_node *n) {
1920 ir_mode *mode = get_irn_mode(n);
1922 if (mode_is_reference(mode)) {
1923 ir_node *left = get_binop_left(n);
1924 ir_node *right = get_binop_right(n);
1925 unsigned ref_bits = get_mode_size_bits(mode);
1927 if (is_Conv(left)) {
1928 ir_mode *lmode = get_irn_mode(left);
1929 unsigned bits = get_mode_size_bits(lmode);
1931 if (ref_bits == bits &&
1932 mode_is_int(lmode) &&
1933 get_mode_arithmetic(lmode) == irma_twos_complement) {
1934 ir_node *pre = get_Conv_op(left);
1935 ir_mode *pre_mode = get_irn_mode(pre);
1937 if (mode_is_int(pre_mode) &&
1938 get_mode_size_bits(pre_mode) == bits &&
1939 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1940 /* ok, this conv just changes to sign, moreover the calculation
1941 * is done with same number of bits as our address mode, so
1942 * we can ignore the conv as address calculation can be viewed
1943 * as either signed or unsigned
1945 set_binop_left(n, pre);
1950 if (is_Conv(right)) {
1951 ir_mode *rmode = get_irn_mode(right);
1952 unsigned bits = get_mode_size_bits(rmode);
1954 if (ref_bits == bits &&
1955 mode_is_int(rmode) &&
1956 get_mode_arithmetic(rmode) == irma_twos_complement) {
1957 ir_node *pre = get_Conv_op(right);
1958 ir_mode *pre_mode = get_irn_mode(pre);
1960 if (mode_is_int(pre_mode) &&
1961 get_mode_size_bits(pre_mode) == bits &&
1962 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1963 /* ok, this conv just changes to sign, moreover the calculation
1964 * is done with same number of bits as our address mode, so
1965 * we can ignore the conv as address calculation can be viewed
1966 * as either signed or unsigned
1968 set_binop_right(n, pre);
1973 /* let address arithmetic use unsigned modes */
1974 if (is_Const(right)) {
1975 ir_mode *rmode = get_irn_mode(right);
1977 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1978 /* convert a AddP(P, *s) into AddP(P, *u) */
1979 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1981 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
1982 set_binop_right(n, pre);
1987 } /* transform_node_AddSub */
1989 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1991 if (is_Const(b) && is_const_Phi(a)) { \
1992 /* check for Op(Phi, Const) */ \
1993 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1995 else if (is_Const(a) && is_const_Phi(b)) { \
1996 /* check for Op(Const, Phi) */ \
1997 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1999 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2000 /* check for Op(Phi, Phi) */ \
2001 c = apply_binop_on_2_phis(a, b, eval, mode); \
2004 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2008 #define HANDLE_UNOP_PHI(eval, a, c) \
2010 if (is_const_Phi(a)) { \
2011 /* check for Op(Phi) */ \
2012 c = apply_unop_on_phi(a, eval); \
2014 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2020 * Do the AddSub optimization, then Transform
2021 * Constant folding on Phi
2022 * Add(a,a) -> Mul(a, 2)
2023 * Add(Mul(a, x), a) -> Mul(a, x+1)
2024 * if the mode is integer or float.
2025 * Transform Add(a,-b) into Sub(a,b).
2026 * Reassociation might fold this further.
2028 static ir_node *transform_node_Add(ir_node *n) {
2030 ir_node *a, *b, *c, *oldn = n;
2032 n = transform_node_AddSub(n);
2034 a = get_Add_left(n);
2035 b = get_Add_right(n);
2037 mode = get_irn_mode(n);
2038 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2040 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2041 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2044 if (mode_is_num(mode)) {
2045 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2046 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2047 ir_node *block = get_irn_n(n, -1);
2050 get_irn_dbg_info(n),
2054 new_r_Const_long(current_ir_graph, block, mode, 2),
2056 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2061 get_irn_dbg_info(n),
2067 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2072 get_irn_dbg_info(n),
2078 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2081 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2082 /* Here we rely on constants be on the RIGHT side */
2084 ir_node *op = get_Not_op(a);
2086 if (is_Const(b) && is_Const_one(b)) {
2088 ir_node *blk = get_irn_n(n, -1);
2089 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2090 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2095 ir_node *blk = get_irn_n(n, -1);
2096 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2097 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2102 ir_node *op = get_Not_op(b);
2106 ir_node *blk = get_irn_n(n, -1);
2107 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2108 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2115 } /* transform_node_Add */
2118 * returns -cnst or NULL if impossible
2120 static ir_node *const_negate(ir_node *cnst) {
2121 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2122 dbg_info *dbgi = get_irn_dbg_info(cnst);
2123 ir_graph *irg = get_irn_irg(cnst);
2124 ir_node *block = get_nodes_block(cnst);
2125 ir_mode *mode = get_irn_mode(cnst);
2126 if (tv == tarval_bad) return NULL;
2127 return new_rd_Const(dbgi, irg, block, mode, tv);
2131 * Do the AddSub optimization, then Transform
2132 * Constant folding on Phi
2133 * Sub(0,a) -> Minus(a)
2134 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2135 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2136 * Sub(Add(a, x), x) -> a
2137 * Sub(x, Add(x, a)) -> -a
2138 * Sub(x, Const) -> Add(x, -Const)
2140 static ir_node *transform_node_Sub(ir_node *n) {
2145 n = transform_node_AddSub(n);
2147 a = get_Sub_left(n);
2148 b = get_Sub_right(n);
2150 mode = get_irn_mode(n);
2153 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2155 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2156 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2159 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2160 /* a - C -> a + (-C) */
2161 ir_node *cnst = const_negate(b);
2163 ir_node *block = get_nodes_block(n);
2164 dbg_info *dbgi = get_irn_dbg_info(n);
2165 ir_graph *irg = get_irn_irg(n);
2167 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2173 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2174 ir_graph *irg = current_ir_graph;
2175 dbg_info *dbg = get_irn_dbg_info(n);
2176 ir_node *block = get_nodes_block(n);
2177 ir_node *left = get_Minus_op(a);
2178 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2180 n = new_rd_Minus(dbg, irg, block, add, mode);
2181 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2183 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2184 ir_graph *irg = current_ir_graph;
2185 dbg_info *dbg = get_irn_dbg_info(n);
2186 ir_node *block = get_nodes_block(n);
2187 ir_node *right = get_Minus_op(b);
2189 n = new_rd_Add(dbg, irg, block, a, right, mode);
2190 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2192 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2193 ir_graph *irg = current_ir_graph;
2194 dbg_info *s_dbg = get_irn_dbg_info(b);
2195 ir_node *s_block = get_nodes_block(b);
2196 ir_node *s_left = get_Sub_right(b);
2197 ir_node *s_right = get_Sub_left(b);
2198 ir_mode *s_mode = get_irn_mode(b);
2199 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2200 dbg_info *a_dbg = get_irn_dbg_info(n);
2201 ir_node *a_block = get_nodes_block(n);
2203 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2204 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2206 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2207 ir_node *m_right = get_Mul_right(b);
2208 if (is_Const(m_right)) {
2209 ir_node *cnst2 = const_negate(m_right);
2210 if (cnst2 != NULL) {
2211 ir_graph *irg = current_ir_graph;
2212 dbg_info *m_dbg = get_irn_dbg_info(b);
2213 ir_node *m_block = get_nodes_block(b);
2214 ir_node *m_left = get_Mul_left(b);
2215 ir_mode *m_mode = get_irn_mode(b);
2216 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2217 dbg_info *a_dbg = get_irn_dbg_info(n);
2218 ir_node *a_block = get_nodes_block(n);
2220 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2221 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2227 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2228 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2230 get_irn_dbg_info(n),
2235 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2239 if (mode_wrap_around(mode)) {
2240 ir_node *left = get_Add_left(a);
2241 ir_node *right = get_Add_right(a);
2243 /* FIXME: Does the Conv's work only for two complement or generally? */
2245 if (mode != get_irn_mode(right)) {
2246 /* This Sub is an effective Cast */
2247 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2250 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2252 } else if (right == b) {
2253 if (mode != get_irn_mode(left)) {
2254 /* This Sub is an effective Cast */
2255 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2258 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2264 if (mode_wrap_around(mode)) {
2265 ir_node *left = get_Add_left(b);
2266 ir_node *right = get_Add_right(b);
2268 /* FIXME: Does the Conv's work only for two complement or generally? */
2270 ir_mode *r_mode = get_irn_mode(right);
2272 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2273 if (mode != r_mode) {
2274 /* This Sub is an effective Cast */
2275 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2277 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2279 } else if (right == a) {
2280 ir_mode *l_mode = get_irn_mode(left);
2282 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2283 if (mode != l_mode) {
2284 /* This Sub is an effective Cast */
2285 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2287 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2292 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2293 ir_mode *mode = get_irn_mode(a);
2295 if (mode == get_irn_mode(b)) {
2297 ir_node *op_a = get_Conv_op(a);
2298 ir_node *op_b = get_Conv_op(b);
2300 /* check if it's allowed to skip the conv */
2301 ma = get_irn_mode(op_a);
2302 mb = get_irn_mode(op_b);
2304 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2305 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2308 set_Sub_right(n, b);
2314 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2315 if (!is_reassoc_running() && is_Mul(a)) {
2316 ir_node *ma = get_Mul_left(a);
2317 ir_node *mb = get_Mul_right(a);
2320 ir_node *blk = get_irn_n(n, -1);
2322 get_irn_dbg_info(n),
2323 current_ir_graph, blk,
2326 get_irn_dbg_info(n),
2327 current_ir_graph, blk,
2329 new_r_Const_long(current_ir_graph, blk, mode, 1),
2332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2334 } else if (mb == b) {
2335 ir_node *blk = get_irn_n(n, -1);
2337 get_irn_dbg_info(n),
2338 current_ir_graph, blk,
2341 get_irn_dbg_info(n),
2342 current_ir_graph, blk,
2344 new_r_Const_long(current_ir_graph, blk, mode, 1),
2347 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2351 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2352 ir_node *x = get_Sub_left(a);
2353 ir_node *y = get_Sub_right(a);
2354 ir_node *blk = get_irn_n(n, -1);
2355 ir_mode *m_b = get_irn_mode(b);
2356 ir_mode *m_y = get_irn_mode(y);
2360 /* Determine the right mode for the Add. */
2363 else if (mode_is_reference(m_b))
2365 else if (mode_is_reference(m_y))
2369 * Both modes are different but none is reference,
2370 * happens for instance in SubP(SubP(P, Iu), Is).
2371 * We have two possibilities here: Cast or ignore.
2372 * Currently we ignore this case.
2377 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2379 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2384 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2385 if (is_Const(a) && is_Not(b)) {
2386 /* c - ~X = X + (c+1) */
2387 tarval *tv = get_Const_tarval(a);
2389 tv = tarval_add(tv, get_mode_one(mode));
2390 if (tv != tarval_bad) {
2391 ir_node *blk = get_irn_n(n, -1);
2392 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2393 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2394 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2400 } /* transform_node_Sub */
2403 * Several transformation done on n*n=2n bits mul.
2404 * These transformations must be done here because new nodes may be produced.
2406 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2408 ir_node *a = get_Mul_left(n);
2409 ir_node *b = get_Mul_right(n);
2410 tarval *ta = value_of(a);
2411 tarval *tb = value_of(b);
2412 ir_mode *smode = get_irn_mode(a);
2414 if (ta == get_mode_one(smode)) {
2415 /* (L)1 * (L)b = (L)b */
2416 ir_node *blk = get_irn_n(n, -1);
2417 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2418 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2421 else if (ta == get_mode_minus_one(smode)) {
2422 /* (L)-1 * (L)b = (L)b */
2423 ir_node *blk = get_irn_n(n, -1);
2424 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2425 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2429 if (tb == get_mode_one(smode)) {
2430 /* (L)a * (L)1 = (L)a */
2431 ir_node *blk = get_irn_n(a, -1);
2432 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2433 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2436 else if (tb == get_mode_minus_one(smode)) {
2437 /* (L)a * (L)-1 = (L)-a */
2438 ir_node *blk = get_irn_n(n, -1);
2439 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2440 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2441 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2448 * Transform Mul(a,-1) into -a.
2449 * Do constant evaluation of Phi nodes.
2450 * Do architecture dependent optimizations on Mul nodes
2452 static ir_node *transform_node_Mul(ir_node *n) {
2453 ir_node *c, *oldn = n;
2454 ir_mode *mode = get_irn_mode(n);
2455 ir_node *a = get_Mul_left(n);
2456 ir_node *b = get_Mul_right(n);
2458 if (is_Bad(a) || is_Bad(b))
2461 if (mode != get_irn_mode(a))
2462 return transform_node_Mul2n(n, mode);
2464 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2466 if (mode_is_signed(mode)) {
2469 if (value_of(a) == get_mode_minus_one(mode))
2471 else if (value_of(b) == get_mode_minus_one(mode))
2474 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2475 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2480 if (is_Const(b)) { /* (-a) * const -> a * -const */
2481 ir_node *cnst = const_negate(b);
2483 dbg_info *dbgi = get_irn_dbg_info(n);
2484 ir_node *block = get_nodes_block(n);
2485 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2486 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2489 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2490 dbg_info *dbgi = get_irn_dbg_info(n);
2491 ir_node *block = get_nodes_block(n);
2492 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2493 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2495 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2496 ir_node *sub_l = get_Sub_left(b);
2497 ir_node *sub_r = get_Sub_right(b);
2498 dbg_info *dbgi = get_irn_dbg_info(n);
2499 ir_graph *irg = current_ir_graph;
2500 ir_node *block = get_nodes_block(n);
2501 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2502 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2503 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2506 } else if (is_Minus(b)) {
2507 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2508 ir_node *sub_l = get_Sub_left(a);
2509 ir_node *sub_r = get_Sub_right(a);
2510 dbg_info *dbgi = get_irn_dbg_info(n);
2511 ir_graph *irg = current_ir_graph;
2512 ir_node *block = get_nodes_block(n);
2513 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2514 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2515 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2519 if (get_mode_arithmetic(mode) == irma_ieee754) {
2521 tarval *tv = get_Const_tarval(a);
2522 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2523 /* 2.0 * b = b + b */
2524 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2525 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2529 else if (is_Const(b)) {
2530 tarval *tv = get_Const_tarval(b);
2531 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2532 /* a * 2.0 = a + a */
2533 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2534 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2539 return arch_dep_replace_mul_with_shifts(n);
2540 } /* transform_node_Mul */
2543 * Transform a Div Node.
2545 static ir_node *transform_node_Div(ir_node *n) {
2546 ir_mode *mode = get_Div_resmode(n);
2547 ir_node *a = get_Div_left(n);
2548 ir_node *b = get_Div_right(n);
2552 if (is_Const(b) && is_const_Phi(a)) {
2553 /* check for Div(Phi, Const) */
2554 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2556 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2560 else if (is_Const(a) && is_const_Phi(b)) {
2561 /* check for Div(Const, Phi) */
2562 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2564 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2568 else if (is_const_Phi(a) && is_const_Phi(b)) {
2569 /* check for Div(Phi, Phi) */
2570 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2572 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2579 if (tv != tarval_bad) {
2580 value = new_Const(get_tarval_mode(tv), tv);
2582 DBG_OPT_CSTEVAL(n, value);
2585 ir_node *a = get_Div_left(n);
2586 ir_node *b = get_Div_right(n);
2589 if (a == b && value_not_zero(a, &dummy)) {
2590 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2591 value = new_Const(mode, get_mode_one(mode));
2592 DBG_OPT_CSTEVAL(n, value);
2595 if (mode_is_signed(mode) && is_Const(b)) {
2596 tarval *tv = get_Const_tarval(b);
2598 if (tv == get_mode_minus_one(mode)) {
2600 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2601 DBG_OPT_CSTEVAL(n, value);
2605 /* Try architecture dependent optimization */
2606 value = arch_dep_replace_div_by_const(n);
2614 /* Turn Div into a tuple (mem, jmp, bad, value) */
2615 mem = get_Div_mem(n);
2616 blk = get_irn_n(n, -1);
2618 /* skip a potential Pin */
2620 mem = get_Pin_op(mem);
2621 turn_into_tuple(n, pn_Div_max);
2622 set_Tuple_pred(n, pn_Div_M, mem);
2623 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2624 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2625 set_Tuple_pred(n, pn_Div_res, value);
2628 } /* transform_node_Div */
2631 * Transform a Mod node.
2633 static ir_node *transform_node_Mod(ir_node *n) {
2634 ir_mode *mode = get_Mod_resmode(n);
2635 ir_node *a = get_Mod_left(n);
2636 ir_node *b = get_Mod_right(n);
2640 if (is_Const(b) && is_const_Phi(a)) {
2641 /* check for Div(Phi, Const) */
2642 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2644 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2648 else if (is_Const(a) && is_const_Phi(b)) {
2649 /* check for Div(Const, Phi) */
2650 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2652 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2656 else if (is_const_Phi(a) && is_const_Phi(b)) {
2657 /* check for Div(Phi, Phi) */
2658 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2660 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2667 if (tv != tarval_bad) {
2668 value = new_Const(get_tarval_mode(tv), tv);
2670 DBG_OPT_CSTEVAL(n, value);
2673 ir_node *a = get_Mod_left(n);
2674 ir_node *b = get_Mod_right(n);
2677 if (a == b && value_not_zero(a, &dummy)) {
2678 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2679 value = new_Const(mode, get_mode_null(mode));
2680 DBG_OPT_CSTEVAL(n, value);
2683 if (mode_is_signed(mode) && is_Const(b)) {
2684 tarval *tv = get_Const_tarval(b);
2686 if (tv == get_mode_minus_one(mode)) {
2688 value = new_Const(mode, get_mode_null(mode));
2689 DBG_OPT_CSTEVAL(n, value);
2693 /* Try architecture dependent optimization */
2694 value = arch_dep_replace_mod_by_const(n);
2702 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2703 mem = get_Mod_mem(n);
2704 blk = get_irn_n(n, -1);
2706 /* skip a potential Pin */
2708 mem = get_Pin_op(mem);
2709 turn_into_tuple(n, pn_Mod_max);
2710 set_Tuple_pred(n, pn_Mod_M, mem);
2711 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2712 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2713 set_Tuple_pred(n, pn_Mod_res, value);
2716 } /* transform_node_Mod */
2719 * Transform a DivMod node.
2721 static ir_node *transform_node_DivMod(ir_node *n) {
2723 ir_node *a = get_DivMod_left(n);
2724 ir_node *b = get_DivMod_right(n);
2725 ir_mode *mode = get_DivMod_resmode(n);
2730 if (is_Const(b) && is_const_Phi(a)) {
2731 /* check for Div(Phi, Const) */
2732 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2733 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2735 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2736 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2740 else if (is_Const(a) && is_const_Phi(b)) {
2741 /* check for Div(Const, Phi) */
2742 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2743 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2745 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2746 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2750 else if (is_const_Phi(a) && is_const_Phi(b)) {
2751 /* check for Div(Phi, Phi) */
2752 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2753 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2755 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2756 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2763 if (tb != tarval_bad) {
2764 if (tb == get_mode_one(get_tarval_mode(tb))) {
2766 vb = new_Const(mode, get_mode_null(mode));
2767 DBG_OPT_CSTEVAL(n, vb);
2769 } else if (ta != tarval_bad) {
2770 tarval *resa, *resb;
2771 resa = tarval_div(ta, tb);
2772 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2773 Jmp for X result!? */
2774 resb = tarval_mod(ta, tb);
2775 if (resb == tarval_bad) return n; /* Causes exception! */
2776 va = new_Const(mode, resa);
2777 vb = new_Const(mode, resb);
2778 DBG_OPT_CSTEVAL(n, va);
2779 DBG_OPT_CSTEVAL(n, vb);
2781 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2782 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2783 vb = new_Const(mode, get_mode_null(mode));
2784 DBG_OPT_CSTEVAL(n, va);
2785 DBG_OPT_CSTEVAL(n, vb);
2787 } else { /* Try architecture dependent optimization */
2790 arch_dep_replace_divmod_by_const(&va, &vb, n);
2791 evaluated = va != NULL;
2793 } else if (a == b) {
2794 if (value_not_zero(a, &dummy)) {
2796 va = new_Const(mode, get_mode_one(mode));
2797 vb = new_Const(mode, get_mode_null(mode));
2798 DBG_OPT_CSTEVAL(n, va);
2799 DBG_OPT_CSTEVAL(n, vb);
2802 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2805 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2806 /* 0 / non-Const = 0 */
2811 if (evaluated) { /* replace by tuple */
2815 mem = get_DivMod_mem(n);
2816 /* skip a potential Pin */
2818 mem = get_Pin_op(mem);
2820 blk = get_irn_n(n, -1);
2821 turn_into_tuple(n, pn_DivMod_max);
2822 set_Tuple_pred(n, pn_DivMod_M, mem);
2823 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2824 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2825 set_Tuple_pred(n, pn_DivMod_res_div, va);
2826 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2830 } /* transform_node_DivMod */
2833 * Optimize x / c to x * (1/c)
2835 static ir_node *transform_node_Quot(ir_node *n) {
2836 ir_mode *mode = get_Quot_resmode(n);
2839 if (get_mode_arithmetic(mode) == irma_ieee754) {
2840 ir_node *b = get_Quot_right(n);
2843 tarval *tv = get_Const_tarval(b);
2847 * Floating point constant folding might be disabled here to
2849 * However, as we check for exact result, doing it is safe.
2852 rem = tarval_enable_fp_ops(1);
2853 tv = tarval_quo(get_mode_one(mode), tv);
2854 (void)tarval_enable_fp_ops(rem);
2856 /* Do the transformation if the result is either exact or we are not
2857 using strict rules. */
2858 if (tv != tarval_bad &&
2859 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2860 ir_node *blk = get_irn_n(n, -1);
2861 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2862 ir_node *a = get_Quot_left(n);
2863 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2864 ir_node *mem = get_Quot_mem(n);
2866 /* skip a potential Pin */
2868 mem = get_Pin_op(mem);
2869 turn_into_tuple(n, pn_Quot_max);
2870 set_Tuple_pred(n, pn_Quot_M, mem);
2871 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2872 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2873 set_Tuple_pred(n, pn_Quot_res, m);
2874 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2879 } /* transform_node_Quot */
2882 * Optimize Abs(x) into x if x is Confirmed >= 0
2883 * Optimize Abs(x) into -x if x is Confirmed <= 0
2884 * Optimize Abs(-x) int Abs(x)
2886 static ir_node *transform_node_Abs(ir_node *n) {
2887 ir_node *c, *oldn = n;
2888 ir_node *a = get_Abs_op(n);
2891 HANDLE_UNOP_PHI(tarval_abs, a, c);
2893 switch (classify_value_sign(a)) {
2894 case value_classified_negative:
2895 mode = get_irn_mode(n);
2898 * We can replace the Abs by -x here.
2899 * We even could add a new Confirm here
2900 * (if not twos complement)
2902 * Note that -x would create a new node, so we could
2903 * not run it in the equivalent_node() context.
2905 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2906 get_nodes_block(n), a, mode);
2908 DBG_OPT_CONFIRM(oldn, n);
2910 case value_classified_positive:
2911 /* n is positive, Abs is not needed */
2914 DBG_OPT_CONFIRM(oldn, n);
2920 /* Abs(-x) = Abs(x) */
2921 mode = get_irn_mode(n);
2922 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2923 get_nodes_block(n), get_Minus_op(a), mode);
2924 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2928 } /* transform_node_Abs */
2931 * Transform a Cond node.
2933 * Replace the Cond by a Jmp if it branches on a constant
2936 static ir_node *transform_node_Cond(ir_node *n) {
2939 ir_node *a = get_Cond_selector(n);
2940 tarval *ta = value_of(a);
2942 /* we need block info which is not available in floating irgs */
2943 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2946 if ((ta != tarval_bad) &&
2947 (get_irn_mode(a) == mode_b) &&
2948 (get_opt_unreachable_code())) {
2949 /* It's a boolean Cond, branching on a boolean constant.
2950 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2951 ir_node *blk = get_nodes_block(n);
2952 jmp = new_r_Jmp(current_ir_graph, blk);
2953 turn_into_tuple(n, pn_Cond_max);
2954 if (ta == tarval_b_true) {
2955 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2956 set_Tuple_pred(n, pn_Cond_true, jmp);
2958 set_Tuple_pred(n, pn_Cond_false, jmp);
2959 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2961 /* We might generate an endless loop, so keep it alive. */
2962 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2965 } /* transform_node_Cond */
2968 * Prototype of a recursive transform function
2969 * for bitwise distributive transformations.
2971 typedef ir_node* (*recursive_transform)(ir_node *n);
2974 * makes use of distributive laws for and, or, eor
2975 * and(a OP c, b OP c) -> and(a, b) OP c
2976 * note, might return a different op than n
2978 static ir_node *transform_bitwise_distributive(ir_node *n,
2979 recursive_transform trans_func)
2982 ir_node *a = get_binop_left(n);
2983 ir_node *b = get_binop_right(n);
2984 ir_op *op = get_irn_op(a);
2985 ir_op *op_root = get_irn_op(n);
2987 if(op != get_irn_op(b))
2990 if (op == op_Conv) {
2991 ir_node *a_op = get_Conv_op(a);
2992 ir_node *b_op = get_Conv_op(b);
2993 ir_mode *a_mode = get_irn_mode(a_op);
2994 ir_mode *b_mode = get_irn_mode(b_op);
2995 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2996 ir_node *blk = get_irn_n(n, -1);
2999 set_binop_left(n, a_op);
3000 set_binop_right(n, b_op);
3001 set_irn_mode(n, a_mode);
3003 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3005 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3011 /* nothing to gain here */
3015 if (op == op_Shrs || op == op_Shr || op == op_Shl
3016 || op == op_And || op == op_Or || op == op_Eor) {
3017 ir_node *a_left = get_binop_left(a);
3018 ir_node *a_right = get_binop_right(a);
3019 ir_node *b_left = get_binop_left(b);
3020 ir_node *b_right = get_binop_right(b);
3022 ir_node *op1 = NULL;
3023 ir_node *op2 = NULL;
3025 if (is_op_commutative(op)) {
3026 if (a_left == b_left) {
3030 } else if(a_left == b_right) {
3034 } else if(a_right == b_left) {
3040 if(a_right == b_right) {
3047 /* (a sop c) & (b sop c) => (a & b) sop c */
3048 ir_node *blk = get_irn_n(n, -1);
3050 ir_node *new_n = exact_copy(n);
3051 set_binop_left(new_n, op1);
3052 set_binop_right(new_n, op2);
3053 new_n = trans_func(new_n);
3055 if(op_root == op_Eor && op == op_Or) {
3056 dbg_info *dbgi = get_irn_dbg_info(n);
3057 ir_graph *irg = current_ir_graph;
3058 ir_mode *mode = get_irn_mode(c);
3060 c = new_rd_Not(dbgi, irg, blk, c, mode);
3061 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3064 set_nodes_block(n, blk);
3065 set_binop_left(n, new_n);
3066 set_binop_right(n, c);
3067 add_identities(current_ir_graph->value_table, n);
3070 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3081 static ir_node *transform_node_And(ir_node *n) {
3082 ir_node *c, *oldn = n;
3083 ir_node *a = get_And_left(n);
3084 ir_node *b = get_And_right(n);
3087 mode = get_irn_mode(n);
3088 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3090 /* we can evaluate 2 Projs of the same Cmp */
3091 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3092 ir_node *pred_a = get_Proj_pred(a);
3093 ir_node *pred_b = get_Proj_pred(b);
3094 if (pred_a == pred_b) {
3095 dbg_info *dbgi = get_irn_dbg_info(n);
3096 ir_node *block = get_nodes_block(pred_a);
3097 pn_Cmp pn_a = get_Proj_proj(a);
3098 pn_Cmp pn_b = get_Proj_proj(b);
3099 /* yes, we can simply calculate with pncs */
3100 pn_Cmp new_pnc = pn_a & pn_b;
3102 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3107 ir_node *op = get_Not_op(b);
3109 ir_node *ba = get_And_left(op);
3110 ir_node *bb = get_And_right(op);
3112 /* it's enough to test the following cases due to normalization! */
3113 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3114 /* (a|b) & ~(a&b) = a^b */
3115 ir_node *block = get_nodes_block(n);
3117 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3118 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3126 ir_node *op = get_Not_op(a);
3128 ir_node *aa = get_And_left(op);
3129 ir_node *ab = get_And_right(op);
3131 /* it's enough to test the following cases due to normalization! */
3132 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3133 /* (a|b) & ~(a&b) = a^b */
3134 ir_node *block = get_nodes_block(n);
3136 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3144 ir_node *al = get_Eor_left(a);
3145 ir_node *ar = get_Eor_right(a);
3148 /* (b ^ a) & b -> ~a & b */
3149 dbg_info *dbg = get_irn_dbg_info(n);
3150 ir_node *block = get_nodes_block(n);
3152 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3153 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3154 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3158 /* (a ^ b) & b -> ~a & b */
3159 dbg_info *dbg = get_irn_dbg_info(n);
3160 ir_node *block = get_nodes_block(n);
3162 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3163 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3164 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3169 ir_node *bl = get_Eor_left(b);
3170 ir_node *br = get_Eor_right(b);
3173 /* a & (a ^ b) -> a & ~b */
3174 dbg_info *dbg = get_irn_dbg_info(n);
3175 ir_node *block = get_nodes_block(n);
3177 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3178 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3179 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3183 /* a & (b ^ a) -> a & ~b */
3184 dbg_info *dbg = get_irn_dbg_info(n);
3185 ir_node *block = get_nodes_block(n);
3187 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3188 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3193 if (is_Not(a) && is_Not(b)) {
3194 /* ~a & ~b = ~(a|b) */
3195 ir_node *block = get_nodes_block(n);
3196 ir_mode *mode = get_irn_mode(n);
3200 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3201 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3206 n = transform_bitwise_distributive(n, transform_node_And);
3209 } /* transform_node_And */
3214 static ir_node *transform_node_Eor(ir_node *n) {
3215 ir_node *c, *oldn = n;
3216 ir_node *a = get_Eor_left(n);
3217 ir_node *b = get_Eor_right(n);
3218 ir_mode *mode = get_irn_mode(n);
3220 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3222 /* we can evaluate 2 Projs of the same Cmp */
3223 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3224 ir_node *pred_a = get_Proj_pred(a);
3225 ir_node *pred_b = get_Proj_pred(b);
3226 if(pred_a == pred_b) {
3227 dbg_info *dbgi = get_irn_dbg_info(n);
3228 ir_node *block = get_nodes_block(pred_a);
3229 pn_Cmp pn_a = get_Proj_proj(a);
3230 pn_Cmp pn_b = get_Proj_proj(b);
3231 /* yes, we can simply calculate with pncs */
3232 pn_Cmp new_pnc = pn_a ^ pn_b;
3234 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3241 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3242 mode, get_mode_null(mode));
3243 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3244 } else if (mode == mode_b &&
3246 is_Const(b) && is_Const_one(b) &&
3247 is_Cmp(get_Proj_pred(a))) {
3248 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3249 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3250 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3252 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3253 } else if (is_Const(b)) {
3254 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3255 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3256 ir_node *not_op = get_Not_op(a);
3257 dbg_info *dbg = get_irn_dbg_info(n);
3258 ir_graph *irg = current_ir_graph;
3259 ir_node *block = get_nodes_block(n);
3260 ir_mode *mode = get_irn_mode(n);
3261 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3263 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3264 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3265 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3268 n = transform_bitwise_distributive(n, transform_node_Eor);
3272 } /* transform_node_Eor */
3277 static ir_node *transform_node_Not(ir_node *n) {
3278 ir_node *c, *oldn = n;
3279 ir_node *a = get_Not_op(n);
3280 ir_mode *mode = get_irn_mode(n);
3282 HANDLE_UNOP_PHI(tarval_not,a,c);
3284 /* check for a boolean Not */
3285 if (mode == mode_b &&
3287 is_Cmp(get_Proj_pred(a))) {
3288 /* We negate a Cmp. The Cmp has the negated result anyways! */
3289 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3290 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3291 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3295 ir_node *eor_b = get_Eor_right(a);
3296 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3297 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3298 ir_node *eor_a = get_Eor_left(a);
3299 dbg_info *dbg = get_irn_dbg_info(n);
3300 ir_graph *irg = current_ir_graph;
3301 ir_node *block = get_nodes_block(n);
3302 ir_mode *mode = get_irn_mode(n);
3303 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3307 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3308 if (is_Minus(a)) { /* ~-x -> x + -1 */
3309 dbg_info *dbg = get_irn_dbg_info(n);
3310 ir_graph *irg = current_ir_graph;
3311 ir_node *block = get_nodes_block(n);
3312 ir_node *add_l = get_Minus_op(a);
3313 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3314 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3315 } else if (is_Add(a)) {
3316 ir_node *add_r = get_Add_right(a);
3317 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3318 /* ~(x + -1) = -x */
3319 ir_node *op = get_Add_left(a);
3320 ir_node *blk = get_irn_n(n, -1);
3321 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3322 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3327 } /* transform_node_Not */
3330 * Transform a Minus.
3334 * -(a >>u (size-1)) = a >>s (size-1)
3335 * -(a >>s (size-1)) = a >>u (size-1)
3336 * -(a * const) -> a * -const
3338 static ir_node *transform_node_Minus(ir_node *n) {
3339 ir_node *c, *oldn = n;
3340 ir_node *a = get_Minus_op(n);
3343 HANDLE_UNOP_PHI(tarval_neg,a,c);
3345 mode = get_irn_mode(a);
3346 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3347 /* the following rules are only to twos-complement */
3350 ir_node *op = get_Not_op(a);
3351 tarval *tv = get_mode_one(mode);
3352 ir_node *blk = get_irn_n(n, -1);
3353 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3354 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3355 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3359 ir_node *c = get_Shr_right(a);
3362 tarval *tv = get_Const_tarval(c);
3364 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3365 /* -(a >>u (size-1)) = a >>s (size-1) */
3366 ir_node *v = get_Shr_left(a);
3368 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3369 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3375 ir_node *c = get_Shrs_right(a);
3378 tarval *tv = get_Const_tarval(c);
3380 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3381 /* -(a >>s (size-1)) = a >>u (size-1) */
3382 ir_node *v = get_Shrs_left(a);
3384 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3385 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3392 /* - (a-b) = b - a */
3393 ir_node *la = get_Sub_left(a);
3394 ir_node *ra = get_Sub_right(a);
3395 ir_node *blk = get_irn_n(n, -1);
3397 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3398 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3402 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3403 ir_node *mul_l = get_Mul_left(a);
3404 ir_node *mul_r = get_Mul_right(a);
3405 if (is_Const(mul_r)) {
3406 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3407 if(tv != tarval_bad) {
3408 ir_node *cnst = new_Const(mode, tv);
3409 dbg_info *dbg = get_irn_dbg_info(a);
3410 ir_graph *irg = current_ir_graph;
3411 ir_node *block = get_nodes_block(a);
3412 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3413 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3420 } /* transform_node_Minus */
3423 * Transform a Cast_type(Const) into a new Const_type
3425 static ir_node *transform_node_Cast(ir_node *n) {
3427 ir_node *pred = get_Cast_op(n);
3428 ir_type *tp = get_irn_type(n);
3430 if (is_Const(pred) && get_Const_type(pred) != tp) {
3431 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3432 get_Const_tarval(pred), tp);
3433 DBG_OPT_CSTEVAL(oldn, n);
3434 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3435 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3436 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3437 DBG_OPT_CSTEVAL(oldn, n);
3441 } /* transform_node_Cast */
3444 * Transform a Proj(Div) with a non-zero value.
3445 * Removes the exceptions and routes the memory to the NoMem node.
3447 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3448 ir_node *div = get_Proj_pred(proj);
3449 ir_node *b = get_Div_right(div);
3450 ir_node *confirm, *res, *new_mem;
3453 if (value_not_zero(b, &confirm)) {
3454 /* div(x, y) && y != 0 */
3455 if (confirm == NULL) {
3456 /* we are sure we have a Const != 0 */
3457 new_mem = get_Div_mem(div);
3458 if (is_Pin(new_mem))
3459 new_mem = get_Pin_op(new_mem);
3460 set_Div_mem(div, new_mem);
3461 set_irn_pinned(div, op_pin_state_floats);
3464 proj_nr = get_Proj_proj(proj);
3466 case pn_Div_X_regular:
3467 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3469 case pn_Div_X_except:
3470 /* we found an exception handler, remove it */
3471 DBG_OPT_EXC_REM(proj);
3475 res = get_Div_mem(div);
3476 new_mem = get_irg_no_mem(current_ir_graph);
3479 /* This node can only float up to the Confirm block */
3480 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3482 set_irn_pinned(div, op_pin_state_floats);
3483 /* this is a Div without exception, we can remove the memory edge */
3484 set_Div_mem(div, new_mem);
3489 } /* transform_node_Proj_Div */
3492 * Transform a Proj(Mod) with a non-zero value.
3493 * Removes the exceptions and routes the memory to the NoMem node.
3495 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3496 ir_node *mod = get_Proj_pred(proj);
3497 ir_node *b = get_Mod_right(mod);
3498 ir_node *confirm, *res, *new_mem;
3501 if (value_not_zero(b, &confirm)) {
3502 /* mod(x, y) && y != 0 */
3503 proj_nr = get_Proj_proj(proj);
3505 if (confirm == NULL) {
3506 /* we are sure we have a Const != 0 */
3507 new_mem = get_Mod_mem(mod);
3508 if (is_Pin(new_mem))
3509 new_mem = get_Pin_op(new_mem);
3510 set_Mod_mem(mod, new_mem);
3511 set_irn_pinned(mod, op_pin_state_floats);
3516 case pn_Mod_X_regular:
3517 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3519 case pn_Mod_X_except:
3520 /* we found an exception handler, remove it */
3521 DBG_OPT_EXC_REM(proj);
3525 res = get_Mod_mem(mod);
3526 new_mem = get_irg_no_mem(current_ir_graph);
3529 /* This node can only float up to the Confirm block */
3530 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3532 /* this is a Mod without exception, we can remove the memory edge */
3533 set_Mod_mem(mod, new_mem);
3536 if (get_Mod_left(mod) == b) {
3537 /* a % a = 0 if a != 0 */
3538 ir_mode *mode = get_irn_mode(proj);
3539 ir_node *res = new_Const(mode, get_mode_null(mode));
3541 DBG_OPT_CSTEVAL(mod, res);
3547 } /* transform_node_Proj_Mod */
3550 * Transform a Proj(DivMod) with a non-zero value.
3551 * Removes the exceptions and routes the memory to the NoMem node.
3553 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3554 ir_node *divmod = get_Proj_pred(proj);
3555 ir_node *b = get_DivMod_right(divmod);
3556 ir_node *confirm, *res, *new_mem;
3559 if (value_not_zero(b, &confirm)) {
3560 /* DivMod(x, y) && y != 0 */
3561 proj_nr = get_Proj_proj(proj);
3563 if (confirm == NULL) {
3564 /* we are sure we have a Const != 0 */
3565 new_mem = get_DivMod_mem(divmod);
3566 if (is_Pin(new_mem))
3567 new_mem = get_Pin_op(new_mem);
3568 set_DivMod_mem(divmod, new_mem);
3569 set_irn_pinned(divmod, op_pin_state_floats);
3574 case pn_DivMod_X_regular:
3575 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3577 case pn_DivMod_X_except:
3578 /* we found an exception handler, remove it */
3579 DBG_OPT_EXC_REM(proj);
3583 res = get_DivMod_mem(divmod);
3584 new_mem = get_irg_no_mem(current_ir_graph);
3587 /* This node can only float up to the Confirm block */
3588 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3590 /* this is a DivMod without exception, we can remove the memory edge */
3591 set_DivMod_mem(divmod, new_mem);
3594 case pn_DivMod_res_mod:
3595 if (get_DivMod_left(divmod) == b) {
3596 /* a % a = 0 if a != 0 */
3597 ir_mode *mode = get_irn_mode(proj);
3598 ir_node *res = new_Const(mode, get_mode_null(mode));
3600 DBG_OPT_CSTEVAL(divmod, res);
3606 } /* transform_node_Proj_DivMod */
3609 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3611 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3612 if (get_opt_unreachable_code()) {
3613 ir_node *n = get_Proj_pred(proj);
3614 ir_node *b = get_Cond_selector(n);
3616 if (mode_is_int(get_irn_mode(b))) {
3617 tarval *tb = value_of(b);
3619 if (tb != tarval_bad) {
3620 /* we have a constant switch */
3621 long num = get_Proj_proj(proj);
3623 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3624 if (get_tarval_long(tb) == num) {
3625 /* Do NOT create a jump here, or we will have 2 control flow ops
3626 * in a block. This case is optimized away in optimize_cf(). */
3629 /* this case will NEVER be taken, kill it */
3637 } /* transform_node_Proj_Cond */
3640 * Create a 0 constant of given mode.
3642 static ir_node *create_zero_const(ir_mode *mode) {
3643 tarval *tv = get_mode_null(mode);
3644 ir_node *cnst = new_Const(mode, tv);
3649 /* the order of the values is important! */
3650 typedef enum const_class {
3656 static const_class classify_const(const ir_node* n)
3658 if (is_Const(n)) return const_const;
3659 if (is_irn_constlike(n)) return const_like;
3664 * Determines whether r is more constlike or has a larger index (in that order)
3667 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3669 const const_class l_order = classify_const(l);
3670 const const_class r_order = classify_const(r);
3672 l_order > r_order ||
3673 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3677 * Normalizes and optimizes Cmp nodes.
3679 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3680 ir_node *n = get_Proj_pred(proj);
3681 ir_node *left = get_Cmp_left(n);
3682 ir_node *right = get_Cmp_right(n);
3685 ir_mode *mode = NULL;
3686 long proj_nr = get_Proj_proj(proj);
3688 /* we can evaluate some cases directly */
3691 return new_Const(mode_b, get_tarval_b_false());
3693 return new_Const(mode_b, get_tarval_b_true());
3695 if (!mode_is_float(get_irn_mode(left)))
3696 return new_Const(mode_b, get_tarval_b_true());
3702 /* remove Casts of both sides */
3703 left = skip_Cast(left);
3704 right = skip_Cast(right);
3706 /* Remove unnecessary conversions */
3707 /* TODO handle constants */
3708 if (is_Conv(left) && is_Conv(right)) {
3709 ir_mode *mode = get_irn_mode(left);
3710 ir_node *op_left = get_Conv_op(left);
3711 ir_node *op_right = get_Conv_op(right);
3712 ir_mode *mode_left = get_irn_mode(op_left);
3713 ir_mode *mode_right = get_irn_mode(op_right);
3715 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3716 && mode_left != mode_b && mode_right != mode_b) {
3717 ir_graph *irg = current_ir_graph;
3718 ir_node *block = get_nodes_block(n);
3720 if (mode_left == mode_right) {
3724 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3725 } else if (smaller_mode(mode_left, mode_right)) {
3726 left = new_r_Conv(irg, block, op_left, mode_right);
3729 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3730 } else if (smaller_mode(mode_right, mode_left)) {
3732 right = new_r_Conv(irg, block, op_right, mode_left);
3734 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3739 /* remove operation on both sides if possible */
3740 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3742 * The following operations are NOT safe for floating point operations, for instance
3743 * 1.0 + inf == 2.0 + inf, =/=> x == y
3745 if (mode_is_int(get_irn_mode(left))) {
3746 unsigned lop = get_irn_opcode(left);
3748 if (lop == get_irn_opcode(right)) {
3749 ir_node *ll, *lr, *rl, *rr;
3751 /* same operation on both sides, try to remove */
3755 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3756 left = get_unop_op(left);
3757 right = get_unop_op(right);
3759 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3762 ll = get_Add_left(left);
3763 lr = get_Add_right(left);
3764 rl = get_Add_left(right);
3765 rr = get_Add_right(right);
3768 /* X + a CMP X + b ==> a CMP b */
3772 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3773 } else if (ll == rr) {
3774 /* X + a CMP b + X ==> a CMP b */
3778 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3779 } else if (lr == rl) {
3780 /* a + X CMP X + b ==> a CMP b */
3784 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3785 } else if (lr == rr) {
3786 /* a + X CMP b + X ==> a CMP b */
3790 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3794 ll = get_Sub_left(left);
3795 lr = get_Sub_right(left);
3796 rl = get_Sub_left(right);
3797 rr = get_Sub_right(right);
3800 /* X - a CMP X - b ==> a CMP b */
3804 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3805 } else if (lr == rr) {
3806 /* a - X CMP b - X ==> a CMP b */
3810 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3814 if (get_Rot_right(left) == get_Rot_right(right)) {
3815 /* a ROT X CMP b ROT X ==> a CMP b */
3816 left = get_Rot_left(left);
3817 right = get_Rot_left(right);
3819 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3827 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3828 if (is_Add(left) || is_Sub(left)) {
3829 ir_node *ll = get_binop_left(left);
3830 ir_node *lr = get_binop_right(left);
3832 if (lr == right && is_Add(left)) {
3839 right = create_zero_const(get_irn_mode(left));
3841 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3844 if (is_Add(right) || is_Sub(right)) {
3845 ir_node *rl = get_binop_left(right);
3846 ir_node *rr = get_binop_right(right);
3848 if (rr == left && is_Add(right)) {
3855 right = create_zero_const(get_irn_mode(left));
3857 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3860 } /* mode_is_int(...) */
3861 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3863 /* replace mode_b compares with ands/ors */
3864 if (get_irn_mode(left) == mode_b) {
3865 ir_graph *irg = current_ir_graph;
3866 ir_node *block = get_nodes_block(n);
3870 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3871 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3872 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3873 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3874 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3875 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3876 default: bres = NULL;
3879 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3885 * First step: normalize the compare op
3886 * by placing the constant on the right side
3887 * or moving the lower address node to the left.
3889 if (!operands_are_normalized(left, right)) {
3895 proj_nr = get_inversed_pnc(proj_nr);
3900 * Second step: Try to reduce the magnitude
3901 * of a constant. This may help to generate better code
3902 * later and may help to normalize more compares.
3903 * Of course this is only possible for integer values.
3905 if (is_Const(right)) {
3906 mode = get_irn_mode(right);
3907 tv = get_Const_tarval(right);
3909 /* TODO extend to arbitrary constants */
3910 if (is_Conv(left) && tarval_is_null(tv)) {
3911 ir_node *op = get_Conv_op(left);
3912 ir_mode *op_mode = get_irn_mode(op);
3915 * UpConv(x) REL 0 ==> x REL 0
3917 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3918 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3919 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3920 tv = get_mode_null(op_mode);
3924 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3928 if (tv != tarval_bad) {
3929 /* the following optimization is possible on modes without Overflow
3930 * on Unary Minus or on == and !=:
3931 * -a CMP c ==> a swap(CMP) -c
3933 * Beware: for two-complement Overflow may occur, so only == and != can
3934 * be optimized, see this:
3935 * -MININT < 0 =/=> MININT > 0 !!!
3937 if (is_Minus(left) &&
3938 (!mode_overflow_on_unary_Minus(mode) ||
3939 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3940 tv = tarval_neg(tv);
3942 if (tv != tarval_bad) {
3943 left = get_Minus_op(left);
3944 proj_nr = get_inversed_pnc(proj_nr);
3946 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3948 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3949 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3950 tv = tarval_not(tv);
3952 if (tv != tarval_bad) {
3953 left = get_Not_op(left);
3955 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3959 /* for integer modes, we have more */
3960 if (mode_is_int(mode)) {
3961 /* Ne includes Unordered which is not possible on integers.
3962 * However, frontends often use this wrong, so fix it here */
3963 if (proj_nr & pn_Cmp_Uo) {
3964 proj_nr &= ~pn_Cmp_Uo;
3965 set_Proj_proj(proj, proj_nr);
3968 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3969 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3970 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3971 tv = tarval_sub(tv, get_mode_one(mode));
3973 if (tv != tarval_bad) {
3974 proj_nr ^= pn_Cmp_Eq;
3976 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3979 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3980 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3981 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3982 tv = tarval_add(tv, get_mode_one(mode));
3984 if (tv != tarval_bad) {
3985 proj_nr ^= pn_Cmp_Eq;
3987 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3991 /* the following reassociations work only for == and != */
3992 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3994 #if 0 /* Might be not that good in general */
3995 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3996 if (tarval_is_null(tv) && is_Sub(left)) {
3997 right = get_Sub_right(left);
3998 left = get_Sub_left(left);
4000 tv = value_of(right);
4002 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4006 if (tv != tarval_bad) {
4007 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4009 ir_node *c1 = get_Sub_right(left);
4010 tarval *tv2 = value_of(c1);
4012 if (tv2 != tarval_bad) {
4013 tv2 = tarval_add(tv, value_of(c1));
4015 if (tv2 != tarval_bad) {
4016 left = get_Sub_left(left);
4019 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4023 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4024 else if (is_Add(left)) {
4025 ir_node *a_l = get_Add_left(left);
4026 ir_node *a_r = get_Add_right(left);
4030 if (is_Const(a_l)) {
4032 tv2 = value_of(a_l);
4035 tv2 = value_of(a_r);
4038 if (tv2 != tarval_bad) {
4039 tv2 = tarval_sub(tv, tv2);
4041 if (tv2 != tarval_bad) {
4045 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4049 /* -a == c ==> a == -c, -a != c ==> a != -c */
4050 else if (is_Minus(left)) {
4051 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4053 if (tv2 != tarval_bad) {
4054 left = get_Minus_op(left);
4057 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4062 /* the following reassociations work only for <= */
4063 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4064 if (tv != tarval_bad) {
4065 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4066 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4072 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4073 switch (get_irn_opcode(left)) {
4077 c1 = get_And_right(left);
4080 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4081 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4083 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
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);
4092 if (tarval_is_single_bit(tv)) {
4094 * optimization for AND:
4096 * And(x, C) == C ==> And(x, C) != 0
4097 * And(x, C) != C ==> And(X, C) == 0
4099 * if C is a single Bit constant.
4102 /* check for Constant's match. We have check hare the tarvals,
4103 because our const might be changed */
4104 if (get_Const_tarval(c1) == tv) {
4105 /* fine: do the transformation */
4106 tv = get_mode_null(get_tarval_mode(tv));
4107 proj_nr ^= pn_Cmp_Leg;
4109 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4115 c1 = get_Or_right(left);
4116 if (is_Const(c1) && tarval_is_null(tv)) {
4118 * Or(x, C) == 0 && C != 0 ==> FALSE
4119 * Or(x, C) != 0 && C != 0 ==> TRUE
4121 if (! tarval_is_null(get_Const_tarval(c1))) {
4122 /* TODO: move to constant evaluation */
4123 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4124 c1 = new_Const(mode_b, tv);
4125 DBG_OPT_CSTEVAL(proj, c1);
4132 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4134 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4137 c1 = get_Shl_right(left);
4139 tarval *tv1 = get_Const_tarval(c1);
4140 ir_mode *mode = get_irn_mode(left);
4141 tarval *minus1 = get_mode_all_one(mode);
4142 tarval *amask = tarval_shr(minus1, tv1);
4143 tarval *cmask = tarval_shl(minus1, tv1);
4146 if (tarval_and(tv, cmask) != tv) {
4147 /* condition not met */
4148 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4149 c1 = new_Const(mode_b, tv);
4150 DBG_OPT_CSTEVAL(proj, c1);
4153 sl = get_Shl_left(left);
4154 blk = get_nodes_block(n);
4155 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4156 tv = tarval_shr(tv, tv1);
4158 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4163 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4165 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4168 c1 = get_Shr_right(left);
4170 tarval *tv1 = get_Const_tarval(c1);
4171 ir_mode *mode = get_irn_mode(left);
4172 tarval *minus1 = get_mode_all_one(mode);
4173 tarval *amask = tarval_shl(minus1, tv1);
4174 tarval *cmask = tarval_shr(minus1, tv1);
4177 if (tarval_and(tv, cmask) != tv) {
4178 /* condition not met */
4179 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4180 c1 = new_Const(mode_b, tv);
4181 DBG_OPT_CSTEVAL(proj, c1);
4184 sl = get_Shr_left(left);
4185 blk = get_nodes_block(n);
4186 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4187 tv = tarval_shl(tv, tv1);
4189 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4194 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4196 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4199 c1 = get_Shrs_right(left);
4201 tarval *tv1 = get_Const_tarval(c1);
4202 ir_mode *mode = get_irn_mode(left);
4203 tarval *minus1 = get_mode_all_one(mode);
4204 tarval *amask = tarval_shl(minus1, tv1);
4205 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4208 cond = tarval_sub(cond, tv1);
4209 cond = tarval_shrs(tv, cond);
4211 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4212 /* condition not met */
4213 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4214 c1 = new_Const(mode_b, tv);
4215 DBG_OPT_CSTEVAL(proj, c1);
4218 sl = get_Shrs_left(left);
4219 blk = get_nodes_block(n);
4220 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4221 tv = tarval_shl(tv, tv1);
4223 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4228 } /* tarval != bad */
4231 if (changed & 2) /* need a new Const */
4232 right = new_Const(mode, tv);
4234 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4235 ir_node *op = get_Proj_pred(left);
4237 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4238 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4239 ir_node *c = get_binop_right(op);
4242 tarval *tv = get_Const_tarval(c);
4244 if (tarval_is_single_bit(tv)) {
4245 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4246 ir_node *v = get_binop_left(op);
4247 ir_node *blk = get_irn_n(op, -1);
4248 ir_mode *mode = get_irn_mode(v);
4250 tv = tarval_sub(tv, get_mode_one(mode));
4251 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4253 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4260 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4262 /* create a new compare */
4263 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4264 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4268 } /* transform_node_Proj_Cmp */
4271 * Does all optimizations on nodes that must be done on it's Proj's
4272 * because of creating new nodes.
4274 static ir_node *transform_node_Proj(ir_node *proj) {
4275 ir_node *n = get_Proj_pred(proj);
4277 switch (get_irn_opcode(n)) {
4279 return transform_node_Proj_Div(proj);
4282 return transform_node_Proj_Mod(proj);
4285 return transform_node_Proj_DivMod(proj);
4288 return transform_node_Proj_Cond(proj);
4291 return transform_node_Proj_Cmp(proj);
4294 /* should not happen, but if it does will be optimized away */
4295 return equivalent_node_Proj(proj);
4301 } /* transform_node_Proj */
4304 * Move Confirms down through Phi nodes.
4306 static ir_node *transform_node_Phi(ir_node *phi) {
4308 ir_mode *mode = get_irn_mode(phi);
4310 if (mode_is_reference(mode)) {
4311 n = get_irn_arity(phi);
4313 /* Beware of Phi0 */
4315 ir_node *pred = get_irn_n(phi, 0);
4316 ir_node *bound, *new_Phi, *block, **in;
4319 if (! is_Confirm(pred))
4322 bound = get_Confirm_bound(pred);
4323 pnc = get_Confirm_cmp(pred);
4325 NEW_ARR_A(ir_node *, in, n);
4326 in[0] = get_Confirm_value(pred);
4328 for (i = 1; i < n; ++i) {
4329 pred = get_irn_n(phi, i);
4331 if (! is_Confirm(pred) ||
4332 get_Confirm_bound(pred) != bound ||
4333 get_Confirm_cmp(pred) != pnc)
4335 in[i] = get_Confirm_value(pred);
4337 /* move the Confirm nodes "behind" the Phi */
4338 block = get_irn_n(phi, -1);
4339 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4340 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4344 } /* transform_node_Phi */
4347 * Returns the operands of a commutative bin-op, if one operand is
4348 * a const, it is returned as the second one.
4350 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4351 ir_node *op_a = get_binop_left(binop);
4352 ir_node *op_b = get_binop_right(binop);
4354 assert(is_op_commutative(get_irn_op(binop)));
4356 if (is_Const(op_a)) {
4363 } /* get_comm_Binop_Ops */
4366 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4367 * Such pattern may arise in bitfield stores.
4369 * value c4 value c4 & c2
4370 * AND c3 AND c1 | c3
4377 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4380 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4383 ir_node *and_l, *c3;
4384 ir_node *value, *c4;
4385 ir_node *new_and, *new_const, *block;
4386 ir_mode *mode = get_irn_mode(or);
4388 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4391 get_comm_Binop_Ops(or, &and, &c1);
4392 if (!is_Const(c1) || !is_And(and))
4395 get_comm_Binop_Ops(and, &or_l, &c2);
4399 tv1 = get_Const_tarval(c1);
4400 tv2 = get_Const_tarval(c2);
4402 tv = tarval_or(tv1, tv2);
4403 if (tarval_is_all_one(tv)) {
4404 /* the AND does NOT clear a bit with isn't set by the OR */
4405 set_Or_left(or, or_l);
4406 set_Or_right(or, c1);
4408 /* check for more */
4415 get_comm_Binop_Ops(or_l, &and_l, &c3);
4416 if (!is_Const(c3) || !is_And(and_l))
4419 get_comm_Binop_Ops(and_l, &value, &c4);
4423 /* ok, found the pattern, check for conditions */
4424 assert(mode == get_irn_mode(and));
4425 assert(mode == get_irn_mode(or_l));
4426 assert(mode == get_irn_mode(and_l));
4428 tv3 = get_Const_tarval(c3);
4429 tv4 = get_Const_tarval(c4);
4431 tv = tarval_or(tv4, tv2);
4432 if (!tarval_is_all_one(tv)) {
4433 /* have at least one 0 at the same bit position */
4437 n_tv4 = tarval_not(tv4);
4438 if (tv3 != tarval_and(tv3, n_tv4)) {
4439 /* bit in the or_mask is outside the and_mask */
4443 n_tv2 = tarval_not(tv2);
4444 if (tv1 != tarval_and(tv1, n_tv2)) {
4445 /* bit in the or_mask is outside the and_mask */
4449 /* ok, all conditions met */
4450 block = get_irn_n(or, -1);
4452 new_and = new_r_And(current_ir_graph, block,
4453 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4455 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4457 set_Or_left(or, new_and);
4458 set_Or_right(or, new_const);
4460 /* check for more */
4462 } /* transform_node_Or_bf_store */
4465 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4467 static ir_node *transform_node_Or_Rot(ir_node *or) {
4468 ir_mode *mode = get_irn_mode(or);
4469 ir_node *shl, *shr, *block;
4470 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4473 if (! mode_is_int(mode))
4476 shl = get_binop_left(or);
4477 shr = get_binop_right(or);
4486 } else if (!is_Shl(shl)) {
4488 } else if (!is_Shr(shr)) {
4491 x = get_Shl_left(shl);
4492 if (x != get_Shr_left(shr))
4495 c1 = get_Shl_right(shl);
4496 c2 = get_Shr_right(shr);
4497 if (is_Const(c1) && is_Const(c2)) {
4498 tv1 = get_Const_tarval(c1);
4499 if (! tarval_is_long(tv1))
4502 tv2 = get_Const_tarval(c2);
4503 if (! tarval_is_long(tv2))
4506 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4507 != (int) get_mode_size_bits(mode))
4510 /* yet, condition met */
4511 block = get_irn_n(or, -1);
4513 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4515 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4517 } else if (is_Sub(c1)) {
4521 if (get_Sub_right(sub) != v)
4524 c1 = get_Sub_left(sub);
4528 tv1 = get_Const_tarval(c1);
4529 if (! tarval_is_long(tv1))
4532 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4535 /* yet, condition met */
4536 block = get_nodes_block(or);
4538 /* a Rot right is not supported, so use a rot left */
4539 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4541 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4543 } else if (is_Sub(c2)) {
4547 c1 = get_Sub_left(sub);
4551 tv1 = get_Const_tarval(c1);
4552 if (! tarval_is_long(tv1))
4555 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4558 /* yet, condition met */
4559 block = get_irn_n(or, -1);
4562 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4564 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4569 } /* transform_node_Or_Rot */
4574 static ir_node *transform_node_Or(ir_node *n) {
4575 ir_node *c, *oldn = n;
4576 ir_node *a = get_Or_left(n);
4577 ir_node *b = get_Or_right(n);
4580 if (is_Not(a) && is_Not(b)) {
4581 /* ~a | ~b = ~(a&b) */
4582 ir_node *block = get_nodes_block(n);
4584 mode = get_irn_mode(n);
4587 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4588 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4593 /* we can evaluate 2 Projs of the same Cmp */
4594 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4595 ir_node *pred_a = get_Proj_pred(a);
4596 ir_node *pred_b = get_Proj_pred(b);
4597 if (pred_a == pred_b) {
4598 dbg_info *dbgi = get_irn_dbg_info(n);
4599 ir_node *block = get_nodes_block(pred_a);
4600 pn_Cmp pn_a = get_Proj_proj(a);
4601 pn_Cmp pn_b = get_Proj_proj(b);
4602 /* yes, we can simply calculate with pncs */
4603 pn_Cmp new_pnc = pn_a | pn_b;
4605 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4610 mode = get_irn_mode(n);
4611 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4613 n = transform_node_Or_bf_store(n);
4614 n = transform_node_Or_Rot(n);
4618 n = transform_bitwise_distributive(n, transform_node_Or);
4621 } /* transform_node_Or */
4625 static ir_node *transform_node(ir_node *n);
4628 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4630 * Should be moved to reassociation?
4632 static ir_node *transform_node_shift(ir_node *n) {
4633 ir_node *left, *right;
4634 tarval *tv1, *tv2, *res;
4636 int modulo_shf, flag;
4638 left = get_binop_left(n);
4640 /* different operations */
4641 if (get_irn_op(left) != get_irn_op(n))
4644 right = get_binop_right(n);
4645 tv1 = value_of(right);
4646 if (tv1 == tarval_bad)
4649 tv2 = value_of(get_binop_right(left));
4650 if (tv2 == tarval_bad)
4653 res = tarval_add(tv1, tv2);
4655 /* beware: a simple replacement works only, if res < modulo shift */
4656 mode = get_irn_mode(n);
4660 modulo_shf = get_mode_modulo_shift(mode);
4661 if (modulo_shf > 0) {
4662 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4664 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4670 /* ok, we can replace it */
4671 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4673 in[0] = get_binop_left(left);
4674 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4676 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4678 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4680 return transform_node(irn);
4683 } /* transform_node_shift */
4688 static ir_node *transform_node_Shr(ir_node *n) {
4689 ir_node *c, *oldn = n;
4690 ir_node *a = get_Shr_left(n);
4691 ir_node *b = get_Shr_right(n);
4692 ir_mode *mode = get_irn_mode(n);
4694 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4695 return transform_node_shift(n);
4696 } /* transform_node_Shr */
4701 static ir_node *transform_node_Shrs(ir_node *n) {
4702 ir_node *c, *oldn = n;
4703 ir_node *a = get_Shrs_left(n);
4704 ir_node *b = get_Shrs_right(n);
4705 ir_mode *mode = get_irn_mode(n);
4707 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4708 return transform_node_shift(n);
4709 } /* transform_node_Shrs */
4714 static ir_node *transform_node_Shl(ir_node *n) {
4715 ir_node *c, *oldn = n;
4716 ir_node *a = get_Shl_left(n);
4717 ir_node *b = get_Shl_right(n);
4718 ir_mode *mode = get_irn_mode(n);
4720 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4721 return transform_node_shift(n);
4722 } /* transform_node_Shl */
4727 static ir_node *transform_node_Rot(ir_node *n) {
4728 ir_node *c, *oldn = n;
4729 ir_node *a = get_Rot_left(n);
4730 ir_node *b = get_Rot_right(n);
4731 ir_mode *mode = get_irn_mode(n);
4733 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4734 return transform_node_shift(n);
4735 } /* transform_node_Rot */
4740 static ir_node *transform_node_Conv(ir_node *n) {
4741 ir_node *c, *oldn = n;
4742 ir_node *a = get_Conv_op(n);
4744 if (is_const_Phi(a)) {
4745 c = apply_conv_on_phi(a, get_irn_mode(n));
4747 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4752 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4753 ir_mode *mode = get_irn_mode(n);
4754 return new_r_Unknown(current_ir_graph, mode);
4758 } /* transform_node_Conv */
4761 * Remove dead blocks and nodes in dead blocks
4762 * in keep alive list. We do not generate a new End node.
4764 static ir_node *transform_node_End(ir_node *n) {
4765 int i, j, n_keepalives = get_End_n_keepalives(n);
4768 NEW_ARR_A(ir_node *, in, n_keepalives);
4770 for (i = j = 0; i < n_keepalives; ++i) {
4771 ir_node *ka = get_End_keepalive(n, i);
4773 if (! is_Block_dead(ka)) {
4777 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4780 /* FIXME: beabi need to keep a Proj(M) */
4781 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4784 if (j != n_keepalives)
4785 set_End_keepalives(n, j, in);
4787 } /* transform_node_End */
4789 /** returns 1 if a == -b */
4790 static int is_negated_value(ir_node *a, ir_node *b) {
4791 if (is_Minus(a) && get_Minus_op(a) == b)
4793 if (is_Minus(b) && get_Minus_op(b) == a)
4795 if (is_Sub(a) && is_Sub(b)) {
4796 ir_node *a_left = get_Sub_left(a);
4797 ir_node *a_right = get_Sub_right(a);
4798 ir_node *b_left = get_Sub_left(b);
4799 ir_node *b_right = get_Sub_right(b);
4801 if (a_left == b_right && a_right == b_left)
4809 * Optimize a Mux into some simpler cases.
4811 static ir_node *transform_node_Mux(ir_node *n) {
4812 ir_node *oldn = n, *sel = get_Mux_sel(n);
4813 ir_mode *mode = get_irn_mode(n);
4815 if (mode == mode_b) {
4816 ir_node *t = get_Mux_true(n);
4817 ir_node *f = get_Mux_false(n);
4818 dbg_info *dbg = get_irn_dbg_info(n);
4819 ir_node *block = get_irn_n(n, -1);
4820 ir_graph *irg = current_ir_graph;
4823 tarval *tv_t = get_Const_tarval(t);
4824 if (tv_t == tarval_b_true) {
4826 /* Muxb(sel, true, false) = sel */
4827 assert(get_Const_tarval(f) == tarval_b_false);
4828 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4831 /* Muxb(sel, true, x) = Or(sel, x) */
4832 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4833 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4837 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4838 assert(tv_t == tarval_b_false);
4840 /* Muxb(sel, false, true) = Not(sel) */
4841 assert(get_Const_tarval(f) == tarval_b_true);
4842 DBG_OPT_ALGSIM0(oldn, not_sel, FS_OPT_MUX_NOT_BOOL);
4845 /* Muxb(sel, false, x) = And(Not(sel), x) */
4846 n = new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4847 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ANDNOT_BOOL);
4851 } else if (is_Const(f)) {
4852 tarval *tv_f = get_Const_tarval(f);
4853 if (tv_f == tarval_b_true) {
4854 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4855 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4856 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4857 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4860 /* Muxb(sel, x, false) = And(sel, x) */
4861 assert(tv_f == tarval_b_false);
4862 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4863 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4869 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4870 ir_node *cmp = get_Proj_pred(sel);
4871 long pn = get_Proj_proj(sel);
4872 ir_node *f = get_Mux_false(n);
4873 ir_node *t = get_Mux_true(n);
4876 * Note: normalization puts the constant on the right side,
4877 * so we check only one case.
4879 * Note further that these optimization work even for floating point
4880 * with NaN's because -NaN == NaN.
4881 * However, if +0 and -0 is handled differently, we cannot use the first
4885 ir_node *cmp_r = get_Cmp_right(cmp);
4886 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4887 ir_node *block = get_irn_n(n, -1);
4889 if (is_negated_value(f, t)) {
4890 ir_node *cmp_left = get_Cmp_left(cmp);
4892 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4893 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4894 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4896 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4898 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4900 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4901 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4902 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4904 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4906 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4908 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4915 return arch_transform_node_Mux(n);
4916 } /* transform_node_Mux */
4919 * Optimize a Psi into some simpler cases.
4921 static ir_node *transform_node_Psi(ir_node *n) {
4923 return transform_node_Mux(n);
4926 } /* transform_node_Psi */
4929 * optimize sync nodes that have other syncs as input we simply add the inputs
4930 * of the other sync to our own inputs
4932 static ir_node *transform_node_Sync(ir_node *n) {
4933 int arity = get_Sync_n_preds(n);
4936 for (i = 0; i < arity;) {
4937 ir_node *pred = get_Sync_pred(n, i);
4941 if (!is_Sync(pred)) {
4949 pred_arity = get_Sync_n_preds(pred);
4950 for (j = 0; j < pred_arity; ++j) {
4951 ir_node *pred_pred = get_Sync_pred(pred, j);
4956 add_irn_n(n, pred_pred);
4960 if (get_Sync_pred(n, k) == pred_pred) break;
4965 /* rehash the sync node */
4966 add_identities(current_ir_graph->value_table, n);
4972 * Tries several [inplace] [optimizing] transformations and returns an
4973 * equivalent node. The difference to equivalent_node() is that these
4974 * transformations _do_ generate new nodes, and thus the old node must
4975 * not be freed even if the equivalent node isn't the old one.
4977 static ir_node *transform_node(ir_node *n) {
4981 * Transform_node is the only "optimizing transformation" that might
4982 * return a node with a different opcode. We iterate HERE until fixpoint
4983 * to get the final result.
4987 if (n->op->ops.transform_node)
4988 n = n->op->ops.transform_node(n);
4989 } while (oldn != n);
4992 } /* transform_node */
4995 * Sets the default transform node operation for an ir_op_ops.
4997 * @param code the opcode for the default operation
4998 * @param ops the operations initialized
5003 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5007 ops->transform_node = transform_node_##a; \
5044 } /* firm_set_default_transform_node */
5047 /* **************** Common Subexpression Elimination **************** */
5049 /** The size of the hash table used, should estimate the number of nodes
5051 #define N_IR_NODES 512
5053 /** Compares the attributes of two Const nodes. */
5054 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5055 return (get_Const_tarval(a) != get_Const_tarval(b))
5056 || (get_Const_type(a) != get_Const_type(b));
5057 } /* node_cmp_attr_Const */
5059 /** Compares the attributes of two Proj nodes. */
5060 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5061 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5062 } /* node_cmp_attr_Proj */
5064 /** Compares the attributes of two Filter nodes. */
5065 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5066 return get_Filter_proj(a) != get_Filter_proj(b);
5067 } /* node_cmp_attr_Filter */
5069 /** Compares the attributes of two Alloc nodes. */
5070 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5071 const alloc_attr *pa = get_irn_alloc_attr(a);
5072 const alloc_attr *pb = get_irn_alloc_attr(b);
5073 return (pa->where != pb->where) || (pa->type != pb->type);
5074 } /* node_cmp_attr_Alloc */
5076 /** Compares the attributes of two Free nodes. */
5077 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5078 const free_attr *pa = get_irn_free_attr(a);
5079 const free_attr *pb = get_irn_free_attr(b);
5080 return (pa->where != pb->where) || (pa->type != pb->type);
5081 } /* node_cmp_attr_Free */
5083 /** Compares the attributes of two SymConst nodes. */
5084 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5085 const symconst_attr *pa = get_irn_symconst_attr(a);
5086 const symconst_attr *pb = get_irn_symconst_attr(b);
5087 return (pa->kind != pb->kind)
5088 || (pa->sym.type_p != pb->sym.type_p)
5089 || (pa->tp != pb->tp);
5090 } /* node_cmp_attr_SymConst */
5092 /** Compares the attributes of two Call nodes. */
5093 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5094 return get_irn_call_attr(a) != get_irn_call_attr(b);
5095 } /* node_cmp_attr_Call */
5097 /** Compares the attributes of two Sel nodes. */
5098 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5099 const ir_entity *a_ent = get_Sel_entity(a);
5100 const ir_entity *b_ent = get_Sel_entity(b);
5102 (a_ent->kind != b_ent->kind) ||
5103 (a_ent->name != b_ent->name) ||
5104 (a_ent->owner != b_ent->owner) ||
5105 (a_ent->ld_name != b_ent->ld_name) ||
5106 (a_ent->type != b_ent->type);
5107 } /* node_cmp_attr_Sel */
5109 /** Compares the attributes of two Phi nodes. */
5110 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5111 /* we can only enter this function if both nodes have the same number of inputs,
5112 hence it is enough to check if one of them is a Phi0 */
5114 /* check the Phi0 pos attribute */
5115 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5118 } /* node_cmp_attr_Phi */
5120 /** Compares the attributes of two Conv nodes. */
5121 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5122 return get_Conv_strict(a) != get_Conv_strict(b);
5123 } /* node_cmp_attr_Conv */
5125 /** Compares the attributes of two Cast nodes. */
5126 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5127 return get_Cast_type(a) != get_Cast_type(b);
5128 } /* node_cmp_attr_Cast */
5130 /** Compares the attributes of two Load nodes. */
5131 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5132 if (get_Load_volatility(a) == volatility_is_volatile ||
5133 get_Load_volatility(b) == volatility_is_volatile)
5134 /* NEVER do CSE on volatile Loads */
5136 /* do not CSE Loads with different alignment. Be conservative. */
5137 if (get_Load_align(a) != get_Load_align(b))
5140 return get_Load_mode(a) != get_Load_mode(b);
5141 } /* node_cmp_attr_Load */
5143 /** Compares the attributes of two Store nodes. */
5144 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5145 /* do not CSE Stores with different alignment. Be conservative. */
5146 if (get_Store_align(a) != get_Store_align(b))
5149 /* NEVER do CSE on volatile Stores */
5150 return (get_Store_volatility(a) == volatility_is_volatile ||
5151 get_Store_volatility(b) == volatility_is_volatile);
5152 } /* node_cmp_attr_Store */
5154 /** Compares two exception attributes */
5155 static int node_cmp_exception(ir_node *a, ir_node *b) {
5156 const except_attr *ea = get_irn_except_attr(a);
5157 const except_attr *eb = get_irn_except_attr(b);
5159 return ea->pin_state != eb->pin_state;
5162 #define node_cmp_attr_Bound node_cmp_exception
5164 /** Compares the attributes of two Div nodes. */
5165 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5166 const divmod_attr *ma = get_irn_divmod_attr(a);
5167 const divmod_attr *mb = get_irn_divmod_attr(b);
5168 return ma->exc.pin_state != mb->exc.pin_state ||
5169 ma->res_mode != mb->res_mode ||
5170 ma->no_remainder != mb->no_remainder;
5171 } /* node_cmp_attr_Div */
5173 /** Compares the attributes of two DivMod nodes. */
5174 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5175 const divmod_attr *ma = get_irn_divmod_attr(a);
5176 const divmod_attr *mb = get_irn_divmod_attr(b);
5177 return ma->exc.pin_state != mb->exc.pin_state ||
5178 ma->res_mode != mb->res_mode;
5179 } /* node_cmp_attr_DivMod */
5181 /** Compares the attributes of two Mod nodes. */
5182 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5183 const divmod_attr *ma = get_irn_divmod_attr(a);
5184 const divmod_attr *mb = get_irn_divmod_attr(b);
5185 return ma->exc.pin_state != mb->exc.pin_state ||
5186 ma->res_mode != mb->res_mode;
5187 } /* node_cmp_attr_Mod */
5189 /** Compares the attributes of two Quot nodes. */
5190 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5191 const divmod_attr *ma = get_irn_divmod_attr(a);
5192 const divmod_attr *mb = get_irn_divmod_attr(b);
5193 return ma->exc.pin_state != mb->exc.pin_state ||
5194 ma->res_mode != mb->res_mode;
5195 } /* node_cmp_attr_Quot */
5197 /** Compares the attributes of two Confirm nodes. */
5198 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5199 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5200 } /* node_cmp_attr_Confirm */
5202 /** Compares the attributes of two ASM nodes. */
5203 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5205 const ir_asm_constraint *ca;
5206 const ir_asm_constraint *cb;
5209 if (get_ASM_text(a) != get_ASM_text(b))
5212 /* Should we really check the constraints here? Should be better, but is strange. */
5213 n = get_ASM_n_input_constraints(a);
5214 if (n != get_ASM_n_input_constraints(b))
5217 ca = get_ASM_input_constraints(a);
5218 cb = get_ASM_input_constraints(b);
5219 for (i = 0; i < n; ++i) {
5220 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5224 n = get_ASM_n_output_constraints(a);
5225 if (n != get_ASM_n_output_constraints(b))
5228 ca = get_ASM_output_constraints(a);
5229 cb = get_ASM_output_constraints(b);
5230 for (i = 0; i < n; ++i) {
5231 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5235 n = get_ASM_n_clobbers(a);
5236 if (n != get_ASM_n_clobbers(b))
5239 cla = get_ASM_clobbers(a);
5240 clb = get_ASM_clobbers(b);
5241 for (i = 0; i < n; ++i) {
5242 if (cla[i] != clb[i])
5246 } /* node_cmp_attr_ASM */
5249 * Set the default node attribute compare operation for an ir_op_ops.
5251 * @param code the opcode for the default operation
5252 * @param ops the operations initialized
5257 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5261 ops->node_cmp_attr = node_cmp_attr_##a; \
5292 } /* firm_set_default_node_cmp_attr */
5295 * Compare function for two nodes in the value table. Gets two
5296 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5298 int identities_cmp(const void *elt, const void *key) {
5299 ir_node *a = (ir_node *)elt;
5300 ir_node *b = (ir_node *)key;
5303 if (a == b) return 0;
5305 if ((get_irn_op(a) != get_irn_op(b)) ||
5306 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5308 /* compare if a's in and b's in are of equal length */
5309 irn_arity_a = get_irn_intra_arity(a);
5310 if (irn_arity_a != get_irn_intra_arity(b))
5313 if (get_irn_pinned(a) == op_pin_state_pinned) {
5314 /* for pinned nodes, the block inputs must be equal */
5315 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5317 } else if (! get_opt_global_cse()) {
5318 /* for block-local CSE both nodes must be in the same MacroBlock */
5319 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5323 /* compare a->in[0..ins] with b->in[0..ins] */
5324 for (i = 0; i < irn_arity_a; i++)
5325 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5329 * here, we already now that the nodes are identical except their
5332 if (a->op->ops.node_cmp_attr)
5333 return a->op->ops.node_cmp_attr(a, b);
5336 } /* identities_cmp */
5339 * Calculate a hash value of a node.
5341 unsigned ir_node_hash(ir_node *node) {
5345 if (node->op == op_Const) {
5346 /* special value for const, as they only differ in their tarval. */
5347 h = HASH_PTR(node->attr.con.tv);
5348 h = 9*h + HASH_PTR(get_irn_mode(node));
5349 } else if (node->op == op_SymConst) {
5350 /* special value for const, as they only differ in their symbol. */
5351 h = HASH_PTR(node->attr.symc.sym.type_p);
5352 h = 9*h + HASH_PTR(get_irn_mode(node));
5355 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5356 h = irn_arity = get_irn_intra_arity(node);
5358 /* consider all in nodes... except the block if not a control flow. */
5359 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5360 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5364 h = 9*h + HASH_PTR(get_irn_mode(node));
5366 h = 9*h + HASH_PTR(get_irn_op(node));
5370 } /* ir_node_hash */
5372 pset *new_identities(void) {
5373 return new_pset(identities_cmp, N_IR_NODES);
5374 } /* new_identities */
5376 void del_identities(pset *value_table) {
5377 del_pset(value_table);
5378 } /* del_identities */
5381 * Normalize a node by putting constants (and operands with larger
5382 * node index) on the right (operator side).
5384 * @param n The node to normalize
5386 static void normalize_node(ir_node *n) {
5387 if (is_op_commutative(get_irn_op(n))) {
5388 ir_node *l = get_binop_left(n);
5389 ir_node *r = get_binop_right(n);
5391 /* For commutative operators perform a OP b == b OP a but keep
5392 * constants on the RIGHT side. This helps greatly in some
5393 * optimizations. Moreover we use the idx number to make the form
5395 if (!operands_are_normalized(l, r)) {
5396 set_binop_left(n, r);
5397 set_binop_right(n, l);
5400 } /* normalize_node */
5403 * Update the nodes after a match in the value table. If both nodes have
5404 * the same MacroBlock but different Blocks, we must ensure that the node
5405 * with the dominating Block (the node that is near to the MacroBlock header
5406 * is stored in the table.
5407 * Because a MacroBlock has only one "non-exception" flow, we don't need
5408 * dominance info here: We known, that one block must dominate the other and
5409 * following the only block input will allow to find it.
5411 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5412 ir_node *known_blk, *new_block, *block, *mbh;
5414 if (get_opt_global_cse()) {
5415 /* Block inputs are meaning less */
5418 known_blk = get_irn_n(known_irn, -1);
5419 new_block = get_irn_n(new_ir_node, -1);
5420 if (known_blk == new_block) {
5421 /* already in the same block */
5425 * We expect the typical case when we built the graph. In that case, the
5426 * known_irn is already the upper one, so checking this should be faster.
5429 mbh = get_Block_MacroBlock(new_block);
5431 if (block == known_blk) {
5432 /* ok, we have found it: known_block dominates new_block as expected */
5437 * We have reached the MacroBlock header NOT founding
5438 * the known_block. new_block must dominate known_block.
5441 set_irn_n(known_irn, -1, new_block);
5444 assert(get_Block_n_cfgpreds(block) == 1);
5445 block = get_Block_cfgpred_block(block, 0);
5447 } /* update_value_table */
5450 * Return the canonical node computing the same value as n.
5452 * @param value_table The value table
5453 * @param n The node to lookup
5455 * Looks up the node in a hash table.
5457 * For Const nodes this is performed in the constructor, too. Const
5458 * nodes are extremely time critical because of their frequent use in
5459 * constant string arrays.
5461 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5464 if (!value_table) return n;
5468 o = pset_find(value_table, n, ir_node_hash(n));
5472 update_known_irn(o, n);
5479 * During construction we set the op_pin_state_pinned flag in the graph right when the
5480 * optimization is performed. The flag turning on procedure global cse could
5481 * be changed between two allocations. This way we are safe.
5483 * @param value_table The value table
5484 * @param n The node to lookup
5486 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5489 n = identify(value_table, n);
5490 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5491 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5493 } /* identify_cons */
5496 * Return the canonical node computing the same value as n.
5497 * Looks up the node in a hash table, enters it in the table
5498 * if it isn't there yet.
5500 * @param value_table the HashSet containing all nodes in the
5502 * @param n the node to look up
5504 * @return a node that computes the same value as n or n if no such
5505 * node could be found
5507 ir_node *identify_remember(pset *value_table, ir_node *n) {
5510 if (!value_table) return n;
5513 /* lookup or insert in hash table with given hash key. */
5514 o = pset_insert(value_table, n, ir_node_hash(n));
5517 update_known_irn(o, n);
5522 } /* identify_remember */
5524 /* Add a node to the identities value table. */
5525 void add_identities(pset *value_table, ir_node *node) {
5526 if (get_opt_cse() && is_no_Block(node))
5527 identify_remember(value_table, node);
5528 } /* add_identities */
5530 /* Visit each node in the value table of a graph. */
5531 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5533 ir_graph *rem = current_ir_graph;
5535 current_ir_graph = irg;
5536 foreach_pset(irg->value_table, node)
5538 current_ir_graph = rem;
5539 } /* visit_all_identities */
5542 * Garbage in, garbage out. If a node has a dead input, i.e., the
5543 * Bad node is input to the node, return the Bad node.
5545 static ir_node *gigo(ir_node *node) {
5547 ir_op *op = get_irn_op(node);
5549 /* remove garbage blocks by looking at control flow that leaves the block
5550 and replacing the control flow by Bad. */
5551 if (get_irn_mode(node) == mode_X) {
5552 ir_node *block = get_nodes_block(skip_Proj(node));
5554 /* Don't optimize nodes in immature blocks. */
5555 if (!get_Block_matured(block))
5557 /* Don't optimize End, may have Bads. */
5558 if (op == op_End) return node;
5560 if (is_Block(block)) {
5561 if (is_Block_dead(block)) {
5562 /* control flow from dead block is dead */
5566 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5567 if (!is_Bad(get_irn_n(block, i)))
5571 ir_graph *irg = get_irn_irg(block);
5572 /* the start block is never dead */
5573 if (block != get_irg_start_block(irg)
5574 && block != get_irg_end_block(irg)) {
5576 * Do NOT kill control flow without setting
5577 * the block to dead of bad things can happen:
5578 * We get a Block that is not reachable be irg_block_walk()
5579 * but can be found by irg_walk()!
5581 set_Block_dead(block);
5588 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5589 blocks predecessors is dead. */
5590 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5591 irn_arity = get_irn_arity(node);
5594 * Beware: we can only read the block of a non-floating node.
5596 if (is_irn_pinned_in_irg(node) &&
5597 is_Block_dead(get_nodes_block(skip_Proj(node))))
5600 for (i = 0; i < irn_arity; i++) {
5601 ir_node *pred = get_irn_n(node, i);
5606 /* Propagating Unknowns here seems to be a bad idea, because
5607 sometimes we need a node as a input and did not want that
5609 However, it might be useful to move this into a later phase
5610 (if you think that optimizing such code is useful). */
5611 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5612 return new_Unknown(get_irn_mode(node));
5617 /* With this code we violate the agreement that local_optimize
5618 only leaves Bads in Block, Phi and Tuple nodes. */
5619 /* If Block has only Bads as predecessors it's garbage. */
5620 /* If Phi has only Bads as predecessors it's garbage. */
5621 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5622 irn_arity = get_irn_arity(node);
5623 for (i = 0; i < irn_arity; i++) {
5624 if (!is_Bad(get_irn_n(node, i))) break;
5626 if (i == irn_arity) node = new_Bad();
5633 * These optimizations deallocate nodes from the obstack.
5634 * It can only be called if it is guaranteed that no other nodes
5635 * reference this one, i.e., right after construction of a node.
5637 * @param n The node to optimize
5639 * current_ir_graph must be set to the graph of the node!
5641 ir_node *optimize_node(ir_node *n) {
5644 ir_opcode iro = get_irn_opcode(n);
5646 /* Always optimize Phi nodes: part of the construction. */
5647 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5649 /* constant expression evaluation / constant folding */
5650 if (get_opt_constant_folding()) {
5651 /* neither constants nor Tuple values can be evaluated */
5652 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5653 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5654 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5655 /* try to evaluate */
5656 tv = computed_value(n);
5657 if (tv != tarval_bad) {
5659 ir_type *old_tp = get_irn_type(n);
5660 int i, arity = get_irn_arity(n);
5664 * Try to recover the type of the new expression.
5666 for (i = 0; i < arity && !old_tp; ++i)
5667 old_tp = get_irn_type(get_irn_n(n, i));
5670 * we MUST copy the node here temporary, because it's still needed
5671 * for DBG_OPT_CSTEVAL
5673 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5674 oldn = alloca(node_size);
5676 memcpy(oldn, n, node_size);
5677 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5679 /* ARG, copy the in array, we need it for statistics */
5680 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5682 /* note the inplace edges module */
5683 edges_node_deleted(n, current_ir_graph);
5685 /* evaluation was successful -- replace the node. */
5686 irg_kill_node(current_ir_graph, n);
5687 nw = new_Const(get_tarval_mode(tv), tv);
5689 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5690 set_Const_type(nw, old_tp);
5691 DBG_OPT_CSTEVAL(oldn, nw);
5692 tarval_enable_fp_ops(old_fp_mode);
5695 tarval_enable_fp_ops(old_fp_mode);
5699 /* remove unnecessary nodes */
5700 if (get_opt_constant_folding() ||
5701 (iro == iro_Phi) || /* always optimize these nodes. */
5703 (iro == iro_Proj) ||
5704 (iro == iro_Block) ) /* Flags tested local. */
5705 n = equivalent_node(n);
5707 /* Common Subexpression Elimination.
5709 * Checks whether n is already available.
5710 * The block input is used to distinguish different subexpressions. Right
5711 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5712 * subexpressions within a block.
5715 n = identify_cons(current_ir_graph->value_table, n);
5718 edges_node_deleted(oldn, current_ir_graph);
5720 /* We found an existing, better node, so we can deallocate the old node. */
5721 irg_kill_node(current_ir_graph, oldn);
5725 /* Some more constant expression evaluation that does not allow to
5727 iro = get_irn_opcode(n);
5728 if (get_opt_constant_folding() ||
5729 (iro == iro_Cond) ||
5730 (iro == iro_Proj)) /* Flags tested local. */
5731 n = transform_node(n);
5733 /* Remove nodes with dead (Bad) input.
5734 Run always for transformation induced Bads. */
5737 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5738 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5739 n = identify_remember(current_ir_graph->value_table, n);
5743 } /* optimize_node */
5747 * These optimizations never deallocate nodes (in place). This can cause dead
5748 * nodes lying on the obstack. Remove these by a dead node elimination,
5749 * i.e., a copying garbage collection.
5751 ir_node *optimize_in_place_2(ir_node *n) {
5754 ir_opcode iro = get_irn_opcode(n);
5756 if (!get_opt_optimize() && !is_Phi(n)) return n;
5758 /* constant expression evaluation / constant folding */
5759 if (get_opt_constant_folding()) {
5760 /* neither constants nor Tuple values can be evaluated */
5761 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5762 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5763 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5764 /* try to evaluate */
5765 tv = computed_value(n);
5766 if (tv != tarval_bad) {
5767 /* evaluation was successful -- replace the node. */
5768 ir_type *old_tp = get_irn_type(n);
5769 int i, arity = get_irn_arity(n);
5772 * Try to recover the type of the new expression.
5774 for (i = 0; i < arity && !old_tp; ++i)
5775 old_tp = get_irn_type(get_irn_n(n, i));
5777 n = new_Const(get_tarval_mode(tv), tv);
5779 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5780 set_Const_type(n, old_tp);
5782 DBG_OPT_CSTEVAL(oldn, n);
5783 tarval_enable_fp_ops(old_fp_mode);
5786 tarval_enable_fp_ops(old_fp_mode);
5790 /* remove unnecessary nodes */
5791 if (get_opt_constant_folding() ||
5792 (iro == iro_Phi) || /* always optimize these nodes. */
5793 (iro == iro_Id) || /* ... */
5794 (iro == iro_Proj) || /* ... */
5795 (iro == iro_Block) ) /* Flags tested local. */
5796 n = equivalent_node(n);
5798 /** common subexpression elimination **/
5799 /* Checks whether n is already available. */
5800 /* The block input is used to distinguish different subexpressions. Right
5801 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5802 subexpressions within a block. */
5803 if (get_opt_cse()) {
5804 n = identify(current_ir_graph->value_table, n);
5807 /* Some more constant expression evaluation. */
5808 iro = get_irn_opcode(n);
5809 if (get_opt_constant_folding() ||
5810 (iro == iro_Cond) ||
5811 (iro == iro_Proj)) /* Flags tested local. */
5812 n = transform_node(n);
5814 /* Remove nodes with dead (Bad) input.
5815 Run always for transformation induced Bads. */
5818 /* Now we can verify the node, as it has no dead inputs any more. */
5821 /* Now we have a legal, useful node. Enter it in hash table for cse.
5822 Blocks should be unique anyways. (Except the successor of start:
5823 is cse with the start block!) */
5824 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5825 n = identify_remember(current_ir_graph->value_table, n);
5828 } /* optimize_in_place_2 */
5831 * Wrapper for external use, set proper status bits after optimization.
5833 ir_node *optimize_in_place(ir_node *n) {
5834 /* Handle graph state */
5835 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5837 if (get_opt_global_cse())
5838 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5839 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5840 set_irg_outs_inconsistent(current_ir_graph);
5842 /* FIXME: Maybe we could also test whether optimizing the node can
5843 change the control graph. */
5844 set_irg_doms_inconsistent(current_ir_graph);
5845 return optimize_in_place_2(n);
5846 } /* optimize_in_place */
5849 * Sets the default operation for an ir_ops.
5851 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5852 ops = firm_set_default_computed_value(code, ops);
5853 ops = firm_set_default_equivalent_node(code, ops);
5854 ops = firm_set_default_transform_node(code, ops);
5855 ops = firm_set_default_node_cmp_attr(code, ops);
5856 ops = firm_set_default_get_type(code, ops);
5857 ops = firm_set_default_get_type_attr(code, ops);
5858 ops = firm_set_default_get_entity_attr(code, ops);
5861 } /* firm_set_default_operations */