2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
46 #include "opt_confirms.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
60 * Returns the tarval of a Const node or tarval_bad for all other nodes.
62 static tarval *default_value_of(const ir_node *n)
65 return get_Const_tarval(n); /* might return tarval_bad */
70 value_of_func value_of_ptr = default_value_of;
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
87 } /* computed_value_Const */
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
118 } /* computed_value_SymConst */
121 * Return the value of an Add.
123 static tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 tarval *ta = value_of(a);
129 tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 } /* computed_value_Add */
138 * Return the value of a Sub.
139 * Special case: a - a
141 static tarval *computed_value_Sub(const ir_node *n)
143 ir_mode *mode = get_irn_mode(n);
144 ir_node *a = get_Sub_left(n);
145 ir_node *b = get_Sub_right(n);
150 if (! mode_is_float(mode)) {
153 return get_mode_null(mode);
159 if ((ta != tarval_bad) && (tb != tarval_bad))
160 return tarval_sub(ta, tb, mode);
163 } /* computed_value_Sub */
166 * Return the value of a Carry.
167 * Special : a op 0, 0 op b
169 static tarval *computed_value_Carry(const ir_node *n)
171 ir_node *a = get_binop_left(n);
172 ir_node *b = get_binop_right(n);
173 ir_mode *m = get_irn_mode(n);
175 tarval *ta = value_of(a);
176 tarval *tb = value_of(b);
178 if ((ta != tarval_bad) && (tb != tarval_bad)) {
180 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
182 if (tarval_is_null(ta) || tarval_is_null(tb))
183 return get_mode_null(m);
186 } /* computed_value_Carry */
189 * Return the value of a Borrow.
192 static tarval *computed_value_Borrow(const ir_node *n)
194 ir_node *a = get_binop_left(n);
195 ir_node *b = get_binop_right(n);
196 ir_mode *m = get_irn_mode(n);
198 tarval *ta = value_of(a);
199 tarval *tb = value_of(b);
201 if ((ta != tarval_bad) && (tb != tarval_bad)) {
202 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
203 } else if (tarval_is_null(ta)) {
204 return get_mode_null(m);
207 } /* computed_value_Borrow */
210 * Return the value of an unary Minus.
212 static tarval *computed_value_Minus(const ir_node *n)
214 ir_node *a = get_Minus_op(n);
215 tarval *ta = value_of(a);
217 if (ta != tarval_bad)
218 return tarval_neg(ta);
221 } /* computed_value_Minus */
224 * Return the value of a Mul.
226 static tarval *computed_value_Mul(const ir_node *n)
228 ir_node *a = get_Mul_left(n);
229 ir_node *b = get_Mul_right(n);
232 tarval *ta = value_of(a);
233 tarval *tb = value_of(b);
235 mode = get_irn_mode(n);
236 if (mode != get_irn_mode(a)) {
237 /* n * n = 2n bit multiplication */
238 ta = tarval_convert_to(ta, mode);
239 tb = tarval_convert_to(tb, mode);
242 if (ta != tarval_bad && tb != tarval_bad) {
243 return tarval_mul(ta, tb);
245 /* a * 0 != 0 if a == NaN or a == Inf */
246 if (!mode_is_float(mode)) {
247 /* a*0 = 0 or 0*b = 0 */
248 if (ta == get_mode_null(mode))
250 if (tb == get_mode_null(mode))
255 } /* computed_value_Mul */
258 * Return the value of an And.
259 * Special case: a & 0, 0 & b
261 static tarval *computed_value_And(const ir_node *n)
263 ir_node *a = get_And_left(n);
264 ir_node *b = get_And_right(n);
266 tarval *ta = value_of(a);
267 tarval *tb = value_of(b);
269 if ((ta != tarval_bad) && (tb != tarval_bad)) {
270 return tarval_and (ta, tb);
272 if (tarval_is_null(ta)) return ta;
273 if (tarval_is_null(tb)) return tb;
276 } /* computed_value_And */
279 * Return the value of an Or.
280 * Special case: a | 1...1, 1...1 | b
282 static tarval *computed_value_Or(const ir_node *n)
284 ir_node *a = get_Or_left(n);
285 ir_node *b = get_Or_right(n);
287 tarval *ta = value_of(a);
288 tarval *tb = value_of(b);
290 if ((ta != tarval_bad) && (tb != tarval_bad)) {
291 return tarval_or (ta, tb);
293 if (tarval_is_all_one(ta)) return ta;
294 if (tarval_is_all_one(tb)) return tb;
297 } /* computed_value_Or */
300 * Return the value of an Eor.
302 static tarval *computed_value_Eor(const ir_node *n)
304 ir_node *a = get_Eor_left(n);
305 ir_node *b = get_Eor_right(n);
310 return get_mode_null(get_irn_mode(n));
315 if ((ta != tarval_bad) && (tb != tarval_bad)) {
316 return tarval_eor(ta, tb);
319 } /* computed_value_Eor */
322 * Return the value of a Not.
324 static tarval *computed_value_Not(const ir_node *n)
326 ir_node *a = get_Not_op(n);
327 tarval *ta = value_of(a);
329 if (ta != tarval_bad)
330 return tarval_not(ta);
333 } /* computed_value_Not */
336 * Return the value of a Shl.
338 static tarval *computed_value_Shl(const ir_node *n)
340 ir_node *a = get_Shl_left(n);
341 ir_node *b = get_Shl_right(n);
343 tarval *ta = value_of(a);
344 tarval *tb = value_of(b);
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_shl(ta, tb);
350 } /* computed_value_Shl */
353 * Return the value of a Shr.
355 static tarval *computed_value_Shr(const ir_node *n)
357 ir_node *a = get_Shr_left(n);
358 ir_node *b = get_Shr_right(n);
360 tarval *ta = value_of(a);
361 tarval *tb = value_of(b);
363 if ((ta != tarval_bad) && (tb != tarval_bad)) {
364 return tarval_shr(ta, tb);
367 } /* computed_value_Shr */
370 * Return the value of a Shrs.
372 static tarval *computed_value_Shrs(const ir_node *n)
374 ir_node *a = get_Shrs_left(n);
375 ir_node *b = get_Shrs_right(n);
377 tarval *ta = value_of(a);
378 tarval *tb = value_of(b);
380 if ((ta != tarval_bad) && (tb != tarval_bad)) {
381 return tarval_shrs(ta, tb);
384 } /* computed_value_Shrs */
387 * Return the value of a Rotl.
389 static tarval *computed_value_Rotl(const ir_node *n)
391 ir_node *a = get_Rotl_left(n);
392 ir_node *b = get_Rotl_right(n);
394 tarval *ta = value_of(a);
395 tarval *tb = value_of(b);
397 if ((ta != tarval_bad) && (tb != tarval_bad)) {
398 return tarval_rotl(ta, tb);
401 } /* computed_value_Rotl */
404 * Return the value of a Conv.
406 static tarval *computed_value_Conv(const ir_node *n)
408 ir_node *a = get_Conv_op(n);
409 tarval *ta = value_of(a);
411 if (ta != tarval_bad)
412 return tarval_convert_to(ta, get_irn_mode(n));
415 } /* computed_value_Conv */
418 * Calculate the value of a Mux: can be evaluated, if the
419 * sel and the right input are known.
421 static tarval *computed_value_Mux(const ir_node *n)
423 ir_node *sel = get_Mux_sel(n);
424 tarval *ts = value_of(sel);
426 if (ts == get_tarval_b_true()) {
427 ir_node *v = get_Mux_true(n);
430 else if (ts == get_tarval_b_false()) {
431 ir_node *v = get_Mux_false(n);
435 } /* computed_value_Mux */
438 * Calculate the value of a Confirm: can be evaluated,
439 * if it has the form Confirm(x, '=', Const).
441 static tarval *computed_value_Confirm(const ir_node *n)
443 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
444 tarval *tv = value_of(get_Confirm_bound(n));
445 if (tv != tarval_bad)
448 return value_of(get_Confirm_value(n));
449 } /* computed_value_Confirm */
452 * Return the value of a Proj(Cmp).
454 * This performs a first step of unreachable code elimination.
455 * Proj can not be computed, but folding a Cmp above the Proj here is
456 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
458 * There are several case where we can evaluate a Cmp node, see later.
460 static tarval *computed_value_Proj_Cmp(const ir_node *n)
462 ir_node *cmp = get_Proj_pred(n);
463 ir_node *left = get_Cmp_left(cmp);
464 ir_node *right = get_Cmp_right(cmp);
465 long pn_cmp = get_Proj_proj(n);
466 ir_mode *mode = get_irn_mode(left);
470 * BEWARE: a == a is NOT always True for floating Point values, as
471 * NaN != NaN is defined, so we must check this here.
473 if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt || pn_cmp == pn_Cmp_Gt)) {
474 /* This is a trick with the bits used for encoding the Cmp
475 Proj numbers, the following statement is not the same:
476 return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
477 return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
479 tv_l = value_of(left);
480 tv_r = value_of(right);
482 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
484 * The predecessors of Cmp are target values. We can evaluate
487 pn_Cmp flags = tarval_cmp(tv_l, tv_r);
488 if (flags != pn_Cmp_False) {
489 return new_tarval_from_long (pn_cmp & flags, mode_b);
491 } else if (mode_is_int(mode)) {
492 /* for integer values, we can check against MIN/MAX */
495 if (tv_l == get_mode_min(mode)) {
496 /* MIN <=/> x. This results in true/false. */
497 if (pn_cmp == pn_Cmp_Le)
498 return tarval_b_true;
499 else if (pn_cmp == pn_Cmp_Gt)
500 return tarval_b_false;
501 } else if (tv_r == get_mode_min(mode)) {
502 /* x >=/< MIN. This results in true/false. */
503 if (pn_cmp == pn_Cmp_Ge)
504 return tarval_b_true;
505 else if (pn_cmp == pn_Cmp_Lt)
506 return tarval_b_false;
507 } else if (tv_l == get_mode_max(mode)) {
508 /* MAX >=/< x. This results in true/false. */
509 if (pn_cmp == pn_Cmp_Ge)
510 return tarval_b_true;
511 else if (pn_cmp == pn_Cmp_Lt)
512 return tarval_b_false;
513 } else if (tv_r == get_mode_max(mode)) {
514 /* x <=/> MAX. This results in true/false. */
515 if (pn_cmp == pn_Cmp_Le)
516 return tarval_b_true;
517 else if (pn_cmp == pn_Cmp_Gt)
518 return tarval_b_false;
521 cmp_result = vrp_cmp(left, right);
522 if (cmp_result != pn_Cmp_False) {
523 if (cmp_result == pn_Cmp_Lg) {
524 if (pn_cmp == pn_Cmp_Eq) {
525 return tarval_b_false;
526 } else if (pn_cmp == pn_Cmp_Lg) {
527 return tarval_b_true;
530 return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
533 } else if (mode_is_reference(mode)) {
534 /* pointer compare */
535 ir_node *s_l = skip_Proj(left);
536 ir_node *s_r = skip_Proj(right);
538 if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
539 (tarval_is_null(tv_l) && is_Alloc(s_r))) {
541 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
542 * return NULL, they raise an exception. Therefore we can predict
545 return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
548 return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
549 } /* computed_value_Proj_Cmp */
552 * Return the value of a floating point Quot.
554 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b)
556 tarval *ta = value_of(a);
557 tarval *tb = value_of(b);
559 /* cannot optimize 0 / b = 0 because of NaN */
560 if (ta != tarval_bad && tb != tarval_bad)
561 return tarval_quo(ta, tb);
563 } /* do_computed_value_Quot */
566 * Calculate the value of an integer Div of two nodes.
567 * Special case: 0 / b
569 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
571 tarval *ta = value_of(a);
573 const ir_node *dummy;
575 /* Compute c1 / c2 or 0 / a, a != 0 */
576 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
577 return ta; /* 0 / b == 0 */
579 if (ta != tarval_bad && tb != tarval_bad)
580 return tarval_div(ta, tb);
582 } /* do_computed_value_Div */
585 * Calculate the value of an integer Mod of two nodes.
586 * Special case: a % 1
588 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
590 tarval *ta = value_of(a);
591 tarval *tb = value_of(b);
593 /* Compute a % 1 or c1 % c2 */
594 if (tarval_is_one(tb))
595 return get_mode_null(get_irn_mode(a));
596 if (ta != tarval_bad && tb != tarval_bad)
597 return tarval_mod(ta, tb);
599 } /* do_computed_value_Mod */
602 * Return the value of a Proj(DivMod).
604 static tarval *computed_value_Proj_DivMod(const ir_node *n)
606 long proj_nr = get_Proj_proj(n);
608 /* compute either the Div or the Mod part */
609 if (proj_nr == pn_DivMod_res_div) {
610 const ir_node *a = get_Proj_pred(n);
611 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
612 } else if (proj_nr == pn_DivMod_res_mod) {
613 const ir_node *a = get_Proj_pred(n);
614 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
617 } /* computed_value_Proj_DivMod */
620 * Return the value of a Proj(Div).
622 static tarval *computed_value_Proj_Div(const ir_node *n)
624 long proj_nr = get_Proj_proj(n);
626 if (proj_nr == pn_Div_res) {
627 const ir_node *a = get_Proj_pred(n);
628 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
631 } /* computed_value_Proj_Div */
634 * Return the value of a Proj(Mod).
636 static tarval *computed_value_Proj_Mod(const ir_node *n)
638 long proj_nr = get_Proj_proj(n);
640 if (proj_nr == pn_Mod_res) {
641 const ir_node *a = get_Proj_pred(n);
642 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
645 } /* computed_value_Proj_Mod */
648 * Return the value of a Proj(Quot).
650 static tarval *computed_value_Proj_Quot(const ir_node *n)
652 long proj_nr = get_Proj_proj(n);
654 if (proj_nr == pn_Quot_res) {
655 const ir_node *a = get_Proj_pred(n);
656 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
659 } /* computed_value_Proj_Quot */
662 * Return the value of a Proj.
664 static tarval *computed_value_Proj(const ir_node *proj)
666 ir_node *n = get_Proj_pred(proj);
668 if (n->op->ops.computed_value_Proj != NULL)
669 return n->op->ops.computed_value_Proj(proj);
671 } /* computed_value_Proj */
674 * If the parameter n can be computed, return its value, else tarval_bad.
675 * Performs constant folding.
677 * @param n The node this should be evaluated
679 tarval *computed_value(const ir_node *n)
681 vrp_attr *vrp = vrp_get_info(n);
682 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
683 return vrp->bits_set;
685 if (n->op->ops.computed_value)
686 return n->op->ops.computed_value(n);
688 } /* computed_value */
691 * Set the default computed_value evaluator in an ir_op_ops.
693 * @param code the opcode for the default operation
694 * @param ops the operations initialized
699 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
703 ops->computed_value = computed_value_##a; \
705 #define CASE_PROJ(a) \
707 ops->computed_value_Proj = computed_value_Proj_##a; \
744 } /* firm_set_default_computed_value */
747 * Returns a equivalent block for another block.
748 * If the block has only one predecessor, this is
749 * the equivalent one. If the only predecessor of a block is
750 * the block itself, this is a dead block.
752 * If both predecessors of a block are the branches of a binary
753 * Cond, the equivalent block is Cond's block.
755 * If all predecessors of a block are bad or lies in a dead
756 * block, the current block is dead as well.
758 * Note, that blocks are NEVER turned into Bad's, instead
759 * the dead_block flag is set. So, never test for is_Bad(block),
760 * always use is_dead_Block(block).
762 static ir_node *equivalent_node_Block(ir_node *n)
768 /* don't optimize dead or labeled blocks */
769 if (is_Block_dead(n) || has_Block_entity(n))
772 n_preds = get_Block_n_cfgpreds(n);
774 /* The Block constructor does not call optimize, but mature_immBlock()
775 calls the optimization. */
776 assert(get_Block_matured(n));
778 irg = get_irn_irg(n);
780 /* Straightening: a single entry Block following a single exit Block
781 can be merged, if it is not the Start block. */
782 /* !!! Beware, all Phi-nodes of n must have been optimized away.
783 This should be true, as the block is matured before optimize is called.
784 But what about Phi-cycles with the Phi0/Id that could not be resolved?
785 Remaining Phi nodes are just Ids. */
787 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
790 ir_node *predblock = get_nodes_block(pred);
791 if (predblock == oldn) {
792 /* Jmp jumps into the block it is in -- deal self cycle. */
793 n = set_Block_dead(n);
794 DBG_OPT_DEAD_BLOCK(oldn, n);
797 DBG_OPT_STG(oldn, n);
799 } else if (is_Cond(pred)) {
800 ir_node *predblock = get_nodes_block(pred);
801 if (predblock == oldn) {
802 /* Jmp jumps into the block it is in -- deal self cycle. */
803 n = set_Block_dead(n);
804 DBG_OPT_DEAD_BLOCK(oldn, n);
807 } else if (n_preds == 2) {
808 /* Test whether Cond jumps twice to this block
809 * The more general case which more than 2 predecessors is handles
810 * in optimize_cf(), we handle only this special case for speed here.
812 ir_node *a = get_Block_cfgpred(n, 0);
813 ir_node *b = get_Block_cfgpred(n, 1);
815 if (is_Proj(a) && is_Proj(b)) {
816 ir_node *cond = get_Proj_pred(a);
818 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
819 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
820 /* Also a single entry Block following a single exit Block. Phis have
821 twice the same operand and will be optimized away. */
822 n = get_nodes_block(cond);
823 DBG_OPT_IFSIM1(oldn, a, b, n);
826 } else if (get_opt_unreachable_code() &&
827 (n != get_irg_start_block(irg)) &&
828 (n != get_irg_end_block(irg))) {
831 /* If all inputs are dead, this block is dead too, except if it is
832 the start or end block. This is one step of unreachable code
834 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
835 ir_node *pred = get_Block_cfgpred(n, i);
838 if (is_Bad(pred)) continue;
839 pred_blk = get_nodes_block(skip_Proj(pred));
841 if (is_Block_dead(pred_blk)) continue;
844 /* really found a living input */
849 n = set_Block_dead(n);
850 DBG_OPT_DEAD_BLOCK(oldn, n);
855 } /* equivalent_node_Block */
858 * Returns a equivalent node for a Jmp, a Bad :-)
859 * Of course this only happens if the Block of the Jmp is dead.
861 static ir_node *equivalent_node_Jmp(ir_node *n)
865 /* unreachable code elimination */
866 if (is_Block_dead(get_nodes_block(n))) {
867 ir_graph *irg = get_irn_irg(n);
868 n = get_irg_bad(irg);
869 DBG_OPT_DEAD_BLOCK(oldn, n);
872 } /* equivalent_node_Jmp */
874 /** Raise is handled in the same way as Jmp. */
875 #define equivalent_node_Raise equivalent_node_Jmp
878 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
879 See transform_node_Proj_Cond(). */
882 * Optimize operations that are commutative and have neutral 0,
883 * so a op 0 = 0 op a = a.
885 static ir_node *equivalent_node_neutral_zero(ir_node *n)
889 ir_node *a = get_binop_left(n);
890 ir_node *b = get_binop_right(n);
895 /* After running compute_node there is only one constant predecessor.
896 Find this predecessors value and remember the other node: */
897 if ((tv = value_of(a)) != tarval_bad) {
899 } else if ((tv = value_of(b)) != tarval_bad) {
904 /* If this predecessors constant value is zero, the operation is
905 * unnecessary. Remove it.
907 * Beware: If n is a Add, the mode of on and n might be different
908 * which happens in this rare construction: NULL + 3.
909 * Then, a Conv would be needed which we cannot include here.
911 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
914 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
918 } /* equivalent_node_neutral_zero */
921 * Eor is commutative and has neutral 0.
923 static ir_node *equivalent_node_Eor(ir_node *n)
929 n = equivalent_node_neutral_zero(n);
930 if (n != oldn) return n;
933 b = get_Eor_right(n);
936 ir_node *aa = get_Eor_left(a);
937 ir_node *ab = get_Eor_right(a);
940 /* (a ^ b) ^ a -> b */
942 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
944 } else if (ab == b) {
945 /* (a ^ b) ^ b -> a */
947 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
952 ir_node *ba = get_Eor_left(b);
953 ir_node *bb = get_Eor_right(b);
956 /* a ^ (a ^ b) -> b */
958 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
960 } else if (bb == a) {
961 /* a ^ (b ^ a) -> b */
963 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
971 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
973 * The second one looks strange, but this construct
974 * is used heavily in the LCC sources :-).
976 * Beware: The Mode of an Add may be different than the mode of its
977 * predecessors, so we could not return a predecessors in all cases.
979 static ir_node *equivalent_node_Add(ir_node *n)
982 ir_node *left, *right;
983 ir_mode *mode = get_irn_mode(n);
985 n = equivalent_node_neutral_zero(n);
989 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
990 if (mode_is_float(mode)) {
991 ir_graph *irg = get_irn_irg(n);
992 if (get_irg_fp_model(irg) & fp_strict_algebraic)
996 left = get_Add_left(n);
997 right = get_Add_right(n);
1000 if (get_Sub_right(left) == right) {
1003 n = get_Sub_left(left);
1004 if (mode == get_irn_mode(n)) {
1005 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1010 if (is_Sub(right)) {
1011 if (get_Sub_right(right) == left) {
1014 n = get_Sub_left(right);
1015 if (mode == get_irn_mode(n)) {
1016 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1022 } /* equivalent_node_Add */
1025 * optimize operations that are not commutative but have neutral 0 on left,
1028 static ir_node *equivalent_node_left_zero(ir_node *n)
1032 ir_node *a = get_binop_left(n);
1033 ir_node *b = get_binop_right(n);
1034 tarval *tb = value_of(b);
1036 if (tarval_is_null(tb)) {
1039 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1042 } /* equivalent_node_left_zero */
1044 #define equivalent_node_Shl equivalent_node_left_zero
1045 #define equivalent_node_Shr equivalent_node_left_zero
1046 #define equivalent_node_Shrs equivalent_node_left_zero
1047 #define equivalent_node_Rotl equivalent_node_left_zero
1050 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1052 * The second one looks strange, but this construct
1053 * is used heavily in the LCC sources :-).
1055 * Beware: The Mode of a Sub may be different than the mode of its
1056 * predecessors, so we could not return a predecessors in all cases.
1058 static ir_node *equivalent_node_Sub(ir_node *n)
1062 ir_mode *mode = get_irn_mode(n);
1065 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1066 if (mode_is_float(mode)) {
1067 ir_graph *irg = get_irn_irg(n);
1068 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1072 b = get_Sub_right(n);
1075 /* Beware: modes might be different */
1076 if (tarval_is_null(tb)) {
1077 ir_node *a = get_Sub_left(n);
1078 if (mode == get_irn_mode(a)) {
1081 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1085 } /* equivalent_node_Sub */
1089 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1092 * -(-a) == a, but might overflow two times.
1093 * We handle it anyway here but the better way would be a
1094 * flag. This would be needed for Pascal for instance.
1096 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1099 ir_node *pred = get_unop_op(n);
1101 /* optimize symmetric unop */
1102 if (get_irn_op(pred) == get_irn_op(n)) {
1103 n = get_unop_op(pred);
1104 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1107 } /* equivalent_node_idempotent_unop */
1109 /** Optimize Not(Not(x)) == x. */
1110 #define equivalent_node_Not equivalent_node_idempotent_unop
1112 /** -(-x) == x ??? Is this possible or can --x raise an
1113 out of bounds exception if min =! max? */
1114 #define equivalent_node_Minus equivalent_node_idempotent_unop
1117 * Optimize a * 1 = 1 * a = a.
1119 static ir_node *equivalent_node_Mul(ir_node *n)
1122 ir_node *a = get_Mul_left(n);
1124 /* we can handle here only the n * n = n bit cases */
1125 if (get_irn_mode(n) == get_irn_mode(a)) {
1126 ir_node *b = get_Mul_right(n);
1130 * Mul is commutative and has again an other neutral element.
1131 * Constants are place right, so check this case first.
1134 if (tarval_is_one(tv)) {
1136 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1139 if (tarval_is_one(tv)) {
1141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1146 } /* equivalent_node_Mul */
1149 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1151 static ir_node *equivalent_node_Or(ir_node *n)
1155 ir_node *a = get_Or_left(n);
1156 ir_node *b = get_Or_right(n);
1160 n = a; /* Or has it's own neutral element */
1161 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1164 /* constants are cormalized to right, check this site first */
1166 if (tarval_is_null(tv)) {
1168 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1172 if (tarval_is_null(tv)) {
1174 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1179 } /* equivalent_node_Or */
1182 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1184 static ir_node *equivalent_node_And(ir_node *n)
1188 ir_node *a = get_And_left(n);
1189 ir_node *b = get_And_right(n);
1193 n = a; /* And has it's own neutral element */
1194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1197 /* constants are normalized to right, check this site first */
1199 if (tarval_is_all_one(tv)) {
1201 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1204 if (tv != get_tarval_bad()) {
1205 ir_mode *mode = get_irn_mode(n);
1206 if (!mode_is_signed(mode) && is_Conv(a)) {
1207 ir_node *convop = get_Conv_op(a);
1208 ir_mode *convopmode = get_irn_mode(convop);
1209 if (!mode_is_signed(convopmode)) {
1210 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1211 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1220 if (tarval_is_all_one(tv)) {
1222 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1226 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1229 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1234 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1237 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1242 } /* equivalent_node_And */
1245 * Try to remove useless Conv's:
1247 static ir_node *equivalent_node_Conv(ir_node *n)
1250 ir_node *a = get_Conv_op(n);
1252 ir_mode *n_mode = get_irn_mode(n);
1253 ir_mode *a_mode = get_irn_mode(a);
1256 if (n_mode == a_mode) { /* No Conv necessary */
1257 if (get_Conv_strict(n)) {
1260 /* neither Minus nor Confirm change the precision,
1261 so we can "look-through" */
1264 p = get_Minus_op(p);
1265 } else if (is_Confirm(p)) {
1266 p = get_Confirm_value(p);
1272 if (is_Conv(p) && get_Conv_strict(p)) {
1273 /* we known already, that a_mode == n_mode, and neither
1274 Minus change the mode, so the second Conv
1276 assert(get_irn_mode(p) == n_mode);
1278 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1282 ir_node *pred = get_Proj_pred(p);
1283 if (is_Load(pred)) {
1284 /* Loads always return with the exact precision of n_mode */
1285 assert(get_Load_mode(pred) == n_mode);
1287 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1290 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1291 pred = get_Proj_pred(pred);
1292 if (is_Start(pred)) {
1293 /* Arguments always return with the exact precision,
1294 as strictConv's are place before Call -- if the
1295 caller was compiled with the same setting.
1296 Otherwise, the semantics is probably still right. */
1297 assert(get_irn_mode(p) == n_mode);
1299 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1305 /* special case: the immediate predecessor is also a Conv */
1306 if (! get_Conv_strict(a)) {
1307 /* first one is not strict, kick it */
1309 a_mode = get_irn_mode(a);
1313 /* else both are strict conv, second is superfluous */
1315 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1320 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1323 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1324 ir_node *b = get_Conv_op(a);
1325 ir_mode *b_mode = get_irn_mode(b);
1327 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1328 /* both are strict conv */
1329 if (smaller_mode(a_mode, n_mode)) {
1330 /* both are strict, but the first is smaller, so
1331 the second cannot remove more precision, remove the
1333 set_Conv_strict(n, 0);
1336 if (n_mode == b_mode) {
1337 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1338 if (n_mode == mode_b) {
1339 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1340 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1342 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1343 if (values_in_mode(b_mode, a_mode)) {
1344 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1345 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1350 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1351 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1352 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1353 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1355 if (float_mantissa >= int_mantissa) {
1357 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1362 if (smaller_mode(b_mode, a_mode)) {
1363 if (get_Conv_strict(n))
1364 set_Conv_strict(b, 1);
1365 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1373 } /* equivalent_node_Conv */
1376 * A Cast may be removed if the type of the previous node
1377 * is already the type of the Cast.
1379 static ir_node *equivalent_node_Cast(ir_node *n)
1382 ir_node *pred = get_Cast_op(n);
1384 if (get_irn_type(pred) == get_Cast_type(n)) {
1386 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1389 } /* equivalent_node_Cast */
1392 * - fold Phi-nodes, iff they have only one predecessor except
1395 static ir_node *equivalent_node_Phi(ir_node *n)
1401 ir_node *first_val = NULL; /* to shutup gcc */
1403 if (!get_opt_optimize() &&
1404 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1407 n_preds = get_Phi_n_preds(n);
1409 block = get_nodes_block(n);
1411 if (is_Block_dead(block)) {
1412 ir_graph *irg = get_irn_irg(n);
1413 return get_irg_bad(irg);
1416 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1418 /* Find first non-self-referencing input */
1419 for (i = 0; i < n_preds; ++i) {
1420 first_val = get_Phi_pred(n, i);
1421 if ( (first_val != n) /* not self pointer */
1423 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1424 * predecessors. Then, Phi nodes in dead code might be removed, causing
1425 * nodes pointing to themself (Add's for instance).
1426 * This is really bad and causes endless recursions in several
1427 * code pathes, so we do NOT optimize such a code.
1428 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1429 * (and bad Phi predecessors), so live code is optimized later.
1431 && (! is_Bad(get_Block_cfgpred(block, i)))
1433 ) { /* value not dead */
1434 break; /* then found first value. */
1439 ir_graph *irg = get_irn_irg(n);
1440 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1441 return get_irg_bad(irg);
1444 /* search for rest of inputs, determine if any of these
1445 are non-self-referencing */
1446 while (++i < n_preds) {
1447 ir_node *scnd_val = get_Phi_pred(n, i);
1448 if ( (scnd_val != n)
1449 && (scnd_val != first_val)
1452 && (! is_Bad(get_Block_cfgpred(block, i)))
1459 if (i >= n_preds && !is_Dummy(first_val)) {
1460 /* Fold, if no multiple distinct non-self-referencing inputs */
1462 DBG_OPT_PHI(oldn, n);
1465 } /* equivalent_node_Phi */
1468 * Several optimizations:
1469 * - fold Sync-nodes, iff they have only one predecessor except
1472 static ir_node *equivalent_node_Sync(ir_node *n)
1474 int arity = get_Sync_n_preds(n);
1477 for (i = 0; i < arity;) {
1478 ir_node *pred = get_Sync_pred(n, i);
1481 /* Remove Bad predecessors */
1488 /* Remove duplicate predecessors */
1494 if (get_Sync_pred(n, j) == pred) {
1503 ir_graph *irg = get_irn_irg(n);
1504 return get_irg_bad(irg);
1506 if (arity == 1) return get_Sync_pred(n, 0);
1508 } /* equivalent_node_Sync */
1511 * Optimize Proj(Tuple).
1513 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1515 ir_node *oldn = proj;
1516 ir_node *tuple = get_Proj_pred(proj);
1518 /* Remove the Tuple/Proj combination. */
1519 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1520 DBG_OPT_TUPLE(oldn, tuple, proj);
1523 } /* equivalent_node_Proj_Tuple */
1526 * Optimize a / 1 = a.
1528 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1530 ir_node *oldn = proj;
1531 ir_node *div = get_Proj_pred(proj);
1532 ir_node *b = get_Div_right(div);
1533 tarval *tb = value_of(b);
1535 /* Div is not commutative. */
1536 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1537 switch (get_Proj_proj(proj)) {
1539 proj = get_Div_mem(div);
1540 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1544 proj = get_Div_left(div);
1545 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1549 /* we cannot replace the exception Proj's here, this is done in
1550 transform_node_Proj_Div() */
1555 } /* equivalent_node_Proj_Div */
1558 * Optimize a / 1.0 = a.
1560 static ir_node *equivalent_node_Proj_Quot(ir_node *proj)
1562 ir_node *oldn = proj;
1563 ir_node *quot = get_Proj_pred(proj);
1564 ir_node *b = get_Quot_right(quot);
1565 tarval *tb = value_of(b);
1567 /* Div is not commutative. */
1568 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1569 switch (get_Proj_proj(proj)) {
1571 proj = get_Quot_mem(quot);
1572 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1576 proj = get_Quot_left(quot);
1577 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1581 /* we cannot replace the exception Proj's here, this is done in
1582 transform_node_Proj_Quot() */
1587 } /* equivalent_node_Proj_Quot */
1590 * Optimize a / 1 = a.
1592 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj)
1594 ir_node *oldn = proj;
1595 ir_node *divmod = get_Proj_pred(proj);
1596 ir_node *b = get_DivMod_right(divmod);
1597 tarval *tb = value_of(b);
1599 /* Div is not commutative. */
1600 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1601 switch (get_Proj_proj(proj)) {
1603 proj = get_DivMod_mem(divmod);
1604 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1607 case pn_DivMod_res_div:
1608 proj = get_DivMod_left(divmod);
1609 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1613 /* we cannot replace the exception Proj's here, this is done in
1614 transform_node_Proj_DivMod().
1615 Note further that the pn_DivMod_res_div case is handled in
1616 computed_value_Proj(). */
1621 } /* equivalent_node_Proj_DivMod */
1624 * Optimize CopyB(mem, x, x) into a Nop.
1626 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1628 ir_node *oldn = proj;
1629 ir_node *copyb = get_Proj_pred(proj);
1630 ir_node *a = get_CopyB_dst(copyb);
1631 ir_node *b = get_CopyB_src(copyb);
1634 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1635 switch (get_Proj_proj(proj)) {
1637 proj = get_CopyB_mem(copyb);
1638 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1641 case pn_CopyB_X_except: {
1642 ir_graph *irg = get_irn_irg(proj);
1643 DBG_OPT_EXC_REM(proj);
1644 proj = get_irg_bad(irg);
1650 } /* equivalent_node_Proj_CopyB */
1653 * Optimize Bounds(idx, idx, upper) into idx.
1655 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1657 ir_node *oldn = proj;
1658 ir_node *bound = get_Proj_pred(proj);
1659 ir_node *idx = get_Bound_index(bound);
1660 ir_node *pred = skip_Proj(idx);
1663 if (idx == get_Bound_lower(bound))
1665 else if (is_Bound(pred)) {
1667 * idx was Bounds checked previously, it is still valid if
1668 * lower <= pred_lower && pred_upper <= upper.
1670 ir_node *lower = get_Bound_lower(bound);
1671 ir_node *upper = get_Bound_upper(bound);
1672 if (get_Bound_lower(pred) == lower &&
1673 get_Bound_upper(pred) == upper) {
1675 * One could expect that we simply return the previous
1676 * Bound here. However, this would be wrong, as we could
1677 * add an exception Proj to a new location then.
1678 * So, we must turn in into a tuple.
1684 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1685 switch (get_Proj_proj(proj)) {
1687 DBG_OPT_EXC_REM(proj);
1688 proj = get_Bound_mem(bound);
1690 case pn_Bound_X_except: {
1691 ir_graph *irg = get_irn_irg(proj);
1692 DBG_OPT_EXC_REM(proj);
1693 proj = get_irg_bad(irg);
1698 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1701 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1706 } /* equivalent_node_Proj_Bound */
1709 * Optimize an Exception Proj(Load) with a non-null address.
1711 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1713 if (get_opt_ldst_only_null_ptr_exceptions()) {
1714 if (get_irn_mode(proj) == mode_X) {
1715 ir_node *load = get_Proj_pred(proj);
1717 /* get the Load address */
1718 const ir_node *addr = get_Load_ptr(load);
1719 const ir_node *confirm;
1721 if (value_not_null(addr, &confirm)) {
1722 if (get_Proj_proj(proj) == pn_Load_X_except) {
1723 ir_graph *irg = get_irn_irg(proj);
1724 DBG_OPT_EXC_REM(proj);
1725 return get_irg_bad(irg);
1731 } /* equivalent_node_Proj_Load */
1734 * Optimize an Exception Proj(Store) with a non-null address.
1736 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1738 if (get_opt_ldst_only_null_ptr_exceptions()) {
1739 if (get_irn_mode(proj) == mode_X) {
1740 ir_node *store = get_Proj_pred(proj);
1742 /* get the load/store address */
1743 const ir_node *addr = get_Store_ptr(store);
1744 const ir_node *confirm;
1746 if (value_not_null(addr, &confirm)) {
1747 if (get_Proj_proj(proj) == pn_Store_X_except) {
1748 ir_graph *irg = get_irn_irg(proj);
1749 DBG_OPT_EXC_REM(proj);
1750 return get_irg_bad(irg);
1756 } /* equivalent_node_Proj_Store */
1759 * Does all optimizations on nodes that must be done on it's Proj's
1760 * because of creating new nodes.
1762 static ir_node *equivalent_node_Proj(ir_node *proj)
1764 ir_node *n = get_Proj_pred(proj);
1766 if (get_irn_mode(proj) == mode_X) {
1767 if (is_Block_dead(get_nodes_block(n))) {
1768 /* Remove dead control flow -- early gigo(). */
1769 ir_graph *irg = get_irn_irg(proj);
1770 return get_irg_bad(irg);
1773 if (n->op->ops.equivalent_node_Proj)
1774 return n->op->ops.equivalent_node_Proj(proj);
1776 } /* equivalent_node_Proj */
1781 static ir_node *equivalent_node_Id(ir_node *n)
1789 DBG_OPT_ID(oldn, n);
1791 } /* equivalent_node_Id */
1796 static ir_node *equivalent_node_Mux(ir_node *n)
1798 ir_node *oldn = n, *sel = get_Mux_sel(n);
1800 tarval *ts = value_of(sel);
1802 /* Mux(true, f, t) == t */
1803 if (ts == tarval_b_true) {
1804 n = get_Mux_true(n);
1805 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1808 /* Mux(false, f, t) == f */
1809 if (ts == tarval_b_false) {
1810 n = get_Mux_false(n);
1811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1814 n_t = get_Mux_true(n);
1815 n_f = get_Mux_false(n);
1817 /* Mux(v, x, T) == x */
1818 if (is_Unknown(n_f)) {
1820 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1823 /* Mux(v, T, x) == x */
1824 if (is_Unknown(n_t)) {
1826 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1830 /* Mux(v, x, x) == x */
1833 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1836 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1837 ir_node *cmp = get_Proj_pred(sel);
1838 long proj_nr = get_Proj_proj(sel);
1839 ir_node *f = get_Mux_false(n);
1840 ir_node *t = get_Mux_true(n);
1843 * Note further that these optimization work even for floating point
1844 * with NaN's because -NaN == NaN.
1845 * However, if +0 and -0 is handled differently, we cannot use the first one.
1848 ir_node *const cmp_l = get_Cmp_left(cmp);
1849 ir_node *const cmp_r = get_Cmp_right(cmp);
1853 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1854 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1856 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1863 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1864 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1873 * Note: normalization puts the constant on the right side,
1874 * so we check only one case.
1876 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1877 /* Mux(t CMP 0, X, t) */
1878 if (is_Minus(f) && get_Minus_op(f) == t) {
1879 /* Mux(t CMP 0, -t, t) */
1880 if (proj_nr == pn_Cmp_Eq) {
1881 /* Mux(t == 0, -t, t) ==> -t */
1883 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1884 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1885 /* Mux(t != 0, -t, t) ==> t */
1887 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1894 } /* equivalent_node_Mux */
1897 * Remove Confirm nodes if setting is on.
1898 * Replace Confirms(x, '=', Constlike) by Constlike.
1900 static ir_node *equivalent_node_Confirm(ir_node *n)
1902 ir_node *pred = get_Confirm_value(n);
1903 pn_Cmp pnc = get_Confirm_cmp(n);
1905 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1907 * rare case: two identical Confirms one after another,
1908 * replace the second one with the first.
1911 pred = get_Confirm_value(n);
1912 pnc = get_Confirm_cmp(n);
1918 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1919 * perform no actual computation, as, e.g., the Id nodes. It does not create
1920 * new nodes. It is therefore safe to free n if the node returned is not n.
1921 * If a node returns a Tuple we can not just skip it. If the size of the
1922 * in array fits, we transform n into a tuple (e.g., Div).
1924 ir_node *equivalent_node(ir_node *n)
1926 if (n->op->ops.equivalent_node)
1927 return n->op->ops.equivalent_node(n);
1929 } /* equivalent_node */
1932 * Sets the default equivalent node operation for an ir_op_ops.
1934 * @param code the opcode for the default operation
1935 * @param ops the operations initialized
1940 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1944 ops->equivalent_node = equivalent_node_##a; \
1946 #define CASE_PROJ(a) \
1948 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1991 } /* firm_set_default_equivalent_node */
1994 * Returns non-zero if a node is a Phi node
1995 * with all predecessors constant.
1997 static int is_const_Phi(ir_node *n)
2001 if (! is_Phi(n) || get_irn_arity(n) == 0)
2003 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
2004 if (! is_Const(get_irn_n(n, i)))
2008 } /* is_const_Phi */
2010 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
2011 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
2014 * in reality eval_func should be tarval (*eval_func)() but incomplete
2015 * declarations are bad style and generate noisy warnings
2017 typedef void (*eval_func)(void);
2020 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2022 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
2024 if (eval == (eval_func) tarval_sub) {
2025 tarval_sub_type func = (tarval_sub_type)eval;
2027 return func(a, b, mode);
2029 tarval_binop_type func = (tarval_binop_type)eval;
2036 * Apply an evaluator on a binop with a constant operators (and one Phi).
2038 * @param phi the Phi node
2039 * @param other the other operand
2040 * @param eval an evaluator function
2041 * @param mode the mode of the result, may be different from the mode of the Phi!
2042 * @param left if non-zero, other is the left operand, else the right
2044 * @return a new Phi node if the conversion was successful, NULL else
2046 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left)
2052 int i, n = get_irn_arity(phi);
2054 NEW_ARR_A(void *, res, n);
2056 for (i = 0; i < n; ++i) {
2057 pred = get_irn_n(phi, i);
2058 tv = get_Const_tarval(pred);
2059 tv = do_eval(eval, other, tv, mode);
2061 if (tv == tarval_bad) {
2062 /* folding failed, bad */
2068 for (i = 0; i < n; ++i) {
2069 pred = get_irn_n(phi, i);
2070 tv = get_Const_tarval(pred);
2071 tv = do_eval(eval, tv, other, mode);
2073 if (tv == tarval_bad) {
2074 /* folding failed, bad */
2080 irg = get_irn_irg(phi);
2081 for (i = 0; i < n; ++i) {
2082 pred = get_irn_n(phi, i);
2083 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2085 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2086 } /* apply_binop_on_phi */
2089 * Apply an evaluator on a binop with two constant Phi.
2091 * @param a the left Phi node
2092 * @param b the right Phi node
2093 * @param eval an evaluator function
2094 * @param mode the mode of the result, may be different from the mode of the Phi!
2096 * @return a new Phi node if the conversion was successful, NULL else
2098 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2100 tarval *tv_l, *tv_r, *tv;
2106 if (get_nodes_block(a) != get_nodes_block(b))
2109 n = get_irn_arity(a);
2110 NEW_ARR_A(void *, res, n);
2112 for (i = 0; i < n; ++i) {
2113 pred = get_irn_n(a, i);
2114 tv_l = get_Const_tarval(pred);
2115 pred = get_irn_n(b, i);
2116 tv_r = get_Const_tarval(pred);
2117 tv = do_eval(eval, tv_l, tv_r, mode);
2119 if (tv == tarval_bad) {
2120 /* folding failed, bad */
2125 irg = get_irn_irg(a);
2126 for (i = 0; i < n; ++i) {
2127 pred = get_irn_n(a, i);
2128 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2130 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2131 } /* apply_binop_on_2_phis */
2134 * Apply an evaluator on a unop with a constant operator (a Phi).
2136 * @param phi the Phi node
2137 * @param eval an evaluator function
2139 * @return a new Phi node if the conversion was successful, NULL else
2141 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *))
2148 int i, n = get_irn_arity(phi);
2150 NEW_ARR_A(void *, res, n);
2151 for (i = 0; i < n; ++i) {
2152 pred = get_irn_n(phi, i);
2153 tv = get_Const_tarval(pred);
2156 if (tv == tarval_bad) {
2157 /* folding failed, bad */
2162 mode = get_irn_mode(phi);
2163 irg = get_irn_irg(phi);
2164 for (i = 0; i < n; ++i) {
2165 pred = get_irn_n(phi, i);
2166 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2168 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2169 } /* apply_unop_on_phi */
2172 * Apply a conversion on a constant operator (a Phi).
2174 * @param phi the Phi node
2176 * @return a new Phi node if the conversion was successful, NULL else
2178 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2184 int i, n = get_irn_arity(phi);
2186 NEW_ARR_A(void *, res, n);
2187 for (i = 0; i < n; ++i) {
2188 pred = get_irn_n(phi, i);
2189 tv = get_Const_tarval(pred);
2190 tv = tarval_convert_to(tv, mode);
2192 if (tv == tarval_bad) {
2193 /* folding failed, bad */
2198 irg = get_irn_irg(phi);
2199 for (i = 0; i < n; ++i) {
2200 pred = get_irn_n(phi, i);
2201 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2203 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2204 } /* apply_conv_on_phi */
2207 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2208 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2209 * If possible, remove the Conv's.
2211 static ir_node *transform_node_AddSub(ir_node *n)
2213 ir_mode *mode = get_irn_mode(n);
2215 if (mode_is_reference(mode)) {
2216 ir_node *left = get_binop_left(n);
2217 ir_node *right = get_binop_right(n);
2218 unsigned ref_bits = get_mode_size_bits(mode);
2220 if (is_Conv(left)) {
2221 ir_mode *lmode = get_irn_mode(left);
2222 unsigned bits = get_mode_size_bits(lmode);
2224 if (ref_bits == bits &&
2225 mode_is_int(lmode) &&
2226 get_mode_arithmetic(lmode) == irma_twos_complement) {
2227 ir_node *pre = get_Conv_op(left);
2228 ir_mode *pre_mode = get_irn_mode(pre);
2230 if (mode_is_int(pre_mode) &&
2231 get_mode_size_bits(pre_mode) == bits &&
2232 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2233 /* ok, this conv just changes to sign, moreover the calculation
2234 * is done with same number of bits as our address mode, so
2235 * we can ignore the conv as address calculation can be viewed
2236 * as either signed or unsigned
2238 set_binop_left(n, pre);
2243 if (is_Conv(right)) {
2244 ir_mode *rmode = get_irn_mode(right);
2245 unsigned bits = get_mode_size_bits(rmode);
2247 if (ref_bits == bits &&
2248 mode_is_int(rmode) &&
2249 get_mode_arithmetic(rmode) == irma_twos_complement) {
2250 ir_node *pre = get_Conv_op(right);
2251 ir_mode *pre_mode = get_irn_mode(pre);
2253 if (mode_is_int(pre_mode) &&
2254 get_mode_size_bits(pre_mode) == bits &&
2255 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2256 /* ok, this conv just changes to sign, moreover the calculation
2257 * is done with same number of bits as our address mode, so
2258 * we can ignore the conv as address calculation can be viewed
2259 * as either signed or unsigned
2261 set_binop_right(n, pre);
2266 /* let address arithmetic use unsigned modes */
2267 if (is_Const(right)) {
2268 ir_mode *rmode = get_irn_mode(right);
2270 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2271 /* convert a AddP(P, *s) into AddP(P, *u) */
2272 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2274 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2275 set_binop_right(n, pre);
2281 } /* transform_node_AddSub */
2283 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2286 if (is_Const(b) && is_const_Phi(a)) { \
2287 /* check for Op(Phi, Const) */ \
2288 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2290 else if (is_Const(a) && is_const_Phi(b)) { \
2291 /* check for Op(Const, Phi) */ \
2292 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2294 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2295 /* check for Op(Phi, Phi) */ \
2296 c = apply_binop_on_2_phis(a, b, eval, mode); \
2299 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2304 #define HANDLE_UNOP_PHI(eval, a, c) \
2307 if (is_const_Phi(a)) { \
2308 /* check for Op(Phi) */ \
2309 c = apply_unop_on_phi(a, eval); \
2311 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2318 * Do the AddSub optimization, then Transform
2319 * Constant folding on Phi
2320 * Add(a,a) -> Mul(a, 2)
2321 * Add(Mul(a, x), a) -> Mul(a, x+1)
2322 * if the mode is integer or float.
2323 * Transform Add(a,-b) into Sub(a,b).
2324 * Reassociation might fold this further.
2326 static ir_node *transform_node_Add(ir_node *n)
2329 ir_node *a, *b, *c, *oldn = n;
2330 vrp_attr *a_vrp, *b_vrp;
2332 n = transform_node_AddSub(n);
2334 a = get_Add_left(n);
2335 b = get_Add_right(n);
2337 mode = get_irn_mode(n);
2339 if (mode_is_reference(mode)) {
2340 ir_mode *lmode = get_irn_mode(a);
2342 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2343 /* an Add(a, NULL) is a hidden Conv */
2344 dbg_info *dbg = get_irn_dbg_info(n);
2345 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2349 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2351 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2352 if (mode_is_float(mode)) {
2353 ir_graph *irg = get_irn_irg(n);
2354 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2358 if (mode_is_num(mode)) {
2359 ir_graph *irg = get_irn_irg(n);
2360 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2361 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2362 && a == b && mode_is_int(mode)) {
2363 ir_node *block = get_nodes_block(n);
2366 get_irn_dbg_info(n),
2369 new_r_Const_long(irg, mode, 2),
2371 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2376 get_irn_dbg_info(n),
2381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2386 get_irn_dbg_info(n),
2391 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2394 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2395 /* Here we rely on constants be on the RIGHT side */
2397 ir_node *op = get_Not_op(a);
2399 if (is_Const(b) && is_Const_one(b)) {
2401 ir_node *blk = get_nodes_block(n);
2402 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2403 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2408 n = new_r_Const(irg, get_mode_minus_one(mode));
2409 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2414 ir_node *op = get_Not_op(b);
2418 n = new_r_Const(irg, get_mode_minus_one(mode));
2419 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2426 a_vrp = vrp_get_info(a);
2427 b_vrp = vrp_get_info(b);
2429 if (a_vrp && b_vrp) {
2430 tarval *c = tarval_and(
2431 a_vrp->bits_not_set,
2435 if (tarval_is_null(c)) {
2436 dbg_info *dbgi = get_irn_dbg_info(n);
2437 return new_rd_Or(dbgi, get_nodes_block(n),
2442 } /* transform_node_Add */
2445 * returns -cnst or NULL if impossible
2447 static ir_node *const_negate(ir_node *cnst)
2449 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2450 dbg_info *dbgi = get_irn_dbg_info(cnst);
2451 ir_graph *irg = get_irn_irg(cnst);
2452 if (tv == tarval_bad) return NULL;
2453 return new_rd_Const(dbgi, irg, tv);
2457 * Do the AddSub optimization, then Transform
2458 * Constant folding on Phi
2459 * Sub(0,a) -> Minus(a)
2460 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2461 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2462 * Sub(Add(a, x), x) -> a
2463 * Sub(x, Add(x, a)) -> -a
2464 * Sub(x, Const) -> Add(x, -Const)
2466 static ir_node *transform_node_Sub(ir_node *n)
2472 n = transform_node_AddSub(n);
2474 a = get_Sub_left(n);
2475 b = get_Sub_right(n);
2477 mode = get_irn_mode(n);
2479 if (mode_is_int(mode)) {
2480 ir_mode *lmode = get_irn_mode(a);
2482 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2483 /* a Sub(a, NULL) is a hidden Conv */
2484 dbg_info *dbg = get_irn_dbg_info(n);
2485 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2486 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2490 if (mode == lmode &&
2491 get_mode_arithmetic(mode) == irma_twos_complement &&
2493 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2495 dbg_info *dbg = get_irn_dbg_info(n);
2496 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2497 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2503 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2505 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2506 if (mode_is_float(mode)) {
2507 ir_graph *irg = get_irn_irg(n);
2508 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2512 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2513 /* a - C -> a + (-C) */
2514 ir_node *cnst = const_negate(b);
2516 ir_node *block = get_nodes_block(n);
2517 dbg_info *dbgi = get_irn_dbg_info(n);
2519 n = new_rd_Add(dbgi, block, a, cnst, mode);
2520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2525 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2526 dbg_info *dbg = get_irn_dbg_info(n);
2527 ir_node *block = get_nodes_block(n);
2528 ir_node *left = get_Minus_op(a);
2529 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2531 n = new_rd_Minus(dbg, block, add, mode);
2532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2534 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2535 dbg_info *dbg = get_irn_dbg_info(n);
2536 ir_node *block = get_nodes_block(n);
2537 ir_node *right = get_Minus_op(b);
2539 n = new_rd_Add(dbg, block, a, right, mode);
2540 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2542 } else if (is_Sub(b)) {
2543 /* a - (b - c) -> a + (c - b)
2544 * -> (a - b) + c iff (b - c) is a pointer */
2545 dbg_info *s_dbg = get_irn_dbg_info(b);
2546 ir_node *s_block = get_nodes_block(b);
2547 ir_node *s_left = get_Sub_left(b);
2548 ir_node *s_right = get_Sub_right(b);
2549 ir_mode *s_mode = get_irn_mode(b);
2550 if (mode_is_reference(s_mode)) {
2551 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2552 dbg_info *a_dbg = get_irn_dbg_info(n);
2553 ir_node *a_block = get_nodes_block(n);
2556 s_right = new_r_Conv(a_block, s_right, mode);
2557 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2559 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2560 dbg_info *a_dbg = get_irn_dbg_info(n);
2561 ir_node *a_block = get_nodes_block(n);
2563 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2565 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2567 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2568 ir_node *m_right = get_Mul_right(b);
2569 if (is_Const(m_right)) {
2570 ir_node *cnst2 = const_negate(m_right);
2571 if (cnst2 != NULL) {
2572 dbg_info *m_dbg = get_irn_dbg_info(b);
2573 ir_node *m_block = get_nodes_block(b);
2574 ir_node *m_left = get_Mul_left(b);
2575 ir_mode *m_mode = get_irn_mode(b);
2576 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2577 dbg_info *a_dbg = get_irn_dbg_info(n);
2578 ir_node *a_block = get_nodes_block(n);
2580 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2581 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2587 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2588 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2590 get_irn_dbg_info(n),
2594 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2598 if (mode_wrap_around(mode)) {
2599 ir_node *left = get_Add_left(a);
2600 ir_node *right = get_Add_right(a);
2602 /* FIXME: Does the Conv's work only for two complement or generally? */
2604 if (mode != get_irn_mode(right)) {
2605 /* This Sub is an effective Cast */
2606 right = new_r_Conv(get_nodes_block(n), right, mode);
2609 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2611 } else if (right == b) {
2612 if (mode != get_irn_mode(left)) {
2613 /* This Sub is an effective Cast */
2614 left = new_r_Conv(get_nodes_block(n), left, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2623 if (mode_wrap_around(mode)) {
2624 ir_node *left = get_Add_left(b);
2625 ir_node *right = get_Add_right(b);
2627 /* FIXME: Does the Conv's work only for two complement or generally? */
2629 ir_mode *r_mode = get_irn_mode(right);
2631 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2632 if (mode != r_mode) {
2633 /* This Sub is an effective Cast */
2634 n = new_r_Conv(get_nodes_block(n), n, mode);
2636 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2638 } else if (right == a) {
2639 ir_mode *l_mode = get_irn_mode(left);
2641 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2642 if (mode != l_mode) {
2643 /* This Sub is an effective Cast */
2644 n = new_r_Conv(get_nodes_block(n), n, mode);
2646 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2651 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2652 ir_mode *mode = get_irn_mode(a);
2654 if (mode == get_irn_mode(b)) {
2656 ir_node *op_a = get_Conv_op(a);
2657 ir_node *op_b = get_Conv_op(b);
2659 /* check if it's allowed to skip the conv */
2660 ma = get_irn_mode(op_a);
2661 mb = get_irn_mode(op_b);
2663 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2664 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2667 set_Sub_right(n, b);
2673 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2674 if (!is_reassoc_running() && is_Mul(a)) {
2675 ir_node *ma = get_Mul_left(a);
2676 ir_node *mb = get_Mul_right(a);
2679 ir_node *blk = get_nodes_block(n);
2680 ir_graph *irg = get_irn_irg(n);
2682 get_irn_dbg_info(n),
2686 get_irn_dbg_info(n),
2689 new_r_Const(irg, get_mode_one(mode)),
2692 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2694 } else if (mb == b) {
2695 ir_node *blk = get_nodes_block(n);
2696 ir_graph *irg = get_irn_irg(n);
2698 get_irn_dbg_info(n),
2702 get_irn_dbg_info(n),
2705 new_r_Const(irg, get_mode_one(mode)),
2708 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2712 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2713 ir_node *x = get_Sub_left(a);
2714 ir_node *y = get_Sub_right(a);
2715 ir_node *blk = get_nodes_block(n);
2716 ir_mode *m_b = get_irn_mode(b);
2717 ir_mode *m_y = get_irn_mode(y);
2721 /* Determine the right mode for the Add. */
2724 else if (mode_is_reference(m_b))
2726 else if (mode_is_reference(m_y))
2730 * Both modes are different but none is reference,
2731 * happens for instance in SubP(SubP(P, Iu), Is).
2732 * We have two possibilities here: Cast or ignore.
2733 * Currently we ignore this case.
2738 add = new_r_Add(blk, y, b, add_mode);
2740 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2741 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2745 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2746 if (is_Const(a) && is_Not(b)) {
2747 /* c - ~X = X + (c+1) */
2748 tarval *tv = get_Const_tarval(a);
2750 tv = tarval_add(tv, get_mode_one(mode));
2751 if (tv != tarval_bad) {
2752 ir_node *blk = get_nodes_block(n);
2753 ir_graph *irg = get_irn_irg(n);
2754 ir_node *c = new_r_Const(irg, tv);
2755 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2756 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2762 } /* transform_node_Sub */
2765 * Several transformation done on n*n=2n bits mul.
2766 * These transformations must be done here because new nodes may be produced.
2768 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2771 ir_node *a = get_Mul_left(n);
2772 ir_node *b = get_Mul_right(n);
2773 tarval *ta = value_of(a);
2774 tarval *tb = value_of(b);
2775 ir_mode *smode = get_irn_mode(a);
2777 if (ta == get_mode_one(smode)) {
2778 /* (L)1 * (L)b = (L)b */
2779 ir_node *blk = get_nodes_block(n);
2780 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2781 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2784 else if (ta == get_mode_minus_one(smode)) {
2785 /* (L)-1 * (L)b = (L)b */
2786 ir_node *blk = get_nodes_block(n);
2787 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2788 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2789 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2792 if (tb == get_mode_one(smode)) {
2793 /* (L)a * (L)1 = (L)a */
2794 ir_node *blk = get_irn_n(a, -1);
2795 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2796 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2799 else if (tb == get_mode_minus_one(smode)) {
2800 /* (L)a * (L)-1 = (L)-a */
2801 ir_node *blk = get_nodes_block(n);
2802 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2803 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2804 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2811 * Transform Mul(a,-1) into -a.
2812 * Do constant evaluation of Phi nodes.
2813 * Do architecture dependent optimizations on Mul nodes
2815 static ir_node *transform_node_Mul(ir_node *n)
2817 ir_node *c, *oldn = n;
2818 ir_mode *mode = get_irn_mode(n);
2819 ir_node *a = get_Mul_left(n);
2820 ir_node *b = get_Mul_right(n);
2822 if (is_Bad(a) || is_Bad(b))
2825 if (mode != get_irn_mode(a))
2826 return transform_node_Mul2n(n, mode);
2828 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2830 if (mode_is_signed(mode)) {
2833 if (value_of(a) == get_mode_minus_one(mode))
2835 else if (value_of(b) == get_mode_minus_one(mode))
2838 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2839 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2844 if (is_Const(b)) { /* (-a) * const -> a * -const */
2845 ir_node *cnst = const_negate(b);
2847 dbg_info *dbgi = get_irn_dbg_info(n);
2848 ir_node *block = get_nodes_block(n);
2849 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2850 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2853 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2854 dbg_info *dbgi = get_irn_dbg_info(n);
2855 ir_node *block = get_nodes_block(n);
2856 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2857 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2859 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2860 ir_node *sub_l = get_Sub_left(b);
2861 ir_node *sub_r = get_Sub_right(b);
2862 dbg_info *dbgi = get_irn_dbg_info(n);
2863 ir_node *block = get_nodes_block(n);
2864 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2865 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2866 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2869 } else if (is_Minus(b)) {
2870 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2871 ir_node *sub_l = get_Sub_left(a);
2872 ir_node *sub_r = get_Sub_right(a);
2873 dbg_info *dbgi = get_irn_dbg_info(n);
2874 ir_node *block = get_nodes_block(n);
2875 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2876 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2877 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2880 } else if (is_Shl(a)) {
2881 ir_node *const shl_l = get_Shl_left(a);
2882 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2883 /* (1 << x) * b -> b << x */
2884 dbg_info *const dbgi = get_irn_dbg_info(n);
2885 ir_node *const block = get_nodes_block(n);
2886 ir_node *const shl_r = get_Shl_right(a);
2887 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2888 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2891 } else if (is_Shl(b)) {
2892 ir_node *const shl_l = get_Shl_left(b);
2893 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2894 /* a * (1 << x) -> a << x */
2895 dbg_info *const dbgi = get_irn_dbg_info(n);
2896 ir_node *const block = get_nodes_block(n);
2897 ir_node *const shl_r = get_Shl_right(b);
2898 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2899 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2903 if (get_mode_arithmetic(mode) == irma_ieee754) {
2905 tarval *tv = get_Const_tarval(a);
2906 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2907 && !tarval_is_negative(tv)) {
2908 /* 2.0 * b = b + b */
2909 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2910 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2914 else if (is_Const(b)) {
2915 tarval *tv = get_Const_tarval(b);
2916 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2917 && !tarval_is_negative(tv)) {
2918 /* a * 2.0 = a + a */
2919 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2920 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2925 return arch_dep_replace_mul_with_shifts(n);
2926 } /* transform_node_Mul */
2929 * Transform a Div Node.
2931 static ir_node *transform_node_Div(ir_node *n)
2933 ir_mode *mode = get_Div_resmode(n);
2934 ir_node *a = get_Div_left(n);
2935 ir_node *b = get_Div_right(n);
2937 const ir_node *dummy;
2939 if (is_Const(b) && is_const_Phi(a)) {
2940 /* check for Div(Phi, Const) */
2941 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2943 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2947 else if (is_Const(a) && is_const_Phi(b)) {
2948 /* check for Div(Const, Phi) */
2949 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2951 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2955 else if (is_const_Phi(a) && is_const_Phi(b)) {
2956 /* check for Div(Phi, Phi) */
2957 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2959 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2966 if (a == b && value_not_zero(a, &dummy)) {
2967 ir_graph *irg = get_irn_irg(n);
2968 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2969 value = new_r_Const(irg, get_mode_one(mode));
2970 DBG_OPT_CSTEVAL(n, value);
2973 if (mode_is_signed(mode) && is_Const(b)) {
2974 tarval *tv = get_Const_tarval(b);
2976 if (tv == get_mode_minus_one(mode)) {
2978 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2979 DBG_OPT_CSTEVAL(n, value);
2983 /* Try architecture dependent optimization */
2984 value = arch_dep_replace_div_by_const(n);
2992 /* Turn Div into a tuple (mem, jmp, bad, value) */
2993 mem = get_Div_mem(n);
2994 blk = get_nodes_block(n);
2995 irg = get_irn_irg(blk);
2997 /* skip a potential Pin */
2998 mem = skip_Pin(mem);
2999 turn_into_tuple(n, pn_Div_max);
3000 set_Tuple_pred(n, pn_Div_M, mem);
3001 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3002 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg));
3003 set_Tuple_pred(n, pn_Div_res, value);
3006 } /* transform_node_Div */
3009 * Transform a Mod node.
3011 static ir_node *transform_node_Mod(ir_node *n)
3013 ir_mode *mode = get_Mod_resmode(n);
3014 ir_node *a = get_Mod_left(n);
3015 ir_node *b = get_Mod_right(n);
3020 if (is_Const(b) && is_const_Phi(a)) {
3021 /* check for Div(Phi, Const) */
3022 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3024 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3028 else if (is_Const(a) && is_const_Phi(b)) {
3029 /* check for Div(Const, Phi) */
3030 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3032 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3036 else if (is_const_Phi(a) && is_const_Phi(b)) {
3037 /* check for Div(Phi, Phi) */
3038 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3040 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3047 irg = get_irn_irg(n);
3048 if (tv != tarval_bad) {
3049 value = new_r_Const(irg, tv);
3051 DBG_OPT_CSTEVAL(n, value);
3054 ir_node *a = get_Mod_left(n);
3055 ir_node *b = get_Mod_right(n);
3056 const ir_node *dummy;
3058 if (a == b && value_not_zero(a, &dummy)) {
3059 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3060 value = new_r_Const(irg, get_mode_null(mode));
3061 DBG_OPT_CSTEVAL(n, value);
3064 if (mode_is_signed(mode) && is_Const(b)) {
3065 tarval *tv = get_Const_tarval(b);
3067 if (tv == get_mode_minus_one(mode)) {
3069 value = new_r_Const(irg, get_mode_null(mode));
3070 DBG_OPT_CSTEVAL(n, value);
3074 /* Try architecture dependent optimization */
3075 value = arch_dep_replace_mod_by_const(n);
3084 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3085 mem = get_Mod_mem(n);
3086 blk = get_nodes_block(n);
3087 irg = get_irn_irg(blk);
3089 /* skip a potential Pin */
3090 mem = skip_Pin(mem);
3091 turn_into_tuple(n, pn_Mod_max);
3092 set_Tuple_pred(n, pn_Mod_M, mem);
3093 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3094 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg));
3095 set_Tuple_pred(n, pn_Mod_res, value);
3098 } /* transform_node_Mod */
3101 * Transform a DivMod node.
3103 static ir_node *transform_node_DivMod(ir_node *n)
3105 const ir_node *dummy;
3106 ir_node *a = get_DivMod_left(n);
3107 ir_node *b = get_DivMod_right(n);
3108 ir_mode *mode = get_DivMod_resmode(n);
3110 ir_graph *irg = get_irn_irg(n);
3114 if (is_Const(b) && is_const_Phi(a)) {
3115 /* check for Div(Phi, Const) */
3116 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3117 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3119 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3120 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3124 else if (is_Const(a) && is_const_Phi(b)) {
3125 /* check for Div(Const, Phi) */
3126 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3127 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3129 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3130 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3134 else if (is_const_Phi(a) && is_const_Phi(b)) {
3135 /* check for Div(Phi, Phi) */
3136 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3137 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3139 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3140 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3147 if (tb != tarval_bad) {
3148 if (tb == get_mode_one(get_tarval_mode(tb))) {
3150 vb = new_r_Const(irg, get_mode_null(mode));
3151 DBG_OPT_CSTEVAL(n, vb);
3153 } else if (ta != tarval_bad) {
3154 tarval *resa, *resb;
3155 resa = tarval_div(ta, tb);
3156 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3157 Jmp for X result!? */
3158 resb = tarval_mod(ta, tb);
3159 if (resb == tarval_bad) return n; /* Causes exception! */
3160 va = new_r_Const(irg, resa);
3161 vb = new_r_Const(irg, resb);
3162 DBG_OPT_CSTEVAL(n, va);
3163 DBG_OPT_CSTEVAL(n, vb);
3165 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3166 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3167 vb = new_r_Const(irg, get_mode_null(mode));
3168 DBG_OPT_CSTEVAL(n, va);
3169 DBG_OPT_CSTEVAL(n, vb);
3171 } else { /* Try architecture dependent optimization */
3174 arch_dep_replace_divmod_by_const(&va, &vb, n);
3175 evaluated = va != NULL;
3177 } else if (a == b) {
3178 if (value_not_zero(a, &dummy)) {
3180 va = new_r_Const(irg, get_mode_one(mode));
3181 vb = new_r_Const(irg, get_mode_null(mode));
3182 DBG_OPT_CSTEVAL(n, va);
3183 DBG_OPT_CSTEVAL(n, vb);
3186 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3189 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3190 /* 0 / non-Const = 0 */
3195 if (evaluated) { /* replace by tuple */
3199 mem = get_DivMod_mem(n);
3200 /* skip a potential Pin */
3201 mem = skip_Pin(mem);
3203 blk = get_nodes_block(n);
3204 turn_into_tuple(n, pn_DivMod_max);
3205 set_Tuple_pred(n, pn_DivMod_M, mem);
3206 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3207 set_Tuple_pred(n, pn_DivMod_X_except, new_r_Bad(irg)); /*no exception*/
3208 set_Tuple_pred(n, pn_DivMod_res_div, va);
3209 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3213 } /* transform_node_DivMod */
3216 * Optimize x / c to x * (1/c)
3218 static ir_node *transform_node_Quot(ir_node *n)
3220 ir_mode *mode = get_Quot_resmode(n);
3223 if (get_mode_arithmetic(mode) == irma_ieee754) {
3224 ir_node *b = get_Quot_right(n);
3225 tarval *tv = value_of(b);
3227 if (tv != tarval_bad) {
3228 int rem = tarval_fp_ops_enabled();
3231 * Floating point constant folding might be disabled here to
3233 * However, as we check for exact result, doing it is safe.
3236 tarval_enable_fp_ops(1);
3237 tv = tarval_quo(get_mode_one(mode), tv);
3238 tarval_enable_fp_ops(rem);
3240 /* Do the transformation if the result is either exact or we are not
3241 using strict rules. */
3242 if (tv != tarval_bad &&
3243 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3244 ir_node *blk = get_nodes_block(n);
3245 ir_graph *irg = get_irn_irg(blk);
3246 ir_node *c = new_r_Const(irg, tv);
3247 ir_node *a = get_Quot_left(n);
3248 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3249 ir_node *mem = get_Quot_mem(n);
3251 /* skip a potential Pin */
3252 mem = skip_Pin(mem);
3253 turn_into_tuple(n, pn_Quot_max);
3254 set_Tuple_pred(n, pn_Quot_M, mem);
3255 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3256 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(irg));
3257 set_Tuple_pred(n, pn_Quot_res, m);
3258 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3263 } /* transform_node_Quot */
3266 * Optimize -a CMP -b into b CMP a.
3267 * This works only for for modes where unary Minus
3269 * Note that two-complement integers can Overflow
3270 * so it will NOT work.
3272 * For == and != can be handled in Proj(Cmp)
3274 static ir_node *transform_node_Cmp(ir_node *n)
3277 ir_node *left = get_Cmp_left(n);
3278 ir_node *right = get_Cmp_right(n);
3280 if (is_Minus(left) && is_Minus(right) &&
3281 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3282 ir_node *const new_left = get_Minus_op(right);
3283 ir_node *const new_right = get_Minus_op(left);
3284 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3285 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3288 } /* transform_node_Cmp */
3292 * Transform a Cond node.
3294 * Replace the Cond by a Jmp if it branches on a constant
3297 static ir_node *transform_node_Cond(ir_node *n)
3301 ir_node *a = get_Cond_selector(n);
3302 tarval *ta = value_of(a);
3303 ir_graph *irg = get_irn_irg(n);
3305 /* we need block info which is not available in floating irgs */
3306 if (get_irg_pinned(irg) == op_pin_state_floats)
3309 if ((ta != tarval_bad) &&
3310 (get_irn_mode(a) == mode_b) &&
3311 (get_opt_unreachable_code())) {
3312 /* It's a boolean Cond, branching on a boolean constant.
3313 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3314 ir_node *blk = get_nodes_block(n);
3315 jmp = new_r_Jmp(blk);
3316 turn_into_tuple(n, pn_Cond_max);
3317 if (ta == tarval_b_true) {
3318 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg));
3319 set_Tuple_pred(n, pn_Cond_true, jmp);
3321 set_Tuple_pred(n, pn_Cond_false, jmp);
3322 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg));
3324 /* We might generate an endless loop, so keep it alive. */
3325 add_End_keepalive(get_irg_end(irg), blk);
3328 } /* transform_node_Cond */
3331 * Prototype of a recursive transform function
3332 * for bitwise distributive transformations.
3334 typedef ir_node* (*recursive_transform)(ir_node *n);
3337 * makes use of distributive laws for and, or, eor
3338 * and(a OP c, b OP c) -> and(a, b) OP c
3339 * note, might return a different op than n
3341 static ir_node *transform_bitwise_distributive(ir_node *n,
3342 recursive_transform trans_func)
3345 ir_node *a = get_binop_left(n);
3346 ir_node *b = get_binop_right(n);
3347 ir_op *op = get_irn_op(a);
3348 ir_op *op_root = get_irn_op(n);
3350 if (op != get_irn_op(b))
3353 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3354 if (op == op_Conv) {
3355 ir_node *a_op = get_Conv_op(a);
3356 ir_node *b_op = get_Conv_op(b);
3357 ir_mode *a_mode = get_irn_mode(a_op);
3358 ir_mode *b_mode = get_irn_mode(b_op);
3359 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3360 ir_node *blk = get_nodes_block(n);
3363 set_binop_left(n, a_op);
3364 set_binop_right(n, b_op);
3365 set_irn_mode(n, a_mode);
3367 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3369 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3375 /* nothing to gain here */
3379 if (op == op_Shrs || op == op_Shr || op == op_Shl
3380 || op == op_And || op == op_Or || op == op_Eor) {
3381 ir_node *a_left = get_binop_left(a);
3382 ir_node *a_right = get_binop_right(a);
3383 ir_node *b_left = get_binop_left(b);
3384 ir_node *b_right = get_binop_right(b);
3386 ir_node *op1 = NULL;
3387 ir_node *op2 = NULL;
3389 if (is_op_commutative(op)) {
3390 if (a_left == b_left) {
3394 } else if (a_left == b_right) {
3398 } else if (a_right == b_left) {
3404 if (a_right == b_right) {
3411 /* (a sop c) & (b sop c) => (a & b) sop c */
3412 ir_node *blk = get_nodes_block(n);
3414 ir_node *new_n = exact_copy(n);
3415 set_binop_left(new_n, op1);
3416 set_binop_right(new_n, op2);
3417 new_n = trans_func(new_n);
3419 if (op_root == op_Eor && op == op_Or) {
3420 dbg_info *dbgi = get_irn_dbg_info(n);
3421 ir_mode *mode = get_irn_mode(c);
3423 c = new_rd_Not(dbgi, blk, c, mode);
3424 n = new_rd_And(dbgi, blk, new_n, c, mode);
3427 set_nodes_block(n, blk);
3428 set_binop_left(n, new_n);
3429 set_binop_right(n, c);
3433 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3444 static ir_node *transform_node_And(ir_node *n)
3446 ir_node *c, *oldn = n;
3447 ir_node *a = get_And_left(n);
3448 ir_node *b = get_And_right(n);
3450 vrp_attr *a_vrp, *b_vrp;
3452 mode = get_irn_mode(n);
3453 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3455 /* we can evaluate 2 Projs of the same Cmp */
3456 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3457 ir_node *pred_a = get_Proj_pred(a);
3458 ir_node *pred_b = get_Proj_pred(b);
3459 if (pred_a == pred_b) {
3460 dbg_info *dbgi = get_irn_dbg_info(n);
3461 pn_Cmp pn_a = get_Proj_proj(a);
3462 pn_Cmp pn_b = get_Proj_proj(b);
3463 /* yes, we can simply calculate with pncs */
3464 pn_Cmp new_pnc = pn_a & pn_b;
3466 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3471 ir_node *op = get_Not_op(b);
3473 ir_node *ba = get_And_left(op);
3474 ir_node *bb = get_And_right(op);
3476 /* it's enough to test the following cases due to normalization! */
3477 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3478 /* (a|b) & ~(a&b) = a^b */
3479 ir_node *block = get_nodes_block(n);
3481 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3482 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3490 ir_node *op = get_Not_op(a);
3492 ir_node *aa = get_And_left(op);
3493 ir_node *ab = get_And_right(op);
3495 /* it's enough to test the following cases due to normalization! */
3496 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3497 /* (a|b) & ~(a&b) = a^b */
3498 ir_node *block = get_nodes_block(n);
3500 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3501 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3508 ir_node *al = get_Eor_left(a);
3509 ir_node *ar = get_Eor_right(a);
3512 /* (b ^ a) & b -> ~a & b */
3513 dbg_info *dbg = get_irn_dbg_info(n);
3514 ir_node *block = get_nodes_block(n);
3516 ar = new_rd_Not(dbg, block, ar, mode);
3517 n = new_rd_And(dbg, block, ar, b, mode);
3518 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3522 /* (a ^ b) & b -> ~a & b */
3523 dbg_info *dbg = get_irn_dbg_info(n);
3524 ir_node *block = get_nodes_block(n);
3526 al = new_rd_Not(dbg, block, al, mode);
3527 n = new_rd_And(dbg, block, al, b, mode);
3528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3533 ir_node *bl = get_Eor_left(b);
3534 ir_node *br = get_Eor_right(b);
3537 /* a & (a ^ b) -> a & ~b */
3538 dbg_info *dbg = get_irn_dbg_info(n);
3539 ir_node *block = get_nodes_block(n);
3541 br = new_rd_Not(dbg, block, br, mode);
3542 n = new_rd_And(dbg, block, br, a, mode);
3543 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3547 /* a & (b ^ a) -> a & ~b */
3548 dbg_info *dbg = get_irn_dbg_info(n);
3549 ir_node *block = get_nodes_block(n);
3551 bl = new_rd_Not(dbg, block, bl, mode);
3552 n = new_rd_And(dbg, block, bl, a, mode);
3553 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3557 if (is_Not(a) && is_Not(b)) {
3558 /* ~a & ~b = ~(a|b) */
3559 ir_node *block = get_nodes_block(n);
3560 ir_mode *mode = get_irn_mode(n);
3564 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3565 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3566 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3570 b_vrp = vrp_get_info(b);
3571 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3572 b_vrp->bits_not_set), get_Const_tarval(a)) == pn_Cmp_Eq)) {
3578 a_vrp = vrp_get_info(a);
3579 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3580 a_vrp->bits_not_set), get_Const_tarval(b)) == pn_Cmp_Eq)) {
3584 n = transform_bitwise_distributive(n, transform_node_And);
3587 } /* transform_node_And */
3589 /* the order of the values is important! */
3590 typedef enum const_class {
3596 static const_class classify_const(const ir_node* n)
3598 if (is_Const(n)) return const_const;
3599 if (is_irn_constlike(n)) return const_like;
3604 * Determines whether r is more constlike or has a larger index (in that order)
3607 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3609 const const_class l_order = classify_const(l);
3610 const const_class r_order = classify_const(r);
3612 l_order > r_order ||
3613 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3619 static ir_node *transform_node_Eor(ir_node *n)
3621 ir_node *c, *oldn = n;
3622 ir_node *a = get_Eor_left(n);
3623 ir_node *b = get_Eor_right(n);
3624 ir_mode *mode = get_irn_mode(n);
3626 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3628 /* we can evaluate 2 Projs of the same Cmp */
3629 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3630 ir_node *pred_a = get_Proj_pred(a);
3631 ir_node *pred_b = get_Proj_pred(b);
3632 if (pred_a == pred_b) {
3633 dbg_info *dbgi = get_irn_dbg_info(n);
3634 pn_Cmp pn_a = get_Proj_proj(a);
3635 pn_Cmp pn_b = get_Proj_proj(b);
3636 /* yes, we can simply calculate with pncs */
3637 pn_Cmp new_pnc = pn_a ^ pn_b;
3639 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3643 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3644 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3645 dbg_info *dbg = get_irn_dbg_info(n);
3646 ir_node *block = get_nodes_block(n);
3647 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3648 ir_node *new_left = get_Not_op(a);
3649 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3650 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3652 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3653 dbg_info *dbg = get_irn_dbg_info(n);
3654 ir_node *block = get_nodes_block(n);
3655 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3656 ir_node *new_right = get_Not_op(b);
3657 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3658 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3662 /* x ^ 1...1 -> ~1 */
3663 if (is_Const(b) && is_Const_all_one(b)) {
3664 n = new_r_Not(get_nodes_block(n), a, mode);
3665 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3669 n = transform_bitwise_distributive(n, transform_node_Eor);
3671 } /* transform_node_Eor */
3676 static ir_node *transform_node_Not(ir_node *n)
3678 ir_node *c, *oldn = n;
3679 ir_node *a = get_Not_op(n);
3680 ir_mode *mode = get_irn_mode(n);
3682 HANDLE_UNOP_PHI(tarval_not,a,c);
3684 /* check for a boolean Not */
3685 if (mode == mode_b && is_Proj(a)) {
3686 ir_node *a_pred = get_Proj_pred(a);
3687 if (is_Cmp(a_pred)) {
3688 /* We negate a Cmp. The Cmp has the negated result anyways! */
3689 n = new_r_Proj(get_Proj_pred(a),
3690 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3691 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3696 /* normalize ~(a ^ b) => a ^ ~b */
3698 dbg_info *dbg = get_irn_dbg_info(n);
3699 ir_node *block = get_nodes_block(n);
3700 ir_node *eor_right = get_Eor_right(a);
3701 ir_node *eor_left = get_Eor_left(a);
3702 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3703 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3707 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3708 if (is_Minus(a)) { /* ~-x -> x + -1 */
3709 dbg_info *dbg = get_irn_dbg_info(n);
3710 ir_graph *irg = get_irn_irg(n);
3711 ir_node *block = get_nodes_block(n);
3712 ir_node *add_l = get_Minus_op(a);
3713 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3714 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3715 } else if (is_Add(a)) {
3716 ir_node *add_r = get_Add_right(a);
3717 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3718 /* ~(x + -1) = -x */
3719 ir_node *op = get_Add_left(a);
3720 ir_node *blk = get_nodes_block(n);
3721 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3722 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3727 } /* transform_node_Not */
3730 * Transform a Minus.
3734 * -(a >>u (size-1)) = a >>s (size-1)
3735 * -(a >>s (size-1)) = a >>u (size-1)
3736 * -(a * const) -> a * -const
3738 static ir_node *transform_node_Minus(ir_node *n)
3740 ir_node *c, *oldn = n;
3741 ir_node *a = get_Minus_op(n);
3744 HANDLE_UNOP_PHI(tarval_neg,a,c);
3746 mode = get_irn_mode(a);
3747 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3748 /* the following rules are only to twos-complement */
3751 ir_node *op = get_Not_op(a);
3752 tarval *tv = get_mode_one(mode);
3753 ir_node *blk = get_nodes_block(n);
3754 ir_graph *irg = get_irn_irg(blk);
3755 ir_node *c = new_r_Const(irg, tv);
3756 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3757 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3761 ir_node *c = get_Shr_right(a);
3764 tarval *tv = get_Const_tarval(c);
3766 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3767 /* -(a >>u (size-1)) = a >>s (size-1) */
3768 ir_node *v = get_Shr_left(a);
3770 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3771 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3777 ir_node *c = get_Shrs_right(a);
3780 tarval *tv = get_Const_tarval(c);
3782 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3783 /* -(a >>s (size-1)) = a >>u (size-1) */
3784 ir_node *v = get_Shrs_left(a);
3786 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3787 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3794 /* - (a-b) = b - a */
3795 ir_node *la = get_Sub_left(a);
3796 ir_node *ra = get_Sub_right(a);
3797 ir_node *blk = get_nodes_block(n);
3799 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3800 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3804 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3805 ir_node *mul_l = get_Mul_left(a);
3806 ir_node *mul_r = get_Mul_right(a);
3807 tarval *tv = value_of(mul_r);
3808 if (tv != tarval_bad) {
3809 tv = tarval_neg(tv);
3810 if (tv != tarval_bad) {
3811 ir_graph *irg = get_irn_irg(n);
3812 ir_node *cnst = new_r_Const(irg, tv);
3813 dbg_info *dbg = get_irn_dbg_info(a);
3814 ir_node *block = get_nodes_block(a);
3815 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3816 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3823 } /* transform_node_Minus */
3826 * Transform a Cast_type(Const) into a new Const_type
3828 static ir_node *transform_node_Cast(ir_node *n)
3831 ir_node *pred = get_Cast_op(n);
3832 ir_type *tp = get_irn_type(n);
3834 if (is_Const(pred) && get_Const_type(pred) != tp) {
3835 ir_graph *irg = get_irn_irg(n);
3836 n = new_rd_Const_type(NULL, irg, get_Const_tarval(pred), tp);
3837 DBG_OPT_CSTEVAL(oldn, n);
3838 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3839 ir_graph *irg = get_irn_irg(n);
3840 n = new_rd_SymConst_type(NULL, irg, get_irn_mode(pred),
3841 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3842 DBG_OPT_CSTEVAL(oldn, n);
3846 } /* transform_node_Cast */
3849 * Transform a Proj(Load) with a non-null address.
3851 static ir_node *transform_node_Proj_Load(ir_node *proj)
3853 if (get_opt_ldst_only_null_ptr_exceptions()) {
3854 if (get_irn_mode(proj) == mode_X) {
3855 ir_node *load = get_Proj_pred(proj);
3857 /* get the Load address */
3858 const ir_node *addr = get_Load_ptr(load);
3859 const ir_node *confirm;
3861 if (value_not_null(addr, &confirm)) {
3862 if (confirm == NULL) {
3863 /* this node may float if it did not depend on a Confirm */
3864 set_irn_pinned(load, op_pin_state_floats);
3866 if (get_Proj_proj(proj) == pn_Load_X_except) {
3867 ir_graph *irg = get_irn_irg(proj);
3868 DBG_OPT_EXC_REM(proj);
3869 return get_irg_bad(irg);
3871 ir_node *blk = get_nodes_block(load);
3872 return new_r_Jmp(blk);
3878 } /* transform_node_Proj_Load */
3881 * Transform a Proj(Store) with a non-null address.
3883 static ir_node *transform_node_Proj_Store(ir_node *proj)
3885 if (get_opt_ldst_only_null_ptr_exceptions()) {
3886 if (get_irn_mode(proj) == mode_X) {
3887 ir_node *store = get_Proj_pred(proj);
3889 /* get the load/store address */
3890 const ir_node *addr = get_Store_ptr(store);
3891 const ir_node *confirm;
3893 if (value_not_null(addr, &confirm)) {
3894 if (confirm == NULL) {
3895 /* this node may float if it did not depend on a Confirm */
3896 set_irn_pinned(store, op_pin_state_floats);
3898 if (get_Proj_proj(proj) == pn_Store_X_except) {
3899 ir_graph *irg = get_irn_irg(proj);
3900 DBG_OPT_EXC_REM(proj);
3901 return get_irg_bad(irg);
3903 ir_node *blk = get_nodes_block(store);
3904 return new_r_Jmp(blk);
3910 } /* transform_node_Proj_Store */
3913 * Transform a Proj(Div) with a non-zero value.
3914 * Removes the exceptions and routes the memory to the NoMem node.
3916 static ir_node *transform_node_Proj_Div(ir_node *proj)
3918 ir_node *div = get_Proj_pred(proj);
3919 ir_node *b = get_Div_right(div);
3920 ir_node *res, *new_mem;
3921 const ir_node *confirm;
3924 if (value_not_zero(b, &confirm)) {
3925 /* div(x, y) && y != 0 */
3926 if (confirm == NULL) {
3927 /* we are sure we have a Const != 0 */
3928 new_mem = get_Div_mem(div);
3929 new_mem = skip_Pin(new_mem);
3930 set_Div_mem(div, new_mem);
3931 set_irn_pinned(div, op_pin_state_floats);
3934 proj_nr = get_Proj_proj(proj);
3936 case pn_Div_X_regular:
3937 return new_r_Jmp(get_nodes_block(div));
3939 case pn_Div_X_except: {
3940 ir_graph *irg = get_irn_irg(proj);
3941 /* we found an exception handler, remove it */
3942 DBG_OPT_EXC_REM(proj);
3943 return new_r_Bad(irg);
3947 ir_graph *irg = get_irn_irg(proj);
3948 res = get_Div_mem(div);
3949 new_mem = get_irg_no_mem(irg);
3952 /* This node can only float up to the Confirm block */
3953 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3955 set_irn_pinned(div, op_pin_state_floats);
3956 /* this is a Div without exception, we can remove the memory edge */
3957 set_Div_mem(div, new_mem);
3963 } /* transform_node_Proj_Div */
3966 * Transform a Proj(Mod) with a non-zero value.
3967 * Removes the exceptions and routes the memory to the NoMem node.
3969 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3971 ir_node *mod = get_Proj_pred(proj);
3972 ir_node *b = get_Mod_right(mod);
3973 ir_node *res, *new_mem;
3974 const ir_node *confirm;
3977 if (value_not_zero(b, &confirm)) {
3978 /* mod(x, y) && y != 0 */
3979 proj_nr = get_Proj_proj(proj);
3981 if (confirm == NULL) {
3982 /* we are sure we have a Const != 0 */
3983 new_mem = get_Mod_mem(mod);
3984 new_mem = skip_Pin(new_mem);
3985 set_Mod_mem(mod, new_mem);
3986 set_irn_pinned(mod, op_pin_state_floats);
3991 case pn_Mod_X_regular:
3992 return new_r_Jmp(get_irn_n(mod, -1));
3994 case pn_Mod_X_except: {
3995 ir_graph *irg = get_irn_irg(proj);
3996 /* we found an exception handler, remove it */
3997 DBG_OPT_EXC_REM(proj);
3998 return new_r_Bad(irg);
4002 ir_graph *irg = get_irn_irg(proj);
4003 res = get_Mod_mem(mod);
4004 new_mem = get_irg_no_mem(irg);
4007 /* This node can only float up to the Confirm block */
4008 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4010 /* this is a Mod without exception, we can remove the memory edge */
4011 set_Mod_mem(mod, new_mem);
4015 if (get_Mod_left(mod) == b) {
4016 /* a % a = 0 if a != 0 */
4017 ir_graph *irg = get_irn_irg(proj);
4018 ir_mode *mode = get_irn_mode(proj);
4019 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4021 DBG_OPT_CSTEVAL(mod, res);
4027 } /* transform_node_Proj_Mod */
4030 * Transform a Proj(DivMod) with a non-zero value.
4031 * Removes the exceptions and routes the memory to the NoMem node.
4033 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
4035 ir_node *divmod = get_Proj_pred(proj);
4036 ir_node *b = get_DivMod_right(divmod);
4037 ir_node *res, *new_mem;
4038 const ir_node *confirm;
4041 if (value_not_zero(b, &confirm)) {
4042 /* DivMod(x, y) && y != 0 */
4043 proj_nr = get_Proj_proj(proj);
4045 if (confirm == NULL) {
4046 /* we are sure we have a Const != 0 */
4047 new_mem = get_DivMod_mem(divmod);
4048 new_mem = skip_Pin(new_mem);
4049 set_DivMod_mem(divmod, new_mem);
4050 set_irn_pinned(divmod, op_pin_state_floats);
4055 case pn_DivMod_X_regular:
4056 return new_r_Jmp(get_nodes_block(divmod));
4058 case pn_DivMod_X_except: {
4059 /* we found an exception handler, remove it */
4060 ir_graph *irg = get_irn_irg(proj);
4061 DBG_OPT_EXC_REM(proj);
4062 return new_r_Bad(irg);
4066 ir_graph *irg = get_irn_irg(proj);
4067 res = get_DivMod_mem(divmod);
4068 new_mem = get_irg_no_mem(irg);
4071 /* This node can only float up to the Confirm block */
4072 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4074 /* this is a DivMod without exception, we can remove the memory edge */
4075 set_DivMod_mem(divmod, new_mem);
4079 case pn_DivMod_res_mod:
4080 if (get_DivMod_left(divmod) == b) {
4081 /* a % a = 0 if a != 0 */
4082 ir_graph *irg = get_irn_irg(proj);
4083 ir_mode *mode = get_irn_mode(proj);
4084 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4086 DBG_OPT_CSTEVAL(divmod, res);
4092 } /* transform_node_Proj_DivMod */
4095 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4097 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4099 if (get_opt_unreachable_code()) {
4100 ir_node *n = get_Proj_pred(proj);
4101 ir_node *b = get_Cond_selector(n);
4103 if (mode_is_int(get_irn_mode(b))) {
4104 tarval *tb = value_of(b);
4106 if (tb != tarval_bad) {
4107 /* we have a constant switch */
4108 long num = get_Proj_proj(proj);
4110 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4111 if (get_tarval_long(tb) == num) {
4112 /* Do NOT create a jump here, or we will have 2 control flow ops
4113 * in a block. This case is optimized away in optimize_cf(). */
4116 ir_graph *irg = get_irn_irg(proj);
4117 /* this case will NEVER be taken, kill it */
4118 return get_irg_bad(irg);
4122 long num = get_Proj_proj(proj);
4123 vrp_attr *b_vrp = vrp_get_info(b);
4124 if (num != get_Cond_default_proj(n) && b_vrp) {
4125 /* Try handling with vrp data. We only remove dead parts. */
4126 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4128 if (b_vrp->range_type == VRP_RANGE) {
4129 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4130 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4132 if ((cmp_result & pn_Cmp_Gt) == cmp_result && (cmp_result2
4133 & pn_Cmp_Lt) == cmp_result2) {
4134 ir_graph *irg = get_irn_irg(proj);
4135 return get_irg_bad(irg);
4137 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
4138 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4139 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4141 if ((cmp_result & pn_Cmp_Le) == cmp_result && (cmp_result2
4142 & pn_Cmp_Ge) == cmp_result2) {
4143 ir_graph *irg = get_irn_irg(proj);
4144 return get_irg_bad(irg);
4149 tarval_and( b_vrp->bits_set, tp),
4152 ir_graph *irg = get_irn_irg(proj);
4153 return get_irg_bad(irg);
4159 tarval_not(b_vrp->bits_not_set)),
4160 tarval_not(b_vrp->bits_not_set))
4162 ir_graph *irg = get_irn_irg(proj);
4163 return get_irg_bad(irg);
4172 } /* transform_node_Proj_Cond */
4175 * Create a 0 constant of given mode.
4177 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
4179 tarval *tv = get_mode_null(mode);
4180 ir_node *cnst = new_r_Const(irg, tv);
4186 * Normalizes and optimizes Cmp nodes.
4188 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4190 ir_node *n = get_Proj_pred(proj);
4191 ir_node *left = get_Cmp_left(n);
4192 ir_node *right = get_Cmp_right(n);
4195 ir_mode *mode = NULL;
4196 long proj_nr = get_Proj_proj(proj);
4198 /* we can evaluate some cases directly */
4200 case pn_Cmp_False: {
4201 ir_graph *irg = get_irn_irg(proj);
4202 return new_r_Const(irg, get_tarval_b_false());
4205 ir_graph *irg = get_irn_irg(proj);
4206 return new_r_Const(irg, get_tarval_b_true());
4209 if (!mode_is_float(get_irn_mode(left))) {
4210 ir_graph *irg = get_irn_irg(proj);
4211 return new_r_Const(irg, get_tarval_b_true());
4218 /* remove Casts of both sides */
4219 left = skip_Cast(left);
4220 right = skip_Cast(right);
4222 /* Remove unnecessary conversions */
4223 /* TODO handle constants */
4224 if (is_Conv(left) && is_Conv(right)) {
4225 ir_mode *mode = get_irn_mode(left);
4226 ir_node *op_left = get_Conv_op(left);
4227 ir_node *op_right = get_Conv_op(right);
4228 ir_mode *mode_left = get_irn_mode(op_left);
4229 ir_mode *mode_right = get_irn_mode(op_right);
4231 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4232 && mode_left != mode_b && mode_right != mode_b) {
4233 ir_node *block = get_nodes_block(n);
4235 if (mode_left == mode_right) {
4239 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4240 } else if (smaller_mode(mode_left, mode_right)) {
4241 left = new_r_Conv(block, op_left, mode_right);
4244 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4245 } else if (smaller_mode(mode_right, mode_left)) {
4247 right = new_r_Conv(block, op_right, mode_left);
4249 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4254 /* remove operation on both sides if possible */
4255 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4257 * The following operations are NOT safe for floating point operations, for instance
4258 * 1.0 + inf == 2.0 + inf, =/=> x == y
4260 if (mode_is_int(get_irn_mode(left))) {
4261 unsigned lop = get_irn_opcode(left);
4263 if (lop == get_irn_opcode(right)) {
4264 ir_node *ll, *lr, *rl, *rr;
4266 /* same operation on both sides, try to remove */
4270 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4271 left = get_unop_op(left);
4272 right = get_unop_op(right);
4274 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4277 ll = get_Add_left(left);
4278 lr = get_Add_right(left);
4279 rl = get_Add_left(right);
4280 rr = get_Add_right(right);
4283 /* X + a CMP X + b ==> a CMP b */
4287 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4288 } else if (ll == rr) {
4289 /* X + a CMP b + X ==> a CMP b */
4293 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4294 } else if (lr == rl) {
4295 /* a + X CMP X + b ==> a CMP b */
4299 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4300 } else if (lr == rr) {
4301 /* a + X CMP b + X ==> a CMP b */
4305 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4309 ll = get_Sub_left(left);
4310 lr = get_Sub_right(left);
4311 rl = get_Sub_left(right);
4312 rr = get_Sub_right(right);
4315 /* X - a CMP X - b ==> a CMP b */
4319 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4320 } else if (lr == rr) {
4321 /* a - X CMP b - X ==> a CMP b */
4325 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4329 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4330 /* a ROTL X CMP b ROTL X ==> a CMP b */
4331 left = get_Rotl_left(left);
4332 right = get_Rotl_left(right);
4334 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4342 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4343 if (is_Add(left) || is_Sub(left)) {
4344 ir_node *ll = get_binop_left(left);
4345 ir_node *lr = get_binop_right(left);
4347 if (lr == right && is_Add(left)) {
4353 ir_graph *irg = get_irn_irg(n);
4355 right = create_zero_const(irg, get_irn_mode(left));
4357 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4360 if (is_Add(right) || is_Sub(right)) {
4361 ir_node *rl = get_binop_left(right);
4362 ir_node *rr = get_binop_right(right);
4364 if (rr == left && is_Add(right)) {
4370 ir_graph *irg = get_irn_irg(n);
4372 right = create_zero_const(irg, get_irn_mode(left));
4374 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4377 if (is_And(left) && is_Const(right)) {
4378 ir_node *ll = get_binop_left(left);
4379 ir_node *lr = get_binop_right(left);
4380 if (is_Shr(ll) && is_Const(lr)) {
4381 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4382 ir_node *block = get_nodes_block(n);
4383 ir_mode *mode = get_irn_mode(left);
4385 ir_node *llr = get_Shr_right(ll);
4386 if (is_Const(llr)) {
4387 dbg_info *dbg = get_irn_dbg_info(left);
4388 ir_graph *irg = get_irn_irg(left);
4390 tarval *c1 = get_Const_tarval(llr);
4391 tarval *c2 = get_Const_tarval(lr);
4392 tarval *c3 = get_Const_tarval(right);
4393 tarval *mask = tarval_shl(c2, c1);
4394 tarval *value = tarval_shl(c3, c1);
4396 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4397 right = new_r_Const(irg, value);
4402 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4404 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4405 right = get_Eor_right(left);
4406 left = get_Eor_left(left);
4409 } /* mode_is_int(...) */
4410 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4412 /* replace mode_b compares with ands/ors */
4413 if (get_irn_mode(left) == mode_b) {
4414 ir_node *block = get_nodes_block(n);
4418 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4419 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4420 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4421 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4422 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4423 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4424 default: bres = NULL;
4427 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4433 * First step: normalize the compare op
4434 * by placing the constant on the right side
4435 * or moving the lower address node to the left.
4437 if (!operands_are_normalized(left, right)) {
4443 proj_nr = get_inversed_pnc(proj_nr);
4448 * Second step: Try to reduce the magnitude
4449 * of a constant. This may help to generate better code
4450 * later and may help to normalize more compares.
4451 * Of course this is only possible for integer values.
4453 tv = value_of(right);
4454 if (tv != tarval_bad) {
4455 mode = get_irn_mode(right);
4457 /* TODO extend to arbitrary constants */
4458 if (is_Conv(left) && tarval_is_null(tv)) {
4459 ir_node *op = get_Conv_op(left);
4460 ir_mode *op_mode = get_irn_mode(op);
4463 * UpConv(x) REL 0 ==> x REL 0
4464 * Don't do this for float values as it's unclear whether it is a
4465 * win. (on the other side it makes detection/creation of fabs hard)
4467 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4468 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4469 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4470 !mode_is_float(mode)) {
4471 tv = get_mode_null(op_mode);
4475 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4479 if (tv != tarval_bad) {
4480 /* the following optimization is possible on modes without Overflow
4481 * on Unary Minus or on == and !=:
4482 * -a CMP c ==> a swap(CMP) -c
4484 * Beware: for two-complement Overflow may occur, so only == and != can
4485 * be optimized, see this:
4486 * -MININT < 0 =/=> MININT > 0 !!!
4488 if (is_Minus(left) &&
4489 (!mode_overflow_on_unary_Minus(mode) ||
4490 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4491 tv = tarval_neg(tv);
4493 if (tv != tarval_bad) {
4494 left = get_Minus_op(left);
4495 proj_nr = get_inversed_pnc(proj_nr);
4497 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4499 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4500 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4501 tv = tarval_not(tv);
4503 if (tv != tarval_bad) {
4504 left = get_Not_op(left);
4506 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4510 /* for integer modes, we have more */
4511 if (mode_is_int(mode)) {
4512 /* Ne includes Unordered which is not possible on integers.
4513 * However, frontends often use this wrong, so fix it here */
4514 if (proj_nr & pn_Cmp_Uo) {
4515 proj_nr &= ~pn_Cmp_Uo;
4516 set_Proj_proj(proj, proj_nr);
4519 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4520 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4521 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4522 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4524 if (tv != tarval_bad) {
4525 proj_nr ^= pn_Cmp_Eq;
4527 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4530 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4531 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4532 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4533 tv = tarval_add(tv, get_mode_one(mode));
4535 if (tv != tarval_bad) {
4536 proj_nr ^= pn_Cmp_Eq;
4538 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4542 /* the following reassociations work only for == and != */
4543 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4545 #if 0 /* Might be not that good in general */
4546 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4547 if (tarval_is_null(tv) && is_Sub(left)) {
4548 right = get_Sub_right(left);
4549 left = get_Sub_left(left);
4551 tv = value_of(right);
4553 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4557 if (tv != tarval_bad) {
4558 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4560 ir_node *c1 = get_Sub_right(left);
4561 tarval *tv2 = value_of(c1);
4563 if (tv2 != tarval_bad) {
4564 tv2 = tarval_add(tv, value_of(c1));
4566 if (tv2 != tarval_bad) {
4567 left = get_Sub_left(left);
4570 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4574 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4575 else if (is_Add(left)) {
4576 ir_node *a_l = get_Add_left(left);
4577 ir_node *a_r = get_Add_right(left);
4581 if (is_Const(a_l)) {
4583 tv2 = value_of(a_l);
4586 tv2 = value_of(a_r);
4589 if (tv2 != tarval_bad) {
4590 tv2 = tarval_sub(tv, tv2, NULL);
4592 if (tv2 != tarval_bad) {
4596 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4600 /* -a == c ==> a == -c, -a != c ==> a != -c */
4601 else if (is_Minus(left)) {
4602 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4604 if (tv2 != tarval_bad) {
4605 left = get_Minus_op(left);
4608 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4615 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4616 switch (get_irn_opcode(left)) {
4620 c1 = get_And_right(left);
4623 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4624 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4626 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4628 /* TODO: move to constant evaluation */
4629 ir_graph *irg = get_irn_irg(n);
4630 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4631 c1 = new_r_Const(irg, tv);
4632 DBG_OPT_CSTEVAL(proj, c1);
4636 if (tarval_is_single_bit(tv)) {
4638 * optimization for AND:
4640 * And(x, C) == C ==> And(x, C) != 0
4641 * And(x, C) != C ==> And(X, C) == 0
4643 * if C is a single Bit constant.
4646 /* check for Constant's match. We have check hare the tarvals,
4647 because our const might be changed */
4648 if (get_Const_tarval(c1) == tv) {
4649 /* fine: do the transformation */
4650 tv = get_mode_null(get_tarval_mode(tv));
4651 proj_nr ^= pn_Cmp_Leg;
4653 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4659 c1 = get_Or_right(left);
4660 if (is_Const(c1) && tarval_is_null(tv)) {
4662 * Or(x, C) == 0 && C != 0 ==> FALSE
4663 * Or(x, C) != 0 && C != 0 ==> TRUE
4665 if (! tarval_is_null(get_Const_tarval(c1))) {
4666 /* TODO: move to constant evaluation */
4667 ir_graph *irg = get_irn_irg(n);
4668 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4669 c1 = new_r_Const(irg, tv);
4670 DBG_OPT_CSTEVAL(proj, c1);
4677 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4679 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4682 c1 = get_Shl_right(left);
4684 ir_graph *irg = get_irn_irg(c1);
4685 tarval *tv1 = get_Const_tarval(c1);
4686 ir_mode *mode = get_irn_mode(left);
4687 tarval *minus1 = get_mode_all_one(mode);
4688 tarval *amask = tarval_shr(minus1, tv1);
4689 tarval *cmask = tarval_shl(minus1, tv1);
4692 if (tarval_and(tv, cmask) != tv) {
4693 /* condition not met */
4694 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4695 c1 = new_r_Const(irg, tv);
4696 DBG_OPT_CSTEVAL(proj, c1);
4699 sl = get_Shl_left(left);
4700 blk = get_nodes_block(n);
4701 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4702 tv = tarval_shr(tv, tv1);
4704 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4709 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4711 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4714 c1 = get_Shr_right(left);
4716 ir_graph *irg = get_irn_irg(c1);
4717 tarval *tv1 = get_Const_tarval(c1);
4718 ir_mode *mode = get_irn_mode(left);
4719 tarval *minus1 = get_mode_all_one(mode);
4720 tarval *amask = tarval_shl(minus1, tv1);
4721 tarval *cmask = tarval_shr(minus1, tv1);
4724 if (tarval_and(tv, cmask) != tv) {
4725 /* condition not met */
4726 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4727 c1 = new_r_Const(irg, tv);
4728 DBG_OPT_CSTEVAL(proj, c1);
4731 sl = get_Shr_left(left);
4732 blk = get_nodes_block(n);
4733 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4734 tv = tarval_shl(tv, tv1);
4736 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4741 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4743 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4746 c1 = get_Shrs_right(left);
4748 ir_graph *irg = get_irn_irg(c1);
4749 tarval *tv1 = get_Const_tarval(c1);
4750 ir_mode *mode = get_irn_mode(left);
4751 tarval *minus1 = get_mode_all_one(mode);
4752 tarval *amask = tarval_shl(minus1, tv1);
4753 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4756 cond = tarval_sub(cond, tv1, NULL);
4757 cond = tarval_shrs(tv, cond);
4759 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4760 /* condition not met */
4761 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4762 c1 = new_r_Const(irg, tv);
4763 DBG_OPT_CSTEVAL(proj, c1);
4766 sl = get_Shrs_left(left);
4767 blk = get_nodes_block(n);
4768 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4769 tv = tarval_shl(tv, tv1);
4771 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4776 } /* tarval != bad */
4779 if (changed & 2) { /* need a new Const */
4780 ir_graph *irg = get_irn_irg(n);
4781 right = new_r_Const(irg, tv);
4784 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4785 ir_node *op = get_Proj_pred(left);
4787 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4788 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4789 ir_node *c = get_binop_right(op);
4792 tarval *tv = get_Const_tarval(c);
4794 if (tarval_is_single_bit(tv)) {
4795 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4796 ir_node *v = get_binop_left(op);
4797 ir_node *blk = get_irn_n(op, -1);
4798 ir_graph *irg = get_irn_irg(op);
4799 ir_mode *mode = get_irn_mode(v);
4801 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4802 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4804 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4811 ir_node *block = get_nodes_block(n);
4813 /* create a new compare */
4814 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4815 proj = new_rd_Proj(get_irn_dbg_info(proj), n, get_irn_mode(proj), proj_nr);
4819 } /* transform_node_Proj_Cmp */
4822 * Optimize CopyB(mem, x, x) into a Nop.
4824 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4826 ir_node *copyb = get_Proj_pred(proj);
4827 ir_node *a = get_CopyB_dst(copyb);
4828 ir_node *b = get_CopyB_src(copyb);
4831 switch (get_Proj_proj(proj)) {
4832 case pn_CopyB_X_regular:
4833 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4834 DBG_OPT_EXC_REM(proj);
4835 proj = new_r_Jmp(get_nodes_block(copyb));
4837 case pn_CopyB_X_except:
4838 DBG_OPT_EXC_REM(proj);
4839 proj = get_irg_bad(get_irn_irg(proj));
4846 } /* transform_node_Proj_CopyB */
4849 * Optimize Bounds(idx, idx, upper) into idx.
4851 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4853 ir_node *oldn = proj;
4854 ir_node *bound = get_Proj_pred(proj);
4855 ir_node *idx = get_Bound_index(bound);
4856 ir_node *pred = skip_Proj(idx);
4859 if (idx == get_Bound_lower(bound))
4861 else if (is_Bound(pred)) {
4863 * idx was Bounds checked previously, it is still valid if
4864 * lower <= pred_lower && pred_upper <= upper.
4866 ir_node *lower = get_Bound_lower(bound);
4867 ir_node *upper = get_Bound_upper(bound);
4868 if (get_Bound_lower(pred) == lower &&
4869 get_Bound_upper(pred) == upper) {
4871 * One could expect that we simply return the previous
4872 * Bound here. However, this would be wrong, as we could
4873 * add an exception Proj to a new location then.
4874 * So, we must turn in into a tuple.
4880 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4881 switch (get_Proj_proj(proj)) {
4883 DBG_OPT_EXC_REM(proj);
4884 proj = get_Bound_mem(bound);
4886 case pn_Bound_X_except:
4887 DBG_OPT_EXC_REM(proj);
4888 proj = get_irg_bad(get_irn_irg(proj));
4892 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4894 case pn_Bound_X_regular:
4895 DBG_OPT_EXC_REM(proj);
4896 proj = new_r_Jmp(get_nodes_block(bound));
4903 } /* transform_node_Proj_Bound */
4906 * Does all optimizations on nodes that must be done on it's Proj's
4907 * because of creating new nodes.
4909 static ir_node *transform_node_Proj(ir_node *proj)
4911 ir_node *n = get_Proj_pred(proj);
4913 if (n->op->ops.transform_node_Proj)
4914 return n->op->ops.transform_node_Proj(proj);
4916 } /* transform_node_Proj */
4919 * Move Confirms down through Phi nodes.
4921 static ir_node *transform_node_Phi(ir_node *phi)
4924 ir_mode *mode = get_irn_mode(phi);
4926 if (mode_is_reference(mode)) {
4927 n = get_irn_arity(phi);
4929 /* Beware of Phi0 */
4931 ir_node *pred = get_irn_n(phi, 0);
4932 ir_node *bound, *new_phi, *block, **in;
4935 if (! is_Confirm(pred))
4938 bound = get_Confirm_bound(pred);
4939 pnc = get_Confirm_cmp(pred);
4941 NEW_ARR_A(ir_node *, in, n);
4942 in[0] = get_Confirm_value(pred);
4944 for (i = 1; i < n; ++i) {
4945 pred = get_irn_n(phi, i);
4947 if (! is_Confirm(pred) ||
4948 get_Confirm_bound(pred) != bound ||
4949 get_Confirm_cmp(pred) != pnc)
4951 in[i] = get_Confirm_value(pred);
4953 /* move the Confirm nodes "behind" the Phi */
4954 block = get_irn_n(phi, -1);
4955 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4956 return new_r_Confirm(block, new_phi, bound, pnc);
4960 } /* transform_node_Phi */
4963 * Returns the operands of a commutative bin-op, if one operand is
4964 * a const, it is returned as the second one.
4966 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4968 ir_node *op_a = get_binop_left(binop);
4969 ir_node *op_b = get_binop_right(binop);
4971 assert(is_op_commutative(get_irn_op(binop)));
4973 if (is_Const(op_a)) {
4980 } /* get_comm_Binop_Ops */
4983 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4984 * Such pattern may arise in bitfield stores.
4986 * value c4 value c4 & c2
4987 * AND c3 AND c1 | c3
4994 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4997 static ir_node *transform_node_Or_bf_store(ir_node *or)
5001 ir_node *and_l, *c3;
5002 ir_node *value, *c4;
5003 ir_node *new_and, *new_const, *block;
5004 ir_mode *mode = get_irn_mode(or);
5006 tarval *tv1, *tv2, *tv3, *tv4, *tv;
5010 get_comm_Binop_Ops(or, &and, &c1);
5011 if (!is_Const(c1) || !is_And(and))
5014 get_comm_Binop_Ops(and, &or_l, &c2);
5018 tv1 = get_Const_tarval(c1);
5019 tv2 = get_Const_tarval(c2);
5021 tv = tarval_or(tv1, tv2);
5022 if (tarval_is_all_one(tv)) {
5023 /* the AND does NOT clear a bit with isn't set by the OR */
5024 set_Or_left(or, or_l);
5025 set_Or_right(or, c1);
5027 /* check for more */
5034 get_comm_Binop_Ops(or_l, &and_l, &c3);
5035 if (!is_Const(c3) || !is_And(and_l))
5038 get_comm_Binop_Ops(and_l, &value, &c4);
5042 /* ok, found the pattern, check for conditions */
5043 assert(mode == get_irn_mode(and));
5044 assert(mode == get_irn_mode(or_l));
5045 assert(mode == get_irn_mode(and_l));
5047 tv3 = get_Const_tarval(c3);
5048 tv4 = get_Const_tarval(c4);
5050 tv = tarval_or(tv4, tv2);
5051 if (!tarval_is_all_one(tv)) {
5052 /* have at least one 0 at the same bit position */
5056 if (tv3 != tarval_andnot(tv3, tv4)) {
5057 /* bit in the or_mask is outside the and_mask */
5061 if (tv1 != tarval_andnot(tv1, tv2)) {
5062 /* bit in the or_mask is outside the and_mask */
5066 /* ok, all conditions met */
5067 block = get_irn_n(or, -1);
5068 irg = get_irn_irg(block);
5070 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
5072 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
5074 set_Or_left(or, new_and);
5075 set_Or_right(or, new_const);
5077 /* check for more */
5079 } /* transform_node_Or_bf_store */
5082 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5084 static ir_node *transform_node_Or_Rotl(ir_node *or)
5086 ir_mode *mode = get_irn_mode(or);
5087 ir_node *shl, *shr, *block;
5088 ir_node *irn, *x, *c1, *c2, *n;
5091 /* some backends can't handle rotl */
5092 if (!be_get_backend_param()->support_rotl)
5095 if (! mode_is_int(mode))
5098 shl = get_binop_left(or);
5099 shr = get_binop_right(or);
5108 } else if (!is_Shl(shl)) {
5110 } else if (!is_Shr(shr)) {
5113 x = get_Shl_left(shl);
5114 if (x != get_Shr_left(shr))
5117 c1 = get_Shl_right(shl);
5118 c2 = get_Shr_right(shr);
5119 if (is_Const(c1) && is_Const(c2)) {
5120 tv1 = get_Const_tarval(c1);
5121 if (! tarval_is_long(tv1))
5124 tv2 = get_Const_tarval(c2);
5125 if (! tarval_is_long(tv2))
5128 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5129 != (int) get_mode_size_bits(mode))
5132 /* yet, condition met */
5133 block = get_nodes_block(or);
5135 n = new_r_Rotl(block, x, c1, mode);
5137 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5141 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
5142 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
5143 if (!is_negated_value(c1, c2)) {
5147 /* yet, condition met */
5148 block = get_nodes_block(or);
5149 n = new_r_Rotl(block, x, c1, mode);
5150 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5152 } /* transform_node_Or_Rotl */
5157 static ir_node *transform_node_Or(ir_node *n)
5159 ir_node *c, *oldn = n;
5160 ir_node *a = get_Or_left(n);
5161 ir_node *b = get_Or_right(n);
5164 if (is_Not(a) && is_Not(b)) {
5165 /* ~a | ~b = ~(a&b) */
5166 ir_node *block = get_nodes_block(n);
5168 mode = get_irn_mode(n);
5171 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5172 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5173 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5177 /* we can evaluate 2 Projs of the same Cmp */
5178 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5179 ir_node *pred_a = get_Proj_pred(a);
5180 ir_node *pred_b = get_Proj_pred(b);
5181 if (pred_a == pred_b) {
5182 dbg_info *dbgi = get_irn_dbg_info(n);
5183 pn_Cmp pn_a = get_Proj_proj(a);
5184 pn_Cmp pn_b = get_Proj_proj(b);
5185 /* yes, we can simply calculate with pncs */
5186 pn_Cmp new_pnc = pn_a | pn_b;
5188 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
5192 mode = get_irn_mode(n);
5193 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5195 n = transform_node_Or_bf_store(n);
5196 n = transform_node_Or_Rotl(n);
5200 n = transform_bitwise_distributive(n, transform_node_Or);
5203 } /* transform_node_Or */
5207 static ir_node *transform_node(ir_node *n);
5210 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5212 * Should be moved to reassociation?
5214 static ir_node *transform_node_shift(ir_node *n)
5216 ir_node *left, *right;
5218 tarval *tv1, *tv2, *res;
5219 ir_node *in[2], *irn, *block;
5222 left = get_binop_left(n);
5224 /* different operations */
5225 if (get_irn_op(left) != get_irn_op(n))
5228 right = get_binop_right(n);
5229 tv1 = value_of(right);
5230 if (tv1 == tarval_bad)
5233 tv2 = value_of(get_binop_right(left));
5234 if (tv2 == tarval_bad)
5237 res = tarval_add(tv1, tv2);
5238 mode = get_irn_mode(n);
5239 irg = get_irn_irg(n);
5241 /* beware: a simple replacement works only, if res < modulo shift */
5243 int modulo_shf = get_mode_modulo_shift(mode);
5244 if (modulo_shf > 0) {
5245 tarval *modulo = new_tarval_from_long(modulo_shf,
5246 get_tarval_mode(res));
5248 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5250 /* shifting too much */
5251 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5253 ir_node *block = get_nodes_block(n);
5254 dbg_info *dbgi = get_irn_dbg_info(n);
5255 ir_mode *smode = get_irn_mode(right);
5256 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5257 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5260 return new_r_Const(irg, get_mode_null(mode));
5264 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5267 /* ok, we can replace it */
5268 block = get_nodes_block(n);
5270 in[0] = get_binop_left(left);
5271 in[1] = new_r_Const(irg, res);
5273 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5275 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5277 return transform_node(irn);
5278 } /* transform_node_shift */
5281 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5283 * - and, or, xor instead of &
5284 * - Shl, Shr, Shrs, rotl instead of >>
5285 * (with a special case for Or/Xor + Shrs)
5287 static ir_node *transform_node_bitop_shift(ir_node *n)
5290 ir_node *right = get_binop_right(n);
5291 ir_mode *mode = get_irn_mode(n);
5292 ir_node *bitop_left;
5293 ir_node *bitop_right;
5305 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5307 if (!is_Const(right))
5310 left = get_binop_left(n);
5311 op_left = get_irn_op(left);
5312 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5315 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5316 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5317 /* TODO: test if sign bit is affectes */
5321 bitop_right = get_binop_right(left);
5322 if (!is_Const(bitop_right))
5325 bitop_left = get_binop_left(left);
5327 block = get_nodes_block(n);
5328 dbgi = get_irn_dbg_info(n);
5329 tv1 = get_Const_tarval(bitop_right);
5330 tv2 = get_Const_tarval(right);
5332 assert(get_tarval_mode(tv1) == mode);
5335 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5336 tv_shift = tarval_shl(tv1, tv2);
5337 } else if (is_Shr(n)) {
5338 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5339 tv_shift = tarval_shr(tv1, tv2);
5340 } else if (is_Shrs(n)) {
5341 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5342 tv_shift = tarval_shrs(tv1, tv2);
5345 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5346 tv_shift = tarval_rotl(tv1, tv2);
5349 assert(get_tarval_mode(tv_shift) == mode);
5350 irg = get_irn_irg(n);
5351 new_const = new_r_Const(irg, tv_shift);
5353 if (op_left == op_And) {
5354 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5355 } else if (op_left == op_Or) {
5356 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5358 assert(op_left == op_Eor);
5359 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5367 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5369 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5370 * (also with x >>s c1 when c1>=c2)
5372 static ir_node *transform_node_shl_shr(ir_node *n)
5375 ir_node *right = get_binop_right(n);
5391 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5393 if (!is_Const(right))
5396 left = get_binop_left(n);
5397 mode = get_irn_mode(n);
5398 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5399 ir_node *shr_right = get_binop_right(left);
5401 if (!is_Const(shr_right))
5404 x = get_binop_left(left);
5405 tv_shr = get_Const_tarval(shr_right);
5406 tv_shl = get_Const_tarval(right);
5408 if (is_Shrs(left)) {
5409 /* shrs variant only allowed if c1 >= c2 */
5410 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5413 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5416 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5418 tv_mask = tarval_shl(tv_mask, tv_shl);
5419 } else if (is_Shr(n) && is_Shl(left)) {
5420 ir_node *shl_right = get_Shl_right(left);
5422 if (!is_Const(shl_right))
5425 x = get_Shl_left(left);
5426 tv_shr = get_Const_tarval(right);
5427 tv_shl = get_Const_tarval(shl_right);
5429 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5430 tv_mask = tarval_shr(tv_mask, tv_shr);
5435 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5436 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5439 assert(tv_mask != tarval_bad);
5440 assert(get_tarval_mode(tv_mask) == mode);
5442 block = get_nodes_block(n);
5443 irg = get_irn_irg(block);
5444 dbgi = get_irn_dbg_info(n);
5446 pnc = tarval_cmp(tv_shl, tv_shr);
5447 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5448 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5449 new_const = new_r_Const(irg, tv_shift);
5451 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5453 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5456 assert(pnc == pn_Cmp_Gt);
5457 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5458 new_const = new_r_Const(irg, tv_shift);
5459 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5462 new_const = new_r_Const(irg, tv_mask);
5463 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5468 static tarval *get_modulo_tv_value(tarval *tv, int modulo_val)
5470 ir_mode *mode = get_tarval_mode(tv);
5471 tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5472 return tarval_mod(tv, modulo_tv);
5475 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5476 ir_node *left, ir_node *right, ir_mode *mode);
5479 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5480 * then we can use that to minimize the value of Add(x, const) or
5481 * Sub(Const, x). In particular this often avoids 1 instruction in some
5482 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5483 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5485 static ir_node *transform_node_shift_modulo(ir_node *n,
5486 new_shift_func new_shift)
5488 ir_mode *mode = get_irn_mode(n);
5489 int modulo = get_mode_modulo_shift(mode);
5490 ir_node *newop = NULL;
5491 ir_mode *mode_right;
5498 if (get_mode_arithmetic(mode) != irma_twos_complement)
5500 if (!is_po2(modulo))
5503 irg = get_irn_irg(n);
5504 block = get_nodes_block(n);
5505 right = get_binop_right(n);
5506 mode_right = get_irn_mode(right);
5507 if (is_Const(right)) {
5508 tarval *tv = get_Const_tarval(right);
5509 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5514 newop = new_r_Const(irg, tv_mod);
5515 } else if (is_Add(right)) {
5516 ir_node *add_right = get_Add_right(right);
5517 if (is_Const(add_right)) {
5518 tarval *tv = get_Const_tarval(add_right);
5519 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5524 newconst = new_r_Const(irg, tv_mod);
5525 newop = new_r_Add(block, get_Add_left(right), newconst,
5528 } else if (is_Sub(right)) {
5529 ir_node *sub_left = get_Sub_left(right);
5530 if (is_Const(sub_left)) {
5531 tarval *tv = get_Const_tarval(sub_left);
5532 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5537 newconst = new_r_Const(irg, tv_mod);
5538 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5545 if (newop != NULL) {
5546 dbg_info *dbgi = get_irn_dbg_info(n);
5547 ir_node *left = get_binop_left(n);
5548 return new_shift(dbgi, block, left, newop, mode);
5556 static ir_node *transform_node_Shr(ir_node *n)
5558 ir_node *c, *oldn = n;
5559 ir_node *left = get_Shr_left(n);
5560 ir_node *right = get_Shr_right(n);
5561 ir_mode *mode = get_irn_mode(n);
5563 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5564 n = transform_node_shift(n);
5567 n = transform_node_shift_modulo(n, new_rd_Shr);
5569 n = transform_node_shl_shr(n);
5571 n = transform_node_bitop_shift(n);
5574 } /* transform_node_Shr */
5579 static ir_node *transform_node_Shrs(ir_node *n)
5581 ir_node *c, *oldn = n;
5582 ir_node *a = get_Shrs_left(n);
5583 ir_node *b = get_Shrs_right(n);
5584 ir_mode *mode = get_irn_mode(n);
5586 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5587 n = transform_node_shift(n);
5590 n = transform_node_shift_modulo(n, new_rd_Shrs);
5592 n = transform_node_bitop_shift(n);
5595 } /* transform_node_Shrs */
5600 static ir_node *transform_node_Shl(ir_node *n)
5602 ir_node *c, *oldn = n;
5603 ir_node *a = get_Shl_left(n);
5604 ir_node *b = get_Shl_right(n);
5605 ir_mode *mode = get_irn_mode(n);
5607 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5608 n = transform_node_shift(n);
5611 n = transform_node_shift_modulo(n, new_rd_Shl);
5613 n = transform_node_shl_shr(n);
5615 n = transform_node_bitop_shift(n);
5618 } /* transform_node_Shl */
5623 static ir_node *transform_node_Rotl(ir_node *n)
5625 ir_node *c, *oldn = n;
5626 ir_node *a = get_Rotl_left(n);
5627 ir_node *b = get_Rotl_right(n);
5628 ir_mode *mode = get_irn_mode(n);
5630 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5631 n = transform_node_shift(n);
5634 n = transform_node_bitop_shift(n);
5637 } /* transform_node_Rotl */
5642 static ir_node *transform_node_Conv(ir_node *n)
5644 ir_node *c, *oldn = n;
5645 ir_mode *mode = get_irn_mode(n);
5646 ir_node *a = get_Conv_op(n);
5648 if (mode != mode_b && is_const_Phi(a)) {
5649 /* Do NOT optimize mode_b Conv's, this leads to remaining
5650 * Phib nodes later, because the conv_b_lower operation
5651 * is instantly reverted, when it tries to insert a Convb.
5653 c = apply_conv_on_phi(a, mode);
5655 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5660 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5661 ir_graph *irg = get_irn_irg(n);
5662 return new_r_Unknown(irg, mode);
5665 if (mode_is_reference(mode) &&
5666 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5668 ir_node *l = get_Add_left(a);
5669 ir_node *r = get_Add_right(a);
5670 dbg_info *dbgi = get_irn_dbg_info(a);
5671 ir_node *block = get_nodes_block(n);
5673 ir_node *lop = get_Conv_op(l);
5674 if (get_irn_mode(lop) == mode) {
5675 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5676 n = new_rd_Add(dbgi, block, lop, r, mode);
5681 ir_node *rop = get_Conv_op(r);
5682 if (get_irn_mode(rop) == mode) {
5683 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5684 n = new_rd_Add(dbgi, block, l, rop, mode);
5691 } /* transform_node_Conv */
5694 * Remove dead blocks and nodes in dead blocks
5695 * in keep alive list. We do not generate a new End node.
5697 static ir_node *transform_node_End(ir_node *n)
5699 int i, j, n_keepalives = get_End_n_keepalives(n);
5702 NEW_ARR_A(ir_node *, in, n_keepalives);
5704 for (i = j = 0; i < n_keepalives; ++i) {
5705 ir_node *ka = get_End_keepalive(n, i);
5707 if (! is_Block_dead(ka)) {
5711 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5713 } else if (is_Bad(ka)) {
5714 /* no need to keep Bad */
5719 if (j != n_keepalives)
5720 set_End_keepalives(n, j, in);
5722 } /* transform_node_End */
5724 bool is_negated_value(ir_node *a, ir_node *b)
5726 if (is_Minus(a) && get_Minus_op(a) == b)
5728 if (is_Minus(b) && get_Minus_op(b) == a)
5730 if (is_Sub(a) && is_Sub(b)) {
5731 ir_node *a_left = get_Sub_left(a);
5732 ir_node *a_right = get_Sub_right(a);
5733 ir_node *b_left = get_Sub_left(b);
5734 ir_node *b_right = get_Sub_right(b);
5736 if (a_left == b_right && a_right == b_left)
5744 * Optimize a Mux into some simpler cases.
5746 static ir_node *transform_node_Mux(ir_node *n)
5748 ir_node *oldn = n, *sel = get_Mux_sel(n);
5749 ir_mode *mode = get_irn_mode(n);
5750 ir_node *t = get_Mux_true(n);
5751 ir_node *f = get_Mux_false(n);
5752 ir_graph *irg = get_irn_irg(n);
5754 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5758 ir_node* block = get_nodes_block(n);
5760 ir_node* c1 = get_Mux_sel(t);
5761 ir_node* t1 = get_Mux_true(t);
5762 ir_node* f1 = get_Mux_false(t);
5764 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5765 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5766 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5771 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5772 } else if (f == t1) {
5773 /* Mux(cond0, Mux(cond1, x, y), x) */
5774 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5775 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5776 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5781 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5783 } else if (is_Mux(f)) {
5784 ir_node* block = get_nodes_block(n);
5786 ir_node* c1 = get_Mux_sel(f);
5787 ir_node* t1 = get_Mux_true(f);
5788 ir_node* f1 = get_Mux_false(f);
5790 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5791 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5792 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5797 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5798 } else if (t == f1) {
5799 /* Mux(cond0, x, Mux(cond1, y, x)) */
5800 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5801 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5802 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5807 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5811 /* first normalization step: try to move a constant to the false side,
5812 * 0 preferred on false side too */
5814 ir_node *cmp = get_Proj_pred(sel);
5816 if (is_Cmp(cmp) && is_Const(t) &&
5817 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5818 pn_Cmp pnc = get_Proj_proj(sel);
5823 /* Mux(x, a, b) => Mux(not(x), b, a) */
5824 sel = new_r_Proj(cmp, mode_b,
5825 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5826 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5830 /* note: after normalization, false can only happen on default */
5831 if (mode == mode_b) {
5832 dbg_info *dbg = get_irn_dbg_info(n);
5833 ir_node *block = get_nodes_block(n);
5836 tarval *tv_t = get_Const_tarval(t);
5837 if (tv_t == tarval_b_true) {
5839 /* Muxb(sel, true, false) = sel */
5840 assert(get_Const_tarval(f) == tarval_b_false);
5841 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5844 /* Muxb(sel, true, x) = Or(sel, x) */
5845 n = new_rd_Or(dbg, block, sel, f, mode_b);
5846 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5850 } else if (is_Const(f)) {
5851 tarval *tv_f = get_Const_tarval(f);
5852 if (tv_f == tarval_b_true) {
5853 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5854 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5855 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5856 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5859 /* Muxb(sel, x, false) = And(sel, x) */
5860 assert(tv_f == tarval_b_false);
5861 n = new_rd_And(dbg, block, sel, t, mode_b);
5862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5868 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5869 * value to integer. */
5870 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5871 tarval *a = get_Const_tarval(t);
5872 tarval *b = get_Const_tarval(f);
5874 if (tarval_is_one(a) && tarval_is_null(b)) {
5875 ir_node *block = get_nodes_block(n);
5876 ir_node *conv = new_r_Conv(block, sel, mode);
5878 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5880 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5881 ir_node *block = get_nodes_block(n);
5882 ir_node *not_ = new_r_Not(block, sel, mode_b);
5883 ir_node *conv = new_r_Conv(block, not_, mode);
5885 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5891 ir_node *cmp = get_Proj_pred(sel);
5892 long pn = get_Proj_proj(sel);
5895 * Note: normalization puts the constant on the right side,
5896 * so we check only one case.
5899 ir_node *cmp_r = get_Cmp_right(cmp);
5900 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5901 ir_node *block = get_nodes_block(n);
5902 ir_node *cmp_l = get_Cmp_left(cmp);
5904 if (mode_is_int(mode)) {
5906 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5907 /* Mux((a & b) != 0, c, 0) */
5908 ir_node *and_r = get_And_right(cmp_l);
5911 if (and_r == t && f == cmp_r) {
5912 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5913 if (pn == pn_Cmp_Lg) {
5914 /* Mux((a & 2^C) != 0, 2^C, 0) */
5916 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5918 /* Mux((a & 2^C) == 0, 2^C, 0) */
5919 n = new_rd_Eor(get_irn_dbg_info(n),
5920 block, cmp_l, t, mode);
5921 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5926 if (is_Shl(and_r)) {
5927 ir_node *shl_l = get_Shl_left(and_r);
5928 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5929 if (and_r == t && f == cmp_r) {
5930 if (pn == pn_Cmp_Lg) {
5931 /* (a & (1 << n)) != 0, (1 << n), 0) */
5933 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5935 /* (a & (1 << n)) == 0, (1 << n), 0) */
5936 n = new_rd_Eor(get_irn_dbg_info(n),
5937 block, cmp_l, t, mode);
5938 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5944 and_l = get_And_left(cmp_l);
5945 if (is_Shl(and_l)) {
5946 ir_node *shl_l = get_Shl_left(and_l);
5947 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5948 if (and_l == t && f == cmp_r) {
5949 if (pn == pn_Cmp_Lg) {
5950 /* ((1 << n) & a) != 0, (1 << n), 0) */
5952 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5954 /* ((1 << n) & a) == 0, (1 << n), 0) */
5955 n = new_rd_Eor(get_irn_dbg_info(n),
5956 block, cmp_l, t, mode);
5957 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5970 } /* transform_node_Mux */
5973 * optimize Sync nodes that have other syncs as input we simply add the inputs
5974 * of the other sync to our own inputs
5976 static ir_node *transform_node_Sync(ir_node *n)
5978 int arity = get_Sync_n_preds(n);
5981 for (i = 0; i < arity;) {
5982 ir_node *pred = get_Sync_pred(n, i);
5986 if (!is_Sync(pred)) {
5994 pred_arity = get_Sync_n_preds(pred);
5995 for (j = 0; j < pred_arity; ++j) {
5996 ir_node *pred_pred = get_Sync_pred(pred, j);
6001 add_irn_n(n, pred_pred);
6005 if (get_Sync_pred(n, k) == pred_pred) break;
6010 /* rehash the sync node */
6014 } /* transform_node_Sync */
6017 * optimize a trampoline Call into a direct Call
6019 static ir_node *transform_node_Call(ir_node *call)
6021 ir_node *callee = get_Call_ptr(call);
6022 ir_node *adr, *mem, *res, *bl, **in;
6023 ir_type *ctp, *mtp, *tp;
6027 int i, n_res, n_param;
6030 if (! is_Proj(callee))
6032 callee = get_Proj_pred(callee);
6033 if (! is_Builtin(callee))
6035 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6038 mem = get_Call_mem(call);
6040 if (skip_Proj(mem) == callee) {
6041 /* memory is routed to the trampoline, skip */
6042 mem = get_Builtin_mem(callee);
6045 /* build a new call type */
6046 mtp = get_Call_type(call);
6047 tdb = get_type_dbg_info(mtp);
6049 n_res = get_method_n_ress(mtp);
6050 n_param = get_method_n_params(mtp);
6051 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6053 for (i = 0; i < n_res; ++i)
6054 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6056 NEW_ARR_A(ir_node *, in, n_param + 1);
6058 /* FIXME: we don't need a new pointer type in every step */
6059 irg = get_irn_irg(call);
6060 tp = get_irg_frame_type(irg);
6061 tp = new_type_pointer(tp);
6062 set_method_param_type(ctp, 0, tp);
6064 in[0] = get_Builtin_param(callee, 2);
6065 for (i = 0; i < n_param; ++i) {
6066 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6067 in[i + 1] = get_Call_param(call, i);
6069 var = get_method_variadicity(mtp);
6070 set_method_variadicity(ctp, var);
6071 if (var == variadicity_variadic) {
6072 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6074 /* When we resolve a trampoline, the function must be called by a this-call */
6075 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6076 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6078 adr = get_Builtin_param(callee, 1);
6080 db = get_irn_dbg_info(call);
6081 bl = get_nodes_block(call);
6083 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6084 if (get_irn_pinned(call) == op_pin_state_floats)
6085 set_irn_pinned(res, op_pin_state_floats);
6087 } /* transform_node_Call */
6090 * Tries several [inplace] [optimizing] transformations and returns an
6091 * equivalent node. The difference to equivalent_node() is that these
6092 * transformations _do_ generate new nodes, and thus the old node must
6093 * not be freed even if the equivalent node isn't the old one.
6095 static ir_node *transform_node(ir_node *n)
6100 * Transform_node is the only "optimizing transformation" that might
6101 * return a node with a different opcode. We iterate HERE until fixpoint
6102 * to get the final result.
6106 if (n->op->ops.transform_node != NULL)
6107 n = n->op->ops.transform_node(n);
6108 } while (oldn != n);
6111 } /* transform_node */
6114 * Sets the default transform node operation for an ir_op_ops.
6116 * @param code the opcode for the default operation
6117 * @param ops the operations initialized
6122 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6126 ops->transform_node = transform_node_##a; \
6128 #define CASE_PROJ(a) \
6130 ops->transform_node_Proj = transform_node_Proj_##a; \
6132 #define CASE_PROJ_EX(a) \
6134 ops->transform_node = transform_node_##a; \
6135 ops->transform_node_Proj = transform_node_Proj_##a; \
6144 CASE_PROJ_EX(DivMod);
6178 } /* firm_set_default_transform_node */
6181 /* **************** Common Subexpression Elimination **************** */
6183 /** The size of the hash table used, should estimate the number of nodes
6185 #define N_IR_NODES 512
6187 /** Compares the attributes of two Const nodes. */
6188 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6190 return (get_Const_tarval(a) != get_Const_tarval(b))
6191 || (get_Const_type(a) != get_Const_type(b));
6192 } /* node_cmp_attr_Const */
6194 /** Compares the attributes of two Proj nodes. */
6195 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6197 return a->attr.proj != b->attr.proj;
6198 } /* node_cmp_attr_Proj */
6200 /** Compares the attributes of two Alloc nodes. */
6201 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6203 const alloc_attr *pa = &a->attr.alloc;
6204 const alloc_attr *pb = &b->attr.alloc;
6205 return (pa->where != pb->where) || (pa->type != pb->type);
6206 } /* node_cmp_attr_Alloc */
6208 /** Compares the attributes of two Free nodes. */
6209 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6211 const free_attr *pa = &a->attr.free;
6212 const free_attr *pb = &b->attr.free;
6213 return (pa->where != pb->where) || (pa->type != pb->type);
6214 } /* node_cmp_attr_Free */
6216 /** Compares the attributes of two SymConst nodes. */
6217 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6219 const symconst_attr *pa = &a->attr.symc;
6220 const symconst_attr *pb = &b->attr.symc;
6221 return (pa->kind != pb->kind)
6222 || (pa->sym.type_p != pb->sym.type_p)
6223 || (pa->tp != pb->tp);
6224 } /* node_cmp_attr_SymConst */
6226 /** Compares the attributes of two Call nodes. */
6227 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6229 const call_attr *pa = &a->attr.call;
6230 const call_attr *pb = &b->attr.call;
6231 return (pa->type != pb->type)
6232 || (pa->tail_call != pb->tail_call);
6233 } /* node_cmp_attr_Call */
6235 /** Compares the attributes of two Sel nodes. */
6236 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6238 const ir_entity *a_ent = get_Sel_entity(a);
6239 const ir_entity *b_ent = get_Sel_entity(b);
6240 return a_ent != b_ent;
6241 } /* node_cmp_attr_Sel */
6243 /** Compares the attributes of two Phi nodes. */
6244 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6246 /* we can only enter this function if both nodes have the same number of inputs,
6247 hence it is enough to check if one of them is a Phi0 */
6249 /* check the Phi0 pos attribute */
6250 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6253 } /* node_cmp_attr_Phi */
6255 /** Compares the attributes of two Conv nodes. */
6256 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6258 return get_Conv_strict(a) != get_Conv_strict(b);
6259 } /* node_cmp_attr_Conv */
6261 /** Compares the attributes of two Cast nodes. */
6262 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6264 return get_Cast_type(a) != get_Cast_type(b);
6265 } /* node_cmp_attr_Cast */
6267 /** Compares the attributes of two Load nodes. */
6268 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6270 if (get_Load_volatility(a) == volatility_is_volatile ||
6271 get_Load_volatility(b) == volatility_is_volatile)
6272 /* NEVER do CSE on volatile Loads */
6274 /* do not CSE Loads with different alignment. Be conservative. */
6275 if (get_Load_align(a) != get_Load_align(b))
6278 return get_Load_mode(a) != get_Load_mode(b);
6279 } /* node_cmp_attr_Load */
6281 /** Compares the attributes of two Store nodes. */
6282 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6284 /* do not CSE Stores with different alignment. Be conservative. */
6285 if (get_Store_align(a) != get_Store_align(b))
6288 /* NEVER do CSE on volatile Stores */
6289 return (get_Store_volatility(a) == volatility_is_volatile ||
6290 get_Store_volatility(b) == volatility_is_volatile);
6291 } /* node_cmp_attr_Store */
6293 /** Compares two exception attributes */
6294 static int node_cmp_exception(ir_node *a, ir_node *b)
6296 const except_attr *ea = &a->attr.except;
6297 const except_attr *eb = &b->attr.except;
6299 return ea->pin_state != eb->pin_state;
6302 #define node_cmp_attr_Bound node_cmp_exception
6304 /** Compares the attributes of two Div nodes. */
6305 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6307 const divmod_attr *ma = &a->attr.divmod;
6308 const divmod_attr *mb = &b->attr.divmod;
6309 return ma->exc.pin_state != mb->exc.pin_state ||
6310 ma->resmode != mb->resmode ||
6311 ma->no_remainder != mb->no_remainder;
6312 } /* node_cmp_attr_Div */
6314 /** Compares the attributes of two DivMod nodes. */
6315 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6317 const divmod_attr *ma = &a->attr.divmod;
6318 const divmod_attr *mb = &b->attr.divmod;
6319 return ma->exc.pin_state != mb->exc.pin_state ||
6320 ma->resmode != mb->resmode;
6321 } /* node_cmp_attr_DivMod */
6323 /** Compares the attributes of two Mod nodes. */
6324 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6326 return node_cmp_attr_DivMod(a, b);
6327 } /* node_cmp_attr_Mod */
6329 /** Compares the attributes of two Quot nodes. */
6330 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6332 return node_cmp_attr_DivMod(a, b);
6333 } /* node_cmp_attr_Quot */
6335 /** Compares the attributes of two Confirm nodes. */
6336 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6338 /* no need to compare the bound, as this is a input */
6339 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6340 } /* node_cmp_attr_Confirm */
6342 /** Compares the attributes of two Builtin nodes. */
6343 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6345 /* no need to compare the type, equal kind means equal type */
6346 return get_Builtin_kind(a) != get_Builtin_kind(b);
6347 } /* node_cmp_attr_Builtin */
6349 /** Compares the attributes of two ASM nodes. */
6350 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6353 const ir_asm_constraint *ca;
6354 const ir_asm_constraint *cb;
6357 if (get_ASM_text(a) != get_ASM_text(b))
6360 /* Should we really check the constraints here? Should be better, but is strange. */
6361 n = get_ASM_n_input_constraints(a);
6362 if (n != get_ASM_n_input_constraints(b))
6365 ca = get_ASM_input_constraints(a);
6366 cb = get_ASM_input_constraints(b);
6367 for (i = 0; i < n; ++i) {
6368 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6372 n = get_ASM_n_output_constraints(a);
6373 if (n != get_ASM_n_output_constraints(b))
6376 ca = get_ASM_output_constraints(a);
6377 cb = get_ASM_output_constraints(b);
6378 for (i = 0; i < n; ++i) {
6379 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6383 n = get_ASM_n_clobbers(a);
6384 if (n != get_ASM_n_clobbers(b))
6387 cla = get_ASM_clobbers(a);
6388 clb = get_ASM_clobbers(b);
6389 for (i = 0; i < n; ++i) {
6390 if (cla[i] != clb[i])
6394 } /* node_cmp_attr_ASM */
6396 /** Compares the inexistent attributes of two Dummy nodes. */
6397 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6405 * Set the default node attribute compare operation for an ir_op_ops.
6407 * @param code the opcode for the default operation
6408 * @param ops the operations initialized
6413 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6417 ops->node_cmp_attr = node_cmp_attr_##a; \
6450 } /* firm_set_default_node_cmp_attr */
6453 * Compare function for two nodes in the value table. Gets two
6454 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6456 int identities_cmp(const void *elt, const void *key)
6458 ir_node *a = (ir_node *)elt;
6459 ir_node *b = (ir_node *)key;
6462 if (a == b) return 0;
6464 if ((get_irn_op(a) != get_irn_op(b)) ||
6465 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6467 /* compare if a's in and b's in are of equal length */
6468 irn_arity_a = get_irn_arity(a);
6469 if (irn_arity_a != get_irn_arity(b))
6472 /* blocks are never the same */
6476 if (get_irn_pinned(a) == op_pin_state_pinned) {
6477 /* for pinned nodes, the block inputs must be equal */
6478 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6480 } else if (! get_opt_global_cse()) {
6481 /* for block-local CSE both nodes must be in the same Block */
6482 if (get_nodes_block(a) != get_nodes_block(b))
6486 /* compare a->in[0..ins] with b->in[0..ins] */
6487 for (i = 0; i < irn_arity_a; ++i) {
6488 ir_node *pred_a = get_irn_n(a, i);
6489 ir_node *pred_b = get_irn_n(b, i);
6490 if (pred_a != pred_b) {
6491 /* if both predecessors are CSE neutral they might be different */
6492 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6498 * here, we already now that the nodes are identical except their
6501 if (a->op->ops.node_cmp_attr)
6502 return a->op->ops.node_cmp_attr(a, b);
6505 } /* identities_cmp */
6508 * Calculate a hash value of a node.
6510 * @param node The IR-node
6512 unsigned ir_node_hash(const ir_node *node)
6514 return node->op->ops.hash(node);
6515 } /* ir_node_hash */
6518 void new_identities(ir_graph *irg)
6520 if (irg->value_table != NULL)
6521 del_pset(irg->value_table);
6522 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6523 } /* new_identities */
6525 void del_identities(ir_graph *irg)
6527 if (irg->value_table != NULL)
6528 del_pset(irg->value_table);
6529 } /* del_identities */
6531 /* Normalize a node by putting constants (and operands with larger
6532 * node index) on the right (operator side). */
6533 void ir_normalize_node(ir_node *n)
6535 if (is_op_commutative(get_irn_op(n))) {
6536 ir_node *l = get_binop_left(n);
6537 ir_node *r = get_binop_right(n);
6539 /* For commutative operators perform a OP b == b OP a but keep
6540 * constants on the RIGHT side. This helps greatly in some
6541 * optimizations. Moreover we use the idx number to make the form
6543 if (!operands_are_normalized(l, r)) {
6544 set_binop_left(n, r);
6545 set_binop_right(n, l);
6549 } /* ir_normalize_node */
6552 * Return the canonical node computing the same value as n.
6553 * Looks up the node in a hash table, enters it in the table
6554 * if it isn't there yet.
6556 * @param n the node to look up
6558 * @return a node that computes the same value as n or n if no such
6559 * node could be found
6561 ir_node *identify_remember(ir_node *n)
6563 ir_graph *irg = get_irn_irg(n);
6564 pset *value_table = irg->value_table;
6567 if (value_table == NULL)
6570 ir_normalize_node(n);
6571 /* lookup or insert in hash table with given hash key. */
6572 nn = pset_insert(value_table, n, ir_node_hash(n));
6575 /* n is reachable again */
6576 edges_node_revival(nn, get_irn_irg(nn));
6580 } /* identify_remember */
6583 * During construction we set the op_pin_state_pinned flag in the graph right
6584 * when the optimization is performed. The flag turning on procedure global
6585 * cse could be changed between two allocations. This way we are safe.
6587 * @param n The node to lookup
6589 static inline ir_node *identify_cons(ir_node *n)
6593 n = identify_remember(n);
6594 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6595 ir_graph *irg = get_irn_irg(n);
6596 set_irg_pinned(irg, op_pin_state_floats);
6599 } /* identify_cons */
6601 /* Add a node to the identities value table. */
6602 void add_identities(ir_node *node)
6609 identify_remember(node);
6612 /* Visit each node in the value table of a graph. */
6613 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6616 ir_graph *rem = current_ir_graph;
6618 current_ir_graph = irg;
6619 foreach_pset(irg->value_table, node) {
6622 current_ir_graph = rem;
6623 } /* visit_all_identities */
6626 * Garbage in, garbage out. If a node has a dead input, i.e., the
6627 * Bad node is input to the node, return the Bad node.
6629 static ir_node *gigo(ir_node *node)
6632 ir_op *op = get_irn_op(node);
6634 /* remove garbage blocks by looking at control flow that leaves the block
6635 and replacing the control flow by Bad. */
6636 if (get_irn_mode(node) == mode_X) {
6637 ir_node *block = get_nodes_block(skip_Proj(node));
6638 ir_graph *irg = get_irn_irg(block);
6640 /* Don't optimize nodes in immature blocks. */
6641 if (!get_Block_matured(block))
6643 /* Don't optimize End, may have Bads. */
6644 if (op == op_End) return node;
6646 if (is_Block(block)) {
6647 if (is_Block_dead(block)) {
6648 /* control flow from dead block is dead */
6649 return new_r_Bad(irg);
6652 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6653 if (!is_Bad(get_irn_n(block, i)))
6657 ir_graph *irg = get_irn_irg(block);
6658 /* the start block is never dead */
6659 if (block != get_irg_start_block(irg)
6660 && block != get_irg_end_block(irg)) {
6662 * Do NOT kill control flow without setting
6663 * the block to dead of bad things can happen:
6664 * We get a Block that is not reachable be irg_block_walk()
6665 * but can be found by irg_walk()!
6667 set_Block_dead(block);
6668 return new_r_Bad(irg);
6674 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6675 blocks predecessors is dead. */
6676 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6677 ir_graph *irg = get_irn_irg(node);
6678 irn_arity = get_irn_arity(node);
6681 * Beware: we can only read the block of a non-floating node.
6683 if (is_irn_pinned_in_irg(node) &&
6684 is_Block_dead(get_nodes_block(skip_Proj(node))))
6685 return new_r_Bad(irg);
6687 for (i = 0; i < irn_arity; i++) {
6688 ir_node *pred = get_irn_n(node, i);
6691 return new_r_Bad(irg);
6693 /* Propagating Unknowns here seems to be a bad idea, because
6694 sometimes we need a node as a input and did not want that
6696 However, it might be useful to move this into a later phase
6697 (if you think that optimizing such code is useful). */
6698 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6699 return new_r_Unknown(irg, get_irn_mode(node));
6704 /* With this code we violate the agreement that local_optimize
6705 only leaves Bads in Block, Phi and Tuple nodes. */
6706 /* If Block has only Bads as predecessors it's garbage. */
6707 /* If Phi has only Bads as predecessors it's garbage. */
6708 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6709 irn_arity = get_irn_arity(node);
6710 for (i = 0; i < irn_arity; i++) {
6711 if (!is_Bad(get_irn_n(node, i))) break;
6713 if (i == irn_arity) node = new_r_Bad(irg);
6720 * These optimizations deallocate nodes from the obstack.
6721 * It can only be called if it is guaranteed that no other nodes
6722 * reference this one, i.e., right after construction of a node.
6724 * @param n The node to optimize
6726 ir_node *optimize_node(ir_node *n)
6730 ir_graph *irg = get_irn_irg(n);
6731 ir_opcode iro = get_irn_opcode(n);
6733 /* Always optimize Phi nodes: part of the construction. */
6734 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6736 /* constant expression evaluation / constant folding */
6737 if (get_opt_constant_folding()) {
6738 /* neither constants nor Tuple values can be evaluated */
6739 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6740 /* try to evaluate */
6741 tv = computed_value(n);
6742 if (tv != tarval_bad) {
6744 ir_type *old_tp = get_irn_type(n);
6745 int i, arity = get_irn_arity(n);
6749 * Try to recover the type of the new expression.
6751 for (i = 0; i < arity && !old_tp; ++i)
6752 old_tp = get_irn_type(get_irn_n(n, i));
6755 * we MUST copy the node here temporary, because it's still needed
6756 * for DBG_OPT_CSTEVAL
6758 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6759 oldn = alloca(node_size);
6761 memcpy(oldn, n, node_size);
6762 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6764 /* ARG, copy the in array, we need it for statistics */
6765 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6767 /* note the inplace edges module */
6768 edges_node_deleted(n, irg);
6770 /* evaluation was successful -- replace the node. */
6771 irg_kill_node(irg, n);
6772 nw = new_r_Const(irg, tv);
6774 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6775 set_Const_type(nw, old_tp);
6776 DBG_OPT_CSTEVAL(oldn, nw);
6782 /* remove unnecessary nodes */
6783 if (get_opt_algebraic_simplification() ||
6784 (iro == iro_Phi) || /* always optimize these nodes. */
6786 (iro == iro_Proj) ||
6787 (iro == iro_Block) ) /* Flags tested local. */
6788 n = equivalent_node(n);
6790 /* Common Subexpression Elimination.
6792 * Checks whether n is already available.
6793 * The block input is used to distinguish different subexpressions. Right
6794 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6795 * subexpressions within a block.
6798 n = identify_cons(n);
6801 edges_node_deleted(oldn, irg);
6803 /* We found an existing, better node, so we can deallocate the old node. */
6804 irg_kill_node(irg, oldn);
6808 /* Some more constant expression evaluation that does not allow to
6810 iro = get_irn_opcode(n);
6811 if (get_opt_algebraic_simplification() ||
6812 (iro == iro_Cond) ||
6813 (iro == iro_Proj)) /* Flags tested local. */
6814 n = transform_node(n);
6816 /* Remove nodes with dead (Bad) input.
6817 Run always for transformation induced Bads. */
6820 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6821 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6823 n = identify_remember(o);
6829 } /* optimize_node */
6833 * These optimizations never deallocate nodes (in place). This can cause dead
6834 * nodes lying on the obstack. Remove these by a dead node elimination,
6835 * i.e., a copying garbage collection.
6837 ir_node *optimize_in_place_2(ir_node *n)
6841 ir_opcode iro = get_irn_opcode(n);
6843 if (!get_opt_optimize() && !is_Phi(n)) return n;
6845 if (iro == iro_Deleted)
6848 /* constant expression evaluation / constant folding */
6849 if (get_opt_constant_folding()) {
6850 /* neither constants nor Tuple values can be evaluated */
6851 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6852 /* try to evaluate */
6853 tv = computed_value(n);
6854 if (tv != tarval_bad) {
6855 /* evaluation was successful -- replace the node. */
6856 ir_type *old_tp = get_irn_type(n);
6857 ir_graph *irg = get_irn_irg(n);
6858 int i, arity = get_irn_arity(n);
6861 * Try to recover the type of the new expression.
6863 for (i = 0; i < arity && !old_tp; ++i)
6864 old_tp = get_irn_type(get_irn_n(n, i));
6866 n = new_r_Const(irg, tv);
6868 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6869 set_Const_type(n, old_tp);
6871 DBG_OPT_CSTEVAL(oldn, n);
6877 /* remove unnecessary nodes */
6878 if (get_opt_constant_folding() ||
6879 (iro == iro_Phi) || /* always optimize these nodes. */
6880 (iro == iro_Id) || /* ... */
6881 (iro == iro_Proj) || /* ... */
6882 (iro == iro_Block) ) /* Flags tested local. */
6883 n = equivalent_node(n);
6885 /** common subexpression elimination **/
6886 /* Checks whether n is already available. */
6887 /* The block input is used to distinguish different subexpressions. Right
6888 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6889 subexpressions within a block. */
6890 if (get_opt_cse()) {
6892 n = identify_remember(o);
6897 /* Some more constant expression evaluation. */
6898 iro = get_irn_opcode(n);
6899 if (get_opt_constant_folding() ||
6900 (iro == iro_Cond) ||
6901 (iro == iro_Proj)) /* Flags tested local. */
6902 n = transform_node(n);
6904 /* Remove nodes with dead (Bad) input.
6905 Run always for transformation induced Bads. */
6908 /* Now we can verify the node, as it has no dead inputs any more. */
6911 /* Now we have a legal, useful node. Enter it in hash table for cse.
6912 Blocks should be unique anyways. (Except the successor of start:
6913 is cse with the start block!) */
6914 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6916 n = identify_remember(o);
6922 } /* optimize_in_place_2 */
6925 * Wrapper for external use, set proper status bits after optimization.
6927 ir_node *optimize_in_place(ir_node *n)
6929 ir_graph *irg = get_irn_irg(n);
6930 /* Handle graph state */
6931 assert(get_irg_phase_state(irg) != phase_building);
6933 if (get_opt_global_cse())
6934 set_irg_pinned(irg, op_pin_state_floats);
6935 if (get_irg_outs_state(irg) == outs_consistent)
6936 set_irg_outs_inconsistent(irg);
6938 /* FIXME: Maybe we could also test whether optimizing the node can
6939 change the control graph. */
6940 set_irg_doms_inconsistent(irg);
6941 return optimize_in_place_2(n);
6942 } /* optimize_in_place */
6945 * Calculate a hash value of a Const node.
6947 static unsigned hash_Const(const ir_node *node)
6951 /* special value for const, as they only differ in their tarval. */
6952 h = HASH_PTR(node->attr.con.tarval);
6958 * Calculate a hash value of a SymConst node.
6960 static unsigned hash_SymConst(const ir_node *node)
6964 /* all others are pointers */
6965 h = HASH_PTR(node->attr.symc.sym.type_p);
6968 } /* hash_SymConst */
6971 * Set the default hash operation in an ir_op_ops.
6973 * @param code the opcode for the default operation
6974 * @param ops the operations initialized
6979 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6983 ops->hash = hash_##a; \
6986 /* hash function already set */
6987 if (ops->hash != NULL)
6994 /* use input/mode default hash if no function was given */
6995 ops->hash = firm_default_hash;
7003 * Sets the default operation for an ir_ops.
7005 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
7007 ops = firm_set_default_hash(code, ops);
7008 ops = firm_set_default_computed_value(code, ops);
7009 ops = firm_set_default_equivalent_node(code, ops);
7010 ops = firm_set_default_transform_node(code, ops);
7011 ops = firm_set_default_node_cmp_attr(code, ops);
7012 ops = firm_set_default_get_type(code, ops);
7013 ops = firm_set_default_get_type_attr(code, ops);
7014 ops = firm_set_default_get_entity_attr(code, ops);
7017 } /* firm_set_default_operations */