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 (tarval_is_null(value_of(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 (tarval_is_null(value_of(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 (tarval_is_one(value_of(a))) {
1015 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1016 } else if (tarval_is_one(value_of(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 (tarval_is_one(value_of(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 (tarval_is_one(value_of(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 (tarval_is_one(value_of(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 (tarval_is_null(value_of(a))) {
1104 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1105 } else if (tarval_is_null(value_of(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 (tarval_is_all_one(value_of(a))) {
1129 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1132 if (tarval_is_all_one(value_of(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_Minus(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_Minus(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_Minus(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_Minus(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 && tarval_is_one(value_of(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 && tarval_is_one(value_of(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_mode(a) == mode_b)
3087 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3088 /* We negate a Cmp. The Cmp has the negated result anyways! */
3089 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3090 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3091 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3094 if (op_a == op_Sub) {
3095 ir_node *sub_r = get_Sub_right(a);
3096 if (is_Const(sub_r) && is_Const_one(sub_r)) {
3098 ir_node *op = get_Sub_left(a);
3099 ir_node *blk = get_irn_n(n, -1);
3100 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3105 } /* transform_node_Not */
3108 * Transform a Minus.
3113 static ir_node *transform_node_Minus(ir_node *n) {
3114 ir_node *c, *oldn = n;
3115 ir_node *a = get_Minus_op(n);
3118 HANDLE_UNOP_PHI(tarval_neg,a,c);
3120 mode = get_irn_mode(a);
3121 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3122 /* the following rules are only to twos-complement */
3125 ir_node *op = get_Not_op(a);
3126 tarval *tv = get_mode_one(mode);
3127 ir_node *blk = get_irn_n(n, -1);
3128 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3129 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3130 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3134 ir_node *c = get_Shr_right(a);
3137 tarval *tv = get_Const_tarval(c);
3139 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3140 /* -(a >>u (size-1)) = a >>s (size-1) */
3141 ir_node *v = get_Shr_left(a);
3143 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3144 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3150 ir_node *c = get_Shrs_right(a);
3153 tarval *tv = get_Const_tarval(c);
3155 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3156 /* -(a >>s (size-1)) = a >>u (size-1) */
3157 ir_node *v = get_Shrs_left(a);
3159 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3160 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3167 /* - (a-b) = b - a */
3168 ir_node *la = get_Sub_left(a);
3169 ir_node *ra = get_Sub_right(a);
3170 ir_node *blk = get_irn_n(n, -1);
3172 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3173 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3178 } /* transform_node_Minus */
3181 * Transform a Cast_type(Const) into a new Const_type
3183 static ir_node *transform_node_Cast(ir_node *n) {
3185 ir_node *pred = get_Cast_op(n);
3186 ir_type *tp = get_irn_type(n);
3188 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
3189 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3190 get_Const_tarval(pred), tp);
3191 DBG_OPT_CSTEVAL(oldn, n);
3192 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
3193 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3194 get_SymConst_kind(pred), tp);
3195 DBG_OPT_CSTEVAL(oldn, n);
3199 } /* transform_node_Cast */
3202 * Transform a Proj(Div) with a non-zero value.
3203 * Removes the exceptions and routes the memory to the NoMem node.
3205 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3206 ir_node *div = get_Proj_pred(proj);
3207 ir_node *b = get_Div_right(div);
3208 ir_node *confirm, *res, *new_mem;
3211 if (value_not_zero(b, &confirm)) {
3212 /* div(x, y) && y != 0 */
3213 proj_nr = get_Proj_proj(proj);
3215 case pn_Div_X_regular:
3216 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3218 case pn_Div_X_except:
3219 /* we found an exception handler, remove it */
3220 DBG_OPT_EXC_REM(proj);
3224 res = get_Div_mem(div);
3225 new_mem = get_irg_no_mem(current_ir_graph);
3228 /* This node can only float up to the Confirm block */
3229 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3231 set_irn_pinned(div, op_pin_state_floats);
3232 /* this is a Div without exception, we can remove the memory edge */
3233 set_Div_mem(div, new_mem);
3238 } /* transform_node_Proj_Div */
3241 * Transform a Proj(Mod) with a non-zero value.
3242 * Removes the exceptions and routes the memory to the NoMem node.
3244 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3245 ir_node *mod = get_Proj_pred(proj);
3246 ir_node *b = get_Mod_right(mod);
3247 ir_node *confirm, *res, *new_mem;
3250 if (value_not_zero(b, &confirm)) {
3251 /* mod(x, y) && y != 0 */
3252 proj_nr = get_Proj_proj(proj);
3256 case pn_Mod_X_regular:
3257 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3259 case pn_Mod_X_except:
3260 /* we found an exception handler, remove it */
3261 DBG_OPT_EXC_REM(proj);
3265 res = get_Mod_mem(mod);
3266 new_mem = get_irg_no_mem(current_ir_graph);
3269 /* This node can only float up to the Confirm block */
3270 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3272 set_irn_pinned(mod, op_pin_state_floats);
3273 /* this is a Mod without exception, we can remove the memory edge */
3274 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3277 if (get_Mod_left(mod) == b) {
3278 /* a % a = 0 if a != 0 */
3279 ir_mode *mode = get_irn_mode(proj);
3280 ir_node *res = new_Const(mode, get_mode_null(mode));
3282 DBG_OPT_CSTEVAL(mod, res);
3288 } /* transform_node_Proj_Mod */
3291 * Transform a Proj(DivMod) with a non-zero value.
3292 * Removes the exceptions and routes the memory to the NoMem node.
3294 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3295 ir_node *divmod = get_Proj_pred(proj);
3296 ir_node *b = get_DivMod_right(divmod);
3297 ir_node *confirm, *res, *new_mem;
3300 if (value_not_zero(b, &confirm)) {
3301 /* DivMod(x, y) && y != 0 */
3302 proj_nr = get_Proj_proj(proj);
3306 case pn_DivMod_X_regular:
3307 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3309 case pn_DivMod_X_except:
3310 /* we found an exception handler, remove it */
3311 DBG_OPT_EXC_REM(proj);
3315 res = get_DivMod_mem(divmod);
3316 new_mem = get_irg_no_mem(current_ir_graph);
3319 /* This node can only float up to the Confirm block */
3320 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3322 set_irn_pinned(divmod, op_pin_state_floats);
3323 /* this is a DivMod without exception, we can remove the memory edge */
3324 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3327 case pn_DivMod_res_mod:
3328 if (get_DivMod_left(divmod) == b) {
3329 /* a % a = 0 if a != 0 */
3330 ir_mode *mode = get_irn_mode(proj);
3331 ir_node *res = new_Const(mode, get_mode_null(mode));
3333 DBG_OPT_CSTEVAL(divmod, res);
3339 } /* transform_node_Proj_DivMod */
3342 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3344 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3345 if (get_opt_unreachable_code()) {
3346 ir_node *n = get_Proj_pred(proj);
3347 ir_node *b = get_Cond_selector(n);
3349 if (mode_is_int(get_irn_mode(b))) {
3350 tarval *tb = value_of(b);
3352 if (tb != tarval_bad) {
3353 /* we have a constant switch */
3354 long num = get_Proj_proj(proj);
3356 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3357 if (get_tarval_long(tb) == num) {
3358 /* Do NOT create a jump here, or we will have 2 control flow ops
3359 * in a block. This case is optimized away in optimize_cf(). */
3362 /* this case will NEVER be taken, kill it */
3370 } /* transform_node_Proj_Cond */
3373 * Normalizes and optimizes Cmp nodes.
3375 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3376 ir_node *n = get_Proj_pred(proj);
3377 ir_node *left = get_Cmp_left(n);
3378 ir_node *right = get_Cmp_right(n);
3382 ir_mode *mode = NULL;
3383 long proj_nr = get_Proj_proj(proj);
3385 /* we can evaluate this direct */
3388 return new_Const(mode_b, get_tarval_b_false());
3390 return new_Const(mode_b, get_tarval_b_true());
3392 if(!mode_is_float(get_irn_mode(left)))
3393 return new_Const(mode_b, get_tarval_b_true());
3399 /* Remove unnecessary conversions */
3400 /* TODO handle constants */
3401 if (is_Conv(left) && is_Conv(right)) {
3402 ir_mode *mode = get_irn_mode(left);
3403 ir_node *op_left = get_Conv_op(left);
3404 ir_node *op_right = get_Conv_op(right);
3405 ir_mode *mode_left = get_irn_mode(op_left);
3406 ir_mode *mode_right = get_irn_mode(op_right);
3408 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3409 ir_graph *irg = current_ir_graph;
3410 ir_node *block = get_nodes_block(n);
3412 if (mode_left == mode_right) {
3416 } else if (smaller_mode(mode_left, mode_right)) {
3417 left = new_r_Conv(irg, block, op_left, mode_right);
3420 } else if (smaller_mode(mode_right, mode_left)) {
3422 right = new_r_Conv(irg, block, op_right, mode_left);
3428 /* TODO extend to arbitrary constants */
3429 if (is_Conv(left) &&
3431 tarval_is_null(get_Const_tarval(right))) {
3432 ir_mode* mode = get_irn_mode(left);
3433 ir_node* op = get_Conv_op(left);
3434 ir_mode* op_mode = get_irn_mode(op);
3436 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3437 (mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3438 ir_node *null = new_Const(op_mode, get_mode_null(op_mode));
3439 set_Cmp_left( n, op);
3440 set_Cmp_right(n, null);
3447 left = get_Cast_op(left);
3449 right = get_Cast_op(right);
3451 /* remove operation of both sides if possible */
3452 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3453 ir_opcode lop = get_irn_opcode(left);
3455 if (lop == get_irn_opcode(right)) {
3456 ir_node *ll, *lr, *rl, *rr;
3458 /* same operation on both sides, try to remove */
3462 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3463 left = get_unop_op(left);
3464 right = get_unop_op(right);
3468 ll = get_Add_left(left);
3469 lr = get_Add_right(left);
3470 rl = get_Add_left(right);
3471 rr = get_Add_right(right);
3474 /* X + a CMP X + b ==> a CMP b */
3478 } else if (ll == rr) {
3479 /* X + a CMP b + X ==> a CMP b */
3483 } else if (lr == rl) {
3484 /* a + X CMP X + b ==> a CMP b */
3488 } else if (lr == rr) {
3489 /* a + X CMP b + X ==> a CMP b */
3496 ll = get_Sub_left(left);
3497 lr = get_Sub_right(left);
3498 rl = get_Sub_left(right);
3499 rr = get_Sub_right(right);
3502 /* X - a CMP X - b ==> a CMP b */
3506 } else if (lr == rr) {
3507 /* a - X CMP b - X ==> a CMP b */
3514 if (get_Rot_right(left) == get_Rot_right(right)) {
3515 /* a ROT X CMP b ROT X */
3516 left = get_Rot_left(left);
3517 right = get_Rot_left(right);
3527 if (get_irn_mode(left) == mode_b) {
3528 ir_graph *irg = current_ir_graph;
3529 ir_node *block = get_nodes_block(n);
3532 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3533 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3534 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3535 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3536 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3537 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3541 if (!get_opt_reassociation())
3545 * First step: normalize the compare op
3546 * by placing the constant on the right side
3547 * or moving the lower address node to the left.
3548 * We ignore the case that both are constants
3549 * this case should be optimized away.
3551 if (is_Const(right)) {
3553 } else if (is_Const(left)) {
3558 proj_nr = get_inversed_pnc(proj_nr);
3560 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3566 proj_nr = get_inversed_pnc(proj_nr);
3571 * Second step: Try to reduce the magnitude
3572 * of a constant. This may help to generate better code
3573 * later and may help to normalize more compares.
3574 * Of course this is only possible for integer values.
3577 mode = get_irn_mode(c);
3578 tv = get_Const_tarval(c);
3580 if (tv != tarval_bad) {
3581 /* the following optimization is possible on modes without Overflow
3582 * on Unary Minus or on == and !=:
3583 * -a CMP c ==> a swap(CMP) -c
3585 * Beware: for two-complement Overflow may occur, so only == and != can
3586 * be optimized, see this:
3587 * -MININT < 0 =/=> MININT > 0 !!!
3589 if (is_Minus(left) &&
3590 (!mode_overflow_on_unary_Minus(mode) ||
3591 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3592 tv = tarval_neg(tv);
3594 if (tv != tarval_bad) {
3595 left = get_Minus_op(left);
3596 proj_nr = get_inversed_pnc(proj_nr);
3599 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3600 tv = tarval_not(tv);
3602 if (tv != tarval_bad) {
3603 left = get_Not_op(left);
3608 /* for integer modes, we have more */
3609 if (mode_is_int(mode)) {
3610 /* Ne includes Unordered which is not possible on integers.
3611 * However, frontends often use this wrong, so fix it here */
3612 if (proj_nr & pn_Cmp_Uo) {
3613 proj_nr &= ~pn_Cmp_Uo;
3614 set_Proj_proj(proj, proj_nr);
3617 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3618 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3619 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3620 tv = tarval_sub(tv, get_mode_one(mode));
3622 if (tv != tarval_bad) {
3623 proj_nr ^= pn_Cmp_Eq;
3627 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3628 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3629 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3630 tv = tarval_add(tv, get_mode_one(mode));
3632 if (tv != tarval_bad) {
3633 proj_nr ^= pn_Cmp_Eq;
3638 /* the following reassociations work only for == and != */
3639 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3641 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3642 if (tarval_is_null(tv) && is_Sub(left)) {
3643 right =get_Sub_right(left);
3644 left = get_Sub_left(left);
3646 tv = value_of(right);
3650 if (tv != tarval_bad) {
3651 ir_op *op = get_irn_op(left);
3653 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3655 ir_node *c1 = get_Sub_right(left);
3656 tarval *tv2 = value_of(c1);
3658 if (tv2 != tarval_bad) {
3659 tv2 = tarval_add(tv, value_of(c1));
3661 if (tv2 != tarval_bad) {
3662 left = get_Sub_left(left);
3668 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3669 else if (op == op_Add) {
3670 ir_node *a_l = get_Add_left(left);
3671 ir_node *a_r = get_Add_right(left);
3675 if (get_irn_op(a_l) == op_Const) {
3677 tv2 = value_of(a_l);
3680 tv2 = value_of(a_r);
3683 if (tv2 != tarval_bad) {
3684 tv2 = tarval_sub(tv, tv2);
3686 if (tv2 != tarval_bad) {
3693 /* -a == c ==> a == -c, -a != c ==> a != -c */
3694 else if (op == op_Minus) {
3695 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3697 if (tv2 != tarval_bad) {
3698 left = get_Minus_op(left);
3705 /* the following reassociations work only for <= */
3706 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3707 if (tv != tarval_bad) {
3708 ir_op *op = get_irn_op(left);
3710 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3718 * optimization for AND:
3720 * And(x, C) == C ==> And(x, C) != 0
3721 * And(x, C) != C ==> And(X, C) == 0
3723 * if C is a single Bit constant.
3725 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3726 if (tarval_is_single_bit(tv)) {
3727 /* check for Constant's match. We have check hare the tarvals,
3728 because our const might be changed */
3729 ir_node *la = get_And_left(left);
3730 ir_node *ra = get_And_right(left);
3731 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3732 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3733 /* fine: do the transformation */
3734 tv = get_mode_null(get_tarval_mode(tv));
3735 proj_nr ^= pn_Cmp_Leg;
3740 } /* tarval != bad */
3744 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3746 if (changed & 2) /* need a new Const */
3747 right = new_Const(mode, tv);
3749 /* create a new compare */
3750 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3752 set_Proj_pred(proj, n);
3753 set_Proj_proj(proj, proj_nr);
3757 } /* transform_node_Proj_Cmp */
3760 * Does all optimizations on nodes that must be done on it's Proj's
3761 * because of creating new nodes.
3763 static ir_node *transform_node_Proj(ir_node *proj) {
3764 ir_node *n = get_Proj_pred(proj);
3766 switch (get_irn_opcode(n)) {
3768 return transform_node_Proj_Div(proj);
3771 return transform_node_Proj_Mod(proj);
3774 return transform_node_Proj_DivMod(proj);
3777 return transform_node_Proj_Cond(proj);
3780 return transform_node_Proj_Cmp(proj);
3783 /* should not happen, but if it does will be optimized away */
3784 return equivalent_node_Proj(proj);
3790 } /* transform_node_Proj */
3793 * Move Confirms down through Phi nodes.
3795 static ir_node *transform_node_Phi(ir_node *phi) {
3797 ir_mode *mode = get_irn_mode(phi);
3799 if (mode_is_reference(mode)) {
3800 n = get_irn_arity(phi);
3802 /* Beware of Phi0 */
3804 ir_node *pred = get_irn_n(phi, 0);
3805 ir_node *bound, *new_Phi, *block, **in;
3808 if (! is_Confirm(pred))
3811 bound = get_Confirm_bound(pred);
3812 pnc = get_Confirm_cmp(pred);
3814 NEW_ARR_A(ir_node *, in, n);
3815 in[0] = get_Confirm_value(pred);
3817 for (i = 1; i < n; ++i) {
3818 pred = get_irn_n(phi, i);
3820 if (! is_Confirm(pred) ||
3821 get_Confirm_bound(pred) != bound ||
3822 get_Confirm_cmp(pred) != pnc)
3824 in[i] = get_Confirm_value(pred);
3826 /* move the Confirm nodes "behind" the Phi */
3827 block = get_irn_n(phi, -1);
3828 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3829 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3833 } /* transform_node_Phi */
3836 * Returns the operands of a commutative bin-op, if one operand is
3837 * a const, it is returned as the second one.
3839 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3840 ir_node *op_a = get_binop_left(binop);
3841 ir_node *op_b = get_binop_right(binop);
3843 assert(is_op_commutative(get_irn_op(binop)));
3845 if (get_irn_op(op_a) == op_Const) {
3852 } /* get_comm_Binop_Ops */
3855 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3856 * Such pattern may arise in bitfield stores.
3858 * value c4 value c4 & c2
3859 * AND c3 AND c1 | c3
3866 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3869 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3872 ir_node *and_l, *c3;
3873 ir_node *value, *c4;
3874 ir_node *new_and, *new_const, *block;
3875 ir_mode *mode = get_irn_mode(or);
3877 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3880 get_comm_Binop_Ops(or, &and, &c1);
3881 if (!is_Const(c1) || !is_And(and))
3884 get_comm_Binop_Ops(and, &or_l, &c2);
3888 tv1 = get_Const_tarval(c1);
3889 tv2 = get_Const_tarval(c2);
3891 tv = tarval_or(tv1, tv2);
3892 if (tarval_is_all_one(tv)) {
3893 /* the AND does NOT clear a bit with isn't set by the OR */
3894 set_Or_left(or, or_l);
3895 set_Or_right(or, c1);
3897 /* check for more */
3904 get_comm_Binop_Ops(or_l, &and_l, &c3);
3905 if (!is_Const(c3) || !is_And(and_l))
3908 get_comm_Binop_Ops(and_l, &value, &c4);
3912 /* ok, found the pattern, check for conditions */
3913 assert(mode == get_irn_mode(and));
3914 assert(mode == get_irn_mode(or_l));
3915 assert(mode == get_irn_mode(and_l));
3917 tv3 = get_Const_tarval(c3);
3918 tv4 = get_Const_tarval(c4);
3920 tv = tarval_or(tv4, tv2);
3921 if (!tarval_is_all_one(tv)) {
3922 /* have at least one 0 at the same bit position */
3926 n_tv4 = tarval_not(tv4);
3927 if (tv3 != tarval_and(tv3, n_tv4)) {
3928 /* bit in the or_mask is outside the and_mask */
3932 n_tv2 = tarval_not(tv2);
3933 if (tv1 != tarval_and(tv1, n_tv2)) {
3934 /* bit in the or_mask is outside the and_mask */
3938 /* ok, all conditions met */
3939 block = get_irn_n(or, -1);
3941 new_and = new_r_And(current_ir_graph, block,
3942 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3944 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3946 set_Or_left(or, new_and);
3947 set_Or_right(or, new_const);
3949 /* check for more */
3951 } /* transform_node_Or_bf_store */
3954 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3956 static ir_node *transform_node_Or_Rot(ir_node *or) {
3957 ir_mode *mode = get_irn_mode(or);
3958 ir_node *shl, *shr, *block;
3959 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3962 if (! mode_is_int(mode))
3965 shl = get_binop_left(or);
3966 shr = get_binop_right(or);
3968 if (get_irn_op(shl) == op_Shr) {
3969 if (get_irn_op(shr) != op_Shl)
3975 } else if (get_irn_op(shl) != op_Shl) {
3977 } else if (get_irn_op(shr) != op_Shr) {
3980 x = get_Shl_left(shl);
3981 if (x != get_Shr_left(shr))
3984 c1 = get_Shl_right(shl);
3985 c2 = get_Shr_right(shr);
3986 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3987 tv1 = get_Const_tarval(c1);
3988 if (! tarval_is_long(tv1))
3991 tv2 = get_Const_tarval(c2);
3992 if (! tarval_is_long(tv2))
3995 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3996 != get_mode_size_bits(mode))
3999 /* yet, condition met */
4000 block = get_irn_n(or, -1);
4002 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4004 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4006 } else if (get_irn_op(c1) == op_Sub) {
4010 if (get_Sub_right(sub) != v)
4013 c1 = get_Sub_left(sub);
4014 if (get_irn_op(c1) != op_Const)
4017 tv1 = get_Const_tarval(c1);
4018 if (! tarval_is_long(tv1))
4021 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4024 /* yet, condition met */
4025 block = get_nodes_block(or);
4027 /* a Rot right is not supported, so use a rot left */
4028 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4030 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4032 } else if (get_irn_op(c2) == op_Sub) {
4036 c1 = get_Sub_left(sub);
4037 if (get_irn_op(c1) != op_Const)
4040 tv1 = get_Const_tarval(c1);
4041 if (! tarval_is_long(tv1))
4044 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4047 /* yet, condition met */
4048 block = get_irn_n(or, -1);
4051 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4053 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4058 } /* transform_node_Or_Rot */
4063 static ir_node *transform_node_Or(ir_node *n) {
4064 ir_node *c, *oldn = n;
4065 ir_node *a = get_Or_left(n);
4066 ir_node *b = get_Or_right(n);
4068 if (is_Not(a) && is_Not(b)) {
4069 /* ~a | ~b = ~(a&b) */
4070 ir_node *block = get_nodes_block(n);
4071 ir_mode *mode = get_irn_mode(n);
4075 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4076 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4077 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4081 /* we can evaluate 2 Projs of the same Cmp */
4082 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4083 ir_node *pred_a = get_Proj_pred(a);
4084 ir_node *pred_b = get_Proj_pred(b);
4085 if (pred_a == pred_b) {
4086 dbg_info *dbgi = get_irn_dbg_info(n);
4087 ir_node *block = get_nodes_block(pred_a);
4088 pn_Cmp pn_a = get_Proj_proj(a);
4089 pn_Cmp pn_b = get_Proj_proj(b);
4090 /* yes, we can simply calculate with pncs */
4091 pn_Cmp new_pnc = pn_a | pn_b;
4093 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4098 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4100 n = transform_node_Or_bf_store(n);
4101 n = transform_node_Or_Rot(n);
4105 n = transform_bitwise_distributive(n, transform_node_Or);
4108 } /* transform_node_Or */
4112 static ir_node *transform_node(ir_node *n);
4115 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4117 * Should be moved to reassociation?
4119 static ir_node *transform_node_shift(ir_node *n) {
4120 ir_node *left, *right;
4121 tarval *tv1, *tv2, *res;
4123 int modulo_shf, flag;
4125 left = get_binop_left(n);
4127 /* different operations */
4128 if (get_irn_op(left) != get_irn_op(n))
4131 right = get_binop_right(n);
4132 tv1 = value_of(right);
4133 if (tv1 == tarval_bad)
4136 tv2 = value_of(get_binop_right(left));
4137 if (tv2 == tarval_bad)
4140 res = tarval_add(tv1, tv2);
4142 /* beware: a simple replacement works only, if res < modulo shift */
4143 mode = get_irn_mode(n);
4147 modulo_shf = get_mode_modulo_shift(mode);
4148 if (modulo_shf > 0) {
4149 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4151 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4157 /* ok, we can replace it */
4158 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4160 in[0] = get_binop_left(left);
4161 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4163 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4165 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4167 return transform_node(irn);
4170 } /* transform_node_shift */
4175 static ir_node *transform_node_Shr(ir_node *n) {
4176 ir_node *c, *oldn = n;
4177 ir_node *a = get_Shr_left(n);
4178 ir_node *b = get_Shr_right(n);
4180 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4181 return transform_node_shift(n);
4182 } /* transform_node_Shr */
4187 static ir_node *transform_node_Shrs(ir_node *n) {
4188 ir_node *c, *oldn = n;
4189 ir_node *a = get_Shrs_left(n);
4190 ir_node *b = get_Shrs_right(n);
4192 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4193 return transform_node_shift(n);
4194 } /* transform_node_Shrs */
4199 static ir_node *transform_node_Shl(ir_node *n) {
4200 ir_node *c, *oldn = n;
4201 ir_node *a = get_Shl_left(n);
4202 ir_node *b = get_Shl_right(n);
4204 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4205 return transform_node_shift(n);
4206 } /* transform_node_Shl */
4209 * Remove dead blocks and nodes in dead blocks
4210 * in keep alive list. We do not generate a new End node.
4212 static ir_node *transform_node_End(ir_node *n) {
4213 int i, j, n_keepalives = get_End_n_keepalives(n);
4216 NEW_ARR_A(ir_node *, in, n_keepalives);
4218 for (i = j = 0; i < n_keepalives; ++i) {
4219 ir_node *ka = get_End_keepalive(n, i);
4221 if (! is_Block_dead(ka)) {
4225 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4228 /* FIXME: beabi need to keep a Proj(M) */
4229 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4232 if (j != n_keepalives)
4233 set_End_keepalives(n, j, in);
4235 } /* transform_node_End */
4237 /** returns 1 if a == -b */
4238 static int is_negated_value(ir_node *a, ir_node *b) {
4239 if(is_Minus(a) && get_Minus_op(a) == b)
4241 if(is_Minus(b) && get_Minus_op(b) == a)
4243 if(is_Sub(a) && is_Sub(b)) {
4244 ir_node *a_left = get_Sub_left(a);
4245 ir_node *a_right = get_Sub_right(a);
4246 ir_node *b_left = get_Sub_left(b);
4247 ir_node *b_right = get_Sub_right(b);
4249 if(a_left == b_right && a_right == b_left)
4257 * Optimize a Mux into some simpler cases.
4259 static ir_node *transform_node_Mux(ir_node *n) {
4260 ir_node *oldn = n, *sel = get_Mux_sel(n);
4261 ir_mode *mode = get_irn_mode(n);
4263 if (mode == mode_b) {
4264 ir_node *t = get_Mux_true(n);
4265 ir_node *f = get_Mux_false(n);
4266 dbg_info *dbg = get_irn_dbg_info(n);
4267 ir_node *block = get_irn_n(n, -1);
4268 ir_graph *irg = current_ir_graph;
4271 tarval *tv_t = get_Const_tarval(t);
4272 if (tv_t == tarval_b_true) {
4274 assert(get_Const_tarval(f) == tarval_b_false);
4277 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4280 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4281 assert(tv_t == tarval_b_false);
4283 assert(get_Const_tarval(f) == tarval_b_true);
4286 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4289 } else if (is_Const(f)) {
4290 tarval *tv_f = get_Const_tarval(f);
4291 if (tv_f == tarval_b_true) {
4292 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4293 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4295 assert(tv_f == tarval_b_false);
4296 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4301 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
4302 ir_node *cmp = get_Proj_pred(sel);
4303 long pn = get_Proj_proj(sel);
4304 ir_node *f = get_Mux_false(n);
4305 ir_node *t = get_Mux_true(n);
4308 * Note: normalization puts the constant on the right side,
4309 * so we check only one case.
4311 * Note further that these optimization work even for floating point
4312 * with NaN's because -NaN == NaN.
4313 * However, if +0 and -0 is handled differently, we cannot use the first
4317 ir_node *cmp_r = get_Cmp_right(cmp);
4318 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4319 ir_node *block = get_irn_n(n, -1);
4321 if(is_negated_value(f, t)) {
4322 ir_node *cmp_left = get_Cmp_left(cmp);
4324 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4325 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4326 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4328 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4330 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4332 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4333 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4334 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4336 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4338 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4340 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4345 if (mode_is_int(mode) && mode_is_signed(mode) &&
4346 get_mode_arithmetic(mode) == irma_twos_complement) {
4347 ir_node *x = get_Cmp_left(cmp);
4349 /* the following optimization works only with signed integer two-complement mode */
4351 if (mode == get_irn_mode(x)) {
4353 * FIXME: this restriction is two rigid, as it would still
4354 * work if mode(x) = Hs and mode == Is, but at least it removes
4357 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4358 is_Const(t) && is_Const_all_one(t) &&
4359 is_Const(f) && is_Const_null(f)) {
4361 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4365 n = new_rd_Shrs(get_irn_dbg_info(n),
4366 current_ir_graph, block, x,
4367 new_r_Const_long(current_ir_graph, block, mode_Iu,
4368 get_mode_size_bits(mode) - 1),
4370 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4372 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4373 is_Const(t) && is_Const_one(t) &&
4374 is_Const(f) && is_Const_null(f)) {
4376 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4380 n = new_rd_Shr(get_irn_dbg_info(n),
4381 current_ir_graph, block,
4382 new_r_Minus(current_ir_graph, block, x, mode),
4383 new_r_Const_long(current_ir_graph, block, mode_Iu,
4384 get_mode_size_bits(mode) - 1),
4386 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4394 return arch_transform_node_Mux(n);
4395 } /* transform_node_Mux */
4398 * Optimize a Psi into some simpler cases.
4400 static ir_node *transform_node_Psi(ir_node *n) {
4402 return transform_node_Mux(n);
4405 } /* transform_node_Psi */
4408 * Tries several [inplace] [optimizing] transformations and returns an
4409 * equivalent node. The difference to equivalent_node() is that these
4410 * transformations _do_ generate new nodes, and thus the old node must
4411 * not be freed even if the equivalent node isn't the old one.
4413 static ir_node *transform_node(ir_node *n) {
4417 * Transform_node is the only "optimizing transformation" that might
4418 * return a node with a different opcode. We iterate HERE until fixpoint
4419 * to get the final result.
4423 if (n->op->ops.transform_node)
4424 n = n->op->ops.transform_node(n);
4425 } while (oldn != n);
4428 } /* transform_node */
4431 * Sets the default transform node operation for an ir_op_ops.
4433 * @param code the opcode for the default operation
4434 * @param ops the operations initialized
4439 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4443 ops->transform_node = transform_node_##a; \
4477 } /* firm_set_default_transform_node */
4480 /* **************** Common Subexpression Elimination **************** */
4482 /** The size of the hash table used, should estimate the number of nodes
4484 #define N_IR_NODES 512
4486 /** Compares the attributes of two Const nodes. */
4487 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4488 return (get_Const_tarval(a) != get_Const_tarval(b))
4489 || (get_Const_type(a) != get_Const_type(b));
4490 } /* node_cmp_attr_Const */
4492 /** Compares the attributes of two Proj nodes. */
4493 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4494 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4495 } /* node_cmp_attr_Proj */
4497 /** Compares the attributes of two Filter nodes. */
4498 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4499 return get_Filter_proj(a) != get_Filter_proj(b);
4500 } /* node_cmp_attr_Filter */
4502 /** Compares the attributes of two Alloc nodes. */
4503 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4504 const alloc_attr *pa = get_irn_alloc_attr(a);
4505 const alloc_attr *pb = get_irn_alloc_attr(b);
4506 return (pa->where != pb->where) || (pa->type != pb->type);
4507 } /* node_cmp_attr_Alloc */
4509 /** Compares the attributes of two Free nodes. */
4510 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4511 const free_attr *pa = get_irn_free_attr(a);
4512 const free_attr *pb = get_irn_free_attr(b);
4513 return (pa->where != pb->where) || (pa->type != pb->type);
4514 } /* node_cmp_attr_Free */
4516 /** Compares the attributes of two SymConst nodes. */
4517 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4518 const symconst_attr *pa = get_irn_symconst_attr(a);
4519 const symconst_attr *pb = get_irn_symconst_attr(b);
4520 return (pa->num != pb->num)
4521 || (pa->sym.type_p != pb->sym.type_p)
4522 || (pa->tp != pb->tp);
4523 } /* node_cmp_attr_SymConst */
4525 /** Compares the attributes of two Call nodes. */
4526 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4527 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4528 } /* node_cmp_attr_Call */
4530 /** Compares the attributes of two Sel nodes. */
4531 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4532 const ir_entity *a_ent = get_Sel_entity(a);
4533 const ir_entity *b_ent = get_Sel_entity(b);
4535 (a_ent->kind != b_ent->kind) ||
4536 (a_ent->name != b_ent->name) ||
4537 (a_ent->owner != b_ent->owner) ||
4538 (a_ent->ld_name != b_ent->ld_name) ||
4539 (a_ent->type != b_ent->type);
4540 } /* node_cmp_attr_Sel */
4542 /** Compares the attributes of two Phi nodes. */
4543 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4544 /* we can only enter this function if both nodes have the same number of inputs,
4545 hence it is enough to check if one of them is a Phi0 */
4547 /* check the Phi0 attribute */
4548 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4551 } /* node_cmp_attr_Phi */
4553 /** Compares the attributes of two Conv nodes. */
4554 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4555 return get_Conv_strict(a) != get_Conv_strict(b);
4556 } /* node_cmp_attr_Conv */
4558 /** Compares the attributes of two Cast nodes. */
4559 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4560 return get_Cast_type(a) != get_Cast_type(b);
4561 } /* node_cmp_attr_Cast */
4563 /** Compares the attributes of two Load nodes. */
4564 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4565 if (get_Load_volatility(a) == volatility_is_volatile ||
4566 get_Load_volatility(b) == volatility_is_volatile)
4567 /* NEVER do CSE on volatile Loads */
4569 /* do not CSE Loads with different alignment. Be conservative. */
4570 if (get_Load_align(a) != get_Load_align(b))
4573 return get_Load_mode(a) != get_Load_mode(b);
4574 } /* node_cmp_attr_Load */
4576 /** Compares the attributes of two Store nodes. */
4577 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4578 /* do not CSE Stores with different alignment. Be conservative. */
4579 if (get_Store_align(a) != get_Store_align(b))
4582 /* NEVER do CSE on volatile Stores */
4583 return (get_Store_volatility(a) == volatility_is_volatile ||
4584 get_Store_volatility(b) == volatility_is_volatile);
4585 } /* node_cmp_attr_Store */
4587 /** Compares the attributes of two Confirm nodes. */
4588 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4589 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4590 } /* node_cmp_attr_Confirm */
4592 /** Compares the attributes of two ASM nodes. */
4593 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4595 const ir_asm_constraint *ca;
4596 const ir_asm_constraint *cb;
4599 if (get_ASM_text(a) != get_ASM_text(b))
4602 /* Should we really check the constraints here? Should be better, but is strange. */
4603 n = get_ASM_n_input_constraints(a);
4604 if (n != get_ASM_n_input_constraints(b))
4607 ca = get_ASM_input_constraints(a);
4608 cb = get_ASM_input_constraints(b);
4609 for (i = 0; i < n; ++i) {
4610 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4614 n = get_ASM_n_output_constraints(a);
4615 if (n != get_ASM_n_output_constraints(b))
4618 ca = get_ASM_output_constraints(a);
4619 cb = get_ASM_output_constraints(b);
4620 for (i = 0; i < n; ++i) {
4621 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4625 n = get_ASM_n_clobbers(a);
4626 if (n != get_ASM_n_clobbers(b))
4629 cla = get_ASM_clobbers(a);
4630 clb = get_ASM_clobbers(b);
4631 for (i = 0; i < n; ++i) {
4632 if (cla[i] != clb[i])
4636 } /* node_cmp_attr_ASM */
4639 * Set the default node attribute compare operation for an ir_op_ops.
4641 * @param code the opcode for the default operation
4642 * @param ops the operations initialized
4647 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4651 ops->node_cmp_attr = node_cmp_attr_##a; \
4676 } /* firm_set_default_node_cmp_attr */
4679 * Compare function for two nodes in the hash table. Gets two
4680 * nodes as parameters. Returns 0 if the nodes are a cse.
4682 int identities_cmp(const void *elt, const void *key) {
4689 if (a == b) return 0;
4691 if ((get_irn_op(a) != get_irn_op(b)) ||
4692 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4694 /* compare if a's in and b's in are of equal length */
4695 irn_arity_a = get_irn_intra_arity (a);
4696 if (irn_arity_a != get_irn_intra_arity(b))
4699 /* for block-local cse and op_pin_state_pinned nodes: */
4700 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4701 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4705 /* compare a->in[0..ins] with b->in[0..ins] */
4706 for (i = 0; i < irn_arity_a; i++)
4707 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4711 * here, we already now that the nodes are identical except their
4714 if (a->op->ops.node_cmp_attr)
4715 return a->op->ops.node_cmp_attr(a, b);
4718 } /* identities_cmp */
4721 * Calculate a hash value of a node.
4723 unsigned ir_node_hash(ir_node *node) {
4727 if (node->op == op_Const) {
4728 /* special value for const, as they only differ in their tarval. */
4729 h = HASH_PTR(node->attr.con.tv);
4730 h = 9*h + HASH_PTR(get_irn_mode(node));
4731 } else if (node->op == op_SymConst) {
4732 /* special value for const, as they only differ in their symbol. */
4733 h = HASH_PTR(node->attr.symc.sym.type_p);
4734 h = 9*h + HASH_PTR(get_irn_mode(node));
4737 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4738 h = irn_arity = get_irn_intra_arity(node);
4740 /* consider all in nodes... except the block if not a control flow. */
4741 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4742 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4746 h = 9*h + HASH_PTR(get_irn_mode(node));
4748 h = 9*h + HASH_PTR(get_irn_op(node));
4752 } /* ir_node_hash */
4754 pset *new_identities(void) {
4755 return new_pset(identities_cmp, N_IR_NODES);
4756 } /* new_identities */
4758 void del_identities(pset *value_table) {
4759 del_pset(value_table);
4760 } /* del_identities */
4763 * Normalize a node by putting constants (and operands with smaller
4764 * node index) on the right
4766 * @param n The node to normalize
4768 static void normalize_node(ir_node *n) {
4769 if (get_opt_reassociation()) {
4770 if (is_op_commutative(get_irn_op(n))) {
4771 ir_node *l = get_binop_left(n);
4772 ir_node *r = get_binop_right(n);
4773 int l_idx = get_irn_idx(l);
4774 int r_idx = get_irn_idx(r);
4776 /* For commutative operators perform a OP b == b OP a but keep
4777 constants on the RIGHT side. This helps greatly in some optimizations.
4778 Moreover we use the idx number to make the form deterministic. */
4779 if (is_irn_constlike(l))
4781 if (is_irn_constlike(r))
4783 if (l_idx < r_idx) {
4784 set_binop_left(n, r);
4785 set_binop_right(n, l);
4789 } /* normalize_node */
4792 * Return the canonical node computing the same value as n.
4794 * @param value_table The value table
4795 * @param n The node to lookup
4797 * Looks up the node in a hash table.
4799 * For Const nodes this is performed in the constructor, too. Const
4800 * nodes are extremely time critical because of their frequent use in
4801 * constant string arrays.
4803 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4806 if (!value_table) return n;
4810 o = pset_find(value_table, n, ir_node_hash(n));
4819 * During construction we set the op_pin_state_pinned flag in the graph right when the
4820 * optimization is performed. The flag turning on procedure global cse could
4821 * be changed between two allocations. This way we are safe.
4823 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4826 n = identify(value_table, n);
4827 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4828 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4830 } /* identify_cons */
4833 * Return the canonical node computing the same value as n.
4834 * Looks up the node in a hash table, enters it in the table
4835 * if it isn't there yet.
4837 ir_node *identify_remember(pset *value_table, ir_node *n) {
4840 if (!value_table) return n;
4843 /* lookup or insert in hash table with given hash key. */
4844 o = pset_insert(value_table, n, ir_node_hash(n));
4851 } /* identify_remember */
4853 /* Add a node to the identities value table. */
4854 void add_identities(pset *value_table, ir_node *node) {
4855 if (get_opt_cse() && is_no_Block(node))
4856 identify_remember(value_table, node);
4857 } /* add_identities */
4859 /* Visit each node in the value table of a graph. */
4860 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4862 ir_graph *rem = current_ir_graph;
4864 current_ir_graph = irg;
4865 foreach_pset(irg->value_table, node)
4867 current_ir_graph = rem;
4868 } /* visit_all_identities */
4871 * Garbage in, garbage out. If a node has a dead input, i.e., the
4872 * Bad node is input to the node, return the Bad node.
4874 static ir_node *gigo(ir_node *node) {
4876 ir_op *op = get_irn_op(node);
4878 /* remove garbage blocks by looking at control flow that leaves the block
4879 and replacing the control flow by Bad. */
4880 if (get_irn_mode(node) == mode_X) {
4881 ir_node *block = get_nodes_block(skip_Proj(node));
4883 /* Don't optimize nodes in immature blocks. */
4884 if (!get_Block_matured(block)) return node;
4885 /* Don't optimize End, may have Bads. */
4886 if (op == op_End) return node;
4888 if (is_Block(block)) {
4889 irn_arity = get_irn_arity(block);
4890 for (i = 0; i < irn_arity; i++) {
4891 if (!is_Bad(get_irn_n(block, i)))
4894 if (i == irn_arity) {
4895 ir_graph *irg = get_irn_irg(block);
4896 /* the start block is never dead */
4897 if (block != get_irg_start_block(irg)
4898 && block != get_irg_end_block(irg))
4904 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4905 blocks predecessors is dead. */
4906 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4907 irn_arity = get_irn_arity(node);
4910 * Beware: we can only read the block of a non-floating node.
4912 if (is_irn_pinned_in_irg(node) &&
4913 is_Block_dead(get_nodes_block(node)))
4916 for (i = 0; i < irn_arity; i++) {
4917 ir_node *pred = get_irn_n(node, i);
4922 /* Propagating Unknowns here seems to be a bad idea, because
4923 sometimes we need a node as a input and did not want that
4925 However, it might be useful to move this into a later phase
4926 (if you think that optimizing such code is useful). */
4927 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4928 return new_Unknown(get_irn_mode(node));
4933 /* With this code we violate the agreement that local_optimize
4934 only leaves Bads in Block, Phi and Tuple nodes. */
4935 /* If Block has only Bads as predecessors it's garbage. */
4936 /* If Phi has only Bads as predecessors it's garbage. */
4937 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4938 irn_arity = get_irn_arity(node);
4939 for (i = 0; i < irn_arity; i++) {
4940 if (!is_Bad(get_irn_n(node, i))) break;
4942 if (i == irn_arity) node = new_Bad();
4949 * These optimizations deallocate nodes from the obstack.
4950 * It can only be called if it is guaranteed that no other nodes
4951 * reference this one, i.e., right after construction of a node.
4953 * @param n The node to optimize
4955 * current_ir_graph must be set to the graph of the node!
4957 ir_node *optimize_node(ir_node *n) {
4960 ir_opcode iro = get_irn_opcode(n);
4962 /* Always optimize Phi nodes: part of the construction. */
4963 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4965 /* constant expression evaluation / constant folding */
4966 if (get_opt_constant_folding()) {
4967 /* neither constants nor Tuple values can be evaluated */
4968 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4969 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4970 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4971 /* try to evaluate */
4972 tv = computed_value(n);
4973 if (tv != tarval_bad) {
4975 ir_type *old_tp = get_irn_type(n);
4976 int i, arity = get_irn_arity(n);
4980 * Try to recover the type of the new expression.
4982 for (i = 0; i < arity && !old_tp; ++i)
4983 old_tp = get_irn_type(get_irn_n(n, i));
4986 * we MUST copy the node here temporary, because it's still needed
4987 * for DBG_OPT_CSTEVAL
4989 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4990 oldn = alloca(node_size);
4992 memcpy(oldn, n, node_size);
4993 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4995 /* ARG, copy the in array, we need it for statistics */
4996 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4998 /* note the inplace edges module */
4999 edges_node_deleted(n, current_ir_graph);
5001 /* evaluation was successful -- replace the node. */
5002 irg_kill_node(current_ir_graph, n);
5003 nw = new_Const(get_tarval_mode(tv), tv);
5005 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5006 set_Const_type(nw, old_tp);
5007 DBG_OPT_CSTEVAL(oldn, nw);
5008 tarval_enable_fp_ops(old_fp_mode);
5011 tarval_enable_fp_ops(old_fp_mode);
5015 /* remove unnecessary nodes */
5016 if (get_opt_constant_folding() ||
5017 (iro == iro_Phi) || /* always optimize these nodes. */
5019 (iro == iro_Proj) ||
5020 (iro == iro_Block) ) /* Flags tested local. */
5021 n = equivalent_node(n);
5023 /* Common Subexpression Elimination.
5025 * Checks whether n is already available.
5026 * The block input is used to distinguish different subexpressions. Right
5027 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5028 * subexpressions within a block.
5031 n = identify_cons(current_ir_graph->value_table, n);
5034 edges_node_deleted(oldn, current_ir_graph);
5036 /* We found an existing, better node, so we can deallocate the old node. */
5037 irg_kill_node(current_ir_graph, oldn);
5041 /* Some more constant expression evaluation that does not allow to
5043 iro = get_irn_opcode(n);
5044 if (get_opt_constant_folding() ||
5045 (iro == iro_Cond) ||
5046 (iro == iro_Proj)) /* Flags tested local. */
5047 n = transform_node(n);
5049 /* Remove nodes with dead (Bad) input.
5050 Run always for transformation induced Bads. */
5053 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5054 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5055 n = identify_remember(current_ir_graph->value_table, n);
5059 } /* optimize_node */
5063 * These optimizations never deallocate nodes (in place). This can cause dead
5064 * nodes lying on the obstack. Remove these by a dead node elimination,
5065 * i.e., a copying garbage collection.
5067 ir_node *optimize_in_place_2(ir_node *n) {
5070 ir_opcode iro = get_irn_opcode(n);
5072 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
5074 /* constant expression evaluation / constant folding */
5075 if (get_opt_constant_folding()) {
5076 /* neither constants nor Tuple values can be evaluated */
5077 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5078 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5079 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5080 /* try to evaluate */
5081 tv = computed_value(n);
5082 if (tv != tarval_bad) {
5083 /* evaluation was successful -- replace the node. */
5084 ir_type *old_tp = get_irn_type(n);
5085 int i, arity = get_irn_arity(n);
5088 * Try to recover the type of the new expression.
5090 for (i = 0; i < arity && !old_tp; ++i)
5091 old_tp = get_irn_type(get_irn_n(n, i));
5093 n = new_Const(get_tarval_mode(tv), tv);
5095 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5096 set_Const_type(n, old_tp);
5098 DBG_OPT_CSTEVAL(oldn, n);
5099 tarval_enable_fp_ops(old_fp_mode);
5102 tarval_enable_fp_ops(old_fp_mode);
5106 /* remove unnecessary nodes */
5107 if (get_opt_constant_folding() ||
5108 (iro == iro_Phi) || /* always optimize these nodes. */
5109 (iro == iro_Id) || /* ... */
5110 (iro == iro_Proj) || /* ... */
5111 (iro == iro_Block) ) /* Flags tested local. */
5112 n = equivalent_node(n);
5114 /** common subexpression elimination **/
5115 /* Checks whether n is already available. */
5116 /* The block input is used to distinguish different subexpressions. Right
5117 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5118 subexpressions within a block. */
5119 if (get_opt_cse()) {
5120 n = identify(current_ir_graph->value_table, n);
5123 /* Some more constant expression evaluation. */
5124 iro = get_irn_opcode(n);
5125 if (get_opt_constant_folding() ||
5126 (iro == iro_Cond) ||
5127 (iro == iro_Proj)) /* Flags tested local. */
5128 n = transform_node(n);
5130 /* Remove nodes with dead (Bad) input.
5131 Run always for transformation induced Bads. */
5134 /* Now we can verify the node, as it has no dead inputs any more. */
5137 /* Now we have a legal, useful node. Enter it in hash table for cse.
5138 Blocks should be unique anyways. (Except the successor of start:
5139 is cse with the start block!) */
5140 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5141 n = identify_remember(current_ir_graph->value_table, n);
5144 } /* optimize_in_place_2 */
5147 * Wrapper for external use, set proper status bits after optimization.
5149 ir_node *optimize_in_place(ir_node *n) {
5150 /* Handle graph state */
5151 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5153 if (get_opt_global_cse())
5154 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5155 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5156 set_irg_outs_inconsistent(current_ir_graph);
5158 /* FIXME: Maybe we could also test whether optimizing the node can
5159 change the control graph. */
5160 set_irg_doms_inconsistent(current_ir_graph);
5161 return optimize_in_place_2(n);
5162 } /* optimize_in_place */
5165 * Sets the default operation for an ir_ops.
5167 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5168 ops = firm_set_default_computed_value(code, ops);
5169 ops = firm_set_default_equivalent_node(code, ops);
5170 ops = firm_set_default_transform_node(code, ops);
5171 ops = firm_set_default_node_cmp_attr(code, ops);
5172 ops = firm_set_default_get_type(code, ops);
5173 ops = firm_set_default_get_type_attr(code, ops);
5174 ops = firm_set_default_get_entity_attr(code, ops);
5177 } /* firm_set_default_operations */