2 * Copyright (C) 1995-2007 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) && mode_is_signed(get_irn_mode(a)))
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 */
537 (get_irn_op(aa) == op_Proj)
538 && (mode_is_reference(get_irn_mode(aa)))
539 && (get_irn_op(aaa) == op_Alloc))
540 && ( (/* ab is NULL */
541 (get_irn_op(ab) == op_Const)
542 && (mode_is_reference(get_irn_mode(ab)))
543 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
544 || (/* ab is other Alloc */
545 (get_irn_op(ab) == op_Proj)
546 && (mode_is_reference(get_irn_mode(ab)))
547 && (get_irn_op(aba) == op_Alloc)
549 || (/* aa is NULL and aba is Alloc */
550 (get_irn_op(aa) == op_Const)
551 && (mode_is_reference(get_irn_mode(aa)))
552 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
553 && (get_irn_op(ab) == op_Proj)
554 && (mode_is_reference(get_irn_mode(ab)))
555 && (get_irn_op(aba) == op_Alloc)))
557 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
560 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 } /* computed_value_Proj_Cmp */
564 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565 * Proj(DivMod) and Proj(Quot).
567 static tarval *computed_value_Proj(ir_node *n) {
568 ir_node *a = get_Proj_pred(n);
571 switch (get_irn_opcode(a)) {
573 return computed_value_Proj_Cmp(n);
576 /* compute either the Div or the Mod part */
577 proj_nr = get_Proj_proj(n);
578 if (proj_nr == pn_DivMod_res_div)
579 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580 else if (proj_nr == pn_DivMod_res_mod)
581 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
585 if (get_Proj_proj(n) == pn_Div_res)
586 return computed_value(a);
590 if (get_Proj_proj(n) == pn_Mod_res)
591 return computed_value(a);
595 if (get_Proj_proj(n) == pn_Quot_res)
596 return computed_value(a);
603 } /* computed_value_Proj */
606 * Calculate the value of a Mux: can be evaluated, if the
607 * sel and the right input are known.
609 static tarval *computed_value_Mux(ir_node *n) {
610 ir_node *sel = get_Mux_sel(n);
611 tarval *ts = value_of(sel);
613 if (ts == get_tarval_b_true()) {
614 ir_node *v = get_Mux_true(n);
617 else if (ts == get_tarval_b_false()) {
618 ir_node *v = get_Mux_false(n);
622 } /* computed_value_Mux */
625 * Calculate the value of a Psi: can be evaluated, if a condition is true
626 * and all previous conditions are false. If all conditions are false
627 * we evaluate to the default one.
629 static tarval *computed_value_Psi(ir_node *n) {
631 return computed_value_Mux(n);
633 } /* computed_value_Psi */
636 * Calculate the value of a Confirm: can be evaluated,
637 * if it has the form Confirm(x, '=', Const).
639 static tarval *computed_value_Confirm(ir_node *n) {
640 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641 value_of(get_Confirm_bound(n)) : tarval_bad;
642 } /* computed_value_Confirm */
645 * If the parameter n can be computed, return its value, else tarval_bad.
646 * Performs constant folding.
648 * @param n The node this should be evaluated
650 tarval *computed_value(ir_node *n) {
651 if (n->op->ops.computed_value)
652 return n->op->ops.computed_value(n);
654 } /* computed_value */
657 * Set the default computed_value evaluator in an ir_op_ops.
659 * @param code the opcode for the default operation
660 * @param ops the operations initialized
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
669 ops->computed_value = computed_value_##a; \
704 } /* firm_set_default_computed_value */
707 * Returns a equivalent block for another block.
708 * If the block has only one predecessor, this is
709 * the equivalent one. If the only predecessor of a block is
710 * the block itself, this is a dead block.
712 * If both predecessors of a block are the branches of a binary
713 * Cond, the equivalent block is Cond's block.
715 * If all predecessors of a block are bad or lies in a dead
716 * block, the current block is dead as well.
718 * Note, that blocks are NEVER turned into Bad's, instead
719 * the dead_block flag is set. So, never test for is_Bad(block),
720 * always use is_dead_Block(block).
722 static ir_node *equivalent_node_Block(ir_node *n)
725 int n_preds = get_Block_n_cfgpreds(n);
727 /* The Block constructor does not call optimize, but mature_immBlock
728 calls the optimization. */
729 assert(get_Block_matured(n));
731 /* Straightening: a single entry Block following a single exit Block
732 can be merged, if it is not the Start block. */
733 /* !!! Beware, all Phi-nodes of n must have been optimized away.
734 This should be true, as the block is matured before optimize is called.
735 But what about Phi-cycles with the Phi0/Id that could not be resolved?
736 Remaining Phi nodes are just Ids. */
737 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
738 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
739 if (predblock == oldn) {
740 /* Jmp jumps into the block it is in -- deal self cycle. */
741 n = set_Block_dead(n);
742 DBG_OPT_DEAD_BLOCK(oldn, n);
743 } else if (get_opt_control_flow_straightening()) {
745 DBG_OPT_STG(oldn, n);
747 } else if ((n_preds == 1) &&
748 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
749 ir_node *predblock = get_Block_cfgpred_block(n, 0);
750 if (predblock == oldn) {
751 /* Jmp jumps into the block it is in -- deal self cycle. */
752 n = set_Block_dead(n);
753 DBG_OPT_DEAD_BLOCK(oldn, n);
755 } else if ((n_preds == 2) &&
756 (get_opt_control_flow_weak_simplification())) {
757 /* Test whether Cond jumps twice to this block
758 * The more general case which more than 2 predecessors is handles
759 * in optimize_cf(), we handle only this special case for speed here.
761 ir_node *a = get_Block_cfgpred(n, 0);
762 ir_node *b = get_Block_cfgpred(n, 1);
764 if ((get_irn_op(a) == op_Proj) &&
765 (get_irn_op(b) == op_Proj) &&
766 (get_Proj_pred(a) == get_Proj_pred(b)) &&
767 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
768 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
769 /* Also a single entry Block following a single exit Block. Phis have
770 twice the same operand and will be optimized away. */
771 n = get_nodes_block(get_Proj_pred(a));
772 DBG_OPT_IFSIM1(oldn, a, b, n);
774 } else if (get_opt_unreachable_code() &&
775 (n != get_irg_start_block(current_ir_graph)) &&
776 (n != get_irg_end_block(current_ir_graph)) ) {
779 /* If all inputs are dead, this block is dead too, except if it is
780 the start or end block. This is one step of unreachable code
782 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
783 ir_node *pred = get_Block_cfgpred(n, i);
786 if (is_Bad(pred)) continue;
787 pred_blk = get_nodes_block(skip_Proj(pred));
789 if (is_Block_dead(pred_blk)) continue;
792 /* really found a living input */
797 n = set_Block_dead(n);
798 DBG_OPT_DEAD_BLOCK(oldn, n);
803 } /* equivalent_node_Block */
806 * Returns a equivalent node for a Jmp, a Bad :-)
807 * Of course this only happens if the Block of the Jmp is dead.
809 static ir_node *equivalent_node_Jmp(ir_node *n) {
810 /* unreachable code elimination */
811 if (is_Block_dead(get_nodes_block(n)))
815 } /* equivalent_node_Jmp */
817 /** Raise is handled in the same way as Jmp. */
818 #define equivalent_node_Raise equivalent_node_Jmp
821 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
822 See transform_node_Proj_Cond(). */
825 * Optimize operations that are commutative and have neutral 0,
826 * so a op 0 = 0 op a = a.
828 static ir_node *equivalent_node_neutral_zero(ir_node *n)
832 ir_node *a = get_binop_left(n);
833 ir_node *b = get_binop_right(n);
838 /* After running compute_node there is only one constant predecessor.
839 Find this predecessors value and remember the other node: */
840 if ((tv = value_of(a)) != tarval_bad) {
842 } else if ((tv = value_of(b)) != tarval_bad) {
847 /* If this predecessors constant value is zero, the operation is
848 * unnecessary. Remove it.
850 * Beware: If n is a Add, the mode of on and n might be different
851 * which happens in this rare construction: NULL + 3.
852 * Then, a Conv would be needed which we cannot include here.
854 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
857 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
861 } /* equivalent_node_neutral_zero */
864 * Eor is commutative and has neutral 0.
866 #define equivalent_node_Eor equivalent_node_neutral_zero
869 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
871 * The second one looks strange, but this construct
872 * is used heavily in the LCC sources :-).
874 * Beware: The Mode of an Add may be different than the mode of its
875 * predecessors, so we could not return a predecessors in all cases.
877 static ir_node *equivalent_node_Add(ir_node *n) {
879 ir_node *left, *right;
880 ir_mode *mode = get_irn_mode(n);
882 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
883 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
886 n = equivalent_node_neutral_zero(n);
890 left = get_Add_left(n);
891 right = get_Add_right(n);
893 if (get_irn_op(left) == op_Sub) {
894 if (get_Sub_right(left) == right) {
897 n = get_Sub_left(left);
898 if (mode == get_irn_mode(n)) {
899 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
904 if (get_irn_op(right) == op_Sub) {
905 if (get_Sub_right(right) == left) {
908 n = get_Sub_left(right);
909 if (mode == get_irn_mode(n)) {
910 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
916 } /* equivalent_node_Add */
919 * optimize operations that are not commutative but have neutral 0 on left,
922 static ir_node *equivalent_node_left_zero(ir_node *n) {
925 ir_node *a = get_binop_left(n);
926 ir_node *b = get_binop_right(n);
928 if (is_Const(b) && is_Const_null(b)) {
931 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
934 } /* equivalent_node_left_zero */
936 #define equivalent_node_Shl equivalent_node_left_zero
937 #define equivalent_node_Shr equivalent_node_left_zero
938 #define equivalent_node_Shrs equivalent_node_left_zero
939 #define equivalent_node_Rot equivalent_node_left_zero
942 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
944 * The second one looks strange, but this construct
945 * is used heavily in the LCC sources :-).
947 * Beware: The Mode of a Sub may be different than the mode of its
948 * predecessors, so we could not return a predecessors in all cases.
950 static ir_node *equivalent_node_Sub(ir_node *n) {
953 ir_mode *mode = get_irn_mode(n);
955 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
956 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
959 b = get_Sub_right(n);
961 /* Beware: modes might be different */
962 if (is_Const(b) && is_Const_null(b)) {
963 ir_node *a = get_Sub_left(n);
964 if (mode == get_irn_mode(a)) {
967 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
971 } /* equivalent_node_Sub */
975 * Optimize an "idempotent unary op", ie op(op(n)) = n.
978 * -(-a) == a, but might overflow two times.
979 * We handle it anyway here but the better way would be a
980 * flag. This would be needed for Pascal for instance.
982 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
984 ir_node *pred = get_unop_op(n);
986 /* optimize symmetric unop */
987 if (get_irn_op(pred) == get_irn_op(n)) {
988 n = get_unop_op(pred);
989 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
992 } /* equivalent_node_idempotent_unop */
994 /** Optimize Not(Not(x)) == x. */
995 #define equivalent_node_Not equivalent_node_idempotent_unop
997 /** -(-x) == x ??? Is this possible or can --x raise an
998 out of bounds exception if min =! max? */
999 #define equivalent_node_Minus equivalent_node_idempotent_unop
1002 * Optimize a * 1 = 1 * a = a.
1004 static ir_node *equivalent_node_Mul(ir_node *n) {
1006 ir_node *a = get_Mul_left(n);
1008 /* we can handle here only the n * n = n bit cases */
1009 if (get_irn_mode(n) == get_irn_mode(a)) {
1010 ir_node *b = get_Mul_right(n);
1012 /* Mul is commutative and has again an other neutral element. */
1013 if (is_Const(a) && is_Const_one(a)) {
1015 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1016 } else if (is_Const(b) && is_Const_one(b)) {
1018 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1022 } /* equivalent_node_Mul */
1025 * Optimize a / 1 = a.
1027 static ir_node *equivalent_node_Div(ir_node *n) {
1028 ir_node *a = get_Div_left(n);
1029 ir_node *b = get_Div_right(n);
1031 /* Div is not commutative. */
1032 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1033 /* Turn Div into a tuple (mem, bad, a) */
1034 ir_node *mem = get_Div_mem(n);
1035 ir_node *blk = get_irn_n(n, -1);
1036 turn_into_tuple(n, pn_Div_max);
1037 set_Tuple_pred(n, pn_Div_M, mem);
1038 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1039 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1040 set_Tuple_pred(n, pn_Div_res, a);
1043 } /* equivalent_node_Div */
1046 * Optimize a / 1.0 = a.
1048 static ir_node *equivalent_node_Quot(ir_node *n) {
1049 ir_node *a = get_Quot_left(n);
1050 ir_node *b = get_Quot_right(n);
1052 /* Div is not commutative. */
1053 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1054 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1055 ir_node *mem = get_Quot_mem(n);
1056 ir_node *blk = get_irn_n(n, -1);
1057 turn_into_tuple(n, pn_Quot_max);
1058 set_Tuple_pred(n, pn_Quot_M, mem);
1059 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1060 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1061 set_Tuple_pred(n, pn_Quot_res, a);
1064 } /* equivalent_node_Quot */
1067 * Optimize a / 1 = a.
1069 static ir_node *equivalent_node_DivMod(ir_node *n) {
1070 ir_node *b = get_DivMod_right(n);
1072 /* Div is not commutative. */
1073 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1074 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1075 ir_node *a = get_DivMod_left(n);
1076 ir_node *mem = get_Div_mem(n);
1077 ir_node *blk = get_irn_n(n, -1);
1078 ir_mode *mode = get_DivMod_resmode(n);
1080 turn_into_tuple(n, pn_DivMod_max);
1081 set_Tuple_pred(n, pn_DivMod_M, mem);
1082 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1083 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1084 set_Tuple_pred(n, pn_DivMod_res_div, a);
1085 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1088 } /* equivalent_node_DivMod */
1091 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1093 static ir_node *equivalent_node_Or(ir_node *n) {
1096 ir_node *a = get_Or_left(n);
1097 ir_node *b = get_Or_right(n);
1100 n = a; /* Or has it's own neutral element */
1101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1102 } else if (is_Const(a) && is_Const_null(a)) {
1104 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1105 } else if (is_Const(b) && is_Const_null(b)) {
1107 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1111 } /* equivalent_node_Or */
1114 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1116 static ir_node *equivalent_node_And(ir_node *n) {
1119 ir_node *a = get_And_left(n);
1120 ir_node *b = get_And_right(n);
1123 n = a; /* And has it's own neutral element */
1124 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1127 if (is_Const(a) && is_Const_all_one(a)) {
1129 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1132 if (is_Const(b) && is_Const_all_one(b)) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1146 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1155 } /* equivalent_node_And */
1158 * Try to remove useless Conv's:
1160 static ir_node *equivalent_node_Conv(ir_node *n) {
1162 ir_node *a = get_Conv_op(n);
1165 ir_mode *n_mode = get_irn_mode(n);
1166 ir_mode *a_mode = get_irn_mode(a);
1168 if (n_mode == a_mode) { /* No Conv necessary */
1169 if (get_Conv_strict(n)) {
1170 /* special case: the predecessor might be a also a Conv */
1172 if (! get_Conv_strict(a)) {
1173 /* first one is not strict, kick it */
1174 set_Conv_op(n, get_Conv_op(a));
1177 /* else both are strict conv, second is superflous */
1179 /* leave strict floating point Conv's */
1184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1185 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1189 n_mode = get_irn_mode(n);
1190 b_mode = get_irn_mode(b);
1192 if (n_mode == b_mode) {
1193 if (n_mode == mode_b) {
1194 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1195 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1196 } else if (mode_is_int(n_mode)) {
1197 if (smaller_mode(b_mode, a_mode)){
1198 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1199 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1205 } /* equivalent_node_Conv */
1208 * A Cast may be removed if the type of the previous node
1209 * is already the type of the Cast.
1211 static ir_node *equivalent_node_Cast(ir_node *n) {
1213 ir_node *pred = get_Cast_op(n);
1215 if (get_irn_type(pred) == get_Cast_type(n)) {
1217 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1220 } /* equivalent_node_Cast */
1223 * Several optimizations:
1224 * - no Phi in start block.
1225 * - remove Id operators that are inputs to Phi
1226 * - fold Phi-nodes, iff they have only one predecessor except
1229 static ir_node *equivalent_node_Phi(ir_node *n) {
1233 ir_node *block = NULL; /* to shutup gcc */
1234 ir_node *first_val = NULL; /* to shutup gcc */
1236 if (!get_opt_normalize()) return n;
1238 n_preds = get_Phi_n_preds(n);
1240 block = get_nodes_block(n);
1241 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1242 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1243 if ((is_Block_dead(block)) || /* Control dead */
1244 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1245 return new_Bad(); /* in the Start Block. */
1247 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1249 /* If the Block has a Bad pred, we also have one. */
1250 for (i = 0; i < n_preds; ++i)
1251 if (is_Bad(get_Block_cfgpred(block, i)))
1252 set_Phi_pred(n, i, new_Bad());
1254 /* Find first non-self-referencing input */
1255 for (i = 0; i < n_preds; ++i) {
1256 first_val = get_Phi_pred(n, i);
1257 if ( (first_val != n) /* not self pointer */
1259 && (! is_Bad(first_val))
1261 ) { /* value not dead */
1262 break; /* then found first value. */
1267 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1271 /* search for rest of inputs, determine if any of these
1272 are non-self-referencing */
1273 while (++i < n_preds) {
1274 ir_node *scnd_val = get_Phi_pred(n, i);
1275 if ( (scnd_val != n)
1276 && (scnd_val != first_val)
1278 && (! is_Bad(scnd_val))
1286 /* Fold, if no multiple distinct non-self-referencing inputs */
1288 DBG_OPT_PHI(oldn, n);
1291 } /* equivalent_node_Phi */
1294 * Several optimizations:
1295 * - no Sync in start block.
1296 * - fold Sync-nodes, iff they have only one predecessor except
1299 static ir_node *equivalent_node_Sync(ir_node *n) {
1303 ir_node *first_val = NULL; /* to shutup gcc */
1305 if (!get_opt_normalize()) return n;
1307 n_preds = get_Sync_n_preds(n);
1309 /* Find first non-self-referencing input */
1310 for (i = 0; i < n_preds; ++i) {
1311 first_val = get_Sync_pred(n, i);
1312 if ((first_val != n) /* not self pointer */ &&
1313 (! is_Bad(first_val))
1314 ) { /* value not dead */
1315 break; /* then found first value. */
1320 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1323 /* search the rest of inputs, determine if any of these
1324 are non-self-referencing */
1325 while (++i < n_preds) {
1326 ir_node *scnd_val = get_Sync_pred(n, i);
1327 if ((scnd_val != n) &&
1328 (scnd_val != first_val) &&
1329 (! is_Bad(scnd_val))
1335 /* Fold, if no multiple distinct non-self-referencing inputs */
1337 DBG_OPT_SYNC(oldn, n);
1340 } /* equivalent_node_Sync */
1343 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1344 * ProjX(Load) and ProjX(Store).
1346 static ir_node *equivalent_node_Proj(ir_node *proj) {
1347 ir_node *oldn = proj;
1348 ir_node *a = get_Proj_pred(proj);
1350 if (get_irn_op(a) == op_Tuple) {
1351 /* Remove the Tuple/Proj combination. */
1352 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1353 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1354 DBG_OPT_TUPLE(oldn, a, proj);
1356 /* This should not happen! */
1357 assert(! "found a Proj with higher number than Tuple predecessors");
1360 } else if (get_irn_mode(proj) == mode_X) {
1361 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1362 /* Remove dead control flow -- early gigo(). */
1364 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1365 ir_op *op = get_irn_op(a);
1367 if (op == op_Load) {
1368 /* get the Load address */
1369 ir_node *addr = get_Load_ptr(a);
1370 ir_node *blk = get_irn_n(a, -1);
1373 if (value_not_null(addr, &confirm)) {
1374 if (confirm == NULL) {
1375 /* this node may float if it did not depend on a Confirm */
1376 set_irn_pinned(a, op_pin_state_floats);
1378 if (get_Proj_proj(proj) == pn_Load_X_except) {
1379 DBG_OPT_EXC_REM(proj);
1382 return new_r_Jmp(current_ir_graph, blk);
1384 } else if (op == op_Store) {
1385 /* get the load/store address */
1386 ir_node *addr = get_Store_ptr(a);
1387 ir_node *blk = get_irn_n(a, -1);
1390 if (value_not_null(addr, &confirm)) {
1391 if (confirm == NULL) {
1392 /* this node may float if it did not depend on a Confirm */
1393 set_irn_pinned(a, op_pin_state_floats);
1395 if (get_Proj_proj(proj) == pn_Store_X_except) {
1396 DBG_OPT_EXC_REM(proj);
1399 return new_r_Jmp(current_ir_graph, blk);
1406 } /* equivalent_node_Proj */
1411 static ir_node *equivalent_node_Id(ir_node *n) {
1416 } while (get_irn_op(n) == op_Id);
1418 DBG_OPT_ID(oldn, n);
1420 } /* equivalent_node_Id */
1425 static ir_node *equivalent_node_Mux(ir_node *n)
1427 ir_node *oldn = n, *sel = get_Mux_sel(n);
1428 tarval *ts = value_of(sel);
1430 /* Mux(true, f, t) == t */
1431 if (ts == tarval_b_true) {
1432 n = get_Mux_true(n);
1433 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1435 /* Mux(false, f, t) == f */
1436 else if (ts == tarval_b_false) {
1437 n = get_Mux_false(n);
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1440 /* Mux(v, x, x) == x */
1441 else if (get_Mux_false(n) == get_Mux_true(n)) {
1442 n = get_Mux_true(n);
1443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1445 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1446 ir_node *cmp = get_Proj_pred(sel);
1447 long proj_nr = get_Proj_proj(sel);
1448 ir_node *b = get_Mux_false(n);
1449 ir_node *a = get_Mux_true(n);
1452 * Note: normalization puts the constant on the right site,
1453 * so we check only one case.
1455 * Note further that these optimization work even for floating point
1456 * with NaN's because -NaN == NaN.
1457 * However, if +0 and -0 is handled differently, we cannot use the first one.
1459 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1460 ir_node *cmp_r = get_Cmp_right(cmp);
1461 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1462 /* Mux(a CMP 0, X, a) */
1463 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1464 /* Mux(a CMP 0, -a, a) */
1465 if (proj_nr == pn_Cmp_Eq) {
1466 /* Mux(a == 0, -a, a) ==> -a */
1468 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1469 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1470 /* Mux(a != 0, -a, a) ==> a */
1472 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1474 } else if (is_Const(b) && is_Const_null(b)) {
1475 /* Mux(a CMP 0, 0, a) */
1476 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1477 /* Mux(a != 0, 0, a) ==> a */
1479 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1480 } else if (proj_nr == pn_Cmp_Eq) {
1481 /* Mux(a == 0, 0, a) ==> 0 */
1483 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1490 } /* equivalent_node_Mux */
1493 * Returns a equivalent node of a Psi: if a condition is true
1494 * and all previous conditions are false we know its value.
1495 * If all conditions are false its value is the default one.
1497 static ir_node *equivalent_node_Psi(ir_node *n) {
1499 return equivalent_node_Mux(n);
1501 } /* equivalent_node_Psi */
1504 * Optimize -a CMP -b into b CMP a.
1505 * This works only for for modes where unary Minus
1507 * Note that two-complement integers can Overflow
1508 * so it will NOT work.
1510 * For == and != can be handled in Proj(Cmp)
1512 static ir_node *equivalent_node_Cmp(ir_node *n) {
1513 ir_node *left = get_Cmp_left(n);
1514 ir_node *right = get_Cmp_right(n);
1516 if (is_Minus(left) && is_Minus(right) &&
1517 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1518 left = get_Minus_op(left);
1519 right = get_Minus_op(right);
1520 set_Cmp_left(n, right);
1521 set_Cmp_right(n, left);
1524 } /* equivalent_node_Cmp */
1527 * Remove Confirm nodes if setting is on.
1528 * Replace Confirms(x, '=', Constlike) by Constlike.
1530 static ir_node *equivalent_node_Confirm(ir_node *n) {
1531 ir_node *pred = get_Confirm_value(n);
1532 pn_Cmp pnc = get_Confirm_cmp(n);
1534 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1536 * rare case: two identical Confirms one after another,
1537 * replace the second one with the first.
1541 if (pnc == pn_Cmp_Eq) {
1542 ir_node *bound = get_Confirm_bound(n);
1545 * Optimize a rare case:
1546 * Confirm(x, '=', Constlike) ==> Constlike
1548 if (is_irn_constlike(bound)) {
1549 DBG_OPT_CONFIRM(n, bound);
1553 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1557 * Optimize CopyB(mem, x, x) into a Nop.
1559 static ir_node *equivalent_node_CopyB(ir_node *n) {
1560 ir_node *a = get_CopyB_dst(n);
1561 ir_node *b = get_CopyB_src(n);
1564 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1565 ir_node *mem = get_CopyB_mem(n);
1566 ir_node *blk = get_nodes_block(n);
1567 turn_into_tuple(n, pn_CopyB_max);
1568 set_Tuple_pred(n, pn_CopyB_M, mem);
1569 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1570 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1571 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1574 } /* equivalent_node_CopyB */
1577 * Optimize Bounds(idx, idx, upper) into idx.
1579 static ir_node *equivalent_node_Bound(ir_node *n) {
1580 ir_node *idx = get_Bound_index(n);
1581 ir_node *lower = get_Bound_lower(n);
1584 /* By definition lower < upper, so if idx == lower -->
1585 lower <= idx && idx < upper */
1587 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1590 ir_node *pred = skip_Proj(idx);
1592 if (get_irn_op(pred) == op_Bound) {
1594 * idx was Bounds_check previously, it is still valid if
1595 * lower <= pred_lower && pred_upper <= upper.
1597 ir_node *upper = get_Bound_upper(n);
1598 if (get_Bound_lower(pred) == lower &&
1599 get_Bound_upper(pred) == upper) {
1601 * One could expect that we simply return the previous
1602 * Bound here. However, this would be wrong, as we could
1603 * add an exception Proj to a new location then.
1604 * So, we must turn in into a tuple.
1611 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1612 ir_node *mem = get_Bound_mem(n);
1613 ir_node *blk = get_nodes_block(n);
1614 turn_into_tuple(n, pn_Bound_max);
1615 set_Tuple_pred(n, pn_Bound_M, mem);
1616 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1617 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1618 set_Tuple_pred(n, pn_Bound_res, idx);
1621 } /* equivalent_node_Bound */
1624 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1625 * perform no actual computation, as, e.g., the Id nodes. It does not create
1626 * new nodes. It is therefore safe to free n if the node returned is not n.
1627 * If a node returns a Tuple we can not just skip it. If the size of the
1628 * in array fits, we transform n into a tuple (e.g., Div).
1630 ir_node *equivalent_node(ir_node *n) {
1631 if (n->op->ops.equivalent_node)
1632 return n->op->ops.equivalent_node(n);
1634 } /* equivalent_node */
1637 * Sets the default equivalent node operation for an ir_op_ops.
1639 * @param code the opcode for the default operation
1640 * @param ops the operations initialized
1645 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1649 ops->equivalent_node = equivalent_node_##a; \
1689 } /* firm_set_default_equivalent_node */
1692 * Returns non-zero if a node is a Phi node
1693 * with all predecessors constant.
1695 static int is_const_Phi(ir_node *n) {
1700 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1701 if (! is_Const(get_irn_n(n, i)))
1704 } /* is_const_Phi */
1707 * Apply an evaluator on a binop with a constant operators (and one Phi).
1709 * @param phi the Phi node
1710 * @param other the other operand
1711 * @param eval an evaluator function
1712 * @param left if non-zero, other is the left operand, else the right
1714 * @return a new Phi node if the conversion was successful, NULL else
1716 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1722 int i, n = get_irn_arity(phi);
1724 NEW_ARR_A(void *, res, n);
1726 for (i = 0; i < n; ++i) {
1727 pred = get_irn_n(phi, i);
1728 tv = get_Const_tarval(pred);
1729 tv = eval(other, tv);
1731 if (tv == tarval_bad) {
1732 /* folding failed, bad */
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 tv = get_Const_tarval(pred);
1741 tv = eval(tv, other);
1743 if (tv == tarval_bad) {
1744 /* folding failed, bad */
1750 mode = get_irn_mode(phi);
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 unop with a constant operator (a Phi).
1763 * @param phi the Phi node
1764 * @param eval an evaluator function
1766 * @return a new Phi node if the conversion was successful, NULL else
1768 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1774 int i, n = get_irn_arity(phi);
1776 NEW_ARR_A(void *, res, n);
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(phi, i);
1779 tv = get_Const_tarval(pred);
1782 if (tv == tarval_bad) {
1783 /* folding failed, bad */
1788 mode = get_irn_mode(phi);
1789 irg = current_ir_graph;
1790 for (i = 0; i < n; ++i) {
1791 pred = get_irn_n(phi, i);
1792 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1793 mode, res[i], get_Const_type(pred));
1795 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1796 } /* apply_unop_on_phi */
1799 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1800 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1801 * If possible, remove the Conv's.
1803 static ir_node *transform_node_AddSub(ir_node *n) {
1804 ir_mode *mode = get_irn_mode(n);
1806 if (mode_is_reference(mode)) {
1807 ir_node *left = get_binop_left(n);
1808 ir_node *right = get_binop_right(n);
1809 int ref_bits = get_mode_size_bits(mode);
1811 if (get_irn_op(left) == op_Conv) {
1812 ir_mode *mode = get_irn_mode(left);
1813 int bits = get_mode_size_bits(mode);
1815 if (ref_bits == bits &&
1816 mode_is_int(mode) &&
1817 get_mode_arithmetic(mode) == irma_twos_complement) {
1818 ir_node *pre = get_Conv_op(left);
1819 ir_mode *pre_mode = get_irn_mode(pre);
1821 if (mode_is_int(pre_mode) &&
1822 get_mode_size_bits(pre_mode) == bits &&
1823 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1824 /* ok, this conv just changes to sign, moreover the calculation
1825 * is done with same number of bits as our address mode, so
1826 * we can ignore the conv as address calculation can be viewed
1827 * as either signed or unsigned
1829 set_binop_left(n, pre);
1834 if (get_irn_op(right) == op_Conv) {
1835 ir_mode *mode = get_irn_mode(right);
1836 int bits = get_mode_size_bits(mode);
1838 if (ref_bits == bits &&
1839 mode_is_int(mode) &&
1840 get_mode_arithmetic(mode) == irma_twos_complement) {
1841 ir_node *pre = get_Conv_op(right);
1842 ir_mode *pre_mode = get_irn_mode(pre);
1844 if (mode_is_int(pre_mode) &&
1845 get_mode_size_bits(pre_mode) == bits &&
1846 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1847 /* ok, this conv just changes to sign, moreover the calculation
1848 * is done with same number of bits as our address mode, so
1849 * we can ignore the conv as address calculation can be viewed
1850 * as either signed or unsigned
1852 set_binop_right(n, pre);
1858 } /* transform_node_AddSub */
1860 #define HANDLE_BINOP_PHI(op,a,b,c) \
1862 if (is_Const(b) && is_const_Phi(a)) { \
1863 /* check for Op(Phi, Const) */ \
1864 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1866 else if (is_Const(a) && is_const_Phi(b)) { \
1867 /* check for Op(Const, Phi) */ \
1868 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1871 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1875 #define HANDLE_UNOP_PHI(op,a,c) \
1877 if (is_const_Phi(a)) { \
1878 /* check for Op(Phi) */ \
1879 c = apply_unop_on_phi(a, op); \
1881 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1887 * Do the AddSub optimization, then Transform
1888 * Constant folding on Phi
1889 * Add(a,a) -> Mul(a, 2)
1890 * Add(Mul(a, x), a) -> Mul(a, x+1)
1891 * if the mode is integer or float.
1892 * Transform Add(a,-b) into Sub(a,b).
1893 * Reassociation might fold this further.
1895 static ir_node *transform_node_Add(ir_node *n) {
1897 ir_node *a, *b, *c, *oldn = n;
1899 n = transform_node_AddSub(n);
1901 a = get_Add_left(n);
1902 b = get_Add_right(n);
1904 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1906 mode = get_irn_mode(n);
1908 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1909 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1912 if (mode_is_num(mode)) {
1913 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1914 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1915 ir_node *block = get_irn_n(n, -1);
1918 get_irn_dbg_info(n),
1922 new_r_Const_long(current_ir_graph, block, mode, 2),
1924 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1929 get_irn_dbg_info(n),
1935 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1940 get_irn_dbg_info(n),
1946 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1949 if (! get_opt_reassociation()) {
1950 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1952 ir_node *ma = get_Mul_left(a);
1953 ir_node *mb = get_Mul_right(a);
1956 ir_node *blk = get_irn_n(n, -1);
1958 get_irn_dbg_info(n), current_ir_graph, blk,
1961 get_irn_dbg_info(n), current_ir_graph, blk,
1963 new_r_Const_long(current_ir_graph, blk, mode, 1),
1966 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1968 } else if (b == mb) {
1969 ir_node *blk = get_irn_n(n, -1);
1971 get_irn_dbg_info(n), current_ir_graph, blk,
1974 get_irn_dbg_info(n), current_ir_graph, blk,
1976 new_r_Const_long(current_ir_graph, blk, mode, 1),
1979 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1984 ir_node *ma = get_Mul_left(b);
1985 ir_node *mb = get_Mul_right(b);
1988 ir_node *blk = get_irn_n(n, -1);
1990 get_irn_dbg_info(n), current_ir_graph, blk,
1993 get_irn_dbg_info(n), current_ir_graph, blk,
1995 new_r_Const_long(current_ir_graph, blk, mode, 1),
1998 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2002 ir_node *blk = get_irn_n(n, -1);
2004 get_irn_dbg_info(n), current_ir_graph, blk,
2007 get_irn_dbg_info(n), current_ir_graph, blk,
2009 new_r_Const_long(current_ir_graph, blk, mode, 1),
2012 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2017 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2018 /* Here we rely on constants be on the RIGHT side */
2020 ir_node *op = get_Not_op(a);
2022 if (is_Const(b) && is_Const_one(b)) {
2024 ir_node *blk = get_irn_n(n, -1);
2025 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2026 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2031 ir_node *blk = get_irn_n(n, -1);
2032 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2033 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2038 ir_node *op = get_Not_op(b);
2042 ir_node *blk = get_irn_n(n, -1);
2043 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2044 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2051 } /* transform_node_Add */
2054 static ir_node *const_negate(ir_node *cnst) {
2055 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2056 dbg_info *dbgi = get_irn_dbg_info(cnst);
2057 ir_graph *irg = get_irn_irg(cnst);
2058 ir_node *block = get_nodes_block(cnst);
2059 ir_mode *mode = get_irn_mode(cnst);
2060 if (tv == tarval_bad) return NULL;
2061 return new_rd_Const(dbgi, irg, block, mode, tv);
2065 * Do the AddSub optimization, then Transform
2066 * Constant folding on Phi
2067 * Sub(0,a) -> Minus(a)
2068 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2069 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2070 * Sub(Add(a, x), x) -> a
2071 * Sub(x, Add(x, a)) -> -a
2072 * Sub(x, Const) -> Add(x, -Const)
2074 static ir_node *transform_node_Sub(ir_node *n) {
2079 n = transform_node_AddSub(n);
2081 a = get_Sub_left(n);
2082 b = get_Sub_right(n);
2084 mode = get_irn_mode(n);
2087 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2089 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2090 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2093 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2094 /* a - C -> a + (-C) */
2095 ir_node *cnst = const_negate(b);
2097 ir_node *block = get_nodes_block(n);
2098 dbg_info *dbgi = get_irn_dbg_info(n);
2099 ir_graph *irg = get_irn_irg(n);
2101 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2102 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2107 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2108 ir_graph *irg = current_ir_graph;
2109 dbg_info *dbg = get_irn_dbg_info(n);
2110 ir_node *block = get_nodes_block(n);
2111 ir_node *left = get_Minus_op(a);
2112 ir_mode *mode = get_irn_mode(n);
2113 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2115 n = new_rd_Minus(dbg, irg, block, add, mode);
2116 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2118 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2119 ir_graph *irg = current_ir_graph;
2120 dbg_info *dbg = get_irn_dbg_info(n);
2121 ir_node *block = get_nodes_block(n);
2122 ir_node *right = get_Minus_op(b);
2123 ir_mode *mode = get_irn_mode(n);
2125 n = new_rd_Add(dbg, irg, block, a, right, mode);
2126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2128 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2129 ir_graph *irg = current_ir_graph;
2130 dbg_info *s_dbg = get_irn_dbg_info(b);
2131 ir_node *s_block = get_nodes_block(b);
2132 ir_node *s_left = get_Sub_right(b);
2133 ir_node *s_right = get_Sub_left(b);
2134 ir_mode *s_mode = get_irn_mode(b);
2135 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2136 dbg_info *a_dbg = get_irn_dbg_info(n);
2137 ir_node *a_block = get_nodes_block(n);
2138 ir_mode *a_mode = get_irn_mode(n);
2140 n = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2143 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2144 ir_node *m_right = get_Mul_right(b);
2145 if (is_Const(m_right)) {
2146 ir_node *cnst2 = const_negate(m_right);
2147 if (cnst2 != NULL) {
2148 ir_graph *irg = current_ir_graph;
2149 dbg_info *m_dbg = get_irn_dbg_info(b);
2150 ir_node *m_block = get_nodes_block(b);
2151 ir_node *m_left = get_Mul_left(b);
2152 ir_mode *m_mode = get_irn_mode(b);
2153 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2154 dbg_info *a_dbg = get_irn_dbg_info(n);
2155 ir_node *a_block = get_nodes_block(n);
2156 ir_mode *a_mode = get_irn_mode(n);
2158 n = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2159 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2165 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2166 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2168 get_irn_dbg_info(n),
2173 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2177 if (mode_wrap_around(mode)) {
2178 ir_node *left = get_Add_left(a);
2179 ir_node *right = get_Add_right(a);
2181 /* FIXME: Does the Conv's work only for two complement or generally? */
2183 if (mode != get_irn_mode(right)) {
2184 /* This Sub is an effective Cast */
2185 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2188 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2190 } else if (right == b) {
2191 if (mode != get_irn_mode(left)) {
2192 /* This Sub is an effective Cast */
2193 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2202 if (mode_wrap_around(mode)) {
2203 ir_node *left = get_Add_left(b);
2204 ir_node *right = get_Add_right(b);
2206 /* FIXME: Does the Conv's work only for two complement or generally? */
2208 ir_mode *r_mode = get_irn_mode(right);
2210 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2211 if (mode != r_mode) {
2212 /* This Sub is an effective Cast */
2213 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2217 } else if (right == a) {
2218 ir_mode *l_mode = get_irn_mode(left);
2220 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2221 if (mode != l_mode) {
2222 /* This Sub is an effective Cast */
2223 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2225 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2230 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2231 ir_mode *mode = get_irn_mode(a);
2233 if (mode == get_irn_mode(b)) {
2239 /* check if it's allowed to skip the conv */
2240 ma = get_irn_mode(a);
2241 mb = get_irn_mode(b);
2243 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2244 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2246 set_Sub_right(n, b);
2252 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2253 if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2254 ir_node *ma = get_Mul_left(a);
2255 ir_node *mb = get_Mul_right(a);
2258 ir_node *blk = get_irn_n(n, -1);
2260 get_irn_dbg_info(n),
2261 current_ir_graph, blk,
2264 get_irn_dbg_info(n),
2265 current_ir_graph, blk,
2267 new_r_Const_long(current_ir_graph, blk, mode, 1),
2270 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2272 } else if (mb == b) {
2273 ir_node *blk = get_irn_n(n, -1);
2275 get_irn_dbg_info(n),
2276 current_ir_graph, blk,
2279 get_irn_dbg_info(n),
2280 current_ir_graph, blk,
2282 new_r_Const_long(current_ir_graph, blk, mode, 1),
2285 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2290 ir_node *x = get_Sub_left(a);
2291 ir_node *y = get_Sub_right(a);
2292 ir_node *blk = get_irn_n(n, -1);
2293 ir_mode *m_b = get_irn_mode(b);
2294 ir_mode *m_y = get_irn_mode(y);
2297 /* Determine the right mode for the Add. */
2300 else if (mode_is_reference(m_b))
2302 else if (mode_is_reference(m_y))
2306 * Both modes are different but none is reference,
2307 * happens for instance in SubP(SubP(P, Iu), Is).
2308 * We have two possibilities here: Cast or ignore.
2309 * Currently we ignore this case.
2314 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2316 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2317 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2321 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2322 if (is_Const(a) && is_Not(b)) {
2323 /* c - ~X = X + (c+1) */
2324 tarval *tv = get_Const_tarval(a);
2326 tv = tarval_add(tv, get_mode_one(mode));
2327 if (tv != tarval_bad) {
2328 ir_node *blk = get_irn_n(n, -1);
2329 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2330 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2337 } /* transform_node_Sub */
2340 * Several transformation done on n*n=2n bits mul.
2341 * These transformations must be done here because new nodes may be produced.
2343 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2345 ir_node *a = get_Mul_left(n);
2346 ir_node *b = get_Mul_right(n);
2347 tarval *ta = value_of(a);
2348 tarval *tb = value_of(b);
2349 ir_mode *smode = get_irn_mode(a);
2351 if (ta == get_mode_one(smode)) {
2352 ir_node *blk = get_irn_n(n, -1);
2353 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2354 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2357 else if (ta == get_mode_minus_one(smode)) {
2358 ir_node *blk = get_irn_n(n, -1);
2359 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2360 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2361 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2364 if (tb == get_mode_one(smode)) {
2365 ir_node *blk = get_irn_n(a, -1);
2366 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2367 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2370 else if (tb == get_mode_minus_one(smode)) {
2371 ir_node *blk = get_irn_n(n, -1);
2372 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2373 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2374 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2381 * Transform Mul(a,-1) into -a.
2382 * Do constant evaluation of Phi nodes.
2383 * Do architecture dependent optimizations on Mul nodes
2385 static ir_node *transform_node_Mul(ir_node *n) {
2386 ir_node *c, *oldn = n;
2387 ir_mode *mode = get_irn_mode(n);
2388 ir_node *a = get_Mul_left(n);
2389 ir_node *b = get_Mul_right(n);
2391 if (is_Bad(a) || is_Bad(b))
2394 if (mode != get_irn_mode(a))
2395 return transform_node_Mul2n(n, mode);
2397 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2399 if (mode_is_signed(mode)) {
2402 if (value_of(a) == get_mode_minus_one(mode))
2404 else if (value_of(b) == get_mode_minus_one(mode))
2407 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2408 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2413 if (is_Const(b)) { /* (-a) * const -> a * -const */
2414 ir_node *cnst = const_negate(b);
2416 dbg_info *dbgi = get_irn_dbg_info(n);
2417 ir_node *block = get_nodes_block(n);
2418 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2419 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2422 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2423 dbg_info *dbgi = get_irn_dbg_info(n);
2424 ir_node *block = get_nodes_block(n);
2425 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2428 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2429 ir_node *sub_l = get_Sub_left(b);
2430 ir_node *sub_r = get_Sub_right(b);
2431 dbg_info *dbgi = get_irn_dbg_info(n);
2432 ir_graph *irg = current_ir_graph;
2433 ir_node *block = get_nodes_block(n);
2434 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2435 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2436 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2439 } else if (is_Minus(b)) {
2440 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2441 ir_node *sub_l = get_Sub_left(a);
2442 ir_node *sub_r = get_Sub_right(a);
2443 dbg_info *dbgi = get_irn_dbg_info(n);
2444 ir_graph *irg = current_ir_graph;
2445 ir_node *block = get_nodes_block(n);
2446 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2447 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2448 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2452 if (get_mode_arithmetic(mode) == irma_ieee754) {
2454 tarval *tv = get_Const_tarval(a);
2455 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2456 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2457 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2461 else if (is_Const(b)) {
2462 tarval *tv = get_Const_tarval(b);
2463 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2464 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2465 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2470 return arch_dep_replace_mul_with_shifts(n);
2471 } /* transform_node_Mul */
2474 * Transform a Div Node.
2476 static ir_node *transform_node_Div(ir_node *n) {
2477 tarval *tv = value_of(n);
2478 ir_mode *mode = get_Div_resmode(n);
2481 if (tv != tarval_bad) {
2482 value = new_Const(get_tarval_mode(tv), tv);
2484 DBG_OPT_CSTEVAL(n, value);
2487 ir_node *a = get_Div_left(n);
2488 ir_node *b = get_Div_right(n);
2491 if (a == b && value_not_zero(a, &dummy)) {
2492 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2493 value = new_Const(mode, get_mode_one(mode));
2494 DBG_OPT_CSTEVAL(n, value);
2497 if (mode_is_signed(mode) && is_Const(b)) {
2498 tarval *tv = get_Const_tarval(b);
2500 if (tv == get_mode_minus_one(mode)) {
2502 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2503 DBG_OPT_CSTEVAL(n, value);
2507 /* Try architecture dependent optimization */
2508 value = arch_dep_replace_div_by_const(n);
2516 /* Turn Div into a tuple (mem, jmp, bad, value) */
2517 mem = get_Div_mem(n);
2518 blk = get_irn_n(n, -1);
2520 /* skip a potential Pin */
2522 mem = get_Pin_op(mem);
2523 turn_into_tuple(n, pn_Div_max);
2524 set_Tuple_pred(n, pn_Div_M, mem);
2525 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2526 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2527 set_Tuple_pred(n, pn_Div_res, value);
2530 } /* transform_node_Div */
2533 * Transform a Mod node.
2535 static ir_node *transform_node_Mod(ir_node *n) {
2536 tarval *tv = value_of(n);
2537 ir_mode *mode = get_Mod_resmode(n);
2540 if (tv != tarval_bad) {
2541 value = new_Const(get_tarval_mode(tv), tv);
2543 DBG_OPT_CSTEVAL(n, value);
2546 ir_node *a = get_Mod_left(n);
2547 ir_node *b = get_Mod_right(n);
2550 if (a == b && value_not_zero(a, &dummy)) {
2551 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2552 value = new_Const(mode, get_mode_null(mode));
2553 DBG_OPT_CSTEVAL(n, value);
2556 if (mode_is_signed(mode) && is_Const(b)) {
2557 tarval *tv = get_Const_tarval(b);
2559 if (tv == get_mode_minus_one(mode)) {
2561 value = new_Const(mode, get_mode_null(mode));
2562 DBG_OPT_CSTEVAL(n, value);
2566 /* Try architecture dependent optimization */
2567 value = arch_dep_replace_mod_by_const(n);
2575 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2576 mem = get_Mod_mem(n);
2577 blk = get_irn_n(n, -1);
2579 /* skip a potential Pin */
2581 mem = get_Pin_op(mem);
2582 turn_into_tuple(n, pn_Mod_max);
2583 set_Tuple_pred(n, pn_Mod_M, mem);
2584 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2585 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2586 set_Tuple_pred(n, pn_Mod_res, value);
2589 } /* transform_node_Mod */
2592 * Transform a DivMod node.
2594 static ir_node *transform_node_DivMod(ir_node *n) {
2596 ir_node *a = get_DivMod_left(n);
2597 ir_node *b = get_DivMod_right(n);
2598 ir_mode *mode = get_DivMod_resmode(n);
2599 tarval *ta = value_of(a);
2600 tarval *tb = value_of(b);
2603 if (tb != tarval_bad) {
2604 if (tb == get_mode_one(get_tarval_mode(tb))) {
2605 b = new_Const(mode, get_mode_null(mode));
2606 DBG_OPT_CSTEVAL(n, b);
2608 } else if (ta != tarval_bad) {
2609 tarval *resa, *resb;
2610 resa = tarval_div(ta, tb);
2611 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2612 Jmp for X result!? */
2613 resb = tarval_mod(ta, tb);
2614 if (resb == tarval_bad) return n; /* Causes exception! */
2615 a = new_Const(mode, resa);
2616 b = new_Const(mode, resb);
2617 DBG_OPT_CSTEVAL(n, a);
2618 DBG_OPT_CSTEVAL(n, b);
2620 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2621 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2622 b = new_Const(mode, get_mode_null(mode));
2623 DBG_OPT_CSTEVAL(n, a);
2624 DBG_OPT_CSTEVAL(n, b);
2626 } else { /* Try architecture dependent optimization */
2627 arch_dep_replace_divmod_by_const(&a, &b, n);
2628 evaluated = a != NULL;
2630 } else if (a == b) {
2631 if (value_not_zero(a, &dummy)) {
2633 a = new_Const(mode, get_mode_one(mode));
2634 b = new_Const(mode, get_mode_null(mode));
2635 DBG_OPT_CSTEVAL(n, a);
2636 DBG_OPT_CSTEVAL(n, b);
2639 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2642 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2643 /* 0 / non-Const = 0 */
2648 if (evaluated) { /* replace by tuple */
2652 mem = get_DivMod_mem(n);
2653 /* skip a potential Pin */
2655 mem = get_Pin_op(mem);
2657 blk = get_irn_n(n, -1);
2658 turn_into_tuple(n, pn_DivMod_max);
2659 set_Tuple_pred(n, pn_DivMod_M, mem);
2660 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2661 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2662 set_Tuple_pred(n, pn_DivMod_res_div, a);
2663 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2667 } /* transform_node_DivMod */
2670 * Optimize x / c to x * (1/c)
2672 static ir_node *transform_node_Quot(ir_node *n) {
2673 ir_mode *mode = get_Quot_resmode(n);
2676 if (get_mode_arithmetic(mode) == irma_ieee754) {
2677 ir_node *b = get_Quot_right(n);
2680 tarval *tv = get_Const_tarval(b);
2682 tv = tarval_quo(get_mode_one(mode), tv);
2684 /* Do the transformation if the result is either exact or we are not
2685 using strict rules. */
2686 if (tv != tarval_bad &&
2687 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2688 ir_node *blk = get_irn_n(n, -1);
2689 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2690 ir_node *a = get_Quot_left(n);
2691 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2692 ir_node *mem = get_Quot_mem(n);
2694 /* skip a potential Pin */
2696 mem = get_Pin_op(mem);
2697 turn_into_tuple(n, pn_Quot_max);
2698 set_Tuple_pred(n, pn_Quot_M, mem);
2699 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2700 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2701 set_Tuple_pred(n, pn_Quot_res, m);
2702 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2707 } /* transform_node_Quot */
2710 * Optimize Abs(x) into x if x is Confirmed >= 0
2711 * Optimize Abs(x) into -x if x is Confirmed <= 0
2713 static ir_node *transform_node_Abs(ir_node *n) {
2715 ir_node *a = get_Abs_op(n);
2716 value_classify_sign sign = classify_value_sign(a);
2718 if (sign == value_classified_negative) {
2719 ir_mode *mode = get_irn_mode(n);
2722 * We can replace the Abs by -x here.
2723 * We even could add a new Confirm here.
2725 * Note that -x would create a new node, so we could
2726 * not run it in the equivalent_node() context.
2728 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2729 get_irn_n(n, -1), a, mode);
2731 DBG_OPT_CONFIRM(oldn, n);
2732 } else if (sign == value_classified_positive) {
2733 /* n is positive, Abs is not needed */
2736 DBG_OPT_CONFIRM(oldn, n);
2740 } /* transform_node_Abs */
2743 * Transform a Cond node.
2745 * Replace the Cond by a Jmp if it branches on a constant
2748 static ir_node *transform_node_Cond(ir_node *n) {
2751 ir_node *a = get_Cond_selector(n);
2752 tarval *ta = value_of(a);
2754 /* we need block info which is not available in floating irgs */
2755 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2758 if ((ta != tarval_bad) &&
2759 (get_irn_mode(a) == mode_b) &&
2760 (get_opt_unreachable_code())) {
2761 /* It's a boolean Cond, branching on a boolean constant.
2762 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2763 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2764 turn_into_tuple(n, pn_Cond_max);
2765 if (ta == tarval_b_true) {
2766 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2767 set_Tuple_pred(n, pn_Cond_true, jmp);
2769 set_Tuple_pred(n, pn_Cond_false, jmp);
2770 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2772 /* We might generate an endless loop, so keep it alive. */
2773 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2776 } /* transform_node_Cond */
2778 typedef ir_node* (*recursive_transform) (ir_node *n);
2781 * makes use of distributive laws for and, or, eor
2782 * and(a OP c, b OP c) -> and(a, b) OP c
2783 * note, might return a different op than n
2785 static ir_node *transform_bitwise_distributive(ir_node *n,
2786 recursive_transform trans_func)
2789 ir_node *a = get_binop_left(n);
2790 ir_node *b = get_binop_right(n);
2791 ir_op *op = get_irn_op(a);
2792 ir_op *op_root = get_irn_op(n);
2794 if(op != get_irn_op(b))
2797 if (op == op_Conv) {
2798 ir_node *a_op = get_Conv_op(a);
2799 ir_node *b_op = get_Conv_op(b);
2800 ir_mode *a_mode = get_irn_mode(a_op);
2801 ir_mode *b_mode = get_irn_mode(b_op);
2802 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2803 ir_node *blk = get_irn_n(n, -1);
2806 set_binop_left(n, a_op);
2807 set_binop_right(n, b_op);
2808 set_irn_mode(n, a_mode);
2810 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2812 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2818 /* nothing to gain here */
2822 if (op == op_Shrs || op == op_Shr || op == op_Shl
2823 || op == op_And || op == op_Or || op == op_Eor) {
2824 ir_node *a_left = get_binop_left(a);
2825 ir_node *a_right = get_binop_right(a);
2826 ir_node *b_left = get_binop_left(b);
2827 ir_node *b_right = get_binop_right(b);
2831 if (is_op_commutative(op)) {
2832 if (a_left == b_left) {
2836 } else if(a_left == b_right) {
2840 } else if(a_right == b_left) {
2846 if(a_right == b_right) {
2853 /* (a sop c) & (b sop c) => (a & b) sop c */
2854 ir_node *blk = get_irn_n(n, -1);
2856 ir_node *new_n = exact_copy(n);
2857 set_binop_left(new_n, op1);
2858 set_binop_right(new_n, op2);
2859 new_n = trans_func(new_n);
2861 if(op_root == op_Eor && op == op_Or) {
2862 dbg_info *dbgi = get_irn_dbg_info(n);
2863 ir_graph *irg = current_ir_graph;
2864 ir_mode *mode = get_irn_mode(c);
2866 c = new_rd_Not(dbgi, irg, blk, c, mode);
2867 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2870 set_irn_n(n, -1, blk);
2871 set_binop_left(n, new_n);
2872 set_binop_right(n, c);
2875 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2886 static ir_node *transform_node_And(ir_node *n) {
2887 ir_node *c, *oldn = n;
2888 ir_node *a = get_And_left(n);
2889 ir_node *b = get_And_right(n);
2892 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2894 mode = get_irn_mode(n);
2896 /* we can evaluate 2 Projs of the same Cmp */
2897 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2898 ir_node *pred_a = get_Proj_pred(a);
2899 ir_node *pred_b = get_Proj_pred(b);
2900 if (pred_a == pred_b) {
2901 dbg_info *dbgi = get_irn_dbg_info(n);
2902 ir_node *block = get_nodes_block(pred_a);
2903 pn_Cmp pn_a = get_Proj_proj(a);
2904 pn_Cmp pn_b = get_Proj_proj(b);
2905 /* yes, we can simply calculate with pncs */
2906 pn_Cmp new_pnc = pn_a & pn_b;
2908 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2913 ir_node *op = get_Not_op(b);
2915 ir_node *ba = get_And_left(op);
2916 ir_node *bb = get_And_right(op);
2918 /* it's enough to test the following cases due to normalization! */
2919 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2920 /* (a|b) & ~(a&b) = a^b */
2921 ir_node *block = get_nodes_block(n);
2923 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2932 ir_node *op = get_Not_op(a);
2934 ir_node *aa = get_And_left(op);
2935 ir_node *ab = get_And_right(op);
2937 /* it's enough to test the following cases due to normalization! */
2938 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2939 /* (a|b) & ~(a&b) = a^b */
2940 ir_node *block = get_nodes_block(n);
2942 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2943 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2950 ir_node *al = get_Eor_left(a);
2951 ir_node *ar = get_Eor_right(a);
2954 /* (b ^ a) & b -> ~a & b */
2955 dbg_info *dbg = get_irn_dbg_info(n);
2956 ir_node *block = get_nodes_block(n);
2958 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
2959 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
2960 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2964 /* (a ^ b) & b -> ~a & b */
2965 dbg_info *dbg = get_irn_dbg_info(n);
2966 ir_node *block = get_nodes_block(n);
2968 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
2969 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
2970 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2975 ir_node *bl = get_Eor_left(b);
2976 ir_node *br = get_Eor_right(b);
2979 /* a & (a ^ b) -> a & ~b */
2980 dbg_info *dbg = get_irn_dbg_info(n);
2981 ir_node *block = get_nodes_block(n);
2983 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
2984 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
2985 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2989 /* a & (b ^ a) -> a & ~b */
2990 dbg_info *dbg = get_irn_dbg_info(n);
2991 ir_node *block = get_nodes_block(n);
2993 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
2994 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
2995 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2999 if (is_Not(a) && is_Not(b)) {
3000 /* ~a & ~b = ~(a|b) */
3001 ir_node *block = get_nodes_block(n);
3002 ir_mode *mode = get_irn_mode(n);
3006 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3007 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3008 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3012 n = transform_bitwise_distributive(n, transform_node_And);
3015 } /* transform_node_And */
3020 static ir_node *transform_node_Eor(ir_node *n) {
3021 ir_node *c, *oldn = n;
3022 ir_node *a = get_Eor_left(n);
3023 ir_node *b = get_Eor_right(n);
3024 ir_mode *mode = get_irn_mode(n);
3026 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
3028 /* we can evaluate 2 Projs of the same Cmp */
3029 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3030 ir_node *pred_a = get_Proj_pred(a);
3031 ir_node *pred_b = get_Proj_pred(b);
3032 if(pred_a == pred_b) {
3033 dbg_info *dbgi = get_irn_dbg_info(n);
3034 ir_node *block = get_nodes_block(pred_a);
3035 pn_Cmp pn_a = get_Proj_proj(a);
3036 pn_Cmp pn_b = get_Proj_proj(b);
3037 /* yes, we can simply calculate with pncs */
3038 pn_Cmp new_pnc = pn_a ^ pn_b;
3040 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3047 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3048 mode, get_mode_null(mode));
3049 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3050 } else if ((mode == mode_b)
3051 && (get_irn_op(a) == op_Proj)
3052 && (get_irn_mode(a) == mode_b)
3053 && is_Const(b) && is_Const_one(b)
3054 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3055 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3056 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3057 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3059 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3060 } else if (mode == mode_b && is_Const(b) && is_Const_one(b)) {
3061 /* The Eor is a Not. Replace it by a Not. */
3062 /* ????!!!Extend to bitfield 1111111. */
3063 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
3065 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3067 n = transform_bitwise_distributive(n, transform_node_Eor);
3071 } /* transform_node_Eor */
3076 static ir_node *transform_node_Not(ir_node *n) {
3077 ir_node *c, *oldn = n;
3078 ir_node *a = get_Not_op(n);
3079 ir_op *op_a = get_irn_op(a);
3081 HANDLE_UNOP_PHI(tarval_not,a,c);
3083 /* check for a boolean Not */
3084 if ( (get_irn_mode(n) == mode_b)
3085 && (op_a == op_Proj)
3086 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3087 /* We negate a Cmp. The Cmp has the negated result anyways! */
3088 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3089 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3090 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3093 if (op_a == op_Sub) {
3094 ir_node *sub_r = get_Sub_right(a);
3095 if (is_Const(sub_r) && is_Const_one(sub_r)) {
3097 ir_node *op = get_Sub_left(a);
3098 ir_node *blk = get_irn_n(n, -1);
3099 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3100 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3104 } /* transform_node_Not */
3107 * Transform a Minus.
3112 static ir_node *transform_node_Minus(ir_node *n) {
3113 ir_node *c, *oldn = n;
3114 ir_node *a = get_Minus_op(n);
3117 HANDLE_UNOP_PHI(tarval_neg,a,c);
3119 mode = get_irn_mode(a);
3120 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3121 /* the following rules are only to twos-complement */
3124 ir_node *op = get_Not_op(a);
3125 tarval *tv = get_mode_one(mode);
3126 ir_node *blk = get_irn_n(n, -1);
3127 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3128 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3129 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3133 ir_node *c = get_Shr_right(a);
3136 tarval *tv = get_Const_tarval(c);
3138 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3139 /* -(a >>u (size-1)) = a >>s (size-1) */
3140 ir_node *v = get_Shr_left(a);
3142 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3143 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3149 ir_node *c = get_Shrs_right(a);
3152 tarval *tv = get_Const_tarval(c);
3154 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3155 /* -(a >>s (size-1)) = a >>u (size-1) */
3156 ir_node *v = get_Shrs_left(a);
3158 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3159 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3166 /* - (a-b) = b - a */
3167 ir_node *la = get_Sub_left(a);
3168 ir_node *ra = get_Sub_right(a);
3169 ir_node *blk = get_irn_n(n, -1);
3171 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3172 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3177 } /* transform_node_Minus */
3180 * Transform a Cast_type(Const) into a new Const_type
3182 static ir_node *transform_node_Cast(ir_node *n) {
3184 ir_node *pred = get_Cast_op(n);
3185 ir_type *tp = get_irn_type(n);
3187 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
3188 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3189 get_Const_tarval(pred), tp);
3190 DBG_OPT_CSTEVAL(oldn, n);
3191 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
3192 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3193 get_SymConst_kind(pred), tp);
3194 DBG_OPT_CSTEVAL(oldn, n);
3198 } /* transform_node_Cast */
3201 * Transform a Proj(Div) with a non-zero value.
3202 * Removes the exceptions and routes the memory to the NoMem node.
3204 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3205 ir_node *div = get_Proj_pred(proj);
3206 ir_node *b = get_Div_right(div);
3207 ir_node *confirm, *res, *new_mem;
3210 if (value_not_zero(b, &confirm)) {
3211 /* div(x, y) && y != 0 */
3212 proj_nr = get_Proj_proj(proj);
3214 case pn_Div_X_regular:
3215 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3217 case pn_Div_X_except:
3218 /* we found an exception handler, remove it */
3219 DBG_OPT_EXC_REM(proj);
3223 res = get_Div_mem(div);
3224 new_mem = get_irg_no_mem(current_ir_graph);
3227 /* This node can only float up to the Confirm block */
3228 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3230 set_irn_pinned(div, op_pin_state_floats);
3231 /* this is a Div without exception, we can remove the memory edge */
3232 set_Div_mem(div, new_mem);
3237 } /* transform_node_Proj_Div */
3240 * Transform a Proj(Mod) with a non-zero value.
3241 * Removes the exceptions and routes the memory to the NoMem node.
3243 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3244 ir_node *mod = get_Proj_pred(proj);
3245 ir_node *b = get_Mod_right(mod);
3246 ir_node *confirm, *res, *new_mem;
3249 if (value_not_zero(b, &confirm)) {
3250 /* mod(x, y) && y != 0 */
3251 proj_nr = get_Proj_proj(proj);
3255 case pn_Mod_X_regular:
3256 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3258 case pn_Mod_X_except:
3259 /* we found an exception handler, remove it */
3260 DBG_OPT_EXC_REM(proj);
3264 res = get_Mod_mem(mod);
3265 new_mem = get_irg_no_mem(current_ir_graph);
3268 /* This node can only float up to the Confirm block */
3269 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3271 set_irn_pinned(mod, op_pin_state_floats);
3272 /* this is a Mod without exception, we can remove the memory edge */
3273 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3276 if (get_Mod_left(mod) == b) {
3277 /* a % a = 0 if a != 0 */
3278 ir_mode *mode = get_irn_mode(proj);
3279 ir_node *res = new_Const(mode, get_mode_null(mode));
3281 DBG_OPT_CSTEVAL(mod, res);
3287 } /* transform_node_Proj_Mod */
3290 * Transform a Proj(DivMod) with a non-zero value.
3291 * Removes the exceptions and routes the memory to the NoMem node.
3293 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3294 ir_node *divmod = get_Proj_pred(proj);
3295 ir_node *b = get_DivMod_right(divmod);
3296 ir_node *confirm, *res, *new_mem;
3299 if (value_not_zero(b, &confirm)) {
3300 /* DivMod(x, y) && y != 0 */
3301 proj_nr = get_Proj_proj(proj);
3305 case pn_DivMod_X_regular:
3306 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3308 case pn_DivMod_X_except:
3309 /* we found an exception handler, remove it */
3310 DBG_OPT_EXC_REM(proj);
3314 res = get_DivMod_mem(divmod);
3315 new_mem = get_irg_no_mem(current_ir_graph);
3318 /* This node can only float up to the Confirm block */
3319 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3321 set_irn_pinned(divmod, op_pin_state_floats);
3322 /* this is a DivMod without exception, we can remove the memory edge */
3323 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3326 case pn_DivMod_res_mod:
3327 if (get_DivMod_left(divmod) == b) {
3328 /* a % a = 0 if a != 0 */
3329 ir_mode *mode = get_irn_mode(proj);
3330 ir_node *res = new_Const(mode, get_mode_null(mode));
3332 DBG_OPT_CSTEVAL(divmod, res);
3338 } /* transform_node_Proj_DivMod */
3341 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3343 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3344 if (get_opt_unreachable_code()) {
3345 ir_node *n = get_Proj_pred(proj);
3346 ir_node *b = get_Cond_selector(n);
3348 if (mode_is_int(get_irn_mode(b))) {
3349 tarval *tb = value_of(b);
3351 if (tb != tarval_bad) {
3352 /* we have a constant switch */
3353 long num = get_Proj_proj(proj);
3355 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3356 if (get_tarval_long(tb) == num) {
3357 /* Do NOT create a jump here, or we will have 2 control flow ops
3358 * in a block. This case is optimized away in optimize_cf(). */
3361 /* this case will NEVER be taken, kill it */
3369 } /* transform_node_Proj_Cond */
3372 * Normalizes and optimizes Cmp nodes.
3374 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3375 ir_node *n = get_Proj_pred(proj);
3376 ir_node *left = get_Cmp_left(n);
3377 ir_node *right = get_Cmp_right(n);
3381 ir_mode *mode = NULL;
3382 long proj_nr = get_Proj_proj(proj);
3384 /* we can evaluate this direct */
3387 return new_Const(mode_b, get_tarval_b_false());
3389 return new_Const(mode_b, get_tarval_b_true());
3391 if(!mode_is_float(get_irn_mode(left)))
3392 return new_Const(mode_b, get_tarval_b_true());
3398 /* Remove unnecessary conversions */
3399 /* TODO handle constants */
3400 if (is_Conv(left) && is_Conv(right)) {
3401 ir_mode *mode = get_irn_mode(left);
3402 ir_node *op_left = get_Conv_op(left);
3403 ir_node *op_right = get_Conv_op(right);
3404 ir_mode *mode_left = get_irn_mode(op_left);
3405 ir_mode *mode_right = get_irn_mode(op_right);
3407 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3408 ir_graph *irg = current_ir_graph;
3409 ir_node *block = get_nodes_block(n);
3411 if (mode_left == mode_right) {
3415 } else if (smaller_mode(mode_left, mode_right)) {
3416 left = new_r_Conv(irg, block, op_left, mode_right);
3419 } else if (smaller_mode(mode_right, mode_left)) {
3421 right = new_r_Conv(irg, block, op_right, mode_left);
3427 /* TODO extend to arbitrary constants */
3428 if (is_Conv(left) && is_Const(right) && is_Const_null(right)) {
3429 ir_mode* mode = get_irn_mode(left);
3430 ir_node* op = get_Conv_op(left);
3431 ir_mode* op_mode = get_irn_mode(op);
3433 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3434 (mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3435 ir_node *null = new_Const(op_mode, get_mode_null(op_mode));
3436 set_Cmp_left( n, op);
3437 set_Cmp_right(n, null);
3444 left = get_Cast_op(left);
3446 right = get_Cast_op(right);
3448 /* remove operation of both sides if possible */
3449 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3450 ir_opcode lop = get_irn_opcode(left);
3452 if (lop == get_irn_opcode(right)) {
3453 ir_node *ll, *lr, *rl, *rr;
3455 /* same operation on both sides, try to remove */
3459 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3460 left = get_unop_op(left);
3461 right = get_unop_op(right);
3465 ll = get_Add_left(left);
3466 lr = get_Add_right(left);
3467 rl = get_Add_left(right);
3468 rr = get_Add_right(right);
3471 /* X + a CMP X + b ==> a CMP b */
3475 } else if (ll == rr) {
3476 /* X + a CMP b + X ==> a CMP b */
3480 } else if (lr == rl) {
3481 /* a + X CMP X + b ==> a CMP b */
3485 } else if (lr == rr) {
3486 /* a + X CMP b + X ==> a CMP b */
3493 ll = get_Sub_left(left);
3494 lr = get_Sub_right(left);
3495 rl = get_Sub_left(right);
3496 rr = get_Sub_right(right);
3499 /* X - a CMP X - b ==> a CMP b */
3503 } else if (lr == rr) {
3504 /* a - X CMP b - X ==> a CMP b */
3511 if (get_Rot_right(left) == get_Rot_right(right)) {
3512 /* a ROT X CMP b ROT X */
3513 left = get_Rot_left(left);
3514 right = get_Rot_left(right);
3524 if (get_irn_mode(left) == mode_b) {
3525 ir_graph *irg = current_ir_graph;
3526 ir_node *block = get_nodes_block(n);
3529 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3530 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3531 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3532 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3533 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3534 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3538 if (!get_opt_reassociation())
3542 * First step: normalize the compare op
3543 * by placing the constant on the right side
3544 * or moving the lower address node to the left.
3545 * We ignore the case that both are constants
3546 * this case should be optimized away.
3548 if (is_Const(right)) {
3550 } else if (is_Const(left)) {
3555 proj_nr = get_inversed_pnc(proj_nr);
3557 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3563 proj_nr = get_inversed_pnc(proj_nr);
3568 * Second step: Try to reduce the magnitude
3569 * of a constant. This may help to generate better code
3570 * later and may help to normalize more compares.
3571 * Of course this is only possible for integer values.
3574 mode = get_irn_mode(c);
3575 tv = get_Const_tarval(c);
3577 if (tv != tarval_bad) {
3578 /* the following optimization is possible on modes without Overflow
3579 * on Unary Minus or on == and !=:
3580 * -a CMP c ==> a swap(CMP) -c
3582 * Beware: for two-complement Overflow may occur, so only == and != can
3583 * be optimized, see this:
3584 * -MININT < 0 =/=> MININT > 0 !!!
3586 if (is_Minus(left) &&
3587 (!mode_overflow_on_unary_Minus(mode) ||
3588 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3589 tv = tarval_neg(tv);
3591 if (tv != tarval_bad) {
3592 left = get_Minus_op(left);
3593 proj_nr = get_inversed_pnc(proj_nr);
3596 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3597 tv = tarval_not(tv);
3599 if (tv != tarval_bad) {
3600 left = get_Not_op(left);
3605 /* for integer modes, we have more */
3606 if (mode_is_int(mode)) {
3607 /* Ne includes Unordered which is not possible on integers.
3608 * However, frontends often use this wrong, so fix it here */
3609 if (proj_nr & pn_Cmp_Uo) {
3610 proj_nr &= ~pn_Cmp_Uo;
3611 set_Proj_proj(proj, proj_nr);
3614 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3615 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3616 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3617 tv = tarval_sub(tv, get_mode_one(mode));
3619 if (tv != tarval_bad) {
3620 proj_nr ^= pn_Cmp_Eq;
3624 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3625 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3626 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3627 tv = tarval_add(tv, get_mode_one(mode));
3629 if (tv != tarval_bad) {
3630 proj_nr ^= pn_Cmp_Eq;
3635 /* the following reassociations work only for == and != */
3636 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3638 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3639 if (tarval_is_null(tv) && is_Sub(left)) {
3640 right =get_Sub_right(left);
3641 left = get_Sub_left(left);
3643 tv = value_of(right);
3647 if (tv != tarval_bad) {
3648 ir_op *op = get_irn_op(left);
3650 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3652 ir_node *c1 = get_Sub_right(left);
3653 tarval *tv2 = value_of(c1);
3655 if (tv2 != tarval_bad) {
3656 tv2 = tarval_add(tv, value_of(c1));
3658 if (tv2 != tarval_bad) {
3659 left = get_Sub_left(left);
3665 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3666 else if (op == op_Add) {
3667 ir_node *a_l = get_Add_left(left);
3668 ir_node *a_r = get_Add_right(left);
3672 if (get_irn_op(a_l) == op_Const) {
3674 tv2 = value_of(a_l);
3677 tv2 = value_of(a_r);
3680 if (tv2 != tarval_bad) {
3681 tv2 = tarval_sub(tv, tv2);
3683 if (tv2 != tarval_bad) {
3690 /* -a == c ==> a == -c, -a != c ==> a != -c */
3691 else if (op == op_Minus) {
3692 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3694 if (tv2 != tarval_bad) {
3695 left = get_Minus_op(left);
3702 /* the following reassociations work only for <= */
3703 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3704 if (tv != tarval_bad) {
3705 ir_op *op = get_irn_op(left);
3707 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3715 * optimization for AND:
3717 * And(x, C) == C ==> And(x, C) != 0
3718 * And(x, C) != C ==> And(X, C) == 0
3720 * if C is a single Bit constant.
3722 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3723 if (tarval_is_single_bit(tv)) {
3724 /* check for Constant's match. We have check hare the tarvals,
3725 because our const might be changed */
3726 ir_node *la = get_And_left(left);
3727 ir_node *ra = get_And_right(left);
3728 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3729 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3730 /* fine: do the transformation */
3731 tv = get_mode_null(get_tarval_mode(tv));
3732 proj_nr ^= pn_Cmp_Leg;
3737 } /* tarval != bad */
3741 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3743 if (changed & 2) /* need a new Const */
3744 right = new_Const(mode, tv);
3746 /* create a new compare */
3747 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3749 set_Proj_pred(proj, n);
3750 set_Proj_proj(proj, proj_nr);
3754 } /* transform_node_Proj_Cmp */
3757 * Does all optimizations on nodes that must be done on it's Proj's
3758 * because of creating new nodes.
3760 static ir_node *transform_node_Proj(ir_node *proj) {
3761 ir_node *n = get_Proj_pred(proj);
3763 switch (get_irn_opcode(n)) {
3765 return transform_node_Proj_Div(proj);
3768 return transform_node_Proj_Mod(proj);
3771 return transform_node_Proj_DivMod(proj);
3774 return transform_node_Proj_Cond(proj);
3777 return transform_node_Proj_Cmp(proj);
3780 /* should not happen, but if it does will be optimized away */
3781 return equivalent_node_Proj(proj);
3787 } /* transform_node_Proj */
3790 * Move Confirms down through Phi nodes.
3792 static ir_node *transform_node_Phi(ir_node *phi) {
3794 ir_mode *mode = get_irn_mode(phi);
3796 if (mode_is_reference(mode)) {
3797 n = get_irn_arity(phi);
3799 /* Beware of Phi0 */
3801 ir_node *pred = get_irn_n(phi, 0);
3802 ir_node *bound, *new_Phi, *block, **in;
3805 if (! is_Confirm(pred))
3808 bound = get_Confirm_bound(pred);
3809 pnc = get_Confirm_cmp(pred);
3811 NEW_ARR_A(ir_node *, in, n);
3812 in[0] = get_Confirm_value(pred);
3814 for (i = 1; i < n; ++i) {
3815 pred = get_irn_n(phi, i);
3817 if (! is_Confirm(pred) ||
3818 get_Confirm_bound(pred) != bound ||
3819 get_Confirm_cmp(pred) != pnc)
3821 in[i] = get_Confirm_value(pred);
3823 /* move the Confirm nodes "behind" the Phi */
3824 block = get_irn_n(phi, -1);
3825 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3826 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3830 } /* transform_node_Phi */
3833 * Returns the operands of a commutative bin-op, if one operand is
3834 * a const, it is returned as the second one.
3836 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3837 ir_node *op_a = get_binop_left(binop);
3838 ir_node *op_b = get_binop_right(binop);
3840 assert(is_op_commutative(get_irn_op(binop)));
3842 if (get_irn_op(op_a) == op_Const) {
3849 } /* get_comm_Binop_Ops */
3852 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3853 * Such pattern may arise in bitfield stores.
3855 * value c4 value c4 & c2
3856 * AND c3 AND c1 | c3
3863 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3866 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3869 ir_node *and_l, *c3;
3870 ir_node *value, *c4;
3871 ir_node *new_and, *new_const, *block;
3872 ir_mode *mode = get_irn_mode(or);
3874 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3877 get_comm_Binop_Ops(or, &and, &c1);
3878 if (!is_Const(c1) || !is_And(and))
3881 get_comm_Binop_Ops(and, &or_l, &c2);
3885 tv1 = get_Const_tarval(c1);
3886 tv2 = get_Const_tarval(c2);
3888 tv = tarval_or(tv1, tv2);
3889 if (tarval_is_all_one(tv)) {
3890 /* the AND does NOT clear a bit with isn't set by the OR */
3891 set_Or_left(or, or_l);
3892 set_Or_right(or, c1);
3894 /* check for more */
3901 get_comm_Binop_Ops(or_l, &and_l, &c3);
3902 if (!is_Const(c3) || !is_And(and_l))
3905 get_comm_Binop_Ops(and_l, &value, &c4);
3909 /* ok, found the pattern, check for conditions */
3910 assert(mode == get_irn_mode(and));
3911 assert(mode == get_irn_mode(or_l));
3912 assert(mode == get_irn_mode(and_l));
3914 tv3 = get_Const_tarval(c3);
3915 tv4 = get_Const_tarval(c4);
3917 tv = tarval_or(tv4, tv2);
3918 if (!tarval_is_all_one(tv)) {
3919 /* have at least one 0 at the same bit position */
3923 n_tv4 = tarval_not(tv4);
3924 if (tv3 != tarval_and(tv3, n_tv4)) {
3925 /* bit in the or_mask is outside the and_mask */
3929 n_tv2 = tarval_not(tv2);
3930 if (tv1 != tarval_and(tv1, n_tv2)) {
3931 /* bit in the or_mask is outside the and_mask */
3935 /* ok, all conditions met */
3936 block = get_irn_n(or, -1);
3938 new_and = new_r_And(current_ir_graph, block,
3939 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3941 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3943 set_Or_left(or, new_and);
3944 set_Or_right(or, new_const);
3946 /* check for more */
3948 } /* transform_node_Or_bf_store */
3951 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3953 static ir_node *transform_node_Or_Rot(ir_node *or) {
3954 ir_mode *mode = get_irn_mode(or);
3955 ir_node *shl, *shr, *block;
3956 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3959 if (! mode_is_int(mode))
3962 shl = get_binop_left(or);
3963 shr = get_binop_right(or);
3965 if (get_irn_op(shl) == op_Shr) {
3966 if (get_irn_op(shr) != op_Shl)
3972 } else if (get_irn_op(shl) != op_Shl) {
3974 } else if (get_irn_op(shr) != op_Shr) {
3977 x = get_Shl_left(shl);
3978 if (x != get_Shr_left(shr))
3981 c1 = get_Shl_right(shl);
3982 c2 = get_Shr_right(shr);
3983 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3984 tv1 = get_Const_tarval(c1);
3985 if (! tarval_is_long(tv1))
3988 tv2 = get_Const_tarval(c2);
3989 if (! tarval_is_long(tv2))
3992 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3993 != get_mode_size_bits(mode))
3996 /* yet, condition met */
3997 block = get_irn_n(or, -1);
3999 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4001 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4003 } else if (get_irn_op(c1) == op_Sub) {
4007 if (get_Sub_right(sub) != v)
4010 c1 = get_Sub_left(sub);
4011 if (get_irn_op(c1) != op_Const)
4014 tv1 = get_Const_tarval(c1);
4015 if (! tarval_is_long(tv1))
4018 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4021 /* yet, condition met */
4022 block = get_nodes_block(or);
4024 /* a Rot right is not supported, so use a rot left */
4025 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4027 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4029 } else if (get_irn_op(c2) == op_Sub) {
4033 c1 = get_Sub_left(sub);
4034 if (get_irn_op(c1) != op_Const)
4037 tv1 = get_Const_tarval(c1);
4038 if (! tarval_is_long(tv1))
4041 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4044 /* yet, condition met */
4045 block = get_irn_n(or, -1);
4048 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4050 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4055 } /* transform_node_Or_Rot */
4060 static ir_node *transform_node_Or(ir_node *n) {
4061 ir_node *c, *oldn = n;
4062 ir_node *a = get_Or_left(n);
4063 ir_node *b = get_Or_right(n);
4065 if (is_Not(a) && is_Not(b)) {
4066 /* ~a | ~b = ~(a&b) */
4067 ir_node *block = get_nodes_block(n);
4068 ir_mode *mode = get_irn_mode(n);
4072 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4073 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4074 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4078 /* we can evaluate 2 Projs of the same Cmp */
4079 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4080 ir_node *pred_a = get_Proj_pred(a);
4081 ir_node *pred_b = get_Proj_pred(b);
4082 if (pred_a == pred_b) {
4083 dbg_info *dbgi = get_irn_dbg_info(n);
4084 ir_node *block = get_nodes_block(pred_a);
4085 pn_Cmp pn_a = get_Proj_proj(a);
4086 pn_Cmp pn_b = get_Proj_proj(b);
4087 /* yes, we can simply calculate with pncs */
4088 pn_Cmp new_pnc = pn_a | pn_b;
4090 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4095 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4097 n = transform_node_Or_bf_store(n);
4098 n = transform_node_Or_Rot(n);
4102 n = transform_bitwise_distributive(n, transform_node_Or);
4105 } /* transform_node_Or */
4109 static ir_node *transform_node(ir_node *n);
4112 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4114 * Should be moved to reassociation?
4116 static ir_node *transform_node_shift(ir_node *n) {
4117 ir_node *left, *right;
4118 tarval *tv1, *tv2, *res;
4120 int modulo_shf, flag;
4122 left = get_binop_left(n);
4124 /* different operations */
4125 if (get_irn_op(left) != get_irn_op(n))
4128 right = get_binop_right(n);
4129 tv1 = value_of(right);
4130 if (tv1 == tarval_bad)
4133 tv2 = value_of(get_binop_right(left));
4134 if (tv2 == tarval_bad)
4137 res = tarval_add(tv1, tv2);
4139 /* beware: a simple replacement works only, if res < modulo shift */
4140 mode = get_irn_mode(n);
4144 modulo_shf = get_mode_modulo_shift(mode);
4145 if (modulo_shf > 0) {
4146 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4148 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4154 /* ok, we can replace it */
4155 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4157 in[0] = get_binop_left(left);
4158 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4160 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4162 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4164 return transform_node(irn);
4167 } /* transform_node_shift */
4172 static ir_node *transform_node_Shr(ir_node *n) {
4173 ir_node *c, *oldn = n;
4174 ir_node *a = get_Shr_left(n);
4175 ir_node *b = get_Shr_right(n);
4177 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4178 return transform_node_shift(n);
4179 } /* transform_node_Shr */
4184 static ir_node *transform_node_Shrs(ir_node *n) {
4185 ir_node *c, *oldn = n;
4186 ir_node *a = get_Shrs_left(n);
4187 ir_node *b = get_Shrs_right(n);
4189 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4190 return transform_node_shift(n);
4191 } /* transform_node_Shrs */
4196 static ir_node *transform_node_Shl(ir_node *n) {
4197 ir_node *c, *oldn = n;
4198 ir_node *a = get_Shl_left(n);
4199 ir_node *b = get_Shl_right(n);
4201 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4202 return transform_node_shift(n);
4203 } /* transform_node_Shl */
4206 * Remove dead blocks and nodes in dead blocks
4207 * in keep alive list. We do not generate a new End node.
4209 static ir_node *transform_node_End(ir_node *n) {
4210 int i, j, n_keepalives = get_End_n_keepalives(n);
4213 NEW_ARR_A(ir_node *, in, n_keepalives);
4215 for (i = j = 0; i < n_keepalives; ++i) {
4216 ir_node *ka = get_End_keepalive(n, i);
4218 if (! is_Block_dead(ka)) {
4222 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4225 /* FIXME: beabi need to keep a Proj(M) */
4226 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4229 if (j != n_keepalives)
4230 set_End_keepalives(n, j, in);
4232 } /* transform_node_End */
4234 /** returns 1 if a == -b */
4235 static int is_negated_value(ir_node *a, ir_node *b) {
4236 if(is_Minus(a) && get_Minus_op(a) == b)
4238 if(is_Minus(b) && get_Minus_op(b) == a)
4240 if(is_Sub(a) && is_Sub(b)) {
4241 ir_node *a_left = get_Sub_left(a);
4242 ir_node *a_right = get_Sub_right(a);
4243 ir_node *b_left = get_Sub_left(b);
4244 ir_node *b_right = get_Sub_right(b);
4246 if(a_left == b_right && a_right == b_left)
4254 * Optimize a Mux into some simpler cases.
4256 static ir_node *transform_node_Mux(ir_node *n) {
4257 ir_node *oldn = n, *sel = get_Mux_sel(n);
4258 ir_mode *mode = get_irn_mode(n);
4260 if (mode == mode_b) {
4261 ir_node *t = get_Mux_true(n);
4262 ir_node *f = get_Mux_false(n);
4263 dbg_info *dbg = get_irn_dbg_info(n);
4264 ir_node *block = get_irn_n(n, -1);
4265 ir_graph *irg = current_ir_graph;
4268 tarval *tv_t = get_Const_tarval(t);
4269 if (tv_t == tarval_b_true) {
4271 assert(get_Const_tarval(f) == tarval_b_false);
4274 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4277 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4278 assert(tv_t == tarval_b_false);
4280 assert(get_Const_tarval(f) == tarval_b_true);
4283 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4286 } else if (is_Const(f)) {
4287 tarval *tv_f = get_Const_tarval(f);
4288 if (tv_f == tarval_b_true) {
4289 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4290 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4292 assert(tv_f == tarval_b_false);
4293 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4298 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
4299 ir_node *cmp = get_Proj_pred(sel);
4300 long pn = get_Proj_proj(sel);
4301 ir_node *f = get_Mux_false(n);
4302 ir_node *t = get_Mux_true(n);
4305 * Note: normalization puts the constant on the right side,
4306 * so we check only one case.
4308 * Note further that these optimization work even for floating point
4309 * with NaN's because -NaN == NaN.
4310 * However, if +0 and -0 is handled differently, we cannot use the first
4314 ir_node *cmp_r = get_Cmp_right(cmp);
4315 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4316 ir_node *block = get_irn_n(n, -1);
4318 if(is_negated_value(f, t)) {
4319 ir_node *cmp_left = get_Cmp_left(cmp);
4321 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4322 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4323 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4325 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4327 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4329 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4330 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4331 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4333 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4335 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4337 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4342 if (mode_is_int(mode) && mode_is_signed(mode) &&
4343 get_mode_arithmetic(mode) == irma_twos_complement) {
4344 ir_node *x = get_Cmp_left(cmp);
4346 /* the following optimization works only with signed integer two-complement mode */
4348 if (mode == get_irn_mode(x)) {
4350 * FIXME: this restriction is two rigid, as it would still
4351 * work if mode(x) = Hs and mode == Is, but at least it removes
4354 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4355 is_Const(t) && is_Const_all_one(t) &&
4356 is_Const(f) && is_Const_null(f)) {
4358 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4362 n = new_rd_Shrs(get_irn_dbg_info(n),
4363 current_ir_graph, block, x,
4364 new_r_Const_long(current_ir_graph, block, mode_Iu,
4365 get_mode_size_bits(mode) - 1),
4367 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4369 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4370 is_Const(t) && is_Const_one(t) &&
4371 is_Const(f) && is_Const_null(f)) {
4373 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4377 n = new_rd_Shr(get_irn_dbg_info(n),
4378 current_ir_graph, block,
4379 new_r_Minus(current_ir_graph, block, x, mode),
4380 new_r_Const_long(current_ir_graph, block, mode_Iu,
4381 get_mode_size_bits(mode) - 1),
4383 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4391 return arch_transform_node_Mux(n);
4392 } /* transform_node_Mux */
4395 * Optimize a Psi into some simpler cases.
4397 static ir_node *transform_node_Psi(ir_node *n) {
4399 return transform_node_Mux(n);
4402 } /* transform_node_Psi */
4405 * Tries several [inplace] [optimizing] transformations and returns an
4406 * equivalent node. The difference to equivalent_node() is that these
4407 * transformations _do_ generate new nodes, and thus the old node must
4408 * not be freed even if the equivalent node isn't the old one.
4410 static ir_node *transform_node(ir_node *n) {
4414 * Transform_node is the only "optimizing transformation" that might
4415 * return a node with a different opcode. We iterate HERE until fixpoint
4416 * to get the final result.
4420 if (n->op->ops.transform_node)
4421 n = n->op->ops.transform_node(n);
4422 } while (oldn != n);
4425 } /* transform_node */
4428 * Sets the default transform node operation for an ir_op_ops.
4430 * @param code the opcode for the default operation
4431 * @param ops the operations initialized
4436 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4440 ops->transform_node = transform_node_##a; \
4474 } /* firm_set_default_transform_node */
4477 /* **************** Common Subexpression Elimination **************** */
4479 /** The size of the hash table used, should estimate the number of nodes
4481 #define N_IR_NODES 512
4483 /** Compares the attributes of two Const nodes. */
4484 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4485 return (get_Const_tarval(a) != get_Const_tarval(b))
4486 || (get_Const_type(a) != get_Const_type(b));
4487 } /* node_cmp_attr_Const */
4489 /** Compares the attributes of two Proj nodes. */
4490 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4491 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4492 } /* node_cmp_attr_Proj */
4494 /** Compares the attributes of two Filter nodes. */
4495 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4496 return get_Filter_proj(a) != get_Filter_proj(b);
4497 } /* node_cmp_attr_Filter */
4499 /** Compares the attributes of two Alloc nodes. */
4500 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4501 const alloc_attr *pa = get_irn_alloc_attr(a);
4502 const alloc_attr *pb = get_irn_alloc_attr(b);
4503 return (pa->where != pb->where) || (pa->type != pb->type);
4504 } /* node_cmp_attr_Alloc */
4506 /** Compares the attributes of two Free nodes. */
4507 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4508 const free_attr *pa = get_irn_free_attr(a);
4509 const free_attr *pb = get_irn_free_attr(b);
4510 return (pa->where != pb->where) || (pa->type != pb->type);
4511 } /* node_cmp_attr_Free */
4513 /** Compares the attributes of two SymConst nodes. */
4514 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4515 const symconst_attr *pa = get_irn_symconst_attr(a);
4516 const symconst_attr *pb = get_irn_symconst_attr(b);
4517 return (pa->num != pb->num)
4518 || (pa->sym.type_p != pb->sym.type_p)
4519 || (pa->tp != pb->tp);
4520 } /* node_cmp_attr_SymConst */
4522 /** Compares the attributes of two Call nodes. */
4523 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4524 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4525 } /* node_cmp_attr_Call */
4527 /** Compares the attributes of two Sel nodes. */
4528 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4529 const ir_entity *a_ent = get_Sel_entity(a);
4530 const ir_entity *b_ent = get_Sel_entity(b);
4532 (a_ent->kind != b_ent->kind) ||
4533 (a_ent->name != b_ent->name) ||
4534 (a_ent->owner != b_ent->owner) ||
4535 (a_ent->ld_name != b_ent->ld_name) ||
4536 (a_ent->type != b_ent->type);
4537 } /* node_cmp_attr_Sel */
4539 /** Compares the attributes of two Phi nodes. */
4540 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4541 /* we can only enter this function if both nodes have the same number of inputs,
4542 hence it is enough to check if one of them is a Phi0 */
4544 /* check the Phi0 attribute */
4545 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4548 } /* node_cmp_attr_Phi */
4550 /** Compares the attributes of two Conv nodes. */
4551 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4552 return get_Conv_strict(a) != get_Conv_strict(b);
4553 } /* node_cmp_attr_Conv */
4555 /** Compares the attributes of two Cast nodes. */
4556 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4557 return get_Cast_type(a) != get_Cast_type(b);
4558 } /* node_cmp_attr_Cast */
4560 /** Compares the attributes of two Load nodes. */
4561 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4562 if (get_Load_volatility(a) == volatility_is_volatile ||
4563 get_Load_volatility(b) == volatility_is_volatile)
4564 /* NEVER do CSE on volatile Loads */
4566 /* do not CSE Loads with different alignment. Be conservative. */
4567 if (get_Load_align(a) != get_Load_align(b))
4570 return get_Load_mode(a) != get_Load_mode(b);
4571 } /* node_cmp_attr_Load */
4573 /** Compares the attributes of two Store nodes. */
4574 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4575 /* do not CSE Stores with different alignment. Be conservative. */
4576 if (get_Store_align(a) != get_Store_align(b))
4579 /* NEVER do CSE on volatile Stores */
4580 return (get_Store_volatility(a) == volatility_is_volatile ||
4581 get_Store_volatility(b) == volatility_is_volatile);
4582 } /* node_cmp_attr_Store */
4584 /** Compares the attributes of two Confirm nodes. */
4585 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4586 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4587 } /* node_cmp_attr_Confirm */
4589 /** Compares the attributes of two ASM nodes. */
4590 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4592 const ir_asm_constraint *ca;
4593 const ir_asm_constraint *cb;
4596 if (get_ASM_text(a) != get_ASM_text(b))
4599 /* Should we really check the constraints here? Should be better, but is strange. */
4600 n = get_ASM_n_input_constraints(a);
4601 if (n != get_ASM_n_input_constraints(b))
4604 ca = get_ASM_input_constraints(a);
4605 cb = get_ASM_input_constraints(b);
4606 for (i = 0; i < n; ++i) {
4607 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4611 n = get_ASM_n_output_constraints(a);
4612 if (n != get_ASM_n_output_constraints(b))
4615 ca = get_ASM_output_constraints(a);
4616 cb = get_ASM_output_constraints(b);
4617 for (i = 0; i < n; ++i) {
4618 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4622 n = get_ASM_n_clobbers(a);
4623 if (n != get_ASM_n_clobbers(b))
4626 cla = get_ASM_clobbers(a);
4627 clb = get_ASM_clobbers(b);
4628 for (i = 0; i < n; ++i) {
4629 if (cla[i] != clb[i])
4633 } /* node_cmp_attr_ASM */
4636 * Set the default node attribute compare operation for an ir_op_ops.
4638 * @param code the opcode for the default operation
4639 * @param ops the operations initialized
4644 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4648 ops->node_cmp_attr = node_cmp_attr_##a; \
4673 } /* firm_set_default_node_cmp_attr */
4676 * Compare function for two nodes in the hash table. Gets two
4677 * nodes as parameters. Returns 0 if the nodes are a cse.
4679 int identities_cmp(const void *elt, const void *key) {
4686 if (a == b) return 0;
4688 if ((get_irn_op(a) != get_irn_op(b)) ||
4689 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4691 /* compare if a's in and b's in are of equal length */
4692 irn_arity_a = get_irn_intra_arity (a);
4693 if (irn_arity_a != get_irn_intra_arity(b))
4696 /* for block-local cse and op_pin_state_pinned nodes: */
4697 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4698 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4702 /* compare a->in[0..ins] with b->in[0..ins] */
4703 for (i = 0; i < irn_arity_a; i++)
4704 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4708 * here, we already now that the nodes are identical except their
4711 if (a->op->ops.node_cmp_attr)
4712 return a->op->ops.node_cmp_attr(a, b);
4715 } /* identities_cmp */
4718 * Calculate a hash value of a node.
4720 unsigned ir_node_hash(ir_node *node) {
4724 if (node->op == op_Const) {
4725 /* special value for const, as they only differ in their tarval. */
4726 h = HASH_PTR(node->attr.con.tv);
4727 h = 9*h + HASH_PTR(get_irn_mode(node));
4728 } else if (node->op == op_SymConst) {
4729 /* special value for const, as they only differ in their symbol. */
4730 h = HASH_PTR(node->attr.symc.sym.type_p);
4731 h = 9*h + HASH_PTR(get_irn_mode(node));
4734 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4735 h = irn_arity = get_irn_intra_arity(node);
4737 /* consider all in nodes... except the block if not a control flow. */
4738 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4739 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4743 h = 9*h + HASH_PTR(get_irn_mode(node));
4745 h = 9*h + HASH_PTR(get_irn_op(node));
4749 } /* ir_node_hash */
4751 pset *new_identities(void) {
4752 return new_pset(identities_cmp, N_IR_NODES);
4753 } /* new_identities */
4755 void del_identities(pset *value_table) {
4756 del_pset(value_table);
4757 } /* del_identities */
4760 * Normalize a node by putting constants (and operands with smaller
4761 * node index) on the right
4763 * @param n The node to normalize
4765 static void normalize_node(ir_node *n) {
4766 if (get_opt_reassociation()) {
4767 if (is_op_commutative(get_irn_op(n))) {
4768 ir_node *l = get_binop_left(n);
4769 ir_node *r = get_binop_right(n);
4770 int l_idx = get_irn_idx(l);
4771 int r_idx = get_irn_idx(r);
4773 /* For commutative operators perform a OP b == b OP a but keep
4774 constants on the RIGHT side. This helps greatly in some optimizations.
4775 Moreover we use the idx number to make the form deterministic. */
4776 if (is_irn_constlike(l))
4778 if (is_irn_constlike(r))
4780 if (l_idx < r_idx) {
4781 set_binop_left(n, r);
4782 set_binop_right(n, l);
4786 } /* normalize_node */
4789 * Return the canonical node computing the same value as n.
4791 * @param value_table The value table
4792 * @param n The node to lookup
4794 * Looks up the node in a hash table.
4796 * For Const nodes this is performed in the constructor, too. Const
4797 * nodes are extremely time critical because of their frequent use in
4798 * constant string arrays.
4800 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4803 if (!value_table) return n;
4807 o = pset_find(value_table, n, ir_node_hash(n));
4816 * During construction we set the op_pin_state_pinned flag in the graph right when the
4817 * optimization is performed. The flag turning on procedure global cse could
4818 * be changed between two allocations. This way we are safe.
4820 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4823 n = identify(value_table, n);
4824 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4825 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4827 } /* identify_cons */
4830 * Return the canonical node computing the same value as n.
4831 * Looks up the node in a hash table, enters it in the table
4832 * if it isn't there yet.
4834 ir_node *identify_remember(pset *value_table, ir_node *n) {
4837 if (!value_table) return n;
4840 /* lookup or insert in hash table with given hash key. */
4841 o = pset_insert(value_table, n, ir_node_hash(n));
4848 } /* identify_remember */
4850 /* Add a node to the identities value table. */
4851 void add_identities(pset *value_table, ir_node *node) {
4852 if (get_opt_cse() && is_no_Block(node))
4853 identify_remember(value_table, node);
4854 } /* add_identities */
4856 /* Visit each node in the value table of a graph. */
4857 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4859 ir_graph *rem = current_ir_graph;
4861 current_ir_graph = irg;
4862 foreach_pset(irg->value_table, node)
4864 current_ir_graph = rem;
4865 } /* visit_all_identities */
4868 * Garbage in, garbage out. If a node has a dead input, i.e., the
4869 * Bad node is input to the node, return the Bad node.
4871 static ir_node *gigo(ir_node *node) {
4873 ir_op *op = get_irn_op(node);
4875 /* remove garbage blocks by looking at control flow that leaves the block
4876 and replacing the control flow by Bad. */
4877 if (get_irn_mode(node) == mode_X) {
4878 ir_node *block = get_nodes_block(skip_Proj(node));
4880 /* Don't optimize nodes in immature blocks. */
4881 if (!get_Block_matured(block)) return node;
4882 /* Don't optimize End, may have Bads. */
4883 if (op == op_End) return node;
4885 if (is_Block(block)) {
4886 irn_arity = get_irn_arity(block);
4887 for (i = 0; i < irn_arity; i++) {
4888 if (!is_Bad(get_irn_n(block, i)))
4891 if (i == irn_arity) {
4892 ir_graph *irg = get_irn_irg(block);
4893 /* the start block is never dead */
4894 if (block != get_irg_start_block(irg)
4895 && block != get_irg_end_block(irg))
4901 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4902 blocks predecessors is dead. */
4903 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4904 irn_arity = get_irn_arity(node);
4907 * Beware: we can only read the block of a non-floating node.
4909 if (is_irn_pinned_in_irg(node) &&
4910 is_Block_dead(get_nodes_block(node)))
4913 for (i = 0; i < irn_arity; i++) {
4914 ir_node *pred = get_irn_n(node, i);
4919 /* Propagating Unknowns here seems to be a bad idea, because
4920 sometimes we need a node as a input and did not want that
4922 However, it might be useful to move this into a later phase
4923 (if you think that optimizing such code is useful). */
4924 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4925 return new_Unknown(get_irn_mode(node));
4930 /* With this code we violate the agreement that local_optimize
4931 only leaves Bads in Block, Phi and Tuple nodes. */
4932 /* If Block has only Bads as predecessors it's garbage. */
4933 /* If Phi has only Bads as predecessors it's garbage. */
4934 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4935 irn_arity = get_irn_arity(node);
4936 for (i = 0; i < irn_arity; i++) {
4937 if (!is_Bad(get_irn_n(node, i))) break;
4939 if (i == irn_arity) node = new_Bad();
4946 * These optimizations deallocate nodes from the obstack.
4947 * It can only be called if it is guaranteed that no other nodes
4948 * reference this one, i.e., right after construction of a node.
4950 * @param n The node to optimize
4952 * current_ir_graph must be set to the graph of the node!
4954 ir_node *optimize_node(ir_node *n) {
4957 ir_opcode iro = get_irn_opcode(n);
4959 /* Always optimize Phi nodes: part of the construction. */
4960 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4962 /* constant expression evaluation / constant folding */
4963 if (get_opt_constant_folding()) {
4964 /* neither constants nor Tuple values can be evaluated */
4965 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4966 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4967 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4968 /* try to evaluate */
4969 tv = computed_value(n);
4970 if (tv != tarval_bad) {
4972 ir_type *old_tp = get_irn_type(n);
4973 int i, arity = get_irn_arity(n);
4977 * Try to recover the type of the new expression.
4979 for (i = 0; i < arity && !old_tp; ++i)
4980 old_tp = get_irn_type(get_irn_n(n, i));
4983 * we MUST copy the node here temporary, because it's still needed
4984 * for DBG_OPT_CSTEVAL
4986 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4987 oldn = alloca(node_size);
4989 memcpy(oldn, n, node_size);
4990 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4992 /* ARG, copy the in array, we need it for statistics */
4993 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4995 /* note the inplace edges module */
4996 edges_node_deleted(n, current_ir_graph);
4998 /* evaluation was successful -- replace the node. */
4999 irg_kill_node(current_ir_graph, n);
5000 nw = new_Const(get_tarval_mode(tv), tv);
5002 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5003 set_Const_type(nw, old_tp);
5004 DBG_OPT_CSTEVAL(oldn, nw);
5005 tarval_enable_fp_ops(old_fp_mode);
5008 tarval_enable_fp_ops(old_fp_mode);
5012 /* remove unnecessary nodes */
5013 if (get_opt_constant_folding() ||
5014 (iro == iro_Phi) || /* always optimize these nodes. */
5016 (iro == iro_Proj) ||
5017 (iro == iro_Block) ) /* Flags tested local. */
5018 n = equivalent_node(n);
5020 /* Common Subexpression Elimination.
5022 * Checks whether n is already available.
5023 * The block input is used to distinguish different subexpressions. Right
5024 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5025 * subexpressions within a block.
5028 n = identify_cons(current_ir_graph->value_table, n);
5031 edges_node_deleted(oldn, current_ir_graph);
5033 /* We found an existing, better node, so we can deallocate the old node. */
5034 irg_kill_node(current_ir_graph, oldn);
5038 /* Some more constant expression evaluation that does not allow to
5040 iro = get_irn_opcode(n);
5041 if (get_opt_constant_folding() ||
5042 (iro == iro_Cond) ||
5043 (iro == iro_Proj)) /* Flags tested local. */
5044 n = transform_node(n);
5046 /* Remove nodes with dead (Bad) input.
5047 Run always for transformation induced Bads. */
5050 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5051 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5052 n = identify_remember(current_ir_graph->value_table, n);
5056 } /* optimize_node */
5060 * These optimizations never deallocate nodes (in place). This can cause dead
5061 * nodes lying on the obstack. Remove these by a dead node elimination,
5062 * i.e., a copying garbage collection.
5064 ir_node *optimize_in_place_2(ir_node *n) {
5067 ir_opcode iro = get_irn_opcode(n);
5069 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
5071 /* constant expression evaluation / constant folding */
5072 if (get_opt_constant_folding()) {
5073 /* neither constants nor Tuple values can be evaluated */
5074 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5075 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5076 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5077 /* try to evaluate */
5078 tv = computed_value(n);
5079 if (tv != tarval_bad) {
5080 /* evaluation was successful -- replace the node. */
5081 ir_type *old_tp = get_irn_type(n);
5082 int i, arity = get_irn_arity(n);
5085 * Try to recover the type of the new expression.
5087 for (i = 0; i < arity && !old_tp; ++i)
5088 old_tp = get_irn_type(get_irn_n(n, i));
5090 n = new_Const(get_tarval_mode(tv), tv);
5092 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5093 set_Const_type(n, old_tp);
5095 DBG_OPT_CSTEVAL(oldn, n);
5096 tarval_enable_fp_ops(old_fp_mode);
5099 tarval_enable_fp_ops(old_fp_mode);
5103 /* remove unnecessary nodes */
5104 if (get_opt_constant_folding() ||
5105 (iro == iro_Phi) || /* always optimize these nodes. */
5106 (iro == iro_Id) || /* ... */
5107 (iro == iro_Proj) || /* ... */
5108 (iro == iro_Block) ) /* Flags tested local. */
5109 n = equivalent_node(n);
5111 /** common subexpression elimination **/
5112 /* Checks whether n is already available. */
5113 /* The block input is used to distinguish different subexpressions. Right
5114 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5115 subexpressions within a block. */
5116 if (get_opt_cse()) {
5117 n = identify(current_ir_graph->value_table, n);
5120 /* Some more constant expression evaluation. */
5121 iro = get_irn_opcode(n);
5122 if (get_opt_constant_folding() ||
5123 (iro == iro_Cond) ||
5124 (iro == iro_Proj)) /* Flags tested local. */
5125 n = transform_node(n);
5127 /* Remove nodes with dead (Bad) input.
5128 Run always for transformation induced Bads. */
5131 /* Now we can verify the node, as it has no dead inputs any more. */
5134 /* Now we have a legal, useful node. Enter it in hash table for cse.
5135 Blocks should be unique anyways. (Except the successor of start:
5136 is cse with the start block!) */
5137 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5138 n = identify_remember(current_ir_graph->value_table, n);
5141 } /* optimize_in_place_2 */
5144 * Wrapper for external use, set proper status bits after optimization.
5146 ir_node *optimize_in_place(ir_node *n) {
5147 /* Handle graph state */
5148 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5150 if (get_opt_global_cse())
5151 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5152 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5153 set_irg_outs_inconsistent(current_ir_graph);
5155 /* FIXME: Maybe we could also test whether optimizing the node can
5156 change the control graph. */
5157 set_irg_doms_inconsistent(current_ir_graph);
5158 return optimize_in_place_2(n);
5159 } /* optimize_in_place */
5162 * Sets the default operation for an ir_ops.
5164 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5165 ops = firm_set_default_computed_value(code, ops);
5166 ops = firm_set_default_equivalent_node(code, ops);
5167 ops = firm_set_default_transform_node(code, ops);
5168 ops = firm_set_default_node_cmp_attr(code, ops);
5169 ops = firm_set_default_get_type(code, ops);
5170 ops = firm_set_default_get_type_attr(code, ops);
5171 ops = firm_set_default_get_entity_attr(code, ops);
5174 } /* firm_set_default_operations */