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 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
891 } else if (ab == b) {
892 /* (a ^ b) ^ b -> a */
894 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
896 } else if (is_Eor(b)) {
897 ir_node *ba = get_Eor_left(b);
898 ir_node *bb = get_Eor_right(b);
901 /* a ^ (a ^ b) -> b */
903 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
904 } else if (bb == a) {
905 /* a ^ (b ^ a) -> b */
907 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
915 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
917 * The second one looks strange, but this construct
918 * is used heavily in the LCC sources :-).
920 * Beware: The Mode of an Add may be different than the mode of its
921 * predecessors, so we could not return a predecessors in all cases.
923 static ir_node *equivalent_node_Add(ir_node *n) {
925 ir_node *left, *right;
926 ir_mode *mode = get_irn_mode(n);
928 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
929 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
932 n = equivalent_node_neutral_zero(n);
936 left = get_Add_left(n);
937 right = get_Add_right(n);
940 if (get_Sub_right(left) == right) {
943 n = get_Sub_left(left);
944 if (mode == get_irn_mode(n)) {
945 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
951 if (get_Sub_right(right) == left) {
954 n = get_Sub_left(right);
955 if (mode == get_irn_mode(n)) {
956 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
962 } /* equivalent_node_Add */
965 * optimize operations that are not commutative but have neutral 0 on left,
968 static ir_node *equivalent_node_left_zero(ir_node *n) {
971 ir_node *a = get_binop_left(n);
972 ir_node *b = get_binop_right(n);
974 if (is_Const(b) && is_Const_null(b)) {
977 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
980 } /* equivalent_node_left_zero */
982 #define equivalent_node_Shl equivalent_node_left_zero
983 #define equivalent_node_Shr equivalent_node_left_zero
984 #define equivalent_node_Shrs equivalent_node_left_zero
985 #define equivalent_node_Rot equivalent_node_left_zero
988 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
990 * The second one looks strange, but this construct
991 * is used heavily in the LCC sources :-).
993 * Beware: The Mode of a Sub may be different than the mode of its
994 * predecessors, so we could not return a predecessors in all cases.
996 static ir_node *equivalent_node_Sub(ir_node *n) {
999 ir_mode *mode = get_irn_mode(n);
1001 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1002 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1005 b = get_Sub_right(n);
1007 /* Beware: modes might be different */
1008 if (is_Const(b) && is_Const_null(b)) {
1009 ir_node *a = get_Sub_left(n);
1010 if (mode == get_irn_mode(a)) {
1013 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1017 } /* equivalent_node_Sub */
1021 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1024 * -(-a) == a, but might overflow two times.
1025 * We handle it anyway here but the better way would be a
1026 * flag. This would be needed for Pascal for instance.
1028 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1030 ir_node *pred = get_unop_op(n);
1032 /* optimize symmetric unop */
1033 if (get_irn_op(pred) == get_irn_op(n)) {
1034 n = get_unop_op(pred);
1035 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1038 } /* equivalent_node_idempotent_unop */
1040 /** Optimize Not(Not(x)) == x. */
1041 #define equivalent_node_Not equivalent_node_idempotent_unop
1043 /** -(-x) == x ??? Is this possible or can --x raise an
1044 out of bounds exception if min =! max? */
1045 #define equivalent_node_Minus equivalent_node_idempotent_unop
1048 * Optimize a * 1 = 1 * a = a.
1050 static ir_node *equivalent_node_Mul(ir_node *n) {
1052 ir_node *a = get_Mul_left(n);
1054 /* we can handle here only the n * n = n bit cases */
1055 if (get_irn_mode(n) == get_irn_mode(a)) {
1056 ir_node *b = get_Mul_right(n);
1058 /* Mul is commutative and has again an other neutral element. */
1059 if (is_Const(a) && is_Const_one(a)) {
1061 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1062 } else if (is_Const(b) && is_Const_one(b)) {
1064 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1068 } /* equivalent_node_Mul */
1071 * Optimize a / 1 = a.
1073 static ir_node *equivalent_node_Div(ir_node *n) {
1074 ir_node *a = get_Div_left(n);
1075 ir_node *b = get_Div_right(n);
1077 /* Div is not commutative. */
1078 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1079 /* Turn Div into a tuple (mem, bad, a) */
1080 ir_node *mem = get_Div_mem(n);
1081 ir_node *blk = get_irn_n(n, -1);
1082 turn_into_tuple(n, pn_Div_max);
1083 set_Tuple_pred(n, pn_Div_M, mem);
1084 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1085 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1086 set_Tuple_pred(n, pn_Div_res, a);
1089 } /* equivalent_node_Div */
1092 * Optimize a / 1.0 = a.
1094 static ir_node *equivalent_node_Quot(ir_node *n) {
1095 ir_node *a = get_Quot_left(n);
1096 ir_node *b = get_Quot_right(n);
1098 /* Div is not commutative. */
1099 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1100 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1101 ir_node *mem = get_Quot_mem(n);
1102 ir_node *blk = get_irn_n(n, -1);
1103 turn_into_tuple(n, pn_Quot_max);
1104 set_Tuple_pred(n, pn_Quot_M, mem);
1105 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1106 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1107 set_Tuple_pred(n, pn_Quot_res, a);
1110 } /* equivalent_node_Quot */
1113 * Optimize a / 1 = a.
1115 static ir_node *equivalent_node_DivMod(ir_node *n) {
1116 ir_node *b = get_DivMod_right(n);
1118 /* Div is not commutative. */
1119 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1120 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1121 ir_node *a = get_DivMod_left(n);
1122 ir_node *mem = get_Div_mem(n);
1123 ir_node *blk = get_irn_n(n, -1);
1124 ir_mode *mode = get_DivMod_resmode(n);
1126 turn_into_tuple(n, pn_DivMod_max);
1127 set_Tuple_pred(n, pn_DivMod_M, mem);
1128 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1129 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1130 set_Tuple_pred(n, pn_DivMod_res_div, a);
1131 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1134 } /* equivalent_node_DivMod */
1137 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1139 static ir_node *equivalent_node_Or(ir_node *n) {
1142 ir_node *a = get_Or_left(n);
1143 ir_node *b = get_Or_right(n);
1146 n = a; /* Or has it's own neutral element */
1147 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1148 } else if (is_Const(a) && is_Const_null(a)) {
1150 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1151 } else if (is_Const(b) && is_Const_null(b)) {
1153 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1157 } /* equivalent_node_Or */
1160 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1162 static ir_node *equivalent_node_And(ir_node *n) {
1165 ir_node *a = get_And_left(n);
1166 ir_node *b = get_And_right(n);
1169 n = a; /* And has it's own neutral element */
1170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1173 if (is_Const(a) && is_Const_all_one(a)) {
1175 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1178 if (is_Const(b) && is_Const_all_one(b)) {
1180 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1184 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1192 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1195 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1201 } /* equivalent_node_And */
1204 * Try to remove useless Conv's:
1206 static ir_node *equivalent_node_Conv(ir_node *n) {
1208 ir_node *a = get_Conv_op(n);
1210 ir_mode *n_mode = get_irn_mode(n);
1211 ir_mode *a_mode = get_irn_mode(a);
1213 if (n_mode == a_mode) { /* No Conv necessary */
1214 if (get_Conv_strict(n)) {
1215 /* special case: the predecessor might be a also a Conv */
1217 if (! get_Conv_strict(a)) {
1218 /* first one is not strict, kick it */
1219 set_Conv_op(n, get_Conv_op(a));
1222 /* else both are strict conv, second is superflous */
1223 } else if(is_Proj(a)) {
1224 ir_node *pred = get_Proj_pred(a);
1226 /* loads always return with the exact precision of n_mode */
1227 assert(get_Load_mode(pred) == n_mode);
1232 /* leave strict floating point Conv's */
1236 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1237 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1238 ir_node *b = get_Conv_op(a);
1239 ir_mode *b_mode = get_irn_mode(b);
1241 if (n_mode == b_mode) {
1242 if (n_mode == mode_b) {
1243 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1244 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1245 } else if (mode_is_int(n_mode)) {
1246 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1247 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1248 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1254 } /* equivalent_node_Conv */
1257 * A Cast may be removed if the type of the previous node
1258 * is already the type of the Cast.
1260 static ir_node *equivalent_node_Cast(ir_node *n) {
1262 ir_node *pred = get_Cast_op(n);
1264 if (get_irn_type(pred) == get_Cast_type(n)) {
1266 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1269 } /* equivalent_node_Cast */
1272 * Several optimizations:
1273 * - no Phi in start block.
1274 * - remove Id operators that are inputs to Phi
1275 * - fold Phi-nodes, iff they have only one predecessor except
1278 static ir_node *equivalent_node_Phi(ir_node *n) {
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_normalize()) return n;
1287 n_preds = get_Phi_n_preds(n);
1289 block = get_nodes_block(n);
1290 if ((is_Block_dead(block)) || /* Control dead */
1291 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1292 return new_Bad(); /* in the Start Block. */
1294 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1296 /* If the Block has a Bad pred, we also have one. */
1297 for (i = 0; i < n_preds; ++i)
1298 if (is_Bad(get_Block_cfgpred(block, i)))
1299 set_Phi_pred(n, i, new_Bad());
1301 /* Find first non-self-referencing input */
1302 for (i = 0; i < n_preds; ++i) {
1303 first_val = get_Phi_pred(n, i);
1304 if ( (first_val != n) /* not self pointer */
1306 && (! is_Bad(first_val))
1308 ) { /* value not dead */
1309 break; /* then found first value. */
1314 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1318 /* search for rest of inputs, determine if any of these
1319 are non-self-referencing */
1320 while (++i < n_preds) {
1321 ir_node *scnd_val = get_Phi_pred(n, i);
1322 if ( (scnd_val != n)
1323 && (scnd_val != first_val)
1325 && (! is_Bad(scnd_val))
1333 /* Fold, if no multiple distinct non-self-referencing inputs */
1335 DBG_OPT_PHI(oldn, n);
1338 } /* equivalent_node_Phi */
1341 * Several optimizations:
1342 * - no Sync in start block.
1343 * - fold Sync-nodes, iff they have only one predecessor except
1346 static ir_node *equivalent_node_Sync(ir_node *n) {
1347 int arity = get_Sync_n_preds(n);
1350 for (i = 0; i < arity;) {
1351 ir_node *pred = get_Sync_pred(n, i);
1354 /* Remove Bad predecessors */
1361 /* Remove duplicate predecessors */
1367 if (get_Sync_pred(n, j) == pred) {
1375 if (arity == 0) return new_Bad();
1376 if (arity == 1) return get_Sync_pred(n, 0);
1378 } /* equivalent_node_Sync */
1381 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1382 * ProjX(Load) and ProjX(Store).
1384 static ir_node *equivalent_node_Proj(ir_node *proj) {
1385 ir_node *oldn = proj;
1386 ir_node *a = get_Proj_pred(proj);
1389 /* Remove the Tuple/Proj combination. */
1390 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1391 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1392 DBG_OPT_TUPLE(oldn, a, proj);
1394 /* This should not happen! */
1395 assert(! "found a Proj with higher number than Tuple predecessors");
1398 } else if (get_irn_mode(proj) == mode_X) {
1399 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1400 /* Remove dead control flow -- early gigo(). */
1402 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1404 /* get the Load address */
1405 ir_node *addr = get_Load_ptr(a);
1406 ir_node *blk = get_irn_n(a, -1);
1409 if (value_not_null(addr, &confirm)) {
1410 if (confirm == NULL) {
1411 /* this node may float if it did not depend on a Confirm */
1412 set_irn_pinned(a, op_pin_state_floats);
1414 if (get_Proj_proj(proj) == pn_Load_X_except) {
1415 DBG_OPT_EXC_REM(proj);
1418 return new_r_Jmp(current_ir_graph, blk);
1420 } else if (is_Store(a)) {
1421 /* get the load/store address */
1422 ir_node *addr = get_Store_ptr(a);
1423 ir_node *blk = get_irn_n(a, -1);
1426 if (value_not_null(addr, &confirm)) {
1427 if (confirm == NULL) {
1428 /* this node may float if it did not depend on a Confirm */
1429 set_irn_pinned(a, op_pin_state_floats);
1431 if (get_Proj_proj(proj) == pn_Store_X_except) {
1432 DBG_OPT_EXC_REM(proj);
1435 return new_r_Jmp(current_ir_graph, blk);
1442 } /* equivalent_node_Proj */
1447 static ir_node *equivalent_node_Id(ir_node *n) {
1452 } while (get_irn_op(n) == op_Id);
1454 DBG_OPT_ID(oldn, n);
1456 } /* equivalent_node_Id */
1461 static ir_node *equivalent_node_Mux(ir_node *n)
1463 ir_node *oldn = n, *sel = get_Mux_sel(n);
1464 tarval *ts = value_of(sel);
1466 /* Mux(true, f, t) == t */
1467 if (ts == tarval_b_true) {
1468 n = get_Mux_true(n);
1469 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1471 /* Mux(false, f, t) == f */
1472 else if (ts == tarval_b_false) {
1473 n = get_Mux_false(n);
1474 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1476 /* Mux(v, x, x) == x */
1477 else if (get_Mux_false(n) == get_Mux_true(n)) {
1478 n = get_Mux_true(n);
1479 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1481 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1482 ir_node *cmp = get_Proj_pred(sel);
1483 long proj_nr = get_Proj_proj(sel);
1484 ir_node *f = get_Mux_false(n);
1485 ir_node *t = get_Mux_true(n);
1488 * Note further that these optimization work even for floating point
1489 * with NaN's because -NaN == NaN.
1490 * However, if +0 and -0 is handled differently, we cannot use the first one.
1493 ir_node *const cmp_l = get_Cmp_left(cmp);
1494 ir_node *const cmp_r = get_Cmp_right(cmp);
1498 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1499 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1508 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1509 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1511 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1518 * Note: normalization puts the constant on the right side,
1519 * so we check only one case.
1521 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1522 /* Mux(t CMP 0, X, t) */
1523 if (is_Minus(f) && get_Minus_op(f) == t) {
1524 /* Mux(t CMP 0, -t, t) */
1525 if (proj_nr == pn_Cmp_Eq) {
1526 /* Mux(t == 0, -t, t) ==> -t */
1528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1529 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1530 /* Mux(t != 0, -t, t) ==> t */
1532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1539 } /* equivalent_node_Mux */
1542 * Returns a equivalent node of a Psi: if a condition is true
1543 * and all previous conditions are false we know its value.
1544 * If all conditions are false its value is the default one.
1546 static ir_node *equivalent_node_Psi(ir_node *n) {
1548 return equivalent_node_Mux(n);
1550 } /* equivalent_node_Psi */
1553 * Optimize -a CMP -b into b CMP a.
1554 * This works only for for modes where unary Minus
1556 * Note that two-complement integers can Overflow
1557 * so it will NOT work.
1559 * For == and != can be handled in Proj(Cmp)
1561 static ir_node *equivalent_node_Cmp(ir_node *n) {
1562 ir_node *left = get_Cmp_left(n);
1563 ir_node *right = get_Cmp_right(n);
1565 if (is_Minus(left) && is_Minus(right) &&
1566 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1567 left = get_Minus_op(left);
1568 right = get_Minus_op(right);
1569 set_Cmp_left(n, right);
1570 set_Cmp_right(n, left);
1573 } /* equivalent_node_Cmp */
1576 * Remove Confirm nodes if setting is on.
1577 * Replace Confirms(x, '=', Constlike) by Constlike.
1579 static ir_node *equivalent_node_Confirm(ir_node *n) {
1580 ir_node *pred = get_Confirm_value(n);
1581 pn_Cmp pnc = get_Confirm_cmp(n);
1583 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1585 * rare case: two identical Confirms one after another,
1586 * replace the second one with the first.
1590 if (pnc == pn_Cmp_Eq) {
1591 ir_node *bound = get_Confirm_bound(n);
1594 * Optimize a rare case:
1595 * Confirm(x, '=', Constlike) ==> Constlike
1597 if (is_irn_constlike(bound)) {
1598 DBG_OPT_CONFIRM(n, bound);
1602 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1606 * Optimize CopyB(mem, x, x) into a Nop.
1608 static ir_node *equivalent_node_CopyB(ir_node *n) {
1609 ir_node *a = get_CopyB_dst(n);
1610 ir_node *b = get_CopyB_src(n);
1613 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1614 ir_node *mem = get_CopyB_mem(n);
1615 ir_node *blk = get_nodes_block(n);
1616 turn_into_tuple(n, pn_CopyB_max);
1617 set_Tuple_pred(n, pn_CopyB_M, mem);
1618 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1619 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1620 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1623 } /* equivalent_node_CopyB */
1626 * Optimize Bounds(idx, idx, upper) into idx.
1628 static ir_node *equivalent_node_Bound(ir_node *n) {
1629 ir_node *idx = get_Bound_index(n);
1630 ir_node *pred = skip_Proj(idx);
1633 if (is_Bound(pred)) {
1635 * idx was Bounds checked in the same MacroBlock previously,
1636 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1638 ir_node *lower = get_Bound_lower(n);
1639 ir_node *upper = get_Bound_upper(n);
1640 if (get_Bound_lower(pred) == lower &&
1641 get_Bound_upper(pred) == upper &&
1642 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1644 * One could expect that we simply return the previous
1645 * Bound here. However, this would be wrong, as we could
1646 * add an exception Proj to a new location then.
1647 * So, we must turn in into a tuple.
1653 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1654 ir_node *mem = get_Bound_mem(n);
1655 ir_node *blk = get_nodes_block(n);
1656 turn_into_tuple(n, pn_Bound_max);
1657 set_Tuple_pred(n, pn_Bound_M, mem);
1658 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1659 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1660 set_Tuple_pred(n, pn_Bound_res, idx);
1663 } /* equivalent_node_Bound */
1666 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1667 * perform no actual computation, as, e.g., the Id nodes. It does not create
1668 * new nodes. It is therefore safe to free n if the node returned is not n.
1669 * If a node returns a Tuple we can not just skip it. If the size of the
1670 * in array fits, we transform n into a tuple (e.g., Div).
1672 ir_node *equivalent_node(ir_node *n) {
1673 if (n->op->ops.equivalent_node)
1674 return n->op->ops.equivalent_node(n);
1676 } /* equivalent_node */
1679 * Sets the default equivalent node operation for an ir_op_ops.
1681 * @param code the opcode for the default operation
1682 * @param ops the operations initialized
1687 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1691 ops->equivalent_node = equivalent_node_##a; \
1731 } /* firm_set_default_equivalent_node */
1734 * Returns non-zero if a node is a Phi node
1735 * with all predecessors constant.
1737 static int is_const_Phi(ir_node *n) {
1740 if (! is_Phi(n) || get_irn_arity(n) == 0)
1742 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1743 if (! is_Const(get_irn_n(n, i)))
1746 } /* is_const_Phi */
1749 * Apply an evaluator on a binop with a constant operators (and one Phi).
1751 * @param phi the Phi node
1752 * @param other the other operand
1753 * @param eval an evaluator function
1754 * @param mode the mode of the result, may be different from the mode of the Phi!
1755 * @param left if non-zero, other is the left operand, else the right
1757 * @return a new Phi node if the conversion was successful, NULL else
1759 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1764 int i, n = get_irn_arity(phi);
1766 NEW_ARR_A(void *, res, n);
1768 for (i = 0; i < n; ++i) {
1769 pred = get_irn_n(phi, i);
1770 tv = get_Const_tarval(pred);
1771 tv = eval(other, tv);
1773 if (tv == tarval_bad) {
1774 /* folding failed, bad */
1780 for (i = 0; i < n; ++i) {
1781 pred = get_irn_n(phi, i);
1782 tv = get_Const_tarval(pred);
1783 tv = eval(tv, other);
1785 if (tv == tarval_bad) {
1786 /* folding failed, bad */
1792 irg = current_ir_graph;
1793 for (i = 0; i < n; ++i) {
1794 pred = get_irn_n(phi, i);
1795 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1796 mode, res[i], get_Const_type(pred));
1798 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1799 } /* apply_binop_on_phi */
1802 * Apply an evaluator on a binop with two constant Phi.
1804 * @param a the left Phi node
1805 * @param b the right Phi node
1806 * @param eval an evaluator function
1807 * @param mode the mode of the result, may be different from the mode of the Phi!
1809 * @return a new Phi node if the conversion was successful, NULL else
1811 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1812 tarval *tv_l, *tv_r, *tv;
1818 if (get_nodes_block(a) != get_nodes_block(b))
1821 n = get_irn_arity(a);
1822 NEW_ARR_A(void *, res, n);
1824 for (i = 0; i < n; ++i) {
1825 pred = get_irn_n(a, i);
1826 tv_l = get_Const_tarval(pred);
1827 pred = get_irn_n(b, i);
1828 tv_r = get_Const_tarval(pred);
1829 tv = eval(tv_l, tv_r);
1831 if (tv == tarval_bad) {
1832 /* folding failed, bad */
1837 irg = current_ir_graph;
1838 for (i = 0; i < n; ++i) {
1839 pred = get_irn_n(a, i);
1840 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1842 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1843 } /* apply_binop_on_2_phis */
1846 * Apply an evaluator on a unop with a constant operator (a Phi).
1848 * @param phi the Phi node
1849 * @param eval an evaluator function
1851 * @return a new Phi node if the conversion was successful, NULL else
1853 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1859 int i, n = get_irn_arity(phi);
1861 NEW_ARR_A(void *, res, n);
1862 for (i = 0; i < n; ++i) {
1863 pred = get_irn_n(phi, i);
1864 tv = get_Const_tarval(pred);
1867 if (tv == tarval_bad) {
1868 /* folding failed, bad */
1873 mode = get_irn_mode(phi);
1874 irg = current_ir_graph;
1875 for (i = 0; i < n; ++i) {
1876 pred = get_irn_n(phi, i);
1877 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1878 mode, res[i], get_Const_type(pred));
1880 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1881 } /* apply_unop_on_phi */
1884 * Apply a conversion on a constant operator (a Phi).
1886 * @param phi the Phi node
1888 * @return a new Phi node if the conversion was successful, NULL else
1890 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1895 int i, n = get_irn_arity(phi);
1897 NEW_ARR_A(void *, res, n);
1898 for (i = 0; i < n; ++i) {
1899 pred = get_irn_n(phi, i);
1900 tv = get_Const_tarval(pred);
1901 tv = tarval_convert_to(tv, mode);
1903 if (tv == tarval_bad) {
1904 /* folding failed, bad */
1909 irg = current_ir_graph;
1910 for (i = 0; i < n; ++i) {
1911 pred = get_irn_n(phi, i);
1912 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1913 mode, res[i], get_Const_type(pred));
1915 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1916 } /* apply_conv_on_phi */
1919 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1920 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1921 * If possible, remove the Conv's.
1923 static ir_node *transform_node_AddSub(ir_node *n) {
1924 ir_mode *mode = get_irn_mode(n);
1926 if (mode_is_reference(mode)) {
1927 ir_node *left = get_binop_left(n);
1928 ir_node *right = get_binop_right(n);
1929 unsigned ref_bits = get_mode_size_bits(mode);
1931 if (is_Conv(left)) {
1932 ir_mode *lmode = get_irn_mode(left);
1933 unsigned bits = get_mode_size_bits(lmode);
1935 if (ref_bits == bits &&
1936 mode_is_int(lmode) &&
1937 get_mode_arithmetic(lmode) == irma_twos_complement) {
1938 ir_node *pre = get_Conv_op(left);
1939 ir_mode *pre_mode = get_irn_mode(pre);
1941 if (mode_is_int(pre_mode) &&
1942 get_mode_size_bits(pre_mode) == bits &&
1943 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1944 /* ok, this conv just changes to sign, moreover the calculation
1945 * is done with same number of bits as our address mode, so
1946 * we can ignore the conv as address calculation can be viewed
1947 * as either signed or unsigned
1949 set_binop_left(n, pre);
1954 if (is_Conv(right)) {
1955 ir_mode *rmode = get_irn_mode(right);
1956 unsigned bits = get_mode_size_bits(rmode);
1958 if (ref_bits == bits &&
1959 mode_is_int(rmode) &&
1960 get_mode_arithmetic(rmode) == irma_twos_complement) {
1961 ir_node *pre = get_Conv_op(right);
1962 ir_mode *pre_mode = get_irn_mode(pre);
1964 if (mode_is_int(pre_mode) &&
1965 get_mode_size_bits(pre_mode) == bits &&
1966 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1967 /* ok, this conv just changes to sign, moreover the calculation
1968 * is done with same number of bits as our address mode, so
1969 * we can ignore the conv as address calculation can be viewed
1970 * as either signed or unsigned
1972 set_binop_right(n, pre);
1977 /* let address arithmetic use unsigned modes */
1978 if (is_Const(right)) {
1979 ir_mode *rmode = get_irn_mode(right);
1981 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1982 /* convert a AddP(P, *s) into AddP(P, *u) */
1983 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1985 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
1986 set_binop_right(n, pre);
1991 } /* transform_node_AddSub */
1993 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1995 if (is_Const(b) && is_const_Phi(a)) { \
1996 /* check for Op(Phi, Const) */ \
1997 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1999 else if (is_Const(a) && is_const_Phi(b)) { \
2000 /* check for Op(Const, Phi) */ \
2001 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2003 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2004 /* check for Op(Phi, Phi) */ \
2005 c = apply_binop_on_2_phis(a, b, eval, mode); \
2008 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2012 #define HANDLE_UNOP_PHI(eval, a, c) \
2014 if (is_const_Phi(a)) { \
2015 /* check for Op(Phi) */ \
2016 c = apply_unop_on_phi(a, eval); \
2018 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2024 * Do the AddSub optimization, then Transform
2025 * Constant folding on Phi
2026 * Add(a,a) -> Mul(a, 2)
2027 * Add(Mul(a, x), a) -> Mul(a, x+1)
2028 * if the mode is integer or float.
2029 * Transform Add(a,-b) into Sub(a,b).
2030 * Reassociation might fold this further.
2032 static ir_node *transform_node_Add(ir_node *n) {
2034 ir_node *a, *b, *c, *oldn = n;
2036 n = transform_node_AddSub(n);
2038 a = get_Add_left(n);
2039 b = get_Add_right(n);
2041 mode = get_irn_mode(n);
2042 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2044 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2045 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2048 if (mode_is_num(mode)) {
2049 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2050 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2051 ir_node *block = get_irn_n(n, -1);
2054 get_irn_dbg_info(n),
2058 new_r_Const_long(current_ir_graph, block, mode, 2),
2060 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2065 get_irn_dbg_info(n),
2071 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2076 get_irn_dbg_info(n),
2082 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2085 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2086 /* Here we rely on constants be on the RIGHT side */
2088 ir_node *op = get_Not_op(a);
2090 if (is_Const(b) && is_Const_one(b)) {
2092 ir_node *blk = get_irn_n(n, -1);
2093 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2094 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2099 ir_node *blk = get_irn_n(n, -1);
2100 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2106 ir_node *op = get_Not_op(b);
2110 ir_node *blk = get_irn_n(n, -1);
2111 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2112 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2119 } /* transform_node_Add */
2122 * returns -cnst or NULL if impossible
2124 static ir_node *const_negate(ir_node *cnst) {
2125 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2126 dbg_info *dbgi = get_irn_dbg_info(cnst);
2127 ir_graph *irg = get_irn_irg(cnst);
2128 ir_node *block = get_nodes_block(cnst);
2129 ir_mode *mode = get_irn_mode(cnst);
2130 if (tv == tarval_bad) return NULL;
2131 return new_rd_Const(dbgi, irg, block, mode, tv);
2135 * Do the AddSub optimization, then Transform
2136 * Constant folding on Phi
2137 * Sub(0,a) -> Minus(a)
2138 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2139 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2140 * Sub(Add(a, x), x) -> a
2141 * Sub(x, Add(x, a)) -> -a
2142 * Sub(x, Const) -> Add(x, -Const)
2144 static ir_node *transform_node_Sub(ir_node *n) {
2149 n = transform_node_AddSub(n);
2151 a = get_Sub_left(n);
2152 b = get_Sub_right(n);
2154 mode = get_irn_mode(n);
2157 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2159 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2160 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2163 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2164 /* a - C -> a + (-C) */
2165 ir_node *cnst = const_negate(b);
2167 ir_node *block = get_nodes_block(n);
2168 dbg_info *dbgi = get_irn_dbg_info(n);
2169 ir_graph *irg = get_irn_irg(n);
2171 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2172 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2177 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2178 ir_graph *irg = current_ir_graph;
2179 dbg_info *dbg = get_irn_dbg_info(n);
2180 ir_node *block = get_nodes_block(n);
2181 ir_node *left = get_Minus_op(a);
2182 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2184 n = new_rd_Minus(dbg, irg, block, add, mode);
2185 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2187 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2188 ir_graph *irg = current_ir_graph;
2189 dbg_info *dbg = get_irn_dbg_info(n);
2190 ir_node *block = get_nodes_block(n);
2191 ir_node *right = get_Minus_op(b);
2193 n = new_rd_Add(dbg, irg, block, a, right, mode);
2194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2196 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2197 ir_graph *irg = current_ir_graph;
2198 dbg_info *s_dbg = get_irn_dbg_info(b);
2199 ir_node *s_block = get_nodes_block(b);
2200 ir_node *s_left = get_Sub_right(b);
2201 ir_node *s_right = get_Sub_left(b);
2202 ir_mode *s_mode = get_irn_mode(b);
2203 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2204 dbg_info *a_dbg = get_irn_dbg_info(n);
2205 ir_node *a_block = get_nodes_block(n);
2207 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2210 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2211 ir_node *m_right = get_Mul_right(b);
2212 if (is_Const(m_right)) {
2213 ir_node *cnst2 = const_negate(m_right);
2214 if (cnst2 != NULL) {
2215 ir_graph *irg = current_ir_graph;
2216 dbg_info *m_dbg = get_irn_dbg_info(b);
2217 ir_node *m_block = get_nodes_block(b);
2218 ir_node *m_left = get_Mul_left(b);
2219 ir_mode *m_mode = get_irn_mode(b);
2220 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2221 dbg_info *a_dbg = get_irn_dbg_info(n);
2222 ir_node *a_block = get_nodes_block(n);
2224 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2225 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2231 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2232 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2234 get_irn_dbg_info(n),
2239 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2243 if (mode_wrap_around(mode)) {
2244 ir_node *left = get_Add_left(a);
2245 ir_node *right = get_Add_right(a);
2247 /* FIXME: Does the Conv's work only for two complement or generally? */
2249 if (mode != get_irn_mode(right)) {
2250 /* This Sub is an effective Cast */
2251 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2254 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2256 } else if (right == b) {
2257 if (mode != get_irn_mode(left)) {
2258 /* This Sub is an effective Cast */
2259 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2262 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2268 if (mode_wrap_around(mode)) {
2269 ir_node *left = get_Add_left(b);
2270 ir_node *right = get_Add_right(b);
2272 /* FIXME: Does the Conv's work only for two complement or generally? */
2274 ir_mode *r_mode = get_irn_mode(right);
2276 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2277 if (mode != r_mode) {
2278 /* This Sub is an effective Cast */
2279 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2281 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2283 } else if (right == a) {
2284 ir_mode *l_mode = get_irn_mode(left);
2286 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2287 if (mode != l_mode) {
2288 /* This Sub is an effective Cast */
2289 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2291 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2296 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2297 ir_mode *mode = get_irn_mode(a);
2299 if (mode == get_irn_mode(b)) {
2301 ir_node *op_a = get_Conv_op(a);
2302 ir_node *op_b = get_Conv_op(b);
2304 /* check if it's allowed to skip the conv */
2305 ma = get_irn_mode(op_a);
2306 mb = get_irn_mode(op_b);
2308 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2309 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2312 set_Sub_right(n, b);
2318 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2319 if (!is_reassoc_running() && is_Mul(a)) {
2320 ir_node *ma = get_Mul_left(a);
2321 ir_node *mb = get_Mul_right(a);
2324 ir_node *blk = get_irn_n(n, -1);
2326 get_irn_dbg_info(n),
2327 current_ir_graph, blk,
2330 get_irn_dbg_info(n),
2331 current_ir_graph, blk,
2333 new_r_Const_long(current_ir_graph, blk, mode, 1),
2336 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2338 } else if (mb == b) {
2339 ir_node *blk = get_irn_n(n, -1);
2341 get_irn_dbg_info(n),
2342 current_ir_graph, blk,
2345 get_irn_dbg_info(n),
2346 current_ir_graph, blk,
2348 new_r_Const_long(current_ir_graph, blk, mode, 1),
2351 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2355 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2356 ir_node *x = get_Sub_left(a);
2357 ir_node *y = get_Sub_right(a);
2358 ir_node *blk = get_irn_n(n, -1);
2359 ir_mode *m_b = get_irn_mode(b);
2360 ir_mode *m_y = get_irn_mode(y);
2364 /* Determine the right mode for the Add. */
2367 else if (mode_is_reference(m_b))
2369 else if (mode_is_reference(m_y))
2373 * Both modes are different but none is reference,
2374 * happens for instance in SubP(SubP(P, Iu), Is).
2375 * We have two possibilities here: Cast or ignore.
2376 * Currently we ignore this case.
2381 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2383 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2384 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2388 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2389 if (is_Const(a) && is_Not(b)) {
2390 /* c - ~X = X + (c+1) */
2391 tarval *tv = get_Const_tarval(a);
2393 tv = tarval_add(tv, get_mode_one(mode));
2394 if (tv != tarval_bad) {
2395 ir_node *blk = get_irn_n(n, -1);
2396 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2397 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2398 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2404 } /* transform_node_Sub */
2407 * Several transformation done on n*n=2n bits mul.
2408 * These transformations must be done here because new nodes may be produced.
2410 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2412 ir_node *a = get_Mul_left(n);
2413 ir_node *b = get_Mul_right(n);
2414 tarval *ta = value_of(a);
2415 tarval *tb = value_of(b);
2416 ir_mode *smode = get_irn_mode(a);
2418 if (ta == get_mode_one(smode)) {
2419 /* (L)1 * (L)b = (L)b */
2420 ir_node *blk = get_irn_n(n, -1);
2421 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2422 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2425 else if (ta == get_mode_minus_one(smode)) {
2426 /* (L)-1 * (L)b = (L)b */
2427 ir_node *blk = get_irn_n(n, -1);
2428 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2429 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2430 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2433 if (tb == get_mode_one(smode)) {
2434 /* (L)a * (L)1 = (L)a */
2435 ir_node *blk = get_irn_n(a, -1);
2436 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2437 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2440 else if (tb == get_mode_minus_one(smode)) {
2441 /* (L)a * (L)-1 = (L)-a */
2442 ir_node *blk = get_irn_n(n, -1);
2443 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2444 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2452 * Transform Mul(a,-1) into -a.
2453 * Do constant evaluation of Phi nodes.
2454 * Do architecture dependent optimizations on Mul nodes
2456 static ir_node *transform_node_Mul(ir_node *n) {
2457 ir_node *c, *oldn = n;
2458 ir_mode *mode = get_irn_mode(n);
2459 ir_node *a = get_Mul_left(n);
2460 ir_node *b = get_Mul_right(n);
2462 if (is_Bad(a) || is_Bad(b))
2465 if (mode != get_irn_mode(a))
2466 return transform_node_Mul2n(n, mode);
2468 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2470 if (mode_is_signed(mode)) {
2473 if (value_of(a) == get_mode_minus_one(mode))
2475 else if (value_of(b) == get_mode_minus_one(mode))
2478 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2479 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2484 if (is_Const(b)) { /* (-a) * const -> a * -const */
2485 ir_node *cnst = const_negate(b);
2487 dbg_info *dbgi = get_irn_dbg_info(n);
2488 ir_node *block = get_nodes_block(n);
2489 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2490 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2493 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2494 dbg_info *dbgi = get_irn_dbg_info(n);
2495 ir_node *block = get_nodes_block(n);
2496 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2497 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2499 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2500 ir_node *sub_l = get_Sub_left(b);
2501 ir_node *sub_r = get_Sub_right(b);
2502 dbg_info *dbgi = get_irn_dbg_info(n);
2503 ir_graph *irg = current_ir_graph;
2504 ir_node *block = get_nodes_block(n);
2505 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2506 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2507 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2510 } else if (is_Minus(b)) {
2511 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2512 ir_node *sub_l = get_Sub_left(a);
2513 ir_node *sub_r = get_Sub_right(a);
2514 dbg_info *dbgi = get_irn_dbg_info(n);
2515 ir_graph *irg = current_ir_graph;
2516 ir_node *block = get_nodes_block(n);
2517 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2518 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2519 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2523 if (get_mode_arithmetic(mode) == irma_ieee754) {
2525 tarval *tv = get_Const_tarval(a);
2526 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2527 /* 2.0 * b = b + b */
2528 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2529 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2533 else if (is_Const(b)) {
2534 tarval *tv = get_Const_tarval(b);
2535 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2536 /* a * 2.0 = a + a */
2537 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2538 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2543 return arch_dep_replace_mul_with_shifts(n);
2544 } /* transform_node_Mul */
2547 * Transform a Div Node.
2549 static ir_node *transform_node_Div(ir_node *n) {
2550 ir_mode *mode = get_Div_resmode(n);
2551 ir_node *a = get_Div_left(n);
2552 ir_node *b = get_Div_right(n);
2556 if (is_Const(b) && is_const_Phi(a)) {
2557 /* check for Div(Phi, Const) */
2558 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2560 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2564 else if (is_Const(a) && is_const_Phi(b)) {
2565 /* check for Div(Const, Phi) */
2566 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2568 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2572 else if (is_const_Phi(a) && is_const_Phi(b)) {
2573 /* check for Div(Phi, Phi) */
2574 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2576 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2583 if (tv != tarval_bad) {
2584 value = new_Const(get_tarval_mode(tv), tv);
2586 DBG_OPT_CSTEVAL(n, value);
2589 ir_node *a = get_Div_left(n);
2590 ir_node *b = get_Div_right(n);
2593 if (a == b && value_not_zero(a, &dummy)) {
2594 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2595 value = new_Const(mode, get_mode_one(mode));
2596 DBG_OPT_CSTEVAL(n, value);
2599 if (mode_is_signed(mode) && is_Const(b)) {
2600 tarval *tv = get_Const_tarval(b);
2602 if (tv == get_mode_minus_one(mode)) {
2604 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2605 DBG_OPT_CSTEVAL(n, value);
2609 /* Try architecture dependent optimization */
2610 value = arch_dep_replace_div_by_const(n);
2618 /* Turn Div into a tuple (mem, jmp, bad, value) */
2619 mem = get_Div_mem(n);
2620 blk = get_irn_n(n, -1);
2622 /* skip a potential Pin */
2624 mem = get_Pin_op(mem);
2625 turn_into_tuple(n, pn_Div_max);
2626 set_Tuple_pred(n, pn_Div_M, mem);
2627 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2628 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2629 set_Tuple_pred(n, pn_Div_res, value);
2632 } /* transform_node_Div */
2635 * Transform a Mod node.
2637 static ir_node *transform_node_Mod(ir_node *n) {
2638 ir_mode *mode = get_Mod_resmode(n);
2639 ir_node *a = get_Mod_left(n);
2640 ir_node *b = get_Mod_right(n);
2644 if (is_Const(b) && is_const_Phi(a)) {
2645 /* check for Div(Phi, Const) */
2646 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2648 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2652 else if (is_Const(a) && is_const_Phi(b)) {
2653 /* check for Div(Const, Phi) */
2654 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2656 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2660 else if (is_const_Phi(a) && is_const_Phi(b)) {
2661 /* check for Div(Phi, Phi) */
2662 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2664 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2671 if (tv != tarval_bad) {
2672 value = new_Const(get_tarval_mode(tv), tv);
2674 DBG_OPT_CSTEVAL(n, value);
2677 ir_node *a = get_Mod_left(n);
2678 ir_node *b = get_Mod_right(n);
2681 if (a == b && value_not_zero(a, &dummy)) {
2682 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2683 value = new_Const(mode, get_mode_null(mode));
2684 DBG_OPT_CSTEVAL(n, value);
2687 if (mode_is_signed(mode) && is_Const(b)) {
2688 tarval *tv = get_Const_tarval(b);
2690 if (tv == get_mode_minus_one(mode)) {
2692 value = new_Const(mode, get_mode_null(mode));
2693 DBG_OPT_CSTEVAL(n, value);
2697 /* Try architecture dependent optimization */
2698 value = arch_dep_replace_mod_by_const(n);
2706 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2707 mem = get_Mod_mem(n);
2708 blk = get_irn_n(n, -1);
2710 /* skip a potential Pin */
2712 mem = get_Pin_op(mem);
2713 turn_into_tuple(n, pn_Mod_max);
2714 set_Tuple_pred(n, pn_Mod_M, mem);
2715 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2716 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2717 set_Tuple_pred(n, pn_Mod_res, value);
2720 } /* transform_node_Mod */
2723 * Transform a DivMod node.
2725 static ir_node *transform_node_DivMod(ir_node *n) {
2727 ir_node *a = get_DivMod_left(n);
2728 ir_node *b = get_DivMod_right(n);
2729 ir_mode *mode = get_DivMod_resmode(n);
2734 if (is_Const(b) && is_const_Phi(a)) {
2735 /* check for Div(Phi, Const) */
2736 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2737 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2739 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2740 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2744 else if (is_Const(a) && is_const_Phi(b)) {
2745 /* check for Div(Const, Phi) */
2746 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2747 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2749 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2750 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2754 else if (is_const_Phi(a) && is_const_Phi(b)) {
2755 /* check for Div(Phi, Phi) */
2756 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2757 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2759 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2760 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2767 if (tb != tarval_bad) {
2768 if (tb == get_mode_one(get_tarval_mode(tb))) {
2770 vb = new_Const(mode, get_mode_null(mode));
2771 DBG_OPT_CSTEVAL(n, vb);
2773 } else if (ta != tarval_bad) {
2774 tarval *resa, *resb;
2775 resa = tarval_div(ta, tb);
2776 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2777 Jmp for X result!? */
2778 resb = tarval_mod(ta, tb);
2779 if (resb == tarval_bad) return n; /* Causes exception! */
2780 va = new_Const(mode, resa);
2781 vb = new_Const(mode, resb);
2782 DBG_OPT_CSTEVAL(n, va);
2783 DBG_OPT_CSTEVAL(n, vb);
2785 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2786 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2787 vb = new_Const(mode, get_mode_null(mode));
2788 DBG_OPT_CSTEVAL(n, va);
2789 DBG_OPT_CSTEVAL(n, vb);
2791 } else { /* Try architecture dependent optimization */
2794 arch_dep_replace_divmod_by_const(&va, &vb, n);
2795 evaluated = va != NULL;
2797 } else if (a == b) {
2798 if (value_not_zero(a, &dummy)) {
2800 va = new_Const(mode, get_mode_one(mode));
2801 vb = new_Const(mode, get_mode_null(mode));
2802 DBG_OPT_CSTEVAL(n, va);
2803 DBG_OPT_CSTEVAL(n, vb);
2806 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2809 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2810 /* 0 / non-Const = 0 */
2815 if (evaluated) { /* replace by tuple */
2819 mem = get_DivMod_mem(n);
2820 /* skip a potential Pin */
2822 mem = get_Pin_op(mem);
2824 blk = get_irn_n(n, -1);
2825 turn_into_tuple(n, pn_DivMod_max);
2826 set_Tuple_pred(n, pn_DivMod_M, mem);
2827 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2828 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2829 set_Tuple_pred(n, pn_DivMod_res_div, va);
2830 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2834 } /* transform_node_DivMod */
2837 * Optimize x / c to x * (1/c)
2839 static ir_node *transform_node_Quot(ir_node *n) {
2840 ir_mode *mode = get_Quot_resmode(n);
2843 if (get_mode_arithmetic(mode) == irma_ieee754) {
2844 ir_node *b = get_Quot_right(n);
2847 tarval *tv = get_Const_tarval(b);
2851 * Floating point constant folding might be disabled here to
2853 * However, as we check for exact result, doing it is safe.
2856 rem = tarval_enable_fp_ops(1);
2857 tv = tarval_quo(get_mode_one(mode), tv);
2858 (void)tarval_enable_fp_ops(rem);
2860 /* Do the transformation if the result is either exact or we are not
2861 using strict rules. */
2862 if (tv != tarval_bad &&
2863 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2864 ir_node *blk = get_irn_n(n, -1);
2865 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2866 ir_node *a = get_Quot_left(n);
2867 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2868 ir_node *mem = get_Quot_mem(n);
2870 /* skip a potential Pin */
2872 mem = get_Pin_op(mem);
2873 turn_into_tuple(n, pn_Quot_max);
2874 set_Tuple_pred(n, pn_Quot_M, mem);
2875 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2876 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2877 set_Tuple_pred(n, pn_Quot_res, m);
2878 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2883 } /* transform_node_Quot */
2886 * Optimize Abs(x) into x if x is Confirmed >= 0
2887 * Optimize Abs(x) into -x if x is Confirmed <= 0
2888 * Optimize Abs(-x) int Abs(x)
2890 static ir_node *transform_node_Abs(ir_node *n) {
2891 ir_node *c, *oldn = n;
2892 ir_node *a = get_Abs_op(n);
2895 HANDLE_UNOP_PHI(tarval_abs, a, c);
2897 switch (classify_value_sign(a)) {
2898 case value_classified_negative:
2899 mode = get_irn_mode(n);
2902 * We can replace the Abs by -x here.
2903 * We even could add a new Confirm here
2904 * (if not twos complement)
2906 * Note that -x would create a new node, so we could
2907 * not run it in the equivalent_node() context.
2909 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2910 get_nodes_block(n), a, mode);
2912 DBG_OPT_CONFIRM(oldn, n);
2914 case value_classified_positive:
2915 /* n is positive, Abs is not needed */
2918 DBG_OPT_CONFIRM(oldn, n);
2924 /* Abs(-x) = Abs(x) */
2925 mode = get_irn_mode(n);
2926 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2927 get_nodes_block(n), get_Minus_op(a), mode);
2928 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2932 } /* transform_node_Abs */
2935 * Transform a Cond node.
2937 * Replace the Cond by a Jmp if it branches on a constant
2940 static ir_node *transform_node_Cond(ir_node *n) {
2943 ir_node *a = get_Cond_selector(n);
2944 tarval *ta = value_of(a);
2946 /* we need block info which is not available in floating irgs */
2947 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2950 if ((ta != tarval_bad) &&
2951 (get_irn_mode(a) == mode_b) &&
2952 (get_opt_unreachable_code())) {
2953 /* It's a boolean Cond, branching on a boolean constant.
2954 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2955 ir_node *blk = get_nodes_block(n);
2956 jmp = new_r_Jmp(current_ir_graph, blk);
2957 turn_into_tuple(n, pn_Cond_max);
2958 if (ta == tarval_b_true) {
2959 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2960 set_Tuple_pred(n, pn_Cond_true, jmp);
2962 set_Tuple_pred(n, pn_Cond_false, jmp);
2963 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2965 /* We might generate an endless loop, so keep it alive. */
2966 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2969 } /* transform_node_Cond */
2972 * Prototype of a recursive transform function
2973 * for bitwise distributive transformations.
2975 typedef ir_node* (*recursive_transform)(ir_node *n);
2978 * makes use of distributive laws for and, or, eor
2979 * and(a OP c, b OP c) -> and(a, b) OP c
2980 * note, might return a different op than n
2982 static ir_node *transform_bitwise_distributive(ir_node *n,
2983 recursive_transform trans_func)
2986 ir_node *a = get_binop_left(n);
2987 ir_node *b = get_binop_right(n);
2988 ir_op *op = get_irn_op(a);
2989 ir_op *op_root = get_irn_op(n);
2991 if(op != get_irn_op(b))
2994 if (op == op_Conv) {
2995 ir_node *a_op = get_Conv_op(a);
2996 ir_node *b_op = get_Conv_op(b);
2997 ir_mode *a_mode = get_irn_mode(a_op);
2998 ir_mode *b_mode = get_irn_mode(b_op);
2999 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3000 ir_node *blk = get_irn_n(n, -1);
3003 set_binop_left(n, a_op);
3004 set_binop_right(n, b_op);
3005 set_irn_mode(n, a_mode);
3007 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3009 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3015 /* nothing to gain here */
3019 if (op == op_Shrs || op == op_Shr || op == op_Shl
3020 || op == op_And || op == op_Or || op == op_Eor) {
3021 ir_node *a_left = get_binop_left(a);
3022 ir_node *a_right = get_binop_right(a);
3023 ir_node *b_left = get_binop_left(b);
3024 ir_node *b_right = get_binop_right(b);
3026 ir_node *op1 = NULL;
3027 ir_node *op2 = NULL;
3029 if (is_op_commutative(op)) {
3030 if (a_left == b_left) {
3034 } else if(a_left == b_right) {
3038 } else if(a_right == b_left) {
3044 if(a_right == b_right) {
3051 /* (a sop c) & (b sop c) => (a & b) sop c */
3052 ir_node *blk = get_irn_n(n, -1);
3054 ir_node *new_n = exact_copy(n);
3055 set_binop_left(new_n, op1);
3056 set_binop_right(new_n, op2);
3057 new_n = trans_func(new_n);
3059 if(op_root == op_Eor && op == op_Or) {
3060 dbg_info *dbgi = get_irn_dbg_info(n);
3061 ir_graph *irg = current_ir_graph;
3062 ir_mode *mode = get_irn_mode(c);
3064 c = new_rd_Not(dbgi, irg, blk, c, mode);
3065 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3068 set_nodes_block(n, blk);
3069 set_binop_left(n, new_n);
3070 set_binop_right(n, c);
3071 add_identities(current_ir_graph->value_table, n);
3074 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3085 static ir_node *transform_node_And(ir_node *n) {
3086 ir_node *c, *oldn = n;
3087 ir_node *a = get_And_left(n);
3088 ir_node *b = get_And_right(n);
3091 mode = get_irn_mode(n);
3092 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3094 /* we can evaluate 2 Projs of the same Cmp */
3095 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3096 ir_node *pred_a = get_Proj_pred(a);
3097 ir_node *pred_b = get_Proj_pred(b);
3098 if (pred_a == pred_b) {
3099 dbg_info *dbgi = get_irn_dbg_info(n);
3100 ir_node *block = get_nodes_block(pred_a);
3101 pn_Cmp pn_a = get_Proj_proj(a);
3102 pn_Cmp pn_b = get_Proj_proj(b);
3103 /* yes, we can simply calculate with pncs */
3104 pn_Cmp new_pnc = pn_a & pn_b;
3106 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3111 ir_node *op = get_Not_op(b);
3113 ir_node *ba = get_And_left(op);
3114 ir_node *bb = get_And_right(op);
3116 /* it's enough to test the following cases due to normalization! */
3117 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3118 /* (a|b) & ~(a&b) = a^b */
3119 ir_node *block = get_nodes_block(n);
3121 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3122 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3130 ir_node *op = get_Not_op(a);
3132 ir_node *aa = get_And_left(op);
3133 ir_node *ab = get_And_right(op);
3135 /* it's enough to test the following cases due to normalization! */
3136 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3137 /* (a|b) & ~(a&b) = a^b */
3138 ir_node *block = get_nodes_block(n);
3140 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3148 ir_node *al = get_Eor_left(a);
3149 ir_node *ar = get_Eor_right(a);
3152 /* (b ^ a) & b -> ~a & b */
3153 dbg_info *dbg = get_irn_dbg_info(n);
3154 ir_node *block = get_nodes_block(n);
3156 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3157 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3158 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3162 /* (a ^ b) & b -> ~a & b */
3163 dbg_info *dbg = get_irn_dbg_info(n);
3164 ir_node *block = get_nodes_block(n);
3166 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3167 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3173 ir_node *bl = get_Eor_left(b);
3174 ir_node *br = get_Eor_right(b);
3177 /* a & (a ^ b) -> a & ~b */
3178 dbg_info *dbg = get_irn_dbg_info(n);
3179 ir_node *block = get_nodes_block(n);
3181 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3182 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3187 /* a & (b ^ a) -> a & ~b */
3188 dbg_info *dbg = get_irn_dbg_info(n);
3189 ir_node *block = get_nodes_block(n);
3191 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3192 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3193 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3197 if (is_Not(a) && is_Not(b)) {
3198 /* ~a & ~b = ~(a|b) */
3199 ir_node *block = get_nodes_block(n);
3200 ir_mode *mode = get_irn_mode(n);
3204 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3205 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3206 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3210 n = transform_bitwise_distributive(n, transform_node_And);
3213 } /* transform_node_And */
3218 static ir_node *transform_node_Eor(ir_node *n) {
3219 ir_node *c, *oldn = n;
3220 ir_node *a = get_Eor_left(n);
3221 ir_node *b = get_Eor_right(n);
3222 ir_mode *mode = get_irn_mode(n);
3224 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3226 /* we can evaluate 2 Projs of the same Cmp */
3227 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3228 ir_node *pred_a = get_Proj_pred(a);
3229 ir_node *pred_b = get_Proj_pred(b);
3230 if(pred_a == pred_b) {
3231 dbg_info *dbgi = get_irn_dbg_info(n);
3232 ir_node *block = get_nodes_block(pred_a);
3233 pn_Cmp pn_a = get_Proj_proj(a);
3234 pn_Cmp pn_b = get_Proj_proj(b);
3235 /* yes, we can simply calculate with pncs */
3236 pn_Cmp new_pnc = pn_a ^ pn_b;
3238 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3245 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3246 mode, get_mode_null(mode));
3247 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3248 } else if (mode == mode_b &&
3250 is_Const(b) && is_Const_one(b) &&
3251 is_Cmp(get_Proj_pred(a))) {
3252 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3253 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3254 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3256 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3257 } else if (is_Const(b)) {
3258 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3259 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3260 ir_node *not_op = get_Not_op(a);
3261 dbg_info *dbg = get_irn_dbg_info(n);
3262 ir_graph *irg = current_ir_graph;
3263 ir_node *block = get_nodes_block(n);
3264 ir_mode *mode = get_irn_mode(n);
3265 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3267 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3268 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3269 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3272 n = transform_bitwise_distributive(n, transform_node_Eor);
3276 } /* transform_node_Eor */
3281 static ir_node *transform_node_Not(ir_node *n) {
3282 ir_node *c, *oldn = n;
3283 ir_node *a = get_Not_op(n);
3284 ir_mode *mode = get_irn_mode(n);
3286 HANDLE_UNOP_PHI(tarval_not,a,c);
3288 /* check for a boolean Not */
3289 if (mode == mode_b &&
3291 is_Cmp(get_Proj_pred(a))) {
3292 /* We negate a Cmp. The Cmp has the negated result anyways! */
3293 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3294 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3299 ir_node *eor_b = get_Eor_right(a);
3300 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3301 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3302 ir_node *eor_a = get_Eor_left(a);
3303 dbg_info *dbg = get_irn_dbg_info(n);
3304 ir_graph *irg = current_ir_graph;
3305 ir_node *block = get_nodes_block(n);
3306 ir_mode *mode = get_irn_mode(n);
3307 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3311 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3312 if (is_Minus(a)) { /* ~-x -> x + -1 */
3313 dbg_info *dbg = get_irn_dbg_info(n);
3314 ir_graph *irg = current_ir_graph;
3315 ir_node *block = get_nodes_block(n);
3316 ir_node *add_l = get_Minus_op(a);
3317 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3318 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3319 } else if (is_Add(a)) {
3320 ir_node *add_r = get_Add_right(a);
3321 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3322 /* ~(x + -1) = -x */
3323 ir_node *op = get_Add_left(a);
3324 ir_node *blk = get_irn_n(n, -1);
3325 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3326 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3331 } /* transform_node_Not */
3334 * Transform a Minus.
3338 * -(a >>u (size-1)) = a >>s (size-1)
3339 * -(a >>s (size-1)) = a >>u (size-1)
3340 * -(a * const) -> a * -const
3342 static ir_node *transform_node_Minus(ir_node *n) {
3343 ir_node *c, *oldn = n;
3344 ir_node *a = get_Minus_op(n);
3347 HANDLE_UNOP_PHI(tarval_neg,a,c);
3349 mode = get_irn_mode(a);
3350 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3351 /* the following rules are only to twos-complement */
3354 ir_node *op = get_Not_op(a);
3355 tarval *tv = get_mode_one(mode);
3356 ir_node *blk = get_irn_n(n, -1);
3357 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3358 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3359 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3363 ir_node *c = get_Shr_right(a);
3366 tarval *tv = get_Const_tarval(c);
3368 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3369 /* -(a >>u (size-1)) = a >>s (size-1) */
3370 ir_node *v = get_Shr_left(a);
3372 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3373 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3379 ir_node *c = get_Shrs_right(a);
3382 tarval *tv = get_Const_tarval(c);
3384 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3385 /* -(a >>s (size-1)) = a >>u (size-1) */
3386 ir_node *v = get_Shrs_left(a);
3388 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3389 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3396 /* - (a-b) = b - a */
3397 ir_node *la = get_Sub_left(a);
3398 ir_node *ra = get_Sub_right(a);
3399 ir_node *blk = get_irn_n(n, -1);
3401 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3402 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3406 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3407 ir_node *mul_l = get_Mul_left(a);
3408 ir_node *mul_r = get_Mul_right(a);
3409 if (is_Const(mul_r)) {
3410 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3411 if(tv != tarval_bad) {
3412 ir_node *cnst = new_Const(mode, tv);
3413 dbg_info *dbg = get_irn_dbg_info(a);
3414 ir_graph *irg = current_ir_graph;
3415 ir_node *block = get_nodes_block(a);
3416 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3417 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3424 } /* transform_node_Minus */
3427 * Transform a Cast_type(Const) into a new Const_type
3429 static ir_node *transform_node_Cast(ir_node *n) {
3431 ir_node *pred = get_Cast_op(n);
3432 ir_type *tp = get_irn_type(n);
3434 if (is_Const(pred) && get_Const_type(pred) != tp) {
3435 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3436 get_Const_tarval(pred), tp);
3437 DBG_OPT_CSTEVAL(oldn, n);
3438 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3439 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3440 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3441 DBG_OPT_CSTEVAL(oldn, n);
3445 } /* transform_node_Cast */
3448 * Transform a Proj(Div) with a non-zero value.
3449 * Removes the exceptions and routes the memory to the NoMem node.
3451 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3452 ir_node *div = get_Proj_pred(proj);
3453 ir_node *b = get_Div_right(div);
3454 ir_node *confirm, *res, *new_mem;
3457 if (value_not_zero(b, &confirm)) {
3458 /* div(x, y) && y != 0 */
3459 if (confirm == NULL) {
3460 /* we are sure we have a Const != 0 */
3461 new_mem = get_Div_mem(div);
3462 if (is_Pin(new_mem))
3463 new_mem = get_Pin_op(new_mem);
3464 set_Div_mem(div, new_mem);
3465 set_irn_pinned(div, op_pin_state_floats);
3468 proj_nr = get_Proj_proj(proj);
3470 case pn_Div_X_regular:
3471 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3473 case pn_Div_X_except:
3474 /* we found an exception handler, remove it */
3475 DBG_OPT_EXC_REM(proj);
3479 res = get_Div_mem(div);
3480 new_mem = get_irg_no_mem(current_ir_graph);
3483 /* This node can only float up to the Confirm block */
3484 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3486 set_irn_pinned(div, op_pin_state_floats);
3487 /* this is a Div without exception, we can remove the memory edge */
3488 set_Div_mem(div, new_mem);
3493 } /* transform_node_Proj_Div */
3496 * Transform a Proj(Mod) with a non-zero value.
3497 * Removes the exceptions and routes the memory to the NoMem node.
3499 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3500 ir_node *mod = get_Proj_pred(proj);
3501 ir_node *b = get_Mod_right(mod);
3502 ir_node *confirm, *res, *new_mem;
3505 if (value_not_zero(b, &confirm)) {
3506 /* mod(x, y) && y != 0 */
3507 proj_nr = get_Proj_proj(proj);
3509 if (confirm == NULL) {
3510 /* we are sure we have a Const != 0 */
3511 new_mem = get_Mod_mem(mod);
3512 if (is_Pin(new_mem))
3513 new_mem = get_Pin_op(new_mem);
3514 set_Mod_mem(mod, new_mem);
3515 set_irn_pinned(mod, op_pin_state_floats);
3520 case pn_Mod_X_regular:
3521 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3523 case pn_Mod_X_except:
3524 /* we found an exception handler, remove it */
3525 DBG_OPT_EXC_REM(proj);
3529 res = get_Mod_mem(mod);
3530 new_mem = get_irg_no_mem(current_ir_graph);
3533 /* This node can only float up to the Confirm block */
3534 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3536 /* this is a Mod without exception, we can remove the memory edge */
3537 set_Mod_mem(mod, new_mem);
3540 if (get_Mod_left(mod) == b) {
3541 /* a % a = 0 if a != 0 */
3542 ir_mode *mode = get_irn_mode(proj);
3543 ir_node *res = new_Const(mode, get_mode_null(mode));
3545 DBG_OPT_CSTEVAL(mod, res);
3551 } /* transform_node_Proj_Mod */
3554 * Transform a Proj(DivMod) with a non-zero value.
3555 * Removes the exceptions and routes the memory to the NoMem node.
3557 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3558 ir_node *divmod = get_Proj_pred(proj);
3559 ir_node *b = get_DivMod_right(divmod);
3560 ir_node *confirm, *res, *new_mem;
3563 if (value_not_zero(b, &confirm)) {
3564 /* DivMod(x, y) && y != 0 */
3565 proj_nr = get_Proj_proj(proj);
3567 if (confirm == NULL) {
3568 /* we are sure we have a Const != 0 */
3569 new_mem = get_DivMod_mem(divmod);
3570 if (is_Pin(new_mem))
3571 new_mem = get_Pin_op(new_mem);
3572 set_DivMod_mem(divmod, new_mem);
3573 set_irn_pinned(divmod, op_pin_state_floats);
3578 case pn_DivMod_X_regular:
3579 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3581 case pn_DivMod_X_except:
3582 /* we found an exception handler, remove it */
3583 DBG_OPT_EXC_REM(proj);
3587 res = get_DivMod_mem(divmod);
3588 new_mem = get_irg_no_mem(current_ir_graph);
3591 /* This node can only float up to the Confirm block */
3592 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3594 /* this is a DivMod without exception, we can remove the memory edge */
3595 set_DivMod_mem(divmod, new_mem);
3598 case pn_DivMod_res_mod:
3599 if (get_DivMod_left(divmod) == b) {
3600 /* a % a = 0 if a != 0 */
3601 ir_mode *mode = get_irn_mode(proj);
3602 ir_node *res = new_Const(mode, get_mode_null(mode));
3604 DBG_OPT_CSTEVAL(divmod, res);
3610 } /* transform_node_Proj_DivMod */
3613 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3615 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3616 if (get_opt_unreachable_code()) {
3617 ir_node *n = get_Proj_pred(proj);
3618 ir_node *b = get_Cond_selector(n);
3620 if (mode_is_int(get_irn_mode(b))) {
3621 tarval *tb = value_of(b);
3623 if (tb != tarval_bad) {
3624 /* we have a constant switch */
3625 long num = get_Proj_proj(proj);
3627 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3628 if (get_tarval_long(tb) == num) {
3629 /* Do NOT create a jump here, or we will have 2 control flow ops
3630 * in a block. This case is optimized away in optimize_cf(). */
3633 /* this case will NEVER be taken, kill it */
3641 } /* transform_node_Proj_Cond */
3644 * Create a 0 constant of given mode.
3646 static ir_node *create_zero_const(ir_mode *mode) {
3647 tarval *tv = get_mode_null(mode);
3648 ir_node *cnst = new_Const(mode, tv);
3653 /* the order of the values is important! */
3654 typedef enum const_class {
3660 static const_class classify_const(const ir_node* n)
3662 if (is_Const(n)) return const_const;
3663 if (is_irn_constlike(n)) return const_like;
3668 * Determines whether r is more constlike or has a larger index (in that order)
3671 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3673 const const_class l_order = classify_const(l);
3674 const const_class r_order = classify_const(r);
3676 l_order > r_order ||
3677 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3681 * Normalizes and optimizes Cmp nodes.
3683 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3684 ir_node *n = get_Proj_pred(proj);
3685 ir_node *left = get_Cmp_left(n);
3686 ir_node *right = get_Cmp_right(n);
3689 ir_mode *mode = NULL;
3690 long proj_nr = get_Proj_proj(proj);
3692 /* we can evaluate some cases directly */
3695 return new_Const(mode_b, get_tarval_b_false());
3697 return new_Const(mode_b, get_tarval_b_true());
3699 if (!mode_is_float(get_irn_mode(left)))
3700 return new_Const(mode_b, get_tarval_b_true());
3706 /* remove Casts of both sides */
3707 left = skip_Cast(left);
3708 right = skip_Cast(right);
3710 /* Remove unnecessary conversions */
3711 /* TODO handle constants */
3712 if (is_Conv(left) && is_Conv(right)) {
3713 ir_mode *mode = get_irn_mode(left);
3714 ir_node *op_left = get_Conv_op(left);
3715 ir_node *op_right = get_Conv_op(right);
3716 ir_mode *mode_left = get_irn_mode(op_left);
3717 ir_mode *mode_right = get_irn_mode(op_right);
3719 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3720 && mode_left != mode_b && mode_right != mode_b) {
3721 ir_graph *irg = current_ir_graph;
3722 ir_node *block = get_nodes_block(n);
3724 if (mode_left == mode_right) {
3728 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3729 } else if (smaller_mode(mode_left, mode_right)) {
3730 left = new_r_Conv(irg, block, op_left, mode_right);
3733 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3734 } else if (smaller_mode(mode_right, mode_left)) {
3736 right = new_r_Conv(irg, block, op_right, mode_left);
3738 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3743 /* remove operation on both sides if possible */
3744 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3746 * The following operations are NOT safe for floating point operations, for instance
3747 * 1.0 + inf == 2.0 + inf, =/=> x == y
3749 if (mode_is_int(get_irn_mode(left))) {
3750 unsigned lop = get_irn_opcode(left);
3752 if (lop == get_irn_opcode(right)) {
3753 ir_node *ll, *lr, *rl, *rr;
3755 /* same operation on both sides, try to remove */
3759 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3760 left = get_unop_op(left);
3761 right = get_unop_op(right);
3763 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3766 ll = get_Add_left(left);
3767 lr = get_Add_right(left);
3768 rl = get_Add_left(right);
3769 rr = get_Add_right(right);
3772 /* X + a CMP X + b ==> a CMP b */
3776 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3777 } else if (ll == rr) {
3778 /* X + a CMP b + X ==> a CMP b */
3782 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3783 } else if (lr == rl) {
3784 /* a + X CMP X + b ==> a CMP b */
3788 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3789 } else if (lr == rr) {
3790 /* a + X CMP b + X ==> a CMP b */
3794 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3798 ll = get_Sub_left(left);
3799 lr = get_Sub_right(left);
3800 rl = get_Sub_left(right);
3801 rr = get_Sub_right(right);
3804 /* X - a CMP X - b ==> a CMP b */
3808 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3809 } else if (lr == rr) {
3810 /* a - X CMP b - X ==> a CMP b */
3814 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3818 if (get_Rot_right(left) == get_Rot_right(right)) {
3819 /* a ROT X CMP b ROT X ==> a CMP b */
3820 left = get_Rot_left(left);
3821 right = get_Rot_left(right);
3823 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3831 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3832 if (is_Add(left) || is_Sub(left)) {
3833 ir_node *ll = get_binop_left(left);
3834 ir_node *lr = get_binop_right(left);
3836 if (lr == right && is_Add(left)) {
3843 right = create_zero_const(get_irn_mode(left));
3845 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3848 if (is_Add(right) || is_Sub(right)) {
3849 ir_node *rl = get_binop_left(right);
3850 ir_node *rr = get_binop_right(right);
3852 if (rr == left && is_Add(right)) {
3859 right = create_zero_const(get_irn_mode(left));
3861 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3864 } /* mode_is_int(...) */
3865 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3867 /* replace mode_b compares with ands/ors */
3868 if (get_irn_mode(left) == mode_b) {
3869 ir_graph *irg = current_ir_graph;
3870 ir_node *block = get_nodes_block(n);
3874 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3875 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3876 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3877 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3878 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3879 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3880 default: bres = NULL;
3883 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3889 * First step: normalize the compare op
3890 * by placing the constant on the right side
3891 * or moving the lower address node to the left.
3893 if (!operands_are_normalized(left, right)) {
3899 proj_nr = get_inversed_pnc(proj_nr);
3904 * Second step: Try to reduce the magnitude
3905 * of a constant. This may help to generate better code
3906 * later and may help to normalize more compares.
3907 * Of course this is only possible for integer values.
3909 if (is_Const(right)) {
3910 mode = get_irn_mode(right);
3911 tv = get_Const_tarval(right);
3913 /* TODO extend to arbitrary constants */
3914 if (is_Conv(left) && tarval_is_null(tv)) {
3915 ir_node *op = get_Conv_op(left);
3916 ir_mode *op_mode = get_irn_mode(op);
3919 * UpConv(x) REL 0 ==> x REL 0
3921 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3922 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3923 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3924 tv = get_mode_null(op_mode);
3928 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3932 if (tv != tarval_bad) {
3933 /* the following optimization is possible on modes without Overflow
3934 * on Unary Minus or on == and !=:
3935 * -a CMP c ==> a swap(CMP) -c
3937 * Beware: for two-complement Overflow may occur, so only == and != can
3938 * be optimized, see this:
3939 * -MININT < 0 =/=> MININT > 0 !!!
3941 if (is_Minus(left) &&
3942 (!mode_overflow_on_unary_Minus(mode) ||
3943 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3944 tv = tarval_neg(tv);
3946 if (tv != tarval_bad) {
3947 left = get_Minus_op(left);
3948 proj_nr = get_inversed_pnc(proj_nr);
3950 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3952 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3953 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3954 tv = tarval_not(tv);
3956 if (tv != tarval_bad) {
3957 left = get_Not_op(left);
3959 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3963 /* for integer modes, we have more */
3964 if (mode_is_int(mode)) {
3965 /* Ne includes Unordered which is not possible on integers.
3966 * However, frontends often use this wrong, so fix it here */
3967 if (proj_nr & pn_Cmp_Uo) {
3968 proj_nr &= ~pn_Cmp_Uo;
3969 set_Proj_proj(proj, proj_nr);
3972 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3973 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3974 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3975 tv = tarval_sub(tv, get_mode_one(mode));
3977 if (tv != tarval_bad) {
3978 proj_nr ^= pn_Cmp_Eq;
3980 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3983 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3984 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3985 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3986 tv = tarval_add(tv, get_mode_one(mode));
3988 if (tv != tarval_bad) {
3989 proj_nr ^= pn_Cmp_Eq;
3991 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3995 /* the following reassociations work only for == and != */
3996 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3998 #if 0 /* Might be not that good in general */
3999 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4000 if (tarval_is_null(tv) && is_Sub(left)) {
4001 right = get_Sub_right(left);
4002 left = get_Sub_left(left);
4004 tv = value_of(right);
4006 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4010 if (tv != tarval_bad) {
4011 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4013 ir_node *c1 = get_Sub_right(left);
4014 tarval *tv2 = value_of(c1);
4016 if (tv2 != tarval_bad) {
4017 tv2 = tarval_add(tv, value_of(c1));
4019 if (tv2 != tarval_bad) {
4020 left = get_Sub_left(left);
4023 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4027 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4028 else if (is_Add(left)) {
4029 ir_node *a_l = get_Add_left(left);
4030 ir_node *a_r = get_Add_right(left);
4034 if (is_Const(a_l)) {
4036 tv2 = value_of(a_l);
4039 tv2 = value_of(a_r);
4042 if (tv2 != tarval_bad) {
4043 tv2 = tarval_sub(tv, tv2);
4045 if (tv2 != tarval_bad) {
4049 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4053 /* -a == c ==> a == -c, -a != c ==> a != -c */
4054 else if (is_Minus(left)) {
4055 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4057 if (tv2 != tarval_bad) {
4058 left = get_Minus_op(left);
4061 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4066 /* the following reassociations work only for <= */
4067 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4068 if (tv != tarval_bad) {
4069 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4070 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4076 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4077 switch (get_irn_opcode(left)) {
4081 c1 = get_And_right(left);
4084 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4085 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4087 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4089 /* TODO: move to constant evaluation */
4090 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4091 c1 = new_Const(mode_b, tv);
4092 DBG_OPT_CSTEVAL(proj, c1);
4096 if (tarval_is_single_bit(tv)) {
4098 * optimization for AND:
4100 * And(x, C) == C ==> And(x, C) != 0
4101 * And(x, C) != C ==> And(X, C) == 0
4103 * if C is a single Bit constant.
4106 /* check for Constant's match. We have check hare the tarvals,
4107 because our const might be changed */
4108 if (get_Const_tarval(c1) == tv) {
4109 /* fine: do the transformation */
4110 tv = get_mode_null(get_tarval_mode(tv));
4111 proj_nr ^= pn_Cmp_Leg;
4113 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4119 c1 = get_Or_right(left);
4120 if (is_Const(c1) && tarval_is_null(tv)) {
4122 * Or(x, C) == 0 && C != 0 ==> FALSE
4123 * Or(x, C) != 0 && C != 0 ==> TRUE
4125 if (! tarval_is_null(get_Const_tarval(c1))) {
4126 /* TODO: move to constant evaluation */
4127 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4128 c1 = new_Const(mode_b, tv);
4129 DBG_OPT_CSTEVAL(proj, c1);
4136 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4138 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4141 c1 = get_Shl_right(left);
4143 tarval *tv1 = get_Const_tarval(c1);
4144 ir_mode *mode = get_irn_mode(left);
4145 tarval *minus1 = get_mode_all_one(mode);
4146 tarval *amask = tarval_shr(minus1, tv1);
4147 tarval *cmask = tarval_shl(minus1, tv1);
4150 if (tarval_and(tv, cmask) != tv) {
4151 /* condition not met */
4152 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4153 c1 = new_Const(mode_b, tv);
4154 DBG_OPT_CSTEVAL(proj, c1);
4157 sl = get_Shl_left(left);
4158 blk = get_nodes_block(n);
4159 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4160 tv = tarval_shr(tv, tv1);
4162 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4167 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4169 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4172 c1 = get_Shr_right(left);
4174 tarval *tv1 = get_Const_tarval(c1);
4175 ir_mode *mode = get_irn_mode(left);
4176 tarval *minus1 = get_mode_all_one(mode);
4177 tarval *amask = tarval_shl(minus1, tv1);
4178 tarval *cmask = tarval_shr(minus1, tv1);
4181 if (tarval_and(tv, cmask) != tv) {
4182 /* condition not met */
4183 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4184 c1 = new_Const(mode_b, tv);
4185 DBG_OPT_CSTEVAL(proj, c1);
4188 sl = get_Shr_left(left);
4189 blk = get_nodes_block(n);
4190 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4191 tv = tarval_shl(tv, tv1);
4193 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4198 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4200 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4203 c1 = get_Shrs_right(left);
4205 tarval *tv1 = get_Const_tarval(c1);
4206 ir_mode *mode = get_irn_mode(left);
4207 tarval *minus1 = get_mode_all_one(mode);
4208 tarval *amask = tarval_shl(minus1, tv1);
4209 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4212 cond = tarval_sub(cond, tv1);
4213 cond = tarval_shrs(tv, cond);
4215 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4216 /* condition not met */
4217 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4218 c1 = new_Const(mode_b, tv);
4219 DBG_OPT_CSTEVAL(proj, c1);
4222 sl = get_Shrs_left(left);
4223 blk = get_nodes_block(n);
4224 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4225 tv = tarval_shl(tv, tv1);
4227 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4232 } /* tarval != bad */
4235 if (changed & 2) /* need a new Const */
4236 right = new_Const(mode, tv);
4238 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4239 ir_node *op = get_Proj_pred(left);
4241 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4242 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4243 ir_node *c = get_binop_right(op);
4246 tarval *tv = get_Const_tarval(c);
4248 if (tarval_is_single_bit(tv)) {
4249 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4250 ir_node *v = get_binop_left(op);
4251 ir_node *blk = get_irn_n(op, -1);
4252 ir_mode *mode = get_irn_mode(v);
4254 tv = tarval_sub(tv, get_mode_one(mode));
4255 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4257 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4264 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4266 /* create a new compare */
4267 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4268 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4272 } /* transform_node_Proj_Cmp */
4275 * Does all optimizations on nodes that must be done on it's Proj's
4276 * because of creating new nodes.
4278 static ir_node *transform_node_Proj(ir_node *proj) {
4279 ir_node *n = get_Proj_pred(proj);
4281 switch (get_irn_opcode(n)) {
4283 return transform_node_Proj_Div(proj);
4286 return transform_node_Proj_Mod(proj);
4289 return transform_node_Proj_DivMod(proj);
4292 return transform_node_Proj_Cond(proj);
4295 return transform_node_Proj_Cmp(proj);
4298 /* should not happen, but if it does will be optimized away */
4299 return equivalent_node_Proj(proj);
4305 } /* transform_node_Proj */
4308 * Move Confirms down through Phi nodes.
4310 static ir_node *transform_node_Phi(ir_node *phi) {
4312 ir_mode *mode = get_irn_mode(phi);
4314 if (mode_is_reference(mode)) {
4315 n = get_irn_arity(phi);
4317 /* Beware of Phi0 */
4319 ir_node *pred = get_irn_n(phi, 0);
4320 ir_node *bound, *new_Phi, *block, **in;
4323 if (! is_Confirm(pred))
4326 bound = get_Confirm_bound(pred);
4327 pnc = get_Confirm_cmp(pred);
4329 NEW_ARR_A(ir_node *, in, n);
4330 in[0] = get_Confirm_value(pred);
4332 for (i = 1; i < n; ++i) {
4333 pred = get_irn_n(phi, i);
4335 if (! is_Confirm(pred) ||
4336 get_Confirm_bound(pred) != bound ||
4337 get_Confirm_cmp(pred) != pnc)
4339 in[i] = get_Confirm_value(pred);
4341 /* move the Confirm nodes "behind" the Phi */
4342 block = get_irn_n(phi, -1);
4343 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4344 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4348 } /* transform_node_Phi */
4351 * Returns the operands of a commutative bin-op, if one operand is
4352 * a const, it is returned as the second one.
4354 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4355 ir_node *op_a = get_binop_left(binop);
4356 ir_node *op_b = get_binop_right(binop);
4358 assert(is_op_commutative(get_irn_op(binop)));
4360 if (is_Const(op_a)) {
4367 } /* get_comm_Binop_Ops */
4370 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4371 * Such pattern may arise in bitfield stores.
4373 * value c4 value c4 & c2
4374 * AND c3 AND c1 | c3
4381 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4384 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4387 ir_node *and_l, *c3;
4388 ir_node *value, *c4;
4389 ir_node *new_and, *new_const, *block;
4390 ir_mode *mode = get_irn_mode(or);
4392 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4395 get_comm_Binop_Ops(or, &and, &c1);
4396 if (!is_Const(c1) || !is_And(and))
4399 get_comm_Binop_Ops(and, &or_l, &c2);
4403 tv1 = get_Const_tarval(c1);
4404 tv2 = get_Const_tarval(c2);
4406 tv = tarval_or(tv1, tv2);
4407 if (tarval_is_all_one(tv)) {
4408 /* the AND does NOT clear a bit with isn't set by the OR */
4409 set_Or_left(or, or_l);
4410 set_Or_right(or, c1);
4412 /* check for more */
4419 get_comm_Binop_Ops(or_l, &and_l, &c3);
4420 if (!is_Const(c3) || !is_And(and_l))
4423 get_comm_Binop_Ops(and_l, &value, &c4);
4427 /* ok, found the pattern, check for conditions */
4428 assert(mode == get_irn_mode(and));
4429 assert(mode == get_irn_mode(or_l));
4430 assert(mode == get_irn_mode(and_l));
4432 tv3 = get_Const_tarval(c3);
4433 tv4 = get_Const_tarval(c4);
4435 tv = tarval_or(tv4, tv2);
4436 if (!tarval_is_all_one(tv)) {
4437 /* have at least one 0 at the same bit position */
4441 n_tv4 = tarval_not(tv4);
4442 if (tv3 != tarval_and(tv3, n_tv4)) {
4443 /* bit in the or_mask is outside the and_mask */
4447 n_tv2 = tarval_not(tv2);
4448 if (tv1 != tarval_and(tv1, n_tv2)) {
4449 /* bit in the or_mask is outside the and_mask */
4453 /* ok, all conditions met */
4454 block = get_irn_n(or, -1);
4456 new_and = new_r_And(current_ir_graph, block,
4457 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4459 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4461 set_Or_left(or, new_and);
4462 set_Or_right(or, new_const);
4464 /* check for more */
4466 } /* transform_node_Or_bf_store */
4469 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4471 static ir_node *transform_node_Or_Rot(ir_node *or) {
4472 ir_mode *mode = get_irn_mode(or);
4473 ir_node *shl, *shr, *block;
4474 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4477 if (! mode_is_int(mode))
4480 shl = get_binop_left(or);
4481 shr = get_binop_right(or);
4490 } else if (!is_Shl(shl)) {
4492 } else if (!is_Shr(shr)) {
4495 x = get_Shl_left(shl);
4496 if (x != get_Shr_left(shr))
4499 c1 = get_Shl_right(shl);
4500 c2 = get_Shr_right(shr);
4501 if (is_Const(c1) && is_Const(c2)) {
4502 tv1 = get_Const_tarval(c1);
4503 if (! tarval_is_long(tv1))
4506 tv2 = get_Const_tarval(c2);
4507 if (! tarval_is_long(tv2))
4510 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4511 != (int) get_mode_size_bits(mode))
4514 /* yet, condition met */
4515 block = get_irn_n(or, -1);
4517 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4519 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4521 } else if (is_Sub(c1)) {
4525 if (get_Sub_right(sub) != v)
4528 c1 = get_Sub_left(sub);
4532 tv1 = get_Const_tarval(c1);
4533 if (! tarval_is_long(tv1))
4536 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4539 /* yet, condition met */
4540 block = get_nodes_block(or);
4542 /* a Rot right is not supported, so use a rot left */
4543 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4545 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4547 } else if (is_Sub(c2)) {
4551 c1 = get_Sub_left(sub);
4555 tv1 = get_Const_tarval(c1);
4556 if (! tarval_is_long(tv1))
4559 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4562 /* yet, condition met */
4563 block = get_irn_n(or, -1);
4566 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4568 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4573 } /* transform_node_Or_Rot */
4578 static ir_node *transform_node_Or(ir_node *n) {
4579 ir_node *c, *oldn = n;
4580 ir_node *a = get_Or_left(n);
4581 ir_node *b = get_Or_right(n);
4584 if (is_Not(a) && is_Not(b)) {
4585 /* ~a | ~b = ~(a&b) */
4586 ir_node *block = get_nodes_block(n);
4588 mode = get_irn_mode(n);
4591 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4592 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4593 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4597 /* we can evaluate 2 Projs of the same Cmp */
4598 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4599 ir_node *pred_a = get_Proj_pred(a);
4600 ir_node *pred_b = get_Proj_pred(b);
4601 if (pred_a == pred_b) {
4602 dbg_info *dbgi = get_irn_dbg_info(n);
4603 ir_node *block = get_nodes_block(pred_a);
4604 pn_Cmp pn_a = get_Proj_proj(a);
4605 pn_Cmp pn_b = get_Proj_proj(b);
4606 /* yes, we can simply calculate with pncs */
4607 pn_Cmp new_pnc = pn_a | pn_b;
4609 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4614 mode = get_irn_mode(n);
4615 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4617 n = transform_node_Or_bf_store(n);
4618 n = transform_node_Or_Rot(n);
4622 n = transform_bitwise_distributive(n, transform_node_Or);
4625 } /* transform_node_Or */
4629 static ir_node *transform_node(ir_node *n);
4632 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4634 * Should be moved to reassociation?
4636 static ir_node *transform_node_shift(ir_node *n) {
4637 ir_node *left, *right;
4638 tarval *tv1, *tv2, *res;
4640 int modulo_shf, flag;
4642 left = get_binop_left(n);
4644 /* different operations */
4645 if (get_irn_op(left) != get_irn_op(n))
4648 right = get_binop_right(n);
4649 tv1 = value_of(right);
4650 if (tv1 == tarval_bad)
4653 tv2 = value_of(get_binop_right(left));
4654 if (tv2 == tarval_bad)
4657 res = tarval_add(tv1, tv2);
4659 /* beware: a simple replacement works only, if res < modulo shift */
4660 mode = get_irn_mode(n);
4664 modulo_shf = get_mode_modulo_shift(mode);
4665 if (modulo_shf > 0) {
4666 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4668 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4674 /* ok, we can replace it */
4675 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4677 in[0] = get_binop_left(left);
4678 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4680 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4682 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4684 return transform_node(irn);
4687 } /* transform_node_shift */
4692 static ir_node *transform_node_Shr(ir_node *n) {
4693 ir_node *c, *oldn = n;
4694 ir_node *a = get_Shr_left(n);
4695 ir_node *b = get_Shr_right(n);
4696 ir_mode *mode = get_irn_mode(n);
4698 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4699 return transform_node_shift(n);
4700 } /* transform_node_Shr */
4705 static ir_node *transform_node_Shrs(ir_node *n) {
4706 ir_node *c, *oldn = n;
4707 ir_node *a = get_Shrs_left(n);
4708 ir_node *b = get_Shrs_right(n);
4709 ir_mode *mode = get_irn_mode(n);
4711 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4712 return transform_node_shift(n);
4713 } /* transform_node_Shrs */
4718 static ir_node *transform_node_Shl(ir_node *n) {
4719 ir_node *c, *oldn = n;
4720 ir_node *a = get_Shl_left(n);
4721 ir_node *b = get_Shl_right(n);
4722 ir_mode *mode = get_irn_mode(n);
4724 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4725 return transform_node_shift(n);
4726 } /* transform_node_Shl */
4731 static ir_node *transform_node_Rot(ir_node *n) {
4732 ir_node *c, *oldn = n;
4733 ir_node *a = get_Rot_left(n);
4734 ir_node *b = get_Rot_right(n);
4735 ir_mode *mode = get_irn_mode(n);
4737 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4738 return transform_node_shift(n);
4739 } /* transform_node_Rot */
4744 static ir_node *transform_node_Conv(ir_node *n) {
4745 ir_node *c, *oldn = n;
4746 ir_node *a = get_Conv_op(n);
4748 if (is_const_Phi(a)) {
4749 c = apply_conv_on_phi(a, get_irn_mode(n));
4751 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4756 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4757 ir_mode *mode = get_irn_mode(n);
4758 return new_r_Unknown(current_ir_graph, mode);
4762 } /* transform_node_Conv */
4765 * Remove dead blocks and nodes in dead blocks
4766 * in keep alive list. We do not generate a new End node.
4768 static ir_node *transform_node_End(ir_node *n) {
4769 int i, j, n_keepalives = get_End_n_keepalives(n);
4772 NEW_ARR_A(ir_node *, in, n_keepalives);
4774 for (i = j = 0; i < n_keepalives; ++i) {
4775 ir_node *ka = get_End_keepalive(n, i);
4777 if (! is_Block_dead(ka)) {
4781 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4784 /* FIXME: beabi need to keep a Proj(M) */
4785 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4788 if (j != n_keepalives)
4789 set_End_keepalives(n, j, in);
4791 } /* transform_node_End */
4793 /** returns 1 if a == -b */
4794 static int is_negated_value(ir_node *a, ir_node *b) {
4795 if (is_Minus(a) && get_Minus_op(a) == b)
4797 if (is_Minus(b) && get_Minus_op(b) == a)
4799 if (is_Sub(a) && is_Sub(b)) {
4800 ir_node *a_left = get_Sub_left(a);
4801 ir_node *a_right = get_Sub_right(a);
4802 ir_node *b_left = get_Sub_left(b);
4803 ir_node *b_right = get_Sub_right(b);
4805 if (a_left == b_right && a_right == b_left)
4813 * Optimize a Mux into some simpler cases.
4815 static ir_node *transform_node_Mux(ir_node *n) {
4816 ir_node *oldn = n, *sel = get_Mux_sel(n);
4817 ir_mode *mode = get_irn_mode(n);
4819 if (mode == mode_b) {
4820 ir_node *t = get_Mux_true(n);
4821 ir_node *f = get_Mux_false(n);
4822 dbg_info *dbg = get_irn_dbg_info(n);
4823 ir_node *block = get_irn_n(n, -1);
4824 ir_graph *irg = current_ir_graph;
4827 tarval *tv_t = get_Const_tarval(t);
4828 if (tv_t == tarval_b_true) {
4830 /* Muxb(sel, true, false) = sel */
4831 assert(get_Const_tarval(f) == tarval_b_false);
4832 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4835 /* Muxb(sel, true, x) = Or(sel, x) */
4836 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4837 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4841 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4842 assert(tv_t == tarval_b_false);
4844 /* Muxb(sel, false, true) = Not(sel) */
4845 assert(get_Const_tarval(f) == tarval_b_true);
4846 DBG_OPT_ALGSIM0(oldn, not_sel, FS_OPT_MUX_NOT_BOOL);
4849 /* Muxb(sel, false, x) = And(Not(sel), x) */
4850 n = new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4851 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ANDNOT_BOOL);
4855 } else if (is_Const(f)) {
4856 tarval *tv_f = get_Const_tarval(f);
4857 if (tv_f == tarval_b_true) {
4858 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4859 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4860 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4861 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4864 /* Muxb(sel, x, false) = And(sel, x) */
4865 assert(tv_f == tarval_b_false);
4866 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4867 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4873 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4874 ir_node *cmp = get_Proj_pred(sel);
4875 long pn = get_Proj_proj(sel);
4876 ir_node *f = get_Mux_false(n);
4877 ir_node *t = get_Mux_true(n);
4880 * Note: normalization puts the constant on the right side,
4881 * so we check only one case.
4883 * Note further that these optimization work even for floating point
4884 * with NaN's because -NaN == NaN.
4885 * However, if +0 and -0 is handled differently, we cannot use the first
4889 ir_node *cmp_r = get_Cmp_right(cmp);
4890 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4891 ir_node *block = get_irn_n(n, -1);
4893 if (is_negated_value(f, t)) {
4894 ir_node *cmp_left = get_Cmp_left(cmp);
4896 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4897 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4898 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4900 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4902 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4904 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4905 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4906 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4908 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4910 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4912 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4919 return arch_transform_node_Mux(n);
4920 } /* transform_node_Mux */
4923 * Optimize a Psi into some simpler cases.
4925 static ir_node *transform_node_Psi(ir_node *n) {
4927 return transform_node_Mux(n);
4930 } /* transform_node_Psi */
4933 * optimize sync nodes that have other syncs as input we simply add the inputs
4934 * of the other sync to our own inputs
4936 static ir_node *transform_node_Sync(ir_node *n) {
4937 int arity = get_Sync_n_preds(n);
4940 for (i = 0; i < arity;) {
4941 ir_node *pred = get_Sync_pred(n, i);
4945 if (!is_Sync(pred)) {
4953 pred_arity = get_Sync_n_preds(pred);
4954 for (j = 0; j < pred_arity; ++j) {
4955 ir_node *pred_pred = get_Sync_pred(pred, j);
4960 add_irn_n(n, pred_pred);
4964 if (get_Sync_pred(n, k) == pred_pred) break;
4969 /* rehash the sync node */
4970 add_identities(current_ir_graph->value_table, n);
4976 * Tries several [inplace] [optimizing] transformations and returns an
4977 * equivalent node. The difference to equivalent_node() is that these
4978 * transformations _do_ generate new nodes, and thus the old node must
4979 * not be freed even if the equivalent node isn't the old one.
4981 static ir_node *transform_node(ir_node *n) {
4985 * Transform_node is the only "optimizing transformation" that might
4986 * return a node with a different opcode. We iterate HERE until fixpoint
4987 * to get the final result.
4991 if (n->op->ops.transform_node)
4992 n = n->op->ops.transform_node(n);
4993 } while (oldn != n);
4996 } /* transform_node */
4999 * Sets the default transform node operation for an ir_op_ops.
5001 * @param code the opcode for the default operation
5002 * @param ops the operations initialized
5007 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5011 ops->transform_node = transform_node_##a; \
5048 } /* firm_set_default_transform_node */
5051 /* **************** Common Subexpression Elimination **************** */
5053 /** The size of the hash table used, should estimate the number of nodes
5055 #define N_IR_NODES 512
5057 /** Compares the attributes of two Const nodes. */
5058 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5059 return (get_Const_tarval(a) != get_Const_tarval(b))
5060 || (get_Const_type(a) != get_Const_type(b));
5061 } /* node_cmp_attr_Const */
5063 /** Compares the attributes of two Proj nodes. */
5064 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5065 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5066 } /* node_cmp_attr_Proj */
5068 /** Compares the attributes of two Filter nodes. */
5069 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5070 return get_Filter_proj(a) != get_Filter_proj(b);
5071 } /* node_cmp_attr_Filter */
5073 /** Compares the attributes of two Alloc nodes. */
5074 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5075 const alloc_attr *pa = get_irn_alloc_attr(a);
5076 const alloc_attr *pb = get_irn_alloc_attr(b);
5077 return (pa->where != pb->where) || (pa->type != pb->type);
5078 } /* node_cmp_attr_Alloc */
5080 /** Compares the attributes of two Free nodes. */
5081 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5082 const free_attr *pa = get_irn_free_attr(a);
5083 const free_attr *pb = get_irn_free_attr(b);
5084 return (pa->where != pb->where) || (pa->type != pb->type);
5085 } /* node_cmp_attr_Free */
5087 /** Compares the attributes of two SymConst nodes. */
5088 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5089 const symconst_attr *pa = get_irn_symconst_attr(a);
5090 const symconst_attr *pb = get_irn_symconst_attr(b);
5091 return (pa->kind != pb->kind)
5092 || (pa->sym.type_p != pb->sym.type_p)
5093 || (pa->tp != pb->tp);
5094 } /* node_cmp_attr_SymConst */
5096 /** Compares the attributes of two Call nodes. */
5097 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5098 return get_irn_call_attr(a) != get_irn_call_attr(b);
5099 } /* node_cmp_attr_Call */
5101 /** Compares the attributes of two Sel nodes. */
5102 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5103 const ir_entity *a_ent = get_Sel_entity(a);
5104 const ir_entity *b_ent = get_Sel_entity(b);
5106 (a_ent->kind != b_ent->kind) ||
5107 (a_ent->name != b_ent->name) ||
5108 (a_ent->owner != b_ent->owner) ||
5109 (a_ent->ld_name != b_ent->ld_name) ||
5110 (a_ent->type != b_ent->type);
5111 } /* node_cmp_attr_Sel */
5113 /** Compares the attributes of two Phi nodes. */
5114 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5115 /* we can only enter this function if both nodes have the same number of inputs,
5116 hence it is enough to check if one of them is a Phi0 */
5118 /* check the Phi0 pos attribute */
5119 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5122 } /* node_cmp_attr_Phi */
5124 /** Compares the attributes of two Conv nodes. */
5125 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5126 return get_Conv_strict(a) != get_Conv_strict(b);
5127 } /* node_cmp_attr_Conv */
5129 /** Compares the attributes of two Cast nodes. */
5130 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5131 return get_Cast_type(a) != get_Cast_type(b);
5132 } /* node_cmp_attr_Cast */
5134 /** Compares the attributes of two Load nodes. */
5135 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5136 if (get_Load_volatility(a) == volatility_is_volatile ||
5137 get_Load_volatility(b) == volatility_is_volatile)
5138 /* NEVER do CSE on volatile Loads */
5140 /* do not CSE Loads with different alignment. Be conservative. */
5141 if (get_Load_align(a) != get_Load_align(b))
5144 return get_Load_mode(a) != get_Load_mode(b);
5145 } /* node_cmp_attr_Load */
5147 /** Compares the attributes of two Store nodes. */
5148 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5149 /* do not CSE Stores with different alignment. Be conservative. */
5150 if (get_Store_align(a) != get_Store_align(b))
5153 /* NEVER do CSE on volatile Stores */
5154 return (get_Store_volatility(a) == volatility_is_volatile ||
5155 get_Store_volatility(b) == volatility_is_volatile);
5156 } /* node_cmp_attr_Store */
5158 /** Compares two exception attributes */
5159 static int node_cmp_exception(ir_node *a, ir_node *b) {
5160 const except_attr *ea = get_irn_except_attr(a);
5161 const except_attr *eb = get_irn_except_attr(b);
5163 return ea->pin_state != eb->pin_state;
5166 #define node_cmp_attr_Bound node_cmp_exception
5168 /** Compares the attributes of two Div nodes. */
5169 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5170 const divmod_attr *ma = get_irn_divmod_attr(a);
5171 const divmod_attr *mb = get_irn_divmod_attr(b);
5172 return ma->exc.pin_state != mb->exc.pin_state ||
5173 ma->res_mode != mb->res_mode ||
5174 ma->no_remainder != mb->no_remainder;
5175 } /* node_cmp_attr_Div */
5177 /** Compares the attributes of two DivMod nodes. */
5178 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5179 const divmod_attr *ma = get_irn_divmod_attr(a);
5180 const divmod_attr *mb = get_irn_divmod_attr(b);
5181 return ma->exc.pin_state != mb->exc.pin_state ||
5182 ma->res_mode != mb->res_mode;
5183 } /* node_cmp_attr_DivMod */
5185 /** Compares the attributes of two Mod nodes. */
5186 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5187 const divmod_attr *ma = get_irn_divmod_attr(a);
5188 const divmod_attr *mb = get_irn_divmod_attr(b);
5189 return ma->exc.pin_state != mb->exc.pin_state ||
5190 ma->res_mode != mb->res_mode;
5191 } /* node_cmp_attr_Mod */
5193 /** Compares the attributes of two Quot nodes. */
5194 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5195 const divmod_attr *ma = get_irn_divmod_attr(a);
5196 const divmod_attr *mb = get_irn_divmod_attr(b);
5197 return ma->exc.pin_state != mb->exc.pin_state ||
5198 ma->res_mode != mb->res_mode;
5199 } /* node_cmp_attr_Quot */
5201 /** Compares the attributes of two Confirm nodes. */
5202 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5203 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5204 } /* node_cmp_attr_Confirm */
5206 /** Compares the attributes of two ASM nodes. */
5207 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5209 const ir_asm_constraint *ca;
5210 const ir_asm_constraint *cb;
5213 if (get_ASM_text(a) != get_ASM_text(b))
5216 /* Should we really check the constraints here? Should be better, but is strange. */
5217 n = get_ASM_n_input_constraints(a);
5218 if (n != get_ASM_n_input_constraints(b))
5221 ca = get_ASM_input_constraints(a);
5222 cb = get_ASM_input_constraints(b);
5223 for (i = 0; i < n; ++i) {
5224 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5228 n = get_ASM_n_output_constraints(a);
5229 if (n != get_ASM_n_output_constraints(b))
5232 ca = get_ASM_output_constraints(a);
5233 cb = get_ASM_output_constraints(b);
5234 for (i = 0; i < n; ++i) {
5235 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5239 n = get_ASM_n_clobbers(a);
5240 if (n != get_ASM_n_clobbers(b))
5243 cla = get_ASM_clobbers(a);
5244 clb = get_ASM_clobbers(b);
5245 for (i = 0; i < n; ++i) {
5246 if (cla[i] != clb[i])
5250 } /* node_cmp_attr_ASM */
5253 * Set the default node attribute compare operation for an ir_op_ops.
5255 * @param code the opcode for the default operation
5256 * @param ops the operations initialized
5261 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5265 ops->node_cmp_attr = node_cmp_attr_##a; \
5296 } /* firm_set_default_node_cmp_attr */
5299 * Compare function for two nodes in the value table. Gets two
5300 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5302 int identities_cmp(const void *elt, const void *key) {
5303 ir_node *a = (ir_node *)elt;
5304 ir_node *b = (ir_node *)key;
5307 if (a == b) return 0;
5309 if ((get_irn_op(a) != get_irn_op(b)) ||
5310 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5312 /* compare if a's in and b's in are of equal length */
5313 irn_arity_a = get_irn_intra_arity(a);
5314 if (irn_arity_a != get_irn_intra_arity(b))
5317 if (get_irn_pinned(a) == op_pin_state_pinned) {
5318 /* for pinned nodes, the block inputs must be equal */
5319 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5321 } else if (! get_opt_global_cse()) {
5322 /* for block-local CSE both nodes must be in the same MacroBlock */
5323 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5327 /* compare a->in[0..ins] with b->in[0..ins] */
5328 for (i = 0; i < irn_arity_a; i++)
5329 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5333 * here, we already now that the nodes are identical except their
5336 if (a->op->ops.node_cmp_attr)
5337 return a->op->ops.node_cmp_attr(a, b);
5340 } /* identities_cmp */
5343 * Calculate a hash value of a node.
5345 unsigned ir_node_hash(ir_node *node) {
5349 if (node->op == op_Const) {
5350 /* special value for const, as they only differ in their tarval. */
5351 h = HASH_PTR(node->attr.con.tv);
5352 h = 9*h + HASH_PTR(get_irn_mode(node));
5353 } else if (node->op == op_SymConst) {
5354 /* special value for const, as they only differ in their symbol. */
5355 h = HASH_PTR(node->attr.symc.sym.type_p);
5356 h = 9*h + HASH_PTR(get_irn_mode(node));
5359 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5360 h = irn_arity = get_irn_intra_arity(node);
5362 /* consider all in nodes... except the block if not a control flow. */
5363 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5364 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5368 h = 9*h + HASH_PTR(get_irn_mode(node));
5370 h = 9*h + HASH_PTR(get_irn_op(node));
5374 } /* ir_node_hash */
5376 pset *new_identities(void) {
5377 return new_pset(identities_cmp, N_IR_NODES);
5378 } /* new_identities */
5380 void del_identities(pset *value_table) {
5381 del_pset(value_table);
5382 } /* del_identities */
5385 * Normalize a node by putting constants (and operands with larger
5386 * node index) on the right (operator side).
5388 * @param n The node to normalize
5390 static void normalize_node(ir_node *n) {
5391 if (is_op_commutative(get_irn_op(n))) {
5392 ir_node *l = get_binop_left(n);
5393 ir_node *r = get_binop_right(n);
5395 /* For commutative operators perform a OP b == b OP a but keep
5396 * constants on the RIGHT side. This helps greatly in some
5397 * optimizations. Moreover we use the idx number to make the form
5399 if (!operands_are_normalized(l, r)) {
5400 set_binop_left(n, r);
5401 set_binop_right(n, l);
5404 } /* normalize_node */
5407 * Update the nodes after a match in the value table. If both nodes have
5408 * the same MacroBlock but different Blocks, we must ensure that the node
5409 * with the dominating Block (the node that is near to the MacroBlock header
5410 * is stored in the table.
5411 * Because a MacroBlock has only one "non-exception" flow, we don't need
5412 * dominance info here: We known, that one block must dominate the other and
5413 * following the only block input will allow to find it.
5415 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5416 ir_node *known_blk, *new_block, *block, *mbh;
5418 if (get_opt_global_cse()) {
5419 /* Block inputs are meaning less */
5422 known_blk = get_irn_n(known_irn, -1);
5423 new_block = get_irn_n(new_ir_node, -1);
5424 if (known_blk == new_block) {
5425 /* already in the same block */
5429 * We expect the typical case when we built the graph. In that case, the
5430 * known_irn is already the upper one, so checking this should be faster.
5433 mbh = get_Block_MacroBlock(new_block);
5435 if (block == known_blk) {
5436 /* ok, we have found it: known_block dominates new_block as expected */
5441 * We have reached the MacroBlock header NOT founding
5442 * the known_block. new_block must dominate known_block.
5445 set_irn_n(known_irn, -1, new_block);
5448 assert(get_Block_n_cfgpreds(block) == 1);
5449 block = get_Block_cfgpred_block(block, 0);
5451 } /* update_value_table */
5454 * Return the canonical node computing the same value as n.
5456 * @param value_table The value table
5457 * @param n The node to lookup
5459 * Looks up the node in a hash table.
5461 * For Const nodes this is performed in the constructor, too. Const
5462 * nodes are extremely time critical because of their frequent use in
5463 * constant string arrays.
5465 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5468 if (!value_table) return n;
5472 o = pset_find(value_table, n, ir_node_hash(n));
5476 update_known_irn(o, n);
5483 * During construction we set the op_pin_state_pinned flag in the graph right when the
5484 * optimization is performed. The flag turning on procedure global cse could
5485 * be changed between two allocations. This way we are safe.
5487 * @param value_table The value table
5488 * @param n The node to lookup
5490 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5493 n = identify(value_table, n);
5494 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5495 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5497 } /* identify_cons */
5500 * Return the canonical node computing the same value as n.
5501 * Looks up the node in a hash table, enters it in the table
5502 * if it isn't there yet.
5504 * @param value_table the HashSet containing all nodes in the
5506 * @param n the node to look up
5508 * @return a node that computes the same value as n or n if no such
5509 * node could be found
5511 ir_node *identify_remember(pset *value_table, ir_node *n) {
5514 if (!value_table) return n;
5517 /* lookup or insert in hash table with given hash key. */
5518 o = pset_insert(value_table, n, ir_node_hash(n));
5521 update_known_irn(o, n);
5526 } /* identify_remember */
5528 /* Add a node to the identities value table. */
5529 void add_identities(pset *value_table, ir_node *node) {
5530 if (get_opt_cse() && is_no_Block(node))
5531 identify_remember(value_table, node);
5532 } /* add_identities */
5534 /* Visit each node in the value table of a graph. */
5535 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5537 ir_graph *rem = current_ir_graph;
5539 current_ir_graph = irg;
5540 foreach_pset(irg->value_table, node)
5542 current_ir_graph = rem;
5543 } /* visit_all_identities */
5546 * Garbage in, garbage out. If a node has a dead input, i.e., the
5547 * Bad node is input to the node, return the Bad node.
5549 static ir_node *gigo(ir_node *node) {
5551 ir_op *op = get_irn_op(node);
5553 /* remove garbage blocks by looking at control flow that leaves the block
5554 and replacing the control flow by Bad. */
5555 if (get_irn_mode(node) == mode_X) {
5556 ir_node *block = get_nodes_block(skip_Proj(node));
5558 /* Don't optimize nodes in immature blocks. */
5559 if (!get_Block_matured(block))
5561 /* Don't optimize End, may have Bads. */
5562 if (op == op_End) return node;
5564 if (is_Block(block)) {
5565 if (is_Block_dead(block)) {
5566 /* control flow from dead block is dead */
5570 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5571 if (!is_Bad(get_irn_n(block, i)))
5575 ir_graph *irg = get_irn_irg(block);
5576 /* the start block is never dead */
5577 if (block != get_irg_start_block(irg)
5578 && block != get_irg_end_block(irg)) {
5580 * Do NOT kill control flow without setting
5581 * the block to dead of bad things can happen:
5582 * We get a Block that is not reachable be irg_block_walk()
5583 * but can be found by irg_walk()!
5585 set_Block_dead(block);
5592 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5593 blocks predecessors is dead. */
5594 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5595 irn_arity = get_irn_arity(node);
5598 * Beware: we can only read the block of a non-floating node.
5600 if (is_irn_pinned_in_irg(node) &&
5601 is_Block_dead(get_nodes_block(skip_Proj(node))))
5604 for (i = 0; i < irn_arity; i++) {
5605 ir_node *pred = get_irn_n(node, i);
5610 /* Propagating Unknowns here seems to be a bad idea, because
5611 sometimes we need a node as a input and did not want that
5613 However, it might be useful to move this into a later phase
5614 (if you think that optimizing such code is useful). */
5615 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5616 return new_Unknown(get_irn_mode(node));
5621 /* With this code we violate the agreement that local_optimize
5622 only leaves Bads in Block, Phi and Tuple nodes. */
5623 /* If Block has only Bads as predecessors it's garbage. */
5624 /* If Phi has only Bads as predecessors it's garbage. */
5625 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5626 irn_arity = get_irn_arity(node);
5627 for (i = 0; i < irn_arity; i++) {
5628 if (!is_Bad(get_irn_n(node, i))) break;
5630 if (i == irn_arity) node = new_Bad();
5637 * These optimizations deallocate nodes from the obstack.
5638 * It can only be called if it is guaranteed that no other nodes
5639 * reference this one, i.e., right after construction of a node.
5641 * @param n The node to optimize
5643 * current_ir_graph must be set to the graph of the node!
5645 ir_node *optimize_node(ir_node *n) {
5648 ir_opcode iro = get_irn_opcode(n);
5650 /* Always optimize Phi nodes: part of the construction. */
5651 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5653 /* constant expression evaluation / constant folding */
5654 if (get_opt_constant_folding()) {
5655 /* neither constants nor Tuple values can be evaluated */
5656 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5657 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5658 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5659 /* try to evaluate */
5660 tv = computed_value(n);
5661 if (tv != tarval_bad) {
5663 ir_type *old_tp = get_irn_type(n);
5664 int i, arity = get_irn_arity(n);
5668 * Try to recover the type of the new expression.
5670 for (i = 0; i < arity && !old_tp; ++i)
5671 old_tp = get_irn_type(get_irn_n(n, i));
5674 * we MUST copy the node here temporary, because it's still needed
5675 * for DBG_OPT_CSTEVAL
5677 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5678 oldn = alloca(node_size);
5680 memcpy(oldn, n, node_size);
5681 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5683 /* ARG, copy the in array, we need it for statistics */
5684 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5686 /* note the inplace edges module */
5687 edges_node_deleted(n, current_ir_graph);
5689 /* evaluation was successful -- replace the node. */
5690 irg_kill_node(current_ir_graph, n);
5691 nw = new_Const(get_tarval_mode(tv), tv);
5693 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5694 set_Const_type(nw, old_tp);
5695 DBG_OPT_CSTEVAL(oldn, nw);
5696 tarval_enable_fp_ops(old_fp_mode);
5699 tarval_enable_fp_ops(old_fp_mode);
5703 /* remove unnecessary nodes */
5704 if (get_opt_constant_folding() ||
5705 (iro == iro_Phi) || /* always optimize these nodes. */
5707 (iro == iro_Proj) ||
5708 (iro == iro_Block) ) /* Flags tested local. */
5709 n = equivalent_node(n);
5711 /* Common Subexpression Elimination.
5713 * Checks whether n is already available.
5714 * The block input is used to distinguish different subexpressions. Right
5715 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5716 * subexpressions within a block.
5719 n = identify_cons(current_ir_graph->value_table, n);
5722 edges_node_deleted(oldn, current_ir_graph);
5724 /* We found an existing, better node, so we can deallocate the old node. */
5725 irg_kill_node(current_ir_graph, oldn);
5729 /* Some more constant expression evaluation that does not allow to
5731 iro = get_irn_opcode(n);
5732 if (get_opt_constant_folding() ||
5733 (iro == iro_Cond) ||
5734 (iro == iro_Proj)) /* Flags tested local. */
5735 n = transform_node(n);
5737 /* Remove nodes with dead (Bad) input.
5738 Run always for transformation induced Bads. */
5741 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5742 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5743 n = identify_remember(current_ir_graph->value_table, n);
5747 } /* optimize_node */
5751 * These optimizations never deallocate nodes (in place). This can cause dead
5752 * nodes lying on the obstack. Remove these by a dead node elimination,
5753 * i.e., a copying garbage collection.
5755 ir_node *optimize_in_place_2(ir_node *n) {
5758 ir_opcode iro = get_irn_opcode(n);
5760 if (!get_opt_optimize() && !is_Phi(n)) return n;
5762 /* constant expression evaluation / constant folding */
5763 if (get_opt_constant_folding()) {
5764 /* neither constants nor Tuple values can be evaluated */
5765 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5766 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5767 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5768 /* try to evaluate */
5769 tv = computed_value(n);
5770 if (tv != tarval_bad) {
5771 /* evaluation was successful -- replace the node. */
5772 ir_type *old_tp = get_irn_type(n);
5773 int i, arity = get_irn_arity(n);
5776 * Try to recover the type of the new expression.
5778 for (i = 0; i < arity && !old_tp; ++i)
5779 old_tp = get_irn_type(get_irn_n(n, i));
5781 n = new_Const(get_tarval_mode(tv), tv);
5783 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5784 set_Const_type(n, old_tp);
5786 DBG_OPT_CSTEVAL(oldn, n);
5787 tarval_enable_fp_ops(old_fp_mode);
5790 tarval_enable_fp_ops(old_fp_mode);
5794 /* remove unnecessary nodes */
5795 if (get_opt_constant_folding() ||
5796 (iro == iro_Phi) || /* always optimize these nodes. */
5797 (iro == iro_Id) || /* ... */
5798 (iro == iro_Proj) || /* ... */
5799 (iro == iro_Block) ) /* Flags tested local. */
5800 n = equivalent_node(n);
5802 /** common subexpression elimination **/
5803 /* Checks whether n is already available. */
5804 /* The block input is used to distinguish different subexpressions. Right
5805 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5806 subexpressions within a block. */
5807 if (get_opt_cse()) {
5808 n = identify(current_ir_graph->value_table, n);
5811 /* Some more constant expression evaluation. */
5812 iro = get_irn_opcode(n);
5813 if (get_opt_constant_folding() ||
5814 (iro == iro_Cond) ||
5815 (iro == iro_Proj)) /* Flags tested local. */
5816 n = transform_node(n);
5818 /* Remove nodes with dead (Bad) input.
5819 Run always for transformation induced Bads. */
5822 /* Now we can verify the node, as it has no dead inputs any more. */
5825 /* Now we have a legal, useful node. Enter it in hash table for cse.
5826 Blocks should be unique anyways. (Except the successor of start:
5827 is cse with the start block!) */
5828 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5829 n = identify_remember(current_ir_graph->value_table, n);
5832 } /* optimize_in_place_2 */
5835 * Wrapper for external use, set proper status bits after optimization.
5837 ir_node *optimize_in_place(ir_node *n) {
5838 /* Handle graph state */
5839 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5841 if (get_opt_global_cse())
5842 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5843 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5844 set_irg_outs_inconsistent(current_ir_graph);
5846 /* FIXME: Maybe we could also test whether optimizing the node can
5847 change the control graph. */
5848 set_irg_doms_inconsistent(current_ir_graph);
5849 return optimize_in_place_2(n);
5850 } /* optimize_in_place */
5853 * Sets the default operation for an ir_ops.
5855 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5856 ops = firm_set_default_computed_value(code, ops);
5857 ops = firm_set_default_equivalent_node(code, ops);
5858 ops = firm_set_default_transform_node(code, ops);
5859 ops = firm_set_default_node_cmp_attr(code, ops);
5860 ops = firm_set_default_get_type(code, ops);
5861 ops = firm_set_default_get_type_attr(code, ops);
5862 ops = firm_set_default_get_entity_attr(code, ops);
5865 } /* firm_set_default_operations */