2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Return the value of a Constant.
59 static tarval *computed_value_Const(ir_node *n) {
60 return get_Const_tarval(n);
61 } /* computed_value_Const */
64 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
66 static tarval *computed_value_SymConst(ir_node *n) {
70 switch (get_SymConst_kind(n)) {
71 case symconst_type_size:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
76 case symconst_type_align:
77 type = get_SymConst_type(n);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
81 case symconst_ofs_ent:
82 ent = get_SymConst_entity(n);
83 type = get_entity_owner(ent);
84 if (get_type_state(type) == layout_fixed)
85 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
91 } /* computed_value_SymConst */
94 * Return the value of an Add.
96 static tarval *computed_value_Add(ir_node *n) {
97 ir_node *a = get_Add_left(n);
98 ir_node *b = get_Add_right(n);
100 tarval *ta = value_of(a);
101 tarval *tb = value_of(b);
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_add(ta, tb);
107 } /* computed_value_Add */
110 * Return the value of a Sub.
111 * Special case: a - a
113 static tarval *computed_value_Sub(ir_node *n) {
114 ir_node *a = get_Sub_left(n);
115 ir_node *b = get_Sub_right(n);
120 if (a == b && !is_Bad(a))
121 return get_mode_null(get_irn_mode(n));
126 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127 return tarval_sub(ta, tb);
130 } /* computed_value_Sub */
133 * Return the value of a Carry.
134 * Special : a op 0, 0 op b
136 static tarval *computed_value_Carry(ir_node *n) {
137 ir_node *a = get_binop_left(n);
138 ir_node *b = get_binop_right(n);
139 ir_mode *m = get_irn_mode(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad)) {
146 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
148 if (tarval_is_null(ta) || tarval_is_null(tb))
149 return get_mode_null(m);
152 } /* computed_value_Carry */
155 * Return the value of a Borrow.
158 static tarval *computed_value_Borrow(ir_node *n) {
159 ir_node *a = get_binop_left(n);
160 ir_node *b = get_binop_right(n);
161 ir_mode *m = get_irn_mode(n);
163 tarval *ta = value_of(a);
164 tarval *tb = value_of(b);
166 if ((ta != tarval_bad) && (tb != tarval_bad)) {
167 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
168 } else if (tarval_is_null(ta)) {
169 return get_mode_null(m);
172 } /* computed_value_Borrow */
175 * Return the value of an unary Minus.
177 static tarval *computed_value_Minus(ir_node *n) {
178 ir_node *a = get_Minus_op(n);
179 tarval *ta = value_of(a);
181 if (ta != tarval_bad)
182 return tarval_neg(ta);
185 } /* computed_value_Minus */
188 * Return the value of a Mul.
190 static tarval *computed_value_Mul(ir_node *n) {
191 ir_node *a = get_Mul_left(n);
192 ir_node *b = get_Mul_right(n);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 mode = get_irn_mode(n);
199 if (mode != get_irn_mode(a)) {
200 /* n * n = 2n bit multiplication */
201 ta = tarval_convert_to(ta, mode);
202 tb = tarval_convert_to(tb, mode);
205 if (ta != tarval_bad && tb != tarval_bad) {
206 return tarval_mul(ta, tb);
208 /* a*0 = 0 or 0*b = 0 */
209 if (ta == get_mode_null(mode))
211 if (tb == get_mode_null(mode))
215 } /* computed_value_Mul */
218 * Return the value of a floating point Quot.
220 static tarval *computed_value_Quot(ir_node *n) {
221 ir_node *a = get_Quot_left(n);
222 ir_node *b = get_Quot_right(n);
224 tarval *ta = value_of(a);
225 tarval *tb = value_of(b);
227 /* This was missing in original implementation. Why? */
228 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
229 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
230 return tarval_quo(ta, tb);
233 } /* computed_value_Quot */
236 * Calculate the value of an integer Div of two nodes.
237 * Special case: 0 / b
239 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
240 tarval *ta = value_of(a);
241 tarval *tb = value_of(b);
243 /* Compute c1 / c2 or 0 / a, a != 0 */
244 if (ta != tarval_bad) {
245 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
246 return tarval_div(ta, tb);
247 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
251 } /* do_computed_value_Div */
254 * Return the value of an integer Div.
256 static tarval *computed_value_Div(ir_node *n) {
257 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
258 } /* computed_value_Div */
261 * Calculate the value of an integer Mod of two nodes.
262 * Special case: a % 1
264 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 /* Compute c1 % c2 or a % 1 */
269 if (tb != tarval_bad) {
270 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
271 return tarval_mod(ta, tb);
272 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
273 return get_mode_null(get_irn_mode(a));
276 } /* do_computed_value_Mod */
279 * Return the value of an integer Mod.
281 static tarval *computed_value_Mod(ir_node *n) {
282 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
283 } /* computed_value_Mod */
286 * Return the value of an Abs.
288 static tarval *computed_value_Abs(ir_node *n) {
289 ir_node *a = get_Abs_op(n);
290 tarval *ta = value_of(a);
292 if (ta != tarval_bad)
293 return tarval_abs(ta);
296 } /* computed_value_Abs */
299 * Return the value of an And.
300 * Special case: a & 0, 0 & b
302 static tarval *computed_value_And(ir_node *n) {
303 ir_node *a = get_And_left(n);
304 ir_node *b = get_And_right(n);
306 tarval *ta = value_of(a);
307 tarval *tb = value_of(b);
309 if ((ta != tarval_bad) && (tb != tarval_bad)) {
310 return tarval_and (ta, tb);
312 if (tarval_is_null(ta)) return ta;
313 if (tarval_is_null(tb)) return tb;
316 } /* computed_value_And */
319 * Return the value of an Or.
320 * Special case: a | 1...1, 1...1 | b
322 static tarval *computed_value_Or(ir_node *n) {
323 ir_node *a = get_Or_left(n);
324 ir_node *b = get_Or_right(n);
326 tarval *ta = value_of(a);
327 tarval *tb = value_of(b);
329 if ((ta != tarval_bad) && (tb != tarval_bad)) {
330 return tarval_or (ta, tb);
332 if (tarval_is_all_one(ta)) return ta;
333 if (tarval_is_all_one(tb)) return tb;
336 } /* computed_value_Or */
339 * Return the value of an Eor.
341 static tarval *computed_value_Eor(ir_node *n) {
342 ir_node *a = get_Eor_left(n);
343 ir_node *b = get_Eor_right(n);
348 return get_mode_null(get_irn_mode(n));
353 if ((ta != tarval_bad) && (tb != tarval_bad)) {
354 return tarval_eor (ta, tb);
357 } /* computed_value_Eor */
360 * Return the value of a Not.
362 static tarval *computed_value_Not(ir_node *n) {
363 ir_node *a = get_Not_op(n);
364 tarval *ta = value_of(a);
366 if (ta != tarval_bad)
367 return tarval_not(ta);
370 } /* computed_value_Not */
373 * Return the value of a Shl.
375 static tarval *computed_value_Shl(ir_node *n) {
376 ir_node *a = get_Shl_left(n);
377 ir_node *b = get_Shl_right(n);
379 tarval *ta = value_of(a);
380 tarval *tb = value_of(b);
382 if ((ta != tarval_bad) && (tb != tarval_bad)) {
383 return tarval_shl (ta, tb);
386 } /* computed_value_Shl */
389 * Return the value of a Shr.
391 static tarval *computed_value_Shr(ir_node *n) {
392 ir_node *a = get_Shr_left(n);
393 ir_node *b = get_Shr_right(n);
395 tarval *ta = value_of(a);
396 tarval *tb = value_of(b);
398 if ((ta != tarval_bad) && (tb != tarval_bad)) {
399 return tarval_shr (ta, tb);
402 } /* computed_value_Shr */
405 * Return the value of a Shrs.
407 static tarval *computed_value_Shrs(ir_node *n) {
408 ir_node *a = get_Shrs_left(n);
409 ir_node *b = get_Shrs_right(n);
411 tarval *ta = value_of(a);
412 tarval *tb = value_of(b);
414 if ((ta != tarval_bad) && (tb != tarval_bad)) {
415 return tarval_shrs (ta, tb);
418 } /* computed_value_Shrs */
421 * Return the value of a Rot.
423 static tarval *computed_value_Rot(ir_node *n) {
424 ir_node *a = get_Rot_left(n);
425 ir_node *b = get_Rot_right(n);
427 tarval *ta = value_of(a);
428 tarval *tb = value_of(b);
430 if ((ta != tarval_bad) && (tb != tarval_bad)) {
431 return tarval_rot (ta, tb);
434 } /* computed_value_Rot */
437 * Return the value of a Conv.
439 static tarval *computed_value_Conv(ir_node *n) {
440 ir_node *a = get_Conv_op(n);
441 tarval *ta = value_of(a);
443 if (ta != tarval_bad)
444 return tarval_convert_to(ta, get_irn_mode(n));
447 } /* computed_value_Conv */
450 * Return the value of a Proj(Cmp).
452 * This performs a first step of unreachable code elimination.
453 * Proj can not be computed, but folding a Cmp above the Proj here is
454 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
456 * There are several case where we can evaluate a Cmp node, see later.
458 static tarval *computed_value_Proj_Cmp(ir_node *n) {
459 ir_node *a = get_Proj_pred(n);
460 ir_node *aa = get_Cmp_left(a);
461 ir_node *ab = get_Cmp_right(a);
462 long proj_nr = get_Proj_proj(n);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
469 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
472 /* This is a trick with the bits used for encoding the Cmp
473 Proj numbers, the following statement is not the same:
474 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
478 tarval *taa = value_of(aa);
479 tarval *tab = value_of(ab);
480 ir_mode *mode = get_irn_mode(aa);
483 * The predecessors of Cmp are target values. We can evaluate
486 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487 /* strange checks... */
488 pn_Cmp flags = tarval_cmp(taa, tab);
489 if (flags != pn_Cmp_False) {
490 return new_tarval_from_long (proj_nr & flags, mode_b);
493 /* for integer values, we can check against MIN/MAX */
494 else if (mode_is_int(mode)) {
495 /* MIN <=/> x. This results in true/false. */
496 if (taa == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Le)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Gt)
501 return get_tarval_b_false();
503 /* x >=/< MIN. This results in true/false. */
505 if (tab == get_mode_min(mode)) {
506 /* a compare with the MIN value */
507 if (proj_nr == pn_Cmp_Ge)
508 return get_tarval_b_true();
509 else if (proj_nr == pn_Cmp_Lt)
510 return get_tarval_b_false();
512 /* MAX >=/< x. This results in true/false. */
513 else if (taa == get_mode_max(mode)) {
514 if (proj_nr == pn_Cmp_Ge)
515 return get_tarval_b_true();
516 else if (proj_nr == pn_Cmp_Lt)
517 return get_tarval_b_false();
519 /* x <=/> MAX. This results in true/false. */
520 else if (tab == get_mode_max(mode)) {
521 if (proj_nr == pn_Cmp_Le)
522 return get_tarval_b_true();
523 else if (proj_nr == pn_Cmp_Gt)
524 return get_tarval_b_false();
528 * The predecessors are Allocs or (void*)(0) constants. Allocs never
529 * return NULL, they raise an exception. Therefore we can predict
533 ir_node *aaa = skip_Id(skip_Proj(aa));
534 ir_node *aba = skip_Id(skip_Proj(ab));
536 if ( ( (/* aa is ProjP and aaa is Alloc */
538 && mode_is_reference(get_irn_mode(aa))
540 && ( (/* ab is NULL */
542 && mode_is_reference(get_irn_mode(ab))
543 && is_Const_null(ab))
544 || (/* ab is other Alloc */
546 && mode_is_reference(get_irn_mode(ab))
549 || (/* aa is NULL and aba is Alloc */
551 && mode_is_reference(get_irn_mode(aa))
554 && mode_is_reference(get_irn_mode(ab))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 } /* computed_value_Proj_Cmp */
564 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565 * Proj(DivMod) and Proj(Quot).
567 static tarval *computed_value_Proj(ir_node *n) {
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(a);
595 if (get_Proj_proj(n) == pn_Quot_res)
596 return computed_value(a);
603 } /* computed_value_Proj */
606 * Calculate the value of a Mux: can be evaluated, if the
607 * sel and the right input are known.
609 static tarval *computed_value_Mux(ir_node *n) {
610 ir_node *sel = get_Mux_sel(n);
611 tarval *ts = value_of(sel);
613 if (ts == get_tarval_b_true()) {
614 ir_node *v = get_Mux_true(n);
617 else if (ts == get_tarval_b_false()) {
618 ir_node *v = get_Mux_false(n);
622 } /* computed_value_Mux */
625 * Calculate the value of a Psi: can be evaluated, if a condition is true
626 * and all previous conditions are false. If all conditions are false
627 * we evaluate to the default one.
629 static tarval *computed_value_Psi(ir_node *n) {
631 return computed_value_Mux(n);
633 } /* computed_value_Psi */
636 * Calculate the value of a Confirm: can be evaluated,
637 * if it has the form Confirm(x, '=', Const).
639 static tarval *computed_value_Confirm(ir_node *n) {
640 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641 value_of(get_Confirm_bound(n)) : tarval_bad;
642 } /* computed_value_Confirm */
645 * If the parameter n can be computed, return its value, else tarval_bad.
646 * Performs constant folding.
648 * @param n The node this should be evaluated
650 tarval *computed_value(ir_node *n) {
651 if (n->op->ops.computed_value)
652 return n->op->ops.computed_value(n);
654 } /* computed_value */
657 * Set the default computed_value evaluator in an ir_op_ops.
659 * @param code the opcode for the default operation
660 * @param ops the operations initialized
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
669 ops->computed_value = computed_value_##a; \
704 } /* firm_set_default_computed_value */
707 * Returns a equivalent block for another block.
708 * If the block has only one predecessor, this is
709 * the equivalent one. If the only predecessor of a block is
710 * the block itself, this is a dead block.
712 * If both predecessors of a block are the branches of a binary
713 * Cond, the equivalent block is Cond's block.
715 * If all predecessors of a block are bad or lies in a dead
716 * block, the current block is dead as well.
718 * Note, that blocks are NEVER turned into Bad's, instead
719 * the dead_block flag is set. So, never test for is_Bad(block),
720 * always use is_dead_Block(block).
722 static ir_node *equivalent_node_Block(ir_node *n)
727 /* don't optimize dead blocks */
728 if (is_Block_dead(n))
731 n_preds = get_Block_n_cfgpreds(n);
733 /* The Block constructor does not call optimize, but mature_immBlock()
734 calls the optimization. */
735 assert(get_Block_matured(n));
737 /* Straightening: a single entry Block following a single exit Block
738 can be merged, if it is not the Start block. */
739 /* !!! Beware, all Phi-nodes of n must have been optimized away.
740 This should be true, as the block is matured before optimize is called.
741 But what about Phi-cycles with the Phi0/Id that could not be resolved?
742 Remaining Phi nodes are just Ids. */
743 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
744 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
745 if (predblock == oldn) {
746 /* Jmp jumps into the block it is in -- deal self cycle. */
747 n = set_Block_dead(n);
748 DBG_OPT_DEAD_BLOCK(oldn, n);
749 } else if (get_opt_control_flow_straightening()) {
751 DBG_OPT_STG(oldn, n);
753 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
754 ir_node *predblock = get_Block_cfgpred_block(n, 0);
755 if (predblock == oldn) {
756 /* Jmp jumps into the block it is in -- deal self cycle. */
757 n = set_Block_dead(n);
758 DBG_OPT_DEAD_BLOCK(oldn, n);
760 } else if ((n_preds == 2) &&
761 (get_opt_control_flow_weak_simplification())) {
762 /* Test whether Cond jumps twice to this block
763 * The more general case which more than 2 predecessors is handles
764 * in optimize_cf(), we handle only this special case for speed here.
766 ir_node *a = get_Block_cfgpred(n, 0);
767 ir_node *b = get_Block_cfgpred(n, 1);
771 (get_Proj_pred(a) == get_Proj_pred(b)) &&
772 is_Cond(get_Proj_pred(a)) &&
773 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
774 /* Also a single entry Block following a single exit Block. Phis have
775 twice the same operand and will be optimized away. */
776 n = get_nodes_block(get_Proj_pred(a));
777 DBG_OPT_IFSIM1(oldn, a, b, n);
779 } else if (get_opt_unreachable_code() &&
780 (n != get_irg_start_block(current_ir_graph)) &&
781 (n != get_irg_end_block(current_ir_graph)) ) {
784 /* If all inputs are dead, this block is dead too, except if it is
785 the start or end block. This is one step of unreachable code
787 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
788 ir_node *pred = get_Block_cfgpred(n, i);
791 if (is_Bad(pred)) continue;
792 pred_blk = get_nodes_block(skip_Proj(pred));
794 if (is_Block_dead(pred_blk)) continue;
797 /* really found a living input */
802 n = set_Block_dead(n);
803 DBG_OPT_DEAD_BLOCK(oldn, n);
808 } /* equivalent_node_Block */
811 * Returns a equivalent node for a Jmp, a Bad :-)
812 * Of course this only happens if the Block of the Jmp is dead.
814 static ir_node *equivalent_node_Jmp(ir_node *n) {
815 /* unreachable code elimination */
816 if (is_Block_dead(get_nodes_block(n)))
820 } /* equivalent_node_Jmp */
822 /** Raise is handled in the same way as Jmp. */
823 #define equivalent_node_Raise equivalent_node_Jmp
826 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
827 See transform_node_Proj_Cond(). */
830 * Optimize operations that are commutative and have neutral 0,
831 * so a op 0 = 0 op a = a.
833 static ir_node *equivalent_node_neutral_zero(ir_node *n)
837 ir_node *a = get_binop_left(n);
838 ir_node *b = get_binop_right(n);
843 /* After running compute_node there is only one constant predecessor.
844 Find this predecessors value and remember the other node: */
845 if ((tv = value_of(a)) != tarval_bad) {
847 } else if ((tv = value_of(b)) != tarval_bad) {
852 /* If this predecessors constant value is zero, the operation is
853 * unnecessary. Remove it.
855 * Beware: If n is a Add, the mode of on and n might be different
856 * which happens in this rare construction: NULL + 3.
857 * Then, a Conv would be needed which we cannot include here.
859 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
866 } /* equivalent_node_neutral_zero */
869 * Eor is commutative and has neutral 0.
871 #define equivalent_node_Eor equivalent_node_neutral_zero
874 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
876 * The second one looks strange, but this construct
877 * is used heavily in the LCC sources :-).
879 * Beware: The Mode of an Add may be different than the mode of its
880 * predecessors, so we could not return a predecessors in all cases.
882 static ir_node *equivalent_node_Add(ir_node *n) {
884 ir_node *left, *right;
885 ir_mode *mode = get_irn_mode(n);
887 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
888 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
891 n = equivalent_node_neutral_zero(n);
895 left = get_Add_left(n);
896 right = get_Add_right(n);
899 if (get_Sub_right(left) == right) {
902 n = get_Sub_left(left);
903 if (mode == get_irn_mode(n)) {
904 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
910 if (get_Sub_right(right) == left) {
913 n = get_Sub_left(right);
914 if (mode == get_irn_mode(n)) {
915 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
921 } /* equivalent_node_Add */
924 * optimize operations that are not commutative but have neutral 0 on left,
927 static ir_node *equivalent_node_left_zero(ir_node *n) {
930 ir_node *a = get_binop_left(n);
931 ir_node *b = get_binop_right(n);
933 if (is_Const(b) && is_Const_null(b)) {
936 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
939 } /* equivalent_node_left_zero */
941 #define equivalent_node_Shl equivalent_node_left_zero
942 #define equivalent_node_Shr equivalent_node_left_zero
943 #define equivalent_node_Shrs equivalent_node_left_zero
944 #define equivalent_node_Rot equivalent_node_left_zero
947 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
949 * The second one looks strange, but this construct
950 * is used heavily in the LCC sources :-).
952 * Beware: The Mode of a Sub may be different than the mode of its
953 * predecessors, so we could not return a predecessors in all cases.
955 static ir_node *equivalent_node_Sub(ir_node *n) {
958 ir_mode *mode = get_irn_mode(n);
960 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
961 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
964 b = get_Sub_right(n);
966 /* Beware: modes might be different */
967 if (is_Const(b) && is_Const_null(b)) {
968 ir_node *a = get_Sub_left(n);
969 if (mode == get_irn_mode(a)) {
972 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
976 } /* equivalent_node_Sub */
980 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
983 * -(-a) == a, but might overflow two times.
984 * We handle it anyway here but the better way would be a
985 * flag. This would be needed for Pascal for instance.
987 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
989 ir_node *pred = get_unop_op(n);
991 /* optimize symmetric unop */
992 if (get_irn_op(pred) == get_irn_op(n)) {
993 n = get_unop_op(pred);
994 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
997 } /* equivalent_node_idempotent_unop */
999 /** Optimize Not(Not(x)) == x. */
1000 #define equivalent_node_Not equivalent_node_idempotent_unop
1002 /** -(-x) == x ??? Is this possible or can --x raise an
1003 out of bounds exception if min =! max? */
1004 #define equivalent_node_Minus equivalent_node_idempotent_unop
1007 * Optimize a * 1 = 1 * a = a.
1009 static ir_node *equivalent_node_Mul(ir_node *n) {
1011 ir_node *a = get_Mul_left(n);
1013 /* we can handle here only the n * n = n bit cases */
1014 if (get_irn_mode(n) == get_irn_mode(a)) {
1015 ir_node *b = get_Mul_right(n);
1017 /* Mul is commutative and has again an other neutral element. */
1018 if (is_Const(a) && is_Const_one(a)) {
1020 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1021 } else if (is_Const(b) && is_Const_one(b)) {
1023 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1027 } /* equivalent_node_Mul */
1030 * Optimize a / 1 = a.
1032 static ir_node *equivalent_node_Div(ir_node *n) {
1033 ir_node *a = get_Div_left(n);
1034 ir_node *b = get_Div_right(n);
1036 /* Div is not commutative. */
1037 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1038 /* Turn Div into a tuple (mem, bad, a) */
1039 ir_node *mem = get_Div_mem(n);
1040 ir_node *blk = get_irn_n(n, -1);
1041 turn_into_tuple(n, pn_Div_max);
1042 set_Tuple_pred(n, pn_Div_M, mem);
1043 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1044 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1045 set_Tuple_pred(n, pn_Div_res, a);
1048 } /* equivalent_node_Div */
1051 * Optimize a / 1.0 = a.
1053 static ir_node *equivalent_node_Quot(ir_node *n) {
1054 ir_node *a = get_Quot_left(n);
1055 ir_node *b = get_Quot_right(n);
1057 /* Div is not commutative. */
1058 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1059 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1060 ir_node *mem = get_Quot_mem(n);
1061 ir_node *blk = get_irn_n(n, -1);
1062 turn_into_tuple(n, pn_Quot_max);
1063 set_Tuple_pred(n, pn_Quot_M, mem);
1064 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1065 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1066 set_Tuple_pred(n, pn_Quot_res, a);
1069 } /* equivalent_node_Quot */
1072 * Optimize a / 1 = a.
1074 static ir_node *equivalent_node_DivMod(ir_node *n) {
1075 ir_node *b = get_DivMod_right(n);
1077 /* Div is not commutative. */
1078 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1079 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1080 ir_node *a = get_DivMod_left(n);
1081 ir_node *mem = get_Div_mem(n);
1082 ir_node *blk = get_irn_n(n, -1);
1083 ir_mode *mode = get_DivMod_resmode(n);
1085 turn_into_tuple(n, pn_DivMod_max);
1086 set_Tuple_pred(n, pn_DivMod_M, mem);
1087 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1088 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1089 set_Tuple_pred(n, pn_DivMod_res_div, a);
1090 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1093 } /* equivalent_node_DivMod */
1096 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1098 static ir_node *equivalent_node_Or(ir_node *n) {
1101 ir_node *a = get_Or_left(n);
1102 ir_node *b = get_Or_right(n);
1105 n = a; /* Or has it's own neutral element */
1106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1107 } else if (is_Const(a) && is_Const_null(a)) {
1109 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } else if (is_Const(b) && is_Const_null(b)) {
1112 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1116 } /* equivalent_node_Or */
1119 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1121 static ir_node *equivalent_node_And(ir_node *n) {
1124 ir_node *a = get_And_left(n);
1125 ir_node *b = get_And_right(n);
1128 n = a; /* And has it's own neutral element */
1129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1132 if (is_Const(a) && is_Const_all_one(a)) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1137 if (is_Const(b) && is_Const_all_one(b)) {
1139 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1151 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1154 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1160 } /* equivalent_node_And */
1163 * Try to remove useless Conv's:
1165 static ir_node *equivalent_node_Conv(ir_node *n) {
1167 ir_node *a = get_Conv_op(n);
1169 ir_mode *n_mode = get_irn_mode(n);
1170 ir_mode *a_mode = get_irn_mode(a);
1172 if (n_mode == a_mode) { /* No Conv necessary */
1173 if (get_Conv_strict(n)) {
1174 /* special case: the predecessor might be a also a Conv */
1176 if (! get_Conv_strict(a)) {
1177 /* first one is not strict, kick it */
1178 set_Conv_op(n, get_Conv_op(a));
1181 /* else both are strict conv, second is superflous */
1182 } else if(is_Proj(a)) {
1183 ir_node *pred = get_Proj_pred(a);
1185 /* loads always return with the exact precision of n_mode */
1186 assert(get_Load_mode(pred) == n_mode);
1191 /* leave strict floating point Conv's */
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1196 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1197 ir_node *b = get_Conv_op(a);
1198 ir_mode *b_mode = get_irn_mode(b);
1200 if (n_mode == b_mode) {
1201 if (n_mode == mode_b) {
1202 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1203 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1204 } else if (mode_is_int(n_mode)) {
1205 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1206 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1213 } /* equivalent_node_Conv */
1216 * A Cast may be removed if the type of the previous node
1217 * is already the type of the Cast.
1219 static ir_node *equivalent_node_Cast(ir_node *n) {
1221 ir_node *pred = get_Cast_op(n);
1223 if (get_irn_type(pred) == get_Cast_type(n)) {
1225 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1228 } /* equivalent_node_Cast */
1231 * Several optimizations:
1232 * - no Phi in start block.
1233 * - remove Id operators that are inputs to Phi
1234 * - fold Phi-nodes, iff they have only one predecessor except
1237 static ir_node *equivalent_node_Phi(ir_node *n) {
1242 ir_node *first_val = NULL; /* to shutup gcc */
1244 if (!get_opt_normalize()) return n;
1246 n_preds = get_Phi_n_preds(n);
1248 block = get_nodes_block(n);
1249 if ((is_Block_dead(block)) || /* Control dead */
1250 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1251 return new_Bad(); /* in the Start Block. */
1253 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1255 /* If the Block has a Bad pred, we also have one. */
1256 for (i = 0; i < n_preds; ++i)
1257 if (is_Bad(get_Block_cfgpred(block, i)))
1258 set_Phi_pred(n, i, new_Bad());
1260 /* Find first non-self-referencing input */
1261 for (i = 0; i < n_preds; ++i) {
1262 first_val = get_Phi_pred(n, i);
1263 if ( (first_val != n) /* not self pointer */
1265 && (! is_Bad(first_val))
1267 ) { /* value not dead */
1268 break; /* then found first value. */
1273 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1277 /* search for rest of inputs, determine if any of these
1278 are non-self-referencing */
1279 while (++i < n_preds) {
1280 ir_node *scnd_val = get_Phi_pred(n, i);
1281 if ( (scnd_val != n)
1282 && (scnd_val != first_val)
1284 && (! is_Bad(scnd_val))
1292 /* Fold, if no multiple distinct non-self-referencing inputs */
1294 DBG_OPT_PHI(oldn, n);
1297 } /* equivalent_node_Phi */
1300 * Several optimizations:
1301 * - no Sync in start block.
1302 * - fold Sync-nodes, iff they have only one predecessor except
1305 static ir_node *equivalent_node_Sync(ir_node *n) {
1306 int arity = get_Sync_n_preds(n);
1309 for (i = 0; i < arity;) {
1310 ir_node *pred = get_Sync_pred(n, i);
1313 /* Remove Bad predecessors */
1320 /* Remove duplicate predecessors */
1326 if (get_Sync_pred(n, j) == pred) {
1334 if (arity == 0) return new_Bad();
1335 if (arity == 1) return get_Sync_pred(n, 0);
1337 } /* equivalent_node_Sync */
1340 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1341 * ProjX(Load) and ProjX(Store).
1343 static ir_node *equivalent_node_Proj(ir_node *proj) {
1344 ir_node *oldn = proj;
1345 ir_node *a = get_Proj_pred(proj);
1348 /* Remove the Tuple/Proj combination. */
1349 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1350 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1351 DBG_OPT_TUPLE(oldn, a, proj);
1353 /* This should not happen! */
1354 assert(! "found a Proj with higher number than Tuple predecessors");
1357 } else if (get_irn_mode(proj) == mode_X) {
1358 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1359 /* Remove dead control flow -- early gigo(). */
1361 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1363 /* get the Load address */
1364 ir_node *addr = get_Load_ptr(a);
1365 ir_node *blk = get_irn_n(a, -1);
1368 if (value_not_null(addr, &confirm)) {
1369 if (confirm == NULL) {
1370 /* this node may float if it did not depend on a Confirm */
1371 set_irn_pinned(a, op_pin_state_floats);
1373 if (get_Proj_proj(proj) == pn_Load_X_except) {
1374 DBG_OPT_EXC_REM(proj);
1377 return new_r_Jmp(current_ir_graph, blk);
1379 } else if (is_Store(a)) {
1380 /* get the load/store address */
1381 ir_node *addr = get_Store_ptr(a);
1382 ir_node *blk = get_irn_n(a, -1);
1385 if (value_not_null(addr, &confirm)) {
1386 if (confirm == NULL) {
1387 /* this node may float if it did not depend on a Confirm */
1388 set_irn_pinned(a, op_pin_state_floats);
1390 if (get_Proj_proj(proj) == pn_Store_X_except) {
1391 DBG_OPT_EXC_REM(proj);
1394 return new_r_Jmp(current_ir_graph, blk);
1401 } /* equivalent_node_Proj */
1406 static ir_node *equivalent_node_Id(ir_node *n) {
1411 } while (get_irn_op(n) == op_Id);
1413 DBG_OPT_ID(oldn, n);
1415 } /* equivalent_node_Id */
1420 static ir_node *equivalent_node_Mux(ir_node *n)
1422 ir_node *oldn = n, *sel = get_Mux_sel(n);
1423 tarval *ts = value_of(sel);
1425 /* Mux(true, f, t) == t */
1426 if (ts == tarval_b_true) {
1427 n = get_Mux_true(n);
1428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1430 /* Mux(false, f, t) == f */
1431 else if (ts == tarval_b_false) {
1432 n = get_Mux_false(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1435 /* Mux(v, x, x) == x */
1436 else if (get_Mux_false(n) == get_Mux_true(n)) {
1437 n = get_Mux_true(n);
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1440 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1441 ir_node *cmp = get_Proj_pred(sel);
1442 long proj_nr = get_Proj_proj(sel);
1443 ir_node *f = get_Mux_false(n);
1444 ir_node *t = get_Mux_true(n);
1447 * Note further that these optimization work even for floating point
1448 * with NaN's because -NaN == NaN.
1449 * However, if +0 and -0 is handled differently, we cannot use the first one.
1452 ir_node *const cmp_l = get_Cmp_left(cmp);
1453 ir_node *const cmp_r = get_Cmp_right(cmp);
1457 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1458 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1460 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1467 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1468 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1477 * Note: normalization puts the constant on the right side,
1478 * so we check only one case.
1480 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1481 /* Mux(t CMP 0, X, t) */
1482 if (is_Minus(f) && get_Minus_op(f) == t) {
1483 /* Mux(t CMP 0, -t, t) */
1484 if (proj_nr == pn_Cmp_Eq) {
1485 /* Mux(t == 0, -t, t) ==> -t */
1487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1488 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1489 /* Mux(t != 0, -t, t) ==> t */
1491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1498 } /* equivalent_node_Mux */
1501 * Returns a equivalent node of a Psi: if a condition is true
1502 * and all previous conditions are false we know its value.
1503 * If all conditions are false its value is the default one.
1505 static ir_node *equivalent_node_Psi(ir_node *n) {
1507 return equivalent_node_Mux(n);
1509 } /* equivalent_node_Psi */
1512 * Optimize -a CMP -b into b CMP a.
1513 * This works only for for modes where unary Minus
1515 * Note that two-complement integers can Overflow
1516 * so it will NOT work.
1518 * For == and != can be handled in Proj(Cmp)
1520 static ir_node *equivalent_node_Cmp(ir_node *n) {
1521 ir_node *left = get_Cmp_left(n);
1522 ir_node *right = get_Cmp_right(n);
1524 if (is_Minus(left) && is_Minus(right) &&
1525 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1526 left = get_Minus_op(left);
1527 right = get_Minus_op(right);
1528 set_Cmp_left(n, right);
1529 set_Cmp_right(n, left);
1532 } /* equivalent_node_Cmp */
1535 * Remove Confirm nodes if setting is on.
1536 * Replace Confirms(x, '=', Constlike) by Constlike.
1538 static ir_node *equivalent_node_Confirm(ir_node *n) {
1539 ir_node *pred = get_Confirm_value(n);
1540 pn_Cmp pnc = get_Confirm_cmp(n);
1542 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1544 * rare case: two identical Confirms one after another,
1545 * replace the second one with the first.
1549 if (pnc == pn_Cmp_Eq) {
1550 ir_node *bound = get_Confirm_bound(n);
1553 * Optimize a rare case:
1554 * Confirm(x, '=', Constlike) ==> Constlike
1556 if (is_irn_constlike(bound)) {
1557 DBG_OPT_CONFIRM(n, bound);
1561 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1565 * Optimize CopyB(mem, x, x) into a Nop.
1567 static ir_node *equivalent_node_CopyB(ir_node *n) {
1568 ir_node *a = get_CopyB_dst(n);
1569 ir_node *b = get_CopyB_src(n);
1572 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1573 ir_node *mem = get_CopyB_mem(n);
1574 ir_node *blk = get_nodes_block(n);
1575 turn_into_tuple(n, pn_CopyB_max);
1576 set_Tuple_pred(n, pn_CopyB_M, mem);
1577 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1578 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1579 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1582 } /* equivalent_node_CopyB */
1585 * Optimize Bounds(idx, idx, upper) into idx.
1587 static ir_node *equivalent_node_Bound(ir_node *n) {
1588 ir_node *idx = get_Bound_index(n);
1589 ir_node *pred = skip_Proj(idx);
1592 if (is_Bound(pred)) {
1594 * idx was Bounds checked in the same MacroBlock previously,
1595 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1597 ir_node *lower = get_Bound_lower(n);
1598 ir_node *upper = get_Bound_upper(n);
1599 if (get_Bound_lower(pred) == lower &&
1600 get_Bound_upper(pred) == upper &&
1601 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1603 * One could expect that we simply return the previous
1604 * Bound here. However, this would be wrong, as we could
1605 * add an exception Proj to a new location then.
1606 * So, we must turn in into a tuple.
1612 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1613 ir_node *mem = get_Bound_mem(n);
1614 ir_node *blk = get_nodes_block(n);
1615 turn_into_tuple(n, pn_Bound_max);
1616 set_Tuple_pred(n, pn_Bound_M, mem);
1617 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1618 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1619 set_Tuple_pred(n, pn_Bound_res, idx);
1622 } /* equivalent_node_Bound */
1625 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1626 * perform no actual computation, as, e.g., the Id nodes. It does not create
1627 * new nodes. It is therefore safe to free n if the node returned is not n.
1628 * If a node returns a Tuple we can not just skip it. If the size of the
1629 * in array fits, we transform n into a tuple (e.g., Div).
1631 ir_node *equivalent_node(ir_node *n) {
1632 if (n->op->ops.equivalent_node)
1633 return n->op->ops.equivalent_node(n);
1635 } /* equivalent_node */
1638 * Sets the default equivalent node operation for an ir_op_ops.
1640 * @param code the opcode for the default operation
1641 * @param ops the operations initialized
1646 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1650 ops->equivalent_node = equivalent_node_##a; \
1690 } /* firm_set_default_equivalent_node */
1693 * Returns non-zero if a node is a Phi node
1694 * with all predecessors constant.
1696 static int is_const_Phi(ir_node *n) {
1699 if (! is_Phi(n) || get_irn_arity(n) == 0)
1701 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1702 if (! is_Const(get_irn_n(n, i)))
1705 } /* is_const_Phi */
1708 * Apply an evaluator on a binop with a constant operators (and one Phi).
1710 * @param phi the Phi node
1711 * @param other the other operand
1712 * @param eval an evaluator function
1713 * @param mode the mode of the result, may be different from the mode of the Phi!
1714 * @param left if non-zero, other is the left operand, else the right
1716 * @return a new Phi node if the conversion was successful, NULL else
1718 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1723 int i, n = get_irn_arity(phi);
1725 NEW_ARR_A(void *, res, n);
1727 for (i = 0; i < n; ++i) {
1728 pred = get_irn_n(phi, i);
1729 tv = get_Const_tarval(pred);
1730 tv = eval(other, tv);
1732 if (tv == tarval_bad) {
1733 /* folding failed, bad */
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 tv = get_Const_tarval(pred);
1742 tv = eval(tv, other);
1744 if (tv == tarval_bad) {
1745 /* folding failed, bad */
1751 irg = current_ir_graph;
1752 for (i = 0; i < n; ++i) {
1753 pred = get_irn_n(phi, i);
1754 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1755 mode, res[i], get_Const_type(pred));
1757 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1758 } /* apply_binop_on_phi */
1761 * Apply an evaluator on a binop with two constant Phi.
1763 * @param a the left Phi node
1764 * @param b the right Phi node
1765 * @param eval an evaluator function
1766 * @param mode the mode of the result, may be different from the mode of the Phi!
1768 * @return a new Phi node if the conversion was successful, NULL else
1770 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1771 tarval *tv_l, *tv_r, *tv;
1777 if (get_nodes_block(a) != get_nodes_block(b))
1780 n = get_irn_arity(a);
1781 NEW_ARR_A(void *, res, n);
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(a, i);
1785 tv_l = get_Const_tarval(pred);
1786 pred = get_irn_n(b, i);
1787 tv_r = get_Const_tarval(pred);
1788 tv = eval(tv_l, tv_r);
1790 if (tv == tarval_bad) {
1791 /* folding failed, bad */
1796 irg = current_ir_graph;
1797 for (i = 0; i < n; ++i) {
1798 pred = get_irn_n(a, i);
1799 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1801 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1802 } /* apply_binop_on_2_phis */
1805 * Apply an evaluator on a unop with a constant operator (a Phi).
1807 * @param phi the Phi node
1808 * @param eval an evaluator function
1810 * @return a new Phi node if the conversion was successful, NULL else
1812 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1818 int i, n = get_irn_arity(phi);
1820 NEW_ARR_A(void *, res, n);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 tv = get_Const_tarval(pred);
1826 if (tv == tarval_bad) {
1827 /* folding failed, bad */
1832 mode = get_irn_mode(phi);
1833 irg = current_ir_graph;
1834 for (i = 0; i < n; ++i) {
1835 pred = get_irn_n(phi, i);
1836 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1837 mode, res[i], get_Const_type(pred));
1839 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1840 } /* apply_unop_on_phi */
1843 * Apply a conversion on a constant operator (a Phi).
1845 * @param phi the Phi node
1847 * @return a new Phi node if the conversion was successful, NULL else
1849 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1854 int i, n = get_irn_arity(phi);
1856 NEW_ARR_A(void *, res, n);
1857 for (i = 0; i < n; ++i) {
1858 pred = get_irn_n(phi, i);
1859 tv = get_Const_tarval(pred);
1860 tv = tarval_convert_to(tv, mode);
1862 if (tv == tarval_bad) {
1863 /* folding failed, bad */
1868 irg = current_ir_graph;
1869 for (i = 0; i < n; ++i) {
1870 pred = get_irn_n(phi, i);
1871 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1872 mode, res[i], get_Const_type(pred));
1874 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1875 } /* apply_conv_on_phi */
1878 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1879 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1880 * If possible, remove the Conv's.
1882 static ir_node *transform_node_AddSub(ir_node *n) {
1883 ir_mode *mode = get_irn_mode(n);
1885 if (mode_is_reference(mode)) {
1886 ir_node *left = get_binop_left(n);
1887 ir_node *right = get_binop_right(n);
1888 unsigned ref_bits = get_mode_size_bits(mode);
1890 if (is_Conv(left)) {
1891 ir_mode *lmode = get_irn_mode(left);
1892 unsigned bits = get_mode_size_bits(lmode);
1894 if (ref_bits == bits &&
1895 mode_is_int(lmode) &&
1896 get_mode_arithmetic(lmode) == irma_twos_complement) {
1897 ir_node *pre = get_Conv_op(left);
1898 ir_mode *pre_mode = get_irn_mode(pre);
1900 if (mode_is_int(pre_mode) &&
1901 get_mode_size_bits(pre_mode) == bits &&
1902 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1903 /* ok, this conv just changes to sign, moreover the calculation
1904 * is done with same number of bits as our address mode, so
1905 * we can ignore the conv as address calculation can be viewed
1906 * as either signed or unsigned
1908 set_binop_left(n, pre);
1913 if (is_Conv(right)) {
1914 ir_mode *rmode = get_irn_mode(right);
1915 unsigned bits = get_mode_size_bits(rmode);
1917 if (ref_bits == bits &&
1918 mode_is_int(rmode) &&
1919 get_mode_arithmetic(rmode) == irma_twos_complement) {
1920 ir_node *pre = get_Conv_op(right);
1921 ir_mode *pre_mode = get_irn_mode(pre);
1923 if (mode_is_int(pre_mode) &&
1924 get_mode_size_bits(pre_mode) == bits &&
1925 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1926 /* ok, this conv just changes to sign, moreover the calculation
1927 * is done with same number of bits as our address mode, so
1928 * we can ignore the conv as address calculation can be viewed
1929 * as either signed or unsigned
1931 set_binop_right(n, pre);
1936 /* let address arithmetic use unsigned modes */
1937 if (is_Const(right)) {
1938 ir_mode *rmode = get_irn_mode(right);
1940 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1941 /* convert a AddP(P, *s) into AddP(P, *u) */
1942 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1944 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
1945 set_binop_right(n, pre);
1950 } /* transform_node_AddSub */
1952 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1954 if (is_Const(b) && is_const_Phi(a)) { \
1955 /* check for Op(Phi, Const) */ \
1956 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1958 else if (is_Const(a) && is_const_Phi(b)) { \
1959 /* check for Op(Const, Phi) */ \
1960 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1962 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1963 /* check for Op(Phi, Phi) */ \
1964 c = apply_binop_on_2_phis(a, b, eval, mode); \
1967 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1971 #define HANDLE_UNOP_PHI(eval, a, c) \
1973 if (is_const_Phi(a)) { \
1974 /* check for Op(Phi) */ \
1975 c = apply_unop_on_phi(a, eval); \
1977 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1983 * Do the AddSub optimization, then Transform
1984 * Constant folding on Phi
1985 * Add(a,a) -> Mul(a, 2)
1986 * Add(Mul(a, x), a) -> Mul(a, x+1)
1987 * if the mode is integer or float.
1988 * Transform Add(a,-b) into Sub(a,b).
1989 * Reassociation might fold this further.
1991 static ir_node *transform_node_Add(ir_node *n) {
1993 ir_node *a, *b, *c, *oldn = n;
1995 n = transform_node_AddSub(n);
1997 a = get_Add_left(n);
1998 b = get_Add_right(n);
2000 mode = get_irn_mode(n);
2001 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2003 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2004 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2007 if (mode_is_num(mode)) {
2008 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2009 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2010 ir_node *block = get_irn_n(n, -1);
2013 get_irn_dbg_info(n),
2017 new_r_Const_long(current_ir_graph, block, mode, 2),
2019 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2024 get_irn_dbg_info(n),
2030 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2035 get_irn_dbg_info(n),
2041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2044 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2045 /* Here we rely on constants be on the RIGHT side */
2047 ir_node *op = get_Not_op(a);
2049 if (is_Const(b) && is_Const_one(b)) {
2051 ir_node *blk = get_irn_n(n, -1);
2052 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2053 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2058 ir_node *blk = get_irn_n(n, -1);
2059 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2060 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2065 ir_node *op = get_Not_op(b);
2069 ir_node *blk = get_irn_n(n, -1);
2070 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2071 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2078 } /* transform_node_Add */
2081 * returns -cnst or NULL if impossible
2083 static ir_node *const_negate(ir_node *cnst) {
2084 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2085 dbg_info *dbgi = get_irn_dbg_info(cnst);
2086 ir_graph *irg = get_irn_irg(cnst);
2087 ir_node *block = get_nodes_block(cnst);
2088 ir_mode *mode = get_irn_mode(cnst);
2089 if (tv == tarval_bad) return NULL;
2090 return new_rd_Const(dbgi, irg, block, mode, tv);
2094 * Do the AddSub optimization, then Transform
2095 * Constant folding on Phi
2096 * Sub(0,a) -> Minus(a)
2097 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2098 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2099 * Sub(Add(a, x), x) -> a
2100 * Sub(x, Add(x, a)) -> -a
2101 * Sub(x, Const) -> Add(x, -Const)
2103 static ir_node *transform_node_Sub(ir_node *n) {
2108 n = transform_node_AddSub(n);
2110 a = get_Sub_left(n);
2111 b = get_Sub_right(n);
2113 mode = get_irn_mode(n);
2116 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2118 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2119 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2122 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2123 /* a - C -> a + (-C) */
2124 ir_node *cnst = const_negate(b);
2126 ir_node *block = get_nodes_block(n);
2127 dbg_info *dbgi = get_irn_dbg_info(n);
2128 ir_graph *irg = get_irn_irg(n);
2130 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2131 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2136 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2137 ir_graph *irg = current_ir_graph;
2138 dbg_info *dbg = get_irn_dbg_info(n);
2139 ir_node *block = get_nodes_block(n);
2140 ir_node *left = get_Minus_op(a);
2141 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2143 n = new_rd_Minus(dbg, irg, block, add, mode);
2144 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2146 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2147 ir_graph *irg = current_ir_graph;
2148 dbg_info *dbg = get_irn_dbg_info(n);
2149 ir_node *block = get_nodes_block(n);
2150 ir_node *right = get_Minus_op(b);
2152 n = new_rd_Add(dbg, irg, block, a, right, mode);
2153 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2155 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2156 ir_graph *irg = current_ir_graph;
2157 dbg_info *s_dbg = get_irn_dbg_info(b);
2158 ir_node *s_block = get_nodes_block(b);
2159 ir_node *s_left = get_Sub_right(b);
2160 ir_node *s_right = get_Sub_left(b);
2161 ir_mode *s_mode = get_irn_mode(b);
2162 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2163 dbg_info *a_dbg = get_irn_dbg_info(n);
2164 ir_node *a_block = get_nodes_block(n);
2166 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2167 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2169 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2170 ir_node *m_right = get_Mul_right(b);
2171 if (is_Const(m_right)) {
2172 ir_node *cnst2 = const_negate(m_right);
2173 if (cnst2 != NULL) {
2174 ir_graph *irg = current_ir_graph;
2175 dbg_info *m_dbg = get_irn_dbg_info(b);
2176 ir_node *m_block = get_nodes_block(b);
2177 ir_node *m_left = get_Mul_left(b);
2178 ir_mode *m_mode = get_irn_mode(b);
2179 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2180 dbg_info *a_dbg = get_irn_dbg_info(n);
2181 ir_node *a_block = get_nodes_block(n);
2183 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2190 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2191 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2193 get_irn_dbg_info(n),
2198 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2202 if (mode_wrap_around(mode)) {
2203 ir_node *left = get_Add_left(a);
2204 ir_node *right = get_Add_right(a);
2206 /* FIXME: Does the Conv's work only for two complement or generally? */
2208 if (mode != get_irn_mode(right)) {
2209 /* This Sub is an effective Cast */
2210 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2215 } else if (right == b) {
2216 if (mode != get_irn_mode(left)) {
2217 /* This Sub is an effective Cast */
2218 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2221 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2227 if (mode_wrap_around(mode)) {
2228 ir_node *left = get_Add_left(b);
2229 ir_node *right = get_Add_right(b);
2231 /* FIXME: Does the Conv's work only for two complement or generally? */
2233 ir_mode *r_mode = get_irn_mode(right);
2235 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2236 if (mode != r_mode) {
2237 /* This Sub is an effective Cast */
2238 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2240 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2242 } else if (right == a) {
2243 ir_mode *l_mode = get_irn_mode(left);
2245 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2246 if (mode != l_mode) {
2247 /* This Sub is an effective Cast */
2248 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2250 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2255 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2256 ir_mode *mode = get_irn_mode(a);
2258 if (mode == get_irn_mode(b)) {
2260 ir_node *op_a = get_Conv_op(a);
2261 ir_node *op_b = get_Conv_op(b);
2263 /* check if it's allowed to skip the conv */
2264 ma = get_irn_mode(op_a);
2265 mb = get_irn_mode(op_b);
2267 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2268 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2271 set_Sub_right(n, b);
2277 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2278 if (!is_reassoc_running() && is_Mul(a)) {
2279 ir_node *ma = get_Mul_left(a);
2280 ir_node *mb = get_Mul_right(a);
2283 ir_node *blk = get_irn_n(n, -1);
2285 get_irn_dbg_info(n),
2286 current_ir_graph, blk,
2289 get_irn_dbg_info(n),
2290 current_ir_graph, blk,
2292 new_r_Const_long(current_ir_graph, blk, mode, 1),
2295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2297 } else if (mb == b) {
2298 ir_node *blk = get_irn_n(n, -1);
2300 get_irn_dbg_info(n),
2301 current_ir_graph, blk,
2304 get_irn_dbg_info(n),
2305 current_ir_graph, blk,
2307 new_r_Const_long(current_ir_graph, blk, mode, 1),
2310 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2314 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2315 ir_node *x = get_Sub_left(a);
2316 ir_node *y = get_Sub_right(a);
2317 ir_node *blk = get_irn_n(n, -1);
2318 ir_mode *m_b = get_irn_mode(b);
2319 ir_mode *m_y = get_irn_mode(y);
2323 /* Determine the right mode for the Add. */
2326 else if (mode_is_reference(m_b))
2328 else if (mode_is_reference(m_y))
2332 * Both modes are different but none is reference,
2333 * happens for instance in SubP(SubP(P, Iu), Is).
2334 * We have two possibilities here: Cast or ignore.
2335 * Currently we ignore this case.
2340 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2342 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2343 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2347 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2348 if (is_Const(a) && is_Not(b)) {
2349 /* c - ~X = X + (c+1) */
2350 tarval *tv = get_Const_tarval(a);
2352 tv = tarval_add(tv, get_mode_one(mode));
2353 if (tv != tarval_bad) {
2354 ir_node *blk = get_irn_n(n, -1);
2355 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2356 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2363 } /* transform_node_Sub */
2366 * Several transformation done on n*n=2n bits mul.
2367 * These transformations must be done here because new nodes may be produced.
2369 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2371 ir_node *a = get_Mul_left(n);
2372 ir_node *b = get_Mul_right(n);
2373 tarval *ta = value_of(a);
2374 tarval *tb = value_of(b);
2375 ir_mode *smode = get_irn_mode(a);
2377 if (ta == get_mode_one(smode)) {
2378 /* (L)1 * (L)b = (L)b */
2379 ir_node *blk = get_irn_n(n, -1);
2380 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2381 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2384 else if (ta == get_mode_minus_one(smode)) {
2385 /* (L)-1 * (L)b = (L)b */
2386 ir_node *blk = get_irn_n(n, -1);
2387 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2388 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2389 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2392 if (tb == get_mode_one(smode)) {
2393 /* (L)a * (L)1 = (L)a */
2394 ir_node *blk = get_irn_n(a, -1);
2395 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2396 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2399 else if (tb == get_mode_minus_one(smode)) {
2400 /* (L)a * (L)-1 = (L)-a */
2401 ir_node *blk = get_irn_n(n, -1);
2402 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2403 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2404 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2411 * Transform Mul(a,-1) into -a.
2412 * Do constant evaluation of Phi nodes.
2413 * Do architecture dependent optimizations on Mul nodes
2415 static ir_node *transform_node_Mul(ir_node *n) {
2416 ir_node *c, *oldn = n;
2417 ir_mode *mode = get_irn_mode(n);
2418 ir_node *a = get_Mul_left(n);
2419 ir_node *b = get_Mul_right(n);
2421 if (is_Bad(a) || is_Bad(b))
2424 if (mode != get_irn_mode(a))
2425 return transform_node_Mul2n(n, mode);
2427 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2429 if (mode_is_signed(mode)) {
2432 if (value_of(a) == get_mode_minus_one(mode))
2434 else if (value_of(b) == get_mode_minus_one(mode))
2437 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2438 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2443 if (is_Const(b)) { /* (-a) * const -> a * -const */
2444 ir_node *cnst = const_negate(b);
2446 dbg_info *dbgi = get_irn_dbg_info(n);
2447 ir_node *block = get_nodes_block(n);
2448 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2449 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2452 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2453 dbg_info *dbgi = get_irn_dbg_info(n);
2454 ir_node *block = get_nodes_block(n);
2455 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2456 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2458 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2459 ir_node *sub_l = get_Sub_left(b);
2460 ir_node *sub_r = get_Sub_right(b);
2461 dbg_info *dbgi = get_irn_dbg_info(n);
2462 ir_graph *irg = current_ir_graph;
2463 ir_node *block = get_nodes_block(n);
2464 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2465 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2466 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2469 } else if (is_Minus(b)) {
2470 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2471 ir_node *sub_l = get_Sub_left(a);
2472 ir_node *sub_r = get_Sub_right(a);
2473 dbg_info *dbgi = get_irn_dbg_info(n);
2474 ir_graph *irg = current_ir_graph;
2475 ir_node *block = get_nodes_block(n);
2476 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2477 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2482 if (get_mode_arithmetic(mode) == irma_ieee754) {
2484 tarval *tv = get_Const_tarval(a);
2485 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2486 /* 2.0 * b = b + b */
2487 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2488 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2492 else if (is_Const(b)) {
2493 tarval *tv = get_Const_tarval(b);
2494 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2495 /* a * 2.0 = a + a */
2496 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2497 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2502 return arch_dep_replace_mul_with_shifts(n);
2503 } /* transform_node_Mul */
2506 * Transform a Div Node.
2508 static ir_node *transform_node_Div(ir_node *n) {
2509 ir_mode *mode = get_Div_resmode(n);
2510 ir_node *a = get_Div_left(n);
2511 ir_node *b = get_Div_right(n);
2515 if (is_Const(b) && is_const_Phi(a)) {
2516 /* check for Div(Phi, Const) */
2517 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2519 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2523 else if (is_Const(a) && is_const_Phi(b)) {
2524 /* check for Div(Const, Phi) */
2525 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2527 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2531 else if (is_const_Phi(a) && is_const_Phi(b)) {
2532 /* check for Div(Phi, Phi) */
2533 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2535 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2542 if (tv != tarval_bad) {
2543 value = new_Const(get_tarval_mode(tv), tv);
2545 DBG_OPT_CSTEVAL(n, value);
2548 ir_node *a = get_Div_left(n);
2549 ir_node *b = get_Div_right(n);
2552 if (a == b && value_not_zero(a, &dummy)) {
2553 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2554 value = new_Const(mode, get_mode_one(mode));
2555 DBG_OPT_CSTEVAL(n, value);
2558 if (mode_is_signed(mode) && is_Const(b)) {
2559 tarval *tv = get_Const_tarval(b);
2561 if (tv == get_mode_minus_one(mode)) {
2563 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2564 DBG_OPT_CSTEVAL(n, value);
2568 /* Try architecture dependent optimization */
2569 value = arch_dep_replace_div_by_const(n);
2577 /* Turn Div into a tuple (mem, jmp, bad, value) */
2578 mem = get_Div_mem(n);
2579 blk = get_irn_n(n, -1);
2581 /* skip a potential Pin */
2583 mem = get_Pin_op(mem);
2584 turn_into_tuple(n, pn_Div_max);
2585 set_Tuple_pred(n, pn_Div_M, mem);
2586 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2587 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2588 set_Tuple_pred(n, pn_Div_res, value);
2591 } /* transform_node_Div */
2594 * Transform a Mod node.
2596 static ir_node *transform_node_Mod(ir_node *n) {
2597 ir_mode *mode = get_Mod_resmode(n);
2598 ir_node *a = get_Mod_left(n);
2599 ir_node *b = get_Mod_right(n);
2603 if (is_Const(b) && is_const_Phi(a)) {
2604 /* check for Div(Phi, Const) */
2605 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2607 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2611 else if (is_Const(a) && is_const_Phi(b)) {
2612 /* check for Div(Const, Phi) */
2613 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2615 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2619 else if (is_const_Phi(a) && is_const_Phi(b)) {
2620 /* check for Div(Phi, Phi) */
2621 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2623 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2630 if (tv != tarval_bad) {
2631 value = new_Const(get_tarval_mode(tv), tv);
2633 DBG_OPT_CSTEVAL(n, value);
2636 ir_node *a = get_Mod_left(n);
2637 ir_node *b = get_Mod_right(n);
2640 if (a == b && value_not_zero(a, &dummy)) {
2641 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2642 value = new_Const(mode, get_mode_null(mode));
2643 DBG_OPT_CSTEVAL(n, value);
2646 if (mode_is_signed(mode) && is_Const(b)) {
2647 tarval *tv = get_Const_tarval(b);
2649 if (tv == get_mode_minus_one(mode)) {
2651 value = new_Const(mode, get_mode_null(mode));
2652 DBG_OPT_CSTEVAL(n, value);
2656 /* Try architecture dependent optimization */
2657 value = arch_dep_replace_mod_by_const(n);
2665 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2666 mem = get_Mod_mem(n);
2667 blk = get_irn_n(n, -1);
2669 /* skip a potential Pin */
2671 mem = get_Pin_op(mem);
2672 turn_into_tuple(n, pn_Mod_max);
2673 set_Tuple_pred(n, pn_Mod_M, mem);
2674 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2675 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2676 set_Tuple_pred(n, pn_Mod_res, value);
2679 } /* transform_node_Mod */
2682 * Transform a DivMod node.
2684 static ir_node *transform_node_DivMod(ir_node *n) {
2686 ir_node *a = get_DivMod_left(n);
2687 ir_node *b = get_DivMod_right(n);
2688 ir_mode *mode = get_DivMod_resmode(n);
2693 if (is_Const(b) && is_const_Phi(a)) {
2694 /* check for Div(Phi, Const) */
2695 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2696 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2698 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2699 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2703 else if (is_Const(a) && is_const_Phi(b)) {
2704 /* check for Div(Const, Phi) */
2705 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2706 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2708 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2709 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2713 else if (is_const_Phi(a) && is_const_Phi(b)) {
2714 /* check for Div(Phi, Phi) */
2715 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2716 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2718 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2719 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2726 if (tb != tarval_bad) {
2727 if (tb == get_mode_one(get_tarval_mode(tb))) {
2729 vb = new_Const(mode, get_mode_null(mode));
2730 DBG_OPT_CSTEVAL(n, vb);
2732 } else if (ta != tarval_bad) {
2733 tarval *resa, *resb;
2734 resa = tarval_div(ta, tb);
2735 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2736 Jmp for X result!? */
2737 resb = tarval_mod(ta, tb);
2738 if (resb == tarval_bad) return n; /* Causes exception! */
2739 va = new_Const(mode, resa);
2740 vb = new_Const(mode, resb);
2741 DBG_OPT_CSTEVAL(n, va);
2742 DBG_OPT_CSTEVAL(n, vb);
2744 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2745 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2746 vb = new_Const(mode, get_mode_null(mode));
2747 DBG_OPT_CSTEVAL(n, va);
2748 DBG_OPT_CSTEVAL(n, vb);
2750 } else { /* Try architecture dependent optimization */
2753 arch_dep_replace_divmod_by_const(&va, &vb, n);
2754 evaluated = va != NULL;
2756 } else if (a == b) {
2757 if (value_not_zero(a, &dummy)) {
2759 va = new_Const(mode, get_mode_one(mode));
2760 vb = new_Const(mode, get_mode_null(mode));
2761 DBG_OPT_CSTEVAL(n, va);
2762 DBG_OPT_CSTEVAL(n, vb);
2765 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2768 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2769 /* 0 / non-Const = 0 */
2774 if (evaluated) { /* replace by tuple */
2778 mem = get_DivMod_mem(n);
2779 /* skip a potential Pin */
2781 mem = get_Pin_op(mem);
2783 blk = get_irn_n(n, -1);
2784 turn_into_tuple(n, pn_DivMod_max);
2785 set_Tuple_pred(n, pn_DivMod_M, mem);
2786 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2787 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2788 set_Tuple_pred(n, pn_DivMod_res_div, va);
2789 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2793 } /* transform_node_DivMod */
2796 * Optimize x / c to x * (1/c)
2798 static ir_node *transform_node_Quot(ir_node *n) {
2799 ir_mode *mode = get_Quot_resmode(n);
2802 if (get_mode_arithmetic(mode) == irma_ieee754) {
2803 ir_node *b = get_Quot_right(n);
2806 tarval *tv = get_Const_tarval(b);
2808 tv = tarval_quo(get_mode_one(mode), tv);
2810 /* Do the transformation if the result is either exact or we are not
2811 using strict rules. */
2812 if (tv != tarval_bad &&
2813 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2814 ir_node *blk = get_irn_n(n, -1);
2815 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2816 ir_node *a = get_Quot_left(n);
2817 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2818 ir_node *mem = get_Quot_mem(n);
2820 /* skip a potential Pin */
2822 mem = get_Pin_op(mem);
2823 turn_into_tuple(n, pn_Quot_max);
2824 set_Tuple_pred(n, pn_Quot_M, mem);
2825 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2826 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2827 set_Tuple_pred(n, pn_Quot_res, m);
2828 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2833 } /* transform_node_Quot */
2836 * Optimize Abs(x) into x if x is Confirmed >= 0
2837 * Optimize Abs(x) into -x if x is Confirmed <= 0
2838 * Optimize Abs(-x) int Abs(x)
2840 static ir_node *transform_node_Abs(ir_node *n) {
2841 ir_node *c, *oldn = n;
2842 ir_node *a = get_Abs_op(n);
2845 HANDLE_UNOP_PHI(tarval_abs, a, c);
2847 switch (classify_value_sign(a)) {
2848 case value_classified_negative:
2849 mode = get_irn_mode(n);
2852 * We can replace the Abs by -x here.
2853 * We even could add a new Confirm here
2854 * (if not twos complement)
2856 * Note that -x would create a new node, so we could
2857 * not run it in the equivalent_node() context.
2859 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2860 get_nodes_block(n), a, mode);
2862 DBG_OPT_CONFIRM(oldn, n);
2864 case value_classified_positive:
2865 /* n is positive, Abs is not needed */
2868 DBG_OPT_CONFIRM(oldn, n);
2874 /* Abs(-x) = Abs(x) */
2875 mode = get_irn_mode(n);
2876 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2877 get_nodes_block(n), get_Minus_op(a), mode);
2878 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2882 } /* transform_node_Abs */
2885 * Transform a Cond node.
2887 * Replace the Cond by a Jmp if it branches on a constant
2890 static ir_node *transform_node_Cond(ir_node *n) {
2893 ir_node *a = get_Cond_selector(n);
2894 tarval *ta = value_of(a);
2896 /* we need block info which is not available in floating irgs */
2897 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2900 if ((ta != tarval_bad) &&
2901 (get_irn_mode(a) == mode_b) &&
2902 (get_opt_unreachable_code())) {
2903 /* It's a boolean Cond, branching on a boolean constant.
2904 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2905 ir_node *blk = get_nodes_block(n);
2906 jmp = new_r_Jmp(current_ir_graph, blk);
2907 turn_into_tuple(n, pn_Cond_max);
2908 if (ta == tarval_b_true) {
2909 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2910 set_Tuple_pred(n, pn_Cond_true, jmp);
2912 set_Tuple_pred(n, pn_Cond_false, jmp);
2913 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2915 /* We might generate an endless loop, so keep it alive. */
2916 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2919 } /* transform_node_Cond */
2922 * Prototype of a recursive transform function
2923 * for bitwise distributive transformations.
2925 typedef ir_node* (*recursive_transform)(ir_node *n);
2928 * makes use of distributive laws for and, or, eor
2929 * and(a OP c, b OP c) -> and(a, b) OP c
2930 * note, might return a different op than n
2932 static ir_node *transform_bitwise_distributive(ir_node *n,
2933 recursive_transform trans_func)
2936 ir_node *a = get_binop_left(n);
2937 ir_node *b = get_binop_right(n);
2938 ir_op *op = get_irn_op(a);
2939 ir_op *op_root = get_irn_op(n);
2941 if(op != get_irn_op(b))
2944 if (op == op_Conv) {
2945 ir_node *a_op = get_Conv_op(a);
2946 ir_node *b_op = get_Conv_op(b);
2947 ir_mode *a_mode = get_irn_mode(a_op);
2948 ir_mode *b_mode = get_irn_mode(b_op);
2949 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2950 ir_node *blk = get_irn_n(n, -1);
2953 set_binop_left(n, a_op);
2954 set_binop_right(n, b_op);
2955 set_irn_mode(n, a_mode);
2957 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2959 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2965 /* nothing to gain here */
2969 if (op == op_Shrs || op == op_Shr || op == op_Shl
2970 || op == op_And || op == op_Or || op == op_Eor) {
2971 ir_node *a_left = get_binop_left(a);
2972 ir_node *a_right = get_binop_right(a);
2973 ir_node *b_left = get_binop_left(b);
2974 ir_node *b_right = get_binop_right(b);
2976 ir_node *op1 = NULL;
2977 ir_node *op2 = NULL;
2979 if (is_op_commutative(op)) {
2980 if (a_left == b_left) {
2984 } else if(a_left == b_right) {
2988 } else if(a_right == b_left) {
2994 if(a_right == b_right) {
3001 /* (a sop c) & (b sop c) => (a & b) sop c */
3002 ir_node *blk = get_irn_n(n, -1);
3004 ir_node *new_n = exact_copy(n);
3005 set_binop_left(new_n, op1);
3006 set_binop_right(new_n, op2);
3007 new_n = trans_func(new_n);
3009 if(op_root == op_Eor && op == op_Or) {
3010 dbg_info *dbgi = get_irn_dbg_info(n);
3011 ir_graph *irg = current_ir_graph;
3012 ir_mode *mode = get_irn_mode(c);
3014 c = new_rd_Not(dbgi, irg, blk, c, mode);
3015 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3018 set_nodes_block(n, blk);
3019 set_binop_left(n, new_n);
3020 set_binop_right(n, c);
3021 add_identities(current_ir_graph->value_table, n);
3024 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3035 static ir_node *transform_node_And(ir_node *n) {
3036 ir_node *c, *oldn = n;
3037 ir_node *a = get_And_left(n);
3038 ir_node *b = get_And_right(n);
3041 mode = get_irn_mode(n);
3042 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3044 /* we can evaluate 2 Projs of the same Cmp */
3045 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3046 ir_node *pred_a = get_Proj_pred(a);
3047 ir_node *pred_b = get_Proj_pred(b);
3048 if (pred_a == pred_b) {
3049 dbg_info *dbgi = get_irn_dbg_info(n);
3050 ir_node *block = get_nodes_block(pred_a);
3051 pn_Cmp pn_a = get_Proj_proj(a);
3052 pn_Cmp pn_b = get_Proj_proj(b);
3053 /* yes, we can simply calculate with pncs */
3054 pn_Cmp new_pnc = pn_a & pn_b;
3056 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3061 ir_node *op = get_Not_op(b);
3063 ir_node *ba = get_And_left(op);
3064 ir_node *bb = get_And_right(op);
3066 /* it's enough to test the following cases due to normalization! */
3067 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3068 /* (a|b) & ~(a&b) = a^b */
3069 ir_node *block = get_nodes_block(n);
3071 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3072 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3080 ir_node *op = get_Not_op(a);
3082 ir_node *aa = get_And_left(op);
3083 ir_node *ab = get_And_right(op);
3085 /* it's enough to test the following cases due to normalization! */
3086 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3087 /* (a|b) & ~(a&b) = a^b */
3088 ir_node *block = get_nodes_block(n);
3090 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3091 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3098 ir_node *al = get_Eor_left(a);
3099 ir_node *ar = get_Eor_right(a);
3102 /* (b ^ a) & b -> ~a & b */
3103 dbg_info *dbg = get_irn_dbg_info(n);
3104 ir_node *block = get_nodes_block(n);
3106 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3107 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3108 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3112 /* (a ^ b) & b -> ~a & b */
3113 dbg_info *dbg = get_irn_dbg_info(n);
3114 ir_node *block = get_nodes_block(n);
3116 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3117 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3118 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3123 ir_node *bl = get_Eor_left(b);
3124 ir_node *br = get_Eor_right(b);
3127 /* a & (a ^ b) -> a & ~b */
3128 dbg_info *dbg = get_irn_dbg_info(n);
3129 ir_node *block = get_nodes_block(n);
3131 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3132 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3133 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3137 /* a & (b ^ a) -> a & ~b */
3138 dbg_info *dbg = get_irn_dbg_info(n);
3139 ir_node *block = get_nodes_block(n);
3141 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3142 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3147 if (is_Not(a) && is_Not(b)) {
3148 /* ~a & ~b = ~(a|b) */
3149 ir_node *block = get_nodes_block(n);
3150 ir_mode *mode = get_irn_mode(n);
3154 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3155 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3156 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3160 n = transform_bitwise_distributive(n, transform_node_And);
3163 } /* transform_node_And */
3168 static ir_node *transform_node_Eor(ir_node *n) {
3169 ir_node *c, *oldn = n;
3170 ir_node *a = get_Eor_left(n);
3171 ir_node *b = get_Eor_right(n);
3172 ir_mode *mode = get_irn_mode(n);
3174 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3176 /* we can evaluate 2 Projs of the same Cmp */
3177 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3178 ir_node *pred_a = get_Proj_pred(a);
3179 ir_node *pred_b = get_Proj_pred(b);
3180 if(pred_a == pred_b) {
3181 dbg_info *dbgi = get_irn_dbg_info(n);
3182 ir_node *block = get_nodes_block(pred_a);
3183 pn_Cmp pn_a = get_Proj_proj(a);
3184 pn_Cmp pn_b = get_Proj_proj(b);
3185 /* yes, we can simply calculate with pncs */
3186 pn_Cmp new_pnc = pn_a ^ pn_b;
3188 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3195 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3196 mode, get_mode_null(mode));
3197 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3198 } else if (mode == mode_b &&
3200 is_Const(b) && is_Const_one(b) &&
3201 is_Cmp(get_Proj_pred(a))) {
3202 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3203 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3204 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3206 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3207 } else if (is_Const(b)) {
3208 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3209 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3210 ir_node *not_op = get_Not_op(a);
3211 dbg_info *dbg = get_irn_dbg_info(n);
3212 ir_graph *irg = current_ir_graph;
3213 ir_node *block = get_nodes_block(n);
3214 ir_mode *mode = get_irn_mode(n);
3215 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3217 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3218 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3219 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3222 n = transform_bitwise_distributive(n, transform_node_Eor);
3226 } /* transform_node_Eor */
3231 static ir_node *transform_node_Not(ir_node *n) {
3232 ir_node *c, *oldn = n;
3233 ir_node *a = get_Not_op(n);
3234 ir_mode *mode = get_irn_mode(n);
3236 HANDLE_UNOP_PHI(tarval_not,a,c);
3238 /* check for a boolean Not */
3239 if (mode == mode_b &&
3241 is_Cmp(get_Proj_pred(a))) {
3242 /* We negate a Cmp. The Cmp has the negated result anyways! */
3243 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3244 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3245 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3249 ir_node *eor_b = get_Eor_right(a);
3250 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3251 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3252 ir_node *eor_a = get_Eor_left(a);
3253 dbg_info *dbg = get_irn_dbg_info(n);
3254 ir_graph *irg = current_ir_graph;
3255 ir_node *block = get_nodes_block(n);
3256 ir_mode *mode = get_irn_mode(n);
3257 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3261 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3262 if (is_Minus(a)) { /* ~-x -> x + -1 */
3263 dbg_info *dbg = get_irn_dbg_info(n);
3264 ir_graph *irg = current_ir_graph;
3265 ir_node *block = get_nodes_block(n);
3266 ir_node *add_l = get_Minus_op(a);
3267 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3268 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3269 } else if (is_Add(a)) {
3270 ir_node *add_r = get_Add_right(a);
3271 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3272 /* ~(x + -1) = -x */
3273 ir_node *op = get_Add_left(a);
3274 ir_node *blk = get_irn_n(n, -1);
3275 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3281 } /* transform_node_Not */
3284 * Transform a Minus.
3288 * -(a >>u (size-1)) = a >>s (size-1)
3289 * -(a >>s (size-1)) = a >>u (size-1)
3290 * -(a * const) -> a * -const
3292 static ir_node *transform_node_Minus(ir_node *n) {
3293 ir_node *c, *oldn = n;
3294 ir_node *a = get_Minus_op(n);
3297 HANDLE_UNOP_PHI(tarval_neg,a,c);
3299 mode = get_irn_mode(a);
3300 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3301 /* the following rules are only to twos-complement */
3304 ir_node *op = get_Not_op(a);
3305 tarval *tv = get_mode_one(mode);
3306 ir_node *blk = get_irn_n(n, -1);
3307 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3308 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3309 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3313 ir_node *c = get_Shr_right(a);
3316 tarval *tv = get_Const_tarval(c);
3318 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3319 /* -(a >>u (size-1)) = a >>s (size-1) */
3320 ir_node *v = get_Shr_left(a);
3322 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3323 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3329 ir_node *c = get_Shrs_right(a);
3332 tarval *tv = get_Const_tarval(c);
3334 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3335 /* -(a >>s (size-1)) = a >>u (size-1) */
3336 ir_node *v = get_Shrs_left(a);
3338 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3339 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3346 /* - (a-b) = b - a */
3347 ir_node *la = get_Sub_left(a);
3348 ir_node *ra = get_Sub_right(a);
3349 ir_node *blk = get_irn_n(n, -1);
3351 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3352 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3356 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3357 ir_node *mul_l = get_Mul_left(a);
3358 ir_node *mul_r = get_Mul_right(a);
3359 if (is_Const(mul_r)) {
3360 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3361 if(tv != tarval_bad) {
3362 ir_node *cnst = new_Const(mode, tv);
3363 dbg_info *dbg = get_irn_dbg_info(a);
3364 ir_graph *irg = current_ir_graph;
3365 ir_node *block = get_nodes_block(a);
3366 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3367 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3374 } /* transform_node_Minus */
3377 * Transform a Cast_type(Const) into a new Const_type
3379 static ir_node *transform_node_Cast(ir_node *n) {
3381 ir_node *pred = get_Cast_op(n);
3382 ir_type *tp = get_irn_type(n);
3384 if (is_Const(pred) && get_Const_type(pred) != tp) {
3385 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3386 get_Const_tarval(pred), tp);
3387 DBG_OPT_CSTEVAL(oldn, n);
3388 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3389 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3390 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3391 DBG_OPT_CSTEVAL(oldn, n);
3395 } /* transform_node_Cast */
3398 * Transform a Proj(Div) with a non-zero value.
3399 * Removes the exceptions and routes the memory to the NoMem node.
3401 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3402 ir_node *div = get_Proj_pred(proj);
3403 ir_node *b = get_Div_right(div);
3404 ir_node *confirm, *res, *new_mem;
3407 if (value_not_zero(b, &confirm)) {
3408 /* div(x, y) && y != 0 */
3409 if (confirm == NULL) {
3410 /* we are sure we have a Const != 0 */
3411 new_mem = get_Div_mem(div);
3412 if (is_Pin(new_mem))
3413 new_mem = get_Pin_op(new_mem);
3414 set_Div_mem(div, new_mem);
3415 set_irn_pinned(div, op_pin_state_floats);
3418 proj_nr = get_Proj_proj(proj);
3420 case pn_Div_X_regular:
3421 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3423 case pn_Div_X_except:
3424 /* we found an exception handler, remove it */
3425 DBG_OPT_EXC_REM(proj);
3429 res = get_Div_mem(div);
3430 new_mem = get_irg_no_mem(current_ir_graph);
3433 /* This node can only float up to the Confirm block */
3434 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3436 set_irn_pinned(div, op_pin_state_floats);
3437 /* this is a Div without exception, we can remove the memory edge */
3438 set_Div_mem(div, new_mem);
3443 } /* transform_node_Proj_Div */
3446 * Transform a Proj(Mod) with a non-zero value.
3447 * Removes the exceptions and routes the memory to the NoMem node.
3449 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3450 ir_node *mod = get_Proj_pred(proj);
3451 ir_node *b = get_Mod_right(mod);
3452 ir_node *confirm, *res, *new_mem;
3455 if (value_not_zero(b, &confirm)) {
3456 /* mod(x, y) && y != 0 */
3457 proj_nr = get_Proj_proj(proj);
3459 if (confirm == NULL) {
3460 /* we are sure we have a Const != 0 */
3461 new_mem = get_Mod_mem(mod);
3462 if (is_Pin(new_mem))
3463 new_mem = get_Pin_op(new_mem);
3464 set_Mod_mem(mod, new_mem);
3465 set_irn_pinned(mod, op_pin_state_floats);
3470 case pn_Mod_X_regular:
3471 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3473 case pn_Mod_X_except:
3474 /* we found an exception handler, remove it */
3475 DBG_OPT_EXC_REM(proj);
3479 res = get_Mod_mem(mod);
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 /* this is a Mod without exception, we can remove the memory edge */
3487 set_Mod_mem(mod, new_mem);
3490 if (get_Mod_left(mod) == b) {
3491 /* a % a = 0 if a != 0 */
3492 ir_mode *mode = get_irn_mode(proj);
3493 ir_node *res = new_Const(mode, get_mode_null(mode));
3495 DBG_OPT_CSTEVAL(mod, res);
3501 } /* transform_node_Proj_Mod */
3504 * Transform a Proj(DivMod) with a non-zero value.
3505 * Removes the exceptions and routes the memory to the NoMem node.
3507 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3508 ir_node *divmod = get_Proj_pred(proj);
3509 ir_node *b = get_DivMod_right(divmod);
3510 ir_node *confirm, *res, *new_mem;
3513 if (value_not_zero(b, &confirm)) {
3514 /* DivMod(x, y) && y != 0 */
3515 proj_nr = get_Proj_proj(proj);
3517 if (confirm == NULL) {
3518 /* we are sure we have a Const != 0 */
3519 new_mem = get_DivMod_mem(divmod);
3520 if (is_Pin(new_mem))
3521 new_mem = get_Pin_op(new_mem);
3522 set_DivMod_mem(divmod, new_mem);
3523 set_irn_pinned(divmod, op_pin_state_floats);
3528 case pn_DivMod_X_regular:
3529 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3531 case pn_DivMod_X_except:
3532 /* we found an exception handler, remove it */
3533 DBG_OPT_EXC_REM(proj);
3537 res = get_DivMod_mem(divmod);
3538 new_mem = get_irg_no_mem(current_ir_graph);
3541 /* This node can only float up to the Confirm block */
3542 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3544 /* this is a DivMod without exception, we can remove the memory edge */
3545 set_DivMod_mem(divmod, new_mem);
3548 case pn_DivMod_res_mod:
3549 if (get_DivMod_left(divmod) == b) {
3550 /* a % a = 0 if a != 0 */
3551 ir_mode *mode = get_irn_mode(proj);
3552 ir_node *res = new_Const(mode, get_mode_null(mode));
3554 DBG_OPT_CSTEVAL(divmod, res);
3560 } /* transform_node_Proj_DivMod */
3563 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3565 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3566 if (get_opt_unreachable_code()) {
3567 ir_node *n = get_Proj_pred(proj);
3568 ir_node *b = get_Cond_selector(n);
3570 if (mode_is_int(get_irn_mode(b))) {
3571 tarval *tb = value_of(b);
3573 if (tb != tarval_bad) {
3574 /* we have a constant switch */
3575 long num = get_Proj_proj(proj);
3577 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3578 if (get_tarval_long(tb) == num) {
3579 /* Do NOT create a jump here, or we will have 2 control flow ops
3580 * in a block. This case is optimized away in optimize_cf(). */
3583 /* this case will NEVER be taken, kill it */
3591 } /* transform_node_Proj_Cond */
3594 * Create a 0 constant of given mode.
3596 static ir_node *create_zero_const(ir_mode *mode) {
3597 tarval *tv = get_mode_null(mode);
3598 ir_node *cnst = new_Const(mode, tv);
3603 /* the order of the values is important! */
3604 typedef enum const_class {
3610 static const_class classify_const(const ir_node* n)
3612 if (is_Const(n)) return const_const;
3613 if (is_irn_constlike(n)) return const_like;
3618 * Determines whether r is more constlike or has a larger index (in that order)
3621 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3623 const const_class l_order = classify_const(l);
3624 const const_class r_order = classify_const(r);
3626 l_order > r_order ||
3627 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3631 * Normalizes and optimizes Cmp nodes.
3633 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3634 ir_node *n = get_Proj_pred(proj);
3635 ir_node *left = get_Cmp_left(n);
3636 ir_node *right = get_Cmp_right(n);
3639 ir_mode *mode = NULL;
3640 long proj_nr = get_Proj_proj(proj);
3642 /* we can evaluate some cases directly */
3645 return new_Const(mode_b, get_tarval_b_false());
3647 return new_Const(mode_b, get_tarval_b_true());
3649 if (!mode_is_float(get_irn_mode(left)))
3650 return new_Const(mode_b, get_tarval_b_true());
3656 /* remove Casts of both sides */
3657 left = skip_Cast(left);
3658 right = skip_Cast(right);
3660 /* Remove unnecessary conversions */
3661 /* TODO handle constants */
3662 if (is_Conv(left) && is_Conv(right)) {
3663 ir_mode *mode = get_irn_mode(left);
3664 ir_node *op_left = get_Conv_op(left);
3665 ir_node *op_right = get_Conv_op(right);
3666 ir_mode *mode_left = get_irn_mode(op_left);
3667 ir_mode *mode_right = get_irn_mode(op_right);
3669 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3670 && mode_left != mode_b && mode_right != mode_b) {
3671 ir_graph *irg = current_ir_graph;
3672 ir_node *block = get_nodes_block(n);
3674 if (mode_left == mode_right) {
3678 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3679 } else if (smaller_mode(mode_left, mode_right)) {
3680 left = new_r_Conv(irg, block, op_left, mode_right);
3683 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3684 } else if (smaller_mode(mode_right, mode_left)) {
3686 right = new_r_Conv(irg, block, op_right, mode_left);
3688 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3693 /* remove operation on both sides if possible */
3694 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3696 * The following operations are NOT safe for floating point operations, for instance
3697 * 1.0 + inf == 2.0 + inf, =/=> x == y
3699 if (mode_is_int(get_irn_mode(left))) {
3700 unsigned lop = get_irn_opcode(left);
3702 if (lop == get_irn_opcode(right)) {
3703 ir_node *ll, *lr, *rl, *rr;
3705 /* same operation on both sides, try to remove */
3709 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3710 left = get_unop_op(left);
3711 right = get_unop_op(right);
3713 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3716 ll = get_Add_left(left);
3717 lr = get_Add_right(left);
3718 rl = get_Add_left(right);
3719 rr = get_Add_right(right);
3722 /* X + a CMP X + b ==> a CMP b */
3726 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3727 } else if (ll == rr) {
3728 /* X + a CMP b + X ==> a CMP b */
3732 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3733 } else if (lr == rl) {
3734 /* a + X CMP X + b ==> a CMP b */
3738 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3739 } else if (lr == rr) {
3740 /* a + X CMP b + X ==> a CMP b */
3744 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3748 ll = get_Sub_left(left);
3749 lr = get_Sub_right(left);
3750 rl = get_Sub_left(right);
3751 rr = get_Sub_right(right);
3754 /* X - a CMP X - b ==> a CMP b */
3758 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3759 } else if (lr == rr) {
3760 /* a - X CMP b - X ==> a CMP b */
3764 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3768 if (get_Rot_right(left) == get_Rot_right(right)) {
3769 /* a ROT X CMP b ROT X ==> a CMP b */
3770 left = get_Rot_left(left);
3771 right = get_Rot_left(right);
3773 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3781 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3782 if (is_Add(left) || is_Sub(left)) {
3783 ir_node *ll = get_binop_left(left);
3784 ir_node *lr = get_binop_right(left);
3786 if (lr == right && is_Add(left)) {
3793 right = create_zero_const(get_irn_mode(left));
3795 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3798 if (is_Add(right) || is_Sub(right)) {
3799 ir_node *rl = get_binop_left(right);
3800 ir_node *rr = get_binop_right(right);
3802 if (rr == left && is_Add(right)) {
3809 right = create_zero_const(get_irn_mode(left));
3811 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3814 } /* mode_is_int(...) */
3815 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3817 /* replace mode_b compares with ands/ors */
3818 if (get_irn_mode(left) == mode_b) {
3819 ir_graph *irg = current_ir_graph;
3820 ir_node *block = get_nodes_block(n);
3824 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3825 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3826 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3827 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3828 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3829 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3830 default: bres = NULL;
3833 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3839 * First step: normalize the compare op
3840 * by placing the constant on the right side
3841 * or moving the lower address node to the left.
3843 if (!operands_are_normalized(left, right)) {
3849 proj_nr = get_inversed_pnc(proj_nr);
3854 * Second step: Try to reduce the magnitude
3855 * of a constant. This may help to generate better code
3856 * later and may help to normalize more compares.
3857 * Of course this is only possible for integer values.
3859 if (is_Const(right)) {
3860 mode = get_irn_mode(right);
3861 tv = get_Const_tarval(right);
3863 /* TODO extend to arbitrary constants */
3864 if (is_Conv(left) && tarval_is_null(tv)) {
3865 ir_node *op = get_Conv_op(left);
3866 ir_mode *op_mode = get_irn_mode(op);
3869 * UpConv(x) REL 0 ==> x REL 0
3871 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3872 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3873 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3874 tv = get_mode_null(op_mode);
3878 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3882 if (tv != tarval_bad) {
3883 /* the following optimization is possible on modes without Overflow
3884 * on Unary Minus or on == and !=:
3885 * -a CMP c ==> a swap(CMP) -c
3887 * Beware: for two-complement Overflow may occur, so only == and != can
3888 * be optimized, see this:
3889 * -MININT < 0 =/=> MININT > 0 !!!
3891 if (is_Minus(left) &&
3892 (!mode_overflow_on_unary_Minus(mode) ||
3893 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3894 tv = tarval_neg(tv);
3896 if (tv != tarval_bad) {
3897 left = get_Minus_op(left);
3898 proj_nr = get_inversed_pnc(proj_nr);
3900 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3902 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3903 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3904 tv = tarval_not(tv);
3906 if (tv != tarval_bad) {
3907 left = get_Not_op(left);
3909 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3913 /* for integer modes, we have more */
3914 if (mode_is_int(mode)) {
3915 /* Ne includes Unordered which is not possible on integers.
3916 * However, frontends often use this wrong, so fix it here */
3917 if (proj_nr & pn_Cmp_Uo) {
3918 proj_nr &= ~pn_Cmp_Uo;
3919 set_Proj_proj(proj, proj_nr);
3922 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3923 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3924 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3925 tv = tarval_sub(tv, get_mode_one(mode));
3927 if (tv != tarval_bad) {
3928 proj_nr ^= pn_Cmp_Eq;
3930 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3933 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3934 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3935 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3936 tv = tarval_add(tv, get_mode_one(mode));
3938 if (tv != tarval_bad) {
3939 proj_nr ^= pn_Cmp_Eq;
3941 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3945 /* the following reassociations work only for == and != */
3946 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3948 #if 0 /* Might be not that good in general */
3949 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3950 if (tarval_is_null(tv) && is_Sub(left)) {
3951 right = get_Sub_right(left);
3952 left = get_Sub_left(left);
3954 tv = value_of(right);
3956 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3960 if (tv != tarval_bad) {
3961 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3963 ir_node *c1 = get_Sub_right(left);
3964 tarval *tv2 = value_of(c1);
3966 if (tv2 != tarval_bad) {
3967 tv2 = tarval_add(tv, value_of(c1));
3969 if (tv2 != tarval_bad) {
3970 left = get_Sub_left(left);
3973 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3977 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3978 else if (is_Add(left)) {
3979 ir_node *a_l = get_Add_left(left);
3980 ir_node *a_r = get_Add_right(left);
3984 if (is_Const(a_l)) {
3986 tv2 = value_of(a_l);
3989 tv2 = value_of(a_r);
3992 if (tv2 != tarval_bad) {
3993 tv2 = tarval_sub(tv, tv2);
3995 if (tv2 != tarval_bad) {
3999 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4003 /* -a == c ==> a == -c, -a != c ==> a != -c */
4004 else if (is_Minus(left)) {
4005 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4007 if (tv2 != tarval_bad) {
4008 left = get_Minus_op(left);
4011 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4016 /* the following reassociations work only for <= */
4017 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4018 if (tv != tarval_bad) {
4019 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4020 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4026 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4027 switch (get_irn_opcode(left)) {
4031 c1 = get_And_right(left);
4034 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4035 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4037 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4039 /* TODO: move to constant evaluation */
4040 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4041 c1 = new_Const(mode_b, tv);
4042 DBG_OPT_CSTEVAL(proj, c1);
4046 if (tarval_is_single_bit(tv)) {
4048 * optimization for AND:
4050 * And(x, C) == C ==> And(x, C) != 0
4051 * And(x, C) != C ==> And(X, C) == 0
4053 * if C is a single Bit constant.
4056 /* check for Constant's match. We have check hare the tarvals,
4057 because our const might be changed */
4058 if (get_Const_tarval(c1) == tv) {
4059 /* fine: do the transformation */
4060 tv = get_mode_null(get_tarval_mode(tv));
4061 proj_nr ^= pn_Cmp_Leg;
4063 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4069 c1 = get_Or_right(left);
4070 if (is_Const(c1) && tarval_is_null(tv)) {
4072 * Or(x, C) == 0 && C != 0 ==> FALSE
4073 * Or(x, C) != 0 && C != 0 ==> TRUE
4075 if (! tarval_is_null(get_Const_tarval(c1))) {
4076 /* TODO: move to constant evaluation */
4077 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4078 c1 = new_Const(mode_b, tv);
4079 DBG_OPT_CSTEVAL(proj, c1);
4086 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4088 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4091 c1 = get_Shl_right(left);
4093 tarval *tv1 = get_Const_tarval(c1);
4094 ir_mode *mode = get_irn_mode(left);
4095 tarval *minus1 = get_mode_all_one(mode);
4096 tarval *amask = tarval_shr(minus1, tv1);
4097 tarval *cmask = tarval_shl(minus1, tv1);
4100 if (tarval_and(tv, cmask) != tv) {
4101 /* condition not met */
4102 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4103 c1 = new_Const(mode_b, tv);
4104 DBG_OPT_CSTEVAL(proj, c1);
4107 sl = get_Shl_left(left);
4108 blk = get_nodes_block(n);
4109 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4110 tv = tarval_shr(tv, tv1);
4112 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4117 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4119 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4122 c1 = get_Shr_right(left);
4124 tarval *tv1 = get_Const_tarval(c1);
4125 ir_mode *mode = get_irn_mode(left);
4126 tarval *minus1 = get_mode_all_one(mode);
4127 tarval *amask = tarval_shl(minus1, tv1);
4128 tarval *cmask = tarval_shr(minus1, tv1);
4131 if (tarval_and(tv, cmask) != tv) {
4132 /* condition not met */
4133 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4134 c1 = new_Const(mode_b, tv);
4135 DBG_OPT_CSTEVAL(proj, c1);
4138 sl = get_Shr_left(left);
4139 blk = get_nodes_block(n);
4140 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4141 tv = tarval_shl(tv, tv1);
4143 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4148 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4150 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4153 c1 = get_Shrs_right(left);
4155 tarval *tv1 = get_Const_tarval(c1);
4156 ir_mode *mode = get_irn_mode(left);
4157 tarval *minus1 = get_mode_all_one(mode);
4158 tarval *amask = tarval_shl(minus1, tv1);
4159 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4162 cond = tarval_sub(cond, tv1);
4163 cond = tarval_shrs(tv, cond);
4165 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4166 /* condition not met */
4167 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4168 c1 = new_Const(mode_b, tv);
4169 DBG_OPT_CSTEVAL(proj, c1);
4172 sl = get_Shrs_left(left);
4173 blk = get_nodes_block(n);
4174 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4175 tv = tarval_shl(tv, tv1);
4177 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4182 } /* tarval != bad */
4185 if (changed & 2) /* need a new Const */
4186 right = new_Const(mode, tv);
4188 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4189 ir_node *op = get_Proj_pred(left);
4191 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4192 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4193 ir_node *c = get_binop_right(op);
4196 tarval *tv = get_Const_tarval(c);
4198 if (tarval_is_single_bit(tv)) {
4199 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4200 ir_node *v = get_binop_left(op);
4201 ir_node *blk = get_irn_n(op, -1);
4202 ir_mode *mode = get_irn_mode(v);
4204 tv = tarval_sub(tv, get_mode_one(mode));
4205 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4207 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4214 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4216 /* create a new compare */
4217 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4218 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4222 } /* transform_node_Proj_Cmp */
4225 * Does all optimizations on nodes that must be done on it's Proj's
4226 * because of creating new nodes.
4228 static ir_node *transform_node_Proj(ir_node *proj) {
4229 ir_node *n = get_Proj_pred(proj);
4231 switch (get_irn_opcode(n)) {
4233 return transform_node_Proj_Div(proj);
4236 return transform_node_Proj_Mod(proj);
4239 return transform_node_Proj_DivMod(proj);
4242 return transform_node_Proj_Cond(proj);
4245 return transform_node_Proj_Cmp(proj);
4248 /* should not happen, but if it does will be optimized away */
4249 return equivalent_node_Proj(proj);
4255 } /* transform_node_Proj */
4258 * Move Confirms down through Phi nodes.
4260 static ir_node *transform_node_Phi(ir_node *phi) {
4262 ir_mode *mode = get_irn_mode(phi);
4264 if (mode_is_reference(mode)) {
4265 n = get_irn_arity(phi);
4267 /* Beware of Phi0 */
4269 ir_node *pred = get_irn_n(phi, 0);
4270 ir_node *bound, *new_Phi, *block, **in;
4273 if (! is_Confirm(pred))
4276 bound = get_Confirm_bound(pred);
4277 pnc = get_Confirm_cmp(pred);
4279 NEW_ARR_A(ir_node *, in, n);
4280 in[0] = get_Confirm_value(pred);
4282 for (i = 1; i < n; ++i) {
4283 pred = get_irn_n(phi, i);
4285 if (! is_Confirm(pred) ||
4286 get_Confirm_bound(pred) != bound ||
4287 get_Confirm_cmp(pred) != pnc)
4289 in[i] = get_Confirm_value(pred);
4291 /* move the Confirm nodes "behind" the Phi */
4292 block = get_irn_n(phi, -1);
4293 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4294 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4298 } /* transform_node_Phi */
4301 * Returns the operands of a commutative bin-op, if one operand is
4302 * a const, it is returned as the second one.
4304 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4305 ir_node *op_a = get_binop_left(binop);
4306 ir_node *op_b = get_binop_right(binop);
4308 assert(is_op_commutative(get_irn_op(binop)));
4310 if (is_Const(op_a)) {
4317 } /* get_comm_Binop_Ops */
4320 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4321 * Such pattern may arise in bitfield stores.
4323 * value c4 value c4 & c2
4324 * AND c3 AND c1 | c3
4331 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4334 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4337 ir_node *and_l, *c3;
4338 ir_node *value, *c4;
4339 ir_node *new_and, *new_const, *block;
4340 ir_mode *mode = get_irn_mode(or);
4342 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4345 get_comm_Binop_Ops(or, &and, &c1);
4346 if (!is_Const(c1) || !is_And(and))
4349 get_comm_Binop_Ops(and, &or_l, &c2);
4353 tv1 = get_Const_tarval(c1);
4354 tv2 = get_Const_tarval(c2);
4356 tv = tarval_or(tv1, tv2);
4357 if (tarval_is_all_one(tv)) {
4358 /* the AND does NOT clear a bit with isn't set by the OR */
4359 set_Or_left(or, or_l);
4360 set_Or_right(or, c1);
4362 /* check for more */
4369 get_comm_Binop_Ops(or_l, &and_l, &c3);
4370 if (!is_Const(c3) || !is_And(and_l))
4373 get_comm_Binop_Ops(and_l, &value, &c4);
4377 /* ok, found the pattern, check for conditions */
4378 assert(mode == get_irn_mode(and));
4379 assert(mode == get_irn_mode(or_l));
4380 assert(mode == get_irn_mode(and_l));
4382 tv3 = get_Const_tarval(c3);
4383 tv4 = get_Const_tarval(c4);
4385 tv = tarval_or(tv4, tv2);
4386 if (!tarval_is_all_one(tv)) {
4387 /* have at least one 0 at the same bit position */
4391 n_tv4 = tarval_not(tv4);
4392 if (tv3 != tarval_and(tv3, n_tv4)) {
4393 /* bit in the or_mask is outside the and_mask */
4397 n_tv2 = tarval_not(tv2);
4398 if (tv1 != tarval_and(tv1, n_tv2)) {
4399 /* bit in the or_mask is outside the and_mask */
4403 /* ok, all conditions met */
4404 block = get_irn_n(or, -1);
4406 new_and = new_r_And(current_ir_graph, block,
4407 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4409 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4411 set_Or_left(or, new_and);
4412 set_Or_right(or, new_const);
4414 /* check for more */
4416 } /* transform_node_Or_bf_store */
4419 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4421 static ir_node *transform_node_Or_Rot(ir_node *or) {
4422 ir_mode *mode = get_irn_mode(or);
4423 ir_node *shl, *shr, *block;
4424 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4427 if (! mode_is_int(mode))
4430 shl = get_binop_left(or);
4431 shr = get_binop_right(or);
4440 } else if (!is_Shl(shl)) {
4442 } else if (!is_Shr(shr)) {
4445 x = get_Shl_left(shl);
4446 if (x != get_Shr_left(shr))
4449 c1 = get_Shl_right(shl);
4450 c2 = get_Shr_right(shr);
4451 if (is_Const(c1) && is_Const(c2)) {
4452 tv1 = get_Const_tarval(c1);
4453 if (! tarval_is_long(tv1))
4456 tv2 = get_Const_tarval(c2);
4457 if (! tarval_is_long(tv2))
4460 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4461 != (int) get_mode_size_bits(mode))
4464 /* yet, condition met */
4465 block = get_irn_n(or, -1);
4467 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4469 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4471 } else if (is_Sub(c1)) {
4475 if (get_Sub_right(sub) != v)
4478 c1 = get_Sub_left(sub);
4482 tv1 = get_Const_tarval(c1);
4483 if (! tarval_is_long(tv1))
4486 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4489 /* yet, condition met */
4490 block = get_nodes_block(or);
4492 /* a Rot right is not supported, so use a rot left */
4493 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4495 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4497 } else if (is_Sub(c2)) {
4501 c1 = get_Sub_left(sub);
4505 tv1 = get_Const_tarval(c1);
4506 if (! tarval_is_long(tv1))
4509 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4512 /* yet, condition met */
4513 block = get_irn_n(or, -1);
4516 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4518 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4523 } /* transform_node_Or_Rot */
4528 static ir_node *transform_node_Or(ir_node *n) {
4529 ir_node *c, *oldn = n;
4530 ir_node *a = get_Or_left(n);
4531 ir_node *b = get_Or_right(n);
4534 if (is_Not(a) && is_Not(b)) {
4535 /* ~a | ~b = ~(a&b) */
4536 ir_node *block = get_nodes_block(n);
4538 mode = get_irn_mode(n);
4541 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4542 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4543 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4547 /* we can evaluate 2 Projs of the same Cmp */
4548 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4549 ir_node *pred_a = get_Proj_pred(a);
4550 ir_node *pred_b = get_Proj_pred(b);
4551 if (pred_a == pred_b) {
4552 dbg_info *dbgi = get_irn_dbg_info(n);
4553 ir_node *block = get_nodes_block(pred_a);
4554 pn_Cmp pn_a = get_Proj_proj(a);
4555 pn_Cmp pn_b = get_Proj_proj(b);
4556 /* yes, we can simply calculate with pncs */
4557 pn_Cmp new_pnc = pn_a | pn_b;
4559 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4564 mode = get_irn_mode(n);
4565 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4567 n = transform_node_Or_bf_store(n);
4568 n = transform_node_Or_Rot(n);
4572 n = transform_bitwise_distributive(n, transform_node_Or);
4575 } /* transform_node_Or */
4579 static ir_node *transform_node(ir_node *n);
4582 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4584 * Should be moved to reassociation?
4586 static ir_node *transform_node_shift(ir_node *n) {
4587 ir_node *left, *right;
4588 tarval *tv1, *tv2, *res;
4590 int modulo_shf, flag;
4592 left = get_binop_left(n);
4594 /* different operations */
4595 if (get_irn_op(left) != get_irn_op(n))
4598 right = get_binop_right(n);
4599 tv1 = value_of(right);
4600 if (tv1 == tarval_bad)
4603 tv2 = value_of(get_binop_right(left));
4604 if (tv2 == tarval_bad)
4607 res = tarval_add(tv1, tv2);
4609 /* beware: a simple replacement works only, if res < modulo shift */
4610 mode = get_irn_mode(n);
4614 modulo_shf = get_mode_modulo_shift(mode);
4615 if (modulo_shf > 0) {
4616 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4618 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4624 /* ok, we can replace it */
4625 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4627 in[0] = get_binop_left(left);
4628 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4630 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4632 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4634 return transform_node(irn);
4637 } /* transform_node_shift */
4642 static ir_node *transform_node_Shr(ir_node *n) {
4643 ir_node *c, *oldn = n;
4644 ir_node *a = get_Shr_left(n);
4645 ir_node *b = get_Shr_right(n);
4646 ir_mode *mode = get_irn_mode(n);
4648 HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4649 return transform_node_shift(n);
4650 } /* transform_node_Shr */
4655 static ir_node *transform_node_Shrs(ir_node *n) {
4656 ir_node *c, *oldn = n;
4657 ir_node *a = get_Shrs_left(n);
4658 ir_node *b = get_Shrs_right(n);
4659 ir_mode *mode = get_irn_mode(n);
4661 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4662 return transform_node_shift(n);
4663 } /* transform_node_Shrs */
4668 static ir_node *transform_node_Shl(ir_node *n) {
4669 ir_node *c, *oldn = n;
4670 ir_node *a = get_Shl_left(n);
4671 ir_node *b = get_Shl_right(n);
4672 ir_mode *mode = get_irn_mode(n);
4674 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4675 return transform_node_shift(n);
4676 } /* transform_node_Shl */
4681 static ir_node *transform_node_Rot(ir_node *n) {
4682 ir_node *c, *oldn = n;
4683 ir_node *a = get_Rot_left(n);
4684 ir_node *b = get_Rot_right(n);
4685 ir_mode *mode = get_irn_mode(n);
4687 HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4688 return transform_node_shift(n);
4689 } /* transform_node_Rot */
4694 static ir_node *transform_node_Conv(ir_node *n) {
4695 ir_node *c, *oldn = n;
4696 ir_node *a = get_Conv_op(n);
4698 if (is_const_Phi(a)) {
4699 c = apply_conv_on_phi(a, get_irn_mode(n));
4701 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4706 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4707 ir_mode *mode = get_irn_mode(n);
4708 return new_r_Unknown(current_ir_graph, mode);
4712 } /* transform_node_Conv */
4715 * Remove dead blocks and nodes in dead blocks
4716 * in keep alive list. We do not generate a new End node.
4718 static ir_node *transform_node_End(ir_node *n) {
4719 int i, j, n_keepalives = get_End_n_keepalives(n);
4722 NEW_ARR_A(ir_node *, in, n_keepalives);
4724 for (i = j = 0; i < n_keepalives; ++i) {
4725 ir_node *ka = get_End_keepalive(n, i);
4727 if (! is_Block_dead(ka)) {
4731 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4734 /* FIXME: beabi need to keep a Proj(M) */
4735 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4738 if (j != n_keepalives)
4739 set_End_keepalives(n, j, in);
4741 } /* transform_node_End */
4743 /** returns 1 if a == -b */
4744 static int is_negated_value(ir_node *a, ir_node *b) {
4745 if(is_Minus(a) && get_Minus_op(a) == b)
4747 if(is_Minus(b) && get_Minus_op(b) == a)
4749 if(is_Sub(a) && is_Sub(b)) {
4750 ir_node *a_left = get_Sub_left(a);
4751 ir_node *a_right = get_Sub_right(a);
4752 ir_node *b_left = get_Sub_left(b);
4753 ir_node *b_right = get_Sub_right(b);
4755 if(a_left == b_right && a_right == b_left)
4763 * Optimize a Mux into some simpler cases.
4765 static ir_node *transform_node_Mux(ir_node *n) {
4766 ir_node *oldn = n, *sel = get_Mux_sel(n);
4767 ir_mode *mode = get_irn_mode(n);
4769 if (mode == mode_b) {
4770 ir_node *t = get_Mux_true(n);
4771 ir_node *f = get_Mux_false(n);
4772 dbg_info *dbg = get_irn_dbg_info(n);
4773 ir_node *block = get_irn_n(n, -1);
4774 ir_graph *irg = current_ir_graph;
4777 tarval *tv_t = get_Const_tarval(t);
4778 if (tv_t == tarval_b_true) {
4780 assert(get_Const_tarval(f) == tarval_b_false);
4783 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4786 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4787 assert(tv_t == tarval_b_false);
4789 assert(get_Const_tarval(f) == tarval_b_true);
4792 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4795 } else if (is_Const(f)) {
4796 tarval *tv_f = get_Const_tarval(f);
4797 if (tv_f == tarval_b_true) {
4798 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4799 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4801 assert(tv_f == tarval_b_false);
4802 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4807 if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4808 ir_node *cmp = get_Proj_pred(sel);
4809 long pn = get_Proj_proj(sel);
4810 ir_node *f = get_Mux_false(n);
4811 ir_node *t = get_Mux_true(n);
4814 * Note: normalization puts the constant on the right side,
4815 * so we check only one case.
4817 * Note further that these optimization work even for floating point
4818 * with NaN's because -NaN == NaN.
4819 * However, if +0 and -0 is handled differently, we cannot use the first
4823 ir_node *cmp_r = get_Cmp_right(cmp);
4824 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4825 ir_node *block = get_irn_n(n, -1);
4827 if(is_negated_value(f, t)) {
4828 ir_node *cmp_left = get_Cmp_left(cmp);
4830 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4831 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4832 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4834 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4836 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4838 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4839 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4840 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4842 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4844 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4846 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4853 return arch_transform_node_Mux(n);
4854 } /* transform_node_Mux */
4857 * Optimize a Psi into some simpler cases.
4859 static ir_node *transform_node_Psi(ir_node *n) {
4861 return transform_node_Mux(n);
4864 } /* transform_node_Psi */
4867 * optimize sync nodes that have other syncs as input we simply add the inputs
4868 * of the other sync to our own inputs
4870 static ir_node *transform_node_Sync(ir_node *n) {
4871 int arity = get_Sync_n_preds(n);
4874 for (i = 0; i < arity;) {
4875 ir_node *pred = get_Sync_pred(n, i);
4879 if (!is_Sync(pred)) {
4887 pred_arity = get_Sync_n_preds(pred);
4888 for (j = 0; j < pred_arity; ++j) {
4889 ir_node *pred_pred = get_Sync_pred(pred, j);
4894 add_irn_n(n, pred_pred);
4898 if (get_Sync_pred(n, k) == pred_pred) break;
4903 /* rehash the sync node */
4904 add_identities(current_ir_graph->value_table, n);
4910 * Tries several [inplace] [optimizing] transformations and returns an
4911 * equivalent node. The difference to equivalent_node() is that these
4912 * transformations _do_ generate new nodes, and thus the old node must
4913 * not be freed even if the equivalent node isn't the old one.
4915 static ir_node *transform_node(ir_node *n) {
4919 * Transform_node is the only "optimizing transformation" that might
4920 * return a node with a different opcode. We iterate HERE until fixpoint
4921 * to get the final result.
4925 if (n->op->ops.transform_node)
4926 n = n->op->ops.transform_node(n);
4927 } while (oldn != n);
4930 } /* transform_node */
4933 * Sets the default transform node operation for an ir_op_ops.
4935 * @param code the opcode for the default operation
4936 * @param ops the operations initialized
4941 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4945 ops->transform_node = transform_node_##a; \
4982 } /* firm_set_default_transform_node */
4985 /* **************** Common Subexpression Elimination **************** */
4987 /** The size of the hash table used, should estimate the number of nodes
4989 #define N_IR_NODES 512
4991 /** Compares the attributes of two Const nodes. */
4992 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4993 return (get_Const_tarval(a) != get_Const_tarval(b))
4994 || (get_Const_type(a) != get_Const_type(b));
4995 } /* node_cmp_attr_Const */
4997 /** Compares the attributes of two Proj nodes. */
4998 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4999 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5000 } /* node_cmp_attr_Proj */
5002 /** Compares the attributes of two Filter nodes. */
5003 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5004 return get_Filter_proj(a) != get_Filter_proj(b);
5005 } /* node_cmp_attr_Filter */
5007 /** Compares the attributes of two Alloc nodes. */
5008 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5009 const alloc_attr *pa = get_irn_alloc_attr(a);
5010 const alloc_attr *pb = get_irn_alloc_attr(b);
5011 return (pa->where != pb->where) || (pa->type != pb->type);
5012 } /* node_cmp_attr_Alloc */
5014 /** Compares the attributes of two Free nodes. */
5015 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5016 const free_attr *pa = get_irn_free_attr(a);
5017 const free_attr *pb = get_irn_free_attr(b);
5018 return (pa->where != pb->where) || (pa->type != pb->type);
5019 } /* node_cmp_attr_Free */
5021 /** Compares the attributes of two SymConst nodes. */
5022 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5023 const symconst_attr *pa = get_irn_symconst_attr(a);
5024 const symconst_attr *pb = get_irn_symconst_attr(b);
5025 return (pa->kind != pb->kind)
5026 || (pa->sym.type_p != pb->sym.type_p)
5027 || (pa->tp != pb->tp);
5028 } /* node_cmp_attr_SymConst */
5030 /** Compares the attributes of two Call nodes. */
5031 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5032 return get_irn_call_attr(a) != get_irn_call_attr(b);
5033 } /* node_cmp_attr_Call */
5035 /** Compares the attributes of two Sel nodes. */
5036 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5037 const ir_entity *a_ent = get_Sel_entity(a);
5038 const ir_entity *b_ent = get_Sel_entity(b);
5040 (a_ent->kind != b_ent->kind) ||
5041 (a_ent->name != b_ent->name) ||
5042 (a_ent->owner != b_ent->owner) ||
5043 (a_ent->ld_name != b_ent->ld_name) ||
5044 (a_ent->type != b_ent->type);
5045 } /* node_cmp_attr_Sel */
5047 /** Compares the attributes of two Phi nodes. */
5048 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5049 /* we can only enter this function if both nodes have the same number of inputs,
5050 hence it is enough to check if one of them is a Phi0 */
5052 /* check the Phi0 pos attribute */
5053 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5056 } /* node_cmp_attr_Phi */
5058 /** Compares the attributes of two Conv nodes. */
5059 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5060 return get_Conv_strict(a) != get_Conv_strict(b);
5061 } /* node_cmp_attr_Conv */
5063 /** Compares the attributes of two Cast nodes. */
5064 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5065 return get_Cast_type(a) != get_Cast_type(b);
5066 } /* node_cmp_attr_Cast */
5068 /** Compares the attributes of two Load nodes. */
5069 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5070 if (get_Load_volatility(a) == volatility_is_volatile ||
5071 get_Load_volatility(b) == volatility_is_volatile)
5072 /* NEVER do CSE on volatile Loads */
5074 /* do not CSE Loads with different alignment. Be conservative. */
5075 if (get_Load_align(a) != get_Load_align(b))
5078 return get_Load_mode(a) != get_Load_mode(b);
5079 } /* node_cmp_attr_Load */
5081 /** Compares the attributes of two Store nodes. */
5082 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5083 /* do not CSE Stores with different alignment. Be conservative. */
5084 if (get_Store_align(a) != get_Store_align(b))
5087 /* NEVER do CSE on volatile Stores */
5088 return (get_Store_volatility(a) == volatility_is_volatile ||
5089 get_Store_volatility(b) == volatility_is_volatile);
5090 } /* node_cmp_attr_Store */
5092 /** Compares two exception attributes */
5093 static int node_cmp_exception(ir_node *a, ir_node *b) {
5094 const except_attr *ea = get_irn_except_attr(a);
5095 const except_attr *eb = get_irn_except_attr(b);
5097 return ea->pin_state != eb->pin_state;
5100 #define node_cmp_attr_Bound node_cmp_exception
5102 /** Compares the attributes of two Div nodes. */
5103 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5104 const divmod_attr *ma = get_irn_divmod_attr(a);
5105 const divmod_attr *mb = get_irn_divmod_attr(b);
5106 return ma->exc.pin_state != mb->exc.pin_state ||
5107 ma->res_mode != mb->res_mode ||
5108 ma->no_remainder != mb->no_remainder;
5109 } /* node_cmp_attr_Div */
5111 /** Compares the attributes of two DivMod nodes. */
5112 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5113 const divmod_attr *ma = get_irn_divmod_attr(a);
5114 const divmod_attr *mb = get_irn_divmod_attr(b);
5115 return ma->exc.pin_state != mb->exc.pin_state ||
5116 ma->res_mode != mb->res_mode;
5117 } /* node_cmp_attr_DivMod */
5119 /** Compares the attributes of two Mod nodes. */
5120 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5121 const divmod_attr *ma = get_irn_divmod_attr(a);
5122 const divmod_attr *mb = get_irn_divmod_attr(b);
5123 return ma->exc.pin_state != mb->exc.pin_state ||
5124 ma->res_mode != mb->res_mode;
5125 } /* node_cmp_attr_Mod */
5127 /** Compares the attributes of two Quot nodes. */
5128 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5129 const divmod_attr *ma = get_irn_divmod_attr(a);
5130 const divmod_attr *mb = get_irn_divmod_attr(b);
5131 return ma->exc.pin_state != mb->exc.pin_state ||
5132 ma->res_mode != mb->res_mode;
5133 } /* node_cmp_attr_Quot */
5135 /** Compares the attributes of two Confirm nodes. */
5136 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5137 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5138 } /* node_cmp_attr_Confirm */
5140 /** Compares the attributes of two ASM nodes. */
5141 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5143 const ir_asm_constraint *ca;
5144 const ir_asm_constraint *cb;
5147 if (get_ASM_text(a) != get_ASM_text(b))
5150 /* Should we really check the constraints here? Should be better, but is strange. */
5151 n = get_ASM_n_input_constraints(a);
5152 if (n != get_ASM_n_input_constraints(b))
5155 ca = get_ASM_input_constraints(a);
5156 cb = get_ASM_input_constraints(b);
5157 for (i = 0; i < n; ++i) {
5158 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5162 n = get_ASM_n_output_constraints(a);
5163 if (n != get_ASM_n_output_constraints(b))
5166 ca = get_ASM_output_constraints(a);
5167 cb = get_ASM_output_constraints(b);
5168 for (i = 0; i < n; ++i) {
5169 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5173 n = get_ASM_n_clobbers(a);
5174 if (n != get_ASM_n_clobbers(b))
5177 cla = get_ASM_clobbers(a);
5178 clb = get_ASM_clobbers(b);
5179 for (i = 0; i < n; ++i) {
5180 if (cla[i] != clb[i])
5184 } /* node_cmp_attr_ASM */
5187 * Set the default node attribute compare operation for an ir_op_ops.
5189 * @param code the opcode for the default operation
5190 * @param ops the operations initialized
5195 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5199 ops->node_cmp_attr = node_cmp_attr_##a; \
5230 } /* firm_set_default_node_cmp_attr */
5233 * Compare function for two nodes in the value table. Gets two
5234 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5236 int identities_cmp(const void *elt, const void *key) {
5237 ir_node *a = (ir_node *)elt;
5238 ir_node *b = (ir_node *)key;
5241 if (a == b) return 0;
5243 if ((get_irn_op(a) != get_irn_op(b)) ||
5244 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5246 /* compare if a's in and b's in are of equal length */
5247 irn_arity_a = get_irn_intra_arity(a);
5248 if (irn_arity_a != get_irn_intra_arity(b))
5251 if (get_irn_pinned(a) == op_pin_state_pinned) {
5252 /* for pinned nodes, the block inputs must be equal */
5253 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5255 } else if (! get_opt_global_cse()) {
5256 /* for block-local CSE both nodes must be in the same MacroBlock */
5257 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5261 /* compare a->in[0..ins] with b->in[0..ins] */
5262 for (i = 0; i < irn_arity_a; i++)
5263 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5267 * here, we already now that the nodes are identical except their
5270 if (a->op->ops.node_cmp_attr)
5271 return a->op->ops.node_cmp_attr(a, b);
5274 } /* identities_cmp */
5277 * Calculate a hash value of a node.
5279 unsigned ir_node_hash(ir_node *node) {
5283 if (node->op == op_Const) {
5284 /* special value for const, as they only differ in their tarval. */
5285 h = HASH_PTR(node->attr.con.tv);
5286 h = 9*h + HASH_PTR(get_irn_mode(node));
5287 } else if (node->op == op_SymConst) {
5288 /* special value for const, as they only differ in their symbol. */
5289 h = HASH_PTR(node->attr.symc.sym.type_p);
5290 h = 9*h + HASH_PTR(get_irn_mode(node));
5293 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5294 h = irn_arity = get_irn_intra_arity(node);
5296 /* consider all in nodes... except the block if not a control flow. */
5297 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5298 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5302 h = 9*h + HASH_PTR(get_irn_mode(node));
5304 h = 9*h + HASH_PTR(get_irn_op(node));
5308 } /* ir_node_hash */
5310 pset *new_identities(void) {
5311 return new_pset(identities_cmp, N_IR_NODES);
5312 } /* new_identities */
5314 void del_identities(pset *value_table) {
5315 del_pset(value_table);
5316 } /* del_identities */
5319 * Normalize a node by putting constants (and operands with larger
5320 * node index) on the right (operator side).
5322 * @param n The node to normalize
5324 static void normalize_node(ir_node *n) {
5325 if (is_op_commutative(get_irn_op(n))) {
5326 ir_node *l = get_binop_left(n);
5327 ir_node *r = get_binop_right(n);
5329 /* For commutative operators perform a OP b == b OP a but keep
5330 * constants on the RIGHT side. This helps greatly in some
5331 * optimizations. Moreover we use the idx number to make the form
5333 if (!operands_are_normalized(l, r)) {
5334 set_binop_left(n, r);
5335 set_binop_right(n, l);
5338 } /* normalize_node */
5341 * Update the nodes after a match in the value table. If both nodes have
5342 * the same MacroBlock but different Blocks, we must ensure that the node
5343 * with the dominating Block (the node that is near to the MacroBlock header
5344 * is stored in the table.
5345 * Because a MacroBlock has only one "non-exception" flow, we don't need
5346 * dominance info here: We known, that one block must dominate the other and
5347 * following the only block input will allow to find it.
5349 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5350 ir_node *known_blk, *new_block, *block, *mbh;
5352 if (get_opt_global_cse()) {
5353 /* Block inputs are meaning less */
5356 known_blk = get_irn_n(known_irn, -1);
5357 new_block = get_irn_n(new_ir_node, -1);
5358 if (known_blk == new_block) {
5359 /* already in the same block */
5363 * We expect the typical case when we built the graph. In that case, the
5364 * known_irn is already the upper one, so checking this should be faster.
5367 mbh = get_Block_MacroBlock(new_block);
5369 if (block == known_blk) {
5370 /* ok, we have found it: known_block dominates new_block as expected */
5375 * We have reached the MacroBlock header NOT founding
5376 * the known_block. new_block must dominate known_block.
5379 set_irn_n(known_irn, -1, new_block);
5382 assert(get_Block_n_cfgpreds(block) == 1);
5383 block = get_Block_cfgpred_block(block, 0);
5385 } /* update_value_table */
5388 * Return the canonical node computing the same value as n.
5390 * @param value_table The value table
5391 * @param n The node to lookup
5393 * Looks up the node in a hash table.
5395 * For Const nodes this is performed in the constructor, too. Const
5396 * nodes are extremely time critical because of their frequent use in
5397 * constant string arrays.
5399 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5402 if (!value_table) return n;
5406 o = pset_find(value_table, n, ir_node_hash(n));
5410 update_known_irn(o, n);
5417 * During construction we set the op_pin_state_pinned flag in the graph right when the
5418 * optimization is performed. The flag turning on procedure global cse could
5419 * be changed between two allocations. This way we are safe.
5421 * @param value_table The value table
5422 * @param n The node to lookup
5424 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5427 n = identify(value_table, n);
5428 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5429 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5431 } /* identify_cons */
5434 * Return the canonical node computing the same value as n.
5435 * Looks up the node in a hash table, enters it in the table
5436 * if it isn't there yet.
5438 * @param value_table the HashSet containing all nodes in the
5440 * @param n the node to look up
5442 * @return a node that computes the same value as n or n if no such
5443 * node could be found
5445 ir_node *identify_remember(pset *value_table, ir_node *n) {
5448 if (!value_table) return n;
5451 /* lookup or insert in hash table with given hash key. */
5452 o = pset_insert(value_table, n, ir_node_hash(n));
5455 update_known_irn(o, n);
5460 } /* identify_remember */
5462 /* Add a node to the identities value table. */
5463 void add_identities(pset *value_table, ir_node *node) {
5464 if (get_opt_cse() && is_no_Block(node))
5465 identify_remember(value_table, node);
5466 } /* add_identities */
5468 /* Visit each node in the value table of a graph. */
5469 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5471 ir_graph *rem = current_ir_graph;
5473 current_ir_graph = irg;
5474 foreach_pset(irg->value_table, node)
5476 current_ir_graph = rem;
5477 } /* visit_all_identities */
5480 * Garbage in, garbage out. If a node has a dead input, i.e., the
5481 * Bad node is input to the node, return the Bad node.
5483 static ir_node *gigo(ir_node *node) {
5485 ir_op *op = get_irn_op(node);
5487 /* remove garbage blocks by looking at control flow that leaves the block
5488 and replacing the control flow by Bad. */
5489 if (get_irn_mode(node) == mode_X) {
5490 ir_node *block = get_nodes_block(skip_Proj(node));
5492 /* Don't optimize nodes in immature blocks. */
5493 if (!get_Block_matured(block))
5495 /* Don't optimize End, may have Bads. */
5496 if (op == op_End) return node;
5498 if (is_Block(block)) {
5499 if (is_Block_dead(block)) {
5500 /* control flow from dead block is dead */
5504 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5505 if (!is_Bad(get_irn_n(block, i)))
5509 ir_graph *irg = get_irn_irg(block);
5510 /* the start block is never dead */
5511 if (block != get_irg_start_block(irg)
5512 && block != get_irg_end_block(irg)) {
5514 * Do NOT kill control flow without setting
5515 * the block to dead of bad things can happen:
5516 * We get a Block that is not reachable be irg_block_walk()
5517 * but can be found by irg_walk()!
5519 set_Block_dead(block);
5526 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5527 blocks predecessors is dead. */
5528 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5529 irn_arity = get_irn_arity(node);
5532 * Beware: we can only read the block of a non-floating node.
5534 if (is_irn_pinned_in_irg(node) &&
5535 is_Block_dead(get_nodes_block(skip_Proj(node))))
5538 for (i = 0; i < irn_arity; i++) {
5539 ir_node *pred = get_irn_n(node, i);
5544 /* Propagating Unknowns here seems to be a bad idea, because
5545 sometimes we need a node as a input and did not want that
5547 However, it might be useful to move this into a later phase
5548 (if you think that optimizing such code is useful). */
5549 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5550 return new_Unknown(get_irn_mode(node));
5555 /* With this code we violate the agreement that local_optimize
5556 only leaves Bads in Block, Phi and Tuple nodes. */
5557 /* If Block has only Bads as predecessors it's garbage. */
5558 /* If Phi has only Bads as predecessors it's garbage. */
5559 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5560 irn_arity = get_irn_arity(node);
5561 for (i = 0; i < irn_arity; i++) {
5562 if (!is_Bad(get_irn_n(node, i))) break;
5564 if (i == irn_arity) node = new_Bad();
5571 * These optimizations deallocate nodes from the obstack.
5572 * It can only be called if it is guaranteed that no other nodes
5573 * reference this one, i.e., right after construction of a node.
5575 * @param n The node to optimize
5577 * current_ir_graph must be set to the graph of the node!
5579 ir_node *optimize_node(ir_node *n) {
5582 ir_opcode iro = get_irn_opcode(n);
5584 /* Always optimize Phi nodes: part of the construction. */
5585 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5587 /* constant expression evaluation / constant folding */
5588 if (get_opt_constant_folding()) {
5589 /* neither constants nor Tuple values can be evaluated */
5590 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5591 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5592 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5593 /* try to evaluate */
5594 tv = computed_value(n);
5595 if (tv != tarval_bad) {
5597 ir_type *old_tp = get_irn_type(n);
5598 int i, arity = get_irn_arity(n);
5602 * Try to recover the type of the new expression.
5604 for (i = 0; i < arity && !old_tp; ++i)
5605 old_tp = get_irn_type(get_irn_n(n, i));
5608 * we MUST copy the node here temporary, because it's still needed
5609 * for DBG_OPT_CSTEVAL
5611 node_size = offsetof(ir_node, attr) + n->op->attr_size;
5612 oldn = alloca(node_size);
5614 memcpy(oldn, n, node_size);
5615 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5617 /* ARG, copy the in array, we need it for statistics */
5618 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5620 /* note the inplace edges module */
5621 edges_node_deleted(n, current_ir_graph);
5623 /* evaluation was successful -- replace the node. */
5624 irg_kill_node(current_ir_graph, n);
5625 nw = new_Const(get_tarval_mode(tv), tv);
5627 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5628 set_Const_type(nw, old_tp);
5629 DBG_OPT_CSTEVAL(oldn, nw);
5630 tarval_enable_fp_ops(old_fp_mode);
5633 tarval_enable_fp_ops(old_fp_mode);
5637 /* remove unnecessary nodes */
5638 if (get_opt_constant_folding() ||
5639 (iro == iro_Phi) || /* always optimize these nodes. */
5641 (iro == iro_Proj) ||
5642 (iro == iro_Block) ) /* Flags tested local. */
5643 n = equivalent_node(n);
5645 /* Common Subexpression Elimination.
5647 * Checks whether n is already available.
5648 * The block input is used to distinguish different subexpressions. Right
5649 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5650 * subexpressions within a block.
5653 n = identify_cons(current_ir_graph->value_table, n);
5656 edges_node_deleted(oldn, current_ir_graph);
5658 /* We found an existing, better node, so we can deallocate the old node. */
5659 irg_kill_node(current_ir_graph, oldn);
5663 /* Some more constant expression evaluation that does not allow to
5665 iro = get_irn_opcode(n);
5666 if (get_opt_constant_folding() ||
5667 (iro == iro_Cond) ||
5668 (iro == iro_Proj)) /* Flags tested local. */
5669 n = transform_node(n);
5671 /* Remove nodes with dead (Bad) input.
5672 Run always for transformation induced Bads. */
5675 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5676 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5677 n = identify_remember(current_ir_graph->value_table, n);
5681 } /* optimize_node */
5685 * These optimizations never deallocate nodes (in place). This can cause dead
5686 * nodes lying on the obstack. Remove these by a dead node elimination,
5687 * i.e., a copying garbage collection.
5689 ir_node *optimize_in_place_2(ir_node *n) {
5692 ir_opcode iro = get_irn_opcode(n);
5694 if (!get_opt_optimize() && !is_Phi(n)) return n;
5696 /* constant expression evaluation / constant folding */
5697 if (get_opt_constant_folding()) {
5698 /* neither constants nor Tuple values can be evaluated */
5699 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5700 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5701 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5702 /* try to evaluate */
5703 tv = computed_value(n);
5704 if (tv != tarval_bad) {
5705 /* evaluation was successful -- replace the node. */
5706 ir_type *old_tp = get_irn_type(n);
5707 int i, arity = get_irn_arity(n);
5710 * Try to recover the type of the new expression.
5712 for (i = 0; i < arity && !old_tp; ++i)
5713 old_tp = get_irn_type(get_irn_n(n, i));
5715 n = new_Const(get_tarval_mode(tv), tv);
5717 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5718 set_Const_type(n, old_tp);
5720 DBG_OPT_CSTEVAL(oldn, n);
5721 tarval_enable_fp_ops(old_fp_mode);
5724 tarval_enable_fp_ops(old_fp_mode);
5728 /* remove unnecessary nodes */
5729 if (get_opt_constant_folding() ||
5730 (iro == iro_Phi) || /* always optimize these nodes. */
5731 (iro == iro_Id) || /* ... */
5732 (iro == iro_Proj) || /* ... */
5733 (iro == iro_Block) ) /* Flags tested local. */
5734 n = equivalent_node(n);
5736 /** common subexpression elimination **/
5737 /* Checks whether n is already available. */
5738 /* The block input is used to distinguish different subexpressions. Right
5739 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5740 subexpressions within a block. */
5741 if (get_opt_cse()) {
5742 n = identify(current_ir_graph->value_table, n);
5745 /* Some more constant expression evaluation. */
5746 iro = get_irn_opcode(n);
5747 if (get_opt_constant_folding() ||
5748 (iro == iro_Cond) ||
5749 (iro == iro_Proj)) /* Flags tested local. */
5750 n = transform_node(n);
5752 /* Remove nodes with dead (Bad) input.
5753 Run always for transformation induced Bads. */
5756 /* Now we can verify the node, as it has no dead inputs any more. */
5759 /* Now we have a legal, useful node. Enter it in hash table for cse.
5760 Blocks should be unique anyways. (Except the successor of start:
5761 is cse with the start block!) */
5762 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5763 n = identify_remember(current_ir_graph->value_table, n);
5766 } /* optimize_in_place_2 */
5769 * Wrapper for external use, set proper status bits after optimization.
5771 ir_node *optimize_in_place(ir_node *n) {
5772 /* Handle graph state */
5773 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5775 if (get_opt_global_cse())
5776 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5777 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5778 set_irg_outs_inconsistent(current_ir_graph);
5780 /* FIXME: Maybe we could also test whether optimizing the node can
5781 change the control graph. */
5782 set_irg_doms_inconsistent(current_ir_graph);
5783 return optimize_in_place_2(n);
5784 } /* optimize_in_place */
5787 * Sets the default operation for an ir_ops.
5789 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5790 ops = firm_set_default_computed_value(code, ops);
5791 ops = firm_set_default_equivalent_node(code, ops);
5792 ops = firm_set_default_transform_node(code, ops);
5793 ops = firm_set_default_node_cmp_attr(code, ops);
5794 ops = firm_set_default_get_type(code, ops);
5795 ops = firm_set_default_get_type_attr(code, ops);
5796 ops = firm_set_default_get_entity_attr(code, ops);
5799 } /* firm_set_default_operations */