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 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1461 /* Mux(a CMP 0, X, a) */
1462 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1463 /* Mux(a CMP 0, -a, a) */
1464 if (proj_nr == pn_Cmp_Eq) {
1465 /* Mux(a == 0, -a, a) ==> -a */
1467 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1468 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1469 /* Mux(a != 0, -a, a) ==> a */
1471 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1473 } else if (classify_Const(b) == CNST_NULL) {
1474 /* Mux(a CMP 0, 0, a) */
1475 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1476 /* Mux(a != 0, 0, a) ==> a */
1478 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1479 } else if (proj_nr == pn_Cmp_Eq) {
1480 /* Mux(a == 0, 0, a) ==> 0 */
1482 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1489 } /* equivalent_node_Mux */
1492 * Returns a equivalent node of a Psi: if a condition is true
1493 * and all previous conditions are false we know its value.
1494 * If all conditions are false its value is the default one.
1496 static ir_node *equivalent_node_Psi(ir_node *n) {
1498 return equivalent_node_Mux(n);
1500 } /* equivalent_node_Psi */
1503 * Optimize -a CMP -b into b CMP a.
1504 * This works only for for modes where unary Minus
1506 * Note that two-complement integers can Overflow
1507 * so it will NOT work.
1509 * For == and != can be handled in Proj(Cmp)
1511 static ir_node *equivalent_node_Cmp(ir_node *n) {
1512 ir_node *left = get_Cmp_left(n);
1513 ir_node *right = get_Cmp_right(n);
1515 if (is_Minus(left) && is_Minus(right) &&
1516 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1517 left = get_Minus_op(left);
1518 right = get_Minus_op(right);
1519 set_Cmp_left(n, right);
1520 set_Cmp_right(n, left);
1523 } /* equivalent_node_Cmp */
1526 * Remove Confirm nodes if setting is on.
1527 * Replace Confirms(x, '=', Constlike) by Constlike.
1529 static ir_node *equivalent_node_Confirm(ir_node *n) {
1530 ir_node *pred = get_Confirm_value(n);
1531 pn_Cmp pnc = get_Confirm_cmp(n);
1533 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1535 * rare case: two identical Confirms one after another,
1536 * replace the second one with the first.
1540 if (pnc == pn_Cmp_Eq) {
1541 ir_node *bound = get_Confirm_bound(n);
1544 * Optimize a rare case:
1545 * Confirm(x, '=', Constlike) ==> Constlike
1547 if (is_irn_constlike(bound)) {
1548 DBG_OPT_CONFIRM(n, bound);
1552 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1556 * Optimize CopyB(mem, x, x) into a Nop.
1558 static ir_node *equivalent_node_CopyB(ir_node *n) {
1559 ir_node *a = get_CopyB_dst(n);
1560 ir_node *b = get_CopyB_src(n);
1563 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1564 ir_node *mem = get_CopyB_mem(n);
1565 ir_node *blk = get_nodes_block(n);
1566 turn_into_tuple(n, pn_CopyB_max);
1567 set_Tuple_pred(n, pn_CopyB_M, mem);
1568 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1569 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1570 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1573 } /* equivalent_node_CopyB */
1576 * Optimize Bounds(idx, idx, upper) into idx.
1578 static ir_node *equivalent_node_Bound(ir_node *n) {
1579 ir_node *idx = get_Bound_index(n);
1580 ir_node *lower = get_Bound_lower(n);
1583 /* By definition lower < upper, so if idx == lower -->
1584 lower <= idx && idx < upper */
1586 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1589 ir_node *pred = skip_Proj(idx);
1591 if (get_irn_op(pred) == op_Bound) {
1593 * idx was Bounds_check previously, it is still valid if
1594 * lower <= pred_lower && pred_upper <= upper.
1596 ir_node *upper = get_Bound_upper(n);
1597 if (get_Bound_lower(pred) == lower &&
1598 get_Bound_upper(pred) == upper) {
1600 * One could expect that we simply return the previous
1601 * Bound here. However, this would be wrong, as we could
1602 * add an exception Proj to a new location then.
1603 * So, we must turn in into a tuple.
1610 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1611 ir_node *mem = get_Bound_mem(n);
1612 ir_node *blk = get_nodes_block(n);
1613 turn_into_tuple(n, pn_Bound_max);
1614 set_Tuple_pred(n, pn_Bound_M, mem);
1615 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1616 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1617 set_Tuple_pred(n, pn_Bound_res, idx);
1620 } /* equivalent_node_Bound */
1623 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1624 * perform no actual computation, as, e.g., the Id nodes. It does not create
1625 * new nodes. It is therefore safe to free n if the node returned is not n.
1626 * If a node returns a Tuple we can not just skip it. If the size of the
1627 * in array fits, we transform n into a tuple (e.g., Div).
1629 ir_node *equivalent_node(ir_node *n) {
1630 if (n->op->ops.equivalent_node)
1631 return n->op->ops.equivalent_node(n);
1633 } /* equivalent_node */
1636 * Sets the default equivalent node operation for an ir_op_ops.
1638 * @param code the opcode for the default operation
1639 * @param ops the operations initialized
1644 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1648 ops->equivalent_node = equivalent_node_##a; \
1688 } /* firm_set_default_equivalent_node */
1691 * Returns non-zero if a node is a Phi node
1692 * with all predecessors constant.
1694 static int is_const_Phi(ir_node *n) {
1699 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1700 if (! is_Const(get_irn_n(n, i)))
1703 } /* is_const_Phi */
1706 * Apply an evaluator on a binop with a constant operators (and one Phi).
1708 * @param phi the Phi node
1709 * @param other the other operand
1710 * @param eval an evaluator function
1711 * @param left if non-zero, other is the left operand, else the right
1713 * @return a new Phi node if the conversion was successful, NULL else
1715 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1721 int i, n = get_irn_arity(phi);
1723 NEW_ARR_A(void *, res, n);
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = eval(other, tv);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1737 for (i = 0; i < n; ++i) {
1738 pred = get_irn_n(phi, i);
1739 tv = get_Const_tarval(pred);
1740 tv = eval(tv, other);
1742 if (tv == tarval_bad) {
1743 /* folding failed, bad */
1749 mode = get_irn_mode(phi);
1750 irg = current_ir_graph;
1751 for (i = 0; i < n; ++i) {
1752 pred = get_irn_n(phi, i);
1753 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1754 mode, res[i], get_Const_type(pred));
1756 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1757 } /* apply_binop_on_phi */
1760 * Apply an evaluator on a unop with a constant operator (a Phi).
1762 * @param phi the Phi node
1763 * @param eval an evaluator function
1765 * @return a new Phi node if the conversion was successful, NULL else
1767 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1773 int i, n = get_irn_arity(phi);
1775 NEW_ARR_A(void *, res, n);
1776 for (i = 0; i < n; ++i) {
1777 pred = get_irn_n(phi, i);
1778 tv = get_Const_tarval(pred);
1781 if (tv == tarval_bad) {
1782 /* folding failed, bad */
1787 mode = get_irn_mode(phi);
1788 irg = current_ir_graph;
1789 for (i = 0; i < n; ++i) {
1790 pred = get_irn_n(phi, i);
1791 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1792 mode, res[i], get_Const_type(pred));
1794 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1795 } /* apply_unop_on_phi */
1798 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1799 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1800 * If possible, remove the Conv's.
1802 static ir_node *transform_node_AddSub(ir_node *n) {
1803 ir_mode *mode = get_irn_mode(n);
1805 if (mode_is_reference(mode)) {
1806 ir_node *left = get_binop_left(n);
1807 ir_node *right = get_binop_right(n);
1808 int ref_bits = get_mode_size_bits(mode);
1810 if (get_irn_op(left) == op_Conv) {
1811 ir_mode *mode = get_irn_mode(left);
1812 int bits = get_mode_size_bits(mode);
1814 if (ref_bits == bits &&
1815 mode_is_int(mode) &&
1816 get_mode_arithmetic(mode) == irma_twos_complement) {
1817 ir_node *pre = get_Conv_op(left);
1818 ir_mode *pre_mode = get_irn_mode(pre);
1820 if (mode_is_int(pre_mode) &&
1821 get_mode_size_bits(pre_mode) == bits &&
1822 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1823 /* ok, this conv just changes to sign, moreover the calculation
1824 * is done with same number of bits as our address mode, so
1825 * we can ignore the conv as address calculation can be viewed
1826 * as either signed or unsigned
1828 set_binop_left(n, pre);
1833 if (get_irn_op(right) == op_Conv) {
1834 ir_mode *mode = get_irn_mode(right);
1835 int bits = get_mode_size_bits(mode);
1837 if (ref_bits == bits &&
1838 mode_is_int(mode) &&
1839 get_mode_arithmetic(mode) == irma_twos_complement) {
1840 ir_node *pre = get_Conv_op(right);
1841 ir_mode *pre_mode = get_irn_mode(pre);
1843 if (mode_is_int(pre_mode) &&
1844 get_mode_size_bits(pre_mode) == bits &&
1845 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1846 /* ok, this conv just changes to sign, moreover the calculation
1847 * is done with same number of bits as our address mode, so
1848 * we can ignore the conv as address calculation can be viewed
1849 * as either signed or unsigned
1851 set_binop_right(n, pre);
1857 } /* transform_node_AddSub */
1859 #define HANDLE_BINOP_PHI(op,a,b,c) \
1861 if (is_Const(b) && is_const_Phi(a)) { \
1862 /* check for Op(Phi, Const) */ \
1863 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1865 else if (is_Const(a) && is_const_Phi(b)) { \
1866 /* check for Op(Const, Phi) */ \
1867 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1870 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1874 #define HANDLE_UNOP_PHI(op,a,c) \
1876 if (is_const_Phi(a)) { \
1877 /* check for Op(Phi) */ \
1878 c = apply_unop_on_phi(a, op); \
1880 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1886 * Do the AddSub optimization, then Transform
1887 * Constant folding on Phi
1888 * Add(a,a) -> Mul(a, 2)
1889 * Add(Mul(a, x), a) -> Mul(a, x+1)
1890 * if the mode is integer or float.
1891 * Transform Add(a,-b) into Sub(a,b).
1892 * Reassociation might fold this further.
1894 static ir_node *transform_node_Add(ir_node *n) {
1896 ir_node *a, *b, *c, *oldn = n;
1898 n = transform_node_AddSub(n);
1900 a = get_Add_left(n);
1901 b = get_Add_right(n);
1903 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1905 mode = get_irn_mode(n);
1907 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1908 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1911 if (mode_is_num(mode)) {
1912 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1913 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1914 ir_node *block = get_irn_n(n, -1);
1917 get_irn_dbg_info(n),
1921 new_r_Const_long(current_ir_graph, block, mode, 2),
1923 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1928 get_irn_dbg_info(n),
1934 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1939 get_irn_dbg_info(n),
1945 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1948 if (! get_opt_reassociation()) {
1949 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1951 ir_node *ma = get_Mul_left(a);
1952 ir_node *mb = get_Mul_right(a);
1955 ir_node *blk = get_irn_n(n, -1);
1957 get_irn_dbg_info(n), current_ir_graph, blk,
1960 get_irn_dbg_info(n), current_ir_graph, blk,
1962 new_r_Const_long(current_ir_graph, blk, mode, 1),
1965 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1967 } else if (b == mb) {
1968 ir_node *blk = get_irn_n(n, -1);
1970 get_irn_dbg_info(n), current_ir_graph, blk,
1973 get_irn_dbg_info(n), current_ir_graph, blk,
1975 new_r_Const_long(current_ir_graph, blk, mode, 1),
1978 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1983 ir_node *ma = get_Mul_left(b);
1984 ir_node *mb = get_Mul_right(b);
1987 ir_node *blk = get_irn_n(n, -1);
1989 get_irn_dbg_info(n), current_ir_graph, blk,
1992 get_irn_dbg_info(n), current_ir_graph, blk,
1994 new_r_Const_long(current_ir_graph, blk, mode, 1),
1997 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2001 ir_node *blk = get_irn_n(n, -1);
2003 get_irn_dbg_info(n), current_ir_graph, blk,
2006 get_irn_dbg_info(n), current_ir_graph, blk,
2008 new_r_Const_long(current_ir_graph, blk, mode, 1),
2011 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2016 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2017 /* Here we rely on constants be on the RIGHT side */
2019 ir_node *op = get_Not_op(a);
2021 if (classify_Const(b) == CNST_ONE) {
2023 ir_node *blk = get_irn_n(n, -1);
2024 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2025 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2030 ir_node *blk = get_irn_n(n, -1);
2031 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2032 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2037 ir_node *op = get_Not_op(b);
2041 ir_node *blk = get_irn_n(n, -1);
2042 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2043 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2050 } /* transform_node_Add */
2053 static ir_node *const_negate(ir_node *cnst) {
2054 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2055 dbg_info *dbgi = get_irn_dbg_info(cnst);
2056 ir_graph *irg = get_irn_irg(cnst);
2057 ir_node *block = get_nodes_block(cnst);
2058 ir_mode *mode = get_irn_mode(cnst);
2059 if (tv == tarval_bad) return NULL;
2060 return new_rd_Const(dbgi, irg, block, mode, tv);
2064 * Do the AddSub optimization, then Transform
2065 * Constant folding on Phi
2066 * Sub(0,a) -> Minus(a)
2067 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2068 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2069 * Sub(Add(a, x), x) -> a
2070 * Sub(x, Add(x, a)) -> -a
2071 * Sub(x, Const) -> Add(x, -Const)
2073 static ir_node *transform_node_Sub(ir_node *n) {
2078 n = transform_node_AddSub(n);
2080 a = get_Sub_left(n);
2081 b = get_Sub_right(n);
2083 mode = get_irn_mode(n);
2086 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2088 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2089 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2092 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2093 /* a - C -> a + (-C) */
2094 ir_node *cnst = const_negate(b);
2096 ir_node *block = get_nodes_block(n);
2097 dbg_info *dbgi = get_irn_dbg_info(n);
2098 ir_graph *irg = get_irn_irg(n);
2100 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2106 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2107 ir_graph *irg = current_ir_graph;
2108 dbg_info *dbg = get_irn_dbg_info(n);
2109 ir_node *block = get_nodes_block(n);
2110 ir_node *left = get_Minus_op(a);
2111 ir_mode *mode = get_irn_mode(n);
2112 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2114 n = new_rd_Minus(dbg, irg, block, add, mode);
2115 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2117 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2118 ir_graph *irg = current_ir_graph;
2119 dbg_info *dbg = get_irn_dbg_info(n);
2120 ir_node *block = get_nodes_block(n);
2121 ir_node *right = get_Minus_op(b);
2122 ir_mode *mode = get_irn_mode(n);
2124 n = new_rd_Add(dbg, irg, block, a, right, mode);
2125 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2127 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2128 ir_graph *irg = current_ir_graph;
2129 dbg_info *s_dbg = get_irn_dbg_info(b);
2130 ir_node *s_block = get_nodes_block(b);
2131 ir_node *s_left = get_Sub_right(b);
2132 ir_node *s_right = get_Sub_left(b);
2133 ir_mode *s_mode = get_irn_mode(b);
2134 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2135 dbg_info *a_dbg = get_irn_dbg_info(n);
2136 ir_node *a_block = get_nodes_block(n);
2137 ir_mode *a_mode = get_irn_mode(n);
2139 n = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2140 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2142 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2143 ir_node *m_right = get_Mul_right(b);
2144 if (is_Const(m_right)) {
2145 ir_node *cnst2 = const_negate(m_right);
2146 if (cnst2 != NULL) {
2147 ir_graph *irg = current_ir_graph;
2148 dbg_info *m_dbg = get_irn_dbg_info(b);
2149 ir_node *m_block = get_nodes_block(b);
2150 ir_node *m_left = get_Mul_left(b);
2151 ir_mode *m_mode = get_irn_mode(b);
2152 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2153 dbg_info *a_dbg = get_irn_dbg_info(n);
2154 ir_node *a_block = get_nodes_block(n);
2155 ir_mode *a_mode = get_irn_mode(n);
2157 n = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2158 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2164 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2165 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2167 get_irn_dbg_info(n),
2172 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2176 if (mode_wrap_around(mode)) {
2177 ir_node *left = get_Add_left(a);
2178 ir_node *right = get_Add_right(a);
2180 /* FIXME: Does the Conv's work only for two complement or generally? */
2182 if (mode != get_irn_mode(right)) {
2183 /* This Sub is an effective Cast */
2184 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2189 } else if (right == b) {
2190 if (mode != get_irn_mode(left)) {
2191 /* This Sub is an effective Cast */
2192 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2195 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2201 if (mode_wrap_around(mode)) {
2202 ir_node *left = get_Add_left(b);
2203 ir_node *right = get_Add_right(b);
2205 /* FIXME: Does the Conv's work only for two complement or generally? */
2207 ir_mode *r_mode = get_irn_mode(right);
2209 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2210 if (mode != r_mode) {
2211 /* This Sub is an effective Cast */
2212 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2214 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2216 } else if (right == a) {
2217 ir_mode *l_mode = get_irn_mode(left);
2219 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2220 if (mode != l_mode) {
2221 /* This Sub is an effective Cast */
2222 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2224 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2229 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2230 ir_mode *mode = get_irn_mode(a);
2232 if (mode == get_irn_mode(b)) {
2238 /* check if it's allowed to skip the conv */
2239 ma = get_irn_mode(a);
2240 mb = get_irn_mode(b);
2242 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2243 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2245 set_Sub_right(n, b);
2251 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2252 if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2253 ir_node *ma = get_Mul_left(a);
2254 ir_node *mb = get_Mul_right(a);
2257 ir_node *blk = get_irn_n(n, -1);
2259 get_irn_dbg_info(n),
2260 current_ir_graph, blk,
2263 get_irn_dbg_info(n),
2264 current_ir_graph, blk,
2266 new_r_Const_long(current_ir_graph, blk, mode, 1),
2269 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2271 } else if (mb == b) {
2272 ir_node *blk = get_irn_n(n, -1);
2274 get_irn_dbg_info(n),
2275 current_ir_graph, blk,
2278 get_irn_dbg_info(n),
2279 current_ir_graph, blk,
2281 new_r_Const_long(current_ir_graph, blk, mode, 1),
2284 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2289 ir_node *x = get_Sub_left(a);
2290 ir_node *y = get_Sub_right(a);
2291 ir_node *blk = get_irn_n(n, -1);
2292 ir_mode *m_b = get_irn_mode(b);
2293 ir_mode *m_y = get_irn_mode(y);
2296 /* Determine the right mode for the Add. */
2299 else if (mode_is_reference(m_b))
2301 else if (mode_is_reference(m_y))
2305 * Both modes are different but none is reference,
2306 * happens for instance in SubP(SubP(P, Iu), Is).
2307 * We have two possibilities here: Cast or ignore.
2308 * Currently we ignore this case.
2313 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2315 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2316 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2320 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2321 if (is_Const(a) && is_Not(b)) {
2322 /* c - ~X = X + (c+1) */
2323 tarval *tv = get_Const_tarval(a);
2325 tv = tarval_add(tv, get_mode_one(mode));
2326 if (tv != tarval_bad) {
2327 ir_node *blk = get_irn_n(n, -1);
2328 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2329 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2330 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2336 } /* transform_node_Sub */
2339 * Several transformation done on n*n=2n bits mul.
2340 * These transformations must be done here because new nodes may be produced.
2342 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2344 ir_node *a = get_Mul_left(n);
2345 ir_node *b = get_Mul_right(n);
2346 tarval *ta = value_of(a);
2347 tarval *tb = value_of(b);
2348 ir_mode *smode = get_irn_mode(a);
2350 if (ta == get_mode_one(smode)) {
2351 ir_node *blk = get_irn_n(n, -1);
2352 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2353 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2356 else if (ta == get_mode_minus_one(smode)) {
2357 ir_node *blk = get_irn_n(n, -1);
2358 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2359 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2360 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2363 if (tb == get_mode_one(smode)) {
2364 ir_node *blk = get_irn_n(a, -1);
2365 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2369 else if (tb == get_mode_minus_one(smode)) {
2370 ir_node *blk = get_irn_n(n, -1);
2371 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2372 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2373 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2380 * Transform Mul(a,-1) into -a.
2381 * Do constant evaluation of Phi nodes.
2382 * Do architecture dependent optimizations on Mul nodes
2384 static ir_node *transform_node_Mul(ir_node *n) {
2385 ir_node *c, *oldn = n;
2386 ir_mode *mode = get_irn_mode(n);
2387 ir_node *a = get_Mul_left(n);
2388 ir_node *b = get_Mul_right(n);
2390 if (is_Bad(a) || is_Bad(b))
2393 if (mode != get_irn_mode(a))
2394 return transform_node_Mul2n(n, mode);
2396 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2398 if (mode_is_signed(mode)) {
2401 if (value_of(a) == get_mode_minus_one(mode))
2403 else if (value_of(b) == get_mode_minus_one(mode))
2406 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2407 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2412 if (is_Const(b)) { /* (-a) * const -> a * -const */
2413 ir_node *cnst = const_negate(b);
2415 dbg_info *dbgi = get_irn_dbg_info(n);
2416 ir_node *block = get_nodes_block(n);
2417 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2418 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2421 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2422 dbg_info *dbgi = get_irn_dbg_info(n);
2423 ir_node *block = get_nodes_block(n);
2424 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2425 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2427 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2428 ir_node *sub_l = get_Sub_left(b);
2429 ir_node *sub_r = get_Sub_right(b);
2430 dbg_info *dbgi = get_irn_dbg_info(n);
2431 ir_graph *irg = current_ir_graph;
2432 ir_node *block = get_nodes_block(n);
2433 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2434 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2435 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2438 } else if (is_Minus(b)) {
2439 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2440 ir_node *sub_l = get_Sub_left(a);
2441 ir_node *sub_r = get_Sub_right(a);
2442 dbg_info *dbgi = get_irn_dbg_info(n);
2443 ir_graph *irg = current_ir_graph;
2444 ir_node *block = get_nodes_block(n);
2445 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2446 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2447 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2451 if (get_mode_arithmetic(mode) == irma_ieee754) {
2453 tarval *tv = get_Const_tarval(a);
2454 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2455 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2456 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2460 else if (is_Const(b)) {
2461 tarval *tv = get_Const_tarval(b);
2462 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2463 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2464 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2469 return arch_dep_replace_mul_with_shifts(n);
2470 } /* transform_node_Mul */
2473 * Transform a Div Node.
2475 static ir_node *transform_node_Div(ir_node *n) {
2476 tarval *tv = value_of(n);
2477 ir_mode *mode = get_Div_resmode(n);
2480 if (tv != tarval_bad) {
2481 value = new_Const(get_tarval_mode(tv), tv);
2483 DBG_OPT_CSTEVAL(n, value);
2486 ir_node *a = get_Div_left(n);
2487 ir_node *b = get_Div_right(n);
2490 if (a == b && value_not_zero(a, &dummy)) {
2491 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2492 value = new_Const(mode, get_mode_one(mode));
2493 DBG_OPT_CSTEVAL(n, value);
2496 if (mode_is_signed(mode) && is_Const(b)) {
2497 tarval *tv = get_Const_tarval(b);
2499 if (tv == get_mode_minus_one(mode)) {
2501 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2502 DBG_OPT_CSTEVAL(n, value);
2506 /* Try architecture dependent optimization */
2507 value = arch_dep_replace_div_by_const(n);
2515 /* Turn Div into a tuple (mem, jmp, bad, value) */
2516 mem = get_Div_mem(n);
2517 blk = get_irn_n(n, -1);
2519 /* skip a potential Pin */
2521 mem = get_Pin_op(mem);
2522 turn_into_tuple(n, pn_Div_max);
2523 set_Tuple_pred(n, pn_Div_M, mem);
2524 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2525 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2526 set_Tuple_pred(n, pn_Div_res, value);
2529 } /* transform_node_Div */
2532 * Transform a Mod node.
2534 static ir_node *transform_node_Mod(ir_node *n) {
2535 tarval *tv = value_of(n);
2536 ir_mode *mode = get_Mod_resmode(n);
2539 if (tv != tarval_bad) {
2540 value = new_Const(get_tarval_mode(tv), tv);
2542 DBG_OPT_CSTEVAL(n, value);
2545 ir_node *a = get_Mod_left(n);
2546 ir_node *b = get_Mod_right(n);
2549 if (a == b && value_not_zero(a, &dummy)) {
2550 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2551 value = new_Const(mode, get_mode_null(mode));
2552 DBG_OPT_CSTEVAL(n, value);
2555 if (mode_is_signed(mode) && is_Const(b)) {
2556 tarval *tv = get_Const_tarval(b);
2558 if (tv == get_mode_minus_one(mode)) {
2560 value = new_Const(mode, get_mode_null(mode));
2561 DBG_OPT_CSTEVAL(n, value);
2565 /* Try architecture dependent optimization */
2566 value = arch_dep_replace_mod_by_const(n);
2574 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2575 mem = get_Mod_mem(n);
2576 blk = get_irn_n(n, -1);
2578 /* skip a potential Pin */
2580 mem = get_Pin_op(mem);
2581 turn_into_tuple(n, pn_Mod_max);
2582 set_Tuple_pred(n, pn_Mod_M, mem);
2583 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2584 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2585 set_Tuple_pred(n, pn_Mod_res, value);
2588 } /* transform_node_Mod */
2591 * Transform a DivMod node.
2593 static ir_node *transform_node_DivMod(ir_node *n) {
2595 ir_node *a = get_DivMod_left(n);
2596 ir_node *b = get_DivMod_right(n);
2597 ir_mode *mode = get_DivMod_resmode(n);
2598 tarval *ta = value_of(a);
2599 tarval *tb = value_of(b);
2602 if (tb != tarval_bad) {
2603 if (tb == get_mode_one(get_tarval_mode(tb))) {
2604 b = new_Const(mode, get_mode_null(mode));
2605 DBG_OPT_CSTEVAL(n, b);
2607 } else if (ta != tarval_bad) {
2608 tarval *resa, *resb;
2609 resa = tarval_div(ta, tb);
2610 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2611 Jmp for X result!? */
2612 resb = tarval_mod(ta, tb);
2613 if (resb == tarval_bad) return n; /* Causes exception! */
2614 a = new_Const(mode, resa);
2615 b = new_Const(mode, resb);
2616 DBG_OPT_CSTEVAL(n, a);
2617 DBG_OPT_CSTEVAL(n, b);
2619 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2620 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2621 b = new_Const(mode, get_mode_null(mode));
2622 DBG_OPT_CSTEVAL(n, a);
2623 DBG_OPT_CSTEVAL(n, b);
2625 } else { /* Try architecture dependent optimization */
2626 arch_dep_replace_divmod_by_const(&a, &b, n);
2627 evaluated = a != NULL;
2629 } else if (a == b) {
2630 if (value_not_zero(a, &dummy)) {
2632 a = new_Const(mode, get_mode_one(mode));
2633 b = new_Const(mode, get_mode_null(mode));
2634 DBG_OPT_CSTEVAL(n, a);
2635 DBG_OPT_CSTEVAL(n, b);
2638 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2641 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2642 /* 0 / non-Const = 0 */
2647 if (evaluated) { /* replace by tuple */
2651 mem = get_DivMod_mem(n);
2652 /* skip a potential Pin */
2654 mem = get_Pin_op(mem);
2656 blk = get_irn_n(n, -1);
2657 turn_into_tuple(n, pn_DivMod_max);
2658 set_Tuple_pred(n, pn_DivMod_M, mem);
2659 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2660 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2661 set_Tuple_pred(n, pn_DivMod_res_div, a);
2662 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2666 } /* transform_node_DivMod */
2669 * Optimize x / c to x * (1/c)
2671 static ir_node *transform_node_Quot(ir_node *n) {
2672 ir_mode *mode = get_Quot_resmode(n);
2675 if (get_mode_arithmetic(mode) == irma_ieee754) {
2676 ir_node *b = get_Quot_right(n);
2679 tarval *tv = get_Const_tarval(b);
2681 tv = tarval_quo(get_mode_one(mode), tv);
2683 /* Do the transformation if the result is either exact or we are not
2684 using strict rules. */
2685 if (tv != tarval_bad &&
2686 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2687 ir_node *blk = get_irn_n(n, -1);
2688 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2689 ir_node *a = get_Quot_left(n);
2690 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2691 ir_node *mem = get_Quot_mem(n);
2693 /* skip a potential Pin */
2695 mem = get_Pin_op(mem);
2696 turn_into_tuple(n, pn_Quot_max);
2697 set_Tuple_pred(n, pn_Quot_M, mem);
2698 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2699 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2700 set_Tuple_pred(n, pn_Quot_res, m);
2701 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2706 } /* transform_node_Quot */
2709 * Optimize Abs(x) into x if x is Confirmed >= 0
2710 * Optimize Abs(x) into -x if x is Confirmed <= 0
2712 static ir_node *transform_node_Abs(ir_node *n) {
2714 ir_node *a = get_Abs_op(n);
2715 value_classify_sign sign = classify_value_sign(a);
2717 if (sign == value_classified_negative) {
2718 ir_mode *mode = get_irn_mode(n);
2721 * We can replace the Abs by -x here.
2722 * We even could add a new Confirm here.
2724 * Note that -x would create a new node, so we could
2725 * not run it in the equivalent_node() context.
2727 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2728 get_irn_n(n, -1), a, mode);
2730 DBG_OPT_CONFIRM(oldn, n);
2731 } else if (sign == value_classified_positive) {
2732 /* n is positive, Abs is not needed */
2735 DBG_OPT_CONFIRM(oldn, n);
2739 } /* transform_node_Abs */
2742 * Transform a Cond node.
2744 * Replace the Cond by a Jmp if it branches on a constant
2747 static ir_node *transform_node_Cond(ir_node *n) {
2750 ir_node *a = get_Cond_selector(n);
2751 tarval *ta = value_of(a);
2753 /* we need block info which is not available in floating irgs */
2754 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2757 if ((ta != tarval_bad) &&
2758 (get_irn_mode(a) == mode_b) &&
2759 (get_opt_unreachable_code())) {
2760 /* It's a boolean Cond, branching on a boolean constant.
2761 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2762 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2763 turn_into_tuple(n, pn_Cond_max);
2764 if (ta == tarval_b_true) {
2765 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2766 set_Tuple_pred(n, pn_Cond_true, jmp);
2768 set_Tuple_pred(n, pn_Cond_false, jmp);
2769 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2771 /* We might generate an endless loop, so keep it alive. */
2772 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2775 } /* transform_node_Cond */
2777 typedef ir_node* (*recursive_transform) (ir_node *n);
2780 * makes use of distributive laws for and, or, eor
2781 * and(a OP c, b OP c) -> and(a, b) OP c
2782 * note, might return a different op than n
2784 static ir_node *transform_bitwise_distributive(ir_node *n,
2785 recursive_transform trans_func)
2788 ir_node *a = get_binop_left(n);
2789 ir_node *b = get_binop_right(n);
2790 ir_op *op = get_irn_op(a);
2791 ir_op *op_root = get_irn_op(n);
2793 if(op != get_irn_op(b))
2796 if (op == op_Conv) {
2797 ir_node *a_op = get_Conv_op(a);
2798 ir_node *b_op = get_Conv_op(b);
2799 ir_mode *a_mode = get_irn_mode(a_op);
2800 ir_mode *b_mode = get_irn_mode(b_op);
2801 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2802 ir_node *blk = get_irn_n(n, -1);
2805 set_binop_left(n, a_op);
2806 set_binop_right(n, b_op);
2807 set_irn_mode(n, a_mode);
2809 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2811 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2817 /* nothing to gain here */
2821 if (op == op_Shrs || op == op_Shr || op == op_Shl
2822 || op == op_And || op == op_Or || op == op_Eor) {
2823 ir_node *a_left = get_binop_left(a);
2824 ir_node *a_right = get_binop_right(a);
2825 ir_node *b_left = get_binop_left(b);
2826 ir_node *b_right = get_binop_right(b);
2830 if (is_op_commutative(op)) {
2831 if (a_left == b_left) {
2835 } else if(a_left == b_right) {
2839 } else if(a_right == b_left) {
2845 if(a_right == b_right) {
2852 /* (a sop c) & (b sop c) => (a & b) sop c */
2853 ir_node *blk = get_irn_n(n, -1);
2855 ir_node *new_n = exact_copy(n);
2856 set_binop_left(new_n, op1);
2857 set_binop_right(new_n, op2);
2858 new_n = trans_func(new_n);
2860 if(op_root == op_Eor && op == op_Or) {
2861 dbg_info *dbgi = get_irn_dbg_info(n);
2862 ir_graph *irg = current_ir_graph;
2863 ir_mode *mode = get_irn_mode(c);
2865 c = new_rd_Not(dbgi, irg, blk, c, mode);
2866 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2869 set_irn_n(n, -1, blk);
2870 set_binop_left(n, new_n);
2871 set_binop_right(n, c);
2874 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2885 static ir_node *transform_node_And(ir_node *n) {
2886 ir_node *c, *oldn = n;
2887 ir_node *a = get_And_left(n);
2888 ir_node *b = get_And_right(n);
2891 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2893 mode = get_irn_mode(n);
2895 /* we can evaluate 2 Projs of the same Cmp */
2896 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2897 ir_node *pred_a = get_Proj_pred(a);
2898 ir_node *pred_b = get_Proj_pred(b);
2899 if (pred_a == pred_b) {
2900 dbg_info *dbgi = get_irn_dbg_info(n);
2901 ir_node *block = get_nodes_block(pred_a);
2902 pn_Cmp pn_a = get_Proj_proj(a);
2903 pn_Cmp pn_b = get_Proj_proj(b);
2904 /* yes, we can simply calculate with pncs */
2905 pn_Cmp new_pnc = pn_a & pn_b;
2907 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2912 ir_node *op = get_Not_op(b);
2914 ir_node *ba = get_And_left(op);
2915 ir_node *bb = get_And_right(op);
2917 /* it's enough to test the following cases due to normalization! */
2918 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2919 /* (a|b) & ~(a&b) = a^b */
2920 ir_node *block = get_nodes_block(n);
2922 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2923 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2931 ir_node *op = get_Not_op(a);
2933 ir_node *aa = get_And_left(op);
2934 ir_node *ab = get_And_right(op);
2936 /* it's enough to test the following cases due to normalization! */
2937 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2938 /* (a|b) & ~(a&b) = a^b */
2939 ir_node *block = get_nodes_block(n);
2941 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2942 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2949 ir_node *al = get_Eor_left(a);
2950 ir_node *ar = get_Eor_right(a);
2953 /* (b ^ a) & b -> ~a & b */
2954 dbg_info *dbg = get_irn_dbg_info(n);
2955 ir_node *block = get_nodes_block(n);
2957 ar = new_rd_Minus(dbg, current_ir_graph, block, ar, mode);
2958 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
2959 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2963 /* (a ^ b) & b -> ~a & b */
2964 dbg_info *dbg = get_irn_dbg_info(n);
2965 ir_node *block = get_nodes_block(n);
2967 al = new_rd_Minus(dbg, current_ir_graph, block, al, mode);
2968 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
2969 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2974 ir_node *bl = get_Eor_left(b);
2975 ir_node *br = get_Eor_right(b);
2978 /* a & (a ^ b) -> a & ~b */
2979 dbg_info *dbg = get_irn_dbg_info(n);
2980 ir_node *block = get_nodes_block(n);
2982 br = new_rd_Minus(dbg, current_ir_graph, block, br, mode);
2983 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
2984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2988 /* a & (b ^ a) -> a & ~b */
2989 dbg_info *dbg = get_irn_dbg_info(n);
2990 ir_node *block = get_nodes_block(n);
2992 bl = new_rd_Minus(dbg, current_ir_graph, block, bl, mode);
2993 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
2994 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2998 if (is_Not(a) && is_Not(b)) {
2999 /* ~a & ~b = ~(a|b) */
3000 ir_node *block = get_nodes_block(n);
3001 ir_mode *mode = get_irn_mode(n);
3005 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3006 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3007 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3011 n = transform_bitwise_distributive(n, transform_node_And);
3014 } /* transform_node_And */
3019 static ir_node *transform_node_Eor(ir_node *n) {
3020 ir_node *c, *oldn = n;
3021 ir_node *a = get_Eor_left(n);
3022 ir_node *b = get_Eor_right(n);
3023 ir_mode *mode = get_irn_mode(n);
3025 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
3027 /* we can evaluate 2 Projs of the same Cmp */
3028 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3029 ir_node *pred_a = get_Proj_pred(a);
3030 ir_node *pred_b = get_Proj_pred(b);
3031 if(pred_a == pred_b) {
3032 dbg_info *dbgi = get_irn_dbg_info(n);
3033 ir_node *block = get_nodes_block(pred_a);
3034 pn_Cmp pn_a = get_Proj_proj(a);
3035 pn_Cmp pn_b = get_Proj_proj(b);
3036 /* yes, we can simply calculate with pncs */
3037 pn_Cmp new_pnc = pn_a ^ pn_b;
3039 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3046 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3047 mode, get_mode_null(mode));
3048 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3049 } else if ((mode == mode_b)
3050 && (get_irn_op(a) == op_Proj)
3051 && (get_irn_mode(a) == mode_b)
3052 && tarval_is_one(value_of(b))
3053 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3054 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3055 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3056 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3058 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3059 } else if (mode == mode_b && tarval_is_one(value_of(b))) {
3060 /* The Eor is a Not. Replace it by a Not. */
3061 /* ????!!!Extend to bitfield 1111111. */
3062 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
3064 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3066 n = transform_bitwise_distributive(n, transform_node_Eor);
3070 } /* transform_node_Eor */
3075 static ir_node *transform_node_Not(ir_node *n) {
3076 ir_node *c, *oldn = n;
3077 ir_node *a = get_Not_op(n);
3078 ir_op *op_a = get_irn_op(a);
3080 HANDLE_UNOP_PHI(tarval_not,a,c);
3082 /* check for a boolean Not */
3083 if ( (get_irn_mode(n) == mode_b)
3084 && (op_a == op_Proj)
3085 && (get_irn_mode(a) == mode_b)
3086 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3087 /* We negate a Cmp. The Cmp has the negated result anyways! */
3088 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3089 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3090 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3093 if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
3095 ir_node *op = get_Sub_left(a);
3096 ir_node *blk = get_irn_n(n, -1);
3097 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3101 } /* transform_node_Not */
3104 * Transform a Minus.
3109 static ir_node *transform_node_Minus(ir_node *n) {
3110 ir_node *c, *oldn = n;
3111 ir_node *a = get_Minus_op(n);
3114 HANDLE_UNOP_PHI(tarval_neg,a,c);
3116 mode = get_irn_mode(a);
3117 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3118 /* the following rules are only to twos-complement */
3121 ir_node *op = get_Not_op(a);
3122 tarval *tv = get_mode_one(mode);
3123 ir_node *blk = get_irn_n(n, -1);
3124 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3125 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3126 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3130 ir_node *c = get_Shr_right(a);
3133 tarval *tv = get_Const_tarval(c);
3135 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3136 /* -(a >>u (size-1)) = a >>s (size-1) */
3137 ir_node *v = get_Shr_left(a);
3139 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3140 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3146 ir_node *c = get_Shrs_right(a);
3149 tarval *tv = get_Const_tarval(c);
3151 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3152 /* -(a >>s (size-1)) = a >>u (size-1) */
3153 ir_node *v = get_Shrs_left(a);
3155 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3156 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3163 /* - (a-b) = b - a */
3164 ir_node *la = get_Sub_left(a);
3165 ir_node *ra = get_Sub_right(a);
3166 ir_node *blk = get_irn_n(n, -1);
3168 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3169 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3174 } /* transform_node_Minus */
3177 * Transform a Cast_type(Const) into a new Const_type
3179 static ir_node *transform_node_Cast(ir_node *n) {
3181 ir_node *pred = get_Cast_op(n);
3182 ir_type *tp = get_irn_type(n);
3184 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
3185 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3186 get_Const_tarval(pred), tp);
3187 DBG_OPT_CSTEVAL(oldn, n);
3188 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
3189 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3190 get_SymConst_kind(pred), tp);
3191 DBG_OPT_CSTEVAL(oldn, n);
3195 } /* transform_node_Cast */
3198 * Transform a Proj(Div) with a non-zero value.
3199 * Removes the exceptions and routes the memory to the NoMem node.
3201 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3202 ir_node *div = get_Proj_pred(proj);
3203 ir_node *b = get_Div_right(div);
3204 ir_node *confirm, *res, *new_mem;
3207 if (value_not_zero(b, &confirm)) {
3208 /* div(x, y) && y != 0 */
3209 proj_nr = get_Proj_proj(proj);
3211 case pn_Div_X_regular:
3212 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3214 case pn_Div_X_except:
3215 /* we found an exception handler, remove it */
3216 DBG_OPT_EXC_REM(proj);
3220 res = get_Div_mem(div);
3221 new_mem = get_irg_no_mem(current_ir_graph);
3224 /* This node can only float up to the Confirm block */
3225 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3227 set_irn_pinned(div, op_pin_state_floats);
3228 /* this is a Div without exception, we can remove the memory edge */
3229 set_Div_mem(div, new_mem);
3234 } /* transform_node_Proj_Div */
3237 * Transform a Proj(Mod) with a non-zero value.
3238 * Removes the exceptions and routes the memory to the NoMem node.
3240 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3241 ir_node *mod = get_Proj_pred(proj);
3242 ir_node *b = get_Mod_right(mod);
3243 ir_node *confirm, *res, *new_mem;
3246 if (value_not_zero(b, &confirm)) {
3247 /* mod(x, y) && y != 0 */
3248 proj_nr = get_Proj_proj(proj);
3252 case pn_Mod_X_regular:
3253 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3255 case pn_Mod_X_except:
3256 /* we found an exception handler, remove it */
3257 DBG_OPT_EXC_REM(proj);
3261 res = get_Mod_mem(mod);
3262 new_mem = get_irg_no_mem(current_ir_graph);
3265 /* This node can only float up to the Confirm block */
3266 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3268 set_irn_pinned(mod, op_pin_state_floats);
3269 /* this is a Mod without exception, we can remove the memory edge */
3270 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3273 if (get_Mod_left(mod) == b) {
3274 /* a % a = 0 if a != 0 */
3275 ir_mode *mode = get_irn_mode(proj);
3276 ir_node *res = new_Const(mode, get_mode_null(mode));
3278 DBG_OPT_CSTEVAL(mod, res);
3284 } /* transform_node_Proj_Mod */
3287 * Transform a Proj(DivMod) with a non-zero value.
3288 * Removes the exceptions and routes the memory to the NoMem node.
3290 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3291 ir_node *divmod = get_Proj_pred(proj);
3292 ir_node *b = get_DivMod_right(divmod);
3293 ir_node *confirm, *res, *new_mem;
3296 if (value_not_zero(b, &confirm)) {
3297 /* DivMod(x, y) && y != 0 */
3298 proj_nr = get_Proj_proj(proj);
3302 case pn_DivMod_X_regular:
3303 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3305 case pn_DivMod_X_except:
3306 /* we found an exception handler, remove it */
3307 DBG_OPT_EXC_REM(proj);
3311 res = get_DivMod_mem(divmod);
3312 new_mem = get_irg_no_mem(current_ir_graph);
3315 /* This node can only float up to the Confirm block */
3316 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3318 set_irn_pinned(divmod, op_pin_state_floats);
3319 /* this is a DivMod without exception, we can remove the memory edge */
3320 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3323 case pn_DivMod_res_mod:
3324 if (get_DivMod_left(divmod) == b) {
3325 /* a % a = 0 if a != 0 */
3326 ir_mode *mode = get_irn_mode(proj);
3327 ir_node *res = new_Const(mode, get_mode_null(mode));
3329 DBG_OPT_CSTEVAL(divmod, res);
3335 } /* transform_node_Proj_DivMod */
3338 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3340 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3341 if (get_opt_unreachable_code()) {
3342 ir_node *n = get_Proj_pred(proj);
3343 ir_node *b = get_Cond_selector(n);
3345 if (mode_is_int(get_irn_mode(b))) {
3346 tarval *tb = value_of(b);
3348 if (tb != tarval_bad) {
3349 /* we have a constant switch */
3350 long num = get_Proj_proj(proj);
3352 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3353 if (get_tarval_long(tb) == num) {
3354 /* Do NOT create a jump here, or we will have 2 control flow ops
3355 * in a block. This case is optimized away in optimize_cf(). */
3358 /* this case will NEVER be taken, kill it */
3366 } /* transform_node_Proj_Cond */
3369 * Normalizes and optimizes Cmp nodes.
3371 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3372 ir_node *n = get_Proj_pred(proj);
3373 ir_node *left = get_Cmp_left(n);
3374 ir_node *right = get_Cmp_right(n);
3378 ir_mode *mode = NULL;
3379 long proj_nr = get_Proj_proj(proj);
3381 /* we can evaluate this direct */
3384 return new_Const(mode_b, get_tarval_b_false());
3386 return new_Const(mode_b, get_tarval_b_true());
3388 if(!mode_is_float(get_irn_mode(left)))
3389 return new_Const(mode_b, get_tarval_b_true());
3395 /* Remove unnecessary conversions */
3396 /* TODO handle constants */
3397 if (is_Conv(left) && is_Conv(right)) {
3398 ir_mode *mode = get_irn_mode(left);
3399 ir_node *op_left = get_Conv_op(left);
3400 ir_node *op_right = get_Conv_op(right);
3401 ir_mode *mode_left = get_irn_mode(op_left);
3402 ir_mode *mode_right = get_irn_mode(op_right);
3404 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3405 ir_graph *irg = current_ir_graph;
3406 ir_node *block = get_nodes_block(n);
3408 if (mode_left == mode_right) {
3412 } else if (smaller_mode(mode_left, mode_right)) {
3413 left = new_r_Conv(irg, block, op_left, mode_right);
3416 } else if (smaller_mode(mode_right, mode_left)) {
3418 right = new_r_Conv(irg, block, op_right, mode_left);
3424 /* TODO extend to arbitrary constants */
3425 if (is_Conv(left) &&
3427 tarval_is_null(get_Const_tarval(right))) {
3428 ir_mode* mode = get_irn_mode(left);
3429 ir_node* op = get_Conv_op(left);
3430 ir_mode* op_mode = get_irn_mode(op);
3432 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3433 (mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3434 ir_node *null = new_Const(op_mode, get_mode_null(op_mode));
3435 set_Cmp_left( n, op);
3436 set_Cmp_right(n, null);
3443 left = get_Cast_op(left);
3445 right = get_Cast_op(right);
3447 /* remove operation of both sides if possible */
3448 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3449 ir_opcode lop = get_irn_opcode(left);
3451 if (lop == get_irn_opcode(right)) {
3452 ir_node *ll, *lr, *rl, *rr;
3454 /* same operation on both sides, try to remove */
3458 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3459 left = get_unop_op(left);
3460 right = get_unop_op(right);
3464 ll = get_Add_left(left);
3465 lr = get_Add_right(left);
3466 rl = get_Add_left(right);
3467 rr = get_Add_right(right);
3470 /* X + a CMP X + b ==> a CMP b */
3474 } else if (ll == rr) {
3475 /* X + a CMP b + X ==> a CMP b */
3479 } else if (lr == rl) {
3480 /* a + X CMP X + b ==> a CMP b */
3484 } else if (lr == rr) {
3485 /* a + X CMP b + X ==> a CMP b */
3492 ll = get_Sub_left(left);
3493 lr = get_Sub_right(left);
3494 rl = get_Sub_left(right);
3495 rr = get_Sub_right(right);
3498 /* X - a CMP X - b ==> a CMP b */
3502 } else if (lr == rr) {
3503 /* a - X CMP b - X ==> a CMP b */
3510 if (get_Rot_right(left) == get_Rot_right(right)) {
3511 /* a ROT X CMP b ROT X */
3512 left = get_Rot_left(left);
3513 right = get_Rot_left(right);
3523 if (get_irn_mode(left) == mode_b) {
3524 ir_graph *irg = current_ir_graph;
3525 ir_node *block = get_nodes_block(n);
3528 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3529 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3530 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3531 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3532 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3533 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3537 if (!get_opt_reassociation())
3541 * First step: normalize the compare op
3542 * by placing the constant on the right side
3543 * or moving the lower address node to the left.
3544 * We ignore the case that both are constants
3545 * this case should be optimized away.
3547 if (is_Const(right)) {
3549 } else if (is_Const(left)) {
3554 proj_nr = get_inversed_pnc(proj_nr);
3556 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3562 proj_nr = get_inversed_pnc(proj_nr);
3567 * Second step: Try to reduce the magnitude
3568 * of a constant. This may help to generate better code
3569 * later and may help to normalize more compares.
3570 * Of course this is only possible for integer values.
3573 mode = get_irn_mode(c);
3574 tv = get_Const_tarval(c);
3576 if (tv != tarval_bad) {
3577 /* the following optimization is possible on modes without Overflow
3578 * on Unary Minus or on == and !=:
3579 * -a CMP c ==> a swap(CMP) -c
3581 * Beware: for two-complement Overflow may occur, so only == and != can
3582 * be optimized, see this:
3583 * -MININT < 0 =/=> MININT > 0 !!!
3585 if (is_Minus(left) &&
3586 (!mode_overflow_on_unary_Minus(mode) ||
3587 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3588 tv = tarval_neg(tv);
3590 if (tv != tarval_bad) {
3591 left = get_Minus_op(left);
3592 proj_nr = get_inversed_pnc(proj_nr);
3595 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3596 tv = tarval_not(tv);
3598 if (tv != tarval_bad) {
3599 left = get_Not_op(left);
3604 /* for integer modes, we have more */
3605 if (mode_is_int(mode)) {
3606 /* Ne includes Unordered which is not possible on integers.
3607 * However, frontends often use this wrong, so fix it here */
3608 if (proj_nr & pn_Cmp_Uo) {
3609 proj_nr &= ~pn_Cmp_Uo;
3610 set_Proj_proj(proj, proj_nr);
3613 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3614 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3615 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3616 tv = tarval_sub(tv, get_mode_one(mode));
3618 if (tv != tarval_bad) {
3619 proj_nr ^= pn_Cmp_Eq;
3623 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3624 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3625 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3626 tv = tarval_add(tv, get_mode_one(mode));
3628 if (tv != tarval_bad) {
3629 proj_nr ^= pn_Cmp_Eq;
3634 /* the following reassociations work only for == and != */
3635 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3637 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3638 if (tarval_is_null(tv) && is_Sub(left)) {
3639 right =get_Sub_right(left);
3640 left = get_Sub_left(left);
3642 tv = value_of(right);
3646 if (tv != tarval_bad) {
3647 ir_op *op = get_irn_op(left);
3649 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3651 ir_node *c1 = get_Sub_right(left);
3652 tarval *tv2 = value_of(c1);
3654 if (tv2 != tarval_bad) {
3655 tv2 = tarval_add(tv, value_of(c1));
3657 if (tv2 != tarval_bad) {
3658 left = get_Sub_left(left);
3664 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3665 else if (op == op_Add) {
3666 ir_node *a_l = get_Add_left(left);
3667 ir_node *a_r = get_Add_right(left);
3671 if (get_irn_op(a_l) == op_Const) {
3673 tv2 = value_of(a_l);
3676 tv2 = value_of(a_r);
3679 if (tv2 != tarval_bad) {
3680 tv2 = tarval_sub(tv, tv2);
3682 if (tv2 != tarval_bad) {
3689 /* -a == c ==> a == -c, -a != c ==> a != -c */
3690 else if (op == op_Minus) {
3691 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3693 if (tv2 != tarval_bad) {
3694 left = get_Minus_op(left);
3701 /* the following reassociations work only for <= */
3702 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3703 if (tv != tarval_bad) {
3704 ir_op *op = get_irn_op(left);
3706 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3714 * optimization for AND:
3716 * And(x, C) == C ==> And(x, C) != 0
3717 * And(x, C) != C ==> And(X, C) == 0
3719 * if C is a single Bit constant.
3721 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3722 if (tarval_is_single_bit(tv)) {
3723 /* check for Constant's match. We have check hare the tarvals,
3724 because our const might be changed */
3725 ir_node *la = get_And_left(left);
3726 ir_node *ra = get_And_right(left);
3727 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3728 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3729 /* fine: do the transformation */
3730 tv = get_mode_null(get_tarval_mode(tv));
3731 proj_nr ^= pn_Cmp_Leg;
3736 } /* tarval != bad */
3740 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3742 if (changed & 2) /* need a new Const */
3743 right = new_Const(mode, tv);
3745 /* create a new compare */
3746 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3748 set_Proj_pred(proj, n);
3749 set_Proj_proj(proj, proj_nr);
3753 } /* transform_node_Proj_Cmp */
3756 * Does all optimizations on nodes that must be done on it's Proj's
3757 * because of creating new nodes.
3759 static ir_node *transform_node_Proj(ir_node *proj) {
3760 ir_node *n = get_Proj_pred(proj);
3762 switch (get_irn_opcode(n)) {
3764 return transform_node_Proj_Div(proj);
3767 return transform_node_Proj_Mod(proj);
3770 return transform_node_Proj_DivMod(proj);
3773 return transform_node_Proj_Cond(proj);
3776 return transform_node_Proj_Cmp(proj);
3779 /* should not happen, but if it does will be optimized away */
3780 return equivalent_node_Proj(proj);
3786 } /* transform_node_Proj */
3789 * Move Confirms down through Phi nodes.
3791 static ir_node *transform_node_Phi(ir_node *phi) {
3793 ir_mode *mode = get_irn_mode(phi);
3795 if (mode_is_reference(mode)) {
3796 n = get_irn_arity(phi);
3798 /* Beware of Phi0 */
3800 ir_node *pred = get_irn_n(phi, 0);
3801 ir_node *bound, *new_Phi, *block, **in;
3804 if (! is_Confirm(pred))
3807 bound = get_Confirm_bound(pred);
3808 pnc = get_Confirm_cmp(pred);
3810 NEW_ARR_A(ir_node *, in, n);
3811 in[0] = get_Confirm_value(pred);
3813 for (i = 1; i < n; ++i) {
3814 pred = get_irn_n(phi, i);
3816 if (! is_Confirm(pred) ||
3817 get_Confirm_bound(pred) != bound ||
3818 get_Confirm_cmp(pred) != pnc)
3820 in[i] = get_Confirm_value(pred);
3822 /* move the Confirm nodes "behind" the Phi */
3823 block = get_irn_n(phi, -1);
3824 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3825 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3829 } /* transform_node_Phi */
3832 * Returns the operands of a commutative bin-op, if one operand is
3833 * a const, it is returned as the second one.
3835 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3836 ir_node *op_a = get_binop_left(binop);
3837 ir_node *op_b = get_binop_right(binop);
3839 assert(is_op_commutative(get_irn_op(binop)));
3841 if (get_irn_op(op_a) == op_Const) {
3848 } /* get_comm_Binop_Ops */
3851 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3852 * Such pattern may arise in bitfield stores.
3854 * value c4 value c4 & c2
3855 * AND c3 AND c1 | c3
3862 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3865 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3868 ir_node *and_l, *c3;
3869 ir_node *value, *c4;
3870 ir_node *new_and, *new_const, *block;
3871 ir_mode *mode = get_irn_mode(or);
3873 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3876 get_comm_Binop_Ops(or, &and, &c1);
3877 if (!is_Const(c1) || !is_And(and))
3880 get_comm_Binop_Ops(and, &or_l, &c2);
3884 tv1 = get_Const_tarval(c1);
3885 tv2 = get_Const_tarval(c2);
3887 tv = tarval_or(tv1, tv2);
3888 if (tarval_is_all_one(tv)) {
3889 /* the AND does NOT clear a bit with isn't set by the OR */
3890 set_Or_left(or, or_l);
3891 set_Or_right(or, c1);
3893 /* check for more */
3900 get_comm_Binop_Ops(or_l, &and_l, &c3);
3901 if (!is_Const(c3) || !is_And(and_l))
3904 get_comm_Binop_Ops(and_l, &value, &c4);
3908 /* ok, found the pattern, check for conditions */
3909 assert(mode == get_irn_mode(and));
3910 assert(mode == get_irn_mode(or_l));
3911 assert(mode == get_irn_mode(and_l));
3913 tv3 = get_Const_tarval(c3);
3914 tv4 = get_Const_tarval(c4);
3916 tv = tarval_or(tv4, tv2);
3917 if (!tarval_is_all_one(tv)) {
3918 /* have at least one 0 at the same bit position */
3922 n_tv4 = tarval_not(tv4);
3923 if (tv3 != tarval_and(tv3, n_tv4)) {
3924 /* bit in the or_mask is outside the and_mask */
3928 n_tv2 = tarval_not(tv2);
3929 if (tv1 != tarval_and(tv1, n_tv2)) {
3930 /* bit in the or_mask is outside the and_mask */
3934 /* ok, all conditions met */
3935 block = get_irn_n(or, -1);
3937 new_and = new_r_And(current_ir_graph, block,
3938 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3940 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3942 set_Or_left(or, new_and);
3943 set_Or_right(or, new_const);
3945 /* check for more */
3947 } /* transform_node_Or_bf_store */
3950 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3952 static ir_node *transform_node_Or_Rot(ir_node *or) {
3953 ir_mode *mode = get_irn_mode(or);
3954 ir_node *shl, *shr, *block;
3955 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3958 if (! mode_is_int(mode))
3961 shl = get_binop_left(or);
3962 shr = get_binop_right(or);
3964 if (get_irn_op(shl) == op_Shr) {
3965 if (get_irn_op(shr) != op_Shl)
3971 } else if (get_irn_op(shl) != op_Shl) {
3973 } else if (get_irn_op(shr) != op_Shr) {
3976 x = get_Shl_left(shl);
3977 if (x != get_Shr_left(shr))
3980 c1 = get_Shl_right(shl);
3981 c2 = get_Shr_right(shr);
3982 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3983 tv1 = get_Const_tarval(c1);
3984 if (! tarval_is_long(tv1))
3987 tv2 = get_Const_tarval(c2);
3988 if (! tarval_is_long(tv2))
3991 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3992 != get_mode_size_bits(mode))
3995 /* yet, condition met */
3996 block = get_irn_n(or, -1);
3998 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4000 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4002 } else if (get_irn_op(c1) == op_Sub) {
4006 if (get_Sub_right(sub) != v)
4009 c1 = get_Sub_left(sub);
4010 if (get_irn_op(c1) != op_Const)
4013 tv1 = get_Const_tarval(c1);
4014 if (! tarval_is_long(tv1))
4017 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4020 /* yet, condition met */
4021 block = get_nodes_block(or);
4023 /* a Rot right is not supported, so use a rot left */
4024 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
4026 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4028 } else if (get_irn_op(c2) == op_Sub) {
4032 c1 = get_Sub_left(sub);
4033 if (get_irn_op(c1) != op_Const)
4036 tv1 = get_Const_tarval(c1);
4037 if (! tarval_is_long(tv1))
4040 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4043 /* yet, condition met */
4044 block = get_irn_n(or, -1);
4047 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4049 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4054 } /* transform_node_Or_Rot */
4059 static ir_node *transform_node_Or(ir_node *n) {
4060 ir_node *c, *oldn = n;
4061 ir_node *a = get_Or_left(n);
4062 ir_node *b = get_Or_right(n);
4064 if (is_Not(a) && is_Not(b)) {
4065 /* ~a | ~b = ~(a&b) */
4066 ir_node *block = get_nodes_block(n);
4067 ir_mode *mode = get_irn_mode(n);
4071 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4072 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4073 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4077 /* we can evaluate 2 Projs of the same Cmp */
4078 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4079 ir_node *pred_a = get_Proj_pred(a);
4080 ir_node *pred_b = get_Proj_pred(b);
4081 if (pred_a == pred_b) {
4082 dbg_info *dbgi = get_irn_dbg_info(n);
4083 ir_node *block = get_nodes_block(pred_a);
4084 pn_Cmp pn_a = get_Proj_proj(a);
4085 pn_Cmp pn_b = get_Proj_proj(b);
4086 /* yes, we can simply calculate with pncs */
4087 pn_Cmp new_pnc = pn_a | pn_b;
4089 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4094 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4096 n = transform_node_Or_bf_store(n);
4097 n = transform_node_Or_Rot(n);
4101 n = transform_bitwise_distributive(n, transform_node_Or);
4104 } /* transform_node_Or */
4108 static ir_node *transform_node(ir_node *n);
4111 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4113 * Should be moved to reassociation?
4115 static ir_node *transform_node_shift(ir_node *n) {
4116 ir_node *left, *right;
4117 tarval *tv1, *tv2, *res;
4119 int modulo_shf, flag;
4121 left = get_binop_left(n);
4123 /* different operations */
4124 if (get_irn_op(left) != get_irn_op(n))
4127 right = get_binop_right(n);
4128 tv1 = value_of(right);
4129 if (tv1 == tarval_bad)
4132 tv2 = value_of(get_binop_right(left));
4133 if (tv2 == tarval_bad)
4136 res = tarval_add(tv1, tv2);
4138 /* beware: a simple replacement works only, if res < modulo shift */
4139 mode = get_irn_mode(n);
4143 modulo_shf = get_mode_modulo_shift(mode);
4144 if (modulo_shf > 0) {
4145 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4147 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4153 /* ok, we can replace it */
4154 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4156 in[0] = get_binop_left(left);
4157 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4159 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4161 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4163 return transform_node(irn);
4166 } /* transform_node_shift */
4171 static ir_node *transform_node_Shr(ir_node *n) {
4172 ir_node *c, *oldn = n;
4173 ir_node *a = get_Shr_left(n);
4174 ir_node *b = get_Shr_right(n);
4176 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4177 return transform_node_shift(n);
4178 } /* transform_node_Shr */
4183 static ir_node *transform_node_Shrs(ir_node *n) {
4184 ir_node *c, *oldn = n;
4185 ir_node *a = get_Shrs_left(n);
4186 ir_node *b = get_Shrs_right(n);
4188 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4189 return transform_node_shift(n);
4190 } /* transform_node_Shrs */
4195 static ir_node *transform_node_Shl(ir_node *n) {
4196 ir_node *c, *oldn = n;
4197 ir_node *a = get_Shl_left(n);
4198 ir_node *b = get_Shl_right(n);
4200 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4201 return transform_node_shift(n);
4202 } /* transform_node_Shl */
4205 * Remove dead blocks and nodes in dead blocks
4206 * in keep alive list. We do not generate a new End node.
4208 static ir_node *transform_node_End(ir_node *n) {
4209 int i, j, n_keepalives = get_End_n_keepalives(n);
4212 NEW_ARR_A(ir_node *, in, n_keepalives);
4214 for (i = j = 0; i < n_keepalives; ++i) {
4215 ir_node *ka = get_End_keepalive(n, i);
4217 if (! is_Block_dead(ka)) {
4221 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4224 /* FIXME: beabi need to keep a Proj(M) */
4225 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4228 if (j != n_keepalives)
4229 set_End_keepalives(n, j, in);
4231 } /* transform_node_End */
4233 /** returns 1 if a == -b */
4234 static int is_negated_value(ir_node *a, ir_node *b) {
4235 if(is_Minus(a) && get_Minus_op(a) == b)
4237 if(is_Minus(b) && get_Minus_op(b) == a)
4239 if(is_Sub(a) && is_Sub(b)) {
4240 ir_node *a_left = get_Sub_left(a);
4241 ir_node *a_right = get_Sub_right(a);
4242 ir_node *b_left = get_Sub_left(b);
4243 ir_node *b_right = get_Sub_right(b);
4245 if(a_left == b_right && a_right == b_left)
4253 * Optimize a Mux into some simpler cases.
4255 static ir_node *transform_node_Mux(ir_node *n) {
4256 ir_node *oldn = n, *sel = get_Mux_sel(n);
4257 ir_mode *mode = get_irn_mode(n);
4259 if (mode == mode_b) {
4260 ir_node *t = get_Mux_true(n);
4261 ir_node *f = get_Mux_false(n);
4262 dbg_info *dbg = get_irn_dbg_info(n);
4263 ir_node *block = get_irn_n(n, -1);
4264 ir_graph *irg = current_ir_graph;
4267 tarval *tv_t = get_Const_tarval(t);
4268 if (tv_t == tarval_b_true) {
4270 assert(get_Const_tarval(f) == tarval_b_false);
4273 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4276 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4277 assert(tv_t == tarval_b_false);
4279 assert(get_Const_tarval(f) == tarval_b_true);
4282 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4285 } else if (is_Const(f)) {
4286 tarval *tv_f = get_Const_tarval(f);
4287 if (tv_f == tarval_b_true) {
4288 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4289 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4291 assert(tv_f == tarval_b_false);
4292 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4297 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
4298 ir_node *cmp = get_Proj_pred(sel);
4299 long pn = get_Proj_proj(sel);
4300 ir_node *f = get_Mux_false(n);
4301 ir_node *t = get_Mux_true(n);
4304 * Note: normalization puts the constant on the right side,
4305 * so we check only one case.
4307 * Note further that these optimization work even for floating point
4308 * with NaN's because -NaN == NaN.
4309 * However, if +0 and -0 is handled differently, we cannot use the first
4312 if (get_irn_op(cmp) == op_Cmp
4313 && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
4314 ir_node *block = get_irn_n(n, -1);
4316 if(is_negated_value(f, t)) {
4317 ir_node *cmp_left = get_Cmp_left(cmp);
4319 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4320 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4321 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4323 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4325 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4327 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4328 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4329 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4331 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4333 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4335 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4340 if (mode_is_int(mode) && mode_is_signed(mode) &&
4341 get_mode_arithmetic(mode) == irma_twos_complement) {
4342 ir_node *x = get_Cmp_left(cmp);
4344 /* the following optimization works only with signed integer two-complement mode */
4346 if (mode == get_irn_mode(x)) {
4348 * FIXME: this restriction is two rigid, as it would still
4349 * work if mode(x) = Hs and mode == Is, but at least it removes
4352 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4353 classify_Const(t) == CNST_ALL_ONE &&
4354 classify_Const(f) == CNST_NULL) {
4356 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4360 n = new_rd_Shrs(get_irn_dbg_info(n),
4361 current_ir_graph, block, x,
4362 new_r_Const_long(current_ir_graph, block, mode_Iu,
4363 get_mode_size_bits(mode) - 1),
4365 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4367 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4368 classify_Const(t) == CNST_ONE &&
4369 classify_Const(f) == CNST_NULL) {
4371 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4375 n = new_rd_Shr(get_irn_dbg_info(n),
4376 current_ir_graph, block,
4377 new_r_Minus(current_ir_graph, block, x, mode),
4378 new_r_Const_long(current_ir_graph, block, mode_Iu,
4379 get_mode_size_bits(mode) - 1),
4381 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4388 return arch_transform_node_Mux(n);
4389 } /* transform_node_Mux */
4392 * Optimize a Psi into some simpler cases.
4394 static ir_node *transform_node_Psi(ir_node *n) {
4396 return transform_node_Mux(n);
4399 } /* transform_node_Psi */
4402 * Tries several [inplace] [optimizing] transformations and returns an
4403 * equivalent node. The difference to equivalent_node() is that these
4404 * transformations _do_ generate new nodes, and thus the old node must
4405 * not be freed even if the equivalent node isn't the old one.
4407 static ir_node *transform_node(ir_node *n) {
4411 * Transform_node is the only "optimizing transformation" that might
4412 * return a node with a different opcode. We iterate HERE until fixpoint
4413 * to get the final result.
4417 if (n->op->ops.transform_node)
4418 n = n->op->ops.transform_node(n);
4419 } while (oldn != n);
4422 } /* transform_node */
4425 * Sets the default transform node operation for an ir_op_ops.
4427 * @param code the opcode for the default operation
4428 * @param ops the operations initialized
4433 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4437 ops->transform_node = transform_node_##a; \
4471 } /* firm_set_default_transform_node */
4474 /* **************** Common Subexpression Elimination **************** */
4476 /** The size of the hash table used, should estimate the number of nodes
4478 #define N_IR_NODES 512
4480 /** Compares the attributes of two Const nodes. */
4481 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4482 return (get_Const_tarval(a) != get_Const_tarval(b))
4483 || (get_Const_type(a) != get_Const_type(b));
4484 } /* node_cmp_attr_Const */
4486 /** Compares the attributes of two Proj nodes. */
4487 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4488 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4489 } /* node_cmp_attr_Proj */
4491 /** Compares the attributes of two Filter nodes. */
4492 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4493 return get_Filter_proj(a) != get_Filter_proj(b);
4494 } /* node_cmp_attr_Filter */
4496 /** Compares the attributes of two Alloc nodes. */
4497 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4498 const alloc_attr *pa = get_irn_alloc_attr(a);
4499 const alloc_attr *pb = get_irn_alloc_attr(b);
4500 return (pa->where != pb->where) || (pa->type != pb->type);
4501 } /* node_cmp_attr_Alloc */
4503 /** Compares the attributes of two Free nodes. */
4504 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4505 const free_attr *pa = get_irn_free_attr(a);
4506 const free_attr *pb = get_irn_free_attr(b);
4507 return (pa->where != pb->where) || (pa->type != pb->type);
4508 } /* node_cmp_attr_Free */
4510 /** Compares the attributes of two SymConst nodes. */
4511 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4512 const symconst_attr *pa = get_irn_symconst_attr(a);
4513 const symconst_attr *pb = get_irn_symconst_attr(b);
4514 return (pa->num != pb->num)
4515 || (pa->sym.type_p != pb->sym.type_p)
4516 || (pa->tp != pb->tp);
4517 } /* node_cmp_attr_SymConst */
4519 /** Compares the attributes of two Call nodes. */
4520 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4521 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4522 } /* node_cmp_attr_Call */
4524 /** Compares the attributes of two Sel nodes. */
4525 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4526 const ir_entity *a_ent = get_Sel_entity(a);
4527 const ir_entity *b_ent = get_Sel_entity(b);
4529 (a_ent->kind != b_ent->kind) ||
4530 (a_ent->name != b_ent->name) ||
4531 (a_ent->owner != b_ent->owner) ||
4532 (a_ent->ld_name != b_ent->ld_name) ||
4533 (a_ent->type != b_ent->type);
4534 } /* node_cmp_attr_Sel */
4536 /** Compares the attributes of two Phi nodes. */
4537 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4538 /* we can only enter this function if both nodes have the same number of inputs,
4539 hence it is enough to check if one of them is a Phi0 */
4541 /* check the Phi0 attribute */
4542 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4545 } /* node_cmp_attr_Phi */
4547 /** Compares the attributes of two Conv nodes. */
4548 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4549 return get_Conv_strict(a) != get_Conv_strict(b);
4550 } /* node_cmp_attr_Conv */
4552 /** Compares the attributes of two Cast nodes. */
4553 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4554 return get_Cast_type(a) != get_Cast_type(b);
4555 } /* node_cmp_attr_Cast */
4557 /** Compares the attributes of two Load nodes. */
4558 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4559 if (get_Load_volatility(a) == volatility_is_volatile ||
4560 get_Load_volatility(b) == volatility_is_volatile)
4561 /* NEVER do CSE on volatile Loads */
4563 /* do not CSE Loads with different alignment. Be conservative. */
4564 if (get_Load_align(a) != get_Load_align(b))
4567 return get_Load_mode(a) != get_Load_mode(b);
4568 } /* node_cmp_attr_Load */
4570 /** Compares the attributes of two Store nodes. */
4571 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4572 /* do not CSE Stores with different alignment. Be conservative. */
4573 if (get_Store_align(a) != get_Store_align(b))
4576 /* NEVER do CSE on volatile Stores */
4577 return (get_Store_volatility(a) == volatility_is_volatile ||
4578 get_Store_volatility(b) == volatility_is_volatile);
4579 } /* node_cmp_attr_Store */
4581 /** Compares the attributes of two Confirm nodes. */
4582 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4583 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4584 } /* node_cmp_attr_Confirm */
4586 /** Compares the attributes of two ASM nodes. */
4587 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4589 const ir_asm_constraint *ca;
4590 const ir_asm_constraint *cb;
4593 if (get_ASM_text(a) != get_ASM_text(b))
4596 /* Should we really check the constraints here? Should be better, but is strange. */
4597 n = get_ASM_n_input_constraints(a);
4598 if (n != get_ASM_n_input_constraints(b))
4601 ca = get_ASM_input_constraints(a);
4602 cb = get_ASM_input_constraints(b);
4603 for (i = 0; i < n; ++i) {
4604 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4608 n = get_ASM_n_output_constraints(a);
4609 if (n != get_ASM_n_output_constraints(b))
4612 ca = get_ASM_output_constraints(a);
4613 cb = get_ASM_output_constraints(b);
4614 for (i = 0; i < n; ++i) {
4615 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4619 n = get_ASM_n_clobbers(a);
4620 if (n != get_ASM_n_clobbers(b))
4623 cla = get_ASM_clobbers(a);
4624 clb = get_ASM_clobbers(b);
4625 for (i = 0; i < n; ++i) {
4626 if (cla[i] != clb[i])
4630 } /* node_cmp_attr_ASM */
4633 * Set the default node attribute compare operation for an ir_op_ops.
4635 * @param code the opcode for the default operation
4636 * @param ops the operations initialized
4641 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4645 ops->node_cmp_attr = node_cmp_attr_##a; \
4670 } /* firm_set_default_node_cmp_attr */
4673 * Compare function for two nodes in the hash table. Gets two
4674 * nodes as parameters. Returns 0 if the nodes are a cse.
4676 int identities_cmp(const void *elt, const void *key) {
4683 if (a == b) return 0;
4685 if ((get_irn_op(a) != get_irn_op(b)) ||
4686 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4688 /* compare if a's in and b's in are of equal length */
4689 irn_arity_a = get_irn_intra_arity (a);
4690 if (irn_arity_a != get_irn_intra_arity(b))
4693 /* for block-local cse and op_pin_state_pinned nodes: */
4694 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4695 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4699 /* compare a->in[0..ins] with b->in[0..ins] */
4700 for (i = 0; i < irn_arity_a; i++)
4701 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4705 * here, we already now that the nodes are identical except their
4708 if (a->op->ops.node_cmp_attr)
4709 return a->op->ops.node_cmp_attr(a, b);
4712 } /* identities_cmp */
4715 * Calculate a hash value of a node.
4717 unsigned ir_node_hash(ir_node *node) {
4721 if (node->op == op_Const) {
4722 /* special value for const, as they only differ in their tarval. */
4723 h = HASH_PTR(node->attr.con.tv);
4724 h = 9*h + HASH_PTR(get_irn_mode(node));
4725 } else if (node->op == op_SymConst) {
4726 /* special value for const, as they only differ in their symbol. */
4727 h = HASH_PTR(node->attr.symc.sym.type_p);
4728 h = 9*h + HASH_PTR(get_irn_mode(node));
4731 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4732 h = irn_arity = get_irn_intra_arity(node);
4734 /* consider all in nodes... except the block if not a control flow. */
4735 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4736 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4740 h = 9*h + HASH_PTR(get_irn_mode(node));
4742 h = 9*h + HASH_PTR(get_irn_op(node));
4746 } /* ir_node_hash */
4748 pset *new_identities(void) {
4749 return new_pset(identities_cmp, N_IR_NODES);
4750 } /* new_identities */
4752 void del_identities(pset *value_table) {
4753 del_pset(value_table);
4754 } /* del_identities */
4757 * Normalize a node by putting constants (and operands with smaller
4758 * node index) on the right
4760 * @param n The node to normalize
4762 static void normalize_node(ir_node *n) {
4763 if (get_opt_reassociation()) {
4764 if (is_op_commutative(get_irn_op(n))) {
4765 ir_node *l = get_binop_left(n);
4766 ir_node *r = get_binop_right(n);
4767 int l_idx = get_irn_idx(l);
4768 int r_idx = get_irn_idx(r);
4770 /* For commutative operators perform a OP b == b OP a but keep
4771 constants on the RIGHT side. This helps greatly in some optimizations.
4772 Moreover we use the idx number to make the form deterministic. */
4773 if (is_irn_constlike(l))
4775 if (is_irn_constlike(r))
4777 if (l_idx < r_idx) {
4778 set_binop_left(n, r);
4779 set_binop_right(n, l);
4783 } /* normalize_node */
4786 * Return the canonical node computing the same value as n.
4788 * @param value_table The value table
4789 * @param n The node to lookup
4791 * Looks up the node in a hash table.
4793 * For Const nodes this is performed in the constructor, too. Const
4794 * nodes are extremely time critical because of their frequent use in
4795 * constant string arrays.
4797 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4800 if (!value_table) return n;
4804 o = pset_find(value_table, n, ir_node_hash(n));
4813 * During construction we set the op_pin_state_pinned flag in the graph right when the
4814 * optimization is performed. The flag turning on procedure global cse could
4815 * be changed between two allocations. This way we are safe.
4817 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4820 n = identify(value_table, n);
4821 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4822 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4824 } /* identify_cons */
4827 * Return the canonical node computing the same value as n.
4828 * Looks up the node in a hash table, enters it in the table
4829 * if it isn't there yet.
4831 ir_node *identify_remember(pset *value_table, ir_node *n) {
4834 if (!value_table) return n;
4837 /* lookup or insert in hash table with given hash key. */
4838 o = pset_insert(value_table, n, ir_node_hash(n));
4845 } /* identify_remember */
4847 /* Add a node to the identities value table. */
4848 void add_identities(pset *value_table, ir_node *node) {
4849 if (get_opt_cse() && is_no_Block(node))
4850 identify_remember(value_table, node);
4851 } /* add_identities */
4853 /* Visit each node in the value table of a graph. */
4854 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4856 ir_graph *rem = current_ir_graph;
4858 current_ir_graph = irg;
4859 foreach_pset(irg->value_table, node)
4861 current_ir_graph = rem;
4862 } /* visit_all_identities */
4865 * Garbage in, garbage out. If a node has a dead input, i.e., the
4866 * Bad node is input to the node, return the Bad node.
4868 static ir_node *gigo(ir_node *node) {
4870 ir_op *op = get_irn_op(node);
4872 /* remove garbage blocks by looking at control flow that leaves the block
4873 and replacing the control flow by Bad. */
4874 if (get_irn_mode(node) == mode_X) {
4875 ir_node *block = get_nodes_block(skip_Proj(node));
4877 /* Don't optimize nodes in immature blocks. */
4878 if (!get_Block_matured(block)) return node;
4879 /* Don't optimize End, may have Bads. */
4880 if (op == op_End) return node;
4882 if (is_Block(block)) {
4883 irn_arity = get_irn_arity(block);
4884 for (i = 0; i < irn_arity; i++) {
4885 if (!is_Bad(get_irn_n(block, i)))
4888 if (i == irn_arity) {
4889 ir_graph *irg = get_irn_irg(block);
4890 /* the start block is never dead */
4891 if (block != get_irg_start_block(irg)
4892 && block != get_irg_end_block(irg))
4898 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4899 blocks predecessors is dead. */
4900 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4901 irn_arity = get_irn_arity(node);
4904 * Beware: we can only read the block of a non-floating node.
4906 if (is_irn_pinned_in_irg(node) &&
4907 is_Block_dead(get_nodes_block(node)))
4910 for (i = 0; i < irn_arity; i++) {
4911 ir_node *pred = get_irn_n(node, i);
4916 /* Propagating Unknowns here seems to be a bad idea, because
4917 sometimes we need a node as a input and did not want that
4919 However, it might be useful to move this into a later phase
4920 (if you think that optimizing such code is useful). */
4921 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4922 return new_Unknown(get_irn_mode(node));
4927 /* With this code we violate the agreement that local_optimize
4928 only leaves Bads in Block, Phi and Tuple nodes. */
4929 /* If Block has only Bads as predecessors it's garbage. */
4930 /* If Phi has only Bads as predecessors it's garbage. */
4931 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4932 irn_arity = get_irn_arity(node);
4933 for (i = 0; i < irn_arity; i++) {
4934 if (!is_Bad(get_irn_n(node, i))) break;
4936 if (i == irn_arity) node = new_Bad();
4943 * These optimizations deallocate nodes from the obstack.
4944 * It can only be called if it is guaranteed that no other nodes
4945 * reference this one, i.e., right after construction of a node.
4947 * @param n The node to optimize
4949 * current_ir_graph must be set to the graph of the node!
4951 ir_node *optimize_node(ir_node *n) {
4954 ir_opcode iro = get_irn_opcode(n);
4956 /* Always optimize Phi nodes: part of the construction. */
4957 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4959 /* constant expression evaluation / constant folding */
4960 if (get_opt_constant_folding()) {
4961 /* neither constants nor Tuple values can be evaluated */
4962 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4963 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4964 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4965 /* try to evaluate */
4966 tv = computed_value(n);
4967 if (tv != tarval_bad) {
4969 ir_type *old_tp = get_irn_type(n);
4970 int i, arity = get_irn_arity(n);
4974 * Try to recover the type of the new expression.
4976 for (i = 0; i < arity && !old_tp; ++i)
4977 old_tp = get_irn_type(get_irn_n(n, i));
4980 * we MUST copy the node here temporary, because it's still needed
4981 * for DBG_OPT_CSTEVAL
4983 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4984 oldn = alloca(node_size);
4986 memcpy(oldn, n, node_size);
4987 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4989 /* ARG, copy the in array, we need it for statistics */
4990 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4992 /* note the inplace edges module */
4993 edges_node_deleted(n, current_ir_graph);
4995 /* evaluation was successful -- replace the node. */
4996 irg_kill_node(current_ir_graph, n);
4997 nw = new_Const(get_tarval_mode(tv), tv);
4999 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5000 set_Const_type(nw, old_tp);
5001 DBG_OPT_CSTEVAL(oldn, nw);
5002 tarval_enable_fp_ops(old_fp_mode);
5005 tarval_enable_fp_ops(old_fp_mode);
5009 /* remove unnecessary nodes */
5010 if (get_opt_constant_folding() ||
5011 (iro == iro_Phi) || /* always optimize these nodes. */
5013 (iro == iro_Proj) ||
5014 (iro == iro_Block) ) /* Flags tested local. */
5015 n = equivalent_node(n);
5017 /* Common Subexpression Elimination.
5019 * Checks whether n is already available.
5020 * The block input is used to distinguish different subexpressions. Right
5021 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5022 * subexpressions within a block.
5025 n = identify_cons(current_ir_graph->value_table, n);
5028 edges_node_deleted(oldn, current_ir_graph);
5030 /* We found an existing, better node, so we can deallocate the old node. */
5031 irg_kill_node(current_ir_graph, oldn);
5035 /* Some more constant expression evaluation that does not allow to
5037 iro = get_irn_opcode(n);
5038 if (get_opt_constant_folding() ||
5039 (iro == iro_Cond) ||
5040 (iro == iro_Proj)) /* Flags tested local. */
5041 n = transform_node(n);
5043 /* Remove nodes with dead (Bad) input.
5044 Run always for transformation induced Bads. */
5047 /* Now we have a legal, useful node. Enter it in hash table for CSE */
5048 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5049 n = identify_remember(current_ir_graph->value_table, n);
5053 } /* optimize_node */
5057 * These optimizations never deallocate nodes (in place). This can cause dead
5058 * nodes lying on the obstack. Remove these by a dead node elimination,
5059 * i.e., a copying garbage collection.
5061 ir_node *optimize_in_place_2(ir_node *n) {
5064 ir_opcode iro = get_irn_opcode(n);
5066 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
5068 /* constant expression evaluation / constant folding */
5069 if (get_opt_constant_folding()) {
5070 /* neither constants nor Tuple values can be evaluated */
5071 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5072 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5073 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5074 /* try to evaluate */
5075 tv = computed_value(n);
5076 if (tv != tarval_bad) {
5077 /* evaluation was successful -- replace the node. */
5078 ir_type *old_tp = get_irn_type(n);
5079 int i, arity = get_irn_arity(n);
5082 * Try to recover the type of the new expression.
5084 for (i = 0; i < arity && !old_tp; ++i)
5085 old_tp = get_irn_type(get_irn_n(n, i));
5087 n = new_Const(get_tarval_mode(tv), tv);
5089 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5090 set_Const_type(n, old_tp);
5092 DBG_OPT_CSTEVAL(oldn, n);
5093 tarval_enable_fp_ops(old_fp_mode);
5096 tarval_enable_fp_ops(old_fp_mode);
5100 /* remove unnecessary nodes */
5101 if (get_opt_constant_folding() ||
5102 (iro == iro_Phi) || /* always optimize these nodes. */
5103 (iro == iro_Id) || /* ... */
5104 (iro == iro_Proj) || /* ... */
5105 (iro == iro_Block) ) /* Flags tested local. */
5106 n = equivalent_node(n);
5108 /** common subexpression elimination **/
5109 /* Checks whether n is already available. */
5110 /* The block input is used to distinguish different subexpressions. Right
5111 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5112 subexpressions within a block. */
5113 if (get_opt_cse()) {
5114 n = identify(current_ir_graph->value_table, n);
5117 /* Some more constant expression evaluation. */
5118 iro = get_irn_opcode(n);
5119 if (get_opt_constant_folding() ||
5120 (iro == iro_Cond) ||
5121 (iro == iro_Proj)) /* Flags tested local. */
5122 n = transform_node(n);
5124 /* Remove nodes with dead (Bad) input.
5125 Run always for transformation induced Bads. */
5128 /* Now we can verify the node, as it has no dead inputs any more. */
5131 /* Now we have a legal, useful node. Enter it in hash table for cse.
5132 Blocks should be unique anyways. (Except the successor of start:
5133 is cse with the start block!) */
5134 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5135 n = identify_remember(current_ir_graph->value_table, n);
5138 } /* optimize_in_place_2 */
5141 * Wrapper for external use, set proper status bits after optimization.
5143 ir_node *optimize_in_place(ir_node *n) {
5144 /* Handle graph state */
5145 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5147 if (get_opt_global_cse())
5148 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5149 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5150 set_irg_outs_inconsistent(current_ir_graph);
5152 /* FIXME: Maybe we could also test whether optimizing the node can
5153 change the control graph. */
5154 set_irg_doms_inconsistent(current_ir_graph);
5155 return optimize_in_place_2(n);
5156 } /* optimize_in_place */
5159 * Sets the default operation for an ir_ops.
5161 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5162 ops = firm_set_default_computed_value(code, ops);
5163 ops = firm_set_default_equivalent_node(code, ops);
5164 ops = firm_set_default_transform_node(code, ops);
5165 ops = firm_set_default_node_cmp_attr(code, ops);
5166 ops = firm_set_default_get_type(code, ops);
5167 ops = firm_set_default_get_type_attr(code, ops);
5168 ops = firm_set_default_get_entity_attr(code, ops);
5171 } /* firm_set_default_operations */